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