xref: /dpdk/lib/reorder/rte_reorder.h (revision b4f0a9bb5807a4f0a4904595bb85a9a386696311)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_REORDER_H_
699a2dd95SBruce Richardson #define _RTE_REORDER_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file
1099a2dd95SBruce Richardson  * RTE reorder
1199a2dd95SBruce Richardson  *
1299a2dd95SBruce Richardson  * Reorder library is a component which is designed to
1399a2dd95SBruce Richardson  * provide ordering of out of ordered packets based on
1499a2dd95SBruce Richardson  * sequence number present in mbuf.
1599a2dd95SBruce Richardson  */
1699a2dd95SBruce Richardson 
171094dd94SDavid Marchand #include <rte_compat.h>
1899a2dd95SBruce Richardson #include <rte_mbuf.h>
1999a2dd95SBruce Richardson #include <rte_mbuf_dyn.h>
2099a2dd95SBruce Richardson 
2199a2dd95SBruce Richardson #ifdef __cplusplus
2299a2dd95SBruce Richardson extern "C" {
2399a2dd95SBruce Richardson #endif
2499a2dd95SBruce Richardson 
2599a2dd95SBruce Richardson struct rte_reorder_buffer;
2699a2dd95SBruce Richardson 
2799a2dd95SBruce Richardson typedef uint32_t rte_reorder_seqn_t;
2899a2dd95SBruce Richardson extern int rte_reorder_seqn_dynfield_offset;
2999a2dd95SBruce Richardson 
3099a2dd95SBruce Richardson /**
3199a2dd95SBruce Richardson  * @warning
3299a2dd95SBruce Richardson  * @b EXPERIMENTAL: this API may change without prior notice
3399a2dd95SBruce Richardson  *
3499a2dd95SBruce Richardson  * Read reorder sequence number from mbuf.
3599a2dd95SBruce Richardson  *
3699a2dd95SBruce Richardson  * @param mbuf Structure to read from.
3799a2dd95SBruce Richardson  * @return pointer to reorder sequence number.
3899a2dd95SBruce Richardson  */
3999a2dd95SBruce Richardson __rte_experimental
4099a2dd95SBruce Richardson static inline rte_reorder_seqn_t *
rte_reorder_seqn(struct rte_mbuf * mbuf)4199a2dd95SBruce Richardson rte_reorder_seqn(struct rte_mbuf *mbuf)
4299a2dd95SBruce Richardson {
4399a2dd95SBruce Richardson 	return RTE_MBUF_DYNFIELD(mbuf, rte_reorder_seqn_dynfield_offset,
4499a2dd95SBruce Richardson 		rte_reorder_seqn_t *);
4599a2dd95SBruce Richardson }
4699a2dd95SBruce Richardson 
4799a2dd95SBruce Richardson /**
4899a2dd95SBruce Richardson  * Create a new reorder buffer instance
4999a2dd95SBruce Richardson  *
5099a2dd95SBruce Richardson  * Allocate memory and initialize a new reorder buffer in that
5199a2dd95SBruce Richardson  * memory, returning the reorder buffer pointer to the user
5299a2dd95SBruce Richardson  *
5399a2dd95SBruce Richardson  * @param name
5499a2dd95SBruce Richardson  *   The name to be given to the reorder buffer instance.
5599a2dd95SBruce Richardson  * @param socket_id
5699a2dd95SBruce Richardson  *   The NUMA node on which the memory for the reorder buffer
5799a2dd95SBruce Richardson  *   instance is to be reserved.
5899a2dd95SBruce Richardson  * @param size
5999a2dd95SBruce Richardson  *   Max number of elements that can be stored in the reorder buffer
6099a2dd95SBruce Richardson  * @return
6199a2dd95SBruce Richardson  *   The initialized reorder buffer instance, or NULL on error
6299a2dd95SBruce Richardson  *   On error case, rte_errno will be set appropriately:
6399a2dd95SBruce Richardson  *    - ENOMEM - no appropriate memory area found in which to create memzone
6499a2dd95SBruce Richardson  *    - EINVAL - invalid parameters
6599a2dd95SBruce Richardson  */
6699a2dd95SBruce Richardson struct rte_reorder_buffer *
6799a2dd95SBruce Richardson rte_reorder_create(const char *name, unsigned socket_id, unsigned int size);
6899a2dd95SBruce Richardson 
6999a2dd95SBruce Richardson /**
7099a2dd95SBruce Richardson  * Initializes given reorder buffer instance
7199a2dd95SBruce Richardson  *
7299a2dd95SBruce Richardson  * @param b
7399a2dd95SBruce Richardson  *   Reorder buffer instance to initialize
7499a2dd95SBruce Richardson  * @param bufsize
7599a2dd95SBruce Richardson  *   Size of the reorder buffer
7699a2dd95SBruce Richardson  * @param name
7799a2dd95SBruce Richardson  *   The name to be given to the reorder buffer
7899a2dd95SBruce Richardson  * @param size
7999a2dd95SBruce Richardson  *   Number of elements that can be stored in reorder buffer
8099a2dd95SBruce Richardson  * @return
8199a2dd95SBruce Richardson  *   The initialized reorder buffer instance, or NULL on error
8299a2dd95SBruce Richardson  *   On error case, rte_errno will be set appropriately:
8399a2dd95SBruce Richardson  *    - EINVAL - invalid parameters
84ec87595fSVolodymyr Fialko  *    - ENOMEM - not enough memory to register dynamic field
8599a2dd95SBruce Richardson  */
8699a2dd95SBruce Richardson struct rte_reorder_buffer *
8799a2dd95SBruce Richardson rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
8899a2dd95SBruce Richardson 		const char *name, unsigned int size);
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson /**
9199a2dd95SBruce Richardson  * Find an existing reorder buffer instance
9299a2dd95SBruce Richardson  * and return a pointer to it.
9399a2dd95SBruce Richardson  *
9499a2dd95SBruce Richardson  * @param name
9599a2dd95SBruce Richardson  *   Name of the reorder buffer instance as passed to rte_reorder_create()
9699a2dd95SBruce Richardson  * @return
9799a2dd95SBruce Richardson  *   Pointer to reorder buffer instance or NULL if object not found with rte_errno
9899a2dd95SBruce Richardson  *   set appropriately. Possible rte_errno values include:
9999a2dd95SBruce Richardson  *    - ENOENT - required entry not available to return.
10099a2dd95SBruce Richardson  *    reorder instance list
10199a2dd95SBruce Richardson  */
10299a2dd95SBruce Richardson struct rte_reorder_buffer *
10399a2dd95SBruce Richardson rte_reorder_find_existing(const char *name);
10499a2dd95SBruce Richardson 
10599a2dd95SBruce Richardson /**
10699a2dd95SBruce Richardson  * Reset the given reorder buffer instance with initial values.
10799a2dd95SBruce Richardson  *
10899a2dd95SBruce Richardson  * @param b
10999a2dd95SBruce Richardson  *   Reorder buffer instance which has to be reset
11099a2dd95SBruce Richardson  */
11199a2dd95SBruce Richardson void
11299a2dd95SBruce Richardson rte_reorder_reset(struct rte_reorder_buffer *b);
11399a2dd95SBruce Richardson 
11499a2dd95SBruce Richardson /**
11599a2dd95SBruce Richardson  * Free reorder buffer instance.
11699a2dd95SBruce Richardson  *
11799a2dd95SBruce Richardson  * @param b
118448e01f1SStephen Hemminger  *   Pointer to reorder buffer instance.
119448e01f1SStephen Hemminger  *   If b is NULL, no operation is performed.
12099a2dd95SBruce Richardson  */
12199a2dd95SBruce Richardson void
12299a2dd95SBruce Richardson rte_reorder_free(struct rte_reorder_buffer *b);
12399a2dd95SBruce Richardson 
12499a2dd95SBruce Richardson /**
12599a2dd95SBruce Richardson  * Insert given mbuf in reorder buffer in its correct position
12699a2dd95SBruce Richardson  *
12799a2dd95SBruce Richardson  * The given mbuf is to be reordered relative to other mbufs in the system.
12899a2dd95SBruce Richardson  * The mbuf must contain a sequence number which is then used to place
12999a2dd95SBruce Richardson  * the buffer in the correct position in the reorder buffer. Reordered
13099a2dd95SBruce Richardson  * packets can later be taken from the buffer using the rte_reorder_drain()
13199a2dd95SBruce Richardson  * API.
13299a2dd95SBruce Richardson  *
13399a2dd95SBruce Richardson  * @param b
13499a2dd95SBruce Richardson  *   Reorder buffer where the mbuf has to be inserted.
13599a2dd95SBruce Richardson  * @param mbuf
13699a2dd95SBruce Richardson  *   mbuf of packet that needs to be inserted in reorder buffer.
13799a2dd95SBruce Richardson  * @return
13899a2dd95SBruce Richardson  *   0 on success
13999a2dd95SBruce Richardson  *   -1 on error
14099a2dd95SBruce Richardson  *   On error case, rte_errno will be set appropriately:
14199a2dd95SBruce Richardson  *    - ENOSPC - Cannot move existing mbufs from reorder buffer to accommodate
14299a2dd95SBruce Richardson  *      early mbuf, but it can be accommodated by performing drain and then insert.
14399a2dd95SBruce Richardson  *    - ERANGE - Too early or late mbuf which is vastly out of range of expected
14499a2dd95SBruce Richardson  *      window should be ignored without any handling.
14599a2dd95SBruce Richardson  */
14699a2dd95SBruce Richardson int
14799a2dd95SBruce Richardson rte_reorder_insert(struct rte_reorder_buffer *b, struct rte_mbuf *mbuf);
14899a2dd95SBruce Richardson 
14999a2dd95SBruce Richardson /**
15099a2dd95SBruce Richardson  * Fetch reordered buffers
15199a2dd95SBruce Richardson  *
15299a2dd95SBruce Richardson  * Returns a set of in-order buffers from the reorder buffer structure. Gaps
15399a2dd95SBruce Richardson  * may be present in the sequence numbers of the mbuf if packets have been
15499a2dd95SBruce Richardson  * delayed too long before reaching the reorder window, or have been previously
15599a2dd95SBruce Richardson  * dropped by the system.
15699a2dd95SBruce Richardson  *
15799a2dd95SBruce Richardson  * @param b
15899a2dd95SBruce Richardson  *   Reorder buffer instance from which packets are to be drained
15999a2dd95SBruce Richardson  * @param mbufs
16099a2dd95SBruce Richardson  *   array of mbufs where reordered packets will be inserted from reorder buffer
16199a2dd95SBruce Richardson  * @param max_mbufs
16299a2dd95SBruce Richardson  *   the number of elements in the mbufs array.
16399a2dd95SBruce Richardson  * @return
16499a2dd95SBruce Richardson  *   number of mbuf pointers written to mbufs. 0 <= N < max_mbufs.
16599a2dd95SBruce Richardson  */
16699a2dd95SBruce Richardson unsigned int
16799a2dd95SBruce Richardson rte_reorder_drain(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs,
16899a2dd95SBruce Richardson 		unsigned max_mbufs);
16999a2dd95SBruce Richardson 
1701dbb8485SVolodymyr Fialko /**
1711dbb8485SVolodymyr Fialko  * @warning
1721dbb8485SVolodymyr Fialko  * @b EXPERIMENTAL: this API may change without prior notice
1731dbb8485SVolodymyr Fialko  *
1741dbb8485SVolodymyr Fialko  * Fetch set of reordered packets up to specified sequence number (exclusive).
1751dbb8485SVolodymyr Fialko  *
1761dbb8485SVolodymyr Fialko  * Returns a set of in-order packets from the reorder buffer structure.
1771dbb8485SVolodymyr Fialko  * Gaps may be present since reorder buffer will try to fetch
1781dbb8485SVolodymyr Fialko  * all possible packets up to given sequence number.
1791dbb8485SVolodymyr Fialko  *
1801dbb8485SVolodymyr Fialko  * @param b
1811dbb8485SVolodymyr Fialko  *   Reorder buffer instance from which packets are to be drained.
1821dbb8485SVolodymyr Fialko  * @param mbufs
1831dbb8485SVolodymyr Fialko  *   Array of mbufs where reordered packets will be inserted from reorder buffer.
1841dbb8485SVolodymyr Fialko  * @param max_mbufs
1851dbb8485SVolodymyr Fialko  *   The number of elements in the mbuf array.
1861dbb8485SVolodymyr Fialko  * @param seqn
1871dbb8485SVolodymyr Fialko  *   Sequence number up to which buffer will be drained.
1881dbb8485SVolodymyr Fialko  * @return
1891dbb8485SVolodymyr Fialko  *   Number of mbuf pointers written to mbufs. 0 <= N < max_mbufs.
1901dbb8485SVolodymyr Fialko  */
1911dbb8485SVolodymyr Fialko __rte_experimental
1921dbb8485SVolodymyr Fialko unsigned int
1931dbb8485SVolodymyr Fialko rte_reorder_drain_up_to_seqn(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs,
1941dbb8485SVolodymyr Fialko 		unsigned int max_mbufs, rte_reorder_seqn_t seqn);
1951dbb8485SVolodymyr Fialko 
1961bc9a84dSVolodymyr Fialko /**
1971bc9a84dSVolodymyr Fialko  * @warning
1981bc9a84dSVolodymyr Fialko  * @b EXPERIMENTAL: this API may change without prior notice
1991bc9a84dSVolodymyr Fialko  *
2001bc9a84dSVolodymyr Fialko  * Set minimum sequence number of packet allowed to be buffered.
2011bc9a84dSVolodymyr Fialko  * To successfully set new value,
2021bc9a84dSVolodymyr Fialko  * reorder buffer has to be empty (after create, reset or drain_all).
2031bc9a84dSVolodymyr Fialko  *
2041bc9a84dSVolodymyr Fialko  * @param b
2051bc9a84dSVolodymyr Fialko  *   Empty reorder buffer instance to modify.
2061bc9a84dSVolodymyr Fialko  * @param min_seqn
2071bc9a84dSVolodymyr Fialko  *   New sequence number to set.
2081bc9a84dSVolodymyr Fialko  * @return
2091bc9a84dSVolodymyr Fialko  *   0 on success, a negative value otherwise.
2101bc9a84dSVolodymyr Fialko  */
2111bc9a84dSVolodymyr Fialko __rte_experimental
2121bc9a84dSVolodymyr Fialko unsigned int
2131bc9a84dSVolodymyr Fialko rte_reorder_min_seqn_set(struct rte_reorder_buffer *b, rte_reorder_seqn_t min_seqn);
2141bc9a84dSVolodymyr Fialko 
215*f35f3dcaSVolodymyr Fialko /**
216*f35f3dcaSVolodymyr Fialko  * @warning
217*f35f3dcaSVolodymyr Fialko  * @b EXPERIMENTAL: this API may change without prior notice
218*f35f3dcaSVolodymyr Fialko  *
219*f35f3dcaSVolodymyr Fialko  * Determine the amount of memory needed by the reorder buffer
220*f35f3dcaSVolodymyr Fialko  * to accommodate a given number of elements.
221*f35f3dcaSVolodymyr Fialko  * @see rte_reorder_init()
222*f35f3dcaSVolodymyr Fialko  *
223*f35f3dcaSVolodymyr Fialko  * @param size
224*f35f3dcaSVolodymyr Fialko  *   Number of elements that can be stored in reorder buffer.
225*f35f3dcaSVolodymyr Fialko  * @return
226*f35f3dcaSVolodymyr Fialko  *   Reorder buffer footprint measured in bytes.
227*f35f3dcaSVolodymyr Fialko  */
228*f35f3dcaSVolodymyr Fialko __rte_experimental
229*f35f3dcaSVolodymyr Fialko unsigned int
230*f35f3dcaSVolodymyr Fialko rte_reorder_memory_footprint_get(unsigned int size);
231*f35f3dcaSVolodymyr Fialko 
23299a2dd95SBruce Richardson #ifdef __cplusplus
23399a2dd95SBruce Richardson }
23499a2dd95SBruce Richardson #endif
23599a2dd95SBruce Richardson 
23699a2dd95SBruce Richardson #endif /* _RTE_REORDER_H_ */
237