Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / lib / dpif-provider.h
1 /*
2  * Copyright (c) 2009, 2010, 2011 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.  A datapath is a collection of physical or virtual ports that are
22  * exposed over OpenFlow as a single switch.  Datapaths and the collections of
23  * ports that they contain may be fixed or dynamic. */
24
25 #include <assert.h>
26 #include "openflow/openflow.h"
27 #include "dpif.h"
28 #include "util.h"
29
30 #ifdef  __cplusplus
31 extern "C" {
32 #endif
33
34 /* Open vSwitch datapath interface.
35  *
36  * This structure should be treated as opaque by dpif implementations. */
37 struct dpif {
38     const struct dpif_class *dpif_class;
39     char *base_name;
40     char *full_name;
41     uint8_t netflow_engine_type;
42     uint8_t netflow_engine_id;
43 };
44
45 void dpif_init(struct dpif *, const struct dpif_class *, const char *name,
46                uint8_t netflow_engine_type, uint8_t netflow_engine_id);
47 void dpif_uninit(struct dpif *dpif, bool close);
48
49 static inline void dpif_assert_class(const struct dpif *dpif,
50                                      const struct dpif_class *dpif_class)
51 {
52     assert(dpif->dpif_class == dpif_class);
53 }
54
55 /* Datapath interface class structure, to be defined by each implementation of
56  * a datapath interface.
57  *
58  * These functions return 0 if successful or a positive errno value on failure,
59  * except where otherwise noted.
60  *
61  * These functions are expected to execute synchronously, that is, to block as
62  * necessary to obtain a result.  Thus, they may not return EAGAIN or
63  * EWOULDBLOCK or EINPROGRESS.  We may relax this requirement in the future if
64  * and when we encounter performance problems. */
65 struct dpif_class {
66     /* Type of dpif in this class, e.g. "system", "netdev", etc.
67      *
68      * One of the providers should supply a "system" type, since this is
69      * the type assumed if no type is specified when opening a dpif. */
70     const char *type;
71
72     /* Performs periodic work needed by dpifs of this class, if any is
73      * necessary. */
74     void (*run)(void);
75
76     /* Arranges for poll_block() to wake up if the "run" member function needs
77      * to be called. */
78     void (*wait)(void);
79
80     /* Enumerates the names of all known created datapaths, if possible, into
81      * 'all_dps'.  The caller has already initialized 'all_dps' and other dpif
82      * classes might already have added names to it.
83      *
84      * This is used by the vswitch at startup, so that it can delete any
85      * datapaths that are not configured.
86      *
87      * Some kinds of datapaths might not be practically enumerable, in which
88      * case this function may be a null pointer. */
89     int (*enumerate)(struct sset *all_dps);
90
91     /* Attempts to open an existing dpif called 'name', if 'create' is false,
92      * or to open an existing dpif or create a new one, if 'create' is true.
93      *
94      * 'dpif_class' is the class of dpif to open.
95      *
96      * If successful, stores a pointer to the new dpif in '*dpifp', which must
97      * have class 'dpif_class'.  On failure there are no requirements on what
98      * is stored in '*dpifp'. */
99     int (*open)(const struct dpif_class *dpif_class,
100                 const char *name, bool create, struct dpif **dpifp);
101
102     /* Closes 'dpif' and frees associated memory. */
103     void (*close)(struct dpif *dpif);
104
105     /* Attempts to destroy the dpif underlying 'dpif'.
106      *
107      * If successful, 'dpif' will not be used again except as an argument for
108      * the 'close' member function. */
109     int (*destroy)(struct dpif *dpif);
110
111     /* Retrieves statistics for 'dpif' into 'stats'. */
112     int (*get_stats)(const struct dpif *dpif, struct odp_stats *stats);
113
114     /* Retrieves 'dpif''s current treatment of IP fragments into '*drop_frags':
115      * true indicates that fragments are dropped, false indicates that
116      * fragments are treated in the same way as other IP packets (except that
117      * the L4 header cannot be read). */
118     int (*get_drop_frags)(const struct dpif *dpif, bool *drop_frags);
119
120     /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose
121      * meaning is the same as for the get_drop_frags member function. */
122     int (*set_drop_frags)(struct dpif *dpif, bool drop_frags);
123
124     /* Adds 'netdev' as a new port in 'dpif'.  If successful, sets '*port_no'
125      * to the new port's port number. */
126     int (*port_add)(struct dpif *dpif, struct netdev *netdev,
127                     uint16_t *port_no);
128
129     /* Removes port numbered 'port_no' from 'dpif'. */
130     int (*port_del)(struct dpif *dpif, uint16_t port_no);
131
132     /* Queries 'dpif' for a port with the given 'port_no' or 'devname'.  Stores
133      * information about the port into '*port' if successful.
134      *
135      * The caller takes ownership of data in 'port' and must free it with
136      * dpif_port_destroy() when it is no longer needed. */
137     int (*port_query_by_number)(const struct dpif *dpif, uint16_t port_no,
138                                 struct dpif_port *port);
139     int (*port_query_by_name)(const struct dpif *dpif, const char *devname,
140                               struct dpif_port *port);
141
142     /* Returns one greater than the largest port number accepted in flow
143      * actions. */
144     int (*get_max_ports)(const struct dpif *dpif);
145
146     /* Attempts to begin dumping the ports in a dpif.  On success, returns 0
147      * and initializes '*statep' with any data needed for iteration.  On
148      * failure, returns a positive errno value. */
149     int (*port_dump_start)(const struct dpif *dpif, void **statep);
150
151     /* Attempts to retrieve another port from 'dpif' for 'state', which was
152      * initialized by a successful call to the 'port_dump_start' function for
153      * 'dpif'.  On success, stores a new dpif_port into 'port' and returns 0.
154      * Returns EOF if the end of the port table has been reached, or a positive
155      * errno value on error.  This function will not be called again once it
156      * returns nonzero once for a given iteration (but the 'port_dump_done'
157      * function will be called afterward).
158      *
159      * The dpif provider retains ownership of the data stored in 'port'.  It
160      * must remain valid until at least the next call to 'port_dump_next' or
161      * 'port_dump_done' for 'state'. */
162     int (*port_dump_next)(const struct dpif *dpif, void *state,
163                           struct dpif_port *port);
164
165     /* Releases resources from 'dpif' for 'state', which was initialized by a
166      * successful call to the 'port_dump_start' function for 'dpif'.  */
167     int (*port_dump_done)(const struct dpif *dpif, void *state);
168
169     /* Polls for changes in the set of ports in 'dpif'.  If the set of ports in
170      * 'dpif' has changed, then this function should do one of the
171      * following:
172      *
173      * - Preferably: store the name of the device that was added to or deleted
174      *   from 'dpif' in '*devnamep' and return 0.  The caller is responsible
175      *   for freeing '*devnamep' (with free()) when it no longer needs it.
176      *
177      * - Alternatively: return ENOBUFS, without indicating the device that was
178      *   added or deleted.
179      *
180      * Occasional 'false positives', in which the function returns 0 while
181      * indicating a device that was not actually added or deleted or returns
182      * ENOBUFS without any change, are acceptable.
183      *
184      * If the set of ports in 'dpif' has not changed, returns EAGAIN.  May also
185      * return other positive errno values to indicate that something has gone
186      * wrong. */
187     int (*port_poll)(const struct dpif *dpif, char **devnamep);
188
189     /* Arranges for the poll loop to wake up when 'port_poll' will return a
190      * value other than EAGAIN. */
191     void (*port_poll_wait)(const struct dpif *dpif);
192
193     /* Queries 'dpif' for a flow entry.  The flow is specified by the Netlink
194      * attributes with types ODP_KEY_ATTR_* in the 'key_len' bytes starting at
195      * 'key'.
196      *
197      * Returns 0 if successful.  If no flow matches, returns ENOENT.  On other
198      * failure, returns a positive errno value.
199      *
200      * If 'actionsp' is nonnull, then on success '*actionsp' must be set to an
201      * ofpbuf owned by the caller that contains the Netlink attributes for the
202      * flow's actions.  The caller must free the ofpbuf (with ofpbuf_delete())
203      * when it is no longer needed.
204      *
205      * If 'stats' is nonnull, then on success it must be updated with the
206      * flow's statistics. */
207     int (*flow_get)(const struct dpif *dpif,
208                     const struct nlattr *key, size_t key_len,
209                     struct ofpbuf **actionsp, struct dpif_flow_stats *stats);
210
211     /* Adds or modifies a flow in 'dpif'.  The flow is specified by the Netlink
212      * attributes with types ODP_KEY_ATTR_* in the 'key_len' bytes starting at
213      * 'key'.  The associated actions are specified by the Netlink attributes
214      * with types ODP_ACTION_ATTR_* in the 'actions_len' bytes starting at
215      * 'actions'.
216      *
217      * - If the flow's key does not exist in 'dpif', then the flow will be
218      *   added if 'flags' includes DPIF_FP_CREATE.  Otherwise the operation
219      *   will fail with ENOENT.
220      *
221      *   If the operation succeeds, then 'stats', if nonnull, must be zeroed.
222      *
223      * - If the flow's key does exist in 'dpif', then the flow's actions will
224      *   be updated if 'flags' includes DPIF_FP_MODIFY.  Otherwise the
225      *   operation will fail with EEXIST.  If the flow's actions are updated,
226      *   then its statistics will be zeroed if 'flags' includes
227      *   DPIF_FP_ZERO_STATS, and left as-is otherwise.
228      *
229      *   If the operation succeeds, then 'stats', if nonnull, must be set to
230      *   the flow's statistics before the update.
231      */
232     int (*flow_put)(struct dpif *dpif, enum dpif_flow_put_flags flags,
233                     const struct nlattr *key, size_t key_len,
234                     const struct nlattr *actions, size_t actions_len,
235                     struct dpif_flow_stats *stats);
236
237     /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif'
238      * does not contain such a flow.  The flow is specified by the Netlink
239      * attributes with types ODP_KEY_ATTR_* in the 'key_len' bytes starting at
240      * 'key'.
241      *
242      * If the operation succeeds, then 'stats', if nonnull, must be set to the
243      * flow's statistics before its deletion. */
244     int (*flow_del)(struct dpif *dpif,
245                     const struct nlattr *key, size_t key_len,
246                     struct dpif_flow_stats *stats);
247
248     /* Deletes all flows from 'dpif' and clears all of its queues of received
249      * packets. */
250     int (*flow_flush)(struct dpif *dpif);
251
252     /* Attempts to begin dumping the flows in a dpif.  On success, returns 0
253      * and initializes '*statep' with any data needed for iteration.  On
254      * failure, returns a positive errno value. */
255     int (*flow_dump_start)(const struct dpif *dpif, void **statep);
256
257     /* Attempts to retrieve another flow from 'dpif' for 'state', which was
258      * initialized by a successful call to the 'flow_dump_start' function for
259      * 'dpif'.  On success, updates the output parameters as described below
260      * and returns 0.  Returns EOF if the end of the flow table has been
261      * reached, or a positive errno value on error.  This function will not be
262      * called again once it returns nonzero within a given iteration (but the
263      * 'flow_dump_done' function will be called afterward).
264      *
265      * On success, if 'key' and 'key_len' are nonnull then '*key' and
266      * '*key_len' must be set to Netlink attributes with types ODP_KEY_ATTR_*
267      * representing the dumped flow's key.  If 'actions' and 'actions_len' are
268      * nonnull then they should be set to Netlink attributes with types
269      * ODP_ACTION_ATTR_* representing the dumped flow's actions.  If 'stats'
270      * is nonnull then it should be set to the dumped flow's statistics.
271      *
272      * All of the returned data is owned by 'dpif', not by the caller, and the
273      * caller must not modify or free it.  'dpif' must guarantee that it
274      * remains accessible and unchanging until at least the next call to
275      * 'flow_dump_next' or 'flow_dump_done' for 'state'. */
276     int (*flow_dump_next)(const struct dpif *dpif, void *state,
277                           const struct nlattr **key, size_t *key_len,
278                           const struct nlattr **actions, size_t *actions_len,
279                           const struct dpif_flow_stats **stats);
280
281     /* Releases resources from 'dpif' for 'state', which was initialized by a
282      * successful call to the 'flow_dump_start' function for 'dpif'.  */
283     int (*flow_dump_done)(const struct dpif *dpif, void *state);
284
285     /* Performs the 'actions_len' bytes of actions in 'actions' on the Ethernet
286      * frame specified in 'packet'. */
287     int (*execute)(struct dpif *dpif, const struct nlattr *actions,
288                    size_t actions_len, const struct ofpbuf *packet);
289
290     /* Retrieves 'dpif''s "listen mask" into '*listen_mask'.  A 1-bit of value
291      * 2**X set in '*listen_mask' indicates that 'dpif' will receive messages
292      * of the type (from "enum dpif_upcall_type") with value X when its 'recv'
293      * function is called. */
294     int (*recv_get_mask)(const struct dpif *dpif, int *listen_mask);
295
296     /* Sets 'dpif''s "listen mask" to 'listen_mask'.  A 1-bit of value 2**X set
297      * in '*listen_mask' requests that 'dpif' will receive messages of the type
298      * (from "enum dpif_upcall_type") with value X when its 'recv' function is
299      * called. */
300     int (*recv_set_mask)(struct dpif *dpif, int listen_mask);
301
302     /* Retrieves 'dpif''s sFlow sampling probability into '*probability'.
303      * Return value is 0 or a positive errno value.  EOPNOTSUPP indicates that
304      * the datapath does not support sFlow, as does a null pointer.
305      *
306      * '*probability' is expressed as the number of packets out of UINT_MAX to
307      * sample, e.g. probability/UINT_MAX is the probability of sampling a given
308      * packet. */
309     int (*get_sflow_probability)(const struct dpif *dpif,
310                                  uint32_t *probability);
311
312     /* Sets 'dpif''s sFlow sampling probability to 'probability'.  Return value
313      * is 0 or a positive errno value.  EOPNOTSUPP indicates that the datapath
314      * does not support sFlow, as does a null pointer.
315      *
316      * 'probability' is expressed as the number of packets out of UINT_MAX to
317      * sample, e.g. probability/UINT_MAX is the probability of sampling a given
318      * packet. */
319     int (*set_sflow_probability)(struct dpif *dpif, uint32_t probability);
320
321     /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a
322      * priority value for use in the ODP_ACTION_ATTR_SET_PRIORITY action in
323      * '*priority'. */
324     int (*queue_to_priority)(const struct dpif *dpif, uint32_t queue_id,
325                              uint32_t *priority);
326
327     /* Polls for an upcall from 'dpif'.  If successful, stores the upcall into
328      * '*upcall'.  Only upcalls of the types selected with the set_listen_mask
329      * member function should be received.
330      *
331      * The caller takes ownership of the data that 'upcall' points to.
332      * 'upcall->key' and 'upcall->actions' (if nonnull) point into data owned
333      * by 'upcall->packet', so their memory cannot be freed separately.  (This
334      * is hardly a great way to do things but it works out OK for the dpif
335      * providers that exist so far.)
336      *
337      * For greatest efficiency, 'upcall->packet' should have at least
338      * offsetof(struct ofp_packet_in, data) bytes of headroom.
339      *
340      * This function must not block.  If no upcall is pending when it is
341      * called, it should return EAGAIN without blocking. */
342     int (*recv)(struct dpif *dpif, struct dpif_upcall *upcall);
343
344     /* Arranges for the poll loop to wake up when 'dpif' has a message queued
345      * to be received with the recv member function. */
346     void (*recv_wait)(struct dpif *dpif);
347
348     /* Throws away any queued upcalls that 'dpif' currently has ready to
349      * return. */
350     void (*recv_purge)(struct dpif *dpif);
351 };
352
353 extern const struct dpif_class dpif_linux_class;
354 extern const struct dpif_class dpif_netdev_class;
355
356 #ifdef  __cplusplus
357 }
358 #endif
359
360 #endif /* dpif-provider.h */