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
, OTHER_TABLE
, "Other Table");
53 /* VRF information update. */
54 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
56 struct listnode
*node
, *nnode
;
59 if (IS_ZEBRA_DEBUG_EVENT
)
60 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
62 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
63 zsend_vrf_add(client
, zvrf
);
66 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
68 struct listnode
*node
, *nnode
;
71 if (IS_ZEBRA_DEBUG_EVENT
)
72 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
74 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
75 zsend_vrf_delete(client
, zvrf
);
78 void zebra_vrf_update_all(struct zserv
*client
)
82 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
83 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
84 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
88 /* Callback upon creating a new VRF. */
89 static int zebra_vrf_new(struct vrf
*vrf
)
91 struct zebra_vrf
*zvrf
;
93 if (IS_ZEBRA_DEBUG_EVENT
)
94 zlog_info("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
96 zvrf
= zebra_vrf_alloc();
100 otable_init(&zvrf
->other_tables
);
102 router_id_init(zvrf
);
106 /* Callback upon enabling a VRF. */
107 static int zebra_vrf_enable(struct vrf
*vrf
)
109 struct zebra_vrf
*zvrf
= vrf
->info
;
110 struct route_table
*table
;
115 if (IS_ZEBRA_DEBUG_EVENT
)
116 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
119 if (vrf_is_backend_netns())
120 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
122 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
123 #if defined(HAVE_RTADV)
127 /* Inform clients that the VRF is now active. This is an
128 * add for the clients.
131 zebra_vrf_add_update(zvrf
);
132 /* Allocate tables */
133 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
134 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
135 zebra_vrf_table_create(zvrf
, afi
, safi
);
137 table
= route_table_init();
138 table
->cleanup
= zebra_rnhtable_node_cleanup
;
139 zvrf
->rnh_table
[afi
] = table
;
141 table
= route_table_init();
142 table
->cleanup
= zebra_rnhtable_node_cleanup
;
143 zvrf
->import_check_table
[afi
] = table
;
146 /* Kick off any VxLAN-EVPN processing. */
147 zebra_vxlan_vrf_enable(zvrf
);
152 /* Callback upon disabling a VRF. */
153 static int zebra_vrf_disable(struct vrf
*vrf
)
155 struct zebra_vrf
*zvrf
= vrf
->info
;
156 struct interface
*ifp
;
162 if (IS_ZEBRA_DEBUG_EVENT
)
163 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
166 /* Stop any VxLAN-EVPN processing. */
167 zebra_vxlan_vrf_disable(zvrf
);
169 #if defined(HAVE_RTADV)
170 rtadv_terminate(zvrf
);
173 /* Inform clients that the VRF is now inactive. This is a
174 * delete for the clients.
176 zebra_vrf_delete_update(zvrf
);
178 /* If asked to retain routes, there's nothing more to do. */
179 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
182 /* Remove all routes. */
183 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
184 route_table_finish(zvrf
->rnh_table
[afi
]);
185 zvrf
->rnh_table
[afi
] = NULL
;
186 route_table_finish(zvrf
->import_check_table
[afi
]);
187 zvrf
->import_check_table
[afi
] = NULL
;
189 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
190 rib_close_table(zvrf
->table
[afi
][safi
]);
193 /* Cleanup Vxlan, MPLS and PW tables. */
194 zebra_vxlan_cleanup_tables(zvrf
);
195 zebra_mpls_cleanup_tables(zvrf
);
198 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
200 FOR_ALL_INTERFACES (vrf
, ifp
)
201 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
203 /* clean-up work queues */
204 for (i
= 0; i
< MQ_SIZE
; i
++) {
205 struct listnode
*lnode
, *nnode
;
206 struct route_node
*rnode
;
209 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
211 dest
= rib_dest_from_rnode(rnode
);
212 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
213 route_unlock_node(rnode
);
214 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
220 /* Cleanup (free) routing tables and NHT tables. */
221 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
223 * Set the table pointer to NULL as that
224 * we no-longer need a copy of it, nor do we
225 * own this data, the zebra_router structure
226 * owns these tables. Once we've cleaned up the
227 * table, see rib_close_table above
228 * we no-longer need this pointer.
230 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
231 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
233 zvrf
->table
[afi
][safi
] = NULL
;
240 static int zebra_vrf_delete(struct vrf
*vrf
)
242 struct zebra_vrf
*zvrf
= vrf
->info
;
243 struct other_route_table
*otable
;
244 struct route_table
*table
;
250 if (IS_ZEBRA_DEBUG_EVENT
)
251 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
254 /* clean-up work queues */
255 for (i
= 0; i
< MQ_SIZE
; i
++) {
256 struct listnode
*lnode
, *nnode
;
257 struct route_node
*rnode
;
260 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
262 dest
= rib_dest_from_rnode(rnode
);
263 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
264 route_unlock_node(rnode
);
265 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
271 /* Free Vxlan and MPLS. */
272 zebra_vxlan_close_tables(zvrf
);
273 zebra_mpls_close_tables(zvrf
);
275 /* release allocated memory */
276 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
277 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
278 table
= zvrf
->table
[afi
][safi
];
280 zebra_router_release_table(zvrf
, zvrf
->table_id
,
282 zvrf
->table
[afi
][safi
] = NULL
;
286 if (zvrf
->rnh_table
[afi
])
287 route_table_finish(zvrf
->rnh_table
[afi
]);
288 if (zvrf
->import_check_table
[afi
])
289 route_table_finish(zvrf
->import_check_table
[afi
]);
292 otable
= otable_pop(&zvrf
->other_tables
);
294 zebra_router_release_table(zvrf
, otable
->table_id
,
295 otable
->afi
, otable
->safi
);
296 XFREE(MTYPE_OTHER_TABLE
, otable
);
298 otable
= otable_pop(&zvrf
->other_tables
);
301 /* Cleanup EVPN states for vrf */
302 zebra_vxlan_vrf_delete(zvrf
);
304 list_delete_all_node(zvrf
->rid_all_sorted_list
);
305 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
307 otable_fini(&zvrf
->other_tables
);
308 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
314 static int zebra_vrf_update(struct vrf
*vrf
)
316 struct zebra_vrf
*zvrf
= vrf
->info
;
319 if (IS_ZEBRA_DEBUG_EVENT
)
320 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
322 zebra_vrf_add_update(zvrf
);
327 /* Return if this VRF has any FRR configuration or not.
328 * IMPORTANT: This function needs to be updated when additional configuration
329 * is added for a VRF.
331 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
340 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
341 * NOTE: Table-id is relevant on two modes:
342 * - case VRF backend is default : on default VRF only
343 * - case VRF backend is netns : on all VRFs
345 struct route_table
*zebra_vrf_table_with_table_id(afi_t afi
, safi_t safi
,
349 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
350 struct other_route_table ort
, *otable
;
351 struct route_table
*table
;
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
);
367 return otable
->table
;
369 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
371 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
374 otable
->table_id
= table_id
;
375 otable
->table
= table
;
376 otable_add(&zvrf
->other_tables
, otable
);
381 void zebra_rtable_node_cleanup(struct route_table
*table
,
382 struct route_node
*node
)
384 struct route_entry
*re
, *next
;
386 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
387 rib_unlink(node
, re
);
391 rib_dest_t
*dest
= node
->info
;
393 rnh_list_fini(&dest
->nht
);
394 XFREE(MTYPE_RIB_DEST
, node
->info
);
398 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
399 struct route_node
*node
)
402 zebra_free_rnh(node
->info
);
406 * Create a routing table for the specific AFI/SAFI in the given VRF.
408 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
411 struct route_node
*rn
;
414 assert(!zvrf
->table
[afi
][safi
]);
416 zvrf
->table
[afi
][safi
] =
417 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
419 memset(&p
, 0, sizeof(p
));
420 p
.family
= afi2family(afi
);
422 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
423 zebra_rib_create_dest(rn
);
426 /* Allocate new zebra VRF. */
427 struct zebra_vrf
*zebra_vrf_alloc(void)
429 struct zebra_vrf
*zvrf
;
431 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
433 zebra_vxlan_init_tables(zvrf
);
434 zebra_mpls_init_tables(zvrf
);
436 zvrf
->table_id
= RT_TABLE_MAIN
;
437 /* by default table ID is default one */
441 /* Lookup VRF by identifier. */
442 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
444 return vrf_info_lookup(vrf_id
);
447 /* Lookup VRF by name. */
448 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
453 name
= VRF_DEFAULT_NAME
;
455 vrf
= vrf_lookup_by_name(name
);
457 return ((struct zebra_vrf
*)vrf
->info
);
462 /* Lookup the routing table in an enabled VRF. */
463 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
465 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
470 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
473 return zvrf
->table
[afi
][safi
];
476 static int vrf_config_write(struct vty
*vty
)
479 struct zebra_vrf
*zvrf
;
481 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
487 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
489 vty_out(vty
, "vni %u\n", zvrf
->l3vni
);
491 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
493 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
494 is_l3vni_for_prefix_routes_only(
496 ? " prefix-routes-only"
498 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
501 zebra_routemap_config_write_protocol(vty
, zvrf
);
503 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
504 vty_endframe(vty
, " exit-vrf\n!\n");
511 /* Zebra VRF initialization. */
512 void zebra_vrf_init(void)
514 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
515 zebra_vrf_delete
, zebra_vrf_update
);
517 vrf_cmd_init(vrf_config_write
, &zserv_privs
);