lib/classifier: Lockless lookups.
[cascardo/ovs.git] / ofproto / ofproto.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 #ifndef OFPROTO_H
18 #define OFPROTO_H 1
19
20 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include "cfm.h"
26 #include "classifier.h"
27 #include "flow.h"
28 #include "meta-flow.h"
29 #include "netflow.h"
30 #include "sset.h"
31 #include "stp.h"
32
33 #ifdef  __cplusplus
34 extern "C" {
35 #endif
36
37 struct bfd_cfg;
38 struct cfm_settings;
39 struct cls_rule;
40 struct netdev;
41 struct netdev_stats;
42 struct ofport;
43 struct ofproto;
44 struct shash;
45 struct simap;
46 struct smap;
47
48 /* Needed for the lock annotations. */
49 extern struct ovs_mutex ofproto_mutex;
50
51 struct ofproto_controller_info {
52     bool is_connected;
53     enum ofp12_controller_role role;
54     struct {
55         const char *keys[4];
56         const char *values[4];
57         size_t n;
58     } pairs;
59 };
60
61 struct ofproto_sflow_options {
62     struct sset targets;
63     uint32_t sampling_rate;
64     uint32_t polling_interval;
65     uint32_t header_len;
66     uint32_t sub_id;
67     char *agent_device;
68     char *control_ip;
69 };
70
71 struct ofproto_ipfix_bridge_exporter_options {
72     struct sset targets;
73     uint32_t sampling_rate;
74     uint32_t obs_domain_id;  /* Bridge-wide Observation Domain ID. */
75     uint32_t obs_point_id;  /* Bridge-wide Observation Point ID. */
76     uint32_t cache_active_timeout;
77     uint32_t cache_max_flows;
78 };
79
80 struct ofproto_ipfix_flow_exporter_options {
81     uint32_t collector_set_id;
82     struct sset targets;
83     uint32_t cache_active_timeout;
84     uint32_t cache_max_flows;
85 };
86
87 struct ofproto_stp_settings {
88     stp_identifier system_id;
89     uint16_t priority;
90     uint16_t hello_time;
91     uint16_t max_age;
92     uint16_t fwd_delay;
93 };
94
95 struct ofproto_stp_status {
96     bool enabled;               /* If false, ignore other members. */
97     stp_identifier bridge_id;
98     stp_identifier designated_root;
99     int root_path_cost;
100 };
101
102 struct ofproto_port_stp_settings {
103     bool enable;
104     uint8_t port_num;           /* In the range 1-255, inclusive. */
105     uint8_t priority;
106     uint16_t path_cost;
107 };
108
109 struct ofproto_port_stp_status {
110     bool enabled;               /* If false, ignore other members. */
111     int port_id;
112     enum stp_state state;
113     unsigned int sec_in_state;
114     enum stp_role role;
115 };
116
117 struct ofproto_port_stp_stats {
118     bool enabled;               /* If false, ignore other members. */
119     int tx_count;               /* Number of BPDUs transmitted. */
120     int rx_count;               /* Number of valid BPDUs received. */
121     int error_count;            /* Number of bad BPDUs received. */
122 };
123
124 struct ofproto_port_queue {
125     uint32_t queue;             /* Queue ID. */
126     uint8_t dscp;               /* DSCP bits (e.g. [0, 63]). */
127 };
128
129 struct ofproto_mcast_snooping_settings {
130     bool flood_unreg;           /* If true, flood unregistered packets to all
131                                    all ports. If false, send only to ports
132                                    connected to multicast routers. */
133     unsigned int idle_time;     /* Entry is removed after the idle time
134                                  * in seconds. */
135     unsigned int max_entries;   /* Size of the multicast snooping table. */
136 };
137
138 /* How the switch should act if the controller cannot be contacted. */
139 enum ofproto_fail_mode {
140     OFPROTO_FAIL_SECURE,        /* Preserve flow table. */
141     OFPROTO_FAIL_STANDALONE     /* Act as a standalone switch. */
142 };
143
144 enum ofproto_band {
145     OFPROTO_IN_BAND,            /* In-band connection to controller. */
146     OFPROTO_OUT_OF_BAND         /* Out-of-band connection to controller. */
147 };
148
149 struct ofproto_controller {
150     char *target;               /* e.g. "tcp:127.0.0.1" */
151     int max_backoff;            /* Maximum reconnection backoff, in seconds. */
152     int probe_interval;         /* Max idle time before probing, in seconds. */
153     enum ofproto_band band;     /* In-band or out-of-band? */
154     bool enable_async_msgs;     /* Initially enable asynchronous messages? */
155
156     /* OpenFlow packet-in rate-limiting. */
157     int rate_limit;             /* Max packet-in rate in packets per second. */
158     int burst_limit;            /* Limit on accumulating packet credits. */
159
160     uint8_t dscp;               /* DSCP value for controller connection. */
161 };
162
163 void ofproto_enumerate_types(struct sset *types);
164 const char *ofproto_normalize_type(const char *);
165
166 int ofproto_enumerate_names(const char *type, struct sset *names);
167 void ofproto_parse_name(const char *name, char **dp_name, char **dp_type);
168
169 /* An interface hint element, which is used by ofproto_init() to
170  * describe the caller's understanding of the startup state. */
171 struct iface_hint {
172     char *br_name;              /* Name of owning bridge. */
173     char *br_type;              /* Type of owning bridge. */
174     ofp_port_t ofp_port;        /* OpenFlow port number. */
175 };
176
177 void ofproto_init(const struct shash *iface_hints);
178
179 int ofproto_type_run(const char *datapath_type);
180 void ofproto_type_wait(const char *datapath_type);
181
182 int ofproto_create(const char *datapath, const char *datapath_type,
183                    struct ofproto **ofprotop);
184 void ofproto_destroy(struct ofproto *);
185 int ofproto_delete(const char *name, const char *type);
186
187 int ofproto_run(struct ofproto *);
188 void ofproto_wait(struct ofproto *);
189 bool ofproto_is_alive(const struct ofproto *);
190
191 void ofproto_get_memory_usage(const struct ofproto *, struct simap *);
192 void ofproto_type_get_memory_usage(const char *datapath_type, struct simap *);
193
194 /* A port within an OpenFlow switch.
195  *
196  * 'name' and 'type' are suitable for passing to netdev_open(). */
197 struct ofproto_port {
198     char *name;                 /* Network device name, e.g. "eth0". */
199     char *type;                 /* Network device type, e.g. "system". */
200     ofp_port_t ofp_port;        /* OpenFlow port number. */
201 };
202 void ofproto_port_clone(struct ofproto_port *, const struct ofproto_port *);
203 void ofproto_port_destroy(struct ofproto_port *);
204
205 struct ofproto_port_dump {
206     const struct ofproto *ofproto;
207     int error;
208     void *state;
209 };
210 void ofproto_port_dump_start(struct ofproto_port_dump *,
211                              const struct ofproto *);
212 bool ofproto_port_dump_next(struct ofproto_port_dump *, struct ofproto_port *);
213 int ofproto_port_dump_done(struct ofproto_port_dump *);
214
215 /* Iterates through each OFPROTO_PORT in OFPROTO, using DUMP as state.
216  *
217  * Arguments all have pointer type.
218  *
219  * If you break out of the loop, then you need to free the dump structure by
220  * hand using ofproto_port_dump_done(). */
221 #define OFPROTO_PORT_FOR_EACH(OFPROTO_PORT, DUMP, OFPROTO)  \
222     for (ofproto_port_dump_start(DUMP, OFPROTO);            \
223          (ofproto_port_dump_next(DUMP, OFPROTO_PORT)        \
224           ? true                                            \
225           : (ofproto_port_dump_done(DUMP), false));         \
226         )
227
228 #define OFPROTO_FLOW_LIMIT_DEFAULT 200000
229 #define OFPROTO_MAX_IDLE_DEFAULT 1500
230
231 const char *ofproto_port_open_type(const char *datapath_type,
232                                    const char *port_type);
233 int ofproto_port_add(struct ofproto *, struct netdev *, ofp_port_t *ofp_portp);
234 int ofproto_port_del(struct ofproto *, ofp_port_t ofp_port);
235 int ofproto_port_get_stats(const struct ofport *, struct netdev_stats *stats);
236
237 int ofproto_port_query_by_name(const struct ofproto *, const char *devname,
238                                struct ofproto_port *);
239
240 /* Top-level configuration. */
241 uint64_t ofproto_get_datapath_id(const struct ofproto *);
242 void ofproto_set_datapath_id(struct ofproto *, uint64_t datapath_id);
243 void ofproto_set_controllers(struct ofproto *,
244                              const struct ofproto_controller *, size_t n,
245                              uint32_t allowed_versions);
246 void ofproto_set_fail_mode(struct ofproto *, enum ofproto_fail_mode fail_mode);
247 void ofproto_reconnect_controllers(struct ofproto *);
248 void ofproto_set_extra_in_band_remotes(struct ofproto *,
249                                        const struct sockaddr_in *, size_t n);
250 void ofproto_set_in_band_queue(struct ofproto *, int queue_id);
251 void ofproto_set_flow_limit(unsigned limit);
252 void ofproto_set_max_idle(unsigned max_idle);
253 void ofproto_set_forward_bpdu(struct ofproto *, bool forward_bpdu);
254 void ofproto_set_mac_table_config(struct ofproto *, unsigned idle_time,
255                                   size_t max_entries);
256 int ofproto_set_mcast_snooping(struct ofproto *ofproto,
257                               const struct ofproto_mcast_snooping_settings *s);
258 int ofproto_port_set_mcast_snooping(struct ofproto *ofproto, void *aux,
259                                     bool flood);
260 void ofproto_set_threads(int n_handlers, int n_revalidators);
261 void ofproto_set_dp_desc(struct ofproto *, const char *dp_desc);
262 int ofproto_set_snoops(struct ofproto *, const struct sset *snoops);
263 int ofproto_set_netflow(struct ofproto *,
264                         const struct netflow_options *nf_options);
265 int ofproto_set_sflow(struct ofproto *, const struct ofproto_sflow_options *);
266 int ofproto_set_ipfix(struct ofproto *,
267                       const struct ofproto_ipfix_bridge_exporter_options *,
268                       const struct ofproto_ipfix_flow_exporter_options *,
269                       size_t);
270 void ofproto_set_flow_restore_wait(bool flow_restore_wait_db);
271 bool ofproto_get_flow_restore_wait(void);
272 int ofproto_set_stp(struct ofproto *, const struct ofproto_stp_settings *);
273 int ofproto_get_stp_status(struct ofproto *, struct ofproto_stp_status *);
274
275 /* Configuration of ports. */
276 void ofproto_port_unregister(struct ofproto *, ofp_port_t ofp_port);
277
278 void ofproto_port_clear_cfm(struct ofproto *, ofp_port_t ofp_port);
279 void ofproto_port_set_cfm(struct ofproto *, ofp_port_t ofp_port,
280                           const struct cfm_settings *);
281 void ofproto_port_set_bfd(struct ofproto *, ofp_port_t ofp_port,
282                           const struct smap *cfg);
283 bool ofproto_port_bfd_status_changed(struct ofproto *, ofp_port_t ofp_port);
284 int ofproto_port_get_bfd_status(struct ofproto *, ofp_port_t ofp_port,
285                                 struct smap *);
286 int ofproto_port_is_lacp_current(struct ofproto *, ofp_port_t ofp_port);
287 int ofproto_port_set_stp(struct ofproto *, ofp_port_t ofp_port,
288                          const struct ofproto_port_stp_settings *);
289 int ofproto_port_get_stp_status(struct ofproto *, ofp_port_t ofp_port,
290                                 struct ofproto_port_stp_status *);
291 int ofproto_port_get_stp_stats(struct ofproto *, ofp_port_t ofp_port,
292                                struct ofproto_port_stp_stats *);
293 int ofproto_port_set_queues(struct ofproto *, ofp_port_t ofp_port,
294                             const struct ofproto_port_queue *,
295                             size_t n_queues);
296
297 /* The behaviour of the port regarding VLAN handling */
298 enum port_vlan_mode {
299     /* This port is an access port.  'vlan' is the VLAN ID.  'trunks' is
300      * ignored. */
301     PORT_VLAN_ACCESS,
302
303     /* This port is a trunk.  'trunks' is the set of trunks. 'vlan' is
304      * ignored. */
305     PORT_VLAN_TRUNK,
306
307     /* Untagged incoming packets are part of 'vlan', as are incoming packets
308      * tagged with 'vlan'.  Outgoing packets tagged with 'vlan' stay tagged.
309      * Other VLANs in 'trunks' are trunked. */
310     PORT_VLAN_NATIVE_TAGGED,
311
312     /* Untagged incoming packets are part of 'vlan', as are incoming packets
313      * tagged with 'vlan'.  Outgoing packets tagged with 'vlan' are untagged.
314      * Other VLANs in 'trunks' are trunked. */
315     PORT_VLAN_NATIVE_UNTAGGED
316 };
317
318 /* Configuration of bundles. */
319 struct ofproto_bundle_settings {
320     char *name;                 /* For use in log messages. */
321
322     ofp_port_t *slaves;         /* OpenFlow port numbers for slaves. */
323     size_t n_slaves;
324
325     enum port_vlan_mode vlan_mode; /* Selects mode for vlan and trunks */
326     int vlan;                   /* VLAN VID, except for PORT_VLAN_TRUNK. */
327     unsigned long *trunks;      /* vlan_bitmap, except for PORT_VLAN_ACCESS. */
328     bool use_priority_tags;     /* Use 802.1p tag for frames in VLAN 0? */
329
330     struct bond_settings *bond; /* Must be nonnull iff if n_slaves > 1. */
331
332     struct lacp_settings *lacp;              /* Nonnull to enable LACP. */
333     struct lacp_slave_settings *lacp_slaves; /* Array of n_slaves elements. */
334
335     /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
336      *
337      * This is deprecated.  It is only for compatibility with broken device
338      * drivers in old versions of Linux that do not properly support VLANs when
339      * VLAN devices are not used.  When broken device drivers are no longer in
340      * widespread use, we will delete these interfaces. */
341     ofp_port_t realdev_ofp_port;/* OpenFlow port number of real device. */
342 };
343
344 int ofproto_bundle_register(struct ofproto *, void *aux,
345                             const struct ofproto_bundle_settings *);
346 int ofproto_bundle_unregister(struct ofproto *, void *aux);
347
348 /* Configuration of mirrors. */
349 struct ofproto_mirror_settings {
350     /* Name for log messages. */
351     char *name;
352
353     /* Bundles that select packets for mirroring upon ingress.  */
354     void **srcs;                /* A set of registered ofbundle handles. */
355     size_t n_srcs;
356
357     /* Bundles that select packets for mirroring upon egress.  */
358     void **dsts;                /* A set of registered ofbundle handles. */
359     size_t n_dsts;
360
361     /* VLANs of packets to select for mirroring. */
362     unsigned long *src_vlans;   /* vlan_bitmap, NULL selects all VLANs. */
363
364     /* Output (mutually exclusive). */
365     void *out_bundle;           /* A registered ofbundle handle or NULL. */
366     uint16_t out_vlan;          /* Output VLAN, only if out_bundle is NULL. */
367 };
368
369 int ofproto_mirror_register(struct ofproto *, void *aux,
370                             const struct ofproto_mirror_settings *);
371 int ofproto_mirror_unregister(struct ofproto *, void *aux);
372 int ofproto_mirror_get_stats(struct ofproto *, void *aux,
373                              uint64_t *packets, uint64_t *bytes);
374
375 int ofproto_set_flood_vlans(struct ofproto *, unsigned long *flood_vlans);
376 bool ofproto_is_mirror_output_bundle(const struct ofproto *, void *aux);
377
378 /* Configuration of OpenFlow tables. */
379 struct ofproto_table_settings {
380     char *name;                 /* Name exported via OpenFlow or NULL. */
381     unsigned int max_flows;     /* Maximum number of flows or UINT_MAX. */
382
383     /* These members determine the handling of an attempt to add a flow that
384      * would cause the table to have more than 'max_flows' flows.
385      *
386      * If 'groups' is NULL, overflows will be rejected with an error.
387      *
388      * If 'groups' is nonnull, an overflow will cause a flow to be removed.
389      * The flow to be removed is chosen to give fairness among groups
390      * distinguished by different values for the subfields within 'groups'. */
391     struct mf_subfield *groups;
392     size_t n_groups;
393
394     /*
395      * Fields for which prefix trie lookup is maintained.
396      */
397     unsigned int n_prefix_fields;
398     enum mf_field_id prefix_fields[CLS_MAX_TRIES];
399 };
400
401 extern const enum mf_field_id default_prefix_fields[2];
402 BUILD_ASSERT_DECL(ARRAY_SIZE(default_prefix_fields) <= CLS_MAX_TRIES);
403
404 int ofproto_get_n_tables(const struct ofproto *);
405 uint8_t ofproto_get_n_visible_tables(const struct ofproto *);
406 void ofproto_configure_table(struct ofproto *, int table_id,
407                              const struct ofproto_table_settings *);
408
409 /* Configuration querying. */
410 bool ofproto_has_snoops(const struct ofproto *);
411 void ofproto_get_snoops(const struct ofproto *, struct sset *);
412 void ofproto_get_all_flows(struct ofproto *p, struct ds *);
413 void ofproto_get_netflow_ids(const struct ofproto *,
414                              uint8_t *engine_type, uint8_t *engine_id);
415
416 void ofproto_get_ofproto_controller_info(const struct ofproto *, struct shash *);
417 void ofproto_free_ofproto_controller_info(struct shash *);
418
419 bool ofproto_port_cfm_status_changed(struct ofproto *, ofp_port_t ofp_port);
420
421 int ofproto_port_get_cfm_status(const struct ofproto *,
422                                 ofp_port_t ofp_port,
423                                 struct cfm_status *);
424 \f
425 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
426  *
427  * This is deprecated.  It is only for compatibility with broken device drivers
428  * in old versions of Linux that do not properly support VLANs when VLAN
429  * devices are not used.  When broken device drivers are no longer in
430  * widespread use, we will delete these interfaces. */
431
432 void ofproto_get_vlan_usage(struct ofproto *, unsigned long int *vlan_bitmap);
433 bool ofproto_has_vlan_usage_changed(const struct ofproto *);
434 int ofproto_port_set_realdev(struct ofproto *, ofp_port_t vlandev_ofp_port,
435                              ofp_port_t realdev_ofp_port, int vid);
436 \f
437 /* Table configuration */
438
439 enum ofproto_table_config {
440     /* Send to controller. */
441     OFPROTO_TABLE_MISS_CONTROLLER = OFPTC11_TABLE_MISS_CONTROLLER,
442
443     /* Continue to the next table in the pipeline (OpenFlow 1.0 behavior). */
444     OFPROTO_TABLE_MISS_CONTINUE   = OFPTC11_TABLE_MISS_CONTINUE,
445
446     /* Drop the packet. */
447     OFPROTO_TABLE_MISS_DROP       = OFPTC11_TABLE_MISS_DROP,
448
449     /* The default miss behaviour for the OpenFlow version of the controller a
450      * packet_in message would be sent to..  For pre-OF1.3 controllers, send
451      * packet_in to controller.  For OF1.3+ controllers, drop. */
452     OFPROTO_TABLE_MISS_DEFAULT    = 3,
453 };
454
455 enum ofproto_table_config ofproto_table_get_config(const struct ofproto *,
456                                                    uint8_t table_id);
457
458 #ifdef  __cplusplus
459 }
460 #endif
461
462 #endif /* ofproto.h */