-
Notifications
You must be signed in to change notification settings - Fork 19
/
functions.c
2168 lines (1584 loc) · 69.1 KB
/
functions.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
/*
* License: MIT
*
* Copyright (c) 2012-2018 James Bensley.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* File: Etherate Gerneral Functions
*
* File Contents:
* int16_t broadcast_etherate()
* void build_headers()
* void build_tlv()
* void build_sub_tlv()
* int16_t cli_args()
* uint8_t explode_char()
* int32_t get_interface_mtu_by_name()
* int16_t get_sock_interface()
* void list_interfaces()
* void print_usage()
* int16_t remove_interface_promisc()
* void reset_app()
* int16_t set_int_promisc()
* int16_t set_sock_interface_index()
* int16_t set_sock_interface_name()
* void signal_handler()
* void sync_settings()
* void update_frame_size()
*
*/
#include "functions.h"
int16_t broadcast_etherate(struct etherate *eth)
{
uint8_t tmp_mac[6] = {
eth->frm.dst_mac[0],
eth->frm.dst_mac[1],
eth->frm.dst_mac[2],
eth->frm.dst_mac[3],
eth->frm.dst_mac[4],
eth->frm.dst_mac[5],
};
eth->frm.dst_mac[0] = 0xFF;
eth->frm.dst_mac[1] = 0xFF;
eth->frm.dst_mac[2] = 0xFF;
eth->frm.dst_mac[3] = 0xFF;
eth->frm.dst_mac[4] = 0xFF;
eth->frm.dst_mac[5] = 0xFF;
// Rebuild frame headers with destination MAC
build_headers(ð->frm);
build_tlv(ð->frm, htons(TYPE_BROADCAST), htonl(VALUE_PRESENCE));
// Build a dummy sub-TLV to align the buffers and pointers
build_sub_tlv(ð->frm, htons(TYPE_APPLICATION_SUB_TLV),
htonll(VALUE_DUMMY));
int16_t tx_ret = 0;
printf("Sending gratuitous broadcasts...\n");
for (uint8_t i = 1; i <= 3; i += 1)
{
tx_ret = sendto(eth->intf.sock_fd,
eth->frm.tx_buffer,
eth->frm.length + eth->frm.tlv_size,
0,
(struct sockaddr*)ð->intf.sock_addr,
sizeof(eth->intf.sock_addr));
if (tx_ret <= 0)
{
perror("Broadcast failed ");
return tx_ret;
}
sleep(1);
}
printf("Done.\n");
// Restore the original destination MAC
eth->frm.dst_mac[0] = tmp_mac[0];
eth->frm.dst_mac[1] = tmp_mac[1];
eth->frm.dst_mac[2] = tmp_mac[2];
eth->frm.dst_mac[3] = tmp_mac[3];
eth->frm.dst_mac[4] = tmp_mac[4];
eth->frm.dst_mac[5] = tmp_mac[5];
return EXIT_SUCCESS;
}
void build_headers(struct frm *frm)
{
uint16_t mpls_offset = 0;
uint16_t eth_offset = 0;
uint32_t mpls_hdr = 0;
uint16_t tpi = 0;
uint16_t tci = 0;
uint16_t *ptpi = &tpi;
uint16_t *ptci = &tci;
uint16_t vlan_id_tmp;
// Check if transporting over MPLS
if (frm->mpls_labels > 0) {
// Copy the destination and source LSR MAC addresses
memcpy((void*)frm->tx_buffer,
(void*)frm->lsp_dst_mac, ETH_ALEN);
mpls_offset+=ETH_ALEN;
memcpy((void*)(frm->tx_buffer + mpls_offset),
(void*)frm->lsp_src_mac, ETH_ALEN);
mpls_offset+=ETH_ALEN;
// Push on the ethertype for unicast MPLS
tpi = htons(0x8847);
memcpy((void*)(frm->tx_buffer + mpls_offset), ptpi, sizeof(tpi));
mpls_offset+=sizeof(tpi);
// For each MPLS label copy it onto the stack
uint8_t i = 0;
while (i < frm->mpls_labels) {
mpls_hdr = (frm->mpls_label[i] & 0xFFFFF) << 12;
mpls_hdr = mpls_hdr | (frm->mpls_exp[i] & 0x07) << 9;
// MPLS BoS bit
if (i == frm->mpls_labels - 1) {
mpls_hdr = mpls_hdr | 1 << 8;
} else {
mpls_hdr = mpls_hdr | 0 << 8;
}
mpls_hdr = mpls_hdr | (frm->mpls_ttl[i] & 0xFF);
mpls_hdr = htonl(mpls_hdr);
memcpy((void*)(frm->tx_buffer + mpls_offset),
&mpls_hdr, sizeof(mpls_hdr));
mpls_offset+=sizeof(mpls_hdr);
mpls_hdr = 0;
i += 1;
}
// Check if we need to push on a pseudowire control word
if (frm->pwe_ctrl_word == 1){
memset((void*)(frm->tx_buffer + mpls_offset), 0, 4);
mpls_offset += 4;
}
}
eth_offset += mpls_offset;
// Copy the destination and source MAC addresses
memcpy((void*)(frm->tx_buffer + eth_offset),
(void*)frm->dst_mac, ETH_ALEN);
eth_offset += ETH_ALEN;
memcpy((void*)(frm->tx_buffer + eth_offset),
(void*)frm->src_mac, ETH_ALEN);
eth_offset += ETH_ALEN;
// Check if QinQ VLAN ID has been supplied
if (frm->qinq_pcp != QINQ_PCP_DEF ||
frm->qinq_id != QINQ_ID_DEF)
{
// Add on the QinQ Tag Protocol Identifier
// 0x88a8 == IEEE802.1ad, 0x9100 == older IEEE802.1QinQ
tpi = htons(0x88a8);
memcpy((void*)(frm->tx_buffer + eth_offset),
ptpi, sizeof(tpi));
eth_offset += sizeof(tpi);
// Build the QinQ Tag Control Identifier...
// pcp value
vlan_id_tmp = frm->qinq_id;
tci = (frm->qinq_pcp & 0x07) << 5;
// DEI value
if (frm->qinq_dei == 1)
{
tci = tci | (1 << 4);
}
// VLAN ID, first 4 bits
frm->qinq_id = frm->qinq_id >> 8;
tci = tci | (frm->qinq_id & 0x0f);
// VLAN ID, last 8 bits
frm->qinq_id = vlan_id_tmp;
frm->qinq_id = frm->qinq_id << 8;
tci = tci | (frm->qinq_id & 0xffff);
frm->qinq_id = vlan_id_tmp;
memcpy((void*)(frm->tx_buffer + eth_offset),
ptci, sizeof(tci));
eth_offset += sizeof(tci);
// If an outer VLAN ID has been set, but not an inner one
// (assume to be a mistake) set it to 1 so the frame is still valid
if (frm->vlan_id == 0) frm->vlan_id = 1;
}
// Check to see if an inner VLAN pcp value or VLAN ID has been supplied
if (frm->pcp != PCP_DEF || frm->vlan_id != VLAN_ID_DEF)
{
tpi = htons(0x8100);
memcpy((void*)(frm->tx_buffer + eth_offset),
ptpi, sizeof(tpi));
eth_offset += sizeof(tpi);
// Build the inner VLAN tci...
// pcp value
vlan_id_tmp = frm->vlan_id;
tci = (frm->pcp & 0x07) << 5;
// DEI value
if (frm->vlan_dei==1)
{
tci = tci | (1 << 4);
}
// VLAN ID, first 4 bits
frm->vlan_id = frm->vlan_id >> 8;
tci = tci | (frm->vlan_id & 0x0f);
// VLAN ID, last 8 bits
frm->vlan_id = vlan_id_tmp;
frm->vlan_id = frm->vlan_id << 8;
tci = tci | (frm->vlan_id & 0xffff);
frm->vlan_id = vlan_id_tmp;
memcpy((void*)(frm->tx_buffer + eth_offset),
ptci, sizeof(tci));
eth_offset += sizeof(tci);
}
// Push on the ethertype for the Etherate payload
tpi = htons(frm->etype);
memcpy((void*)(frm->tx_buffer + eth_offset),
ptpi, sizeof(tpi));
eth_offset += sizeof(tpi);
frm->length = eth_offset;
// Pointers to the payload section of the frame
frm->tx_data = frm->tx_buffer + frm->length;
frm->rx_data = frm->rx_buffer + frm->length;
/* When receiving a VLAN tagged frame (one or more VLAN tags) the Linux
* Kernel is stripping off the outer most VLAN, so for the RX buffer the
* data starts 4 bytes earlier, see this post:
* http://stackoverflow.com/questions/24355597/linux-when-sending-ethernet-
* frames-the-ethertype-is-being-re-written
*/
if (frm->vlan_id != VLAN_ID_DEF ||
frm->qinq_id != QINQ_ID_DEF) {
frm->rx_data -= 4;
}
}
#if defined(NOINLINE)
void build_tlv(struct frm *frm, uint16_t TLV_TYPE,
uint32_t TLV_VALUE)
{
uint8_t *buffer_offset = frm->tx_data;
(void) memcpy(buffer_offset, &TLV_TYPE, sizeof(TLV_TYPE));
buffer_offset += sizeof(TLV_TYPE);
*buffer_offset++ = sizeof(TLV_VALUE);
(void) memcpy(buffer_offset, &TLV_VALUE, sizeof(TLV_VALUE));
frm->rx_tlv_type = (uint16_t*) frm->rx_data;
frm->rx_tlv_value = (uint32_t*) (frm->rx_data + sizeof(TLV_TYPE) +
sizeof(uint8_t));
}
void build_sub_tlv(struct frm *frm, uint16_t SUB_TLV_TYPE,
uint64_t SUB_TLV_VALUE)
{
uint8_t *buffer_offset = frm->tx_data + frm->tlv_size;
(void) memcpy(buffer_offset, &SUB_TLV_TYPE, sizeof(SUB_TLV_TYPE));
buffer_offset += sizeof(SUB_TLV_TYPE);
*buffer_offset++ = sizeof(SUB_TLV_VALUE);
(void) memcpy(buffer_offset, &SUB_TLV_VALUE, sizeof(SUB_TLV_VALUE));
frm->sub_tlv_val = (uint64_t*)buffer_offset;
frm->rx_sub_tlv_type = (uint16_t*) (frm->rx_data + frm->tlv_size);
frm->rx_sub_tlv_value = (uint64_t*) (frm->rx_data + frm->tlv_size +
sizeof(SUB_TLV_TYPE) + sizeof(uint8_t));
}
#endif
int16_t cli_args(int argc, char *argv[], struct etherate *eth)
{
if (argc > 1)
{
for (uint16_t i = 1; i < argc; i += 1)
{
// Change to receive mode
if (strncmp(argv[i], "-r" ,2)==0)
{
eth->app.tx_mode = false;
eth->frm.src_mac[0] = 0x00;
eth->frm.src_mac[1] = 0x00;
eth->frm.src_mac[2] = 0x5E;
eth->frm.src_mac[3] = 0x00;
eth->frm.src_mac[4] = 0x00;
eth->frm.src_mac[5] = 0x02;
eth->frm.dst_mac[0] = 0x00;
eth->frm.dst_mac[1] = 0x00;
eth->frm.dst_mac[2] = 0x5E;
eth->frm.dst_mac[3] = 0x00;
eth->frm.dst_mac[4] = 0x00;
eth->frm.dst_mac[5] = 0x01;
// Specifying a custom destination MAC address
} else if (strncmp(argv[i], "-d", 2)==0) {
uint8_t count;
char *tokens[6];
char delim[] = ":";
count = explode_char(argv[i+1], delim, tokens);
if (count == 6) {
for (uint8_t j = 0; j < 6; j += 1)
{
eth->frm.dst_mac[j] = (uint8_t)strtoul(tokens[j], NULL, 16);
}
} else {
printf("Error: Invalid destination MAC address!\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
i += 1;
// Disable settings sync between TX and RX
} else if (strncmp(argv[i], "-g", 2)==0) {
eth->app.tx_sync = false;
// Disable the TX to RX delay check
} else if (strncmp(argv[i], "-G", 2)==0) {
eth->app.tx_delay = 0;
// Specifying a custom source MAC address
} else if (strncmp(argv[i], "-s" ,2)==0) {
uint8_t count;
char *tokens[6];
char delim[] = ":";
count = explode_char(argv[i+1], delim, tokens);
if (count == 6) {
for (uint8_t j = 0; j < 6; j += 1)
{
eth->frm.src_mac[j] = (uint8_t)strtoul(tokens[j], NULL, 16);
}
} else {
printf("Error: Invalid source MAC address!\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
i += 1;
// Specifying a custom interface name
} else if (strncmp(argv[i], "-i", 2)==0) {
if (argc > (i+1))
{
strncpy((char*)eth->intf.if_name, argv[i+1],
sizeof(eth->intf.if_name));
i += 1;
} else {
printf("Oops! Missing interface name\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Specifying a custom interface index
} else if (strncmp(argv[i], "-I", 2)==0) {
if (argc > (i+1))
{
eth->intf.if_index = (int)strtoul(argv[i+1], NULL, 0);
i += 1;
} else {
printf("Oops! Missing interface index\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Requesting to list interfaces
} else if (strncmp(argv[i], "-l", 2)==0) {
list_interfaces();
return RET_EXIT_APP;
// Specifying a custom frame payload size in bytes
} else if (strncmp(argv[i], "-f", 2)==0) {
if (argc > (i+1))
{
eth->params.f_size = (uint32_t)strtoul(argv[i+1], NULL, 0);
if (eth->params.f_size > 1500)
{
printf("WARNING: Make sure your device supports baby "
"giants or jumbo frames as required\n");
}
if (eth->params.f_size < 46) {
printf("WARNING: Minimum ethernet payload is 46 bytes, "
"Linux may pad the frame out to 46 bytes\n");
if (eth->params.f_size < (eth->frm.sub_tlv_size))
eth->params.f_size = eth->frm.sub_tlv_size;
}
i += 1;
} else {
printf("Oops! Missing frame size\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Limit TX rate using a frame pacing interval
} else if (strncmp(argv[i], "-T", 2)==0) {
if (argc > (i+1))
{
eth->params.f_tx_dly = strtoul(argv[i+1], NULL, 0);
i += 1;
} else {
printf("Oops! Missing max TX rate\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Specifying a custom transmission duration in seconds
} else if (strncmp(argv[i], "-t", 2)==0) {
if (argc > (i+1))
{
eth->params.f_duration = strtoull(argv[i+1], NULL, 0);
i += 1;
} else {
printf("Oops! Missing transmission duration\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Specifying the total number of frames to send instead of duration
} else if (strncmp(argv[i], "-c", 2)==0) {
if (argc > (i+1))
{
eth->params.f_count = strtoull(argv[i+1], NULL, 0);
i += 1;
} else {
printf("Oops! Missing max frame count\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Load a custom frame from file for TX
} else if (strncmp(argv[i], "-C", 2)==0) {
if (argc > (i+1))
{
FILE* f_payload = fopen(argv[i+1], "r");
if (f_payload==NULL){
printf("Opps! File loading error!\n");
return EX_SOFTWARE;
}
int file_ret = 0;
eth->speed_test.f_payload_size = 0;
while (file_ret != EOF &&
(eth->speed_test.f_payload_size < F_SIZE_MAX)) {
file_ret = fscanf(f_payload, "%hhx",
eth->speed_test.f_payload + eth->speed_test.f_payload_size);
if (file_ret == EOF)
break;
eth->speed_test.f_payload_size += 1;
}
if (fclose(f_payload) != 0)
{
perror("Error closing file ");
}
printf("Using custom frame (%" PRIu16 " octets loaded)\n",
eth->speed_test.f_payload_size);
// Disable initial broadcast
eth->app.broadcast = false;
// Disable settings sync
eth->app.tx_sync = false;
// Disable delay calculation
eth->app.tx_delay = false;
i += 1;
} else {
printf("Oops! Missing filename\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Specifying the total number of bytes to send instead of duration
} else if (strncmp(argv[i], "-b", 2)==0) {
if (argc > (i+1))
{
eth->params.f_bytes = strtoull(argv[i+1], NULL, 0);
i += 1;
} else {
printf("Oops! Missing max byte transfer limit\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Enable ACK mode when testing
} else if (strncmp(argv[i], "-a", 2)==0) {
if (argc > (i+2))
{
eth->params.f_ack = true;
eth->params.f_ack_timeout = strtoul(argv[i+1], NULL, 0);
eth->params.f_ack_count = strtoul(argv[i+2], NULL, 0);
i += 2;
} else {
printf("Oops! Missing timeout and ACK frame count\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Specify the max frames per second rate
} else if (strncmp(argv[i], "-F", 2)==0) {
if (argc > (i+1))
{
eth->params.f_tx_count_max = strtoul(argv[i+1], NULL, 0);
i += 1;
} else {
printf("Oops! Missing max frame rate\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Limit TX rate to max bytes per second
} else if (strncmp(argv[i], "-m", 2)==0) {
if (argc > (i+1))
{
eth->speed_test.b_tx_speed_max = strtoull(argv[i+1], NULL, 0);
i += 1;
} else {
printf("Oops! Missing max TX rate\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Limit TX rate to max bits per second
} else if (strncmp(argv[i], "-M", 2)==0) {
if (argc > (i+1))
{
eth->speed_test.b_tx_speed_max = (uint64_t)floor(strtoull(argv[i+1], NULL, 0) / 8);
i += 1;
} else {
printf("Oops! Missing max TX rate\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Set 802.1q VLAN ID
} else if (strncmp(argv[i], "-v", 2)==0) {
if (argc > (i+1))
{
eth->frm.vlan_id = (uint16_t)atoi(argv[i+1]);
i += 1;
} else {
printf("Oops! Missing 802.1p VLAN ID\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Set 802.1p pcp value
} else if (strncmp(argv[i], "-p", 2)==0) {
if (argc > (i+1))
{
eth->frm.pcp = (uint16_t)atoi(argv[i+1]);
i += 1;
} else {
printf("Oops! Missing 802.1p pcp value\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Set the DEI bit on the inner VLAN
} else if (strncmp(argv[i], "-x", 2)==0) {
eth->frm.vlan_dei = 1;
// Set 802.1ad QinQ outer VLAN ID
} else if (strncmp(argv[i], "-q", 2)==0) {
if (argc > (i+1))
{
eth->frm.qinq_id = (uint16_t)atoi(argv[i+1]);
i += 1;
} else {
printf("Oops! Missing 802.1ad QinQ outer VLAN ID\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Set 802.1ad QinQ outer pcp value
} else if (strncmp(argv[i], "-o", 2)==0) {
if (argc > (i+1))
{
eth->frm.qinq_pcp = (uint16_t)atoi(argv[i+1]);
i += 1;
} else {
printf("Oops! Missing 802.1ad QinQ outer pcp value\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Set the DEI bit on the outer VLAN
} else if (strncmp(argv[i], "-X", 2)==0) {
eth->frm.qinq_dei = 1;
// Set a custom ethertype
} else if (strncmp(argv[i], "-e", 2)==0) {
if (argc > (i+1))
{
eth->frm.etype = (uint16_t)strtol(argv[i+1], NULL, 16);
i += 1;
} else {
printf("Oops! Missing ethertype value\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Destination and source MAC for next-hop MPLS LSR
} else if (strncmp(argv[i], "-D", 2)==0) {
uint8_t count;
char *tokens[6];
char delim[] = ":";
count = explode_char(argv[i+1], delim, tokens);
if (count == 6) {
for (uint8_t j = 0; j < 6; j += 1)
{
eth->frm.lsp_dst_mac[j] = (uint8_t)strtoul(tokens[j], NULL, 16);
}
} else {
printf("Error: Invalid destination LSR MAC address!\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
i += 1;
count = explode_char(argv[i+1], delim, tokens);
if (count == 6) {
for (uint8_t j = 0; j < 6; j += 1)
{
eth->frm.lsp_src_mac[j] = (uint8_t)strtoul(tokens[j], NULL, 16);
}
} else {
printf("Error: Invalid source LSR MAC address!\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
i += 1;
// Push an MPLS label onto the stack
} else if (strncmp(argv[i], "-L", 2)==0) {
if (argc > (i+3))
{
if (eth->frm.mpls_labels < MPLS_LABELS_MAX) {
if ((uint32_t)atoi(argv[i+1]) > 1048575) {
printf("Oops! MPLS label higher than 1,048,575\n");
return RET_EXIT_FAILURE;
}
if ((uint16_t)atoi(argv[i+2]) > 7) {
printf("Oops! MPLS EXP higher than 7\n");
return RET_EXIT_FAILURE;
}
if ((uint16_t)atoi(argv[i+3]) > 255) {
printf("Oops! MPLS TTL higher than 255\n");
return RET_EXIT_FAILURE;
}
eth->frm.mpls_label[eth->frm.mpls_labels] =
(uint32_t)atoi(argv[i+1]);
eth->frm.mpls_exp[eth->frm.mpls_labels] =
(uint16_t)atoi(argv[i+2]);
eth->frm.mpls_ttl[eth->frm.mpls_labels] =
(uint16_t)atoi(argv[i+3]);
eth->frm.mpls_labels += 1;
i+=3;
} else {
printf("Oops! You have exceeded the maximum number of "
"MPLS labels (%" PRIu32 ")\n", MPLS_LABELS_MAX);
return RET_EXIT_FAILURE;
}
} else {
printf("Oops! Missing MPLS label values\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Push pseudowire control word atop the label stack
} else if (strncmp(argv[i], "-P", 2)==0) {
eth->frm.pwe_ctrl_word = 1;
// Enable the MTU sweep test
} else if (strncmp(argv[i], "-U", 2)==0) {
if (argc > (i+2))
{
eth->mtu_test.mtu_tx_min = (uint16_t)atoi(argv[i+1]);
eth->mtu_test.mtu_tx_max = (uint16_t)atoi(argv[i+2]);
if (eth->mtu_test.mtu_tx_max > F_SIZE_MAX) {
printf("MTU size can not exceed the maximum hard coded"
" size: %" PRIu32 "\n", F_SIZE_MAX);
return RET_EXIT_FAILURE;
}
eth->mtu_test.enabled = true;
i+=2;
} else {
printf("Oops! Missing min/max MTU sizes\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Enable the link quality measurement tests
} else if (strncmp(argv[i], "-Q", 2)==0) {
if (argc > (i+2))
{
eth->qm_test.interval = (uint32_t)atoi(argv[i+1]);
eth->qm_test.timeout = (uint32_t)atoi(argv[i+2]);
if (eth->qm_test.timeout > eth->qm_test.interval) {
printf("Oops! Echo timeout exceeded the interval\n");
return RET_EXIT_FAILURE;
}
// Convert to ns for use with timespec
eth->qm_test.interval_nsec = (eth->qm_test.interval * 1000000) % 1000000000;
eth->qm_test.interval_sec = ((eth->qm_test.interval * 1000000) - eth->qm_test.interval_nsec) / 1000000000;
eth->qm_test.timeout_nsec = (eth->qm_test.timeout * 1000000) % 1000000000;
eth->qm_test.timeout_sec = ((eth->qm_test.timeout * 1000000) - eth->qm_test.timeout_nsec) / 1000000000;
eth->qm_test.enabled = true;
i += 2;
} else {
printf("Oops! Missing interval and timeout values\n"
"Usage info: %s -h|--h\n", argv[0]);
return RET_EXIT_FAILURE;
}
// Display version
} else if (strncmp(argv[i], "-V", 2)==0 ||
strncmp(argv[i], "--version", 9)==0) {
printf("Etherate version %s\n", APP_VERSION);
return RET_EXIT_APP;
// Display usage instructions
} else if (strncmp(argv[i], "-h", 2)==0 ||
strncmp(argv[i], "--help", 6)==0) {
print_usage(eth);
return RET_EXIT_APP;
// Else the user entered an invalid argument
} else {
printf("Oops! Invalid argument %s\n"
"Usage info: %s -h|--h\n", argv[i], argv[0]);
return RET_EXIT_FAILURE;
}
}
}
if (eth->app.tx_mode == true)
{
printf("Running in TX mode\n");
} else {
printf("Running in RX mode\n");
}
return EXIT_SUCCESS;
}
uint8_t explode_char(char *string, char *delim, char *tokens[])
{
// Stole this function from somewhere, can't remember where,
// either way, props to someone else ¯\_(ツ)_/¯
uint8_t count = 0;
char *token;
char *stringp = string;
while ((token = strtok_r(stringp, delim, &stringp)))
{
tokens[count] = token;
count += 1;
}
return count;
}
int32_t get_interface_mtu_by_name(struct intf *intf)
{
struct ifreq ifr;
strncpy(ifr.ifr_name, (char *)intf->if_name, sizeof(ifr.ifr_name));
if (ioctl(intf->sock_fd, SIOCGIFMTU, &ifr)==0)
return ifr.ifr_mtu;
return RET_EXIT_FAILURE;
}
int16_t get_sock_interface(struct intf *intf)
{
struct ifreq *ifr, *ifend;
struct ifreq ifreq;
struct ifconf ifc;
struct ifreq ifs[MAX_IFS];
ifc.ifc_len = sizeof(ifs);
ifc.ifc_req = ifs;
if (ioctl(intf->sock_fd, SIOCGIFCONF, &ifc) == -1)
{
perror("No compatible interfaces found ");
return RET_EXIT_FAILURE;
}
// Loop over all interfaces
ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
for (ifr = ifc.ifc_req; ifr < ifend; ifr += 1)
{
// Is this a packet capable device? (Doesn't work with AF_PACKET?)
if (ifr->ifr_addr.sa_family == AF_INET)
{
// Try to get a typical ethernet adapter by name, not a bridge or virt interface
if (strncmp(ifr->ifr_name, "eth", 3)==0 ||
strncmp(ifr->ifr_name, "en", 2)==0 ||
strncmp(ifr->ifr_name, "em", 3)==0 ||
strncmp(ifr->ifr_name, "wlan", 4)==0 ||
strncmp(ifr->ifr_name, "wlp", 3)==0 )
{
strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
// Does this device even have hardware address?
if (ioctl (intf->sock_fd, SIOCGIFHWADDR, &ifreq) == -1) break;
// Copy MAC address before SIOCGIFINDEX
char mac[6];
memcpy(mac, ifreq.ifr_addr.sa_data, 6);
if (ioctl(intf->sock_fd, SIOCGIFINDEX, &ifreq) == -1) break;
int32_t ifindex = ifreq.ifr_ifindex;
strncpy((char*)intf->if_name, ifreq.ifr_name, IFNAMSIZ);
// Is this interface even up?
if (ioctl(intf->sock_fd, SIOCGIFFLAGS, &ifreq) == -1) break;
if (!(ifreq.ifr_flags & IFF_UP && ifreq.ifr_flags & IFF_RUNNING)) break;