forked from appneta/tcpreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_packets.c
1359 lines (1179 loc) · 45.2 KB
/
send_packets.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
/* $Id$ */
/*
* Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
* Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
*
* The Tcpreplay Suite of tools 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 3 of the
* License, or with the authors permission any later version.
*
* The Tcpreplay Suite 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 the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "defines.h"
#include "common.h"
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "tcpreplay_api.h"
#include "timestamp_trace.h"
#include "../lib/sll.h"
#ifdef HAVE_NETMAP
#ifdef HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif
#include <sys/ioctl.h>
#include <net/netmap.h>
#include <net/netmap_user.h>
#endif /* HAVE_NETMAP */
#ifdef TCPREPLAY
#ifdef TCPREPLAY_EDIT
#include "tcpreplay_edit_opts.h"
#include "tcpedit/tcpedit.h"
extern tcpedit_t *tcpedit;
#else
#include "tcpreplay_opts.h"
#endif /* TCPREPLAY_EDIT */
#endif /* TCPREPLAY */
#include "send_packets.h"
#include "sleep.h"
#ifdef DEBUG
extern int debug;
#endif
static bool calc_sleep_time(tcpreplay_t *ctx, struct timeval *pkt_time,
struct timeval *last, COUNTER len,
sendpacket_t *sp, COUNTER counter, timestamp_t *sent_timestamp,
COUNTER *start_us, COUNTER *skip_length);
static void tcpr_sleep(tcpreplay_t *ctx, sendpacket_t *sp _U_,
struct timespec *nap_this_time, struct timeval *now,
tcpreplay_accurate accurate);
static u_char *get_next_packet(tcpreplay_t *ctx, pcap_t *pcap,
struct pcap_pkthdr *pkthdr,
int file_idx,
packet_cache_t **prev_packet);
static uint32_t get_user_count(tcpreplay_t *ctx, sendpacket_t *sp, COUNTER counter);
/**
* Fast flow packet edit
*
* Attempts to alter the packet IP addresses without
* changing CRC, which will avoid overhead of tcpreplay-edit
*
* This code is a bit bloated but it is the result of
* optimizing. Test performance on 10GigE+ networks if
* modifying.
*/
static void
fast_edit_packet_dl(struct pcap_pkthdr *pkthdr, u_char **pktdata,
uint32_t iteration, bool cached, int datalink)
{
int l2_len = 0;
ipv4_hdr_t *ip_hdr;
ipv6_hdr_t *ip6_hdr;
hdlc_hdr_t *hdlc_hdr;
sll_hdr_t *sll_hdr;
struct tcpr_pppserial_hdr *ppp;
uint32_t src_ip, dst_ip;
uint32_t src_ip_orig, dst_ip_orig;
uint16_t ether_type = 0;
if (pkthdr->caplen < (bpf_u_int32)TCPR_IPV6_H) {
dbgx(1, "Packet too short for Unique IP feature: %u", pkthdr->caplen);
return;
}
switch (datalink) {
case DLT_LINUX_SLL:
l2_len = 16;
sll_hdr = (sll_hdr_t *)*pktdata;
ether_type = sll_hdr->sll_protocol;
break;
case DLT_PPP_SERIAL:
l2_len = 4;
ppp = (struct tcpr_pppserial_hdr *)*pktdata;
if (ntohs(ppp->protocol) == 0x0021)
ether_type = htons(ETHERTYPE_IP);
else
ether_type = ppp->protocol;
break;
case DLT_C_HDLC:
l2_len = 4;
hdlc_hdr = (hdlc_hdr_t *)*pktdata;
ether_type = hdlc_hdr->protocol;
break;
case DLT_RAW:
if ((*pktdata[0] >> 4) == 4)
ether_type = ETHERTYPE_IP;
else if ((*pktdata[0] >> 4) == 6)
ether_type = ETHERTYPE_IP6;
break;
default:
warnx("Unable to process unsupported DLT type: %s (0x%x)",
pcap_datalink_val_to_description(datalink), datalink);
return;
}
switch (ether_type) {
case ETHERTYPE_IP:
ip_hdr = (ipv4_hdr_t *)(*pktdata + l2_len);
if (ip_hdr->ip_v != 4) {
dbgx(2, "expected IPv4 but got: %u", ip_hdr->ip_v);
return;
}
if (pkthdr->caplen < (bpf_u_int32)sizeof(*ip_hdr)) {
dbgx(2, "Packet too short for Unique IP feature: %u", pkthdr->caplen);
return;
}
ip_hdr = (ipv4_hdr_t *)(*pktdata + l2_len);
src_ip_orig = src_ip = ntohl(ip_hdr->ip_src.s_addr);
dst_ip_orig = dst_ip = ntohl(ip_hdr->ip_dst.s_addr);
break;
case ETHERTYPE_IP6:
if ((*pktdata[0] >> 4) != 6) {
dbgx(2, "expected IPv6 but got: %u", *pktdata[0] >> 4);
return;
}
if (pkthdr->caplen < (bpf_u_int32)TCPR_IPV6_H) {
dbgx(2, "Packet too short for Unique IPv6 feature: %u", pkthdr->caplen);
return;
}
ip6_hdr = (ipv6_hdr_t *)(*pktdata + l2_len);
src_ip_orig = src_ip = ntohl(ip6_hdr->ip_src.__u6_addr.__u6_addr32[3]);
dst_ip_orig = dst_ip = ntohl(ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3]);
break;
default:
return; /* non-IP */
}
/* swap src/dst IP's in a manner that does not affect CRC */
if ((!cached && dst_ip > src_ip) ||
(cached && (dst_ip - iteration) > (src_ip - 1 - iteration))) {
if (cached) {
--src_ip;
++dst_ip;
} else {
src_ip -= iteration;
dst_ip += iteration;
}
/* CRC compensations for wrap conditions */
if (src_ip > src_ip_orig && dst_ip > dst_ip_orig) {
dbgx(1, "dst_ip > src_ip(%u): before(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
--src_ip;
dbgx(1, "dst_ip > src_ip(%u): after(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
} else if (dst_ip < dst_ip_orig && src_ip < src_ip_orig) {
dbgx(1, "dst_ip > src_ip(%u): before(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
++dst_ip;
dbgx(1, "dst_ip > src_ip(%u): after(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
}
} else {
if (cached) {
++src_ip;
--dst_ip;
} else {
src_ip += iteration;
dst_ip -= iteration;
}
/* CRC compensations for wrap conditions */
if (dst_ip > dst_ip_orig && src_ip > src_ip_orig) {
dbgx(1, "src_ip > dst_ip(%u): before(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
--dst_ip;
dbgx(1, "src_ip > dst_ip(%u): after(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
} else if (src_ip < src_ip_orig && dst_ip < dst_ip_orig) {
dbgx(1, "src_ip > dst_ip(%u): before(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
++src_ip;
dbgx(1, "src_ip > dst_ip(%u): after(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
}
}
dbgx(1, "(%u): final src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
}
#if defined HAVE_NETMAP
static inline void wake_send_queues(sendpacket_t *sp, tcpreplay_opt_t *options)
{
#ifdef HAVE_NETMAP
if (options->netmap)
ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
#endif
}
#endif /* HAVE_NETMAP */
static inline void
fast_edit_packet(struct pcap_pkthdr *pkthdr, u_char **pktdata,
uint32_t iteration, bool cached, int datalink)
{
uint16_t ether_type;
vlan_hdr_t *vlan_hdr;
ipv4_hdr_t *ip_hdr = NULL;
ipv6_hdr_t *ip6_hdr = NULL;
uint32_t src_ip, dst_ip;
uint32_t src_ip_orig, dst_ip_orig;
int l2_len;
u_char *packet = *pktdata;
if (datalink != DLT_EN10MB && datalink != DLT_JUNIPER_ETHER)
fast_edit_packet_dl(pkthdr, pktdata, iteration, cached, datalink);
if (pkthdr->caplen < (bpf_u_int32)TCPR_IPV6_H) {
dbgx(2, "Packet too short for Unique IP feature: %u", pkthdr->caplen);
return;
}
l2_len = 0;
if (datalink == DLT_JUNIPER_ETHER) {
if (memcmp(packet, "MGC", 3))
warnx("No Magic Number found: %s (0x%x)",
pcap_datalink_val_to_description(datalink), datalink);
if ((packet[3] & 0x80) == 0x80) {
l2_len = ntohs(*((uint16_t*)&packet[4]));
if (l2_len > 1024) {
warnx("L2 length too long: %u", l2_len);
return;
}
l2_len += 6;
} else
l2_len = 4; /* no header extensions */
}
/* assume Ethernet, IPv4 for now */
ether_type = ntohs(((eth_hdr_t*)(packet + l2_len))->ether_type);
while (ether_type == ETHERTYPE_VLAN) {
vlan_hdr = (vlan_hdr_t *)(packet + l2_len);
ether_type = ntohs(vlan_hdr->vlan_len);
l2_len += 4;
}
l2_len += sizeof(eth_hdr_t);
switch (ether_type) {
case ETHERTYPE_IP:
ip_hdr = (ipv4_hdr_t *)(packet + l2_len);
src_ip_orig = src_ip = ntohl(ip_hdr->ip_src.s_addr);
dst_ip_orig = dst_ip = ntohl(ip_hdr->ip_dst.s_addr);
break;
case ETHERTYPE_IP6:
ip6_hdr = (ipv6_hdr_t *)(packet + l2_len);
src_ip_orig = src_ip = ntohl(ip6_hdr->ip_src.__u6_addr.__u6_addr32[3]);
dst_ip_orig = dst_ip = ntohl(ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3]);
break;
default:
return; /* non-IP */
}
dbgx(2, "Layer 3 protocol type is: 0x%04x", ether_type);
/* swap src/dst IP's in a manner that does not affect CRC */
if ((!cached && dst_ip > src_ip) ||
(cached && (dst_ip - iteration) > (src_ip - 1 - iteration))) {
if (cached) {
--src_ip;
++dst_ip;
} else {
src_ip -= iteration;
dst_ip += iteration;
}
/* CRC compensations for wrap conditions */
if (src_ip > src_ip_orig && dst_ip > dst_ip_orig) {
dbgx(1, "dst_ip > src_ip(%u): before(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
--src_ip;
dbgx(1, "dst_ip > src_ip(%u): after(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
} else if (dst_ip < dst_ip_orig && src_ip < src_ip_orig) {
dbgx(1, "dst_ip > src_ip(%u): before(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
++dst_ip;
dbgx(1, "dst_ip > src_ip(%u): after(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
}
} else {
if (cached) {
++src_ip;
--dst_ip;
} else {
src_ip += iteration;
dst_ip -= iteration;
}
/* CRC compensations for wrap conditions */
if (dst_ip > dst_ip_orig && src_ip > src_ip_orig) {
dbgx(1, "src_ip > dst_ip(%u): before(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
--dst_ip;
dbgx(1, "src_ip > dst_ip(%u): after(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
} else if (src_ip < src_ip_orig && dst_ip < dst_ip_orig) {
dbgx(1, "src_ip > dst_ip(%u): before(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
++src_ip;
dbgx(1, "src_ip > dst_ip(%u): after(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
}
}
dbgx(1, "(%u): final src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
switch (ether_type) {
case ETHERTYPE_IP:
ip_hdr->ip_src.s_addr = htonl(src_ip);
ip_hdr->ip_dst.s_addr = htonl(dst_ip);
break;
case ETHERTYPE_IP6:
ip6_hdr->ip_src.__u6_addr.__u6_addr32[3] = htonl(src_ip);
ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3] = htonl(dst_ip);
break;
}
}
/**
* \brief Update flow stats
*
* Finds out if flow is unique and updates stats.
*/
static inline void update_flow_stats(tcpreplay_t *ctx, sendpacket_t *sp,
const struct pcap_pkthdr *pkthdr, const u_char *pktdata, int datalink)
{
flow_entry_type_t res = flow_decode(ctx->flow_hash_table,
pkthdr, pktdata, datalink, ctx->options->flow_expiry);
switch (res) {
case FLOW_ENTRY_NEW:
++ctx->stats.flows;
++ctx->stats.flows_unique;
++ctx->stats.flow_packets;
if (sp) {
++sp->flows;
++sp->flows_unique;
++sp->flow_packets;
}
break;
case FLOW_ENTRY_EXISTING:
++ctx->stats.flow_packets;
if (sp)
++sp->flow_packets;
break;
case FLOW_ENTRY_EXPIRED:
++ctx->stats.flows_expired;
++ctx->stats.flows;
++ctx->stats.flow_packets;
if (sp) {
++sp->flows_expired;
++sp->flows;
++sp->flow_packets;
}
break;
case FLOW_ENTRY_NON_IP:
++ctx->stats.flow_non_flow_packets;
if (sp)
++sp->flow_non_flow_packets;
break;
case FLOW_ENTRY_INVALID:
++ctx->stats.flows_invalid_packets;
if (sp)
++sp->flows_invalid_packets;
break;
}
}
/**
* \brief Preloads the memory cache for the given pcap file_idx
*
* Preloading can be used with or without --loop
*/
void
preload_pcap_file(tcpreplay_t *ctx, int idx)
{
tcpreplay_opt_t *options = ctx->options;
char *path = options->sources[idx].filename;
pcap_t *pcap = NULL;
char ebuf[PCAP_ERRBUF_SIZE];
const u_char *pktdata = NULL;
struct pcap_pkthdr pkthdr;
packet_cache_t *cached_packet = NULL;
packet_cache_t **prev_packet = &cached_packet;
COUNTER packetnum = 0;
int dlt;
/* close stdin if reading from it (needed for some OS's) */
if (strncmp(path, "-", 1) == 0)
if (close(1) == -1)
warnx("unable to close stdin: %s", strerror(errno));
if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
errx(-1, "Error opening pcap file: %s", ebuf);
dlt = pcap_datalink(pcap);
/* loop through the pcap. get_next_packet() builds the cache for us! */
while ((pktdata = get_next_packet(ctx, pcap, &pkthdr, idx, prev_packet)) != NULL) {
packetnum++;
if (options->flow_stats)
update_flow_stats(ctx, NULL, &pkthdr, pktdata, dlt);
}
/* mark this file as cached */
options->file_cache[idx].cached = TRUE;
options->file_cache[idx].dlt = dlt;
pcap_close(pcap);
}
static void increment_iteration(tcpreplay_t *ctx)
{
tcpreplay_opt_t *options = ctx->options;
ctx->last_unique_iteration = ctx->unique_iteration;
++ctx->iteration;
if (options->unique_ip) {
assert(options->unique_loops > 0.0);
ctx->unique_iteration =
(COUNTER)((float)ctx->iteration / options->unique_loops) + 1;
}
}
/**
* the main loop function for tcpreplay. This is where we figure out
* what to do with each packet
*/
void
send_packets(tcpreplay_t *ctx, pcap_t *pcap, int idx)
{
struct timeval print_delta, now, first_pkt_ts, pkt_ts_delta;
tcpreplay_opt_t *options = ctx->options;
COUNTER packetnum = 0;
COUNTER limit_send = options->limit_send;
struct pcap_pkthdr pkthdr;
u_char *pktdata = NULL;
sendpacket_t *sp = ctx->intf1;
COUNTER pktlen;
packet_cache_t *cached_packet = NULL;
packet_cache_t **prev_packet = NULL;
#if defined TCPREPLAY && defined TCPREPLAY_EDIT
struct pcap_pkthdr *pkthdr_ptr;
#endif
int datalink = options->file_cache[idx].dlt;
COUNTER skip_length = 0;
COUNTER start_us;
COUNTER end_us;
bool preload = options->file_cache[idx].cached;
bool top_speed = (options->speed.mode == speed_topspeed ||
(options->speed.mode == speed_mbpsrate && options->speed.speed == 0));
bool now_is_now;
ctx->skip_packets = 0;
start_us = TIMEVAL_TO_MICROSEC(&ctx->stats.start_time);
timerclear(&first_pkt_ts);
if (options->limit_time > 0)
end_us = start_us + SEC_TO_MICROSEC(options->limit_time);
else
end_us = 0;
if (options->preload_pcap) {
prev_packet = &cached_packet;
} else {
prev_packet = NULL;
}
if (ctx->first_time) {
gettimeofday(&now, NULL);
now_is_now = true;
}
if (!top_speed) {
gettimeofday(&now, NULL);
now_is_now = true;
start_us = TIMEVAL_TO_MICROSEC(&now);
ctx->first_time = true;
} else {
now_is_now = false;
}
/* MAIN LOOP
* Keep sending while we have packets or until
* we've sent enough packets
*/
while (!ctx->abort &&
(pktdata = get_next_packet(ctx, pcap, &pkthdr, idx, prev_packet)) != NULL) {
now_is_now = false;
packetnum++;
#if defined TCPREPLAY || defined TCPREPLAY_EDIT
/* do we use the snaplen (caplen) or the "actual" packet len? */
pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr.len : (COUNTER)pkthdr.caplen;
#elif TCPBRIDGE
pktlen = (COUNTER)pkthdr.caplen;
#else
#error WTF??? We should not be here!
#endif
dbgx(2, "packet " COUNTER_SPEC " caplen " COUNTER_SPEC, packetnum, pktlen);
/* Dual nic processing */
if (ctx->intf2 != NULL) {
sp = (sendpacket_t *) cache_mode(ctx, options->cachedata, packetnum);
/* sometimes we should not send the packet */
if (sp == TCPR_DIR_NOSEND)
continue;
}
#if defined TCPREPLAY && defined TCPREPLAY_EDIT
pkthdr_ptr = &pkthdr;
if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
}
pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
#endif
/* do we need to print the packet via tcpdump? */
#ifdef ENABLE_VERBOSE
if (options->verbose)
tcpdump_print(options->tcpdump, &pkthdr, pktdata);
#endif
if (ctx->options->unique_ip && ctx->unique_iteration &&
ctx->unique_iteration > ctx->last_unique_iteration) {
/* edit packet to ensure every pass has unique IP addresses */
fast_edit_packet(&pkthdr, &pktdata, ctx->unique_iteration - 1,
preload, datalink);
}
/* update flow stats */
if (options->flow_stats && !preload)
update_flow_stats(ctx,
options->cache_packets ? sp : NULL, &pkthdr, pktdata, datalink);
if (ctx->first_time) {
/* get timestamp of the first packet */
memcpy(&first_pkt_ts, &pkthdr.ts, sizeof(struct timeval));
}
/*
* this accelerator improves performance by avoiding expensive
* time stamps during periods where we have fallen behind in our
* sending
*/
if (skip_length && pktlen < skip_length) {
skip_length -= pktlen;
} else if (ctx->skip_packets) {
--ctx->skip_packets;
} else {
/*
* time stamping is expensive, but now is the
* time to do it.
*/
dbgx(4, "This packet time: " TIMEVAL_FORMAT, pkthdr.ts.tv_sec, pkthdr.ts.tv_usec);
skip_length = 0;
ctx->skip_packets = 0;
/*
* Only sleep if we're not in top speed mode (-t)
* or if the current packet is not late.
*
* This also sets skip_length which will avoid timestamping for
* a given number of packets.
*/
timersub(&pkthdr.ts, &first_pkt_ts, &pkt_ts_delta);
if (calc_sleep_time(ctx, &pkt_ts_delta, &ctx->stats.last_time, pktlen, sp, packetnum,
&ctx->stats.end_time, &start_us, &skip_length)) {
now_is_now = true;
gettimeofday(&now, NULL);
}
/*
* track the time of the "last packet sent". Again, because of OpenBSD
* we have to do a memcpy rather then assignment.
*
* A number of 3rd party tools generate bad timestamps which go backwards
* in time. Hence, don't update the "last" unless pkthdr.ts > last
*/
if (!top_speed && timercmp(&ctx->stats.last_time, &pkt_ts_delta, <))
memcpy(&ctx->stats.last_time, &pkt_ts_delta, sizeof(struct timeval));
/*
* we know how long to sleep between sends, now do it.
*/
if (timesisset(&ctx->nap))
tcpr_sleep(ctx, sp, &ctx->nap, &now, options->accurate);
}
dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
/* write packet out on network */
if (sendpacket(sp, pktdata, pktlen, &pkthdr) < (int)pktlen)
warnx("Unable to send packet: %s", sendpacket_geterr(sp));
/*
* mark the time when we sent the last packet
*
* we have to cast the ts, since OpenBSD sucks
* had to be special and use bpf_timeval.
*/
memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
#ifdef TIMESTAMP_TRACE
add_timestamp_trace_entry(pktlen, &ctx->stats.end_time, skip_length);
#endif
ctx->stats.pkts_sent++;
ctx->stats.bytes_sent += pktlen;
/* print stats during the run? */
if (options->stats > 0) {
if (! timerisset(&ctx->stats.last_print)) {
memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
} else {
timersub(&now, &ctx->stats.last_print, &print_delta);
if (print_delta.tv_sec >= options->stats) {
memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
packet_stats(&ctx->stats);
memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
}
}
}
#if defined HAVE_NETMAP
if (sp->first_packet) {
wake_send_queues(sp, options);
sp->first_packet = false;
}
#endif
/* stop sending based on the duration limit... */
if ((end_us > 0 && TIMEVAL_TO_MICROSEC(&now) > end_us) ||
/* ... or stop sending based on the limit -L? */
(limit_send > 0 && ctx->stats.pkts_sent >= limit_send)) {
ctx->abort = true;
}
} /* while */
#ifdef HAVE_NETMAP
/* when completing test, wait until the last packet is sent */
if (options->netmap && (ctx->abort || options->loop == 1)) {
while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
gettimeofday(&now, NULL);
now_is_now = true;
}
while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
gettimeofday(&now, NULL);
now_is_now = true;
}
}
#endif /* HAVE_NETMAP */
if (!now_is_now)
gettimeofday(&now, NULL);
memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
increment_iteration(ctx);
}
/**
* the alternate main loop function for tcpreplay. This is where we figure out
* what to do with each packet when processing two files a the same time
*/
void
send_dual_packets(tcpreplay_t *ctx, pcap_t *pcap1, int cache_file_idx1, pcap_t *pcap2, int cache_file_idx2)
{
struct timeval print_delta, now, first_pkt_ts, pkt_ts_delta;
tcpreplay_opt_t *options = ctx->options;
COUNTER packetnum = 0;
COUNTER limit_send = options->limit_send;
int cache_file_idx;
struct pcap_pkthdr pkthdr1, pkthdr2;
u_char *pktdata1 = NULL, *pktdata2 = NULL, *pktdata = NULL;
sendpacket_t *sp;
COUNTER pktlen;
packet_cache_t *cached_packet1 = NULL, *cached_packet2 = NULL;
packet_cache_t **prev_packet1 = NULL, **prev_packet2 = NULL;
struct pcap_pkthdr *pkthdr_ptr;
int datalink;
COUNTER start_us;
COUNTER end_us;
COUNTER skip_length = 0;
bool top_speed = (options->speed.mode == speed_topspeed ||
(options->speed.mode == speed_mbpsrate && options->speed.speed == 0));
bool now_is_now;
ctx->skip_packets = 0;
start_us = TIMEVAL_TO_MICROSEC(&ctx->stats.start_time);
timerclear(&first_pkt_ts);
if (options->limit_time > 0)
end_us = start_us + SEC_TO_MICROSEC(options->limit_time);
else
end_us = 0;
if (options->preload_pcap) {
prev_packet1 = &cached_packet1;
prev_packet2 = &cached_packet2;
} else {
prev_packet1 = NULL;
prev_packet2 = NULL;
}
pktdata1 = get_next_packet(ctx, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
pktdata2 = get_next_packet(ctx, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
if (ctx->first_time) {
gettimeofday(&now, NULL);
now_is_now = true;
}
if (!top_speed) {
gettimeofday(&now, NULL);
now_is_now = true;
start_us = TIMEVAL_TO_MICROSEC(&now);
ctx->first_time = true;
} else {
now_is_now = false;
}
/* MAIN LOOP
* Keep sending while we have packets or until
* we've sent enough packets
*/
while (!ctx->abort &&
!(pktdata1 == NULL && pktdata2 == NULL)) {
now_is_now = false;
packetnum++;
/* figure out which pcap file we need to process next
* when get_next_packet() returns null for pktdata, the pkthdr
* will still have the old values from the previous call. This
* means we can't always trust the timestamps to tell us which
* file to process.
*/
if (pktdata1 == NULL) {
/* file 2 is next */
sp = ctx->intf2;
datalink = options->file_cache[cache_file_idx2].dlt;
pkthdr_ptr = &pkthdr2;
cache_file_idx = cache_file_idx2;
pktdata = pktdata2;
} else if (pktdata2 == NULL) {
/* file 1 is next */
sp = ctx->intf1;
datalink = options->file_cache[cache_file_idx1].dlt;
pkthdr_ptr = &pkthdr1;
cache_file_idx = cache_file_idx1;
pktdata = pktdata1;
} else if (timercmp(&pkthdr1.ts, &pkthdr2.ts, <=)) {
/* file 1 is next */
sp = ctx->intf1;
datalink = options->file_cache[cache_file_idx1].dlt;
pkthdr_ptr = &pkthdr1;
cache_file_idx = cache_file_idx1;
pktdata = pktdata1;
} else {
/* file 2 is next */
sp = ctx->intf2;
datalink = options->file_cache[cache_file_idx2].dlt;
pkthdr_ptr = &pkthdr2;
cache_file_idx = cache_file_idx2;
pktdata = pktdata2;
}
#if defined TCPREPLAY || defined TCPREPLAY_EDIT
/* do we use the snaplen (caplen) or the "actual" packet len? */
pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
#elif TCPBRIDGE
pktlen = (COUNTER)pkthdr_ptr->caplen;
#else
#error WTF??? We should not be here!
#endif
dbgx(2, "packet " COUNTER_SPEC " caplen " COUNTER_SPEC, packetnum, pktlen);
#if defined TCPREPLAY && defined TCPREPLAY_EDIT
if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
}
pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
#endif
/* do we need to print the packet via tcpdump? */
#ifdef ENABLE_VERBOSE
if (options->verbose)
tcpdump_print(options->tcpdump, pkthdr_ptr, pktdata);
#endif
if (ctx->options->unique_ip && ctx->unique_iteration &&
ctx->unique_iteration > ctx->last_unique_iteration) {
/* edit packet to ensure every pass is unique */
fast_edit_packet(pkthdr_ptr, &pktdata, ctx->unique_iteration - 1,
options->file_cache[cache_file_idx].cached, datalink);
}
/* update flow stats */
if (options->flow_stats && !options->file_cache[cache_file_idx].cached)
update_flow_stats(ctx, sp, pkthdr_ptr, pktdata, datalink);
if (ctx->first_time) {
/* get timestamp of the first packet */
memcpy(&first_pkt_ts, &pkthdr_ptr->ts, sizeof(struct timeval));
}
/*
* this accelerator improves performance by avoiding expensive
* time stamps during periods where we have fallen behind in our
* sending
*/
if (skip_length && pktlen < skip_length) {
skip_length -= pktlen;
} else if (ctx->skip_packets) {
--ctx->skip_packets;
} else {
/*
* time stamping is expensive, but now is the
* time to do it.
*/
dbgx(4, "This packet time: " TIMEVAL_FORMAT, pkthdr_ptr->ts.tv_sec, pkthdr_ptr->ts.tv_usec);
skip_length = 0;
ctx->skip_packets = 0;
/*
* Only sleep if we're not in top speed mode (-t)
* or if the current packet is not late.
*
* This also sets skip_length which will avoid timestamping for
* a given number of packets.
*/
timersub(&pkthdr_ptr->ts, &first_pkt_ts, &pkt_ts_delta);
if (calc_sleep_time(ctx, &pkt_ts_delta, &ctx->stats.last_time, pktlen, sp, packetnum,
&ctx->stats.end_time, &start_us, &skip_length)) {
now_is_now = true;
gettimeofday(&now, NULL);
}
/*
* track the time of the "last packet sent". Again, because of OpenBSD
* we have to do a memcpy rather then assignment.
*
* A number of 3rd party tools generate bad timestamps which go backwards
* in time. Hence, don't update the "last" unless pkthdr.ts > last
*/
if (!top_speed && timercmp(&ctx->stats.last_time, &pkt_ts_delta, <))
memcpy(&ctx->stats.last_time, &pkt_ts_delta, sizeof(struct timeval));
/*
* we know how long to sleep between sends, now do it.
*/
if (timesisset(&ctx->nap))
tcpr_sleep(ctx, sp, &ctx->nap, &now, options->accurate);
}
dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
/* write packet out on network */
if (sendpacket(sp, pktdata, pktlen, pkthdr_ptr) < (int)pktlen)
warnx("Unable to send packet: %s", sendpacket_geterr(sp));
/*
* mark the time when we sent the last packet
*
* we have to cast the ts, since OpenBSD sucks
* had to be special and use bpf_timeval.
*/
memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
ctx->stats.pkts_sent++;
ctx->stats.bytes_sent += pktlen;
/* print stats during the run? */
if (options->stats > 0) {
if (! timerisset(&ctx->stats.last_print)) {
memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
} else {
timersub(&now, &ctx->stats.last_print, &print_delta);
if (print_delta.tv_sec >= options->stats) {
memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
packet_stats(&ctx->stats);
memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
}
}
}
#if defined HAVE_NETMAP
if (sp->first_packet) {
wake_send_queues(sp, options);
sp->first_packet = false;
}
#endif
/* get the next packet for this file handle depending on which we last used */
if (sp == ctx->intf2) {
pktdata2 = get_next_packet(ctx, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
} else {
pktdata1 = get_next_packet(ctx, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
}
/* stop sending based on the duration limit... */
if ((end_us > 0 && TIMEVAL_TO_MICROSEC(&now) > end_us) ||
/* ... or stop sending based on the limit -L? */
(limit_send > 0 && ctx->stats.pkts_sent >= limit_send)) {
ctx->abort = true;
}
} /* while */
#ifdef HAVE_NETMAP
/* when completing test, wait until the last packet is sent */
if (options->netmap && (ctx->abort || options->loop == 1)) {
while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
gettimeofday(&now, NULL);
now_is_now = true;
}
while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
gettimeofday(&now, NULL);
now_is_now = true;
}
}
#endif /* HAVE_NETMAP */
if (!now_is_now)
gettimeofday(&now, NULL);
memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
increment_iteration(ctx);
}
/**
* Gets the next packet to be sent out. This will either read from the pcap file
* or will retrieve the packet from the internal cache.
*
* The parameter prev_packet is used as the parent of the new entry in the cache list.
* This should be NULL on the first call to this function for each file and
* will be updated as new entries are added (or retrieved) from the cache list.
*/
u_char *
get_next_packet(tcpreplay_t *ctx, pcap_t *pcap, struct pcap_pkthdr *pkthdr, int idx,
packet_cache_t **prev_packet)
{
tcpreplay_opt_t *options = ctx->options;