Merge branch 'master' into next
[sliver-openvswitch.git] / lib / dpif-provider.h
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
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
21  * datapath. */
22
23 #include <assert.h>
24 #include "dpif.h"
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
30 /* Open vSwitch datapath interface.
31  *
32  * This structure should be treated as opaque by dpif implementations. */
33 struct dpif {
34     const struct dpif_class *dpif_class;
35     char *base_name;
36     char *full_name;
37     uint8_t netflow_engine_type;
38     uint8_t netflow_engine_id;
39 };
40
41 void dpif_init(struct dpif *, const struct dpif_class *, const char *name,
42                uint8_t netflow_engine_type, uint8_t netflow_engine_id);
43 void dpif_uninit(struct dpif *dpif, bool close);
44
45 static inline void dpif_assert_class(const struct dpif *dpif,
46                                      const struct dpif_class *dpif_class)
47 {
48     assert(dpif->dpif_class == dpif_class);
49 }
50
51 /* Datapath interface class structure, to be defined by each implementation of
52  * a datapath interface.
53  *
54  * These functions return 0 if successful or a positive errno value on failure,
55  * except where otherwise noted.
56  *
57  * These functions are expected to execute synchronously, that is, to block as
58  * necessary to obtain a result.  Thus, they may not return EAGAIN or
59  * EWOULDBLOCK or EINPROGRESS.  We may relax this requirement in the future if
60  * and when we encounter performance problems. */
61 struct dpif_class {
62     /* Type of dpif in this class, e.g. "system", "netdev", etc.
63      *
64      * One of the providers should supply a "system" type, since this is
65      * the type assumed if no type is specified when opening a dpif. */
66     const char *type;
67
68     /* Performs periodic work needed by dpifs of this class, if any is
69      * necessary. */
70     void (*run)(void);
71
72     /* Arranges for poll_block() to wake up if the "run" member function needs
73      * to be called. */
74     void (*wait)(void);
75
76     /* Enumerates the names of all known created datapaths, if possible, into
77      * 'all_dps'.  The caller has already initialized 'all_dps' and other dpif
78      * classes might already have added names to it.
79      *
80      * This is used by the vswitch at startup, so that it can delete any
81      * datapaths that are not configured.
82      *
83      * Some kinds of datapaths might not be practically enumerable, in which
84      * case this function may be a null pointer. */
85     int (*enumerate)(struct svec *all_dps);
86
87     /* Attempts to open an existing dpif called 'name', if 'create' is false,
88      * or to open an existing dpif or create a new one, if 'create' is true.
89      * 'type' corresponds to the 'type' field used in the dpif_class
90      * structure.
91      *
92      * If successful, stores a pointer to the new dpif in '*dpifp'.  On failure
93      * there are no requirements on what is stored in '*dpifp'. */
94     int (*open)(const char *name, const char *type, bool create,
95                 struct dpif **dpifp);
96
97     /* Closes 'dpif' and frees associated memory. */
98     void (*close)(struct dpif *dpif);
99
100     /* Enumerates all names that may be used to open 'dpif' into 'all_names'.
101      * The Linux datapath, for example, supports opening a datapath both by
102      * number, e.g. "dp0", and by the name of the datapath's local port.  For
103      * some datapaths, this might be an infinite set (e.g. in a file name,
104      * slashes may be duplicated any number of times), in which case only the
105      * names most likely to be used should be enumerated.
106      *
107      * The caller has already initialized 'all_names' and might already have
108      * added some names to it.  This function should not disturb any existing
109      * names in 'all_names'.
110      *
111      * If a datapath class does not support multiple names for a datapath, this
112      * function may be a null pointer.
113      *
114      * This is used by the vswitch at startup, */
115     int (*get_all_names)(const struct dpif *dpif, struct svec *all_names);
116
117     /* Attempts to destroy the dpif underlying 'dpif'.
118      *
119      * If successful, 'dpif' will not be used again except as an argument for
120      * the 'close' member function. */
121     int (*destroy)(struct dpif *dpif);
122
123     /* Retrieves statistics for 'dpif' into 'stats'. */
124     int (*get_stats)(const struct dpif *dpif, struct odp_stats *stats);
125
126     /* Retrieves 'dpif''s current treatment of IP fragments into '*drop_frags':
127      * true indicates that fragments are dropped, false indicates that
128      * fragments are treated in the same way as other IP packets (except that
129      * the L4 header cannot be read). */
130     int (*get_drop_frags)(const struct dpif *dpif, bool *drop_frags);
131
132     /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose
133      * meaning is the same as for the get_drop_frags member function. */
134     int (*set_drop_frags)(struct dpif *dpif, bool drop_frags);
135
136     /* Creates a new port in 'dpif' connected to network device 'devname'.
137      * 'flags' is a set of ODP_PORT_* flags.  If successful, sets '*port_no'
138      * to the new port's port number. */
139     int (*port_add)(struct dpif *dpif, const char *devname, uint16_t flags,
140                     uint16_t *port_no);
141
142     /* Removes port numbered 'port_no' from 'dpif'. */
143     int (*port_del)(struct dpif *dpif, uint16_t port_no);
144
145     /* Queries 'dpif' for a port with the given 'port_no' or 'devname'.  Stores
146      * information about the port into '*port' if successful. */
147     int (*port_query_by_number)(const struct dpif *dpif, uint16_t port_no,
148                                 struct odp_port *port);
149     int (*port_query_by_name)(const struct dpif *dpif, const char *devname,
150                               struct odp_port *port);
151
152     /* Stores in 'ports' information about up to 'n' ports attached to 'dpif',
153      * in no particular order.  Returns the number of ports attached to 'dpif'
154      * (not the number stored), if successful, otherwise a negative errno
155      * value. */
156     int (*port_list)(const struct dpif *dpif, struct odp_port *ports, int n);
157
158     /* Polls for changes in the set of ports in 'dpif'.  If the set of ports in
159      * 'dpif' has changed, then this function should do one of the
160      * following:
161      *
162      * - Preferably: store the name of the device that was added to or deleted
163      *   from 'dpif' in '*devnamep' and return 0.  The caller is responsible
164      *   for freeing '*devnamep' (with free()) when it no longer needs it.
165      *
166      * - Alternatively: return ENOBUFS, without indicating the device that was
167      *   added or deleted.
168      *
169      * Occasional 'false positives', in which the function returns 0 while
170      * indicating a device that was not actually added or deleted or returns
171      * ENOBUFS without any change, are acceptable.
172      *
173      * If the set of ports in 'dpif' has not changed, returns EAGAIN.  May also
174      * return other positive errno values to indicate that something has gone
175      * wrong. */
176     int (*port_poll)(const struct dpif *dpif, char **devnamep);
177
178     /* Arranges for the poll loop to wake up when 'port_poll' will return a
179      * value other than EAGAIN. */
180     void (*port_poll_wait)(const struct dpif *dpif);
181
182     /* Stores in 'ports' the port numbers of up to 'n' ports that belong to
183      * 'group' in 'dpif'.  Returns the number of ports in 'group' (not the
184      * number stored), if successful, otherwise a negative errno value. */
185     int (*port_group_get)(const struct dpif *dpif, int group,
186                           uint16_t ports[], int n);
187
188     /* Changes port group 'group' in 'dpif' to consist of the 'n' ports whose
189      * numbers are given in 'ports'.
190      *
191      * Use the get_stats member function to obtain the number of supported port
192      * groups. */
193     int (*port_group_set)(struct dpif *dpif, int group,
194                           const uint16_t ports[], int n);
195
196     /* For each flow 'flow' in the 'n' flows in 'flows':
197      *
198      * - If a flow matching 'flow->key' exists in 'dpif':
199      *
200      *     Stores 0 into 'flow->stats.error' and stores statistics for the flow
201      *     into 'flow->stats'.
202      *
203      *     If 'flow->n_actions' is zero, then 'flow->actions' is ignored.  If
204      *     'flow->n_actions' is nonzero, then 'flow->actions' should point to
205      *     an array of the specified number of actions.  At most that many of
206      *     the flow's actions will be copied into that array.
207      *     'flow->n_actions' will be updated to the number of actions actually
208      *     present in the flow, which may be greater than the number stored if
209      *     the flow has more actions than space available in the array.
210      *
211      * - Flow-specific errors are indicated by a positive errno value in
212      *   'flow->stats.error'.  In particular, ENOENT indicates that no flow
213      *   matching 'flow->key' exists in 'dpif'.  When an error value is stored,
214      *   the contents of 'flow->key' are preserved but other members of 'flow'
215      *   should be treated as indeterminate.
216      *
217      * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
218      * individually successful or not is indicated by 'flow->stats.error',
219      * however).  Returns a positive errno value if an error that prevented
220      * this update occurred, in which the caller must not depend on any
221      * elements in 'flows' being updated or not updated.
222      */
223     int (*flow_get)(const struct dpif *dpif, struct odp_flow flows[], int n);
224
225     /* Adds or modifies a flow in 'dpif' as specified in 'put':
226      *
227      * - If the flow specified in 'put->flow' does not exist in 'dpif', then
228      *   behavior depends on whether ODPPF_CREATE is specified in 'put->flags':
229      *   if it is, the flow will be added, otherwise the operation will fail
230      *   with ENOENT.
231      *
232      * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'.
233      *   Behavior in this case depends on whether ODPPF_MODIFY is specified in
234      *   'put->flags': if it is, the flow's actions will be updated, otherwise
235      *   the operation will fail with EEXIST.  If the flow's actions are
236      *   updated, then its statistics will be zeroed if ODPPF_ZERO_STATS is set
237      *   in 'put->flags', left as-is otherwise.
238      */
239     int (*flow_put)(struct dpif *dpif, struct odp_flow_put *put);
240
241     /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if
242      * 'dpif' does not contain such a flow.
243      *
244      * If successful, updates 'flow->stats', 'flow->n_actions', and
245      * 'flow->actions' as described in more detail under the flow_get member
246      * function below. */
247     int (*flow_del)(struct dpif *dpif, struct odp_flow *flow);
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
253     /* Stores up to 'n' flows in 'dpif' into 'flows', updating their statistics
254      * and actions as described under the flow_get member function.  If
255      * successful, returns the number of flows actually present in 'dpif',
256      * which might be greater than the number stored (if 'dpif' has more than
257      * 'n' flows).  On failure, returns a negative errno value. */
258     int (*flow_list)(const struct dpif *dpif, struct odp_flow flows[], int n);
259
260     /* Performs the 'n_actions' actions in 'actions' on the Ethernet frame
261      * specified in 'packet'.
262      *
263      * Pretends that the frame was originally received on the port numbered
264      * 'in_port'.  This affects only ODPAT_OUTPUT_GROUP actions, which will not
265      * send a packet out their input port.  Specify the number of an unused
266      * port (e.g. UINT16_MAX is currently always unused) to avoid this
267      * behavior. */
268     int (*execute)(struct dpif *dpif, uint16_t in_port,
269                    const union odp_action actions[], int n_actions,
270                    const struct ofpbuf *packet);
271
272     /* Retrieves 'dpif''s "listen mask" into '*listen_mask'.  Each ODPL_* bit
273      * set in '*listen_mask' indicates the 'dpif' will receive messages of the
274      * corresponding type when it calls the recv member function. */
275     int (*recv_get_mask)(const struct dpif *dpif, int *listen_mask);
276
277     /* Sets 'dpif''s "listen mask" to 'listen_mask'.  Each ODPL_* bit set in
278      * 'listen_mask' indicates the 'dpif' will receive messages of the
279      * corresponding type when it calls the recv member function. */
280     int (*recv_set_mask)(struct dpif *dpif, int listen_mask);
281
282     /* Retrieves 'dpif''s sFlow sampling probability into '*probability'.
283      * Return value is 0 or a positive errno value.  EOPNOTSUPP indicates that
284      * the datapath does not support sFlow, as does a null pointer.
285      *
286      * '*probability' is expressed as the number of packets out of UINT_MAX to
287      * sample, e.g. probability/UINT_MAX is the probability of sampling a given
288      * packet. */
289     int (*get_sflow_probability)(const struct dpif *dpif,
290                                  uint32_t *probability);
291
292     /* Sets 'dpif''s sFlow sampling probability to 'probability'.  Return value
293      * is 0 or a positive errno value.  EOPNOTSUPP indicates that the datapath
294      * does not support sFlow, as does a null pointer.
295      *
296      * 'probability' is expressed as the number of packets out of UINT_MAX to
297      * sample, e.g. probability/UINT_MAX is the probability of sampling a given
298      * packet. */
299     int (*set_sflow_probability)(struct dpif *dpif, uint32_t probability);
300
301     /* Attempts to receive a message from 'dpif'.  If successful, stores the
302      * message into '*packetp'.  The message, if one is received, must begin
303      * with 'struct odp_msg' as a header.  Only messages of the types selected
304      * with the set_listen_mask member function should be received.
305      *
306      * This function must not block.  If no message is ready to be received
307      * when it is called, it should return EAGAIN without blocking. */
308     int (*recv)(struct dpif *dpif, struct ofpbuf **packetp);
309
310     /* Arranges for the poll loop to wake up when 'dpif' has a message queued
311      * to be received with the recv member function. */
312     void (*recv_wait)(struct dpif *dpif);
313 };
314
315 extern const struct dpif_class dpif_linux_class;
316 extern const struct dpif_class dpif_netdev_class;
317
318 #ifdef  __cplusplus
319 }
320 #endif
321
322 #endif /* dpif-provider.h */