aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ark/ark_pktchkr.c
blob: 202a1d9be819173f522130ba56faa503dd2f9f14 (plain)
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
/*-
 * BSD LICENSE
 *
 * Copyright (c) 2015-2017 Atomic Rules LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 * * Neither the name of copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <getopt.h>
#include <sys/time.h>
#include <locale.h>
#include <unistd.h>

#include <rte_ethdev.h>
#include <rte_malloc.h>

#include "ark_pktchkr.h"
#include "ark_logs.h"

static int set_arg(char *arg, char *val);
static int ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle);

#define ARK_MAX_STR_LEN 64
union OPTV {
	int INT;
	int BOOL;
	uint64_t LONG;
	char STR[ARK_MAX_STR_LEN];
};

enum OPTYPE {
	OTINT,
	OTLONG,
	OTBOOL,
	OTSTRING
};

struct OPTIONS {
	char opt[ARK_MAX_STR_LEN];
	enum OPTYPE t;
	union OPTV v;
};

static struct OPTIONS toptions[] = {
	{{"configure"}, OTBOOL, {1} },
	{{"port"}, OTINT, {0} },
	{{"mac-dump"}, OTBOOL, {0} },
	{{"dg-mode"}, OTBOOL, {1} },
	{{"run"}, OTBOOL, {0} },
	{{"stop"}, OTBOOL, {0} },
	{{"dump"}, OTBOOL, {0} },
	{{"en_resync"}, OTBOOL, {0} },
	{{"tuser_err_val"}, OTINT, {1} },
	{{"gen_forever"}, OTBOOL, {0} },
	{{"en_slaved_start"}, OTBOOL, {0} },
	{{"vary_length"}, OTBOOL, {0} },
	{{"incr_payload"}, OTINT, {0} },
	{{"incr_first_byte"}, OTBOOL, {0} },
	{{"ins_seq_num"}, OTBOOL, {0} },
	{{"ins_time_stamp"}, OTBOOL, {1} },
	{{"ins_udp_hdr"}, OTBOOL, {0} },
	{{"num_pkts"}, OTLONG, .v.LONG = 10000000000000L},
	{{"payload_byte"}, OTINT, {0x55} },
	{{"pkt_spacing"}, OTINT, {60} },
	{{"pkt_size_min"}, OTINT, {2005} },
	{{"pkt_size_max"}, OTINT, {1514} },
	{{"pkt_size_incr"}, OTINT, {1} },
	{{"eth_type"}, OTINT, {0x0800} },
	{{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L},
	{{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L},
	{{"hdr_dW0"}, OTINT, {0x0016e319} },
	{{"hdr_dW1"}, OTINT, {0x27150004} },
	{{"hdr_dW2"}, OTINT, {0x76967bda} },
	{{"hdr_dW3"}, OTINT, {0x08004500} },
	{{"hdr_dW4"}, OTINT, {0x005276ed} },
	{{"hdr_dW5"}, OTINT, {0x40004006} },
	{{"hdr_dW6"}, OTINT, {0x56cfc0a8} },
	{{"start_offset"}, OTINT, {0} },
	{{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"},
	{{"dst_port"}, OTINT, {65536} },
	{{"src_port"}, OTINT, {65536} },
};

ark_pkt_chkr_t
ark_pktchkr_init(void *addr, int ord, int l2_mode)
{
	struct ark_pkt_chkr_inst *inst =
		rte_malloc("ark_pkt_chkr_inst",
			   sizeof(struct ark_pkt_chkr_inst), 0);
	if (inst == NULL) {
		PMD_DRV_LOG(ERR, "Failed to malloc ark_pkt_chkr_inst.\n");
		return inst;
	}
	inst->sregs = (struct ark_pkt_chkr_stat_regs *)addr;
	inst->cregs =
		(struct ark_pkt_chkr_ctl_regs *)(((uint8_t *)addr) + 0x100);
	inst->ordinal = ord;
	inst->l2_mode = l2_mode;
	return inst;
}

void
ark_pktchkr_uninit(ark_pkt_chkr_t handle)
{
	rte_free(handle);
}

void
ark_pktchkr_run(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->sregs->pkt_start_stop = 0;
	inst->sregs->pkt_start_stop = 0x1;
}

int
ark_pktchkr_stopped(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
	uint32_t r = inst->sregs->pkt_start_stop;

	return (((r >> 16) & 1) == 1);
}

void
ark_pktchkr_stop(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
	int wait_cycle = 10;

	inst->sregs->pkt_start_stop = 0;
	while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
		usleep(1000);
		wait_cycle--;
		PMD_DEBUG_LOG(DEBUG, "Waiting for pktchk %d to stop...\n",
			      inst->ordinal);
	}
	PMD_DEBUG_LOG(DEBUG, "Pktchk %d stopped.\n", inst->ordinal);
}

int
ark_pktchkr_is_running(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
	uint32_t r = inst->sregs->pkt_start_stop;

	return ((r & 1) == 1);
}

static void
ark_pktchkr_set_pkt_ctrl(ark_pkt_chkr_t handle,
			 uint32_t gen_forever,
			 uint32_t vary_length,
			 uint32_t incr_payload,
			 uint32_t incr_first_byte,
			 uint32_t ins_seq_num,
			 uint32_t ins_udp_hdr,
			 uint32_t en_resync,
			 uint32_t tuser_err_val,
			 uint32_t ins_time_stamp)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
	uint32_t r = (tuser_err_val << 16) | (en_resync << 0);

	inst->sregs->pkt_ctrl = r;
	if (!inst->l2_mode)
		ins_udp_hdr = 0;
	r = ((gen_forever << 24) |
	     (vary_length << 16) |
	     (incr_payload << 12) |
	     (incr_first_byte << 8) |
	     (ins_time_stamp << 5) |
	     (ins_seq_num << 4) |
	     ins_udp_hdr);
	inst->cregs->pkt_ctrl = r;
}

static
int
ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
	uint32_t r = inst->cregs->pkt_ctrl;

	return (((r >> 24) & 1) == 1);
}

int
ark_pktchkr_wait_done(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	if (ark_pktchkr_is_gen_forever(handle)) {
		PMD_DEBUG_LOG(ERR, "Pktchk wait_done will not terminate"
			      " because gen_forever=1\n");
		return -1;
	}
	int wait_cycle = 10;

	while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
		usleep(1000);
		wait_cycle--;
		PMD_DEBUG_LOG(DEBUG, "Waiting for packet checker %d's"
			      " internal pktgen to finish sending...\n",
			      inst->ordinal);
		PMD_DEBUG_LOG(DEBUG, "Pktchk %d's pktgen done.\n",
			      inst->ordinal);
	}
	return 0;
}

int
ark_pktchkr_get_pkts_sent(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	return inst->cregs->pkts_sent;
}

void
ark_pktchkr_set_payload_byte(ark_pkt_chkr_t handle, uint32_t b)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->pkt_payload = b;
}

void
ark_pktchkr_set_pkt_size_min(ark_pkt_chkr_t handle, uint32_t x)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->pkt_size_min = x;
}

void
ark_pktchkr_set_pkt_size_max(ark_pkt_chkr_t handle, uint32_t x)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->pkt_size_max = x;
}

void
ark_pktchkr_set_pkt_size_incr(ark_pkt_chkr_t handle, uint32_t x)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->pkt_size_incr = x;
}

void
ark_pktchkr_set_num_pkts(ark_pkt_chkr_t handle, uint32_t x)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->num_pkts = x;
}

void
ark_pktchkr_set_src_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
	inst->cregs->src_mac_addr_l = mac_addr & 0xffffffff;
}

void
ark_pktchkr_set_dst_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
	inst->cregs->dst_mac_addr_l = mac_addr & 0xffffffff;
}

void
ark_pktchkr_set_eth_type(ark_pkt_chkr_t handle, uint32_t x)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	inst->cregs->eth_type = x;
}

void
ark_pktchkr_set_hdr_dW(ark_pkt_chkr_t handle, uint32_t *hdr)
{
	uint32_t i;
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	for (i = 0; i < 7; i++)
		inst->cregs->hdr_dw[i] = hdr[i];
}

void
ark_pktchkr_dump_stats(ark_pkt_chkr_t handle)
{
	struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;

	PMD_STATS_LOG(INFO, "pkts_rcvd      = (%'u)\n",
		      inst->sregs->pkts_rcvd);
	PMD_STATS_LOG(INFO, "bytes_rcvd     = (%'" PRIU64 ")\n",
		      inst->sregs->bytes_rcvd);
	PMD_STATS_LOG(INFO, "pkts_ok        = (%'u)\n",
		      inst->sregs->pkts_ok);
	PMD_STATS_LOG(INFO, "pkts_mismatch  = (%'u)\n",
		      inst->sregs->pkts_mismatch);
	PMD_STATS_LOG(INFO, "pkts_err       = (%'u)\n",
		      inst->sregs->pkts_err);
	PMD_STATS_LOG(INFO, "first_mismatch = (%'u)\n",
		      inst->sregs->first_mismatch);
	PMD_STATS_LOG(INFO, "resync_events  = (%'u)\n",
		      inst->sregs->resync_events);
	PMD_STATS_LOG(INFO, "pkts_missing   = (%'u)\n",
		      inst->sregs->pkts_missing);
	PMD_STATS_LOG(INFO, "min_latency    = (%'u)\n",
		      inst->sregs->min_latency);
	PMD_STATS_LOG(INFO, "max_latency    = (%'u)\n",
		      inst->sregs->max_latency);
}

static struct OPTIONS *
options(const char *id)
{
	unsigned int i;

	for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
		if (strcmp(id, toptions[i].opt) == 0)
			return &toptions[i];
	}
	PMD_DRV_LOG(ERR,
		    "pktchkr: Could not find requested option!, option = %s\n",
		    id);
	return NULL;
}

static int
set_arg(char *arg, char *val)
{
	struct OPTIONS *o = options(arg);

	if (o) {
		switch (o->t) {
		case OTINT:
		case OTBOOL:
			o->v.INT = atoi(val);
			break;
		case OTLONG:
			o->v.INT = atoll(val);
			break;
		case OTSTRING:
			snprintf(o->v.STR, ARK_MAX_STR_LEN, "%s", val);
			break;
		}
		return 1;
	}
	return 0;
}

/******
 * Arg format = "opt0=v,opt_n=v ..."
 ******/
void
ark_pktchkr_parse(char *args)
{
	char *argv, *v;
	const char toks[] = "=\n\t\v\f \r";
	argv = strtok(args, toks);
	v = strtok(NULL, toks);
	while (argv && v) {
		set_arg(argv, v);
		argv = strtok(NULL, toks);
		v = strtok(NULL, toks);
	}
}

static int32_t parse_ipv4_string(char const *ip_address);
static int32_t
parse_ipv4_string(char const *ip_address)
{
	unsigned int ip[4];

	if (sscanf(ip_address, "%u.%u.%u.%u",
		   &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
		return 0;
	return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
}

void
ark_pktchkr_setup(ark_pkt_chkr_t handle)
{
	uint32_t hdr[7];
	int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);

	if (!options("stop")->v.BOOL && options("configure")->v.BOOL) {
		ark_pktchkr_set_payload_byte(handle,
					     options("payload_byte")->v.INT);
		ark_pktchkr_set_src_mac_addr(handle,
					     options("src_mac_addr")->v.INT);
		ark_pktchkr_set_dst_mac_addr(handle,
					     options("dst_mac_addr")->v.LONG);

		ark_pktchkr_set_eth_type(handle,
					 options("eth_type")->v.INT);
		if (options("dg-mode")->v.BOOL) {
			hdr[0] = options("hdr_dW0")->v.INT;
			hdr[1] = options("hdr_dW1")->v.INT;
			hdr[2] = options("hdr_dW2")->v.INT;
			hdr[3] = options("hdr_dW3")->v.INT;
			hdr[4] = options("hdr_dW4")->v.INT;
			hdr[5] = options("hdr_dW5")->v.INT;
			hdr[6] = options("hdr_dW6")->v.INT;
		} else {
			hdr[0] = dst_ip;
			hdr[1] = options("dst_port")->v.INT;
			hdr[2] = options("src_port")->v.INT;
			hdr[3] = 0;
			hdr[4] = 0;
			hdr[5] = 0;
			hdr[6] = 0;
		}
		ark_pktchkr_set_hdr_dW(handle, hdr);
		ark_pktchkr_set_num_pkts(handle,
					 options("num_pkts")->v.INT);
		ark_pktchkr_set_pkt_size_min(handle,
					     options("pkt_size_min")->v.INT);
		ark_pktchkr_set_pkt_size_max(handle,
					     options("pkt_size_max")->v.INT);
		ark_pktchkr_set_pkt_size_incr(handle,
					      options("pkt_size_incr")->v.INT);
		ark_pktchkr_set_pkt_ctrl(handle,
					 options("gen_forever")->v.BOOL,
					 options("vary_length")->v.BOOL,
					 options("incr_payload")->v.BOOL,
					 options("incr_first_byte")->v.BOOL,
					 options("ins_seq_num")->v.INT,
					 options("ins_udp_hdr")->v.BOOL,
					 options("en_resync")->v.BOOL,
					 options("tuser_err_val")->v.INT,
					 options("ins_time_stamp")->v.INT);
	}

	if (options("stop")->v.BOOL)
		ark_pktchkr_stop(handle);

	if (options("run")->v.BOOL) {
		PMD_DEBUG_LOG(DEBUG, "Starting packet checker on port %d\n",
			      options("port")->v.INT);
		ark_pktchkr_run(handle);
	}
}