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