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