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