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