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