]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vrf.c
Merge branch 'queue/osr/vtysh-generic'
[mirror_frr.git] / zebra / zebra_vrf.c
1 /*
2 * Copyright (C) 2016 CumulusNetworks
3 * Donald Sharp
4 *
5 * This file is part of Quagga
6 *
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
10 * later version.
11 *
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.
16 *
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
20 * 02111-1307, USA.
21 */
22 #include <zebra.h>
23
24 #include "log.h"
25 #include "linklist.h"
26 #include "command.h"
27 #include "memory.h"
28
29 #include "vty.h"
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"
40
41 extern struct zebra_t zebrad;
42
43 /* VRF information update. */
44 static void
45 zebra_vrf_add_update (struct zebra_vrf *zvrf)
46 {
47 struct listnode *node, *nnode;
48 struct zserv *client;
49
50 if (IS_ZEBRA_DEBUG_EVENT)
51 zlog_debug ("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name (zvrf));
52
53 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
54 zsend_vrf_add (client, zvrf);
55 }
56
57 static void
58 zebra_vrf_delete_update (struct zebra_vrf *zvrf)
59 {
60 struct listnode *node, *nnode;
61 struct zserv *client;
62
63 if (IS_ZEBRA_DEBUG_EVENT)
64 zlog_debug ("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name (zvrf));
65
66 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
67 zsend_vrf_delete (client, zvrf);
68 }
69
70 void
71 zebra_vrf_update_all (struct zserv *client)
72 {
73 struct vrf *vrf;
74
75 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
76 {
77 if (vrf->vrf_id)
78 zsend_vrf_add (client, vrf_info_lookup (vrf->vrf_id));
79 }
80 }
81
82 /* Callback upon creating a new VRF. */
83 static int
84 zebra_vrf_new (struct vrf *vrf)
85 {
86 struct zebra_vrf *zvrf;
87
88 if (IS_ZEBRA_DEBUG_EVENT)
89 zlog_info ("ZVRF %s with id %u", vrf->name, vrf->vrf_id);
90
91 zvrf = zebra_vrf_alloc ();
92 zvrf->zns = zebra_ns_lookup (NS_DEFAULT); /* Point to the global (single) NS */
93 router_id_init (zvrf);
94 vrf->info = zvrf;
95 zvrf->vrf = vrf;
96
97 return 0;
98 }
99
100 /*
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
106 */
107 void
108 zebra_vrf_static_route_interface_fixup (struct interface *ifp)
109 {
110 afi_t afi;
111 safi_t safi;
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;
116
117 if (!zvrf)
118 return;
119
120 for (afi = AFI_IP; afi < AFI_MAX; afi++)
121 {
122 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
123 {
124 stable = zvrf->stable[afi][safi];
125 if (stable)
126 for (rn = route_top (stable); rn; rn = route_next (rn))
127 {
128 if (rn->info)
129 {
130 si = rn->info;
131 if ((strcmp (si->ifname, ifp->name) == 0) &&
132 (si->ifindex != ifp->ifindex))
133 {
134 si->ifindex = ifp->ifindex;
135 static_install_route (afi, safi, &rn->p, si);
136 }
137 }
138 }
139 }
140 }
141
142 }
143
144 /* Callback upon enabling a VRF. */
145 static int
146 zebra_vrf_enable (struct vrf *vrf)
147 {
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;
153 afi_t afi;
154 safi_t safi;
155
156 assert (zvrf);
157
158 zebra_vrf_add_update (zvrf);
159
160 for (afi = AFI_IP; afi < AFI_MAX; afi++)
161 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
162 {
163 stable = zvrf->stable[afi][safi];
164 if (! stable)
165 continue;
166
167 for (rn = route_top (stable); rn; rn = route_next (rn))
168 for (si = rn->info; si; si = si->next)
169 {
170 si->vrf_id = vrf->vrf_id;
171 if (si->ifindex)
172 {
173 ifp = if_lookup_by_name_vrf (si->ifname, si->vrf_id);
174 if (ifp)
175 si->ifindex = ifp->ifindex;
176 else
177 continue;
178 }
179 static_install_route (afi, safi, &rn->p, si);
180 }
181 }
182
183 return 0;
184 }
185
186 /* Callback upon disabling a VRF. */
187 static int
188 zebra_vrf_disable (struct vrf *vrf)
189 {
190 struct zebra_vrf *zvrf = vrf->info;
191 struct route_table *stable;
192 struct route_node *rn;
193 struct static_route *si;
194 afi_t afi;
195 safi_t safi;
196
197 if (IS_ZEBRA_DEBUG_KERNEL)
198 zlog_debug ("VRF %s id %u is now disabled.",
199 zvrf_name (zvrf), zvrf_id (zvrf));
200
201 for (afi = AFI_IP; afi < AFI_MAX; afi++)
202 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
203 {
204 stable = zvrf->stable[afi][safi];
205 if (! stable)
206 continue;
207
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);
211 }
212
213 return 0;
214 }
215
216 static int
217 zebra_vrf_delete (struct vrf *vrf)
218 {
219 struct zebra_vrf *zvrf = vrf->info;
220 struct route_table *table;
221 u_int32_t table_id;
222 afi_t afi;
223 safi_t safi;
224 unsigned i;
225
226 assert (zvrf);
227
228 zebra_vrf_delete_update (zvrf);
229
230 /* uninstall everything */
231 if (! CHECK_FLAG (zvrf->flags, ZEBRA_VRF_RETAIN))
232 {
233 struct listnode *node;
234 struct interface *ifp;
235
236 for (afi = AFI_IP; afi <= AFI_IP6; afi++)
237 {
238 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
239 rib_close_table (zvrf->table[afi][safi]);
240
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]);
245 }
246
247 zebra_mpls_close_tables (zvrf);
248
249 for (ALL_LIST_ELEMENTS_RO (vrf->iflist, node, ifp))
250 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all (ifp);
251 }
252
253 /* clean-up work queues */
254 for (i = 0; i < MQ_SIZE; i++)
255 {
256 struct listnode *lnode, *nnode;
257 struct route_node *rnode;
258 rib_dest_t *dest;
259
260 for (ALL_LIST_ELEMENTS (zebrad.mq->subq[i], lnode, nnode, rnode))
261 {
262 dest = rib_dest_from_rnode (rnode);
263 if (dest && rib_dest_vrf (dest) == zvrf)
264 {
265 route_unlock_node (rnode);
266 list_delete_node (zebrad.mq->subq[i], lnode);
267 }
268 }
269 }
270
271 /* release allocated memory */
272 for (afi = AFI_IP; afi <= AFI_IP6; afi++)
273 {
274 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
275 {
276 table = zvrf->table[afi][safi];
277 XFREE (MTYPE_RIB_TABLE_INFO, table->info);
278 route_table_finish (table);
279
280 table = zvrf->stable[afi][safi];
281 route_table_finish (table);
282 }
283
284 for (table_id = 0; table_id < ZEBRA_KERNEL_TABLE_MAX; table_id++)
285 if (zvrf->other_table[afi][table_id])
286 {
287 table = zvrf->other_table[afi][table_id];
288 XFREE (MTYPE_RIB_TABLE_INFO, table->info);
289 route_table_finish (table);
290 }
291
292 route_table_finish (zvrf->rnh_table[afi]);
293 route_table_finish (zvrf->import_check_table[afi]);
294 }
295 list_delete_all_node (zvrf->rid_all_sorted_list);
296 list_delete_all_node (zvrf->rid_lo_sorted_list);
297 XFREE (MTYPE_ZEBRA_VRF, zvrf);
298 vrf->info = NULL;
299
300 return 0;
301 }
302
303 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
304 * NOTE: Table-id is relevant only in the Default VRF.
305 */
306 struct route_table *
307 zebra_vrf_table_with_table_id (afi_t afi, safi_t safi,
308 vrf_id_t vrf_id, u_int32_t table_id)
309 {
310 struct route_table *table = NULL;
311
312 if (afi >= AFI_MAX || safi >= SAFI_MAX)
313 return NULL;
314
315 if (vrf_id == VRF_DEFAULT)
316 {
317 if (table_id == RT_TABLE_MAIN ||
318 table_id == zebrad.rtm_table_default)
319 table = zebra_vrf_table (afi, safi, vrf_id);
320 else
321 table = zebra_vrf_other_route_table (afi, table_id, vrf_id);
322 }
323 else
324 table = zebra_vrf_table (afi, safi, vrf_id);
325
326 return table;
327 }
328
329 static void
330 zebra_rtable_node_destroy (route_table_delegate_t *delegate,
331 struct route_table *table, struct route_node *node)
332 {
333 struct rib *rib, *next;
334
335 RNODE_FOREACH_RIB_SAFE (node, rib, next)
336 rib_unlink (node, rib);
337
338 if (node->info)
339 XFREE (MTYPE_RIB_DEST, node->info);
340
341 route_node_destroy (delegate, table, node);
342 }
343
344 static void
345 zebra_stable_node_destroy (route_table_delegate_t *delegate,
346 struct route_table *table, struct route_node *node)
347 {
348 struct static_route *si, *next;
349
350 if (node->info)
351 for (si = node->info; si; si = next)
352 {
353 next = si->next;
354 XFREE (MTYPE_STATIC_ROUTE, si);
355 }
356
357 route_node_destroy (delegate, table, node);
358 }
359
360 static void
361 zebra_rnhtable_node_destroy (route_table_delegate_t *delegate,
362 struct route_table *table, struct route_node *node)
363 {
364 if (node->info)
365 zebra_free_rnh (node->info);
366
367 route_node_destroy (delegate, table, node);
368 }
369
370 route_table_delegate_t zebra_rtable_delegate = {
371 .create_node = route_node_create,
372 .destroy_node = zebra_rtable_node_destroy
373 };
374
375 route_table_delegate_t zebra_stable_delegate = {
376 .create_node = route_node_create,
377 .destroy_node = zebra_stable_node_destroy
378 };
379
380 route_table_delegate_t zebra_rnhtable_delegate = {
381 .create_node = route_node_create,
382 .destroy_node = zebra_rnhtable_node_destroy
383 };
384
385 /*
386 * Create a routing table for the specific AFI/SAFI in the given VRF.
387 */
388 static void
389 zebra_vrf_table_create (struct zebra_vrf *zvrf, afi_t afi, safi_t safi)
390 {
391 rib_table_info_t *info;
392 struct route_table *table;
393
394 assert (!zvrf->table[afi][safi]);
395
396 table = route_table_init_with_delegate (&zebra_rtable_delegate);
397 zvrf->table[afi][safi] = table;
398
399 info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
400 info->zvrf = zvrf;
401 info->afi = afi;
402 info->safi = safi;
403 table->info = info;
404 }
405
406 /* Allocate new zebra VRF. */
407 struct zebra_vrf *
408 zebra_vrf_alloc (void)
409 {
410 struct zebra_vrf *zvrf;
411 afi_t afi;
412 safi_t safi;
413
414 zvrf = XCALLOC (MTYPE_ZEBRA_VRF, sizeof (struct zebra_vrf));
415
416 for (afi = AFI_IP; afi <= AFI_IP6; afi++)
417 {
418 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
419 {
420 zebra_vrf_table_create (zvrf, afi, safi);
421 zvrf->stable[afi][safi] =
422 route_table_init_with_delegate (&zebra_stable_delegate);
423 }
424
425 zvrf->rnh_table[afi] =
426 route_table_init_with_delegate (&zebra_rnhtable_delegate);
427 zvrf->import_check_table[afi] =
428 route_table_init_with_delegate (&zebra_rnhtable_delegate);
429 }
430
431 zebra_mpls_init_tables (zvrf);
432
433 return zvrf;
434 }
435
436 /* Lookup VRF by identifier. */
437 struct zebra_vrf *
438 zebra_vrf_lookup_by_id (vrf_id_t vrf_id)
439 {
440 return vrf_info_lookup (vrf_id);
441 }
442
443 /* Lookup VRF by name. */
444 struct zebra_vrf *
445 zebra_vrf_lookup_by_name (const char *name)
446 {
447 struct vrf *vrf;
448
449 if (!name)
450 name = VRF_DEFAULT_NAME;
451
452 vrf = vrf_lookup_by_name (name);
453 if (vrf)
454 return ((struct zebra_vrf *) vrf->info);
455
456 return NULL;
457 }
458
459 /* Lookup the routing table in an enabled VRF. */
460 struct route_table *
461 zebra_vrf_table (afi_t afi, safi_t safi, vrf_id_t vrf_id)
462 {
463 struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id);
464
465 if (!zvrf)
466 return NULL;
467
468 if (afi >= AFI_MAX || safi >= SAFI_MAX)
469 return NULL;
470
471 return zvrf->table[afi][safi];
472 }
473
474 /* Lookup the static routing table in a VRF. */
475 struct route_table *
476 zebra_vrf_static_table (afi_t afi, safi_t safi, struct zebra_vrf *zvrf)
477 {
478 if (!zvrf)
479 return NULL;
480
481 if (afi >= AFI_MAX || safi >= SAFI_MAX)
482 return NULL;
483
484 return zvrf->stable[afi][safi];
485 }
486
487 struct route_table *
488 zebra_vrf_other_route_table (afi_t afi, u_int32_t table_id, vrf_id_t vrf_id)
489 {
490 struct zebra_vrf *zvrf;
491 rib_table_info_t *info;
492 struct route_table *table;
493
494 zvrf = vrf_info_lookup (vrf_id);
495 if (! zvrf)
496 return NULL;
497
498 if(afi >= AFI_MAX)
499 return NULL;
500
501 if (table_id >= ZEBRA_KERNEL_TABLE_MAX)
502 return NULL;
503
504 if ((vrf_id == VRF_DEFAULT) && (table_id != RT_TABLE_MAIN) && (table_id != zebrad.rtm_table_default))
505 {
506 if (zvrf->other_table[afi][table_id] == NULL)
507 {
508 table = route_table_init();
509 info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
510 info->zvrf = zvrf;
511 info->afi = afi;
512 info->safi = SAFI_UNICAST;
513 table->info = info;
514 zvrf->other_table[afi][table_id] = table;
515 }
516
517 return (zvrf->other_table[afi][table_id]);
518 }
519
520 return zvrf->table[afi][SAFI_UNICAST];
521 }
522
523 static int
524 vrf_config_write (struct vty *vty)
525 {
526 struct vrf *vrf;
527 struct zebra_vrf *zvrf;
528
529 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
530 {
531 zvrf = vrf->info;
532 if (! zvrf || strcmp (zvrf_name (zvrf), VRF_DEFAULT_NAME))
533 {
534 vty_out (vty, "vrf %s%s", zvrf_name (zvrf), VTY_NEWLINE);
535 vty_out (vty, "!%s", VTY_NEWLINE);
536 }
537 }
538 return 0;
539 }
540
541 /* Zebra VRF initialization. */
542 void
543 zebra_vrf_init (void)
544 {
545 vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
546 vrf_add_hook (VRF_ENABLE_HOOK, zebra_vrf_enable);
547 vrf_add_hook (VRF_DISABLE_HOOK, zebra_vrf_disable);
548 vrf_add_hook (VRF_DELETE_HOOK, zebra_vrf_delete);
549
550 vrf_init ();
551 vrf_cmd_init (vrf_config_write);
552 }