dpif: Support fetching flow mask via dpif_flow_get().
[cascardo/ovs.git] / lib / dpif-provider.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "openflow/openflow.h"
26 #include "dpif.h"
27 #include "util.h"
28
29 #ifdef  __cplusplus
30 extern "C" {
31 #endif
32
33 /* Open vSwitch datapath interface.
34  *
35  * This structure should be treated as opaque by dpif implementations. */
36 struct dpif {
37     const struct dpif_class *dpif_class;
38     char *base_name;
39     char *full_name;
40     uint8_t netflow_engine_type;
41     uint8_t netflow_engine_id;
42 };
43
44 void dpif_init(struct dpif *, const struct dpif_class *, const char *name,
45                uint8_t netflow_engine_type, uint8_t netflow_engine_id);
46 void dpif_uninit(struct dpif *dpif, bool close);
47
48 static inline void dpif_assert_class(const struct dpif *dpif,
49                                      const struct dpif_class *dpif_class)
50 {
51     ovs_assert(dpif->dpif_class == dpif_class);
52 }
53
54 /* Datapath interface class structure, to be defined by each implementation of
55  * a datapath interface.
56  *
57  * These functions return 0 if successful or a positive errno value on failure,
58  * except where otherwise noted.
59  *
60  * These functions are expected to execute synchronously, that is, to block as
61  * necessary to obtain a result.  Thus, they may not return EAGAIN or
62  * EWOULDBLOCK or EINPROGRESS.  We may relax this requirement in the future if
63  * and when we encounter performance problems. */
64 struct dpif_class {
65     /* Type of dpif in this class, e.g. "system", "netdev", etc.
66      *
67      * One of the providers should supply a "system" type, since this is
68      * the type assumed if no type is specified when opening a dpif. */
69     const char *type;
70
71     /* Enumerates the names of all known created datapaths, if possible, into
72      * 'all_dps'.  The caller has already initialized 'all_dps' and other dpif
73      * classes might already have added names to it.
74      *
75      * This is used by the vswitch at startup, so that it can delete any
76      * datapaths that are not configured.
77      *
78      * Some kinds of datapaths might not be practically enumerable, in which
79      * case this function may be a null pointer. */
80     int (*enumerate)(struct sset *all_dps);
81
82     /* Returns the type to pass to netdev_open() when a dpif of class
83      * 'dpif_class' has a port of type 'type', for a few special cases
84      * when a netdev type differs from a port type.  For example, when
85      * using the userspace datapath, a port of type "internal" needs to
86      * be opened as "tap".
87      *
88      * Returns either 'type' itself or a string literal, which must not
89      * be freed. */
90     const char *(*port_open_type)(const struct dpif_class *dpif_class,
91                                   const char *type);
92
93     /* Attempts to open an existing dpif called 'name', if 'create' is false,
94      * or to open an existing dpif or create a new one, if 'create' is true.
95      *
96      * 'dpif_class' is the class of dpif to open.
97      *
98      * If successful, stores a pointer to the new dpif in '*dpifp', which must
99      * have class 'dpif_class'.  On failure there are no requirements on what
100      * is stored in '*dpifp'. */
101     int (*open)(const struct dpif_class *dpif_class,
102                 const char *name, bool create, struct dpif **dpifp);
103
104     /* Closes 'dpif' and frees associated memory. */
105     void (*close)(struct dpif *dpif);
106
107     /* Attempts to destroy the dpif underlying 'dpif'.
108      *
109      * If successful, 'dpif' will not be used again except as an argument for
110      * the 'close' member function. */
111     int (*destroy)(struct dpif *dpif);
112
113     /* Performs periodic work needed by 'dpif', if any is necessary. */
114     void (*run)(struct dpif *dpif);
115
116     /* Arranges for poll_block() to wake up if the "run" member function needs
117      * to be called for 'dpif'. */
118     void (*wait)(struct dpif *dpif);
119
120     /* Retrieves statistics for 'dpif' into 'stats'. */
121     int (*get_stats)(const struct dpif *dpif, struct dpif_dp_stats *stats);
122
123     /* Adds 'netdev' as a new port in 'dpif'.  If '*port_no' is not
124      * UINT32_MAX, attempts to use that as the port's port number.
125      *
126      * If port is successfully added, sets '*port_no' to the new port's
127      * port number.  Returns EBUSY if caller attempted to choose a port
128      * number, and it was in use. */
129     int (*port_add)(struct dpif *dpif, struct netdev *netdev,
130                     odp_port_t *port_no);
131
132     /* Removes port numbered 'port_no' from 'dpif'. */
133     int (*port_del)(struct dpif *dpif, odp_port_t port_no);
134
135     /* Queries 'dpif' for a port with the given 'port_no' or 'devname'.
136      * If 'port' is not null, stores information about the port into
137      * '*port' if successful.
138      *
139      * If 'port' is not null, the caller takes ownership of data in
140      * 'port' and must free it with dpif_port_destroy() when it is no
141      * longer needed. */
142     int (*port_query_by_number)(const struct dpif *dpif, odp_port_t port_no,
143                                 struct dpif_port *port);
144     int (*port_query_by_name)(const struct dpif *dpif, const char *devname,
145                               struct dpif_port *port);
146
147     /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
148      * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
149      * flows whose packets arrived on port 'port_no'.  In the case where the
150      * provider allocates multiple Netlink PIDs to a single port, it may use
151      * 'hash' to spread load among them.  The caller need not use a particular
152      * hash function; a 5-tuple hash is suitable.
153      *
154      * (The datapath implementation might use some different hash function for
155      * distributing packets received via flow misses among PIDs.  This means
156      * that packets received via flow misses might be reordered relative to
157      * packets received via userspace actions.  This is not ordinarily a
158      * problem.)
159      *
160      * A 'port_no' of UINT32_MAX should be treated as a special case.  The
161      * implementation should return a reserved PID, not allocated to any port,
162      * that the client may use for special purposes.
163      *
164      * The return value only needs to be meaningful when DPIF_UC_ACTION has
165      * been enabled in the 'dpif''s listen mask, and it is allowed to change
166      * when DPIF_UC_ACTION is disabled and then re-enabled.
167      *
168      * A dpif provider that doesn't have meaningful Netlink PIDs can use NULL
169      * for this function.  This is equivalent to always returning 0. */
170     uint32_t (*port_get_pid)(const struct dpif *dpif, odp_port_t port_no,
171                              uint32_t hash);
172
173     /* Attempts to begin dumping the ports in a dpif.  On success, returns 0
174      * and initializes '*statep' with any data needed for iteration.  On
175      * failure, returns a positive errno value. */
176     int (*port_dump_start)(const struct dpif *dpif, void **statep);
177
178     /* Attempts to retrieve another port from 'dpif' for 'state', which was
179      * initialized by a successful call to the 'port_dump_start' function for
180      * 'dpif'.  On success, stores a new dpif_port into 'port' and returns 0.
181      * Returns EOF if the end of the port table has been reached, or a positive
182      * errno value on error.  This function will not be called again once it
183      * returns nonzero once for a given iteration (but the 'port_dump_done'
184      * function will be called afterward).
185      *
186      * The dpif provider retains ownership of the data stored in 'port'.  It
187      * must remain valid until at least the next call to 'port_dump_next' or
188      * 'port_dump_done' for 'state'. */
189     int (*port_dump_next)(const struct dpif *dpif, void *state,
190                           struct dpif_port *port);
191
192     /* Releases resources from 'dpif' for 'state', which was initialized by a
193      * successful call to the 'port_dump_start' function for 'dpif'.  */
194     int (*port_dump_done)(const struct dpif *dpif, void *state);
195
196     /* Polls for changes in the set of ports in 'dpif'.  If the set of ports in
197      * 'dpif' has changed, then this function should do one of the
198      * following:
199      *
200      * - Preferably: store the name of the device that was added to or deleted
201      *   from 'dpif' in '*devnamep' and return 0.  The caller is responsible
202      *   for freeing '*devnamep' (with free()) when it no longer needs it.
203      *
204      * - Alternatively: return ENOBUFS, without indicating the device that was
205      *   added or deleted.
206      *
207      * Occasional 'false positives', in which the function returns 0 while
208      * indicating a device that was not actually added or deleted or returns
209      * ENOBUFS without any change, are acceptable.
210      *
211      * If the set of ports in 'dpif' has not changed, returns EAGAIN.  May also
212      * return other positive errno values to indicate that something has gone
213      * wrong. */
214     int (*port_poll)(const struct dpif *dpif, char **devnamep);
215
216     /* Arranges for the poll loop to wake up when 'port_poll' will return a
217      * value other than EAGAIN. */
218     void (*port_poll_wait)(const struct dpif *dpif);
219
220     /* Queries 'dpif' for a flow entry.  The flow is specified by the Netlink
221      * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
222      * 'key'.
223      *
224      * Returns 0 if successful.  If no flow matches, returns ENOENT.  On other
225      * failure, returns a positive errno value.
226      *
227      * On success, '*bufp' will be set to an ofpbuf owned by the caller that
228      * contains the response for 'maskp' and 'actionsp'. The caller must supply
229      * a valid pointer, and must free the ofpbuf (with ofpbuf_delete()) when it
230      * is no longer needed.
231      *
232      * If 'maskp' is nonnull, then on success '*maskp' will point to the
233      * Netlink attributes for the flow's mask, stored in '*bufp'. '*mask_len'
234      * will be set to the length of the mask attributes.
235      *
236      * If 'actionsp' is nonnull, then on success '*actionsp' will point to the
237      * Netlink attributes for the flow's actions, stored in '*bufp'.
238      * '*actions_len' will be set to the length of the actions attributes.
239      *
240      * If 'stats' is nonnull, then on success it must be updated with the
241      * flow's statistics. */
242     int (*flow_get)(const struct dpif *dpif,
243                     const struct nlattr *key, size_t key_len,
244                     struct ofpbuf **bufp,
245                     struct nlattr **maskp, size_t *mask_len,
246                     struct nlattr **actionsp, size_t *acts_len,
247                     struct dpif_flow_stats *stats);
248
249     /* Adds or modifies a flow in 'dpif'.  The flow is specified by the Netlink
250      * attributes with types OVS_KEY_ATTR_* in the 'put->key_len' bytes
251      * starting at 'put->key'.  The associated actions are specified by the
252      * Netlink attributes with types OVS_ACTION_ATTR_* in the
253      * 'put->actions_len' bytes starting at 'put->actions'.
254      *
255      * - If the flow's key does not exist in 'dpif', then the flow will be
256      *   added if 'put->flags' includes DPIF_FP_CREATE.  Otherwise the
257      *   operation will fail with ENOENT.
258      *
259      *   If the operation succeeds, then 'put->stats', if nonnull, must be
260      *   zeroed.
261      *
262      * - If the flow's key does exist in 'dpif', then the flow's actions will
263      *   be updated if 'put->flags' includes DPIF_FP_MODIFY.  Otherwise the
264      *   operation will fail with EEXIST.  If the flow's actions are updated,
265      *   then its statistics will be zeroed if 'put->flags' includes
266      *   DPIF_FP_ZERO_STATS, and left as-is otherwise.
267      *
268      *   If the operation succeeds, then 'put->stats', if nonnull, must be set
269      *   to the flow's statistics before the update.
270      */
271     int (*flow_put)(struct dpif *dpif, const struct dpif_flow_put *put);
272
273     /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif'
274      * does not contain such a flow.  The flow is specified by the Netlink
275      * attributes with types OVS_KEY_ATTR_* in the 'del->key_len' bytes
276      * starting at 'del->key'.
277      *
278      * If the operation succeeds, then 'del->stats', if nonnull, must be set to
279      * the flow's statistics before its deletion. */
280     int (*flow_del)(struct dpif *dpif, const struct dpif_flow_del *del);
281
282     /* Deletes all flows from 'dpif' and clears all of its queues of received
283      * packets. */
284     int (*flow_flush)(struct dpif *dpif);
285
286     /* Allocates thread-local state for use with the function 'flow_dump_next'.
287      * On return, initializes '*statep' with any private data needed for
288      * iteration. */
289     void (*flow_dump_state_init)(void **statep);
290
291     /* Attempts to begin dumping the flows in a dpif.  On success, returns 0
292      * and initializes '*iterp' with any shared data needed for iteration.
293      * On failure, returns a positive errno value. */
294     int (*flow_dump_start)(const struct dpif *dpif, void **iterp);
295
296     /* Attempts to retrieve another flow from 'dpif' for 'iter', using
297      * 'state' for storage. 'iter' must have been initialized by a successful
298      * call to the 'flow_dump_start' function for 'dpif'. 'state' must have
299      * been initialised with a call to the 'flow_dump_state_init' function for
300      * 'dpif.
301      *
302      * On success, updates the output parameters as described below and returns
303      * 0. Returns EOF if the end of the flow table has been reached, or a
304      * positive errno value on error. Multiple threads may use the same 'dpif'
305      * and 'iter' with this function, but all other parameters must be
306      * different for each thread. If this function returns non-zero,
307      * subsequent calls with the same arguments will also return non-zero.
308      *
309      * On success:
310      *
311      *     - If 'key' and 'key_len' are nonnull, then '*key' and '*key_len'
312      *       must be set to Netlink attributes with types OVS_KEY_ATTR_*
313      *       representing the dumped flow's key.
314      *
315      *     - If 'mask' and 'mask_len' are nonnull then '*mask' and '*mask_len'
316      *       must be set to Netlink attributes with types of OVS_KEY_ATTR_*
317      *       representing the dumped flow's mask.
318      *
319      *     - If 'actions' and 'actions_len' are nonnull then they should be set
320      *       to Netlink attributes with types OVS_ACTION_ATTR_* representing
321      *       the dumped flow's actions.
322      *
323      *     - If 'stats' is nonnull then it should be set to the dumped flow's
324      *       statistics.
325      *
326      * All of the returned data is owned by 'dpif', not by the caller, and the
327      * caller must not modify or free it.  'dpif' must guarantee that it
328      * remains accessible and unchanging until at least the next call to
329      * 'flow_dump_next' or 'flow_dump_done' for 'iter' and 'state'. */
330     int (*flow_dump_next)(const struct dpif *dpif, void *iter, void *state,
331                           const struct nlattr **key, size_t *key_len,
332                           const struct nlattr **mask, size_t *mask_len,
333                           const struct nlattr **actions, size_t *actions_len,
334                           const struct dpif_flow_stats **stats);
335
336     /* Determines whether the next call to 'flow_dump_next' with 'state' will
337      * modify or free the keys that it previously returned. 'state' must have
338      * been initialized by a call to 'flow_dump_state_init' for 'dpif'.
339      *
340      * 'dpif' guarantees that data returned by flow_dump_next() will remain
341      * accessible and unchanging until the next call. This function provides a
342      * way for callers to determine whether that guarantee extends beyond the
343      * next call.
344      *
345      * Returns true if the next call to flow_dump_next() is expected to be
346      * destructive to previously returned keys for 'state', false otherwise. */
347     bool (*flow_dump_next_may_destroy_keys)(void *state);
348
349     /* Releases resources from 'dpif' for 'iter', which was initialized by a
350      * successful call to the 'flow_dump_start' function for 'dpif'. Callers
351      * must ensure that this function is called once within a given iteration,
352      * as the final flow dump operation. */
353     int (*flow_dump_done)(const struct dpif *dpif, void *iter);
354
355     /* Releases 'state' which was initialized by a call to the
356      * 'flow_dump_state_init' function for this 'dpif'. */
357     void (*flow_dump_state_uninit)(void *statep);
358
359     /* Performs the 'execute->actions_len' bytes of actions in
360      * 'execute->actions' on the Ethernet frame in 'execute->packet'
361      * and on the packet metadata in 'execute->md'.
362      * May modify both packet and metadata. */
363     int (*execute)(struct dpif *dpif, struct dpif_execute *execute);
364
365     /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order
366      * in which they are specified, placing each operation's results in the
367      * "output" members documented in comments.
368      *
369      * This function is optional.  It is only worthwhile to implement it if
370      * 'dpif' can perform operations in batch faster than individually. */
371     void (*operate)(struct dpif *dpif, struct dpif_op **ops, size_t n_ops);
372
373     /* Enables or disables receiving packets with dpif_recv() for 'dpif'.
374      * Turning packet receive off and then back on is allowed to change Netlink
375      * PID assignments (see ->port_get_pid()).  The client is responsible for
376      * updating flows as necessary if it does this. */
377     int (*recv_set)(struct dpif *dpif, bool enable);
378
379     /* Refreshes the poll loops and Netlink sockets associated to each port,
380      * when the number of upcall handlers (upcall receiving thread) is changed
381      * to 'n_handlers' and receiving packets for 'dpif' is enabled by
382      * recv_set().
383      *
384      * Since multiple upcall handlers can read upcalls simultaneously from
385      * 'dpif', each port can have multiple Netlink sockets, one per upcall
386      * handler.  So, handlers_set() is responsible for the following tasks:
387      *
388      *    When receiving upcall is enabled, extends or creates the
389      *    configuration to support:
390      *
391      *        - 'n_handlers' Netlink sockets for each port.
392      *
393      *        - 'n_handlers' poll loops, one for each upcall handler.
394      *
395      *        - registering the Netlink sockets for the same upcall handler to
396      *          the corresponding poll loop.
397      * */
398     int (*handlers_set)(struct dpif *dpif, uint32_t n_handlers);
399
400     /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a
401      * priority value used for setting packet priority. */
402     int (*queue_to_priority)(const struct dpif *dpif, uint32_t queue_id,
403                              uint32_t *priority);
404
405     /* Polls for an upcall from 'dpif' for an upcall handler.  Since there
406      * can be multiple poll loops (see ->handlers_set()), 'handler_id' is
407      * needed as index to identify the corresponding poll loop.  If
408      * successful, stores the upcall into '*upcall', using 'buf' for
409      * storage.  Should only be called if 'recv_set' has been used to enable
410      * receiving packets from 'dpif'.
411      *
412      * The implementation should point 'upcall->key' and 'upcall->userdata'
413      * (if any) into data in the caller-provided 'buf'.  The implementation may
414      * also use 'buf' for storing the data of 'upcall->packet'.  If necessary
415      * to make room, the implementation may reallocate the data in 'buf'.
416      *
417      * The caller owns the data of 'upcall->packet' and may modify it.  If
418      * packet's headroom is exhausted as it is manipulated, 'upcall->packet'
419      * will be reallocated.  This requires the data of 'upcall->packet' to be
420      * released with ofpbuf_uninit() before 'upcall' is destroyed.  However,
421      * when an error is returned, the 'upcall->packet' may be uninitialized
422      * and should not be released.
423      *
424      * This function must not block.  If no upcall is pending when it is
425      * called, it should return EAGAIN without blocking. */
426     int (*recv)(struct dpif *dpif, uint32_t handler_id,
427                 struct dpif_upcall *upcall, struct ofpbuf *buf);
428
429     /* Arranges for the poll loop for an upcall handler to wake up when 'dpif'
430      * has a message queued to be received with the recv member functions.
431      * Since there can be multiple poll loops (see ->handlers_set()),
432      * 'handler_id' is needed as index to identify the corresponding poll loop.
433      * */
434     void (*recv_wait)(struct dpif *dpif, uint32_t handler_id);
435
436     /* Throws away any queued upcalls that 'dpif' currently has ready to
437      * return. */
438     void (*recv_purge)(struct dpif *dpif);
439 };
440
441 extern const struct dpif_class dpif_linux_class;
442 extern const struct dpif_class dpif_netdev_class;
443
444 #ifdef  __cplusplus
445 }
446 #endif
447
448 #endif /* dpif-provider.h */