distributor: enhance and fix tag matching
[dpdk.git] / lib / librte_distributor / rte_distributor.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_DISTRIBUTE_H_
35 #define _RTE_DISTRIBUTE_H_
36
37 /**
38  * @file
39  * RTE distributor
40  *
41  * The distributor is a component which is designed to pass packets
42  * one-at-a-time to workers, with dynamic load balancing.
43  */
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include <rte_mbuf.h>
50
51 #define RTE_DISTRIBUTOR_NAMESIZE 32 /**< Length of name for instance */
52
53 struct rte_distributor;
54
55 /**
56  * Function to create a new distributor instance
57  *
58  * Reserves the memory needed for the distributor operation and
59  * initializes the distributor to work with the configured number of workers.
60  *
61  * @param name
62  *   The name to be given to the distributor instance.
63  * @param socket_id
64  *   The NUMA node on which the memory is to be allocated
65  * @param num_workers
66  *   The maximum number of workers that will request packets from this
67  *   distributor
68  * @return
69  *   The newly created distributor instance
70  */
71 struct rte_distributor *
72 rte_distributor_create(const char *name, unsigned socket_id,
73                 unsigned num_workers);
74
75 /*  *** APIS to be called on the distributor lcore ***  */
76 /*
77  * The following APIs are the public APIs which are designed for use on a
78  * single lcore which acts as the distributor lcore for a given distributor
79  * instance. These functions cannot be called on multiple cores simultaneously
80  * without using locking to protect access to the internals of the distributor.
81  *
82  * NOTE: a given lcore cannot act as both a distributor lcore and a worker lcore
83  * for the same distributor instance, otherwise deadlock will result.
84  */
85
86 /**
87  * Process a set of packets by distributing them among workers that request
88  * packets. The distributor will ensure that no two packets that have the
89  * same flow id, or tag, in the mbuf will be procesed at the same time.
90  *
91  * The user is advocated to set tag for each mbuf before calling this function.
92  * If user doesn't set the tag, the tag value can be various values depending on
93  * driver implementation and configuration.
94  *
95  * This is not multi-thread safe and should only be called on a single lcore.
96  *
97  * @param d
98  *   The distributor instance to be used
99  * @param mbufs
100  *   The mbufs to be distributed
101  * @param num_mbufs
102  *   The number of mbufs in the mbufs array
103  * @return
104  *   The number of mbufs processed.
105  */
106 int
107 rte_distributor_process(struct rte_distributor *d,
108                 struct rte_mbuf **mbufs, unsigned num_mbufs);
109
110 /**
111  * Get a set of mbufs that have been returned to the distributor by workers
112  *
113  * This should only be called on the same lcore as rte_distributor_process()
114  *
115  * @param d
116  *   The distributor instance to be used
117  * @param mbufs
118  *   The mbufs pointer array to be filled in
119  * @param max_mbufs
120  *   The size of the mbufs array
121  * @return
122  *   The number of mbufs returned in the mbufs array.
123  */
124 int
125 rte_distributor_returned_pkts(struct rte_distributor *d,
126                 struct rte_mbuf **mbufs, unsigned max_mbufs);
127
128 /**
129  * Flush the distributor component, so that there are no in-flight or
130  * backlogged packets awaiting processing
131  *
132  * This should only be called on the same lcore as rte_distributor_process()
133  *
134  * @param d
135  *   The distributor instance to be used
136  * @return
137  *   The number of queued/in-flight packets that were completed by this call.
138  */
139 int
140 rte_distributor_flush(struct rte_distributor *d);
141
142 /**
143  * Clears the array of returned packets used as the source for the
144  * rte_distributor_returned_pkts() API call.
145  *
146  * This should only be called on the same lcore as rte_distributor_process()
147  *
148  * @param d
149  *   The distributor instance to be used
150  */
151 void
152 rte_distributor_clear_returns(struct rte_distributor *d);
153
154 /*  *** APIS to be called on the worker lcores ***  */
155 /*
156  * The following APIs are the public APIs which are designed for use on
157  * multiple lcores which act as workers for a distributor. Each lcore should use
158  * a unique worker id when requesting packets.
159  *
160  * NOTE: a given lcore cannot act as both a distributor lcore and a worker lcore
161  * for the same distributor instance, otherwise deadlock will result.
162  */
163
164 /**
165  * API called by a worker to get a new packet to process. Any previous packet
166  * given to the worker is assumed to have completed processing, and may be
167  * optionally returned to the distributor via the oldpkt parameter.
168  *
169  * @param d
170  *   The distributor instance to be used
171  * @param worker_id
172  *   The worker instance number to use - must be less that num_workers passed
173  *   at distributor creation time.
174  * @param oldpkt
175  *   The previous packet, if any, being processed by the worker
176  *
177  * @return
178  *   A new packet to be processed by the worker thread.
179  */
180 struct rte_mbuf *
181 rte_distributor_get_pkt(struct rte_distributor *d,
182                 unsigned worker_id, struct rte_mbuf *oldpkt);
183
184 /**
185  * API called by a worker to return a completed packet without requesting a
186  * new packet, for example, because a worker thread is shutting down
187  *
188  * @param d
189  *   The distributor instance to be used
190  * @param worker_id
191  *   The worker instance number to use - must be less that num_workers passed
192  *   at distributor creation time.
193  * @param mbuf
194  *   The previous packet being processed by the worker
195  */
196 int
197 rte_distributor_return_pkt(struct rte_distributor *d, unsigned worker_id,
198                 struct rte_mbuf *mbuf);
199
200 /**
201  * API called by a worker to request a new packet to process.
202  * Any previous packet given to the worker is assumed to have completed
203  * processing, and may be optionally returned to the distributor via
204  * the oldpkt parameter.
205  * Unlike rte_distributor_get_pkt(), this function does not wait for a new
206  * packet to be provided by the distributor.
207  *
208  * NOTE: after calling this function, rte_distributor_poll_pkt() should
209  * be used to poll for the packet requested. The rte_distributor_get_pkt()
210  * API should *not* be used to try and retrieve the new packet.
211  *
212  * @param d
213  *   The distributor instance to be used
214  * @param worker_id
215  *   The worker instance number to use - must be less that num_workers passed
216  *   at distributor creation time.
217  * @param oldpkt
218  *   The previous packet, if any, being processed by the worker
219  */
220 void
221 rte_distributor_request_pkt(struct rte_distributor *d,
222                 unsigned worker_id, struct rte_mbuf *oldpkt);
223
224 /**
225  * API called by a worker to check for a new packet that was previously
226  * requested by a call to rte_distributor_request_pkt(). It does not wait
227  * for the new packet to be available, but returns NULL if the request has
228  * not yet been fulfilled by the distributor.
229  *
230  * @param d
231  *   The distributor instance to be used
232  * @param worker_id
233  *   The worker instance number to use - must be less that num_workers passed
234  *   at distributor creation time.
235  *
236  * @return
237  *   A new packet to be processed by the worker thread, or NULL if no
238  *   packet is yet available.
239  */
240 struct rte_mbuf *
241 rte_distributor_poll_pkt(struct rte_distributor *d,
242                 unsigned worker_id);
243
244 #ifdef __cplusplus
245 }
246 #endif
247
248 #endif