Skip to content

Commit

Permalink
Add SetLinkAddress method to NetworkLinkEndpoint interface.
Browse files Browse the repository at this point in the history
The method will be primarily used by IFLA_ADDRESS.

PiperOrigin-RevId: 642492748
  • Loading branch information
milantracy authored and gvisor-bot committed Jun 12, 2024
1 parent c1661e7 commit 2c5c786
Show file tree
Hide file tree
Showing 30 changed files with 344 additions and 8 deletions.
9 changes: 8 additions & 1 deletion pkg/tcpip/link/channel/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools:defs.bzl", "go_library")
load("//tools:defs.bzl", "go_library", "go_test")

package(
default_applicable_licenses = ["//:license"],
Expand All @@ -16,3 +16,10 @@ go_library(
"//pkg/tcpip/stack",
],
)

go_test(
name = "channel_test",
srcs = ["channel_test.go"],
library = ":channel",
deps = ["//pkg/tcpip"],
)
7 changes: 7 additions & 0 deletions pkg/tcpip/link/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 34 additions & 0 deletions pkg/tcpip/link/channel/channel_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions pkg/tcpip/link/fdbased/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 13 additions & 0 deletions pkg/tcpip/link/fdbased/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions pkg/tcpip/link/loopback/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down
3 changes: 3 additions & 0 deletions pkg/tcpip/link/muxed/injectable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/tcpip/link/nested/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions pkg/tcpip/link/nested/nested_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions pkg/tcpip/link/packetsocket/packetsocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion pkg/tcpip/link/pipe/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools:defs.bzl", "go_library")
load("//tools:defs.bzl", "go_library", "go_test")

package(
default_applicable_licenses = ["//:license"],
Expand All @@ -15,3 +15,10 @@ go_library(
"//pkg/tcpip/stack",
],
)

go_test(
name = "pipe_test",
srcs = ["pipe_test.go"],
library = ":pipe",
deps = ["//pkg/tcpip"],
)
7 changes: 7 additions & 0 deletions pkg/tcpip/link/pipe/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions pkg/tcpip/link/pipe/pipe_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions pkg/tcpip/link/sharedmem/sharedmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions pkg/tcpip/link/sharedmem/sharedmem_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions pkg/tcpip/link/sharedmem/sharedmem_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions pkg/tcpip/link/sharedmem/sharedmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion pkg/tcpip/link/veth/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools:defs.bzl", "go_library")
load("//tools:defs.bzl", "go_library", "go_test")

package(
default_applicable_licenses = ["//:license"],
Expand All @@ -16,3 +16,10 @@ go_library(
"//pkg/tcpip/stack",
],
)

go_test(
name = "veth_test",
srcs = ["veth_test.go"],
library = ":veth",
deps = ["//pkg/tcpip"],
)
7 changes: 7 additions & 0 deletions pkg/tcpip/link/veth/veth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 2c5c786

Please sign in to comment.