-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrpr.cpp
More file actions
executable file
·5391 lines (4987 loc) · 177 KB
/
trpr.cpp
File metadata and controls
executable file
·5391 lines (4987 loc) · 177 KB
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
/*********************************************************************
*
* AUTHORIZATION TO USE AND DISTRIBUTE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that:
*
* (1) source code distributions retain this paragraph in its entirety,
*
* (2) distributions including binary code include this paragraph in
* its entirety in the documentation or other materials provided
* with the distribution, and
*
* (3) all advertising materials mentioning features or use of this
* software display the following acknowledgment:
*
* "This product includes software written and developed
* by Brian Adamson and Joe Macker of the Naval Research
* Laboratory (NRL)."
*
* The name of NRL, the name(s) of NRL employee(s), or any entity
* of the United States Government may not be used to endorse or
* promote products derived from this software, nor does the
* inclusion of the NRL written and developed software directly or
* indirectly suggest NRL or United States Government endorsement
* of this product.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
********************************************************************/
/* allow files >2GB */
#define _LARGEFILE_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // for PATH_MAX
#include <string.h>
#include <math.h>
#include <ctype.h> // for isspace()
#include <assert.h>
#ifdef WIN32
#include <winsock2.h>
#include <float.h> // for _isnan()
#define PATH_MAX MAX_PATH
inline int isnan(double x) {return _isnan(x);}
#else
#include <unistd.h>
#include <errno.h>
#include <sys/time.h> // for gettimeofday()
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif // !WIN32
#define VERSION "2.1b11"
#ifndef MIN
#define MIN(X,Y) ((X<Y)?X:Y)
#define MAX(X,Y) ((X>Y)?X:Y)
#endif // !MIN
unsigned lineCount = 0;
const int MAX_LINE = 65536;
enum TraceFormat {TCPDUMP, MGEN, NS};
enum PlotMode {RATE, INTERARRIVAL, LATENCY, DROPS, LOSS, LOSS2, COUNT, VELOCITY};
class FastReader
{
public:
enum Result {OK, ERROR_, DONE, TIMEOUT};
FastReader();
FastReader::Result Read(FILE* filePtr, char* buffer, unsigned int* len,
double timeout = -1.0);
FastReader::Result Readline(FILE* filePtr, char* buffer, unsigned int* len,
double timeout = -1.0);
private:
enum {BUFSIZE = 2048};
char savebuf[BUFSIZE];
char* saveptr;
unsigned int savecount;
}; // end class FastReader
#ifndef WIN32
// (No real-time TRPR support for WIN32 yet)
class Waiter
{
public:
Waiter();
void Reset();
bool Wait(double seconds);
private:
struct timeval last_time;
double excess;
}; // end class Waiter
Waiter::Waiter()
: excess(0.0)
{
Reset();
}
void Waiter::Reset()
{
struct timezone tz;
gettimeofday(&last_time, &tz);
excess = 0.0;
} // end Waiter::Reset()
bool Waiter::Wait(double delay)
{
delay -= excess;
struct timeval timeout;
if (delay >= (double)0.0)
{
timeout.tv_sec = (unsigned long) delay;
timeout.tv_usec = (unsigned long)(1000000.0 * (delay - timeout.tv_sec));
}
else
{
timeout.tv_sec = timeout.tv_usec = 0;
}
fd_set fdSet;
FD_ZERO(&fdSet);
select(0, (fd_set*)NULL, (fd_set*)NULL, (fd_set*)NULL, &timeout);
struct timeval thisTime;
struct timezone tz;
gettimeofday(&thisTime, &tz);
double actual = thisTime.tv_sec - last_time.tv_sec;
if (thisTime.tv_usec < last_time.tv_usec)
actual -= ((double)(thisTime.tv_usec - last_time.tv_usec)) * 1.0e-06;
else
actual += ((double)(thisTime.tv_usec - last_time.tv_usec)) * 1.0e-06;
excess = actual - delay;
if (excess < -2.0)
{
fprintf(stderr, "Waiter::Wait() Warning! dropping behind real time ...\n");
excess = 0.0;
}
memcpy(&last_time, &thisTime, sizeof(struct timeval));
return true;
} // end Waiter::Wait()
#endif // !WIN32
class FlowId
{
public:
FlowId() : valid(false) {}
FlowId(unsigned long x) : valid(true), value(x) {}
bool IsValid() const {return valid;}
unsigned long Value() const {return value;}
void Invalidate() {valid = false;}
operator unsigned long() const {return value;}
bool Match(unsigned long x) const;
private:
bool valid;
unsigned long value;
}; // end class FlowId
bool FlowId::Match(unsigned long x) const
{
if (!valid || (x == value))
return true;
else
return false;
} // end FlowId::Match()
class Address
{
public:
enum Domain {IPv4, IPv6, OTHER};
Address() {addr[0] = '\0';}
Address(unsigned long value, Domain domain) {Set(value, domain);}
Address(const char* string) {Set(string);}
void Set(const char* string)
{
strncpy(addr, string, 63);
addr[63] = '\0';
}
void Set(unsigned long value, Domain domain)
{
if (IPv4 == domain)
{
unsigned char* a = (unsigned char*)&value;
sprintf(addr, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
}
else if (((unsigned long)-1) == value)
{
strcpy(addr, "-1");
}
else
{
sprintf(addr, "%lu", value);
}
}
// buf is network order IPv6 address
void SetIPv6(unsigned long* buf)
{
addr[0] = '\0';
for (unsigned int i = 0; i < 4; i++)
{
unsigned short* a = (unsigned short*)(buf+i);
char temp[16];
sprintf(temp, "%x:%x%s", ntohs(a[0]), ntohs(a[1]),
(i < 3) ? ":" : "");
strcat(addr, temp);
}
}
void Invalidate() {addr[0] = '\0';}
bool IsValid() const {return ('\0' != addr[0]);}
bool operator==(const Address& x) const
{return (0 == strcmp(addr, x.addr));}
void PrintDescription(FILE* f) const {fprintf(f, "%s", addr);}
//private:
char addr[64];
}; // end class Address
class PacketEvent
{
public:
enum EventType {INVALID, RECEPTION, TRANSMISSION, DROP, LOSS, TIMEOUT};
PacketEvent();
~PacketEvent();
// Generalized tracepoint node(a,[b]) NS
class TracePoint
{
public:
TracePoint() : src_port(-1), dst_port(-1) {}
TracePoint(const Address& sa) : src_addr(sa), src_port(-1), dst_port(-1) {}
TracePoint(const Address& sa, unsigned short sp) : src_addr(sa), src_port(sp), dst_port(-1) {}
TracePoint(const Address& sa, unsigned short sp, const Address& da, unsigned short dp)
: src_addr(sa), src_port(sp), dst_addr(da), dst_port(dp) {}
bool Match(const TracePoint& p) const;
const Address& SrcAddr() const {return src_addr;}
unsigned short SrcPort() const {return src_port;}
const Address& DstAddr() const {return dst_addr;}
unsigned short DstPort() const {return dst_port;}
bool IsValid() const {return (src_addr.IsValid() || dst_addr.IsValid());}
void Invalidate()
{
src_addr.Invalidate();
src_port = -1;
dst_addr.Invalidate();
dst_port = -1;
}
void SetSrc(const Address& srcAddr)
{
src_addr = srcAddr;
src_port = -1;
}
void SetSrc(const Address& srcAddr, unsigned short srcPort)
{
src_addr = srcAddr;
src_port = srcPort;
}
void SetDst(const Address& dstAddr)
{
dst_addr = dstAddr;
dst_port = -1;
}
void SetDst(const Address& dstAddr, unsigned short dstPort)
{
dst_addr = dstAddr;
dst_port = -1;
}
void PrintDescription(FILE* f) const
{
src_addr.PrintDescription(f);
if (src_port >= 0) fprintf(f, ":%d", src_port);
fprintf(f, ", ");
dst_addr.PrintDescription(f);
if (dst_port >= 0) fprintf(f, ":%d", dst_port);
}
private:
Address src_addr;
int src_port; // -1 is invalid
Address dst_addr;
int dst_port; // -1 is invalid
}; // end class TracePoint
EventType Type() {return type;}
void SetType(EventType theType) {type = theType;}
double Time() {return time;}
void SetTime(double theTime) {time = theTime;}
PacketEvent::TracePoint& Link() {return link;}
void LinkClear() {link.Invalidate();}
void SetLinkSrc(const Address& srcAddr)
{link.SetSrc(srcAddr);}
void SetLinkSrc(const Address& srcAddr, unsigned short srcPort)
{link.SetSrc(srcAddr,srcPort);}
void SetLinkDst(const Address& dstAddr)
{link.SetDst(dstAddr);}
void SetLinkDst(const Address& dstAddr, unsigned short dstPort)
{link.SetDst(dstAddr, dstPort);}
const Address& SrcAddr() {return src_addr;}
void SetSrcAddr(const Address& theAddr) {src_addr = theAddr;}
unsigned short SrcPort() {return src_port;}
void SetSrcPort(unsigned short thePort) {src_port = thePort;}
const Address& DstAddr() {return dst_addr;}
void SetDstAddr(const Address& theAddr) {dst_addr = theAddr;}
unsigned short DstPort() {return dst_port;}
void SetDstPort(unsigned short thePort) {dst_port = thePort;}
const char* Protocol() {return protocol;}
bool SetProtocol(const char* name);
unsigned int Size() {return size;}
void SetSize(unsigned int theSize) {size = theSize;}
double RxTime() {return rx_time;}
void SetRxTime(double theTime) {rx_time = theTime;}
double TxTime() {return tx_time;}
void SetTxTime(double theTime) {tx_time = theTime;}
unsigned long Sequence() {return sequence;}
void SetSequence(unsigned long theSequence) {sequence = theSequence;}
unsigned long GetFlowIdValue() {return flow_id.Value();}
void SetFlowId(unsigned long id) {flow_id = id;}
void InvalidateFlowId() {flow_id.Invalidate();}
void SetPosition(double x, double y) {pos_x = x; pos_y = y;}
double PosX() {return pos_x;}
double PosY() {return pos_y;}
private:
EventType type;
double time;
TracePoint link;
Address src_addr; // (TBD) Use NetworkAddress class here?
int src_port;
Address dst_addr;
int dst_port;
unsigned int size;
char* protocol;
double tx_time;
double rx_time;
unsigned long sequence;
::FlowId flow_id;
double pos_x; // mgen GPS only
double pos_y; // mgen GPS only
}; // end class PacketEvent
PacketEvent::PacketEvent()
: type(INVALID), time(-1.0), src_port(-1), dst_port(-1), size(0), protocol(NULL),
pos_x(999.0), pos_y(999.0)
{
}
PacketEvent::~PacketEvent()
{
if (protocol) delete protocol;
}
bool PacketEvent::SetProtocol(const char* name)
{
if (protocol) delete protocol;
unsigned int len = name ? strlen(name) + 1 : 0;
if (len)
{
protocol = new char[len];
if (protocol)
{
strcpy(protocol, name);
return true;
}
else
{
perror("trpr: PacketEvent::SetProtocol() \"new()\" error");
return false;
}
}
else
{
protocol = NULL;
return true;
}
} // end PacketEvent::SetProtocol()
bool PacketEvent::TracePoint::Match(const TracePoint& p) const
{
if ((!src_addr.IsValid() || src_addr == p.SrcAddr()) &&
((-1 == src_port) || (src_port == p.SrcPort())) &&
(!dst_addr.IsValid() || dst_addr == p.DstAddr()) &&
((-1 == dst_port) || (dst_port == p.DstPort())))
{
//fprintf(stderr, "");
return true;
}
else
{
return false;
}
} // end PacketEvent::TracePoint::Match()
class EventParser
{
public:
virtual bool GetNextPacketEvent(FILE* filePtr,
PacketEvent* theEvent,
double timeout = -1.0) = 0;
protected:
FastReader reader;
}; // end class EventParser
class NsEventParser : public EventParser
{
public:
enum NodeType {AGT, RTR, MAC};
bool GetNextPacketEvent(FILE* filePtr,
PacketEvent* theEvent,
double timeout = -1.0);
}; // end class EventParser
class TcpdumpEventParser : public EventParser
{
public:
bool GetNextPacketEvent(FILE* filePtr,
PacketEvent* theEvent,
double timeout = -1.0);
unsigned int PackHexLine(char* text, char* buf, unsigned int buflen);
// IP protocol version
unsigned int Version(const char* hdr) const
{return (((unsigned char)hdr[0] >> 4 ) & 0x0f);}
// IP protocol header length
unsigned int HeaderLength(const char* hdr) const
{
if(Version(hdr) == 4)
return 4 * (((unsigned char)hdr[0]) & 0x0f);
else return 40;
}
// IP protocol payload length
unsigned int PayloadLength(const char* hdr)
{return (256 * (unsigned char)hdr[4] + (unsigned char)hdr[5]);}
// Total IP or ARP packet length
unsigned int TotalLength(const char* hdr, const char* pname)
{
if (0 == strncmp(pname, "IP", 2))
{
if(Version(hdr) == 4)
return (256*((unsigned char)hdr[2]) + ((unsigned char)hdr[3]));
else
return (PayloadLength(hdr) + 40);
}
else if (0 == strncmp(pname, "ARP", 3))
{
// ARP length is 8 + 2*hlen + 2*plen
return (8 + 2*((unsigned char)hdr[4]) + 2 * ((unsigned char)hdr[5]));
}
else
{
fprintf(stderr, "trpr error: unsupported packet type \"%s\" in tcpdump trace!\n", pname);
return 0;
}
}
// IP packet protocol id
unsigned char Protocol(char* hdr)
{
if(Version(hdr) == 4)
return ((unsigned char)hdr[9]);
else
return ((unsigned char)hdr[6]);
}
// IP packet ID (sequence) field
unsigned long Sequence(char* hdr)
{
if(Version(hdr) == 4)
{
return (256*((unsigned char)hdr[4]) + ((unsigned char)hdr[5]));
}
else
{
return 0;
}
}
const char* ProtocolType(unsigned char value) const;
Address SourceAddress(const char* hdr, const char* pname) const
{
unsigned int version = 4;
int offset = 0;
if (0 == strncmp(pname, "IP", 2))
{
version = Version(hdr);
if (4 == version)
offset = 12;
else
offset = 8;
}
else if (0 == strncmp(pname, "ARP", 3))
{
// Make protocol is IP (PTYPE == 0x0800)
if ((0x08 != (unsigned char)hdr[2]) || (0x00 != (unsigned char)hdr[3]))
{
fprintf(stderr, "trpr error: unsupported ARP PTYPE 0x%02x%02x in tcpdump trace!\n", hdr[2], hdr[3]);
return Address("unknown");
}
version = 4;
offset = 8 + (unsigned char)hdr[4]; // 8 + hlen
}
if( 4 == version)
{
unsigned long buf;
buf = ((256*256*256)*((unsigned char)hdr[offset]) +
(256*256)*((unsigned char)hdr[offset+1]) +
(256)*((unsigned char)hdr[offset+2]) +
((unsigned char)hdr[offset+3]));
return Address(htonl(buf), Address::IPv4);
}
else // (Version(hdr) == 6)
{
unsigned long buf[4];
Address theAddress;
for(unsigned int i = 0; i < 4; i++)
{
buf[i] = ((256*256*256)*((unsigned char)hdr[(i*4)+offset]) +
(256*256)*((unsigned char)hdr[(i*4)+offset+1]) +
(256)*((unsigned char)hdr[(i*4)+offset+2]) +
((unsigned char)hdr[(i*4)+offset+3]));
buf[i] = htonl(buf[i]);
}
theAddress.SetIPv6(buf);
return theAddress;
}
}
Address DestinationAddress(const char* hdr, const char* pname) const
{
unsigned int version = 4;
int offset = 0;
if (0 == strncmp(pname, "IP", 2))
{
version = Version(hdr);
if (4 == version)
offset = 16;
else
offset = 24;
}
else if (0 == strncmp(pname, "ARP", 3))
{
// Make protocol is IP (PTYPE == 0x0800)
if ((0x08 != (unsigned char)hdr[2]) || (0x00 != (unsigned char)hdr[3]))
{
fprintf(stderr, "trpr error: unsupported ARP PTYPE 0x%02x%02x in tcpdump trace!\n", hdr[2], hdr[3]);
return Address("unknown");
}
version = 4;
offset = 8 + 2*(unsigned char)hdr[4] + 4; // 8 + hlen + plen + hlen
}
if( 4 == version)
{
unsigned long buf;
buf = ((256*256*256)*((unsigned char)hdr[offset]) +
(256*256)*((unsigned char)hdr[offset+1]) +
(256)*((unsigned char)hdr[offset+2]) +
((unsigned char)hdr[offset+3]));
return Address(htonl(buf), Address::IPv4);
}
else // (Version(hdr) == 6)
{
unsigned long buf[4];
Address theAddress;
for(unsigned int i = 0; i < 4; i++)
{
buf[i] = ((256*256*256)*((unsigned char)hdr[(i*4)+offset]) +
(256*256)*((unsigned char)hdr[(i*4)+offset+1]) +
(256)*((unsigned char)hdr[(i*4)+offset+2]) +
((unsigned char)hdr[(i*4)+offset+3]));
buf[i] = htonl(buf[i]);
}
theAddress.SetIPv6(buf);
return theAddress;
}
}
unsigned short SourcePort(const char* hdr) const
{
unsigned int ipHdrLen;
if(Version(hdr) == 4)
ipHdrLen = 4 * (((unsigned char)hdr[0]) & 0x0f);
else
ipHdrLen = 40;
hdr += ipHdrLen;
return (256*((unsigned char)hdr[0]) + (unsigned char)hdr[1]);
}
unsigned short DestinationPort(const char* hdr) const
{
unsigned int ipHdrLen;
if(Version(hdr) == 4)
ipHdrLen = 4 * (((unsigned char)hdr[0]) & 0x0f);
else
ipHdrLen = 40;
hdr += ipHdrLen;
return (256*((unsigned char)hdr[2]) + (unsigned char)hdr[3]);
}
}; // end class TcpdumpEventParser
class MgenEventParser : public EventParser
{
public:
bool GetNextPacketEvent(FILE* filePtr,
PacketEvent* theEvent,
double timeout = -1.0);
}; // end class mgenEventParser
class Point
{
friend class PointList;
public:
Point(double x, double y);
Point(double x, unsigned long k);
double X() {return xval;}
double Y() {return yval;}
double K() {return kval;}
Point* Prev() {return prev;}
Point* Next() {return next;}
private:
double xval;
double yval;
unsigned long kval;
Point* prev;
Point* next;
}; // end class Point()
class PointList
{
public:
PointList();
~PointList();
Point* Head() {return head;}
Point* Tail() {return tail;}
void Append(Point* thePoint);
void Remove(Point* thePoint);
void Destroy();
void PruneData(double xMin);
bool PrintData(FILE* filePtr);
Point* FindPointByK(unsigned long k);
private:
Point* head;
Point* tail;
};
class LossTracker
{
public:
LossTracker();
void Reset()
{
loss_list.Destroy();
packet_count = 0;
loss_count = 0;
duplicate_count = 0;
late_count = 0;
loss_fraction = 1.0;
}
void Init(unsigned long seqMax)
{
Reset();
SetSeqMax(seqMax);
resync_count = 0;
first_packet = true;
}
bool Update(double theTime, unsigned long theSequence, unsigned long theFlow = 0);
void SetSeqMax(unsigned long seqMax)
{
seq_max = seqMax;
seq_hlf = seqMax >> 1;
seq_qtr = seqMax >> 2;
}
double LossFraction() {return loss_fraction;}
unsigned long ResyncCount() {return resync_count;}
unsigned long DuplicateCount() {return duplicate_count;}
unsigned long LateCount() {return late_count;}
private:
PointList loss_list;
double last_time;
double loss_fraction;
long loss_max;
bool first_packet;
unsigned long packet_count;
unsigned long loss_count;
unsigned long resync_count;
unsigned long duplicate_count;
unsigned long late_count;
unsigned long seq_max;
unsigned long seq_hlf;
unsigned long seq_qtr;
unsigned long seq_last;
unsigned long flow_id; // makes sure all mgen packets from same flow
}; // end class LossTracker
// A data driven loss tracker, procrastinates as needed.
class LossTracker2
{
public:
LossTracker2();
void Init(double windowSize, unsigned long seqMax = 0xffffffff)
{
window_size = windowSize;
seq_max = seqMax;
seq_hlf = seqMax >> 1;
seq_qtr = seqMax >> 2;
first_packet = true;
packet_count = 0;
}
void Reset()
{
seq_first = seq_last;
time_first = time_last;
if (window_size > 0)
window_end = time_first + window_size;
packet_count = 1;
wrap = false;
wrap_count = 0;
}
int Update(double theTime, unsigned long theSeq, unsigned long theFlow = 0);
double LossFraction();
double LossWindowStart() {return time_first;}
double LossWindowEnd() {return time_last;}
private:
bool first_packet;
bool wrap;
unsigned long wrap_count;
double time_first;
double time_last;
double window_size;
double window_end;
unsigned long packet_count;
unsigned long seq_first;
unsigned long seq_last;
unsigned long duplicate_count;
unsigned long resync_count;
unsigned long seq_max;
unsigned long seq_hlf;
unsigned long seq_qtr;
unsigned long flow_id; // to make sure all mgen packets from same flow
}; // end class LossTracker2
class LossTracker3
{
public:
LossTracker3();
void Init(double windowSize, unsigned long seqMax = 0xffffffff)
{
window_size = windowSize;
seq_max = seqMax;
seq_sign = (seqMax ^ (seqMax >> 1)); // sign bit for sequence space
seq_qtr = seqMax >> 2;
init = true;
packet_count = 0;
}
void Reset()
{
packet_count = 1;
wrap_count = 0;
seq_first = seq_last;
time_first = time_last;
}
int Update(double theTime, unsigned long theSeq, unsigned long theFlow = 0);
bool IsDuplicate(double theTime, unsigned long theSeq, unsigned long theFlow = 0);
double LossWindowStart() {return time_first;}
double LossWindowEnd() {return time_last;}
double LossFraction();
enum {HISTORY_MAX = 4096};
private:
long SeqDelta(unsigned long a, unsigned long b)
{
long result = a - b;
return ((0 == (result & seq_sign)) ?
(result & seq_max) :
((((unsigned long)result != seq_sign) || (a < b)) ?
(result | ~seq_max) : result));
}
bool init;
unsigned long history[HISTORY_MAX];
unsigned long offset;
unsigned long seq_max;
unsigned long seq_qtr;
unsigned long seq_sign;
unsigned long seq_first;
unsigned long seq_last;
double window_size; // time window
double time_first;
double time_last;
unsigned long wrap_count;
unsigned long packet_count; // make this longlong???
unsigned long duplicate_count; // make this longlong???
}; // end class LossTracker3
LossTracker3::LossTracker3()
: init(true)
{
memset(history, 0, HISTORY_MAX*sizeof(unsigned long));
}
bool LossTracker3::IsDuplicate(double theTime, unsigned long theSeq, unsigned long theFlow)
{
unsigned long oldDupCount = duplicate_count;
Update(theTime, theSeq, theFlow);
return (oldDupCount != duplicate_count);
} // end LossTracker3::IsDuplicate()
int LossTracker3::Update(double theTime, unsigned long theSeq, unsigned long theFlow)
{
int result = 0;
if (init)
{
memset(history, 0, HISTORY_MAX*sizeof(unsigned long));
offset = (theSeq & 0xffffffc0) - 32*(HISTORY_MAX - 1);
offset &= seq_max;
packet_count = 0;
seq_first = seq_last = theSeq;
time_first = time_last = theTime;
init = false;
}
long index = SeqDelta(theSeq, offset);
if (index < -((long)seq_qtr))
{
/// Assume large outage instead of old packet
fprintf(stderr, "trpr: LossTracker3::Update() big outage? index:%ld seq_qtr:%lu, seq:%lu offset:%lu\n",
index, seq_qtr, theSeq, offset);
index += seq_max;
seq_last = theSeq;
}
else if (index < 0)
{
// Packet is quite a bit old, so consider it lost?
fprintf(stderr, "trpr: LossTracker3::Update() warning: ignoring very old packet (seq:%lu)?\n", theSeq);
return 0;
}
else if (SeqDelta(theSeq, seq_last) > 0)
{
if (theSeq < seq_last) wrap_count++;
seq_last = theSeq;
}
unsigned long word = index >> 5; // divide by 32
if (word >= HISTORY_MAX)
{
//fprintf("shifting ...
// Shift our bit mask, determining new "offset"
unsigned long wordShift = word - HISTORY_MAX + 1;
if (wordShift < HISTORY_MAX)
{
memmove(history, history+wordShift,(HISTORY_MAX - wordShift)*sizeof(unsigned long));
memset(history+(HISTORY_MAX - wordShift), 0, wordShift * sizeof(unsigned long));
offset += 32*wordShift;
offset &= seq_max;
}
else
{
memset(history, 0, HISTORY_MAX*sizeof(unsigned long));
offset = (theSeq & 0xffffffc0) - 32*(HISTORY_MAX - 1);
offset &= seq_max;
}
word = HISTORY_MAX - 1;
}
unsigned long bit = index & 0x0000007f;
bit = 0x00000001 << (31 - bit);
// Have we seen this one before?
if (0 != (history[word] & bit))
{
duplicate_count++;
}
else
{
// New packet
packet_count++;
history[word] |= bit;
}
if (window_size > 0.0)
{
if ((theTime - time_first) >= window_size)
{
// Indicate that the window has past
result = 1;
}
}
time_last = theTime;
return result;
} // end LossTracker3::Update()
double LossTracker3::LossFraction()
{
unsigned long pktsExpected = wrap_count * seq_max;
pktsExpected += SeqDelta(seq_last, seq_first) + 1;
double lossFraction = (packet_count < pktsExpected) ?
(1.0 - ((double)packet_count) / ((double)pktsExpected)) : 0.0;
return lossFraction;
} // end LossTracker3::LossFraction()
// Simple self-scaling linear/non-linear histogram (one-sided)
class Histogram
{
public:
Histogram();
void Init(unsigned long numBins, double linearity)
{
num_bins = numBins;
q = linearity;
if (bin) delete[] bin;
bin = NULL;
}
bool Tally(double value, unsigned long count = 1);
void Print(FILE* file);
unsigned long Count();
double PercentageInRange(double rangeMin, double rangeMax);
double Min() {return min_val;}
double Max() {return max_val;}
double Percentile(double p);
private:
typedef struct
{
double total;
unsigned long count;
} Bin;
double q;
unsigned long num_bins;
double min_val;
double max_val;
Bin* bin;
}; // end class Histogram
Histogram::Histogram()
: q(1.0), num_bins(1000), min_val(0.0), max_val(0.0), bin(NULL)
{
}
bool Histogram::Tally(double value, unsigned long count)
{
if (!bin)
{