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"
28 #include "connectivity.h"
31 #include "dynamic-string.h"
34 #include "meta-flow.h"
37 #include "ofp-actions.h"
38 #include "ofp-errors.h"
40 #include "ofp-print.h"
43 #include "ofproto-provider.h"
44 #include "openflow/nicira-ext.h"
45 #include "openflow/openflow.h"
49 #include "poll-loop.h"
57 #include "unaligned.h"
61 VLOG_DEFINE_THIS_MODULE(ofproto);
63 COVERAGE_DEFINE(ofproto_flush);
64 COVERAGE_DEFINE(ofproto_packet_out);
65 COVERAGE_DEFINE(ofproto_queue_req);
66 COVERAGE_DEFINE(ofproto_recv_openflow);
67 COVERAGE_DEFINE(ofproto_reinit_ports);
68 COVERAGE_DEFINE(ofproto_update_port);
71 S_OPENFLOW, /* Processing OpenFlow commands. */
72 S_EVICT, /* Evicting flows from over-limit tables. */
73 S_FLUSH, /* Deleting all flow table rules. */
76 enum ofoperation_type {
83 /* A single OpenFlow request can execute any number of operations. The
84 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
85 * ofconn to which an error reply should be sent if necessary.
87 * ofproto initiates some operations internally. These operations are still
88 * assigned to groups but will not have an associated ofconn. */
90 struct ofproto *ofproto; /* Owning ofproto. */
91 struct list ofproto_node; /* In ofproto's "pending" list. */
92 struct list ops; /* List of "struct ofoperation"s. */
93 int n_running; /* Number of ops still pending. */
95 /* Data needed to send OpenFlow reply on failure or to send a buffered
98 * If list_is_empty(ofconn_node) then this ofopgroup never had an
99 * associated ofconn or its ofconn's connection dropped after it initiated
100 * the operation. In the latter case 'ofconn' is a wild pointer that
101 * refers to freed memory, so the 'ofconn' member must be used only if
102 * !list_is_empty(ofconn_node).
104 struct list ofconn_node; /* In ofconn's list of pending opgroups. */
105 struct ofconn *ofconn; /* ofconn for reply (but see note above). */
106 struct ofp_header *request; /* Original request (truncated at 64 bytes). */
107 uint32_t buffer_id; /* Buffer id from original request. */
110 static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *);
111 static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *,
112 const struct ofp_header *,
114 static void ofopgroup_submit(struct ofopgroup *);
115 static void ofopgroup_complete(struct ofopgroup *);
117 /* A single flow table operation. */
119 struct ofopgroup *group; /* Owning group. */
120 struct list group_node; /* In ofopgroup's "ops" list. */
121 struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */
122 struct rule *rule; /* Rule being operated upon. */
123 enum ofoperation_type type; /* Type of operation. */
125 /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions
127 struct rule_actions *actions;
129 /* OFOPERATION_DELETE. */
130 enum ofp_flow_removed_reason reason; /* Reason flow was removed. */
132 ovs_be64 flow_cookie; /* Rule's old flow cookie. */
133 uint16_t idle_timeout; /* Rule's old idle timeout. */
134 uint16_t hard_timeout; /* Rule's old hard timeout. */
135 enum ofputil_flow_mod_flags flags; /* Rule's old flags. */
136 enum ofperr error; /* 0 if no error. */
139 static struct ofoperation *ofoperation_create(struct ofopgroup *,
141 enum ofoperation_type,
142 enum ofp_flow_removed_reason);
143 static void ofoperation_destroy(struct ofoperation *);
146 static void oftable_init(struct oftable *);
147 static void oftable_destroy(struct oftable *);
149 static void oftable_set_name(struct oftable *, const char *name);
151 static void oftable_disable_eviction(struct oftable *);
152 static void oftable_enable_eviction(struct oftable *,
153 const struct mf_subfield *fields,
156 static void oftable_remove_rule(struct rule *rule) OVS_REQUIRES(ofproto_mutex);
157 static void oftable_remove_rule__(struct ofproto *, struct rule *)
158 OVS_REQUIRES(ofproto_mutex);
159 static void oftable_insert_rule(struct rule *);
161 /* A set of rules within a single OpenFlow table (oftable) that have the same
162 * values for the oftable's eviction_fields. A rule to be evicted, when one is
163 * needed, is taken from the eviction group that contains the greatest number
166 * An oftable owns any number of eviction groups, each of which contains any
169 * Membership in an eviction group is imprecise, based on the hash of the
170 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
171 * That is, if two rules have different eviction_fields, but those
172 * eviction_fields hash to the same value, then they will belong to the same
173 * eviction_group anyway.
175 * (When eviction is not enabled on an oftable, we don't track any eviction
176 * groups, to save time and space.) */
177 struct eviction_group {
178 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
179 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
180 struct heap rules; /* Contains "struct rule"s. */
183 static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep);
184 static void ofproto_evict(struct ofproto *) OVS_EXCLUDED(ofproto_mutex);
185 static uint32_t rule_eviction_priority(struct rule *);
186 static void eviction_group_add_rule(struct rule *);
187 static void eviction_group_remove_rule(struct rule *);
189 /* Criteria that flow_mod and other operations use for selecting rules on
190 * which to operate. */
191 struct rule_criteria {
192 /* An OpenFlow table or 255 for all tables. */
195 /* OpenFlow matching criteria. Interpreted different in "loose" way by
196 * collect_rules_loose() and "strict" way by collect_rules_strict(), as
197 * defined in the OpenFlow spec. */
200 /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's
201 * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'.
202 * The rule will not be selected if M is 1 and B != C. */
204 ovs_be64 cookie_mask;
206 /* Selection based on actions within a rule:
208 * If out_port != OFPP_ANY, selects only rules that output to out_port.
209 * If out_group != OFPG_ALL, select only rules that output to out_group. */
214 static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
215 const struct match *match,
216 unsigned int priority,
217 ovs_be64 cookie, ovs_be64 cookie_mask,
218 ofp_port_t out_port, uint32_t out_group);
219 static void rule_criteria_destroy(struct rule_criteria *);
221 /* A packet that needs to be passed to rule_execute().
223 * (We can't do this immediately from ofopgroup_complete() because that holds
224 * ofproto_mutex, which rule_execute() needs released.) */
225 struct rule_execute {
226 struct list list_node; /* In struct ofproto's "rule_executes" list. */
227 struct rule *rule; /* Owns a reference to the rule. */
229 struct ofpbuf *packet; /* Owns the packet. */
232 static void run_rule_executes(struct ofproto *) OVS_EXCLUDED(ofproto_mutex);
233 static void destroy_rule_executes(struct ofproto *);
236 static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex);
237 static void ofport_destroy(struct ofport *);
239 static void update_port(struct ofproto *, const char *devname);
240 static int init_ports(struct ofproto *);
241 static void reinit_ports(struct ofproto *);
243 static long long int ofport_get_usage(const struct ofproto *,
244 ofp_port_t ofp_port);
245 static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port,
246 long long int last_used);
247 static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port);
251 * Keeps track of the currently used and recently used ofport values and is
252 * used to prevent immediate recycling of ofport values. */
253 struct ofport_usage {
254 struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */
255 ofp_port_t ofp_port; /* OpenFlow port number. */
256 long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX
257 represents in-use ofports. */
261 static void ofproto_rule_destroy__(struct rule *);
262 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
263 static bool rule_is_modifiable(const struct rule *);
266 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
267 struct ofputil_flow_mod *,
268 const struct ofp_header *);
269 static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
270 struct ofputil_flow_mod *,
271 const struct ofp_header *,
272 const struct rule_collection *);
273 static void delete_flow__(struct rule *rule, struct ofopgroup *,
274 enum ofp_flow_removed_reason)
275 OVS_REQUIRES(ofproto_mutex);
276 static bool ofproto_group_exists__(const struct ofproto *ofproto,
278 OVS_REQ_RDLOCK(ofproto->groups_rwlock);
279 static bool ofproto_group_exists(const struct ofproto *ofproto,
281 OVS_EXCLUDED(ofproto->groups_rwlock);
282 static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
283 static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
284 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
285 struct ofputil_flow_mod *,
286 const struct ofp_header *)
287 OVS_EXCLUDED(ofproto_mutex);
288 static void calc_duration(long long int start, long long int now,
289 uint32_t *sec, uint32_t *nsec);
292 static uint64_t pick_datapath_id(const struct ofproto *);
293 static uint64_t pick_fallback_dpid(void);
294 static void ofproto_destroy__(struct ofproto *);
295 static void update_mtu(struct ofproto *, struct ofport *);
296 static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
299 static void ofproto_unixctl_init(void);
301 /* All registered ofproto classes, in probe order. */
302 static const struct ofproto_class **ofproto_classes;
303 static size_t n_ofproto_classes;
304 static size_t allocated_ofproto_classes;
306 /* Global lock that protects all flow table operations. */
307 struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER;
309 unsigned ofproto_flow_limit = OFPROTO_FLOW_LIMIT_DEFAULT;
310 enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO;
312 size_t n_handlers, n_revalidators;
314 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
315 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
317 /* Initial mappings of port to OpenFlow number mappings. */
318 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
320 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
322 /* The default value of true waits for flow restore. */
323 static bool flow_restore_wait = true;
325 /* Must be called to initialize the ofproto library.
327 * The caller may pass in 'iface_hints', which contains an shash of
328 * "iface_hint" elements indexed by the interface's name. The provider
329 * may use these hints to describe the startup configuration in order to
330 * reinitialize its state. The caller owns the provided data, so a
331 * provider will make copies of anything required. An ofproto provider
332 * will remove any existing state that is not described by the hint, and
333 * may choose to remove it all. */
335 ofproto_init(const struct shash *iface_hints)
337 struct shash_node *node;
340 ofproto_class_register(&ofproto_dpif_class);
342 /* Make a local copy, since we don't own 'iface_hints' elements. */
343 SHASH_FOR_EACH(node, iface_hints) {
344 const struct iface_hint *orig_hint = node->data;
345 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
346 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
348 new_hint->br_name = xstrdup(orig_hint->br_name);
349 new_hint->br_type = xstrdup(br_type);
350 new_hint->ofp_port = orig_hint->ofp_port;
352 shash_add(&init_ofp_ports, node->name, new_hint);
355 for (i = 0; i < n_ofproto_classes; i++) {
356 ofproto_classes[i]->init(&init_ofp_ports);
360 /* 'type' should be a normalized datapath type, as returned by
361 * ofproto_normalize_type(). Returns the corresponding ofproto_class
362 * structure, or a null pointer if there is none registered for 'type'. */
363 static const struct ofproto_class *
364 ofproto_class_find__(const char *type)
368 for (i = 0; i < n_ofproto_classes; i++) {
369 const struct ofproto_class *class = ofproto_classes[i];
374 class->enumerate_types(&types);
375 found = sset_contains(&types, type);
376 sset_destroy(&types);
382 VLOG_WARN("unknown datapath type %s", type);
386 /* Registers a new ofproto class. After successful registration, new ofprotos
387 * of that type can be created using ofproto_create(). */
389 ofproto_class_register(const struct ofproto_class *new_class)
393 for (i = 0; i < n_ofproto_classes; i++) {
394 if (ofproto_classes[i] == new_class) {
399 if (n_ofproto_classes >= allocated_ofproto_classes) {
400 ofproto_classes = x2nrealloc(ofproto_classes,
401 &allocated_ofproto_classes,
402 sizeof *ofproto_classes);
404 ofproto_classes[n_ofproto_classes++] = new_class;
408 /* Unregisters a datapath provider. 'type' must have been previously
409 * registered and not currently be in use by any ofprotos. After
410 * unregistration new datapaths of that type cannot be opened using
411 * ofproto_create(). */
413 ofproto_class_unregister(const struct ofproto_class *class)
417 for (i = 0; i < n_ofproto_classes; i++) {
418 if (ofproto_classes[i] == class) {
419 for (i++; i < n_ofproto_classes; i++) {
420 ofproto_classes[i - 1] = ofproto_classes[i];
426 VLOG_WARN("attempted to unregister an ofproto class that is not "
431 /* Clears 'types' and enumerates all registered ofproto types into it. The
432 * caller must first initialize the sset. */
434 ofproto_enumerate_types(struct sset *types)
439 for (i = 0; i < n_ofproto_classes; i++) {
440 ofproto_classes[i]->enumerate_types(types);
444 /* Returns the fully spelled out name for the given ofproto 'type'.
446 * Normalized type string can be compared with strcmp(). Unnormalized type
447 * string might be the same even if they have different spellings. */
449 ofproto_normalize_type(const char *type)
451 return type && type[0] ? type : "system";
454 /* Clears 'names' and enumerates the names of all known created ofprotos with
455 * the given 'type'. The caller must first initialize the sset. Returns 0 if
456 * successful, otherwise a positive errno value.
458 * Some kinds of datapaths might not be practically enumerable. This is not
459 * considered an error. */
461 ofproto_enumerate_names(const char *type, struct sset *names)
463 const struct ofproto_class *class = ofproto_class_find__(type);
464 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
468 ofproto_create(const char *datapath_name, const char *datapath_type,
469 struct ofproto **ofprotop)
471 const struct ofproto_class *class;
472 struct ofproto *ofproto;
478 ofproto_unixctl_init();
480 datapath_type = ofproto_normalize_type(datapath_type);
481 class = ofproto_class_find__(datapath_type);
483 VLOG_WARN("could not create datapath %s of unknown type %s",
484 datapath_name, datapath_type);
488 ofproto = class->alloc();
490 VLOG_ERR("failed to allocate datapath %s of type %s",
491 datapath_name, datapath_type);
496 ovs_mutex_lock(&ofproto_mutex);
497 memset(ofproto, 0, sizeof *ofproto);
498 ofproto->ofproto_class = class;
499 ofproto->name = xstrdup(datapath_name);
500 ofproto->type = xstrdup(datapath_type);
501 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
502 hash_string(ofproto->name, 0));
503 ofproto->datapath_id = 0;
504 ofproto->forward_bpdu = false;
505 ofproto->fallback_dpid = pick_fallback_dpid();
506 ofproto->mfr_desc = NULL;
507 ofproto->hw_desc = NULL;
508 ofproto->sw_desc = NULL;
509 ofproto->serial_desc = NULL;
510 ofproto->dp_desc = NULL;
511 ofproto->frag_handling = OFPC_FRAG_NORMAL;
512 hmap_init(&ofproto->ports);
513 hmap_init(&ofproto->ofport_usage);
514 shash_init(&ofproto->port_by_name);
515 simap_init(&ofproto->ofp_requests);
516 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
517 ofproto->eviction_group_timer = LLONG_MIN;
518 ofproto->tables = NULL;
519 ofproto->n_tables = 0;
520 hindex_init(&ofproto->cookies);
521 list_init(&ofproto->expirable);
522 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
523 ofproto->state = S_OPENFLOW;
524 list_init(&ofproto->pending);
525 ofproto->n_pending = 0;
526 hmap_init(&ofproto->deletions);
527 guarded_list_init(&ofproto->rule_executes);
528 ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0;
529 ofproto->first_op = ofproto->last_op = LLONG_MIN;
530 ofproto->next_op_report = LLONG_MAX;
531 ofproto->op_backoff = LLONG_MIN;
532 ofproto->vlan_bitmap = NULL;
533 ofproto->vlans_changed = false;
534 ofproto->min_mtu = INT_MAX;
535 ovs_rwlock_init(&ofproto->groups_rwlock);
536 hmap_init(&ofproto->groups);
537 ovs_mutex_unlock(&ofproto_mutex);
538 ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS |
539 OFPGFC_SELECT_WEIGHT;
540 ofproto->ogf.max_groups[OFPGT11_ALL] = OFPG_MAX;
541 ofproto->ogf.max_groups[OFPGT11_SELECT] = OFPG_MAX;
542 ofproto->ogf.max_groups[OFPGT11_INDIRECT] = OFPG_MAX;
543 ofproto->ogf.max_groups[OFPGT11_FF] = OFPG_MAX;
544 ofproto->ogf.actions[0] =
545 (1 << OFPAT11_OUTPUT) |
546 (1 << OFPAT11_COPY_TTL_OUT) |
547 (1 << OFPAT11_COPY_TTL_IN) |
548 (1 << OFPAT11_SET_MPLS_TTL) |
549 (1 << OFPAT11_DEC_MPLS_TTL) |
550 (1 << OFPAT11_PUSH_VLAN) |
551 (1 << OFPAT11_POP_VLAN) |
552 (1 << OFPAT11_PUSH_MPLS) |
553 (1 << OFPAT11_POP_MPLS) |
554 (1 << OFPAT11_SET_QUEUE) |
555 (1 << OFPAT11_GROUP) |
556 (1 << OFPAT11_SET_NW_TTL) |
557 (1 << OFPAT11_DEC_NW_TTL) |
558 (1 << OFPAT12_SET_FIELD);
560 * (1 << OFPAT13_PUSH_PBB) |
561 * (1 << OFPAT13_POP_PBB) */
563 error = ofproto->ofproto_class->construct(ofproto);
565 VLOG_ERR("failed to open datapath %s: %s",
566 datapath_name, ovs_strerror(error));
567 ofproto_destroy__(ofproto);
571 /* Check that hidden tables, if any, are at the end. */
572 ovs_assert(ofproto->n_tables);
573 for (i = 0; i + 1 < ofproto->n_tables; i++) {
574 enum oftable_flags flags = ofproto->tables[i].flags;
575 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
577 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
580 ofproto->datapath_id = pick_datapath_id(ofproto);
583 /* Initialize meters table. */
584 if (ofproto->ofproto_class->meter_get_features) {
585 ofproto->ofproto_class->meter_get_features(ofproto,
586 &ofproto->meter_features);
588 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
590 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
591 * sizeof(struct meter *));
597 /* Must be called (only) by an ofproto implementation in its constructor
598 * function. See the large comment on 'construct' in struct ofproto_class for
601 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
603 struct oftable *table;
605 ovs_assert(!ofproto->n_tables);
606 ovs_assert(n_tables >= 1 && n_tables <= 255);
608 ofproto->n_tables = n_tables;
609 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
610 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
615 /* To be optionally called (only) by an ofproto implementation in its
616 * constructor function. See the large comment on 'construct' in struct
617 * ofproto_class for details.
619 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
620 * will then ensure that actions passed into the ofproto implementation will
621 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
622 * function is not called, there will be no such restriction.
624 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
625 * the 'max_ports' restriction. */
627 ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
629 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
630 ofproto->max_ports = max_ports;
634 ofproto_get_datapath_id(const struct ofproto *ofproto)
636 return ofproto->datapath_id;
640 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
642 uint64_t old_dpid = p->datapath_id;
643 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
644 if (p->datapath_id != old_dpid) {
645 /* Force all active connections to reconnect, since there is no way to
646 * notify a controller that the datapath ID has changed. */
647 ofproto_reconnect_controllers(p);
652 ofproto_set_controllers(struct ofproto *p,
653 const struct ofproto_controller *controllers,
654 size_t n_controllers, uint32_t allowed_versions)
656 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
661 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
663 connmgr_set_fail_mode(p->connmgr, fail_mode);
666 /* Drops the connections between 'ofproto' and all of its controllers, forcing
667 * them to reconnect. */
669 ofproto_reconnect_controllers(struct ofproto *ofproto)
671 connmgr_reconnect(ofproto->connmgr);
674 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
675 * in-band control should guarantee access, in the same way that in-band
676 * control guarantees access to OpenFlow controllers. */
678 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
679 const struct sockaddr_in *extras, size_t n)
681 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
684 /* Sets the OpenFlow queue used by flows set up by in-band control on
685 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
686 * flows will use the default queue. */
688 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
690 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
693 /* Sets the number of flows at which eviction from the kernel flow table
696 ofproto_set_flow_limit(unsigned limit)
698 ofproto_flow_limit = limit;
701 /* Sets the path for handling flow misses. */
703 ofproto_set_flow_miss_model(unsigned model)
705 flow_miss_model = model;
708 /* If forward_bpdu is true, the NORMAL action will forward frames with
709 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
710 * the NORMAL action will drop these frames. */
712 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
714 bool old_val = ofproto->forward_bpdu;
715 ofproto->forward_bpdu = forward_bpdu;
716 if (old_val != ofproto->forward_bpdu) {
717 if (ofproto->ofproto_class->forward_bpdu_changed) {
718 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
723 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
724 * 'idle_time', in seconds, and the maximum number of MAC table entries to
727 ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
730 if (ofproto->ofproto_class->set_mac_table_config) {
731 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
737 ofproto_set_threads(size_t n_handlers_, size_t n_revalidators_)
739 int threads = MAX(count_cpu_cores(), 2);
741 n_revalidators = n_revalidators_;
742 n_handlers = n_handlers_;
744 if (!n_revalidators) {
745 n_revalidators = n_handlers
746 ? MAX(threads - (int) n_handlers, 1)
751 n_handlers = MAX(threads - (int) n_revalidators, 1);
756 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
759 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
763 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
765 return connmgr_set_snoops(ofproto->connmgr, snoops);
769 ofproto_set_netflow(struct ofproto *ofproto,
770 const struct netflow_options *nf_options)
772 if (nf_options && sset_is_empty(&nf_options->collectors)) {
776 if (ofproto->ofproto_class->set_netflow) {
777 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
779 return nf_options ? EOPNOTSUPP : 0;
784 ofproto_set_sflow(struct ofproto *ofproto,
785 const struct ofproto_sflow_options *oso)
787 if (oso && sset_is_empty(&oso->targets)) {
791 if (ofproto->ofproto_class->set_sflow) {
792 return ofproto->ofproto_class->set_sflow(ofproto, oso);
794 return oso ? EOPNOTSUPP : 0;
799 ofproto_set_ipfix(struct ofproto *ofproto,
800 const struct ofproto_ipfix_bridge_exporter_options *bo,
801 const struct ofproto_ipfix_flow_exporter_options *fo,
804 if (ofproto->ofproto_class->set_ipfix) {
805 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
807 return (bo || fo) ? EOPNOTSUPP : 0;
812 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
814 flow_restore_wait = flow_restore_wait_db;
818 ofproto_get_flow_restore_wait(void)
820 return flow_restore_wait;
824 /* Spanning Tree Protocol (STP) configuration. */
826 /* Configures STP on 'ofproto' using the settings defined in 's'. If
827 * 's' is NULL, disables STP.
829 * Returns 0 if successful, otherwise a positive errno value. */
831 ofproto_set_stp(struct ofproto *ofproto,
832 const struct ofproto_stp_settings *s)
834 return (ofproto->ofproto_class->set_stp
835 ? ofproto->ofproto_class->set_stp(ofproto, s)
839 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
840 * 'enabled' member of 's' is false, then the other members are not
843 * Returns 0 if successful, otherwise a positive errno value. */
845 ofproto_get_stp_status(struct ofproto *ofproto,
846 struct ofproto_stp_status *s)
848 return (ofproto->ofproto_class->get_stp_status
849 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
853 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
854 * in 's'. The caller is responsible for assigning STP port numbers
855 * (using the 'port_num' member in the range of 1 through 255, inclusive)
856 * and ensuring there are no duplicates. If the 's' is NULL, then STP
857 * is disabled on the port.
859 * Returns 0 if successful, otherwise a positive errno value.*/
861 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
862 const struct ofproto_port_stp_settings *s)
864 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
866 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
867 ofproto->name, ofp_port);
871 return (ofproto->ofproto_class->set_stp_port
872 ? ofproto->ofproto_class->set_stp_port(ofport, s)
876 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
877 * 's'. If the 'enabled' member in 's' is false, then the other members
878 * are not meaningful.
880 * Returns 0 if successful, otherwise a positive errno value.*/
882 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
883 struct ofproto_port_stp_status *s)
885 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
887 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
888 "port %"PRIu16, ofproto->name, ofp_port);
892 return (ofproto->ofproto_class->get_stp_port_status
893 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
897 /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
898 * 's'. If the 'enabled' member in 's' is false, then the other members
899 * are not meaningful.
901 * Returns 0 if successful, otherwise a positive errno value.*/
903 ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port,
904 struct ofproto_port_stp_stats *s)
906 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
908 VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent "
909 "port %"PRIu16, ofproto->name, ofp_port);
913 return (ofproto->ofproto_class->get_stp_port_stats
914 ? ofproto->ofproto_class->get_stp_port_stats(ofport, s)
918 /* Queue DSCP configuration. */
920 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
921 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
922 * to implement QoS. Instead, it is used to implement features which require
923 * knowledge of what queues exist on a port, and some basic information about
926 * Returns 0 if successful, otherwise a positive errno value. */
928 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
929 const struct ofproto_port_queue *queues,
932 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
935 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
936 ofproto->name, ofp_port);
940 return (ofproto->ofproto_class->set_queues
941 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
945 /* Connectivity Fault Management configuration. */
947 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
949 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
951 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
952 if (ofport && ofproto->ofproto_class->set_cfm) {
953 ofproto->ofproto_class->set_cfm(ofport, NULL);
957 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
958 * basic configuration from the configuration members in 'cfm', and the remote
959 * maintenance point ID from remote_mpid. Ignores the statistics members of
962 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
964 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
965 const struct cfm_settings *s)
967 struct ofport *ofport;
970 ofport = ofproto_get_port(ofproto, ofp_port);
972 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
973 ofproto->name, ofp_port);
977 /* XXX: For configuration simplicity, we only support one remote_mpid
978 * outside of the CFM module. It's not clear if this is the correct long
979 * term solution or not. */
980 error = (ofproto->ofproto_class->set_cfm
981 ? ofproto->ofproto_class->set_cfm(ofport, s)
984 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
985 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
986 ovs_strerror(error));
990 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
991 * 'ofproto' does not have a port 'ofp_port'. */
993 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
994 const struct smap *cfg)
996 struct ofport *ofport;
999 ofport = ofproto_get_port(ofproto, ofp_port);
1001 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
1002 ofproto->name, ofp_port);
1006 error = (ofproto->ofproto_class->set_bfd
1007 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
1010 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
1011 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
1012 ovs_strerror(error));
1016 /* Populates 'status' with key value pairs indicating the status of the BFD
1017 * session on 'ofp_port'. This information is intended to be populated in the
1018 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
1021 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
1022 struct smap *status)
1024 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1025 return (ofport && ofproto->ofproto_class->get_bfd_status
1026 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
1030 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1031 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1032 * 0 if LACP partner information is not current (generally indicating a
1033 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1035 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
1037 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1038 return (ofport && ofproto->ofproto_class->port_is_lacp_current
1039 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
1045 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1046 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1047 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1048 * configuration plus, if there is more than one slave, a bonding
1051 * If 'aux' is already registered then this function updates its configuration
1052 * to 's'. Otherwise, this function registers a new bundle.
1054 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1057 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
1058 const struct ofproto_bundle_settings *s)
1060 return (ofproto->ofproto_class->bundle_set
1061 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
1065 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1066 * If no such bundle has been registered, this has no effect. */
1068 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
1070 return ofproto_bundle_register(ofproto, aux, NULL);
1074 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1075 * If 'aux' is already registered then this function updates its configuration
1076 * to 's'. Otherwise, this function registers a new mirror. */
1078 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1079 const struct ofproto_mirror_settings *s)
1081 return (ofproto->ofproto_class->mirror_set
1082 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1086 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1087 * If no mirror has been registered, this has no effect. */
1089 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
1091 return ofproto_mirror_register(ofproto, aux, NULL);
1094 /* Retrieves statistics from mirror associated with client data pointer
1095 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1096 * 'bytes', respectively. If a particular counters is not supported,
1097 * the appropriate argument is set to UINT64_MAX. */
1099 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1100 uint64_t *packets, uint64_t *bytes)
1102 if (!ofproto->ofproto_class->mirror_get_stats) {
1103 *packets = *bytes = UINT64_MAX;
1107 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1111 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1112 * which all packets are flooded, instead of using MAC learning. If
1113 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1115 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1118 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
1120 return (ofproto->ofproto_class->set_flood_vlans
1121 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1125 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1126 * output for a mirror. */
1128 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1130 return (ofproto->ofproto_class->is_mirror_output_bundle
1131 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1135 /* Configuration of OpenFlow tables. */
1137 /* Returns the number of OpenFlow tables in 'ofproto'. */
1139 ofproto_get_n_tables(const struct ofproto *ofproto)
1141 return ofproto->n_tables;
1144 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1145 * settings from 's'. 'table_id' must be in the range 0 through the number of
1146 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1148 * For read-only tables, only the name may be configured. */
1150 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1151 const struct ofproto_table_settings *s)
1153 struct oftable *table;
1155 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1156 table = &ofproto->tables[table_id];
1158 oftable_set_name(table, s->name);
1160 if (table->flags & OFTABLE_READONLY) {
1165 oftable_enable_eviction(table, s->groups, s->n_groups);
1167 oftable_disable_eviction(table);
1170 table->max_flows = s->max_flows;
1171 fat_rwlock_wrlock(&table->cls.rwlock);
1172 if (classifier_count(&table->cls) > table->max_flows
1173 && table->eviction_fields) {
1174 /* 'table' contains more flows than allowed. We might not be able to
1175 * evict them right away because of the asynchronous nature of flow
1176 * table changes. Schedule eviction for later. */
1177 switch (ofproto->state) {
1179 ofproto->state = S_EVICT;
1183 /* We're already deleting flows, nothing more to do. */
1188 classifier_set_prefix_fields(&table->cls,
1189 s->prefix_fields, s->n_prefix_fields);
1191 fat_rwlock_unlock(&table->cls.rwlock);
1195 ofproto_has_snoops(const struct ofproto *ofproto)
1197 return connmgr_has_snoops(ofproto->connmgr);
1201 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1203 connmgr_get_snoops(ofproto->connmgr, snoops);
1207 ofproto_rule_delete__(struct ofproto *ofproto, struct rule *rule,
1209 OVS_REQUIRES(ofproto_mutex)
1211 struct ofopgroup *group;
1213 ovs_assert(!rule->pending);
1215 group = ofopgroup_create_unattached(ofproto);
1216 delete_flow__(rule, group, reason);
1217 ofopgroup_submit(group);
1220 /* Deletes 'rule' from 'cls' within 'ofproto'.
1222 * Within an ofproto implementation, this function allows an ofproto
1223 * implementation to destroy any rules that remain when its ->destruct()
1224 * function is called. This function is not suitable for use elsewhere in an
1225 * ofproto implementation.
1227 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1228 * Cycle" in ofproto-provider.h. */
1230 ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule)
1231 OVS_EXCLUDED(ofproto_mutex)
1233 struct ofopgroup *group;
1235 ovs_mutex_lock(&ofproto_mutex);
1236 ovs_assert(!rule->pending);
1238 group = ofopgroup_create_unattached(ofproto);
1239 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1240 oftable_remove_rule__(ofproto, rule);
1241 ofproto->ofproto_class->rule_delete(rule);
1242 ofopgroup_submit(group);
1244 ovs_mutex_unlock(&ofproto_mutex);
1248 ofproto_flush__(struct ofproto *ofproto)
1249 OVS_EXCLUDED(ofproto_mutex)
1251 struct oftable *table;
1253 if (ofproto->ofproto_class->flush) {
1254 ofproto->ofproto_class->flush(ofproto);
1257 ovs_mutex_lock(&ofproto_mutex);
1258 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1259 struct rule *rule, *next_rule;
1260 struct cls_cursor cursor;
1262 if (table->flags & OFTABLE_HIDDEN) {
1266 fat_rwlock_rdlock(&table->cls.rwlock);
1267 cls_cursor_init(&cursor, &table->cls, NULL);
1268 fat_rwlock_unlock(&table->cls.rwlock);
1269 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1270 if (!rule->pending) {
1271 ofproto_rule_delete__(ofproto, rule, OFPRR_DELETE);
1275 ovs_mutex_unlock(&ofproto_mutex);
1278 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1281 ofproto_destroy__(struct ofproto *ofproto)
1282 OVS_EXCLUDED(ofproto_mutex)
1284 struct oftable *table;
1286 ovs_assert(list_is_empty(&ofproto->pending));
1288 destroy_rule_executes(ofproto);
1289 guarded_list_destroy(&ofproto->rule_executes);
1291 delete_group(ofproto, OFPG_ALL);
1292 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1293 hmap_destroy(&ofproto->groups);
1295 connmgr_destroy(ofproto->connmgr);
1297 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1298 free(ofproto->name);
1299 free(ofproto->type);
1300 free(ofproto->mfr_desc);
1301 free(ofproto->hw_desc);
1302 free(ofproto->sw_desc);
1303 free(ofproto->serial_desc);
1304 free(ofproto->dp_desc);
1305 hmap_destroy(&ofproto->ports);
1306 hmap_destroy(&ofproto->ofport_usage);
1307 shash_destroy(&ofproto->port_by_name);
1308 simap_destroy(&ofproto->ofp_requests);
1310 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1311 oftable_destroy(table);
1313 free(ofproto->tables);
1315 hmap_destroy(&ofproto->deletions);
1317 free(ofproto->vlan_bitmap);
1319 ofproto->ofproto_class->dealloc(ofproto);
1323 ofproto_destroy(struct ofproto *p)
1324 OVS_EXCLUDED(ofproto_mutex)
1326 struct ofport *ofport, *next_ofport;
1327 struct ofport_usage *usage, *next_usage;
1334 meter_delete(p, 1, p->meter_features.max_meters);
1335 p->meter_features.max_meters = 0;
1341 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1342 ofport_destroy(ofport);
1345 HMAP_FOR_EACH_SAFE (usage, next_usage, hmap_node, &p->ofport_usage) {
1346 hmap_remove(&p->ofport_usage, &usage->hmap_node);
1350 p->ofproto_class->destruct(p);
1351 ofproto_destroy__(p);
1354 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1355 * kernel datapath, for example, this destroys the datapath in the kernel, and
1356 * with the netdev-based datapath, it tears down the data structures that
1357 * represent the datapath.
1359 * The datapath should not be currently open as an ofproto. */
1361 ofproto_delete(const char *name, const char *type)
1363 const struct ofproto_class *class = ofproto_class_find__(type);
1364 return (!class ? EAFNOSUPPORT
1365 : !class->del ? EACCES
1366 : class->del(type, name));
1370 process_port_change(struct ofproto *ofproto, int error, char *devname)
1372 if (error == ENOBUFS) {
1373 reinit_ports(ofproto);
1374 } else if (!error) {
1375 update_port(ofproto, devname);
1381 ofproto_type_run(const char *datapath_type)
1383 const struct ofproto_class *class;
1386 datapath_type = ofproto_normalize_type(datapath_type);
1387 class = ofproto_class_find__(datapath_type);
1389 error = class->type_run ? class->type_run(datapath_type) : 0;
1390 if (error && error != EAGAIN) {
1391 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1392 datapath_type, ovs_strerror(error));
1398 ofproto_type_wait(const char *datapath_type)
1400 const struct ofproto_class *class;
1402 datapath_type = ofproto_normalize_type(datapath_type);
1403 class = ofproto_class_find__(datapath_type);
1405 if (class->type_wait) {
1406 class->type_wait(datapath_type);
1411 any_pending_ops(const struct ofproto *p)
1412 OVS_EXCLUDED(ofproto_mutex)
1416 ovs_mutex_lock(&ofproto_mutex);
1417 b = !list_is_empty(&p->pending);
1418 ovs_mutex_unlock(&ofproto_mutex);
1424 ofproto_run(struct ofproto *p)
1429 error = p->ofproto_class->run(p);
1430 if (error && error != EAGAIN) {
1431 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1434 run_rule_executes(p);
1436 /* Restore the eviction group heap invariant occasionally. */
1437 if (p->eviction_group_timer < time_msec()) {
1440 p->eviction_group_timer = time_msec() + 1000;
1442 for (i = 0; i < p->n_tables; i++) {
1443 struct oftable *table = &p->tables[i];
1444 struct eviction_group *evg;
1445 struct cls_cursor cursor;
1448 if (!table->eviction_fields) {
1452 ovs_mutex_lock(&ofproto_mutex);
1453 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1454 heap_rebuild(&evg->rules);
1457 fat_rwlock_rdlock(&table->cls.rwlock);
1458 cls_cursor_init(&cursor, &table->cls, NULL);
1459 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1460 if (!rule->eviction_group
1461 && (rule->idle_timeout || rule->hard_timeout)) {
1462 eviction_group_add_rule(rule);
1465 fat_rwlock_unlock(&table->cls.rwlock);
1466 ovs_mutex_unlock(&ofproto_mutex);
1470 if (p->ofproto_class->port_poll) {
1473 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1474 process_port_change(p, error, devname);
1478 new_seq = seq_read(connectivity_seq_get());
1479 if (new_seq != p->change_seq) {
1480 struct sset devnames;
1481 const char *devname;
1482 struct ofport *ofport;
1484 /* Update OpenFlow port status for any port whose netdev has changed.
1486 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1487 * destroyed, so it's not safe to update ports directly from the
1488 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1489 * need this two-phase approach. */
1490 sset_init(&devnames);
1491 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1492 sset_add(&devnames, netdev_get_name(ofport->netdev));
1494 SSET_FOR_EACH (devname, &devnames) {
1495 update_port(p, devname);
1497 sset_destroy(&devnames);
1499 p->change_seq = new_seq;
1504 connmgr_run(p->connmgr, handle_openflow);
1508 connmgr_run(p->connmgr, NULL);
1510 if (!any_pending_ops(p)) {
1511 p->state = S_OPENFLOW;
1516 connmgr_run(p->connmgr, NULL);
1518 if (!any_pending_ops(p)) {
1519 connmgr_flushed(p->connmgr);
1520 p->state = S_OPENFLOW;
1528 if (time_msec() >= p->next_op_report) {
1529 long long int ago = (time_msec() - p->first_op) / 1000;
1530 long long int interval = (p->last_op - p->first_op) / 1000;
1534 ds_put_format(&s, "%d flow_mods ",
1535 p->n_add + p->n_delete + p->n_modify);
1536 if (interval == ago) {
1537 ds_put_format(&s, "in the last %lld s", ago);
1538 } else if (interval) {
1539 ds_put_format(&s, "in the %lld s starting %lld s ago",
1542 ds_put_format(&s, "%lld s ago", ago);
1545 ds_put_cstr(&s, " (");
1547 ds_put_format(&s, "%d adds, ", p->n_add);
1550 ds_put_format(&s, "%d deletes, ", p->n_delete);
1553 ds_put_format(&s, "%d modifications, ", p->n_modify);
1556 ds_put_char(&s, ')');
1558 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1561 p->n_add = p->n_delete = p->n_modify = 0;
1562 p->next_op_report = LLONG_MAX;
1569 ofproto_wait(struct ofproto *p)
1571 p->ofproto_class->wait(p);
1572 if (p->ofproto_class->port_poll_wait) {
1573 p->ofproto_class->port_poll_wait(p);
1575 seq_wait(connectivity_seq_get(), p->change_seq);
1579 connmgr_wait(p->connmgr, true);
1584 connmgr_wait(p->connmgr, false);
1585 if (!any_pending_ops(p)) {
1586 poll_immediate_wake();
1593 ofproto_is_alive(const struct ofproto *p)
1595 return connmgr_has_controllers(p->connmgr);
1598 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1599 * memory_report(). */
1601 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1603 const struct oftable *table;
1604 unsigned int n_rules;
1606 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1608 ovs_mutex_lock(&ofproto_mutex);
1609 simap_increase(usage, "ops",
1610 ofproto->n_pending + hmap_count(&ofproto->deletions));
1611 ovs_mutex_unlock(&ofproto_mutex);
1614 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1615 fat_rwlock_rdlock(&table->cls.rwlock);
1616 n_rules += classifier_count(&table->cls);
1617 fat_rwlock_unlock(&table->cls.rwlock);
1619 simap_increase(usage, "rules", n_rules);
1621 if (ofproto->ofproto_class->get_memory_usage) {
1622 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1625 connmgr_get_memory_usage(ofproto->connmgr, usage);
1629 ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage)
1631 const struct ofproto_class *class;
1633 datapath_type = ofproto_normalize_type(datapath_type);
1634 class = ofproto_class_find__(datapath_type);
1636 if (class && class->type_get_memory_usage) {
1637 class->type_get_memory_usage(datapath_type, usage);
1642 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1645 connmgr_get_controller_info(ofproto->connmgr, info);
1649 ofproto_free_ofproto_controller_info(struct shash *info)
1651 connmgr_free_controller_info(info);
1654 /* Makes a deep copy of 'old' into 'port'. */
1656 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1658 port->name = xstrdup(old->name);
1659 port->type = xstrdup(old->type);
1660 port->ofp_port = old->ofp_port;
1663 /* Frees memory allocated to members of 'ofproto_port'.
1665 * Do not call this function on an ofproto_port obtained from
1666 * ofproto_port_dump_next(): that function retains ownership of the data in the
1669 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1671 free(ofproto_port->name);
1672 free(ofproto_port->type);
1675 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1677 * This function provides no status indication. An error status for the entire
1678 * dump operation is provided when it is completed by calling
1679 * ofproto_port_dump_done().
1682 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1683 const struct ofproto *ofproto)
1685 dump->ofproto = ofproto;
1686 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1690 /* Attempts to retrieve another port from 'dump', which must have been created
1691 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1692 * 'port' and returns true. On failure, returns false.
1694 * Failure might indicate an actual error or merely that the last port has been
1695 * dumped. An error status for the entire dump operation is provided when it
1696 * is completed by calling ofproto_port_dump_done().
1698 * The ofproto owns the data stored in 'port'. It will remain valid until at
1699 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1700 * ofproto_port_dump_done(). */
1702 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1703 struct ofproto_port *port)
1705 const struct ofproto *ofproto = dump->ofproto;
1711 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1714 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1720 /* Completes port table dump operation 'dump', which must have been created
1721 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1722 * error-free, otherwise a positive errno value describing the problem. */
1724 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1726 const struct ofproto *ofproto = dump->ofproto;
1728 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1731 return dump->error == EOF ? 0 : dump->error;
1734 /* Returns the type to pass to netdev_open() when a datapath of type
1735 * 'datapath_type' has a port of type 'port_type', for a few special
1736 * cases when a netdev type differs from a port type. For example, when
1737 * using the userspace datapath, a port of type "internal" needs to be
1740 * Returns either 'type' itself or a string literal, which must not be
1743 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1745 const struct ofproto_class *class;
1747 datapath_type = ofproto_normalize_type(datapath_type);
1748 class = ofproto_class_find__(datapath_type);
1753 return (class->port_open_type
1754 ? class->port_open_type(datapath_type, port_type)
1758 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1759 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1760 * the port's OpenFlow port number.
1762 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1763 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1764 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1765 * 'ofp_portp' is non-null). */
1767 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1768 ofp_port_t *ofp_portp)
1770 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1773 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1775 const char *netdev_name = netdev_get_name(netdev);
1777 simap_put(&ofproto->ofp_requests, netdev_name,
1778 ofp_to_u16(ofp_port));
1779 update_port(ofproto, netdev_name);
1782 *ofp_portp = OFPP_NONE;
1784 struct ofproto_port ofproto_port;
1786 error = ofproto_port_query_by_name(ofproto,
1787 netdev_get_name(netdev),
1790 *ofp_portp = ofproto_port.ofp_port;
1791 ofproto_port_destroy(&ofproto_port);
1798 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1799 * initializes '*port' appropriately; on failure, returns a positive errno
1802 * The caller owns the data in 'ofproto_port' and must free it with
1803 * ofproto_port_destroy() when it is no longer needed. */
1805 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1806 struct ofproto_port *port)
1810 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1812 memset(port, 0, sizeof *port);
1817 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1818 * Returns 0 if successful, otherwise a positive errno. */
1820 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1822 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1823 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1824 struct simap_node *ofp_request_node;
1827 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1828 if (ofp_request_node) {
1829 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1832 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1833 if (!error && ofport) {
1834 /* 'name' is the netdev's name and update_port() is going to close the
1835 * netdev. Just in case update_port() refers to 'name' after it
1836 * destroys 'ofport', make a copy of it around the update_port()
1838 char *devname = xstrdup(name);
1839 update_port(ofproto, devname);
1846 flow_mod_init(struct ofputil_flow_mod *fm,
1847 const struct match *match, unsigned int priority,
1848 const struct ofpact *ofpacts, size_t ofpacts_len,
1849 enum ofp_flow_mod_command command)
1851 memset(fm, 0, sizeof *fm);
1853 fm->priority = priority;
1856 fm->modify_cookie = false;
1858 fm->command = command;
1859 fm->idle_timeout = 0;
1860 fm->hard_timeout = 0;
1861 fm->buffer_id = UINT32_MAX;
1862 fm->out_port = OFPP_ANY;
1863 fm->out_group = OFPG_ANY;
1865 fm->ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1866 fm->ofpacts_len = ofpacts_len;
1870 simple_flow_mod(struct ofproto *ofproto,
1871 const struct match *match, unsigned int priority,
1872 const struct ofpact *ofpacts, size_t ofpacts_len,
1873 enum ofp_flow_mod_command command)
1875 struct ofputil_flow_mod fm;
1877 flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command);
1879 return handle_flow_mod__(ofproto, NULL, &fm, NULL);
1882 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1883 * performs the 'n_actions' actions in 'actions'. The new flow will not
1886 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1887 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1888 * controllers; otherwise, it will be hidden.
1890 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1892 * This is a helper function for in-band control and fail-open. */
1894 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1895 unsigned int priority,
1896 const struct ofpact *ofpacts, size_t ofpacts_len)
1897 OVS_EXCLUDED(ofproto_mutex)
1899 const struct rule *rule;
1902 /* First do a cheap check whether the rule we're looking for already exists
1903 * with the actions that we want. If it does, then we're done. */
1904 fat_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1905 rule = rule_from_cls_rule(classifier_find_match_exactly(
1906 &ofproto->tables[0].cls, match, priority));
1908 ovs_mutex_lock(&rule->mutex);
1909 must_add = !ofpacts_equal(rule->actions->ofpacts,
1910 rule->actions->ofpacts_len,
1911 ofpacts, ofpacts_len);
1912 ovs_mutex_unlock(&rule->mutex);
1916 fat_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1918 /* If there's no such rule or the rule doesn't have the actions we want,
1919 * fall back to a executing a full flow mod. We can't optimize this at
1920 * all because we didn't take enough locks above to ensure that the flow
1921 * table didn't already change beneath us. */
1923 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1924 OFPFC_MODIFY_STRICT);
1928 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1929 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1930 * operation cannot be initiated now but may be retried later.
1932 * This is a helper function for in-band control and fail-open and the "learn"
1935 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1936 OVS_EXCLUDED(ofproto_mutex)
1938 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1941 /* Searches for a rule with matching criteria exactly equal to 'target' in
1942 * ofproto's table 0 and, if it finds one, deletes it.
1944 * This is a helper function for in-band control and fail-open. */
1946 ofproto_delete_flow(struct ofproto *ofproto,
1947 const struct match *target, unsigned int priority)
1948 OVS_EXCLUDED(ofproto_mutex)
1950 struct classifier *cls = &ofproto->tables[0].cls;
1953 /* First do a cheap check whether the rule we're looking for has already
1954 * been deleted. If so, then we're done. */
1955 fat_rwlock_rdlock(&cls->rwlock);
1956 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1958 fat_rwlock_unlock(&cls->rwlock);
1963 /* Fall back to a executing a full flow mod. We can't optimize this at all
1964 * because we didn't take enough locks above to ensure that the flow table
1965 * didn't already change beneath us. */
1966 return simple_flow_mod(ofproto, target, priority, NULL, 0,
1967 OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE;
1970 /* Starts the process of deleting all of the flows from all of ofproto's flow
1971 * tables and then reintroducing the flows required by in-band control and
1972 * fail-open. The process will complete in a later call to ofproto_run(). */
1974 ofproto_flush_flows(struct ofproto *ofproto)
1976 COVERAGE_INC(ofproto_flush);
1977 ofproto->state = S_FLUSH;
1981 reinit_ports(struct ofproto *p)
1983 struct ofproto_port_dump dump;
1984 struct sset devnames;
1985 struct ofport *ofport;
1986 struct ofproto_port ofproto_port;
1987 const char *devname;
1989 COVERAGE_INC(ofproto_reinit_ports);
1991 sset_init(&devnames);
1992 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1993 sset_add(&devnames, netdev_get_name(ofport->netdev));
1995 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1996 sset_add(&devnames, ofproto_port.name);
1999 SSET_FOR_EACH (devname, &devnames) {
2000 update_port(p, devname);
2002 sset_destroy(&devnames);
2006 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
2010 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
2011 port_idx = port_idx ? port_idx : UINT16_MAX;
2013 if (port_idx >= ofproto->max_ports
2014 || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) {
2015 uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no;
2016 long long int last_used_at, lru = LLONG_MAX;
2018 /* Search for a free OpenFlow port number. We try not to
2019 * immediately reuse them to prevent problems due to old
2022 * We limit the automatically assigned port numbers to the lower half
2023 * of the port range, to reserve the upper half for assignment by
2026 if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) {
2027 ofproto->alloc_port_no = 1;
2029 last_used_at = ofport_get_usage(ofproto,
2030 u16_to_ofp(ofproto->alloc_port_no));
2031 if (!last_used_at) {
2032 port_idx = ofproto->alloc_port_no;
2034 } else if ( last_used_at < time_msec() - 60*60*1000) {
2035 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2036 * more than an hour ago, consider it usable. */
2037 ofport_remove_usage(ofproto,
2038 u16_to_ofp(ofproto->alloc_port_no));
2039 port_idx = ofproto->alloc_port_no;
2041 } else if (last_used_at < lru) {
2043 lru_ofport = ofproto->alloc_port_no;
2046 if (ofproto->alloc_port_no == end_port_no) {
2048 port_idx = lru_ofport;
2055 ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX);
2056 return u16_to_ofp(port_idx);
2060 dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port)
2062 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
2063 ofport_set_usage(ofproto, ofp_port, time_msec());
2067 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2068 * pointer if the netdev cannot be opened. On success, also fills in
2070 static struct netdev *
2071 ofport_open(struct ofproto *ofproto,
2072 struct ofproto_port *ofproto_port,
2073 struct ofputil_phy_port *pp)
2075 enum netdev_flags flags;
2076 struct netdev *netdev;
2079 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
2081 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
2082 "cannot be opened (%s)",
2084 ofproto_port->name, ofproto_port->ofp_port,
2085 ofproto_port->name, ovs_strerror(error));
2089 if (ofproto_port->ofp_port == OFPP_NONE) {
2090 if (!strcmp(ofproto->name, ofproto_port->name)) {
2091 ofproto_port->ofp_port = OFPP_LOCAL;
2093 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
2094 ofproto_port->name);
2097 pp->port_no = ofproto_port->ofp_port;
2098 netdev_get_etheraddr(netdev, pp->hw_addr);
2099 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
2100 netdev_get_flags(netdev, &flags);
2101 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
2102 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
2103 netdev_get_features(netdev, &pp->curr, &pp->advertised,
2104 &pp->supported, &pp->peer);
2105 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2106 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2111 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2112 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
2115 ofport_equal(const struct ofputil_phy_port *a,
2116 const struct ofputil_phy_port *b)
2118 return (eth_addr_equals(a->hw_addr, b->hw_addr)
2119 && a->state == b->state
2120 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
2121 && a->curr == b->curr
2122 && a->advertised == b->advertised
2123 && a->supported == b->supported
2124 && a->peer == b->peer
2125 && a->curr_speed == b->curr_speed
2126 && a->max_speed == b->max_speed);
2129 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2130 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2131 * one with the same name or port number). */
2133 ofport_install(struct ofproto *p,
2134 struct netdev *netdev, const struct ofputil_phy_port *pp)
2136 const char *netdev_name = netdev_get_name(netdev);
2137 struct ofport *ofport;
2140 /* Create ofport. */
2141 ofport = p->ofproto_class->port_alloc();
2146 ofport->ofproto = p;
2147 ofport->netdev = netdev;
2149 ofport->ofp_port = pp->port_no;
2150 ofport->created = time_msec();
2152 /* Add port to 'p'. */
2153 hmap_insert(&p->ports, &ofport->hmap_node,
2154 hash_ofp_port(ofport->ofp_port));
2155 shash_add(&p->port_by_name, netdev_name, ofport);
2157 update_mtu(p, ofport);
2159 /* Let the ofproto_class initialize its private data. */
2160 error = p->ofproto_class->port_construct(ofport);
2164 connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD);
2168 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
2169 p->name, netdev_name, ovs_strerror(error));
2171 ofport_destroy__(ofport);
2173 netdev_close(netdev);
2177 /* Removes 'ofport' from 'p' and destroys it. */
2179 ofport_remove(struct ofport *ofport)
2181 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp,
2183 ofport_destroy(ofport);
2186 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2189 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2191 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2193 ofport_remove(port);
2197 /* Updates 'port' with new 'pp' description.
2199 * Does not handle a name or port number change. The caller must implement
2200 * such a change as a delete followed by an add. */
2202 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
2204 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2205 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2206 | (pp->config & OFPUTIL_PC_PORT_DOWN));
2207 port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN)
2208 | (pp->state & OFPUTIL_PS_LINK_DOWN));
2209 port->pp.curr = pp->curr;
2210 port->pp.advertised = pp->advertised;
2211 port->pp.supported = pp->supported;
2212 port->pp.peer = pp->peer;
2213 port->pp.curr_speed = pp->curr_speed;
2214 port->pp.max_speed = pp->max_speed;
2216 connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY);
2219 /* Update OpenFlow 'state' in 'port' and notify controller. */
2221 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2223 if (port->pp.state != state) {
2224 port->pp.state = state;
2225 connmgr_send_port_status(port->ofproto->connmgr, &port->pp,
2231 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2233 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2235 if (port->ofproto->ofproto_class->set_realdev) {
2236 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2238 if (port->ofproto->ofproto_class->set_stp_port) {
2239 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2241 if (port->ofproto->ofproto_class->set_cfm) {
2242 port->ofproto->ofproto_class->set_cfm(port, NULL);
2244 if (port->ofproto->ofproto_class->bundle_remove) {
2245 port->ofproto->ofproto_class->bundle_remove(port);
2251 ofport_destroy__(struct ofport *port)
2253 struct ofproto *ofproto = port->ofproto;
2254 const char *name = netdev_get_name(port->netdev);
2256 hmap_remove(&ofproto->ports, &port->hmap_node);
2257 shash_delete(&ofproto->port_by_name,
2258 shash_find(&ofproto->port_by_name, name));
2260 netdev_close(port->netdev);
2261 ofproto->ofproto_class->port_dealloc(port);
2265 ofport_destroy(struct ofport *port)
2268 dealloc_ofp_port(port->ofproto, port->ofp_port);
2269 port->ofproto->ofproto_class->port_destruct(port);
2270 ofport_destroy__(port);
2275 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2277 struct ofport *port;
2279 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2281 if (port->ofp_port == ofp_port) {
2288 static long long int
2289 ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port)
2291 struct ofport_usage *usage;
2293 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2294 &ofproto->ofport_usage) {
2295 if (usage->ofp_port == ofp_port) {
2296 return usage->last_used;
2303 ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port,
2304 long long int last_used)
2306 struct ofport_usage *usage;
2307 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2308 &ofproto->ofport_usage) {
2309 if (usage->ofp_port == ofp_port) {
2310 usage->last_used = last_used;
2314 ovs_assert(last_used == LLONG_MAX);
2316 usage = xmalloc(sizeof *usage);
2317 usage->ofp_port = ofp_port;
2318 usage->last_used = last_used;
2319 hmap_insert(&ofproto->ofport_usage, &usage->hmap_node,
2320 hash_ofp_port(ofp_port));
2324 ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port)
2326 struct ofport_usage *usage;
2327 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2328 &ofproto->ofport_usage) {
2329 if (usage->ofp_port == ofp_port) {
2330 hmap_remove(&ofproto->ofport_usage, &usage->hmap_node);
2338 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2340 struct ofproto *ofproto = port->ofproto;
2343 if (ofproto->ofproto_class->port_get_stats) {
2344 error = ofproto->ofproto_class->port_get_stats(port, stats);
2353 update_port(struct ofproto *ofproto, const char *name)
2355 struct ofproto_port ofproto_port;
2356 struct ofputil_phy_port pp;
2357 struct netdev *netdev;
2358 struct ofport *port;
2360 COVERAGE_INC(ofproto_update_port);
2362 /* Fetch 'name''s location and properties from the datapath. */
2363 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2364 ? ofport_open(ofproto, &ofproto_port, &pp)
2368 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2369 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2370 struct netdev *old_netdev = port->netdev;
2372 /* 'name' hasn't changed location. Any properties changed? */
2373 if (!ofport_equal(&port->pp, &pp)) {
2374 ofport_modified(port, &pp);
2377 update_mtu(ofproto, port);
2379 /* Install the newly opened netdev in case it has changed.
2380 * Don't close the old netdev yet in case port_modified has to
2381 * remove a retained reference to it.*/
2382 port->netdev = netdev;
2384 if (port->ofproto->ofproto_class->port_modified) {
2385 port->ofproto->ofproto_class->port_modified(port);
2388 netdev_close(old_netdev);
2390 /* If 'port' is nonnull then its name differs from 'name' and thus
2391 * we should delete it. If we think there's a port named 'name'
2392 * then its port number must be wrong now so delete it too. */
2394 ofport_remove(port);
2396 ofport_remove_with_name(ofproto, name);
2397 ofport_install(ofproto, netdev, &pp);
2400 /* Any port named 'name' is gone now. */
2401 ofport_remove_with_name(ofproto, name);
2403 ofproto_port_destroy(&ofproto_port);
2407 init_ports(struct ofproto *p)
2409 struct ofproto_port_dump dump;
2410 struct ofproto_port ofproto_port;
2411 struct shash_node *node, *next;
2413 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2414 const char *name = ofproto_port.name;
2416 if (shash_find(&p->port_by_name, name)) {
2417 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2420 struct ofputil_phy_port pp;
2421 struct netdev *netdev;
2423 /* Check if an OpenFlow port number had been requested. */
2424 node = shash_find(&init_ofp_ports, name);
2426 const struct iface_hint *iface_hint = node->data;
2427 simap_put(&p->ofp_requests, name,
2428 ofp_to_u16(iface_hint->ofp_port));
2431 netdev = ofport_open(p, &ofproto_port, &pp);
2433 ofport_install(p, netdev, &pp);
2434 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2435 p->alloc_port_no = MAX(p->alloc_port_no,
2436 ofp_to_u16(ofproto_port.ofp_port));
2442 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2443 struct iface_hint *iface_hint = node->data;
2445 if (!strcmp(iface_hint->br_name, p->name)) {
2446 free(iface_hint->br_name);
2447 free(iface_hint->br_type);
2449 shash_delete(&init_ofp_ports, node);
2456 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2457 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2459 find_min_mtu(struct ofproto *p)
2461 struct ofport *ofport;
2464 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2465 struct netdev *netdev = ofport->netdev;
2468 /* Skip any internal ports, since that's what we're trying to
2470 if (!strcmp(netdev_get_type(netdev), "internal")) {
2474 if (netdev_get_mtu(netdev, &dev_mtu)) {
2477 if (!mtu || dev_mtu < mtu) {
2482 return mtu ? mtu: ETH_PAYLOAD_MAX;
2485 /* Update MTU of all datapath devices on 'p' to the minimum of the
2486 * non-datapath ports in event of 'port' added or changed. */
2488 update_mtu(struct ofproto *p, struct ofport *port)
2490 struct ofport *ofport;
2491 struct netdev *netdev = port->netdev;
2492 int dev_mtu, old_min;
2494 if (netdev_get_mtu(netdev, &dev_mtu)) {
2498 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2499 if (dev_mtu > p->min_mtu) {
2500 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2501 dev_mtu = p->min_mtu;
2504 port->mtu = dev_mtu;
2508 /* For non-internal port find new min mtu. */
2509 old_min = p->min_mtu;
2510 port->mtu = dev_mtu;
2511 p->min_mtu = find_min_mtu(p);
2512 if (p->min_mtu == old_min) {
2516 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2517 struct netdev *netdev = ofport->netdev;
2519 if (!strcmp(netdev_get_type(netdev), "internal")) {
2520 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2521 ofport->mtu = p->min_mtu;
2528 ofproto_rule_ref(struct rule *rule)
2533 atomic_add(&rule->ref_count, 1, &orig);
2534 ovs_assert(orig != 0);
2539 ofproto_rule_unref(struct rule *rule)
2544 atomic_sub(&rule->ref_count, 1, &orig);
2546 rule->ofproto->ofproto_class->rule_destruct(rule);
2547 ofproto_rule_destroy__(rule);
2549 ovs_assert(orig != 0);
2554 struct rule_actions *
2555 rule_get_actions(const struct rule *rule)
2556 OVS_EXCLUDED(rule->mutex)
2558 struct rule_actions *actions;
2560 ovs_mutex_lock(&rule->mutex);
2561 actions = rule_get_actions__(rule);
2562 ovs_mutex_unlock(&rule->mutex);
2567 struct rule_actions *
2568 rule_get_actions__(const struct rule *rule)
2569 OVS_REQUIRES(rule->mutex)
2571 rule_actions_ref(rule->actions);
2572 return rule->actions;
2576 ofproto_rule_destroy__(struct rule *rule)
2577 OVS_NO_THREAD_SAFETY_ANALYSIS
2579 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2580 rule_actions_unref(rule->actions);
2581 ovs_mutex_destroy(&rule->mutex);
2582 rule->ofproto->ofproto_class->rule_dealloc(rule);
2585 static uint32_t get_provider_meter_id(const struct ofproto *,
2586 uint32_t of_meter_id);
2588 /* Creates and returns a new 'struct rule_actions', with a ref_count of 1,
2589 * whose actions are a copy of from the 'ofpacts_len' bytes of 'ofpacts'. */
2590 struct rule_actions *
2591 rule_actions_create(const struct ofproto *ofproto,
2592 const struct ofpact *ofpacts, size_t ofpacts_len)
2594 struct rule_actions *actions;
2596 actions = xmalloc(sizeof *actions);
2597 atomic_init(&actions->ref_count, 1);
2598 actions->ofpacts = xmemdup(ofpacts, ofpacts_len);
2599 actions->ofpacts_len = ofpacts_len;
2600 actions->provider_meter_id
2601 = get_provider_meter_id(ofproto,
2602 ofpacts_get_meter(ofpacts, ofpacts_len));
2607 /* Increments 'actions''s ref_count. */
2609 rule_actions_ref(struct rule_actions *actions)
2614 atomic_add(&actions->ref_count, 1, &orig);
2615 ovs_assert(orig != 0);
2619 /* Decrements 'actions''s ref_count and frees 'actions' if the ref_count
2622 rule_actions_unref(struct rule_actions *actions)
2627 atomic_sub(&actions->ref_count, 1, &orig);
2629 free(actions->ofpacts);
2632 ovs_assert(orig != 0);
2637 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2638 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2640 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2641 OVS_REQUIRES(ofproto_mutex)
2643 return (port == OFPP_ANY
2644 || ofpacts_output_to_port(rule->actions->ofpacts,
2645 rule->actions->ofpacts_len, port));
2648 /* Returns true if 'rule' has group and equals group_id. */
2650 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2651 OVS_REQUIRES(ofproto_mutex)
2653 return (group_id == OFPG11_ANY
2654 || ofpacts_output_to_group(rule->actions->ofpacts,
2655 rule->actions->ofpacts_len, group_id));
2658 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2659 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2661 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2662 OVS_REQUIRES(ofproto_mutex)
2664 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2669 case OFOPERATION_ADD:
2670 case OFOPERATION_DELETE:
2673 case OFOPERATION_MODIFY:
2674 case OFOPERATION_REPLACE:
2675 return ofpacts_output_to_port(op->actions->ofpacts,
2676 op->actions->ofpacts_len, out_port);
2683 rule_execute_destroy(struct rule_execute *e)
2685 ofproto_rule_unref(e->rule);
2686 list_remove(&e->list_node);
2690 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2691 * by passing them to the ofproto provider. */
2693 run_rule_executes(struct ofproto *ofproto)
2694 OVS_EXCLUDED(ofproto_mutex)
2696 struct rule_execute *e, *next;
2697 struct list executes;
2699 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2700 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2701 union flow_in_port in_port_;
2704 in_port_.ofp_port = e->in_port;
2705 flow_extract(e->packet, 0, 0, NULL, &in_port_, &flow);
2706 ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet);
2708 rule_execute_destroy(e);
2712 /* Destroys and discards all "rule_execute" operations queued up in
2713 * ofproto->rule_executes. */
2715 destroy_rule_executes(struct ofproto *ofproto)
2717 struct rule_execute *e, *next;
2718 struct list executes;
2720 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2721 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2722 ofpbuf_delete(e->packet);
2723 rule_execute_destroy(e);
2727 /* Returns true if 'rule' should be hidden from the controller.
2729 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2730 * (e.g. by in-band control) and are intentionally hidden from the
2733 ofproto_rule_is_hidden(const struct rule *rule)
2735 return rule->cr.priority > UINT16_MAX;
2738 static enum oftable_flags
2739 rule_get_flags(const struct rule *rule)
2741 return rule->ofproto->tables[rule->table_id].flags;
2745 rule_is_modifiable(const struct rule *rule)
2747 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2751 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2753 ofconn_send_reply(ofconn, make_echo_reply(oh));
2758 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2760 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2761 struct ofputil_switch_features features;
2762 struct ofport *port;
2768 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2770 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2772 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2773 * if present, are always at the end.) */
2774 n_tables = ofproto->n_tables;
2775 for (i = 0; i < ofproto->n_tables; i++) {
2776 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2782 features.datapath_id = ofproto->datapath_id;
2783 features.n_buffers = pktbuf_capacity();
2784 features.n_tables = n_tables;
2785 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2786 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2788 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2790 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2791 features.auxiliary_id = 0;
2792 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2794 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2795 ofputil_put_switch_features_port(&port->pp, b);
2798 ofconn_send_reply(ofconn, b);
2803 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2805 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2806 struct ofp_switch_config *osc;
2807 enum ofp_config_flags flags;
2811 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2812 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2813 flags = ofproto->frag_handling;
2814 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2815 if (oh->version < OFP13_VERSION
2816 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2817 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2819 osc->flags = htons(flags);
2820 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2821 ofconn_send_reply(ofconn, buf);
2827 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2829 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2830 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2831 uint16_t flags = ntohs(osc->flags);
2833 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2834 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2835 enum ofp_config_flags cur = ofproto->frag_handling;
2836 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2838 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2840 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2841 ofproto->frag_handling = next;
2843 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2845 ofputil_frag_handling_to_string(next));
2849 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2850 ofconn_set_invalid_ttl_to_controller(ofconn,
2851 (oh->version < OFP13_VERSION
2852 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2854 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2859 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2860 * error message code for the caller to propagate upward. Otherwise, returns
2863 * The log message mentions 'msg_type'. */
2865 reject_slave_controller(struct ofconn *ofconn)
2867 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2868 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2869 return OFPERR_OFPBRC_EPERM;
2875 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
2878 * - If they use a meter, then 'ofproto' has that meter configured.
2880 * - If they use any groups, then 'ofproto' has that group configured.
2882 * Returns 0 if successful, otherwise an OpenFlow error. */
2884 ofproto_check_ofpacts(struct ofproto *ofproto,
2885 const struct ofpact ofpacts[], size_t ofpacts_len)
2887 const struct ofpact *a;
2890 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2891 if (mid && get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2892 return OFPERR_OFPMMFC_INVALID_METER;
2895 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2896 if (a->type == OFPACT_GROUP
2897 && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) {
2898 return OFPERR_OFPBAC_BAD_OUT_GROUP;
2906 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2908 struct ofproto *p = ofconn_get_ofproto(ofconn);
2909 struct ofputil_packet_out po;
2910 struct ofpbuf *payload;
2911 uint64_t ofpacts_stub[1024 / 8];
2912 struct ofpbuf ofpacts;
2914 union flow_in_port in_port_;
2917 COVERAGE_INC(ofproto_packet_out);
2919 error = reject_slave_controller(ofconn);
2924 /* Decode message. */
2925 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2926 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2928 goto exit_free_ofpacts;
2930 if (ofp_to_u16(po.in_port) >= p->max_ports
2931 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2932 error = OFPERR_OFPBRC_BAD_PORT;
2933 goto exit_free_ofpacts;
2937 if (po.buffer_id != UINT32_MAX) {
2938 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2939 if (error || !payload) {
2940 goto exit_free_ofpacts;
2943 /* Ensure that the L3 header is 32-bit aligned. */
2944 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2947 /* Verify actions against packet, then send packet if successful. */
2948 in_port_.ofp_port = po.in_port;
2949 flow_extract(payload, 0, 0, NULL, &in_port_, &flow);
2950 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len);
2952 error = p->ofproto_class->packet_out(p, payload, &flow,
2953 po.ofpacts, po.ofpacts_len);
2955 ofpbuf_delete(payload);
2958 ofpbuf_uninit(&ofpacts);
2964 update_port_config(struct ofport *port,
2965 enum ofputil_port_config config,
2966 enum ofputil_port_config mask)
2968 enum ofputil_port_config old_config = port->pp.config;
2969 enum ofputil_port_config toggle;
2971 toggle = (config ^ port->pp.config) & mask;
2972 if (toggle & OFPUTIL_PC_PORT_DOWN) {
2973 if (config & OFPUTIL_PC_PORT_DOWN) {
2974 netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL);
2976 netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL);
2978 toggle &= ~OFPUTIL_PC_PORT_DOWN;
2981 port->pp.config ^= toggle;
2982 if (port->pp.config != old_config) {
2983 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2988 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2990 struct ofproto *p = ofconn_get_ofproto(ofconn);
2991 struct ofputil_port_mod pm;
2992 struct ofport *port;
2995 error = reject_slave_controller(ofconn);
3000 error = ofputil_decode_port_mod(oh, &pm);
3005 port = ofproto_get_port(p, pm.port_no);
3007 return OFPERR_OFPPMFC_BAD_PORT;
3008 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
3009 return OFPERR_OFPPMFC_BAD_HW_ADDR;
3011 update_port_config(port, pm.config, pm.mask);
3013 netdev_set_advertisements(port->netdev, pm.advertise);
3020 handle_desc_stats_request(struct ofconn *ofconn,
3021 const struct ofp_header *request)
3023 static const char *default_mfr_desc = "Nicira, Inc.";
3024 static const char *default_hw_desc = "Open vSwitch";
3025 static const char *default_sw_desc = VERSION;
3026 static const char *default_serial_desc = "None";
3027 static const char *default_dp_desc = "None";
3029 struct ofproto *p = ofconn_get_ofproto(ofconn);
3030 struct ofp_desc_stats *ods;
3033 msg = ofpraw_alloc_stats_reply(request, 0);
3034 ods = ofpbuf_put_zeros(msg, sizeof *ods);
3035 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
3036 sizeof ods->mfr_desc);
3037 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
3038 sizeof ods->hw_desc);
3039 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
3040 sizeof ods->sw_desc);
3041 ovs_strlcpy(ods->serial_num,
3042 p->serial_desc ? p->serial_desc : default_serial_desc,
3043 sizeof ods->serial_num);
3044 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
3045 sizeof ods->dp_desc);
3046 ofconn_send_reply(ofconn, msg);
3052 handle_table_stats_request(struct ofconn *ofconn,
3053 const struct ofp_header *request)
3055 struct ofproto *p = ofconn_get_ofproto(ofconn);
3056 struct ofp12_table_stats *ots;
3061 /* Set up default values.
3063 * ofp12_table_stats is used as a generic structure as
3064 * it is able to hold all the fields for ofp10_table_stats
3065 * and ofp11_table_stats (and of course itself).
3067 ots = xcalloc(p->n_tables, sizeof *ots);
3068 for (i = 0; i < p->n_tables; i++) {
3069 ots[i].table_id = i;
3070 sprintf(ots[i].name, "table%"PRIuSIZE, i);
3071 ots[i].match = htonll(OFPXMT13_MASK);
3072 ots[i].wildcards = htonll(OFPXMT13_MASK);
3073 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
3074 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
3075 ots[i].write_setfields = htonll(OFPXMT13_MASK);
3076 ots[i].apply_setfields = htonll(OFPXMT13_MASK);
3077 ots[i].metadata_match = OVS_BE64_MAX;
3078 ots[i].metadata_write = OVS_BE64_MAX;
3079 ots[i].instructions = htonl(OFPIT11_ALL);
3080 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
3081 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
3082 fat_rwlock_rdlock(&p->tables[i].cls.rwlock);
3083 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
3084 fat_rwlock_unlock(&p->tables[i].cls.rwlock);
3087 p->ofproto_class->get_tables(p, ots);
3089 /* Post-process the tables, dropping hidden tables. */
3090 n_tables = p->n_tables;
3091 for (i = 0; i < p->n_tables; i++) {
3092 const struct oftable *table = &p->tables[i];
3094 if (table->flags & OFTABLE_HIDDEN) {
3100 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
3103 if (table->max_flows < ntohl(ots[i].max_entries)) {
3104 ots[i].max_entries = htonl(table->max_flows);
3108 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
3109 ofconn_send_reply(ofconn, msg);
3117 append_port_stat(struct ofport *port, struct list *replies)
3119 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
3121 calc_duration(port->created, time_msec(),
3122 &ops.duration_sec, &ops.duration_nsec);
3124 /* Intentionally ignore return value, since errors will set
3125 * 'stats' to all-1s, which is correct for OpenFlow, and
3126 * netdev_get_stats() will log errors. */
3127 ofproto_port_get_stats(port, &ops.stats);
3129 ofputil_append_port_stat(replies, &ops);
3133 handle_port_stats_request(struct ofconn *ofconn,
3134 const struct ofp_header *request)
3136 struct ofproto *p = ofconn_get_ofproto(ofconn);
3137 struct ofport *port;
3138 struct list replies;
3142 error = ofputil_decode_port_stats_request(request, &port_no);
3147 ofpmp_init(&replies, request);
3148 if (port_no != OFPP_ANY) {
3149 port = ofproto_get_port(p, port_no);
3151 append_port_stat(port, &replies);
3154 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3155 append_port_stat(port, &replies);
3159 ofconn_send_replies(ofconn, &replies);
3164 handle_port_desc_stats_request(struct ofconn *ofconn,
3165 const struct ofp_header *request)
3167 struct ofproto *p = ofconn_get_ofproto(ofconn);
3168 enum ofp_version version;
3169 struct ofport *port;
3170 struct list replies;
3172 ofpmp_init(&replies, request);
3174 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
3175 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3176 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
3179 ofconn_send_replies(ofconn, &replies);
3184 hash_cookie(ovs_be64 cookie)
3186 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
3187 (OVS_FORCE uint64_t)cookie);
3191 cookies_insert(struct ofproto *ofproto, struct rule *rule)
3192 OVS_REQUIRES(ofproto_mutex)
3194 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3195 hash_cookie(rule->flow_cookie));
3199 cookies_remove(struct ofproto *ofproto, struct rule *rule)
3200 OVS_REQUIRES(ofproto_mutex)
3202 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3206 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
3207 ovs_be64 new_cookie)
3208 OVS_REQUIRES(ofproto_mutex)
3210 if (new_cookie != rule->flow_cookie) {
3211 cookies_remove(ofproto, rule);
3213 ovs_mutex_lock(&rule->mutex);
3214 rule->flow_cookie = new_cookie;
3215 ovs_mutex_unlock(&rule->mutex);
3217 cookies_insert(ofproto, rule);
3222 calc_duration(long long int start, long long int now,
3223 uint32_t *sec, uint32_t *nsec)
3225 long long int msecs = now - start;
3226 *sec = msecs / 1000;
3227 *nsec = (msecs % 1000) * (1000 * 1000);
3230 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3231 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
3233 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3235 return (table_id == 0xff || table_id < ofproto->n_tables
3237 : OFPERR_OFPBRC_BAD_TABLE_ID);
3241 static struct oftable *
3242 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
3244 struct oftable *table;
3246 for (table = &ofproto->tables[table_id];
3247 table < &ofproto->tables[ofproto->n_tables];
3249 if (!(table->flags & OFTABLE_HIDDEN)) {
3257 static struct oftable *
3258 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
3260 if (table_id == 0xff) {
3261 return next_visible_table(ofproto, 0);
3262 } else if (table_id < ofproto->n_tables) {
3263 return &ofproto->tables[table_id];
3269 static struct oftable *
3270 next_matching_table(const struct ofproto *ofproto,
3271 const struct oftable *table, uint8_t table_id)
3273 return (table_id == 0xff
3274 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
3278 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3280 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3281 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3283 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3284 * only once, for that table. (This can be used to access tables marked
3287 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3288 * entered at all. (Perhaps you should have validated TABLE_ID with
3289 * check_table_id().)
3291 * All parameters are evaluated multiple times.
3293 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3294 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3296 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3298 /* Initializes 'criteria' in a straightforward way based on the other
3301 * For "loose" matching, the 'priority' parameter is unimportant and may be
3304 rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
3305 const struct match *match, unsigned int priority,
3306 ovs_be64 cookie, ovs_be64 cookie_mask,
3307 ofp_port_t out_port, uint32_t out_group)
3309 criteria->table_id = table_id;
3310 cls_rule_init(&criteria->cr, match, priority);
3311 criteria->cookie = cookie;
3312 criteria->cookie_mask = cookie_mask;
3313 criteria->out_port = out_port;
3314 criteria->out_group = out_group;
3318 rule_criteria_destroy(struct rule_criteria *criteria)
3320 cls_rule_destroy(&criteria->cr);
3324 rule_collection_init(struct rule_collection *rules)
3326 rules->rules = rules->stub;
3328 rules->capacity = ARRAY_SIZE(rules->stub);
3332 rule_collection_add(struct rule_collection *rules, struct rule *rule)
3334 if (rules->n >= rules->capacity) {
3335 size_t old_size, new_size;
3337 old_size = rules->capacity * sizeof *rules->rules;
3338 rules->capacity *= 2;
3339 new_size = rules->capacity * sizeof *rules->rules;
3341 if (rules->rules == rules->stub) {
3342 rules->rules = xmalloc(new_size);
3343 memcpy(rules->rules, rules->stub, old_size);
3345 rules->rules = xrealloc(rules->rules, new_size);
3349 rules->rules[rules->n++] = rule;
3353 rule_collection_ref(struct rule_collection *rules)
3354 OVS_REQUIRES(ofproto_mutex)
3358 for (i = 0; i < rules->n; i++) {
3359 ofproto_rule_ref(rules->rules[i]);
3364 rule_collection_unref(struct rule_collection *rules)
3368 for (i = 0; i < rules->n; i++) {
3369 ofproto_rule_unref(rules->rules[i]);
3374 rule_collection_destroy(struct rule_collection *rules)
3376 if (rules->rules != rules->stub) {
3382 collect_rule(struct rule *rule, const struct rule_criteria *c,
3383 struct rule_collection *rules)
3384 OVS_REQUIRES(ofproto_mutex)
3386 /* We ordinarily want to skip hidden rules, but there has to be a way for
3387 * code internal to OVS to modify and delete them, so if the criteria
3388 * specify a priority that can only be for a hidden flow, then allow hidden
3389 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
3390 * with hidden flows because OpenFlow uses only a 16-bit field to specify
3392 if (ofproto_rule_is_hidden(rule) && c->cr.priority <= UINT16_MAX) {
3394 } else if (rule->pending) {
3395 return OFPROTO_POSTPONE;
3397 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3398 && ofproto_rule_has_out_port(rule, c->out_port)
3399 && ofproto_rule_has_out_group(rule, c->out_group)
3400 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)) {
3401 rule_collection_add(rules, rule);
3407 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3408 * on classifiers rules are done in the "loose" way required for OpenFlow
3409 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3412 * Hidden rules are always omitted.
3414 * Returns 0 on success, otherwise an OpenFlow error code. */
3416 collect_rules_loose(struct ofproto *ofproto,
3417 const struct rule_criteria *criteria,
3418 struct rule_collection *rules)
3419 OVS_REQUIRES(ofproto_mutex)
3421 struct oftable *table;
3424 rule_collection_init(rules);
3426 error = check_table_id(ofproto, criteria->table_id);
3431 if (criteria->cookie_mask == OVS_BE64_MAX) {
3434 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3435 hash_cookie(criteria->cookie),
3436 &ofproto->cookies) {
3437 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
3438 error = collect_rule(rule, criteria, rules);
3445 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3446 struct cls_cursor cursor;
3449 fat_rwlock_rdlock(&table->cls.rwlock);
3450 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
3451 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3452 error = collect_rule(rule, criteria, rules);
3457 fat_rwlock_unlock(&table->cls.rwlock);
3463 rule_collection_destroy(rules);
3468 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3469 * on classifiers rules are done in the "strict" way required for OpenFlow
3470 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3471 * rules on list 'rules'.
3473 * Hidden rules are always omitted.
3475 * Returns 0 on success, otherwise an OpenFlow error code. */
3477 collect_rules_strict(struct ofproto *ofproto,
3478 const struct rule_criteria *criteria,
3479 struct rule_collection *rules)
3480 OVS_REQUIRES(ofproto_mutex)
3482 struct oftable *table;
3485 rule_collection_init(rules);
3487 error = check_table_id(ofproto, criteria->table_id);
3492 if (criteria->cookie_mask == OVS_BE64_MAX) {
3495 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3496 hash_cookie(criteria->cookie),
3497 &ofproto->cookies) {
3498 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
3499 error = collect_rule(rule, criteria, rules);
3506 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3509 fat_rwlock_rdlock(&table->cls.rwlock);
3510 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3511 &table->cls, &criteria->cr));
3512 fat_rwlock_unlock(&table->cls.rwlock);
3514 error = collect_rule(rule, criteria, rules);
3524 rule_collection_destroy(rules);
3529 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3530 * forced into the range of a uint16_t. */
3532 age_secs(long long int age_ms)
3534 return (age_ms < 0 ? 0
3535 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3536 : (unsigned int) age_ms / 1000);
3540 handle_flow_stats_request(struct ofconn *ofconn,
3541 const struct ofp_header *request)
3542 OVS_EXCLUDED(ofproto_mutex)
3544 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3545 struct ofputil_flow_stats_request fsr;
3546 struct rule_criteria criteria;
3547 struct rule_collection rules;
3548 struct list replies;
3552 error = ofputil_decode_flow_stats_request(&fsr, request);
3557 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3558 fsr.cookie_mask, fsr.out_port, fsr.out_group);
3560 ovs_mutex_lock(&ofproto_mutex);
3561 error = collect_rules_loose(ofproto, &criteria, &rules);
3562 rule_criteria_destroy(&criteria);
3564 rule_collection_ref(&rules);
3566 ovs_mutex_unlock(&ofproto_mutex);
3572 ofpmp_init(&replies, request);
3573 for (i = 0; i < rules.n; i++) {
3574 struct rule *rule = rules.rules[i];
3575 long long int now = time_msec();
3576 struct ofputil_flow_stats fs;
3577 long long int created, used, modified;
3578 struct rule_actions *actions;
3579 enum ofputil_flow_mod_flags flags;
3581 ovs_mutex_lock(&rule->mutex);
3582 fs.cookie = rule->flow_cookie;
3583 fs.idle_timeout = rule->idle_timeout;
3584 fs.hard_timeout = rule->hard_timeout;
3585 created = rule->created;
3587 modified = rule->modified;
3588 actions = rule_get_actions__(rule);
3589 flags = rule->flags;
3590 ovs_mutex_unlock(&rule->mutex);
3592 minimatch_expand(&rule->cr.match, &fs.match);
3593 fs.table_id = rule->table_id;
3594 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
3595 fs.priority = rule->cr.priority;
3596 fs.idle_age = age_secs(now - used);
3597 fs.hard_age = age_secs(now - modified);
3598 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3600 fs.ofpacts = actions->ofpacts;
3601 fs.ofpacts_len = actions->ofpacts_len;
3604 ofputil_append_flow_stats_reply(&fs, &replies);
3606 rule_actions_unref(actions);
3609 rule_collection_unref(&rules);
3610 rule_collection_destroy(&rules);
3612 ofconn_send_replies(ofconn, &replies);
3618 flow_stats_ds(struct rule *rule, struct ds *results)
3620 uint64_t packet_count, byte_count;
3621 struct rule_actions *actions;
3622 long long int created;
3624 rule->ofproto->ofproto_class->rule_get_stats(rule,
3625 &packet_count, &byte_count);
3627 ovs_mutex_lock(&rule->mutex);
3628 actions = rule_get_actions__(rule);
3629 created = rule->created;
3630 ovs_mutex_unlock(&rule->mutex);
3632 if (rule->table_id != 0) {
3633 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3635 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
3636 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3637 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3638 cls_rule_format(&rule->cr, results);
3639 ds_put_char(results, ',');
3641 ds_put_cstr(results, "actions=");
3642 ofpacts_format(actions->ofpacts, actions->ofpacts_len, results);
3644 ds_put_cstr(results, "\n");
3646 rule_actions_unref(actions);
3649 /* Adds a pretty-printed description of all flows to 'results', including
3650 * hidden flows (e.g., set up by in-band control). */
3652 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3654 struct oftable *table;
3656 OFPROTO_FOR_EACH_TABLE (table, p) {
3657 struct cls_cursor cursor;
3660 fat_rwlock_rdlock(&table->cls.rwlock);
3661 cls_cursor_init(&cursor, &table->cls, NULL);
3662 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3663 flow_stats_ds(rule, results);
3665 fat_rwlock_unlock(&table->cls.rwlock);
3669 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3670 * '*engine_type' and '*engine_id', respectively. */
3672 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3673 uint8_t *engine_type, uint8_t *engine_id)
3675 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3678 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3679 * true if the port's CFM status was successfully stored into '*status'.
3680 * Returns false if the port did not have CFM configured, in which case
3681 * '*status' is indeterminate.
3683 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3685 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3686 struct ofproto_cfm_status *status)
3688 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3690 && ofproto->ofproto_class->get_cfm_status
3691 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3695 handle_aggregate_stats_request(struct ofconn *ofconn,
3696 const struct ofp_header *oh)
3697 OVS_EXCLUDED(ofproto_mutex)
3699 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3700 struct ofputil_flow_stats_request request;
3701 struct ofputil_aggregate_stats stats;
3702 bool unknown_packets, unknown_bytes;
3703 struct rule_criteria criteria;
3704 struct rule_collection rules;
3705 struct ofpbuf *reply;
3709 error = ofputil_decode_flow_stats_request(&request, oh);
3714 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3715 request.cookie, request.cookie_mask,
3716 request.out_port, request.out_group);
3718 ovs_mutex_lock(&ofproto_mutex);
3719 error = collect_rules_loose(ofproto, &criteria, &rules);
3720 rule_criteria_destroy(&criteria);
3722 rule_collection_ref(&rules);
3724 ovs_mutex_unlock(&ofproto_mutex);
3730 memset(&stats, 0, sizeof stats);
3731 unknown_packets = unknown_bytes = false;
3732 for (i = 0; i < rules.n; i++) {
3733 struct rule *rule = rules.rules[i];
3734 uint64_t packet_count;
3735 uint64_t byte_count;
3737 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3740 if (packet_count == UINT64_MAX) {
3741 unknown_packets = true;
3743 stats.packet_count += packet_count;
3746 if (byte_count == UINT64_MAX) {
3747 unknown_bytes = true;
3749 stats.byte_count += byte_count;
3754 if (unknown_packets) {
3755 stats.packet_count = UINT64_MAX;
3757 if (unknown_bytes) {
3758 stats.byte_count = UINT64_MAX;
3761 rule_collection_unref(&rules);
3762 rule_collection_destroy(&rules);
3764 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3765 ofconn_send_reply(ofconn, reply);
3770 struct queue_stats_cbdata {
3771 struct ofport *ofport;
3772 struct list replies;
3777 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3778 const struct netdev_queue_stats *stats)
3780 struct ofputil_queue_stats oqs;
3782 oqs.port_no = cbdata->ofport->pp.port_no;
3783 oqs.queue_id = queue_id;
3784 oqs.tx_bytes = stats->tx_bytes;
3785 oqs.tx_packets = stats->tx_packets;
3786 oqs.tx_errors = stats->tx_errors;
3787 if (stats->created != LLONG_MIN) {
3788 calc_duration(stats->created, cbdata->now,
3789 &oqs.duration_sec, &oqs.duration_nsec);
3791 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3793 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3797 handle_queue_stats_dump_cb(uint32_t queue_id,
3798 struct netdev_queue_stats *stats,
3801 struct queue_stats_cbdata *cbdata = cbdata_;
3803 put_queue_stats(cbdata, queue_id, stats);
3807 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3808 struct queue_stats_cbdata *cbdata)
3810 cbdata->ofport = port;
3811 if (queue_id == OFPQ_ALL) {
3812 netdev_dump_queue_stats(port->netdev,
3813 handle_queue_stats_dump_cb, cbdata);
3815 struct netdev_queue_stats stats;
3817 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3818 put_queue_stats(cbdata, queue_id, &stats);
3820 return OFPERR_OFPQOFC_BAD_QUEUE;
3827 handle_queue_stats_request(struct ofconn *ofconn,
3828 const struct ofp_header *rq)
3830 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3831 struct queue_stats_cbdata cbdata;
3832 struct ofport *port;
3834 struct ofputil_queue_stats_request oqsr;
3836 COVERAGE_INC(ofproto_queue_req);
3838 ofpmp_init(&cbdata.replies, rq);
3839 cbdata.now = time_msec();
3841 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3846 if (oqsr.port_no == OFPP_ANY) {
3847 error = OFPERR_OFPQOFC_BAD_QUEUE;
3848 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3849 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3854 port = ofproto_get_port(ofproto, oqsr.port_no);
3856 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3857 : OFPERR_OFPQOFC_BAD_PORT);
3860 ofconn_send_replies(ofconn, &cbdata.replies);
3862 ofpbuf_list_delete(&cbdata.replies);
3869 is_flow_deletion_pending(const struct ofproto *ofproto,
3870 const struct cls_rule *cls_rule,
3872 OVS_REQUIRES(ofproto_mutex)
3874 if (!hmap_is_empty(&ofproto->deletions)) {
3875 struct ofoperation *op;
3877 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3878 cls_rule_hash(cls_rule, table_id),
3879 &ofproto->deletions) {
3880 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3890 should_evict_a_rule(struct oftable *table, unsigned int extra_space)
3891 OVS_REQUIRES(ofproto_mutex)
3892 OVS_NO_THREAD_SAFETY_ANALYSIS
3894 return classifier_count(&table->cls) + extra_space > table->max_flows;
3898 evict_rules_from_table(struct ofproto *ofproto, struct oftable *table,
3899 unsigned int extra_space)
3900 OVS_REQUIRES(ofproto_mutex)
3902 while (should_evict_a_rule(table, extra_space)) {
3905 if (!choose_rule_to_evict(table, &rule)) {
3906 return OFPERR_OFPFMFC_TABLE_FULL;
3907 } else if (rule->pending) {
3908 return OFPROTO_POSTPONE;
3910 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
3911 delete_flow__(rule, group, OFPRR_EVICTION);
3912 ofopgroup_submit(group);
3919 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3920 * in which no matching flow already exists in the flow table.
3922 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3923 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3924 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3925 * initiated now but may be retried later.
3927 * The caller retains ownership of 'fm->ofpacts'.
3929 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3932 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3933 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3934 OVS_REQUIRES(ofproto_mutex)
3936 struct oftable *table;
3937 struct ofopgroup *group;
3943 error = check_table_id(ofproto, fm->table_id);
3949 if (fm->table_id == 0xff) {
3950 if (ofproto->ofproto_class->rule_choose_table) {
3951 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3957 ovs_assert(table_id < ofproto->n_tables);
3961 } else if (fm->table_id < ofproto->n_tables) {
3962 table_id = fm->table_id;
3964 return OFPERR_OFPBRC_BAD_TABLE_ID;
3967 table = &ofproto->tables[table_id];
3969 if (table->flags & OFTABLE_READONLY) {
3970 return OFPERR_OFPBRC_EPERM;
3973 cls_rule_init(&cr, &fm->match, fm->priority);
3975 /* Transform "add" into "modify" if there's an existing identical flow. */
3976 fat_rwlock_rdlock(&table->cls.rwlock);
3977 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
3978 fat_rwlock_unlock(&table->cls.rwlock);
3980 cls_rule_destroy(&cr);
3981 if (!rule_is_modifiable(rule)) {
3982 return OFPERR_OFPBRC_EPERM;
3983 } else if (rule->pending) {
3984 return OFPROTO_POSTPONE;
3986 struct rule_collection rules;
3988 rule_collection_init(&rules);
3989 rule_collection_add(&rules, rule);
3990 fm->modify_cookie = true;
3991 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
3992 rule_collection_destroy(&rules);
3998 /* Serialize against pending deletion. */
3999 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
4000 cls_rule_destroy(&cr);
4001 return OFPROTO_POSTPONE;
4004 /* Check for overlap, if requested. */
4005 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
4008 fat_rwlock_rdlock(&table->cls.rwlock);
4009 overlaps = classifier_rule_overlaps(&table->cls, &cr);
4010 fat_rwlock_unlock(&table->cls.rwlock);
4013 cls_rule_destroy(&cr);
4014 return OFPERR_OFPFMFC_OVERLAP;
4018 /* If necessary, evict an existing rule to clear out space. */
4019 error = evict_rules_from_table(ofproto, table, 1);
4021 cls_rule_destroy(&cr);
4025 /* Allocate new rule. */
4026 rule = ofproto->ofproto_class->rule_alloc();
4028 cls_rule_destroy(&cr);
4029 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
4030 ofproto->name, ovs_strerror(error));
4034 /* Initialize base state. */
4035 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4036 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
4037 atomic_init(&rule->ref_count, 1);
4038 rule->pending = NULL;
4039 rule->flow_cookie = fm->new_cookie;
4040 rule->created = rule->modified = rule->used = time_msec();
4042 ovs_mutex_init(&rule->mutex);
4043 ovs_mutex_lock(&rule->mutex);
4044 rule->idle_timeout = fm->idle_timeout;
4045 rule->hard_timeout = fm->hard_timeout;
4046 ovs_mutex_unlock(&rule->mutex);
4048 *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
4049 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4050 rule->actions = rule_actions_create(ofproto, fm->ofpacts, fm->ofpacts_len);
4051 list_init(&rule->meter_list_node);
4052 rule->eviction_group = NULL;
4053 list_init(&rule->expirable);
4054 rule->monitor_flags = 0;
4055 rule->add_seqno = 0;
4056 rule->modify_seqno = 0;
4058 /* Construct rule, initializing derived state. */
4059 error = ofproto->ofproto_class->rule_construct(rule);
4061 ofproto_rule_destroy__(rule);
4066 oftable_insert_rule(rule);
4068 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4069 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
4070 ofproto->ofproto_class->rule_insert(rule);
4071 ofopgroup_submit(group);
4076 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4078 /* Modifies the rules listed in 'rules', changing their actions to match those
4081 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4084 * Returns 0 on success, otherwise an OpenFlow error code. */
4086 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4087 struct ofputil_flow_mod *fm, const struct ofp_header *request,
4088 const struct rule_collection *rules)
4089 OVS_REQUIRES(ofproto_mutex)
4091 enum ofoperation_type type;
4092 struct ofopgroup *group;
4096 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
4097 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4098 error = OFPERR_OFPBRC_EPERM;
4099 for (i = 0; i < rules->n; i++) {
4100 struct rule *rule = rules->rules[i];
4101 struct ofoperation *op;
4102 bool actions_changed;
4103 bool reset_counters;
4105 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
4107 if (rule_is_modifiable(rule)) {
4108 /* At least one rule is modifiable, don't report EPERM error. */
4114 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
4115 rule->actions->ofpacts,
4116 rule->actions->ofpacts_len);
4118 op = ofoperation_create(group, rule, type, 0);
4120 if (fm->modify_cookie && fm->new_cookie != OVS_BE64_MAX) {
4121 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
4123 if (type == OFOPERATION_REPLACE) {
4124 ovs_mutex_lock(&rule->mutex);
4125 rule->idle_timeout = fm->idle_timeout;
4126 rule->hard_timeout = fm->hard_timeout;
4127 ovs_mutex_unlock(&rule->mutex);
4129 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4130 if (fm->idle_timeout || fm->hard_timeout) {
4131 if (!rule->eviction_group) {
4132 eviction_group_add_rule(rule);
4135 eviction_group_remove_rule(rule);
4139 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
4140 if (actions_changed || reset_counters) {
4141 struct rule_actions *new_actions;
4143 op->actions = rule->actions;
4144 new_actions = rule_actions_create(ofproto,
4145 fm->ofpacts, fm->ofpacts_len);
4147 ovs_mutex_lock(&rule->mutex);
4148 rule->actions = new_actions;
4149 ovs_mutex_unlock(&rule->mutex);
4151 rule->ofproto->ofproto_class->rule_modify_actions(rule,
4154 ofoperation_complete(op, 0);
4157 ofopgroup_submit(group);
4163 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
4164 struct ofputil_flow_mod *fm, const struct ofp_header *request)
4165 OVS_REQUIRES(ofproto_mutex)
4167 if (fm->cookie_mask != htonll(0) || fm->new_cookie == OVS_BE64_MAX) {
4170 return add_flow(ofproto, ofconn, fm, request);
4173 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4176 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4179 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4180 struct ofputil_flow_mod *fm,
4181 const struct ofp_header *request)
4182 OVS_REQUIRES(ofproto_mutex)
4184 struct rule_criteria criteria;
4185 struct rule_collection rules;
4188 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4189 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4190 error = collect_rules_loose(ofproto, &criteria, &rules);
4191 rule_criteria_destroy(&criteria);
4194 error = (rules.n > 0
4195 ? modify_flows__(ofproto, ofconn, fm, request, &rules)
4196 : modify_flows_add(ofproto, ofconn, fm, request));
4199 rule_collection_destroy(&rules);
4204 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
4207 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4210 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4211 struct ofputil_flow_mod *fm,
4212 const struct ofp_header *request)
4213 OVS_REQUIRES(ofproto_mutex)
4215 struct rule_criteria criteria;
4216 struct rule_collection rules;
4219 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4220 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4221 error = collect_rules_strict(ofproto, &criteria, &rules);
4222 rule_criteria_destroy(&criteria);
4226 error = modify_flows_add(ofproto, ofconn, fm, request);
4227 } else if (rules.n == 1) {
4228 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4232 rule_collection_destroy(&rules);
4237 /* OFPFC_DELETE implementation. */
4240 delete_flow__(struct rule *rule, struct ofopgroup *group,
4241 enum ofp_flow_removed_reason reason)
4242 OVS_REQUIRES(ofproto_mutex)
4244 struct ofproto *ofproto = rule->ofproto;
4246 ofproto_rule_send_removed(rule, reason);
4248 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
4249 oftable_remove_rule(rule);
4250 ofproto->ofproto_class->rule_delete(rule);
4253 /* Deletes the rules listed in 'rules'.
4255 * Returns 0 on success, otherwise an OpenFlow error code. */
4257 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4258 const struct ofp_header *request,
4259 const struct rule_collection *rules,
4260 enum ofp_flow_removed_reason reason)
4261 OVS_REQUIRES(ofproto_mutex)
4263 struct ofopgroup *group;
4266 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
4267 for (i = 0; i < rules->n; i++) {
4268 delete_flow__(rules->rules[i], group, reason);
4270 ofopgroup_submit(group);
4275 /* Implements OFPFC_DELETE. */
4277 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4278 const struct ofputil_flow_mod *fm,
4279 const struct ofp_header *request)
4280 OVS_REQUIRES(ofproto_mutex)
4282 struct rule_criteria criteria;
4283 struct rule_collection rules;
4286 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4287 fm->cookie, fm->cookie_mask,
4288 fm->out_port, fm->out_group);
4289 error = collect_rules_loose(ofproto, &criteria, &rules);
4290 rule_criteria_destroy(&criteria);
4292 if (!error && rules.n > 0) {
4293 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4295 rule_collection_destroy(&rules);
4300 /* Implements OFPFC_DELETE_STRICT. */
4302 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4303 const struct ofputil_flow_mod *fm,
4304 const struct ofp_header *request)
4305 OVS_REQUIRES(ofproto_mutex)
4307 struct rule_criteria criteria;
4308 struct rule_collection rules;
4311 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4312 fm->cookie, fm->cookie_mask,
4313 fm->out_port, fm->out_group);
4314 error = collect_rules_strict(ofproto, &criteria, &rules);
4315 rule_criteria_destroy(&criteria);
4317 if (!error && rules.n > 0) {
4318 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4320 rule_collection_destroy(&rules);
4326 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
4327 OVS_REQUIRES(ofproto_mutex)
4329 struct ofputil_flow_removed fr;
4331 if (ofproto_rule_is_hidden(rule) ||
4332 !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
4336 minimatch_expand(&rule->cr.match, &fr.match);
4337 fr.priority = rule->cr.priority;
4338 fr.cookie = rule->flow_cookie;
4340 fr.table_id = rule->table_id;
4341 calc_duration(rule->created, time_msec(),
4342 &fr.duration_sec, &fr.duration_nsec);
4343 ovs_mutex_lock(&rule->mutex);
4344 fr.idle_timeout = rule->idle_timeout;
4345 fr.hard_timeout = rule->hard_timeout;
4346 ovs_mutex_unlock(&rule->mutex);
4347 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
4350 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
4353 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4354 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4357 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
4360 * ofproto implementation ->run() functions should use this function to expire
4361 * OpenFlow flows. */
4363 ofproto_rule_expire(struct rule *rule, uint8_t reason)
4364 OVS_REQUIRES(ofproto_mutex)
4366 struct ofproto *ofproto = rule->ofproto;
4368 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
4369 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
4371 ofproto_rule_delete__(ofproto, rule, reason);
4374 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4375 * means "infinite". */
4377 reduce_timeout(uint16_t max, uint16_t *timeout)
4379 if (max && (!*timeout || *timeout > max)) {
4384 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4385 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4386 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4388 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4390 ofproto_rule_reduce_timeouts(struct rule *rule,
4391 uint16_t idle_timeout, uint16_t hard_timeout)
4392 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
4394 if (!idle_timeout && !hard_timeout) {
4398 ovs_mutex_lock(&ofproto_mutex);
4399 if (list_is_empty(&rule->expirable)) {
4400 list_insert(&rule->ofproto->expirable, &rule->expirable);
4402 ovs_mutex_unlock(&ofproto_mutex);
4404 ovs_mutex_lock(&rule->mutex);
4405 reduce_timeout(idle_timeout, &rule->idle_timeout);
4406 reduce_timeout(hard_timeout, &rule->hard_timeout);
4407 ovs_mutex_unlock(&rule->mutex);
4411 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4412 OVS_EXCLUDED(ofproto_mutex)
4414 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4415 struct ofputil_flow_mod fm;
4416 uint64_t ofpacts_stub[1024 / 8];
4417 struct ofpbuf ofpacts;
4421 error = reject_slave_controller(ofconn);
4426 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4427 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
4429 u16_to_ofp(ofproto->max_ports),
4432 error = ofproto_check_ofpacts(ofproto, fm.ofpacts, fm.ofpacts_len);
4435 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
4438 goto exit_free_ofpacts;
4441 /* Record the operation for logging a summary report. */
4442 switch (fm.command) {
4448 case OFPFC_MODIFY_STRICT:
4449 ofproto->n_modify++;
4453 case OFPFC_DELETE_STRICT:
4454 ofproto->n_delete++;
4459 if (ofproto->next_op_report == LLONG_MAX) {
4460 ofproto->first_op = now;
4461 ofproto->next_op_report = MAX(now + 10 * 1000,
4462 ofproto->op_backoff);
4463 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
4465 ofproto->last_op = now;
4468 ofpbuf_uninit(&ofpacts);
4474 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
4475 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
4476 OVS_EXCLUDED(ofproto_mutex)
4480 ovs_mutex_lock(&ofproto_mutex);
4481 if (ofproto->n_pending < 50) {
4482 switch (fm->command) {
4484 error = add_flow(ofproto, ofconn, fm, oh);
4488 error = modify_flows_loose(ofproto, ofconn, fm, oh);
4491 case OFPFC_MODIFY_STRICT:
4492 error = modify_flow_strict(ofproto, ofconn, fm, oh);
4496 error = delete_flows_loose(ofproto, ofconn, fm, oh);
4499 case OFPFC_DELETE_STRICT:
4500 error = delete_flow_strict(ofproto, ofconn, fm, oh);
4504 if (fm->command > 0xff) {
4505 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4506 "flow_mod_table_id extension is not enabled",
4509 error = OFPERR_OFPFMFC_BAD_COMMAND;
4513 ovs_assert(!list_is_empty(&ofproto->pending));
4514 error = OFPROTO_POSTPONE;
4516 ovs_mutex_unlock(&ofproto_mutex);
4518 run_rule_executes(ofproto);
4523 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4525 struct ofputil_role_request request;
4526 struct ofputil_role_request reply;
4530 error = ofputil_decode_role_message(oh, &request);
4535 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4536 if (ofconn_get_role(ofconn) != request.role
4537 && ofconn_has_pending_opgroups(ofconn)) {
4538 return OFPROTO_POSTPONE;
4541 if (request.have_generation_id
4542 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4543 return OFPERR_OFPRRFC_STALE;
4546 ofconn_set_role(ofconn, request.role);
4549 reply.role = ofconn_get_role(ofconn);
4550 reply.have_generation_id = ofconn_get_master_election_id(
4551 ofconn, &reply.generation_id);
4552 buf = ofputil_encode_role_reply(oh, &reply);
4553 ofconn_send_reply(ofconn, buf);
4559 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4560 const struct ofp_header *oh)
4562 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4563 enum ofputil_protocol cur, next;
4565 cur = ofconn_get_protocol(ofconn);
4566 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4567 ofconn_set_protocol(ofconn, next);
4573 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4575 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4576 enum ofputil_protocol cur, next;
4577 enum ofputil_protocol next_base;
4579 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4581 return OFPERR_OFPBRC_EPERM;
4584 cur = ofconn_get_protocol(ofconn);
4585 next = ofputil_protocol_set_base(cur, next_base);
4586 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4587 /* Avoid sending async messages in surprising protocol. */
4588 return OFPROTO_POSTPONE;
4591 ofconn_set_protocol(ofconn, next);
4596 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4597 const struct ofp_header *oh)
4599 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4602 format = ntohl(msg->format);
4603 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4604 return OFPERR_OFPBRC_EPERM;
4607 if (format != ofconn_get_packet_in_format(ofconn)
4608 && ofconn_has_pending_opgroups(ofconn)) {
4609 /* Avoid sending async message in surprsing packet in format. */
4610 return OFPROTO_POSTPONE;
4613 ofconn_set_packet_in_format(ofconn, format);
4618 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4620 const struct nx_async_config *msg = ofpmsg_body(oh);
4621 uint32_t master[OAM_N_TYPES];
4622 uint32_t slave[OAM_N_TYPES];
4624 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4625 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4626 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4628 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4629 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4630 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4632 ofconn_set_async_config(ofconn, master, slave);
4633 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4634 !ofconn_get_miss_send_len(ofconn)) {
4635 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4642 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4645 uint32_t master[OAM_N_TYPES];
4646 uint32_t slave[OAM_N_TYPES];
4647 struct nx_async_config *msg;
4649 ofconn_get_async_config(ofconn, master, slave);
4650 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4651 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4653 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4654 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4655 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4657 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4658 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4659 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4661 ofconn_send_reply(ofconn, buf);
4667 handle_nxt_set_controller_id(struct ofconn *ofconn,
4668 const struct ofp_header *oh)
4670 const struct nx_controller_id *nci = ofpmsg_body(oh);
4672 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4673 return OFPERR_NXBRC_MUST_BE_ZERO;
4676 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4681 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4685 if (ofconn_has_pending_opgroups(ofconn)) {
4686 return OFPROTO_POSTPONE;
4689 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4690 ? OFPRAW_OFPT10_BARRIER_REPLY
4691 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4692 ofconn_send_reply(ofconn, buf);
4697 ofproto_compose_flow_refresh_update(const struct rule *rule,
4698 enum nx_flow_monitor_flags flags,
4700 OVS_REQUIRES(ofproto_mutex)
4702 struct ofoperation *op = rule->pending;
4703 const struct rule_actions *actions;
4704 struct ofputil_flow_update fu;
4707 if (op && op->type == OFOPERATION_ADD) {
4708 /* We'll report the final flow when the operation completes. Reporting
4709 * it now would cause a duplicate report later. */
4713 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4714 ? NXFME_ADDED : NXFME_MODIFIED);
4716 ovs_mutex_lock(&rule->mutex);
4717 fu.idle_timeout = rule->idle_timeout;
4718 fu.hard_timeout = rule->hard_timeout;
4719 ovs_mutex_unlock(&rule->mutex);
4720 fu.table_id = rule->table_id;
4721 fu.cookie = rule->flow_cookie;
4722 minimatch_expand(&rule->cr.match, &match);
4724 fu.priority = rule->cr.priority;
4726 if (!(flags & NXFMF_ACTIONS)) {
4729 actions = rule->actions;
4731 /* An operation is in progress. Use the previous version of the flow's
4732 * actions, so that when the operation commits we report the change. */
4734 case OFOPERATION_ADD:
4737 case OFOPERATION_MODIFY:
4738 case OFOPERATION_REPLACE:
4739 actions = op->actions ? op->actions : rule->actions;
4742 case OFOPERATION_DELETE:
4743 actions = rule->actions;
4750 fu.ofpacts = actions ? actions->ofpacts : NULL;
4751 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
4753 if (list_is_empty(msgs)) {
4754 ofputil_start_flow_update(msgs);
4756 ofputil_append_flow_update(&fu, msgs);
4760 ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4762 OVS_REQUIRES(ofproto_mutex)
4766 for (i = 0; i < rules->n; i++) {
4767 struct rule *rule = rules->rules[i];
4768 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4769 rule->monitor_flags = 0;
4771 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4776 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4777 struct rule *rule, uint64_t seqno,
4778 struct rule_collection *rules)
4779 OVS_REQUIRES(ofproto_mutex)
4781 enum nx_flow_monitor_flags update;
4783 if (ofproto_rule_is_hidden(rule)) {
4788 ? ofoperation_has_out_port(rule->pending, m->out_port)
4789 : ofproto_rule_has_out_port(rule, m->out_port))) {
4794 if (rule->add_seqno > seqno) {
4795 update = NXFMF_ADD | NXFMF_MODIFY;
4796 } else if (rule->modify_seqno > seqno) {
4797 update = NXFMF_MODIFY;
4802 if (!(m->flags & update)) {
4806 update = NXFMF_INITIAL;
4809 if (!rule->monitor_flags) {
4810 rule_collection_add(rules, rule);
4812 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4816 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4818 struct rule_collection *rules)
4819 OVS_REQUIRES(ofproto_mutex)
4821 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4822 const struct ofoperation *op;
4823 const struct oftable *table;
4824 struct cls_rule target;
4826 cls_rule_init_from_minimatch(&target, &m->match, 0);
4827 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4828 struct cls_cursor cursor;
4831 fat_rwlock_rdlock(&table->cls.rwlock);
4832 cls_cursor_init(&cursor, &table->cls, &target);
4833 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4834 ovs_assert(!rule->pending); /* XXX */
4835 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4837 fat_rwlock_unlock(&table->cls.rwlock);
4840 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4841 struct rule *rule = op->rule;
4843 if (((m->table_id == 0xff
4844 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4845 : m->table_id == rule->table_id))
4846 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4847 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4850 cls_rule_destroy(&target);
4854 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4855 struct rule_collection *rules)
4856 OVS_REQUIRES(ofproto_mutex)
4858 if (m->flags & NXFMF_INITIAL) {
4859 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4864 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4865 uint64_t seqno, struct rule_collection *rules)
4866 OVS_REQUIRES(ofproto_mutex)
4868 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4872 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4873 OVS_EXCLUDED(ofproto_mutex)
4875 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4876 struct ofmonitor **monitors;
4877 size_t n_monitors, allocated_monitors;
4878 struct rule_collection rules;
4879 struct list replies;
4885 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4887 n_monitors = allocated_monitors = 0;
4889 ovs_mutex_lock(&ofproto_mutex);
4891 struct ofputil_flow_monitor_request request;
4892 struct ofmonitor *m;
4895 retval = ofputil_decode_flow_monitor_request(&request, &b);
4896 if (retval == EOF) {
4898 } else if (retval) {
4903 if (request.table_id != 0xff
4904 && request.table_id >= ofproto->n_tables) {
4905 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4909 error = ofmonitor_create(&request, ofconn, &m);
4914 if (n_monitors >= allocated_monitors) {
4915 monitors = x2nrealloc(monitors, &allocated_monitors,
4918 monitors[n_monitors++] = m;
4921 rule_collection_init(&rules);
4922 for (i = 0; i < n_monitors; i++) {
4923 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4926 ofpmp_init(&replies, oh);
4927 ofmonitor_compose_refresh_updates(&rules, &replies);
4928 ovs_mutex_unlock(&ofproto_mutex);
4930 rule_collection_destroy(&rules);
4932 ofconn_send_replies(ofconn, &replies);
4938 for (i = 0; i < n_monitors; i++) {
4939 ofmonitor_destroy(monitors[i]);
4942 ovs_mutex_unlock(&ofproto_mutex);
4948 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4949 OVS_EXCLUDED(ofproto_mutex)
4951 struct ofmonitor *m;
4955 id = ofputil_decode_flow_monitor_cancel(oh);
4957 ovs_mutex_lock(&ofproto_mutex);
4958 m = ofmonitor_lookup(ofconn, id);
4960 ofmonitor_destroy(m);
4963 error = OFPERR_NXBRC_FM_BAD_ID;
4965 ovs_mutex_unlock(&ofproto_mutex);
4970 /* Meters implementation.
4972 * Meter table entry, indexed by the OpenFlow meter_id.
4973 * These are always dynamically allocated to allocate enough space for
4975 * 'created' is used to compute the duration for meter stats.
4976 * 'list rules' is needed so that we can delete the dependent rules when the
4977 * meter table entry is deleted.
4978 * 'provider_meter_id' is for the provider's private use.
4981 long long int created; /* Time created. */
4982 struct list rules; /* List of "struct rule_dpif"s. */
4983 ofproto_meter_id provider_meter_id;
4984 uint16_t flags; /* Meter flags. */
4985 uint16_t n_bands; /* Number of meter bands. */
4986 struct ofputil_meter_band *bands;
4990 * This is used in instruction validation at flow set-up time,
4991 * as flows may not use non-existing meters.
4992 * Return value of UINT32_MAX signifies an invalid meter.
4995 get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id)
4997 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
4998 const struct meter *meter = ofproto->meters[of_meter_id];
5000 return meter->provider_meter_id.uint32;
5007 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
5011 meter->flags = config->flags;
5012 meter->n_bands = config->n_bands;
5013 meter->bands = xmemdup(config->bands,
5014 config->n_bands * sizeof *meter->bands);
5017 static struct meter *
5018 meter_create(const struct ofputil_meter_config *config,
5019 ofproto_meter_id provider_meter_id)
5021 struct meter *meter;
5023 meter = xzalloc(sizeof *meter);
5024 meter->provider_meter_id = provider_meter_id;
5025 meter->created = time_msec();
5026 list_init(&meter->rules);
5028 meter_update(meter, config);
5034 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
5035 OVS_REQUIRES(ofproto_mutex)
5038 for (mid = first; mid <= last; ++mid) {
5039 struct meter *meter = ofproto->meters[mid];
5041 ofproto->meters[mid] = NULL;
5042 ofproto->ofproto_class->meter_del(ofproto,
5043 meter->provider_meter_id);
5051 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5053 ofproto_meter_id provider_meter_id = { UINT32_MAX };
5054 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
5058 return OFPERR_OFPMMFC_METER_EXISTS;
5061 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
5064 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
5065 *meterp = meter_create(&mm->meter, provider_meter_id);
5071 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5073 struct meter *meter = ofproto->meters[mm->meter.meter_id];
5075 uint32_t provider_meter_id;
5078 return OFPERR_OFPMMFC_UNKNOWN_METER;
5081 provider_meter_id = meter->provider_meter_id.uint32;
5082 error = ofproto->ofproto_class->meter_set(ofproto,
5083 &meter->provider_meter_id,
5085 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
5087 meter_update(meter, &mm->meter);
5093 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
5094 struct ofputil_meter_mod *mm)
5095 OVS_EXCLUDED(ofproto_mutex)
5097 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5098 uint32_t meter_id = mm->meter.meter_id;
5099 struct rule_collection rules;
5100 enum ofperr error = 0;
5101 uint32_t first, last;
5103 if (meter_id == OFPM13_ALL) {
5105 last = ofproto->meter_features.max_meters;
5107 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
5110 first = last = meter_id;
5113 /* First delete the rules that use this meter. If any of those rules are
5114 * currently being modified, postpone the whole operation until later. */
5115 rule_collection_init(&rules);
5116 ovs_mutex_lock(&ofproto_mutex);
5117 for (meter_id = first; meter_id <= last; ++meter_id) {
5118 struct meter *meter = ofproto->meters[meter_id];
5119 if (meter && !list_is_empty(&meter->rules)) {
5122 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
5123 if (rule->pending) {
5124 error = OFPROTO_POSTPONE;
5127 rule_collection_add(&rules, rule);
5132 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
5135 /* Delete the meters. */
5136 meter_delete(ofproto, first, last);
5139 ovs_mutex_unlock(&ofproto_mutex);
5140 rule_collection_destroy(&rules);
5146 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5148 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5149 struct ofputil_meter_mod mm;
5150 uint64_t bands_stub[256 / 8];
5151 struct ofpbuf bands;
5155 error = reject_slave_controller(ofconn);
5160 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5162 error = ofputil_decode_meter_mod(oh, &mm, &bands);
5164 goto exit_free_bands;
5167 meter_id = mm.meter.meter_id;
5169 if (mm.command != OFPMC13_DELETE) {
5170 /* Fails also when meters are not implemented by the provider. */
5171 if (meter_id == 0 || meter_id > OFPM13_MAX) {
5172 error = OFPERR_OFPMMFC_INVALID_METER;
5173 goto exit_free_bands;
5174 } else if (meter_id > ofproto->meter_features.max_meters) {
5175 error = OFPERR_OFPMMFC_OUT_OF_METERS;
5176 goto exit_free_bands;
5178 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
5179 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
5180 goto exit_free_bands;
5184 switch (mm.command) {
5186 error = handle_add_meter(ofproto, &mm);
5189 case OFPMC13_MODIFY:
5190 error = handle_modify_meter(ofproto, &mm);
5193 case OFPMC13_DELETE:
5194 error = handle_delete_meter(ofconn, oh, &mm);
5198 error = OFPERR_OFPMMFC_BAD_COMMAND;
5203 ofpbuf_uninit(&bands);
5208 handle_meter_features_request(struct ofconn *ofconn,
5209 const struct ofp_header *request)
5211 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5212 struct ofputil_meter_features features;
5215 if (ofproto->ofproto_class->meter_get_features) {
5216 ofproto->ofproto_class->meter_get_features(ofproto, &features);
5218 memset(&features, 0, sizeof features);
5220 b = ofputil_encode_meter_features_reply(&features, request);
5222 ofconn_send_reply(ofconn, b);
5227 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
5230 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5231 struct list replies;
5232 uint64_t bands_stub[256 / 8];
5233 struct ofpbuf bands;
5234 uint32_t meter_id, first, last;
5236 ofputil_decode_meter_request(request, &meter_id);
5238 if (meter_id == OFPM13_ALL) {
5240 last = ofproto->meter_features.max_meters;
5242 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
5243 !ofproto->meters[meter_id]) {
5244 return OFPERR_OFPMMFC_UNKNOWN_METER;
5246 first = last = meter_id;
5249 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5250 ofpmp_init(&replies, request);
5252 for (meter_id = first; meter_id <= last; ++meter_id) {
5253 struct meter *meter = ofproto->meters[meter_id];
5255 continue; /* Skip non-existing meters. */
5257 if (type == OFPTYPE_METER_STATS_REQUEST) {
5258 struct ofputil_meter_stats stats;
5260 stats.meter_id = meter_id;
5262 /* Provider sets the packet and byte counts, we do the rest. */
5263 stats.flow_count = list_size(&meter->rules);
5264 calc_duration(meter->created, time_msec(),
5265 &stats.duration_sec, &stats.duration_nsec);
5266 stats.n_bands = meter->n_bands;
5267 ofpbuf_clear(&bands);
5269 = ofpbuf_put_uninit(&bands,
5270 meter->n_bands * sizeof *stats.bands);
5272 if (!ofproto->ofproto_class->meter_get(ofproto,
5273 meter->provider_meter_id,
5275 ofputil_append_meter_stats(&replies, &stats);
5277 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
5278 struct ofputil_meter_config config;
5280 config.meter_id = meter_id;
5281 config.flags = meter->flags;
5282 config.n_bands = meter->n_bands;
5283 config.bands = meter->bands;
5284 ofputil_append_meter_config(&replies, &config);
5288 ofconn_send_replies(ofconn, &replies);
5289 ofpbuf_uninit(&bands);
5294 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5295 struct ofgroup **group)
5296 OVS_TRY_RDLOCK(true, (*group)->rwlock)
5298 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5299 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5300 hash_int(group_id, 0), &ofproto->groups) {
5301 if ((*group)->group_id == group_id) {
5302 ovs_rwlock_rdlock(&(*group)->rwlock);
5303 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5307 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5312 ofproto_group_release(struct ofgroup *group)
5313 OVS_RELEASES(group->rwlock)
5315 ovs_rwlock_unlock(&group->rwlock);
5319 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
5320 struct ofgroup **group)
5321 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
5322 OVS_TRY_WRLOCK(true, (*group)->rwlock)
5324 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5325 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5326 hash_int(group_id, 0), &ofproto->groups) {
5327 if ((*group)->group_id == group_id) {
5328 ovs_rwlock_wrlock(&(*group)->rwlock);
5332 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5337 ofproto_group_exists__(const struct ofproto *ofproto, uint32_t group_id)
5338 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5340 struct ofgroup *grp;
5342 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
5343 hash_int(group_id, 0), &ofproto->groups) {
5344 if (grp->group_id == group_id) {
5352 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
5353 OVS_EXCLUDED(ofproto->groups_rwlock)
5357 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5358 exists = ofproto_group_exists__(ofproto, group_id);
5359 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5365 group_get_ref_count(struct ofgroup *group)
5366 OVS_EXCLUDED(ofproto_mutex)
5368 struct ofproto *ofproto = group->ofproto;
5369 struct rule_criteria criteria;
5370 struct rule_collection rules;
5375 match_init_catchall(&match);
5376 rule_criteria_init(&criteria, 0xff, &match, 0, htonll(0), htonll(0),
5377 OFPP_ANY, group->group_id);
5378 ovs_mutex_lock(&ofproto_mutex);
5379 error = collect_rules_loose(ofproto, &criteria, &rules);
5380 ovs_mutex_unlock(&ofproto_mutex);
5381 rule_criteria_destroy(&criteria);
5383 count = !error && rules.n < UINT32_MAX ? rules.n : UINT32_MAX;
5385 rule_collection_destroy(&rules);
5390 append_group_stats(struct ofgroup *group, struct list *replies)
5391 OVS_REQ_RDLOCK(group->rwlock)
5393 struct ofputil_group_stats ogs;
5394 struct ofproto *ofproto = group->ofproto;
5395 long long int now = time_msec();
5398 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
5400 /* Provider sets the packet and byte counts, we do the rest. */
5401 ogs.ref_count = group_get_ref_count(group);
5402 ogs.n_buckets = group->n_buckets;
5404 error = (ofproto->ofproto_class->group_get_stats
5405 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
5408 ogs.packet_count = UINT64_MAX;
5409 ogs.byte_count = UINT64_MAX;
5410 memset(ogs.bucket_stats, 0xff,
5411 ogs.n_buckets * sizeof *ogs.bucket_stats);
5414 ogs.group_id = group->group_id;
5415 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
5417 ofputil_append_group_stats(replies, &ogs);
5419 free(ogs.bucket_stats);
5423 handle_group_stats_request(struct ofconn *ofconn,
5424 const struct ofp_header *request)
5426 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5427 struct list replies;
5429 struct ofgroup *group;
5432 error = ofputil_decode_group_stats_request(request, &group_id);
5437 ofpmp_init(&replies, request);
5439 if (group_id == OFPG_ALL) {
5440 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5441 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5442 ovs_rwlock_rdlock(&group->rwlock);
5443 append_group_stats(group, &replies);
5444 ovs_rwlock_unlock(&group->rwlock);
5446 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5448 if (ofproto_group_lookup(ofproto, group_id, &group)) {
5449 append_group_stats(group, &replies);
5450 ofproto_group_release(group);
5454 ofconn_send_replies(ofconn, &replies);
5460 handle_group_desc_stats_request(struct ofconn *ofconn,
5461 const struct ofp_header *request)
5463 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5464 struct list replies;
5465 struct ofputil_group_desc gds;
5466 struct ofgroup *group;
5468 ofpmp_init(&replies, request);
5470 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5471 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5472 gds.group_id = group->group_id;
5473 gds.type = group->type;
5474 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
5476 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5478 ofconn_send_replies(ofconn, &replies);
5484 handle_group_features_stats_request(struct ofconn *ofconn,
5485 const struct ofp_header *request)
5487 struct ofproto *p = ofconn_get_ofproto(ofconn);
5490 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5492 ofconn_send_reply(ofconn, msg);
5499 handle_queue_get_config_request(struct ofconn *ofconn,
5500 const struct ofp_header *oh)
5502 struct ofproto *p = ofconn_get_ofproto(ofconn);
5503 struct netdev_queue_dump queue_dump;
5504 struct ofport *ofport;
5505 unsigned int queue_id;
5506 struct ofpbuf *reply;
5507 struct smap details;
5511 error = ofputil_decode_queue_get_config_request(oh, &request);
5516 ofport = ofproto_get_port(p, request);
5518 return OFPERR_OFPQOFC_BAD_PORT;
5521 reply = ofputil_encode_queue_get_config_reply(oh);
5523 smap_init(&details);
5524 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, ofport->netdev) {
5525 struct ofputil_queue_config queue;
5527 /* None of the existing queues have compatible properties, so we
5528 * hard-code omitting min_rate and max_rate. */
5529 queue.queue_id = queue_id;
5530 queue.min_rate = UINT16_MAX;
5531 queue.max_rate = UINT16_MAX;
5532 ofputil_append_queue_get_config_reply(reply, &queue);
5534 smap_destroy(&details);
5536 ofconn_send_reply(ofconn, reply);
5541 /* Implements OFPGC11_ADD
5542 * in which no matching flow already exists in the flow table.
5544 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
5545 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
5546 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
5547 * initiated now but may be retried later.
5549 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
5550 * ownership remains with the caller.
5552 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5555 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5557 struct ofgroup *ofgroup;
5560 if (gm->group_id > OFPG_MAX) {
5561 return OFPERR_OFPGMFC_INVALID_GROUP;
5563 if (gm->type > OFPGT11_FF) {
5564 return OFPERR_OFPGMFC_BAD_TYPE;
5567 /* Allocate new group and initialize it. */
5568 ofgroup = ofproto->ofproto_class->group_alloc();
5570 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
5571 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5574 ovs_rwlock_init(&ofgroup->rwlock);
5575 ofgroup->ofproto = ofproto;
5576 ofgroup->group_id = gm->group_id;
5577 ofgroup->type = gm->type;
5578 ofgroup->created = ofgroup->modified = time_msec();
5580 list_move(&ofgroup->buckets, &gm->buckets);
5581 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5583 /* Construct called BEFORE any locks are held. */
5584 error = ofproto->ofproto_class->group_construct(ofgroup);
5589 /* We wrlock as late as possible to minimize the time we jam any other
5590 * threads: No visible state changes before acquiring the lock. */
5591 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5593 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5594 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5598 if (ofproto_group_exists__(ofproto, gm->group_id)) {
5599 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5604 /* Insert new group. */
5605 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5606 hash_int(ofgroup->group_id, 0));
5607 ofproto->n_groups[ofgroup->type]++;
5609 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5614 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5615 ofproto->ofproto_class->group_destruct(ofgroup);
5617 ofputil_bucket_list_destroy(&ofgroup->buckets);
5618 ofproto->ofproto_class->group_dealloc(ofgroup);
5623 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5626 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5629 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5631 struct ofgroup *ofgroup;
5632 struct ofgroup *victim;
5635 if (gm->group_id > OFPG_MAX) {
5636 return OFPERR_OFPGMFC_INVALID_GROUP;
5639 if (gm->type > OFPGT11_FF) {
5640 return OFPERR_OFPGMFC_BAD_TYPE;
5643 victim = ofproto->ofproto_class->group_alloc();
5645 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5646 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5649 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
5650 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5653 /* Both group's and its container's write locks held now.
5654 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5655 if (ofgroup->type != gm->type
5656 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5657 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5662 list_move(&victim->buckets, &ofgroup->buckets);
5664 ofgroup->type = gm->type;
5665 list_move(&ofgroup->buckets, &gm->buckets);
5666 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5668 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
5670 ofputil_bucket_list_destroy(&victim->buckets);
5671 ofproto->n_groups[victim->type]--;
5672 ofproto->n_groups[ofgroup->type]++;
5673 ofgroup->modified = time_msec();
5675 ofputil_bucket_list_destroy(&ofgroup->buckets);
5678 list_move(&ofgroup->buckets, &victim->buckets);
5682 ovs_rwlock_unlock(&ofgroup->rwlock);
5683 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5685 ofproto->ofproto_class->group_dealloc(victim);
5690 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5691 OVS_RELEASES(ofproto->groups_rwlock)
5694 struct ofputil_flow_mod fm;
5696 /* Delete all flow entries containing this group in a group action */
5697 match_init_catchall(&match);
5698 flow_mod_init(&fm, &match, 0, NULL, 0, OFPFC_DELETE);
5699 fm.out_group = ofgroup->group_id;
5700 handle_flow_mod__(ofproto, NULL, &fm, NULL);
5702 /* Must wait until existing readers are done,
5703 * while holding the container's write lock at the same time. */
5704 ovs_rwlock_wrlock(&ofgroup->rwlock);
5705 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5706 /* No-one can find this group any more. */
5707 ofproto->n_groups[ofgroup->type]--;
5708 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5710 ofproto->ofproto_class->group_destruct(ofgroup);
5711 ofputil_bucket_list_destroy(&ofgroup->buckets);
5712 ovs_rwlock_unlock(&ofgroup->rwlock);
5713 ovs_rwlock_destroy(&ofgroup->rwlock);
5714 ofproto->ofproto_class->group_dealloc(ofgroup);
5717 /* Implements OFPGC_DELETE. */
5719 delete_group(struct ofproto *ofproto, uint32_t group_id)
5721 struct ofgroup *ofgroup;
5723 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5724 if (group_id == OFPG_ALL) {
5726 struct hmap_node *node = hmap_first(&ofproto->groups);
5730 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5731 delete_group__(ofproto, ofgroup);
5732 /* Lock for each node separately, so that we will not jam the
5733 * other threads for too long time. */
5734 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5737 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5738 hash_int(group_id, 0), &ofproto->groups) {
5739 if (ofgroup->group_id == group_id) {
5740 delete_group__(ofproto, ofgroup);
5745 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5749 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5751 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5752 struct ofputil_group_mod gm;
5755 error = reject_slave_controller(ofconn);
5760 error = ofputil_decode_group_mod(oh, &gm);
5765 switch (gm.command) {
5767 return add_group(ofproto, &gm);
5769 case OFPGC11_MODIFY:
5770 return modify_group(ofproto, &gm);
5772 case OFPGC11_DELETE:
5773 delete_group(ofproto, gm.group_id);
5777 if (gm.command > OFPGC11_DELETE) {
5778 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5779 ofproto->name, gm.command);
5781 return OFPERR_OFPGMFC_BAD_COMMAND;
5786 table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
5788 /* XXX Reject all configurations because none are currently supported */
5789 return OFPERR_OFPTMFC_BAD_CONFIG;
5791 if (tm->table_id == OFPTT_ALL) {
5793 for (i = 0; i < ofproto->n_tables; i++) {
5794 atomic_store(&ofproto->tables[i].config,
5795 (unsigned int)tm->config);
5797 } else if (!check_table_id(ofproto, tm->table_id)) {
5798 return OFPERR_OFPTMFC_BAD_TABLE;
5800 atomic_store(&ofproto->tables[tm->table_id].config,
5801 (unsigned int)tm->config);
5808 handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5810 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5811 struct ofputil_table_mod tm;
5814 error = reject_slave_controller(ofconn);
5819 error = ofputil_decode_table_mod(oh, &tm);
5824 return table_mod(ofproto, &tm);
5828 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5829 OVS_EXCLUDED(ofproto_mutex)
5831 const struct ofp_header *oh = msg->data;
5835 error = ofptype_decode(&type, oh);
5839 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
5840 && ofpmp_more(oh)) {
5841 /* We have no buffer implementation for multipart requests.
5842 * Report overflow for requests which consists of multiple
5844 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
5848 /* OpenFlow requests. */
5849 case OFPTYPE_ECHO_REQUEST:
5850 return handle_echo_request(ofconn, oh);
5852 case OFPTYPE_FEATURES_REQUEST:
5853 return handle_features_request(ofconn, oh);
5855 case OFPTYPE_GET_CONFIG_REQUEST:
5856 return handle_get_config_request(ofconn, oh);
5858 case OFPTYPE_SET_CONFIG:
5859 return handle_set_config(ofconn, oh);
5861 case OFPTYPE_PACKET_OUT:
5862 return handle_packet_out(ofconn, oh);
5864 case OFPTYPE_PORT_MOD:
5865 return handle_port_mod(ofconn, oh);
5867 case OFPTYPE_FLOW_MOD:
5868 return handle_flow_mod(ofconn, oh);
5870 case OFPTYPE_GROUP_MOD:
5871 return handle_group_mod(ofconn, oh);
5873 case OFPTYPE_TABLE_MOD:
5874 return handle_table_mod(ofconn, oh);
5876 case OFPTYPE_METER_MOD:
5877 return handle_meter_mod(ofconn, oh);
5879 case OFPTYPE_BARRIER_REQUEST:
5880 return handle_barrier_request(ofconn, oh);
5882 case OFPTYPE_ROLE_REQUEST:
5883 return handle_role_request(ofconn, oh);
5885 /* OpenFlow replies. */
5886 case OFPTYPE_ECHO_REPLY:
5889 /* Nicira extension requests. */
5890 case OFPTYPE_FLOW_MOD_TABLE_ID:
5891 return handle_nxt_flow_mod_table_id(ofconn, oh);
5893 case OFPTYPE_SET_FLOW_FORMAT:
5894 return handle_nxt_set_flow_format(ofconn, oh);
5896 case OFPTYPE_SET_PACKET_IN_FORMAT:
5897 return handle_nxt_set_packet_in_format(ofconn, oh);
5899 case OFPTYPE_SET_CONTROLLER_ID:
5900 return handle_nxt_set_controller_id(ofconn, oh);
5902 case OFPTYPE_FLOW_AGE:
5903 /* Nothing to do. */
5906 case OFPTYPE_FLOW_MONITOR_CANCEL:
5907 return handle_flow_monitor_cancel(ofconn, oh);
5909 case OFPTYPE_SET_ASYNC_CONFIG:
5910 return handle_nxt_set_async_config(ofconn, oh);
5912 case OFPTYPE_GET_ASYNC_REQUEST:
5913 return handle_nxt_get_async_request(ofconn, oh);
5915 /* Statistics requests. */
5916 case OFPTYPE_DESC_STATS_REQUEST:
5917 return handle_desc_stats_request(ofconn, oh);
5919 case OFPTYPE_FLOW_STATS_REQUEST:
5920 return handle_flow_stats_request(ofconn, oh);
5922 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5923 return handle_aggregate_stats_request(ofconn, oh);
5925 case OFPTYPE_TABLE_STATS_REQUEST:
5926 return handle_table_stats_request(ofconn, oh);
5928 case OFPTYPE_PORT_STATS_REQUEST:
5929 return handle_port_stats_request(ofconn, oh);
5931 case OFPTYPE_QUEUE_STATS_REQUEST:
5932 return handle_queue_stats_request(ofconn, oh);
5934 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5935 return handle_port_desc_stats_request(ofconn, oh);
5937 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5938 return handle_flow_monitor_request(ofconn, oh);
5940 case OFPTYPE_METER_STATS_REQUEST:
5941 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5942 return handle_meter_request(ofconn, oh, type);
5944 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5945 return handle_meter_features_request(ofconn, oh);
5947 case OFPTYPE_GROUP_STATS_REQUEST:
5948 return handle_group_stats_request(ofconn, oh);
5950 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5951 return handle_group_desc_stats_request(ofconn, oh);
5953 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5954 return handle_group_features_stats_request(ofconn, oh);
5956 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5957 return handle_queue_get_config_request(ofconn, oh);
5961 case OFPTYPE_FEATURES_REPLY:
5962 case OFPTYPE_GET_CONFIG_REPLY:
5963 case OFPTYPE_PACKET_IN:
5964 case OFPTYPE_FLOW_REMOVED:
5965 case OFPTYPE_PORT_STATUS:
5966 case OFPTYPE_BARRIER_REPLY:
5967 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5968 case OFPTYPE_DESC_STATS_REPLY:
5969 case OFPTYPE_FLOW_STATS_REPLY:
5970 case OFPTYPE_QUEUE_STATS_REPLY:
5971 case OFPTYPE_PORT_STATS_REPLY:
5972 case OFPTYPE_TABLE_STATS_REPLY:
5973 case OFPTYPE_AGGREGATE_STATS_REPLY:
5974 case OFPTYPE_PORT_DESC_STATS_REPLY:
5975 case OFPTYPE_ROLE_REPLY:
5976 case OFPTYPE_FLOW_MONITOR_PAUSED:
5977 case OFPTYPE_FLOW_MONITOR_RESUMED:
5978 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
5979 case OFPTYPE_GET_ASYNC_REPLY:
5980 case OFPTYPE_GROUP_STATS_REPLY:
5981 case OFPTYPE_GROUP_DESC_STATS_REPLY:
5982 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
5983 case OFPTYPE_METER_STATS_REPLY:
5984 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5985 case OFPTYPE_METER_FEATURES_STATS_REPLY:
5986 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
5987 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
5988 case OFPTYPE_ROLE_STATUS:
5990 if (ofpmsg_is_stat_request(oh)) {
5991 return OFPERR_OFPBRC_BAD_STAT;
5993 return OFPERR_OFPBRC_BAD_TYPE;
5999 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
6000 OVS_EXCLUDED(ofproto_mutex)
6002 int error = handle_openflow__(ofconn, ofp_msg);
6003 if (error && error != OFPROTO_POSTPONE) {
6004 ofconn_send_error(ofconn, ofp_msg->data, error);
6006 COVERAGE_INC(ofproto_recv_openflow);
6007 return error != OFPROTO_POSTPONE;
6010 /* Asynchronous operations. */
6012 /* Creates and returns a new ofopgroup that is not associated with any
6013 * OpenFlow connection.
6015 * The caller should add operations to the returned group with
6016 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6017 static struct ofopgroup *
6018 ofopgroup_create_unattached(struct ofproto *ofproto)
6019 OVS_REQUIRES(ofproto_mutex)
6021 struct ofopgroup *group = xzalloc(sizeof *group);
6022 group->ofproto = ofproto;
6023 list_init(&group->ofproto_node);
6024 list_init(&group->ops);
6025 list_init(&group->ofconn_node);
6029 /* Creates and returns a new ofopgroup for 'ofproto'.
6031 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
6032 * connection. The 'request' and 'buffer_id' arguments are ignored.
6034 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
6035 * If the ofopgroup eventually fails, then the error reply will include
6036 * 'request'. If the ofopgroup eventually succeeds, then the packet with
6037 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
6039 * The caller should add operations to the returned group with
6040 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6041 static struct ofopgroup *
6042 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
6043 const struct ofp_header *request, uint32_t buffer_id)
6044 OVS_REQUIRES(ofproto_mutex)
6046 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
6048 size_t request_len = ntohs(request->length);
6050 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
6052 ofconn_add_opgroup(ofconn, &group->ofconn_node);
6053 group->ofconn = ofconn;
6054 group->request = xmemdup(request, MIN(request_len, 64));
6055 group->buffer_id = buffer_id;
6060 /* Submits 'group' for processing.
6062 * If 'group' contains no operations (e.g. none were ever added, or all of the
6063 * ones that were added completed synchronously), then it is destroyed
6064 * immediately. Otherwise it is added to the ofproto's list of pending
6067 ofopgroup_submit(struct ofopgroup *group)
6068 OVS_REQUIRES(ofproto_mutex)
6070 if (!group->n_running) {
6071 ofopgroup_complete(group);
6073 list_push_back(&group->ofproto->pending, &group->ofproto_node);
6074 group->ofproto->n_pending++;
6079 ofopgroup_complete(struct ofopgroup *group)
6080 OVS_REQUIRES(ofproto_mutex)
6082 struct ofproto *ofproto = group->ofproto;
6084 struct ofconn *abbrev_ofconn;
6085 ovs_be32 abbrev_xid;
6087 struct ofoperation *op, *next_op;
6090 ovs_assert(!group->n_running);
6093 LIST_FOR_EACH (op, group_node, &group->ops) {
6100 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
6101 LIST_FOR_EACH (op, group_node, &group->ops) {
6102 if (op->type != OFOPERATION_DELETE) {
6103 struct ofpbuf *packet;
6106 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
6109 struct rule_execute *re;
6113 ofproto_rule_ref(op->rule);
6115 re = xmalloc(sizeof *re);
6116 re->rule = op->rule;
6117 re->in_port = in_port;
6118 re->packet = packet;
6120 if (!guarded_list_push_back(&ofproto->rule_executes,
6121 &re->list_node, 1024)) {
6122 ofproto_rule_unref(op->rule);
6123 ofpbuf_delete(re->packet);
6132 if (!error && !list_is_empty(&group->ofconn_node)) {
6133 abbrev_ofconn = group->ofconn;
6134 abbrev_xid = group->request->xid;
6136 abbrev_ofconn = NULL;
6137 abbrev_xid = htonl(0);
6139 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
6140 struct rule *rule = op->rule;
6142 /* We generally want to report the change to active OpenFlow flow
6143 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
6145 - The operation failed.
6147 - The affected rule is not visible to controllers.
6149 - The operation's only effect was to update rule->modified. */
6151 || ofproto_rule_is_hidden(rule)
6152 || (op->type == OFOPERATION_MODIFY
6154 && rule->flow_cookie == op->flow_cookie))) {
6155 /* Check that we can just cast from ofoperation_type to
6156 * nx_flow_update_event. */
6157 enum nx_flow_update_event event_type;
6160 case OFOPERATION_ADD:
6161 case OFOPERATION_REPLACE:
6162 event_type = NXFME_ADDED;
6165 case OFOPERATION_DELETE:
6166 event_type = NXFME_DELETED;
6169 case OFOPERATION_MODIFY:
6170 event_type = NXFME_MODIFIED;
6177 ofmonitor_report(ofproto->connmgr, rule, event_type,
6178 op->reason, abbrev_ofconn, abbrev_xid);
6181 rule->pending = NULL;
6184 case OFOPERATION_ADD:
6188 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
6189 if (vid_mask == VLAN_VID_MASK) {
6190 if (ofproto->vlan_bitmap) {
6191 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
6192 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
6193 bitmap_set1(ofproto->vlan_bitmap, vid);
6194 ofproto->vlans_changed = true;
6197 ofproto->vlans_changed = true;
6201 oftable_remove_rule(rule);
6202 ofproto_rule_unref(rule);
6206 case OFOPERATION_DELETE:
6207 ovs_assert(!op->error);
6208 ofproto_rule_unref(rule);
6212 case OFOPERATION_MODIFY:
6213 case OFOPERATION_REPLACE:
6215 long long int now = time_msec();
6217 rule->modified = now;
6218 if (op->type == OFOPERATION_REPLACE) {
6219 rule->created = rule->used = now;
6222 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
6223 ovs_mutex_lock(&rule->mutex);
6224 rule->idle_timeout = op->idle_timeout;
6225 rule->hard_timeout = op->hard_timeout;
6226 ovs_mutex_unlock(&rule->mutex);
6228 struct rule_actions *old_actions;
6230 ovs_mutex_lock(&rule->mutex);
6231 old_actions = rule->actions;
6232 rule->actions = op->actions;
6233 ovs_mutex_unlock(&rule->mutex);
6236 rule_actions_unref(old_actions);
6238 rule->flags = op->flags;
6246 ofoperation_destroy(op);
6249 ofmonitor_flush(ofproto->connmgr);
6251 if (!list_is_empty(&group->ofproto_node)) {
6252 ovs_assert(ofproto->n_pending > 0);
6253 ofproto->n_pending--;
6254 list_remove(&group->ofproto_node);
6256 if (!list_is_empty(&group->ofconn_node)) {
6257 list_remove(&group->ofconn_node);
6259 ofconn_send_error(group->ofconn, group->request, error);
6261 connmgr_retry(ofproto->connmgr);
6263 free(group->request);
6267 /* Initiates a new operation on 'rule', of the specified 'type', within
6268 * 'group'. Prior to calling, 'rule' must not have any pending operation.
6270 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
6271 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
6273 * Returns the newly created ofoperation (which is also available as
6274 * rule->pending). */
6275 static struct ofoperation *
6276 ofoperation_create(struct ofopgroup *group, struct rule *rule,
6277 enum ofoperation_type type,
6278 enum ofp_flow_removed_reason reason)
6279 OVS_REQUIRES(ofproto_mutex)
6281 struct ofproto *ofproto = group->ofproto;
6282 struct ofoperation *op;
6284 ovs_assert(!rule->pending);
6286 op = rule->pending = xzalloc(sizeof *op);
6288 list_push_back(&group->ops, &op->group_node);
6291 op->reason = reason;
6292 op->flow_cookie = rule->flow_cookie;
6293 ovs_mutex_lock(&rule->mutex);
6294 op->idle_timeout = rule->idle_timeout;
6295 op->hard_timeout = rule->hard_timeout;
6296 ovs_mutex_unlock(&rule->mutex);
6297 op->flags = rule->flags;
6301 if (type == OFOPERATION_DELETE) {
6302 hmap_insert(&ofproto->deletions, &op->hmap_node,
6303 cls_rule_hash(&rule->cr, rule->table_id));
6310 ofoperation_destroy(struct ofoperation *op)
6311 OVS_REQUIRES(ofproto_mutex)
6313 struct ofopgroup *group = op->group;
6316 op->rule->pending = NULL;
6318 if (op->type == OFOPERATION_DELETE) {
6319 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
6321 list_remove(&op->group_node);
6322 rule_actions_unref(op->actions);
6326 /* Indicates that 'op' completed with status 'error', which is either 0 to
6327 * indicate success or an OpenFlow error code on failure.
6329 * If 'error' is 0, indicating success, the operation will be committed
6330 * permanently to the flow table.
6332 * If 'error' is nonzero, then generally the operation will be rolled back:
6334 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
6335 * restores the original rule. The caller must have uninitialized any
6336 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
6337 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
6338 * and 7 for the new rule, calling its ->rule_dealloc() function.
6340 * - If 'op' is a "modify flow" operation, ofproto restores the original
6343 * - 'op' must not be a "delete flow" operation. Removing a rule is not
6344 * allowed to fail. It must always succeed.
6346 * Please see the large comment in ofproto/ofproto-provider.h titled
6347 * "Asynchronous Operation Support" for more information. */
6349 ofoperation_complete(struct ofoperation *op, enum ofperr error)
6351 struct ofopgroup *group = op->group;
6353 ovs_assert(group->n_running > 0);
6354 ovs_assert(!error || op->type != OFOPERATION_DELETE);
6357 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
6358 /* This function can be called from ->rule_construct(), in which case
6359 * ofproto_mutex is held, or it can be called from ->run(), in which
6360 * case ofproto_mutex is not held. But only in the latter case can we
6361 * arrive here, so we can safely take ofproto_mutex now. */
6362 ovs_mutex_lock(&ofproto_mutex);
6363 ovs_assert(op->rule->pending == op);
6364 ofopgroup_complete(group);
6365 ovs_mutex_unlock(&ofproto_mutex);
6370 pick_datapath_id(const struct ofproto *ofproto)
6372 const struct ofport *port;
6374 port = ofproto_get_port(ofproto, OFPP_LOCAL);
6376 uint8_t ea[ETH_ADDR_LEN];
6379 error = netdev_get_etheraddr(port->netdev, ea);
6381 return eth_addr_to_uint64(ea);
6383 VLOG_WARN("%s: could not get MAC address for %s (%s)",
6384 ofproto->name, netdev_get_name(port->netdev),
6385 ovs_strerror(error));
6387 return ofproto->fallback_dpid;
6391 pick_fallback_dpid(void)
6393 uint8_t ea[ETH_ADDR_LEN];
6394 eth_addr_nicira_random(ea);
6395 return eth_addr_to_uint64(ea);
6398 /* Table overflow policy. */
6400 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
6401 * to NULL if the table is not configured to evict rules or if the table
6402 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
6403 * or with no timeouts are not evictable.) */
6405 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
6406 OVS_REQUIRES(ofproto_mutex)
6408 struct eviction_group *evg;
6411 if (!table->eviction_fields) {
6415 /* In the common case, the outer and inner loops here will each be entered
6418 * - The inner loop normally "return"s in its first iteration. If the
6419 * eviction group has any evictable rules, then it always returns in
6422 * - The outer loop only iterates more than once if the largest eviction
6423 * group has no evictable rules.
6425 * - The outer loop can exit only if table's 'max_flows' is all filled up
6426 * by unevictable rules. */
6427 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
6430 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
6439 /* Searches 'ofproto' for tables that have more flows than their configured
6440 * maximum and that have flow eviction enabled, and evicts as many flows as
6441 * necessary and currently feasible from them.
6443 * This triggers only when an OpenFlow table has N flows in it and then the
6444 * client configures a maximum number of flows less than N. */
6446 ofproto_evict(struct ofproto *ofproto)
6448 struct oftable *table;
6450 ovs_mutex_lock(&ofproto_mutex);
6451 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
6452 evict_rules_from_table(ofproto, table, 0);
6454 ovs_mutex_unlock(&ofproto_mutex);
6457 /* Eviction groups. */
6459 /* Returns the priority to use for an eviction_group that contains 'n_rules'
6460 * rules. The priority contains low-order random bits to ensure that eviction
6461 * groups with the same number of rules are prioritized randomly. */
6463 eviction_group_priority(size_t n_rules)
6465 uint16_t size = MIN(UINT16_MAX, n_rules);
6466 return (size << 16) | random_uint16();
6469 /* Updates 'evg', an eviction_group within 'table', following a change that
6470 * adds or removes rules in 'evg'. */
6472 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
6473 OVS_REQUIRES(ofproto_mutex)
6475 heap_change(&table->eviction_groups_by_size, &evg->size_node,
6476 eviction_group_priority(heap_count(&evg->rules)));
6479 /* Destroys 'evg', an eviction_group within 'table':
6481 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
6482 * rules themselves, just removes them from the eviction group.)
6484 * - Removes 'evg' from 'table'.
6488 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
6489 OVS_REQUIRES(ofproto_mutex)
6491 while (!heap_is_empty(&evg->rules)) {
6494 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
6495 rule->eviction_group = NULL;
6497 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
6498 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
6499 heap_destroy(&evg->rules);
6503 /* Removes 'rule' from its eviction group, if any. */
6505 eviction_group_remove_rule(struct rule *rule)
6506 OVS_REQUIRES(ofproto_mutex)
6508 if (rule->eviction_group) {
6509 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6510 struct eviction_group *evg = rule->eviction_group;
6512 rule->eviction_group = NULL;
6513 heap_remove(&evg->rules, &rule->evg_node);
6514 if (heap_is_empty(&evg->rules)) {
6515 eviction_group_destroy(table, evg);
6517 eviction_group_resized(table, evg);
6522 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6523 * returns the hash value. */
6525 eviction_group_hash_rule(struct rule *rule)
6526 OVS_REQUIRES(ofproto_mutex)
6528 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6529 const struct mf_subfield *sf;
6533 hash = table->eviction_group_id_basis;
6534 miniflow_expand(&rule->cr.match.flow, &flow);
6535 for (sf = table->eviction_fields;
6536 sf < &table->eviction_fields[table->n_eviction_fields];
6539 if (mf_are_prereqs_ok(sf->field, &flow)) {
6540 union mf_value value;
6542 mf_get_value(sf->field, &flow, &value);
6544 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
6546 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
6547 unsigned int start = sf->ofs + sf->n_bits;
6548 bitwise_zero(&value, sf->field->n_bytes, start,
6549 sf->field->n_bytes * 8 - start);
6551 hash = hash_bytes(&value, sf->field->n_bytes, hash);
6553 hash = hash_int(hash, 0);
6560 /* Returns an eviction group within 'table' with the given 'id', creating one
6562 static struct eviction_group *
6563 eviction_group_find(struct oftable *table, uint32_t id)
6564 OVS_REQUIRES(ofproto_mutex)
6566 struct eviction_group *evg;
6568 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6572 evg = xmalloc(sizeof *evg);
6573 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6574 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6575 eviction_group_priority(0));
6576 heap_init(&evg->rules);
6581 /* Returns an eviction priority for 'rule'. The return value should be
6582 * interpreted so that higher priorities make a rule more attractive candidates
6585 rule_eviction_priority(struct rule *rule)
6586 OVS_REQUIRES(ofproto_mutex)
6588 long long int hard_expiration;
6589 long long int idle_expiration;
6590 long long int expiration;
6591 uint32_t expiration_offset;
6593 /* Calculate time of expiration. */
6594 ovs_mutex_lock(&rule->mutex);
6595 hard_expiration = (rule->hard_timeout
6596 ? rule->modified + rule->hard_timeout * 1000
6598 idle_expiration = (rule->idle_timeout
6599 ? rule->used + rule->idle_timeout * 1000
6601 expiration = MIN(hard_expiration, idle_expiration);
6602 ovs_mutex_unlock(&rule->mutex);
6603 if (expiration == LLONG_MAX) {
6607 /* Calculate the time of expiration as a number of (approximate) seconds
6608 * after program startup.
6610 * This should work OK for program runs that last UINT32_MAX seconds or
6611 * less. Therefore, please restart OVS at least once every 136 years. */
6612 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6614 /* Invert the expiration offset because we're using a max-heap. */
6615 return UINT32_MAX - expiration_offset;
6618 /* Adds 'rule' to an appropriate eviction group for its oftable's
6619 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6620 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6623 * The caller must ensure that 'rule' is not already in an eviction group. */
6625 eviction_group_add_rule(struct rule *rule)
6626 OVS_REQUIRES(ofproto_mutex)
6628 struct ofproto *ofproto = rule->ofproto;
6629 struct oftable *table = &ofproto->tables[rule->table_id];
6632 ovs_mutex_lock(&rule->mutex);
6633 has_timeout = rule->hard_timeout || rule->idle_timeout;
6634 ovs_mutex_unlock(&rule->mutex);
6636 if (table->eviction_fields && has_timeout) {
6637 struct eviction_group *evg;
6639 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6641 rule->eviction_group = evg;
6642 heap_insert(&evg->rules, &rule->evg_node,
6643 rule_eviction_priority(rule));
6644 eviction_group_resized(table, evg);
6650 /* Initializes 'table'. */
6652 oftable_init(struct oftable *table)
6654 memset(table, 0, sizeof *table);
6655 classifier_init(&table->cls, flow_segment_u32s);
6656 table->max_flows = UINT_MAX;
6657 atomic_init(&table->config, (unsigned int)OFPTC11_TABLE_MISS_CONTROLLER);
6660 /* Destroys 'table', including its classifier and eviction groups.
6662 * The caller is responsible for freeing 'table' itself. */
6664 oftable_destroy(struct oftable *table)
6666 fat_rwlock_rdlock(&table->cls.rwlock);
6667 ovs_assert(classifier_is_empty(&table->cls));
6668 fat_rwlock_unlock(&table->cls.rwlock);
6669 oftable_disable_eviction(table);
6670 classifier_destroy(&table->cls);
6674 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6675 * string, then 'table' will use its default name.
6677 * This only affects the name exposed for a table exposed through the OpenFlow
6678 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6680 oftable_set_name(struct oftable *table, const char *name)
6682 if (name && name[0]) {
6683 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6684 if (!table->name || strncmp(name, table->name, len)) {
6686 table->name = xmemdup0(name, len);
6694 /* oftables support a choice of two policies when adding a rule would cause the
6695 * number of flows in the table to exceed the configured maximum number: either
6696 * they can refuse to add the new flow or they can evict some existing flow.
6697 * This function configures the former policy on 'table'. */
6699 oftable_disable_eviction(struct oftable *table)
6700 OVS_REQUIRES(ofproto_mutex)
6702 if (table->eviction_fields) {
6703 struct eviction_group *evg, *next;
6705 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6706 &table->eviction_groups_by_id) {
6707 eviction_group_destroy(table, evg);
6709 hmap_destroy(&table->eviction_groups_by_id);
6710 heap_destroy(&table->eviction_groups_by_size);
6712 free(table->eviction_fields);
6713 table->eviction_fields = NULL;
6714 table->n_eviction_fields = 0;
6718 /* oftables support a choice of two policies when adding a rule would cause the
6719 * number of flows in the table to exceed the configured maximum number: either
6720 * they can refuse to add the new flow or they can evict some existing flow.
6721 * This function configures the latter policy on 'table', with fairness based
6722 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6723 * 'n_fields' as 0 disables fairness.) */
6725 oftable_enable_eviction(struct oftable *table,
6726 const struct mf_subfield *fields, size_t n_fields)
6727 OVS_REQUIRES(ofproto_mutex)
6729 struct cls_cursor cursor;
6732 if (table->eviction_fields
6733 && n_fields == table->n_eviction_fields
6735 || !memcmp(fields, table->eviction_fields,
6736 n_fields * sizeof *fields))) {
6741 oftable_disable_eviction(table);
6743 table->n_eviction_fields = n_fields;
6744 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6746 table->eviction_group_id_basis = random_uint32();
6747 hmap_init(&table->eviction_groups_by_id);
6748 heap_init(&table->eviction_groups_by_size);
6750 fat_rwlock_rdlock(&table->cls.rwlock);
6751 cls_cursor_init(&cursor, &table->cls, NULL);
6752 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6753 eviction_group_add_rule(rule);
6755 fat_rwlock_unlock(&table->cls.rwlock);
6758 /* Removes 'rule' from the oftable that contains it. */
6760 oftable_remove_rule__(struct ofproto *ofproto, struct rule *rule)
6761 OVS_REQUIRES(ofproto_mutex)
6763 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
6765 fat_rwlock_wrlock(&cls->rwlock);
6766 classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
6767 fat_rwlock_unlock(&cls->rwlock);
6769 cookies_remove(ofproto, rule);
6771 eviction_group_remove_rule(rule);
6772 if (!list_is_empty(&rule->expirable)) {
6773 list_remove(&rule->expirable);
6775 if (!list_is_empty(&rule->meter_list_node)) {
6776 list_remove(&rule->meter_list_node);
6777 list_init(&rule->meter_list_node);
6782 oftable_remove_rule(struct rule *rule)
6783 OVS_REQUIRES(ofproto_mutex)
6785 oftable_remove_rule__(rule->ofproto, rule);
6788 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6789 * the same cls_rule. */
6791 oftable_insert_rule(struct rule *rule)
6792 OVS_REQUIRES(ofproto_mutex)
6794 struct ofproto *ofproto = rule->ofproto;
6795 struct oftable *table = &ofproto->tables[rule->table_id];
6798 ovs_mutex_lock(&rule->mutex);
6799 may_expire = rule->hard_timeout || rule->idle_timeout;
6800 ovs_mutex_unlock(&rule->mutex);
6803 list_insert(&ofproto->expirable, &rule->expirable);
6806 cookies_insert(ofproto, rule);
6808 if (rule->actions->provider_meter_id != UINT32_MAX) {
6809 uint32_t meter_id = ofpacts_get_meter(rule->actions->ofpacts,
6810 rule->actions->ofpacts_len);
6811 struct meter *meter = ofproto->meters[meter_id];
6812 list_insert(&meter->rules, &rule->meter_list_node);
6814 fat_rwlock_wrlock(&table->cls.rwlock);
6815 classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
6816 fat_rwlock_unlock(&table->cls.rwlock);
6817 eviction_group_add_rule(rule);
6820 /* unixctl commands. */
6823 ofproto_lookup(const char *name)
6825 struct ofproto *ofproto;
6827 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6829 if (!strcmp(ofproto->name, name)) {
6837 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6838 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6840 struct ofproto *ofproto;
6844 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6845 ds_put_format(&results, "%s\n", ofproto->name);
6847 unixctl_command_reply(conn, ds_cstr(&results));
6848 ds_destroy(&results);
6852 ofproto_unixctl_init(void)
6854 static bool registered;
6860 unixctl_command_register("ofproto/list", "", 0, 0,
6861 ofproto_unixctl_list, NULL);
6864 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6866 * This is deprecated. It is only for compatibility with broken device drivers
6867 * in old versions of Linux that do not properly support VLANs when VLAN
6868 * devices are not used. When broken device drivers are no longer in
6869 * widespread use, we will delete these interfaces. */
6871 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6872 * (exactly) by an OpenFlow rule in 'ofproto'. */
6874 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6876 const struct oftable *oftable;
6878 free(ofproto->vlan_bitmap);
6879 ofproto->vlan_bitmap = bitmap_allocate(4096);
6880 ofproto->vlans_changed = false;
6882 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6883 const struct cls_subtable *table;
6885 fat_rwlock_rdlock(&oftable->cls.rwlock);
6886 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.subtables) {
6887 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6888 const struct cls_rule *rule;
6890 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6891 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6892 bitmap_set1(vlan_bitmap, vid);
6893 bitmap_set1(ofproto->vlan_bitmap, vid);
6897 fat_rwlock_unlock(&oftable->cls.rwlock);
6901 /* Returns true if new VLANs have come into use by the flow table since the
6902 * last call to ofproto_get_vlan_usage().
6904 * We don't track when old VLANs stop being used. */
6906 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6908 return ofproto->vlans_changed;
6911 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6912 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6913 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6914 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6915 * then the VLAN device is un-enslaved. */
6917 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6918 ofp_port_t realdev_ofp_port, int vid)
6920 struct ofport *ofport;
6923 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6925 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6927 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6928 ofproto->name, vlandev_ofp_port);
6932 if (!ofproto->ofproto_class->set_realdev) {
6933 if (!vlandev_ofp_port) {
6936 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6940 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6942 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6943 ofproto->name, vlandev_ofp_port,
6944 netdev_get_name(ofport->netdev), ovs_strerror(error));