openflow: Remove OFPG11_*
[cascardo/ovs.git] / ofproto / ofproto-provider.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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_OFPROTO_PROVIDER_H
18 #define OFPROTO_OFPROTO_PROVIDER_H 1
19
20 /* Definitions for use within ofproto.
21  *
22  *
23  * Thread-safety
24  * =============
25  *
26  * Lots of ofproto data structures are only accessed from a single thread.
27  * Those data structures are generally not thread-safe.
28  *
29  * The ofproto-dpif ofproto implementation accesses the flow table from
30  * multiple threads, including modifying the flow table from multiple threads
31  * via the "learn" action, so the flow table and various structures that index
32  * it have been made thread-safe.  Refer to comments on individual data
33  * structures for details.
34  */
35
36 #include "cfm.h"
37 #include "classifier.h"
38 #include "guarded-list.h"
39 #include "heap.h"
40 #include "hindex.h"
41 #include "list.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
44 #include "ofp-util.h"
45 #include "ofproto/ofproto.h"
46 #include "ovs-atomic.h"
47 #include "ovs-rcu.h"
48 #include "ovs-thread.h"
49 #include "shash.h"
50 #include "simap.h"
51 #include "timeval.h"
52
53 struct match;
54 struct ofputil_flow_mod;
55 struct bfd_cfg;
56 struct meter;
57 struct ofoperation;
58
59 extern struct ovs_mutex ofproto_mutex;
60
61 /* An OpenFlow switch.
62  *
63  * With few exceptions, ofproto implementations may look at these fields but
64  * should not modify them. */
65 struct ofproto {
66     struct hmap_node hmap_node; /* In global 'all_ofprotos' hmap. */
67     const struct ofproto_class *ofproto_class;
68     char *type;                 /* Datapath type. */
69     char *name;                 /* Datapath name. */
70
71     /* Settings. */
72     uint64_t fallback_dpid;     /* Datapath ID if no better choice found. */
73     uint64_t datapath_id;       /* Datapath ID. */
74     bool forward_bpdu;          /* Option to allow forwarding of BPDU frames
75                                  * when NORMAL action is invoked. */
76     char *mfr_desc;             /* Manufacturer (NULL for default). */
77     char *hw_desc;              /* Hardware (NULL for default). */
78     char *sw_desc;              /* Software version (NULL for default). */
79     char *serial_desc;          /* Serial number (NULL for default). */
80     char *dp_desc;              /* Datapath description (NULL for default). */
81     enum ofp_config_flags frag_handling; /* One of OFPC_*.  */
82
83     /* Datapath. */
84     struct hmap ports;          /* Contains "struct ofport"s. */
85     struct shash port_by_name;
86     struct simap ofp_requests;  /* OpenFlow port number requests. */
87     uint16_t alloc_port_no;     /* Last allocated OpenFlow port number. */
88     uint16_t max_ports;         /* Max possible OpenFlow port num, plus one. */
89     struct hmap ofport_usage;   /* Map ofport to last used time. */
90     uint64_t change_seq;        /* Change sequence for netdev status. */
91
92     /* Flow tables. */
93     long long int eviction_group_timer; /* For rate limited reheapification. */
94     struct oftable *tables;
95     int n_tables;
96     cls_version_t tables_version;  /* Controls which rules are visible to
97                                     * table lookups. */
98
99     /* Rules indexed on their cookie values, in all flow tables. */
100     struct hindex cookies OVS_GUARDED_BY(ofproto_mutex);
101     struct hmap learned_cookies OVS_GUARDED_BY(ofproto_mutex);
102
103     /* List of expirable flows, in all flow tables. */
104     struct ovs_list expirable OVS_GUARDED_BY(ofproto_mutex);
105
106     /* Meter table.
107      * OpenFlow meters start at 1.  To avoid confusion we leave the first
108      * pointer in the array un-used, and index directly with the OpenFlow
109      * meter_id. */
110     struct ofputil_meter_features meter_features;
111     struct meter **meters; /* 'meter_features.max_meter' + 1 pointers. */
112
113     /* OpenFlow connections. */
114     struct connmgr *connmgr;
115
116     /* Delayed rule executions.
117      *
118      * We delay calls to ->ofproto_class->rule_execute() past releasing
119      * ofproto_mutex during a flow_mod, because otherwise a "learn" action
120      * triggered by the executing the packet would try to recursively modify
121      * the flow table and reacquire the global lock. */
122     struct guarded_list rule_executes; /* Contains "struct rule_execute"s. */
123
124     /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
125      *
126      * This is deprecated.  It is only for compatibility with broken device
127      * drivers in old versions of Linux that do not properly support VLANs when
128      * VLAN devices are not used.  When broken device drivers are no longer in
129      * widespread use, we will delete these interfaces. */
130     unsigned long int *vlan_bitmap; /* 4096-bit bitmap of in-use VLANs. */
131     bool vlans_changed;             /* True if new VLANs are in use. */
132     int min_mtu;                    /* Current MTU of non-internal ports. */
133
134     /* Groups. */
135     struct ovs_rwlock groups_rwlock;
136     struct hmap groups OVS_GUARDED;   /* Contains "struct ofgroup"s. */
137     uint32_t n_groups[4] OVS_GUARDED; /* # of existing groups of each type. */
138     struct ofputil_group_features ogf;
139 };
140
141 void ofproto_init_tables(struct ofproto *, int n_tables);
142 void ofproto_init_max_ports(struct ofproto *, uint16_t max_ports);
143
144 struct ofproto *ofproto_lookup(const char *name);
145 struct ofport *ofproto_get_port(const struct ofproto *, ofp_port_t ofp_port);
146
147 /* An OpenFlow port within a "struct ofproto".
148  *
149  * The port's name is netdev_get_name(port->netdev).
150  *
151  * With few exceptions, ofproto implementations may look at these fields but
152  * should not modify them. */
153 struct ofport {
154     struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
155     struct ofproto *ofproto;    /* The ofproto that contains this port. */
156     struct netdev *netdev;
157     struct ofputil_phy_port pp;
158     ofp_port_t ofp_port;        /* OpenFlow port number. */
159     uint64_t change_seq;
160     long long int created;      /* Time created, in msec. */
161     int mtu;
162 };
163
164 void ofproto_port_set_state(struct ofport *, enum ofputil_port_state);
165
166 /* OpenFlow table flags:
167  *
168  *   - "Hidden" tables are not included in OpenFlow operations that operate on
169  *     "all tables".  For example, a request for flow stats on all tables will
170  *     omit flows in hidden tables, table stats requests will omit the table
171  *     entirely, and the switch features reply will not count the hidden table.
172  *
173  *     However, operations that specifically name the particular table still
174  *     operate on it.  For example, flow_mods and flow stats requests on a
175  *     hidden table work.
176  *
177  *     To avoid gaps in table IDs (which have unclear validity in OpenFlow),
178  *     hidden tables must be the highest-numbered tables that a provider
179  *     implements.
180  *
181  *   - "Read-only" tables can't be changed through OpenFlow operations.  (At
182  *     the moment all flow table operations go effectively through OpenFlow, so
183  *     this means that read-only tables can't be changed at all after the
184  *     read-only flag is set.)
185  *
186  * The generic ofproto layer never sets these flags.  An ofproto provider can
187  * set them if it is appropriate.
188  */
189 enum oftable_flags {
190     OFTABLE_HIDDEN = 1 << 0,   /* Hide from most OpenFlow operations. */
191     OFTABLE_READONLY = 1 << 1  /* Don't allow OpenFlow controller to change
192                                   this table. */
193 };
194
195 /* A flow table within a "struct ofproto".
196  *
197  *
198  * Thread-safety
199  * =============
200  *
201  * Adding or removing rules requires holding ofproto_mutex.
202  *
203  * Rules in 'cls' are RCU protected.  For extended access to a rule, try
204  * incrementing its ref_count with ofproto_rule_try_ref(), or
205  * ofproto_rule_ref(), if the rule is still known to be in 'cls'.  A rule
206  * will be freed using ovsrcu_postpone() once its 'ref_count' reaches zero.
207  *
208  * Modifying a rule requires the rule's own mutex.
209  *
210  * Freeing a rule requires ofproto_mutex.  After removing the rule from the
211  * classifier, release a ref_count from the rule ('cls''s reference to the
212  * rule).
213  *
214  * Refer to the thread-safety notes on struct rule for more information.*/
215 struct oftable {
216     enum oftable_flags flags;
217     struct classifier cls;      /* Contains "struct rule"s. */
218     char *name;                 /* Table name exposed via OpenFlow, or NULL. */
219
220     /* Maximum number of flows or UINT_MAX if there is no limit besides any
221      * limit imposed by resource limitations. */
222     unsigned int max_flows;
223     /* Current number of flows, not counting temporary duplicates nor deferred
224      * deletions. */
225     unsigned int n_flows;
226
227     /* These members determine the handling of an attempt to add a flow that
228      * would cause the table to have more than 'max_flows' flows.
229      *
230      * If 'eviction_fields' is NULL, overflows will be rejected with an error.
231      *
232      * If 'eviction_fields' is nonnull (regardless of whether n_eviction_fields
233      * is nonzero), an overflow will cause a flow to be removed.  The flow to
234      * be removed is chosen to give fairness among groups distinguished by
235      * different values for the subfields within 'groups'. */
236     struct mf_subfield *eviction_fields;
237     size_t n_eviction_fields;
238
239     /* Eviction groups.
240      *
241      * When a flow is added that would cause the table to have more than
242      * 'max_flows' flows, and 'eviction_fields' is nonnull, these groups are
243      * used to decide which rule to evict: the rule is chosen from the eviction
244      * group that contains the greatest number of rules.*/
245     uint32_t eviction_group_id_basis;
246     struct hmap eviction_groups_by_id;
247     struct heap eviction_groups_by_size;
248
249     /* Flow table miss handling configuration. */
250     ATOMIC(enum ofputil_table_miss) miss_config;
251
252     /* Eviction is enabled if either the client (vswitchd) enables it or an
253      * OpenFlow controller enables it; thus, a nonzero value indicates that
254      * eviction is enabled.  */
255 #define EVICTION_CLIENT  (1 << 0)  /* Set to 1 if client enables eviction. */
256 #define EVICTION_OPENFLOW (1 << 1) /* Set to 1 if OpenFlow enables eviction. */
257     unsigned int eviction;
258
259     atomic_ulong n_matched;
260     atomic_ulong n_missed;
261 };
262
263 /* Assigns TABLE to each oftable, in turn, in OFPROTO.
264  *
265  * All parameters are evaluated multiple times. */
266 #define OFPROTO_FOR_EACH_TABLE(TABLE, OFPROTO)              \
267     for ((TABLE) = (OFPROTO)->tables;                       \
268          (TABLE) < &(OFPROTO)->tables[(OFPROTO)->n_tables]; \
269          (TABLE)++)
270
271 /* An OpenFlow flow within a "struct ofproto".
272  *
273  * With few exceptions, ofproto implementations may look at these fields but
274  * should not modify them.
275  *
276  *
277  * Thread-safety
278  * =============
279  *
280  * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
281  * the classifier rule->ofproto->tables[rule->table_id].cls.  The text below
282  * calls this classifier 'cls'.
283  *
284  * Motivation
285  * ----------
286  *
287  * The thread safety rules described here for "struct rule" are motivated by
288  * two goals:
289  *
290  *    - Prevent threads that read members of "struct rule" from reading bad
291  *      data due to changes by some thread concurrently modifying those
292  *      members.
293  *
294  *    - Prevent two threads making changes to members of a given "struct rule"
295  *      from interfering with each other.
296  *
297  *
298  * Rules
299  * -----
300  *
301  * A rule 'rule' may be accessed without a risk of being freed by a thread
302  * until the thread quiesces (i.e., rules are RCU protected and destructed
303  * using ovsrcu_postpone()).  Code that needs to hold onto a rule for a
304  * while should increment 'rule->ref_count' either with ofproto_rule_ref()
305  * (if 'ofproto_mutex' is held), or with ofproto_rule_try_ref() (when some
306  * other thread might remove the rule from 'cls').  ofproto_rule_try_ref()
307  * will fail if the rule has already been scheduled for destruction.
308  *
309  * 'rule->ref_count' protects 'rule' from being freed.  It doesn't protect the
310  * rule from being deleted from 'cls' (that's 'ofproto_mutex') and it doesn't
311  * protect members of 'rule' from modification (that's 'rule->mutex').
312  *
313  * 'rule->mutex' protects the members of 'rule' from modification.  It doesn't
314  * protect the rule from being deleted from 'cls' (that's 'ofproto_mutex') and
315  * it doesn't prevent the rule from being freed (that's 'rule->ref_count').
316  *
317  * Regarding thread safety, the members of a rule fall into the following
318  * categories:
319  *
320  *    - Immutable.  These members are marked 'const'.
321  *
322  *    - Members that may be safely read or written only by code holding
323  *      ofproto_mutex.  These are marked OVS_GUARDED_BY(ofproto_mutex).
324  *
325  *    - Members that may be safely read only by code holding ofproto_mutex or
326  *      'rule->mutex', and safely written only by coding holding ofproto_mutex
327  *      AND 'rule->mutex'.  These are marked OVS_GUARDED.
328  */
329 struct rule {
330     /* Where this rule resides in an OpenFlow switch.
331      *
332      * These are immutable once the rule is constructed, hence 'const'. */
333     struct ofproto *const ofproto; /* The ofproto that contains this rule. */
334     const struct cls_rule cr;      /* In owning ofproto's classifier. */
335     const uint8_t table_id;        /* Index in ofproto's 'tables' array. */
336     bool removed;                  /* Rule has been removed from the ofproto
337                                     * data structures. */
338
339     /* Protects members marked OVS_GUARDED.
340      * Readers only need to hold this mutex.
341      * Writers must hold both this mutex AND ofproto_mutex.
342      * By implication writers can read *without* taking this mutex while they
343      * hold ofproto_mutex. */
344     struct ovs_mutex mutex OVS_ACQ_AFTER(ofproto_mutex);
345
346     /* Number of references.
347      * The classifier owns one reference.
348      * Any thread trying to keep a rule from being freed should hold its own
349      * reference. */
350     struct ovs_refcount ref_count;
351
352     /* A "flow cookie" is the OpenFlow name for a 64-bit value associated with
353      * a flow.. */
354     ovs_be64 flow_cookie OVS_GUARDED;
355     struct hindex_node cookie_node OVS_GUARDED_BY(ofproto_mutex);
356
357     enum ofputil_flow_mod_flags flags OVS_GUARDED;
358
359     /* Timeouts. */
360     uint16_t hard_timeout OVS_GUARDED; /* In seconds from ->modified. */
361     uint16_t idle_timeout OVS_GUARDED; /* In seconds from ->used. */
362
363     /* Eviction precedence. */
364     const uint16_t importance;
365
366     /* Removal reason for sending flow removed message.
367      * Used only if 'flags' has OFPUTIL_FF_SEND_FLOW_REM set and if the
368      * value is not OVS_OFPRR_NONE. */
369     uint8_t removed_reason;
370
371     /* Eviction groups (see comment on struct eviction_group for explanation) .
372      *
373      * 'eviction_group' is this rule's eviction group, or NULL if it is not in
374      * any eviction group.  When 'eviction_group' is nonnull, 'evg_node' is in
375      * the ->eviction_group->rules hmap. */
376     struct eviction_group *eviction_group OVS_GUARDED_BY(ofproto_mutex);
377     struct heap_node evg_node OVS_GUARDED_BY(ofproto_mutex);
378
379     /* OpenFlow actions.  See struct rule_actions for more thread-safety
380      * notes. */
381     const struct rule_actions * const actions;
382
383     /* In owning meter's 'rules' list.  An empty list if there is no meter. */
384     struct ovs_list meter_list_node OVS_GUARDED_BY(ofproto_mutex);
385
386     /* Flow monitors (e.g. for NXST_FLOW_MONITOR, related to struct ofmonitor).
387      *
388      * 'add_seqno' is the sequence number when this rule was created.
389      * 'modify_seqno' is the sequence number when this rule was last modified.
390      * See 'monitor_seqno' in connmgr.c for more information. */
391     enum nx_flow_monitor_flags monitor_flags OVS_GUARDED_BY(ofproto_mutex);
392     uint64_t add_seqno OVS_GUARDED_BY(ofproto_mutex);
393     uint64_t modify_seqno OVS_GUARDED_BY(ofproto_mutex);
394
395     /* Optimisation for flow expiry.  In ofproto's 'expirable' list if this
396      * rule is expirable, otherwise empty. */
397     struct ovs_list expirable OVS_GUARDED_BY(ofproto_mutex);
398
399     /* Times.  Last so that they are more likely close to the stats managed
400      * by the provider. */
401     long long int created OVS_GUARDED; /* Creation time. */
402
403     /* Must hold 'mutex' for both read/write, 'ofproto_mutex' not needed. */
404     long long int modified OVS_GUARDED; /* Time of last modification. */
405 };
406
407 void ofproto_rule_ref(struct rule *);
408 bool ofproto_rule_try_ref(struct rule *);
409 void ofproto_rule_unref(struct rule *);
410
411 static inline const struct rule_actions * rule_get_actions(const struct rule *);
412 static inline bool rule_is_table_miss(const struct rule *);
413 static inline bool rule_is_hidden(const struct rule *);
414
415 /* A set of actions within a "struct rule".
416  *
417  *
418  * Thread-safety
419  * =============
420  *
421  * A struct rule_actions may be accessed without a risk of being freed by
422  * code that holds 'rule->mutex' (where 'rule' is the rule for which
423  * 'rule->actions == actions') or during the RCU active period.
424  *
425  * All members are immutable: they do not change during the rule's
426  * lifetime. */
427 struct rule_actions {
428     /* Flags.
429      *
430      * 'has_meter' is true if 'ofpacts' contains an OFPACT_METER action.
431      *
432      * 'has_learn_with_delete' is true if 'ofpacts' contains an OFPACT_LEARN
433      * action whose flags include NX_LEARN_F_DELETE_LEARNED. */
434     bool has_meter;
435     bool has_learn_with_delete;
436
437     /* Actions. */
438     uint32_t ofpacts_len;         /* Size of 'ofpacts', in bytes. */
439     struct ofpact ofpacts[];      /* Sequence of "struct ofpacts". */
440 };
441 BUILD_ASSERT_DECL(offsetof(struct rule_actions, ofpacts) % OFPACT_ALIGNTO == 0);
442
443 const struct rule_actions *rule_actions_create(const struct ofpact *, size_t);
444 void rule_actions_destroy(const struct rule_actions *);
445 bool ofproto_rule_has_out_port(const struct rule *, ofp_port_t port)
446     OVS_REQUIRES(ofproto_mutex);
447
448 /* A set of rules to which an OpenFlow operation applies. */
449 struct rule_collection {
450     struct rule **rules;        /* The rules. */
451     size_t n;                   /* Number of rules collected. */
452
453     size_t capacity;            /* Number of rules that will fit in 'rules'. */
454     struct rule *stub[64];      /* Preallocated rules to avoid malloc(). */
455 };
456
457 void rule_collection_init(struct rule_collection *);
458 void rule_collection_add(struct rule_collection *, struct rule *);
459 void rule_collection_ref(struct rule_collection *) OVS_REQUIRES(ofproto_mutex);
460 void rule_collection_unref(struct rule_collection *);
461 void rule_collection_destroy(struct rule_collection *);
462
463 /* Limits the number of flows allowed in the datapath. Only affects the
464  * ofproto-dpif implementation. */
465 extern unsigned ofproto_flow_limit;
466
467 /* Maximum idle time (in ms) for flows to be cached in the datapath.
468  * Revalidators may expire flows more quickly than the configured value based
469  * on system load and other factors. This variable is subject to change. */
470 extern unsigned ofproto_max_idle;
471
472 /* Number of upcall handler and revalidator threads. Only affects the
473  * ofproto-dpif implementation. */
474 extern size_t n_handlers, n_revalidators;
475
476 /* Number of rx queues to be created for each dpdk interface. */
477 extern size_t n_dpdk_rxqs;
478
479 /* Cpu mask for pmd threads. */
480 extern char *pmd_cpu_mask;
481
482 static inline struct rule *rule_from_cls_rule(const struct cls_rule *);
483
484 void ofproto_rule_expire(struct rule *rule, uint8_t reason)
485     OVS_REQUIRES(ofproto_mutex);
486 void ofproto_rule_delete(struct ofproto *, struct rule *)
487     OVS_EXCLUDED(ofproto_mutex);
488 void ofproto_rule_reduce_timeouts(struct rule *rule, uint16_t idle_timeout,
489                                   uint16_t hard_timeout)
490     OVS_EXCLUDED(ofproto_mutex);
491
492 /* A group within a "struct ofproto".
493  *
494  * With few exceptions, ofproto implementations may look at these fields but
495  * should not modify them. */
496 struct ofgroup {
497     struct hmap_node hmap_node OVS_GUARDED; /* In ofproto's "groups" hmap. */
498
499     /* Number of references.
500      *
501      * This is needed to keep track of references to the group in the xlate
502      * module.
503      *
504      * If the main thread removes the group from an ofproto, we need to
505      * guarantee that the group remains accessible to users of
506      * xlate_group_actions and the xlate_cache, as the xlate_cache will not be
507      * cleaned up until the corresponding datapath flows are revalidated. */
508     struct ovs_refcount ref_count;
509
510     /* No lock is needed to protect the fields below since they are not
511      * modified after construction. */
512     const struct ofproto *ofproto;  /* The ofproto that contains this group. */
513     const uint32_t group_id;
514     const enum ofp11_group_type type; /* One of OFPGT_*. */
515
516     const long long int created;      /* Creation time. */
517     const long long int modified;     /* Time of last modification. */
518
519     struct ovs_list buckets;        /* Contains "struct ofputil_bucket"s. */
520     const uint32_t n_buckets;
521
522     const struct ofputil_group_props props;
523 };
524
525 bool ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
526                           struct ofgroup **group);
527
528 void ofproto_group_ref(struct ofgroup *);
529 void ofproto_group_unref(struct ofgroup *);
530
531 void ofproto_group_delete_all(struct ofproto *);
532
533 /* ofproto class structure, to be defined by each ofproto implementation.
534  *
535  *
536  * Data Structures
537  * ===============
538  *
539  * These functions work primarily with four different kinds of data
540  * structures:
541  *
542  *   - "struct ofproto", which represents an OpenFlow switch.
543  *
544  *   - "struct ofport", which represents a port within an ofproto.
545  *
546  *   - "struct rule", which represents an OpenFlow flow within an ofproto.
547  *
548  *   - "struct ofgroup", which represents an OpenFlow 1.1+ group within an
549  *     ofproto.
550  *
551  * Each of these data structures contains all of the implementation-independent
552  * generic state for the respective concept, called the "base" state.  None of
553  * them contains any extra space for ofproto implementations to use.  Instead,
554  * each implementation is expected to declare its own data structure that
555  * contains an instance of the generic data structure plus additional
556  * implementation-specific members, called the "derived" state.  The
557  * implementation can use casts or (preferably) the CONTAINER_OF macro to
558  * obtain access to derived state given only a pointer to the embedded generic
559  * data structure.
560  *
561  *
562  * Life Cycle
563  * ==========
564  *
565  * Four stylized functions accompany each of these data structures:
566  *
567  *            "alloc"       "construct"       "destruct"       "dealloc"
568  *            ------------  ----------------  ---------------  --------------
569  *   ofproto  ->alloc       ->construct       ->destruct       ->dealloc
570  *   ofport   ->port_alloc  ->port_construct  ->port_destruct  ->port_dealloc
571  *   rule     ->rule_alloc  ->rule_construct  ->rule_destruct  ->rule_dealloc
572  *   group    ->group_alloc ->group_construct ->group_destruct ->group_dealloc
573  *
574  * "ofproto", "ofport", and "group" have this exact life cycle.  The "rule"
575  * data structure also follow this life cycle with some additional elaborations
576  * described under "Rule Life Cycle" below.
577  *
578  * Any instance of a given data structure goes through the following life
579  * cycle:
580  *
581  *   1. The client calls the "alloc" function to obtain raw memory.  If "alloc"
582  *      fails, skip all the other steps.
583  *
584  *   2. The client initializes all of the data structure's base state.  If this
585  *      fails, skip to step 7.
586  *
587  *   3. The client calls the "construct" function.  The implementation
588  *      initializes derived state.  It may refer to the already-initialized
589  *      base state.  If "construct" fails, skip to step 6.
590  *
591  *   4. The data structure is now initialized and in use.
592  *
593  *   5. When the data structure is no longer needed, the client calls the
594  *      "destruct" function.  The implementation uninitializes derived state.
595  *      The base state has not been uninitialized yet, so the implementation
596  *      may still refer to it.
597  *
598  *   6. The client uninitializes all of the data structure's base state.
599  *
600  *   7. The client calls the "dealloc" to free the raw memory.  The
601  *      implementation must not refer to base or derived state in the data
602  *      structure, because it has already been uninitialized.
603  *
604  * Each "alloc" function allocates and returns a new instance of the respective
605  * data structure.  The "alloc" function is not given any information about the
606  * use of the new data structure, so it cannot perform much initialization.
607  * Its purpose is just to ensure that the new data structure has enough room
608  * for base and derived state.  It may return a null pointer if memory is not
609  * available, in which case none of the other functions is called.
610  *
611  * Each "construct" function initializes derived state in its respective data
612  * structure.  When "construct" is called, all of the base state has already
613  * been initialized, so the "construct" function may refer to it.  The
614  * "construct" function is allowed to fail, in which case the client calls the
615  * "dealloc" function (but not the "destruct" function).
616  *
617  * Each "destruct" function uninitializes and frees derived state in its
618  * respective data structure.  When "destruct" is called, the base state has
619  * not yet been uninitialized, so the "destruct" function may refer to it.  The
620  * "destruct" function is not allowed to fail.
621  *
622  * Each "dealloc" function frees raw memory that was allocated by the
623  * "alloc" function.  The memory's base and derived members might not have ever
624  * been initialized (but if "construct" returned successfully, then it has been
625  * "destruct"ed already).  The "dealloc" function is not allowed to fail.
626  *
627  *
628  * Conventions
629  * ===========
630  *
631  * Most of these functions return 0 if they are successful or a positive error
632  * code on failure.  Depending on the function, valid error codes are either
633  * errno values or OFPERR_* OpenFlow error codes.
634  *
635  * Most of these functions are expected to execute synchronously, that is, to
636  * block as necessary to obtain a result.  Thus, these functions may return
637  * EAGAIN (or EWOULDBLOCK or EINPROGRESS) only where the function descriptions
638  * explicitly say those errors are a possibility.  We may relax this
639  * requirement in the future if and when we encounter performance problems. */
640 struct ofproto_class {
641 /* ## ----------------- ## */
642 /* ## Factory Functions ## */
643 /* ## ----------------- ## */
644
645     /* Initializes provider.  The caller may pass in 'iface_hints',
646      * which contains an shash of "struct iface_hint" elements indexed
647      * by the interface's name.  The provider may use these hints to
648      * describe the startup configuration in order to reinitialize its
649      * state.  The caller owns the provided data, so a provider must
650      * make copies of anything required.  An ofproto provider must
651      * remove any existing state that is not described by the hint, and
652      * may choose to remove it all. */
653     void (*init)(const struct shash *iface_hints);
654
655     /* Enumerates the types of all supported ofproto types into 'types'.  The
656      * caller has already initialized 'types'.  The implementation should add
657      * its own types to 'types' but not remove any existing ones, because other
658      * ofproto classes might already have added names to it. */
659     void (*enumerate_types)(struct sset *types);
660
661     /* Enumerates the names of all existing datapath of the specified 'type'
662      * into 'names' 'all_dps'.  The caller has already initialized 'names' as
663      * an empty sset.
664      *
665      * 'type' is one of the types enumerated by ->enumerate_types().
666      *
667      * Returns 0 if successful, otherwise a positive errno value.
668      */
669     int (*enumerate_names)(const char *type, struct sset *names);
670
671     /* Deletes the datapath with the specified 'type' and 'name'.  The caller
672      * should have closed any open ofproto with this 'type' and 'name'; this
673      * function is allowed to fail if that is not the case.
674      *
675      * 'type' is one of the types enumerated by ->enumerate_types().
676      * 'name' is one of the names enumerated by ->enumerate_names() for 'type'.
677      *
678      * Returns 0 if successful, otherwise a positive errno value.
679      */
680     int (*del)(const char *type, const char *name);
681
682     /* Returns the type to pass to netdev_open() when a datapath of type
683      * 'datapath_type' has a port of type 'port_type', for a few special
684      * cases when a netdev type differs from a port type.  For example,
685      * when using the userspace datapath, a port of type "internal"
686      * needs to be opened as "tap".
687      *
688      * Returns either 'type' itself or a string literal, which must not
689      * be freed. */
690     const char *(*port_open_type)(const char *datapath_type,
691                                   const char *port_type);
692
693 /* ## ------------------------ ## */
694 /* ## Top-Level type Functions ## */
695 /* ## ------------------------ ## */
696
697     /* Performs any periodic activity required on ofprotos of type
698      * 'type'.
699      *
700      * An ofproto provider may implement it or not, depending on whether
701      * it needs type-level maintenance.
702      *
703      * Returns 0 if successful, otherwise a positive errno value. */
704     int (*type_run)(const char *type);
705
706     /* Causes the poll loop to wake up when a type 'type''s 'run'
707      * function needs to be called, e.g. by calling the timer or fd
708      * waiting functions in poll-loop.h.
709      *
710      * An ofproto provider may implement it or not, depending on whether
711      * it needs type-level maintenance. */
712     void (*type_wait)(const char *type);
713
714 /* ## --------------------------- ## */
715 /* ## Top-Level ofproto Functions ## */
716 /* ## --------------------------- ## */
717
718     /* Life-cycle functions for an "ofproto" (see "Life Cycle" above).
719      *
720      *
721      * Construction
722      * ============
723      *
724      * ->construct() should not modify any base members of the ofproto.  The
725      * client will initialize the ofproto's 'ports' and 'tables' members after
726      * construction is complete.
727      *
728      * When ->construct() is called, the client does not yet know how many flow
729      * tables the datapath supports, so ofproto->n_tables will be 0 and
730      * ofproto->tables will be NULL.  ->construct() should call
731      * ofproto_init_tables() to allocate and initialize ofproto->n_tables and
732      * ofproto->tables.  Each flow table will be initially empty, so
733      * ->construct() should delete flows from the underlying datapath, if
734      * necessary, rather than populating the tables.
735      *
736      * If the ofproto knows the maximum port number that the datapath can have,
737      * then it can call ofproto_init_max_ports().  If it does so, then the
738      * client will ensure that the actions it allows to be used through
739      * OpenFlow do not refer to ports above that maximum number.
740      *
741      * Only one ofproto instance needs to be supported for any given datapath.
742      * If a datapath is already open as part of one "ofproto", then another
743      * attempt to "construct" the same datapath as part of another ofproto is
744      * allowed to fail with an error.
745      *
746      * ->construct() returns 0 if successful, otherwise a positive errno
747      * value.
748      *
749      *
750      * Destruction
751      * ===========
752      *
753      * ->destruct() must also destroy all remaining rules in the ofproto's
754      * tables, by passing each remaining rule to ofproto_rule_delete(), then
755      * destroy all remaining groups by calling ofproto_group_delete_all().
756      *
757      * The client will destroy the flow tables themselves after ->destruct()
758      * returns.
759      */
760     struct ofproto *(*alloc)(void);
761     int (*construct)(struct ofproto *ofproto);
762     void (*destruct)(struct ofproto *ofproto);
763     void (*dealloc)(struct ofproto *ofproto);
764
765     /* Performs any periodic activity required by 'ofproto'.  It should:
766      *
767      *   - Call connmgr_send_packet_in() for each received packet that missed
768      *     in the OpenFlow flow table or that had a OFPP_CONTROLLER output
769      *     action.
770      *
771      *   - Call ofproto_rule_expire() for each OpenFlow flow that has reached
772      *     its hard_timeout or idle_timeout, to expire the flow.
773      *
774      * Returns 0 if successful, otherwise a positive errno value. */
775     int (*run)(struct ofproto *ofproto);
776
777     /* Causes the poll loop to wake up when 'ofproto''s 'run' function needs to
778      * be called, e.g. by calling the timer or fd waiting functions in
779      * poll-loop.h.  */
780     void (*wait)(struct ofproto *ofproto);
781
782     /* Adds some memory usage statistics for the implementation of 'ofproto'
783      * into 'usage', for use with memory_report().
784      *
785      * This function is optional. */
786     void (*get_memory_usage)(const struct ofproto *ofproto,
787                              struct simap *usage);
788
789     /* Adds some memory usage statistics for the implementation of 'type'
790      * into 'usage', for use with memory_report().
791      *
792      * This function is optional. */
793     void (*type_get_memory_usage)(const char *type, struct simap *usage);
794
795     /* Every "struct rule" in 'ofproto' is about to be deleted, one by one.
796      * This function may prepare for that, for example by clearing state in
797      * advance.  It should *not* actually delete any "struct rule"s from
798      * 'ofproto', only prepare for it.
799      *
800      * This function is optional; it's really just for optimization in case
801      * it's cheaper to delete all the flows from your hardware in a single pass
802      * than to do it one by one. */
803     void (*flush)(struct ofproto *ofproto);
804
805     /* Helper for the OpenFlow OFPT_TABLE_FEATURES request.
806      *
807      * The 'features' array contains 'ofproto->n_tables' elements.  Each
808      * element is initialized as:
809      *
810      *   - 'table_id' to the array index.
811      *
812      *   - 'name' to "table#" where # is the table ID.
813      *
814      *   - 'metadata_match' and 'metadata_write' to OVS_BE64_MAX.
815      *
816      *   - 'config' to the table miss configuration.
817      *
818      *   - 'max_entries' to 1,000,000.
819      *
820      *   - Both 'nonmiss' and 'miss' to:
821      *
822      *     * 'next' to all 1-bits for all later tables.
823      *
824      *     * 'instructions' to all instructions.
825      *
826      *     * 'write' and 'apply' both to:
827      *
828      *       - 'ofpacts': All actions.
829      *
830      *       - 'set_fields': All fields.
831      *
832      *   - 'match', 'mask', and 'wildcard' to all fields.
833      *
834      * If 'stats' is nonnull, it also contains 'ofproto->n_tables' elements.
835      * Each element is initialized as:
836      *
837      *   - 'table_id' to the array index.
838      *
839      *   - 'active_count' to the 'n_flows' of struct ofproto for the table.
840      *
841      *   - 'lookup_count' and 'matched_count' to 0.
842      *
843      * The implementation should update any members in each element for which
844      * it has better values:
845      *
846      *   - Any member of 'features' to better describe the implementation's
847      *     capabilities.
848      *
849      *   - 'lookup_count' to the number of packets looked up in this flow table
850      *     so far.
851      *
852      *   - 'matched_count' to the number of packets looked up in this flow
853      *     table so far that matched one of the flow entries.
854      */
855     void (*query_tables)(struct ofproto *ofproto,
856                          struct ofputil_table_features *features,
857                          struct ofputil_table_stats *stats);
858
859     /* Sets the current tables version the provider should use for classifier
860      * lookups. */
861     void (*set_tables_version)(struct ofproto *ofproto, cls_version_t version);
862 /* ## ---------------- ## */
863 /* ## ofport Functions ## */
864 /* ## ---------------- ## */
865
866     /* Life-cycle functions for a "struct ofport" (see "Life Cycle" above).
867      *
868      * ->port_construct() should not modify any base members of the ofport.
869      * An ofproto implementation should use the 'ofp_port' member of
870      * "struct ofport" as the OpenFlow port number.
871      *
872      * ofports are managed by the base ofproto code.  The ofproto
873      * implementation should only create and destroy them in response to calls
874      * to these functions.  The base ofproto code will create and destroy
875      * ofports in the following situations:
876      *
877      *   - Just after the ->construct() function is called, the base ofproto
878      *     iterates over all of the implementation's ports, using
879      *     ->port_dump_start() and related functions, and constructs an ofport
880      *     for each dumped port.
881      *
882      *   - If ->port_poll() reports that a specific port has changed, then the
883      *     base ofproto will query that port with ->port_query_by_name() and
884      *     construct or destruct ofports as necessary to reflect the updated
885      *     set of ports.
886      *
887      *   - If ->port_poll() returns ENOBUFS to report an unspecified port set
888      *     change, then the base ofproto will iterate over all of the
889      *     implementation's ports, in the same way as at ofproto
890      *     initialization, and construct and destruct ofports to reflect all of
891      *     the changes.
892      *
893      * ->port_construct() returns 0 if successful, otherwise a positive errno
894      * value.
895      */
896     struct ofport *(*port_alloc)(void);
897     int (*port_construct)(struct ofport *ofport);
898     void (*port_destruct)(struct ofport *ofport);
899     void (*port_dealloc)(struct ofport *ofport);
900
901     /* Called after 'ofport->netdev' is replaced by a new netdev object.  If
902      * the ofproto implementation uses the ofport's netdev internally, then it
903      * should switch to using the new one.  The old one has been closed.
904      *
905      * An ofproto implementation that doesn't need to do anything in this
906      * function may use a null pointer. */
907     void (*port_modified)(struct ofport *ofport);
908
909     /* Called after an OpenFlow request changes a port's configuration.
910      * 'ofport->pp.config' contains the new configuration.  'old_config'
911      * contains the previous configuration.
912      *
913      * The caller implements OFPUTIL_PC_PORT_DOWN using netdev functions to
914      * turn NETDEV_UP on and off, so this function doesn't have to do anything
915      * for that bit (and it won't be called if that is the only bit that
916      * changes). */
917     void (*port_reconfigured)(struct ofport *ofport,
918                               enum ofputil_port_config old_config);
919
920     /* Looks up a port named 'devname' in 'ofproto'.  On success, returns 0 and
921      * initializes '*port' appropriately. Otherwise, returns a positive errno
922      * value.
923      *
924      * The caller owns the data in 'port' and must free it with
925      * ofproto_port_destroy() when it is no longer needed. */
926     int (*port_query_by_name)(const struct ofproto *ofproto,
927                               const char *devname, struct ofproto_port *port);
928
929     /* Attempts to add 'netdev' as a port on 'ofproto'.  Returns 0 if
930      * successful, otherwise a positive errno value.  The caller should
931      * inform the implementation of the OpenFlow port through the
932      * ->port_construct() method.
933      *
934      * It doesn't matter whether the new port will be returned by a later call
935      * to ->port_poll(); the implementation may do whatever is more
936      * convenient. */
937     int (*port_add)(struct ofproto *ofproto, struct netdev *netdev);
938
939     /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.  Returns
940      * 0 if successful, otherwise a positive errno value.
941      *
942      * It doesn't matter whether the new port will be returned by a later call
943      * to ->port_poll(); the implementation may do whatever is more
944      * convenient. */
945     int (*port_del)(struct ofproto *ofproto, ofp_port_t ofp_port);
946
947     /* Get port stats */
948     int (*port_get_stats)(const struct ofport *port,
949                           struct netdev_stats *stats);
950
951     /* Port iteration functions.
952      *
953      * The client might not be entirely in control of the ports within an
954      * ofproto.  Some hardware implementations, for example, might have a fixed
955      * set of ports in a datapath.  For this reason, the client needs a way to
956      * iterate through all the ports that are actually in a datapath.  These
957      * functions provide that functionality.
958      *
959      * The 'state' pointer provides the implementation a place to
960      * keep track of its position.  Its format is opaque to the caller.
961      *
962      * The ofproto provider retains ownership of the data that it stores into
963      * ->port_dump_next()'s 'port' argument.  The data must remain valid until
964      * at least the next call to ->port_dump_next() or ->port_dump_done() for
965      * 'state'.  The caller will not modify or free it.
966      *
967      * Details
968      * =======
969      *
970      * ->port_dump_start() attempts to begin dumping the ports in 'ofproto'.
971      * On success, it should return 0 and initialize '*statep' with any data
972      * needed for iteration.  On failure, returns a positive errno value, and
973      * the client will not call ->port_dump_next() or ->port_dump_done().
974      *
975      * ->port_dump_next() attempts to retrieve another port from 'ofproto' for
976      * 'state'.  If there is another port, it should store the port's
977      * information into 'port' and return 0.  It should return EOF if all ports
978      * have already been iterated.  Otherwise, on error, it should return a
979      * positive errno value.  This function will not be called again once it
980      * returns nonzero once for a given iteration (but the 'port_dump_done'
981      * function will be called afterward).
982      *
983      * ->port_dump_done() allows the implementation to release resources used
984      * for iteration.  The caller might decide to stop iteration in the middle
985      * by calling this function before ->port_dump_next() returns nonzero.
986      *
987      * Usage Example
988      * =============
989      *
990      * int error;
991      * void *state;
992      *
993      * error = ofproto->ofproto_class->port_dump_start(ofproto, &state);
994      * if (!error) {
995      *     for (;;) {
996      *         struct ofproto_port port;
997      *
998      *         error = ofproto->ofproto_class->port_dump_next(
999      *                     ofproto, state, &port);
1000      *         if (error) {
1001      *             break;
1002      *         }
1003      *         // Do something with 'port' here (without modifying or freeing
1004      *         // any of its data).
1005      *     }
1006      *     ofproto->ofproto_class->port_dump_done(ofproto, state);
1007      * }
1008      * // 'error' is now EOF (success) or a positive errno value (failure).
1009      */
1010     int (*port_dump_start)(const struct ofproto *ofproto, void **statep);
1011     int (*port_dump_next)(const struct ofproto *ofproto, void *state,
1012                           struct ofproto_port *port);
1013     int (*port_dump_done)(const struct ofproto *ofproto, void *state);
1014
1015     /* Polls for changes in the set of ports in 'ofproto'.  If the set of ports
1016      * in 'ofproto' has changed, then this function should do one of the
1017      * following:
1018      *
1019      * - Preferably: store the name of the device that was added to or deleted
1020      *   from 'ofproto' in '*devnamep' and return 0.  The caller is responsible
1021      *   for freeing '*devnamep' (with free()) when it no longer needs it.
1022      *
1023      * - Alternatively: return ENOBUFS, without indicating the device that was
1024      *   added or deleted.
1025      *
1026      * Occasional 'false positives', in which the function returns 0 while
1027      * indicating a device that was not actually added or deleted or returns
1028      * ENOBUFS without any change, are acceptable.
1029      *
1030      * The purpose of 'port_poll' is to let 'ofproto' know about changes made
1031      * externally to the 'ofproto' object, e.g. by a system administrator via
1032      * ovs-dpctl.  Therefore, it's OK, and even preferable, for port_poll() to
1033      * not report changes made through calls to 'port_add' or 'port_del' on the
1034      * same 'ofproto' object.  (But it's OK for it to report them too, just
1035      * slightly less efficient.)
1036      *
1037      * If the set of ports in 'ofproto' has not changed, returns EAGAIN.  May
1038      * also return other positive errno values to indicate that something has
1039      * gone wrong.
1040      *
1041      * If the set of ports in a datapath is fixed, or if the only way that the
1042      * set of ports in a datapath can change is through ->port_add() and
1043      * ->port_del(), then this function may be a null pointer.
1044      */
1045     int (*port_poll)(const struct ofproto *ofproto, char **devnamep);
1046
1047     /* Arranges for the poll loop to wake up when ->port_poll() will return a
1048      * value other than EAGAIN.
1049      *
1050      * If the set of ports in a datapath is fixed, or if the only way that the
1051      * set of ports in a datapath can change is through ->port_add() and
1052      * ->port_del(), or if the poll loop will always wake up anyway when
1053      * ->port_poll() will return a value other than EAGAIN, then this function
1054      * may be a null pointer.
1055      */
1056     void (*port_poll_wait)(const struct ofproto *ofproto);
1057
1058     /* Checks the status of LACP negotiation for 'port'.  Returns 1 if LACP
1059      * partner information for 'port' is up-to-date, 0 if LACP partner
1060      * information is not current (generally indicating a connectivity
1061      * problem), or -1 if LACP is not enabled on 'port'.
1062      *
1063      * This function may be a null pointer if the ofproto implementation does
1064      * not support LACP.
1065      */
1066     int (*port_is_lacp_current)(const struct ofport *port);
1067
1068     /* Get LACP port stats. Returns -1 if LACP is not enabled on 'port'.
1069      *
1070      * This function may be a null pointer if the ofproto implementation does
1071      * not support LACP.
1072      */
1073     int (*port_get_lacp_stats)(const struct ofport *port,
1074                                struct lacp_slave_stats *stats);
1075
1076 /* ## ----------------------- ## */
1077 /* ## OpenFlow Rule Functions ## */
1078 /* ## ----------------------- ## */
1079
1080     /* Chooses an appropriate table for 'match' within 'ofproto'.  On
1081      * success, stores the table ID into '*table_idp' and returns 0.  On
1082      * failure, returns an OpenFlow error code.
1083      *
1084      * The choice of table should be a function of 'match' and 'ofproto''s
1085      * datapath capabilities.  It should not depend on the flows already in
1086      * 'ofproto''s flow tables.  Failure implies that an OpenFlow rule with
1087      * 'match' as its matching condition can never be inserted into 'ofproto',
1088      * even starting from an empty flow table.
1089      *
1090      * If multiple tables are candidates for inserting the flow, the function
1091      * should choose one arbitrarily (but deterministically).
1092      *
1093      * If this function is NULL then table 0 is always chosen. */
1094     enum ofperr (*rule_choose_table)(const struct ofproto *ofproto,
1095                                      const struct match *match,
1096                                      uint8_t *table_idp);
1097
1098     /* Life-cycle functions for a "struct rule".
1099      *
1100      *
1101      * Rule Life Cycle
1102      * ===============
1103      *
1104      * The life cycle of a struct rule is an elaboration of the basic life
1105      * cycle described above under "Life Cycle".
1106      *
1107      * After a rule is successfully constructed, it is then inserted.  If
1108      * insertion is successful, then before it is later destructed, it is
1109      * deleted.
1110      *
1111      * You can think of a rule as having the following extra steps inserted
1112      * between "Life Cycle" steps 4 and 5:
1113      *
1114      *   4.1. The client inserts the rule into the flow table, making it
1115      *        visible in flow table lookups.
1116      *
1117      *   4.2. The client calls "rule_insert" to insert the flow.  The
1118      *        implementation attempts to install the flow in the underlying
1119      *        hardware and returns an error code indicate success or failure.
1120      *        On failure, go to step 5.
1121      *
1122      *   4.3. The rule is now installed in the flow table.  Eventually it will
1123      *        be deleted.
1124      *
1125      *   4.4. The client removes the rule from the flow table.  It is no longer
1126      *        visible in flow table lookups.
1127      *
1128      *   4.5. The client calls "rule_delete".  The implementation uninstalls
1129      *        the flow from the underlying hardware.  Deletion is not allowed
1130      *        to fail.
1131      *
1132      *
1133      * Construction
1134      * ============
1135      *
1136      * When ->rule_construct() is called, 'rule' is a new rule that is not yet
1137      * inserted into a flow table.  ->rule_construct() should initialize enough
1138      * of the rule's derived state for 'rule' to be suitable for inserting into
1139      * a flow table.  ->rule_construct() should not modify any base members of
1140      * struct rule.
1141      *
1142      * If ->rule_construct() fails (as indicated by returning a nonzero
1143      * OpenFlow error code), the ofproto base code will uninitialize and
1144      * deallocate 'rule'.  See "Rule Life Cycle" above for more details.
1145      *
1146      * ->rule_construct() must also:
1147      *
1148      *   - Validate that the datapath supports the matching rule in 'rule->cr'
1149      *     datapath.  For example, if the rule's table does not support
1150      *     registers, then it is an error if 'rule->cr' does not wildcard all
1151      *     registers.
1152      *
1153      *   - Validate that the datapath can correctly implement 'rule->ofpacts'.
1154      *
1155      * After a successful construction the rest of the rule life cycle calls
1156      * may not fail, so ->rule_construct() must also make sure that the rule
1157      * can be inserted in to the datapath.
1158      *
1159      *
1160      * Insertion
1161      * =========
1162      *
1163      * Following successful construction, the ofproto base case inserts 'rule'
1164      * into its flow table, then it calls ->rule_insert().  ->rule_insert()
1165      * must add the new rule to the datapath flow table and return only after
1166      * this is complete.  The 'new_rule' may be a duplicate of an 'old_rule'.
1167      * In this case the 'old_rule' is non-null, and the implementation should
1168      * forward rule statistics from the 'old_rule' to the 'new_rule' if
1169      * 'forward_stats' is 'true'.  This may not fail.
1170      *
1171      *
1172      * Deletion
1173      * ========
1174      *
1175      * The ofproto base code removes 'rule' from its flow table before it calls
1176      * ->rule_delete().  ->rule_delete() must remove 'rule' from the datapath
1177      * flow table and return only after this has completed successfully.
1178      *
1179      * Rule deletion must not fail.
1180      *
1181      *
1182      * Destruction
1183      * ===========
1184      *
1185      * ->rule_destruct() must uninitialize derived state.
1186      *
1187      * Rule destruction must not fail. */
1188     struct rule *(*rule_alloc)(void);
1189     enum ofperr (*rule_construct)(struct rule *rule)
1190         /* OVS_REQUIRES(ofproto_mutex) */;
1191     void (*rule_insert)(struct rule *rule, struct rule *old_rule,
1192                         bool forward_stats)
1193         /* OVS_REQUIRES(ofproto_mutex) */;
1194     void (*rule_delete)(struct rule *rule) /* OVS_REQUIRES(ofproto_mutex) */;
1195     void (*rule_destruct)(struct rule *rule);
1196     void (*rule_dealloc)(struct rule *rule);
1197
1198     /* Obtains statistics for 'rule', storing the number of packets that have
1199      * matched it in '*packet_count' and the number of bytes in those packets
1200      * in '*byte_count'.  UINT64_MAX indicates that the packet count or byte
1201      * count is unknown. */
1202     void (*rule_get_stats)(struct rule *rule, uint64_t *packet_count,
1203                            uint64_t *byte_count, long long int *used)
1204         /* OVS_EXCLUDED(ofproto_mutex) */;
1205
1206     /* Applies the actions in 'rule' to 'packet'.  (This implements sending
1207      * buffered packets for OpenFlow OFPT_FLOW_MOD commands.)
1208      *
1209      * Takes ownership of 'packet' (so it should eventually free it, with
1210      * ofpbuf_delete()).
1211      *
1212      * 'flow' reflects the flow information for 'packet'.  All of the
1213      * information in 'flow' is extracted from 'packet', except for
1214      * flow->tunnel and flow->in_port, which are assigned the correct values
1215      * for the incoming packet.  The register values are zeroed.  'packet''s
1216      * header pointers and offsets (e.g. packet->l3) are appropriately
1217      * initialized.  packet->l3 is aligned on a 32-bit boundary.
1218      *
1219      * The implementation should add the statistics for 'packet' into 'rule'.
1220      *
1221      * Returns 0 if successful, otherwise an OpenFlow error code. */
1222     enum ofperr (*rule_execute)(struct rule *rule, const struct flow *flow,
1223                                 struct dp_packet *packet);
1224
1225     /* Changes the OpenFlow IP fragment handling policy to 'frag_handling',
1226      * which takes one of the following values, with the corresponding
1227      * meanings:
1228      *
1229      *  - OFPC_FRAG_NORMAL: The switch should treat IP fragments the same way
1230      *    as other packets, omitting TCP and UDP port numbers (always setting
1231      *    them to 0).
1232      *
1233      *  - OFPC_FRAG_DROP: The switch should drop all IP fragments without
1234      *    passing them through the flow table.
1235      *
1236      *  - OFPC_FRAG_REASM: The switch should reassemble IP fragments before
1237      *    passing packets through the flow table.
1238      *
1239      *  - OFPC_FRAG_NX_MATCH (a Nicira extension): Similar to OFPC_FRAG_NORMAL,
1240      *    except that TCP and UDP port numbers should be included in fragments
1241      *    with offset 0.
1242      *
1243      * Implementations are not required to support every mode.
1244      * OFPC_FRAG_NORMAL is the default mode when an ofproto is created.
1245      *
1246      * At the time of the call to ->set_frag_handling(), the current mode is
1247      * available in 'ofproto->frag_handling'.  ->set_frag_handling() returns
1248      * true if the requested mode was set, false if it is not supported.
1249      *
1250      * Upon successful return, the caller changes 'ofproto->frag_handling' to
1251      * reflect the new mode.
1252      */
1253     bool (*set_frag_handling)(struct ofproto *ofproto,
1254                               enum ofp_config_flags frag_handling);
1255
1256     /* Implements the OpenFlow OFPT_PACKET_OUT command.  The datapath should
1257      * execute the 'ofpacts_len' bytes of "struct ofpacts" in 'ofpacts'.
1258      *
1259      * The caller retains ownership of 'packet' and of 'ofpacts', so
1260      * ->packet_out() should not modify or free them.
1261      *
1262      * This function must validate that it can correctly implement 'ofpacts'.
1263      * If not, then it should return an OpenFlow error code.
1264      *
1265      * 'flow' reflects the flow information for 'packet'.  All of the
1266      * information in 'flow' is extracted from 'packet', except for
1267      * flow->in_port (see below).  flow->tunnel and its register values are
1268      * zeroed.
1269      *
1270      * flow->in_port comes from the OpenFlow OFPT_PACKET_OUT message.  The
1271      * implementation should reject invalid flow->in_port values by returning
1272      * OFPERR_OFPBRC_BAD_PORT.  (If the implementation called
1273      * ofproto_init_max_ports(), then the client will reject these ports
1274      * itself.)  For consistency, the implementation should consider valid for
1275      * flow->in_port any value that could possibly be seen in a packet that it
1276      * passes to connmgr_send_packet_in().  Ideally, even an implementation
1277      * that never generates packet-ins (e.g. due to hardware limitations)
1278      * should still allow flow->in_port values for every possible physical port
1279      * and OFPP_LOCAL.  The only virtual ports (those above OFPP_MAX) that the
1280      * caller will ever pass in as flow->in_port, other than OFPP_LOCAL, are
1281      * OFPP_NONE and OFPP_CONTROLLER.  The implementation should allow both of
1282      * these, treating each of them as packets generated by the controller as
1283      * opposed to packets originating from some switch port.
1284      *
1285      * (Ordinarily the only effect of flow->in_port is on output actions that
1286      * involve the input port, such as actions that output to OFPP_IN_PORT,
1287      * OFPP_FLOOD, or OFPP_ALL.  flow->in_port can also affect Nicira extension
1288      * "resubmit" actions.)
1289      *
1290      * 'packet' is not matched against the OpenFlow flow table, so its
1291      * statistics should not be included in OpenFlow flow statistics.
1292      *
1293      * Returns 0 if successful, otherwise an OpenFlow error code. */
1294     enum ofperr (*packet_out)(struct ofproto *ofproto, struct dp_packet *packet,
1295                               const struct flow *flow,
1296                               const struct ofpact *ofpacts,
1297                               size_t ofpacts_len);
1298
1299 /* ## ------------------------- ## */
1300 /* ## OFPP_NORMAL configuration ## */
1301 /* ## ------------------------- ## */
1302
1303     /* Configures NetFlow on 'ofproto' according to the options in
1304      * 'netflow_options', or turns off NetFlow if 'netflow_options' is NULL.
1305      *
1306      * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
1307      * NetFlow, as does a null pointer. */
1308     int (*set_netflow)(struct ofproto *ofproto,
1309                        const struct netflow_options *netflow_options);
1310
1311     void (*get_netflow_ids)(const struct ofproto *ofproto,
1312                             uint8_t *engine_type, uint8_t *engine_id);
1313
1314     /* Configures sFlow on 'ofproto' according to the options in
1315      * 'sflow_options', or turns off sFlow if 'sflow_options' is NULL.
1316      *
1317      * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
1318      * sFlow, as does a null pointer. */
1319     int (*set_sflow)(struct ofproto *ofproto,
1320                      const struct ofproto_sflow_options *sflow_options);
1321
1322     /* Configures IPFIX on 'ofproto' according to the options in
1323      * 'bridge_exporter_options' and the 'flow_exporters_options'
1324      * array, or turns off IPFIX if 'bridge_exporter_options' and
1325      * 'flow_exporters_options' is NULL.
1326      *
1327      * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
1328      * IPFIX, as does a null pointer. */
1329     int (*set_ipfix)(
1330         struct ofproto *ofproto,
1331         const struct ofproto_ipfix_bridge_exporter_options
1332             *bridge_exporter_options,
1333         const struct ofproto_ipfix_flow_exporter_options
1334             *flow_exporters_options, size_t n_flow_exporters_options);
1335
1336     /* Configures connectivity fault management on 'ofport'.
1337      *
1338      * If 'cfm_settings' is nonnull, configures CFM according to its members.
1339      *
1340      * If 'cfm_settings' is null, removes any connectivity fault management
1341      * configuration from 'ofport'.
1342      *
1343      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1344      * support CFM, as does a null pointer. */
1345     int (*set_cfm)(struct ofport *ofport, const struct cfm_settings *s);
1346
1347     /* Checks the status change of CFM on 'ofport'.  Returns true if
1348      * there is status change since last call or if CFM is not specified. */
1349     bool (*cfm_status_changed)(struct ofport *ofport);
1350
1351     /* Populates 'smap' with the status of CFM on 'ofport'.  Returns 0 on
1352      * success, or a positive errno.  EOPNOTSUPP as a return value indicates
1353      * that this ofproto_class does not support CFM, as does a null pointer.
1354      *
1355      * The caller must provide and own '*status', and it must free the array
1356      * returned in 'status->rmps'.  '*status' is indeterminate if the return
1357      * value is non-zero. */
1358     int (*get_cfm_status)(const struct ofport *ofport,
1359                           struct cfm_status *status);
1360
1361     /* Configures LLDP on 'ofport'.
1362      *
1363      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1364      * support LLDP, as does a null pointer. */
1365     int (*set_lldp)(struct ofport *ofport, const struct smap *cfg);
1366
1367     /* Checks the status of LLDP configured on 'ofport'.  Returns true if the
1368      * port's LLDP status was successfully stored into '*status'.  Returns
1369      * false if the port did not have LLDP configured, in which case '*status'
1370      * is indeterminate.
1371      *
1372      * The caller must provide and own '*status'.  '*status' is indeterminate
1373      * if the return value is non-zero. */
1374     bool (*get_lldp_status)(const struct ofport *ofport,
1375                             struct lldp_status *status);
1376
1377     /* Configures Auto Attach.
1378      *
1379      * If 's' is nonnull, configures Auto Attach according to its members.
1380      *
1381      * If 's' is null, removes any Auto Attach configuration.
1382      */
1383     int (*set_aa)(struct ofproto *ofproto,
1384                   const struct aa_settings *s);
1385
1386     /* If 's' is nonnull, this function registers a mapping associated with
1387      * client data pointer 'aux' in 'ofproto'.  If 'aux' is already registered
1388      * then this function updates its configuration to 's'.  Otherwise, this
1389      * function registers a new mapping.
1390      *
1391      * An implementation that does not support mapping at all may set
1392      * it to NULL or return EOPNOTSUPP.  An implementation that supports
1393      * only a subset of the functionality should implement what it can
1394      * and return 0.
1395      */
1396     int (*aa_mapping_set)(struct ofproto *ofproto, void *aux,
1397                           const struct aa_mapping_settings *s);
1398
1399     /* If 's' is nonnull, this function unregisters a mapping associated with
1400      * client data pointer 'aux' in 'ofproto'.  If 'aux' is already registered
1401      * then this function updates its configuration to 's'.  Otherwise, this
1402      * function unregisters a new mapping.
1403      *
1404      * An implementation that does not support mapping at all may set
1405      * it to NULL or return EOPNOTSUPP.  An implementation that supports
1406      * only a subset of the functionality should implement what it can
1407      * and return 0.
1408      */
1409     int (*aa_mapping_unset)(struct ofproto *ofproto, void *aux);
1410
1411     /*
1412      * Returns the a list of AutoAttach VLAN operations.  When Auto Attach is
1413      * enabled, the VLAN associated with an I-SID/VLAN mapping is first
1414      * negotiated with an Auto Attach Server.  Once an I-SID VLAN mapping
1415      * becomes active, the corresponding VLAN needs to be communicated to the
1416      * bridge in order to add the VLAN to the trunk port linking the Auto
1417      * Attach Client (in this case openvswitch) and the Auto Attach Server.
1418      *
1419      * The list entries are of type "struct bridge_aa_vlan".  Each entry
1420      * specifies the operation (add or remove), the interface on which to
1421      * execute the operation and the VLAN.
1422      */
1423     int (*aa_vlan_get_queued)(struct ofproto *ofproto, struct ovs_list *list);
1424
1425     /*
1426      * Returns the current number of entries in the list of VLAN operations
1427      * in the Auto Attach Client (see previous function description
1428      * aa_vlan_get_queued).  Returns 0 if Auto Attach is disabled.
1429      */
1430     unsigned int (*aa_vlan_get_queue_size)(struct ofproto *ofproto);
1431
1432     /* Configures BFD on 'ofport'.
1433      *
1434      * If 'cfg' is NULL, or 'cfg' does not contain the key value pair
1435      * "enable=true", removes BFD from 'ofport'.  Otherwise, configures BFD
1436      * according to 'cfg'.
1437      *
1438      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1439      * support BFD, as does a null pointer. */
1440     int (*set_bfd)(struct ofport *ofport, const struct smap *cfg);
1441
1442     /* Checks the status change of BFD on 'ofport'.  Returns true if there
1443      * is status change since last call or if BFD is not specified. */
1444     bool (*bfd_status_changed)(struct ofport *ofport);
1445
1446     /* Populates 'smap' with the status of BFD on 'ofport'.  Returns 0 on
1447      * success, or a positive errno.  EOPNOTSUPP as a return value indicates
1448      * that this ofproto_class does not support BFD, as does a null pointer. */
1449     int (*get_bfd_status)(struct ofport *ofport, struct smap *smap);
1450
1451     /* Configures spanning tree protocol (STP) on 'ofproto' using the
1452      * settings defined in 's'.
1453      *
1454      * If 's' is nonnull, configures STP according to its members.
1455      *
1456      * If 's' is null, removes any STP configuration from 'ofproto'.
1457      *
1458      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1459      * support STP, as does a null pointer. */
1460     int (*set_stp)(struct ofproto *ofproto,
1461                    const struct ofproto_stp_settings *s);
1462
1463     /* Retrieves state of spanning tree protocol (STP) on 'ofproto'.
1464      *
1465      * Stores STP state for 'ofproto' in 's'.  If the 'enabled' member
1466      * is false, the other member values are not meaningful.
1467      *
1468      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1469      * support STP, as does a null pointer. */
1470     int (*get_stp_status)(struct ofproto *ofproto,
1471                           struct ofproto_stp_status *s);
1472
1473     /* Configures spanning tree protocol (STP) on 'ofport' using the
1474      * settings defined in 's'.
1475      *
1476      * If 's' is nonnull, configures STP according to its members.  The
1477      * caller is responsible for assigning STP port numbers (using the
1478      * 'port_num' member in the range of 1 through 255, inclusive) and
1479      * ensuring there are no duplicates.
1480      *
1481      * If 's' is null, removes any STP configuration from 'ofport'.
1482      *
1483      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1484      * support STP, as does a null pointer. */
1485     int (*set_stp_port)(struct ofport *ofport,
1486                         const struct ofproto_port_stp_settings *s);
1487
1488     /* Retrieves spanning tree protocol (STP) port status of 'ofport'.
1489      *
1490      * Stores STP state for 'ofport' in 's'.  If the 'enabled' member is
1491      * false, the other member values are not meaningful.
1492      *
1493      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1494      * support STP, as does a null pointer. */
1495     int (*get_stp_port_status)(struct ofport *ofport,
1496                                struct ofproto_port_stp_status *s);
1497
1498     /* Retrieves spanning tree protocol (STP) port statistics of 'ofport'.
1499      *
1500      * Stores STP state for 'ofport' in 's'.  If the 'enabled' member is
1501      * false, the other member values are not meaningful.
1502      *
1503      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1504      * support STP, as does a null pointer. */
1505     int (*get_stp_port_stats)(struct ofport *ofport,
1506                               struct ofproto_port_stp_stats *s);
1507
1508     /* Configures Rapid Spanning Tree Protocol (RSTP) on 'ofproto' using the
1509      * settings defined in 's'.
1510      *
1511      * If 's' is nonnull, configures RSTP according to its members.
1512      *
1513      * If 's' is null, removes any RSTP configuration from 'ofproto'.
1514      *
1515      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1516      * support RSTP, as does a null pointer. */
1517     void (*set_rstp)(struct ofproto *ofproto,
1518                     const struct ofproto_rstp_settings *s);
1519
1520     /* Retrieves state of Rapid Spanning Tree Protocol (RSTP) on 'ofproto'.
1521      *
1522      * Stores RSTP state for 'ofproto' in 's'.  If the 'enabled' member
1523      * is false, the other member values are not meaningful.
1524      *
1525      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1526      * support RSTP, as does a null pointer. */
1527     void (*get_rstp_status)(struct ofproto *ofproto,
1528                            struct ofproto_rstp_status *s);
1529
1530     /* Configures Rapid Spanning Tree Protocol (RSTP) on 'ofport' using the
1531      * settings defined in 's'.
1532      *
1533      * If 's' is nonnull, configures RSTP according to its members.  The
1534      * caller is responsible for assigning RSTP port numbers (using the
1535      * 'port_num' member in the range of 1 through 255, inclusive) and
1536      * ensuring there are no duplicates.
1537      *
1538      * If 's' is null, removes any RSTP configuration from 'ofport'.
1539      *
1540      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1541      * support STP, as does a null pointer. */
1542     void (*set_rstp_port)(struct ofport *ofport,
1543                          const struct ofproto_port_rstp_settings *s);
1544
1545     /* Retrieves Rapid Spanning Tree Protocol (RSTP) port status of 'ofport'.
1546      *
1547      * Stores RSTP state for 'ofport' in 's'.  If the 'enabled' member is
1548      * false, the other member values are not meaningful.
1549      *
1550      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1551      * support RSTP, as does a null pointer. */
1552     void (*get_rstp_port_status)(struct ofport *ofport,
1553                                 struct ofproto_port_rstp_status *s);
1554
1555     /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
1556      * 'queues' attached to 'ofport'.  This data is not intended to be
1557      * sufficient to implement QoS.  Instead, providers may use this
1558      * information to implement features which require knowledge of what queues
1559      * exist on a port, and some basic information about them.
1560      *
1561      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1562      * support QoS, as does a null pointer. */
1563     int (*set_queues)(struct ofport *ofport,
1564                       const struct ofproto_port_queue *queues, size_t n_qdscp);
1565
1566     /* If 's' is nonnull, this function registers a "bundle" associated with
1567      * client data pointer 'aux' in 'ofproto'.  A bundle is the same concept as
1568      * a Port in OVSDB, that is, it consists of one or more "slave" devices
1569      * (Interfaces, in OVSDB) along with VLAN and LACP configuration and, if
1570      * there is more than one slave, a bonding configuration.  If 'aux' is
1571      * already registered then this function updates its configuration to 's'.
1572      * Otherwise, this function registers a new bundle.
1573      *
1574      * If 's' is NULL, this function unregisters the bundle registered on
1575      * 'ofproto' associated with client data pointer 'aux'.  If no such bundle
1576      * has been registered, this has no effect.
1577      *
1578      * This function affects only the behavior of the NXAST_AUTOPATH action and
1579      * output to the OFPP_NORMAL port.  An implementation that does not support
1580      * it at all may set it to NULL or return EOPNOTSUPP.  An implementation
1581      * that supports only a subset of the functionality should implement what
1582      * it can and return 0. */
1583     int (*bundle_set)(struct ofproto *ofproto, void *aux,
1584                       const struct ofproto_bundle_settings *s);
1585
1586     /* If 'port' is part of any bundle, removes it from that bundle.  If the
1587      * bundle now has no ports, deletes the bundle.  If the bundle now has only
1588      * one port, deconfigures the bundle's bonding configuration. */
1589     void (*bundle_remove)(struct ofport *ofport);
1590
1591     /* If 's' is nonnull, this function registers a mirror associated with
1592      * client data pointer 'aux' in 'ofproto'.  A mirror is the same concept as
1593      * a Mirror in OVSDB.  If 'aux' is already registered then this function
1594      * updates its configuration to 's'.  Otherwise, this function registers a
1595      * new mirror.
1596      *
1597      * If 's' is NULL, this function unregisters the mirror registered on
1598      * 'ofproto' associated with client data pointer 'aux'.  If no such mirror
1599      * has been registered, this has no effect.
1600      *
1601      * An implementation that does not support mirroring at all may set
1602      * it to NULL or return EOPNOTSUPP.  An implementation that supports
1603      * only a subset of the functionality should implement what it can
1604      * and return 0. */
1605     int (*mirror_set)(struct ofproto *ofproto, void *aux,
1606                       const struct ofproto_mirror_settings *s);
1607
1608     /* Retrieves statistics from mirror associated with client data
1609      * pointer 'aux' in 'ofproto'.  Stores packet and byte counts in
1610      * 'packets' and 'bytes', respectively.  If a particular counter is
1611      * not supported, the appropriate argument is set to UINT64_MAX.
1612      *
1613      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1614      * support retrieving mirror statistics. */
1615     int (*mirror_get_stats)(struct ofproto *ofproto, void *aux,
1616                             uint64_t *packets, uint64_t *bytes);
1617
1618     /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs
1619      * on which all packets are flooded, instead of using MAC learning.  If
1620      * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1621      *
1622      * This function affects only the behavior of the OFPP_NORMAL action.  An
1623      * implementation that does not support it may set it to NULL or return
1624      * EOPNOTSUPP. */
1625     int (*set_flood_vlans)(struct ofproto *ofproto,
1626                            unsigned long *flood_vlans);
1627
1628     /* Returns true if 'aux' is a registered bundle that is currently in use as
1629      * the output for a mirror. */
1630     bool (*is_mirror_output_bundle)(const struct ofproto *ofproto, void *aux);
1631
1632     /* When the configuration option of forward_bpdu changes, this function
1633      * will be invoked. */
1634     void (*forward_bpdu_changed)(struct ofproto *ofproto);
1635
1636     /* Sets the MAC aging timeout for the OFPP_NORMAL action to 'idle_time', in
1637      * seconds, and the maximum number of MAC table entries to
1638      * 'max_entries'.
1639      *
1640      * An implementation that doesn't support configuring these features may
1641      * set this function to NULL or implement it as a no-op. */
1642     void (*set_mac_table_config)(struct ofproto *ofproto,
1643                                  unsigned int idle_time, size_t max_entries);
1644
1645     /* Configures multicast snooping on 'ofport' using the settings
1646      * defined in 's'.
1647      *
1648      * If 's' is nonnull, this function updates multicast snooping
1649      * configuration to 's' in 'ofproto'.
1650      *
1651      * If 's' is NULL, this function disables multicast snooping
1652      * on 'ofproto'.
1653      *
1654      * An implementation that does not support multicast snooping may set
1655      * it to NULL or return EOPNOTSUPP. */
1656     int (*set_mcast_snooping)(struct ofproto *ofproto,
1657                               const struct ofproto_mcast_snooping_settings *s);
1658
1659     /* Configures multicast snooping port's flood setting on 'ofproto'.
1660      *
1661      * If 's' is nonnull, this function updates multicast snooping
1662      * configuration to 's' in 'ofproto'.
1663      *
1664      * If 's' is NULL, this function doesn't change anything.
1665      *
1666      * An implementation that does not support multicast snooping may set
1667      * it to NULL or return EOPNOTSUPP. */
1668     int (*set_mcast_snooping_port)(struct ofproto *ofproto_, void *aux,
1669                           const struct ofproto_mcast_snooping_port_settings *s);
1670
1671 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
1672  *
1673  * This is deprecated.  It is only for compatibility with broken device drivers
1674  * in old versions of Linux that do not properly support VLANs when VLAN
1675  * devices are not used.  When broken device drivers are no longer in
1676  * widespread use, we will delete these interfaces. */
1677
1678     /* If 'realdev_ofp_port' is nonzero, then this function configures 'ofport'
1679      * as a VLAN splinter port for VLAN 'vid', associated with the real device
1680      * that has OpenFlow port number 'realdev_ofp_port'.
1681      *
1682      * If 'realdev_ofp_port' is zero, then this function deconfigures 'ofport'
1683      * as a VLAN splinter port.
1684      *
1685      * This function should be NULL if an implementation does not support it.
1686      */
1687     int (*set_realdev)(struct ofport *ofport,
1688                        ofp_port_t realdev_ofp_port, int vid);
1689
1690 /* ## ------------------------ ## */
1691 /* ## OpenFlow meter functions ## */
1692 /* ## ------------------------ ## */
1693
1694     /* These functions should be NULL if an implementation does not support
1695      * them.  They must be all null or all non-null.. */
1696
1697     /* Initializes 'features' to describe the metering features supported by
1698      * 'ofproto'. */
1699     void (*meter_get_features)(const struct ofproto *ofproto,
1700                                struct ofputil_meter_features *features);
1701
1702     /* If '*id' is UINT32_MAX, adds a new meter with the given 'config'.  On
1703      * success the function must store a provider meter ID other than
1704      * UINT32_MAX in '*id'.  All further references to the meter will be made
1705      * with the returned provider meter id rather than the OpenFlow meter id.
1706      * The caller does not try to interpret the provider meter id, giving the
1707      * implementation the freedom to either use the OpenFlow meter_id value
1708      * provided in the meter configuration, or any other value suitable for the
1709      * implementation.
1710      *
1711      * If '*id' is a value other than UINT32_MAX, modifies the existing meter
1712      * with that meter provider ID to have configuration 'config', while
1713      * leaving '*id' unchanged.  On failure, the existing meter configuration
1714      * is left intact. */
1715     enum ofperr (*meter_set)(struct ofproto *ofproto, ofproto_meter_id *id,
1716                              const struct ofputil_meter_config *config);
1717
1718     /* Gets the meter and meter band packet and byte counts for maximum of
1719      * 'stats->n_bands' bands for the meter with provider ID 'id' within
1720      * 'ofproto'.  The caller fills in the other stats values.  The band stats
1721      * are copied to memory at 'stats->bands' provided by the caller.  The
1722      * number of returned band stats is returned in 'stats->n_bands'. */
1723     enum ofperr (*meter_get)(const struct ofproto *ofproto,
1724                              ofproto_meter_id id,
1725                              struct ofputil_meter_stats *stats);
1726
1727     /* Deletes a meter, making the 'ofproto_meter_id' invalid for any
1728      * further calls. */
1729     void (*meter_del)(struct ofproto *, ofproto_meter_id);
1730
1731
1732 /* ## -------------------- ## */
1733 /* ## OpenFlow 1.1+ groups ## */
1734 /* ## -------------------- ## */
1735
1736     struct ofgroup *(*group_alloc)(void);
1737     enum ofperr (*group_construct)(struct ofgroup *);
1738     void (*group_destruct)(struct ofgroup *);
1739     void (*group_dealloc)(struct ofgroup *);
1740
1741     enum ofperr (*group_modify)(struct ofgroup *);
1742
1743     enum ofperr (*group_get_stats)(const struct ofgroup *,
1744                                    struct ofputil_group_stats *);
1745
1746 /* ## --------------------- ## */
1747 /* ## Datapath information  ## */
1748 /* ## --------------------- ## */
1749     /* Retrieve the version string of the datapath. The version
1750      * string can be NULL if it can not be determined.
1751      *
1752      * The version retuned is read only. The caller should not
1753      * free it.
1754      *
1755      * This function should be NULL if an implementation does not support it.
1756      */
1757     const char *(*get_datapath_version)(const struct ofproto *);
1758 };
1759
1760 extern const struct ofproto_class ofproto_dpif_class;
1761
1762 int ofproto_class_register(const struct ofproto_class *);
1763 int ofproto_class_unregister(const struct ofproto_class *);
1764
1765 /* flow_mod with execution context. */
1766 struct ofproto_flow_mod {
1767     struct ofputil_flow_mod fm;
1768
1769     cls_version_t version;              /* Version in which changes take
1770                                          * effect. */
1771     struct rule_collection old_rules;   /* Affected rules. */
1772     struct rule_collection new_rules;   /* Replacement rules. */
1773 };
1774
1775 /* port_mod with execution context. */
1776 struct ofproto_port_mod {
1777     struct ofputil_port_mod pm;
1778     struct ofport *port;                /* Affected port. */
1779 };
1780
1781 enum ofperr ofproto_flow_mod(struct ofproto *, struct ofproto_flow_mod *)
1782     OVS_EXCLUDED(ofproto_mutex);
1783 void ofproto_add_flow(struct ofproto *, const struct match *, int priority,
1784                       const struct ofpact *ofpacts, size_t ofpacts_len)
1785     OVS_EXCLUDED(ofproto_mutex);
1786 void ofproto_delete_flow(struct ofproto *, const struct match *, int priority)
1787     OVS_EXCLUDED(ofproto_mutex);
1788 void ofproto_flush_flows(struct ofproto *);
1789
1790 enum ofperr ofproto_check_ofpacts(struct ofproto *,
1791                                   const struct ofpact ofpacts[],
1792                                   size_t ofpacts_len);
1793 \f
1794 static inline const struct rule_actions *
1795 rule_get_actions(const struct rule *rule)
1796 {
1797     return rule->actions;
1798 }
1799
1800 /* Returns true if 'rule' is an OpenFlow 1.3 "table-miss" rule, false
1801  * otherwise.
1802  *
1803  * ("Table-miss" rules are special because a packet_in generated through one
1804  * uses OFPR_NO_MATCH as its reason, whereas packet_ins generated by any other
1805  * rule use OFPR_ACTION.) */
1806 static inline bool
1807 rule_is_table_miss(const struct rule *rule)
1808 {
1809     return rule->cr.priority == 0 && cls_rule_is_catchall(&rule->cr);
1810 }
1811
1812 /* Returns true if 'rule' should be hidden from the controller.
1813  *
1814  * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1815  * (e.g. by in-band control) and are intentionally hidden from the
1816  * controller. */
1817 static inline bool
1818 rule_is_hidden(const struct rule *rule)
1819 {
1820     return rule->cr.priority > UINT16_MAX;
1821 }
1822
1823 static inline struct rule *
1824 rule_from_cls_rule(const struct cls_rule *cls_rule)
1825 {
1826     return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1827 }
1828
1829 #endif /* ofproto/ofproto-provider.h */