Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions netio/mocknetio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package netio

import (
"errors"
"fmt"
"net"
)

type MockNetIO struct {
fail bool
failAttempt int
numTimesCalled int
}

// ErrMockNetIOFail - mock netio error
var ErrMockNetIOFail = errors.New("netio fail")

func NewMockNetIO(fail bool, failAttempt int) *MockNetIO {
return &MockNetIO{
fail: fail,
failAttempt: failAttempt,
}
}

func (netshim *MockNetIO) GetNetworkInterfaceByName(name string) (*net.Interface, error) {
netshim.numTimesCalled++

if netshim.fail && netshim.failAttempt == netshim.numTimesCalled {
return nil, fmt.Errorf("%w:%s", ErrMockNetIOFail, name)
}

hwAddr, _ := net.ParseMAC("ab:cd:ef:12:34:56")

return &net.Interface{
//nolint:gomnd // Dummy MTU
MTU: 1000,
Name: name,
HardwareAddr: hwAddr,
//nolint:gomnd // Dummy interface index
Index: 2,
}, nil
}

func (netshim *MockNetIO) GetNetworkInterfaceAddrs(iface *net.Interface) ([]net.Addr, error) {
return []net.Addr{}, nil
}
32 changes: 32 additions & 0 deletions netio/netio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package netio

import (
"net"

"github.com/pkg/errors"
)

//nolint:revive // keeping NetIOInterface makes sense
type NetIOInterface interface {
GetNetworkInterfaceByName(name string) (*net.Interface, error)
GetNetworkInterfaceAddrs(iface *net.Interface) ([]net.Addr, error)
}

// ErrInterfaceNil - errors out when interface is nil
var ErrInterfaceNil = errors.New("Interface is nil")

type NetIO struct{}

func (ns *NetIO) GetNetworkInterfaceByName(name string) (*net.Interface, error) {
iface, err := net.InterfaceByName(name)
return iface, errors.Wrap(err, "GetNetworkInterfaceByName failed")
}

func (ns *NetIO) GetNetworkInterfaceAddrs(iface *net.Interface) ([]net.Addr, error) {
if iface == nil {
return []net.Addr{}, ErrInterfaceNil
}

addrs, err := iface.Addrs()
return addrs, errors.Wrap(err, "GetNetworkInterfaceAddrs failed")
}
24 changes: 24 additions & 0 deletions netlink/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net"

"github.com/Azure/azure-container-networking/log"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -151,6 +152,29 @@ func (Netlink) AddLink(link Link) error {
return s.sendAndWaitForAck(req)
}

func (Netlink) SetLinkMTU(name string, mtu int) error {
iface, err := net.InterfaceByName(name)
if err != nil {
log.Printf("[net] Interface not found. returning error")
return errors.Wrap(err, "SetLinkMTU:InterfaceByName failed")
}

s, err := getSocket()
if err != nil {
return err
}

req := newRequest(unix.RTM_SETLINK, unix.NLM_F_ACK)
ifInfo := newIfInfoMsg()
ifInfo.Index = int32(iface.Index)
req.addPayload(ifInfo)

mtuData := newAttributeUint32(unix.IFLA_MTU, uint32(mtu))
req.addPayload(mtuData)

return s.sendAndWaitForAck(req)
}

// DeleteLink deletes a network interface.
func (Netlink) DeleteLink(name string) error {
if name == "" {
Expand Down
17 changes: 11 additions & 6 deletions netlink/mocknetlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"net"
)

var errorMockNetlink = errors.New("Mock Netlink Error")
// ErrorMockNetlink - netlink mock error
var ErrorMockNetlink = errors.New("Mock Netlink Error")

func newErrorMockNetlink(errStr string) error {
return fmt.Errorf("%w : %s", errorMockNetlink, errStr)
return fmt.Errorf("%w : %s", ErrorMockNetlink, errStr)
}

type MockNetlink struct {
returnError bool
errorString string
}

func NewMockNetlink(returnError bool, errorString string) MockNetlink {
return MockNetlink{
func NewMockNetlink(returnError bool, errorString string) *MockNetlink {
return &MockNetlink{
returnError: returnError,
errorString: errorString,
}
Expand All @@ -31,11 +32,15 @@ func (f *MockNetlink) error() error {
return nil
}

func (f *MockNetlink) AddLink(Link) error {
func (f *MockNetlink) AddLink(l Link) error {
return f.error()
}

func (f *MockNetlink) DeleteLink(string) error {
func (f *MockNetlink) SetLinkMTU(name string, mtu int) error {
return f.error()
}

func (f *MockNetlink) DeleteLink(name string) error {
return f.error()
}

Expand Down
33 changes: 33 additions & 0 deletions netlink/netlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package netlink
import (
"net"
"testing"

"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -99,6 +101,37 @@ func TestAddDeleteVEth(t *testing.T) {
}
}

// TestSetMTU tests if MTU can be sent on link
func TestSetMTU(t *testing.T) {
link := VEthLink{
LinkInfo: LinkInfo{
Type: LINK_TYPE_VETH,
Name: ifName,
},
PeerName: ifName2,
}
nl := NewNetlink()

err := nl.AddLink(&link)
if err != nil {
t.Errorf("AddLink failed: %+v", err)
}

//nolint:errcheck // not testing deletelink here
defer nl.DeleteLink(ifName)

if err = nl.SetLinkMTU(ifName, 1028); err != nil {
t.Errorf("SetMTU failed: %+v", err)
}

iface, err := net.InterfaceByName(ifName)
if err != nil {
t.Errorf("InterfaceByName err:%v", err)
}

require.Equal(t, 1028, iface.MTU, "Expected mtu:1024 but got %d", iface.MTU)
}

// TestAddDeleteIPVlan tests adding and deleting an IPVLAN interface.
func TestAddDeleteIPVlan(t *testing.T) {
dummy, err := addDummyInterface(dummyName)
Expand Down
4 changes: 4 additions & 0 deletions netlink/netlink_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (Netlink) AddLink(link Link) error {
return nil
}

func (Netlink) SetLinkMTU(name string, mtu int) error {
return nil
}

func (Netlink) DeleteLink(name string) error {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// Copyright 2021 Microsoft. All rights reserved.
// MIT License

package netlinkinterface
package netlink

import (
"net"

"github.com/Azure/azure-container-networking/netlink"
)

type NetlinkInterface interface {
AddLink(link netlink.Link) error
AddLink(link Link) error
DeleteLink(name string) error
SetLinkName(name string, newName string) error
SetLinkState(name string, up bool) error
SetLinkMTU(name string, mtu int) error
SetLinkMaster(name string, master string) error
SetLinkNetNs(name string, fd uintptr) error
SetLinkAddress(ifName string, hwAddress net.HardwareAddr) error
Expand All @@ -22,7 +21,7 @@ type NetlinkInterface interface {
AddOrRemoveStaticArp(mode int, name string, ipaddr net.IP, mac net.HardwareAddr, isProxy bool) error
AddIPAddress(ifName string, ipAddress net.IP, ipNet *net.IPNet) error
DeleteIPAddress(ifName string, ipAddress net.IP, ipNet *net.IPNet) error
GetIPRoute(filter *netlink.Route) ([]*netlink.Route, error)
AddIPRoute(route *netlink.Route) error
DeleteIPRoute(route *netlink.Route) error
GetIPRoute(filter *Route) ([]*Route, error)
AddIPRoute(route *Route) error
DeleteIPRoute(route *Route) error
}
5 changes: 2 additions & 3 deletions network/bridge_endpointclient_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/netlink"
"github.com/Azure/azure-container-networking/network/epcommon"
"github.com/Azure/azure-container-networking/network/netlinkinterface"
)

const (
Expand All @@ -26,15 +25,15 @@ type LinuxBridgeEndpointClient struct {
containerMac net.HardwareAddr
hostIPAddresses []*net.IPNet
mode string
netlink netlinkinterface.NetlinkInterface
netlink netlink.NetlinkInterface
}

func NewLinuxBridgeEndpointClient(
extIf *externalInterface,
hostVethName string,
containerVethName string,
mode string,
nl netlinkinterface.NetlinkInterface,
nl netlink.NetlinkInterface,
) *LinuxBridgeEndpointClient {

client := &LinuxBridgeEndpointClient{
Expand Down
5 changes: 2 additions & 3 deletions network/bridge_networkclient_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/netlink"
"github.com/Azure/azure-container-networking/network/epcommon"
"github.com/Azure/azure-container-networking/network/netlinkinterface"
)

const (
Expand All @@ -26,14 +25,14 @@ type LinuxBridgeClient struct {
bridgeName string
hostInterfaceName string
nwInfo NetworkInfo
netlink netlinkinterface.NetlinkInterface
netlink netlink.NetlinkInterface
}

func NewLinuxBridgeClient(
bridgeName string,
hostInterfaceName string,
nwInfo NetworkInfo,
nl netlinkinterface.NetlinkInterface,
nl netlink.NetlinkInterface,
) *LinuxBridgeClient {
client := &LinuxBridgeClient{
bridgeName: bridgeName,
Expand Down
6 changes: 3 additions & 3 deletions network/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/network/netlinkinterface"
"github.com/Azure/azure-container-networking/netlink"
"github.com/Azure/azure-container-networking/network/policy"
)

Expand Down Expand Up @@ -98,7 +98,7 @@ type apipaClient interface {
}

// NewEndpoint creates a new endpoint in the network.
func (nw *network) newEndpoint(cli apipaClient, nl netlinkinterface.NetlinkInterface, epInfo *EndpointInfo) (*endpoint, error) {
func (nw *network) newEndpoint(cli apipaClient, nl netlink.NetlinkInterface, epInfo *EndpointInfo) (*endpoint, error) {
var ep *endpoint
var err error

Expand All @@ -122,7 +122,7 @@ func (nw *network) newEndpoint(cli apipaClient, nl netlinkinterface.NetlinkInter
}

// DeleteEndpoint deletes an existing endpoint from the network.
func (nw *network) deleteEndpoint(cli apipaClient, nl netlinkinterface.NetlinkInterface, endpointID string) error {
func (nw *network) deleteEndpoint(cli apipaClient, nl netlink.NetlinkInterface, endpointID string) error {
var err error

log.Printf("[net] Deleting endpoint %v from network %v.", endpointID, nw.Id)
Expand Down
9 changes: 4 additions & 5 deletions network/endpoint_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/netlink"
"github.com/Azure/azure-container-networking/network/netlinkinterface"
"github.com/Azure/azure-container-networking/ovsctl"
)

Expand Down Expand Up @@ -46,7 +45,7 @@ func ConstructEndpointID(containerID string, _ string, ifName string) (string, s
}

// newEndpointImpl creates a new endpoint in the network.
func (nw *network) newEndpointImpl(_ apipaClient, nl netlinkinterface.NetlinkInterface, epInfo *EndpointInfo) (*endpoint, error) {
func (nw *network) newEndpointImpl(_ apipaClient, nl netlink.NetlinkInterface, epInfo *EndpointInfo) (*endpoint, error) {
var containerIf *net.Interface
var ns *Namespace
var ep *endpoint
Expand Down Expand Up @@ -219,7 +218,7 @@ func (nw *network) newEndpointImpl(_ apipaClient, nl netlinkinterface.NetlinkInt
}

// deleteEndpointImpl deletes an existing endpoint from the network.
func (nw *network) deleteEndpointImpl(_ apipaClient, nl netlinkinterface.NetlinkInterface, ep *endpoint) error {
func (nw *network) deleteEndpointImpl(_ apipaClient, nl netlink.NetlinkInterface, ep *endpoint) error {
var epClient EndpointClient

// Delete the veth pair by deleting one of the peer interfaces.
Expand All @@ -244,7 +243,7 @@ func (nw *network) deleteEndpointImpl(_ apipaClient, nl netlinkinterface.Netlink
func (ep *endpoint) getInfoImpl(epInfo *EndpointInfo) {
}

func addRoutes(nl netlinkinterface.NetlinkInterface, interfaceName string, routes []RouteInfo) error {
func addRoutes(nl netlink.NetlinkInterface, interfaceName string, routes []RouteInfo) error {
ifIndex := 0
interfaceIf, _ := net.InterfaceByName(interfaceName)

Expand Down Expand Up @@ -285,7 +284,7 @@ func addRoutes(nl netlinkinterface.NetlinkInterface, interfaceName string, route
return nil
}

func deleteRoutes(nl netlinkinterface.NetlinkInterface, interfaceName string, routes []RouteInfo) error {
func deleteRoutes(nl netlink.NetlinkInterface, interfaceName string, routes []RouteInfo) error {
ifIndex := 0
interfaceIf, _ := net.InterfaceByName(interfaceName)

Expand Down
Loading