Merge branch 'mainstream'
[sliver-openvswitch.git] / lib / dpif-provider.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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     /* Enumerates the names of all known created datapaths, if possible, into
73      * 'all_dps'.  The caller has already initialized 'all_dps' and other dpif
74      * classes might already have added names to it.
75      *
76      * This is used by the vswitch at startup, so that it can delete any
77      * datapaths that are not configured.
78      *
79      * Some kinds of datapaths might not be practically enumerable, in which
80      * case this function may be a null pointer. */
81     int (*enumerate)(struct sset *all_dps);
82
83     /* Returns the type to pass to netdev_open() when a dpif of class
84      * 'dpif_class' has a port of type 'type', for a few special cases
85      * when a netdev type differs from a port type.  For example, when
86      * using the userspace datapath, a port of type "internal" needs to
87      * be opened as "tap".
88      *
89      * Returns either 'type' itself or a string literal, which must not
90      * be freed. */
91     const char *(*port_open_type)(const struct dpif_class *dpif_class,
92                                   const char *type);
93
94     /* Attempts to open an existing dpif called 'name', if 'create' is false,
95      * or to open an existing dpif or create a new one, if 'create' is true.
96      *
97      * 'dpif_class' is the class of dpif to open.
98      *
99      * If successful, stores a pointer to the new dpif in '*dpifp', which must
100      * have class 'dpif_class'.  On failure there are no requirements on what
101      * is stored in '*dpifp'. */
102     int (*open)(const struct dpif_class *dpif_class,
103                 const char *name, bool create, struct dpif **dpifp);
104
105     /* Closes 'dpif' and frees associated memory. */
106     void (*close)(struct dpif *dpif);
107
108     /* Attempts to destroy the dpif underlying 'dpif'.
109      *
110      * If successful, 'dpif' will not be used again except as an argument for
111      * the 'close' member function. */
112     int (*destroy)(struct dpif *dpif);
113
114     /* Performs periodic work needed by 'dpif', if any is necessary. */
115     void (*run)(struct dpif *dpif);
116
117     /* Arranges for poll_block() to wake up if the "run" member function needs
118      * to be called for 'dpif'. */
119     void (*wait)(struct dpif *dpif);
120
121     /* Retrieves statistics for 'dpif' into 'stats'. */
122     int (*get_stats)(const struct dpif *dpif, struct dpif_dp_stats *stats);
123
124     /* Adds 'netdev' as a new port in 'dpif'.  If '*port_no' is not
125      * UINT32_MAX, attempts to use that as the port's port number.
126      *
127      * If port is successfully added, sets '*port_no' to the new port's
128      * port number.  Returns EBUSY if caller attempted to choose a port
129      * number, and it was in use. */
130     int (*port_add)(struct dpif *dpif, struct netdev *netdev,
131                     uint32_t *port_no);
132
133     /* Removes port numbered 'port_no' from 'dpif'. */
134     int (*port_del)(struct dpif *dpif, uint32_t port_no);
135
136     /* Queries 'dpif' for a port with the given 'port_no' or 'devname'.
137      * If 'port' is not null, stores information about the port into
138      * '*port' if successful.
139      *
140      * If 'port' is not null, the caller takes ownership of data in
141      * 'port' and must free it with dpif_port_destroy() when it is no
142      * longer needed. */
143     int (*port_query_by_number)(const struct dpif *dpif, uint32_t port_no,
144                                 struct dpif_port *port);
145     int (*port_query_by_name)(const struct dpif *dpif, const char *devname,
146                               struct dpif_port *port);
147
148     /* Returns one greater than the largest port number accepted in flow
149      * actions. */
150     int (*get_max_ports)(const struct dpif *dpif);
151
152     /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
153      * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
154      * flows whose packets arrived on port 'port_no'.
155      *
156      * A 'port_no' of UINT32_MAX should be treated as a special case.  The
157      * implementation should return a reserved PID, not allocated to any port,
158      * that the client may use for special purposes.
159      *
160      * The return value only needs to be meaningful when DPIF_UC_ACTION has
161      * been enabled in the 'dpif''s listen mask, and it is allowed to change
162      * when DPIF_UC_ACTION is disabled and then re-enabled.
163      *
164      * A dpif provider that doesn't have meaningful Netlink PIDs can use NULL
165      * for this function.  This is equivalent to always returning 0. */
166     uint32_t (*port_get_pid)(const struct dpif *dpif, uint32_t port_no);
167
168     /* Attempts to begin dumping the ports in a dpif.  On success, returns 0
169      * and initializes '*statep' with any data needed for iteration.  On
170      * failure, returns a positive errno value. */
171     int (*port_dump_start)(const struct dpif *dpif, void **statep);
172
173     /* Attempts to retrieve another port from 'dpif' for 'state', which was
174      * initialized by a successful call to the 'port_dump_start' function for
175      * 'dpif'.  On success, stores a new dpif_port into 'port' and returns 0.
176      * Returns EOF if the end of the port table has been reached, or a positive
177      * errno value on error.  This function will not be called again once it
178      * returns nonzero once for a given iteration (but the 'port_dump_done'
179      * function will be called afterward).
180      *
181      * The dpif provider retains ownership of the data stored in 'port'.  It
182      * must remain valid until at least the next call to 'port_dump_next' or
183      * 'port_dump_done' for 'state'. */
184     int (*port_dump_next)(const struct dpif *dpif, void *state,
185                           struct dpif_port *port);
186
187     /* Releases resources from 'dpif' for 'state', which was initialized by a
188      * successful call to the 'port_dump_start' function for 'dpif'.  */
189     int (*port_dump_done)(const struct dpif *dpif, void *state);
190
191     /* Polls for changes in the set of ports in 'dpif'.  If the set of ports in
192      * 'dpif' has changed, then this function should do one of the
193      * following:
194      *
195      * - Preferably: store the name of the device that was added to or deleted
196      *   from 'dpif' in '*devnamep' and return 0.  The caller is responsible
197      *   for freeing '*devnamep' (with free()) when it no longer needs it.
198      *
199      * - Alternatively: return ENOBUFS, without indicating the device that was
200      *   added or deleted.
201      *
202      * Occasional 'false positives', in which the function returns 0 while
203      * indicating a device that was not actually added or deleted or returns
204      * ENOBUFS without any change, are acceptable.
205      *
206      * If the set of ports in 'dpif' has not changed, returns EAGAIN.  May also
207      * return other positive errno values to indicate that something has gone
208      * wrong. */
209     int (*port_poll)(const struct dpif *dpif, char **devnamep);
210
211     /* Arranges for the poll loop to wake up when 'port_poll' will return a
212      * value other than EAGAIN. */
213     void (*port_poll_wait)(const struct dpif *dpif);
214
215     /* Queries 'dpif' for a flow entry.  The flow is specified by the Netlink
216      * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
217      * 'key'.
218      *
219      * Returns 0 if successful.  If no flow matches, returns ENOENT.  On other
220      * failure, returns a positive errno value.
221      *
222      * If 'actionsp' is nonnull, then on success '*actionsp' must be set to an
223      * ofpbuf owned by the caller that contains the Netlink attributes for the
224      * flow's actions.  The caller must free the ofpbuf (with ofpbuf_delete())
225      * when it is no longer needed.
226      *
227      * If 'stats' is nonnull, then on success it must be updated with the
228      * flow's statistics. */
229     int (*flow_get)(const struct dpif *dpif,
230                     const struct nlattr *key, size_t key_len,
231                     struct ofpbuf **actionsp, struct dpif_flow_stats *stats);
232
233     /* Adds or modifies a flow in 'dpif'.  The flow is specified by the Netlink
234      * attributes with types OVS_KEY_ATTR_* in the 'put->key_len' bytes
235      * starting at 'put->key'.  The associated actions are specified by the
236      * Netlink attributes with types OVS_ACTION_ATTR_* in the
237      * 'put->actions_len' bytes starting at 'put->actions'.
238      *
239      * - If the flow's key does not exist in 'dpif', then the flow will be
240      *   added if 'put->flags' includes DPIF_FP_CREATE.  Otherwise the
241      *   operation will fail with ENOENT.
242      *
243      *   If the operation succeeds, then 'put->stats', if nonnull, must be
244      *   zeroed.
245      *
246      * - If the flow's key does exist in 'dpif', then the flow's actions will
247      *   be updated if 'put->flags' includes DPIF_FP_MODIFY.  Otherwise the
248      *   operation will fail with EEXIST.  If the flow's actions are updated,
249      *   then its statistics will be zeroed if 'put->flags' includes
250      *   DPIF_FP_ZERO_STATS, and left as-is otherwise.
251      *
252      *   If the operation succeeds, then 'put->stats', if nonnull, must be set
253      *   to the flow's statistics before the update.
254      */
255     int (*flow_put)(struct dpif *dpif, const struct dpif_flow_put *put);
256
257     /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif'
258      * does not contain such a flow.  The flow is specified by the Netlink
259      * attributes with types OVS_KEY_ATTR_* in the 'del->key_len' bytes
260      * starting at 'del->key'.
261      *
262      * If the operation succeeds, then 'del->stats', if nonnull, must be set to
263      * the flow's statistics before its deletion. */
264     int (*flow_del)(struct dpif *dpif, const struct dpif_flow_del *del);
265
266     /* Deletes all flows from 'dpif' and clears all of its queues of received
267      * packets. */
268     int (*flow_flush)(struct dpif *dpif);
269
270     /* Attempts to begin dumping the flows in a dpif.  On success, returns 0
271      * and initializes '*statep' with any data needed for iteration.  On
272      * failure, returns a positive errno value. */
273     int (*flow_dump_start)(const struct dpif *dpif, void **statep);
274
275     /* Attempts to retrieve another flow from 'dpif' for 'state', which was
276      * initialized by a successful call to the 'flow_dump_start' function for
277      * 'dpif'.  On success, updates the output parameters as described below
278      * and returns 0.  Returns EOF if the end of the flow table has been
279      * reached, or a positive errno value on error.  This function will not be
280      * called again once it returns nonzero within a given iteration (but the
281      * 'flow_dump_done' function will be called afterward).
282      *
283      * On success, if 'key' and 'key_len' are nonnull then '*key' and
284      * '*key_len' must be set to Netlink attributes with types OVS_KEY_ATTR_*
285      * representing the dumped flow's key.  If 'actions' and 'actions_len' are
286      * nonnull then they should be set to Netlink attributes with types
287      * OVS_ACTION_ATTR_* representing the dumped flow's actions.  If 'stats'
288      * is nonnull then it should be set to the dumped flow's statistics.
289      *
290      * All of the returned data is owned by 'dpif', not by the caller, and the
291      * caller must not modify or free it.  'dpif' must guarantee that it
292      * remains accessible and unchanging until at least the next call to
293      * 'flow_dump_next' or 'flow_dump_done' for 'state'. */
294     int (*flow_dump_next)(const struct dpif *dpif, void *state,
295                           const struct nlattr **key, size_t *key_len,
296                           const struct nlattr **actions, size_t *actions_len,
297                           const struct dpif_flow_stats **stats);
298
299     /* Releases resources from 'dpif' for 'state', which was initialized by a
300      * successful call to the 'flow_dump_start' function for 'dpif'.  */
301     int (*flow_dump_done)(const struct dpif *dpif, void *state);
302
303     /* Performs the 'execute->actions_len' bytes of actions in
304      * 'execute->actions' on the Ethernet frame specified in 'execute->packet'
305      * taken from the flow specified in the 'execute->key_len' bytes of
306      * 'execute->key'.  ('execute->key' is mostly redundant with
307      * 'execute->packet', but it contains some metadata that cannot be
308      * recovered from 'execute->packet', such as tunnel and in_port.) */
309     int (*execute)(struct dpif *dpif, const struct dpif_execute *execute);
310
311     /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order
312      * in which they are specified, placing each operation's results in the
313      * "output" members documented in comments.
314      *
315      * This function is optional.  It is only worthwhile to implement it if
316      * 'dpif' can perform operations in batch faster than individually. */
317     void (*operate)(struct dpif *dpif, struct dpif_op **ops, size_t n_ops);
318
319     /* Enables or disables receiving packets with dpif_recv() for 'dpif'.
320      * Turning packet receive off and then back on is allowed to change Netlink
321      * PID assignments (see ->port_get_pid()).  The client is responsible for
322      * updating flows as necessary if it does this. */
323     int (*recv_set)(struct dpif *dpif, bool enable);
324
325     /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a
326      * priority value used for setting packet priority. */
327     int (*queue_to_priority)(const struct dpif *dpif, uint32_t queue_id,
328                              uint32_t *priority);
329
330     /* Polls for an upcall from 'dpif'.  If successful, stores the upcall into
331      * '*upcall', using 'buf' for storage.  Should only be called if 'recv_set'
332      * has been used to enable receiving packets from 'dpif'.
333      *
334      * The implementation should point 'upcall->packet' and 'upcall->key' into
335      * data in the caller-provided 'buf'.  If necessary to make room, the
336      * implementation may expand the data in 'buf'.  (This is hardly a great
337      * way to do things but it works out OK for the dpif providers that exist
338      * so far.)
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                 struct ofpbuf *buf);
344
345     /* Arranges for the poll loop to wake up when 'dpif' has a message queued
346      * to be received with the recv member function. */
347     void (*recv_wait)(struct dpif *dpif);
348
349     /* Throws away any queued upcalls that 'dpif' currently has ready to
350      * return. */
351     void (*recv_purge)(struct dpif *dpif);
352 };
353
354 extern const struct dpif_class dpif_linux_class;
355 extern const struct dpif_class dpif_netdev_class;
356 extern const struct dpif_class dpif_planetlab_class;
357
358 #ifdef  __cplusplus
359 }
360 #endif
361
362 #endif /* dpif-provider.h */