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

Commit

Permalink
ipv6: Add support for ipv6
Browse files Browse the repository at this point in the history
Do not ignore ipv6 addresses and routes. These are now processed
along with ipv4 addresses/routes. Add unit tests to verify ipv6.

Fixes #147

Signed-off-by: Archana Shinde <[email protected]>
  • Loading branch information
amshinde committed Jan 28, 2020
1 parent f1f9414 commit b169476
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
34 changes: 6 additions & 28 deletions virtcontainers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func tapNetworkPair(endpoint Endpoint, queues int, disableVhostNet bool) error {
}

// Clear the IP addresses from the veth interface to prevent ARP conflict
netPair.VirtIface.Addrs, err = netlink.AddrList(link, netlink.FAMILY_V4)
netPair.VirtIface.Addrs, err = netlink.AddrList(link, netlink.FAMILY_ALL)
if err != nil {
return fmt.Errorf("Unable to obtain veth IP addresses: %s", err)
}
Expand Down Expand Up @@ -954,25 +954,21 @@ func generateInterfacesAndRoutes(networkNS NetworkNamespace) ([]*vcTypes.Interfa

var ipAddresses []*vcTypes.IPAddress
for _, addr := range endpoint.Properties().Addrs {
// Skip IPv6 because not supported
if addr.IP.To4() == nil {
// Skip IPv6 because not supported
networkLogger().WithFields(logrus.Fields{
"unsupported-address-type": "ipv6",
"address": addr,
}).Warn("unsupported address")
continue
}
// Skip localhost interface
if addr.IP.IsLoopback() {
continue
}

netMask, _ := addr.Mask.Size()
ipAddress := vcTypes.IPAddress{
Family: netlink.FAMILY_V4,
Address: addr.IP.String(),
Mask: fmt.Sprintf("%d", netMask),
}

if addr.IP.To4() == nil {
ipAddress.Family = netlink.FAMILY_V6
}
ipAddresses = append(ipAddresses, &ipAddress)
}
noarp := endpoint.Properties().Iface.RawFlags & unix.IFF_NOARP
Expand All @@ -997,28 +993,10 @@ func generateInterfacesAndRoutes(networkNS NetworkNamespace) ([]*vcTypes.Interfa

if route.Dst != nil {
r.Dest = route.Dst.String()

if route.Dst.IP.To4() == nil {
// Skip IPv6 because not supported
networkLogger().WithFields(logrus.Fields{
"unsupported-route-type": "ipv6",
"destination": r.Dest,
}).Warn("unsupported route")
continue
}
}

if route.Gw != nil {
gateway := route.Gw.String()

if route.Gw.To4() == nil {
// Skip IPv6 because is is not supported
networkLogger().WithFields(logrus.Fields{
"unsupported-route-type": "ipv6",
"gateway": gateway,
}).Warn("unsupported route")
continue
}
r.Gateway = gateway
}

Expand Down
15 changes: 15 additions & 0 deletions virtcontainers/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package virtcontainers

import (
"fmt"
"net"
"os"
"reflect"
Expand Down Expand Up @@ -40,20 +41,27 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
//
address1 := &net.IPNet{IP: net.IPv4(172, 17, 0, 2), Mask: net.CIDRMask(16, 32)}
address2 := &net.IPNet{IP: net.IPv4(182, 17, 0, 2), Mask: net.CIDRMask(16, 32)}
address3 := &net.IPNet{IP: net.ParseIP("2001:db8:1::242:ac11:2"), Mask: net.CIDRMask(64, 128)}

addrs := []netlink.Addr{
{IPNet: address1, Label: "phyaddr1"},
{IPNet: address2, Label: "phyaddr2"},
{IPNet: address3, Label: "phyaddr3"},
}

// Create a couple of routes:
dst2 := &net.IPNet{IP: net.IPv4(172, 17, 0, 0), Mask: net.CIDRMask(16, 32)}
src2 := net.IPv4(172, 17, 0, 2)
gw2 := net.IPv4(172, 17, 0, 1)

dstV6 := &net.IPNet{IP: net.ParseIP("2001:db8:1::"), Mask: net.CIDRMask(64, 128)}
gatewayV6 := net.ParseIP("2001:db8:1::1")

routes := []netlink.Route{
{LinkIndex: 329, Dst: nil, Src: nil, Gw: net.IPv4(172, 17, 0, 1), Scope: netlink.Scope(254)},
{LinkIndex: 329, Dst: dst2, Src: src2, Gw: gw2},
{LinkIndex: 329, Dst: dstV6, Src: nil, Gw: nil},
{LinkIndex: 329, Dst: nil, Src: nil, Gw: gatewayV6},
}

networkInfo := NetworkInfo{
Expand Down Expand Up @@ -83,6 +91,7 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
expectedAddresses := []*vcTypes.IPAddress{
{Family: netlink.FAMILY_V4, Address: "172.17.0.2", Mask: "16"},
{Family: netlink.FAMILY_V4, Address: "182.17.0.2", Mask: "16"},
{Family: netlink.FAMILY_V6, Address: "2001:db8:1::242:ac11:2", Mask: "64"},
}

expectedInterfaces := []*vcTypes.Interface{
Expand All @@ -92,6 +101,12 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
expectedRoutes := []*vcTypes.Route{
{Dest: "", Gateway: "172.17.0.1", Device: "eth0", Source: "", Scope: uint32(254)},
{Dest: "172.17.0.0/16", Gateway: "172.17.0.1", Device: "eth0", Source: "172.17.0.2"},
{Dest: "2001:db8:1::/64", Gateway: "", Device: "eth0", Source: ""},
{Dest: "", Gateway: "2001:db8:1::1", Device: "eth0", Source: ""},
}

for _, r := range resRoutes {
fmt.Printf("resRoute: %+v\n", r)
}

assert.Nil(t, err, "unexpected failure when calling generateKataInterfacesAndRoutes")
Expand Down

0 comments on commit b169476

Please sign in to comment.