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