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