Add SetLinkAddress method to NetworkLinkEndpoint interface.

The method will be primarily used by IFLA_ADDRESS.

PiperOrigin-RevId: 642492748
This commit is contained in:
Jing Chen
2024-06-11 21:40:39 -07:00
committed by gVisor bot
parent c1661e7c84
commit 2c5c7869d9
30 changed files with 344 additions and 8 deletions
+8 -1
View File
@@ -1,4 +1,4 @@
load("//tools:defs.bzl", "go_library")
load("//tools:defs.bzl", "go_library", "go_test")
package(
default_applicable_licenses = ["//:license"],
@@ -16,3 +16,10 @@ go_library(
"//pkg/tcpip/stack",
],
)
go_test(
name = "channel_test",
srcs = ["channel_test.go"],
library = ":channel",
deps = ["//pkg/tcpip"],
)
+7
View File
@@ -252,6 +252,13 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
return e.linkAddr
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (e *Endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
e.mu.Lock()
defer e.mu.Unlock()
e.linkAddr = addr
}
// WritePackets stores outbound packets into the channel.
// Multiple concurrent calls are permitted.
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2024 The gVisor Authors.
//
// 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.
package channel
import (
"testing"
"gvisor.dev/gvisor/pkg/tcpip"
)
func TestSetLinkAddress(t *testing.T) {
addrs := []tcpip.LinkAddress{"abc", "def"}
size, mtu, linkAddr := 10, uint32(2000), tcpip.LinkAddress("xyz")
e := New(size, mtu, linkAddr)
defer e.Close()
for _, addr := range addrs {
e.SetLinkAddress(addr)
if want, v := addr, e.LinkAddress(); want != v {
t.Errorf("LinkAddress() = %v, want %v", v, want)
}
}
}
+7
View File
@@ -473,6 +473,13 @@ func (e *endpoint) LinkAddress() tcpip.LinkAddress {
return e.addr
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (e *endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
e.mu.Lock()
defer e.mu.Unlock()
e.addr = addr
}
// Wait implements stack.LinkEndpoint.Wait. It waits for the endpoint to stop
// reading from its FD.
func (e *endpoint) Wait() {
+13
View File
@@ -181,6 +181,19 @@ func TestAddress(t *testing.T) {
}
}
func TestSetAddress(t *testing.T) {
addrs := []tcpip.LinkAddress{"abc", "def"}
c := newContext(t, &Options{Address: laddr, MTU: mtu})
defer c.cleanup()
for _, addr := range addrs {
c.ep.SetLinkAddress(addr)
if want, v := addr, c.ep.LinkAddress(); want != v {
t.Errorf("LinkAddress() = %v, want %v", v, want)
}
}
}
func testWritePacket(t *testing.T, plen int, eth bool, gsoMaxSize uint32, hash uint32) {
c := newContext(t, &Options{Address: laddr, MTU: mtu, EthernetHeader: eth, GSOMaxSize: gsoMaxSize})
defer c.cleanup()
+3
View File
@@ -79,6 +79,9 @@ func (*endpoint) LinkAddress() tcpip.LinkAddress {
return ""
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (*endpoint) SetLinkAddress(tcpip.LinkAddress) {}
// Wait implements stack.LinkEndpoint.Wait.
func (*endpoint) Wait() {}
+3
View File
@@ -74,6 +74,9 @@ func (m *InjectableEndpoint) LinkAddress() tcpip.LinkAddress {
return ""
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (m *InjectableEndpoint) SetLinkAddress(tcpip.LinkAddress) {}
// Attach implements stack.LinkEndpoint.
func (m *InjectableEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
for _, endpoint := range m.routes {
+7
View File
@@ -114,6 +114,13 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
return e.child.LinkAddress()
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (e *Endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
e.mu.Lock()
defer e.mu.Unlock()
e.child.SetLinkAddress(addr)
}
// WritePackets implements stack.LinkEndpoint.
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
return e.child.WritePackets(pkts)
+26
View File
@@ -33,6 +33,7 @@ var _ stack.LinkEndpoint = (*parentEndpoint)(nil)
var _ stack.NetworkDispatcher = (*parentEndpoint)(nil)
type childEndpoint struct {
addr tcpip.LinkAddress
stack.LinkEndpoint
dispatcher stack.NetworkDispatcher
}
@@ -47,6 +48,14 @@ func (c *childEndpoint) IsAttached() bool {
return c.dispatcher != nil
}
func (c *childEndpoint) LinkAddress() tcpip.LinkAddress {
return c.addr
}
func (c *childEndpoint) SetLinkAddress(addr tcpip.LinkAddress) {
c.addr = addr
}
type counterDispatcher struct {
count int
}
@@ -115,6 +124,23 @@ func TestNestedLinkEndpoint(t *testing.T) {
}
}
func TestSetLinkAddress(t *testing.T) {
var (
childEP childEndpoint
ep parentEndpoint
disp counterDispatcher
)
addrs := []tcpip.LinkAddress{"abc", "def"}
ep.Endpoint.Init(&childEP, &disp)
for _, addr := range addrs {
ep.SetLinkAddress(addr)
if want, v := addr, ep.LinkAddress(); want != v {
t.Errorf("LinkAddress() = %v, want %v", v, want)
}
}
}
func TestMain(m *testing.M) {
refs.SetLeakMode(refs.LeaksPanic)
code := m.Run()
@@ -42,8 +42,9 @@ func (*nullEndpoint) MaxHeaderLength() uint16 {
return 0
}
func (*nullEndpoint) LinkAddress() tcpip.LinkAddress {
var l tcpip.LinkAddress
return l
return ""
}
func (*nullEndpoint) SetLinkAddress(tcpip.LinkAddress) {
}
func (*nullEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
return pkts.Len(), nil
+8 -1
View File
@@ -1,4 +1,4 @@
load("//tools:defs.bzl", "go_library")
load("//tools:defs.bzl", "go_library", "go_test")
package(
default_applicable_licenses = ["//:license"],
@@ -15,3 +15,10 @@ go_library(
"//pkg/tcpip/stack",
],
)
go_test(
name = "pipe_test",
srcs = ["pipe_test.go"],
library = ":pipe",
deps = ["//pkg/tcpip"],
)
+7
View File
@@ -118,6 +118,13 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
return e.linkAddr
}
// SetLinkAddress implements stack.LinkEndpoint.
func (e *Endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
e.mu.Lock()
defer e.mu.Unlock()
e.linkAddr = addr
}
// ARPHardwareType implements stack.LinkEndpoint.
func (*Endpoint) ARPHardwareType() header.ARPHardwareType {
return header.ARPHardwareNone
+35
View File
@@ -0,0 +1,35 @@
// Copyright 2024 The gVisor Authors.
//
// 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.
package pipe
import (
"testing"
"gvisor.dev/gvisor/pkg/tcpip"
)
func TestSetAddress(t *testing.T) {
addrs := []tcpip.LinkAddress{"abc", "def"}
e := &Endpoint{
linkAddr: "xyz",
mtu: 1234,
}
for _, addr := range addrs {
e.SetLinkAddress(addr)
if want, v := addr, e.LinkAddress(); want != v {
t.Errorf("LinkAddress() = %v, want %v", v, want)
}
}
}
+7
View File
@@ -345,6 +345,13 @@ func (e *endpoint) LinkAddress() tcpip.LinkAddress {
return e.addr
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (e *endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
e.mu.Lock()
defer e.mu.Unlock()
e.addr = addr
}
// AddHeader implements stack.LinkEndpoint.AddHeader.
func (e *endpoint) AddHeader(pkt *stack.PacketBuffer) {
// Add ethernet header if needed.
@@ -202,6 +202,13 @@ func (e *serverEndpoint) LinkAddress() tcpip.LinkAddress {
return e.addr
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (e *serverEndpoint) SetLinkAddress(addr tcpip.LinkAddress) {
e.mu.Lock()
defer e.mu.Unlock()
e.addr = addr
}
// AddHeader implements stack.LinkEndpoint.AddHeader.
func (e *serverEndpoint) AddHeader(pkt *stack.PacketBuffer) {
// Add ethernet header if needed.
@@ -403,6 +403,34 @@ func TestClientBulkTransfer(t *testing.T) {
}
}
func TestSetLinkAddress(t *testing.T) {
q, err := sharedmem.NewQueuePair(sharedmem.QueueOptions{})
if err != nil {
q.Close()
t.Fatalf("failed to create sharedmem queue: %s", err)
}
defer q.Close()
ep, err := sharedmem.NewServerEndpoint(sharedmem.Options{
MTU: defaultMTU,
BufferSize: defaultBufferSize,
LinkAddress: remoteLinkAddr,
TX: q.TXQueueConfig(),
RX: q.RXQueueConfig(),
PeerFD: 123,
})
if err != nil {
t.Fatalf("failed to create sharedmem endpoint: %s", err)
}
addrs := []tcpip.LinkAddress{"abc", "def"}
for _, addr := range addrs {
ep.SetLinkAddress(addr)
if want, v := addr, ep.LinkAddress(); want != v {
t.Errorf("LinkAddress() = %v, want %v", v, want)
}
}
}
func TestMain(m *testing.M) {
refs.SetLeakMode(refs.LeaksPanic)
code := m.Run()
@@ -821,6 +821,20 @@ func TestCloseWhileWaitingToPost(t *testing.T) {
c.ep.Wait()
}
func TestSetLinkAddress(t *testing.T) {
c := newTestContext(t, 20000, 1500, tcpip.LinkAddress("xyz"))
defer c.cleanup()
addrs := []tcpip.LinkAddress{"abc", "def"}
for _, addr := range addrs {
c.ep.SetLinkAddress(addr)
if want, v := addr, c.ep.LinkAddress(); want != v {
t.Errorf("LinkAddress() = %v, want %v", v, want)
}
}
}
func TestMain(m *testing.M) {
refs.SetLeakMode(refs.LeaksPanic)
code := m.Run()
+8 -1
View File
@@ -1,4 +1,4 @@
load("//tools:defs.bzl", "go_library")
load("//tools:defs.bzl", "go_library", "go_test")
package(
default_applicable_licenses = ["//:license"],
@@ -16,3 +16,10 @@ go_library(
"//pkg/tcpip/stack",
],
)
go_test(
name = "veth_test",
srcs = ["veth_test.go"],
library = ":veth",
deps = ["//pkg/tcpip"],
)
+7
View File
@@ -170,6 +170,13 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
return e.linkAddr
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (e *Endpoint) SetLinkAddress(addr tcpip.LinkAddress) {
e.mu.Lock()
defer e.mu.Unlock()
e.linkAddr = addr
}
// WritePackets stores outbound packets into the channel.
// Multiple concurrent calls are permitted.
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
+35
View File
@@ -0,0 +1,35 @@
// Copyright 2024 The gVisor Authors.
//
// 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.
package veth
import (
"testing"
"gvisor.dev/gvisor/pkg/tcpip"
)
func TestSetLinkAddress(t *testing.T) {
addrs := []tcpip.LinkAddress{"abc", "def"}
e := &Endpoint{
linkAddr: tcpip.LinkAddress("xyz"),
}
defer e.Close()
for _, addr := range addrs {
e.SetLinkAddress(addr)
if want, v := addr, e.LinkAddress(); want != v {
t.Errorf("LinkAddress() = %v, want %v", v, want)
}
}
}

Some files were not shown because too many files have changed in this diff Show More