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