]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
zebra: Abstract the rib_dest_t creation
[mirror_frr.git] / zebra / zebra_vrf.c
CommitLineData
7c551956
DS
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 *
896014f4
DL
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
7c551956
DS
20 */
21#include <zebra.h>
22
23#include "log.h"
24#include "linklist.h"
f30c50b9 25#include "command.h"
4a1ab8e4 26#include "memory.h"
05737783 27#include "srcdest_table.h"
78dd30b2 28#include "vrf.h"
82f97584 29#include "vty.h"
78dd30b2 30
89272910 31#include "zebra/zebra_router.h"
7c551956 32#include "zebra/debug.h"
bf094f69 33#include "zebra/zapi_msg.h"
7c551956
DS
34#include "zebra/rib.h"
35#include "zebra/zebra_vrf.h"
5a8dfcd8 36#include "zebra/zebra_rnh.h"
7c551956 37#include "zebra/router-id.h"
4a1ab8e4 38#include "zebra/zebra_memory.h"
5a8dfcd8 39#include "zebra/interface.h"
7758e3f3 40#include "zebra/zebra_mpls.h"
13d60d35 41#include "zebra/zebra_vxlan.h"
3bc34908 42#include "zebra/zebra_netns_notify.h"
7cf16e19 43#include "zebra/zebra_routemap.h"
7c551956 44
9d97533e 45static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
46 safi_t safi);
47static void zebra_rnhtable_node_cleanup(struct route_table *table,
48 struct route_node *node);
49
7c551956 50/* VRF information update. */
d62a17ae 51static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
7c551956 52{
d62a17ae 53 struct listnode *node, *nnode;
54 struct zserv *client;
7c551956 55
d62a17ae 56 if (IS_ZEBRA_DEBUG_EVENT)
57 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
7c551956 58
161e9ab7 59 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
d62a17ae 60 zsend_vrf_add(client, zvrf);
7c551956
DS
61}
62
d62a17ae 63static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
7c551956 64{
d62a17ae 65 struct listnode *node, *nnode;
66 struct zserv *client;
7c551956 67
d62a17ae 68 if (IS_ZEBRA_DEBUG_EVENT)
69 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
7c551956 70
161e9ab7 71 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
d62a17ae 72 zsend_vrf_delete(client, zvrf);
7c551956
DS
73}
74
d62a17ae 75void zebra_vrf_update_all(struct zserv *client)
7c551956 76{
d62a17ae 77 struct vrf *vrf;
7c551956 78
a2addae8 79 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4691b65a 80 if (vrf->vrf_id != VRF_UNKNOWN)
d62a17ae 81 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
82 }
7c551956
DS
83}
84
85/* Callback upon creating a new VRF. */
d62a17ae 86static int zebra_vrf_new(struct vrf *vrf)
7c551956 87{
d62a17ae 88 struct zebra_vrf *zvrf;
7c551956 89
d62a17ae 90 if (IS_ZEBRA_DEBUG_EVENT)
84915b0a 91 zlog_info("VRF %s created, id %u", vrf->name, vrf->vrf_id);
7c551956 92
d62a17ae 93 zvrf = zebra_vrf_alloc();
d62a17ae 94 vrf->info = zvrf;
95 zvrf->vrf = vrf;
fbb65ff5 96 router_id_init(zvrf);
d62a17ae 97 return 0;
7c551956
DS
98}
99
100/* Callback upon enabling a VRF. */
d62a17ae 101static int zebra_vrf_enable(struct vrf *vrf)
7c551956 102{
d62a17ae 103 struct zebra_vrf *zvrf = vrf->info;
9d97533e 104 struct route_table *table;
d62a17ae 105 afi_t afi;
106 safi_t safi;
107
108 assert(zvrf);
84915b0a 109 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
110 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
111 zvrf_id(zvrf));
d62a17ae 112
fbb65ff5
PG
113 if (vrf_is_backend_netns())
114 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
115 else
116 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
84915b0a 117 /* Inform clients that the VRF is now active. This is an
118 * add for the clients.
119 */
d62a17ae 120
8288a24f 121 zebra_vrf_add_update(zvrf);
9d97533e 122 /* Allocate tables */
123 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
124 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
125 zebra_vrf_table_create(zvrf, afi, safi);
126
127 table = route_table_init();
128 table->cleanup = zebra_rnhtable_node_cleanup;
129 zvrf->rnh_table[afi] = table;
130
131 table = route_table_init();
132 table->cleanup = zebra_rnhtable_node_cleanup;
133 zvrf->import_check_table[afi] = table;
134 }
135
84915b0a 136 /* Kick off any VxLAN-EVPN processing. */
137 zebra_vxlan_vrf_enable(zvrf);
138
d62a17ae 139 return 0;
7c551956
DS
140}
141
142/* Callback upon disabling a VRF. */
d62a17ae 143static int zebra_vrf_disable(struct vrf *vrf)
7c551956 144{
d62a17ae 145 struct zebra_vrf *zvrf = vrf->info;
9d97533e 146 struct interface *ifp;
d62a17ae 147 afi_t afi;
148 safi_t safi;
9d97533e 149 unsigned i;
d62a17ae 150
84915b0a 151 assert(zvrf);
152 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
153 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
154 zvrf_id(zvrf));
d62a17ae 155
84915b0a 156 /* Stop any VxLAN-EVPN processing. */
157 zebra_vxlan_vrf_disable(zvrf);
d62a17ae 158
84915b0a 159 /* Inform clients that the VRF is now inactive. This is a
160 * delete for the clients.
161 */
d62a17ae 162 zebra_vrf_delete_update(zvrf);
163
9d97533e 164 /* If asked to retain routes, there's nothing more to do. */
165 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
166 return 0;
167
168 /* Remove all routes. */
169 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
41dc8c14
DS
170 route_table_finish(zvrf->rnh_table[afi]);
171 zvrf->rnh_table[afi] = NULL;
172 route_table_finish(zvrf->import_check_table[afi]);
173 zvrf->import_check_table[afi] = NULL;
174
9d97533e 175 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
176 rib_close_table(zvrf->table[afi][safi]);
9d97533e 177 }
178
179 /* Cleanup Vxlan, MPLS and PW tables. */
180 zebra_vxlan_cleanup_tables(zvrf);
181 zebra_mpls_cleanup_tables(zvrf);
182 zebra_pw_exit(zvrf);
183
996c9314
LB
184 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
185 */
9d97533e 186 FOR_ALL_INTERFACES (vrf, ifp)
187 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
188
189 /* clean-up work queues */
190 for (i = 0; i < MQ_SIZE; i++) {
191 struct listnode *lnode, *nnode;
192 struct route_node *rnode;
193 rib_dest_t *dest;
194
ea45a4e7 195 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
996c9314 196 rnode)) {
9d97533e 197 dest = rib_dest_from_rnode(rnode);
198 if (dest && rib_dest_vrf(dest) == zvrf) {
199 route_unlock_node(rnode);
ea45a4e7
DS
200 list_delete_node(zrouter.mq->subq[i], lnode);
201 zrouter.mq->size--;
9d97533e 202 }
d62a17ae 203 }
9d97533e 204 }
7c551956 205
9d97533e 206 /* Cleanup (free) routing tables and NHT tables. */
207 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
89272910
DS
208 /*
209 * Set the table pointer to NULL as that
210 * we no-longer need a copy of it, nor do we
211 * own this data, the zebra_router structure
212 * owns these tables. Once we've cleaned up the
213 * table, see rib_close_table above
214 * we no-longer need this pointer.
215 */
bd4fb615
DS
216 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
217 zebra_router_release_table(zvrf, zvrf->table_id, afi,
218 safi);
9d97533e 219 zvrf->table[afi][safi] = NULL;
bd4fb615 220 }
5a8dfcd8
RW
221 }
222
d62a17ae 223 return 0;
7c551956
DS
224}
225
d62a17ae 226static int zebra_vrf_delete(struct vrf *vrf)
7c551956 227{
d62a17ae 228 struct zebra_vrf *zvrf = vrf->info;
229 struct route_table *table;
d62a17ae 230 afi_t afi;
231 safi_t safi;
232 unsigned i;
233
234 assert(zvrf);
84915b0a 235 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
236 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
237 zvrf_id(zvrf));
5a8dfcd8 238
d62a17ae 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
ea45a4e7 245 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
996c9314 246 rnode)) {
d62a17ae 247 dest = rib_dest_from_rnode(rnode);
248 if (dest && rib_dest_vrf(dest) == zvrf) {
249 route_unlock_node(rnode);
ea45a4e7
DS
250 list_delete_node(zrouter.mq->subq[i], lnode);
251 zrouter.mq->size--;
d62a17ae 252 }
253 }
5a8dfcd8 254 }
5a8dfcd8 255
84915b0a 256 /* Free Vxlan and MPLS. */
257 zebra_vxlan_close_tables(zvrf);
258 zebra_mpls_close_tables(zvrf);
259
d62a17ae 260 /* release allocated memory */
261 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
d62a17ae 262 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
263 table = zvrf->table[afi][safi];
9d97533e 264 if (table) {
bd4fb615
DS
265 zebra_router_release_table(zvrf, zvrf->table_id,
266 afi, safi);
267 zvrf->table[afi][safi] = NULL;
9d97533e 268 }
d62a17ae 269 }
5a8dfcd8 270
41dc8c14
DS
271 if (zvrf->rnh_table[afi])
272 route_table_finish(zvrf->rnh_table[afi]);
273 if (zvrf->import_check_table[afi])
274 route_table_finish(zvrf->import_check_table[afi]);
5a8dfcd8 275 }
b7cfce93 276
84915b0a 277 /* Cleanup EVPN states for vrf */
b7cfce93
MK
278 zebra_vxlan_vrf_delete(zvrf);
279
d62a17ae 280 list_delete_all_node(zvrf->rid_all_sorted_list);
281 list_delete_all_node(zvrf->rid_lo_sorted_list);
282 XFREE(MTYPE_ZEBRA_VRF, zvrf);
283 vrf->info = NULL;
5a8dfcd8 284
d62a17ae 285 return 0;
7c551956
DS
286}
287
ecbc5a37
PG
288static int zebra_vrf_update(struct vrf *vrf)
289{
290 struct zebra_vrf *zvrf = vrf->info;
291
292 assert(zvrf);
293 if (IS_ZEBRA_DEBUG_EVENT)
294 zlog_debug("VRF %s id %u, name updated", vrf->name,
295 zvrf_id(zvrf));
296 zebra_vrf_add_update(zvrf);
297 return 0;
298}
299
300
22bd3e94 301/* Return if this VRF has any FRR configuration or not.
302 * IMPORTANT: This function needs to be updated when additional configuration
303 * is added for a VRF.
304 */
305int zebra_vrf_has_config(struct zebra_vrf *zvrf)
306{
22bd3e94 307 /* EVPN L3-VNI? */
308 if (zvrf->l3vni)
309 return 1;
310
311 return 0;
312}
313
7c551956 314/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
e9748a89
PG
315 * NOTE: Table-id is relevant on two modes:
316 * - case VRF backend is default : on default VRF only
317 * - case VRF backend is netns : on all VRFs
7c551956 318 */
d62a17ae 319struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
320 vrf_id_t vrf_id,
d7c0a89a 321 uint32_t table_id)
7c551956 322{
d62a17ae 323 struct route_table *table = NULL;
324
325 if (afi >= AFI_MAX || safi >= SAFI_MAX)
326 return NULL;
327
328 if (vrf_id == VRF_DEFAULT) {
329 if (table_id == RT_TABLE_MAIN
b3d43ff4 330 || table_id == zrouter.rtm_table_default)
d62a17ae 331 table = zebra_vrf_table(afi, safi, vrf_id);
332 else
333 table = zebra_vrf_other_route_table(afi, table_id,
334 vrf_id);
e9748a89
PG
335 } else if (vrf_is_backend_netns()) {
336 if (table_id == RT_TABLE_MAIN
b3d43ff4 337 || table_id == zrouter.rtm_table_default)
e9748a89
PG
338 table = zebra_vrf_table(afi, safi, vrf_id);
339 else
340 table = zebra_vrf_other_route_table(afi, table_id,
341 vrf_id);
d62a17ae 342 } else
343 table = zebra_vrf_table(afi, safi, vrf_id);
344
345 return table;
7c551956
DS
346}
347
5335613b
DS
348void zebra_rtable_node_cleanup(struct route_table *table,
349 struct route_node *node)
5a8dfcd8 350{
d62a17ae 351 struct route_entry *re, *next;
5a8dfcd8 352
a2addae8 353 RNODE_FOREACH_RE_SAFE (node, re, next) {
407c87a6
DS
354 rib_unlink(node, re);
355 }
5a8dfcd8 356
0a22ddfb 357 XFREE(MTYPE_RIB_DEST, node->info);
5a8dfcd8
RW
358}
359
d62a17ae 360static void zebra_rnhtable_node_cleanup(struct route_table *table,
361 struct route_node *node)
5a8dfcd8 362{
d62a17ae 363 if (node->info)
364 zebra_free_rnh(node->info);
5a8dfcd8
RW
365}
366
7c551956
DS
367/*
368 * Create a routing table for the specific AFI/SAFI in the given VRF.
369 */
d62a17ae 370static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
371 safi_t safi)
7c551956 372{
d62a17ae 373 assert(!zvrf->table[afi][safi]);
374
ea66cec4
DS
375 zvrf->table[afi][safi] =
376 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
7c551956
DS
377}
378
379/* Allocate new zebra VRF. */
d62a17ae 380struct zebra_vrf *zebra_vrf_alloc(void)
7c551956 381{
d62a17ae 382 struct zebra_vrf *zvrf;
d62a17ae 383
384 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
385
d62a17ae 386 zebra_vxlan_init_tables(zvrf);
387 zebra_mpls_init_tables(zvrf);
6833ae01 388 zebra_pw_init(zvrf);
e9748a89
PG
389 zvrf->table_id = RT_TABLE_MAIN;
390 /* by default table ID is default one */
d62a17ae 391 return zvrf;
7c551956
DS
392}
393
394/* Lookup VRF by identifier. */
d62a17ae 395struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 396{
d62a17ae 397 return vrf_info_lookup(vrf_id);
7c551956
DS
398}
399
51bdc5f8 400/* Lookup VRF by name. */
d62a17ae 401struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 402{
d62a17ae 403 struct vrf *vrf;
871d39b3 404
d62a17ae 405 if (!name)
406 name = VRF_DEFAULT_NAME;
a3d21ef3 407
d62a17ae 408 vrf = vrf_lookup_by_name(name);
409 if (vrf)
410 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 411
d62a17ae 412 return NULL;
871d39b3
DS
413}
414
7c551956 415/* Lookup the routing table in an enabled VRF. */
d62a17ae 416struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 417{
d62a17ae 418 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 419
d62a17ae 420 if (!zvrf)
421 return NULL;
7c551956 422
d62a17ae 423 if (afi >= AFI_MAX || safi >= SAFI_MAX)
424 return NULL;
7c551956 425
d62a17ae 426 return zvrf->table[afi][safi];
7c551956
DS
427}
428
d7c0a89a 429struct route_table *zebra_vrf_other_route_table(afi_t afi, uint32_t table_id,
d62a17ae 430 vrf_id_t vrf_id)
7c551956 431{
d62a17ae 432 struct zebra_vrf *zvrf;
d62a17ae 433
434 zvrf = vrf_info_lookup(vrf_id);
435 if (!zvrf)
436 return NULL;
437
438 if (afi >= AFI_MAX)
439 return NULL;
440
e9748a89 441 if ((table_id != RT_TABLE_MAIN)
b3d43ff4 442 && (table_id != zrouter.rtm_table_default)) {
e9748a89 443 if (zvrf->table_id == RT_TABLE_MAIN ||
b3d43ff4 444 zvrf->table_id == zrouter.rtm_table_default) {
e9748a89
PG
445 /* this VRF use default table
446 * so in all cases, it does not use specific table
447 * so it is possible to configure tables in this VRF
448 */
89272910
DS
449 return zebra_router_get_table(zvrf, table_id, afi,
450 SAFI_UNICAST);
e9748a89 451 }
d62a17ae 452 }
453
454 return zvrf->table[afi][SAFI_UNICAST];
7c551956
DS
455}
456
d62a17ae 457static int vrf_config_write(struct vty *vty)
f30c50b9 458{
d62a17ae 459 struct vrf *vrf;
460 struct zebra_vrf *zvrf;
461
a2addae8 462 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 463 zvrf = vrf->info;
1e9f448f
DS
464
465 if (!zvrf)
466 continue;
467
a5654735
MK
468 if (zvrf_id(zvrf) == VRF_DEFAULT) {
469 if (zvrf->l3vni)
470 vty_out(vty, "vni %u\n", zvrf->l3vni);
c319e19d
QY
471 } else {
472 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
22bd3e94 473 if (zvrf->l3vni)
996c9314
LB
474 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
475 is_l3vni_for_prefix_routes_only(
476 zvrf->l3vni)
477 ? " prefix-routes-only"
478 : "");
b95c1883 479 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
22bd3e94 480 }
37728041 481
7cf16e19 482 zebra_routemap_config_write_protocol(vty, zvrf);
483
c319e19d
QY
484 if (zvrf_id(zvrf) != VRF_DEFAULT)
485 vty_endframe(vty, " exit-vrf\n!\n");
7cf16e19 486 else
487 vty_out(vty, "!\n");
d62a17ae 488 }
489 return 0;
f30c50b9
RW
490}
491
7c551956 492/* Zebra VRF initialization. */
d62a17ae 493void zebra_vrf_init(void)
7c551956 494{
996c9314 495 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
ecbc5a37 496 zebra_vrf_delete, zebra_vrf_update);
7c551956 497
3bc34908 498 vrf_cmd_init(vrf_config_write, &zserv_privs);
7c551956 499}