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