]>
Commit | Line | Data |
---|---|---|
96fba48f | 1 | /* |
1ba530f4 | 2 | * Copyright (c) 2009, 2010, 2011 Nicira Networks. |
96fba48f BP |
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 | |
bc34d060 BP |
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. */ | |
96fba48f BP |
24 | |
25 | #include <assert.h> | |
43253595 | 26 | #include "openflow/openflow.h" |
96fba48f | 27 | #include "dpif.h" |
43253595 | 28 | #include "util.h" |
96fba48f | 29 | |
1acb6baa BP |
30 | #ifdef __cplusplus |
31 | extern "C" { | |
32 | #endif | |
33 | ||
96fba48f BP |
34 | /* Open vSwitch datapath interface. |
35 | * | |
36 | * This structure should be treated as opaque by dpif implementations. */ | |
37 | struct dpif { | |
1acb6baa | 38 | const struct dpif_class *dpif_class; |
1a6f1e2a JG |
39 | char *base_name; |
40 | char *full_name; | |
96fba48f BP |
41 | uint8_t netflow_engine_type; |
42 | uint8_t netflow_engine_id; | |
43 | }; | |
44 | ||
45 | void dpif_init(struct dpif *, const struct dpif_class *, const char *name, | |
46 | uint8_t netflow_engine_type, uint8_t netflow_engine_id); | |
999401aa JG |
47 | void dpif_uninit(struct dpif *dpif, bool close); |
48 | ||
96fba48f | 49 | static inline void dpif_assert_class(const struct dpif *dpif, |
1acb6baa | 50 | const struct dpif_class *dpif_class) |
96fba48f | 51 | { |
1acb6baa | 52 | assert(dpif->dpif_class == dpif_class); |
96fba48f BP |
53 | } |
54 | ||
55 | /* Datapath interface class structure, to be defined by each implementation of | |
f20279af | 56 | * a datapath interface. |
96fba48f BP |
57 | * |
58 | * These functions return 0 if successful or a positive errno value on failure, | |
59 | * except where otherwise noted. | |
60 | * | |
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. */ | |
65 | struct dpif_class { | |
1a6f1e2a | 66 | /* Type of dpif in this class, e.g. "system", "netdev", etc. |
96fba48f | 67 | * |
1a6f1e2a JG |
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. */ | |
70 | const char *type; | |
96fba48f | 71 | |
d3d22744 BP |
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. | |
75 | * | |
76 | * This is used by the vswitch at startup, so that it can delete any | |
77 | * datapaths that are not configured. | |
78 | * | |
79 | * Some kinds of datapaths might not be practically enumerable, in which | |
80 | * case this function may be a null pointer. */ | |
d0c23a1a | 81 | int (*enumerate)(struct sset *all_dps); |
d3d22744 | 82 | |
1a6f1e2a JG |
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. | |
96fba48f | 85 | * |
4a387741 BP |
86 | * 'dpif_class' is the class of dpif to open. |
87 | * | |
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); | |
96fba48f BP |
93 | |
94 | /* Closes 'dpif' and frees associated memory. */ | |
95 | void (*close)(struct dpif *dpif); | |
96 | ||
97 | /* Attempts to destroy the dpif underlying 'dpif'. | |
98 | * | |
99 | * If successful, 'dpif' will not be used again except as an argument for | |
100 | * the 'close' member function. */ | |
1acb6baa | 101 | int (*destroy)(struct dpif *dpif); |
96fba48f | 102 | |
640e1b20 BP |
103 | /* Performs periodic work needed by 'dpif', if any is necessary. */ |
104 | void (*run)(struct dpif *dpif); | |
105 | ||
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); | |
109 | ||
96fba48f | 110 | /* Retrieves statistics for 'dpif' into 'stats'. */ |
a8d9304d | 111 | int (*get_stats)(const struct dpif *dpif, struct dpif_dp_stats *stats); |
96fba48f | 112 | |
c3827f61 | 113 | /* Adds 'netdev' as a new port in 'dpif'. If successful, sets '*port_no' |
96fba48f | 114 | * to the new port's port number. */ |
c3827f61 | 115 | int (*port_add)(struct dpif *dpif, struct netdev *netdev, |
96fba48f BP |
116 | uint16_t *port_no); |
117 | ||
118 | /* Removes port numbered 'port_no' from 'dpif'. */ | |
119 | int (*port_del)(struct dpif *dpif, uint16_t port_no); | |
120 | ||
121 | /* Queries 'dpif' for a port with the given 'port_no' or 'devname'. Stores | |
4c738a8d BP |
122 | * information about the port into '*port' if successful. |
123 | * | |
124 | * The caller takes ownership of data in 'port' and must free it with | |
125 | * dpif_port_destroy() when it is no longer needed. */ | |
96fba48f | 126 | int (*port_query_by_number)(const struct dpif *dpif, uint16_t port_no, |
4c738a8d | 127 | struct dpif_port *port); |
96fba48f | 128 | int (*port_query_by_name)(const struct dpif *dpif, const char *devname, |
4c738a8d | 129 | struct dpif_port *port); |
96fba48f | 130 | |
996c1b3d BP |
131 | /* Returns one greater than the largest port number accepted in flow |
132 | * actions. */ | |
133 | int (*get_max_ports)(const struct dpif *dpif); | |
134 | ||
98403001 BP |
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'. | |
138 | * | |
139 | * The return value only needs to be meaningful when DPIF_UC_ACTION has | |
140 | * been enabled in the 'dpif''s listen mask, and it is allowed to change | |
141 | * when DPIF_UC_ACTION is disabled and then re-enabled. | |
142 | * | |
143 | * A dpif provider that doesn't have meaningful Netlink PIDs can use NULL | |
144 | * for this function. This is equivalent to always returning 0. */ | |
145 | uint32_t (*port_get_pid)(const struct dpif *dpif, uint16_t port_no); | |
146 | ||
b0ec0f27 BP |
147 | /* Attempts to begin dumping the ports in a dpif. On success, returns 0 |
148 | * and initializes '*statep' with any data needed for iteration. On | |
149 | * failure, returns a positive errno value. */ | |
150 | int (*port_dump_start)(const struct dpif *dpif, void **statep); | |
151 | ||
152 | /* Attempts to retrieve another port from 'dpif' for 'state', which was | |
153 | * initialized by a successful call to the 'port_dump_start' function for | |
4c738a8d | 154 | * 'dpif'. On success, stores a new dpif_port into 'port' and returns 0. |
b0ec0f27 BP |
155 | * Returns EOF if the end of the port table has been reached, or a positive |
156 | * errno value on error. This function will not be called again once it | |
157 | * returns nonzero once for a given iteration (but the 'port_dump_done' | |
4c738a8d BP |
158 | * function will be called afterward). |
159 | * | |
160 | * The dpif provider retains ownership of the data stored in 'port'. It | |
161 | * must remain valid until at least the next call to 'port_dump_next' or | |
162 | * 'port_dump_done' for 'state'. */ | |
b0ec0f27 | 163 | int (*port_dump_next)(const struct dpif *dpif, void *state, |
4c738a8d | 164 | struct dpif_port *port); |
b0ec0f27 BP |
165 | |
166 | /* Releases resources from 'dpif' for 'state', which was initialized by a | |
167 | * successful call to the 'port_dump_start' function for 'dpif'. */ | |
168 | int (*port_dump_done)(const struct dpif *dpif, void *state); | |
96fba48f | 169 | |
e9e28be3 BP |
170 | /* Polls for changes in the set of ports in 'dpif'. If the set of ports in |
171 | * 'dpif' has changed, then this function should do one of the | |
172 | * following: | |
173 | * | |
174 | * - Preferably: store the name of the device that was added to or deleted | |
175 | * from 'dpif' in '*devnamep' and return 0. The caller is responsible | |
176 | * for freeing '*devnamep' (with free()) when it no longer needs it. | |
177 | * | |
178 | * - Alternatively: return ENOBUFS, without indicating the device that was | |
179 | * added or deleted. | |
180 | * | |
181 | * Occasional 'false positives', in which the function returns 0 while | |
182 | * indicating a device that was not actually added or deleted or returns | |
183 | * ENOBUFS without any change, are acceptable. | |
184 | * | |
185 | * If the set of ports in 'dpif' has not changed, returns EAGAIN. May also | |
186 | * return other positive errno values to indicate that something has gone | |
187 | * wrong. */ | |
188 | int (*port_poll)(const struct dpif *dpif, char **devnamep); | |
189 | ||
190 | /* Arranges for the poll loop to wake up when 'port_poll' will return a | |
191 | * value other than EAGAIN. */ | |
192 | void (*port_poll_wait)(const struct dpif *dpif); | |
193 | ||
feebdea2 | 194 | /* Queries 'dpif' for a flow entry. The flow is specified by the Netlink |
df2c07f4 | 195 | * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at |
feebdea2 | 196 | * 'key'. |
bc4a05c6 | 197 | * |
feebdea2 BP |
198 | * Returns 0 if successful. If no flow matches, returns ENOENT. On other |
199 | * failure, returns a positive errno value. | |
bc4a05c6 | 200 | * |
feebdea2 BP |
201 | * If 'actionsp' is nonnull, then on success '*actionsp' must be set to an |
202 | * ofpbuf owned by the caller that contains the Netlink attributes for the | |
203 | * flow's actions. The caller must free the ofpbuf (with ofpbuf_delete()) | |
204 | * when it is no longer needed. | |
205 | * | |
206 | * If 'stats' is nonnull, then on success it must be updated with the | |
207 | * flow's statistics. */ | |
693c4a01 | 208 | int (*flow_get)(const struct dpif *dpif, |
feebdea2 | 209 | const struct nlattr *key, size_t key_len, |
c97fb132 | 210 | struct ofpbuf **actionsp, struct dpif_flow_stats *stats); |
feebdea2 BP |
211 | |
212 | /* Adds or modifies a flow in 'dpif'. The flow is specified by the Netlink | |
df2c07f4 | 213 | * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at |
feebdea2 | 214 | * 'key'. The associated actions are specified by the Netlink attributes |
df2c07f4 | 215 | * with types OVS_ACTION_ATTR_* in the 'actions_len' bytes starting at |
7aec165d | 216 | * 'actions'. |
feebdea2 BP |
217 | * |
218 | * - If the flow's key does not exist in 'dpif', then the flow will be | |
ba25b8f4 BP |
219 | * added if 'flags' includes DPIF_FP_CREATE. Otherwise the operation |
220 | * will fail with ENOENT. | |
feebdea2 BP |
221 | * |
222 | * If the operation succeeds, then 'stats', if nonnull, must be zeroed. | |
96fba48f | 223 | * |
feebdea2 | 224 | * - If the flow's key does exist in 'dpif', then the flow's actions will |
ba25b8f4 BP |
225 | * be updated if 'flags' includes DPIF_FP_MODIFY. Otherwise the |
226 | * operation will fail with EEXIST. If the flow's actions are updated, | |
227 | * then its statistics will be zeroed if 'flags' includes | |
228 | * DPIF_FP_ZERO_STATS, and left as-is otherwise. | |
96fba48f | 229 | * |
feebdea2 BP |
230 | * If the operation succeeds, then 'stats', if nonnull, must be set to |
231 | * the flow's statistics before the update. | |
96fba48f | 232 | */ |
ba25b8f4 | 233 | int (*flow_put)(struct dpif *dpif, enum dpif_flow_put_flags flags, |
feebdea2 BP |
234 | const struct nlattr *key, size_t key_len, |
235 | const struct nlattr *actions, size_t actions_len, | |
c97fb132 | 236 | struct dpif_flow_stats *stats); |
feebdea2 BP |
237 | |
238 | /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif' | |
239 | * does not contain such a flow. The flow is specified by the Netlink | |
df2c07f4 | 240 | * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at |
feebdea2 | 241 | * 'key'. |
96fba48f | 242 | * |
feebdea2 BP |
243 | * If the operation succeeds, then 'stats', if nonnull, must be set to the |
244 | * flow's statistics before its deletion. */ | |
245 | int (*flow_del)(struct dpif *dpif, | |
246 | const struct nlattr *key, size_t key_len, | |
c97fb132 | 247 | struct dpif_flow_stats *stats); |
96fba48f BP |
248 | |
249 | /* Deletes all flows from 'dpif' and clears all of its queues of received | |
250 | * packets. */ | |
251 | int (*flow_flush)(struct dpif *dpif); | |
252 | ||
704a1e09 BP |
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); | |
257 | ||
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 | |
feebdea2 BP |
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). | |
265 | * | |
266 | * On success, if 'key' and 'key_len' are nonnull then '*key' and | |
df2c07f4 | 267 | * '*key_len' must be set to Netlink attributes with types OVS_KEY_ATTR_* |
feebdea2 | 268 | * representing the dumped flow's key. If 'actions' and 'actions_len' are |
7aec165d | 269 | * nonnull then they should be set to Netlink attributes with types |
df2c07f4 | 270 | * OVS_ACTION_ATTR_* representing the dumped flow's actions. If 'stats' |
7aec165d | 271 | * is nonnull then it should be set to the dumped flow's statistics. |
704a1e09 | 272 | * |
feebdea2 BP |
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'. */ | |
704a1e09 | 277 | int (*flow_dump_next)(const struct dpif *dpif, void *state, |
feebdea2 BP |
278 | const struct nlattr **key, size_t *key_len, |
279 | const struct nlattr **actions, size_t *actions_len, | |
c97fb132 | 280 | const struct dpif_flow_stats **stats); |
704a1e09 BP |
281 | |
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); | |
96fba48f | 285 | |
cdee00fd | 286 | /* Performs the 'actions_len' bytes of actions in 'actions' on the Ethernet |
80e5eed9 BP |
287 | * frame specified in 'packet' taken from the flow specified in the |
288 | * 'key_len' bytes of 'key'. ('key' is mostly redundant with 'packet', but | |
289 | * it contains some metadata that cannot be recovered from 'packet', such | |
290 | * as tun_id and in_port.) */ | |
291 | int (*execute)(struct dpif *dpif, | |
292 | const struct nlattr *key, size_t key_len, | |
293 | const struct nlattr *actions, size_t actions_len, | |
294 | const struct ofpbuf *packet); | |
96fba48f | 295 | |
6bc60024 BP |
296 | /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order |
297 | * in which they are specified, placing each operation's results in the | |
298 | * "output" members documented in comments. | |
299 | * | |
300 | * This function is optional. It is only worthwhile to implement it if | |
301 | * 'dpif' can perform operations in batch faster than individually. */ | |
302 | void (*operate)(struct dpif *dpif, union dpif_op **ops, size_t n_ops); | |
303 | ||
82272ede BP |
304 | /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. A 1-bit of value |
305 | * 2**X set in '*listen_mask' indicates that 'dpif' will receive messages | |
306 | * of the type (from "enum dpif_upcall_type") with value X when its 'recv' | |
307 | * function is called. */ | |
96fba48f BP |
308 | int (*recv_get_mask)(const struct dpif *dpif, int *listen_mask); |
309 | ||
82272ede BP |
310 | /* Sets 'dpif''s "listen mask" to 'listen_mask'. A 1-bit of value 2**X set |
311 | * in '*listen_mask' requests that 'dpif' will receive messages of the type | |
312 | * (from "enum dpif_upcall_type") with value X when its 'recv' function is | |
98403001 BP |
313 | * called. |
314 | * | |
315 | * Turning DPIF_UC_ACTION off and then back on is allowed to change Netlink | |
316 | * PID assignments (see ->port_get_pid()). The client is responsible for | |
317 | * updating flows as necessary if it does this. */ | |
96fba48f BP |
318 | int (*recv_set_mask)(struct dpif *dpif, int listen_mask); |
319 | ||
aae51f53 | 320 | /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a |
abff858b | 321 | * priority value used for setting packet priority. */ |
aae51f53 BP |
322 | int (*queue_to_priority)(const struct dpif *dpif, uint32_t queue_id, |
323 | uint32_t *priority); | |
324 | ||
856081f6 BP |
325 | /* Polls for an upcall from 'dpif'. If successful, stores the upcall into |
326 | * '*upcall'. Only upcalls of the types selected with the set_listen_mask | |
327 | * member function should be received. | |
96fba48f | 328 | * |
856081f6 BP |
329 | * The caller takes ownership of the data that 'upcall' points to. |
330 | * 'upcall->key' and 'upcall->actions' (if nonnull) point into data owned | |
331 | * by 'upcall->packet', so their memory cannot be freed separately. (This | |
332 | * is hardly a great way to do things but it works out OK for the dpif | |
333 | * providers that exist so far.) | |
334 | * | |
335 | * For greatest efficiency, 'upcall->packet' should have at least | |
336 | * offsetof(struct ofp_packet_in, data) bytes of headroom. | |
337 | * | |
338 | * This function must not block. If no upcall is pending when it is | |
339 | * called, it should return EAGAIN without blocking. */ | |
340 | int (*recv)(struct dpif *dpif, struct dpif_upcall *upcall); | |
96fba48f BP |
341 | |
342 | /* Arranges for the poll loop to wake up when 'dpif' has a message queued | |
343 | * to be received with the recv member function. */ | |
344 | void (*recv_wait)(struct dpif *dpif); | |
1ba530f4 BP |
345 | |
346 | /* Throws away any queued upcalls that 'dpif' currently has ready to | |
347 | * return. */ | |
348 | void (*recv_purge)(struct dpif *dpif); | |
96fba48f BP |
349 | }; |
350 | ||
351 | extern const struct dpif_class dpif_linux_class; | |
72865317 | 352 | extern const struct dpif_class dpif_netdev_class; |
96fba48f | 353 | |
1acb6baa BP |
354 | #ifdef __cplusplus |
355 | } | |
356 | #endif | |
357 | ||
96fba48f | 358 | #endif /* dpif-provider.h */ |