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