summaryrefslogtreecommitdiffstats
path: root/zebra/zebra_vrf.c
blob: e1623477267f35419e0a80e36f52a47a2c717579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
 * Copyright (C) 2016 CumulusNetworks
 *                    Donald Sharp
 *
 * This file is part of Quagga
 *
 * Quagga is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * Quagga is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <zebra.h>

#include "log.h"
#include "linklist.h"
#include "command.h"
#include "memory.h"
#include "srcdest_table.h"
#include "vrf.h"
#include "vty.h"

#include "zebra/zebra_router.h"
#include "zebra/rtadv.h"
#include "zebra/debug.h"
#include "zebra/zapi_msg.h"
#include "zebra/rib.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_rnh.h"
#include "zebra/router-id.h"
#include "zebra/interface.h"
#include "zebra/zebra_mpls.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zebra_netns_notify.h"
#include "zebra/zebra_routemap.h"
#ifndef VTYSH_EXTRACT_PL
#include "zebra/zebra_vrf_clippy.c"
#endif
#include "zebra/table_manager.h"

static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
				   safi_t safi);
static void zebra_rnhtable_node_cleanup(struct route_table *table,
					struct route_node *node);

DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_VRF, "ZEBRA VRF");
DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table");

/* VRF information update. */
static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
{
	struct listnode *node, *nnode;
	struct zserv *client;

	if (IS_ZEBRA_DEBUG_EVENT)
		zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));

	for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
		/* Do not send unsolicited messages to synchronous clients. */
		if (client->synchronous)
			continue;

		zsend_vrf_add(client, zvrf);
	}
}

static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
{
	struct listnode *node, *nnode;
	struct zserv *client;

	if (IS_ZEBRA_DEBUG_EVENT)
		zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));

	for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
		/* Do not send unsolicited messages to synchronous clients. */
		if (client->synchronous)
			continue;

		zsend_vrf_delete(client, zvrf);
	}
}

void zebra_vrf_update_all(struct zserv *client)
{
	struct vrf *vrf;

	RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
		if (vrf->vrf_id != VRF_UNKNOWN)
			zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
	}
}

/* Callback upon creating a new VRF. */
static int zebra_vrf_new(struct vrf *vrf)
{
	struct zebra_vrf *zvrf;

	if (IS_ZEBRA_DEBUG_EVENT)
		zlog_debug("VRF %s created, id %u", vrf->name, vrf->vrf_id);

	zvrf = zebra_vrf_alloc(vrf);
	if (!vrf_is_backend_netns())
		zvrf->zns = zebra_ns_lookup(NS_DEFAULT);

	otable_init(&zvrf->other_tables);

	router_id_init(zvrf);

	/* Initiate Table Manager per ZNS */
	table_manager_enable(zvrf);

	return 0;
}

/* Callback upon enabling a VRF. */
static int zebra_vrf_enable(struct vrf *vrf)
{
	struct zebra_vrf *zvrf = vrf->info;
	struct route_table *table;
	afi_t afi;
	safi_t safi;

	assert(zvrf);
	if (IS_ZEBRA_DEBUG_EVENT)
		zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
			   zvrf_id(zvrf));

	if (vrf_is_backend_netns())
		zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
	else
		zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
#if defined(HAVE_RTADV)
	rtadv_vrf_init(zvrf);
#endif

	/* Inform clients that the VRF is now active. This is an
	 * add for the clients.
	 */

	zebra_vrf_add_update(zvrf);
	/* Allocate tables */
	for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
		for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
			zebra_vrf_table_create(zvrf, afi, safi);

		table = route_table_init();
		table->cleanup = zebra_rnhtable_node_cleanup;
		zvrf->rnh_table[afi] = table;

		table = route_table_init();
		table->cleanup = zebra_rnhtable_node_cleanup;
		zvrf->import_check_table[afi] = table;
	}

	/* Kick off any VxLAN-EVPN processing. */
	zebra_vxlan_vrf_enable(zvrf);

	return 0;
}

/* Callback upon disabling a VRF. */
static int zebra_vrf_disable(struct vrf *vrf)
{
	struct zebra_vrf *zvrf = vrf->info;
	struct interface *ifp;
	afi_t afi;
	safi_t safi;
	unsigned i;

	assert(zvrf);
	if (IS_ZEBRA_DEBUG_EVENT)
		zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
			   zvrf_id(zvrf));

	table_manager_disable(zvrf);

	/* Stop any VxLAN-EVPN processing. */
	zebra_vxlan_vrf_disable(zvrf);

#if defined(HAVE_RTADV)
	rtadv_vrf_terminate(zvrf);
#endif

	/* Inform clients that the VRF is now inactive. This is a
	 * delete for the clients.
	 */
	zebra_vrf_delete_update(zvrf);

	/* If asked to retain routes, there's nothing more to do. */
	if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
		return 0;

	/* Remove all routes. */
	for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
		route_table_finish(zvrf->rnh_table[afi]);
		zvrf->rnh_table[afi] = NULL;
		route_table_finish(zvrf->import_check_table[afi]);
		zvrf->import_check_table[afi] = NULL;

		for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
			rib_close_table(zvrf->table[afi][safi]);
	}

	/* Cleanup Vxlan, MPLS and PW tables. */
	zebra_vxlan_cleanup_tables(zvrf);
	zebra_mpls_cleanup_tables(zvrf);
	zebra_pw_exit(zvrf);

	/* Remove link-local IPv4 addresses created for BGP unnumbered peering.
	 */
	FOR_ALL_INTERFACES (vrf, ifp)
		if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);

	/* clean-up work queues */
	for (i = 0; i < MQ_SIZE; i++) {
		struct listnode *lnode, *nnode;
		struct route_node *rnode;
		rib_dest_t *dest;

		for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
				       rnode)) {
			dest = rib_dest_from_rnode(rnode);
			if (dest && rib_dest_vrf(dest) == zvrf) {
				route_unlock_node(rnode);
				list_delete_node(zrouter.mq->subq[i], lnode);
				zrouter.mq->size--;
			}
		}
	}

	/* Cleanup (free) routing tables and NHT tables. */
	for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
		/*
		 * Set the table pointer to NULL as that
		 * we no-longer need a copy of it, nor do we
		 * own this data, the zebra_router structure
		 * owns these tables.  Once we've cleaned up the
		 * table, see rib_close_table above
		 * we no-longer need this pointer.
		 */
		for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
			zebra_router_release_table(zvrf, zvrf->table_id, afi,
						   safi);
			zvrf->table[afi][safi] = NULL;
		}
	}

	return 0;
}

static int zebra_vrf_delete(struct vrf *vrf)
{
	struct zebra_vrf *zvrf = vrf->info;
	struct other_route_table *otable;
	struct route_table *table;
	afi_t afi;
	safi_t safi;
	unsigned i;

	assert(zvrf);
	if (IS_ZEBRA_DEBUG_EVENT)
		zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
			   zvrf_id(zvrf));

	/* clean-up work queues */
	for (i = 0; i < MQ_SIZE; i++) {
		struct listnode *lnode, *nnode;
		struct route_node *rnode;
		rib_dest_t *dest;

		for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
				       rnode)) {
			dest = rib_dest_from_rnode(rnode);
			if (dest && rib_dest_vrf(dest) == zvrf) {
				route_unlock_node(rnode);
				list_delete_node(zrouter.mq->subq[i], lnode);
				zrouter.mq->size--;
			}
		}
	}

	/* Free Vxlan and MPLS. */
	zebra_vxlan_close_tables(zvrf);
	zebra_mpls_close_tables(zvrf);

	/* release allocated memory */
	for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
		for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
			table = zvrf->table[afi][safi];
			if (table) {
				zebra_router_release_table(zvrf, zvrf->table_id,
							   afi, safi);
				zvrf->table[afi][safi] = NULL;
			}
		}

		if (zvrf->rnh_table[afi])
			route_table_finish(zvrf->rnh_table[afi]);
		if (zvrf->import_check_table[afi])
			route_table_finish(zvrf->import_check_table[afi]);
	}

	otable = otable_pop(&zvrf->other_tables);
	while (otable) {
		zebra_router_release_table(zvrf, otable->table_id,
					   otable->afi, otable->safi);
		XFREE(MTYPE_OTHER_TABLE, otable);

		otable = otable_pop(&zvrf->other_tables);
	}

	/* Cleanup EVPN states for vrf */
	zebra_vxlan_vrf_delete(zvrf);

	list_delete_all_node(zvrf->rid_all_sorted_list);
	list_delete_all_node(zvrf->rid_lo_sorted_list);

	list_delete_all_node(zvrf->rid6_all_sorted_list);
	list_delete_all_node(zvrf->rid6_lo_sorted_list);

	otable_fini(&zvrf->other_tables);
	XFREE(MTYPE_ZEBRA_VRF, zvrf);
	vrf->info = NULL;

	return 0;
}

static int zebra_vrf_update(struct vrf *vrf)
{
	struct zebra_vrf *zvrf = vrf->info;

	assert(zvrf);
	if (IS_ZEBRA_DEBUG_EVENT)
		zlog_debug("VRF %s id %u, name updated", vrf->name,
			   zvrf_id(zvrf));
	zebra_vrf_add_update(zvrf);
	return 0;
}

/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
 * NOTE: Table-id is relevant on two modes:
 * - case VRF backend is default : on default VRF only
 * - case VRF backend is netns : on all VRFs
 */
struct route_table *zebra_vrf_lookup_table_with_table_id(afi_t afi, safi_t safi,
							 vrf_id_t vrf_id,
							 uint32_t table_id)
{
	struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
	struct other_route_table ort, *otable;

	if (!zvrf)
		return NULL;

	if (afi >= AFI_MAX || safi >= SAFI_MAX)
		return NULL;

	if (table_id == zvrf->table_id)
		return zebra_vrf_table(afi, safi, vrf_id);

	ort.afi = afi;
	ort.safi = safi;
	ort.table_id = table_id;
	otable = otable_find(&zvrf->other_tables, &ort);

	if (otable)
		return otable->table;

	return NULL;
}

struct route_table *zebra_vrf_get_table_with_table_id(afi_t afi, safi_t safi,
						      vrf_id_t vrf_id,
						      uint32_t table_id)
{
	struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
	struct other_route_table *otable;
	struct route_table *table;

	table = zebra_vrf_lookup_table_with_table_id(afi, safi, vrf_id,
						     table_id);

	if (table)
		goto done;

	/* Create it as an `other` table */
	table = zebra_router_get_table(zvrf, table_id, afi, safi);

	otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
	otable->afi = afi;
	otable->safi = safi;
	otable->table_id = table_id;
	otable->table = table;
	otable_add(&zvrf->other_tables, otable);

done:
	return table;
}

static void zebra_rnhtable_node_cleanup(struct route_table *table,
					struct route_node *node)
{
	if (node->info)
		zebra_free_rnh(node->info);
}

/*
 * Create a routing table for the specific AFI/SAFI in the given VRF.
 */
static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
				   safi_t safi)
{
	struct route_node *rn;
	struct prefix p;

	assert(!zvrf->table[afi][safi]);

	zvrf->table[afi][safi] =
		zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);

	memset(&p, 0, sizeof(p));
	p.family = afi2family(afi);

	rn = srcdest_rnode_get(zvrf->table[afi][safi], &p, NULL);
	zebra_rib_create_dest(rn);
}

/* Allocate new zebra VRF. */
struct zebra_vrf *zebra_vrf_alloc(struct vrf *vrf)
{
	struct zebra_vrf *zvrf;

	zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));

	zvrf->vrf = vrf;
	vrf->info = zvrf;

	zebra_vxlan_init_tables(zvrf);
	zebra_mpls_init_tables(zvrf);
	zebra_pw_init(zvrf);
	zvrf->table_id = RT_TABLE_MAIN;
	/* by default table ID is default one */
	return zvrf;
}

/* Lookup VRF by identifier.  */
struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
{
	return vrf_info_lookup(vrf_id);
}

/* Lookup VRF by name.  */
struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
{
	struct vrf *vrf;

	if (!name)
		name = VRF_DEFAULT_NAME;

	vrf = vrf_lookup_by_name(name);
	if (vrf)
		return ((struct zebra_vrf *)vrf->info);

	return NULL;
}

/* Lookup the routing table in an enabled VRF. */
struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
{
	struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);

	if (!zvrf)
		return NULL;

	if (afi >= AFI_MAX || safi >= SAFI_MAX)
		return NULL;

	return zvrf->table[afi][safi];
}

static int vrf_config_write(struct vty *vty)
{
	struct vrf *vrf;
	struct zebra_vrf *zvrf;

	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
		zvrf = vrf->info;

		if (!zvrf)
			continue;

		if (zvrf_id(zvrf) == VRF_DEFAULT) {
			if (zvrf->l3vni)
				vty_out(vty, "vni %u%s\n", zvrf->l3vni,
					is_l3vni_for_prefix_routes_only(
						zvrf->l3vni)
						? " prefix-routes-only"
						: "");
			if (zvrf->zebra_rnh_ip_default_route)
				vty_out(vty, "ip nht resolve-via-default\n");

			if (zvrf->zebra_rnh_ipv6_default_route)
				vty_out(vty, "ipv6 nht resolve-via-default\n");

			if (zvrf->tbl_mgr
			    && (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end))
				vty_out(vty, "ip table range %u %u\n",
					zvrf->tbl_mgr->start,
					zvrf->tbl_mgr->end);
		} else {
			vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
			if (zvrf->l3vni)
				vty_out(vty, " vni %u%s\n", zvrf->l3vni,
					is_l3vni_for_prefix_routes_only(
						zvrf->l3vni)
						? " prefix-routes-only"
						: "");
			zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
			if (zvrf->zebra_rnh_ip_default_route)
				vty_out(vty, " ip nht resolve-via-default\n");

			if (zvrf->zebra_rnh_ipv6_default_route)
				vty_out(vty, " ipv6 nht resolve-via-default\n");

			if (zvrf->tbl_mgr && vrf_is_backend_netns()
			    && (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end))
				vty_out(vty, " ip table range %u %u\n",
					zvrf->tbl_mgr->start,
					zvrf->tbl_mgr->end);
		}


		zebra_routemap_config_write_protocol(vty, zvrf);
		router_id_write(vty, zvrf);

		if (zvrf_id(zvrf) != VRF_DEFAULT)
			vty_endframe(vty, "exit-vrf\n!\n");
		else
			vty_out(vty, "!\n");
	}
	return 0;
}

DEFPY (vrf_netns,
       vrf_netns_cmd,
       "netns NAME$netns_name",
       "Attach VRF to a Namespace\n"
       "The file name in " NS_RUN_DIR ", or a full pathname\n")
{
	char *pathname = ns_netns_pathname(vty, netns_name);
	int ret;

	VTY_DECLVAR_CONTEXT(vrf, vrf);

	if (!pathname)
		return CMD_WARNING_CONFIG_FAILED;

	frr_with_privs(&zserv_privs) {
		ret = vrf_netns_handler_create(vty, vrf, pathname,
					       NS_UNKNOWN,
					       NS_UNKNOWN,
					       NS_UNKNOWN);
	}

	return ret;
}

DEFUN (no_vrf_netns,
       no_vrf_netns_cmd,
       "no netns [NAME]",
       NO_STR
       "Detach VRF from a Namespace\n"
       "The file name in " NS_RUN_DIR ", or a full pathname\n")
{
	struct ns *ns = NULL;

	VTY_DECLVAR_CONTEXT(vrf, vrf);

	if (!vrf_is_backend_netns()) {
		vty_out(vty, "VRF backend is not Netns. Aborting\n");
		return CMD_WARNING_CONFIG_FAILED;
	}
	if (!vrf->ns_ctxt) {
		vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
			vrf->name, vrf->vrf_id);
		return CMD_WARNING_CONFIG_FAILED;
	}

	ns = (struct ns *)vrf->ns_ctxt;

	ns->vrf_ctxt = NULL;
	vrf_disable(vrf);
	/* vrf ID from VRF is necessary for Zebra
	 * so that propagate to other clients is done
	 */
	ns_delete(ns);
	vrf->ns_ctxt = NULL;
	return CMD_SUCCESS;
}

/* Zebra VRF initialization. */
void zebra_vrf_init(void)
{
	vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
		 zebra_vrf_delete, zebra_vrf_update);

	vrf_cmd_init(vrf_config_write);

	if (vrf_is_backend_netns() && ns_have_netns()) {
		/* Install NS commands. */
		install_element(VRF_NODE, &vrf_netns_cmd);
		install_element(VRF_NODE, &no_vrf_netns_cmd);
	}
}