]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
bgpd/zebra: show command enhancements for EVPN symmetric routing
[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"
7c551956 28
82f97584 29#include "vty.h"
7c551956
DS
30#include "zebra/debug.h"
31#include "zebra/zserv.h"
32#include "zebra/rib.h"
33#include "zebra/zebra_vrf.h"
5a8dfcd8 34#include "zebra/zebra_rnh.h"
7c551956 35#include "zebra/router-id.h"
4a1ab8e4 36#include "zebra/zebra_memory.h"
28f6dde8 37#include "zebra/zebra_static.h"
5a8dfcd8 38#include "zebra/interface.h"
7758e3f3 39#include "zebra/zebra_mpls.h"
13d60d35 40#include "zebra/zebra_vxlan.h"
7c551956
DS
41
42extern struct zebra_t zebrad;
43
44/* VRF information update. */
d62a17ae 45static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
7c551956 46{
d62a17ae 47 struct listnode *node, *nnode;
48 struct zserv *client;
7c551956 49
d62a17ae 50 if (IS_ZEBRA_DEBUG_EVENT)
51 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
7c551956 52
d62a17ae 53 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
54 zsend_vrf_add(client, zvrf);
7c551956
DS
55}
56
d62a17ae 57static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
7c551956 58{
d62a17ae 59 struct listnode *node, *nnode;
60 struct zserv *client;
7c551956 61
d62a17ae 62 if (IS_ZEBRA_DEBUG_EVENT)
63 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
7c551956 64
d62a17ae 65 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
66 zsend_vrf_delete(client, zvrf);
7c551956
DS
67}
68
d62a17ae 69void zebra_vrf_update_all(struct zserv *client)
7c551956 70{
d62a17ae 71 struct vrf *vrf;
7c551956 72
a2addae8 73 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
d62a17ae 74 if (vrf->vrf_id)
75 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
76 }
7c551956
DS
77}
78
79/* Callback upon creating a new VRF. */
d62a17ae 80static int zebra_vrf_new(struct vrf *vrf)
7c551956 81{
d62a17ae 82 struct zebra_vrf *zvrf;
7c551956 83
d62a17ae 84 if (IS_ZEBRA_DEBUG_EVENT)
85 zlog_info("ZVRF %s with id %u", vrf->name, vrf->vrf_id);
7c551956 86
d62a17ae 87 zvrf = zebra_vrf_alloc();
88 zvrf->zns = zebra_ns_lookup(
89 NS_DEFAULT); /* Point to the global (single) NS */
90 router_id_init(zvrf);
91 vrf->info = zvrf;
92 zvrf->vrf = vrf;
34f8e6af 93
d62a17ae 94 return 0;
7c551956
DS
95}
96
97/* Callback upon enabling a VRF. */
d62a17ae 98static int zebra_vrf_enable(struct vrf *vrf)
7c551956 99{
d62a17ae 100 struct zebra_vrf *zvrf = vrf->info;
101 struct route_table *stable;
102 struct route_node *rn;
103 struct static_route *si;
104 struct interface *ifp;
105 afi_t afi;
106 safi_t safi;
107
108 assert(zvrf);
109
110 zebra_vrf_add_update(zvrf);
111
112 for (afi = AFI_IP; afi < AFI_MAX; afi++)
113 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
114 stable = zvrf->stable[afi][safi];
115 if (!stable)
116 continue;
117
118 for (rn = route_top(stable); rn; rn = route_next(rn))
119 for (si = rn->info; si; si = si->next) {
120 si->vrf_id = vrf->vrf_id;
121 if (si->ifindex) {
122 ifp = if_lookup_by_name(
123 si->ifname, si->vrf_id);
124 if (ifp)
125 si->ifindex =
126 ifp->ifindex;
127 else
128 continue;
129 }
130 static_install_route(afi, safi, &rn->p,
131 NULL, si);
132 }
fb148af4 133 }
2414ffe5 134
d62a17ae 135 return 0;
7c551956
DS
136}
137
138/* Callback upon disabling a VRF. */
d62a17ae 139static int zebra_vrf_disable(struct vrf *vrf)
7c551956 140{
d62a17ae 141 struct zebra_vrf *zvrf = vrf->info;
142 struct route_table *stable;
143 struct route_node *rn;
144 struct static_route *si;
145 afi_t afi;
146 safi_t safi;
147
148 if (IS_ZEBRA_DEBUG_KERNEL)
149 zlog_debug("VRF %s id %u is now disabled.", zvrf_name(zvrf),
150 zvrf_id(zvrf));
151
152 for (afi = AFI_IP; afi < AFI_MAX; afi++)
153 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
154 stable = zvrf->stable[afi][safi];
155 if (!stable)
156 continue;
157
158 for (rn = route_top(stable); rn; rn = route_next(rn))
159 for (si = rn->info; si; si = si->next)
160 static_uninstall_route(
161 afi, safi, &rn->p, NULL, si);
162 }
163
164 return 0;
7c551956
DS
165}
166
d62a17ae 167static int zebra_vrf_delete(struct vrf *vrf)
7c551956 168{
d62a17ae 169 struct zebra_vrf *zvrf = vrf->info;
170 struct route_table *table;
171 u_int32_t table_id;
172 afi_t afi;
173 safi_t safi;
174 unsigned i;
175
176 assert(zvrf);
177
178 zebra_vrf_delete_update(zvrf);
179
180 /* uninstall everything */
181 if (!CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN)) {
d62a17ae 182 struct interface *ifp;
183
184 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
185 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST;
186 safi++)
187 rib_close_table(zvrf->table[afi][safi]);
188
189 if (vrf->vrf_id == VRF_DEFAULT)
190 for (table_id = 0;
191 table_id < ZEBRA_KERNEL_TABLE_MAX;
192 table_id++)
193 if (zvrf->other_table[afi][table_id])
194 rib_close_table(
195 zvrf->other_table
196 [afi]
197 [table_id]);
198 }
7c551956 199
d62a17ae 200 /* Cleanup Vxlan table and update kernel */
201 zebra_vxlan_close_tables(zvrf);
7c551956 202
d62a17ae 203 zebra_mpls_close_tables(zvrf);
6833ae01 204 zebra_pw_exit(zvrf);
5a8dfcd8 205
451fda4f 206 FOR_ALL_INTERFACES (vrf, ifp)
d62a17ae 207 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
5a8dfcd8
RW
208 }
209
d62a17ae 210 /* clean-up work queues */
211 for (i = 0; i < MQ_SIZE; i++) {
212 struct listnode *lnode, *nnode;
213 struct route_node *rnode;
214 rib_dest_t *dest;
215
216 for (ALL_LIST_ELEMENTS(zebrad.mq->subq[i], lnode, nnode,
217 rnode)) {
218 dest = rib_dest_from_rnode(rnode);
219 if (dest && rib_dest_vrf(dest) == zvrf) {
220 route_unlock_node(rnode);
221 list_delete_node(zebrad.mq->subq[i], lnode);
222 zebrad.mq->size--;
223 }
224 }
5a8dfcd8 225 }
5a8dfcd8 226
d62a17ae 227 /* release allocated memory */
228 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
229 void *table_info;
0f124559 230
d62a17ae 231 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
232 table = zvrf->table[afi][safi];
233 table_info = table->info;
234 route_table_finish(table);
235 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
236
237 table = zvrf->stable[afi][safi];
238 route_table_finish(table);
239 }
5a8dfcd8 240
d62a17ae 241 for (table_id = 0; table_id < ZEBRA_KERNEL_TABLE_MAX;
242 table_id++)
243 if (zvrf->other_table[afi][table_id]) {
244 table = zvrf->other_table[afi][table_id];
245 table_info = table->info;
246 route_table_finish(table);
247 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
248 }
249
250 route_table_finish(zvrf->rnh_table[afi]);
251 route_table_finish(zvrf->import_check_table[afi]);
5a8dfcd8 252 }
b7cfce93
MK
253
254 /* cleanup evpn states for vrf */
255 zebra_vxlan_vrf_delete(zvrf);
256
d62a17ae 257 list_delete_all_node(zvrf->rid_all_sorted_list);
258 list_delete_all_node(zvrf->rid_lo_sorted_list);
259 XFREE(MTYPE_ZEBRA_VRF, zvrf);
260 vrf->info = NULL;
5a8dfcd8 261
d62a17ae 262 return 0;
7c551956
DS
263}
264
265/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
266 * NOTE: Table-id is relevant only in the Default VRF.
267 */
d62a17ae 268struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
269 vrf_id_t vrf_id,
270 u_int32_t table_id)
7c551956 271{
d62a17ae 272 struct route_table *table = NULL;
273
274 if (afi >= AFI_MAX || safi >= SAFI_MAX)
275 return NULL;
276
277 if (vrf_id == VRF_DEFAULT) {
278 if (table_id == RT_TABLE_MAIN
279 || table_id == zebrad.rtm_table_default)
280 table = zebra_vrf_table(afi, safi, vrf_id);
281 else
282 table = zebra_vrf_other_route_table(afi, table_id,
283 vrf_id);
284 } else
285 table = zebra_vrf_table(afi, safi, vrf_id);
286
287 return table;
7c551956
DS
288}
289
d62a17ae 290static void zebra_rtable_node_cleanup(struct route_table *table,
291 struct route_node *node)
5a8dfcd8 292{
d62a17ae 293 struct route_entry *re, *next;
5a8dfcd8 294
a2addae8 295 RNODE_FOREACH_RE_SAFE (node, re, next) {
407c87a6
DS
296 rib_unlink(node, re);
297 }
5a8dfcd8 298
d62a17ae 299 if (node->info)
300 XFREE(MTYPE_RIB_DEST, node->info);
5a8dfcd8
RW
301}
302
d62a17ae 303static void zebra_stable_node_cleanup(struct route_table *table,
304 struct route_node *node)
5a8dfcd8 305{
d62a17ae 306 struct static_route *si, *next;
307
308 if (node->info)
309 for (si = node->info; si; si = next) {
310 next = si->next;
311 XFREE(MTYPE_STATIC_ROUTE, si);
312 }
5a8dfcd8
RW
313}
314
d62a17ae 315static void zebra_rnhtable_node_cleanup(struct route_table *table,
316 struct route_node *node)
5a8dfcd8 317{
d62a17ae 318 if (node->info)
319 zebra_free_rnh(node->info);
5a8dfcd8
RW
320}
321
7c551956
DS
322/*
323 * Create a routing table for the specific AFI/SAFI in the given VRF.
324 */
d62a17ae 325static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
326 safi_t safi)
7c551956 327{
d62a17ae 328 rib_table_info_t *info;
329 struct route_table *table;
330
331 assert(!zvrf->table[afi][safi]);
332
333 if (afi == AFI_IP6)
334 table = srcdest_table_init();
335 else
336 table = route_table_init();
337 table->cleanup = zebra_rtable_node_cleanup;
338 zvrf->table[afi][safi] = table;
339
340 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
341 info->zvrf = zvrf;
342 info->afi = afi;
343 info->safi = safi;
344 table->info = info;
7c551956
DS
345}
346
347/* Allocate new zebra VRF. */
d62a17ae 348struct zebra_vrf *zebra_vrf_alloc(void)
7c551956 349{
d62a17ae 350 struct zebra_vrf *zvrf;
351 afi_t afi;
352 safi_t safi;
353 struct route_table *table;
354
355 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
356
357 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
358 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
359 zebra_vrf_table_create(zvrf, afi, safi);
360 if (afi == AFI_IP6)
361 table = srcdest_table_init();
362 else
363 table = route_table_init();
364 table->cleanup = zebra_stable_node_cleanup;
365 zvrf->stable[afi][safi] = table;
366 }
367
368 table = route_table_init();
369 table->cleanup = zebra_rnhtable_node_cleanup;
370 zvrf->rnh_table[afi] = table;
371
372 table = route_table_init();
373 table->cleanup = zebra_rnhtable_node_cleanup;
374 zvrf->import_check_table[afi] = table;
375 }
376
377 zebra_vxlan_init_tables(zvrf);
378 zebra_mpls_init_tables(zvrf);
6833ae01 379 zebra_pw_init(zvrf);
d62a17ae 380
381 return zvrf;
7c551956
DS
382}
383
384/* Lookup VRF by identifier. */
d62a17ae 385struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 386{
d62a17ae 387 return vrf_info_lookup(vrf_id);
7c551956
DS
388}
389
51bdc5f8 390/* Lookup VRF by name. */
d62a17ae 391struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 392{
d62a17ae 393 struct vrf *vrf;
871d39b3 394
d62a17ae 395 if (!name)
396 name = VRF_DEFAULT_NAME;
a3d21ef3 397
d62a17ae 398 vrf = vrf_lookup_by_name(name);
399 if (vrf)
400 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 401
d62a17ae 402 return NULL;
871d39b3
DS
403}
404
7c551956 405/* Lookup the routing table in an enabled VRF. */
d62a17ae 406struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 407{
d62a17ae 408 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 409
d62a17ae 410 if (!zvrf)
411 return NULL;
7c551956 412
d62a17ae 413 if (afi >= AFI_MAX || safi >= SAFI_MAX)
414 return NULL;
7c551956 415
d62a17ae 416 return zvrf->table[afi][safi];
7c551956
DS
417}
418
419/* Lookup the static routing table in a VRF. */
d62a17ae 420struct route_table *zebra_vrf_static_table(afi_t afi, safi_t safi,
421 struct zebra_vrf *zvrf)
7c551956 422{
d62a17ae 423 if (!zvrf)
424 return NULL;
7c551956 425
d62a17ae 426 if (afi >= AFI_MAX || safi >= SAFI_MAX)
427 return NULL;
7c551956 428
d62a17ae 429 return zvrf->stable[afi][safi];
7c551956
DS
430}
431
d62a17ae 432struct route_table *zebra_vrf_other_route_table(afi_t afi, u_int32_t table_id,
433 vrf_id_t vrf_id)
7c551956 434{
d62a17ae 435 struct zebra_vrf *zvrf;
436 rib_table_info_t *info;
437 struct route_table *table;
438
439 zvrf = vrf_info_lookup(vrf_id);
440 if (!zvrf)
441 return NULL;
442
443 if (afi >= AFI_MAX)
444 return NULL;
445
446 if (table_id >= ZEBRA_KERNEL_TABLE_MAX)
447 return NULL;
448
449 if ((vrf_id == VRF_DEFAULT) && (table_id != RT_TABLE_MAIN)
450 && (table_id != zebrad.rtm_table_default)) {
451 if (zvrf->other_table[afi][table_id] == NULL) {
452 table = (afi == AFI_IP6) ? srcdest_table_init()
453 : route_table_init();
454 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
455 info->zvrf = zvrf;
456 info->afi = afi;
457 info->safi = SAFI_UNICAST;
458 table->info = info;
459 zvrf->other_table[afi][table_id] = table;
460 }
461
462 return (zvrf->other_table[afi][table_id]);
463 }
464
465 return zvrf->table[afi][SAFI_UNICAST];
7c551956
DS
466}
467
d62a17ae 468static int vrf_config_write(struct vty *vty)
f30c50b9 469{
d62a17ae 470 struct vrf *vrf;
471 struct zebra_vrf *zvrf;
472
a2addae8 473 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 474 zvrf = vrf->info;
1e9f448f
DS
475
476 if (!zvrf)
477 continue;
478
37728041
DS
479 if (vrf->vrf_id != VRF_DEFAULT)
480 vty_out(vty, "vrf %s\n", zvrf_name(zvrf));
481
482 static_config(vty, zvrf, AFI_IP, SAFI_UNICAST, "ip route");
483 static_config(vty, zvrf, AFI_IP, SAFI_MULTICAST, "ip mroute");
484 static_config(vty, zvrf, AFI_IP6, SAFI_UNICAST, "ipv6 route");
ab32921c 485
37728041 486 if (vrf->vrf_id != VRF_DEFAULT && zvrf->l3vni)
ab32921c 487 vty_out(vty, " vni %u\n", zvrf->l3vni);
37728041
DS
488
489 if (vrf->vrf_id != VRF_DEFAULT)
490 vty_out(vty, "!\n");
d62a17ae 491 }
492 return 0;
f30c50b9
RW
493}
494
7c551956 495/* Zebra VRF initialization. */
d62a17ae 496void zebra_vrf_init(void)
7c551956 497{
d62a17ae 498 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
499 zebra_vrf_delete);
7c551956 500
d62a17ae 501 vrf_cmd_init(vrf_config_write);
7c551956 502}