test_tle_udp_stream.h revision 128f0192
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_UDP_STREAM_H_
17#define TEST_TLE_UDP_STREAM_H_
18#include <iostream>
19#include <arpa/inet.h>
20#include <gtest/gtest.h>
21#include <gmock/gmock.h>
22#include <rte_errno.h>
23
24#include <tle_udp_impl.h>
25#include <tle_event.h>
26
27int
28dummy_lookup4(void *opaque, const struct in_addr *addr,
29	struct tle_udp_dest *res)
30{
31	RTE_SET_USED(opaque);
32	RTE_SET_USED(addr);
33	RTE_SET_USED(res);
34	return -ENOENT;
35}
36
37int
38dummy_lookup6(void *opaque, const struct in6_addr *addr,
39	struct tle_udp_dest *res)
40{
41	RTE_SET_USED(opaque);
42	RTE_SET_USED(addr);
43	RTE_SET_USED(res);
44	return -ENOENT;
45}
46
47struct tle_udp_ctx_param ctx_prm_tmpl = {
48	.socket_id = SOCKET_ID_ANY,
49	.max_streams = 0x10,
50	.max_stream_rbufs = 0x100,
51	.max_stream_sbufs = 0x100
52};
53
54struct tle_udp_dev_param dev_prm_tmpl = {
55	.rx_offload = 0x100,
56	.tx_offload = 0x100
57};
58
59class test_tle_udp_stream: public ::testing::Test {
60public:
61	void setup_dev_prm(struct tle_udp_dev_param *,
62			char const *, char const *);
63	struct tle_udp_ctx *setup_ctx(struct tle_udp_ctx_param *prm);
64	struct tle_udp_dev *setup_dev(struct tle_udp_ctx *ctx,
65			struct tle_udp_dev_param *dev_prm);
66	struct tle_evq *setup_event();
67
68	virtual void SetUp(void)
69	{
70		char const *ipv4_laddr = "192.168.0.1";
71		char const *ipv4_raddr = "192.168.0.2";
72		char const *ipv6 = "fe80::21e:67ff:fec2:2568";
73		struct tle_udp_ctx_param cprm;
74
75		ctx = nullptr;
76		dev = nullptr;
77		stream = nullptr;
78		/* Setup Context */
79		cprm = ctx_prm_tmpl;
80		cprm.lookup4 = dummy_lookup4;
81		cprm.lookup6 = dummy_lookup6;
82		ctx = setup_ctx(&cprm);
83		/* Setup Dev */
84		memset(&dev_prm, 0, sizeof(dev_prm));
85		setup_dev_prm(&dev_prm, ipv4_laddr, ipv6);
86		dev = setup_dev(ctx, &dev_prm);
87
88		/* Stream Param & Event param */
89		memset(&stream_prm, 0, sizeof(struct tle_udp_stream_param));
90		inet_pton(AF_INET, ipv4_laddr, &stream_prm.local_addr);
91		inet_pton(AF_INET, ipv4_raddr, &stream_prm.remote_addr);
92		stream_prm.local_addr.ss_family = AF_INET;
93		stream_prm.remote_addr.ss_family = AF_INET;
94		stream_prm.recv_ev = tle_event_alloc(setup_event(), nullptr);
95		stream_prm.send_ev = tle_event_alloc(setup_event(), nullptr);
96	}
97
98	virtual void TearDown(void)
99	{
100		ret = 0;
101		tle_udp_stream_close(stream);
102		tle_udp_del_dev(dev);
103		tle_udp_destroy(ctx);
104	}
105
106	int ret;
107	struct tle_udp_ctx *ctx;
108	struct tle_udp_dev *dev;
109	struct tle_udp_stream *stream;
110
111	struct tle_udp_ctx_param ctx_prm;
112	struct tle_udp_dev_param dev_prm;
113	struct tle_udp_stream_param stream_prm;
114};
115
116struct tle_evq *test_tle_udp_stream::setup_event() {
117	int32_t socket_id;
118	uint32_t max_events;
119	struct tle_evq_param evq_params;
120	struct tle_evq *evq;
121
122	socket_id = SOCKET_ID_ANY;
123	max_events = 10;
124	rte_errno = 0;
125	memset(&evq_params, 0, sizeof(struct tle_evq_param));
126	evq_params.socket_id = socket_id;
127	evq_params.max_events = max_events;
128	evq = tle_evq_create(&evq_params);
129	return evq;
130}
131
132struct tle_udp_ctx
133*test_tle_udp_stream::setup_ctx(struct tle_udp_ctx_param *prm) {
134	struct tle_udp_ctx *ctx;
135
136	ctx = tle_udp_create(prm);
137
138	return ctx;
139}
140
141struct tle_udp_dev
142*test_tle_udp_stream::setup_dev(struct tle_udp_ctx *ctx,
143		struct tle_udp_dev_param *dev_prm) {
144
145	struct tle_udp_dev *dev;
146
147	dev = tle_udp_add_dev(ctx, dev_prm);
148
149	return dev;
150}
151
152void test_tle_udp_stream::setup_dev_prm(struct tle_udp_dev_param *dev_prm,
153		char const *ipv4, char const *ipv6) {
154
155	inet_pton(AF_INET, ipv4, &dev_prm->local_addr4);
156	inet_pton(AF_INET6, ipv6, &dev_prm->local_addr6);
157
158}
159
160#endif /* TEST_TLE_UDP_STREAM_H_ */
161