2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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.
26 #include "byte-order.h"
27 #include "classifier.h"
30 #include "dynamic-string.h"
33 #include "meta-flow.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
39 #include "ofp-print.h"
42 #include "ofproto-provider.h"
43 #include "openflow/nicira-ext.h"
44 #include "openflow/openflow.h"
48 #include "poll-loop.h"
55 #include "unaligned.h"
59 VLOG_DEFINE_THIS_MODULE(ofproto);
61 COVERAGE_DEFINE(ofproto_flush);
62 COVERAGE_DEFINE(ofproto_packet_out);
63 COVERAGE_DEFINE(ofproto_queue_req);
64 COVERAGE_DEFINE(ofproto_recv_openflow);
65 COVERAGE_DEFINE(ofproto_reinit_ports);
66 COVERAGE_DEFINE(ofproto_update_port);
69 S_OPENFLOW, /* Processing OpenFlow commands. */
70 S_EVICT, /* Evicting flows from over-limit tables. */
71 S_FLUSH, /* Deleting all flow table rules. */
74 enum ofoperation_type {
81 /* A single OpenFlow request can execute any number of operations. The
82 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
83 * ofconn to which an error reply should be sent if necessary.
85 * ofproto initiates some operations internally. These operations are still
86 * assigned to groups but will not have an associated ofconn. */
88 struct ofproto *ofproto; /* Owning ofproto. */
89 struct list ofproto_node; /* In ofproto's "pending" list. */
90 struct list ops; /* List of "struct ofoperation"s. */
91 int n_running; /* Number of ops still pending. */
93 /* Data needed to send OpenFlow reply on failure or to send a buffered
96 * If list_is_empty(ofconn_node) then this ofopgroup never had an
97 * associated ofconn or its ofconn's connection dropped after it initiated
98 * the operation. In the latter case 'ofconn' is a wild pointer that
99 * refers to freed memory, so the 'ofconn' member must be used only if
100 * !list_is_empty(ofconn_node).
102 struct list ofconn_node; /* In ofconn's list of pending opgroups. */
103 struct ofconn *ofconn; /* ofconn for reply (but see note above). */
104 struct ofp_header *request; /* Original request (truncated at 64 bytes). */
105 uint32_t buffer_id; /* Buffer id from original request. */
108 static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *);
109 static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *,
110 const struct ofp_header *,
112 static void ofopgroup_submit(struct ofopgroup *);
113 static void ofopgroup_complete(struct ofopgroup *);
115 /* A single flow table operation. */
117 struct ofopgroup *group; /* Owning group. */
118 struct list group_node; /* In ofopgroup's "ops" list. */
119 struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */
120 struct rule *rule; /* Rule being operated upon. */
121 enum ofoperation_type type; /* Type of operation. */
123 /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions
125 struct rule_actions *actions;
127 /* OFOPERATION_DELETE. */
128 enum ofp_flow_removed_reason reason; /* Reason flow was removed. */
130 ovs_be64 flow_cookie; /* Rule's old flow cookie. */
131 uint16_t idle_timeout; /* Rule's old idle timeout. */
132 uint16_t hard_timeout; /* Rule's old hard timeout. */
133 enum ofputil_flow_mod_flags flags; /* Rule's old flags. */
134 enum ofperr error; /* 0 if no error. */
137 static struct ofoperation *ofoperation_create(struct ofopgroup *,
139 enum ofoperation_type,
140 enum ofp_flow_removed_reason);
141 static void ofoperation_destroy(struct ofoperation *);
144 static void oftable_init(struct oftable *);
145 static void oftable_destroy(struct oftable *);
147 static void oftable_set_name(struct oftable *, const char *name);
149 static void oftable_disable_eviction(struct oftable *);
150 static void oftable_enable_eviction(struct oftable *,
151 const struct mf_subfield *fields,
154 static void oftable_remove_rule(struct rule *rule) OVS_REQUIRES(ofproto_mutex);
155 static void oftable_remove_rule__(struct ofproto *, struct rule *)
156 OVS_REQUIRES(ofproto_mutex);
157 static void oftable_insert_rule(struct rule *);
159 /* A set of rules within a single OpenFlow table (oftable) that have the same
160 * values for the oftable's eviction_fields. A rule to be evicted, when one is
161 * needed, is taken from the eviction group that contains the greatest number
164 * An oftable owns any number of eviction groups, each of which contains any
167 * Membership in an eviction group is imprecise, based on the hash of the
168 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
169 * That is, if two rules have different eviction_fields, but those
170 * eviction_fields hash to the same value, then they will belong to the same
171 * eviction_group anyway.
173 * (When eviction is not enabled on an oftable, we don't track any eviction
174 * groups, to save time and space.) */
175 struct eviction_group {
176 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
177 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
178 struct heap rules; /* Contains "struct rule"s. */
181 static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep);
182 static void ofproto_evict(struct ofproto *) OVS_EXCLUDED(ofproto_mutex);
183 static uint32_t rule_eviction_priority(struct rule *);
184 static void eviction_group_add_rule(struct rule *);
185 static void eviction_group_remove_rule(struct rule *);
187 /* Criteria that flow_mod and other operations use for selecting rules on
188 * which to operate. */
189 struct rule_criteria {
190 /* An OpenFlow table or 255 for all tables. */
193 /* OpenFlow matching criteria. Interpreted different in "loose" way by
194 * collect_rules_loose() and "strict" way by collect_rules_strict(), as
195 * defined in the OpenFlow spec. */
198 /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's
199 * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'.
200 * The rule will not be selected if M is 1 and B != C. */
202 ovs_be64 cookie_mask;
204 /* Selection based on actions within a rule:
206 * If out_port != OFPP_ANY, selects only rules that output to out_port.
207 * If out_group != OFPG_ALL, select only rules that output to out_group. */
212 static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
213 const struct match *match,
214 unsigned int priority,
215 ovs_be64 cookie, ovs_be64 cookie_mask,
216 ofp_port_t out_port, uint32_t out_group);
217 static void rule_criteria_destroy(struct rule_criteria *);
219 /* A packet that needs to be passed to rule_execute().
221 * (We can't do this immediately from ofopgroup_complete() because that holds
222 * ofproto_mutex, which rule_execute() needs released.) */
223 struct rule_execute {
224 struct list list_node; /* In struct ofproto's "rule_executes" list. */
225 struct rule *rule; /* Owns a reference to the rule. */
227 struct ofpbuf *packet; /* Owns the packet. */
230 static void run_rule_executes(struct ofproto *) OVS_EXCLUDED(ofproto_mutex);
231 static void destroy_rule_executes(struct ofproto *);
234 static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex);
235 static void ofport_destroy(struct ofport *);
237 static void update_port(struct ofproto *, const char *devname);
238 static int init_ports(struct ofproto *);
239 static void reinit_ports(struct ofproto *);
241 static long long int ofport_get_usage(const struct ofproto *,
242 ofp_port_t ofp_port);
243 static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port,
244 long long int last_used);
245 static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port);
249 * Keeps track of the currently used and recently used ofport values and is
250 * used to prevent immediate recycling of ofport values. */
251 struct ofport_usage {
252 struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */
253 ofp_port_t ofp_port; /* OpenFlow port number. */
254 long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX
255 represents in-use ofports. */
259 static void ofproto_rule_destroy__(struct rule *);
260 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
261 static bool rule_is_modifiable(const struct rule *);
264 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
265 struct ofputil_flow_mod *,
266 const struct ofp_header *);
267 static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
268 struct ofputil_flow_mod *,
269 const struct ofp_header *,
270 const struct rule_collection *);
271 static void delete_flow__(struct rule *rule, struct ofopgroup *,
272 enum ofp_flow_removed_reason)
273 OVS_REQUIRES(ofproto_mutex);
274 static bool ofproto_group_exists__(const struct ofproto *ofproto,
276 OVS_REQ_RDLOCK(ofproto->groups_rwlock);
277 static bool ofproto_group_exists(const struct ofproto *ofproto,
279 OVS_EXCLUDED(ofproto->groups_rwlock);
280 static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
281 static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
282 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
283 struct ofputil_flow_mod *,
284 const struct ofp_header *)
285 OVS_EXCLUDED(ofproto_mutex);
286 static void calc_duration(long long int start, long long int now,
287 uint32_t *sec, uint32_t *nsec);
290 static uint64_t pick_datapath_id(const struct ofproto *);
291 static uint64_t pick_fallback_dpid(void);
292 static void ofproto_destroy__(struct ofproto *);
293 static void update_mtu(struct ofproto *, struct ofport *);
294 static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
297 static void ofproto_unixctl_init(void);
299 /* All registered ofproto classes, in probe order. */
300 static const struct ofproto_class **ofproto_classes;
301 static size_t n_ofproto_classes;
302 static size_t allocated_ofproto_classes;
304 /* Global lock that protects all flow table operations. */
305 struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER;
307 unsigned flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT;
308 unsigned n_handler_threads;
309 enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO;
311 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
312 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
314 /* Initial mappings of port to OpenFlow number mappings. */
315 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
317 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
319 /* The default value of true waits for flow restore. */
320 static bool flow_restore_wait = true;
322 /* Must be called to initialize the ofproto library.
324 * The caller may pass in 'iface_hints', which contains an shash of
325 * "iface_hint" elements indexed by the interface's name. The provider
326 * may use these hints to describe the startup configuration in order to
327 * reinitialize its state. The caller owns the provided data, so a
328 * provider will make copies of anything required. An ofproto provider
329 * will remove any existing state that is not described by the hint, and
330 * may choose to remove it all. */
332 ofproto_init(const struct shash *iface_hints)
334 struct shash_node *node;
337 ofproto_class_register(&ofproto_dpif_class);
339 /* Make a local copy, since we don't own 'iface_hints' elements. */
340 SHASH_FOR_EACH(node, iface_hints) {
341 const struct iface_hint *orig_hint = node->data;
342 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
343 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
345 new_hint->br_name = xstrdup(orig_hint->br_name);
346 new_hint->br_type = xstrdup(br_type);
347 new_hint->ofp_port = orig_hint->ofp_port;
349 shash_add(&init_ofp_ports, node->name, new_hint);
352 for (i = 0; i < n_ofproto_classes; i++) {
353 ofproto_classes[i]->init(&init_ofp_ports);
357 /* 'type' should be a normalized datapath type, as returned by
358 * ofproto_normalize_type(). Returns the corresponding ofproto_class
359 * structure, or a null pointer if there is none registered for 'type'. */
360 static const struct ofproto_class *
361 ofproto_class_find__(const char *type)
365 for (i = 0; i < n_ofproto_classes; i++) {
366 const struct ofproto_class *class = ofproto_classes[i];
371 class->enumerate_types(&types);
372 found = sset_contains(&types, type);
373 sset_destroy(&types);
379 VLOG_WARN("unknown datapath type %s", type);
383 /* Registers a new ofproto class. After successful registration, new ofprotos
384 * of that type can be created using ofproto_create(). */
386 ofproto_class_register(const struct ofproto_class *new_class)
390 for (i = 0; i < n_ofproto_classes; i++) {
391 if (ofproto_classes[i] == new_class) {
396 if (n_ofproto_classes >= allocated_ofproto_classes) {
397 ofproto_classes = x2nrealloc(ofproto_classes,
398 &allocated_ofproto_classes,
399 sizeof *ofproto_classes);
401 ofproto_classes[n_ofproto_classes++] = new_class;
405 /* Unregisters a datapath provider. 'type' must have been previously
406 * registered and not currently be in use by any ofprotos. After
407 * unregistration new datapaths of that type cannot be opened using
408 * ofproto_create(). */
410 ofproto_class_unregister(const struct ofproto_class *class)
414 for (i = 0; i < n_ofproto_classes; i++) {
415 if (ofproto_classes[i] == class) {
416 for (i++; i < n_ofproto_classes; i++) {
417 ofproto_classes[i - 1] = ofproto_classes[i];
423 VLOG_WARN("attempted to unregister an ofproto class that is not "
428 /* Clears 'types' and enumerates all registered ofproto types into it. The
429 * caller must first initialize the sset. */
431 ofproto_enumerate_types(struct sset *types)
436 for (i = 0; i < n_ofproto_classes; i++) {
437 ofproto_classes[i]->enumerate_types(types);
441 /* Returns the fully spelled out name for the given ofproto 'type'.
443 * Normalized type string can be compared with strcmp(). Unnormalized type
444 * string might be the same even if they have different spellings. */
446 ofproto_normalize_type(const char *type)
448 return type && type[0] ? type : "system";
451 /* Clears 'names' and enumerates the names of all known created ofprotos with
452 * the given 'type'. The caller must first initialize the sset. Returns 0 if
453 * successful, otherwise a positive errno value.
455 * Some kinds of datapaths might not be practically enumerable. This is not
456 * considered an error. */
458 ofproto_enumerate_names(const char *type, struct sset *names)
460 const struct ofproto_class *class = ofproto_class_find__(type);
461 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
465 ofproto_create(const char *datapath_name, const char *datapath_type,
466 struct ofproto **ofprotop)
468 const struct ofproto_class *class;
469 struct ofproto *ofproto;
475 ofproto_unixctl_init();
477 datapath_type = ofproto_normalize_type(datapath_type);
478 class = ofproto_class_find__(datapath_type);
480 VLOG_WARN("could not create datapath %s of unknown type %s",
481 datapath_name, datapath_type);
485 ofproto = class->alloc();
487 VLOG_ERR("failed to allocate datapath %s of type %s",
488 datapath_name, datapath_type);
493 ovs_mutex_lock(&ofproto_mutex);
494 memset(ofproto, 0, sizeof *ofproto);
495 ofproto->ofproto_class = class;
496 ofproto->name = xstrdup(datapath_name);
497 ofproto->type = xstrdup(datapath_type);
498 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
499 hash_string(ofproto->name, 0));
500 ofproto->datapath_id = 0;
501 ofproto->forward_bpdu = false;
502 ofproto->fallback_dpid = pick_fallback_dpid();
503 ofproto->mfr_desc = NULL;
504 ofproto->hw_desc = NULL;
505 ofproto->sw_desc = NULL;
506 ofproto->serial_desc = NULL;
507 ofproto->dp_desc = NULL;
508 ofproto->frag_handling = OFPC_FRAG_NORMAL;
509 hmap_init(&ofproto->ports);
510 hmap_init(&ofproto->ofport_usage);
511 shash_init(&ofproto->port_by_name);
512 simap_init(&ofproto->ofp_requests);
513 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
514 ofproto->eviction_group_timer = LLONG_MIN;
515 ofproto->tables = NULL;
516 ofproto->n_tables = 0;
517 hindex_init(&ofproto->cookies);
518 list_init(&ofproto->expirable);
519 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
520 ofproto->state = S_OPENFLOW;
521 list_init(&ofproto->pending);
522 ofproto->n_pending = 0;
523 hmap_init(&ofproto->deletions);
524 guarded_list_init(&ofproto->rule_executes);
525 ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0;
526 ofproto->first_op = ofproto->last_op = LLONG_MIN;
527 ofproto->next_op_report = LLONG_MAX;
528 ofproto->op_backoff = LLONG_MIN;
529 ofproto->vlan_bitmap = NULL;
530 ofproto->vlans_changed = false;
531 ofproto->min_mtu = INT_MAX;
532 ovs_rwlock_init(&ofproto->groups_rwlock);
533 hmap_init(&ofproto->groups);
534 ovs_mutex_unlock(&ofproto_mutex);
535 ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS |
536 OFPGFC_SELECT_WEIGHT;
537 ofproto->ogf.max_groups[OFPGT11_ALL] = OFPG_MAX;
538 ofproto->ogf.max_groups[OFPGT11_SELECT] = OFPG_MAX;
539 ofproto->ogf.max_groups[OFPGT11_INDIRECT] = OFPG_MAX;
540 ofproto->ogf.max_groups[OFPGT11_FF] = OFPG_MAX;
541 ofproto->ogf.actions[0] =
542 (1 << OFPAT11_OUTPUT) |
543 (1 << OFPAT11_COPY_TTL_OUT) |
544 (1 << OFPAT11_COPY_TTL_IN) |
545 (1 << OFPAT11_SET_MPLS_TTL) |
546 (1 << OFPAT11_DEC_MPLS_TTL) |
547 (1 << OFPAT11_PUSH_VLAN) |
548 (1 << OFPAT11_POP_VLAN) |
549 (1 << OFPAT11_PUSH_MPLS) |
550 (1 << OFPAT11_POP_MPLS) |
551 (1 << OFPAT11_SET_QUEUE) |
552 (1 << OFPAT11_GROUP) |
553 (1 << OFPAT11_SET_NW_TTL) |
554 (1 << OFPAT11_DEC_NW_TTL) |
555 (1 << OFPAT12_SET_FIELD);
557 * (1 << OFPAT13_PUSH_PBB) |
558 * (1 << OFPAT13_POP_PBB) */
560 error = ofproto->ofproto_class->construct(ofproto);
562 VLOG_ERR("failed to open datapath %s: %s",
563 datapath_name, ovs_strerror(error));
564 ofproto_destroy__(ofproto);
568 /* Check that hidden tables, if any, are at the end. */
569 ovs_assert(ofproto->n_tables);
570 for (i = 0; i + 1 < ofproto->n_tables; i++) {
571 enum oftable_flags flags = ofproto->tables[i].flags;
572 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
574 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
577 ofproto->datapath_id = pick_datapath_id(ofproto);
580 /* Initialize meters table. */
581 if (ofproto->ofproto_class->meter_get_features) {
582 ofproto->ofproto_class->meter_get_features(ofproto,
583 &ofproto->meter_features);
585 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
587 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
588 * sizeof(struct meter *));
594 /* Must be called (only) by an ofproto implementation in its constructor
595 * function. See the large comment on 'construct' in struct ofproto_class for
598 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
600 struct oftable *table;
602 ovs_assert(!ofproto->n_tables);
603 ovs_assert(n_tables >= 1 && n_tables <= 255);
605 ofproto->n_tables = n_tables;
606 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
607 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
612 /* To be optionally called (only) by an ofproto implementation in its
613 * constructor function. See the large comment on 'construct' in struct
614 * ofproto_class for details.
616 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
617 * will then ensure that actions passed into the ofproto implementation will
618 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
619 * function is not called, there will be no such restriction.
621 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
622 * the 'max_ports' restriction. */
624 ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
626 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
627 ofproto->max_ports = max_ports;
631 ofproto_get_datapath_id(const struct ofproto *ofproto)
633 return ofproto->datapath_id;
637 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
639 uint64_t old_dpid = p->datapath_id;
640 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
641 if (p->datapath_id != old_dpid) {
642 /* Force all active connections to reconnect, since there is no way to
643 * notify a controller that the datapath ID has changed. */
644 ofproto_reconnect_controllers(p);
649 ofproto_set_controllers(struct ofproto *p,
650 const struct ofproto_controller *controllers,
651 size_t n_controllers, uint32_t allowed_versions)
653 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
658 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
660 connmgr_set_fail_mode(p->connmgr, fail_mode);
663 /* Drops the connections between 'ofproto' and all of its controllers, forcing
664 * them to reconnect. */
666 ofproto_reconnect_controllers(struct ofproto *ofproto)
668 connmgr_reconnect(ofproto->connmgr);
671 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
672 * in-band control should guarantee access, in the same way that in-band
673 * control guarantees access to OpenFlow controllers. */
675 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
676 const struct sockaddr_in *extras, size_t n)
678 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
681 /* Sets the OpenFlow queue used by flows set up by in-band control on
682 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
683 * flows will use the default queue. */
685 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
687 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
690 /* Sets the number of flows at which eviction from the kernel flow table
693 ofproto_set_flow_eviction_threshold(unsigned threshold)
695 flow_eviction_threshold = MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN,
699 /* Sets the path for handling flow misses. */
701 ofproto_set_flow_miss_model(unsigned model)
703 flow_miss_model = model;
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 /* Sets number of upcall handler threads. The default is
735 * (number of online cores - 2). */
737 ofproto_set_n_handler_threads(unsigned limit)
740 n_handler_threads = limit;
742 int n_proc = count_cpu_cores();
743 n_handler_threads = n_proc > 2 ? n_proc - 2 : 1;
748 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
751 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
755 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
757 return connmgr_set_snoops(ofproto->connmgr, snoops);
761 ofproto_set_netflow(struct ofproto *ofproto,
762 const struct netflow_options *nf_options)
764 if (nf_options && sset_is_empty(&nf_options->collectors)) {
768 if (ofproto->ofproto_class->set_netflow) {
769 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
771 return nf_options ? EOPNOTSUPP : 0;
776 ofproto_set_sflow(struct ofproto *ofproto,
777 const struct ofproto_sflow_options *oso)
779 if (oso && sset_is_empty(&oso->targets)) {
783 if (ofproto->ofproto_class->set_sflow) {
784 return ofproto->ofproto_class->set_sflow(ofproto, oso);
786 return oso ? EOPNOTSUPP : 0;
791 ofproto_set_ipfix(struct ofproto *ofproto,
792 const struct ofproto_ipfix_bridge_exporter_options *bo,
793 const struct ofproto_ipfix_flow_exporter_options *fo,
796 if (ofproto->ofproto_class->set_ipfix) {
797 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
799 return (bo || fo) ? EOPNOTSUPP : 0;
804 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
806 flow_restore_wait = flow_restore_wait_db;
810 ofproto_get_flow_restore_wait(void)
812 return flow_restore_wait;
816 /* Spanning Tree Protocol (STP) configuration. */
818 /* Configures STP on 'ofproto' using the settings defined in 's'. If
819 * 's' is NULL, disables STP.
821 * Returns 0 if successful, otherwise a positive errno value. */
823 ofproto_set_stp(struct ofproto *ofproto,
824 const struct ofproto_stp_settings *s)
826 return (ofproto->ofproto_class->set_stp
827 ? ofproto->ofproto_class->set_stp(ofproto, s)
831 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
832 * 'enabled' member of 's' is false, then the other members are not
835 * Returns 0 if successful, otherwise a positive errno value. */
837 ofproto_get_stp_status(struct ofproto *ofproto,
838 struct ofproto_stp_status *s)
840 return (ofproto->ofproto_class->get_stp_status
841 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
845 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
846 * in 's'. The caller is responsible for assigning STP port numbers
847 * (using the 'port_num' member in the range of 1 through 255, inclusive)
848 * and ensuring there are no duplicates. If the 's' is NULL, then STP
849 * is disabled on the port.
851 * Returns 0 if successful, otherwise a positive errno value.*/
853 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
854 const struct ofproto_port_stp_settings *s)
856 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
858 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
859 ofproto->name, ofp_port);
863 return (ofproto->ofproto_class->set_stp_port
864 ? ofproto->ofproto_class->set_stp_port(ofport, s)
868 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
869 * 's'. If the 'enabled' member in 's' is false, then the other members
870 * are not meaningful.
872 * Returns 0 if successful, otherwise a positive errno value.*/
874 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
875 struct ofproto_port_stp_status *s)
877 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
879 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
880 "port %"PRIu16, ofproto->name, ofp_port);
884 return (ofproto->ofproto_class->get_stp_port_status
885 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
889 /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
890 * 's'. If the 'enabled' member in 's' is false, then the other members
891 * are not meaningful.
893 * Returns 0 if successful, otherwise a positive errno value.*/
895 ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port,
896 struct ofproto_port_stp_stats *s)
898 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
900 VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent "
901 "port %"PRIu16, ofproto->name, ofp_port);
905 return (ofproto->ofproto_class->get_stp_port_stats
906 ? ofproto->ofproto_class->get_stp_port_stats(ofport, s)
910 /* Queue DSCP configuration. */
912 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
913 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
914 * to implement QoS. Instead, it is used to implement features which require
915 * knowledge of what queues exist on a port, and some basic information about
918 * Returns 0 if successful, otherwise a positive errno value. */
920 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
921 const struct ofproto_port_queue *queues,
924 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
927 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
928 ofproto->name, ofp_port);
932 return (ofproto->ofproto_class->set_queues
933 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
937 /* Connectivity Fault Management configuration. */
939 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
941 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
943 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
944 if (ofport && ofproto->ofproto_class->set_cfm) {
945 ofproto->ofproto_class->set_cfm(ofport, NULL);
949 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
950 * basic configuration from the configuration members in 'cfm', and the remote
951 * maintenance point ID from remote_mpid. Ignores the statistics members of
954 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
956 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
957 const struct cfm_settings *s)
959 struct ofport *ofport;
962 ofport = ofproto_get_port(ofproto, ofp_port);
964 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
965 ofproto->name, ofp_port);
969 /* XXX: For configuration simplicity, we only support one remote_mpid
970 * outside of the CFM module. It's not clear if this is the correct long
971 * term solution or not. */
972 error = (ofproto->ofproto_class->set_cfm
973 ? ofproto->ofproto_class->set_cfm(ofport, s)
976 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
977 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
978 ovs_strerror(error));
982 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
983 * 'ofproto' does not have a port 'ofp_port'. */
985 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
986 const struct smap *cfg)
988 struct ofport *ofport;
991 ofport = ofproto_get_port(ofproto, ofp_port);
993 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
994 ofproto->name, ofp_port);
998 error = (ofproto->ofproto_class->set_bfd
999 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
1002 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
1003 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
1004 ovs_strerror(error));
1008 /* Populates 'status' with key value pairs indicating the status of the BFD
1009 * session on 'ofp_port'. This information is intended to be populated in the
1010 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
1013 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
1014 struct smap *status)
1016 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1017 return (ofport && ofproto->ofproto_class->get_bfd_status
1018 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
1022 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1023 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1024 * 0 if LACP partner information is not current (generally indicating a
1025 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1027 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
1029 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1030 return (ofport && ofproto->ofproto_class->port_is_lacp_current
1031 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
1037 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1038 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1039 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1040 * configuration plus, if there is more than one slave, a bonding
1043 * If 'aux' is already registered then this function updates its configuration
1044 * to 's'. Otherwise, this function registers a new bundle.
1046 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1049 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
1050 const struct ofproto_bundle_settings *s)
1052 return (ofproto->ofproto_class->bundle_set
1053 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
1057 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1058 * If no such bundle has been registered, this has no effect. */
1060 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
1062 return ofproto_bundle_register(ofproto, aux, NULL);
1066 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1067 * If 'aux' is already registered then this function updates its configuration
1068 * to 's'. Otherwise, this function registers a new mirror. */
1070 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1071 const struct ofproto_mirror_settings *s)
1073 return (ofproto->ofproto_class->mirror_set
1074 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1078 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1079 * If no mirror has been registered, this has no effect. */
1081 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
1083 return ofproto_mirror_register(ofproto, aux, NULL);
1086 /* Retrieves statistics from mirror associated with client data pointer
1087 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1088 * 'bytes', respectively. If a particular counters is not supported,
1089 * the appropriate argument is set to UINT64_MAX. */
1091 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1092 uint64_t *packets, uint64_t *bytes)
1094 if (!ofproto->ofproto_class->mirror_get_stats) {
1095 *packets = *bytes = UINT64_MAX;
1099 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1103 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1104 * which all packets are flooded, instead of using MAC learning. If
1105 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1107 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1110 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
1112 return (ofproto->ofproto_class->set_flood_vlans
1113 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1117 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1118 * output for a mirror. */
1120 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1122 return (ofproto->ofproto_class->is_mirror_output_bundle
1123 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1127 /* Configuration of OpenFlow tables. */
1129 /* Returns the number of OpenFlow tables in 'ofproto'. */
1131 ofproto_get_n_tables(const struct ofproto *ofproto)
1133 return ofproto->n_tables;
1136 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1137 * settings from 's'. 'table_id' must be in the range 0 through the number of
1138 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1140 * For read-only tables, only the name may be configured. */
1142 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1143 const struct ofproto_table_settings *s)
1145 struct oftable *table;
1147 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1148 table = &ofproto->tables[table_id];
1150 oftable_set_name(table, s->name);
1152 if (table->flags & OFTABLE_READONLY) {
1157 oftable_enable_eviction(table, s->groups, s->n_groups);
1159 oftable_disable_eviction(table);
1162 table->max_flows = s->max_flows;
1163 ovs_rwlock_wrlock(&table->cls.rwlock);
1164 if (classifier_count(&table->cls) > table->max_flows
1165 && table->eviction_fields) {
1166 /* 'table' contains more flows than allowed. We might not be able to
1167 * evict them right away because of the asynchronous nature of flow
1168 * table changes. Schedule eviction for later. */
1169 switch (ofproto->state) {
1171 ofproto->state = S_EVICT;
1175 /* We're already deleting flows, nothing more to do. */
1180 classifier_set_prefix_fields(&table->cls,
1181 s->prefix_fields, s->n_prefix_fields);
1183 ovs_rwlock_unlock(&table->cls.rwlock);
1187 ofproto_has_snoops(const struct ofproto *ofproto)
1189 return connmgr_has_snoops(ofproto->connmgr);
1193 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1195 connmgr_get_snoops(ofproto->connmgr, snoops);
1199 ofproto_rule_delete__(struct ofproto *ofproto, struct rule *rule,
1201 OVS_REQUIRES(ofproto_mutex)
1203 struct ofopgroup *group;
1205 ovs_assert(!rule->pending);
1207 group = ofopgroup_create_unattached(ofproto);
1208 delete_flow__(rule, group, reason);
1209 ofopgroup_submit(group);
1212 /* Deletes 'rule' from 'cls' within 'ofproto'.
1214 * Within an ofproto implementation, this function allows an ofproto
1215 * implementation to destroy any rules that remain when its ->destruct()
1216 * function is called. This function is not suitable for use elsewhere in an
1217 * ofproto implementation.
1219 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1220 * Cycle" in ofproto-provider.h. */
1222 ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule)
1223 OVS_EXCLUDED(ofproto_mutex)
1225 struct ofopgroup *group;
1227 ovs_mutex_lock(&ofproto_mutex);
1228 ovs_assert(!rule->pending);
1230 group = ofopgroup_create_unattached(ofproto);
1231 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1232 oftable_remove_rule__(ofproto, rule);
1233 ofproto->ofproto_class->rule_delete(rule);
1234 ofopgroup_submit(group);
1236 ovs_mutex_unlock(&ofproto_mutex);
1240 ofproto_flush__(struct ofproto *ofproto)
1241 OVS_EXCLUDED(ofproto_mutex)
1243 struct oftable *table;
1245 if (ofproto->ofproto_class->flush) {
1246 ofproto->ofproto_class->flush(ofproto);
1249 ovs_mutex_lock(&ofproto_mutex);
1250 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1251 struct rule *rule, *next_rule;
1252 struct cls_cursor cursor;
1254 if (table->flags & OFTABLE_HIDDEN) {
1258 ovs_rwlock_rdlock(&table->cls.rwlock);
1259 cls_cursor_init(&cursor, &table->cls, NULL);
1260 ovs_rwlock_unlock(&table->cls.rwlock);
1261 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1262 if (!rule->pending) {
1263 ofproto_rule_delete__(ofproto, rule, OFPRR_DELETE);
1267 ovs_mutex_unlock(&ofproto_mutex);
1270 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1273 ofproto_destroy__(struct ofproto *ofproto)
1274 OVS_EXCLUDED(ofproto_mutex)
1276 struct oftable *table;
1278 ovs_assert(list_is_empty(&ofproto->pending));
1280 destroy_rule_executes(ofproto);
1281 guarded_list_destroy(&ofproto->rule_executes);
1283 delete_group(ofproto, OFPG_ALL);
1284 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1285 hmap_destroy(&ofproto->groups);
1287 connmgr_destroy(ofproto->connmgr);
1289 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1290 free(ofproto->name);
1291 free(ofproto->type);
1292 free(ofproto->mfr_desc);
1293 free(ofproto->hw_desc);
1294 free(ofproto->sw_desc);
1295 free(ofproto->serial_desc);
1296 free(ofproto->dp_desc);
1297 hmap_destroy(&ofproto->ports);
1298 hmap_destroy(&ofproto->ofport_usage);
1299 shash_destroy(&ofproto->port_by_name);
1300 simap_destroy(&ofproto->ofp_requests);
1302 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1303 oftable_destroy(table);
1305 free(ofproto->tables);
1307 hmap_destroy(&ofproto->deletions);
1309 free(ofproto->vlan_bitmap);
1311 ofproto->ofproto_class->dealloc(ofproto);
1315 ofproto_destroy(struct ofproto *p)
1316 OVS_EXCLUDED(ofproto_mutex)
1318 struct ofport *ofport, *next_ofport;
1319 struct ofport_usage *usage, *next_usage;
1326 meter_delete(p, 1, p->meter_features.max_meters);
1327 p->meter_features.max_meters = 0;
1333 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1334 ofport_destroy(ofport);
1337 HMAP_FOR_EACH_SAFE (usage, next_usage, hmap_node, &p->ofport_usage) {
1338 hmap_remove(&p->ofport_usage, &usage->hmap_node);
1342 p->ofproto_class->destruct(p);
1343 ofproto_destroy__(p);
1346 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1347 * kernel datapath, for example, this destroys the datapath in the kernel, and
1348 * with the netdev-based datapath, it tears down the data structures that
1349 * represent the datapath.
1351 * The datapath should not be currently open as an ofproto. */
1353 ofproto_delete(const char *name, const char *type)
1355 const struct ofproto_class *class = ofproto_class_find__(type);
1356 return (!class ? EAFNOSUPPORT
1357 : !class->del ? EACCES
1358 : class->del(type, name));
1362 process_port_change(struct ofproto *ofproto, int error, char *devname)
1364 if (error == ENOBUFS) {
1365 reinit_ports(ofproto);
1366 } else if (!error) {
1367 update_port(ofproto, devname);
1373 ofproto_type_run(const char *datapath_type)
1375 const struct ofproto_class *class;
1378 datapath_type = ofproto_normalize_type(datapath_type);
1379 class = ofproto_class_find__(datapath_type);
1381 error = class->type_run ? class->type_run(datapath_type) : 0;
1382 if (error && error != EAGAIN) {
1383 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1384 datapath_type, ovs_strerror(error));
1390 ofproto_type_run_fast(const char *datapath_type)
1392 const struct ofproto_class *class;
1395 datapath_type = ofproto_normalize_type(datapath_type);
1396 class = ofproto_class_find__(datapath_type);
1398 error = class->type_run_fast ? class->type_run_fast(datapath_type) : 0;
1399 if (error && error != EAGAIN) {
1400 VLOG_ERR_RL(&rl, "%s: type_run_fast failed (%s)",
1401 datapath_type, ovs_strerror(error));
1407 ofproto_type_wait(const char *datapath_type)
1409 const struct ofproto_class *class;
1411 datapath_type = ofproto_normalize_type(datapath_type);
1412 class = ofproto_class_find__(datapath_type);
1414 if (class->type_wait) {
1415 class->type_wait(datapath_type);
1420 any_pending_ops(const struct ofproto *p)
1421 OVS_EXCLUDED(ofproto_mutex)
1425 ovs_mutex_lock(&ofproto_mutex);
1426 b = !list_is_empty(&p->pending);
1427 ovs_mutex_unlock(&ofproto_mutex);
1433 ofproto_run(struct ofproto *p)
1435 struct sset changed_netdevs;
1436 const char *changed_netdev;
1437 struct ofport *ofport;
1440 error = p->ofproto_class->run(p);
1441 if (error && error != EAGAIN) {
1442 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1445 run_rule_executes(p);
1447 /* Restore the eviction group heap invariant occasionally. */
1448 if (p->eviction_group_timer < time_msec()) {
1451 p->eviction_group_timer = time_msec() + 1000;
1453 for (i = 0; i < p->n_tables; i++) {
1454 struct oftable *table = &p->tables[i];
1455 struct eviction_group *evg;
1456 struct cls_cursor cursor;
1459 if (!table->eviction_fields) {
1463 ovs_mutex_lock(&ofproto_mutex);
1464 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1465 heap_rebuild(&evg->rules);
1468 ovs_rwlock_rdlock(&table->cls.rwlock);
1469 cls_cursor_init(&cursor, &table->cls, NULL);
1470 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1471 if (!rule->eviction_group
1472 && (rule->idle_timeout || rule->hard_timeout)) {
1473 eviction_group_add_rule(rule);
1476 ovs_rwlock_unlock(&table->cls.rwlock);
1477 ovs_mutex_unlock(&ofproto_mutex);
1481 if (p->ofproto_class->port_poll) {
1484 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1485 process_port_change(p, error, devname);
1489 /* Update OpenFlow port status for any port whose netdev has changed.
1491 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1492 * destroyed, so it's not safe to update ports directly from the
1493 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1494 * need this two-phase approach. */
1495 sset_init(&changed_netdevs);
1496 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1497 unsigned int change_seq = netdev_change_seq(ofport->netdev);
1498 if (ofport->change_seq != change_seq) {
1499 ofport->change_seq = change_seq;
1500 sset_add(&changed_netdevs, netdev_get_name(ofport->netdev));
1503 SSET_FOR_EACH (changed_netdev, &changed_netdevs) {
1504 update_port(p, changed_netdev);
1506 sset_destroy(&changed_netdevs);
1510 connmgr_run(p->connmgr, handle_openflow);
1514 connmgr_run(p->connmgr, NULL);
1516 if (!any_pending_ops(p)) {
1517 p->state = S_OPENFLOW;
1522 connmgr_run(p->connmgr, NULL);
1524 if (!any_pending_ops(p)) {
1525 connmgr_flushed(p->connmgr);
1526 p->state = S_OPENFLOW;
1534 if (time_msec() >= p->next_op_report) {
1535 long long int ago = (time_msec() - p->first_op) / 1000;
1536 long long int interval = (p->last_op - p->first_op) / 1000;
1540 ds_put_format(&s, "%d flow_mods ",
1541 p->n_add + p->n_delete + p->n_modify);
1542 if (interval == ago) {
1543 ds_put_format(&s, "in the last %lld s", ago);
1544 } else if (interval) {
1545 ds_put_format(&s, "in the %lld s starting %lld s ago",
1548 ds_put_format(&s, "%lld s ago", ago);
1551 ds_put_cstr(&s, " (");
1553 ds_put_format(&s, "%d adds, ", p->n_add);
1556 ds_put_format(&s, "%d deletes, ", p->n_delete);
1559 ds_put_format(&s, "%d modifications, ", p->n_modify);
1562 ds_put_char(&s, ')');
1564 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1567 p->n_add = p->n_delete = p->n_modify = 0;
1568 p->next_op_report = LLONG_MAX;
1574 /* Performs periodic activity required by 'ofproto' that needs to be done
1575 * with the least possible latency.
1577 * It makes sense to call this function a couple of times per poll loop, to
1578 * provide a significant performance boost on some benchmarks with the
1579 * ofproto-dpif implementation. */
1581 ofproto_run_fast(struct ofproto *p)
1585 error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0;
1586 if (error && error != EAGAIN) {
1587 VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)",
1588 p->name, ovs_strerror(error));
1594 ofproto_wait(struct ofproto *p)
1596 struct ofport *ofport;
1598 p->ofproto_class->wait(p);
1599 if (p->ofproto_class->port_poll_wait) {
1600 p->ofproto_class->port_poll_wait(p);
1603 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1604 if (ofport->change_seq != netdev_change_seq(ofport->netdev)) {
1605 poll_immediate_wake();
1611 connmgr_wait(p->connmgr, true);
1616 connmgr_wait(p->connmgr, false);
1617 if (!any_pending_ops(p)) {
1618 poll_immediate_wake();
1625 ofproto_is_alive(const struct ofproto *p)
1627 return connmgr_has_controllers(p->connmgr);
1630 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1631 * memory_report(). */
1633 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1635 const struct oftable *table;
1636 unsigned int n_rules;
1638 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1640 ovs_mutex_lock(&ofproto_mutex);
1641 simap_increase(usage, "ops",
1642 ofproto->n_pending + hmap_count(&ofproto->deletions));
1643 ovs_mutex_unlock(&ofproto_mutex);
1646 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1647 ovs_rwlock_rdlock(&table->cls.rwlock);
1648 n_rules += classifier_count(&table->cls);
1649 ovs_rwlock_unlock(&table->cls.rwlock);
1651 simap_increase(usage, "rules", n_rules);
1653 if (ofproto->ofproto_class->get_memory_usage) {
1654 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1657 connmgr_get_memory_usage(ofproto->connmgr, usage);
1661 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1664 connmgr_get_controller_info(ofproto->connmgr, info);
1668 ofproto_free_ofproto_controller_info(struct shash *info)
1670 connmgr_free_controller_info(info);
1673 /* Makes a deep copy of 'old' into 'port'. */
1675 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1677 port->name = xstrdup(old->name);
1678 port->type = xstrdup(old->type);
1679 port->ofp_port = old->ofp_port;
1682 /* Frees memory allocated to members of 'ofproto_port'.
1684 * Do not call this function on an ofproto_port obtained from
1685 * ofproto_port_dump_next(): that function retains ownership of the data in the
1688 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1690 free(ofproto_port->name);
1691 free(ofproto_port->type);
1694 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1696 * This function provides no status indication. An error status for the entire
1697 * dump operation is provided when it is completed by calling
1698 * ofproto_port_dump_done().
1701 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1702 const struct ofproto *ofproto)
1704 dump->ofproto = ofproto;
1705 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1709 /* Attempts to retrieve another port from 'dump', which must have been created
1710 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1711 * 'port' and returns true. On failure, returns false.
1713 * Failure might indicate an actual error or merely that the last port has been
1714 * dumped. An error status for the entire dump operation is provided when it
1715 * is completed by calling ofproto_port_dump_done().
1717 * The ofproto owns the data stored in 'port'. It will remain valid until at
1718 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1719 * ofproto_port_dump_done(). */
1721 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1722 struct ofproto_port *port)
1724 const struct ofproto *ofproto = dump->ofproto;
1730 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1733 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1739 /* Completes port table dump operation 'dump', which must have been created
1740 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1741 * error-free, otherwise a positive errno value describing the problem. */
1743 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1745 const struct ofproto *ofproto = dump->ofproto;
1747 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1750 return dump->error == EOF ? 0 : dump->error;
1753 /* Returns the type to pass to netdev_open() when a datapath of type
1754 * 'datapath_type' has a port of type 'port_type', for a few special
1755 * cases when a netdev type differs from a port type. For example, when
1756 * using the userspace datapath, a port of type "internal" needs to be
1759 * Returns either 'type' itself or a string literal, which must not be
1762 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1764 const struct ofproto_class *class;
1766 datapath_type = ofproto_normalize_type(datapath_type);
1767 class = ofproto_class_find__(datapath_type);
1772 return (class->port_open_type
1773 ? class->port_open_type(datapath_type, port_type)
1777 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1778 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1779 * the port's OpenFlow port number.
1781 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1782 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1783 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1784 * 'ofp_portp' is non-null). */
1786 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1787 ofp_port_t *ofp_portp)
1789 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1792 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1794 const char *netdev_name = netdev_get_name(netdev);
1796 simap_put(&ofproto->ofp_requests, netdev_name,
1797 ofp_to_u16(ofp_port));
1798 update_port(ofproto, netdev_name);
1801 *ofp_portp = OFPP_NONE;
1803 struct ofproto_port ofproto_port;
1805 error = ofproto_port_query_by_name(ofproto,
1806 netdev_get_name(netdev),
1809 *ofp_portp = ofproto_port.ofp_port;
1810 ofproto_port_destroy(&ofproto_port);
1817 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1818 * initializes '*port' appropriately; on failure, returns a positive errno
1821 * The caller owns the data in 'ofproto_port' and must free it with
1822 * ofproto_port_destroy() when it is no longer needed. */
1824 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1825 struct ofproto_port *port)
1829 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1831 memset(port, 0, sizeof *port);
1836 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1837 * Returns 0 if successful, otherwise a positive errno. */
1839 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1841 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1842 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1843 struct simap_node *ofp_request_node;
1846 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1847 if (ofp_request_node) {
1848 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1851 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1852 if (!error && ofport) {
1853 /* 'name' is the netdev's name and update_port() is going to close the
1854 * netdev. Just in case update_port() refers to 'name' after it
1855 * destroys 'ofport', make a copy of it around the update_port()
1857 char *devname = xstrdup(name);
1858 update_port(ofproto, devname);
1865 flow_mod_init(struct ofputil_flow_mod *fm,
1866 const struct match *match, unsigned int priority,
1867 const struct ofpact *ofpacts, size_t ofpacts_len,
1868 enum ofp_flow_mod_command command)
1870 memset(fm, 0, sizeof *fm);
1872 fm->priority = priority;
1875 fm->modify_cookie = false;
1877 fm->command = command;
1878 fm->idle_timeout = 0;
1879 fm->hard_timeout = 0;
1880 fm->buffer_id = UINT32_MAX;
1881 fm->out_port = OFPP_ANY;
1882 fm->out_group = OFPG_ANY;
1884 fm->ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1885 fm->ofpacts_len = ofpacts_len;
1889 simple_flow_mod(struct ofproto *ofproto,
1890 const struct match *match, unsigned int priority,
1891 const struct ofpact *ofpacts, size_t ofpacts_len,
1892 enum ofp_flow_mod_command command)
1894 struct ofputil_flow_mod fm;
1896 flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command);
1898 return handle_flow_mod__(ofproto, NULL, &fm, NULL);
1901 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1902 * performs the 'n_actions' actions in 'actions'. The new flow will not
1905 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1906 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1907 * controllers; otherwise, it will be hidden.
1909 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1911 * This is a helper function for in-band control and fail-open. */
1913 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1914 unsigned int priority,
1915 const struct ofpact *ofpacts, size_t ofpacts_len)
1916 OVS_EXCLUDED(ofproto_mutex)
1918 const struct rule *rule;
1921 /* First do a cheap check whether the rule we're looking for already exists
1922 * with the actions that we want. If it does, then we're done. */
1923 ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1924 rule = rule_from_cls_rule(classifier_find_match_exactly(
1925 &ofproto->tables[0].cls, match, priority));
1927 ovs_mutex_lock(&rule->mutex);
1928 must_add = !ofpacts_equal(rule->actions->ofpacts,
1929 rule->actions->ofpacts_len,
1930 ofpacts, ofpacts_len);
1931 ovs_mutex_unlock(&rule->mutex);
1935 ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1937 /* If there's no such rule or the rule doesn't have the actions we want,
1938 * fall back to a executing a full flow mod. We can't optimize this at
1939 * all because we didn't take enough locks above to ensure that the flow
1940 * table didn't already change beneath us. */
1942 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1943 OFPFC_MODIFY_STRICT);
1947 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1948 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1949 * operation cannot be initiated now but may be retried later.
1951 * This is a helper function for in-band control and fail-open and the "learn"
1954 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1955 OVS_EXCLUDED(ofproto_mutex)
1957 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1960 /* Searches for a rule with matching criteria exactly equal to 'target' in
1961 * ofproto's table 0 and, if it finds one, deletes it.
1963 * This is a helper function for in-band control and fail-open. */
1965 ofproto_delete_flow(struct ofproto *ofproto,
1966 const struct match *target, unsigned int priority)
1967 OVS_EXCLUDED(ofproto_mutex)
1969 struct classifier *cls = &ofproto->tables[0].cls;
1972 /* First do a cheap check whether the rule we're looking for has already
1973 * been deleted. If so, then we're done. */
1974 ovs_rwlock_rdlock(&cls->rwlock);
1975 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1977 ovs_rwlock_unlock(&cls->rwlock);
1982 /* Fall back to a executing a full flow mod. We can't optimize this at all
1983 * because we didn't take enough locks above to ensure that the flow table
1984 * didn't already change beneath us. */
1985 return simple_flow_mod(ofproto, target, priority, NULL, 0,
1986 OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE;
1989 /* Starts the process of deleting all of the flows from all of ofproto's flow
1990 * tables and then reintroducing the flows required by in-band control and
1991 * fail-open. The process will complete in a later call to ofproto_run(). */
1993 ofproto_flush_flows(struct ofproto *ofproto)
1995 COVERAGE_INC(ofproto_flush);
1996 ofproto->state = S_FLUSH;
2000 reinit_ports(struct ofproto *p)
2002 struct ofproto_port_dump dump;
2003 struct sset devnames;
2004 struct ofport *ofport;
2005 struct ofproto_port ofproto_port;
2006 const char *devname;
2008 COVERAGE_INC(ofproto_reinit_ports);
2010 sset_init(&devnames);
2011 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2012 sset_add(&devnames, netdev_get_name(ofport->netdev));
2014 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2015 sset_add(&devnames, ofproto_port.name);
2018 SSET_FOR_EACH (devname, &devnames) {
2019 update_port(p, devname);
2021 sset_destroy(&devnames);
2025 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
2029 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
2030 port_idx = port_idx ? port_idx : UINT16_MAX;
2032 if (port_idx >= ofproto->max_ports
2033 || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) {
2034 uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no;
2035 long long int last_used_at, lru = LLONG_MAX;
2037 /* Search for a free OpenFlow port number. We try not to
2038 * immediately reuse them to prevent problems due to old
2041 * We limit the automatically assigned port numbers to the lower half
2042 * of the port range, to reserve the upper half for assignment by
2045 if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) {
2046 ofproto->alloc_port_no = 1;
2048 last_used_at = ofport_get_usage(ofproto,
2049 u16_to_ofp(ofproto->alloc_port_no));
2050 if (!last_used_at) {
2051 port_idx = ofproto->alloc_port_no;
2053 } else if ( last_used_at < time_msec() - 60*60*1000) {
2054 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2055 * more than an hour ago, consider it usable. */
2056 ofport_remove_usage(ofproto,
2057 u16_to_ofp(ofproto->alloc_port_no));
2058 port_idx = ofproto->alloc_port_no;
2060 } else if (last_used_at < lru) {
2062 lru_ofport = ofproto->alloc_port_no;
2065 if (ofproto->alloc_port_no == end_port_no) {
2067 port_idx = lru_ofport;
2074 ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX);
2075 return u16_to_ofp(port_idx);
2079 dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port)
2081 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
2082 ofport_set_usage(ofproto, ofp_port, time_msec());
2086 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2087 * pointer if the netdev cannot be opened. On success, also fills in
2089 static struct netdev *
2090 ofport_open(struct ofproto *ofproto,
2091 struct ofproto_port *ofproto_port,
2092 struct ofputil_phy_port *pp)
2094 enum netdev_flags flags;
2095 struct netdev *netdev;
2098 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
2100 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
2101 "cannot be opened (%s)",
2103 ofproto_port->name, ofproto_port->ofp_port,
2104 ofproto_port->name, ovs_strerror(error));
2108 if (ofproto_port->ofp_port == OFPP_NONE) {
2109 if (!strcmp(ofproto->name, ofproto_port->name)) {
2110 ofproto_port->ofp_port = OFPP_LOCAL;
2112 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
2113 ofproto_port->name);
2116 pp->port_no = ofproto_port->ofp_port;
2117 netdev_get_etheraddr(netdev, pp->hw_addr);
2118 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
2119 netdev_get_flags(netdev, &flags);
2120 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
2121 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
2122 netdev_get_features(netdev, &pp->curr, &pp->advertised,
2123 &pp->supported, &pp->peer);
2124 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2125 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2130 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2131 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
2134 ofport_equal(const struct ofputil_phy_port *a,
2135 const struct ofputil_phy_port *b)
2137 return (eth_addr_equals(a->hw_addr, b->hw_addr)
2138 && a->state == b->state
2139 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
2140 && a->curr == b->curr
2141 && a->advertised == b->advertised
2142 && a->supported == b->supported
2143 && a->peer == b->peer
2144 && a->curr_speed == b->curr_speed
2145 && a->max_speed == b->max_speed);
2148 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2149 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2150 * one with the same name or port number). */
2152 ofport_install(struct ofproto *p,
2153 struct netdev *netdev, const struct ofputil_phy_port *pp)
2155 const char *netdev_name = netdev_get_name(netdev);
2156 struct ofport *ofport;
2159 /* Create ofport. */
2160 ofport = p->ofproto_class->port_alloc();
2165 ofport->ofproto = p;
2166 ofport->netdev = netdev;
2167 ofport->change_seq = netdev_change_seq(netdev);
2169 ofport->ofp_port = pp->port_no;
2170 ofport->created = time_msec();
2172 /* Add port to 'p'. */
2173 hmap_insert(&p->ports, &ofport->hmap_node,
2174 hash_ofp_port(ofport->ofp_port));
2175 shash_add(&p->port_by_name, netdev_name, ofport);
2177 update_mtu(p, ofport);
2179 /* Let the ofproto_class initialize its private data. */
2180 error = p->ofproto_class->port_construct(ofport);
2184 connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD);
2188 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
2189 p->name, netdev_name, ovs_strerror(error));
2191 ofport_destroy__(ofport);
2193 netdev_close(netdev);
2197 /* Removes 'ofport' from 'p' and destroys it. */
2199 ofport_remove(struct ofport *ofport)
2201 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp,
2203 ofport_destroy(ofport);
2206 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2209 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2211 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2213 ofport_remove(port);
2217 /* Updates 'port' with new 'pp' description.
2219 * Does not handle a name or port number change. The caller must implement
2220 * such a change as a delete followed by an add. */
2222 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
2224 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2225 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2226 | (pp->config & OFPUTIL_PC_PORT_DOWN));
2227 port->pp.state = pp->state;
2228 port->pp.curr = pp->curr;
2229 port->pp.advertised = pp->advertised;
2230 port->pp.supported = pp->supported;
2231 port->pp.peer = pp->peer;
2232 port->pp.curr_speed = pp->curr_speed;
2233 port->pp.max_speed = pp->max_speed;
2235 connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY);
2238 /* Update OpenFlow 'state' in 'port' and notify controller. */
2240 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2242 if (port->pp.state != state) {
2243 port->pp.state = state;
2244 connmgr_send_port_status(port->ofproto->connmgr, &port->pp,
2250 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2252 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2254 if (port->ofproto->ofproto_class->set_realdev) {
2255 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2257 if (port->ofproto->ofproto_class->set_stp_port) {
2258 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2260 if (port->ofproto->ofproto_class->set_cfm) {
2261 port->ofproto->ofproto_class->set_cfm(port, NULL);
2263 if (port->ofproto->ofproto_class->bundle_remove) {
2264 port->ofproto->ofproto_class->bundle_remove(port);
2270 ofport_destroy__(struct ofport *port)
2272 struct ofproto *ofproto = port->ofproto;
2273 const char *name = netdev_get_name(port->netdev);
2275 hmap_remove(&ofproto->ports, &port->hmap_node);
2276 shash_delete(&ofproto->port_by_name,
2277 shash_find(&ofproto->port_by_name, name));
2279 netdev_close(port->netdev);
2280 ofproto->ofproto_class->port_dealloc(port);
2284 ofport_destroy(struct ofport *port)
2287 dealloc_ofp_port(port->ofproto, port->ofp_port);
2288 port->ofproto->ofproto_class->port_destruct(port);
2289 ofport_destroy__(port);
2294 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2296 struct ofport *port;
2298 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2300 if (port->ofp_port == ofp_port) {
2307 static long long int
2308 ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port)
2310 struct ofport_usage *usage;
2312 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2313 &ofproto->ofport_usage) {
2314 if (usage->ofp_port == ofp_port) {
2315 return usage->last_used;
2322 ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port,
2323 long long int last_used)
2325 struct ofport_usage *usage;
2326 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2327 &ofproto->ofport_usage) {
2328 if (usage->ofp_port == ofp_port) {
2329 usage->last_used = last_used;
2333 ovs_assert(last_used == LLONG_MAX);
2335 usage = xmalloc(sizeof *usage);
2336 usage->ofp_port = ofp_port;
2337 usage->last_used = last_used;
2338 hmap_insert(&ofproto->ofport_usage, &usage->hmap_node,
2339 hash_ofp_port(ofp_port));
2343 ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port)
2345 struct ofport_usage *usage;
2346 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2347 &ofproto->ofport_usage) {
2348 if (usage->ofp_port == ofp_port) {
2349 hmap_remove(&ofproto->ofport_usage, &usage->hmap_node);
2357 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2359 struct ofproto *ofproto = port->ofproto;
2362 if (ofproto->ofproto_class->port_get_stats) {
2363 error = ofproto->ofproto_class->port_get_stats(port, stats);
2372 update_port(struct ofproto *ofproto, const char *name)
2374 struct ofproto_port ofproto_port;
2375 struct ofputil_phy_port pp;
2376 struct netdev *netdev;
2377 struct ofport *port;
2379 COVERAGE_INC(ofproto_update_port);
2381 /* Fetch 'name''s location and properties from the datapath. */
2382 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2383 ? ofport_open(ofproto, &ofproto_port, &pp)
2387 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2388 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2389 struct netdev *old_netdev = port->netdev;
2391 /* 'name' hasn't changed location. Any properties changed? */
2392 if (!ofport_equal(&port->pp, &pp)) {
2393 ofport_modified(port, &pp);
2396 update_mtu(ofproto, port);
2398 /* Install the newly opened netdev in case it has changed.
2399 * Don't close the old netdev yet in case port_modified has to
2400 * remove a retained reference to it.*/
2401 port->netdev = netdev;
2402 port->change_seq = netdev_change_seq(netdev);
2404 if (port->ofproto->ofproto_class->port_modified) {
2405 port->ofproto->ofproto_class->port_modified(port);
2408 netdev_close(old_netdev);
2410 /* If 'port' is nonnull then its name differs from 'name' and thus
2411 * we should delete it. If we think there's a port named 'name'
2412 * then its port number must be wrong now so delete it too. */
2414 ofport_remove(port);
2416 ofport_remove_with_name(ofproto, name);
2417 ofport_install(ofproto, netdev, &pp);
2420 /* Any port named 'name' is gone now. */
2421 ofport_remove_with_name(ofproto, name);
2423 ofproto_port_destroy(&ofproto_port);
2427 init_ports(struct ofproto *p)
2429 struct ofproto_port_dump dump;
2430 struct ofproto_port ofproto_port;
2431 struct shash_node *node, *next;
2433 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2434 const char *name = ofproto_port.name;
2436 if (shash_find(&p->port_by_name, name)) {
2437 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2440 struct ofputil_phy_port pp;
2441 struct netdev *netdev;
2443 /* Check if an OpenFlow port number had been requested. */
2444 node = shash_find(&init_ofp_ports, name);
2446 const struct iface_hint *iface_hint = node->data;
2447 simap_put(&p->ofp_requests, name,
2448 ofp_to_u16(iface_hint->ofp_port));
2451 netdev = ofport_open(p, &ofproto_port, &pp);
2453 ofport_install(p, netdev, &pp);
2454 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2455 p->alloc_port_no = MAX(p->alloc_port_no,
2456 ofp_to_u16(ofproto_port.ofp_port));
2462 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2463 struct iface_hint *iface_hint = node->data;
2465 if (!strcmp(iface_hint->br_name, p->name)) {
2466 free(iface_hint->br_name);
2467 free(iface_hint->br_type);
2469 shash_delete(&init_ofp_ports, node);
2476 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2477 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2479 find_min_mtu(struct ofproto *p)
2481 struct ofport *ofport;
2484 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2485 struct netdev *netdev = ofport->netdev;
2488 /* Skip any internal ports, since that's what we're trying to
2490 if (!strcmp(netdev_get_type(netdev), "internal")) {
2494 if (netdev_get_mtu(netdev, &dev_mtu)) {
2497 if (!mtu || dev_mtu < mtu) {
2502 return mtu ? mtu: ETH_PAYLOAD_MAX;
2505 /* Update MTU of all datapath devices on 'p' to the minimum of the
2506 * non-datapath ports in event of 'port' added or changed. */
2508 update_mtu(struct ofproto *p, struct ofport *port)
2510 struct ofport *ofport;
2511 struct netdev *netdev = port->netdev;
2512 int dev_mtu, old_min;
2514 if (netdev_get_mtu(netdev, &dev_mtu)) {
2518 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2519 if (dev_mtu > p->min_mtu) {
2520 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2521 dev_mtu = p->min_mtu;
2524 port->mtu = dev_mtu;
2528 /* For non-internal port find new min mtu. */
2529 old_min = p->min_mtu;
2530 port->mtu = dev_mtu;
2531 p->min_mtu = find_min_mtu(p);
2532 if (p->min_mtu == old_min) {
2536 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2537 struct netdev *netdev = ofport->netdev;
2539 if (!strcmp(netdev_get_type(netdev), "internal")) {
2540 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2541 ofport->mtu = p->min_mtu;
2548 ofproto_rule_ref(struct rule *rule)
2553 atomic_add(&rule->ref_count, 1, &orig);
2554 ovs_assert(orig != 0);
2559 ofproto_rule_unref(struct rule *rule)
2564 atomic_sub(&rule->ref_count, 1, &orig);
2566 rule->ofproto->ofproto_class->rule_destruct(rule);
2567 ofproto_rule_destroy__(rule);
2569 ovs_assert(orig != 0);
2574 struct rule_actions *
2575 rule_get_actions(const struct rule *rule)
2576 OVS_EXCLUDED(rule->mutex)
2578 struct rule_actions *actions;
2580 ovs_mutex_lock(&rule->mutex);
2581 actions = rule_get_actions__(rule);
2582 ovs_mutex_unlock(&rule->mutex);
2587 struct rule_actions *
2588 rule_get_actions__(const struct rule *rule)
2589 OVS_REQUIRES(rule->mutex)
2591 rule_actions_ref(rule->actions);
2592 return rule->actions;
2596 ofproto_rule_destroy__(struct rule *rule)
2597 OVS_NO_THREAD_SAFETY_ANALYSIS
2599 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2600 rule_actions_unref(rule->actions);
2601 ovs_mutex_destroy(&rule->mutex);
2602 rule->ofproto->ofproto_class->rule_dealloc(rule);
2605 static uint32_t get_provider_meter_id(const struct ofproto *,
2606 uint32_t of_meter_id);
2608 /* Creates and returns a new 'struct rule_actions', with a ref_count of 1,
2609 * whose actions are a copy of from the 'ofpacts_len' bytes of 'ofpacts'. */
2610 struct rule_actions *
2611 rule_actions_create(const struct ofproto *ofproto,
2612 const struct ofpact *ofpacts, size_t ofpacts_len)
2614 struct rule_actions *actions;
2616 actions = xmalloc(sizeof *actions);
2617 atomic_init(&actions->ref_count, 1);
2618 actions->ofpacts = xmemdup(ofpacts, ofpacts_len);
2619 actions->ofpacts_len = ofpacts_len;
2620 actions->provider_meter_id
2621 = get_provider_meter_id(ofproto,
2622 ofpacts_get_meter(ofpacts, ofpacts_len));
2627 /* Increments 'actions''s ref_count. */
2629 rule_actions_ref(struct rule_actions *actions)
2634 atomic_add(&actions->ref_count, 1, &orig);
2635 ovs_assert(orig != 0);
2639 /* Decrements 'actions''s ref_count and frees 'actions' if the ref_count
2642 rule_actions_unref(struct rule_actions *actions)
2647 atomic_sub(&actions->ref_count, 1, &orig);
2649 free(actions->ofpacts);
2652 ovs_assert(orig != 0);
2657 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2658 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2660 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2661 OVS_REQUIRES(ofproto_mutex)
2663 return (port == OFPP_ANY
2664 || ofpacts_output_to_port(rule->actions->ofpacts,
2665 rule->actions->ofpacts_len, port));
2668 /* Returns true if 'rule' has group and equals group_id. */
2670 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2671 OVS_REQUIRES(ofproto_mutex)
2673 return (group_id == OFPG11_ANY
2674 || ofpacts_output_to_group(rule->actions->ofpacts,
2675 rule->actions->ofpacts_len, group_id));
2678 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2679 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2681 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2682 OVS_REQUIRES(ofproto_mutex)
2684 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2689 case OFOPERATION_ADD:
2690 case OFOPERATION_DELETE:
2693 case OFOPERATION_MODIFY:
2694 case OFOPERATION_REPLACE:
2695 return ofpacts_output_to_port(op->actions->ofpacts,
2696 op->actions->ofpacts_len, out_port);
2703 rule_execute_destroy(struct rule_execute *e)
2705 ofproto_rule_unref(e->rule);
2706 list_remove(&e->list_node);
2710 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2711 * by passing them to the ofproto provider. */
2713 run_rule_executes(struct ofproto *ofproto)
2714 OVS_EXCLUDED(ofproto_mutex)
2716 struct rule_execute *e, *next;
2717 struct list executes;
2719 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2720 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2721 union flow_in_port in_port_;
2724 in_port_.ofp_port = e->in_port;
2725 flow_extract(e->packet, 0, 0, NULL, &in_port_, &flow);
2726 ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet);
2728 rule_execute_destroy(e);
2732 /* Destroys and discards all "rule_execute" operations queued up in
2733 * ofproto->rule_executes. */
2735 destroy_rule_executes(struct ofproto *ofproto)
2737 struct rule_execute *e, *next;
2738 struct list executes;
2740 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2741 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2742 ofpbuf_delete(e->packet);
2743 rule_execute_destroy(e);
2747 /* Returns true if 'rule' should be hidden from the controller.
2749 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2750 * (e.g. by in-band control) and are intentionally hidden from the
2753 ofproto_rule_is_hidden(const struct rule *rule)
2755 return rule->cr.priority > UINT16_MAX;
2758 static enum oftable_flags
2759 rule_get_flags(const struct rule *rule)
2761 return rule->ofproto->tables[rule->table_id].flags;
2765 rule_is_modifiable(const struct rule *rule)
2767 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2771 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2773 ofconn_send_reply(ofconn, make_echo_reply(oh));
2778 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2780 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2781 struct ofputil_switch_features features;
2782 struct ofport *port;
2788 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2790 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2792 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2793 * if present, are always at the end.) */
2794 n_tables = ofproto->n_tables;
2795 for (i = 0; i < ofproto->n_tables; i++) {
2796 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2802 features.datapath_id = ofproto->datapath_id;
2803 features.n_buffers = pktbuf_capacity();
2804 features.n_tables = n_tables;
2805 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2806 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2808 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2810 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2811 features.auxiliary_id = 0;
2812 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2814 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2815 ofputil_put_switch_features_port(&port->pp, b);
2818 ofconn_send_reply(ofconn, b);
2823 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2825 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2826 struct ofp_switch_config *osc;
2827 enum ofp_config_flags flags;
2831 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2832 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2833 flags = ofproto->frag_handling;
2834 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2835 if (oh->version < OFP13_VERSION
2836 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2837 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2839 osc->flags = htons(flags);
2840 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2841 ofconn_send_reply(ofconn, buf);
2847 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2849 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2850 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2851 uint16_t flags = ntohs(osc->flags);
2853 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2854 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2855 enum ofp_config_flags cur = ofproto->frag_handling;
2856 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2858 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2860 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2861 ofproto->frag_handling = next;
2863 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2865 ofputil_frag_handling_to_string(next));
2869 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2870 ofconn_set_invalid_ttl_to_controller(ofconn,
2871 (oh->version < OFP13_VERSION
2872 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2874 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2879 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2880 * error message code for the caller to propagate upward. Otherwise, returns
2883 * The log message mentions 'msg_type'. */
2885 reject_slave_controller(struct ofconn *ofconn)
2887 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2888 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2889 return OFPERR_OFPBRC_EPERM;
2895 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
2898 * - If they use a meter, then 'ofproto' has that meter configured.
2900 * - If they use any groups, then 'ofproto' has that group configured.
2902 * Returns 0 if successful, otherwise an OpenFlow error. */
2904 ofproto_check_ofpacts(struct ofproto *ofproto,
2905 const struct ofpact ofpacts[], size_t ofpacts_len)
2907 const struct ofpact *a;
2910 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2911 if (mid && get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2912 return OFPERR_OFPMMFC_INVALID_METER;
2915 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2916 if (a->type == OFPACT_GROUP
2917 && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) {
2918 return OFPERR_OFPBAC_BAD_OUT_GROUP;
2926 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2928 struct ofproto *p = ofconn_get_ofproto(ofconn);
2929 struct ofputil_packet_out po;
2930 struct ofpbuf *payload;
2931 uint64_t ofpacts_stub[1024 / 8];
2932 struct ofpbuf ofpacts;
2934 union flow_in_port in_port_;
2937 COVERAGE_INC(ofproto_packet_out);
2939 error = reject_slave_controller(ofconn);
2944 /* Decode message. */
2945 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2946 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2948 goto exit_free_ofpacts;
2950 if (ofp_to_u16(po.in_port) >= p->max_ports
2951 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2952 error = OFPERR_OFPBRC_BAD_PORT;
2953 goto exit_free_ofpacts;
2957 if (po.buffer_id != UINT32_MAX) {
2958 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2959 if (error || !payload) {
2960 goto exit_free_ofpacts;
2963 /* Ensure that the L3 header is 32-bit aligned. */
2964 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2967 /* Verify actions against packet, then send packet if successful. */
2968 in_port_.ofp_port = po.in_port;
2969 flow_extract(payload, 0, 0, NULL, &in_port_, &flow);
2970 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len);
2972 error = p->ofproto_class->packet_out(p, payload, &flow,
2973 po.ofpacts, po.ofpacts_len);
2975 ofpbuf_delete(payload);
2978 ofpbuf_uninit(&ofpacts);
2984 update_port_config(struct ofport *port,
2985 enum ofputil_port_config config,
2986 enum ofputil_port_config mask)
2988 enum ofputil_port_config old_config = port->pp.config;
2989 enum ofputil_port_config toggle;
2991 toggle = (config ^ port->pp.config) & mask;
2992 if (toggle & OFPUTIL_PC_PORT_DOWN) {
2993 if (config & OFPUTIL_PC_PORT_DOWN) {
2994 netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL);
2996 netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL);
2998 toggle &= ~OFPUTIL_PC_PORT_DOWN;
3001 port->pp.config ^= toggle;
3002 if (port->pp.config != old_config) {
3003 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
3008 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3010 struct ofproto *p = ofconn_get_ofproto(ofconn);
3011 struct ofputil_port_mod pm;
3012 struct ofport *port;
3015 error = reject_slave_controller(ofconn);
3020 error = ofputil_decode_port_mod(oh, &pm);
3025 port = ofproto_get_port(p, pm.port_no);
3027 return OFPERR_OFPPMFC_BAD_PORT;
3028 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
3029 return OFPERR_OFPPMFC_BAD_HW_ADDR;
3031 update_port_config(port, pm.config, pm.mask);
3033 netdev_set_advertisements(port->netdev, pm.advertise);
3040 handle_desc_stats_request(struct ofconn *ofconn,
3041 const struct ofp_header *request)
3043 static const char *default_mfr_desc = "Nicira, Inc.";
3044 static const char *default_hw_desc = "Open vSwitch";
3045 static const char *default_sw_desc = VERSION;
3046 static const char *default_serial_desc = "None";
3047 static const char *default_dp_desc = "None";
3049 struct ofproto *p = ofconn_get_ofproto(ofconn);
3050 struct ofp_desc_stats *ods;
3053 msg = ofpraw_alloc_stats_reply(request, 0);
3054 ods = ofpbuf_put_zeros(msg, sizeof *ods);
3055 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
3056 sizeof ods->mfr_desc);
3057 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
3058 sizeof ods->hw_desc);
3059 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
3060 sizeof ods->sw_desc);
3061 ovs_strlcpy(ods->serial_num,
3062 p->serial_desc ? p->serial_desc : default_serial_desc,
3063 sizeof ods->serial_num);
3064 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
3065 sizeof ods->dp_desc);
3066 ofconn_send_reply(ofconn, msg);
3072 handle_table_stats_request(struct ofconn *ofconn,
3073 const struct ofp_header *request)
3075 struct ofproto *p = ofconn_get_ofproto(ofconn);
3076 struct ofp12_table_stats *ots;
3081 /* Set up default values.
3083 * ofp12_table_stats is used as a generic structure as
3084 * it is able to hold all the fields for ofp10_table_stats
3085 * and ofp11_table_stats (and of course itself).
3087 ots = xcalloc(p->n_tables, sizeof *ots);
3088 for (i = 0; i < p->n_tables; i++) {
3089 ots[i].table_id = i;
3090 sprintf(ots[i].name, "table%"PRIuSIZE, i);
3091 ots[i].match = htonll(OFPXMT13_MASK);
3092 ots[i].wildcards = htonll(OFPXMT13_MASK);
3093 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
3094 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
3095 ots[i].write_setfields = htonll(OFPXMT13_MASK);
3096 ots[i].apply_setfields = htonll(OFPXMT13_MASK);
3097 ots[i].metadata_match = OVS_BE64_MAX;
3098 ots[i].metadata_write = OVS_BE64_MAX;
3099 ots[i].instructions = htonl(OFPIT11_ALL);
3100 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
3101 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
3102 ovs_rwlock_rdlock(&p->tables[i].cls.rwlock);
3103 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
3104 ovs_rwlock_unlock(&p->tables[i].cls.rwlock);
3107 p->ofproto_class->get_tables(p, ots);
3109 /* Post-process the tables, dropping hidden tables. */
3110 n_tables = p->n_tables;
3111 for (i = 0; i < p->n_tables; i++) {
3112 const struct oftable *table = &p->tables[i];
3114 if (table->flags & OFTABLE_HIDDEN) {
3120 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
3123 if (table->max_flows < ntohl(ots[i].max_entries)) {
3124 ots[i].max_entries = htonl(table->max_flows);
3128 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
3129 ofconn_send_reply(ofconn, msg);
3137 append_port_stat(struct ofport *port, struct list *replies)
3139 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
3141 calc_duration(port->created, time_msec(),
3142 &ops.duration_sec, &ops.duration_nsec);
3144 /* Intentionally ignore return value, since errors will set
3145 * 'stats' to all-1s, which is correct for OpenFlow, and
3146 * netdev_get_stats() will log errors. */
3147 ofproto_port_get_stats(port, &ops.stats);
3149 ofputil_append_port_stat(replies, &ops);
3153 handle_port_stats_request(struct ofconn *ofconn,
3154 const struct ofp_header *request)
3156 struct ofproto *p = ofconn_get_ofproto(ofconn);
3157 struct ofport *port;
3158 struct list replies;
3162 error = ofputil_decode_port_stats_request(request, &port_no);
3167 ofpmp_init(&replies, request);
3168 if (port_no != OFPP_ANY) {
3169 port = ofproto_get_port(p, port_no);
3171 append_port_stat(port, &replies);
3174 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3175 append_port_stat(port, &replies);
3179 ofconn_send_replies(ofconn, &replies);
3184 handle_port_desc_stats_request(struct ofconn *ofconn,
3185 const struct ofp_header *request)
3187 struct ofproto *p = ofconn_get_ofproto(ofconn);
3188 enum ofp_version version;
3189 struct ofport *port;
3190 struct list replies;
3192 ofpmp_init(&replies, request);
3194 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
3195 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3196 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
3199 ofconn_send_replies(ofconn, &replies);
3204 hash_cookie(ovs_be64 cookie)
3206 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
3207 (OVS_FORCE uint64_t)cookie);
3211 cookies_insert(struct ofproto *ofproto, struct rule *rule)
3212 OVS_REQUIRES(ofproto_mutex)
3214 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3215 hash_cookie(rule->flow_cookie));
3219 cookies_remove(struct ofproto *ofproto, struct rule *rule)
3220 OVS_REQUIRES(ofproto_mutex)
3222 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3226 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
3227 ovs_be64 new_cookie)
3228 OVS_REQUIRES(ofproto_mutex)
3230 if (new_cookie != rule->flow_cookie) {
3231 cookies_remove(ofproto, rule);
3233 ovs_mutex_lock(&rule->mutex);
3234 rule->flow_cookie = new_cookie;
3235 ovs_mutex_unlock(&rule->mutex);
3237 cookies_insert(ofproto, rule);
3242 calc_duration(long long int start, long long int now,
3243 uint32_t *sec, uint32_t *nsec)
3245 long long int msecs = now - start;
3246 *sec = msecs / 1000;
3247 *nsec = (msecs % 1000) * (1000 * 1000);
3250 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3251 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
3253 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3255 return (table_id == 0xff || table_id < ofproto->n_tables
3257 : OFPERR_OFPBRC_BAD_TABLE_ID);
3261 static struct oftable *
3262 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
3264 struct oftable *table;
3266 for (table = &ofproto->tables[table_id];
3267 table < &ofproto->tables[ofproto->n_tables];
3269 if (!(table->flags & OFTABLE_HIDDEN)) {
3277 static struct oftable *
3278 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
3280 if (table_id == 0xff) {
3281 return next_visible_table(ofproto, 0);
3282 } else if (table_id < ofproto->n_tables) {
3283 return &ofproto->tables[table_id];
3289 static struct oftable *
3290 next_matching_table(const struct ofproto *ofproto,
3291 const struct oftable *table, uint8_t table_id)
3293 return (table_id == 0xff
3294 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
3298 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3300 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3301 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3303 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3304 * only once, for that table. (This can be used to access tables marked
3307 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3308 * entered at all. (Perhaps you should have validated TABLE_ID with
3309 * check_table_id().)
3311 * All parameters are evaluated multiple times.
3313 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3314 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3316 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3318 /* Initializes 'criteria' in a straightforward way based on the other
3321 * For "loose" matching, the 'priority' parameter is unimportant and may be
3324 rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
3325 const struct match *match, unsigned int priority,
3326 ovs_be64 cookie, ovs_be64 cookie_mask,
3327 ofp_port_t out_port, uint32_t out_group)
3329 criteria->table_id = table_id;
3330 cls_rule_init(&criteria->cr, match, priority);
3331 criteria->cookie = cookie;
3332 criteria->cookie_mask = cookie_mask;
3333 criteria->out_port = out_port;
3334 criteria->out_group = out_group;
3338 rule_criteria_destroy(struct rule_criteria *criteria)
3340 cls_rule_destroy(&criteria->cr);
3344 rule_collection_init(struct rule_collection *rules)
3346 rules->rules = rules->stub;
3348 rules->capacity = ARRAY_SIZE(rules->stub);
3352 rule_collection_add(struct rule_collection *rules, struct rule *rule)
3354 if (rules->n >= rules->capacity) {
3355 size_t old_size, new_size;
3357 old_size = rules->capacity * sizeof *rules->rules;
3358 rules->capacity *= 2;
3359 new_size = rules->capacity * sizeof *rules->rules;
3361 if (rules->rules == rules->stub) {
3362 rules->rules = xmalloc(new_size);
3363 memcpy(rules->rules, rules->stub, old_size);
3365 rules->rules = xrealloc(rules->rules, new_size);
3369 rules->rules[rules->n++] = rule;
3373 rule_collection_ref(struct rule_collection *rules)
3374 OVS_REQUIRES(ofproto_mutex)
3378 for (i = 0; i < rules->n; i++) {
3379 ofproto_rule_ref(rules->rules[i]);
3384 rule_collection_unref(struct rule_collection *rules)
3388 for (i = 0; i < rules->n; i++) {
3389 ofproto_rule_unref(rules->rules[i]);
3394 rule_collection_destroy(struct rule_collection *rules)
3396 if (rules->rules != rules->stub) {
3402 collect_rule(struct rule *rule, const struct rule_criteria *c,
3403 struct rule_collection *rules)
3404 OVS_REQUIRES(ofproto_mutex)
3406 /* We ordinarily want to skip hidden rules, but there has to be a way for
3407 * code internal to OVS to modify and delete them, so if the criteria
3408 * specify a priority that can only be for a hidden flow, then allow hidden
3409 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
3410 * with hidden flows because OpenFlow uses only a 16-bit field to specify
3412 if (ofproto_rule_is_hidden(rule) && c->cr.priority <= UINT16_MAX) {
3414 } else if (rule->pending) {
3415 return OFPROTO_POSTPONE;
3417 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3418 && ofproto_rule_has_out_port(rule, c->out_port)
3419 && ofproto_rule_has_out_group(rule, c->out_group)
3420 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)) {
3421 rule_collection_add(rules, rule);
3427 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3428 * on classifiers rules are done in the "loose" way required for OpenFlow
3429 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3432 * Hidden rules are always omitted.
3434 * Returns 0 on success, otherwise an OpenFlow error code. */
3436 collect_rules_loose(struct ofproto *ofproto,
3437 const struct rule_criteria *criteria,
3438 struct rule_collection *rules)
3439 OVS_REQUIRES(ofproto_mutex)
3441 struct oftable *table;
3444 rule_collection_init(rules);
3446 error = check_table_id(ofproto, criteria->table_id);
3451 if (criteria->cookie_mask == OVS_BE64_MAX) {
3454 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3455 hash_cookie(criteria->cookie),
3456 &ofproto->cookies) {
3457 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
3458 error = collect_rule(rule, criteria, rules);
3465 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3466 struct cls_cursor cursor;
3469 ovs_rwlock_rdlock(&table->cls.rwlock);
3470 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
3471 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3472 error = collect_rule(rule, criteria, rules);
3477 ovs_rwlock_unlock(&table->cls.rwlock);
3483 rule_collection_destroy(rules);
3488 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3489 * on classifiers rules are done in the "strict" way required for OpenFlow
3490 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3491 * rules on list 'rules'.
3493 * Hidden rules are always omitted.
3495 * Returns 0 on success, otherwise an OpenFlow error code. */
3497 collect_rules_strict(struct ofproto *ofproto,
3498 const struct rule_criteria *criteria,
3499 struct rule_collection *rules)
3500 OVS_REQUIRES(ofproto_mutex)
3502 struct oftable *table;
3505 rule_collection_init(rules);
3507 error = check_table_id(ofproto, criteria->table_id);
3512 if (criteria->cookie_mask == OVS_BE64_MAX) {
3515 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3516 hash_cookie(criteria->cookie),
3517 &ofproto->cookies) {
3518 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
3519 error = collect_rule(rule, criteria, rules);
3526 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3529 ovs_rwlock_rdlock(&table->cls.rwlock);
3530 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3531 &table->cls, &criteria->cr));
3532 ovs_rwlock_unlock(&table->cls.rwlock);
3534 error = collect_rule(rule, criteria, rules);
3544 rule_collection_destroy(rules);
3549 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3550 * forced into the range of a uint16_t. */
3552 age_secs(long long int age_ms)
3554 return (age_ms < 0 ? 0
3555 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3556 : (unsigned int) age_ms / 1000);
3560 handle_flow_stats_request(struct ofconn *ofconn,
3561 const struct ofp_header *request)
3562 OVS_EXCLUDED(ofproto_mutex)
3564 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3565 struct ofputil_flow_stats_request fsr;
3566 struct rule_criteria criteria;
3567 struct rule_collection rules;
3568 struct list replies;
3572 error = ofputil_decode_flow_stats_request(&fsr, request);
3577 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3578 fsr.cookie_mask, fsr.out_port, fsr.out_group);
3580 ovs_mutex_lock(&ofproto_mutex);
3581 error = collect_rules_loose(ofproto, &criteria, &rules);
3582 rule_criteria_destroy(&criteria);
3584 rule_collection_ref(&rules);
3586 ovs_mutex_unlock(&ofproto_mutex);
3592 ofpmp_init(&replies, request);
3593 for (i = 0; i < rules.n; i++) {
3594 struct rule *rule = rules.rules[i];
3595 long long int now = time_msec();
3596 struct ofputil_flow_stats fs;
3597 long long int created, used, modified;
3598 struct rule_actions *actions;
3599 enum ofputil_flow_mod_flags flags;
3601 ovs_mutex_lock(&rule->mutex);
3602 fs.cookie = rule->flow_cookie;
3603 fs.idle_timeout = rule->idle_timeout;
3604 fs.hard_timeout = rule->hard_timeout;
3605 created = rule->created;
3607 modified = rule->modified;
3608 actions = rule_get_actions__(rule);
3609 flags = rule->flags;
3610 ovs_mutex_unlock(&rule->mutex);
3612 minimatch_expand(&rule->cr.match, &fs.match);
3613 fs.table_id = rule->table_id;
3614 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
3615 fs.priority = rule->cr.priority;
3616 fs.idle_age = age_secs(now - used);
3617 fs.hard_age = age_secs(now - modified);
3618 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3620 fs.ofpacts = actions->ofpacts;
3621 fs.ofpacts_len = actions->ofpacts_len;
3624 ofputil_append_flow_stats_reply(&fs, &replies);
3626 rule_actions_unref(actions);
3629 rule_collection_unref(&rules);
3630 rule_collection_destroy(&rules);
3632 ofconn_send_replies(ofconn, &replies);
3638 flow_stats_ds(struct rule *rule, struct ds *results)
3640 uint64_t packet_count, byte_count;
3641 struct rule_actions *actions;
3642 long long int created;
3644 rule->ofproto->ofproto_class->rule_get_stats(rule,
3645 &packet_count, &byte_count);
3647 ovs_mutex_lock(&rule->mutex);
3648 actions = rule_get_actions__(rule);
3649 created = rule->created;
3650 ovs_mutex_unlock(&rule->mutex);
3652 if (rule->table_id != 0) {
3653 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3655 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
3656 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3657 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3658 cls_rule_format(&rule->cr, results);
3659 ds_put_char(results, ',');
3661 ds_put_cstr(results, "actions=");
3662 ofpacts_format(actions->ofpacts, actions->ofpacts_len, results);
3664 ds_put_cstr(results, "\n");
3666 rule_actions_unref(actions);
3669 /* Adds a pretty-printed description of all flows to 'results', including
3670 * hidden flows (e.g., set up by in-band control). */
3672 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3674 struct oftable *table;
3676 OFPROTO_FOR_EACH_TABLE (table, p) {
3677 struct cls_cursor cursor;
3680 ovs_rwlock_rdlock(&table->cls.rwlock);
3681 cls_cursor_init(&cursor, &table->cls, NULL);
3682 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3683 flow_stats_ds(rule, results);
3685 ovs_rwlock_unlock(&table->cls.rwlock);
3689 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3690 * '*engine_type' and '*engine_id', respectively. */
3692 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3693 uint8_t *engine_type, uint8_t *engine_id)
3695 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3698 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3699 * true if the port's CFM status was successfully stored into '*status'.
3700 * Returns false if the port did not have CFM configured, in which case
3701 * '*status' is indeterminate.
3703 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3705 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3706 struct ofproto_cfm_status *status)
3708 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3710 && ofproto->ofproto_class->get_cfm_status
3711 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3715 handle_aggregate_stats_request(struct ofconn *ofconn,
3716 const struct ofp_header *oh)
3717 OVS_EXCLUDED(ofproto_mutex)
3719 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3720 struct ofputil_flow_stats_request request;
3721 struct ofputil_aggregate_stats stats;
3722 bool unknown_packets, unknown_bytes;
3723 struct rule_criteria criteria;
3724 struct rule_collection rules;
3725 struct ofpbuf *reply;
3729 error = ofputil_decode_flow_stats_request(&request, oh);
3734 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3735 request.cookie, request.cookie_mask,
3736 request.out_port, request.out_group);
3738 ovs_mutex_lock(&ofproto_mutex);
3739 error = collect_rules_loose(ofproto, &criteria, &rules);
3740 rule_criteria_destroy(&criteria);
3742 rule_collection_ref(&rules);
3744 ovs_mutex_unlock(&ofproto_mutex);
3750 memset(&stats, 0, sizeof stats);
3751 unknown_packets = unknown_bytes = false;
3752 for (i = 0; i < rules.n; i++) {
3753 struct rule *rule = rules.rules[i];
3754 uint64_t packet_count;
3755 uint64_t byte_count;
3757 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3760 if (packet_count == UINT64_MAX) {
3761 unknown_packets = true;
3763 stats.packet_count += packet_count;
3766 if (byte_count == UINT64_MAX) {
3767 unknown_bytes = true;
3769 stats.byte_count += byte_count;
3774 if (unknown_packets) {
3775 stats.packet_count = UINT64_MAX;
3777 if (unknown_bytes) {
3778 stats.byte_count = UINT64_MAX;
3781 rule_collection_unref(&rules);
3782 rule_collection_destroy(&rules);
3784 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3785 ofconn_send_reply(ofconn, reply);
3790 struct queue_stats_cbdata {
3791 struct ofport *ofport;
3792 struct list replies;
3797 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3798 const struct netdev_queue_stats *stats)
3800 struct ofputil_queue_stats oqs;
3802 oqs.port_no = cbdata->ofport->pp.port_no;
3803 oqs.queue_id = queue_id;
3804 oqs.tx_bytes = stats->tx_bytes;
3805 oqs.tx_packets = stats->tx_packets;
3806 oqs.tx_errors = stats->tx_errors;
3807 if (stats->created != LLONG_MIN) {
3808 calc_duration(stats->created, cbdata->now,
3809 &oqs.duration_sec, &oqs.duration_nsec);
3811 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3813 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3817 handle_queue_stats_dump_cb(uint32_t queue_id,
3818 struct netdev_queue_stats *stats,
3821 struct queue_stats_cbdata *cbdata = cbdata_;
3823 put_queue_stats(cbdata, queue_id, stats);
3827 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3828 struct queue_stats_cbdata *cbdata)
3830 cbdata->ofport = port;
3831 if (queue_id == OFPQ_ALL) {
3832 netdev_dump_queue_stats(port->netdev,
3833 handle_queue_stats_dump_cb, cbdata);
3835 struct netdev_queue_stats stats;
3837 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3838 put_queue_stats(cbdata, queue_id, &stats);
3840 return OFPERR_OFPQOFC_BAD_QUEUE;
3847 handle_queue_stats_request(struct ofconn *ofconn,
3848 const struct ofp_header *rq)
3850 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3851 struct queue_stats_cbdata cbdata;
3852 struct ofport *port;
3854 struct ofputil_queue_stats_request oqsr;
3856 COVERAGE_INC(ofproto_queue_req);
3858 ofpmp_init(&cbdata.replies, rq);
3859 cbdata.now = time_msec();
3861 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3866 if (oqsr.port_no == OFPP_ANY) {
3867 error = OFPERR_OFPQOFC_BAD_QUEUE;
3868 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3869 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3874 port = ofproto_get_port(ofproto, oqsr.port_no);
3876 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3877 : OFPERR_OFPQOFC_BAD_PORT);
3880 ofconn_send_replies(ofconn, &cbdata.replies);
3882 ofpbuf_list_delete(&cbdata.replies);
3889 is_flow_deletion_pending(const struct ofproto *ofproto,
3890 const struct cls_rule *cls_rule,
3892 OVS_REQUIRES(ofproto_mutex)
3894 if (!hmap_is_empty(&ofproto->deletions)) {
3895 struct ofoperation *op;
3897 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3898 cls_rule_hash(cls_rule, table_id),
3899 &ofproto->deletions) {
3900 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3910 should_evict_a_rule(struct oftable *table, unsigned int extra_space)
3911 OVS_REQUIRES(ofproto_mutex)
3912 OVS_NO_THREAD_SAFETY_ANALYSIS
3914 return classifier_count(&table->cls) + extra_space > table->max_flows;
3918 evict_rules_from_table(struct ofproto *ofproto, struct oftable *table,
3919 unsigned int extra_space)
3920 OVS_REQUIRES(ofproto_mutex)
3922 while (should_evict_a_rule(table, extra_space)) {
3925 if (!choose_rule_to_evict(table, &rule)) {
3926 return OFPERR_OFPFMFC_TABLE_FULL;
3927 } else if (rule->pending) {
3928 return OFPROTO_POSTPONE;
3930 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
3931 delete_flow__(rule, group, OFPRR_EVICTION);
3932 ofopgroup_submit(group);
3939 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3940 * in which no matching flow already exists in the flow table.
3942 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3943 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3944 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3945 * initiated now but may be retried later.
3947 * The caller retains ownership of 'fm->ofpacts'.
3949 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3952 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3953 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3954 OVS_REQUIRES(ofproto_mutex)
3956 struct oftable *table;
3957 struct ofopgroup *group;
3963 error = check_table_id(ofproto, fm->table_id);
3969 if (fm->table_id == 0xff) {
3970 if (ofproto->ofproto_class->rule_choose_table) {
3971 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3977 ovs_assert(table_id < ofproto->n_tables);
3981 } else if (fm->table_id < ofproto->n_tables) {
3982 table_id = fm->table_id;
3984 return OFPERR_OFPBRC_BAD_TABLE_ID;
3987 table = &ofproto->tables[table_id];
3989 if (table->flags & OFTABLE_READONLY) {
3990 return OFPERR_OFPBRC_EPERM;
3993 cls_rule_init(&cr, &fm->match, fm->priority);
3995 /* Transform "add" into "modify" if there's an existing identical flow. */
3996 ovs_rwlock_rdlock(&table->cls.rwlock);
3997 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
3998 ovs_rwlock_unlock(&table->cls.rwlock);
4000 cls_rule_destroy(&cr);
4001 if (!rule_is_modifiable(rule)) {
4002 return OFPERR_OFPBRC_EPERM;
4003 } else if (rule->pending) {
4004 return OFPROTO_POSTPONE;
4006 struct rule_collection rules;
4008 rule_collection_init(&rules);
4009 rule_collection_add(&rules, rule);
4010 fm->modify_cookie = true;
4011 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4012 rule_collection_destroy(&rules);
4018 /* Serialize against pending deletion. */
4019 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
4020 cls_rule_destroy(&cr);
4021 return OFPROTO_POSTPONE;
4024 /* Check for overlap, if requested. */
4025 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
4028 ovs_rwlock_rdlock(&table->cls.rwlock);
4029 overlaps = classifier_rule_overlaps(&table->cls, &cr);
4030 ovs_rwlock_unlock(&table->cls.rwlock);
4033 cls_rule_destroy(&cr);
4034 return OFPERR_OFPFMFC_OVERLAP;
4038 /* If necessary, evict an existing rule to clear out space. */
4039 error = evict_rules_from_table(ofproto, table, 1);
4041 cls_rule_destroy(&cr);
4045 /* Allocate new rule. */
4046 rule = ofproto->ofproto_class->rule_alloc();
4048 cls_rule_destroy(&cr);
4049 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
4050 ofproto->name, ovs_strerror(error));
4054 /* Initialize base state. */
4055 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4056 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
4057 atomic_init(&rule->ref_count, 1);
4058 rule->pending = NULL;
4059 rule->flow_cookie = fm->new_cookie;
4060 rule->created = rule->modified = rule->used = time_msec();
4062 ovs_mutex_init(&rule->mutex);
4063 ovs_mutex_lock(&rule->mutex);
4064 rule->idle_timeout = fm->idle_timeout;
4065 rule->hard_timeout = fm->hard_timeout;
4066 ovs_mutex_unlock(&rule->mutex);
4068 *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
4069 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4070 rule->actions = rule_actions_create(ofproto, fm->ofpacts, fm->ofpacts_len);
4071 list_init(&rule->meter_list_node);
4072 rule->eviction_group = NULL;
4073 list_init(&rule->expirable);
4074 rule->monitor_flags = 0;
4075 rule->add_seqno = 0;
4076 rule->modify_seqno = 0;
4078 /* Construct rule, initializing derived state. */
4079 error = ofproto->ofproto_class->rule_construct(rule);
4081 ofproto_rule_destroy__(rule);
4086 oftable_insert_rule(rule);
4088 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4089 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
4090 ofproto->ofproto_class->rule_insert(rule);
4091 ofopgroup_submit(group);
4096 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4098 /* Modifies the rules listed in 'rules', changing their actions to match those
4101 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4104 * Returns 0 on success, otherwise an OpenFlow error code. */
4106 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4107 struct ofputil_flow_mod *fm, const struct ofp_header *request,
4108 const struct rule_collection *rules)
4109 OVS_REQUIRES(ofproto_mutex)
4111 enum ofoperation_type type;
4112 struct ofopgroup *group;
4116 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
4117 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4118 error = OFPERR_OFPBRC_EPERM;
4119 for (i = 0; i < rules->n; i++) {
4120 struct rule *rule = rules->rules[i];
4121 struct ofoperation *op;
4122 bool actions_changed;
4123 bool reset_counters;
4125 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
4127 if (rule_is_modifiable(rule)) {
4128 /* At least one rule is modifiable, don't report EPERM error. */
4134 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
4135 rule->actions->ofpacts,
4136 rule->actions->ofpacts_len);
4138 op = ofoperation_create(group, rule, type, 0);
4140 if (fm->modify_cookie && fm->new_cookie != OVS_BE64_MAX) {
4141 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
4143 if (type == OFOPERATION_REPLACE) {
4144 ovs_mutex_lock(&rule->mutex);
4145 rule->idle_timeout = fm->idle_timeout;
4146 rule->hard_timeout = fm->hard_timeout;
4147 ovs_mutex_unlock(&rule->mutex);
4149 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4150 if (fm->idle_timeout || fm->hard_timeout) {
4151 if (!rule->eviction_group) {
4152 eviction_group_add_rule(rule);
4155 eviction_group_remove_rule(rule);
4159 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
4160 if (actions_changed || reset_counters) {
4161 struct rule_actions *new_actions;
4163 op->actions = rule->actions;
4164 new_actions = rule_actions_create(ofproto,
4165 fm->ofpacts, fm->ofpacts_len);
4167 ovs_mutex_lock(&rule->mutex);
4168 rule->actions = new_actions;
4169 ovs_mutex_unlock(&rule->mutex);
4171 rule->ofproto->ofproto_class->rule_modify_actions(rule,
4174 ofoperation_complete(op, 0);
4177 ofopgroup_submit(group);
4183 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
4184 struct ofputil_flow_mod *fm, const struct ofp_header *request)
4185 OVS_REQUIRES(ofproto_mutex)
4187 if (fm->cookie_mask != htonll(0) || fm->new_cookie == OVS_BE64_MAX) {
4190 return add_flow(ofproto, ofconn, fm, request);
4193 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4196 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4199 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4200 struct ofputil_flow_mod *fm,
4201 const struct ofp_header *request)
4202 OVS_REQUIRES(ofproto_mutex)
4204 struct rule_criteria criteria;
4205 struct rule_collection rules;
4208 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4209 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4210 error = collect_rules_loose(ofproto, &criteria, &rules);
4211 rule_criteria_destroy(&criteria);
4214 error = (rules.n > 0
4215 ? modify_flows__(ofproto, ofconn, fm, request, &rules)
4216 : modify_flows_add(ofproto, ofconn, fm, request));
4219 rule_collection_destroy(&rules);
4224 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
4227 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4230 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4231 struct ofputil_flow_mod *fm,
4232 const struct ofp_header *request)
4233 OVS_REQUIRES(ofproto_mutex)
4235 struct rule_criteria criteria;
4236 struct rule_collection rules;
4239 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4240 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4241 error = collect_rules_strict(ofproto, &criteria, &rules);
4242 rule_criteria_destroy(&criteria);
4246 error = modify_flows_add(ofproto, ofconn, fm, request);
4247 } else if (rules.n == 1) {
4248 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4252 rule_collection_destroy(&rules);
4257 /* OFPFC_DELETE implementation. */
4260 delete_flow__(struct rule *rule, struct ofopgroup *group,
4261 enum ofp_flow_removed_reason reason)
4262 OVS_REQUIRES(ofproto_mutex)
4264 struct ofproto *ofproto = rule->ofproto;
4266 ofproto_rule_send_removed(rule, reason);
4268 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
4269 oftable_remove_rule(rule);
4270 ofproto->ofproto_class->rule_delete(rule);
4273 /* Deletes the rules listed in 'rules'.
4275 * Returns 0 on success, otherwise an OpenFlow error code. */
4277 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4278 const struct ofp_header *request,
4279 const struct rule_collection *rules,
4280 enum ofp_flow_removed_reason reason)
4281 OVS_REQUIRES(ofproto_mutex)
4283 struct ofopgroup *group;
4286 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
4287 for (i = 0; i < rules->n; i++) {
4288 delete_flow__(rules->rules[i], group, reason);
4290 ofopgroup_submit(group);
4295 /* Implements OFPFC_DELETE. */
4297 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4298 const struct ofputil_flow_mod *fm,
4299 const struct ofp_header *request)
4300 OVS_REQUIRES(ofproto_mutex)
4302 struct rule_criteria criteria;
4303 struct rule_collection rules;
4306 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4307 fm->cookie, fm->cookie_mask,
4308 fm->out_port, fm->out_group);
4309 error = collect_rules_loose(ofproto, &criteria, &rules);
4310 rule_criteria_destroy(&criteria);
4312 if (!error && rules.n > 0) {
4313 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4315 rule_collection_destroy(&rules);
4320 /* Implements OFPFC_DELETE_STRICT. */
4322 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4323 const struct ofputil_flow_mod *fm,
4324 const struct ofp_header *request)
4325 OVS_REQUIRES(ofproto_mutex)
4327 struct rule_criteria criteria;
4328 struct rule_collection rules;
4331 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4332 fm->cookie, fm->cookie_mask,
4333 fm->out_port, fm->out_group);
4334 error = collect_rules_strict(ofproto, &criteria, &rules);
4335 rule_criteria_destroy(&criteria);
4337 if (!error && rules.n > 0) {
4338 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4340 rule_collection_destroy(&rules);
4346 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
4347 OVS_REQUIRES(ofproto_mutex)
4349 struct ofputil_flow_removed fr;
4351 if (ofproto_rule_is_hidden(rule) ||
4352 !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
4356 minimatch_expand(&rule->cr.match, &fr.match);
4357 fr.priority = rule->cr.priority;
4358 fr.cookie = rule->flow_cookie;
4360 fr.table_id = rule->table_id;
4361 calc_duration(rule->created, time_msec(),
4362 &fr.duration_sec, &fr.duration_nsec);
4363 ovs_mutex_lock(&rule->mutex);
4364 fr.idle_timeout = rule->idle_timeout;
4365 fr.hard_timeout = rule->hard_timeout;
4366 ovs_mutex_unlock(&rule->mutex);
4367 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
4370 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
4373 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4374 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4377 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
4380 * ofproto implementation ->run() functions should use this function to expire
4381 * OpenFlow flows. */
4383 ofproto_rule_expire(struct rule *rule, uint8_t reason)
4384 OVS_REQUIRES(ofproto_mutex)
4386 struct ofproto *ofproto = rule->ofproto;
4388 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
4389 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
4391 ofproto_rule_delete__(ofproto, rule, reason);
4394 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4395 * means "infinite". */
4397 reduce_timeout(uint16_t max, uint16_t *timeout)
4399 if (max && (!*timeout || *timeout > max)) {
4404 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4405 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4406 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4408 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4410 ofproto_rule_reduce_timeouts(struct rule *rule,
4411 uint16_t idle_timeout, uint16_t hard_timeout)
4412 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
4414 if (!idle_timeout && !hard_timeout) {
4418 ovs_mutex_lock(&ofproto_mutex);
4419 if (list_is_empty(&rule->expirable)) {
4420 list_insert(&rule->ofproto->expirable, &rule->expirable);
4422 ovs_mutex_unlock(&ofproto_mutex);
4424 ovs_mutex_lock(&rule->mutex);
4425 reduce_timeout(idle_timeout, &rule->idle_timeout);
4426 reduce_timeout(hard_timeout, &rule->hard_timeout);
4427 ovs_mutex_unlock(&rule->mutex);
4431 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4432 OVS_EXCLUDED(ofproto_mutex)
4434 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4435 struct ofputil_flow_mod fm;
4436 uint64_t ofpacts_stub[1024 / 8];
4437 struct ofpbuf ofpacts;
4441 error = reject_slave_controller(ofconn);
4446 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4447 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
4449 u16_to_ofp(ofproto->max_ports),
4452 error = ofproto_check_ofpacts(ofproto, fm.ofpacts, fm.ofpacts_len);
4455 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
4458 goto exit_free_ofpacts;
4461 /* Record the operation for logging a summary report. */
4462 switch (fm.command) {
4468 case OFPFC_MODIFY_STRICT:
4469 ofproto->n_modify++;
4473 case OFPFC_DELETE_STRICT:
4474 ofproto->n_delete++;
4479 if (ofproto->next_op_report == LLONG_MAX) {
4480 ofproto->first_op = now;
4481 ofproto->next_op_report = MAX(now + 10 * 1000,
4482 ofproto->op_backoff);
4483 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
4485 ofproto->last_op = now;
4488 ofpbuf_uninit(&ofpacts);
4494 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
4495 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
4496 OVS_EXCLUDED(ofproto_mutex)
4500 ovs_mutex_lock(&ofproto_mutex);
4501 if (ofproto->n_pending < 50) {
4502 switch (fm->command) {
4504 error = add_flow(ofproto, ofconn, fm, oh);
4508 error = modify_flows_loose(ofproto, ofconn, fm, oh);
4511 case OFPFC_MODIFY_STRICT:
4512 error = modify_flow_strict(ofproto, ofconn, fm, oh);
4516 error = delete_flows_loose(ofproto, ofconn, fm, oh);
4519 case OFPFC_DELETE_STRICT:
4520 error = delete_flow_strict(ofproto, ofconn, fm, oh);
4524 if (fm->command > 0xff) {
4525 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4526 "flow_mod_table_id extension is not enabled",
4529 error = OFPERR_OFPFMFC_BAD_COMMAND;
4533 ovs_assert(!list_is_empty(&ofproto->pending));
4534 error = OFPROTO_POSTPONE;
4536 ovs_mutex_unlock(&ofproto_mutex);
4538 run_rule_executes(ofproto);
4543 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4545 struct ofputil_role_request request;
4546 struct ofputil_role_request reply;
4550 error = ofputil_decode_role_message(oh, &request);
4555 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4556 if (ofconn_get_role(ofconn) != request.role
4557 && ofconn_has_pending_opgroups(ofconn)) {
4558 return OFPROTO_POSTPONE;
4561 if (request.have_generation_id
4562 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4563 return OFPERR_OFPRRFC_STALE;
4566 ofconn_set_role(ofconn, request.role);
4569 reply.role = ofconn_get_role(ofconn);
4570 reply.have_generation_id = ofconn_get_master_election_id(
4571 ofconn, &reply.generation_id);
4572 buf = ofputil_encode_role_reply(oh, &reply);
4573 ofconn_send_reply(ofconn, buf);
4579 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4580 const struct ofp_header *oh)
4582 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4583 enum ofputil_protocol cur, next;
4585 cur = ofconn_get_protocol(ofconn);
4586 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4587 ofconn_set_protocol(ofconn, next);
4593 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4595 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4596 enum ofputil_protocol cur, next;
4597 enum ofputil_protocol next_base;
4599 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4601 return OFPERR_OFPBRC_EPERM;
4604 cur = ofconn_get_protocol(ofconn);
4605 next = ofputil_protocol_set_base(cur, next_base);
4606 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4607 /* Avoid sending async messages in surprising protocol. */
4608 return OFPROTO_POSTPONE;
4611 ofconn_set_protocol(ofconn, next);
4616 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4617 const struct ofp_header *oh)
4619 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4622 format = ntohl(msg->format);
4623 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4624 return OFPERR_OFPBRC_EPERM;
4627 if (format != ofconn_get_packet_in_format(ofconn)
4628 && ofconn_has_pending_opgroups(ofconn)) {
4629 /* Avoid sending async message in surprsing packet in format. */
4630 return OFPROTO_POSTPONE;
4633 ofconn_set_packet_in_format(ofconn, format);
4638 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4640 const struct nx_async_config *msg = ofpmsg_body(oh);
4641 uint32_t master[OAM_N_TYPES];
4642 uint32_t slave[OAM_N_TYPES];
4644 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4645 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4646 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4648 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4649 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4650 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4652 ofconn_set_async_config(ofconn, master, slave);
4653 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4654 !ofconn_get_miss_send_len(ofconn)) {
4655 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4662 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4665 uint32_t master[OAM_N_TYPES];
4666 uint32_t slave[OAM_N_TYPES];
4667 struct nx_async_config *msg;
4669 ofconn_get_async_config(ofconn, master, slave);
4670 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4671 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4673 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4674 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4675 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4677 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4678 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4679 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4681 ofconn_send_reply(ofconn, buf);
4687 handle_nxt_set_controller_id(struct ofconn *ofconn,
4688 const struct ofp_header *oh)
4690 const struct nx_controller_id *nci = ofpmsg_body(oh);
4692 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4693 return OFPERR_NXBRC_MUST_BE_ZERO;
4696 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4701 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4705 if (ofconn_has_pending_opgroups(ofconn)) {
4706 return OFPROTO_POSTPONE;
4709 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4710 ? OFPRAW_OFPT10_BARRIER_REPLY
4711 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4712 ofconn_send_reply(ofconn, buf);
4717 ofproto_compose_flow_refresh_update(const struct rule *rule,
4718 enum nx_flow_monitor_flags flags,
4720 OVS_REQUIRES(ofproto_mutex)
4722 struct ofoperation *op = rule->pending;
4723 const struct rule_actions *actions;
4724 struct ofputil_flow_update fu;
4727 if (op && op->type == OFOPERATION_ADD) {
4728 /* We'll report the final flow when the operation completes. Reporting
4729 * it now would cause a duplicate report later. */
4733 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4734 ? NXFME_ADDED : NXFME_MODIFIED);
4736 ovs_mutex_lock(&rule->mutex);
4737 fu.idle_timeout = rule->idle_timeout;
4738 fu.hard_timeout = rule->hard_timeout;
4739 ovs_mutex_unlock(&rule->mutex);
4740 fu.table_id = rule->table_id;
4741 fu.cookie = rule->flow_cookie;
4742 minimatch_expand(&rule->cr.match, &match);
4744 fu.priority = rule->cr.priority;
4746 if (!(flags & NXFMF_ACTIONS)) {
4749 actions = rule->actions;
4751 /* An operation is in progress. Use the previous version of the flow's
4752 * actions, so that when the operation commits we report the change. */
4754 case OFOPERATION_ADD:
4757 case OFOPERATION_MODIFY:
4758 case OFOPERATION_REPLACE:
4759 actions = op->actions ? op->actions : rule->actions;
4762 case OFOPERATION_DELETE:
4763 actions = rule->actions;
4770 fu.ofpacts = actions ? actions->ofpacts : NULL;
4771 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
4773 if (list_is_empty(msgs)) {
4774 ofputil_start_flow_update(msgs);
4776 ofputil_append_flow_update(&fu, msgs);
4780 ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4782 OVS_REQUIRES(ofproto_mutex)
4786 for (i = 0; i < rules->n; i++) {
4787 struct rule *rule = rules->rules[i];
4788 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4789 rule->monitor_flags = 0;
4791 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4796 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4797 struct rule *rule, uint64_t seqno,
4798 struct rule_collection *rules)
4799 OVS_REQUIRES(ofproto_mutex)
4801 enum nx_flow_monitor_flags update;
4803 if (ofproto_rule_is_hidden(rule)) {
4808 ? ofoperation_has_out_port(rule->pending, m->out_port)
4809 : ofproto_rule_has_out_port(rule, m->out_port))) {
4814 if (rule->add_seqno > seqno) {
4815 update = NXFMF_ADD | NXFMF_MODIFY;
4816 } else if (rule->modify_seqno > seqno) {
4817 update = NXFMF_MODIFY;
4822 if (!(m->flags & update)) {
4826 update = NXFMF_INITIAL;
4829 if (!rule->monitor_flags) {
4830 rule_collection_add(rules, rule);
4832 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4836 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4838 struct rule_collection *rules)
4839 OVS_REQUIRES(ofproto_mutex)
4841 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4842 const struct ofoperation *op;
4843 const struct oftable *table;
4844 struct cls_rule target;
4846 cls_rule_init_from_minimatch(&target, &m->match, 0);
4847 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4848 struct cls_cursor cursor;
4851 ovs_rwlock_rdlock(&table->cls.rwlock);
4852 cls_cursor_init(&cursor, &table->cls, &target);
4853 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4854 ovs_assert(!rule->pending); /* XXX */
4855 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4857 ovs_rwlock_unlock(&table->cls.rwlock);
4860 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4861 struct rule *rule = op->rule;
4863 if (((m->table_id == 0xff
4864 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4865 : m->table_id == rule->table_id))
4866 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4867 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4870 cls_rule_destroy(&target);
4874 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4875 struct rule_collection *rules)
4876 OVS_REQUIRES(ofproto_mutex)
4878 if (m->flags & NXFMF_INITIAL) {
4879 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4884 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4885 uint64_t seqno, struct rule_collection *rules)
4886 OVS_REQUIRES(ofproto_mutex)
4888 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4892 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4893 OVS_EXCLUDED(ofproto_mutex)
4895 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4896 struct ofmonitor **monitors;
4897 size_t n_monitors, allocated_monitors;
4898 struct rule_collection rules;
4899 struct list replies;
4905 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4907 n_monitors = allocated_monitors = 0;
4909 ovs_mutex_lock(&ofproto_mutex);
4911 struct ofputil_flow_monitor_request request;
4912 struct ofmonitor *m;
4915 retval = ofputil_decode_flow_monitor_request(&request, &b);
4916 if (retval == EOF) {
4918 } else if (retval) {
4923 if (request.table_id != 0xff
4924 && request.table_id >= ofproto->n_tables) {
4925 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4929 error = ofmonitor_create(&request, ofconn, &m);
4934 if (n_monitors >= allocated_monitors) {
4935 monitors = x2nrealloc(monitors, &allocated_monitors,
4938 monitors[n_monitors++] = m;
4941 rule_collection_init(&rules);
4942 for (i = 0; i < n_monitors; i++) {
4943 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4946 ofpmp_init(&replies, oh);
4947 ofmonitor_compose_refresh_updates(&rules, &replies);
4948 ovs_mutex_unlock(&ofproto_mutex);
4950 rule_collection_destroy(&rules);
4952 ofconn_send_replies(ofconn, &replies);
4958 for (i = 0; i < n_monitors; i++) {
4959 ofmonitor_destroy(monitors[i]);
4962 ovs_mutex_unlock(&ofproto_mutex);
4968 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4969 OVS_EXCLUDED(ofproto_mutex)
4971 struct ofmonitor *m;
4975 id = ofputil_decode_flow_monitor_cancel(oh);
4977 ovs_mutex_lock(&ofproto_mutex);
4978 m = ofmonitor_lookup(ofconn, id);
4980 ofmonitor_destroy(m);
4983 error = OFPERR_NXBRC_FM_BAD_ID;
4985 ovs_mutex_unlock(&ofproto_mutex);
4990 /* Meters implementation.
4992 * Meter table entry, indexed by the OpenFlow meter_id.
4993 * These are always dynamically allocated to allocate enough space for
4995 * 'created' is used to compute the duration for meter stats.
4996 * 'list rules' is needed so that we can delete the dependent rules when the
4997 * meter table entry is deleted.
4998 * 'provider_meter_id' is for the provider's private use.
5001 long long int created; /* Time created. */
5002 struct list rules; /* List of "struct rule_dpif"s. */
5003 ofproto_meter_id provider_meter_id;
5004 uint16_t flags; /* Meter flags. */
5005 uint16_t n_bands; /* Number of meter bands. */
5006 struct ofputil_meter_band *bands;
5010 * This is used in instruction validation at flow set-up time,
5011 * as flows may not use non-existing meters.
5012 * Return value of UINT32_MAX signifies an invalid meter.
5015 get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id)
5017 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
5018 const struct meter *meter = ofproto->meters[of_meter_id];
5020 return meter->provider_meter_id.uint32;
5027 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
5031 meter->flags = config->flags;
5032 meter->n_bands = config->n_bands;
5033 meter->bands = xmemdup(config->bands,
5034 config->n_bands * sizeof *meter->bands);
5037 static struct meter *
5038 meter_create(const struct ofputil_meter_config *config,
5039 ofproto_meter_id provider_meter_id)
5041 struct meter *meter;
5043 meter = xzalloc(sizeof *meter);
5044 meter->provider_meter_id = provider_meter_id;
5045 meter->created = time_msec();
5046 list_init(&meter->rules);
5048 meter_update(meter, config);
5054 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
5055 OVS_REQUIRES(ofproto_mutex)
5058 for (mid = first; mid <= last; ++mid) {
5059 struct meter *meter = ofproto->meters[mid];
5061 ofproto->meters[mid] = NULL;
5062 ofproto->ofproto_class->meter_del(ofproto,
5063 meter->provider_meter_id);
5071 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5073 ofproto_meter_id provider_meter_id = { UINT32_MAX };
5074 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
5078 return OFPERR_OFPMMFC_METER_EXISTS;
5081 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
5084 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
5085 *meterp = meter_create(&mm->meter, provider_meter_id);
5091 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5093 struct meter *meter = ofproto->meters[mm->meter.meter_id];
5095 uint32_t provider_meter_id;
5098 return OFPERR_OFPMMFC_UNKNOWN_METER;
5101 provider_meter_id = meter->provider_meter_id.uint32;
5102 error = ofproto->ofproto_class->meter_set(ofproto,
5103 &meter->provider_meter_id,
5105 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
5107 meter_update(meter, &mm->meter);
5113 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
5114 struct ofputil_meter_mod *mm)
5115 OVS_EXCLUDED(ofproto_mutex)
5117 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5118 uint32_t meter_id = mm->meter.meter_id;
5119 struct rule_collection rules;
5120 enum ofperr error = 0;
5121 uint32_t first, last;
5123 if (meter_id == OFPM13_ALL) {
5125 last = ofproto->meter_features.max_meters;
5127 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
5130 first = last = meter_id;
5133 /* First delete the rules that use this meter. If any of those rules are
5134 * currently being modified, postpone the whole operation until later. */
5135 rule_collection_init(&rules);
5136 ovs_mutex_lock(&ofproto_mutex);
5137 for (meter_id = first; meter_id <= last; ++meter_id) {
5138 struct meter *meter = ofproto->meters[meter_id];
5139 if (meter && !list_is_empty(&meter->rules)) {
5142 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
5143 if (rule->pending) {
5144 error = OFPROTO_POSTPONE;
5147 rule_collection_add(&rules, rule);
5152 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
5155 /* Delete the meters. */
5156 meter_delete(ofproto, first, last);
5159 ovs_mutex_unlock(&ofproto_mutex);
5160 rule_collection_destroy(&rules);
5166 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5168 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5169 struct ofputil_meter_mod mm;
5170 uint64_t bands_stub[256 / 8];
5171 struct ofpbuf bands;
5175 error = reject_slave_controller(ofconn);
5180 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5182 error = ofputil_decode_meter_mod(oh, &mm, &bands);
5184 goto exit_free_bands;
5187 meter_id = mm.meter.meter_id;
5189 if (mm.command != OFPMC13_DELETE) {
5190 /* Fails also when meters are not implemented by the provider. */
5191 if (meter_id == 0 || meter_id > OFPM13_MAX) {
5192 error = OFPERR_OFPMMFC_INVALID_METER;
5193 goto exit_free_bands;
5194 } else if (meter_id > ofproto->meter_features.max_meters) {
5195 error = OFPERR_OFPMMFC_OUT_OF_METERS;
5196 goto exit_free_bands;
5198 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
5199 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
5200 goto exit_free_bands;
5204 switch (mm.command) {
5206 error = handle_add_meter(ofproto, &mm);
5209 case OFPMC13_MODIFY:
5210 error = handle_modify_meter(ofproto, &mm);
5213 case OFPMC13_DELETE:
5214 error = handle_delete_meter(ofconn, oh, &mm);
5218 error = OFPERR_OFPMMFC_BAD_COMMAND;
5223 ofpbuf_uninit(&bands);
5228 handle_meter_features_request(struct ofconn *ofconn,
5229 const struct ofp_header *request)
5231 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5232 struct ofputil_meter_features features;
5235 if (ofproto->ofproto_class->meter_get_features) {
5236 ofproto->ofproto_class->meter_get_features(ofproto, &features);
5238 memset(&features, 0, sizeof features);
5240 b = ofputil_encode_meter_features_reply(&features, request);
5242 ofconn_send_reply(ofconn, b);
5247 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
5250 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5251 struct list replies;
5252 uint64_t bands_stub[256 / 8];
5253 struct ofpbuf bands;
5254 uint32_t meter_id, first, last;
5256 ofputil_decode_meter_request(request, &meter_id);
5258 if (meter_id == OFPM13_ALL) {
5260 last = ofproto->meter_features.max_meters;
5262 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
5263 !ofproto->meters[meter_id]) {
5264 return OFPERR_OFPMMFC_UNKNOWN_METER;
5266 first = last = meter_id;
5269 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5270 ofpmp_init(&replies, request);
5272 for (meter_id = first; meter_id <= last; ++meter_id) {
5273 struct meter *meter = ofproto->meters[meter_id];
5275 continue; /* Skip non-existing meters. */
5277 if (type == OFPTYPE_METER_STATS_REQUEST) {
5278 struct ofputil_meter_stats stats;
5280 stats.meter_id = meter_id;
5282 /* Provider sets the packet and byte counts, we do the rest. */
5283 stats.flow_count = list_size(&meter->rules);
5284 calc_duration(meter->created, time_msec(),
5285 &stats.duration_sec, &stats.duration_nsec);
5286 stats.n_bands = meter->n_bands;
5287 ofpbuf_clear(&bands);
5289 = ofpbuf_put_uninit(&bands,
5290 meter->n_bands * sizeof *stats.bands);
5292 if (!ofproto->ofproto_class->meter_get(ofproto,
5293 meter->provider_meter_id,
5295 ofputil_append_meter_stats(&replies, &stats);
5297 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
5298 struct ofputil_meter_config config;
5300 config.meter_id = meter_id;
5301 config.flags = meter->flags;
5302 config.n_bands = meter->n_bands;
5303 config.bands = meter->bands;
5304 ofputil_append_meter_config(&replies, &config);
5308 ofconn_send_replies(ofconn, &replies);
5309 ofpbuf_uninit(&bands);
5314 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5315 struct ofgroup **group)
5316 OVS_TRY_RDLOCK(true, (*group)->rwlock)
5318 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5319 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5320 hash_int(group_id, 0), &ofproto->groups) {
5321 if ((*group)->group_id == group_id) {
5322 ovs_rwlock_rdlock(&(*group)->rwlock);
5323 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5327 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5332 ofproto_group_release(struct ofgroup *group)
5333 OVS_RELEASES(group->rwlock)
5335 ovs_rwlock_unlock(&group->rwlock);
5339 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
5340 struct ofgroup **group)
5341 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
5342 OVS_TRY_WRLOCK(true, (*group)->rwlock)
5344 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5345 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5346 hash_int(group_id, 0), &ofproto->groups) {
5347 if ((*group)->group_id == group_id) {
5348 ovs_rwlock_wrlock(&(*group)->rwlock);
5352 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5357 ofproto_group_exists__(const struct ofproto *ofproto, uint32_t group_id)
5358 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5360 struct ofgroup *grp;
5362 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
5363 hash_int(group_id, 0), &ofproto->groups) {
5364 if (grp->group_id == group_id) {
5372 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
5373 OVS_EXCLUDED(ofproto->groups_rwlock)
5377 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5378 exists = ofproto_group_exists__(ofproto, group_id);
5379 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5385 group_get_ref_count(struct ofgroup *group)
5386 OVS_EXCLUDED(ofproto_mutex)
5388 struct ofproto *ofproto = group->ofproto;
5389 struct rule_criteria criteria;
5390 struct rule_collection rules;
5395 match_init_catchall(&match);
5396 rule_criteria_init(&criteria, 0xff, &match, 0, htonll(0), htonll(0),
5397 OFPP_ANY, group->group_id);
5398 ovs_mutex_lock(&ofproto_mutex);
5399 error = collect_rules_loose(ofproto, &criteria, &rules);
5400 ovs_mutex_unlock(&ofproto_mutex);
5401 rule_criteria_destroy(&criteria);
5403 count = !error && rules.n < UINT32_MAX ? rules.n : UINT32_MAX;
5405 rule_collection_destroy(&rules);
5410 append_group_stats(struct ofgroup *group, struct list *replies)
5411 OVS_REQ_RDLOCK(group->rwlock)
5413 struct ofputil_group_stats ogs;
5414 struct ofproto *ofproto = group->ofproto;
5415 long long int now = time_msec();
5418 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
5420 /* Provider sets the packet and byte counts, we do the rest. */
5421 ogs.ref_count = group_get_ref_count(group);
5422 ogs.n_buckets = group->n_buckets;
5424 error = (ofproto->ofproto_class->group_get_stats
5425 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
5428 ogs.packet_count = UINT64_MAX;
5429 ogs.byte_count = UINT64_MAX;
5430 memset(ogs.bucket_stats, 0xff,
5431 ogs.n_buckets * sizeof *ogs.bucket_stats);
5434 ogs.group_id = group->group_id;
5435 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
5437 ofputil_append_group_stats(replies, &ogs);
5439 free(ogs.bucket_stats);
5443 handle_group_stats_request(struct ofconn *ofconn,
5444 const struct ofp_header *request)
5446 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5447 struct list replies;
5449 struct ofgroup *group;
5452 error = ofputil_decode_group_stats_request(request, &group_id);
5457 ofpmp_init(&replies, request);
5459 if (group_id == OFPG_ALL) {
5460 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5461 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5462 ovs_rwlock_rdlock(&group->rwlock);
5463 append_group_stats(group, &replies);
5464 ovs_rwlock_unlock(&group->rwlock);
5466 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5468 if (ofproto_group_lookup(ofproto, group_id, &group)) {
5469 append_group_stats(group, &replies);
5470 ofproto_group_release(group);
5474 ofconn_send_replies(ofconn, &replies);
5480 handle_group_desc_stats_request(struct ofconn *ofconn,
5481 const struct ofp_header *request)
5483 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5484 struct list replies;
5485 struct ofputil_group_desc gds;
5486 struct ofgroup *group;
5488 ofpmp_init(&replies, request);
5490 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5491 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5492 gds.group_id = group->group_id;
5493 gds.type = group->type;
5494 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
5496 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5498 ofconn_send_replies(ofconn, &replies);
5504 handle_group_features_stats_request(struct ofconn *ofconn,
5505 const struct ofp_header *request)
5507 struct ofproto *p = ofconn_get_ofproto(ofconn);
5510 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5512 ofconn_send_reply(ofconn, msg);
5519 handle_queue_get_config_request(struct ofconn *ofconn,
5520 const struct ofp_header *oh)
5522 struct ofproto *p = ofconn_get_ofproto(ofconn);
5523 struct netdev_queue_dump queue_dump;
5524 struct ofport *ofport;
5525 unsigned int queue_id;
5526 struct ofpbuf *reply;
5527 struct smap details;
5531 error = ofputil_decode_queue_get_config_request(oh, &request);
5536 ofport = ofproto_get_port(p, request);
5538 return OFPERR_OFPQOFC_BAD_PORT;
5541 reply = ofputil_encode_queue_get_config_reply(oh);
5543 smap_init(&details);
5544 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, ofport->netdev) {
5545 struct ofputil_queue_config queue;
5547 /* None of the existing queues have compatible properties, so we
5548 * hard-code omitting min_rate and max_rate. */
5549 queue.queue_id = queue_id;
5550 queue.min_rate = UINT16_MAX;
5551 queue.max_rate = UINT16_MAX;
5552 ofputil_append_queue_get_config_reply(reply, &queue);
5554 smap_destroy(&details);
5556 ofconn_send_reply(ofconn, reply);
5561 /* Implements OFPGC11_ADD
5562 * in which no matching flow already exists in the flow table.
5564 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
5565 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
5566 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
5567 * initiated now but may be retried later.
5569 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
5570 * ownership remains with the caller.
5572 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5575 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5577 struct ofgroup *ofgroup;
5580 if (gm->group_id > OFPG_MAX) {
5581 return OFPERR_OFPGMFC_INVALID_GROUP;
5583 if (gm->type > OFPGT11_FF) {
5584 return OFPERR_OFPGMFC_BAD_TYPE;
5587 /* Allocate new group and initialize it. */
5588 ofgroup = ofproto->ofproto_class->group_alloc();
5590 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
5591 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5594 ovs_rwlock_init(&ofgroup->rwlock);
5595 ofgroup->ofproto = ofproto;
5596 ofgroup->group_id = gm->group_id;
5597 ofgroup->type = gm->type;
5598 ofgroup->created = ofgroup->modified = time_msec();
5600 list_move(&ofgroup->buckets, &gm->buckets);
5601 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5603 /* Construct called BEFORE any locks are held. */
5604 error = ofproto->ofproto_class->group_construct(ofgroup);
5609 /* We wrlock as late as possible to minimize the time we jam any other
5610 * threads: No visible state changes before acquiring the lock. */
5611 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5613 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5614 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5618 if (ofproto_group_exists__(ofproto, gm->group_id)) {
5619 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5624 /* Insert new group. */
5625 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5626 hash_int(ofgroup->group_id, 0));
5627 ofproto->n_groups[ofgroup->type]++;
5629 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5634 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5635 ofproto->ofproto_class->group_destruct(ofgroup);
5637 ofputil_bucket_list_destroy(&ofgroup->buckets);
5638 ofproto->ofproto_class->group_dealloc(ofgroup);
5643 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5646 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5649 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5651 struct ofgroup *ofgroup;
5652 struct ofgroup *victim;
5655 if (gm->group_id > OFPG_MAX) {
5656 return OFPERR_OFPGMFC_INVALID_GROUP;
5659 if (gm->type > OFPGT11_FF) {
5660 return OFPERR_OFPGMFC_BAD_TYPE;
5663 victim = ofproto->ofproto_class->group_alloc();
5665 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5666 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5669 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
5670 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5673 /* Both group's and its container's write locks held now.
5674 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5675 if (ofgroup->type != gm->type
5676 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5677 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5682 list_move(&victim->buckets, &ofgroup->buckets);
5684 ofgroup->type = gm->type;
5685 list_move(&ofgroup->buckets, &gm->buckets);
5686 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5688 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
5690 ofputil_bucket_list_destroy(&victim->buckets);
5691 ofproto->n_groups[victim->type]--;
5692 ofproto->n_groups[ofgroup->type]++;
5693 ofgroup->modified = time_msec();
5695 ofputil_bucket_list_destroy(&ofgroup->buckets);
5698 list_move(&ofgroup->buckets, &victim->buckets);
5702 ovs_rwlock_unlock(&ofgroup->rwlock);
5703 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5705 ofproto->ofproto_class->group_dealloc(victim);
5710 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5711 OVS_RELEASES(ofproto->groups_rwlock)
5714 struct ofputil_flow_mod fm;
5716 /* Delete all flow entries containing this group in a group action */
5717 match_init_catchall(&match);
5718 flow_mod_init(&fm, &match, 0, NULL, 0, OFPFC_DELETE);
5719 fm.out_group = ofgroup->group_id;
5720 handle_flow_mod__(ofproto, NULL, &fm, NULL);
5722 /* Must wait until existing readers are done,
5723 * while holding the container's write lock at the same time. */
5724 ovs_rwlock_wrlock(&ofgroup->rwlock);
5725 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5726 /* No-one can find this group any more. */
5727 ofproto->n_groups[ofgroup->type]--;
5728 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5730 ofproto->ofproto_class->group_destruct(ofgroup);
5731 ofputil_bucket_list_destroy(&ofgroup->buckets);
5732 ovs_rwlock_unlock(&ofgroup->rwlock);
5733 ovs_rwlock_destroy(&ofgroup->rwlock);
5734 ofproto->ofproto_class->group_dealloc(ofgroup);
5737 /* Implements OFPGC_DELETE. */
5739 delete_group(struct ofproto *ofproto, uint32_t group_id)
5741 struct ofgroup *ofgroup;
5743 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5744 if (group_id == OFPG_ALL) {
5746 struct hmap_node *node = hmap_first(&ofproto->groups);
5750 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5751 delete_group__(ofproto, ofgroup);
5752 /* Lock for each node separately, so that we will not jam the
5753 * other threads for too long time. */
5754 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5757 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5758 hash_int(group_id, 0), &ofproto->groups) {
5759 if (ofgroup->group_id == group_id) {
5760 delete_group__(ofproto, ofgroup);
5765 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5769 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5771 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5772 struct ofputil_group_mod gm;
5775 error = reject_slave_controller(ofconn);
5780 error = ofputil_decode_group_mod(oh, &gm);
5785 switch (gm.command) {
5787 return add_group(ofproto, &gm);
5789 case OFPGC11_MODIFY:
5790 return modify_group(ofproto, &gm);
5792 case OFPGC11_DELETE:
5793 delete_group(ofproto, gm.group_id);
5797 if (gm.command > OFPGC11_DELETE) {
5798 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5799 ofproto->name, gm.command);
5801 return OFPERR_OFPGMFC_BAD_COMMAND;
5806 handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5808 struct ofputil_table_mod tm;
5811 error = reject_slave_controller(ofconn);
5816 error = ofputil_decode_table_mod(oh, &tm);
5821 /* XXX Actual table mod support is not implemented yet. */
5826 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5827 OVS_EXCLUDED(ofproto_mutex)
5829 const struct ofp_header *oh = msg->data;
5833 error = ofptype_decode(&type, oh);
5837 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
5838 && ofpmp_more(oh)) {
5839 /* We have no buffer implementation for multipart requests.
5840 * Report overflow for requests which consists of multiple
5842 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
5846 /* OpenFlow requests. */
5847 case OFPTYPE_ECHO_REQUEST:
5848 return handle_echo_request(ofconn, oh);
5850 case OFPTYPE_FEATURES_REQUEST:
5851 return handle_features_request(ofconn, oh);
5853 case OFPTYPE_GET_CONFIG_REQUEST:
5854 return handle_get_config_request(ofconn, oh);
5856 case OFPTYPE_SET_CONFIG:
5857 return handle_set_config(ofconn, oh);
5859 case OFPTYPE_PACKET_OUT:
5860 return handle_packet_out(ofconn, oh);
5862 case OFPTYPE_PORT_MOD:
5863 return handle_port_mod(ofconn, oh);
5865 case OFPTYPE_FLOW_MOD:
5866 return handle_flow_mod(ofconn, oh);
5868 case OFPTYPE_GROUP_MOD:
5869 return handle_group_mod(ofconn, oh);
5871 case OFPTYPE_TABLE_MOD:
5872 return handle_table_mod(ofconn, oh);
5874 case OFPTYPE_METER_MOD:
5875 return handle_meter_mod(ofconn, oh);
5877 case OFPTYPE_BARRIER_REQUEST:
5878 return handle_barrier_request(ofconn, oh);
5880 case OFPTYPE_ROLE_REQUEST:
5881 return handle_role_request(ofconn, oh);
5883 /* OpenFlow replies. */
5884 case OFPTYPE_ECHO_REPLY:
5887 /* Nicira extension requests. */
5888 case OFPTYPE_FLOW_MOD_TABLE_ID:
5889 return handle_nxt_flow_mod_table_id(ofconn, oh);
5891 case OFPTYPE_SET_FLOW_FORMAT:
5892 return handle_nxt_set_flow_format(ofconn, oh);
5894 case OFPTYPE_SET_PACKET_IN_FORMAT:
5895 return handle_nxt_set_packet_in_format(ofconn, oh);
5897 case OFPTYPE_SET_CONTROLLER_ID:
5898 return handle_nxt_set_controller_id(ofconn, oh);
5900 case OFPTYPE_FLOW_AGE:
5901 /* Nothing to do. */
5904 case OFPTYPE_FLOW_MONITOR_CANCEL:
5905 return handle_flow_monitor_cancel(ofconn, oh);
5907 case OFPTYPE_SET_ASYNC_CONFIG:
5908 return handle_nxt_set_async_config(ofconn, oh);
5910 case OFPTYPE_GET_ASYNC_REQUEST:
5911 return handle_nxt_get_async_request(ofconn, oh);
5913 /* Statistics requests. */
5914 case OFPTYPE_DESC_STATS_REQUEST:
5915 return handle_desc_stats_request(ofconn, oh);
5917 case OFPTYPE_FLOW_STATS_REQUEST:
5918 return handle_flow_stats_request(ofconn, oh);
5920 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5921 return handle_aggregate_stats_request(ofconn, oh);
5923 case OFPTYPE_TABLE_STATS_REQUEST:
5924 return handle_table_stats_request(ofconn, oh);
5926 case OFPTYPE_PORT_STATS_REQUEST:
5927 return handle_port_stats_request(ofconn, oh);
5929 case OFPTYPE_QUEUE_STATS_REQUEST:
5930 return handle_queue_stats_request(ofconn, oh);
5932 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5933 return handle_port_desc_stats_request(ofconn, oh);
5935 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5936 return handle_flow_monitor_request(ofconn, oh);
5938 case OFPTYPE_METER_STATS_REQUEST:
5939 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5940 return handle_meter_request(ofconn, oh, type);
5942 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5943 return handle_meter_features_request(ofconn, oh);
5945 case OFPTYPE_GROUP_STATS_REQUEST:
5946 return handle_group_stats_request(ofconn, oh);
5948 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5949 return handle_group_desc_stats_request(ofconn, oh);
5951 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5952 return handle_group_features_stats_request(ofconn, oh);
5954 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5955 return handle_queue_get_config_request(ofconn, oh);
5959 case OFPTYPE_FEATURES_REPLY:
5960 case OFPTYPE_GET_CONFIG_REPLY:
5961 case OFPTYPE_PACKET_IN:
5962 case OFPTYPE_FLOW_REMOVED:
5963 case OFPTYPE_PORT_STATUS:
5964 case OFPTYPE_BARRIER_REPLY:
5965 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5966 case OFPTYPE_DESC_STATS_REPLY:
5967 case OFPTYPE_FLOW_STATS_REPLY:
5968 case OFPTYPE_QUEUE_STATS_REPLY:
5969 case OFPTYPE_PORT_STATS_REPLY:
5970 case OFPTYPE_TABLE_STATS_REPLY:
5971 case OFPTYPE_AGGREGATE_STATS_REPLY:
5972 case OFPTYPE_PORT_DESC_STATS_REPLY:
5973 case OFPTYPE_ROLE_REPLY:
5974 case OFPTYPE_FLOW_MONITOR_PAUSED:
5975 case OFPTYPE_FLOW_MONITOR_RESUMED:
5976 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
5977 case OFPTYPE_GET_ASYNC_REPLY:
5978 case OFPTYPE_GROUP_STATS_REPLY:
5979 case OFPTYPE_GROUP_DESC_STATS_REPLY:
5980 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
5981 case OFPTYPE_METER_STATS_REPLY:
5982 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5983 case OFPTYPE_METER_FEATURES_STATS_REPLY:
5984 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
5985 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
5986 case OFPTYPE_ROLE_STATUS:
5988 if (ofpmsg_is_stat_request(oh)) {
5989 return OFPERR_OFPBRC_BAD_STAT;
5991 return OFPERR_OFPBRC_BAD_TYPE;
5997 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
5998 OVS_EXCLUDED(ofproto_mutex)
6000 int error = handle_openflow__(ofconn, ofp_msg);
6001 if (error && error != OFPROTO_POSTPONE) {
6002 ofconn_send_error(ofconn, ofp_msg->data, error);
6004 COVERAGE_INC(ofproto_recv_openflow);
6005 return error != OFPROTO_POSTPONE;
6008 /* Asynchronous operations. */
6010 /* Creates and returns a new ofopgroup that is not associated with any
6011 * OpenFlow connection.
6013 * The caller should add operations to the returned group with
6014 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6015 static struct ofopgroup *
6016 ofopgroup_create_unattached(struct ofproto *ofproto)
6017 OVS_REQUIRES(ofproto_mutex)
6019 struct ofopgroup *group = xzalloc(sizeof *group);
6020 group->ofproto = ofproto;
6021 list_init(&group->ofproto_node);
6022 list_init(&group->ops);
6023 list_init(&group->ofconn_node);
6027 /* Creates and returns a new ofopgroup for 'ofproto'.
6029 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
6030 * connection. The 'request' and 'buffer_id' arguments are ignored.
6032 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
6033 * If the ofopgroup eventually fails, then the error reply will include
6034 * 'request'. If the ofopgroup eventually succeeds, then the packet with
6035 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
6037 * The caller should add operations to the returned group with
6038 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6039 static struct ofopgroup *
6040 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
6041 const struct ofp_header *request, uint32_t buffer_id)
6042 OVS_REQUIRES(ofproto_mutex)
6044 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
6046 size_t request_len = ntohs(request->length);
6048 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
6050 ofconn_add_opgroup(ofconn, &group->ofconn_node);
6051 group->ofconn = ofconn;
6052 group->request = xmemdup(request, MIN(request_len, 64));
6053 group->buffer_id = buffer_id;
6058 /* Submits 'group' for processing.
6060 * If 'group' contains no operations (e.g. none were ever added, or all of the
6061 * ones that were added completed synchronously), then it is destroyed
6062 * immediately. Otherwise it is added to the ofproto's list of pending
6065 ofopgroup_submit(struct ofopgroup *group)
6066 OVS_REQUIRES(ofproto_mutex)
6068 if (!group->n_running) {
6069 ofopgroup_complete(group);
6071 list_push_back(&group->ofproto->pending, &group->ofproto_node);
6072 group->ofproto->n_pending++;
6077 ofopgroup_complete(struct ofopgroup *group)
6078 OVS_REQUIRES(ofproto_mutex)
6080 struct ofproto *ofproto = group->ofproto;
6082 struct ofconn *abbrev_ofconn;
6083 ovs_be32 abbrev_xid;
6085 struct ofoperation *op, *next_op;
6088 ovs_assert(!group->n_running);
6091 LIST_FOR_EACH (op, group_node, &group->ops) {
6098 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
6099 LIST_FOR_EACH (op, group_node, &group->ops) {
6100 if (op->type != OFOPERATION_DELETE) {
6101 struct ofpbuf *packet;
6104 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
6107 struct rule_execute *re;
6111 ofproto_rule_ref(op->rule);
6113 re = xmalloc(sizeof *re);
6114 re->rule = op->rule;
6115 re->in_port = in_port;
6116 re->packet = packet;
6118 if (!guarded_list_push_back(&ofproto->rule_executes,
6119 &re->list_node, 1024)) {
6120 ofproto_rule_unref(op->rule);
6121 ofpbuf_delete(re->packet);
6130 if (!error && !list_is_empty(&group->ofconn_node)) {
6131 abbrev_ofconn = group->ofconn;
6132 abbrev_xid = group->request->xid;
6134 abbrev_ofconn = NULL;
6135 abbrev_xid = htonl(0);
6137 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
6138 struct rule *rule = op->rule;
6140 /* We generally want to report the change to active OpenFlow flow
6141 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
6143 - The operation failed.
6145 - The affected rule is not visible to controllers.
6147 - The operation's only effect was to update rule->modified. */
6149 || ofproto_rule_is_hidden(rule)
6150 || (op->type == OFOPERATION_MODIFY
6152 && rule->flow_cookie == op->flow_cookie))) {
6153 /* Check that we can just cast from ofoperation_type to
6154 * nx_flow_update_event. */
6155 enum nx_flow_update_event event_type;
6158 case OFOPERATION_ADD:
6159 case OFOPERATION_REPLACE:
6160 event_type = NXFME_ADDED;
6163 case OFOPERATION_DELETE:
6164 event_type = NXFME_DELETED;
6167 case OFOPERATION_MODIFY:
6168 event_type = NXFME_MODIFIED;
6175 ofmonitor_report(ofproto->connmgr, rule, event_type,
6176 op->reason, abbrev_ofconn, abbrev_xid);
6179 rule->pending = NULL;
6182 case OFOPERATION_ADD:
6186 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
6187 if (vid_mask == VLAN_VID_MASK) {
6188 if (ofproto->vlan_bitmap) {
6189 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
6190 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
6191 bitmap_set1(ofproto->vlan_bitmap, vid);
6192 ofproto->vlans_changed = true;
6195 ofproto->vlans_changed = true;
6199 oftable_remove_rule(rule);
6200 ofproto_rule_unref(rule);
6204 case OFOPERATION_DELETE:
6205 ovs_assert(!op->error);
6206 ofproto_rule_unref(rule);
6210 case OFOPERATION_MODIFY:
6211 case OFOPERATION_REPLACE:
6213 long long int now = time_msec();
6215 rule->modified = now;
6216 if (op->type == OFOPERATION_REPLACE) {
6217 rule->created = rule->used = now;
6220 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
6221 ovs_mutex_lock(&rule->mutex);
6222 rule->idle_timeout = op->idle_timeout;
6223 rule->hard_timeout = op->hard_timeout;
6224 ovs_mutex_unlock(&rule->mutex);
6226 struct rule_actions *old_actions;
6228 ovs_mutex_lock(&rule->mutex);
6229 old_actions = rule->actions;
6230 rule->actions = op->actions;
6231 ovs_mutex_unlock(&rule->mutex);
6234 rule_actions_unref(old_actions);
6236 rule->flags = op->flags;
6244 ofoperation_destroy(op);
6247 ofmonitor_flush(ofproto->connmgr);
6249 if (!list_is_empty(&group->ofproto_node)) {
6250 ovs_assert(ofproto->n_pending > 0);
6251 ofproto->n_pending--;
6252 list_remove(&group->ofproto_node);
6254 if (!list_is_empty(&group->ofconn_node)) {
6255 list_remove(&group->ofconn_node);
6257 ofconn_send_error(group->ofconn, group->request, error);
6259 connmgr_retry(ofproto->connmgr);
6261 free(group->request);
6265 /* Initiates a new operation on 'rule', of the specified 'type', within
6266 * 'group'. Prior to calling, 'rule' must not have any pending operation.
6268 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
6269 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
6271 * Returns the newly created ofoperation (which is also available as
6272 * rule->pending). */
6273 static struct ofoperation *
6274 ofoperation_create(struct ofopgroup *group, struct rule *rule,
6275 enum ofoperation_type type,
6276 enum ofp_flow_removed_reason reason)
6277 OVS_REQUIRES(ofproto_mutex)
6279 struct ofproto *ofproto = group->ofproto;
6280 struct ofoperation *op;
6282 ovs_assert(!rule->pending);
6284 op = rule->pending = xzalloc(sizeof *op);
6286 list_push_back(&group->ops, &op->group_node);
6289 op->reason = reason;
6290 op->flow_cookie = rule->flow_cookie;
6291 ovs_mutex_lock(&rule->mutex);
6292 op->idle_timeout = rule->idle_timeout;
6293 op->hard_timeout = rule->hard_timeout;
6294 ovs_mutex_unlock(&rule->mutex);
6295 op->flags = rule->flags;
6299 if (type == OFOPERATION_DELETE) {
6300 hmap_insert(&ofproto->deletions, &op->hmap_node,
6301 cls_rule_hash(&rule->cr, rule->table_id));
6308 ofoperation_destroy(struct ofoperation *op)
6309 OVS_REQUIRES(ofproto_mutex)
6311 struct ofopgroup *group = op->group;
6314 op->rule->pending = NULL;
6316 if (op->type == OFOPERATION_DELETE) {
6317 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
6319 list_remove(&op->group_node);
6320 rule_actions_unref(op->actions);
6324 /* Indicates that 'op' completed with status 'error', which is either 0 to
6325 * indicate success or an OpenFlow error code on failure.
6327 * If 'error' is 0, indicating success, the operation will be committed
6328 * permanently to the flow table.
6330 * If 'error' is nonzero, then generally the operation will be rolled back:
6332 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
6333 * restores the original rule. The caller must have uninitialized any
6334 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
6335 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
6336 * and 7 for the new rule, calling its ->rule_dealloc() function.
6338 * - If 'op' is a "modify flow" operation, ofproto restores the original
6341 * - 'op' must not be a "delete flow" operation. Removing a rule is not
6342 * allowed to fail. It must always succeed.
6344 * Please see the large comment in ofproto/ofproto-provider.h titled
6345 * "Asynchronous Operation Support" for more information. */
6347 ofoperation_complete(struct ofoperation *op, enum ofperr error)
6349 struct ofopgroup *group = op->group;
6351 ovs_assert(group->n_running > 0);
6352 ovs_assert(!error || op->type != OFOPERATION_DELETE);
6355 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
6356 /* This function can be called from ->rule_construct(), in which case
6357 * ofproto_mutex is held, or it can be called from ->run(), in which
6358 * case ofproto_mutex is not held. But only in the latter case can we
6359 * arrive here, so we can safely take ofproto_mutex now. */
6360 ovs_mutex_lock(&ofproto_mutex);
6361 ovs_assert(op->rule->pending == op);
6362 ofopgroup_complete(group);
6363 ovs_mutex_unlock(&ofproto_mutex);
6368 pick_datapath_id(const struct ofproto *ofproto)
6370 const struct ofport *port;
6372 port = ofproto_get_port(ofproto, OFPP_LOCAL);
6374 uint8_t ea[ETH_ADDR_LEN];
6377 error = netdev_get_etheraddr(port->netdev, ea);
6379 return eth_addr_to_uint64(ea);
6381 VLOG_WARN("%s: could not get MAC address for %s (%s)",
6382 ofproto->name, netdev_get_name(port->netdev),
6383 ovs_strerror(error));
6385 return ofproto->fallback_dpid;
6389 pick_fallback_dpid(void)
6391 uint8_t ea[ETH_ADDR_LEN];
6392 eth_addr_nicira_random(ea);
6393 return eth_addr_to_uint64(ea);
6396 /* Table overflow policy. */
6398 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
6399 * to NULL if the table is not configured to evict rules or if the table
6400 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
6401 * or with no timeouts are not evictable.) */
6403 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
6404 OVS_REQUIRES(ofproto_mutex)
6406 struct eviction_group *evg;
6409 if (!table->eviction_fields) {
6413 /* In the common case, the outer and inner loops here will each be entered
6416 * - The inner loop normally "return"s in its first iteration. If the
6417 * eviction group has any evictable rules, then it always returns in
6420 * - The outer loop only iterates more than once if the largest eviction
6421 * group has no evictable rules.
6423 * - The outer loop can exit only if table's 'max_flows' is all filled up
6424 * by unevictable rules. */
6425 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
6428 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
6437 /* Searches 'ofproto' for tables that have more flows than their configured
6438 * maximum and that have flow eviction enabled, and evicts as many flows as
6439 * necessary and currently feasible from them.
6441 * This triggers only when an OpenFlow table has N flows in it and then the
6442 * client configures a maximum number of flows less than N. */
6444 ofproto_evict(struct ofproto *ofproto)
6446 struct oftable *table;
6448 ovs_mutex_lock(&ofproto_mutex);
6449 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
6450 evict_rules_from_table(ofproto, table, 0);
6452 ovs_mutex_unlock(&ofproto_mutex);
6455 /* Eviction groups. */
6457 /* Returns the priority to use for an eviction_group that contains 'n_rules'
6458 * rules. The priority contains low-order random bits to ensure that eviction
6459 * groups with the same number of rules are prioritized randomly. */
6461 eviction_group_priority(size_t n_rules)
6463 uint16_t size = MIN(UINT16_MAX, n_rules);
6464 return (size << 16) | random_uint16();
6467 /* Updates 'evg', an eviction_group within 'table', following a change that
6468 * adds or removes rules in 'evg'. */
6470 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
6471 OVS_REQUIRES(ofproto_mutex)
6473 heap_change(&table->eviction_groups_by_size, &evg->size_node,
6474 eviction_group_priority(heap_count(&evg->rules)));
6477 /* Destroys 'evg', an eviction_group within 'table':
6479 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
6480 * rules themselves, just removes them from the eviction group.)
6482 * - Removes 'evg' from 'table'.
6486 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
6487 OVS_REQUIRES(ofproto_mutex)
6489 while (!heap_is_empty(&evg->rules)) {
6492 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
6493 rule->eviction_group = NULL;
6495 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
6496 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
6497 heap_destroy(&evg->rules);
6501 /* Removes 'rule' from its eviction group, if any. */
6503 eviction_group_remove_rule(struct rule *rule)
6504 OVS_REQUIRES(ofproto_mutex)
6506 if (rule->eviction_group) {
6507 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6508 struct eviction_group *evg = rule->eviction_group;
6510 rule->eviction_group = NULL;
6511 heap_remove(&evg->rules, &rule->evg_node);
6512 if (heap_is_empty(&evg->rules)) {
6513 eviction_group_destroy(table, evg);
6515 eviction_group_resized(table, evg);
6520 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6521 * returns the hash value. */
6523 eviction_group_hash_rule(struct rule *rule)
6524 OVS_REQUIRES(ofproto_mutex)
6526 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6527 const struct mf_subfield *sf;
6531 hash = table->eviction_group_id_basis;
6532 miniflow_expand(&rule->cr.match.flow, &flow);
6533 for (sf = table->eviction_fields;
6534 sf < &table->eviction_fields[table->n_eviction_fields];
6537 if (mf_are_prereqs_ok(sf->field, &flow)) {
6538 union mf_value value;
6540 mf_get_value(sf->field, &flow, &value);
6542 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
6544 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
6545 unsigned int start = sf->ofs + sf->n_bits;
6546 bitwise_zero(&value, sf->field->n_bytes, start,
6547 sf->field->n_bytes * 8 - start);
6549 hash = hash_bytes(&value, sf->field->n_bytes, hash);
6551 hash = hash_int(hash, 0);
6558 /* Returns an eviction group within 'table' with the given 'id', creating one
6560 static struct eviction_group *
6561 eviction_group_find(struct oftable *table, uint32_t id)
6562 OVS_REQUIRES(ofproto_mutex)
6564 struct eviction_group *evg;
6566 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6570 evg = xmalloc(sizeof *evg);
6571 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6572 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6573 eviction_group_priority(0));
6574 heap_init(&evg->rules);
6579 /* Returns an eviction priority for 'rule'. The return value should be
6580 * interpreted so that higher priorities make a rule more attractive candidates
6583 rule_eviction_priority(struct rule *rule)
6584 OVS_REQUIRES(ofproto_mutex)
6586 long long int hard_expiration;
6587 long long int idle_expiration;
6588 long long int expiration;
6589 uint32_t expiration_offset;
6591 /* Calculate time of expiration. */
6592 ovs_mutex_lock(&rule->mutex);
6593 hard_expiration = (rule->hard_timeout
6594 ? rule->modified + rule->hard_timeout * 1000
6596 idle_expiration = (rule->idle_timeout
6597 ? rule->used + rule->idle_timeout * 1000
6599 expiration = MIN(hard_expiration, idle_expiration);
6600 ovs_mutex_unlock(&rule->mutex);
6601 if (expiration == LLONG_MAX) {
6605 /* Calculate the time of expiration as a number of (approximate) seconds
6606 * after program startup.
6608 * This should work OK for program runs that last UINT32_MAX seconds or
6609 * less. Therefore, please restart OVS at least once every 136 years. */
6610 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6612 /* Invert the expiration offset because we're using a max-heap. */
6613 return UINT32_MAX - expiration_offset;
6616 /* Adds 'rule' to an appropriate eviction group for its oftable's
6617 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6618 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6621 * The caller must ensure that 'rule' is not already in an eviction group. */
6623 eviction_group_add_rule(struct rule *rule)
6624 OVS_REQUIRES(ofproto_mutex)
6626 struct ofproto *ofproto = rule->ofproto;
6627 struct oftable *table = &ofproto->tables[rule->table_id];
6630 ovs_mutex_lock(&rule->mutex);
6631 has_timeout = rule->hard_timeout || rule->idle_timeout;
6632 ovs_mutex_unlock(&rule->mutex);
6634 if (table->eviction_fields && has_timeout) {
6635 struct eviction_group *evg;
6637 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6639 rule->eviction_group = evg;
6640 heap_insert(&evg->rules, &rule->evg_node,
6641 rule_eviction_priority(rule));
6642 eviction_group_resized(table, evg);
6648 /* Initializes 'table'. */
6650 oftable_init(struct oftable *table)
6652 memset(table, 0, sizeof *table);
6653 classifier_init(&table->cls, flow_segment_u32s);
6654 table->max_flows = UINT_MAX;
6657 /* Destroys 'table', including its classifier and eviction groups.
6659 * The caller is responsible for freeing 'table' itself. */
6661 oftable_destroy(struct oftable *table)
6663 ovs_rwlock_rdlock(&table->cls.rwlock);
6664 ovs_assert(classifier_is_empty(&table->cls));
6665 ovs_rwlock_unlock(&table->cls.rwlock);
6666 oftable_disable_eviction(table);
6667 classifier_destroy(&table->cls);
6671 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6672 * string, then 'table' will use its default name.
6674 * This only affects the name exposed for a table exposed through the OpenFlow
6675 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6677 oftable_set_name(struct oftable *table, const char *name)
6679 if (name && name[0]) {
6680 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6681 if (!table->name || strncmp(name, table->name, len)) {
6683 table->name = xmemdup0(name, len);
6691 /* oftables support a choice of two policies when adding a rule would cause the
6692 * number of flows in the table to exceed the configured maximum number: either
6693 * they can refuse to add the new flow or they can evict some existing flow.
6694 * This function configures the former policy on 'table'. */
6696 oftable_disable_eviction(struct oftable *table)
6697 OVS_REQUIRES(ofproto_mutex)
6699 if (table->eviction_fields) {
6700 struct eviction_group *evg, *next;
6702 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6703 &table->eviction_groups_by_id) {
6704 eviction_group_destroy(table, evg);
6706 hmap_destroy(&table->eviction_groups_by_id);
6707 heap_destroy(&table->eviction_groups_by_size);
6709 free(table->eviction_fields);
6710 table->eviction_fields = NULL;
6711 table->n_eviction_fields = 0;
6715 /* oftables support a choice of two policies when adding a rule would cause the
6716 * number of flows in the table to exceed the configured maximum number: either
6717 * they can refuse to add the new flow or they can evict some existing flow.
6718 * This function configures the latter policy on 'table', with fairness based
6719 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6720 * 'n_fields' as 0 disables fairness.) */
6722 oftable_enable_eviction(struct oftable *table,
6723 const struct mf_subfield *fields, size_t n_fields)
6724 OVS_REQUIRES(ofproto_mutex)
6726 struct cls_cursor cursor;
6729 if (table->eviction_fields
6730 && n_fields == table->n_eviction_fields
6732 || !memcmp(fields, table->eviction_fields,
6733 n_fields * sizeof *fields))) {
6738 oftable_disable_eviction(table);
6740 table->n_eviction_fields = n_fields;
6741 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6743 table->eviction_group_id_basis = random_uint32();
6744 hmap_init(&table->eviction_groups_by_id);
6745 heap_init(&table->eviction_groups_by_size);
6747 ovs_rwlock_rdlock(&table->cls.rwlock);
6748 cls_cursor_init(&cursor, &table->cls, NULL);
6749 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6750 eviction_group_add_rule(rule);
6752 ovs_rwlock_unlock(&table->cls.rwlock);
6755 /* Removes 'rule' from the oftable that contains it. */
6757 oftable_remove_rule__(struct ofproto *ofproto, struct rule *rule)
6758 OVS_REQUIRES(ofproto_mutex)
6760 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
6762 ovs_rwlock_wrlock(&cls->rwlock);
6763 classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
6764 ovs_rwlock_unlock(&cls->rwlock);
6766 cookies_remove(ofproto, rule);
6768 eviction_group_remove_rule(rule);
6769 if (!list_is_empty(&rule->expirable)) {
6770 list_remove(&rule->expirable);
6772 if (!list_is_empty(&rule->meter_list_node)) {
6773 list_remove(&rule->meter_list_node);
6774 list_init(&rule->meter_list_node);
6779 oftable_remove_rule(struct rule *rule)
6780 OVS_REQUIRES(ofproto_mutex)
6782 oftable_remove_rule__(rule->ofproto, rule);
6785 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6786 * the same cls_rule. */
6788 oftable_insert_rule(struct rule *rule)
6789 OVS_REQUIRES(ofproto_mutex)
6791 struct ofproto *ofproto = rule->ofproto;
6792 struct oftable *table = &ofproto->tables[rule->table_id];
6795 ovs_mutex_lock(&rule->mutex);
6796 may_expire = rule->hard_timeout || rule->idle_timeout;
6797 ovs_mutex_unlock(&rule->mutex);
6800 list_insert(&ofproto->expirable, &rule->expirable);
6803 cookies_insert(ofproto, rule);
6805 if (rule->actions->provider_meter_id != UINT32_MAX) {
6806 uint32_t meter_id = ofpacts_get_meter(rule->actions->ofpacts,
6807 rule->actions->ofpacts_len);
6808 struct meter *meter = ofproto->meters[meter_id];
6809 list_insert(&meter->rules, &rule->meter_list_node);
6811 ovs_rwlock_wrlock(&table->cls.rwlock);
6812 classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
6813 ovs_rwlock_unlock(&table->cls.rwlock);
6814 eviction_group_add_rule(rule);
6817 /* unixctl commands. */
6820 ofproto_lookup(const char *name)
6822 struct ofproto *ofproto;
6824 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6826 if (!strcmp(ofproto->name, name)) {
6834 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6835 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6837 struct ofproto *ofproto;
6841 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6842 ds_put_format(&results, "%s\n", ofproto->name);
6844 unixctl_command_reply(conn, ds_cstr(&results));
6845 ds_destroy(&results);
6849 ofproto_unixctl_init(void)
6851 static bool registered;
6857 unixctl_command_register("ofproto/list", "", 0, 0,
6858 ofproto_unixctl_list, NULL);
6861 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6863 * This is deprecated. It is only for compatibility with broken device drivers
6864 * in old versions of Linux that do not properly support VLANs when VLAN
6865 * devices are not used. When broken device drivers are no longer in
6866 * widespread use, we will delete these interfaces. */
6868 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6869 * (exactly) by an OpenFlow rule in 'ofproto'. */
6871 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6873 const struct oftable *oftable;
6875 free(ofproto->vlan_bitmap);
6876 ofproto->vlan_bitmap = bitmap_allocate(4096);
6877 ofproto->vlans_changed = false;
6879 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6880 const struct cls_subtable *table;
6882 ovs_rwlock_rdlock(&oftable->cls.rwlock);
6883 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.subtables) {
6884 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6885 const struct cls_rule *rule;
6887 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6888 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6889 bitmap_set1(vlan_bitmap, vid);
6890 bitmap_set1(ofproto->vlan_bitmap, vid);
6894 ovs_rwlock_unlock(&oftable->cls.rwlock);
6898 /* Returns true if new VLANs have come into use by the flow table since the
6899 * last call to ofproto_get_vlan_usage().
6901 * We don't track when old VLANs stop being used. */
6903 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6905 return ofproto->vlans_changed;
6908 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6909 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6910 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6911 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6912 * then the VLAN device is un-enslaved. */
6914 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6915 ofp_port_t realdev_ofp_port, int vid)
6917 struct ofport *ofport;
6920 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6922 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6924 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6925 ofproto->name, vlandev_ofp_port);
6929 if (!ofproto->ofproto_class->set_realdev) {
6930 if (!vlandev_ofp_port) {
6933 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6937 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6939 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6940 ofproto->name, vlandev_ofp_port,
6941 netdev_get_name(ofport->netdev), ovs_strerror(error));