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