aboutsummaryrefslogtreecommitdiff
path: root/sys/netinet/in_fib_algo.c
blob: a5048c52b75d18133811bc967a32f433b67f188a (plain) (blame)
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2020 Alexander V. Chernikov
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_inet.h"

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/rmlock.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/priv.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/vnet.h>

#include <net/if.h>
#include <netinet/in.h>

#include <net/route.h>
#include <net/route/nhop.h>
#include <net/route/route_ctl.h>
#include <net/route/route_var.h>
#include <net/route/fib_algo.h>

/*
 * Binary search lookup algo.
 *
 * Compiles route table into a sorted array.
 * Used with small amount of routes (< 16).
 * As array is immutable, it is rebuild on each rtable change.
 *
 * Example:
 *
 * 0.0.0.0/0 -> nh1
 * 10.0.0.0/24 -> nh2
 * 10.0.0.1/32 -> nh3
 *
 * gets compiled to:
 *
 * 0.0.0.0 -> nh1
 * 10.0.0.0 -> nh2
 * 10.0.0.1 -> nh3
 * 10.0.0.2 -> nh2
 * 10.0.1.0 -> nh1
 *
 */

struct bsearch4_record {
	uint32_t		addr4;
	uint32_t		mask4;
	struct nhop_object	*nh;
};

struct bsearch4_data {
	struct fib_data		*fd;
	uint32_t		alloc_items;
	uint32_t		num_items;
	void			*mem;
	struct bsearch4_record	*rr;
	struct bsearch4_record	br[0];
};

/*
 * Main IPv4 address lookup function.
 *
 * Finds array record with maximum index that is <= provided key.
 * Assumes 0.0.0.0/0 always exists (may be with NULL nhop)
 */
static struct nhop_object *
bsearch4_lookup(void *algo_data, const struct flm_lookup_key key, uint32_t scopeid)
{
	const struct bsearch4_data *bd = (const struct bsearch4_data *)algo_data;
	const struct bsearch4_record *br;
	uint32_t addr4 = ntohl(key.addr4.s_addr);

	int start = 0;
	int end = bd->num_items;

	int i = (start + end) / 2;
	while (start + 1 < end) {
		i = (start + end) / 2;
		br = &bd->br[i];
		if (addr4 < br->addr4) {
			/* key < average, reduce right boundary */
			end = i;
			continue;
		} else if (addr4 > br->addr4) {
			/* key > average, increase left aboundary */
			start = i;
			continue;
		} else {
			/* direct match */
			return (br->nh);
		}
	}
	/* start + 1 == end */
	return (bd->br[start].nh);
}

/*
 * Preference function.
 * Assume ideal for < 10 (typical single-interface setup has 5)
 * Then gradually degrade.
 * Assume 30 prefixes is at least 60 records, so it will require 8 lookup,
 *  which is even worse than radix.
 */
static uint8_t
bsearch4_get_pref(const struct rib_rtable_info *rinfo)
{

	if (rinfo->num_prefixes < 10)
		return (253);
	else if (rinfo->num_prefixes < 30)
		return (255 - rinfo->num_prefixes * 8);
	else
		return (1);
}

static enum flm_op_result
bsearch4_init(uint32_t fibnum, struct fib_data *fd, void *_old_data, void **_data)
{
	struct bsearch4_data *bd;
	struct rib_rtable_info rinfo;
	uint32_t count;
	size_t sz;
	void *mem;

	fib_get_rtable_info(fib_get_rh(fd), &rinfo);
	count = rinfo.num_prefixes * 11 / 10 + 64;

	sz = sizeof(struct bsearch4_data) + sizeof(struct bsearch4_record) * count;
	/* add cache line sz to ease alignment */
	sz += CACHE_LINE_SIZE;
	mem = malloc(sz, M_RTABLE, M_NOWAIT | M_ZERO);
	if (mem == NULL)
		return (FLM_REBUILD);
	/* Align datapath-usable structure to cache line boundary */
	bd = (struct bsearch4_data *)roundup2((uintptr_t)mem, CACHE_LINE_SIZE);
	bd->mem = mem;
	bd->alloc_items = count;
	bd->fd = fd;

	*_data = bd;

	/*
	 * Allocate temporary array to store all rtable data.
	 * This step is required to provide the required prefix iteration order.
	 */
	bd->rr = mallocarray(count, sizeof(struct bsearch4_record), M_TEMP, M_NOWAIT | M_ZERO);
	if (bd->rr == NULL)
		return (FLM_REBUILD);

	return (FLM_SUCCESS);
}

static void
bsearch4_destroy(void *_data)
{
	struct bsearch4_data *bd = (struct bsearch4_data *)_data;

	if (bd->rr != NULL)
		free(bd->rr, M_TEMP);
	free(bd->mem, M_RTABLE);
}

/*
 * Callback storing converted rtable prefixes in the temporary array.
 * Addresses are converted to a host order.
 */
static enum flm_op_result
bsearch4_add_route_cb(struct rtentry *rt, void *_data)
{
	struct bsearch4_data *bd = (struct bsearch4_data *)_data;
	struct bsearch4_record *rr;
	struct in_addr addr4, mask4;
	uint32_t scopeid;

	if (bd->num_items >= bd->alloc_items)
		return (FLM_REBUILD);

	rr = &bd->rr[bd->num_items++];
	rt_get_inet_prefix_pmask(rt, &addr4, &mask4, &scopeid);
	rr->addr4 = ntohl(addr4.s_addr);
	rr->mask4 = ntohl(mask4.s_addr);
	rr->nh = rt_get_raw_nhop(rt);

	return (FLM_SUCCESS);
}

/*
 * Prefix comparison function.
 * 10.0.0.0/24 < 10.0.0.0/25 <- less specific wins
 * 10.0.0.0/25 < 10.0.0.1/32 <- bigger base wins
 */
static int
rr_cmp(const void *_rec1, const void *_rec2)
{
	const struct bsearch4_record *rec1, *rec2;
	rec1 = _rec1;
	rec2 = _rec2;

	if (rec1->addr4 < rec2->addr4)
		return (-1);
	else if (rec1->addr4 > rec2->addr4)
		return (1);

	/*
	 * wider mask value is lesser mask
	 * we want less specific come first, e.g. <
	 */
	if (rec1->mask4 < rec2->mask4)
		return (-1);
	else if (rec1->mask4 > rec2->mask4)
		return (1);
	return (0);
}

struct bsearch4_array {
	uint32_t		alloc_items;
	uint32_t		num_items;
	struct bsearch4_record	*arr;
};

static bool
add_array_entry(struct bsearch4_array *ba, struct bsearch4_record *br_new)
{

	if (ba->num_items < ba->alloc_items) {
		ba->arr[ba->num_items++] = *br_new;
		return (true);
	}
	return (false);
}

static struct bsearch4_record *
get_last_entry(struct bsearch4_array *ba)
{

	return (&ba->arr[ba->num_items - 1]);
}

/*
 *
 * Example:
 *  stack: 10.0.1.0/24,nh=3 array: 10.0.1.0/25,nh=4 -> ++10.0.1.128/24,nh=3
 *
 *
 */
static bool
pop_stack_entry(struct bsearch4_array *dst_array, struct bsearch4_array *stack)
{
	uint32_t last_stack_addr, last_array_addr;

	struct bsearch4_record *br_prev = get_last_entry(dst_array);
	struct bsearch4_record *pstack = get_last_entry(stack);

	/* Regardless of the result, pop stack entry */
	stack->num_items--;

	/* Prefix last address for the last entry in lookup array */
	last_array_addr = (br_prev->addr4 | ~br_prev->mask4);
	/* Prefix last address for the stack record entry */
	last_stack_addr = (pstack->addr4 | ~pstack->mask4);

	if (last_stack_addr > last_array_addr) {
		/*
		 * Stack record covers > address space than
		 * the last entry in the lookup array.
		 * Add the remaining parts of a stack record to
		 * the lookup array.
		 */
		struct bsearch4_record br_new = {
			.addr4 = last_array_addr + 1,
			.mask4 = pstack->mask4,
			.nh = pstack->nh,
		};
		return (add_array_entry(dst_array, &br_new));
	}

	return (true);
}

/*
 * Updates resulting array @dst_array with a rib entry @rib_entry.
 */
static bool
bsearch4_process_record(struct bsearch4_array *dst_array,
    struct bsearch4_array *stack, struct bsearch4_record *rib_entry)
{

	/*
	 * Maintain invariant: current rib_entry is always contained
	 *  in the top stack entry.
	 * Note we always have 0.0.0.0/0.
	 */
	while (stack->num_items > 0) {
		struct bsearch4_record *pst = get_last_entry(stack);

		/*
		 * Check if we need to pop stack.
		 * Rely on the ordering - larger prefixes comes up first
		 * Pop any entry that doesn't contain current prefix.
		 */
		if (pst->addr4 == (rib_entry->addr4 & pst->mask4))
			break;

		if (!pop_stack_entry(dst_array, stack))
			return (false);
	}

	 if (dst_array->num_items > 0) {

		 /*
		  * Check if there is a gap between previous entry and a
		  *  current entry. Code above guarantees that both previous
		  *  and current entry are contained in the top stack entry.
		  *
		  * Example: last: 10.0.0.1(/32,nh=3) cur: 10.0.0.3(/32,nh=4),
		  *  stack: 10.0.0.0/24,nh=2.
		  * Cover a gap between previous and current by adding stack
		  *  nexthop.
		  */
		 struct bsearch4_record *br_tmp = get_last_entry(dst_array);
		 uint32_t last_declared_addr = br_tmp->addr4 | ~br_tmp->mask4;
		 if (last_declared_addr < rib_entry->addr4 - 1) {
			 /* Cover a hole */
			struct bsearch4_record *pst = get_last_entry(stack);
			struct bsearch4_record new_entry = {
				.addr4 = last_declared_addr + 1,
				.mask4 = pst->mask4,
				.nh = pst->nh,
			};
			if (!add_array_entry(dst_array, &new_entry))
				return (false);
		 }
	 }

	if (!add_array_entry(dst_array, rib_entry))
		return (false);
	add_array_entry(stack, rib_entry);

	return (true);
}

static enum flm_op_result
bsearch4_build_array(struct bsearch4_array *dst_array, struct bsearch4_array *src_array)
{

	/*
	 * During iteration, we keep track of all prefixes in rtable
	 * we currently match, by maintaining stack. As there can be only
	 * 32 prefixes for a single address, pre-allocate stack of size 32.
	 */
	struct bsearch4_array stack = {
		.alloc_items = 32,
		.arr = mallocarray(32, sizeof(struct bsearch4_record), M_TEMP, M_NOWAIT | M_ZERO),
	};
	if (stack.arr == NULL)
		return (FLM_REBUILD);

	for (int i = 0; i < src_array->num_items; i++) {
		struct bsearch4_record *rib_entry = &src_array->arr[i];

		if (!bsearch4_process_record(dst_array, &stack, rib_entry)) {
			free(stack.arr, M_TEMP);
			return (FLM_REBUILD);
		}
	}

	/*
	 * We know that last record is contained in the top stack entry.
	 */
	while (stack.num_items > 0) {
		if (!pop_stack_entry(dst_array, &stack))
			return (FLM_REBUILD);
	}
	free(stack.arr, M_TEMP);

	return (FLM_SUCCESS);
}

static enum flm_op_result
bsearch4_build(struct bsearch4_data *bd)
{
	enum flm_op_result ret;

	struct bsearch4_array prefixes_array = {
		.alloc_items = bd->alloc_items,
		.num_items = bd->num_items,
		.arr = bd->rr,
	};

	/* Add default route if not exists */
	bool default_found = false;
	for (int i = 0; i < prefixes_array.num_items; i++) {
		if (prefixes_array.arr[i].mask4 == 0) {
			default_found = true;
			break;
		}
	}
	if (!default_found) {
		 /* Add default route with NULL nhop */
		struct bsearch4_record default_entry = {};
		if (!add_array_entry(&prefixes_array, &default_entry))
			 return (FLM_REBUILD);
	}

	/* Sort prefixes */
	qsort(prefixes_array.arr, prefixes_array.num_items, sizeof(struct bsearch4_record), rr_cmp);

	struct bsearch4_array dst_array = {
		.alloc_items = bd->alloc_items,
		.arr = bd->br,
	};

	ret = bsearch4_build_array(&dst_array, &prefixes_array);
	bd->num_items = dst_array.num_items;

	free(bd->rr, M_TEMP);
	bd->rr = NULL;
	return (ret);
}


static enum flm_op_result
bsearch4_end_dump(void *_data, struct fib_dp *dp)
{
	struct bsearch4_data *bd = (struct bsearch4_data *)_data;
	enum flm_op_result ret;

	ret = bsearch4_build(bd);
	if (ret == FLM_SUCCESS) {
		dp->f = bsearch4_lookup;
		dp->arg = bd;
	}

	return (ret);
}

static enum flm_op_result
bsearch4_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
    void *_data)
{

	return (FLM_REBUILD);
}

struct fib_lookup_module flm_bsearch4= {
	.flm_name = "bsearch4",
	.flm_family = AF_INET,
	.flm_init_cb = bsearch4_init,
	.flm_destroy_cb = bsearch4_destroy,
	.flm_dump_rib_item_cb = bsearch4_add_route_cb,
	.flm_dump_end_cb = bsearch4_end_dump,
	.flm_change_rib_item_cb = bsearch4_change_cb,
	.flm_get_pref = bsearch4_get_pref,
};

/*
 * Lockless radix lookup algo.
 *
 * Compiles immutable radix from the current routing table.
 * Used with small amount of routes (<1000).
 * As datastructure is immutable, it gets rebuild on each rtable change.
 *
 * Lookups are slightly faster as shorter lookup keys are used
 *  (4 bytes instead of 8 in stock radix).
 */

#define KEY_LEN_INET	(offsetof(struct sockaddr_in, sin_addr) + sizeof(in_addr_t))
#define OFF_LEN_INET	(8 * offsetof(struct sockaddr_in, sin_addr))
struct radix4_addr_entry {
	struct radix_node	rn[2];
	struct sockaddr_in	addr;
	struct nhop_object	*nhop;
};
#define	LRADIX4_ITEM_SZ	roundup2(sizeof(struct radix4_addr_entry), 64)

struct lradix4_data {
	struct radix_node_head	*rnh;
	struct fib_data		*fd;
	void			*mem;
	char			*rt_base;
	uint32_t		alloc_items;
	uint32_t		num_items;
};

static struct nhop_object *
lradix4_lookup(void *algo_data, const struct flm_lookup_key key, uint32_t scopeid)
{
	struct radix_node_head *rnh = (struct radix_node_head *)algo_data;
	struct radix4_addr_entry *ent;
	struct sockaddr_in addr4 = {
		.sin_len = KEY_LEN_INET,
		.sin_addr = key.addr4,
	};
	ent = (struct radix4_addr_entry *)(rnh->rnh_matchaddr(&addr4, &rnh->rh));
	if (ent != NULL)
		return (ent->nhop);
	return (NULL);
}

/*
 * Preference function.
 * Assume close-to-ideal of < 10 routes (though worse than bsearch), then
 * gradually degrade until 1000 routes are reached.
 */
static uint8_t
lradix4_get_pref(const struct rib_rtable_info *rinfo)
{

	if (rinfo->num_prefixes < 10)
		return (250);
	else if (rinfo->num_prefixes < 1000)
		return (254 - rinfo->num_prefixes / 4);
	else
		return (1);
}

static enum flm_op_result
lradix4_init(uint32_t fibnum, struct fib_data *fd, void *_old_data, void **_data)
{
	struct lradix4_data *lr;
	struct rib_rtable_info rinfo;
	uint32_t count;
	size_t sz;

	lr = malloc(sizeof(struct lradix4_data), M_RTABLE, M_NOWAIT | M_ZERO);
	if (lr == NULL || !rn_inithead((void **)&lr->rnh, OFF_LEN_INET))
		return (FLM_REBUILD);
	fib_get_rtable_info(fib_get_rh(fd), &rinfo);

	count = rinfo.num_prefixes * 11 / 10;
	sz = count * LRADIX4_ITEM_SZ + CACHE_LINE_SIZE;
	lr->mem = malloc(sz, M_RTABLE, M_NOWAIT | M_ZERO);
	if (lr->mem == NULL)
		return (FLM_REBUILD);
	/* Align all rtentries to a cacheline boundary */
	lr->rt_base = (char *)roundup2((uintptr_t)lr->mem, CACHE_LINE_SIZE);
	lr->alloc_items = count;
	lr->fd = fd;

	*_data = lr;

	return (FLM_SUCCESS);
}

static void
lradix4_destroy(void *_data)
{
	struct lradix4_data *lr = (struct lradix4_data *)_data;

	if (lr->rnh != NULL)
		rn_detachhead((void **)&lr->rnh);
	if (lr->mem != NULL)
		free(lr->mem, M_RTABLE);
	free(lr, M_RTABLE);
}

static enum flm_op_result
lradix4_add_route_cb(struct rtentry *rt, void *_data)
{
	struct lradix4_data *lr = (struct lradix4_data *)_data;
	struct radix4_addr_entry *ae;
	struct sockaddr_in mask;
	struct sockaddr *rt_mask = NULL;
	struct radix_node *rn;
	struct in_addr addr4, mask4;
	uint32_t scopeid;

	if (lr->num_items >= lr->alloc_items)
		return (FLM_REBUILD);

	ae = (struct radix4_addr_entry *)(lr->rt_base + lr->num_items * LRADIX4_ITEM_SZ);
	lr->num_items++;

	ae->nhop = rt_get_raw_nhop(rt);

	rt_get_inet_prefix_pmask(rt, &addr4, &mask4, &scopeid);
	ae->addr.sin_len = KEY_LEN_INET;
	ae->addr.sin_addr = addr4;

	if (mask4.s_addr != INADDR_ANY) {
		bzero(&mask, sizeof(mask));
		mask.sin_len = KEY_LEN_INET;
		mask.sin_addr = mask4;
		rt_mask = (struct sockaddr *)&mask;
	}

	rn = lr->rnh->rnh_addaddr((struct sockaddr *)&ae->addr, rt_mask,
	    &lr->rnh->rh, ae->rn);
	if (rn == NULL)
		return (FLM_REBUILD);

	return (FLM_SUCCESS);
}

static enum flm_op_result
lradix4_end_dump(void *_data, struct fib_dp *dp)
{
	struct lradix4_data *lr = (struct lradix4_data *)_data;

	dp->f = lradix4_lookup;
	dp->arg = lr->rnh;

	return (FLM_SUCCESS);
}

static enum flm_op_result
lradix4_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
    void *_data)
{

	return (FLM_REBUILD);
}

struct fib_lookup_module flm_radix4_lockless = {
	.flm_name = "radix4_lockless",
	.flm_family = AF_INET,
	.flm_init_cb = lradix4_init,
	.flm_destroy_cb = lradix4_destroy,
	.flm_dump_rib_item_cb = lradix4_add_route_cb,
	.flm_dump_end_cb = lradix4_end_dump,
	.flm_change_rib_item_cb = lradix4_change_cb,
	.flm_get_pref = lradix4_get_pref,
};

/*
 * Fallback lookup algorithm.
 * This is a simple wrapper around system radix.
 */

struct radix4_data {
	struct fib_data *fd;
	struct rib_head *rh;
};

static struct nhop_object *
radix4_lookup(void *algo_data, const struct flm_lookup_key key, uint32_t scopeid)
{
	RIB_RLOCK_TRACKER;
	struct rib_head *rh = (struct rib_head *)algo_data;
	struct radix_node *rn;
	struct nhop_object *nh;

	/* Prepare lookup key */
	struct sockaddr_in sin4 = {
		.sin_family = AF_INET,
		.sin_len = sizeof(struct sockaddr_in),
		.sin_addr = key.addr4,
	};

	nh = NULL;
	RIB_RLOCK(rh);
	rn = rh->rnh_matchaddr((void *)&sin4, &rh->head);
	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0))
		nh = (RNTORT(rn))->rt_nhop;
	RIB_RUNLOCK(rh);

	return (nh);
}

static uint8_t
radix4_get_pref(const struct rib_rtable_info *rinfo)
{

	return (50);
}

static enum flm_op_result
radix4_init(uint32_t fibnum, struct fib_data *fd, void *_old_data, void **_data)
{
	struct radix4_data *r4;

	r4 = malloc(sizeof(struct radix4_data), M_RTABLE, M_NOWAIT | M_ZERO);
	if (r4 == NULL)
		return (FLM_REBUILD);
	r4->fd = fd;
	r4->rh = fib_get_rh(fd);

	*_data = r4;

	return (FLM_SUCCESS);
}

static void
radix4_destroy(void *_data)
{

	free(_data, M_RTABLE);
}

static enum flm_op_result
radix4_add_route_cb(struct rtentry *rt, void *_data)
{

	return (FLM_SUCCESS);
}

static enum flm_op_result
radix4_end_dump(void *_data, struct fib_dp *dp)
{
	struct radix4_data *r4 = (struct radix4_data *)_data;

	dp->f = radix4_lookup;
	dp->arg = r4->rh;

	return (FLM_SUCCESS);
}

static enum flm_op_result
radix4_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
    void *_data)
{

	return (FLM_SUCCESS);
}

struct fib_lookup_module flm_radix4 = {
	.flm_name = "radix4",
	.flm_family = AF_INET,
	.flm_init_cb = radix4_init,
	.flm_destroy_cb = radix4_destroy,
	.flm_dump_rib_item_cb = radix4_add_route_cb,
	.flm_dump_end_cb = radix4_end_dump,
	.flm_change_rib_item_cb = radix4_change_cb,
	.flm_get_pref = radix4_get_pref,
};

static void
fib4_algo_init(void)
{

	fib_module_register(&flm_bsearch4);
	fib_module_register(&flm_radix4_lockless);
	fib_module_register(&flm_radix4);
}
SYSINIT(fib4_algo_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, fib4_algo_init, NULL);