Pārlūkot izejas kodu

Pass PacketMeta separately, not within IpPacket.

Dario Nieuwenhuis 1 gadu atpakaļ
vecāks
revīzija
3a8f133a21

+ 2 - 1
src/iface/interface/ieee802154.rs

@@ -44,6 +44,7 @@ impl InterfaceInner {
         &mut self,
         ll_dst_a: Ieee802154Address,
         tx_token: Tx,
+        meta: PacketMeta,
         packet: IpPacket,
         frag: &mut Fragmenter,
     ) {
@@ -64,7 +65,7 @@ impl InterfaceInner {
             src_addr: Some(ll_src_a),
         };
 
-        self.dispatch_sixlowpan(tx_token, packet, ieee_repr, frag);
+        self.dispatch_sixlowpan(tx_token, meta, packet, ieee_repr, frag);
     }
 
     #[cfg(feature = "proto-sixlowpan-fragmentation")]

+ 10 - 5
src/iface/interface/igmp.rs

@@ -1,5 +1,5 @@
 use super::{check, IgmpReportState, Interface, InterfaceInner, IpPacket};
-use crate::phy::Device;
+use crate::phy::{Device, PacketMeta};
 use crate::time::{Duration, Instant};
 use crate::wire::*;
 
@@ -65,7 +65,7 @@ impl Interface {
 
                     // NOTE(unwrap): packet destination is multicast, which is always routable and doesn't require neighbor discovery.
                     self.inner
-                        .dispatch_ip(tx_token, pkt, &mut self.fragmenter)
+                        .dispatch_ip(tx_token, PacketMeta::default(), pkt, &mut self.fragmenter)
                         .unwrap();
 
                     Ok(true)
@@ -107,7 +107,7 @@ impl Interface {
 
                     // NOTE(unwrap): packet destination is multicast, which is always routable and doesn't require neighbor discovery.
                     self.inner
-                        .dispatch_ip(tx_token, pkt, &mut self.fragmenter)
+                        .dispatch_ip(tx_token, PacketMeta::default(), pkt, &mut self.fragmenter)
                         .unwrap();
 
                     Ok(true)
@@ -143,7 +143,7 @@ impl Interface {
                     if let Some(tx_token) = device.transmit(self.inner.now) {
                         // NOTE(unwrap): packet destination is multicast, which is always routable and doesn't require neighbor discovery.
                         self.inner
-                            .dispatch_ip(tx_token, pkt, &mut self.fragmenter)
+                            .dispatch_ip(tx_token, PacketMeta::default(), pkt, &mut self.fragmenter)
                             .unwrap();
                     } else {
                         return false;
@@ -173,7 +173,12 @@ impl Interface {
                             if let Some(tx_token) = device.transmit(self.inner.now) {
                                 // NOTE(unwrap): packet destination is multicast, which is always routable and doesn't require neighbor discovery.
                                 self.inner
-                                    .dispatch_ip(tx_token, pkt, &mut self.fragmenter)
+                                    .dispatch_ip(
+                                        tx_token,
+                                        PacketMeta::default(),
+                                        pkt,
+                                        &mut self.fragmenter,
+                                    )
                                     .unwrap();
                             } else {
                                 return false;

+ 43 - 37
src/iface/interface/mod.rs

@@ -321,7 +321,7 @@ pub(crate) enum IpPacket<'a> {
     #[cfg(feature = "socket-raw")]
     Raw((IpRepr, &'a [u8])),
     #[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
-    Udp((IpRepr, UdpRepr, &'a [u8], PacketMeta)),
+    Udp((IpRepr, UdpRepr, &'a [u8])),
     #[cfg(feature = "socket-tcp")]
     Tcp((IpRepr, TcpRepr<'a>)),
     #[cfg(feature = "socket-dhcpv4")]
@@ -340,7 +340,7 @@ impl<'a> IpPacket<'a> {
             #[cfg(feature = "socket-raw")]
             IpPacket::Raw((ip_repr, _)) => ip_repr.clone(),
             #[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
-            IpPacket::Udp((ip_repr, _, _, _)) => ip_repr.clone(),
+            IpPacket::Udp((ip_repr, _, _)) => ip_repr.clone(),
             #[cfg(feature = "socket-tcp")]
             IpPacket::Tcp((ip_repr, _)) => ip_repr.clone(),
             #[cfg(feature = "socket-dhcpv4")]
@@ -348,14 +348,6 @@ impl<'a> IpPacket<'a> {
         }
     }
 
-    pub(crate) fn meta(&self) -> PacketMeta {
-        match self {
-            #[cfg(feature = "socket-udp")]
-            IpPacket::Udp((_, _, _, meta)) => *meta,
-            _ => PacketMeta::default(),
-        }
-    }
-
     pub(crate) fn emit_payload(
         &self,
         _ip_repr: &IpRepr,
@@ -381,7 +373,7 @@ impl<'a> IpPacket<'a> {
             #[cfg(feature = "socket-raw")]
             IpPacket::Raw((_, raw_packet)) => payload.copy_from_slice(raw_packet),
             #[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
-            IpPacket::Udp((_, udp_repr, inner_payload, _)) => {
+            IpPacket::Udp((_, udp_repr, inner_payload)) => {
                 udp_repr.emit(
                     &mut UdpPacket::new_unchecked(payload),
                     &_ip_repr.src_addr(),
@@ -838,10 +830,12 @@ impl Interface {
                             self.inner
                                 .process_ip(sockets, rx_meta, &frame, &mut self.fragments)
                         {
-                            if let Err(err) =
-                                self.inner
-                                    .dispatch_ip(tx_token, packet, &mut self.fragmenter)
-                            {
+                            if let Err(err) = self.inner.dispatch_ip(
+                                tx_token,
+                                PacketMeta::default(),
+                                packet,
+                                &mut self.fragmenter,
+                            ) {
                                 net_debug!("Failed to send response: {:?}", err);
                             }
                         }
@@ -854,10 +848,12 @@ impl Interface {
                             &frame,
                             &mut self.fragments,
                         ) {
-                            if let Err(err) =
-                                self.inner
-                                    .dispatch_ip(tx_token, packet, &mut self.fragmenter)
-                            {
+                            if let Err(err) = self.inner.dispatch_ip(
+                                tx_token,
+                                PacketMeta::default(),
+                                packet,
+                                &mut self.fragmenter,
+                            ) {
                                 net_debug!("Failed to send response: {:?}", err);
                             }
                         }
@@ -891,7 +887,7 @@ impl Interface {
             }
 
             let mut neighbor_addr = None;
-            let mut respond = |inner: &mut InterfaceInner, response: IpPacket| {
+            let mut respond = |inner: &mut InterfaceInner, meta: PacketMeta, response: IpPacket| {
                 neighbor_addr = Some(response.ip_repr().dst_addr());
                 let t = device.transmit(inner.now).ok_or_else(|| {
                     net_debug!("failed to transmit IP: device exhausted");
@@ -899,7 +895,7 @@ impl Interface {
                 })?;
 
                 inner
-                    .dispatch_ip(t, response, &mut self.fragmenter)
+                    .dispatch_ip(t, meta, response, &mut self.fragmenter)
                     .map_err(EgressError::Dispatch)?;
 
                 emitted_any = true;
@@ -910,41 +906,46 @@ impl Interface {
             let result = match &mut item.socket {
                 #[cfg(feature = "socket-raw")]
                 Socket::Raw(socket) => socket.dispatch(&mut self.inner, |inner, response| {
-                    respond(inner, IpPacket::Raw(response))
+                    respond(inner, PacketMeta::default(), IpPacket::Raw(response))
                 }),
                 #[cfg(feature = "socket-icmp")]
                 Socket::Icmp(socket) => {
                     socket.dispatch(&mut self.inner, |inner, response| match response {
                         #[cfg(feature = "proto-ipv4")]
-                        (IpRepr::Ipv4(ipv4_repr), IcmpRepr::Ipv4(icmpv4_repr)) => {
-                            respond(inner, IpPacket::Icmpv4((ipv4_repr, icmpv4_repr)))
-                        }
+                        (IpRepr::Ipv4(ipv4_repr), IcmpRepr::Ipv4(icmpv4_repr)) => respond(
+                            inner,
+                            PacketMeta::default(),
+                            IpPacket::Icmpv4((ipv4_repr, icmpv4_repr)),
+                        ),
                         #[cfg(feature = "proto-ipv6")]
-                        (IpRepr::Ipv6(ipv6_repr), IcmpRepr::Ipv6(icmpv6_repr)) => {
-                            respond(inner, IpPacket::Icmpv6((ipv6_repr, icmpv6_repr)))
-                        }
+                        (IpRepr::Ipv6(ipv6_repr), IcmpRepr::Ipv6(icmpv6_repr)) => respond(
+                            inner,
+                            PacketMeta::default(),
+                            IpPacket::Icmpv6((ipv6_repr, icmpv6_repr)),
+                        ),
                         #[allow(unreachable_patterns)]
                         _ => unreachable!(),
                     })
                 }
                 #[cfg(feature = "socket-udp")]
-                Socket::Udp(socket) => socket.dispatch(&mut self.inner, |inner, response| {
-                    respond(inner, IpPacket::Udp(response))
+                Socket::Udp(socket) => socket.dispatch(&mut self.inner, |inner, meta, response| {
+                    respond(inner, meta, IpPacket::Udp(response))
                 }),
                 #[cfg(feature = "socket-tcp")]
                 Socket::Tcp(socket) => socket.dispatch(&mut self.inner, |inner, response| {
-                    respond(inner, IpPacket::Tcp(response))
+                    respond(inner, PacketMeta::default(), IpPacket::Tcp(response))
                 }),
                 #[cfg(feature = "socket-dhcpv4")]
                 Socket::Dhcpv4(socket) => socket.dispatch(&mut self.inner, |inner, response| {
-                    respond(inner, IpPacket::Dhcpv4(response))
+                    respond(inner, PacketMeta::default(), IpPacket::Dhcpv4(response))
                 }),
                 #[cfg(feature = "socket-dns")]
                 Socket::Dns(socket) => {
                     socket.dispatch(&mut self.inner, |inner, (ip, udp, payload)| {
                         respond(
                             inner,
-                            IpPacket::Udp((ip, udp, payload, PacketMeta::default())),
+                            PacketMeta::default(),
+                            IpPacket::Udp((ip, udp, payload)),
                         )
                     })
                 }
@@ -1492,7 +1493,9 @@ impl InterfaceInner {
                     arp_repr.emit(&mut packet);
                 })
             }
-            EthernetPacket::Ip(packet) => self.dispatch_ip(tx_token, packet, frag),
+            EthernetPacket::Ip(packet) => {
+                self.dispatch_ip(tx_token, PacketMeta::default(), packet, frag)
+            }
         }
     }
 
@@ -1646,7 +1649,9 @@ impl InterfaceInner {
                     solicit,
                 ));
 
-                if let Err(e) = self.dispatch_ip(tx_token, packet, fragmenter) {
+                if let Err(e) =
+                    self.dispatch_ip(tx_token, PacketMeta::default(), packet, fragmenter)
+                {
                     net_debug!("Failed to dispatch NDISC solicit: {:?}", e);
                     return Err(DispatchError::NeighborPending);
                 }
@@ -1671,6 +1676,7 @@ impl InterfaceInner {
         // NOTE(unused_mut): tx_token isn't always mutated, depending on
         // the feature set that is used.
         #[allow(unused_mut)] mut tx_token: Tx,
+        meta: PacketMeta,
         packet: IpPacket,
         frag: &mut Fragmenter,
     ) -> Result<(), DispatchError> {
@@ -1689,7 +1695,7 @@ impl InterfaceInner {
             )?;
             let addr = addr.ieee802154_or_panic();
 
-            self.dispatch_ieee802154(addr, tx_token, packet, frag);
+            self.dispatch_ieee802154(addr, tx_token, meta, packet, frag);
             return Ok(());
         }
 
@@ -1835,7 +1841,7 @@ impl InterfaceInner {
                         Ok(())
                     }
                 } else {
-                    tx_token.set_meta(packet.meta());
+                    tx_token.set_meta(meta);
 
                     // No fragmentation is required.
                     tx_token.consume(total_len, |mut tx_buffer| {

+ 5 - 4
src/iface/interface/sixlowpan.rs

@@ -229,6 +229,7 @@ impl InterfaceInner {
     pub(super) fn dispatch_sixlowpan<Tx: TxToken>(
         &mut self,
         mut tx_token: Tx,
+        meta: PacketMeta,
         packet: IpPacket,
         ieee_repr: Ieee802154Repr,
         frag: &mut Fragmenter,
@@ -276,7 +277,7 @@ impl InterfaceInner {
 
         match packet {
             #[cfg(feature = "socket-udp")]
-            IpPacket::Udp((_, udpv6_repr, payload, _)) => {
+            IpPacket::Udp((_, udpv6_repr, payload)) => {
                 let udp_repr = SixlowpanUdpNhcRepr(udpv6_repr);
                 _compressed_headers_len += udp_repr.header_len();
                 _uncompressed_headers_len += udpv6_repr.header_len();
@@ -329,7 +330,7 @@ impl InterfaceInner {
 
                 match packet {
                     #[cfg(feature = "socket-udp")]
-                    IpPacket::Udp((_, udpv6_repr, payload, _)) => {
+                    IpPacket::Udp((_, udpv6_repr, payload)) => {
                         let udp_repr = SixlowpanUdpNhcRepr(udpv6_repr);
                         let mut udp_packet = SixlowpanUdpNhcPacket::new_unchecked(
                             &mut b[..udp_repr.header_len() + payload.len()],
@@ -430,7 +431,7 @@ impl InterfaceInner {
                 return;
             }
         } else {
-            tx_token.set_meta(packet.meta());
+            tx_token.set_meta(meta);
 
             // We don't need fragmentation, so we emit everything to the TX token.
             tx_token.consume(total_size + ieee_len, |mut tx_buf| {
@@ -445,7 +446,7 @@ impl InterfaceInner {
 
                 match packet {
                     #[cfg(feature = "socket-udp")]
-                    IpPacket::Udp((_, udpv6_repr, payload, _)) => {
+                    IpPacket::Udp((_, udpv6_repr, payload)) => {
                         let udp_repr = SixlowpanUdpNhcRepr(udpv6_repr);
                         let mut udp_packet = SixlowpanUdpNhcPacket::new_unchecked(
                             &mut tx_buf[..udp_repr.header_len() + payload.len()],

+ 2 - 1
src/iface/interface/tests.rs

@@ -1584,6 +1584,7 @@ fn test_echo_request_sixlowpan_128_bytes() {
     iface.inner.dispatch_ieee802154(
         Ieee802154Address::default(),
         tx_token,
+        PacketMeta::default(),
         result.unwrap(),
         &mut iface.fragmenter,
     );
@@ -1724,6 +1725,7 @@ fn test_sixlowpan_udp_with_fragmentation() {
     iface.inner.dispatch_ieee802154(
         Ieee802154Address::default(),
         tx_token,
+        PacketMeta::default(),
         IpPacket::Udp((
             IpRepr::Ipv6(Ipv6Repr {
                 src_addr: Ipv6Address::default(),
@@ -1737,7 +1739,6 @@ fn test_sixlowpan_udp_with_fragmentation() {
                 dst_port: 1234,
             },
             udp_data,
-            PacketMeta::default(),
         )),
         &mut iface.fragmenter,
     );

+ 6 - 6
src/socket/udp.rs

@@ -493,7 +493,7 @@ impl<'a> Socket<'a> {
 
     pub(crate) fn dispatch<F, E>(&mut self, cx: &mut Context, emit: F) -> Result<(), E>
     where
-        F: FnOnce(&mut Context, (IpRepr, UdpRepr, &[u8], PacketMeta)) -> Result<(), E>,
+        F: FnOnce(&mut Context, PacketMeta, (IpRepr, UdpRepr, &[u8])) -> Result<(), E>,
     {
         let endpoint = self.endpoint;
         let hop_limit = self.hop_limit.unwrap_or(64);
@@ -533,7 +533,7 @@ impl<'a> Socket<'a> {
                 hop_limit,
             );
 
-            emit(cx, (ip_repr, repr, payload_buf, packet_meta.meta))
+            emit(cx, packet_meta.meta, (ip_repr, repr, payload_buf))
         });
         match res {
             Err(Empty) => Ok(()),
@@ -711,7 +711,7 @@ mod test {
 
         assert!(socket.can_send());
         assert_eq!(
-            socket.dispatch(&mut cx, |_, _| unreachable!()),
+            socket.dispatch(&mut cx, |_, _, _| unreachable!()),
             Ok::<_, ()>(())
         );
 
@@ -723,7 +723,7 @@ mod test {
         assert!(!socket.can_send());
 
         assert_eq!(
-            socket.dispatch(&mut cx, |_, (ip_repr, udp_repr, payload, _meta)| {
+            socket.dispatch(&mut cx, |_, _, (ip_repr, udp_repr, payload)| {
                 assert_eq!(ip_repr, LOCAL_IP_REPR);
                 assert_eq!(udp_repr, LOCAL_UDP_REPR);
                 assert_eq!(payload, PAYLOAD);
@@ -734,7 +734,7 @@ mod test {
         assert!(!socket.can_send());
 
         assert_eq!(
-            socket.dispatch(&mut cx, |_, (ip_repr, udp_repr, payload, _meta)| {
+            socket.dispatch(&mut cx, |_, _, (ip_repr, udp_repr, payload)| {
                 assert_eq!(ip_repr, LOCAL_IP_REPR);
                 assert_eq!(udp_repr, LOCAL_UDP_REPR);
                 assert_eq!(payload, PAYLOAD);
@@ -862,7 +862,7 @@ mod test {
         s.set_hop_limit(Some(0x2a));
         assert_eq!(s.send_slice(b"abcdef", REMOTE_END), Ok(()));
         assert_eq!(
-            s.dispatch(&mut cx, |_, (ip_repr, _, _, _)| {
+            s.dispatch(&mut cx, |_, _, (ip_repr, _, _)| {
                 assert_eq!(
                     ip_repr,
                     IpReprIpvX(IpvXRepr {