From 4c3d9dad5feba88ee05272908e4e73f0c56fbf11 Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Tue, 28 Apr 2020 10:31:36 -0400 Subject: [PATCH] update github.com/moby/ipvs to v1.0.1 to fix IPVS compatiblity issue with old kernels Signed-off-by: Andrew Sy Kim --- go.mod | 4 +-- go.sum | 4 +-- vendor/github.com/moby/ipvs/netlink.go | 47 +++++++++++++++++++++++++- vendor/modules.txt | 2 +- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 7b0f0d47614a2..b59b54a32b050 100644 --- a/go.mod +++ b/go.mod @@ -71,7 +71,7 @@ require ( github.com/lpabon/godbc v0.1.1 // indirect github.com/magiconair/properties v1.8.1 // indirect github.com/miekg/dns v1.1.4 - github.com/moby/ipvs v1.0.0 + github.com/moby/ipvs v1.0.1 github.com/mohae/deepcopy v0.0.0-20170603005431-491d3605edfb // indirect github.com/mrunalp/fileutils v0.0.0-20171103030105-7d4729fb3618 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 @@ -328,7 +328,7 @@ replace ( github.com/mitchellh/go-homedir => github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-wordwrap => github.com/mitchellh/go-wordwrap v1.0.0 github.com/mitchellh/mapstructure => github.com/mitchellh/mapstructure v1.1.2 - github.com/moby/ipvs => github.com/moby/ipvs v1.0.0 + github.com/moby/ipvs => github.com/moby/ipvs v1.0.1 github.com/moby/term => github.com/moby/term v0.0.0-20200312100748-672ec06f55cd github.com/modern-go/concurrent => github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd github.com/modern-go/reflect2 => github.com/modern-go/reflect2 v1.0.1 diff --git a/go.sum b/go.sum index 60915d6e2affe..513e0361f2fca 100644 --- a/go.sum +++ b/go.sum @@ -315,8 +315,8 @@ github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9 github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/moby/ipvs v1.0.0 h1:89i8bPaL2DC0cjyRDv0QQOYUOU4fujziJmhF4ca/mtY= -github.com/moby/ipvs v1.0.0/go.mod h1:2pngiyseZbIKXNv7hsKj3O9UEz30c53MT9005gt2hxQ= +github.com/moby/ipvs v1.0.1 h1:aoZ7fhLTXgDbzVrAnvV+XbKOU8kOET7B3+xULDF/1o0= +github.com/moby/ipvs v1.0.1/go.mod h1:2pngiyseZbIKXNv7hsKj3O9UEz30c53MT9005gt2hxQ= github.com/moby/term v0.0.0-20200312100748-672ec06f55cd h1:aY7OQNf2XqY/JQ6qREWamhI/81os/agb2BAGpcx5yWI= github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= diff --git a/vendor/github.com/moby/ipvs/netlink.go b/vendor/github.com/moby/ipvs/netlink.go index 1a822da4b60ff..ac48d05a31d76 100644 --- a/vendor/github.com/moby/ipvs/netlink.go +++ b/vendor/github.com/moby/ipvs/netlink.go @@ -5,6 +5,7 @@ package ipvs import ( "bytes" "encoding/binary" + "errors" "fmt" "net" "os/exec" @@ -449,7 +450,7 @@ func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) d.ActiveConnections = int(native.Uint16(attr.Value)) case ipvsDestAttrInactiveConnections: d.InactiveConnections = int(native.Uint16(attr.Value)) - case ipvsSvcAttrStats: + case ipvsDestAttrStats: stats, err := assembleStats(attr.Value) if err != nil { return nil, err @@ -458,6 +459,19 @@ func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) } } + // in older kernels (< 3.18), the destination address family attribute doesn't exist so we must + // assume it based on the destination address provided. + if d.AddressFamily == 0 { + // we can't check the address family using net stdlib because netlink returns + // IPv4 addresses as the first 4 bytes in a []byte of length 16 where as + // stdlib expects it as the last 4 bytes. + addressFamily, err := getIPFamily(addressBytes) + if err != nil { + return nil, err + } + d.AddressFamily = addressFamily + } + // parse Address after parse AddressFamily incase of parseIP error if addressBytes != nil { ip, err := parseIP(addressBytes, d.AddressFamily) @@ -470,6 +484,37 @@ func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) return &d, nil } +// getIPFamily parses the IP family based on raw data from netlink. +// For AF_INET, netlink will set the first 4 bytes with trailing zeros +// 10.0.0.1 -> [10 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0] +// For AF_INET6, the full 16 byte array is used: +// 2001:db8:3c4d:15::1a00 -> [32 1 13 184 60 77 0 21 0 0 0 0 0 0 26 0] +func getIPFamily(address []byte) (uint16, error) { + if len(address) == 4 { + return syscall.AF_INET, nil + } + + if isZeros(address) { + return 0, errors.New("could not parse IP family from address data") + } + + // assume IPv4 if first 4 bytes are non-zero but rest of the data is trailing zeros + if !isZeros(address[:4]) && isZeros(address[4:]) { + return syscall.AF_INET, nil + } + + return syscall.AF_INET6, nil +} + +func isZeros(b []byte) bool { + for i := 0; i < len(b); i++ { + if b[i] != 0 { + return false + } + } + return true +} + // parseDestination given a ipvs netlink response this function will respond with a valid destination entry, an error otherwise func (i *Handle) parseDestination(msg []byte) (*Destination, error) { var dst *Destination diff --git a/vendor/modules.txt b/vendor/modules.txt index 1da83cf0958a4..c45abc0bfce83 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -485,7 +485,7 @@ github.com/mistifyio/go-zfs github.com/mitchellh/go-wordwrap # github.com/mitchellh/mapstructure v1.1.2 => github.com/mitchellh/mapstructure v1.1.2 github.com/mitchellh/mapstructure -# github.com/moby/ipvs v1.0.0 => github.com/moby/ipvs v1.0.0 +# github.com/moby/ipvs v1.0.1 => github.com/moby/ipvs v1.0.1 github.com/moby/ipvs # github.com/moby/term v0.0.0-20200312100748-672ec06f55cd => github.com/moby/term v0.0.0-20200312100748-672ec06f55cd github.com/moby/term