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/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
, ZEBRA_VRF
, "ZEBRA VRF");
51 DEFINE_MTYPE_STATIC(ZEBRA
, OTHER_TABLE
, "Other Table");
53 /* VRF information update. */
54 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
56 struct listnode
*node
, *nnode
;
59 if (IS_ZEBRA_DEBUG_EVENT
)
60 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
62 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
63 /* Do not send unsolicited messages to synchronous clients. */
64 if (client
->synchronous
)
67 zsend_vrf_add(client
, zvrf
);
71 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
73 struct listnode
*node
, *nnode
;
76 if (IS_ZEBRA_DEBUG_EVENT
)
77 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
79 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
80 /* Do not send unsolicited messages to synchronous clients. */
81 if (client
->synchronous
)
84 zsend_vrf_delete(client
, zvrf
);
88 void zebra_vrf_update_all(struct zserv
*client
)
92 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
93 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
94 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
98 /* Callback upon creating a new VRF. */
99 static int zebra_vrf_new(struct vrf
*vrf
)
101 struct zebra_vrf
*zvrf
;
103 if (IS_ZEBRA_DEBUG_EVENT
)
104 zlog_debug("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
106 zvrf
= zebra_vrf_alloc();
109 if (!vrf_is_backend_netns())
110 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
112 otable_init(&zvrf
->other_tables
);
114 router_id_init(zvrf
);
118 /* Callback upon enabling a VRF. */
119 static int zebra_vrf_enable(struct vrf
*vrf
)
121 struct zebra_vrf
*zvrf
= vrf
->info
;
122 struct route_table
*table
;
127 if (IS_ZEBRA_DEBUG_EVENT
)
128 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
131 if (vrf_is_backend_netns())
132 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
134 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
135 #if defined(HAVE_RTADV)
139 /* Inform clients that the VRF is now active. This is an
140 * add for the clients.
143 zebra_vrf_add_update(zvrf
);
144 /* Allocate tables */
145 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
146 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
147 zebra_vrf_table_create(zvrf
, afi
, safi
);
149 table
= route_table_init();
150 table
->cleanup
= zebra_rnhtable_node_cleanup
;
151 zvrf
->rnh_table
[afi
] = table
;
153 table
= route_table_init();
154 table
->cleanup
= zebra_rnhtable_node_cleanup
;
155 zvrf
->import_check_table
[afi
] = table
;
158 /* Kick off any VxLAN-EVPN processing. */
159 zebra_vxlan_vrf_enable(zvrf
);
164 /* Callback upon disabling a VRF. */
165 static int zebra_vrf_disable(struct vrf
*vrf
)
167 struct zebra_vrf
*zvrf
= vrf
->info
;
168 struct interface
*ifp
;
174 if (IS_ZEBRA_DEBUG_EVENT
)
175 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
178 /* Stop any VxLAN-EVPN processing. */
179 zebra_vxlan_vrf_disable(zvrf
);
181 #if defined(HAVE_RTADV)
182 rtadv_vrf_terminate(zvrf
);
185 /* Inform clients that the VRF is now inactive. This is a
186 * delete for the clients.
188 zebra_vrf_delete_update(zvrf
);
190 /* If asked to retain routes, there's nothing more to do. */
191 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
194 /* Remove all routes. */
195 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
196 route_table_finish(zvrf
->rnh_table
[afi
]);
197 zvrf
->rnh_table
[afi
] = NULL
;
198 route_table_finish(zvrf
->import_check_table
[afi
]);
199 zvrf
->import_check_table
[afi
] = NULL
;
201 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
202 rib_close_table(zvrf
->table
[afi
][safi
]);
205 /* Cleanup Vxlan, MPLS and PW tables. */
206 zebra_vxlan_cleanup_tables(zvrf
);
207 zebra_mpls_cleanup_tables(zvrf
);
210 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
212 FOR_ALL_INTERFACES (vrf
, ifp
)
213 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
215 /* clean-up work queues */
216 for (i
= 0; i
< MQ_SIZE
; i
++) {
217 struct listnode
*lnode
, *nnode
;
218 struct route_node
*rnode
;
221 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
223 dest
= rib_dest_from_rnode(rnode
);
224 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
225 route_unlock_node(rnode
);
226 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
232 /* Cleanup (free) routing tables and NHT tables. */
233 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
235 * Set the table pointer to NULL as that
236 * we no-longer need a copy of it, nor do we
237 * own this data, the zebra_router structure
238 * owns these tables. Once we've cleaned up the
239 * table, see rib_close_table above
240 * we no-longer need this pointer.
242 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
243 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
245 zvrf
->table
[afi
][safi
] = NULL
;
252 static int zebra_vrf_delete(struct vrf
*vrf
)
254 struct zebra_vrf
*zvrf
= vrf
->info
;
255 struct other_route_table
*otable
;
256 struct route_table
*table
;
262 if (IS_ZEBRA_DEBUG_EVENT
)
263 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
266 /* clean-up work queues */
267 for (i
= 0; i
< MQ_SIZE
; i
++) {
268 struct listnode
*lnode
, *nnode
;
269 struct route_node
*rnode
;
272 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
274 dest
= rib_dest_from_rnode(rnode
);
275 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
276 route_unlock_node(rnode
);
277 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
283 /* Free Vxlan and MPLS. */
284 zebra_vxlan_close_tables(zvrf
);
285 zebra_mpls_close_tables(zvrf
);
287 /* release allocated memory */
288 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
289 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
290 table
= zvrf
->table
[afi
][safi
];
292 zebra_router_release_table(zvrf
, zvrf
->table_id
,
294 zvrf
->table
[afi
][safi
] = NULL
;
298 if (zvrf
->rnh_table
[afi
])
299 route_table_finish(zvrf
->rnh_table
[afi
]);
300 if (zvrf
->import_check_table
[afi
])
301 route_table_finish(zvrf
->import_check_table
[afi
]);
304 otable
= otable_pop(&zvrf
->other_tables
);
306 zebra_router_release_table(zvrf
, otable
->table_id
,
307 otable
->afi
, otable
->safi
);
308 XFREE(MTYPE_OTHER_TABLE
, otable
);
310 otable
= otable_pop(&zvrf
->other_tables
);
313 /* Cleanup EVPN states for vrf */
314 zebra_vxlan_vrf_delete(zvrf
);
316 list_delete_all_node(zvrf
->rid_all_sorted_list
);
317 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
319 list_delete_all_node(zvrf
->rid6_all_sorted_list
);
320 list_delete_all_node(zvrf
->rid6_lo_sorted_list
);
322 otable_fini(&zvrf
->other_tables
);
323 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
329 static int zebra_vrf_update(struct vrf
*vrf
)
331 struct zebra_vrf
*zvrf
= vrf
->info
;
334 if (IS_ZEBRA_DEBUG_EVENT
)
335 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
337 zebra_vrf_add_update(zvrf
);
342 /* Return if this VRF has any FRR configuration or not.
343 * IMPORTANT: This function needs to be updated when additional configuration
344 * is added for a VRF.
346 int zebra_vrf_has_config(struct zebra_vrf
*zvrf
)
355 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
356 * NOTE: Table-id is relevant on two modes:
357 * - case VRF backend is default : on default VRF only
358 * - case VRF backend is netns : on all VRFs
360 struct route_table
*zebra_vrf_lookup_table_with_table_id(afi_t afi
, safi_t safi
,
364 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
365 struct other_route_table ort
, *otable
;
370 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
373 if (table_id
== zvrf
->table_id
)
374 return zebra_vrf_table(afi
, safi
, vrf_id
);
378 ort
.table_id
= table_id
;
379 otable
= otable_find(&zvrf
->other_tables
, &ort
);
382 return otable
->table
;
387 struct route_table
*zebra_vrf_get_table_with_table_id(afi_t afi
, safi_t safi
,
391 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
392 struct other_route_table
*otable
;
393 struct route_table
*table
;
395 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
401 /* Create it as an `other` table */
402 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
404 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
407 otable
->table_id
= table_id
;
408 otable
->table
= table
;
409 otable_add(&zvrf
->other_tables
, otable
);
415 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
416 struct route_node
*node
)
419 zebra_free_rnh(node
->info
);
423 * Create a routing table for the specific AFI/SAFI in the given VRF.
425 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
428 struct route_node
*rn
;
431 assert(!zvrf
->table
[afi
][safi
]);
433 zvrf
->table
[afi
][safi
] =
434 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
436 memset(&p
, 0, sizeof(p
));
437 p
.family
= afi2family(afi
);
439 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
440 zebra_rib_create_dest(rn
);
443 /* Allocate new zebra VRF. */
444 struct zebra_vrf
*zebra_vrf_alloc(void)
446 struct zebra_vrf
*zvrf
;
448 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
450 zebra_vxlan_init_tables(zvrf
);
451 zebra_mpls_init_tables(zvrf
);
453 zvrf
->table_id
= RT_TABLE_MAIN
;
454 /* by default table ID is default one */
458 /* Lookup VRF by identifier. */
459 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
461 return vrf_info_lookup(vrf_id
);
464 /* Lookup VRF by name. */
465 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
470 name
= VRF_DEFAULT_NAME
;
472 vrf
= vrf_lookup_by_name(name
);
474 return ((struct zebra_vrf
*)vrf
->info
);
479 /* Lookup the routing table in an enabled VRF. */
480 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
482 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
487 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
490 return zvrf
->table
[afi
][safi
];
493 static int vrf_config_write(struct vty
*vty
)
496 struct zebra_vrf
*zvrf
;
498 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
504 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
506 vty_out(vty
, "vni %u%s\n", zvrf
->l3vni
,
507 is_l3vni_for_prefix_routes_only(
509 ? " prefix-routes-only"
511 if (zvrf
->zebra_rnh_ip_default_route
)
512 vty_out(vty
, "ip nht resolve-via-default\n");
514 if (zvrf
->zebra_rnh_ipv6_default_route
)
515 vty_out(vty
, "ipv6 nht resolve-via-default\n");
517 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
519 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
520 is_l3vni_for_prefix_routes_only(
522 ? " prefix-routes-only"
524 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
525 if (zvrf
->zebra_rnh_ip_default_route
)
526 vty_out(vty
, " ip nht resolve-via-default\n");
528 if (zvrf
->zebra_rnh_ipv6_default_route
)
529 vty_out(vty
, " ipv6 nht resolve-via-default\n");
533 zebra_routemap_config_write_protocol(vty
, zvrf
);
534 router_id_write(vty
, zvrf
);
536 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
537 vty_endframe(vty
, " exit-vrf\n!\n");
544 /* Zebra VRF initialization. */
545 void zebra_vrf_init(void)
547 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
548 zebra_vrf_delete
, zebra_vrf_update
);
550 vrf_cmd_init(vrf_config_write
, &zserv_privs
);