]>
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 | |
5792c5c6 BP |
72 | /* Performs periodic work needed by dpifs of this class, if any is |
73 | * necessary. */ | |
74 | void (*run)(void); | |
75 | ||
76 | /* Arranges for poll_block() to wake up if the "run" member function needs | |
77 | * to be called. */ | |
78 | void (*wait)(void); | |
79 | ||
d3d22744 BP |
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. | |
83 | * | |
84 | * This is used by the vswitch at startup, so that it can delete any | |
85 | * datapaths that are not configured. | |
86 | * | |
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); | |
90 | ||
1a6f1e2a JG |
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. | |
96fba48f | 93 | * |
4a387741 BP |
94 | * 'dpif_class' is the class of dpif to open. |
95 | * | |
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); | |
96fba48f BP |
101 | |
102 | /* Closes 'dpif' and frees associated memory. */ | |
103 | void (*close)(struct dpif *dpif); | |
104 | ||
d3d22744 BP |
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. | |
111 | * | |
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'. | |
115 | * | |
116 | * If a datapath class does not support multiple names for a datapath, this | |
117 | * function may be a null pointer. | |
118 | * | |
119 | * This is used by the vswitch at startup, */ | |
120 | int (*get_all_names)(const struct dpif *dpif, struct svec *all_names); | |
121 | ||
96fba48f BP |
122 | /* Attempts to destroy the dpif underlying 'dpif'. |
123 | * | |
124 | * If successful, 'dpif' will not be used again except as an argument for | |
125 | * the 'close' member function. */ | |
1acb6baa | 126 | int (*destroy)(struct dpif *dpif); |
96fba48f BP |
127 | |
128 | /* Retrieves statistics for 'dpif' into 'stats'. */ | |
129 | int (*get_stats)(const struct dpif *dpif, struct odp_stats *stats); | |
130 | ||
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); | |
136 | ||
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); | |
140 | ||
c3827f61 | 141 | /* Adds 'netdev' as a new port in 'dpif'. If successful, sets '*port_no' |
96fba48f | 142 | * to the new port's port number. */ |
c3827f61 | 143 | int (*port_add)(struct dpif *dpif, struct netdev *netdev, |
96fba48f BP |
144 | uint16_t *port_no); |
145 | ||
146 | /* Removes port numbered 'port_no' from 'dpif'. */ | |
147 | int (*port_del)(struct dpif *dpif, uint16_t port_no); | |
148 | ||
149 | /* Queries 'dpif' for a port with the given 'port_no' or 'devname'. Stores | |
4c738a8d BP |
150 | * information about the port into '*port' if successful. |
151 | * | |
152 | * The caller takes ownership of data in 'port' and must free it with | |
153 | * dpif_port_destroy() when it is no longer needed. */ | |
96fba48f | 154 | int (*port_query_by_number)(const struct dpif *dpif, uint16_t port_no, |
4c738a8d | 155 | struct dpif_port *port); |
96fba48f | 156 | int (*port_query_by_name)(const struct dpif *dpif, const char *devname, |
4c738a8d | 157 | struct dpif_port *port); |
96fba48f | 158 | |
996c1b3d BP |
159 | /* Returns one greater than the largest port number accepted in flow |
160 | * actions. */ | |
161 | int (*get_max_ports)(const struct dpif *dpif); | |
162 | ||
b0ec0f27 BP |
163 | /* Attempts to begin dumping the ports in a dpif. On success, returns 0 |
164 | * and initializes '*statep' with any data needed for iteration. On | |
165 | * failure, returns a positive errno value. */ | |
166 | int (*port_dump_start)(const struct dpif *dpif, void **statep); | |
167 | ||
168 | /* Attempts to retrieve another port from 'dpif' for 'state', which was | |
169 | * initialized by a successful call to the 'port_dump_start' function for | |
4c738a8d | 170 | * 'dpif'. On success, stores a new dpif_port into 'port' and returns 0. |
b0ec0f27 BP |
171 | * Returns EOF if the end of the port table has been reached, or a positive |
172 | * errno value on error. This function will not be called again once it | |
173 | * returns nonzero once for a given iteration (but the 'port_dump_done' | |
4c738a8d BP |
174 | * function will be called afterward). |
175 | * | |
176 | * The dpif provider retains ownership of the data stored in 'port'. It | |
177 | * must remain valid until at least the next call to 'port_dump_next' or | |
178 | * 'port_dump_done' for 'state'. */ | |
b0ec0f27 | 179 | int (*port_dump_next)(const struct dpif *dpif, void *state, |
4c738a8d | 180 | struct dpif_port *port); |
b0ec0f27 BP |
181 | |
182 | /* Releases resources from 'dpif' for 'state', which was initialized by a | |
183 | * successful call to the 'port_dump_start' function for 'dpif'. */ | |
184 | int (*port_dump_done)(const struct dpif *dpif, void *state); | |
96fba48f | 185 | |
e9e28be3 BP |
186 | /* Polls for changes in the set of ports in 'dpif'. If the set of ports in |
187 | * 'dpif' has changed, then this function should do one of the | |
188 | * following: | |
189 | * | |
190 | * - Preferably: store the name of the device that was added to or deleted | |
191 | * from 'dpif' in '*devnamep' and return 0. The caller is responsible | |
192 | * for freeing '*devnamep' (with free()) when it no longer needs it. | |
193 | * | |
194 | * - Alternatively: return ENOBUFS, without indicating the device that was | |
195 | * added or deleted. | |
196 | * | |
197 | * Occasional 'false positives', in which the function returns 0 while | |
198 | * indicating a device that was not actually added or deleted or returns | |
199 | * ENOBUFS without any change, are acceptable. | |
200 | * | |
201 | * If the set of ports in 'dpif' has not changed, returns EAGAIN. May also | |
202 | * return other positive errno values to indicate that something has gone | |
203 | * wrong. */ | |
204 | int (*port_poll)(const struct dpif *dpif, char **devnamep); | |
205 | ||
206 | /* Arranges for the poll loop to wake up when 'port_poll' will return a | |
207 | * value other than EAGAIN. */ | |
208 | void (*port_poll_wait)(const struct dpif *dpif); | |
209 | ||
feebdea2 BP |
210 | /* Queries 'dpif' for a flow entry. The flow is specified by the Netlink |
211 | * attributes with types ODP_KEY_ATTR_* in the 'key_len' bytes starting at | |
212 | * 'key'. | |
bc4a05c6 | 213 | * |
feebdea2 BP |
214 | * Returns 0 if successful. If no flow matches, returns ENOENT. On other |
215 | * failure, returns a positive errno value. | |
bc4a05c6 | 216 | * |
feebdea2 BP |
217 | * If 'actionsp' is nonnull, then on success '*actionsp' must be set to an |
218 | * ofpbuf owned by the caller that contains the Netlink attributes for the | |
219 | * flow's actions. The caller must free the ofpbuf (with ofpbuf_delete()) | |
220 | * when it is no longer needed. | |
221 | * | |
222 | * If 'stats' is nonnull, then on success it must be updated with the | |
223 | * flow's statistics. */ | |
693c4a01 | 224 | int (*flow_get)(const struct dpif *dpif, |
feebdea2 | 225 | const struct nlattr *key, size_t key_len, |
c97fb132 | 226 | struct ofpbuf **actionsp, struct dpif_flow_stats *stats); |
feebdea2 BP |
227 | |
228 | /* Adds or modifies a flow in 'dpif'. The flow is specified by the Netlink | |
229 | * attributes with types ODP_KEY_ATTR_* in the 'key_len' bytes starting at | |
230 | * 'key'. The associated actions are specified by the Netlink attributes | |
231 | * with types ODPAT_* in the 'actions_len' bytes starting at 'actions'. | |
232 | * | |
233 | * - If the flow's key does not exist in 'dpif', then the flow will be | |
ba25b8f4 BP |
234 | * added if 'flags' includes DPIF_FP_CREATE. Otherwise the operation |
235 | * will fail with ENOENT. | |
feebdea2 BP |
236 | * |
237 | * If the operation succeeds, then 'stats', if nonnull, must be zeroed. | |
96fba48f | 238 | * |
feebdea2 | 239 | * - If the flow's key does exist in 'dpif', then the flow's actions will |
ba25b8f4 BP |
240 | * be updated if 'flags' includes DPIF_FP_MODIFY. Otherwise the |
241 | * operation will fail with EEXIST. If the flow's actions are updated, | |
242 | * then its statistics will be zeroed if 'flags' includes | |
243 | * DPIF_FP_ZERO_STATS, and left as-is otherwise. | |
96fba48f | 244 | * |
feebdea2 BP |
245 | * If the operation succeeds, then 'stats', if nonnull, must be set to |
246 | * the flow's statistics before the update. | |
96fba48f | 247 | */ |
ba25b8f4 | 248 | int (*flow_put)(struct dpif *dpif, enum dpif_flow_put_flags flags, |
feebdea2 BP |
249 | const struct nlattr *key, size_t key_len, |
250 | const struct nlattr *actions, size_t actions_len, | |
c97fb132 | 251 | struct dpif_flow_stats *stats); |
feebdea2 BP |
252 | |
253 | /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif' | |
254 | * does not contain such a flow. The flow is specified by the Netlink | |
255 | * attributes with types ODP_KEY_ATTR_* in the 'key_len' bytes starting at | |
256 | * 'key'. | |
96fba48f | 257 | * |
feebdea2 BP |
258 | * If the operation succeeds, then 'stats', if nonnull, must be set to the |
259 | * flow's statistics before its deletion. */ | |
260 | int (*flow_del)(struct dpif *dpif, | |
261 | const struct nlattr *key, size_t key_len, | |
c97fb132 | 262 | struct dpif_flow_stats *stats); |
96fba48f BP |
263 | |
264 | /* Deletes all flows from 'dpif' and clears all of its queues of received | |
265 | * packets. */ | |
266 | int (*flow_flush)(struct dpif *dpif); | |
267 | ||
704a1e09 BP |
268 | /* Attempts to begin dumping the flows in a dpif. On success, returns 0 |
269 | * and initializes '*statep' with any data needed for iteration. On | |
270 | * failure, returns a positive errno value. */ | |
271 | int (*flow_dump_start)(const struct dpif *dpif, void **statep); | |
272 | ||
273 | /* Attempts to retrieve another flow from 'dpif' for 'state', which was | |
274 | * initialized by a successful call to the 'flow_dump_start' function for | |
feebdea2 BP |
275 | * 'dpif'. On success, updates the output parameters as described below |
276 | * and returns 0. Returns EOF if the end of the flow table has been | |
277 | * reached, or a positive errno value on error. This function will not be | |
278 | * called again once it returns nonzero within a given iteration (but the | |
279 | * 'flow_dump_done' function will be called afterward). | |
280 | * | |
281 | * On success, if 'key' and 'key_len' are nonnull then '*key' and | |
282 | * '*key_len' must be set to Netlink attributes with types ODP_KEY_ATTR_* | |
283 | * representing the dumped flow's key. If 'actions' and 'actions_len' are | |
284 | * nonnull then they should be set to Netlink attributes with types ODPAT_* | |
285 | * representing the dumped flow's actions. If 'stats' is nonnull then it | |
286 | * should be set to the dumped flow's statistics. | |
704a1e09 | 287 | * |
feebdea2 BP |
288 | * All of the returned data is owned by 'dpif', not by the caller, and the |
289 | * caller must not modify or free it. 'dpif' must guarantee that it | |
290 | * remains accessible and unchanging until at least the next call to | |
291 | * 'flow_dump_next' or 'flow_dump_done' for 'state'. */ | |
704a1e09 | 292 | int (*flow_dump_next)(const struct dpif *dpif, void *state, |
feebdea2 BP |
293 | const struct nlattr **key, size_t *key_len, |
294 | const struct nlattr **actions, size_t *actions_len, | |
c97fb132 | 295 | const struct dpif_flow_stats **stats); |
704a1e09 BP |
296 | |
297 | /* Releases resources from 'dpif' for 'state', which was initialized by a | |
298 | * successful call to the 'flow_dump_start' function for 'dpif'. */ | |
299 | int (*flow_dump_done)(const struct dpif *dpif, void *state); | |
96fba48f | 300 | |
cdee00fd BP |
301 | /* Performs the 'actions_len' bytes of actions in 'actions' on the Ethernet |
302 | * frame specified in 'packet'. */ | |
303 | int (*execute)(struct dpif *dpif, const struct nlattr *actions, | |
304 | size_t actions_len, const struct ofpbuf *packet); | |
96fba48f BP |
305 | |
306 | /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. Each ODPL_* bit | |
307 | * set in '*listen_mask' indicates the 'dpif' will receive messages of the | |
308 | * corresponding type when it calls the recv member function. */ | |
309 | int (*recv_get_mask)(const struct dpif *dpif, int *listen_mask); | |
310 | ||
311 | /* Sets 'dpif''s "listen mask" to 'listen_mask'. Each ODPL_* bit set in | |
312 | * 'listen_mask' indicates the 'dpif' will receive messages of the | |
313 | * corresponding type when it calls the recv member function. */ | |
314 | int (*recv_set_mask)(struct dpif *dpif, int listen_mask); | |
315 | ||
72b06300 BP |
316 | /* Retrieves 'dpif''s sFlow sampling probability into '*probability'. |
317 | * Return value is 0 or a positive errno value. EOPNOTSUPP indicates that | |
318 | * the datapath does not support sFlow, as does a null pointer. | |
319 | * | |
b4a7a3f3 BP |
320 | * '*probability' is expressed as the number of packets out of UINT_MAX to |
321 | * sample, e.g. probability/UINT_MAX is the probability of sampling a given | |
322 | * packet. */ | |
72b06300 BP |
323 | int (*get_sflow_probability)(const struct dpif *dpif, |
324 | uint32_t *probability); | |
325 | ||
326 | /* Sets 'dpif''s sFlow sampling probability to 'probability'. Return value | |
327 | * is 0 or a positive errno value. EOPNOTSUPP indicates that the datapath | |
328 | * does not support sFlow, as does a null pointer. | |
329 | * | |
b4a7a3f3 BP |
330 | * 'probability' is expressed as the number of packets out of UINT_MAX to |
331 | * sample, e.g. probability/UINT_MAX is the probability of sampling a given | |
332 | * packet. */ | |
72b06300 BP |
333 | int (*set_sflow_probability)(struct dpif *dpif, uint32_t probability); |
334 | ||
aae51f53 BP |
335 | /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a |
336 | * priority value for use in the ODPAT_SET_PRIORITY action in | |
337 | * '*priority'. */ | |
338 | int (*queue_to_priority)(const struct dpif *dpif, uint32_t queue_id, | |
339 | uint32_t *priority); | |
340 | ||
856081f6 BP |
341 | /* Polls for an upcall from 'dpif'. If successful, stores the upcall into |
342 | * '*upcall'. Only upcalls of the types selected with the set_listen_mask | |
343 | * member function should be received. | |
96fba48f | 344 | * |
856081f6 BP |
345 | * The caller takes ownership of the data that 'upcall' points to. |
346 | * 'upcall->key' and 'upcall->actions' (if nonnull) point into data owned | |
347 | * by 'upcall->packet', so their memory cannot be freed separately. (This | |
348 | * is hardly a great way to do things but it works out OK for the dpif | |
349 | * providers that exist so far.) | |
350 | * | |
351 | * For greatest efficiency, 'upcall->packet' should have at least | |
352 | * offsetof(struct ofp_packet_in, data) bytes of headroom. | |
353 | * | |
354 | * This function must not block. If no upcall is pending when it is | |
355 | * called, it should return EAGAIN without blocking. */ | |
356 | int (*recv)(struct dpif *dpif, struct dpif_upcall *upcall); | |
96fba48f BP |
357 | |
358 | /* Arranges for the poll loop to wake up when 'dpif' has a message queued | |
359 | * to be received with the recv member function. */ | |
360 | void (*recv_wait)(struct dpif *dpif); | |
1ba530f4 BP |
361 | |
362 | /* Throws away any queued upcalls that 'dpif' currently has ready to | |
363 | * return. */ | |
364 | void (*recv_purge)(struct dpif *dpif); | |
96fba48f BP |
365 | }; |
366 | ||
367 | extern const struct dpif_class dpif_linux_class; | |
72865317 | 368 | extern const struct dpif_class dpif_netdev_class; |
96fba48f | 369 | |
1acb6baa BP |
370 | #ifdef __cplusplus |
371 | } | |
372 | #endif | |
373 | ||
96fba48f | 374 | #endif /* dpif-provider.h */ |