bd6095acf2dd14f39ae7d0da12076e3d704444b7
[openvswitch] / lib / dpif.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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
18 #ifndef DPIF_H
19 #define DPIF_H 1
20
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <linux/openvswitch.h>
25 #include "openflow/openflow.h"
26 #include "netdev.h"
27 #include "util.h"
28
29 #ifdef  __cplusplus
30 extern "C" {
31 #endif
32
33 struct dpif;
34 struct ds;
35 struct flow;
36 struct nlattr;
37 struct ofpbuf;
38 struct sset;
39 struct dpif_class;
40
41 int dp_register_provider(const struct dpif_class *);
42 int dp_unregister_provider(const char *type);
43 void dp_blacklist_provider(const char *type);
44 void dp_enumerate_types(struct sset *types);
45 const char *dpif_normalize_type(const char *);
46
47 int dp_enumerate_names(const char *type, struct sset *names);
48 void dp_parse_name(const char *datapath_name, char **name, char **type);
49
50 int dpif_open(const char *name, const char *type, struct dpif **);
51 int dpif_create(const char *name, const char *type, struct dpif **);
52 int dpif_create_and_open(const char *name, const char *type, struct dpif **);
53 void dpif_close(struct dpif *);
54
55 void dpif_run(struct dpif *);
56 void dpif_wait(struct dpif *);
57
58 const char *dpif_name(const struct dpif *);
59 const char *dpif_base_name(const struct dpif *);
60
61 int dpif_delete(struct dpif *);
62
63 /* Statistics for a dpif as a whole. */
64 struct dpif_dp_stats {
65     uint64_t n_hit;             /* Number of flow table matches. */
66     uint64_t n_missed;          /* Number of flow table misses. */
67     uint64_t n_lost;            /* Number of misses not sent to userspace. */
68     uint64_t n_flows;           /* Number of flows present. */
69 };
70 int dpif_get_dp_stats(const struct dpif *, struct dpif_dp_stats *);
71
72 \f
73 /* Port operations. */
74
75 int dpif_port_add(struct dpif *, struct netdev *, uint32_t *port_nop);
76 int dpif_port_del(struct dpif *, uint32_t port_no);
77
78 /* A port within a datapath.
79  *
80  * 'name' and 'type' are suitable for passing to netdev_open(). */
81 struct dpif_port {
82     char *name;                 /* Network device name, e.g. "eth0". */
83     char *type;                 /* Network device type, e.g. "system". */
84     uint32_t port_no;           /* Port number within datapath. */
85 };
86 void dpif_port_clone(struct dpif_port *, const struct dpif_port *);
87 void dpif_port_destroy(struct dpif_port *);
88 bool dpif_port_exists(const struct dpif *dpif, const char *devname);
89 int dpif_port_query_by_number(const struct dpif *, uint32_t port_no,
90                               struct dpif_port *);
91 int dpif_port_query_by_name(const struct dpif *, const char *devname,
92                             struct dpif_port *);
93 int dpif_port_get_name(struct dpif *, uint32_t port_no,
94                        char *name, size_t name_size);
95 int dpif_get_max_ports(const struct dpif *);
96 uint32_t dpif_port_get_pid(const struct dpif *, uint32_t port_no);
97
98 struct dpif_port_dump {
99     const struct dpif *dpif;
100     int error;
101     void *state;
102 };
103 void dpif_port_dump_start(struct dpif_port_dump *, const struct dpif *);
104 bool dpif_port_dump_next(struct dpif_port_dump *, struct dpif_port *);
105 int dpif_port_dump_done(struct dpif_port_dump *);
106
107 /* Iterates through each DPIF_PORT in DPIF, using DUMP as state.
108  *
109  * Arguments all have pointer type.
110  *
111  * If you break out of the loop, then you need to free the dump structure by
112  * hand using dpif_port_dump_done(). */
113 #define DPIF_PORT_FOR_EACH(DPIF_PORT, DUMP, DPIF)   \
114     for (dpif_port_dump_start(DUMP, DPIF);          \
115          (dpif_port_dump_next(DUMP, DPIF_PORT)      \
116           ? true                                    \
117           : (dpif_port_dump_done(DUMP), false));    \
118         )
119
120 int dpif_port_poll(const struct dpif *, char **devnamep);
121 void dpif_port_poll_wait(const struct dpif *);
122 \f
123 /* Flow table operations. */
124
125 struct dpif_flow_stats {
126     uint64_t n_packets;
127     uint64_t n_bytes;
128     long long int used;
129     uint8_t tcp_flags;
130 };
131
132 void dpif_flow_stats_extract(const struct flow *, const struct ofpbuf *packet,
133                              long long int used, struct dpif_flow_stats *);
134 void dpif_flow_stats_format(const struct dpif_flow_stats *, struct ds *);
135
136 enum dpif_flow_put_flags {
137     DPIF_FP_CREATE = 1 << 0,    /* Allow creating a new flow. */
138     DPIF_FP_MODIFY = 1 << 1,    /* Allow modifying an existing flow. */
139     DPIF_FP_ZERO_STATS = 1 << 2 /* Zero the stats of an existing flow. */
140 };
141
142 int dpif_flow_flush(struct dpif *);
143 int dpif_flow_put(struct dpif *, enum dpif_flow_put_flags,
144                   const struct nlattr *key, size_t key_len,
145                   const struct nlattr *actions, size_t actions_len,
146                   struct dpif_flow_stats *);
147 int dpif_flow_del(struct dpif *,
148                   const struct nlattr *key, size_t key_len,
149                   struct dpif_flow_stats *);
150 int dpif_flow_get(const struct dpif *,
151                   const struct nlattr *key, size_t key_len,
152                   struct ofpbuf **actionsp, struct dpif_flow_stats *);
153
154 struct dpif_flow_dump {
155     const struct dpif *dpif;
156     int error;
157     void *state;
158 };
159 void dpif_flow_dump_start(struct dpif_flow_dump *, const struct dpif *);
160 bool dpif_flow_dump_next(struct dpif_flow_dump *,
161                          const struct nlattr **key, size_t *key_len,
162                          const struct nlattr **actions, size_t *actions_len,
163                          const struct dpif_flow_stats **);
164 int dpif_flow_dump_done(struct dpif_flow_dump *);
165 \f
166 /* Packet operations. */
167
168 int dpif_execute(struct dpif *,
169                  const struct nlattr *key, size_t key_len,
170                  const struct nlattr *actions, size_t actions_len,
171                  const struct ofpbuf *);
172 \f
173 /* Operation batching interface.
174  *
175  * Some datapaths are faster at performing N operations together than the same
176  * N operations individually, hence an interface for batching.
177  */
178
179 enum dpif_op_type {
180     DPIF_OP_FLOW_PUT = 1,
181     DPIF_OP_FLOW_DEL,
182     DPIF_OP_EXECUTE,
183 };
184
185 struct dpif_flow_put {
186     /* Input. */
187     enum dpif_flow_put_flags flags; /* DPIF_FP_*. */
188     const struct nlattr *key;       /* Flow to put. */
189     size_t key_len;                 /* Length of 'key' in bytes. */
190     const struct nlattr *actions;   /* Actions to perform on flow. */
191     size_t actions_len;             /* Length of 'actions' in bytes. */
192
193     /* Output. */
194     struct dpif_flow_stats *stats;  /* Optional flow statistics. */
195 };
196
197 struct dpif_flow_del {
198     /* Input. */
199     const struct nlattr *key;       /* Flow to delete. */
200     size_t key_len;                 /* Length of 'key' in bytes. */
201
202     /* Output. */
203     struct dpif_flow_stats *stats;  /* Optional flow statistics. */
204 };
205
206 struct dpif_execute {
207     const struct nlattr *key;       /* Partial flow key (only for metadata). */
208     size_t key_len;                 /* Length of 'key' in bytes. */
209     const struct nlattr *actions;   /* Actions to execute on packet. */
210     size_t actions_len;             /* Length of 'actions' in bytes. */
211     const struct ofpbuf *packet;    /* Packet to execute. */
212 };
213
214 struct dpif_op {
215     enum dpif_op_type type;
216     int error;
217     union {
218         struct dpif_flow_put flow_put;
219         struct dpif_flow_del flow_del;
220         struct dpif_execute execute;
221     } u;
222 };
223
224 void dpif_operate(struct dpif *, struct dpif_op **ops, size_t n_ops);
225 \f
226 /* Upcalls. */
227
228 enum dpif_upcall_type {
229     DPIF_UC_MISS,               /* Miss in flow table. */
230     DPIF_UC_ACTION,             /* OVS_ACTION_ATTR_USERSPACE action. */
231     DPIF_N_UC_TYPES
232 };
233
234 const char *dpif_upcall_type_to_string(enum dpif_upcall_type);
235
236 /* A packet passed up from the datapath to userspace.
237  *
238  * If 'key' or 'actions' is nonnull, then it points into data owned by
239  * 'packet', so their memory cannot be freed separately.  (This is hardly a
240  * great way to do things but it works out OK for the dpif providers and
241  * clients that exist so far.)
242  */
243 struct dpif_upcall {
244     /* All types. */
245     enum dpif_upcall_type type;
246     struct ofpbuf *packet;      /* Packet data. */
247     struct nlattr *key;         /* Flow key. */
248     size_t key_len;             /* Length of 'key' in bytes. */
249
250     /* DPIF_UC_ACTION only. */
251     uint64_t userdata;          /* Argument to OVS_ACTION_ATTR_USERSPACE. */
252 };
253
254 int dpif_recv_set(struct dpif *, bool enable);
255 int dpif_recv(struct dpif *, struct dpif_upcall *, struct ofpbuf *);
256 void dpif_recv_purge(struct dpif *);
257 void dpif_recv_wait(struct dpif *);
258 \f
259 /* Miscellaneous. */
260
261 void dpif_get_netflow_ids(const struct dpif *,
262                           uint8_t *engine_type, uint8_t *engine_id);
263
264 int dpif_queue_to_priority(const struct dpif *, uint32_t queue_id,
265                            uint32_t *priority);
266
267 #ifdef  __cplusplus
268 }
269 #endif
270
271 #endif /* dpif.h */