2 * Copyright (C) 2016 CumulusNetworks
5 * This file is part of Quagga
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
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.
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
27 #include "srcdest_table.h"
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"
46 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
48 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
49 struct route_node
*node
);
51 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_VRF
, "ZEBRA VRF")
52 DEFINE_MTYPE_STATIC(ZEBRA
, OTHER_TABLE
, "Other Table")
54 /* VRF information update. */
55 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
57 struct listnode
*node
, *nnode
;
60 if (IS_ZEBRA_DEBUG_EVENT
)
61 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
63 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
64 zsend_vrf_add(client
, zvrf
);
67 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
69 struct listnode
*node
, *nnode
;
72 if (IS_ZEBRA_DEBUG_EVENT
)
73 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
75 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
76 zsend_vrf_delete(client
, zvrf
);
79 void zebra_vrf_update_all(struct zserv
*client
)
83 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
84 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
85 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
89 /* Callback upon creating a new VRF. */
90 static int zebra_vrf_new(struct vrf
*vrf
)
92 struct zebra_vrf
*zvrf
;
94 if (IS_ZEBRA_DEBUG_EVENT
)
95 zlog_debug("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
97 zvrf
= zebra_vrf_alloc();
101 otable_init(&zvrf
->other_tables
);
103 router_id_init(zvrf
);
107 /* Callback upon enabling a VRF. */
108 static int zebra_vrf_enable(struct vrf
*vrf
)
110 struct zebra_vrf
*zvrf
= vrf
->info
;
111 struct route_table
*table
;
116 if (IS_ZEBRA_DEBUG_EVENT
)
117 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
120 if (vrf_is_backend_netns())
121 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
123 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
124 #if defined(HAVE_RTADV)
128 /* Inform clients that the VRF is now active. This is an
129 * add for the clients.
132 zebra_vrf_add_update(zvrf
);
133 /* Allocate tables */
134 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
135 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
136 zebra_vrf_table_create(zvrf
, afi
, safi
);
138 table
= route_table_init();
139 table
->cleanup
= zebra_rnhtable_node_cleanup
;
140 zvrf
->rnh_table
[afi
] = table
;
142 table
= route_table_init();
143 table
->cleanup
= zebra_rnhtable_node_cleanup
;
144 zvrf
->import_check_table
[afi
] = table
;
147 /* Kick off any VxLAN-EVPN processing. */
148 zebra_vxlan_vrf_enable(zvrf
);
153 /* Callback upon disabling a VRF. */
154 static int zebra_vrf_disable(struct vrf
*vrf
)
156 struct zebra_vrf
*zvrf
= vrf
->info
;
157 struct interface
*ifp
;
163 if (IS_ZEBRA_DEBUG_EVENT
)
164 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
167 /* Stop any VxLAN-EVPN processing. */
168 zebra_vxlan_vrf_disable(zvrf
);
170 #if defined(HAVE_RTADV)
171 rtadv_terminate(zvrf
);
174 /* Inform clients that the VRF is now inactive. This is a
175 * delete for the clients.
177 zebra_vrf_delete_update(zvrf
);
179 /* If asked to retain routes, there's nothing more to do. */
180 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
183 /* Remove all routes. */
184 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
185 route_table_finish(zvrf
->rnh_table
[afi
]);
186 zvrf
->rnh_table
[afi
] = NULL
;
187 route_table_finish(zvrf
->import_check_table
[afi
]);
188 zvrf
->import_check_table
[afi
] = NULL
;
190 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
191 rib_close_table(zvrf
->table
[afi
][safi
]);
194 /* Cleanup Vxlan, MPLS and PW tables. */
195 zebra_vxlan_cleanup_tables(zvrf
);
196 zebra_mpls_cleanup_tables(zvrf
);
199 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
201 FOR_ALL_INTERFACES (vrf
, ifp
)
202 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
204 /* clean-up work queues */
205 for (i
= 0; i
< MQ_SIZE
; i
++) {
206 struct listnode
*lnode
, *nnode
;
207 struct route_node
*rnode
;
210 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
212 dest
= rib_dest_from_rnode(rnode
);
213 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
214 route_unlock_node(rnode
);
215 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
221 /* Cleanup (free) routing tables and NHT tables. */
222 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
224 * Set the table pointer to NULL as that
225 * we no-longer need a copy of it, nor do we
226 * own this data, the zebra_router structure
227 * owns these tables. Once we've cleaned up the
228 * table, see rib_close_table above
229 * we no-longer need this pointer.
231 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
232 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
234 zvrf
->table
[afi
][safi
] = NULL
;
241 static int zebra_vrf_delete(struct vrf
*vrf
)
243 struct zebra_vrf
*zvrf
= vrf
->info
;
244 struct other_route_table
*otable
;
245 struct route_table
*table
;
251 if (IS_ZEBRA_DEBUG_EVENT
)
252 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
255 /* clean-up work queues */
256 for (i
= 0; i
< MQ_SIZE
; i
++) {
257 struct listnode
*lnode
, *nnode
;
258 struct route_node
*rnode
;
261 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
263 dest
= rib_dest_from_rnode(rnode
);
264 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
265 route_unlock_node(rnode
);
266 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
272 /* Free Vxlan and MPLS. */
273 zebra_vxlan_close_tables(zvrf
);
274 zebra_mpls_close_tables(zvrf
);
276 /* release allocated memory */
277 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
278 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
279 table
= zvrf
->table
[afi
][safi
];
281 zebra_router_release_table(zvrf
, zvrf
->table_id
,
283 zvrf
->table
[afi
][safi
] = NULL
;
287 if (zvrf
->rnh_table
[afi
])
288 route_table_finish(zvrf
->rnh_table
[afi
]);
289 if (zvrf
->import_check_table
[afi
])
290 route_table_finish(zvrf
->import_check_table
[afi
]);
293 otable
= otable_pop(&zvrf
->other_tables
);
295 zebra_router_release_table(zvrf
, otable
->table_id
,
296 otable
->afi
, otable
->safi
);
297 XFREE(MTYPE_OTHER_TABLE
, otable
);
299 otable
= otable_pop(&zvrf
->other_tables
);
302 /* Cleanup EVPN states for vrf */
303 zebra_vxlan_vrf_delete(zvrf
);
305 list_delete_all_node(zvrf
->rid_all_sorted_list
);
306 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
308 otable_fini(&zvrf
->other_tables
);
309 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
315 static int zebra_vrf_update(struct vrf
*vrf
)
317 struct zebra_vrf
*zvrf
= vrf
->info
;
320 if (IS_ZEBRA_DEBUG_EVENT
)
321 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
323 zebra_vrf_add_update(zvrf
);
328 /* Return if this VRF has any FRR configuration or not.
329 * IMPORTANT: This function needs to be updated when additional configuration
330 * is added for a VRF.
332 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
341 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
342 * NOTE: Table-id is relevant on two modes:
343 * - case VRF backend is default : on default VRF only
344 * - case VRF backend is netns : on all VRFs
346 struct route_table
*zebra_vrf_lookup_table_with_table_id(afi_t afi
, safi_t safi
,
350 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
351 struct other_route_table ort
, *otable
;
356 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
359 if (table_id
== zvrf
->table_id
)
360 return zebra_vrf_table(afi
, safi
, vrf_id
);
364 ort
.table_id
= table_id
;
365 otable
= otable_find(&zvrf
->other_tables
, &ort
);
368 return otable
->table
;
373 struct route_table
*zebra_vrf_get_table_with_table_id(afi_t afi
, safi_t safi
,
377 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
378 struct other_route_table
*otable
;
379 struct route_table
*table
;
381 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
387 /* Create it as an `other` table */
388 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
390 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
393 otable
->table_id
= table_id
;
394 otable
->table
= table
;
395 otable_add(&zvrf
->other_tables
, otable
);
401 void zebra_rtable_node_cleanup(struct route_table
*table
,
402 struct route_node
*node
)
404 struct route_entry
*re
, *next
;
406 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
407 rib_unlink(node
, re
);
411 rib_dest_t
*dest
= node
->info
;
413 rnh_list_fini(&dest
->nht
);
414 XFREE(MTYPE_RIB_DEST
, node
->info
);
418 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
419 struct route_node
*node
)
422 zebra_free_rnh(node
->info
);
426 * Create a routing table for the specific AFI/SAFI in the given VRF.
428 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
431 struct route_node
*rn
;
434 assert(!zvrf
->table
[afi
][safi
]);
436 zvrf
->table
[afi
][safi
] =
437 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
439 memset(&p
, 0, sizeof(p
));
440 p
.family
= afi2family(afi
);
442 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
443 zebra_rib_create_dest(rn
);
446 /* Allocate new zebra VRF. */
447 struct zebra_vrf
*zebra_vrf_alloc(void)
449 struct zebra_vrf
*zvrf
;
451 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
453 zebra_vxlan_init_tables(zvrf
);
454 zebra_mpls_init_tables(zvrf
);
456 zvrf
->table_id
= RT_TABLE_MAIN
;
457 /* by default table ID is default one */
461 /* Lookup VRF by identifier. */
462 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
464 return vrf_info_lookup(vrf_id
);
467 /* Lookup VRF by name. */
468 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
473 name
= VRF_DEFAULT_NAME
;
475 vrf
= vrf_lookup_by_name(name
);
477 return ((struct zebra_vrf
*)vrf
->info
);
482 /* Lookup the routing table in an enabled VRF. */
483 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
485 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
490 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
493 return zvrf
->table
[afi
][safi
];
496 static int vrf_config_write(struct vty
*vty
)
499 struct zebra_vrf
*zvrf
;
501 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
507 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
509 vty_out(vty
, "vni %u%s\n", zvrf
->l3vni
,
510 is_l3vni_for_prefix_routes_only(
512 ? " prefix-routes-only"
514 if (zvrf
->zebra_rnh_ip_default_route
)
515 vty_out(vty
, "ip nht resolve-via-default\n");
517 if (zvrf
->zebra_rnh_ipv6_default_route
)
518 vty_out(vty
, "ipv6 nht resolve-via-default\n");
520 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
522 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
523 is_l3vni_for_prefix_routes_only(
525 ? " prefix-routes-only"
527 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
528 if (zvrf
->zebra_rnh_ip_default_route
)
529 vty_out(vty
, " ip nht resolve-via-default\n");
531 if (zvrf
->zebra_rnh_ipv6_default_route
)
532 vty_out(vty
, " ipv6 nht resolve-via-default\n");
536 zebra_routemap_config_write_protocol(vty
, zvrf
);
538 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
539 vty_endframe(vty
, " exit-vrf\n!\n");
546 /* Zebra VRF initialization. */
547 void zebra_vrf_init(void)
549 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
550 zebra_vrf_delete
, zebra_vrf_update
);
552 vrf_cmd_init(vrf_config_write
, &zserv_privs
);