Skip to content

Commit

Permalink
agent: add ListInterfaces and ListRoutes rpc
Browse files Browse the repository at this point in the history
These are part of network hotplug and used to show actual network
info in the sandbox.

Fixes kata-containers#315

Signed-off-by: Ruidong Cao <caoruidong@huawei.com>
  • Loading branch information
caoruidong authored and jshachm committed Nov 22, 2018
1 parent c883da2 commit 65ce7d3
Show file tree
Hide file tree
Showing 6 changed files with 697 additions and 158 deletions.
8 changes: 8 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,14 @@ func (a *agentGRPC) UpdateRoutes(ctx context.Context, req *pb.UpdateRoutesReques
return a.sandbox.updateRoutes(nil, req.Routes)
}

func (a *agentGRPC) ListInterfaces(ctx context.Context, req *pb.ListInterfacesRequest) (*pb.Interfaces, error) {
return a.sandbox.listInterfaces(nil)
}

func (a *agentGRPC) ListRoutes(ctx context.Context, req *pb.ListRoutesRequest) (*pb.Routes, error) {
return a.sandbox.listRoutes(nil)
}

func (a *agentGRPC) OnlineCPUMem(ctx context.Context, req *pb.OnlineCPUMemRequest) (*gpb.Empty, error) {
if !req.Wait {
go a.onlineCPUMem(req)
Expand Down
35 changes: 33 additions & 2 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,33 @@ func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface,
return &ifc, nil
}

func (s *sandbox) listInterfaces(netHandle *netlink.Handle) (*pb.Interfaces, error) {
var err error
if netHandle == nil {
netHandle, err = netlink.NewHandle(unix.NETLINK_ROUTE)
if err != nil {
return nil, err
}
defer netHandle.Delete()
}

links, err := netHandle.LinkList()
if err != nil {
return nil, err
}

var interfaces pb.Interfaces
for _, link := range links {
ifc, err := getInterface(netHandle, link)
if err != nil {
agentLog.WithField("link", link).WithError(err).Error("listInterfaces() failed")
return &interfaces, err
}
interfaces.Interfaces = append(interfaces.Interfaces, ifc)
}
return &interfaces, nil
}

////////////
// Routes //
////////////
Expand Down Expand Up @@ -420,11 +447,15 @@ func (s *sandbox) updateRoutes(netHandle *netlink.Handle, requestedRoutes *pb.Ro
return requestedRoutes, err
}

func (s *sandbox) listRoutes(netHandle *netlink.Handle) (*pb.Routes, error) {
return getCurrentRoutes(netHandle)
}

//getCurrentRoutes is a helper to gather existing routes in gRPC protocol format
func getCurrentRoutes(netHandle *netlink.Handle) (*pb.Routes, error) {

var err error
if netHandle == nil {
netHandle, err := netlink.NewHandle(unix.NETLINK_ROUTE)
netHandle, err = netlink.NewHandle(unix.NETLINK_ROUTE)
if err != nil {
return nil, err
}
Expand Down
89 changes: 89 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,92 @@ func TestUpdateRoutes(t *testing.T) {
assert.True(t, reflect.DeepEqual(results.Routes[0], testRoutes.Routes[1]),
"Interface created didn't match: got %+v, expecting %+v", results.Routes[0], testRoutes.Routes[1])
}

func TestListInterfaces(t *testing.T) {
tearDown := setupNetworkTest(t)
defer tearDown()

assert := assert.New(t)

s := sandbox{}
ifc := pb.Interface{
Name: "enoNumber",
Mtu: 1500,
HwAddr: "02:00:ca:fe:00:48",
}
ip := pb.IPAddress{
Family: 0,
Address: "192.168.0.101",
Mask: "24",
}
ifc.IPAddresses = append(ifc.IPAddresses, &ip)
netHandle, _ := netlink.NewHandle()
defer netHandle.Delete()
// create a dummy link that we can test update on
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
MTU: 1500,
TxQLen: -1,
Name: "ifc-name",
HardwareAddr: macAddr,
},
}
netHandle.LinkAdd(link)
netHandle.LinkSetUp(link)
s.updateInterface(netHandle, &ifc)
//
// With a link populated, check to see if we can successfully list:
//
results, err := s.listInterfaces(nil)
assert.Nil(err, "Expected to list all interfaces")
assert.True(reflect.DeepEqual(results.Interfaces[1], &ifc),
"Interface listed didn't match: got %+v, expecting %+v", results.Interfaces[1], &ifc)
}

func TestListRoutes(t *testing.T) {
tearDown := setupNetworkTest(t)
defer tearDown()

assert := assert.New(t)

s := sandbox{}

// create a dummy link which we'll play with
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
MTU: 1500,
TxQLen: -1,
Name: "ifc-name",
HardwareAddr: macAddr,
},
}
netHandle, _ := netlink.NewHandle()
defer netHandle.Delete()

netHandle.LinkAdd(link)
if err := netHandle.LinkSetUp(link); err != nil {
t.Fatal(err)
}
netlinkAddr, _ := netlink.ParseAddr("192.168.0.2/16")
netHandle.AddrAdd(link, netlinkAddr)

//Test a simple route setup:
inputRoutesSimple := []*pb.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 253, Device: "ifc-name"},
}

testRoutes := &pb.Routes{
Routes: inputRoutesSimple,
}

s.updateRoutes(netHandle, testRoutes)
results, err := s.listRoutes(nil)
assert.Nil(err, "Expected to list all routes")
assert.True(reflect.DeepEqual(results.Routes[0], inputRoutesSimple[0]),
"Route listed didn't match: got %+v, expecting %+v", results.Routes[0], inputRoutesSimple[0])
assert.True(reflect.DeepEqual(results.Routes[1], inputRoutesSimple[1]),
"Route listed didn't match: got %+v, expecting %+v", results.Routes[1], inputRoutesSimple[1])
}
Loading

0 comments on commit 65ce7d3

Please sign in to comment.