1aa97dd1cSKonstantin Ananyev/*
27e18fa1bSKonstantin Ananyev * Copyright (c) 2016-2017  Intel Corporation.
3aa97dd1cSKonstantin Ananyev * Licensed under the Apache License, Version 2.0 (the "License");
4aa97dd1cSKonstantin Ananyev * you may not use this file except in compliance with the License.
5aa97dd1cSKonstantin Ananyev * You may obtain a copy of the License at:
6aa97dd1cSKonstantin Ananyev *
7aa97dd1cSKonstantin Ananyev *     http://www.apache.org/licenses/LICENSE-2.0
8aa97dd1cSKonstantin Ananyev *
9aa97dd1cSKonstantin Ananyev * Unless required by applicable law or agreed to in writing, software
10aa97dd1cSKonstantin Ananyev * distributed under the License is distributed on an "AS IS" BASIS,
11aa97dd1cSKonstantin Ananyev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12aa97dd1cSKonstantin Ananyev * See the License for the specific language governing permissions and
13aa97dd1cSKonstantin Ananyev * limitations under the License.
14aa97dd1cSKonstantin Ananyev */
15aa97dd1cSKonstantin Ananyev
16aa97dd1cSKonstantin Ananyev#ifndef _TLE_CTX_H_
17aa97dd1cSKonstantin Ananyev#define _TLE_CTX_H_
18aa97dd1cSKonstantin Ananyev
19aa97dd1cSKonstantin Ananyev#include <stdint.h>
20aa97dd1cSKonstantin Ananyev#include <sys/socket.h>
21aa97dd1cSKonstantin Ananyev#include <netinet/in.h>
22aa97dd1cSKonstantin Ananyev#include <rte_common.h>
23aa97dd1cSKonstantin Ananyev#include <rte_mbuf.h>
24aa97dd1cSKonstantin Ananyev
25aa97dd1cSKonstantin Ananyev#ifdef __cplusplus
26aa97dd1cSKonstantin Ananyevextern "C" {
27aa97dd1cSKonstantin Ananyev#endif
28aa97dd1cSKonstantin Ananyev
29aa97dd1cSKonstantin Ananyev/**
30aa97dd1cSKonstantin Ananyev * <tle_ctx>  - each such ctx represents an 'independent copy of the stack'.
31aa97dd1cSKonstantin Ananyev * It owns set of <stream>s and <dev>s entities and provides
32aa97dd1cSKonstantin Ananyev * (de)multiplexing input/output packets from/into devices into/from streams.
33aa97dd1cSKonstantin Ananyev * <dev> is an abstraction for the underlying device, that is able
34aa97dd1cSKonstantin Ananyev * to RX/TX packets and may provide some HW offload capabilities.
35aa97dd1cSKonstantin Ananyev * It is a user responsibility to add to the <ctx> all <dev>s,
36aa97dd1cSKonstantin Ananyev * that context has to manage, before starting to do stream operations
37aa97dd1cSKonstantin Ananyev * (open/send/recv,close) over that context.
38aa97dd1cSKonstantin Ananyev * Right now adding/deleting <dev>s to the context with open
39aa97dd1cSKonstantin Ananyev * streams is not supported.
40aa97dd1cSKonstantin Ananyev * <stream> represents an L4(UDP/TCP, etc.) endpoint <addr, port> and
41aa97dd1cSKonstantin Ananyev * is an analogy to socket entity.
42aa97dd1cSKonstantin Ananyev * As with a socket, there are ability to do recv/send over it.
43aa97dd1cSKonstantin Ananyev * <stream> belongs to particular <ctx> but is visible globally across
44aa97dd1cSKonstantin Ananyev * the process, i.e. any thread within the process can do recv/send over it
45aa97dd1cSKonstantin Ananyev * without any further synchronisation.
46aa97dd1cSKonstantin Ananyev * While 'upper' layer API is thread safe, lower layer API (rx_bulk/tx_bulk)
47aa97dd1cSKonstantin Ananyev * is not thread safe and is not supposed to be run on multiple threads
48aa97dd1cSKonstantin Ananyev * in parallel.
49aa97dd1cSKonstantin Ananyev * So single thread can drive multiple <ctx>s and do IO for them,
50aa97dd1cSKonstantin Ananyev * but multiple threads can't drive same <ctx> without some
51aa97dd1cSKonstantin Ananyev * explicit synchronization.
52aa97dd1cSKonstantin Ananyev */
53aa97dd1cSKonstantin Ananyev
54aa97dd1cSKonstantin Ananyevstruct tle_ctx;
55aa97dd1cSKonstantin Ananyevstruct tle_dev;
56aa97dd1cSKonstantin Ananyev
57aa97dd1cSKonstantin Ananyev/**
58aa97dd1cSKonstantin Ananyev * Blocked L4 ports info.
59aa97dd1cSKonstantin Ananyev */
60aa97dd1cSKonstantin Ananyevstruct tle_bl_port {
61aa97dd1cSKonstantin Ananyev	uint32_t nb_port;     /**< number of blocked ports. */
62aa97dd1cSKonstantin Ananyev	const uint16_t *port; /**< list of blocked ports. */
63aa97dd1cSKonstantin Ananyev};
64aa97dd1cSKonstantin Ananyev
65aa97dd1cSKonstantin Ananyev
66aa97dd1cSKonstantin Ananyev/**
67aa97dd1cSKonstantin Ananyev * device parameters.
68aa97dd1cSKonstantin Ananyev */
69aa97dd1cSKonstantin Ananyevstruct tle_dev_param {
705c795f7bSKonstantin Ananyev	uint64_t rx_offload; /**< DEV_RX_OFFLOAD_* supported. */
715c795f7bSKonstantin Ananyev	uint64_t tx_offload; /**< DEV_TX_OFFLOAD_* supported. */
72aa97dd1cSKonstantin Ananyev	struct in_addr local_addr4;  /**< local IPv4 address assigned. */
73aa97dd1cSKonstantin Ananyev	struct in6_addr local_addr6; /**< local IPv6 address assigned. */
74aa97dd1cSKonstantin Ananyev	struct tle_bl_port bl4; /**< blocked ports for IPv4 address. */
75aa97dd1cSKonstantin Ananyev	struct tle_bl_port bl6; /**< blocked ports for IPv4 address. */
76aa97dd1cSKonstantin Ananyev};
77aa97dd1cSKonstantin Ananyev
78aa97dd1cSKonstantin Ananyev#define TLE_DST_MAX_HDR	0x60
79aa97dd1cSKonstantin Ananyev
80aa97dd1cSKonstantin Ananyevstruct tle_dest {
81aa97dd1cSKonstantin Ananyev	struct rte_mempool *head_mp;
82aa97dd1cSKonstantin Ananyev	/**< MP for fragment headers and control packets. */
837e18fa1bSKonstantin Ananyev	struct tle_dev *dev;    /**< device to send packets through. */
847e18fa1bSKonstantin Ananyev	uint64_t ol_flags;      /**< tx ofload flags. */
857e18fa1bSKonstantin Ananyev	uint16_t mtu;           /**< MTU for given destination. */
86aa97dd1cSKonstantin Ananyev	uint8_t l2_len;  /**< L2 header length. */
87aa97dd1cSKonstantin Ananyev	uint8_t l3_len;  /**< L3 header length. */
88aa97dd1cSKonstantin Ananyev	uint8_t hdr[TLE_DST_MAX_HDR]; /**< L2/L3 headers. */
89aa97dd1cSKonstantin Ananyev};
90aa97dd1cSKonstantin Ananyev
91aa97dd1cSKonstantin Ananyev/**
92aa97dd1cSKonstantin Ananyev * context creation parameters.
93aa97dd1cSKonstantin Ananyev */
94aa97dd1cSKonstantin Ananyev
95aa97dd1cSKonstantin Ananyevenum {
96aa97dd1cSKonstantin Ananyev	TLE_PROTO_UDP,
97aa97dd1cSKonstantin Ananyev	TLE_PROTO_TCP,
98aa97dd1cSKonstantin Ananyev	TLE_PROTO_NUM
99aa97dd1cSKonstantin Ananyev};
100aa97dd1cSKonstantin Ananyev
1019fa82a63SReshma Pattanenum {
1029fa82a63SReshma Pattan	TLE_JHASH,
1039fa82a63SReshma Pattan	TLE_SIPHASH,
1049fa82a63SReshma Pattan	TLE_HASH_NUM
1059fa82a63SReshma Pattan};
1069fa82a63SReshma Pattan
1077e18fa1bSKonstantin Ananyevenum {
1087e18fa1bSKonstantin Ananyev	TLE_CTX_FLAG_ST = 1,  /**< ctx will be used by single thread */
1097e18fa1bSKonstantin Ananyev};
1107e18fa1bSKonstantin Ananyev
111aa97dd1cSKonstantin Ananyevstruct tle_ctx_param {
112aa97dd1cSKonstantin Ananyev	int32_t socket_id;         /**< socket ID to allocate memory for. */
113aa97dd1cSKonstantin Ananyev	uint32_t proto;            /**< L4 proto to handle. */
114aa97dd1cSKonstantin Ananyev	uint32_t max_streams;      /**< max number of streams in context. */
115b8f1ef2bSKonstantin Ananyev	struct {
116b8f1ef2bSKonstantin Ananyev		uint32_t min;
117b8f1ef2bSKonstantin Ananyev		/**< min number of free streams (grow threshold). */
118b8f1ef2bSKonstantin Ananyev		uint32_t max;
119b8f1ef2bSKonstantin Ananyev		/**< max number of free streams (shrink threshold). */
120b8f1ef2bSKonstantin Ananyev	} free_streams;
121aa97dd1cSKonstantin Ananyev	uint32_t max_stream_rbufs; /**< max recv mbufs per stream. */
122aa97dd1cSKonstantin Ananyev	uint32_t max_stream_sbufs; /**< max send mbufs per stream. */
123aa97dd1cSKonstantin Ananyev	uint32_t send_bulk_size;   /**< expected # of packets per send call. */
1247e18fa1bSKonstantin Ananyev	uint32_t flags;            /**< specific flags */
125aa97dd1cSKonstantin Ananyev
126aa97dd1cSKonstantin Ananyev	int (*lookup4)(void *opaque, const struct in_addr *addr,
127aa97dd1cSKonstantin Ananyev		struct tle_dest *res);
128aa97dd1cSKonstantin Ananyev	/**< will be called by send() to get IPv4 packet destination info. */
129aa97dd1cSKonstantin Ananyev	void *lookup4_data;
130aa97dd1cSKonstantin Ananyev	/**< opaque data pointer for lookup4() callback. */
131aa97dd1cSKonstantin Ananyev
132aa97dd1cSKonstantin Ananyev	int (*lookup6)(void *opaque, const struct in6_addr *addr,
133aa97dd1cSKonstantin Ananyev		struct tle_dest *res);
134aa97dd1cSKonstantin Ananyev	/**< will be called by send() to get IPv6 packet destination info. */
135aa97dd1cSKonstantin Ananyev	void *lookup6_data;
136aa97dd1cSKonstantin Ananyev	/**< opaque data pointer for lookup6() callback. */
1379fa82a63SReshma Pattan
1389fa82a63SReshma Pattan	uint32_t hash_alg;
1399fa82a63SReshma Pattan	/**< hash algorithm to be used to generate sequence number. */
1409fa82a63SReshma Pattan	rte_xmm_t secret_key;
1419fa82a63SReshma Pattan	/**< secret key to be used to calculate the hash. */
1427e18fa1bSKonstantin Ananyev
1437e18fa1bSKonstantin Ananyev	uint32_t icw; /**< initial congestion window, default is 2*MSS if 0. */
1447e18fa1bSKonstantin Ananyev	uint32_t timewait;
1457e18fa1bSKonstantin Ananyev	/**< TCP TIME_WAIT state timeout duration in milliseconds,
1467e18fa1bSKonstantin Ananyev	 * default 2MSL, if UINT32_MAX */
147aa97dd1cSKonstantin Ananyev};
148aa97dd1cSKonstantin Ananyev
1497e18fa1bSKonstantin Ananyev/**
1507e18fa1bSKonstantin Ananyev * use default TIMEWAIT timeout value.
1517e18fa1bSKonstantin Ananyev */
1527e18fa1bSKonstantin Ananyev#define	TLE_TCP_TIMEWAIT_DEFAULT	UINT32_MAX
1537e18fa1bSKonstantin Ananyev
154aa97dd1cSKonstantin Ananyev/**
155aa97dd1cSKonstantin Ananyev * create L4 processing context.
156aa97dd1cSKonstantin Ananyev * @param ctx_prm
157aa97dd1cSKonstantin Ananyev *   Parameters used to create and initialise the L4 context.
158aa97dd1cSKonstantin Ananyev * @return
159aa97dd1cSKonstantin Ananyev *   Pointer to context structure that can be used in future operations,
160aa97dd1cSKonstantin Ananyev *   or NULL on error, with error code set in rte_errno.
161aa97dd1cSKonstantin Ananyev *
162aa97dd1cSKonstantin Ananyev *   Possible rte_errno errors include:
163aa97dd1cSKonstantin Ananyev *   - EINVAL - invalid parameter passed to function
164aa97dd1cSKonstantin Ananyev *   - ENOMEM - out of memory
165aa97dd1cSKonstantin Ananyev */
166aa97dd1cSKonstantin Ananyevstruct tle_ctx *
167aa97dd1cSKonstantin Ananyevtle_ctx_create(const struct tle_ctx_param *ctx_prm);
168aa97dd1cSKonstantin Ananyev
169aa97dd1cSKonstantin Ananyev/**
170aa97dd1cSKonstantin Ananyev * Destroy given context.
171aa97dd1cSKonstantin Ananyev *
172aa97dd1cSKonstantin Ananyev * @param ctx
173aa97dd1cSKonstantin Ananyev *   context to destroy
174aa97dd1cSKonstantin Ananyev */
175aa97dd1cSKonstantin Ananyevvoid tle_ctx_destroy(struct tle_ctx *ctx);
176aa97dd1cSKonstantin Ananyev
177aa97dd1cSKonstantin Ananyev/**
178aa97dd1cSKonstantin Ananyev * Add new device into the given context.
179aa97dd1cSKonstantin Ananyev * This function is not multi-thread safe.
180aa97dd1cSKonstantin Ananyev *
181aa97dd1cSKonstantin Ananyev * @param ctx
182aa97dd1cSKonstantin Ananyev *   context to add new device into.
183aa97dd1cSKonstantin Ananyev * @param dev_prm
184aa97dd1cSKonstantin Ananyev *   Parameters used to create and initialise new device inside the context.
185aa97dd1cSKonstantin Ananyev * @return
186aa97dd1cSKonstantin Ananyev *   Pointer to device structure that can be used in future operations,
187aa97dd1cSKonstantin Ananyev *   or NULL on error, with error code set in rte_errno.
188aa97dd1cSKonstantin Ananyev *   Possible rte_errno errors include:
189aa97dd1cSKonstantin Ananyev *   - EINVAL - invalid parameter passed to function
190aa97dd1cSKonstantin Ananyev *   - ENODEV - max possible value of open devices is reached
191aa97dd1cSKonstantin Ananyev *   - ENOMEM - out of memory
192aa97dd1cSKonstantin Ananyev */
193aa97dd1cSKonstantin Ananyevstruct tle_dev *
194aa97dd1cSKonstantin Ananyevtle_add_dev(struct tle_ctx *ctx, const struct tle_dev_param *dev_prm);
195aa97dd1cSKonstantin Ananyev
196aa97dd1cSKonstantin Ananyev/**
197aa97dd1cSKonstantin Ananyev * Remove and destroy previously added device from the given context.
198aa97dd1cSKonstantin Ananyev * This function is not multi-thread safe.
199aa97dd1cSKonstantin Ananyev *
200aa97dd1cSKonstantin Ananyev * @param dev
201aa97dd1cSKonstantin Ananyev *   device to remove and destroy.
202aa97dd1cSKonstantin Ananyev * @return
203aa97dd1cSKonstantin Ananyev *   zero on successful completion.
204aa97dd1cSKonstantin Ananyev *   - -EINVAL - invalid parameter passed to function
205aa97dd1cSKonstantin Ananyev */
206aa97dd1cSKonstantin Ananyevint tle_del_dev(struct tle_dev *dev);
207aa97dd1cSKonstantin Ananyev
208aa97dd1cSKonstantin Ananyev/**
209aa97dd1cSKonstantin Ananyev * Flags to the context that destinations info might be changed,
210aa97dd1cSKonstantin Ananyev * so if it has any destinations data cached, then
211aa97dd1cSKonstantin Ananyev * it has to be invalidated.
212aa97dd1cSKonstantin Ananyev * @param ctx
213aa97dd1cSKonstantin Ananyev *   context to invalidate.
214aa97dd1cSKonstantin Ananyev */
215aa97dd1cSKonstantin Ananyevvoid tle_ctx_invalidate(struct tle_ctx *ctx);
216aa97dd1cSKonstantin Ananyev
217aa97dd1cSKonstantin Ananyev/**
218aa97dd1cSKonstantin Ananyev * Stream asynchronous notification mechanisms:
219aa97dd1cSKonstantin Ananyev * a) recv/send callback.
220aa97dd1cSKonstantin Ananyev * Stream recv/send notification callbacks behaviour is edge-triggered (ET).
221aa97dd1cSKonstantin Ananyev * recv callback will be invoked if stream receive buffer was empty and
222aa97dd1cSKonstantin Ananyev * new packet(s) have arrived.
223aa97dd1cSKonstantin Ananyev * send callback will be invoked when stream send buffer was full,
224aa97dd1cSKonstantin Ananyev * and some packets belonging to that stream were sent
225aa97dd1cSKonstantin Ananyev * (part of send buffer became free again).
226aa97dd1cSKonstantin Ananyev * Note that both recv and send callbacks are called with sort of read lock
227aa97dd1cSKonstantin Ananyev * held on that stream. So it is not permitted to call stream_close()
228aa97dd1cSKonstantin Ananyev * within the callback function. Doing that would cause a deadlock.
229aa97dd1cSKonstantin Ananyev * While it is allowed to call stream send/recv functions within the
230aa97dd1cSKonstantin Ananyev * callback, it is not recommended: callback function will be invoked
231aa97dd1cSKonstantin Ananyev * within tle_udp_rx_bulk/tle_udp_tx_bulk context and some heavy processing
232aa97dd1cSKonstantin Ananyev * within the callback functions might cause performance degradation
233aa97dd1cSKonstantin Ananyev * or even loss of packets for further streams.
234aa97dd1cSKonstantin Ananyev * b) recv/send event.
235aa97dd1cSKonstantin Ananyev * Stream recv/send events behaviour is level-triggered (LT).
236aa97dd1cSKonstantin Ananyev * receive event will be raised by either
237aa97dd1cSKonstantin Ananyev * tle_udp_rx_burst() or tle_udp_stream_recv() as long as there are any
238aa97dd1cSKonstantin Ananyev * remaining packets inside stream receive buffer.
239aa97dd1cSKonstantin Ananyev * send event will be raised by either
240aa97dd1cSKonstantin Ananyev * tle_udp_tx_burst() or tle_udp_stream_send() as long as there are any
241aa97dd1cSKonstantin Ananyev * free space inside stream send buffer.
242aa97dd1cSKonstantin Ananyev * Note that callback and event are mutually exclusive on <stream, op> basis.
243aa97dd1cSKonstantin Ananyev * It is not possible to  open a stream with both recv event and callback
244aa97dd1cSKonstantin Ananyev * specified.
245aa97dd1cSKonstantin Ananyev * Though it is possible to open a stream with recv callback and send event,
246aa97dd1cSKonstantin Ananyev * or visa-versa.
247aa97dd1cSKonstantin Ananyev * If the user doesn't need any notification mechanism for that stream,
248aa97dd1cSKonstantin Ananyev * both event and callback could be set to zero.
249aa97dd1cSKonstantin Ananyev */
250aa97dd1cSKonstantin Ananyev
251aa97dd1cSKonstantin Ananyevstruct tle_event;
252aa97dd1cSKonstantin Ananyevstruct tle_stream;
253aa97dd1cSKonstantin Ananyev
254aa97dd1cSKonstantin Ananyev/**
255aa97dd1cSKonstantin Ananyev * Stream recv/send callback function and data.
256aa97dd1cSKonstantin Ananyev */
257aa97dd1cSKonstantin Ananyevstruct tle_stream_cb {
258aa97dd1cSKonstantin Ananyev	void (*func)(void *, struct tle_stream *);
259aa97dd1cSKonstantin Ananyev	void *data;
260aa97dd1cSKonstantin Ananyev};
261aa97dd1cSKonstantin Ananyev
262aa97dd1cSKonstantin Ananyev#ifdef __cplusplus
263aa97dd1cSKonstantin Ananyev}
264aa97dd1cSKonstantin Ananyev#endif
265aa97dd1cSKonstantin Ananyev
266aa97dd1cSKonstantin Ananyev#endif /* _TLE_CTX_H_ */
267