test_tle_tcp_stream.h revision aa97dd1c
1/*
2 * Copyright (c) 2016  Intel Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef TEST_TLE_TCP_STREAM_H_
17#define TEST_TLE_TCP_STREAM_H_
18
19#include <iostream>
20#include <arpa/inet.h>
21#include <netinet/ip6.h>
22#include <sys/socket.h>
23#include <netdb.h>
24#include <gtest/gtest.h>
25#include <gmock/gmock.h>
26#include <rte_errno.h>
27
28#include <tle_event.h>
29#include <tle_ctx.h>
30#include <tle_tcp.h>
31
32#include "test_common.h"
33
34#define MAX_STREAMS          0x100
35#define MAX_STREAM_RBUFS     0x100
36#define MAX_STREAM_SBUFS     0x100
37#define RX_NO_OFFLOAD        0x0
38#define TX_NO_OFFLOAD        0x0
39
40static struct tle_ctx_param ctx_prm_tmpl = {
41	.socket_id = SOCKET_ID_ANY,
42	.proto = TLE_PROTO_TCP,
43	.max_streams = MAX_STREAMS,
44	.max_stream_rbufs = MAX_STREAM_RBUFS,
45	.max_stream_sbufs = MAX_STREAM_SBUFS,
46};
47
48static struct tle_dev_param dev_prm_tmpl = {
49	.rx_offload = RX_NO_OFFLOAD,
50	.tx_offload = TX_NO_OFFLOAD
51};
52
53class tcp_stream_base: public ::testing::Test {
54
55public:
56	struct tle_ctx *setup_ctx(struct tle_ctx_param *prm);
57	struct tle_dev *setup_dev(struct tle_ctx *ctx,
58				struct tle_dev_param *dev_prm);
59	void setup_dev_prm(struct tle_dev_param *dev_prm,
60			char const *ipv4, char const *ipv6);
61	int setup_stream_prm(struct tle_tcp_stream_param *stream_prm,
62			char const *l_ip, char const *r_ip,
63			int l_port, int r_port);
64	struct tle_evq *setup_event();
65};
66
67struct tle_evq
68*tcp_stream_base::setup_event()
69{
70	int32_t socket_id;
71	uint32_t max_events;
72	struct tle_evq_param evq_params;
73	struct tle_evq *evq;
74
75	socket_id = SOCKET_ID_ANY;
76	max_events = 10;
77	rte_errno = 0;
78	memset(&evq_params, 0, sizeof(struct tle_evq_param));
79	evq_params.socket_id = socket_id;
80	evq_params.max_events = max_events;
81	evq = tle_evq_create(&evq_params);
82	return evq;
83}
84
85struct tle_ctx
86*tcp_stream_base::setup_ctx(struct tle_ctx_param *prm)
87{
88	struct tle_ctx *ctx;
89
90	prm->lookup4 = dummy_lookup4;
91	prm->lookup6 = dummy_lookup6;
92
93	ctx = tle_ctx_create(prm);
94
95	return ctx;
96}
97
98struct tle_dev
99*tcp_stream_base::setup_dev(struct tle_ctx *ctx, struct tle_dev_param *dev_prm)
100{
101	struct tle_dev *dev;
102
103	dev = tle_add_dev(ctx, dev_prm);
104
105	return dev;
106}
107
108void
109tcp_stream_base::setup_dev_prm(struct tle_dev_param *dev_prm, char const *ipv4,
110	char const *ipv6)
111{
112	inet_pton(AF_INET, ipv4, &dev_prm->local_addr4);
113	inet_pton(AF_INET6, ipv6, &dev_prm->local_addr6);
114}
115
116int
117tcp_stream_base::setup_stream_prm(struct tle_tcp_stream_param *stream_prm,
118	char const *l_ip, char const *r_ip, int l_port, int r_port)
119{
120	int32_t ret;
121	struct sockaddr_in *ip4_addr;
122	struct sockaddr_in6 *ip6_addr;
123	struct addrinfo hint, *res = NULL;
124	struct tle_tcp_stream_cfg stream_cfg;
125
126	memset(&hint, '\0', sizeof(hint));
127	memset(&stream_cfg, 0, sizeof(stream_cfg));
128
129	ret = getaddrinfo(l_ip, NULL, &hint, &res);
130	if (ret != 0)
131		return -EINVAL;
132
133	if (res->ai_family == AF_INET) {
134		ip4_addr = (struct sockaddr_in *) &stream_prm->addr.local;
135		ip4_addr->sin_family = AF_INET;
136		ip4_addr->sin_port = htons(l_port);
137		ip4_addr->sin_addr.s_addr = inet_addr(l_ip);
138	} else if (res->ai_family == AF_INET6) {
139		ip6_addr = (struct sockaddr_in6 *) &stream_prm->addr.local;
140		ip6_addr->sin6_family = AF_INET6;
141		inet_pton(AF_INET6, l_ip, &ip6_addr->sin6_addr);
142		ip6_addr->sin6_port = htons(l_port);
143	} else {
144		freeaddrinfo(res);
145		return -EINVAL;
146	}
147	freeaddrinfo(res);
148
149	memset(&hint, '\0', sizeof(hint));
150	ret = getaddrinfo(r_ip, NULL, &hint, &res);
151	if (ret != 0)
152		return -EINVAL;
153
154	if (res->ai_family == AF_INET) {
155		ip4_addr = (struct sockaddr_in *) &stream_prm->addr.remote;
156		ip4_addr->sin_family = AF_INET;
157		ip4_addr->sin_port = htons(r_port);
158		ip4_addr->sin_addr.s_addr = inet_addr(r_ip);
159	} else if (res->ai_family == AF_INET6) {
160		ip6_addr = (struct sockaddr_in6 *) &stream_prm->addr.remote;
161		ip6_addr->sin6_family = AF_INET6;
162		inet_pton(AF_INET6, r_ip, &ip6_addr->sin6_addr);
163		ip6_addr->sin6_port = htons(r_port);
164	} else {
165		freeaddrinfo(res);
166		return -EINVAL;
167	}
168	freeaddrinfo(res);
169
170	stream_prm->cfg = stream_cfg;
171
172	return 0;
173}
174
175class test_tle_tcp_stream: public ::tcp_stream_base {
176protected:
177	virtual void SetUp(void)
178	{
179		ipv4_laddr = "192.0.0.1";
180		ipv4_raddr = "192.0.0.2";
181		ipv6_laddr = "2001::1000";
182		ipv6_raddr = "2001::2000";
183		l_port = 10000;
184		r_port = 10000;
185
186		memset(&ctx_prm, 0, sizeof(ctx_prm));
187		memset(&dev_prm, 0, sizeof(dev_prm));
188		memset(&stream_prm, 0, sizeof(stream_prm));
189		memset(&stream_prm6, 0, sizeof(stream_prm6));
190
191		ctx_prm = ctx_prm_tmpl;
192		dev_prm = dev_prm_tmpl;
193		setup_dev_prm(&dev_prm, ipv4_laddr, ipv6_laddr);
194		ret = setup_stream_prm(&stream_prm, ipv4_laddr, ipv4_raddr,
195			l_port, r_port);
196		ASSERT_EQ(ret, 0);
197		setup_stream_prm(&stream_prm6, ipv6_laddr, ipv6_raddr, l_port,
198			r_port);
199		ASSERT_EQ(ret, 0);
200
201		ctx = setup_ctx(&ctx_prm);
202		ASSERT_NE(ctx, (void *) NULL);
203		dev = setup_dev(ctx, &dev_prm);
204		ASSERT_NE(dev, (void *) NULL);
205	}
206
207	virtual void TearDown(void)
208	{
209		ret = 0;
210		tle_del_dev(dev);
211		tle_ctx_destroy(ctx);
212	}
213
214	int ret;
215	struct tle_ctx *ctx;
216	struct tle_dev *dev;
217	struct tle_stream *stream;
218	struct tle_stream *stream6;
219
220	struct tle_ctx_param ctx_prm;
221	struct tle_dev_param dev_prm;
222	struct tle_tcp_stream_param stream_prm;
223	struct tle_tcp_stream_param stream_prm6;
224
225	int l_port, r_port;
226	char const *ipv4_laddr;
227	char const *ipv4_raddr;
228	char const *ipv6_laddr;
229	char const *ipv6_raddr;
230};
231
232class test_tle_tcp_stream_ops: public ::test_tle_tcp_stream {
233public:
234	virtual void SetUp(void)
235	{
236		test_tle_tcp_stream::SetUp();
237		stream = tle_tcp_stream_open(ctx,
238			(const struct tle_tcp_stream_param *)&stream_prm);
239		stream6 = tle_tcp_stream_open(ctx,
240			(const struct tle_tcp_stream_param *)&stream_prm6);
241	}
242
243	virtual void TearDown(void)
244	{
245		tle_tcp_stream_close(stream6);
246		tle_tcp_stream_close(stream);
247		test_tle_tcp_stream::TearDown();
248	}
249};
250
251#endif /* TEST_TLE_TCP_STREAM_H_ */
252