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
28 #include "srcdest_table.h"
31 #include "zebra/debug.h"
32 #include "zebra/zserv.h"
33 #include "zebra/rib.h"
34 #include "zebra/zebra_vrf.h"
35 #include "zebra/zebra_rnh.h"
36 #include "zebra/router-id.h"
37 #include "zebra/zebra_memory.h"
38 #include "zebra/zebra_static.h"
39 #include "zebra/interface.h"
40 #include "zebra/zebra_mpls.h"
42 extern struct zebra_t zebrad
;
44 /* VRF information update. */
46 zebra_vrf_add_update (struct zebra_vrf
*zvrf
)
48 struct listnode
*node
, *nnode
;
51 if (IS_ZEBRA_DEBUG_EVENT
)
52 zlog_debug ("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name (zvrf
));
54 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
55 zsend_vrf_add (client
, zvrf
);
59 zebra_vrf_delete_update (struct zebra_vrf
*zvrf
)
61 struct listnode
*node
, *nnode
;
64 if (IS_ZEBRA_DEBUG_EVENT
)
65 zlog_debug ("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name (zvrf
));
67 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
68 zsend_vrf_delete (client
, zvrf
);
72 zebra_vrf_update_all (struct zserv
*client
)
76 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
79 zsend_vrf_add (client
, vrf_info_lookup (vrf
->vrf_id
));
83 /* Callback upon creating a new VRF. */
85 zebra_vrf_new (struct vrf
*vrf
)
87 struct zebra_vrf
*zvrf
;
89 if (IS_ZEBRA_DEBUG_EVENT
)
90 zlog_info ("ZVRF %s with id %u", vrf
->name
, vrf
->vrf_id
);
92 zvrf
= zebra_vrf_alloc ();
93 zvrf
->zns
= zebra_ns_lookup (NS_DEFAULT
); /* Point to the global (single) NS */
94 router_id_init (zvrf
);
102 * Moving an interface amongst different vrf's
103 * causes the interface to get a new ifindex
104 * so we need to find static routes with
105 * the old ifindex and replace with new
106 * ifindex to insert back into the table
109 zebra_vrf_static_route_interface_fixup (struct interface
*ifp
)
113 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id (ifp
->vrf_id
);
114 struct route_table
*stable
= NULL
;
115 struct route_node
*rn
= NULL
;
116 struct static_route
*si
= NULL
;
121 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
123 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
125 stable
= zvrf
->stable
[afi
][safi
];
127 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
132 if ((strcmp (si
->ifname
, ifp
->name
) == 0) &&
133 (si
->ifindex
!= ifp
->ifindex
))
135 si
->ifindex
= ifp
->ifindex
;
136 static_install_route (afi
, safi
, &rn
->p
, NULL
, si
);
145 /* Callback upon enabling a VRF. */
147 zebra_vrf_enable (struct vrf
*vrf
)
149 struct zebra_vrf
*zvrf
= vrf
->info
;
150 struct route_table
*stable
;
151 struct route_node
*rn
;
152 struct static_route
*si
;
153 struct interface
*ifp
;
159 zebra_vrf_add_update (zvrf
);
161 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
162 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
164 stable
= zvrf
->stable
[afi
][safi
];
168 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
169 for (si
= rn
->info
; si
; si
= si
->next
)
171 si
->vrf_id
= vrf
->vrf_id
;
174 ifp
= if_lookup_by_name (si
->ifname
, si
->vrf_id
);
176 si
->ifindex
= ifp
->ifindex
;
180 static_install_route (afi
, safi
, &rn
->p
, NULL
, si
);
187 /* Callback upon disabling a VRF. */
189 zebra_vrf_disable (struct vrf
*vrf
)
191 struct zebra_vrf
*zvrf
= vrf
->info
;
192 struct route_table
*stable
;
193 struct route_node
*rn
;
194 struct static_route
*si
;
198 if (IS_ZEBRA_DEBUG_KERNEL
)
199 zlog_debug ("VRF %s id %u is now disabled.",
200 zvrf_name (zvrf
), zvrf_id (zvrf
));
202 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
203 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
205 stable
= zvrf
->stable
[afi
][safi
];
209 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
210 for (si
= rn
->info
; si
; si
= si
->next
)
211 static_uninstall_route(afi
, safi
, &rn
->p
, NULL
, si
);
218 zebra_vrf_delete (struct vrf
*vrf
)
220 struct zebra_vrf
*zvrf
= vrf
->info
;
221 struct route_table
*table
;
229 zebra_vrf_delete_update (zvrf
);
231 /* uninstall everything */
232 if (! CHECK_FLAG (zvrf
->flags
, ZEBRA_VRF_RETAIN
))
234 struct listnode
*node
;
235 struct interface
*ifp
;
237 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++)
239 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
240 rib_close_table (zvrf
->table
[afi
][safi
]);
242 if (vrf
->vrf_id
== VRF_DEFAULT
)
243 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
244 if (zvrf
->other_table
[afi
][table_id
])
245 rib_close_table (zvrf
->other_table
[afi
][table_id
]);
248 zebra_mpls_close_tables (zvrf
);
250 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, ifp
))
251 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all (ifp
);
254 /* clean-up work queues */
255 for (i
= 0; i
< MQ_SIZE
; i
++)
257 struct listnode
*lnode
, *nnode
;
258 struct route_node
*rnode
;
261 for (ALL_LIST_ELEMENTS (zebrad
.mq
->subq
[i
], lnode
, nnode
, rnode
))
263 dest
= rib_dest_from_rnode (rnode
);
264 if (dest
&& rib_dest_vrf (dest
) == zvrf
)
266 route_unlock_node (rnode
);
267 list_delete_node (zebrad
.mq
->subq
[i
], lnode
);
273 /* release allocated memory */
274 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++)
278 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
280 table
= zvrf
->table
[afi
][safi
];
281 table_info
= table
->info
;
282 route_table_finish (table
);
283 XFREE (MTYPE_RIB_TABLE_INFO
, table_info
);
285 table
= zvrf
->stable
[afi
][safi
];
286 route_table_finish (table
);
289 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
290 if (zvrf
->other_table
[afi
][table_id
])
292 table
= zvrf
->other_table
[afi
][table_id
];
293 table_info
= table
->info
;
294 route_table_finish (table
);
295 XFREE (MTYPE_RIB_TABLE_INFO
, table_info
);
298 route_table_finish (zvrf
->rnh_table
[afi
]);
299 route_table_finish (zvrf
->import_check_table
[afi
]);
301 list_delete_all_node (zvrf
->rid_all_sorted_list
);
302 list_delete_all_node (zvrf
->rid_lo_sorted_list
);
303 XFREE (MTYPE_ZEBRA_VRF
, zvrf
);
309 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
310 * NOTE: Table-id is relevant only in the Default VRF.
313 zebra_vrf_table_with_table_id (afi_t afi
, safi_t safi
,
314 vrf_id_t vrf_id
, u_int32_t table_id
)
316 struct route_table
*table
= NULL
;
318 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
321 if (vrf_id
== VRF_DEFAULT
)
323 if (table_id
== RT_TABLE_MAIN
||
324 table_id
== zebrad
.rtm_table_default
)
325 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
327 table
= zebra_vrf_other_route_table (afi
, table_id
, vrf_id
);
330 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
336 zebra_rtable_node_cleanup (struct route_table
*table
, struct route_node
*node
)
338 struct rib
*rib
, *next
;
340 RNODE_FOREACH_RIB_SAFE (node
, rib
, next
)
341 rib_unlink (node
, rib
);
344 XFREE (MTYPE_RIB_DEST
, node
->info
);
348 zebra_stable_node_cleanup (struct route_table
*table
, struct route_node
*node
)
350 struct static_route
*si
, *next
;
353 for (si
= node
->info
; si
; si
= next
)
356 XFREE (MTYPE_STATIC_ROUTE
, si
);
361 zebra_rnhtable_node_cleanup (struct route_table
*table
, struct route_node
*node
)
364 zebra_free_rnh (node
->info
);
368 * Create a routing table for the specific AFI/SAFI in the given VRF.
371 zebra_vrf_table_create (struct zebra_vrf
*zvrf
, afi_t afi
, safi_t safi
)
373 rib_table_info_t
*info
;
374 struct route_table
*table
;
376 assert (!zvrf
->table
[afi
][safi
]);
379 table
= srcdest_table_init();
381 table
= route_table_init();
382 table
->cleanup
= zebra_rtable_node_cleanup
;
383 zvrf
->table
[afi
][safi
] = table
;
385 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
392 /* Allocate new zebra VRF. */
394 zebra_vrf_alloc (void)
396 struct zebra_vrf
*zvrf
;
399 struct route_table
*table
;
401 zvrf
= XCALLOC (MTYPE_ZEBRA_VRF
, sizeof (struct zebra_vrf
));
403 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++)
405 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
407 zebra_vrf_table_create (zvrf
, afi
, safi
);
409 table
= srcdest_table_init();
411 table
= route_table_init();
412 table
->cleanup
= zebra_stable_node_cleanup
;
413 zvrf
->stable
[afi
][safi
] = table
;
416 table
= route_table_init();
417 table
->cleanup
= zebra_rnhtable_node_cleanup
;
418 zvrf
->rnh_table
[afi
] = table
;
420 table
= route_table_init();
421 table
->cleanup
= zebra_rnhtable_node_cleanup
;
422 zvrf
->import_check_table
[afi
] = table
;
425 zebra_mpls_init_tables (zvrf
);
430 /* Lookup VRF by identifier. */
432 zebra_vrf_lookup_by_id (vrf_id_t vrf_id
)
434 return vrf_info_lookup (vrf_id
);
437 /* Lookup VRF by name. */
439 zebra_vrf_lookup_by_name (const char *name
)
444 name
= VRF_DEFAULT_NAME
;
446 vrf
= vrf_lookup_by_name (name
);
448 return ((struct zebra_vrf
*) vrf
->info
);
453 /* Lookup the routing table in an enabled VRF. */
455 zebra_vrf_table (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
457 struct zebra_vrf
*zvrf
= vrf_info_lookup (vrf_id
);
462 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
465 return zvrf
->table
[afi
][safi
];
468 /* Lookup the static routing table in a VRF. */
470 zebra_vrf_static_table (afi_t afi
, safi_t safi
, struct zebra_vrf
*zvrf
)
475 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
478 return zvrf
->stable
[afi
][safi
];
482 zebra_vrf_other_route_table (afi_t afi
, u_int32_t table_id
, vrf_id_t vrf_id
)
484 struct zebra_vrf
*zvrf
;
485 rib_table_info_t
*info
;
486 struct route_table
*table
;
488 zvrf
= vrf_info_lookup (vrf_id
);
495 if (table_id
>= ZEBRA_KERNEL_TABLE_MAX
)
498 if ((vrf_id
== VRF_DEFAULT
) && (table_id
!= RT_TABLE_MAIN
) && (table_id
!= zebrad
.rtm_table_default
))
500 if (zvrf
->other_table
[afi
][table_id
] == NULL
)
502 table
= (afi
== AFI_IP6
) ? srcdest_table_init() : route_table_init();
503 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
506 info
->safi
= SAFI_UNICAST
;
508 zvrf
->other_table
[afi
][table_id
] = table
;
511 return (zvrf
->other_table
[afi
][table_id
]);
514 return zvrf
->table
[afi
][SAFI_UNICAST
];
518 vrf_config_write (struct vty
*vty
)
521 struct zebra_vrf
*zvrf
;
523 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
526 if (! zvrf
|| strcmp (zvrf_name (zvrf
), VRF_DEFAULT_NAME
))
528 vty_out (vty
, "vrf %s%s", zvrf_name (zvrf
), VTY_NEWLINE
);
529 vty_out (vty
, "!%s", VTY_NEWLINE
);
535 /* Zebra VRF initialization. */
537 zebra_vrf_init (void)
539 vrf_add_hook (VRF_NEW_HOOK
, zebra_vrf_new
);
540 vrf_add_hook (VRF_ENABLE_HOOK
, zebra_vrf_enable
);
541 vrf_add_hook (VRF_DISABLE_HOOK
, zebra_vrf_disable
);
542 vrf_add_hook (VRF_DELETE_HOOK
, zebra_vrf_delete
);
545 vrf_cmd_init (vrf_config_write
);