configure: Silence check for broken strtok_r().
[sliver-openvswitch.git] / lib / dpif-provider.h
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef DPIF_PROVIDER_H
18 #define DPIF_PROVIDER_H 1
19
20 /* Provider interface to dpifs, which provide an interface to an Open vSwitch
21  * datapath. */
22
23 #include <assert.h>
24 #include "dpif.h"
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
30 /* Open vSwitch datapath interface.
31  *
32  * This structure should be treated as opaque by dpif implementations. */
33 struct dpif {
34     const struct dpif_class *dpif_class;
35     char *name;
36     uint8_t netflow_engine_type;
37     uint8_t netflow_engine_id;
38 };
39
40 void dpif_init(struct dpif *, const struct dpif_class *, const char *name,
41                uint8_t netflow_engine_type, uint8_t netflow_engine_id);
42 static inline void dpif_assert_class(const struct dpif *dpif,
43                                      const struct dpif_class *dpif_class)
44 {
45     assert(dpif->dpif_class == dpif_class);
46 }
47
48 /* Datapath interface class structure, to be defined by each implementation of
49  * a datapath interface.
50  *
51  * These functions return 0 if successful or a positive errno value on failure,
52  * except where otherwise noted.
53  *
54  * These functions are expected to execute synchronously, that is, to block as
55  * necessary to obtain a result.  Thus, they may not return EAGAIN or
56  * EWOULDBLOCK or EINPROGRESS.  We may relax this requirement in the future if
57  * and when we encounter performance problems. */
58 struct dpif_class {
59     /* Prefix for names of dpifs in this class, e.g. "netdev:".
60      *
61      * One dpif class may have the empty string "" as its prefix, in which case
62      * that dpif class is associated with dpif names that don't match any other
63      * class name. */
64     const char *prefix;
65
66     /* Class name, for use in error messages. */
67     const char *name;
68
69     /* Performs periodic work needed by dpifs of this class, if any is
70      * necessary. */
71     void (*run)(void);
72
73     /* Arranges for poll_block() to wake up if the "run" member function needs
74      * to be called. */
75     void (*wait)(void);
76
77     /* Enumerates the names of all known created datapaths, if possible, into
78      * 'all_dps'.  The caller has already initialized 'all_dps' and other dpif
79      * classes might already have added names to it.
80      *
81      * This is used by the vswitch at startup, so that it can delete any
82      * datapaths that are not configured.
83      *
84      * Some kinds of datapaths might not be practically enumerable, in which
85      * case this function may be a null pointer. */
86     int (*enumerate)(struct svec *all_dps);
87
88     /* Attempts to open an existing dpif, if 'create' is false, or to open an
89      * existing dpif or create a new one, if 'create' is true.  'name' is the
90      * full dpif name provided by the user, e.g. "udatapath:/var/run/mypath".
91      * This name is useful for error messages but must not be modified.
92      *
93      * 'suffix' is a copy of 'name' following the dpif's 'prefix'.
94      *
95      * If successful, stores a pointer to the new dpif in '*dpifp'.  On failure
96      * there are no requirements on what is stored in '*dpifp'. */
97     int (*open)(const char *name, char *suffix, bool create,
98                 struct dpif **dpifp);
99
100     /* Closes 'dpif' and frees associated memory. */
101     void (*close)(struct dpif *dpif);
102
103     /* Enumerates all names that may be used to open 'dpif' into 'all_names'.
104      * The Linux datapath, for example, supports opening a datapath both by
105      * number, e.g. "dp0", and by the name of the datapath's local port.  For
106      * some datapaths, this might be an infinite set (e.g. in a file name,
107      * slashes may be duplicated any number of times), in which case only the
108      * names most likely to be used should be enumerated.
109      *
110      * The caller has already initialized 'all_names' and might already have
111      * added some names to it.  This function should not disturb any existing
112      * names in 'all_names'.
113      *
114      * If a datapath class does not support multiple names for a datapath, this
115      * function may be a null pointer.
116      *
117      * This is used by the vswitch at startup, */
118     int (*get_all_names)(const struct dpif *dpif, struct svec *all_names);
119
120     /* Attempts to destroy the dpif underlying 'dpif'.
121      *
122      * If successful, 'dpif' will not be used again except as an argument for
123      * the 'close' member function. */
124     int (*destroy)(struct dpif *dpif);
125
126     /* Retrieves statistics for 'dpif' into 'stats'. */
127     int (*get_stats)(const struct dpif *dpif, struct odp_stats *stats);
128
129     /* Retrieves 'dpif''s current treatment of IP fragments into '*drop_frags':
130      * true indicates that fragments are dropped, false indicates that
131      * fragments are treated in the same way as other IP packets (except that
132      * the L4 header cannot be read). */
133     int (*get_drop_frags)(const struct dpif *dpif, bool *drop_frags);
134
135     /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose
136      * meaning is the same as for the get_drop_frags member function. */
137     int (*set_drop_frags)(struct dpif *dpif, bool drop_frags);
138
139     /* Creates a new port in 'dpif' connected to network device 'devname'.
140      * 'flags' is a set of ODP_PORT_* flags.  If successful, sets '*port_no'
141      * to the new port's port number. */
142     int (*port_add)(struct dpif *dpif, const char *devname, uint16_t flags,
143                     uint16_t *port_no);
144
145     /* Removes port numbered 'port_no' from 'dpif'. */
146     int (*port_del)(struct dpif *dpif, uint16_t port_no);
147
148     /* Queries 'dpif' for a port with the given 'port_no' or 'devname'.  Stores
149      * information about the port into '*port' if successful. */
150     int (*port_query_by_number)(const struct dpif *dpif, uint16_t port_no,
151                                 struct odp_port *port);
152     int (*port_query_by_name)(const struct dpif *dpif, const char *devname,
153                               struct odp_port *port);
154
155     /* Stores in 'ports' information about up to 'n' ports attached to 'dpif',
156      * in no particular order.  Returns the number of ports attached to 'dpif'
157      * (not the number stored), if successful, otherwise a negative errno
158      * value. */
159     int (*port_list)(const struct dpif *dpif, struct odp_port *ports, int n);
160
161     /* Polls for changes in the set of ports in 'dpif'.  If the set of ports in
162      * 'dpif' has changed, then this function should do one of the
163      * following:
164      *
165      * - Preferably: store the name of the device that was added to or deleted
166      *   from 'dpif' in '*devnamep' and return 0.  The caller is responsible
167      *   for freeing '*devnamep' (with free()) when it no longer needs it.
168      *
169      * - Alternatively: return ENOBUFS, without indicating the device that was
170      *   added or deleted.
171      *
172      * Occasional 'false positives', in which the function returns 0 while
173      * indicating a device that was not actually added or deleted or returns
174      * ENOBUFS without any change, are acceptable.
175      *
176      * If the set of ports in 'dpif' has not changed, returns EAGAIN.  May also
177      * return other positive errno values to indicate that something has gone
178      * wrong. */
179     int (*port_poll)(const struct dpif *dpif, char **devnamep);
180
181     /* Arranges for the poll loop to wake up when 'port_poll' will return a
182      * value other than EAGAIN. */
183     void (*port_poll_wait)(const struct dpif *dpif);
184
185     /* Stores in 'ports' the port numbers of up to 'n' ports that belong to
186      * 'group' in 'dpif'.  Returns the number of ports in 'group' (not the
187      * number stored), if successful, otherwise a negative errno value. */
188     int (*port_group_get)(const struct dpif *dpif, int group,
189                           uint16_t ports[], int n);
190
191     /* Changes port group 'group' in 'dpif' to consist of the 'n' ports whose
192      * numbers are given in 'ports'.
193      *
194      * Use the get_stats member function to obtain the number of supported port
195      * groups. */
196     int (*port_group_set)(struct dpif *dpif, int group,
197                           const uint16_t ports[], int n);
198
199     /* For each flow 'flow' in the 'n' flows in 'flows':
200      *
201      * - If a flow matching 'flow->key' exists in 'dpif':
202      *
203      *     Stores 0 into 'flow->stats.error' and stores statistics for the flow
204      *     into 'flow->stats'.
205      *
206      *     If 'flow->n_actions' is zero, then 'flow->actions' is ignored.  If
207      *     'flow->n_actions' is nonzero, then 'flow->actions' should point to
208      *     an array of the specified number of actions.  At most that many of
209      *     the flow's actions will be copied into that array.
210      *     'flow->n_actions' will be updated to the number of actions actually
211      *     present in the flow, which may be greater than the number stored if
212      *     the flow has more actions than space available in the array.
213      *
214      * - Flow-specific errors are indicated by a positive errno value in
215      *   'flow->stats.error'.  In particular, ENOENT indicates that no flow
216      *   matching 'flow->key' exists in 'dpif'.  When an error value is stored,
217      *   the contents of 'flow->key' are preserved but other members of 'flow'
218      *   should be treated as indeterminate.
219      *
220      * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
221      * individually successful or not is indicated by 'flow->stats.error',
222      * however).  Returns a positive errno value if an error that prevented
223      * this update occurred, in which the caller must not depend on any
224      * elements in 'flows' being updated or not updated.
225      */
226     int (*flow_get)(const struct dpif *dpif, struct odp_flow flows[], int n);
227
228     /* Adds or modifies a flow in 'dpif' as specified in 'put':
229      *
230      * - If the flow specified in 'put->flow' does not exist in 'dpif', then
231      *   behavior depends on whether ODPPF_CREATE is specified in 'put->flags':
232      *   if it is, the flow will be added, otherwise the operation will fail
233      *   with ENOENT.
234      *
235      * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'.
236      *   Behavior in this case depends on whether ODPPF_MODIFY is specified in
237      *   'put->flags': if it is, the flow's actions will be updated, otherwise
238      *   the operation will fail with EEXIST.  If the flow's actions are
239      *   updated, then its statistics will be zeroed if ODPPF_ZERO_STATS is set
240      *   in 'put->flags', left as-is otherwise.
241      */
242     int (*flow_put)(struct dpif *dpif, struct odp_flow_put *put);
243
244     /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if
245      * 'dpif' does not contain such a flow.
246      *
247      * If successful, updates 'flow->stats', 'flow->n_actions', and
248      * 'flow->actions' as described in more detail under the flow_get member
249      * function below. */
250     int (*flow_del)(struct dpif *dpif, struct odp_flow *flow);
251
252     /* Deletes all flows from 'dpif' and clears all of its queues of received
253      * packets. */
254     int (*flow_flush)(struct dpif *dpif);
255
256     /* Stores up to 'n' flows in 'dpif' into 'flows', updating their statistics
257      * and actions as described under the flow_get member function.  If
258      * successful, returns the number of flows actually present in 'dpif',
259      * which might be greater than the number stored (if 'dpif' has more than
260      * 'n' flows).  On failure, returns a negative errno value. */
261     int (*flow_list)(const struct dpif *dpif, struct odp_flow flows[], int n);
262
263     /* Performs the 'n_actions' actions in 'actions' on the Ethernet frame
264      * specified in 'packet'.
265      *
266      * Pretends that the frame was originally received on the port numbered
267      * 'in_port'.  This affects only ODPAT_OUTPUT_GROUP actions, which will not
268      * send a packet out their input port.  Specify the number of an unused
269      * port (e.g. UINT16_MAX is currently always unused) to avoid this
270      * behavior. */
271     int (*execute)(struct dpif *dpif, uint16_t in_port,
272                    const union odp_action actions[], int n_actions,
273                    const struct ofpbuf *packet);
274
275     /* Retrieves 'dpif''s "listen mask" into '*listen_mask'.  Each ODPL_* bit
276      * set in '*listen_mask' indicates the 'dpif' will receive messages of the
277      * corresponding type when it calls the recv member function. */
278     int (*recv_get_mask)(const struct dpif *dpif, int *listen_mask);
279
280     /* Sets 'dpif''s "listen mask" to 'listen_mask'.  Each ODPL_* bit set in
281      * 'listen_mask' indicates the 'dpif' will receive messages of the
282      * corresponding type when it calls the recv member function. */
283     int (*recv_set_mask)(struct dpif *dpif, int listen_mask);
284
285     /* Retrieves 'dpif''s sFlow sampling probability into '*probability'.
286      * Return value is 0 or a positive errno value.  EOPNOTSUPP indicates that
287      * the datapath does not support sFlow, as does a null pointer.
288      *
289      * '*probability' is expressed as the number of packets out of UINT_MAX to
290      * sample, e.g. probability/UINT_MAX is the probability of sampling a given
291      * packet. */
292     int (*get_sflow_probability)(const struct dpif *dpif,
293                                  uint32_t *probability);
294
295     /* Sets 'dpif''s sFlow sampling probability to 'probability'.  Return value
296      * is 0 or a positive errno value.  EOPNOTSUPP indicates that the datapath
297      * does not support sFlow, as does a null pointer.
298      *
299      * 'probability' is expressed as the number of packets out of UINT_MAX to
300      * sample, e.g. probability/UINT_MAX is the probability of sampling a given
301      * packet. */
302     int (*set_sflow_probability)(struct dpif *dpif, uint32_t probability);
303
304     /* Attempts to receive a message from 'dpif'.  If successful, stores the
305      * message into '*packetp'.  The message, if one is received, must begin
306      * with 'struct odp_msg' as a header.  Only messages of the types selected
307      * with the set_listen_mask member function should be received.
308      *
309      * This function must not block.  If no message is ready to be received
310      * when it is called, it should return EAGAIN without blocking. */
311     int (*recv)(struct dpif *dpif, struct ofpbuf **packetp);
312
313     /* Arranges for the poll loop to wake up when 'dpif' has a message queued
314      * to be received with the recv member function. */
315     void (*recv_wait)(struct dpif *dpif);
316 };
317
318 extern const struct dpif_class dpif_linux_class;
319 extern const struct dpif_class dpif_netdev_class;
320
321 #ifdef  __cplusplus
322 }
323 #endif
324
325 #endif /* dpif-provider.h */