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