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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* 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>
#include "opt_inet.h"
#include "opt_inet6.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/rmlock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/vnet.h>
#include <net/if.h>
#include <net/if_var.h>
#include <netinet/in.h>
#include <netinet/in_fib.h>
#include <netinet/ip.h>
#include <netinet6/in6_fib.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>
#define CHUNK_SIZE 10000
VNET_DEFINE_STATIC(struct in_addr *, inet_addr_list);
#define V_inet_addr_list VNET(inet_addr_list)
VNET_DEFINE_STATIC(int, inet_list_size);
#define V_inet_list_size VNET(inet_list_size)
VNET_DEFINE_STATIC(struct in6_addr *, inet6_addr_list);
#define V_inet6_addr_list VNET(inet6_addr_list)
VNET_DEFINE_STATIC(int, inet6_list_size);
#define V_inet6_list_size VNET(inet6_list_size)
SYSCTL_DECL(_net_route);
SYSCTL_NODE(_net_route, OID_AUTO, test, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"Route algorithm lookups");
static int
add_addr(int family, char *addr_str)
{
if (family == AF_INET) {
struct in_addr *paddr_old = V_inet_addr_list;
int size_old = V_inet_list_size;
struct in_addr addr;
if (inet_pton(AF_INET, addr_str, &addr) != 1)
return (EINVAL);
struct in_addr *paddr = mallocarray(size_old + 1,
sizeof(struct in_addr), M_TEMP, M_ZERO | M_WAITOK);
if (paddr_old != NULL) {
memcpy(paddr, paddr_old, size_old * sizeof(struct in_addr));
free(paddr_old, M_TEMP);
}
paddr[size_old] = addr;
V_inet_addr_list = paddr;
V_inet_list_size = size_old + 1;
inet_ntop(AF_INET, &addr, addr_str, sizeof(addr_str));
} else if (family == AF_INET6) {
struct in6_addr *paddr_old = V_inet6_addr_list;
int size_old = V_inet6_list_size;
struct in6_addr addr6;
if (inet_pton(AF_INET6, addr_str, &addr6) != 1)
return (EINVAL);
struct in6_addr *paddr = mallocarray(size_old + 1,
sizeof(struct in6_addr), M_TEMP, M_ZERO | M_WAITOK);
if (paddr_old != NULL) {
memcpy(paddr, paddr_old, size_old * sizeof(struct in6_addr));
free(paddr_old, M_TEMP);
}
paddr[size_old] = addr6;
V_inet6_addr_list = paddr;
V_inet6_list_size = size_old + 1;
inet_ntop(AF_INET6, &addr6, addr_str, sizeof(addr_str));
}
return (0);
}
static int
add_addr_sysctl_handler(struct sysctl_oid *oidp, struct sysctl_req *req, int family)
{
char addr_str[INET6_ADDRSTRLEN];
int error;
bzero(addr_str, sizeof(addr_str));
error = sysctl_handle_string(oidp, addr_str, sizeof(addr_str), req);
if (error != 0 || req->newptr == NULL)
return (error);
error = add_addr(family, addr_str);
return (0);
}
static int
add_inet_addr_sysctl_handler(SYSCTL_HANDLER_ARGS)
{
return (add_addr_sysctl_handler(oidp, req, AF_INET));
}
SYSCTL_PROC(_net_route_test, OID_AUTO, add_inet_addr,
CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
add_inet_addr_sysctl_handler, "A", "Set");
static int
add_inet6_addr_sysctl_handler(SYSCTL_HANDLER_ARGS)
{
return (add_addr_sysctl_handler(oidp, req, AF_INET6));
}
SYSCTL_PROC(_net_route_test, OID_AUTO, add_inet6_addr,
CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
add_inet6_addr_sysctl_handler, "A", "Set");
static uint64_t
run_test_inet_one_pass(uint32_t fibnum)
{
/* Assume epoch */
int sz = V_inet_list_size;
int tries = CHUNK_SIZE / sz;
const struct in_addr *a = V_inet_addr_list;
uint64_t count = 0;
for (int pass = 0; pass < tries; pass++) {
for (int i = 0; i < sz; i++) {
fib4_lookup(fibnum, a[i], 0, NHR_NONE, 0);
count++;
}
}
return (count);
}
static int
run_test_inet(SYSCTL_HANDLER_ARGS)
{
struct epoch_tracker et;
int count = 0;
int error = sysctl_handle_int(oidp, &count, 0, req);
if (error != 0)
return (error);
if (count == 0)
return (0);
if (V_inet_list_size <= 0)
return (ENOENT);
printf("run: %d packets vnet %p\n", count, curvnet);
if (count < CHUNK_SIZE)
count = CHUNK_SIZE;
struct timespec ts_pre, ts_post;
int64_t pass_diff, total_diff = 0;
uint64_t pass_packets, total_packets = 0;
uint32_t fibnum = curthread->td_proc->p_fibnum;
for (int pass = 0; pass < count / CHUNK_SIZE; pass++) {
NET_EPOCH_ENTER(et);
nanouptime(&ts_pre);
pass_packets = run_test_inet_one_pass(fibnum);
nanouptime(&ts_post);
NET_EPOCH_EXIT(et);
pass_diff = (ts_post.tv_sec - ts_pre.tv_sec) * 1000000000 +
(ts_post.tv_nsec - ts_pre.tv_nsec);
total_diff += pass_diff;
total_packets += pass_packets;
}
printf("%zu packets in %zu nanoseconds, %zu pps\n",
total_packets, total_diff, total_packets * 1000000000 / total_diff);
return (0);
}
SYSCTL_PROC(_net_route_test, OID_AUTO, run_inet,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, 0, run_test_inet, "I", "Execute fib4_lookup test");
static uint64_t
run_test_inet6_one_pass(uint32_t fibnum)
{
/* Assume epoch */
int sz = V_inet6_list_size;
int tries = CHUNK_SIZE / sz;
const struct in6_addr *a = V_inet6_addr_list;
uint64_t count = 0;
for (int pass = 0; pass < tries; pass++) {
for (int i = 0; i < sz; i++) {
fib6_lookup(fibnum, &a[i], 0, NHR_NONE, 0);
count++;
}
}
return (count);
}
static int
run_test_inet6(SYSCTL_HANDLER_ARGS)
{
struct epoch_tracker et;
int count = 0;
int error = sysctl_handle_int(oidp, &count, 0, req);
if (error != 0)
return (error);
if (count == 0)
return (0);
if (V_inet6_list_size <= 0)
return (ENOENT);
printf("run: %d packets vnet %p\n", count, curvnet);
if (count < CHUNK_SIZE)
count = CHUNK_SIZE;
struct timespec ts_pre, ts_post;
int64_t pass_diff, total_diff = 0;
uint64_t pass_packets, total_packets = 0;
uint32_t fibnum = curthread->td_proc->p_fibnum;
for (int pass = 0; pass < count / CHUNK_SIZE; pass++) {
NET_EPOCH_ENTER(et);
nanouptime(&ts_pre);
pass_packets = run_test_inet6_one_pass(fibnum);
nanouptime(&ts_post);
NET_EPOCH_EXIT(et);
pass_diff = (ts_post.tv_sec - ts_pre.tv_sec) * 1000000000 +
(ts_post.tv_nsec - ts_pre.tv_nsec);
total_diff += pass_diff;
total_packets += pass_packets;
}
printf("%zu packets in %zu nanoseconds, %zu pps\n",
total_packets, total_diff, total_packets * 1000000000 / total_diff);
return (0);
}
SYSCTL_PROC(_net_route_test, OID_AUTO, run_inet6,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, 0, run_test_inet6, "I", "Execute fib6_lookup test");
static bool
cmp_dst(uint32_t fibnum, struct in_addr a)
{
struct nhop_object *nh_fib;
struct rtentry *rt;
struct route_nhop_data rnd = {};
nh_fib = fib4_lookup(fibnum, a, 0, NHR_NONE, 0);
rt = fib4_lookup_rt(fibnum, a, 0, NHR_NONE, &rnd);
if (nh_fib == NULL && rt == NULL) {
return (true);
} else if (nh_fib == nhop_select(rnd.rnd_nhop, 0)) {
return (true);
}
struct in_addr dst;
int plen;
uint32_t scopeid;
char key_str[INET_ADDRSTRLEN], dst_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &a, key_str, sizeof(key_str));
if (rnd.rnd_nhop == NULL) {
printf("[RT BUG] lookup for %s: RIB: ENOENT FIB: nh=%u\n",
key_str, nhop_get_idx(nh_fib));
} else {
rt_get_inet_prefix_plen(rt, &dst, &plen, &scopeid);
inet_ntop(AF_INET, &dst, dst_str, sizeof(dst_str));
printf("[RT BUG] lookup for %s: RIB: %s/%d,nh=%u FIB: nh=%u\n",
key_str, dst_str, plen,
nhop_get_idx(nhop_select(rnd.rnd_nhop, 0)),
nh_fib ? nhop_get_idx(nh_fib) : 0);
}
return (false);
}
static bool
cmp_dst6(uint32_t fibnum, const struct in6_addr *a)
{
struct nhop_object *nh_fib;
struct rtentry *rt;
struct route_nhop_data rnd = {};
nh_fib = fib6_lookup(fibnum, a, 0, NHR_NONE, 0);
rt = fib6_lookup_rt(fibnum, a, 0, NHR_NONE, &rnd);
if (nh_fib == NULL && rt == NULL) {
return (true);
} else if (nh_fib == nhop_select(rnd.rnd_nhop, 0)) {
return (true);
}
struct in6_addr dst;
int plen;
uint32_t scopeid;
char key_str[INET6_ADDRSTRLEN], dst_str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, a, key_str, sizeof(key_str));
if (rnd.rnd_nhop == NULL) {
printf("[RT BUG] lookup for %s: RIB: ENOENT FIB: nh=%u\n",
key_str, nhop_get_idx(nh_fib));
} else {
rt_get_inet6_prefix_plen(rt, &dst, &plen, &scopeid);
inet_ntop(AF_INET6, &dst, dst_str, sizeof(dst_str));
printf("[RT BUG] lookup for %s: RIB: %s/%d,nh=%u FIB: nh=%u\n",
key_str, dst_str, plen,
nhop_get_idx(nhop_select(rnd.rnd_nhop, 0)),
nh_fib ? nhop_get_idx(nh_fib) : 0);
}
return (false);
}
/* Random lookups: correctness verification */
static uint64_t
run_test_inet_one_pass_random(uint32_t fibnum)
{
/* Assume epoch */
struct in_addr a[64];
int sz = 64;
uint64_t count = 0;
for (int pass = 0; pass < CHUNK_SIZE / sz; pass++) {
arc4random_buf(a, sizeof(a));
for (int i = 0; i < sz; i++) {
if (!cmp_dst(fibnum, a[i]))
return (0);
count++;
}
}
return (count);
}
static int
run_test_inet_random(SYSCTL_HANDLER_ARGS)
{
struct epoch_tracker et;
int count = 0;
int error = sysctl_handle_int(oidp, &count, 0, req);
if (error != 0)
return (error);
if (count == 0)
return (0);
if (count < CHUNK_SIZE)
count = CHUNK_SIZE;
struct timespec ts_pre, ts_post;
int64_t pass_diff, total_diff = 1;
uint64_t pass_packets, total_packets = 0;
uint32_t fibnum = curthread->td_proc->p_fibnum;
for (int pass = 0; pass < count / CHUNK_SIZE; pass++) {
NET_EPOCH_ENTER(et);
nanouptime(&ts_pre);
pass_packets = run_test_inet_one_pass_random(fibnum);
nanouptime(&ts_post);
NET_EPOCH_EXIT(et);
pass_diff = (ts_post.tv_sec - ts_pre.tv_sec) * 1000000000 +
(ts_post.tv_nsec - ts_pre.tv_nsec);
total_diff += pass_diff;
total_packets += pass_packets;
if (pass_packets == 0)
break;
}
/* Signal error to userland */
if (pass_packets == 0)
return (EINVAL);
printf("%zu packets in %zu nanoseconds, %zu pps\n",
total_packets, total_diff, total_packets * 1000000000 / total_diff);
return (0);
}
SYSCTL_PROC(_net_route_test, OID_AUTO, run_inet_random,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, 0, run_test_inet_random, "I", "Execute fib4_lookup random check tests");
struct inet_array {
uint32_t alloc_items;
uint32_t num_items;
uint32_t rnh_prefixes;
int error;
struct in_addr *arr;
};
/*
* For each prefix, add the following records to the lookup array:
* * prefix-1, prefix, prefix + 1, prefix_end, prefix_end + 1
*/
static int
add_prefix(struct rtentry *rt, void *_data)
{
struct inet_array *pa = (struct inet_array *)_data;
struct in_addr addr;
int plen;
uint32_t scopeid, haddr;
pa->rnh_prefixes++;
if (pa->num_items + 5 >= pa->alloc_items) {
if (pa->error == 0)
pa->error = ENOSPC;
return (0);
}
rt_get_inet_prefix_plen(rt, &addr, &plen, &scopeid);
pa->arr[pa->num_items++] = addr;
haddr = ntohl(addr.s_addr);
if (haddr > 0) {
pa->arr[pa->num_items++].s_addr = htonl(haddr - 1);
pa->arr[pa->num_items++].s_addr = htonl(haddr + 1);
/* assume mask != 0 */
uint32_t mlen = (1 << (32 - plen)) - 1;
pa->arr[pa->num_items++].s_addr = htonl(haddr + mlen);
/* can overflow, but who cares */
pa->arr[pa->num_items++].s_addr = htonl(haddr + mlen + 1);
}
return (0);
}
static bool
prepare_list(uint32_t fibnum, struct inet_array *pa)
{
struct rib_head *rh;
rh = rt_tables_get_rnh(fibnum, AF_INET);
uint32_t num_prefixes = rh->rnh_prefixes;
bzero(pa, sizeof(struct inet_array));
pa->alloc_items = (num_prefixes + 10) * 5;
pa->arr = mallocarray(pa->alloc_items, sizeof(struct in_addr),
M_TEMP, M_ZERO | M_WAITOK);
rib_walk(fibnum, AF_INET, false, add_prefix, pa);
if (pa->error != 0) {
printf("prefixes: old: %u, current: %u, walked: %u, allocated: %u\n",
num_prefixes, rh->rnh_prefixes, pa->rnh_prefixes, pa->alloc_items);
}
return (pa->error == 0);
}
static int
run_test_inet_scan(SYSCTL_HANDLER_ARGS)
{
struct epoch_tracker et;
int count = 0;
int error = sysctl_handle_int(oidp, &count, 0, req);
if (error != 0)
return (error);
if (count == 0)
return (0);
struct inet_array pa = {};
uint32_t fibnum = curthread->td_proc->p_fibnum;
if (!prepare_list(fibnum, &pa))
return (pa.error);
struct timespec ts_pre, ts_post;
int64_t total_diff = 1;
uint64_t total_packets = 0;
int failure_count = 0;
NET_EPOCH_ENTER(et);
nanouptime(&ts_pre);
for (int i = 0; i < pa.num_items; i++) {
if (!cmp_dst(fibnum, pa.arr[i])) {
failure_count++;
}
total_packets++;
}
nanouptime(&ts_post);
NET_EPOCH_EXIT(et);
if (pa.arr != NULL)
free(pa.arr, M_TEMP);
/* Signal error to userland */
if (failure_count > 0) {
printf("[RT ERROR] total failures: %d\n", failure_count);
return (EINVAL);
}
total_diff = (ts_post.tv_sec - ts_pre.tv_sec) * 1000000000 +
(ts_post.tv_nsec - ts_pre.tv_nsec);
printf("%zu packets in %zu nanoseconds, %zu pps\n",
total_packets, total_diff, total_packets * 1000000000 / total_diff);
return (0);
}
SYSCTL_PROC(_net_route_test, OID_AUTO, run_inet_scan,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, 0, run_test_inet_scan, "I", "Execute fib4_lookup scan tests");
struct inet6_array {
uint32_t alloc_items;
uint32_t num_items;
uint32_t rnh_prefixes;
int error;
struct in6_addr *arr;
};
static bool
safe_add(uint32_t *v, uint32_t inc)
{
if (*v < (UINT32_MAX - inc)) {
*v += inc;
return (true);
} else {
*v -= (UINT32_MAX - inc + 1);
return (false);
}
}
static bool
safe_dec(uint32_t *v, uint32_t inc)
{
if (*v >= inc) {
*v -= inc;
return (true);
} else {
*v += (UINT32_MAX - inc + 1);
return (false);
}
}
static void
inc_prefix6(struct in6_addr *addr, int inc)
{
for (int i = 0; i < 4; i++) {
uint32_t v = ntohl(addr->s6_addr32[3 - i]);
bool ret = safe_add(&v, inc);
addr->s6_addr32[3 - i] = htonl(v);
if (ret)
return;
inc = 1;
}
}
static void
dec_prefix6(struct in6_addr *addr, int dec)
{
for (int i = 0; i < 4; i++) {
uint32_t v = ntohl(addr->s6_addr32[3 - i]);
bool ret = safe_dec(&v, dec);
addr->s6_addr32[3 - i] = htonl(v);
if (ret)
return;
dec = 1;
}
}
static void
ipv6_writemask(struct in6_addr *addr6, uint8_t mask)
{
uint32_t *cp;
for (cp = (uint32_t *)addr6; mask >= 32; mask -= 32)
*cp++ = 0xFFFFFFFF;
if (mask > 0)
*cp = htonl(mask ? ~((1 << (32 - mask)) - 1) : 0);
}
/*
* For each prefix, add the following records to the lookup array:
* * prefix-1, prefix, prefix + 1, prefix_end, prefix_end + 1
*/
static int
add_prefix6(struct rtentry *rt, void *_data)
{
struct inet6_array *pa = (struct inet6_array *)_data;
struct in6_addr addr, naddr;
int plen;
uint32_t scopeid;
pa->rnh_prefixes++;
if (pa->num_items + 5 >= pa->alloc_items) {
if (pa->error == 0)
pa->error = ENOSPC;
return (0);
}
rt_get_inet6_prefix_plen(rt, &addr, &plen, &scopeid);
pa->arr[pa->num_items++] = addr;
if (!IN6_ARE_ADDR_EQUAL(&addr, &in6addr_any)) {
naddr = addr;
dec_prefix6(&naddr, 1);
pa->arr[pa->num_items++] = naddr;
naddr = addr;
inc_prefix6(&naddr, 1);
pa->arr[pa->num_items++] = naddr;
/* assume mask != 0 */
struct in6_addr mask6;
ipv6_writemask(&mask6, plen);
naddr = addr;
for (int i = 0; i < 3; i++)
naddr.s6_addr32[i] = htonl(ntohl(naddr.s6_addr32[i]) | ~ntohl(mask6.s6_addr32[i]));
pa->arr[pa->num_items++] = naddr;
inc_prefix6(&naddr, 1);
pa->arr[pa->num_items++] = naddr;
}
return (0);
}
static bool
prepare_list6(uint32_t fibnum, struct inet6_array *pa)
{
struct rib_head *rh;
rh = rt_tables_get_rnh(fibnum, AF_INET6);
uint32_t num_prefixes = rh->rnh_prefixes;
bzero(pa, sizeof(struct inet6_array));
pa->alloc_items = (num_prefixes + 10) * 5;
pa->arr = mallocarray(pa->alloc_items, sizeof(struct in6_addr),
M_TEMP, M_ZERO | M_WAITOK);
rib_walk(fibnum, AF_INET6, false, add_prefix6, pa);
if (pa->error != 0) {
printf("prefixes: old: %u, current: %u, walked: %u, allocated: %u\n",
num_prefixes, rh->rnh_prefixes, pa->rnh_prefixes, pa->alloc_items);
}
return (pa->error == 0);
}
static int
run_test_inet6_scan(SYSCTL_HANDLER_ARGS)
{
struct epoch_tracker et;
int count = 0;
int error = sysctl_handle_int(oidp, &count, 0, req);
if (error != 0)
return (error);
if (count == 0)
return (0);
struct inet6_array pa = {};
uint32_t fibnum = curthread->td_proc->p_fibnum;
if (!prepare_list6(fibnum, &pa))
return (pa.error);
struct timespec ts_pre, ts_post;
int64_t total_diff = 1;
uint64_t total_packets = 0;
int failure_count = 0;
NET_EPOCH_ENTER(et);
nanouptime(&ts_pre);
for (int i = 0; i < pa.num_items; i++) {
if (!cmp_dst6(fibnum, &pa.arr[i])) {
failure_count++;
}
total_packets++;
}
nanouptime(&ts_post);
NET_EPOCH_EXIT(et);
if (pa.arr != NULL)
free(pa.arr, M_TEMP);
/* Signal error to userland */
if (failure_count > 0) {
printf("[RT ERROR] total failures: %d\n", failure_count);
return (EINVAL);
}
total_diff = (ts_post.tv_sec - ts_pre.tv_sec) * 1000000000 +
(ts_post.tv_nsec - ts_pre.tv_nsec);
printf("%zu packets in %zu nanoseconds, %zu pps\n",
total_packets, total_diff, total_packets * 1000000000 / total_diff);
return (0);
}
SYSCTL_PROC(_net_route_test, OID_AUTO, run_inet6_scan,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, 0, run_test_inet6_scan, "I", "Execute fib6_lookup scan tests");
#define LPS_SEQ 0x1
#define LPS_ANN 0x2
#define LPS_REP 0x4
struct lps_walk_state {
uint32_t *keys;
int pos;
int lim;
};
static int
reduce_keys(struct rtentry *rt, void *_data)
{
struct lps_walk_state *wa = (struct lps_walk_state *) _data;
struct in_addr addr;
uint32_t scopeid;
int plen;
rt_get_inet_prefix_plen(rt, &addr, &plen, &scopeid);
wa->keys[wa->pos] = ntohl(addr.s_addr) |
(wa->keys[wa->pos] & ~(0xffffffffU << (32 - plen)));
wa->pos++;
return (wa->pos == wa->lim);
}
static int
rnd_lps(SYSCTL_HANDLER_ARGS)
{
struct epoch_tracker et;
struct in_addr key;
struct lps_walk_state wa;
struct timespec ts_pre, ts_post;
struct nhop_object *nh_fib;
uint64_t total_diff, lps;
uint32_t *keys, fibnum;
uint32_t t, p;
uintptr_t acc = 0;
int i, pos, count = 0;
int seq = 0, rep = 0;
int error;
error = sysctl_handle_int(oidp, &count, 0, req);
if (error != 0)
return (error);
if (count <= 0)
return (0);
fibnum = curthread->td_proc->p_fibnum;
keys = malloc(sizeof(*keys) * count, M_TEMP, M_NOWAIT);
if (keys == NULL)
return (ENOMEM);
printf("Preparing %d random keys...\n", count);
arc4random_buf(keys, sizeof(*keys) * count);
if (arg2 & LPS_ANN) {
wa.keys = keys;
wa.pos = 0;
wa.lim = count;
printf("Reducing keys to announced address space...\n");
do {
rib_walk(fibnum, AF_INET, false, reduce_keys,
&wa);
} while (wa.pos < wa.lim);
printf("Reshuffling keys...\n");
for (int i = 0; i < count; i++) {
p = random() % count;
t = keys[p];
keys[p] = keys[i];
keys[i] = t;
}
}
if (arg2 & LPS_REP) {
rep = 1;
printf("REP ");
}
if (arg2 & LPS_SEQ) {
seq = 1;
printf("SEQ");
} else if (arg2 & LPS_ANN)
printf("ANN");
else
printf("RND");
printf(" LPS test starting...\n");
NET_EPOCH_ENTER(et);
nanouptime(&ts_pre);
for (i = 0, pos = 0; i < count; i++) {
key.s_addr = keys[pos++] ^ ((acc >> 10) & 0xff);
nh_fib = fib4_lookup(fibnum, key, 0, NHR_NONE, 0);
if (seq) {
if (nh_fib != NULL) {
acc += (uintptr_t) nh_fib + 123;
if (acc & 0x1000)
acc += (uintptr_t) nh_fib->nh_ifp;
else
acc -= (uintptr_t) nh_fib->nh_ifp;
} else
acc ^= (acc >> 3) + (acc << 2) + i;
if (acc & 0x800)
pos++;
if (pos >= count)
pos = 0;
}
if (rep && ((i & 0xf) == 0xf)) {
pos -= 0xf;
if (pos < 0)
pos += 0xf;
}
}
nanouptime(&ts_post);
NET_EPOCH_EXIT(et);
free(keys, M_TEMP);
total_diff = (ts_post.tv_sec - ts_pre.tv_sec) * 1000000000 +
(ts_post.tv_nsec - ts_pre.tv_nsec);
lps = 1000000000ULL * count / total_diff;
printf("%d lookups in %zu.%06zu milliseconds, %lu.%06lu MLPS\n",
count, total_diff / 1000000, total_diff % 1000000,
lps / 1000000, lps % 1000000);
return (0);
}
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_rnd,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, 0, rnd_lps, "I",
"Measure lookups per second, uniformly random keys, independent lookups");
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_rnd_ann,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, LPS_ANN, rnd_lps, "I",
"Measure lookups per second, random keys from announced address space, "
"independent lookups");
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_seq,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, LPS_SEQ, rnd_lps, "I",
"Measure lookups per second, uniformly random keys, "
"artificial dependencies between lookups");
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_seq_ann,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, LPS_SEQ | LPS_ANN, rnd_lps, "I",
"Measure lookups per second, random keys from announced address space, "
"artificial dependencies between lookups");
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_rnd_rep,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, LPS_REP, rnd_lps, "I",
"Measure lookups per second, uniformly random keys, independent lookups, "
"repeated keys");
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_rnd_ann_rep,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, LPS_ANN | LPS_REP, rnd_lps, "I",
"Measure lookups per second, random keys from announced address space, "
"independent lookups, repeated keys");
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_seq_rep,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, LPS_SEQ | LPS_REP, rnd_lps, "I",
"Measure lookups per second, uniformly random keys, "
"artificial dependencies between lookups, repeated keys");
SYSCTL_PROC(_net_route_test, OID_AUTO, run_lps_seq_ann_rep,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, LPS_SEQ | LPS_ANN | LPS_REP, rnd_lps, "I",
"Measure lookups per second, random keys from announced address space, "
"artificial dependencies between lookups, repeated keys");
static int
test_fib_lookup_modevent(module_t mod, int type, void *unused)
{
int error = 0;
switch (type) {
case MOD_LOAD:
break;
case MOD_UNLOAD:
if (V_inet_addr_list != NULL)
free(V_inet_addr_list, M_TEMP);
if (V_inet6_addr_list != NULL)
free(V_inet6_addr_list, M_TEMP);
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}
static moduledata_t testfiblookupmod = {
"test_fib_lookup",
test_fib_lookup_modevent,
0
};
DECLARE_MODULE(testfiblookupmod, testfiblookupmod, SI_SUB_PSEUDO, SI_ORDER_ANY);
MODULE_VERSION(testfiblookup, 1);
|