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