inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/tcpip/link/pipe/pipe.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package pipe provides the implementation of pipe-like data-link layer
    16  // endpoints. Such endpoints allow packets to be sent between two interfaces.
    17  package pipe
    18  
    19  import (
    20  	"inet.af/netstack/tcpip"
    21  	"inet.af/netstack/tcpip/buffer"
    22  	"inet.af/netstack/tcpip/header"
    23  	"inet.af/netstack/tcpip/stack"
    24  )
    25  
    26  var _ stack.LinkEndpoint = (*Endpoint)(nil)
    27  
    28  // New returns both ends of a new pipe.
    29  func New(linkAddr1, linkAddr2 tcpip.LinkAddress) (*Endpoint, *Endpoint) {
    30  	ep1 := &Endpoint{
    31  		linkAddr: linkAddr1,
    32  	}
    33  	ep2 := &Endpoint{
    34  		linkAddr: linkAddr2,
    35  	}
    36  	ep1.linked = ep2
    37  	ep2.linked = ep1
    38  	return ep1, ep2
    39  }
    40  
    41  // Endpoint is one end of a pipe.
    42  type Endpoint struct {
    43  	dispatcher stack.NetworkDispatcher
    44  	linked     *Endpoint
    45  	linkAddr   tcpip.LinkAddress
    46  }
    47  
    48  func (e *Endpoint) deliverPackets(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkts stack.PacketBufferList) {
    49  	if !e.linked.IsAttached() {
    50  		return
    51  	}
    52  
    53  	// Note that the local address from the perspective of this endpoint is the
    54  	// remote address from the perspective of the other end of the pipe
    55  	// (e.linked). Similarly, the remote address from the perspective of this
    56  	// endpoint is the local address on the other end.
    57  	//
    58  	// Deliver the packet in a new goroutine to escape this goroutine's stack and
    59  	// avoid a deadlock when a packet triggers a response which leads the stack to
    60  	// try and take a lock it already holds.
    61  	for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() {
    62  		newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
    63  			Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()),
    64  		})
    65  		e.linked.dispatcher.DeliverNetworkPacket(r.LocalLinkAddress /* remote */, r.RemoteLinkAddress /* local */, proto, newPkt)
    66  		newPkt.DecRef()
    67  	}
    68  }
    69  
    70  // WritePacket implements stack.LinkEndpoint.
    71  func (e *Endpoint) WritePacket(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
    72  	var pkts stack.PacketBufferList
    73  	pkts.PushBack(pkt)
    74  	e.deliverPackets(r, proto, pkts)
    75  	return nil
    76  }
    77  
    78  // WritePackets implements stack.LinkEndpoint.
    79  func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
    80  	n := pkts.Len()
    81  	e.deliverPackets(r, proto, pkts)
    82  	return n, nil
    83  }
    84  
    85  // Attach implements stack.LinkEndpoint.
    86  func (e *Endpoint) Attach(dispatcher stack.NetworkDispatcher) {
    87  	e.dispatcher = dispatcher
    88  }
    89  
    90  // IsAttached implements stack.LinkEndpoint.
    91  func (e *Endpoint) IsAttached() bool {
    92  	return e.dispatcher != nil
    93  }
    94  
    95  // Wait implements stack.LinkEndpoint.
    96  func (*Endpoint) Wait() {}
    97  
    98  // MTU implements stack.LinkEndpoint.
    99  func (*Endpoint) MTU() uint32 {
   100  	return header.IPv6MinimumMTU
   101  }
   102  
   103  // Capabilities implements stack.LinkEndpoint.
   104  func (*Endpoint) Capabilities() stack.LinkEndpointCapabilities {
   105  	return 0
   106  }
   107  
   108  // MaxHeaderLength implements stack.LinkEndpoint.
   109  func (*Endpoint) MaxHeaderLength() uint16 {
   110  	return 0
   111  }
   112  
   113  // LinkAddress implements stack.LinkEndpoint.
   114  func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
   115  	return e.linkAddr
   116  }
   117  
   118  // ARPHardwareType implements stack.LinkEndpoint.
   119  func (*Endpoint) ARPHardwareType() header.ARPHardwareType {
   120  	return header.ARPHardwareNone
   121  }
   122  
   123  // AddHeader implements stack.LinkEndpoint.
   124  func (*Endpoint) AddHeader(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, _ *stack.PacketBuffer) {
   125  }
   126  
   127  // WriteRawPacket implements stack.LinkEndpoint.
   128  func (e *Endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error {
   129  	return e.WritePacket(stack.RouteInfo{}, 0, pkt)
   130  }