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"
44 #ifndef VTYSH_EXTRACT_PL
45 #include "zebra/zebra_vrf_clippy.c"
48 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
50 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
51 struct route_node
*node
);
53 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_VRF
, "ZEBRA VRF");
54 DEFINE_MTYPE_STATIC(ZEBRA
, OTHER_TABLE
, "Other Table");
56 /* VRF information update. */
57 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
59 struct listnode
*node
, *nnode
;
62 if (IS_ZEBRA_DEBUG_EVENT
)
63 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
65 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
66 /* Do not send unsolicited messages to synchronous clients. */
67 if (client
->synchronous
)
70 zsend_vrf_add(client
, zvrf
);
74 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
76 struct listnode
*node
, *nnode
;
79 if (IS_ZEBRA_DEBUG_EVENT
)
80 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
82 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
83 /* Do not send unsolicited messages to synchronous clients. */
84 if (client
->synchronous
)
87 zsend_vrf_delete(client
, zvrf
);
91 void zebra_vrf_update_all(struct zserv
*client
)
95 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
96 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
97 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
101 /* Callback upon creating a new VRF. */
102 static int zebra_vrf_new(struct vrf
*vrf
)
104 struct zebra_vrf
*zvrf
;
106 if (IS_ZEBRA_DEBUG_EVENT
)
107 zlog_debug("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
109 zvrf
= zebra_vrf_alloc(vrf
);
110 if (!vrf_is_backend_netns())
111 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
113 otable_init(&zvrf
->other_tables
);
115 router_id_init(zvrf
);
119 /* Callback upon enabling a VRF. */
120 static int zebra_vrf_enable(struct vrf
*vrf
)
122 struct zebra_vrf
*zvrf
= vrf
->info
;
123 struct route_table
*table
;
128 if (IS_ZEBRA_DEBUG_EVENT
)
129 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
132 if (vrf_is_backend_netns())
133 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
135 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
136 #if defined(HAVE_RTADV)
137 rtadv_vrf_init(zvrf
);
140 /* Inform clients that the VRF is now active. This is an
141 * add for the clients.
144 zebra_vrf_add_update(zvrf
);
145 /* Allocate tables */
146 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
147 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
148 zebra_vrf_table_create(zvrf
, afi
, safi
);
150 table
= route_table_init();
151 table
->cleanup
= zebra_rnhtable_node_cleanup
;
152 zvrf
->rnh_table
[afi
] = table
;
154 table
= route_table_init();
155 table
->cleanup
= zebra_rnhtable_node_cleanup
;
156 zvrf
->import_check_table
[afi
] = table
;
159 /* Kick off any VxLAN-EVPN processing. */
160 zebra_vxlan_vrf_enable(zvrf
);
165 /* Callback upon disabling a VRF. */
166 static int zebra_vrf_disable(struct vrf
*vrf
)
168 struct zebra_vrf
*zvrf
= vrf
->info
;
169 struct interface
*ifp
;
175 if (IS_ZEBRA_DEBUG_EVENT
)
176 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
179 /* Stop any VxLAN-EVPN processing. */
180 zebra_vxlan_vrf_disable(zvrf
);
182 #if defined(HAVE_RTADV)
183 rtadv_vrf_terminate(zvrf
);
186 /* Inform clients that the VRF is now inactive. This is a
187 * delete for the clients.
189 zebra_vrf_delete_update(zvrf
);
191 /* If asked to retain routes, there's nothing more to do. */
192 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
195 /* Remove all routes. */
196 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
197 route_table_finish(zvrf
->rnh_table
[afi
]);
198 zvrf
->rnh_table
[afi
] = NULL
;
199 route_table_finish(zvrf
->import_check_table
[afi
]);
200 zvrf
->import_check_table
[afi
] = NULL
;
202 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
203 rib_close_table(zvrf
->table
[afi
][safi
]);
206 /* Cleanup Vxlan, MPLS and PW tables. */
207 zebra_vxlan_cleanup_tables(zvrf
);
208 zebra_mpls_cleanup_tables(zvrf
);
211 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
213 FOR_ALL_INTERFACES (vrf
, ifp
)
214 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
216 /* clean-up work queues */
217 for (i
= 0; i
< MQ_SIZE
; i
++) {
218 struct listnode
*lnode
, *nnode
;
219 struct route_node
*rnode
;
222 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
224 dest
= rib_dest_from_rnode(rnode
);
225 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
226 route_unlock_node(rnode
);
227 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
233 /* Cleanup (free) routing tables and NHT tables. */
234 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
236 * Set the table pointer to NULL as that
237 * we no-longer need a copy of it, nor do we
238 * own this data, the zebra_router structure
239 * owns these tables. Once we've cleaned up the
240 * table, see rib_close_table above
241 * we no-longer need this pointer.
243 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
244 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
246 zvrf
->table
[afi
][safi
] = NULL
;
253 static int zebra_vrf_delete(struct vrf
*vrf
)
255 struct zebra_vrf
*zvrf
= vrf
->info
;
256 struct other_route_table
*otable
;
257 struct route_table
*table
;
263 if (IS_ZEBRA_DEBUG_EVENT
)
264 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
267 /* clean-up work queues */
268 for (i
= 0; i
< MQ_SIZE
; i
++) {
269 struct listnode
*lnode
, *nnode
;
270 struct route_node
*rnode
;
273 for (ALL_LIST_ELEMENTS(zrouter
.mq
->subq
[i
], lnode
, nnode
,
275 dest
= rib_dest_from_rnode(rnode
);
276 if (dest
&& rib_dest_vrf(dest
) == zvrf
) {
277 route_unlock_node(rnode
);
278 list_delete_node(zrouter
.mq
->subq
[i
], lnode
);
284 /* Free Vxlan and MPLS. */
285 zebra_vxlan_close_tables(zvrf
);
286 zebra_mpls_close_tables(zvrf
);
288 /* release allocated memory */
289 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
290 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
291 table
= zvrf
->table
[afi
][safi
];
293 zebra_router_release_table(zvrf
, zvrf
->table_id
,
295 zvrf
->table
[afi
][safi
] = NULL
;
299 if (zvrf
->rnh_table
[afi
])
300 route_table_finish(zvrf
->rnh_table
[afi
]);
301 if (zvrf
->import_check_table
[afi
])
302 route_table_finish(zvrf
->import_check_table
[afi
]);
305 otable
= otable_pop(&zvrf
->other_tables
);
307 zebra_router_release_table(zvrf
, otable
->table_id
,
308 otable
->afi
, otable
->safi
);
309 XFREE(MTYPE_OTHER_TABLE
, otable
);
311 otable
= otable_pop(&zvrf
->other_tables
);
314 /* Cleanup EVPN states for vrf */
315 zebra_vxlan_vrf_delete(zvrf
);
317 list_delete_all_node(zvrf
->rid_all_sorted_list
);
318 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
320 list_delete_all_node(zvrf
->rid6_all_sorted_list
);
321 list_delete_all_node(zvrf
->rid6_lo_sorted_list
);
323 otable_fini(&zvrf
->other_tables
);
324 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
330 static int zebra_vrf_update(struct vrf
*vrf
)
332 struct zebra_vrf
*zvrf
= vrf
->info
;
335 if (IS_ZEBRA_DEBUG_EVENT
)
336 zlog_debug("VRF %s id %u, name updated", vrf
->name
,
338 zebra_vrf_add_update(zvrf
);
342 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
343 * NOTE: Table-id is relevant on two modes:
344 * - case VRF backend is default : on default VRF only
345 * - case VRF backend is netns : on all VRFs
347 struct route_table
*zebra_vrf_lookup_table_with_table_id(afi_t afi
, safi_t safi
,
351 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
352 struct other_route_table ort
, *otable
;
357 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
360 if (table_id
== zvrf
->table_id
)
361 return zebra_vrf_table(afi
, safi
, vrf_id
);
365 ort
.table_id
= table_id
;
366 otable
= otable_find(&zvrf
->other_tables
, &ort
);
369 return otable
->table
;
374 struct route_table
*zebra_vrf_get_table_with_table_id(afi_t afi
, safi_t safi
,
378 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
379 struct other_route_table
*otable
;
380 struct route_table
*table
;
382 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
388 /* Create it as an `other` table */
389 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
391 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
394 otable
->table_id
= table_id
;
395 otable
->table
= table
;
396 otable_add(&zvrf
->other_tables
, otable
);
402 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
403 struct route_node
*node
)
406 zebra_free_rnh(node
->info
);
410 * Create a routing table for the specific AFI/SAFI in the given VRF.
412 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
415 struct route_node
*rn
;
418 assert(!zvrf
->table
[afi
][safi
]);
420 zvrf
->table
[afi
][safi
] =
421 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
423 memset(&p
, 0, sizeof(p
));
424 p
.family
= afi2family(afi
);
426 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
427 zebra_rib_create_dest(rn
);
430 /* Allocate new zebra VRF. */
431 struct zebra_vrf
*zebra_vrf_alloc(struct vrf
*vrf
)
433 struct zebra_vrf
*zvrf
;
435 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
440 zebra_vxlan_init_tables(zvrf
);
441 zebra_mpls_init_tables(zvrf
);
443 zvrf
->table_id
= RT_TABLE_MAIN
;
444 /* by default table ID is default one */
448 /* Lookup VRF by identifier. */
449 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
451 return vrf_info_lookup(vrf_id
);
454 /* Lookup VRF by name. */
455 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
460 name
= VRF_DEFAULT_NAME
;
462 vrf
= vrf_lookup_by_name(name
);
464 return ((struct zebra_vrf
*)vrf
->info
);
469 /* Lookup the routing table in an enabled VRF. */
470 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
472 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
477 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
480 return zvrf
->table
[afi
][safi
];
483 static int vrf_config_write(struct vty
*vty
)
486 struct zebra_vrf
*zvrf
;
488 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
494 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
496 vty_out(vty
, "vni %u%s\n", zvrf
->l3vni
,
497 is_l3vni_for_prefix_routes_only(
499 ? " prefix-routes-only"
501 if (zvrf
->zebra_rnh_ip_default_route
)
502 vty_out(vty
, "ip nht resolve-via-default\n");
504 if (zvrf
->zebra_rnh_ipv6_default_route
)
505 vty_out(vty
, "ipv6 nht resolve-via-default\n");
507 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
509 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
510 is_l3vni_for_prefix_routes_only(
512 ? " prefix-routes-only"
514 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
515 if (zvrf
->zebra_rnh_ip_default_route
)
516 vty_out(vty
, " ip nht resolve-via-default\n");
518 if (zvrf
->zebra_rnh_ipv6_default_route
)
519 vty_out(vty
, " ipv6 nht resolve-via-default\n");
523 zebra_routemap_config_write_protocol(vty
, zvrf
);
524 router_id_write(vty
, zvrf
);
526 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
527 vty_endframe(vty
, " exit-vrf\n!\n");
536 "netns NAME$netns_name",
537 "Attach VRF to a Namespace\n"
538 "The file name in " NS_RUN_DIR
", or a full pathname\n")
540 char *pathname
= ns_netns_pathname(vty
, netns_name
);
543 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
546 return CMD_WARNING_CONFIG_FAILED
;
548 frr_with_privs(&zserv_privs
) {
549 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
562 "Detach VRF from a Namespace\n"
563 "The file name in " NS_RUN_DIR
", or a full pathname\n")
565 struct ns
*ns
= NULL
;
567 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
569 if (!vrf_is_backend_netns()) {
570 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
571 return CMD_WARNING_CONFIG_FAILED
;
574 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
575 vrf
->name
, vrf
->vrf_id
);
576 return CMD_WARNING_CONFIG_FAILED
;
579 ns
= (struct ns
*)vrf
->ns_ctxt
;
583 /* vrf ID from VRF is necessary for Zebra
584 * so that propagate to other clients is done
591 /* Zebra VRF initialization. */
592 void zebra_vrf_init(void)
594 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
595 zebra_vrf_delete
, zebra_vrf_update
);
597 vrf_cmd_init(vrf_config_write
, &zserv_privs
);
599 if (vrf_is_backend_netns() && ns_have_netns()) {
600 /* Install NS commands. */
601 install_element(VRF_NODE
, &vrf_netns_cmd
);
602 install_element(VRF_NODE
, &no_vrf_netns_cmd
);