-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1386 lines (1171 loc) · 41.9 KB
/
main.c
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/queue.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include "immintrin.h"
#include <rte_common.h>
#include <rte_vect.h>
#include <rte_byteorder.h>
#include <rte_log.h>
#include <rte_memory.h>
#include <rte_memzone.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_debug.h>
#include <rte_ethdev.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
#include <rte_cycles.h>
#include <rte_ip.h>
#include <rte_udp.h>
#include <rte_arp.h>
#include "barrier.h"
#include "config.h"
#define MEMPOOL_CACHE_SIZE 256
/* packet magic number to identify our packets */
#define PKT_MAGIC_ID 0xAA55DEADCAFE55AA
/* time to flush network buffers in seconds */
#define FLUSH_BUFFER_TIME 2
config_t *conf = NULL;
#define TICKS_TO_NSEC(x) ((x) * 1000 / ticks_per_usec)
uint64_t ticks_per_sec;
int ticks_per_usec;
int should_quit = 0;
volatile unsigned int rx_should_stop = 0; /* >0 to stop all rx threads */
volatile unsigned int tx_should_stop = 0; /* >0 to stop all rx threads */
volatile unsigned int tx_threads_stopped = 0; /* number of stopped tx threads */
struct timespec started, actual;
worker_barrier_t *b;
static struct rte_mempool * pktmbuf_pool;
static void __attribute__ ((unused))
hexdump(uint8_t buffer[], int len)
{
#define HEXDUMP_LINE_LEN 16
int i;
char s[HEXDUMP_LINE_LEN+1];
bzero(s, HEXDUMP_LINE_LEN+1);
for(i=0; i < len; i++) {
if (!(i%HEXDUMP_LINE_LEN)) {
if (s[0])
printf("[%s]",s);
printf("\n%05x: ", i);
bzero(s, HEXDUMP_LINE_LEN);
}
s[i%HEXDUMP_LINE_LEN]=isprint(buffer[i])?buffer[i]:'.';
printf("%02x ", buffer[i]);
}
while(i++%HEXDUMP_LINE_LEN)
printf(" ");
printf("[%s]\n", s);
}
static struct rte_eth_conf port_conf = {
.rxmode = {
.mq_mode = ETH_MQ_RX_RSS,
.max_rx_pkt_len = ETHER_MAX_LEN,
.split_hdr_size = 0,
.header_split = 0, /**< Header Split disabled */
.hw_ip_checksum = 0, /**< IP checksum offload enabled */
.hw_vlan_filter = 0,
.hw_vlan_strip = 0,
.hw_vlan_extend = 0,
.jumbo_frame = 0, /**< Jumbo Frame Support disabled */
.hw_strip_crc = 1, /**< CRC stripped by hardware */
},
.rx_adv_conf = {
.rss_conf = {
.rss_key = NULL,
.rss_hf = ETH_RSS_UDP | ETH_RSS_IP | ETH_RSS_TCP /*ETH_RSS_IP*/,
},
},
.txmode = {
.mq_mode = ETH_MQ_TX_NONE,
},
};
typedef enum {
THREAD_RX=1,
THREAD_TX=2
} thread_type_t;
typedef struct {
/* stats */
uint64_t num_tx_pkts;
uint64_t num_tx_octets;
uint64_t num_rx_pkts;
uint64_t num_rx_octets;
uint64_t num_rx_dropped;
uint64_t latency_sum;
uint64_t latency_min; /* total latency */
uint64_t latency_max;
} counters;
typedef struct {
char cacheline0[0] __attribute__((aligned(2*64)));
int port;
int queue;
thread_type_t type;
int lcore_id;
/* packet data */
uint16_t pkt_len;
uint64_t pkts_to_transmit; /* stop tx thread after this number is reached */
struct ether_addr src_mac;
struct ether_addr dst_mac;
uint32_t src_ip4;
uint32_t dst_ip4;
uint32_t src_ip6[16];
uint32_t dst_ip6[16];
uint32_t src_port;
uint32_t dst_port;
uint64_t delay; /* delay between packets, calculated at thread start from pps */
uint64_t pts; /* number of packets to send by this thread */
counters counters;
} per_thread_data_t;
typedef struct {
uint64_t magic_id;
uint64_t tsc; // time stamp counter
} packet_payload;
counters runtime_cnt = {0};
/* Shows DPDK error on stdout. Called from RX func. */
static inline void dump_dpdk_error(uint64_t flags)
{
printf("received packet with DPDK errors: ");
if (flags & PKT_RX_L4_CKSUM_BAD)
printf("L4 cksum of RX pkt. is not OK.\n");
if (flags & PKT_RX_IP_CKSUM_BAD)
printf("IP cksum of RX pkt. is not OK.\n");
if (flags & PKT_RX_EIP_CKSUM_BAD)
printf("External IP header checksum error.\n");
printf("\n");
}
static void print_configuration(void)
{
char temp1[64];
char temp2[64];
char port_str[32];
printf("Current configuration:\n");
printf("======================\n");
printf("selected test: ");
switch(conf->test)
{
case BINSEARCH:
printf("binary search");
break;
case DELAY:
printf("delay between packets");
break;
case FIXRATE:
printf("fixed rate");
break;
case LINSEARCH:
printf("linear search");
break;
}
printf(", status is printed in %is interval, test duration: %is.\n", conf->stats_interval, conf->duration);
printf("packet size: %i, IP version: %s\n", conf->packet_size, conf->ipv6 ? "IPv6" : "IPv4");
printf("PPS: %lu, PTS: %lu, ports: %i, tx_queues: %i, rx_queues: %i, burst_size: %i\n",
conf->pps, conf->pts, conf->num_ports, conf->num_tx_queues, conf->num_rx_queues, conf->burst_size);
printf("MAC info:\n");
ether_format_addr(temp1, sizeof(temp1),(const struct ether_addr *)&conf->src_mac[0]);
ether_format_addr(temp2, sizeof(temp2),(const struct ether_addr *)&conf->dst_mac[0]);
printf("%s -> %s\n", temp1, temp2);
ether_format_addr(temp1, sizeof(temp1),(const struct ether_addr *)&conf->src_mac[1]);
ether_format_addr(temp2, sizeof(temp2),(const struct ether_addr *)&conf->dst_mac[1]);
printf("%s -> %s\n", temp1, temp2);
if (conf->udp_port & PORT_INCREMENT)
sprintf(port_str, "%i[incremented]", conf->udp_port & 0xffff);
else if (conf->udp_port & PORT_RANDOM)
sprintf(port_str, "[RANDOM]");
else
sprintf(port_str, "%i", conf->udp_port & 0xffff);
printf("IP info:\n");
if (conf->ipv6)
{
printf("%s:%s -> %s:%s\n",
inet_ntop(AF_INET6, (void *)&conf->src_ip6[0], temp1, sizeof(temp1)), port_str,
inet_ntop(AF_INET6, (void *)&conf->dst_ip6[0], temp2, sizeof(temp2)), port_str);
printf("%s:%s -> %s:%s\n",
inet_ntop(AF_INET6, (void *)&conf->src_ip6[1], temp1, sizeof(temp1)), port_str,
inet_ntop(AF_INET6, (void *)&conf->dst_ip6[1], temp2, sizeof(temp2)), port_str);
} else {
printf("%s:%s -> %s:%s\n",
inet_ntop(AF_INET, (void *)&conf->src_ip4[0], temp1, sizeof(temp1)), port_str,
inet_ntop(AF_INET, (void *)&conf->dst_ip4[0], temp2, sizeof(temp2)), port_str);
printf("%s:%s -> %s:%s\n",
inet_ntop(AF_INET, (void *)&conf->src_ip4[1], temp1, sizeof(temp1)), port_str,
inet_ntop(AF_INET, (void *)&conf->dst_ip4[1], temp2, sizeof(temp2)), port_str);
}
printf("======================\n");
}
/* Calculates ipv4 hdr checksum and ipv4,6 UDP checksum. */
static inline void
calculate_checksum(struct rte_mbuf *pkt)
{
struct ether_hdr * eth;
struct ipv4_hdr * ip4;
struct ipv6_hdr * ip6;
struct udp_hdr * udp;
eth = rte_pktmbuf_mtod_offset(pkt, struct ether_hdr *, 0);
if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
{
ip4 = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *, sizeof(*eth));
udp = rte_pktmbuf_mtod_offset(pkt, struct udp_hdr *, sizeof(*eth)+sizeof(*ip4));
ip4->hdr_checksum = rte_ipv4_cksum(ip4);
udp->dgram_cksum = rte_ipv4_udptcp_cksum(ip4, udp);
}
if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
{
ip6 = rte_pktmbuf_mtod_offset(pkt, struct ipv6_hdr *, sizeof(*eth));
udp = rte_pktmbuf_mtod_offset(pkt, struct udp_hdr *, sizeof(*eth)+sizeof(*ip6));
udp->dgram_cksum = rte_ipv6_udptcp_cksum(ip6, udp);
}
}
/* Fills ethernet, ipv4 and UDP header in packet. Returns pointer to UDP payload. */
static inline void *
craft_packet_ipv4(per_thread_data_t * ptd, struct rte_mbuf *pkt)
{
struct ether_hdr * eth;
struct ipv4_hdr * ip4;
struct udp_hdr * udp;
eth = rte_pktmbuf_mtod_offset(pkt, struct ether_hdr *, 0);
ip4 = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *, sizeof(*eth));
udp = rte_pktmbuf_mtod_offset(pkt, struct udp_hdr *, sizeof(*eth)+sizeof(*ip4));
/* Metadata */
pkt->next = 0;
pkt->nb_segs = 1;
rte_pktmbuf_data_len (pkt) = ptd->pkt_len;
rte_pktmbuf_pkt_len (pkt) = ptd->pkt_len;
/* Ethernet */
eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
ether_addr_copy(&ptd->src_mac, ð->s_addr);
ether_addr_copy(&ptd->dst_mac, ð->d_addr);
/* IPv4 */
ip4->version_ihl = 0x45;
ip4->type_of_service = 0;
ip4->fragment_offset = 0;
ip4->time_to_live = 64;
ip4->next_proto_id = 17 /* UDP */;
ip4->packet_id = 0;
ip4->total_length = rte_cpu_to_be_16(ptd->pkt_len - sizeof(*eth));
ip4->src_addr = ptd->src_ip4;
ip4->dst_addr = ptd->dst_ip4;
ip4->hdr_checksum = 0;
/* UDP */
switch(ptd->src_port & 0xffff0000) {
case PORT_RANDOM:
udp->src_port = rand() & 0xffff; // FIXME rand (is up to 32768)!
break;
case PORT_INCREMENT:
if ((ptd->counters.num_tx_pkts % (conf->udp_port & 0xffff)) == 0) {
ptd->src_port++;
ptd->src_port = (ptd->src_port & 0xffff) + PORT_INCREMENT;
}
// no break here
default:
udp->src_port = rte_cpu_to_be_16(ptd->src_port);
}
switch(ptd->dst_port & 0xffff0000) {
case PORT_RANDOM:
udp->dst_port = rand() & 0xffff; // FIXME rand (is up to 32768)!
break;
case PORT_INCREMENT:
if ((ptd->counters.num_tx_pkts % (conf->udp_port & 0xffff)) == 0) {
ptd->dst_port++;
ptd->dst_port = (ptd->dst_port & 0xffff) + PORT_INCREMENT;
}
// no break here
default:
udp->dst_port = rte_cpu_to_be_16(ptd->dst_port);
}
udp->dgram_len = rte_cpu_to_be_16(ptd->pkt_len - sizeof(*eth) - sizeof(*ip4));
udp->dgram_cksum = 0;
return (udp+1);
}
/* Fills ethernet, ipv6 and UDP header in packet. Returns pointer to UDP payload. */
static inline void *
craft_packet_ipv6(per_thread_data_t * ptd, struct rte_mbuf *pkt)
{
struct ether_hdr * eth;
struct ipv6_hdr * ip6;
struct udp_hdr * udp;
eth = rte_pktmbuf_mtod_offset(pkt, struct ether_hdr *, 0);
ip6 = rte_pktmbuf_mtod_offset(pkt, struct ipv6_hdr *, sizeof(*eth));
udp = rte_pktmbuf_mtod_offset(pkt, struct udp_hdr *, sizeof(*eth)+sizeof(*ip6));
/* Metadata */
pkt->next = 0;
pkt->nb_segs = 1;
rte_pktmbuf_data_len (pkt) = ptd->pkt_len;
rte_pktmbuf_pkt_len (pkt) = ptd->pkt_len;
/* Ethernet */
ether_addr_copy(&ptd->src_mac, ð->s_addr);
ether_addr_copy(&ptd->dst_mac, ð->d_addr);
eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
/* IPv6 */
ip6->proto = IPPROTO_UDP;
ip6->vtc_flow = rte_cpu_to_be_32(0x60000000);
ip6->hop_limits = 64;
ip6->payload_len = rte_cpu_to_be_16(ptd->pkt_len - sizeof(*eth) - sizeof(*ip6));
memcpy(ip6->src_addr, ptd->src_ip6, 16);
memcpy(ip6->dst_addr, ptd->dst_ip6, 16);
/* UDP */
switch(ptd->src_port & 0xffff0000) {
case PORT_RANDOM:
udp->src_port = rand() & 0xffff; // FIXME rand (is up to 32768)!
break;
case PORT_INCREMENT:
if ((ptd->counters.num_tx_pkts % (conf->udp_port & 0xffff)) == 0) {
ptd->src_port++;
ptd->src_port = (ptd->src_port & 0xffff) + PORT_INCREMENT;
}
// no break here
default:
udp->src_port = rte_cpu_to_be_16(ptd->src_port);
}
switch(ptd->dst_port & 0xffff0000) {
case PORT_RANDOM:
udp->dst_port = rand() & 0xffff; // FIXME rand (is up to 32768)!
break;
case PORT_INCREMENT:
if ((ptd->counters.num_tx_pkts % (conf->udp_port & 0xffff)) == 0) {
ptd->dst_port++;
ptd->dst_port = (ptd->dst_port & 0xffff) + PORT_INCREMENT;
}
// no break here
default:
udp->dst_port = rte_cpu_to_be_16(ptd->dst_port);
}
udp->dgram_len = rte_cpu_to_be_16(ptd->pkt_len - sizeof(*eth) - sizeof(*ip6));
udp->dgram_cksum = 0;
return (udp+1);
}
/* Prepares ARP packet. */
static inline void prepare_arp(config_t *conf, struct rte_mbuf *pkt, uint16_t arp_opcode, int port)
{
struct ether_hdr * eth;
struct arp_hdr * arp;
eth = rte_pktmbuf_mtod_offset(pkt, struct ether_hdr *, 0);
arp = rte_pktmbuf_mtod_offset(pkt, struct arp_hdr *, sizeof(*eth));
/* Metadata */
pkt->next = 0;
pkt->nb_segs = 1;
rte_pktmbuf_data_len(pkt) = sizeof(struct ether_hdr) + sizeof(struct arp_hdr);
rte_pktmbuf_pkt_len(pkt) = sizeof(struct ether_hdr) + sizeof(struct arp_hdr);
/* Ethernet */
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port], ð->s_addr);
eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_ARP);
/* arp */
arp->arp_hrd = rte_cpu_to_be_16(ARP_HRD_ETHER);
arp->arp_pro = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
arp->arp_hln = ETHER_ADDR_LEN;
arp->arp_pln = sizeof(uint32_t);
if (arp_opcode == ARP_OP_REQUEST) {
arp->arp_op = rte_cpu_to_be_16(ARP_OP_REQUEST);
arp->arp_data.arp_sip = conf->src_ip4[port];
arp->arp_data.arp_tip = conf->dst_ip4[port];
memset(ð->d_addr, 0xff, 6);
memset(&arp->arp_data.arp_tha, 0, 6);
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port], &arp->arp_data.arp_sha);
}
if (arp_opcode == ARP_OP_REPLY) {
arp->arp_op = rte_cpu_to_be_16(ARP_OP_REPLY);
// (port==0)?1:0
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port], ð->s_addr);
ether_addr_copy((const struct ether_addr *)&conf->src_mac[(port==0)?1:0], ð->d_addr);
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port], &arp->arp_data.arp_sha);
ether_addr_copy((const struct ether_addr *)&conf->src_mac[(port==0)?1:0], &arp->arp_data.arp_tha);
arp->arp_data.arp_sip = conf->dst_ip4[(port==0)?1:0];
arp->arp_data.arp_tip = conf->src_ip4[(port==0)?1:0];
}
}
/* Prepares ARP packet. */
static inline void prepare_soladv(config_t *conf, struct rte_mbuf *pkt, int port, int icmp_type)
{
struct ether_hdr * eth;
struct ipv6_hdr * ip6;
struct icmp * icmpv6;
eth = rte_pktmbuf_mtod_offset(pkt, struct ether_hdr *, 0);
ip6 = rte_pktmbuf_mtod_offset(pkt, struct ipv6_hdr *, sizeof(*eth));
icmpv6 = rte_pktmbuf_mtod_offset(pkt, struct icmp *, sizeof(*eth) + sizeof(*ip6));
/* Metadata */
pkt->next = 0;
pkt->nb_segs = 1;
rte_pktmbuf_data_len(pkt) = sizeof(*eth) + sizeof(*ip6) + sizeof(*icmpv6);
rte_pktmbuf_pkt_len(pkt) = sizeof(*eth) + sizeof(*ip6) + sizeof(*icmpv6);
if (icmp_type == NEIGHBOR_SOLICITATION)
{
/* Ethernet */
eth->d_addr.addr_bytes[0] = 0x33;
eth->d_addr.addr_bytes[1] = 0x33;
eth->d_addr.addr_bytes[2] = 0xff;
eth->d_addr.addr_bytes[3] = (uint8_t)conf->dst_ip6[port*16+13];
eth->d_addr.addr_bytes[4] = (uint8_t)conf->dst_ip6[port*16+14];
eth->d_addr.addr_bytes[5] = (uint8_t)conf->dst_ip6[port*16+15];
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port], ð->s_addr);
eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
/* IPv6 */
ip6->proto = IPPROTO_ICMPV6;
ip6->vtc_flow = rte_cpu_to_be_32(0x60000000);
ip6->hop_limits = 0xff;
ip6->payload_len = rte_cpu_to_be_16(sizeof(*icmpv6));
memcpy(&ip6->src_addr, (const void *)&conf->src_ip6[port*16], 16);
memcpy(&ip6->dst_addr, (const void *)&conf->dst_ip6[port*16], 16);
/* Icmpv6 */
icmpv6->icmp_type = NEIGHBOR_SOLICITATION;
icmpv6->icmp_code = 0;
icmpv6->reserved = 0;
icmpv6->option_type = 1;
icmpv6->option_length = 1;
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port],
(struct ether_addr *)&icmpv6->option_ll_addr);
icmpv6->icmp_cksum = 0;
icmpv6->icmp_cksum = rte_ipv6_udptcp_cksum(ip6, icmpv6);
}
else if (icmp_type == NEIGHBOR_ADVERTISEMENT)
{
/* RESPONSE TO SOLICITATION */
/* Ethernet */
ether_addr_copy(ð->s_addr, ð->d_addr);
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port], ð->s_addr);
eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
port = ~port & 1;
/* IPv6 */
ip6->proto = IPPROTO_ICMPV6;
ip6->vtc_flow = rte_cpu_to_be_32(0x60000000);
ip6->hop_limits = 0xff;
ip6->payload_len = rte_cpu_to_be_16(sizeof(*icmpv6));
memcpy(&ip6->src_addr, (const void *)&conf->dst_ip6[port*16], 16);
memcpy(&ip6->dst_addr, (const void *)&conf->src_ip6[port*16], 16);
port = ~port & 1;
/* Icmpv6 */
icmpv6->icmp_type = NEIGHBOR_ADVERTISEMENT;
icmpv6->icmp_code = 0;
icmpv6->reserved = rte_cpu_to_be_32(0x60000000);
icmpv6->option_type = 1;
icmpv6->option_length = 1;
ether_addr_copy((const struct ether_addr *)&conf->src_mac[port],
(struct ether_addr *)&icmpv6->option_ll_addr);
icmpv6->icmp_cksum = 0;
icmpv6->icmp_cksum = rte_ipv6_udptcp_cksum(ip6, icmpv6);
}
}
/* Sends 2 ARP requests */
static inline void send_arp(config_t *conf)
{
struct rte_mbuf *pkt;
uint16_t ret = 0;
pkt = rte_pktmbuf_alloc(pktmbuf_pool);
prepare_arp(conf, pkt, ARP_OP_REQUEST, 0);
ret += rte_eth_tx_burst(0, 0, &pkt, 1); /* request from port 0 */
pkt = rte_pktmbuf_alloc(pktmbuf_pool);
prepare_arp(conf, pkt, ARP_OP_REQUEST, 1);
ret += rte_eth_tx_burst(1, 0, &pkt, 1); /* request from port 1 */
if (ret != 2) {
printf("Unable to send ARP packets !!\n");
abort();
}
}
static inline void send_solicitation(config_t *conf)
{
struct rte_mbuf *pkt;
uint16_t ret = 0;
pkt = rte_pktmbuf_alloc(pktmbuf_pool);
prepare_soladv(conf, pkt, 0, NEIGHBOR_SOLICITATION);
ret += rte_eth_tx_burst(0, 0, &pkt, 1); /* request from port 0 */
pkt = rte_pktmbuf_alloc(pktmbuf_pool);
prepare_soladv(conf, pkt, 1, NEIGHBOR_SOLICITATION);
ret += rte_eth_tx_burst(1, 0, &pkt, 1); /* request from port 0 */
if (ret != 2) {
printf("Unable to send Neighbor Solicitation packets !!\n");
abort();
}
}
/* Main transmit function */
static int lcore_tx_main(__attribute__((unused)) void *arg)
{
int i;
unsigned lcore_id;
per_thread_data_t * ptd = (per_thread_data_t *) arg;
uint64_t tsc, last_run_tsc = 0;
packet_payload *payload;
struct rte_mbuf *pkts[MAX_PKT_BURST];
int pkts_in_round = conf->burst_size;
int pkts_sent;
int pts_present = 0; /* was "packets to send" set ? */
lcore_id = rte_lcore_id();
printf("Handling port %u TX queue %u on core %u\n", ptd->port, ptd->queue, lcore_id);
if (ptd->src_port & PORT_INCREMENT) /* start from zero*/
ptd->src_port &= 0xffff0000;
if (ptd->dst_port & PORT_INCREMENT) /* start from zero*/
ptd->dst_port &= 0xffff0000;
while(!tx_should_stop) {
worker_barrier_check(b);
tsc = rte_rdtsc();
/* Is number of "packets to send" set ? */
if(unlikely(ptd->pts)) {
pts_present = 1;
pkts_in_round = (ptd->pts > (uint64_t)conf->burst_size) ? conf->burst_size : ptd->pts;
}
/* Is "delay between packets" set ? */
if(unlikely(ptd->delay)) {
if (tsc < (last_run_tsc + ptd->delay * pkts_in_round))
continue;
}
last_run_tsc = tsc;
for (i=0; i<pkts_in_round; i++) {
pkts[i] = rte_pktmbuf_alloc(pktmbuf_pool); /* allocate packet memory */
if (pkts[i] == NULL)
{
printf("Cannot allocate mbuf !\n");
abort();
}
if (!conf->ipv6) /* fill headers */
payload = craft_packet_ipv4(ptd, pkts[i]);
else
payload = craft_packet_ipv6(ptd, pkts[i]);
payload->magic_id = PKT_MAGIC_ID;
payload->tsc = tsc; /* put tsc into payload and recalculate checksum */
calculate_checksum(pkts[i]);
}
pkts_sent = rte_eth_tx_burst(ptd->port, ptd->queue, pkts, pkts_in_round);
ptd->counters.num_tx_pkts += pkts_sent;
ptd->counters.num_tx_octets += ptd->pkt_len * pkts_sent;
if (pkts_sent != pkts_in_round)
{
for (i=pkts_sent; i<pkts_in_round; i++)
rte_pktmbuf_free(pkts[i]);
}
/* Was all "packets to send" sent ? If yes, stop thread. */
if(unlikely(pts_present)) {
ptd->pts -= pkts_sent;
if (!ptd->pts) {
__sync_fetch_and_add(&tx_threads_stopped, 1);
break;
}
}
}
/* this was the last thread - send ALARM signal to the main thread */
if (tx_threads_stopped == conf->num_tx_queues * conf->num_ports) {
kill(0, SIGALRM);
}
__sync_fetch_and_add(b->num_workers, -1);
return 0;
}
/* Main receive function */
static int
lcore_rx_main(__attribute__((unused)) void *arg)
{
int nb_rx = 0;
int i;
unsigned lcore_id;
uint64_t latency;
uint16_t *eth_type;
struct rte_mbuf *pkts[MAX_PKT_BURST];
packet_payload *payload;
per_thread_data_t * ptd = (per_thread_data_t *) arg;
struct ipv4_hdr *ip4;
struct ipv6_hdr *ip6;
struct udp_hdr * udp;
struct arp_hdr * arp;
struct icmp * icmpv6;
FILE * fp;
char filename[64];
if (conf->save_latencies) {
sprintf(filename, "port_%u_queue_%u_core_%u.txt", ptd->port, ptd->queue, lcore_id);
fp = fopen (filename, "w+");
}
lcore_id = rte_lcore_id();
printf("Handling port %u RX queue %u on core %u\n", ptd->port, ptd->queue, lcore_id);
while(!rx_should_stop) {
worker_barrier_check(b);
nb_rx = rte_eth_rx_burst(ptd->port, ptd->queue, pkts, MAX_PKT_BURST);
uint64_t tsc2 = rte_rdtsc();
if (!nb_rx)
continue;
for (i = 0; i < nb_rx; i++) {
eth_type = rte_pktmbuf_mtod_offset(pkts[i], uint16_t *, 12);
/* handle ARP */
if (unlikely(rte_be_to_cpu_16(*eth_type) == ETHER_TYPE_ARP)) {
arp = rte_pktmbuf_mtod_offset(pkts[i], struct arp_hdr *, sizeof(struct ether_hdr));
if (arp->arp_op == rte_cpu_to_be_16(ARP_OP_REPLY)) {
if (arp->arp_data.arp_sip == conf->dst_ip4[0])
ether_addr_copy(&arp->arp_data.arp_sha, (struct ether_addr *)&conf->dst_mac[0]);
if (arp->arp_data.arp_sip == conf->dst_ip4[1])
ether_addr_copy(&arp->arp_data.arp_sha, (struct ether_addr *)&conf->dst_mac[1]);
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
} else if (arp->arp_op == rte_cpu_to_be_16(ARP_OP_REQUEST)) {
if (arp->arp_data.arp_tip == ptd->dst_ip4) {
prepare_arp(conf, pkts[i], ARP_OP_REPLY, ptd->port);
rte_eth_tx_burst(ptd->port, 0, &pkts[i], 1);
continue;
}
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
}
/* handle IPv6 */
} else if (rte_be_to_cpu_16(*eth_type) == ETHER_TYPE_IPv6) {
if (!conf->ipv6) { /* We are using IPv4, drop IPv6 packet */
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
}
ip6 = rte_pktmbuf_mtod_offset(pkts[i], struct ipv6_hdr *, sizeof(struct ether_hdr));
udp = rte_pktmbuf_mtod_offset(pkts[i], struct udp_hdr *, sizeof(struct ether_hdr)+sizeof(*ip6));
icmpv6 = rte_pktmbuf_mtod_offset(pkts[i], struct icmp *, sizeof(struct ether_hdr)+sizeof(*ip6));
payload = rte_pktmbuf_mtod_offset(pkts[i], packet_payload *,
sizeof(struct ether_hdr)+sizeof(*ip6)+sizeof(*udp));
/* handle Network solicitation request */
if (unlikely(ip6->proto == IPPROTO_ICMPV6)) {
if (icmpv6->icmp_type == NEIGHBOR_SOLICITATION) {
prepare_soladv(conf, pkts[i], ptd->port, NEIGHBOR_ADVERTISEMENT);
rte_eth_tx_burst(ptd->port, 0, &pkts[i], 1);
} else if (icmpv6->icmp_type == NEIGHBOR_ADVERTISEMENT) {
if (!memcmp(&ip6->src_addr, &conf->dst_ip6[0], 16))
ether_addr_copy((struct ether_addr *)&icmpv6->option_ll_addr,
(struct ether_addr *)&conf->dst_mac[0]);
if (!memcmp(&ip6->src_addr, &conf->dst_ip6[16], 16))
ether_addr_copy((struct ether_addr *)&icmpv6->option_ll_addr,
(struct ether_addr *)&conf->dst_mac[1]);
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
}
}
/* Is it our packet ? check addr && dst ports */
if ((!memcmp(ip6->src_addr, ptd->src_ip6, 16)) || (!memcmp(ip6->dst_addr, ptd->dst_ip6, 16))) {
ptd->counters.num_rx_dropped++;
continue;
}
if(unlikely((conf->udp_port & (PORT_RANDOM|PORT_INCREMENT)) == 0)) {
if (rte_be_to_cpu_16(udp->dst_port) != ptd->dst_port) {
ptd->counters.num_rx_dropped++;
continue;
}
}
/* Set payload to UDP payload */
payload = (packet_payload *)rte_pktmbuf_mtod_offset(pkts[i], uint64_t *,
sizeof(struct ether_hdr)+sizeof(*ip6)+sizeof(*udp));
if (payload->magic_id != PKT_MAGIC_ID) {
printf("Bad magic number !\n"); fflush(stdout);
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
}
/* handle IPv4 */
} else if (rte_be_to_cpu_16(*eth_type) == ETHER_TYPE_IPv4) {
if (conf->ipv6) { /* We are using IPv6, drop IPv4 packet */
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
}
ip4 = rte_pktmbuf_mtod_offset(pkts[i], struct ipv4_hdr *, sizeof(struct ether_hdr));
udp = rte_pktmbuf_mtod_offset(pkts[i], struct udp_hdr *, sizeof(struct ether_hdr)+sizeof(*ip4));
payload = (packet_payload *)rte_pktmbuf_mtod_offset(pkts[i], uint64_t *,
sizeof(struct ether_hdr)+sizeof(*ip4)+sizeof(*udp));
/* Is it our packet ? check addr && dst ports */
if ((ip4->src_addr != ptd->src_ip4) || (ip4->dst_addr != ptd->dst_ip4)) {
ptd->counters.num_rx_dropped++;
continue;
}
if(unlikely((conf->udp_port & (PORT_RANDOM|PORT_INCREMENT)) == 0)) {
if (rte_be_to_cpu_16(udp->dst_port) != ptd->dst_port) {
ptd->counters.num_rx_dropped++;
continue;
}
}
/* Set payload to UDP payload */
if (payload->magic_id != PKT_MAGIC_ID) {
printf("Bad magic number !\n"); fflush(stdout);
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
}
} else {
/* this was not IP packet, free memory && continue*/
// printf("UNKNOWN PACKET !\n"); fflush(stdout);
// hexdump(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), pkts[i]->pkt_len);
rte_pktmbuf_free(pkts[i]);
ptd->counters.num_rx_dropped++;
continue;
}
//printf("port %u queue %u tx_timestamp %lu rx_timestamp %lu delte %lu (%lu ns)\n",
// ptd->port, ptd->queue, *tsc1, tsc2, tsc2-*tsc1, TICKS_TO_NSEC(tsc2-*tsc1));
//hexdump(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), pkts[i]->pkt_len);
latency = tsc2 - payload->tsc;
if (payload->tsc > tsc2) {
printf("Received invalid TSC! TSC@send: 0x%lx, TSC@recv: 0x%lx\n", payload->tsc, tsc2);
hexdump(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), pkts[i]->pkt_len);
latency = 0;
}
if (conf->save_latencies)
fprintf(fp, "%lu\n", TICKS_TO_NSEC(latency));
/* set min/max and sum latency */
ptd->counters.latency_sum += latency;
if (latency < ptd->counters.latency_min)
ptd->counters.latency_min = latency;
if (latency > ptd->counters.latency_max)
ptd->counters.latency_max = latency;
/* Was there any DPDK error ? */
if (unlikely(pkts[i]->ol_flags &
(PKT_RX_L4_CKSUM_BAD | PKT_RX_IP_CKSUM_BAD | PKT_RX_EIP_CKSUM_BAD)))
dump_dpdk_error(pkts[i]->ol_flags);
/* Inc counters && free memory */
ptd->counters.num_rx_pkts++;
ptd->counters.num_rx_octets += pkts[i]->pkt_len;
rte_pktmbuf_free(pkts[i]);
}
}
__sync_fetch_and_add(b->num_workers, -1);
if (conf->save_latencies)
fclose(fp);
return 0;
}
enum { QUIT_CTRL_C=1, QUIT_ALARM };
static void signal_stop(__attribute__((unused)) int signal)
{
/* CTRL+C keypress from terminal handler */
printf("\nSIGSTOP received, exitting...\n");
should_quit = QUIT_CTRL_C;
}
static void signal_alarm(__attribute__((unused)) int signal)
{
/* SIGALRM handler. SIGALRM is sent from last running RX thread */
should_quit = QUIT_ALARM;
}
/* Calculate time diff. Used for latency calculation. */
static uint64_t clock_diff(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp.tv_sec*1000+temp.tv_nsec/1000000;
}
/* Start DPDK RX/TX threads */
static per_thread_data_t * launch_threads(per_thread_data_t * ptd, thread_type_t rxtx)
{
int lcore_id;
unsigned int p, q, d;
int num_threads = conf->num_ports * (conf->num_rx_queues + conf->num_tx_queues);
if (ptd)
free(ptd);
/* Allocate cache-alligned per thread data */
ptd = aligned_alloc(64, num_threads * sizeof(per_thread_data_t));
memset(ptd, 0, num_threads * sizeof(per_thread_data_t));
p = q = d = 0;
clock_gettime(CLOCK_MONOTONIC, &started);
RTE_LCORE_FOREACH_SLAVE(lcore_id) {
unsigned int threads_per_port = conf->num_tx_queues + conf->num_rx_queues;
int i = p * threads_per_port + q;
if (p == conf->num_ports)
continue;
ptd[i].port = p;
ptd[i].lcore_id = lcore_id;
if (q < conf->num_tx_queues) {
if (rxtx & THREAD_TX) {
/* prepare TX threads */
if (conf->pps)
ptd[i].delay = ticks_per_sec / (conf->pps / conf->num_tx_queues);
ptd[i].pts = conf->pts / conf->num_tx_queues;
ptd[i].queue = q;
ptd[i].type = THREAD_TX;
if (p == 0) {
rte_memcpy(&ptd[i].src_mac, (const void *)&conf->src_mac[0], 6);
rte_memcpy(&ptd[i].dst_mac, (const void *)&conf->dst_mac[0], 6);
} else {
rte_memcpy(&ptd[i].src_mac, (const void *)&conf->src_mac[1], 6);
rte_memcpy(&ptd[i].dst_mac, (const void *)&conf->dst_mac[1], 6);
}
ptd[i].src_ip4 = conf->src_ip4[p];
ptd[i].dst_ip4 = conf->dst_ip4[p];
memcpy(ptd[i].src_ip6, &conf->src_ip6[p*16], 16);
memcpy(ptd[i].dst_ip6, &conf->dst_ip6[p*16], 16);
ptd[i].src_port = conf->udp_port+q;
ptd[i].dst_port = conf->udp_port;
ptd[i].pkt_len = conf->packet_size;
/* and launch it */
__sync_fetch_and_add(b->num_workers, 1);
rte_eal_remote_launch(lcore_tx_main, &ptd[i], lcore_id);
}
} else {
if (rxtx & THREAD_RX) {
/* prepare RX threads */
ptd[i].queue = q - conf->num_tx_queues;
ptd[i].type = THREAD_RX;
ptd[i].src_ip4 = conf->src_ip4[(p==1)?0:1];
ptd[i].dst_ip4 = conf->dst_ip4[(p==1)?0:1];
ptd[i].dst_port = conf->udp_port;
ptd[i].counters.latency_min = 0xffffffffffffffff;
ptd[i].counters.latency_max = 0;
/* and launch it */
__sync_fetch_and_add(b->num_workers, 1);
rte_eal_remote_launch(lcore_rx_main, &ptd[i], lcore_id);
}
}
q++;
if (q == threads_per_port) {
q = 0;
p++;
}
}
/* If test duration was set, set up alarm. Alarm callback will shut down pktgen */
if (conf->duration)
alarm(conf->duration);
return ptd;
}
typedef enum {RATE_START, RATE_UP, RATE_DOWN} updown;
static void binsrch_get_pps(updown direction)
{
switch(direction)
{
case RATE_START:
conf->cur_rate = (conf->max_rate - conf->min_rate) / 2;
conf->binsrch_step = conf->cur_rate;
break;
case RATE_UP:
conf->min_rate = conf->cur_rate + 1;
conf->binsrch_step = (conf->max_rate - conf->min_rate) / 2;
conf->cur_rate += conf->binsrch_step;
break;
case RATE_DOWN:
conf->max_rate = conf->cur_rate - 1;
conf->binsrch_step = (conf->max_rate - conf->min_rate) / 2;
conf->cur_rate -= conf->binsrch_step;
break;
}
conf->pps = rate_to_pps(conf->cur_rate);
return;
}
static void linsrch_get_pps(updown direction)
{
switch(direction)
{
case RATE_START:
conf->cur_rate = conf->max_rate;
break;