2 * Copyright (c) 2009-2017 Nicira, Inc.
3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "connectivity.h"
32 #include "dp-packet.h"
34 #include "openvswitch/hmap.h"
38 #include "ofproto-provider.h"
39 #include "openflow/nicira-ext.h"
40 #include "openflow/openflow.h"
41 #include "openvswitch/dynamic-string.h"
42 #include "openvswitch/meta-flow.h"
43 #include "openvswitch/ofp-actions.h"
44 #include "openvswitch/ofp-errors.h"
45 #include "openvswitch/ofp-msgs.h"
46 #include "openvswitch/ofp-print.h"
47 #include "openvswitch/ofp-util.h"
48 #include "openvswitch/ofpbuf.h"
49 #include "openvswitch/vlog.h"
53 #include "openvswitch/poll-loop.h"
56 #include "openvswitch/shash.h"
61 #include "tun-metadata.h"
62 #include "unaligned.h"
66 VLOG_DEFINE_THIS_MODULE(ofproto
);
68 COVERAGE_DEFINE(ofproto_flush
);
69 COVERAGE_DEFINE(ofproto_packet_out
);
70 COVERAGE_DEFINE(ofproto_queue_req
);
71 COVERAGE_DEFINE(ofproto_recv_openflow
);
72 COVERAGE_DEFINE(ofproto_reinit_ports
);
73 COVERAGE_DEFINE(ofproto_update_port
);
75 /* Default fields to use for prefix tries in each flow table, unless something
76 * else is configured. */
77 const enum mf_field_id default_prefix_fields
[2] =
78 { MFF_IPV4_DST
, MFF_IPV4_SRC
};
81 static void oftable_init(struct oftable
*);
82 static void oftable_destroy(struct oftable
*);
84 static void oftable_set_name(struct oftable
*, const char *name
);
86 static enum ofperr
evict_rules_from_table(struct oftable
*)
87 OVS_REQUIRES(ofproto_mutex
);
88 static void oftable_configure_eviction(struct oftable
*,
89 unsigned int eviction
,
90 const struct mf_subfield
*fields
,
92 OVS_REQUIRES(ofproto_mutex
);
94 /* This is the only combination of OpenFlow eviction flags that OVS supports: a
95 * combination of OF1.4+ importance, the remaining lifetime of the flow, and
96 * fairness based on user-specified fields. */
97 #define OFPROTO_EVICTION_FLAGS \
98 (OFPTMPEF14_OTHER | OFPTMPEF14_IMPORTANCE | OFPTMPEF14_LIFETIME)
100 /* A set of rules within a single OpenFlow table (oftable) that have the same
101 * values for the oftable's eviction_fields. A rule to be evicted, when one is
102 * needed, is taken from the eviction group that contains the greatest number
105 * An oftable owns any number of eviction groups, each of which contains any
108 * Membership in an eviction group is imprecise, based on the hash of the
109 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
110 * That is, if two rules have different eviction_fields, but those
111 * eviction_fields hash to the same value, then they will belong to the same
112 * eviction_group anyway.
114 * (When eviction is not enabled on an oftable, we don't track any eviction
115 * groups, to save time and space.) */
116 struct eviction_group
{
117 struct hmap_node id_node
; /* In oftable's "eviction_groups_by_id". */
118 struct heap_node size_node
; /* In oftable's "eviction_groups_by_size". */
119 struct heap rules
; /* Contains "struct rule"s. */
122 static bool choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
123 OVS_REQUIRES(ofproto_mutex
);
124 static uint64_t rule_eviction_priority(struct ofproto
*ofproto
, struct rule
*)
125 OVS_REQUIRES(ofproto_mutex
);
126 static void eviction_group_add_rule(struct rule
*)
127 OVS_REQUIRES(ofproto_mutex
);
128 static void eviction_group_remove_rule(struct rule
*)
129 OVS_REQUIRES(ofproto_mutex
);
131 static void rule_criteria_init(struct rule_criteria
*, uint8_t table_id
,
132 const struct match
*match
, int priority
,
133 ovs_version_t version
,
134 ovs_be64 cookie
, ovs_be64 cookie_mask
,
135 ofp_port_t out_port
, uint32_t out_group
);
136 static void rule_criteria_require_rw(struct rule_criteria
*,
137 bool can_write_readonly
);
138 static void rule_criteria_destroy(struct rule_criteria
*);
140 static enum ofperr
collect_rules_loose(struct ofproto
*,
141 const struct rule_criteria
*,
142 struct rule_collection
*);
144 struct learned_cookie
{
146 /* In struct ofproto's 'learned_cookies' hmap. */
147 struct hmap_node hmap_node
OVS_GUARDED_BY(ofproto_mutex
);
149 /* In 'dead_cookies' list when removed from hmap. */
150 struct ovs_list list_node
;
154 ovs_be64 cookie
OVS_GUARDED_BY(ofproto_mutex
);
155 uint8_t table_id
OVS_GUARDED_BY(ofproto_mutex
);
157 /* Number of references from "learn" actions.
159 * When this drops to 0, all of the flows in 'table_id' with the specified
160 * 'cookie' are deleted. */
161 int n
OVS_GUARDED_BY(ofproto_mutex
);
164 static const struct ofpact_learn
*next_learn_with_delete(
165 const struct rule_actions
*, const struct ofpact_learn
*start
);
167 static void learned_cookies_inc(struct ofproto
*, const struct rule_actions
*)
168 OVS_REQUIRES(ofproto_mutex
);
169 static void learned_cookies_dec(struct ofproto
*, const struct rule_actions
*,
170 struct ovs_list
*dead_cookies
)
171 OVS_REQUIRES(ofproto_mutex
);
172 static void learned_cookies_flush(struct ofproto
*, struct ovs_list
*dead_cookies
)
173 OVS_REQUIRES(ofproto_mutex
);
176 static void ofport_destroy__(struct ofport
*) OVS_EXCLUDED(ofproto_mutex
);
177 static void ofport_destroy(struct ofport
*, bool del
);
178 static bool ofport_is_mtu_overridden(const struct ofproto
*,
179 const struct ofport
*);
181 static int update_port(struct ofproto
*, const char *devname
);
182 static int init_ports(struct ofproto
*);
183 static void reinit_ports(struct ofproto
*);
185 static long long int ofport_get_usage(const struct ofproto
*,
186 ofp_port_t ofp_port
);
187 static void ofport_set_usage(struct ofproto
*, ofp_port_t ofp_port
,
188 long long int last_used
);
189 static void ofport_remove_usage(struct ofproto
*, ofp_port_t ofp_port
);
193 * Keeps track of the currently used and recently used ofport values and is
194 * used to prevent immediate recycling of ofport values. */
195 struct ofport_usage
{
196 struct hmap_node hmap_node
; /* In struct ofproto's "ofport_usage" hmap. */
197 ofp_port_t ofp_port
; /* OpenFlow port number. */
198 long long int last_used
; /* Last time the 'ofp_port' was used. LLONG_MAX
199 represents in-use ofports. */
203 static void ofproto_rule_send_removed(struct rule
*)
204 OVS_EXCLUDED(ofproto_mutex
);
205 static bool rule_is_readonly(const struct rule
*);
206 static void ofproto_rule_insert__(struct ofproto
*, struct rule
*)
207 OVS_REQUIRES(ofproto_mutex
);
208 static void ofproto_rule_remove__(struct ofproto
*, struct rule
*)
209 OVS_REQUIRES(ofproto_mutex
);
211 /* The source of an OpenFlow request.
213 * A table modification request can be generated externally, via OpenFlow, or
214 * internally through a function call. This structure indicates the source of
215 * an OpenFlow-generated table modification. For an internal flow_mod, it
216 * isn't meaningful and thus supplied as NULL. */
217 struct openflow_mod_requester
{
218 struct ofconn
*ofconn
; /* Connection on which flow_mod arrived. */
219 const struct ofp_header
*request
;
223 static enum ofperr
ofproto_rule_create(struct ofproto
*, struct cls_rule
*,
224 uint8_t table_id
, ovs_be64 new_cookie
,
225 uint16_t idle_timeout
,
226 uint16_t hard_timeout
,
227 enum ofputil_flow_mod_flags flags
,
229 const struct ofpact
*ofpacts
,
231 uint64_t match_tlv_bitmap
,
232 uint64_t ofpacts_tlv_bitmap
,
233 struct rule
**new_rule
)
234 OVS_NO_THREAD_SAFETY_ANALYSIS
;
236 static void replace_rule_start(struct ofproto
*, struct ofproto_flow_mod
*,
237 struct rule
*old_rule
, struct rule
*new_rule
)
238 OVS_REQUIRES(ofproto_mutex
);
240 static void replace_rule_revert(struct ofproto
*, struct rule
*old_rule
,
241 struct rule
*new_rule
)
242 OVS_REQUIRES(ofproto_mutex
);
244 static void replace_rule_finish(struct ofproto
*, struct ofproto_flow_mod
*,
245 const struct openflow_mod_requester
*,
246 struct rule
*old_rule
, struct rule
*new_rule
,
247 struct ovs_list
*dead_cookies
)
248 OVS_REQUIRES(ofproto_mutex
);
249 static void delete_flows__(struct rule_collection
*,
250 enum ofp_flow_removed_reason
,
251 const struct openflow_mod_requester
*)
252 OVS_REQUIRES(ofproto_mutex
);
254 static void ofproto_group_delete_all__(struct ofproto
*)
255 OVS_REQUIRES(ofproto_mutex
);
256 static bool ofproto_group_exists(const struct ofproto
*, uint32_t group_id
);
257 static void handle_openflow(struct ofconn
*, const struct ofpbuf
*);
258 static enum ofperr
ofproto_flow_mod_init(struct ofproto
*,
259 struct ofproto_flow_mod
*,
260 const struct ofputil_flow_mod
*fm
,
262 OVS_EXCLUDED(ofproto_mutex
);
263 static enum ofperr
ofproto_flow_mod_start(struct ofproto
*,
264 struct ofproto_flow_mod
*)
265 OVS_REQUIRES(ofproto_mutex
);
266 static void ofproto_flow_mod_revert(struct ofproto
*,
267 struct ofproto_flow_mod
*)
268 OVS_REQUIRES(ofproto_mutex
);
269 static void ofproto_flow_mod_finish(struct ofproto
*,
270 struct ofproto_flow_mod
*,
271 const struct openflow_mod_requester
*)
272 OVS_REQUIRES(ofproto_mutex
);
273 static enum ofperr
handle_flow_mod__(struct ofproto
*,
274 const struct ofputil_flow_mod
*,
275 const struct openflow_mod_requester
*)
276 OVS_EXCLUDED(ofproto_mutex
);
277 static void calc_duration(long long int start
, long long int now
,
278 uint32_t *sec
, uint32_t *nsec
);
281 static uint64_t pick_datapath_id(const struct ofproto
*);
282 static uint64_t pick_fallback_dpid(void);
283 static void ofproto_destroy__(struct ofproto
*);
284 static void update_mtu(struct ofproto
*, struct ofport
*);
285 static void update_mtu_ofproto(struct ofproto
*);
286 static void meter_delete(struct ofproto
*, uint32_t);
287 static void meter_delete_all(struct ofproto
*);
288 static void meter_insert_rule(struct rule
*);
291 static void ofproto_unixctl_init(void);
293 /* All registered ofproto classes, in probe order. */
294 static const struct ofproto_class
**ofproto_classes
;
295 static size_t n_ofproto_classes
;
296 static size_t allocated_ofproto_classes
;
298 /* Global lock that protects all flow table operations. */
299 struct ovs_mutex ofproto_mutex
= OVS_MUTEX_INITIALIZER
;
301 unsigned ofproto_flow_limit
= OFPROTO_FLOW_LIMIT_DEFAULT
;
302 unsigned ofproto_max_idle
= OFPROTO_MAX_IDLE_DEFAULT
;
304 size_t n_handlers
, n_revalidators
;
306 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
307 static struct hmap all_ofprotos
= HMAP_INITIALIZER(&all_ofprotos
);
309 /* Initial mappings of port to OpenFlow number mappings. */
310 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
312 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
314 /* The default value of true waits for flow restore. */
315 static bool flow_restore_wait
= true;
317 /* Must be called to initialize the ofproto library.
319 * The caller may pass in 'iface_hints', which contains an shash of
320 * "iface_hint" elements indexed by the interface's name. The provider
321 * may use these hints to describe the startup configuration in order to
322 * reinitialize its state. The caller owns the provided data, so a
323 * provider will make copies of anything required. An ofproto provider
324 * will remove any existing state that is not described by the hint, and
325 * may choose to remove it all. */
327 ofproto_init(const struct shash
*iface_hints
)
329 struct shash_node
*node
;
332 ofproto_class_register(&ofproto_dpif_class
);
334 /* Make a local copy, since we don't own 'iface_hints' elements. */
335 SHASH_FOR_EACH(node
, iface_hints
) {
336 const struct iface_hint
*orig_hint
= node
->data
;
337 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
338 const char *br_type
= ofproto_normalize_type(orig_hint
->br_type
);
340 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
341 new_hint
->br_type
= xstrdup(br_type
);
342 new_hint
->ofp_port
= orig_hint
->ofp_port
;
344 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
347 for (i
= 0; i
< n_ofproto_classes
; i
++) {
348 ofproto_classes
[i
]->init(&init_ofp_ports
);
351 ofproto_unixctl_init();
354 /* 'type' should be a normalized datapath type, as returned by
355 * ofproto_normalize_type(). Returns the corresponding ofproto_class
356 * structure, or a null pointer if there is none registered for 'type'. */
357 static const struct ofproto_class
*
358 ofproto_class_find__(const char *type
)
362 for (i
= 0; i
< n_ofproto_classes
; i
++) {
363 const struct ofproto_class
*class = ofproto_classes
[i
];
368 class->enumerate_types(&types
);
369 found
= sset_contains(&types
, type
);
370 sset_destroy(&types
);
376 VLOG_WARN("unknown datapath type %s", type
);
380 /* Registers a new ofproto class. After successful registration, new ofprotos
381 * of that type can be created using ofproto_create(). */
383 ofproto_class_register(const struct ofproto_class
*new_class
)
387 for (i
= 0; i
< n_ofproto_classes
; i
++) {
388 if (ofproto_classes
[i
] == new_class
) {
393 if (n_ofproto_classes
>= allocated_ofproto_classes
) {
394 ofproto_classes
= x2nrealloc(ofproto_classes
,
395 &allocated_ofproto_classes
,
396 sizeof *ofproto_classes
);
398 ofproto_classes
[n_ofproto_classes
++] = new_class
;
402 /* Unregisters a datapath provider. 'type' must have been previously
403 * registered and not currently be in use by any ofprotos. After
404 * unregistration new datapaths of that type cannot be opened using
405 * ofproto_create(). */
407 ofproto_class_unregister(const struct ofproto_class
*class)
411 for (i
= 0; i
< n_ofproto_classes
; i
++) {
412 if (ofproto_classes
[i
] == class) {
413 for (i
++; i
< n_ofproto_classes
; i
++) {
414 ofproto_classes
[i
- 1] = ofproto_classes
[i
];
420 VLOG_WARN("attempted to unregister an ofproto class that is not "
425 /* Clears 'types' and enumerates all registered ofproto types into it. The
426 * caller must first initialize the sset. */
428 ofproto_enumerate_types(struct sset
*types
)
433 for (i
= 0; i
< n_ofproto_classes
; i
++) {
434 ofproto_classes
[i
]->enumerate_types(types
);
438 /* Returns the fully spelled out name for the given ofproto 'type'.
440 * Normalized type string can be compared with strcmp(). Unnormalized type
441 * string might be the same even if they have different spellings. */
443 ofproto_normalize_type(const char *type
)
445 return type
&& type
[0] ? type
: "system";
448 /* Clears 'names' and enumerates the names of all known created ofprotos with
449 * the given 'type'. The caller must first initialize the sset. Returns 0 if
450 * successful, otherwise a positive errno value.
452 * Some kinds of datapaths might not be practically enumerable. This is not
453 * considered an error. */
455 ofproto_enumerate_names(const char *type
, struct sset
*names
)
457 const struct ofproto_class
*class = ofproto_class_find__(type
);
458 return class ? class->enumerate_names(type
, names
) : EAFNOSUPPORT
;
462 ofproto_bump_tables_version(struct ofproto
*ofproto
)
464 ++ofproto
->tables_version
;
465 ofproto
->ofproto_class
->set_tables_version(ofproto
,
466 ofproto
->tables_version
);
470 ofproto_create(const char *datapath_name
, const char *datapath_type
,
471 struct ofproto
**ofprotop
)
472 OVS_EXCLUDED(ofproto_mutex
)
474 const struct ofproto_class
*class;
475 struct ofproto
*ofproto
;
481 datapath_type
= ofproto_normalize_type(datapath_type
);
482 class = ofproto_class_find__(datapath_type
);
484 VLOG_WARN("could not create datapath %s of unknown type %s",
485 datapath_name
, datapath_type
);
489 ofproto
= class->alloc();
491 VLOG_ERR("failed to allocate datapath %s of type %s",
492 datapath_name
, datapath_type
);
497 ovs_mutex_lock(&ofproto_mutex
);
498 memset(ofproto
, 0, sizeof *ofproto
);
499 ofproto
->ofproto_class
= class;
500 ofproto
->name
= xstrdup(datapath_name
);
501 ofproto
->type
= xstrdup(datapath_type
);
502 hmap_insert(&all_ofprotos
, &ofproto
->hmap_node
,
503 hash_string(ofproto
->name
, 0));
504 ofproto
->datapath_id
= 0;
505 ofproto
->forward_bpdu
= false;
506 ofproto
->fallback_dpid
= pick_fallback_dpid();
507 ofproto
->mfr_desc
= NULL
;
508 ofproto
->hw_desc
= NULL
;
509 ofproto
->sw_desc
= NULL
;
510 ofproto
->serial_desc
= NULL
;
511 ofproto
->dp_desc
= NULL
;
512 ofproto
->frag_handling
= OFPUTIL_FRAG_NORMAL
;
513 hmap_init(&ofproto
->ports
);
514 hmap_init(&ofproto
->ofport_usage
);
515 shash_init(&ofproto
->port_by_name
);
516 simap_init(&ofproto
->ofp_requests
);
517 ofproto
->max_ports
= ofp_to_u16(OFPP_MAX
);
518 ofproto
->eviction_group_timer
= LLONG_MIN
;
519 ofproto
->tables
= NULL
;
520 ofproto
->n_tables
= 0;
521 ofproto
->tables_version
= OVS_VERSION_MIN
;
522 hindex_init(&ofproto
->cookies
);
523 hmap_init(&ofproto
->learned_cookies
);
524 ovs_list_init(&ofproto
->expirable
);
525 ofproto
->connmgr
= connmgr_create(ofproto
, datapath_name
, datapath_name
);
526 ofproto
->min_mtu
= INT_MAX
;
527 cmap_init(&ofproto
->groups
);
528 ovs_mutex_unlock(&ofproto_mutex
);
529 ofproto
->ogf
.types
= 0xf;
530 ofproto
->ogf
.capabilities
= OFPGFC_CHAINING
| OFPGFC_SELECT_LIVENESS
|
531 OFPGFC_SELECT_WEIGHT
;
532 for (i
= 0; i
< 4; i
++) {
533 ofproto
->ogf
.max_groups
[i
] = OFPG_MAX
;
534 ofproto
->ogf
.ofpacts
[i
] = (UINT64_C(1) << N_OFPACTS
) - 1;
536 ovsrcu_set(&ofproto
->metadata_tab
, tun_metadata_alloc(NULL
));
538 ovs_mutex_init(&ofproto
->vl_mff_map
.mutex
);
539 cmap_init(&ofproto
->vl_mff_map
.cmap
);
541 error
= ofproto
->ofproto_class
->construct(ofproto
);
543 VLOG_ERR("failed to open datapath %s: %s",
544 datapath_name
, ovs_strerror(error
));
545 ovs_mutex_lock(&ofproto_mutex
);
546 connmgr_destroy(ofproto
->connmgr
);
547 ofproto
->connmgr
= NULL
;
548 ovs_mutex_unlock(&ofproto_mutex
);
549 ofproto_destroy__(ofproto
);
553 /* Check that hidden tables, if any, are at the end. */
554 ovs_assert(ofproto
->n_tables
);
555 for (i
= 0; i
+ 1 < ofproto
->n_tables
; i
++) {
556 enum oftable_flags flags
= ofproto
->tables
[i
].flags
;
557 enum oftable_flags next_flags
= ofproto
->tables
[i
+ 1].flags
;
559 ovs_assert(!(flags
& OFTABLE_HIDDEN
) || next_flags
& OFTABLE_HIDDEN
);
562 ofproto
->datapath_id
= pick_datapath_id(ofproto
);
565 /* Initialize meters table. */
566 if (ofproto
->ofproto_class
->meter_get_features
) {
567 ofproto
->ofproto_class
->meter_get_features(ofproto
,
568 &ofproto
->meter_features
);
570 memset(&ofproto
->meter_features
, 0, sizeof ofproto
->meter_features
);
572 hmap_init(&ofproto
->meters
);
573 ofproto
->slowpath_meter_id
= UINT32_MAX
;
574 ofproto
->controller_meter_id
= UINT32_MAX
;
576 /* Set the initial tables version. */
577 ofproto_bump_tables_version(ofproto
);
583 /* Must be called (only) by an ofproto implementation in its constructor
584 * function. See the large comment on 'construct' in struct ofproto_class for
587 ofproto_init_tables(struct ofproto
*ofproto
, int n_tables
)
589 struct oftable
*table
;
591 ovs_assert(!ofproto
->n_tables
);
592 ovs_assert(n_tables
>= 1 && n_tables
<= 255);
594 ofproto
->n_tables
= n_tables
;
595 ofproto
->tables
= xmalloc(n_tables
* sizeof *ofproto
->tables
);
596 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
601 /* To be optionally called (only) by an ofproto implementation in its
602 * constructor function. See the large comment on 'construct' in struct
603 * ofproto_class for details.
605 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
606 * will then ensure that actions passed into the ofproto implementation will
607 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
608 * function is not called, there will be no such restriction.
610 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
611 * the 'max_ports' restriction. */
613 ofproto_init_max_ports(struct ofproto
*ofproto
, uint16_t max_ports
)
615 ovs_assert(max_ports
<= ofp_to_u16(OFPP_MAX
));
616 ofproto
->max_ports
= max_ports
;
620 ofproto_get_datapath_id(const struct ofproto
*ofproto
)
622 return ofproto
->datapath_id
;
626 ofproto_set_datapath_id(struct ofproto
*p
, uint64_t datapath_id
)
628 uint64_t old_dpid
= p
->datapath_id
;
629 p
->datapath_id
= datapath_id
? datapath_id
: pick_datapath_id(p
);
630 if (p
->datapath_id
!= old_dpid
) {
631 /* Force all active connections to reconnect, since there is no way to
632 * notify a controller that the datapath ID has changed. */
633 ofproto_reconnect_controllers(p
);
638 ofproto_set_controllers(struct ofproto
*p
,
639 const struct ofproto_controller
*controllers
,
640 size_t n_controllers
, uint32_t allowed_versions
)
642 connmgr_set_controllers(p
->connmgr
, controllers
, n_controllers
,
647 ofproto_set_fail_mode(struct ofproto
*p
, enum ofproto_fail_mode fail_mode
)
649 connmgr_set_fail_mode(p
->connmgr
, fail_mode
);
652 /* Drops the connections between 'ofproto' and all of its controllers, forcing
653 * them to reconnect. */
655 ofproto_reconnect_controllers(struct ofproto
*ofproto
)
657 connmgr_reconnect(ofproto
->connmgr
);
660 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
661 * in-band control should guarantee access, in the same way that in-band
662 * control guarantees access to OpenFlow controllers. */
664 ofproto_set_extra_in_band_remotes(struct ofproto
*ofproto
,
665 const struct sockaddr_in
*extras
, size_t n
)
667 connmgr_set_extra_in_band_remotes(ofproto
->connmgr
, extras
, n
);
670 /* Sets the OpenFlow queue used by flows set up by in-band control on
671 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
672 * flows will use the default queue. */
674 ofproto_set_in_band_queue(struct ofproto
*ofproto
, int queue_id
)
676 connmgr_set_in_band_queue(ofproto
->connmgr
, queue_id
);
679 /* Sets the number of flows at which eviction from the kernel flow table
682 ofproto_set_flow_limit(unsigned limit
)
684 ofproto_flow_limit
= limit
;
687 /* Sets the maximum idle time for flows in the datapath before they are
690 ofproto_set_max_idle(unsigned max_idle
)
692 ofproto_max_idle
= max_idle
;
695 /* If forward_bpdu is true, the NORMAL action will forward frames with
696 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
697 * the NORMAL action will drop these frames. */
699 ofproto_set_forward_bpdu(struct ofproto
*ofproto
, bool forward_bpdu
)
701 bool old_val
= ofproto
->forward_bpdu
;
702 ofproto
->forward_bpdu
= forward_bpdu
;
703 if (old_val
!= ofproto
->forward_bpdu
) {
704 if (ofproto
->ofproto_class
->forward_bpdu_changed
) {
705 ofproto
->ofproto_class
->forward_bpdu_changed(ofproto
);
710 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
711 * 'idle_time', in seconds, and the maximum number of MAC table entries to
714 ofproto_set_mac_table_config(struct ofproto
*ofproto
, unsigned idle_time
,
717 if (ofproto
->ofproto_class
->set_mac_table_config
) {
718 ofproto
->ofproto_class
->set_mac_table_config(ofproto
, idle_time
,
723 /* Multicast snooping configuration. */
725 /* Configures multicast snooping on 'ofproto' using the settings
726 * defined in 's'. If 's' is NULL, disables multicast snooping.
728 * Returns 0 if successful, otherwise a positive errno value. */
730 ofproto_set_mcast_snooping(struct ofproto
*ofproto
,
731 const struct ofproto_mcast_snooping_settings
*s
)
733 return (ofproto
->ofproto_class
->set_mcast_snooping
734 ? ofproto
->ofproto_class
->set_mcast_snooping(ofproto
, s
)
738 /* Configures multicast snooping flood settings on 'ofp_port' of 'ofproto'.
740 * Returns 0 if successful, otherwise a positive errno value.*/
742 ofproto_port_set_mcast_snooping(struct ofproto
*ofproto
, void *aux
,
743 const struct ofproto_mcast_snooping_port_settings
*s
)
745 return (ofproto
->ofproto_class
->set_mcast_snooping_port
746 ? ofproto
->ofproto_class
->set_mcast_snooping_port(ofproto
, aux
, s
)
751 ofproto_type_set_config(const char *datapath_type
, const struct smap
*cfg
)
753 const struct ofproto_class
*class;
755 datapath_type
= ofproto_normalize_type(datapath_type
);
756 class = ofproto_class_find__(datapath_type
);
758 if (class->type_set_config
) {
759 class->type_set_config(datapath_type
, cfg
);
764 ofproto_set_threads(int n_handlers_
, int n_revalidators_
)
766 int threads
= MAX(count_cpu_cores(), 2);
768 n_revalidators
= MAX(n_revalidators_
, 0);
769 n_handlers
= MAX(n_handlers_
, 0);
771 if (!n_revalidators
) {
772 n_revalidators
= n_handlers
773 ? MAX(threads
- (int) n_handlers
, 1)
778 n_handlers
= MAX(threads
- (int) n_revalidators
, 1);
783 ofproto_set_dp_desc(struct ofproto
*p
, const char *dp_desc
)
786 p
->dp_desc
= nullable_xstrdup(dp_desc
);
790 ofproto_set_snoops(struct ofproto
*ofproto
, const struct sset
*snoops
)
792 return connmgr_set_snoops(ofproto
->connmgr
, snoops
);
796 ofproto_set_netflow(struct ofproto
*ofproto
,
797 const struct netflow_options
*nf_options
)
799 if (nf_options
&& sset_is_empty(&nf_options
->collectors
)) {
803 if (ofproto
->ofproto_class
->set_netflow
) {
804 return ofproto
->ofproto_class
->set_netflow(ofproto
, nf_options
);
806 return nf_options
? EOPNOTSUPP
: 0;
811 ofproto_set_sflow(struct ofproto
*ofproto
,
812 const struct ofproto_sflow_options
*oso
)
814 if (oso
&& sset_is_empty(&oso
->targets
)) {
818 if (ofproto
->ofproto_class
->set_sflow
) {
819 return ofproto
->ofproto_class
->set_sflow(ofproto
, oso
);
821 return oso
? EOPNOTSUPP
: 0;
826 ofproto_set_ipfix(struct ofproto
*ofproto
,
827 const struct ofproto_ipfix_bridge_exporter_options
*bo
,
828 const struct ofproto_ipfix_flow_exporter_options
*fo
,
831 if (ofproto
->ofproto_class
->set_ipfix
) {
832 return ofproto
->ofproto_class
->set_ipfix(ofproto
, bo
, fo
, n_fo
);
834 return (bo
|| fo
) ? EOPNOTSUPP
: 0;
839 ofproto_get_ipfix_stats(struct ofproto
*ofproto
,
841 struct ovs_list
*replies
)
845 if (ofproto
->ofproto_class
->get_ipfix_stats
) {
846 error
= ofproto
->ofproto_class
->get_ipfix_stats(ofproto
,
857 handle_ipfix_bridge_stats_request(struct ofconn
*ofconn
,
858 const struct ofp_header
*request
)
860 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
861 struct ovs_list replies
;
864 ofpmp_init(&replies
, request
);
865 error
= ofproto_get_ipfix_stats(ofproto
, true, &replies
);
868 ofconn_send_replies(ofconn
, &replies
);
870 ofpbuf_list_delete(&replies
);
877 handle_ipfix_flow_stats_request(struct ofconn
*ofconn
,
878 const struct ofp_header
*request
)
880 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
881 struct ovs_list replies
;
884 ofpmp_init(&replies
, request
);
885 error
= ofproto_get_ipfix_stats(ofproto
, false, &replies
);
888 ofconn_send_replies(ofconn
, &replies
);
890 ofpbuf_list_delete(&replies
);
897 handle_nxt_ct_flush_zone(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
899 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
900 const struct nx_zone_id
*nzi
= ofpmsg_body(oh
);
902 if (!is_all_zeros(nzi
->zero
, sizeof nzi
->zero
)) {
903 return OFPERR_NXBRC_MUST_BE_ZERO
;
906 uint16_t zone
= ntohs(nzi
->zone_id
);
907 if (ofproto
->ofproto_class
->ct_flush
) {
908 ofproto
->ofproto_class
->ct_flush(ofproto
, &zone
);
917 ofproto_set_flow_restore_wait(bool flow_restore_wait_db
)
919 flow_restore_wait
= flow_restore_wait_db
;
923 ofproto_get_flow_restore_wait(void)
925 return flow_restore_wait
;
929 /* Spanning Tree Protocol (STP) configuration. */
931 /* Configures STP on 'ofproto' using the settings defined in 's'. If
932 * 's' is NULL, disables STP.
934 * Returns 0 if successful, otherwise a positive errno value. */
936 ofproto_set_stp(struct ofproto
*ofproto
,
937 const struct ofproto_stp_settings
*s
)
939 return (ofproto
->ofproto_class
->set_stp
940 ? ofproto
->ofproto_class
->set_stp(ofproto
, s
)
944 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
945 * 'enabled' member of 's' is false, then the other members are not
948 * Returns 0 if successful, otherwise a positive errno value. */
950 ofproto_get_stp_status(struct ofproto
*ofproto
,
951 struct ofproto_stp_status
*s
)
953 return (ofproto
->ofproto_class
->get_stp_status
954 ? ofproto
->ofproto_class
->get_stp_status(ofproto
, s
)
958 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
959 * in 's'. The caller is responsible for assigning STP port numbers
960 * (using the 'port_num' member in the range of 1 through 255, inclusive)
961 * and ensuring there are no duplicates. If the 's' is NULL, then STP
962 * is disabled on the port.
964 * Returns 0 if successful, otherwise a positive errno value.*/
966 ofproto_port_set_stp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
967 const struct ofproto_port_stp_settings
*s
)
969 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
971 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu32
,
972 ofproto
->name
, ofp_port
);
976 return (ofproto
->ofproto_class
->set_stp_port
977 ? ofproto
->ofproto_class
->set_stp_port(ofport
, s
)
981 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
982 * 's'. If the 'enabled' member in 's' is false, then the other members
983 * are not meaningful.
985 * Returns 0 if successful, otherwise a positive errno value.*/
987 ofproto_port_get_stp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
988 struct ofproto_port_stp_status
*s
)
990 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
992 VLOG_WARN_RL(&rl
, "%s: cannot get STP status on nonexistent "
993 "port %"PRIu32
, ofproto
->name
, ofp_port
);
997 return (ofproto
->ofproto_class
->get_stp_port_status
998 ? ofproto
->ofproto_class
->get_stp_port_status(ofport
, s
)
1002 /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
1003 * 's'. If the 'enabled' member in 's' is false, then the other members
1004 * are not meaningful.
1006 * Returns 0 if successful, otherwise a positive errno value.*/
1008 ofproto_port_get_stp_stats(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1009 struct ofproto_port_stp_stats
*s
)
1011 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1013 VLOG_WARN_RL(&rl
, "%s: cannot get STP stats on nonexistent "
1014 "port %"PRIu32
, ofproto
->name
, ofp_port
);
1018 return (ofproto
->ofproto_class
->get_stp_port_stats
1019 ? ofproto
->ofproto_class
->get_stp_port_stats(ofport
, s
)
1023 /* Rapid Spanning Tree Protocol (RSTP) configuration. */
1025 /* Configures RSTP on 'ofproto' using the settings defined in 's'. If
1026 * 's' is NULL, disables RSTP.
1028 * Returns 0 if successful, otherwise a positive errno value. */
1030 ofproto_set_rstp(struct ofproto
*ofproto
,
1031 const struct ofproto_rstp_settings
*s
)
1033 if (!ofproto
->ofproto_class
->set_rstp
) {
1036 ofproto
->ofproto_class
->set_rstp(ofproto
, s
);
1040 /* Retrieves RSTP status of 'ofproto' and stores it in 's'. If the
1041 * 'enabled' member of 's' is false, then the other members are not
1044 * Returns 0 if successful, otherwise a positive errno value. */
1046 ofproto_get_rstp_status(struct ofproto
*ofproto
,
1047 struct ofproto_rstp_status
*s
)
1049 if (!ofproto
->ofproto_class
->get_rstp_status
) {
1052 ofproto
->ofproto_class
->get_rstp_status(ofproto
, s
);
1056 /* Configures RSTP on 'ofp_port' of 'ofproto' using the settings defined
1057 * in 's'. The caller is responsible for assigning RSTP port numbers
1058 * (using the 'port_num' member in the range of 1 through 255, inclusive)
1059 * and ensuring there are no duplicates. If the 's' is NULL, then RSTP
1060 * is disabled on the port.
1062 * Returns 0 if successful, otherwise a positive errno value.*/
1064 ofproto_port_set_rstp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1065 const struct ofproto_port_rstp_settings
*s
)
1067 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1069 VLOG_WARN("%s: cannot configure RSTP on nonexistent port %"PRIu32
,
1070 ofproto
->name
, ofp_port
);
1074 if (!ofproto
->ofproto_class
->set_rstp_port
) {
1077 ofproto
->ofproto_class
->set_rstp_port(ofport
, s
);
1081 /* Retrieves RSTP port status of 'ofp_port' on 'ofproto' and stores it in
1082 * 's'. If the 'enabled' member in 's' is false, then the other members
1083 * are not meaningful.
1085 * Returns 0 if successful, otherwise a positive errno value.*/
1087 ofproto_port_get_rstp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1088 struct ofproto_port_rstp_status
*s
)
1090 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1092 VLOG_WARN_RL(&rl
, "%s: cannot get RSTP status on nonexistent "
1093 "port %"PRIu32
, ofproto
->name
, ofp_port
);
1097 if (!ofproto
->ofproto_class
->get_rstp_port_status
) {
1100 ofproto
->ofproto_class
->get_rstp_port_status(ofport
, s
);
1104 /* Queue DSCP configuration. */
1106 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
1107 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
1108 * to implement QoS. Instead, it is used to implement features which require
1109 * knowledge of what queues exist on a port, and some basic information about
1112 * Returns 0 if successful, otherwise a positive errno value. */
1114 ofproto_port_set_queues(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1115 const struct ofproto_port_queue
*queues
,
1118 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1121 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu32
,
1122 ofproto
->name
, ofp_port
);
1126 return (ofproto
->ofproto_class
->set_queues
1127 ? ofproto
->ofproto_class
->set_queues(ofport
, queues
, n_queues
)
1131 /* LLDP configuration. */
1133 ofproto_port_set_lldp(struct ofproto
*ofproto
,
1134 ofp_port_t ofp_port
,
1135 const struct smap
*cfg
)
1137 struct ofport
*ofport
;
1140 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1142 VLOG_WARN("%s: cannot configure LLDP on nonexistent port %"PRIu32
,
1143 ofproto
->name
, ofp_port
);
1146 error
= (ofproto
->ofproto_class
->set_lldp
1147 ? ofproto
->ofproto_class
->set_lldp(ofport
, cfg
)
1150 VLOG_WARN("%s: lldp configuration on port %"PRIu32
" (%s) failed (%s)",
1151 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1152 ovs_strerror(error
));
1157 ofproto_set_aa(struct ofproto
*ofproto
, void *aux OVS_UNUSED
,
1158 const struct aa_settings
*s
)
1160 if (!ofproto
->ofproto_class
->set_aa
) {
1163 ofproto
->ofproto_class
->set_aa(ofproto
, s
);
1168 ofproto_aa_mapping_register(struct ofproto
*ofproto
, void *aux
,
1169 const struct aa_mapping_settings
*s
)
1171 if (!ofproto
->ofproto_class
->aa_mapping_set
) {
1174 ofproto
->ofproto_class
->aa_mapping_set(ofproto
, aux
, s
);
1179 ofproto_aa_mapping_unregister(struct ofproto
*ofproto
, void *aux
)
1181 if (!ofproto
->ofproto_class
->aa_mapping_unset
) {
1184 ofproto
->ofproto_class
->aa_mapping_unset(ofproto
, aux
);
1189 ofproto_aa_vlan_get_queued(struct ofproto
*ofproto
,
1190 struct ovs_list
*list
)
1192 if (!ofproto
->ofproto_class
->aa_vlan_get_queued
) {
1195 ofproto
->ofproto_class
->aa_vlan_get_queued(ofproto
, list
);
1200 ofproto_aa_vlan_get_queue_size(struct ofproto
*ofproto
)
1202 if (!ofproto
->ofproto_class
->aa_vlan_get_queue_size
) {
1205 return ofproto
->ofproto_class
->aa_vlan_get_queue_size(ofproto
);
1208 /* Connectivity Fault Management configuration. */
1210 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
1212 ofproto_port_clear_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1214 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1215 if (ofport
&& ofproto
->ofproto_class
->set_cfm
) {
1216 ofproto
->ofproto_class
->set_cfm(ofport
, NULL
);
1220 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
1221 * basic configuration from the configuration members in 'cfm', and the remote
1222 * maintenance point ID from remote_mpid. Ignores the statistics members of
1225 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
1227 ofproto_port_set_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1228 const struct cfm_settings
*s
)
1230 struct ofport
*ofport
;
1233 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1235 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32
,
1236 ofproto
->name
, ofp_port
);
1240 /* XXX: For configuration simplicity, we only support one remote_mpid
1241 * outside of the CFM module. It's not clear if this is the correct long
1242 * term solution or not. */
1243 error
= (ofproto
->ofproto_class
->set_cfm
1244 ? ofproto
->ofproto_class
->set_cfm(ofport
, s
)
1247 VLOG_WARN("%s: CFM configuration on port %"PRIu32
" (%s) failed (%s)",
1248 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1249 ovs_strerror(error
));
1253 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
1254 * 'ofproto' does not have a port 'ofp_port'. */
1256 ofproto_port_set_bfd(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1257 const struct smap
*cfg
)
1259 struct ofport
*ofport
;
1262 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1264 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu32
,
1265 ofproto
->name
, ofp_port
);
1269 error
= (ofproto
->ofproto_class
->set_bfd
1270 ? ofproto
->ofproto_class
->set_bfd(ofport
, cfg
)
1273 VLOG_WARN("%s: bfd configuration on port %"PRIu32
" (%s) failed (%s)",
1274 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1275 ovs_strerror(error
));
1279 /* Checks the status change of BFD on 'ofport'.
1281 * Returns true if 'ofproto_class' does not support 'bfd_status_changed'. */
1283 ofproto_port_bfd_status_changed(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1285 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1286 return (ofport
&& ofproto
->ofproto_class
->bfd_status_changed
1287 ? ofproto
->ofproto_class
->bfd_status_changed(ofport
)
1291 /* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on
1292 * success. Returns a positive errno otherwise. Has no effect if 'ofp_port'
1293 * is not an OpenFlow port in 'ofproto'.
1295 * The caller must provide and own '*status'. */
1297 ofproto_port_get_bfd_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1298 struct smap
*status
)
1300 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1301 return (ofport
&& ofproto
->ofproto_class
->get_bfd_status
1302 ? ofproto
->ofproto_class
->get_bfd_status(ofport
, status
)
1306 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1307 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1308 * 0 if LACP partner information is not current (generally indicating a
1309 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1311 ofproto_port_is_lacp_current(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1313 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1314 return (ofport
&& ofproto
->ofproto_class
->port_is_lacp_current
1315 ? ofproto
->ofproto_class
->port_is_lacp_current(ofport
)
1320 ofproto_port_get_lacp_stats(const struct ofport
*port
, struct lacp_slave_stats
*stats
)
1322 struct ofproto
*ofproto
= port
->ofproto
;
1325 if (ofproto
->ofproto_class
->port_get_lacp_stats
) {
1326 error
= ofproto
->ofproto_class
->port_get_lacp_stats(port
, stats
);
1336 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1337 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1338 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1339 * configuration plus, if there is more than one slave, a bonding
1342 * If 'aux' is already registered then this function updates its configuration
1343 * to 's'. Otherwise, this function registers a new bundle.
1345 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1348 ofproto_bundle_register(struct ofproto
*ofproto
, void *aux
,
1349 const struct ofproto_bundle_settings
*s
)
1351 return (ofproto
->ofproto_class
->bundle_set
1352 ? ofproto
->ofproto_class
->bundle_set(ofproto
, aux
, s
)
1356 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1357 * If no such bundle has been registered, this has no effect. */
1359 ofproto_bundle_unregister(struct ofproto
*ofproto
, void *aux
)
1361 return ofproto_bundle_register(ofproto
, aux
, NULL
);
1365 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1366 * If 'aux' is already registered then this function updates its configuration
1367 * to 's'. Otherwise, this function registers a new mirror. */
1369 ofproto_mirror_register(struct ofproto
*ofproto
, void *aux
,
1370 const struct ofproto_mirror_settings
*s
)
1372 return (ofproto
->ofproto_class
->mirror_set
1373 ? ofproto
->ofproto_class
->mirror_set(ofproto
, aux
, s
)
1377 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1378 * If no mirror has been registered, this has no effect. */
1380 ofproto_mirror_unregister(struct ofproto
*ofproto
, void *aux
)
1382 return ofproto_mirror_register(ofproto
, aux
, NULL
);
1385 /* Retrieves statistics from mirror associated with client data pointer
1386 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1387 * 'bytes', respectively. If a particular counters is not supported,
1388 * the appropriate argument is set to UINT64_MAX.
1391 ofproto_mirror_get_stats(struct ofproto
*ofproto
, void *aux
,
1392 uint64_t *packets
, uint64_t *bytes
)
1394 if (!ofproto
->ofproto_class
->mirror_get_stats
) {
1395 *packets
= *bytes
= UINT64_MAX
;
1399 return ofproto
->ofproto_class
->mirror_get_stats(ofproto
, aux
,
1403 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1404 * which all packets are flooded, instead of using MAC learning. If
1405 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1407 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1410 ofproto_set_flood_vlans(struct ofproto
*ofproto
, unsigned long *flood_vlans
)
1412 return (ofproto
->ofproto_class
->set_flood_vlans
1413 ? ofproto
->ofproto_class
->set_flood_vlans(ofproto
, flood_vlans
)
1417 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1418 * output for a mirror. */
1420 ofproto_is_mirror_output_bundle(const struct ofproto
*ofproto
, void *aux
)
1422 return (ofproto
->ofproto_class
->is_mirror_output_bundle
1423 ? ofproto
->ofproto_class
->is_mirror_output_bundle(ofproto
, aux
)
1427 /* Configuration of OpenFlow tables. */
1429 /* Returns the number of OpenFlow tables in 'ofproto'. */
1431 ofproto_get_n_tables(const struct ofproto
*ofproto
)
1433 return ofproto
->n_tables
;
1436 /* Returns the number of Controller visible OpenFlow tables
1437 * in 'ofproto'. This number will exclude Hidden tables.
1438 * This funtion's return value should be less or equal to that of
1439 * ofproto_get_n_tables() . */
1441 ofproto_get_n_visible_tables(const struct ofproto
*ofproto
)
1443 uint8_t n
= ofproto
->n_tables
;
1445 /* Count only non-hidden tables in the number of tables. (Hidden tables,
1446 * if present, are always at the end.) */
1447 while(n
&& (ofproto
->tables
[n
- 1].flags
& OFTABLE_HIDDEN
)) {
1454 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1455 * settings from 's'. 'table_id' must be in the range 0 through the number of
1456 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1458 * For read-only tables, only the name may be configured. */
1460 ofproto_configure_table(struct ofproto
*ofproto
, int table_id
,
1461 const struct ofproto_table_settings
*s
)
1463 struct oftable
*table
;
1465 ovs_assert(table_id
>= 0 && table_id
< ofproto
->n_tables
);
1466 table
= &ofproto
->tables
[table_id
];
1468 oftable_set_name(table
, s
->name
);
1470 if (table
->flags
& OFTABLE_READONLY
) {
1474 if (classifier_set_prefix_fields(&table
->cls
,
1475 s
->prefix_fields
, s
->n_prefix_fields
)) {
1476 /* XXX: Trigger revalidation. */
1479 ovs_mutex_lock(&ofproto_mutex
);
1480 unsigned int new_eviction
= (s
->enable_eviction
1481 ? table
->eviction
| EVICTION_CLIENT
1482 : table
->eviction
& ~EVICTION_CLIENT
);
1483 oftable_configure_eviction(table
, new_eviction
, s
->groups
, s
->n_groups
);
1484 table
->max_flows
= s
->max_flows
;
1485 evict_rules_from_table(table
);
1486 ovs_mutex_unlock(&ofproto_mutex
);
1490 ofproto_has_snoops(const struct ofproto
*ofproto
)
1492 return connmgr_has_snoops(ofproto
->connmgr
);
1496 ofproto_get_snoops(const struct ofproto
*ofproto
, struct sset
*snoops
)
1498 connmgr_get_snoops(ofproto
->connmgr
, snoops
);
1501 /* Deletes 'rule' from 'ofproto'.
1503 * Within an ofproto implementation, this function allows an ofproto
1504 * implementation to destroy any rules that remain when its ->destruct()
1505 * function is called. This function is not suitable for use elsewhere in an
1506 * ofproto implementation.
1508 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1509 * Cycle" in ofproto-provider.h. */
1511 ofproto_rule_delete(struct ofproto
*ofproto
, struct rule
*rule
)
1512 OVS_EXCLUDED(ofproto_mutex
)
1514 /* This skips the ofmonitor and flow-removed notifications because the
1515 * switch is being deleted and any OpenFlow channels have been or soon will
1517 ovs_mutex_lock(&ofproto_mutex
);
1519 if (rule
->state
== RULE_INSERTED
) {
1520 /* Make sure there is no postponed removal of the rule. */
1521 ovs_assert(cls_rule_visible_in_version(&rule
->cr
, OVS_VERSION_MAX
));
1523 if (!classifier_remove(&rule
->ofproto
->tables
[rule
->table_id
].cls
,
1527 ofproto_rule_remove__(rule
->ofproto
, rule
);
1528 if (ofproto
->ofproto_class
->rule_delete
) {
1529 ofproto
->ofproto_class
->rule_delete(rule
);
1532 /* This may not be the last reference to the rule. */
1533 ofproto_rule_unref(rule
);
1535 ovs_mutex_unlock(&ofproto_mutex
);
1539 ofproto_flush__(struct ofproto
*ofproto
)
1540 OVS_EXCLUDED(ofproto_mutex
)
1542 struct oftable
*table
;
1544 /* This will flush all datapath flows. */
1545 if (ofproto
->ofproto_class
->flush
) {
1546 ofproto
->ofproto_class
->flush(ofproto
);
1549 /* XXX: There is a small race window here, where new datapath flows can be
1550 * created by upcall handlers based on the existing flow table. We can not
1551 * call ofproto class flush while holding 'ofproto_mutex' to prevent this,
1552 * as then we could deadlock on syncing with the handler threads waiting on
1553 * the same mutex. */
1555 ovs_mutex_lock(&ofproto_mutex
);
1556 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1557 struct rule_collection rules
;
1560 if (table
->flags
& OFTABLE_HIDDEN
) {
1564 rule_collection_init(&rules
);
1566 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
1567 rule_collection_add(&rules
, rule
);
1569 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
1571 ofproto_group_delete_all__(ofproto
);
1572 meter_delete_all(ofproto
);
1573 /* XXX: Concurrent handler threads may insert new learned flows based on
1574 * learn actions of the now deleted flows right after we release
1575 * 'ofproto_mutex'. */
1576 ovs_mutex_unlock(&ofproto_mutex
);
1580 ofproto_destroy__(struct ofproto
*ofproto
)
1581 OVS_EXCLUDED(ofproto_mutex
)
1583 struct oftable
*table
;
1585 cmap_destroy(&ofproto
->groups
);
1587 hmap_remove(&all_ofprotos
, &ofproto
->hmap_node
);
1589 free(ofproto
->name
);
1590 free(ofproto
->type
);
1591 free(ofproto
->mfr_desc
);
1592 free(ofproto
->hw_desc
);
1593 free(ofproto
->sw_desc
);
1594 free(ofproto
->serial_desc
);
1595 free(ofproto
->dp_desc
);
1596 hmap_destroy(&ofproto
->ports
);
1597 hmap_destroy(&ofproto
->ofport_usage
);
1598 shash_destroy(&ofproto
->port_by_name
);
1599 simap_destroy(&ofproto
->ofp_requests
);
1601 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1602 oftable_destroy(table
);
1604 free(ofproto
->tables
);
1606 hmap_destroy(&ofproto
->meters
);
1608 ovs_mutex_lock(&ofproto
->vl_mff_map
.mutex
);
1609 mf_vl_mff_map_clear(&ofproto
->vl_mff_map
, true);
1610 ovs_mutex_unlock(&ofproto
->vl_mff_map
.mutex
);
1611 cmap_destroy(&ofproto
->vl_mff_map
.cmap
);
1612 ovs_mutex_destroy(&ofproto
->vl_mff_map
.mutex
);
1613 tun_metadata_free(ovsrcu_get_protected(struct tun_table
*,
1614 &ofproto
->metadata_tab
));
1616 ovs_assert(hindex_is_empty(&ofproto
->cookies
));
1617 hindex_destroy(&ofproto
->cookies
);
1619 ovs_assert(hmap_is_empty(&ofproto
->learned_cookies
));
1620 hmap_destroy(&ofproto
->learned_cookies
);
1622 ofproto
->ofproto_class
->dealloc(ofproto
);
1625 /* Destroying rules is doubly deferred, must have 'ofproto' around for them.
1626 * - 1st we defer the removal of the rules from the classifier
1627 * - 2nd we defer the actual destruction of the rules. */
1629 ofproto_destroy_defer__(struct ofproto
*ofproto
)
1630 OVS_EXCLUDED(ofproto_mutex
)
1632 ovsrcu_postpone(ofproto_destroy__
, ofproto
);
1636 ofproto_destroy(struct ofproto
*p
, bool del
)
1637 OVS_EXCLUDED(ofproto_mutex
)
1639 struct ofport
*ofport
, *next_ofport
;
1640 struct ofport_usage
*usage
;
1647 HMAP_FOR_EACH_SAFE (ofport
, next_ofport
, hmap_node
, &p
->ports
) {
1648 ofport_destroy(ofport
, del
);
1651 HMAP_FOR_EACH_POP (usage
, hmap_node
, &p
->ofport_usage
) {
1655 p
->ofproto_class
->destruct(p
, del
);
1657 /* We should not postpone this because it involves deleting a listening
1658 * socket which we may want to reopen soon. 'connmgr' may be used by other
1659 * threads only if they take the ofproto_mutex and read a non-NULL
1660 * 'ofproto->connmgr'. */
1661 ovs_mutex_lock(&ofproto_mutex
);
1662 connmgr_destroy(p
->connmgr
);
1664 ovs_mutex_unlock(&ofproto_mutex
);
1666 /* Destroying rules is deferred, must have 'ofproto' around for them. */
1667 ovsrcu_postpone(ofproto_destroy_defer__
, p
);
1670 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1671 * kernel datapath, for example, this destroys the datapath in the kernel, and
1672 * with the netdev-based datapath, it tears down the data structures that
1673 * represent the datapath.
1675 * The datapath should not be currently open as an ofproto. */
1677 ofproto_delete(const char *name
, const char *type
)
1679 const struct ofproto_class
*class = ofproto_class_find__(type
);
1680 return (!class ? EAFNOSUPPORT
1681 : !class->del
? EACCES
1682 : class->del(type
, name
));
1686 process_port_change(struct ofproto
*ofproto
, int error
, char *devname
)
1688 if (error
== ENOBUFS
) {
1689 reinit_ports(ofproto
);
1690 } else if (!error
) {
1691 update_port(ofproto
, devname
);
1697 ofproto_type_run(const char *datapath_type
)
1699 const struct ofproto_class
*class;
1702 datapath_type
= ofproto_normalize_type(datapath_type
);
1703 class = ofproto_class_find__(datapath_type
);
1705 error
= class->type_run
? class->type_run(datapath_type
) : 0;
1706 if (error
&& error
!= EAGAIN
) {
1707 VLOG_ERR_RL(&rl
, "%s: type_run failed (%s)",
1708 datapath_type
, ovs_strerror(error
));
1714 ofproto_type_wait(const char *datapath_type
)
1716 const struct ofproto_class
*class;
1718 datapath_type
= ofproto_normalize_type(datapath_type
);
1719 class = ofproto_class_find__(datapath_type
);
1721 if (class->type_wait
) {
1722 class->type_wait(datapath_type
);
1727 ofproto_run(struct ofproto
*p
)
1732 error
= p
->ofproto_class
->run(p
);
1733 if (error
&& error
!= EAGAIN
) {
1734 VLOG_ERR_RL(&rl
, "%s: run failed (%s)", p
->name
, ovs_strerror(error
));
1737 /* Restore the eviction group heap invariant occasionally. */
1738 if (p
->eviction_group_timer
< time_msec()) {
1741 p
->eviction_group_timer
= time_msec() + 1000;
1743 for (i
= 0; i
< p
->n_tables
; i
++) {
1744 struct oftable
*table
= &p
->tables
[i
];
1745 struct eviction_group
*evg
;
1748 if (!table
->eviction
) {
1752 if (table
->n_flows
> 100000) {
1753 static struct vlog_rate_limit count_rl
=
1754 VLOG_RATE_LIMIT_INIT(1, 1);
1755 VLOG_WARN_RL(&count_rl
, "Table %"PRIuSIZE
" has an excessive"
1756 " number of rules: %d", i
, table
->n_flows
);
1759 ovs_mutex_lock(&ofproto_mutex
);
1760 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
1761 if (rule
->idle_timeout
|| rule
->hard_timeout
) {
1762 if (!rule
->eviction_group
) {
1763 eviction_group_add_rule(rule
);
1765 heap_raw_change(&rule
->evg_node
,
1766 rule_eviction_priority(p
, rule
));
1771 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
1772 heap_rebuild(&evg
->rules
);
1774 ovs_mutex_unlock(&ofproto_mutex
);
1778 if (p
->ofproto_class
->port_poll
) {
1781 while ((error
= p
->ofproto_class
->port_poll(p
, &devname
)) != EAGAIN
) {
1782 process_port_change(p
, error
, devname
);
1786 new_seq
= seq_read(connectivity_seq_get());
1787 if (new_seq
!= p
->change_seq
) {
1788 struct sset devnames
;
1789 const char *devname
;
1790 struct ofport
*ofport
;
1792 /* Update OpenFlow port status for any port whose netdev has changed.
1794 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1795 * destroyed, so it's not safe to update ports directly from the
1796 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1797 * need this two-phase approach. */
1798 sset_init(&devnames
);
1799 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1800 uint64_t port_change_seq
;
1802 port_change_seq
= netdev_get_change_seq(ofport
->netdev
);
1803 if (ofport
->change_seq
!= port_change_seq
) {
1804 ofport
->change_seq
= port_change_seq
;
1805 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
1808 SSET_FOR_EACH (devname
, &devnames
) {
1809 update_port(p
, devname
);
1811 sset_destroy(&devnames
);
1813 p
->change_seq
= new_seq
;
1816 connmgr_run(p
->connmgr
, handle_openflow
);
1822 ofproto_wait(struct ofproto
*p
)
1824 p
->ofproto_class
->wait(p
);
1825 if (p
->ofproto_class
->port_poll_wait
) {
1826 p
->ofproto_class
->port_poll_wait(p
);
1828 seq_wait(connectivity_seq_get(), p
->change_seq
);
1829 connmgr_wait(p
->connmgr
);
1833 ofproto_is_alive(const struct ofproto
*p
)
1835 return connmgr_has_controllers(p
->connmgr
);
1838 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1839 * memory_report(). */
1841 ofproto_get_memory_usage(const struct ofproto
*ofproto
, struct simap
*usage
)
1843 const struct oftable
*table
;
1844 unsigned int n_rules
;
1846 simap_increase(usage
, "ports", hmap_count(&ofproto
->ports
));
1849 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1850 n_rules
+= table
->n_flows
;
1852 simap_increase(usage
, "rules", n_rules
);
1854 if (ofproto
->ofproto_class
->get_memory_usage
) {
1855 ofproto
->ofproto_class
->get_memory_usage(ofproto
, usage
);
1858 connmgr_get_memory_usage(ofproto
->connmgr
, usage
);
1862 ofproto_type_get_memory_usage(const char *datapath_type
, struct simap
*usage
)
1864 const struct ofproto_class
*class;
1866 datapath_type
= ofproto_normalize_type(datapath_type
);
1867 class = ofproto_class_find__(datapath_type
);
1869 if (class && class->type_get_memory_usage
) {
1870 class->type_get_memory_usage(datapath_type
, usage
);
1875 ofproto_get_ofproto_controller_info(const struct ofproto
*ofproto
,
1878 connmgr_get_controller_info(ofproto
->connmgr
, info
);
1882 ofproto_free_ofproto_controller_info(struct shash
*info
)
1884 connmgr_free_controller_info(info
);
1887 /* Makes a deep copy of 'old' into 'port'. */
1889 ofproto_port_clone(struct ofproto_port
*port
, const struct ofproto_port
*old
)
1891 port
->name
= xstrdup(old
->name
);
1892 port
->type
= xstrdup(old
->type
);
1893 port
->ofp_port
= old
->ofp_port
;
1896 /* Frees memory allocated to members of 'ofproto_port'.
1898 * Do not call this function on an ofproto_port obtained from
1899 * ofproto_port_dump_next(): that function retains ownership of the data in the
1902 ofproto_port_destroy(struct ofproto_port
*ofproto_port
)
1904 free(ofproto_port
->name
);
1905 free(ofproto_port
->type
);
1908 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1910 * This function provides no status indication. An error status for the entire
1911 * dump operation is provided when it is completed by calling
1912 * ofproto_port_dump_done().
1915 ofproto_port_dump_start(struct ofproto_port_dump
*dump
,
1916 const struct ofproto
*ofproto
)
1918 dump
->ofproto
= ofproto
;
1919 dump
->error
= ofproto
->ofproto_class
->port_dump_start(ofproto
,
1923 /* Attempts to retrieve another port from 'dump', which must have been created
1924 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1925 * 'port' and returns true. On failure, returns false.
1927 * Failure might indicate an actual error or merely that the last port has been
1928 * dumped. An error status for the entire dump operation is provided when it
1929 * is completed by calling ofproto_port_dump_done().
1931 * The ofproto owns the data stored in 'port'. It will remain valid until at
1932 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1933 * ofproto_port_dump_done(). */
1935 ofproto_port_dump_next(struct ofproto_port_dump
*dump
,
1936 struct ofproto_port
*port
)
1938 const struct ofproto
*ofproto
= dump
->ofproto
;
1944 dump
->error
= ofproto
->ofproto_class
->port_dump_next(ofproto
, dump
->state
,
1947 ofproto
->ofproto_class
->port_dump_done(ofproto
, dump
->state
);
1953 /* Completes port table dump operation 'dump', which must have been created
1954 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1955 * error-free, otherwise a positive errno value describing the problem. */
1957 ofproto_port_dump_done(struct ofproto_port_dump
*dump
)
1959 const struct ofproto
*ofproto
= dump
->ofproto
;
1961 dump
->error
= ofproto
->ofproto_class
->port_dump_done(ofproto
,
1964 return dump
->error
== EOF
? 0 : dump
->error
;
1967 /* Returns the type to pass to netdev_open() when a datapath of type
1968 * 'datapath_type' has a port of type 'port_type', for a few special
1969 * cases when a netdev type differs from a port type. For example, when
1970 * using the userspace datapath, a port of type "internal" needs to be
1973 * Returns either 'type' itself or a string literal, which must not be
1976 ofproto_port_open_type(const char *datapath_type
, const char *port_type
)
1978 const struct ofproto_class
*class;
1980 datapath_type
= ofproto_normalize_type(datapath_type
);
1981 class = ofproto_class_find__(datapath_type
);
1986 return (class->port_open_type
1987 ? class->port_open_type(datapath_type
, port_type
)
1991 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1992 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1993 * the port's OpenFlow port number.
1995 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1996 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1997 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1998 * 'ofp_portp' is non-null). */
2000 ofproto_port_add(struct ofproto
*ofproto
, struct netdev
*netdev
,
2001 ofp_port_t
*ofp_portp
)
2003 ofp_port_t ofp_port
= ofp_portp
? *ofp_portp
: OFPP_NONE
;
2006 error
= ofproto
->ofproto_class
->port_add(ofproto
, netdev
);
2008 const char *netdev_name
= netdev_get_name(netdev
);
2010 simap_put(&ofproto
->ofp_requests
, netdev_name
,
2011 ofp_to_u16(ofp_port
));
2012 error
= update_port(ofproto
, netdev_name
);
2015 *ofp_portp
= OFPP_NONE
;
2017 struct ofproto_port ofproto_port
;
2019 error
= ofproto_port_query_by_name(ofproto
,
2020 netdev_get_name(netdev
),
2023 *ofp_portp
= ofproto_port
.ofp_port
;
2024 ofproto_port_destroy(&ofproto_port
);
2031 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
2032 * initializes '*port' appropriately; on failure, returns a positive errno
2035 * The caller owns the data in 'ofproto_port' and must free it with
2036 * ofproto_port_destroy() when it is no longer needed. */
2038 ofproto_port_query_by_name(const struct ofproto
*ofproto
, const char *devname
,
2039 struct ofproto_port
*port
)
2043 error
= ofproto
->ofproto_class
->port_query_by_name(ofproto
, devname
, port
);
2045 memset(port
, 0, sizeof *port
);
2050 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
2051 * Returns 0 if successful, otherwise a positive errno. */
2053 ofproto_port_del(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2055 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
2056 const char *name
= ofport
? netdev_get_name(ofport
->netdev
) : "<unknown>";
2057 struct simap_node
*ofp_request_node
;
2060 ofp_request_node
= simap_find(&ofproto
->ofp_requests
, name
);
2061 if (ofp_request_node
) {
2062 simap_delete(&ofproto
->ofp_requests
, ofp_request_node
);
2065 error
= ofproto
->ofproto_class
->port_del(ofproto
, ofp_port
);
2066 if (!error
&& ofport
) {
2067 /* 'name' is the netdev's name and update_port() is going to close the
2068 * netdev. Just in case update_port() refers to 'name' after it
2069 * destroys 'ofport', make a copy of it around the update_port()
2071 char *devname
= xstrdup(name
);
2072 update_port(ofproto
, devname
);
2078 /* Refreshes datapath configuration of port number 'ofp_port' in 'ofproto'.
2080 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
2082 ofproto_port_set_config(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
2083 const struct smap
*cfg
)
2085 struct ofport
*ofport
;
2088 ofport
= ofproto_get_port(ofproto
, ofp_port
);
2090 VLOG_WARN("%s: cannot configure datapath on nonexistent port %"PRIu32
,
2091 ofproto
->name
, ofp_port
);
2095 error
= (ofproto
->ofproto_class
->port_set_config
2096 ? ofproto
->ofproto_class
->port_set_config(ofport
, cfg
)
2099 VLOG_WARN("%s: datapath configuration on port %"PRIu32
2100 " (%s) failed (%s)",
2101 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
2102 ovs_strerror(error
));
2108 flow_mod_init(struct ofputil_flow_mod
*fm
,
2109 const struct match
*match
, int priority
,
2110 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2111 enum ofp_flow_mod_command command
)
2113 *fm
= (struct ofputil_flow_mod
) {
2115 .priority
= priority
,
2118 .buffer_id
= UINT32_MAX
,
2119 .out_port
= OFPP_ANY
,
2120 .out_group
= OFPG_ANY
,
2121 .ofpacts
= CONST_CAST(struct ofpact
*, ofpacts
),
2122 .ofpacts_len
= ofpacts_len
,
2127 simple_flow_mod(struct ofproto
*ofproto
,
2128 const struct match
*match
, int priority
,
2129 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2130 enum ofp_flow_mod_command command
)
2132 struct ofputil_flow_mod fm
;
2134 flow_mod_init(&fm
, match
, priority
, ofpacts
, ofpacts_len
, command
);
2136 return handle_flow_mod__(ofproto
, &fm
, NULL
);
2139 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
2140 * performs the 'n_actions' actions in 'actions'. The new flow will not
2143 * If cls_rule->priority is in the range of priorities supported by OpenFlow
2144 * (0...65535, inclusive) then the flow will be visible to OpenFlow
2145 * controllers; otherwise, it will be hidden.
2147 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
2149 * This is a helper function for in-band control and fail-open. */
2151 ofproto_add_flow(struct ofproto
*ofproto
, const struct match
*match
,
2153 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
2154 OVS_EXCLUDED(ofproto_mutex
)
2156 const struct rule
*rule
;
2159 /* First do a cheap check whether the rule we're looking for already exists
2160 * with the actions that we want. If it does, then we're done. */
2161 rule
= rule_from_cls_rule(classifier_find_match_exactly(
2162 &ofproto
->tables
[0].cls
, match
, priority
,
2165 const struct rule_actions
*actions
= rule_get_actions(rule
);
2166 must_add
= !ofpacts_equal(actions
->ofpacts
, actions
->ofpacts_len
,
2167 ofpacts
, ofpacts_len
);
2172 /* If there's no such rule or the rule doesn't have the actions we want,
2173 * fall back to a executing a full flow mod. We can't optimize this at
2174 * all because we didn't take enough locks above to ensure that the flow
2175 * table didn't already change beneath us. */
2177 simple_flow_mod(ofproto
, match
, priority
, ofpacts
, ofpacts_len
,
2178 OFPFC_MODIFY_STRICT
);
2182 /* Executes the flow modification specified in 'fm'. Returns 0 on success, or
2183 * an OFPERR_* OpenFlow error code on failure.
2185 * This is a helper function for in-band control and fail-open. */
2187 ofproto_flow_mod(struct ofproto
*ofproto
, const struct ofputil_flow_mod
*fm
)
2188 OVS_EXCLUDED(ofproto_mutex
)
2190 return handle_flow_mod__(ofproto
, fm
, NULL
);
2193 /* Searches for a rule with matching criteria exactly equal to 'target' in
2194 * ofproto's table 0 and, if it finds one, deletes it.
2196 * This is a helper function for in-band control and fail-open. */
2198 ofproto_delete_flow(struct ofproto
*ofproto
,
2199 const struct match
*target
, int priority
)
2200 OVS_REQUIRES(ofproto_mutex
)
2202 struct classifier
*cls
= &ofproto
->tables
[0].cls
;
2205 /* First do a cheap check whether the rule we're looking for has already
2206 * been deleted. If so, then we're done. */
2207 rule
= rule_from_cls_rule(classifier_find_match_exactly(
2208 cls
, target
, priority
, OVS_VERSION_MAX
));
2213 struct rule_collection rules
;
2215 rule_collection_init(&rules
);
2216 rule_collection_add(&rules
, rule
);
2217 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
2218 rule_collection_destroy(&rules
);
2221 /* Delete all of the flows from all of ofproto's flow tables, then reintroduce
2222 * the flows required by in-band control and fail-open. */
2224 ofproto_flush_flows(struct ofproto
*ofproto
)
2226 COVERAGE_INC(ofproto_flush
);
2227 ofproto_flush__(ofproto
);
2228 connmgr_flushed(ofproto
->connmgr
);
2232 reinit_ports(struct ofproto
*p
)
2234 struct ofproto_port_dump dump
;
2235 struct sset devnames
;
2236 struct ofport
*ofport
;
2237 struct ofproto_port ofproto_port
;
2238 const char *devname
;
2240 COVERAGE_INC(ofproto_reinit_ports
);
2242 sset_init(&devnames
);
2243 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2244 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
2246 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2247 sset_add(&devnames
, ofproto_port
.name
);
2250 SSET_FOR_EACH (devname
, &devnames
) {
2251 update_port(p
, devname
);
2253 sset_destroy(&devnames
);
2257 alloc_ofp_port(struct ofproto
*ofproto
, const char *netdev_name
)
2261 port_idx
= simap_get(&ofproto
->ofp_requests
, netdev_name
);
2262 port_idx
= port_idx
? port_idx
: UINT16_MAX
;
2264 if (port_idx
>= ofproto
->max_ports
2265 || ofport_get_usage(ofproto
, u16_to_ofp(port_idx
)) == LLONG_MAX
) {
2266 uint16_t lru_ofport
= 0, end_port_no
= ofproto
->alloc_port_no
;
2267 long long int last_used_at
, lru
= LLONG_MAX
;
2269 /* Search for a free OpenFlow port number. We try not to
2270 * immediately reuse them to prevent problems due to old
2273 * We limit the automatically assigned port numbers to the lower half
2274 * of the port range, to reserve the upper half for assignment by
2277 if (++ofproto
->alloc_port_no
>= MIN(ofproto
->max_ports
, 32768)) {
2278 ofproto
->alloc_port_no
= 1;
2280 last_used_at
= ofport_get_usage(ofproto
,
2281 u16_to_ofp(ofproto
->alloc_port_no
));
2282 if (!last_used_at
) {
2283 port_idx
= ofproto
->alloc_port_no
;
2285 } else if ( last_used_at
< time_msec() - 60*60*1000) {
2286 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2287 * more than an hour ago, consider it usable. */
2288 ofport_remove_usage(ofproto
,
2289 u16_to_ofp(ofproto
->alloc_port_no
));
2290 port_idx
= ofproto
->alloc_port_no
;
2292 } else if (last_used_at
< lru
) {
2294 lru_ofport
= ofproto
->alloc_port_no
;
2297 if (ofproto
->alloc_port_no
== end_port_no
) {
2299 port_idx
= lru_ofport
;
2306 ofport_set_usage(ofproto
, u16_to_ofp(port_idx
), LLONG_MAX
);
2307 return u16_to_ofp(port_idx
);
2311 dealloc_ofp_port(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2313 if (ofp_to_u16(ofp_port
) < ofproto
->max_ports
) {
2314 ofport_set_usage(ofproto
, ofp_port
, time_msec());
2318 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2319 * pointer if the netdev cannot be opened. On success, also fills in
2321 static struct netdev
*
2322 ofport_open(struct ofproto
*ofproto
,
2323 struct ofproto_port
*ofproto_port
,
2324 struct ofputil_phy_port
*pp
)
2326 enum netdev_flags flags
;
2327 struct netdev
*netdev
;
2330 error
= netdev_open(ofproto_port
->name
, ofproto_port
->type
, &netdev
);
2332 VLOG_WARN_RL(&rl
, "%s: ignoring port %s (%"PRIu32
") because netdev %s "
2333 "cannot be opened (%s)",
2335 ofproto_port
->name
, ofproto_port
->ofp_port
,
2336 ofproto_port
->name
, ovs_strerror(error
));
2340 if (ofproto_port
->ofp_port
== OFPP_NONE
) {
2341 if (!strcmp(ofproto
->name
, ofproto_port
->name
)) {
2342 ofproto_port
->ofp_port
= OFPP_LOCAL
;
2344 ofproto_port
->ofp_port
= alloc_ofp_port(ofproto
,
2345 ofproto_port
->name
);
2348 pp
->port_no
= ofproto_port
->ofp_port
;
2349 netdev_get_etheraddr(netdev
, &pp
->hw_addr
);
2350 pp
->hw_addr64
= eth_addr64_zero
;
2351 ovs_strlcpy(pp
->name
, ofproto_port
->name
, sizeof pp
->name
);
2352 netdev_get_flags(netdev
, &flags
);
2353 pp
->config
= flags
& NETDEV_UP
? 0 : OFPUTIL_PC_PORT_DOWN
;
2354 pp
->state
= netdev_get_carrier(netdev
) ? 0 : OFPUTIL_PS_LINK_DOWN
;
2355 netdev_get_features(netdev
, &pp
->curr
, &pp
->advertised
,
2356 &pp
->supported
, &pp
->peer
);
2357 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
2358 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
2363 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2364 * port number, and 'config' bits other than OFPUTIL_PC_PORT_DOWN are
2367 ofport_equal(const struct ofputil_phy_port
*a
,
2368 const struct ofputil_phy_port
*b
)
2370 return (eth_addr_equals(a
->hw_addr
, b
->hw_addr
)
2371 && eth_addr64_equals(a
->hw_addr64
, b
->hw_addr64
)
2372 && a
->state
== b
->state
2373 && !((a
->config
^ b
->config
) & OFPUTIL_PC_PORT_DOWN
)
2374 && a
->curr
== b
->curr
2375 && a
->advertised
== b
->advertised
2376 && a
->supported
== b
->supported
2377 && a
->peer
== b
->peer
2378 && a
->curr_speed
== b
->curr_speed
2379 && a
->max_speed
== b
->max_speed
);
2382 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2383 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2384 * one with the same name or port number). */
2386 ofport_install(struct ofproto
*p
,
2387 struct netdev
*netdev
, const struct ofputil_phy_port
*pp
)
2389 const char *netdev_name
= netdev_get_name(netdev
);
2390 struct ofport
*ofport
;
2393 /* Create ofport. */
2394 ofport
= p
->ofproto_class
->port_alloc();
2399 ofport
->ofproto
= p
;
2400 ofport
->netdev
= netdev
;
2401 ofport
->change_seq
= netdev_get_change_seq(netdev
);
2403 ofport
->ofp_port
= pp
->port_no
;
2404 ofport
->created
= time_msec();
2406 /* Add port to 'p'. */
2407 hmap_insert(&p
->ports
, &ofport
->hmap_node
,
2408 hash_ofp_port(ofport
->ofp_port
));
2409 shash_add(&p
->port_by_name
, netdev_name
, ofport
);
2411 update_mtu(p
, ofport
);
2413 /* Let the ofproto_class initialize its private data. */
2414 error
= p
->ofproto_class
->port_construct(ofport
);
2418 connmgr_send_port_status(p
->connmgr
, NULL
, pp
, OFPPR_ADD
);
2422 VLOG_WARN_RL(&rl
, "%s: could not add port %s (%s)",
2423 p
->name
, netdev_name
, ovs_strerror(error
));
2425 ofport_destroy__(ofport
);
2427 netdev_close(netdev
);
2432 /* Removes 'ofport' from 'p' and destroys it. */
2434 ofport_remove(struct ofport
*ofport
)
2436 struct ofproto
*p
= ofport
->ofproto
;
2437 bool is_mtu_overridden
= ofport_is_mtu_overridden(p
, ofport
);
2439 connmgr_send_port_status(ofport
->ofproto
->connmgr
, NULL
, &ofport
->pp
,
2441 ofport_destroy(ofport
, true);
2442 if (!is_mtu_overridden
) {
2443 update_mtu_ofproto(p
);
2447 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2450 ofport_remove_with_name(struct ofproto
*ofproto
, const char *name
)
2452 struct ofport
*port
= shash_find_data(&ofproto
->port_by_name
, name
);
2454 ofport_remove(port
);
2458 /* Updates 'port' with new 'pp' description.
2460 * Does not handle a name or port number change. The caller must implement
2461 * such a change as a delete followed by an add. */
2463 ofport_modified(struct ofport
*port
, struct ofputil_phy_port
*pp
)
2465 port
->pp
.hw_addr
= pp
->hw_addr
;
2466 port
->pp
.hw_addr64
= pp
->hw_addr64
;
2467 port
->pp
.config
= ((port
->pp
.config
& ~OFPUTIL_PC_PORT_DOWN
)
2468 | (pp
->config
& OFPUTIL_PC_PORT_DOWN
));
2469 port
->pp
.state
= ((port
->pp
.state
& ~OFPUTIL_PS_LINK_DOWN
)
2470 | (pp
->state
& OFPUTIL_PS_LINK_DOWN
));
2471 port
->pp
.curr
= pp
->curr
;
2472 port
->pp
.advertised
= pp
->advertised
;
2473 port
->pp
.supported
= pp
->supported
;
2474 port
->pp
.peer
= pp
->peer
;
2475 port
->pp
.curr_speed
= pp
->curr_speed
;
2476 port
->pp
.max_speed
= pp
->max_speed
;
2479 /* Update OpenFlow 'state' in 'port' and notify controller. */
2481 ofproto_port_set_state(struct ofport
*port
, enum ofputil_port_state state
)
2483 if (port
->pp
.state
!= state
) {
2484 port
->pp
.state
= state
;
2485 connmgr_send_port_status(port
->ofproto
->connmgr
, NULL
,
2486 &port
->pp
, OFPPR_MODIFY
);
2491 ofproto_port_unregister(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2493 struct ofport
*port
= ofproto_get_port(ofproto
, ofp_port
);
2495 if (port
->ofproto
->ofproto_class
->set_stp_port
) {
2496 port
->ofproto
->ofproto_class
->set_stp_port(port
, NULL
);
2498 if (port
->ofproto
->ofproto_class
->set_rstp_port
) {
2499 port
->ofproto
->ofproto_class
->set_rstp_port(port
, NULL
);
2501 if (port
->ofproto
->ofproto_class
->set_cfm
) {
2502 port
->ofproto
->ofproto_class
->set_cfm(port
, NULL
);
2504 if (port
->ofproto
->ofproto_class
->bundle_remove
) {
2505 port
->ofproto
->ofproto_class
->bundle_remove(port
);
2511 ofport_destroy__(struct ofport
*port
)
2513 struct ofproto
*ofproto
= port
->ofproto
;
2514 const char *name
= netdev_get_name(port
->netdev
);
2516 hmap_remove(&ofproto
->ports
, &port
->hmap_node
);
2517 shash_find_and_delete(&ofproto
->port_by_name
, name
);
2519 netdev_close(port
->netdev
);
2520 ofproto
->ofproto_class
->port_dealloc(port
);
2524 ofport_destroy(struct ofport
*port
, bool del
)
2527 dealloc_ofp_port(port
->ofproto
, port
->ofp_port
);
2528 port
->ofproto
->ofproto_class
->port_destruct(port
, del
);
2529 ofport_destroy__(port
);
2534 ofproto_get_port(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2536 struct ofport
*port
;
2538 HMAP_FOR_EACH_IN_BUCKET (port
, hmap_node
, hash_ofp_port(ofp_port
),
2540 if (port
->ofp_port
== ofp_port
) {
2547 static long long int
2548 ofport_get_usage(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2550 struct ofport_usage
*usage
;
2552 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2553 &ofproto
->ofport_usage
) {
2554 if (usage
->ofp_port
== ofp_port
) {
2555 return usage
->last_used
;
2562 ofport_set_usage(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
2563 long long int last_used
)
2565 struct ofport_usage
*usage
;
2566 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2567 &ofproto
->ofport_usage
) {
2568 if (usage
->ofp_port
== ofp_port
) {
2569 usage
->last_used
= last_used
;
2573 ovs_assert(last_used
== LLONG_MAX
);
2575 usage
= xmalloc(sizeof *usage
);
2576 usage
->ofp_port
= ofp_port
;
2577 usage
->last_used
= last_used
;
2578 hmap_insert(&ofproto
->ofport_usage
, &usage
->hmap_node
,
2579 hash_ofp_port(ofp_port
));
2583 ofport_remove_usage(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2585 struct ofport_usage
*usage
;
2586 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2587 &ofproto
->ofport_usage
) {
2588 if (usage
->ofp_port
== ofp_port
) {
2589 hmap_remove(&ofproto
->ofport_usage
, &usage
->hmap_node
);
2597 ofproto_port_get_stats(const struct ofport
*port
, struct netdev_stats
*stats
)
2599 struct ofproto
*ofproto
= port
->ofproto
;
2602 if (ofproto
->ofproto_class
->port_get_stats
) {
2603 error
= ofproto
->ofproto_class
->port_get_stats(port
, stats
);
2612 update_port(struct ofproto
*ofproto
, const char *name
)
2614 struct ofproto_port ofproto_port
;
2615 struct ofputil_phy_port pp
;
2616 struct netdev
*netdev
;
2617 struct ofport
*port
;
2620 COVERAGE_INC(ofproto_update_port
);
2622 /* Fetch 'name''s location and properties from the datapath. */
2623 netdev
= (!ofproto_port_query_by_name(ofproto
, name
, &ofproto_port
)
2624 ? ofport_open(ofproto
, &ofproto_port
, &pp
)
2628 port
= ofproto_get_port(ofproto
, ofproto_port
.ofp_port
);
2629 if (port
&& !strcmp(netdev_get_name(port
->netdev
), name
)) {
2630 struct netdev
*old_netdev
= port
->netdev
;
2632 /* 'name' hasn't changed location. Any properties changed? */
2633 bool port_changed
= !ofport_equal(&port
->pp
, &pp
);
2635 ofport_modified(port
, &pp
);
2638 update_mtu(ofproto
, port
);
2640 /* Install the newly opened netdev in case it has changed.
2641 * Don't close the old netdev yet in case port_modified has to
2642 * remove a retained reference to it.*/
2643 port
->netdev
= netdev
;
2644 port
->change_seq
= netdev_get_change_seq(netdev
);
2646 if (port
->ofproto
->ofproto_class
->port_modified
) {
2647 port
->ofproto
->ofproto_class
->port_modified(port
);
2650 /* Send status update, if any port property changed */
2652 connmgr_send_port_status(port
->ofproto
->connmgr
, NULL
,
2653 &port
->pp
, OFPPR_MODIFY
);
2656 netdev_close(old_netdev
);
2658 /* If 'port' is nonnull then its name differs from 'name' and thus
2659 * we should delete it. If we think there's a port named 'name'
2660 * then its port number must be wrong now so delete it too. */
2662 ofport_remove(port
);
2664 ofport_remove_with_name(ofproto
, name
);
2665 error
= ofport_install(ofproto
, netdev
, &pp
);
2668 /* Any port named 'name' is gone now. */
2669 ofport_remove_with_name(ofproto
, name
);
2671 ofproto_port_destroy(&ofproto_port
);
2677 init_ports(struct ofproto
*p
)
2679 struct ofproto_port_dump dump
;
2680 struct ofproto_port ofproto_port
;
2681 struct shash_node
*node
, *next
;
2683 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2684 const char *name
= ofproto_port
.name
;
2686 if (shash_find(&p
->port_by_name
, name
)) {
2687 VLOG_WARN_RL(&rl
, "%s: ignoring duplicate device %s in datapath",
2690 struct ofputil_phy_port pp
;
2691 struct netdev
*netdev
;
2693 /* Check if an OpenFlow port number had been requested. */
2694 node
= shash_find(&init_ofp_ports
, name
);
2696 const struct iface_hint
*iface_hint
= node
->data
;
2697 simap_put(&p
->ofp_requests
, name
,
2698 ofp_to_u16(iface_hint
->ofp_port
));
2701 netdev
= ofport_open(p
, &ofproto_port
, &pp
);
2703 ofport_install(p
, netdev
, &pp
);
2704 if (ofp_to_u16(ofproto_port
.ofp_port
) < p
->max_ports
) {
2705 p
->alloc_port_no
= MAX(p
->alloc_port_no
,
2706 ofp_to_u16(ofproto_port
.ofp_port
));
2712 SHASH_FOR_EACH_SAFE(node
, next
, &init_ofp_ports
) {
2713 struct iface_hint
*iface_hint
= node
->data
;
2715 if (!strcmp(iface_hint
->br_name
, p
->name
)) {
2716 free(iface_hint
->br_name
);
2717 free(iface_hint
->br_type
);
2719 shash_delete(&init_ofp_ports
, node
);
2727 ofport_is_internal_or_patch(const struct ofproto
*p
, const struct ofport
*port
)
2729 return !strcmp(netdev_get_type(port
->netdev
),
2730 ofproto_port_open_type(p
->type
, "internal")) ||
2731 !strcmp(netdev_get_type(port
->netdev
),
2732 ofproto_port_open_type(p
->type
, "patch"));
2735 /* If 'port' is internal or patch and if the user didn't explicitly specify an
2736 * mtu through the database, we have to override it. */
2738 ofport_is_mtu_overridden(const struct ofproto
*p
, const struct ofport
*port
)
2740 return ofport_is_internal_or_patch(p
, port
)
2741 && !netdev_mtu_is_user_config(port
->netdev
);
2744 /* Find the minimum MTU of all non-overridden devices attached to 'p'.
2745 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2747 find_min_mtu(struct ofproto
*p
)
2749 struct ofport
*ofport
;
2752 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2753 struct netdev
*netdev
= ofport
->netdev
;
2756 /* Skip any overridden port, since that's what we're trying to set. */
2757 if (ofport_is_mtu_overridden(p
, ofport
)) {
2761 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2764 if (!mtu
|| dev_mtu
< mtu
) {
2769 return mtu
? mtu
: ETH_PAYLOAD_MAX
;
2772 /* Update MTU of all overridden devices on 'p' to the minimum of the
2773 * non-overridden ports in event of 'port' added or changed. */
2775 update_mtu(struct ofproto
*p
, struct ofport
*port
)
2777 struct netdev
*netdev
= port
->netdev
;
2780 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2784 if (ofport_is_mtu_overridden(p
, port
)) {
2785 if (dev_mtu
> p
->min_mtu
) {
2786 if (!netdev_set_mtu(port
->netdev
, p
->min_mtu
)) {
2787 dev_mtu
= p
->min_mtu
;
2790 port
->mtu
= dev_mtu
;
2794 port
->mtu
= dev_mtu
;
2795 /* For non-overridden port find new min mtu. */
2797 update_mtu_ofproto(p
);
2801 update_mtu_ofproto(struct ofproto
*p
)
2803 struct ofport
*ofport
;
2804 int old_min
= p
->min_mtu
;
2806 p
->min_mtu
= find_min_mtu(p
);
2807 if (p
->min_mtu
== old_min
) {
2811 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2812 struct netdev
*netdev
= ofport
->netdev
;
2814 if (ofport_is_mtu_overridden(p
, ofport
)) {
2815 if (!netdev_set_mtu(netdev
, p
->min_mtu
)) {
2816 ofport
->mtu
= p
->min_mtu
;
2823 ofproto_rule_destroy__(struct rule
*rule
)
2824 OVS_NO_THREAD_SAFETY_ANALYSIS
2826 cls_rule_destroy(CONST_CAST(struct cls_rule
*, &rule
->cr
));
2827 rule_actions_destroy(rule_get_actions(rule
));
2828 ovs_mutex_destroy(&rule
->mutex
);
2829 rule
->ofproto
->ofproto_class
->rule_dealloc(rule
);
2833 rule_destroy_cb(struct rule
*rule
)
2834 OVS_NO_THREAD_SAFETY_ANALYSIS
2836 /* Send rule removed if needed. */
2837 if (rule
->flags
& OFPUTIL_FF_SEND_FLOW_REM
2838 && rule
->removed_reason
!= OVS_OFPRR_NONE
2839 && !rule_is_hidden(rule
)) {
2840 ofproto_rule_send_removed(rule
);
2842 rule
->ofproto
->ofproto_class
->rule_destruct(rule
);
2843 mf_vl_mff_unref(&rule
->ofproto
->vl_mff_map
, rule
->match_tlv_bitmap
);
2844 mf_vl_mff_unref(&rule
->ofproto
->vl_mff_map
, rule
->ofpacts_tlv_bitmap
);
2845 ofproto_rule_destroy__(rule
);
2849 ofproto_rule_ref(struct rule
*rule
)
2852 ovs_refcount_ref(&rule
->ref_count
);
2857 ofproto_rule_try_ref(struct rule
*rule
)
2860 return ovs_refcount_try_ref_rcu(&rule
->ref_count
);
2865 /* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the
2866 * ref_count reaches 0.
2868 * Use of RCU allows short term use (between RCU quiescent periods) without
2869 * keeping a reference. A reference must be taken if the rule needs to
2870 * stay around accross the RCU quiescent periods. */
2872 ofproto_rule_unref(struct rule
*rule
)
2874 if (rule
&& ovs_refcount_unref_relaxed(&rule
->ref_count
) == 1) {
2875 ovs_assert(rule
->state
!= RULE_INSERTED
);
2876 ovsrcu_postpone(rule_destroy_cb
, rule
);
2881 remove_rule_rcu__(struct rule
*rule
)
2882 OVS_REQUIRES(ofproto_mutex
)
2884 struct ofproto
*ofproto
= rule
->ofproto
;
2885 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
2887 ovs_assert(!cls_rule_visible_in_version(&rule
->cr
, OVS_VERSION_MAX
));
2888 if (!classifier_remove(&table
->cls
, &rule
->cr
)) {
2891 if (ofproto
->ofproto_class
->rule_delete
) {
2892 ofproto
->ofproto_class
->rule_delete(rule
);
2894 ofproto_rule_unref(rule
);
2898 remove_rule_rcu(struct rule
*rule
)
2899 OVS_EXCLUDED(ofproto_mutex
)
2901 ovs_mutex_lock(&ofproto_mutex
);
2902 remove_rule_rcu__(rule
);
2903 ovs_mutex_unlock(&ofproto_mutex
);
2906 /* Removes and deletes rules from a NULL-terminated array of rule pointers. */
2908 remove_rules_rcu(struct rule
**rules
)
2909 OVS_EXCLUDED(ofproto_mutex
)
2911 struct rule
**orig_rules
= rules
;
2914 struct ofproto
*ofproto
= rules
[0]->ofproto
;
2915 unsigned long tables
[BITMAP_N_LONGS(256)];
2919 memset(tables
, 0, sizeof tables
);
2921 ovs_mutex_lock(&ofproto_mutex
);
2922 while ((rule
= *rules
++)) {
2923 /* Defer once for each new table. This defers the subtable cleanup
2924 * until later, so that when removing large number of flows the
2925 * operation is faster. */
2926 if (!bitmap_is_set(tables
, rule
->table_id
)) {
2927 struct classifier
*cls
= &ofproto
->tables
[rule
->table_id
].cls
;
2929 bitmap_set1(tables
, rule
->table_id
);
2930 classifier_defer(cls
);
2932 remove_rule_rcu__(rule
);
2935 BITMAP_FOR_EACH_1(table_id
, 256, tables
) {
2936 struct classifier
*cls
= &ofproto
->tables
[table_id
].cls
;
2938 classifier_publish(cls
);
2940 ovs_mutex_unlock(&ofproto_mutex
);
2947 ofproto_group_ref(struct ofgroup
*group
)
2950 ovs_refcount_ref(&group
->ref_count
);
2955 ofproto_group_try_ref(struct ofgroup
*group
)
2958 return ovs_refcount_try_ref_rcu(&group
->ref_count
);
2964 group_destroy_cb(struct ofgroup
*group
)
2966 group
->ofproto
->ofproto_class
->group_destruct(group
);
2967 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props
*,
2969 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list
*,
2971 group
->ofproto
->ofproto_class
->group_dealloc(group
);
2975 ofproto_group_unref(struct ofgroup
*group
)
2976 OVS_NO_THREAD_SAFETY_ANALYSIS
2978 if (group
&& ovs_refcount_unref_relaxed(&group
->ref_count
) == 1) {
2979 ovs_assert(rule_collection_n(&group
->rules
) == 0);
2980 ovsrcu_postpone(group_destroy_cb
, group
);
2985 remove_group_rcu__(struct ofgroup
*group
)
2986 OVS_REQUIRES(ofproto_mutex
)
2988 struct ofproto
*ofproto
= group
->ofproto
;
2990 ovs_assert(!versions_visible_in_version(&group
->versions
, OVS_VERSION_MAX
));
2992 cmap_remove(&ofproto
->groups
, &group
->cmap_node
,
2993 hash_int(group
->group_id
, 0));
2994 ofproto_group_unref(group
);
2998 remove_group_rcu(struct ofgroup
*group
)
2999 OVS_EXCLUDED(ofproto_mutex
)
3001 ovs_mutex_lock(&ofproto_mutex
);
3002 remove_group_rcu__(group
);
3003 ovs_mutex_unlock(&ofproto_mutex
);
3006 /* Removes and deletes groups from a NULL-terminated array of group
3009 remove_groups_rcu(struct ofgroup
**groups
)
3010 OVS_EXCLUDED(ofproto_mutex
)
3012 ovs_mutex_lock(&ofproto_mutex
);
3013 for (struct ofgroup
**g
= groups
; *g
; g
++) {
3014 remove_group_rcu__(*g
);
3016 ovs_mutex_unlock(&ofproto_mutex
);
3020 static bool ofproto_fix_meter_action(const struct ofproto
*,
3021 struct ofpact_meter
*);
3023 /* Creates and returns a new 'struct rule_actions', whose actions are a copy
3024 * of from the 'ofpacts_len' bytes of 'ofpacts'. */
3025 const struct rule_actions
*
3026 rule_actions_create(const struct ofpact
*ofpacts
, size_t ofpacts_len
)
3028 struct rule_actions
*actions
;
3030 actions
= xmalloc(sizeof *actions
+ ofpacts_len
);
3031 actions
->ofpacts_len
= ofpacts_len
;
3032 memcpy(actions
->ofpacts
, ofpacts
, ofpacts_len
);
3033 actions
->has_meter
= ofpacts_get_meter(ofpacts
, ofpacts_len
) != 0;
3034 actions
->has_groups
=
3035 (ofpact_find_type_flattened(ofpacts
, OFPACT_GROUP
,
3036 ofpact_end(ofpacts
, ofpacts_len
))
3038 actions
->has_learn_with_delete
= (next_learn_with_delete(actions
, NULL
)
3044 /* Free the actions after the RCU quiescent period is reached. */
3046 rule_actions_destroy(const struct rule_actions
*actions
)
3049 ovsrcu_postpone(free
, CONST_CAST(struct rule_actions
*, actions
));
3053 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
3054 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
3056 ofproto_rule_has_out_port(const struct rule
*rule
, ofp_port_t port
)
3057 OVS_REQUIRES(ofproto_mutex
)
3059 if (port
== OFPP_ANY
) {
3062 const struct rule_actions
*actions
= rule_get_actions(rule
);
3063 return ofpacts_output_to_port(actions
->ofpacts
,
3064 actions
->ofpacts_len
, port
);
3068 /* Returns true if 'rule' has group and equals group_id. */
3070 ofproto_rule_has_out_group(const struct rule
*rule
, uint32_t group_id
)
3071 OVS_REQUIRES(ofproto_mutex
)
3073 if (group_id
== OFPG_ANY
) {
3076 const struct rule_actions
*actions
= rule_get_actions(rule
);
3077 return ofpacts_output_to_group(actions
->ofpacts
,
3078 actions
->ofpacts_len
, group_id
);
3083 rule_is_readonly(const struct rule
*rule
)
3085 const struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
3086 return (table
->flags
& OFTABLE_READONLY
) != 0;
3090 hash_learned_cookie(ovs_be64 cookie_
, uint8_t table_id
)
3092 uint64_t cookie
= (OVS_FORCE
uint64_t) cookie_
;
3093 return hash_3words(cookie
, cookie
>> 32, table_id
);
3097 learned_cookies_update_one__(struct ofproto
*ofproto
,
3098 const struct ofpact_learn
*learn
,
3099 int delta
, struct ovs_list
*dead_cookies
)
3100 OVS_REQUIRES(ofproto_mutex
)
3102 uint32_t hash
= hash_learned_cookie(learn
->cookie
, learn
->table_id
);
3103 struct learned_cookie
*c
;
3105 HMAP_FOR_EACH_WITH_HASH (c
, u
.hmap_node
, hash
, &ofproto
->learned_cookies
) {
3106 if (c
->cookie
== learn
->cookie
&& c
->table_id
== learn
->table_id
) {
3108 ovs_assert(c
->n
>= 0);
3111 hmap_remove(&ofproto
->learned_cookies
, &c
->u
.hmap_node
);
3112 ovs_list_push_back(dead_cookies
, &c
->u
.list_node
);
3119 ovs_assert(delta
> 0);
3120 c
= xmalloc(sizeof *c
);
3121 hmap_insert(&ofproto
->learned_cookies
, &c
->u
.hmap_node
, hash
);
3122 c
->cookie
= learn
->cookie
;
3123 c
->table_id
= learn
->table_id
;
3127 static const struct ofpact_learn
*
3128 next_learn_with_delete(const struct rule_actions
*actions
,
3129 const struct ofpact_learn
*start
)
3131 const struct ofpact
*pos
;
3133 for (pos
= start
? ofpact_next(&start
->ofpact
) : actions
->ofpacts
;
3134 pos
< ofpact_end(actions
->ofpacts
, actions
->ofpacts_len
);
3135 pos
= ofpact_next(pos
)) {
3136 if (pos
->type
== OFPACT_LEARN
) {
3137 const struct ofpact_learn
*learn
= ofpact_get_LEARN(pos
);
3138 if (learn
->flags
& NX_LEARN_F_DELETE_LEARNED
) {
3148 learned_cookies_update__(struct ofproto
*ofproto
,
3149 const struct rule_actions
*actions
,
3150 int delta
, struct ovs_list
*dead_cookies
)
3151 OVS_REQUIRES(ofproto_mutex
)
3153 if (actions
->has_learn_with_delete
) {
3154 const struct ofpact_learn
*learn
;
3156 for (learn
= next_learn_with_delete(actions
, NULL
); learn
;
3157 learn
= next_learn_with_delete(actions
, learn
)) {
3158 learned_cookies_update_one__(ofproto
, learn
, delta
, dead_cookies
);
3164 learned_cookies_inc(struct ofproto
*ofproto
,
3165 const struct rule_actions
*actions
)
3166 OVS_REQUIRES(ofproto_mutex
)
3168 learned_cookies_update__(ofproto
, actions
, +1, NULL
);
3172 learned_cookies_dec(struct ofproto
*ofproto
,
3173 const struct rule_actions
*actions
,
3174 struct ovs_list
*dead_cookies
)
3175 OVS_REQUIRES(ofproto_mutex
)
3177 learned_cookies_update__(ofproto
, actions
, -1, dead_cookies
);
3181 learned_cookies_flush(struct ofproto
*ofproto
, struct ovs_list
*dead_cookies
)
3182 OVS_REQUIRES(ofproto_mutex
)
3184 struct learned_cookie
*c
;
3186 LIST_FOR_EACH_POP (c
, u
.list_node
, dead_cookies
) {
3187 struct rule_criteria criteria
;
3188 struct rule_collection rules
;
3191 match_init_catchall(&match
);
3192 rule_criteria_init(&criteria
, c
->table_id
, &match
, 0, OVS_VERSION_MAX
,
3193 c
->cookie
, OVS_BE64_MAX
, OFPP_ANY
, OFPG_ANY
);
3194 rule_criteria_require_rw(&criteria
, false);
3195 collect_rules_loose(ofproto
, &criteria
, &rules
);
3196 rule_criteria_destroy(&criteria
);
3197 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
3204 handle_echo_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3206 ofconn_send_reply(ofconn
, make_echo_reply(oh
));
3211 query_tables(struct ofproto
*ofproto
,
3212 struct ofputil_table_features
**featuresp
,
3213 struct ofputil_table_stats
**statsp
)
3215 struct mf_bitmap rw_fields
= oxm_writable_fields();
3216 struct mf_bitmap match
= oxm_matchable_fields();
3217 struct mf_bitmap mask
= oxm_maskable_fields();
3219 struct ofputil_table_features
*features
;
3220 struct ofputil_table_stats
*stats
;
3223 features
= *featuresp
= xcalloc(ofproto
->n_tables
, sizeof *features
);
3224 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3225 struct ofputil_table_features
*f
= &features
[i
];
3228 sprintf(f
->name
, "table%d", i
);
3229 f
->metadata_match
= OVS_BE64_MAX
;
3230 f
->metadata_write
= OVS_BE64_MAX
;
3231 atomic_read_relaxed(&ofproto
->tables
[i
].miss_config
, &f
->miss_config
);
3232 f
->max_entries
= 1000000;
3234 bool more_tables
= false;
3235 for (int j
= i
+ 1; j
< ofproto
->n_tables
; j
++) {
3236 if (!(ofproto
->tables
[j
].flags
& OFTABLE_HIDDEN
)) {
3237 bitmap_set1(f
->nonmiss
.next
, j
);
3241 f
->nonmiss
.instructions
= (1u << N_OVS_INSTRUCTIONS
) - 1;
3243 f
->nonmiss
.instructions
&= ~(1u << OVSINST_OFPIT11_GOTO_TABLE
);
3245 f
->nonmiss
.write
.ofpacts
= (UINT64_C(1) << N_OFPACTS
) - 1;
3246 f
->nonmiss
.write
.set_fields
= rw_fields
;
3247 f
->nonmiss
.apply
= f
->nonmiss
.write
;
3248 f
->miss
= f
->nonmiss
;
3252 f
->wildcard
= match
;
3256 stats
= *statsp
= xcalloc(ofproto
->n_tables
, sizeof *stats
);
3257 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3258 struct ofputil_table_stats
*s
= &stats
[i
];
3261 s
->active_count
= ofproto
->tables
[i
].n_flows
;
3263 s
->active_count
-= connmgr_count_hidden_rules(
3271 ofproto
->ofproto_class
->query_tables(ofproto
, features
, stats
);
3273 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3274 const struct oftable
*table
= &ofproto
->tables
[i
];
3275 struct ofputil_table_features
*f
= &features
[i
];
3278 ovs_strzcpy(f
->name
, table
->name
, sizeof f
->name
);
3281 if (table
->max_flows
< f
->max_entries
) {
3282 f
->max_entries
= table
->max_flows
;
3288 query_switch_features(struct ofproto
*ofproto
,
3289 bool *arp_match_ip
, uint64_t *ofpacts
)
3291 struct ofputil_table_features
*features
, *f
;
3293 *arp_match_ip
= false;
3296 query_tables(ofproto
, &features
, NULL
);
3297 for (f
= features
; f
< &features
[ofproto
->n_tables
]; f
++) {
3298 *ofpacts
|= f
->nonmiss
.apply
.ofpacts
| f
->miss
.apply
.ofpacts
;
3299 if (bitmap_is_set(f
->match
.bm
, MFF_ARP_SPA
) ||
3300 bitmap_is_set(f
->match
.bm
, MFF_ARP_TPA
)) {
3301 *arp_match_ip
= true;
3307 ovs_assert(*ofpacts
& (UINT64_C(1) << OFPACT_OUTPUT
));
3311 handle_features_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3313 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3314 struct ofputil_switch_features features
;
3315 struct ofport
*port
;
3319 query_switch_features(ofproto
, &arp_match_ip
, &features
.ofpacts
);
3321 features
.datapath_id
= ofproto
->datapath_id
;
3322 features
.n_buffers
= 0;
3323 features
.n_tables
= ofproto_get_n_visible_tables(ofproto
);
3324 features
.capabilities
= (OFPUTIL_C_FLOW_STATS
| OFPUTIL_C_TABLE_STATS
|
3325 OFPUTIL_C_PORT_STATS
| OFPUTIL_C_QUEUE_STATS
|
3326 OFPUTIL_C_GROUP_STATS
| OFPUTIL_C_BUNDLES
);
3328 features
.capabilities
|= OFPUTIL_C_ARP_MATCH_IP
;
3330 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
3331 features
.auxiliary_id
= 0;
3332 b
= ofputil_encode_switch_features(&features
, ofconn_get_protocol(ofconn
),
3334 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3335 ofputil_put_switch_features_port(&port
->pp
, b
);
3338 ofconn_send_reply(ofconn
, b
);
3343 handle_get_config_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3345 struct ofputil_switch_config config
;
3346 config
.frag
= ofconn_get_ofproto(ofconn
)->frag_handling
;
3347 config
.invalid_ttl_to_controller
3348 = ofconn_get_invalid_ttl_to_controller(ofconn
);
3349 config
.miss_send_len
= ofconn_get_miss_send_len(ofconn
);
3351 ofconn_send_reply(ofconn
, ofputil_encode_get_config_reply(oh
, &config
));
3357 handle_set_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3359 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3360 struct ofputil_switch_config config
;
3363 error
= ofputil_decode_set_config(oh
, &config
);
3368 if (ofconn_get_type(ofconn
) != OFCONN_PRIMARY
3369 || ofconn_get_role(ofconn
) != OFPCR12_ROLE_SLAVE
) {
3370 enum ofputil_frag_handling cur
= ofproto
->frag_handling
;
3371 enum ofputil_frag_handling next
= config
.frag
;
3374 if (ofproto
->ofproto_class
->set_frag_handling(ofproto
, next
)) {
3375 ofproto
->frag_handling
= next
;
3377 VLOG_WARN_RL(&rl
, "%s: unsupported fragment handling mode %s",
3379 ofputil_frag_handling_to_string(next
));
3384 if (config
.invalid_ttl_to_controller
>= 0) {
3385 ofconn_set_invalid_ttl_to_controller(ofconn
,
3386 config
.invalid_ttl_to_controller
);
3389 ofconn_set_miss_send_len(ofconn
, config
.miss_send_len
);
3394 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
3395 * error message code for the caller to propagate upward. Otherwise, returns
3398 * The log message mentions 'msg_type'. */
3400 reject_slave_controller(struct ofconn
*ofconn
)
3402 if (ofconn_get_type(ofconn
) == OFCONN_PRIMARY
3403 && ofconn_get_role(ofconn
) == OFPCR12_ROLE_SLAVE
) {
3404 return OFPERR_OFPBRC_IS_SLAVE
;
3410 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
3413 * - If they use a meter, then 'ofproto' has that meter configured.
3414 * Updates the meter action with ofproto's datapath's provider_meter_id.
3416 * - If they use any groups, then 'ofproto' has that group configured.
3418 * Returns 0 if successful, otherwise an OpenFlow error. Caller must hold
3419 * 'ofproto_mutex' for the result to be valid also after this function
3422 ofproto_check_ofpacts(struct ofproto
*ofproto
,
3423 const struct ofpact ofpacts
[], size_t ofpacts_len
)
3424 OVS_REQUIRES(ofproto_mutex
)
3426 const struct ofpact
*a
;
3428 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
3429 if (a
->type
== OFPACT_METER
&&
3430 !ofproto_fix_meter_action(ofproto
, ofpact_get_METER(a
))) {
3431 return OFPERR_OFPMMFC_INVALID_METER
;
3434 if (a
->type
== OFPACT_GROUP
3435 && !ofproto_group_exists(ofproto
, ofpact_get_GROUP(a
)->group_id
)) {
3436 return OFPERR_OFPBAC_BAD_OUT_GROUP
;
3444 ofproto_packet_out_uninit(struct ofproto_packet_out
*opo
)
3446 dp_packet_delete(opo
->packet
);
3451 opo
->ofpacts
= NULL
;
3452 opo
->ofpacts_len
= 0;
3453 ovs_assert(!opo
->aux
);
3456 /* Takes ownership of po->ofpacts, which must have been malloc'ed. */
3458 ofproto_packet_out_init(struct ofproto
*ofproto
,
3459 struct ofconn
*ofconn
,
3460 struct ofproto_packet_out
*opo
,
3461 const struct ofputil_packet_out
*po
)
3467 uint64_t buf
[FLOW_U64S
];
3470 uint16_t in_port
= ofp_to_u16(po
->flow_metadata
.flow
.in_port
.ofp_port
);
3471 if (in_port
>= ofproto
->max_ports
&& in_port
< ofp_to_u16(OFPP_MAX
)) {
3472 return OFPERR_OFPBRC_BAD_PORT
;
3476 if (po
->buffer_id
!= UINT32_MAX
) {
3477 return OFPERR_OFPBRC_BUFFER_UNKNOWN
;
3480 /* Ensure that the L3 header is 32-bit aligned. */
3481 opo
->packet
= dp_packet_clone_data_with_headroom(po
->packet
,
3483 /* Take the received packet_tpye as packet_type of the packet. */
3484 opo
->packet
->packet_type
= po
->flow_metadata
.flow
.packet_type
;
3486 /* Store struct flow. */
3487 opo
->flow
= xmalloc(sizeof *opo
->flow
);
3488 *opo
->flow
= po
->flow_metadata
.flow
;
3489 miniflow_extract(opo
->packet
, &m
.mf
);
3490 flow_union_with_miniflow(opo
->flow
, &m
.mf
);
3492 /* Check actions like for flow mods. We pass a 'table_id' of 0 to
3493 * ofproto_check_consistency(), which isn't strictly correct because these
3494 * actions aren't in any table. This is OK as 'table_id' is only used to
3495 * check instructions (e.g., goto-table), which can't appear on the action
3496 * list of a packet-out. */
3497 match_wc_init(&match
, opo
->flow
);
3498 error
= ofpacts_check_consistency(po
->ofpacts
, po
->ofpacts_len
, &match
,
3499 u16_to_ofp(ofproto
->max_ports
), 0,
3501 ofconn_get_protocol(ofconn
));
3503 dp_packet_delete(opo
->packet
);
3508 opo
->ofpacts
= po
->ofpacts
;
3509 opo
->ofpacts_len
= po
->ofpacts_len
;
3516 ofproto_packet_out_start(struct ofproto
*ofproto
,
3517 struct ofproto_packet_out
*opo
)
3518 OVS_REQUIRES(ofproto_mutex
)
3522 error
= ofproto_check_ofpacts(ofproto
, opo
->ofpacts
, opo
->ofpacts_len
);
3527 return ofproto
->ofproto_class
->packet_xlate(ofproto
, opo
);
3531 ofproto_packet_out_revert(struct ofproto
*ofproto
,
3532 struct ofproto_packet_out
*opo
)
3533 OVS_REQUIRES(ofproto_mutex
)
3535 ofproto
->ofproto_class
->packet_xlate_revert(ofproto
, opo
);
3539 ofproto_packet_out_finish(struct ofproto
*ofproto
,
3540 struct ofproto_packet_out
*opo
)
3541 OVS_REQUIRES(ofproto_mutex
)
3543 ofproto
->ofproto_class
->packet_execute(ofproto
, opo
);
3547 handle_packet_out(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3548 OVS_EXCLUDED(ofproto_mutex
)
3550 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3551 struct ofputil_packet_out po
;
3552 struct ofproto_packet_out opo
;
3553 uint64_t ofpacts_stub
[1024 / 8];
3554 struct ofpbuf ofpacts
;
3557 COVERAGE_INC(ofproto_packet_out
);
3559 error
= reject_slave_controller(ofconn
);
3564 /* Decode message. */
3565 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
3566 error
= ofputil_decode_packet_out(&po
, oh
, ofproto_get_tun_tab(p
),
3569 ofpbuf_uninit(&ofpacts
);
3573 po
.ofpacts
= ofpbuf_steal_data(&ofpacts
); /* Move to heap. */
3574 error
= ofproto_packet_out_init(p
, ofconn
, &opo
, &po
);
3580 ovs_mutex_lock(&ofproto_mutex
);
3581 opo
.version
= p
->tables_version
;
3582 error
= ofproto_packet_out_start(p
, &opo
);
3584 ofproto_packet_out_finish(p
, &opo
);
3586 ovs_mutex_unlock(&ofproto_mutex
);
3588 ofproto_packet_out_uninit(&opo
);
3593 handle_nxt_resume(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3595 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3596 struct ofputil_packet_in_private pin
;
3599 error
= ofputil_decode_packet_in_private(oh
, false,
3600 ofproto_get_tun_tab(ofproto
),
3601 &ofproto
->vl_mff_map
, &pin
, NULL
,
3607 error
= (ofproto
->ofproto_class
->nxt_resume
3608 ? ofproto
->ofproto_class
->nxt_resume(ofproto
, &pin
)
3609 : OFPERR_NXR_NOT_SUPPORTED
);
3611 ofputil_packet_in_private_destroy(&pin
);
3617 update_port_config(struct ofconn
*ofconn
, struct ofport
*port
,
3618 enum ofputil_port_config config
,
3619 enum ofputil_port_config mask
)
3621 enum ofputil_port_config toggle
= (config
^ port
->pp
.config
) & mask
;
3623 if (toggle
& OFPUTIL_PC_PORT_DOWN
3624 && (config
& OFPUTIL_PC_PORT_DOWN
3625 ? netdev_turn_flags_off(port
->netdev
, NETDEV_UP
, NULL
)
3626 : netdev_turn_flags_on(port
->netdev
, NETDEV_UP
, NULL
))) {
3627 /* We tried to bring the port up or down, but it failed, so don't
3628 * update the "down" bit. */
3629 toggle
&= ~OFPUTIL_PC_PORT_DOWN
;
3633 enum ofputil_port_config old_config
= port
->pp
.config
;
3634 port
->pp
.config
^= toggle
;
3635 port
->ofproto
->ofproto_class
->port_reconfigured(port
, old_config
);
3636 connmgr_send_port_status(port
->ofproto
->connmgr
, ofconn
, &port
->pp
,
3642 port_mod_start(struct ofconn
*ofconn
, struct ofputil_port_mod
*pm
,
3643 struct ofport
**port
)
3645 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3647 *port
= ofproto_get_port(p
, pm
->port_no
);
3649 return OFPERR_OFPPMFC_BAD_PORT
;
3651 if (!eth_addr_equals((*port
)->pp
.hw_addr
, pm
->hw_addr
) ||
3652 !eth_addr64_equals((*port
)->pp
.hw_addr64
, pm
->hw_addr64
)) {
3653 return OFPERR_OFPPMFC_BAD_HW_ADDR
;
3659 port_mod_finish(struct ofconn
*ofconn
, struct ofputil_port_mod
*pm
,
3660 struct ofport
*port
)
3662 update_port_config(ofconn
, port
, pm
->config
, pm
->mask
);
3663 if (pm
->advertise
) {
3664 netdev_set_advertisements(port
->netdev
, pm
->advertise
);
3669 handle_port_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3671 struct ofputil_port_mod pm
;
3672 struct ofport
*port
;
3675 error
= reject_slave_controller(ofconn
);
3680 error
= ofputil_decode_port_mod(oh
, &pm
, false);
3685 error
= port_mod_start(ofconn
, &pm
, &port
);
3687 port_mod_finish(ofconn
, &pm
, port
);
3693 handle_desc_stats_request(struct ofconn
*ofconn
,
3694 const struct ofp_header
*request
)
3696 static const char *default_mfr_desc
= "Nicira, Inc.";
3697 static const char *default_hw_desc
= "Open vSwitch";
3698 static const char *default_sw_desc
= VERSION
;
3699 static const char *default_serial_desc
= "None";
3700 static const char *default_dp_desc
= "None";
3702 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3703 struct ofp_desc_stats
*ods
;
3706 msg
= ofpraw_alloc_stats_reply(request
, 0);
3707 ods
= ofpbuf_put_zeros(msg
, sizeof *ods
);
3708 ovs_strlcpy(ods
->mfr_desc
, p
->mfr_desc
? p
->mfr_desc
: default_mfr_desc
,
3709 sizeof ods
->mfr_desc
);
3710 ovs_strlcpy(ods
->hw_desc
, p
->hw_desc
? p
->hw_desc
: default_hw_desc
,
3711 sizeof ods
->hw_desc
);
3712 ovs_strlcpy(ods
->sw_desc
, p
->sw_desc
? p
->sw_desc
: default_sw_desc
,
3713 sizeof ods
->sw_desc
);
3714 ovs_strlcpy(ods
->serial_num
,
3715 p
->serial_desc
? p
->serial_desc
: default_serial_desc
,
3716 sizeof ods
->serial_num
);
3717 ovs_strlcpy(ods
->dp_desc
, p
->dp_desc
? p
->dp_desc
: default_dp_desc
,
3718 sizeof ods
->dp_desc
);
3719 ofconn_send_reply(ofconn
, msg
);
3725 handle_table_stats_request(struct ofconn
*ofconn
,
3726 const struct ofp_header
*request
)
3728 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3729 struct ofputil_table_features
*features
;
3730 struct ofputil_table_stats
*stats
;
3731 struct ofpbuf
*reply
;
3734 query_tables(ofproto
, &features
, &stats
);
3736 reply
= ofputil_encode_table_stats_reply(request
);
3737 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3738 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3739 ofputil_append_table_stats_reply(reply
, &stats
[i
], &features
[i
]);
3742 ofconn_send_reply(ofconn
, reply
);
3751 handle_table_features_request(struct ofconn
*ofconn
,
3752 const struct ofp_header
*request
)
3754 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3755 struct ofpbuf msg
= ofpbuf_const_initializer(request
,
3756 ntohs(request
->length
));
3757 ofpraw_pull_assert(&msg
);
3758 if (msg
.size
|| ofpmp_more(request
)) {
3759 return OFPERR_OFPTFFC_EPERM
;
3762 struct ofputil_table_features
*features
;
3763 query_tables(ofproto
, &features
, NULL
);
3765 struct ovs_list replies
;
3766 ofpmp_init(&replies
, request
);
3767 for (size_t i
= 0; i
< ofproto
->n_tables
; i
++) {
3768 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3769 ofputil_append_table_features_reply(&features
[i
], &replies
);
3772 ofconn_send_replies(ofconn
, &replies
);
3779 /* Returns the vacancy of 'oftable', a number that ranges from 0 (if the table
3780 * is full) to 100 (if the table is empty).
3782 * A table without a limit on flows is considered to be empty. */
3784 oftable_vacancy(const struct oftable
*t
)
3786 return (!t
->max_flows
? 100
3787 : t
->n_flows
>= t
->max_flows
? 0
3788 : (t
->max_flows
- t
->n_flows
) * 100.0 / t
->max_flows
);
3792 query_table_desc__(struct ofputil_table_desc
*td
,
3793 struct ofproto
*ofproto
, uint8_t table_id
)
3795 const struct oftable
*t
= &ofproto
->tables
[table_id
];
3797 td
->table_id
= table_id
;
3798 td
->eviction
= (t
->eviction
& EVICTION_OPENFLOW
3799 ? OFPUTIL_TABLE_EVICTION_ON
3800 : OFPUTIL_TABLE_EVICTION_OFF
);
3801 td
->eviction_flags
= OFPROTO_EVICTION_FLAGS
;
3802 td
->vacancy
= (t
->vacancy_event
3803 ? OFPUTIL_TABLE_VACANCY_ON
3804 : OFPUTIL_TABLE_VACANCY_OFF
);
3805 td
->table_vacancy
.vacancy_down
= t
->vacancy_down
;
3806 td
->table_vacancy
.vacancy_up
= t
->vacancy_up
;
3807 td
->table_vacancy
.vacancy
= oftable_vacancy(t
);
3810 /* This function queries the database for dumping table-desc. */
3812 query_tables_desc(struct ofproto
*ofproto
, struct ofputil_table_desc
**descp
)
3814 struct ofputil_table_desc
*table_desc
;
3817 table_desc
= *descp
= xcalloc(ofproto
->n_tables
, sizeof *table_desc
);
3818 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3819 struct ofputil_table_desc
*td
= &table_desc
[i
];
3820 query_table_desc__(td
, ofproto
, i
);
3824 /* Function to handle dump-table-desc request. */
3826 handle_table_desc_request(struct ofconn
*ofconn
,
3827 const struct ofp_header
*request
)
3829 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3830 struct ofputil_table_desc
*table_desc
;
3831 struct ovs_list replies
;
3834 query_tables_desc(ofproto
, &table_desc
);
3835 ofpmp_init(&replies
, request
);
3836 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3837 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3838 ofputil_append_table_desc_reply(&table_desc
[i
], &replies
,
3842 ofconn_send_replies(ofconn
, &replies
);
3847 /* This function determines and sends the vacancy event, based on the value
3848 * of current vacancy and threshold vacancy. If the current vacancy is less
3849 * than or equal to vacancy_down, vacancy up events must be enabled, and when
3850 * the current vacancy is greater or equal to vacancy_up, vacancy down events
3851 * must be enabled. */
3853 send_table_status(struct ofproto
*ofproto
, uint8_t table_id
)
3855 struct oftable
*t
= &ofproto
->tables
[table_id
];
3856 if (!t
->vacancy_event
) {
3860 uint8_t vacancy
= oftable_vacancy(t
);
3861 enum ofp14_table_reason event
;
3862 if (vacancy
< t
->vacancy_down
) {
3863 event
= OFPTR_VACANCY_DOWN
;
3864 } else if (vacancy
> t
->vacancy_up
) {
3865 event
= OFPTR_VACANCY_UP
;
3870 if (event
== t
->vacancy_event
) {
3871 struct ofputil_table_desc td
;
3872 query_table_desc__(&td
, ofproto
, table_id
);
3873 connmgr_send_table_status(ofproto
->connmgr
, &td
, event
);
3875 t
->vacancy_event
= (event
== OFPTR_VACANCY_DOWN
3877 : OFPTR_VACANCY_DOWN
);
3882 append_port_stat(struct ofport
*port
, struct ovs_list
*replies
)
3884 struct ofputil_port_stats ops
= { .port_no
= port
->pp
.port_no
};
3886 calc_duration(port
->created
, time_msec(),
3887 &ops
.duration_sec
, &ops
.duration_nsec
);
3889 /* Intentionally ignore return value, since errors will set
3890 * 'stats' to all-1s, which is correct for OpenFlow, and
3891 * netdev_get_stats() will log errors. */
3892 ofproto_port_get_stats(port
, &ops
.stats
);
3893 netdev_get_custom_stats(port
->netdev
, &ops
.custom_stats
);
3895 ofputil_append_port_stat(replies
, &ops
);
3897 netdev_free_custom_stats_counters(&ops
.custom_stats
);
3901 handle_port_request(struct ofconn
*ofconn
,
3902 const struct ofp_header
*request
, ofp_port_t port_no
,
3903 void (*cb
)(struct ofport
*, struct ovs_list
*replies
))
3905 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3906 struct ofport
*port
;
3907 struct ovs_list replies
;
3909 ofpmp_init(&replies
, request
);
3910 if (port_no
!= OFPP_ANY
) {
3911 port
= ofproto_get_port(ofproto
, port_no
);
3916 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3921 ofconn_send_replies(ofconn
, &replies
);
3925 handle_port_stats_request(struct ofconn
*ofconn
,
3926 const struct ofp_header
*request
)
3931 error
= ofputil_decode_port_stats_request(request
, &port_no
);
3933 handle_port_request(ofconn
, request
, port_no
, append_port_stat
);
3939 append_port_desc(struct ofport
*port
, struct ovs_list
*replies
)
3941 ofputil_append_port_desc_stats_reply(&port
->pp
, replies
);
3945 handle_port_desc_stats_request(struct ofconn
*ofconn
,
3946 const struct ofp_header
*request
)
3951 error
= ofputil_decode_port_desc_stats_request(request
, &port_no
);
3953 handle_port_request(ofconn
, request
, port_no
, append_port_desc
);
3959 hash_cookie(ovs_be64 cookie
)
3961 return hash_uint64((OVS_FORCE
uint64_t)cookie
);
3965 cookies_insert(struct ofproto
*ofproto
, struct rule
*rule
)
3966 OVS_REQUIRES(ofproto_mutex
)
3968 hindex_insert(&ofproto
->cookies
, &rule
->cookie_node
,
3969 hash_cookie(rule
->flow_cookie
));
3973 cookies_remove(struct ofproto
*ofproto
, struct rule
*rule
)
3974 OVS_REQUIRES(ofproto_mutex
)
3976 hindex_remove(&ofproto
->cookies
, &rule
->cookie_node
);
3980 calc_duration(long long int start
, long long int now
,
3981 uint32_t *sec
, uint32_t *nsec
)
3983 long long int msecs
= now
- start
;
3984 *sec
= msecs
/ 1000;
3985 *nsec
= (msecs
% 1000) * (1000 * 1000);
3988 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3989 * true if 'table_id' is OK, false otherwise. */
3991 check_table_id(const struct ofproto
*ofproto
, uint8_t table_id
)
3993 return table_id
== OFPTT_ALL
|| table_id
< ofproto
->n_tables
;
3996 static struct oftable
*
3997 next_visible_table(const struct ofproto
*ofproto
, uint8_t table_id
)
3999 struct oftable
*table
;
4001 for (table
= &ofproto
->tables
[table_id
];
4002 table
< &ofproto
->tables
[ofproto
->n_tables
];
4004 if (!(table
->flags
& OFTABLE_HIDDEN
)) {
4012 static struct oftable
*
4013 first_matching_table(const struct ofproto
*ofproto
, uint8_t table_id
)
4015 if (table_id
== 0xff) {
4016 return next_visible_table(ofproto
, 0);
4017 } else if (table_id
< ofproto
->n_tables
) {
4018 return &ofproto
->tables
[table_id
];
4024 static struct oftable
*
4025 next_matching_table(const struct ofproto
*ofproto
,
4026 const struct oftable
*table
, uint8_t table_id
)
4028 return (table_id
== 0xff
4029 ? next_visible_table(ofproto
, (table
- ofproto
->tables
) + 1)
4033 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
4035 * - If TABLE_ID is 0xff, this iterates over every classifier table in
4036 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
4038 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
4039 * only once, for that table. (This can be used to access tables marked
4042 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
4043 * entered at all. (Perhaps you should have validated TABLE_ID with
4044 * check_table_id().)
4046 * All parameters are evaluated multiple times.
4048 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
4049 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
4051 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
4053 /* Initializes 'criteria' in a straightforward way based on the other
4056 * By default, the criteria include flows that are read-only, on the assumption
4057 * that the collected flows won't be modified. Call rule_criteria_require_rw()
4058 * if flows will be modified.
4060 * For "loose" matching, the 'priority' parameter is unimportant and may be
4063 rule_criteria_init(struct rule_criteria
*criteria
, uint8_t table_id
,
4064 const struct match
*match
, int priority
,
4065 ovs_version_t version
, ovs_be64 cookie
,
4066 ovs_be64 cookie_mask
, ofp_port_t out_port
,
4069 criteria
->table_id
= table_id
;
4070 cls_rule_init(&criteria
->cr
, match
, priority
);
4071 criteria
->version
= version
;
4072 criteria
->cookie
= cookie
;
4073 criteria
->cookie_mask
= cookie_mask
;
4074 criteria
->out_port
= out_port
;
4075 criteria
->out_group
= out_group
;
4077 /* We ordinarily want to skip hidden rules, but there has to be a way for
4078 * code internal to OVS to modify and delete them, so if the criteria
4079 * specify a priority that can only be for a hidden flow, then allow hidden
4080 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
4081 * with hidden flows because OpenFlow uses only a 16-bit field to specify
4083 criteria
->include_hidden
= priority
> UINT16_MAX
;
4085 /* We assume that the criteria are being used to collect flows for reading
4086 * but not modification. Thus, we should collect read-only flows. */
4087 criteria
->include_readonly
= true;
4090 /* By default, criteria initialized by rule_criteria_init() will match flows
4091 * that are read-only, on the assumption that the collected flows won't be
4092 * modified. Call this function to match only flows that are be modifiable.
4094 * Specify 'can_write_readonly' as false in ordinary circumstances, true if the
4095 * caller has special privileges that allow it to modify even "read-only"
4098 rule_criteria_require_rw(struct rule_criteria
*criteria
,
4099 bool can_write_readonly
)
4101 criteria
->include_readonly
= can_write_readonly
;
4105 rule_criteria_destroy(struct rule_criteria
*criteria
)
4107 cls_rule_destroy(&criteria
->cr
);
4108 criteria
->version
= OVS_VERSION_NOT_REMOVED
; /* Mark as destroyed. */
4111 /* Schedules postponed removal of rules, destroys 'rules'. */
4113 remove_rules_postponed(struct rule_collection
*rules
)
4114 OVS_REQUIRES(ofproto_mutex
)
4116 if (rule_collection_n(rules
) > 0) {
4117 if (rule_collection_n(rules
) == 1) {
4118 ovsrcu_postpone(remove_rule_rcu
, rule_collection_rules(rules
)[0]);
4119 rule_collection_init(rules
);
4121 ovsrcu_postpone(remove_rules_rcu
, rule_collection_detach(rules
));
4126 /* Schedules postponed removal of groups, destroys 'groups'. */
4128 remove_groups_postponed(struct group_collection
*groups
)
4129 OVS_REQUIRES(ofproto_mutex
)
4131 if (group_collection_n(groups
) > 0) {
4132 if (group_collection_n(groups
) == 1) {
4133 ovsrcu_postpone(remove_group_rcu
,
4134 group_collection_groups(groups
)[0]);
4135 group_collection_init(groups
);
4137 ovsrcu_postpone(remove_groups_rcu
,
4138 group_collection_detach(groups
));
4143 /* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This
4144 * function verifies most of the criteria in 'c' itself, but the caller must
4145 * check 'c->cr' itself.
4147 * Rules that have already been marked for removal are not collected.
4149 * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and
4150 * 'c' only includes modifiable rules). */
4152 collect_rule(struct rule
*rule
, const struct rule_criteria
*c
,
4153 struct rule_collection
*rules
, size_t *n_readonly
)
4154 OVS_REQUIRES(ofproto_mutex
)
4156 if ((c
->table_id
== rule
->table_id
|| c
->table_id
== 0xff)
4157 && ofproto_rule_has_out_port(rule
, c
->out_port
)
4158 && ofproto_rule_has_out_group(rule
, c
->out_group
)
4159 && !((rule
->flow_cookie
^ c
->cookie
) & c
->cookie_mask
)
4160 && (!rule_is_hidden(rule
) || c
->include_hidden
)
4161 && cls_rule_visible_in_version(&rule
->cr
, c
->version
)) {
4162 /* Rule matches all the criteria... */
4163 if (!rule_is_readonly(rule
) || c
->include_readonly
) {
4165 rule_collection_add(rules
, rule
);
4167 /* ...except it's read-only. */
4173 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4174 * on classifiers rules are done in the "loose" way required for OpenFlow
4175 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
4178 * Returns 0 on success, otherwise an OpenFlow error code. */
4180 collect_rules_loose(struct ofproto
*ofproto
,
4181 const struct rule_criteria
*criteria
,
4182 struct rule_collection
*rules
)
4183 OVS_REQUIRES(ofproto_mutex
)
4185 struct oftable
*table
;
4186 enum ofperr error
= 0;
4187 size_t n_readonly
= 0;
4189 rule_collection_init(rules
);
4191 if (!check_table_id(ofproto
, criteria
->table_id
)) {
4192 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4196 if (criteria
->cookie_mask
== OVS_BE64_MAX
) {
4199 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
4200 hash_cookie(criteria
->cookie
),
4201 &ofproto
->cookies
) {
4202 if (cls_rule_is_loose_match(&rule
->cr
, &criteria
->cr
.match
)) {
4203 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4207 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
4210 CLS_FOR_EACH_TARGET (rule
, cr
, &table
->cls
, &criteria
->cr
,
4211 criteria
->version
) {
4212 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4218 if (!error
&& !rule_collection_n(rules
) && n_readonly
) {
4219 /* We didn't find any rules to modify. We did find some read-only
4220 * rules that we're not allowed to modify, so report that. */
4221 error
= OFPERR_OFPBRC_EPERM
;
4224 rule_collection_destroy(rules
);
4229 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4230 * on classifiers rules are done in the "strict" way required for OpenFlow
4231 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
4232 * rules on list 'rules'.
4234 * Returns 0 on success, otherwise an OpenFlow error code. */
4236 collect_rules_strict(struct ofproto
*ofproto
,
4237 const struct rule_criteria
*criteria
,
4238 struct rule_collection
*rules
)
4239 OVS_REQUIRES(ofproto_mutex
)
4241 struct oftable
*table
;
4242 size_t n_readonly
= 0;
4243 enum ofperr error
= 0;
4245 rule_collection_init(rules
);
4247 if (!check_table_id(ofproto
, criteria
->table_id
)) {
4248 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4252 if (criteria
->cookie_mask
== OVS_BE64_MAX
) {
4255 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
4256 hash_cookie(criteria
->cookie
),
4257 &ofproto
->cookies
) {
4258 if (cls_rule_equal(&rule
->cr
, &criteria
->cr
)) {
4259 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4263 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
4266 rule
= rule_from_cls_rule(classifier_find_rule_exactly(
4267 &table
->cls
, &criteria
->cr
,
4268 criteria
->version
));
4270 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4276 if (!error
&& !rule_collection_n(rules
) && n_readonly
) {
4277 /* We didn't find any rules to modify. We did find some read-only
4278 * rules that we're not allowed to modify, so report that. */
4279 error
= OFPERR_OFPBRC_EPERM
;
4282 rule_collection_destroy(rules
);
4287 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
4288 * forced into the range of a uint16_t. */
4290 age_secs(long long int age_ms
)
4292 return (age_ms
< 0 ? 0
4293 : age_ms
>= UINT16_MAX
* 1000 ? UINT16_MAX
4294 : (unsigned int) age_ms
/ 1000);
4298 handle_flow_stats_request(struct ofconn
*ofconn
,
4299 const struct ofp_header
*request
)
4300 OVS_EXCLUDED(ofproto_mutex
)
4302 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4303 struct ofputil_flow_stats_request fsr
;
4304 struct rule_criteria criteria
;
4305 struct rule_collection rules
;
4306 struct ovs_list replies
;
4309 error
= ofputil_decode_flow_stats_request(&fsr
, request
,
4310 ofproto_get_tun_tab(ofproto
),
4311 &ofproto
->vl_mff_map
);
4316 rule_criteria_init(&criteria
, fsr
.table_id
, &fsr
.match
, 0, OVS_VERSION_MAX
,
4317 fsr
.cookie
, fsr
.cookie_mask
, fsr
.out_port
,
4320 ovs_mutex_lock(&ofproto_mutex
);
4321 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4322 rule_criteria_destroy(&criteria
);
4324 rule_collection_ref(&rules
);
4326 ovs_mutex_unlock(&ofproto_mutex
);
4332 ofpmp_init(&replies
, request
);
4334 RULE_COLLECTION_FOR_EACH (rule
, &rules
) {
4335 long long int now
= time_msec();
4336 struct ofputil_flow_stats fs
;
4337 long long int created
, used
, modified
;
4338 const struct rule_actions
*actions
;
4339 enum ofputil_flow_mod_flags flags
;
4341 ovs_mutex_lock(&rule
->mutex
);
4342 fs
.cookie
= rule
->flow_cookie
;
4343 fs
.idle_timeout
= rule
->idle_timeout
;
4344 fs
.hard_timeout
= rule
->hard_timeout
;
4345 fs
.importance
= rule
->importance
;
4346 created
= rule
->created
;
4347 modified
= rule
->modified
;
4348 actions
= rule_get_actions(rule
);
4349 flags
= rule
->flags
;
4350 ovs_mutex_unlock(&rule
->mutex
);
4352 ofproto
->ofproto_class
->rule_get_stats(rule
, &fs
.packet_count
,
4353 &fs
.byte_count
, &used
);
4355 minimatch_expand(&rule
->cr
.match
, &fs
.match
);
4356 fs
.table_id
= rule
->table_id
;
4357 calc_duration(created
, now
, &fs
.duration_sec
, &fs
.duration_nsec
);
4358 fs
.priority
= rule
->cr
.priority
;
4359 fs
.idle_age
= age_secs(now
- used
);
4360 fs
.hard_age
= age_secs(now
- modified
);
4361 fs
.ofpacts
= actions
->ofpacts
;
4362 fs
.ofpacts_len
= actions
->ofpacts_len
;
4365 ofputil_append_flow_stats_reply(&fs
, &replies
,
4366 ofproto_get_tun_tab(ofproto
));
4369 rule_collection_unref(&rules
);
4370 rule_collection_destroy(&rules
);
4372 ofconn_send_replies(ofconn
, &replies
);
4378 flow_stats_ds(struct ofproto
*ofproto
, struct rule
*rule
, struct ds
*results
)
4380 uint64_t packet_count
, byte_count
;
4381 const struct rule_actions
*actions
;
4382 long long int created
, used
;
4384 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
4385 &byte_count
, &used
);
4387 ovs_mutex_lock(&rule
->mutex
);
4388 actions
= rule_get_actions(rule
);
4389 created
= rule
->created
;
4390 ovs_mutex_unlock(&rule
->mutex
);
4392 if (rule
->table_id
!= 0) {
4393 ds_put_format(results
, "table_id=%"PRIu8
", ", rule
->table_id
);
4395 ds_put_format(results
, "duration=%llds, ", (time_msec() - created
) / 1000);
4396 ds_put_format(results
, "n_packets=%"PRIu64
", ", packet_count
);
4397 ds_put_format(results
, "n_bytes=%"PRIu64
", ", byte_count
);
4398 cls_rule_format(&rule
->cr
, ofproto_get_tun_tab(ofproto
), NULL
, results
);
4399 ds_put_char(results
, ',');
4401 ds_put_cstr(results
, "actions=");
4402 ofpacts_format(actions
->ofpacts
, actions
->ofpacts_len
, NULL
, results
);
4404 ds_put_cstr(results
, "\n");
4407 /* Adds a pretty-printed description of all flows to 'results', including
4408 * hidden flows (e.g., set up by in-band control). */
4410 ofproto_get_all_flows(struct ofproto
*p
, struct ds
*results
)
4412 struct oftable
*table
;
4414 OFPROTO_FOR_EACH_TABLE (table
, p
) {
4417 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
4418 flow_stats_ds(p
, rule
, results
);
4423 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
4424 * '*engine_type' and '*engine_id', respectively. */
4426 ofproto_get_netflow_ids(const struct ofproto
*ofproto
,
4427 uint8_t *engine_type
, uint8_t *engine_id
)
4429 ofproto
->ofproto_class
->get_netflow_ids(ofproto
, engine_type
, engine_id
);
4432 /* Checks the status change of CFM on 'ofport'.
4434 * Returns true if 'ofproto_class' does not support 'cfm_status_changed'. */
4436 ofproto_port_cfm_status_changed(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
4438 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
4439 return (ofport
&& ofproto
->ofproto_class
->cfm_status_changed
4440 ? ofproto
->ofproto_class
->cfm_status_changed(ofport
)
4444 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'.
4445 * Returns 0 if the port's CFM status was successfully stored into
4446 * '*status'. Returns positive errno if the port did not have CFM
4449 * The caller must provide and own '*status', and must free 'status->rmps'.
4450 * '*status' is indeterminate if the return value is non-zero. */
4452 ofproto_port_get_cfm_status(const struct ofproto
*ofproto
, ofp_port_t ofp_port
,
4453 struct cfm_status
*status
)
4455 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
4456 return (ofport
&& ofproto
->ofproto_class
->get_cfm_status
4457 ? ofproto
->ofproto_class
->get_cfm_status(ofport
, status
)
4462 handle_aggregate_stats_request(struct ofconn
*ofconn
,
4463 const struct ofp_header
*oh
)
4464 OVS_EXCLUDED(ofproto_mutex
)
4466 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4467 struct ofputil_flow_stats_request request
;
4468 struct ofputil_aggregate_stats stats
;
4469 bool unknown_packets
, unknown_bytes
;
4470 struct rule_criteria criteria
;
4471 struct rule_collection rules
;
4472 struct ofpbuf
*reply
;
4475 error
= ofputil_decode_flow_stats_request(&request
, oh
,
4476 ofproto_get_tun_tab(ofproto
),
4477 &ofproto
->vl_mff_map
);
4482 rule_criteria_init(&criteria
, request
.table_id
, &request
.match
, 0,
4483 OVS_VERSION_MAX
, request
.cookie
, request
.cookie_mask
,
4484 request
.out_port
, request
.out_group
);
4486 ovs_mutex_lock(&ofproto_mutex
);
4487 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4488 rule_criteria_destroy(&criteria
);
4490 rule_collection_ref(&rules
);
4492 ovs_mutex_unlock(&ofproto_mutex
);
4498 memset(&stats
, 0, sizeof stats
);
4499 unknown_packets
= unknown_bytes
= false;
4502 RULE_COLLECTION_FOR_EACH (rule
, &rules
) {
4503 uint64_t packet_count
;
4504 uint64_t byte_count
;
4507 ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
4508 &byte_count
, &used
);
4510 if (packet_count
== UINT64_MAX
) {
4511 unknown_packets
= true;
4513 stats
.packet_count
+= packet_count
;
4516 if (byte_count
== UINT64_MAX
) {
4517 unknown_bytes
= true;
4519 stats
.byte_count
+= byte_count
;
4524 if (unknown_packets
) {
4525 stats
.packet_count
= UINT64_MAX
;
4527 if (unknown_bytes
) {
4528 stats
.byte_count
= UINT64_MAX
;
4531 rule_collection_unref(&rules
);
4532 rule_collection_destroy(&rules
);
4534 reply
= ofputil_encode_aggregate_stats_reply(&stats
, oh
);
4535 ofconn_send_reply(ofconn
, reply
);
4540 struct queue_stats_cbdata
{
4541 struct ofport
*ofport
;
4542 struct ovs_list replies
;
4547 put_queue_stats(struct queue_stats_cbdata
*cbdata
, uint32_t queue_id
,
4548 const struct netdev_queue_stats
*stats
)
4550 struct ofputil_queue_stats oqs
;
4552 oqs
.port_no
= cbdata
->ofport
->pp
.port_no
;
4553 oqs
.queue_id
= queue_id
;
4554 oqs
.tx_bytes
= stats
->tx_bytes
;
4555 oqs
.tx_packets
= stats
->tx_packets
;
4556 oqs
.tx_errors
= stats
->tx_errors
;
4557 if (stats
->created
!= LLONG_MIN
) {
4558 calc_duration(stats
->created
, cbdata
->now
,
4559 &oqs
.duration_sec
, &oqs
.duration_nsec
);
4561 oqs
.duration_sec
= oqs
.duration_nsec
= UINT32_MAX
;
4563 ofputil_append_queue_stat(&cbdata
->replies
, &oqs
);
4567 handle_queue_stats_dump_cb(uint32_t queue_id
,
4568 struct netdev_queue_stats
*stats
,
4571 struct queue_stats_cbdata
*cbdata
= cbdata_
;
4573 put_queue_stats(cbdata
, queue_id
, stats
);
4577 handle_queue_stats_for_port(struct ofport
*port
, uint32_t queue_id
,
4578 struct queue_stats_cbdata
*cbdata
)
4580 cbdata
->ofport
= port
;
4581 if (queue_id
== OFPQ_ALL
) {
4582 netdev_dump_queue_stats(port
->netdev
,
4583 handle_queue_stats_dump_cb
, cbdata
);
4585 struct netdev_queue_stats stats
;
4587 if (!netdev_get_queue_stats(port
->netdev
, queue_id
, &stats
)) {
4588 put_queue_stats(cbdata
, queue_id
, &stats
);
4590 return OFPERR_OFPQOFC_BAD_QUEUE
;
4597 handle_queue_stats_request(struct ofconn
*ofconn
,
4598 const struct ofp_header
*rq
)
4600 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4601 struct queue_stats_cbdata cbdata
;
4602 struct ofport
*port
;
4604 struct ofputil_queue_stats_request oqsr
;
4606 COVERAGE_INC(ofproto_queue_req
);
4608 ofpmp_init(&cbdata
.replies
, rq
);
4609 cbdata
.now
= time_msec();
4611 error
= ofputil_decode_queue_stats_request(rq
, &oqsr
);
4616 if (oqsr
.port_no
== OFPP_ANY
) {
4617 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
4618 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
4619 if (!handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)) {
4624 port
= ofproto_get_port(ofproto
, oqsr
.port_no
);
4626 ? handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)
4627 : OFPERR_OFPQOFC_BAD_PORT
);
4630 ofconn_send_replies(ofconn
, &cbdata
.replies
);
4632 ofpbuf_list_delete(&cbdata
.replies
);
4639 evict_rules_from_table(struct oftable
*table
)
4640 OVS_REQUIRES(ofproto_mutex
)
4642 enum ofperr error
= 0;
4643 struct rule_collection rules
;
4644 unsigned int count
= table
->n_flows
;
4645 unsigned int max_flows
= table
->max_flows
;
4647 rule_collection_init(&rules
);
4649 while (count
-- > max_flows
) {
4652 if (!choose_rule_to_evict(table
, &rule
)) {
4653 error
= OFPERR_OFPFMFC_TABLE_FULL
;
4656 eviction_group_remove_rule(rule
);
4657 rule_collection_add(&rules
, rule
);
4660 delete_flows__(&rules
, OFPRR_EVICTION
, NULL
);
4666 get_conjunctions(const struct ofputil_flow_mod
*fm
,
4667 struct cls_conjunction
**conjsp
, size_t *n_conjsp
)
4669 struct cls_conjunction
*conjs
= NULL
;
4672 const struct ofpact
*ofpact
;
4673 OFPACT_FOR_EACH (ofpact
, fm
->ofpacts
, fm
->ofpacts_len
) {
4674 if (ofpact
->type
== OFPACT_CONJUNCTION
) {
4676 } else if (ofpact
->type
!= OFPACT_NOTE
) {
4677 /* "conjunction" may appear with "note" actions but not with any
4678 * other type of actions. */
4679 ovs_assert(!n_conjs
);
4686 conjs
= xzalloc(n_conjs
* sizeof *conjs
);
4687 OFPACT_FOR_EACH (ofpact
, fm
->ofpacts
, fm
->ofpacts_len
) {
4688 if (ofpact
->type
== OFPACT_CONJUNCTION
) {
4689 struct ofpact_conjunction
*oc
= ofpact_get_CONJUNCTION(ofpact
);
4690 conjs
[i
].clause
= oc
->clause
;
4691 conjs
[i
].n_clauses
= oc
->n_clauses
;
4692 conjs
[i
].id
= oc
->id
;
4699 *n_conjsp
= n_conjs
;
4702 /* add_flow_init(), add_flow_start(), add_flow_revert(), and add_flow_finish()
4703 * implement OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
4704 * in which no matching flow already exists in the flow table.
4706 * add_flow_init() creates a new flow according to 'fm' and stores it to 'ofm'
4707 * for later reference. If the flow replaces other flow, it will be updated to
4708 * match modify semantics later by add_flow_start() (by calling
4709 * replace_rule_start()).
4711 * Returns 0 on success, or an OpenFlow error code on failure.
4713 * On successful return the caller must complete the operation by calling
4714 * add_flow_start(), and if that succeeds, then either add_flow_finish(), or
4715 * add_flow_revert() if the operation needs to be reverted due to a later
4719 add_flow_init(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4720 const struct ofputil_flow_mod
*fm
)
4721 OVS_EXCLUDED(ofproto_mutex
)
4723 struct oftable
*table
;
4728 if (!check_table_id(ofproto
, fm
->table_id
)) {
4729 return OFPERR_OFPBRC_BAD_TABLE_ID
;
4733 if (fm
->table_id
== 0xff) {
4734 if (ofproto
->ofproto_class
->rule_choose_table
) {
4735 error
= ofproto
->ofproto_class
->rule_choose_table(ofproto
,
4741 ovs_assert(table_id
< ofproto
->n_tables
);
4745 } else if (fm
->table_id
< ofproto
->n_tables
) {
4746 table_id
= fm
->table_id
;
4748 return OFPERR_OFPBRC_BAD_TABLE_ID
;
4751 table
= &ofproto
->tables
[table_id
];
4752 if (table
->flags
& OFTABLE_READONLY
4753 && !(fm
->flags
& OFPUTIL_FF_NO_READONLY
)) {
4754 return OFPERR_OFPBRC_EPERM
;
4757 if (!(fm
->flags
& OFPUTIL_FF_HIDDEN_FIELDS
)
4758 && !match_has_default_hidden_fields(&fm
->match
)) {
4759 VLOG_WARN_RL(&rl
, "%s: (add_flow) only internal flows can set "
4760 "non-default values to hidden fields", ofproto
->name
);
4761 return OFPERR_OFPBRC_EPERM
;
4764 if (!ofm
->temp_rule
) {
4765 cls_rule_init(&cr
, &fm
->match
, fm
->priority
);
4767 /* Allocate new rule. Destroys 'cr'. */
4768 error
= ofproto_rule_create(ofproto
, &cr
, table
- ofproto
->tables
,
4769 fm
->new_cookie
, fm
->idle_timeout
,
4770 fm
->hard_timeout
, fm
->flags
,
4771 fm
->importance
, fm
->ofpacts
,
4773 fm
->match
.flow
.tunnel
.metadata
.present
.map
,
4774 fm
->ofpacts_tlv_bitmap
, &ofm
->temp_rule
);
4779 get_conjunctions(fm
, &ofm
->conjs
, &ofm
->n_conjs
);
4784 /* ofm->temp_rule is consumed only in the successful case. */
4786 add_flow_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
4787 OVS_REQUIRES(ofproto_mutex
)
4789 struct rule
*old_rule
= NULL
;
4790 struct rule
*new_rule
= ofm
->temp_rule
;
4791 const struct rule_actions
*actions
= rule_get_actions(new_rule
);
4792 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
4795 /* Must check actions while holding ofproto_mutex to avoid a race. */
4796 error
= ofproto_check_ofpacts(ofproto
, actions
->ofpacts
,
4797 actions
->ofpacts_len
);
4802 /* Check for the existence of an identical rule.
4803 * This will not return rules earlier marked for removal. */
4804 old_rule
= rule_from_cls_rule(classifier_find_rule_exactly(&table
->cls
,
4808 /* Check for overlap, if requested. */
4809 if (new_rule
->flags
& OFPUTIL_FF_CHECK_OVERLAP
4810 && classifier_rule_overlaps(&table
->cls
, &new_rule
->cr
,
4812 return OFPERR_OFPFMFC_OVERLAP
;
4815 /* If necessary, evict an existing rule to clear out space. */
4816 if (table
->n_flows
>= table
->max_flows
) {
4817 if (!choose_rule_to_evict(table
, &old_rule
)) {
4818 return OFPERR_OFPFMFC_TABLE_FULL
;
4820 eviction_group_remove_rule(old_rule
);
4821 /* Marks 'old_rule' as an evicted rule rather than replaced rule.
4823 old_rule
->removed_reason
= OFPRR_EVICTION
;
4826 ofm
->modify_cookie
= true;
4830 rule_collection_add(&ofm
->old_rules
, old_rule
);
4832 /* Take ownership of the temp_rule. */
4833 rule_collection_add(&ofm
->new_rules
, new_rule
);
4834 ofm
->temp_rule
= NULL
;
4836 replace_rule_start(ofproto
, ofm
, old_rule
, new_rule
);
4840 /* Revert the effects of add_flow_start(). */
4842 add_flow_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
4843 OVS_REQUIRES(ofproto_mutex
)
4845 struct rule
*old_rule
= rule_collection_n(&ofm
->old_rules
)
4846 ? rule_collection_rules(&ofm
->old_rules
)[0] : NULL
;
4847 struct rule
*new_rule
= rule_collection_rules(&ofm
->new_rules
)[0];
4849 replace_rule_revert(ofproto
, old_rule
, new_rule
);
4852 /* To be called after version bump. */
4854 add_flow_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4855 const struct openflow_mod_requester
*req
)
4856 OVS_REQUIRES(ofproto_mutex
)
4858 struct rule
*old_rule
= rule_collection_n(&ofm
->old_rules
)
4859 ? rule_collection_rules(&ofm
->old_rules
)[0] : NULL
;
4860 struct rule
*new_rule
= rule_collection_rules(&ofm
->new_rules
)[0];
4861 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
4863 replace_rule_finish(ofproto
, ofm
, req
, old_rule
, new_rule
, &dead_cookies
);
4864 learned_cookies_flush(ofproto
, &dead_cookies
);
4867 ovsrcu_postpone(remove_rule_rcu
, old_rule
);
4869 ofmonitor_report(ofproto
->connmgr
, new_rule
, NXFME_ADDED
, 0,
4870 req
? req
->ofconn
: NULL
,
4871 req
? req
->request
->xid
: 0, NULL
);
4873 /* Send Vacancy Events for OF1.4+. */
4874 send_table_status(ofproto
, new_rule
->table_id
);
4878 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4880 /* Create a new rule. Note that the rule is NOT inserted into a any data
4881 * structures yet. Takes ownership of 'cr'. Only assigns '*new_rule' if
4884 ofproto_rule_create(struct ofproto
*ofproto
, struct cls_rule
*cr
,
4885 uint8_t table_id
, ovs_be64 new_cookie
,
4886 uint16_t idle_timeout
, uint16_t hard_timeout
,
4887 enum ofputil_flow_mod_flags flags
, uint16_t importance
,
4888 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
4889 uint64_t match_tlv_bitmap
, uint64_t ofpacts_tlv_bitmap
,
4890 struct rule
**new_rule
)
4891 OVS_NO_THREAD_SAFETY_ANALYSIS
4896 /* Allocate new rule. */
4897 rule
= ofproto
->ofproto_class
->rule_alloc();
4899 cls_rule_destroy(cr
);
4900 VLOG_WARN_RL(&rl
, "%s: failed to allocate a rule.", ofproto
->name
);
4901 return OFPERR_OFPFMFC_UNKNOWN
;
4904 /* Initialize base state. */
4905 *CONST_CAST(struct ofproto
**, &rule
->ofproto
) = ofproto
;
4906 cls_rule_move(CONST_CAST(struct cls_rule
*, &rule
->cr
), cr
);
4907 ovs_refcount_init(&rule
->ref_count
);
4909 ovs_mutex_init(&rule
->mutex
);
4910 ovs_mutex_lock(&rule
->mutex
);
4911 *CONST_CAST(ovs_be64
*, &rule
->flow_cookie
) = new_cookie
;
4912 rule
->created
= rule
->modified
= time_msec();
4913 rule
->idle_timeout
= idle_timeout
;
4914 rule
->hard_timeout
= hard_timeout
;
4915 *CONST_CAST(uint16_t *, &rule
->importance
) = importance
;
4916 rule
->removed_reason
= OVS_OFPRR_NONE
;
4918 *CONST_CAST(uint8_t *, &rule
->table_id
) = table_id
;
4919 rule
->flags
= flags
& OFPUTIL_FF_STATE
;
4921 *CONST_CAST(const struct rule_actions
**, &rule
->actions
)
4922 = rule_actions_create(ofpacts
, ofpacts_len
);
4924 ovs_list_init(&rule
->meter_list_node
);
4925 rule
->eviction_group
= NULL
;
4926 rule
->monitor_flags
= 0;
4927 rule
->add_seqno
= 0;
4928 rule
->modify_seqno
= 0;
4929 ovs_list_init(&rule
->expirable
);
4930 ovs_mutex_unlock(&rule
->mutex
);
4932 /* Construct rule, initializing derived state. */
4933 error
= ofproto
->ofproto_class
->rule_construct(rule
);
4935 ofproto_rule_destroy__(rule
);
4939 rule
->state
= RULE_INITIALIZED
;
4940 rule
->match_tlv_bitmap
= match_tlv_bitmap
;
4941 rule
->ofpacts_tlv_bitmap
= ofpacts_tlv_bitmap
;
4942 mf_vl_mff_ref(&rule
->ofproto
->vl_mff_map
, match_tlv_bitmap
);
4943 mf_vl_mff_ref(&rule
->ofproto
->vl_mff_map
, ofpacts_tlv_bitmap
);
4949 /* Initialize 'ofm' for a learn action. If the rule already existed, reference
4950 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
4951 * rule reference in both. This does not take the global 'ofproto_mutex'. */
4953 ofproto_flow_mod_init_for_learn(struct ofproto
*ofproto
,
4954 const struct ofputil_flow_mod
*fm
,
4955 struct ofproto_flow_mod
*ofm
)
4956 OVS_EXCLUDED(ofproto_mutex
)
4960 /* Reject flow mods that do not look like they were generated by a learn
4962 if (fm
->command
!= OFPFC_MODIFY_STRICT
|| fm
->table_id
== OFPTT_ALL
4963 || fm
->flags
& OFPUTIL_FF_RESET_COUNTS
4964 || fm
->buffer_id
!= UINT32_MAX
) {
4965 return OFPERR_OFPFMFC_UNKNOWN
;
4968 /* Check if the rule already exists, and we can get a reference to it. */
4969 struct oftable
*table
= &ofproto
->tables
[fm
->table_id
];
4972 rule
= rule_from_cls_rule(classifier_find_match_exactly(
4973 &table
->cls
, &fm
->match
, fm
->priority
,
4976 /* Check if the rule's attributes match as well. */
4977 const struct rule_actions
*actions
;
4979 ovs_mutex_lock(&rule
->mutex
);
4980 actions
= rule_get_actions(rule
);
4981 if (rule
->idle_timeout
== fm
->idle_timeout
4982 && rule
->hard_timeout
== fm
->hard_timeout
4983 && rule
->importance
== fm
->importance
4984 && rule
->flags
== (fm
->flags
& OFPUTIL_FF_STATE
)
4985 && (!fm
->modify_cookie
|| (fm
->new_cookie
== rule
->flow_cookie
))
4986 && ofpacts_equal(fm
->ofpacts
, fm
->ofpacts_len
,
4987 actions
->ofpacts
, actions
->ofpacts_len
)) {
4988 /* Rule already exists and need not change, except for the modified
4989 * timestamp. Get a reference to the existing rule. */
4990 ovs_mutex_unlock(&rule
->mutex
);
4991 if (!ofproto_rule_try_ref(rule
)) {
4992 rule
= NULL
; /* Pretend it did not exist. */
4995 ovs_mutex_unlock(&rule
->mutex
);
5000 /* Initialize ofproto_flow_mod for future use. */
5001 error
= ofproto_flow_mod_init(ofproto
, ofm
, fm
, rule
);
5003 ofproto_rule_unref(rule
);
5010 ofproto_flow_mod_learn_refresh(struct ofproto_flow_mod
*ofm
)
5012 enum ofperr error
= 0;
5014 /* ofm->temp_rule is our reference to the learned rule. We have a
5015 * reference to an existing rule, if it already was in the classifier,
5016 * otherwise we may have a fresh rule that we need to insert. */
5017 struct rule
*rule
= ofm
->temp_rule
;
5019 return OFPERR_OFPFMFC_UNKNOWN
;
5022 /* Create a new rule if the current one has been removed from the
5023 * classifier. We need to do this since RCU does not allow a current rule
5024 * to be reinserted before all threads have quiesced.
5026 * It is possible that the rule is removed asynchronously, e.g., right
5027 * after we have read the 'rule->state' below. In this case the next time
5028 * this function is executed the rule will be reinstated. */
5029 if (rule
->state
== RULE_REMOVED
) {
5032 cls_rule_clone(&cr
, &rule
->cr
);
5033 ovs_mutex_lock(&rule
->mutex
);
5034 error
= ofproto_rule_create(rule
->ofproto
, &cr
, rule
->table_id
,
5037 rule
->hard_timeout
, rule
->flags
,
5039 rule
->actions
->ofpacts
,
5040 rule
->actions
->ofpacts_len
,
5041 rule
->match_tlv_bitmap
,
5042 rule
->ofpacts_tlv_bitmap
,
5044 ovs_mutex_unlock(&rule
->mutex
);
5046 ofproto_rule_unref(rule
); /* Release old reference. */
5049 /* Refresh the existing rule. */
5050 ovs_mutex_lock(&rule
->mutex
);
5051 rule
->modified
= time_msec();
5052 ovs_mutex_unlock(&rule
->mutex
);
5058 ofproto_flow_mod_learn_start(struct ofproto_flow_mod
*ofm
)
5059 OVS_REQUIRES(ofproto_mutex
)
5061 struct rule
*rule
= ofm
->temp_rule
;
5063 /* ofproto_flow_mod_start() consumes the reference, so we
5064 * take a new one. */
5065 ofproto_rule_ref(rule
);
5066 enum ofperr error
= ofproto_flow_mod_start(rule
->ofproto
, ofm
);
5067 ofm
->temp_rule
= rule
;
5073 ofproto_flow_mod_learn_revert(struct ofproto_flow_mod
*ofm
)
5074 OVS_REQUIRES(ofproto_mutex
)
5076 struct rule
*rule
= rule_collection_rules(&ofm
->new_rules
)[0];
5077 ofproto_flow_mod_revert(rule
->ofproto
, ofm
);
5081 ofproto_flow_mod_learn_finish(struct ofproto_flow_mod
*ofm
,
5082 struct ofproto
*orig_ofproto
)
5083 OVS_REQUIRES(ofproto_mutex
)
5085 struct rule
*rule
= rule_collection_rules(&ofm
->new_rules
)[0];
5087 /* If learning on a different bridge, must bump its version
5088 * number and flush connmgr afterwards. */
5089 if (rule
->ofproto
!= orig_ofproto
) {
5090 ofproto_bump_tables_version(rule
->ofproto
);
5092 ofproto_flow_mod_finish(rule
->ofproto
, ofm
, NULL
);
5093 if (rule
->ofproto
!= orig_ofproto
) {
5094 ofmonitor_flush(rule
->ofproto
->connmgr
);
5098 /* Refresh 'ofm->temp_rule', for which the caller holds a reference, if already
5099 * in the classifier, insert it otherwise. If the rule has already been
5100 * removed from the classifier, a new rule is created using 'ofm->temp_rule' as
5101 * a template and the reference to the old 'ofm->temp_rule' is freed. If
5102 * 'keep_ref' is true, then a reference to the current rule is held, otherwise
5103 * it is released and 'ofm->temp_rule' is set to NULL.
5105 * If 'limit' != 0, insertion will fail if there are more than 'limit' rules
5106 * in the same table with the same cookie. If insertion succeeds,
5107 * '*below_limitp' will be set to true. If insertion fails '*below_limitp'
5108 * will be set to false.
5110 * Caller needs to be the exclusive owner of 'ofm' as it is being manipulated
5111 * during the call. */
5113 ofproto_flow_mod_learn(struct ofproto_flow_mod
*ofm
, bool keep_ref
,
5114 unsigned limit
, bool *below_limitp
)
5115 OVS_EXCLUDED(ofproto_mutex
)
5117 enum ofperr error
= ofproto_flow_mod_learn_refresh(ofm
);
5118 struct rule
*rule
= ofm
->temp_rule
;
5119 bool below_limit
= true;
5121 /* Do we need to insert the rule? */
5122 if (!error
&& rule
->state
== RULE_INITIALIZED
) {
5123 ovs_mutex_lock(&ofproto_mutex
);
5126 struct rule_criteria criteria
;
5127 struct rule_collection rules
;
5130 match_init_catchall(&match
);
5131 rule_criteria_init(&criteria
, rule
->table_id
, &match
, 0,
5132 OVS_VERSION_MAX
, rule
->flow_cookie
,
5133 OVS_BE64_MAX
, OFPP_ANY
, OFPG_ANY
);
5134 rule_criteria_require_rw(&criteria
, false);
5135 collect_rules_loose(rule
->ofproto
, &criteria
, &rules
);
5136 if (rule_collection_n(&rules
) >= limit
) {
5137 below_limit
= false;
5139 rule_collection_destroy(&rules
);
5140 rule_criteria_destroy(&criteria
);
5144 ofm
->version
= rule
->ofproto
->tables_version
+ 1;
5146 error
= ofproto_flow_mod_learn_start(ofm
);
5148 ofproto_flow_mod_learn_finish(ofm
, NULL
);
5151 ofproto_flow_mod_uninit(ofm
);
5153 ovs_mutex_unlock(&ofproto_mutex
);
5156 static struct vlog_rate_limit learn_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5157 VLOG_INFO_RL(&learn_rl
, "Learn limit for flow %"PRIu64
" reached.",
5162 if (!keep_ref
&& below_limit
) {
5163 ofproto_rule_unref(rule
);
5164 ofm
->temp_rule
= NULL
;
5167 *below_limitp
= below_limit
;
5173 replace_rule_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5174 struct rule
*old_rule
, struct rule
*new_rule
)
5176 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
5178 /* 'old_rule' may be either an evicted rule or replaced rule. */
5180 /* Copy values from old rule for modify semantics. */
5181 if (old_rule
->removed_reason
!= OFPRR_EVICTION
) {
5182 bool change_cookie
= (ofm
->modify_cookie
5183 && new_rule
->flow_cookie
!= OVS_BE64_MAX
5184 && new_rule
->flow_cookie
!= old_rule
->flow_cookie
);
5186 ovs_mutex_lock(&new_rule
->mutex
);
5187 ovs_mutex_lock(&old_rule
->mutex
);
5188 if (ofm
->command
!= OFPFC_ADD
) {
5189 new_rule
->idle_timeout
= old_rule
->idle_timeout
;
5190 new_rule
->hard_timeout
= old_rule
->hard_timeout
;
5191 *CONST_CAST(uint16_t *, &new_rule
->importance
) = old_rule
->importance
;
5192 new_rule
->flags
= old_rule
->flags
;
5193 new_rule
->created
= old_rule
->created
;
5195 if (!change_cookie
) {
5196 *CONST_CAST(ovs_be64
*, &new_rule
->flow_cookie
)
5197 = old_rule
->flow_cookie
;
5199 ovs_mutex_unlock(&old_rule
->mutex
);
5200 ovs_mutex_unlock(&new_rule
->mutex
);
5203 /* Mark the old rule for removal in the next version. */
5204 cls_rule_make_invisible_in_version(&old_rule
->cr
, ofm
->version
);
5206 /* Remove the old rule from data structures. */
5207 ofproto_rule_remove__(ofproto
, old_rule
);
5211 /* Insert flow to ofproto data structures, so that later flow_mods may
5212 * relate to it. This is reversible, in case later errors require this to
5214 ofproto_rule_insert__(ofproto
, new_rule
);
5215 /* Make the new rule visible for classifier lookups only from the next
5217 classifier_insert(&table
->cls
, &new_rule
->cr
, ofm
->version
, ofm
->conjs
,
5222 replace_rule_revert(struct ofproto
*ofproto
,
5223 struct rule
*old_rule
, struct rule
*new_rule
)
5225 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
5228 if (old_rule
->removed_reason
== OFPRR_EVICTION
) {
5229 /* Revert the eviction. */
5230 eviction_group_add_rule(old_rule
);
5233 /* Restore the old rule to data structures. */
5234 ofproto_rule_insert__(ofproto
, old_rule
);
5236 /* Restore the original visibility of the old rule. */
5237 cls_rule_restore_visibility(&old_rule
->cr
);
5239 /* Restore table's rule count. */
5243 /* Remove the new rule immediately. It was never visible to lookups. */
5244 if (!classifier_remove(&table
->cls
, &new_rule
->cr
)) {
5247 ofproto_rule_remove__(ofproto
, new_rule
);
5248 ofproto_rule_unref(new_rule
);
5251 /* Adds the 'new_rule', replacing the 'old_rule'. */
5253 replace_rule_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5254 const struct openflow_mod_requester
*req
,
5255 struct rule
*old_rule
, struct rule
*new_rule
,
5256 struct ovs_list
*dead_cookies
)
5257 OVS_REQUIRES(ofproto_mutex
)
5259 struct rule
*replaced_rule
;
5261 replaced_rule
= (old_rule
&& old_rule
->removed_reason
!= OFPRR_EVICTION
)
5264 /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule'
5265 * is a duplicate rule the 'new_rule' is replacing. The provider should
5266 * link the packet and byte counts from the old rule to the new one if
5267 * 'modify_keep_counts' is 'true'. The 'replaced_rule' will be deleted
5268 * right after this call. */
5269 ofproto
->ofproto_class
->rule_insert(new_rule
, replaced_rule
,
5270 ofm
->modify_keep_counts
);
5271 learned_cookies_inc(ofproto
, rule_get_actions(new_rule
));
5274 const struct rule_actions
*old_actions
= rule_get_actions(old_rule
);
5275 const struct rule_actions
*new_actions
= rule_get_actions(new_rule
);
5277 learned_cookies_dec(ofproto
, old_actions
, dead_cookies
);
5279 if (replaced_rule
) {
5280 enum nx_flow_update_event event
= ofm
->command
== OFPFC_ADD
5281 ? NXFME_ADDED
: NXFME_MODIFIED
;
5283 bool changed_cookie
= (new_rule
->flow_cookie
5284 != old_rule
->flow_cookie
);
5286 bool changed_actions
= !ofpacts_equal(new_actions
->ofpacts
,
5287 new_actions
->ofpacts_len
,
5288 old_actions
->ofpacts
,
5289 old_actions
->ofpacts_len
);
5291 if (event
!= NXFME_MODIFIED
|| changed_actions
5292 || changed_cookie
) {
5293 ofmonitor_report(ofproto
->connmgr
, new_rule
, event
, 0,
5294 req
? req
->ofconn
: NULL
,
5295 req
? req
->request
->xid
: 0,
5296 changed_actions
? old_actions
: NULL
);
5299 /* XXX: This is slight duplication with delete_flows_finish__() */
5300 ofmonitor_report(ofproto
->connmgr
, old_rule
, NXFME_DELETED
,
5302 req
? req
->ofconn
: NULL
,
5303 req
? req
->request
->xid
: 0, NULL
);
5308 /* ofm->temp_rule is consumed only in the successful case. */
5310 modify_flows_start__(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5311 OVS_REQUIRES(ofproto_mutex
)
5313 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5314 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5317 if (rule_collection_n(old_rules
) > 0) {
5318 /* Create a new 'modified' rule for each old rule. */
5319 struct rule
*old_rule
, *new_rule
;
5320 const struct rule_actions
*actions
= rule_get_actions(ofm
->temp_rule
);
5322 /* Must check actions while holding ofproto_mutex to avoid a race. */
5323 error
= ofproto_check_ofpacts(ofproto
, actions
->ofpacts
,
5324 actions
->ofpacts_len
);
5329 /* Use the temp rule as the first new rule, and as the template for
5331 struct rule
*temp
= ofm
->temp_rule
;
5332 ofm
->temp_rule
= NULL
; /* We consume the template. */
5335 RULE_COLLECTION_FOR_EACH (old_rule
, old_rules
) {
5337 /* The template rule's match is possibly a loose one, so it
5338 * must be replaced with the old rule's match so that the new
5339 * rule actually replaces the old one. */
5340 cls_rule_destroy(CONST_CAST(struct cls_rule
*, &temp
->cr
));
5341 cls_rule_clone(CONST_CAST(struct cls_rule
*, &temp
->cr
),
5343 if (temp
->match_tlv_bitmap
!= old_rule
->match_tlv_bitmap
) {
5344 mf_vl_mff_unref(&temp
->ofproto
->vl_mff_map
,
5345 temp
->match_tlv_bitmap
);
5346 temp
->match_tlv_bitmap
= old_rule
->match_tlv_bitmap
;
5347 mf_vl_mff_ref(&temp
->ofproto
->vl_mff_map
,
5348 temp
->match_tlv_bitmap
);
5350 *CONST_CAST(uint8_t *, &temp
->table_id
) = old_rule
->table_id
;
5351 rule_collection_add(new_rules
, temp
);
5355 cls_rule_clone(&cr
, &old_rule
->cr
);
5356 error
= ofproto_rule_create(ofproto
, &cr
, old_rule
->table_id
,
5359 temp
->hard_timeout
, temp
->flags
,
5361 temp
->actions
->ofpacts
,
5362 temp
->actions
->ofpacts_len
,
5363 old_rule
->match_tlv_bitmap
,
5364 temp
->ofpacts_tlv_bitmap
,
5367 rule_collection_add(new_rules
, new_rule
);
5369 /* Return the template rule in place in the error case. */
5370 ofm
->temp_rule
= temp
;
5371 rule_collection_rules(new_rules
)[0] = NULL
;
5373 rule_collection_unref(new_rules
);
5374 rule_collection_destroy(new_rules
);
5379 ovs_assert(rule_collection_n(new_rules
)
5380 == rule_collection_n(old_rules
));
5382 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5383 replace_rule_start(ofproto
, ofm
, old_rule
, new_rule
);
5385 } else if (ofm
->modify_may_add_flow
) {
5386 /* No match, add a new flow, consumes 'temp'. */
5387 error
= add_flow_start(ofproto
, ofm
);
5389 /* No flow to modify and may not add a flow. */
5390 ofproto_rule_unref(ofm
->temp_rule
);
5391 ofm
->temp_rule
= NULL
; /* We consume the template. */
5399 modify_flows_init_loose(struct ofproto
*ofproto
,
5400 struct ofproto_flow_mod
*ofm
,
5401 const struct ofputil_flow_mod
*fm
)
5402 OVS_EXCLUDED(ofproto_mutex
)
5404 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, 0,
5405 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
,
5407 rule_criteria_require_rw(&ofm
->criteria
,
5408 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5409 /* Must create a new flow in advance for the case that no matches are
5410 * found. Also used for template for multiple modified flows. */
5411 add_flow_init(ofproto
, ofm
, fm
);
5416 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5419 modify_flows_start_loose(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5420 OVS_REQUIRES(ofproto_mutex
)
5422 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5425 error
= collect_rules_loose(ofproto
, &ofm
->criteria
, old_rules
);
5428 error
= modify_flows_start__(ofproto
, ofm
);
5432 rule_collection_destroy(old_rules
);
5439 modify_flows_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5440 OVS_REQUIRES(ofproto_mutex
)
5442 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5443 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5445 /* Old rules were not changed yet, only need to revert new rules. */
5446 if (rule_collection_n(old_rules
) > 0) {
5447 struct rule
*old_rule
, *new_rule
;
5448 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5449 replace_rule_revert(ofproto
, old_rule
, new_rule
);
5451 rule_collection_destroy(new_rules
);
5452 rule_collection_destroy(old_rules
);
5457 modify_flows_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5458 const struct openflow_mod_requester
*req
)
5459 OVS_REQUIRES(ofproto_mutex
)
5461 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5462 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5464 if (rule_collection_n(old_rules
) == 0
5465 && rule_collection_n(new_rules
) == 1) {
5466 add_flow_finish(ofproto
, ofm
, req
);
5467 } else if (rule_collection_n(old_rules
) > 0) {
5468 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
5470 ovs_assert(rule_collection_n(new_rules
)
5471 == rule_collection_n(old_rules
));
5473 struct rule
*old_rule
, *new_rule
;
5474 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5475 replace_rule_finish(ofproto
, ofm
, req
, old_rule
, new_rule
,
5478 learned_cookies_flush(ofproto
, &dead_cookies
);
5479 remove_rules_postponed(old_rules
);
5484 modify_flow_init_strict(struct ofproto
*ofproto OVS_UNUSED
,
5485 struct ofproto_flow_mod
*ofm
,
5486 const struct ofputil_flow_mod
*fm
)
5487 OVS_EXCLUDED(ofproto_mutex
)
5489 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
5490 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
,
5492 rule_criteria_require_rw(&ofm
->criteria
,
5493 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5494 /* Must create a new flow in advance for the case that no matches are
5495 * found. Also used for template for multiple modified flows. */
5496 add_flow_init(ofproto
, ofm
, fm
);
5501 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
5502 * code on failure. */
5504 modify_flow_start_strict(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5505 OVS_REQUIRES(ofproto_mutex
)
5507 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5510 error
= collect_rules_strict(ofproto
, &ofm
->criteria
, old_rules
);
5513 /* collect_rules_strict() can return max 1 rule. */
5514 error
= modify_flows_start__(ofproto
, ofm
);
5520 /* OFPFC_DELETE implementation. */
5523 delete_flows_start__(struct ofproto
*ofproto
, ovs_version_t version
,
5524 const struct rule_collection
*rules
)
5525 OVS_REQUIRES(ofproto_mutex
)
5529 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5530 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5533 cls_rule_make_invisible_in_version(&rule
->cr
, version
);
5535 /* Remove rule from ofproto data structures. */
5536 ofproto_rule_remove__(ofproto
, rule
);
5541 delete_flows_revert__(struct ofproto
*ofproto
,
5542 const struct rule_collection
*rules
)
5543 OVS_REQUIRES(ofproto_mutex
)
5547 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5548 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5550 /* Add rule back to ofproto data structures. */
5551 ofproto_rule_insert__(ofproto
, rule
);
5553 /* Restore table's rule count. */
5556 /* Restore the original visibility of the rule. */
5557 cls_rule_restore_visibility(&rule
->cr
);
5562 delete_flows_finish__(struct ofproto
*ofproto
,
5563 struct rule_collection
*rules
,
5564 enum ofp_flow_removed_reason reason
,
5565 const struct openflow_mod_requester
*req
)
5566 OVS_REQUIRES(ofproto_mutex
)
5568 if (rule_collection_n(rules
)) {
5569 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
5572 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5573 /* This value will be used to send the flow removed message right
5574 * before the rule is actually destroyed. */
5575 rule
->removed_reason
= reason
;
5577 ofmonitor_report(ofproto
->connmgr
, rule
, NXFME_DELETED
, reason
,
5578 req
? req
->ofconn
: NULL
,
5579 req
? req
->request
->xid
: 0, NULL
);
5581 /* Send Vacancy Event for OF1.4+. */
5582 send_table_status(ofproto
, rule
->table_id
);
5584 learned_cookies_dec(ofproto
, rule_get_actions(rule
),
5587 remove_rules_postponed(rules
);
5589 learned_cookies_flush(ofproto
, &dead_cookies
);
5593 /* Deletes the rules listed in 'rules'.
5594 * The deleted rules will become invisible to the lookups in the next version.
5595 * Destroys 'rules'. */
5597 delete_flows__(struct rule_collection
*rules
,
5598 enum ofp_flow_removed_reason reason
,
5599 const struct openflow_mod_requester
*req
)
5600 OVS_REQUIRES(ofproto_mutex
)
5602 if (rule_collection_n(rules
)) {
5603 struct ofproto
*ofproto
= rule_collection_rules(rules
)[0]->ofproto
;
5605 delete_flows_start__(ofproto
, ofproto
->tables_version
+ 1, rules
);
5606 ofproto_bump_tables_version(ofproto
);
5607 delete_flows_finish__(ofproto
, rules
, reason
, req
);
5608 ofmonitor_flush(ofproto
->connmgr
);
5613 delete_flows_init_loose(struct ofproto
*ofproto OVS_UNUSED
,
5614 struct ofproto_flow_mod
*ofm
,
5615 const struct ofputil_flow_mod
*fm
)
5616 OVS_EXCLUDED(ofproto_mutex
)
5618 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, 0,
5619 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
,
5620 fm
->out_port
, fm
->out_group
);
5621 rule_criteria_require_rw(&ofm
->criteria
,
5622 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5626 /* Implements OFPFC_DELETE. */
5628 delete_flows_start_loose(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5629 OVS_REQUIRES(ofproto_mutex
)
5631 struct rule_collection
*rules
= &ofm
->old_rules
;
5634 error
= collect_rules_loose(ofproto
, &ofm
->criteria
, rules
);
5637 delete_flows_start__(ofproto
, ofm
->version
, rules
);
5644 delete_flows_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5645 OVS_REQUIRES(ofproto_mutex
)
5647 delete_flows_revert__(ofproto
, &ofm
->old_rules
);
5651 delete_flows_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5652 const struct openflow_mod_requester
*req
)
5653 OVS_REQUIRES(ofproto_mutex
)
5655 delete_flows_finish__(ofproto
, &ofm
->old_rules
, OFPRR_DELETE
, req
);
5659 delete_flows_init_strict(struct ofproto
*ofproto OVS_UNUSED
,
5660 struct ofproto_flow_mod
*ofm
,
5661 const struct ofputil_flow_mod
*fm
)
5662 OVS_EXCLUDED(ofproto_mutex
)
5664 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
5665 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
,
5666 fm
->out_port
, fm
->out_group
);
5667 rule_criteria_require_rw(&ofm
->criteria
,
5668 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5672 /* Implements OFPFC_DELETE_STRICT. */
5674 delete_flow_start_strict(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5675 OVS_REQUIRES(ofproto_mutex
)
5677 struct rule_collection
*rules
= &ofm
->old_rules
;
5680 error
= collect_rules_strict(ofproto
, &ofm
->criteria
, rules
);
5683 delete_flows_start__(ofproto
, ofm
->version
, rules
);
5689 /* This may only be called by rule_destroy_cb()! */
5691 ofproto_rule_send_removed(struct rule
*rule
)
5692 OVS_EXCLUDED(ofproto_mutex
)
5694 struct ofputil_flow_removed fr
;
5697 minimatch_expand(&rule
->cr
.match
, &fr
.match
);
5698 fr
.priority
= rule
->cr
.priority
;
5700 /* Synchronize with connmgr_destroy() calls to prevent connmgr disappearing
5701 * while we use it. */
5702 ovs_mutex_lock(&ofproto_mutex
);
5703 struct connmgr
*connmgr
= rule
->ofproto
->connmgr
;
5705 ovs_mutex_unlock(&ofproto_mutex
);
5709 fr
.cookie
= rule
->flow_cookie
;
5710 fr
.reason
= rule
->removed_reason
;
5711 fr
.table_id
= rule
->table_id
;
5712 calc_duration(rule
->created
, time_msec(),
5713 &fr
.duration_sec
, &fr
.duration_nsec
);
5714 ovs_mutex_lock(&rule
->mutex
);
5715 fr
.idle_timeout
= rule
->idle_timeout
;
5716 fr
.hard_timeout
= rule
->hard_timeout
;
5717 ovs_mutex_unlock(&rule
->mutex
);
5718 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &fr
.packet_count
,
5719 &fr
.byte_count
, &used
);
5720 connmgr_send_flow_removed(connmgr
, &fr
);
5721 ovs_mutex_unlock(&ofproto_mutex
);
5724 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
5725 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
5728 * ofproto implementation ->run() functions should use this function to expire
5729 * OpenFlow flows. */
5731 ofproto_rule_expire(struct rule
*rule
, uint8_t reason
)
5732 OVS_REQUIRES(ofproto_mutex
)
5734 struct rule_collection rules
;
5736 rule_collection_init(&rules
);
5737 rule_collection_add(&rules
, rule
);
5738 delete_flows__(&rules
, reason
, NULL
);
5741 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5742 * means "infinite". */
5744 reduce_timeout(uint16_t max
, uint16_t *timeout
)
5746 if (max
&& (!*timeout
|| *timeout
> max
)) {
5751 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
5752 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
5753 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
5755 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
5757 ofproto_rule_reduce_timeouts__(struct rule
*rule
,
5758 uint16_t idle_timeout
, uint16_t hard_timeout
)
5759 OVS_REQUIRES(ofproto_mutex
)
5760 OVS_EXCLUDED(rule
->mutex
)
5762 if (!idle_timeout
&& !hard_timeout
) {
5766 if (ovs_list_is_empty(&rule
->expirable
)) {
5767 ovs_list_insert(&rule
->ofproto
->expirable
, &rule
->expirable
);
5770 ovs_mutex_lock(&rule
->mutex
);
5771 reduce_timeout(idle_timeout
, &rule
->idle_timeout
);
5772 reduce_timeout(hard_timeout
, &rule
->hard_timeout
);
5773 ovs_mutex_unlock(&rule
->mutex
);
5777 ofproto_rule_reduce_timeouts(struct rule
*rule
,
5778 uint16_t idle_timeout
, uint16_t hard_timeout
)
5779 OVS_EXCLUDED(ofproto_mutex
, rule
->mutex
)
5781 if (!idle_timeout
&& !hard_timeout
) {
5785 ovs_mutex_lock(&ofproto_mutex
);
5786 if (ovs_list_is_empty(&rule
->expirable
)) {
5787 ovs_list_insert(&rule
->ofproto
->expirable
, &rule
->expirable
);
5789 ovs_mutex_unlock(&ofproto_mutex
);
5791 ovs_mutex_lock(&rule
->mutex
);
5792 reduce_timeout(idle_timeout
, &rule
->idle_timeout
);
5793 reduce_timeout(hard_timeout
, &rule
->hard_timeout
);
5794 ovs_mutex_unlock(&rule
->mutex
);
5798 handle_flow_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5799 OVS_EXCLUDED(ofproto_mutex
)
5801 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5802 struct ofputil_flow_mod fm
;
5803 uint64_t ofpacts_stub
[1024 / 8];
5804 struct ofpbuf ofpacts
;
5807 error
= reject_slave_controller(ofconn
);
5812 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
5813 error
= ofputil_decode_flow_mod(&fm
, oh
, ofconn_get_protocol(ofconn
),
5814 ofproto_get_tun_tab(ofproto
),
5815 &ofproto
->vl_mff_map
, &ofpacts
,
5816 u16_to_ofp(ofproto
->max_ports
),
5819 struct openflow_mod_requester req
= { ofconn
, oh
};
5820 error
= handle_flow_mod__(ofproto
, &fm
, &req
);
5823 ofpbuf_uninit(&ofpacts
);
5828 handle_flow_mod__(struct ofproto
*ofproto
, const struct ofputil_flow_mod
*fm
,
5829 const struct openflow_mod_requester
*req
)
5830 OVS_EXCLUDED(ofproto_mutex
)
5832 struct ofproto_flow_mod ofm
;
5835 error
= ofproto_flow_mod_init(ofproto
, &ofm
, fm
, NULL
);
5840 ovs_mutex_lock(&ofproto_mutex
);
5841 ofm
.version
= ofproto
->tables_version
+ 1;
5842 error
= ofproto_flow_mod_start(ofproto
, &ofm
);
5844 ofproto_bump_tables_version(ofproto
);
5845 ofproto_flow_mod_finish(ofproto
, &ofm
, req
);
5846 ofmonitor_flush(ofproto
->connmgr
);
5848 ovs_mutex_unlock(&ofproto_mutex
);
5854 handle_role_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5856 struct ofputil_role_request request
;
5857 struct ofputil_role_request reply
;
5861 error
= ofputil_decode_role_message(oh
, &request
);
5866 if (request
.role
!= OFPCR12_ROLE_NOCHANGE
) {
5867 if (request
.role
!= OFPCR12_ROLE_EQUAL
5868 && request
.have_generation_id
5869 && !ofconn_set_master_election_id(ofconn
, request
.generation_id
)) {
5870 return OFPERR_OFPRRFC_STALE
;
5873 ofconn_set_role(ofconn
, request
.role
);
5876 reply
.role
= ofconn_get_role(ofconn
);
5877 reply
.have_generation_id
= ofconn_get_master_election_id(
5878 ofconn
, &reply
.generation_id
);
5879 buf
= ofputil_encode_role_reply(oh
, &reply
);
5880 ofconn_send_reply(ofconn
, buf
);
5886 handle_nxt_flow_mod_table_id(struct ofconn
*ofconn
,
5887 const struct ofp_header
*oh
)
5889 const struct nx_flow_mod_table_id
*msg
= ofpmsg_body(oh
);
5890 enum ofputil_protocol cur
, next
;
5892 cur
= ofconn_get_protocol(ofconn
);
5893 next
= ofputil_protocol_set_tid(cur
, msg
->set
!= 0);
5894 ofconn_set_protocol(ofconn
, next
);
5900 handle_nxt_set_flow_format(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5902 const struct nx_set_flow_format
*msg
= ofpmsg_body(oh
);
5903 enum ofputil_protocol cur
, next
;
5904 enum ofputil_protocol next_base
;
5906 next_base
= ofputil_nx_flow_format_to_protocol(ntohl(msg
->format
));
5908 return OFPERR_OFPBRC_EPERM
;
5911 cur
= ofconn_get_protocol(ofconn
);
5912 next
= ofputil_protocol_set_base(cur
, next_base
);
5913 ofconn_set_protocol(ofconn
, next
);
5919 handle_nxt_set_packet_in_format(struct ofconn
*ofconn
,
5920 const struct ofp_header
*oh
)
5922 const struct nx_set_packet_in_format
*msg
= ofpmsg_body(oh
);
5925 format
= ntohl(msg
->format
);
5926 if (!ofputil_packet_in_format_is_valid(format
)) {
5927 return OFPERR_OFPBRC_EPERM
;
5930 ofconn_set_packet_in_format(ofconn
, format
);
5935 handle_nxt_set_async_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5937 struct ofputil_async_cfg basis
= ofconn_get_async_config(ofconn
);
5938 struct ofputil_async_cfg ac
;
5941 error
= ofputil_decode_set_async_config(oh
, false, &basis
, &ac
);
5946 ofconn_set_async_config(ofconn
, &ac
);
5947 if (ofconn_get_type(ofconn
) == OFCONN_SERVICE
&&
5948 !ofconn_get_miss_send_len(ofconn
)) {
5949 ofconn_set_miss_send_len(ofconn
, OFP_DEFAULT_MISS_SEND_LEN
);
5956 handle_nxt_get_async_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5958 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
5959 ofconn_send_reply(ofconn
, ofputil_encode_get_async_reply(oh
, &ac
));
5965 handle_nxt_set_controller_id(struct ofconn
*ofconn
,
5966 const struct ofp_header
*oh
)
5968 const struct nx_controller_id
*nci
= ofpmsg_body(oh
);
5970 if (!is_all_zeros(nci
->zero
, sizeof nci
->zero
)) {
5971 return OFPERR_NXBRC_MUST_BE_ZERO
;
5974 ofconn_set_controller_id(ofconn
, ntohs(nci
->controller_id
));
5979 handle_barrier_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5983 buf
= ofpraw_alloc_reply((oh
->version
== OFP10_VERSION
5984 ? OFPRAW_OFPT10_BARRIER_REPLY
5985 : OFPRAW_OFPT11_BARRIER_REPLY
), oh
, 0);
5986 ofconn_send_reply(ofconn
, buf
);
5991 ofproto_compose_flow_refresh_update(const struct rule
*rule
,
5992 enum nx_flow_monitor_flags flags
,
5993 struct ovs_list
*msgs
,
5994 const struct tun_table
*tun_table
)
5995 OVS_REQUIRES(ofproto_mutex
)
5997 const struct rule_actions
*actions
;
5998 struct ofputil_flow_update fu
;
6000 fu
.event
= (flags
& (NXFMF_INITIAL
| NXFMF_ADD
)
6001 ? NXFME_ADDED
: NXFME_MODIFIED
);
6003 ovs_mutex_lock(&rule
->mutex
);
6004 fu
.idle_timeout
= rule
->idle_timeout
;
6005 fu
.hard_timeout
= rule
->hard_timeout
;
6006 ovs_mutex_unlock(&rule
->mutex
);
6007 fu
.table_id
= rule
->table_id
;
6008 fu
.cookie
= rule
->flow_cookie
;
6009 minimatch_expand(&rule
->cr
.match
, &fu
.match
);
6010 fu
.priority
= rule
->cr
.priority
;
6012 actions
= flags
& NXFMF_ACTIONS
? rule_get_actions(rule
) : NULL
;
6013 fu
.ofpacts
= actions
? actions
->ofpacts
: NULL
;
6014 fu
.ofpacts_len
= actions
? actions
->ofpacts_len
: 0;
6016 if (ovs_list_is_empty(msgs
)) {
6017 ofputil_start_flow_update(msgs
);
6019 ofputil_append_flow_update(&fu
, msgs
, tun_table
);
6023 ofmonitor_compose_refresh_updates(struct rule_collection
*rules
,
6024 struct ovs_list
*msgs
)
6025 OVS_REQUIRES(ofproto_mutex
)
6029 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
6030 enum nx_flow_monitor_flags flags
= rule
->monitor_flags
;
6031 rule
->monitor_flags
= 0;
6033 ofproto_compose_flow_refresh_update(rule
, flags
, msgs
,
6034 ofproto_get_tun_tab(rule
->ofproto
));
6039 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor
*m
,
6040 struct rule
*rule
, uint64_t seqno
,
6041 struct rule_collection
*rules
)
6042 OVS_REQUIRES(ofproto_mutex
)
6044 enum nx_flow_monitor_flags update
;
6046 if (rule_is_hidden(rule
)) {
6050 if (!ofproto_rule_has_out_port(rule
, m
->out_port
)) {
6055 if (rule
->add_seqno
> seqno
) {
6056 update
= NXFMF_ADD
| NXFMF_MODIFY
;
6057 } else if (rule
->modify_seqno
> seqno
) {
6058 update
= NXFMF_MODIFY
;
6063 if (!(m
->flags
& update
)) {
6067 update
= NXFMF_INITIAL
;
6070 if (!rule
->monitor_flags
) {
6071 rule_collection_add(rules
, rule
);
6073 rule
->monitor_flags
|= update
| (m
->flags
& NXFMF_ACTIONS
);
6077 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor
*m
,
6079 struct rule_collection
*rules
)
6080 OVS_REQUIRES(ofproto_mutex
)
6082 const struct ofproto
*ofproto
= ofconn_get_ofproto(m
->ofconn
);
6083 const struct oftable
*table
;
6084 struct cls_rule target
;
6086 cls_rule_init_from_minimatch(&target
, &m
->match
, 0);
6087 FOR_EACH_MATCHING_TABLE (table
, m
->table_id
, ofproto
) {
6090 CLS_FOR_EACH_TARGET (rule
, cr
, &table
->cls
, &target
, OVS_VERSION_MAX
) {
6091 ofproto_collect_ofmonitor_refresh_rule(m
, rule
, seqno
, rules
);
6094 cls_rule_destroy(&target
);
6098 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor
*m
,
6099 struct rule_collection
*rules
)
6100 OVS_REQUIRES(ofproto_mutex
)
6102 if (m
->flags
& NXFMF_INITIAL
) {
6103 ofproto_collect_ofmonitor_refresh_rules(m
, 0, rules
);
6108 ofmonitor_collect_resume_rules(struct ofmonitor
*m
,
6109 uint64_t seqno
, struct rule_collection
*rules
)
6110 OVS_REQUIRES(ofproto_mutex
)
6112 ofproto_collect_ofmonitor_refresh_rules(m
, seqno
, rules
);
6116 flow_monitor_delete(struct ofconn
*ofconn
, uint32_t id
)
6117 OVS_REQUIRES(ofproto_mutex
)
6119 struct ofmonitor
*m
;
6122 m
= ofmonitor_lookup(ofconn
, id
);
6124 ofmonitor_destroy(m
);
6127 error
= OFPERR_OFPMOFC_UNKNOWN_MONITOR
;
6134 handle_flow_monitor_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6135 OVS_EXCLUDED(ofproto_mutex
)
6137 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6139 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6141 struct ofmonitor
**monitors
= NULL
;
6142 size_t allocated_monitors
= 0;
6143 size_t n_monitors
= 0;
6147 ovs_mutex_lock(&ofproto_mutex
);
6149 struct ofputil_flow_monitor_request request
;
6150 struct ofmonitor
*m
;
6153 retval
= ofputil_decode_flow_monitor_request(&request
, &b
);
6154 if (retval
== EOF
) {
6156 } else if (retval
) {
6161 if (request
.table_id
!= 0xff
6162 && request
.table_id
>= ofproto
->n_tables
) {
6163 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
6167 error
= ofmonitor_create(&request
, ofconn
, &m
);
6172 if (n_monitors
>= allocated_monitors
) {
6173 monitors
= x2nrealloc(monitors
, &allocated_monitors
,
6176 monitors
[n_monitors
++] = m
;
6179 struct rule_collection rules
;
6180 rule_collection_init(&rules
);
6181 for (size_t i
= 0; i
< n_monitors
; i
++) {
6182 ofproto_collect_ofmonitor_initial_rules(monitors
[i
], &rules
);
6185 struct ovs_list replies
;
6186 ofpmp_init(&replies
, oh
);
6187 ofmonitor_compose_refresh_updates(&rules
, &replies
);
6188 ovs_mutex_unlock(&ofproto_mutex
);
6190 rule_collection_destroy(&rules
);
6192 ofconn_send_replies(ofconn
, &replies
);
6198 for (size_t i
= 0; i
< n_monitors
; i
++) {
6199 ofmonitor_destroy(monitors
[i
]);
6202 ovs_mutex_unlock(&ofproto_mutex
);
6208 handle_flow_monitor_cancel(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6209 OVS_EXCLUDED(ofproto_mutex
)
6214 id
= ofputil_decode_flow_monitor_cancel(oh
);
6216 ovs_mutex_lock(&ofproto_mutex
);
6217 error
= flow_monitor_delete(ofconn
, id
);
6218 ovs_mutex_unlock(&ofproto_mutex
);
6223 /* Meters implementation.
6225 * Meter table entry, indexed by the OpenFlow meter_id.
6226 * 'created' is used to compute the duration for meter stats.
6227 * 'list rules' is needed so that we can delete the dependent rules when the
6228 * meter table entry is deleted.
6229 * 'provider_meter_id' is for the provider's private use.
6232 struct hmap_node node
; /* In ofproto->meters. */
6233 long long int created
; /* Time created. */
6234 struct ovs_list rules
; /* List of "struct rule_dpif"s. */
6235 uint32_t id
; /* OpenFlow meter_id. */
6236 ofproto_meter_id provider_meter_id
;
6237 uint16_t flags
; /* Meter flags. */
6238 uint16_t n_bands
; /* Number of meter bands. */
6239 struct ofputil_meter_band
*bands
;
6242 static struct meter
*
6243 ofproto_get_meter(const struct ofproto
*ofproto
, uint32_t meter_id
)
6245 struct meter
*meter
;
6246 uint32_t hash
= hash_int(meter_id
, 0);
6248 HMAP_FOR_EACH_WITH_HASH (meter
, node
, hash
, &ofproto
->meters
) {
6249 if (meter
->id
== meter_id
) {
6258 ofproto_upcall_meter_ptr(struct ofproto
*ofproto
, uint32_t meter_id
)
6261 case OFPM13_SLOWPATH
:
6262 return &ofproto
->slowpath_meter_id
;
6264 case OFPM13_CONTROLLER
:
6265 return &ofproto
->controller_meter_id
;
6275 ofproto_add_meter(struct ofproto
*ofproto
, struct meter
*meter
)
6277 uint32_t *upcall_meter_ptr
= ofproto_upcall_meter_ptr(ofproto
, meter
->id
);
6279 /* Cache datapath meter IDs of special meters. */
6280 if (upcall_meter_ptr
) {
6281 *upcall_meter_ptr
= meter
->provider_meter_id
.uint32
;
6284 hmap_insert(&ofproto
->meters
, &meter
->node
, hash_int(meter
->id
, 0));
6288 * This is used in instruction validation at flow set-up time, to map
6289 * the OpenFlow meter ID to the corresponding datapath provider meter
6290 * ID. If either does not exist, returns false. Otherwise updates
6291 * the meter action and returns true.
6294 ofproto_fix_meter_action(const struct ofproto
*ofproto
,
6295 struct ofpact_meter
*ma
)
6298 const struct meter
*meter
= ofproto_get_meter(ofproto
, ma
->meter_id
);
6300 if (meter
&& meter
->provider_meter_id
.uint32
!= UINT32_MAX
) {
6301 /* Update the action with the provider's meter ID, so that we
6302 * do not need any synchronization between ofproto_dpif_xlate
6303 * and ofproto for meter table access. */
6304 ma
->provider_meter_id
= meter
->provider_meter_id
.uint32
;
6311 /* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
6314 meter_insert_rule(struct rule
*rule
)
6316 const struct rule_actions
*a
= rule_get_actions(rule
);
6317 uint32_t meter_id
= ofpacts_get_meter(a
->ofpacts
, a
->ofpacts_len
);
6318 struct meter
*meter
= ofproto_get_meter(rule
->ofproto
, meter_id
);
6320 ovs_list_insert(&meter
->rules
, &rule
->meter_list_node
);
6324 meter_update(struct meter
*meter
, const struct ofputil_meter_config
*config
)
6328 meter
->flags
= config
->flags
;
6329 meter
->n_bands
= config
->n_bands
;
6330 meter
->bands
= xmemdup(config
->bands
,
6331 config
->n_bands
* sizeof *meter
->bands
);
6334 static struct meter
*
6335 meter_create(const struct ofputil_meter_config
*config
,
6336 ofproto_meter_id provider_meter_id
)
6338 struct meter
*meter
;
6340 meter
= xzalloc(sizeof *meter
);
6341 meter
->provider_meter_id
= provider_meter_id
;
6342 meter
->created
= time_msec();
6343 meter
->id
= config
->meter_id
;
6344 ovs_list_init(&meter
->rules
);
6346 meter_update(meter
, config
);
6352 meter_destroy(struct ofproto
*ofproto
, struct meter
*meter
)
6353 OVS_REQUIRES(ofproto_mutex
)
6355 uint32_t *upcall_meter_ptr
;
6356 upcall_meter_ptr
= ofproto_upcall_meter_ptr(ofproto
, meter
->id
);
6357 if (upcall_meter_ptr
) {
6358 *upcall_meter_ptr
= UINT32_MAX
;
6361 if (!ovs_list_is_empty(&meter
->rules
)) {
6362 struct rule_collection rules
;
6365 rule_collection_init(&rules
);
6366 LIST_FOR_EACH (rule
, meter_list_node
, &meter
->rules
) {
6367 rule_collection_add(&rules
, rule
);
6369 delete_flows__(&rules
, OFPRR_METER_DELETE
, NULL
);
6372 ofproto
->ofproto_class
->meter_del(ofproto
, meter
->provider_meter_id
);
6378 meter_delete(struct ofproto
*ofproto
, uint32_t meter_id
)
6379 OVS_REQUIRES(ofproto_mutex
)
6381 struct meter
*meter
= ofproto_get_meter(ofproto
, meter_id
);
6384 hmap_remove(&ofproto
->meters
, &meter
->node
);
6385 meter_destroy(ofproto
, meter
);
6390 meter_delete_all(struct ofproto
*ofproto
)
6391 OVS_REQUIRES(ofproto_mutex
)
6393 struct meter
*meter
, *next
;
6395 HMAP_FOR_EACH_SAFE (meter
, next
, node
, &ofproto
->meters
) {
6396 hmap_remove(&ofproto
->meters
, &meter
->node
);
6397 meter_destroy(ofproto
, meter
);
6402 handle_add_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
6404 ofproto_meter_id provider_meter_id
= { UINT32_MAX
};
6405 struct meter
*meter
= ofproto_get_meter(ofproto
, mm
->meter
.meter_id
);
6409 return OFPERR_OFPMMFC_METER_EXISTS
;
6412 error
= ofproto
->ofproto_class
->meter_set(ofproto
, &provider_meter_id
,
6415 ovs_assert(provider_meter_id
.uint32
!= UINT32_MAX
);
6416 meter
= meter_create(&mm
->meter
, provider_meter_id
);
6417 ofproto_add_meter(ofproto
, meter
);
6423 handle_modify_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
6425 struct meter
*meter
= ofproto_get_meter(ofproto
, mm
->meter
.meter_id
);
6427 uint32_t provider_meter_id
;
6430 return OFPERR_OFPMMFC_UNKNOWN_METER
;
6433 provider_meter_id
= meter
->provider_meter_id
.uint32
;
6434 error
= ofproto
->ofproto_class
->meter_set(ofproto
,
6435 &meter
->provider_meter_id
,
6437 ovs_assert(meter
->provider_meter_id
.uint32
== provider_meter_id
);
6439 meter_update(meter
, &mm
->meter
);
6445 handle_delete_meter(struct ofconn
*ofconn
, struct ofputil_meter_mod
*mm
)
6446 OVS_EXCLUDED(ofproto_mutex
)
6448 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6449 uint32_t meter_id
= mm
->meter
.meter_id
;
6451 /* OpenFlow does not support Meter ID 0. */
6453 ovs_mutex_lock(&ofproto_mutex
);
6455 if (meter_id
== OFPM13_ALL
) {
6456 meter_delete_all(ofproto
);
6458 meter_delete(ofproto
, meter_id
);
6461 ovs_mutex_unlock(&ofproto_mutex
);
6468 handle_meter_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6470 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6471 struct ofputil_meter_mod mm
;
6472 uint64_t bands_stub
[256 / 8];
6473 struct ofpbuf bands
;
6477 error
= reject_slave_controller(ofconn
);
6482 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
6484 error
= ofputil_decode_meter_mod(oh
, &mm
, &bands
);
6486 goto exit_free_bands
;
6489 meter_id
= mm
.meter
.meter_id
;
6491 if (mm
.command
!= OFPMC13_DELETE
) {
6492 /* Fails also when meters are not implemented by the provider. */
6493 if (ofproto
->meter_features
.max_meters
== 0) {
6494 error
= OFPERR_OFPMMFC_INVALID_METER
;
6495 goto exit_free_bands
;
6498 if (meter_id
== 0) {
6499 error
= OFPERR_OFPMMFC_INVALID_METER
;
6500 goto exit_free_bands
;
6501 } else if (meter_id
> OFPM13_MAX
) {
6503 case OFPM13_SLOWPATH
:
6504 case OFPM13_CONTROLLER
:
6508 error
= OFPERR_OFPMMFC_INVALID_METER
;
6509 goto exit_free_bands
;
6512 if (mm
.meter
.n_bands
> ofproto
->meter_features
.max_bands
) {
6513 error
= OFPERR_OFPMMFC_OUT_OF_BANDS
;
6514 goto exit_free_bands
;
6518 switch (mm
.command
) {
6520 error
= handle_add_meter(ofproto
, &mm
);
6523 case OFPMC13_MODIFY
:
6524 error
= handle_modify_meter(ofproto
, &mm
);
6527 case OFPMC13_DELETE
:
6528 error
= handle_delete_meter(ofconn
, &mm
);
6532 error
= OFPERR_OFPMMFC_BAD_COMMAND
;
6537 struct ofputil_requestforward rf
;
6539 rf
.reason
= OFPRFR_METER_MOD
;
6541 connmgr_send_requestforward(ofproto
->connmgr
, ofconn
, &rf
);
6545 ofpbuf_uninit(&bands
);
6550 handle_meter_features_request(struct ofconn
*ofconn
,
6551 const struct ofp_header
*request
)
6553 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6554 struct ofputil_meter_features features
;
6557 if (ofproto
->ofproto_class
->meter_get_features
) {
6558 ofproto
->ofproto_class
->meter_get_features(ofproto
, &features
);
6560 memset(&features
, 0, sizeof features
);
6562 b
= ofputil_encode_meter_features_reply(&features
, request
);
6564 ofconn_send_reply(ofconn
, b
);
6569 meter_request_reply(struct ofproto
*ofproto
, struct meter
*meter
,
6570 enum ofptype type
, struct ovs_list
*replies
)
6572 uint64_t bands_stub
[256 / 8];
6573 struct ofpbuf bands
;
6575 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
6577 if (type
== OFPTYPE_METER_STATS_REQUEST
) {
6578 struct ofputil_meter_stats stats
;
6580 stats
.meter_id
= meter
->id
;
6582 /* Provider sets the packet and byte counts, we do the rest. */
6583 stats
.flow_count
= ovs_list_size(&meter
->rules
);
6584 calc_duration(meter
->created
, time_msec(),
6585 &stats
.duration_sec
, &stats
.duration_nsec
);
6586 stats
.n_bands
= meter
->n_bands
;
6587 ofpbuf_clear(&bands
);
6588 stats
.bands
= ofpbuf_put_uninit(&bands
, meter
->n_bands
6589 * sizeof *stats
.bands
);
6591 if (!ofproto
->ofproto_class
->meter_get(ofproto
,
6592 meter
->provider_meter_id
,
6593 &stats
, meter
->n_bands
)) {
6594 ofputil_append_meter_stats(replies
, &stats
);
6596 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
6597 struct ofputil_meter_config config
;
6599 config
.meter_id
= meter
->id
;
6600 config
.flags
= meter
->flags
;
6601 config
.n_bands
= meter
->n_bands
;
6602 config
.bands
= meter
->bands
;
6603 ofputil_append_meter_config(replies
, &config
);
6606 ofpbuf_uninit(&bands
);
6610 meter_request_reply_all(struct ofproto
*ofproto
, enum ofptype type
,
6611 struct ovs_list
*replies
)
6613 struct meter
*meter
;
6615 HMAP_FOR_EACH (meter
, node
, &ofproto
->meters
) {
6616 meter_request_reply(ofproto
, meter
, type
, replies
);
6621 handle_meter_request(struct ofconn
*ofconn
, const struct ofp_header
*request
,
6624 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6625 struct ovs_list replies
;
6627 struct meter
*meter
;
6629 ofputil_decode_meter_request(request
, &meter_id
);
6631 if (meter_id
!= OFPM13_ALL
) {
6632 meter
= ofproto_get_meter(ofproto
, meter_id
);
6634 /* Meter does not exist. */
6635 return OFPERR_OFPMMFC_UNKNOWN_METER
;
6638 /* GCC 4.9.2 complains about 'meter' can potentially be used
6639 * uninitialized. Logically, this is a false alarm since
6640 * meter is only used when meter_id != OFPM13_ALL.
6641 * Set NULL to make compiler happy. */
6645 ofpmp_init(&replies
, request
);
6646 if (meter_id
== OFPM13_ALL
) {
6647 meter_request_reply_all(ofproto
, type
, &replies
);
6649 meter_request_reply(ofproto
, meter
, type
, &replies
);
6651 ofconn_send_replies(ofconn
, &replies
);
6655 /* Returned group is RCU protected. */
6656 static struct ofgroup
*
6657 ofproto_group_lookup__(const struct ofproto
*ofproto
, uint32_t group_id
,
6658 ovs_version_t version
)
6660 struct ofgroup
*group
;
6662 CMAP_FOR_EACH_WITH_HASH (group
, cmap_node
, hash_int(group_id
, 0),
6664 if (group
->group_id
== group_id
6665 && versions_visible_in_version(&group
->versions
, version
)) {
6673 /* If the group exists, this function increments the groups's reference count.
6675 * Make sure to call ofproto_group_unref() after no longer needing to maintain
6676 * a reference to the group. */
6678 ofproto_group_lookup(const struct ofproto
*ofproto
, uint32_t group_id
,
6679 ovs_version_t version
, bool take_ref
)
6681 struct ofgroup
*group
;
6683 group
= ofproto_group_lookup__(ofproto
, group_id
, version
);
6684 if (group
&& take_ref
) {
6685 /* Not holding a lock, so it is possible that another thread releases
6686 * the last reference just before we manage to get one. */
6687 return ofproto_group_try_ref(group
) ? group
: NULL
;
6692 /* Caller should hold 'ofproto_mutex' if it is important that the
6693 * group is not removed by someone else. */
6695 ofproto_group_exists(const struct ofproto
*ofproto
, uint32_t group_id
)
6697 return ofproto_group_lookup__(ofproto
, group_id
, OVS_VERSION_MAX
) != NULL
;
6701 group_add_rule(struct ofgroup
*group
, struct rule
*rule
)
6703 rule_collection_add(&group
->rules
, rule
);
6707 group_remove_rule(struct ofgroup
*group
, struct rule
*rule
)
6709 rule_collection_remove(&group
->rules
, rule
);
6713 append_group_stats(struct ofgroup
*group
, struct ovs_list
*replies
)
6714 OVS_REQUIRES(ofproto_mutex
)
6716 struct ofputil_group_stats ogs
;
6717 const struct ofproto
*ofproto
= group
->ofproto
;
6718 long long int now
= time_msec();
6721 ogs
.bucket_stats
= xmalloc(group
->n_buckets
* sizeof *ogs
.bucket_stats
);
6723 /* Provider sets the packet and byte counts, we do the rest. */
6724 ogs
.ref_count
= rule_collection_n(&group
->rules
);
6725 ogs
.n_buckets
= group
->n_buckets
;
6727 error
= (ofproto
->ofproto_class
->group_get_stats
6728 ? ofproto
->ofproto_class
->group_get_stats(group
, &ogs
)
6731 ogs
.packet_count
= UINT64_MAX
;
6732 ogs
.byte_count
= UINT64_MAX
;
6733 memset(ogs
.bucket_stats
, 0xff,
6734 ogs
.n_buckets
* sizeof *ogs
.bucket_stats
);
6737 ogs
.group_id
= group
->group_id
;
6738 calc_duration(group
->created
, now
, &ogs
.duration_sec
, &ogs
.duration_nsec
);
6740 ofputil_append_group_stats(replies
, &ogs
);
6742 free(ogs
.bucket_stats
);
6746 handle_group_request(struct ofconn
*ofconn
,
6747 const struct ofp_header
*request
, uint32_t group_id
,
6748 void (*cb
)(struct ofgroup
*, struct ovs_list
*replies
))
6749 OVS_EXCLUDED(ofproto_mutex
)
6751 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6752 struct ofgroup
*group
;
6753 struct ovs_list replies
;
6755 ofpmp_init(&replies
, request
);
6756 /* Must exclude modifications to guarantee iterating groups. */
6757 ovs_mutex_lock(&ofproto_mutex
);
6758 if (group_id
== OFPG_ALL
) {
6759 CMAP_FOR_EACH (group
, cmap_node
, &ofproto
->groups
) {
6760 if (versions_visible_in_version(&group
->versions
,
6762 cb(group
, &replies
);
6766 group
= ofproto_group_lookup__(ofproto
, group_id
, OVS_VERSION_MAX
);
6768 cb(group
, &replies
);
6771 ovs_mutex_unlock(&ofproto_mutex
);
6772 ofconn_send_replies(ofconn
, &replies
);
6776 handle_group_stats_request(struct ofconn
*ofconn
,
6777 const struct ofp_header
*request
)
6782 error
= ofputil_decode_group_stats_request(request
, &group_id
);
6787 handle_group_request(ofconn
, request
, group_id
, append_group_stats
);
6792 append_group_desc(struct ofgroup
*group
, struct ovs_list
*replies
)
6794 struct ofputil_group_desc gds
;
6796 gds
.group_id
= group
->group_id
;
6797 gds
.type
= group
->type
;
6798 gds
.props
= group
->props
;
6800 ofputil_append_group_desc_reply(&gds
, &group
->buckets
, replies
);
6804 handle_group_desc_stats_request(struct ofconn
*ofconn
,
6805 const struct ofp_header
*request
)
6807 handle_group_request(ofconn
, request
,
6808 ofputil_decode_group_desc_request(request
),
6814 handle_group_features_stats_request(struct ofconn
*ofconn
,
6815 const struct ofp_header
*request
)
6817 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
6820 msg
= ofputil_encode_group_features_reply(&p
->ogf
, request
);
6822 ofconn_send_reply(ofconn
, msg
);
6829 put_queue_get_config_reply(struct ofport
*port
, uint32_t queue
,
6830 struct ovs_list
*replies
)
6832 struct ofputil_queue_config qc
;
6834 /* None of the existing queues have compatible properties, so we hard-code
6835 * omitting min_rate and max_rate. */
6836 qc
.port
= port
->ofp_port
;
6838 qc
.min_rate
= UINT16_MAX
;
6839 qc
.max_rate
= UINT16_MAX
;
6840 ofputil_append_queue_get_config_reply(&qc
, replies
);
6844 handle_queue_get_config_request_for_port(struct ofport
*port
, uint32_t queue
,
6845 struct ovs_list
*replies
)
6847 struct smap details
= SMAP_INITIALIZER(&details
);
6848 if (queue
!= OFPQ_ALL
) {
6849 int error
= netdev_get_queue(port
->netdev
, queue
, &details
);
6852 put_queue_get_config_reply(port
, queue
, replies
);
6856 return OFPERR_OFPQOFC_BAD_QUEUE
;
6858 return OFPERR_NXQOFC_QUEUE_ERROR
;
6861 struct netdev_queue_dump queue_dump
;
6864 NETDEV_QUEUE_FOR_EACH (&queue_id
, &details
, &queue_dump
,
6866 put_queue_get_config_reply(port
, queue_id
, replies
);
6869 smap_destroy(&details
);
6874 handle_queue_get_config_request(struct ofconn
*ofconn
,
6875 const struct ofp_header
*oh
)
6877 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6878 struct ovs_list replies
;
6879 struct ofport
*port
;
6880 ofp_port_t req_port
;
6884 error
= ofputil_decode_queue_get_config_request(oh
, &req_port
, &req_queue
);
6889 ofputil_start_queue_get_config_reply(oh
, &replies
);
6890 if (req_port
== OFPP_ANY
) {
6891 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
6892 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
6893 if (!handle_queue_get_config_request_for_port(port
, req_queue
,
6899 port
= ofproto_get_port(ofproto
, req_port
);
6901 ? handle_queue_get_config_request_for_port(port
, req_queue
,
6903 : OFPERR_OFPQOFC_BAD_PORT
);
6906 ofconn_send_replies(ofconn
, &replies
);
6908 ofpbuf_list_delete(&replies
);
6914 /* Allocates, initializes, and constructs a new group in 'ofproto', obtaining
6915 * all the attributes for it from 'gm', and stores a pointer to it in
6916 * '*ofgroup'. Makes the new group visible from the flow table starting from
6919 * Returns 0 if successful, otherwise an error code. If there is an error then
6920 * '*ofgroup' is indeterminate upon return. */
6922 init_group(struct ofproto
*ofproto
, const struct ofputil_group_mod
*gm
,
6923 ovs_version_t version
, struct ofgroup
**ofgroup
)
6926 const long long int now
= time_msec();
6928 if (gm
->group_id
> OFPG_MAX
) {
6929 return OFPERR_OFPGMFC_INVALID_GROUP
;
6931 if (gm
->type
> OFPGT11_FF
) {
6932 return OFPERR_OFPGMFC_BAD_TYPE
;
6935 *ofgroup
= ofproto
->ofproto_class
->group_alloc();
6937 VLOG_WARN_RL(&rl
, "%s: failed to allocate group", ofproto
->name
);
6938 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
6941 *CONST_CAST(struct ofproto
**, &(*ofgroup
)->ofproto
) = ofproto
;
6942 *CONST_CAST(uint32_t *, &((*ofgroup
)->group_id
)) = gm
->group_id
;
6943 *CONST_CAST(enum ofp11_group_type
*, &(*ofgroup
)->type
) = gm
->type
;
6944 *CONST_CAST(long long int *, &((*ofgroup
)->created
)) = now
;
6945 *CONST_CAST(long long int *, &((*ofgroup
)->modified
)) = now
;
6946 ovs_refcount_init(&(*ofgroup
)->ref_count
);
6947 (*ofgroup
)->being_deleted
= false;
6949 ovs_list_init(CONST_CAST(struct ovs_list
*, &(*ofgroup
)->buckets
));
6950 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
6951 &(*ofgroup
)->buckets
),
6952 &gm
->buckets
, NULL
);
6954 *CONST_CAST(uint32_t *, &(*ofgroup
)->n_buckets
) =
6955 ovs_list_size(&(*ofgroup
)->buckets
);
6957 ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props
*,
6958 &(*ofgroup
)->props
),
6960 rule_collection_init(&(*ofgroup
)->rules
);
6962 /* Make group visible from 'version'. */
6963 (*ofgroup
)->versions
= VERSIONS_INITIALIZER(version
,
6964 OVS_VERSION_NOT_REMOVED
);
6966 /* Construct called BEFORE any locks are held. */
6967 error
= ofproto
->ofproto_class
->group_construct(*ofgroup
);
6969 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props
*,
6970 &(*ofgroup
)->props
));
6971 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list
*,
6972 &(*ofgroup
)->buckets
));
6973 ofproto
->ofproto_class
->group_dealloc(*ofgroup
);
6978 /* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
6979 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
6982 add_group_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
6983 OVS_REQUIRES(ofproto_mutex
)
6987 if (ofproto_group_exists(ofproto
, ogm
->gm
.group_id
)) {
6988 return OFPERR_OFPGMFC_GROUP_EXISTS
;
6991 if (ofproto
->n_groups
[ogm
->gm
.type
]
6992 >= ofproto
->ogf
.max_groups
[ogm
->gm
.type
]) {
6993 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
6996 /* Allocate new group and initialize it. */
6997 error
= init_group(ofproto
, &ogm
->gm
, ogm
->version
, &ogm
->new_group
);
6999 /* Insert new group. */
7000 cmap_insert(&ofproto
->groups
, &ogm
->new_group
->cmap_node
,
7001 hash_int(ogm
->new_group
->group_id
, 0));
7002 ofproto
->n_groups
[ogm
->new_group
->type
]++;
7007 /* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets
7008 * already in 'new_ofgroup' will be placed just after the (copy of the) bucket
7009 * in 'ofgroup' with bucket ID 'command_bucket_id'. Special
7010 * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are
7013 copy_buckets_for_insert_bucket(const struct ofgroup
*ofgroup
,
7014 struct ofgroup
*new_ofgroup
,
7015 uint32_t command_bucket_id
)
7017 struct ofputil_bucket
*last
= NULL
;
7019 if (command_bucket_id
<= OFPG15_BUCKET_MAX
) {
7020 /* Check here to ensure that a bucket corresponding to
7021 * command_bucket_id exists in the old bucket list.
7023 * The subsequent search of below of new_ofgroup covers
7024 * both buckets in the old bucket list and buckets added
7025 * by the insert buckets group mod message this function processes. */
7026 if (!ofputil_bucket_find(&ofgroup
->buckets
, command_bucket_id
)) {
7027 return OFPERR_OFPGMFC_UNKNOWN_BUCKET
;
7030 if (!ovs_list_is_empty(&new_ofgroup
->buckets
)) {
7031 last
= ofputil_bucket_list_back(&new_ofgroup
->buckets
);
7035 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
7036 &new_ofgroup
->buckets
),
7037 &ofgroup
->buckets
, NULL
);
7039 if (ofputil_bucket_check_duplicate_id(&new_ofgroup
->buckets
)) {
7040 VLOG_INFO_RL(&rl
, "Duplicate bucket id");
7041 return OFPERR_OFPGMFC_BUCKET_EXISTS
;
7044 /* Rearrange list according to command_bucket_id */
7045 if (command_bucket_id
== OFPG15_BUCKET_LAST
) {
7046 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
7047 struct ofputil_bucket
*new_first
;
7048 const struct ofputil_bucket
*first
;
7050 first
= ofputil_bucket_list_front(&ofgroup
->buckets
);
7051 new_first
= ofputil_bucket_find(&new_ofgroup
->buckets
,
7054 ovs_list_splice(new_ofgroup
->buckets
.next
, &new_first
->list_node
,
7055 CONST_CAST(struct ovs_list
*,
7056 &new_ofgroup
->buckets
));
7058 } else if (command_bucket_id
<= OFPG15_BUCKET_MAX
&& last
) {
7059 struct ofputil_bucket
*after
;
7061 /* Presence of bucket is checked above so after should never be NULL */
7062 after
= ofputil_bucket_find(&new_ofgroup
->buckets
, command_bucket_id
);
7064 ovs_list_splice(after
->list_node
.next
, new_ofgroup
->buckets
.next
,
7065 last
->list_node
.next
);
7071 /* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup'
7072 * with the exception of the bucket whose bucket id is 'command_bucket_id'.
7073 * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST
7074 * and OFPG15_BUCKET_ALL are also honored. */
7076 copy_buckets_for_remove_bucket(const struct ofgroup
*ofgroup
,
7077 struct ofgroup
*new_ofgroup
,
7078 uint32_t command_bucket_id
)
7080 const struct ofputil_bucket
*skip
= NULL
;
7082 if (command_bucket_id
== OFPG15_BUCKET_ALL
) {
7086 if (command_bucket_id
== OFPG15_BUCKET_FIRST
) {
7087 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
7088 skip
= ofputil_bucket_list_front(&ofgroup
->buckets
);
7090 } else if (command_bucket_id
== OFPG15_BUCKET_LAST
) {
7091 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
7092 skip
= ofputil_bucket_list_back(&ofgroup
->buckets
);
7095 skip
= ofputil_bucket_find(&ofgroup
->buckets
, command_bucket_id
);
7097 return OFPERR_OFPGMFC_UNKNOWN_BUCKET
;
7101 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
7102 &new_ofgroup
->buckets
),
7103 &ofgroup
->buckets
, skip
);
7108 /* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and
7109 * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code
7112 * Note that the group is re-created and then replaces the old group in
7113 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
7114 * the xlate module hold a pointer to the group. */
7116 modify_group_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
7117 OVS_REQUIRES(ofproto_mutex
)
7119 struct ofgroup
*old_group
; /* Modified group. */
7120 struct ofgroup
*new_group
;
7123 old_group
= ofproto_group_lookup__(ofproto
, ogm
->gm
.group_id
,
7126 return OFPERR_OFPGMFC_UNKNOWN_GROUP
;
7129 /* Inserting or deleting a bucket should not change the group's type or
7130 * properties, so change the group mod so that these aspects match the old
7131 * group. (See EXT-570.) */
7132 if (ogm
->gm
.command
== OFPGC15_INSERT_BUCKET
||
7133 ogm
->gm
.command
== OFPGC15_REMOVE_BUCKET
) {
7134 ogm
->gm
.type
= old_group
->type
;
7135 ofputil_group_properties_destroy(&ogm
->gm
.props
);
7136 ofputil_group_properties_copy(&ogm
->gm
.props
, &old_group
->props
);
7139 if (old_group
->type
!= ogm
->gm
.type
7140 && (ofproto
->n_groups
[ogm
->gm
.type
]
7141 >= ofproto
->ogf
.max_groups
[ogm
->gm
.type
])) {
7142 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
7145 error
= init_group(ofproto
, &ogm
->gm
, ogm
->version
, &ogm
->new_group
);
7149 new_group
= ogm
->new_group
;
7151 /* Manipulate bucket list for bucket commands */
7152 if (ogm
->gm
.command
== OFPGC15_INSERT_BUCKET
) {
7153 error
= copy_buckets_for_insert_bucket(old_group
, new_group
,
7154 ogm
->gm
.command_bucket_id
);
7155 } else if (ogm
->gm
.command
== OFPGC15_REMOVE_BUCKET
) {
7156 error
= copy_buckets_for_remove_bucket(old_group
, new_group
,
7157 ogm
->gm
.command_bucket_id
);
7163 /* The group creation time does not change during modification. */
7164 *CONST_CAST(long long int *, &(new_group
->created
)) = old_group
->created
;
7165 *CONST_CAST(long long int *, &(new_group
->modified
)) = time_msec();
7167 group_collection_add(&ogm
->old_groups
, old_group
);
7169 /* Mark the old group for deletion. */
7170 versions_set_remove_version(&old_group
->versions
, ogm
->version
);
7171 /* Insert replacement group. */
7172 cmap_insert(&ofproto
->groups
, &new_group
->cmap_node
,
7173 hash_int(new_group
->group_id
, 0));
7174 /* Transfer rules. */
7175 rule_collection_move(&new_group
->rules
, &old_group
->rules
);
7177 if (old_group
->type
!= new_group
->type
) {
7178 ofproto
->n_groups
[old_group
->type
]--;
7179 ofproto
->n_groups
[new_group
->type
]++;
7184 ofproto_group_unref(new_group
);
7188 /* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not
7189 * exist yet and modifies it otherwise */
7191 add_or_modify_group_start(struct ofproto
*ofproto
,
7192 struct ofproto_group_mod
*ogm
)
7193 OVS_REQUIRES(ofproto_mutex
)
7197 if (!ofproto_group_exists(ofproto
, ogm
->gm
.group_id
)) {
7198 error
= add_group_start(ofproto
, ogm
);
7200 error
= modify_group_start(ofproto
, ogm
);
7207 delete_group_start(struct ofproto
*ofproto
, ovs_version_t version
,
7208 struct group_collection
*groups
, struct ofgroup
*group
)
7209 OVS_REQUIRES(ofproto_mutex
)
7211 /* Makes flow deletion code leave the rule pointers in 'group->rules'
7212 * intact, so that we can later refer to the rules deleted due to the group
7213 * deletion. Rule pointers will be removed from all other groups, if any,
7214 * so we will never try to delete the same rule twice. */
7215 group
->being_deleted
= true;
7217 /* Mark all the referring groups for deletion. */
7218 delete_flows_start__(ofproto
, version
, &group
->rules
);
7219 group_collection_add(groups
, group
);
7220 versions_set_remove_version(&group
->versions
, version
);
7221 ofproto
->n_groups
[group
->type
]--;
7225 delete_group_finish(struct ofproto
*ofproto
, struct ofgroup
*group
)
7226 OVS_REQUIRES(ofproto_mutex
)
7228 /* Finish deletion of all flow entries containing this group in a group
7230 delete_flows_finish__(ofproto
, &group
->rules
, OFPRR_GROUP_DELETE
, NULL
);
7232 /* Group removal is postponed by the caller. */
7235 /* Implements OFPGC11_DELETE. */
7237 delete_groups_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
7238 OVS_REQUIRES(ofproto_mutex
)
7240 struct ofgroup
*group
;
7242 if (ogm
->gm
.group_id
== OFPG_ALL
) {
7243 CMAP_FOR_EACH (group
, cmap_node
, &ofproto
->groups
) {
7244 if (versions_visible_in_version(&group
->versions
, ogm
->version
)) {
7245 delete_group_start(ofproto
, ogm
->version
, &ogm
->old_groups
,
7250 group
= ofproto_group_lookup__(ofproto
, ogm
->gm
.group_id
, ogm
->version
);
7252 delete_group_start(ofproto
, ogm
->version
, &ogm
->old_groups
, group
);
7258 ofproto_group_mod_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
7259 OVS_REQUIRES(ofproto_mutex
)
7263 ogm
->new_group
= NULL
;
7264 group_collection_init(&ogm
->old_groups
);
7266 switch (ogm
->gm
.command
) {
7268 error
= add_group_start(ofproto
, ogm
);
7271 case OFPGC11_MODIFY
:
7272 error
= modify_group_start(ofproto
, ogm
);
7275 case OFPGC11_ADD_OR_MOD
:
7276 error
= add_or_modify_group_start(ofproto
, ogm
);
7279 case OFPGC11_DELETE
:
7280 delete_groups_start(ofproto
, ogm
);
7284 case OFPGC15_INSERT_BUCKET
:
7285 error
= modify_group_start(ofproto
, ogm
);
7288 case OFPGC15_REMOVE_BUCKET
:
7289 error
= modify_group_start(ofproto
, ogm
);
7293 if (ogm
->gm
.command
> OFPGC11_DELETE
) {
7294 VLOG_INFO_RL(&rl
, "%s: Invalid group_mod command type %d",
7295 ofproto
->name
, ogm
->gm
.command
);
7297 error
= OFPERR_OFPGMFC_BAD_COMMAND
;
7304 ofproto_group_mod_revert(struct ofproto
*ofproto
,
7305 struct ofproto_group_mod
*ogm
)
7306 OVS_REQUIRES(ofproto_mutex
)
7308 struct ofgroup
*new_group
= ogm
->new_group
;
7309 struct ofgroup
*old_group
;
7311 /* Restore replaced or deleted groups. */
7312 GROUP_COLLECTION_FOR_EACH (old_group
, &ogm
->old_groups
) {
7313 ofproto
->n_groups
[old_group
->type
]++;
7315 ovs_assert(group_collection_n(&ogm
->old_groups
) == 1);
7316 /* Transfer rules back. */
7317 rule_collection_move(&old_group
->rules
, &new_group
->rules
);
7319 old_group
->being_deleted
= false;
7320 /* Revert rule deletion. */
7321 delete_flows_revert__(ofproto
, &old_group
->rules
);
7323 /* Restore visibility. */
7324 versions_set_remove_version(&old_group
->versions
,
7325 OVS_VERSION_NOT_REMOVED
);
7328 /* Remove the new group immediately. It was never visible to
7330 cmap_remove(&ofproto
->groups
, &new_group
->cmap_node
,
7331 hash_int(new_group
->group_id
, 0));
7332 ofproto
->n_groups
[new_group
->type
]--;
7333 ofproto_group_unref(new_group
);
7338 ofproto_group_mod_finish(struct ofproto
*ofproto
,
7339 struct ofproto_group_mod
*ogm
,
7340 const struct openflow_mod_requester
*req
)
7341 OVS_REQUIRES(ofproto_mutex
)
7343 struct ofgroup
*new_group
= ogm
->new_group
;
7344 struct ofgroup
*old_group
;
7346 if (new_group
&& group_collection_n(&ogm
->old_groups
) &&
7347 ofproto
->ofproto_class
->group_modify
) {
7348 /* Modify a group. */
7349 ovs_assert(group_collection_n(&ogm
->old_groups
) == 1);
7351 /* XXX: OK to lose old group's stats? */
7352 ofproto
->ofproto_class
->group_modify(new_group
);
7355 /* Delete old groups. */
7356 GROUP_COLLECTION_FOR_EACH(old_group
, &ogm
->old_groups
) {
7357 delete_group_finish(ofproto
, old_group
);
7359 remove_groups_postponed(&ogm
->old_groups
);
7362 struct ofputil_requestforward rf
;
7363 rf
.xid
= req
->request
->xid
;
7364 rf
.reason
= OFPRFR_GROUP_MOD
;
7365 rf
.group_mod
= &ogm
->gm
;
7366 connmgr_send_requestforward(ofproto
->connmgr
, req
->ofconn
, &rf
);
7370 /* Delete all groups from 'ofproto'.
7372 * This is intended for use within an ofproto provider's 'destruct'
7375 ofproto_group_delete_all__(struct ofproto
*ofproto
)
7376 OVS_REQUIRES(ofproto_mutex
)
7378 struct ofproto_group_mod ogm
;
7379 ogm
.gm
.command
= OFPGC11_DELETE
;
7380 ogm
.gm
.group_id
= OFPG_ALL
;
7381 ogm
.version
= ofproto
->tables_version
+ 1;
7383 ofproto_group_mod_start(ofproto
, &ogm
);
7384 ofproto_bump_tables_version(ofproto
);
7385 ofproto_group_mod_finish(ofproto
, &ogm
, NULL
);
7388 /* Delete all groups from 'ofproto'.
7390 * This is intended for use within an ofproto provider's 'destruct'
7393 ofproto_group_delete_all(struct ofproto
*ofproto
)
7394 OVS_EXCLUDED(ofproto_mutex
)
7396 ovs_mutex_lock(&ofproto_mutex
);
7397 ofproto_group_delete_all__(ofproto
);
7398 ovs_mutex_unlock(&ofproto_mutex
);
7402 handle_group_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7403 OVS_EXCLUDED(ofproto_mutex
)
7405 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7406 struct ofproto_group_mod ogm
;
7409 error
= reject_slave_controller(ofconn
);
7414 error
= ofputil_decode_group_mod(oh
, &ogm
.gm
);
7419 ovs_mutex_lock(&ofproto_mutex
);
7420 ogm
.version
= ofproto
->tables_version
+ 1;
7421 error
= ofproto_group_mod_start(ofproto
, &ogm
);
7423 struct openflow_mod_requester req
= { ofconn
, oh
};
7425 ofproto_bump_tables_version(ofproto
);
7426 ofproto_group_mod_finish(ofproto
, &ogm
, &req
);
7427 ofmonitor_flush(ofproto
->connmgr
);
7429 ovs_mutex_unlock(&ofproto_mutex
);
7431 ofputil_uninit_group_mod(&ogm
.gm
);
7436 enum ofputil_table_miss
7437 ofproto_table_get_miss_config(const struct ofproto
*ofproto
, uint8_t table_id
)
7439 enum ofputil_table_miss miss
;
7441 atomic_read_relaxed(&ofproto
->tables
[table_id
].miss_config
, &miss
);
7446 table_mod__(struct oftable
*oftable
,
7447 const struct ofputil_table_mod
*tm
)
7449 if (tm
->miss
== OFPUTIL_TABLE_MISS_DEFAULT
) {
7450 /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any
7451 * table-miss configuration (because the protocol used doesn't have
7452 * such a concept), so there's nothing to do. */
7454 atomic_store_relaxed(&oftable
->miss_config
, tm
->miss
);
7457 unsigned int new_eviction
= oftable
->eviction
;
7458 if (tm
->eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
7459 new_eviction
|= EVICTION_OPENFLOW
;
7460 } else if (tm
->eviction
== OFPUTIL_TABLE_EVICTION_OFF
) {
7461 new_eviction
&= ~EVICTION_OPENFLOW
;
7464 if (new_eviction
!= oftable
->eviction
) {
7465 ovs_mutex_lock(&ofproto_mutex
);
7466 oftable_configure_eviction(oftable
, new_eviction
,
7467 oftable
->eviction_fields
,
7468 oftable
->n_eviction_fields
);
7469 ovs_mutex_unlock(&ofproto_mutex
);
7472 if (tm
->vacancy
!= OFPUTIL_TABLE_VACANCY_DEFAULT
) {
7473 ovs_mutex_lock(&ofproto_mutex
);
7474 oftable
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
7475 oftable
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
7476 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_OFF
) {
7477 oftable
->vacancy_event
= 0;
7478 } else if (!oftable
->vacancy_event
) {
7479 uint8_t vacancy
= oftable_vacancy(oftable
);
7480 oftable
->vacancy_event
= (vacancy
< oftable
->vacancy_up
7482 : OFPTR_VACANCY_DOWN
);
7484 ovs_mutex_unlock(&ofproto_mutex
);
7489 table_mod(struct ofproto
*ofproto
, const struct ofputil_table_mod
*tm
)
7491 if (!check_table_id(ofproto
, tm
->table_id
)) {
7492 return OFPERR_OFPTMFC_BAD_TABLE
;
7495 /* Don't allow the eviction flags to be changed (except to the only fixed
7496 * value that OVS supports). OF1.4 says this is normal: "The
7497 * OFPTMPT_EVICTION property usually cannot be modified using a
7498 * OFP_TABLE_MOD request, because the eviction mechanism is switch
7500 if (tm
->eviction_flags
!= UINT32_MAX
7501 && tm
->eviction_flags
!= OFPROTO_EVICTION_FLAGS
) {
7502 return OFPERR_OFPTMFC_BAD_CONFIG
;
7505 if (tm
->table_id
== OFPTT_ALL
) {
7506 struct oftable
*oftable
;
7507 OFPROTO_FOR_EACH_TABLE (oftable
, ofproto
) {
7508 if (!(oftable
->flags
& (OFTABLE_HIDDEN
| OFTABLE_READONLY
))) {
7509 table_mod__(oftable
, tm
);
7513 struct oftable
*oftable
= &ofproto
->tables
[tm
->table_id
];
7514 if (oftable
->flags
& OFTABLE_READONLY
) {
7515 return OFPERR_OFPTMFC_EPERM
;
7517 table_mod__(oftable
, tm
);
7524 handle_table_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7526 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7527 struct ofputil_table_mod tm
;
7530 error
= reject_slave_controller(ofconn
);
7535 error
= ofputil_decode_table_mod(oh
, &tm
);
7540 return table_mod(ofproto
, &tm
);
7543 /* Free resources that may be allocated by ofproto_flow_mod_init(). */
7545 ofproto_flow_mod_uninit(struct ofproto_flow_mod
*ofm
)
7547 if (ofm
->temp_rule
) {
7548 ofproto_rule_unref(ofm
->temp_rule
);
7549 ofm
->temp_rule
= NULL
;
7551 if (ofm
->criteria
.version
!= OVS_VERSION_NOT_REMOVED
) {
7552 rule_criteria_destroy(&ofm
->criteria
);
7562 ofproto_flow_mod_init(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
7563 const struct ofputil_flow_mod
*fm
, struct rule
*rule
)
7564 OVS_EXCLUDED(ofproto_mutex
)
7568 /* Forward flow mod fields we need later. */
7569 ofm
->command
= fm
->command
;
7570 ofm
->modify_cookie
= fm
->modify_cookie
;
7572 ofm
->modify_may_add_flow
= (fm
->new_cookie
!= OVS_BE64_MAX
7573 && fm
->cookie_mask
== htonll(0));
7574 /* Old flags must be kept when modifying a flow, but we still must
7575 * honor the reset counts flag if present in the flow mod. */
7576 ofm
->modify_keep_counts
= !(fm
->flags
& OFPUTIL_FF_RESET_COUNTS
);
7578 /* Initialize state needed by ofproto_flow_mod_uninit(). */
7579 ofm
->temp_rule
= rule
;
7580 ofm
->criteria
.version
= OVS_VERSION_NOT_REMOVED
;
7584 bool check_buffer_id
= false;
7586 switch (ofm
->command
) {
7588 check_buffer_id
= true;
7589 error
= add_flow_init(ofproto
, ofm
, fm
);
7592 check_buffer_id
= true;
7593 error
= modify_flows_init_loose(ofproto
, ofm
, fm
);
7595 case OFPFC_MODIFY_STRICT
:
7596 check_buffer_id
= true;
7597 error
= modify_flow_init_strict(ofproto
, ofm
, fm
);
7600 error
= delete_flows_init_loose(ofproto
, ofm
, fm
);
7602 case OFPFC_DELETE_STRICT
:
7603 error
= delete_flows_init_strict(ofproto
, ofm
, fm
);
7606 error
= OFPERR_OFPFMFC_BAD_COMMAND
;
7609 if (!error
&& check_buffer_id
&& fm
->buffer_id
!= UINT32_MAX
) {
7610 error
= OFPERR_OFPBRC_BUFFER_UNKNOWN
;
7614 ofproto_flow_mod_uninit(ofm
);
7620 ofproto_flow_mod_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
7621 OVS_REQUIRES(ofproto_mutex
)
7625 rule_collection_init(&ofm
->old_rules
);
7626 rule_collection_init(&ofm
->new_rules
);
7628 switch (ofm
->command
) {
7630 error
= add_flow_start(ofproto
, ofm
);
7633 error
= modify_flows_start_loose(ofproto
, ofm
);
7635 case OFPFC_MODIFY_STRICT
:
7636 error
= modify_flow_start_strict(ofproto
, ofm
);
7639 error
= delete_flows_start_loose(ofproto
, ofm
);
7641 case OFPFC_DELETE_STRICT
:
7642 error
= delete_flow_start_strict(ofproto
, ofm
);
7647 /* Release resources not needed after start. */
7648 ofproto_flow_mod_uninit(ofm
);
7651 rule_collection_destroy(&ofm
->old_rules
);
7652 rule_collection_destroy(&ofm
->new_rules
);
7658 ofproto_flow_mod_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
7659 OVS_REQUIRES(ofproto_mutex
)
7661 switch (ofm
->command
) {
7663 add_flow_revert(ofproto
, ofm
);
7667 case OFPFC_MODIFY_STRICT
:
7668 modify_flows_revert(ofproto
, ofm
);
7672 case OFPFC_DELETE_STRICT
:
7673 delete_flows_revert(ofproto
, ofm
);
7680 rule_collection_destroy(&ofm
->old_rules
);
7681 rule_collection_destroy(&ofm
->new_rules
);
7685 ofproto_flow_mod_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
7686 const struct openflow_mod_requester
*req
)
7687 OVS_REQUIRES(ofproto_mutex
)
7689 switch (ofm
->command
) {
7691 add_flow_finish(ofproto
, ofm
, req
);
7695 case OFPFC_MODIFY_STRICT
:
7696 modify_flows_finish(ofproto
, ofm
, req
);
7700 case OFPFC_DELETE_STRICT
:
7701 delete_flows_finish(ofproto
, ofm
, req
);
7708 rule_collection_destroy(&ofm
->old_rules
);
7709 rule_collection_destroy(&ofm
->new_rules
);
7712 ofconn_report_flow_mod(req
->ofconn
, ofm
->command
);
7716 /* Commit phases (all while locking ofproto_mutex):
7718 * 1. Begin: Gather resources and make changes visible in the next version.
7719 * - Mark affected rules for removal in the next version.
7720 * - Create new replacement rules, make visible in the next
7722 * - Do not send any events or notifications.
7724 * 2. Revert: Fail if any errors are found. After this point no errors are
7725 * possible. No visible changes were made, so rollback is minimal (remove
7726 * added invisible rules, restore visibility of rules marked for removal).
7728 * 3. Finish: Make the changes visible for lookups. Insert replacement rules to
7729 * the ofproto provider. Remove replaced and deleted rules from ofproto data
7730 * structures, and Schedule postponed removal of deleted rules from the
7731 * classifier. Send notifications, buffered packets, etc.
7734 do_bundle_commit(struct ofconn
*ofconn
, uint32_t id
, uint16_t flags
)
7736 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7737 ovs_version_t version
= ofproto
->tables_version
+ 1;
7738 struct ofp_bundle
*bundle
;
7739 struct ofp_bundle_entry
*be
;
7742 bundle
= ofconn_get_bundle(ofconn
, id
);
7745 return OFPERR_OFPBFC_BAD_ID
;
7747 if (bundle
->flags
!= flags
) {
7748 error
= OFPERR_OFPBFC_BAD_FLAGS
;
7750 bool prev_is_port_mod
= false;
7753 ovs_mutex_lock(&ofproto_mutex
);
7756 LIST_FOR_EACH (be
, node
, &bundle
->msg_list
) {
7757 if (be
->type
== OFPTYPE_PORT_MOD
) {
7758 /* Our port mods are not atomic. */
7759 if (flags
& OFPBF_ATOMIC
) {
7760 error
= OFPERR_OFPBFC_MSG_FAILED
;
7762 prev_is_port_mod
= true;
7763 error
= port_mod_start(ofconn
, &be
->opm
.pm
, &be
->opm
.port
);
7766 /* Flow & group mods between port mods are applied as a single
7767 * version, but the versions are published only after we know
7768 * the commit is successful. */
7769 if (prev_is_port_mod
) {
7770 prev_is_port_mod
= false;
7773 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7774 /* Store the version in which the changes should take
7776 be
->ofm
.version
= version
;
7777 error
= ofproto_flow_mod_start(ofproto
, &be
->ofm
);
7778 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7779 /* Store the version in which the changes should take
7781 be
->ogm
.version
= version
;
7782 error
= ofproto_group_mod_start(ofproto
, &be
->ogm
);
7783 } else if (be
->type
== OFPTYPE_PACKET_OUT
) {
7784 be
->opo
.version
= version
;
7785 error
= ofproto_packet_out_start(ofproto
, &be
->opo
);
7796 /* Send error referring to the original message. */
7798 ofconn_send_error(ofconn
, be
->msg
, error
);
7799 error
= OFPERR_OFPBFC_MSG_FAILED
;
7802 /* 2. Revert. Undo all the changes made above. */
7803 LIST_FOR_EACH_REVERSE_CONTINUE(be
, node
, &bundle
->msg_list
) {
7804 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7805 ofproto_flow_mod_revert(ofproto
, &be
->ofm
);
7806 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7807 ofproto_group_mod_revert(ofproto
, &be
->ogm
);
7808 } else if (be
->type
== OFPTYPE_PACKET_OUT
) {
7809 ofproto_packet_out_revert(ofproto
, &be
->opo
);
7811 /* Nothing needs to be reverted for a port mod. */
7815 LIST_FOR_EACH (be
, node
, &bundle
->msg_list
) {
7816 if (be
->type
== OFPTYPE_PORT_MOD
) {
7817 /* Perform the actual port mod. This is not atomic, i.e.,
7818 * the effects will be immediately seen by upcall
7819 * processing regardless of the lookup version. It should
7820 * be noted that port configuration changes can originate
7821 * also from OVSDB changes asynchronously to all upcall
7823 port_mod_finish(ofconn
, &be
->opm
.pm
, be
->opm
.port
);
7826 (be
->type
== OFPTYPE_FLOW_MOD
) ? be
->ofm
.version
:
7827 (be
->type
== OFPTYPE_GROUP_MOD
) ? be
->ogm
.version
:
7828 (be
->type
== OFPTYPE_PACKET_OUT
) ? be
->opo
.version
:
7831 /* Bump the lookup version to the one of the current
7832 * message. This makes all the changes in the bundle at
7833 * this version visible to lookups at once. */
7834 if (ofproto
->tables_version
< version
) {
7835 ofproto
->tables_version
= version
;
7836 ofproto
->ofproto_class
->set_tables_version(
7837 ofproto
, ofproto
->tables_version
);
7840 struct openflow_mod_requester req
= { ofconn
, be
->msg
};
7842 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7843 ofproto_flow_mod_finish(ofproto
, &be
->ofm
, &req
);
7844 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7845 ofproto_group_mod_finish(ofproto
, &be
->ogm
, &req
);
7846 } else if (be
->type
== OFPTYPE_PACKET_OUT
) {
7847 ofproto_packet_out_finish(ofproto
, &be
->opo
);
7853 ofmonitor_flush(ofproto
->connmgr
);
7854 ovs_mutex_unlock(&ofproto_mutex
);
7857 /* The bundle is discarded regardless the outcome. */
7858 ofp_bundle_remove__(ofconn
, bundle
);
7863 handle_bundle_control(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7865 struct ofputil_bundle_ctrl_msg bctrl
;
7866 struct ofputil_bundle_ctrl_msg reply
;
7870 error
= reject_slave_controller(ofconn
);
7875 error
= ofputil_decode_bundle_ctrl(oh
, &bctrl
);
7880 reply
.bundle_id
= bctrl
.bundle_id
;
7882 switch (bctrl
.type
) {
7883 case OFPBCT_OPEN_REQUEST
:
7884 error
= ofp_bundle_open(ofconn
, bctrl
.bundle_id
, bctrl
.flags
, oh
);
7885 reply
.type
= OFPBCT_OPEN_REPLY
;
7887 case OFPBCT_CLOSE_REQUEST
:
7888 error
= ofp_bundle_close(ofconn
, bctrl
.bundle_id
, bctrl
.flags
);
7889 reply
.type
= OFPBCT_CLOSE_REPLY
;
7891 case OFPBCT_COMMIT_REQUEST
:
7892 error
= do_bundle_commit(ofconn
, bctrl
.bundle_id
, bctrl
.flags
);
7893 reply
.type
= OFPBCT_COMMIT_REPLY
;
7895 case OFPBCT_DISCARD_REQUEST
:
7896 error
= ofp_bundle_discard(ofconn
, bctrl
.bundle_id
);
7897 reply
.type
= OFPBCT_DISCARD_REPLY
;
7900 case OFPBCT_OPEN_REPLY
:
7901 case OFPBCT_CLOSE_REPLY
:
7902 case OFPBCT_COMMIT_REPLY
:
7903 case OFPBCT_DISCARD_REPLY
:
7904 return OFPERR_OFPBFC_BAD_TYPE
;
7909 buf
= ofputil_encode_bundle_ctrl_reply(oh
, &reply
);
7910 ofconn_send_reply(ofconn
, buf
);
7916 handle_bundle_add(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7917 OVS_EXCLUDED(ofproto_mutex
)
7919 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7921 struct ofputil_bundle_add_msg badd
;
7922 struct ofp_bundle_entry
*bmsg
;
7925 error
= reject_slave_controller(ofconn
);
7930 error
= ofputil_decode_bundle_add(oh
, &badd
, &type
);
7935 bmsg
= ofp_bundle_entry_alloc(type
, badd
.msg
);
7937 struct ofpbuf ofpacts
;
7938 uint64_t ofpacts_stub
[1024 / 8];
7939 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
7941 if (type
== OFPTYPE_PORT_MOD
) {
7942 error
= ofputil_decode_port_mod(badd
.msg
, &bmsg
->opm
.pm
, false);
7943 } else if (type
== OFPTYPE_FLOW_MOD
) {
7944 struct ofputil_flow_mod fm
;
7946 error
= ofputil_decode_flow_mod(&fm
, badd
.msg
,
7947 ofconn_get_protocol(ofconn
),
7948 ofproto_get_tun_tab(ofproto
),
7949 &ofproto
->vl_mff_map
, &ofpacts
,
7950 u16_to_ofp(ofproto
->max_ports
),
7953 error
= ofproto_flow_mod_init(ofproto
, &bmsg
->ofm
, &fm
, NULL
);
7955 } else if (type
== OFPTYPE_GROUP_MOD
) {
7956 error
= ofputil_decode_group_mod(badd
.msg
, &bmsg
->ogm
.gm
);
7957 } else if (type
== OFPTYPE_PACKET_OUT
) {
7958 struct ofputil_packet_out po
;
7960 COVERAGE_INC(ofproto_packet_out
);
7962 /* Decode message. */
7963 error
= ofputil_decode_packet_out(&po
, badd
.msg
,
7964 ofproto_get_tun_tab(ofproto
),
7967 po
.ofpacts
= ofpbuf_steal_data(&ofpacts
); /* Move to heap. */
7968 error
= ofproto_packet_out_init(ofproto
, ofconn
, &bmsg
->opo
, &po
);
7974 ofpbuf_uninit(&ofpacts
);
7977 error
= ofp_bundle_add_message(ofconn
, badd
.bundle_id
, badd
.flags
,
7982 ofp_bundle_entry_free(bmsg
);
7989 handle_tlv_table_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7991 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7992 struct tun_table
*old_tab
, *new_tab
;
7993 struct ofputil_tlv_table_mod ttm
;
7996 error
= reject_slave_controller(ofconn
);
8001 error
= ofputil_decode_tlv_table_mod(oh
, &ttm
);
8006 old_tab
= ovsrcu_get_protected(struct tun_table
*, &ofproto
->metadata_tab
);
8007 error
= tun_metadata_table_mod(&ttm
, old_tab
, &new_tab
);
8009 ovs_mutex_lock(&ofproto
->vl_mff_map
.mutex
);
8010 error
= mf_vl_mff_map_mod_from_tun_metadata(&ofproto
->vl_mff_map
,
8012 ovs_mutex_unlock(&ofproto
->vl_mff_map
.mutex
);
8014 ovsrcu_set(&ofproto
->metadata_tab
, new_tab
);
8015 tun_metadata_postpone_free(old_tab
);
8017 tun_metadata_free(new_tab
);
8021 ofputil_uninit_tlv_table(&ttm
.mappings
);
8026 handle_tlv_table_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
8028 const struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
8029 struct ofputil_tlv_table_reply ttr
;
8032 tun_metadata_table_request(ofproto_get_tun_tab(ofproto
), &ttr
);
8034 b
= ofputil_encode_tlv_table_reply(oh
, &ttr
);
8035 ofputil_uninit_tlv_table(&ttr
.mappings
);
8037 ofconn_send_reply(ofconn
, b
);
8042 handle_openflow__(struct ofconn
*ofconn
, const struct ofpbuf
*msg
)
8043 OVS_EXCLUDED(ofproto_mutex
)
8045 const struct ofp_header
*oh
= msg
->data
;
8049 error
= ofptype_decode(&type
, oh
);
8053 if (oh
->version
>= OFP13_VERSION
&& ofpmsg_is_stat_request(oh
)
8054 && ofpmp_more(oh
)) {
8055 /* We have no buffer implementation for multipart requests.
8056 * Report overflow for requests which consists of multiple
8058 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW
;
8062 /* OpenFlow requests. */
8063 case OFPTYPE_ECHO_REQUEST
:
8064 return handle_echo_request(ofconn
, oh
);
8066 case OFPTYPE_FEATURES_REQUEST
:
8067 return handle_features_request(ofconn
, oh
);
8069 case OFPTYPE_GET_CONFIG_REQUEST
:
8070 return handle_get_config_request(ofconn
, oh
);
8072 case OFPTYPE_SET_CONFIG
:
8073 return handle_set_config(ofconn
, oh
);
8075 case OFPTYPE_PACKET_OUT
:
8076 return handle_packet_out(ofconn
, oh
);
8078 case OFPTYPE_PORT_MOD
:
8079 return handle_port_mod(ofconn
, oh
);
8081 case OFPTYPE_FLOW_MOD
:
8082 return handle_flow_mod(ofconn
, oh
);
8084 case OFPTYPE_GROUP_MOD
:
8085 return handle_group_mod(ofconn
, oh
);
8087 case OFPTYPE_TABLE_MOD
:
8088 return handle_table_mod(ofconn
, oh
);
8090 case OFPTYPE_METER_MOD
:
8091 return handle_meter_mod(ofconn
, oh
);
8093 case OFPTYPE_BARRIER_REQUEST
:
8094 return handle_barrier_request(ofconn
, oh
);
8096 case OFPTYPE_ROLE_REQUEST
:
8097 return handle_role_request(ofconn
, oh
);
8099 /* OpenFlow replies. */
8100 case OFPTYPE_ECHO_REPLY
:
8103 /* Nicira extension requests. */
8104 case OFPTYPE_FLOW_MOD_TABLE_ID
:
8105 return handle_nxt_flow_mod_table_id(ofconn
, oh
);
8107 case OFPTYPE_SET_FLOW_FORMAT
:
8108 return handle_nxt_set_flow_format(ofconn
, oh
);
8110 case OFPTYPE_SET_PACKET_IN_FORMAT
:
8111 return handle_nxt_set_packet_in_format(ofconn
, oh
);
8113 case OFPTYPE_SET_CONTROLLER_ID
:
8114 return handle_nxt_set_controller_id(ofconn
, oh
);
8116 case OFPTYPE_FLOW_AGE
:
8117 /* Nothing to do. */
8120 case OFPTYPE_FLOW_MONITOR_CANCEL
:
8121 return handle_flow_monitor_cancel(ofconn
, oh
);
8123 case OFPTYPE_SET_ASYNC_CONFIG
:
8124 return handle_nxt_set_async_config(ofconn
, oh
);
8126 case OFPTYPE_GET_ASYNC_REQUEST
:
8127 return handle_nxt_get_async_request(ofconn
, oh
);
8129 case OFPTYPE_NXT_RESUME
:
8130 return handle_nxt_resume(ofconn
, oh
);
8132 /* Statistics requests. */
8133 case OFPTYPE_DESC_STATS_REQUEST
:
8134 return handle_desc_stats_request(ofconn
, oh
);
8136 case OFPTYPE_FLOW_STATS_REQUEST
:
8137 return handle_flow_stats_request(ofconn
, oh
);
8139 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
8140 return handle_aggregate_stats_request(ofconn
, oh
);
8142 case OFPTYPE_TABLE_STATS_REQUEST
:
8143 return handle_table_stats_request(ofconn
, oh
);
8145 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
8146 return handle_table_features_request(ofconn
, oh
);
8148 case OFPTYPE_TABLE_DESC_REQUEST
:
8149 return handle_table_desc_request(ofconn
, oh
);
8151 case OFPTYPE_PORT_STATS_REQUEST
:
8152 return handle_port_stats_request(ofconn
, oh
);
8154 case OFPTYPE_QUEUE_STATS_REQUEST
:
8155 return handle_queue_stats_request(ofconn
, oh
);
8157 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
8158 return handle_port_desc_stats_request(ofconn
, oh
);
8160 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
8161 return handle_flow_monitor_request(ofconn
, oh
);
8163 case OFPTYPE_METER_STATS_REQUEST
:
8164 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
8165 return handle_meter_request(ofconn
, oh
, type
);
8167 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
8168 return handle_meter_features_request(ofconn
, oh
);
8170 case OFPTYPE_GROUP_STATS_REQUEST
:
8171 return handle_group_stats_request(ofconn
, oh
);
8173 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
8174 return handle_group_desc_stats_request(ofconn
, oh
);
8176 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
8177 return handle_group_features_stats_request(ofconn
, oh
);
8179 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
8180 return handle_queue_get_config_request(ofconn
, oh
);
8182 case OFPTYPE_BUNDLE_CONTROL
:
8183 return handle_bundle_control(ofconn
, oh
);
8185 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
8186 return handle_bundle_add(ofconn
, oh
);
8188 case OFPTYPE_NXT_TLV_TABLE_MOD
:
8189 return handle_tlv_table_mod(ofconn
, oh
);
8191 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
8192 return handle_tlv_table_request(ofconn
, oh
);
8194 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST
:
8195 return handle_ipfix_bridge_stats_request(ofconn
, oh
);
8197 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST
:
8198 return handle_ipfix_flow_stats_request(ofconn
, oh
);
8200 case OFPTYPE_CT_FLUSH_ZONE
:
8201 return handle_nxt_ct_flush_zone(ofconn
, oh
);
8205 case OFPTYPE_FEATURES_REPLY
:
8206 case OFPTYPE_GET_CONFIG_REPLY
:
8207 case OFPTYPE_PACKET_IN
:
8208 case OFPTYPE_FLOW_REMOVED
:
8209 case OFPTYPE_PORT_STATUS
:
8210 case OFPTYPE_BARRIER_REPLY
:
8211 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
8212 case OFPTYPE_DESC_STATS_REPLY
:
8213 case OFPTYPE_FLOW_STATS_REPLY
:
8214 case OFPTYPE_QUEUE_STATS_REPLY
:
8215 case OFPTYPE_PORT_STATS_REPLY
:
8216 case OFPTYPE_TABLE_STATS_REPLY
:
8217 case OFPTYPE_AGGREGATE_STATS_REPLY
:
8218 case OFPTYPE_PORT_DESC_STATS_REPLY
:
8219 case OFPTYPE_ROLE_REPLY
:
8220 case OFPTYPE_FLOW_MONITOR_PAUSED
:
8221 case OFPTYPE_FLOW_MONITOR_RESUMED
:
8222 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
8223 case OFPTYPE_GET_ASYNC_REPLY
:
8224 case OFPTYPE_GROUP_STATS_REPLY
:
8225 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
8226 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
8227 case OFPTYPE_METER_STATS_REPLY
:
8228 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
8229 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
8230 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
8231 case OFPTYPE_TABLE_DESC_REPLY
:
8232 case OFPTYPE_ROLE_STATUS
:
8233 case OFPTYPE_REQUESTFORWARD
:
8234 case OFPTYPE_TABLE_STATUS
:
8235 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
8236 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY
:
8237 case OFPTYPE_IPFIX_FLOW_STATS_REPLY
:
8239 if (ofpmsg_is_stat_request(oh
)) {
8240 return OFPERR_OFPBRC_BAD_STAT
;
8242 return OFPERR_OFPBRC_BAD_TYPE
;
8248 handle_openflow(struct ofconn
*ofconn
, const struct ofpbuf
*ofp_msg
)
8249 OVS_EXCLUDED(ofproto_mutex
)
8251 enum ofperr error
= handle_openflow__(ofconn
, ofp_msg
);
8254 ofconn_send_error(ofconn
, ofp_msg
->data
, error
);
8256 COVERAGE_INC(ofproto_recv_openflow
);
8260 pick_datapath_id(const struct ofproto
*ofproto
)
8262 const struct ofport
*port
;
8264 port
= ofproto_get_port(ofproto
, OFPP_LOCAL
);
8269 error
= netdev_get_etheraddr(port
->netdev
, &ea
);
8271 return eth_addr_to_uint64(ea
);
8273 VLOG_WARN("%s: could not get MAC address for %s (%s)",
8274 ofproto
->name
, netdev_get_name(port
->netdev
),
8275 ovs_strerror(error
));
8277 return ofproto
->fallback_dpid
;
8281 pick_fallback_dpid(void)
8284 eth_addr_nicira_random(&ea
);
8285 return eth_addr_to_uint64(ea
);
8288 /* Table overflow policy. */
8290 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
8291 * to NULL if the table is not configured to evict rules or if the table
8292 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
8293 * or with no timeouts are not evictable.) */
8295 choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
8296 OVS_REQUIRES(ofproto_mutex
)
8298 struct eviction_group
*evg
;
8301 if (!table
->eviction
) {
8305 /* In the common case, the outer and inner loops here will each be entered
8308 * - The inner loop normally "return"s in its first iteration. If the
8309 * eviction group has any evictable rules, then it always returns in
8312 * - The outer loop only iterates more than once if the largest eviction
8313 * group has no evictable rules.
8315 * - The outer loop can exit only if table's 'max_flows' is all filled up
8316 * by unevictable rules. */
8317 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
8320 HEAP_FOR_EACH (rule
, evg_node
, &evg
->rules
) {
8329 /* Eviction groups. */
8331 /* Returns the priority to use for an eviction_group that contains 'n_rules'
8332 * rules. The priority contains low-order random bits to ensure that eviction
8333 * groups with the same number of rules are prioritized randomly. */
8335 eviction_group_priority(size_t n_rules
)
8337 uint16_t size
= MIN(UINT16_MAX
, n_rules
);
8338 return (size
<< 16) | random_uint16();
8341 /* Updates 'evg', an eviction_group within 'table', following a change that
8342 * adds or removes rules in 'evg'. */
8344 eviction_group_resized(struct oftable
*table
, struct eviction_group
*evg
)
8345 OVS_REQUIRES(ofproto_mutex
)
8347 heap_change(&table
->eviction_groups_by_size
, &evg
->size_node
,
8348 eviction_group_priority(heap_count(&evg
->rules
)));
8351 /* Destroys 'evg', an eviction_group within 'table':
8353 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
8354 * rules themselves, just removes them from the eviction group.)
8356 * - Removes 'evg' from 'table'.
8360 eviction_group_destroy(struct oftable
*table
, struct eviction_group
*evg
)
8361 OVS_REQUIRES(ofproto_mutex
)
8363 while (!heap_is_empty(&evg
->rules
)) {
8366 rule
= CONTAINER_OF(heap_pop(&evg
->rules
), struct rule
, evg_node
);
8367 rule
->eviction_group
= NULL
;
8369 hmap_remove(&table
->eviction_groups_by_id
, &evg
->id_node
);
8370 heap_remove(&table
->eviction_groups_by_size
, &evg
->size_node
);
8371 heap_destroy(&evg
->rules
);
8375 /* Removes 'rule' from its eviction group, if any. */
8377 eviction_group_remove_rule(struct rule
*rule
)
8378 OVS_REQUIRES(ofproto_mutex
)
8380 if (rule
->eviction_group
) {
8381 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
8382 struct eviction_group
*evg
= rule
->eviction_group
;
8384 rule
->eviction_group
= NULL
;
8385 heap_remove(&evg
->rules
, &rule
->evg_node
);
8386 if (heap_is_empty(&evg
->rules
)) {
8387 eviction_group_destroy(table
, evg
);
8389 eviction_group_resized(table
, evg
);
8394 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
8395 * returns the hash value. */
8397 eviction_group_hash_rule(struct rule
*rule
)
8398 OVS_REQUIRES(ofproto_mutex
)
8400 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
8401 const struct mf_subfield
*sf
;
8405 hash
= table
->eviction_group_id_basis
;
8406 miniflow_expand(rule
->cr
.match
.flow
, &flow
);
8407 for (sf
= table
->eviction_fields
;
8408 sf
< &table
->eviction_fields
[table
->n_eviction_fields
];
8411 if (mf_are_prereqs_ok(sf
->field
, &flow
, NULL
)) {
8412 union mf_value value
;
8414 mf_get_value(sf
->field
, &flow
, &value
);
8416 bitwise_zero(&value
, sf
->field
->n_bytes
, 0, sf
->ofs
);
8418 if (sf
->ofs
+ sf
->n_bits
< sf
->field
->n_bytes
* 8) {
8419 unsigned int start
= sf
->ofs
+ sf
->n_bits
;
8420 bitwise_zero(&value
, sf
->field
->n_bytes
, start
,
8421 sf
->field
->n_bytes
* 8 - start
);
8423 hash
= hash_bytes(&value
, sf
->field
->n_bytes
, hash
);
8425 hash
= hash_int(hash
, 0);
8432 /* Returns an eviction group within 'table' with the given 'id', creating one
8434 static struct eviction_group
*
8435 eviction_group_find(struct oftable
*table
, uint32_t id
)
8436 OVS_REQUIRES(ofproto_mutex
)
8438 struct eviction_group
*evg
;
8440 HMAP_FOR_EACH_WITH_HASH (evg
, id_node
, id
, &table
->eviction_groups_by_id
) {
8444 evg
= xmalloc(sizeof *evg
);
8445 hmap_insert(&table
->eviction_groups_by_id
, &evg
->id_node
, id
);
8446 heap_insert(&table
->eviction_groups_by_size
, &evg
->size_node
,
8447 eviction_group_priority(0));
8448 heap_init(&evg
->rules
);
8453 /* Returns an eviction priority for 'rule'. The return value should be
8454 * interpreted so that higher priorities make a rule a more attractive
8455 * candidate for eviction. */
8457 rule_eviction_priority(struct ofproto
*ofproto
, struct rule
*rule
)
8458 OVS_REQUIRES(ofproto_mutex
)
8460 /* Calculate absolute time when this flow will expire. If it will never
8461 * expire, then return 0 to make it unevictable. */
8462 long long int expiration
= LLONG_MAX
;
8463 if (rule
->hard_timeout
) {
8464 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
8465 ovs_mutex_lock(&rule
->mutex
);
8466 long long int modified
= rule
->modified
;
8467 ovs_mutex_unlock(&rule
->mutex
);
8469 expiration
= modified
+ rule
->hard_timeout
* 1000;
8471 if (rule
->idle_timeout
) {
8472 uint64_t packets
, bytes
;
8474 long long int idle_expiration
;
8476 ofproto
->ofproto_class
->rule_get_stats(rule
, &packets
, &bytes
, &used
);
8477 idle_expiration
= used
+ rule
->idle_timeout
* 1000;
8478 expiration
= MIN(expiration
, idle_expiration
);
8480 if (expiration
== LLONG_MAX
) {
8484 /* Calculate the time of expiration as a number of (approximate) seconds
8485 * after program startup.
8487 * This should work OK for program runs that last UINT32_MAX seconds or
8488 * less. Therefore, please restart OVS at least once every 136 years. */
8489 uint32_t expiration_ofs
= (expiration
>> 10) - (time_boot_msec() >> 10);
8491 /* Combine expiration time with OpenFlow "importance" to form a single
8492 * priority value. We want flows with relatively low "importance" to be
8493 * evicted before even considering expiration time, so put "importance" in
8494 * the most significant bits and expiration time in the least significant
8497 * Small 'priority' should be evicted before those with large 'priority'.
8498 * The caller expects the opposite convention (a large return value being
8499 * more attractive for eviction) so we invert it before returning. */
8500 uint64_t priority
= ((uint64_t) rule
->importance
<< 32) + expiration_ofs
;
8501 return UINT64_MAX
- priority
;
8504 /* Adds 'rule' to an appropriate eviction group for its oftable's
8505 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
8506 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
8509 * The caller must ensure that 'rule' is not already in an eviction group. */
8511 eviction_group_add_rule(struct rule
*rule
)
8512 OVS_REQUIRES(ofproto_mutex
)
8514 struct ofproto
*ofproto
= rule
->ofproto
;
8515 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
8518 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
8519 * so no additional protection is needed. */
8520 has_timeout
= rule
->hard_timeout
|| rule
->idle_timeout
;
8522 if (table
->eviction
&& has_timeout
) {
8523 struct eviction_group
*evg
;
8525 evg
= eviction_group_find(table
, eviction_group_hash_rule(rule
));
8527 rule
->eviction_group
= evg
;
8528 heap_insert(&evg
->rules
, &rule
->evg_node
,
8529 rule_eviction_priority(ofproto
, rule
));
8530 eviction_group_resized(table
, evg
);
8536 /* Initializes 'table'. */
8538 oftable_init(struct oftable
*table
)
8540 memset(table
, 0, sizeof *table
);
8541 classifier_init(&table
->cls
, flow_segment_u64s
);
8542 table
->max_flows
= UINT_MAX
;
8544 hmap_init(&table
->eviction_groups_by_id
);
8545 heap_init(&table
->eviction_groups_by_size
);
8546 atomic_init(&table
->miss_config
, OFPUTIL_TABLE_MISS_DEFAULT
);
8548 classifier_set_prefix_fields(&table
->cls
, default_prefix_fields
,
8549 ARRAY_SIZE(default_prefix_fields
));
8551 atomic_init(&table
->n_matched
, 0);
8552 atomic_init(&table
->n_missed
, 0);
8555 /* Destroys 'table', including its classifier and eviction groups.
8557 * The caller is responsible for freeing 'table' itself. */
8559 oftable_destroy(struct oftable
*table
)
8561 ovs_assert(classifier_is_empty(&table
->cls
));
8563 ovs_mutex_lock(&ofproto_mutex
);
8564 oftable_configure_eviction(table
, 0, NULL
, 0);
8565 ovs_mutex_unlock(&ofproto_mutex
);
8567 hmap_destroy(&table
->eviction_groups_by_id
);
8568 heap_destroy(&table
->eviction_groups_by_size
);
8569 classifier_destroy(&table
->cls
);
8573 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
8574 * string, then 'table' will use its default name.
8576 * This only affects the name exposed for a table exposed through the OpenFlow
8577 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
8579 oftable_set_name(struct oftable
*table
, const char *name
)
8581 if (name
&& name
[0]) {
8582 int len
= strnlen(name
, OFP_MAX_TABLE_NAME_LEN
);
8583 if (!table
->name
|| strncmp(name
, table
->name
, len
)) {
8585 table
->name
= xmemdup0(name
, len
);
8593 /* oftables support a choice of two policies when adding a rule would cause the
8594 * number of flows in the table to exceed the configured maximum number: either
8595 * they can refuse to add the new flow or they can evict some existing flow.
8596 * This function configures the latter policy on 'table', with fairness based
8597 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
8598 * 'n_fields' as 0 disables fairness.) */
8600 oftable_configure_eviction(struct oftable
*table
, unsigned int eviction
,
8601 const struct mf_subfield
*fields
, size_t n_fields
)
8602 OVS_REQUIRES(ofproto_mutex
)
8606 if ((table
->eviction
!= 0) == (eviction
!= 0)
8607 && n_fields
== table
->n_eviction_fields
8609 || !memcmp(fields
, table
->eviction_fields
,
8610 n_fields
* sizeof *fields
))) {
8611 /* The set of eviction fields did not change. If 'eviction' changed,
8612 * it remains nonzero, so that we can just update table->eviction
8613 * without fussing with the eviction groups. */
8614 table
->eviction
= eviction
;
8618 /* Destroy existing eviction groups, then destroy and recreate data
8619 * structures to recover memory. */
8620 struct eviction_group
*evg
, *next
;
8621 HMAP_FOR_EACH_SAFE (evg
, next
, id_node
, &table
->eviction_groups_by_id
) {
8622 eviction_group_destroy(table
, evg
);
8624 hmap_destroy(&table
->eviction_groups_by_id
);
8625 hmap_init(&table
->eviction_groups_by_id
);
8626 heap_destroy(&table
->eviction_groups_by_size
);
8627 heap_init(&table
->eviction_groups_by_size
);
8629 /* Replace eviction groups by the new ones, if there is a change. Free the
8630 * old fields only after allocating the new ones, because 'fields ==
8631 * table->eviction_fields' is possible. */
8632 struct mf_subfield
*old_fields
= table
->eviction_fields
;
8633 table
->n_eviction_fields
= n_fields
;
8634 table
->eviction_fields
= (fields
8635 ? xmemdup(fields
, n_fields
* sizeof *fields
)
8639 /* Add the new eviction groups, if enabled. */
8640 table
->eviction
= eviction
;
8641 if (table
->eviction
) {
8642 table
->eviction_group_id_basis
= random_uint32();
8643 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
8644 eviction_group_add_rule(rule
);
8649 /* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted
8650 * it to the classifier. */
8652 ofproto_rule_insert__(struct ofproto
*ofproto
, struct rule
*rule
)
8653 OVS_REQUIRES(ofproto_mutex
)
8655 const struct rule_actions
*actions
= rule_get_actions(rule
);
8657 /* A rule may not be reinserted. */
8658 ovs_assert(rule
->state
== RULE_INITIALIZED
);
8660 if (rule
->hard_timeout
|| rule
->idle_timeout
) {
8661 ovs_list_insert(&ofproto
->expirable
, &rule
->expirable
);
8663 cookies_insert(ofproto
, rule
);
8664 eviction_group_add_rule(rule
);
8665 if (actions
->has_meter
) {
8666 meter_insert_rule(rule
);
8668 if (actions
->has_groups
) {
8669 const struct ofpact_group
*a
;
8670 OFPACT_FOR_EACH_TYPE_FLATTENED (a
, GROUP
, actions
->ofpacts
,
8671 actions
->ofpacts_len
) {
8672 struct ofgroup
*group
;
8674 group
= ofproto_group_lookup(ofproto
, a
->group_id
, OVS_VERSION_MAX
,
8676 ovs_assert(group
!= NULL
);
8677 group_add_rule(group
, rule
);
8681 rule
->state
= RULE_INSERTED
;
8684 /* Removes 'rule' from the ofproto data structures. Caller may have deferred
8685 * the removal from the classifier. */
8687 ofproto_rule_remove__(struct ofproto
*ofproto
, struct rule
*rule
)
8688 OVS_REQUIRES(ofproto_mutex
)
8690 ovs_assert(rule
->state
== RULE_INSERTED
);
8692 cookies_remove(ofproto
, rule
);
8694 eviction_group_remove_rule(rule
);
8695 if (!ovs_list_is_empty(&rule
->expirable
)) {
8696 ovs_list_remove(&rule
->expirable
);
8698 if (!ovs_list_is_empty(&rule
->meter_list_node
)) {
8699 ovs_list_remove(&rule
->meter_list_node
);
8700 ovs_list_init(&rule
->meter_list_node
);
8703 /* Remove the rule from any groups, except from the group that is being
8704 * deleted, if any. */
8705 const struct rule_actions
*actions
= rule_get_actions(rule
);
8707 if (actions
->has_groups
) {
8708 const struct ofpact_group
*a
;
8710 OFPACT_FOR_EACH_TYPE_FLATTENED(a
, GROUP
, actions
->ofpacts
,
8711 actions
->ofpacts_len
) {
8712 struct ofgroup
*group
;
8714 group
= ofproto_group_lookup(ofproto
, a
->group_id
, OVS_VERSION_MAX
,
8718 /* Leave the rule for the group that is being deleted, if any,
8719 * as we still need the list of rules for clean-up. */
8720 if (!group
->being_deleted
) {
8721 group_remove_rule(group
, rule
);
8726 rule
->state
= RULE_REMOVED
;
8729 /* unixctl commands. */
8732 ofproto_lookup(const char *name
)
8734 struct ofproto
*ofproto
;
8736 HMAP_FOR_EACH_WITH_HASH (ofproto
, hmap_node
, hash_string(name
, 0),
8738 if (!strcmp(ofproto
->name
, name
)) {
8746 ofproto_unixctl_list(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
8747 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
8749 struct ofproto
*ofproto
;
8753 HMAP_FOR_EACH (ofproto
, hmap_node
, &all_ofprotos
) {
8754 ds_put_format(&results
, "%s\n", ofproto
->name
);
8756 unixctl_command_reply(conn
, ds_cstr(&results
));
8757 ds_destroy(&results
);
8761 ofproto_unixctl_init(void)
8763 static bool registered
;
8769 unixctl_command_register("ofproto/list", "", 0, 0,
8770 ofproto_unixctl_list
, NULL
);
8774 ofproto_set_vlan_limit(int vlan_limit
)
8776 flow_limit_vlans(vlan_limit
);