Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
netmon: Rely on agent/pkg/types instead of duplicating types
Browse files Browse the repository at this point in the history
Now that the agent has split the generic types in their own package,
kata-netmon can use them directly and get rid of the duplication of
those. This is very helpful as it will prevent structures from being
out of sync between kata-netmon and the kata-runtime, without bringing
in the huge overhead that the initial grpc package was introducing.

Fixes #857

Signed-off-by: Sebastien Boeuf <[email protected]>
  • Loading branch information
Sebastien Boeuf committed Oct 26, 2018
1 parent 309dcf9 commit 38d56c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 67 deletions.
64 changes: 15 additions & 49 deletions netmon/netmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,14 @@ import (
"syscall"
"time"

"github.com/kata-containers/agent/pkg/types"
"github.com/kata-containers/runtime/pkg/signals"
"github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)

// The following types and structures have to be kept in sync with the
// description of the agent protocol. Those definitions need to be in their
// own separate package so that they can be imported directly from this code.
// The reason for not importing them now, is because importing the whole agent
// protocol adds up too much overhead because of the grpc protocol involved.

// IPFamily define the IP address family type.
type IPFamily int32

// IPAddress describes the IP address format expected by Kata API.
type IPAddress struct {
Family IPFamily `json:"family,omitempty"`
Address string `json:"address,omitempty"`
Mask string `json:"mask,omitempty"`
}

// Interface describes the network interface format expected by Kata API.
type Interface struct {
Device string `json:"device,omitempty"`
Name string `json:"name,omitempty"`
IPAddresses []*IPAddress `json:"IPAddresses,omitempty"`
Mtu uint64 `json:"mtu,omitempty"`
HwAddr string `json:"hwAddr,omitempty"`
PciAddr string `json:"pciAddr,omitempty"`
}

// Route describes the network route format expected by Kata API.
type Route struct {
Dest string `json:"dest,omitempty"`
Gateway string `json:"gateway,omitempty"`
Device string `json:"device,omitempty"`
Source string `json:"source,omitempty"`
Scope uint32 `json:"scope,omitempty"`
}

const (
netmonName = "kata-netmon"

Expand Down Expand Up @@ -104,7 +70,7 @@ type netmon struct {
storagePath string
sharedFile string

netIfaces map[int]Interface
netIfaces map[int]types.Interface

linkUpdateCh chan netlink.LinkUpdate
linkDoneCh chan struct{}
Expand Down Expand Up @@ -185,7 +151,7 @@ func newNetmon(params netmonParams) (*netmon, error) {
netmonParams: params,
storagePath: filepath.Join(storageParentPath, params.sandboxID),
sharedFile: filepath.Join(storageParentPath, params.sandboxID, sharedFile),
netIfaces: make(map[int]Interface),
netIfaces: make(map[int]types.Interface),
linkUpdateCh: make(chan netlink.LinkUpdate),
linkDoneCh: make(chan struct{}),
rtUpdateCh: make(chan netlink.RouteUpdate),
Expand Down Expand Up @@ -293,13 +259,13 @@ func (n *netmon) listenNetlinkEvents() error {
// convertInterface converts a link and its IP addresses as defined by netlink
// package, into the Interface structure format expected by kata-runtime to
// describe an interface and its associated IP addresses.
func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interface {
func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) types.Interface {
if linkAttrs == nil {
netmonLog.Warn("Link attributes are nil")
return Interface{}
return types.Interface{}
}

var ipAddrs []*IPAddress
var ipAddrs []*types.IPAddress

for _, addr := range addrs {
if addr.IPNet == nil {
Expand All @@ -308,16 +274,16 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interf

netMask, _ := addr.Mask.Size()

ipAddr := &IPAddress{
Family: IPFamily(netlinkFamily),
ipAddr := &types.IPAddress{
Family: types.IPFamily(netlinkFamily),
Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask),
}

ipAddrs = append(ipAddrs, ipAddr)
}

iface := Interface{
iface := types.Interface{
Device: linkAttrs.Name,
Name: linkAttrs.Name,
IPAddresses: ipAddrs,
Expand All @@ -333,8 +299,8 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interf
// convertRoutes converts a list of routes as defined by netlink package,
// into a list of Route structure format expected by kata-runtime to
// describe a set of routes.
func convertRoutes(netRoutes []netlink.Route) []Route {
var routes []Route
func convertRoutes(netRoutes []netlink.Route) []types.Route {
var routes []types.Route

// Ignore routes with IPv6 addresses as this is not supported
// by Kata yet.
Expand Down Expand Up @@ -368,7 +334,7 @@ func convertRoutes(netRoutes []netlink.Route) []Route {
dev = iface.Name
}

route := Route{
route := types.Route{
Dest: dst,
Gateway: gw,
Device: dev,
Expand Down Expand Up @@ -440,23 +406,23 @@ func (n *netmon) execKataCmd(subCmd string) error {
return os.Remove(n.sharedFile)
}

func (n *netmon) addInterfaceCLI(iface Interface) error {
func (n *netmon) addInterfaceCLI(iface types.Interface) error {
if err := n.storeDataToSend(iface); err != nil {
return err
}

return n.execKataCmd(kataCLIAddIfaceCmd)
}

func (n *netmon) delInterfaceCLI(iface Interface) error {
func (n *netmon) delInterfaceCLI(iface types.Interface) error {
if err := n.storeDataToSend(iface); err != nil {
return err
}

return n.execKataCmd(kataCLIDelIfaceCmd)
}

func (n *netmon) updateRoutesCLI(routes []Route) error {
func (n *netmon) updateRoutesCLI(routes []types.Route) error {
if err := n.storeDataToSend(routes); err != nil {
return err
}
Expand Down
37 changes: 19 additions & 18 deletions netmon/netmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"runtime"
"testing"

"github.com/kata-containers/agent/pkg/types"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -173,14 +174,14 @@ func TestConvertInterface(t *testing.T) {
HardwareAddr: hwAddr,
}

expected := Interface{
expected := types.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
HwAddr: testHwAddr,
IPAddresses: []*IPAddress{
IPAddresses: []*types.IPAddress{
{
Family: IPFamily(netlinkFamily),
Family: types.IPFamily(netlinkFamily),
Address: testIPAddress,
Mask: "0",
},
Expand All @@ -207,7 +208,7 @@ func TestConvertRoutes(t *testing.T) {
},
}

expected := []Route{
expected := []types.Route{
{
Dest: testIPAddress,
Gateway: testIPAddress,
Expand Down Expand Up @@ -241,7 +242,7 @@ func testSetupNetwork(t *testing.T) testTeardownNetwork {
}
}

func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, Interface) {
func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, types.Interface) {
hwAddr, err := net.ParseMAC(testHwAddr)
assert.Nil(t, err)

Expand All @@ -262,7 +263,7 @@ func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, Interfa
attrs := link.Attrs()
assert.NotNil(t, attrs)

iface := Interface{
iface := types.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
Expand All @@ -284,7 +285,7 @@ func TestScanNetwork(t *testing.T) {
idx, expected := testCreateDummyNetwork(t, handler)

n := &netmon{
netIfaces: make(map[int]Interface),
netIfaces: make(map[int]types.Interface),
netHandler: handler,
}

Expand All @@ -295,9 +296,9 @@ func TestScanNetwork(t *testing.T) {
}

func TestStoreDataToSend(t *testing.T) {
var got Interface
var got types.Interface

expected := Interface{
expected := types.Interface{
Device: testIfaceName,
Name: testIfaceName,
Mtu: uint64(testMTU),
Expand Down Expand Up @@ -394,15 +395,15 @@ func TestActionsCLI(t *testing.T) {
defer os.RemoveAll(testStorageParentPath)

// Test addInterfaceCLI
err = n.addInterfaceCLI(Interface{})
err = n.addInterfaceCLI(types.Interface{})
assert.Nil(t, err)

// Test delInterfaceCLI
err = n.delInterfaceCLI(Interface{})
err = n.delInterfaceCLI(types.Interface{})
assert.Nil(t, err)

// Test updateRoutesCLI
err = n.updateRoutesCLI([]Route{})
err = n.updateRoutesCLI([]types.Route{})
assert.Nil(t, err)

tearDownNetworkCb := testSetupNetwork(t)
Expand Down Expand Up @@ -460,8 +461,8 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)

// Interface already exist in list
n.netIfaces = make(map[int]Interface)
n.netIfaces[testIfaceIndex] = Interface{}
n.netIfaces = make(map[int]types.Interface)
n.netIfaces[testIfaceIndex] = types.Interface{}
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand All @@ -474,7 +475,7 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)

// Flags are not up and running
n.netIfaces = make(map[int]Interface)
n.netIfaces = make(map[int]types.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand All @@ -487,7 +488,7 @@ func TestHandleRTMNewLink(t *testing.T) {
assert.Nil(t, err)

// Invalid link
n.netIfaces = make(map[int]Interface)
n.netIfaces = make(map[int]types.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand Down Expand Up @@ -528,7 +529,7 @@ func TestHandleRTMDelLink(t *testing.T) {
assert.Nil(t, err)

// Interface does not exist in list
n.netIfaces = make(map[int]Interface)
n.netIfaces = make(map[int]types.Interface)
ev = netlink.LinkUpdate{
Link: &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Expand All @@ -543,7 +544,7 @@ func TestHandleRTMDelLink(t *testing.T) {

func TestHandleRTMNewRouteIfaceNotFound(t *testing.T) {
n := &netmon{
netIfaces: make(map[int]Interface),
netIfaces: make(map[int]types.Interface),
}

err := n.handleRTMNewRoute(netlink.RouteUpdate{})
Expand Down

0 comments on commit 38d56c9

Please sign in to comment.