2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef DPIF_PROVIDER_H
18 #define DPIF_PROVIDER_H 1
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. */
25 #include "openflow/openflow.h"
33 /* Open vSwitch datapath interface.
35 * This structure should be treated as opaque by dpif implementations. */
37 const struct dpif_class
*dpif_class
;
40 uint8_t netflow_engine_type
;
41 uint8_t netflow_engine_id
;
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
);
48 static inline void dpif_assert_class(const struct dpif
*dpif
,
49 const struct dpif_class
*dpif_class
)
51 ovs_assert(dpif
->dpif_class
== dpif_class
);
54 struct dpif_flow_dump
{
56 bool terse
; /* If true, key/mask/actions may be omitted. */
60 dpif_flow_dump_init(struct dpif_flow_dump
*dump
, const struct dpif
*dpif
)
62 dump
->dpif
= CONST_CAST(struct dpif
*, dpif
);
65 struct dpif_flow_dump_thread
{
70 dpif_flow_dump_thread_init(struct dpif_flow_dump_thread
*thread
,
71 struct dpif_flow_dump
*dump
)
73 thread
->dpif
= dump
->dpif
;
76 /* Datapath interface class structure, to be defined by each implementation of
77 * a datapath interface.
79 * These functions return 0 if successful or a positive errno value on failure,
80 * except where otherwise noted.
82 * These functions are expected to execute synchronously, that is, to block as
83 * necessary to obtain a result. Thus, they may not return EAGAIN or
84 * EWOULDBLOCK or EINPROGRESS. We may relax this requirement in the future if
85 * and when we encounter performance problems. */
87 /* Type of dpif in this class, e.g. "system", "netdev", etc.
89 * One of the providers should supply a "system" type, since this is
90 * the type assumed if no type is specified when opening a dpif. */
93 /* Called when the dpif provider is registered, typically at program
94 * startup. Returning an error from this function will prevent any
95 * datapath with this class from being created.
97 * This function may be set to null if a datapath class needs no
98 * initialization at registration time. */
101 /* Enumerates the names of all known created datapaths (of class
102 * 'dpif_class'), if possible, into 'all_dps'. The caller has already
103 * initialized 'all_dps' and other dpif classes might already have added
106 * This is used by the vswitch at startup, so that it can delete any
107 * datapaths that are not configured.
109 * Some kinds of datapaths might not be practically enumerable, in which
110 * case this function may be a null pointer. */
111 int (*enumerate
)(struct sset
*all_dps
, const struct dpif_class
*dpif_class
);
113 /* Returns the type to pass to netdev_open() when a dpif of class
114 * 'dpif_class' has a port of type 'type', for a few special cases
115 * when a netdev type differs from a port type. For example, when
116 * using the userspace datapath, a port of type "internal" needs to
117 * be opened as "tap".
119 * Returns either 'type' itself or a string literal, which must not
121 const char *(*port_open_type
)(const struct dpif_class
*dpif_class
,
124 /* Attempts to open an existing dpif called 'name', if 'create' is false,
125 * or to open an existing dpif or create a new one, if 'create' is true.
127 * 'dpif_class' is the class of dpif to open.
129 * If successful, stores a pointer to the new dpif in '*dpifp', which must
130 * have class 'dpif_class'. On failure there are no requirements on what
131 * is stored in '*dpifp'. */
132 int (*open
)(const struct dpif_class
*dpif_class
,
133 const char *name
, bool create
, struct dpif
**dpifp
);
135 /* Closes 'dpif' and frees associated memory. */
136 void (*close
)(struct dpif
*dpif
);
138 /* Attempts to destroy the dpif underlying 'dpif'.
140 * If successful, 'dpif' will not be used again except as an argument for
141 * the 'close' member function. */
142 int (*destroy
)(struct dpif
*dpif
);
144 /* Performs periodic work needed by 'dpif', if any is necessary.
145 * Returns true if need to revalidate. */
146 bool (*run
)(struct dpif
*dpif
);
148 /* Arranges for poll_block() to wake up if the "run" member function needs
149 * to be called for 'dpif'. */
150 void (*wait
)(struct dpif
*dpif
);
152 /* Retrieves statistics for 'dpif' into 'stats'. */
153 int (*get_stats
)(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
);
155 /* Adds 'netdev' as a new port in 'dpif'. If '*port_no' is not
156 * UINT32_MAX, attempts to use that as the port's port number.
158 * If port is successfully added, sets '*port_no' to the new port's
159 * port number. Returns EBUSY if caller attempted to choose a port
160 * number, and it was in use. */
161 int (*port_add
)(struct dpif
*dpif
, struct netdev
*netdev
,
162 odp_port_t
*port_no
);
164 /* Removes port numbered 'port_no' from 'dpif'. */
165 int (*port_del
)(struct dpif
*dpif
, odp_port_t port_no
);
167 /* Queries 'dpif' for a port with the given 'port_no' or 'devname'.
168 * If 'port' is not null, stores information about the port into
169 * '*port' if successful.
171 * If 'port' is not null, the caller takes ownership of data in
172 * 'port' and must free it with dpif_port_destroy() when it is no
174 int (*port_query_by_number
)(const struct dpif
*dpif
, odp_port_t port_no
,
175 struct dpif_port
*port
);
176 int (*port_query_by_name
)(const struct dpif
*dpif
, const char *devname
,
177 struct dpif_port
*port
);
179 /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
180 * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
181 * flows whose packets arrived on port 'port_no'. In the case where the
182 * provider allocates multiple Netlink PIDs to a single port, it may use
183 * 'hash' to spread load among them. The caller need not use a particular
184 * hash function; a 5-tuple hash is suitable.
186 * (The datapath implementation might use some different hash function for
187 * distributing packets received via flow misses among PIDs. This means
188 * that packets received via flow misses might be reordered relative to
189 * packets received via userspace actions. This is not ordinarily a
192 * A 'port_no' of UINT32_MAX should be treated as a special case. The
193 * implementation should return a reserved PID, not allocated to any port,
194 * that the client may use for special purposes.
196 * The return value only needs to be meaningful when DPIF_UC_ACTION has
197 * been enabled in the 'dpif''s listen mask, and it is allowed to change
198 * when DPIF_UC_ACTION is disabled and then re-enabled.
200 * A dpif provider that doesn't have meaningful Netlink PIDs can use NULL
201 * for this function. This is equivalent to always returning 0. */
202 uint32_t (*port_get_pid
)(const struct dpif
*dpif
, odp_port_t port_no
,
205 /* Attempts to begin dumping the ports in a dpif. On success, returns 0
206 * and initializes '*statep' with any data needed for iteration. On
207 * failure, returns a positive errno value. */
208 int (*port_dump_start
)(const struct dpif
*dpif
, void **statep
);
210 /* Attempts to retrieve another port from 'dpif' for 'state', which was
211 * initialized by a successful call to the 'port_dump_start' function for
212 * 'dpif'. On success, stores a new dpif_port into 'port' and returns 0.
213 * Returns EOF if the end of the port table has been reached, or a positive
214 * errno value on error. This function will not be called again once it
215 * returns nonzero once for a given iteration (but the 'port_dump_done'
216 * function will be called afterward).
218 * The dpif provider retains ownership of the data stored in 'port'. It
219 * must remain valid until at least the next call to 'port_dump_next' or
220 * 'port_dump_done' for 'state'. */
221 int (*port_dump_next
)(const struct dpif
*dpif
, void *state
,
222 struct dpif_port
*port
);
224 /* Releases resources from 'dpif' for 'state', which was initialized by a
225 * successful call to the 'port_dump_start' function for 'dpif'. */
226 int (*port_dump_done
)(const struct dpif
*dpif
, void *state
);
228 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
229 * 'dpif' has changed, then this function should do one of the
232 * - Preferably: store the name of the device that was added to or deleted
233 * from 'dpif' in '*devnamep' and return 0. The caller is responsible
234 * for freeing '*devnamep' (with free()) when it no longer needs it.
236 * - Alternatively: return ENOBUFS, without indicating the device that was
239 * Occasional 'false positives', in which the function returns 0 while
240 * indicating a device that was not actually added or deleted or returns
241 * ENOBUFS without any change, are acceptable.
243 * If the set of ports in 'dpif' has not changed, returns EAGAIN. May also
244 * return other positive errno values to indicate that something has gone
246 int (*port_poll
)(const struct dpif
*dpif
, char **devnamep
);
248 /* Arranges for the poll loop to wake up when 'port_poll' will return a
249 * value other than EAGAIN. */
250 void (*port_poll_wait
)(const struct dpif
*dpif
);
252 /* Deletes all flows from 'dpif' and clears all of its queues of received
254 int (*flow_flush
)(struct dpif
*dpif
);
256 /* Flow dumping interface.
258 * This is the back-end for the flow dumping interface described in
259 * dpif.h. Please read the comments there first, because this code
260 * closely follows it.
262 * 'flow_dump_create' and 'flow_dump_thread_create' must always return an
263 * initialized and usable data structure and defer error return until
264 * flow_dump_destroy(). This hasn't been a problem for the dpifs that
267 * 'flow_dump_create' and 'flow_dump_thread_create' must initialize the
268 * structures that they return with dpif_flow_dump_init() and
269 * dpif_flow_dump_thread_init(), respectively.
271 * If 'terse' is true, then only UID and statistics will
272 * be returned in the dump. Otherwise, all fields will be returned. */
273 struct dpif_flow_dump
*(*flow_dump_create
)(const struct dpif
*dpif
,
275 int (*flow_dump_destroy
)(struct dpif_flow_dump
*dump
);
277 struct dpif_flow_dump_thread
*(*flow_dump_thread_create
)(
278 struct dpif_flow_dump
*dump
);
279 void (*flow_dump_thread_destroy
)(struct dpif_flow_dump_thread
*thread
);
281 int (*flow_dump_next
)(struct dpif_flow_dump_thread
*thread
,
282 struct dpif_flow
*flows
, int max_flows
);
284 /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order
285 * in which they are specified, placing each operation's results in the
286 * "output" members documented in comments and the 'error' member of each
288 void (*operate
)(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
);
290 /* Enables or disables receiving packets with dpif_recv() for 'dpif'.
291 * Turning packet receive off and then back on is allowed to change Netlink
292 * PID assignments (see ->port_get_pid()). The client is responsible for
293 * updating flows as necessary if it does this. */
294 int (*recv_set
)(struct dpif
*dpif
, bool enable
);
296 /* Refreshes the poll loops and Netlink sockets associated to each port,
297 * when the number of upcall handlers (upcall receiving thread) is changed
298 * to 'n_handlers' and receiving packets for 'dpif' is enabled by
301 * Since multiple upcall handlers can read upcalls simultaneously from
302 * 'dpif', each port can have multiple Netlink sockets, one per upcall
303 * handler. So, handlers_set() is responsible for the following tasks:
305 * When receiving upcall is enabled, extends or creates the
306 * configuration to support:
308 * - 'n_handlers' Netlink sockets for each port.
310 * - 'n_handlers' poll loops, one for each upcall handler.
312 * - registering the Netlink sockets for the same upcall handler to
313 * the corresponding poll loop.
315 int (*handlers_set
)(struct dpif
*dpif
, uint32_t n_handlers
);
317 /* If 'dpif' creates its own I/O polling threads, refreshes poll threads
318 * configuration. 'n_rxqs' configures the number of rx_queues, which
319 * are distributed among threads. 'cmask' configures the cpu mask
320 * for setting the polling threads' cpu affinity. */
321 int (*poll_threads_set
)(struct dpif
*dpif
, unsigned int n_rxqs
,
324 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a
325 * priority value used for setting packet priority. */
326 int (*queue_to_priority
)(const struct dpif
*dpif
, uint32_t queue_id
,
329 /* Polls for an upcall from 'dpif' for an upcall handler. Since there
330 * can be multiple poll loops (see ->handlers_set()), 'handler_id' is
331 * needed as index to identify the corresponding poll loop. If
332 * successful, stores the upcall into '*upcall', using 'buf' for
333 * storage. Should only be called if 'recv_set' has been used to enable
334 * receiving packets from 'dpif'.
336 * The implementation should point 'upcall->key' and 'upcall->userdata'
337 * (if any) into data in the caller-provided 'buf'. The implementation may
338 * also use 'buf' for storing the data of 'upcall->packet'. If necessary
339 * to make room, the implementation may reallocate the data in 'buf'.
341 * The caller owns the data of 'upcall->packet' and may modify it. If
342 * packet's headroom is exhausted as it is manipulated, 'upcall->packet'
343 * will be reallocated. This requires the data of 'upcall->packet' to be
344 * released with ofpbuf_uninit() before 'upcall' is destroyed. However,
345 * when an error is returned, the 'upcall->packet' may be uninitialized
346 * and should not be released.
348 * This function must not block. If no upcall is pending when it is
349 * called, it should return EAGAIN without blocking. */
350 int (*recv
)(struct dpif
*dpif
, uint32_t handler_id
,
351 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
);
353 /* Arranges for the poll loop for an upcall handler to wake up when 'dpif'
354 * has a message queued to be received with the recv member functions.
355 * Since there can be multiple poll loops (see ->handlers_set()),
356 * 'handler_id' is needed as index to identify the corresponding poll loop.
358 void (*recv_wait
)(struct dpif
*dpif
, uint32_t handler_id
);
360 /* Throws away any queued upcalls that 'dpif' currently has ready to
362 void (*recv_purge
)(struct dpif
*dpif
);
364 /* When 'dpif' is about to purge the datapath, the higher layer may want
365 * to be notified so that it could try reacting accordingly (e.g. grabbing
366 * all flow stats before they are gone).
368 * Registers an upcall callback function with 'dpif'. This is only used
369 * if 'dpif' needs to notify the purging of datapath. 'aux' is passed to
370 * the callback on invocation. */
371 void (*register_dp_purge_cb
)(struct dpif
*, dp_purge_callback
*, void *aux
);
373 /* For datapaths that run in userspace (i.e. dpif-netdev), threads polling
374 * for incoming packets can directly call upcall functions instead of
375 * offloading packet processing to separate handler threads. Datapaths
376 * that directly call upcall functions should use the functions below to
377 * to register an upcall function and enable / disable upcalls.
379 * Registers an upcall callback function with 'dpif'. This is only used
380 * if 'dpif' directly executes upcall functions. 'aux' is passed to the
381 * callback on invocation. */
382 void (*register_upcall_cb
)(struct dpif
*, upcall_callback
*, void *aux
);
384 /* Enables upcalls if 'dpif' directly executes upcall functions. */
385 void (*enable_upcall
)(struct dpif
*);
387 /* Disables upcalls if 'dpif' directly executes upcall functions. */
388 void (*disable_upcall
)(struct dpif
*);
390 /* Get datapath version. Caller is responsible for freeing the string
392 char *(*get_datapath_version
)(void);
395 extern const struct dpif_class dpif_netlink_class
;
396 extern const struct dpif_class dpif_netdev_class
;
402 #endif /* dpif-provider.h */