Skip to content

Commit

Permalink
Merge pull request kubevirt#846 from mlsorensen/filter-gateway-dst-route
Browse files Browse the repository at this point in the history
Filter pod route destined to default gateway
  • Loading branch information
vladikr authored Mar 28, 2018
2 parents 9823b02 + c5c69d0 commit aed5d0d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
18 changes: 12 additions & 6 deletions pkg/virt-launcher/virtwrap/network/dhcp/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@ func SingleClientDHCPServer(

log.Log.Info("Starting SingleClientDHCPServer")

dhcpOptions := dhcp.Options{
dhcp.OptionSubnetMask: []byte(clientMask),
dhcp.OptionRouter: []byte(routerIP),
dhcp.OptionDomainNameServer: bytes.Join(dnsIPs, nil),
}

netRoutes := FormClasslessRoutes(routes, routerIP)

if netRoutes != nil {
dhcpOptions[dhcp.OptionClasslessRouteFormat] = netRoutes
}

handler := &DHCPHandler{
clientIP: clientIP,
clientMAC: clientMAC,
serverIP: serverIP.To4(),
leaseDuration: infiniteLease,
options: dhcp.Options{
dhcp.OptionSubnetMask: []byte(clientMask),
dhcp.OptionRouter: []byte(routerIP),
dhcp.OptionDomainNameServer: bytes.Join(dnsIPs, nil),
dhcp.OptionClasslessRouteFormat: netRoutes,
},
options: dhcpOptions,
}

l, err := dhcpConn.NewUDP4BoundListener(serverIface, ":67")
Expand Down
26 changes: 19 additions & 7 deletions pkg/virt-launcher/virtwrap/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,8 @@ func discoverPodNetworkInterface(nic *VIF) (netlink.Link, error) {
return nil, fmt.Errorf("No gateway address found in routes for %s", podInterface)
}
nic.Gateway = routes[0].Gw
var dhcpRoutes []netlink.Route
if len(routes) > 1 {
// Filter out irrelevant routes
for _, route := range routes[1:] {
if !route.Src.Equal(nic.IP.IP) {
dhcpRoutes = append(dhcpRoutes, route)
}
}
dhcpRoutes := filterPodNetworkRoutes(routes, nic)
nic.Routes = &dhcpRoutes
}

Expand All @@ -354,6 +348,24 @@ func discoverPodNetworkInterface(nic *VIF) (netlink.Link, error) {
return nicLink, nil
}

// 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) {
continue
}

// don't create static route for src == nic
if route.Src != nil && route.Src.Equal(nic.IP.IP) {
continue
}

filteredRoutes = append(filteredRoutes, route)
}
return
}

func preparePodNetworkInterfaces(nic *VIF, nicLink netlink.Link) error {
// Remove IP from POD interface
err := Handler.AddrDel(nicLink, &nic.IP)
Expand Down
15 changes: 15 additions & 0 deletions pkg/virt-launcher/virtwrap/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ var _ = Describe("Network", func() {
})
})

Context("func filterPodNetworkRoutes()", func() {
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)}}
nicRoute := netlink.Route{Src: net.IPv4(10, 35, 0, 6)}
staticRouteList := []netlink.Route{routeAddr, gwRoute, nicRoute, 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))
})
})

Context("Function ParseNameservers()", func() {
It("should return a byte array of nameservers", func() {
ns1, ns2 := []uint8{8, 8, 8, 8}, []uint8{8, 8, 4, 4}
Expand Down

0 comments on commit aed5d0d

Please sign in to comment.