netmon: Don't rely on agent protocol

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 <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2018-07-31 13:19:52 -07:00
parent b708a4a05c
commit bbf2a47866

View File

@ -16,11 +16,45 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/kata-containers/agent/protocols/grpc"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
"golang.org/x/sys/unix" "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 ( const (
netmonName = "kata-netmon" netmonName = "kata-netmon"
netmonVersion = "0.0.1" netmonVersion = "0.0.1"
@ -57,7 +91,7 @@ type netmon struct {
storagePath string storagePath string
sharedFile string sharedFile string
netIfaces map[int]grpc.Interface netIfaces map[int]Interface
linkUpdateCh chan netlink.LinkUpdate linkUpdateCh chan netlink.LinkUpdate
linkDoneCh chan struct{} linkDoneCh chan struct{}
@ -134,7 +168,7 @@ func newNetmon(params netmonParams) (*netmon, error) {
netmonParams: params, netmonParams: params,
storagePath: filepath.Join(storageParentPath, params.sandboxID), storagePath: filepath.Join(storageParentPath, params.sandboxID),
sharedFile: filepath.Join(storageParentPath, params.sandboxID, sharedFile), sharedFile: filepath.Join(storageParentPath, params.sandboxID, sharedFile),
netIfaces: make(map[int]grpc.Interface), netIfaces: make(map[int]Interface),
linkUpdateCh: make(chan netlink.LinkUpdate), linkUpdateCh: make(chan netlink.LinkUpdate),
linkDoneCh: make(chan struct{}), linkDoneCh: make(chan struct{}),
rtUpdateCh: make(chan netlink.RouteUpdate), rtUpdateCh: make(chan netlink.RouteUpdate),
@ -171,13 +205,13 @@ func (n *netmon) listenNetlinkEvents() error {
// convertInterface converts a link and its IP addresses as defined by netlink // convertInterface converts a link and its IP addresses as defined by netlink
// package, into the Interface structure format expected by kata-runtime to // package, into the Interface structure format expected by kata-runtime to
// describe an interface and its associated IP addresses. // 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 { if linkAttrs == nil {
fmt.Printf("Link attributes are nil") fmt.Printf("Link attributes are nil")
return grpc.Interface{} return Interface{}
} }
var ipAddrs []*grpc.IPAddress var ipAddrs []*IPAddress
for _, addr := range addrs { for _, addr := range addrs {
if addr.IPNet == nil { if addr.IPNet == nil {
@ -186,8 +220,8 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) grpc.I
netMask, _ := addr.Mask.Size() netMask, _ := addr.Mask.Size()
ipAddr := &grpc.IPAddress{ ipAddr := &IPAddress{
Family: grpc.IPFamily(netlinkFamily), Family: IPFamily(netlinkFamily),
Address: addr.IP.String(), Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask), Mask: fmt.Sprintf("%d", netMask),
} }
@ -195,7 +229,7 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, addrs []netlink.Addr) grpc.I
ipAddrs = append(ipAddrs, ipAddr) ipAddrs = append(ipAddrs, ipAddr)
} }
return grpc.Interface{ return Interface{
Device: linkAttrs.Name, Device: linkAttrs.Name,
Name: linkAttrs.Name, Name: linkAttrs.Name,
IPAddresses: ipAddrs, IPAddresses: ipAddrs,
@ -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, // convertRoutes converts a list of routes as defined by netlink package,
// into a list of Route structure format expected by kata-runtime to // into a list of Route structure format expected by kata-runtime to
// describe a set of routes. // describe a set of routes.
func convertRoutes(netRoutes []netlink.Route) []grpc.Route { func convertRoutes(netRoutes []netlink.Route) []Route {
var routes []grpc.Route var routes []Route
// Ignore routes with IPv6 addresses as this is not supported // Ignore routes with IPv6 addresses as this is not supported
// by Kata yet. // by Kata yet.
@ -234,7 +268,7 @@ func convertRoutes(netRoutes []netlink.Route) []grpc.Route {
dev = iface.Name dev = iface.Name
} }
route := grpc.Route{ route := Route{
Dest: dst, Dest: dst,
Gateway: gw, Gateway: gw,
Device: dev, Device: dev,
@ -303,7 +337,7 @@ func (n *netmon) execKataCmd(subCmd string) error {
return os.Remove(n.sharedFile) 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) fmt.Printf("%s %s %+v\n", n.runtimePath, kataCLIAddIfaceCmd, iface)
if err := n.storeDataToSend(iface); err != nil { if err := n.storeDataToSend(iface); err != nil {
@ -313,7 +347,7 @@ func (n *netmon) addInterfaceCLI(iface grpc.Interface) error {
return n.execKataCmd(kataCLIAddIfaceCmd) 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) fmt.Printf("%s %s %+v\n", n.runtimePath, kataCLIDelIfaceCmd, iface)
if err := n.storeDataToSend(iface); err != nil { if err := n.storeDataToSend(iface); err != nil {
@ -323,7 +357,7 @@ func (n *netmon) delInterfaceCLI(iface grpc.Interface) error {
return n.execKataCmd(kataCLIDelIfaceCmd) 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) fmt.Printf("%s %s %+v\n", n.runtimePath, kataCLIUpdtRoutesCmd, routes)
if err := n.storeDataToSend(routes); err != nil { if err := n.storeDataToSend(routes); err != nil {
@ -340,7 +374,7 @@ func (n *netmon) updateRoutes() error {
return err return err
} }
// Translate them into grpc.Route structures. // Translate them into Route structures.
routes := convertRoutes(netlinkRoutes) routes := convertRoutes(netlinkRoutes)
// Update the routes through the Kata CLI. // Update the routes through the Kata CLI.