18
18
//!
19
19
//! [Click here for Zenoh's documentation](https://docs.rs/zenoh/latest/zenoh)
20
20
mod adminspace;
21
- #[ cfg( target_os = "linux" ) ]
22
- mod netlink;
23
21
pub mod orchestrator;
24
22
mod scouting;
25
23
26
24
#[ cfg( feature = "plugins" ) ]
27
25
use std:: sync:: { Mutex , MutexGuard } ;
28
- #[ cfg( target_os = "linux" ) ]
29
- use std:: time:: Duration ;
30
26
use std:: {
31
27
any:: Any ,
32
28
collections:: HashSet ,
33
- net:: IpAddr ,
34
29
sync:: {
35
30
atomic:: { AtomicU32 , Ordering } ,
36
31
Arc , Weak ,
37
32
} ,
33
+ time:: Duration ,
38
34
} ;
39
35
40
36
pub use adminspace:: AdminSpace ;
41
37
use async_trait:: async_trait;
42
38
use futures:: { stream:: StreamExt , Future } ;
43
- #[ cfg( target_os = "linux" ) ]
44
- use rtnetlink:: packet_route:: {
45
- address:: { AddressAttribute , AddressMessage } ,
46
- RouteNetlinkMessage ,
47
- } ;
48
39
pub use scouting:: Scouting ;
49
40
use tokio:: task:: JoinHandle ;
50
41
use tokio_util:: sync:: CancellationToken ;
@@ -67,7 +58,7 @@ use zenoh_transport::{
67
58
multicast:: TransportMulticast , unicast:: TransportUnicast , TransportEventHandler ,
68
59
TransportManager , TransportMulticastEventHandler , TransportPeer , TransportPeerEventHandler ,
69
60
} ;
70
- #[ cfg( target_os = "linux" ) ]
61
+ #[ cfg( unix ) ]
71
62
use zenoh_util:: net:: update_iface_cache;
72
63
73
64
use self :: orchestrator:: StartConditions ;
@@ -86,9 +77,6 @@ use crate::{
86
77
GIT_VERSION , LONG_VERSION ,
87
78
} ;
88
79
89
- #[ cfg( target_os = "linux" ) ]
90
- const NETLINK_TIMEOUT : Duration = Duration :: from_millis ( 500 ) ;
91
-
92
80
pub ( crate ) struct RuntimeState {
93
81
zid : ZenohId ,
94
82
whatami : WhatAmI ,
@@ -259,11 +247,9 @@ impl RuntimeBuilder {
259
247
zenoh_config:: ShmInitMode :: Lazy => { }
260
248
} ;
261
249
262
- #[ cfg( target_os = "linux" ) ]
263
250
runtime. spawn ( {
264
- let netlink_monitor = netlink:: NetlinkMonitor :: new ( ) ?;
265
251
let runtime2 = runtime. clone ( ) ;
266
- async move { runtime2. monitor_netlink_socket ( netlink_monitor ) . await }
252
+ async move { runtime2. monitor_available_addrs ( ) . await }
267
253
} ) ;
268
254
269
255
Ok ( runtime)
@@ -393,91 +379,31 @@ impl Runtime {
393
379
self . state . pending_connections . lock ( ) . await . remove ( zid)
394
380
}
395
381
396
- #[ cfg( target_os = "linux" ) ]
397
- async fn monitor_netlink_socket ( & self , mut netlink : netlink:: NetlinkMonitor ) {
398
- fn add_addr_to_set (
399
- message : & AddressMessage ,
400
- new : bool ,
401
- new_addresses : & mut HashSet < IpAddr > ,
402
- del_addresses : & mut HashSet < IpAddr > ,
403
- ) {
404
- if let Some ( addr) = get_relevant_address ( message) {
405
- if new {
406
- new_addresses. insert ( * addr) ;
407
- del_addresses. remove ( addr) ;
408
- } else {
409
- del_addresses. insert ( * addr) ;
410
- new_addresses. remove ( addr) ;
411
- }
412
- }
413
- }
414
-
382
+ async fn monitor_available_addrs ( & self ) {
415
383
let token = self . get_cancellation_token ( ) ;
384
+ let update_interval = Duration :: from_secs ( 10 ) ;
416
385
loop {
417
- let mut new_addresses = HashSet :: < IpAddr > :: new ( ) ;
418
- let mut old_addresses = HashSet :: < IpAddr > :: new ( ) ;
419
386
tokio:: select! {
420
- message = netlink. next( ) => {
421
- tracing:: trace!( "NETLINK message: {:?}" , message) ;
422
- match & message {
423
- Some ( RouteNetlinkMessage :: NewAddress ( msg) ) => add_addr_to_set( msg, true , & mut new_addresses, & mut old_addresses) ,
424
- Some ( RouteNetlinkMessage :: DelAddress ( msg) ) => add_addr_to_set( msg, false , & mut new_addresses, & mut old_addresses) ,
425
- Some ( _) => ( ) ,
426
- None => { break ; }
427
- }
428
- } ,
429
- _ = token. cancelled( ) => { return }
430
- }
431
-
432
- // Same, but with a timeout so we collect multiple netlink messages
433
- // and restart the scouting routine only one time.
434
- loop {
435
- tokio:: select! {
436
- message = netlink. next( ) => {
437
- tracing:: trace!( "NETLINK message: {:?}" , message) ;
438
- match & message {
439
- Some ( RouteNetlinkMessage :: NewAddress ( msg) ) => add_addr_to_set( msg, true , & mut new_addresses, & mut old_addresses) ,
440
- Some ( RouteNetlinkMessage :: DelAddress ( msg) ) => add_addr_to_set( msg, false , & mut new_addresses, & mut old_addresses) ,
441
- Some ( _) => ( ) ,
442
- None => { break ; }
443
- }
444
- } ,
445
- _ = tokio:: time:: sleep( NETLINK_TIMEOUT ) => { break ; }
446
- _ = token. cancelled( ) => { return }
447
- }
387
+ _ = tokio:: time:: sleep( update_interval) => self . update_available_addrs( ) . await ,
388
+ _ = token. cancelled( ) => return ,
448
389
}
390
+ }
391
+ }
449
392
450
- update_iface_cache ( ) ;
451
- self . update_locators ( ) ;
393
+ async fn update_available_addrs ( & self ) {
394
+ #[ cfg( unix) ]
395
+ update_iface_cache ( ) ;
452
396
453
- {
454
- let tables_lock = & self . state . router . tables ;
455
- let _ctrl_lock = zlock ! ( tables_lock. ctrl_lock) ;
456
- let mut tables = zwrite ! ( tables_lock. tables) ;
457
- tables_lock. hat_code . update_self_locators ( & mut tables) ;
458
- }
397
+ if self . update_locators ( ) {
398
+ let tables_lock = & self . state . router . tables ;
399
+ let _ctrl_lock = zlock ! ( tables_lock. ctrl_lock) ;
400
+ let mut tables = zwrite ! ( tables_lock. tables) ;
401
+ tables_lock. hat_code . update_self_locators ( & mut tables) ;
402
+ }
459
403
460
- let scouting = self . state . scouting . lock ( ) . await ;
461
- if let Some ( scouting) = scouting. as_ref ( ) {
462
- let new_addresses = new_addresses. drain ( ) . collect :: < Vec < _ > > ( ) ;
463
- let old_addresses = old_addresses. drain ( ) . collect :: < Vec < _ > > ( ) ;
464
- tracing:: debug!(
465
- "Update scouting addresses with +{:?}, -{:?}" ,
466
- new_addresses,
467
- old_addresses
468
- ) ;
469
- let res = scouting
470
- . update_addresses ( & new_addresses, & old_addresses)
471
- . await ;
472
- if let Err ( e) = res {
473
- tracing:: error!(
474
- "Could not update scouting addresses with +{:?}, -{:?}: {}" ,
475
- new_addresses,
476
- old_addresses,
477
- e
478
- ) ;
479
- }
480
- }
404
+ let scouting = self . state . scouting . lock ( ) . await ;
405
+ if let Some ( scouting) = scouting. as_ref ( ) {
406
+ scouting. update_addrs_if_needed ( ) . await ;
481
407
}
482
408
}
483
409
}
@@ -679,18 +605,3 @@ impl Closeable for Runtime {
679
605
self . state . clone ( )
680
606
}
681
607
}
682
-
683
- /// Find the relevant IpAddr in the attributes.
684
- /// Prefer the IFA_LOCAL address, or else use the IFA_ADDRESS address
685
- /// TODO: Prove why this choice
686
- fn get_relevant_address ( address_message : & AddressMessage ) -> Option < & IpAddr > {
687
- let mut res = None ;
688
- for attribute in & address_message. attributes {
689
- match attribute {
690
- AddressAttribute :: Address ( addr) => res = Some ( addr) ,
691
- AddressAttribute :: Local ( addr) => return Some ( addr) ,
692
- _ => ( ) ,
693
- }
694
- }
695
- res
696
- }
0 commit comments