Skip to content

Commit 63eb0a0

Browse files
updated routes and added static arp for virtual gw ip (#752)
1 parent 28207fc commit 63eb0a0

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

network/endpoint_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ func addRoutes(interfaceName string, routes []RouteInfo) error {
269269
Gw: route.Gw,
270270
LinkIndex: ifIndex,
271271
Priority: route.Priority,
272+
Protocol: route.Protocol,
273+
Scope: route.Scope,
272274
}
273275

274276
if err := netlink.AddIpRoute(nlRoute); err != nil {
@@ -312,6 +314,8 @@ func deleteRoutes(interfaceName string, routes []RouteInfo) error {
312314
Dst: &route.Dst,
313315
Gw: route.Gw,
314316
LinkIndex: ifIndex,
317+
Protocol: route.Protocol,
318+
Scope: route.Scope,
315319
}
316320

317321
if err := netlink.DeleteIpRoute(nlRoute); err != nil {

network/transparent_endpointclient_linux.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
)
1212

1313
const (
14-
FAKE_GW_IP = "169.254.1.1/32"
15-
DEFAULT_GW = "0.0.0.0/0"
14+
virtualGwIPString = "169.254.1.1/32"
15+
defaultGwCidr = "0.0.0.0/0"
16+
defaultGw = "0.0.0.0"
1617
)
1718

1819
type TransparentEndpointClient struct {
@@ -147,7 +148,44 @@ func (client *TransparentEndpointClient) ConfigureContainerInterfacesAndRoutes(e
147148
return err
148149
}
149150

150-
return addRoutes(client.containerVethName, epInfo.Routes)
151+
//ip route del 10.240.0.0/12 dev eth0 (removing kernel subnet route added by above call)
152+
for _, ipAddr := range epInfo.IPAddresses {
153+
_, ipnet, _ := net.ParseCIDR(ipAddr.String())
154+
routeInfo := RouteInfo{
155+
Dst: *ipnet,
156+
Scope: netlink.RT_SCOPE_LINK,
157+
Protocol: netlink.RTPROT_KERNEL,
158+
}
159+
if err := deleteRoutes(client.containerVethName, []RouteInfo{routeInfo}); err != nil {
160+
return err
161+
}
162+
}
163+
164+
//add route for virtualgwip
165+
//ip route add 169.254.1.1/32 dev eth0
166+
virtualGwIP, virtualGwNet, _ := net.ParseCIDR(virtualGwIPString)
167+
routeInfo := RouteInfo{
168+
Dst: *virtualGwNet,
169+
Scope: netlink.RT_SCOPE_LINK,
170+
}
171+
if err := addRoutes(client.containerVethName, []RouteInfo{routeInfo}); err != nil {
172+
return err
173+
}
174+
175+
//ip route add default via 169.254.1.1 dev eth0
176+
_, defaultIPNet, _ := net.ParseCIDR(defaultGwCidr)
177+
dstIP := net.IPNet{IP: net.ParseIP(defaultGw), Mask: defaultIPNet.Mask}
178+
routeInfo = RouteInfo{
179+
Dst: dstIP,
180+
Gw: virtualGwIP,
181+
}
182+
if err := addRoutes(client.containerVethName, []RouteInfo{routeInfo}); err != nil {
183+
return err
184+
}
185+
186+
//arp -s 169.254.1.1 e3:45:f4:ac:34:12 - add static arp entry for virtualgwip to hostveth interface mac
187+
log.Printf("[net] Adding static arp for IP address %v and MAC %v in Container namespace", virtualGwNet.String(), client.hostVethMac)
188+
return netlink.AddOrRemoveStaticArp(netlink.ADD, client.containerVethName, virtualGwNet.IP, client.hostVethMac, false)
151189
}
152190

153191
func (client *TransparentEndpointClient) DeleteEndpoints(ep *endpoint) error {

0 commit comments

Comments
 (0)