@@ -9,19 +9,30 @@ use ts_tunnel::NodeKeyPair;
99
1010use crate :: { EventResult , InboundResult , OutboundResult } ;
1111
12- /// Queue for packets leaving the data plane "up" into an overlay transport.
13- pub type DataplaneToOverlay = mpsc:: UnboundedSender < Vec < PacketMut > > ;
14-
15- /// Queue for packets entering the data plane "down" from an overlay transport.
16- pub type DataplaneFromOverlay = mpsc:: UnboundedReceiver < Vec < PacketMut > > ;
17-
18- /// Queue for packets leaving the data plane "down" into an underlay transport.
19- pub type DataplaneToUnderlay = mpsc:: UnboundedSender < ( PeerId , Vec < PacketMut > ) > ;
20-
21- /// Queue for packets entering the data plane "up" from an underlay transport.
22- pub type DataplaneFromUnderlay = mpsc:: UnboundedReceiver < ( PeerId , Vec < PacketMut > ) > ;
23-
24- // TODO: wire in overlay/underlay transport traits
12+ // NOTE(npry): this used to have unique types for each queue, but the names got confusing due to
13+ // having to think about the cartesian product of PacketType x QueueDirection x Network
14+ // (this is a "sender handle for receive packets on the overlay", vs. "receive handle for sender
15+ // packets on the overlay", etc.). It wasn't always clear to distinguish what referred to the
16+ // channel direction (sender/receiver) and what referred to the actual traffic kind (packets
17+ // received _from_ the underlay are different from packets sent _to_ the underlay). So now we name
18+ // the packet type here (to/from overlay/underlay) and use separate helper types to make the channel
19+ // directions easier to name.
20+
21+ /// Packet batches sent to an overlay.
22+ pub type ToOverlay = Vec < PacketMut > ;
23+ /// Packet batches received from an overlay.
24+ pub type FromOverlay = Vec < PacketMut > ;
25+
26+ /// Packet batches sent to an underlay.
27+ pub type ToUnderlay = ( PeerId , Vec < PacketMut > ) ;
28+ /// Packet batches received from an underlay.
29+ pub type FromUnderlay = Vec < PacketMut > ;
30+
31+ /// Shorthand for a sender channel.
32+ pub type Tx < T > = mpsc:: UnboundedSender < T > ;
33+
34+ /// Shorthand for a receiver channel.
35+ pub type Rx < T > = mpsc:: UnboundedReceiver < T > ;
2536
2637/// Transforms packets to make tailscale happen.
2738pub struct DataPlane {
@@ -30,8 +41,10 @@ pub struct DataPlane {
3041
3142 transports_changed : tokio:: sync:: Notify ,
3243
33- underlay_down : DataplaneToUnderlay ,
34- overlay_up : DataplaneToOverlay ,
44+ // These are the senders handed out in new_*_transport, just held here so we can clone them, we
45+ // never send to them.
46+ underlay_down : Tx < FromUnderlay > ,
47+ overlay_up : Tx < FromOverlay > ,
3548
3649 next_underlay_transport : AtomicU32 ,
3750 next_overlay_transport : AtomicU32 ,
@@ -42,17 +55,17 @@ struct CoreState {
4255 sync : crate :: DataPlane ,
4356
4457 /// Queues to write packets to overlay transports.
45- overlay_transports : HashMap < OverlayTransportId , DataplaneToOverlay > ,
58+ overlay_transports : HashMap < OverlayTransportId , Tx < ToOverlay > > ,
4659 /// Queues to write packets to underlay transports.
47- underlay_transports : HashMap < UnderlayTransportId , DataplaneToUnderlay > ,
60+ underlay_transports : HashMap < UnderlayTransportId , Tx < ToUnderlay > > ,
4861}
4962
5063/// State that must be held during async polling.
5164struct PollState {
5265 /// Queue for packets entering the data plane ("coming down") from overlay transports.
53- from_overlay : DataplaneFromOverlay ,
66+ from_overlay : Rx < FromOverlay > ,
5467 /// Queue for packets entering the data plane ("coming up") from underlay transports.
55- from_underlay : DataplaneFromUnderlay ,
68+ from_underlay : Rx < FromUnderlay > ,
5669}
5770
5871impl DataPlane {
@@ -89,13 +102,12 @@ impl DataPlane {
89102 }
90103
91104 /// Allocate a new underlay transport.
105+ ///
106+ /// The channels handed back are for an underlay to receive messages from the dataplane
107+ /// (`ToUnderlay`) and send messages to the dataplane (`FromUnderlay`).
92108 pub async fn new_underlay_transport (
93109 & self ,
94- ) -> (
95- UnderlayTransportId ,
96- DataplaneFromUnderlay ,
97- DataplaneToUnderlay ,
98- ) {
110+ ) -> ( UnderlayTransportId , Rx < ToUnderlay > , Tx < FromUnderlay > ) {
99111 let id = self
100112 . next_underlay_transport
101113 . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed )
@@ -114,9 +126,12 @@ impl DataPlane {
114126 }
115127
116128 /// Allocate a new overlay transport.
129+ ///
130+ /// The channels handed back are for an overlay to send messages to the dataplane
131+ /// (`FromOverlay`) and receive messages from the dataplane (`ToOverlay`).
117132 pub async fn new_overlay_transport (
118133 & self ,
119- ) -> ( OverlayTransportId , DataplaneToOverlay , DataplaneFromOverlay ) {
134+ ) -> ( OverlayTransportId , Tx < FromOverlay > , Rx < ToOverlay > ) {
120135 let id = self
121136 . next_overlay_transport
122137 . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed )
@@ -146,7 +161,7 @@ impl DataPlane {
146161 pub async fn step ( & self ) {
147162 enum SelectResult {
148163 OverlayDown ( Vec < PacketMut > ) ,
149- UnderlayUp ( PeerId , Vec < PacketMut > ) ,
164+ UnderlayUp ( Vec < PacketMut > ) ,
150165 TransportsChanged ,
151166 Event ,
152167 }
@@ -185,10 +200,10 @@ impl DataPlane {
185200 }
186201
187202 underlay_pkts = underlay_up. recv( ) => {
188- let ( peer_id , underlay_pkts) = underlay_pkts. unwrap( ) ;
189- tracing:: trace!( %peer_id , n_underlay_pkts = underlay_pkts. len( ) ) ;
203+ let underlay_pkts = underlay_pkts. unwrap( ) ;
204+ tracing:: trace!( n_underlay_pkts = underlay_pkts. len( ) ) ;
190205
191- SelectResult :: UnderlayUp ( peer_id , underlay_pkts)
206+ SelectResult :: UnderlayUp ( underlay_pkts)
192207 }
193208
194209 _ = self . transports_changed. notified( ) => {
@@ -214,7 +229,7 @@ impl DataPlane {
214229
215230 ( Some ( to_peers) , Some ( loopback) )
216231 }
217- SelectResult :: UnderlayUp ( _peer_id , underlay_up) => {
232+ SelectResult :: UnderlayUp ( underlay_up) => {
218233 let InboundResult { to_local, to_peers } = core. sync . process_inbound ( underlay_up) ;
219234
220235 ( Some ( to_peers) , Some ( to_local) )
0 commit comments