2 * Copyright (c) 2009, 2010, 2011, 2012 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. */
26 #include "openflow/openflow.h"
34 /* Open vSwitch datapath interface.
36 * This structure should be treated as opaque by dpif implementations. */
38 const struct dpif_class *dpif_class;
41 uint8_t netflow_engine_type;
42 uint8_t netflow_engine_id;
45 void dpif_init(struct dpif *, const struct dpif_class *, const char *name,
46 uint8_t netflow_engine_type, uint8_t netflow_engine_id);
47 void dpif_uninit(struct dpif *dpif, bool close);
49 static inline void dpif_assert_class(const struct dpif *dpif,
50 const struct dpif_class *dpif_class)
52 assert(dpif->dpif_class == dpif_class);
55 /* Datapath interface class structure, to be defined by each implementation of
56 * a datapath interface.
58 * These functions return 0 if successful or a positive errno value on failure,
59 * except where otherwise noted.
61 * These functions are expected to execute synchronously, that is, to block as
62 * necessary to obtain a result. Thus, they may not return EAGAIN or
63 * EWOULDBLOCK or EINPROGRESS. We may relax this requirement in the future if
64 * and when we encounter performance problems. */
66 /* Type of dpif in this class, e.g. "system", "netdev", etc.
68 * One of the providers should supply a "system" type, since this is
69 * the type assumed if no type is specified when opening a dpif. */
72 /* Enumerates the names of all known created datapaths, if possible, into
73 * 'all_dps'. The caller has already initialized 'all_dps' and other dpif
74 * classes might already have added names to it.
76 * This is used by the vswitch at startup, so that it can delete any
77 * datapaths that are not configured.
79 * Some kinds of datapaths might not be practically enumerable, in which
80 * case this function may be a null pointer. */
81 int (*enumerate)(struct sset *all_dps);
83 /* Attempts to open an existing dpif called 'name', if 'create' is false,
84 * or to open an existing dpif or create a new one, if 'create' is true.
86 * 'dpif_class' is the class of dpif to open.
88 * If successful, stores a pointer to the new dpif in '*dpifp', which must
89 * have class 'dpif_class'. On failure there are no requirements on what
90 * is stored in '*dpifp'. */
91 int (*open)(const struct dpif_class *dpif_class,
92 const char *name, bool create, struct dpif **dpifp);
94 /* Closes 'dpif' and frees associated memory. */
95 void (*close)(struct dpif *dpif);
97 /* Attempts to destroy the dpif underlying 'dpif'.
99 * If successful, 'dpif' will not be used again except as an argument for
100 * the 'close' member function. */
101 int (*destroy)(struct dpif *dpif);
103 /* Performs periodic work needed by 'dpif', if any is necessary. */
104 void (*run)(struct dpif *dpif);
106 /* Arranges for poll_block() to wake up if the "run" member function needs
107 * to be called for 'dpif'. */
108 void (*wait)(struct dpif *dpif);
110 /* Retrieves statistics for 'dpif' into 'stats'. */
111 int (*get_stats)(const struct dpif *dpif, struct dpif_dp_stats *stats);
113 /* Adds 'netdev' as a new port in 'dpif'. If successful, sets '*port_no'
114 * to the new port's port number. */
115 int (*port_add)(struct dpif *dpif, struct netdev *netdev,
118 /* Removes port numbered 'port_no' from 'dpif'. */
119 int (*port_del)(struct dpif *dpif, uint16_t port_no);
121 /* Queries 'dpif' for a port with the given 'port_no' or 'devname'. Stores
122 * information about the port into '*port' if successful.
124 * The caller takes ownership of data in 'port' and must free it with
125 * dpif_port_destroy() when it is no longer needed. */
126 int (*port_query_by_number)(const struct dpif *dpif, uint16_t port_no,
127 struct dpif_port *port);
128 int (*port_query_by_name)(const struct dpif *dpif, const char *devname,
129 struct dpif_port *port);
131 /* Returns one greater than the largest port number accepted in flow
133 int (*get_max_ports)(const struct dpif *dpif);
135 /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
136 * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
137 * flows whose packets arrived on port 'port_no'.
139 * A 'port_no' of UINT16_MAX should be treated as a special case. The
140 * implementation should return a reserved PID, not allocated to any port,
141 * that the client may use for special purposes.
143 * The return value only needs to be meaningful when DPIF_UC_ACTION has
144 * been enabled in the 'dpif''s listen mask, and it is allowed to change
145 * when DPIF_UC_ACTION is disabled and then re-enabled.
147 * A dpif provider that doesn't have meaningful Netlink PIDs can use NULL
148 * for this function. This is equivalent to always returning 0. */
149 uint32_t (*port_get_pid)(const struct dpif *dpif, uint16_t port_no);
151 /* Attempts to begin dumping the ports in a dpif. On success, returns 0
152 * and initializes '*statep' with any data needed for iteration. On
153 * failure, returns a positive errno value. */
154 int (*port_dump_start)(const struct dpif *dpif, void **statep);
156 /* Attempts to retrieve another port from 'dpif' for 'state', which was
157 * initialized by a successful call to the 'port_dump_start' function for
158 * 'dpif'. On success, stores a new dpif_port into 'port' and returns 0.
159 * Returns EOF if the end of the port table has been reached, or a positive
160 * errno value on error. This function will not be called again once it
161 * returns nonzero once for a given iteration (but the 'port_dump_done'
162 * function will be called afterward).
164 * The dpif provider retains ownership of the data stored in 'port'. It
165 * must remain valid until at least the next call to 'port_dump_next' or
166 * 'port_dump_done' for 'state'. */
167 int (*port_dump_next)(const struct dpif *dpif, void *state,
168 struct dpif_port *port);
170 /* Releases resources from 'dpif' for 'state', which was initialized by a
171 * successful call to the 'port_dump_start' function for 'dpif'. */
172 int (*port_dump_done)(const struct dpif *dpif, void *state);
174 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
175 * 'dpif' has changed, then this function should do one of the
178 * - Preferably: store the name of the device that was added to or deleted
179 * from 'dpif' in '*devnamep' and return 0. The caller is responsible
180 * for freeing '*devnamep' (with free()) when it no longer needs it.
182 * - Alternatively: return ENOBUFS, without indicating the device that was
185 * Occasional 'false positives', in which the function returns 0 while
186 * indicating a device that was not actually added or deleted or returns
187 * ENOBUFS without any change, are acceptable.
189 * If the set of ports in 'dpif' has not changed, returns EAGAIN. May also
190 * return other positive errno values to indicate that something has gone
192 int (*port_poll)(const struct dpif *dpif, char **devnamep);
194 /* Arranges for the poll loop to wake up when 'port_poll' will return a
195 * value other than EAGAIN. */
196 void (*port_poll_wait)(const struct dpif *dpif);
198 /* Queries 'dpif' for a flow entry. The flow is specified by the Netlink
199 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
202 * Returns 0 if successful. If no flow matches, returns ENOENT. On other
203 * failure, returns a positive errno value.
205 * If 'actionsp' is nonnull, then on success '*actionsp' must be set to an
206 * ofpbuf owned by the caller that contains the Netlink attributes for the
207 * flow's actions. The caller must free the ofpbuf (with ofpbuf_delete())
208 * when it is no longer needed.
210 * If 'stats' is nonnull, then on success it must be updated with the
211 * flow's statistics. */
212 int (*flow_get)(const struct dpif *dpif,
213 const struct nlattr *key, size_t key_len,
214 struct ofpbuf **actionsp, struct dpif_flow_stats *stats);
216 /* Adds or modifies a flow in 'dpif'. The flow is specified by the Netlink
217 * attributes with types OVS_KEY_ATTR_* in the 'put->key_len' bytes
218 * starting at 'put->key'. The associated actions are specified by the
219 * Netlink attributes with types OVS_ACTION_ATTR_* in the
220 * 'put->actions_len' bytes starting at 'put->actions'.
222 * - If the flow's key does not exist in 'dpif', then the flow will be
223 * added if 'put->flags' includes DPIF_FP_CREATE. Otherwise the
224 * operation will fail with ENOENT.
226 * If the operation succeeds, then 'put->stats', if nonnull, must be
229 * - If the flow's key does exist in 'dpif', then the flow's actions will
230 * be updated if 'put->flags' includes DPIF_FP_MODIFY. Otherwise the
231 * operation will fail with EEXIST. If the flow's actions are updated,
232 * then its statistics will be zeroed if 'put->flags' includes
233 * DPIF_FP_ZERO_STATS, and left as-is otherwise.
235 * If the operation succeeds, then 'put->stats', if nonnull, must be set
236 * to the flow's statistics before the update.
238 int (*flow_put)(struct dpif *dpif, const struct dpif_flow_put *put);
240 /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif'
241 * does not contain such a flow. The flow is specified by the Netlink
242 * attributes with types OVS_KEY_ATTR_* in the 'del->key_len' bytes
243 * starting at 'del->key'.
245 * If the operation succeeds, then 'del->stats', if nonnull, must be set to
246 * the flow's statistics before its deletion. */
247 int (*flow_del)(struct dpif *dpif, const struct dpif_flow_del *del);
249 /* Deletes all flows from 'dpif' and clears all of its queues of received
251 int (*flow_flush)(struct dpif *dpif);
253 /* Attempts to begin dumping the flows in a dpif. On success, returns 0
254 * and initializes '*statep' with any data needed for iteration. On
255 * failure, returns a positive errno value. */
256 int (*flow_dump_start)(const struct dpif *dpif, void **statep);
258 /* Attempts to retrieve another flow from 'dpif' for 'state', which was
259 * initialized by a successful call to the 'flow_dump_start' function for
260 * 'dpif'. On success, updates the output parameters as described below
261 * and returns 0. Returns EOF if the end of the flow table has been
262 * reached, or a positive errno value on error. This function will not be
263 * called again once it returns nonzero within a given iteration (but the
264 * 'flow_dump_done' function will be called afterward).
266 * On success, if 'key' and 'key_len' are nonnull then '*key' and
267 * '*key_len' must be set to Netlink attributes with types OVS_KEY_ATTR_*
268 * representing the dumped flow's key. If 'actions' and 'actions_len' are
269 * nonnull then they should be set to Netlink attributes with types
270 * OVS_ACTION_ATTR_* representing the dumped flow's actions. If 'stats'
271 * is nonnull then it should be set to the dumped flow's statistics.
273 * All of the returned data is owned by 'dpif', not by the caller, and the
274 * caller must not modify or free it. 'dpif' must guarantee that it
275 * remains accessible and unchanging until at least the next call to
276 * 'flow_dump_next' or 'flow_dump_done' for 'state'. */
277 int (*flow_dump_next)(const struct dpif *dpif, void *state,
278 const struct nlattr **key, size_t *key_len,
279 const struct nlattr **actions, size_t *actions_len,
280 const struct dpif_flow_stats **stats);
282 /* Releases resources from 'dpif' for 'state', which was initialized by a
283 * successful call to the 'flow_dump_start' function for 'dpif'. */
284 int (*flow_dump_done)(const struct dpif *dpif, void *state);
286 /* Performs the 'execute->actions_len' bytes of actions in
287 * 'execute->actions' on the Ethernet frame specified in 'execute->packet'
288 * taken from the flow specified in the 'execute->key_len' bytes of
289 * 'execute->key'. ('execute->key' is mostly redundant with
290 * 'execute->packet', but it contains some metadata that cannot be
291 * recovered from 'execute->packet', such as tun_id and in_port.) */
292 int (*execute)(struct dpif *dpif, const struct dpif_execute *execute);
294 /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order
295 * in which they are specified, placing each operation's results in the
296 * "output" members documented in comments.
298 * This function is optional. It is only worthwhile to implement it if
299 * 'dpif' can perform operations in batch faster than individually. */
300 void (*operate)(struct dpif *dpif, struct dpif_op **ops, size_t n_ops);
302 /* Enables or disables receiving packets with dpif_recv() for 'dpif'.
303 * Turning packet receive off and then back on is allowed to change Netlink
304 * PID assignments (see ->port_get_pid()). The client is responsible for
305 * updating flows as necessary if it does this. */
306 int (*recv_set)(struct dpif *dpif, bool enable);
308 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a
309 * priority value used for setting packet priority. */
310 int (*queue_to_priority)(const struct dpif *dpif, uint32_t queue_id,
313 /* Polls for an upcall from 'dpif'. If successful, stores the upcall into
314 * '*upcall', using 'buf' for storage. Should only be called if 'recv_set'
315 * has been used to enable receiving packets from 'dpif'.
317 * The implementation should point 'upcall->packet' and 'upcall->key' into
318 * data in the caller-provided 'buf'. If necessary to make room, the
319 * implementation may expand the data in 'buf'. (This is hardly a great
320 * way to do things but it works out OK for the dpif providers that exist
323 * This function must not block. If no upcall is pending when it is
324 * called, it should return EAGAIN without blocking. */
325 int (*recv)(struct dpif *dpif, struct dpif_upcall *upcall,
328 /* Arranges for the poll loop to wake up when 'dpif' has a message queued
329 * to be received with the recv member function. */
330 void (*recv_wait)(struct dpif *dpif);
332 /* Throws away any queued upcalls that 'dpif' currently has ready to
334 void (*recv_purge)(struct dpif *dpif);
337 extern const struct dpif_class dpif_linux_class;
338 extern const struct dpif_class dpif_netdev_class;
344 #endif /* dpif-provider.h */