2 * Copyright (c) 2009, 2010 Nicira Networks.
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 /* Performs periodic work needed by dpifs of this class, if any is
76 /* Arranges for poll_block() to wake up if the "run" member function needs
80 /* Enumerates the names of all known created datapaths, if possible, into
81 * 'all_dps'. The caller has already initialized 'all_dps' and other dpif
82 * classes might already have added names to it.
84 * This is used by the vswitch at startup, so that it can delete any
85 * datapaths that are not configured.
87 * Some kinds of datapaths might not be practically enumerable, in which
88 * case this function may be a null pointer. */
89 int (*enumerate)(struct svec *all_dps);
91 /* Attempts to open an existing dpif called 'name', if 'create' is false,
92 * or to open an existing dpif or create a new one, if 'create' is true.
94 * 'dpif_class' is the class of dpif to open.
96 * If successful, stores a pointer to the new dpif in '*dpifp', which must
97 * have class 'dpif_class'. On failure there are no requirements on what
98 * is stored in '*dpifp'. */
99 int (*open)(const struct dpif_class *dpif_class,
100 const char *name, bool create, struct dpif **dpifp);
102 /* Closes 'dpif' and frees associated memory. */
103 void (*close)(struct dpif *dpif);
105 /* Enumerates all names that may be used to open 'dpif' into 'all_names'.
106 * The Linux datapath, for example, supports opening a datapath both by
107 * number, e.g. "dp0", and by the name of the datapath's local port. For
108 * some datapaths, this might be an infinite set (e.g. in a file name,
109 * slashes may be duplicated any number of times), in which case only the
110 * names most likely to be used should be enumerated.
112 * The caller has already initialized 'all_names' and might already have
113 * added some names to it. This function should not disturb any existing
114 * names in 'all_names'.
116 * If a datapath class does not support multiple names for a datapath, this
117 * function may be a null pointer.
119 * This is used by the vswitch at startup, */
120 int (*get_all_names)(const struct dpif *dpif, struct svec *all_names);
122 /* Attempts to destroy the dpif underlying 'dpif'.
124 * If successful, 'dpif' will not be used again except as an argument for
125 * the 'close' member function. */
126 int (*destroy)(struct dpif *dpif);
128 /* Retrieves statistics for 'dpif' into 'stats'. */
129 int (*get_stats)(const struct dpif *dpif, struct odp_stats *stats);
131 /* Retrieves 'dpif''s current treatment of IP fragments into '*drop_frags':
132 * true indicates that fragments are dropped, false indicates that
133 * fragments are treated in the same way as other IP packets (except that
134 * the L4 header cannot be read). */
135 int (*get_drop_frags)(const struct dpif *dpif, bool *drop_frags);
137 /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose
138 * meaning is the same as for the get_drop_frags member function. */
139 int (*set_drop_frags)(struct dpif *dpif, bool drop_frags);
141 /* Adds 'netdev' as a new port in 'dpif'. If successful, sets '*port_no'
142 * to the new port's port number. */
143 int (*port_add)(struct dpif *dpif, struct netdev *netdev,
146 /* Removes port numbered 'port_no' from 'dpif'. */
147 int (*port_del)(struct dpif *dpif, uint16_t port_no);
149 /* Queries 'dpif' for a port with the given 'port_no' or 'devname'. Stores
150 * information about the port into '*port' if successful. */
151 int (*port_query_by_number)(const struct dpif *dpif, uint16_t port_no,
152 struct odp_port *port);
153 int (*port_query_by_name)(const struct dpif *dpif, const char *devname,
154 struct odp_port *port);
156 /* Attempts to begin dumping the ports in a dpif. On success, returns 0
157 * and initializes '*statep' with any data needed for iteration. On
158 * failure, returns a positive errno value. */
159 int (*port_dump_start)(const struct dpif *dpif, void **statep);
161 /* Attempts to retrieve another port from 'dpif' for 'state', which was
162 * initialized by a successful call to the 'port_dump_start' function for
163 * 'dpif'. On success, stores a new odp_port into 'port' and returns 0.
164 * Returns EOF if the end of the port table has been reached, or a positive
165 * errno value on error. This function will not be called again once it
166 * returns nonzero once for a given iteration (but the 'port_dump_done'
167 * function will be called afterward). */
168 int (*port_dump_next)(const struct dpif *dpif, void *state,
169 struct odp_port *port);
171 /* Releases resources from 'dpif' for 'state', which was initialized by a
172 * successful call to the 'port_dump_start' function for 'dpif'. */
173 int (*port_dump_done)(const struct dpif *dpif, void *state);
175 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
176 * 'dpif' has changed, then this function should do one of the
179 * - Preferably: store the name of the device that was added to or deleted
180 * from 'dpif' in '*devnamep' and return 0. The caller is responsible
181 * for freeing '*devnamep' (with free()) when it no longer needs it.
183 * - Alternatively: return ENOBUFS, without indicating the device that was
186 * Occasional 'false positives', in which the function returns 0 while
187 * indicating a device that was not actually added or deleted or returns
188 * ENOBUFS without any change, are acceptable.
190 * If the set of ports in 'dpif' has not changed, returns EAGAIN. May also
191 * return other positive errno values to indicate that something has gone
193 int (*port_poll)(const struct dpif *dpif, char **devnamep);
195 /* Arranges for the poll loop to wake up when 'port_poll' will return a
196 * value other than EAGAIN. */
197 void (*port_poll_wait)(const struct dpif *dpif);
199 /* For each flow 'flow' in the 'n' flows in 'flows':
201 * - If a flow matching 'flow->key' exists in 'dpif':
203 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
204 * into 'flow->stats'.
206 * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If
207 * 'flow->n_actions' is nonzero, then 'flow->actions' should point to
208 * an array of the specified number of actions. At most that many of
209 * the flow's actions will be copied into that array.
210 * 'flow->n_actions' will be updated to the number of actions actually
211 * present in the flow, which may be greater than the number stored if
212 * the flow has more actions than space available in the array.
214 * - Flow-specific errors are indicated by a positive errno value in
215 * 'flow->stats.error'. In particular, ENOENT indicates that no flow
216 * matching 'flow->key' exists in 'dpif'. When an error value is stored,
217 * the contents of 'flow->key' are preserved but other members of 'flow'
218 * should be treated as indeterminate.
220 * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
221 * individually successful or not is indicated by 'flow->stats.error',
222 * however). Returns a positive errno value if an error that prevented
223 * this update occurred, in which the caller must not depend on any
224 * elements in 'flows' being updated or not updated.
226 int (*flow_get)(const struct dpif *dpif, struct odp_flow flows[], int n);
228 /* Adds or modifies a flow in 'dpif' as specified in 'put':
230 * - If the flow specified in 'put->flow' does not exist in 'dpif', then
231 * behavior depends on whether ODPPF_CREATE is specified in 'put->flags':
232 * if it is, the flow will be added, otherwise the operation will fail
235 * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'.
236 * Behavior in this case depends on whether ODPPF_MODIFY is specified in
237 * 'put->flags': if it is, the flow's actions will be updated, otherwise
238 * the operation will fail with EEXIST. If the flow's actions are
239 * updated, then its statistics will be zeroed if ODPPF_ZERO_STATS is set
240 * in 'put->flags', left as-is otherwise.
242 int (*flow_put)(struct dpif *dpif, struct odp_flow_put *put);
244 /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if
245 * 'dpif' does not contain such a flow.
247 * If successful, updates 'flow->stats', 'flow->n_actions', and
248 * 'flow->actions' as described in more detail under the flow_get member
250 int (*flow_del)(struct dpif *dpif, struct odp_flow *flow);
252 /* Deletes all flows from 'dpif' and clears all of its queues of received
254 int (*flow_flush)(struct dpif *dpif);
256 /* Attempts to begin dumping the flows in a dpif. On success, returns 0
257 * and initializes '*statep' with any data needed for iteration. On
258 * failure, returns a positive errno value. */
259 int (*flow_dump_start)(const struct dpif *dpif, void **statep);
261 /* Attempts to retrieve another flow from 'dpif' for 'state', which was
262 * initialized by a successful call to the 'flow_dump_start' function for
263 * 'dpif'. On success, stores a new odp_flow into 'flow' and returns 0.
264 * Returns EOF if the end of the flow table has been reached, or a positive
265 * errno value on error. This function will not be called again once it
266 * returns nonzero once for a given iteration (but the 'flow_dump_done'
267 * function will be called afterward).
269 * Dumping flow actions is optional. If the caller does not want to dump
270 * actions it will initialize 'flow->actions' to NULL and
271 * 'flow->actions_len' to 0. Otherwise, 'flow->actions' points to an array
272 * of struct nlattr and 'flow->actions_len' contains the number of bytes of
273 * Netlink attributes. The implemention should fill in as many actions as
274 * will fit into the provided array and update 'flow->actions_len' with the
275 * number of bytes required (regardless of whether they fit in the provided
277 int (*flow_dump_next)(const struct dpif *dpif, void *state,
278 struct odp_flow *flow);
280 /* Releases resources from 'dpif' for 'state', which was initialized by a
281 * successful call to the 'flow_dump_start' function for 'dpif'. */
282 int (*flow_dump_done)(const struct dpif *dpif, void *state);
284 /* Performs the 'actions_len' bytes of actions in 'actions' on the Ethernet
285 * frame specified in 'packet'. */
286 int (*execute)(struct dpif *dpif, const struct nlattr *actions,
287 size_t actions_len, const struct ofpbuf *packet);
289 /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. Each ODPL_* bit
290 * set in '*listen_mask' indicates the 'dpif' will receive messages of the
291 * corresponding type when it calls the recv member function. */
292 int (*recv_get_mask)(const struct dpif *dpif, int *listen_mask);
294 /* Sets 'dpif''s "listen mask" to 'listen_mask'. Each ODPL_* bit set in
295 * 'listen_mask' indicates the 'dpif' will receive messages of the
296 * corresponding type when it calls the recv member function. */
297 int (*recv_set_mask)(struct dpif *dpif, int listen_mask);
299 /* Retrieves 'dpif''s sFlow sampling probability into '*probability'.
300 * Return value is 0 or a positive errno value. EOPNOTSUPP indicates that
301 * the datapath does not support sFlow, as does a null pointer.
303 * '*probability' is expressed as the number of packets out of UINT_MAX to
304 * sample, e.g. probability/UINT_MAX is the probability of sampling a given
306 int (*get_sflow_probability)(const struct dpif *dpif,
307 uint32_t *probability);
309 /* Sets 'dpif''s sFlow sampling probability to 'probability'. Return value
310 * is 0 or a positive errno value. EOPNOTSUPP indicates that the datapath
311 * does not support sFlow, as does a null pointer.
313 * 'probability' is expressed as the number of packets out of UINT_MAX to
314 * sample, e.g. probability/UINT_MAX is the probability of sampling a given
316 int (*set_sflow_probability)(struct dpif *dpif, uint32_t probability);
318 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a
319 * priority value for use in the ODPAT_SET_PRIORITY action in
321 int (*queue_to_priority)(const struct dpif *dpif, uint32_t queue_id,
324 /* Polls for an upcall from 'dpif'. If successful, stores the upcall into
325 * '*upcall'. Only upcalls of the types selected with the set_listen_mask
326 * member function should be received.
328 * The caller takes ownership of the data that 'upcall' points to.
329 * 'upcall->key' and 'upcall->actions' (if nonnull) point into data owned
330 * by 'upcall->packet', so their memory cannot be freed separately. (This
331 * is hardly a great way to do things but it works out OK for the dpif
332 * providers that exist so far.)
334 * For greatest efficiency, 'upcall->packet' should have at least
335 * offsetof(struct ofp_packet_in, data) bytes of headroom.
337 * This function must not block. If no upcall is pending when it is
338 * called, it should return EAGAIN without blocking. */
339 int (*recv)(struct dpif *dpif, struct dpif_upcall *upcall);
341 /* Arranges for the poll loop to wake up when 'dpif' has a message queued
342 * to be received with the recv member function. */
343 void (*recv_wait)(struct dpif *dpif);
346 extern const struct dpif_class dpif_linux_class;
347 extern const struct dpif_class dpif_netdev_class;
353 #endif /* dpif-provider.h */