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