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

View File

@ -1397,3 +1397,12 @@ func removeTxRateLimiter(endpoint Endpoint, networkNSPath string) error {
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:
//
expectedAddresses := []*pbTypes.IPAddress{
{Family: utils.ConvertNetlinkFamily(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.ConvertNetlinkFamily(netlink.FAMILY_V6), Address: "2001:db8:1::242:ac11:2", Mask: "64"},
{Family: utils.ConvertAddressFamily(netlink.FAMILY_V4), Address: "172.17.0.2", Mask: "16"},
{Family: utils.ConvertAddressFamily(netlink.FAMILY_V4), Address: "182.17.0.2", Mask: "16"},
{Family: utils.ConvertAddressFamily(netlink.FAMILY_V6), Address: "2001:db8:1::242:ac11:2", Mask: "64"},
}
expectedInterfaces := []*pbTypes.Interface{
@ -104,7 +104,7 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
Device: "eth0",
State: netlink.NUD_PERMANENT,
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"
"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"
)
@ -290,11 +290,11 @@ const (
GiB = MiB << 10
)
func ConvertNetlinkFamily(netlinkFamily int32) pbTypes.IPFamily {
switch netlinkFamily {
case netlink.FAMILY_V6:
func ConvertAddressFamily(family int32) pbTypes.IPFamily {
switch family {
case unix.AF_INET6:
return pbTypes.IPFamily_v6
case netlink.FAMILY_V4:
case unix.AF_INET:
fallthrough
default:
return pbTypes.IPFamily_v4