]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
Merge pull request #3071 from donaldsharp/fix_vrf_name
[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
7c551956 31#include "zebra/debug.h"
bf094f69 32#include "zebra/zapi_msg.h"
7c551956
DS
33#include "zebra/rib.h"
34#include "zebra/zebra_vrf.h"
5a8dfcd8 35#include "zebra/zebra_rnh.h"
7c551956 36#include "zebra/router-id.h"
4a1ab8e4 37#include "zebra/zebra_memory.h"
5a8dfcd8 38#include "zebra/interface.h"
7758e3f3 39#include "zebra/zebra_mpls.h"
13d60d35 40#include "zebra/zebra_vxlan.h"
3bc34908 41#include "zebra/zebra_netns_notify.h"
7c551956
DS
42
43extern struct zebra_t zebrad;
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
d62a17ae 59 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
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
d62a17ae 71 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
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 route_table *table;
147 struct interface *ifp;
d62a17ae 148 afi_t afi;
149 safi_t safi;
9d97533e 150 unsigned i;
d62a17ae 151
84915b0a 152 assert(zvrf);
153 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
154 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
155 zvrf_id(zvrf));
d62a17ae 156
84915b0a 157 /* Stop any VxLAN-EVPN processing. */
158 zebra_vxlan_vrf_disable(zvrf);
d62a17ae 159
84915b0a 160 /* Inform clients that the VRF is now inactive. This is a
161 * delete for the clients.
162 */
d62a17ae 163 zebra_vrf_delete_update(zvrf);
164
9d97533e 165 /* If asked to retain routes, there's nothing more to do. */
166 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
167 return 0;
168
169 /* Remove all routes. */
170 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
171 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
172 rib_close_table(zvrf->table[afi][safi]);
9d97533e 173 }
174
175 /* Cleanup Vxlan, MPLS and PW tables. */
176 zebra_vxlan_cleanup_tables(zvrf);
177 zebra_mpls_cleanup_tables(zvrf);
178 zebra_pw_exit(zvrf);
179
996c9314
LB
180 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
181 */
9d97533e 182 FOR_ALL_INTERFACES (vrf, ifp)
183 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
184
185 /* clean-up work queues */
186 for (i = 0; i < MQ_SIZE; i++) {
187 struct listnode *lnode, *nnode;
188 struct route_node *rnode;
189 rib_dest_t *dest;
190
996c9314
LB
191 for (ALL_LIST_ELEMENTS(zebrad.mq->subq[i], lnode, nnode,
192 rnode)) {
9d97533e 193 dest = rib_dest_from_rnode(rnode);
194 if (dest && rib_dest_vrf(dest) == zvrf) {
195 route_unlock_node(rnode);
196 list_delete_node(zebrad.mq->subq[i], lnode);
197 zebrad.mq->size--;
198 }
d62a17ae 199 }
9d97533e 200 }
7c551956 201
9d97533e 202 /* Cleanup (free) routing tables and NHT tables. */
203 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
204 void *table_info;
5a8dfcd8 205
9d97533e 206 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
207 table = zvrf->table[afi][safi];
208 table_info = table->info;
209 route_table_finish(table);
210 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
211 zvrf->table[afi][safi] = NULL;
212 }
213
9d97533e 214 route_table_finish(zvrf->rnh_table[afi]);
215 zvrf->rnh_table[afi] = NULL;
216 route_table_finish(zvrf->import_check_table[afi]);
217 zvrf->import_check_table[afi] = NULL;
5a8dfcd8
RW
218 }
219
d62a17ae 220 return 0;
7c551956
DS
221}
222
d62a17ae 223static int zebra_vrf_delete(struct vrf *vrf)
7c551956 224{
d62a17ae 225 struct zebra_vrf *zvrf = vrf->info;
226 struct route_table *table;
d62a17ae 227 afi_t afi;
228 safi_t safi;
229 unsigned i;
230
231 assert(zvrf);
84915b0a 232 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
233 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
234 zvrf_id(zvrf));
5a8dfcd8 235
d62a17ae 236 /* clean-up work queues */
237 for (i = 0; i < MQ_SIZE; i++) {
238 struct listnode *lnode, *nnode;
239 struct route_node *rnode;
240 rib_dest_t *dest;
241
996c9314
LB
242 for (ALL_LIST_ELEMENTS(zebrad.mq->subq[i], lnode, nnode,
243 rnode)) {
d62a17ae 244 dest = rib_dest_from_rnode(rnode);
245 if (dest && rib_dest_vrf(dest) == zvrf) {
246 route_unlock_node(rnode);
247 list_delete_node(zebrad.mq->subq[i], lnode);
248 zebrad.mq->size--;
249 }
250 }
5a8dfcd8 251 }
5a8dfcd8 252
84915b0a 253 /* Free Vxlan and MPLS. */
254 zebra_vxlan_close_tables(zvrf);
255 zebra_mpls_close_tables(zvrf);
256
d62a17ae 257 /* release allocated memory */
258 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
259 void *table_info;
0f124559 260
d62a17ae 261 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
262 table = zvrf->table[afi][safi];
9d97533e 263 if (table) {
264 table_info = table->info;
265 route_table_finish(table);
266 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
267 }
d62a17ae 268 }
5a8dfcd8 269
d62a17ae 270 route_table_finish(zvrf->rnh_table[afi]);
271 route_table_finish(zvrf->import_check_table[afi]);
5a8dfcd8 272 }
b7cfce93 273
84915b0a 274 /* Cleanup EVPN states for vrf */
b7cfce93
MK
275 zebra_vxlan_vrf_delete(zvrf);
276
d62a17ae 277 list_delete_all_node(zvrf->rid_all_sorted_list);
278 list_delete_all_node(zvrf->rid_lo_sorted_list);
279 XFREE(MTYPE_ZEBRA_VRF, zvrf);
280 vrf->info = NULL;
5a8dfcd8 281
d62a17ae 282 return 0;
7c551956
DS
283}
284
ecbc5a37
PG
285static int zebra_vrf_update(struct vrf *vrf)
286{
287 struct zebra_vrf *zvrf = vrf->info;
288
289 assert(zvrf);
290 if (IS_ZEBRA_DEBUG_EVENT)
291 zlog_debug("VRF %s id %u, name updated", vrf->name,
292 zvrf_id(zvrf));
293 zebra_vrf_add_update(zvrf);
294 return 0;
295}
296
297
22bd3e94 298/* Return if this VRF has any FRR configuration or not.
299 * IMPORTANT: This function needs to be updated when additional configuration
300 * is added for a VRF.
301 */
302int zebra_vrf_has_config(struct zebra_vrf *zvrf)
303{
22bd3e94 304 /* EVPN L3-VNI? */
305 if (zvrf->l3vni)
306 return 1;
307
308 return 0;
309}
310
7c551956 311/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
e9748a89
PG
312 * NOTE: Table-id is relevant on two modes:
313 * - case VRF backend is default : on default VRF only
314 * - case VRF backend is netns : on all VRFs
7c551956 315 */
d62a17ae 316struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
317 vrf_id_t vrf_id,
d7c0a89a 318 uint32_t table_id)
7c551956 319{
d62a17ae 320 struct route_table *table = NULL;
321
322 if (afi >= AFI_MAX || safi >= SAFI_MAX)
323 return NULL;
324
325 if (vrf_id == VRF_DEFAULT) {
326 if (table_id == RT_TABLE_MAIN
327 || table_id == zebrad.rtm_table_default)
328 table = zebra_vrf_table(afi, safi, vrf_id);
329 else
330 table = zebra_vrf_other_route_table(afi, table_id,
331 vrf_id);
e9748a89
PG
332 } else if (vrf_is_backend_netns()) {
333 if (table_id == RT_TABLE_MAIN
334 || table_id == zebrad.rtm_table_default)
335 table = zebra_vrf_table(afi, safi, vrf_id);
336 else
337 table = zebra_vrf_other_route_table(afi, table_id,
338 vrf_id);
d62a17ae 339 } else
340 table = zebra_vrf_table(afi, safi, vrf_id);
341
342 return table;
7c551956
DS
343}
344
5335613b
DS
345void zebra_rtable_node_cleanup(struct route_table *table,
346 struct route_node *node)
5a8dfcd8 347{
d62a17ae 348 struct route_entry *re, *next;
5a8dfcd8 349
a2addae8 350 RNODE_FOREACH_RE_SAFE (node, re, next) {
407c87a6
DS
351 rib_unlink(node, re);
352 }
5a8dfcd8 353
d62a17ae 354 if (node->info)
355 XFREE(MTYPE_RIB_DEST, node->info);
5a8dfcd8
RW
356}
357
d62a17ae 358static void zebra_rnhtable_node_cleanup(struct route_table *table,
359 struct route_node *node)
5a8dfcd8 360{
d62a17ae 361 if (node->info)
362 zebra_free_rnh(node->info);
5a8dfcd8
RW
363}
364
7c551956
DS
365/*
366 * Create a routing table for the specific AFI/SAFI in the given VRF.
367 */
d62a17ae 368static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
369 safi_t safi)
7c551956 370{
d62a17ae 371 rib_table_info_t *info;
372 struct route_table *table;
373
374 assert(!zvrf->table[afi][safi]);
375
376 if (afi == AFI_IP6)
377 table = srcdest_table_init();
378 else
379 table = route_table_init();
380 table->cleanup = zebra_rtable_node_cleanup;
381 zvrf->table[afi][safi] = table;
382
383 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
384 info->zvrf = zvrf;
385 info->afi = afi;
386 info->safi = safi;
387 table->info = info;
7c551956
DS
388}
389
390/* Allocate new zebra VRF. */
d62a17ae 391struct zebra_vrf *zebra_vrf_alloc(void)
7c551956 392{
d62a17ae 393 struct zebra_vrf *zvrf;
d62a17ae 394
395 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
396
d62a17ae 397 zebra_vxlan_init_tables(zvrf);
398 zebra_mpls_init_tables(zvrf);
6833ae01 399 zebra_pw_init(zvrf);
e9748a89
PG
400 zvrf->table_id = RT_TABLE_MAIN;
401 /* by default table ID is default one */
d62a17ae 402 return zvrf;
7c551956
DS
403}
404
405/* Lookup VRF by identifier. */
d62a17ae 406struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 407{
d62a17ae 408 return vrf_info_lookup(vrf_id);
7c551956
DS
409}
410
51bdc5f8 411/* Lookup VRF by name. */
d62a17ae 412struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 413{
d62a17ae 414 struct vrf *vrf;
871d39b3 415
d62a17ae 416 if (!name)
417 name = VRF_DEFAULT_NAME;
a3d21ef3 418
d62a17ae 419 vrf = vrf_lookup_by_name(name);
420 if (vrf)
421 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 422
d62a17ae 423 return NULL;
871d39b3
DS
424}
425
7c551956 426/* Lookup the routing table in an enabled VRF. */
d62a17ae 427struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 428{
d62a17ae 429 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 430
d62a17ae 431 if (!zvrf)
432 return NULL;
7c551956 433
d62a17ae 434 if (afi >= AFI_MAX || safi >= SAFI_MAX)
435 return NULL;
7c551956 436
d62a17ae 437 return zvrf->table[afi][safi];
7c551956
DS
438}
439
d7c0a89a 440struct route_table *zebra_vrf_other_route_table(afi_t afi, uint32_t table_id,
d62a17ae 441 vrf_id_t vrf_id)
7c551956 442{
d62a17ae 443 struct zebra_vrf *zvrf;
5335613b 444 struct zebra_ns *zns;
d62a17ae 445
446 zvrf = vrf_info_lookup(vrf_id);
447 if (!zvrf)
448 return NULL;
449
5335613b
DS
450 zns = zvrf->zns;
451
d62a17ae 452 if (afi >= AFI_MAX)
453 return NULL;
454
e9748a89 455 if ((table_id != RT_TABLE_MAIN)
d62a17ae 456 && (table_id != zebrad.rtm_table_default)) {
e9748a89
PG
457 if (zvrf->table_id == RT_TABLE_MAIN ||
458 zvrf->table_id == zebrad.rtm_table_default) {
459 /* this VRF use default table
460 * so in all cases, it does not use specific table
461 * so it is possible to configure tables in this VRF
462 */
463 return zebra_ns_get_table(zns, zvrf, table_id, afi);
464 }
d62a17ae 465 }
466
467 return zvrf->table[afi][SAFI_UNICAST];
7c551956
DS
468}
469
d62a17ae 470static int vrf_config_write(struct vty *vty)
f30c50b9 471{
d62a17ae 472 struct vrf *vrf;
473 struct zebra_vrf *zvrf;
474
a2addae8 475 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 476 zvrf = vrf->info;
1e9f448f
DS
477
478 if (!zvrf)
479 continue;
480
a5654735
MK
481 if (zvrf_id(zvrf) == VRF_DEFAULT) {
482 if (zvrf->l3vni)
483 vty_out(vty, "vni %u\n", zvrf->l3vni);
484 vty_out(vty, "!\n");
c319e19d
QY
485 } else {
486 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
22bd3e94 487 if (zvrf->l3vni)
996c9314
LB
488 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
489 is_l3vni_for_prefix_routes_only(
490 zvrf->l3vni)
491 ? " prefix-routes-only"
492 : "");
b95c1883 493 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
c319e19d 494
22bd3e94 495 }
37728041 496
c319e19d
QY
497 if (zvrf_id(zvrf) != VRF_DEFAULT)
498 vty_endframe(vty, " exit-vrf\n!\n");
d62a17ae 499 }
500 return 0;
f30c50b9
RW
501}
502
7c551956 503/* Zebra VRF initialization. */
d62a17ae 504void zebra_vrf_init(void)
7c551956 505{
996c9314 506 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
ecbc5a37 507 zebra_vrf_delete, zebra_vrf_update);
7c551956 508
3bc34908 509 vrf_cmd_init(vrf_config_write, &zserv_privs);
7c551956 510}