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