/
routes.go
80 lines (72 loc) · 2.67 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package routing
import (
"errors"
"fmt"
"net"
log "github.com/Sirupsen/logrus"
"github.com/vishvananda/netlink"
)
// AddHostRoute adds a route to a device using netlink into the Linux default namespace.
func addNetlinkRoute(neighborNetwork *net.IPNet, nextHop net.IP, netIface string) error {
iface, err := netlink.LinkByName(netIface)
if err != nil {
return err
}
log.Infof("Adding route learned via BGP for a remote endpoint with:")
log.Infof("IP Prefix: [ %s ] - Next Hop: [ %s ] - Source Interface: [ %s ]", neighborNetwork, nextHop, iface.Attrs().Name)
return netlink.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: iface.Attrs().Index,
Dst: neighborNetwork,
Gw: nextHop,
})
}
// Advertise the local namespace IP prefixes to the bgp neighbors
func OriginateBgpRoute(localPrefix *net.IPNet) error {
log.Infof("Adding this hosts container network [ %s ] into the BGP domain", localPrefix)
_, stderr, err := gobgp(bgpCmd, global, rib, bgpAdd, localPrefix.String(), addrFamily, ipv4)
if err != nil {
return errors.New(stderr.String())
}
return nil
}
// AddHostRoute adds a route to a device using netlink into the Linux default namespace.
func delNetlinkRoute(neighborNetwork *net.IPNet, nextHop net.IP, netIface string) error {
iface, err := netlink.LinkByName(netIface)
if err != nil {
return err
}
log.Infof("Deleting netlink route learned via BGP for the remote endpoint:")
log.Infof("IP Prefix: [ %s ] - Next Hop: [ %s ] - Source Interface: [ %s ]", neighborNetwork, nextHop, iface.Attrs().Name)
return netlink.RouteDel(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: iface.Attrs().Index,
Dst: neighborNetwork,
Gw: nextHop,
})
}
// Add a route to the global namespace using the default gateway to determine the iface
func checkAddRoute(dest *net.IPNet, nh net.IP) error {
gwRoutes, err := netlink.RouteGet(nh)
if err != nil {
return fmt.Errorf("route for the next hop %s could not be found: %v", nh, err)
}
return netlink.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: gwRoutes[0].LinkIndex,
Gw: gwRoutes[0].Gw,
Dst: dest,
})
}
// AddHostRoute adds a host-scoped route to a device.
func addRoute(neighborNetwork *net.IPNet, nextHop net.IP, iface netlink.Link) error {
log.Debugf("Adding route in the default namespace for IPVlan L3 mode with the following:")
log.Debugf("IP Prefix: [ %s ] - Next Hop: [ %s ] - Source Interface: [ %s ]",
neighborNetwork, nextHop, iface.Attrs().Name)
return netlink.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: iface.Attrs().Index,
Dst: neighborNetwork,
Gw: nextHop,
})
}