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
);
341 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
342 * NOTE: Table-id is relevant on two modes:
343 * - case VRF backend is default : on default VRF only
344 * - case VRF backend is netns : on all VRFs
346 struct route_table
*zebra_vrf_lookup_table_with_table_id(afi_t afi
, safi_t safi
,
350 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
351 struct other_route_table ort
, *otable
;
356 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
359 if (table_id
== zvrf
->table_id
)
360 return zebra_vrf_table(afi
, safi
, vrf_id
);
364 ort
.table_id
= table_id
;
365 otable
= otable_find(&zvrf
->other_tables
, &ort
);
368 return otable
->table
;
373 struct route_table
*zebra_vrf_get_table_with_table_id(afi_t afi
, safi_t safi
,
377 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
378 struct other_route_table
*otable
;
379 struct route_table
*table
;
381 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
387 /* Create it as an `other` table */
388 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
390 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
393 otable
->table_id
= table_id
;
394 otable
->table
= table
;
395 otable_add(&zvrf
->other_tables
, otable
);
401 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
402 struct route_node
*node
)
405 zebra_free_rnh(node
->info
);
409 * Create a routing table for the specific AFI/SAFI in the given VRF.
411 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
414 struct route_node
*rn
;
417 assert(!zvrf
->table
[afi
][safi
]);
419 zvrf
->table
[afi
][safi
] =
420 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
422 memset(&p
, 0, sizeof(p
));
423 p
.family
= afi2family(afi
);
425 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
426 zebra_rib_create_dest(rn
);
429 /* Allocate new zebra VRF. */
430 struct zebra_vrf
*zebra_vrf_alloc(void)
432 struct zebra_vrf
*zvrf
;
434 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
436 zebra_vxlan_init_tables(zvrf
);
437 zebra_mpls_init_tables(zvrf
);
439 zvrf
->table_id
= RT_TABLE_MAIN
;
440 /* by default table ID is default one */
444 /* Lookup VRF by identifier. */
445 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
447 return vrf_info_lookup(vrf_id
);
450 /* Lookup VRF by name. */
451 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
456 name
= VRF_DEFAULT_NAME
;
458 vrf
= vrf_lookup_by_name(name
);
460 return ((struct zebra_vrf
*)vrf
->info
);
465 /* Lookup the routing table in an enabled VRF. */
466 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
468 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
473 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
476 return zvrf
->table
[afi
][safi
];
479 static int vrf_config_write(struct vty
*vty
)
482 struct zebra_vrf
*zvrf
;
484 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
490 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
492 vty_out(vty
, "vni %u%s\n", zvrf
->l3vni
,
493 is_l3vni_for_prefix_routes_only(
495 ? " prefix-routes-only"
497 if (zvrf
->zebra_rnh_ip_default_route
)
498 vty_out(vty
, "ip nht resolve-via-default\n");
500 if (zvrf
->zebra_rnh_ipv6_default_route
)
501 vty_out(vty
, "ipv6 nht resolve-via-default\n");
503 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
505 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
506 is_l3vni_for_prefix_routes_only(
508 ? " prefix-routes-only"
510 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
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");
519 zebra_routemap_config_write_protocol(vty
, zvrf
);
520 router_id_write(vty
, zvrf
);
522 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
523 vty_endframe(vty
, " exit-vrf\n!\n");
530 /* Zebra VRF initialization. */
531 void zebra_vrf_init(void)
533 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
534 zebra_vrf_delete
, zebra_vrf_update
);
536 vrf_cmd_init(vrf_config_write
, &zserv_privs
);