]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vrf.c
Merge pull request #2366 from msablic/pim_doc
[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/debug.h"
32 #include "zebra/zapi_msg.h"
33 #include "zebra/rib.h"
34 #include "zebra/zebra_vrf.h"
35 #include "zebra/zebra_rnh.h"
36 #include "zebra/router-id.h"
37 #include "zebra/zebra_memory.h"
38 #include "zebra/zebra_static.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
44 extern struct zebra_t zebrad;
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 /* VRF information update. */
52 static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
53 {
54 struct listnode *node, *nnode;
55 struct zserv *client;
56
57 if (IS_ZEBRA_DEBUG_EVENT)
58 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
59
60 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
61 zsend_vrf_add(client, zvrf);
62 }
63
64 static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
65 {
66 struct listnode *node, *nnode;
67 struct zserv *client;
68
69 if (IS_ZEBRA_DEBUG_EVENT)
70 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
71
72 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
73 zsend_vrf_delete(client, zvrf);
74 }
75
76 void zebra_vrf_update_all(struct zserv *client)
77 {
78 struct vrf *vrf;
79
80 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
81 if (vrf->vrf_id != VRF_UNKNOWN)
82 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
83 }
84 }
85
86 /* Callback upon creating a new VRF. */
87 static int zebra_vrf_new(struct vrf *vrf)
88 {
89 struct zebra_vrf *zvrf;
90
91 if (IS_ZEBRA_DEBUG_EVENT)
92 zlog_info("VRF %s created, id %u", vrf->name, vrf->vrf_id);
93
94 zvrf = zebra_vrf_alloc();
95 vrf->info = zvrf;
96 zvrf->vrf = vrf;
97 router_id_init(zvrf);
98 return 0;
99 }
100
101 /* Callback upon enabling a VRF. */
102 static int zebra_vrf_enable(struct vrf *vrf)
103 {
104 struct zebra_vrf *zvrf = vrf->info;
105 struct route_table *table;
106 afi_t afi;
107 safi_t safi;
108
109 assert(zvrf);
110 if (IS_ZEBRA_DEBUG_EVENT)
111 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
112 zvrf_id(zvrf));
113
114 if (vrf_is_backend_netns())
115 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
116 else
117 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
118 /* Inform clients that the VRF is now active. This is an
119 * add for the clients.
120 */
121
122 zebra_vrf_add_update(zvrf);
123 /* Allocate tables */
124 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
125 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
126 zebra_vrf_table_create(zvrf, afi, safi);
127
128 table = route_table_init();
129 table->cleanup = zebra_rnhtable_node_cleanup;
130 zvrf->rnh_table[afi] = table;
131
132 table = route_table_init();
133 table->cleanup = zebra_rnhtable_node_cleanup;
134 zvrf->import_check_table[afi] = table;
135 }
136
137 static_fixup_vrf_ids(zvrf);
138
139 /*
140 * We may have static routes that are now possible to
141 * insert into the appropriate tables
142 */
143 static_config_install_delayed_routes(zvrf);
144
145 /* Kick off any VxLAN-EVPN processing. */
146 zebra_vxlan_vrf_enable(zvrf);
147
148 return 0;
149 }
150
151 /* Callback upon disabling a VRF. */
152 static int zebra_vrf_disable(struct vrf *vrf)
153 {
154 struct zebra_vrf *zvrf = vrf->info;
155 struct route_table *table;
156 struct interface *ifp;
157 afi_t afi;
158 safi_t safi;
159 unsigned i;
160
161 assert(zvrf);
162 if (IS_ZEBRA_DEBUG_EVENT)
163 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
164 zvrf_id(zvrf));
165
166 static_cleanup_vrf_ids(zvrf);
167
168 /* Stop any VxLAN-EVPN processing. */
169 zebra_vxlan_vrf_disable(zvrf);
170
171 /* Inform clients that the VRF is now inactive. This is a
172 * delete for the clients.
173 */
174 zebra_vrf_delete_update(zvrf);
175
176 /* If asked to retain routes, there's nothing more to do. */
177 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
178 return 0;
179
180 /* Remove all routes. */
181 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
182 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
183 rib_close_table(zvrf->table[afi][safi]);
184 }
185
186 /* Cleanup Vxlan, MPLS and PW tables. */
187 zebra_vxlan_cleanup_tables(zvrf);
188 zebra_mpls_cleanup_tables(zvrf);
189 zebra_pw_exit(zvrf);
190
191 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
192 */
193 FOR_ALL_INTERFACES (vrf, ifp)
194 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
195
196 /* clean-up work queues */
197 for (i = 0; i < MQ_SIZE; i++) {
198 struct listnode *lnode, *nnode;
199 struct route_node *rnode;
200 rib_dest_t *dest;
201
202 for (ALL_LIST_ELEMENTS(zebrad.mq->subq[i], lnode, nnode,
203 rnode)) {
204 dest = rib_dest_from_rnode(rnode);
205 if (dest && rib_dest_vrf(dest) == zvrf) {
206 route_unlock_node(rnode);
207 list_delete_node(zebrad.mq->subq[i], lnode);
208 zebrad.mq->size--;
209 }
210 }
211 }
212
213 /* Cleanup (free) routing tables and NHT tables. */
214 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
215 void *table_info;
216
217 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
218 table = zvrf->table[afi][safi];
219 table_info = table->info;
220 route_table_finish(table);
221 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
222 zvrf->table[afi][safi] = NULL;
223 }
224
225 route_table_finish(zvrf->rnh_table[afi]);
226 zvrf->rnh_table[afi] = NULL;
227 route_table_finish(zvrf->import_check_table[afi]);
228 zvrf->import_check_table[afi] = NULL;
229 }
230
231 return 0;
232 }
233
234 static int zebra_vrf_delete(struct vrf *vrf)
235 {
236 struct zebra_vrf *zvrf = vrf->info;
237 struct route_table *table;
238 afi_t afi;
239 safi_t safi;
240 unsigned i;
241
242 assert(zvrf);
243 if (IS_ZEBRA_DEBUG_EVENT)
244 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
245 zvrf_id(zvrf));
246
247 /* clean-up work queues */
248 for (i = 0; i < MQ_SIZE; i++) {
249 struct listnode *lnode, *nnode;
250 struct route_node *rnode;
251 rib_dest_t *dest;
252
253 for (ALL_LIST_ELEMENTS(zebrad.mq->subq[i], lnode, nnode,
254 rnode)) {
255 dest = rib_dest_from_rnode(rnode);
256 if (dest && rib_dest_vrf(dest) == zvrf) {
257 route_unlock_node(rnode);
258 list_delete_node(zebrad.mq->subq[i], lnode);
259 zebrad.mq->size--;
260 }
261 }
262 }
263
264 /* Free Vxlan and MPLS. */
265 zebra_vxlan_close_tables(zvrf);
266 zebra_mpls_close_tables(zvrf);
267
268 /* release allocated memory */
269 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
270 void *table_info;
271
272 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
273 table = zvrf->table[afi][safi];
274 if (table) {
275 table_info = table->info;
276 route_table_finish(table);
277 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
278 }
279
280 table = zvrf->stable[afi][safi];
281 route_table_finish(table);
282 }
283
284 route_table_finish(zvrf->rnh_table[afi]);
285 route_table_finish(zvrf->import_check_table[afi]);
286 }
287
288 /* Cleanup EVPN states for vrf */
289 zebra_vxlan_vrf_delete(zvrf);
290
291 list_delete_all_node(zvrf->rid_all_sorted_list);
292 list_delete_all_node(zvrf->rid_lo_sorted_list);
293 XFREE(MTYPE_ZEBRA_VRF, zvrf);
294 vrf->info = NULL;
295
296 return 0;
297 }
298
299 /* Return if this VRF has any FRR configuration or not.
300 * IMPORTANT: This function needs to be updated when additional configuration
301 * is added for a VRF.
302 */
303 int zebra_vrf_has_config(struct zebra_vrf *zvrf)
304 {
305 afi_t afi;
306 safi_t safi;
307 struct route_table *stable;
308
309 /* NOTE: This is a don't care for the default VRF, but we go through
310 * the motions to keep things consistent.
311 */
312 /* Any static routes? */
313 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
314 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
315 stable = zvrf->stable[afi][safi];
316 if (!stable)
317 continue;
318 if (route_table_count(stable))
319 return 1;
320 }
321 }
322
323 /* EVPN L3-VNI? */
324 if (zvrf->l3vni)
325 return 1;
326
327 return 0;
328 }
329
330 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
331 * NOTE: Table-id is relevant on two modes:
332 * - case VRF backend is default : on default VRF only
333 * - case VRF backend is netns : on all VRFs
334 */
335 struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
336 vrf_id_t vrf_id,
337 uint32_t table_id)
338 {
339 struct route_table *table = NULL;
340
341 if (afi >= AFI_MAX || safi >= SAFI_MAX)
342 return NULL;
343
344 if (vrf_id == VRF_DEFAULT) {
345 if (table_id == RT_TABLE_MAIN
346 || table_id == zebrad.rtm_table_default)
347 table = zebra_vrf_table(afi, safi, vrf_id);
348 else
349 table = zebra_vrf_other_route_table(afi, table_id,
350 vrf_id);
351 } else if (vrf_is_backend_netns()) {
352 if (table_id == RT_TABLE_MAIN
353 || table_id == zebrad.rtm_table_default)
354 table = zebra_vrf_table(afi, safi, vrf_id);
355 else
356 table = zebra_vrf_other_route_table(afi, table_id,
357 vrf_id);
358 } else
359 table = zebra_vrf_table(afi, safi, vrf_id);
360
361 return table;
362 }
363
364 void zebra_rtable_node_cleanup(struct route_table *table,
365 struct route_node *node)
366 {
367 struct route_entry *re, *next;
368
369 RNODE_FOREACH_RE_SAFE (node, re, next) {
370 rib_unlink(node, re);
371 }
372
373 if (node->info)
374 XFREE(MTYPE_RIB_DEST, node->info);
375 }
376
377 static void zebra_stable_node_cleanup(struct route_table *table,
378 struct route_node *node)
379 {
380 struct static_route *si, *next;
381
382 if (node->info)
383 for (si = node->info; si; si = next) {
384 next = si->next;
385 XFREE(MTYPE_STATIC_ROUTE, si);
386 }
387 }
388
389 static void zebra_rnhtable_node_cleanup(struct route_table *table,
390 struct route_node *node)
391 {
392 if (node->info)
393 zebra_free_rnh(node->info);
394 }
395
396 /*
397 * Create a routing table for the specific AFI/SAFI in the given VRF.
398 */
399 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
400 safi_t safi)
401 {
402 rib_table_info_t *info;
403 struct route_table *table;
404
405 assert(!zvrf->table[afi][safi]);
406
407 if (afi == AFI_IP6)
408 table = srcdest_table_init();
409 else
410 table = route_table_init();
411 table->cleanup = zebra_rtable_node_cleanup;
412 zvrf->table[afi][safi] = table;
413
414 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
415 info->zvrf = zvrf;
416 info->afi = afi;
417 info->safi = safi;
418 table->info = info;
419 }
420
421 /* Allocate new zebra VRF. */
422 struct zebra_vrf *zebra_vrf_alloc(void)
423 {
424 struct zebra_vrf *zvrf;
425 afi_t afi;
426 safi_t safi;
427 struct route_table *table;
428
429 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
430
431 /* Allocate table for static route configuration. */
432 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
433 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
434 if (afi == AFI_IP6)
435 table = srcdest_table_init();
436 else
437 table = route_table_init();
438 table->cleanup = zebra_stable_node_cleanup;
439 zvrf->stable[afi][safi] = table;
440 }
441 }
442
443 zebra_vxlan_init_tables(zvrf);
444 zebra_mpls_init_tables(zvrf);
445 zebra_pw_init(zvrf);
446 zvrf->table_id = RT_TABLE_MAIN;
447 /* by default table ID is default one */
448 return zvrf;
449 }
450
451 /* Lookup VRF by identifier. */
452 struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
453 {
454 return vrf_info_lookup(vrf_id);
455 }
456
457 /* Lookup VRF by name. */
458 struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
459 {
460 struct vrf *vrf;
461
462 if (!name)
463 name = VRF_DEFAULT_NAME;
464
465 vrf = vrf_lookup_by_name(name);
466 if (vrf)
467 return ((struct zebra_vrf *)vrf->info);
468
469 return NULL;
470 }
471
472 /* Lookup the routing table in an enabled VRF. */
473 struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
474 {
475 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
476
477 if (!zvrf)
478 return NULL;
479
480 if (afi >= AFI_MAX || safi >= SAFI_MAX)
481 return NULL;
482
483 return zvrf->table[afi][safi];
484 }
485
486 /* Lookup the static routing table in a VRF. */
487 struct route_table *zebra_vrf_static_table(afi_t afi, safi_t safi,
488 struct zebra_vrf *zvrf)
489 {
490 if (!zvrf)
491 return NULL;
492
493 if (afi >= AFI_MAX || safi >= SAFI_MAX)
494 return NULL;
495
496 return zvrf->stable[afi][safi];
497 }
498
499 struct route_table *zebra_vrf_other_route_table(afi_t afi, uint32_t table_id,
500 vrf_id_t vrf_id)
501 {
502 struct zebra_vrf *zvrf;
503 struct zebra_ns *zns;
504
505 zvrf = vrf_info_lookup(vrf_id);
506 if (!zvrf)
507 return NULL;
508
509 zns = zvrf->zns;
510
511 if (afi >= AFI_MAX)
512 return NULL;
513
514 if ((table_id != RT_TABLE_MAIN)
515 && (table_id != zebrad.rtm_table_default)) {
516 if (zvrf->table_id == RT_TABLE_MAIN ||
517 zvrf->table_id == zebrad.rtm_table_default) {
518 /* this VRF use default table
519 * so in all cases, it does not use specific table
520 * so it is possible to configure tables in this VRF
521 */
522 return zebra_ns_get_table(zns, zvrf, table_id, afi);
523 }
524 }
525
526 return zvrf->table[afi][SAFI_UNICAST];
527 }
528
529 static int vrf_config_write(struct vty *vty)
530 {
531 struct vrf *vrf;
532 struct zebra_vrf *zvrf;
533
534 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
535 zvrf = vrf->info;
536
537 if (!zvrf)
538 continue;
539
540 if (zvrf_id(zvrf) == VRF_DEFAULT) {
541 if (zvrf->l3vni)
542 vty_out(vty, "vni %u\n", zvrf->l3vni);
543 vty_out(vty, "!\n");
544 } else {
545 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
546 if (zvrf->l3vni)
547 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
548 is_l3vni_for_prefix_routes_only(
549 zvrf->l3vni)
550 ? " prefix-routes-only"
551 : "");
552 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
553
554 }
555
556 static_config(vty, zvrf, AFI_IP, SAFI_UNICAST, "ip route");
557 static_config(vty, zvrf, AFI_IP, SAFI_MULTICAST, "ip mroute");
558 static_config(vty, zvrf, AFI_IP6, SAFI_UNICAST, "ipv6 route");
559
560 if (zvrf_id(zvrf) != VRF_DEFAULT)
561 vty_endframe(vty, " exit-vrf\n!\n");
562 }
563 return 0;
564 }
565
566 /* Zebra VRF initialization. */
567 void zebra_vrf_init(void)
568 {
569 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
570 zebra_vrf_delete);
571
572 vrf_cmd_init(vrf_config_write, &zserv_privs);
573 }