@@ -36,6 +36,8 @@ use net::buffer::test_buffer::TestBuffer;
3636use net:: interface:: { InterfaceIndex , InterfaceName } ;
3737use net:: packet:: { DoneReason , Packet , PortIndex } ;
3838use netdev:: Interface ;
39+ use nix:: net:: if_:: if_nametoindex;
40+
3941use pipeline:: { DynPipeline , NetworkFunction } ;
4042#[ allow( unused) ]
4143use tracing:: { debug, error, info, trace, warn} ;
@@ -61,8 +63,8 @@ pub struct Kif {
6163 raw_fd : RawFd , // raw desc of packet socket
6264 pindex : PortIndex , // port index. This is how the kernel interface is externally identified
6365
64- tapname : InterfaceName , // name of tap interface
65- tapifindex : Option < InterfaceIndex > , // tap ifindex
66+ tapname : InterfaceName , // name of tap interface
67+ tapifindex : InterfaceIndex , // tap ifindex
6668}
6769
6870impl Kif {
@@ -74,6 +76,7 @@ impl Kif {
7476 name : & InterfaceName ,
7577 token : Token ,
7678 tapname : & InterfaceName ,
79+ tapifindex : InterfaceIndex ,
7780 ) -> Result < Self , String > {
7881 let mut sock = RawPacketStream :: new ( )
7982 . map_err ( |e| format ! ( "Failed to open raw sock for interface {name}: {e}" ) ) ?;
@@ -92,7 +95,7 @@ impl Kif {
9295 raw_fd,
9396 pindex,
9497 tapname : tapname. clone ( ) ,
95- tapifindex : None ,
98+ tapifindex,
9699 } ;
97100 debug ! ( "Successfully created interface '{name}'" ) ;
98101 Ok ( iface)
@@ -126,13 +129,17 @@ impl KifTable {
126129 ) -> Result < ( ) , String > {
127130 debug ! ( "Adding interface '{name}'..." ) ;
128131 let token = Token ( self . next_token ) ;
129- let interface = Kif :: new ( ifindex, name, token, tapname) ?;
130- let mut source = SourceFd ( & interface. raw_fd ) ;
132+ let tapifindex = if_nametoindex ( tapname. as_ref ( ) )
133+ . map_err ( |e| format ! ( "Could not find ifindex for tap {tapname}: {e}" ) ) ?;
134+ let tapifindex = InterfaceIndex :: try_from ( tapifindex) . map_err ( |e| e. to_string ( ) ) ?;
135+
136+ let kif = Kif :: new ( ifindex, name, token, tapname, tapifindex) ?;
137+ let mut source = SourceFd ( & kif. raw_fd ) ;
131138 self . poll
132139 . registry ( )
133140 . register ( & mut source, token, Interest :: READABLE )
134141 . map_err ( |e| format ! ( "Failed to register interface '{name}': {e}" ) ) ?;
135- self . by_token . insert ( token, interface ) ;
142+ self . by_token . insert ( token, kif ) ;
136143 self . next_token += 1 ;
137144 debug ! ( "Successfully registered interface '{name}' with token {token:?}" ) ;
138145 Ok ( ( ) )
@@ -146,7 +153,7 @@ impl KifTable {
146153 pub fn get_mut_by_tap_index ( & mut self , tapifindex : InterfaceIndex ) -> Option < & mut Kif > {
147154 self . by_token
148155 . values_mut ( )
149- . find ( |kif| kif. tapifindex == Some ( tapifindex) )
156+ . find ( |kif| kif. tapifindex == tapifindex)
150157 }
151158}
152159
@@ -165,19 +172,14 @@ fn fmt_heading(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
165172
166173impl Display for Kif {
167174 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
168- let tapifindex = if let Some ( i) = self . tapifindex {
169- i. to_string ( )
170- } else {
171- "--" . to_string ( )
172- } ;
173175 writeln ! (
174176 f,
175177 KIF_FMT !( ) ,
176178 self . name. to_string( ) ,
177179 self . ifindex. to_string( ) ,
178180 self . pindex. to_string( ) ,
179181 self . tapname. to_string( ) ,
180- tapifindex
182+ self . tapifindex
181183 )
182184 }
183185}
@@ -494,7 +496,7 @@ impl DriverKernel {
494496 Ok ( mut incoming) => {
495497 // we'll probably ditch iport, but for the time being....
496498 incoming. get_meta_mut ( ) . iport = Some ( kif. pindex ) ;
497- incoming. get_meta_mut ( ) . iif = kif. tapifindex ;
499+ incoming. get_meta_mut ( ) . iif = Some ( kif. tapifindex ) ;
498500 pkts. push ( Box :: new ( incoming) ) ;
499501 }
500502 Err ( e) => {
0 commit comments