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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "zebra/debug.h"
31 #include "zebra/zserv.h"
32 #include "zebra/rib.h"
33 #include "zebra/zebra_vrf.h"
34 #include "zebra/zebra_rnh.h"
35 #include "zebra/router-id.h"
36 #include "zebra/zebra_memory.h"
37 #include "zebra/zebra_static.h"
38 #include "zebra/interface.h"
39 #include "zebra/zebra_mpls.h"
41 extern struct zebra_t zebrad
;
43 /* VRF information update. */
45 zebra_vrf_add_update (struct zebra_vrf
*zvrf
)
47 struct listnode
*node
, *nnode
;
50 if (IS_ZEBRA_DEBUG_EVENT
)
51 zlog_debug ("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name (zvrf
));
53 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
54 zsend_vrf_add (client
, zvrf
);
58 zebra_vrf_delete_update (struct zebra_vrf
*zvrf
)
60 struct listnode
*node
, *nnode
;
63 if (IS_ZEBRA_DEBUG_EVENT
)
64 zlog_debug ("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name (zvrf
));
66 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
67 zsend_vrf_delete (client
, zvrf
);
71 zebra_vrf_update_all (struct zserv
*client
)
75 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
78 zsend_vrf_add (client
, vrf_info_lookup (vrf
->vrf_id
));
82 /* Callback upon creating a new VRF. */
84 zebra_vrf_new (struct vrf
*vrf
)
86 struct zebra_vrf
*zvrf
;
88 if (IS_ZEBRA_DEBUG_EVENT
)
89 zlog_info ("ZVRF %s with id %u", vrf
->name
, vrf
->vrf_id
);
91 zvrf
= zebra_vrf_alloc ();
92 zvrf
->zns
= zebra_ns_lookup (NS_DEFAULT
); /* Point to the global (single) NS */
93 router_id_init (zvrf
);
101 * Moving an interface amongst different vrf's
102 * causes the interface to get a new ifindex
103 * so we need to find static routes with
104 * the old ifindex and replace with new
105 * ifindex to insert back into the table
108 zebra_vrf_static_route_interface_fixup (struct interface
*ifp
)
112 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id (ifp
->vrf_id
);
113 struct route_table
*stable
= NULL
;
114 struct route_node
*rn
= NULL
;
115 struct static_route
*si
= NULL
;
120 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
122 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
124 stable
= zvrf
->stable
[afi
][safi
];
126 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
131 if ((strcmp (si
->ifname
, ifp
->name
) == 0) &&
132 (si
->ifindex
!= ifp
->ifindex
))
134 si
->ifindex
= ifp
->ifindex
;
135 static_install_route (afi
, safi
, &rn
->p
, si
);
144 /* Callback upon enabling a VRF. */
146 zebra_vrf_enable (struct vrf
*vrf
)
148 struct zebra_vrf
*zvrf
= vrf
->info
;
149 struct route_table
*stable
;
150 struct route_node
*rn
;
151 struct static_route
*si
;
152 struct interface
*ifp
;
158 zebra_vrf_add_update (zvrf
);
160 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
161 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
163 stable
= zvrf
->stable
[afi
][safi
];
167 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
168 for (si
= rn
->info
; si
; si
= si
->next
)
170 si
->vrf_id
= vrf
->vrf_id
;
173 ifp
= if_lookup_by_name_vrf (si
->ifname
, si
->vrf_id
);
175 si
->ifindex
= ifp
->ifindex
;
179 static_install_route (afi
, safi
, &rn
->p
, si
);
186 /* Callback upon disabling a VRF. */
188 zebra_vrf_disable (struct vrf
*vrf
)
190 struct zebra_vrf
*zvrf
= vrf
->info
;
191 struct route_table
*stable
;
192 struct route_node
*rn
;
193 struct static_route
*si
;
197 if (IS_ZEBRA_DEBUG_KERNEL
)
198 zlog_debug ("VRF %s id %u is now disabled.",
199 zvrf_name (zvrf
), zvrf_id (zvrf
));
201 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
202 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
204 stable
= zvrf
->stable
[afi
][safi
];
208 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
209 for (si
= rn
->info
; si
; si
= si
->next
)
210 static_uninstall_route(afi
, safi
, &rn
->p
, si
);
217 zebra_vrf_delete (struct vrf
*vrf
)
219 struct zebra_vrf
*zvrf
= vrf
->info
;
220 struct route_table
*table
;
228 zebra_vrf_delete_update (zvrf
);
230 /* uninstall everything */
231 if (! CHECK_FLAG (zvrf
->flags
, ZEBRA_VRF_RETAIN
))
233 struct listnode
*node
;
234 struct interface
*ifp
;
236 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++)
238 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
239 rib_close_table (zvrf
->table
[afi
][safi
]);
241 if (vrf
->vrf_id
== VRF_DEFAULT
)
242 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
243 if (zvrf
->other_table
[afi
][table_id
])
244 rib_close_table (zvrf
->other_table
[afi
][table_id
]);
247 zebra_mpls_close_tables (zvrf
);
249 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, ifp
))
250 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all (ifp
);
253 /* clean-up work queues */
254 for (i
= 0; i
< MQ_SIZE
; i
++)
256 struct listnode
*lnode
, *nnode
;
257 struct route_node
*rnode
;
260 for (ALL_LIST_ELEMENTS (zebrad
.mq
->subq
[i
], lnode
, nnode
, rnode
))
262 dest
= rib_dest_from_rnode (rnode
);
263 if (dest
&& rib_dest_vrf (dest
) == zvrf
)
265 route_unlock_node (rnode
);
266 list_delete_node (zebrad
.mq
->subq
[i
], lnode
);
271 /* release allocated memory */
272 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++)
276 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
278 table
= zvrf
->table
[afi
][safi
];
279 table_info
= table
->info
;
280 route_table_finish (table
);
281 XFREE (MTYPE_RIB_TABLE_INFO
, table_info
);
283 table
= zvrf
->stable
[afi
][safi
];
284 route_table_finish (table
);
287 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
288 if (zvrf
->other_table
[afi
][table_id
])
290 table
= zvrf
->other_table
[afi
][table_id
];
291 table_info
= table
->info
;
292 route_table_finish (table
);
293 XFREE (MTYPE_RIB_TABLE_INFO
, table_info
);
296 route_table_finish (zvrf
->rnh_table
[afi
]);
297 route_table_finish (zvrf
->import_check_table
[afi
]);
299 list_delete_all_node (zvrf
->rid_all_sorted_list
);
300 list_delete_all_node (zvrf
->rid_lo_sorted_list
);
301 XFREE (MTYPE_ZEBRA_VRF
, zvrf
);
307 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
308 * NOTE: Table-id is relevant only in the Default VRF.
311 zebra_vrf_table_with_table_id (afi_t afi
, safi_t safi
,
312 vrf_id_t vrf_id
, u_int32_t table_id
)
314 struct route_table
*table
= NULL
;
316 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
319 if (vrf_id
== VRF_DEFAULT
)
321 if (table_id
== RT_TABLE_MAIN
||
322 table_id
== zebrad
.rtm_table_default
)
323 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
325 table
= zebra_vrf_other_route_table (afi
, table_id
, vrf_id
);
328 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
334 zebra_rtable_node_destroy (route_table_delegate_t
*delegate
,
335 struct route_table
*table
, struct route_node
*node
)
337 struct rib
*rib
, *next
;
339 RNODE_FOREACH_RIB_SAFE (node
, rib
, next
)
340 rib_unlink (node
, rib
);
343 XFREE (MTYPE_RIB_DEST
, node
->info
);
345 route_node_destroy (delegate
, table
, node
);
349 zebra_stable_node_destroy (route_table_delegate_t
*delegate
,
350 struct route_table
*table
, struct route_node
*node
)
352 struct static_route
*si
, *next
;
355 for (si
= node
->info
; si
; si
= next
)
358 XFREE (MTYPE_STATIC_ROUTE
, si
);
361 route_node_destroy (delegate
, table
, node
);
365 zebra_rnhtable_node_destroy (route_table_delegate_t
*delegate
,
366 struct route_table
*table
, struct route_node
*node
)
369 zebra_free_rnh (node
->info
);
371 route_node_destroy (delegate
, table
, node
);
374 route_table_delegate_t zebra_rtable_delegate
= {
375 .create_node
= route_node_create
,
376 .destroy_node
= zebra_rtable_node_destroy
379 route_table_delegate_t zebra_stable_delegate
= {
380 .create_node
= route_node_create
,
381 .destroy_node
= zebra_stable_node_destroy
384 route_table_delegate_t zebra_rnhtable_delegate
= {
385 .create_node
= route_node_create
,
386 .destroy_node
= zebra_rnhtable_node_destroy
390 * Create a routing table for the specific AFI/SAFI in the given VRF.
393 zebra_vrf_table_create (struct zebra_vrf
*zvrf
, afi_t afi
, safi_t safi
)
395 rib_table_info_t
*info
;
396 struct route_table
*table
;
398 assert (!zvrf
->table
[afi
][safi
]);
400 table
= route_table_init_with_delegate (&zebra_rtable_delegate
);
401 zvrf
->table
[afi
][safi
] = table
;
403 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
410 /* Allocate new zebra VRF. */
412 zebra_vrf_alloc (void)
414 struct zebra_vrf
*zvrf
;
418 zvrf
= XCALLOC (MTYPE_ZEBRA_VRF
, sizeof (struct zebra_vrf
));
420 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++)
422 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
424 zebra_vrf_table_create (zvrf
, afi
, safi
);
425 zvrf
->stable
[afi
][safi
] =
426 route_table_init_with_delegate (&zebra_stable_delegate
);
429 zvrf
->rnh_table
[afi
] =
430 route_table_init_with_delegate (&zebra_rnhtable_delegate
);
431 zvrf
->import_check_table
[afi
] =
432 route_table_init_with_delegate (&zebra_rnhtable_delegate
);
435 zebra_mpls_init_tables (zvrf
);
440 /* Lookup VRF by identifier. */
442 zebra_vrf_lookup_by_id (vrf_id_t vrf_id
)
444 return vrf_info_lookup (vrf_id
);
447 /* Lookup VRF by name. */
449 zebra_vrf_lookup_by_name (const char *name
)
454 name
= VRF_DEFAULT_NAME
;
456 vrf
= vrf_lookup_by_name (name
);
458 return ((struct zebra_vrf
*) vrf
->info
);
463 /* Lookup the routing table in an enabled VRF. */
465 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. */
480 zebra_vrf_static_table (afi_t afi
, safi_t safi
, struct zebra_vrf
*zvrf
)
485 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
488 return zvrf
->stable
[afi
][safi
];
492 zebra_vrf_other_route_table (afi_t afi
, u_int32_t table_id
, vrf_id_t vrf_id
)
494 struct zebra_vrf
*zvrf
;
495 rib_table_info_t
*info
;
496 struct route_table
*table
;
498 zvrf
= vrf_info_lookup (vrf_id
);
505 if (table_id
>= ZEBRA_KERNEL_TABLE_MAX
)
508 if ((vrf_id
== VRF_DEFAULT
) && (table_id
!= RT_TABLE_MAIN
) && (table_id
!= zebrad
.rtm_table_default
))
510 if (zvrf
->other_table
[afi
][table_id
] == NULL
)
512 table
= route_table_init();
513 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
516 info
->safi
= SAFI_UNICAST
;
518 zvrf
->other_table
[afi
][table_id
] = table
;
521 return (zvrf
->other_table
[afi
][table_id
]);
524 return zvrf
->table
[afi
][SAFI_UNICAST
];
528 vrf_config_write (struct vty
*vty
)
531 struct zebra_vrf
*zvrf
;
533 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
536 if (! zvrf
|| strcmp (zvrf_name (zvrf
), VRF_DEFAULT_NAME
))
538 vty_out (vty
, "vrf %s%s", zvrf_name (zvrf
), VTY_NEWLINE
);
539 vty_out (vty
, "!%s", VTY_NEWLINE
);
545 /* Zebra VRF initialization. */
547 zebra_vrf_init (void)
549 vrf_add_hook (VRF_NEW_HOOK
, zebra_vrf_new
);
550 vrf_add_hook (VRF_ENABLE_HOOK
, zebra_vrf_enable
);
551 vrf_add_hook (VRF_DISABLE_HOOK
, zebra_vrf_disable
);
552 vrf_add_hook (VRF_DELETE_HOOK
, zebra_vrf_delete
);
555 vrf_cmd_init (vrf_config_write
);