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
26 /* Open vSwitch datapath interface.
28 * This structure should be treated as opaque by dpif implementations. */
30 const struct dpif_class
*class;
33 uint8_t netflow_engine_type
;
34 uint8_t netflow_engine_id
;
37 void dpif_init(struct dpif
*, const struct dpif_class
*, const char *name
,
38 uint8_t netflow_engine_type
, uint8_t netflow_engine_id
);
39 static inline void dpif_assert_class(const struct dpif
*dpif
,
40 const struct dpif_class
*class)
42 assert(dpif
->class == class);
45 /* Datapath interface class structure, to be defined by each implementation of
46 * a datapath interface.
48 * These functions return 0 if successful or a positive errno value on failure,
49 * except where otherwise noted.
51 * These functions are expected to execute synchronously, that is, to block as
52 * necessary to obtain a result. Thus, they may not return EAGAIN or
53 * EWOULDBLOCK or EINPROGRESS. We may relax this requirement in the future if
54 * and when we encounter performance problems. */
56 /* Type of dpif in this class, e.g. "system", "netdev", etc.
58 * One of the providers should supply a "system" type, since this is
59 * the type assumed if no type is specified when opening a dpif. */
62 /* Performs periodic work needed by dpifs of this class, if any is
66 /* Arranges for poll_block() to wake up if the "run" member function needs
70 /* Enumerates the names of all known created datapaths, if possible, into
71 * 'all_dps'. The caller has already initialized 'all_dps' and other dpif
72 * classes might already have added names to it.
74 * This is used by the vswitch at startup, so that it can delete any
75 * datapaths that are not configured.
77 * Some kinds of datapaths might not be practically enumerable, in which
78 * case this function may be a null pointer. */
79 int (*enumerate
)(struct svec
*all_dps
);
81 /* Attempts to open an existing dpif called 'name', if 'create' is false,
82 * or to open an existing dpif or create a new one, if 'create' is true.
83 * 'type' corresponds to the 'type' field used in the dpif_class
86 * If successful, stores a pointer to the new dpif in '*dpifp'. On failure
87 * there are no requirements on what is stored in '*dpifp'. */
88 int (*open
)(const char *name
, const char *type
, bool create
,
91 /* Closes 'dpif' and frees associated memory. */
92 void (*close
)(struct dpif
*dpif
);
94 /* Enumerates all names that may be used to open 'dpif' into 'all_names'.
95 * The Linux datapath, for example, supports opening a datapath both by
96 * number, e.g. "dp0", and by the name of the datapath's local port. For
97 * some datapaths, this might be an infinite set (e.g. in a file name,
98 * slashes may be duplicated any number of times), in which case only the
99 * names most likely to be used should be enumerated.
101 * The caller has already initialized 'all_names' and might already have
102 * added some names to it. This function should not disturb any existing
103 * names in 'all_names'.
105 * If a datapath class does not support multiple names for a datapath, this
106 * function may be a null pointer.
108 * This is used by the vswitch at startup, */
109 int (*get_all_names
)(const struct dpif
*dpif
, struct svec
*all_names
);
111 /* Attempts to destroy the dpif underlying 'dpif'.
113 * If successful, 'dpif' will not be used again except as an argument for
114 * the 'close' member function. */
115 int (*delete)(struct dpif
*dpif
);
117 /* Retrieves statistics for 'dpif' into 'stats'. */
118 int (*get_stats
)(const struct dpif
*dpif
, struct odp_stats
*stats
);
120 /* Retrieves 'dpif''s current treatment of IP fragments into '*drop_frags':
121 * true indicates that fragments are dropped, false indicates that
122 * fragments are treated in the same way as other IP packets (except that
123 * the L4 header cannot be read). */
124 int (*get_drop_frags
)(const struct dpif
*dpif
, bool *drop_frags
);
126 /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose
127 * meaning is the same as for the get_drop_frags member function. */
128 int (*set_drop_frags
)(struct dpif
*dpif
, bool drop_frags
);
130 /* Creates a new port in 'dpif' connected to network device 'devname'.
131 * 'flags' is a set of ODP_PORT_* flags. If successful, sets '*port_no'
132 * to the new port's port number. */
133 int (*port_add
)(struct dpif
*dpif
, const char *devname
, uint16_t flags
,
136 /* Removes port numbered 'port_no' from 'dpif'. */
137 int (*port_del
)(struct dpif
*dpif
, uint16_t port_no
);
139 /* Queries 'dpif' for a port with the given 'port_no' or 'devname'. Stores
140 * information about the port into '*port' if successful. */
141 int (*port_query_by_number
)(const struct dpif
*dpif
, uint16_t port_no
,
142 struct odp_port
*port
);
143 int (*port_query_by_name
)(const struct dpif
*dpif
, const char *devname
,
144 struct odp_port
*port
);
146 /* Stores in 'ports' information about up to 'n' ports attached to 'dpif',
147 * in no particular order. Returns the number of ports attached to 'dpif'
148 * (not the number stored), if successful, otherwise a negative errno
150 int (*port_list
)(const struct dpif
*dpif
, struct odp_port
*ports
, int n
);
152 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
153 * 'dpif' has changed, then this function should do one of the
156 * - Preferably: store the name of the device that was added to or deleted
157 * from 'dpif' in '*devnamep' and return 0. The caller is responsible
158 * for freeing '*devnamep' (with free()) when it no longer needs it.
160 * - Alternatively: return ENOBUFS, without indicating the device that was
163 * Occasional 'false positives', in which the function returns 0 while
164 * indicating a device that was not actually added or deleted or returns
165 * ENOBUFS without any change, are acceptable.
167 * If the set of ports in 'dpif' has not changed, returns EAGAIN. May also
168 * return other positive errno values to indicate that something has gone
170 int (*port_poll
)(const struct dpif
*dpif
, char **devnamep
);
172 /* Arranges for the poll loop to wake up when 'port_poll' will return a
173 * value other than EAGAIN. */
174 void (*port_poll_wait
)(const struct dpif
*dpif
);
176 /* Stores in 'ports' the port numbers of up to 'n' ports that belong to
177 * 'group' in 'dpif'. Returns the number of ports in 'group' (not the
178 * number stored), if successful, otherwise a negative errno value. */
179 int (*port_group_get
)(const struct dpif
*dpif
, int group
,
180 uint16_t ports
[], int n
);
182 /* Changes port group 'group' in 'dpif' to consist of the 'n' ports whose
183 * numbers are given in 'ports'.
185 * Use the get_stats member function to obtain the number of supported port
187 int (*port_group_set
)(struct dpif
*dpif
, int group
,
188 const uint16_t ports
[], int n
);
190 /* For each flow 'flow' in the 'n' flows in 'flows':
192 * - If a flow matching 'flow->key' exists in 'dpif':
194 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
195 * into 'flow->stats'.
197 * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If
198 * 'flow->n_actions' is nonzero, then 'flow->actions' should point to
199 * an array of the specified number of actions. At most that many of
200 * the flow's actions will be copied into that array.
201 * 'flow->n_actions' will be updated to the number of actions actually
202 * present in the flow, which may be greater than the number stored if
203 * the flow has more actions than space available in the array.
205 * - Flow-specific errors are indicated by a positive errno value in
206 * 'flow->stats.error'. In particular, ENOENT indicates that no flow
207 * matching 'flow->key' exists in 'dpif'. When an error value is stored,
208 * the contents of 'flow->key' are preserved but other members of 'flow'
209 * should be treated as indeterminate.
211 * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
212 * individually successful or not is indicated by 'flow->stats.error',
213 * however). Returns a positive errno value if an error that prevented
214 * this update occurred, in which the caller must not depend on any
215 * elements in 'flows' being updated or not updated.
217 int (*flow_get
)(const struct dpif
*dpif
, struct odp_flow flows
[], int n
);
219 /* Adds or modifies a flow in 'dpif' as specified in 'put':
221 * - If the flow specified in 'put->flow' does not exist in 'dpif', then
222 * behavior depends on whether ODPPF_CREATE is specified in 'put->flags':
223 * if it is, the flow will be added, otherwise the operation will fail
226 * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'.
227 * Behavior in this case depends on whether ODPPF_MODIFY is specified in
228 * 'put->flags': if it is, the flow's actions will be updated, otherwise
229 * the operation will fail with EEXIST. If the flow's actions are
230 * updated, then its statistics will be zeroed if ODPPF_ZERO_STATS is set
231 * in 'put->flags', left as-is otherwise.
233 int (*flow_put
)(struct dpif
*dpif
, struct odp_flow_put
*put
);
235 /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if
236 * 'dpif' does not contain such a flow.
238 * If successful, updates 'flow->stats', 'flow->n_actions', and
239 * 'flow->actions' as described in more detail under the flow_get member
241 int (*flow_del
)(struct dpif
*dpif
, struct odp_flow
*flow
);
243 /* Deletes all flows from 'dpif' and clears all of its queues of received
245 int (*flow_flush
)(struct dpif
*dpif
);
247 /* Stores up to 'n' flows in 'dpif' into 'flows', updating their statistics
248 * and actions as described under the flow_get member function. If
249 * successful, returns the number of flows actually present in 'dpif',
250 * which might be greater than the number stored (if 'dpif' has more than
251 * 'n' flows). On failure, returns a negative errno value. */
252 int (*flow_list
)(const struct dpif
*dpif
, struct odp_flow flows
[], int n
);
254 /* Performs the 'n_actions' actions in 'actions' on the Ethernet frame
255 * specified in 'packet'.
257 * Pretends that the frame was originally received on the port numbered
258 * 'in_port'. This affects only ODPAT_OUTPUT_GROUP actions, which will not
259 * send a packet out their input port. Specify the number of an unused
260 * port (e.g. UINT16_MAX is currently always unused) to avoid this
262 int (*execute
)(struct dpif
*dpif
, uint16_t in_port
,
263 const union odp_action actions
[], int n_actions
,
264 const struct ofpbuf
*packet
);
266 /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. Each ODPL_* bit
267 * set in '*listen_mask' indicates the 'dpif' will receive messages of the
268 * corresponding type when it calls the recv member function. */
269 int (*recv_get_mask
)(const struct dpif
*dpif
, int *listen_mask
);
271 /* Sets 'dpif''s "listen mask" to 'listen_mask'. Each ODPL_* bit set in
272 * 'listen_mask' indicates the 'dpif' will receive messages of the
273 * corresponding type when it calls the recv member function. */
274 int (*recv_set_mask
)(struct dpif
*dpif
, int listen_mask
);
276 /* Attempts to receive a message from 'dpif'. If successful, stores the
277 * message into '*packetp'. The message, if one is received, must begin
278 * with 'struct odp_msg' as a header. Only messages of the types selected
279 * with the set_listen_mask member function should be received.
281 * This function must not block. If no message is ready to be received
282 * when it is called, it should return EAGAIN without blocking. */
283 int (*recv
)(struct dpif
*dpif
, struct ofpbuf
**packetp
);
285 /* Arranges for the poll loop to wake up when 'dpif' has a message queued
286 * to be received with the recv member function. */
287 void (*recv_wait
)(struct dpif
*dpif
);
290 extern const struct dpif_class dpif_linux_class
;
291 extern const struct dpif_class dpif_netdev_class
;
293 #endif /* dpif-provider.h */