Skip to content

Commit 9f597f8

Browse files
committed
use a routing protocol number to identify vxrnet routes
1 parent 684c397 commit 9f597f8

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

const.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package vxrouter
22

33
// usefule constants for the whole project
44
const (
5-
Version = "0.0.4"
6-
EnvPrefix = "VXR_"
7-
NetworkDriver = "vxrNet"
8-
IpamDriver = "vxrIpam"
5+
Version = "0.0.4"
6+
EnvPrefix = "VXR_"
7+
NetworkDriver = "vxrNet"
8+
IpamDriver = "vxrIpam"
9+
DefaultReqAddrSleepTimeMS = 100
10+
DefaultRouteProto = 192
911
)

docker/core/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ func (c *Core) ConnectAndGetAddress(addr, poolid string) (*net.IPNet, error) {
171171
}
172172

173173
//exclude network and (normal) broadcast addresses by default
174-
xf := getEnvIntWithDefault(envPrefix+"excludefirst", nr.Options["excludefirst"], 1)
175-
xl := getEnvIntWithDefault(envPrefix+"excludelast", nr.Options["excludelast"], 1)
174+
xf := vxrouter.GetEnvIntWithDefault(envPrefix+"excludefirst", nr.Options["excludefirst"], 1)
175+
xl := vxrouter.GetEnvIntWithDefault(envPrefix+"excludelast", nr.Options["excludelast"], 1)
176176

177177
hi, err := host.GetOrCreateInterface(nr.Name, gw, nr.Options)
178178
if err != nil {

docker/core/functions.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ package core
33
import (
44
"fmt"
55
"net"
6-
"os"
7-
"strconv"
86
"strings"
97

10-
log "github.com/Sirupsen/logrus"
118
"github.com/docker/docker/api/types"
129
)
1310

@@ -20,22 +17,6 @@ func poolFromNR(nr *types.NetworkResource) (string, error) {
2017
return "", fmt.Errorf("pool not found")
2118
}
2219

23-
func getEnvIntWithDefault(val, opt string, def int) int { //nolint: unparam
24-
e := os.Getenv(val)
25-
if e == "" {
26-
e = opt
27-
}
28-
if e == "" {
29-
return def
30-
}
31-
ei, err := strconv.Atoi(e)
32-
if err != nil {
33-
log.WithField("string", e).WithError(err).Warnf("failed to convert string to int, using default")
34-
return def
35-
}
36-
return ei
37-
}
38-
3920
func poolFromID(poolid string) string {
4021
return strings.TrimPrefix(poolid, ipamDriverName+"/")
4122
}

funcs.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package vxrouter
2+
3+
import (
4+
"os"
5+
"strconv"
6+
7+
log "github.com/Sirupsen/logrus"
8+
)
9+
10+
// GetEnvIntWithDefault gets value, prioritizing first opt, if it is not empty, then the environment variable specified by val, and lastly the default.
11+
func GetEnvIntWithDefault(val, opt string, def int) int { //nolint: unparam
12+
e := os.Getenv(val)
13+
if e == "" {
14+
e = opt
15+
}
16+
if e == "" {
17+
return def
18+
}
19+
ei, err := strconv.Atoi(e)
20+
if err != nil {
21+
log.WithField("string", e).WithError(err).Warnf("failed to convert string to int, using default")
22+
return def
23+
}
24+
return ei
25+
}

host/interface.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010
"github.com/vishvananda/netlink"
1111

1212
"github.com/TrilliumIT/iputil"
13+
"github.com/TrilliumIT/vxrouter"
1314
"github.com/TrilliumIT/vxrouter/macvlan"
1415
"github.com/TrilliumIT/vxrouter/vxlan"
1516
)
1617

1718
var (
18-
rwm = make(map[string]*sync.RWMutex)
19-
rwmLock sync.Mutex
19+
rwm = make(map[string]*sync.RWMutex)
20+
rwmLock sync.Mutex
21+
routeProto = vxrouter.GetEnvIntWithDefault(vxrouter.EnvPrefix+"ROUTE_PROTO", "", vxrouter.DefaultRouteProto)
22+
reqAddrSleepTime = time.Duration(vxrouter.GetEnvIntWithDefault(vxrouter.EnvPrefix+"REQ_ADDR_SLEEP", "", vxrouter.DefaultReqAddrSleepTimeMS)) * time.Millisecond
2023
)
2124

2225
// Interface holds a vxlan and a host macvlan interface used for the gateway interface on a container network
@@ -172,25 +175,13 @@ func (hi *Interface) Delete() error {
172175
return nil
173176
}
174177

175-
sn, err := hi.getSubnet()
176-
if err != nil {
177-
hi.log.WithError(err).Debug("failed to get subnet for host interface")
178-
return err
179-
}
180-
181178
// if there are any other routes, don't delete
182-
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{LinkIndex: hi.mvl.GetIndex()}, netlink.RT_FILTER_OIF)
179+
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{LinkIndex: hi.mvl.GetIndex(), Protocol: routeProto}, netlink.RT_FILTER_OIF|netlink.RT_FILTER_PROTOCOL)
183180
if err != nil {
184181
hi.log.WithError(err).Error("failed to get routes")
185182
return err
186183
}
187184
for _, r := range routes {
188-
if r.Dst.IP.IsLinkLocalUnicast() {
189-
continue
190-
}
191-
if iputil.SubnetEqualSubnet(r.Dst, sn) {
192-
continue
193-
}
194185
hi.log.WithField("r.Dst", r.Dst.String()).Debug("other routes found on this device, not deleting")
195186
return nil
196187
}
@@ -228,6 +219,11 @@ func (hi *Interface) SelectAddress(reqAddress net.IP, propTime, respTime time.Du
228219
var ip *net.IPNet
229220
var err error
230221

222+
var sleepTime time.Duration
223+
if reqAddress != nil {
224+
sleepTime = reqAddrSleepTime
225+
}
226+
231227
stop := time.Now().Add(respTime)
232228
for time.Now().Before(stop) {
233229
ip, err = hi.selectAddress(reqAddress, propTime, xf, xl)
@@ -238,9 +234,7 @@ func (hi *Interface) SelectAddress(reqAddress net.IP, propTime, respTime time.Du
238234
if ip != nil {
239235
break
240236
}
241-
if reqAddress != nil {
242-
time.Sleep(100 * time.Millisecond)
243-
}
237+
time.Sleep(sleepTime)
244238
}
245239

246240
if ip == nil {
@@ -289,6 +283,7 @@ func (hi *Interface) selectAddress(reqAddress net.IP, propTime time.Duration, xf
289283
err = netlink.RouteAdd(&netlink.Route{
290284
LinkIndex: hi.mvl.GetIndex(),
291285
Dst: addrOnly,
286+
Protocol: routeProto,
292287
})
293288
if err != nil {
294289
log.WithError(err).Error("failed to add route")
@@ -332,5 +327,6 @@ func (hi *Interface) DelRoute(ip net.IP) error {
332327
return netlink.RouteDel(&netlink.Route{
333328
LinkIndex: hi.mvl.GetIndex(),
334329
Dst: addrOnly,
330+
Protocol: routeProto,
335331
})
336332
}

0 commit comments

Comments
 (0)