2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef OFPROTO_OFPROTO_PROVIDER_H
18 #define OFPROTO_OFPROTO_PROVIDER_H 1
20 /* Definitions for use within ofproto.
26 * Lots of ofproto data structures are only accessed from a single thread.
27 * Those data structures are generally not thread-safe.
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.
37 #include "classifier.h"
38 #include "guarded-list.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
45 #include "ofproto/ofproto.h"
46 #include "ovs-atomic.h"
48 #include "ovs-thread.h"
54 struct ofputil_flow_mod;
59 extern struct ovs_mutex ofproto_mutex;
61 /* An OpenFlow switch.
63 * With few exceptions, ofproto implementations may look at these fields but
64 * should not modify them. */
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. */
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_*. */
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. */
93 long long int eviction_group_timer; /* For rate limited reheapification. */
94 struct oftable *tables;
97 /* Rules indexed on their cookie values, in all flow tables. */
98 struct hindex cookies OVS_GUARDED_BY(ofproto_mutex);
99 struct hmap learned_cookies OVS_GUARDED_BY(ofproto_mutex);
101 /* List of expirable flows, in all flow tables. */
102 struct list expirable OVS_GUARDED_BY(ofproto_mutex);
105 * OpenFlow meters start at 1. To avoid confusion we leave the first
106 * pointer in the array un-used, and index directly with the OpenFlow
108 struct ofputil_meter_features meter_features;
109 struct meter **meters; /* 'meter_features.max_meter' + 1 pointers. */
111 /* OpenFlow connections. */
112 struct connmgr *connmgr;
114 /* Delayed rule executions.
116 * We delay calls to ->ofproto_class->rule_execute() past releasing
117 * ofproto_mutex during a flow_mod, because otherwise a "learn" action
118 * triggered by the executing the packet would try to recursively modify
119 * the flow table and reacquire the global lock. */
120 struct guarded_list rule_executes; /* Contains "struct rule_execute"s. */
122 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
124 * This is deprecated. It is only for compatibility with broken device
125 * drivers in old versions of Linux that do not properly support VLANs when
126 * VLAN devices are not used. When broken device drivers are no longer in
127 * widespread use, we will delete these interfaces. */
128 unsigned long int *vlan_bitmap; /* 4096-bit bitmap of in-use VLANs. */
129 bool vlans_changed; /* True if new VLANs are in use. */
130 int min_mtu; /* Current MTU of non-internal ports. */
133 struct ovs_rwlock groups_rwlock;
134 struct hmap groups OVS_GUARDED; /* Contains "struct ofgroup"s. */
135 uint32_t n_groups[4] OVS_GUARDED; /* # of existing groups of each type. */
136 struct ofputil_group_features ogf;
139 void ofproto_init_tables(struct ofproto *, int n_tables);
140 void ofproto_init_max_ports(struct ofproto *, uint16_t max_ports);
142 struct ofproto *ofproto_lookup(const char *name);
143 struct ofport *ofproto_get_port(const struct ofproto *, ofp_port_t ofp_port);
145 /* An OpenFlow port within a "struct ofproto".
147 * The port's name is netdev_get_name(port->netdev).
149 * With few exceptions, ofproto implementations may look at these fields but
150 * should not modify them. */
152 struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
153 struct ofproto *ofproto; /* The ofproto that contains this port. */
154 struct netdev *netdev;
155 struct ofputil_phy_port pp;
156 ofp_port_t ofp_port; /* OpenFlow port number. */
158 long long int created; /* Time created, in msec. */
162 void ofproto_port_set_state(struct ofport *, enum ofputil_port_state);
164 /* OpenFlow table flags:
166 * - "Hidden" tables are not included in OpenFlow operations that operate on
167 * "all tables". For example, a request for flow stats on all tables will
168 * omit flows in hidden tables, table stats requests will omit the table
169 * entirely, and the switch features reply will not count the hidden table.
171 * However, operations that specifically name the particular table still
172 * operate on it. For example, flow_mods and flow stats requests on a
175 * To avoid gaps in table IDs (which have unclear validity in OpenFlow),
176 * hidden tables must be the highest-numbered tables that a provider
179 * - "Read-only" tables can't be changed through OpenFlow operations. (At
180 * the moment all flow table operations go effectively through OpenFlow, so
181 * this means that read-only tables can't be changed at all after the
182 * read-only flag is set.)
184 * The generic ofproto layer never sets these flags. An ofproto provider can
185 * set them if it is appropriate.
188 OFTABLE_HIDDEN = 1 << 0, /* Hide from most OpenFlow operations. */
189 OFTABLE_READONLY = 1 << 1 /* Don't allow OpenFlow controller to change
193 /* A flow table within a "struct ofproto".
199 * A cls->rwlock read-lock holder prevents rules from being added or deleted.
201 * Adding or removing rules requires holding ofproto_mutex AND the cls->rwlock
204 * cls->rwlock should be held only briefly. For extended access to a rule,
205 * increment its ref_count with ofproto_rule_ref(). A rule will not be freed
206 * until its ref_count reaches zero.
208 * Modifying a rule requires the rule's own mutex. Holding cls->rwlock (for
209 * read or write) does not allow the holder to modify the rule.
211 * Freeing a rule requires ofproto_mutex and the cls->rwlock write-lock. After
212 * removing the rule from the classifier, release a ref_count from the rule
213 * ('cls''s reference to the rule).
215 * Refer to the thread-safety notes on struct rule for more information.*/
217 enum oftable_flags flags;
218 struct classifier cls; /* Contains "struct rule"s. */
219 char *name; /* Table name exposed via OpenFlow, or NULL. */
221 /* Maximum number of flows or UINT_MAX if there is no limit besides any
222 * limit imposed by resource limitations. */
223 unsigned int max_flows;
225 /* These members determine the handling of an attempt to add a flow that
226 * would cause the table to have more than 'max_flows' flows.
228 * If 'eviction_fields' is NULL, overflows will be rejected with an error.
230 * If 'eviction_fields' is nonnull (regardless of whether n_eviction_fields
231 * is nonzero), an overflow will cause a flow to be removed. The flow to
232 * be removed is chosen to give fairness among groups distinguished by
233 * different values for the subfields within 'groups'. */
234 struct mf_subfield *eviction_fields;
235 size_t n_eviction_fields;
239 * When a flow is added that would cause the table to have more than
240 * 'max_flows' flows, and 'eviction_fields' is nonnull, these groups are
241 * used to decide which rule to evict: the rule is chosen from the eviction
242 * group that contains the greatest number of rules.*/
243 uint32_t eviction_group_id_basis;
244 struct hmap eviction_groups_by_id;
245 struct heap eviction_groups_by_size;
247 /* Table config: contains enum ofproto_table_config; accessed atomically. */
250 atomic_ulong n_matched;
251 atomic_ulong n_missed;
254 /* Assigns TABLE to each oftable, in turn, in OFPROTO.
256 * All parameters are evaluated multiple times. */
257 #define OFPROTO_FOR_EACH_TABLE(TABLE, OFPROTO) \
258 for ((TABLE) = (OFPROTO)->tables; \
259 (TABLE) < &(OFPROTO)->tables[(OFPROTO)->n_tables]; \
262 /* An OpenFlow flow within a "struct ofproto".
264 * With few exceptions, ofproto implementations may look at these fields but
265 * should not modify them.
271 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
272 * the classifier rule->ofproto->tables[rule->table_id].cls. The text below
273 * calls this classifier 'cls'.
278 * The thread safety rules described here for "struct rule" are motivated by
281 * - Prevent threads that read members of "struct rule" from reading bad
282 * data due to changes by some thread concurrently modifying those
285 * - Prevent two threads making changes to members of a given "struct rule"
286 * from interfering with each other.
292 * A rule 'rule' may be accessed without a risk of being freed by code that
293 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
294 * 'rule->ref_count' (or both). Code that needs to hold onto a rule for a
295 * while should take 'cls->rwlock', find the rule it needs, increment
296 * 'rule->ref_count' with ofproto_rule_ref(), and drop 'cls->rwlock'.
298 * 'rule->ref_count' protects 'rule' from being freed. It doesn't protect the
299 * rule from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
300 * protect members of 'rule' from modification (that's 'rule->mutex').
302 * 'rule->mutex' protects the members of 'rule' from modification. It doesn't
303 * protect the rule from being deleted from 'cls' (that's 'cls->rwlock') and it
304 * doesn't prevent the rule from being freed (that's 'rule->ref_count').
306 * Regarding thread safety, the members of a rule fall into the following
309 * - Immutable. These members are marked 'const'.
311 * - Members that may be safely read or written only by code holding
312 * ofproto_mutex. These are marked OVS_GUARDED_BY(ofproto_mutex).
314 * - Members that may be safely read only by code holding ofproto_mutex or
315 * 'rule->mutex', and safely written only by coding holding ofproto_mutex
316 * AND 'rule->mutex'. These are marked OVS_GUARDED.
319 /* Where this rule resides in an OpenFlow switch.
321 * These are immutable once the rule is constructed, hence 'const'. */
322 struct ofproto *const ofproto; /* The ofproto that contains this rule. */
323 const struct cls_rule cr; /* In owning ofproto's classifier. */
324 const uint8_t table_id; /* Index in ofproto's 'tables' array. */
326 /* Protects members marked OVS_GUARDED.
327 * Readers only need to hold this mutex.
328 * Writers must hold both this mutex AND ofproto_mutex.
329 * By implication writers can read *without* taking this mutex while they
330 * hold ofproto_mutex. */
331 struct ovs_mutex mutex OVS_ACQ_AFTER(ofproto_mutex);
333 /* Number of references.
334 * The classifier owns one reference.
335 * Any thread trying to keep a rule from being freed should hold its own
337 struct ovs_refcount ref_count;
339 /* A "flow cookie" is the OpenFlow name for a 64-bit value associated with
341 ovs_be64 flow_cookie OVS_GUARDED;
342 struct hindex_node cookie_node OVS_GUARDED_BY(ofproto_mutex);
344 enum ofputil_flow_mod_flags flags OVS_GUARDED;
347 uint16_t hard_timeout OVS_GUARDED; /* In seconds from ->modified. */
348 uint16_t idle_timeout OVS_GUARDED; /* In seconds from ->used. */
350 /* Eviction groups (see comment on struct eviction_group for explanation) .
352 * 'eviction_group' is this rule's eviction group, or NULL if it is not in
353 * any eviction group. When 'eviction_group' is nonnull, 'evg_node' is in
354 * the ->eviction_group->rules hmap. */
355 struct eviction_group *eviction_group OVS_GUARDED_BY(ofproto_mutex);
356 struct heap_node evg_node OVS_GUARDED_BY(ofproto_mutex);
358 /* OpenFlow actions. See struct rule_actions for more thread-safety
360 OVSRCU_TYPE(const struct rule_actions *) actions;
362 /* In owning meter's 'rules' list. An empty list if there is no meter. */
363 struct list meter_list_node OVS_GUARDED_BY(ofproto_mutex);
365 /* Flow monitors (e.g. for NXST_FLOW_MONITOR, related to struct ofmonitor).
367 * 'add_seqno' is the sequence number when this rule was created.
368 * 'modify_seqno' is the sequence number when this rule was last modified.
369 * See 'monitor_seqno' in connmgr.c for more information. */
370 enum nx_flow_monitor_flags monitor_flags OVS_GUARDED_BY(ofproto_mutex);
371 uint64_t add_seqno OVS_GUARDED_BY(ofproto_mutex);
372 uint64_t modify_seqno OVS_GUARDED_BY(ofproto_mutex);
374 /* Optimisation for flow expiry. In ofproto's 'expirable' list if this
375 * rule is expirable, otherwise empty. */
376 struct list expirable OVS_GUARDED_BY(ofproto_mutex);
378 /* Times. Last so that they are more likely close to the stats managed
379 * by the provider. */
380 long long int created OVS_GUARDED; /* Creation time. */
382 /* Must hold 'mutex' for both read/write, 'ofproto_mutex' not needed. */
383 long long int modified OVS_GUARDED; /* Time of last modification. */
386 void ofproto_rule_ref(struct rule *);
387 bool ofproto_rule_try_ref(struct rule *);
388 void ofproto_rule_unref(struct rule *);
390 static inline const struct rule_actions * rule_get_actions(const struct rule *);
391 static inline bool rule_is_table_miss(const struct rule *);
392 static inline bool rule_is_hidden(const struct rule *);
394 /* A set of actions within a "struct rule".
400 * A struct rule_actions may be accessed without a risk of being
401 * freed by code that holds a read-lock or write-lock on 'rule->mutex' (where
402 * 'rule' is the rule for which 'rule->actions == actions') or during the RCU
405 * All members are immutable: they do not change during the struct's
407 struct rule_actions {
410 * 'has_meter' is true if 'ofpacts' contains an OFPACT_METER action.
412 * 'has_learn_with_delete' is true if 'ofpacts' contains an OFPACT_LEARN
413 * action whose flags include NX_LEARN_F_DELETE_LEARNED. */
415 bool has_learn_with_delete;
418 uint32_t ofpacts_len; /* Size of 'ofpacts', in bytes. */
419 struct ofpact ofpacts[]; /* Sequence of "struct ofpacts". */
421 BUILD_ASSERT_DECL(offsetof(struct rule_actions, ofpacts) % OFPACT_ALIGNTO == 0);
423 const struct rule_actions *rule_actions_create(const struct ofpact *, size_t);
424 void rule_actions_destroy(const struct rule_actions *);
425 bool ofproto_rule_has_out_port(const struct rule *, ofp_port_t port)
426 OVS_REQUIRES(ofproto_mutex);
428 /* A set of rules to which an OpenFlow operation applies. */
429 struct rule_collection {
430 struct rule **rules; /* The rules. */
431 size_t n; /* Number of rules collected. */
433 size_t capacity; /* Number of rules that will fit in 'rules'. */
434 struct rule *stub[64]; /* Preallocated rules to avoid malloc(). */
437 void rule_collection_init(struct rule_collection *);
438 void rule_collection_add(struct rule_collection *, struct rule *);
439 void rule_collection_ref(struct rule_collection *) OVS_REQUIRES(ofproto_mutex);
440 void rule_collection_unref(struct rule_collection *);
441 void rule_collection_destroy(struct rule_collection *);
443 /* Limits the number of flows allowed in the datapath. Only affects the
444 * ofproto-dpif implementation. */
445 extern unsigned ofproto_flow_limit;
447 /* Maximum idle time (in ms) for flows to be cached in the datapath.
448 * Revalidators may expire flows more quickly than the configured value based
449 * on system load and other factors. This variable is subject to change. */
450 extern unsigned ofproto_max_idle;
452 /* Number of upcall handler and revalidator threads. Only affects the
453 * ofproto-dpif implementation. */
454 extern size_t n_handlers, n_revalidators;
456 static inline struct rule *rule_from_cls_rule(const struct cls_rule *);
458 void ofproto_rule_expire(struct rule *rule, uint8_t reason)
459 OVS_REQUIRES(ofproto_mutex);
460 void ofproto_rule_delete(struct ofproto *, struct rule *)
461 OVS_EXCLUDED(ofproto_mutex);
462 void ofproto_rule_reduce_timeouts(struct rule *rule, uint16_t idle_timeout,
463 uint16_t hard_timeout)
464 OVS_EXCLUDED(ofproto_mutex);
466 /* A group within a "struct ofproto".
468 * With few exceptions, ofproto implementations may look at these fields but
469 * should not modify them. */
471 struct hmap_node hmap_node OVS_GUARDED; /* In ofproto's "groups" hmap. */
473 /* Number of references.
475 * This is needed to keep track of references to the group in the xlate
478 * If the main thread removes the group from an ofproto, we need to
479 * guarantee that the group remains accessible to users of
480 * xlate_group_actions and the xlate_cache, as the xlate_cache will not be
481 * cleaned up until the corresponding datapath flows are revalidated. */
482 struct ovs_refcount ref_count;
484 /* No lock is needed to protect the fields below since they are not
485 * modified after construction. */
486 const struct ofproto *ofproto; /* The ofproto that contains this group. */
487 const uint32_t group_id;
488 const enum ofp11_group_type type; /* One of OFPGT_*. */
490 const long long int created; /* Creation time. */
491 const long long int modified; /* Time of last modification. */
493 struct list buckets; /* Contains "struct ofputil_bucket"s. */
494 const uint32_t n_buckets;
497 bool ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
498 struct ofgroup **group);
500 void ofproto_group_ref(struct ofgroup *);
501 void ofproto_group_unref(struct ofgroup *);
503 /* ofproto class structure, to be defined by each ofproto implementation.
509 * These functions work primarily with four different kinds of data
512 * - "struct ofproto", which represents an OpenFlow switch.
514 * - "struct ofport", which represents a port within an ofproto.
516 * - "struct rule", which represents an OpenFlow flow within an ofproto.
518 * - "struct ofgroup", which represents an OpenFlow 1.1+ group within an
521 * Each of these data structures contains all of the implementation-independent
522 * generic state for the respective concept, called the "base" state. None of
523 * them contains any extra space for ofproto implementations to use. Instead,
524 * each implementation is expected to declare its own data structure that
525 * contains an instance of the generic data structure plus additional
526 * implementation-specific members, called the "derived" state. The
527 * implementation can use casts or (preferably) the CONTAINER_OF macro to
528 * obtain access to derived state given only a pointer to the embedded generic
535 * Four stylized functions accompany each of these data structures:
537 * "alloc" "construct" "destruct" "dealloc"
538 * ------------ ---------------- --------------- --------------
539 * ofproto ->alloc ->construct ->destruct ->dealloc
540 * ofport ->port_alloc ->port_construct ->port_destruct ->port_dealloc
541 * rule ->rule_alloc ->rule_construct ->rule_destruct ->rule_dealloc
542 * group ->group_alloc ->group_construct ->group_destruct ->group_dealloc
544 * "ofproto", "ofport", and "group" have this exact life cycle. The "rule"
545 * data structure also follow this life cycle with some additional elaborations
546 * described under "Rule Life Cycle" below.
548 * Any instance of a given data structure goes through the following life
551 * 1. The client calls the "alloc" function to obtain raw memory. If "alloc"
552 * fails, skip all the other steps.
554 * 2. The client initializes all of the data structure's base state. If this
555 * fails, skip to step 7.
557 * 3. The client calls the "construct" function. The implementation
558 * initializes derived state. It may refer to the already-initialized
559 * base state. If "construct" fails, skip to step 6.
561 * 4. The data structure is now initialized and in use.
563 * 5. When the data structure is no longer needed, the client calls the
564 * "destruct" function. The implementation uninitializes derived state.
565 * The base state has not been uninitialized yet, so the implementation
566 * may still refer to it.
568 * 6. The client uninitializes all of the data structure's base state.
570 * 7. The client calls the "dealloc" to free the raw memory. The
571 * implementation must not refer to base or derived state in the data
572 * structure, because it has already been uninitialized.
574 * Each "alloc" function allocates and returns a new instance of the respective
575 * data structure. The "alloc" function is not given any information about the
576 * use of the new data structure, so it cannot perform much initialization.
577 * Its purpose is just to ensure that the new data structure has enough room
578 * for base and derived state. It may return a null pointer if memory is not
579 * available, in which case none of the other functions is called.
581 * Each "construct" function initializes derived state in its respective data
582 * structure. When "construct" is called, all of the base state has already
583 * been initialized, so the "construct" function may refer to it. The
584 * "construct" function is allowed to fail, in which case the client calls the
585 * "dealloc" function (but not the "destruct" function).
587 * Each "destruct" function uninitializes and frees derived state in its
588 * respective data structure. When "destruct" is called, the base state has
589 * not yet been uninitialized, so the "destruct" function may refer to it. The
590 * "destruct" function is not allowed to fail.
592 * Each "dealloc" function frees raw memory that was allocated by the the
593 * "alloc" function. The memory's base and derived members might not have ever
594 * been initialized (but if "construct" returned successfully, then it has been
595 * "destruct"ed already). The "dealloc" function is not allowed to fail.
601 * Most of these functions return 0 if they are successful or a positive error
602 * code on failure. Depending on the function, valid error codes are either
603 * errno values or OFPERR_* OpenFlow error codes.
605 * Most of these functions are expected to execute synchronously, that is, to
606 * block as necessary to obtain a result. Thus, these functions may return
607 * EAGAIN (or EWOULDBLOCK or EINPROGRESS) only where the function descriptions
608 * explicitly say those errors are a possibility. We may relax this
609 * requirement in the future if and when we encounter performance problems. */
610 struct ofproto_class {
611 /* ## ----------------- ## */
612 /* ## Factory Functions ## */
613 /* ## ----------------- ## */
615 /* Initializes provider. The caller may pass in 'iface_hints',
616 * which contains an shash of "struct iface_hint" elements indexed
617 * by the interface's name. The provider may use these hints to
618 * describe the startup configuration in order to reinitialize its
619 * state. The caller owns the provided data, so a provider must
620 * make copies of anything required. An ofproto provider must
621 * remove any existing state that is not described by the hint, and
622 * may choose to remove it all. */
623 void (*init)(const struct shash *iface_hints);
625 /* Enumerates the types of all support ofproto types into 'types'. The
626 * caller has already initialized 'types' and other ofproto classes might
627 * already have added names to it. */
628 void (*enumerate_types)(struct sset *types);
630 /* Enumerates the names of all existing datapath of the specified 'type'
631 * into 'names' 'all_dps'. The caller has already initialized 'names' as
634 * 'type' is one of the types enumerated by ->enumerate_types().
636 * Returns 0 if successful, otherwise a positive errno value.
638 int (*enumerate_names)(const char *type, struct sset *names);
640 /* Deletes the datapath with the specified 'type' and 'name'. The caller
641 * should have closed any open ofproto with this 'type' and 'name'; this
642 * function is allowed to fail if that is not the case.
644 * 'type' is one of the types enumerated by ->enumerate_types().
645 * 'name' is one of the names enumerated by ->enumerate_names() for 'type'.
647 * Returns 0 if successful, otherwise a positive errno value.
649 int (*del)(const char *type, const char *name);
651 /* Returns the type to pass to netdev_open() when a datapath of type
652 * 'datapath_type' has a port of type 'port_type', for a few special
653 * cases when a netdev type differs from a port type. For example,
654 * when using the userspace datapath, a port of type "internal"
655 * needs to be opened as "tap".
657 * Returns either 'type' itself or a string literal, which must not
659 const char *(*port_open_type)(const char *datapath_type,
660 const char *port_type);
662 /* ## ------------------------ ## */
663 /* ## Top-Level type Functions ## */
664 /* ## ------------------------ ## */
666 /* Performs any periodic activity required on ofprotos of type
669 * An ofproto provider may implement it or not, depending on whether
670 * it needs type-level maintenance.
672 * Returns 0 if successful, otherwise a positive errno value. */
673 int (*type_run)(const char *type);
675 /* Causes the poll loop to wake up when a type 'type''s 'run'
676 * function needs to be called, e.g. by calling the timer or fd
677 * waiting functions in poll-loop.h.
679 * An ofproto provider may implement it or not, depending on whether
680 * it needs type-level maintenance. */
681 void (*type_wait)(const char *type);
683 /* ## --------------------------- ## */
684 /* ## Top-Level ofproto Functions ## */
685 /* ## --------------------------- ## */
687 /* Life-cycle functions for an "ofproto" (see "Life Cycle" above).
693 * ->construct() should not modify any base members of the ofproto. The
694 * client will initialize the ofproto's 'ports' and 'tables' members after
695 * construction is complete.
697 * When ->construct() is called, the client does not yet know how many flow
698 * tables the datapath supports, so ofproto->n_tables will be 0 and
699 * ofproto->tables will be NULL. ->construct() should call
700 * ofproto_init_tables() to allocate and initialize ofproto->n_tables and
701 * ofproto->tables. Each flow table will be initially empty, so
702 * ->construct() should delete flows from the underlying datapath, if
703 * necessary, rather than populating the tables.
705 * If the ofproto knows the maximum port number that the datapath can have,
706 * then it can call ofproto_init_max_ports(). If it does so, then the
707 * client will ensure that the actions it allows to be used through
708 * OpenFlow do not refer to ports above that maximum number.
710 * Only one ofproto instance needs to be supported for any given datapath.
711 * If a datapath is already open as part of one "ofproto", then another
712 * attempt to "construct" the same datapath as part of another ofproto is
713 * allowed to fail with an error.
715 * ->construct() returns 0 if successful, otherwise a positive errno
722 * ->destruct() must also destroy all remaining rules in the ofproto's
723 * tables, by passing each remaining rule to ofproto_rule_delete().
725 * The client will destroy the flow tables themselves after ->destruct()
728 struct ofproto *(*alloc)(void);
729 int (*construct)(struct ofproto *ofproto);
730 void (*destruct)(struct ofproto *ofproto);
731 void (*dealloc)(struct ofproto *ofproto);
733 /* Performs any periodic activity required by 'ofproto'. It should:
735 * - Call connmgr_send_packet_in() for each received packet that missed
736 * in the OpenFlow flow table or that had a OFPP_CONTROLLER output
739 * - Call ofproto_rule_expire() for each OpenFlow flow that has reached
740 * its hard_timeout or idle_timeout, to expire the flow.
742 * Returns 0 if successful, otherwise a positive errno value. */
743 int (*run)(struct ofproto *ofproto);
745 /* Causes the poll loop to wake up when 'ofproto''s 'run' function needs to
746 * be called, e.g. by calling the timer or fd waiting functions in
748 void (*wait)(struct ofproto *ofproto);
750 /* Adds some memory usage statistics for the implementation of 'ofproto'
751 * into 'usage', for use with memory_report().
753 * This function is optional. */
754 void (*get_memory_usage)(const struct ofproto *ofproto,
755 struct simap *usage);
757 /* Adds some memory usage statistics for the implementation of 'type'
758 * into 'usage', for use with memory_report().
760 * This function is optional. */
761 void (*type_get_memory_usage)(const char *type, struct simap *usage);
763 /* Every "struct rule" in 'ofproto' is about to be deleted, one by one.
764 * This function may prepare for that, for example by clearing state in
765 * advance. It should *not* actually delete any "struct rule"s from
766 * 'ofproto', only prepare for it.
768 * This function is optional; it's really just for optimization in case
769 * it's cheaper to delete all the flows from your hardware in a single pass
770 * than to do it one by one. */
771 void (*flush)(struct ofproto *ofproto);
773 /* Helper for the OpenFlow OFPT_FEATURES_REQUEST request.
775 * The implementation should store true in '*arp_match_ip' if the switch
776 * supports matching IP addresses inside ARP requests and replies, false
779 * The implementation should store in '*actions' a bitmap of the supported
780 * OpenFlow actions. Vendor actions are not included in '*actions'. */
781 void (*get_features)(struct ofproto *ofproto,
783 enum ofputil_action_bitmap *actions);
785 /* Helper for the OpenFlow OFPST_TABLE statistics request.
787 * The 'ots' array contains 'ofproto->n_tables' elements. Each element is
790 * - 'table_id' to the array index.
792 * - 'name' to "table#" where # is the table ID.
794 * - 'match' and 'wildcards' to OFPXMT12_MASK.
796 * - 'write_actions' and 'apply_actions' to OFPAT12_OUTPUT.
798 * - 'write_setfields' and 'apply_setfields' to OFPXMT12_MASK.
800 * - 'metadata_match' and 'metadata_write' to OVS_BE64_MAX.
802 * - 'instructions' to OFPIT11_ALL.
804 * - 'config' to OFPTC11_TABLE_MISS_MASK.
806 * - 'max_entries' to 1,000,000.
808 * - 'active_count' to the classifier_count() for the table.
810 * - 'lookup_count' and 'matched_count' to 0.
812 * The implementation should update any members in each element for which
813 * it has better values:
815 * - 'name' to a more meaningful name.
817 * - 'wildcards' to the set of wildcards actually supported by the table
818 * (if it doesn't support all OpenFlow wildcards).
820 * - 'instructions' to set the instructions actually supported by
823 * - 'write_actions' to set the write actions actually supported by
824 * the table (if it doesn't support all OpenFlow actions).
826 * - 'apply_actions' to set the apply actions actually supported by
827 * the table (if it doesn't support all OpenFlow actions).
829 * - 'write_setfields' to set the write setfields actually supported by
832 * - 'apply_setfields' to set the apply setfields actually supported by
835 * - 'max_entries' to the maximum number of flows actually supported by
838 * - 'lookup_count' to the number of packets looked up in this flow table
841 * - 'matched_count' to the number of packets looked up in this flow
842 * table so far that matched one of the flow entries.
844 * All of the members of struct ofp12_table_stats are in network byte
847 void (*get_tables)(struct ofproto *ofproto, struct ofp12_table_stats *ots);
849 /* ## ---------------- ## */
850 /* ## ofport Functions ## */
851 /* ## ---------------- ## */
853 /* Life-cycle functions for a "struct ofport" (see "Life Cycle" above).
855 * ->port_construct() should not modify any base members of the ofport.
856 * An ofproto implementation should use the 'ofp_port' member of
857 * "struct ofport" as the OpenFlow port number.
859 * ofports are managed by the base ofproto code. The ofproto
860 * implementation should only create and destroy them in response to calls
861 * to these functions. The base ofproto code will create and destroy
862 * ofports in the following situations:
864 * - Just after the ->construct() function is called, the base ofproto
865 * iterates over all of the implementation's ports, using
866 * ->port_dump_start() and related functions, and constructs an ofport
867 * for each dumped port.
869 * - If ->port_poll() reports that a specific port has changed, then the
870 * base ofproto will query that port with ->port_query_by_name() and
871 * construct or destruct ofports as necessary to reflect the updated
874 * - If ->port_poll() returns ENOBUFS to report an unspecified port set
875 * change, then the base ofproto will iterate over all of the
876 * implementation's ports, in the same way as at ofproto
877 * initialization, and construct and destruct ofports to reflect all of
880 * ->port_construct() returns 0 if successful, otherwise a positive errno
883 struct ofport *(*port_alloc)(void);
884 int (*port_construct)(struct ofport *ofport);
885 void (*port_destruct)(struct ofport *ofport);
886 void (*port_dealloc)(struct ofport *ofport);
888 /* Called after 'ofport->netdev' is replaced by a new netdev object. If
889 * the ofproto implementation uses the ofport's netdev internally, then it
890 * should switch to using the new one. The old one has been closed.
892 * An ofproto implementation that doesn't need to do anything in this
893 * function may use a null pointer. */
894 void (*port_modified)(struct ofport *ofport);
896 /* Called after an OpenFlow request changes a port's configuration.
897 * 'ofport->pp.config' contains the new configuration. 'old_config'
898 * contains the previous configuration.
900 * The caller implements OFPUTIL_PC_PORT_DOWN using netdev functions to
901 * turn NETDEV_UP on and off, so this function doesn't have to do anything
902 * for that bit (and it won't be called if that is the only bit that
904 void (*port_reconfigured)(struct ofport *ofport,
905 enum ofputil_port_config old_config);
907 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
908 * initializes '*port' appropriately. Otherwise, returns a positive errno
911 * The caller owns the data in 'port' and must free it with
912 * ofproto_port_destroy() when it is no longer needed. */
913 int (*port_query_by_name)(const struct ofproto *ofproto,
914 const char *devname, struct ofproto_port *port);
916 /* Attempts to add 'netdev' as a port on 'ofproto'. Returns 0 if
917 * successful, otherwise a positive errno value. The caller should
918 * inform the implementation of the OpenFlow port through the
919 * ->port_construct() method.
921 * It doesn't matter whether the new port will be returned by a later call
922 * to ->port_poll(); the implementation may do whatever is more
924 int (*port_add)(struct ofproto *ofproto, struct netdev *netdev);
926 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'. Returns
927 * 0 if successful, otherwise a positive errno value.
929 * It doesn't matter whether the new port will be returned by a later call
930 * to ->port_poll(); the implementation may do whatever is more
932 int (*port_del)(struct ofproto *ofproto, ofp_port_t ofp_port);
935 int (*port_get_stats)(const struct ofport *port,
936 struct netdev_stats *stats);
938 /* Port iteration functions.
940 * The client might not be entirely in control of the ports within an
941 * ofproto. Some hardware implementations, for example, might have a fixed
942 * set of ports in a datapath. For this reason, the client needs a way to
943 * iterate through all the ports that are actually in a datapath. These
944 * functions provide that functionality.
946 * The 'state' pointer provides the implementation a place to
947 * keep track of its position. Its format is opaque to the caller.
949 * The ofproto provider retains ownership of the data that it stores into
950 * ->port_dump_next()'s 'port' argument. The data must remain valid until
951 * at least the next call to ->port_dump_next() or ->port_dump_done() for
952 * 'state'. The caller will not modify or free it.
957 * ->port_dump_start() attempts to begin dumping the ports in 'ofproto'.
958 * On success, it should return 0 and initialize '*statep' with any data
959 * needed for iteration. On failure, returns a positive errno value, and
960 * the client will not call ->port_dump_next() or ->port_dump_done().
962 * ->port_dump_next() attempts to retrieve another port from 'ofproto' for
963 * 'state'. If there is another port, it should store the port's
964 * information into 'port' and return 0. It should return EOF if all ports
965 * have already been iterated. Otherwise, on error, it should return a
966 * positive errno value. This function will not be called again once it
967 * returns nonzero once for a given iteration (but the 'port_dump_done'
968 * function will be called afterward).
970 * ->port_dump_done() allows the implementation to release resources used
971 * for iteration. The caller might decide to stop iteration in the middle
972 * by calling this function before ->port_dump_next() returns nonzero.
980 * error = ofproto->ofproto_class->port_dump_start(ofproto, &state);
983 * struct ofproto_port port;
985 * error = ofproto->ofproto_class->port_dump_next(
986 * ofproto, state, &port);
990 * // Do something with 'port' here (without modifying or freeing
991 * // any of its data).
993 * ofproto->ofproto_class->port_dump_done(ofproto, state);
995 * // 'error' is now EOF (success) or a positive errno value (failure).
997 int (*port_dump_start)(const struct ofproto *ofproto, void **statep);
998 int (*port_dump_next)(const struct ofproto *ofproto, void *state,
999 struct ofproto_port *port);
1000 int (*port_dump_done)(const struct ofproto *ofproto, void *state);
1002 /* Polls for changes in the set of ports in 'ofproto'. If the set of ports
1003 * in 'ofproto' has changed, then this function should do one of the
1006 * - Preferably: store the name of the device that was added to or deleted
1007 * from 'ofproto' in '*devnamep' and return 0. The caller is responsible
1008 * for freeing '*devnamep' (with free()) when it no longer needs it.
1010 * - Alternatively: return ENOBUFS, without indicating the device that was
1013 * Occasional 'false positives', in which the function returns 0 while
1014 * indicating a device that was not actually added or deleted or returns
1015 * ENOBUFS without any change, are acceptable.
1017 * The purpose of 'port_poll' is to let 'ofproto' know about changes made
1018 * externally to the 'ofproto' object, e.g. by a system administrator via
1019 * ovs-dpctl. Therefore, it's OK, and even preferable, for port_poll() to
1020 * not report changes made through calls to 'port_add' or 'port_del' on the
1021 * same 'ofproto' object. (But it's OK for it to report them too, just
1022 * slightly less efficient.)
1024 * If the set of ports in 'ofproto' has not changed, returns EAGAIN. May
1025 * also return other positive errno values to indicate that something has
1028 * If the set of ports in a datapath is fixed, or if the only way that the
1029 * set of ports in a datapath can change is through ->port_add() and
1030 * ->port_del(), then this function may be a null pointer.
1032 int (*port_poll)(const struct ofproto *ofproto, char **devnamep);
1034 /* Arranges for the poll loop to wake up when ->port_poll() will return a
1035 * value other than EAGAIN.
1037 * If the set of ports in a datapath is fixed, or if the only way that the
1038 * set of ports in a datapath can change is through ->port_add() and
1039 * ->port_del(), or if the poll loop will always wake up anyway when
1040 * ->port_poll() will return a value other than EAGAIN, then this function
1041 * may be a null pointer.
1043 void (*port_poll_wait)(const struct ofproto *ofproto);
1045 /* Checks the status of LACP negotiation for 'port'. Returns 1 if LACP
1046 * partner information for 'port' is up-to-date, 0 if LACP partner
1047 * information is not current (generally indicating a connectivity
1048 * problem), or -1 if LACP is not enabled on 'port'.
1050 * This function may be a null pointer if the ofproto implementation does
1051 * not support LACP. */
1052 int (*port_is_lacp_current)(const struct ofport *port);
1054 /* ## ----------------------- ## */
1055 /* ## OpenFlow Rule Functions ## */
1056 /* ## ----------------------- ## */
1058 /* Chooses an appropriate table for 'match' within 'ofproto'. On
1059 * success, stores the table ID into '*table_idp' and returns 0. On
1060 * failure, returns an OpenFlow error code.
1062 * The choice of table should be a function of 'match' and 'ofproto''s
1063 * datapath capabilities. It should not depend on the flows already in
1064 * 'ofproto''s flow tables. Failure implies that an OpenFlow rule with
1065 * 'match' as its matching condition can never be inserted into 'ofproto',
1066 * even starting from an empty flow table.
1068 * If multiple tables are candidates for inserting the flow, the function
1069 * should choose one arbitrarily (but deterministically).
1071 * If this function is NULL then table 0 is always chosen. */
1072 enum ofperr (*rule_choose_table)(const struct ofproto *ofproto,
1073 const struct match *match,
1074 uint8_t *table_idp);
1076 /* Life-cycle functions for a "struct rule".
1082 * The life cycle of a struct rule is an elaboration of the basic life
1083 * cycle described above under "Life Cycle".
1085 * After a rule is successfully constructed, it is then inserted. If
1086 * insertion is successful, then before it is later destructed, it is
1089 * You can think of a rule as having the following extra steps inserted
1090 * between "Life Cycle" steps 4 and 5:
1092 * 4.1. The client inserts the rule into the flow table, making it
1093 * visible in flow table lookups.
1095 * 4.2. The client calls "rule_insert" to insert the flow. The
1096 * implementation attempts to install the flow in the underlying
1097 * hardware and returns an error code indicate success or failure.
1098 * On failure, go to step 5.
1100 * 4.3. The rule is now installed in the flow table. Eventually it will
1103 * 4.4. The client removes the rule from the flow table. It is no longer
1104 * visible in flow table lookups.
1106 * 4.5. The client calls "rule_delete". The implementation uninstalls
1107 * the flow from the underlying hardware. Deletion is not allowed
1114 * When ->rule_construct() is called, 'rule' is a new rule that is not yet
1115 * inserted into a flow table. ->rule_construct() should initialize enough
1116 * of the rule's derived state for 'rule' to be suitable for inserting into
1117 * a flow table. ->rule_construct() should not modify any base members of
1120 * If ->rule_construct() fails (as indicated by returning a nonzero
1121 * OpenFlow error code), the ofproto base code will uninitialize and
1122 * deallocate 'rule'. See "Rule Life Cycle" above for more details.
1124 * ->rule_construct() may also:
1126 * - Validate that the datapath supports the matching rule in 'rule->cr'
1127 * datapath. For example, if the rule's table does not support
1128 * registers, then it is an error if 'rule->cr' does not wildcard all
1131 * - Validate that the datapath can correctly implement 'rule->ofpacts'.
1133 * Some implementations might need to defer these tasks to ->rule_insert(),
1134 * which is also acceptable.
1140 * Following successful construction, the ofproto base case inserts 'rule'
1141 * into its flow table, then it calls ->rule_insert(). ->rule_insert()
1142 * must add the new rule to the datapath flow table and return only after
1143 * this is complete (whether it succeeds or fails).
1145 * If ->rule_insert() fails, the ofproto base code will remove 'rule' from
1146 * the flow table, destruct, uninitialize, and deallocate 'rule'. See
1147 * "Rule Life Cycle" above for more details.
1153 * The ofproto base code removes 'rule' from its flow table before it calls
1154 * ->rule_delete(). ->rule_delete() must remove 'rule' from the datapath
1155 * flow table and return only after this has completed successfully.
1157 * Rule deletion must not fail.
1163 * ->rule_destruct() must uninitialize derived state.
1165 * Rule destruction must not fail. */
1166 struct rule *(*rule_alloc)(void);
1167 enum ofperr (*rule_construct)(struct rule *rule)
1168 /* OVS_REQUIRES(ofproto_mutex) */;
1169 enum ofperr (*rule_insert)(struct rule *rule)
1170 /* OVS_REQUIRES(ofproto_mutex) */;
1171 void (*rule_delete)(struct rule *rule) /* OVS_REQUIRES(ofproto_mutex) */;
1172 void (*rule_destruct)(struct rule *rule);
1173 void (*rule_dealloc)(struct rule *rule);
1175 /* Obtains statistics for 'rule', storing the number of packets that have
1176 * matched it in '*packet_count' and the number of bytes in those packets
1177 * in '*byte_count'. UINT64_MAX indicates that the packet count or byte
1178 * count is unknown. */
1179 void (*rule_get_stats)(struct rule *rule, uint64_t *packet_count,
1180 uint64_t *byte_count, long long int *used)
1181 /* OVS_EXCLUDED(ofproto_mutex) */;
1183 /* Applies the actions in 'rule' to 'packet'. (This implements sending
1184 * buffered packets for OpenFlow OFPT_FLOW_MOD commands.)
1186 * Takes ownership of 'packet' (so it should eventually free it, with
1189 * 'flow' reflects the flow information for 'packet'. All of the
1190 * information in 'flow' is extracted from 'packet', except for
1191 * flow->tunnel and flow->in_port, which are assigned the correct values
1192 * for the incoming packet. The register values are zeroed. 'packet''s
1193 * header pointers and offsets (e.g. packet->l3) are appropriately
1194 * initialized. packet->l3 is aligned on a 32-bit boundary.
1196 * The implementation should add the statistics for 'packet' into 'rule'.
1198 * Returns 0 if successful, otherwise an OpenFlow error code. */
1199 enum ofperr (*rule_execute)(struct rule *rule, const struct flow *flow,
1200 struct ofpbuf *packet);
1202 /* If the datapath can properly implement changing 'rule''s actions to the
1203 * 'ofpacts_len' bytes in 'ofpacts', returns 0. Otherwise, returns an enum
1204 * ofperr indicating why the new actions wouldn't work.
1206 * May be a null pointer if any set of actions is acceptable. */
1207 enum ofperr (*rule_premodify_actions)(const struct rule *rule,
1208 const struct ofpact *ofpacts,
1210 /* OVS_REQUIRES(ofproto_mutex) */;
1212 /* When ->rule_modify_actions() is called, the caller has already replaced
1213 * the OpenFlow actions in 'rule' by a new set. (If
1214 * ->rule_premodify_actions is nonnull, then it was previously called to
1215 * verify that the new set of actions is acceptable.)
1217 * ->rule_modify_actions() must:
1219 * - Update the datapath flow table with the new actions.
1221 * - Only if 'reset_counters' is true, reset any packet or byte counters
1222 * associated with the rule to zero, so that rule_get_stats() will not
1223 * longer count those packets or bytes.
1225 * Rule modification must not fail.
1227 * ->rule_modify_actions() should not modify any base members of struct
1229 void (*rule_modify_actions)(struct rule *rule, bool reset_counters)
1230 /* OVS_REQUIRES(ofproto_mutex) */;
1232 /* Changes the OpenFlow IP fragment handling policy to 'frag_handling',
1233 * which takes one of the following values, with the corresponding
1236 * - OFPC_FRAG_NORMAL: The switch should treat IP fragments the same way
1237 * as other packets, omitting TCP and UDP port numbers (always setting
1240 * - OFPC_FRAG_DROP: The switch should drop all IP fragments without
1241 * passing them through the flow table.
1243 * - OFPC_FRAG_REASM: The switch should reassemble IP fragments before
1244 * passing packets through the flow table.
1246 * - OFPC_FRAG_NX_MATCH (a Nicira extension): Similar to OFPC_FRAG_NORMAL,
1247 * except that TCP and UDP port numbers should be included in fragments
1250 * Implementations are not required to support every mode.
1251 * OFPC_FRAG_NORMAL is the default mode when an ofproto is created.
1253 * At the time of the call to ->set_frag_handling(), the current mode is
1254 * available in 'ofproto->frag_handling'. ->set_frag_handling() returns
1255 * true if the requested mode was set, false if it is not supported.
1257 * Upon successful return, the caller changes 'ofproto->frag_handling' to
1258 * reflect the new mode.
1260 bool (*set_frag_handling)(struct ofproto *ofproto,
1261 enum ofp_config_flags frag_handling);
1263 /* Implements the OpenFlow OFPT_PACKET_OUT command. The datapath should
1264 * execute the 'ofpacts_len' bytes of "struct ofpacts" in 'ofpacts'.
1266 * The caller retains ownership of 'packet' and of 'ofpacts', so
1267 * ->packet_out() should not modify or free them.
1269 * This function must validate that it can correctly implement 'ofpacts'.
1270 * If not, then it should return an OpenFlow error code.
1272 * 'flow' reflects the flow information for 'packet'. All of the
1273 * information in 'flow' is extracted from 'packet', except for
1274 * flow->in_port (see below). flow->tunnel and its register values are
1277 * flow->in_port comes from the OpenFlow OFPT_PACKET_OUT message. The
1278 * implementation should reject invalid flow->in_port values by returning
1279 * OFPERR_OFPBRC_BAD_PORT. (If the implementation called
1280 * ofproto_init_max_ports(), then the client will reject these ports
1281 * itself.) For consistency, the implementation should consider valid for
1282 * flow->in_port any value that could possibly be seen in a packet that it
1283 * passes to connmgr_send_packet_in(). Ideally, even an implementation
1284 * that never generates packet-ins (e.g. due to hardware limitations)
1285 * should still allow flow->in_port values for every possible physical port
1286 * and OFPP_LOCAL. The only virtual ports (those above OFPP_MAX) that the
1287 * caller will ever pass in as flow->in_port, other than OFPP_LOCAL, are
1288 * OFPP_NONE and OFPP_CONTROLLER. The implementation should allow both of
1289 * these, treating each of them as packets generated by the controller as
1290 * opposed to packets originating from some switch port.
1292 * (Ordinarily the only effect of flow->in_port is on output actions that
1293 * involve the input port, such as actions that output to OFPP_IN_PORT,
1294 * OFPP_FLOOD, or OFPP_ALL. flow->in_port can also affect Nicira extension
1295 * "resubmit" actions.)
1297 * 'packet' is not matched against the OpenFlow flow table, so its
1298 * statistics should not be included in OpenFlow flow statistics.
1300 * Returns 0 if successful, otherwise an OpenFlow error code. */
1301 enum ofperr (*packet_out)(struct ofproto *ofproto, struct ofpbuf *packet,
1302 const struct flow *flow,
1303 const struct ofpact *ofpacts,
1304 size_t ofpacts_len);
1306 /* ## ------------------------- ## */
1307 /* ## OFPP_NORMAL configuration ## */
1308 /* ## ------------------------- ## */
1310 /* Configures NetFlow on 'ofproto' according to the options in
1311 * 'netflow_options', or turns off NetFlow if 'netflow_options' is NULL.
1313 * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
1314 * NetFlow, as does a null pointer. */
1315 int (*set_netflow)(struct ofproto *ofproto,
1316 const struct netflow_options *netflow_options);
1318 void (*get_netflow_ids)(const struct ofproto *ofproto,
1319 uint8_t *engine_type, uint8_t *engine_id);
1321 /* Configures sFlow on 'ofproto' according to the options in
1322 * 'sflow_options', or turns off sFlow if 'sflow_options' is NULL.
1324 * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
1325 * sFlow, as does a null pointer. */
1326 int (*set_sflow)(struct ofproto *ofproto,
1327 const struct ofproto_sflow_options *sflow_options);
1329 /* Configures IPFIX on 'ofproto' according to the options in
1330 * 'bridge_exporter_options' and the 'flow_exporters_options'
1331 * array, or turns off IPFIX if 'bridge_exporter_options' and
1332 * 'flow_exporters_options' is NULL.
1334 * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
1335 * IPFIX, as does a null pointer. */
1337 struct ofproto *ofproto,
1338 const struct ofproto_ipfix_bridge_exporter_options
1339 *bridge_exporter_options,
1340 const struct ofproto_ipfix_flow_exporter_options
1341 *flow_exporters_options, size_t n_flow_exporters_options);
1343 /* Configures connectivity fault management on 'ofport'.
1345 * If 'cfm_settings' is nonnull, configures CFM according to its members.
1347 * If 'cfm_settings' is null, removes any connectivity fault management
1348 * configuration from 'ofport'.
1350 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1351 * support CFM, as does a null pointer. */
1352 int (*set_cfm)(struct ofport *ofport, const struct cfm_settings *s);
1354 /* Checks the status change of CFM on 'ofport'. Returns true if
1355 * there is status change since last call or if CFM is not specified. */
1356 bool (*cfm_status_changed)(struct ofport *ofport);
1358 /* Populates 'smap' with the status of CFM on 'ofport'. Returns 0 on
1359 * success, or a positive errno. EOPNOTSUPP as a return value indicates
1360 * that this ofproto_class does not support CFM, as does a null pointer.
1362 * The caller must provide and own '*status', and it must free the array
1363 * returned in 'status->rmps'. '*status' is indeterminate if the return
1364 * value is non-zero. */
1365 int (*get_cfm_status)(const struct ofport *ofport,
1366 struct cfm_status *status);
1368 /* Configures BFD on 'ofport'.
1370 * If 'cfg' is NULL, or 'cfg' does not contain the key value pair
1371 * "enable=true", removes BFD from 'ofport'. Otherwise, configures BFD
1372 * according to 'cfg'.
1374 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1375 * support BFD, as does a null pointer. */
1376 int (*set_bfd)(struct ofport *ofport, const struct smap *cfg);
1378 /* Checks the status change of BFD on 'ofport'. Returns true if there
1379 * is status change since last call or if BFD is not specified. */
1380 bool (*bfd_status_changed)(struct ofport *ofport);
1382 /* Populates 'smap' with the status of BFD on 'ofport'. Returns 0 on
1383 * success, or a positive errno. EOPNOTSUPP as a return value indicates
1384 * that this ofproto_class does not support BFD, as does a null pointer. */
1385 int (*get_bfd_status)(struct ofport *ofport, struct smap *smap);
1387 /* Configures spanning tree protocol (STP) on 'ofproto' using the
1388 * settings defined in 's'.
1390 * If 's' is nonnull, configures STP according to its members.
1392 * If 's' is null, removes any STP configuration from 'ofproto'.
1394 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1395 * support STP, as does a null pointer. */
1396 int (*set_stp)(struct ofproto *ofproto,
1397 const struct ofproto_stp_settings *s);
1399 /* Retrieves state of spanning tree protocol (STP) on 'ofproto'.
1401 * Stores STP state for 'ofproto' in 's'. If the 'enabled' member
1402 * is false, the other member values are not meaningful.
1404 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1405 * support STP, as does a null pointer. */
1406 int (*get_stp_status)(struct ofproto *ofproto,
1407 struct ofproto_stp_status *s);
1409 /* Configures spanning tree protocol (STP) on 'ofport' using the
1410 * settings defined in 's'.
1412 * If 's' is nonnull, configures STP according to its members. The
1413 * caller is responsible for assigning STP port numbers (using the
1414 * 'port_num' member in the range of 1 through 255, inclusive) and
1415 * ensuring there are no duplicates.
1417 * If 's' is null, removes any STP configuration from 'ofport'.
1419 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1420 * support STP, as does a null pointer. */
1421 int (*set_stp_port)(struct ofport *ofport,
1422 const struct ofproto_port_stp_settings *s);
1424 /* Retrieves spanning tree protocol (STP) port status of 'ofport'.
1426 * Stores STP state for 'ofport' in 's'. If the 'enabled' member is
1427 * false, the other member values are not meaningful.
1429 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1430 * support STP, as does a null pointer. */
1431 int (*get_stp_port_status)(struct ofport *ofport,
1432 struct ofproto_port_stp_status *s);
1434 /* Retrieves spanning tree protocol (STP) port statistics of 'ofport'.
1436 * Stores STP state for 'ofport' in 's'. If the 'enabled' member is
1437 * false, the other member values are not meaningful.
1439 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1440 * support STP, as does a null pointer. */
1441 int (*get_stp_port_stats)(struct ofport *ofport,
1442 struct ofproto_port_stp_stats *s);
1444 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
1445 * 'queues' attached to 'ofport'. This data is not intended to be
1446 * sufficient to implement QoS. Instead, providers may use this
1447 * information to implement features which require knowledge of what queues
1448 * exist on a port, and some basic information about them.
1450 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1451 * support QoS, as does a null pointer. */
1452 int (*set_queues)(struct ofport *ofport,
1453 const struct ofproto_port_queue *queues, size_t n_qdscp);
1455 /* If 's' is nonnull, this function registers a "bundle" associated with
1456 * client data pointer 'aux' in 'ofproto'. A bundle is the same concept as
1457 * a Port in OVSDB, that is, it consists of one or more "slave" devices
1458 * (Interfaces, in OVSDB) along with VLAN and LACP configuration and, if
1459 * there is more than one slave, a bonding configuration. If 'aux' is
1460 * already registered then this function updates its configuration to 's'.
1461 * Otherwise, this function registers a new bundle.
1463 * If 's' is NULL, this function unregisters the bundle registered on
1464 * 'ofproto' associated with client data pointer 'aux'. If no such bundle
1465 * has been registered, this has no effect.
1467 * This function affects only the behavior of the NXAST_AUTOPATH action and
1468 * output to the OFPP_NORMAL port. An implementation that does not support
1469 * it at all may set it to NULL or return EOPNOTSUPP. An implementation
1470 * that supports only a subset of the functionality should implement what
1471 * it can and return 0. */
1472 int (*bundle_set)(struct ofproto *ofproto, void *aux,
1473 const struct ofproto_bundle_settings *s);
1475 /* If 'port' is part of any bundle, removes it from that bundle. If the
1476 * bundle now has no ports, deletes the bundle. If the bundle now has only
1477 * one port, deconfigures the bundle's bonding configuration. */
1478 void (*bundle_remove)(struct ofport *ofport);
1480 /* If 's' is nonnull, this function registers a mirror associated with
1481 * client data pointer 'aux' in 'ofproto'. A mirror is the same concept as
1482 * a Mirror in OVSDB. If 'aux' is already registered then this function
1483 * updates its configuration to 's'. Otherwise, this function registers a
1486 * If 's' is NULL, this function unregisters the mirror registered on
1487 * 'ofproto' associated with client data pointer 'aux'. If no such mirror
1488 * has been registered, this has no effect.
1490 * An implementation that does not support mirroring at all may set
1491 * it to NULL or return EOPNOTSUPP. An implementation that supports
1492 * only a subset of the functionality should implement what it can
1494 int (*mirror_set)(struct ofproto *ofproto, void *aux,
1495 const struct ofproto_mirror_settings *s);
1497 /* Retrieves statistics from mirror associated with client data
1498 * pointer 'aux' in 'ofproto'. Stores packet and byte counts in
1499 * 'packets' and 'bytes', respectively. If a particular counter is
1500 * not supported, the appropriate argument is set to UINT64_MAX.
1502 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
1503 * support retrieving mirror statistics. */
1504 int (*mirror_get_stats)(struct ofproto *ofproto, void *aux,
1505 uint64_t *packets, uint64_t *bytes);
1507 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs
1508 * on which all packets are flooded, instead of using MAC learning. If
1509 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1511 * This function affects only the behavior of the OFPP_NORMAL action. An
1512 * implementation that does not support it may set it to NULL or return
1514 int (*set_flood_vlans)(struct ofproto *ofproto,
1515 unsigned long *flood_vlans);
1517 /* Returns true if 'aux' is a registered bundle that is currently in use as
1518 * the output for a mirror. */
1519 bool (*is_mirror_output_bundle)(const struct ofproto *ofproto, void *aux);
1521 /* When the configuration option of forward_bpdu changes, this function
1522 * will be invoked. */
1523 void (*forward_bpdu_changed)(struct ofproto *ofproto);
1525 /* Sets the MAC aging timeout for the OFPP_NORMAL action to 'idle_time', in
1526 * seconds, and the maximum number of MAC table entries to
1529 * An implementation that doesn't support configuring these features may
1530 * set this function to NULL or implement it as a no-op. */
1531 void (*set_mac_table_config)(struct ofproto *ofproto,
1532 unsigned int idle_time, size_t max_entries);
1534 /* Configures multicast snooping on 'ofport' using the settings
1537 * If 's' is nonnull, this function updates multicast snooping
1538 * configuration to 's' in 'ofproto'.
1540 * If 's' is NULL, this function disables multicast snooping
1543 * An implementation that does not support multicast snooping may set
1544 * it to NULL or return EOPNOTSUPP. */
1545 int (*set_mcast_snooping)(struct ofproto *ofproto,
1546 const struct ofproto_mcast_snooping_settings *s);
1548 /* Configures multicast snooping port's flood setting on 'ofproto'.
1550 * All multicast traffic is sent to struct port 'aux' in 'ofproto'
1551 * if 'flood' is true. Otherwise, struct port 'aux' is an ordinary
1554 * An implementation that does not support multicast snooping may set
1555 * it to NULL or return EOPNOTSUPP. */
1556 int (*set_mcast_snooping_port)(struct ofproto *ofproto_, void *aux,
1559 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
1561 * This is deprecated. It is only for compatibility with broken device drivers
1562 * in old versions of Linux that do not properly support VLANs when VLAN
1563 * devices are not used. When broken device drivers are no longer in
1564 * widespread use, we will delete these interfaces. */
1566 /* If 'realdev_ofp_port' is nonzero, then this function configures 'ofport'
1567 * as a VLAN splinter port for VLAN 'vid', associated with the real device
1568 * that has OpenFlow port number 'realdev_ofp_port'.
1570 * If 'realdev_ofp_port' is zero, then this function deconfigures 'ofport'
1571 * as a VLAN splinter port.
1573 * This function should be NULL if an implementation does not support it.
1575 int (*set_realdev)(struct ofport *ofport,
1576 ofp_port_t realdev_ofp_port, int vid);
1578 /* ## ------------------------ ## */
1579 /* ## OpenFlow meter functions ## */
1580 /* ## ------------------------ ## */
1582 /* These functions should be NULL if an implementation does not support
1583 * them. They must be all null or all non-null.. */
1585 /* Initializes 'features' to describe the metering features supported by
1587 void (*meter_get_features)(const struct ofproto *ofproto,
1588 struct ofputil_meter_features *features);
1590 /* If '*id' is UINT32_MAX, adds a new meter with the given 'config'. On
1591 * success the function must store a provider meter ID other than
1592 * UINT32_MAX in '*id'. All further references to the meter will be made
1593 * with the returned provider meter id rather than the OpenFlow meter id.
1594 * The caller does not try to interpret the provider meter id, giving the
1595 * implementation the freedom to either use the OpenFlow meter_id value
1596 * provided in the meter configuration, or any other value suitable for the
1599 * If '*id' is a value other than UINT32_MAX, modifies the existing meter
1600 * with that meter provider ID to have configuration 'config', while
1601 * leaving '*id' unchanged. On failure, the existing meter configuration
1602 * is left intact. */
1603 enum ofperr (*meter_set)(struct ofproto *ofproto, ofproto_meter_id *id,
1604 const struct ofputil_meter_config *config);
1606 /* Gets the meter and meter band packet and byte counts for maximum of
1607 * 'stats->n_bands' bands for the meter with provider ID 'id' within
1608 * 'ofproto'. The caller fills in the other stats values. The band stats
1609 * are copied to memory at 'stats->bands' provided by the caller. The
1610 * number of returned band stats is returned in 'stats->n_bands'. */
1611 enum ofperr (*meter_get)(const struct ofproto *ofproto,
1612 ofproto_meter_id id,
1613 struct ofputil_meter_stats *stats);
1615 /* Deletes a meter, making the 'ofproto_meter_id' invalid for any
1617 void (*meter_del)(struct ofproto *, ofproto_meter_id);
1620 /* ## -------------------- ## */
1621 /* ## OpenFlow 1.1+ groups ## */
1622 /* ## -------------------- ## */
1624 struct ofgroup *(*group_alloc)(void);
1625 enum ofperr (*group_construct)(struct ofgroup *);
1626 void (*group_destruct)(struct ofgroup *);
1627 void (*group_dealloc)(struct ofgroup *);
1629 enum ofperr (*group_modify)(struct ofgroup *);
1631 enum ofperr (*group_get_stats)(const struct ofgroup *,
1632 struct ofputil_group_stats *);
1635 extern const struct ofproto_class ofproto_dpif_class;
1637 int ofproto_class_register(const struct ofproto_class *);
1638 int ofproto_class_unregister(const struct ofproto_class *);
1640 int ofproto_flow_mod(struct ofproto *, struct ofputil_flow_mod *)
1641 OVS_EXCLUDED(ofproto_mutex);
1642 void ofproto_add_flow(struct ofproto *, const struct match *,
1643 unsigned int priority,
1644 const struct ofpact *ofpacts, size_t ofpacts_len)
1645 OVS_EXCLUDED(ofproto_mutex);
1646 void ofproto_delete_flow(struct ofproto *,
1647 const struct match *, unsigned int priority)
1648 OVS_EXCLUDED(ofproto_mutex);
1649 void ofproto_flush_flows(struct ofproto *);
1652 static inline const struct rule_actions *
1653 rule_get_actions(const struct rule *rule)
1655 return ovsrcu_get(const struct rule_actions *, &rule->actions);
1658 /* Returns true if 'rule' is an OpenFlow 1.3 "table-miss" rule, false
1661 * ("Table-miss" rules are special because a packet_in generated through one
1662 * uses OFPR_NO_MATCH as its reason, whereas packet_ins generated by any other
1663 * rule use OFPR_ACTION.) */
1665 rule_is_table_miss(const struct rule *rule)
1667 return rule->cr.priority == 0 && cls_rule_is_catchall(&rule->cr);
1670 /* Returns true if 'rule' should be hidden from the controller.
1672 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1673 * (e.g. by in-band control) and are intentionally hidden from the
1676 rule_is_hidden(const struct rule *rule)
1678 return rule->cr.priority > UINT16_MAX;
1681 static inline struct rule *
1682 rule_from_cls_rule(const struct cls_rule *cls_rule)
1684 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1687 #endif /* ofproto/ofproto-provider.h */