]>
Commit | Line | Data |
---|---|---|
8a71d93d DS |
1 | /* |
2 | * Zebra connect code. | |
3 | * Copyright (C) Cumulus Networks, Inc. | |
4 | * Donald Sharp | |
5 | * | |
6 | * This file is part of FRR. | |
7 | * | |
8 | * FRR is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * FRR is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; see the file COPYING; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | #include <zebra.h> | |
23 | ||
24 | #include "thread.h" | |
25 | #include "command.h" | |
26 | #include "network.h" | |
27 | #include "prefix.h" | |
28 | #include "routemap.h" | |
29 | #include "table.h" | |
30 | #include "stream.h" | |
31 | #include "memory.h" | |
32 | #include "zclient.h" | |
33 | #include "filter.h" | |
34 | #include "plist.h" | |
35 | #include "log.h" | |
36 | #include "nexthop.h" | |
694b242f | 37 | #include "nexthop_group.h" |
8a71d93d | 38 | |
547dc642 | 39 | #include "sharp_globals.h" |
86da53ab | 40 | #include "sharp_nht.h" |
8a71d93d DS |
41 | #include "sharp_zebra.h" |
42 | ||
43 | /* Zebra structure to hold current status. */ | |
44 | struct zclient *zclient = NULL; | |
45 | ||
46 | /* For registering threads. */ | |
47 | extern struct thread_master *master; | |
48 | ||
8a71d93d | 49 | /* Inteface addition message from zebra. */ |
ef7bd2a3 | 50 | static int sharp_ifp_create(struct interface *ifp) |
8a71d93d | 51 | { |
8a71d93d DS |
52 | return 0; |
53 | } | |
54 | ||
3c3c3252 | 55 | static int sharp_ifp_destroy(struct interface *ifp) |
8a71d93d | 56 | { |
8a71d93d DS |
57 | return 0; |
58 | } | |
59 | ||
121f9dee | 60 | static int interface_address_add(ZAPI_CALLBACK_ARGS) |
8a71d93d | 61 | { |
121f9dee | 62 | zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
8a71d93d DS |
63 | |
64 | return 0; | |
65 | } | |
66 | ||
121f9dee | 67 | static int interface_address_delete(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
68 | { |
69 | struct connected *c; | |
70 | ||
121f9dee | 71 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
8a71d93d DS |
72 | |
73 | if (!c) | |
74 | return 0; | |
75 | ||
721c0857 | 76 | connected_free(&c); |
8a71d93d DS |
77 | return 0; |
78 | } | |
79 | ||
ddbf3e60 | 80 | static int sharp_ifp_up(struct interface *ifp) |
8a71d93d | 81 | { |
8a71d93d DS |
82 | return 0; |
83 | } | |
84 | ||
b0b69e59 | 85 | static int sharp_ifp_down(struct interface *ifp) |
8a71d93d | 86 | { |
8a71d93d DS |
87 | return 0; |
88 | } | |
89 | ||
0cf08685 DS |
90 | void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id, |
91 | uint8_t instance, struct nexthop_group *nhg, | |
6b98d34f DS |
92 | uint32_t routes) |
93 | { | |
94 | uint32_t temp, i; | |
dbc1bf46 | 95 | bool v4 = false; |
6b98d34f DS |
96 | |
97 | zlog_debug("Inserting %u routes", routes); | |
98 | ||
dbc1bf46 DS |
99 | if (p->family == AF_INET) { |
100 | v4 = true; | |
101 | temp = ntohl(p->u.prefix4.s_addr); | |
102 | } else | |
103 | temp = ntohl(p->u.val32[3]); | |
104 | ||
547dc642 | 105 | monotime(&sg.r.t_start); |
6b98d34f | 106 | for (i = 0; i < routes; i++) { |
0cf08685 | 107 | route_add(p, vrf_id, (uint8_t)instance, nhg); |
dbc1bf46 DS |
108 | if (v4) |
109 | p->u.prefix4.s_addr = htonl(++temp); | |
110 | else | |
111 | p->u.val32[3] = htonl(++temp); | |
6b98d34f DS |
112 | } |
113 | } | |
114 | ||
0cf08685 DS |
115 | void sharp_remove_routes_helper(struct prefix *p, vrf_id_t vrf_id, |
116 | uint8_t instance, uint32_t routes) | |
6b98d34f DS |
117 | { |
118 | uint32_t temp, i; | |
dbc1bf46 | 119 | bool v4 = false; |
6b98d34f DS |
120 | |
121 | zlog_debug("Removing %u routes", routes); | |
122 | ||
dbc1bf46 DS |
123 | if (p->family == AF_INET) { |
124 | v4 = true; | |
125 | temp = ntohl(p->u.prefix4.s_addr); | |
126 | } else | |
127 | temp = ntohl(p->u.val32[3]); | |
128 | ||
547dc642 | 129 | monotime(&sg.r.t_start); |
6b98d34f | 130 | for (i = 0; i < routes; i++) { |
0cf08685 | 131 | route_delete(p, vrf_id, (uint8_t)instance); |
dbc1bf46 DS |
132 | if (v4) |
133 | p->u.prefix4.s_addr = htonl(++temp); | |
134 | else | |
135 | p->u.val32[3] = htonl(++temp); | |
6b98d34f DS |
136 | } |
137 | } | |
138 | ||
b939f6ff | 139 | static void handle_repeated(bool installed) |
6b98d34f | 140 | { |
547dc642 DS |
141 | struct prefix p = sg.r.orig_prefix; |
142 | sg.r.repeat--; | |
6b98d34f | 143 | |
547dc642 | 144 | if (sg.r.repeat <= 0) |
6b98d34f DS |
145 | return; |
146 | ||
147 | if (installed) { | |
547dc642 | 148 | sg.r.removed_routes = 0; |
0cf08685 DS |
149 | sharp_remove_routes_helper(&p, sg.r.vrf_id, |
150 | sg.r.inst, sg.r.total_routes); | |
6b98d34f DS |
151 | } |
152 | ||
f54f37c1 | 153 | if (!installed) { |
547dc642 | 154 | sg.r.installed_routes = 0; |
0cf08685 DS |
155 | sharp_install_routes_helper(&p, sg.r.vrf_id, sg.r.inst, |
156 | &sg.r.nhop_group, | |
547dc642 | 157 | sg.r.total_routes); |
6b98d34f DS |
158 | } |
159 | } | |
160 | ||
121f9dee | 161 | static int route_notify_owner(ZAPI_CALLBACK_ARGS) |
8a71d93d | 162 | { |
25c84d86 | 163 | struct timeval r; |
8a71d93d DS |
164 | struct prefix p; |
165 | enum zapi_route_notify_owner note; | |
28610f7e | 166 | uint32_t table; |
8a71d93d | 167 | |
28610f7e | 168 | if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, ¬e)) |
8a71d93d DS |
169 | return -1; |
170 | ||
5e54c602 DS |
171 | switch (note) { |
172 | case ZAPI_ROUTE_INSTALLED: | |
547dc642 DS |
173 | sg.r.installed_routes++; |
174 | if (sg.r.total_routes == sg.r.installed_routes) { | |
175 | monotime(&sg.r.t_end); | |
176 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
051a0be4 DL |
177 | zlog_debug("Installed All Items %jd.%ld", |
178 | (intmax_t)r.tv_sec, (long)r.tv_usec); | |
6b98d34f DS |
179 | handle_repeated(true); |
180 | } | |
5e54c602 DS |
181 | break; |
182 | case ZAPI_ROUTE_FAIL_INSTALL: | |
183 | zlog_debug("Failed install of route"); | |
184 | break; | |
185 | case ZAPI_ROUTE_BETTER_ADMIN_WON: | |
186 | zlog_debug("Better Admin Distance won over us"); | |
187 | break; | |
188 | case ZAPI_ROUTE_REMOVED: | |
547dc642 DS |
189 | sg.r.removed_routes++; |
190 | if (sg.r.total_routes == sg.r.removed_routes) { | |
191 | monotime(&sg.r.t_end); | |
192 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
051a0be4 DL |
193 | zlog_debug("Removed all Items %jd.%ld", |
194 | (intmax_t)r.tv_sec, (long)r.tv_usec); | |
6b98d34f DS |
195 | handle_repeated(false); |
196 | } | |
5e54c602 DS |
197 | break; |
198 | case ZAPI_ROUTE_REMOVE_FAIL: | |
199 | zlog_debug("Route removal Failure"); | |
200 | break; | |
201 | } | |
8a71d93d DS |
202 | return 0; |
203 | } | |
204 | ||
205 | static void zebra_connected(struct zclient *zclient) | |
206 | { | |
207 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
67a9eda9 DS |
208 | |
209 | /* | |
210 | * Do not actually turn this on yet | |
211 | * This is just the start of the infrastructure needed here | |
212 | * This can be fixed at a later time. | |
213 | * | |
214 | * zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, | |
215 | * ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
216 | */ | |
8a71d93d DS |
217 | } |
218 | ||
7d061b3c | 219 | void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label) |
ab18a495 | 220 | { |
7d061b3c | 221 | zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP); |
ab18a495 DS |
222 | } |
223 | ||
0cf08685 DS |
224 | void route_add(struct prefix *p, vrf_id_t vrf_id, |
225 | uint8_t instance, struct nexthop_group *nhg) | |
8a71d93d DS |
226 | { |
227 | struct zapi_route api; | |
228 | struct zapi_nexthop *api_nh; | |
694b242f DS |
229 | struct nexthop *nh; |
230 | int i = 0; | |
8a71d93d DS |
231 | |
232 | memset(&api, 0, sizeof(api)); | |
0cf08685 | 233 | api.vrf_id = vrf_id; |
8a71d93d | 234 | api.type = ZEBRA_ROUTE_SHARP; |
ae252c02 | 235 | api.instance = instance; |
8a71d93d DS |
236 | api.safi = SAFI_UNICAST; |
237 | memcpy(&api.prefix, p, sizeof(*p)); | |
238 | ||
a3896844 | 239 | SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION); |
8a71d93d DS |
240 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
241 | ||
694b242f DS |
242 | for (ALL_NEXTHOPS_PTR(nhg, nh)) { |
243 | api_nh = &api.nexthops[i]; | |
0cf08685 | 244 | api_nh->vrf_id = nh->vrf_id; |
694b242f | 245 | api_nh->type = nh->type; |
bd054c1a DS |
246 | api_nh->weight = nh->weight; |
247 | ||
694b242f DS |
248 | switch (nh->type) { |
249 | case NEXTHOP_TYPE_IPV4: | |
250 | api_nh->gate = nh->gate; | |
251 | break; | |
252 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
253 | api_nh->gate = nh->gate; | |
254 | api_nh->ifindex = nh->ifindex; | |
255 | break; | |
256 | case NEXTHOP_TYPE_IFINDEX: | |
257 | api_nh->ifindex = nh->ifindex; | |
258 | break; | |
259 | case NEXTHOP_TYPE_IPV6: | |
260 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
261 | break; | |
262 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
263 | api_nh->ifindex = nh->ifindex; | |
264 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
265 | break; | |
266 | case NEXTHOP_TYPE_BLACKHOLE: | |
267 | api_nh->bh_type = nh->bh_type; | |
268 | break; | |
269 | } | |
054af19a MS |
270 | |
271 | if (nh->nh_label && nh->nh_label->num_labels > 0) { | |
272 | int j; | |
273 | ||
274 | SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL); | |
275 | ||
276 | api_nh->label_num = nh->nh_label->num_labels; | |
277 | for (j = 0; j < nh->nh_label->num_labels; j++) | |
278 | api_nh->labels[j] = nh->nh_label->label[j]; | |
279 | } | |
280 | ||
694b242f DS |
281 | i++; |
282 | } | |
283 | api.nexthop_num = i; | |
8a71d93d DS |
284 | |
285 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api); | |
286 | } | |
287 | ||
0cf08685 | 288 | void route_delete(struct prefix *p, vrf_id_t vrf_id, uint8_t instance) |
8a71d93d DS |
289 | { |
290 | struct zapi_route api; | |
291 | ||
292 | memset(&api, 0, sizeof(api)); | |
0cf08685 | 293 | api.vrf_id = vrf_id; |
8a71d93d DS |
294 | api.type = ZEBRA_ROUTE_SHARP; |
295 | api.safi = SAFI_UNICAST; | |
ae252c02 | 296 | api.instance = instance; |
8a71d93d DS |
297 | memcpy(&api.prefix, p, sizeof(*p)); |
298 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
299 | ||
300 | return; | |
301 | } | |
302 | ||
91529dc8 | 303 | void sharp_zebra_nexthop_watch(struct prefix *p, vrf_id_t vrf_id, bool import, |
b47dc61a | 304 | bool watch, bool connected) |
0ae8130d | 305 | { |
b47dc61a | 306 | int command; |
0ae8130d | 307 | |
b47dc61a DS |
308 | if (!import) { |
309 | command = ZEBRA_NEXTHOP_REGISTER; | |
310 | ||
311 | if (!watch) | |
312 | command = ZEBRA_NEXTHOP_UNREGISTER; | |
313 | } else { | |
314 | command = ZEBRA_IMPORT_ROUTE_REGISTER; | |
315 | ||
316 | if (!watch) | |
317 | command = ZEBRA_IMPORT_ROUTE_UNREGISTER; | |
318 | } | |
0ae8130d | 319 | |
91529dc8 | 320 | if (zclient_send_rnh(zclient, command, p, connected, vrf_id) < 0) |
b3beaea0 A |
321 | zlog_warn("%s: Failure to send nexthop to zebra", |
322 | __PRETTY_FUNCTION__); | |
0ae8130d DS |
323 | } |
324 | ||
67a9eda9 | 325 | static int sharp_debug_nexthops(struct zapi_route *api) |
0ae8130d | 326 | { |
0ae8130d | 327 | int i; |
67a9eda9 | 328 | char buf[PREFIX_STRLEN]; |
0ae8130d | 329 | |
67a9eda9 DS |
330 | for (i = 0; i < api->nexthop_num; i++) { |
331 | struct zapi_nexthop *znh = &api->nexthops[i]; | |
0ae8130d DS |
332 | |
333 | switch (znh->type) { | |
334 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
335 | case NEXTHOP_TYPE_IPV4: | |
336 | zlog_debug( | |
337 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
338 | inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf, | |
339 | sizeof(buf)), | |
340 | znh->type, znh->ifindex, znh->vrf_id, | |
341 | znh->label_num); | |
342 | break; | |
343 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
344 | case NEXTHOP_TYPE_IPV6: | |
345 | zlog_debug( | |
346 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
347 | inet_ntop(AF_INET6, &znh->gate.ipv6, buf, | |
348 | sizeof(buf)), | |
349 | znh->type, znh->ifindex, znh->vrf_id, | |
350 | znh->label_num); | |
351 | break; | |
352 | case NEXTHOP_TYPE_IFINDEX: | |
353 | zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d", | |
354 | znh->type, znh->ifindex); | |
355 | break; | |
356 | case NEXTHOP_TYPE_BLACKHOLE: | |
357 | zlog_debug("\tNexthop blackhole"); | |
358 | break; | |
359 | } | |
360 | } | |
67a9eda9 DS |
361 | |
362 | return i; | |
363 | } | |
364 | static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS) | |
365 | { | |
366 | struct sharp_nh_tracker *nht; | |
367 | struct zapi_route nhr; | |
368 | ||
369 | if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) { | |
370 | zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__); | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | zlog_debug("Received update for %pFX", &nhr.prefix); | |
376 | ||
377 | nht = sharp_nh_tracker_get(&nhr.prefix); | |
378 | nht->nhop_num = nhr.nexthop_num; | |
379 | nht->updates++; | |
380 | ||
381 | sharp_debug_nexthops(&nhr); | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | static int sharp_redistribute_route(ZAPI_CALLBACK_ARGS) | |
387 | { | |
388 | struct zapi_route api; | |
389 | ||
390 | if (zapi_route_decode(zclient->ibuf, &api) < 0) | |
391 | zlog_warn("%s: Decode of redistribute failed: %d", | |
392 | __PRETTY_FUNCTION__, | |
393 | ZEBRA_REDISTRIBUTE_ROUTE_ADD); | |
394 | ||
395 | zlog_debug("%s: %pFX (%s)", zserv_command_string(cmd), | |
396 | &api.prefix, zebra_route_string(api.type)); | |
397 | ||
398 | sharp_debug_nexthops(&api); | |
399 | ||
0ae8130d DS |
400 | return 0; |
401 | } | |
402 | ||
8a71d93d DS |
403 | extern struct zebra_privs_t sharp_privs; |
404 | ||
405 | void sharp_zebra_init(void) | |
406 | { | |
996c9314 | 407 | struct zclient_options opt = {.receive_notify = true}; |
8a71d93d | 408 | |
138c5a74 DS |
409 | if_zapi_callbacks(sharp_ifp_create, sharp_ifp_up, |
410 | sharp_ifp_down, sharp_ifp_destroy); | |
411 | ||
26f63a1e | 412 | zclient = zclient_new(master, &opt); |
8a71d93d DS |
413 | |
414 | zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs); | |
415 | zclient->zebra_connected = zebra_connected; | |
8a71d93d DS |
416 | zclient->interface_address_add = interface_address_add; |
417 | zclient->interface_address_delete = interface_address_delete; | |
28b11f81 | 418 | zclient->route_notify_owner = route_notify_owner; |
0ae8130d | 419 | zclient->nexthop_update = sharp_nexthop_update; |
b47dc61a | 420 | zclient->import_check_update = sharp_nexthop_update; |
67a9eda9 DS |
421 | |
422 | zclient->redistribute_route_add = sharp_redistribute_route; | |
423 | zclient->redistribute_route_del = sharp_redistribute_route; | |
8a71d93d | 424 | } |