test_tle_tcp_stream.h revision b8f1ef2b
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	. free_streams = {
45		.min = 0,
46		.max = 0,
47	},
48	.max_stream_rbufs = MAX_STREAM_RBUFS,
49	.max_stream_sbufs = MAX_STREAM_SBUFS,
50};
51
52static struct tle_dev_param dev_prm_tmpl = {
53	.rx_offload = RX_NO_OFFLOAD,
54	.tx_offload = TX_NO_OFFLOAD
55};
56
57class tcp_stream_base: public ::testing::Test {
58
59public:
60	struct tle_ctx *setup_ctx(struct tle_ctx_param *prm);
61	struct tle_dev *setup_dev(struct tle_ctx *ctx,
62				struct tle_dev_param *dev_prm);
63	void setup_dev_prm(struct tle_dev_param *dev_prm,
64			char const *ipv4, char const *ipv6);
65	int setup_stream_prm(struct tle_tcp_stream_param *stream_prm,
66			char const *l_ip, char const *r_ip,
67			int l_port, int r_port);
68	struct tle_evq *setup_event();
69};
70
71struct tle_evq
72*tcp_stream_base::setup_event()
73{
74	int32_t socket_id;
75	uint32_t max_events;
76	struct tle_evq_param evq_params;
77	struct tle_evq *evq;
78
79	socket_id = SOCKET_ID_ANY;
80	max_events = 10;
81	rte_errno = 0;
82	memset(&evq_params, 0, sizeof(struct tle_evq_param));
83	evq_params.socket_id = socket_id;
84	evq_params.max_events = max_events;
85	evq = tle_evq_create(&evq_params);
86	return evq;
87}
88
89struct tle_ctx
90*tcp_stream_base::setup_ctx(struct tle_ctx_param *prm)
91{
92	struct tle_ctx *ctx;
93
94	prm->lookup4 = dummy_lookup4;
95	prm->lookup6 = dummy_lookup6;
96
97	ctx = tle_ctx_create(prm);
98
99	return ctx;
100}
101
102struct tle_dev
103*tcp_stream_base::setup_dev(struct tle_ctx *ctx, struct tle_dev_param *dev_prm)
104{
105	struct tle_dev *dev;
106
107	dev = tle_add_dev(ctx, dev_prm);
108
109	return dev;
110}
111
112void
113tcp_stream_base::setup_dev_prm(struct tle_dev_param *dev_prm, char const *ipv4,
114	char const *ipv6)
115{
116	inet_pton(AF_INET, ipv4, &dev_prm->local_addr4);
117	inet_pton(AF_INET6, ipv6, &dev_prm->local_addr6);
118}
119
120int
121tcp_stream_base::setup_stream_prm(struct tle_tcp_stream_param *stream_prm,
122	char const *l_ip, char const *r_ip, int l_port, int r_port)
123{
124	int32_t ret;
125	struct sockaddr_in *ip4_addr;
126	struct sockaddr_in6 *ip6_addr;
127	struct addrinfo hint, *res = NULL;
128	struct tle_tcp_stream_cfg stream_cfg;
129
130	memset(&hint, '\0', sizeof(hint));
131	memset(&stream_cfg, 0, sizeof(stream_cfg));
132
133	ret = getaddrinfo(l_ip, NULL, &hint, &res);
134	if (ret != 0)
135		return -EINVAL;
136
137	if (res->ai_family == AF_INET) {
138		ip4_addr = (struct sockaddr_in *) &stream_prm->addr.local;
139		ip4_addr->sin_family = AF_INET;
140		ip4_addr->sin_port = htons(l_port);
141		ip4_addr->sin_addr.s_addr = inet_addr(l_ip);
142	} else if (res->ai_family == AF_INET6) {
143		ip6_addr = (struct sockaddr_in6 *) &stream_prm->addr.local;
144		ip6_addr->sin6_family = AF_INET6;
145		inet_pton(AF_INET6, l_ip, &ip6_addr->sin6_addr);
146		ip6_addr->sin6_port = htons(l_port);
147	} else {
148		freeaddrinfo(res);
149		return -EINVAL;
150	}
151	freeaddrinfo(res);
152
153	memset(&hint, '\0', sizeof(hint));
154	ret = getaddrinfo(r_ip, NULL, &hint, &res);
155	if (ret != 0)
156		return -EINVAL;
157
158	if (res->ai_family == AF_INET) {
159		ip4_addr = (struct sockaddr_in *) &stream_prm->addr.remote;
160		ip4_addr->sin_family = AF_INET;
161		ip4_addr->sin_port = htons(r_port);
162		ip4_addr->sin_addr.s_addr = inet_addr(r_ip);
163	} else if (res->ai_family == AF_INET6) {
164		ip6_addr = (struct sockaddr_in6 *) &stream_prm->addr.remote;
165		ip6_addr->sin6_family = AF_INET6;
166		inet_pton(AF_INET6, r_ip, &ip6_addr->sin6_addr);
167		ip6_addr->sin6_port = htons(r_port);
168	} else {
169		freeaddrinfo(res);
170		return -EINVAL;
171	}
172	freeaddrinfo(res);
173
174	stream_prm->cfg = stream_cfg;
175
176	return 0;
177}
178
179class test_tle_tcp_stream: public ::tcp_stream_base {
180protected:
181	virtual void SetUp(void)
182	{
183		ipv4_laddr = "192.0.0.1";
184		ipv4_raddr = "192.0.0.2";
185		ipv6_laddr = "2001::1000";
186		ipv6_raddr = "2001::2000";
187		l_port = 10000;
188		r_port = 10000;
189
190		memset(&ctx_prm, 0, sizeof(ctx_prm));
191		memset(&dev_prm, 0, sizeof(dev_prm));
192		memset(&stream_prm, 0, sizeof(stream_prm));
193		memset(&stream_prm6, 0, sizeof(stream_prm6));
194
195		ctx_prm = ctx_prm_tmpl;
196		dev_prm = dev_prm_tmpl;
197		setup_dev_prm(&dev_prm, ipv4_laddr, ipv6_laddr);
198		ret = setup_stream_prm(&stream_prm, ipv4_laddr, ipv4_raddr,
199			l_port, r_port);
200		ASSERT_EQ(ret, 0);
201		setup_stream_prm(&stream_prm6, ipv6_laddr, ipv6_raddr, l_port,
202			r_port);
203		ASSERT_EQ(ret, 0);
204
205		ctx = setup_ctx(&ctx_prm);
206		ASSERT_NE(ctx, (void *) NULL);
207		dev = setup_dev(ctx, &dev_prm);
208		ASSERT_NE(dev, (void *) NULL);
209	}
210
211	virtual void TearDown(void)
212	{
213		ret = 0;
214		tle_del_dev(dev);
215		tle_ctx_destroy(ctx);
216	}
217
218	int ret;
219	struct tle_ctx *ctx;
220	struct tle_dev *dev;
221	struct tle_stream *stream;
222	struct tle_stream *stream6;
223
224	struct tle_ctx_param ctx_prm;
225	struct tle_dev_param dev_prm;
226	struct tle_tcp_stream_param stream_prm;
227	struct tle_tcp_stream_param stream_prm6;
228
229	int l_port, r_port;
230	char const *ipv4_laddr;
231	char const *ipv4_raddr;
232	char const *ipv6_laddr;
233	char const *ipv6_raddr;
234};
235
236class test_tle_tcp_stream_ops: public ::test_tle_tcp_stream {
237public:
238	virtual void SetUp(void)
239	{
240		test_tle_tcp_stream::SetUp();
241		stream = tle_tcp_stream_open(ctx,
242			(const struct tle_tcp_stream_param *)&stream_prm);
243		stream6 = tle_tcp_stream_open(ctx,
244			(const struct tle_tcp_stream_param *)&stream_prm6);
245	}
246
247	virtual void TearDown(void)
248	{
249		tle_tcp_stream_close(stream6);
250		tle_tcp_stream_close(stream);
251		test_tle_tcp_stream::TearDown();
252	}
253};
254
255#endif /* TEST_TLE_TCP_STREAM_H_ */
256