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/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 /* Kick off any VxLAN-EVPN processing. */
138 zebra_vxlan_vrf_enable(zvrf
);
143 /* Callback upon disabling a VRF. */
144 static int zebra_vrf_disable(struct vrf
*vrf
)
146 struct zebra_vrf
*zvrf
= vrf
->info
;
147 struct route_table
*table
;
148 struct interface
*ifp
;
154 if (IS_ZEBRA_DEBUG_EVENT
)
155 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
158 static_cleanup_vrf_ids(zvrf
);
160 /* Stop any VxLAN-EVPN processing. */
161 zebra_vxlan_vrf_disable(zvrf
);
163 /* Inform clients that the VRF is now inactive. This is a
164 * delete for the clients.
166 zebra_vrf_delete_update(zvrf
);
168 /* If asked to retain routes, there's nothing more to do. */
169 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
172 /* Remove all routes. */
173 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
174 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
175 rib_close_table(zvrf
->table
[afi
][safi
]);
178 /* Cleanup Vxlan, MPLS and PW tables. */
179 zebra_vxlan_cleanup_tables(zvrf
);
180 zebra_mpls_cleanup_tables(zvrf
);
183 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
185 FOR_ALL_INTERFACES (vrf
, ifp
)
186 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
188 /* clean-up work queues */
189 for (i
= 0; i
< MQ_SIZE
; i
++) {
190 struct listnode
*lnode
, *nnode
;
191 struct route_node
*rnode
;
194 for (ALL_LIST_ELEMENTS(zebrad
.mq
->subq
[i
], lnode
, nnode
,
196 dest
= rib_dest_from_rnode(rnode
);
197 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
198 route_unlock_node(rnode
);
199 list_delete_node(zebrad
.mq
->subq
[i
], lnode
);
205 /* Cleanup (free) routing tables and NHT tables. */
206 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
209 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
210 table
= zvrf
->table
[afi
][safi
];
211 table_info
= table
->info
;
212 route_table_finish(table
);
213 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
214 zvrf
->table
[afi
][safi
] = NULL
;
217 route_table_finish(zvrf
->rnh_table
[afi
]);
218 zvrf
->rnh_table
[afi
] = NULL
;
219 route_table_finish(zvrf
->import_check_table
[afi
]);
220 zvrf
->import_check_table
[afi
] = NULL
;
226 static int zebra_vrf_delete(struct vrf
*vrf
)
228 struct zebra_vrf
*zvrf
= vrf
->info
;
229 struct route_table
*table
;
235 if (IS_ZEBRA_DEBUG_EVENT
)
236 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
239 /* clean-up work queues */
240 for (i
= 0; i
< MQ_SIZE
; i
++) {
241 struct listnode
*lnode
, *nnode
;
242 struct route_node
*rnode
;
245 for (ALL_LIST_ELEMENTS(zebrad
.mq
->subq
[i
], lnode
, nnode
,
247 dest
= rib_dest_from_rnode(rnode
);
248 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
249 route_unlock_node(rnode
);
250 list_delete_node(zebrad
.mq
->subq
[i
], lnode
);
256 /* Free Vxlan and MPLS. */
257 zebra_vxlan_close_tables(zvrf
);
258 zebra_mpls_close_tables(zvrf
);
260 /* release allocated memory */
261 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
264 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
265 table
= zvrf
->table
[afi
][safi
];
267 table_info
= table
->info
;
268 route_table_finish(table
);
269 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
272 table
= zvrf
->stable
[afi
][safi
];
273 route_table_finish(table
);
276 route_table_finish(zvrf
->rnh_table
[afi
]);
277 route_table_finish(zvrf
->import_check_table
[afi
]);
280 /* Cleanup EVPN states for vrf */
281 zebra_vxlan_vrf_delete(zvrf
);
283 list_delete_all_node(zvrf
->rid_all_sorted_list
);
284 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
285 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
291 /* Return if this VRF has any FRR configuration or not.
292 * IMPORTANT: This function needs to be updated when additional configuration
293 * is added for a VRF.
295 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
299 struct route_table
*stable
;
301 /* NOTE: This is a don't care for the default VRF, but we go through
302 * the motions to keep things consistent.
304 /* Any static routes? */
305 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
306 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
307 stable
= zvrf
->stable
[afi
][safi
];
310 if (route_table_count(stable
))
322 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
323 * NOTE: Table-id is relevant on two modes:
324 * - case VRF backend is default : on default VRF only
325 * - case VRF backend is netns : on all VRFs
327 struct route_table
*zebra_vrf_table_with_table_id(afi_t afi
, safi_t safi
,
331 struct route_table
*table
= NULL
;
333 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
336 if (vrf_id
== VRF_DEFAULT
) {
337 if (table_id
== RT_TABLE_MAIN
338 || table_id
== zebrad
.rtm_table_default
)
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_id
== zebrad
.rtm_table_default
)
346 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
348 table
= zebra_vrf_other_route_table(afi
, table_id
,
351 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
356 void zebra_rtable_node_cleanup(struct route_table
*table
,
357 struct route_node
*node
)
359 struct route_entry
*re
, *next
;
361 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
362 rib_unlink(node
, re
);
366 XFREE(MTYPE_RIB_DEST
, node
->info
);
369 static void zebra_stable_node_cleanup(struct route_table
*table
,
370 struct route_node
*node
)
372 struct static_route
*si
, *next
;
375 for (si
= node
->info
; si
; si
= next
) {
377 XFREE(MTYPE_STATIC_ROUTE
, si
);
381 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
382 struct route_node
*node
)
385 zebra_free_rnh(node
->info
);
389 * Create a routing table for the specific AFI/SAFI in the given VRF.
391 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
394 rib_table_info_t
*info
;
395 struct route_table
*table
;
397 assert(!zvrf
->table
[afi
][safi
]);
400 table
= srcdest_table_init();
402 table
= route_table_init();
403 table
->cleanup
= zebra_rtable_node_cleanup
;
404 zvrf
->table
[afi
][safi
] = table
;
406 info
= XCALLOC(MTYPE_RIB_TABLE_INFO
, sizeof(*info
));
413 /* Allocate new zebra VRF. */
414 struct zebra_vrf
*zebra_vrf_alloc(void)
416 struct zebra_vrf
*zvrf
;
419 struct route_table
*table
;
421 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
423 /* Allocate table for static route configuration. */
424 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
425 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
427 table
= srcdest_table_init();
429 table
= route_table_init();
430 table
->cleanup
= zebra_stable_node_cleanup
;
431 zvrf
->stable
[afi
][safi
] = table
;
435 zebra_vxlan_init_tables(zvrf
);
436 zebra_mpls_init_tables(zvrf
);
438 zvrf
->table_id
= RT_TABLE_MAIN
;
439 /* by default table ID is default one */
443 /* Lookup VRF by identifier. */
444 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
446 return vrf_info_lookup(vrf_id
);
449 /* Lookup VRF by name. */
450 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
455 name
= VRF_DEFAULT_NAME
;
457 vrf
= vrf_lookup_by_name(name
);
459 return ((struct zebra_vrf
*)vrf
->info
);
464 /* Lookup the routing table in an enabled VRF. */
465 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
467 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
472 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
475 return zvrf
->table
[afi
][safi
];
478 /* Lookup the static routing table in a VRF. */
479 struct route_table
*zebra_vrf_static_table(afi_t afi
, safi_t safi
,
480 struct zebra_vrf
*zvrf
)
485 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
488 return zvrf
->stable
[afi
][safi
];
491 struct route_table
*zebra_vrf_other_route_table(afi_t afi
, uint32_t table_id
,
494 struct zebra_vrf
*zvrf
;
495 struct zebra_ns
*zns
;
497 zvrf
= vrf_info_lookup(vrf_id
);
506 if ((table_id
!= RT_TABLE_MAIN
)
507 && (table_id
!= zebrad
.rtm_table_default
)) {
508 if (zvrf
->table_id
== RT_TABLE_MAIN
||
509 zvrf
->table_id
== zebrad
.rtm_table_default
) {
510 /* this VRF use default table
511 * so in all cases, it does not use specific table
512 * so it is possible to configure tables in this VRF
514 return zebra_ns_get_table(zns
, zvrf
, table_id
, afi
);
518 return zvrf
->table
[afi
][SAFI_UNICAST
];
521 static int vrf_config_write(struct vty
*vty
)
524 struct zebra_vrf
*zvrf
;
526 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
532 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
534 vty_out(vty
, "vni %u\n", zvrf
->l3vni
);
537 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
539 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
540 is_l3vni_for_prefix_routes_only(
542 ? " prefix-routes-only"
544 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
548 static_config(vty
, zvrf
, AFI_IP
, SAFI_UNICAST
, "ip route");
549 static_config(vty
, zvrf
, AFI_IP
, SAFI_MULTICAST
, "ip mroute");
550 static_config(vty
, zvrf
, AFI_IP6
, SAFI_UNICAST
, "ipv6 route");
552 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
553 vty_endframe(vty
, " exit-vrf\n!\n");
558 /* Zebra VRF initialization. */
559 void zebra_vrf_init(void)
561 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
564 vrf_cmd_init(vrf_config_write
, &zserv_privs
);