Newer
Older
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package proto_netlink
import (
"fmt"
"sync"
"github.com/bio-routing/bio-rd/config"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
type NetlinkWriter struct {
options *config.Netlink
filter *filter.Filter
// Routingtable for buffering, to ensure no double writes (a.k.a rtnetlink: file exists)
mu sync.RWMutex
pt map[bnet.Prefix][]*route.Path
}
func NewNetlinkWriter(options *config.Netlink) *NetlinkWriter {
return &NetlinkWriter{
options: options,
filter: options.ExportFilter,
pt: make(map[bnet.Prefix][]*route.Path),
}
}
// Not supported
func (nw *NetlinkWriter) UpdateNewClient(routingtable.RouteTableClient) error {
return fmt.Errorf("Not supported")
}
// Not supported
func (nw *NetlinkWriter) Register(routingtable.RouteTableClient) {
log.Error("Not supported")
}
// Not supported
func (nw *NetlinkWriter) RegisterWithOptions(routingtable.RouteTableClient, routingtable.ClientOptions) {
log.Error("Not supported")
}
// Not supported
func (nw *NetlinkWriter) Unregister(routingtable.RouteTableClient) {
log.Error("Not supported")
}
// RouteCount returns the number of stored routes
func (nw *NetlinkWriter) RouteCount() int64 {
nw.mu.RLock()
defer nw.mu.RUnlock()
return int64(len(nw.pt))
}
// Add a path to the Kernel using netlink
// This function is triggered by the loc_rib, cause we are subscribed as
// client in the loc_rib
func (nw *NetlinkWriter) AddPath(pfx bnet.Prefix, path *route.Path) error {
// check if for this prefix already a route is existing
existingPaths, ok := nw.pt[pfx]
// if no route exists, add that route
if existingPaths == nil || !ok {
paths := make([]*route.Path, 1)
paths = append(paths, path)
nw.pt[pfx] = paths
// add the route to kernel
return nw.addKernel(pfx, path)
}
// if the new path is already in, don't do anything
for _, ePath := range existingPaths {
if ePath.Equal(path) {
return nil
}
}
existingPaths = append(existingPaths, path)
nw.pt[pfx] = existingPaths
// now add to netlink
return nw.addKernel(pfx, path)
}
// Remove a path from the Kernel using netlink
// This function is triggered by the loc_rib, cause we are subscribed as
// client in the loc_rib
func (nw *NetlinkWriter) RemovePath(pfx bnet.Prefix, path *route.Path) bool {
// check if for this prefix already a route is existing
existingPaths, ok := nw.pt[pfx]
// if no route exists, nothing to do
if existingPaths == nil || !ok {
return true
}
// if the new path is already in: remove
removeIdx := 0
remove := false
for idx, ePath := range existingPaths {
if ePath.Equal(path) {
removeIdx = idx
remove = true
err := nw.removeKernel(pfx, path)
if err != nil {
log.WithError(err).Errorf("Error while removing path %s for prefix %s", path.String(), pfx.String())
remove = false
}
break
}
}
if remove {
existingPaths = append(existingPaths[:removeIdx], existingPaths[removeIdx+1:]...)
nw.pt[pfx] = existingPaths
}
return true
}
// Add pfx/path to kernel
func (nw *NetlinkWriter) addKernel(pfx bnet.Prefix, path *route.Path) error {
route, err := nw.createRoute(pfx, path)
if err != nil {
log.Errorf("Error while creating route: %v", err)
return fmt.Errorf("Error while creating route: %v", err)
}
log.WithFields(log.Fields{
"Prefix": pfx.String(),
"Table": route.Table,
}).Debug("AddPath to netlink")
err = netlink.RouteAdd(route)
if err != nil {
log.Errorf("Error while adding route: %v", err)
return fmt.Errorf("Error while adding route: %v", err)
}
return nil
}
// remove pfx/path from kernel
func (nw *NetlinkWriter) removeKernel(pfx bnet.Prefix, path *route.Path) error {
log.WithFields(log.Fields{
"Prefix": pfx.String(),
}).Debug("Remove from netlink")
route, err := nw.createRoute(pfx, path)
if err != nil {
return fmt.Errorf("Error while creating route: %v", err)
}
err = netlink.RouteDel(route)
if err != nil {
return fmt.Errorf("Error while removing route: %v", err)
}
return nil
}
// create a route from a prefix and a path
func (nw *NetlinkWriter) createRoute(pfx bnet.Prefix, path *route.Path) (*netlink.Route, error) {
if path.Type != route.NetlinkPathType {
}
switch path.Type {
case route.NetlinkPathType:
return nw.createRouteFromNetlink(pfx, path)
case route.BGPPathType:
return nw.createRouteFromBGPPath(pfx, path)
default:
return nil, fmt.Errorf("PathType %d is not supported for adding to netlink", path.Type)
}
}
func (nw *NetlinkWriter) createRouteFromNetlink(pfx bnet.Prefix, path *route.Path) (*netlink.Route, error) {
nlPath := path.NetlinkPath
log.WithFields(log.Fields{
"Dst": nlPath.Dst,
"Src": nlPath.Src,
"NextHop": nlPath.NextHop,
"Priority": nlPath.Priority,
"Protocol": nlPath.Protocol,
"Type": nlPath.Type,
"Table": nw.options.RoutingTable,
}).Debug("created route")
return &netlink.Route{
Dst: nlPath.Dst.GetIPNet(),
Src: nlPath.Src.Bytes(),
Gw: nlPath.NextHop.Bytes(),
Priority: nlPath.Priority,
Type: nlPath.Type,
Table: nw.options.RoutingTable, // config dependent
Protocol: route.ProtoBio, // fix
}, nil
}
func (nw *NetlinkWriter) createRouteFromBGPPath(pfx bnet.Prefix, path *route.Path) (*netlink.Route, error) {
bgpPath := path.BGPPath
log.WithFields(log.Fields{
"Dst": pfx,
"NextHop": bgpPath.NextHop,
"Protocol": "BGP",
"BGPIdentifier": bgpPath.BGPIdentifier,
"Table": nw.options.RoutingTable,
}).Debug("created route")
return &netlink.Route{
Dst: pfx.GetIPNet(),
Gw: bgpPath.NextHop.Bytes(),
Table: nw.options.RoutingTable, // config dependent
Protocol: route.ProtoBio, // fix
}, nil
}