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(vrf
);
107 if (!vrf_is_backend_netns())
108 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
110 otable_init(&zvrf
->other_tables
);
112 router_id_init(zvrf
);
116 /* Callback upon enabling a VRF. */
117 static int zebra_vrf_enable(struct vrf
*vrf
)
119 struct zebra_vrf
*zvrf
= vrf
->info
;
120 struct route_table
*table
;
125 if (IS_ZEBRA_DEBUG_EVENT
)
126 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
129 if (vrf_is_backend_netns())
130 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
132 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
133 #if defined(HAVE_RTADV)
137 /* Inform clients that the VRF is now active. This is an
138 * add for the clients.
141 zebra_vrf_add_update(zvrf
);
142 /* Allocate tables */
143 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
144 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
145 zebra_vrf_table_create(zvrf
, afi
, safi
);
147 table
= route_table_init();
148 table
->cleanup
= zebra_rnhtable_node_cleanup
;
149 zvrf
->rnh_table
[afi
] = table
;
151 table
= route_table_init();
152 table
->cleanup
= zebra_rnhtable_node_cleanup
;
153 zvrf
->import_check_table
[afi
] = table
;
156 /* Kick off any VxLAN-EVPN processing. */
157 zebra_vxlan_vrf_enable(zvrf
);
162 /* Callback upon disabling a VRF. */
163 static int zebra_vrf_disable(struct vrf
*vrf
)
165 struct zebra_vrf
*zvrf
= vrf
->info
;
166 struct interface
*ifp
;
172 if (IS_ZEBRA_DEBUG_EVENT
)
173 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
176 /* Stop any VxLAN-EVPN processing. */
177 zebra_vxlan_vrf_disable(zvrf
);
179 #if defined(HAVE_RTADV)
180 rtadv_vrf_terminate(zvrf
);
183 /* Inform clients that the VRF is now inactive. This is a
184 * delete for the clients.
186 zebra_vrf_delete_update(zvrf
);
188 /* If asked to retain routes, there's nothing more to do. */
189 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
192 /* Remove all routes. */
193 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
194 route_table_finish(zvrf
->rnh_table
[afi
]);
195 zvrf
->rnh_table
[afi
] = NULL
;
196 route_table_finish(zvrf
->import_check_table
[afi
]);
197 zvrf
->import_check_table
[afi
] = NULL
;
199 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
200 rib_close_table(zvrf
->table
[afi
][safi
]);
203 /* Cleanup Vxlan, MPLS and PW tables. */
204 zebra_vxlan_cleanup_tables(zvrf
);
205 zebra_mpls_cleanup_tables(zvrf
);
208 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
210 FOR_ALL_INTERFACES (vrf
, ifp
)
211 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
213 /* clean-up work queues */
214 for (i
= 0; i
< MQ_SIZE
; i
++) {
215 struct listnode
*lnode
, *nnode
;
216 struct route_node
*rnode
;
219 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
221 dest
= rib_dest_from_rnode(rnode
);
222 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
223 route_unlock_node(rnode
);
224 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
230 /* Cleanup (free) routing tables and NHT tables. */
231 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
233 * Set the table pointer to NULL as that
234 * we no-longer need a copy of it, nor do we
235 * own this data, the zebra_router structure
236 * owns these tables. Once we've cleaned up the
237 * table, see rib_close_table above
238 * we no-longer need this pointer.
240 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
241 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
243 zvrf
->table
[afi
][safi
] = NULL
;
250 static int zebra_vrf_delete(struct vrf
*vrf
)
252 struct zebra_vrf
*zvrf
= vrf
->info
;
253 struct other_route_table
*otable
;
254 struct route_table
*table
;
260 if (IS_ZEBRA_DEBUG_EVENT
)
261 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
264 /* clean-up work queues */
265 for (i
= 0; i
< MQ_SIZE
; i
++) {
266 struct listnode
*lnode
, *nnode
;
267 struct route_node
*rnode
;
270 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
272 dest
= rib_dest_from_rnode(rnode
);
273 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
274 route_unlock_node(rnode
);
275 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
281 /* Free Vxlan and MPLS. */
282 zebra_vxlan_close_tables(zvrf
);
283 zebra_mpls_close_tables(zvrf
);
285 /* release allocated memory */
286 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
287 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
288 table
= zvrf
->table
[afi
][safi
];
290 zebra_router_release_table(zvrf
, zvrf
->table_id
,
292 zvrf
->table
[afi
][safi
] = NULL
;
296 if (zvrf
->rnh_table
[afi
])
297 route_table_finish(zvrf
->rnh_table
[afi
]);
298 if (zvrf
->import_check_table
[afi
])
299 route_table_finish(zvrf
->import_check_table
[afi
]);
302 otable
= otable_pop(&zvrf
->other_tables
);
304 zebra_router_release_table(zvrf
, otable
->table_id
,
305 otable
->afi
, otable
->safi
);
306 XFREE(MTYPE_OTHER_TABLE
, otable
);
308 otable
= otable_pop(&zvrf
->other_tables
);
311 /* Cleanup EVPN states for vrf */
312 zebra_vxlan_vrf_delete(zvrf
);
314 list_delete_all_node(zvrf
->rid_all_sorted_list
);
315 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
317 list_delete_all_node(zvrf
->rid6_all_sorted_list
);
318 list_delete_all_node(zvrf
->rid6_lo_sorted_list
);
320 otable_fini(&zvrf
->other_tables
);
321 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
327 static int zebra_vrf_update(struct vrf
*vrf
)
329 struct zebra_vrf
*zvrf
= vrf
->info
;
332 if (IS_ZEBRA_DEBUG_EVENT
)
333 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
335 zebra_vrf_add_update(zvrf
);
339 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
340 * NOTE: Table-id is relevant on two modes:
341 * - case VRF backend is default : on default VRF only
342 * - case VRF backend is netns : on all VRFs
344 struct route_table
*zebra_vrf_lookup_table_with_table_id(afi_t afi
, safi_t safi
,
348 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
349 struct other_route_table ort
, *otable
;
354 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
357 if (table_id
== zvrf
->table_id
)
358 return zebra_vrf_table(afi
, safi
, vrf_id
);
362 ort
.table_id
= table_id
;
363 otable
= otable_find(&zvrf
->other_tables
, &ort
);
366 return otable
->table
;
371 struct route_table
*zebra_vrf_get_table_with_table_id(afi_t afi
, safi_t safi
,
375 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
376 struct other_route_table
*otable
;
377 struct route_table
*table
;
379 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
385 /* Create it as an `other` table */
386 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
388 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
391 otable
->table_id
= table_id
;
392 otable
->table
= table
;
393 otable_add(&zvrf
->other_tables
, otable
);
399 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
400 struct route_node
*node
)
403 zebra_free_rnh(node
->info
);
407 * Create a routing table for the specific AFI/SAFI in the given VRF.
409 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
412 struct route_node
*rn
;
415 assert(!zvrf
->table
[afi
][safi
]);
417 zvrf
->table
[afi
][safi
] =
418 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
420 memset(&p
, 0, sizeof(p
));
421 p
.family
= afi2family(afi
);
423 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
424 zebra_rib_create_dest(rn
);
427 /* Allocate new zebra VRF. */
428 struct zebra_vrf
*zebra_vrf_alloc(struct vrf
*vrf
)
430 struct zebra_vrf
*zvrf
;
432 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
437 zebra_vxlan_init_tables(zvrf
);
438 zebra_mpls_init_tables(zvrf
);
440 zvrf
->table_id
= RT_TABLE_MAIN
;
441 /* by default table ID is default one */
445 /* Lookup VRF by identifier. */
446 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
448 return vrf_info_lookup(vrf_id
);
451 /* Lookup VRF by name. */
452 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
457 name
= VRF_DEFAULT_NAME
;
459 vrf
= vrf_lookup_by_name(name
);
461 return ((struct zebra_vrf
*)vrf
->info
);
466 /* Lookup the routing table in an enabled VRF. */
467 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
469 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
474 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
477 return zvrf
->table
[afi
][safi
];
480 static int vrf_config_write(struct vty
*vty
)
483 struct zebra_vrf
*zvrf
;
485 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
491 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
493 vty_out(vty
, "vni %u%s\n", zvrf
->l3vni
,
494 is_l3vni_for_prefix_routes_only(
496 ? " prefix-routes-only"
498 if (zvrf
->zebra_rnh_ip_default_route
)
499 vty_out(vty
, "ip nht resolve-via-default\n");
501 if (zvrf
->zebra_rnh_ipv6_default_route
)
502 vty_out(vty
, "ipv6 nht resolve-via-default\n");
504 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
506 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
507 is_l3vni_for_prefix_routes_only(
509 ? " prefix-routes-only"
511 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
512 if (zvrf
->zebra_rnh_ip_default_route
)
513 vty_out(vty
, " ip nht resolve-via-default\n");
515 if (zvrf
->zebra_rnh_ipv6_default_route
)
516 vty_out(vty
, " ipv6 nht resolve-via-default\n");
520 zebra_routemap_config_write_protocol(vty
, zvrf
);
521 router_id_write(vty
, zvrf
);
523 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
524 vty_endframe(vty
, " exit-vrf\n!\n");
531 /* Zebra VRF initialization. */
532 void zebra_vrf_init(void)
534 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
535 zebra_vrf_delete
, zebra_vrf_update
);
537 vrf_cmd_init(vrf_config_write
, &zserv_privs
);