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
|
|
|
|
2023-10-25 12:06:44 -07:00
|
|
|
// Package loopback provides the implementation of loopback data-link layer
|
2018-04-27 10:37:02 -07:00
|
|
|
// endpoints. Such endpoints just turn outbound packets into inbound ones.
|
|
|
|
|
//
|
|
|
|
|
// Loopback endpoints can be used in the networking stack by calling New() to
|
|
|
|
|
// create a new endpoint, and then passing it as an argument to
|
|
|
|
|
// Stack.CreateNIC().
|
|
|
|
|
package loopback
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-13 16:49:09 -07:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
2019-10-21 13:21:58 -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
|
|
|
)
|
|
|
|
|
|
2024-07-02 00:32:36 -07:00
|
|
|
const (
|
|
|
|
|
loopbackMTU = 65536
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-21 15:15:21 -07:00
|
|
|
// +stateify savable
|
2018-04-27 10:37:02 -07:00
|
|
|
type endpoint struct {
|
2024-10-31 18:53:45 -07:00
|
|
|
mu endpointRWMutex `state:"nosave"`
|
2023-01-17 13:37:13 -08:00
|
|
|
// +checklocks:mu
|
2018-04-27 10:37:02 -07:00
|
|
|
dispatcher stack.NetworkDispatcher
|
2024-07-02 00:32:36 -07:00
|
|
|
// +checklocks:mu
|
2024-07-08 12:15:29 -07:00
|
|
|
addr tcpip.LinkAddress
|
|
|
|
|
// +checklocks:mu
|
2024-07-02 00:32:36 -07:00
|
|
|
mtu uint32
|
2018-04-27 10:37:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new loopback endpoint. This link-layer endpoint just turns
|
|
|
|
|
// outbound packets into inbound packets.
|
2019-09-06 17:59:46 -07:00
|
|
|
func New() stack.LinkEndpoint {
|
2024-07-02 00:32:36 -07:00
|
|
|
return &endpoint{
|
|
|
|
|
mtu: loopbackMTU,
|
|
|
|
|
}
|
2018-04-27 10:37:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attach implements stack.LinkEndpoint.Attach. It just saves the stack network-
|
|
|
|
|
// layer dispatcher for later use when packets need to be dispatched.
|
|
|
|
|
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) {
|
2023-01-17 13:37:13 -08:00
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
2018-04-27 10:37:02 -07:00
|
|
|
e.dispatcher = dispatcher
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-02 00:32:36 -07:00
|
|
|
// MTU implements stack.LinkEndpoint.MTU.
|
|
|
|
|
func (e *endpoint) MTU() uint32 {
|
|
|
|
|
e.mu.RLock()
|
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
return e.mtu
|
2018-04-27 10:37:02 -07:00
|
|
|
}
|
|
|
|
|
|
2024-06-24 16:54:47 -07:00
|
|
|
// SetMTU implements stack.LinkEndpoint.SetMTU. It has no impact.
|
2024-07-02 00:32:36 -07:00
|
|
|
func (e *endpoint) SetMTU(mtu uint32) {
|
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
e.mtu = mtu
|
|
|
|
|
}
|
2024-06-24 16:54:47 -07:00
|
|
|
|
2018-04-27 10:37:02 -07:00
|
|
|
// Capabilities implements stack.LinkEndpoint.Capabilities. Loopback advertises
|
|
|
|
|
// itself as supporting checksum offload, but in reality it's just omitted.
|
|
|
|
|
func (*endpoint) Capabilities() stack.LinkEndpointCapabilities {
|
2019-04-09 11:22:28 -07:00
|
|
|
return stack.CapabilityRXChecksumOffload | stack.CapabilityTXChecksumOffload | stack.CapabilitySaveRestore | stack.CapabilityLoopback
|
2018-04-27 10:37:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MaxHeaderLength implements stack.LinkEndpoint.MaxHeaderLength. Given that the
|
|
|
|
|
// loopback interface doesn't have a header, it just returns 0.
|
|
|
|
|
func (*endpoint) MaxHeaderLength() uint16 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LinkAddress returns the link address of this endpoint.
|
2024-07-08 12:15:29 -07:00
|
|
|
func (e *endpoint) LinkAddress() tcpip.LinkAddress {
|
|
|
|
|
e.mu.RLock()
|
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
return e.addr
|
2018-04-27 10:37:02 -07:00
|
|
|
}
|
|
|
|
|
|
2024-06-11 21:37:10 -07:00
|
|
|
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
|
2024-07-08 12:15:29 -07:00
|
|
|
func (e *endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
|
|
|
|
|
e.mu.Lock()
|
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
e.addr = addr
|
|
|
|
|
}
|
2024-06-11 21:37:10 -07:00
|
|
|
|
2019-10-21 13:21:58 -07:00
|
|
|
// Wait implements stack.LinkEndpoint.Wait.
|
|
|
|
|
func (*endpoint) Wait() {}
|
|
|
|
|
|
2023-03-30 13:25:02 -07:00
|
|
|
// WritePackets implements stack.LinkEndpoint.WritePackets. If the endpoint is
|
|
|
|
|
// not attached, the packets are not delivered.
|
2022-01-14 13:20:15 -08:00
|
|
|
func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
|
2023-01-17 13:37:13 -08:00
|
|
|
e.mu.RLock()
|
|
|
|
|
d := e.dispatcher
|
|
|
|
|
e.mu.RUnlock()
|
2023-05-17 12:06:30 -07:00
|
|
|
for _, pkt := range pkts.AsSlice() {
|
2022-05-09 14:19:22 -07:00
|
|
|
// In order to properly loop back to the inbound side we must create a
|
|
|
|
|
// fresh packet that only contains the underlying payload with no headers
|
|
|
|
|
// or struct fields set.
|
2022-01-26 18:31:27 -08:00
|
|
|
newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
2022-07-21 13:07:09 -07:00
|
|
|
Payload: pkt.ToBuffer(),
|
2022-01-26 18:31:27 -08:00
|
|
|
})
|
2023-03-30 13:25:02 -07:00
|
|
|
if d != nil {
|
|
|
|
|
d.DeliverNetworkPacket(pkt.NetworkProtocolNumber, newPkt)
|
|
|
|
|
}
|
2022-01-26 18:31:27 -08:00
|
|
|
newPkt.DecRef()
|
2021-12-20 10:19:55 -08:00
|
|
|
}
|
2022-01-26 18:31:27 -08:00
|
|
|
return pkts.Len(), nil
|
2019-10-22 11:54:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-15 14:13:42 -07:00
|
|
|
// ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType.
|
|
|
|
|
func (*endpoint) ARPHardwareType() header.ARPHardwareType {
|
|
|
|
|
return header.ARPHardwareLoopback
|
|
|
|
|
}
|
2020-07-22 15:12:56 -07:00
|
|
|
|
2023-06-21 13:38:29 -07:00
|
|
|
// AddHeader implements stack.LinkEndpoint.
|
2024-02-29 11:07:59 -08:00
|
|
|
func (*endpoint) AddHeader(*stack.PacketBuffer) {}
|
2023-06-21 13:38:29 -07:00
|
|
|
|
|
|
|
|
// ParseHeader implements stack.LinkEndpoint.
|
2024-02-29 11:07:59 -08:00
|
|
|
func (*endpoint) ParseHeader(*stack.PacketBuffer) bool { return true }
|
2024-06-21 18:47:21 -07:00
|
|
|
|
|
|
|
|
// Close implements stack.LinkEndpoint.
|
|
|
|
|
func (*endpoint) Close() {}
|
2024-07-02 16:45:52 -07:00
|
|
|
|
|
|
|
|
// SetOnCloseAction implements stack.LinkEndpoint.
|
|
|
|
|
func (*endpoint) SetOnCloseAction(func()) {}
|