2019-04-29 14:25:05 -07:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-07-09 14:03:03 -07:00
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
2018-04-27 10:37:02 -07:00
|
|
|
|
|
|
|
|
// Package waitable provides the implementation of data-link layer endpoints
|
|
|
|
|
// that wrap other endpoints, and can wait for inflight calls to WritePacket or
|
|
|
|
|
// DeliverNetworkPacket to finish (and new ones to be prevented).
|
|
|
|
|
//
|
|
|
|
|
// Waitable endpoints can be used in the networking stack by calling New(eID) to
|
|
|
|
|
// create a new endpoint, where eID is the ID of the endpoint being wrapped,
|
|
|
|
|
// and then passing it as an argument to Stack.CreateNIC().
|
|
|
|
|
package waitable
|
|
|
|
|
|
|
|
|
|
import (
|
2021-02-24 11:55:02 -08:00
|
|
|
"gvisor.dev/gvisor/pkg/sync"
|
2019-06-13 16:49:09 -07:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
2020-07-15 14:13:42 -07:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/header"
|
2019-06-13 16:49:09 -07:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
2018-04-27 10:37:02 -07:00
|
|
|
)
|
|
|
|
|
|
2022-01-28 13:41:00 -08:00
|
|
|
var _ stack.NetworkDispatcher = (*Endpoint)(nil)
|
|
|
|
|
var _ stack.LinkEndpoint = (*Endpoint)(nil)
|
|
|
|
|
|
2018-04-27 10:37:02 -07:00
|
|
|
// Endpoint is a waitable link-layer endpoint.
|
2024-05-21 15:15:21 -07:00
|
|
|
//
|
|
|
|
|
// +stateify savable
|
2018-04-27 10:37:02 -07:00
|
|
|
type Endpoint struct {
|
2021-02-24 11:55:02 -08:00
|
|
|
dispatchGate sync.Gate
|
2023-01-17 13:37:13 -08:00
|
|
|
|
2024-10-31 18:53:45 -07:00
|
|
|
mu endpointRWMutex `state:"nosave"`
|
2023-01-17 13:37:13 -08:00
|
|
|
// +checklocks:mu
|
|
|
|
|
dispatcher stack.NetworkDispatcher
|
2018-04-27 10:37:02 -07:00
|
|
|
|
2021-02-24 11:55:02 -08:00
|
|
|
writeGate sync.Gate
|
2018-04-27 10:37:02 -07:00
|
|
|
lower stack.LinkEndpoint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new waitable link-layer endpoint. It wraps around another
|
|
|
|
|
// endpoint and allows the caller to block new write/dispatch calls and wait for
|
|
|
|
|
// the inflight ones to finish before returning.
|
2019-09-06 17:59:46 -07:00
|
|
|
func New(lower stack.LinkEndpoint) *Endpoint {
|
|
|
|
|
return &Endpoint{
|
|
|
|
|
lower: lower,
|
2018-04-27 10:37:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeliverNetworkPacket implements stack.NetworkDispatcher.DeliverNetworkPacket.
|
|
|
|
|
// It is called by the link-layer endpoint being wrapped when a packet arrives,
|
|
|
|
|
// and only forwards to the actual dispatcher if Wait or WaitDispatch haven't
|
|
|
|
|
// been called.
|
2024-02-29 11:07:59 -08:00
|
|
|
func (e *Endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
|
2018-04-27 10:37:02 -07:00
|
|
|
if !e.dispatchGate.Enter() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-17 13:37:13 -08:00
|
|
|
e.mu.RLock()
|
|
|
|
|
d := e.dispatcher
|
|
|
|
|
e.mu.RUnlock()
|
2023-03-30 13:25:02 -07:00
|
|
|
if d != nil {
|
|
|
|
|
d.DeliverNetworkPacket(protocol, pkt)
|
|
|
|
|
}
|
2018-04-27 10:37:02 -07:00
|
|
|
e.dispatchGate.Leave()
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 13:41:00 -08:00
|
|
|
// DeliverLinkPacket implements stack.NetworkDispatcher.
|
2024-02-29 11:07:59 -08:00
|
|
|
func (e *Endpoint) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
|
2022-01-28 13:41:00 -08:00
|
|
|
if !e.dispatchGate.Enter() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-17 13:37:13 -08:00
|
|
|
e.mu.RLock()
|
|
|
|
|
d := e.dispatcher
|
|
|
|
|
e.mu.RUnlock()
|
2023-03-30 13:25:02 -07:00
|
|
|
if d != nil {
|
|
|
|
|
d.DeliverLinkPacket(protocol, pkt)
|
|
|
|
|
}
|
2022-01-28 13:41:00 -08:00
|
|
|
e.dispatchGate.Leave()
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 10:37:02 -07:00
|
|
|
// Attach implements stack.LinkEndpoint.Attach. It saves the dispatcher and
|
|
|
|
|
// registers with the lower endpoint as its dispatcher so that "e" is called
|
|
|
|
|
// for inbound packets.
|
|
|
|
|
func (e *Endpoint) Attach(dispatcher stack.NetworkDispatcher) {
|
2023-01-17 13:37:13 -08:00
|
|
|
e.mu.Lock()
|
2018-04-27 10:37:02 -07:00
|
|
|
e.dispatcher = dispatcher
|
2023-01-17 13:37:13 -08:00
|
|
|
e.mu.Unlock()
|
2018-04-27 10:37:02 -07:00
|
|
|
e.lower.Attach(e)
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 13:46:37 -07:00
|
|
|
// IsAttached implements stack.LinkEndpoint.IsAttached.
|
|
|
|
|
func (e *Endpoint) IsAttached() bool {
|
2023-01-17 13:37:13 -08:00
|
|
|
e.mu.RLock()
|
|
|
|
|
defer e.mu.RUnlock()
|
2018-05-22 13:46:37 -07:00
|
|
|
return e.dispatcher != nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 10:37:02 -07:00
|
|
|
// MTU implements stack.LinkEndpoint.MTU. It just forwards the request to the
|
|
|
|
|
// lower endpoint.
|
|
|
|
|
func (e *Endpoint) MTU() uint32 {
|
|
|
|
|
return e.lower.MTU()
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 16:54:47 -07:00
|
|
|
// SetMTU implements stack.LinkEndpoint.SetMTU. It just forwards the request to
|
|
|
|
|
// the lower endpoint.
|
|
|
|
|
func (e *Endpoint) SetMTU(mtu uint32) {
|
|
|
|
|
e.lower.SetMTU(mtu)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 10:37:02 -07:00
|
|
|
// Capabilities implements stack.LinkEndpoint.Capabilities. It just forwards the
|
|
|
|
|
// request to the lower endpoint.
|
|
|
|
|
func (e *Endpoint) Capabilities() stack.LinkEndpointCapabilities {
|
|
|
|
|
return e.lower.Capabilities()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MaxHeaderLength implements stack.LinkEndpoint.MaxHeaderLength. It just
|
|
|
|
|
// forwards the request to the lower endpoint.
|
|
|
|
|
func (e *Endpoint) MaxHeaderLength() uint16 {
|
|
|
|
|
return e.lower.MaxHeaderLength()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LinkAddress implements stack.LinkEndpoint.LinkAddress. It just forwards the
|
|
|
|
|
// request to the lower endpoint.
|
|
|
|
|
func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
|
|
|
|
|
return e.lower.LinkAddress()
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 21:37:10 -07:00
|
|
|
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress. It forwards the
|
|
|
|
|
// request to the lower endpoint.
|
|
|
|
|
func (e *Endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
|
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
e.lower.SetLinkAddress(addr)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 11:54:14 -07:00
|
|
|
// WritePackets implements stack.LinkEndpoint.WritePackets. It is called by
|
|
|
|
|
// higher-level protocols to write packets. It only forwards packets to the
|
|
|
|
|
// lower endpoint if Wait or WaitWrite haven't been called.
|
2022-01-14 13:20:15 -08:00
|
|
|
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
|
2019-10-22 11:54:14 -07:00
|
|
|
if !e.writeGate.Enter() {
|
2020-04-03 18:34:48 -07:00
|
|
|
return pkts.Len(), nil
|
2019-10-22 11:54:14 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-14 13:20:15 -08:00
|
|
|
n, err := e.lower.WritePackets(pkts)
|
2019-10-22 11:54:14 -07:00
|
|
|
e.writeGate.Leave()
|
|
|
|
|
return n, err
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 10:37:02 -07:00
|
|
|
// WaitWrite prevents new calls to WritePacket from reaching the lower endpoint,
|
|
|
|
|
// and waits for inflight ones to finish before returning.
|
|
|
|
|
func (e *Endpoint) WaitWrite() {
|
|
|
|
|
e.writeGate.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitDispatch prevents new calls to DeliverNetworkPacket from reaching the
|
|
|
|
|
// actual dispatcher, and waits for inflight ones to finish before returning.
|
|
|
|
|
func (e *Endpoint) WaitDispatch() {
|
|
|
|
|
e.dispatchGate.Close()
|
|
|
|
|
}
|
2019-09-20 14:08:46 -07:00
|
|
|
|
|
|
|
|
// Wait implements stack.LinkEndpoint.Wait.
|
|
|
|
|
func (e *Endpoint) Wait() {}
|
2020-07-15 14:13:42 -07:00
|
|
|
|
|
|
|
|
// ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType.
|
|
|
|
|
func (e *Endpoint) ARPHardwareType() header.ARPHardwareType {
|
|
|
|
|
return e.lower.ARPHardwareType()
|
|
|
|
|
}
|
2020-07-22 15:12:56 -07:00
|
|
|
|
|
|
|
|
// AddHeader implements stack.LinkEndpoint.AddHeader.
|
2024-02-29 11:07:59 -08:00
|
|
|
func (e *Endpoint) AddHeader(pkt *stack.PacketBuffer) {
|
2022-01-26 16:02:39 -08:00
|
|
|
e.lower.AddHeader(pkt)
|
2020-07-22 15:12:56 -07:00
|
|
|
}
|
2023-06-21 13:38:29 -07:00
|
|
|
|
|
|
|
|
// ParseHeader implements stack.LinkEndpoint.ParseHeader.
|
2024-02-29 11:07:59 -08:00
|
|
|
func (e *Endpoint) ParseHeader(pkt *stack.PacketBuffer) bool {
|
2023-06-21 13:38:29 -07:00
|
|
|
return e.lower.ParseHeader(pkt)
|
|
|
|
|
}
|
2024-06-21 18:47:21 -07:00
|
|
|
|
2024-07-02 16:45:52 -07:00
|
|
|
// SetOnCloseAction implements stack.LinkEndpoint.SetOnCloseAction.
|
|
|
|
|
func (e *Endpoint) SetOnCloseAction(action func()) {
|
|
|
|
|
e.lower.SetOnCloseAction(action)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 18:47:21 -07:00
|
|
|
// Close implements stack.LinkEndpoint.
|
|
|
|
|
func (e *Endpoint) Close() {
|
|
|
|
|
e.lower.Close()
|
|
|
|
|
}
|