-
Notifications
You must be signed in to change notification settings - Fork 1
/
IperfApp.c
1961 lines (1446 loc) · 52.4 KB
/
IperfApp.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
/******************************************************************************
MRF24WB0M Driver iperf
Module for Microchip TCP/IP Stack
-Provides access to MRF24WB0M WiFi controller
-Reference: MRF24WB0M Data sheet, IEEE 802.11 Standard
*******************************************************************************
FileName: IperfApp.c
Dependencies: TCP/IP Stack header files
Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
Compiler: Microchip C32 v1.10b or higher
Microchip C30 v3.22 or higher
Microchip C18 v3.34 or higher
Company: Microchip Technology, Inc.
Software License Agreement
Copyright (C) 2002-2010 Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy, and distribute:
(i) the Software when embedded on a Microchip microcontroller or digital
signal controller product ("Device") which is integrated into
Licensee's product; or
(ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device used in
conjunction with a Microchip ethernet controller for the sole purpose
of interfacing with the ethernet controller.
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP BE LIABLE FOR ANY INCIDENTAL,
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST
OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS BY
THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS
FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON
THE BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR
OTHERWISE.
Author Date Comment
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
KH 27 Jan 2010 Updated for MRF24WB0M
******************************************************************************/
#include <string.h> /* for memcpy */
#include "TCPIP Stack/TCPIP.h"
#if defined (WF_CONSOLE)
#include "IperfApp.h"
#include "TCPIP Stack/WFConsole.h"
#if defined(WF_DEBUG)
#define WF_MODULE_NUMBER WF_MODULE_IPERF_APP
#endif
/*****************************************************************************/
/* Constants used internally by code in this file. */
/*****************************************************************************/
#define LOCAL_PORT_START_NUMBER (1024u)
//#define kIperfRxTimeOut 1000 // 1 second.
#define kIperfUDPFinRetransmitCount 10u // iperf retransmits 10 times the last UDP packet,
#define kIperfUDPFinRetransmitPeriod 10 // at 10ms apart.
#define kIperfTimingErrorMargin 2 // Account for msec tick uncertainty.
enum {
kStateStandby=1,
kStateRxStart,
kStateUdpRx,
kStateUdpRxDrain,
kStateUdpRxDone,
kStateTcpRxListen,
kStateTcpRx,
kStateTcpRxDone,
kStateRxDone,
kStateTxStart,
kStateTxArpResolve,
kStateTcpTxOpen,
kStateTcpTxConnect,
kStateTcpTxSegment,
kStateTcpTxDone,
kStateUdpTxOpen,
kStateUdpTxDatagram,
kStateUdpTxDone
};
/*****************************************************************************/
/* Data types used internally by code in this file. */
/*****************************************************************************/
typedef enum {
kIperfProtoUDP = 1,
kIperfProtoTCP
} tIperfProto;
typedef struct
{
UINT32 mInterval; // -i
UINT32 mAmount; // -n
UINT32 mDuration; // -t. Default = 10*TICK_SECOND msec
UINT32 mDatagramSize; // -l
UINT16 mMSS; // -M
BOOL mServerMode; // -s or -c
BOOL mBufLenSet; // -l
tIperfProto mProtocol; // -b or -u
UINT16 mServerPort; // -p
UINT32 mTxRate; // -b or
// -x: NONE-STANDARD IPERF OPTION. Max Tx bps rate for TCP.
double totalLen; // mTotalLen
long pktId; // datagramID
long lastPktId; // lastDatagramID
UINT32 errorCount;
UINT32 outofOrder;
TCP_SOCKET tcpServerSock;
TCP_SOCKET tcpClientSock;
UDP_SOCKET udpSock;
SOCKET_INFO remoteSide;
UINT16 localPort;
//struct sockaddr_in remoteAddr;
// int remoteAddrlen;
// tWFSocketAddr remoteAddr;
// Calculated packet period, in msec, to reflect the target bit rate.
UINT32 mPktPeriod;
UINT32 startTime;
UINT32 stopTime;
UINT32 nextTxTime;
//UINT32 remoteStartTime;
//UINT32 remoteStopTime;
UINT8 nAttempts;
UINT32 pktCount;
BOOL stopRequested;
UINT32 lastCheckPktCount; // Check if pktCount changes within mInterval; or kIperfRxTimeOut.
long lastCheckPktId;
UINT32 lastCheckErrorCount;
UINT32 lastCheckTotalLen;
UINT32 lastCheckTime;
UINT8 statusReported;
// long mPendingACK; // number of outstanding TCP ACKs
// UINT8 mRetransmit;
UINT8 state;
UINT32 timer;
UINT16 remainingTxData;
UINT16 availUDPTxSpace;
BOOL isLastTransmit;
} tAppState;
typedef enum
{
kIntervalReport,
kSubtotalReport,
kSessionReport
} tIperfReport;
//
// Data structure used by iperf protocol
//
#define HEADER_VERSION1 0x80000000
typedef struct
{
long id;
UINT32 tv_sec;
UINT32 tv_usec;
} tIperfPktInfo; // In the original Iperf, this is the "UDP_datagram" structure.
// tUDP_datagram
typedef struct
{
UINT32 flags;
UINT32 total_len1;
UINT32 total_len2;
UINT32 stop_sec;
UINT32 stop_usec;
UINT32 error_cnt;
UINT32 outorder_cnt;
UINT32 datagrams;
UINT32 jitter1;
UINT32 jitter2;
} tServerHdr;
typedef struct
{
UINT32 flags;
UINT32 numThreads;
UINT32 mPort;
UINT32 bufferlen;
UINT32 mWinBand;
UINT32 mAmount;
} tClientHdr;
/*****************************************************************************/
/* Global variables used internally by code in this file. */
/*****************************************************************************/
#define MAX_BUFFER (sizeof(tIperfPktInfo) + sizeof(tServerHdr))
UINT8 g_bfr[ MAX_BUFFER ];
static tAppState gAppState;
#define APPCTX (gAppState)
//
// Helper utilities
//
/* On the Microchip v4.51 stack the MSS is a compile time setting and not within the control */
/* of the application to change on a per socket basis, nor is it even a runtime configurable */
/* setting. Search tcp.c for TCP_MAX_SEG_SIZE. */
/* However, TCP_MAX_SEG_SIZE is not a public macro. */
/* RFC 879 specifies the default MSS to be 536. Hence we assume this number here. */
/* (WF_IPERF_TCP_MSS == TCP_MAX_SEG_SIZE) is desirable but not required. */
#define WF_IPERF_TCP_MSS 536
static void
ResetIperfCounters(void)
{
// APPCTX.mAmount = 0;
// APPCTX.mDuration = 10*1000; // -t: default 10 sec
// APPCTX.mInterval = 1000; // -i: default 1 sec
APPCTX.mMSS = WF_IPERF_TCP_MSS;
APPCTX.mDatagramSize = 1470; // -l: default 1470 bytes. UDP datagram size.
APPCTX.totalLen = 0;
APPCTX.pktId = 0;
APPCTX.lastPktId = 0;
APPCTX.errorCount = 0;
APPCTX.outofOrder = 0;
APPCTX.pktCount = 0;
APPCTX.statusReported = 0;
APPCTX.startTime = 0;
APPCTX.stopTime = 0;
APPCTX.lastCheckPktCount = 0;
APPCTX.lastCheckPktId = 0;
APPCTX.lastCheckErrorCount = 0;
APPCTX.lastCheckTotalLen = 0;
APPCTX.lastCheckTime = 0;
APPCTX.isLastTransmit = FALSE;
// APPCTX.mPendingACK = 0;
// APPCTX.mRetransmit = 0;
}
static void
ascii_to_u32s(INT8 *ptr, UINT32 *values, UINT8 count)
{
UINT8 i;
UINT32 tmp;
// Convert "123.456_78_90k", with count set to 4, to
// unsigned 32-bit numbers 123, 456, 78 and 90000, and
// store them in the values array.
for (i = 0; i < count; i++)
{
tmp = 0;
while ( (*ptr > (INT8)'9') || (*ptr < (INT8)'0') )
{
if ( (*ptr == (INT8)' ') || (*ptr == (INT8)0) ) return; // terminates at blank or NULL.
ptr++;
}
while ( (*ptr <= (INT8)'9') && (*ptr >= (INT8)'0') )
{
tmp = tmp*10 + *ptr - '0';
ptr++;
}
if ( (*ptr == (INT8)'k') || (*ptr == (INT8)'K') )
{
tmp = tmp * 1000;
ptr++;
}
else if ( (*ptr == (INT8)'m') || (*ptr == (INT8)'M') )
{
tmp = tmp * 1000 * 1000;
ptr++;
}
values[i] = tmp;
}
}
//
// Report bandwith, jitter, and packet loss stastistics.
// Used by in both server and client modes.
//
// Todo: implement the jitter report.
//
static void
ReportBW_Jitter_Loss(tIperfReport reportType)
{
UINT32 nAttempted;
UINT32 nDropped;
double kbps;
UINT32 currentTime;
UINT32 sec;
UINT32 msec;
currentTime = IPERF_GET_MSEC_TICK_COUNT();
switch ( reportType )
{
case kIntervalReport:
nDropped = APPCTX.errorCount - APPCTX.lastCheckErrorCount;
// bits-per-msec == Kbps
sec = (currentTime- APPCTX.lastCheckTime)/TICK_SECOND;
msec = ((double) (currentTime - APPCTX.lastCheckTime)) / (((double)(TICK_SECOND))/1000);
if ( APPCTX.state == (UINT8)kStateUdpTxDone )
{
nAttempted = (APPCTX.lastPktId - APPCTX.lastCheckPktId) + nDropped;
}
else
{
nAttempted = APPCTX.pktId - APPCTX.lastCheckPktId;
}
if ( msec == 0u )
{
kbps = 0;
}
else
{
kbps = ((APPCTX.totalLen - APPCTX.lastCheckTotalLen)*((double) 8)) / msec;
}
sec = (APPCTX.lastCheckTime - APPCTX.startTime)/TICK_SECOND;
sprintf( (char *) g_ConsoleContext.txBuf," - [%2lu- %2lu sec] %3lu/ %3lu (%2lu%%) %4lu Kbps",
(unsigned long)sec,
(unsigned long)sec + ( (unsigned long) (APPCTX.mInterval/TICK_SECOND) ),
(unsigned long)nDropped,
(unsigned long)nAttempted,
(unsigned long)nDropped*100/(unsigned long)nAttempted,
(unsigned long) (kbps + ((double) 0.5)));
WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE);
break;
case kSubtotalReport:
// intentional fall-through
case kSessionReport:
nDropped = APPCTX.errorCount;
if (APPCTX.state == (UINT8)kStateUdpTxDone)
{
nAttempted = APPCTX.lastPktId + nDropped;
}
else
{
nAttempted = APPCTX.lastPktId;
}
msec = ((double) (APPCTX.stopTime - APPCTX.startTime)) / (((double)(TICK_SECOND))/1000);
if ( msec == 0u )
{
kbps = 0;
}
else
{
kbps = (APPCTX.totalLen * ((double) 8)) / msec;
}
sprintf( (char *) g_ConsoleContext.txBuf, " - [0.0- %lu.%lu sec] %3lu/ %3lu (%2lu%%) %4lu Kbps",
(unsigned long)(msec/1000),
(unsigned long)((msec%1000)/100),
(unsigned long)nDropped,
(unsigned long)nAttempted,
(nAttempted == 0u) ? 0 : ((unsigned long)nDropped*100/(unsigned long)nAttempted),
(unsigned long) (kbps + ((double) 0.5)));
WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE);
break;
}
if ( reportType == kSessionReport )
{
WFConsolePrintRomStr(" Session completed ...", TRUE);
}
APPCTX.lastCheckPktId = APPCTX.pktId;
APPCTX.lastCheckErrorCount = APPCTX.errorCount;
APPCTX.lastCheckPktCount = APPCTX.pktCount;
APPCTX.lastCheckTime = currentTime;
APPCTX.lastCheckTotalLen = APPCTX.totalLen;
}
void IperfAppInit(void)
{
memset( &APPCTX, 0, sizeof(tAppState) );
APPCTX.state = kStateStandby;
APPCTX.stopRequested = FALSE;
APPCTX.tcpClientSock = INVALID_SOCKET;
APPCTX.tcpServerSock = INVALID_SOCKET;
APPCTX.udpSock = INVALID_SOCKET;
}
static BOOL IperfAppKillRequested(void)
{
UINT8 argc;
INT8 **argv;
// Did user ask to "kill iperf"?
if ( WFConsoleIsConsoleMsgReceived() == FALSE ) return FALSE;
argv = WFConsoleGetCmdLineArgv();
argc = WFConsoleGetCmdLineArgc();
if (argc == 2u)
{
if ((memcmppgm2ram(argv[0], "kill", 4) == 0) &&
(memcmppgm2ram(argv[1], "iperf", 5) == 0))
{
//WFCliServicePerformed();
WFConsoleReleaseConsoleMsg();
APPCTX.stopRequested = TRUE;
return TRUE;
}
}
WFConsolePrintRomStr("Invalid commmand sequence", TRUE);
WFConsoleReleaseConsoleMsg();
return FALSE;
}
static void StateMachineStandby(void)
{
//tWFCliArguments *args;
UINT8 argc;
INT8 **argv;
UINT8 i;
INT8 *ptr;
UINT32 values[4];
UINT16 payloadSize;
float pktRate;
//if ( WFCliServiceRequested() == FALSE ) return;
//if ( !(args = WFCliGetArguments()) ) return;
if ( WFConsoleIsConsoleMsgReceived() == FALSE ) return;
argv = WFConsoleGetCmdLineArgv();
argc = WFConsoleGetCmdLineArgc(); // needed because WFConsoleGetCmdLineTokens() returns 0 for argc when in -Os
if ( (argc == 2u) &&
(memcmppgm2ram(argv[0], "kill", 4) == 0) &&
(memcmppgm2ram(argv[1], "iperf", 5) == 0) )
{
WFConsolePrintRomStr("Iperf not started for the kill...", TRUE);
WFConsoleReleaseConsoleMsg();
return;
}
if ((memcmppgm2ram(argv[0], "kill", 4) == 0) && (argc == 1u))
{
WFConsolePrintRomStr("'kill iperf' is the only valid kill command", TRUE);
WFConsoleReleaseConsoleMsg();
}
if ( (argc == 2u) &&
(memcmppgm2ram(argv[0], "kill", 4) == 0) &&
(memcmppgm2ram(argv[1], "iperf", 5) != 0) )
{
WFConsolePrintRomStr("Invalid command sequence...", TRUE);
WFConsoleReleaseConsoleMsg();
return;
}
if ( memcmppgm2ram(argv[0], "iperf", 5) ) return;
// OK, we will handle the "iperf" command.
APPCTX.mServerMode = FALSE;
APPCTX.mProtocol = kIperfProtoTCP; // default is TCP mode.
APPCTX.stopRequested = FALSE;
APPCTX.mServerPort = IPERF_APP_SERVER_PORT; // -p. default: server port 5001
APPCTX.mTxRate = ((UINT32) 500)*((UINT32) 1000); // -b or -x. Target tx rate.
// KS: default tx rate for iperf is actually 1Mbps. Here we set it to 500Kbps instead.
APPCTX.mAmount = 0; // -n: default 0.
APPCTX.mDuration = ((UINT32) 10)*((UINT32) TICK_SECOND); // -t: default 10 sec.
APPCTX.mInterval = TICK_SECOND; // -i: default 1 sec.
// Initialize statistics
ResetIperfCounters();
WF_ASSERT(argc != 0);
for (i = 1; i < argc; i++)
{
if ((memcmppgm2ram(argv[i], "-s", 2) == 0) ||
(memcmppgm2ram(argv[i], "--server", 5) == 0) )
{
// Function as an iperf server.
APPCTX.mServerMode = TRUE;
}
else if ((memcmppgm2ram(argv[i], "-u", 2) == 0) ||
(memcmppgm2ram(argv[i], "--udp", 5) == 0) )
{
// iperf UDP mode.
APPCTX.mProtocol = kIperfProtoUDP;
}
else if ((memcmppgm2ram(argv[i], "-b", 2) == 0) ||
(memcmppgm2ram(argv[i], "--bandwidth", 5) == 0) )
{
// iperf UDP mode.
APPCTX.mProtocol = kIperfProtoUDP;
// Next argument should be the target rate, in bps.
i++;
ptr = argv[i];
ascii_to_u32s(ptr, values, 1);
APPCTX.mTxRate = values[0];
}
else if ((memcmppgm2ram(argv[i], "-x", 2) == 0) ||
(memcmppgm2ram(argv[i], "--xmitrate", 5) == 0) )
{
// NON-STANDARD IPERF OPTION. Set the max TCP tx rate.
// Next argument should be the target rate, in bps.
i++;
ptr = argv[i];
ascii_to_u32s(ptr, values, 1);
APPCTX.mTxRate = values[0];
}
else if ((memcmppgm2ram(argv[i], "-c", 2) == 0) ||
(memcmppgm2ram(argv[i], "--client", 5) == 0) )
{
// Function as an iperf client.
APPCTX.mServerMode = FALSE;
// Next argument should be the server IP, such as "192.168.1.100".
i++;
ptr = argv[i];
ascii_to_u32s(ptr, values, 4);
APPCTX.remoteSide.remote.IPAddr.v[0] = values[0];
APPCTX.remoteSide.remote.IPAddr.v[1] = values[1];
APPCTX.remoteSide.remote.IPAddr.v[2] = values[2];
APPCTX.remoteSide.remote.IPAddr.v[3] = values[3];
}
else if ((memcmppgm2ram(argv[i], "-t", 2) == 0) ||
(memcmppgm2ram(argv[i], "--time", 5) == 0) )
{
// Next argument should be the (client tx) duration, in seconds.
i++;
ptr = argv[i];
ascii_to_u32s(ptr, values, 1);
APPCTX.mDuration = values[0]*TICK_SECOND;
APPCTX.mAmount = 0;
}
else if ((memcmppgm2ram(argv[i], "-n", 2) == 0) ||
(memcmppgm2ram(argv[i], "--num", 5) == 0) )
{
// Next argument should be the (client tx) size, in bytes.
i++;
ptr = argv[i];
ascii_to_u32s(ptr, values, 1);
APPCTX.mAmount = values[0];
APPCTX.mDuration = 0;
}
/* On the Microchip v4.51 stack the MSS is a compile time setting and not within the control */
/* of the application to change on a per socket basis, nor is it even a runtime configurable */
/* setting. Search tcp.c for TCP_MAX_SEG_SIZE */
// else if ((memcmppgm2ram(argv[i], "-M", 2) == 0) ||
// (memcmppgm2ram(argv[i], "--mss", 5) == 0) )
// {
// // Next argument should be the (client tcp tx) MSS size, in bytes.
//
// i++;
// ptr = argv[i];
//
// ascii_to_u32s(ptr, values, 1);
//
// APPCTX.mMSS = values[0];
// }
else if ((memcmppgm2ram(argv[i], "-i", 2) == 0) ||
(memcmppgm2ram(argv[i], "--interval", 5) == 0) )
{
// Next argument should be the report interval, in seconds.
i++;
ptr = argv[i];
ascii_to_u32s(ptr, values, 1);
APPCTX.mInterval = values[0]*TICK_SECOND; // Convert to msec
}
else if ((memcmppgm2ram(argv[i], "-l", 2) == 0) ||
(memcmppgm2ram(argv[i], "--len", 5) == 0) )
{
// Next argument should be the buffer length, in bytes.
// This is used as the UDP datagram size.
i++;
ptr = argv[i];
ascii_to_u32s(ptr, values, 1);
if ( values[0] < MAX_BUFFER )
{
sprintf( (char *) g_ConsoleContext.txBuf,"The minimum datagram size is %d", (int)MAX_BUFFER);
WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf, TRUE);
WFConsoleReleaseConsoleMsg();
return;
}
APPCTX.mDatagramSize = values[0];
}
}
//WFCliServicePerformed();
WFConsoleReleaseConsoleMsg();
switch (APPCTX.mServerMode)
{
case 0:
// iperf client
payloadSize = (APPCTX.mProtocol == kIperfProtoUDP) ?
APPCTX.mDatagramSize : APPCTX.mMSS;
pktRate = (float) (APPCTX.mTxRate / 8) / (float) payloadSize;
APPCTX.mPktPeriod = (UINT32) ( (float) TICK_SECOND / pktRate );
APPCTX.state = kStateTxStart;
break;
case 1:
// iperf server
WFConsolePrintRomStr("---------------------------------------------------------", TRUE);
WFConsolePrintRomStr("Server listening on ", FALSE);
if (APPCTX.mProtocol == kIperfProtoUDP)
{
WFConsolePrintRomStr((ROM char *)"UDP", FALSE);
}
else
{
WFConsolePrintRomStr((ROM char *)"TCP", FALSE);
}
WFConsolePrintRomStr(" port ", FALSE);
WFConsolePrintInteger(APPCTX.mServerPort, 'd');
WFConsolePrintRomStr("", TRUE);
APPCTX.state = kStateRxStart;
break;
}
}
static void
StateMachineRxStart(void)
{
if ( !APPCTX.mServerMode )
{
putrsUART("Unsupported Configuration\n\r");
APPCTX.state = kStateStandby;
return;
}
switch ( APPCTX.mProtocol )
{
case kIperfProtoTCP: // TCP
/* TCP Server sockets are allocated for entire runtime duration, a call to disconnect does not free them */
/* therefore a subsequent N+1 open will fail */
if ( (APPCTX.tcpServerSock == INVALID_SOCKET) &&
(APPCTX.tcpServerSock = TCPOpen(0, TCP_OPEN_SERVER, APPCTX.mServerPort, TCP_PURPOSE_GENERIC_TCP_SERVER)) == INVALID_SOCKET )
{
/* error case */
WFConsolePrintRomStr("Create TCP socket failed", TRUE);
APPCTX.state = kStateStandby;
return;
}
TCPAdjustFIFOSize(APPCTX.tcpServerSock, 1, 0,TCP_ADJUST_PRESERVE_RX | TCP_ADJUST_GIVE_REST_TO_RX);
APPCTX.state = kStateTcpRxListen;
break;
case kIperfProtoUDP: // UDP
if ( (APPCTX.udpSock = UDPOpen(APPCTX.mServerPort, NULL, APPCTX.mServerPort)) == INVALID_UDP_SOCKET )
{
/* error case */
WFConsolePrintRomStr("Create UDP socket failed", TRUE);
APPCTX.state = kStateStandby;
return;
}
APPCTX.state = kStateUdpRx;
break;
default:
WFConsolePrintRomStr("Protocol error", TRUE);
APPCTX.state = kStateStandby;
return;
}
}
static void
StateMachineTcpListen(void)
{
if ( IperfAppKillRequested() == TRUE )
{
APPCTX.state = kStateRxDone;
return;
}
if( TCPIsConnected(APPCTX.tcpServerSock) )
{
SOCKET_INFO* ptrSocketInfo = TCPGetRemoteInfo( APPCTX.tcpServerSock );
memcpy ( (void *) &APPCTX.remoteSide, (void *) ptrSocketInfo, sizeof ( SOCKET_INFO) );
APPCTX.state = kStateTcpRx;
/* clear the stack's reset flag */
TCPWasReset(APPCTX.tcpServerSock);
}
}
static void
StateMachineTcpRx(void)
{
UINT16 length;
if( (length = TCPIsGetReady(APPCTX.tcpServerSock)) == 0 )
{
if ( TCPWasReset(APPCTX.tcpServerSock) )
{
APPCTX.stopTime = IPERF_GET_MSEC_TICK_COUNT();
APPCTX.state = kStateTcpRxDone;
return;
}
}
else
{
if ( APPCTX.pktId == 0)
{
// This is the first rx pkt.
putrsUART("\n\r Session started ...\n\r");
APPCTX.startTime = IPERF_GET_MSEC_TICK_COUNT();
APPCTX.lastCheckTime = APPCTX.startTime;
APPCTX.lastCheckPktId = APPCTX.pktId;
sprintf((char *) g_ConsoleContext.txBuf, " - Local %u.%u.%u.%u port %u connected with",
AppConfig.MyIPAddr.v[0],
AppConfig.MyIPAddr.v[1],
AppConfig.MyIPAddr.v[2],
AppConfig.MyIPAddr.v[3],
APPCTX.mServerPort);
WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE);
sprintf( (char *) g_ConsoleContext.txBuf," - Remote %u.%u.%u.%u port %u",
APPCTX.remoteSide.remote.IPAddr.v[0],
APPCTX.remoteSide.remote.IPAddr.v[1],
APPCTX.remoteSide.remote.IPAddr.v[2],
APPCTX.remoteSide.remote.IPAddr.v[3],
APPCTX.remoteSide.remotePort.Val );
WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE);
}
APPCTX.pktId++;
APPCTX.pktCount++;
APPCTX.lastPktId = APPCTX.pktId;
APPCTX.totalLen += length;
/* read the remaining datagram payload */
/* a UdpDiscard would be disingenuous, because it would not reflect the bandwidth at L7 */
while ( length > 0 )
{
UINT16 chunk;
if ( length < (UINT16)MAX_BUFFER )
chunk = length;
else
chunk = MAX_BUFFER;
TCPGetArray( APPCTX.tcpServerSock, (BYTE*)g_bfr, chunk);
length -= chunk;
}
}
if ((APPCTX.pktId != (long)0) &&
(IPERF_GET_MSEC_TICK_COUNT() > (APPCTX.lastCheckTime + APPCTX.mInterval)) )
{
// Time to report statistics
ReportBW_Jitter_Loss(kIntervalReport);
}
if ( IperfAppKillRequested() == TRUE )
{
APPCTX.state = kStateTcpRxDone;
APPCTX.stopTime = IPERF_GET_MSEC_TICK_COUNT();
return;
}
}
static void
StateMachineUdpRx(void)
{
UINT16 length =0;
tIperfPktInfo *pPktInfo;
// Do nothing if no data is waiting
if( (length = UDPIsGetReady(APPCTX.udpSock)) >= (UINT16)(sizeof(tIperfPktInfo)) )
{
/* The GetArray should not fail... */
if ( UDPGetArray((BYTE*)g_bfr, sizeof(tIperfPktInfo)) != sizeof(tIperfPktInfo) )
{
putrsUART(" UDP Get Array Failed\n\r");
APPCTX.state = kStateUdpRxDrain;
return;
}
pPktInfo = (tIperfPktInfo *) g_bfr;
APPCTX.pktId = WFTOHL(pPktInfo->id);
if ( (APPCTX.pktCount == (UINT32)0) && (APPCTX.pktId < (long)0) )
{
// Ignore retransmits from previous session.
UDPDiscard();
return;
}
APPCTX.pktCount++;
if (APPCTX.pktCount == (UINT32)1 )
{
// The first pkt is used to set up the server,
// does not count as a data pkt.
WFConsolePrintRomStr("", TRUE);
WFConsolePrintRomStr(" Session started ...", TRUE);
if ( APPCTX.pktId != 0 )
{
// We have lost a few packets before the first pkt arrived.
sprintf((char *) g_ConsoleContext.txBuf, " - First pkt id = %ld (should be 0)",
APPCTX.pktId);
WFConsolePrintRamStr((char *) g_ConsoleContext.txBuf, TRUE );
// The first data pkt starts with id = 1.
APPCTX.errorCount += APPCTX.pktId - 1;
}
APPCTX.lastPktId = APPCTX.pktId;
sprintf((char *) g_ConsoleContext.txBuf, " - Local %u.%u.%u.%u port %u connected with ",
AppConfig.MyIPAddr.v[0],
AppConfig.MyIPAddr.v[1],
AppConfig.MyIPAddr.v[2],
AppConfig.MyIPAddr.v[3],
APPCTX.mServerPort);
WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE);
sprintf((char *) g_ConsoleContext.txBuf," - Remote %u.%u.%u.%u port %u",
UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[0],
UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[1],
UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[2],
UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[3],
UDPSocketInfo[APPCTX.udpSock].remotePort );
WFConsolePrintRamStr( (char *) g_ConsoleContext.txBuf , TRUE);
// Store the remote info so we can send the iperf "UDP-FIN-ACK" msg
APPCTX.remoteSide.remote.IPAddr.v[0] = UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[0];
APPCTX.remoteSide.remote.IPAddr.v[1] = UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[1];
APPCTX.remoteSide.remote.IPAddr.v[2] = UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[2];
APPCTX.remoteSide.remote.IPAddr.v[3] = UDPSocketInfo[APPCTX.udpSock].remoteNode.IPAddr.v[3];
APPCTX.remoteSide.remotePort.Val = UDPSocketInfo[APPCTX.udpSock].remotePort;
APPCTX.startTime = IPERF_GET_MSEC_TICK_COUNT();
//APPCTX.remoteStartTime = WFTOHL(pPktInfo->tv_sec);
APPCTX.lastCheckTime = APPCTX.startTime;
APPCTX.lastCheckPktId = APPCTX.pktId;
APPCTX.lastCheckPktCount = APPCTX.pktCount;
APPCTX.lastCheckErrorCount = APPCTX.errorCount;
UDPDiscard();
return;
}
APPCTX.totalLen += length;
if ( APPCTX.pktId < 0 )
{
// this is the last datagram
APPCTX.pktId = - APPCTX.pktId;
APPCTX.stopTime = IPERF_GET_MSEC_TICK_COUNT();
//APPCTX.remoteStopTime = WFTOHL(pPktInfo->tv_sec);