summaryrefslogtreecommitdiffstats
path: root/bgpd/rfapi/rfapi_ap.c
blob: fcc6168cfaf9834a0f1162349274eb56ee170bb5 (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
/*
 *
 * Copyright 2009-2016, LabN Consulting, L.L.C.
 *
 *
 * This program 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
 * of the License, or (at your option) any later version.
 *
 * This program 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 "lib/zebra.h"
#include "lib/prefix.h"
#include "lib/agg_table.h"
#include "lib/vty.h"
#include "lib/memory.h"
#include "lib/routemap.h"
#include "lib/log.h"
#include "lib/linklist.h"
#include "lib/command.h"
#include "lib/stream.h"

#include "bgpd/bgpd.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_attr.h"

#include "bgpd/rfapi/bgp_rfapi_cfg.h"
#include "bgpd/rfapi/rfapi.h"
#include "bgpd/rfapi/rfapi_backend.h"

#include "bgpd/bgp_route.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_advertise.h"

#include "bgpd/rfapi/rfapi_import.h"
#include "bgpd/rfapi/rfapi_private.h"
#include "bgpd/rfapi/rfapi_monitor.h"
#include "bgpd/rfapi/rfapi_vty.h"
#include "bgpd/rfapi/vnc_export_bgp.h"
#include "bgpd/rfapi/vnc_export_bgp_p.h"
#include "bgpd/rfapi/vnc_zebra.h"
#include "bgpd/rfapi/vnc_import_bgp.h"
#include "bgpd/rfapi/rfapi_rib.h"

#include "bgpd/rfapi/rfapi_ap.h"
#include "bgpd/rfapi/vnc_debug.h"

/*
 * Per-NVE Advertised prefixes
 *
 * We maintain a list of prefixes advertised by each NVE.
 * There are two indices: by prefix and by lifetime.
 *
 * BY-PREFIX skiplist
 *
 *  key:	ptr to struct prefix (when storing, point to prefix that
 *		is part of rfapi_adb).
 *
 *  value:	ptr to struct rfapi_adb
 *
 * BY-LIFETIME skiplist
 *
 *  key:	ptr to struct rfapi_adb
 *  value:	ptr to struct rfapi_adb
 *
 */

/*
 * Skiplist sort function that sorts first according to lifetime
 * and then according to adb pointer value. The adb pointer
 * is used to spread out the sort for adbs with the same lifetime
 * and thereby make the skip list operations more efficient.
 */
static int sl_adb_lifetime_cmp(const void *adb1, const void *adb2)
{
	const struct rfapi_adb *a1 = adb1;
	const struct rfapi_adb *a2 = adb2;

	if (a1->lifetime < a2->lifetime)
		return -1;
	if (a1->lifetime > a2->lifetime)
		return 1;

	if (a1 < a2)
		return -1;
	if (a1 > a2)
		return 1;

	return 0;
}

void rfapiApInit(struct rfapi_advertised_prefixes *ap)
{
	ap->ipN_by_prefix = skiplist_new(0, rfapi_rib_key_cmp, NULL);
	ap->ip0_by_ether = skiplist_new(0, rfapi_rib_key_cmp, NULL);
	ap->by_lifetime = skiplist_new(0, sl_adb_lifetime_cmp, NULL);
}

void rfapiApRelease(struct rfapi_advertised_prefixes *ap)
{
	struct rfapi_adb *adb;

	/* Free ADBs and lifetime items */
	while (0 == skiplist_first(ap->by_lifetime, NULL, (void **)&adb)) {
		rfapiAdbFree(adb);
		skiplist_delete_first(ap->by_lifetime);
	}

	while (0 == skiplist_delete_first(ap->ipN_by_prefix))
		;
	while (0 == skiplist_delete_first(ap->ip0_by_ether))
		;

	/* Free lists */
	skiplist_free(ap->ipN_by_prefix);
	skiplist_free(ap->ip0_by_ether);
	skiplist_free(ap->by_lifetime);

	ap->ipN_by_prefix = NULL;
	ap->ip0_by_ether = NULL;
	ap->by_lifetime = NULL;
}

int rfapiApCount(struct rfapi_descriptor *rfd)
{
	if (!rfd->advertised.by_lifetime)
		return 0;

	return skiplist_count(rfd->advertised.by_lifetime);
}

int rfapiApCountAll(struct bgp *bgp)
{
	struct rfapi *h;
	struct listnode *node;
	struct rfapi_descriptor *rfd;
	int total = 0;

	h = bgp->rfapi;
	if (h) {
		for (ALL_LIST_ELEMENTS_RO(&h->descriptors, node, rfd)) {
			total += rfapiApCount(rfd);
		}
	}
	return total;
}


void rfapiApReadvertiseAll(struct bgp *bgp, struct rfapi_descriptor *rfd)
{
	struct rfapi_adb *adb;
	void *cursor = NULL;
	int rc;

	for (rc = skiplist_next(rfd->advertised.by_lifetime, NULL,
				(void **)&adb, &cursor);
	     rc == 0; rc = skiplist_next(rfd->advertised.by_lifetime, NULL,
					 (void **)&adb, &cursor)) {

		struct prefix_rd prd;
		uint32_t local_pref = rfp_cost_to_localpref(adb->cost);

		prd = rfd->rd;
		prd.family = AF_UNSPEC;
		prd.prefixlen = 64;

		/*
		 * TBD this is not quite right. When pfx_ip is 0/32 or 0/128,
		 * we need to substitute the VN address as the prefix
		 */
		add_vnc_route(rfd, bgp, SAFI_MPLS_VPN, &adb->u.s.prefix_ip,
			      &prd,	  /* RD to use (0 for ENCAP) */
			      &rfd->vn_addr, /* nexthop */
			      &local_pref, &adb->lifetime, NULL,
			      NULL, /* struct rfapi_un_option */
			      NULL, /* struct rfapi_vn_option */
			      rfd->rt_export_list, NULL, /* med */
			      NULL, ZEBRA_ROUTE_BGP, BGP_ROUTE_RFP, 0);
	}
}

void rfapiApWithdrawAll(struct bgp *bgp, struct rfapi_descriptor *rfd)
{
	struct rfapi_adb *adb;
	void *cursor;
	int rc;


	cursor = NULL;
	for (rc = skiplist_next(rfd->advertised.by_lifetime, NULL,
				(void **)&adb, &cursor);
	     rc == 0; rc = skiplist_next(rfd->advertised.by_lifetime, NULL,
					 (void **)&adb, &cursor)) {

		struct prefix pfx_vn_buf;
		struct prefix *pfx_ip;

		if (!(RFAPI_0_PREFIX(&adb->u.s.prefix_ip)
		      && RFAPI_HOST_PREFIX(&adb->u.s.prefix_ip))) {

			pfx_ip = &adb->u.s.prefix_ip;

		} else {

			pfx_ip = NULL;

			/*
			 * 0/32 or 0/128 => mac advertisement
			 */
			if (rfapiRaddr2Qprefix(&rfd->vn_addr, &pfx_vn_buf)) {
				/*
				 * Bad: it means we can't delete the route
				 */
				vnc_zlog_debug_verbose(
					"%s: BAD: handle has bad vn_addr: skipping",
					__func__);
				continue;
			}
		}

		del_vnc_route(rfd, rfd->peer, bgp, SAFI_MPLS_VPN,
			      pfx_ip ? pfx_ip : &pfx_vn_buf,
			      &adb->u.s.prd, /* RD to use (0 for ENCAP) */
			      ZEBRA_ROUTE_BGP, BGP_ROUTE_RFP, NULL, 0);
	}
}

/*
 * returns nonzero if tunnel readvertisement is needed, 0 otherwise
 */
static int rfapiApAdjustLifetimeStats(
	struct rfapi_descriptor *rfd,
	uint32_t *old_lifetime, /* set if removing/replacing */
	uint32_t *new_lifetime) /* set if replacing/adding */
{
	int advertise = 0;
	int find_max = 0;
	int find_min = 0;

	vnc_zlog_debug_verbose("%s: rfd=%p, pOldLife=%p, pNewLife=%p", __func__,
			       rfd, old_lifetime, new_lifetime);
	if (old_lifetime)
		vnc_zlog_debug_verbose("%s: OldLife=%d", __func__,
				       *old_lifetime);
	if (new_lifetime)
		vnc_zlog_debug_verbose("%s: NewLife=%d", __func__,
				       *new_lifetime);

	if (new_lifetime) {
		/*
		 * Adding new lifetime
		 */
		if (old_lifetime) {
			/*
			 * replacing existing lifetime
			 */


			/* old and new are same */
			if (*old_lifetime == *new_lifetime)
				return 0;

			if (*old_lifetime == rfd->min_prefix_lifetime) {
				find_min = 1;
			}
			if (*old_lifetime == rfd->max_prefix_lifetime) {
				find_max = 1;
			}

			/* no need to search if new value is at or equals
			 * min|max  */
			if (*new_lifetime <= rfd->min_prefix_lifetime) {
				rfd->min_prefix_lifetime = *new_lifetime;
				find_min = 0;
			}
			if (*new_lifetime >= rfd->max_prefix_lifetime) {
				rfd->max_prefix_lifetime = *new_lifetime;
				advertise = 1;
				find_max = 0;
			}

		} else {
			/*
			 * Just adding new lifetime
			 */
			if (*new_lifetime < rfd->min_prefix_lifetime) {
				rfd->min_prefix_lifetime = *new_lifetime;
			}
			if (*new_lifetime > rfd->max_prefix_lifetime) {
				advertise = 1;
				rfd->max_prefix_lifetime = *new_lifetime;
			}
		}
	} else {
		/*
		 * Deleting
		 */

		/*
		 * See if the max prefix lifetime for this NVE has decreased.
		 * The easy optimization: track min & max; walk the table only
		 * if they are different.
		 * The general optimization: index the advertised_prefixes
		 * table by lifetime.
		 *
		 * Note: for a given nve_descriptor, only one of the
		 * advertised_prefixes[] tables will be used: viz., the
		 * address family that matches the VN address.
		 *
		 */
		if (rfd->max_prefix_lifetime == rfd->min_prefix_lifetime) {

			/*
			 * Common case: all lifetimes are the same. Only
			 * thing we need to do here is check if there are
			 * no exported routes left. In that case, reinitialize
			 * the max and min values.
			 */
			if (!rfapiApCount(rfd)) {
				rfd->max_prefix_lifetime = 0;
				rfd->min_prefix_lifetime = UINT32_MAX;
			}


		} else {
			if (old_lifetime) {
				if (*old_lifetime == rfd->min_prefix_lifetime) {
					find_min = 1;
				}
				if (*old_lifetime == rfd->max_prefix_lifetime) {
					find_max = 1;
				}
			}
		}
	}

	if (find_min || find_max) {
		uint32_t min = UINT32_MAX;
		uint32_t max = 0;

		struct rfapi_adb *adb_min;
		struct rfapi_adb *adb_max;

		if (!skiplist_first(rfd->advertised.by_lifetime,
				    (void **)&adb_min, NULL)
		    && !skiplist_last(rfd->advertised.by_lifetime,
				      (void **)&adb_max, NULL)) {

			/*
			 * This should always work
			 */
			min = adb_min->lifetime;
			max = adb_max->lifetime;

		} else {

			void *cursor;
			struct rfapi_rib_key rk;
			struct rfapi_adb *adb;
			int rc;

			vnc_zlog_debug_verbose(
				"%s: walking to find new min/max", __func__);

			cursor = NULL;
			for (rc = skiplist_next(rfd->advertised.ipN_by_prefix,
						(void **)&rk, (void **)&adb,
						&cursor);
			     !rc;
			     rc = skiplist_next(rfd->advertised.ipN_by_prefix,
						(void **)&rk, (void **)&adb,
						&cursor)) {

				uint32_t lt = adb->lifetime;

				if (lt > max)
					max = lt;
				if (lt < min)
					min = lt;
			}
			cursor = NULL;
			for (rc = skiplist_next(rfd->advertised.ip0_by_ether,
						(void **)&rk, (void **)&adb,
						&cursor);
			     !rc;
			     rc = skiplist_next(rfd->advertised.ip0_by_ether,
						(void **)&rk, (void **)&adb,
						&cursor)) {

				uint32_t lt = adb->lifetime;

				if (lt > max)
					max = lt;
				if (lt < min)
					min = lt;
			}
		}

		/*
		 * trigger tunnel route update
		 * but only if we found a VPN route and it had
		 * a lifetime greater than 0
		 */
		if (max && rfd->max_prefix_lifetime != max)
			advertise = 1;
		rfd->max_prefix_lifetime = max;
		rfd->min_prefix_lifetime = min;
	}

	vnc_zlog_debug_verbose("%s: returning advertise=%d, min=%d, max=%d",
			       __func__, advertise, rfd->min_prefix_lifetime,
			       rfd->max_prefix_lifetime);

	return (advertise != 0);
}

/*
 * Return Value
 *
 *	0	No need to advertise tunnel route
 *	non-0	advertise tunnel route
 */
int rfapiApAdd(struct bgp *bgp, struct rfapi_descriptor *rfd,
	       struct prefix *pfx_ip, struct prefix *pfx_eth,
	       struct prefix_rd *prd, uint32_t lifetime, uint8_t cost,
	       struct rfapi_l2address_option *l2o) /* other options TBD */
{
	int rc;
	struct rfapi_adb *adb;
	uint32_t old_lifetime = 0;
	int use_ip0 = 0;
	struct rfapi_rib_key rk;

	rfapi_rib_key_init(pfx_ip, prd, pfx_eth, &rk);
	if (RFAPI_0_PREFIX(pfx_ip) && RFAPI_HOST_PREFIX(pfx_ip)) {
		use_ip0 = 1;
		assert(pfx_eth);
		rc = skiplist_search(rfd->advertised.ip0_by_ether, &rk,
				     (void **)&adb);

	} else {

		/* find prefix in advertised prefixes list */
		rc = skiplist_search(rfd->advertised.ipN_by_prefix, &rk,
				     (void **)&adb);
	}


	if (rc) {
		/* Not found */
		adb = XCALLOC(MTYPE_RFAPI_ADB, sizeof(struct rfapi_adb));
		adb->lifetime = lifetime;
		adb->u.key = rk;

		if (use_ip0) {
			assert(pfx_eth);
			skiplist_insert(rfd->advertised.ip0_by_ether,
					&adb->u.key, adb);
		} else {
			skiplist_insert(rfd->advertised.ipN_by_prefix,
					&adb->u.key, adb);
		}

		skiplist_insert(rfd->advertised.by_lifetime, adb, adb);
	} else {
		old_lifetime = adb->lifetime;
		if (old_lifetime != lifetime) {
			assert(!skiplist_delete(rfd->advertised.by_lifetime,
						adb, NULL));
			adb->lifetime = lifetime;
			assert(!skiplist_insert(rfd->advertised.by_lifetime,
						adb, adb));
		}
	}
	adb->cost = cost;
	if (l2o)
		adb->l2o = *l2o;
	else
		memset(&adb->l2o, 0, sizeof(struct rfapi_l2address_option));

	if (rfapiApAdjustLifetimeStats(rfd, (rc ? NULL : &old_lifetime),
				       &lifetime))
		return 1;

	return 0;
}

/*
 * After this function returns successfully, caller should call
 * rfapiAdjustLifetimeStats() and possibly rfapiTunnelRouteAnnounce()
 */
int rfapiApDelete(struct bgp *bgp, struct rfapi_descriptor *rfd,
		  struct prefix *pfx_ip, struct prefix *pfx_eth,
		  struct prefix_rd *prd, int *advertise_tunnel) /* out */
{
	int rc;
	struct rfapi_adb *adb;
	uint32_t old_lifetime;
	int use_ip0 = 0;
	struct rfapi_rib_key rk;

	if (advertise_tunnel)
		*advertise_tunnel = 0;

	rfapi_rib_key_init(pfx_ip, prd, pfx_eth, &rk);
	/* find prefix in advertised prefixes list */
	if (RFAPI_0_PREFIX(pfx_ip) && RFAPI_HOST_PREFIX(pfx_ip)) {
		use_ip0 = 1;
		assert(pfx_eth);

		rc = skiplist_search(rfd->advertised.ip0_by_ether, &rk,
				     (void **)&adb);

	} else {

		/* find prefix in advertised prefixes list */
		rc = skiplist_search(rfd->advertised.ipN_by_prefix, &rk,
				     (void **)&adb);
	}

	if (rc) {
		return ENOENT;
	}

	old_lifetime = adb->lifetime;

	if (use_ip0) {
		rc = skiplist_delete(rfd->advertised.ip0_by_ether, &rk, NULL);
	} else {
		rc = skiplist_delete(rfd->advertised.ipN_by_prefix, &rk, NULL);
	}
	assert(!rc);

	rc = skiplist_delete(rfd->advertised.by_lifetime, adb, NULL);
	assert(!rc);

	rfapiAdbFree(adb);

	if (rfapiApAdjustLifetimeStats(rfd, &old_lifetime, NULL)) {
		if (advertise_tunnel)
			*advertise_tunnel = 1;
	}

	return 0;
}