Skip to content

Commit

Permalink
Merge pull request kubevirt#869 from mlsorensen/fix-static-routes
Browse files Browse the repository at this point in the history
publish default route via classless-static-routes
  • Loading branch information
rmohr authored Apr 6, 2018
2 parents 3a383bb + af2af2b commit 9274f38
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
14 changes: 10 additions & 4 deletions pkg/virt-launcher/virtwrap/network/dhcp/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func SingleClientDHCPServer(
dhcp.OptionDomainNameServer: bytes.Join(dnsIPs, nil),
}

netRoutes := FormClasslessRoutes(routes, routerIP)
netRoutes := formClasslessRoutes(routes)

if netRoutes != nil {
dhcpOptions[dhcp.OptionClasslessRouteFormat] = netRoutes
Expand Down Expand Up @@ -131,7 +131,7 @@ func (h *DHCPHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options
}
}

func FormClasslessRoutes(routes *[]netlink.Route, routerIP net.IP) (formattedRoutes []byte) {
func formClasslessRoutes(routes *[]netlink.Route) (formattedRoutes []byte) {
// See RFC4332 for additional information
// (https://tools.ietf.org/html/rfc3442)
// For example:
Expand All @@ -143,11 +143,17 @@ func FormClasslessRoutes(routes *[]netlink.Route, routerIP net.IP) (formattedRou

for _, route := range *routes {
if route.Dst == nil {
continue
route.Dst = &net.IPNet{
IP: net.IPv4(0, 0, 0, 0),
Mask: net.CIDRMask(0, 32),
}
}
ip := route.Dst.IP.To4()
width, _ := route.Dst.Mask.Size()
octets := (width-1)/8 + 1
octets := 0
if width > 0 {
octets = (width-1)/8 + 1
}
newRoute := append([]byte{byte(width)}, ip[0:octets]...)
gateway := route.Gw.To4()
if gateway == nil {
Expand Down
38 changes: 36 additions & 2 deletions pkg/virt-launcher/virtwrap/network/dhcp/dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var _ = Describe("DHCP", func() {
Context("check routes", func() {
It("verify should form correctly", func() {
expected := []byte{4, 224, 0, 0, 0, 0, 24, 192, 168, 1, 192, 168, 2, 1}
gateway := net.IPv4(192, 168, 1, 1)
routes := []netlink.Route{
netlink.Route{
LinkIndex: 3,
Expand All @@ -52,9 +51,44 @@ var _ = Describe("DHCP", func() {
},
}

dhcpRoutes := FormClasslessRoutes(&routes, gateway)
dhcpRoutes := formClasslessRoutes(&routes)
Expect(dhcpRoutes).To(Equal(expected))
})

It("should build OpenShift routes correctly", func() {
expected := []byte{0, 10, 129, 0, 1, 14, 10, 128, 0, 0, 0, 0, 4, 224, 0, 0, 0, 0}
gatewayRoute := netlink.Route{Gw: net.IPv4(10, 129, 0, 1)}
staticRoute1 := netlink.Route{
Dst: &net.IPNet{
IP: net.IPv4(10, 128, 0, 0),
Mask: net.CIDRMask(14, 32),
},
}
staticRoute2 := netlink.Route{
Dst: &net.IPNet{
IP: net.IPv4(224, 0, 0, 0),
Mask: net.CIDRMask(4, 32),
},
}
routes := []netlink.Route{gatewayRoute, staticRoute1, staticRoute2}
routeBytes := formClasslessRoutes(&routes)
Expect(routeBytes).To(Equal(expected))
})

It("should build Calico routes correctly", func() {
expected := []byte{0, 169, 254, 1, 1, 32, 169, 254, 1, 1, 0, 0, 0, 0}
gatewayRoute := netlink.Route{Gw: net.IPv4(169, 254, 1, 1)}
staticRoute1 := netlink.Route{
Dst: &net.IPNet{
IP: net.IPv4(169, 254, 1, 1),
Mask: net.CIDRMask(32, 32),
},
}

routes := []netlink.Route{gatewayRoute, staticRoute1}
routeBytes := formClasslessRoutes(&routes)
Expect(routeBytes).To(Equal(expected))
})
})

Context("function convertSearchDomainsToBytes(searchDomainStrings []string) ([]byte, error)", func() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/virt-launcher/virtwrap/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ func discoverPodNetworkInterface(nic *VIF) (netlink.Link, error) {

// filter out irrelevant routes
func filterPodNetworkRoutes(routes []netlink.Route, nic *VIF) (filteredRoutes []netlink.Route) {
for _, route := range routes[1:] {
// don't create static route to default gateway
if route.Dst != nil && route.Dst.IP.Equal(nic.Gateway) && route.Src.Equal(nil) {
for _, route := range routes {
// don't create empty static routes
if route.Dst == nil && route.Src.Equal(nil) && route.Gw.Equal(nil) {
continue
}

Expand Down
16 changes: 11 additions & 5 deletions pkg/virt-launcher/virtwrap/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,23 @@ var _ = Describe("Network", func() {
})

Context("func filterPodNetworkRoutes()", func() {
defRoute := netlink.Route{
Gw: net.IPv4(10, 35, 0, 1),
}
staticRoute := netlink.Route{
Dst: &net.IPNet{IP: net.IPv4(10, 45, 0, 10), Mask: net.CIDRMask(32, 32)},
Gw: net.IPv4(10, 25, 0, 1),
}
gwRoute := netlink.Route{Dst: &net.IPNet{IP: net.IPv4(10, 35, 0, 1), Mask: net.CIDRMask(32, 32)}}
gwRoute := netlink.Route{
Dst: &net.IPNet{IP: net.IPv4(10, 35, 0, 1), Mask: net.CIDRMask(32, 32)},
}
nicRoute := netlink.Route{Src: net.IPv4(10, 35, 0, 6)}
staticRouteList := []netlink.Route{routeAddr, gwRoute, nicRoute, staticRoute}
emptyRoute := netlink.Route{}
staticRouteList := []netlink.Route{defRoute, gwRoute, nicRoute, emptyRoute, staticRoute}

It("should remove default gateway and source IP from routes, leaving others intact", func() {
expected := []netlink.Route{staticRoute}
Expect(filterPodNetworkRoutes(staticRouteList, testNic)).To(Equal(expected))
It("should remove empty routes, and routes matching nic, leaving others intact", func() {
expectedRouteList := []netlink.Route{defRoute, gwRoute, staticRoute}
Expect(filterPodNetworkRoutes(staticRouteList, testNic)).To(Equal(expectedRouteList))
})
})

Expand Down

0 comments on commit 9274f38

Please sign in to comment.