distributor: new packet distributor library
[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  * This is not multi-thread safe and should only be called on a single lcore.
92  *
93  * @param d
94  *   The distributor instance to be used
95  * @param mbufs
96  *   The mbufs to be distributed
97  * @param num_mbufs
98  *   The number of mbufs in the mbufs array
99  * @return
100  *   The number of mbufs processed.
101  */
102 int
103 rte_distributor_process(struct rte_distributor *d,
104                 struct rte_mbuf **mbufs, unsigned num_mbufs);
105
106 /**
107  * Get a set of mbufs that have been returned to the distributor by workers
108  *
109  * This should only be called on the same lcore as rte_distributor_process()
110  *
111  * @param d
112  *   The distributor instance to be used
113  * @param mbufs
114  *   The mbufs pointer array to be filled in
115  * @param max_mbufs
116  *   The size of the mbufs array
117  * @return
118  *   The number of mbufs returned in the mbufs array.
119  */
120 int
121 rte_distributor_returned_pkts(struct rte_distributor *d,
122                 struct rte_mbuf **mbufs, unsigned max_mbufs);
123
124 /**
125  * Flush the distributor component, so that there are no in-flight or
126  * backlogged packets awaiting processing
127  *
128  * This should only be called on the same lcore as rte_distributor_process()
129  *
130  * @param d
131  *   The distributor instance to be used
132  * @return
133  *   The number of queued/in-flight packets that were completed by this call.
134  */
135 int
136 rte_distributor_flush(struct rte_distributor *d);
137
138 /**
139  * Clears the array of returned packets used as the source for the
140  * rte_distributor_returned_pkts() API call.
141  *
142  * This should only be called on the same lcore as rte_distributor_process()
143  *
144  * @param d
145  *   The distributor instance to be used
146  */
147 void
148 rte_distributor_clear_returns(struct rte_distributor *d);
149
150 /*  *** APIS to be called on the worker lcores ***  */
151 /*
152  * The following APIs are the public APIs which are designed for use on
153  * multiple lcores which act as workers for a distributor. Each lcore should use
154  * a unique worker id when requesting packets.
155  *
156  * NOTE: a given lcore cannot act as both a distributor lcore and a worker lcore
157  * for the same distributor instance, otherwise deadlock will result.
158  */
159
160 /**
161  * API called by a worker to get a new packet to process. Any previous packet
162  * given to the worker is assumed to have completed processing, and may be
163  * optionally returned to the distributor via the oldpkt parameter.
164  *
165  * @param d
166  *   The distributor instance to be used
167  * @param worker_id
168  *   The worker instance number to use - must be less that num_workers passed
169  *   at distributor creation time.
170  * @param oldpkt
171  *   The previous packet, if any, being processed by the worker
172  *
173  * @return
174  *   A new packet to be processed by the worker thread.
175  */
176 struct rte_mbuf *
177 rte_distributor_get_pkt(struct rte_distributor *d,
178                 unsigned worker_id, struct rte_mbuf *oldpkt);
179
180 /**
181  * API called by a worker to return a completed packet without requesting a
182  * new packet, for example, because a worker thread is shutting down
183  *
184  * @param d
185  *   The distributor instance to be used
186  * @param worker_id
187  *   The worker instance number to use - must be less that num_workers passed
188  *   at distributor creation time.
189  * @param mbuf
190  *   The previous packet being processed by the worker
191  */
192 int
193 rte_distributor_return_pkt(struct rte_distributor *d, unsigned worker_id,
194                 struct rte_mbuf *mbuf);
195
196 /******************************************/
197
198 #ifdef __cplusplus
199 }
200 #endif
201
202 #endif