virtcontainers: Remove the netlink package dependency from network.go

Move the netlink dependent code into network_linux.go.
Other OSes will have to provide the same functions.

Signed-off-by: Samuel Ortiz <s.ortiz@apple.com>
This commit is contained in:
Samuel Ortiz 2021-11-24 16:59:18 +01:00 committed by Samuel Ortiz
parent 7fca5792f7
commit 0269077ebf
4 changed files with 25 additions and 17 deletions

View File

@ -249,13 +249,13 @@ func generateVCNetworkStructures(ctx context.Context, network Network) ([]*pbTyp
netMask, _ := addr.Mask.Size() netMask, _ := addr.Mask.Size()
ipAddress := pbTypes.IPAddress{ ipAddress := pbTypes.IPAddress{
Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Family: pbTypes.IPFamily_v4,
Address: addr.IP.String(), Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask), Mask: fmt.Sprintf("%d", netMask),
} }
if addr.IP.To4() == nil { if addr.IP.To4() == nil {
ipAddress.Family = utils.ConvertNetlinkFamily(netlink.FAMILY_V6) ipAddress.Family = pbTypes.IPFamily_v6
} }
ipAddresses = append(ipAddresses, &ipAddress) ipAddresses = append(ipAddresses, &ipAddress)
} }
@ -275,7 +275,7 @@ func generateVCNetworkStructures(ctx context.Context, network Network) ([]*pbTyp
for _, route := range endpoint.Properties().Routes { for _, route := range endpoint.Properties().Routes {
var r pbTypes.Route var r pbTypes.Route
if route.Protocol == unix.RTPROT_KERNEL { if !validGuestRoute(route) {
continue continue
} }
@ -294,15 +294,14 @@ func generateVCNetworkStructures(ctx context.Context, network Network) ([]*pbTyp
r.Device = endpoint.Name() r.Device = endpoint.Name()
r.Scope = uint32(route.Scope) r.Scope = uint32(route.Scope)
r.Family = utils.ConvertNetlinkFamily((int32)(route.Family)) r.Family = utils.ConvertAddressFamily((int32)(route.Family))
routes = append(routes, &r) routes = append(routes, &r)
} }
for _, neigh := range endpoint.Properties().Neighbors { for _, neigh := range endpoint.Properties().Neighbors {
var n pbTypes.ARPNeighbor var n pbTypes.ARPNeighbor
// We add only static ARP entries if !validGuestNeighbor(neigh) {
if neigh.State != netlink.NUD_PERMANENT {
continue continue
} }
@ -315,11 +314,11 @@ func generateVCNetworkStructures(ctx context.Context, network Network) ([]*pbTyp
} }
n.ToIPAddress = &pbTypes.IPAddress{ n.ToIPAddress = &pbTypes.IPAddress{
Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Family: pbTypes.IPFamily_v4,
Address: neigh.IP.String(), Address: neigh.IP.String(),
} }
if neigh.IP.To4() == nil { if neigh.IP.To4() == nil {
n.ToIPAddress.Family = netlink.FAMILY_V6 n.ToIPAddress.Family = pbTypes.IPFamily_v6
} }
neighs = append(neighs, &n) neighs = append(neighs, &n)

View File

@ -1397,3 +1397,12 @@ func removeTxRateLimiter(endpoint Endpoint, networkNSPath string) error {
return nil return nil
} }
func validGuestRoute(route netlink.Route) bool {
return route.Protocol != unix.RTPROT_KERNEL
}
func validGuestNeighbor(neigh netlink.Neigh) bool {
// We add only static ARP entries
return neigh.State == netlink.NUD_PERMANENT
}

View File

@ -83,9 +83,9 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
// Build expected results: // Build expected results:
// //
expectedAddresses := []*pbTypes.IPAddress{ expectedAddresses := []*pbTypes.IPAddress{
{Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Address: "172.17.0.2", Mask: "16"}, {Family: utils.ConvertAddressFamily(netlink.FAMILY_V4), Address: "172.17.0.2", Mask: "16"},
{Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4), Address: "182.17.0.2", Mask: "16"}, {Family: utils.ConvertAddressFamily(netlink.FAMILY_V4), Address: "182.17.0.2", Mask: "16"},
{Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V6), Address: "2001:db8:1::242:ac11:2", Mask: "64"}, {Family: utils.ConvertAddressFamily(netlink.FAMILY_V6), Address: "2001:db8:1::242:ac11:2", Mask: "64"},
} }
expectedInterfaces := []*pbTypes.Interface{ expectedInterfaces := []*pbTypes.Interface{
@ -104,7 +104,7 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
Device: "eth0", Device: "eth0",
State: netlink.NUD_PERMANENT, State: netlink.NUD_PERMANENT,
Lladdr: "6a:92:3a:59:70:aa", Lladdr: "6a:92:3a:59:70:aa",
ToIPAddress: &pbTypes.IPAddress{Address: "192.168.0.101", Family: utils.ConvertNetlinkFamily(netlink.FAMILY_V4)}, ToIPAddress: &pbTypes.IPAddress{Address: "192.168.0.101", Family: utils.ConvertAddressFamily(netlink.FAMILY_V4)},
}, },
} }

View File

@ -16,7 +16,7 @@ import (
"time" "time"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink" "golang.org/x/sys/unix"
pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols" pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols"
) )
@ -290,11 +290,11 @@ const (
GiB = MiB << 10 GiB = MiB << 10
) )
func ConvertNetlinkFamily(netlinkFamily int32) pbTypes.IPFamily { func ConvertAddressFamily(family int32) pbTypes.IPFamily {
switch netlinkFamily { switch family {
case netlink.FAMILY_V6: case unix.AF_INET6:
return pbTypes.IPFamily_v6 return pbTypes.IPFamily_v6
case netlink.FAMILY_V4: case unix.AF_INET:
fallthrough fallthrough
default: default:
return pbTypes.IPFamily_v4 return pbTypes.IPFamily_v4