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/debug.h"
33 #include "zebra/zapi_msg.h"
34 #include "zebra/rib.h"
35 #include "zebra/zebra_vrf.h"
36 #include "zebra/zebra_rnh.h"
37 #include "zebra/router-id.h"
38 #include "zebra/zebra_memory.h"
39 #include "zebra/interface.h"
40 #include "zebra/zebra_mpls.h"
41 #include "zebra/zebra_vxlan.h"
42 #include "zebra/zebra_netns_notify.h"
43 #include "zebra/zebra_routemap.h"
45 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
47 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
48 struct route_node
*node
);
50 DEFINE_MTYPE_STATIC(ZEBRA
, OTHER_TABLE
, "Other Table");
52 /* VRF information update. */
53 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
55 struct listnode
*node
, *nnode
;
58 if (IS_ZEBRA_DEBUG_EVENT
)
59 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
61 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
62 zsend_vrf_add(client
, zvrf
);
65 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
67 struct listnode
*node
, *nnode
;
70 if (IS_ZEBRA_DEBUG_EVENT
)
71 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
73 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
74 zsend_vrf_delete(client
, zvrf
);
77 void zebra_vrf_update_all(struct zserv
*client
)
81 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
82 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
83 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
87 /* Callback upon creating a new VRF. */
88 static int zebra_vrf_new(struct vrf
*vrf
)
90 struct zebra_vrf
*zvrf
;
92 if (IS_ZEBRA_DEBUG_EVENT
)
93 zlog_info("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
95 zvrf
= zebra_vrf_alloc();
99 otable_init(&zvrf
->other_tables
);
101 router_id_init(zvrf
);
105 /* Callback upon enabling a VRF. */
106 static int zebra_vrf_enable(struct vrf
*vrf
)
108 struct zebra_vrf
*zvrf
= vrf
->info
;
109 struct route_table
*table
;
114 if (IS_ZEBRA_DEBUG_EVENT
)
115 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
118 if (vrf_is_backend_netns())
119 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
121 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
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 /* Inform clients that the VRF is now inactive. This is a
165 * delete for the clients.
167 zebra_vrf_delete_update(zvrf
);
169 /* If asked to retain routes, there's nothing more to do. */
170 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
173 /* Remove all routes. */
174 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
175 route_table_finish(zvrf
->rnh_table
[afi
]);
176 zvrf
->rnh_table
[afi
] = NULL
;
177 route_table_finish(zvrf
->import_check_table
[afi
]);
178 zvrf
->import_check_table
[afi
] = NULL
;
180 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
181 rib_close_table(zvrf
->table
[afi
][safi
]);
184 /* Cleanup Vxlan, MPLS and PW tables. */
185 zebra_vxlan_cleanup_tables(zvrf
);
186 zebra_mpls_cleanup_tables(zvrf
);
189 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
191 FOR_ALL_INTERFACES (vrf
, ifp
)
192 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
194 /* clean-up work queues */
195 for (i
= 0; i
< MQ_SIZE
; i
++) {
196 struct listnode
*lnode
, *nnode
;
197 struct route_node
*rnode
;
200 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
202 dest
= rib_dest_from_rnode(rnode
);
203 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
204 route_unlock_node(rnode
);
205 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
211 /* Cleanup (free) routing tables and NHT tables. */
212 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
214 * Set the table pointer to NULL as that
215 * we no-longer need a copy of it, nor do we
216 * own this data, the zebra_router structure
217 * owns these tables. Once we've cleaned up the
218 * table, see rib_close_table above
219 * we no-longer need this pointer.
221 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
222 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
224 zvrf
->table
[afi
][safi
] = NULL
;
231 static int zebra_vrf_delete(struct vrf
*vrf
)
233 struct zebra_vrf
*zvrf
= vrf
->info
;
234 struct other_route_table
*otable
;
235 struct route_table
*table
;
241 if (IS_ZEBRA_DEBUG_EVENT
)
242 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
245 /* clean-up work queues */
246 for (i
= 0; i
< MQ_SIZE
; i
++) {
247 struct listnode
*lnode
, *nnode
;
248 struct route_node
*rnode
;
251 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
253 dest
= rib_dest_from_rnode(rnode
);
254 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
255 route_unlock_node(rnode
);
256 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
262 /* Free Vxlan and MPLS. */
263 zebra_vxlan_close_tables(zvrf
);
264 zebra_mpls_close_tables(zvrf
);
266 /* release allocated memory */
267 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
268 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
269 table
= zvrf
->table
[afi
][safi
];
271 zebra_router_release_table(zvrf
, zvrf
->table_id
,
273 zvrf
->table
[afi
][safi
] = NULL
;
277 if (zvrf
->rnh_table
[afi
])
278 route_table_finish(zvrf
->rnh_table
[afi
]);
279 if (zvrf
->import_check_table
[afi
])
280 route_table_finish(zvrf
->import_check_table
[afi
]);
283 otable
= otable_pop(&zvrf
->other_tables
);
285 zebra_router_release_table(zvrf
, otable
->table_id
,
286 otable
->afi
, otable
->safi
);
287 XFREE(MTYPE_OTHER_TABLE
, otable
);
289 otable
= otable_pop(&zvrf
->other_tables
);
292 /* Cleanup EVPN states for vrf */
293 zebra_vxlan_vrf_delete(zvrf
);
295 list_delete_all_node(zvrf
->rid_all_sorted_list
);
296 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
298 otable_fini(&zvrf
->other_tables
);
299 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
305 static int zebra_vrf_update(struct vrf
*vrf
)
307 struct zebra_vrf
*zvrf
= vrf
->info
;
310 if (IS_ZEBRA_DEBUG_EVENT
)
311 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
313 zebra_vrf_add_update(zvrf
);
318 /* Return if this VRF has any FRR configuration or not.
319 * IMPORTANT: This function needs to be updated when additional configuration
320 * is added for a VRF.
322 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
331 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
332 * NOTE: Table-id is relevant on two modes:
333 * - case VRF backend is default : on default VRF only
334 * - case VRF backend is netns : on all VRFs
336 struct route_table
*zebra_vrf_table_with_table_id(afi_t afi
, safi_t safi
,
340 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
341 struct other_route_table ort
, *otable
;
342 struct route_table
*table
;
347 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
350 if (table_id
== zvrf
->table_id
)
351 return zebra_vrf_table(afi
, safi
, vrf_id
);
355 ort
.table_id
= table_id
;
356 otable
= otable_find(&zvrf
->other_tables
, &ort
);
358 return otable
->table
;
360 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
362 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
365 otable
->table_id
= table_id
;
366 otable
->table
= table
;
367 otable_add(&zvrf
->other_tables
, otable
);
372 void zebra_rtable_node_cleanup(struct route_table
*table
,
373 struct route_node
*node
)
375 struct route_entry
*re
, *next
;
377 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
378 rib_unlink(node
, re
);
382 rib_dest_t
*dest
= node
->info
;
384 rnh_list_fini(&dest
->nht
);
385 XFREE(MTYPE_RIB_DEST
, node
->info
);
389 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
390 struct route_node
*node
)
393 zebra_free_rnh(node
->info
);
397 * Create a routing table for the specific AFI/SAFI in the given VRF.
399 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
402 struct route_node
*rn
;
405 assert(!zvrf
->table
[afi
][safi
]);
407 zvrf
->table
[afi
][safi
] =
408 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
410 memset(&p
, 0, sizeof(p
));
411 p
.family
= afi2family(afi
);
413 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
414 zebra_rib_create_dest(rn
);
417 /* Allocate new zebra VRF. */
418 struct zebra_vrf
*zebra_vrf_alloc(void)
420 struct zebra_vrf
*zvrf
;
422 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
424 zebra_vxlan_init_tables(zvrf
);
425 zebra_mpls_init_tables(zvrf
);
427 zvrf
->table_id
= RT_TABLE_MAIN
;
428 /* by default table ID is default one */
432 /* Lookup VRF by identifier. */
433 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
435 return vrf_info_lookup(vrf_id
);
438 /* Lookup VRF by name. */
439 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
444 name
= VRF_DEFAULT_NAME
;
446 vrf
= vrf_lookup_by_name(name
);
448 return ((struct zebra_vrf
*)vrf
->info
);
453 /* Lookup the routing table in an enabled VRF. */
454 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
456 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
461 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
464 return zvrf
->table
[afi
][safi
];
467 static int vrf_config_write(struct vty
*vty
)
470 struct zebra_vrf
*zvrf
;
472 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
478 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
480 vty_out(vty
, "vni %u\n", zvrf
->l3vni
);
482 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
484 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
485 is_l3vni_for_prefix_routes_only(
487 ? " prefix-routes-only"
489 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
492 zebra_routemap_config_write_protocol(vty
, zvrf
);
494 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
495 vty_endframe(vty
, " exit-vrf\n!\n");
502 /* Zebra VRF initialization. */
503 void zebra_vrf_init(void)
505 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
506 zebra_vrf_delete
, zebra_vrf_update
);
508 vrf_cmd_init(vrf_config_write
, &zserv_privs
);