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