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