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 /* VRF information update. */
52 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
54 struct listnode
*node
, *nnode
;
57 if (IS_ZEBRA_DEBUG_EVENT
)
58 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
60 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
61 zsend_vrf_add(client
, zvrf
);
64 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
66 struct listnode
*node
, *nnode
;
69 if (IS_ZEBRA_DEBUG_EVENT
)
70 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
72 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
73 zsend_vrf_delete(client
, zvrf
);
76 void zebra_vrf_update_all(struct zserv
*client
)
80 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
81 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
82 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
86 /* Callback upon creating a new VRF. */
87 static int zebra_vrf_new(struct vrf
*vrf
)
89 struct zebra_vrf
*zvrf
;
91 if (IS_ZEBRA_DEBUG_EVENT
)
92 zlog_info("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
94 zvrf
= zebra_vrf_alloc();
101 /* Callback upon enabling a VRF. */
102 static int zebra_vrf_enable(struct vrf
*vrf
)
104 struct zebra_vrf
*zvrf
= vrf
->info
;
105 struct route_table
*table
;
110 if (IS_ZEBRA_DEBUG_EVENT
)
111 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
114 if (vrf_is_backend_netns())
115 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
117 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
118 #if defined(HAVE_RTADV)
122 /* Inform clients that the VRF is now active. This is an
123 * add for the clients.
126 zebra_vrf_add_update(zvrf
);
127 /* Allocate tables */
128 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
129 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
130 zebra_vrf_table_create(zvrf
, afi
, safi
);
132 table
= route_table_init();
133 table
->cleanup
= zebra_rnhtable_node_cleanup
;
134 zvrf
->rnh_table
[afi
] = table
;
136 table
= route_table_init();
137 table
->cleanup
= zebra_rnhtable_node_cleanup
;
138 zvrf
->import_check_table
[afi
] = table
;
141 /* Kick off any VxLAN-EVPN processing. */
142 zebra_vxlan_vrf_enable(zvrf
);
147 /* Callback upon disabling a VRF. */
148 static int zebra_vrf_disable(struct vrf
*vrf
)
150 struct zebra_vrf
*zvrf
= vrf
->info
;
151 struct interface
*ifp
;
157 if (IS_ZEBRA_DEBUG_EVENT
)
158 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
161 /* Stop any VxLAN-EVPN processing. */
162 zebra_vxlan_vrf_disable(zvrf
);
164 #if defined(HAVE_RTADV)
165 rtadv_terminate(zvrf
);
168 /* Inform clients that the VRF is now inactive. This is a
169 * delete for the clients.
171 zebra_vrf_delete_update(zvrf
);
173 /* If asked to retain routes, there's nothing more to do. */
174 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
177 /* Remove all routes. */
178 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
179 route_table_finish(zvrf
->rnh_table
[afi
]);
180 zvrf
->rnh_table
[afi
] = NULL
;
181 route_table_finish(zvrf
->import_check_table
[afi
]);
182 zvrf
->import_check_table
[afi
] = NULL
;
184 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
185 rib_close_table(zvrf
->table
[afi
][safi
]);
188 /* Cleanup Vxlan, MPLS and PW tables. */
189 zebra_vxlan_cleanup_tables(zvrf
);
190 zebra_mpls_cleanup_tables(zvrf
);
193 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
195 FOR_ALL_INTERFACES (vrf
, ifp
)
196 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
198 /* clean-up work queues */
199 for (i
= 0; i
< MQ_SIZE
; i
++) {
200 struct listnode
*lnode
, *nnode
;
201 struct route_node
*rnode
;
204 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
206 dest
= rib_dest_from_rnode(rnode
);
207 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
208 route_unlock_node(rnode
);
209 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
215 /* Cleanup (free) routing tables and NHT tables. */
216 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
218 * Set the table pointer to NULL as that
219 * we no-longer need a copy of it, nor do we
220 * own this data, the zebra_router structure
221 * owns these tables. Once we've cleaned up the
222 * table, see rib_close_table above
223 * we no-longer need this pointer.
225 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
226 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
228 zvrf
->table
[afi
][safi
] = NULL
;
235 static int zebra_vrf_delete(struct vrf
*vrf
)
237 struct zebra_vrf
*zvrf
= vrf
->info
;
238 struct route_table
*table
;
244 if (IS_ZEBRA_DEBUG_EVENT
)
245 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
248 /* clean-up work queues */
249 for (i
= 0; i
< MQ_SIZE
; i
++) {
250 struct listnode
*lnode
, *nnode
;
251 struct route_node
*rnode
;
254 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
256 dest
= rib_dest_from_rnode(rnode
);
257 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
258 route_unlock_node(rnode
);
259 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
265 /* Free Vxlan and MPLS. */
266 zebra_vxlan_close_tables(zvrf
);
267 zebra_mpls_close_tables(zvrf
);
269 /* release allocated memory */
270 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
271 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
272 table
= zvrf
->table
[afi
][safi
];
274 zebra_router_release_table(zvrf
, zvrf
->table_id
,
276 zvrf
->table
[afi
][safi
] = NULL
;
280 if (zvrf
->rnh_table
[afi
])
281 route_table_finish(zvrf
->rnh_table
[afi
]);
282 if (zvrf
->import_check_table
[afi
])
283 route_table_finish(zvrf
->import_check_table
[afi
]);
286 /* Cleanup EVPN states for vrf */
287 zebra_vxlan_vrf_delete(zvrf
);
289 list_delete_all_node(zvrf
->rid_all_sorted_list
);
290 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
291 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
297 static int zebra_vrf_update(struct vrf
*vrf
)
299 struct zebra_vrf
*zvrf
= vrf
->info
;
302 if (IS_ZEBRA_DEBUG_EVENT
)
303 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
305 zebra_vrf_add_update(zvrf
);
310 /* Return if this VRF has any FRR configuration or not.
311 * IMPORTANT: This function needs to be updated when additional configuration
312 * is added for a VRF.
314 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
323 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
324 * NOTE: Table-id is relevant on two modes:
325 * - case VRF backend is default : on default VRF only
326 * - case VRF backend is netns : on all VRFs
328 struct route_table
*zebra_vrf_table_with_table_id(afi_t afi
, safi_t safi
,
332 struct route_table
*table
= NULL
;
334 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
337 if (vrf_id
== VRF_DEFAULT
) {
338 if (table_id
== RT_TABLE_MAIN
)
339 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
341 table
= zebra_vrf_other_route_table(afi
, table_id
,
343 } else if (vrf_is_backend_netns()) {
344 if (table_id
== RT_TABLE_MAIN
)
345 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
347 table
= zebra_vrf_other_route_table(afi
, table_id
,
350 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
355 void zebra_rtable_node_cleanup(struct route_table
*table
,
356 struct route_node
*node
)
358 struct route_entry
*re
, *next
;
360 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
361 rib_unlink(node
, re
);
365 rib_dest_t
*dest
= node
->info
;
367 rnh_list_fini(&dest
->nht
);
368 XFREE(MTYPE_RIB_DEST
, node
->info
);
372 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
373 struct route_node
*node
)
376 zebra_free_rnh(node
->info
);
380 * Create a routing table for the specific AFI/SAFI in the given VRF.
382 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
385 struct route_node
*rn
;
388 assert(!zvrf
->table
[afi
][safi
]);
390 zvrf
->table
[afi
][safi
] =
391 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
393 memset(&p
, 0, sizeof(p
));
394 p
.family
= afi2family(afi
);
396 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
397 zebra_rib_create_dest(rn
);
400 /* Allocate new zebra VRF. */
401 struct zebra_vrf
*zebra_vrf_alloc(void)
403 struct zebra_vrf
*zvrf
;
405 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
407 zebra_vxlan_init_tables(zvrf
);
408 zebra_mpls_init_tables(zvrf
);
410 zvrf
->table_id
= RT_TABLE_MAIN
;
411 /* by default table ID is default one */
415 /* Lookup VRF by identifier. */
416 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
418 return vrf_info_lookup(vrf_id
);
421 /* Lookup VRF by name. */
422 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
427 name
= VRF_DEFAULT_NAME
;
429 vrf
= vrf_lookup_by_name(name
);
431 return ((struct zebra_vrf
*)vrf
->info
);
436 /* Lookup the routing table in an enabled VRF. */
437 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
439 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
444 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
447 return zvrf
->table
[afi
][safi
];
450 struct route_table
*zebra_vrf_other_route_table(afi_t afi
, uint32_t table_id
,
453 struct zebra_vrf
*zvrf
;
455 zvrf
= vrf_info_lookup(vrf_id
);
462 if (table_id
!= RT_TABLE_MAIN
) {
463 if (zvrf
->table_id
== RT_TABLE_MAIN
) {
464 /* this VRF use default table
465 * so in all cases, it does not use specific table
466 * so it is possible to configure tables in this VRF
468 return zebra_router_get_table(zvrf
, table_id
, afi
,
473 return zvrf
->table
[afi
][SAFI_UNICAST
];
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
);