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