aboutsummaryrefslogtreecommitdiffstats
path: root/test/gtest/test_tle_udp_stream_gen.h
blob: d9d53370f338c2160fbbc45648e9f5cba2485045 (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
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
/*
 * Copyright (c) 2016  Intel Corporation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TEST_TLE_UDP_STREAM_GEN_H_
#define TEST_TLE_UDP_STREAM_GEN_H_

#include <sys/types.h>

#include <stdio.h>
#include <map>
#include <string>
#include <algorithm>
#include <arpa/inet.h>
#include <netinet/ip6.h>
#include <sys/socket.h>
#include <netdb.h>
#include <gtest/gtest.h>

#include <rte_version.h>

#include <tle_udp.h>
#include <tle_event.h>

#include "test_common.h"

#define RX_NO_OFFLOAD 0
#define TX_NO_OFFLOAD 0
#define CTX_MAX_RBUFS 0x100
#define CTX_MAX_SBUFS 0x100

#define RX_PCAP "rx_pcap.cap"
#define TX_PCAP "tx_pcap.cap"

/*
 * Check DPDK version:
 * Previous "eth_pcap" was changed to "net_pcap" after DPDK 16.07.
 * Use correct vdev name depending on version.
 */
#if (RTE_VERSION_NUM(16, 7, 0, 0) < \
	RTE_VERSION_NUM(RTE_VER_YEAR, RTE_VER_MONTH, 0, 0))
	#define VDEV_NAME "net_pcap0"
#else
	#define VDEV_NAME "eth_pcap0"
#endif

using namespace std;

extern struct rte_mempool *mbuf_pool;

/* Dummy lookup functions, TX operations are not performed in these tests */

static int
lookup4_function(void *opaque, const struct in_addr *addr, struct tle_dest *res)
{
	struct in_addr route;
	struct ether_hdr *eth;
	struct ipv4_hdr *ip4h;
	auto routes = static_cast<map<string, tle_dev *> *>(opaque);

	/* Check all routes added in map for a match with dest *addr */
	for (auto it = routes->begin(); it != routes->end(); ++it) {
		inet_pton(AF_INET, it->first.c_str(), &route);

		/* If it matches then fill *res and return with 0 code */
		if (memcmp(&route, addr, sizeof(struct in_addr)) == 0) {
			memset(res, 0, sizeof(*res));
			res->dev = it->second;
			res->mtu = 1500;
			res->l2_len = sizeof(*eth);
			res->l3_len = sizeof(*ip4h);
			res->head_mp = mbuf_pool;
			eth = (struct ether_hdr *)res->hdr;
			eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
			ip4h = (struct ipv4_hdr *)(eth + 1);
			ip4h->version_ihl = (4 << 4) |
				(sizeof(*ip4h) / IPV4_IHL_MULTIPLIER);
			ip4h->time_to_live = 64;
			ip4h->next_proto_id = IPPROTO_UDP;
			ip4h->fragment_offset = 0;

			return 0;
		}
	}

	return -ENOENT;
}

static int
lookup6_function(void *opaque, const struct in6_addr *addr,
	struct tle_dest *res)
{
	struct ether_hdr *eth;
	struct ipv6_hdr *ip6h;
	struct in6_addr route;
	auto routes = static_cast<map<string, tle_dev *> *>(opaque);

	/* Check all routes added in map for a match with dest *addr */
	for (auto it = routes->begin(); it != routes->end(); ++it) {
		inet_pton(AF_INET6, it->first.c_str(), &route);

		/* If it matches then fill *res and return with 0 code */
		if (memcmp(&route, addr, sizeof(struct in6_addr)) == 0) {
			memset(res, 0, sizeof(*res));
			res->dev = it->second;
			res->mtu = 1500;
			res->l2_len = sizeof(*eth);
			res->l3_len = sizeof(*ip6h);
			res->head_mp = mbuf_pool;
			eth = (struct ether_hdr *)res->hdr;
			eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
			ip6h = (struct ipv6_hdr *)(eth + 1);
			ip6h->vtc_flow = 6 << 4;
			ip6h->proto = IPPROTO_UDP;
			ip6h->hop_limits = 64;

			return 0;
		}
	}
	return -ENOENT;
}

/*
 * Structures used to describe test instances:
 * test_str - main structure for describing test case instance; contains
 *            instance description, and vectors with information about
 *            devices, streams & streams to be generated for RX/TX path.
 * dev_s    - structure describing single device; contains device addresses,
 *            checksum offload information and expected number of received /
 *            transmitted packets.
 *            packets for that device in scenario.
 * stream_s - structure describing single stream to be created; contains
 *            information on local & remote IP's and port numbers, expected
 *            number of received and transmitted packets.
 * stream_g - structure describing a stream which to generate via scapy script;
 *            Contains information on IP addresses and port numbers and if
 *            L3/L4 checksums should be incorrectly calculated.
 *            In future: if packet should be fragmented.
 */

struct stream_g {
	int family;
	string src_ip;
	string dst_ip;
	int src_port;
	int dst_port;
	int nb_pkts;
	bool bad_chksum_l3;
	bool bad_chksum_l4;
	bool fragment;
};

struct stream_s {
	int family;
	int l_port;
	int r_port;
	string l_ip;
	string r_ip;
	int exp_pkts_rx;
	int exp_pkts_tx;
	int act_pkts_rx;
	int act_pkts_tx;
	tle_stream *ptr;
};

struct dev_s {
	string l_ipv4;
	string l_ipv6;
	uint64_t rx_offload;
	uint64_t tx_offload;
	int exp_pkts_bulk_rx;
	int exp_pkts_bulk_tx;
	int exp_pkts_enoent;
	int act_pkts_bulk_rx;
	int act_pkts_bulk_tx;
	int act_pkts_enoent;
	tle_dev *ptr;
};

struct test_str {
	string test_desc;
	vector<dev_s> devs;
	vector<stream_s> streams;
	vector<stream_g> gen_streams;
};

const char *vdevargs[] = {VDEV_NAME",rx_pcap=" RX_PCAP",tx_pcap=" TX_PCAP};

class test_tle_udp_gen_base : public testing::TestWithParam<test_str> {
public:

	tle_ctx *setup_ctx(void);
	tle_dev *setup_dev(tle_ctx *ctx, uint64_t rx_offload,
		uint64_t tx_offload, const char *local_ipv4,
		const char *local_ipv6);
	tle_evq *setup_evq(void);
	tle_event *setup_event(void);
	tle_stream *setup_stream(struct tle_ctx *ctx, int family,
		const char *l_ip, const char *r_ip, int l_port, int r_port);
	int setup_devices(dpdk_port_t *portid);
	int cleanup_devices(dpdk_port_t portid);
	int prepare_pcaps(string l_ip, string r_ip, int l_port, int r_port,
		int nb_pkts, int l3_chksum, int l4_chksum, string rx_pcap_dest);

	int cleanup_pcaps(const char *file);
	int close_streams(vector<struct stream_s> streams);
	int del_devs(vector<struct dev_s> devs);

	virtual void SetUp(void)
	{
		nb_ports = 1;
		tp = GetParam();

		/* Usual tldk stuff below -> ctx, dev, events etc. */
		ctx = setup_ctx();
		ASSERT_NE(ctx, nullptr);

		evq = setup_evq();
		ASSERT_NE(evq, nullptr);

		for (auto &d : tp.devs) {
			dev = setup_dev(ctx, d.rx_offload, d.tx_offload,
				d.l_ipv4.c_str(), d.l_ipv6.c_str());
			ASSERT_NE(dev, nullptr);

			/* Initialize counters for verifying results */
			d.act_pkts_bulk_rx = 0;
			d.act_pkts_bulk_tx = 0;
			d.act_pkts_enoent = 0;

			/* Save pointer to device */
			d.ptr = dev;
		}

		for (auto &s : tp.streams) {
			stream = setup_stream(ctx, s.family,
					s.l_ip.c_str(), s.r_ip.c_str(),
					s.l_port, s.r_port);
			ASSERT_NE(stream, nullptr);

			/* Initialize counters for verifying results */
			s.act_pkts_rx = 0;
			s.act_pkts_tx = 0;

			/* Save pointer to stream */
			s.ptr = stream;

			/* Find which dev has the same address as streams
			 * local address and save destination for later use
			 * in lookup functions
			 */
			if (s.family == AF_INET) {
				for (auto &d : tp.devs) {
					if (s.l_ip.compare(d.l_ipv4) == 0)
						routes4.insert(pair<string,
							tle_dev *>(s.r_ip,
								d.ptr));
				}
			} else if (s.family == AF_INET6) {
				for (auto &d : tp.devs) {
					if (s.l_ip.compare(d.l_ipv6) == 0)
						routes6.insert(pair<string,
							tle_dev *>(s.r_ip,
								d.ptr));
				}
			}
		}

		/* setup pcap/eth devices */
		setup_devices(&portid);
	}

	virtual void TearDown(void)
	{
		/*
		 * Remember to shutdown & detach rte devices
		 * and clean / delete .pcap files so not to
		 * interfere with next test
		 */
		close_streams(tp.streams);
		del_devs(tp.devs);
		tle_ctx_destroy(ctx);
		cleanup_devices(portid);
		cleanup_pcaps(RX_PCAP);
		cleanup_pcaps(TX_PCAP);
	}

	dpdk_port_t nb_ports;
	dpdk_port_t portid;
	uint32_t socket_id;
	uint32_t max_events;
	struct tle_ctx *ctx;
	struct tle_dev *dev;
	struct tle_evq *evq;
	struct tle_stream *stream;
	map<string, tle_dev *> routes4;
	map<string, tle_dev *> routes6;
	test_str tp;
	void *cb;
};

int
test_tle_udp_gen_base::setup_devices(dpdk_port_t *portid)
{
	/* attach + configure + start pmd device */
	if (rte_eth_dev_attach(vdevargs[0], portid) != 0)
		return -1;
	cb = rte_eth_add_rx_callback(*portid, 0,
		typen_rx_callback, nullptr);
	if (port_init(*portid, mbuf_pool) != 0)
		return -1;

	return 0;
}

int
test_tle_udp_gen_base::cleanup_devices(dpdk_port_t portid)
{
	/* release mbufs + detach device */
	char name[RTE_ETH_NAME_MAX_LEN];

	rte_eth_dev_stop(portid);
	rte_eth_dev_close(portid);
	rte_eth_dev_detach(portid, name);

	return 0;
}

int
test_tle_udp_gen_base::prepare_pcaps(string l_ip, string r_ip, int l_port,
	int r_port, int nb_pkts, int l3_chksum, int l4_chksum,
	string rx_pcap_dest)
{
	string py_cmd;

	/* generate pcap rx & tx files * for tests using scapy */
	py_cmd = "python " + string(binpath) + "/test_scapy_gen.py ";
	py_cmd = py_cmd + " " + l_ip + " " + r_ip + " " +
			to_string(l_port) + " " + to_string(r_port) + " " +
			to_string(nb_pkts);

	if (l3_chksum > 0)
		py_cmd = py_cmd + " -bc3 " + to_string(l3_chksum);
	if (l4_chksum > 0)
		py_cmd = py_cmd + " -bc4 " + to_string(l4_chksum);
	py_cmd = py_cmd + " " + rx_pcap_dest;
	system(py_cmd.c_str());
	return 0;
}

int
test_tle_udp_gen_base::cleanup_pcaps(const char *file)
{
	if (remove(file) != 0)
		perror("Error deleting pcap file");

	return 0;
}

tle_ctx *
test_tle_udp_gen_base::setup_ctx(void)
{

	struct tle_ctx *ctx;
	struct tle_ctx_param ctx_prm;

	memset(&ctx_prm, 0, sizeof(ctx_prm));
	ctx_prm.socket_id = SOCKET_ID_ANY;
	ctx_prm.max_streams = 0x10;
	ctx_prm.max_stream_rbufs = CTX_MAX_RBUFS;
	ctx_prm.max_stream_sbufs = CTX_MAX_SBUFS;
	ctx_prm.lookup4 = lookup4_function;
	ctx_prm.lookup6 = lookup6_function;
	ctx_prm.lookup4_data = &routes4;
	ctx_prm.lookup6_data = &routes6;

	ctx = tle_ctx_create(&ctx_prm);

	return ctx;
}

struct tle_dev *
test_tle_udp_gen_base::setup_dev(struct tle_ctx *ctx, uint64_t rx_offload,
	uint64_t tx_offload, const char *l_ipv4, const char *l_ipv6)
{
	struct tle_dev *dev;
	struct tle_dev_param dev_prm;

	memset(&dev_prm, 0, sizeof(dev_prm));
	dev_prm.rx_offload = RX_NO_OFFLOAD;
	dev_prm.tx_offload = TX_NO_OFFLOAD;
	if (l_ipv4 != NULL)
		inet_pton(AF_INET, l_ipv4, &(dev_prm).local_addr4);
	if (l_ipv6 != NULL)
		inet_pton(AF_INET6, l_ipv6, &(dev_prm).local_addr6);

	dev = tle_add_dev(ctx, &dev_prm);

	return dev;
}

struct tle_evq *
test_tle_udp_gen_base::setup_evq()
{
	uint32_t socket_id;
	uint32_t max_events;
	struct tle_evq_param evq_params;
	struct tle_evq *evq;

	socket_id = SOCKET_ID_ANY;
	max_events = 10;
	memset(&evq_params, 0, sizeof(struct tle_evq_param));

	evq_params.socket_id = socket_id;
	evq_params.max_events = max_events;
	evq = tle_evq_create(&evq_params);
	return evq;
}

struct tle_stream *
test_tle_udp_gen_base::setup_stream(struct tle_ctx *ctx, int family,
	const char *l_ip, const char *r_ip, int l_port, int r_port)
{
	struct tle_stream *stream;
	struct tle_udp_stream_param stream_prm;
	struct sockaddr_in *ip4_addr;
	struct sockaddr_in6 *ip6_addr;
	int32_t ret;

	memset(&stream_prm, 0, sizeof(stream_prm));

	if (family == AF_INET) {
		ip4_addr = (struct sockaddr_in *) &stream_prm.local_addr;
		ip4_addr->sin_family = AF_INET;
		ip4_addr->sin_port = htons(l_port);
		ip4_addr->sin_addr.s_addr = inet_addr(l_ip);

		ip4_addr = (struct sockaddr_in *) &stream_prm.remote_addr;
		ip4_addr->sin_family = AF_INET;
		ip4_addr->sin_port = htons(r_port);
		ip4_addr->sin_addr.s_addr = inet_addr(r_ip);
	} else if (family == AF_INET6) {
		ip6_addr = (struct sockaddr_in6 *) &stream_prm.local_addr;
		ip6_addr->sin6_family = AF_INET6;
		inet_pton(AF_INET6, l_ip, &ip6_addr->sin6_addr);
		ip6_addr->sin6_port = htons(l_port);

		ip6_addr = (struct sockaddr_in6 *) &stream_prm.remote_addr;
		ip6_addr->sin6_family = AF_INET6;
		inet_pton(AF_INET6, r_ip, &ip6_addr->sin6_addr);
		ip6_addr->sin6_port = htons(r_port);
	} else {
		printf("Invalid address family, stream not created\n");
		return NULL;
	}

	/* Not supporting callbacks and events at the moment */
	/* TODO: Add tests which use cb's and events. */
	stream_prm.recv_ev = tle_event_alloc(evq, nullptr);
	stream_prm.send_ev = tle_event_alloc(evq, nullptr);

	stream = tle_udp_stream_open(ctx,
			(const struct tle_udp_stream_param *) &stream_prm);

	return stream;
}

int
test_tle_udp_gen_base::close_streams(vector<struct stream_s> streams)
{
	int rc;

	for (auto &s : streams) {
		rc = tle_udp_stream_close(s.ptr);
		if (rc != 0)
			return -1;
	}

	return 0;
}

int
test_tle_udp_gen_base::del_devs(vector<struct dev_s> devs)
{
	int rc;

	for (auto &d : devs) {
		rc = tle_del_dev(d.ptr);
		if (rc != 0)
			return -1;
	}

	return 0;
}

class tle_rx_test : public test_tle_udp_gen_base {
public:
	virtual void SetUp(void)
	{
		/* Generate RX pcap file, for RX tests, then
		 * follow setup steps as in base class */
		tp = GetParam();

		for(auto &s : tp.gen_streams) {
			prepare_pcaps(s.src_ip.c_str(), s.dst_ip.c_str(),
				s.src_port, s.dst_port, s.nb_pkts,
				s.bad_chksum_l3, s.bad_chksum_l4, RX_PCAP);
		}
		test_tle_udp_gen_base::SetUp();
	}
};

class tle_rx_enobufs: public tle_rx_test { };

class tle_tx_test: public test_tle_udp_gen_base {
public:
	virtual void SetUp(void)
	{
		/* Generate 1-packet PCAP RX file so that virtual device can be
		 * initialized (needs a pcap file present during init), then
		 * follow setup steps as in base class
		 */
		prepare_pcaps("10.0.0.1", "10.0.0.1", 100, 100, 1, 0, 0,
			RX_PCAP);
		test_tle_udp_gen_base::SetUp();
	}
};

#endif /* TEST_TLE_UDP_STREAM_GEN_H_ */