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