2 * Copyright (c) 2009-2017 Nicira, Inc.
3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "connectivity.h"
32 #include "dp-packet.h"
34 #include "openvswitch/hmap.h"
38 #include "ofproto-provider.h"
39 #include "openflow/nicira-ext.h"
40 #include "openflow/openflow.h"
41 #include "openvswitch/dynamic-string.h"
42 #include "openvswitch/meta-flow.h"
43 #include "openvswitch/ofp-actions.h"
44 #include "openvswitch/ofp-bundle.h"
45 #include "openvswitch/ofp-errors.h"
46 #include "openvswitch/ofp-match.h"
47 #include "openvswitch/ofp-msgs.h"
48 #include "openvswitch/ofp-monitor.h"
49 #include "openvswitch/ofp-print.h"
50 #include "openvswitch/ofp-queue.h"
51 #include "openvswitch/ofp-util.h"
52 #include "openvswitch/ofpbuf.h"
53 #include "openvswitch/vlog.h"
57 #include "openvswitch/poll-loop.h"
60 #include "openvswitch/shash.h"
65 #include "tun-metadata.h"
66 #include "unaligned.h"
70 VLOG_DEFINE_THIS_MODULE(ofproto
);
72 COVERAGE_DEFINE(ofproto_flush
);
73 COVERAGE_DEFINE(ofproto_packet_out
);
74 COVERAGE_DEFINE(ofproto_queue_req
);
75 COVERAGE_DEFINE(ofproto_recv_openflow
);
76 COVERAGE_DEFINE(ofproto_reinit_ports
);
77 COVERAGE_DEFINE(ofproto_update_port
);
79 /* Default fields to use for prefix tries in each flow table, unless something
80 * else is configured. */
81 const enum mf_field_id default_prefix_fields
[2] =
82 { MFF_IPV4_DST
, MFF_IPV4_SRC
};
85 static void oftable_init(struct oftable
*);
86 static void oftable_destroy(struct oftable
*);
88 static void oftable_set_name(struct oftable
*, const char *name
);
90 static enum ofperr
evict_rules_from_table(struct oftable
*)
91 OVS_REQUIRES(ofproto_mutex
);
92 static void oftable_configure_eviction(struct oftable
*,
93 unsigned int eviction
,
94 const struct mf_subfield
*fields
,
96 OVS_REQUIRES(ofproto_mutex
);
98 /* This is the only combination of OpenFlow eviction flags that OVS supports: a
99 * combination of OF1.4+ importance, the remaining lifetime of the flow, and
100 * fairness based on user-specified fields. */
101 #define OFPROTO_EVICTION_FLAGS \
102 (OFPTMPEF14_OTHER | OFPTMPEF14_IMPORTANCE | OFPTMPEF14_LIFETIME)
104 /* A set of rules within a single OpenFlow table (oftable) that have the same
105 * values for the oftable's eviction_fields. A rule to be evicted, when one is
106 * needed, is taken from the eviction group that contains the greatest number
109 * An oftable owns any number of eviction groups, each of which contains any
112 * Membership in an eviction group is imprecise, based on the hash of the
113 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
114 * That is, if two rules have different eviction_fields, but those
115 * eviction_fields hash to the same value, then they will belong to the same
116 * eviction_group anyway.
118 * (When eviction is not enabled on an oftable, we don't track any eviction
119 * groups, to save time and space.) */
120 struct eviction_group
{
121 struct hmap_node id_node
; /* In oftable's "eviction_groups_by_id". */
122 struct heap_node size_node
; /* In oftable's "eviction_groups_by_size". */
123 struct heap rules
; /* Contains "struct rule"s. */
126 static bool choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
127 OVS_REQUIRES(ofproto_mutex
);
128 static uint64_t rule_eviction_priority(struct ofproto
*ofproto
, struct rule
*)
129 OVS_REQUIRES(ofproto_mutex
);
130 static void eviction_group_add_rule(struct rule
*)
131 OVS_REQUIRES(ofproto_mutex
);
132 static void eviction_group_remove_rule(struct rule
*)
133 OVS_REQUIRES(ofproto_mutex
);
135 static void rule_criteria_init(struct rule_criteria
*, uint8_t table_id
,
136 const struct minimatch
*match
, int priority
,
137 ovs_version_t version
,
138 ovs_be64 cookie
, ovs_be64 cookie_mask
,
139 ofp_port_t out_port
, uint32_t out_group
);
140 static void rule_criteria_require_rw(struct rule_criteria
*,
141 bool can_write_readonly
);
142 static void rule_criteria_destroy(struct rule_criteria
*);
144 static enum ofperr
collect_rules_loose(struct ofproto
*,
145 const struct rule_criteria
*,
146 struct rule_collection
*);
148 struct learned_cookie
{
150 /* In struct ofproto's 'learned_cookies' hmap. */
151 struct hmap_node hmap_node
OVS_GUARDED_BY(ofproto_mutex
);
153 /* In 'dead_cookies' list when removed from hmap. */
154 struct ovs_list list_node
;
158 ovs_be64 cookie
OVS_GUARDED_BY(ofproto_mutex
);
159 uint8_t table_id
OVS_GUARDED_BY(ofproto_mutex
);
161 /* Number of references from "learn" actions.
163 * When this drops to 0, all of the flows in 'table_id' with the specified
164 * 'cookie' are deleted. */
165 int n
OVS_GUARDED_BY(ofproto_mutex
);
168 static const struct ofpact_learn
*next_learn_with_delete(
169 const struct rule_actions
*, const struct ofpact_learn
*start
);
171 static void learned_cookies_inc(struct ofproto
*, const struct rule_actions
*)
172 OVS_REQUIRES(ofproto_mutex
);
173 static void learned_cookies_dec(struct ofproto
*, const struct rule_actions
*,
174 struct ovs_list
*dead_cookies
)
175 OVS_REQUIRES(ofproto_mutex
);
176 static void learned_cookies_flush(struct ofproto
*, struct ovs_list
*dead_cookies
)
177 OVS_REQUIRES(ofproto_mutex
);
180 static void ofport_destroy__(struct ofport
*) OVS_EXCLUDED(ofproto_mutex
);
181 static void ofport_destroy(struct ofport
*, bool del
);
182 static bool ofport_is_mtu_overridden(const struct ofproto
*,
183 const struct ofport
*);
185 static int update_port(struct ofproto
*, const char *devname
);
186 static int init_ports(struct ofproto
*);
187 static void reinit_ports(struct ofproto
*);
189 static long long int ofport_get_usage(const struct ofproto
*,
190 ofp_port_t ofp_port
);
191 static void ofport_set_usage(struct ofproto
*, ofp_port_t ofp_port
,
192 long long int last_used
);
193 static void ofport_remove_usage(struct ofproto
*, ofp_port_t ofp_port
);
197 * Keeps track of the currently used and recently used ofport values and is
198 * used to prevent immediate recycling of ofport values. */
199 struct ofport_usage
{
200 struct hmap_node hmap_node
; /* In struct ofproto's "ofport_usage" hmap. */
201 ofp_port_t ofp_port
; /* OpenFlow port number. */
202 long long int last_used
; /* Last time the 'ofp_port' was used. LLONG_MAX
203 represents in-use ofports. */
207 static void ofproto_rule_send_removed(struct rule
*)
208 OVS_EXCLUDED(ofproto_mutex
);
209 static bool rule_is_readonly(const struct rule
*);
210 static void ofproto_rule_insert__(struct ofproto
*, struct rule
*)
211 OVS_REQUIRES(ofproto_mutex
);
212 static void ofproto_rule_remove__(struct ofproto
*, struct rule
*)
213 OVS_REQUIRES(ofproto_mutex
);
215 /* The source of an OpenFlow request.
217 * A table modification request can be generated externally, via OpenFlow, or
218 * internally through a function call. This structure indicates the source of
219 * an OpenFlow-generated table modification. For an internal flow_mod, it
220 * isn't meaningful and thus supplied as NULL. */
221 struct openflow_mod_requester
{
222 struct ofconn
*ofconn
; /* Connection on which flow_mod arrived. */
223 const struct ofp_header
*request
;
227 static enum ofperr
ofproto_rule_create(struct ofproto
*, struct cls_rule
*,
228 uint8_t table_id
, ovs_be64 new_cookie
,
229 uint16_t idle_timeout
,
230 uint16_t hard_timeout
,
231 enum ofputil_flow_mod_flags flags
,
233 const struct ofpact
*ofpacts
,
235 uint64_t match_tlv_bitmap
,
236 uint64_t ofpacts_tlv_bitmap
,
237 struct rule
**new_rule
)
238 OVS_NO_THREAD_SAFETY_ANALYSIS
;
240 static void replace_rule_start(struct ofproto
*, struct ofproto_flow_mod
*,
241 struct rule
*old_rule
, struct rule
*new_rule
)
242 OVS_REQUIRES(ofproto_mutex
);
244 static void replace_rule_revert(struct ofproto
*, struct rule
*old_rule
,
245 struct rule
*new_rule
)
246 OVS_REQUIRES(ofproto_mutex
);
248 static void replace_rule_finish(struct ofproto
*, struct ofproto_flow_mod
*,
249 const struct openflow_mod_requester
*,
250 struct rule
*old_rule
, struct rule
*new_rule
,
251 struct ovs_list
*dead_cookies
)
252 OVS_REQUIRES(ofproto_mutex
);
253 static void delete_flows__(struct rule_collection
*,
254 enum ofp_flow_removed_reason
,
255 const struct openflow_mod_requester
*)
256 OVS_REQUIRES(ofproto_mutex
);
258 static void ofproto_group_delete_all__(struct ofproto
*)
259 OVS_REQUIRES(ofproto_mutex
);
260 static bool ofproto_group_exists(const struct ofproto
*, uint32_t group_id
);
261 static void handle_openflow(struct ofconn
*, const struct ofpbuf
*);
262 static enum ofperr
ofproto_flow_mod_init(struct ofproto
*,
263 struct ofproto_flow_mod
*,
264 const struct ofputil_flow_mod
*fm
,
266 OVS_EXCLUDED(ofproto_mutex
);
267 static enum ofperr
ofproto_flow_mod_start(struct ofproto
*,
268 struct ofproto_flow_mod
*)
269 OVS_REQUIRES(ofproto_mutex
);
270 static void ofproto_flow_mod_revert(struct ofproto
*,
271 struct ofproto_flow_mod
*)
272 OVS_REQUIRES(ofproto_mutex
);
273 static void ofproto_flow_mod_finish(struct ofproto
*,
274 struct ofproto_flow_mod
*,
275 const struct openflow_mod_requester
*)
276 OVS_REQUIRES(ofproto_mutex
);
277 static enum ofperr
handle_flow_mod__(struct ofproto
*,
278 const struct ofputil_flow_mod
*,
279 const struct openflow_mod_requester
*)
280 OVS_EXCLUDED(ofproto_mutex
);
281 static void calc_duration(long long int start
, long long int now
,
282 uint32_t *sec
, uint32_t *nsec
);
285 static uint64_t pick_datapath_id(const struct ofproto
*);
286 static uint64_t pick_fallback_dpid(void);
287 static void ofproto_destroy__(struct ofproto
*);
288 static void update_mtu(struct ofproto
*, struct ofport
*);
289 static void update_mtu_ofproto(struct ofproto
*);
290 static void meter_delete(struct ofproto
*, uint32_t);
291 static void meter_delete_all(struct ofproto
*);
292 static void meter_insert_rule(struct rule
*);
295 static void ofproto_unixctl_init(void);
297 /* All registered ofproto classes, in probe order. */
298 static const struct ofproto_class
**ofproto_classes
;
299 static size_t n_ofproto_classes
;
300 static size_t allocated_ofproto_classes
;
302 /* Global lock that protects all flow table operations. */
303 struct ovs_mutex ofproto_mutex
= OVS_MUTEX_INITIALIZER
;
305 unsigned ofproto_flow_limit
= OFPROTO_FLOW_LIMIT_DEFAULT
;
306 unsigned ofproto_max_idle
= OFPROTO_MAX_IDLE_DEFAULT
;
308 size_t n_handlers
, n_revalidators
;
310 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
311 static struct hmap all_ofprotos
= HMAP_INITIALIZER(&all_ofprotos
);
313 /* Initial mappings of port to OpenFlow number mappings. */
314 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
316 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
318 /* The default value of true waits for flow restore. */
319 static bool flow_restore_wait
= true;
321 /* Must be called to initialize the ofproto library.
323 * The caller may pass in 'iface_hints', which contains an shash of
324 * "iface_hint" elements indexed by the interface's name. The provider
325 * may use these hints to describe the startup configuration in order to
326 * reinitialize its state. The caller owns the provided data, so a
327 * provider will make copies of anything required. An ofproto provider
328 * will remove any existing state that is not described by the hint, and
329 * may choose to remove it all. */
331 ofproto_init(const struct shash
*iface_hints
)
333 struct shash_node
*node
;
336 ofproto_class_register(&ofproto_dpif_class
);
338 /* Make a local copy, since we don't own 'iface_hints' elements. */
339 SHASH_FOR_EACH(node
, iface_hints
) {
340 const struct iface_hint
*orig_hint
= node
->data
;
341 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
342 const char *br_type
= ofproto_normalize_type(orig_hint
->br_type
);
344 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
345 new_hint
->br_type
= xstrdup(br_type
);
346 new_hint
->ofp_port
= orig_hint
->ofp_port
;
348 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
351 for (i
= 0; i
< n_ofproto_classes
; i
++) {
352 ofproto_classes
[i
]->init(&init_ofp_ports
);
355 ofproto_unixctl_init();
358 /* 'type' should be a normalized datapath type, as returned by
359 * ofproto_normalize_type(). Returns the corresponding ofproto_class
360 * structure, or a null pointer if there is none registered for 'type'. */
361 static const struct ofproto_class
*
362 ofproto_class_find__(const char *type
)
366 for (i
= 0; i
< n_ofproto_classes
; i
++) {
367 const struct ofproto_class
*class = ofproto_classes
[i
];
372 class->enumerate_types(&types
);
373 found
= sset_contains(&types
, type
);
374 sset_destroy(&types
);
380 VLOG_WARN("unknown datapath type %s", type
);
384 /* Registers a new ofproto class. After successful registration, new ofprotos
385 * of that type can be created using ofproto_create(). */
387 ofproto_class_register(const struct ofproto_class
*new_class
)
391 for (i
= 0; i
< n_ofproto_classes
; i
++) {
392 if (ofproto_classes
[i
] == new_class
) {
397 if (n_ofproto_classes
>= allocated_ofproto_classes
) {
398 ofproto_classes
= x2nrealloc(ofproto_classes
,
399 &allocated_ofproto_classes
,
400 sizeof *ofproto_classes
);
402 ofproto_classes
[n_ofproto_classes
++] = new_class
;
406 /* Unregisters a datapath provider. 'type' must have been previously
407 * registered and not currently be in use by any ofprotos. After
408 * unregistration new datapaths of that type cannot be opened using
409 * ofproto_create(). */
411 ofproto_class_unregister(const struct ofproto_class
*class)
415 for (i
= 0; i
< n_ofproto_classes
; i
++) {
416 if (ofproto_classes
[i
] == class) {
417 for (i
++; i
< n_ofproto_classes
; i
++) {
418 ofproto_classes
[i
- 1] = ofproto_classes
[i
];
424 VLOG_WARN("attempted to unregister an ofproto class that is not "
429 /* Clears 'types' and enumerates all registered ofproto types into it. The
430 * caller must first initialize the sset. */
432 ofproto_enumerate_types(struct sset
*types
)
437 for (i
= 0; i
< n_ofproto_classes
; i
++) {
438 ofproto_classes
[i
]->enumerate_types(types
);
442 /* Returns the fully spelled out name for the given ofproto 'type'.
444 * Normalized type string can be compared with strcmp(). Unnormalized type
445 * string might be the same even if they have different spellings. */
447 ofproto_normalize_type(const char *type
)
449 return type
&& type
[0] ? type
: "system";
452 /* Clears 'names' and enumerates the names of all known created ofprotos with
453 * the given 'type'. The caller must first initialize the sset. Returns 0 if
454 * successful, otherwise a positive errno value.
456 * Some kinds of datapaths might not be practically enumerable. This is not
457 * considered an error. */
459 ofproto_enumerate_names(const char *type
, struct sset
*names
)
461 const struct ofproto_class
*class = ofproto_class_find__(type
);
462 return class ? class->enumerate_names(type
, names
) : EAFNOSUPPORT
;
466 ofproto_bump_tables_version(struct ofproto
*ofproto
)
468 ++ofproto
->tables_version
;
469 ofproto
->ofproto_class
->set_tables_version(ofproto
,
470 ofproto
->tables_version
);
474 ofproto_create(const char *datapath_name
, const char *datapath_type
,
475 struct ofproto
**ofprotop
)
476 OVS_EXCLUDED(ofproto_mutex
)
478 const struct ofproto_class
*class;
479 struct ofproto
*ofproto
;
485 datapath_type
= ofproto_normalize_type(datapath_type
);
486 class = ofproto_class_find__(datapath_type
);
488 VLOG_WARN("could not create datapath %s of unknown type %s",
489 datapath_name
, datapath_type
);
493 ofproto
= class->alloc();
495 VLOG_ERR("failed to allocate datapath %s of type %s",
496 datapath_name
, datapath_type
);
501 ovs_mutex_lock(&ofproto_mutex
);
502 memset(ofproto
, 0, sizeof *ofproto
);
503 ofproto
->ofproto_class
= class;
504 ofproto
->name
= xstrdup(datapath_name
);
505 ofproto
->type
= xstrdup(datapath_type
);
506 hmap_insert(&all_ofprotos
, &ofproto
->hmap_node
,
507 hash_string(ofproto
->name
, 0));
508 ofproto
->datapath_id
= 0;
509 ofproto
->forward_bpdu
= false;
510 ofproto
->fallback_dpid
= pick_fallback_dpid();
511 ofproto
->mfr_desc
= NULL
;
512 ofproto
->hw_desc
= NULL
;
513 ofproto
->sw_desc
= NULL
;
514 ofproto
->serial_desc
= NULL
;
515 ofproto
->dp_desc
= NULL
;
516 ofproto
->frag_handling
= OFPUTIL_FRAG_NORMAL
;
517 hmap_init(&ofproto
->ports
);
518 hmap_init(&ofproto
->ofport_usage
);
519 shash_init(&ofproto
->port_by_name
);
520 simap_init(&ofproto
->ofp_requests
);
521 ofproto
->max_ports
= ofp_to_u16(OFPP_MAX
);
522 ofproto
->eviction_group_timer
= LLONG_MIN
;
523 ofproto
->tables
= NULL
;
524 ofproto
->n_tables
= 0;
525 ofproto
->tables_version
= OVS_VERSION_MIN
;
526 hindex_init(&ofproto
->cookies
);
527 hmap_init(&ofproto
->learned_cookies
);
528 ovs_list_init(&ofproto
->expirable
);
529 ofproto
->connmgr
= connmgr_create(ofproto
, datapath_name
, datapath_name
);
530 ofproto
->min_mtu
= INT_MAX
;
531 cmap_init(&ofproto
->groups
);
532 ovs_mutex_unlock(&ofproto_mutex
);
533 ofproto
->ogf
.types
= 0xf;
534 ofproto
->ogf
.capabilities
= OFPGFC_CHAINING
| OFPGFC_SELECT_LIVENESS
|
535 OFPGFC_SELECT_WEIGHT
;
536 for (i
= 0; i
< 4; i
++) {
537 ofproto
->ogf
.max_groups
[i
] = OFPG_MAX
;
538 ofproto
->ogf
.ofpacts
[i
] = (UINT64_C(1) << N_OFPACTS
) - 1;
540 ovsrcu_set(&ofproto
->metadata_tab
, tun_metadata_alloc(NULL
));
542 ovs_mutex_init(&ofproto
->vl_mff_map
.mutex
);
543 cmap_init(&ofproto
->vl_mff_map
.cmap
);
545 error
= ofproto
->ofproto_class
->construct(ofproto
);
547 VLOG_ERR("failed to open datapath %s: %s",
548 datapath_name
, ovs_strerror(error
));
549 ovs_mutex_lock(&ofproto_mutex
);
550 connmgr_destroy(ofproto
->connmgr
);
551 ofproto
->connmgr
= NULL
;
552 ovs_mutex_unlock(&ofproto_mutex
);
553 ofproto_destroy__(ofproto
);
557 /* Check that hidden tables, if any, are at the end. */
558 ovs_assert(ofproto
->n_tables
);
559 for (i
= 0; i
+ 1 < ofproto
->n_tables
; i
++) {
560 enum oftable_flags flags
= ofproto
->tables
[i
].flags
;
561 enum oftable_flags next_flags
= ofproto
->tables
[i
+ 1].flags
;
563 ovs_assert(!(flags
& OFTABLE_HIDDEN
) || next_flags
& OFTABLE_HIDDEN
);
566 ofproto
->datapath_id
= pick_datapath_id(ofproto
);
569 /* Initialize meters table. */
570 if (ofproto
->ofproto_class
->meter_get_features
) {
571 ofproto
->ofproto_class
->meter_get_features(ofproto
,
572 &ofproto
->meter_features
);
574 memset(&ofproto
->meter_features
, 0, sizeof ofproto
->meter_features
);
576 hmap_init(&ofproto
->meters
);
577 ofproto
->slowpath_meter_id
= UINT32_MAX
;
578 ofproto
->controller_meter_id
= UINT32_MAX
;
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 bundle max idle timeout */
685 ofproto_set_bundle_idle_timeout(unsigned timeout
)
687 connmgr_set_bundle_idle_timeout(timeout
);
690 /* Sets the number of flows at which eviction from the kernel flow table
693 ofproto_set_flow_limit(unsigned limit
)
695 ofproto_flow_limit
= limit
;
698 /* Sets the maximum idle time for flows in the datapath before they are
701 ofproto_set_max_idle(unsigned max_idle
)
703 ofproto_max_idle
= max_idle
;
706 /* If forward_bpdu is true, the NORMAL action will forward frames with
707 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
708 * the NORMAL action will drop these frames. */
710 ofproto_set_forward_bpdu(struct ofproto
*ofproto
, bool forward_bpdu
)
712 bool old_val
= ofproto
->forward_bpdu
;
713 ofproto
->forward_bpdu
= forward_bpdu
;
714 if (old_val
!= ofproto
->forward_bpdu
) {
715 if (ofproto
->ofproto_class
->forward_bpdu_changed
) {
716 ofproto
->ofproto_class
->forward_bpdu_changed(ofproto
);
721 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
722 * 'idle_time', in seconds, and the maximum number of MAC table entries to
725 ofproto_set_mac_table_config(struct ofproto
*ofproto
, unsigned idle_time
,
728 if (ofproto
->ofproto_class
->set_mac_table_config
) {
729 ofproto
->ofproto_class
->set_mac_table_config(ofproto
, idle_time
,
734 /* Multicast snooping configuration. */
736 /* Configures multicast snooping on 'ofproto' using the settings
737 * defined in 's'. If 's' is NULL, disables multicast snooping.
739 * Returns 0 if successful, otherwise a positive errno value. */
741 ofproto_set_mcast_snooping(struct ofproto
*ofproto
,
742 const struct ofproto_mcast_snooping_settings
*s
)
744 return (ofproto
->ofproto_class
->set_mcast_snooping
745 ? ofproto
->ofproto_class
->set_mcast_snooping(ofproto
, s
)
749 /* Configures multicast snooping flood settings on 'ofp_port' of 'ofproto'.
751 * Returns 0 if successful, otherwise a positive errno value.*/
753 ofproto_port_set_mcast_snooping(struct ofproto
*ofproto
, void *aux
,
754 const struct ofproto_mcast_snooping_port_settings
*s
)
756 return (ofproto
->ofproto_class
->set_mcast_snooping_port
757 ? ofproto
->ofproto_class
->set_mcast_snooping_port(ofproto
, aux
, s
)
762 ofproto_type_set_config(const char *datapath_type
, const struct smap
*cfg
)
764 const struct ofproto_class
*class;
766 datapath_type
= ofproto_normalize_type(datapath_type
);
767 class = ofproto_class_find__(datapath_type
);
769 if (class->type_set_config
) {
770 class->type_set_config(datapath_type
, cfg
);
775 ofproto_set_threads(int n_handlers_
, int n_revalidators_
)
777 int threads
= MAX(count_cpu_cores(), 2);
779 n_revalidators
= MAX(n_revalidators_
, 0);
780 n_handlers
= MAX(n_handlers_
, 0);
782 if (!n_revalidators
) {
783 n_revalidators
= n_handlers
784 ? MAX(threads
- (int) n_handlers
, 1)
789 n_handlers
= MAX(threads
- (int) n_revalidators
, 1);
794 ofproto_set_dp_desc(struct ofproto
*p
, const char *dp_desc
)
797 p
->dp_desc
= nullable_xstrdup(dp_desc
);
801 ofproto_set_snoops(struct ofproto
*ofproto
, const struct sset
*snoops
)
803 return connmgr_set_snoops(ofproto
->connmgr
, snoops
);
807 ofproto_set_netflow(struct ofproto
*ofproto
,
808 const struct netflow_options
*nf_options
)
810 if (nf_options
&& sset_is_empty(&nf_options
->collectors
)) {
814 if (ofproto
->ofproto_class
->set_netflow
) {
815 return ofproto
->ofproto_class
->set_netflow(ofproto
, nf_options
);
817 return nf_options
? EOPNOTSUPP
: 0;
822 ofproto_set_sflow(struct ofproto
*ofproto
,
823 const struct ofproto_sflow_options
*oso
)
825 if (oso
&& sset_is_empty(&oso
->targets
)) {
829 if (ofproto
->ofproto_class
->set_sflow
) {
830 return ofproto
->ofproto_class
->set_sflow(ofproto
, oso
);
832 return oso
? EOPNOTSUPP
: 0;
837 ofproto_set_ipfix(struct ofproto
*ofproto
,
838 const struct ofproto_ipfix_bridge_exporter_options
*bo
,
839 const struct ofproto_ipfix_flow_exporter_options
*fo
,
842 if (ofproto
->ofproto_class
->set_ipfix
) {
843 return ofproto
->ofproto_class
->set_ipfix(ofproto
, bo
, fo
, n_fo
);
845 return (bo
|| fo
) ? EOPNOTSUPP
: 0;
850 ofproto_get_ipfix_stats(struct ofproto
*ofproto
,
852 struct ovs_list
*replies
)
856 if (ofproto
->ofproto_class
->get_ipfix_stats
) {
857 error
= ofproto
->ofproto_class
->get_ipfix_stats(ofproto
,
868 handle_ipfix_bridge_stats_request(struct ofconn
*ofconn
,
869 const struct ofp_header
*request
)
871 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
872 struct ovs_list replies
;
875 ofpmp_init(&replies
, request
);
876 error
= ofproto_get_ipfix_stats(ofproto
, true, &replies
);
879 ofconn_send_replies(ofconn
, &replies
);
881 ofpbuf_list_delete(&replies
);
888 handle_ipfix_flow_stats_request(struct ofconn
*ofconn
,
889 const struct ofp_header
*request
)
891 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
892 struct ovs_list replies
;
895 ofpmp_init(&replies
, request
);
896 error
= ofproto_get_ipfix_stats(ofproto
, false, &replies
);
899 ofconn_send_replies(ofconn
, &replies
);
901 ofpbuf_list_delete(&replies
);
908 handle_nxt_ct_flush_zone(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
910 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
911 const struct nx_zone_id
*nzi
= ofpmsg_body(oh
);
913 if (!is_all_zeros(nzi
->zero
, sizeof nzi
->zero
)) {
914 return OFPERR_NXBRC_MUST_BE_ZERO
;
917 uint16_t zone
= ntohs(nzi
->zone_id
);
918 if (ofproto
->ofproto_class
->ct_flush
) {
919 ofproto
->ofproto_class
->ct_flush(ofproto
, &zone
);
928 ofproto_set_flow_restore_wait(bool flow_restore_wait_db
)
930 flow_restore_wait
= flow_restore_wait_db
;
934 ofproto_get_flow_restore_wait(void)
936 return flow_restore_wait
;
940 /* Spanning Tree Protocol (STP) configuration. */
942 /* Configures STP on 'ofproto' using the settings defined in 's'. If
943 * 's' is NULL, disables STP.
945 * Returns 0 if successful, otherwise a positive errno value. */
947 ofproto_set_stp(struct ofproto
*ofproto
,
948 const struct ofproto_stp_settings
*s
)
950 return (ofproto
->ofproto_class
->set_stp
951 ? ofproto
->ofproto_class
->set_stp(ofproto
, s
)
955 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
956 * 'enabled' member of 's' is false, then the other members are not
959 * Returns 0 if successful, otherwise a positive errno value. */
961 ofproto_get_stp_status(struct ofproto
*ofproto
,
962 struct ofproto_stp_status
*s
)
964 return (ofproto
->ofproto_class
->get_stp_status
965 ? ofproto
->ofproto_class
->get_stp_status(ofproto
, s
)
969 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
970 * in 's'. The caller is responsible for assigning STP port numbers
971 * (using the 'port_num' member in the range of 1 through 255, inclusive)
972 * and ensuring there are no duplicates. If the 's' is NULL, then STP
973 * is disabled on the port.
975 * Returns 0 if successful, otherwise a positive errno value.*/
977 ofproto_port_set_stp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
978 const struct ofproto_port_stp_settings
*s
)
980 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
982 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu32
,
983 ofproto
->name
, ofp_port
);
987 return (ofproto
->ofproto_class
->set_stp_port
988 ? ofproto
->ofproto_class
->set_stp_port(ofport
, s
)
992 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
993 * 's'. If the 'enabled' member in 's' is false, then the other members
994 * are not meaningful.
996 * Returns 0 if successful, otherwise a positive errno value.*/
998 ofproto_port_get_stp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
999 struct ofproto_port_stp_status
*s
)
1001 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1003 VLOG_WARN_RL(&rl
, "%s: cannot get STP status on nonexistent "
1004 "port %"PRIu32
, ofproto
->name
, ofp_port
);
1008 return (ofproto
->ofproto_class
->get_stp_port_status
1009 ? ofproto
->ofproto_class
->get_stp_port_status(ofport
, s
)
1013 /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
1014 * 's'. If the 'enabled' member in 's' is false, then the other members
1015 * are not meaningful.
1017 * Returns 0 if successful, otherwise a positive errno value.*/
1019 ofproto_port_get_stp_stats(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1020 struct ofproto_port_stp_stats
*s
)
1022 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1024 VLOG_WARN_RL(&rl
, "%s: cannot get STP stats on nonexistent "
1025 "port %"PRIu32
, ofproto
->name
, ofp_port
);
1029 return (ofproto
->ofproto_class
->get_stp_port_stats
1030 ? ofproto
->ofproto_class
->get_stp_port_stats(ofport
, s
)
1034 /* Rapid Spanning Tree Protocol (RSTP) configuration. */
1036 /* Configures RSTP on 'ofproto' using the settings defined in 's'. If
1037 * 's' is NULL, disables RSTP.
1039 * Returns 0 if successful, otherwise a positive errno value. */
1041 ofproto_set_rstp(struct ofproto
*ofproto
,
1042 const struct ofproto_rstp_settings
*s
)
1044 if (!ofproto
->ofproto_class
->set_rstp
) {
1047 ofproto
->ofproto_class
->set_rstp(ofproto
, s
);
1051 /* Retrieves RSTP status of 'ofproto' and stores it in 's'. If the
1052 * 'enabled' member of 's' is false, then the other members are not
1055 * Returns 0 if successful, otherwise a positive errno value. */
1057 ofproto_get_rstp_status(struct ofproto
*ofproto
,
1058 struct ofproto_rstp_status
*s
)
1060 if (!ofproto
->ofproto_class
->get_rstp_status
) {
1063 ofproto
->ofproto_class
->get_rstp_status(ofproto
, s
);
1067 /* Configures RSTP on 'ofp_port' of 'ofproto' using the settings defined
1068 * in 's'. The caller is responsible for assigning RSTP port numbers
1069 * (using the 'port_num' member in the range of 1 through 255, inclusive)
1070 * and ensuring there are no duplicates. If the 's' is NULL, then RSTP
1071 * is disabled on the port.
1073 * Returns 0 if successful, otherwise a positive errno value.*/
1075 ofproto_port_set_rstp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1076 const struct ofproto_port_rstp_settings
*s
)
1078 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1080 VLOG_WARN("%s: cannot configure RSTP on nonexistent port %"PRIu32
,
1081 ofproto
->name
, ofp_port
);
1085 if (!ofproto
->ofproto_class
->set_rstp_port
) {
1088 ofproto
->ofproto_class
->set_rstp_port(ofport
, s
);
1092 /* Retrieves RSTP port status of 'ofp_port' on 'ofproto' and stores it in
1093 * 's'. If the 'enabled' member in 's' is false, then the other members
1094 * are not meaningful.
1096 * Returns 0 if successful, otherwise a positive errno value.*/
1098 ofproto_port_get_rstp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1099 struct ofproto_port_rstp_status
*s
)
1101 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1103 VLOG_WARN_RL(&rl
, "%s: cannot get RSTP status on nonexistent "
1104 "port %"PRIu32
, ofproto
->name
, ofp_port
);
1108 if (!ofproto
->ofproto_class
->get_rstp_port_status
) {
1111 ofproto
->ofproto_class
->get_rstp_port_status(ofport
, s
);
1115 /* Queue DSCP configuration. */
1117 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
1118 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
1119 * to implement QoS. Instead, it is used to implement features which require
1120 * knowledge of what queues exist on a port, and some basic information about
1123 * Returns 0 if successful, otherwise a positive errno value. */
1125 ofproto_port_set_queues(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1126 const struct ofproto_port_queue
*queues
,
1129 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1132 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu32
,
1133 ofproto
->name
, ofp_port
);
1137 return (ofproto
->ofproto_class
->set_queues
1138 ? ofproto
->ofproto_class
->set_queues(ofport
, queues
, n_queues
)
1142 /* LLDP configuration. */
1144 ofproto_port_set_lldp(struct ofproto
*ofproto
,
1145 ofp_port_t ofp_port
,
1146 const struct smap
*cfg
)
1148 struct ofport
*ofport
;
1151 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1153 VLOG_WARN("%s: cannot configure LLDP on nonexistent port %"PRIu32
,
1154 ofproto
->name
, ofp_port
);
1157 error
= (ofproto
->ofproto_class
->set_lldp
1158 ? ofproto
->ofproto_class
->set_lldp(ofport
, cfg
)
1161 VLOG_WARN("%s: lldp configuration on port %"PRIu32
" (%s) failed (%s)",
1162 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1163 ovs_strerror(error
));
1168 ofproto_set_aa(struct ofproto
*ofproto
, void *aux OVS_UNUSED
,
1169 const struct aa_settings
*s
)
1171 if (!ofproto
->ofproto_class
->set_aa
) {
1174 ofproto
->ofproto_class
->set_aa(ofproto
, s
);
1179 ofproto_aa_mapping_register(struct ofproto
*ofproto
, void *aux
,
1180 const struct aa_mapping_settings
*s
)
1182 if (!ofproto
->ofproto_class
->aa_mapping_set
) {
1185 ofproto
->ofproto_class
->aa_mapping_set(ofproto
, aux
, s
);
1190 ofproto_aa_mapping_unregister(struct ofproto
*ofproto
, void *aux
)
1192 if (!ofproto
->ofproto_class
->aa_mapping_unset
) {
1195 ofproto
->ofproto_class
->aa_mapping_unset(ofproto
, aux
);
1200 ofproto_aa_vlan_get_queued(struct ofproto
*ofproto
,
1201 struct ovs_list
*list
)
1203 if (!ofproto
->ofproto_class
->aa_vlan_get_queued
) {
1206 ofproto
->ofproto_class
->aa_vlan_get_queued(ofproto
, list
);
1211 ofproto_aa_vlan_get_queue_size(struct ofproto
*ofproto
)
1213 if (!ofproto
->ofproto_class
->aa_vlan_get_queue_size
) {
1216 return ofproto
->ofproto_class
->aa_vlan_get_queue_size(ofproto
);
1219 /* Connectivity Fault Management configuration. */
1221 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
1223 ofproto_port_clear_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1225 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1226 if (ofport
&& ofproto
->ofproto_class
->set_cfm
) {
1227 ofproto
->ofproto_class
->set_cfm(ofport
, NULL
);
1231 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
1232 * basic configuration from the configuration members in 'cfm', and the remote
1233 * maintenance point ID from remote_mpid. Ignores the statistics members of
1236 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
1238 ofproto_port_set_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1239 const struct cfm_settings
*s
)
1241 struct ofport
*ofport
;
1244 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1246 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32
,
1247 ofproto
->name
, ofp_port
);
1251 /* XXX: For configuration simplicity, we only support one remote_mpid
1252 * outside of the CFM module. It's not clear if this is the correct long
1253 * term solution or not. */
1254 error
= (ofproto
->ofproto_class
->set_cfm
1255 ? ofproto
->ofproto_class
->set_cfm(ofport
, s
)
1258 VLOG_WARN("%s: CFM configuration on port %"PRIu32
" (%s) failed (%s)",
1259 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1260 ovs_strerror(error
));
1264 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
1265 * 'ofproto' does not have a port 'ofp_port'. */
1267 ofproto_port_set_bfd(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1268 const struct smap
*cfg
)
1270 struct ofport
*ofport
;
1273 ofport
= ofproto_get_port(ofproto
, ofp_port
);
1275 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu32
,
1276 ofproto
->name
, ofp_port
);
1280 error
= (ofproto
->ofproto_class
->set_bfd
1281 ? ofproto
->ofproto_class
->set_bfd(ofport
, cfg
)
1284 VLOG_WARN("%s: bfd configuration on port %"PRIu32
" (%s) failed (%s)",
1285 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
1286 ovs_strerror(error
));
1290 /* Checks the status change of BFD on 'ofport'.
1292 * Returns true if 'ofproto_class' does not support 'bfd_status_changed'. */
1294 ofproto_port_bfd_status_changed(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1296 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1297 return (ofport
&& ofproto
->ofproto_class
->bfd_status_changed
1298 ? ofproto
->ofproto_class
->bfd_status_changed(ofport
)
1302 /* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on
1303 * success. Returns a positive errno otherwise. Has no effect if 'ofp_port'
1304 * is not an OpenFlow port in 'ofproto'.
1306 * The caller must provide and own '*status'. */
1308 ofproto_port_get_bfd_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
1309 struct smap
*status
)
1311 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1312 return (ofport
&& ofproto
->ofproto_class
->get_bfd_status
1313 ? ofproto
->ofproto_class
->get_bfd_status(ofport
, status
)
1317 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1318 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1319 * 0 if LACP partner information is not current (generally indicating a
1320 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1322 ofproto_port_is_lacp_current(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1324 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1325 return (ofport
&& ofproto
->ofproto_class
->port_is_lacp_current
1326 ? ofproto
->ofproto_class
->port_is_lacp_current(ofport
)
1331 ofproto_port_get_lacp_stats(const struct ofport
*port
, struct lacp_slave_stats
*stats
)
1333 struct ofproto
*ofproto
= port
->ofproto
;
1336 if (ofproto
->ofproto_class
->port_get_lacp_stats
) {
1337 error
= ofproto
->ofproto_class
->port_get_lacp_stats(port
, stats
);
1347 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1348 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1349 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1350 * configuration plus, if there is more than one slave, a bonding
1353 * If 'aux' is already registered then this function updates its configuration
1354 * to 's'. Otherwise, this function registers a new bundle.
1356 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1359 ofproto_bundle_register(struct ofproto
*ofproto
, void *aux
,
1360 const struct ofproto_bundle_settings
*s
)
1362 return (ofproto
->ofproto_class
->bundle_set
1363 ? ofproto
->ofproto_class
->bundle_set(ofproto
, aux
, s
)
1367 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1368 * If no such bundle has been registered, this has no effect. */
1370 ofproto_bundle_unregister(struct ofproto
*ofproto
, void *aux
)
1372 return ofproto_bundle_register(ofproto
, aux
, NULL
);
1376 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1377 * If 'aux' is already registered then this function updates its configuration
1378 * to 's'. Otherwise, this function registers a new mirror. */
1380 ofproto_mirror_register(struct ofproto
*ofproto
, void *aux
,
1381 const struct ofproto_mirror_settings
*s
)
1383 return (ofproto
->ofproto_class
->mirror_set
1384 ? ofproto
->ofproto_class
->mirror_set(ofproto
, aux
, s
)
1388 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1389 * If no mirror has been registered, this has no effect. */
1391 ofproto_mirror_unregister(struct ofproto
*ofproto
, void *aux
)
1393 return ofproto_mirror_register(ofproto
, aux
, NULL
);
1396 /* Retrieves statistics from mirror associated with client data pointer
1397 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1398 * 'bytes', respectively. If a particular counters is not supported,
1399 * the appropriate argument is set to UINT64_MAX.
1402 ofproto_mirror_get_stats(struct ofproto
*ofproto
, void *aux
,
1403 uint64_t *packets
, uint64_t *bytes
)
1405 if (!ofproto
->ofproto_class
->mirror_get_stats
) {
1406 *packets
= *bytes
= UINT64_MAX
;
1410 return ofproto
->ofproto_class
->mirror_get_stats(ofproto
, aux
,
1414 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1415 * which all packets are flooded, instead of using MAC learning. If
1416 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1418 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1421 ofproto_set_flood_vlans(struct ofproto
*ofproto
, unsigned long *flood_vlans
)
1423 return (ofproto
->ofproto_class
->set_flood_vlans
1424 ? ofproto
->ofproto_class
->set_flood_vlans(ofproto
, flood_vlans
)
1428 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1429 * output for a mirror. */
1431 ofproto_is_mirror_output_bundle(const struct ofproto
*ofproto
, void *aux
)
1433 return (ofproto
->ofproto_class
->is_mirror_output_bundle
1434 ? ofproto
->ofproto_class
->is_mirror_output_bundle(ofproto
, aux
)
1438 /* Configuration of OpenFlow tables. */
1440 /* Returns the number of OpenFlow tables in 'ofproto'. */
1442 ofproto_get_n_tables(const struct ofproto
*ofproto
)
1444 return ofproto
->n_tables
;
1447 /* Returns the number of Controller visible OpenFlow tables
1448 * in 'ofproto'. This number will exclude Hidden tables.
1449 * This funtion's return value should be less or equal to that of
1450 * ofproto_get_n_tables() . */
1452 ofproto_get_n_visible_tables(const struct ofproto
*ofproto
)
1454 uint8_t n
= ofproto
->n_tables
;
1456 /* Count only non-hidden tables in the number of tables. (Hidden tables,
1457 * if present, are always at the end.) */
1458 while(n
&& (ofproto
->tables
[n
- 1].flags
& OFTABLE_HIDDEN
)) {
1465 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1466 * settings from 's'. 'table_id' must be in the range 0 through the number of
1467 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1469 * For read-only tables, only the name may be configured. */
1471 ofproto_configure_table(struct ofproto
*ofproto
, int table_id
,
1472 const struct ofproto_table_settings
*s
)
1474 struct oftable
*table
;
1476 ovs_assert(table_id
>= 0 && table_id
< ofproto
->n_tables
);
1477 table
= &ofproto
->tables
[table_id
];
1479 oftable_set_name(table
, s
->name
);
1481 if (table
->flags
& OFTABLE_READONLY
) {
1485 if (classifier_set_prefix_fields(&table
->cls
,
1486 s
->prefix_fields
, s
->n_prefix_fields
)) {
1487 /* XXX: Trigger revalidation. */
1490 ovs_mutex_lock(&ofproto_mutex
);
1491 unsigned int new_eviction
= (s
->enable_eviction
1492 ? table
->eviction
| EVICTION_CLIENT
1493 : table
->eviction
& ~EVICTION_CLIENT
);
1494 oftable_configure_eviction(table
, new_eviction
, s
->groups
, s
->n_groups
);
1495 table
->max_flows
= s
->max_flows
;
1496 evict_rules_from_table(table
);
1497 ovs_mutex_unlock(&ofproto_mutex
);
1501 ofproto_has_snoops(const struct ofproto
*ofproto
)
1503 return connmgr_has_snoops(ofproto
->connmgr
);
1507 ofproto_get_snoops(const struct ofproto
*ofproto
, struct sset
*snoops
)
1509 connmgr_get_snoops(ofproto
->connmgr
, snoops
);
1512 /* Deletes 'rule' from 'ofproto'.
1514 * Within an ofproto implementation, this function allows an ofproto
1515 * implementation to destroy any rules that remain when its ->destruct()
1516 * function is called. This function is not suitable for use elsewhere in an
1517 * ofproto implementation.
1519 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1520 * Cycle" in ofproto-provider.h. */
1522 ofproto_rule_delete(struct ofproto
*ofproto
, struct rule
*rule
)
1523 OVS_EXCLUDED(ofproto_mutex
)
1525 /* This skips the ofmonitor and flow-removed notifications because the
1526 * switch is being deleted and any OpenFlow channels have been or soon will
1528 ovs_mutex_lock(&ofproto_mutex
);
1530 if (rule
->state
== RULE_INSERTED
) {
1531 /* Make sure there is no postponed removal of the rule. */
1532 ovs_assert(cls_rule_visible_in_version(&rule
->cr
, OVS_VERSION_MAX
));
1534 classifier_remove_assert(&rule
->ofproto
->tables
[rule
->table_id
].cls
,
1536 ofproto_rule_remove__(rule
->ofproto
, rule
);
1537 if (ofproto
->ofproto_class
->rule_delete
) {
1538 ofproto
->ofproto_class
->rule_delete(rule
);
1541 /* This may not be the last reference to the rule. */
1542 ofproto_rule_unref(rule
);
1544 ovs_mutex_unlock(&ofproto_mutex
);
1548 ofproto_flush__(struct ofproto
*ofproto
)
1549 OVS_EXCLUDED(ofproto_mutex
)
1551 struct oftable
*table
;
1553 /* This will flush all datapath flows. */
1554 if (ofproto
->ofproto_class
->flush
) {
1555 ofproto
->ofproto_class
->flush(ofproto
);
1558 /* XXX: There is a small race window here, where new datapath flows can be
1559 * created by upcall handlers based on the existing flow table. We can not
1560 * call ofproto class flush while holding 'ofproto_mutex' to prevent this,
1561 * as then we could deadlock on syncing with the handler threads waiting on
1562 * the same mutex. */
1564 ovs_mutex_lock(&ofproto_mutex
);
1565 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1566 struct rule_collection rules
;
1569 if (table
->flags
& OFTABLE_HIDDEN
) {
1573 rule_collection_init(&rules
);
1575 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
1576 rule_collection_add(&rules
, rule
);
1578 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
1580 ofproto_group_delete_all__(ofproto
);
1581 meter_delete_all(ofproto
);
1582 /* XXX: Concurrent handler threads may insert new learned flows based on
1583 * learn actions of the now deleted flows right after we release
1584 * 'ofproto_mutex'. */
1585 ovs_mutex_unlock(&ofproto_mutex
);
1589 ofproto_destroy__(struct ofproto
*ofproto
)
1590 OVS_EXCLUDED(ofproto_mutex
)
1592 struct oftable
*table
;
1594 cmap_destroy(&ofproto
->groups
);
1596 ovs_mutex_lock(&ofproto_mutex
);
1597 hmap_remove(&all_ofprotos
, &ofproto
->hmap_node
);
1598 ovs_mutex_unlock(&ofproto_mutex
);
1600 free(ofproto
->name
);
1601 free(ofproto
->type
);
1602 free(ofproto
->mfr_desc
);
1603 free(ofproto
->hw_desc
);
1604 free(ofproto
->sw_desc
);
1605 free(ofproto
->serial_desc
);
1606 free(ofproto
->dp_desc
);
1607 hmap_destroy(&ofproto
->ports
);
1608 hmap_destroy(&ofproto
->ofport_usage
);
1609 shash_destroy(&ofproto
->port_by_name
);
1610 simap_destroy(&ofproto
->ofp_requests
);
1612 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1613 oftable_destroy(table
);
1615 free(ofproto
->tables
);
1617 hmap_destroy(&ofproto
->meters
);
1619 ovs_mutex_lock(&ofproto
->vl_mff_map
.mutex
);
1620 mf_vl_mff_map_clear(&ofproto
->vl_mff_map
, true);
1621 ovs_mutex_unlock(&ofproto
->vl_mff_map
.mutex
);
1622 cmap_destroy(&ofproto
->vl_mff_map
.cmap
);
1623 ovs_mutex_destroy(&ofproto
->vl_mff_map
.mutex
);
1624 tun_metadata_free(ovsrcu_get_protected(struct tun_table
*,
1625 &ofproto
->metadata_tab
));
1627 ovs_assert(hindex_is_empty(&ofproto
->cookies
));
1628 hindex_destroy(&ofproto
->cookies
);
1630 ovs_assert(hmap_is_empty(&ofproto
->learned_cookies
));
1631 hmap_destroy(&ofproto
->learned_cookies
);
1633 ofproto
->ofproto_class
->dealloc(ofproto
);
1636 /* Destroying rules is doubly deferred, must have 'ofproto' around for them.
1637 * - 1st we defer the removal of the rules from the classifier
1638 * - 2nd we defer the actual destruction of the rules. */
1640 ofproto_destroy_defer__(struct ofproto
*ofproto
)
1641 OVS_EXCLUDED(ofproto_mutex
)
1643 ovsrcu_postpone(ofproto_destroy__
, ofproto
);
1647 ofproto_destroy(struct ofproto
*p
, bool del
)
1648 OVS_EXCLUDED(ofproto_mutex
)
1650 struct ofport
*ofport
, *next_ofport
;
1651 struct ofport_usage
*usage
;
1658 HMAP_FOR_EACH_SAFE (ofport
, next_ofport
, hmap_node
, &p
->ports
) {
1659 ofport_destroy(ofport
, del
);
1662 HMAP_FOR_EACH_POP (usage
, hmap_node
, &p
->ofport_usage
) {
1666 p
->ofproto_class
->destruct(p
, del
);
1668 /* We should not postpone this because it involves deleting a listening
1669 * socket which we may want to reopen soon. 'connmgr' may be used by other
1670 * threads only if they take the ofproto_mutex and read a non-NULL
1671 * 'ofproto->connmgr'. */
1672 ovs_mutex_lock(&ofproto_mutex
);
1673 connmgr_destroy(p
->connmgr
);
1675 ovs_mutex_unlock(&ofproto_mutex
);
1677 /* Destroying rules is deferred, must have 'ofproto' around for them. */
1678 ovsrcu_postpone(ofproto_destroy_defer__
, p
);
1681 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1682 * kernel datapath, for example, this destroys the datapath in the kernel, and
1683 * with the netdev-based datapath, it tears down the data structures that
1684 * represent the datapath.
1686 * The datapath should not be currently open as an ofproto. */
1688 ofproto_delete(const char *name
, const char *type
)
1690 const struct ofproto_class
*class = ofproto_class_find__(type
);
1691 return (!class ? EAFNOSUPPORT
1692 : !class->del
? EACCES
1693 : class->del(type
, name
));
1697 process_port_change(struct ofproto
*ofproto
, int error
, char *devname
)
1699 if (error
== ENOBUFS
) {
1700 reinit_ports(ofproto
);
1701 } else if (!error
) {
1702 update_port(ofproto
, devname
);
1708 ofproto_type_run(const char *datapath_type
)
1710 const struct ofproto_class
*class;
1713 datapath_type
= ofproto_normalize_type(datapath_type
);
1714 class = ofproto_class_find__(datapath_type
);
1716 error
= class->type_run
? class->type_run(datapath_type
) : 0;
1717 if (error
&& error
!= EAGAIN
) {
1718 VLOG_ERR_RL(&rl
, "%s: type_run failed (%s)",
1719 datapath_type
, ovs_strerror(error
));
1725 ofproto_type_wait(const char *datapath_type
)
1727 const struct ofproto_class
*class;
1729 datapath_type
= ofproto_normalize_type(datapath_type
);
1730 class = ofproto_class_find__(datapath_type
);
1732 if (class->type_wait
) {
1733 class->type_wait(datapath_type
);
1738 ofproto_run(struct ofproto
*p
)
1743 error
= p
->ofproto_class
->run(p
);
1744 if (error
&& error
!= EAGAIN
) {
1745 VLOG_ERR_RL(&rl
, "%s: run failed (%s)", p
->name
, ovs_strerror(error
));
1748 /* Restore the eviction group heap invariant occasionally. */
1749 if (p
->eviction_group_timer
< time_msec()) {
1752 p
->eviction_group_timer
= time_msec() + 1000;
1754 for (i
= 0; i
< p
->n_tables
; i
++) {
1755 struct oftable
*table
= &p
->tables
[i
];
1756 struct eviction_group
*evg
;
1759 if (!table
->eviction
) {
1763 if (table
->n_flows
> 100000) {
1764 static struct vlog_rate_limit count_rl
=
1765 VLOG_RATE_LIMIT_INIT(1, 1);
1766 VLOG_WARN_RL(&count_rl
, "Table %"PRIuSIZE
" has an excessive"
1767 " number of rules: %d", i
, table
->n_flows
);
1770 ovs_mutex_lock(&ofproto_mutex
);
1771 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
1772 if (rule
->idle_timeout
|| rule
->hard_timeout
) {
1773 if (!rule
->eviction_group
) {
1774 eviction_group_add_rule(rule
);
1776 heap_raw_change(&rule
->evg_node
,
1777 rule_eviction_priority(p
, rule
));
1782 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
1783 heap_rebuild(&evg
->rules
);
1785 ovs_mutex_unlock(&ofproto_mutex
);
1789 if (p
->ofproto_class
->port_poll
) {
1792 while ((error
= p
->ofproto_class
->port_poll(p
, &devname
)) != EAGAIN
) {
1793 process_port_change(p
, error
, devname
);
1797 new_seq
= seq_read(connectivity_seq_get());
1798 if (new_seq
!= p
->change_seq
) {
1799 struct sset devnames
;
1800 const char *devname
;
1801 struct ofport
*ofport
;
1803 /* Update OpenFlow port status for any port whose netdev has changed.
1805 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1806 * destroyed, so it's not safe to update ports directly from the
1807 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1808 * need this two-phase approach. */
1809 sset_init(&devnames
);
1810 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1811 uint64_t port_change_seq
;
1813 port_change_seq
= netdev_get_change_seq(ofport
->netdev
);
1814 if (ofport
->change_seq
!= port_change_seq
) {
1815 ofport
->change_seq
= port_change_seq
;
1816 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
1819 SSET_FOR_EACH (devname
, &devnames
) {
1820 update_port(p
, devname
);
1822 sset_destroy(&devnames
);
1824 p
->change_seq
= new_seq
;
1827 connmgr_run(p
->connmgr
, handle_openflow
);
1833 ofproto_wait(struct ofproto
*p
)
1835 p
->ofproto_class
->wait(p
);
1836 if (p
->ofproto_class
->port_poll_wait
) {
1837 p
->ofproto_class
->port_poll_wait(p
);
1839 seq_wait(connectivity_seq_get(), p
->change_seq
);
1840 connmgr_wait(p
->connmgr
);
1844 ofproto_is_alive(const struct ofproto
*p
)
1846 return connmgr_has_controllers(p
->connmgr
);
1849 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1850 * memory_report(). */
1852 ofproto_get_memory_usage(const struct ofproto
*ofproto
, struct simap
*usage
)
1854 const struct oftable
*table
;
1855 unsigned int n_rules
;
1857 simap_increase(usage
, "ports", hmap_count(&ofproto
->ports
));
1860 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1861 n_rules
+= table
->n_flows
;
1863 simap_increase(usage
, "rules", n_rules
);
1865 if (ofproto
->ofproto_class
->get_memory_usage
) {
1866 ofproto
->ofproto_class
->get_memory_usage(ofproto
, usage
);
1869 connmgr_get_memory_usage(ofproto
->connmgr
, usage
);
1873 ofproto_type_get_memory_usage(const char *datapath_type
, struct simap
*usage
)
1875 const struct ofproto_class
*class;
1877 datapath_type
= ofproto_normalize_type(datapath_type
);
1878 class = ofproto_class_find__(datapath_type
);
1880 if (class && class->type_get_memory_usage
) {
1881 class->type_get_memory_usage(datapath_type
, usage
);
1886 ofproto_get_ofproto_controller_info(const struct ofproto
*ofproto
,
1889 connmgr_get_controller_info(ofproto
->connmgr
, info
);
1893 ofproto_free_ofproto_controller_info(struct shash
*info
)
1895 connmgr_free_controller_info(info
);
1898 /* Makes a deep copy of 'old' into 'port'. */
1900 ofproto_port_clone(struct ofproto_port
*port
, const struct ofproto_port
*old
)
1902 port
->name
= xstrdup(old
->name
);
1903 port
->type
= xstrdup(old
->type
);
1904 port
->ofp_port
= old
->ofp_port
;
1907 /* Frees memory allocated to members of 'ofproto_port'.
1909 * Do not call this function on an ofproto_port obtained from
1910 * ofproto_port_dump_next(): that function retains ownership of the data in the
1913 ofproto_port_destroy(struct ofproto_port
*ofproto_port
)
1915 free(ofproto_port
->name
);
1916 free(ofproto_port
->type
);
1919 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1921 * This function provides no status indication. An error status for the entire
1922 * dump operation is provided when it is completed by calling
1923 * ofproto_port_dump_done().
1926 ofproto_port_dump_start(struct ofproto_port_dump
*dump
,
1927 const struct ofproto
*ofproto
)
1929 dump
->ofproto
= ofproto
;
1930 dump
->error
= ofproto
->ofproto_class
->port_dump_start(ofproto
,
1934 /* Attempts to retrieve another port from 'dump', which must have been created
1935 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1936 * 'port' and returns true. On failure, returns false.
1938 * Failure might indicate an actual error or merely that the last port has been
1939 * dumped. An error status for the entire dump operation is provided when it
1940 * is completed by calling ofproto_port_dump_done().
1942 * The ofproto owns the data stored in 'port'. It will remain valid until at
1943 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1944 * ofproto_port_dump_done(). */
1946 ofproto_port_dump_next(struct ofproto_port_dump
*dump
,
1947 struct ofproto_port
*port
)
1949 const struct ofproto
*ofproto
= dump
->ofproto
;
1955 dump
->error
= ofproto
->ofproto_class
->port_dump_next(ofproto
, dump
->state
,
1958 ofproto
->ofproto_class
->port_dump_done(ofproto
, dump
->state
);
1964 /* Completes port table dump operation 'dump', which must have been created
1965 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1966 * error-free, otherwise a positive errno value describing the problem. */
1968 ofproto_port_dump_done(struct ofproto_port_dump
*dump
)
1970 const struct ofproto
*ofproto
= dump
->ofproto
;
1972 dump
->error
= ofproto
->ofproto_class
->port_dump_done(ofproto
,
1975 return dump
->error
== EOF
? 0 : dump
->error
;
1978 /* Returns the type to pass to netdev_open() when 'ofproto' has a port of type
1979 * 'port_type', for a few special cases when a netdev type differs from a port
1980 * type. For example, when using the userspace datapath, a port of type
1981 * "internal" needs to be opened as "tap".
1983 * Returns either 'type' itself or a string literal, which must not be
1986 ofproto_port_open_type(const struct ofproto
*ofproto
, const char *port_type
)
1988 return (ofproto
->ofproto_class
->port_open_type
1989 ? ofproto
->ofproto_class
->port_open_type(ofproto
->type
, port_type
)
1993 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1994 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1995 * the port's OpenFlow port number.
1997 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1998 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1999 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
2000 * 'ofp_portp' is non-null). */
2002 ofproto_port_add(struct ofproto
*ofproto
, struct netdev
*netdev
,
2003 ofp_port_t
*ofp_portp
)
2005 ofp_port_t ofp_port
= ofp_portp
? *ofp_portp
: OFPP_NONE
;
2008 error
= ofproto
->ofproto_class
->port_add(ofproto
, netdev
);
2010 const char *netdev_name
= netdev_get_name(netdev
);
2012 simap_put(&ofproto
->ofp_requests
, netdev_name
,
2013 ofp_to_u16(ofp_port
));
2014 error
= update_port(ofproto
, netdev_name
);
2017 *ofp_portp
= OFPP_NONE
;
2019 struct ofproto_port ofproto_port
;
2021 error
= ofproto_port_query_by_name(ofproto
,
2022 netdev_get_name(netdev
),
2025 *ofp_portp
= ofproto_port
.ofp_port
;
2026 ofproto_port_destroy(&ofproto_port
);
2033 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
2034 * initializes '*port' appropriately; on failure, returns a positive errno
2037 * The caller owns the data in 'ofproto_port' and must free it with
2038 * ofproto_port_destroy() when it is no longer needed. */
2040 ofproto_port_query_by_name(const struct ofproto
*ofproto
, const char *devname
,
2041 struct ofproto_port
*port
)
2045 error
= ofproto
->ofproto_class
->port_query_by_name(ofproto
, devname
, port
);
2047 memset(port
, 0, sizeof *port
);
2052 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
2053 * Returns 0 if successful, otherwise a positive errno. */
2055 ofproto_port_del(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2057 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
2058 const char *name
= ofport
? netdev_get_name(ofport
->netdev
) : "<unknown>";
2059 struct simap_node
*ofp_request_node
;
2062 ofp_request_node
= simap_find(&ofproto
->ofp_requests
, name
);
2063 if (ofp_request_node
) {
2064 simap_delete(&ofproto
->ofp_requests
, ofp_request_node
);
2067 error
= ofproto
->ofproto_class
->port_del(ofproto
, ofp_port
);
2068 if (!error
&& ofport
) {
2069 /* 'name' is the netdev's name and update_port() is going to close the
2070 * netdev. Just in case update_port() refers to 'name' after it
2071 * destroys 'ofport', make a copy of it around the update_port()
2073 char *devname
= xstrdup(name
);
2074 update_port(ofproto
, devname
);
2080 /* Refreshes datapath configuration of port number 'ofp_port' in 'ofproto'.
2082 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
2084 ofproto_port_set_config(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
2085 const struct smap
*cfg
)
2087 struct ofport
*ofport
;
2090 ofport
= ofproto_get_port(ofproto
, ofp_port
);
2092 VLOG_WARN("%s: cannot configure datapath on nonexistent port %"PRIu32
,
2093 ofproto
->name
, ofp_port
);
2097 error
= (ofproto
->ofproto_class
->port_set_config
2098 ? ofproto
->ofproto_class
->port_set_config(ofport
, cfg
)
2101 VLOG_WARN("%s: datapath configuration on port %"PRIu32
2102 " (%s) failed (%s)",
2103 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
2104 ovs_strerror(error
));
2110 flow_mod_init(struct ofputil_flow_mod
*fm
,
2111 const struct match
*match
, int priority
,
2112 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2113 enum ofp_flow_mod_command command
)
2115 *fm
= (struct ofputil_flow_mod
) {
2116 .priority
= priority
,
2119 .buffer_id
= UINT32_MAX
,
2120 .out_port
= OFPP_ANY
,
2121 .out_group
= OFPG_ANY
,
2122 .ofpacts
= CONST_CAST(struct ofpact
*, ofpacts
),
2123 .ofpacts_len
= ofpacts_len
,
2125 minimatch_init(&fm
->match
, match
);
2129 simple_flow_mod(struct ofproto
*ofproto
,
2130 const struct match
*match
, int priority
,
2131 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2132 enum ofp_flow_mod_command command
)
2134 struct ofputil_flow_mod fm
;
2135 flow_mod_init(&fm
, match
, priority
, ofpacts
, ofpacts_len
, command
);
2136 enum ofperr error
= handle_flow_mod__(ofproto
, &fm
, NULL
);
2137 minimatch_destroy(&fm
.match
);
2141 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
2142 * performs the 'n_actions' actions in 'actions'. The new flow will not
2145 * If cls_rule->priority is in the range of priorities supported by OpenFlow
2146 * (0...65535, inclusive) then the flow will be visible to OpenFlow
2147 * controllers; otherwise, it will be hidden.
2149 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
2151 * This is a helper function for in-band control and fail-open. */
2153 ofproto_add_flow(struct ofproto
*ofproto
, const struct match
*match
,
2155 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
2156 OVS_EXCLUDED(ofproto_mutex
)
2158 const struct rule
*rule
;
2161 /* First do a cheap check whether the rule we're looking for already exists
2162 * with the actions that we want. If it does, then we're done. */
2163 rule
= rule_from_cls_rule(classifier_find_match_exactly(
2164 &ofproto
->tables
[0].cls
, match
, priority
,
2167 const struct rule_actions
*actions
= rule_get_actions(rule
);
2168 must_add
= !ofpacts_equal(actions
->ofpacts
, actions
->ofpacts_len
,
2169 ofpacts
, ofpacts_len
);
2174 /* If there's no such rule or the rule doesn't have the actions we want,
2175 * fall back to a executing a full flow mod. We can't optimize this at
2176 * all because we didn't take enough locks above to ensure that the flow
2177 * table didn't already change beneath us. */
2179 simple_flow_mod(ofproto
, match
, priority
, ofpacts
, ofpacts_len
,
2180 OFPFC_MODIFY_STRICT
);
2184 /* Executes the flow modification specified in 'fm'. Returns 0 on success, or
2185 * an OFPERR_* OpenFlow error code on failure.
2187 * This is a helper function for in-band control and fail-open. */
2189 ofproto_flow_mod(struct ofproto
*ofproto
, const struct ofputil_flow_mod
*fm
)
2190 OVS_EXCLUDED(ofproto_mutex
)
2192 return handle_flow_mod__(ofproto
, fm
, NULL
);
2195 /* Searches for a rule with matching criteria exactly equal to 'target' in
2196 * ofproto's table 0 and, if it finds one, deletes it.
2198 * This is a helper function for in-band control and fail-open. */
2200 ofproto_delete_flow(struct ofproto
*ofproto
,
2201 const struct match
*target
, int priority
)
2202 OVS_REQUIRES(ofproto_mutex
)
2204 struct classifier
*cls
= &ofproto
->tables
[0].cls
;
2207 /* First do a cheap check whether the rule we're looking for has already
2208 * been deleted. If so, then we're done. */
2209 rule
= rule_from_cls_rule(classifier_find_match_exactly(
2210 cls
, target
, priority
, OVS_VERSION_MAX
));
2215 struct rule_collection rules
;
2217 rule_collection_init(&rules
);
2218 rule_collection_add(&rules
, rule
);
2219 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
2220 rule_collection_destroy(&rules
);
2223 /* Delete all of the flows from all of ofproto's flow tables, then reintroduce
2224 * the flows required by in-band control and fail-open. */
2226 ofproto_flush_flows(struct ofproto
*ofproto
)
2228 COVERAGE_INC(ofproto_flush
);
2229 ofproto_flush__(ofproto
);
2230 connmgr_flushed(ofproto
->connmgr
);
2234 reinit_ports(struct ofproto
*p
)
2236 struct ofproto_port_dump dump
;
2237 struct sset devnames
;
2238 struct ofport
*ofport
;
2239 struct ofproto_port ofproto_port
;
2240 const char *devname
;
2242 COVERAGE_INC(ofproto_reinit_ports
);
2244 sset_init(&devnames
);
2245 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2246 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
2248 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2249 sset_add(&devnames
, ofproto_port
.name
);
2252 SSET_FOR_EACH (devname
, &devnames
) {
2253 update_port(p
, devname
);
2255 sset_destroy(&devnames
);
2259 alloc_ofp_port(struct ofproto
*ofproto
, const char *netdev_name
)
2263 port_idx
= simap_get(&ofproto
->ofp_requests
, netdev_name
);
2264 port_idx
= port_idx
? port_idx
: UINT16_MAX
;
2266 if (port_idx
>= ofproto
->max_ports
2267 || ofport_get_usage(ofproto
, u16_to_ofp(port_idx
)) == LLONG_MAX
) {
2268 uint16_t lru_ofport
= 0, end_port_no
= ofproto
->alloc_port_no
;
2269 long long int last_used_at
, lru
= LLONG_MAX
;
2271 /* Search for a free OpenFlow port number. We try not to
2272 * immediately reuse them to prevent problems due to old
2275 * We limit the automatically assigned port numbers to the lower half
2276 * of the port range, to reserve the upper half for assignment by
2279 if (++ofproto
->alloc_port_no
>= MIN(ofproto
->max_ports
, 32768)) {
2280 ofproto
->alloc_port_no
= 1;
2282 last_used_at
= ofport_get_usage(ofproto
,
2283 u16_to_ofp(ofproto
->alloc_port_no
));
2284 if (!last_used_at
) {
2285 port_idx
= ofproto
->alloc_port_no
;
2287 } else if ( last_used_at
< time_msec() - 60*60*1000) {
2288 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2289 * more than an hour ago, consider it usable. */
2290 ofport_remove_usage(ofproto
,
2291 u16_to_ofp(ofproto
->alloc_port_no
));
2292 port_idx
= ofproto
->alloc_port_no
;
2294 } else if (last_used_at
< lru
) {
2296 lru_ofport
= ofproto
->alloc_port_no
;
2299 if (ofproto
->alloc_port_no
== end_port_no
) {
2301 port_idx
= lru_ofport
;
2308 ofport_set_usage(ofproto
, u16_to_ofp(port_idx
), LLONG_MAX
);
2309 return u16_to_ofp(port_idx
);
2313 dealloc_ofp_port(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2315 if (ofp_to_u16(ofp_port
) < ofproto
->max_ports
) {
2316 ofport_set_usage(ofproto
, ofp_port
, time_msec());
2320 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2321 * pointer if the netdev cannot be opened. On success, also fills in
2323 static struct netdev
*
2324 ofport_open(struct ofproto
*ofproto
,
2325 struct ofproto_port
*ofproto_port
,
2326 struct ofputil_phy_port
*pp
)
2328 enum netdev_flags flags
;
2329 struct netdev
*netdev
;
2332 error
= netdev_open(ofproto_port
->name
, ofproto_port
->type
, &netdev
);
2334 VLOG_WARN_RL(&rl
, "%s: ignoring port %s (%"PRIu32
") because netdev %s "
2335 "cannot be opened (%s)",
2337 ofproto_port
->name
, ofproto_port
->ofp_port
,
2338 ofproto_port
->name
, ovs_strerror(error
));
2342 if (ofproto_port
->ofp_port
== OFPP_NONE
) {
2343 if (!strcmp(ofproto
->name
, ofproto_port
->name
)) {
2344 ofproto_port
->ofp_port
= OFPP_LOCAL
;
2346 ofproto_port
->ofp_port
= alloc_ofp_port(ofproto
,
2347 ofproto_port
->name
);
2350 pp
->port_no
= ofproto_port
->ofp_port
;
2351 netdev_get_etheraddr(netdev
, &pp
->hw_addr
);
2352 pp
->hw_addr64
= eth_addr64_zero
;
2353 ovs_strlcpy(pp
->name
, ofproto_port
->name
, sizeof pp
->name
);
2354 netdev_get_flags(netdev
, &flags
);
2355 pp
->config
= flags
& NETDEV_UP
? 0 : OFPUTIL_PC_PORT_DOWN
;
2356 pp
->state
= netdev_get_carrier(netdev
) ? 0 : OFPUTIL_PS_LINK_DOWN
;
2357 netdev_get_features(netdev
, &pp
->curr
, &pp
->advertised
,
2358 &pp
->supported
, &pp
->peer
);
2359 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
2360 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
2365 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name
2366 * and port number are disregarded. */
2368 ofport_equal(const struct ofputil_phy_port
*a
,
2369 const struct ofputil_phy_port
*b
)
2371 return (eth_addr_equals(a
->hw_addr
, b
->hw_addr
)
2372 && eth_addr64_equals(a
->hw_addr64
, b
->hw_addr64
)
2373 && a
->state
== b
->state
2374 && a
->config
== b
->config
2375 && a
->curr
== b
->curr
2376 && a
->advertised
== b
->advertised
2377 && a
->supported
== b
->supported
2378 && a
->peer
== b
->peer
2379 && a
->curr_speed
== b
->curr_speed
2380 && a
->max_speed
== b
->max_speed
);
2383 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2384 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2385 * one with the same name or port number). */
2387 ofport_install(struct ofproto
*p
,
2388 struct netdev
*netdev
, const struct ofputil_phy_port
*pp
)
2390 const char *netdev_name
= netdev_get_name(netdev
);
2391 struct ofport
*ofport
;
2394 /* Create ofport. */
2395 ofport
= p
->ofproto_class
->port_alloc();
2400 ofport
->ofproto
= p
;
2401 ofport
->netdev
= netdev
;
2402 ofport
->change_seq
= netdev_get_change_seq(netdev
);
2404 ofport
->ofp_port
= pp
->port_no
;
2405 ofport
->created
= time_msec();
2406 ofport
->may_enable
= false;
2408 /* Add port to 'p'. */
2409 hmap_insert(&p
->ports
, &ofport
->hmap_node
,
2410 hash_ofp_port(ofport
->ofp_port
));
2411 shash_add(&p
->port_by_name
, netdev_name
, ofport
);
2413 update_mtu(p
, ofport
);
2415 /* Let the ofproto_class initialize its private data. */
2416 error
= p
->ofproto_class
->port_construct(ofport
);
2420 connmgr_send_port_status(p
->connmgr
, NULL
, NULL
, pp
, OFPPR_ADD
);
2424 VLOG_WARN_RL(&rl
, "%s: could not add port %s (%s)",
2425 p
->name
, netdev_name
, ovs_strerror(error
));
2427 ofport_destroy__(ofport
);
2429 netdev_close(netdev
);
2434 /* Removes 'ofport' from 'p' and destroys it. */
2436 ofport_remove(struct ofport
*ofport
)
2438 struct ofproto
*p
= ofport
->ofproto
;
2439 bool is_mtu_overridden
= ofport_is_mtu_overridden(p
, ofport
);
2441 connmgr_send_port_status(ofport
->ofproto
->connmgr
, NULL
, NULL
, &ofport
->pp
,
2443 ofport_destroy(ofport
, true);
2444 if (!is_mtu_overridden
) {
2445 update_mtu_ofproto(p
);
2449 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2452 ofport_remove_with_name(struct ofproto
*ofproto
, const char *name
)
2454 struct ofport
*port
= shash_find_data(&ofproto
->port_by_name
, name
);
2456 ofport_remove(port
);
2460 static enum ofputil_port_state
2461 normalize_state(enum ofputil_port_config config
,
2462 enum ofputil_port_state state
,
2465 return (config
& OFPUTIL_PC_PORT_DOWN
2466 || state
& OFPUTIL_PS_LINK_DOWN
2468 ? state
& ~OFPUTIL_PS_LIVE
2469 : state
| OFPUTIL_PS_LIVE
);
2473 ofproto_port_set_enable(struct ofport
*port
, bool enable
)
2475 if (enable
!= port
->may_enable
) {
2476 port
->may_enable
= enable
;
2477 ofproto_port_set_state(port
, normalize_state(port
->pp
.config
,
2483 /* Update OpenFlow 'state' in 'port' and notify controller. */
2485 ofproto_port_set_state(struct ofport
*port
, enum ofputil_port_state state
)
2487 state
= normalize_state(port
->pp
.config
, state
, port
->may_enable
);
2488 if (port
->pp
.state
!= state
) {
2489 struct ofputil_phy_port old_pp
= port
->pp
;
2490 port
->pp
.state
= state
;
2491 connmgr_send_port_status(port
->ofproto
->connmgr
, NULL
, &old_pp
,
2492 &port
->pp
, OFPPR_MODIFY
);
2497 ofproto_port_unregister(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2499 struct ofport
*port
= ofproto_get_port(ofproto
, ofp_port
);
2501 if (port
->ofproto
->ofproto_class
->set_stp_port
) {
2502 port
->ofproto
->ofproto_class
->set_stp_port(port
, NULL
);
2504 if (port
->ofproto
->ofproto_class
->set_rstp_port
) {
2505 port
->ofproto
->ofproto_class
->set_rstp_port(port
, NULL
);
2507 if (port
->ofproto
->ofproto_class
->set_cfm
) {
2508 port
->ofproto
->ofproto_class
->set_cfm(port
, NULL
);
2510 if (port
->ofproto
->ofproto_class
->bundle_remove
) {
2511 port
->ofproto
->ofproto_class
->bundle_remove(port
);
2517 ofport_destroy__(struct ofport
*port
)
2519 struct ofproto
*ofproto
= port
->ofproto
;
2520 const char *name
= netdev_get_name(port
->netdev
);
2522 hmap_remove(&ofproto
->ports
, &port
->hmap_node
);
2523 shash_find_and_delete(&ofproto
->port_by_name
, name
);
2525 netdev_close(port
->netdev
);
2526 ofproto
->ofproto_class
->port_dealloc(port
);
2530 ofport_destroy(struct ofport
*port
, bool del
)
2533 dealloc_ofp_port(port
->ofproto
, port
->ofp_port
);
2534 port
->ofproto
->ofproto_class
->port_destruct(port
, del
);
2535 ofport_destroy__(port
);
2540 ofproto_get_port(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2542 struct ofport
*port
;
2544 HMAP_FOR_EACH_IN_BUCKET (port
, hmap_node
, hash_ofp_port(ofp_port
),
2546 if (port
->ofp_port
== ofp_port
) {
2553 static long long int
2554 ofport_get_usage(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2556 struct ofport_usage
*usage
;
2558 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2559 &ofproto
->ofport_usage
) {
2560 if (usage
->ofp_port
== ofp_port
) {
2561 return usage
->last_used
;
2568 ofport_set_usage(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
2569 long long int last_used
)
2571 struct ofport_usage
*usage
;
2572 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2573 &ofproto
->ofport_usage
) {
2574 if (usage
->ofp_port
== ofp_port
) {
2575 usage
->last_used
= last_used
;
2579 ovs_assert(last_used
== LLONG_MAX
);
2581 usage
= xmalloc(sizeof *usage
);
2582 usage
->ofp_port
= ofp_port
;
2583 usage
->last_used
= last_used
;
2584 hmap_insert(&ofproto
->ofport_usage
, &usage
->hmap_node
,
2585 hash_ofp_port(ofp_port
));
2589 ofport_remove_usage(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2591 struct ofport_usage
*usage
;
2592 HMAP_FOR_EACH_IN_BUCKET (usage
, hmap_node
, hash_ofp_port(ofp_port
),
2593 &ofproto
->ofport_usage
) {
2594 if (usage
->ofp_port
== ofp_port
) {
2595 hmap_remove(&ofproto
->ofport_usage
, &usage
->hmap_node
);
2603 ofproto_port_get_stats(const struct ofport
*port
, struct netdev_stats
*stats
)
2605 struct ofproto
*ofproto
= port
->ofproto
;
2608 if (ofproto
->ofproto_class
->port_get_stats
) {
2609 error
= ofproto
->ofproto_class
->port_get_stats(port
, stats
);
2618 update_port(struct ofproto
*ofproto
, const char *name
)
2620 struct ofproto_port ofproto_port
;
2621 struct ofputil_phy_port pp
;
2622 struct netdev
*netdev
;
2623 struct ofport
*port
;
2626 COVERAGE_INC(ofproto_update_port
);
2628 /* Fetch 'name''s location and properties from the datapath. */
2629 netdev
= (!ofproto_port_query_by_name(ofproto
, name
, &ofproto_port
)
2630 ? ofport_open(ofproto
, &ofproto_port
, &pp
)
2634 port
= ofproto_get_port(ofproto
, ofproto_port
.ofp_port
);
2635 if (port
&& !strcmp(netdev_get_name(port
->netdev
), name
)) {
2636 struct netdev
*old_netdev
= port
->netdev
;
2638 /* ofport_open() only sets OFPUTIL_PC_PORT_DOWN and
2639 * OFPUTIL_PS_LINK_DOWN. Keep the other config and state bits (but
2640 * a port that is down cannot be live). */
2641 pp
.config
|= port
->pp
.config
& ~OFPUTIL_PC_PORT_DOWN
;
2642 pp
.state
|= port
->pp
.state
& ~OFPUTIL_PS_LINK_DOWN
;
2643 pp
.state
= normalize_state(pp
.config
, pp
.state
, port
->may_enable
);
2645 /* 'name' hasn't changed location. Any properties changed? */
2646 if (!ofport_equal(&port
->pp
, &pp
)) {
2647 connmgr_send_port_status(port
->ofproto
->connmgr
, NULL
,
2648 &port
->pp
, &pp
, OFPPR_MODIFY
);
2652 update_mtu(ofproto
, port
);
2654 /* Install the newly opened netdev in case it has changed.
2655 * Don't close the old netdev yet in case port_modified has to
2656 * remove a retained reference to it.*/
2657 port
->netdev
= netdev
;
2658 port
->change_seq
= netdev_get_change_seq(netdev
);
2660 if (port
->ofproto
->ofproto_class
->port_modified
) {
2661 port
->ofproto
->ofproto_class
->port_modified(port
);
2664 netdev_close(old_netdev
);
2666 /* If 'port' is nonnull then its name differs from 'name' and thus
2667 * we should delete it. If we think there's a port named 'name'
2668 * then its port number must be wrong now so delete it too. */
2670 ofport_remove(port
);
2672 ofport_remove_with_name(ofproto
, name
);
2673 error
= ofport_install(ofproto
, netdev
, &pp
);
2676 /* Any port named 'name' is gone now. */
2677 ofport_remove_with_name(ofproto
, name
);
2679 ofproto_port_destroy(&ofproto_port
);
2685 init_ports(struct ofproto
*p
)
2687 struct ofproto_port_dump dump
;
2688 struct ofproto_port ofproto_port
;
2689 struct shash_node
*node
, *next
;
2691 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2692 const char *name
= ofproto_port
.name
;
2694 if (shash_find(&p
->port_by_name
, name
)) {
2695 VLOG_WARN_RL(&rl
, "%s: ignoring duplicate device %s in datapath",
2698 struct ofputil_phy_port pp
;
2699 struct netdev
*netdev
;
2701 /* Check if an OpenFlow port number had been requested. */
2702 node
= shash_find(&init_ofp_ports
, name
);
2704 const struct iface_hint
*iface_hint
= node
->data
;
2705 simap_put(&p
->ofp_requests
, name
,
2706 ofp_to_u16(iface_hint
->ofp_port
));
2709 netdev
= ofport_open(p
, &ofproto_port
, &pp
);
2711 ofport_install(p
, netdev
, &pp
);
2712 if (ofp_to_u16(ofproto_port
.ofp_port
) < p
->max_ports
) {
2713 p
->alloc_port_no
= MAX(p
->alloc_port_no
,
2714 ofp_to_u16(ofproto_port
.ofp_port
));
2720 SHASH_FOR_EACH_SAFE(node
, next
, &init_ofp_ports
) {
2721 struct iface_hint
*iface_hint
= node
->data
;
2723 if (!strcmp(iface_hint
->br_name
, p
->name
)) {
2724 free(iface_hint
->br_name
);
2725 free(iface_hint
->br_type
);
2727 shash_delete(&init_ofp_ports
, node
);
2735 ofport_is_internal_or_patch(const struct ofproto
*p
, const struct ofport
*port
)
2737 const char *netdev_type
= netdev_get_type(port
->netdev
);
2738 return !strcmp(netdev_type
, ofproto_port_open_type(p
, "internal")) ||
2739 !strcmp(netdev_type
, ofproto_port_open_type(p
, "patch"));
2742 /* If 'port' is internal or patch and if the user didn't explicitly specify an
2743 * mtu through the database, we have to override it. */
2745 ofport_is_mtu_overridden(const struct ofproto
*p
, const struct ofport
*port
)
2747 return ofport_is_internal_or_patch(p
, port
)
2748 && !netdev_mtu_is_user_config(port
->netdev
);
2751 /* Find the minimum MTU of all non-overridden devices attached to 'p'.
2752 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2754 find_min_mtu(struct ofproto
*p
)
2756 struct ofport
*ofport
;
2759 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2760 struct netdev
*netdev
= ofport
->netdev
;
2763 /* Skip any overridden port, since that's what we're trying to set. */
2764 if (ofport_is_mtu_overridden(p
, ofport
)) {
2768 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2771 if (!mtu
|| dev_mtu
< mtu
) {
2776 return mtu
? mtu
: ETH_PAYLOAD_MAX
;
2779 /* Update MTU of all overridden devices on 'p' to the minimum of the
2780 * non-overridden ports in event of 'port' added or changed. */
2782 update_mtu(struct ofproto
*p
, struct ofport
*port
)
2784 struct netdev
*netdev
= port
->netdev
;
2787 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2791 if (ofport_is_mtu_overridden(p
, port
)) {
2792 if (dev_mtu
> p
->min_mtu
) {
2793 if (!netdev_set_mtu(port
->netdev
, p
->min_mtu
)) {
2794 dev_mtu
= p
->min_mtu
;
2797 port
->mtu
= dev_mtu
;
2801 port
->mtu
= dev_mtu
;
2802 /* For non-overridden port find new min mtu. */
2804 update_mtu_ofproto(p
);
2808 update_mtu_ofproto(struct ofproto
*p
)
2810 struct ofport
*ofport
;
2811 int old_min
= p
->min_mtu
;
2813 p
->min_mtu
= find_min_mtu(p
);
2814 if (p
->min_mtu
== old_min
) {
2818 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2819 struct netdev
*netdev
= ofport
->netdev
;
2821 if (ofport_is_mtu_overridden(p
, ofport
)) {
2822 if (!netdev_set_mtu(netdev
, p
->min_mtu
)) {
2823 ofport
->mtu
= p
->min_mtu
;
2830 ofproto_rule_destroy__(struct rule
*rule
)
2831 OVS_NO_THREAD_SAFETY_ANALYSIS
2833 cls_rule_destroy(CONST_CAST(struct cls_rule
*, &rule
->cr
));
2834 rule_actions_destroy(rule_get_actions(rule
));
2835 ovs_mutex_destroy(&rule
->mutex
);
2836 rule
->ofproto
->ofproto_class
->rule_dealloc(rule
);
2840 rule_destroy_cb(struct rule
*rule
)
2841 OVS_NO_THREAD_SAFETY_ANALYSIS
2843 /* Send rule removed if needed. */
2844 if (rule
->flags
& OFPUTIL_FF_SEND_FLOW_REM
2845 && rule
->removed_reason
!= OVS_OFPRR_NONE
2846 && !rule_is_hidden(rule
)) {
2847 ofproto_rule_send_removed(rule
);
2849 rule
->ofproto
->ofproto_class
->rule_destruct(rule
);
2850 mf_vl_mff_unref(&rule
->ofproto
->vl_mff_map
, rule
->match_tlv_bitmap
);
2851 mf_vl_mff_unref(&rule
->ofproto
->vl_mff_map
, rule
->ofpacts_tlv_bitmap
);
2852 ofproto_rule_destroy__(rule
);
2856 ofproto_rule_ref(struct rule
*rule
)
2859 ovs_refcount_ref(&rule
->ref_count
);
2864 ofproto_rule_try_ref(struct rule
*rule
)
2867 return ovs_refcount_try_ref_rcu(&rule
->ref_count
);
2872 /* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the
2873 * ref_count reaches 0.
2875 * Use of RCU allows short term use (between RCU quiescent periods) without
2876 * keeping a reference. A reference must be taken if the rule needs to
2877 * stay around accross the RCU quiescent periods. */
2879 ofproto_rule_unref(struct rule
*rule
)
2881 if (rule
&& ovs_refcount_unref_relaxed(&rule
->ref_count
) == 1) {
2882 ovs_assert(rule
->state
!= RULE_INSERTED
);
2883 ovsrcu_postpone(rule_destroy_cb
, rule
);
2888 remove_rule_rcu__(struct rule
*rule
)
2889 OVS_REQUIRES(ofproto_mutex
)
2891 struct ofproto
*ofproto
= rule
->ofproto
;
2892 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
2894 ovs_assert(!cls_rule_visible_in_version(&rule
->cr
, OVS_VERSION_MAX
));
2895 classifier_remove_assert(&table
->cls
, &rule
->cr
);
2896 if (ofproto
->ofproto_class
->rule_delete
) {
2897 ofproto
->ofproto_class
->rule_delete(rule
);
2899 ofproto_rule_unref(rule
);
2903 remove_rule_rcu(struct rule
*rule
)
2904 OVS_EXCLUDED(ofproto_mutex
)
2906 ovs_mutex_lock(&ofproto_mutex
);
2907 remove_rule_rcu__(rule
);
2908 ovs_mutex_unlock(&ofproto_mutex
);
2911 /* Removes and deletes rules from a NULL-terminated array of rule pointers. */
2913 remove_rules_rcu(struct rule
**rules
)
2914 OVS_EXCLUDED(ofproto_mutex
)
2916 struct rule
**orig_rules
= rules
;
2919 struct ofproto
*ofproto
= rules
[0]->ofproto
;
2920 unsigned long tables
[BITMAP_N_LONGS(256)];
2924 memset(tables
, 0, sizeof tables
);
2926 ovs_mutex_lock(&ofproto_mutex
);
2927 while ((rule
= *rules
++)) {
2928 /* Defer once for each new table. This defers the subtable cleanup
2929 * until later, so that when removing large number of flows the
2930 * operation is faster. */
2931 if (!bitmap_is_set(tables
, rule
->table_id
)) {
2932 struct classifier
*cls
= &ofproto
->tables
[rule
->table_id
].cls
;
2934 bitmap_set1(tables
, rule
->table_id
);
2935 classifier_defer(cls
);
2937 remove_rule_rcu__(rule
);
2940 BITMAP_FOR_EACH_1(table_id
, 256, tables
) {
2941 struct classifier
*cls
= &ofproto
->tables
[table_id
].cls
;
2943 classifier_publish(cls
);
2945 ovs_mutex_unlock(&ofproto_mutex
);
2952 ofproto_group_ref(struct ofgroup
*group
)
2955 ovs_refcount_ref(&group
->ref_count
);
2960 ofproto_group_try_ref(struct ofgroup
*group
)
2963 return ovs_refcount_try_ref_rcu(&group
->ref_count
);
2969 group_destroy_cb(struct ofgroup
*group
)
2971 group
->ofproto
->ofproto_class
->group_destruct(group
);
2972 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props
*,
2974 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list
*,
2976 group
->ofproto
->ofproto_class
->group_dealloc(group
);
2980 ofproto_group_unref(struct ofgroup
*group
)
2981 OVS_NO_THREAD_SAFETY_ANALYSIS
2983 if (group
&& ovs_refcount_unref_relaxed(&group
->ref_count
) == 1) {
2984 ovs_assert(rule_collection_n(&group
->rules
) == 0);
2985 ovsrcu_postpone(group_destroy_cb
, group
);
2990 remove_group_rcu__(struct ofgroup
*group
)
2991 OVS_REQUIRES(ofproto_mutex
)
2993 struct ofproto
*ofproto
= group
->ofproto
;
2995 ovs_assert(!versions_visible_in_version(&group
->versions
, OVS_VERSION_MAX
));
2997 cmap_remove(&ofproto
->groups
, &group
->cmap_node
,
2998 hash_int(group
->group_id
, 0));
2999 ofproto_group_unref(group
);
3003 remove_group_rcu(struct ofgroup
*group
)
3004 OVS_EXCLUDED(ofproto_mutex
)
3006 ovs_mutex_lock(&ofproto_mutex
);
3007 remove_group_rcu__(group
);
3008 ovs_mutex_unlock(&ofproto_mutex
);
3011 /* Removes and deletes groups from a NULL-terminated array of group
3014 remove_groups_rcu(struct ofgroup
**groups
)
3015 OVS_EXCLUDED(ofproto_mutex
)
3017 ovs_mutex_lock(&ofproto_mutex
);
3018 for (struct ofgroup
**g
= groups
; *g
; g
++) {
3019 remove_group_rcu__(*g
);
3021 ovs_mutex_unlock(&ofproto_mutex
);
3025 static bool ofproto_fix_meter_action(const struct ofproto
*,
3026 struct ofpact_meter
*);
3028 static bool ofproto_fix_controller_action(const struct ofproto
*,
3029 struct ofpact_controller
*);
3031 /* Creates and returns a new 'struct rule_actions', whose actions are a copy
3032 * of from the 'ofpacts_len' bytes of 'ofpacts'. */
3033 const struct rule_actions
*
3034 rule_actions_create(const struct ofpact
*ofpacts
, size_t ofpacts_len
)
3036 struct rule_actions
*actions
;
3038 actions
= xmalloc(sizeof *actions
+ ofpacts_len
);
3039 actions
->ofpacts_len
= ofpacts_len
;
3040 memcpy(actions
->ofpacts
, ofpacts
, ofpacts_len
);
3041 actions
->has_meter
= ofpacts_get_meter(ofpacts
, ofpacts_len
) != 0;
3042 actions
->has_groups
=
3043 (ofpact_find_type_flattened(ofpacts
, OFPACT_GROUP
,
3044 ofpact_end(ofpacts
, ofpacts_len
))
3046 actions
->has_learn_with_delete
= (next_learn_with_delete(actions
, NULL
)
3052 /* Free the actions after the RCU quiescent period is reached. */
3054 rule_actions_destroy(const struct rule_actions
*actions
)
3057 ovsrcu_postpone(free
, CONST_CAST(struct rule_actions
*, actions
));
3061 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
3062 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
3064 ofproto_rule_has_out_port(const struct rule
*rule
, ofp_port_t port
)
3065 OVS_REQUIRES(ofproto_mutex
)
3067 if (port
== OFPP_ANY
) {
3070 const struct rule_actions
*actions
= rule_get_actions(rule
);
3071 return ofpacts_output_to_port(actions
->ofpacts
,
3072 actions
->ofpacts_len
, port
);
3076 /* Returns true if 'rule' has group and equals group_id. */
3078 ofproto_rule_has_out_group(const struct rule
*rule
, uint32_t group_id
)
3079 OVS_REQUIRES(ofproto_mutex
)
3081 if (group_id
== OFPG_ANY
) {
3084 const struct rule_actions
*actions
= rule_get_actions(rule
);
3085 return ofpacts_output_to_group(actions
->ofpacts
,
3086 actions
->ofpacts_len
, group_id
);
3091 rule_is_readonly(const struct rule
*rule
)
3093 const struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
3094 return (table
->flags
& OFTABLE_READONLY
) != 0;
3098 hash_learned_cookie(ovs_be64 cookie_
, uint8_t table_id
)
3100 uint64_t cookie
= (OVS_FORCE
uint64_t) cookie_
;
3101 return hash_3words(cookie
, cookie
>> 32, table_id
);
3105 learned_cookies_update_one__(struct ofproto
*ofproto
,
3106 const struct ofpact_learn
*learn
,
3107 int delta
, struct ovs_list
*dead_cookies
)
3108 OVS_REQUIRES(ofproto_mutex
)
3110 uint32_t hash
= hash_learned_cookie(learn
->cookie
, learn
->table_id
);
3111 struct learned_cookie
*c
;
3113 HMAP_FOR_EACH_WITH_HASH (c
, hmap_node
, hash
, &ofproto
->learned_cookies
) {
3114 if (c
->cookie
== learn
->cookie
&& c
->table_id
== learn
->table_id
) {
3116 ovs_assert(c
->n
>= 0);
3119 hmap_remove(&ofproto
->learned_cookies
, &c
->hmap_node
);
3120 ovs_list_push_back(dead_cookies
, &c
->list_node
);
3127 ovs_assert(delta
> 0);
3128 c
= xmalloc(sizeof *c
);
3129 hmap_insert(&ofproto
->learned_cookies
, &c
->hmap_node
, hash
);
3130 c
->cookie
= learn
->cookie
;
3131 c
->table_id
= learn
->table_id
;
3135 static const struct ofpact_learn
*
3136 next_learn_with_delete(const struct rule_actions
*actions
,
3137 const struct ofpact_learn
*start
)
3139 const struct ofpact
*pos
;
3141 for (pos
= start
? ofpact_next(&start
->ofpact
) : actions
->ofpacts
;
3142 pos
< ofpact_end(actions
->ofpacts
, actions
->ofpacts_len
);
3143 pos
= ofpact_next(pos
)) {
3144 if (pos
->type
== OFPACT_LEARN
) {
3145 const struct ofpact_learn
*learn
= ofpact_get_LEARN(pos
);
3146 if (learn
->flags
& NX_LEARN_F_DELETE_LEARNED
) {
3156 learned_cookies_update__(struct ofproto
*ofproto
,
3157 const struct rule_actions
*actions
,
3158 int delta
, struct ovs_list
*dead_cookies
)
3159 OVS_REQUIRES(ofproto_mutex
)
3161 if (actions
->has_learn_with_delete
) {
3162 const struct ofpact_learn
*learn
;
3164 for (learn
= next_learn_with_delete(actions
, NULL
); learn
;
3165 learn
= next_learn_with_delete(actions
, learn
)) {
3166 learned_cookies_update_one__(ofproto
, learn
, delta
, dead_cookies
);
3172 learned_cookies_inc(struct ofproto
*ofproto
,
3173 const struct rule_actions
*actions
)
3174 OVS_REQUIRES(ofproto_mutex
)
3176 learned_cookies_update__(ofproto
, actions
, +1, NULL
);
3180 learned_cookies_dec(struct ofproto
*ofproto
,
3181 const struct rule_actions
*actions
,
3182 struct ovs_list
*dead_cookies
)
3183 OVS_REQUIRES(ofproto_mutex
)
3185 learned_cookies_update__(ofproto
, actions
, -1, dead_cookies
);
3189 learned_cookies_flush(struct ofproto
*ofproto
, struct ovs_list
*dead_cookies
)
3190 OVS_REQUIRES(ofproto_mutex
)
3192 struct learned_cookie
*c
;
3194 struct minimatch match
;
3196 minimatch_init_catchall(&match
);
3197 LIST_FOR_EACH_POP (c
, list_node
, dead_cookies
) {
3198 struct rule_criteria criteria
;
3199 struct rule_collection rules
;
3200 rule_criteria_init(&criteria
, c
->table_id
, &match
, 0, OVS_VERSION_MAX
,
3201 c
->cookie
, OVS_BE64_MAX
, OFPP_ANY
, OFPG_ANY
);
3202 rule_criteria_require_rw(&criteria
, false);
3203 collect_rules_loose(ofproto
, &criteria
, &rules
);
3204 rule_criteria_destroy(&criteria
);
3205 delete_flows__(&rules
, OFPRR_DELETE
, NULL
);
3209 minimatch_destroy(&match
);
3213 handle_echo_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3215 ofconn_send_reply(ofconn
, ofputil_encode_echo_reply(oh
));
3220 query_tables(struct ofproto
*ofproto
,
3221 struct ofputil_table_features
**featuresp
,
3222 struct ofputil_table_stats
**statsp
)
3224 struct mf_bitmap rw_fields
= oxm_writable_fields();
3225 struct mf_bitmap match
= oxm_matchable_fields();
3226 struct mf_bitmap mask
= oxm_maskable_fields();
3228 struct ofputil_table_features
*features
;
3229 struct ofputil_table_stats
*stats
;
3232 features
= *featuresp
= xcalloc(ofproto
->n_tables
, sizeof *features
);
3233 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3234 struct ofputil_table_features
*f
= &features
[i
];
3238 f
->metadata_match
= OVS_BE64_MAX
;
3239 f
->metadata_write
= OVS_BE64_MAX
;
3240 atomic_read_relaxed(&ofproto
->tables
[i
].miss_config
, &f
->miss_config
);
3241 f
->max_entries
= 1000000;
3243 bool more_tables
= false;
3244 for (int j
= i
+ 1; j
< ofproto
->n_tables
; j
++) {
3245 if (!(ofproto
->tables
[j
].flags
& OFTABLE_HIDDEN
)) {
3246 bitmap_set1(f
->nonmiss
.next
, j
);
3250 f
->nonmiss
.instructions
= (1u << N_OVS_INSTRUCTIONS
) - 1;
3252 f
->nonmiss
.instructions
&= ~(1u << OVSINST_OFPIT11_GOTO_TABLE
);
3254 f
->nonmiss
.write
.ofpacts
= (UINT64_C(1) << N_OFPACTS
) - 1;
3255 f
->nonmiss
.write
.set_fields
= rw_fields
;
3256 f
->nonmiss
.apply
= f
->nonmiss
.write
;
3257 f
->miss
= f
->nonmiss
;
3261 f
->wildcard
= match
;
3265 stats
= *statsp
= xcalloc(ofproto
->n_tables
, sizeof *stats
);
3266 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3267 struct ofputil_table_stats
*s
= &stats
[i
];
3270 s
->active_count
= ofproto
->tables
[i
].n_flows
;
3272 s
->active_count
-= connmgr_count_hidden_rules(
3280 ofproto
->ofproto_class
->query_tables(ofproto
, features
, stats
);
3282 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3283 const struct oftable
*table
= &ofproto
->tables
[i
];
3284 struct ofputil_table_features
*f
= &features
[i
];
3287 ovs_strzcpy(f
->name
, table
->name
, sizeof f
->name
);
3290 if (table
->max_flows
< f
->max_entries
) {
3291 f
->max_entries
= table
->max_flows
;
3297 query_switch_features(struct ofproto
*ofproto
,
3298 bool *arp_match_ip
, uint64_t *ofpacts
)
3300 struct ofputil_table_features
*features
, *f
;
3302 *arp_match_ip
= false;
3305 query_tables(ofproto
, &features
, NULL
);
3306 for (f
= features
; f
< &features
[ofproto
->n_tables
]; f
++) {
3307 *ofpacts
|= f
->nonmiss
.apply
.ofpacts
| f
->miss
.apply
.ofpacts
;
3308 if (bitmap_is_set(f
->match
.bm
, MFF_ARP_SPA
) ||
3309 bitmap_is_set(f
->match
.bm
, MFF_ARP_TPA
)) {
3310 *arp_match_ip
= true;
3316 ovs_assert(*ofpacts
& (UINT64_C(1) << OFPACT_OUTPUT
));
3320 handle_features_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3322 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3323 struct ofputil_switch_features features
;
3324 struct ofport
*port
;
3328 query_switch_features(ofproto
, &arp_match_ip
, &features
.ofpacts
);
3330 features
.datapath_id
= ofproto
->datapath_id
;
3331 features
.n_buffers
= 0;
3332 features
.n_tables
= ofproto_get_n_visible_tables(ofproto
);
3333 features
.capabilities
= (OFPUTIL_C_FLOW_STATS
| OFPUTIL_C_TABLE_STATS
|
3334 OFPUTIL_C_PORT_STATS
| OFPUTIL_C_QUEUE_STATS
|
3335 OFPUTIL_C_GROUP_STATS
| OFPUTIL_C_BUNDLES
);
3337 features
.capabilities
|= OFPUTIL_C_ARP_MATCH_IP
;
3339 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
3340 features
.auxiliary_id
= 0;
3341 b
= ofputil_encode_switch_features(&features
, ofconn_get_protocol(ofconn
),
3343 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3344 ofputil_put_switch_features_port(&port
->pp
, b
);
3347 ofconn_send_reply(ofconn
, b
);
3352 handle_get_config_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3354 struct ofputil_switch_config config
;
3355 config
.frag
= ofconn_get_ofproto(ofconn
)->frag_handling
;
3356 config
.invalid_ttl_to_controller
3357 = ofconn_get_invalid_ttl_to_controller(ofconn
);
3358 config
.miss_send_len
= ofconn_get_miss_send_len(ofconn
);
3360 ofconn_send_reply(ofconn
, ofputil_encode_get_config_reply(oh
, &config
));
3366 handle_set_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3368 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3369 struct ofputil_switch_config config
;
3372 error
= ofputil_decode_set_config(oh
, &config
);
3377 if (ofconn_get_type(ofconn
) != OFCONN_PRIMARY
3378 || ofconn_get_role(ofconn
) != OFPCR12_ROLE_SLAVE
) {
3379 enum ofputil_frag_handling cur
= ofproto
->frag_handling
;
3380 enum ofputil_frag_handling next
= config
.frag
;
3383 if (ofproto
->ofproto_class
->set_frag_handling(ofproto
, next
)) {
3384 ofproto
->frag_handling
= next
;
3386 VLOG_WARN_RL(&rl
, "%s: unsupported fragment handling mode %s",
3388 ofputil_frag_handling_to_string(next
));
3393 if (config
.invalid_ttl_to_controller
>= 0) {
3394 ofconn_set_invalid_ttl_to_controller(ofconn
,
3395 config
.invalid_ttl_to_controller
);
3398 ofconn_set_miss_send_len(ofconn
, config
.miss_send_len
);
3403 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
3404 * error message code for the caller to propagate upward. Otherwise, returns
3407 * The log message mentions 'msg_type'. */
3409 reject_slave_controller(struct ofconn
*ofconn
)
3411 if (ofconn_get_type(ofconn
) == OFCONN_PRIMARY
3412 && ofconn_get_role(ofconn
) == OFPCR12_ROLE_SLAVE
) {
3413 return OFPERR_OFPBRC_IS_SLAVE
;
3419 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
3422 * - If they use a meter, then 'ofproto' has that meter configured.
3423 * Updates the meter action with ofproto's datapath's provider_meter_id.
3425 * - If they use any groups, then 'ofproto' has that group configured.
3427 * Returns 0 if successful, otherwise an OpenFlow error. Caller must hold
3428 * 'ofproto_mutex' for the result to be valid also after this function
3431 ofproto_check_ofpacts(struct ofproto
*ofproto
,
3432 const struct ofpact ofpacts
[], size_t ofpacts_len
)
3433 OVS_REQUIRES(ofproto_mutex
)
3435 const struct ofpact
*a
;
3437 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
3438 if (a
->type
== OFPACT_METER
&&
3439 !ofproto_fix_meter_action(ofproto
, ofpact_get_METER(a
))) {
3440 return OFPERR_OFPMMFC_INVALID_METER
;
3443 if (a
->type
== OFPACT_CONTROLLER
) {
3444 struct ofpact_controller
*ca
= ofpact_get_CONTROLLER(a
);
3446 if (!ofproto_fix_controller_action(ofproto
, ca
)) {
3447 static struct vlog_rate_limit rl2
= VLOG_RATE_LIMIT_INIT(1, 5);
3448 VLOG_INFO_RL(&rl2
, "%s: controller action specified an "
3449 "unknown meter id: %d",
3450 ofproto
->name
, ca
->meter_id
);
3454 if (a
->type
== OFPACT_GROUP
3455 && !ofproto_group_exists(ofproto
, ofpact_get_GROUP(a
)->group_id
)) {
3456 return OFPERR_OFPBAC_BAD_OUT_GROUP
;
3464 ofproto_packet_out_uninit(struct ofproto_packet_out
*opo
)
3466 dp_packet_delete(opo
->packet
);
3471 opo
->ofpacts
= NULL
;
3472 opo
->ofpacts_len
= 0;
3473 ovs_assert(!opo
->aux
);
3476 /* Takes ownership of po->ofpacts, which must have been malloc'ed. */
3478 ofproto_packet_out_init(struct ofproto
*ofproto
,
3479 struct ofconn
*ofconn
,
3480 struct ofproto_packet_out
*opo
,
3481 const struct ofputil_packet_out
*po
)
3487 uint64_t buf
[FLOW_U64S
];
3490 uint16_t in_port
= ofp_to_u16(po
->flow_metadata
.flow
.in_port
.ofp_port
);
3491 if (in_port
>= ofproto
->max_ports
&& in_port
< ofp_to_u16(OFPP_MAX
)) {
3492 return OFPERR_OFPBRC_BAD_PORT
;
3496 if (po
->buffer_id
!= UINT32_MAX
) {
3497 return OFPERR_OFPBRC_BUFFER_UNKNOWN
;
3500 /* Ensure that the L3 header is 32-bit aligned. */
3501 opo
->packet
= dp_packet_clone_data_with_headroom(po
->packet
,
3503 /* Take the received packet_tpye as packet_type of the packet. */
3504 opo
->packet
->packet_type
= po
->flow_metadata
.flow
.packet_type
;
3506 /* Store struct flow. */
3507 opo
->flow
= xmalloc(sizeof *opo
->flow
);
3508 *opo
->flow
= po
->flow_metadata
.flow
;
3509 miniflow_extract(opo
->packet
, &m
.mf
);
3510 flow_union_with_miniflow(opo
->flow
, &m
.mf
);
3512 /* Check actions like for flow mods. We pass a 'table_id' of 0 to
3513 * ofproto_check_consistency(), which isn't strictly correct because these
3514 * actions aren't in any table. This is OK as 'table_id' is only used to
3515 * check instructions (e.g., goto-table), which can't appear on the action
3516 * list of a packet-out. */
3517 match_wc_init(&match
, opo
->flow
);
3518 struct ofpact_check_params cp
= {
3520 .max_ports
= u16_to_ofp(ofproto
->max_ports
),
3522 .n_tables
= ofproto
->n_tables
3524 error
= ofpacts_check_consistency(po
->ofpacts
, po
->ofpacts_len
,
3525 ofconn_get_protocol(ofconn
), &cp
);
3527 dp_packet_delete(opo
->packet
);
3532 opo
->ofpacts
= po
->ofpacts
;
3533 opo
->ofpacts_len
= po
->ofpacts_len
;
3540 ofproto_packet_out_start(struct ofproto
*ofproto
,
3541 struct ofproto_packet_out
*opo
)
3542 OVS_REQUIRES(ofproto_mutex
)
3546 error
= ofproto_check_ofpacts(ofproto
, opo
->ofpacts
, opo
->ofpacts_len
);
3551 return ofproto
->ofproto_class
->packet_xlate(ofproto
, opo
);
3555 ofproto_packet_out_revert(struct ofproto
*ofproto
,
3556 struct ofproto_packet_out
*opo
)
3557 OVS_REQUIRES(ofproto_mutex
)
3559 ofproto
->ofproto_class
->packet_xlate_revert(ofproto
, opo
);
3563 ofproto_packet_out_finish(struct ofproto
*ofproto
,
3564 struct ofproto_packet_out
*opo
)
3565 OVS_REQUIRES(ofproto_mutex
)
3567 ofproto
->ofproto_class
->packet_execute(ofproto
, opo
);
3571 handle_packet_out(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3572 OVS_EXCLUDED(ofproto_mutex
)
3574 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3575 struct ofputil_packet_out po
;
3576 struct ofproto_packet_out opo
;
3577 uint64_t ofpacts_stub
[1024 / 8];
3578 struct ofpbuf ofpacts
;
3581 COVERAGE_INC(ofproto_packet_out
);
3583 error
= reject_slave_controller(ofconn
);
3588 /* Decode message. */
3589 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
3590 error
= ofputil_decode_packet_out(&po
, oh
, ofproto_get_tun_tab(p
),
3593 ofpbuf_uninit(&ofpacts
);
3597 po
.ofpacts
= ofpbuf_steal_data(&ofpacts
); /* Move to heap. */
3598 error
= ofproto_packet_out_init(p
, ofconn
, &opo
, &po
);
3604 ovs_mutex_lock(&ofproto_mutex
);
3605 opo
.version
= p
->tables_version
;
3606 error
= ofproto_packet_out_start(p
, &opo
);
3608 ofproto_packet_out_finish(p
, &opo
);
3610 ovs_mutex_unlock(&ofproto_mutex
);
3612 ofproto_packet_out_uninit(&opo
);
3617 handle_nxt_resume(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3619 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3620 struct ofputil_packet_in_private pin
;
3623 error
= ofputil_decode_packet_in_private(oh
, false,
3624 ofproto_get_tun_tab(ofproto
),
3625 &ofproto
->vl_mff_map
, &pin
, NULL
,
3631 error
= (ofproto
->ofproto_class
->nxt_resume
3632 ? ofproto
->ofproto_class
->nxt_resume(ofproto
, &pin
)
3633 : OFPERR_NXR_NOT_SUPPORTED
);
3635 ofputil_packet_in_private_destroy(&pin
);
3641 update_port_config(struct ofconn
*ofconn
, struct ofport
*port
,
3642 enum ofputil_port_config config
,
3643 enum ofputil_port_config mask
)
3645 enum ofputil_port_config toggle
= (config
^ port
->pp
.config
) & mask
;
3647 if (toggle
& OFPUTIL_PC_PORT_DOWN
3648 && (config
& OFPUTIL_PC_PORT_DOWN
3649 ? netdev_turn_flags_off(port
->netdev
, NETDEV_UP
, NULL
)
3650 : netdev_turn_flags_on(port
->netdev
, NETDEV_UP
, NULL
))) {
3651 /* We tried to bring the port up or down, but it failed, so don't
3652 * update the "down" bit. */
3653 toggle
&= ~OFPUTIL_PC_PORT_DOWN
;
3657 struct ofputil_phy_port old_pp
= port
->pp
;
3659 port
->pp
.config
^= toggle
;
3660 port
->pp
.state
= normalize_state(port
->pp
.config
, port
->pp
.state
,
3663 port
->ofproto
->ofproto_class
->port_reconfigured(port
, old_pp
.config
);
3664 connmgr_send_port_status(port
->ofproto
->connmgr
, ofconn
, &old_pp
,
3665 &port
->pp
, OFPPR_MODIFY
);
3670 port_mod_start(struct ofconn
*ofconn
, struct ofputil_port_mod
*pm
,
3671 struct ofport
**port
)
3673 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3675 *port
= ofproto_get_port(p
, pm
->port_no
);
3677 return OFPERR_OFPPMFC_BAD_PORT
;
3679 if (!eth_addr_equals((*port
)->pp
.hw_addr
, pm
->hw_addr
) ||
3680 !eth_addr64_equals((*port
)->pp
.hw_addr64
, pm
->hw_addr64
)) {
3681 return OFPERR_OFPPMFC_BAD_HW_ADDR
;
3687 port_mod_finish(struct ofconn
*ofconn
, struct ofputil_port_mod
*pm
,
3688 struct ofport
*port
)
3690 update_port_config(ofconn
, port
, pm
->config
, pm
->mask
);
3691 if (pm
->advertise
) {
3692 netdev_set_advertisements(port
->netdev
, pm
->advertise
);
3697 handle_port_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3699 struct ofputil_port_mod pm
;
3700 struct ofport
*port
;
3703 error
= reject_slave_controller(ofconn
);
3708 error
= ofputil_decode_port_mod(oh
, &pm
, false);
3713 error
= port_mod_start(ofconn
, &pm
, &port
);
3715 port_mod_finish(ofconn
, &pm
, port
);
3721 handle_desc_stats_request(struct ofconn
*ofconn
,
3722 const struct ofp_header
*request
)
3724 static const char *default_mfr_desc
= "Nicira, Inc.";
3725 static const char *default_hw_desc
= "Open vSwitch";
3726 static const char *default_sw_desc
= VERSION
;
3727 static const char *default_serial_desc
= "None";
3728 static const char *default_dp_desc
= "None";
3730 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3731 struct ofp_desc_stats
*ods
;
3734 msg
= ofpraw_alloc_stats_reply(request
, 0);
3735 ods
= ofpbuf_put_zeros(msg
, sizeof *ods
);
3736 ovs_strlcpy(ods
->mfr_desc
, p
->mfr_desc
? p
->mfr_desc
: default_mfr_desc
,
3737 sizeof ods
->mfr_desc
);
3738 ovs_strlcpy(ods
->hw_desc
, p
->hw_desc
? p
->hw_desc
: default_hw_desc
,
3739 sizeof ods
->hw_desc
);
3740 ovs_strlcpy(ods
->sw_desc
, p
->sw_desc
? p
->sw_desc
: default_sw_desc
,
3741 sizeof ods
->sw_desc
);
3742 ovs_strlcpy(ods
->serial_num
,
3743 p
->serial_desc
? p
->serial_desc
: default_serial_desc
,
3744 sizeof ods
->serial_num
);
3745 ovs_strlcpy(ods
->dp_desc
, p
->dp_desc
? p
->dp_desc
: default_dp_desc
,
3746 sizeof ods
->dp_desc
);
3747 ofconn_send_reply(ofconn
, msg
);
3753 handle_table_stats_request(struct ofconn
*ofconn
,
3754 const struct ofp_header
*request
)
3756 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3757 struct ofputil_table_features
*features
;
3758 struct ofputil_table_stats
*stats
;
3759 struct ofpbuf
*reply
;
3762 query_tables(ofproto
, &features
, &stats
);
3764 reply
= ofputil_encode_table_stats_reply(request
);
3765 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3766 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3767 ofputil_append_table_stats_reply(reply
, &stats
[i
], &features
[i
]);
3770 ofconn_send_reply(ofconn
, reply
);
3779 handle_table_features_request(struct ofconn
*ofconn
,
3780 const struct ofp_header
*request
)
3782 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3783 struct ofpbuf msg
= ofpbuf_const_initializer(request
,
3784 ntohs(request
->length
));
3785 ofpraw_pull_assert(&msg
);
3786 if (msg
.size
|| ofpmp_more(request
)) {
3787 return OFPERR_OFPTFFC_EPERM
;
3790 struct ofputil_table_features
*features
;
3791 query_tables(ofproto
, &features
, NULL
);
3793 struct ovs_list replies
;
3794 ofpmp_init(&replies
, request
);
3795 for (size_t i
= 0; i
< ofproto
->n_tables
; i
++) {
3796 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3797 ofputil_append_table_features_reply(&features
[i
], &replies
);
3800 ofconn_send_replies(ofconn
, &replies
);
3807 /* Returns the vacancy of 'oftable', a number that ranges from 0 (if the table
3808 * is full) to 100 (if the table is empty).
3810 * A table without a limit on flows is considered to be empty. */
3812 oftable_vacancy(const struct oftable
*t
)
3814 return (!t
->max_flows
? 100
3815 : t
->n_flows
>= t
->max_flows
? 0
3816 : (t
->max_flows
- t
->n_flows
) * 100.0 / t
->max_flows
);
3820 query_table_desc__(struct ofputil_table_desc
*td
,
3821 struct ofproto
*ofproto
, uint8_t table_id
)
3823 const struct oftable
*t
= &ofproto
->tables
[table_id
];
3825 td
->table_id
= table_id
;
3826 td
->eviction
= (t
->eviction
& EVICTION_OPENFLOW
3827 ? OFPUTIL_TABLE_EVICTION_ON
3828 : OFPUTIL_TABLE_EVICTION_OFF
);
3829 td
->eviction_flags
= OFPROTO_EVICTION_FLAGS
;
3830 td
->vacancy
= (t
->vacancy_event
3831 ? OFPUTIL_TABLE_VACANCY_ON
3832 : OFPUTIL_TABLE_VACANCY_OFF
);
3833 td
->table_vacancy
.vacancy_down
= t
->vacancy_down
;
3834 td
->table_vacancy
.vacancy_up
= t
->vacancy_up
;
3835 td
->table_vacancy
.vacancy
= oftable_vacancy(t
);
3838 /* This function queries the database for dumping table-desc. */
3840 query_tables_desc(struct ofproto
*ofproto
, struct ofputil_table_desc
**descp
)
3842 struct ofputil_table_desc
*table_desc
;
3845 table_desc
= *descp
= xcalloc(ofproto
->n_tables
, sizeof *table_desc
);
3846 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3847 struct ofputil_table_desc
*td
= &table_desc
[i
];
3848 query_table_desc__(td
, ofproto
, i
);
3852 /* Function to handle dump-table-desc request. */
3854 handle_table_desc_request(struct ofconn
*ofconn
,
3855 const struct ofp_header
*request
)
3857 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3858 struct ofputil_table_desc
*table_desc
;
3859 struct ovs_list replies
;
3862 query_tables_desc(ofproto
, &table_desc
);
3863 ofpmp_init(&replies
, request
);
3864 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
3865 if (!(ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
)) {
3866 ofputil_append_table_desc_reply(&table_desc
[i
], &replies
,
3870 ofconn_send_replies(ofconn
, &replies
);
3875 /* This function determines and sends the vacancy event, based on the value
3876 * of current vacancy and threshold vacancy. If the current vacancy is less
3877 * than or equal to vacancy_down, vacancy up events must be enabled, and when
3878 * the current vacancy is greater or equal to vacancy_up, vacancy down events
3879 * must be enabled. */
3881 send_table_status(struct ofproto
*ofproto
, uint8_t table_id
)
3883 struct oftable
*t
= &ofproto
->tables
[table_id
];
3884 if (!t
->vacancy_event
) {
3888 uint8_t vacancy
= oftable_vacancy(t
);
3889 enum ofp14_table_reason event
;
3890 if (vacancy
< t
->vacancy_down
) {
3891 event
= OFPTR_VACANCY_DOWN
;
3892 } else if (vacancy
> t
->vacancy_up
) {
3893 event
= OFPTR_VACANCY_UP
;
3898 if (event
== t
->vacancy_event
) {
3899 struct ofputil_table_desc td
;
3900 query_table_desc__(&td
, ofproto
, table_id
);
3901 connmgr_send_table_status(ofproto
->connmgr
, &td
, event
);
3903 t
->vacancy_event
= (event
== OFPTR_VACANCY_DOWN
3905 : OFPTR_VACANCY_DOWN
);
3910 append_port_stat(struct ofport
*port
, struct ovs_list
*replies
)
3912 struct ofputil_port_stats ops
= { .port_no
= port
->pp
.port_no
};
3914 calc_duration(port
->created
, time_msec(),
3915 &ops
.duration_sec
, &ops
.duration_nsec
);
3917 /* Intentionally ignore return value, since errors will set
3918 * 'stats' to all-1s, which is correct for OpenFlow, and
3919 * netdev_get_stats() will log errors. */
3920 ofproto_port_get_stats(port
, &ops
.stats
);
3921 netdev_get_custom_stats(port
->netdev
, &ops
.custom_stats
);
3923 ofputil_append_port_stat(replies
, &ops
);
3925 netdev_free_custom_stats_counters(&ops
.custom_stats
);
3929 handle_port_request(struct ofconn
*ofconn
,
3930 const struct ofp_header
*request
, ofp_port_t port_no
,
3931 void (*cb
)(struct ofport
*, struct ovs_list
*replies
))
3933 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3934 struct ofport
*port
;
3935 struct ovs_list replies
;
3937 ofpmp_init(&replies
, request
);
3938 if (port_no
!= OFPP_ANY
) {
3939 port
= ofproto_get_port(ofproto
, port_no
);
3944 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3949 ofconn_send_replies(ofconn
, &replies
);
3953 handle_port_stats_request(struct ofconn
*ofconn
,
3954 const struct ofp_header
*request
)
3959 error
= ofputil_decode_port_stats_request(request
, &port_no
);
3961 handle_port_request(ofconn
, request
, port_no
, append_port_stat
);
3967 append_port_desc(struct ofport
*port
, struct ovs_list
*replies
)
3969 ofputil_append_port_desc_stats_reply(&port
->pp
, replies
);
3973 handle_port_desc_stats_request(struct ofconn
*ofconn
,
3974 const struct ofp_header
*request
)
3979 error
= ofputil_decode_port_desc_stats_request(request
, &port_no
);
3981 handle_port_request(ofconn
, request
, port_no
, append_port_desc
);
3987 hash_cookie(ovs_be64 cookie
)
3989 return hash_uint64((OVS_FORCE
uint64_t)cookie
);
3993 cookies_insert(struct ofproto
*ofproto
, struct rule
*rule
)
3994 OVS_REQUIRES(ofproto_mutex
)
3996 hindex_insert(&ofproto
->cookies
, &rule
->cookie_node
,
3997 hash_cookie(rule
->flow_cookie
));
4001 cookies_remove(struct ofproto
*ofproto
, struct rule
*rule
)
4002 OVS_REQUIRES(ofproto_mutex
)
4004 hindex_remove(&ofproto
->cookies
, &rule
->cookie_node
);
4008 calc_duration(long long int start
, long long int now
,
4009 uint32_t *sec
, uint32_t *nsec
)
4011 long long int msecs
= now
- start
;
4012 *sec
= msecs
/ 1000;
4013 *nsec
= (msecs
% 1000) * (1000 * 1000);
4016 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
4017 * true if 'table_id' is OK, false otherwise. */
4019 check_table_id(const struct ofproto
*ofproto
, uint8_t table_id
)
4021 return table_id
== OFPTT_ALL
|| table_id
< ofproto
->n_tables
;
4024 static struct oftable
*
4025 next_visible_table(const struct ofproto
*ofproto
, uint8_t table_id
)
4027 struct oftable
*table
;
4029 for (table
= &ofproto
->tables
[table_id
];
4030 table
< &ofproto
->tables
[ofproto
->n_tables
];
4032 if (!(table
->flags
& OFTABLE_HIDDEN
)) {
4040 static struct oftable
*
4041 first_matching_table(const struct ofproto
*ofproto
, uint8_t table_id
)
4043 if (table_id
== 0xff) {
4044 return next_visible_table(ofproto
, 0);
4045 } else if (table_id
< ofproto
->n_tables
) {
4046 return &ofproto
->tables
[table_id
];
4052 static struct oftable
*
4053 next_matching_table(const struct ofproto
*ofproto
,
4054 const struct oftable
*table
, uint8_t table_id
)
4056 return (table_id
== 0xff
4057 ? next_visible_table(ofproto
, (table
- ofproto
->tables
) + 1)
4061 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
4063 * - If TABLE_ID is 0xff, this iterates over every classifier table in
4064 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
4066 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
4067 * only once, for that table. (This can be used to access tables marked
4070 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
4071 * entered at all. (Perhaps you should have validated TABLE_ID with
4072 * check_table_id().)
4074 * All parameters are evaluated multiple times.
4076 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
4077 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
4079 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
4081 /* Initializes 'criteria' in a straightforward way based on the other
4084 * By default, the criteria include flows that are read-only, on the assumption
4085 * that the collected flows won't be modified. Call rule_criteria_require_rw()
4086 * if flows will be modified.
4088 * For "loose" matching, the 'priority' parameter is unimportant and may be
4091 rule_criteria_init(struct rule_criteria
*criteria
, uint8_t table_id
,
4092 const struct minimatch
*match
, int priority
,
4093 ovs_version_t version
, ovs_be64 cookie
,
4094 ovs_be64 cookie_mask
, ofp_port_t out_port
,
4097 criteria
->table_id
= table_id
;
4098 cls_rule_init_from_minimatch(&criteria
->cr
, match
, priority
);
4099 criteria
->version
= version
;
4100 criteria
->cookie
= cookie
;
4101 criteria
->cookie_mask
= cookie_mask
;
4102 criteria
->out_port
= out_port
;
4103 criteria
->out_group
= out_group
;
4105 /* We ordinarily want to skip hidden rules, but there has to be a way for
4106 * code internal to OVS to modify and delete them, so if the criteria
4107 * specify a priority that can only be for a hidden flow, then allow hidden
4108 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
4109 * with hidden flows because OpenFlow uses only a 16-bit field to specify
4111 criteria
->include_hidden
= priority
> UINT16_MAX
;
4113 /* We assume that the criteria are being used to collect flows for reading
4114 * but not modification. Thus, we should collect read-only flows. */
4115 criteria
->include_readonly
= true;
4118 /* By default, criteria initialized by rule_criteria_init() will match flows
4119 * that are read-only, on the assumption that the collected flows won't be
4120 * modified. Call this function to match only flows that are be modifiable.
4122 * Specify 'can_write_readonly' as false in ordinary circumstances, true if the
4123 * caller has special privileges that allow it to modify even "read-only"
4126 rule_criteria_require_rw(struct rule_criteria
*criteria
,
4127 bool can_write_readonly
)
4129 criteria
->include_readonly
= can_write_readonly
;
4133 rule_criteria_destroy(struct rule_criteria
*criteria
)
4135 cls_rule_destroy(&criteria
->cr
);
4136 criteria
->version
= OVS_VERSION_NOT_REMOVED
; /* Mark as destroyed. */
4139 /* Schedules postponed removal of rules, destroys 'rules'. */
4141 remove_rules_postponed(struct rule_collection
*rules
)
4142 OVS_REQUIRES(ofproto_mutex
)
4144 if (rule_collection_n(rules
) > 0) {
4145 if (rule_collection_n(rules
) == 1) {
4146 ovsrcu_postpone(remove_rule_rcu
, rule_collection_rules(rules
)[0]);
4147 rule_collection_init(rules
);
4149 ovsrcu_postpone(remove_rules_rcu
, rule_collection_detach(rules
));
4154 /* Schedules postponed removal of groups, destroys 'groups'. */
4156 remove_groups_postponed(struct group_collection
*groups
)
4157 OVS_REQUIRES(ofproto_mutex
)
4159 if (group_collection_n(groups
) > 0) {
4160 if (group_collection_n(groups
) == 1) {
4161 ovsrcu_postpone(remove_group_rcu
,
4162 group_collection_groups(groups
)[0]);
4163 group_collection_init(groups
);
4165 ovsrcu_postpone(remove_groups_rcu
,
4166 group_collection_detach(groups
));
4171 /* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This
4172 * function verifies most of the criteria in 'c' itself, but the caller must
4173 * check 'c->cr' itself.
4175 * Rules that have already been marked for removal are not collected.
4177 * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and
4178 * 'c' only includes modifiable rules). */
4180 collect_rule(struct rule
*rule
, const struct rule_criteria
*c
,
4181 struct rule_collection
*rules
, size_t *n_readonly
)
4182 OVS_REQUIRES(ofproto_mutex
)
4184 if ((c
->table_id
== rule
->table_id
|| c
->table_id
== 0xff)
4185 && ofproto_rule_has_out_port(rule
, c
->out_port
)
4186 && ofproto_rule_has_out_group(rule
, c
->out_group
)
4187 && !((rule
->flow_cookie
^ c
->cookie
) & c
->cookie_mask
)
4188 && (!rule_is_hidden(rule
) || c
->include_hidden
)
4189 && cls_rule_visible_in_version(&rule
->cr
, c
->version
)) {
4190 /* Rule matches all the criteria... */
4191 if (!rule_is_readonly(rule
) || c
->include_readonly
) {
4193 rule_collection_add(rules
, rule
);
4195 /* ...except it's read-only. */
4201 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4202 * on classifiers rules are done in the "loose" way required for OpenFlow
4203 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
4206 * Returns 0 on success, otherwise an OpenFlow error code. */
4208 collect_rules_loose(struct ofproto
*ofproto
,
4209 const struct rule_criteria
*criteria
,
4210 struct rule_collection
*rules
)
4211 OVS_REQUIRES(ofproto_mutex
)
4213 struct oftable
*table
;
4214 enum ofperr error
= 0;
4215 size_t n_readonly
= 0;
4217 rule_collection_init(rules
);
4219 if (!check_table_id(ofproto
, criteria
->table_id
)) {
4220 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4224 if (criteria
->cookie_mask
== OVS_BE64_MAX
) {
4227 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
4228 hash_cookie(criteria
->cookie
),
4229 &ofproto
->cookies
) {
4230 if (cls_rule_is_loose_match(&rule
->cr
, &criteria
->cr
.match
)) {
4231 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4235 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
4238 CLS_FOR_EACH_TARGET (rule
, cr
, &table
->cls
, &criteria
->cr
,
4239 criteria
->version
) {
4240 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4246 if (!error
&& !rule_collection_n(rules
) && n_readonly
) {
4247 /* We didn't find any rules to modify. We did find some read-only
4248 * rules that we're not allowed to modify, so report that. */
4249 error
= OFPERR_OFPBRC_EPERM
;
4252 rule_collection_destroy(rules
);
4257 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4258 * on classifiers rules are done in the "strict" way required for OpenFlow
4259 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
4260 * rules on list 'rules'.
4262 * Returns 0 on success, otherwise an OpenFlow error code. */
4264 collect_rules_strict(struct ofproto
*ofproto
,
4265 const struct rule_criteria
*criteria
,
4266 struct rule_collection
*rules
)
4267 OVS_REQUIRES(ofproto_mutex
)
4269 struct oftable
*table
;
4270 size_t n_readonly
= 0;
4271 enum ofperr error
= 0;
4273 rule_collection_init(rules
);
4275 if (!check_table_id(ofproto
, criteria
->table_id
)) {
4276 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4280 if (criteria
->cookie_mask
== OVS_BE64_MAX
) {
4283 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
4284 hash_cookie(criteria
->cookie
),
4285 &ofproto
->cookies
) {
4286 if (cls_rule_equal(&rule
->cr
, &criteria
->cr
)) {
4287 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4291 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
4294 rule
= rule_from_cls_rule(classifier_find_rule_exactly(
4295 &table
->cls
, &criteria
->cr
,
4296 criteria
->version
));
4298 collect_rule(rule
, criteria
, rules
, &n_readonly
);
4304 if (!error
&& !rule_collection_n(rules
) && n_readonly
) {
4305 /* We didn't find any rules to modify. We did find some read-only
4306 * rules that we're not allowed to modify, so report that. */
4307 error
= OFPERR_OFPBRC_EPERM
;
4310 rule_collection_destroy(rules
);
4315 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
4316 * forced into the range of a uint16_t. */
4318 age_secs(long long int age_ms
)
4320 return (age_ms
< 0 ? 0
4321 : age_ms
>= UINT16_MAX
* 1000 ? UINT16_MAX
4322 : (unsigned int) age_ms
/ 1000);
4326 handle_flow_stats_request(struct ofconn
*ofconn
,
4327 const struct ofp_header
*request
)
4328 OVS_EXCLUDED(ofproto_mutex
)
4330 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4331 struct ofputil_flow_stats_request fsr
;
4332 struct rule_criteria criteria
;
4333 struct rule_collection rules
;
4334 struct ovs_list replies
;
4337 error
= ofputil_decode_flow_stats_request(&fsr
, request
,
4338 ofproto_get_tun_tab(ofproto
),
4339 &ofproto
->vl_mff_map
);
4344 struct minimatch match
;
4345 minimatch_init(&match
, &fsr
.match
);
4346 rule_criteria_init(&criteria
, fsr
.table_id
, &match
, 0, OVS_VERSION_MAX
,
4347 fsr
.cookie
, fsr
.cookie_mask
, fsr
.out_port
,
4349 minimatch_destroy(&match
);
4351 ovs_mutex_lock(&ofproto_mutex
);
4352 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4353 rule_criteria_destroy(&criteria
);
4355 rule_collection_ref(&rules
);
4357 ovs_mutex_unlock(&ofproto_mutex
);
4363 ofpmp_init(&replies
, request
);
4365 RULE_COLLECTION_FOR_EACH (rule
, &rules
) {
4366 long long int now
= time_msec();
4367 struct ofputil_flow_stats fs
;
4368 long long int created
, used
, modified
;
4369 const struct rule_actions
*actions
;
4370 enum ofputil_flow_mod_flags flags
;
4372 ovs_mutex_lock(&rule
->mutex
);
4373 fs
.cookie
= rule
->flow_cookie
;
4374 fs
.idle_timeout
= rule
->idle_timeout
;
4375 fs
.hard_timeout
= rule
->hard_timeout
;
4376 fs
.importance
= rule
->importance
;
4377 created
= rule
->created
;
4378 modified
= rule
->modified
;
4379 actions
= rule_get_actions(rule
);
4380 flags
= rule
->flags
;
4381 ovs_mutex_unlock(&rule
->mutex
);
4383 ofproto
->ofproto_class
->rule_get_stats(rule
, &fs
.packet_count
,
4384 &fs
.byte_count
, &used
);
4386 minimatch_expand(&rule
->cr
.match
, &fs
.match
);
4387 fs
.table_id
= rule
->table_id
;
4388 calc_duration(created
, now
, &fs
.duration_sec
, &fs
.duration_nsec
);
4389 fs
.priority
= rule
->cr
.priority
;
4390 fs
.idle_age
= age_secs(now
- used
);
4391 fs
.hard_age
= age_secs(now
- modified
);
4392 fs
.ofpacts
= actions
->ofpacts
;
4393 fs
.ofpacts_len
= actions
->ofpacts_len
;
4396 ofputil_append_flow_stats_reply(&fs
, &replies
,
4397 ofproto_get_tun_tab(ofproto
));
4400 rule_collection_unref(&rules
);
4401 rule_collection_destroy(&rules
);
4403 ofconn_send_replies(ofconn
, &replies
);
4409 flow_stats_ds(struct ofproto
*ofproto
, struct rule
*rule
, struct ds
*results
)
4411 uint64_t packet_count
, byte_count
;
4412 const struct rule_actions
*actions
;
4413 long long int created
, used
;
4415 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
4416 &byte_count
, &used
);
4418 ovs_mutex_lock(&rule
->mutex
);
4419 actions
= rule_get_actions(rule
);
4420 created
= rule
->created
;
4421 ovs_mutex_unlock(&rule
->mutex
);
4423 if (rule
->table_id
!= 0) {
4424 ds_put_format(results
, "table_id=%"PRIu8
", ", rule
->table_id
);
4426 ds_put_format(results
, "duration=%llds, ", (time_msec() - created
) / 1000);
4427 ds_put_format(results
, "n_packets=%"PRIu64
", ", packet_count
);
4428 ds_put_format(results
, "n_bytes=%"PRIu64
", ", byte_count
);
4429 cls_rule_format(&rule
->cr
, ofproto_get_tun_tab(ofproto
), NULL
, results
);
4430 ds_put_char(results
, ',');
4432 ds_put_cstr(results
, "actions=");
4433 struct ofpact_format_params fp
= { .s
= results
};
4434 ofpacts_format(actions
->ofpacts
, actions
->ofpacts_len
, &fp
);
4436 ds_put_cstr(results
, "\n");
4439 /* Adds a pretty-printed description of all flows to 'results', including
4440 * hidden flows (e.g., set up by in-band control). */
4442 ofproto_get_all_flows(struct ofproto
*p
, struct ds
*results
)
4444 struct oftable
*table
;
4446 OFPROTO_FOR_EACH_TABLE (table
, p
) {
4449 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
4450 flow_stats_ds(p
, rule
, results
);
4455 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
4456 * '*engine_type' and '*engine_id', respectively. */
4458 ofproto_get_netflow_ids(const struct ofproto
*ofproto
,
4459 uint8_t *engine_type
, uint8_t *engine_id
)
4461 ofproto
->ofproto_class
->get_netflow_ids(ofproto
, engine_type
, engine_id
);
4464 /* Checks the status change of CFM on 'ofport'.
4466 * Returns true if 'ofproto_class' does not support 'cfm_status_changed'. */
4468 ofproto_port_cfm_status_changed(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
4470 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
4471 return (ofport
&& ofproto
->ofproto_class
->cfm_status_changed
4472 ? ofproto
->ofproto_class
->cfm_status_changed(ofport
)
4476 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'.
4477 * Returns 0 if the port's CFM status was successfully stored into
4478 * '*status'. Returns positive errno if the port did not have CFM
4481 * The caller must provide and own '*status', and must free 'status->rmps'.
4482 * '*status' is indeterminate if the return value is non-zero. */
4484 ofproto_port_get_cfm_status(const struct ofproto
*ofproto
, ofp_port_t ofp_port
,
4485 struct cfm_status
*status
)
4487 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
4488 return (ofport
&& ofproto
->ofproto_class
->get_cfm_status
4489 ? ofproto
->ofproto_class
->get_cfm_status(ofport
, status
)
4494 handle_aggregate_stats_request(struct ofconn
*ofconn
,
4495 const struct ofp_header
*oh
)
4496 OVS_EXCLUDED(ofproto_mutex
)
4498 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4499 struct ofputil_flow_stats_request request
;
4500 struct ofputil_aggregate_stats stats
;
4501 bool unknown_packets
, unknown_bytes
;
4502 struct rule_criteria criteria
;
4503 struct rule_collection rules
;
4504 struct ofpbuf
*reply
;
4507 error
= ofputil_decode_flow_stats_request(&request
, oh
,
4508 ofproto_get_tun_tab(ofproto
),
4509 &ofproto
->vl_mff_map
);
4514 struct minimatch match
;
4515 minimatch_init(&match
, &request
.match
);
4516 rule_criteria_init(&criteria
, request
.table_id
, &match
, 0,
4517 OVS_VERSION_MAX
, request
.cookie
, request
.cookie_mask
,
4518 request
.out_port
, request
.out_group
);
4519 minimatch_destroy(&match
);
4521 ovs_mutex_lock(&ofproto_mutex
);
4522 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4523 rule_criteria_destroy(&criteria
);
4525 rule_collection_ref(&rules
);
4527 ovs_mutex_unlock(&ofproto_mutex
);
4533 memset(&stats
, 0, sizeof stats
);
4534 unknown_packets
= unknown_bytes
= false;
4537 RULE_COLLECTION_FOR_EACH (rule
, &rules
) {
4538 uint64_t packet_count
;
4539 uint64_t byte_count
;
4542 ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
4543 &byte_count
, &used
);
4545 if (packet_count
== UINT64_MAX
) {
4546 unknown_packets
= true;
4548 stats
.packet_count
+= packet_count
;
4551 if (byte_count
== UINT64_MAX
) {
4552 unknown_bytes
= true;
4554 stats
.byte_count
+= byte_count
;
4559 if (unknown_packets
) {
4560 stats
.packet_count
= UINT64_MAX
;
4562 if (unknown_bytes
) {
4563 stats
.byte_count
= UINT64_MAX
;
4566 rule_collection_unref(&rules
);
4567 rule_collection_destroy(&rules
);
4569 reply
= ofputil_encode_aggregate_stats_reply(&stats
, oh
);
4570 ofconn_send_reply(ofconn
, reply
);
4575 struct queue_stats_cbdata
{
4576 struct ofport
*ofport
;
4577 struct ovs_list replies
;
4582 put_queue_stats(struct queue_stats_cbdata
*cbdata
, uint32_t queue_id
,
4583 const struct netdev_queue_stats
*stats
)
4585 struct ofputil_queue_stats oqs
;
4587 oqs
.port_no
= cbdata
->ofport
->pp
.port_no
;
4588 oqs
.queue_id
= queue_id
;
4589 oqs
.tx_bytes
= stats
->tx_bytes
;
4590 oqs
.tx_packets
= stats
->tx_packets
;
4591 oqs
.tx_errors
= stats
->tx_errors
;
4592 if (stats
->created
!= LLONG_MIN
) {
4593 calc_duration(stats
->created
, cbdata
->now
,
4594 &oqs
.duration_sec
, &oqs
.duration_nsec
);
4596 oqs
.duration_sec
= oqs
.duration_nsec
= UINT32_MAX
;
4598 ofputil_append_queue_stat(&cbdata
->replies
, &oqs
);
4602 handle_queue_stats_dump_cb(uint32_t queue_id
,
4603 struct netdev_queue_stats
*stats
,
4606 struct queue_stats_cbdata
*cbdata
= cbdata_
;
4608 put_queue_stats(cbdata
, queue_id
, stats
);
4612 handle_queue_stats_for_port(struct ofport
*port
, uint32_t queue_id
,
4613 struct queue_stats_cbdata
*cbdata
)
4615 cbdata
->ofport
= port
;
4616 if (queue_id
== OFPQ_ALL
) {
4617 netdev_dump_queue_stats(port
->netdev
,
4618 handle_queue_stats_dump_cb
, cbdata
);
4620 struct netdev_queue_stats stats
;
4622 if (!netdev_get_queue_stats(port
->netdev
, queue_id
, &stats
)) {
4623 put_queue_stats(cbdata
, queue_id
, &stats
);
4625 return OFPERR_OFPQOFC_BAD_QUEUE
;
4632 handle_queue_stats_request(struct ofconn
*ofconn
,
4633 const struct ofp_header
*rq
)
4635 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4636 struct queue_stats_cbdata cbdata
;
4637 struct ofport
*port
;
4639 struct ofputil_queue_stats_request oqsr
;
4641 COVERAGE_INC(ofproto_queue_req
);
4643 ofpmp_init(&cbdata
.replies
, rq
);
4644 cbdata
.now
= time_msec();
4646 error
= ofputil_decode_queue_stats_request(rq
, &oqsr
);
4651 if (oqsr
.port_no
== OFPP_ANY
) {
4652 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
4653 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
4654 if (!handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)) {
4659 port
= ofproto_get_port(ofproto
, oqsr
.port_no
);
4661 ? handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)
4662 : OFPERR_OFPQOFC_BAD_PORT
);
4665 ofconn_send_replies(ofconn
, &cbdata
.replies
);
4667 ofpbuf_list_delete(&cbdata
.replies
);
4674 evict_rules_from_table(struct oftable
*table
)
4675 OVS_REQUIRES(ofproto_mutex
)
4677 enum ofperr error
= 0;
4678 struct rule_collection rules
;
4679 unsigned int count
= table
->n_flows
;
4680 unsigned int max_flows
= table
->max_flows
;
4682 rule_collection_init(&rules
);
4684 while (count
-- > max_flows
) {
4687 if (!choose_rule_to_evict(table
, &rule
)) {
4688 error
= OFPERR_OFPFMFC_TABLE_FULL
;
4691 eviction_group_remove_rule(rule
);
4692 rule_collection_add(&rules
, rule
);
4695 delete_flows__(&rules
, OFPRR_EVICTION
, NULL
);
4701 get_conjunctions(const struct ofputil_flow_mod
*fm
,
4702 struct cls_conjunction
**conjsp
, size_t *n_conjsp
)
4704 struct cls_conjunction
*conjs
= NULL
;
4707 const struct ofpact
*ofpact
;
4708 OFPACT_FOR_EACH (ofpact
, fm
->ofpacts
, fm
->ofpacts_len
) {
4709 if (ofpact
->type
== OFPACT_CONJUNCTION
) {
4711 } else if (ofpact
->type
!= OFPACT_NOTE
) {
4712 /* "conjunction" may appear with "note" actions but not with any
4713 * other type of actions. */
4714 ovs_assert(!n_conjs
);
4721 conjs
= xzalloc(n_conjs
* sizeof *conjs
);
4722 OFPACT_FOR_EACH (ofpact
, fm
->ofpacts
, fm
->ofpacts_len
) {
4723 if (ofpact
->type
== OFPACT_CONJUNCTION
) {
4724 struct ofpact_conjunction
*oc
= ofpact_get_CONJUNCTION(ofpact
);
4725 conjs
[i
].clause
= oc
->clause
;
4726 conjs
[i
].n_clauses
= oc
->n_clauses
;
4727 conjs
[i
].id
= oc
->id
;
4734 *n_conjsp
= n_conjs
;
4737 /* add_flow_init(), add_flow_start(), add_flow_revert(), and add_flow_finish()
4738 * implement OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
4739 * in which no matching flow already exists in the flow table.
4741 * add_flow_init() creates a new flow according to 'fm' and stores it to 'ofm'
4742 * for later reference. If the flow replaces other flow, it will be updated to
4743 * match modify semantics later by add_flow_start() (by calling
4744 * replace_rule_start()).
4746 * Returns 0 on success, or an OpenFlow error code on failure.
4748 * On successful return the caller must complete the operation by calling
4749 * add_flow_start(), and if that succeeds, then either add_flow_finish(), or
4750 * add_flow_revert() if the operation needs to be reverted due to a later
4754 add_flow_init(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4755 const struct ofputil_flow_mod
*fm
)
4756 OVS_EXCLUDED(ofproto_mutex
)
4758 struct oftable
*table
;
4763 if (!check_table_id(ofproto
, fm
->table_id
)) {
4764 return OFPERR_OFPBRC_BAD_TABLE_ID
;
4768 if (fm
->table_id
== 0xff) {
4769 if (ofproto
->ofproto_class
->rule_choose_table
) {
4770 error
= ofproto
->ofproto_class
->rule_choose_table(ofproto
,
4776 ovs_assert(table_id
< ofproto
->n_tables
);
4780 } else if (fm
->table_id
< ofproto
->n_tables
) {
4781 table_id
= fm
->table_id
;
4783 return OFPERR_OFPBRC_BAD_TABLE_ID
;
4786 table
= &ofproto
->tables
[table_id
];
4787 if (table
->flags
& OFTABLE_READONLY
4788 && !(fm
->flags
& OFPUTIL_FF_NO_READONLY
)) {
4789 return OFPERR_OFPBRC_EPERM
;
4792 if (!(fm
->flags
& OFPUTIL_FF_HIDDEN_FIELDS
)
4793 && !minimatch_has_default_hidden_fields(&fm
->match
)) {
4794 VLOG_WARN_RL(&rl
, "%s: (add_flow) only internal flows can set "
4795 "non-default values to hidden fields", ofproto
->name
);
4796 return OFPERR_OFPBRC_EPERM
;
4799 if (!ofm
->temp_rule
) {
4800 cls_rule_init_from_minimatch(&cr
, &fm
->match
, fm
->priority
);
4802 /* Allocate new rule. Destroys 'cr'. */
4803 uint64_t map
= miniflow_get_tun_metadata_present_map(fm
->match
.flow
);
4804 error
= ofproto_rule_create(ofproto
, &cr
, table
- ofproto
->tables
,
4805 fm
->new_cookie
, fm
->idle_timeout
,
4806 fm
->hard_timeout
, fm
->flags
,
4807 fm
->importance
, fm
->ofpacts
,
4808 fm
->ofpacts_len
, map
,
4809 fm
->ofpacts_tlv_bitmap
, &ofm
->temp_rule
);
4814 get_conjunctions(fm
, &ofm
->conjs
, &ofm
->n_conjs
);
4819 /* ofm->temp_rule is consumed only in the successful case. */
4821 add_flow_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
4822 OVS_REQUIRES(ofproto_mutex
)
4824 struct rule
*old_rule
= NULL
;
4825 struct rule
*new_rule
= ofm
->temp_rule
;
4826 const struct rule_actions
*actions
= rule_get_actions(new_rule
);
4827 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
4830 /* Must check actions while holding ofproto_mutex to avoid a race. */
4831 error
= ofproto_check_ofpacts(ofproto
, actions
->ofpacts
,
4832 actions
->ofpacts_len
);
4837 /* Check for the existence of an identical rule.
4838 * This will not return rules earlier marked for removal. */
4839 old_rule
= rule_from_cls_rule(classifier_find_rule_exactly(&table
->cls
,
4843 /* Check for overlap, if requested. */
4844 if (new_rule
->flags
& OFPUTIL_FF_CHECK_OVERLAP
4845 && classifier_rule_overlaps(&table
->cls
, &new_rule
->cr
,
4847 return OFPERR_OFPFMFC_OVERLAP
;
4850 /* If necessary, evict an existing rule to clear out space. */
4851 if (table
->n_flows
>= table
->max_flows
) {
4852 if (!choose_rule_to_evict(table
, &old_rule
)) {
4853 return OFPERR_OFPFMFC_TABLE_FULL
;
4855 eviction_group_remove_rule(old_rule
);
4856 /* Marks 'old_rule' as an evicted rule rather than replaced rule.
4858 old_rule
->removed_reason
= OFPRR_EVICTION
;
4861 ofm
->modify_cookie
= true;
4865 rule_collection_add(&ofm
->old_rules
, old_rule
);
4867 /* Take ownership of the temp_rule. */
4868 rule_collection_add(&ofm
->new_rules
, new_rule
);
4869 ofm
->temp_rule
= NULL
;
4871 replace_rule_start(ofproto
, ofm
, old_rule
, new_rule
);
4875 /* Revert the effects of add_flow_start(). */
4877 add_flow_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
4878 OVS_REQUIRES(ofproto_mutex
)
4880 struct rule
*old_rule
= rule_collection_n(&ofm
->old_rules
)
4881 ? rule_collection_rules(&ofm
->old_rules
)[0] : NULL
;
4882 struct rule
*new_rule
= rule_collection_rules(&ofm
->new_rules
)[0];
4884 replace_rule_revert(ofproto
, old_rule
, new_rule
);
4887 /* To be called after version bump. */
4889 add_flow_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
4890 const struct openflow_mod_requester
*req
)
4891 OVS_REQUIRES(ofproto_mutex
)
4893 struct rule
*old_rule
= rule_collection_n(&ofm
->old_rules
)
4894 ? rule_collection_rules(&ofm
->old_rules
)[0] : NULL
;
4895 struct rule
*new_rule
= rule_collection_rules(&ofm
->new_rules
)[0];
4896 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
4898 replace_rule_finish(ofproto
, ofm
, req
, old_rule
, new_rule
, &dead_cookies
);
4899 learned_cookies_flush(ofproto
, &dead_cookies
);
4902 ovsrcu_postpone(remove_rule_rcu
, old_rule
);
4904 ofmonitor_report(ofproto
->connmgr
, new_rule
, NXFME_ADDED
, 0,
4905 req
? req
->ofconn
: NULL
,
4906 req
? req
->request
->xid
: 0, NULL
);
4908 /* Send Vacancy Events for OF1.4+. */
4909 send_table_status(ofproto
, new_rule
->table_id
);
4913 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4915 /* Create a new rule. Note that the rule is NOT inserted into a any data
4916 * structures yet. Takes ownership of 'cr'. Only assigns '*new_rule' if
4919 ofproto_rule_create(struct ofproto
*ofproto
, struct cls_rule
*cr
,
4920 uint8_t table_id
, ovs_be64 new_cookie
,
4921 uint16_t idle_timeout
, uint16_t hard_timeout
,
4922 enum ofputil_flow_mod_flags flags
, uint16_t importance
,
4923 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
4924 uint64_t match_tlv_bitmap
, uint64_t ofpacts_tlv_bitmap
,
4925 struct rule
**new_rule
)
4926 OVS_NO_THREAD_SAFETY_ANALYSIS
4931 /* Allocate new rule. */
4932 rule
= ofproto
->ofproto_class
->rule_alloc();
4934 cls_rule_destroy(cr
);
4935 VLOG_WARN_RL(&rl
, "%s: failed to allocate a rule.", ofproto
->name
);
4936 return OFPERR_OFPFMFC_UNKNOWN
;
4939 /* Initialize base state. */
4940 *CONST_CAST(struct ofproto
**, &rule
->ofproto
) = ofproto
;
4941 cls_rule_move(CONST_CAST(struct cls_rule
*, &rule
->cr
), cr
);
4942 ovs_refcount_init(&rule
->ref_count
);
4944 ovs_mutex_init(&rule
->mutex
);
4945 ovs_mutex_lock(&rule
->mutex
);
4946 *CONST_CAST(ovs_be64
*, &rule
->flow_cookie
) = new_cookie
;
4947 rule
->created
= rule
->modified
= time_msec();
4948 rule
->idle_timeout
= idle_timeout
;
4949 rule
->hard_timeout
= hard_timeout
;
4950 *CONST_CAST(uint16_t *, &rule
->importance
) = importance
;
4951 rule
->removed_reason
= OVS_OFPRR_NONE
;
4953 *CONST_CAST(uint8_t *, &rule
->table_id
) = table_id
;
4954 rule
->flags
= flags
& OFPUTIL_FF_STATE
;
4956 *CONST_CAST(const struct rule_actions
**, &rule
->actions
)
4957 = rule_actions_create(ofpacts
, ofpacts_len
);
4959 ovs_list_init(&rule
->meter_list_node
);
4960 rule
->eviction_group
= NULL
;
4961 rule
->monitor_flags
= 0;
4962 rule
->add_seqno
= 0;
4963 rule
->modify_seqno
= 0;
4964 ovs_list_init(&rule
->expirable
);
4965 ovs_mutex_unlock(&rule
->mutex
);
4967 /* Construct rule, initializing derived state. */
4968 error
= ofproto
->ofproto_class
->rule_construct(rule
);
4970 ofproto_rule_destroy__(rule
);
4974 rule
->state
= RULE_INITIALIZED
;
4975 rule
->match_tlv_bitmap
= match_tlv_bitmap
;
4976 rule
->ofpacts_tlv_bitmap
= ofpacts_tlv_bitmap
;
4977 mf_vl_mff_ref(&rule
->ofproto
->vl_mff_map
, match_tlv_bitmap
);
4978 mf_vl_mff_ref(&rule
->ofproto
->vl_mff_map
, ofpacts_tlv_bitmap
);
4984 /* Initialize 'ofm' for a learn action. If the rule already existed, reference
4985 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
4986 * rule reference in both. This does not take the global 'ofproto_mutex'. */
4988 ofproto_flow_mod_init_for_learn(struct ofproto
*ofproto
,
4989 const struct ofputil_flow_mod
*fm
,
4990 struct ofproto_flow_mod
*ofm
)
4991 OVS_EXCLUDED(ofproto_mutex
)
4993 /* Reject flow mods that do not look like they were generated by a learn
4995 if (fm
->command
!= OFPFC_MODIFY_STRICT
|| fm
->table_id
== OFPTT_ALL
4996 || fm
->flags
& OFPUTIL_FF_RESET_COUNTS
4997 || fm
->buffer_id
!= UINT32_MAX
) {
4998 return OFPERR_OFPFMFC_UNKNOWN
;
5001 /* Check if the rule already exists, and we can get a reference to it. */
5002 struct oftable
*table
= &ofproto
->tables
[fm
->table_id
];
5005 rule
= rule_from_cls_rule(classifier_find_minimatch_exactly(
5006 &table
->cls
, &fm
->match
, fm
->priority
,
5009 /* Check if the rule's attributes match as well. */
5010 const struct rule_actions
*actions
;
5012 ovs_mutex_lock(&rule
->mutex
);
5013 actions
= rule_get_actions(rule
);
5014 if (rule
->idle_timeout
== fm
->idle_timeout
5015 && rule
->hard_timeout
== fm
->hard_timeout
5016 && rule
->importance
== fm
->importance
5017 && rule
->flags
== (fm
->flags
& OFPUTIL_FF_STATE
)
5018 && (!fm
->modify_cookie
|| (fm
->new_cookie
== rule
->flow_cookie
))
5019 && ofpacts_equal(fm
->ofpacts
, fm
->ofpacts_len
,
5020 actions
->ofpacts
, actions
->ofpacts_len
)) {
5021 /* Rule already exists and need not change, except for the modified
5022 * timestamp. Get a reference to the existing rule. */
5023 ovs_mutex_unlock(&rule
->mutex
);
5024 if (!ofproto_rule_try_ref(rule
)) {
5025 rule
= NULL
; /* Pretend it did not exist. */
5028 ovs_mutex_unlock(&rule
->mutex
);
5033 return ofproto_flow_mod_init(ofproto
, ofm
, fm
, rule
);
5037 ofproto_flow_mod_learn_refresh(struct ofproto_flow_mod
*ofm
)
5039 enum ofperr error
= 0;
5041 /* ofm->temp_rule is our reference to the learned rule. We have a
5042 * reference to an existing rule, if it already was in the classifier,
5043 * otherwise we may have a fresh rule that we need to insert. */
5044 struct rule
*rule
= ofm
->temp_rule
;
5046 return OFPERR_OFPFMFC_UNKNOWN
;
5049 /* Create a new rule if the current one has been removed from the
5050 * classifier. We need to do this since RCU does not allow a current rule
5051 * to be reinserted before all threads have quiesced.
5053 * It is possible that the rule is removed asynchronously, e.g., right
5054 * after we have read the 'rule->state' below. In this case the next time
5055 * this function is executed the rule will be reinstated. */
5056 if (rule
->state
== RULE_REMOVED
) {
5059 cls_rule_clone(&cr
, &rule
->cr
);
5060 ovs_mutex_lock(&rule
->mutex
);
5061 error
= ofproto_rule_create(rule
->ofproto
, &cr
, rule
->table_id
,
5064 rule
->hard_timeout
, rule
->flags
,
5066 rule
->actions
->ofpacts
,
5067 rule
->actions
->ofpacts_len
,
5068 rule
->match_tlv_bitmap
,
5069 rule
->ofpacts_tlv_bitmap
,
5071 ovs_mutex_unlock(&rule
->mutex
);
5073 ofproto_rule_unref(rule
); /* Release old reference. */
5076 /* Refresh the existing rule. */
5077 ovs_mutex_lock(&rule
->mutex
);
5078 rule
->modified
= time_msec();
5079 ovs_mutex_unlock(&rule
->mutex
);
5085 ofproto_flow_mod_learn_start(struct ofproto_flow_mod
*ofm
)
5086 OVS_REQUIRES(ofproto_mutex
)
5088 struct rule
*rule
= ofm
->temp_rule
;
5090 /* ofproto_flow_mod_start() consumes the reference, so we
5091 * take a new one. */
5092 ofproto_rule_ref(rule
);
5093 enum ofperr error
= ofproto_flow_mod_start(rule
->ofproto
, ofm
);
5094 ofm
->temp_rule
= rule
;
5100 ofproto_flow_mod_learn_revert(struct ofproto_flow_mod
*ofm
)
5101 OVS_REQUIRES(ofproto_mutex
)
5103 struct rule
*rule
= rule_collection_rules(&ofm
->new_rules
)[0];
5104 ofproto_flow_mod_revert(rule
->ofproto
, ofm
);
5108 ofproto_flow_mod_learn_finish(struct ofproto_flow_mod
*ofm
,
5109 struct ofproto
*orig_ofproto
)
5110 OVS_REQUIRES(ofproto_mutex
)
5112 struct rule
*rule
= rule_collection_rules(&ofm
->new_rules
)[0];
5114 /* If learning on a different bridge, must bump its version
5115 * number and flush connmgr afterwards. */
5116 if (rule
->ofproto
!= orig_ofproto
) {
5117 ofproto_bump_tables_version(rule
->ofproto
);
5119 ofproto_flow_mod_finish(rule
->ofproto
, ofm
, NULL
);
5120 if (rule
->ofproto
!= orig_ofproto
) {
5121 ofmonitor_flush(rule
->ofproto
->connmgr
);
5125 /* Refresh 'ofm->temp_rule', for which the caller holds a reference, if already
5126 * in the classifier, insert it otherwise. If the rule has already been
5127 * removed from the classifier, a new rule is created using 'ofm->temp_rule' as
5128 * a template and the reference to the old 'ofm->temp_rule' is freed. If
5129 * 'keep_ref' is true, then a reference to the current rule is held, otherwise
5130 * it is released and 'ofm->temp_rule' is set to NULL.
5132 * If 'limit' != 0, insertion will fail if there are more than 'limit' rules
5133 * in the same table with the same cookie. If insertion succeeds,
5134 * '*below_limitp' will be set to true. If insertion fails '*below_limitp'
5135 * will be set to false.
5137 * Caller needs to be the exclusive owner of 'ofm' as it is being manipulated
5138 * during the call. */
5140 ofproto_flow_mod_learn(struct ofproto_flow_mod
*ofm
, bool keep_ref
,
5141 unsigned limit
, bool *below_limitp
)
5142 OVS_EXCLUDED(ofproto_mutex
)
5144 enum ofperr error
= ofproto_flow_mod_learn_refresh(ofm
);
5145 struct rule
*rule
= ofm
->temp_rule
;
5146 bool below_limit
= true;
5148 /* Do we need to insert the rule? */
5149 if (!error
&& rule
->state
== RULE_INITIALIZED
) {
5150 ovs_mutex_lock(&ofproto_mutex
);
5153 struct rule_criteria criteria
;
5154 struct rule_collection rules
;
5155 struct minimatch match
;
5157 minimatch_init_catchall(&match
);
5158 rule_criteria_init(&criteria
, rule
->table_id
, &match
, 0,
5159 OVS_VERSION_MAX
, rule
->flow_cookie
,
5160 OVS_BE64_MAX
, OFPP_ANY
, OFPG_ANY
);
5161 minimatch_destroy(&match
);
5163 rule_criteria_require_rw(&criteria
, false);
5164 collect_rules_loose(rule
->ofproto
, &criteria
, &rules
);
5165 if (rule_collection_n(&rules
) >= limit
) {
5166 below_limit
= false;
5168 rule_collection_destroy(&rules
);
5169 rule_criteria_destroy(&criteria
);
5173 ofm
->version
= rule
->ofproto
->tables_version
+ 1;
5175 error
= ofproto_flow_mod_learn_start(ofm
);
5177 ofproto_flow_mod_learn_finish(ofm
, NULL
);
5180 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
5181 VLOG_INFO_RL(&rll
, "Learn limit for flow %"PRIu64
" reached.",
5184 ofproto_flow_mod_uninit(ofm
);
5186 ovs_mutex_unlock(&ofproto_mutex
);
5189 if (!keep_ref
&& below_limit
) {
5190 ofproto_rule_unref(rule
);
5191 ofm
->temp_rule
= NULL
;
5194 *below_limitp
= below_limit
;
5200 replace_rule_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5201 struct rule
*old_rule
, struct rule
*new_rule
)
5203 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
5205 /* 'old_rule' may be either an evicted rule or replaced rule. */
5207 /* Copy values from old rule for modify semantics. */
5208 if (old_rule
->removed_reason
!= OFPRR_EVICTION
) {
5209 bool change_cookie
= (ofm
->modify_cookie
5210 && new_rule
->flow_cookie
!= OVS_BE64_MAX
5211 && new_rule
->flow_cookie
!= old_rule
->flow_cookie
);
5213 ovs_mutex_lock(&new_rule
->mutex
);
5214 ovs_mutex_lock(&old_rule
->mutex
);
5215 if (ofm
->command
!= OFPFC_ADD
) {
5216 new_rule
->idle_timeout
= old_rule
->idle_timeout
;
5217 new_rule
->hard_timeout
= old_rule
->hard_timeout
;
5218 *CONST_CAST(uint16_t *, &new_rule
->importance
) = old_rule
->importance
;
5219 new_rule
->flags
= old_rule
->flags
;
5220 new_rule
->created
= old_rule
->created
;
5222 if (!change_cookie
) {
5223 *CONST_CAST(ovs_be64
*, &new_rule
->flow_cookie
)
5224 = old_rule
->flow_cookie
;
5226 ovs_mutex_unlock(&old_rule
->mutex
);
5227 ovs_mutex_unlock(&new_rule
->mutex
);
5230 /* Mark the old rule for removal in the next version. */
5231 cls_rule_make_invisible_in_version(&old_rule
->cr
, ofm
->version
);
5233 /* Remove the old rule from data structures. */
5234 ofproto_rule_remove__(ofproto
, old_rule
);
5238 /* Insert flow to ofproto data structures, so that later flow_mods may
5239 * relate to it. This is reversible, in case later errors require this to
5241 ofproto_rule_insert__(ofproto
, new_rule
);
5242 /* Make the new rule visible for classifier lookups only from the next
5244 classifier_insert(&table
->cls
, &new_rule
->cr
, ofm
->version
, ofm
->conjs
,
5249 replace_rule_revert(struct ofproto
*ofproto
,
5250 struct rule
*old_rule
, struct rule
*new_rule
)
5252 struct oftable
*table
= &ofproto
->tables
[new_rule
->table_id
];
5255 if (old_rule
->removed_reason
== OFPRR_EVICTION
) {
5256 /* Revert the eviction. */
5257 eviction_group_add_rule(old_rule
);
5260 /* Restore the old rule to data structures. */
5261 ofproto_rule_insert__(ofproto
, old_rule
);
5263 /* Restore the original visibility of the old rule. */
5264 cls_rule_restore_visibility(&old_rule
->cr
);
5266 /* Restore table's rule count. */
5270 /* Remove the new rule immediately. It was never visible to lookups. */
5271 classifier_remove_assert(&table
->cls
, &new_rule
->cr
);
5272 ofproto_rule_remove__(ofproto
, new_rule
);
5273 ofproto_rule_unref(new_rule
);
5276 /* Adds the 'new_rule', replacing the 'old_rule'. */
5278 replace_rule_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5279 const struct openflow_mod_requester
*req
,
5280 struct rule
*old_rule
, struct rule
*new_rule
,
5281 struct ovs_list
*dead_cookies
)
5282 OVS_REQUIRES(ofproto_mutex
)
5284 struct rule
*replaced_rule
;
5286 replaced_rule
= (old_rule
&& old_rule
->removed_reason
!= OFPRR_EVICTION
)
5289 /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule'
5290 * is a duplicate rule the 'new_rule' is replacing. The provider should
5291 * link the packet and byte counts from the old rule to the new one if
5292 * 'modify_keep_counts' is 'true'. The 'replaced_rule' will be deleted
5293 * right after this call. */
5294 ofproto
->ofproto_class
->rule_insert(new_rule
, replaced_rule
,
5295 ofm
->modify_keep_counts
);
5296 learned_cookies_inc(ofproto
, rule_get_actions(new_rule
));
5299 const struct rule_actions
*old_actions
= rule_get_actions(old_rule
);
5300 const struct rule_actions
*new_actions
= rule_get_actions(new_rule
);
5302 learned_cookies_dec(ofproto
, old_actions
, dead_cookies
);
5304 if (replaced_rule
) {
5305 enum nx_flow_update_event event
= ofm
->command
== OFPFC_ADD
5306 ? NXFME_ADDED
: NXFME_MODIFIED
;
5308 bool changed_cookie
= (new_rule
->flow_cookie
5309 != old_rule
->flow_cookie
);
5311 bool changed_actions
= !ofpacts_equal(new_actions
->ofpacts
,
5312 new_actions
->ofpacts_len
,
5313 old_actions
->ofpacts
,
5314 old_actions
->ofpacts_len
);
5316 if (event
!= NXFME_MODIFIED
|| changed_actions
5317 || changed_cookie
) {
5318 ofmonitor_report(ofproto
->connmgr
, new_rule
, event
, 0,
5319 req
? req
->ofconn
: NULL
,
5320 req
? req
->request
->xid
: 0,
5321 changed_actions
? old_actions
: NULL
);
5324 /* XXX: This is slight duplication with delete_flows_finish__() */
5325 ofmonitor_report(ofproto
->connmgr
, old_rule
, NXFME_DELETED
,
5327 req
? req
->ofconn
: NULL
,
5328 req
? req
->request
->xid
: 0, NULL
);
5333 /* ofm->temp_rule is consumed only in the successful case. */
5335 modify_flows_start__(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5336 OVS_REQUIRES(ofproto_mutex
)
5338 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5339 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5342 if (rule_collection_n(old_rules
) > 0) {
5343 /* Create a new 'modified' rule for each old rule. */
5344 struct rule
*old_rule
, *new_rule
;
5345 const struct rule_actions
*actions
= rule_get_actions(ofm
->temp_rule
);
5347 /* Must check actions while holding ofproto_mutex to avoid a race. */
5348 error
= ofproto_check_ofpacts(ofproto
, actions
->ofpacts
,
5349 actions
->ofpacts_len
);
5354 /* Use the temp rule as the first new rule, and as the template for
5356 struct rule
*temp
= ofm
->temp_rule
;
5357 ofm
->temp_rule
= NULL
; /* We consume the template. */
5360 RULE_COLLECTION_FOR_EACH (old_rule
, old_rules
) {
5362 /* The template rule's match is possibly a loose one, so it
5363 * must be replaced with the old rule's match so that the new
5364 * rule actually replaces the old one. */
5365 cls_rule_destroy(CONST_CAST(struct cls_rule
*, &temp
->cr
));
5366 cls_rule_clone(CONST_CAST(struct cls_rule
*, &temp
->cr
),
5368 if (temp
->match_tlv_bitmap
!= old_rule
->match_tlv_bitmap
) {
5369 mf_vl_mff_unref(&temp
->ofproto
->vl_mff_map
,
5370 temp
->match_tlv_bitmap
);
5371 temp
->match_tlv_bitmap
= old_rule
->match_tlv_bitmap
;
5372 mf_vl_mff_ref(&temp
->ofproto
->vl_mff_map
,
5373 temp
->match_tlv_bitmap
);
5375 *CONST_CAST(uint8_t *, &temp
->table_id
) = old_rule
->table_id
;
5376 rule_collection_add(new_rules
, temp
);
5380 cls_rule_clone(&cr
, &old_rule
->cr
);
5381 error
= ofproto_rule_create(ofproto
, &cr
, old_rule
->table_id
,
5384 temp
->hard_timeout
, temp
->flags
,
5386 temp
->actions
->ofpacts
,
5387 temp
->actions
->ofpacts_len
,
5388 old_rule
->match_tlv_bitmap
,
5389 temp
->ofpacts_tlv_bitmap
,
5392 rule_collection_add(new_rules
, new_rule
);
5394 /* Return the template rule in place in the error case. */
5395 ofm
->temp_rule
= temp
;
5396 rule_collection_rules(new_rules
)[0] = NULL
;
5398 rule_collection_unref(new_rules
);
5399 rule_collection_destroy(new_rules
);
5404 ovs_assert(rule_collection_n(new_rules
)
5405 == rule_collection_n(old_rules
));
5407 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5408 replace_rule_start(ofproto
, ofm
, old_rule
, new_rule
);
5410 } else if (ofm
->modify_may_add_flow
) {
5411 /* No match, add a new flow, consumes 'temp'. */
5412 error
= add_flow_start(ofproto
, ofm
);
5414 /* No flow to modify and may not add a flow. */
5415 ofproto_rule_unref(ofm
->temp_rule
);
5416 ofm
->temp_rule
= NULL
; /* We consume the template. */
5424 modify_flows_init_loose(struct ofproto
*ofproto
,
5425 struct ofproto_flow_mod
*ofm
,
5426 const struct ofputil_flow_mod
*fm
)
5427 OVS_EXCLUDED(ofproto_mutex
)
5429 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, 0,
5430 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
,
5432 rule_criteria_require_rw(&ofm
->criteria
,
5433 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5434 /* Must create a new flow in advance for the case that no matches are
5435 * found. Also used for template for multiple modified flows. */
5436 add_flow_init(ofproto
, ofm
, fm
);
5441 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5444 modify_flows_start_loose(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5445 OVS_REQUIRES(ofproto_mutex
)
5447 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5450 error
= collect_rules_loose(ofproto
, &ofm
->criteria
, old_rules
);
5453 error
= modify_flows_start__(ofproto
, ofm
);
5457 rule_collection_destroy(old_rules
);
5464 modify_flows_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5465 OVS_REQUIRES(ofproto_mutex
)
5467 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5468 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5470 /* Old rules were not changed yet, only need to revert new rules. */
5471 if (rule_collection_n(old_rules
) > 0) {
5472 struct rule
*old_rule
, *new_rule
;
5473 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5474 replace_rule_revert(ofproto
, old_rule
, new_rule
);
5476 rule_collection_destroy(new_rules
);
5477 rule_collection_destroy(old_rules
);
5482 modify_flows_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5483 const struct openflow_mod_requester
*req
)
5484 OVS_REQUIRES(ofproto_mutex
)
5486 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5487 struct rule_collection
*new_rules
= &ofm
->new_rules
;
5489 if (rule_collection_n(old_rules
) == 0
5490 && rule_collection_n(new_rules
) == 1) {
5491 add_flow_finish(ofproto
, ofm
, req
);
5492 } else if (rule_collection_n(old_rules
) > 0) {
5493 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
5495 ovs_assert(rule_collection_n(new_rules
)
5496 == rule_collection_n(old_rules
));
5498 struct rule
*old_rule
, *new_rule
;
5499 RULE_COLLECTIONS_FOR_EACH (old_rule
, new_rule
, old_rules
, new_rules
) {
5500 replace_rule_finish(ofproto
, ofm
, req
, old_rule
, new_rule
,
5503 learned_cookies_flush(ofproto
, &dead_cookies
);
5504 remove_rules_postponed(old_rules
);
5509 modify_flow_init_strict(struct ofproto
*ofproto OVS_UNUSED
,
5510 struct ofproto_flow_mod
*ofm
,
5511 const struct ofputil_flow_mod
*fm
)
5512 OVS_EXCLUDED(ofproto_mutex
)
5514 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
5515 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
,
5517 rule_criteria_require_rw(&ofm
->criteria
,
5518 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5519 /* Must create a new flow in advance for the case that no matches are
5520 * found. Also used for template for multiple modified flows. */
5521 add_flow_init(ofproto
, ofm
, fm
);
5526 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
5527 * code on failure. */
5529 modify_flow_start_strict(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5530 OVS_REQUIRES(ofproto_mutex
)
5532 struct rule_collection
*old_rules
= &ofm
->old_rules
;
5535 error
= collect_rules_strict(ofproto
, &ofm
->criteria
, old_rules
);
5538 /* collect_rules_strict() can return max 1 rule. */
5539 error
= modify_flows_start__(ofproto
, ofm
);
5545 /* OFPFC_DELETE implementation. */
5548 delete_flows_start__(struct ofproto
*ofproto
, ovs_version_t version
,
5549 const struct rule_collection
*rules
)
5550 OVS_REQUIRES(ofproto_mutex
)
5554 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5555 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5558 cls_rule_make_invisible_in_version(&rule
->cr
, version
);
5560 /* Remove rule from ofproto data structures. */
5561 ofproto_rule_remove__(ofproto
, rule
);
5566 delete_flows_revert__(struct ofproto
*ofproto
,
5567 const struct rule_collection
*rules
)
5568 OVS_REQUIRES(ofproto_mutex
)
5572 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5573 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5575 /* Add rule back to ofproto data structures. */
5576 ofproto_rule_insert__(ofproto
, rule
);
5578 /* Restore table's rule count. */
5581 /* Restore the original visibility of the rule. */
5582 cls_rule_restore_visibility(&rule
->cr
);
5587 delete_flows_finish__(struct ofproto
*ofproto
,
5588 struct rule_collection
*rules
,
5589 enum ofp_flow_removed_reason reason
,
5590 const struct openflow_mod_requester
*req
)
5591 OVS_REQUIRES(ofproto_mutex
)
5593 if (rule_collection_n(rules
)) {
5594 struct ovs_list dead_cookies
= OVS_LIST_INITIALIZER(&dead_cookies
);
5597 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
5598 /* This value will be used to send the flow removed message right
5599 * before the rule is actually destroyed. */
5600 rule
->removed_reason
= reason
;
5602 ofmonitor_report(ofproto
->connmgr
, rule
, NXFME_DELETED
, reason
,
5603 req
? req
->ofconn
: NULL
,
5604 req
? req
->request
->xid
: 0, NULL
);
5606 /* Send Vacancy Event for OF1.4+. */
5607 send_table_status(ofproto
, rule
->table_id
);
5609 learned_cookies_dec(ofproto
, rule_get_actions(rule
),
5612 remove_rules_postponed(rules
);
5614 learned_cookies_flush(ofproto
, &dead_cookies
);
5618 /* Deletes the rules listed in 'rules'.
5619 * The deleted rules will become invisible to the lookups in the next version.
5620 * Destroys 'rules'. */
5622 delete_flows__(struct rule_collection
*rules
,
5623 enum ofp_flow_removed_reason reason
,
5624 const struct openflow_mod_requester
*req
)
5625 OVS_REQUIRES(ofproto_mutex
)
5627 if (rule_collection_n(rules
)) {
5628 struct ofproto
*ofproto
= rule_collection_rules(rules
)[0]->ofproto
;
5630 delete_flows_start__(ofproto
, ofproto
->tables_version
+ 1, rules
);
5631 ofproto_bump_tables_version(ofproto
);
5632 delete_flows_finish__(ofproto
, rules
, reason
, req
);
5633 ofmonitor_flush(ofproto
->connmgr
);
5638 delete_flows_init_loose(struct ofproto
*ofproto OVS_UNUSED
,
5639 struct ofproto_flow_mod
*ofm
,
5640 const struct ofputil_flow_mod
*fm
)
5641 OVS_EXCLUDED(ofproto_mutex
)
5643 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, 0,
5644 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
,
5645 fm
->out_port
, fm
->out_group
);
5646 rule_criteria_require_rw(&ofm
->criteria
,
5647 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5651 /* Implements OFPFC_DELETE. */
5653 delete_flows_start_loose(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5654 OVS_REQUIRES(ofproto_mutex
)
5656 struct rule_collection
*rules
= &ofm
->old_rules
;
5659 error
= collect_rules_loose(ofproto
, &ofm
->criteria
, rules
);
5662 delete_flows_start__(ofproto
, ofm
->version
, rules
);
5669 delete_flows_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5670 OVS_REQUIRES(ofproto_mutex
)
5672 delete_flows_revert__(ofproto
, &ofm
->old_rules
);
5676 delete_flows_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
5677 const struct openflow_mod_requester
*req
)
5678 OVS_REQUIRES(ofproto_mutex
)
5680 delete_flows_finish__(ofproto
, &ofm
->old_rules
, OFPRR_DELETE
, req
);
5684 delete_flows_init_strict(struct ofproto
*ofproto OVS_UNUSED
,
5685 struct ofproto_flow_mod
*ofm
,
5686 const struct ofputil_flow_mod
*fm
)
5687 OVS_EXCLUDED(ofproto_mutex
)
5689 rule_criteria_init(&ofm
->criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
5690 OVS_VERSION_MAX
, fm
->cookie
, fm
->cookie_mask
,
5691 fm
->out_port
, fm
->out_group
);
5692 rule_criteria_require_rw(&ofm
->criteria
,
5693 (fm
->flags
& OFPUTIL_FF_NO_READONLY
) != 0);
5697 /* Implements OFPFC_DELETE_STRICT. */
5699 delete_flow_start_strict(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
5700 OVS_REQUIRES(ofproto_mutex
)
5702 struct rule_collection
*rules
= &ofm
->old_rules
;
5705 error
= collect_rules_strict(ofproto
, &ofm
->criteria
, rules
);
5708 delete_flows_start__(ofproto
, ofm
->version
, rules
);
5714 /* This may only be called by rule_destroy_cb()! */
5716 ofproto_rule_send_removed(struct rule
*rule
)
5717 OVS_EXCLUDED(ofproto_mutex
)
5719 struct ofputil_flow_removed fr
;
5722 minimatch_expand(&rule
->cr
.match
, &fr
.match
);
5723 fr
.priority
= rule
->cr
.priority
;
5725 /* Synchronize with connmgr_destroy() calls to prevent connmgr disappearing
5726 * while we use it. */
5727 ovs_mutex_lock(&ofproto_mutex
);
5728 struct connmgr
*connmgr
= rule
->ofproto
->connmgr
;
5730 ovs_mutex_unlock(&ofproto_mutex
);
5734 fr
.cookie
= rule
->flow_cookie
;
5735 fr
.reason
= rule
->removed_reason
;
5736 fr
.table_id
= rule
->table_id
;
5737 calc_duration(rule
->created
, time_msec(),
5738 &fr
.duration_sec
, &fr
.duration_nsec
);
5739 ovs_mutex_lock(&rule
->mutex
);
5740 fr
.idle_timeout
= rule
->idle_timeout
;
5741 fr
.hard_timeout
= rule
->hard_timeout
;
5742 ovs_mutex_unlock(&rule
->mutex
);
5743 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &fr
.packet_count
,
5744 &fr
.byte_count
, &used
);
5745 connmgr_send_flow_removed(connmgr
, &fr
);
5746 ovs_mutex_unlock(&ofproto_mutex
);
5749 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
5750 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
5753 * ofproto implementation ->run() functions should use this function to expire
5754 * OpenFlow flows. */
5756 ofproto_rule_expire(struct rule
*rule
, uint8_t reason
)
5757 OVS_REQUIRES(ofproto_mutex
)
5759 struct rule_collection rules
;
5761 rule_collection_init(&rules
);
5762 rule_collection_add(&rules
, rule
);
5763 delete_flows__(&rules
, reason
, NULL
);
5766 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5767 * means "infinite". */
5769 reduce_timeout(uint16_t max
, uint16_t *timeout
)
5771 if (max
&& (!*timeout
|| *timeout
> max
)) {
5776 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
5777 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
5778 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
5780 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
5782 ofproto_rule_reduce_timeouts__(struct rule
*rule
,
5783 uint16_t idle_timeout
, uint16_t hard_timeout
)
5784 OVS_REQUIRES(ofproto_mutex
)
5785 OVS_EXCLUDED(rule
->mutex
)
5787 if (!idle_timeout
&& !hard_timeout
) {
5791 if (ovs_list_is_empty(&rule
->expirable
)) {
5792 ovs_list_insert(&rule
->ofproto
->expirable
, &rule
->expirable
);
5795 ovs_mutex_lock(&rule
->mutex
);
5796 reduce_timeout(idle_timeout
, &rule
->idle_timeout
);
5797 reduce_timeout(hard_timeout
, &rule
->hard_timeout
);
5798 ovs_mutex_unlock(&rule
->mutex
);
5802 ofproto_rule_reduce_timeouts(struct rule
*rule
,
5803 uint16_t idle_timeout
, uint16_t hard_timeout
)
5804 OVS_EXCLUDED(ofproto_mutex
, rule
->mutex
)
5806 if (!idle_timeout
&& !hard_timeout
) {
5810 ovs_mutex_lock(&ofproto_mutex
);
5811 if (ovs_list_is_empty(&rule
->expirable
)) {
5812 ovs_list_insert(&rule
->ofproto
->expirable
, &rule
->expirable
);
5814 ovs_mutex_unlock(&ofproto_mutex
);
5816 ovs_mutex_lock(&rule
->mutex
);
5817 reduce_timeout(idle_timeout
, &rule
->idle_timeout
);
5818 reduce_timeout(hard_timeout
, &rule
->hard_timeout
);
5819 ovs_mutex_unlock(&rule
->mutex
);
5823 handle_flow_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5824 OVS_EXCLUDED(ofproto_mutex
)
5826 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5827 struct ofputil_flow_mod fm
;
5828 uint64_t ofpacts_stub
[1024 / 8];
5829 struct ofpbuf ofpacts
;
5832 error
= reject_slave_controller(ofconn
);
5837 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
5838 error
= ofputil_decode_flow_mod(&fm
, oh
, ofconn_get_protocol(ofconn
),
5839 ofproto_get_tun_tab(ofproto
),
5840 &ofproto
->vl_mff_map
, &ofpacts
,
5841 u16_to_ofp(ofproto
->max_ports
),
5844 struct openflow_mod_requester req
= { ofconn
, oh
};
5845 error
= handle_flow_mod__(ofproto
, &fm
, &req
);
5846 minimatch_destroy(&fm
.match
);
5849 ofpbuf_uninit(&ofpacts
);
5854 handle_flow_mod__(struct ofproto
*ofproto
, const struct ofputil_flow_mod
*fm
,
5855 const struct openflow_mod_requester
*req
)
5856 OVS_EXCLUDED(ofproto_mutex
)
5858 struct ofproto_flow_mod ofm
;
5861 error
= ofproto_flow_mod_init(ofproto
, &ofm
, fm
, NULL
);
5866 ovs_mutex_lock(&ofproto_mutex
);
5867 ofm
.version
= ofproto
->tables_version
+ 1;
5868 error
= ofproto_flow_mod_start(ofproto
, &ofm
);
5870 ofproto_bump_tables_version(ofproto
);
5871 ofproto_flow_mod_finish(ofproto
, &ofm
, req
);
5872 ofmonitor_flush(ofproto
->connmgr
);
5874 ovs_mutex_unlock(&ofproto_mutex
);
5880 handle_role_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5882 struct ofputil_role_request request
;
5883 struct ofputil_role_request reply
;
5887 error
= ofputil_decode_role_message(oh
, &request
);
5892 if (request
.role
!= OFPCR12_ROLE_NOCHANGE
) {
5893 if (request
.role
!= OFPCR12_ROLE_EQUAL
5894 && request
.have_generation_id
5895 && !ofconn_set_master_election_id(ofconn
, request
.generation_id
)) {
5896 return OFPERR_OFPRRFC_STALE
;
5899 ofconn_set_role(ofconn
, request
.role
);
5902 reply
.role
= ofconn_get_role(ofconn
);
5903 reply
.have_generation_id
= ofconn_get_master_election_id(
5904 ofconn
, &reply
.generation_id
);
5905 buf
= ofputil_encode_role_reply(oh
, &reply
);
5906 ofconn_send_reply(ofconn
, buf
);
5912 handle_nxt_flow_mod_table_id(struct ofconn
*ofconn
,
5913 const struct ofp_header
*oh
)
5915 bool enable
= ofputil_decode_nx_flow_mod_table_id(oh
);
5916 enum ofputil_protocol cur
= ofconn_get_protocol(ofconn
);
5917 ofconn_set_protocol(ofconn
, ofputil_protocol_set_tid(cur
, enable
));
5923 handle_nxt_set_flow_format(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5925 enum ofputil_protocol next_base
= ofputil_decode_nx_set_flow_format(oh
);
5927 return OFPERR_OFPBRC_EPERM
;
5930 enum ofputil_protocol cur
= ofconn_get_protocol(ofconn
);
5931 ofconn_set_protocol(ofconn
, ofputil_protocol_set_base(cur
, next_base
));
5937 handle_nxt_set_packet_in_format(struct ofconn
*ofconn
,
5938 const struct ofp_header
*oh
)
5940 enum ofputil_packet_in_format format
;
5941 enum ofperr error
= ofputil_decode_set_packet_in_format(oh
, &format
);
5943 ofconn_set_packet_in_format(ofconn
, format
);
5949 handle_nxt_set_async_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5951 struct ofputil_async_cfg basis
= ofconn_get_async_config(ofconn
);
5952 struct ofputil_async_cfg ac
;
5955 error
= ofputil_decode_set_async_config(oh
, false, &basis
, &ac
);
5960 ofconn_set_async_config(ofconn
, &ac
);
5961 if (ofconn_get_type(ofconn
) == OFCONN_SERVICE
&&
5962 !ofconn_get_miss_send_len(ofconn
)) {
5963 ofconn_set_miss_send_len(ofconn
, OFP_DEFAULT_MISS_SEND_LEN
);
5970 handle_nxt_get_async_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5972 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
5973 ofconn_send_reply(ofconn
, ofputil_encode_get_async_reply(oh
, &ac
));
5979 handle_nxt_set_controller_id(struct ofconn
*ofconn
,
5980 const struct ofp_header
*oh
)
5982 const struct nx_controller_id
*nci
= ofpmsg_body(oh
);
5984 if (!is_all_zeros(nci
->zero
, sizeof nci
->zero
)) {
5985 return OFPERR_NXBRC_MUST_BE_ZERO
;
5988 ofconn_set_controller_id(ofconn
, ntohs(nci
->controller_id
));
5993 handle_barrier_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5997 buf
= ofpraw_alloc_reply((oh
->version
== OFP10_VERSION
5998 ? OFPRAW_OFPT10_BARRIER_REPLY
5999 : OFPRAW_OFPT11_BARRIER_REPLY
), oh
, 0);
6000 ofconn_send_reply(ofconn
, buf
);
6005 ofproto_compose_flow_refresh_update(const struct rule
*rule
,
6006 enum nx_flow_monitor_flags flags
,
6007 struct ovs_list
*msgs
,
6008 const struct tun_table
*tun_table
)
6009 OVS_REQUIRES(ofproto_mutex
)
6011 const struct rule_actions
*actions
;
6012 struct ofputil_flow_update fu
;
6014 fu
.event
= (flags
& (NXFMF_INITIAL
| NXFMF_ADD
)
6015 ? NXFME_ADDED
: NXFME_MODIFIED
);
6017 ovs_mutex_lock(&rule
->mutex
);
6018 fu
.idle_timeout
= rule
->idle_timeout
;
6019 fu
.hard_timeout
= rule
->hard_timeout
;
6020 ovs_mutex_unlock(&rule
->mutex
);
6021 fu
.table_id
= rule
->table_id
;
6022 fu
.cookie
= rule
->flow_cookie
;
6023 minimatch_expand(&rule
->cr
.match
, &fu
.match
);
6024 fu
.priority
= rule
->cr
.priority
;
6026 actions
= flags
& NXFMF_ACTIONS
? rule_get_actions(rule
) : NULL
;
6027 fu
.ofpacts
= actions
? actions
->ofpacts
: NULL
;
6028 fu
.ofpacts_len
= actions
? actions
->ofpacts_len
: 0;
6030 if (ovs_list_is_empty(msgs
)) {
6031 ofputil_start_flow_update(msgs
);
6033 ofputil_append_flow_update(&fu
, msgs
, tun_table
);
6037 ofmonitor_compose_refresh_updates(struct rule_collection
*rules
,
6038 struct ovs_list
*msgs
)
6039 OVS_REQUIRES(ofproto_mutex
)
6043 RULE_COLLECTION_FOR_EACH (rule
, rules
) {
6044 enum nx_flow_monitor_flags flags
= rule
->monitor_flags
;
6045 rule
->monitor_flags
= 0;
6047 ofproto_compose_flow_refresh_update(rule
, flags
, msgs
,
6048 ofproto_get_tun_tab(rule
->ofproto
));
6053 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor
*m
,
6054 struct rule
*rule
, uint64_t seqno
,
6055 struct rule_collection
*rules
)
6056 OVS_REQUIRES(ofproto_mutex
)
6058 enum nx_flow_monitor_flags update
;
6060 if (rule_is_hidden(rule
)) {
6064 if (!ofproto_rule_has_out_port(rule
, m
->out_port
)) {
6069 if (rule
->add_seqno
> seqno
) {
6070 update
= NXFMF_ADD
| NXFMF_MODIFY
;
6071 } else if (rule
->modify_seqno
> seqno
) {
6072 update
= NXFMF_MODIFY
;
6077 if (!(m
->flags
& update
)) {
6081 update
= NXFMF_INITIAL
;
6084 if (!rule
->monitor_flags
) {
6085 rule_collection_add(rules
, rule
);
6087 rule
->monitor_flags
|= update
| (m
->flags
& NXFMF_ACTIONS
);
6091 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor
*m
,
6093 struct rule_collection
*rules
)
6094 OVS_REQUIRES(ofproto_mutex
)
6096 const struct ofproto
*ofproto
= ofconn_get_ofproto(m
->ofconn
);
6097 const struct oftable
*table
;
6098 struct cls_rule target
;
6100 cls_rule_init_from_minimatch(&target
, &m
->match
, 0);
6101 FOR_EACH_MATCHING_TABLE (table
, m
->table_id
, ofproto
) {
6104 CLS_FOR_EACH_TARGET (rule
, cr
, &table
->cls
, &target
, OVS_VERSION_MAX
) {
6105 ofproto_collect_ofmonitor_refresh_rule(m
, rule
, seqno
, rules
);
6108 cls_rule_destroy(&target
);
6112 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor
*m
,
6113 struct rule_collection
*rules
)
6114 OVS_REQUIRES(ofproto_mutex
)
6116 if (m
->flags
& NXFMF_INITIAL
) {
6117 ofproto_collect_ofmonitor_refresh_rules(m
, 0, rules
);
6122 ofmonitor_collect_resume_rules(struct ofmonitor
*m
,
6123 uint64_t seqno
, struct rule_collection
*rules
)
6124 OVS_REQUIRES(ofproto_mutex
)
6126 ofproto_collect_ofmonitor_refresh_rules(m
, seqno
, rules
);
6130 flow_monitor_delete(struct ofconn
*ofconn
, uint32_t id
)
6131 OVS_REQUIRES(ofproto_mutex
)
6133 struct ofmonitor
*m
;
6136 m
= ofmonitor_lookup(ofconn
, id
);
6138 ofmonitor_destroy(m
);
6141 error
= OFPERR_OFPMOFC_UNKNOWN_MONITOR
;
6148 handle_flow_monitor_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6149 OVS_EXCLUDED(ofproto_mutex
)
6151 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6153 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6155 struct ofmonitor
**monitors
= NULL
;
6156 size_t allocated_monitors
= 0;
6157 size_t n_monitors
= 0;
6161 ovs_mutex_lock(&ofproto_mutex
);
6163 struct ofputil_flow_monitor_request request
;
6164 struct ofmonitor
*m
;
6167 retval
= ofputil_decode_flow_monitor_request(&request
, &b
);
6168 if (retval
== EOF
) {
6170 } else if (retval
) {
6175 if (request
.table_id
!= 0xff
6176 && request
.table_id
>= ofproto
->n_tables
) {
6177 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
6181 error
= ofmonitor_create(&request
, ofconn
, &m
);
6186 if (n_monitors
>= allocated_monitors
) {
6187 monitors
= x2nrealloc(monitors
, &allocated_monitors
,
6190 monitors
[n_monitors
++] = m
;
6193 struct rule_collection rules
;
6194 rule_collection_init(&rules
);
6195 for (size_t i
= 0; i
< n_monitors
; i
++) {
6196 ofproto_collect_ofmonitor_initial_rules(monitors
[i
], &rules
);
6199 struct ovs_list replies
;
6200 ofpmp_init(&replies
, oh
);
6201 ofmonitor_compose_refresh_updates(&rules
, &replies
);
6202 ovs_mutex_unlock(&ofproto_mutex
);
6204 rule_collection_destroy(&rules
);
6206 ofconn_send_replies(ofconn
, &replies
);
6212 for (size_t i
= 0; i
< n_monitors
; i
++) {
6213 ofmonitor_destroy(monitors
[i
]);
6216 ovs_mutex_unlock(&ofproto_mutex
);
6222 handle_flow_monitor_cancel(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6223 OVS_EXCLUDED(ofproto_mutex
)
6228 id
= ofputil_decode_flow_monitor_cancel(oh
);
6230 ovs_mutex_lock(&ofproto_mutex
);
6231 error
= flow_monitor_delete(ofconn
, id
);
6232 ovs_mutex_unlock(&ofproto_mutex
);
6237 /* Meters implementation.
6239 * Meter table entry, indexed by the OpenFlow meter_id.
6240 * 'created' is used to compute the duration for meter stats.
6241 * 'list rules' is needed so that we can delete the dependent rules when the
6242 * meter table entry is deleted.
6243 * 'provider_meter_id' is for the provider's private use.
6246 struct hmap_node node
; /* In ofproto->meters. */
6247 long long int created
; /* Time created. */
6248 struct ovs_list rules
; /* List of "struct rule_dpif"s. */
6249 uint32_t id
; /* OpenFlow meter_id. */
6250 ofproto_meter_id provider_meter_id
;
6251 uint16_t flags
; /* Meter flags. */
6252 uint16_t n_bands
; /* Number of meter bands. */
6253 struct ofputil_meter_band
*bands
;
6256 static struct meter
*
6257 ofproto_get_meter(const struct ofproto
*ofproto
, uint32_t meter_id
)
6259 struct meter
*meter
;
6260 uint32_t hash
= hash_int(meter_id
, 0);
6262 HMAP_FOR_EACH_WITH_HASH (meter
, node
, hash
, &ofproto
->meters
) {
6263 if (meter
->id
== meter_id
) {
6272 ofproto_upcall_meter_ptr(struct ofproto
*ofproto
, uint32_t meter_id
)
6275 case OFPM13_SLOWPATH
:
6276 return &ofproto
->slowpath_meter_id
;
6278 case OFPM13_CONTROLLER
:
6279 return &ofproto
->controller_meter_id
;
6289 ofproto_add_meter(struct ofproto
*ofproto
, struct meter
*meter
)
6291 uint32_t *upcall_meter_ptr
= ofproto_upcall_meter_ptr(ofproto
, meter
->id
);
6293 /* Cache datapath meter IDs of special meters. */
6294 if (upcall_meter_ptr
) {
6295 *upcall_meter_ptr
= meter
->provider_meter_id
.uint32
;
6298 hmap_insert(&ofproto
->meters
, &meter
->node
, hash_int(meter
->id
, 0));
6302 * This is used in instruction validation at flow set-up time, to map
6303 * the OpenFlow meter ID to the corresponding datapath provider meter
6304 * ID. If either does not exist, returns false. Otherwise updates
6305 * the meter action and returns true.
6308 ofproto_fix_meter_action(const struct ofproto
*ofproto
,
6309 struct ofpact_meter
*ma
)
6312 const struct meter
*meter
= ofproto_get_meter(ofproto
, ma
->meter_id
);
6314 if (meter
&& meter
->provider_meter_id
.uint32
!= UINT32_MAX
) {
6315 /* Update the action with the provider's meter ID, so that we
6316 * do not need any synchronization between ofproto_dpif_xlate
6317 * and ofproto for meter table access. */
6318 ma
->provider_meter_id
= meter
->provider_meter_id
.uint32
;
6325 /* This is used in instruction validation at flow set-up time, to map
6326 * the OpenFlow meter ID in a controller action to the corresponding
6327 * datapath provider meter ID. If either does not exist, sets the
6328 * provider meter id to a value to prevent the provider from using it
6329 * and returns false. Otherwise, updates the meter action and returns
6332 ofproto_fix_controller_action(const struct ofproto
*ofproto
,
6333 struct ofpact_controller
*ca
)
6335 if (ca
->meter_id
== NX_CTLR_NO_METER
) {
6336 ca
->provider_meter_id
= UINT32_MAX
;
6340 const struct meter
*meter
= ofproto_get_meter(ofproto
, ca
->meter_id
);
6342 if (meter
&& meter
->provider_meter_id
.uint32
!= UINT32_MAX
) {
6343 /* Update the action with the provider's meter ID, so that we
6344 * do not need any synchronization between ofproto_dpif_xlate
6345 * and ofproto for meter table access. */
6346 ca
->provider_meter_id
= meter
->provider_meter_id
.uint32
;
6350 /* Prevent the meter from being set by the ofproto provider. */
6351 ca
->provider_meter_id
= UINT32_MAX
;
6355 /* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
6358 meter_insert_rule(struct rule
*rule
)
6360 const struct rule_actions
*a
= rule_get_actions(rule
);
6361 uint32_t meter_id
= ofpacts_get_meter(a
->ofpacts
, a
->ofpacts_len
);
6362 struct meter
*meter
= ofproto_get_meter(rule
->ofproto
, meter_id
);
6364 ovs_list_insert(&meter
->rules
, &rule
->meter_list_node
);
6368 meter_update(struct meter
*meter
, const struct ofputil_meter_config
*config
)
6372 meter
->flags
= config
->flags
;
6373 meter
->n_bands
= config
->n_bands
;
6374 meter
->bands
= xmemdup(config
->bands
,
6375 config
->n_bands
* sizeof *meter
->bands
);
6378 static struct meter
*
6379 meter_create(const struct ofputil_meter_config
*config
,
6380 ofproto_meter_id provider_meter_id
)
6382 struct meter
*meter
;
6384 meter
= xzalloc(sizeof *meter
);
6385 meter
->provider_meter_id
= provider_meter_id
;
6386 meter
->created
= time_msec();
6387 meter
->id
= config
->meter_id
;
6388 ovs_list_init(&meter
->rules
);
6390 meter_update(meter
, config
);
6396 meter_destroy(struct ofproto
*ofproto
, struct meter
*meter
)
6397 OVS_REQUIRES(ofproto_mutex
)
6399 uint32_t *upcall_meter_ptr
;
6400 upcall_meter_ptr
= ofproto_upcall_meter_ptr(ofproto
, meter
->id
);
6401 if (upcall_meter_ptr
) {
6402 *upcall_meter_ptr
= UINT32_MAX
;
6405 if (!ovs_list_is_empty(&meter
->rules
)) {
6406 struct rule_collection rules
;
6409 rule_collection_init(&rules
);
6410 LIST_FOR_EACH (rule
, meter_list_node
, &meter
->rules
) {
6411 rule_collection_add(&rules
, rule
);
6413 delete_flows__(&rules
, OFPRR_METER_DELETE
, NULL
);
6416 ofproto
->ofproto_class
->meter_del(ofproto
, meter
->provider_meter_id
);
6422 meter_delete(struct ofproto
*ofproto
, uint32_t meter_id
)
6423 OVS_REQUIRES(ofproto_mutex
)
6425 struct meter
*meter
= ofproto_get_meter(ofproto
, meter_id
);
6428 hmap_remove(&ofproto
->meters
, &meter
->node
);
6429 meter_destroy(ofproto
, meter
);
6434 meter_delete_all(struct ofproto
*ofproto
)
6435 OVS_REQUIRES(ofproto_mutex
)
6437 struct meter
*meter
, *next
;
6439 HMAP_FOR_EACH_SAFE (meter
, next
, node
, &ofproto
->meters
) {
6440 hmap_remove(&ofproto
->meters
, &meter
->node
);
6441 meter_destroy(ofproto
, meter
);
6446 handle_add_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
6448 ofproto_meter_id provider_meter_id
= { UINT32_MAX
};
6449 struct meter
*meter
= ofproto_get_meter(ofproto
, mm
->meter
.meter_id
);
6453 return OFPERR_OFPMMFC_METER_EXISTS
;
6456 error
= ofproto
->ofproto_class
->meter_set(ofproto
, &provider_meter_id
,
6459 ovs_assert(provider_meter_id
.uint32
!= UINT32_MAX
);
6460 meter
= meter_create(&mm
->meter
, provider_meter_id
);
6461 ofproto_add_meter(ofproto
, meter
);
6467 handle_modify_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
6469 struct meter
*meter
= ofproto_get_meter(ofproto
, mm
->meter
.meter_id
);
6471 uint32_t provider_meter_id
;
6474 return OFPERR_OFPMMFC_UNKNOWN_METER
;
6477 provider_meter_id
= meter
->provider_meter_id
.uint32
;
6478 error
= ofproto
->ofproto_class
->meter_set(ofproto
,
6479 &meter
->provider_meter_id
,
6481 ovs_assert(meter
->provider_meter_id
.uint32
== provider_meter_id
);
6483 meter_update(meter
, &mm
->meter
);
6489 handle_delete_meter(struct ofconn
*ofconn
, struct ofputil_meter_mod
*mm
)
6490 OVS_EXCLUDED(ofproto_mutex
)
6492 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6493 uint32_t meter_id
= mm
->meter
.meter_id
;
6495 /* OpenFlow does not support Meter ID 0. */
6497 ovs_mutex_lock(&ofproto_mutex
);
6499 if (meter_id
== OFPM13_ALL
) {
6500 meter_delete_all(ofproto
);
6502 meter_delete(ofproto
, meter_id
);
6505 ovs_mutex_unlock(&ofproto_mutex
);
6512 handle_meter_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
6514 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6515 struct ofputil_meter_mod mm
;
6516 uint64_t bands_stub
[256 / 8];
6517 struct ofpbuf bands
;
6521 error
= reject_slave_controller(ofconn
);
6526 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
6528 error
= ofputil_decode_meter_mod(oh
, &mm
, &bands
);
6530 goto exit_free_bands
;
6533 meter_id
= mm
.meter
.meter_id
;
6535 if (mm
.command
!= OFPMC13_DELETE
) {
6536 /* Fails also when meters are not implemented by the provider. */
6537 if (ofproto
->meter_features
.max_meters
== 0) {
6538 error
= OFPERR_OFPMMFC_INVALID_METER
;
6539 goto exit_free_bands
;
6542 if (meter_id
== 0) {
6543 error
= OFPERR_OFPMMFC_INVALID_METER
;
6544 goto exit_free_bands
;
6545 } else if (meter_id
> OFPM13_MAX
) {
6547 case OFPM13_SLOWPATH
:
6548 case OFPM13_CONTROLLER
:
6552 error
= OFPERR_OFPMMFC_INVALID_METER
;
6553 goto exit_free_bands
;
6556 if (mm
.meter
.n_bands
> ofproto
->meter_features
.max_bands
) {
6557 error
= OFPERR_OFPMMFC_OUT_OF_BANDS
;
6558 goto exit_free_bands
;
6562 switch (mm
.command
) {
6564 error
= handle_add_meter(ofproto
, &mm
);
6567 case OFPMC13_MODIFY
:
6568 error
= handle_modify_meter(ofproto
, &mm
);
6571 case OFPMC13_DELETE
:
6572 error
= handle_delete_meter(ofconn
, &mm
);
6576 error
= OFPERR_OFPMMFC_BAD_COMMAND
;
6581 struct ofputil_requestforward rf
;
6583 rf
.reason
= OFPRFR_METER_MOD
;
6585 connmgr_send_requestforward(ofproto
->connmgr
, ofconn
, &rf
);
6589 ofpbuf_uninit(&bands
);
6594 handle_meter_features_request(struct ofconn
*ofconn
,
6595 const struct ofp_header
*request
)
6597 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6598 struct ofputil_meter_features features
;
6601 if (ofproto
->ofproto_class
->meter_get_features
) {
6602 ofproto
->ofproto_class
->meter_get_features(ofproto
, &features
);
6604 memset(&features
, 0, sizeof features
);
6606 b
= ofputil_encode_meter_features_reply(&features
, request
);
6608 ofconn_send_reply(ofconn
, b
);
6613 meter_request_reply(struct ofproto
*ofproto
, struct meter
*meter
,
6614 enum ofptype type
, struct ovs_list
*replies
)
6616 uint64_t bands_stub
[256 / 8];
6617 struct ofpbuf bands
;
6619 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
6621 if (type
== OFPTYPE_METER_STATS_REQUEST
) {
6622 struct ofputil_meter_stats stats
;
6624 stats
.meter_id
= meter
->id
;
6626 /* Provider sets the packet and byte counts, we do the rest. */
6627 stats
.flow_count
= ovs_list_size(&meter
->rules
);
6628 calc_duration(meter
->created
, time_msec(),
6629 &stats
.duration_sec
, &stats
.duration_nsec
);
6630 stats
.n_bands
= meter
->n_bands
;
6631 ofpbuf_clear(&bands
);
6632 stats
.bands
= ofpbuf_put_uninit(&bands
, meter
->n_bands
6633 * sizeof *stats
.bands
);
6635 if (!ofproto
->ofproto_class
->meter_get(ofproto
,
6636 meter
->provider_meter_id
,
6637 &stats
, meter
->n_bands
)) {
6638 ofputil_append_meter_stats(replies
, &stats
);
6640 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
6641 struct ofputil_meter_config config
;
6643 config
.meter_id
= meter
->id
;
6644 config
.flags
= meter
->flags
;
6645 config
.n_bands
= meter
->n_bands
;
6646 config
.bands
= meter
->bands
;
6647 ofputil_append_meter_config(replies
, &config
);
6650 ofpbuf_uninit(&bands
);
6654 meter_request_reply_all(struct ofproto
*ofproto
, enum ofptype type
,
6655 struct ovs_list
*replies
)
6657 struct meter
*meter
;
6659 HMAP_FOR_EACH (meter
, node
, &ofproto
->meters
) {
6660 meter_request_reply(ofproto
, meter
, type
, replies
);
6665 handle_meter_request(struct ofconn
*ofconn
, const struct ofp_header
*request
,
6668 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6669 struct ovs_list replies
;
6671 struct meter
*meter
;
6673 ofputil_decode_meter_request(request
, &meter_id
);
6675 if (meter_id
!= OFPM13_ALL
) {
6676 meter
= ofproto_get_meter(ofproto
, meter_id
);
6678 /* Meter does not exist. */
6679 return OFPERR_OFPMMFC_UNKNOWN_METER
;
6682 /* GCC 4.9.2 complains about 'meter' can potentially be used
6683 * uninitialized. Logically, this is a false alarm since
6684 * meter is only used when meter_id != OFPM13_ALL.
6685 * Set NULL to make compiler happy. */
6689 ofpmp_init(&replies
, request
);
6690 if (meter_id
== OFPM13_ALL
) {
6691 meter_request_reply_all(ofproto
, type
, &replies
);
6693 meter_request_reply(ofproto
, meter
, type
, &replies
);
6695 ofconn_send_replies(ofconn
, &replies
);
6699 /* Returned group is RCU protected. */
6700 static struct ofgroup
*
6701 ofproto_group_lookup__(const struct ofproto
*ofproto
, uint32_t group_id
,
6702 ovs_version_t version
)
6704 struct ofgroup
*group
;
6706 CMAP_FOR_EACH_WITH_HASH (group
, cmap_node
, hash_int(group_id
, 0),
6708 if (group
->group_id
== group_id
6709 && versions_visible_in_version(&group
->versions
, version
)) {
6717 /* If the group exists, this function increments the groups's reference count.
6719 * Make sure to call ofproto_group_unref() after no longer needing to maintain
6720 * a reference to the group. */
6722 ofproto_group_lookup(const struct ofproto
*ofproto
, uint32_t group_id
,
6723 ovs_version_t version
, bool take_ref
)
6725 struct ofgroup
*group
;
6727 group
= ofproto_group_lookup__(ofproto
, group_id
, version
);
6728 if (group
&& take_ref
) {
6729 /* Not holding a lock, so it is possible that another thread releases
6730 * the last reference just before we manage to get one. */
6731 return ofproto_group_try_ref(group
) ? group
: NULL
;
6736 /* Caller should hold 'ofproto_mutex' if it is important that the
6737 * group is not removed by someone else. */
6739 ofproto_group_exists(const struct ofproto
*ofproto
, uint32_t group_id
)
6741 return ofproto_group_lookup__(ofproto
, group_id
, OVS_VERSION_MAX
) != NULL
;
6745 group_add_rule(struct ofgroup
*group
, struct rule
*rule
)
6747 rule_collection_add(&group
->rules
, rule
);
6751 group_remove_rule(struct ofgroup
*group
, struct rule
*rule
)
6753 rule_collection_remove(&group
->rules
, rule
);
6757 append_group_stats(struct ofgroup
*group
, struct ovs_list
*replies
)
6758 OVS_REQUIRES(ofproto_mutex
)
6760 struct ofputil_group_stats ogs
;
6761 const struct ofproto
*ofproto
= group
->ofproto
;
6762 long long int now
= time_msec();
6765 ogs
.bucket_stats
= xmalloc(group
->n_buckets
* sizeof *ogs
.bucket_stats
);
6767 /* Provider sets the packet and byte counts, we do the rest. */
6768 ogs
.ref_count
= rule_collection_n(&group
->rules
);
6769 ogs
.n_buckets
= group
->n_buckets
;
6771 error
= (ofproto
->ofproto_class
->group_get_stats
6772 ? ofproto
->ofproto_class
->group_get_stats(group
, &ogs
)
6775 ogs
.packet_count
= UINT64_MAX
;
6776 ogs
.byte_count
= UINT64_MAX
;
6777 memset(ogs
.bucket_stats
, 0xff,
6778 ogs
.n_buckets
* sizeof *ogs
.bucket_stats
);
6781 ogs
.group_id
= group
->group_id
;
6782 calc_duration(group
->created
, now
, &ogs
.duration_sec
, &ogs
.duration_nsec
);
6784 ofputil_append_group_stats(replies
, &ogs
);
6786 free(ogs
.bucket_stats
);
6790 handle_group_request(struct ofconn
*ofconn
,
6791 const struct ofp_header
*request
, uint32_t group_id
,
6792 void (*cb
)(struct ofgroup
*, struct ovs_list
*replies
))
6793 OVS_EXCLUDED(ofproto_mutex
)
6795 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6796 struct ofgroup
*group
;
6797 struct ovs_list replies
;
6799 ofpmp_init(&replies
, request
);
6800 /* Must exclude modifications to guarantee iterating groups. */
6801 ovs_mutex_lock(&ofproto_mutex
);
6802 if (group_id
== OFPG_ALL
) {
6803 CMAP_FOR_EACH (group
, cmap_node
, &ofproto
->groups
) {
6804 if (versions_visible_in_version(&group
->versions
,
6806 cb(group
, &replies
);
6810 group
= ofproto_group_lookup__(ofproto
, group_id
, OVS_VERSION_MAX
);
6812 cb(group
, &replies
);
6815 ovs_mutex_unlock(&ofproto_mutex
);
6816 ofconn_send_replies(ofconn
, &replies
);
6820 handle_group_stats_request(struct ofconn
*ofconn
,
6821 const struct ofp_header
*request
)
6826 error
= ofputil_decode_group_stats_request(request
, &group_id
);
6831 handle_group_request(ofconn
, request
, group_id
, append_group_stats
);
6836 append_group_desc(struct ofgroup
*group
, struct ovs_list
*replies
)
6838 struct ofputil_group_desc gds
;
6840 gds
.group_id
= group
->group_id
;
6841 gds
.type
= group
->type
;
6842 gds
.props
= group
->props
;
6844 ofputil_append_group_desc_reply(&gds
, &group
->buckets
, replies
);
6848 handle_group_desc_stats_request(struct ofconn
*ofconn
,
6849 const struct ofp_header
*request
)
6851 handle_group_request(ofconn
, request
,
6852 ofputil_decode_group_desc_request(request
),
6858 handle_group_features_stats_request(struct ofconn
*ofconn
,
6859 const struct ofp_header
*request
)
6861 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
6864 msg
= ofputil_encode_group_features_reply(&p
->ogf
, request
);
6866 ofconn_send_reply(ofconn
, msg
);
6873 put_queue_get_config_reply(struct ofport
*port
, uint32_t queue
,
6874 struct ovs_list
*replies
)
6876 struct ofputil_queue_config qc
;
6878 /* None of the existing queues have compatible properties, so we hard-code
6879 * omitting min_rate and max_rate. */
6880 qc
.port
= port
->ofp_port
;
6882 qc
.min_rate
= UINT16_MAX
;
6883 qc
.max_rate
= UINT16_MAX
;
6884 ofputil_append_queue_get_config_reply(&qc
, replies
);
6888 handle_queue_get_config_request_for_port(struct ofport
*port
, uint32_t queue
,
6889 struct ovs_list
*replies
)
6891 struct smap details
= SMAP_INITIALIZER(&details
);
6892 if (queue
!= OFPQ_ALL
) {
6893 int error
= netdev_get_queue(port
->netdev
, queue
, &details
);
6896 put_queue_get_config_reply(port
, queue
, replies
);
6900 return OFPERR_OFPQOFC_BAD_QUEUE
;
6902 return OFPERR_NXQOFC_QUEUE_ERROR
;
6905 struct netdev_queue_dump queue_dump
;
6908 NETDEV_QUEUE_FOR_EACH (&queue_id
, &details
, &queue_dump
,
6910 put_queue_get_config_reply(port
, queue_id
, replies
);
6913 smap_destroy(&details
);
6918 handle_queue_get_config_request(struct ofconn
*ofconn
,
6919 const struct ofp_header
*oh
)
6921 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
6922 struct ovs_list replies
;
6923 struct ofport
*port
;
6924 ofp_port_t req_port
;
6928 error
= ofputil_decode_queue_get_config_request(oh
, &req_port
, &req_queue
);
6933 ofputil_start_queue_get_config_reply(oh
, &replies
);
6934 if (req_port
== OFPP_ANY
) {
6935 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
6936 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
6937 if (!handle_queue_get_config_request_for_port(port
, req_queue
,
6943 port
= ofproto_get_port(ofproto
, req_port
);
6945 ? handle_queue_get_config_request_for_port(port
, req_queue
,
6947 : OFPERR_OFPQOFC_BAD_PORT
);
6950 ofconn_send_replies(ofconn
, &replies
);
6952 ofpbuf_list_delete(&replies
);
6958 /* Allocates, initializes, and constructs a new group in 'ofproto', obtaining
6959 * all the attributes for it from 'gm', and stores a pointer to it in
6960 * '*ofgroup'. Makes the new group visible from the flow table starting from
6963 * Returns 0 if successful, otherwise an error code. If there is an error then
6964 * '*ofgroup' is indeterminate upon return. */
6966 init_group(struct ofproto
*ofproto
, const struct ofputil_group_mod
*gm
,
6967 ovs_version_t version
, struct ofgroup
**ofgroup
)
6970 const long long int now
= time_msec();
6972 if (gm
->group_id
> OFPG_MAX
) {
6973 return OFPERR_OFPGMFC_INVALID_GROUP
;
6975 if (gm
->type
> OFPGT11_FF
) {
6976 return OFPERR_OFPGMFC_BAD_TYPE
;
6979 *ofgroup
= ofproto
->ofproto_class
->group_alloc();
6981 VLOG_WARN_RL(&rl
, "%s: failed to allocate group", ofproto
->name
);
6982 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
6985 *CONST_CAST(struct ofproto
**, &(*ofgroup
)->ofproto
) = ofproto
;
6986 *CONST_CAST(uint32_t *, &((*ofgroup
)->group_id
)) = gm
->group_id
;
6987 *CONST_CAST(enum ofp11_group_type
*, &(*ofgroup
)->type
) = gm
->type
;
6988 *CONST_CAST(long long int *, &((*ofgroup
)->created
)) = now
;
6989 *CONST_CAST(long long int *, &((*ofgroup
)->modified
)) = now
;
6990 ovs_refcount_init(&(*ofgroup
)->ref_count
);
6991 (*ofgroup
)->being_deleted
= false;
6993 ovs_list_init(CONST_CAST(struct ovs_list
*, &(*ofgroup
)->buckets
));
6994 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
6995 &(*ofgroup
)->buckets
),
6996 &gm
->buckets
, NULL
);
6998 *CONST_CAST(uint32_t *, &(*ofgroup
)->n_buckets
) =
6999 ovs_list_size(&(*ofgroup
)->buckets
);
7001 ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props
*,
7002 &(*ofgroup
)->props
),
7004 rule_collection_init(&(*ofgroup
)->rules
);
7006 /* Make group visible from 'version'. */
7007 (*ofgroup
)->versions
= VERSIONS_INITIALIZER(version
,
7008 OVS_VERSION_NOT_REMOVED
);
7010 /* Construct called BEFORE any locks are held. */
7011 error
= ofproto
->ofproto_class
->group_construct(*ofgroup
);
7013 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props
*,
7014 &(*ofgroup
)->props
));
7015 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list
*,
7016 &(*ofgroup
)->buckets
));
7017 ofproto
->ofproto_class
->group_dealloc(*ofgroup
);
7022 /* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
7023 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
7026 add_group_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
7027 OVS_REQUIRES(ofproto_mutex
)
7031 if (ofproto_group_exists(ofproto
, ogm
->gm
.group_id
)) {
7032 return OFPERR_OFPGMFC_GROUP_EXISTS
;
7035 if (ofproto
->n_groups
[ogm
->gm
.type
]
7036 >= ofproto
->ogf
.max_groups
[ogm
->gm
.type
]) {
7037 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
7040 /* Allocate new group and initialize it. */
7041 error
= init_group(ofproto
, &ogm
->gm
, ogm
->version
, &ogm
->new_group
);
7043 /* Insert new group. */
7044 cmap_insert(&ofproto
->groups
, &ogm
->new_group
->cmap_node
,
7045 hash_int(ogm
->new_group
->group_id
, 0));
7046 ofproto
->n_groups
[ogm
->new_group
->type
]++;
7051 /* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets
7052 * already in 'new_ofgroup' will be placed just after the (copy of the) bucket
7053 * in 'ofgroup' with bucket ID 'command_bucket_id'. Special
7054 * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are
7057 copy_buckets_for_insert_bucket(const struct ofgroup
*ofgroup
,
7058 struct ofgroup
*new_ofgroup
,
7059 uint32_t command_bucket_id
)
7061 struct ofputil_bucket
*last
= NULL
;
7063 if (command_bucket_id
<= OFPG15_BUCKET_MAX
) {
7064 /* Check here to ensure that a bucket corresponding to
7065 * command_bucket_id exists in the old bucket list.
7067 * The subsequent search of below of new_ofgroup covers
7068 * both buckets in the old bucket list and buckets added
7069 * by the insert buckets group mod message this function processes. */
7070 if (!ofputil_bucket_find(&ofgroup
->buckets
, command_bucket_id
)) {
7071 return OFPERR_OFPGMFC_UNKNOWN_BUCKET
;
7074 if (!ovs_list_is_empty(&new_ofgroup
->buckets
)) {
7075 last
= ofputil_bucket_list_back(&new_ofgroup
->buckets
);
7079 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
7080 &new_ofgroup
->buckets
),
7081 &ofgroup
->buckets
, NULL
);
7083 if (ofputil_bucket_check_duplicate_id(&new_ofgroup
->buckets
)) {
7084 VLOG_INFO_RL(&rl
, "Duplicate bucket id");
7085 return OFPERR_OFPGMFC_BUCKET_EXISTS
;
7088 /* Rearrange list according to command_bucket_id */
7089 if (command_bucket_id
== OFPG15_BUCKET_LAST
) {
7090 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
7091 struct ofputil_bucket
*new_first
;
7092 const struct ofputil_bucket
*first
;
7094 first
= ofputil_bucket_list_front(&ofgroup
->buckets
);
7095 new_first
= ofputil_bucket_find(&new_ofgroup
->buckets
,
7098 ovs_list_splice(new_ofgroup
->buckets
.next
, &new_first
->list_node
,
7099 CONST_CAST(struct ovs_list
*,
7100 &new_ofgroup
->buckets
));
7102 } else if (command_bucket_id
<= OFPG15_BUCKET_MAX
&& last
) {
7103 struct ofputil_bucket
*after
;
7105 /* Presence of bucket is checked above so after should never be NULL */
7106 after
= ofputil_bucket_find(&new_ofgroup
->buckets
, command_bucket_id
);
7108 ovs_list_splice(after
->list_node
.next
, new_ofgroup
->buckets
.next
,
7109 last
->list_node
.next
);
7115 /* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup'
7116 * with the exception of the bucket whose bucket id is 'command_bucket_id'.
7117 * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST
7118 * and OFPG15_BUCKET_ALL are also honored. */
7120 copy_buckets_for_remove_bucket(const struct ofgroup
*ofgroup
,
7121 struct ofgroup
*new_ofgroup
,
7122 uint32_t command_bucket_id
)
7124 const struct ofputil_bucket
*skip
= NULL
;
7126 if (command_bucket_id
== OFPG15_BUCKET_ALL
) {
7130 if (command_bucket_id
== OFPG15_BUCKET_FIRST
) {
7131 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
7132 skip
= ofputil_bucket_list_front(&ofgroup
->buckets
);
7134 } else if (command_bucket_id
== OFPG15_BUCKET_LAST
) {
7135 if (!ovs_list_is_empty(&ofgroup
->buckets
)) {
7136 skip
= ofputil_bucket_list_back(&ofgroup
->buckets
);
7139 skip
= ofputil_bucket_find(&ofgroup
->buckets
, command_bucket_id
);
7141 return OFPERR_OFPGMFC_UNKNOWN_BUCKET
;
7145 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list
*,
7146 &new_ofgroup
->buckets
),
7147 &ofgroup
->buckets
, skip
);
7152 /* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and
7153 * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code
7156 * Note that the group is re-created and then replaces the old group in
7157 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
7158 * the xlate module hold a pointer to the group. */
7160 modify_group_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
7161 OVS_REQUIRES(ofproto_mutex
)
7163 struct ofgroup
*old_group
; /* Modified group. */
7164 struct ofgroup
*new_group
;
7167 old_group
= ofproto_group_lookup__(ofproto
, ogm
->gm
.group_id
,
7170 return OFPERR_OFPGMFC_UNKNOWN_GROUP
;
7173 /* Inserting or deleting a bucket should not change the group's type or
7174 * properties, so change the group mod so that these aspects match the old
7175 * group. (See EXT-570.) */
7176 if (ogm
->gm
.command
== OFPGC15_INSERT_BUCKET
||
7177 ogm
->gm
.command
== OFPGC15_REMOVE_BUCKET
) {
7178 ogm
->gm
.type
= old_group
->type
;
7179 ofputil_group_properties_destroy(&ogm
->gm
.props
);
7180 ofputil_group_properties_copy(&ogm
->gm
.props
, &old_group
->props
);
7183 if (old_group
->type
!= ogm
->gm
.type
7184 && (ofproto
->n_groups
[ogm
->gm
.type
]
7185 >= ofproto
->ogf
.max_groups
[ogm
->gm
.type
])) {
7186 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
7189 error
= init_group(ofproto
, &ogm
->gm
, ogm
->version
, &ogm
->new_group
);
7193 new_group
= ogm
->new_group
;
7195 /* Manipulate bucket list for bucket commands */
7196 if (ogm
->gm
.command
== OFPGC15_INSERT_BUCKET
) {
7197 error
= copy_buckets_for_insert_bucket(old_group
, new_group
,
7198 ogm
->gm
.command_bucket_id
);
7199 } else if (ogm
->gm
.command
== OFPGC15_REMOVE_BUCKET
) {
7200 error
= copy_buckets_for_remove_bucket(old_group
, new_group
,
7201 ogm
->gm
.command_bucket_id
);
7207 /* The group creation time does not change during modification. */
7208 *CONST_CAST(long long int *, &(new_group
->created
)) = old_group
->created
;
7209 *CONST_CAST(long long int *, &(new_group
->modified
)) = time_msec();
7211 group_collection_add(&ogm
->old_groups
, old_group
);
7213 /* Mark the old group for deletion. */
7214 versions_set_remove_version(&old_group
->versions
, ogm
->version
);
7215 /* Insert replacement group. */
7216 cmap_insert(&ofproto
->groups
, &new_group
->cmap_node
,
7217 hash_int(new_group
->group_id
, 0));
7218 /* Transfer rules. */
7219 rule_collection_move(&new_group
->rules
, &old_group
->rules
);
7221 if (old_group
->type
!= new_group
->type
) {
7222 ofproto
->n_groups
[old_group
->type
]--;
7223 ofproto
->n_groups
[new_group
->type
]++;
7228 ofproto_group_unref(new_group
);
7232 /* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not
7233 * exist yet and modifies it otherwise */
7235 add_or_modify_group_start(struct ofproto
*ofproto
,
7236 struct ofproto_group_mod
*ogm
)
7237 OVS_REQUIRES(ofproto_mutex
)
7241 if (!ofproto_group_exists(ofproto
, ogm
->gm
.group_id
)) {
7242 error
= add_group_start(ofproto
, ogm
);
7244 error
= modify_group_start(ofproto
, ogm
);
7251 delete_group_start(struct ofproto
*ofproto
, ovs_version_t version
,
7252 struct group_collection
*groups
, struct ofgroup
*group
)
7253 OVS_REQUIRES(ofproto_mutex
)
7255 /* Makes flow deletion code leave the rule pointers in 'group->rules'
7256 * intact, so that we can later refer to the rules deleted due to the group
7257 * deletion. Rule pointers will be removed from all other groups, if any,
7258 * so we will never try to delete the same rule twice. */
7259 group
->being_deleted
= true;
7261 /* Mark all the referring groups for deletion. */
7262 delete_flows_start__(ofproto
, version
, &group
->rules
);
7263 group_collection_add(groups
, group
);
7264 versions_set_remove_version(&group
->versions
, version
);
7265 ofproto
->n_groups
[group
->type
]--;
7269 delete_group_finish(struct ofproto
*ofproto
, struct ofgroup
*group
)
7270 OVS_REQUIRES(ofproto_mutex
)
7272 /* Finish deletion of all flow entries containing this group in a group
7274 delete_flows_finish__(ofproto
, &group
->rules
, OFPRR_GROUP_DELETE
, NULL
);
7276 /* Group removal is postponed by the caller. */
7279 /* Implements OFPGC11_DELETE. */
7281 delete_groups_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
7282 OVS_REQUIRES(ofproto_mutex
)
7284 struct ofgroup
*group
;
7286 if (ogm
->gm
.group_id
== OFPG_ALL
) {
7287 CMAP_FOR_EACH (group
, cmap_node
, &ofproto
->groups
) {
7288 if (versions_visible_in_version(&group
->versions
, ogm
->version
)) {
7289 delete_group_start(ofproto
, ogm
->version
, &ogm
->old_groups
,
7294 group
= ofproto_group_lookup__(ofproto
, ogm
->gm
.group_id
, ogm
->version
);
7296 delete_group_start(ofproto
, ogm
->version
, &ogm
->old_groups
, group
);
7302 ofproto_group_mod_start(struct ofproto
*ofproto
, struct ofproto_group_mod
*ogm
)
7303 OVS_REQUIRES(ofproto_mutex
)
7307 ogm
->new_group
= NULL
;
7308 group_collection_init(&ogm
->old_groups
);
7310 switch (ogm
->gm
.command
) {
7312 error
= add_group_start(ofproto
, ogm
);
7315 case OFPGC11_MODIFY
:
7316 error
= modify_group_start(ofproto
, ogm
);
7319 case OFPGC11_ADD_OR_MOD
:
7320 error
= add_or_modify_group_start(ofproto
, ogm
);
7323 case OFPGC11_DELETE
:
7324 delete_groups_start(ofproto
, ogm
);
7328 case OFPGC15_INSERT_BUCKET
:
7329 error
= modify_group_start(ofproto
, ogm
);
7332 case OFPGC15_REMOVE_BUCKET
:
7333 error
= modify_group_start(ofproto
, ogm
);
7337 if (ogm
->gm
.command
> OFPGC11_DELETE
) {
7338 VLOG_INFO_RL(&rl
, "%s: Invalid group_mod command type %d",
7339 ofproto
->name
, ogm
->gm
.command
);
7341 error
= OFPERR_OFPGMFC_BAD_COMMAND
;
7348 ofproto_group_mod_revert(struct ofproto
*ofproto
,
7349 struct ofproto_group_mod
*ogm
)
7350 OVS_REQUIRES(ofproto_mutex
)
7352 struct ofgroup
*new_group
= ogm
->new_group
;
7353 struct ofgroup
*old_group
;
7355 /* Restore replaced or deleted groups. */
7356 GROUP_COLLECTION_FOR_EACH (old_group
, &ogm
->old_groups
) {
7357 ofproto
->n_groups
[old_group
->type
]++;
7359 ovs_assert(group_collection_n(&ogm
->old_groups
) == 1);
7360 /* Transfer rules back. */
7361 rule_collection_move(&old_group
->rules
, &new_group
->rules
);
7363 old_group
->being_deleted
= false;
7364 /* Revert rule deletion. */
7365 delete_flows_revert__(ofproto
, &old_group
->rules
);
7367 /* Restore visibility. */
7368 versions_set_remove_version(&old_group
->versions
,
7369 OVS_VERSION_NOT_REMOVED
);
7372 /* Remove the new group immediately. It was never visible to
7374 cmap_remove(&ofproto
->groups
, &new_group
->cmap_node
,
7375 hash_int(new_group
->group_id
, 0));
7376 ofproto
->n_groups
[new_group
->type
]--;
7377 ofproto_group_unref(new_group
);
7382 ofproto_group_mod_finish(struct ofproto
*ofproto
,
7383 struct ofproto_group_mod
*ogm
,
7384 const struct openflow_mod_requester
*req
)
7385 OVS_REQUIRES(ofproto_mutex
)
7387 struct ofgroup
*new_group
= ogm
->new_group
;
7388 struct ofgroup
*old_group
;
7390 if (new_group
&& group_collection_n(&ogm
->old_groups
) &&
7391 ofproto
->ofproto_class
->group_modify
) {
7392 /* Modify a group. */
7393 ovs_assert(group_collection_n(&ogm
->old_groups
) == 1);
7395 /* XXX: OK to lose old group's stats? */
7396 ofproto
->ofproto_class
->group_modify(new_group
);
7399 /* Delete old groups. */
7400 GROUP_COLLECTION_FOR_EACH(old_group
, &ogm
->old_groups
) {
7401 delete_group_finish(ofproto
, old_group
);
7403 remove_groups_postponed(&ogm
->old_groups
);
7406 struct ofputil_requestforward rf
;
7407 rf
.xid
= req
->request
->xid
;
7408 rf
.reason
= OFPRFR_GROUP_MOD
;
7409 rf
.group_mod
= &ogm
->gm
;
7410 rf
.new_buckets
= new_group
? &new_group
->buckets
: NULL
;
7411 rf
.group_existed
= group_collection_n(&ogm
->old_groups
) > 0;
7412 connmgr_send_requestforward(ofproto
->connmgr
, req
->ofconn
, &rf
);
7416 /* Delete all groups from 'ofproto'.
7418 * This is intended for use within an ofproto provider's 'destruct'
7421 ofproto_group_delete_all__(struct ofproto
*ofproto
)
7422 OVS_REQUIRES(ofproto_mutex
)
7424 struct ofproto_group_mod ogm
;
7425 ogm
.gm
.command
= OFPGC11_DELETE
;
7426 ogm
.gm
.group_id
= OFPG_ALL
;
7427 ogm
.version
= ofproto
->tables_version
+ 1;
7429 ofproto_group_mod_start(ofproto
, &ogm
);
7430 ofproto_bump_tables_version(ofproto
);
7431 ofproto_group_mod_finish(ofproto
, &ogm
, NULL
);
7434 /* Delete all groups from 'ofproto'.
7436 * This is intended for use within an ofproto provider's 'destruct'
7439 ofproto_group_delete_all(struct ofproto
*ofproto
)
7440 OVS_EXCLUDED(ofproto_mutex
)
7442 ovs_mutex_lock(&ofproto_mutex
);
7443 ofproto_group_delete_all__(ofproto
);
7444 ovs_mutex_unlock(&ofproto_mutex
);
7448 handle_group_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7449 OVS_EXCLUDED(ofproto_mutex
)
7451 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7452 struct ofproto_group_mod ogm
;
7455 error
= reject_slave_controller(ofconn
);
7460 error
= ofputil_decode_group_mod(oh
, &ogm
.gm
);
7465 ovs_mutex_lock(&ofproto_mutex
);
7466 ogm
.version
= ofproto
->tables_version
+ 1;
7467 error
= ofproto_group_mod_start(ofproto
, &ogm
);
7469 struct openflow_mod_requester req
= { ofconn
, oh
};
7471 ofproto_bump_tables_version(ofproto
);
7472 ofproto_group_mod_finish(ofproto
, &ogm
, &req
);
7473 ofmonitor_flush(ofproto
->connmgr
);
7475 ovs_mutex_unlock(&ofproto_mutex
);
7477 ofputil_uninit_group_mod(&ogm
.gm
);
7482 enum ofputil_table_miss
7483 ofproto_table_get_miss_config(const struct ofproto
*ofproto
, uint8_t table_id
)
7485 enum ofputil_table_miss miss
;
7487 atomic_read_relaxed(&ofproto
->tables
[table_id
].miss_config
, &miss
);
7492 table_mod__(struct oftable
*oftable
,
7493 const struct ofputil_table_mod
*tm
)
7495 if (tm
->miss
== OFPUTIL_TABLE_MISS_DEFAULT
) {
7496 /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any
7497 * table-miss configuration (because the protocol used doesn't have
7498 * such a concept), so there's nothing to do. */
7500 atomic_store_relaxed(&oftable
->miss_config
, tm
->miss
);
7503 unsigned int new_eviction
= oftable
->eviction
;
7504 if (tm
->eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
7505 new_eviction
|= EVICTION_OPENFLOW
;
7506 } else if (tm
->eviction
== OFPUTIL_TABLE_EVICTION_OFF
) {
7507 new_eviction
&= ~EVICTION_OPENFLOW
;
7510 if (new_eviction
!= oftable
->eviction
) {
7511 ovs_mutex_lock(&ofproto_mutex
);
7512 oftable_configure_eviction(oftable
, new_eviction
,
7513 oftable
->eviction_fields
,
7514 oftable
->n_eviction_fields
);
7515 ovs_mutex_unlock(&ofproto_mutex
);
7518 if (tm
->vacancy
!= OFPUTIL_TABLE_VACANCY_DEFAULT
) {
7519 ovs_mutex_lock(&ofproto_mutex
);
7520 oftable
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
7521 oftable
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
7522 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_OFF
) {
7523 oftable
->vacancy_event
= 0;
7524 } else if (!oftable
->vacancy_event
) {
7525 uint8_t vacancy
= oftable_vacancy(oftable
);
7526 oftable
->vacancy_event
= (vacancy
< oftable
->vacancy_up
7528 : OFPTR_VACANCY_DOWN
);
7530 ovs_mutex_unlock(&ofproto_mutex
);
7535 table_mod(struct ofproto
*ofproto
, const struct ofputil_table_mod
*tm
)
7537 if (!check_table_id(ofproto
, tm
->table_id
)) {
7538 return OFPERR_OFPTMFC_BAD_TABLE
;
7541 /* Don't allow the eviction flags to be changed (except to the only fixed
7542 * value that OVS supports). OF1.4 says this is normal: "The
7543 * OFPTMPT_EVICTION property usually cannot be modified using a
7544 * OFP_TABLE_MOD request, because the eviction mechanism is switch
7546 if (tm
->eviction_flags
!= UINT32_MAX
7547 && tm
->eviction_flags
!= OFPROTO_EVICTION_FLAGS
) {
7548 return OFPERR_OFPTMFC_BAD_CONFIG
;
7551 if (tm
->table_id
== OFPTT_ALL
) {
7552 struct oftable
*oftable
;
7553 OFPROTO_FOR_EACH_TABLE (oftable
, ofproto
) {
7554 if (!(oftable
->flags
& (OFTABLE_HIDDEN
| OFTABLE_READONLY
))) {
7555 table_mod__(oftable
, tm
);
7559 struct oftable
*oftable
= &ofproto
->tables
[tm
->table_id
];
7560 if (oftable
->flags
& OFTABLE_READONLY
) {
7561 return OFPERR_OFPTMFC_EPERM
;
7563 table_mod__(oftable
, tm
);
7570 handle_table_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7572 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7573 struct ofputil_table_mod tm
;
7576 error
= reject_slave_controller(ofconn
);
7581 error
= ofputil_decode_table_mod(oh
, &tm
);
7586 return table_mod(ofproto
, &tm
);
7589 /* Free resources that may be allocated by ofproto_flow_mod_init(). */
7591 ofproto_flow_mod_uninit(struct ofproto_flow_mod
*ofm
)
7593 if (ofm
->temp_rule
) {
7594 ofproto_rule_unref(ofm
->temp_rule
);
7595 ofm
->temp_rule
= NULL
;
7597 if (ofm
->criteria
.version
!= OVS_VERSION_NOT_REMOVED
) {
7598 rule_criteria_destroy(&ofm
->criteria
);
7607 /* Initializes 'ofm' with 'ofproto', 'fm', and 'rule'. 'rule' may be null, but
7608 * if it is nonnull then the caller must own a reference to it, which on
7609 * success is transferred to 'ofm' and on failure is unreffed. */
7611 ofproto_flow_mod_init(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
7612 const struct ofputil_flow_mod
*fm
, struct rule
*rule
)
7613 OVS_EXCLUDED(ofproto_mutex
)
7617 /* Forward flow mod fields we need later. */
7618 ofm
->command
= fm
->command
;
7619 ofm
->modify_cookie
= fm
->modify_cookie
;
7621 ofm
->modify_may_add_flow
= (fm
->new_cookie
!= OVS_BE64_MAX
7622 && fm
->cookie_mask
== htonll(0));
7623 /* Old flags must be kept when modifying a flow, but we still must
7624 * honor the reset counts flag if present in the flow mod. */
7625 ofm
->modify_keep_counts
= !(fm
->flags
& OFPUTIL_FF_RESET_COUNTS
);
7627 /* Initialize state needed by ofproto_flow_mod_uninit(). */
7628 ofm
->temp_rule
= rule
;
7629 ofm
->criteria
.version
= OVS_VERSION_NOT_REMOVED
;
7633 bool check_buffer_id
= false;
7635 switch (ofm
->command
) {
7637 check_buffer_id
= true;
7638 error
= add_flow_init(ofproto
, ofm
, fm
);
7641 check_buffer_id
= true;
7642 error
= modify_flows_init_loose(ofproto
, ofm
, fm
);
7644 case OFPFC_MODIFY_STRICT
:
7645 check_buffer_id
= true;
7646 error
= modify_flow_init_strict(ofproto
, ofm
, fm
);
7649 error
= delete_flows_init_loose(ofproto
, ofm
, fm
);
7651 case OFPFC_DELETE_STRICT
:
7652 error
= delete_flows_init_strict(ofproto
, ofm
, fm
);
7655 error
= OFPERR_OFPFMFC_BAD_COMMAND
;
7658 if (!error
&& check_buffer_id
&& fm
->buffer_id
!= UINT32_MAX
) {
7659 error
= OFPERR_OFPBRC_BUFFER_UNKNOWN
;
7663 ofproto_flow_mod_uninit(ofm
);
7669 ofproto_flow_mod_start(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
7670 OVS_REQUIRES(ofproto_mutex
)
7674 rule_collection_init(&ofm
->old_rules
);
7675 rule_collection_init(&ofm
->new_rules
);
7677 switch (ofm
->command
) {
7679 error
= add_flow_start(ofproto
, ofm
);
7682 error
= modify_flows_start_loose(ofproto
, ofm
);
7684 case OFPFC_MODIFY_STRICT
:
7685 error
= modify_flow_start_strict(ofproto
, ofm
);
7688 error
= delete_flows_start_loose(ofproto
, ofm
);
7690 case OFPFC_DELETE_STRICT
:
7691 error
= delete_flow_start_strict(ofproto
, ofm
);
7696 /* Release resources not needed after start. */
7697 ofproto_flow_mod_uninit(ofm
);
7700 rule_collection_destroy(&ofm
->old_rules
);
7701 rule_collection_destroy(&ofm
->new_rules
);
7707 ofproto_flow_mod_revert(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
)
7708 OVS_REQUIRES(ofproto_mutex
)
7710 switch (ofm
->command
) {
7712 add_flow_revert(ofproto
, ofm
);
7716 case OFPFC_MODIFY_STRICT
:
7717 modify_flows_revert(ofproto
, ofm
);
7721 case OFPFC_DELETE_STRICT
:
7722 delete_flows_revert(ofproto
, ofm
);
7729 rule_collection_destroy(&ofm
->old_rules
);
7730 rule_collection_destroy(&ofm
->new_rules
);
7734 ofproto_flow_mod_finish(struct ofproto
*ofproto
, struct ofproto_flow_mod
*ofm
,
7735 const struct openflow_mod_requester
*req
)
7736 OVS_REQUIRES(ofproto_mutex
)
7738 switch (ofm
->command
) {
7740 add_flow_finish(ofproto
, ofm
, req
);
7744 case OFPFC_MODIFY_STRICT
:
7745 modify_flows_finish(ofproto
, ofm
, req
);
7749 case OFPFC_DELETE_STRICT
:
7750 delete_flows_finish(ofproto
, ofm
, req
);
7757 rule_collection_destroy(&ofm
->old_rules
);
7758 rule_collection_destroy(&ofm
->new_rules
);
7761 ofconn_report_flow_mod(req
->ofconn
, ofm
->command
);
7765 /* Commit phases (all while locking ofproto_mutex):
7767 * 1. Begin: Gather resources and make changes visible in the next version.
7768 * - Mark affected rules for removal in the next version.
7769 * - Create new replacement rules, make visible in the next
7771 * - Do not send any events or notifications.
7773 * 2. Revert: Fail if any errors are found. After this point no errors are
7774 * possible. No visible changes were made, so rollback is minimal (remove
7775 * added invisible rules, restore visibility of rules marked for removal).
7777 * 3. Finish: Make the changes visible for lookups. Insert replacement rules to
7778 * the ofproto provider. Remove replaced and deleted rules from ofproto data
7779 * structures, and Schedule postponed removal of deleted rules from the
7780 * classifier. Send notifications, buffered packets, etc.
7783 do_bundle_commit(struct ofconn
*ofconn
, uint32_t id
, uint16_t flags
)
7785 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7786 ovs_version_t version
= ofproto
->tables_version
+ 1;
7787 struct ofp_bundle
*bundle
;
7788 struct ofp_bundle_entry
*be
;
7791 bundle
= ofconn_get_bundle(ofconn
, id
);
7794 return OFPERR_OFPBFC_BAD_ID
;
7796 if (bundle
->flags
!= flags
) {
7797 error
= OFPERR_OFPBFC_BAD_FLAGS
;
7799 bool prev_is_port_mod
= false;
7802 ovs_mutex_lock(&ofproto_mutex
);
7805 LIST_FOR_EACH (be
, node
, &bundle
->msg_list
) {
7806 if (be
->type
== OFPTYPE_PORT_MOD
) {
7807 /* Our port mods are not atomic. */
7808 if (flags
& OFPBF_ATOMIC
) {
7809 error
= OFPERR_OFPBFC_MSG_FAILED
;
7811 prev_is_port_mod
= true;
7812 error
= port_mod_start(ofconn
, &be
->opm
.pm
, &be
->opm
.port
);
7815 /* Flow & group mods between port mods are applied as a single
7816 * version, but the versions are published only after we know
7817 * the commit is successful. */
7818 if (prev_is_port_mod
) {
7819 prev_is_port_mod
= false;
7822 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7823 /* Store the version in which the changes should take
7825 be
->ofm
.version
= version
;
7826 error
= ofproto_flow_mod_start(ofproto
, &be
->ofm
);
7827 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7828 /* Store the version in which the changes should take
7830 be
->ogm
.version
= version
;
7831 error
= ofproto_group_mod_start(ofproto
, &be
->ogm
);
7832 } else if (be
->type
== OFPTYPE_PACKET_OUT
) {
7833 be
->opo
.version
= version
;
7834 error
= ofproto_packet_out_start(ofproto
, &be
->opo
);
7845 /* Send error referring to the original message. */
7847 ofconn_send_error(ofconn
, be
->msg
, error
);
7848 error
= OFPERR_OFPBFC_MSG_FAILED
;
7851 /* 2. Revert. Undo all the changes made above. */
7852 LIST_FOR_EACH_REVERSE_CONTINUE(be
, node
, &bundle
->msg_list
) {
7853 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7854 ofproto_flow_mod_revert(ofproto
, &be
->ofm
);
7855 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7856 ofproto_group_mod_revert(ofproto
, &be
->ogm
);
7857 } else if (be
->type
== OFPTYPE_PACKET_OUT
) {
7858 ofproto_packet_out_revert(ofproto
, &be
->opo
);
7860 /* Nothing needs to be reverted for a port mod. */
7864 LIST_FOR_EACH (be
, node
, &bundle
->msg_list
) {
7865 if (be
->type
== OFPTYPE_PORT_MOD
) {
7866 /* Perform the actual port mod. This is not atomic, i.e.,
7867 * the effects will be immediately seen by upcall
7868 * processing regardless of the lookup version. It should
7869 * be noted that port configuration changes can originate
7870 * also from OVSDB changes asynchronously to all upcall
7872 port_mod_finish(ofconn
, &be
->opm
.pm
, be
->opm
.port
);
7875 (be
->type
== OFPTYPE_FLOW_MOD
) ? be
->ofm
.version
:
7876 (be
->type
== OFPTYPE_GROUP_MOD
) ? be
->ogm
.version
:
7877 (be
->type
== OFPTYPE_PACKET_OUT
) ? be
->opo
.version
:
7880 /* Bump the lookup version to the one of the current
7881 * message. This makes all the changes in the bundle at
7882 * this version visible to lookups at once. */
7883 if (ofproto
->tables_version
< version
) {
7884 ofproto
->tables_version
= version
;
7885 ofproto
->ofproto_class
->set_tables_version(
7886 ofproto
, ofproto
->tables_version
);
7889 struct openflow_mod_requester req
= { ofconn
, be
->msg
};
7891 if (be
->type
== OFPTYPE_FLOW_MOD
) {
7892 ofproto_flow_mod_finish(ofproto
, &be
->ofm
, &req
);
7893 } else if (be
->type
== OFPTYPE_GROUP_MOD
) {
7894 ofproto_group_mod_finish(ofproto
, &be
->ogm
, &req
);
7895 } else if (be
->type
== OFPTYPE_PACKET_OUT
) {
7896 ofproto_packet_out_finish(ofproto
, &be
->opo
);
7902 ofmonitor_flush(ofproto
->connmgr
);
7903 ovs_mutex_unlock(&ofproto_mutex
);
7906 /* The bundle is discarded regardless the outcome. */
7907 ofp_bundle_remove__(ofconn
, bundle
);
7912 handle_bundle_control(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7914 struct ofputil_bundle_ctrl_msg bctrl
;
7915 struct ofputil_bundle_ctrl_msg reply
;
7919 error
= reject_slave_controller(ofconn
);
7924 error
= ofputil_decode_bundle_ctrl(oh
, &bctrl
);
7929 reply
.bundle_id
= bctrl
.bundle_id
;
7931 switch (bctrl
.type
) {
7932 case OFPBCT_OPEN_REQUEST
:
7933 error
= ofp_bundle_open(ofconn
, bctrl
.bundle_id
, bctrl
.flags
, oh
);
7934 reply
.type
= OFPBCT_OPEN_REPLY
;
7936 case OFPBCT_CLOSE_REQUEST
:
7937 error
= ofp_bundle_close(ofconn
, bctrl
.bundle_id
, bctrl
.flags
);
7938 reply
.type
= OFPBCT_CLOSE_REPLY
;
7940 case OFPBCT_COMMIT_REQUEST
:
7941 error
= do_bundle_commit(ofconn
, bctrl
.bundle_id
, bctrl
.flags
);
7942 reply
.type
= OFPBCT_COMMIT_REPLY
;
7944 case OFPBCT_DISCARD_REQUEST
:
7945 error
= ofp_bundle_discard(ofconn
, bctrl
.bundle_id
);
7946 reply
.type
= OFPBCT_DISCARD_REPLY
;
7949 case OFPBCT_OPEN_REPLY
:
7950 case OFPBCT_CLOSE_REPLY
:
7951 case OFPBCT_COMMIT_REPLY
:
7952 case OFPBCT_DISCARD_REPLY
:
7953 return OFPERR_OFPBFC_BAD_TYPE
;
7958 buf
= ofputil_encode_bundle_ctrl_reply(oh
, &reply
);
7959 ofconn_send_reply(ofconn
, buf
);
7965 handle_bundle_add(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
7966 OVS_EXCLUDED(ofproto_mutex
)
7968 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
7970 struct ofputil_bundle_add_msg badd
;
7973 error
= reject_slave_controller(ofconn
);
7978 error
= ofputil_decode_bundle_add(oh
, &badd
, &type
);
7983 /* Allocate bundle entry and decode the embedded message. */
7984 struct ofp_bundle_entry
*bmsg
= xmalloc(sizeof *bmsg
);
7986 struct ofpbuf ofpacts
;
7987 uint64_t ofpacts_stub
[1024 / 8];
7988 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
7990 if (type
== OFPTYPE_PORT_MOD
) {
7991 error
= ofputil_decode_port_mod(badd
.msg
, &bmsg
->opm
.pm
, false);
7992 } else if (type
== OFPTYPE_FLOW_MOD
) {
7993 struct ofputil_flow_mod fm
;
7995 error
= ofputil_decode_flow_mod(&fm
, badd
.msg
,
7996 ofconn_get_protocol(ofconn
),
7997 ofproto_get_tun_tab(ofproto
),
7998 &ofproto
->vl_mff_map
, &ofpacts
,
7999 u16_to_ofp(ofproto
->max_ports
),
8002 error
= ofproto_flow_mod_init(ofproto
, &bmsg
->ofm
, &fm
, NULL
);
8003 minimatch_destroy(&fm
.match
);
8005 } else if (type
== OFPTYPE_GROUP_MOD
) {
8006 error
= ofputil_decode_group_mod(badd
.msg
, &bmsg
->ogm
.gm
);
8007 } else if (type
== OFPTYPE_PACKET_OUT
) {
8008 struct ofputil_packet_out po
;
8010 COVERAGE_INC(ofproto_packet_out
);
8012 /* Decode message. */
8013 error
= ofputil_decode_packet_out(&po
, badd
.msg
,
8014 ofproto_get_tun_tab(ofproto
),
8017 po
.ofpacts
= ofpbuf_steal_data(&ofpacts
); /* Move to heap. */
8018 error
= ofproto_packet_out_init(ofproto
, ofconn
, &bmsg
->opo
, &po
);
8023 ofpbuf_uninit(&ofpacts
);
8029 /* Now that the embedded message has been successfully decoded, finish up
8030 * initializing the bundle entry. */
8032 bmsg
->msg
= xmemdup(oh
, ntohs(oh
->length
));
8034 /* Add bundle entry to bundle. */
8035 error
= ofp_bundle_add_message(ofconn
, badd
.bundle_id
, badd
.flags
,
8038 ofp_bundle_entry_free(bmsg
);
8044 handle_tlv_table_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
8046 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
8047 struct tun_table
*old_tab
, *new_tab
;
8048 struct ofputil_tlv_table_mod ttm
;
8051 error
= reject_slave_controller(ofconn
);
8056 error
= ofputil_decode_tlv_table_mod(oh
, &ttm
);
8061 old_tab
= ovsrcu_get_protected(struct tun_table
*, &ofproto
->metadata_tab
);
8062 error
= tun_metadata_table_mod(&ttm
, old_tab
, &new_tab
);
8064 ovs_mutex_lock(&ofproto
->vl_mff_map
.mutex
);
8065 error
= mf_vl_mff_map_mod_from_tun_metadata(&ofproto
->vl_mff_map
,
8067 ovs_mutex_unlock(&ofproto
->vl_mff_map
.mutex
);
8069 ovsrcu_set(&ofproto
->metadata_tab
, new_tab
);
8070 tun_metadata_postpone_free(old_tab
);
8072 tun_metadata_free(new_tab
);
8076 ofputil_uninit_tlv_table(&ttm
.mappings
);
8081 handle_tlv_table_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
8083 const struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
8084 struct ofputil_tlv_table_reply ttr
;
8087 tun_metadata_table_request(ofproto_get_tun_tab(ofproto
), &ttr
);
8089 b
= ofputil_encode_tlv_table_reply(oh
, &ttr
);
8090 ofputil_uninit_tlv_table(&ttr
.mappings
);
8092 ofconn_send_reply(ofconn
, b
);
8097 handle_openflow__(struct ofconn
*ofconn
, const struct ofpbuf
*msg
)
8098 OVS_EXCLUDED(ofproto_mutex
)
8100 const struct ofp_header
*oh
= msg
->data
;
8104 error
= ofptype_decode(&type
, oh
);
8108 if (oh
->version
>= OFP13_VERSION
&& ofpmsg_is_stat_request(oh
)
8109 && ofpmp_more(oh
)) {
8110 /* We have no buffer implementation for multipart requests.
8111 * Report overflow for requests which consists of multiple
8113 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW
;
8117 /* OpenFlow requests. */
8118 case OFPTYPE_ECHO_REQUEST
:
8119 return handle_echo_request(ofconn
, oh
);
8121 case OFPTYPE_FEATURES_REQUEST
:
8122 return handle_features_request(ofconn
, oh
);
8124 case OFPTYPE_GET_CONFIG_REQUEST
:
8125 return handle_get_config_request(ofconn
, oh
);
8127 case OFPTYPE_SET_CONFIG
:
8128 return handle_set_config(ofconn
, oh
);
8130 case OFPTYPE_PACKET_OUT
:
8131 return handle_packet_out(ofconn
, oh
);
8133 case OFPTYPE_PORT_MOD
:
8134 return handle_port_mod(ofconn
, oh
);
8136 case OFPTYPE_FLOW_MOD
:
8137 return handle_flow_mod(ofconn
, oh
);
8139 case OFPTYPE_GROUP_MOD
:
8140 return handle_group_mod(ofconn
, oh
);
8142 case OFPTYPE_TABLE_MOD
:
8143 return handle_table_mod(ofconn
, oh
);
8145 case OFPTYPE_METER_MOD
:
8146 return handle_meter_mod(ofconn
, oh
);
8148 case OFPTYPE_BARRIER_REQUEST
:
8149 return handle_barrier_request(ofconn
, oh
);
8151 case OFPTYPE_ROLE_REQUEST
:
8152 return handle_role_request(ofconn
, oh
);
8154 /* OpenFlow replies. */
8155 case OFPTYPE_ECHO_REPLY
:
8158 /* Nicira extension requests. */
8159 case OFPTYPE_FLOW_MOD_TABLE_ID
:
8160 return handle_nxt_flow_mod_table_id(ofconn
, oh
);
8162 case OFPTYPE_SET_FLOW_FORMAT
:
8163 return handle_nxt_set_flow_format(ofconn
, oh
);
8165 case OFPTYPE_SET_PACKET_IN_FORMAT
:
8166 return handle_nxt_set_packet_in_format(ofconn
, oh
);
8168 case OFPTYPE_SET_CONTROLLER_ID
:
8169 return handle_nxt_set_controller_id(ofconn
, oh
);
8171 case OFPTYPE_FLOW_AGE
:
8172 /* Nothing to do. */
8175 case OFPTYPE_FLOW_MONITOR_CANCEL
:
8176 return handle_flow_monitor_cancel(ofconn
, oh
);
8178 case OFPTYPE_SET_ASYNC_CONFIG
:
8179 return handle_nxt_set_async_config(ofconn
, oh
);
8181 case OFPTYPE_GET_ASYNC_REQUEST
:
8182 return handle_nxt_get_async_request(ofconn
, oh
);
8184 case OFPTYPE_NXT_RESUME
:
8185 return handle_nxt_resume(ofconn
, oh
);
8187 /* Statistics requests. */
8188 case OFPTYPE_DESC_STATS_REQUEST
:
8189 return handle_desc_stats_request(ofconn
, oh
);
8191 case OFPTYPE_FLOW_STATS_REQUEST
:
8192 return handle_flow_stats_request(ofconn
, oh
);
8194 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
8195 return handle_aggregate_stats_request(ofconn
, oh
);
8197 case OFPTYPE_TABLE_STATS_REQUEST
:
8198 return handle_table_stats_request(ofconn
, oh
);
8200 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
8201 return handle_table_features_request(ofconn
, oh
);
8203 case OFPTYPE_TABLE_DESC_REQUEST
:
8204 return handle_table_desc_request(ofconn
, oh
);
8206 case OFPTYPE_PORT_STATS_REQUEST
:
8207 return handle_port_stats_request(ofconn
, oh
);
8209 case OFPTYPE_QUEUE_STATS_REQUEST
:
8210 return handle_queue_stats_request(ofconn
, oh
);
8212 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
8213 return handle_port_desc_stats_request(ofconn
, oh
);
8215 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
8216 return handle_flow_monitor_request(ofconn
, oh
);
8218 case OFPTYPE_METER_STATS_REQUEST
:
8219 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
8220 return handle_meter_request(ofconn
, oh
, type
);
8222 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
8223 return handle_meter_features_request(ofconn
, oh
);
8225 case OFPTYPE_GROUP_STATS_REQUEST
:
8226 return handle_group_stats_request(ofconn
, oh
);
8228 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
8229 return handle_group_desc_stats_request(ofconn
, oh
);
8231 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
8232 return handle_group_features_stats_request(ofconn
, oh
);
8234 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
8235 return handle_queue_get_config_request(ofconn
, oh
);
8237 case OFPTYPE_BUNDLE_CONTROL
:
8238 return handle_bundle_control(ofconn
, oh
);
8240 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
8241 return handle_bundle_add(ofconn
, oh
);
8243 case OFPTYPE_NXT_TLV_TABLE_MOD
:
8244 return handle_tlv_table_mod(ofconn
, oh
);
8246 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
8247 return handle_tlv_table_request(ofconn
, oh
);
8249 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST
:
8250 return handle_ipfix_bridge_stats_request(ofconn
, oh
);
8252 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST
:
8253 return handle_ipfix_flow_stats_request(ofconn
, oh
);
8255 case OFPTYPE_CT_FLUSH_ZONE
:
8256 return handle_nxt_ct_flush_zone(ofconn
, oh
);
8260 case OFPTYPE_FEATURES_REPLY
:
8261 case OFPTYPE_GET_CONFIG_REPLY
:
8262 case OFPTYPE_PACKET_IN
:
8263 case OFPTYPE_FLOW_REMOVED
:
8264 case OFPTYPE_PORT_STATUS
:
8265 case OFPTYPE_BARRIER_REPLY
:
8266 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
8267 case OFPTYPE_DESC_STATS_REPLY
:
8268 case OFPTYPE_FLOW_STATS_REPLY
:
8269 case OFPTYPE_QUEUE_STATS_REPLY
:
8270 case OFPTYPE_PORT_STATS_REPLY
:
8271 case OFPTYPE_TABLE_STATS_REPLY
:
8272 case OFPTYPE_AGGREGATE_STATS_REPLY
:
8273 case OFPTYPE_PORT_DESC_STATS_REPLY
:
8274 case OFPTYPE_ROLE_REPLY
:
8275 case OFPTYPE_FLOW_MONITOR_PAUSED
:
8276 case OFPTYPE_FLOW_MONITOR_RESUMED
:
8277 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
8278 case OFPTYPE_GET_ASYNC_REPLY
:
8279 case OFPTYPE_GROUP_STATS_REPLY
:
8280 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
8281 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
8282 case OFPTYPE_METER_STATS_REPLY
:
8283 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
8284 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
8285 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
8286 case OFPTYPE_TABLE_DESC_REPLY
:
8287 case OFPTYPE_ROLE_STATUS
:
8288 case OFPTYPE_REQUESTFORWARD
:
8289 case OFPTYPE_TABLE_STATUS
:
8290 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
8291 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY
:
8292 case OFPTYPE_IPFIX_FLOW_STATS_REPLY
:
8294 if (ofpmsg_is_stat_request(oh
)) {
8295 return OFPERR_OFPBRC_BAD_STAT
;
8297 return OFPERR_OFPBRC_BAD_TYPE
;
8303 handle_openflow(struct ofconn
*ofconn
, const struct ofpbuf
*ofp_msg
)
8304 OVS_EXCLUDED(ofproto_mutex
)
8306 enum ofperr error
= handle_openflow__(ofconn
, ofp_msg
);
8309 ofconn_send_error(ofconn
, ofp_msg
->data
, error
);
8311 COVERAGE_INC(ofproto_recv_openflow
);
8315 pick_datapath_id(const struct ofproto
*ofproto
)
8317 const struct ofport
*port
;
8319 port
= ofproto_get_port(ofproto
, OFPP_LOCAL
);
8324 error
= netdev_get_etheraddr(port
->netdev
, &ea
);
8326 return eth_addr_to_uint64(ea
);
8328 VLOG_WARN("%s: could not get MAC address for %s (%s)",
8329 ofproto
->name
, netdev_get_name(port
->netdev
),
8330 ovs_strerror(error
));
8332 return ofproto
->fallback_dpid
;
8336 pick_fallback_dpid(void)
8339 eth_addr_nicira_random(&ea
);
8340 return eth_addr_to_uint64(ea
);
8343 /* Table overflow policy. */
8345 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
8346 * to NULL if the table is not configured to evict rules or if the table
8347 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
8348 * or with no timeouts are not evictable.) */
8350 choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
8351 OVS_REQUIRES(ofproto_mutex
)
8353 struct eviction_group
*evg
;
8356 if (!table
->eviction
) {
8360 /* In the common case, the outer and inner loops here will each be entered
8363 * - The inner loop normally "return"s in its first iteration. If the
8364 * eviction group has any evictable rules, then it always returns in
8367 * - The outer loop only iterates more than once if the largest eviction
8368 * group has no evictable rules.
8370 * - The outer loop can exit only if table's 'max_flows' is all filled up
8371 * by unevictable rules. */
8372 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
8375 HEAP_FOR_EACH (rule
, evg_node
, &evg
->rules
) {
8384 /* Eviction groups. */
8386 /* Returns the priority to use for an eviction_group that contains 'n_rules'
8387 * rules. The priority contains low-order random bits to ensure that eviction
8388 * groups with the same number of rules are prioritized randomly. */
8390 eviction_group_priority(size_t n_rules
)
8392 uint16_t size
= MIN(UINT16_MAX
, n_rules
);
8393 return (size
<< 16) | random_uint16();
8396 /* Updates 'evg', an eviction_group within 'table', following a change that
8397 * adds or removes rules in 'evg'. */
8399 eviction_group_resized(struct oftable
*table
, struct eviction_group
*evg
)
8400 OVS_REQUIRES(ofproto_mutex
)
8402 heap_change(&table
->eviction_groups_by_size
, &evg
->size_node
,
8403 eviction_group_priority(heap_count(&evg
->rules
)));
8406 /* Destroys 'evg', an eviction_group within 'table':
8408 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
8409 * rules themselves, just removes them from the eviction group.)
8411 * - Removes 'evg' from 'table'.
8415 eviction_group_destroy(struct oftable
*table
, struct eviction_group
*evg
)
8416 OVS_REQUIRES(ofproto_mutex
)
8418 while (!heap_is_empty(&evg
->rules
)) {
8421 rule
= CONTAINER_OF(heap_pop(&evg
->rules
), struct rule
, evg_node
);
8422 rule
->eviction_group
= NULL
;
8424 hmap_remove(&table
->eviction_groups_by_id
, &evg
->id_node
);
8425 heap_remove(&table
->eviction_groups_by_size
, &evg
->size_node
);
8426 heap_destroy(&evg
->rules
);
8430 /* Removes 'rule' from its eviction group, if any. */
8432 eviction_group_remove_rule(struct rule
*rule
)
8433 OVS_REQUIRES(ofproto_mutex
)
8435 if (rule
->eviction_group
) {
8436 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
8437 struct eviction_group
*evg
= rule
->eviction_group
;
8439 rule
->eviction_group
= NULL
;
8440 heap_remove(&evg
->rules
, &rule
->evg_node
);
8441 if (heap_is_empty(&evg
->rules
)) {
8442 eviction_group_destroy(table
, evg
);
8444 eviction_group_resized(table
, evg
);
8449 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
8450 * returns the hash value. */
8452 eviction_group_hash_rule(struct rule
*rule
)
8453 OVS_REQUIRES(ofproto_mutex
)
8455 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
8456 const struct mf_subfield
*sf
;
8460 hash
= table
->eviction_group_id_basis
;
8461 miniflow_expand(rule
->cr
.match
.flow
, &flow
);
8462 for (sf
= table
->eviction_fields
;
8463 sf
< &table
->eviction_fields
[table
->n_eviction_fields
];
8466 if (mf_are_prereqs_ok(sf
->field
, &flow
, NULL
)) {
8467 union mf_value value
;
8469 mf_get_value(sf
->field
, &flow
, &value
);
8471 bitwise_zero(&value
, sf
->field
->n_bytes
, 0, sf
->ofs
);
8473 if (sf
->ofs
+ sf
->n_bits
< sf
->field
->n_bytes
* 8) {
8474 unsigned int start
= sf
->ofs
+ sf
->n_bits
;
8475 bitwise_zero(&value
, sf
->field
->n_bytes
, start
,
8476 sf
->field
->n_bytes
* 8 - start
);
8478 hash
= hash_bytes(&value
, sf
->field
->n_bytes
, hash
);
8480 hash
= hash_int(hash
, 0);
8487 /* Returns an eviction group within 'table' with the given 'id', creating one
8489 static struct eviction_group
*
8490 eviction_group_find(struct oftable
*table
, uint32_t id
)
8491 OVS_REQUIRES(ofproto_mutex
)
8493 struct eviction_group
*evg
;
8495 HMAP_FOR_EACH_WITH_HASH (evg
, id_node
, id
, &table
->eviction_groups_by_id
) {
8499 evg
= xmalloc(sizeof *evg
);
8500 hmap_insert(&table
->eviction_groups_by_id
, &evg
->id_node
, id
);
8501 heap_insert(&table
->eviction_groups_by_size
, &evg
->size_node
,
8502 eviction_group_priority(0));
8503 heap_init(&evg
->rules
);
8508 /* Returns an eviction priority for 'rule'. The return value should be
8509 * interpreted so that higher priorities make a rule a more attractive
8510 * candidate for eviction. */
8512 rule_eviction_priority(struct ofproto
*ofproto
, struct rule
*rule
)
8513 OVS_REQUIRES(ofproto_mutex
)
8515 /* Calculate absolute time when this flow will expire. If it will never
8516 * expire, then return 0 to make it unevictable. */
8517 long long int expiration
= LLONG_MAX
;
8518 if (rule
->hard_timeout
) {
8519 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
8520 ovs_mutex_lock(&rule
->mutex
);
8521 long long int modified
= rule
->modified
;
8522 ovs_mutex_unlock(&rule
->mutex
);
8524 expiration
= modified
+ rule
->hard_timeout
* 1000;
8526 if (rule
->idle_timeout
) {
8527 uint64_t packets
, bytes
;
8529 long long int idle_expiration
;
8531 ofproto
->ofproto_class
->rule_get_stats(rule
, &packets
, &bytes
, &used
);
8532 idle_expiration
= used
+ rule
->idle_timeout
* 1000;
8533 expiration
= MIN(expiration
, idle_expiration
);
8535 if (expiration
== LLONG_MAX
) {
8539 /* Calculate the time of expiration as a number of (approximate) seconds
8540 * after program startup.
8542 * This should work OK for program runs that last UINT32_MAX seconds or
8543 * less. Therefore, please restart OVS at least once every 136 years. */
8544 uint32_t expiration_ofs
= (expiration
>> 10) - (time_boot_msec() >> 10);
8546 /* Combine expiration time with OpenFlow "importance" to form a single
8547 * priority value. We want flows with relatively low "importance" to be
8548 * evicted before even considering expiration time, so put "importance" in
8549 * the most significant bits and expiration time in the least significant
8552 * Small 'priority' should be evicted before those with large 'priority'.
8553 * The caller expects the opposite convention (a large return value being
8554 * more attractive for eviction) so we invert it before returning. */
8555 uint64_t priority
= ((uint64_t) rule
->importance
<< 32) + expiration_ofs
;
8556 return UINT64_MAX
- priority
;
8559 /* Adds 'rule' to an appropriate eviction group for its oftable's
8560 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
8561 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
8564 * The caller must ensure that 'rule' is not already in an eviction group. */
8566 eviction_group_add_rule(struct rule
*rule
)
8567 OVS_REQUIRES(ofproto_mutex
)
8569 struct ofproto
*ofproto
= rule
->ofproto
;
8570 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
8573 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
8574 * so no additional protection is needed. */
8575 has_timeout
= rule
->hard_timeout
|| rule
->idle_timeout
;
8577 if (table
->eviction
&& has_timeout
) {
8578 struct eviction_group
*evg
;
8580 evg
= eviction_group_find(table
, eviction_group_hash_rule(rule
));
8582 rule
->eviction_group
= evg
;
8583 heap_insert(&evg
->rules
, &rule
->evg_node
,
8584 rule_eviction_priority(ofproto
, rule
));
8585 eviction_group_resized(table
, evg
);
8591 /* Initializes 'table'. */
8593 oftable_init(struct oftable
*table
)
8595 memset(table
, 0, sizeof *table
);
8596 classifier_init(&table
->cls
, flow_segment_u64s
);
8597 table
->max_flows
= UINT_MAX
;
8599 hmap_init(&table
->eviction_groups_by_id
);
8600 heap_init(&table
->eviction_groups_by_size
);
8601 atomic_init(&table
->miss_config
, OFPUTIL_TABLE_MISS_DEFAULT
);
8603 classifier_set_prefix_fields(&table
->cls
, default_prefix_fields
,
8604 ARRAY_SIZE(default_prefix_fields
));
8606 atomic_init(&table
->n_matched
, 0);
8607 atomic_init(&table
->n_missed
, 0);
8610 /* Destroys 'table', including its classifier and eviction groups.
8612 * The caller is responsible for freeing 'table' itself. */
8614 oftable_destroy(struct oftable
*table
)
8616 ovs_assert(classifier_is_empty(&table
->cls
));
8618 ovs_mutex_lock(&ofproto_mutex
);
8619 oftable_configure_eviction(table
, 0, NULL
, 0);
8620 ovs_mutex_unlock(&ofproto_mutex
);
8622 hmap_destroy(&table
->eviction_groups_by_id
);
8623 heap_destroy(&table
->eviction_groups_by_size
);
8624 classifier_destroy(&table
->cls
);
8628 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
8629 * string, then 'table' will use its default name.
8631 * This only affects the name exposed for a table exposed through the OpenFlow
8632 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
8634 oftable_set_name(struct oftable
*table
, const char *name
)
8636 if (name
&& name
[0]) {
8637 int len
= strnlen(name
, OFP_MAX_TABLE_NAME_LEN
);
8638 if (!table
->name
|| strncmp(name
, table
->name
, len
)) {
8640 table
->name
= xmemdup0(name
, len
);
8648 /* oftables support a choice of two policies when adding a rule would cause the
8649 * number of flows in the table to exceed the configured maximum number: either
8650 * they can refuse to add the new flow or they can evict some existing flow.
8651 * This function configures the latter policy on 'table', with fairness based
8652 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
8653 * 'n_fields' as 0 disables fairness.) */
8655 oftable_configure_eviction(struct oftable
*table
, unsigned int eviction
,
8656 const struct mf_subfield
*fields
, size_t n_fields
)
8657 OVS_REQUIRES(ofproto_mutex
)
8661 if ((table
->eviction
!= 0) == (eviction
!= 0)
8662 && n_fields
== table
->n_eviction_fields
8664 || !memcmp(fields
, table
->eviction_fields
,
8665 n_fields
* sizeof *fields
))) {
8666 /* The set of eviction fields did not change. If 'eviction' changed,
8667 * it remains nonzero, so that we can just update table->eviction
8668 * without fussing with the eviction groups. */
8669 table
->eviction
= eviction
;
8673 /* Destroy existing eviction groups, then destroy and recreate data
8674 * structures to recover memory. */
8675 struct eviction_group
*evg
, *next
;
8676 HMAP_FOR_EACH_SAFE (evg
, next
, id_node
, &table
->eviction_groups_by_id
) {
8677 eviction_group_destroy(table
, evg
);
8679 hmap_destroy(&table
->eviction_groups_by_id
);
8680 hmap_init(&table
->eviction_groups_by_id
);
8681 heap_destroy(&table
->eviction_groups_by_size
);
8682 heap_init(&table
->eviction_groups_by_size
);
8684 /* Replace eviction groups by the new ones, if there is a change. Free the
8685 * old fields only after allocating the new ones, because 'fields ==
8686 * table->eviction_fields' is possible. */
8687 struct mf_subfield
*old_fields
= table
->eviction_fields
;
8688 table
->n_eviction_fields
= n_fields
;
8689 table
->eviction_fields
= (fields
8690 ? xmemdup(fields
, n_fields
* sizeof *fields
)
8694 /* Add the new eviction groups, if enabled. */
8695 table
->eviction
= eviction
;
8696 if (table
->eviction
) {
8697 table
->eviction_group_id_basis
= random_uint32();
8698 CLS_FOR_EACH (rule
, cr
, &table
->cls
) {
8699 eviction_group_add_rule(rule
);
8704 /* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted
8705 * it to the classifier. */
8707 ofproto_rule_insert__(struct ofproto
*ofproto
, struct rule
*rule
)
8708 OVS_REQUIRES(ofproto_mutex
)
8710 const struct rule_actions
*actions
= rule_get_actions(rule
);
8712 /* A rule may not be reinserted. */
8713 ovs_assert(rule
->state
!= RULE_INSERTED
);
8715 if (rule
->hard_timeout
|| rule
->idle_timeout
) {
8716 ovs_list_insert(&ofproto
->expirable
, &rule
->expirable
);
8718 cookies_insert(ofproto
, rule
);
8719 eviction_group_add_rule(rule
);
8720 if (actions
->has_meter
) {
8721 meter_insert_rule(rule
);
8723 if (actions
->has_groups
) {
8724 const struct ofpact_group
*a
;
8725 OFPACT_FOR_EACH_TYPE_FLATTENED (a
, GROUP
, actions
->ofpacts
,
8726 actions
->ofpacts_len
) {
8727 struct ofgroup
*group
;
8729 group
= ofproto_group_lookup(ofproto
, a
->group_id
, OVS_VERSION_MAX
,
8731 ovs_assert(group
!= NULL
);
8732 group_add_rule(group
, rule
);
8736 rule
->state
= RULE_INSERTED
;
8739 /* Removes 'rule' from the ofproto data structures. Caller may have deferred
8740 * the removal from the classifier. */
8742 ofproto_rule_remove__(struct ofproto
*ofproto
, struct rule
*rule
)
8743 OVS_REQUIRES(ofproto_mutex
)
8745 ovs_assert(rule
->state
== RULE_INSERTED
);
8747 cookies_remove(ofproto
, rule
);
8749 eviction_group_remove_rule(rule
);
8750 if (!ovs_list_is_empty(&rule
->expirable
)) {
8751 ovs_list_remove(&rule
->expirable
);
8753 if (!ovs_list_is_empty(&rule
->meter_list_node
)) {
8754 ovs_list_remove(&rule
->meter_list_node
);
8755 ovs_list_init(&rule
->meter_list_node
);
8758 /* Remove the rule from any groups, except from the group that is being
8759 * deleted, if any. */
8760 const struct rule_actions
*actions
= rule_get_actions(rule
);
8762 if (actions
->has_groups
) {
8763 const struct ofpact_group
*a
;
8765 OFPACT_FOR_EACH_TYPE_FLATTENED(a
, GROUP
, actions
->ofpacts
,
8766 actions
->ofpacts_len
) {
8767 struct ofgroup
*group
;
8769 group
= ofproto_group_lookup(ofproto
, a
->group_id
, OVS_VERSION_MAX
,
8773 /* Leave the rule for the group that is being deleted, if any,
8774 * as we still need the list of rules for clean-up. */
8775 if (!group
->being_deleted
) {
8776 group_remove_rule(group
, rule
);
8781 rule
->state
= RULE_REMOVED
;
8784 /* unixctl commands. */
8787 ofproto_lookup(const char *name
)
8789 struct ofproto
*ofproto
;
8791 HMAP_FOR_EACH_WITH_HASH (ofproto
, hmap_node
, hash_string(name
, 0),
8793 if (!strcmp(ofproto
->name
, name
)) {
8801 ofproto_unixctl_list(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
8802 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
8804 struct ofproto
*ofproto
;
8808 HMAP_FOR_EACH (ofproto
, hmap_node
, &all_ofprotos
) {
8809 ds_put_format(&results
, "%s\n", ofproto
->name
);
8811 unixctl_command_reply(conn
, ds_cstr(&results
));
8812 ds_destroy(&results
);
8816 ofproto_unixctl_init(void)
8818 static bool registered
;
8824 unixctl_command_register("ofproto/list", "", 0, 0,
8825 ofproto_unixctl_list
, NULL
);
8829 ofproto_set_vlan_limit(int vlan_limit
)
8831 flow_limit_vlans(vlan_limit
);