-
Notifications
You must be signed in to change notification settings - Fork 19
/
etherate.c
230 lines (174 loc) · 6.91 KB
/
etherate.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
/*
* 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 Main Loop
*
* File Contents:
* DEFAULT VALUES
* CLI ARGS
* INTERFACE SETUP
* SETTINGS SYNC
* MAIN TEST PHASE
*
*/
#include <byteswap.h> // __bswap_64
#include <endian.h> // __BYTE_ORDER
#include <errno.h> // errno
#include <ifaddrs.h> // freeifaddrs(), getifaddrs()
#define __STDC_FORMAT_MACROS
#include <inttypes.h> // intN_t, uintN_t, PRIuN, SCNuN
#include <linux/if_arp.h> // IFNAMSIZ, struct ifreq, sockaddr_ll
#include <linux/if_ether.h> // ETH_P_ALL (0x003)
// ETH_FRAME_LEN (default 1514)
// ETH_ALEN (default 6)
#include <linux/net.h> // SOCK_RAW
#include <math.h> // fabsl(), floorl(), roundl()
#include <netinet/in.h> // htonl(), htons(), ntohl(), ntohs()
#include <signal.h> // signal()
#include <stdbool.h> // false, true
#include <stdio.h> // perror(), printf()
#include <stdlib.h> // atoi(), calloc(), EXIT_SUCCESS, free(),
// fscanf(), malloc(), strtoul(), strtoull()
#include <string.h> // memcpy(), strncmp(), strsep(), strtok_r()
#define MAX_IFS 64 // Max struct ifreq
#include <sys/ioctl.h> // ioctl()
#include <sys/socket.h> // AF_PACKET
#include "sysexits.h" // EX_USAGE, EX_NOPERM, EX_PROTOCOL, EX_SOFTWARE
#include <time.h> // clock_gettime(), struct timeval, time_t,
// struct tm
#include "unistd.h" // getuid(), sleep()
#ifndef CLOCK_MONOTONIC_RAW
#define CLOCK_MONOTONIC_RAW 4
#endif
#include "etherate.h"
#include "test_functions.c"
#include "functions.c"
#include "defaults.c"
#include "speed_tests.c"
#include "tests.c"
int main(int argc, char *argv[]) {
struct etherate eth;
// Global pointer used by signal handler function
eth_p = ð
uint8_t testing = true;
while(testing == true)
{
// Setup default application settings
int16_t def_ret = default_app(ð);
if (def_ret != EXIT_SUCCESS)
return def_ret;
// Check for root privs, low level socket access is required
if (getuid() != 0) {
printf("Must be root to use this program!\n");
reset_app(ð);
return EX_NOPERM;
}
// Parse CLI arguments
int16_t cli_ret = cli_args(argc, argv, ð);
if (cli_ret != EXIT_SUCCESS) {
reset_app(ð);
if (cli_ret == RET_EXIT_APP) {
return EXIT_SUCCESS;
} else {
return cli_ret;
}
}
// Set up a new socket bind to an interface
if (setup_sock(ð.intf) != EXIT_SUCCESS) {
int32_t errnum = errno;
reset_app(ð);
return errnum;
}
if (set_sock_int(ð) != EXIT_SUCCESS)
{
int32_t errnum = errno;
reset_app(ð);
return errnum;
}
update_frame_size(ð);
// Set the network interface to promiscuos mode
if (set_int_promisc(ð.intf) != EXIT_SUCCESS)
{
int32_t errnum = errno;
reset_app(ð);
return errnum;
}
// Declare sigint handler, TX will signal RX to reset when it quits.
// This can not be declared until now because the interfaces must be
// set up so that a dying gasp can be sent.
signal (SIGINT, signal_handler);
// Set up the frame data and send broadcasts
if (setup_frame(ð) != EXIT_SUCCESS)
{
int32_t errnum = errno;
reset_app(ð);
return errnum;
}
if (eth.app.tx_sync == true)
sync_settings(ð);
// Pause to allow the RX host to process the sent settings
sleep(2);
// If this is the RX host, rebuild the test frame headers incase any
// settings have been changed by the TX host
if (eth.app.tx_mode != true) build_headers(ð.frm);
// If using frame pacing, calculate the inter-frame delay now that the
// frame size is no longer subject to change
if (eth.params.f_tx_dly != F_TX_DLY_DEF) {
long double f_tx_max = floor((eth.params.f_tx_dly / (eth.params.f_size_total * 8)));
eth.params.f_tx_dly = (1000000000 / f_tx_max) * 1e-9;
printf("Pacing to a maximum rate of %.0Lffps (%.9Lfs spacing)\n",
f_tx_max, eth.params.f_tx_dly);
}
// Try to measure the TX to RX one way delay
if (eth.app.tx_delay == true)
delay_test(ð);
// Start the chosen test
eth.app.ts_now = time(0);
eth.app.tm_local = localtime(ð.app.ts_now);
printf("Starting test on %s\n", asctime(eth.app.tm_local));
if (eth.mtu_test.enabled) {
mtu_sweep_test(ð);
} else if (eth.qm_test.enabled) {
latency_test(ð);
} else if (eth.speed_test.f_payload_size > 0) {
eth.speed_test.enabled = true;
printf("Frame size is %" PRId16 " bytes\n", eth.params.f_size_total);
send_custom_frame(ð);
} else {
eth.speed_test.enabled = true;
printf("Frame size is %" PRId16 " bytes\n", eth.params.f_size_total);
speed_test_prep(ð);
}
eth.app.ts_now = time(0);
eth.app.tm_local = localtime(ð.app.ts_now);
printf("Ending test on %s\n\n", asctime(eth.app.tm_local));
if (remove_interface_promisc(ð.intf) != EXIT_SUCCESS)
printf("Error leaving promiscuous mode\n");
reset_app(ð);
// End the testing loop if TX host
if (eth.app.tx_mode == true) testing = false;
}
return EXIT_SUCCESS;
}