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