2 * Copyright (c) 2009-2016 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"
54 #include "poll-loop.h"
57 #include "openvswitch/shash.h"
62 #include "tun-metadata.h"
63 #include "unaligned.h"
67 VLOG_DEFINE_THIS_MODULE(ofproto
);
69 COVERAGE_DEFINE(ofproto_flush
);
70 COVERAGE_DEFINE(ofproto_packet_out
);
71 COVERAGE_DEFINE(ofproto_queue_req
);
72 COVERAGE_DEFINE(ofproto_recv_openflow
);
73 COVERAGE_DEFINE(ofproto_reinit_ports
);
74 COVERAGE_DEFINE(ofproto_update_port
);
76 /* Default fields to use for prefix tries in each flow table, unless something
77 * else is configured. */
78 const enum mf_field_id default_prefix_fields
[2] =
79 { MFF_IPV4_DST
, MFF_IPV4_SRC
};
82 static void oftable_init(struct oftable
*);
83 static void oftable_destroy(struct oftable
*);
85 static void oftable_set_name(struct oftable
*, const char *name
);
87 static enum ofperr
evict_rules_from_table(struct oftable
*)
88 OVS_REQUIRES(ofproto_mutex
);
89 static void oftable_configure_eviction(struct oftable
*,
90 unsigned int eviction
,
91 const struct mf_subfield
*fields
,
93 OVS_REQUIRES(ofproto_mutex
);
95 /* This is the only combination of OpenFlow eviction flags that OVS supports: a
96 * combination of OF1.4+ importance, the remaining lifetime of the flow, and
97 * fairness based on user-specified fields. */
98 #define OFPROTO_EVICTION_FLAGS \
99 (OFPTMPEF14_OTHER | OFPTMPEF14_IMPORTANCE | OFPTMPEF14_LIFETIME)
101 /* A set of rules within a single OpenFlow table (oftable) that have the same
102 * values for the oftable's eviction_fields. A rule to be evicted, when one is
103 * needed, is taken from the eviction group that contains the greatest number
106 * An oftable owns any number of eviction groups, each of which contains any
109 * Membership in an eviction group is imprecise, based on the hash of the
110 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
111 * That is, if two rules have different eviction_fields, but those
112 * eviction_fields hash to the same value, then they will belong to the same
113 * eviction_group anyway.
115 * (When eviction is not enabled on an oftable, we don't track any eviction
116 * groups, to save time and space.) */
117 struct eviction_group
{
118 struct hmap_node id_node
; /* In oftable's "eviction_groups_by_id". */
119 struct heap_node size_node
; /* In oftable's "eviction_groups_by_size". */
120 struct heap rules
; /* Contains "struct rule"s. */
123 static bool choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
124 OVS_REQUIRES(ofproto_mutex
);
125 static uint64_t rule_eviction_priority(struct ofproto
*ofproto
, struct rule
*)
126 OVS_REQUIRES(ofproto_mutex
);
127 static void eviction_group_add_rule(struct rule
*)
128 OVS_REQUIRES(ofproto_mutex
);
129 static void eviction_group_remove_rule(struct rule
*)
130 OVS_REQUIRES(ofproto_mutex
);
132 static void rule_criteria_init(struct rule_criteria
*, uint8_t table_id
,
133 const struct match
*match
, int priority
,
134 ovs_version_t version
,
135 ovs_be64 cookie
, ovs_be64 cookie_mask
,
136 ofp_port_t out_port
, uint32_t out_group
);
137 static void rule_criteria_require_rw(struct rule_criteria
*,
138 bool can_write_readonly
);
139 static void rule_criteria_destroy(struct rule_criteria
*);
141 static enum ofperr
collect_rules_loose(struct ofproto
*,
142 const struct rule_criteria
*,
143 struct rule_collection
*);
145 /* A packet that needs to be passed to rule_execute().
147 * (We can't do this immediately from ofopgroup_complete() because that holds
148 * ofproto_mutex, which rule_execute() needs released.) */
149 struct rule_execute
{
150 struct ovs_list list_node
; /* In struct ofproto's "rule_executes" list. */
151 struct rule
*rule
; /* Owns a reference to the rule. */
153 struct dp_packet
*packet
; /* Owns the packet. */
156 static void run_rule_executes(struct ofproto
*) OVS_EXCLUDED(ofproto_mutex
);
157 static void destroy_rule_executes(struct ofproto
*);
159 struct learned_cookie
{
161 /* In struct ofproto's 'learned_cookies' hmap. */
162 struct hmap_node hmap_node
OVS_GUARDED_BY(ofproto_mutex
);
164 /* In 'dead_cookies' list when removed from hmap. */
165 struct ovs_list list_node
;
169 ovs_be64 cookie
OVS_GUARDED_BY(ofproto_mutex
);
170 uint8_t table_id
OVS_GUARDED_BY(ofproto_mutex
);
172 /* Number of references from "learn" actions.
174 * When this drops to 0, all of the flows in 'table_id' with the specified
175 * 'cookie' are deleted. */
176 int n
OVS_GUARDED_BY(ofproto_mutex
);
179 static const struct ofpact_learn
*next_learn_with_delete(
180 const struct rule_actions
*, const struct ofpact_learn
*start
);
182 static void learned_cookies_inc(struct ofproto
*, const struct rule_actions
*)
183 OVS_REQUIRES(ofproto_mutex
);
184 static void learned_cookies_dec(struct ofproto
*, const struct rule_actions
*,
185 struct ovs_list
*dead_cookies
)
186 OVS_REQUIRES(ofproto_mutex
);
187 static void learned_cookies_flush(struct ofproto
*, struct ovs_list
*dead_cookies
)
188 OVS_REQUIRES(ofproto_mutex
);
191 static void ofport_destroy__(struct ofport
*) OVS_EXCLUDED(ofproto_mutex
);
192 static void ofport_destroy(struct ofport
*, bool del
);
193 static inline bool ofport_is_internal(const struct ofproto
*,
194 const struct ofport
*);
196 static int update_port(struct ofproto
*, const char *devname
);
197 static int init_ports(struct ofproto
*);
198 static void reinit_ports(struct ofproto
*);
200 static long long int ofport_get_usage(const struct ofproto
*,
201 ofp_port_t ofp_port
);
202 static void ofport_set_usage(struct ofproto
*, ofp_port_t ofp_port
,
203 long long int last_used
);
204 static void ofport_remove_usage(struct ofproto
*, ofp_port_t ofp_port
);
208 * Keeps track of the currently used and recently used ofport values and is
209 * used to prevent immediate recycling of ofport values. */
210 struct ofport_usage
{
211 struct hmap_node hmap_node
; /* In struct ofproto's "ofport_usage" hmap. */
212 ofp_port_t ofp_port
; /* OpenFlow port number. */
213 long long int last_used
; /* Last time the 'ofp_port' was used. LLONG_MAX
214 represents in-use ofports. */
218 static void ofproto_rule_send_removed(struct rule
*)
219 OVS_EXCLUDED(ofproto_mutex
);
220 static bool rule_is_readonly(const struct rule
*);
221 static void ofproto_rule_insert__(struct ofproto
*, struct rule
*)
222 OVS_REQUIRES(ofproto_mutex
);
223 static void ofproto_rule_remove__(struct ofproto
*, struct rule
*)
224 OVS_REQUIRES(ofproto_mutex
);
226 /* The source of an OpenFlow request.
228 * A table modification request can be generated externally, via OpenFlow, or
229 * internally through a function call. This structure indicates the source of
230 * an OpenFlow-generated table modification. For an internal flow_mod, it
231 * isn't meaningful and thus supplied as NULL. */
232 struct openflow_mod_requester
{
233 struct ofconn
*ofconn
; /* Connection on which flow_mod arrived. */
234 const struct ofp_header
*request
;
238 static enum ofperr
ofproto_rule_create(struct ofproto
*, struct cls_rule
*,
239 uint8_t table_id
, ovs_be64 new_cookie
,
240 uint16_t idle_timeout
,
241 uint16_t hard_timeout
,
242 enum ofputil_flow_mod_flags flags
,
244 const struct ofpact
*ofpacts
,
246 struct rule
**new_rule
)
247 OVS_NO_THREAD_SAFETY_ANALYSIS
;
249 static void replace_rule_start(struct ofproto
*, struct ofproto_flow_mod
*,
250 struct rule
*old_rule
, struct rule
*new_rule
)
251 OVS_REQUIRES(ofproto_mutex
);
253 static void replace_rule_revert(struct ofproto
*, struct rule
*old_rule
,
254 struct rule
*new_rule
)
255 OVS_REQUIRES(ofproto_mutex
);
257 static void replace_rule_finish(struct ofproto
*, struct ofproto_flow_mod
*,
258 const struct openflow_mod_requester
*,
259 struct rule
*old_rule
, struct rule
*new_rule
,
260 struct ovs_list
*dead_cookies
)
261 OVS_REQUIRES(ofproto_mutex
);
262 static void delete_flows__(struct rule_collection
*,
263 enum ofp_flow_removed_reason
,
264 const struct openflow_mod_requester
*)
265 OVS_REQUIRES(ofproto_mutex
);
267 static void send_buffered_packet(const struct openflow_mod_requester
*,
268 uint32_t buffer_id
, struct rule
*)
269 OVS_REQUIRES(ofproto_mutex
);
271 static bool ofproto_group_exists(const struct ofproto
*, uint32_t group_id
);
272 static void handle_openflow(struct ofconn
*, const struct ofpbuf
*);
273 static enum ofperr
ofproto_flow_mod_init(struct ofproto
*,
274 struct ofproto_flow_mod
*,
275 const struct ofputil_flow_mod
*fm
)
276 OVS_EXCLUDED(ofproto_mutex
);
277 static enum ofperr
ofproto_flow_mod_start(struct ofproto
*,
278 struct ofproto_flow_mod
*)
279 OVS_REQUIRES(ofproto_mutex
);
280 static void ofproto_flow_mod_finish(struct ofproto
*,
281 struct ofproto_flow_mod
*,
282 const struct openflow_mod_requester
*)
283 OVS_REQUIRES(ofproto_mutex
);
284 static enum ofperr
handle_flow_mod__(struct ofproto
*,
285 const struct ofputil_flow_mod
*,
286 const struct openflow_mod_requester
*)
287 OVS_EXCLUDED(ofproto_mutex
);
288 static void calc_duration(long long int start
, long long int now
,
289 uint32_t *sec
, uint32_t *nsec
);
292 static uint64_t pick_datapath_id(const struct ofproto
*);
293 static uint64_t pick_fallback_dpid(void);
294 static void ofproto_destroy__(struct ofproto
*);
295 static void update_mtu(struct ofproto
*, struct ofport
*);
296 static void update_mtu_ofproto(struct ofproto
*);
297 static void meter_delete(struct ofproto
*, uint32_t first
, uint32_t last
);
298 static void meter_insert_rule(struct rule
*);
301 static void ofproto_unixctl_init(void);
303 /* All registered ofproto classes, in probe order. */
304 static const struct ofproto_class
**ofproto_classes
;
305 static size_t n_ofproto_classes
;
306 static size_t allocated_ofproto_classes
;
308 /* Global lock that protects all flow table operations. */
309 struct ovs_mutex ofproto_mutex
= OVS_MUTEX_INITIALIZER
;
311 unsigned ofproto_flow_limit
= OFPROTO_FLOW_LIMIT_DEFAULT
;
312 unsigned ofproto_max_idle
= OFPROTO_MAX_IDLE_DEFAULT
;
314 size_t n_handlers
, n_revalidators
;
317 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
318 static struct hmap all_ofprotos
= HMAP_INITIALIZER(&all_ofprotos
);
320 /* Initial mappings of port to OpenFlow number mappings. */
321 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
323 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
325 /* The default value of true waits for flow restore. */
326 static bool flow_restore_wait
= true;
328 /* Must be called to initialize the ofproto library.
330 * The caller may pass in 'iface_hints', which contains an shash of
331 * "iface_hint" elements indexed by the interface's name. The provider
332 * may use these hints to describe the startup configuration in order to
333 * reinitialize its state. The caller owns the provided data, so a
334 * provider will make copies of anything required. An ofproto provider
335 * will remove any existing state that is not described by the hint, and
336 * may choose to remove it all. */
338 ofproto_init(const struct shash
*iface_hints
)
340 struct shash_node
*node
;
343 ofproto_class_register(&ofproto_dpif_class
);
345 /* Make a local copy, since we don't own 'iface_hints' elements. */
346 SHASH_FOR_EACH(node
, iface_hints
) {
347 const struct iface_hint
*orig_hint
= node
->data
;
348 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
349 const char *br_type
= ofproto_normalize_type(orig_hint
->br_type
);
351 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
352 new_hint
->br_type
= xstrdup(br_type
);
353 new_hint
->ofp_port
= orig_hint
->ofp_port
;
355 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
358 for (i
= 0; i
< n_ofproto_classes
; i
++) {
359 ofproto_classes
[i
]->init(&init_ofp_ports
);
362 ofproto_unixctl_init();
365 /* 'type' should be a normalized datapath type, as returned by
366 * ofproto_normalize_type(). Returns the corresponding ofproto_class
367 * structure, or a null pointer if there is none registered for 'type'. */
368 static const struct ofproto_class
*
369 ofproto_class_find__(const char *type
)
373 for (i
= 0; i
< n_ofproto_classes
; i
++) {
374 const struct ofproto_class
*class = ofproto_classes
[i
];
379 class->enumerate_types(&types
);
380 found
= sset_contains(&types
, type
);
381 sset_destroy(&types
);
387 VLOG_WARN("unknown datapath type %s", type
);
391 /* Registers a new ofproto class. After successful registration, new ofprotos
392 * of that type can be created using ofproto_create(). */
394 ofproto_class_register(const struct ofproto_class
*new_class
)
398 for (i
= 0; i
< n_ofproto_classes
; i
++) {
399 if (ofproto_classes
[i
] == new_class
) {
404 if (n_ofproto_classes
>= allocated_ofproto_classes
) {
405 ofproto_classes
= x2nrealloc(ofproto_classes
,
406 &allocated_ofproto_classes
,
407 sizeof *ofproto_classes
);
409 ofproto_classes
[n_ofproto_classes
++] = new_class
;
413 /* Unregisters a datapath provider. 'type' must have been previously
414 * registered and not currently be in use by any ofprotos. After
415 * unregistration new datapaths of that type cannot be opened using
416 * ofproto_create(). */
418 ofproto_class_unregister(const struct ofproto_class
*class)
422 for (i
= 0; i
< n_ofproto_classes
; i
++) {
423 if (ofproto_classes
[i
] == class) {
424 for (i
++; i
< n_ofproto_classes
; i
++) {
425 ofproto_classes
[i
- 1] = ofproto_classes
[i
];
431 VLOG_WARN("attempted to unregister an ofproto class that is not "
436 /* Clears 'types' and enumerates all registered ofproto types into it. The
437 * caller must first initialize the sset. */
439 ofproto_enumerate_types(struct sset
*types
)
444 for (i
= 0; i
< n_ofproto_classes
; i
++) {
445 ofproto_classes
[i
]->enumerate_types(types
);
449 /* Returns the fully spelled out name for the given ofproto 'type'.
451 * Normalized type string can be compared with strcmp(). Unnormalized type
452 * string might be the same even if they have different spellings. */
454 ofproto_normalize_type(const char *type
)
456 return type
&& type
[0] ? type
: "system";
459 /* Clears 'names' and enumerates the names of all known created ofprotos with
460 * the given 'type'. The caller must first initialize the sset. Returns 0 if
461 * successful, otherwise a positive errno value.
463 * Some kinds of datapaths might not be practically enumerable. This is not
464 * considered an error. */
466 ofproto_enumerate_names(const char *type
, struct sset
*names
)
468 const struct ofproto_class
*class = ofproto_class_find__(type
);
469 return class ? class->enumerate_names(type
, names
) : EAFNOSUPPORT
;
473 ofproto_bump_tables_version(struct ofproto
*ofproto
)
475 ++ofproto
->tables_version
;
476 ofproto
->ofproto_class
->set_tables_version(ofproto
,
477 ofproto
->tables_version
);
481 ofproto_create(const char *datapath_name
, const char *datapath_type
,
482 struct ofproto
**ofprotop
)
484 const struct ofproto_class
*class;
485 struct ofproto
*ofproto
;
491 datapath_type
= ofproto_normalize_type(datapath_type
);
492 class = ofproto_class_find__(datapath_type
);
494 VLOG_WARN("could not create datapath %s of unknown type %s",
495 datapath_name
, datapath_type
);
499 ofproto
= class->alloc();
501 VLOG_ERR("failed to allocate datapath %s of type %s",
502 datapath_name
, datapath_type
);
507 ovs_mutex_lock(&ofproto_mutex
);
508 memset(ofproto
, 0, sizeof *ofproto
);
509 ofproto
->ofproto_class
= class;
510 ofproto
->name
= xstrdup(datapath_name
);
511 ofproto
->type
= xstrdup(datapath_type
);
512 hmap_insert(&all_ofprotos
, &ofproto
->hmap_node
,
513 hash_string(ofproto
->name
, 0));
514 ofproto
->datapath_id
= 0;
515 ofproto
->forward_bpdu
= false;
516 ofproto
->fallback_dpid
= pick_fallback_dpid();
517 ofproto
->mfr_desc
= NULL
;
518 ofproto
->hw_desc
= NULL
;
519 ofproto
->sw_desc
= NULL
;
520 ofproto
->serial_desc
= NULL
;
521 ofproto
->dp_desc
= NULL
;
522 ofproto
->frag_handling
= OFPUTIL_FRAG_NORMAL
;
523 hmap_init(&ofproto
->ports
);
524 hmap_init(&ofproto
->ofport_usage
);
525 shash_init(&ofproto
->port_by_name
);
526 simap_init(&ofproto
->ofp_requests
);
527 ofproto
->max_ports
= ofp_to_u16(OFPP_MAX
);
528 ofproto
->eviction_group_timer
= LLONG_MIN
;
529 ofproto
->tables
= NULL
;
530 ofproto
->n_tables
= 0;
531 ofproto
->tables_version
= OVS_VERSION_MIN
;
532 hindex_init(&ofproto
->cookies
);
533 hmap_init(&ofproto
->learned_cookies
);
534 ovs_list_init(&ofproto
->expirable
);
535 ofproto
->connmgr
= connmgr_create(ofproto
, datapath_name
, datapath_name
);
536 guarded_list_init(&ofproto
->rule_executes
);
537 ofproto
->min_mtu
= INT_MAX
;
538 cmap_init(&ofproto
->groups
);
539 ovs_mutex_unlock(&ofproto_mutex
);
540 ofproto
->ogf
.types
= 0xf;
541 ofproto
->ogf
.capabilities
= OFPGFC_CHAINING
| OFPGFC_SELECT_LIVENESS
|
542 OFPGFC_SELECT_WEIGHT
;
543 for (i
= 0; i
< 4; i
++) {
544 ofproto
->ogf
.max_groups
[i
] = OFPG_MAX
;
545 ofproto
->ogf
.ofpacts
[i
] = (UINT64_C(1) << N_OFPACTS
) - 1;
549 error
= ofproto
->ofproto_class
->construct(ofproto
);
551 VLOG_ERR("failed to open datapath %s: %s",
552 datapath_name
, ovs_strerror(error
));
553 connmgr_destroy(ofproto
->connmgr
);
554 ofproto_destroy__(ofproto
);
558 /* Check that hidden tables, if any, are at the end. */
559 ovs_assert(ofproto
->n_tables
);
560 for (i
= 0; i
+ 1 < ofproto
->n_tables
; i
++) {
561 enum oftable_flags flags
= ofproto
->tables
[i
].flags
;
562 enum oftable_flags next_flags
= ofproto
->tables
[i
+ 1].flags
;
564 ovs_assert(!(flags
& OFTABLE_HIDDEN
) || next_flags
& OFTABLE_HIDDEN
);
567 ofproto
->datapath_id
= pick_datapath_id(ofproto
);
570 /* Initialize meters table. */
571 if (ofproto
->ofproto_class
->meter_get_features
) {
572 ofproto
->ofproto_class
->meter_get_features(ofproto
,
573 &ofproto
->meter_features
);
575 memset(&ofproto
->meter_features
, 0, sizeof ofproto
->meter_features
);
577 ofproto
->meters
= xzalloc((ofproto
->meter_features
.max_meters
+ 1)
578 * sizeof(struct meter
*));
580 /* Set the initial tables version. */
581 ofproto_bump_tables_version(ofproto
);
587 /* Must be called (only) by an ofproto implementation in its constructor
588 * function. See the large comment on 'construct' in struct ofproto_class for
591 ofproto_init_tables(struct ofproto
*ofproto
, int n_tables
)
593 struct oftable
*table
;
595 ovs_assert(!ofproto
->n_tables
);
596 ovs_assert(n_tables
>= 1 && n_tables
<= 255);
598 ofproto
->n_tables
= n_tables
;
599 ofproto
->tables
= xmalloc(n_tables
* sizeof *ofproto
->tables
);
600 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
605 /* To be optionally called (only) by an ofproto implementation in its
606 * constructor function. See the large comment on 'construct' in struct
607 * ofproto_class for details.
609 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
610 * will then ensure that actions passed into the ofproto implementation will
611 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
612 * function is not called, there will be no such restriction.
614 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
615 * the 'max_ports' restriction. */
617 ofproto_init_max_ports(struct ofproto
*ofproto
, uint16_t max_ports
)
619 ovs_assert(max_ports
<= ofp_to_u16(OFPP_MAX
));
620 ofproto
->max_ports
= max_ports
;
624 ofproto_get_datapath_id(const struct ofproto
*ofproto
)
626 return ofproto
->datapath_id
;
630 ofproto_set_datapath_id(struct ofproto
*p
, uint64_t datapath_id
)
632 uint64_t old_dpid
= p
->datapath_id
;
633 p
->datapath_id
= datapath_id
? datapath_id
: pick_datapath_id(p
);
634 if (p
->datapath_id
!= old_dpid
) {
635 /* Force all active connections to reconnect, since there is no way to
636 * notify a controller that the datapath ID has changed. */
637 ofproto_reconnect_controllers(p
);
642 ofproto_set_controllers(struct ofproto
*p
,
643 const struct ofproto_controller
*controllers
,
644 size_t n_controllers
, uint32_t allowed_versions
)
646 connmgr_set_controllers(p
->connmgr
, controllers
, n_controllers
,
651 ofproto_set_fail_mode(struct ofproto
*p
, enum ofproto_fail_mode fail_mode
)
653 connmgr_set_fail_mode(p
->connmgr
, fail_mode
);
656 /* Drops the connections between 'ofproto' and all of its controllers, forcing
657 * them to reconnect. */
659 ofproto_reconnect_controllers(struct ofproto
*ofproto
)
661 connmgr_reconnect(ofproto
->connmgr
);
664 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
665 * in-band control should guarantee access, in the same way that in-band
666 * control guarantees access to OpenFlow controllers. */
668 ofproto_set_extra_in_band_remotes(struct ofproto
*ofproto
,
669 const struct sockaddr_in
*extras
, size_t n
)
671 connmgr_set_extra_in_band_remotes(ofproto
->connmgr
, extras
, n
);
674 /* Sets the OpenFlow queue used by flows set up by in-band control on
675 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
676 * flows will use the default queue. */
678 ofproto_set_in_band_queue(struct ofproto
*ofproto
, int queue_id
)
680 connmgr_set_in_band_queue(ofproto
->connmgr
, queue_id
);
683 /* Sets the number of flows at which eviction from the kernel flow table
686 ofproto_set_flow_limit(unsigned limit
)
688 ofproto_flow_limit
= limit
;
691 /* Sets the maximum idle time for flows in the datapath before they are
694 ofproto_set_max_idle(unsigned max_idle
)
696 ofproto_max_idle
= max_idle
;
699 /* If forward_bpdu is true, the NORMAL action will forward frames with
700 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
701 * the NORMAL action will drop these frames. */
703 ofproto_set_forward_bpdu(struct ofproto
*ofproto
, bool forward_bpdu
)
705 bool old_val
= ofproto
->forward_bpdu
;
706 ofproto
->forward_bpdu
= forward_bpdu
;
707 if (old_val
!= ofproto
->forward_bpdu
) {
708 if (ofproto
->ofproto_class
->forward_bpdu_changed
) {
709 ofproto
->ofproto_class
->forward_bpdu_changed(ofproto
);
714 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
715 * 'idle_time', in seconds, and the maximum number of MAC table entries to
718 ofproto_set_mac_table_config(struct ofproto
*ofproto
, unsigned idle_time
,
721 if (ofproto
->ofproto_class
->set_mac_table_config
) {
722 ofproto
->ofproto_class
->set_mac_table_config(ofproto
, idle_time
,
727 /* Multicast snooping configuration. */
729 /* Configures multicast snooping on 'ofproto' using the settings
730 * defined in 's'. If 's' is NULL, disables multicast snooping.
732 * Returns 0 if successful, otherwise a positive errno value. */
734 ofproto_set_mcast_snooping(struct ofproto
*ofproto
,
735 const struct ofproto_mcast_snooping_settings
*s
)
737 return (ofproto
->ofproto_class
->set_mcast_snooping
738 ? ofproto
->ofproto_class
->set_mcast_snooping(ofproto
, s
)
742 /* Configures multicast snooping flood settings on 'ofp_port' of 'ofproto'.
744 * Returns 0 if successful, otherwise a positive errno value.*/
746 ofproto_port_set_mcast_snooping(struct ofproto
*ofproto
, void *aux
,
747 const struct ofproto_mcast_snooping_port_settings
*s
)
749 return (ofproto
->ofproto_class
->set_mcast_snooping_port
750 ? ofproto
->ofproto_class
->set_mcast_snooping_port(ofproto
, aux
, s
)
755 ofproto_set_cpu_mask(const char *cmask
)
758 pmd_cpu_mask
= nullable_xstrdup(cmask
);
762 ofproto_set_threads(int n_handlers_
, int n_revalidators_
)
764 int threads
= MAX(count_cpu_cores(), 2);
766 n_revalidators
= MAX(n_revalidators_
, 0);
767 n_handlers
= MAX(n_handlers_
, 0);
769 if (!n_revalidators
) {
770 n_revalidators
= n_handlers
771 ? MAX(threads
- (int) n_handlers
, 1)
776 n_handlers
= MAX(threads
- (int) n_revalidators
, 1);
781 ofproto_set_dp_desc(struct ofproto
*p
, const char *dp_desc
)
784 p
->dp_desc
= nullable_xstrdup(dp_desc
);
788 ofproto_set_snoops(struct ofproto
*ofproto
, const struct sset
*snoops
)
790 return connmgr_set_snoops(ofproto
->connmgr
, snoops
);
794 ofproto_set_netflow(struct ofproto
*ofproto
,
795 const struct netflow_options
*nf_options
)
797 if (nf_options
&& sset_is_empty(&nf_options
->collectors
)) {
801 if (ofproto
->ofproto_class
->set_netflow
) {
802 return ofproto
->ofproto_class
->set_netflow(ofproto
, nf_options
);
804 return nf_options
? EOPNOTSUPP
: 0;
809 ofproto_set_sflow(struct ofproto
*ofproto
,
810 const struct ofproto_sflow_options
*oso
)
812 if (oso
&& sset_is_empty(&oso
->targets
)) {
816 if (ofproto
->ofproto_class
->set_sflow
) {
817 return ofproto
->ofproto_class
->set_sflow(ofproto
, oso
);
819 return oso
? EOPNOTSUPP
: 0;
824 ofproto_set_ipfix(struct ofproto
*ofproto
,
825 const struct ofproto_ipfix_bridge_exporter_options
*bo
,
826 const struct ofproto_ipfix_flow_exporter_options
*fo
,
829 if (ofproto
->ofproto_class
->set_ipfix
) {
830 return ofproto
->ofproto_class
->set_ipfix(ofproto
, bo
, fo
, n_fo
);
832 return (bo
|| fo
) ? EOPNOTSUPP
: 0;
837 ofproto_get_ipfix_stats(struct ofproto
*ofproto
,
839 struct ovs_list
*replies
)
843 if (ofproto
->ofproto_class
->get_ipfix_stats
) {
844 error
= ofproto
->ofproto_class
->get_ipfix_stats(ofproto
,
855 handle_ipfix_bridge_stats_request(struct ofconn
*ofconn
,
856 const struct ofp_header
*request
)
858 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
859 struct ovs_list replies
;
862 ofpmp_init(&replies
, request
);
863 error
= ofproto_get_ipfix_stats(ofproto
, true, &replies
);
866 ofconn_send_replies(ofconn
, &replies
);
868 ofpbuf_list_delete(&replies
);
875 handle_ipfix_flow_stats_request(struct ofconn
*ofconn
,
876 const struct ofp_header
*request
)
878 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
879 struct ovs_list replies
;
882 ofpmp_init(&replies
, request
);
883 error
= ofproto_get_ipfix_stats(ofproto
, false, &replies
);
886 ofconn_send_replies(ofconn
, &replies
);
888 ofpbuf_list_delete(&replies
);
895 ofproto_set_flow_restore_wait(bool flow_restore_wait_db
)
897 flow_restore_wait
= flow_restore_wait_db
;
901 ofproto_get_flow_restore_wait(void)
903 return flow_restore_wait
;
907 /* Spanning Tree Protocol (STP) configuration. */
909 /* Configures STP on 'ofproto' using the settings defined in 's'. If
910 * 's' is NULL, disables STP.
912 * Returns 0 if successful, otherwise a positive errno value. */
914 ofproto_set_stp(struct ofproto
*ofproto
,
915 const struct ofproto_stp_settings
*s
)
917 return (ofproto
->ofproto_class
->set_stp
918 ? ofproto
->ofproto_class
->set_stp(ofproto
, s
)
922 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
923 * 'enabled' member of 's' is false, then the other members are not
926 * Returns 0 if successful, otherwise a positive errno value. */
928 ofproto_get_stp_status(struct ofproto
*ofproto
,
929 struct ofproto_stp_status
*s
)
931 return (ofproto
->ofproto_class
->get_stp_status
932 ? ofproto
->ofproto_class
->get_stp_status(ofproto
, s
)
936 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
937 * in 's'. The caller is responsible for assigning STP port numbers
938 * (using the 'port_num' member in the range of 1 through 255, inclusive)
939 * and ensuring there are no duplicates. If the 's' is NULL, then STP
940 * is disabled on the port.
942 * Returns 0 if successful, otherwise a positive errno value.*/
944 ofproto_port_set_stp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
945 const struct ofproto_port_stp_settings
*s
)
947 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
949 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16
,
950 ofproto
->name
, ofp_port
);
954 return (ofproto
->ofproto_class
->set_stp_port
955 ? ofproto
->ofproto_class
->set_stp_port(ofport
, s
)
959 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
960 * 's'. If the 'enabled' member in 's' is false, then the other members
961 * are not meaningful.
963 * Returns 0 if successful, otherwise a positive errno value.*/
965 ofproto_port_get_stp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
966 struct ofproto_port_stp_status
*s
)
968 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
970 VLOG_WARN_RL(&rl
, "%s: cannot get STP status on nonexistent "
971 "port %"PRIu16
, ofproto
->name
, ofp_port
);
975 return (ofproto
->ofproto_class
->get_stp_port_status
976 ? ofproto
->ofproto_class
->get_stp_port_status(ofport
, s
)
980 /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
981 * 's'. If the 'enabled' member in 's' is false, then the other members
982 * are not meaningful.
984 * Returns 0 if successful, otherwise a positive errno value.*/
986 ofproto_port_get_stp_stats(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
987 struct ofproto_port_stp_stats
*s
)
989 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
991 VLOG_WARN_RL(&rl
, "%s: cannot get STP stats on nonexistent "
992 "port %"PRIu16
, ofproto
->name
, ofp_port
);
996 return (ofproto
->ofproto_class
->get_stp_port_stats
997 ? ofproto
->ofproto_class
->get_stp_port_stats(ofport
, s
)
1001 /* Rapid Spanning Tree Protocol (RSTP) configuration. */
1003 /* Configures RSTP on 'ofproto' using the settings defined in 's'. If
1004 * 's' is NULL, disables RSTP.
1006 * Returns 0 if successful, otherwise a positive errno value. */
1008 ofproto_set_rstp(struct ofproto
*ofproto
,
1009 const struct ofproto_rstp_settings
*s
)
1011 if (!ofproto
->ofproto_class
->set_rstp
) {
1014 ofproto
->ofproto_class
->set_rstp(ofproto
, s
);
1018 /* Retrieves RSTP status of 'ofproto' and stores it in 's'. If the
1019 * 'enabled' member of 's' is false, then the other members are not
1022 * Returns 0 if successful, otherwise a positive errno value. */
1024 ofproto_get_rstp_status(struct ofproto
*ofproto
,
1025 struct ofproto_rstp_status
*s
)
1027 if (!ofproto
->ofproto_class
->get_rstp_status
) {
1030 ofproto
->ofproto_class
->get_rstp_status(ofproto
, s
);
1034 /* Configures RSTP on 'ofp_port' of 'ofproto' using the settings defined
1035 * in 's'. The caller is responsible for assigning RSTP port numbers
1036 * (using the 'port_num' member in the range of 1 through 255, inclusive)
1037 * and ensuring there are no duplicates. If the 's' is NULL, then RSTP
1038 * is disabled on the port.
1040 * Returns 0 if successful, otherwise a positive errno value.*/
1042 ofproto_port_set_rstp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1043 const struct ofproto_port_rstp_settings
*s
)
1045 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1047 VLOG_WARN("%s: cannot configure RSTP on nonexistent port %"PRIu16
,
1048 ofproto
->name
, ofp_port
);
1052 if (!ofproto
->ofproto_class
->set_rstp_port
) {
1055 ofproto
->ofproto_class
->set_rstp_port(ofport
, s
);
1059 /* Retrieves RSTP port status of 'ofp_port' on 'ofproto' and stores it in
1060 * 's'. If the 'enabled' member in 's' is false, then the other members
1061 * are not meaningful.
1063 * Returns 0 if successful, otherwise a positive errno value.*/
1065 ofproto_port_get_rstp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1066 struct ofproto_port_rstp_status
*s
)
1068 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1070 VLOG_WARN_RL(&rl
, "%s: cannot get RSTP status on nonexistent "
1071 "port %"PRIu16
, ofproto
->name
, ofp_port
);
1075 if (!ofproto
->ofproto_class
->get_rstp_port_status
) {
1078 ofproto
->ofproto_class
->get_rstp_port_status(ofport
, s
);
1082 /* Queue DSCP configuration. */
1084 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
1085 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
1086 * to implement QoS. Instead, it is used to implement features which require
1087 * knowledge of what queues exist on a port, and some basic information about
1090 * Returns 0 if successful, otherwise a positive errno value. */
1092 ofproto_port_set_queues(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1093 const struct ofproto_port_queue
*queues
,
1096 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1099 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16
,
1100 ofproto
->name
, ofp_port
);
1104 return (ofproto
->ofproto_class
->set_queues
1105 ? ofproto
->ofproto_class
->set_queues(ofport
, queues
, n_queues
)
1109 /* LLDP configuration. */
1111 ofproto_port_set_lldp(struct ofproto
*ofproto
,
1112 ofp_port_t ofp_port
,
1113 const struct smap
*cfg
)
1115 struct ofport
*ofport
;
1118 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1120 VLOG_WARN("%s: cannot configure LLDP on nonexistent port %"PRIu16
,
1121 ofproto
->name
, ofp_port
);
1124 error
= (ofproto
->ofproto_class
->set_lldp
1125 ? ofproto
->ofproto_class
->set_lldp(ofport
, cfg
)
1128 VLOG_WARN("%s: lldp configuration on port %"PRIu16
" (%s) failed (%s)",
1129 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1130 ovs_strerror(error
));
1135 ofproto_set_aa(struct ofproto
*ofproto
, void *aux OVS_UNUSED
,
1136 const struct aa_settings
*s
)
1138 if (!ofproto
->ofproto_class
->set_aa
) {
1141 ofproto
->ofproto_class
->set_aa(ofproto
, s
);
1146 ofproto_aa_mapping_register(struct ofproto
*ofproto
, void *aux
,
1147 const struct aa_mapping_settings
*s
)
1149 if (!ofproto
->ofproto_class
->aa_mapping_set
) {
1152 ofproto
->ofproto_class
->aa_mapping_set(ofproto
, aux
, s
);
1157 ofproto_aa_mapping_unregister(struct ofproto
*ofproto
, void *aux
)
1159 if (!ofproto
->ofproto_class
->aa_mapping_unset
) {
1162 ofproto
->ofproto_class
->aa_mapping_unset(ofproto
, aux
);
1167 ofproto_aa_vlan_get_queued(struct ofproto
*ofproto
,
1168 struct ovs_list
*list
)
1170 if (!ofproto
->ofproto_class
->aa_vlan_get_queued
) {
1173 ofproto
->ofproto_class
->aa_vlan_get_queued(ofproto
, list
);
1178 ofproto_aa_vlan_get_queue_size(struct ofproto
*ofproto
)
1180 if (!ofproto
->ofproto_class
->aa_vlan_get_queue_size
) {
1183 return ofproto
->ofproto_class
->aa_vlan_get_queue_size(ofproto
);
1186 /* Connectivity Fault Management configuration. */
1188 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
1190 ofproto_port_clear_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1192 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1193 if (ofport
&& ofproto
->ofproto_class
->set_cfm
) {
1194 ofproto
->ofproto_class
->set_cfm(ofport
, NULL
);
1198 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
1199 * basic configuration from the configuration members in 'cfm', and the remote
1200 * maintenance point ID from remote_mpid. Ignores the statistics members of
1203 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
1205 ofproto_port_set_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1206 const struct cfm_settings
*s
)
1208 struct ofport
*ofport
;
1211 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1213 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16
,
1214 ofproto
->name
, ofp_port
);
1218 /* XXX: For configuration simplicity, we only support one remote_mpid
1219 * outside of the CFM module. It's not clear if this is the correct long
1220 * term solution or not. */
1221 error
= (ofproto
->ofproto_class
->set_cfm
1222 ? ofproto
->ofproto_class
->set_cfm(ofport
, s
)
1225 VLOG_WARN("%s: CFM configuration on port %"PRIu16
" (%s) failed (%s)",
1226 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1227 ovs_strerror(error
));
1231 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
1232 * 'ofproto' does not have a port 'ofp_port'. */
1234 ofproto_port_set_bfd(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1235 const struct smap
*cfg
)
1237 struct ofport
*ofport
;
1240 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1242 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16
,
1243 ofproto
->name
, ofp_port
);
1247 error
= (ofproto
->ofproto_class
->set_bfd
1248 ? ofproto
->ofproto_class
->set_bfd(ofport
, cfg
)
1251 VLOG_WARN("%s: bfd configuration on port %"PRIu16
" (%s) failed (%s)",
1252 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1253 ovs_strerror(error
));
1257 /* Checks the status change of BFD on 'ofport'.
1259 * Returns true if 'ofproto_class' does not support 'bfd_status_changed'. */
1261 ofproto_port_bfd_status_changed(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1263 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1264 return (ofport
&& ofproto
->ofproto_class
->bfd_status_changed
1265 ? ofproto
->ofproto_class
->bfd_status_changed(ofport
)
1269 /* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on
1270 * success. Returns a positive errno otherwise. Has no effect if 'ofp_port'
1271 * is not an OpenFlow port in 'ofproto'.
1273 * The caller must provide and own '*status'. */
1275 ofproto_port_get_bfd_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1276 struct smap
*status
)
1278 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1279 return (ofport
&& ofproto
->ofproto_class
->get_bfd_status
1280 ? ofproto
->ofproto_class
->get_bfd_status(ofport
, status
)
1284 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1285 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1286 * 0 if LACP partner information is not current (generally indicating a
1287 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1289 ofproto_port_is_lacp_current(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1291 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1292 return (ofport
&& ofproto
->ofproto_class
->port_is_lacp_current
1293 ? ofproto
->ofproto_class
->port_is_lacp_current(ofport
)
1298 ofproto_port_get_lacp_stats(const struct ofport
*port
, struct lacp_slave_stats
*stats
)
1300 struct ofproto
*ofproto
= port
->ofproto
;
1303 if (ofproto
->ofproto_class
->port_get_lacp_stats
) {
1304 error
= ofproto
->ofproto_class
->port_get_lacp_stats(port
, stats
);
1314 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1315 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1316 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1317 * configuration plus, if there is more than one slave, a bonding
1320 * If 'aux' is already registered then this function updates its configuration
1321 * to 's'. Otherwise, this function registers a new bundle.
1323 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1326 ofproto_bundle_register(struct ofproto
*ofproto
, void *aux
,
1327 const struct ofproto_bundle_settings
*s
)
1329 return (ofproto
->ofproto_class
->bundle_set
1330 ? ofproto
->ofproto_class
->bundle_set(ofproto
, aux
, s
)
1334 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1335 * If no such bundle has been registered, this has no effect. */
1337 ofproto_bundle_unregister(struct ofproto
*ofproto
, void *aux
)
1339 return ofproto_bundle_register(ofproto
, aux
, NULL
);
1343 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1344 * If 'aux' is already registered then this function updates its configuration
1345 * to 's'. Otherwise, this function registers a new mirror. */
1347 ofproto_mirror_register(struct ofproto
*ofproto
, void *aux
,
1348 const struct ofproto_mirror_settings
*s
)
1350 return (ofproto
->ofproto_class
->mirror_set
1351 ? ofproto
->ofproto_class
->mirror_set(ofproto
, aux
, s
)
1355 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1356 * If no mirror has been registered, this has no effect. */
1358 ofproto_mirror_unregister(struct ofproto
*ofproto
, void *aux
)
1360 return ofproto_mirror_register(ofproto
, aux
, NULL
);
1363 /* Retrieves statistics from mirror associated with client data pointer
1364 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1365 * 'bytes', respectively. If a particular counters is not supported,
1366 * the appropriate argument is set to UINT64_MAX.
1369 ofproto_mirror_get_stats(struct ofproto
*ofproto
, void *aux
,
1370 uint64_t *packets
, uint64_t *bytes
)
1372 if (!ofproto
->ofproto_class
->mirror_get_stats
) {
1373 *packets
= *bytes
= UINT64_MAX
;
1377 return ofproto
->ofproto_class
->mirror_get_stats(ofproto
, aux
,
1381 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1382 * which all packets are flooded, instead of using MAC learning. If
1383 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1385 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1388 ofproto_set_flood_vlans(struct ofproto
*ofproto
, unsigned long *flood_vlans
)
1390 return (ofproto
->ofproto_class
->set_flood_vlans
1391 ? ofproto
->ofproto_class
->set_flood_vlans(ofproto
, flood_vlans
)
1395 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1396 * output for a mirror. */
1398 ofproto_is_mirror_output_bundle(const struct ofproto
*ofproto
, void *aux
)
1400 return (ofproto
->ofproto_class
->is_mirror_output_bundle
1401 ? ofproto
->ofproto_class
->is_mirror_output_bundle(ofproto
, aux
)
1405 /* Configuration of OpenFlow tables. */
1407 /* Returns the number of OpenFlow tables in 'ofproto'. */
1409 ofproto_get_n_tables(const struct ofproto
*ofproto
)
1411 return ofproto
->n_tables
;
1414 /* Returns the number of Controller visible OpenFlow tables
1415 * in 'ofproto'. This number will exclude Hidden tables.
1416 * This funtion's return value should be less or equal to that of
1417 * ofproto_get_n_tables() . */
1419 ofproto_get_n_visible_tables(const struct ofproto
*ofproto
)
1421 uint8_t n
= ofproto
->n_tables
;
1423 /* Count only non-hidden tables in the number of tables. (Hidden tables,
1424 * if present, are always at the end.) */
1425 while(n
&& (ofproto
->tables
[n
- 1].flags
& OFTABLE_HIDDEN
)) {
1432 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1433 * settings from 's'. 'table_id' must be in the range 0 through the number of
1434 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1436 * For read-only tables, only the name may be configured. */
1438 ofproto_configure_table(struct ofproto
*ofproto
, int table_id
,
1439 const struct ofproto_table_settings
*s
)
1441 struct oftable
*table
;
1443 ovs_assert(table_id
>= 0 && table_id
< ofproto
->n_tables
);
1444 table
= &ofproto
->tables
[table_id
];
1446 oftable_set_name(table
, s
->name
);
1448 if (table
->flags
& OFTABLE_READONLY
) {
1452 if (classifier_set_prefix_fields(&table
->cls
,
1453 s
->prefix_fields
, s
->n_prefix_fields
)) {
1454 /* XXX: Trigger revalidation. */
1457 ovs_mutex_lock(&ofproto_mutex
);
1458 unsigned int new_eviction
= (s
->enable_eviction
1459 ? table
->eviction
| EVICTION_CLIENT
1460 : table
->eviction
& ~EVICTION_CLIENT
);
1461 oftable_configure_eviction(table
, new_eviction
, s
->groups
, s
->n_groups
);
1462 table
->max_flows
= s
->max_flows
;
1463 evict_rules_from_table(table
);
1464 ovs_mutex_unlock(&ofproto_mutex
);
1468 ofproto_has_snoops(const struct ofproto
*ofproto
)
1470 return connmgr_has_snoops(ofproto
->connmgr
);
1474 ofproto_get_snoops(const struct ofproto
*ofproto
, struct sset
*snoops
)
1476 connmgr_get_snoops(ofproto
->connmgr
, snoops
);
1479 /* Deletes 'rule' from 'ofproto'.
1481 * Within an ofproto implementation, this function allows an ofproto
1482 * implementation to destroy any rules that remain when its ->destruct()
1483 * function is called. This function is not suitable for use elsewhere in an
1484 * ofproto implementation.
1486 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1487 * Cycle" in ofproto-provider.h. */
1489 ofproto_rule_delete(struct ofproto
*ofproto
, struct rule
*rule
)
1490 OVS_EXCLUDED(ofproto_mutex
)
1492 /* This skips the ofmonitor and flow-removed notifications because the
1493 * switch is being deleted and any OpenFlow channels have been or soon will
1495 ovs_mutex_lock(&ofproto_mutex
);
1497 if (!rule
->removed
) {
1498 /* Make sure there is no postponed removal of the rule. */
1499 ovs_assert(cls_rule_visible_in_version(&rule
->cr
, OVS_VERSION_MAX
));
1501 if (!classifier_remove(&rule
->ofproto
->tables
[rule
->table_id
].cls
,
1505 ofproto_rule_remove__(rule
->ofproto
, rule
);
1506 if (ofproto
->ofproto_class
->rule_delete
) {
1507 ofproto
->ofproto_class
->rule_delete(rule
);
1509 ofproto_rule_unref(rule
);
1511 ovs_mutex_unlock(&ofproto_mutex
);
1515 ofproto_flush__(struct ofproto
*ofproto
)
1516 OVS_EXCLUDED(ofproto_mutex
)
1518 struct oftable
*table
;
1520 /* This will flush all datapath flows. */
1521 if (ofproto
->ofproto_class
->flush
) {
1522 ofproto
->ofproto_class
->flush(ofproto
);
1525 /* XXX: There is a small race window here, where new datapath flows can be
1526 * created by upcall handlers based on the existing flow table. We can not
1527 * call ofproto class flush while holding 'ofproto_mutex' to prevent this,
1528 * as then we could deadlock on syncing with the handler threads waiting on
1529 * the same mutex. */
1531 ovs_mutex_lock(&ofproto_mutex
);
1532 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1533 struct rule_collection rules
;
1536 if (table
->flags
& OFTABLE_HIDDEN
) {
1540 rule_collection_init(&rules
);
1542 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
1543 rule_collection_add(&rules
, rule
);
1545 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
1547 /* XXX: Concurrent handler threads may insert new learned flows based on
1548 * learn actions of the now deleted flows right after we release
1549 * 'ofproto_mutex'. */
1550 ovs_mutex_unlock(&ofproto_mutex
);
1554 ofproto_destroy__(struct ofproto
*ofproto
)
1555 OVS_EXCLUDED(ofproto_mutex
)
1557 struct oftable
*table
;
1559 destroy_rule_executes(ofproto
);
1561 guarded_list_destroy(&ofproto
->rule_executes
);
1562 cmap_destroy(&ofproto
->groups
);
1564 hmap_remove(&all_ofprotos
, &ofproto
->hmap_node
);
1565 free(ofproto
->name
);
1566 free(ofproto
->type
);
1567 free(ofproto
->mfr_desc
);
1568 free(ofproto
->hw_desc
);
1569 free(ofproto
->sw_desc
);
1570 free(ofproto
->serial_desc
);
1571 free(ofproto
->dp_desc
);
1572 hmap_destroy(&ofproto
->ports
);
1573 hmap_destroy(&ofproto
->ofport_usage
);
1574 shash_destroy(&ofproto
->port_by_name
);
1575 simap_destroy(&ofproto
->ofp_requests
);
1577 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1578 oftable_destroy(table
);
1580 free(ofproto
->tables
);
1582 ovs_assert(hindex_is_empty(&ofproto
->cookies
));
1583 hindex_destroy(&ofproto
->cookies
);
1585 ovs_assert(hmap_is_empty(&ofproto
->learned_cookies
));
1586 hmap_destroy(&ofproto
->learned_cookies
);
1588 ofproto
->ofproto_class
->dealloc(ofproto
);
1591 /* Destroying rules is doubly deferred, must have 'ofproto' around for them.
1592 * - 1st we defer the removal of the rules from the classifier
1593 * - 2nd we defer the actual destruction of the rules. */
1595 ofproto_destroy_defer__(struct ofproto
*ofproto
)
1596 OVS_EXCLUDED(ofproto_mutex
)
1598 ovsrcu_postpone(ofproto_destroy__
, ofproto
);
1602 ofproto_destroy(struct ofproto
*p
, bool del
)
1603 OVS_EXCLUDED(ofproto_mutex
)
1605 struct ofport
*ofport
, *next_ofport
;
1606 struct ofport_usage
*usage
;
1613 meter_delete(p
, 1, p
->meter_features
.max_meters
);
1614 p
->meter_features
.max_meters
= 0;
1620 HMAP_FOR_EACH_SAFE (ofport
, next_ofport
, hmap_node
, &p
->ports
) {
1621 ofport_destroy(ofport
, del
);
1624 HMAP_FOR_EACH_POP (usage
, hmap_node
, &p
->ofport_usage
) {
1628 p
->ofproto_class
->destruct(p
);
1630 /* We should not postpone this because it involves deleting a listening
1631 * socket which we may want to reopen soon. 'connmgr' should not be used
1632 * by other threads */
1633 connmgr_destroy(p
->connmgr
);
1635 /* Destroying rules is deferred, must have 'ofproto' around for them. */
1636 ovsrcu_postpone(ofproto_destroy_defer__
, p
);
1639 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1640 * kernel datapath, for example, this destroys the datapath in the kernel, and
1641 * with the netdev-based datapath, it tears down the data structures that
1642 * represent the datapath.
1644 * The datapath should not be currently open as an ofproto. */
1646 ofproto_delete(const char *name
, const char *type
)
1648 const struct ofproto_class
*class = ofproto_class_find__(type
);
1649 return (!class ? EAFNOSUPPORT
1650 : !class->del
? EACCES
1651 : class->del(type
, name
));
1655 process_port_change(struct ofproto
*ofproto
, int error
, char *devname
)
1657 if (error
== ENOBUFS
) {
1658 reinit_ports(ofproto
);
1659 } else if (!error
) {
1660 update_port(ofproto
, devname
);
1666 ofproto_type_run(const char *datapath_type
)
1668 const struct ofproto_class
*class;
1671 datapath_type
= ofproto_normalize_type(datapath_type
);
1672 class = ofproto_class_find__(datapath_type
);
1674 error
= class->type_run
? class->type_run(datapath_type
) : 0;
1675 if (error
&& error
!= EAGAIN
) {
1676 VLOG_ERR_RL(&rl
, "%s: type_run failed (%s)",
1677 datapath_type
, ovs_strerror(error
));
1683 ofproto_type_wait(const char *datapath_type
)
1685 const struct ofproto_class
*class;
1687 datapath_type
= ofproto_normalize_type(datapath_type
);
1688 class = ofproto_class_find__(datapath_type
);
1690 if (class->type_wait
) {
1691 class->type_wait(datapath_type
);
1696 ofproto_run(struct ofproto
*p
)
1701 error
= p
->ofproto_class
->run(p
);
1702 if (error
&& error
!= EAGAIN
) {
1703 VLOG_ERR_RL(&rl
, "%s: run failed (%s)", p
->name
, ovs_strerror(error
));
1706 run_rule_executes(p
);
1708 /* Restore the eviction group heap invariant occasionally. */
1709 if (p
->eviction_group_timer
< time_msec()) {
1712 p
->eviction_group_timer
= time_msec() + 1000;
1714 for (i
= 0; i
< p
->n_tables
; i
++) {
1715 struct oftable
*table
= &p
->tables
[i
];
1716 struct eviction_group
*evg
;
1719 if (!table
->eviction
) {
1723 if (table
->n_flows
> 100000) {
1724 static struct vlog_rate_limit count_rl
=
1725 VLOG_RATE_LIMIT_INIT(1, 1);
1726 VLOG_WARN_RL(&count_rl
, "Table %"PRIuSIZE
" has an excessive"
1727 " number of rules: %d", i
, table
->n_flows
);
1730 ovs_mutex_lock(&ofproto_mutex
);
1731 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
1732 if (rule
->idle_timeout
|| rule
->hard_timeout
) {
1733 if (!rule
->eviction_group
) {
1734 eviction_group_add_rule(rule
);
1736 heap_raw_change(&rule
->evg_node
,
1737 rule_eviction_priority(p
, rule
));
1742 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
1743 heap_rebuild(&evg
->rules
);
1745 ovs_mutex_unlock(&ofproto_mutex
);
1749 if (p
->ofproto_class
->port_poll
) {
1752 while ((error
= p
->ofproto_class
->port_poll(p
, &devname
)) != EAGAIN
) {
1753 process_port_change(p
, error
, devname
);
1757 new_seq
= seq_read(connectivity_seq_get());
1758 if (new_seq
!= p
->change_seq
) {
1759 struct sset devnames
;
1760 const char *devname
;
1761 struct ofport
*ofport
;
1763 /* Update OpenFlow port status for any port whose netdev has changed.
1765 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1766 * destroyed, so it's not safe to update ports directly from the
1767 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1768 * need this two-phase approach. */
1769 sset_init(&devnames
);
1770 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1771 uint64_t port_change_seq
;
1773 port_change_seq
= netdev_get_change_seq(ofport
->netdev
);
1774 if (ofport
->change_seq
!= port_change_seq
) {
1775 ofport
->change_seq
= port_change_seq
;
1776 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
1779 SSET_FOR_EACH (devname
, &devnames
) {
1780 update_port(p
, devname
);
1782 sset_destroy(&devnames
);
1784 p
->change_seq
= new_seq
;
1787 connmgr_run(p
->connmgr
, handle_openflow
);
1793 ofproto_wait(struct ofproto
*p
)
1795 p
->ofproto_class
->wait(p
);
1796 if (p
->ofproto_class
->port_poll_wait
) {
1797 p
->ofproto_class
->port_poll_wait(p
);
1799 seq_wait(connectivity_seq_get(), p
->change_seq
);
1800 connmgr_wait(p
->connmgr
);
1804 ofproto_is_alive(const struct ofproto
*p
)
1806 return connmgr_has_controllers(p
->connmgr
);
1809 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1810 * memory_report(). */
1812 ofproto_get_memory_usage(const struct ofproto
*ofproto
, struct simap
*usage
)
1814 const struct oftable
*table
;
1815 unsigned int n_rules
;
1817 simap_increase(usage
, "ports", hmap_count(&ofproto
->ports
));
1820 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1821 n_rules
+= table
->n_flows
;
1823 simap_increase(usage
, "rules", n_rules
);
1825 if (ofproto
->ofproto_class
->get_memory_usage
) {
1826 ofproto
->ofproto_class
->get_memory_usage(ofproto
, usage
);
1829 connmgr_get_memory_usage(ofproto
->connmgr
, usage
);
1833 ofproto_type_get_memory_usage(const char *datapath_type
, struct simap
*usage
)
1835 const struct ofproto_class
*class;
1837 datapath_type
= ofproto_normalize_type(datapath_type
);
1838 class = ofproto_class_find__(datapath_type
);
1840 if (class && class->type_get_memory_usage
) {
1841 class->type_get_memory_usage(datapath_type
, usage
);
1846 ofproto_get_ofproto_controller_info(const struct ofproto
*ofproto
,
1849 connmgr_get_controller_info(ofproto
->connmgr
, info
);
1853 ofproto_free_ofproto_controller_info(struct shash
*info
)
1855 connmgr_free_controller_info(info
);
1858 /* Makes a deep copy of 'old' into 'port'. */
1860 ofproto_port_clone(struct ofproto_port
*port
, const struct ofproto_port
*old
)
1862 port
->name
= xstrdup(old
->name
);
1863 port
->type
= xstrdup(old
->type
);
1864 port
->ofp_port
= old
->ofp_port
;
1867 /* Frees memory allocated to members of 'ofproto_port'.
1869 * Do not call this function on an ofproto_port obtained from
1870 * ofproto_port_dump_next(): that function retains ownership of the data in the
1873 ofproto_port_destroy(struct ofproto_port
*ofproto_port
)
1875 free(ofproto_port
->name
);
1876 free(ofproto_port
->type
);
1879 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1881 * This function provides no status indication. An error status for the entire
1882 * dump operation is provided when it is completed by calling
1883 * ofproto_port_dump_done().
1886 ofproto_port_dump_start(struct ofproto_port_dump
*dump
,
1887 const struct ofproto
*ofproto
)
1889 dump
->ofproto
= ofproto
;
1890 dump
->error
= ofproto
->ofproto_class
->port_dump_start(ofproto
,
1894 /* Attempts to retrieve another port from 'dump', which must have been created
1895 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1896 * 'port' and returns true. On failure, returns false.
1898 * Failure might indicate an actual error or merely that the last port has been
1899 * dumped. An error status for the entire dump operation is provided when it
1900 * is completed by calling ofproto_port_dump_done().
1902 * The ofproto owns the data stored in 'port'. It will remain valid until at
1903 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1904 * ofproto_port_dump_done(). */
1906 ofproto_port_dump_next(struct ofproto_port_dump
*dump
,
1907 struct ofproto_port
*port
)
1909 const struct ofproto
*ofproto
= dump
->ofproto
;
1915 dump
->error
= ofproto
->ofproto_class
->port_dump_next(ofproto
, dump
->state
,
1918 ofproto
->ofproto_class
->port_dump_done(ofproto
, dump
->state
);
1924 /* Completes port table dump operation 'dump', which must have been created
1925 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1926 * error-free, otherwise a positive errno value describing the problem. */
1928 ofproto_port_dump_done(struct ofproto_port_dump
*dump
)
1930 const struct ofproto
*ofproto
= dump
->ofproto
;
1932 dump
->error
= ofproto
->ofproto_class
->port_dump_done(ofproto
,
1935 return dump
->error
== EOF
? 0 : dump
->error
;
1938 /* Returns the type to pass to netdev_open() when a datapath of type
1939 * 'datapath_type' has a port of type 'port_type', for a few special
1940 * cases when a netdev type differs from a port type. For example, when
1941 * using the userspace datapath, a port of type "internal" needs to be
1944 * Returns either 'type' itself or a string literal, which must not be
1947 ofproto_port_open_type(const char *datapath_type
, const char *port_type
)
1949 const struct ofproto_class
*class;
1951 datapath_type
= ofproto_normalize_type(datapath_type
);
1952 class = ofproto_class_find__(datapath_type
);
1957 return (class->port_open_type
1958 ? class->port_open_type(datapath_type
, port_type
)
1962 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1963 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1964 * the port's OpenFlow port number.
1966 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1967 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1968 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1969 * 'ofp_portp' is non-null). */
1971 ofproto_port_add(struct ofproto
*ofproto
, struct netdev
*netdev
,
1972 ofp_port_t
*ofp_portp
)
1974 ofp_port_t ofp_port
= ofp_portp
? *ofp_portp
: OFPP_NONE
;
1977 error
= ofproto
->ofproto_class
->port_add(ofproto
, netdev
);
1979 const char *netdev_name
= netdev_get_name(netdev
);
1981 simap_put(&ofproto
->ofp_requests
, netdev_name
,
1982 ofp_to_u16(ofp_port
));
1983 error
= update_port(ofproto
, netdev_name
);
1986 *ofp_portp
= OFPP_NONE
;
1988 struct ofproto_port ofproto_port
;
1990 error
= ofproto_port_query_by_name(ofproto
,
1991 netdev_get_name(netdev
),
1994 *ofp_portp
= ofproto_port
.ofp_port
;
1995 ofproto_port_destroy(&ofproto_port
);
2002 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
2003 * initializes '*port' appropriately; on failure, returns a positive errno
2006 * The caller owns the data in 'ofproto_port' and must free it with
2007 * ofproto_port_destroy() when it is no longer needed. */
2009 ofproto_port_query_by_name(const struct ofproto
*ofproto
, const char *devname
,
2010 struct ofproto_port
*port
)
2014 error
= ofproto
->ofproto_class
->port_query_by_name(ofproto
, devname
, port
);
2016 memset(port
, 0, sizeof *port
);
2021 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
2022 * Returns 0 if successful, otherwise a positive errno. */
2024 ofproto_port_del(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2026 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
2027 const char *name
= ofport
? netdev_get_name(ofport
->netdev
) : "<unknown>";
2028 struct simap_node
*ofp_request_node
;
2031 ofp_request_node
= simap_find(&ofproto
->ofp_requests
, name
);
2032 if (ofp_request_node
) {
2033 simap_delete(&ofproto
->ofp_requests
, ofp_request_node
);
2036 error
= ofproto
->ofproto_class
->port_del(ofproto
, ofp_port
);
2037 if (!error
&& ofport
) {
2038 /* 'name' is the netdev's name and update_port() is going to close the
2039 * netdev. Just in case update_port() refers to 'name' after it
2040 * destroys 'ofport', make a copy of it around the update_port()
2042 char *devname
= xstrdup(name
);
2043 update_port(ofproto
, devname
);
2049 /* Refreshes datapath configuration of port number 'ofp_port' in 'ofproto'.
2051 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
2053 ofproto_port_set_config(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
2054 const struct smap
*cfg
)
2056 struct ofport
*ofport
;
2059 ofport
= ofproto_get_port(ofproto
, ofp_port
);
2061 VLOG_WARN("%s: cannot configure datapath on nonexistent port %"PRIu16
,
2062 ofproto
->name
, ofp_port
);
2066 error
= (ofproto
->ofproto_class
->port_set_config
2067 ? ofproto
->ofproto_class
->port_set_config(ofport
, cfg
)
2070 VLOG_WARN("%s: datapath configuration on port %"PRIu16
2071 " (%s) failed (%s)",
2072 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
2073 ovs_strerror(error
));
2079 flow_mod_init(struct ofputil_flow_mod
*fm
,
2080 const struct match
*match
, int priority
,
2081 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2082 enum ofp_flow_mod_command command
)
2084 *fm
= (struct ofputil_flow_mod
) {
2086 .priority
= priority
,
2089 .buffer_id
= UINT32_MAX
,
2090 .out_port
= OFPP_ANY
,
2091 .out_group
= OFPG_ANY
,
2092 .ofpacts
= CONST_CAST(struct ofpact
*, ofpacts
),
2093 .ofpacts_len
= ofpacts_len
,
2098 simple_flow_mod(struct ofproto
*ofproto
,
2099 const struct match
*match
, int priority
,
2100 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2101 enum ofp_flow_mod_command command
)
2103 struct ofputil_flow_mod fm
;
2105 flow_mod_init(&fm
, match
, priority
, ofpacts
, ofpacts_len
, command
);
2107 return handle_flow_mod__(ofproto
, &fm
, NULL
);
2110 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
2111 * performs the 'n_actions' actions in 'actions'. The new flow will not
2114 * If cls_rule->priority is in the range of priorities supported by OpenFlow
2115 * (0...65535, inclusive) then the flow will be visible to OpenFlow
2116 * controllers; otherwise, it will be hidden.
2118 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
2120 * This is a helper function for in-band control and fail-open. */
2122 ofproto_add_flow(struct ofproto
*ofproto
, const struct match
*match
,
2124 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
2125 OVS_EXCLUDED(ofproto_mutex
)
2127 const struct rule
*rule
;
2130 /* First do a cheap check whether the rule we're looking for already exists
2131 * with the actions that we want. If it does, then we're done. */
2132 rule
= rule_from_cls_rule(classifier_find_match_exactly(
2133 &ofproto
->tables
[0].cls
, match
, priority
,
2136 const struct rule_actions
*actions
= rule_get_actions(rule
);
2137 must_add
= !ofpacts_equal(actions
->ofpacts
, actions
->ofpacts_len
,
2138 ofpacts
, ofpacts_len
);
2143 /* If there's no such rule or the rule doesn't have the actions we want,
2144 * fall back to a executing a full flow mod. We can't optimize this at
2145 * all because we didn't take enough locks above to ensure that the flow
2146 * table didn't already change beneath us. */
2148 simple_flow_mod(ofproto
, match
, priority
, ofpacts
, ofpacts_len
,
2149 OFPFC_MODIFY_STRICT
);
2153 /* Executes the flow modification specified in 'fm'. Returns 0 on success, or
2154 * an OFPERR_* OpenFlow error code on failure.
2156 * This is a helper function for in-band control and fail-open and the "learn"
2159 ofproto_flow_mod(struct ofproto
*ofproto
, const struct ofputil_flow_mod
*fm
)
2160 OVS_EXCLUDED(ofproto_mutex
)
2162 /* Optimize for the most common case of a repeated learn action.
2163 * If an identical flow already exists we only need to update its
2164 * 'modified' time. */
2165 if (fm
->command
== OFPFC_MODIFY_STRICT
&& fm
->table_id
!= OFPTT_ALL
2166 && !(fm
->flags
& OFPUTIL_FF_RESET_COUNTS
)) {
2167 struct oftable
*table
= &ofproto
->tables
[fm
->table_id
];
2171 rule
= rule_from_cls_rule(classifier_find_match_exactly(
2172 &table
->cls
, &fm
->match
, fm
->priority
,
2175 /* Reading many of the rule fields and writing on 'modified'
2176 * requires the rule->mutex. */
2177 const struct rule_actions
*actions
;
2179 ovs_mutex_lock(&rule
->mutex
);
2180 actions
= rule_get_actions(rule
);
2181 if (rule
->idle_timeout
== fm
->idle_timeout
2182 && rule
->hard_timeout
== fm
->hard_timeout
2183 && rule
->importance
== fm
->importance
2184 && rule
->flags
== (fm
->flags
& OFPUTIL_FF_STATE
)
2185 && (!fm
->modify_cookie
|| (fm
->new_cookie
== rule
->flow_cookie
))
2186 && ofpacts_equal(fm
->ofpacts
, fm
->ofpacts_len
,
2187 actions
->ofpacts
, actions
->ofpacts_len
)) {
2188 /* Rule already exists and need not change, only update the
2189 modified timestamp. */
2190 rule
->modified
= time_msec();
2193 ovs_mutex_unlock(&rule
->mutex
);
2201 return handle_flow_mod__(ofproto
, fm
, NULL
);
2204 /* Searches for a rule with matching criteria exactly equal to 'target' in
2205 * ofproto's table 0 and, if it finds one, deletes it.
2207 * This is a helper function for in-band control and fail-open. */
2209 ofproto_delete_flow(struct ofproto
*ofproto
,
2210 const struct match
*target
, int priority
)
2211 OVS_EXCLUDED(ofproto_mutex
)
2213 struct classifier
*cls
= &ofproto
->tables
[0].cls
;
2216 /* First do a cheap check whether the rule we're looking for has already
2217 * been deleted. If so, then we're done. */
2218 rule
= rule_from_cls_rule(classifier_find_match_exactly(
2219 cls
, target
, priority
, OVS_VERSION_MAX
));
2224 /* Execute a flow mod. We can't optimize this at all because we didn't
2225 * take enough locks above to ensure that the flow table didn't already
2226 * change beneath us. */
2227 simple_flow_mod(ofproto
, target
, priority
, NULL
, 0, OFPFC_DELETE_STRICT
);
2230 /* Delete all of the flows from all of ofproto's flow tables, then reintroduce
2231 * the flows required by in-band control and fail-open. */
2233 ofproto_flush_flows(struct ofproto
*ofproto
)
2235 COVERAGE_INC(ofproto_flush
);
2236 ofproto_flush__(ofproto
);
2237 connmgr_flushed(ofproto
->connmgr
);
2241 reinit_ports(struct ofproto
*p
)
2243 struct ofproto_port_dump dump
;
2244 struct sset devnames
;
2245 struct ofport
*ofport
;
2246 struct ofproto_port ofproto_port
;
2247 const char *devname
;
2249 COVERAGE_INC(ofproto_reinit_ports
);
2251 sset_init(&devnames
);
2252 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2253 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
2255 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2256 sset_add(&devnames
, ofproto_port
.name
);
2259 SSET_FOR_EACH (devname
, &devnames
) {
2260 update_port(p
, devname
);
2262 sset_destroy(&devnames
);
2266 alloc_ofp_port(struct ofproto
*ofproto
, const char *netdev_name
)
2270 port_idx
= simap_get(&ofproto
->ofp_requests
, netdev_name
);
2271 port_idx
= port_idx
? port_idx
: UINT16_MAX
;
2273 if (port_idx
>= ofproto
->max_ports
2274 || ofport_get_usage(ofproto
, u16_to_ofp(port_idx
)) == LLONG_MAX
) {
2275 uint16_t lru_ofport
= 0, end_port_no
= ofproto
->alloc_port_no
;
2276 long long int last_used_at
, lru
= LLONG_MAX
;
2278 /* Search for a free OpenFlow port number. We try not to
2279 * immediately reuse them to prevent problems due to old
2282 * We limit the automatically assigned port numbers to the lower half
2283 * of the port range, to reserve the upper half for assignment by
2286 if (++ofproto
->alloc_port_no
>= MIN(ofproto
->max_ports
, 32768)) {
2287 ofproto
->alloc_port_no
= 1;
2289 last_used_at
= ofport_get_usage(ofproto
,
2290 u16_to_ofp(ofproto
->alloc_port_no
));
2291 if (!last_used_at
) {
2292 port_idx
= ofproto
->alloc_port_no
;
2294 } else if ( last_used_at
< time_msec() - 60*60*1000) {
2295 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2296 * more than an hour ago, consider it usable. */
2297 ofport_remove_usage(ofproto
,
2298 u16_to_ofp(ofproto
->alloc_port_no
));
2299 port_idx
= ofproto
->alloc_port_no
;
2301 } else if (last_used_at
< lru
) {
2303 lru_ofport
= ofproto
->alloc_port_no
;
2306 if (ofproto
->alloc_port_no
== end_port_no
) {
2308 port_idx
= lru_ofport
;
2315 ofport_set_usage(ofproto
, u16_to_ofp(port_idx
), LLONG_MAX
);
2316 return u16_to_ofp(port_idx
);
2320 dealloc_ofp_port(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2322 if (ofp_to_u16(ofp_port
) < ofproto
->max_ports
) {
2323 ofport_set_usage(ofproto
, ofp_port
, time_msec());
2327 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2328 * pointer if the netdev cannot be opened. On success, also fills in
2330 static struct netdev
*
2331 ofport_open(struct ofproto
*ofproto
,
2332 struct ofproto_port
*ofproto_port
,
2333 struct ofputil_phy_port
*pp
)
2335 enum netdev_flags flags
;
2336 struct netdev
*netdev
;
2339 error
= netdev_open(ofproto_port
->name
, ofproto_port
->type
, &netdev
);
2341 VLOG_WARN_RL(&rl
, "%s: ignoring port %s (%"PRIu16
") because netdev %s "
2342 "cannot be opened (%s)",
2344 ofproto_port
->name
, ofproto_port
->ofp_port
,
2345 ofproto_port
->name
, ovs_strerror(error
));
2349 if (ofproto_port
->ofp_port
== OFPP_NONE
) {
2350 if (!strcmp(ofproto
->name
, ofproto_port
->name
)) {
2351 ofproto_port
->ofp_port
= OFPP_LOCAL
;
2353 ofproto_port
->ofp_port
= alloc_ofp_port(ofproto
,
2354 ofproto_port
->name
);
2357 pp
->port_no
= ofproto_port
->ofp_port
;
2358 netdev_get_etheraddr(netdev
, &pp
->hw_addr
);
2359 ovs_strlcpy(pp
->name
, ofproto_port
->name
, sizeof pp
->name
);
2360 netdev_get_flags(netdev
, &flags
);
2361 pp
->config
= flags
& NETDEV_UP
? 0 : OFPUTIL_PC_PORT_DOWN
;
2362 pp
->state
= netdev_get_carrier(netdev
) ? 0 : OFPUTIL_PS_LINK_DOWN
;
2363 netdev_get_features(netdev
, &pp
->curr
, &pp
->advertised
,
2364 &pp
->supported
, &pp
->peer
);
2365 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
2366 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
2371 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2372 * port number, and 'config' bits other than OFPUTIL_PC_PORT_DOWN are
2375 ofport_equal(const struct ofputil_phy_port
*a
,
2376 const struct ofputil_phy_port
*b
)
2378 return (eth_addr_equals(a
->hw_addr
, b
->hw_addr
)
2379 && a
->state
== b
->state
2380 && !((a
->config
^ b
->config
) & OFPUTIL_PC_PORT_DOWN
)
2381 && a
->curr
== b
->curr
2382 && a
->advertised
== b
->advertised
2383 && a
->supported
== b
->supported
2384 && a
->peer
== b
->peer
2385 && a
->curr_speed
== b
->curr_speed
2386 && a
->max_speed
== b
->max_speed
);
2389 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2390 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2391 * one with the same name or port number). */
2393 ofport_install(struct ofproto
*p
,
2394 struct netdev
*netdev
, const struct ofputil_phy_port
*pp
)
2396 const char *netdev_name
= netdev_get_name(netdev
);
2397 struct ofport
*ofport
;
2400 /* Create ofport. */
2401 ofport
= p
->ofproto_class
->port_alloc();
2406 ofport
->ofproto
= p
;
2407 ofport
->netdev
= netdev
;
2408 ofport
->change_seq
= netdev_get_change_seq(netdev
);
2410 ofport
->ofp_port
= pp
->port_no
;
2411 ofport
->created
= time_msec();
2413 /* Add port to 'p'. */
2414 hmap_insert(&p
->ports
, &ofport
->hmap_node
,
2415 hash_ofp_port(ofport
->ofp_port
));
2416 shash_add(&p
->port_by_name
, netdev_name
, ofport
);
2418 update_mtu(p
, ofport
);
2420 /* Let the ofproto_class initialize its private data. */
2421 error
= p
->ofproto_class
->port_construct(ofport
);
2425 connmgr_send_port_status(p
->connmgr
, NULL
, pp
, OFPPR_ADD
);
2429 VLOG_WARN_RL(&rl
, "%s: could not add port %s (%s)",
2430 p
->name
, netdev_name
, ovs_strerror(error
));
2432 ofport_destroy__(ofport
);
2434 netdev_close(netdev
);
2439 /* Removes 'ofport' from 'p' and destroys it. */
2441 ofport_remove(struct ofport
*ofport
)
2443 struct ofproto
*p
= ofport
->ofproto
;
2444 bool is_internal
= ofport_is_internal(p
, ofport
);
2446 connmgr_send_port_status(ofport
->ofproto
->connmgr
, NULL
, &ofport
->pp
,
2448 ofport_destroy(ofport
, true);
2450 update_mtu_ofproto(p
);
2454 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2457 ofport_remove_with_name(struct ofproto
*ofproto
, const char *name
)
2459 struct ofport
*port
= shash_find_data(&ofproto
->port_by_name
, name
);
2461 ofport_remove(port
);
2465 /* Updates 'port' with new 'pp' description.
2467 * Does not handle a name or port number change. The caller must implement
2468 * such a change as a delete followed by an add. */
2470 ofport_modified(struct ofport
*port
, struct ofputil_phy_port
*pp
)
2472 port
->pp
.hw_addr
= pp
->hw_addr
;
2473 port
->pp
.config
= ((port
->pp
.config
& ~OFPUTIL_PC_PORT_DOWN
)
2474 | (pp
->config
& OFPUTIL_PC_PORT_DOWN
));
2475 port
->pp
.state
= ((port
->pp
.state
& ~OFPUTIL_PS_LINK_DOWN
)
2476 | (pp
->state
& OFPUTIL_PS_LINK_DOWN
));
2477 port
->pp
.curr
= pp
->curr
;
2478 port
->pp
.advertised
= pp
->advertised
;
2479 port
->pp
.supported
= pp
->supported
;
2480 port
->pp
.peer
= pp
->peer
;
2481 port
->pp
.curr_speed
= pp
->curr_speed
;
2482 port
->pp
.max_speed
= pp
->max_speed
;
2484 connmgr_send_port_status(port
->ofproto
->connmgr
, NULL
,
2485 &port
->pp
, OFPPR_MODIFY
);
2488 /* Update OpenFlow 'state' in 'port' and notify controller. */
2490 ofproto_port_set_state(struct ofport
*port
, enum ofputil_port_state state
)
2492 if (port
->pp
.state
!= state
) {
2493 port
->pp
.state
= state
;
2494 connmgr_send_port_status(port
->ofproto
->connmgr
, NULL
,
2495 &port
->pp
, OFPPR_MODIFY
);
2500 ofproto_port_unregister(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2502 struct ofport
*port
= ofproto_get_port(ofproto
, ofp_port
);
2504 if (port
->ofproto
->ofproto_class
->set_stp_port
) {
2505 port
->ofproto
->ofproto_class
->set_stp_port(port
, NULL
);
2507 if (port
->ofproto
->ofproto_class
->set_rstp_port
) {
2508 port
->ofproto
->ofproto_class
->set_rstp_port(port
, NULL
);
2510 if (port
->ofproto
->ofproto_class
->set_cfm
) {
2511 port
->ofproto
->ofproto_class
->set_cfm(port
, NULL
);
2513 if (port
->ofproto
->ofproto_class
->bundle_remove
) {
2514 port
->ofproto
->ofproto_class
->bundle_remove(port
);
2520 ofport_destroy__(struct ofport
*port
)
2522 struct ofproto
*ofproto
= port
->ofproto
;
2523 const char *name
= netdev_get_name(port
->netdev
);
2525 hmap_remove(&ofproto
->ports
, &port
->hmap_node
);
2526 shash_delete(&ofproto
->port_by_name
,
2527 shash_find(&ofproto
->port_by_name
, name
));
2529 netdev_close(port
->netdev
);
2530 ofproto
->ofproto_class
->port_dealloc(port
);
2534 ofport_destroy(struct ofport
*port
, bool del
)
2537 dealloc_ofp_port(port
->ofproto
, port
->ofp_port
);
2538 port
->ofproto
->ofproto_class
->port_destruct(port
, del
);
2539 ofport_destroy__(port
);
2544 ofproto_get_port(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2546 struct ofport
*port
;
2548 HMAP_FOR_EACH_IN_BUCKET (port
, hmap_node
, hash_ofp_port(ofp_port
),
2550 if (port
->ofp_port
== ofp_port
) {
2557 static long long int
2558 ofport_get_usage(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2560 struct ofport_usage
*usage
;
2562 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2563 &ofproto
->ofport_usage
) {
2564 if (usage
->ofp_port
== ofp_port
) {
2565 return usage
->last_used
;
2572 ofport_set_usage(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
2573 long long int last_used
)
2575 struct ofport_usage
*usage
;
2576 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2577 &ofproto
->ofport_usage
) {
2578 if (usage
->ofp_port
== ofp_port
) {
2579 usage
->last_used
= last_used
;
2583 ovs_assert(last_used
== LLONG_MAX
);
2585 usage
= xmalloc(sizeof *usage
);
2586 usage
->ofp_port
= ofp_port
;
2587 usage
->last_used
= last_used
;
2588 hmap_insert(&ofproto
->ofport_usage
, &usage
->hmap_node
,
2589 hash_ofp_port(ofp_port
));
2593 ofport_remove_usage(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2595 struct ofport_usage
*usage
;
2596 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2597 &ofproto
->ofport_usage
) {
2598 if (usage
->ofp_port
== ofp_port
) {
2599 hmap_remove(&ofproto
->ofport_usage
, &usage
->hmap_node
);
2607 ofproto_port_get_stats(const struct ofport
*port
, struct netdev_stats
*stats
)
2609 struct ofproto
*ofproto
= port
->ofproto
;
2612 if (ofproto
->ofproto_class
->port_get_stats
) {
2613 error
= ofproto
->ofproto_class
->port_get_stats(port
, stats
);
2622 update_port(struct ofproto
*ofproto
, const char *name
)
2624 struct ofproto_port ofproto_port
;
2625 struct ofputil_phy_port pp
;
2626 struct netdev
*netdev
;
2627 struct ofport
*port
;
2630 COVERAGE_INC(ofproto_update_port
);
2632 /* Fetch 'name''s location and properties from the datapath. */
2633 netdev
= (!ofproto_port_query_by_name(ofproto
, name
, &ofproto_port
)
2634 ? ofport_open(ofproto
, &ofproto_port
, &pp
)
2638 port
= ofproto_get_port(ofproto
, ofproto_port
.ofp_port
);
2639 if (port
&& !strcmp(netdev_get_name(port
->netdev
), name
)) {
2640 struct netdev
*old_netdev
= port
->netdev
;
2642 /* 'name' hasn't changed location. Any properties changed? */
2643 if (!ofport_equal(&port
->pp
, &pp
)) {
2644 ofport_modified(port
, &pp
);
2647 update_mtu(ofproto
, port
);
2649 /* Install the newly opened netdev in case it has changed.
2650 * Don't close the old netdev yet in case port_modified has to
2651 * remove a retained reference to it.*/
2652 port
->netdev
= netdev
;
2653 port
->change_seq
= netdev_get_change_seq(netdev
);
2655 if (port
->ofproto
->ofproto_class
->port_modified
) {
2656 port
->ofproto
->ofproto_class
->port_modified(port
);
2659 netdev_close(old_netdev
);
2661 /* If 'port' is nonnull then its name differs from 'name' and thus
2662 * we should delete it. If we think there's a port named 'name'
2663 * then its port number must be wrong now so delete it too. */
2665 ofport_remove(port
);
2667 ofport_remove_with_name(ofproto
, name
);
2668 error
= ofport_install(ofproto
, netdev
, &pp
);
2671 /* Any port named 'name' is gone now. */
2672 ofport_remove_with_name(ofproto
, name
);
2674 ofproto_port_destroy(&ofproto_port
);
2680 init_ports(struct ofproto
*p
)
2682 struct ofproto_port_dump dump
;
2683 struct ofproto_port ofproto_port
;
2684 struct shash_node
*node
, *next
;
2686 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2687 const char *name
= ofproto_port
.name
;
2689 if (shash_find(&p
->port_by_name
, name
)) {
2690 VLOG_WARN_RL(&rl
, "%s: ignoring duplicate device %s in datapath",
2693 struct ofputil_phy_port pp
;
2694 struct netdev
*netdev
;
2696 /* Check if an OpenFlow port number had been requested. */
2697 node
= shash_find(&init_ofp_ports
, name
);
2699 const struct iface_hint
*iface_hint
= node
->data
;
2700 simap_put(&p
->ofp_requests
, name
,
2701 ofp_to_u16(iface_hint
->ofp_port
));
2704 netdev
= ofport_open(p
, &ofproto_port
, &pp
);
2706 ofport_install(p
, netdev
, &pp
);
2707 if (ofp_to_u16(ofproto_port
.ofp_port
) < p
->max_ports
) {
2708 p
->alloc_port_no
= MAX(p
->alloc_port_no
,
2709 ofp_to_u16(ofproto_port
.ofp_port
));
2715 SHASH_FOR_EACH_SAFE(node
, next
, &init_ofp_ports
) {
2716 struct iface_hint
*iface_hint
= node
->data
;
2718 if (!strcmp(iface_hint
->br_name
, p
->name
)) {
2719 free(iface_hint
->br_name
);
2720 free(iface_hint
->br_type
);
2722 shash_delete(&init_ofp_ports
, node
);
2730 ofport_is_internal(const struct ofproto
*p
, const struct ofport
*port
)
2732 return !strcmp(netdev_get_type(port
->netdev
),
2733 ofproto_port_open_type(p
->type
, "internal"));
2736 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2737 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2739 find_min_mtu(struct ofproto
*p
)
2741 struct ofport
*ofport
;
2744 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2745 struct netdev
*netdev
= ofport
->netdev
;
2748 /* Skip any internal ports, since that's what we're trying to
2750 if (ofport_is_internal(p
, ofport
)) {
2754 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2757 if (!mtu
|| dev_mtu
< mtu
) {
2762 return mtu
? mtu
: ETH_PAYLOAD_MAX
;
2765 /* Update MTU of all datapath devices on 'p' to the minimum of the
2766 * non-datapath ports in event of 'port' added or changed. */
2768 update_mtu(struct ofproto
*p
, struct ofport
*port
)
2770 struct netdev
*netdev
= port
->netdev
;
2773 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2777 if (ofport_is_internal(p
, port
)) {
2778 if (dev_mtu
> p
->min_mtu
) {
2779 if (!netdev_set_mtu(port
->netdev
, p
->min_mtu
)) {
2780 dev_mtu
= p
->min_mtu
;
2783 port
->mtu
= dev_mtu
;
2787 port
->mtu
= dev_mtu
;
2788 /* For non-internal port find new min mtu. */
2789 update_mtu_ofproto(p
);
2793 update_mtu_ofproto(struct ofproto
*p
)
2795 /* For non-internal port find new min mtu. */
2796 struct ofport
*ofport
;
2797 int old_min
= p
->min_mtu
;
2799 p
->min_mtu
= find_min_mtu(p
);
2800 if (p
->min_mtu
== old_min
) {
2804 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2805 struct netdev
*netdev
= ofport
->netdev
;
2807 if (ofport_is_internal(p
, ofport
)) {
2808 if (!netdev_set_mtu(netdev
, p
->min_mtu
)) {
2809 ofport
->mtu
= p
->min_mtu
;
2816 ofproto_rule_destroy__(struct rule
*rule
)
2817 OVS_NO_THREAD_SAFETY_ANALYSIS
2819 cls_rule_destroy(CONST_CAST(struct cls_rule
*, &rule
->cr
));
2820 rule_actions_destroy(rule_get_actions(rule
));
2821 ovs_mutex_destroy(&rule
->mutex
);
2822 rule
->ofproto
->ofproto_class
->rule_dealloc(rule
);
2826 rule_destroy_cb(struct rule
*rule
)
2827 OVS_NO_THREAD_SAFETY_ANALYSIS
2829 /* Send rule removed if needed. */
2830 if (rule
->flags
& OFPUTIL_FF_SEND_FLOW_REM
2831 && rule
->removed_reason
!= OVS_OFPRR_NONE
2832 && !rule_is_hidden(rule
)) {
2833 ofproto_rule_send_removed(rule
);
2835 rule
->ofproto
->ofproto_class
->rule_destruct(rule
);
2836 ofproto_rule_destroy__(rule
);
2840 ofproto_rule_ref(struct rule
*rule
)
2843 ovs_refcount_ref(&rule
->ref_count
);
2848 ofproto_rule_try_ref(struct rule
*rule
)
2851 return ovs_refcount_try_ref_rcu(&rule
->ref_count
);
2856 /* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the
2857 * ref_count reaches 0.
2859 * Use of RCU allows short term use (between RCU quiescent periods) without
2860 * keeping a reference. A reference must be taken if the rule needs to
2861 * stay around accross the RCU quiescent periods. */
2863 ofproto_rule_unref(struct rule
*rule
)
2865 if (rule
&& ovs_refcount_unref_relaxed(&rule
->ref_count
) == 1) {
2866 ovsrcu_postpone(rule_destroy_cb
, rule
);
2871 remove_rule_rcu__(struct rule
*rule
)
2872 OVS_REQUIRES(ofproto_mutex
)
2874 struct ofproto
*ofproto
= rule
->ofproto
;
2875 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
2877 ovs_assert(!cls_rule_visible_in_version(&rule
->cr
, OVS_VERSION_MAX
));
2878 if (!classifier_remove(&table
->cls
, &rule
->cr
)) {
2881 if (ofproto
->ofproto_class
->rule_delete
) {
2882 ofproto
->ofproto_class
->rule_delete(rule
);
2884 ofproto_rule_unref(rule
);
2888 remove_rule_rcu(struct rule
*rule
)
2889 OVS_EXCLUDED(ofproto_mutex
)
2891 ovs_mutex_lock(&ofproto_mutex
);
2892 remove_rule_rcu__(rule
);
2893 ovs_mutex_unlock(&ofproto_mutex
);
2896 /* Removes and deletes rules from a NULL-terminated array of rule pointers. */
2898 remove_rules_rcu(struct rule
**rules
)
2899 OVS_EXCLUDED(ofproto_mutex
)
2901 struct rule
**orig_rules
= rules
;
2904 struct ofproto
*ofproto
= rules
[0]->ofproto
;
2905 unsigned long tables
[BITMAP_N_LONGS(256)];
2909 memset(tables
, 0, sizeof tables
);
2911 ovs_mutex_lock(&ofproto_mutex
);
2912 while ((rule
= *rules
++)) {
2913 /* Defer once for each new table. This defers the subtable cleanup
2914 * until later, so that when removing large number of flows the
2915 * operation is faster. */
2916 if (!bitmap_is_set(tables
, rule
->table_id
)) {
2917 struct classifier
*cls
= &ofproto
->tables
[rule
->table_id
].cls
;
2919 bitmap_set1(tables
, rule
->table_id
);
2920 classifier_defer(cls
);
2922 remove_rule_rcu__(rule
);
2925 BITMAP_FOR_EACH_1(table_id
, 256, tables
) {
2926 struct classifier
*cls
= &ofproto
->tables
[table_id
].cls
;
2928 classifier_publish(cls
);
2930 ovs_mutex_unlock(&ofproto_mutex
);
2937 ofproto_group_ref(struct ofgroup
*group
)
2940 ovs_refcount_ref(&group
->ref_count
);
2945 ofproto_group_try_ref(struct ofgroup
*group
)
2948 return ovs_refcount_try_ref_rcu(&group
->ref_count
);
2954 group_destroy_cb(struct ofgroup
*group
)
2956 group
->ofproto
->ofproto_class
->group_destruct(group
);
2957 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props
*,
2959 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list
*,
2961 group
->ofproto
->ofproto_class
->group_dealloc(group
);
2965 ofproto_group_unref(struct ofgroup
*group
)
2966 OVS_NO_THREAD_SAFETY_ANALYSIS
2968 if (group
&& ovs_refcount_unref_relaxed(&group
->ref_count
) == 1) {
2969 ovs_assert(rule_collection_n(&group
->rules
) == 0);
2970 ovsrcu_postpone(group_destroy_cb
, group
);
2975 remove_group_rcu__(struct ofgroup
*group
)
2976 OVS_REQUIRES(ofproto_mutex
)
2978 struct ofproto
*ofproto
= group
->ofproto
;
2980 ovs_assert(!versions_visible_in_version(&group
->versions
, OVS_VERSION_MAX
));
2982 cmap_remove(&ofproto
->groups
, &group
->cmap_node
,
2983 hash_int(group
->group_id
, 0));
2984 ofproto_group_unref(group
);
2988 remove_group_rcu(struct ofgroup
*group
)
2989 OVS_EXCLUDED(ofproto_mutex
)
2991 ovs_mutex_lock(&ofproto_mutex
);
2992 remove_group_rcu__(group
);
2993 ovs_mutex_unlock(&ofproto_mutex
);
2996 /* Removes and deletes groups from a NULL-terminated array of group
2999 remove_groups_rcu(struct ofgroup
**groups
)
3000 OVS_EXCLUDED(ofproto_mutex
)
3002 ovs_mutex_lock(&ofproto_mutex
);
3003 for (struct ofgroup
**g
= groups
; *g
; g
++) {
3004 remove_group_rcu__(*g
);
3006 ovs_mutex_unlock(&ofproto_mutex
);
3010 static uint32_t get_provider_meter_id(const struct ofproto
*,
3011 uint32_t of_meter_id
);
3013 /* Creates and returns a new 'struct rule_actions', whose actions are a copy
3014 * of from the 'ofpacts_len' bytes of 'ofpacts'. */
3015 const struct rule_actions
*
3016 rule_actions_create(const struct ofpact
*ofpacts
, size_t ofpacts_len
)
3018 struct rule_actions
*actions
;
3020 actions
= xmalloc(sizeof *actions
+ ofpacts_len
);
3021 actions
->ofpacts_len
= ofpacts_len
;
3022 memcpy(actions
->ofpacts
, ofpacts
, ofpacts_len
);
3023 actions
->has_meter
= ofpacts_get_meter(ofpacts
, ofpacts_len
) != 0;
3024 actions
->has_groups
=
3025 (ofpact_find_type_flattened(ofpacts
, OFPACT_GROUP
,
3026 ofpact_end(ofpacts
, ofpacts_len
))
3028 actions
->has_learn_with_delete
= (next_learn_with_delete(actions
, NULL
)
3034 /* Free the actions after the RCU quiescent period is reached. */
3036 rule_actions_destroy(const struct rule_actions
*actions
)
3039 ovsrcu_postpone(free
, CONST_CAST(struct rule_actions
*, actions
));
3043 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
3044 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
3046 ofproto_rule_has_out_port(const struct rule
*rule
, ofp_port_t port
)
3047 OVS_REQUIRES(ofproto_mutex
)
3049 if (port
== OFPP_ANY
) {
3052 const struct rule_actions
*actions
= rule_get_actions(rule
);
3053 return ofpacts_output_to_port(actions
->ofpacts
,
3054 actions
->ofpacts_len
, port
);
3058 /* Returns true if 'rule' has group and equals group_id. */
3060 ofproto_rule_has_out_group(const struct rule
*rule
, uint32_t group_id
)
3061 OVS_REQUIRES(ofproto_mutex
)
3063 if (group_id
== OFPG_ANY
) {
3066 const struct rule_actions
*actions
= rule_get_actions(rule
);
3067 return ofpacts_output_to_group(actions
->ofpacts
,
3068 actions
->ofpacts_len
, group_id
);
3073 rule_execute_destroy(struct rule_execute
*e
)
3075 ofproto_rule_unref(e
->rule
);
3076 ovs_list_remove(&e
->list_node
);
3080 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
3081 * by passing them to the ofproto provider. */
3083 run_rule_executes(struct ofproto
*ofproto
)
3084 OVS_EXCLUDED(ofproto_mutex
)
3086 struct rule_execute
*e
, *next
;
3087 struct ovs_list executes
;
3089 guarded_list_pop_all(&ofproto
->rule_executes
, &executes
);
3090 LIST_FOR_EACH_SAFE (e
, next
, list_node
, &executes
) {
3093 flow_extract(e
->packet
, &flow
);
3094 flow
.in_port
.ofp_port
= e
->in_port
;
3095 ofproto
->ofproto_class
->rule_execute(e
->rule
, &flow
, e
->packet
);
3097 rule_execute_destroy(e
);
3101 /* Destroys and discards all "rule_execute" operations queued up in
3102 * ofproto->rule_executes. */
3104 destroy_rule_executes(struct ofproto
*ofproto
)
3106 struct rule_execute
*e
, *next
;
3107 struct ovs_list executes
;
3109 guarded_list_pop_all(&ofproto
->rule_executes
, &executes
);
3110 LIST_FOR_EACH_SAFE (e
, next
, list_node
, &executes
) {
3111 dp_packet_delete(e
->packet
);
3112 rule_execute_destroy(e
);
3117 rule_is_readonly(const struct rule
*rule
)
3119 const struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
3120 return (table
->flags
& OFTABLE_READONLY
) != 0;
3124 hash_learned_cookie(ovs_be64 cookie_
, uint8_t table_id
)
3126 uint64_t cookie
= (OVS_FORCE
uint64_t) cookie_
;
3127 return hash_3words(cookie
, cookie
>> 32, table_id
);
3131 learned_cookies_update_one__(struct ofproto
*ofproto
,
3132 const struct ofpact_learn
*learn
,
3133 int delta
, struct ovs_list
*dead_cookies
)
3134 OVS_REQUIRES(ofproto_mutex
)
3136 uint32_t hash
= hash_learned_cookie(learn
->cookie
, learn
->table_id
);
3137 struct learned_cookie
*c
;
3139 HMAP_FOR_EACH_WITH_HASH (c
, u
.hmap_node
, hash
, &ofproto
->learned_cookies
) {
3140 if (c
->cookie
== learn
->cookie
&& c
->table_id
== learn
->table_id
) {
3142 ovs_assert(c
->n
>= 0);
3145 hmap_remove(&ofproto
->learned_cookies
, &c
->u
.hmap_node
);
3146 ovs_list_push_back(dead_cookies
, &c
->u
.list_node
);
3153 ovs_assert(delta
> 0);
3154 c
= xmalloc(sizeof *c
);
3155 hmap_insert(&ofproto
->learned_cookies
, &c
->u
.hmap_node
, hash
);
3156 c
->cookie
= learn
->cookie
;
3157 c
->table_id
= learn
->table_id
;
3161 static const struct ofpact_learn
*
3162 next_learn_with_delete(const struct rule_actions
*actions
,
3163 const struct ofpact_learn
*start
)
3165 const struct ofpact
*pos
;
3167 for (pos
= start
? ofpact_next(&start
->ofpact
) : actions
->ofpacts
;
3168 pos
< ofpact_end(actions
->ofpacts
, actions
->ofpacts_len
);
3169 pos
= ofpact_next(pos
)) {
3170 if (pos
->type
== OFPACT_LEARN
) {
3171 const struct ofpact_learn
*learn
= ofpact_get_LEARN(pos
);
3172 if (learn
->flags
& NX_LEARN_F_DELETE_LEARNED
) {
3182 learned_cookies_update__(struct ofproto
*ofproto
,
3183 const struct rule_actions
*actions
,
3184 int delta
, struct ovs_list
*dead_cookies
)
3185 OVS_REQUIRES(ofproto_mutex
)
3187 if (actions
->has_learn_with_delete
) {
3188 const struct ofpact_learn
*learn
;
3190 for (learn
= next_learn_with_delete(actions
, NULL
); learn
;
3191 learn
= next_learn_with_delete(actions
, learn
)) {
3192 learned_cookies_update_one__(ofproto
, learn
, delta
, dead_cookies
);
3198 learned_cookies_inc(struct ofproto
*ofproto
,
3199 const struct rule_actions
*actions
)
3200 OVS_REQUIRES(ofproto_mutex
)
3202 learned_cookies_update__(ofproto
, actions
, +1, NULL
);
3206 learned_cookies_dec(struct ofproto
*ofproto
,
3207 const struct rule_actions
*actions
,
3208 struct ovs_list
*dead_cookies
)
3209 OVS_REQUIRES(ofproto_mutex
)
3211 learned_cookies_update__(ofproto
, actions
, -1, dead_cookies
);
3215 learned_cookies_flush(struct ofproto
*ofproto
, struct ovs_list
*dead_cookies
)
3216 OVS_REQUIRES(ofproto_mutex
)
3218 struct learned_cookie
*c
;
3220 LIST_FOR_EACH_POP (c
, u
.list_node
, dead_cookies
) {
3221 struct rule_criteria criteria
;
3222 struct rule_collection rules
;
3225 match_init_catchall(&match
);
3226 rule_criteria_init(&criteria
, c
->table_id
, &match
, 0, OVS_VERSION_MAX
,
3227 c
->cookie
, OVS_BE64_MAX
, OFPP_ANY
, OFPG_ANY
);
3228 rule_criteria_require_rw(&criteria
, false);
3229 collect_rules_loose(ofproto
, &criteria
, &rules
);
3230 rule_criteria_destroy(&criteria
);
3231 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
3238 handle_echo_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3240 ofconn_send_reply(ofconn
, make_echo_reply(oh
));
3245 query_tables(struct ofproto
*ofproto
,
3246 struct ofputil_table_features
**featuresp
,
3247 struct ofputil_table_stats
**statsp
)
3249 struct mf_bitmap rw_fields
= oxm_writable_fields();
3250 struct mf_bitmap match
= oxm_matchable_fields();
3251 struct mf_bitmap mask
= oxm_maskable_fields();
3253 struct ofputil_table_features
*features
;
3254 struct ofputil_table_stats
*stats
;
3257 features
= *featuresp
= xcalloc(ofproto
->n_tables
, sizeof *features
);
3258 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3259 struct ofputil_table_features
*f
= &features
[i
];
3262 sprintf(f
->name
, "table%d", i
);
3263 f
->metadata_match
= OVS_BE64_MAX
;
3264 f
->metadata_write
= OVS_BE64_MAX
;
3265 atomic_read_relaxed(&ofproto
->tables
[i
].miss_config
, &f
->miss_config
);
3266 f
->max_entries
= 1000000;
3268 bool more_tables
= false;
3269 for (int j
= i
+ 1; j
< ofproto
->n_tables
; j
++) {
3270 if (!(ofproto
->tables
[j
].flags
& OFTABLE_HIDDEN
)) {
3271 bitmap_set1(f
->nonmiss
.next
, j
);
3275 f
->nonmiss
.instructions
= (1u << N_OVS_INSTRUCTIONS
) - 1;
3277 f
->nonmiss
.instructions
&= ~(1u << OVSINST_OFPIT11_GOTO_TABLE
);
3279 f
->nonmiss
.write
.ofpacts
= (UINT64_C(1) << N_OFPACTS
) - 1;
3280 f
->nonmiss
.write
.set_fields
= rw_fields
;
3281 f
->nonmiss
.apply
= f
->nonmiss
.write
;
3282 f
->miss
= f
->nonmiss
;
3286 f
->wildcard
= match
;
3290 stats
= *statsp
= xcalloc(ofproto
->n_tables
, sizeof *stats
);
3291 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3292 struct ofputil_table_stats
*s
= &stats
[i
];
3295 s
->active_count
= ofproto
->tables
[i
].n_flows
;
3297 s
->active_count
-= connmgr_count_hidden_rules(
3305 ofproto
->ofproto_class
->query_tables(ofproto
, features
, stats
);
3307 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3308 const struct oftable
*table
= &ofproto
->tables
[i
];
3309 struct ofputil_table_features
*f
= &features
[i
];
3312 ovs_strzcpy(f
->name
, table
->name
, sizeof f
->name
);
3315 if (table
->max_flows
< f
->max_entries
) {
3316 f
->max_entries
= table
->max_flows
;
3322 query_switch_features(struct ofproto
*ofproto
,
3323 bool *arp_match_ip
, uint64_t *ofpacts
)
3325 struct ofputil_table_features
*features
, *f
;
3327 *arp_match_ip
= false;
3330 query_tables(ofproto
, &features
, NULL
);
3331 for (f
= features
; f
< &features
[ofproto
->n_tables
]; f
++) {
3332 *ofpacts
|= f
->nonmiss
.apply
.ofpacts
| f
->miss
.apply
.ofpacts
;
3333 if (bitmap_is_set(f
->match
.bm
, MFF_ARP_SPA
) ||
3334 bitmap_is_set(f
->match
.bm
, MFF_ARP_TPA
)) {
3335 *arp_match_ip
= true;
3341 ovs_assert(*ofpacts
& (UINT64_C(1) << OFPACT_OUTPUT
));
3345 handle_features_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3347 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3348 struct ofputil_switch_features features
;
3349 struct ofport
*port
;
3353 query_switch_features(ofproto
, &arp_match_ip
, &features
.ofpacts
);
3355 features
.datapath_id
= ofproto
->datapath_id
;
3356 features
.n_buffers
= pktbuf_capacity();
3357 features
.n_tables
= ofproto_get_n_visible_tables(ofproto
);
3358 features
.capabilities
= (OFPUTIL_C_FLOW_STATS
| OFPUTIL_C_TABLE_STATS
|
3359 OFPUTIL_C_PORT_STATS
| OFPUTIL_C_QUEUE_STATS
|
3360 OFPUTIL_C_GROUP_STATS
);
3362 features
.capabilities
|= OFPUTIL_C_ARP_MATCH_IP
;
3364 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
3365 features
.auxiliary_id
= 0;
3366 b
= ofputil_encode_switch_features(&features
, ofconn_get_protocol(ofconn
),
3368 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3369 ofputil_put_switch_features_port(&port
->pp
, b
);
3372 ofconn_send_reply(ofconn
, b
);
3377 handle_get_config_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3379 struct ofputil_switch_config config
;
3380 config
.frag
= ofconn_get_ofproto(ofconn
)->frag_handling
;
3381 config
.invalid_ttl_to_controller
3382 = ofconn_get_invalid_ttl_to_controller(ofconn
);
3383 config
.miss_send_len
= ofconn_get_miss_send_len(ofconn
);
3385 ofconn_send_reply(ofconn
, ofputil_encode_get_config_reply(oh
, &config
));
3391 handle_set_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3393 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3394 struct ofputil_switch_config config
;
3397 error
= ofputil_decode_set_config(oh
, &config
);
3402 if (ofconn_get_type(ofconn
) != OFCONN_PRIMARY
3403 || ofconn_get_role(ofconn
) != OFPCR12_ROLE_SLAVE
) {
3404 enum ofputil_frag_handling cur
= ofproto
->frag_handling
;
3405 enum ofputil_frag_handling next
= config
.frag
;
3408 if (ofproto
->ofproto_class
->set_frag_handling(ofproto
, next
)) {
3409 ofproto
->frag_handling
= next
;
3411 VLOG_WARN_RL(&rl
, "%s: unsupported fragment handling mode %s",
3413 ofputil_frag_handling_to_string(next
));
3418 if (config
.invalid_ttl_to_controller
>= 0) {
3419 ofconn_set_invalid_ttl_to_controller(ofconn
,
3420 config
.invalid_ttl_to_controller
);
3423 ofconn_set_miss_send_len(ofconn
, config
.miss_send_len
);
3428 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
3429 * error message code for the caller to propagate upward. Otherwise, returns
3432 * The log message mentions 'msg_type'. */
3434 reject_slave_controller(struct ofconn
*ofconn
)
3436 if (ofconn_get_type(ofconn
) == OFCONN_PRIMARY
3437 && ofconn_get_role(ofconn
) == OFPCR12_ROLE_SLAVE
) {
3438 return OFPERR_OFPBRC_IS_SLAVE
;
3444 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
3447 * - If they use a meter, then 'ofproto' has that meter configured.
3449 * - If they use any groups, then 'ofproto' has that group configured.
3451 * Returns 0 if successful, otherwise an OpenFlow error. */
3453 ofproto_check_ofpacts(struct ofproto
*ofproto
,
3454 const struct ofpact ofpacts
[], size_t ofpacts_len
)
3458 mid
= ofpacts_get_meter(ofpacts
, ofpacts_len
);
3459 if (mid
&& get_provider_meter_id(ofproto
, mid
) == UINT32_MAX
) {
3460 return OFPERR_OFPMMFC_INVALID_METER
;
3463 const struct ofpact_group
*a
;
3464 OFPACT_FOR_EACH_TYPE_FLATTENED (a
, GROUP
, ofpacts
, ofpacts_len
) {
3465 if (!ofproto_group_exists(ofproto
, a
->group_id
)) {
3466 return OFPERR_OFPBAC_BAD_OUT_GROUP
;
3474 handle_packet_out(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3476 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3477 struct ofputil_packet_out po
;
3478 struct dp_packet
*payload
;
3479 uint64_t ofpacts_stub
[1024 / 8];
3480 struct ofpbuf ofpacts
;
3484 COVERAGE_INC(ofproto_packet_out
);
3486 error
= reject_slave_controller(ofconn
);
3491 /* Decode message. */
3492 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
3493 error
= ofputil_decode_packet_out(&po
, oh
, &ofpacts
);
3495 goto exit_free_ofpacts
;
3497 if (ofp_to_u16(po
.in_port
) >= p
->max_ports
3498 && ofp_to_u16(po
.in_port
) < ofp_to_u16(OFPP_MAX
)) {
3499 error
= OFPERR_OFPBRC_BAD_PORT
;
3500 goto exit_free_ofpacts
;
3504 if (po
.buffer_id
!= UINT32_MAX
) {
3505 error
= ofconn_pktbuf_retrieve(ofconn
, po
.buffer_id
, &payload
, NULL
);
3507 goto exit_free_ofpacts
;
3510 /* Ensure that the L3 header is 32-bit aligned. */
3511 payload
= dp_packet_clone_data_with_headroom(po
.packet
, po
.packet_len
, 2);
3514 /* Verify actions against packet, then send packet if successful. */
3515 flow_extract(payload
, &flow
);
3516 flow
.in_port
.ofp_port
= po
.in_port
;
3518 /* Check actions like for flow mods. We pass a 'table_id' of 0 to
3519 * ofproto_check_consistency(), which isn't strictly correct because these
3520 * actions aren't in any table. This is OK as 'table_id' is only used to
3521 * check instructions (e.g., goto-table), which can't appear on the action
3522 * list of a packet-out. */
3523 error
= ofpacts_check_consistency(po
.ofpacts
, po
.ofpacts_len
,
3524 &flow
, u16_to_ofp(p
->max_ports
),
3526 ofconn_get_protocol(ofconn
));
3528 /* Should hold ofproto_mutex to guarantee state don't
3529 * change between the check and the execution. */
3530 error
= ofproto_check_ofpacts(p
, po
.ofpacts
, po
.ofpacts_len
);
3532 error
= p
->ofproto_class
->packet_out(p
, payload
, &flow
,
3533 po
.ofpacts
, po
.ofpacts_len
);
3536 dp_packet_delete(payload
);
3539 ofpbuf_uninit(&ofpacts
);
3545 handle_nxt_resume(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3547 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3548 struct ofputil_packet_in_private pin
;
3551 error
= ofputil_decode_packet_in_private(oh
, false, &pin
, NULL
, NULL
);
3556 error
= (ofproto
->ofproto_class
->nxt_resume
3557 ? ofproto
->ofproto_class
->nxt_resume(ofproto
, &pin
)
3558 : OFPERR_NXR_NOT_SUPPORTED
);
3560 ofputil_packet_in_private_destroy(&pin
);
3566 update_port_config(struct ofconn
*ofconn
, struct ofport
*port
,
3567 enum ofputil_port_config config
,
3568 enum ofputil_port_config mask
)
3570 enum ofputil_port_config toggle
= (config
^ port
->pp
.config
) & mask
;
3572 if (toggle
& OFPUTIL_PC_PORT_DOWN
3573 && (config
& OFPUTIL_PC_PORT_DOWN
3574 ? netdev_turn_flags_off(port
->netdev
, NETDEV_UP
, NULL
)
3575 : netdev_turn_flags_on(port
->netdev
, NETDEV_UP
, NULL
))) {
3576 /* We tried to bring the port up or down, but it failed, so don't
3577 * update the "down" bit. */
3578 toggle
&= ~OFPUTIL_PC_PORT_DOWN
;
3582 enum ofputil_port_config old_config
= port
->pp
.config
;
3583 port
->pp
.config
^= toggle
;
3584 port
->ofproto
->ofproto_class
->port_reconfigured(port
, old_config
);
3585 connmgr_send_port_status(port
->ofproto
->connmgr
, ofconn
, &port
->pp
,
3591 port_mod_start(struct ofconn
*ofconn
, struct ofputil_port_mod
*pm
,
3592 struct ofport
**port
)
3594 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3596 *port
= ofproto_get_port(p
, pm
->port_no
);
3598 return OFPERR_OFPPMFC_BAD_PORT
;
3600 if (!eth_addr_equals((*port
)->pp
.hw_addr
, pm
->hw_addr
)) {
3601 return OFPERR_OFPPMFC_BAD_HW_ADDR
;
3607 port_mod_finish(struct ofconn
*ofconn
, struct ofputil_port_mod
*pm
,
3608 struct ofport
*port
)
3610 update_port_config(ofconn
, port
, pm
->config
, pm
->mask
);
3611 if (pm
->advertise
) {
3612 netdev_set_advertisements(port
->netdev
, pm
->advertise
);
3617 handle_port_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3619 struct ofputil_port_mod pm
;
3620 struct ofport
*port
;
3623 error
= reject_slave_controller(ofconn
);
3628 error
= ofputil_decode_port_mod(oh
, &pm
, false);
3633 error
= port_mod_start(ofconn
, &pm
, &port
);
3635 port_mod_finish(ofconn
, &pm
, port
);
3641 handle_desc_stats_request(struct ofconn
*ofconn
,
3642 const struct ofp_header
*request
)
3644 static const char *default_mfr_desc
= "Nicira, Inc.";
3645 static const char *default_hw_desc
= "Open vSwitch";
3646 static const char *default_sw_desc
= VERSION
;
3647 static const char *default_serial_desc
= "None";
3648 static const char *default_dp_desc
= "None";
3650 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3651 struct ofp_desc_stats
*ods
;
3654 msg
= ofpraw_alloc_stats_reply(request
, 0);
3655 ods
= ofpbuf_put_zeros(msg
, sizeof *ods
);
3656 ovs_strlcpy(ods
->mfr_desc
, p
->mfr_desc
? p
->mfr_desc
: default_mfr_desc
,
3657 sizeof ods
->mfr_desc
);
3658 ovs_strlcpy(ods
->hw_desc
, p
->hw_desc
? p
->hw_desc
: default_hw_desc
,
3659 sizeof ods
->hw_desc
);
3660 ovs_strlcpy(ods
->sw_desc
, p
->sw_desc
? p
->sw_desc
: default_sw_desc
,
3661 sizeof ods
->sw_desc
);
3662 ovs_strlcpy(ods
->serial_num
,
3663 p
->serial_desc
? p
->serial_desc
: default_serial_desc
,
3664 sizeof ods
->serial_num
);
3665 ovs_strlcpy(ods
->dp_desc
, p
->dp_desc
? p
->dp_desc
: default_dp_desc
,
3666 sizeof ods
->dp_desc
);
3667 ofconn_send_reply(ofconn
, msg
);
3673 handle_table_stats_request(struct ofconn
*ofconn
,
3674 const struct ofp_header
*request
)
3676 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3677 struct ofputil_table_features
*features
;
3678 struct ofputil_table_stats
*stats
;
3679 struct ofpbuf
*reply
;
3682 query_tables(ofproto
, &features
, &stats
);
3684 reply
= ofputil_encode_table_stats_reply(request
);
3685 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3686 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3687 ofputil_append_table_stats_reply(reply
, &stats
[i
], &features
[i
]);
3690 ofconn_send_reply(ofconn
, reply
);
3699 handle_table_features_request(struct ofconn
*ofconn
,
3700 const struct ofp_header
*request
)
3702 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3703 struct ofpbuf msg
= ofpbuf_const_initializer(request
,
3704 ntohs(request
->length
));
3705 ofpraw_pull_assert(&msg
);
3706 if (msg
.size
|| ofpmp_more(request
)) {
3707 return OFPERR_OFPTFFC_EPERM
;
3710 struct ofputil_table_features
*features
;
3711 query_tables(ofproto
, &features
, NULL
);
3713 struct ovs_list replies
;
3714 ofpmp_init(&replies
, request
);
3715 for (size_t i
= 0; i
< ofproto
->n_tables
; i
++) {
3716 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3717 ofputil_append_table_features_reply(&features
[i
], &replies
);
3720 ofconn_send_replies(ofconn
, &replies
);
3727 /* Returns the vacancy of 'oftable', a number that ranges from 0 (if the table
3728 * is full) to 100 (if the table is empty).
3730 * A table without a limit on flows is considered to be empty. */
3732 oftable_vacancy(const struct oftable
*t
)
3734 return (!t
->max_flows
? 100
3735 : t
->n_flows
>= t
->max_flows
? 0
3736 : (t
->max_flows
- t
->n_flows
) * 100.0 / t
->max_flows
);
3740 query_table_desc__(struct ofputil_table_desc
*td
,
3741 struct ofproto
*ofproto
, uint8_t table_id
)
3743 const struct oftable
*t
= &ofproto
->tables
[table_id
];
3745 td
->table_id
= table_id
;
3746 td
->eviction
= (t
->eviction
& EVICTION_OPENFLOW
3747 ? OFPUTIL_TABLE_EVICTION_ON
3748 : OFPUTIL_TABLE_EVICTION_OFF
);
3749 td
->eviction_flags
= OFPROTO_EVICTION_FLAGS
;
3750 td
->vacancy
= (t
->vacancy_event
3751 ? OFPUTIL_TABLE_VACANCY_ON
3752 : OFPUTIL_TABLE_VACANCY_OFF
);
3753 td
->table_vacancy
.vacancy_down
= t
->vacancy_down
;
3754 td
->table_vacancy
.vacancy_up
= t
->vacancy_up
;
3755 td
->table_vacancy
.vacancy
= oftable_vacancy(t
);
3758 /* This function queries the database for dumping table-desc. */
3760 query_tables_desc(struct ofproto
*ofproto
, struct ofputil_table_desc
**descp
)
3762 struct ofputil_table_desc
*table_desc
;
3765 table_desc
= *descp
= xcalloc(ofproto
->n_tables
, sizeof *table_desc
);
3766 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3767 struct ofputil_table_desc
*td
= &table_desc
[i
];
3768 query_table_desc__(td
, ofproto
, i
);
3772 /* Function to handle dump-table-desc request. */
3774 handle_table_desc_request(struct ofconn
*ofconn
,
3775 const struct ofp_header
*request
)
3777 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3778 struct ofputil_table_desc
*table_desc
;
3779 struct ovs_list replies
;
3782 query_tables_desc(ofproto
, &table_desc
);
3783 ofpmp_init(&replies
, request
);
3784 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3785 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3786 ofputil_append_table_desc_reply(&table_desc
[i
], &replies
,
3790 ofconn_send_replies(ofconn
, &replies
);
3795 /* This function determines and sends the vacancy event, based on the value
3796 * of current vacancy and threshold vacancy. If the current vacancy is less
3797 * than or equal to vacancy_down, vacancy up events must be enabled, and when
3798 * the current vacancy is greater or equal to vacancy_up, vacancy down events
3799 * must be enabled. */
3801 send_table_status(struct ofproto
*ofproto
, uint8_t table_id
)
3803 struct oftable
*t
= &ofproto
->tables
[table_id
];
3804 if (!t
->vacancy_event
) {
3808 uint8_t vacancy
= oftable_vacancy(t
);
3809 enum ofp14_table_reason event
;
3810 if (vacancy
< t
->vacancy_down
) {
3811 event
= OFPTR_VACANCY_DOWN
;
3812 } else if (vacancy
> t
->vacancy_up
) {
3813 event
= OFPTR_VACANCY_UP
;
3818 if (event
== t
->vacancy_event
) {
3819 struct ofputil_table_desc td
;
3820 query_table_desc__(&td
, ofproto
, table_id
);
3821 connmgr_send_table_status(ofproto
->connmgr
, &td
, event
);
3823 t
->vacancy_event
= (event
== OFPTR_VACANCY_DOWN
3825 : OFPTR_VACANCY_DOWN
);
3830 append_port_stat(struct ofport
*port
, struct ovs_list
*replies
)
3832 struct ofputil_port_stats ops
= { .port_no
= port
->pp
.port_no
};
3834 calc_duration(port
->created
, time_msec(),
3835 &ops
.duration_sec
, &ops
.duration_nsec
);
3837 /* Intentionally ignore return value, since errors will set
3838 * 'stats' to all-1s, which is correct for OpenFlow, and
3839 * netdev_get_stats() will log errors. */
3840 ofproto_port_get_stats(port
, &ops
.stats
);
3842 ofputil_append_port_stat(replies
, &ops
);
3846 handle_port_request(struct ofconn
*ofconn
,
3847 const struct ofp_header
*request
, ofp_port_t port_no
,
3848 void (*cb
)(struct ofport
*, struct ovs_list
*replies
))
3850 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3851 struct ofport
*port
;
3852 struct ovs_list replies
;
3854 ofpmp_init(&replies
, request
);
3855 if (port_no
!= OFPP_ANY
) {
3856 port
= ofproto_get_port(ofproto
, port_no
);
3861 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3866 ofconn_send_replies(ofconn
, &replies
);
3870 handle_port_stats_request(struct ofconn
*ofconn
,
3871 const struct ofp_header
*request
)
3876 error
= ofputil_decode_port_stats_request(request
, &port_no
);
3878 handle_port_request(ofconn
, request
, port_no
, append_port_stat
);
3884 append_port_desc(struct ofport
*port
, struct ovs_list
*replies
)
3886 ofputil_append_port_desc_stats_reply(&port
->pp
, replies
);
3890 handle_port_desc_stats_request(struct ofconn
*ofconn
,
3891 const struct ofp_header
*request
)
3896 error
= ofputil_decode_port_desc_stats_request(request
, &port_no
);
3898 handle_port_request(ofconn
, request
, port_no
, append_port_desc
);
3904 hash_cookie(ovs_be64 cookie
)
3906 return hash_uint64((OVS_FORCE
uint64_t)cookie
);
3910 cookies_insert(struct ofproto
*ofproto
, struct rule
*rule
)
3911 OVS_REQUIRES(ofproto_mutex
)
3913 hindex_insert(&ofproto
->cookies
, &rule
->cookie_node
,
3914 hash_cookie(rule
->flow_cookie
));
3918 cookies_remove(struct ofproto
*ofproto
, struct rule
*rule
)
3919 OVS_REQUIRES(ofproto_mutex
)
3921 hindex_remove(&ofproto
->cookies
, &rule
->cookie_node
);
3925 calc_duration(long long int start
, long long int now
,
3926 uint32_t *sec
, uint32_t *nsec
)
3928 long long int msecs
= now
- start
;
3929 *sec
= msecs
/ 1000;
3930 *nsec
= (msecs
% 1000) * (1000 * 1000);
3933 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3934 * true if 'table_id' is OK, false otherwise. */
3936 check_table_id(const struct ofproto
*ofproto
, uint8_t table_id
)
3938 return table_id
== OFPTT_ALL
|| table_id
< ofproto
->n_tables
;
3941 static struct oftable
*
3942 next_visible_table(const struct ofproto
*ofproto
, uint8_t table_id
)
3944 struct oftable
*table
;
3946 for (table
= &ofproto
->tables
[table_id
];
3947 table
< &ofproto
->tables
[ofproto
->n_tables
];
3949 if (!(table
->flags
& OFTABLE_HIDDEN
)) {
3957 static struct oftable
*
3958 first_matching_table(const struct ofproto
*ofproto
, uint8_t table_id
)
3960 if (table_id
== 0xff) {
3961 return next_visible_table(ofproto
, 0);
3962 } else if (table_id
< ofproto
->n_tables
) {
3963 return &ofproto
->tables
[table_id
];
3969 static struct oftable
*
3970 next_matching_table(const struct ofproto
*ofproto
,
3971 const struct oftable
*table
, uint8_t table_id
)
3973 return (table_id
== 0xff
3974 ? next_visible_table(ofproto
, (table
- ofproto
->tables
) + 1)
3978 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3980 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3981 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3983 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3984 * only once, for that table. (This can be used to access tables marked
3987 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3988 * entered at all. (Perhaps you should have validated TABLE_ID with
3989 * check_table_id().)
3991 * All parameters are evaluated multiple times.
3993 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3994 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3996 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3998 /* Initializes 'criteria' in a straightforward way based on the other
4001 * By default, the criteria include flows that are read-only, on the assumption
4002 * that the collected flows won't be modified. Call rule_criteria_require_rw()
4003 * if flows will be modified.
4005 * For "loose" matching, the 'priority' parameter is unimportant and may be
4008 rule_criteria_init(struct rule_criteria
*criteria
, uint8_t table_id
,
4009 const struct match
*match
, int priority
,
4010 ovs_version_t version
, ovs_be64 cookie
,
4011 ovs_be64 cookie_mask
, ofp_port_t out_port
,
4014 criteria
->table_id
= table_id
;
4015 cls_rule_init(&criteria
->cr
, match
, priority
);
4016 criteria
->version
= version
;
4017 criteria
->cookie
= cookie
;
4018 criteria
->cookie_mask
= cookie_mask
;
4019 criteria
->out_port
= out_port
;
4020 criteria
->out_group
= out_group
;
4022 /* We ordinarily want to skip hidden rules, but there has to be a way for
4023 * code internal to OVS to modify and delete them, so if the criteria
4024 * specify a priority that can only be for a hidden flow, then allow hidden
4025 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
4026 * with hidden flows because OpenFlow uses only a 16-bit field to specify
4028 criteria
->include_hidden
= priority
> UINT16_MAX
;
4030 /* We assume that the criteria are being used to collect flows for reading
4031 * but not modification. Thus, we should collect read-only flows. */
4032 criteria
->include_readonly
= true;
4035 /* By default, criteria initialized by rule_criteria_init() will match flows
4036 * that are read-only, on the assumption that the collected flows won't be
4037 * modified. Call this function to match only flows that are be modifiable.
4039 * Specify 'can_write_readonly' as false in ordinary circumstances, true if the
4040 * caller has special privileges that allow it to modify even "read-only"
4043 rule_criteria_require_rw(struct rule_criteria
*criteria
,
4044 bool can_write_readonly
)
4046 criteria
->include_readonly
= can_write_readonly
;
4050 rule_criteria_destroy(struct rule_criteria
*criteria
)
4052 cls_rule_destroy(&criteria
->cr
);
4053 criteria
->version
= OVS_VERSION_NOT_REMOVED
; /* Mark as destroyed. */
4056 /* Schedules postponed removal of rules, destroys 'rules'. */
4058 remove_rules_postponed(struct rule_collection
*rules
)
4059 OVS_REQUIRES(ofproto_mutex
)
4061 if (rule_collection_n(rules
) > 0) {
4062 if (rule_collection_n(rules
) == 1) {
4063 ovsrcu_postpone(remove_rule_rcu
, rule_collection_rules(rules
)[0]);
4064 rule_collection_init(rules
);
4066 ovsrcu_postpone(remove_rules_rcu
, rule_collection_detach(rules
));
4071 /* Schedules postponed removal of groups, destroys 'groups'. */
4073 remove_groups_postponed(struct group_collection
*groups
)
4074 OVS_REQUIRES(ofproto_mutex
)
4076 if (group_collection_n(groups
) > 0) {
4077 if (group_collection_n(groups
) == 1) {
4078 ovsrcu_postpone(remove_group_rcu
,
4079 group_collection_groups(groups
)[0]);
4080 group_collection_init(groups
);
4082 ovsrcu_postpone(remove_groups_rcu
,
4083 group_collection_detach(groups
));
4088 /* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This
4089 * function verifies most of the criteria in 'c' itself, but the caller must
4090 * check 'c->cr' itself.
4092 * Rules that have already been marked for removal are not collected.
4094 * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and
4095 * 'c' only includes modifiable rules). */
4097 collect_rule(struct rule
*rule
, const struct rule_criteria
*c
,
4098 struct rule_collection
*rules
, size_t *n_readonly
)
4099 OVS_REQUIRES(ofproto_mutex
)
4101 if ((c
->table_id
== rule
->table_id
|| c
->table_id
== 0xff)
4102 && ofproto_rule_has_out_port(rule
, c
->out_port
)
4103 && ofproto_rule_has_out_group(rule
, c
->out_group
)
4104 && !((rule
->flow_cookie
^ c
->cookie
) & c
->cookie_mask
)
4105 && (!rule_is_hidden(rule
) || c
->include_hidden
)
4106 && cls_rule_visible_in_version(&rule
->cr
, c
->version
)) {
4107 /* Rule matches all the criteria... */
4108 if (!rule_is_readonly(rule
) || c
->include_readonly
) {
4110 rule_collection_add(rules
, rule
);
4112 /* ...except it's read-only. */
4118 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4119 * on classifiers rules are done in the "loose" way required for OpenFlow
4120 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
4123 * Returns 0 on success, otherwise an OpenFlow error code. */
4125 collect_rules_loose(struct ofproto
*ofproto
,
4126 const struct rule_criteria
*criteria
,
4127 struct rule_collection
*rules
)
4128 OVS_REQUIRES(ofproto_mutex
)
4130 struct oftable
*table
;
4131 enum ofperr error
= 0;
4132 size_t n_readonly
= 0;
4134 rule_collection_init(rules
);
4136 if (!check_table_id(ofproto
, criteria
->table_id
)) {
4137 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4141 if (criteria
->cookie_mask
== OVS_BE64_MAX
) {
4144 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
4145 hash_cookie(criteria
->cookie
),
4146 &ofproto
->cookies
) {
4147 if (cls_rule_is_loose_match(&rule
->cr
, &criteria
->cr
.match
)) {
4148 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4152 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
4155 CLS_FOR_EACH_TARGET (rule
, cr
, &table
->cls
, &criteria
->cr
,
4156 criteria
->version
) {
4157 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4163 if (!error
&& !rule_collection_n(rules
) && n_readonly
) {
4164 /* We didn't find any rules to modify. We did find some read-only
4165 * rules that we're not allowed to modify, so report that. */
4166 error
= OFPERR_OFPBRC_EPERM
;
4169 rule_collection_destroy(rules
);
4174 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4175 * on classifiers rules are done in the "strict" way required for OpenFlow
4176 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
4177 * rules on list 'rules'.
4179 * Returns 0 on success, otherwise an OpenFlow error code. */
4181 collect_rules_strict(struct ofproto
*ofproto
,
4182 const struct rule_criteria
*criteria
,
4183 struct rule_collection
*rules
)
4184 OVS_REQUIRES(ofproto_mutex
)
4186 struct oftable
*table
;
4187 size_t n_readonly
= 0;
4188 enum ofperr error
= 0;
4190 rule_collection_init(rules
);
4192 if (!check_table_id(ofproto
, criteria
->table_id
)) {
4193 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4197 if (criteria
->cookie_mask
== OVS_BE64_MAX
) {
4200 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
4201 hash_cookie(criteria
->cookie
),
4202 &ofproto
->cookies
) {
4203 if (cls_rule_equal(&rule
->cr
, &criteria
->cr
)) {
4204 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4208 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
4211 rule
= rule_from_cls_rule(classifier_find_rule_exactly(
4212 &table
->cls
, &criteria
->cr
,
4213 criteria
->version
));
4215 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4221 if (!error
&& !rule_collection_n(rules
) && n_readonly
) {
4222 /* We didn't find any rules to modify. We did find some read-only
4223 * rules that we're not allowed to modify, so report that. */
4224 error
= OFPERR_OFPBRC_EPERM
;
4227 rule_collection_destroy(rules
);
4232 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
4233 * forced into the range of a uint16_t. */
4235 age_secs(long long int age_ms
)
4237 return (age_ms
< 0 ? 0
4238 : age_ms
>= UINT16_MAX
* 1000 ? UINT16_MAX
4239 : (unsigned int) age_ms
/ 1000);
4243 handle_flow_stats_request(struct ofconn
*ofconn
,
4244 const struct ofp_header
*request
)
4245 OVS_EXCLUDED(ofproto_mutex
)
4247 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4248 struct ofputil_flow_stats_request fsr
;
4249 struct rule_criteria criteria
;
4250 struct rule_collection rules
;
4251 struct ovs_list replies
;
4254 error
= ofputil_decode_flow_stats_request(&fsr
, request
);
4259 rule_criteria_init(&criteria
, fsr
.table_id
, &fsr
.match
, 0, OVS_VERSION_MAX
,
4260 fsr
.cookie
, fsr
.cookie_mask
, fsr
.out_port
,
4263 ovs_mutex_lock(&ofproto_mutex
);
4264 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4265 rule_criteria_destroy(&criteria
);
4267 rule_collection_ref(&rules
);
4269 ovs_mutex_unlock(&ofproto_mutex
);
4275 ofpmp_init(&replies
, request
);
4277 RULE_COLLECTION_FOR_EACH (rule
, &rules
) {
4278 long long int now
= time_msec();
4279 struct ofputil_flow_stats fs
;
4280 long long int created
, used
, modified
;
4281 const struct rule_actions
*actions
;
4282 enum ofputil_flow_mod_flags flags
;
4284 ovs_mutex_lock(&rule
->mutex
);
4285 fs
.cookie
= rule
->flow_cookie
;
4286 fs
.idle_timeout
= rule
->idle_timeout
;
4287 fs
.hard_timeout
= rule
->hard_timeout
;
4288 fs
.importance
= rule
->importance
;
4289 created
= rule
->created
;
4290 modified
= rule
->modified
;
4291 actions
= rule_get_actions(rule
);
4292 flags
= rule
->flags
;
4293 ovs_mutex_unlock(&rule
->mutex
);
4295 ofproto
->ofproto_class
->rule_get_stats(rule
, &fs
.packet_count
,
4296 &fs
.byte_count
, &used
);
4298 minimatch_expand(&rule
->cr
.match
, &fs
.match
);
4299 fs
.table_id
= rule
->table_id
;
4300 calc_duration(created
, now
, &fs
.duration_sec
, &fs
.duration_nsec
);
4301 fs
.priority
= rule
->cr
.priority
;
4302 fs
.idle_age
= age_secs(now
- used
);
4303 fs
.hard_age
= age_secs(now
- modified
);
4304 fs
.ofpacts
= actions
->ofpacts
;
4305 fs
.ofpacts_len
= actions
->ofpacts_len
;
4308 ofputil_append_flow_stats_reply(&fs
, &replies
);
4311 rule_collection_unref(&rules
);
4312 rule_collection_destroy(&rules
);
4314 ofconn_send_replies(ofconn
, &replies
);
4320 flow_stats_ds(struct rule
*rule
, struct ds
*results
)
4322 uint64_t packet_count
, byte_count
;
4323 const struct rule_actions
*actions
;
4324 long long int created
, used
;
4326 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
4327 &byte_count
, &used
);
4329 ovs_mutex_lock(&rule
->mutex
);
4330 actions
= rule_get_actions(rule
);
4331 created
= rule
->created
;
4332 ovs_mutex_unlock(&rule
->mutex
);
4334 if (rule
->table_id
!= 0) {
4335 ds_put_format(results
, "table_id=%"PRIu8
", ", rule
->table_id
);
4337 ds_put_format(results
, "duration=%llds, ", (time_msec() - created
) / 1000);
4338 ds_put_format(results
, "n_packets=%"PRIu64
", ", packet_count
);
4339 ds_put_format(results
, "n_bytes=%"PRIu64
", ", byte_count
);
4340 cls_rule_format(&rule
->cr
, results
);
4341 ds_put_char(results
, ',');
4343 ds_put_cstr(results
, "actions=");
4344 ofpacts_format(actions
->ofpacts
, actions
->ofpacts_len
, results
);
4346 ds_put_cstr(results
, "\n");
4349 /* Adds a pretty-printed description of all flows to 'results', including
4350 * hidden flows (e.g., set up by in-band control). */
4352 ofproto_get_all_flows(struct ofproto
*p
, struct ds
*results
)
4354 struct oftable
*table
;
4356 OFPROTO_FOR_EACH_TABLE (table
, p
) {
4359 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
4360 flow_stats_ds(rule
, results
);
4365 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
4366 * '*engine_type' and '*engine_id', respectively. */
4368 ofproto_get_netflow_ids(const struct ofproto
*ofproto
,
4369 uint8_t *engine_type
, uint8_t *engine_id
)
4371 ofproto
->ofproto_class
->get_netflow_ids(ofproto
, engine_type
, engine_id
);
4374 /* Checks the status change of CFM on 'ofport'.
4376 * Returns true if 'ofproto_class' does not support 'cfm_status_changed'. */
4378 ofproto_port_cfm_status_changed(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
4380 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
4381 return (ofport
&& ofproto
->ofproto_class
->cfm_status_changed
4382 ? ofproto
->ofproto_class
->cfm_status_changed(ofport
)
4386 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'.
4387 * Returns 0 if the port's CFM status was successfully stored into
4388 * '*status'. Returns positive errno if the port did not have CFM
4391 * The caller must provide and own '*status', and must free 'status->rmps'.
4392 * '*status' is indeterminate if the return value is non-zero. */
4394 ofproto_port_get_cfm_status(const struct ofproto
*ofproto
, ofp_port_t ofp_port
,
4395 struct cfm_status
*status
)
4397 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
4398 return (ofport
&& ofproto
->ofproto_class
->get_cfm_status
4399 ? ofproto
->ofproto_class
->get_cfm_status(ofport
, status
)
4404 handle_aggregate_stats_request(struct ofconn
*ofconn
,
4405 const struct ofp_header
*oh
)
4406 OVS_EXCLUDED(ofproto_mutex
)
4408 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4409 struct ofputil_flow_stats_request request
;
4410 struct ofputil_aggregate_stats stats
;
4411 bool unknown_packets
, unknown_bytes
;
4412 struct rule_criteria criteria
;
4413 struct rule_collection rules
;
4414 struct ofpbuf
*reply
;
4417 error
= ofputil_decode_flow_stats_request(&request
, oh
);
4422 rule_criteria_init(&criteria
, request
.table_id
, &request
.match
, 0,
4423 OVS_VERSION_MAX
, request
.cookie
, request
.cookie_mask
,
4424 request
.out_port
, request
.out_group
);
4426 ovs_mutex_lock(&ofproto_mutex
);
4427 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4428 rule_criteria_destroy(&criteria
);
4430 rule_collection_ref(&rules
);
4432 ovs_mutex_unlock(&ofproto_mutex
);
4438 memset(&stats
, 0, sizeof stats
);
4439 unknown_packets
= unknown_bytes
= false;
4442 RULE_COLLECTION_FOR_EACH (rule
, &rules
) {
4443 uint64_t packet_count
;
4444 uint64_t byte_count
;
4447 ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
4448 &byte_count
, &used
);
4450 if (packet_count
== UINT64_MAX
) {
4451 unknown_packets
= true;
4453 stats
.packet_count
+= packet_count
;
4456 if (byte_count
== UINT64_MAX
) {
4457 unknown_bytes
= true;
4459 stats
.byte_count
+= byte_count
;
4464 if (unknown_packets
) {
4465 stats
.packet_count
= UINT64_MAX
;
4467 if (unknown_bytes
) {
4468 stats
.byte_count
= UINT64_MAX
;
4471 rule_collection_unref(&rules
);
4472 rule_collection_destroy(&rules
);
4474 reply
= ofputil_encode_aggregate_stats_reply(&stats
, oh
);
4475 ofconn_send_reply(ofconn
, reply
);
4480 struct queue_stats_cbdata
{
4481 struct ofport
*ofport
;
4482 struct ovs_list replies
;
4487 put_queue_stats(struct queue_stats_cbdata
*cbdata
, uint32_t queue_id
,
4488 const struct netdev_queue_stats
*stats
)
4490 struct ofputil_queue_stats oqs
;
4492 oqs
.port_no
= cbdata
->ofport
->pp
.port_no
;
4493 oqs
.queue_id
= queue_id
;
4494 oqs
.tx_bytes
= stats
->tx_bytes
;
4495 oqs
.tx_packets
= stats
->tx_packets
;
4496 oqs
.tx_errors
= stats
->tx_errors
;
4497 if (stats
->created
!= LLONG_MIN
) {
4498 calc_duration(stats
->created
, cbdata
->now
,
4499 &oqs
.duration_sec
, &oqs
.duration_nsec
);
4501 oqs
.duration_sec
= oqs
.duration_nsec
= UINT32_MAX
;
4503 ofputil_append_queue_stat(&cbdata
->replies
, &oqs
);
4507 handle_queue_stats_dump_cb(uint32_t queue_id
,
4508 struct netdev_queue_stats
*stats
,
4511 struct queue_stats_cbdata
*cbdata
= cbdata_
;
4513 put_queue_stats(cbdata
, queue_id
, stats
);
4517 handle_queue_stats_for_port(struct ofport
*port
, uint32_t queue_id
,
4518 struct queue_stats_cbdata
*cbdata
)
4520 cbdata
->ofport
= port
;
4521 if (queue_id
== OFPQ_ALL
) {
4522 netdev_dump_queue_stats(port
->netdev
,
4523 handle_queue_stats_dump_cb
, cbdata
);
4525 struct netdev_queue_stats stats
;
4527 if (!netdev_get_queue_stats(port
->netdev
, queue_id
, &stats
)) {
4528 put_queue_stats(cbdata
, queue_id
, &stats
);
4530 return OFPERR_OFPQOFC_BAD_QUEUE
;
4537 handle_queue_stats_request(struct ofconn
*ofconn
,
4538 const struct ofp_header
*rq
)
4540 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4541 struct queue_stats_cbdata cbdata
;
4542 struct ofport
*port
;
4544 struct ofputil_queue_stats_request oqsr
;
4546 COVERAGE_INC(ofproto_queue_req
);
4548 ofpmp_init(&cbdata
.replies
, rq
);
4549 cbdata
.now
= time_msec();
4551 error
= ofputil_decode_queue_stats_request(rq
, &oqsr
);
4556 if (oqsr
.port_no
== OFPP_ANY
) {
4557 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
4558 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
4559 if (!handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)) {
4564 port
= ofproto_get_port(ofproto
, oqsr
.port_no
);
4566 ? handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)
4567 : OFPERR_OFPQOFC_BAD_PORT
);
4570 ofconn_send_replies(ofconn
, &cbdata
.replies
);
4572 ofpbuf_list_delete(&cbdata
.replies
);
4579 evict_rules_from_table(struct oftable
*table
)
4580 OVS_REQUIRES(ofproto_mutex
)
4582 enum ofperr error
= 0;
4583 struct rule_collection rules
;
4584 unsigned int count
= table
->n_flows
;
4585 unsigned int max_flows
= table
->max_flows
;
4587 rule_collection_init(&rules
);
4589 while (count
-- > max_flows
) {
4592 if (!choose_rule_to_evict(table
, &rule
)) {
4593 error
= OFPERR_OFPFMFC_TABLE_FULL
;
4596 eviction_group_remove_rule(rule
);
4597 rule_collection_add(&rules
, rule
);
4600 delete_flows__(&rules
, OFPRR_EVICTION
, NULL
);
4606 get_conjunctions(const struct ofputil_flow_mod
*fm
,
4607 struct cls_conjunction
**conjsp
, size_t *n_conjsp
)
4609 struct cls_conjunction
*conjs
= NULL
;
4612 const struct ofpact
*ofpact
;
4613 OFPACT_FOR_EACH (ofpact
, fm
->ofpacts
, fm
->ofpacts_len
) {
4614 if (ofpact
->type
== OFPACT_CONJUNCTION
) {
4616 } else if (ofpact
->type
!= OFPACT_NOTE
) {
4617 /* "conjunction" may appear with "note" actions but not with any
4618 * other type of actions. */
4619 ovs_assert(!n_conjs
);
4626 conjs
= xzalloc(n_conjs
* sizeof *conjs
);
4627 OFPACT_FOR_EACH (ofpact
, fm
->ofpacts
, fm
->ofpacts_len
) {
4628 if (ofpact
->type
== OFPACT_CONJUNCTION
) {
4629 struct ofpact_conjunction
*oc
= ofpact_get_CONJUNCTION(ofpact
);
4630 conjs
[i
].clause
= oc
->clause
;
4631 conjs
[i
].n_clauses
= oc
->n_clauses
;
4632 conjs
[i
].id
= oc
->id
;
4639 *n_conjsp
= n_conjs
;
4642 /* add_flow_init(), add_flow_start(), add_flow_revert(), and add_flow_finish()
4643 * implement OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
4644 * in which no matching flow already exists in the flow table.
4646 * add_flow_init() creates a new flow according to 'fm' and stores it to 'ofm'
4647 * for later reference. If the flow replaces other flow, it will be updated to
4648 * match modify semantics later by add_flow_start() (by calling
4649 * replace_rule_start()).
4651 * Returns 0 on success, or an OpenFlow error code on failure.
4653 * On successful return the caller must complete the operation by calling
4654 * add_flow_start(), and if that succeeds, then either add_flow_finish(), or
4655 * add_flow_revert() if the operation needs to be reverted due to a later
4659 add_flow_init(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4660 const struct ofputil_flow_mod
*fm
)
4661 OVS_EXCLUDED(ofproto_mutex
)
4663 struct oftable
*table
;
4668 if (!check_table_id(ofproto
, fm
->table_id
)) {
4669 return OFPERR_OFPBRC_BAD_TABLE_ID
;
4673 if (fm
->table_id
== 0xff) {
4674 if (ofproto
->ofproto_class
->rule_choose_table
) {
4675 error
= ofproto
->ofproto_class
->rule_choose_table(ofproto
,
4681 ovs_assert(table_id
< ofproto
->n_tables
);
4685 } else if (fm
->table_id
< ofproto
->n_tables
) {
4686 table_id
= fm
->table_id
;
4688 return OFPERR_OFPBRC_BAD_TABLE_ID
;
4691 table
= &ofproto
->tables
[table_id
];
4692 if (table
->flags
& OFTABLE_READONLY
4693 && !(fm
->flags
& OFPUTIL_FF_NO_READONLY
)) {
4694 return OFPERR_OFPBRC_EPERM
;
4697 if (!(fm
->flags
& OFPUTIL_FF_HIDDEN_FIELDS
)
4698 && !match_has_default_hidden_fields(&fm
->match
)) {
4699 VLOG_WARN_RL(&rl
, "%s: (add_flow) only internal flows can set "
4700 "non-default values to hidden fields", ofproto
->name
);
4701 return OFPERR_OFPBRC_EPERM
;
4704 cls_rule_init(&cr
, &fm
->match
, fm
->priority
);
4706 /* Allocate new rule. Destroys 'cr'. */
4707 error
= ofproto_rule_create(ofproto
, &cr
, table
- ofproto
->tables
,
4708 fm
->new_cookie
, fm
->idle_timeout
,
4709 fm
->hard_timeout
, fm
->flags
, fm
->importance
,
4710 fm
->ofpacts
, fm
->ofpacts_len
, &ofm
->temp_rule
);
4715 get_conjunctions(fm
, &ofm
->conjs
, &ofm
->n_conjs
);
4720 add_flow_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
4721 OVS_REQUIRES(ofproto_mutex
)
4723 struct rule
*old_rule
= NULL
;
4724 struct rule
*new_rule
= ofm
->temp_rule
;
4725 const struct rule_actions
*actions
= rule_get_actions(new_rule
);
4726 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
4729 /* Must check actions while holding ofproto_mutex to avoid a race. */
4730 error
= ofproto_check_ofpacts(ofproto
, actions
->ofpacts
,
4731 actions
->ofpacts_len
);
4736 /* Check for the existence of an identical rule.
4737 * This will not return rules earlier marked for removal. */
4738 old_rule
= rule_from_cls_rule(classifier_find_rule_exactly(&table
->cls
,
4742 /* Check for overlap, if requested. */
4743 if (new_rule
->flags
& OFPUTIL_FF_CHECK_OVERLAP
4744 && classifier_rule_overlaps(&table
->cls
, &new_rule
->cr
,
4746 return OFPERR_OFPFMFC_OVERLAP
;
4749 /* If necessary, evict an existing rule to clear out space. */
4750 if (table
->n_flows
>= table
->max_flows
) {
4751 if (!choose_rule_to_evict(table
, &old_rule
)) {
4752 return OFPERR_OFPFMFC_TABLE_FULL
;
4754 eviction_group_remove_rule(old_rule
);
4755 /* Marks 'old_rule' as an evicted rule rather than replaced rule.
4757 old_rule
->removed_reason
= OFPRR_EVICTION
;
4760 ofm
->modify_cookie
= true;
4764 rule_collection_add(&ofm
->old_rules
, old_rule
);
4766 /* Take ownership of the temp_rule. */
4767 rule_collection_stub(&ofm
->new_rules
)[0] = new_rule
;
4768 ofm
->temp_rule
= NULL
;
4770 replace_rule_start(ofproto
, ofm
, old_rule
, new_rule
);
4774 /* Revert the effects of add_flow_start(). */
4776 add_flow_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
4777 OVS_REQUIRES(ofproto_mutex
)
4779 struct rule
*old_rule
= rule_collection_n(&ofm
->old_rules
)
4780 ? rule_collection_stub(&ofm
->old_rules
)[0] : NULL
;
4781 struct rule
*new_rule
= rule_collection_stub(&ofm
->new_rules
)[0];
4783 replace_rule_revert(ofproto
, old_rule
, new_rule
);
4786 /* To be called after version bump. */
4788 add_flow_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4789 const struct openflow_mod_requester
*req
)
4790 OVS_REQUIRES(ofproto_mutex
)
4792 struct rule
*old_rule
= rule_collection_n(&ofm
->old_rules
)
4793 ? rule_collection_stub(&ofm
->old_rules
)[0] : NULL
;
4794 struct rule
*new_rule
= rule_collection_stub(&ofm
->new_rules
)[0];
4795 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
4797 replace_rule_finish(ofproto
, ofm
, req
, old_rule
, new_rule
, &dead_cookies
);
4798 learned_cookies_flush(ofproto
, &dead_cookies
);
4801 ovsrcu_postpone(remove_rule_rcu
, old_rule
);
4803 ofmonitor_report(ofproto
->connmgr
, new_rule
, NXFME_ADDED
, 0,
4804 req
? req
->ofconn
: NULL
,
4805 req
? req
->request
->xid
: 0, NULL
);
4807 /* Send Vacancy Events for OF1.4+. */
4808 send_table_status(ofproto
, new_rule
->table_id
);
4811 send_buffered_packet(req
, ofm
->buffer_id
, new_rule
);
4814 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4816 /* Create a new rule. Note that the rule is NOT inserted into a any data
4817 * structures yet. Takes ownership of 'cr'. */
4819 ofproto_rule_create(struct ofproto
*ofproto
, struct cls_rule
*cr
,
4820 uint8_t table_id
, ovs_be64 new_cookie
,
4821 uint16_t idle_timeout
, uint16_t hard_timeout
,
4822 enum ofputil_flow_mod_flags flags
, uint16_t importance
,
4823 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
4824 struct rule
**new_rule
)
4825 OVS_NO_THREAD_SAFETY_ANALYSIS
4830 /* Allocate new rule. */
4831 rule
= ofproto
->ofproto_class
->rule_alloc();
4833 cls_rule_destroy(cr
);
4834 VLOG_WARN_RL(&rl
, "%s: failed to allocate a rule.", ofproto
->name
);
4835 return OFPERR_OFPFMFC_UNKNOWN
;
4838 /* Initialize base state. */
4839 *CONST_CAST(struct ofproto
**, &rule
->ofproto
) = ofproto
;
4840 cls_rule_move(CONST_CAST(struct cls_rule
*, &rule
->cr
), cr
);
4841 ovs_refcount_init(&rule
->ref_count
);
4843 ovs_mutex_init(&rule
->mutex
);
4844 ovs_mutex_lock(&rule
->mutex
);
4845 rule
->flow_cookie
= new_cookie
;
4846 rule
->created
= rule
->modified
= time_msec();
4847 rule
->idle_timeout
= idle_timeout
;
4848 rule
->hard_timeout
= hard_timeout
;
4849 *CONST_CAST(uint16_t *, &rule
->importance
) = importance
;
4850 rule
->removed_reason
= OVS_OFPRR_NONE
;
4852 *CONST_CAST(uint8_t *, &rule
->table_id
) = table_id
;
4853 rule
->flags
= flags
& OFPUTIL_FF_STATE
;
4855 *CONST_CAST(const struct rule_actions
**, &rule
->actions
)
4856 = rule_actions_create(ofpacts
, ofpacts_len
);
4858 ovs_list_init(&rule
->meter_list_node
);
4859 rule
->eviction_group
= NULL
;
4860 rule
->monitor_flags
= 0;
4861 rule
->add_seqno
= 0;
4862 rule
->modify_seqno
= 0;
4863 ovs_list_init(&rule
->expirable
);
4864 ovs_mutex_unlock(&rule
->mutex
);
4866 /* Construct rule, initializing derived state. */
4867 error
= ofproto
->ofproto_class
->rule_construct(rule
);
4869 ofproto_rule_destroy__(rule
);
4873 rule
->removed
= true; /* Not yet in ofproto data structures. */
4880 replace_rule_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4881 struct rule
*old_rule
, struct rule
*new_rule
)
4883 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
4885 /* 'old_rule' may be either an evicted rule or replaced rule. */
4887 /* Copy values from old rule for modify semantics. */
4888 if (old_rule
->removed_reason
!= OFPRR_EVICTION
) {
4889 bool change_cookie
= (ofm
->modify_cookie
4890 && new_rule
->flow_cookie
!= OVS_BE64_MAX
4891 && new_rule
->flow_cookie
!= old_rule
->flow_cookie
);
4893 ovs_mutex_lock(&new_rule
->mutex
);
4894 ovs_mutex_lock(&old_rule
->mutex
);
4895 if (ofm
->command
!= OFPFC_ADD
) {
4896 new_rule
->idle_timeout
= old_rule
->idle_timeout
;
4897 new_rule
->hard_timeout
= old_rule
->hard_timeout
;
4898 *CONST_CAST(uint16_t *, &new_rule
->importance
) = old_rule
->importance
;
4899 new_rule
->flags
= old_rule
->flags
;
4900 new_rule
->created
= old_rule
->created
;
4902 if (!change_cookie
) {
4903 new_rule
->flow_cookie
= old_rule
->flow_cookie
;
4905 ovs_mutex_unlock(&old_rule
->mutex
);
4906 ovs_mutex_unlock(&new_rule
->mutex
);
4909 /* Mark the old rule for removal in the next version. */
4910 cls_rule_make_invisible_in_version(&old_rule
->cr
, ofm
->version
);
4912 /* Remove the old rule from data structures. */
4913 ofproto_rule_remove__(ofproto
, old_rule
);
4917 /* Insert flow to ofproto data structures, so that later flow_mods may
4918 * relate to it. This is reversible, in case later errors require this to
4920 ofproto_rule_insert__(ofproto
, new_rule
);
4921 /* Make the new rule visible for classifier lookups only from the next
4923 classifier_insert(&table
->cls
, &new_rule
->cr
, ofm
->version
, ofm
->conjs
,
4928 replace_rule_revert(struct ofproto
*ofproto
,
4929 struct rule
*old_rule
, struct rule
*new_rule
)
4931 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
4934 if (old_rule
->removed_reason
== OFPRR_EVICTION
) {
4935 /* Revert the eviction. */
4936 eviction_group_add_rule(old_rule
);
4939 /* Restore the old rule to data structures. */
4940 ofproto_rule_insert__(ofproto
, old_rule
);
4942 /* Restore the original visibility of the old rule. */
4943 cls_rule_restore_visibility(&old_rule
->cr
);
4945 /* Restore table's rule count. */
4949 /* Remove the new rule immediately. It was never visible to lookups. */
4950 if (!classifier_remove(&table
->cls
, &new_rule
->cr
)) {
4953 ofproto_rule_remove__(ofproto
, new_rule
);
4954 /* The rule was not inserted to the ofproto provider, so we can
4955 * release it without deleting it from the ofproto provider. */
4956 ofproto_rule_unref(new_rule
);
4959 /* Adds the 'new_rule', replacing the 'old_rule'. */
4961 replace_rule_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4962 const struct openflow_mod_requester
*req
,
4963 struct rule
*old_rule
, struct rule
*new_rule
,
4964 struct ovs_list
*dead_cookies
)
4965 OVS_REQUIRES(ofproto_mutex
)
4967 bool forward_counts
= !(new_rule
->flags
& OFPUTIL_FF_RESET_COUNTS
);
4968 struct rule
*replaced_rule
;
4970 replaced_rule
= (old_rule
&& old_rule
->removed_reason
!= OFPRR_EVICTION
)
4973 /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule'
4974 * is a duplicate rule the 'new_rule' is replacing. The provider should
4975 * link the packet and byte counts from the old rule to the new one if
4976 * 'forward_counts' is 'true'. The 'replaced_rule' will be deleted right
4977 * after this call. */
4978 ofproto
->ofproto_class
->rule_insert(new_rule
, replaced_rule
,
4980 learned_cookies_inc(ofproto
, rule_get_actions(new_rule
));
4983 const struct rule_actions
*old_actions
= rule_get_actions(old_rule
);
4984 const struct rule_actions
*new_actions
= rule_get_actions(new_rule
);
4986 learned_cookies_dec(ofproto
, old_actions
, dead_cookies
);
4988 if (replaced_rule
) {
4989 enum nx_flow_update_event event
= ofm
->command
== OFPFC_ADD
4990 ? NXFME_ADDED
: NXFME_MODIFIED
;
4992 bool changed_cookie
= (new_rule
->flow_cookie
4993 != old_rule
->flow_cookie
);
4995 bool changed_actions
= !ofpacts_equal(new_actions
->ofpacts
,
4996 new_actions
->ofpacts_len
,
4997 old_actions
->ofpacts
,
4998 old_actions
->ofpacts_len
);
5000 if (event
!= NXFME_MODIFIED
|| changed_actions
5001 || changed_cookie
) {
5002 ofmonitor_report(ofproto
->connmgr
, new_rule
, event
, 0,
5003 req
? req
->ofconn
: NULL
,
5004 req
? req
->request
->xid
: 0,
5005 changed_actions
? old_actions
: NULL
);
5008 /* XXX: This is slight duplication with delete_flows_finish__() */
5009 ofmonitor_report(ofproto
->connmgr
, old_rule
, NXFME_DELETED
,
5011 req
? req
->ofconn
: NULL
,
5012 req
? req
->request
->xid
: 0, NULL
);
5018 modify_flows_start__(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5019 OVS_REQUIRES(ofproto_mutex
)
5021 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5022 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5025 if (rule_collection_n(old_rules
) > 0) {
5026 /* Create a new 'modified' rule for each old rule. */
5027 struct rule
*old_rule
, *new_rule
;
5028 const struct rule_actions
*actions
= rule_get_actions(ofm
->temp_rule
);
5030 /* Must check actions while holding ofproto_mutex to avoid a race. */
5031 error
= ofproto_check_ofpacts(ofproto
, actions
->ofpacts
,
5032 actions
->ofpacts_len
);
5037 /* Use the temp rule as the first new rule, and as the template for
5039 struct rule
*temp
= ofm
->temp_rule
;
5040 ofm
->temp_rule
= NULL
; /* We consume the template. */
5043 RULE_COLLECTION_FOR_EACH (old_rule
, old_rules
) {
5045 /* The template rule's match is possibly a loose one, so it
5046 * must be replaced with the old rule's match so that the new
5047 * rule actually replaces the old one. */
5048 cls_rule_destroy(CONST_CAST(struct cls_rule
*, &temp
->cr
));
5049 cls_rule_clone(CONST_CAST(struct cls_rule
*, &temp
->cr
),
5051 *CONST_CAST(uint8_t *, &temp
->table_id
) = old_rule
->table_id
;
5052 rule_collection_add(new_rules
, temp
);
5056 cls_rule_clone(&cr
, &old_rule
->cr
);
5057 error
= ofproto_rule_create(ofproto
, &cr
, old_rule
->table_id
,
5060 temp
->hard_timeout
, temp
->flags
,
5062 temp
->actions
->ofpacts
,
5063 temp
->actions
->ofpacts_len
,
5066 rule_collection_add(new_rules
, new_rule
);
5068 rule_collection_unref(new_rules
);
5069 rule_collection_destroy(new_rules
);
5074 ovs_assert(rule_collection_n(new_rules
)
5075 == rule_collection_n(old_rules
));
5077 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5078 replace_rule_start(ofproto
, ofm
, old_rule
, new_rule
);
5080 } else if (ofm
->modify_may_add_flow
) {
5081 /* No match, add a new flow, consumes 'temp'. */
5082 error
= add_flow_start(ofproto
, ofm
);
5083 new_rules
->collection
.n
= 1;
5092 modify_flows_init_loose(struct ofproto
*ofproto
,
5093 struct ofproto_flow_mod
*ofm
,
5094 const struct ofputil_flow_mod
*fm
)
5095 OVS_EXCLUDED(ofproto_mutex
)
5097 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, 0,
5098 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
,
5100 rule_criteria_require_rw(&ofm
->criteria
,
5101 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5102 /* Must create a new flow in advance for the case that no matches are
5103 * found. Also used for template for multiple modified flows. */
5104 add_flow_init(ofproto
, ofm
, fm
);
5109 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5112 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5115 modify_flows_start_loose(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5116 OVS_REQUIRES(ofproto_mutex
)
5118 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5121 error
= collect_rules_loose(ofproto
, &ofm
->criteria
, old_rules
);
5124 error
= modify_flows_start__(ofproto
, ofm
);
5128 rule_collection_destroy(old_rules
);
5135 modify_flows_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5136 OVS_REQUIRES(ofproto_mutex
)
5138 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5139 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5141 /* Old rules were not changed yet, only need to revert new rules. */
5142 if (rule_collection_n(old_rules
) > 0) {
5143 struct rule
*old_rule
, *new_rule
;
5144 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5145 replace_rule_revert(ofproto
, old_rule
, new_rule
);
5147 rule_collection_destroy(new_rules
);
5148 rule_collection_destroy(old_rules
);
5153 modify_flows_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5154 const struct openflow_mod_requester
*req
)
5155 OVS_REQUIRES(ofproto_mutex
)
5157 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5158 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5160 if (rule_collection_n(old_rules
) == 0
5161 && rule_collection_n(new_rules
) == 1) {
5162 add_flow_finish(ofproto
, ofm
, req
);
5163 } else if (rule_collection_n(old_rules
) > 0) {
5164 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
5166 ovs_assert(rule_collection_n(new_rules
)
5167 == rule_collection_n(old_rules
));
5169 struct rule
*old_rule
, *new_rule
;
5170 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5171 replace_rule_finish(ofproto
, ofm
, req
, old_rule
, new_rule
,
5174 learned_cookies_flush(ofproto
, &dead_cookies
);
5175 remove_rules_postponed(old_rules
);
5177 send_buffered_packet(req
, ofm
->buffer_id
,
5178 rule_collection_rules(new_rules
)[0]);
5179 rule_collection_destroy(new_rules
);
5184 modify_flow_init_strict(struct ofproto
*ofproto OVS_UNUSED
,
5185 struct ofproto_flow_mod
*ofm
,
5186 const struct ofputil_flow_mod
*fm
)
5187 OVS_EXCLUDED(ofproto_mutex
)
5189 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
5190 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
,
5192 rule_criteria_require_rw(&ofm
->criteria
,
5193 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5194 /* Must create a new flow in advance for the case that no matches are
5195 * found. Also used for template for multiple modified flows. */
5196 add_flow_init(ofproto
, ofm
, fm
);
5201 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
5202 * code on failure. */
5204 modify_flow_start_strict(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5205 OVS_REQUIRES(ofproto_mutex
)
5207 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5210 error
= collect_rules_strict(ofproto
, &ofm
->criteria
, old_rules
);
5213 /* collect_rules_strict() can return max 1 rule. */
5214 error
= modify_flows_start__(ofproto
, ofm
);
5220 /* OFPFC_DELETE implementation. */
5223 delete_flows_start__(struct ofproto
*ofproto
, ovs_version_t version
,
5224 const struct rule_collection
*rules
)
5225 OVS_REQUIRES(ofproto_mutex
)
5229 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5230 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5233 cls_rule_make_invisible_in_version(&rule
->cr
, version
);
5235 /* Remove rule from ofproto data structures. */
5236 ofproto_rule_remove__(ofproto
, rule
);
5241 delete_flows_revert__(struct ofproto
*ofproto
,
5242 const struct rule_collection
*rules
)
5243 OVS_REQUIRES(ofproto_mutex
)
5247 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5248 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5250 /* Add rule back to ofproto data structures. */
5251 ofproto_rule_insert__(ofproto
, rule
);
5253 /* Restore table's rule count. */
5256 /* Restore the original visibility of the rule. */
5257 cls_rule_restore_visibility(&rule
->cr
);
5262 delete_flows_finish__(struct ofproto
*ofproto
,
5263 struct rule_collection
*rules
,
5264 enum ofp_flow_removed_reason reason
,
5265 const struct openflow_mod_requester
*req
)
5266 OVS_REQUIRES(ofproto_mutex
)
5268 if (rule_collection_n(rules
)) {
5269 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
5272 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5273 /* This value will be used to send the flow removed message right
5274 * before the rule is actually destroyed. */
5275 rule
->removed_reason
= reason
;
5277 ofmonitor_report(ofproto
->connmgr
, rule
, NXFME_DELETED
, reason
,
5278 req
? req
->ofconn
: NULL
,
5279 req
? req
->request
->xid
: 0, NULL
);
5281 /* Send Vacancy Event for OF1.4+. */
5282 send_table_status(ofproto
, rule
->table_id
);
5284 learned_cookies_dec(ofproto
, rule_get_actions(rule
),
5287 remove_rules_postponed(rules
);
5289 learned_cookies_flush(ofproto
, &dead_cookies
);
5293 /* Deletes the rules listed in 'rules'.
5294 * The deleted rules will become invisible to the lookups in the next version.
5295 * Destroys 'rules'. */
5297 delete_flows__(struct rule_collection
*rules
,
5298 enum ofp_flow_removed_reason reason
,
5299 const struct openflow_mod_requester
*req
)
5300 OVS_REQUIRES(ofproto_mutex
)
5302 if (rule_collection_n(rules
)) {
5303 struct ofproto
*ofproto
= rule_collection_rules(rules
)[0]->ofproto
;
5305 delete_flows_start__(ofproto
, ofproto
->tables_version
+ 1, rules
);
5306 ofproto_bump_tables_version(ofproto
);
5307 delete_flows_finish__(ofproto
, rules
, reason
, req
);
5308 ofmonitor_flush(ofproto
->connmgr
);
5313 delete_flows_init_loose(struct ofproto
*ofproto OVS_UNUSED
,
5314 struct ofproto_flow_mod
*ofm
,
5315 const struct ofputil_flow_mod
*fm
)
5316 OVS_EXCLUDED(ofproto_mutex
)
5318 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, 0,
5319 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
,
5320 fm
->out_port
, fm
->out_group
);
5321 rule_criteria_require_rw(&ofm
->criteria
,
5322 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5326 /* Implements OFPFC_DELETE. */
5328 delete_flows_start_loose(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5329 OVS_REQUIRES(ofproto_mutex
)
5331 struct rule_collection
*rules
= &ofm
->old_rules
;
5334 error
= collect_rules_loose(ofproto
, &ofm
->criteria
, rules
);
5337 delete_flows_start__(ofproto
, ofm
->version
, rules
);
5344 delete_flows_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5345 OVS_REQUIRES(ofproto_mutex
)
5347 delete_flows_revert__(ofproto
, &ofm
->old_rules
);
5351 delete_flows_finish(struct ofproto
*ofproto
,
5352 struct ofproto_flow_mod
*ofm
,
5353 const struct openflow_mod_requester
*req
)
5354 OVS_REQUIRES(ofproto_mutex
)
5356 delete_flows_finish__(ofproto
, &ofm
->old_rules
, OFPRR_DELETE
, req
);
5360 delete_flows_init_strict(struct ofproto
*ofproto OVS_UNUSED
,
5361 struct ofproto_flow_mod
*ofm
,
5362 const struct ofputil_flow_mod
*fm
)
5363 OVS_EXCLUDED(ofproto_mutex
)
5365 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
5366 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
,
5367 fm
->out_port
, fm
->out_group
);
5368 rule_criteria_require_rw(&ofm
->criteria
,
5369 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5373 /* Implements OFPFC_DELETE_STRICT. */
5375 delete_flow_start_strict(struct ofproto
*ofproto
,
5376 struct ofproto_flow_mod
*ofm
)
5377 OVS_REQUIRES(ofproto_mutex
)
5379 struct rule_collection
*rules
= &ofm
->old_rules
;
5382 error
= collect_rules_strict(ofproto
, &ofm
->criteria
, rules
);
5385 delete_flows_start__(ofproto
, ofm
->version
, rules
);
5391 /* This may only be called by rule_destroy_cb()! */
5393 ofproto_rule_send_removed(struct rule
*rule
)
5394 OVS_EXCLUDED(ofproto_mutex
)
5396 struct ofputil_flow_removed fr
;
5399 minimatch_expand(&rule
->cr
.match
, &fr
.match
);
5400 fr
.priority
= rule
->cr
.priority
;
5402 ovs_mutex_lock(&ofproto_mutex
);
5403 fr
.cookie
= rule
->flow_cookie
;
5404 fr
.reason
= rule
->removed_reason
;
5405 fr
.table_id
= rule
->table_id
;
5406 calc_duration(rule
->created
, time_msec(),
5407 &fr
.duration_sec
, &fr
.duration_nsec
);
5408 ovs_mutex_lock(&rule
->mutex
);
5409 fr
.idle_timeout
= rule
->idle_timeout
;
5410 fr
.hard_timeout
= rule
->hard_timeout
;
5411 ovs_mutex_unlock(&rule
->mutex
);
5412 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &fr
.packet_count
,
5413 &fr
.byte_count
, &used
);
5414 connmgr_send_flow_removed(rule
->ofproto
->connmgr
, &fr
);
5415 ovs_mutex_unlock(&ofproto_mutex
);
5418 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
5419 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
5422 * ofproto implementation ->run() functions should use this function to expire
5423 * OpenFlow flows. */
5425 ofproto_rule_expire(struct rule
*rule
, uint8_t reason
)
5426 OVS_REQUIRES(ofproto_mutex
)
5428 struct rule_collection rules
;
5430 rule_collection_init(&rules
);
5431 rule_collection_add(&rules
, rule
);
5432 delete_flows__(&rules
, reason
, NULL
);
5435 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5436 * means "infinite". */
5438 reduce_timeout(uint16_t max
, uint16_t *timeout
)
5440 if (max
&& (!*timeout
|| *timeout
> max
)) {
5445 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
5446 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
5447 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
5449 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
5451 ofproto_rule_reduce_timeouts(struct rule
*rule
,
5452 uint16_t idle_timeout
, uint16_t hard_timeout
)
5453 OVS_EXCLUDED(ofproto_mutex
, rule
->mutex
)
5455 if (!idle_timeout
&& !hard_timeout
) {
5459 ovs_mutex_lock(&ofproto_mutex
);
5460 if (ovs_list_is_empty(&rule
->expirable
)) {
5461 ovs_list_insert(&rule
->ofproto
->expirable
, &rule
->expirable
);
5463 ovs_mutex_unlock(&ofproto_mutex
);
5465 ovs_mutex_lock(&rule
->mutex
);
5466 reduce_timeout(idle_timeout
, &rule
->idle_timeout
);
5467 reduce_timeout(hard_timeout
, &rule
->hard_timeout
);
5468 ovs_mutex_unlock(&rule
->mutex
);
5472 handle_flow_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5473 OVS_EXCLUDED(ofproto_mutex
)
5475 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5476 struct ofputil_flow_mod fm
;
5477 uint64_t ofpacts_stub
[1024 / 8];
5478 struct ofpbuf ofpacts
;
5481 error
= reject_slave_controller(ofconn
);
5486 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
5487 error
= ofputil_decode_flow_mod(&fm
, oh
, ofconn_get_protocol(ofconn
),
5489 u16_to_ofp(ofproto
->max_ports
),
5492 struct openflow_mod_requester req
= { ofconn
, oh
};
5493 error
= handle_flow_mod__(ofproto
, &fm
, &req
);
5496 ofpbuf_uninit(&ofpacts
);
5501 handle_flow_mod__(struct ofproto
*ofproto
, const struct ofputil_flow_mod
*fm
,
5502 const struct openflow_mod_requester
*req
)
5503 OVS_EXCLUDED(ofproto_mutex
)
5505 struct ofproto_flow_mod ofm
;
5508 error
= ofproto_flow_mod_init(ofproto
, &ofm
, fm
);
5513 ovs_mutex_lock(&ofproto_mutex
);
5514 ofm
.version
= ofproto
->tables_version
+ 1;
5515 error
= ofproto_flow_mod_start(ofproto
, &ofm
);
5517 ofproto_bump_tables_version(ofproto
);
5518 ofproto_flow_mod_finish(ofproto
, &ofm
, req
);
5520 ofmonitor_flush(ofproto
->connmgr
);
5521 ovs_mutex_unlock(&ofproto_mutex
);
5523 run_rule_executes(ofproto
);
5528 handle_role_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5530 struct ofputil_role_request request
;
5531 struct ofputil_role_request reply
;
5535 error
= ofputil_decode_role_message(oh
, &request
);
5540 if (request
.role
!= OFPCR12_ROLE_NOCHANGE
) {
5541 if (request
.role
!= OFPCR12_ROLE_EQUAL
5542 && request
.have_generation_id
5543 && !ofconn_set_master_election_id(ofconn
, request
.generation_id
)) {
5544 return OFPERR_OFPRRFC_STALE
;
5547 ofconn_set_role(ofconn
, request
.role
);
5550 reply
.role
= ofconn_get_role(ofconn
);
5551 reply
.have_generation_id
= ofconn_get_master_election_id(
5552 ofconn
, &reply
.generation_id
);
5553 buf
= ofputil_encode_role_reply(oh
, &reply
);
5554 ofconn_send_reply(ofconn
, buf
);
5560 handle_nxt_flow_mod_table_id(struct ofconn
*ofconn
,
5561 const struct ofp_header
*oh
)
5563 const struct nx_flow_mod_table_id
*msg
= ofpmsg_body(oh
);
5564 enum ofputil_protocol cur
, next
;
5566 cur
= ofconn_get_protocol(ofconn
);
5567 next
= ofputil_protocol_set_tid(cur
, msg
->set
!= 0);
5568 ofconn_set_protocol(ofconn
, next
);
5574 handle_nxt_set_flow_format(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5576 const struct nx_set_flow_format
*msg
= ofpmsg_body(oh
);
5577 enum ofputil_protocol cur
, next
;
5578 enum ofputil_protocol next_base
;
5580 next_base
= ofputil_nx_flow_format_to_protocol(ntohl(msg
->format
));
5582 return OFPERR_OFPBRC_EPERM
;
5585 cur
= ofconn_get_protocol(ofconn
);
5586 next
= ofputil_protocol_set_base(cur
, next_base
);
5587 ofconn_set_protocol(ofconn
, next
);
5593 handle_nxt_set_packet_in_format(struct ofconn
*ofconn
,
5594 const struct ofp_header
*oh
)
5596 const struct nx_set_packet_in_format
*msg
= ofpmsg_body(oh
);
5599 format
= ntohl(msg
->format
);
5600 if (!ofputil_packet_in_format_is_valid(format
)) {
5601 return OFPERR_OFPBRC_EPERM
;
5604 ofconn_set_packet_in_format(ofconn
, format
);
5609 handle_nxt_set_async_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5611 struct ofputil_async_cfg basis
= ofconn_get_async_config(ofconn
);
5612 struct ofputil_async_cfg ac
;
5615 error
= ofputil_decode_set_async_config(oh
, false, &basis
, &ac
);
5620 ofconn_set_async_config(ofconn
, &ac
);
5621 if (ofconn_get_type(ofconn
) == OFCONN_SERVICE
&&
5622 !ofconn_get_miss_send_len(ofconn
)) {
5623 ofconn_set_miss_send_len(ofconn
, OFP_DEFAULT_MISS_SEND_LEN
);
5630 handle_nxt_get_async_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5632 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
5633 ofconn_send_reply(ofconn
, ofputil_encode_get_async_reply(oh
, &ac
));
5639 handle_nxt_set_controller_id(struct ofconn
*ofconn
,
5640 const struct ofp_header
*oh
)
5642 const struct nx_controller_id
*nci
= ofpmsg_body(oh
);
5644 if (!is_all_zeros(nci
->zero
, sizeof nci
->zero
)) {
5645 return OFPERR_NXBRC_MUST_BE_ZERO
;
5648 ofconn_set_controller_id(ofconn
, ntohs(nci
->controller_id
));
5653 handle_barrier_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5657 buf
= ofpraw_alloc_reply((oh
->version
== OFP10_VERSION
5658 ? OFPRAW_OFPT10_BARRIER_REPLY
5659 : OFPRAW_OFPT11_BARRIER_REPLY
), oh
, 0);
5660 ofconn_send_reply(ofconn
, buf
);
5665 ofproto_compose_flow_refresh_update(const struct rule
*rule
,
5666 enum nx_flow_monitor_flags flags
,
5667 struct ovs_list
*msgs
)
5668 OVS_REQUIRES(ofproto_mutex
)
5670 const struct rule_actions
*actions
;
5671 struct ofputil_flow_update fu
;
5674 fu
.event
= (flags
& (NXFMF_INITIAL
| NXFMF_ADD
)
5675 ? NXFME_ADDED
: NXFME_MODIFIED
);
5677 ovs_mutex_lock(&rule
->mutex
);
5678 fu
.idle_timeout
= rule
->idle_timeout
;
5679 fu
.hard_timeout
= rule
->hard_timeout
;
5680 ovs_mutex_unlock(&rule
->mutex
);
5681 fu
.table_id
= rule
->table_id
;
5682 fu
.cookie
= rule
->flow_cookie
;
5683 minimatch_expand(&rule
->cr
.match
, &match
);
5685 fu
.priority
= rule
->cr
.priority
;
5687 actions
= flags
& NXFMF_ACTIONS
? rule_get_actions(rule
) : NULL
;
5688 fu
.ofpacts
= actions
? actions
->ofpacts
: NULL
;
5689 fu
.ofpacts_len
= actions
? actions
->ofpacts_len
: 0;
5691 if (ovs_list_is_empty(msgs
)) {
5692 ofputil_start_flow_update(msgs
);
5694 ofputil_append_flow_update(&fu
, msgs
);
5698 ofmonitor_compose_refresh_updates(struct rule_collection
*rules
,
5699 struct ovs_list
*msgs
)
5700 OVS_REQUIRES(ofproto_mutex
)
5704 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5705 enum nx_flow_monitor_flags flags
= rule
->monitor_flags
;
5706 rule
->monitor_flags
= 0;
5708 ofproto_compose_flow_refresh_update(rule
, flags
, msgs
);
5713 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor
*m
,
5714 struct rule
*rule
, uint64_t seqno
,
5715 struct rule_collection
*rules
)
5716 OVS_REQUIRES(ofproto_mutex
)
5718 enum nx_flow_monitor_flags update
;
5720 if (rule_is_hidden(rule
)) {
5724 if (!ofproto_rule_has_out_port(rule
, m
->out_port
)) {
5729 if (rule
->add_seqno
> seqno
) {
5730 update
= NXFMF_ADD
| NXFMF_MODIFY
;
5731 } else if (rule
->modify_seqno
> seqno
) {
5732 update
= NXFMF_MODIFY
;
5737 if (!(m
->flags
& update
)) {
5741 update
= NXFMF_INITIAL
;
5744 if (!rule
->monitor_flags
) {
5745 rule_collection_add(rules
, rule
);
5747 rule
->monitor_flags
|= update
| (m
->flags
& NXFMF_ACTIONS
);
5751 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor
*m
,
5753 struct rule_collection
*rules
)
5754 OVS_REQUIRES(ofproto_mutex
)
5756 const struct ofproto
*ofproto
= ofconn_get_ofproto(m
->ofconn
);
5757 const struct oftable
*table
;
5758 struct cls_rule target
;
5760 cls_rule_init_from_minimatch(&target
, &m
->match
, 0);
5761 FOR_EACH_MATCHING_TABLE (table
, m
->table_id
, ofproto
) {
5764 CLS_FOR_EACH_TARGET (rule
, cr
, &table
->cls
, &target
, OVS_VERSION_MAX
) {
5765 ofproto_collect_ofmonitor_refresh_rule(m
, rule
, seqno
, rules
);
5768 cls_rule_destroy(&target
);
5772 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor
*m
,
5773 struct rule_collection
*rules
)
5774 OVS_REQUIRES(ofproto_mutex
)
5776 if (m
->flags
& NXFMF_INITIAL
) {
5777 ofproto_collect_ofmonitor_refresh_rules(m
, 0, rules
);
5782 ofmonitor_collect_resume_rules(struct ofmonitor
*m
,
5783 uint64_t seqno
, struct rule_collection
*rules
)
5784 OVS_REQUIRES(ofproto_mutex
)
5786 ofproto_collect_ofmonitor_refresh_rules(m
, seqno
, rules
);
5790 flow_monitor_delete(struct ofconn
*ofconn
, uint32_t id
)
5791 OVS_REQUIRES(ofproto_mutex
)
5793 struct ofmonitor
*m
;
5796 m
= ofmonitor_lookup(ofconn
, id
);
5798 ofmonitor_destroy(m
);
5801 error
= OFPERR_OFPMOFC_UNKNOWN_MONITOR
;
5808 handle_flow_monitor_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5809 OVS_EXCLUDED(ofproto_mutex
)
5811 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5813 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5815 struct ofmonitor
**monitors
= NULL
;
5816 size_t allocated_monitors
= 0;
5817 size_t n_monitors
= 0;
5821 ovs_mutex_lock(&ofproto_mutex
);
5823 struct ofputil_flow_monitor_request request
;
5824 struct ofmonitor
*m
;
5827 retval
= ofputil_decode_flow_monitor_request(&request
, &b
);
5828 if (retval
== EOF
) {
5830 } else if (retval
) {
5835 if (request
.table_id
!= 0xff
5836 && request
.table_id
>= ofproto
->n_tables
) {
5837 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
5841 error
= ofmonitor_create(&request
, ofconn
, &m
);
5846 if (n_monitors
>= allocated_monitors
) {
5847 monitors
= x2nrealloc(monitors
, &allocated_monitors
,
5850 monitors
[n_monitors
++] = m
;
5853 struct rule_collection rules
;
5854 rule_collection_init(&rules
);
5855 for (size_t i
= 0; i
< n_monitors
; i
++) {
5856 ofproto_collect_ofmonitor_initial_rules(monitors
[i
], &rules
);
5859 struct ovs_list replies
;
5860 ofpmp_init(&replies
, oh
);
5861 ofmonitor_compose_refresh_updates(&rules
, &replies
);
5862 ovs_mutex_unlock(&ofproto_mutex
);
5864 rule_collection_destroy(&rules
);
5866 ofconn_send_replies(ofconn
, &replies
);
5872 for (size_t i
= 0; i
< n_monitors
; i
++) {
5873 ofmonitor_destroy(monitors
[i
]);
5876 ovs_mutex_unlock(&ofproto_mutex
);
5882 handle_flow_monitor_cancel(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5883 OVS_EXCLUDED(ofproto_mutex
)
5888 id
= ofputil_decode_flow_monitor_cancel(oh
);
5890 ovs_mutex_lock(&ofproto_mutex
);
5891 error
= flow_monitor_delete(ofconn
, id
);
5892 ovs_mutex_unlock(&ofproto_mutex
);
5897 /* Meters implementation.
5899 * Meter table entry, indexed by the OpenFlow meter_id.
5900 * 'created' is used to compute the duration for meter stats.
5901 * 'list rules' is needed so that we can delete the dependent rules when the
5902 * meter table entry is deleted.
5903 * 'provider_meter_id' is for the provider's private use.
5906 long long int created
; /* Time created. */
5907 struct ovs_list rules
; /* List of "struct rule_dpif"s. */
5908 ofproto_meter_id provider_meter_id
;
5909 uint16_t flags
; /* Meter flags. */
5910 uint16_t n_bands
; /* Number of meter bands. */
5911 struct ofputil_meter_band
*bands
;
5915 * This is used in instruction validation at flow set-up time,
5916 * as flows may not use non-existing meters.
5917 * Return value of UINT32_MAX signifies an invalid meter.
5920 get_provider_meter_id(const struct ofproto
*ofproto
, uint32_t of_meter_id
)
5922 if (of_meter_id
&& of_meter_id
<= ofproto
->meter_features
.max_meters
) {
5923 const struct meter
*meter
= ofproto
->meters
[of_meter_id
];
5925 return meter
->provider_meter_id
.uint32
;
5931 /* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
5934 meter_insert_rule(struct rule
*rule
)
5936 const struct rule_actions
*a
= rule_get_actions(rule
);
5937 uint32_t meter_id
= ofpacts_get_meter(a
->ofpacts
, a
->ofpacts_len
);
5938 struct meter
*meter
= rule
->ofproto
->meters
[meter_id
];
5940 ovs_list_insert(&meter
->rules
, &rule
->meter_list_node
);
5944 meter_update(struct meter
*meter
, const struct ofputil_meter_config
*config
)
5948 meter
->flags
= config
->flags
;
5949 meter
->n_bands
= config
->n_bands
;
5950 meter
->bands
= xmemdup(config
->bands
,
5951 config
->n_bands
* sizeof *meter
->bands
);
5954 static struct meter
*
5955 meter_create(const struct ofputil_meter_config
*config
,
5956 ofproto_meter_id provider_meter_id
)
5958 struct meter
*meter
;
5960 meter
= xzalloc(sizeof *meter
);
5961 meter
->provider_meter_id
= provider_meter_id
;
5962 meter
->created
= time_msec();
5963 ovs_list_init(&meter
->rules
);
5965 meter_update(meter
, config
);
5971 meter_delete(struct ofproto
*ofproto
, uint32_t first
, uint32_t last
)
5972 OVS_REQUIRES(ofproto_mutex
)
5975 for (mid
= first
; mid
<= last
; ++mid
) {
5976 struct meter
*meter
= ofproto
->meters
[mid
];
5978 ofproto
->meters
[mid
] = NULL
;
5979 ofproto
->ofproto_class
->meter_del(ofproto
,
5980 meter
->provider_meter_id
);
5988 handle_add_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
5990 ofproto_meter_id provider_meter_id
= { UINT32_MAX
};
5991 struct meter
**meterp
= &ofproto
->meters
[mm
->meter
.meter_id
];
5995 return OFPERR_OFPMMFC_METER_EXISTS
;
5998 error
= ofproto
->ofproto_class
->meter_set(ofproto
, &provider_meter_id
,
6001 ovs_assert(provider_meter_id
.uint32
!= UINT32_MAX
);
6002 *meterp
= meter_create(&mm
->meter
, provider_meter_id
);
6008 handle_modify_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
6010 struct meter
*meter
= ofproto
->meters
[mm
->meter
.meter_id
];
6012 uint32_t provider_meter_id
;
6015 return OFPERR_OFPMMFC_UNKNOWN_METER
;
6018 provider_meter_id
= meter
->provider_meter_id
.uint32
;
6019 error
= ofproto
->ofproto_class
->meter_set(ofproto
,
6020 &meter
->provider_meter_id
,
6022 ovs_assert(meter
->provider_meter_id
.uint32
== provider_meter_id
);
6024 meter_update(meter
, &mm
->meter
);
6030 handle_delete_meter(struct ofconn
*ofconn
, struct ofputil_meter_mod
*mm
)
6031 OVS_EXCLUDED(ofproto_mutex
)
6033 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6034 uint32_t meter_id
= mm
->meter
.meter_id
;
6035 struct rule_collection rules
;
6036 enum ofperr error
= 0;
6037 uint32_t first
, last
;
6039 if (meter_id
== OFPM13_ALL
) {
6041 last
= ofproto
->meter_features
.max_meters
;
6043 if (!meter_id
|| meter_id
> ofproto
->meter_features
.max_meters
) {
6046 first
= last
= meter_id
;
6049 /* First delete the rules that use this meter. If any of those rules are
6050 * currently being modified, postpone the whole operation until later. */
6051 rule_collection_init(&rules
);
6052 ovs_mutex_lock(&ofproto_mutex
);
6053 for (meter_id
= first
; meter_id
<= last
; ++meter_id
) {
6054 struct meter
*meter
= ofproto
->meters
[meter_id
];
6055 if (meter
&& !ovs_list_is_empty(&meter
->rules
)) {
6058 LIST_FOR_EACH (rule
, meter_list_node
, &meter
->rules
) {
6059 rule_collection_add(&rules
, rule
);
6063 delete_flows__(&rules
, OFPRR_METER_DELETE
, NULL
);
6065 /* Delete the meters. */
6066 meter_delete(ofproto
, first
, last
);
6068 ovs_mutex_unlock(&ofproto_mutex
);
6074 handle_meter_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6076 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6077 struct ofputil_meter_mod mm
;
6078 uint64_t bands_stub
[256 / 8];
6079 struct ofpbuf bands
;
6083 error
= reject_slave_controller(ofconn
);
6088 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
6090 error
= ofputil_decode_meter_mod(oh
, &mm
, &bands
);
6092 goto exit_free_bands
;
6095 meter_id
= mm
.meter
.meter_id
;
6097 if (mm
.command
!= OFPMC13_DELETE
) {
6098 /* Fails also when meters are not implemented by the provider. */
6099 if (meter_id
== 0 || meter_id
> OFPM13_MAX
) {
6100 error
= OFPERR_OFPMMFC_INVALID_METER
;
6101 goto exit_free_bands
;
6102 } else if (meter_id
> ofproto
->meter_features
.max_meters
) {
6103 error
= OFPERR_OFPMMFC_OUT_OF_METERS
;
6104 goto exit_free_bands
;
6106 if (mm
.meter
.n_bands
> ofproto
->meter_features
.max_bands
) {
6107 error
= OFPERR_OFPMMFC_OUT_OF_BANDS
;
6108 goto exit_free_bands
;
6112 switch (mm
.command
) {
6114 error
= handle_add_meter(ofproto
, &mm
);
6117 case OFPMC13_MODIFY
:
6118 error
= handle_modify_meter(ofproto
, &mm
);
6121 case OFPMC13_DELETE
:
6122 error
= handle_delete_meter(ofconn
, &mm
);
6126 error
= OFPERR_OFPMMFC_BAD_COMMAND
;
6131 struct ofputil_requestforward rf
;
6133 rf
.reason
= OFPRFR_METER_MOD
;
6135 connmgr_send_requestforward(ofproto
->connmgr
, ofconn
, &rf
);
6139 ofpbuf_uninit(&bands
);
6144 handle_meter_features_request(struct ofconn
*ofconn
,
6145 const struct ofp_header
*request
)
6147 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6148 struct ofputil_meter_features features
;
6151 if (ofproto
->ofproto_class
->meter_get_features
) {
6152 ofproto
->ofproto_class
->meter_get_features(ofproto
, &features
);
6154 memset(&features
, 0, sizeof features
);
6156 b
= ofputil_encode_meter_features_reply(&features
, request
);
6158 ofconn_send_reply(ofconn
, b
);
6163 handle_meter_request(struct ofconn
*ofconn
, const struct ofp_header
*request
,
6166 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6167 struct ovs_list replies
;
6168 uint64_t bands_stub
[256 / 8];
6169 struct ofpbuf bands
;
6170 uint32_t meter_id
, first
, last
;
6172 ofputil_decode_meter_request(request
, &meter_id
);
6174 if (meter_id
== OFPM13_ALL
) {
6176 last
= ofproto
->meter_features
.max_meters
;
6178 if (!meter_id
|| meter_id
> ofproto
->meter_features
.max_meters
||
6179 !ofproto
->meters
[meter_id
]) {
6180 return OFPERR_OFPMMFC_UNKNOWN_METER
;
6182 first
= last
= meter_id
;
6185 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
6186 ofpmp_init(&replies
, request
);
6188 for (meter_id
= first
; meter_id
<= last
; ++meter_id
) {
6189 struct meter
*meter
= ofproto
->meters
[meter_id
];
6191 continue; /* Skip non-existing meters. */
6193 if (type
== OFPTYPE_METER_STATS_REQUEST
) {
6194 struct ofputil_meter_stats stats
;
6196 stats
.meter_id
= meter_id
;
6198 /* Provider sets the packet and byte counts, we do the rest. */
6199 stats
.flow_count
= ovs_list_size(&meter
->rules
);
6200 calc_duration(meter
->created
, time_msec(),
6201 &stats
.duration_sec
, &stats
.duration_nsec
);
6202 stats
.n_bands
= meter
->n_bands
;
6203 ofpbuf_clear(&bands
);
6205 = ofpbuf_put_uninit(&bands
,
6206 meter
->n_bands
* sizeof *stats
.bands
);
6208 if (!ofproto
->ofproto_class
->meter_get(ofproto
,
6209 meter
->provider_meter_id
,
6211 ofputil_append_meter_stats(&replies
, &stats
);
6213 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
6214 struct ofputil_meter_config config
;
6216 config
.meter_id
= meter_id
;
6217 config
.flags
= meter
->flags
;
6218 config
.n_bands
= meter
->n_bands
;
6219 config
.bands
= meter
->bands
;
6220 ofputil_append_meter_config(&replies
, &config
);
6224 ofconn_send_replies(ofconn
, &replies
);
6225 ofpbuf_uninit(&bands
);
6229 /* Returned group is RCU protected. */
6230 static struct ofgroup
*
6231 ofproto_group_lookup__(const struct ofproto
*ofproto
, uint32_t group_id
,
6232 ovs_version_t version
)
6234 struct ofgroup
*group
;
6236 CMAP_FOR_EACH_WITH_HASH (group
, cmap_node
, hash_int(group_id
, 0),
6238 if (group
->group_id
== group_id
6239 && versions_visible_in_version(&group
->versions
, version
)) {
6247 /* If the group exists, this function increments the groups's reference count.
6249 * Make sure to call ofproto_group_unref() after no longer needing to maintain
6250 * a reference to the group. */
6252 ofproto_group_lookup(const struct ofproto
*ofproto
, uint32_t group_id
,
6253 ovs_version_t version
, bool take_ref
)
6255 struct ofgroup
*group
;
6257 group
= ofproto_group_lookup__(ofproto
, group_id
, version
);
6258 if (group
&& take_ref
) {
6259 /* Not holding a lock, so it is possible that another thread releases
6260 * the last reference just before we manage to get one. */
6261 return ofproto_group_try_ref(group
) ? group
: NULL
;
6266 /* Caller should hold 'ofproto_mutex' if it is important that the
6267 * group is not removed by someone else. */
6269 ofproto_group_exists(const struct ofproto
*ofproto
, uint32_t group_id
)
6271 return ofproto_group_lookup__(ofproto
, group_id
, OVS_VERSION_MAX
) != NULL
;
6275 group_add_rule(struct ofgroup
*group
, struct rule
*rule
)
6277 rule_collection_add(&group
->rules
, rule
);
6281 group_remove_rule(struct ofgroup
*group
, struct rule
*rule
)
6283 rule_collection_remove(&group
->rules
, rule
);
6287 append_group_stats(struct ofgroup
*group
, struct ovs_list
*replies
)
6288 OVS_REQUIRES(ofproto_mutex
)
6290 struct ofputil_group_stats ogs
;
6291 const struct ofproto
*ofproto
= group
->ofproto
;
6292 long long int now
= time_msec();
6295 ogs
.bucket_stats
= xmalloc(group
->n_buckets
* sizeof *ogs
.bucket_stats
);
6297 /* Provider sets the packet and byte counts, we do the rest. */
6298 ogs
.ref_count
= rule_collection_n(&group
->rules
);
6299 ogs
.n_buckets
= group
->n_buckets
;
6301 error
= (ofproto
->ofproto_class
->group_get_stats
6302 ? ofproto
->ofproto_class
->group_get_stats(group
, &ogs
)
6305 ogs
.packet_count
= UINT64_MAX
;
6306 ogs
.byte_count
= UINT64_MAX
;
6307 memset(ogs
.bucket_stats
, 0xff,
6308 ogs
.n_buckets
* sizeof *ogs
.bucket_stats
);
6311 ogs
.group_id
= group
->group_id
;
6312 calc_duration(group
->created
, now
, &ogs
.duration_sec
, &ogs
.duration_nsec
);
6314 ofputil_append_group_stats(replies
, &ogs
);
6316 free(ogs
.bucket_stats
);
6320 handle_group_request(struct ofconn
*ofconn
,
6321 const struct ofp_header
*request
, uint32_t group_id
,
6322 void (*cb
)(struct ofgroup
*, struct ovs_list
*replies
))
6323 OVS_EXCLUDED(ofproto_mutex
)
6325 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6326 struct ofgroup
*group
;
6327 struct ovs_list replies
;
6329 ofpmp_init(&replies
, request
);
6330 /* Must exclude modifications to guarantee iterating groups. */
6331 ovs_mutex_lock(&ofproto_mutex
);
6332 if (group_id
== OFPG_ALL
) {
6333 CMAP_FOR_EACH (group
, cmap_node
, &ofproto
->groups
) {
6334 cb(group
, &replies
);
6337 group
= ofproto_group_lookup__(ofproto
, group_id
, OVS_VERSION_MAX
);
6339 cb(group
, &replies
);
6342 ovs_mutex_unlock(&ofproto_mutex
);
6343 ofconn_send_replies(ofconn
, &replies
);
6347 handle_group_stats_request(struct ofconn
*ofconn
,
6348 const struct ofp_header
*request
)
6353 error
= ofputil_decode_group_stats_request(request
, &group_id
);
6358 handle_group_request(ofconn
, request
, group_id
, append_group_stats
);
6363 append_group_desc(struct ofgroup
*group
, struct ovs_list
*replies
)
6365 struct ofputil_group_desc gds
;
6367 gds
.group_id
= group
->group_id
;
6368 gds
.type
= group
->type
;
6369 gds
.props
= group
->props
;
6371 ofputil_append_group_desc_reply(&gds
, &group
->buckets
, replies
);
6375 handle_group_desc_stats_request(struct ofconn
*ofconn
,
6376 const struct ofp_header
*request
)
6378 handle_group_request(ofconn
, request
,
6379 ofputil_decode_group_desc_request(request
),
6385 handle_group_features_stats_request(struct ofconn
*ofconn
,
6386 const struct ofp_header
*request
)
6388 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
6391 msg
= ofputil_encode_group_features_reply(&p
->ogf
, request
);
6393 ofconn_send_reply(ofconn
, msg
);
6400 put_queue_get_config_reply(struct ofport
*port
, uint32_t queue
,
6401 struct ovs_list
*replies
)
6403 struct ofputil_queue_config qc
;
6405 /* None of the existing queues have compatible properties, so we hard-code
6406 * omitting min_rate and max_rate. */
6407 qc
.port
= port
->ofp_port
;
6409 qc
.min_rate
= UINT16_MAX
;
6410 qc
.max_rate
= UINT16_MAX
;
6411 ofputil_append_queue_get_config_reply(&qc
, replies
);
6415 handle_queue_get_config_request_for_port(struct ofport
*port
, uint32_t queue
,
6416 struct ovs_list
*replies
)
6418 struct smap details
= SMAP_INITIALIZER(&details
);
6419 if (queue
!= OFPQ_ALL
) {
6420 int error
= netdev_get_queue(port
->netdev
, queue
, &details
);
6423 put_queue_get_config_reply(port
, queue
, replies
);
6427 return OFPERR_OFPQOFC_BAD_QUEUE
;
6429 return OFPERR_NXQOFC_QUEUE_ERROR
;
6432 struct netdev_queue_dump queue_dump
;
6435 NETDEV_QUEUE_FOR_EACH (&queue_id
, &details
, &queue_dump
,
6437 put_queue_get_config_reply(port
, queue_id
, replies
);
6440 smap_destroy(&details
);
6445 handle_queue_get_config_request(struct ofconn
*ofconn
,
6446 const struct ofp_header
*oh
)
6448 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6449 struct ovs_list replies
;
6450 struct ofport
*port
;
6451 ofp_port_t req_port
;
6455 error
= ofputil_decode_queue_get_config_request(oh
, &req_port
, &req_queue
);
6460 ofputil_start_queue_get_config_reply(oh
, &replies
);
6461 if (req_port
== OFPP_ANY
) {
6462 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
6463 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
6464 if (!handle_queue_get_config_request_for_port(port
, req_queue
,
6470 port
= ofproto_get_port(ofproto
, req_port
);
6472 ? handle_queue_get_config_request_for_port(port
, req_queue
,
6474 : OFPERR_OFPQOFC_BAD_PORT
);
6477 ofconn_send_replies(ofconn
, &replies
);
6479 ofpbuf_list_delete(&replies
);
6486 init_group(struct ofproto
*ofproto
, const struct ofputil_group_mod
*gm
,
6487 ovs_version_t version
, struct ofgroup
**ofgroup
)
6490 const long long int now
= time_msec();
6492 if (gm
->group_id
> OFPG_MAX
) {
6493 return OFPERR_OFPGMFC_INVALID_GROUP
;
6495 if (gm
->type
> OFPGT11_FF
) {
6496 return OFPERR_OFPGMFC_BAD_TYPE
;
6499 *ofgroup
= ofproto
->ofproto_class
->group_alloc();
6501 VLOG_WARN_RL(&rl
, "%s: failed to allocate group", ofproto
->name
);
6502 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
6505 *CONST_CAST(struct ofproto
**, &(*ofgroup
)->ofproto
) = ofproto
;
6506 *CONST_CAST(uint32_t *, &((*ofgroup
)->group_id
)) = gm
->group_id
;
6507 *CONST_CAST(enum ofp11_group_type
*, &(*ofgroup
)->type
) = gm
->type
;
6508 *CONST_CAST(long long int *, &((*ofgroup
)->created
)) = now
;
6509 *CONST_CAST(long long int *, &((*ofgroup
)->modified
)) = now
;
6510 ovs_refcount_init(&(*ofgroup
)->ref_count
);
6511 (*ofgroup
)->being_deleted
= false;
6513 ovs_list_init(CONST_CAST(struct ovs_list
*, &(*ofgroup
)->buckets
));
6514 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
6515 &(*ofgroup
)->buckets
),
6516 &gm
->buckets
, NULL
);
6518 *CONST_CAST(uint32_t *, &(*ofgroup
)->n_buckets
) =
6519 ovs_list_size(&(*ofgroup
)->buckets
);
6521 ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props
*,
6522 &(*ofgroup
)->props
),
6524 rule_collection_init(&(*ofgroup
)->rules
);
6526 /* Make group visible from 'version'. */
6527 (*ofgroup
)->versions
= VERSIONS_INITIALIZER(version
,
6528 OVS_VERSION_NOT_REMOVED
);
6530 /* Construct called BEFORE any locks are held. */
6531 error
= ofproto
->ofproto_class
->group_construct(*ofgroup
);
6533 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props
*,
6534 &(*ofgroup
)->props
));
6535 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list
*,
6536 &(*ofgroup
)->buckets
));
6537 ofproto
->ofproto_class
->group_dealloc(*ofgroup
);
6542 /* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
6543 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
6546 add_group_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
6547 OVS_REQUIRES(ofproto_mutex
)
6551 if (ofproto_group_exists(ofproto
, ogm
->gm
.group_id
)) {
6552 return OFPERR_OFPGMFC_GROUP_EXISTS
;
6555 if (ofproto
->n_groups
[ogm
->gm
.type
]
6556 >= ofproto
->ogf
.max_groups
[ogm
->gm
.type
]) {
6557 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
6560 /* Allocate new group and initialize it. */
6561 error
= init_group(ofproto
, &ogm
->gm
, ogm
->version
, &ogm
->new_group
);
6563 /* Insert new group. */
6564 cmap_insert(&ofproto
->groups
, &ogm
->new_group
->cmap_node
,
6565 hash_int(ogm
->new_group
->group_id
, 0));
6566 ofproto
->n_groups
[ogm
->new_group
->type
]++;
6571 /* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets
6572 * already in 'new_ofgroup' will be placed just after the (copy of the) bucket
6573 * in 'ofgroup' with bucket ID 'command_bucket_id'. Special
6574 * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are
6577 copy_buckets_for_insert_bucket(const struct ofgroup
*ofgroup
,
6578 struct ofgroup
*new_ofgroup
,
6579 uint32_t command_bucket_id
)
6581 struct ofputil_bucket
*last
= NULL
;
6583 if (command_bucket_id
<= OFPG15_BUCKET_MAX
) {
6584 /* Check here to ensure that a bucket corresponding to
6585 * command_bucket_id exists in the old bucket list.
6587 * The subsequent search of below of new_ofgroup covers
6588 * both buckets in the old bucket list and buckets added
6589 * by the insert buckets group mod message this function processes. */
6590 if (!ofputil_bucket_find(&ofgroup
->buckets
, command_bucket_id
)) {
6591 return OFPERR_OFPGMFC_UNKNOWN_BUCKET
;
6594 if (!ovs_list_is_empty(&new_ofgroup
->buckets
)) {
6595 last
= ofputil_bucket_list_back(&new_ofgroup
->buckets
);
6599 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
6600 &new_ofgroup
->buckets
),
6601 &ofgroup
->buckets
, NULL
);
6603 if (ofputil_bucket_check_duplicate_id(&new_ofgroup
->buckets
)) {
6604 VLOG_INFO_RL(&rl
, "Duplicate bucket id");
6605 return OFPERR_OFPGMFC_BUCKET_EXISTS
;
6608 /* Rearrange list according to command_bucket_id */
6609 if (command_bucket_id
== OFPG15_BUCKET_LAST
) {
6610 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
6611 struct ofputil_bucket
*new_first
;
6612 const struct ofputil_bucket
*first
;
6614 first
= ofputil_bucket_list_front(&ofgroup
->buckets
);
6615 new_first
= ofputil_bucket_find(&new_ofgroup
->buckets
,
6618 ovs_list_splice(new_ofgroup
->buckets
.next
, &new_first
->list_node
,
6619 CONST_CAST(struct ovs_list
*,
6620 &new_ofgroup
->buckets
));
6622 } else if (command_bucket_id
<= OFPG15_BUCKET_MAX
&& last
) {
6623 struct ofputil_bucket
*after
;
6625 /* Presence of bucket is checked above so after should never be NULL */
6626 after
= ofputil_bucket_find(&new_ofgroup
->buckets
, command_bucket_id
);
6628 ovs_list_splice(after
->list_node
.next
, new_ofgroup
->buckets
.next
,
6629 last
->list_node
.next
);
6635 /* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup'
6636 * with the exception of the bucket whose bucket id is 'command_bucket_id'.
6637 * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST
6638 * and OFPG15_BUCKET_ALL are also honored. */
6640 copy_buckets_for_remove_bucket(const struct ofgroup
*ofgroup
,
6641 struct ofgroup
*new_ofgroup
,
6642 uint32_t command_bucket_id
)
6644 const struct ofputil_bucket
*skip
= NULL
;
6646 if (command_bucket_id
== OFPG15_BUCKET_ALL
) {
6650 if (command_bucket_id
== OFPG15_BUCKET_FIRST
) {
6651 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
6652 skip
= ofputil_bucket_list_front(&ofgroup
->buckets
);
6654 } else if (command_bucket_id
== OFPG15_BUCKET_LAST
) {
6655 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
6656 skip
= ofputil_bucket_list_back(&ofgroup
->buckets
);
6659 skip
= ofputil_bucket_find(&ofgroup
->buckets
, command_bucket_id
);
6661 return OFPERR_OFPGMFC_UNKNOWN_BUCKET
;
6665 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
6666 &new_ofgroup
->buckets
),
6667 &ofgroup
->buckets
, skip
);
6672 /* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and
6673 * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code
6676 * Note that the group is re-created and then replaces the old group in
6677 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
6678 * the xlate module hold a pointer to the group. */
6680 modify_group_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
6681 OVS_REQUIRES(ofproto_mutex
)
6683 struct ofgroup
*old_group
; /* Modified group. */
6684 struct ofgroup
*new_group
;
6687 old_group
= ofproto_group_lookup__(ofproto
, ogm
->gm
.group_id
,
6690 return OFPERR_OFPGMFC_UNKNOWN_GROUP
;
6693 if (old_group
->type
!= ogm
->gm
.type
6694 && (ofproto
->n_groups
[ogm
->gm
.type
]
6695 >= ofproto
->ogf
.max_groups
[ogm
->gm
.type
])) {
6696 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
6699 error
= init_group(ofproto
, &ogm
->gm
, ogm
->version
, &ogm
->new_group
);
6703 new_group
= ogm
->new_group
;
6705 /* Manipulate bucket list for bucket commands */
6706 if (ogm
->gm
.command
== OFPGC15_INSERT_BUCKET
) {
6707 error
= copy_buckets_for_insert_bucket(old_group
, new_group
,
6708 ogm
->gm
.command_bucket_id
);
6709 } else if (ogm
->gm
.command
== OFPGC15_REMOVE_BUCKET
) {
6710 error
= copy_buckets_for_remove_bucket(old_group
, new_group
,
6711 ogm
->gm
.command_bucket_id
);
6717 /* The group creation time does not change during modification. */
6718 *CONST_CAST(long long int *, &(new_group
->created
)) = old_group
->created
;
6719 *CONST_CAST(long long int *, &(new_group
->modified
)) = time_msec();
6721 group_collection_add(&ogm
->old_groups
, old_group
);
6723 /* Mark the old group for deletion. */
6724 versions_set_remove_version(&old_group
->versions
, ogm
->version
);
6725 /* Insert replacement group. */
6726 cmap_insert(&ofproto
->groups
, &new_group
->cmap_node
,
6727 hash_int(new_group
->group_id
, 0));
6728 /* Transfer rules. */
6729 rule_collection_move(&new_group
->rules
, &old_group
->rules
);
6731 if (old_group
->type
!= new_group
->type
) {
6732 ofproto
->n_groups
[old_group
->type
]--;
6733 ofproto
->n_groups
[new_group
->type
]++;
6738 ofproto_group_unref(new_group
);
6742 /* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not
6743 * exist yet and modifies it otherwise */
6745 add_or_modify_group_start(struct ofproto
*ofproto
,
6746 struct ofproto_group_mod
*ogm
)
6747 OVS_REQUIRES(ofproto_mutex
)
6751 if (!ofproto_group_exists(ofproto
, ogm
->gm
.group_id
)) {
6752 error
= add_group_start(ofproto
, ogm
);
6754 error
= modify_group_start(ofproto
, ogm
);
6761 delete_group_start(struct ofproto
*ofproto
, ovs_version_t version
,
6762 struct group_collection
*groups
, struct ofgroup
*group
)
6763 OVS_REQUIRES(ofproto_mutex
)
6765 /* Makes flow deletion code leave the rule pointers in 'group->rules'
6766 * intact, so that we can later refer to the rules deleted due to the group
6767 * deletion. Rule pointers will be removed from all other groups, if any,
6768 * so we will never try to delete the same rule twice. */
6769 group
->being_deleted
= true;
6771 /* Mark all the referring groups for deletion. */
6772 delete_flows_start__(ofproto
, version
, &group
->rules
);
6773 group_collection_add(groups
, group
);
6774 versions_set_remove_version(&group
->versions
, version
);
6775 ofproto
->n_groups
[group
->type
]--;
6779 delete_group_finish(struct ofproto
*ofproto
, struct ofgroup
*group
)
6780 OVS_REQUIRES(ofproto_mutex
)
6782 /* Finish deletion of all flow entries containing this group in a group
6784 delete_flows_finish__(ofproto
, &group
->rules
, OFPRR_GROUP_DELETE
, NULL
);
6786 /* Group removal is postponed by the caller. */
6789 /* Implements OFPGC11_DELETE. */
6791 delete_groups_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
6792 OVS_REQUIRES(ofproto_mutex
)
6794 struct ofgroup
*group
;
6796 if (ogm
->gm
.group_id
== OFPG_ALL
) {
6797 CMAP_FOR_EACH (group
, cmap_node
, &ofproto
->groups
) {
6798 if (versions_visible_in_version(&group
->versions
, ogm
->version
)) {
6799 delete_group_start(ofproto
, ogm
->version
, &ogm
->old_groups
,
6804 group
= ofproto_group_lookup__(ofproto
, ogm
->gm
.group_id
, ogm
->version
);
6806 delete_group_start(ofproto
, ogm
->version
, &ogm
->old_groups
, group
);
6812 ofproto_group_mod_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
6813 OVS_REQUIRES(ofproto_mutex
)
6817 ogm
->new_group
= NULL
;
6818 group_collection_init(&ogm
->old_groups
);
6820 switch (ogm
->gm
.command
) {
6822 error
= add_group_start(ofproto
, ogm
);
6825 case OFPGC11_MODIFY
:
6826 error
= modify_group_start(ofproto
, ogm
);
6829 case OFPGC11_ADD_OR_MOD
:
6830 error
= add_or_modify_group_start(ofproto
, ogm
);
6833 case OFPGC11_DELETE
:
6834 delete_groups_start(ofproto
, ogm
);
6838 case OFPGC15_INSERT_BUCKET
:
6839 error
= modify_group_start(ofproto
, ogm
);
6842 case OFPGC15_REMOVE_BUCKET
:
6843 error
= modify_group_start(ofproto
, ogm
);
6847 if (ogm
->gm
.command
> OFPGC11_DELETE
) {
6848 VLOG_INFO_RL(&rl
, "%s: Invalid group_mod command type %d",
6849 ofproto
->name
, ogm
->gm
.command
);
6851 error
= OFPERR_OFPGMFC_BAD_COMMAND
;
6858 ofproto_group_mod_revert(struct ofproto
*ofproto
,
6859 struct ofproto_group_mod
*ogm
)
6860 OVS_REQUIRES(ofproto_mutex
)
6862 struct ofgroup
*new_group
= ogm
->new_group
;
6863 struct ofgroup
*old_group
;
6865 /* Restore replaced or deleted groups. */
6866 GROUP_COLLECTION_FOR_EACH (old_group
, &ogm
->old_groups
) {
6867 ofproto
->n_groups
[old_group
->type
]++;
6869 ovs_assert(group_collection_n(&ogm
->old_groups
) == 1);
6870 /* Transfer rules back. */
6871 rule_collection_move(&old_group
->rules
, &new_group
->rules
);
6873 old_group
->being_deleted
= false;
6874 /* Revert rule deletion. */
6875 delete_flows_revert__(ofproto
, &old_group
->rules
);
6877 /* Restore visibility. */
6878 versions_set_remove_version(&old_group
->versions
,
6879 OVS_VERSION_NOT_REMOVED
);
6882 /* Remove the new group immediately. It was never visible to
6884 cmap_remove(&ofproto
->groups
, &new_group
->cmap_node
,
6885 hash_int(new_group
->group_id
, 0));
6886 ofproto
->n_groups
[new_group
->type
]--;
6887 ofproto_group_unref(new_group
);
6892 ofproto_group_mod_finish(struct ofproto
*ofproto
,
6893 struct ofproto_group_mod
*ogm
,
6894 const struct openflow_mod_requester
*req
)
6895 OVS_REQUIRES(ofproto_mutex
)
6897 struct ofgroup
*new_group
= ogm
->new_group
;
6898 struct ofgroup
*old_group
;
6900 if (new_group
&& group_collection_n(&ogm
->old_groups
)) {
6901 /* Modify a group. */
6902 ovs_assert(group_collection_n(&ogm
->old_groups
) == 1);
6904 /* XXX: OK to lose old group's stats? */
6905 ofproto
->ofproto_class
->group_modify(new_group
);
6908 /* Delete old groups. */
6909 GROUP_COLLECTION_FOR_EACH(old_group
, &ogm
->old_groups
) {
6910 delete_group_finish(ofproto
, old_group
);
6912 remove_groups_postponed(&ogm
->old_groups
);
6915 struct ofputil_requestforward rf
;
6916 rf
.xid
= req
->request
->xid
;
6917 rf
.reason
= OFPRFR_GROUP_MOD
;
6918 rf
.group_mod
= &ogm
->gm
;
6919 connmgr_send_requestforward(ofproto
->connmgr
, req
->ofconn
, &rf
);
6923 /* Delete all groups from 'ofproto'.
6925 * This is intended for use within an ofproto provider's 'destruct'
6928 ofproto_group_delete_all(struct ofproto
*ofproto
)
6929 OVS_EXCLUDED(ofproto_mutex
)
6931 struct ofproto_group_mod ogm
;
6933 ogm
.gm
.command
= OFPGC11_DELETE
;
6934 ogm
.gm
.group_id
= OFPG_ALL
;
6936 ovs_mutex_lock(&ofproto_mutex
);
6937 ogm
.version
= ofproto
->tables_version
+ 1;
6938 ofproto_group_mod_start(ofproto
, &ogm
);
6939 ofproto_bump_tables_version(ofproto
);
6940 ofproto_group_mod_finish(ofproto
, &ogm
, NULL
);
6941 ovs_mutex_unlock(&ofproto_mutex
);
6945 handle_group_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6946 OVS_EXCLUDED(ofproto_mutex
)
6948 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6949 struct ofproto_group_mod ogm
;
6952 error
= reject_slave_controller(ofconn
);
6957 error
= ofputil_decode_group_mod(oh
, &ogm
.gm
);
6962 ovs_mutex_lock(&ofproto_mutex
);
6963 ogm
.version
= ofproto
->tables_version
+ 1;
6964 error
= ofproto_group_mod_start(ofproto
, &ogm
);
6966 struct openflow_mod_requester req
= { ofconn
, oh
};
6968 ofproto_bump_tables_version(ofproto
);
6969 ofproto_group_mod_finish(ofproto
, &ogm
, &req
);
6971 ofmonitor_flush(ofproto
->connmgr
);
6972 ovs_mutex_unlock(&ofproto_mutex
);
6974 ofputil_uninit_group_mod(&ogm
.gm
);
6979 enum ofputil_table_miss
6980 ofproto_table_get_miss_config(const struct ofproto
*ofproto
, uint8_t table_id
)
6982 enum ofputil_table_miss miss
;
6984 atomic_read_relaxed(&ofproto
->tables
[table_id
].miss_config
, &miss
);
6989 table_mod__(struct oftable
*oftable
,
6990 const struct ofputil_table_mod
*tm
)
6992 if (tm
->miss
== OFPUTIL_TABLE_MISS_DEFAULT
) {
6993 /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any
6994 * table-miss configuration (because the protocol used doesn't have
6995 * such a concept), so there's nothing to do. */
6997 atomic_store_relaxed(&oftable
->miss_config
, tm
->miss
);
7000 unsigned int new_eviction
= oftable
->eviction
;
7001 if (tm
->eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
7002 new_eviction
|= EVICTION_OPENFLOW
;
7003 } else if (tm
->eviction
== OFPUTIL_TABLE_EVICTION_OFF
) {
7004 new_eviction
&= ~EVICTION_OPENFLOW
;
7007 if (new_eviction
!= oftable
->eviction
) {
7008 ovs_mutex_lock(&ofproto_mutex
);
7009 oftable_configure_eviction(oftable
, new_eviction
,
7010 oftable
->eviction_fields
,
7011 oftable
->n_eviction_fields
);
7012 ovs_mutex_unlock(&ofproto_mutex
);
7015 if (tm
->vacancy
!= OFPUTIL_TABLE_VACANCY_DEFAULT
) {
7016 ovs_mutex_lock(&ofproto_mutex
);
7017 oftable
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
7018 oftable
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
7019 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_OFF
) {
7020 oftable
->vacancy_event
= 0;
7021 } else if (!oftable
->vacancy_event
) {
7022 uint8_t vacancy
= oftable_vacancy(oftable
);
7023 oftable
->vacancy_event
= (vacancy
< oftable
->vacancy_up
7025 : OFPTR_VACANCY_DOWN
);
7027 ovs_mutex_unlock(&ofproto_mutex
);
7032 table_mod(struct ofproto
*ofproto
, const struct ofputil_table_mod
*tm
)
7034 if (!check_table_id(ofproto
, tm
->table_id
)) {
7035 return OFPERR_OFPTMFC_BAD_TABLE
;
7038 /* Don't allow the eviction flags to be changed (except to the only fixed
7039 * value that OVS supports). OF1.4 says this is normal: "The
7040 * OFPTMPT_EVICTION property usually cannot be modified using a
7041 * OFP_TABLE_MOD request, because the eviction mechanism is switch
7043 if (tm
->eviction_flags
!= UINT32_MAX
7044 && tm
->eviction_flags
!= OFPROTO_EVICTION_FLAGS
) {
7045 return OFPERR_OFPTMFC_BAD_CONFIG
;
7048 if (tm
->table_id
== OFPTT_ALL
) {
7049 struct oftable
*oftable
;
7050 OFPROTO_FOR_EACH_TABLE (oftable
, ofproto
) {
7051 if (!(oftable
->flags
& (OFTABLE_HIDDEN
| OFTABLE_READONLY
))) {
7052 table_mod__(oftable
, tm
);
7056 struct oftable
*oftable
= &ofproto
->tables
[tm
->table_id
];
7057 if (oftable
->flags
& OFTABLE_READONLY
) {
7058 return OFPERR_OFPTMFC_EPERM
;
7060 table_mod__(oftable
, tm
);
7067 handle_table_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7069 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7070 struct ofputil_table_mod tm
;
7073 error
= reject_slave_controller(ofconn
);
7078 error
= ofputil_decode_table_mod(oh
, &tm
);
7083 return table_mod(ofproto
, &tm
);
7086 /* Free resources that may be allocated by ofproto_flow_mod_init(). */
7088 ofproto_flow_mod_uninit(struct ofproto_flow_mod
*ofm
)
7090 if (ofm
->temp_rule
) {
7091 ofproto_rule_unref(ofm
->temp_rule
);
7092 ofm
->temp_rule
= NULL
;
7094 if (ofm
->criteria
.version
!= OVS_VERSION_NOT_REMOVED
) {
7095 rule_criteria_destroy(&ofm
->criteria
);
7105 ofproto_flow_mod_init(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
7106 const struct ofputil_flow_mod
*fm
)
7107 OVS_EXCLUDED(ofproto_mutex
)
7111 /* Forward flow mod fields we need later. */
7112 ofm
->command
= fm
->command
;
7113 ofm
->buffer_id
= fm
->buffer_id
;
7114 ofm
->modify_cookie
= fm
->modify_cookie
;
7116 ofm
->modify_may_add_flow
= (fm
->new_cookie
!= OVS_BE64_MAX
7117 && fm
->cookie_mask
== htonll(0));
7119 /* Initialize state needed by ofproto_flow_mod_uninit(). */
7120 ofm
->temp_rule
= NULL
;
7121 ofm
->criteria
.version
= OVS_VERSION_NOT_REMOVED
;
7125 switch (ofm
->command
) {
7127 error
= add_flow_init(ofproto
, ofm
, fm
);
7130 error
= modify_flows_init_loose(ofproto
, ofm
, fm
);
7132 case OFPFC_MODIFY_STRICT
:
7133 error
= modify_flow_init_strict(ofproto
, ofm
, fm
);
7136 error
= delete_flows_init_loose(ofproto
, ofm
, fm
);
7138 case OFPFC_DELETE_STRICT
:
7139 error
= delete_flows_init_strict(ofproto
, ofm
, fm
);
7142 error
= OFPERR_OFPFMFC_BAD_COMMAND
;
7146 ofproto_flow_mod_uninit(ofm
);
7152 ofproto_flow_mod_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
7153 OVS_REQUIRES(ofproto_mutex
)
7157 rule_collection_init(&ofm
->old_rules
);
7158 rule_collection_init(&ofm
->new_rules
);
7160 switch (ofm
->command
) {
7162 error
= add_flow_start(ofproto
, ofm
);
7165 error
= modify_flows_start_loose(ofproto
, ofm
);
7167 case OFPFC_MODIFY_STRICT
:
7168 error
= modify_flow_start_strict(ofproto
, ofm
);
7171 error
= delete_flows_start_loose(ofproto
, ofm
);
7173 case OFPFC_DELETE_STRICT
:
7174 error
= delete_flow_start_strict(ofproto
, ofm
);
7179 /* Release resources not needed after start. */
7180 ofproto_flow_mod_uninit(ofm
);
7183 rule_collection_destroy(&ofm
->old_rules
);
7184 rule_collection_destroy(&ofm
->new_rules
);
7190 ofproto_flow_mod_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
7191 OVS_REQUIRES(ofproto_mutex
)
7193 switch (ofm
->command
) {
7195 add_flow_revert(ofproto
, ofm
);
7199 case OFPFC_MODIFY_STRICT
:
7200 modify_flows_revert(ofproto
, ofm
);
7204 case OFPFC_DELETE_STRICT
:
7205 delete_flows_revert(ofproto
, ofm
);
7211 rule_collection_destroy(&ofm
->old_rules
);
7212 rule_collection_destroy(&ofm
->new_rules
);
7216 ofproto_flow_mod_finish(struct ofproto
*ofproto
,
7217 struct ofproto_flow_mod
*ofm
,
7218 const struct openflow_mod_requester
*req
)
7219 OVS_REQUIRES(ofproto_mutex
)
7221 switch (ofm
->command
) {
7223 add_flow_finish(ofproto
, ofm
, req
);
7227 case OFPFC_MODIFY_STRICT
:
7228 modify_flows_finish(ofproto
, ofm
, req
);
7232 case OFPFC_DELETE_STRICT
:
7233 delete_flows_finish(ofproto
, ofm
, req
);
7240 rule_collection_destroy(&ofm
->old_rules
);
7241 rule_collection_destroy(&ofm
->new_rules
);
7244 ofconn_report_flow_mod(req
->ofconn
, ofm
->command
);
7248 /* Commit phases (all while locking ofproto_mutex):
7250 * 1. Begin: Gather resources and make changes visible in the next version.
7251 * - Mark affected rules for removal in the next version.
7252 * - Create new replacement rules, make visible in the next
7254 * - Do not send any events or notifications.
7256 * 2. Revert: Fail if any errors are found. After this point no errors are
7257 * possible. No visible changes were made, so rollback is minimal (remove
7258 * added invisible rules, restore visibility of rules marked for removal).
7260 * 3. Finish: Make the changes visible for lookups. Insert replacement rules to
7261 * the ofproto provider. Remove replaced and deleted rules from ofproto data
7262 * structures, and Schedule postponed removal of deleted rules from the
7263 * classifier. Send notifications, buffered packets, etc.
7266 do_bundle_commit(struct ofconn
*ofconn
, uint32_t id
, uint16_t flags
)
7268 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7269 ovs_version_t version
= ofproto
->tables_version
+ 1;
7270 struct ofp_bundle
*bundle
;
7271 struct ofp_bundle_entry
*be
;
7274 bundle
= ofconn_get_bundle(ofconn
, id
);
7277 return OFPERR_OFPBFC_BAD_ID
;
7279 if (bundle
->flags
!= flags
) {
7280 error
= OFPERR_OFPBFC_BAD_FLAGS
;
7282 bool prev_is_port_mod
= false;
7285 ovs_mutex_lock(&ofproto_mutex
);
7288 LIST_FOR_EACH (be
, node
, &bundle
->msg_list
) {
7289 if (be
->type
== OFPTYPE_PORT_MOD
) {
7290 /* Our port mods are not atomic. */
7291 if (flags
& OFPBF_ATOMIC
) {
7292 error
= OFPERR_OFPBFC_MSG_FAILED
;
7294 prev_is_port_mod
= true;
7295 error
= port_mod_start(ofconn
, &be
->opm
.pm
, &be
->opm
.port
);
7298 /* Flow & group mods between port mods are applied as a single
7299 * version, but the versions are published only after we know
7300 * the commit is successful. */
7301 if (prev_is_port_mod
) {
7302 prev_is_port_mod
= false;
7305 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7306 /* Store the version in which the changes should take
7308 be
->ofm
.version
= version
;
7309 error
= ofproto_flow_mod_start(ofproto
, &be
->ofm
);
7310 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7311 /* Store the version in which the changes should take
7313 be
->ogm
.version
= version
;
7314 error
= ofproto_group_mod_start(ofproto
, &be
->ogm
);
7325 /* Send error referring to the original message. */
7327 ofconn_send_error(ofconn
, be
->ofp_msg
, error
);
7328 error
= OFPERR_OFPBFC_MSG_FAILED
;
7331 /* 2. Revert. Undo all the changes made above. */
7332 LIST_FOR_EACH_REVERSE_CONTINUE(be
, node
, &bundle
->msg_list
) {
7333 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7334 ofproto_flow_mod_revert(ofproto
, &be
->ofm
);
7335 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7336 ofproto_group_mod_revert(ofproto
, &be
->ogm
);
7339 /* Nothing needs to be reverted for a port mod. */
7343 LIST_FOR_EACH (be
, node
, &bundle
->msg_list
) {
7344 if (be
->type
== OFPTYPE_PORT_MOD
) {
7345 /* Perform the actual port mod. This is not atomic, i.e.,
7346 * the effects will be immediately seen by upcall
7347 * processing regardless of the lookup version. It should
7348 * be noted that port configuration changes can originate
7349 * also from OVSDB changes asynchronously to all upcall
7351 port_mod_finish(ofconn
, &be
->opm
.pm
, be
->opm
.port
);
7353 struct openflow_mod_requester req
= { ofconn
,
7355 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7356 /* Bump the lookup version to the one of the current
7357 * message. This makes all the changes in the bundle
7358 * at this version visible to lookups at once. */
7359 if (ofproto
->tables_version
< be
->ofm
.version
) {
7360 ofproto
->tables_version
= be
->ofm
.version
;
7361 ofproto
->ofproto_class
->set_tables_version(
7362 ofproto
, ofproto
->tables_version
);
7365 ofproto_flow_mod_finish(ofproto
, &be
->ofm
, &req
);
7366 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7367 /* Bump the lookup version to the one of the current
7368 * message. This makes all the changes in the bundle
7369 * at this version visible to lookups at once. */
7370 if (ofproto
->tables_version
< be
->ogm
.version
) {
7371 ofproto
->tables_version
= be
->ogm
.version
;
7372 ofproto
->ofproto_class
->set_tables_version(
7373 ofproto
, ofproto
->tables_version
);
7376 ofproto_group_mod_finish(ofproto
, &be
->ogm
, &req
);
7382 ofmonitor_flush(ofproto
->connmgr
);
7383 ovs_mutex_unlock(&ofproto_mutex
);
7385 run_rule_executes(ofproto
);
7388 /* The bundle is discarded regardless the outcome. */
7389 ofp_bundle_remove__(ofconn
, bundle
, !error
);
7394 handle_bundle_control(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7396 struct ofputil_bundle_ctrl_msg bctrl
;
7397 struct ofputil_bundle_ctrl_msg reply
;
7401 error
= reject_slave_controller(ofconn
);
7406 error
= ofputil_decode_bundle_ctrl(oh
, &bctrl
);
7411 reply
.bundle_id
= bctrl
.bundle_id
;
7413 switch (bctrl
.type
) {
7414 case OFPBCT_OPEN_REQUEST
:
7415 error
= ofp_bundle_open(ofconn
, bctrl
.bundle_id
, bctrl
.flags
);
7416 reply
.type
= OFPBCT_OPEN_REPLY
;
7418 case OFPBCT_CLOSE_REQUEST
:
7419 error
= ofp_bundle_close(ofconn
, bctrl
.bundle_id
, bctrl
.flags
);
7420 reply
.type
= OFPBCT_CLOSE_REPLY
;
7422 case OFPBCT_COMMIT_REQUEST
:
7423 error
= do_bundle_commit(ofconn
, bctrl
.bundle_id
, bctrl
.flags
);
7424 reply
.type
= OFPBCT_COMMIT_REPLY
;
7426 case OFPBCT_DISCARD_REQUEST
:
7427 error
= ofp_bundle_discard(ofconn
, bctrl
.bundle_id
);
7428 reply
.type
= OFPBCT_DISCARD_REPLY
;
7431 case OFPBCT_OPEN_REPLY
:
7432 case OFPBCT_CLOSE_REPLY
:
7433 case OFPBCT_COMMIT_REPLY
:
7434 case OFPBCT_DISCARD_REPLY
:
7435 return OFPERR_OFPBFC_BAD_TYPE
;
7440 buf
= ofputil_encode_bundle_ctrl_reply(oh
, &reply
);
7441 ofconn_send_reply(ofconn
, buf
);
7447 handle_bundle_add(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7448 OVS_EXCLUDED(ofproto_mutex
)
7450 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7452 struct ofputil_bundle_add_msg badd
;
7453 struct ofp_bundle_entry
*bmsg
;
7456 error
= reject_slave_controller(ofconn
);
7461 error
= ofputil_decode_bundle_add(oh
, &badd
, &type
);
7466 bmsg
= ofp_bundle_entry_alloc(type
, badd
.msg
);
7468 if (type
== OFPTYPE_PORT_MOD
) {
7469 error
= ofputil_decode_port_mod(badd
.msg
, &bmsg
->opm
.pm
, false);
7470 } else if (type
== OFPTYPE_FLOW_MOD
) {
7471 struct ofputil_flow_mod fm
;
7472 struct ofpbuf ofpacts
;
7473 uint64_t ofpacts_stub
[1024 / 8];
7475 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
7476 error
= ofputil_decode_flow_mod(&fm
, badd
.msg
,
7477 ofconn_get_protocol(ofconn
),
7479 u16_to_ofp(ofproto
->max_ports
),
7482 error
= ofproto_flow_mod_init(ofproto
, &bmsg
->ofm
, &fm
);
7484 ofpbuf_uninit(&ofpacts
);
7485 } else if (type
== OFPTYPE_GROUP_MOD
) {
7486 error
= ofputil_decode_group_mod(badd
.msg
, &bmsg
->ogm
.gm
);
7492 error
= ofp_bundle_add_message(ofconn
, badd
.bundle_id
, badd
.flags
,
7497 ofp_bundle_entry_free(bmsg
);
7504 handle_tlv_table_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7506 struct ofputil_tlv_table_mod ttm
;
7509 error
= reject_slave_controller(ofconn
);
7514 error
= ofputil_decode_tlv_table_mod(oh
, &ttm
);
7519 error
= tun_metadata_table_mod(&ttm
);
7521 ofputil_uninit_tlv_table(&ttm
.mappings
);
7526 handle_tlv_table_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7528 struct ofputil_tlv_table_reply ttr
;
7531 tun_metadata_table_request(&ttr
);
7532 b
= ofputil_encode_tlv_table_reply(oh
, &ttr
);
7533 ofputil_uninit_tlv_table(&ttr
.mappings
);
7535 ofconn_send_reply(ofconn
, b
);
7540 handle_openflow__(struct ofconn
*ofconn
, const struct ofpbuf
*msg
)
7541 OVS_EXCLUDED(ofproto_mutex
)
7543 const struct ofp_header
*oh
= msg
->data
;
7547 error
= ofptype_decode(&type
, oh
);
7551 if (oh
->version
>= OFP13_VERSION
&& ofpmsg_is_stat_request(oh
)
7552 && ofpmp_more(oh
)) {
7553 /* We have no buffer implementation for multipart requests.
7554 * Report overflow for requests which consists of multiple
7556 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW
;
7560 /* OpenFlow requests. */
7561 case OFPTYPE_ECHO_REQUEST
:
7562 return handle_echo_request(ofconn
, oh
);
7564 case OFPTYPE_FEATURES_REQUEST
:
7565 return handle_features_request(ofconn
, oh
);
7567 case OFPTYPE_GET_CONFIG_REQUEST
:
7568 return handle_get_config_request(ofconn
, oh
);
7570 case OFPTYPE_SET_CONFIG
:
7571 return handle_set_config(ofconn
, oh
);
7573 case OFPTYPE_PACKET_OUT
:
7574 return handle_packet_out(ofconn
, oh
);
7576 case OFPTYPE_PORT_MOD
:
7577 return handle_port_mod(ofconn
, oh
);
7579 case OFPTYPE_FLOW_MOD
:
7580 return handle_flow_mod(ofconn
, oh
);
7582 case OFPTYPE_GROUP_MOD
:
7583 return handle_group_mod(ofconn
, oh
);
7585 case OFPTYPE_TABLE_MOD
:
7586 return handle_table_mod(ofconn
, oh
);
7588 case OFPTYPE_METER_MOD
:
7589 return handle_meter_mod(ofconn
, oh
);
7591 case OFPTYPE_BARRIER_REQUEST
:
7592 return handle_barrier_request(ofconn
, oh
);
7594 case OFPTYPE_ROLE_REQUEST
:
7595 return handle_role_request(ofconn
, oh
);
7597 /* OpenFlow replies. */
7598 case OFPTYPE_ECHO_REPLY
:
7601 /* Nicira extension requests. */
7602 case OFPTYPE_FLOW_MOD_TABLE_ID
:
7603 return handle_nxt_flow_mod_table_id(ofconn
, oh
);
7605 case OFPTYPE_SET_FLOW_FORMAT
:
7606 return handle_nxt_set_flow_format(ofconn
, oh
);
7608 case OFPTYPE_SET_PACKET_IN_FORMAT
:
7609 return handle_nxt_set_packet_in_format(ofconn
, oh
);
7611 case OFPTYPE_SET_CONTROLLER_ID
:
7612 return handle_nxt_set_controller_id(ofconn
, oh
);
7614 case OFPTYPE_FLOW_AGE
:
7615 /* Nothing to do. */
7618 case OFPTYPE_FLOW_MONITOR_CANCEL
:
7619 return handle_flow_monitor_cancel(ofconn
, oh
);
7621 case OFPTYPE_SET_ASYNC_CONFIG
:
7622 return handle_nxt_set_async_config(ofconn
, oh
);
7624 case OFPTYPE_GET_ASYNC_REQUEST
:
7625 return handle_nxt_get_async_request(ofconn
, oh
);
7627 case OFPTYPE_NXT_RESUME
:
7628 return handle_nxt_resume(ofconn
, oh
);
7630 /* Statistics requests. */
7631 case OFPTYPE_DESC_STATS_REQUEST
:
7632 return handle_desc_stats_request(ofconn
, oh
);
7634 case OFPTYPE_FLOW_STATS_REQUEST
:
7635 return handle_flow_stats_request(ofconn
, oh
);
7637 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
7638 return handle_aggregate_stats_request(ofconn
, oh
);
7640 case OFPTYPE_TABLE_STATS_REQUEST
:
7641 return handle_table_stats_request(ofconn
, oh
);
7643 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
7644 return handle_table_features_request(ofconn
, oh
);
7646 case OFPTYPE_TABLE_DESC_REQUEST
:
7647 return handle_table_desc_request(ofconn
, oh
);
7649 case OFPTYPE_PORT_STATS_REQUEST
:
7650 return handle_port_stats_request(ofconn
, oh
);
7652 case OFPTYPE_QUEUE_STATS_REQUEST
:
7653 return handle_queue_stats_request(ofconn
, oh
);
7655 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
7656 return handle_port_desc_stats_request(ofconn
, oh
);
7658 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
7659 return handle_flow_monitor_request(ofconn
, oh
);
7661 case OFPTYPE_METER_STATS_REQUEST
:
7662 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
7663 return handle_meter_request(ofconn
, oh
, type
);
7665 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
7666 return handle_meter_features_request(ofconn
, oh
);
7668 case OFPTYPE_GROUP_STATS_REQUEST
:
7669 return handle_group_stats_request(ofconn
, oh
);
7671 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
7672 return handle_group_desc_stats_request(ofconn
, oh
);
7674 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
7675 return handle_group_features_stats_request(ofconn
, oh
);
7677 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
7678 return handle_queue_get_config_request(ofconn
, oh
);
7680 case OFPTYPE_BUNDLE_CONTROL
:
7681 return handle_bundle_control(ofconn
, oh
);
7683 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
7684 return handle_bundle_add(ofconn
, oh
);
7686 case OFPTYPE_NXT_TLV_TABLE_MOD
:
7687 return handle_tlv_table_mod(ofconn
, oh
);
7689 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
7690 return handle_tlv_table_request(ofconn
, oh
);
7692 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST
:
7693 return handle_ipfix_bridge_stats_request(ofconn
, oh
);
7695 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST
:
7696 return handle_ipfix_flow_stats_request(ofconn
, oh
);
7700 case OFPTYPE_FEATURES_REPLY
:
7701 case OFPTYPE_GET_CONFIG_REPLY
:
7702 case OFPTYPE_PACKET_IN
:
7703 case OFPTYPE_FLOW_REMOVED
:
7704 case OFPTYPE_PORT_STATUS
:
7705 case OFPTYPE_BARRIER_REPLY
:
7706 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
7707 case OFPTYPE_DESC_STATS_REPLY
:
7708 case OFPTYPE_FLOW_STATS_REPLY
:
7709 case OFPTYPE_QUEUE_STATS_REPLY
:
7710 case OFPTYPE_PORT_STATS_REPLY
:
7711 case OFPTYPE_TABLE_STATS_REPLY
:
7712 case OFPTYPE_AGGREGATE_STATS_REPLY
:
7713 case OFPTYPE_PORT_DESC_STATS_REPLY
:
7714 case OFPTYPE_ROLE_REPLY
:
7715 case OFPTYPE_FLOW_MONITOR_PAUSED
:
7716 case OFPTYPE_FLOW_MONITOR_RESUMED
:
7717 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
7718 case OFPTYPE_GET_ASYNC_REPLY
:
7719 case OFPTYPE_GROUP_STATS_REPLY
:
7720 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
7721 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
7722 case OFPTYPE_METER_STATS_REPLY
:
7723 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
7724 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
7725 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
7726 case OFPTYPE_TABLE_DESC_REPLY
:
7727 case OFPTYPE_ROLE_STATUS
:
7728 case OFPTYPE_REQUESTFORWARD
:
7729 case OFPTYPE_TABLE_STATUS
:
7730 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
7731 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY
:
7732 case OFPTYPE_IPFIX_FLOW_STATS_REPLY
:
7734 if (ofpmsg_is_stat_request(oh
)) {
7735 return OFPERR_OFPBRC_BAD_STAT
;
7737 return OFPERR_OFPBRC_BAD_TYPE
;
7743 handle_openflow(struct ofconn
*ofconn
, const struct ofpbuf
*ofp_msg
)
7744 OVS_EXCLUDED(ofproto_mutex
)
7746 enum ofperr error
= handle_openflow__(ofconn
, ofp_msg
);
7749 ofconn_send_error(ofconn
, ofp_msg
->data
, error
);
7751 COVERAGE_INC(ofproto_recv_openflow
);
7754 /* Asynchronous operations. */
7757 send_buffered_packet(const struct openflow_mod_requester
*req
,
7758 uint32_t buffer_id
, struct rule
*rule
)
7759 OVS_REQUIRES(ofproto_mutex
)
7761 if (req
&& req
->ofconn
&& buffer_id
!= UINT32_MAX
) {
7762 struct ofproto
*ofproto
= ofconn_get_ofproto(req
->ofconn
);
7763 struct dp_packet
*packet
;
7767 error
= ofconn_pktbuf_retrieve(req
->ofconn
, buffer_id
, &packet
,
7770 struct rule_execute
*re
;
7772 ofproto_rule_ref(rule
);
7774 re
= xmalloc(sizeof *re
);
7776 re
->in_port
= in_port
;
7777 re
->packet
= packet
;
7779 if (!guarded_list_push_back(&ofproto
->rule_executes
,
7780 &re
->list_node
, 1024)) {
7781 ofproto_rule_unref(rule
);
7782 dp_packet_delete(re
->packet
);
7786 ofconn_send_error(req
->ofconn
, req
->request
, error
);
7792 pick_datapath_id(const struct ofproto
*ofproto
)
7794 const struct ofport
*port
;
7796 port
= ofproto_get_port(ofproto
, OFPP_LOCAL
);
7801 error
= netdev_get_etheraddr(port
->netdev
, &ea
);
7803 return eth_addr_to_uint64(ea
);
7805 VLOG_WARN("%s: could not get MAC address for %s (%s)",
7806 ofproto
->name
, netdev_get_name(port
->netdev
),
7807 ovs_strerror(error
));
7809 return ofproto
->fallback_dpid
;
7813 pick_fallback_dpid(void)
7816 eth_addr_nicira_random(&ea
);
7817 return eth_addr_to_uint64(ea
);
7820 /* Table overflow policy. */
7822 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
7823 * to NULL if the table is not configured to evict rules or if the table
7824 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
7825 * or with no timeouts are not evictable.) */
7827 choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
7828 OVS_REQUIRES(ofproto_mutex
)
7830 struct eviction_group
*evg
;
7833 if (!table
->eviction
) {
7837 /* In the common case, the outer and inner loops here will each be entered
7840 * - The inner loop normally "return"s in its first iteration. If the
7841 * eviction group has any evictable rules, then it always returns in
7844 * - The outer loop only iterates more than once if the largest eviction
7845 * group has no evictable rules.
7847 * - The outer loop can exit only if table's 'max_flows' is all filled up
7848 * by unevictable rules. */
7849 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
7852 HEAP_FOR_EACH (rule
, evg_node
, &evg
->rules
) {
7861 /* Eviction groups. */
7863 /* Returns the priority to use for an eviction_group that contains 'n_rules'
7864 * rules. The priority contains low-order random bits to ensure that eviction
7865 * groups with the same number of rules are prioritized randomly. */
7867 eviction_group_priority(size_t n_rules
)
7869 uint16_t size
= MIN(UINT16_MAX
, n_rules
);
7870 return (size
<< 16) | random_uint16();
7873 /* Updates 'evg', an eviction_group within 'table', following a change that
7874 * adds or removes rules in 'evg'. */
7876 eviction_group_resized(struct oftable
*table
, struct eviction_group
*evg
)
7877 OVS_REQUIRES(ofproto_mutex
)
7879 heap_change(&table
->eviction_groups_by_size
, &evg
->size_node
,
7880 eviction_group_priority(heap_count(&evg
->rules
)));
7883 /* Destroys 'evg', an eviction_group within 'table':
7885 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
7886 * rules themselves, just removes them from the eviction group.)
7888 * - Removes 'evg' from 'table'.
7892 eviction_group_destroy(struct oftable
*table
, struct eviction_group
*evg
)
7893 OVS_REQUIRES(ofproto_mutex
)
7895 while (!heap_is_empty(&evg
->rules
)) {
7898 rule
= CONTAINER_OF(heap_pop(&evg
->rules
), struct rule
, evg_node
);
7899 rule
->eviction_group
= NULL
;
7901 hmap_remove(&table
->eviction_groups_by_id
, &evg
->id_node
);
7902 heap_remove(&table
->eviction_groups_by_size
, &evg
->size_node
);
7903 heap_destroy(&evg
->rules
);
7907 /* Removes 'rule' from its eviction group, if any. */
7909 eviction_group_remove_rule(struct rule
*rule
)
7910 OVS_REQUIRES(ofproto_mutex
)
7912 if (rule
->eviction_group
) {
7913 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
7914 struct eviction_group
*evg
= rule
->eviction_group
;
7916 rule
->eviction_group
= NULL
;
7917 heap_remove(&evg
->rules
, &rule
->evg_node
);
7918 if (heap_is_empty(&evg
->rules
)) {
7919 eviction_group_destroy(table
, evg
);
7921 eviction_group_resized(table
, evg
);
7926 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
7927 * returns the hash value. */
7929 eviction_group_hash_rule(struct rule
*rule
)
7930 OVS_REQUIRES(ofproto_mutex
)
7932 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
7933 const struct mf_subfield
*sf
;
7937 hash
= table
->eviction_group_id_basis
;
7938 miniflow_expand(rule
->cr
.match
.flow
, &flow
);
7939 for (sf
= table
->eviction_fields
;
7940 sf
< &table
->eviction_fields
[table
->n_eviction_fields
];
7943 if (mf_are_prereqs_ok(sf
->field
, &flow
, NULL
)) {
7944 union mf_value value
;
7946 mf_get_value(sf
->field
, &flow
, &value
);
7948 bitwise_zero(&value
, sf
->field
->n_bytes
, 0, sf
->ofs
);
7950 if (sf
->ofs
+ sf
->n_bits
< sf
->field
->n_bytes
* 8) {
7951 unsigned int start
= sf
->ofs
+ sf
->n_bits
;
7952 bitwise_zero(&value
, sf
->field
->n_bytes
, start
,
7953 sf
->field
->n_bytes
* 8 - start
);
7955 hash
= hash_bytes(&value
, sf
->field
->n_bytes
, hash
);
7957 hash
= hash_int(hash
, 0);
7964 /* Returns an eviction group within 'table' with the given 'id', creating one
7966 static struct eviction_group
*
7967 eviction_group_find(struct oftable
*table
, uint32_t id
)
7968 OVS_REQUIRES(ofproto_mutex
)
7970 struct eviction_group
*evg
;
7972 HMAP_FOR_EACH_WITH_HASH (evg
, id_node
, id
, &table
->eviction_groups_by_id
) {
7976 evg
= xmalloc(sizeof *evg
);
7977 hmap_insert(&table
->eviction_groups_by_id
, &evg
->id_node
, id
);
7978 heap_insert(&table
->eviction_groups_by_size
, &evg
->size_node
,
7979 eviction_group_priority(0));
7980 heap_init(&evg
->rules
);
7985 /* Returns an eviction priority for 'rule'. The return value should be
7986 * interpreted so that higher priorities make a rule a more attractive
7987 * candidate for eviction. */
7989 rule_eviction_priority(struct ofproto
*ofproto
, struct rule
*rule
)
7990 OVS_REQUIRES(ofproto_mutex
)
7992 /* Calculate absolute time when this flow will expire. If it will never
7993 * expire, then return 0 to make it unevictable. */
7994 long long int expiration
= LLONG_MAX
;
7995 if (rule
->hard_timeout
) {
7996 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
7997 ovs_mutex_lock(&rule
->mutex
);
7998 long long int modified
= rule
->modified
;
7999 ovs_mutex_unlock(&rule
->mutex
);
8001 expiration
= modified
+ rule
->hard_timeout
* 1000;
8003 if (rule
->idle_timeout
) {
8004 uint64_t packets
, bytes
;
8006 long long int idle_expiration
;
8008 ofproto
->ofproto_class
->rule_get_stats(rule
, &packets
, &bytes
, &used
);
8009 idle_expiration
= used
+ rule
->idle_timeout
* 1000;
8010 expiration
= MIN(expiration
, idle_expiration
);
8012 if (expiration
== LLONG_MAX
) {
8016 /* Calculate the time of expiration as a number of (approximate) seconds
8017 * after program startup.
8019 * This should work OK for program runs that last UINT32_MAX seconds or
8020 * less. Therefore, please restart OVS at least once every 136 years. */
8021 uint32_t expiration_ofs
= (expiration
>> 10) - (time_boot_msec() >> 10);
8023 /* Combine expiration time with OpenFlow "importance" to form a single
8024 * priority value. We want flows with relatively low "importance" to be
8025 * evicted before even considering expiration time, so put "importance" in
8026 * the most significant bits and expiration time in the least significant
8029 * Small 'priority' should be evicted before those with large 'priority'.
8030 * The caller expects the opposite convention (a large return value being
8031 * more attractive for eviction) so we invert it before returning. */
8032 uint64_t priority
= ((uint64_t) rule
->importance
<< 32) + expiration_ofs
;
8033 return UINT64_MAX
- priority
;
8036 /* Adds 'rule' to an appropriate eviction group for its oftable's
8037 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
8038 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
8041 * The caller must ensure that 'rule' is not already in an eviction group. */
8043 eviction_group_add_rule(struct rule
*rule
)
8044 OVS_REQUIRES(ofproto_mutex
)
8046 struct ofproto
*ofproto
= rule
->ofproto
;
8047 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
8050 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
8051 * so no additional protection is needed. */
8052 has_timeout
= rule
->hard_timeout
|| rule
->idle_timeout
;
8054 if (table
->eviction
&& has_timeout
) {
8055 struct eviction_group
*evg
;
8057 evg
= eviction_group_find(table
, eviction_group_hash_rule(rule
));
8059 rule
->eviction_group
= evg
;
8060 heap_insert(&evg
->rules
, &rule
->evg_node
,
8061 rule_eviction_priority(ofproto
, rule
));
8062 eviction_group_resized(table
, evg
);
8068 /* Initializes 'table'. */
8070 oftable_init(struct oftable
*table
)
8072 memset(table
, 0, sizeof *table
);
8073 classifier_init(&table
->cls
, flow_segment_u64s
);
8074 table
->max_flows
= UINT_MAX
;
8076 hmap_init(&table
->eviction_groups_by_id
);
8077 heap_init(&table
->eviction_groups_by_size
);
8078 atomic_init(&table
->miss_config
, OFPUTIL_TABLE_MISS_DEFAULT
);
8080 classifier_set_prefix_fields(&table
->cls
, default_prefix_fields
,
8081 ARRAY_SIZE(default_prefix_fields
));
8083 atomic_init(&table
->n_matched
, 0);
8084 atomic_init(&table
->n_missed
, 0);
8087 /* Destroys 'table', including its classifier and eviction groups.
8089 * The caller is responsible for freeing 'table' itself. */
8091 oftable_destroy(struct oftable
*table
)
8093 ovs_assert(classifier_is_empty(&table
->cls
));
8095 ovs_mutex_lock(&ofproto_mutex
);
8096 oftable_configure_eviction(table
, 0, NULL
, 0);
8097 ovs_mutex_unlock(&ofproto_mutex
);
8099 hmap_destroy(&table
->eviction_groups_by_id
);
8100 heap_destroy(&table
->eviction_groups_by_size
);
8101 classifier_destroy(&table
->cls
);
8105 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
8106 * string, then 'table' will use its default name.
8108 * This only affects the name exposed for a table exposed through the OpenFlow
8109 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
8111 oftable_set_name(struct oftable
*table
, const char *name
)
8113 if (name
&& name
[0]) {
8114 int len
= strnlen(name
, OFP_MAX_TABLE_NAME_LEN
);
8115 if (!table
->name
|| strncmp(name
, table
->name
, len
)) {
8117 table
->name
= xmemdup0(name
, len
);
8125 /* oftables support a choice of two policies when adding a rule would cause the
8126 * number of flows in the table to exceed the configured maximum number: either
8127 * they can refuse to add the new flow or they can evict some existing flow.
8128 * This function configures the latter policy on 'table', with fairness based
8129 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
8130 * 'n_fields' as 0 disables fairness.) */
8132 oftable_configure_eviction(struct oftable
*table
, unsigned int eviction
,
8133 const struct mf_subfield
*fields
, size_t n_fields
)
8134 OVS_REQUIRES(ofproto_mutex
)
8138 if ((table
->eviction
!= 0) == (eviction
!= 0)
8139 && n_fields
== table
->n_eviction_fields
8141 || !memcmp(fields
, table
->eviction_fields
,
8142 n_fields
* sizeof *fields
))) {
8143 /* The set of eviction fields did not change. If 'eviction' changed,
8144 * it remains nonzero, so that we can just update table->eviction
8145 * without fussing with the eviction groups. */
8146 table
->eviction
= eviction
;
8150 /* Destroy existing eviction groups, then destroy and recreate data
8151 * structures to recover memory. */
8152 struct eviction_group
*evg
, *next
;
8153 HMAP_FOR_EACH_SAFE (evg
, next
, id_node
, &table
->eviction_groups_by_id
) {
8154 eviction_group_destroy(table
, evg
);
8156 hmap_destroy(&table
->eviction_groups_by_id
);
8157 hmap_init(&table
->eviction_groups_by_id
);
8158 heap_destroy(&table
->eviction_groups_by_size
);
8159 heap_init(&table
->eviction_groups_by_size
);
8161 /* Replace eviction groups by the new ones, if there is a change. Free the
8162 * old fields only after allocating the new ones, because 'fields ==
8163 * table->eviction_fields' is possible. */
8164 struct mf_subfield
*old_fields
= table
->eviction_fields
;
8165 table
->n_eviction_fields
= n_fields
;
8166 table
->eviction_fields
= (fields
8167 ? xmemdup(fields
, n_fields
* sizeof *fields
)
8171 /* Add the new eviction groups, if enabled. */
8172 table
->eviction
= eviction
;
8173 if (table
->eviction
) {
8174 table
->eviction_group_id_basis
= random_uint32();
8175 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
8176 eviction_group_add_rule(rule
);
8181 /* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted
8182 * it to the classifier. */
8184 ofproto_rule_insert__(struct ofproto
*ofproto
, struct rule
*rule
)
8185 OVS_REQUIRES(ofproto_mutex
)
8187 const struct rule_actions
*actions
= rule_get_actions(rule
);
8189 ovs_assert(rule
->removed
);
8191 if (rule
->hard_timeout
|| rule
->idle_timeout
) {
8192 ovs_list_insert(&ofproto
->expirable
, &rule
->expirable
);
8194 cookies_insert(ofproto
, rule
);
8195 eviction_group_add_rule(rule
);
8196 if (actions
->has_meter
) {
8197 meter_insert_rule(rule
);
8199 if (actions
->has_groups
) {
8200 const struct ofpact_group
*a
;
8201 OFPACT_FOR_EACH_TYPE_FLATTENED (a
, GROUP
, actions
->ofpacts
,
8202 actions
->ofpacts_len
) {
8203 struct ofgroup
*group
;
8205 group
= ofproto_group_lookup(ofproto
, a
->group_id
, OVS_VERSION_MAX
,
8207 ovs_assert(group
!= NULL
);
8208 group_add_rule(group
, rule
);
8212 rule
->removed
= false;
8215 /* Removes 'rule' from the ofproto data structures. Caller may have deferred
8216 * the removal from the classifier. */
8218 ofproto_rule_remove__(struct ofproto
*ofproto
, struct rule
*rule
)
8219 OVS_REQUIRES(ofproto_mutex
)
8221 ovs_assert(!rule
->removed
);
8223 cookies_remove(ofproto
, rule
);
8225 eviction_group_remove_rule(rule
);
8226 if (!ovs_list_is_empty(&rule
->expirable
)) {
8227 ovs_list_remove(&rule
->expirable
);
8229 if (!ovs_list_is_empty(&rule
->meter_list_node
)) {
8230 ovs_list_remove(&rule
->meter_list_node
);
8231 ovs_list_init(&rule
->meter_list_node
);
8234 /* Remove the rule from any groups, except from the group that is being
8235 * deleted, if any. */
8236 const struct rule_actions
*actions
= rule_get_actions(rule
);
8238 if (actions
->has_groups
) {
8239 const struct ofpact_group
*a
;
8241 OFPACT_FOR_EACH_TYPE (a
, GROUP
, actions
->ofpacts
,
8242 actions
->ofpacts_len
) {
8243 struct ofgroup
*group
;
8245 group
= ofproto_group_lookup(ofproto
, a
->group_id
, OVS_VERSION_MAX
,
8249 /* Leave the rule for the group that is being deleted, if any,
8250 * as we still need the list of rules for clean-up. */
8251 if (!group
->being_deleted
) {
8252 group_remove_rule(group
, rule
);
8257 rule
->removed
= true;
8260 /* unixctl commands. */
8263 ofproto_lookup(const char *name
)
8265 struct ofproto
*ofproto
;
8267 HMAP_FOR_EACH_WITH_HASH (ofproto
, hmap_node
, hash_string(name
, 0),
8269 if (!strcmp(ofproto
->name
, name
)) {
8277 ofproto_unixctl_list(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
8278 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
8280 struct ofproto
*ofproto
;
8284 HMAP_FOR_EACH (ofproto
, hmap_node
, &all_ofprotos
) {
8285 ds_put_format(&results
, "%s\n", ofproto
->name
);
8287 unixctl_command_reply(conn
, ds_cstr(&results
));
8288 ds_destroy(&results
);
8292 ofproto_unixctl_init(void)
8294 static bool registered
;
8300 unixctl_command_register("ofproto/list", "", 0, 0,
8301 ofproto_unixctl_list
, NULL
);