]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vrf.c
Revert "zebra: move rtadv service from zrouter to zvrf"
[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 #include "vrf.h"
29 #include "vty.h"
30
31 #include "zebra/zebra_router.h"
32 #include "zebra/debug.h"
33 #include "zebra/zapi_msg.h"
34 #include "zebra/rib.h"
35 #include "zebra/zebra_vrf.h"
36 #include "zebra/zebra_rnh.h"
37 #include "zebra/router-id.h"
38 #include "zebra/zebra_memory.h"
39 #include "zebra/interface.h"
40 #include "zebra/zebra_mpls.h"
41 #include "zebra/zebra_vxlan.h"
42 #include "zebra/zebra_netns_notify.h"
43 #include "zebra/zebra_routemap.h"
44
45 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
46 safi_t safi);
47 static void zebra_rnhtable_node_cleanup(struct route_table *table,
48 struct route_node *node);
49
50 DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table");
51
52 /* VRF information update. */
53 static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
54 {
55 struct listnode *node, *nnode;
56 struct zserv *client;
57
58 if (IS_ZEBRA_DEBUG_EVENT)
59 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
60
61 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
62 zsend_vrf_add(client, zvrf);
63 }
64
65 static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
66 {
67 struct listnode *node, *nnode;
68 struct zserv *client;
69
70 if (IS_ZEBRA_DEBUG_EVENT)
71 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
72
73 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
74 zsend_vrf_delete(client, zvrf);
75 }
76
77 void zebra_vrf_update_all(struct zserv *client)
78 {
79 struct vrf *vrf;
80
81 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
82 if (vrf->vrf_id != VRF_UNKNOWN)
83 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
84 }
85 }
86
87 /* Callback upon creating a new VRF. */
88 static int zebra_vrf_new(struct vrf *vrf)
89 {
90 struct zebra_vrf *zvrf;
91
92 if (IS_ZEBRA_DEBUG_EVENT)
93 zlog_info("VRF %s created, id %u", vrf->name, vrf->vrf_id);
94
95 zvrf = zebra_vrf_alloc();
96 vrf->info = zvrf;
97 zvrf->vrf = vrf;
98
99 otable_init(&zvrf->other_tables);
100
101 router_id_init(zvrf);
102 return 0;
103 }
104
105 /* Callback upon enabling a VRF. */
106 static int zebra_vrf_enable(struct vrf *vrf)
107 {
108 struct zebra_vrf *zvrf = vrf->info;
109 struct route_table *table;
110 afi_t afi;
111 safi_t safi;
112
113 assert(zvrf);
114 if (IS_ZEBRA_DEBUG_EVENT)
115 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
116 zvrf_id(zvrf));
117
118 if (vrf_is_backend_netns())
119 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
120 else
121 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
122 /* Inform clients that the VRF is now active. This is an
123 * add for the clients.
124 */
125
126 zebra_vrf_add_update(zvrf);
127 /* Allocate tables */
128 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
129 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
130 zebra_vrf_table_create(zvrf, afi, safi);
131
132 table = route_table_init();
133 table->cleanup = zebra_rnhtable_node_cleanup;
134 zvrf->rnh_table[afi] = table;
135
136 table = route_table_init();
137 table->cleanup = zebra_rnhtable_node_cleanup;
138 zvrf->import_check_table[afi] = table;
139 }
140
141 /* Kick off any VxLAN-EVPN processing. */
142 zebra_vxlan_vrf_enable(zvrf);
143
144 return 0;
145 }
146
147 /* Callback upon disabling a VRF. */
148 static int zebra_vrf_disable(struct vrf *vrf)
149 {
150 struct zebra_vrf *zvrf = vrf->info;
151 struct interface *ifp;
152 afi_t afi;
153 safi_t safi;
154 unsigned i;
155
156 assert(zvrf);
157 if (IS_ZEBRA_DEBUG_EVENT)
158 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
159 zvrf_id(zvrf));
160
161 /* Stop any VxLAN-EVPN processing. */
162 zebra_vxlan_vrf_disable(zvrf);
163
164 /* Inform clients that the VRF is now inactive. This is a
165 * delete for the clients.
166 */
167 zebra_vrf_delete_update(zvrf);
168
169 /* If asked to retain routes, there's nothing more to do. */
170 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
171 return 0;
172
173 /* Remove all routes. */
174 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
175 route_table_finish(zvrf->rnh_table[afi]);
176 zvrf->rnh_table[afi] = NULL;
177 route_table_finish(zvrf->import_check_table[afi]);
178 zvrf->import_check_table[afi] = NULL;
179
180 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
181 rib_close_table(zvrf->table[afi][safi]);
182 }
183
184 /* Cleanup Vxlan, MPLS and PW tables. */
185 zebra_vxlan_cleanup_tables(zvrf);
186 zebra_mpls_cleanup_tables(zvrf);
187 zebra_pw_exit(zvrf);
188
189 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
190 */
191 FOR_ALL_INTERFACES (vrf, ifp)
192 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
193
194 /* clean-up work queues */
195 for (i = 0; i < MQ_SIZE; i++) {
196 struct listnode *lnode, *nnode;
197 struct route_node *rnode;
198 rib_dest_t *dest;
199
200 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
201 rnode)) {
202 dest = rib_dest_from_rnode(rnode);
203 if (dest && rib_dest_vrf(dest) == zvrf) {
204 route_unlock_node(rnode);
205 list_delete_node(zrouter.mq->subq[i], lnode);
206 zrouter.mq->size--;
207 }
208 }
209 }
210
211 /* Cleanup (free) routing tables and NHT tables. */
212 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
213 /*
214 * Set the table pointer to NULL as that
215 * we no-longer need a copy of it, nor do we
216 * own this data, the zebra_router structure
217 * owns these tables. Once we've cleaned up the
218 * table, see rib_close_table above
219 * we no-longer need this pointer.
220 */
221 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
222 zebra_router_release_table(zvrf, zvrf->table_id, afi,
223 safi);
224 zvrf->table[afi][safi] = NULL;
225 }
226 }
227
228 return 0;
229 }
230
231 static int zebra_vrf_delete(struct vrf *vrf)
232 {
233 struct zebra_vrf *zvrf = vrf->info;
234 struct other_route_table *otable;
235 struct route_table *table;
236 afi_t afi;
237 safi_t safi;
238 unsigned i;
239
240 assert(zvrf);
241 if (IS_ZEBRA_DEBUG_EVENT)
242 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
243 zvrf_id(zvrf));
244
245 /* clean-up work queues */
246 for (i = 0; i < MQ_SIZE; i++) {
247 struct listnode *lnode, *nnode;
248 struct route_node *rnode;
249 rib_dest_t *dest;
250
251 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
252 rnode)) {
253 dest = rib_dest_from_rnode(rnode);
254 if (dest && rib_dest_vrf(dest) == zvrf) {
255 route_unlock_node(rnode);
256 list_delete_node(zrouter.mq->subq[i], lnode);
257 zrouter.mq->size--;
258 }
259 }
260 }
261
262 /* Free Vxlan and MPLS. */
263 zebra_vxlan_close_tables(zvrf);
264 zebra_mpls_close_tables(zvrf);
265
266 /* release allocated memory */
267 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
268 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
269 table = zvrf->table[afi][safi];
270 if (table) {
271 zebra_router_release_table(zvrf, zvrf->table_id,
272 afi, safi);
273 zvrf->table[afi][safi] = NULL;
274 }
275 }
276
277 if (zvrf->rnh_table[afi])
278 route_table_finish(zvrf->rnh_table[afi]);
279 if (zvrf->import_check_table[afi])
280 route_table_finish(zvrf->import_check_table[afi]);
281 }
282
283 otable = otable_pop(&zvrf->other_tables);
284 while (otable) {
285 zebra_router_release_table(zvrf, otable->table_id,
286 otable->afi, otable->safi);
287 XFREE(MTYPE_OTHER_TABLE, otable);
288
289 otable = otable_pop(&zvrf->other_tables);
290 }
291
292 /* Cleanup EVPN states for vrf */
293 zebra_vxlan_vrf_delete(zvrf);
294
295 list_delete_all_node(zvrf->rid_all_sorted_list);
296 list_delete_all_node(zvrf->rid_lo_sorted_list);
297
298 otable_fini(&zvrf->other_tables);
299 XFREE(MTYPE_ZEBRA_VRF, zvrf);
300 vrf->info = NULL;
301
302 return 0;
303 }
304
305 static int zebra_vrf_update(struct vrf *vrf)
306 {
307 struct zebra_vrf *zvrf = vrf->info;
308
309 assert(zvrf);
310 if (IS_ZEBRA_DEBUG_EVENT)
311 zlog_debug("VRF %s id %u, name updated", vrf->name,
312 zvrf_id(zvrf));
313 zebra_vrf_add_update(zvrf);
314 return 0;
315 }
316
317
318 /* Return if this VRF has any FRR configuration or not.
319 * IMPORTANT: This function needs to be updated when additional configuration
320 * is added for a VRF.
321 */
322 int zebra_vrf_has_config(struct zebra_vrf *zvrf)
323 {
324 /* EVPN L3-VNI? */
325 if (zvrf->l3vni)
326 return 1;
327
328 return 0;
329 }
330
331 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
332 * NOTE: Table-id is relevant on two modes:
333 * - case VRF backend is default : on default VRF only
334 * - case VRF backend is netns : on all VRFs
335 */
336 struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
337 vrf_id_t vrf_id,
338 uint32_t table_id)
339 {
340 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
341 struct other_route_table ort, *otable;
342 struct route_table *table;
343
344 if (!zvrf)
345 return NULL;
346
347 if (afi >= AFI_MAX || safi >= SAFI_MAX)
348 return NULL;
349
350 if (table_id == zvrf->table_id)
351 return zebra_vrf_table(afi, safi, vrf_id);
352
353 ort.afi = afi;
354 ort.safi = safi;
355 ort.table_id = table_id;
356 otable = otable_find(&zvrf->other_tables, &ort);
357 if (otable)
358 return otable->table;
359
360 table = zebra_router_get_table(zvrf, table_id, afi, safi);
361
362 otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
363 otable->afi = afi;
364 otable->safi = safi;
365 otable->table_id = table_id;
366 otable->table = table;
367 otable_add(&zvrf->other_tables, otable);
368
369 return table;
370 }
371
372 void zebra_rtable_node_cleanup(struct route_table *table,
373 struct route_node *node)
374 {
375 struct route_entry *re, *next;
376
377 RNODE_FOREACH_RE_SAFE (node, re, next) {
378 rib_unlink(node, re);
379 }
380
381 if (node->info) {
382 rib_dest_t *dest = node->info;
383
384 rnh_list_fini(&dest->nht);
385 XFREE(MTYPE_RIB_DEST, node->info);
386 }
387 }
388
389 static void zebra_rnhtable_node_cleanup(struct route_table *table,
390 struct route_node *node)
391 {
392 if (node->info)
393 zebra_free_rnh(node->info);
394 }
395
396 /*
397 * Create a routing table for the specific AFI/SAFI in the given VRF.
398 */
399 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
400 safi_t safi)
401 {
402 struct route_node *rn;
403 struct prefix p;
404
405 assert(!zvrf->table[afi][safi]);
406
407 zvrf->table[afi][safi] =
408 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
409
410 memset(&p, 0, sizeof(p));
411 p.family = afi2family(afi);
412
413 rn = srcdest_rnode_get(zvrf->table[afi][safi], &p, NULL);
414 zebra_rib_create_dest(rn);
415 }
416
417 /* Allocate new zebra VRF. */
418 struct zebra_vrf *zebra_vrf_alloc(void)
419 {
420 struct zebra_vrf *zvrf;
421
422 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
423
424 zebra_vxlan_init_tables(zvrf);
425 zebra_mpls_init_tables(zvrf);
426 zebra_pw_init(zvrf);
427 zvrf->table_id = RT_TABLE_MAIN;
428 /* by default table ID is default one */
429 return zvrf;
430 }
431
432 /* Lookup VRF by identifier. */
433 struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
434 {
435 return vrf_info_lookup(vrf_id);
436 }
437
438 /* Lookup VRF by name. */
439 struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
440 {
441 struct vrf *vrf;
442
443 if (!name)
444 name = VRF_DEFAULT_NAME;
445
446 vrf = vrf_lookup_by_name(name);
447 if (vrf)
448 return ((struct zebra_vrf *)vrf->info);
449
450 return NULL;
451 }
452
453 /* Lookup the routing table in an enabled VRF. */
454 struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
455 {
456 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
457
458 if (!zvrf)
459 return NULL;
460
461 if (afi >= AFI_MAX || safi >= SAFI_MAX)
462 return NULL;
463
464 return zvrf->table[afi][safi];
465 }
466
467 static int vrf_config_write(struct vty *vty)
468 {
469 struct vrf *vrf;
470 struct zebra_vrf *zvrf;
471
472 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
473 zvrf = vrf->info;
474
475 if (!zvrf)
476 continue;
477
478 if (zvrf_id(zvrf) == VRF_DEFAULT) {
479 if (zvrf->l3vni)
480 vty_out(vty, "vni %u\n", zvrf->l3vni);
481 } else {
482 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
483 if (zvrf->l3vni)
484 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
485 is_l3vni_for_prefix_routes_only(
486 zvrf->l3vni)
487 ? " prefix-routes-only"
488 : "");
489 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
490 }
491
492 zebra_routemap_config_write_protocol(vty, zvrf);
493
494 if (zvrf_id(zvrf) != VRF_DEFAULT)
495 vty_endframe(vty, " exit-vrf\n!\n");
496 else
497 vty_out(vty, "!\n");
498 }
499 return 0;
500 }
501
502 /* Zebra VRF initialization. */
503 void zebra_vrf_init(void)
504 {
505 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
506 zebra_vrf_delete, zebra_vrf_update);
507
508 vrf_cmd_init(vrf_config_write, &zserv_privs);
509 }