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