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 struct ct_dpif_dump_state
;
80 /* Datapath interface class structure, to be defined by each implementation of
81 * a datapath interface.
83 * These functions return 0 if successful or a positive errno value on failure,
84 * except where otherwise noted.
86 * These functions are expected to execute synchronously, that is, to block as
87 * necessary to obtain a result. Thus, they may not return EAGAIN or
88 * EWOULDBLOCK or EINPROGRESS. We may relax this requirement in the future if
89 * and when we encounter performance problems. */
91 /* Type of dpif in this class, e.g. "system", "netdev", etc.
93 * One of the providers should supply a "system" type, since this is
94 * the type assumed if no type is specified when opening a dpif. */
97 /* Called when the dpif provider is registered, typically at program
98 * startup. Returning an error from this function will prevent any
99 * datapath with this class from being created.
101 * This function may be set to null if a datapath class needs no
102 * initialization at registration time. */
105 /* Enumerates the names of all known created datapaths (of class
106 * 'dpif_class'), if possible, into 'all_dps'. The caller has already
107 * initialized 'all_dps' and other dpif classes might already have added
110 * This is used by the vswitch at startup, so that it can delete any
111 * datapaths that are not configured.
113 * Some kinds of datapaths might not be practically enumerable, in which
114 * case this function may be a null pointer. */
115 int (*enumerate
)(struct sset
*all_dps
, const struct dpif_class
*dpif_class
);
117 /* Returns the type to pass to netdev_open() when a dpif of class
118 * 'dpif_class' has a port of type 'type', for a few special cases
119 * when a netdev type differs from a port type. For example, when
120 * using the userspace datapath, a port of type "internal" needs to
121 * be opened as "tap".
123 * Returns either 'type' itself or a string literal, which must not
125 const char *(*port_open_type
)(const struct dpif_class
*dpif_class
,
128 /* Attempts to open an existing dpif called 'name', if 'create' is false,
129 * or to open an existing dpif or create a new one, if 'create' is true.
131 * 'dpif_class' is the class of dpif to open.
133 * If successful, stores a pointer to the new dpif in '*dpifp', which must
134 * have class 'dpif_class'. On failure there are no requirements on what
135 * is stored in '*dpifp'. */
136 int (*open
)(const struct dpif_class
*dpif_class
,
137 const char *name
, bool create
, struct dpif
**dpifp
);
139 /* Closes 'dpif' and frees associated memory. */
140 void (*close
)(struct dpif
*dpif
);
142 /* Attempts to destroy the dpif underlying 'dpif'.
144 * If successful, 'dpif' will not be used again except as an argument for
145 * the 'close' member function. */
146 int (*destroy
)(struct dpif
*dpif
);
148 /* Performs periodic work needed by 'dpif', if any is necessary.
149 * Returns true if need to revalidate. */
150 bool (*run
)(struct dpif
*dpif
);
152 /* Arranges for poll_block() to wake up if the "run" member function needs
153 * to be called for 'dpif'. */
154 void (*wait
)(struct dpif
*dpif
);
156 /* Retrieves statistics for 'dpif' into 'stats'. */
157 int (*get_stats
)(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
);
159 /* Adds 'netdev' as a new port in 'dpif'. If '*port_no' is not
160 * ODPP_NONE, attempts to use that as the port's port number.
162 * If port is successfully added, sets '*port_no' to the new port's
163 * port number. Returns EBUSY if caller attempted to choose a port
164 * number, and it was in use. */
165 int (*port_add
)(struct dpif
*dpif
, struct netdev
*netdev
,
166 odp_port_t
*port_no
);
168 /* Removes port numbered 'port_no' from 'dpif'. */
169 int (*port_del
)(struct dpif
*dpif
, odp_port_t port_no
);
171 /* Refreshes configuration of 'dpif's port. The implementation might
172 * postpone applying the changes until run() is called. */
173 int (*port_set_config
)(struct dpif
*dpif
, odp_port_t port_no
,
174 const struct smap
*cfg
);
176 /* Queries 'dpif' for a port with the given 'port_no' or 'devname'.
177 * If 'port' is not null, stores information about the port into
178 * '*port' if successful.
180 * If the port doesn't exist, the provider must return ENODEV. Other
181 * error numbers means that something wrong happened and will be
182 * treated differently by upper layers.
184 * If 'port' is not null, the caller takes ownership of data in
185 * 'port' and must free it with dpif_port_destroy() when it is no
187 int (*port_query_by_number
)(const struct dpif
*dpif
, odp_port_t port_no
,
188 struct dpif_port
*port
);
189 int (*port_query_by_name
)(const struct dpif
*dpif
, const char *devname
,
190 struct dpif_port
*port
);
192 /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
193 * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
194 * flows whose packets arrived on port 'port_no'. In the case where the
195 * provider allocates multiple Netlink PIDs to a single port, it may use
196 * 'hash' to spread load among them. The caller need not use a particular
197 * hash function; a 5-tuple hash is suitable.
199 * (The datapath implementation might use some different hash function for
200 * distributing packets received via flow misses among PIDs. This means
201 * that packets received via flow misses might be reordered relative to
202 * packets received via userspace actions. This is not ordinarily a
205 * A 'port_no' of UINT32_MAX should be treated as a special case. The
206 * implementation should return a reserved PID, not allocated to any port,
207 * that the client may use for special purposes.
209 * The return value only needs to be meaningful when DPIF_UC_ACTION has
210 * been enabled in the 'dpif''s listen mask, and it is allowed to change
211 * when DPIF_UC_ACTION is disabled and then re-enabled.
213 * A dpif provider that doesn't have meaningful Netlink PIDs can use NULL
214 * for this function. This is equivalent to always returning 0. */
215 uint32_t (*port_get_pid
)(const struct dpif
*dpif
, odp_port_t port_no
,
218 /* Attempts to begin dumping the ports in a dpif. On success, returns 0
219 * and initializes '*statep' with any data needed for iteration. On
220 * failure, returns a positive errno value. */
221 int (*port_dump_start
)(const struct dpif
*dpif
, void **statep
);
223 /* Attempts to retrieve another port from 'dpif' for 'state', which was
224 * initialized by a successful call to the 'port_dump_start' function for
225 * 'dpif'. On success, stores a new dpif_port into 'port' and returns 0.
226 * Returns EOF if the end of the port table has been reached, or a positive
227 * errno value on error. This function will not be called again once it
228 * returns nonzero once for a given iteration (but the 'port_dump_done'
229 * function will be called afterward).
231 * The dpif provider retains ownership of the data stored in 'port'. It
232 * must remain valid until at least the next call to 'port_dump_next' or
233 * 'port_dump_done' for 'state'. */
234 int (*port_dump_next
)(const struct dpif
*dpif
, void *state
,
235 struct dpif_port
*port
);
237 /* Releases resources from 'dpif' for 'state', which was initialized by a
238 * successful call to the 'port_dump_start' function for 'dpif'. */
239 int (*port_dump_done
)(const struct dpif
*dpif
, void *state
);
241 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
242 * 'dpif' has changed, then this function should do one of the
245 * - Preferably: store the name of the device that was added to or deleted
246 * from 'dpif' in '*devnamep' and return 0. The caller is responsible
247 * for freeing '*devnamep' (with free()) when it no longer needs it.
249 * - Alternatively: return ENOBUFS, without indicating the device that was
252 * Occasional 'false positives', in which the function returns 0 while
253 * indicating a device that was not actually added or deleted or returns
254 * ENOBUFS without any change, are acceptable.
256 * If the set of ports in 'dpif' has not changed, returns EAGAIN. May also
257 * return other positive errno values to indicate that something has gone
259 int (*port_poll
)(const struct dpif
*dpif
, char **devnamep
);
261 /* Arranges for the poll loop to wake up when 'port_poll' will return a
262 * value other than EAGAIN. */
263 void (*port_poll_wait
)(const struct dpif
*dpif
);
265 /* Deletes all flows from 'dpif' and clears all of its queues of received
267 int (*flow_flush
)(struct dpif
*dpif
);
269 /* Flow dumping interface.
271 * This is the back-end for the flow dumping interface described in
272 * dpif.h. Please read the comments there first, because this code
273 * closely follows it.
275 * 'flow_dump_create' and 'flow_dump_thread_create' must always return an
276 * initialized and usable data structure and defer error return until
277 * flow_dump_destroy(). This hasn't been a problem for the dpifs that
280 * 'flow_dump_create' and 'flow_dump_thread_create' must initialize the
281 * structures that they return with dpif_flow_dump_init() and
282 * dpif_flow_dump_thread_init(), respectively.
284 * If 'terse' is true, then only UID and statistics will
285 * be returned in the dump. Otherwise, all fields will be returned.
287 * If 'type' isn't null, dumps only the flows of the given type. */
288 struct dpif_flow_dump
*(*flow_dump_create
)(const struct dpif
*dpif
,
289 bool terse
, char *type
);
290 int (*flow_dump_destroy
)(struct dpif_flow_dump
*dump
);
292 struct dpif_flow_dump_thread
*(*flow_dump_thread_create
)(
293 struct dpif_flow_dump
*dump
);
294 void (*flow_dump_thread_destroy
)(struct dpif_flow_dump_thread
*thread
);
296 int (*flow_dump_next
)(struct dpif_flow_dump_thread
*thread
,
297 struct dpif_flow
*flows
, int max_flows
);
299 /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order
300 * in which they are specified, placing each operation's results in the
301 * "output" members documented in comments and the 'error' member of each
303 void (*operate
)(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
);
305 /* Enables or disables receiving packets with dpif_recv() for 'dpif'.
306 * Turning packet receive off and then back on is allowed to change Netlink
307 * PID assignments (see ->port_get_pid()). The client is responsible for
308 * updating flows as necessary if it does this. */
309 int (*recv_set
)(struct dpif
*dpif
, bool enable
);
311 /* Refreshes the poll loops and Netlink sockets associated to each port,
312 * when the number of upcall handlers (upcall receiving thread) is changed
313 * to 'n_handlers' and receiving packets for 'dpif' is enabled by
316 * Since multiple upcall handlers can read upcalls simultaneously from
317 * 'dpif', each port can have multiple Netlink sockets, one per upcall
318 * handler. So, handlers_set() is responsible for the following tasks:
320 * When receiving upcall is enabled, extends or creates the
321 * configuration to support:
323 * - 'n_handlers' Netlink sockets for each port.
325 * - 'n_handlers' poll loops, one for each upcall handler.
327 * - registering the Netlink sockets for the same upcall handler to
328 * the corresponding poll loop.
330 int (*handlers_set
)(struct dpif
*dpif
, uint32_t n_handlers
);
332 /* Pass custom configuration options to the datapath. The implementation
333 * might postpone applying the changes until run() is called. */
334 int (*set_config
)(struct dpif
*dpif
, const struct smap
*other_config
);
336 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a
337 * priority value used for setting packet priority. */
338 int (*queue_to_priority
)(const struct dpif
*dpif
, uint32_t queue_id
,
341 /* Polls for an upcall from 'dpif' for an upcall handler. Since there
342 * can be multiple poll loops (see ->handlers_set()), 'handler_id' is
343 * needed as index to identify the corresponding poll loop. If
344 * successful, stores the upcall into '*upcall', using 'buf' for
345 * storage. Should only be called if 'recv_set' has been used to enable
346 * receiving packets from 'dpif'.
348 * The implementation should point 'upcall->key' and 'upcall->userdata'
349 * (if any) into data in the caller-provided 'buf'. The implementation may
350 * also use 'buf' for storing the data of 'upcall->packet'. If necessary
351 * to make room, the implementation may reallocate the data in 'buf'.
353 * The caller owns the data of 'upcall->packet' and may modify it. If
354 * packet's headroom is exhausted as it is manipulated, 'upcall->packet'
355 * will be reallocated. This requires the data of 'upcall->packet' to be
356 * released with ofpbuf_uninit() before 'upcall' is destroyed. However,
357 * when an error is returned, the 'upcall->packet' may be uninitialized
358 * and should not be released.
360 * This function must not block. If no upcall is pending when it is
361 * called, it should return EAGAIN without blocking. */
362 int (*recv
)(struct dpif
*dpif
, uint32_t handler_id
,
363 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
);
365 /* Arranges for the poll loop for an upcall handler to wake up when 'dpif'
366 * has a message queued to be received with the recv member functions.
367 * Since there can be multiple poll loops (see ->handlers_set()),
368 * 'handler_id' is needed as index to identify the corresponding poll loop.
370 void (*recv_wait
)(struct dpif
*dpif
, uint32_t handler_id
);
372 /* Throws away any queued upcalls that 'dpif' currently has ready to
374 void (*recv_purge
)(struct dpif
*dpif
);
376 /* When 'dpif' is about to purge the datapath, the higher layer may want
377 * to be notified so that it could try reacting accordingly (e.g. grabbing
378 * all flow stats before they are gone).
380 * Registers an upcall callback function with 'dpif'. This is only used
381 * if 'dpif' needs to notify the purging of datapath. 'aux' is passed to
382 * the callback on invocation. */
383 void (*register_dp_purge_cb
)(struct dpif
*, dp_purge_callback
*, void *aux
);
385 /* For datapaths that run in userspace (i.e. dpif-netdev), threads polling
386 * for incoming packets can directly call upcall functions instead of
387 * offloading packet processing to separate handler threads. Datapaths
388 * that directly call upcall functions should use the functions below to
389 * to register an upcall function and enable / disable upcalls.
391 * Registers an upcall callback function with 'dpif'. This is only used
392 * if 'dpif' directly executes upcall functions. 'aux' is passed to the
393 * callback on invocation. */
394 void (*register_upcall_cb
)(struct dpif
*, upcall_callback
*, void *aux
);
396 /* Enables upcalls if 'dpif' directly executes upcall functions. */
397 void (*enable_upcall
)(struct dpif
*);
399 /* Disables upcalls if 'dpif' directly executes upcall functions. */
400 void (*disable_upcall
)(struct dpif
*);
402 /* Get datapath version. Caller is responsible for freeing the string
404 char *(*get_datapath_version
)(void);
406 /* Conntrack entry dumping interface.
408 * These functions are used by ct-dpif.c to provide a datapath-agnostic
409 * dumping interface to the connection trackers provided by the
412 * ct_dump_start() should put in '*state' a pointer to a newly allocated
413 * stucture that will be passed by the caller to ct_dump_next() and
414 * ct_dump_done(). If 'zone' is not NULL, only the entries in '*zone'
417 * ct_dump_next() should fill 'entry' with information from a connection
418 * and prepare to dump the next one on a subsequest invocation.
420 * ct_dump_done() should perform any cleanup necessary (including
421 * deallocating the 'state' structure, if applicable). */
422 int (*ct_dump_start
)(struct dpif
*, struct ct_dpif_dump_state
**state
,
423 const uint16_t *zone
, int *);
424 int (*ct_dump_next
)(struct dpif
*, struct ct_dpif_dump_state
*state
,
425 struct ct_dpif_entry
*entry
);
426 int (*ct_dump_done
)(struct dpif
*, struct ct_dpif_dump_state
*state
);
428 /* Flushes the connection tracking tables. The arguments have the
429 * following behavior:
431 * - If both 'zone' and 'tuple' are NULL, flush all the conntrack
433 * - If 'zone' is not NULL, and 'tuple' is NULL, flush all the
434 * conntrack entries in '*zone'.
435 * - If 'tuple' is not NULL, flush the conntrack entry specified by
436 * 'tuple' in '*zone'. If 'zone' is NULL, use the default zone
438 int (*ct_flush
)(struct dpif
*, const uint16_t *zone
,
439 const struct ct_dpif_tuple
*tuple
);
440 /* Set max connections allowed. */
441 int (*ct_set_maxconns
)(struct dpif
*, uint32_t maxconns
);
442 /* Get max connections allowed. */
443 int (*ct_get_maxconns
)(struct dpif
*, uint32_t *maxconns
);
444 /* Get number of connections tracked. */
445 int (*ct_get_nconns
)(struct dpif
*, uint32_t *nconns
);
449 /* Queries 'dpif' for supported meter features.
450 * NULL pointer means no meter features are supported. */
451 void (*meter_get_features
)(const struct dpif
*,
452 struct ofputil_meter_features
*);
454 /* Adds or modifies 'meter' in 'dpif'. If '*meter_id' is UINT32_MAX,
455 * adds a new meter, otherwise modifies an existing meter.
457 * If meter is successfully added, sets '*meter_id' to the new meter's
458 * meter id selected by 'dpif'. */
459 int (*meter_set
)(struct dpif
*, ofproto_meter_id
*meter_id
,
460 struct ofputil_meter_config
*);
462 /* Queries 'dpif' for meter stats with the given 'meter_id'. Stores
463 * maximum of 'n_bands' meter statistics, returning the number of band
464 * stats returned in 'stats->n_bands' if successful. */
465 int (*meter_get
)(const struct dpif
*, ofproto_meter_id meter_id
,
466 struct ofputil_meter_stats
*, uint16_t n_bands
);
468 /* Removes meter 'meter_id' from 'dpif'. Stores meter and band statistics
469 * (for maximum of 'n_bands', returning the number of band stats returned
470 * in 'stats->n_bands' if successful. 'stats' may be passed in as NULL if
471 * no stats are needed, in which case 'n_bands' must be passed in as
473 int (*meter_del
)(struct dpif
*, ofproto_meter_id meter_id
,
474 struct ofputil_meter_stats
*, uint16_t n_bands
);
477 extern const struct dpif_class dpif_netlink_class
;
478 extern const struct dpif_class dpif_netdev_class
;
484 #endif /* dpif-provider.h */