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/zapi_msg.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/interface.h"
39 #include "zebra/zebra_mpls.h"
40 #include "zebra/zebra_vxlan.h"
41 #include "zebra/zebra_netns_notify.h"
43 extern struct zebra_t zebrad
;
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 /* VRF information update. */
51 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
53 struct listnode
*node
, *nnode
;
56 if (IS_ZEBRA_DEBUG_EVENT
)
57 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
59 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
60 zsend_vrf_add(client
, zvrf
);
63 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
65 struct listnode
*node
, *nnode
;
68 if (IS_ZEBRA_DEBUG_EVENT
)
69 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
71 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
72 zsend_vrf_delete(client
, zvrf
);
75 void zebra_vrf_update_all(struct zserv
*client
)
79 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
80 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
81 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
85 /* Callback upon creating a new VRF. */
86 static int zebra_vrf_new(struct vrf
*vrf
)
88 struct zebra_vrf
*zvrf
;
90 if (IS_ZEBRA_DEBUG_EVENT
)
91 zlog_info("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
93 zvrf
= zebra_vrf_alloc();
100 /* Callback upon enabling a VRF. */
101 static int zebra_vrf_enable(struct vrf
*vrf
)
103 struct zebra_vrf
*zvrf
= vrf
->info
;
104 struct route_table
*table
;
109 if (IS_ZEBRA_DEBUG_EVENT
)
110 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
113 if (vrf_is_backend_netns())
114 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
116 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
117 /* Inform clients that the VRF is now active. This is an
118 * add for the clients.
121 zebra_vrf_add_update(zvrf
);
122 /* Allocate tables */
123 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
124 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
125 zebra_vrf_table_create(zvrf
, afi
, safi
);
127 table
= route_table_init();
128 table
->cleanup
= zebra_rnhtable_node_cleanup
;
129 zvrf
->rnh_table
[afi
] = table
;
131 table
= route_table_init();
132 table
->cleanup
= zebra_rnhtable_node_cleanup
;
133 zvrf
->import_check_table
[afi
] = table
;
136 /* Kick off any VxLAN-EVPN processing. */
137 zebra_vxlan_vrf_enable(zvrf
);
142 /* Callback upon disabling a VRF. */
143 static int zebra_vrf_disable(struct vrf
*vrf
)
145 struct zebra_vrf
*zvrf
= vrf
->info
;
146 struct route_table
*table
;
147 struct interface
*ifp
;
153 if (IS_ZEBRA_DEBUG_EVENT
)
154 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
157 /* Stop any VxLAN-EVPN processing. */
158 zebra_vxlan_vrf_disable(zvrf
);
160 /* Inform clients that the VRF is now inactive. This is a
161 * delete for the clients.
163 zebra_vrf_delete_update(zvrf
);
165 /* If asked to retain routes, there's nothing more to do. */
166 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
169 /* Remove all routes. */
170 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
171 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
172 rib_close_table(zvrf
->table
[afi
][safi
]);
175 /* Cleanup Vxlan, MPLS and PW tables. */
176 zebra_vxlan_cleanup_tables(zvrf
);
177 zebra_mpls_cleanup_tables(zvrf
);
180 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
182 FOR_ALL_INTERFACES (vrf
, ifp
)
183 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
185 /* clean-up work queues */
186 for (i
= 0; i
< MQ_SIZE
; i
++) {
187 struct listnode
*lnode
, *nnode
;
188 struct route_node
*rnode
;
191 for (ALL_LIST_ELEMENTS(zebrad
.mq
->subq
[i
], lnode
, nnode
,
193 dest
= rib_dest_from_rnode(rnode
);
194 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
195 route_unlock_node(rnode
);
196 list_delete_node(zebrad
.mq
->subq
[i
], lnode
);
202 /* Cleanup (free) routing tables and NHT tables. */
203 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
206 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
207 table
= zvrf
->table
[afi
][safi
];
208 table_info
= table
->info
;
209 route_table_finish(table
);
210 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
211 zvrf
->table
[afi
][safi
] = NULL
;
214 route_table_finish(zvrf
->rnh_table
[afi
]);
215 zvrf
->rnh_table
[afi
] = NULL
;
216 route_table_finish(zvrf
->import_check_table
[afi
]);
217 zvrf
->import_check_table
[afi
] = NULL
;
223 static int zebra_vrf_delete(struct vrf
*vrf
)
225 struct zebra_vrf
*zvrf
= vrf
->info
;
226 struct route_table
*table
;
232 if (IS_ZEBRA_DEBUG_EVENT
)
233 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
236 /* clean-up work queues */
237 for (i
= 0; i
< MQ_SIZE
; i
++) {
238 struct listnode
*lnode
, *nnode
;
239 struct route_node
*rnode
;
242 for (ALL_LIST_ELEMENTS(zebrad
.mq
->subq
[i
], lnode
, nnode
,
244 dest
= rib_dest_from_rnode(rnode
);
245 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
246 route_unlock_node(rnode
);
247 list_delete_node(zebrad
.mq
->subq
[i
], lnode
);
253 /* Free Vxlan and MPLS. */
254 zebra_vxlan_close_tables(zvrf
);
255 zebra_mpls_close_tables(zvrf
);
257 /* release allocated memory */
258 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
261 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
262 table
= zvrf
->table
[afi
][safi
];
264 table_info
= table
->info
;
265 route_table_finish(table
);
266 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
270 route_table_finish(zvrf
->rnh_table
[afi
]);
271 route_table_finish(zvrf
->import_check_table
[afi
]);
274 /* Cleanup EVPN states for vrf */
275 zebra_vxlan_vrf_delete(zvrf
);
277 list_delete_all_node(zvrf
->rid_all_sorted_list
);
278 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
279 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
285 static int zebra_vrf_update(struct vrf
*vrf
)
287 struct zebra_vrf
*zvrf
= vrf
->info
;
290 if (IS_ZEBRA_DEBUG_EVENT
)
291 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
293 zebra_vrf_add_update(zvrf
);
298 /* Return if this VRF has any FRR configuration or not.
299 * IMPORTANT: This function needs to be updated when additional configuration
300 * is added for a VRF.
302 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
311 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
312 * NOTE: Table-id is relevant on two modes:
313 * - case VRF backend is default : on default VRF only
314 * - case VRF backend is netns : on all VRFs
316 struct route_table
*zebra_vrf_table_with_table_id(afi_t afi
, safi_t safi
,
320 struct route_table
*table
= NULL
;
322 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
325 if (vrf_id
== VRF_DEFAULT
) {
326 if (table_id
== RT_TABLE_MAIN
327 || table_id
== zebrad
.rtm_table_default
)
328 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
330 table
= zebra_vrf_other_route_table(afi
, table_id
,
332 } else if (vrf_is_backend_netns()) {
333 if (table_id
== RT_TABLE_MAIN
334 || table_id
== zebrad
.rtm_table_default
)
335 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
337 table
= zebra_vrf_other_route_table(afi
, table_id
,
340 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
345 void zebra_rtable_node_cleanup(struct route_table
*table
,
346 struct route_node
*node
)
348 struct route_entry
*re
, *next
;
350 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
351 rib_unlink(node
, re
);
355 XFREE(MTYPE_RIB_DEST
, node
->info
);
358 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
359 struct route_node
*node
)
362 zebra_free_rnh(node
->info
);
366 * Create a routing table for the specific AFI/SAFI in the given VRF.
368 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
371 rib_table_info_t
*info
;
372 struct route_table
*table
;
374 assert(!zvrf
->table
[afi
][safi
]);
377 table
= srcdest_table_init();
379 table
= route_table_init();
380 table
->cleanup
= zebra_rtable_node_cleanup
;
381 zvrf
->table
[afi
][safi
] = table
;
383 info
= XCALLOC(MTYPE_RIB_TABLE_INFO
, sizeof(*info
));
390 /* Allocate new zebra VRF. */
391 struct zebra_vrf
*zebra_vrf_alloc(void)
393 struct zebra_vrf
*zvrf
;
395 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
397 zebra_vxlan_init_tables(zvrf
);
398 zebra_mpls_init_tables(zvrf
);
400 zvrf
->table_id
= RT_TABLE_MAIN
;
401 /* by default table ID is default one */
405 /* Lookup VRF by identifier. */
406 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
408 return vrf_info_lookup(vrf_id
);
411 /* Lookup VRF by name. */
412 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
417 name
= VRF_DEFAULT_NAME
;
419 vrf
= vrf_lookup_by_name(name
);
421 return ((struct zebra_vrf
*)vrf
->info
);
426 /* Lookup the routing table in an enabled VRF. */
427 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
429 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
434 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
437 return zvrf
->table
[afi
][safi
];
440 struct route_table
*zebra_vrf_other_route_table(afi_t afi
, uint32_t table_id
,
443 struct zebra_vrf
*zvrf
;
444 struct zebra_ns
*zns
;
446 zvrf
= vrf_info_lookup(vrf_id
);
455 if ((table_id
!= RT_TABLE_MAIN
)
456 && (table_id
!= zebrad
.rtm_table_default
)) {
457 if (zvrf
->table_id
== RT_TABLE_MAIN
||
458 zvrf
->table_id
== zebrad
.rtm_table_default
) {
459 /* this VRF use default table
460 * so in all cases, it does not use specific table
461 * so it is possible to configure tables in this VRF
463 return zebra_ns_get_table(zns
, zvrf
, table_id
, afi
);
467 return zvrf
->table
[afi
][SAFI_UNICAST
];
470 static int vrf_config_write(struct vty
*vty
)
473 struct zebra_vrf
*zvrf
;
475 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
481 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
483 vty_out(vty
, "vni %u\n", zvrf
->l3vni
);
486 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
488 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
489 is_l3vni_for_prefix_routes_only(
491 ? " prefix-routes-only"
493 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
497 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
498 vty_endframe(vty
, " exit-vrf\n!\n");
503 /* Zebra VRF initialization. */
504 void zebra_vrf_init(void)
506 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
507 zebra_vrf_delete
, zebra_vrf_update
);
509 vrf_cmd_init(vrf_config_write
, &zserv_privs
);