]> git.proxmox.com Git - mirror_frr.git/blob - staticd/static_zebra.c
staticd: minor refactor NHT register code
[mirror_frr.git] / staticd / static_zebra.c
1 /*
2 * Zebra connect code.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21
22 #include "thread.h"
23 #include "command.h"
24 #include "network.h"
25 #include "prefix.h"
26 #include "routemap.h"
27 #include "table.h"
28 #include "srcdest_table.h"
29 #include "stream.h"
30 #include "memory.h"
31 #include "zclient.h"
32 #include "filter.h"
33 #include "plist.h"
34 #include "log.h"
35 #include "nexthop.h"
36 #include "nexthop_group.h"
37 #include "hash.h"
38 #include "jhash.h"
39
40 #include "static_vrf.h"
41 #include "static_routes.h"
42 #include "static_zebra.h"
43 #include "static_nht.h"
44 #include "static_vty.h"
45 #include "static_debug.h"
46
47 DEFINE_MTYPE_STATIC(STATIC, STATIC_NHT_DATA, "Static Nexthop tracking data");
48 PREDECL_HASH(static_nht_hash);
49
50 struct static_nht_data {
51 struct static_nht_hash_item itm;
52
53 struct prefix nh;
54 safi_t safi;
55
56 vrf_id_t nh_vrf_id;
57
58 uint32_t refcount;
59 uint8_t nh_num;
60 };
61
62 static int static_nht_data_cmp(const struct static_nht_data *nhtd1,
63 const struct static_nht_data *nhtd2)
64 {
65 if (nhtd1->nh_vrf_id != nhtd2->nh_vrf_id)
66 return numcmp(nhtd1->nh_vrf_id, nhtd2->nh_vrf_id);
67 if (nhtd1->safi != nhtd2->safi)
68 return numcmp(nhtd1->safi, nhtd2->safi);
69
70 return prefix_cmp(&nhtd1->nh, &nhtd2->nh);
71 }
72
73 static unsigned int static_nht_data_hash(const struct static_nht_data *nhtd)
74 {
75 unsigned int key = 0;
76
77 key = prefix_hash_key(&nhtd->nh);
78 return jhash_2words(nhtd->nh_vrf_id, nhtd->safi, key);
79 }
80
81 DECLARE_HASH(static_nht_hash, struct static_nht_data, itm, static_nht_data_cmp,
82 static_nht_data_hash);
83
84 static struct static_nht_hash_head static_nht_hash[1];
85
86 /* Zebra structure to hold current status. */
87 struct zclient *zclient;
88 uint32_t zebra_ecmp_count = MULTIPATH_NUM;
89
90 /* Interface addition message from zebra. */
91 static int static_ifp_create(struct interface *ifp)
92 {
93 static_ifindex_update(ifp, true);
94
95 return 0;
96 }
97
98 static int static_ifp_destroy(struct interface *ifp)
99 {
100 static_ifindex_update(ifp, false);
101 return 0;
102 }
103
104 static int interface_address_add(ZAPI_CALLBACK_ARGS)
105 {
106 zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
107
108 return 0;
109 }
110
111 static int interface_address_delete(ZAPI_CALLBACK_ARGS)
112 {
113 struct connected *c;
114
115 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
116
117 if (!c)
118 return 0;
119
120 connected_free(&c);
121 return 0;
122 }
123
124 static int static_ifp_up(struct interface *ifp)
125 {
126 /* Install any static reliant on this interface coming up */
127 static_install_intf_nh(ifp);
128 static_ifindex_update(ifp, true);
129
130 return 0;
131 }
132
133 static int static_ifp_down(struct interface *ifp)
134 {
135 static_ifindex_update(ifp, false);
136
137 return 0;
138 }
139
140 static int route_notify_owner(ZAPI_CALLBACK_ARGS)
141 {
142 struct prefix p;
143 enum zapi_route_notify_owner note;
144 uint32_t table_id;
145 safi_t safi;
146
147 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note, NULL,
148 &safi))
149 return -1;
150
151 switch (note) {
152 case ZAPI_ROUTE_FAIL_INSTALL:
153 static_nht_mark_state(&p, safi, vrf_id, STATIC_NOT_INSTALLED);
154 zlog_warn("%s: Route %pFX failed to install for table: %u",
155 __func__, &p, table_id);
156 break;
157 case ZAPI_ROUTE_BETTER_ADMIN_WON:
158 static_nht_mark_state(&p, safi, vrf_id, STATIC_NOT_INSTALLED);
159 zlog_warn(
160 "%s: Route %pFX over-ridden by better route for table: %u",
161 __func__, &p, table_id);
162 break;
163 case ZAPI_ROUTE_INSTALLED:
164 static_nht_mark_state(&p, safi, vrf_id, STATIC_INSTALLED);
165 break;
166 case ZAPI_ROUTE_REMOVED:
167 static_nht_mark_state(&p, safi, vrf_id, STATIC_NOT_INSTALLED);
168 break;
169 case ZAPI_ROUTE_REMOVE_FAIL:
170 static_nht_mark_state(&p, safi, vrf_id, STATIC_INSTALLED);
171 zlog_warn("%s: Route %pFX failure to remove for table: %u",
172 __func__, &p, table_id);
173 break;
174 }
175
176 return 0;
177 }
178 static void zebra_connected(struct zclient *zclient)
179 {
180 zclient_send_reg_requests(zclient, VRF_DEFAULT);
181 }
182
183 /* API to check whether the configured nexthop address is
184 * one of its local connected address or not.
185 */
186 static bool
187 static_nexthop_is_local(vrf_id_t vrfid, struct prefix *addr, int family)
188 {
189 if (family == AF_INET) {
190 if (if_address_is_local(&addr->u.prefix4, AF_INET, vrfid))
191 return true;
192 } else if (family == AF_INET6) {
193 if (if_address_is_local(&addr->u.prefix6, AF_INET6, vrfid))
194 return true;
195 }
196 return false;
197 }
198 static int static_zebra_nexthop_update(ZAPI_CALLBACK_ARGS)
199 {
200 struct static_nht_data *nhtd, lookup;
201 struct zapi_route nhr;
202 struct prefix matched;
203 afi_t afi = AFI_IP;
204
205 if (!zapi_nexthop_update_decode(zclient->ibuf, &matched, &nhr)) {
206 zlog_err("Failure to decode nexthop update message");
207 return 1;
208 }
209
210 if (matched.family == AF_INET6)
211 afi = AFI_IP6;
212
213 if (nhr.type == ZEBRA_ROUTE_CONNECT) {
214 if (static_nexthop_is_local(vrf_id, &matched,
215 nhr.prefix.family))
216 nhr.nexthop_num = 0;
217 }
218
219 memset(&lookup, 0, sizeof(lookup));
220 lookup.nh = matched;
221 lookup.nh_vrf_id = vrf_id;
222 lookup.safi = nhr.safi;
223
224 nhtd = static_nht_hash_find(static_nht_hash, &lookup);
225
226 if (nhtd) {
227 nhtd->nh_num = nhr.nexthop_num;
228
229 static_nht_reset_start(&matched, afi, nhr.safi,
230 nhtd->nh_vrf_id);
231 static_nht_update(NULL, &matched, nhr.nexthop_num, afi,
232 nhr.safi, nhtd->nh_vrf_id);
233 } else
234 zlog_err("No nhtd?");
235
236 return 1;
237 }
238
239 static void static_zebra_capabilities(struct zclient_capabilities *cap)
240 {
241 mpls_enabled = cap->mpls_enabled;
242 zebra_ecmp_count = cap->ecmp;
243 }
244
245 static struct static_nht_data *
246 static_nht_hash_getref(const struct static_nht_data *ref)
247 {
248 struct static_nht_data *nhtd;
249
250 nhtd = static_nht_hash_find(static_nht_hash, ref);
251 if (!nhtd) {
252 nhtd = XCALLOC(MTYPE_STATIC_NHT_DATA, sizeof(*nhtd));
253
254 prefix_copy(&nhtd->nh, &ref->nh);
255 nhtd->nh_vrf_id = ref->nh_vrf_id;
256 nhtd->safi = ref->safi;
257
258 static_nht_hash_add(static_nht_hash, nhtd);
259 }
260
261 nhtd->refcount++;
262 return nhtd;
263 }
264
265 static bool static_nht_hash_decref(struct static_nht_data *nhtd)
266 {
267 if (--nhtd->refcount > 0)
268 return true;
269
270 static_nht_hash_del(static_nht_hash, nhtd);
271 XFREE(MTYPE_STATIC_NHT_DATA, nhtd);
272 return false;
273 }
274
275 static void static_nht_hash_clear(void)
276 {
277 struct static_nht_data *nhtd;
278
279 while ((nhtd = static_nht_hash_pop(static_nht_hash)))
280 XFREE(MTYPE_STATIC_NHT_DATA, nhtd);
281 }
282
283 static bool static_zebra_nht_get_prefix(const struct static_nexthop *nh,
284 struct prefix *p)
285 {
286 switch (nh->type) {
287 case STATIC_IFNAME:
288 case STATIC_BLACKHOLE:
289 p->family = AF_UNSPEC;
290 return false;
291
292 case STATIC_IPV4_GATEWAY:
293 case STATIC_IPV4_GATEWAY_IFNAME:
294 p->family = AF_INET;
295 p->prefixlen = IPV4_MAX_BITLEN;
296 p->u.prefix4 = nh->addr.ipv4;
297 return true;
298
299 case STATIC_IPV6_GATEWAY:
300 case STATIC_IPV6_GATEWAY_IFNAME:
301 p->family = AF_INET6;
302 p->prefixlen = IPV6_MAX_BITLEN;
303 p->u.prefix6 = nh->addr.ipv6;
304 return true;
305 }
306
307 assertf(0, "BUG: someone forgot to add nexthop type %u", nh->type);
308 }
309
310 void static_zebra_nht_register(struct static_nexthop *nh, bool reg)
311 {
312 struct static_path *pn = nh->pn;
313 struct route_node *rn = pn->rn;
314 struct static_route_info *si = static_route_info_from_rnode(rn);
315 struct static_nht_data lookup = {};
316 uint32_t cmd;
317
318 cmd = (reg) ?
319 ZEBRA_NEXTHOP_REGISTER : ZEBRA_NEXTHOP_UNREGISTER;
320
321 if (nh->nh_registered && reg)
322 return;
323
324 if (!nh->nh_registered && !reg)
325 return;
326
327 if (!static_zebra_nht_get_prefix(nh, &lookup.nh))
328 return;
329 lookup.nh_vrf_id = nh->nh_vrf_id;
330 lookup.safi = si->safi;
331
332 nh->nh_registered = reg;
333
334 if (reg) {
335 struct static_nht_data *nhtd;
336
337 nhtd = static_nht_hash_getref(&lookup);
338
339 if (nhtd->refcount > 1) {
340 DEBUGD(&static_dbg_route,
341 "Already registered nexthop(%pFX) for %pRN %d",
342 &lookup.nh, rn, nhtd->nh_num);
343 if (nhtd->nh_num) {
344 afi_t afi = prefix_afi(&lookup.nh);
345
346 static_nht_update(&rn->p, &nhtd->nh,
347 nhtd->nh_num, afi, si->safi,
348 nh->nh_vrf_id);
349 }
350 return;
351 }
352 } else {
353 struct static_nht_data *nhtd;
354
355 nhtd = static_nht_hash_find(static_nht_hash, &lookup);
356 if (!nhtd)
357 return;
358 if (static_nht_hash_decref(nhtd))
359 return;
360 }
361
362 DEBUGD(&static_dbg_route, "%s nexthop(%pFX) for %pRN",
363 reg ? "Registering" : "Unregistering", &lookup.nh, rn);
364
365 if (zclient_send_rnh(zclient, cmd, &lookup.nh, si->safi, false, false,
366 nh->nh_vrf_id) == ZCLIENT_SEND_FAILURE)
367 zlog_warn("%s: Failure to send nexthop to zebra", __func__);
368 }
369 /*
370 * When nexthop gets updated via configuration then use the
371 * already registered NH and resend the route to zebra
372 */
373 int static_zebra_nh_update(struct static_nexthop *nh)
374 {
375 struct static_path *pn = nh->pn;
376 struct route_node *rn = pn->rn;
377 struct static_route_info *si = static_route_info_from_rnode(rn);
378 struct static_nht_data *nhtd, lookup = {};
379
380 if (!nh->nh_registered)
381 return 0;
382
383 if (!static_zebra_nht_get_prefix(nh, &lookup.nh))
384 return 0;
385 lookup.nh_vrf_id = nh->nh_vrf_id;
386 lookup.safi = si->safi;
387
388 nhtd = static_nht_hash_find(static_nht_hash, &lookup);
389 if (nhtd && nhtd->nh_num) {
390 afi_t afi = prefix_afi(&lookup.nh);
391
392 nh->state = STATIC_START;
393 static_nht_update(&rn->p, &nhtd->nh, nhtd->nh_num, afi,
394 si->safi, nh->nh_vrf_id);
395 return 1;
396 }
397 return 0;
398 }
399
400 extern void static_zebra_route_add(struct static_path *pn, bool install)
401 {
402 struct route_node *rn = pn->rn;
403 struct static_route_info *si = rn->info;
404 struct static_nexthop *nh;
405 const struct prefix *p, *src_pp;
406 struct zapi_nexthop *api_nh;
407 struct zapi_route api;
408 uint32_t nh_num = 0;
409
410 p = src_pp = NULL;
411 srcdest_rnode_prefixes(rn, &p, &src_pp);
412
413 memset(&api, 0, sizeof(api));
414 api.vrf_id = si->svrf->vrf->vrf_id;
415 api.type = ZEBRA_ROUTE_STATIC;
416 api.safi = si->safi;
417 memcpy(&api.prefix, p, sizeof(api.prefix));
418
419 if (src_pp) {
420 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
421 memcpy(&api.src_prefix, src_pp, sizeof(api.src_prefix));
422 }
423 SET_FLAG(api.flags, ZEBRA_FLAG_RR_USE_DISTANCE);
424 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
425 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
426 if (pn->distance) {
427 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
428 api.distance = pn->distance;
429 }
430 if (pn->tag) {
431 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
432 api.tag = pn->tag;
433 }
434 if (pn->table_id != 0) {
435 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
436 api.tableid = pn->table_id;
437 }
438 frr_each(static_nexthop_list, &pn->nexthop_list, nh) {
439 /* Don't overrun the nexthop array */
440 if (nh_num == zebra_ecmp_count)
441 break;
442
443 api_nh = &api.nexthops[nh_num];
444 if (nh->nh_vrf_id == VRF_UNKNOWN)
445 continue;
446
447 api_nh->vrf_id = nh->nh_vrf_id;
448 if (nh->onlink)
449 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
450 if (nh->color != 0) {
451 SET_FLAG(api.message, ZAPI_MESSAGE_SRTE);
452 api_nh->srte_color = nh->color;
453 }
454
455 nh->state = STATIC_SENT_TO_ZEBRA;
456
457 switch (nh->type) {
458 case STATIC_IFNAME:
459 if (nh->ifindex == IFINDEX_INTERNAL)
460 continue;
461 api_nh->ifindex = nh->ifindex;
462 api_nh->type = NEXTHOP_TYPE_IFINDEX;
463 break;
464 case STATIC_IPV4_GATEWAY:
465 if (!nh->nh_valid)
466 continue;
467 api_nh->type = NEXTHOP_TYPE_IPV4;
468 api_nh->gate = nh->addr;
469 break;
470 case STATIC_IPV4_GATEWAY_IFNAME:
471 if (nh->ifindex == IFINDEX_INTERNAL)
472 continue;
473 api_nh->ifindex = nh->ifindex;
474 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
475 api_nh->gate = nh->addr;
476 break;
477 case STATIC_IPV6_GATEWAY:
478 if (!nh->nh_valid)
479 continue;
480 api_nh->type = NEXTHOP_TYPE_IPV6;
481 api_nh->gate = nh->addr;
482 break;
483 case STATIC_IPV6_GATEWAY_IFNAME:
484 if (nh->ifindex == IFINDEX_INTERNAL)
485 continue;
486 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
487 api_nh->ifindex = nh->ifindex;
488 api_nh->gate = nh->addr;
489 break;
490 case STATIC_BLACKHOLE:
491 api_nh->type = NEXTHOP_TYPE_BLACKHOLE;
492 switch (nh->bh_type) {
493 case STATIC_BLACKHOLE_DROP:
494 case STATIC_BLACKHOLE_NULL:
495 api_nh->bh_type = BLACKHOLE_NULL;
496 break;
497 case STATIC_BLACKHOLE_REJECT:
498 api_nh->bh_type = BLACKHOLE_REJECT;
499 }
500 break;
501 }
502
503 if (nh->snh_label.num_labels) {
504 int i;
505
506 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
507 api_nh->label_num = nh->snh_label.num_labels;
508 for (i = 0; i < api_nh->label_num; i++)
509 api_nh->labels[i] = nh->snh_label.label[i];
510 }
511 nh_num++;
512 }
513
514 api.nexthop_num = nh_num;
515
516 /*
517 * If we have been given an install but nothing is valid
518 * go ahead and delete the route for double plus fun
519 */
520 if (!nh_num && install)
521 install = false;
522
523 zclient_route_send(install ?
524 ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
525 zclient, &api);
526 }
527
528 static zclient_handler *const static_handlers[] = {
529 [ZEBRA_INTERFACE_ADDRESS_ADD] = interface_address_add,
530 [ZEBRA_INTERFACE_ADDRESS_DELETE] = interface_address_delete,
531 [ZEBRA_ROUTE_NOTIFY_OWNER] = route_notify_owner,
532 [ZEBRA_NEXTHOP_UPDATE] = static_zebra_nexthop_update,
533 };
534
535 void static_zebra_init(void)
536 {
537 struct zclient_options opt = { .receive_notify = true };
538
539 if_zapi_callbacks(static_ifp_create, static_ifp_up,
540 static_ifp_down, static_ifp_destroy);
541
542 zclient = zclient_new(master, &opt, static_handlers,
543 array_size(static_handlers));
544
545 zclient_init(zclient, ZEBRA_ROUTE_STATIC, 0, &static_privs);
546 zclient->zebra_capabilities = static_zebra_capabilities;
547 zclient->zebra_connected = zebra_connected;
548
549 static_nht_hash_init(static_nht_hash);
550 }
551
552 /* static_zebra_stop used by tests/lib/test_grpc.cpp */
553 void static_zebra_stop(void)
554 {
555 static_nht_hash_clear();
556 static_nht_hash_fini(static_nht_hash);
557
558 if (!zclient)
559 return;
560 zclient_stop(zclient);
561 zclient_free(zclient);
562 zclient = NULL;
563 }
564
565 void static_zebra_vrf_register(struct vrf *vrf)
566 {
567 if (vrf->vrf_id == VRF_DEFAULT)
568 return;
569 zclient_send_reg_requests(zclient, vrf->vrf_id);
570 }
571
572 void static_zebra_vrf_unregister(struct vrf *vrf)
573 {
574 if (vrf->vrf_id == VRF_DEFAULT)
575 return;
576 zclient_send_dereg_requests(zclient, vrf->vrf_id);
577 }