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 XFIF_PROVIDER_H
18 #define XFIF_PROVIDER_H 1
20 /* Provider interface to xfifs, 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 xfif implementations. */
38 const struct xfif_class *xfif_class;
41 uint8_t netflow_engine_type;
42 uint8_t netflow_engine_id;
45 void xfif_init(struct xfif *, const struct xfif_class *, const char *name,
46 uint8_t netflow_engine_type, uint8_t netflow_engine_id);
47 void xfif_uninit(struct xfif *xfif, bool close);
49 static inline void xfif_assert_class(const struct xfif *xfif,
50 const struct xfif_class *xfif_class)
52 assert(xfif->xfif_class == xfif_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 * Most of these functions are expected to execute synchronously, that is, to
62 * block as necessary to obtain a result. Thus, these functions may return
63 * EAGAIN (or EWOULDBLOCK or EINPROGRESS) only where the function descriptions
64 * explicitly say those errors are a possibility. We may relax this
65 * requirement in the future if and when we encounter performance problems. */
67 /* Type of xfif in this class, e.g. "system", "netdev", etc.
69 * One of the providers should supply a "system" type, since this is
70 * the type assumed if no type is specified when opening a xfif. */
73 /* Performs periodic work needed by xfifs of this class, if any is
77 /* Arranges for poll_block() to wake up if the "run" member function needs
81 /* Enumerates the names of all known created datapaths, if possible, into
82 * 'all_dps'. The caller has already initialized 'all_dps' and other xfif
83 * classes might already have added names to it.
85 * This is used by the vswitch at startup, so that it can delete any
86 * datapaths that are not configured.
88 * Some kinds of datapaths might not be practically enumerable, in which
89 * case this function may be a null pointer. */
90 int (*enumerate)(struct svec *all_dps);
92 /* Attempts to open an existing xfif called 'name', if 'create' is false,
93 * or to open an existing xfif or create a new one, if 'create' is true.
94 * 'type' corresponds to the 'type' field used in the xfif_class
97 * If successful, stores a pointer to the new xfif in '*xfifp'. On failure
98 * there are no requirements on what is stored in '*xfifp'. */
99 int (*open)(const char *name, const char *type, bool create,
100 struct xfif **xfifp);
102 /* Closes 'xfif' and frees associated memory. */
103 void (*close)(struct xfif *xfif);
105 /* Enumerates all names that may be used to open 'xfif' 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 xfif *xfif, struct svec *all_names);
122 /* Attempts to destroy the xfif underlying 'xfif'.
124 * If successful, 'xfif' will not be used again except as an argument for
125 * the 'close' member function. */
126 int (*destroy)(struct xfif *xfif);
128 /* Retrieves statistics for 'xfif' into 'stats'. */
129 int (*get_stats)(const struct xfif *xfif, struct xflow_stats *stats);
131 /* Retrieves 'xfif''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 xfif *xfif, bool *drop_frags);
137 /* Changes 'xfif''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 xfif *xfif, bool drop_frags);
141 /* Creates a new port in 'xfif' connected to network device 'devname'.
142 * 'flags' is a set of XFLOW_PORT_* flags. If successful, sets '*port_no'
143 * to the new port's port number. */
144 int (*port_add)(struct xfif *xfif, const char *devname, uint16_t flags,
147 /* Removes port numbered 'port_no' from 'xfif'. */
148 int (*port_del)(struct xfif *xfif, uint16_t port_no);
150 /* Queries 'xfif' for a port with the given 'port_no' or 'devname'. Stores
151 * information about the port into '*port' if successful. */
152 int (*port_query_by_number)(const struct xfif *xfif, uint16_t port_no,
153 struct xflow_port *port);
154 int (*port_query_by_name)(const struct xfif *xfif, const char *devname,
155 struct xflow_port *port);
157 /* Stores in 'ports' information about up to 'n' ports attached to 'xfif',
158 * in no particular order. Returns the number of ports attached to 'xfif'
159 * (not the number stored), if successful, otherwise a negative errno
161 int (*port_list)(const struct xfif *xfif, struct xflow_port *ports, int n);
163 /* Polls for changes in the set of ports in 'xfif'. If the set of ports in
164 * 'xfif' has changed, then this function should do one of the
167 * - Preferably: store the name of the device that was added to or deleted
168 * from 'xfif' in '*devnamep' and return 0. The caller is responsible
169 * for freeing '*devnamep' (with free()) when it no longer needs it.
171 * - Alternatively: return ENOBUFS, without indicating the device that was
174 * Occasional 'false positives', in which the function returns 0 while
175 * indicating a device that was not actually added or deleted or returns
176 * ENOBUFS without any change, are acceptable.
178 * If the set of ports in 'xfif' has not changed, returns EAGAIN. May also
179 * return other positive errno values to indicate that something has gone
181 int (*port_poll)(const struct xfif *xfif, char **devnamep);
183 /* Arranges for the poll loop to wake up when 'port_poll' will return a
184 * value other than EAGAIN. */
185 void (*port_poll_wait)(const struct xfif *xfif);
187 /* Stores in 'ports' the port numbers of up to 'n' ports that belong to
188 * 'group' in 'xfif'. Returns the number of ports in 'group' (not the
189 * number stored), if successful, otherwise a negative errno value. */
190 int (*port_group_get)(const struct xfif *xfif, int group,
191 uint16_t ports[], int n);
193 /* Changes port group 'group' in 'xfif' to consist of the 'n' ports whose
194 * numbers are given in 'ports'.
196 * Use the get_stats member function to obtain the number of supported port
198 int (*port_group_set)(struct xfif *xfif, int group,
199 const uint16_t ports[], int n);
201 /* For each flow 'flow' in the 'n' flows in 'flows':
203 * - If a flow matching 'flow->key' exists in 'xfif':
205 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
206 * into 'flow->stats'.
208 * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If
209 * 'flow->n_actions' is nonzero, then 'flow->actions' should point to
210 * an array of the specified number of actions. At most that many of
211 * the flow's actions will be copied into that array.
212 * 'flow->n_actions' will be updated to the number of actions actually
213 * present in the flow, which may be greater than the number stored if
214 * the flow has more actions than space available in the array.
216 * - Flow-specific errors are indicated by a positive errno value in
217 * 'flow->stats.error'. In particular, ENOENT indicates that no flow
218 * matching 'flow->key' exists in 'xfif'. When an error value is stored,
219 * the contents of 'flow->key' are preserved but other members of 'flow'
220 * should be treated as indeterminate.
222 * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
223 * individually successful or not is indicated by 'flow->stats.error',
224 * however). Returns a positive errno value if an error that prevented
225 * this update occurred, in which the caller must not depend on any
226 * elements in 'flows' being updated or not updated.
228 int (*flow_get)(const struct xfif *xfif, struct xflow_flow flows[], int n);
230 /* Adds or modifies a flow in 'xfif' as specified in 'put':
232 * - If the flow specified in 'put->flow' does not exist in 'xfif', then
233 * behavior depends on whether XFLOWPF_CREATE is specified in 'put->flags':
234 * if it is, the flow will be added, otherwise the operation will fail
237 * - Otherwise, the flow specified in 'put->flow' does exist in 'xfif'.
238 * Behavior in this case depends on whether XFLOWPF_MODIFY is specified in
239 * 'put->flags': if it is, the flow's actions will be updated, otherwise
240 * the operation will fail with EEXIST. If the flow's actions are
241 * updated, then its statistics will be zeroed if XFLOWPF_ZERO_STATS is set
242 * in 'put->flags', left as-is otherwise.
244 int (*flow_put)(struct xfif *xfif, struct xflow_flow_put *put);
246 /* Deletes a flow matching 'flow->key' from 'xfif' or returns ENOENT if
247 * 'xfif' does not contain such a flow.
249 * If successful, updates 'flow->stats', 'flow->n_actions', and
250 * 'flow->actions' as described in more detail under the flow_get member
252 int (*flow_del)(struct xfif *xfif, struct xflow_flow *flow);
254 /* Deletes all flows from 'xfif' and clears all of its queues of received
256 int (*flow_flush)(struct xfif *xfif);
258 /* Stores up to 'n' flows in 'xfif' into 'flows', updating their statistics
259 * and actions as described under the flow_get member function. If
260 * successful, returns the number of flows actually present in 'xfif',
261 * which might be greater than the number stored (if 'xfif' has more than
262 * 'n' flows). On failure, returns a negative errno value. */
263 int (*flow_list)(const struct xfif *xfif,
264 struct xflow_flow flows[], int n);
266 /* Performs the 'n_actions' actions in 'actions' on the Ethernet frame
267 * specified in 'packet'.
269 * Pretends that the frame was originally received on the port numbered
270 * 'in_port'. This affects only XFLOWAT_OUTPUT_GROUP actions, which will
271 * not send a packet out their input port. Specify the number of an unused
272 * port (e.g. UINT16_MAX is currently always unused) to avoid this
274 int (*execute)(struct xfif *xfif, uint16_t in_port,
275 const union xflow_action actions[], int n_actions,
276 const struct ofpbuf *packet);
278 /* Retrieves 'xfif''s "listen mask" into '*listen_mask'. Each XFLOWL_* bit
279 * set in '*listen_mask' indicates the 'xfif' will receive messages of the
280 * corresponding type when it calls the recv member function. */
281 int (*recv_get_mask)(const struct xfif *xfif, int *listen_mask);
283 /* Sets 'xfif''s "listen mask" to 'listen_mask'. Each XFLOWL_* bit set in
284 * 'listen_mask' indicates the 'xfif' will receive messages of the
285 * corresponding type when it calls the recv member function. */
286 int (*recv_set_mask)(struct xfif *xfif, int listen_mask);
288 /* Retrieves 'xfif''s sFlow sampling probability into '*probability'.
289 * Return value is 0 or a positive errno value. EOPNOTSUPP indicates that
290 * the datapath does not support sFlow, as does a null pointer.
292 * '*probability' is expressed as the number of packets out of UINT_MAX to
293 * sample, e.g. probability/UINT_MAX is the probability of sampling a given
295 int (*get_sflow_probability)(const struct xfif *xfif,
296 uint32_t *probability);
298 /* Sets 'xfif''s sFlow sampling probability to 'probability'. Return value
299 * is 0 or a positive errno value. EOPNOTSUPP indicates that the datapath
300 * does not support sFlow, as does a null pointer.
302 * 'probability' is expressed as the number of packets out of UINT_MAX to
303 * sample, e.g. probability/UINT_MAX is the probability of sampling a given
305 int (*set_sflow_probability)(struct xfif *xfif, uint32_t probability);
307 /* Attempts to receive a message from 'xfif'. If successful, stores the
308 * message into '*packetp'. The message, if one is received, must begin
309 * with 'struct xflow_msg' as a header, and must have at least
310 * XFIF_RECV_MSG_PADDING bytes of headroom (allocated using
311 * e.g. ofpbuf_reserve()). Only messages of the types selected with the
312 * set_listen_mask member function should be received.
314 * This function must not block. If no message is ready to be received
315 * when it is called, it should return EAGAIN without blocking. */
316 int (*recv)(struct xfif *xfif, struct ofpbuf **packetp);
318 /* Arranges for the poll loop to wake up when 'xfif' has a message queued
319 * to be received with the recv member function. */
320 void (*recv_wait)(struct xfif *xfif);
323 /* Minimum number of bytes of headroom for a packet returned by the 'recv'
324 * member function (see above). This headroom allows "struct xflow_msg" to be
325 * replaced by "struct ofp_packet_in" without copying the buffer. */
326 #define XFIF_RECV_MSG_PADDING (sizeof(struct ofp_packet_in) \
327 - sizeof(struct xflow_msg))
328 BUILD_ASSERT_DECL(sizeof(struct ofp_packet_in) > sizeof(struct xflow_msg));
329 BUILD_ASSERT_DECL(XFIF_RECV_MSG_PADDING % 4 == 0);
331 extern const struct xfif_class xfif_linux_class;
332 extern const struct xfif_class xfif_netdev_class;
338 #endif /* xfif-provider.h */