-
Notifications
You must be signed in to change notification settings - Fork 19
/
defaults.c
336 lines (267 loc) · 10.9 KB
/
defaults.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
/*
* 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 Setup Functions
*
*/
#include "defaults.h"
int16_t default_app(struct etherate *eth)
{
eth->app.broadcast = true;
eth->app.tx_mode = true;
eth->app.tx_sync = true;
eth->app.tx_delay = TX_DELAY_DEF;
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] = 0x02;
eth->frm.etype = ETYPE_DEF;
eth->frm.length = HEADERS_LEN_DEF;
eth->frm.lsp_dst_mac[0] = 0x00;
eth->frm.lsp_dst_mac[1] = 0x00;
eth->frm.lsp_dst_mac[2] = 0x00;
eth->frm.lsp_dst_mac[3] = 0x00;
eth->frm.lsp_dst_mac[4] = 0x00;
eth->frm.lsp_dst_mac[5] = 0x00;
eth->frm.lsp_src_mac[0] = 0x00;
eth->frm.lsp_src_mac[1] = 0x00;
eth->frm.lsp_src_mac[2] = 0x00;
eth->frm.lsp_src_mac[3] = 0x00;
eth->frm.lsp_src_mac[4] = 0x00;
eth->frm.lsp_src_mac[5] = 0x00;
eth->frm.mpls_labels = 0;
for (uint16_t i = 0; i<MPLS_LABELS_MAX; i += 1) {
eth->frm.mpls_label[i] = 0;
eth->frm.mpls_exp[i] = 0;
eth->frm.mpls_ttl[i] = 0;
}
eth->frm.pcp = PCP_DEF;
eth->frm.pwe_ctrl_word = 0;
eth->frm.qinq_dei = 0;
eth->frm.qinq_id = QINQ_ID_DEF;
eth->frm.qinq_pcp = QINQ_PCP_DEF;
eth->frm.rx_buffer = (uint8_t*)calloc(1, F_SIZE_MAX);
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] = 0x01;
eth->frm.tlv_size = sizeof(uint8_t) + sizeof(uint16_t) +
sizeof(uint32_t);
eth->frm.sub_tlv_size = eth->frm.tlv_size + sizeof(uint8_t) +
sizeof(uint16_t) + sizeof(uint64_t);
eth->frm.sub_tlv_val = NULL; /////
eth->frm.tx_buffer = (uint8_t*)calloc(1, F_SIZE_MAX);
eth->frm.vlan_dei = 0;
eth->frm.vlan_id = VLAN_ID_DEF;
if (eth->frm.rx_buffer == NULL ||
eth->frm.tx_buffer == NULL ) {
perror("Couldn't allocate Tx/Rx buffers ");
return EXIT_FAILURE;
}
eth->mtu_test.enabled = false;
eth->mtu_test.largest = 0;
eth->mtu_test.mtu_tx_min = 1400;
eth->mtu_test.mtu_tx_max = 1500;
eth->qm_test.enabled = false;
eth->qm_test.delay_test_count = 10000;
eth->qm_test.interval = 1000;
eth->qm_test.interval_sec = 0;
eth->qm_test.interval_nsec = 0;
eth->qm_test.interval_min = 99999.99999;
eth->qm_test.interval_max = 0.0;
eth->qm_test.jitter_min = 999999.999999;
eth->qm_test.jitter_max = 0.0;
eth->qm_test.rtt_min = 999999.999999;
eth->qm_test.rtt_max = 0.0;
eth->qm_test.test_count = 0;
eth->qm_test.time_tx_1 = NULL;
eth->qm_test.time_tx_2 = NULL;
eth->qm_test.time_rx_1 = NULL;
eth->qm_test.time_rx_2 = NULL;
eth->qm_test.time_tx_diff = NULL;
eth->qm_test.time_rx_diff = NULL;
eth->qm_test.timeout = 1000;
eth->qm_test.timeout_nsec = 0;
eth->qm_test.timeout_sec = 0;
eth->qm_test.timeout_count = 0;
eth->qm_test.delay_results = (double*)calloc(eth->qm_test.delay_test_count,sizeof(double));
if (eth->qm_test.delay_results == NULL) {
perror("Couldn't allocate quality test results buffer ");
return EXIT_FAILURE;
}
eth->speed_test.enabled = false;
eth->speed_test.b_rx = 0;
eth->speed_test.b_rx_prev = 0;
eth->speed_test.b_tx = 0;
eth->speed_test.b_tx_prev = 0;
eth->speed_test.b_tx_speed_max = B_TX_SPEED_MAX_DEF;
eth->speed_test.b_tx_speed_prev = 0;
eth->speed_test.f_payload = (uint8_t*)calloc(1, F_SIZE_MAX);
eth->speed_test.f_payload_size = 0;
eth->speed_test.f_index_prev = 0;
eth->speed_test.f_speed = 0;
eth->speed_test.f_speed_avg = 0;
eth->speed_test.f_speed_max = 0;
eth->speed_test.b_speed = 0;
eth->speed_test.b_speed_max = 0;
eth->speed_test.b_speed_avg = 0;
if (eth->speed_test.f_payload == NULL) {
perror("Couldn't allocate custom frame buffer ");
return EXIT_FAILURE;
}
eth->intf.if_index = IF_INDEX_DEF;
for (uint16_t i = 0; i<IFNAMSIZ; i += 1) {
eth->intf.if_name[i] = 0;
}
eth->intf.sock_fd = SOCK_FD_DEF;
eth->params.f_ack = false;
eth->params.f_ack_pending = 0;
eth->params.f_bytes = F_BYTES_DEF;
eth->params.f_count = F_COUNT_DEF;
eth->params.f_duration = F_DURATION_DEF;
eth->params.f_rx_count = 0;
eth->params.f_rx_count_prev = 0;
eth->params.f_rx_early = 0;
eth->params.f_rx_late = 0;
eth->params.f_rx_ontime = 0;
eth->params.f_rx_other = 0;
eth->params.f_tx_dly = F_TX_DLY_DEF;
eth->params.f_tx_count = 0;
eth->params.f_tx_count_max = F_TX_COUNT_MAX_DEF;
eth->params.f_tx_count_prev = 0;
eth->params.f_size = F_SIZE_DEF;
eth->params.f_size_total = F_SIZE_DEF + eth->frm.length;
eth->params.f_waiting_ack = false;
eth->params.s_elapsed = 0;
return EXIT_SUCCESS;
}
int16_t setup_frame(struct etherate *eth)
{
printf("Source MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
eth->frm.src_mac[0], eth->frm.src_mac[1],
eth->frm.src_mac[2], eth->frm.src_mac[3],
eth->frm.src_mac[4], eth->frm.src_mac[5]);
printf("Destination MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
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]);
// Fill the test frame buffer with random data
for (uint32_t i = 0; i < (uint16_t)(F_SIZE_MAX - eth->frm.length); i += 1)
{
eth->frm.tx_data[i] = (uint8_t)((255.0*rand()/(RAND_MAX+1.0)));
}
if (eth->app.broadcast == true) {
// Broadcast to populate any switch/MAC tables
int16_t broad_ret = broadcast_etherate(eth);
if (broad_ret != 0) return broad_ret;
}
build_headers(ð->frm);
return EXIT_SUCCESS;
}
int16_t setup_sock(struct intf *intf)
{
// Setup a new raw socket
intf->sock_fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (intf->sock_fd < 0 )
{
perror("Error opening socket ");
return EX_SOFTWARE;
}
// On newer Kernels skip the interface qdisc for a slight speed increase,
// requires Kernel version 3.14.0 or newer
#if defined(PACKET_QDISC_BYPASS)
static const int32_t sock_qdisc_bypass = 1;
int32_t sock_qdisc_ret = setsockopt(intf->sock_fd, SOL_PACKET, PACKET_QDISC_BYPASS, &sock_qdisc_bypass, sizeof(sock_qdisc_bypass));
if (sock_qdisc_ret == -1) {
perror("Error enabling QDISC bypass on socket ");
return EXIT_FAILURE;
}
#endif
return EXIT_SUCCESS;
}
int16_t set_sock_int(struct etherate *eth)
{
// If the user has supplied an interface index try to use that
if (eth->intf.if_index != IF_INDEX_DEF) {
eth->intf.if_index = set_sock_interface_index(ð->intf);
if (eth->intf.if_index <= 0)
{
printf("Error: Couldn't set interface with index, "
"returned index was %" PRId32 "!\n",
eth->intf.if_index);
return EX_SOFTWARE;
}
// Or if the user has supplied an interface name try to use that
} else if (strcmp((char*)eth->intf.if_name, "") != 0) {
eth->intf.if_index = set_sock_interface_name(ð->intf);
if (eth->intf.if_index <= 0)
{
printf("Error: Couldn't set interface index from name, "
"returned index was %" PRId32 "!\n",
eth->intf.if_index);
return EX_SOFTWARE;
}
// Otherwise, try and best guess an interface
} else if (eth->intf.if_index == IF_INDEX_DEF) {
eth->intf.if_index = get_sock_interface(ð->intf);
if (eth->intf.if_index <= 0)
{
printf("Error: Couldn't find appropriate interface ID, "
"returned ID was %" PRId32 "!\n",
eth->intf.if_index);
return EX_SOFTWARE;
}
}
// Link layer socket setup
eth->intf.sock_addr.sll_family = AF_PACKET;
eth->intf.sock_addr.sll_protocol = htons(ETH_P_ALL);
eth->intf.sock_addr.sll_ifindex = eth->intf.if_index;
eth->intf.sock_addr.sll_hatype = ARPHRD_ETHER;
eth->intf.sock_addr.sll_pkttype = PACKET_OTHERHOST;
eth->intf.sock_addr.sll_halen = ETH_ALEN;
eth->intf.sock_addr.sll_addr[0] = eth->frm.dst_mac[0];
eth->intf.sock_addr.sll_addr[1] = eth->frm.dst_mac[1];
eth->intf.sock_addr.sll_addr[2] = eth->frm.dst_mac[2];
eth->intf.sock_addr.sll_addr[3] = eth->frm.dst_mac[3];
eth->intf.sock_addr.sll_addr[4] = eth->frm.dst_mac[4];
eth->intf.sock_addr.sll_addr[5] = eth->frm.dst_mac[5];
eth->intf.sock_addr.sll_addr[6] = 0x00;
eth->intf.sock_addr.sll_addr[7] = 0x00;
// Bind the socket to the physical interface to remove duplicate frames
// when VLAN tagged sub-interfaces are present
int32_t sock_bind = bind(eth->intf.sock_fd,
(struct sockaddr *)ð->intf.sock_addr,
sizeof(eth->intf.sock_addr));
if (sock_bind == -1) {
perror("Can't bind socket to interface ");
return EXIT_FAILURE;
}
build_headers(ð->frm);
return EXIT_SUCCESS;
}