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

Commit

Permalink
netmon: Don't rely on agent protocol
Browse files Browse the repository at this point in the history
In order to reduce the overhead due to the import of the whole
agent protocol, only the needed structures are duplicated. This
is a temporary solution, and those structures should be defined
into their own package to prevent from such overhead.

Note: the overhead of the binray size went down from 15MiB to
3MiB when this commit removed the dependency on the agent protocol.

Signed-off-by: Sebastien Boeuf <[email protected]>
  • Loading branch information
Sebastien Boeuf committed Sep 14, 2018
1 parent b708a4a commit bbf2a47
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions netmon/netmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,45 @@ import (
"path/filepath"
"strings"

"github.com/kata-containers/agent/protocols/grpc"
"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"
netmonVersion = "0.0.1"
Expand Down Expand Up @@ -57,7 +91,7 @@ type netmon struct {
storagePath string
sharedFile string

netIfaces map[int]grpc.Interface
netIfaces map[int]Interface

linkUpdateCh chan netlink.LinkUpdate
linkDoneCh chan struct{}
Expand Down Expand Up @@ -134,7 +168,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]grpc.Interface),
netIfaces: make(map[int]Interface),
linkUpdateCh: make(chan netlink.LinkUpdate),
linkDoneCh: make(chan struct{}),
rtUpdateCh: make(chan netlink.RouteUpdate),
Expand Down Expand Up @@ -171,13 +205,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) grpc.Interface {
func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) Interface {
if linkAttrs == nil {
fmt.Printf("Link attributes are nil")
return grpc.Interface{}
return Interface{}
}

var ipAddrs []*grpc.IPAddress
var ipAddrs []*IPAddress

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

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

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

ipAddrs = append(ipAddrs, ipAddr)
}

return grpc.Interface{
return Interface{
Device: linkAttrs.Name,
Name: linkAttrs.Name,
IPAddresses: ipAddrs,
Expand All @@ -207,8 +241,8 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) grpc.I
// 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) []grpc.Route {
var routes []grpc.Route
func convertRoutes(netRoutes []netlink.Route) []Route {
var routes []Route

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

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

func (n *netmon) addInterfaceCLI(iface grpc.Interface) error {
func (n *netmon) addInterfaceCLI(iface Interface) error {
fmt.Printf("%s %s %+v\n", n.runtimePath, kataCLIAddIfaceCmd, iface)

if err := n.storeDataToSend(iface); err != nil {
Expand All @@ -313,7 +347,7 @@ func (n *netmon) addInterfaceCLI(iface grpc.Interface) error {
return n.execKataCmd(kataCLIAddIfaceCmd)
}

func (n *netmon) delInterfaceCLI(iface grpc.Interface) error {
func (n *netmon) delInterfaceCLI(iface Interface) error {
fmt.Printf("%s %s %+v\n", n.runtimePath, kataCLIDelIfaceCmd, iface)

if err := n.storeDataToSend(iface); err != nil {
Expand All @@ -323,7 +357,7 @@ func (n *netmon) delInterfaceCLI(iface grpc.Interface) error {
return n.execKataCmd(kataCLIDelIfaceCmd)
}

func (n *netmon) updateRoutesCLI(routes []grpc.Route) error {
func (n *netmon) updateRoutesCLI(routes []Route) error {
fmt.Printf("%s %s %+v\n", n.runtimePath, kataCLIUpdtRoutesCmd, routes)

if err := n.storeDataToSend(routes); err != nil {
Expand All @@ -340,7 +374,7 @@ func (n *netmon) updateRoutes() error {
return err
}

// Translate them into grpc.Route structures.
// Translate them into Route structures.
routes := convertRoutes(netlinkRoutes)

// Update the routes through the Kata CLI.
Expand Down

0 comments on commit bbf2a47

Please sign in to comment.