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/debug.h"
32 #include "zebra/zserv.h"
33 #include "zebra/rib.h"
34 #include "zebra/zebra_vrf.h"
35 #include "zebra/zebra_rnh.h"
36 #include "zebra/router-id.h"
37 #include "zebra/zebra_memory.h"
38 #include "zebra/zebra_static.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"
44 extern struct zebra_t zebrad
;
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(zebrad
.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(zebrad
.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 /* Inform clients that the VRF is now active. This is an
119 * add for the clients.
122 zebra_vrf_add_update(zvrf
);
123 /* Allocate tables */
124 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
125 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
126 zebra_vrf_table_create(zvrf
, afi
, safi
);
128 table
= route_table_init();
129 table
->cleanup
= zebra_rnhtable_node_cleanup
;
130 zvrf
->rnh_table
[afi
] = table
;
132 table
= route_table_init();
133 table
->cleanup
= zebra_rnhtable_node_cleanup
;
134 zvrf
->import_check_table
[afi
] = table
;
137 static_fixup_vrf_ids(zvrf
);
140 * We may have static routes that are now possible to
141 * insert into the appropriate tables
143 static_config_install_delayed_routes(zvrf
);
145 /* Kick off any VxLAN-EVPN processing. */
146 zebra_vxlan_vrf_enable(zvrf
);
151 /* Callback upon disabling a VRF. */
152 static int zebra_vrf_disable(struct vrf
*vrf
)
154 struct zebra_vrf
*zvrf
= vrf
->info
;
155 struct route_table
*table
;
156 struct interface
*ifp
;
162 if (IS_ZEBRA_DEBUG_EVENT
)
163 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
166 static_cleanup_vrf_ids(zvrf
);
168 /* Stop any VxLAN-EVPN processing. */
169 zebra_vxlan_vrf_disable(zvrf
);
171 /* Inform clients that the VRF is now inactive. This is a
172 * delete for the clients.
174 zebra_vrf_delete_update(zvrf
);
176 /* If asked to retain routes, there's nothing more to do. */
177 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
180 /* Remove all routes. */
181 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
182 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
183 rib_close_table(zvrf
->table
[afi
][safi
]);
186 /* Cleanup Vxlan, MPLS and PW tables. */
187 zebra_vxlan_cleanup_tables(zvrf
);
188 zebra_mpls_cleanup_tables(zvrf
);
191 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
193 FOR_ALL_INTERFACES (vrf
, ifp
)
194 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
196 /* clean-up work queues */
197 for (i
= 0; i
< MQ_SIZE
; i
++) {
198 struct listnode
*lnode
, *nnode
;
199 struct route_node
*rnode
;
202 for (ALL_LIST_ELEMENTS(zebrad
.mq
->subq
[i
], lnode
, nnode
,
204 dest
= rib_dest_from_rnode(rnode
);
205 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
206 route_unlock_node(rnode
);
207 list_delete_node(zebrad
.mq
->subq
[i
], lnode
);
213 /* Cleanup (free) routing tables and NHT tables. */
214 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
217 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
218 table
= zvrf
->table
[afi
][safi
];
219 table_info
= table
->info
;
220 route_table_finish(table
);
221 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
222 zvrf
->table
[afi
][safi
] = NULL
;
225 route_table_finish(zvrf
->rnh_table
[afi
]);
226 zvrf
->rnh_table
[afi
] = NULL
;
227 route_table_finish(zvrf
->import_check_table
[afi
]);
228 zvrf
->import_check_table
[afi
] = NULL
;
234 static int zebra_vrf_delete(struct vrf
*vrf
)
236 struct zebra_vrf
*zvrf
= vrf
->info
;
237 struct route_table
*table
;
243 if (IS_ZEBRA_DEBUG_EVENT
)
244 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
247 /* clean-up work queues */
248 for (i
= 0; i
< MQ_SIZE
; i
++) {
249 struct listnode
*lnode
, *nnode
;
250 struct route_node
*rnode
;
253 for (ALL_LIST_ELEMENTS(zebrad
.mq
->subq
[i
], lnode
, nnode
,
255 dest
= rib_dest_from_rnode(rnode
);
256 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
257 route_unlock_node(rnode
);
258 list_delete_node(zebrad
.mq
->subq
[i
], lnode
);
264 /* Free Vxlan and MPLS. */
265 zebra_vxlan_close_tables(zvrf
);
266 zebra_mpls_close_tables(zvrf
);
268 /* release allocated memory */
269 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
272 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
273 table
= zvrf
->table
[afi
][safi
];
275 table_info
= table
->info
;
276 route_table_finish(table
);
277 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
280 table
= zvrf
->stable
[afi
][safi
];
281 route_table_finish(table
);
284 route_table_finish(zvrf
->rnh_table
[afi
]);
285 route_table_finish(zvrf
->import_check_table
[afi
]);
288 /* Cleanup EVPN states for vrf */
289 zebra_vxlan_vrf_delete(zvrf
);
291 list_delete_all_node(zvrf
->rid_all_sorted_list
);
292 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
293 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
299 /* Return if this VRF has any FRR configuration or not.
300 * IMPORTANT: This function needs to be updated when additional configuration
301 * is added for a VRF.
303 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
307 struct route_table
*stable
;
309 /* NOTE: This is a don't care for the default VRF, but we go through
310 * the motions to keep things consistent.
312 /* Any static routes? */
313 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
314 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
315 stable
= zvrf
->stable
[afi
][safi
];
318 if (route_table_count(stable
))
330 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
331 * NOTE: Table-id is relevant only in the Default VRF.
333 struct route_table
*zebra_vrf_table_with_table_id(afi_t afi
, safi_t safi
,
337 struct route_table
*table
= NULL
;
339 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
342 if (vrf_id
== VRF_DEFAULT
) {
343 if (table_id
== RT_TABLE_MAIN
344 || table_id
== zebrad
.rtm_table_default
)
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 XFREE(MTYPE_RIB_DEST
, node
->info
);
368 static void zebra_stable_node_cleanup(struct route_table
*table
,
369 struct route_node
*node
)
371 struct static_route
*si
, *next
;
374 for (si
= node
->info
; si
; si
= next
) {
376 XFREE(MTYPE_STATIC_ROUTE
, si
);
380 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
381 struct route_node
*node
)
384 zebra_free_rnh(node
->info
);
388 * Create a routing table for the specific AFI/SAFI in the given VRF.
390 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
393 rib_table_info_t
*info
;
394 struct route_table
*table
;
396 assert(!zvrf
->table
[afi
][safi
]);
399 table
= srcdest_table_init();
401 table
= route_table_init();
402 table
->cleanup
= zebra_rtable_node_cleanup
;
403 zvrf
->table
[afi
][safi
] = table
;
405 info
= XCALLOC(MTYPE_RIB_TABLE_INFO
, sizeof(*info
));
412 /* Allocate new zebra VRF. */
413 struct zebra_vrf
*zebra_vrf_alloc(void)
415 struct zebra_vrf
*zvrf
;
418 struct route_table
*table
;
420 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
422 /* Allocate table for static route configuration. */
423 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
424 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
426 table
= srcdest_table_init();
428 table
= route_table_init();
429 table
->cleanup
= zebra_stable_node_cleanup
;
430 zvrf
->stable
[afi
][safi
] = table
;
434 zebra_vxlan_init_tables(zvrf
);
435 zebra_mpls_init_tables(zvrf
);
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 /* Lookup the static routing table in a VRF. */
477 struct route_table
*zebra_vrf_static_table(afi_t afi
, safi_t safi
,
478 struct zebra_vrf
*zvrf
)
483 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
486 return zvrf
->stable
[afi
][safi
];
489 struct route_table
*zebra_vrf_other_route_table(afi_t afi
, uint32_t table_id
,
492 struct zebra_vrf
*zvrf
;
493 struct zebra_ns
*zns
;
495 zvrf
= vrf_info_lookup(vrf_id
);
504 if ((vrf_id
== VRF_DEFAULT
) && (table_id
!= RT_TABLE_MAIN
)
505 && (table_id
!= zebrad
.rtm_table_default
)) {
506 return zebra_ns_get_table(zns
, zvrf
, table_id
, afi
);
509 return zvrf
->table
[afi
][SAFI_UNICAST
];
512 static int vrf_config_write(struct vty
*vty
)
515 struct zebra_vrf
*zvrf
;
517 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
523 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
525 vty_out(vty
, "vni %u\n", zvrf
->l3vni
);
528 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
530 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
531 is_l3vni_for_prefix_routes_only(
533 ? " prefix-routes-only"
535 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
539 static_config(vty
, zvrf
, AFI_IP
, SAFI_UNICAST
, "ip route");
540 static_config(vty
, zvrf
, AFI_IP
, SAFI_MULTICAST
, "ip mroute");
541 static_config(vty
, zvrf
, AFI_IP6
, SAFI_UNICAST
, "ipv6 route");
543 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
544 vty_endframe(vty
, " exit-vrf\n!\n");
549 /* Zebra VRF initialization. */
550 void zebra_vrf_init(void)
552 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
555 vrf_cmd_init(vrf_config_write
, &zserv_privs
);