]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vrf.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / zebra / zebra_vrf.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 CumulusNetworks
4 * Donald Sharp
5 *
6 * This file is part of Quagga
7 */
8 #include <zebra.h>
9
10 /* for basename */
11 #include <libgen.h>
12
13 #include "log.h"
14 #include "linklist.h"
15 #include "command.h"
16 #include "memory.h"
17 #include "srcdest_table.h"
18 #include "vrf.h"
19 #include "vty.h"
20
21 #include "zebra/zebra_router.h"
22 #include "zebra/rtadv.h"
23 #include "zebra/debug.h"
24 #include "zebra/zapi_msg.h"
25 #include "zebra/rib.h"
26 #include "zebra/zebra_vrf.h"
27 #include "zebra/zebra_rnh.h"
28 #include "zebra/router-id.h"
29 #include "zebra/interface.h"
30 #include "zebra/zebra_mpls.h"
31 #include "zebra/zebra_vxlan.h"
32 #include "zebra/zebra_netns_notify.h"
33 #include "zebra/zebra_routemap.h"
34 #include "zebra/zebra_vrf_clippy.c"
35 #include "zebra/table_manager.h"
36
37 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
38 safi_t safi);
39 static void zebra_rnhtable_node_cleanup(struct route_table *table,
40 struct route_node *node);
41
42 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_VRF, "ZEBRA VRF");
43 DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table");
44
45 /* VRF information update. */
46 static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
47 {
48 struct listnode *node, *nnode;
49 struct zserv *client;
50
51 if (IS_ZEBRA_DEBUG_EVENT)
52 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
53
54 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
55 /* Do not send unsolicited messages to synchronous clients. */
56 if (client->synchronous)
57 continue;
58
59 zsend_vrf_add(client, zvrf);
60 }
61 }
62
63 static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
64 {
65 struct listnode *node, *nnode;
66 struct zserv *client;
67
68 if (IS_ZEBRA_DEBUG_EVENT)
69 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
70
71 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
72 /* Do not send unsolicited messages to synchronous clients. */
73 if (client->synchronous)
74 continue;
75
76 zsend_vrf_delete(client, zvrf);
77 }
78 }
79
80 void zebra_vrf_update_all(struct zserv *client)
81 {
82 struct vrf *vrf;
83
84 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
85 if (vrf->vrf_id != VRF_UNKNOWN)
86 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
87 }
88 }
89
90 /* Callback upon creating a new VRF. */
91 static int zebra_vrf_new(struct vrf *vrf)
92 {
93 struct zebra_vrf *zvrf;
94
95 if (IS_ZEBRA_DEBUG_EVENT)
96 zlog_debug("VRF %s created, id %u", vrf->name, vrf->vrf_id);
97
98 zvrf = zebra_vrf_alloc(vrf);
99 if (!vrf_is_backend_netns())
100 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
101
102 otable_init(&zvrf->other_tables);
103
104 router_id_init(zvrf);
105
106 /* Initiate Table Manager per ZNS */
107 table_manager_enable(zvrf);
108
109 return 0;
110 }
111
112 /* Callback upon enabling a VRF. */
113 static int zebra_vrf_enable(struct vrf *vrf)
114 {
115 struct zebra_vrf *zvrf = vrf->info;
116 struct route_table *table;
117 afi_t afi;
118 safi_t safi;
119
120 assert(zvrf);
121 if (IS_ZEBRA_DEBUG_EVENT)
122 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
123 zvrf_id(zvrf));
124
125 if (vrf_is_backend_netns())
126 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
127 else
128 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
129
130 rtadv_vrf_init(zvrf);
131
132 /* Inform clients that the VRF is now active. This is an
133 * add for the clients.
134 */
135
136 zebra_vrf_add_update(zvrf);
137 /* Allocate tables */
138 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
139 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
140 zebra_vrf_table_create(zvrf, afi, safi);
141
142 table = route_table_init();
143 table->cleanup = zebra_rnhtable_node_cleanup;
144 zvrf->rnh_table[afi] = table;
145
146 table = route_table_init();
147 table->cleanup = zebra_rnhtable_node_cleanup;
148 zvrf->rnh_table_multicast[afi] = table;
149 }
150
151 /* Kick off any VxLAN-EVPN processing. */
152 zebra_vxlan_vrf_enable(zvrf);
153
154 return 0;
155 }
156
157 /* Callback upon disabling a VRF. */
158 static int zebra_vrf_disable(struct vrf *vrf)
159 {
160 struct zebra_vrf *zvrf = vrf->info;
161 struct interface *ifp;
162 afi_t afi;
163 safi_t safi;
164
165 assert(zvrf);
166 if (IS_ZEBRA_DEBUG_EVENT)
167 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
168 zvrf_id(zvrf));
169
170 /* Stop any VxLAN-EVPN processing. */
171 zebra_vxlan_vrf_disable(zvrf);
172
173 rtadv_vrf_terminate(zvrf);
174
175 /* Inform clients that the VRF is now inactive. This is a
176 * delete for the clients.
177 */
178 zebra_vrf_delete_update(zvrf);
179
180 /* If asked to retain routes, there's nothing more to do. */
181 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
182 return 0;
183
184 /* Remove all routes. */
185 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
186 route_table_finish(zvrf->rnh_table[afi]);
187 zvrf->rnh_table[afi] = NULL;
188 route_table_finish(zvrf->rnh_table_multicast[afi]);
189 zvrf->rnh_table_multicast[afi] = NULL;
190
191 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
192 rib_close_table(zvrf->table[afi][safi]);
193 }
194
195 /* Cleanup Vxlan, MPLS and PW tables. */
196 zebra_vxlan_cleanup_tables(zvrf);
197 zebra_mpls_cleanup_tables(zvrf);
198 zebra_pw_exit(zvrf);
199
200 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
201 */
202 FOR_ALL_INTERFACES (vrf, ifp)
203 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
204
205 /* clean-up work queues */
206 meta_queue_free(zrouter.mq, zvrf);
207
208 /* Cleanup (free) routing tables and NHT tables. */
209 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
210 /*
211 * Set the table pointer to NULL as that
212 * we no-longer need a copy of it, nor do we
213 * own this data, the zebra_router structure
214 * owns these tables. Once we've cleaned up the
215 * table, see rib_close_table above
216 * we no-longer need this pointer.
217 */
218 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
219 zebra_router_release_table(zvrf, zvrf->table_id, afi,
220 safi);
221 zvrf->table[afi][safi] = NULL;
222 }
223 }
224
225 return 0;
226 }
227
228 static int zebra_vrf_delete(struct vrf *vrf)
229 {
230 struct zebra_vrf *zvrf = vrf->info;
231 struct other_route_table *otable;
232
233 assert(zvrf);
234 if (IS_ZEBRA_DEBUG_EVENT)
235 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
236 zvrf_id(zvrf));
237
238 table_manager_disable(zvrf);
239
240 /* clean-up work queues */
241 meta_queue_free(zrouter.mq, zvrf);
242
243 /* Free Vxlan and MPLS. */
244 zebra_vxlan_close_tables(zvrf);
245 zebra_mpls_close_tables(zvrf);
246
247 otable = otable_pop(&zvrf->other_tables);
248 while (otable) {
249 zebra_router_release_table(zvrf, otable->table_id,
250 otable->afi, otable->safi);
251 XFREE(MTYPE_OTHER_TABLE, otable);
252
253 otable = otable_pop(&zvrf->other_tables);
254 }
255
256 /* Cleanup EVPN states for vrf */
257 zebra_vxlan_vrf_delete(zvrf);
258 zebra_routemap_vrf_delete(zvrf);
259
260 list_delete_all_node(zvrf->rid_all_sorted_list);
261 list_delete_all_node(zvrf->rid_lo_sorted_list);
262
263 list_delete_all_node(zvrf->rid6_all_sorted_list);
264 list_delete_all_node(zvrf->rid6_lo_sorted_list);
265
266 otable_fini(&zvrf->other_tables);
267 XFREE(MTYPE_ZEBRA_VRF, zvrf);
268 vrf->info = NULL;
269
270 return 0;
271 }
272
273 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
274 * NOTE: Table-id is relevant on two modes:
275 * - case VRF backend is default : on default VRF only
276 * - case VRF backend is netns : on all VRFs
277 */
278 struct route_table *zebra_vrf_lookup_table_with_table_id(afi_t afi, safi_t safi,
279 vrf_id_t vrf_id,
280 uint32_t table_id)
281 {
282 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
283 struct other_route_table ort, *otable;
284
285 if (!zvrf)
286 return NULL;
287
288 if (afi >= AFI_MAX || safi >= SAFI_MAX)
289 return NULL;
290
291 if (table_id == zvrf->table_id)
292 return zebra_vrf_table(afi, safi, vrf_id);
293
294 ort.afi = afi;
295 ort.safi = safi;
296 ort.table_id = table_id;
297 otable = otable_find(&zvrf->other_tables, &ort);
298
299 if (otable)
300 return otable->table;
301
302 return NULL;
303 }
304
305 struct route_table *zebra_vrf_get_table_with_table_id(afi_t afi, safi_t safi,
306 vrf_id_t vrf_id,
307 uint32_t table_id)
308 {
309 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
310 struct other_route_table *otable;
311 struct route_table *table;
312
313 table = zebra_vrf_lookup_table_with_table_id(afi, safi, vrf_id,
314 table_id);
315
316 if (table)
317 goto done;
318
319 /* Create it as an `other` table */
320 table = zebra_router_get_table(zvrf, table_id, afi, safi);
321
322 otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
323 otable->afi = afi;
324 otable->safi = safi;
325 otable->table_id = table_id;
326 otable->table = table;
327 otable_add(&zvrf->other_tables, otable);
328
329 done:
330 return table;
331 }
332
333 static void zebra_rnhtable_node_cleanup(struct route_table *table,
334 struct route_node *node)
335 {
336 if (node->info)
337 zebra_free_rnh(node->info);
338 }
339
340 /*
341 * Create a routing table for the specific AFI/SAFI in the given VRF.
342 */
343 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
344 safi_t safi)
345 {
346 struct route_node *rn;
347 struct prefix p;
348
349 assert(!zvrf->table[afi][safi]);
350
351 zvrf->table[afi][safi] =
352 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
353
354 memset(&p, 0, sizeof(p));
355 p.family = afi2family(afi);
356
357 rn = srcdest_rnode_get(zvrf->table[afi][safi], &p, NULL);
358 zebra_rib_create_dest(rn);
359 }
360
361 /* Allocate new zebra VRF. */
362 struct zebra_vrf *zebra_vrf_alloc(struct vrf *vrf)
363 {
364 struct zebra_vrf *zvrf;
365
366 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
367
368 zvrf->vrf = vrf;
369 vrf->info = zvrf;
370
371 zebra_vxlan_init_tables(zvrf);
372 zebra_mpls_init_tables(zvrf);
373 zebra_pw_init(zvrf);
374 zvrf->table_id = RT_TABLE_MAIN;
375 /* by default table ID is default one */
376 return zvrf;
377 }
378
379 /* Lookup VRF by identifier. */
380 struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
381 {
382 return vrf_info_lookup(vrf_id);
383 }
384
385 /* Lookup VRF by name. */
386 struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
387 {
388 struct vrf *vrf;
389
390 if (!name)
391 name = VRF_DEFAULT_NAME;
392
393 vrf = vrf_lookup_by_name(name);
394 if (vrf)
395 return ((struct zebra_vrf *)vrf->info);
396
397 return NULL;
398 }
399
400 /* Lookup the routing table in an enabled VRF. */
401 struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
402 {
403 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
404
405 if (!zvrf)
406 return NULL;
407
408 if (afi >= AFI_MAX || safi >= SAFI_MAX)
409 return NULL;
410
411 return zvrf->table[afi][safi];
412 }
413
414 static int vrf_config_write(struct vty *vty)
415 {
416 struct vrf *vrf;
417 struct zebra_vrf *zvrf;
418
419 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
420 zvrf = vrf->info;
421
422 if (!zvrf)
423 continue;
424
425 if (zvrf_id(zvrf) == VRF_DEFAULT) {
426 if (zvrf->l3vni)
427 vty_out(vty, "vni %u%s\n", zvrf->l3vni,
428 is_l3vni_for_prefix_routes_only(
429 zvrf->l3vni)
430 ? " prefix-routes-only"
431 : "");
432 if (zvrf->zebra_rnh_ip_default_route)
433 vty_out(vty, "ip nht resolve-via-default\n");
434
435 if (zvrf->zebra_rnh_ipv6_default_route)
436 vty_out(vty, "ipv6 nht resolve-via-default\n");
437
438 if (zvrf->tbl_mgr
439 && (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end))
440 vty_out(vty, "ip table range %u %u\n",
441 zvrf->tbl_mgr->start,
442 zvrf->tbl_mgr->end);
443 } else {
444 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
445 if (zvrf->l3vni)
446 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
447 is_l3vni_for_prefix_routes_only(
448 zvrf->l3vni)
449 ? " prefix-routes-only"
450 : "");
451 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
452 if (zvrf->zebra_rnh_ip_default_route)
453 vty_out(vty, " ip nht resolve-via-default\n");
454
455 if (zvrf->zebra_rnh_ipv6_default_route)
456 vty_out(vty, " ipv6 nht resolve-via-default\n");
457
458 if (zvrf->tbl_mgr && vrf_is_backend_netns()
459 && (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end))
460 vty_out(vty, " ip table range %u %u\n",
461 zvrf->tbl_mgr->start,
462 zvrf->tbl_mgr->end);
463 }
464
465
466 zebra_routemap_config_write_protocol(vty, zvrf);
467 router_id_write(vty, zvrf);
468
469 if (zvrf_id(zvrf) != VRF_DEFAULT)
470 vty_endframe(vty, "exit-vrf\n!\n");
471 else
472 vty_out(vty, "!\n");
473 }
474 return 0;
475 }
476
477 DEFPY (vrf_netns,
478 vrf_netns_cmd,
479 "netns NAME$netns_name",
480 "Attach VRF to a Namespace\n"
481 "The file name in " NS_RUN_DIR ", or a full pathname\n")
482 {
483 char *pathname = ns_netns_pathname(vty, netns_name);
484 int ret;
485
486 VTY_DECLVAR_CONTEXT(vrf, vrf);
487
488 if (!pathname)
489 return CMD_WARNING_CONFIG_FAILED;
490
491 frr_with_privs(&zserv_privs) {
492 ret = zebra_vrf_netns_handler_create(
493 vty, vrf, pathname, NS_UNKNOWN, NS_UNKNOWN, NS_UNKNOWN);
494 }
495
496 return ret;
497 }
498
499 DEFUN (no_vrf_netns,
500 no_vrf_netns_cmd,
501 "no netns [NAME]",
502 NO_STR
503 "Detach VRF from a Namespace\n"
504 "The file name in " NS_RUN_DIR ", or a full pathname\n")
505 {
506 struct ns *ns = NULL;
507
508 VTY_DECLVAR_CONTEXT(vrf, vrf);
509
510 if (!vrf_is_backend_netns()) {
511 vty_out(vty, "VRF backend is not Netns. Aborting\n");
512 return CMD_WARNING_CONFIG_FAILED;
513 }
514 if (!vrf->ns_ctxt) {
515 vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
516 vrf->name, vrf->vrf_id);
517 return CMD_WARNING_CONFIG_FAILED;
518 }
519
520 ns = (struct ns *)vrf->ns_ctxt;
521
522 ns->vrf_ctxt = NULL;
523 vrf_disable(vrf);
524 /* vrf ID from VRF is necessary for Zebra
525 * so that propagate to other clients is done
526 */
527 ns_delete(ns);
528 vrf->ns_ctxt = NULL;
529 return CMD_SUCCESS;
530 }
531
532 /* if ns_id is different and not VRF_UNKNOWN,
533 * then update vrf identifier, and enable VRF
534 */
535 static void vrf_update_vrf_id(ns_id_t ns_id, void *opaqueptr)
536 {
537 ns_id_t vrf_id = (vrf_id_t)ns_id;
538 vrf_id_t old_vrf_id;
539 struct vrf *vrf = (struct vrf *)opaqueptr;
540
541 if (!vrf)
542 return;
543 old_vrf_id = vrf->vrf_id;
544 if (vrf_id == vrf->vrf_id)
545 return;
546 if (vrf->vrf_id != VRF_UNKNOWN)
547 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
548 vrf->vrf_id = vrf_id;
549 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
550 if (old_vrf_id == VRF_UNKNOWN)
551 vrf_enable(vrf);
552 }
553
554 int zebra_vrf_netns_handler_create(struct vty *vty, struct vrf *vrf,
555 char *pathname, ns_id_t ns_id,
556 ns_id_t internal_ns_id,
557 ns_id_t rel_def_ns_id)
558 {
559 struct ns *ns = NULL;
560
561 if (!vrf)
562 return CMD_WARNING_CONFIG_FAILED;
563 if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) {
564 if (vty)
565 vty_out(vty,
566 "VRF %u is already configured with VRF %s\n",
567 vrf->vrf_id, vrf->name);
568 else
569 zlog_info("VRF %u is already configured with VRF %s",
570 vrf->vrf_id, vrf->name);
571 return CMD_WARNING_CONFIG_FAILED;
572 }
573 if (vrf->ns_ctxt != NULL) {
574 ns = (struct ns *)vrf->ns_ctxt;
575 if (!strcmp(ns->name, pathname)) {
576 if (vty)
577 vty_out(vty,
578 "VRF %u already configured with NETNS %s\n",
579 vrf->vrf_id, ns->name);
580 else
581 zlog_info(
582 "VRF %u already configured with NETNS %s",
583 vrf->vrf_id, ns->name);
584 return CMD_WARNING;
585 }
586 }
587 ns = ns_lookup_name(pathname);
588 if (ns && ns->vrf_ctxt) {
589 struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt;
590
591 if (vrf2 == vrf)
592 return CMD_SUCCESS;
593 if (vty)
594 vty_out(vty,
595 "NS %s is already configured with VRF %u(%s)\n",
596 ns->name, vrf2->vrf_id, vrf2->name);
597 else
598 zlog_info("NS %s is already configured with VRF %u(%s)",
599 ns->name, vrf2->vrf_id, vrf2->name);
600 return CMD_WARNING_CONFIG_FAILED;
601 }
602 ns = ns_get_created(ns, pathname, ns_id);
603 ns->internal_ns_id = internal_ns_id;
604 ns->relative_default_ns = rel_def_ns_id;
605 ns->vrf_ctxt = (void *)vrf;
606 vrf->ns_ctxt = (void *)ns;
607 /* update VRF netns NAME */
608 strlcpy(vrf->data.l.netns_name, basename(pathname), NS_NAMSIZ);
609
610 if (!ns_enable(ns, vrf_update_vrf_id)) {
611 if (vty)
612 vty_out(vty, "Can not associate NS %u with NETNS %s\n",
613 ns->ns_id, ns->name);
614 else
615 zlog_info("Can not associate NS %u with NETNS %s",
616 ns->ns_id, ns->name);
617 return CMD_WARNING_CONFIG_FAILED;
618 }
619
620 return CMD_SUCCESS;
621 }
622
623 /* Zebra VRF initialization. */
624 void zebra_vrf_init(void)
625 {
626 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
627 zebra_vrf_delete);
628
629 hook_register(zserv_client_close, release_daemon_table_chunks);
630
631 vrf_cmd_init(vrf_config_write);
632
633 if (vrf_is_backend_netns() && ns_have_netns()) {
634 /* Install NS commands. */
635 install_element(VRF_NODE, &vrf_netns_cmd);
636 install_element(VRF_NODE, &no_vrf_netns_cmd);
637 }
638 }