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