2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 ofproto *ofproto, 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 unsigned ofproto_max_idle = OFPROTO_MAX_IDLE_DEFAULT;
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 maximum idle time for flows in the datapath before they are
704 ofproto_set_max_idle(unsigned max_idle)
706 ofproto_max_idle = max_idle;
709 /* If forward_bpdu is true, the NORMAL action will forward frames with
710 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
711 * the NORMAL action will drop these frames. */
713 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
715 bool old_val = ofproto->forward_bpdu;
716 ofproto->forward_bpdu = forward_bpdu;
717 if (old_val != ofproto->forward_bpdu) {
718 if (ofproto->ofproto_class->forward_bpdu_changed) {
719 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
724 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
725 * 'idle_time', in seconds, and the maximum number of MAC table entries to
728 ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
731 if (ofproto->ofproto_class->set_mac_table_config) {
732 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
738 ofproto_set_threads(int n_handlers_, int n_revalidators_)
740 int threads = MAX(count_cpu_cores(), 2);
742 n_revalidators = MAX(n_revalidators_, 0);
743 n_handlers = MAX(n_handlers_, 0);
745 if (!n_revalidators) {
746 n_revalidators = n_handlers
747 ? MAX(threads - (int) n_handlers, 1)
752 n_handlers = MAX(threads - (int) n_revalidators, 1);
757 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
760 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
764 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
766 return connmgr_set_snoops(ofproto->connmgr, snoops);
770 ofproto_set_netflow(struct ofproto *ofproto,
771 const struct netflow_options *nf_options)
773 if (nf_options && sset_is_empty(&nf_options->collectors)) {
777 if (ofproto->ofproto_class->set_netflow) {
778 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
780 return nf_options ? EOPNOTSUPP : 0;
785 ofproto_set_sflow(struct ofproto *ofproto,
786 const struct ofproto_sflow_options *oso)
788 if (oso && sset_is_empty(&oso->targets)) {
792 if (ofproto->ofproto_class->set_sflow) {
793 return ofproto->ofproto_class->set_sflow(ofproto, oso);
795 return oso ? EOPNOTSUPP : 0;
800 ofproto_set_ipfix(struct ofproto *ofproto,
801 const struct ofproto_ipfix_bridge_exporter_options *bo,
802 const struct ofproto_ipfix_flow_exporter_options *fo,
805 if (ofproto->ofproto_class->set_ipfix) {
806 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
808 return (bo || fo) ? EOPNOTSUPP : 0;
813 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
815 flow_restore_wait = flow_restore_wait_db;
819 ofproto_get_flow_restore_wait(void)
821 return flow_restore_wait;
825 /* Spanning Tree Protocol (STP) configuration. */
827 /* Configures STP on 'ofproto' using the settings defined in 's'. If
828 * 's' is NULL, disables STP.
830 * Returns 0 if successful, otherwise a positive errno value. */
832 ofproto_set_stp(struct ofproto *ofproto,
833 const struct ofproto_stp_settings *s)
835 return (ofproto->ofproto_class->set_stp
836 ? ofproto->ofproto_class->set_stp(ofproto, s)
840 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
841 * 'enabled' member of 's' is false, then the other members are not
844 * Returns 0 if successful, otherwise a positive errno value. */
846 ofproto_get_stp_status(struct ofproto *ofproto,
847 struct ofproto_stp_status *s)
849 return (ofproto->ofproto_class->get_stp_status
850 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
854 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
855 * in 's'. The caller is responsible for assigning STP port numbers
856 * (using the 'port_num' member in the range of 1 through 255, inclusive)
857 * and ensuring there are no duplicates. If the 's' is NULL, then STP
858 * is disabled on the port.
860 * Returns 0 if successful, otherwise a positive errno value.*/
862 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
863 const struct ofproto_port_stp_settings *s)
865 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
867 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
868 ofproto->name, ofp_port);
872 return (ofproto->ofproto_class->set_stp_port
873 ? ofproto->ofproto_class->set_stp_port(ofport, s)
877 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
878 * 's'. If the 'enabled' member in 's' is false, then the other members
879 * are not meaningful.
881 * Returns 0 if successful, otherwise a positive errno value.*/
883 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
884 struct ofproto_port_stp_status *s)
886 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
888 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
889 "port %"PRIu16, ofproto->name, ofp_port);
893 return (ofproto->ofproto_class->get_stp_port_status
894 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
898 /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
899 * 's'. If the 'enabled' member in 's' is false, then the other members
900 * are not meaningful.
902 * Returns 0 if successful, otherwise a positive errno value.*/
904 ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port,
905 struct ofproto_port_stp_stats *s)
907 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
909 VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent "
910 "port %"PRIu16, ofproto->name, ofp_port);
914 return (ofproto->ofproto_class->get_stp_port_stats
915 ? ofproto->ofproto_class->get_stp_port_stats(ofport, s)
919 /* Queue DSCP configuration. */
921 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
922 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
923 * to implement QoS. Instead, it is used to implement features which require
924 * knowledge of what queues exist on a port, and some basic information about
927 * Returns 0 if successful, otherwise a positive errno value. */
929 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
930 const struct ofproto_port_queue *queues,
933 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
936 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
937 ofproto->name, ofp_port);
941 return (ofproto->ofproto_class->set_queues
942 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
946 /* Connectivity Fault Management configuration. */
948 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
950 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
952 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
953 if (ofport && ofproto->ofproto_class->set_cfm) {
954 ofproto->ofproto_class->set_cfm(ofport, NULL);
958 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
959 * basic configuration from the configuration members in 'cfm', and the remote
960 * maintenance point ID from remote_mpid. Ignores the statistics members of
963 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
965 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
966 const struct cfm_settings *s)
968 struct ofport *ofport;
971 ofport = ofproto_get_port(ofproto, ofp_port);
973 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
974 ofproto->name, ofp_port);
978 /* XXX: For configuration simplicity, we only support one remote_mpid
979 * outside of the CFM module. It's not clear if this is the correct long
980 * term solution or not. */
981 error = (ofproto->ofproto_class->set_cfm
982 ? ofproto->ofproto_class->set_cfm(ofport, s)
985 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
986 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
987 ovs_strerror(error));
991 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
992 * 'ofproto' does not have a port 'ofp_port'. */
994 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
995 const struct smap *cfg)
997 struct ofport *ofport;
1000 ofport = ofproto_get_port(ofproto, ofp_port);
1002 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
1003 ofproto->name, ofp_port);
1007 error = (ofproto->ofproto_class->set_bfd
1008 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
1011 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
1012 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
1013 ovs_strerror(error));
1017 /* Populates 'status' with key value pairs indicating the status of the BFD
1018 * session on 'ofp_port'. This information is intended to be populated in the
1019 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
1022 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
1023 struct smap *status)
1025 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1026 return (ofport && ofproto->ofproto_class->get_bfd_status
1027 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
1031 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1032 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1033 * 0 if LACP partner information is not current (generally indicating a
1034 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1036 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
1038 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1039 return (ofport && ofproto->ofproto_class->port_is_lacp_current
1040 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
1046 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1047 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1048 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1049 * configuration plus, if there is more than one slave, a bonding
1052 * If 'aux' is already registered then this function updates its configuration
1053 * to 's'. Otherwise, this function registers a new bundle.
1055 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1058 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
1059 const struct ofproto_bundle_settings *s)
1061 return (ofproto->ofproto_class->bundle_set
1062 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
1066 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1067 * If no such bundle has been registered, this has no effect. */
1069 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
1071 return ofproto_bundle_register(ofproto, aux, NULL);
1075 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1076 * If 'aux' is already registered then this function updates its configuration
1077 * to 's'. Otherwise, this function registers a new mirror. */
1079 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1080 const struct ofproto_mirror_settings *s)
1082 return (ofproto->ofproto_class->mirror_set
1083 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1087 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1088 * If no mirror has been registered, this has no effect. */
1090 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
1092 return ofproto_mirror_register(ofproto, aux, NULL);
1095 /* Retrieves statistics from mirror associated with client data pointer
1096 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1097 * 'bytes', respectively. If a particular counters is not supported,
1098 * the appropriate argument is set to UINT64_MAX. */
1100 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1101 uint64_t *packets, uint64_t *bytes)
1103 if (!ofproto->ofproto_class->mirror_get_stats) {
1104 *packets = *bytes = UINT64_MAX;
1108 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1112 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1113 * which all packets are flooded, instead of using MAC learning. If
1114 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1116 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1119 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
1121 return (ofproto->ofproto_class->set_flood_vlans
1122 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1126 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1127 * output for a mirror. */
1129 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1131 return (ofproto->ofproto_class->is_mirror_output_bundle
1132 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1136 /* Configuration of OpenFlow tables. */
1138 /* Returns the number of OpenFlow tables in 'ofproto'. */
1140 ofproto_get_n_tables(const struct ofproto *ofproto)
1142 return ofproto->n_tables;
1145 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1146 * settings from 's'. 'table_id' must be in the range 0 through the number of
1147 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1149 * For read-only tables, only the name may be configured. */
1151 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1152 const struct ofproto_table_settings *s)
1154 struct oftable *table;
1156 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1157 table = &ofproto->tables[table_id];
1159 oftable_set_name(table, s->name);
1161 if (table->flags & OFTABLE_READONLY) {
1166 oftable_enable_eviction(table, s->groups, s->n_groups);
1168 oftable_disable_eviction(table);
1171 table->max_flows = s->max_flows;
1172 fat_rwlock_wrlock(&table->cls.rwlock);
1173 if (classifier_count(&table->cls) > table->max_flows
1174 && table->eviction_fields) {
1175 /* 'table' contains more flows than allowed. We might not be able to
1176 * evict them right away because of the asynchronous nature of flow
1177 * table changes. Schedule eviction for later. */
1178 switch (ofproto->state) {
1180 ofproto->state = S_EVICT;
1184 /* We're already deleting flows, nothing more to do. */
1189 classifier_set_prefix_fields(&table->cls,
1190 s->prefix_fields, s->n_prefix_fields);
1192 fat_rwlock_unlock(&table->cls.rwlock);
1196 ofproto_has_snoops(const struct ofproto *ofproto)
1198 return connmgr_has_snoops(ofproto->connmgr);
1202 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1204 connmgr_get_snoops(ofproto->connmgr, snoops);
1208 ofproto_rule_delete__(struct ofproto *ofproto, struct rule *rule,
1210 OVS_REQUIRES(ofproto_mutex)
1212 struct ofopgroup *group;
1214 ovs_assert(!rule->pending);
1216 group = ofopgroup_create_unattached(ofproto);
1217 delete_flow__(rule, group, reason);
1218 ofopgroup_submit(group);
1221 /* Deletes 'rule' from 'ofproto'.
1223 * Within an ofproto implementation, this function allows an ofproto
1224 * implementation to destroy any rules that remain when its ->destruct()
1225 * function is called. This function is not suitable for use elsewhere in an
1226 * ofproto implementation.
1228 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1229 * Cycle" in ofproto-provider.h. */
1231 ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule)
1232 OVS_EXCLUDED(ofproto_mutex)
1234 struct ofopgroup *group;
1236 ovs_mutex_lock(&ofproto_mutex);
1237 ovs_assert(!rule->pending);
1239 group = ofopgroup_create_unattached(ofproto);
1240 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1241 oftable_remove_rule__(ofproto, rule);
1242 ofproto->ofproto_class->rule_delete(rule);
1243 ofopgroup_submit(group);
1245 ovs_mutex_unlock(&ofproto_mutex);
1249 ofproto_flush__(struct ofproto *ofproto)
1250 OVS_EXCLUDED(ofproto_mutex)
1252 struct oftable *table;
1254 if (ofproto->ofproto_class->flush) {
1255 ofproto->ofproto_class->flush(ofproto);
1258 ovs_mutex_lock(&ofproto_mutex);
1259 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1260 struct rule *rule, *next_rule;
1261 struct cls_cursor cursor;
1263 if (table->flags & OFTABLE_HIDDEN) {
1267 fat_rwlock_rdlock(&table->cls.rwlock);
1268 cls_cursor_init(&cursor, &table->cls, NULL);
1269 fat_rwlock_unlock(&table->cls.rwlock);
1270 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1271 if (!rule->pending) {
1272 ofproto_rule_delete__(ofproto, rule, OFPRR_DELETE);
1276 ovs_mutex_unlock(&ofproto_mutex);
1279 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1282 ofproto_destroy__(struct ofproto *ofproto)
1283 OVS_EXCLUDED(ofproto_mutex)
1285 struct oftable *table;
1287 ovs_assert(list_is_empty(&ofproto->pending));
1289 destroy_rule_executes(ofproto);
1290 guarded_list_destroy(&ofproto->rule_executes);
1292 delete_group(ofproto, OFPG_ALL);
1293 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1294 hmap_destroy(&ofproto->groups);
1296 connmgr_destroy(ofproto->connmgr);
1298 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1299 free(ofproto->name);
1300 free(ofproto->type);
1301 free(ofproto->mfr_desc);
1302 free(ofproto->hw_desc);
1303 free(ofproto->sw_desc);
1304 free(ofproto->serial_desc);
1305 free(ofproto->dp_desc);
1306 hmap_destroy(&ofproto->ports);
1307 hmap_destroy(&ofproto->ofport_usage);
1308 shash_destroy(&ofproto->port_by_name);
1309 simap_destroy(&ofproto->ofp_requests);
1311 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1312 oftable_destroy(table);
1314 free(ofproto->tables);
1316 hmap_destroy(&ofproto->deletions);
1318 free(ofproto->vlan_bitmap);
1320 ofproto->ofproto_class->dealloc(ofproto);
1324 ofproto_destroy(struct ofproto *p)
1325 OVS_EXCLUDED(ofproto_mutex)
1327 struct ofport *ofport, *next_ofport;
1328 struct ofport_usage *usage, *next_usage;
1335 meter_delete(p, 1, p->meter_features.max_meters);
1336 p->meter_features.max_meters = 0;
1342 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1343 ofport_destroy(ofport);
1346 HMAP_FOR_EACH_SAFE (usage, next_usage, hmap_node, &p->ofport_usage) {
1347 hmap_remove(&p->ofport_usage, &usage->hmap_node);
1351 p->ofproto_class->destruct(p);
1352 ofproto_destroy__(p);
1355 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1356 * kernel datapath, for example, this destroys the datapath in the kernel, and
1357 * with the netdev-based datapath, it tears down the data structures that
1358 * represent the datapath.
1360 * The datapath should not be currently open as an ofproto. */
1362 ofproto_delete(const char *name, const char *type)
1364 const struct ofproto_class *class = ofproto_class_find__(type);
1365 return (!class ? EAFNOSUPPORT
1366 : !class->del ? EACCES
1367 : class->del(type, name));
1371 process_port_change(struct ofproto *ofproto, int error, char *devname)
1373 if (error == ENOBUFS) {
1374 reinit_ports(ofproto);
1375 } else if (!error) {
1376 update_port(ofproto, devname);
1382 ofproto_type_run(const char *datapath_type)
1384 const struct ofproto_class *class;
1387 datapath_type = ofproto_normalize_type(datapath_type);
1388 class = ofproto_class_find__(datapath_type);
1390 error = class->type_run ? class->type_run(datapath_type) : 0;
1391 if (error && error != EAGAIN) {
1392 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1393 datapath_type, ovs_strerror(error));
1399 ofproto_type_wait(const char *datapath_type)
1401 const struct ofproto_class *class;
1403 datapath_type = ofproto_normalize_type(datapath_type);
1404 class = ofproto_class_find__(datapath_type);
1406 if (class->type_wait) {
1407 class->type_wait(datapath_type);
1412 any_pending_ops(const struct ofproto *p)
1413 OVS_EXCLUDED(ofproto_mutex)
1417 ovs_mutex_lock(&ofproto_mutex);
1418 b = !list_is_empty(&p->pending);
1419 ovs_mutex_unlock(&ofproto_mutex);
1425 ofproto_run(struct ofproto *p)
1430 error = p->ofproto_class->run(p);
1431 if (error && error != EAGAIN) {
1432 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1435 run_rule_executes(p);
1437 /* Restore the eviction group heap invariant occasionally. */
1438 if (p->eviction_group_timer < time_msec()) {
1441 p->eviction_group_timer = time_msec() + 1000;
1443 for (i = 0; i < p->n_tables; i++) {
1444 struct oftable *table = &p->tables[i];
1445 struct eviction_group *evg;
1446 struct cls_cursor cursor;
1449 if (!table->eviction_fields) {
1453 ovs_mutex_lock(&ofproto_mutex);
1454 fat_rwlock_rdlock(&table->cls.rwlock);
1455 cls_cursor_init(&cursor, &table->cls, NULL);
1456 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1457 if (rule->idle_timeout || rule->hard_timeout) {
1458 if (!rule->eviction_group) {
1459 eviction_group_add_rule(rule);
1461 heap_raw_change(&rule->evg_node,
1462 rule_eviction_priority(p, rule));
1466 fat_rwlock_unlock(&table->cls.rwlock);
1468 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1469 heap_rebuild(&evg->rules);
1471 ovs_mutex_unlock(&ofproto_mutex);
1475 if (p->ofproto_class->port_poll) {
1478 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1479 process_port_change(p, error, devname);
1483 new_seq = seq_read(connectivity_seq_get());
1484 if (new_seq != p->change_seq) {
1485 struct sset devnames;
1486 const char *devname;
1487 struct ofport *ofport;
1489 /* Update OpenFlow port status for any port whose netdev has changed.
1491 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1492 * destroyed, so it's not safe to update ports directly from the
1493 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1494 * need this two-phase approach. */
1495 sset_init(&devnames);
1496 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1497 sset_add(&devnames, netdev_get_name(ofport->netdev));
1499 SSET_FOR_EACH (devname, &devnames) {
1500 update_port(p, devname);
1502 sset_destroy(&devnames);
1504 p->change_seq = new_seq;
1509 connmgr_run(p->connmgr, handle_openflow);
1513 connmgr_run(p->connmgr, NULL);
1515 if (!any_pending_ops(p)) {
1516 p->state = S_OPENFLOW;
1521 connmgr_run(p->connmgr, NULL);
1523 if (!any_pending_ops(p)) {
1524 connmgr_flushed(p->connmgr);
1525 p->state = S_OPENFLOW;
1533 if (time_msec() >= p->next_op_report) {
1534 long long int ago = (time_msec() - p->first_op) / 1000;
1535 long long int interval = (p->last_op - p->first_op) / 1000;
1539 ds_put_format(&s, "%d flow_mods ",
1540 p->n_add + p->n_delete + p->n_modify);
1541 if (interval == ago) {
1542 ds_put_format(&s, "in the last %lld s", ago);
1543 } else if (interval) {
1544 ds_put_format(&s, "in the %lld s starting %lld s ago",
1547 ds_put_format(&s, "%lld s ago", ago);
1550 ds_put_cstr(&s, " (");
1552 ds_put_format(&s, "%d adds, ", p->n_add);
1555 ds_put_format(&s, "%d deletes, ", p->n_delete);
1558 ds_put_format(&s, "%d modifications, ", p->n_modify);
1561 ds_put_char(&s, ')');
1563 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1566 p->n_add = p->n_delete = p->n_modify = 0;
1567 p->next_op_report = LLONG_MAX;
1574 ofproto_wait(struct ofproto *p)
1576 p->ofproto_class->wait(p);
1577 if (p->ofproto_class->port_poll_wait) {
1578 p->ofproto_class->port_poll_wait(p);
1580 seq_wait(connectivity_seq_get(), p->change_seq);
1584 connmgr_wait(p->connmgr, true);
1589 connmgr_wait(p->connmgr, false);
1590 if (!any_pending_ops(p)) {
1591 poll_immediate_wake();
1598 ofproto_is_alive(const struct ofproto *p)
1600 return connmgr_has_controllers(p->connmgr);
1603 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1604 * memory_report(). */
1606 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1608 const struct oftable *table;
1609 unsigned int n_rules;
1611 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1613 ovs_mutex_lock(&ofproto_mutex);
1614 simap_increase(usage, "ops",
1615 ofproto->n_pending + hmap_count(&ofproto->deletions));
1616 ovs_mutex_unlock(&ofproto_mutex);
1619 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1620 fat_rwlock_rdlock(&table->cls.rwlock);
1621 n_rules += classifier_count(&table->cls);
1622 fat_rwlock_unlock(&table->cls.rwlock);
1624 simap_increase(usage, "rules", n_rules);
1626 if (ofproto->ofproto_class->get_memory_usage) {
1627 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1630 connmgr_get_memory_usage(ofproto->connmgr, usage);
1634 ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage)
1636 const struct ofproto_class *class;
1638 datapath_type = ofproto_normalize_type(datapath_type);
1639 class = ofproto_class_find__(datapath_type);
1641 if (class && class->type_get_memory_usage) {
1642 class->type_get_memory_usage(datapath_type, usage);
1647 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1650 connmgr_get_controller_info(ofproto->connmgr, info);
1654 ofproto_free_ofproto_controller_info(struct shash *info)
1656 connmgr_free_controller_info(info);
1659 /* Makes a deep copy of 'old' into 'port'. */
1661 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1663 port->name = xstrdup(old->name);
1664 port->type = xstrdup(old->type);
1665 port->ofp_port = old->ofp_port;
1668 /* Frees memory allocated to members of 'ofproto_port'.
1670 * Do not call this function on an ofproto_port obtained from
1671 * ofproto_port_dump_next(): that function retains ownership of the data in the
1674 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1676 free(ofproto_port->name);
1677 free(ofproto_port->type);
1680 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1682 * This function provides no status indication. An error status for the entire
1683 * dump operation is provided when it is completed by calling
1684 * ofproto_port_dump_done().
1687 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1688 const struct ofproto *ofproto)
1690 dump->ofproto = ofproto;
1691 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1695 /* Attempts to retrieve another port from 'dump', which must have been created
1696 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1697 * 'port' and returns true. On failure, returns false.
1699 * Failure might indicate an actual error or merely that the last port has been
1700 * dumped. An error status for the entire dump operation is provided when it
1701 * is completed by calling ofproto_port_dump_done().
1703 * The ofproto owns the data stored in 'port'. It will remain valid until at
1704 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1705 * ofproto_port_dump_done(). */
1707 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1708 struct ofproto_port *port)
1710 const struct ofproto *ofproto = dump->ofproto;
1716 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1719 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1725 /* Completes port table dump operation 'dump', which must have been created
1726 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1727 * error-free, otherwise a positive errno value describing the problem. */
1729 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1731 const struct ofproto *ofproto = dump->ofproto;
1733 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1736 return dump->error == EOF ? 0 : dump->error;
1739 /* Returns the type to pass to netdev_open() when a datapath of type
1740 * 'datapath_type' has a port of type 'port_type', for a few special
1741 * cases when a netdev type differs from a port type. For example, when
1742 * using the userspace datapath, a port of type "internal" needs to be
1745 * Returns either 'type' itself or a string literal, which must not be
1748 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1750 const struct ofproto_class *class;
1752 datapath_type = ofproto_normalize_type(datapath_type);
1753 class = ofproto_class_find__(datapath_type);
1758 return (class->port_open_type
1759 ? class->port_open_type(datapath_type, port_type)
1763 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1764 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1765 * the port's OpenFlow port number.
1767 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1768 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1769 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1770 * 'ofp_portp' is non-null). */
1772 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1773 ofp_port_t *ofp_portp)
1775 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1778 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1780 const char *netdev_name = netdev_get_name(netdev);
1782 simap_put(&ofproto->ofp_requests, netdev_name,
1783 ofp_to_u16(ofp_port));
1784 update_port(ofproto, netdev_name);
1787 *ofp_portp = OFPP_NONE;
1789 struct ofproto_port ofproto_port;
1791 error = ofproto_port_query_by_name(ofproto,
1792 netdev_get_name(netdev),
1795 *ofp_portp = ofproto_port.ofp_port;
1796 ofproto_port_destroy(&ofproto_port);
1803 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1804 * initializes '*port' appropriately; on failure, returns a positive errno
1807 * The caller owns the data in 'ofproto_port' and must free it with
1808 * ofproto_port_destroy() when it is no longer needed. */
1810 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1811 struct ofproto_port *port)
1815 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1817 memset(port, 0, sizeof *port);
1822 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1823 * Returns 0 if successful, otherwise a positive errno. */
1825 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1827 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1828 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1829 struct simap_node *ofp_request_node;
1832 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1833 if (ofp_request_node) {
1834 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1837 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1838 if (!error && ofport) {
1839 /* 'name' is the netdev's name and update_port() is going to close the
1840 * netdev. Just in case update_port() refers to 'name' after it
1841 * destroys 'ofport', make a copy of it around the update_port()
1843 char *devname = xstrdup(name);
1844 update_port(ofproto, devname);
1851 flow_mod_init(struct ofputil_flow_mod *fm,
1852 const struct match *match, unsigned int priority,
1853 const struct ofpact *ofpacts, size_t ofpacts_len,
1854 enum ofp_flow_mod_command command)
1856 memset(fm, 0, sizeof *fm);
1858 fm->priority = priority;
1861 fm->modify_cookie = false;
1863 fm->command = command;
1864 fm->idle_timeout = 0;
1865 fm->hard_timeout = 0;
1866 fm->buffer_id = UINT32_MAX;
1867 fm->out_port = OFPP_ANY;
1868 fm->out_group = OFPG_ANY;
1870 fm->ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1871 fm->ofpacts_len = ofpacts_len;
1875 simple_flow_mod(struct ofproto *ofproto,
1876 const struct match *match, unsigned int priority,
1877 const struct ofpact *ofpacts, size_t ofpacts_len,
1878 enum ofp_flow_mod_command command)
1880 struct ofputil_flow_mod fm;
1882 flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command);
1884 return handle_flow_mod__(ofproto, NULL, &fm, NULL);
1887 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1888 * performs the 'n_actions' actions in 'actions'. The new flow will not
1891 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1892 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1893 * controllers; otherwise, it will be hidden.
1895 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1897 * This is a helper function for in-band control and fail-open. */
1899 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1900 unsigned int priority,
1901 const struct ofpact *ofpacts, size_t ofpacts_len)
1902 OVS_EXCLUDED(ofproto_mutex)
1904 const struct rule *rule;
1907 /* First do a cheap check whether the rule we're looking for already exists
1908 * with the actions that we want. If it does, then we're done. */
1909 fat_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1910 rule = rule_from_cls_rule(classifier_find_match_exactly(
1911 &ofproto->tables[0].cls, match, priority));
1913 ovs_mutex_lock(&rule->mutex);
1914 must_add = !ofpacts_equal(rule->actions->ofpacts,
1915 rule->actions->ofpacts_len,
1916 ofpacts, ofpacts_len);
1917 ovs_mutex_unlock(&rule->mutex);
1921 fat_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1923 /* If there's no such rule or the rule doesn't have the actions we want,
1924 * fall back to a executing a full flow mod. We can't optimize this at
1925 * all because we didn't take enough locks above to ensure that the flow
1926 * table didn't already change beneath us. */
1928 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1929 OFPFC_MODIFY_STRICT);
1933 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1934 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1935 * operation cannot be initiated now but may be retried later.
1937 * This is a helper function for in-band control and fail-open and the "learn"
1940 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1941 OVS_EXCLUDED(ofproto_mutex)
1943 /* Optimize for the most common case of a repeated learn action.
1944 * If an identical flow already exists we only need to update its
1945 * 'modified' time. */
1946 if (fm->command == OFPFC_MODIFY_STRICT && fm->table_id != OFPTT_ALL
1947 && !(fm->flags & OFPUTIL_FF_RESET_COUNTS)) {
1948 struct oftable *table = &ofproto->tables[fm->table_id];
1949 struct cls_rule match_rule;
1953 cls_rule_init(&match_rule, &fm->match, fm->priority);
1954 fat_rwlock_rdlock(&table->cls.rwlock);
1955 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls,
1958 /* Reading many of the rule fields and writing on 'modified'
1959 * requires the rule->mutex. Also, rule->actions may change
1960 * if rule->mutex is not held. */
1961 ovs_mutex_lock(&rule->mutex);
1962 if (rule->idle_timeout == fm->idle_timeout
1963 && rule->hard_timeout == fm->hard_timeout
1964 && rule->flags == (fm->flags & OFPUTIL_FF_STATE)
1965 && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie))
1966 && ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
1967 rule->actions->ofpacts,
1968 rule->actions->ofpacts_len)) {
1969 /* Rule already exists and need not change, only update the
1970 modified timestamp. */
1971 rule->modified = time_msec();
1974 ovs_mutex_unlock(&rule->mutex);
1976 fat_rwlock_unlock(&table->cls.rwlock);
1983 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1986 /* Searches for a rule with matching criteria exactly equal to 'target' in
1987 * ofproto's table 0 and, if it finds one, deletes it.
1989 * This is a helper function for in-band control and fail-open. */
1991 ofproto_delete_flow(struct ofproto *ofproto,
1992 const struct match *target, unsigned int priority)
1993 OVS_EXCLUDED(ofproto_mutex)
1995 struct classifier *cls = &ofproto->tables[0].cls;
1998 /* First do a cheap check whether the rule we're looking for has already
1999 * been deleted. If so, then we're done. */
2000 fat_rwlock_rdlock(&cls->rwlock);
2001 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
2003 fat_rwlock_unlock(&cls->rwlock);
2008 /* Fall back to a executing a full flow mod. We can't optimize this at all
2009 * because we didn't take enough locks above to ensure that the flow table
2010 * didn't already change beneath us. */
2011 return simple_flow_mod(ofproto, target, priority, NULL, 0,
2012 OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE;
2015 /* Starts the process of deleting all of the flows from all of ofproto's flow
2016 * tables and then reintroducing the flows required by in-band control and
2017 * fail-open. The process will complete in a later call to ofproto_run(). */
2019 ofproto_flush_flows(struct ofproto *ofproto)
2021 COVERAGE_INC(ofproto_flush);
2022 ofproto->state = S_FLUSH;
2026 reinit_ports(struct ofproto *p)
2028 struct ofproto_port_dump dump;
2029 struct sset devnames;
2030 struct ofport *ofport;
2031 struct ofproto_port ofproto_port;
2032 const char *devname;
2034 COVERAGE_INC(ofproto_reinit_ports);
2036 sset_init(&devnames);
2037 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2038 sset_add(&devnames, netdev_get_name(ofport->netdev));
2040 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2041 sset_add(&devnames, ofproto_port.name);
2044 SSET_FOR_EACH (devname, &devnames) {
2045 update_port(p, devname);
2047 sset_destroy(&devnames);
2051 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
2055 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
2056 port_idx = port_idx ? port_idx : UINT16_MAX;
2058 if (port_idx >= ofproto->max_ports
2059 || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) {
2060 uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no;
2061 long long int last_used_at, lru = LLONG_MAX;
2063 /* Search for a free OpenFlow port number. We try not to
2064 * immediately reuse them to prevent problems due to old
2067 * We limit the automatically assigned port numbers to the lower half
2068 * of the port range, to reserve the upper half for assignment by
2071 if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) {
2072 ofproto->alloc_port_no = 1;
2074 last_used_at = ofport_get_usage(ofproto,
2075 u16_to_ofp(ofproto->alloc_port_no));
2076 if (!last_used_at) {
2077 port_idx = ofproto->alloc_port_no;
2079 } else if ( last_used_at < time_msec() - 60*60*1000) {
2080 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2081 * more than an hour ago, consider it usable. */
2082 ofport_remove_usage(ofproto,
2083 u16_to_ofp(ofproto->alloc_port_no));
2084 port_idx = ofproto->alloc_port_no;
2086 } else if (last_used_at < lru) {
2088 lru_ofport = ofproto->alloc_port_no;
2091 if (ofproto->alloc_port_no == end_port_no) {
2093 port_idx = lru_ofport;
2100 ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX);
2101 return u16_to_ofp(port_idx);
2105 dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port)
2107 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
2108 ofport_set_usage(ofproto, ofp_port, time_msec());
2112 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2113 * pointer if the netdev cannot be opened. On success, also fills in
2115 static struct netdev *
2116 ofport_open(struct ofproto *ofproto,
2117 struct ofproto_port *ofproto_port,
2118 struct ofputil_phy_port *pp)
2120 enum netdev_flags flags;
2121 struct netdev *netdev;
2124 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
2126 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
2127 "cannot be opened (%s)",
2129 ofproto_port->name, ofproto_port->ofp_port,
2130 ofproto_port->name, ovs_strerror(error));
2134 if (ofproto_port->ofp_port == OFPP_NONE) {
2135 if (!strcmp(ofproto->name, ofproto_port->name)) {
2136 ofproto_port->ofp_port = OFPP_LOCAL;
2138 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
2139 ofproto_port->name);
2142 pp->port_no = ofproto_port->ofp_port;
2143 netdev_get_etheraddr(netdev, pp->hw_addr);
2144 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
2145 netdev_get_flags(netdev, &flags);
2146 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
2147 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
2148 netdev_get_features(netdev, &pp->curr, &pp->advertised,
2149 &pp->supported, &pp->peer);
2150 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2151 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2156 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2157 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
2160 ofport_equal(const struct ofputil_phy_port *a,
2161 const struct ofputil_phy_port *b)
2163 return (eth_addr_equals(a->hw_addr, b->hw_addr)
2164 && a->state == b->state
2165 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
2166 && a->curr == b->curr
2167 && a->advertised == b->advertised
2168 && a->supported == b->supported
2169 && a->peer == b->peer
2170 && a->curr_speed == b->curr_speed
2171 && a->max_speed == b->max_speed);
2174 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2175 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2176 * one with the same name or port number). */
2178 ofport_install(struct ofproto *p,
2179 struct netdev *netdev, const struct ofputil_phy_port *pp)
2181 const char *netdev_name = netdev_get_name(netdev);
2182 struct ofport *ofport;
2185 /* Create ofport. */
2186 ofport = p->ofproto_class->port_alloc();
2191 ofport->ofproto = p;
2192 ofport->netdev = netdev;
2194 ofport->ofp_port = pp->port_no;
2195 ofport->created = time_msec();
2197 /* Add port to 'p'. */
2198 hmap_insert(&p->ports, &ofport->hmap_node,
2199 hash_ofp_port(ofport->ofp_port));
2200 shash_add(&p->port_by_name, netdev_name, ofport);
2202 update_mtu(p, ofport);
2204 /* Let the ofproto_class initialize its private data. */
2205 error = p->ofproto_class->port_construct(ofport);
2209 connmgr_send_port_status(p->connmgr, NULL, pp, OFPPR_ADD);
2213 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
2214 p->name, netdev_name, ovs_strerror(error));
2216 ofport_destroy__(ofport);
2218 netdev_close(netdev);
2222 /* Removes 'ofport' from 'p' and destroys it. */
2224 ofport_remove(struct ofport *ofport)
2226 connmgr_send_port_status(ofport->ofproto->connmgr, NULL, &ofport->pp,
2228 ofport_destroy(ofport);
2231 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2234 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2236 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2238 ofport_remove(port);
2242 /* Updates 'port' with new 'pp' description.
2244 * Does not handle a name or port number change. The caller must implement
2245 * such a change as a delete followed by an add. */
2247 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
2249 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2250 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2251 | (pp->config & OFPUTIL_PC_PORT_DOWN));
2252 port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN)
2253 | (pp->state & OFPUTIL_PS_LINK_DOWN));
2254 port->pp.curr = pp->curr;
2255 port->pp.advertised = pp->advertised;
2256 port->pp.supported = pp->supported;
2257 port->pp.peer = pp->peer;
2258 port->pp.curr_speed = pp->curr_speed;
2259 port->pp.max_speed = pp->max_speed;
2261 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2262 &port->pp, OFPPR_MODIFY);
2265 /* Update OpenFlow 'state' in 'port' and notify controller. */
2267 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2269 if (port->pp.state != state) {
2270 port->pp.state = state;
2271 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2272 &port->pp, OFPPR_MODIFY);
2277 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2279 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2281 if (port->ofproto->ofproto_class->set_realdev) {
2282 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2284 if (port->ofproto->ofproto_class->set_stp_port) {
2285 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2287 if (port->ofproto->ofproto_class->set_cfm) {
2288 port->ofproto->ofproto_class->set_cfm(port, NULL);
2290 if (port->ofproto->ofproto_class->bundle_remove) {
2291 port->ofproto->ofproto_class->bundle_remove(port);
2297 ofport_destroy__(struct ofport *port)
2299 struct ofproto *ofproto = port->ofproto;
2300 const char *name = netdev_get_name(port->netdev);
2302 hmap_remove(&ofproto->ports, &port->hmap_node);
2303 shash_delete(&ofproto->port_by_name,
2304 shash_find(&ofproto->port_by_name, name));
2306 netdev_close(port->netdev);
2307 ofproto->ofproto_class->port_dealloc(port);
2311 ofport_destroy(struct ofport *port)
2314 dealloc_ofp_port(port->ofproto, port->ofp_port);
2315 port->ofproto->ofproto_class->port_destruct(port);
2316 ofport_destroy__(port);
2321 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2323 struct ofport *port;
2325 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2327 if (port->ofp_port == ofp_port) {
2334 static long long int
2335 ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port)
2337 struct ofport_usage *usage;
2339 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2340 &ofproto->ofport_usage) {
2341 if (usage->ofp_port == ofp_port) {
2342 return usage->last_used;
2349 ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port,
2350 long long int last_used)
2352 struct ofport_usage *usage;
2353 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2354 &ofproto->ofport_usage) {
2355 if (usage->ofp_port == ofp_port) {
2356 usage->last_used = last_used;
2360 ovs_assert(last_used == LLONG_MAX);
2362 usage = xmalloc(sizeof *usage);
2363 usage->ofp_port = ofp_port;
2364 usage->last_used = last_used;
2365 hmap_insert(&ofproto->ofport_usage, &usage->hmap_node,
2366 hash_ofp_port(ofp_port));
2370 ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port)
2372 struct ofport_usage *usage;
2373 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2374 &ofproto->ofport_usage) {
2375 if (usage->ofp_port == ofp_port) {
2376 hmap_remove(&ofproto->ofport_usage, &usage->hmap_node);
2384 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2386 struct ofproto *ofproto = port->ofproto;
2389 if (ofproto->ofproto_class->port_get_stats) {
2390 error = ofproto->ofproto_class->port_get_stats(port, stats);
2399 update_port(struct ofproto *ofproto, const char *name)
2401 struct ofproto_port ofproto_port;
2402 struct ofputil_phy_port pp;
2403 struct netdev *netdev;
2404 struct ofport *port;
2406 COVERAGE_INC(ofproto_update_port);
2408 /* Fetch 'name''s location and properties from the datapath. */
2409 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2410 ? ofport_open(ofproto, &ofproto_port, &pp)
2414 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2415 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2416 struct netdev *old_netdev = port->netdev;
2418 /* 'name' hasn't changed location. Any properties changed? */
2419 if (!ofport_equal(&port->pp, &pp)) {
2420 ofport_modified(port, &pp);
2423 update_mtu(ofproto, port);
2425 /* Install the newly opened netdev in case it has changed.
2426 * Don't close the old netdev yet in case port_modified has to
2427 * remove a retained reference to it.*/
2428 port->netdev = netdev;
2430 if (port->ofproto->ofproto_class->port_modified) {
2431 port->ofproto->ofproto_class->port_modified(port);
2434 netdev_close(old_netdev);
2436 /* If 'port' is nonnull then its name differs from 'name' and thus
2437 * we should delete it. If we think there's a port named 'name'
2438 * then its port number must be wrong now so delete it too. */
2440 ofport_remove(port);
2442 ofport_remove_with_name(ofproto, name);
2443 ofport_install(ofproto, netdev, &pp);
2446 /* Any port named 'name' is gone now. */
2447 ofport_remove_with_name(ofproto, name);
2449 ofproto_port_destroy(&ofproto_port);
2453 init_ports(struct ofproto *p)
2455 struct ofproto_port_dump dump;
2456 struct ofproto_port ofproto_port;
2457 struct shash_node *node, *next;
2459 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2460 const char *name = ofproto_port.name;
2462 if (shash_find(&p->port_by_name, name)) {
2463 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2466 struct ofputil_phy_port pp;
2467 struct netdev *netdev;
2469 /* Check if an OpenFlow port number had been requested. */
2470 node = shash_find(&init_ofp_ports, name);
2472 const struct iface_hint *iface_hint = node->data;
2473 simap_put(&p->ofp_requests, name,
2474 ofp_to_u16(iface_hint->ofp_port));
2477 netdev = ofport_open(p, &ofproto_port, &pp);
2479 ofport_install(p, netdev, &pp);
2480 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2481 p->alloc_port_no = MAX(p->alloc_port_no,
2482 ofp_to_u16(ofproto_port.ofp_port));
2488 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2489 struct iface_hint *iface_hint = node->data;
2491 if (!strcmp(iface_hint->br_name, p->name)) {
2492 free(iface_hint->br_name);
2493 free(iface_hint->br_type);
2495 shash_delete(&init_ofp_ports, node);
2502 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2503 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2505 find_min_mtu(struct ofproto *p)
2507 struct ofport *ofport;
2510 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2511 struct netdev *netdev = ofport->netdev;
2514 /* Skip any internal ports, since that's what we're trying to
2516 if (!strcmp(netdev_get_type(netdev), "internal")) {
2520 if (netdev_get_mtu(netdev, &dev_mtu)) {
2523 if (!mtu || dev_mtu < mtu) {
2528 return mtu ? mtu: ETH_PAYLOAD_MAX;
2531 /* Update MTU of all datapath devices on 'p' to the minimum of the
2532 * non-datapath ports in event of 'port' added or changed. */
2534 update_mtu(struct ofproto *p, struct ofport *port)
2536 struct ofport *ofport;
2537 struct netdev *netdev = port->netdev;
2538 int dev_mtu, old_min;
2540 if (netdev_get_mtu(netdev, &dev_mtu)) {
2544 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2545 if (dev_mtu > p->min_mtu) {
2546 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2547 dev_mtu = p->min_mtu;
2550 port->mtu = dev_mtu;
2554 /* For non-internal port find new min mtu. */
2555 old_min = p->min_mtu;
2556 port->mtu = dev_mtu;
2557 p->min_mtu = find_min_mtu(p);
2558 if (p->min_mtu == old_min) {
2562 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2563 struct netdev *netdev = ofport->netdev;
2565 if (!strcmp(netdev_get_type(netdev), "internal")) {
2566 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2567 ofport->mtu = p->min_mtu;
2574 ofproto_rule_ref(struct rule *rule)
2577 ovs_refcount_ref(&rule->ref_count);
2582 ofproto_rule_unref(struct rule *rule)
2584 if (rule && ovs_refcount_unref(&rule->ref_count) == 1) {
2585 rule->ofproto->ofproto_class->rule_destruct(rule);
2586 ofproto_rule_destroy__(rule);
2590 struct rule_actions *
2591 rule_get_actions(const struct rule *rule)
2592 OVS_EXCLUDED(rule->mutex)
2594 struct rule_actions *actions;
2596 ovs_mutex_lock(&rule->mutex);
2597 actions = rule_get_actions__(rule);
2598 ovs_mutex_unlock(&rule->mutex);
2603 struct rule_actions *
2604 rule_get_actions__(const struct rule *rule)
2605 OVS_REQUIRES(rule->mutex)
2607 rule_actions_ref(rule->actions);
2608 return rule->actions;
2612 ofproto_rule_destroy__(struct rule *rule)
2613 OVS_NO_THREAD_SAFETY_ANALYSIS
2615 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2616 rule_actions_unref(rule->actions);
2617 ovs_mutex_destroy(&rule->mutex);
2618 ovs_refcount_destroy(&rule->ref_count);
2619 rule->ofproto->ofproto_class->rule_dealloc(rule);
2622 static uint32_t get_provider_meter_id(const struct ofproto *,
2623 uint32_t of_meter_id);
2625 /* Creates and returns a new 'struct rule_actions', with a ref_count of 1,
2626 * whose actions are a copy of from the 'ofpacts_len' bytes of 'ofpacts'. */
2627 struct rule_actions *
2628 rule_actions_create(const struct ofproto *ofproto,
2629 const struct ofpact *ofpacts, size_t ofpacts_len)
2631 struct rule_actions *actions;
2633 actions = xmalloc(sizeof *actions);
2634 ovs_refcount_init(&actions->ref_count);
2635 actions->ofpacts = xmemdup(ofpacts, ofpacts_len);
2636 actions->ofpacts_len = ofpacts_len;
2637 actions->provider_meter_id
2638 = get_provider_meter_id(ofproto,
2639 ofpacts_get_meter(ofpacts, ofpacts_len));
2644 /* Increments 'actions''s ref_count. */
2646 rule_actions_ref(struct rule_actions *actions)
2649 ovs_refcount_ref(&actions->ref_count);
2653 /* Decrements 'actions''s ref_count and frees 'actions' if the ref_count
2656 rule_actions_unref(struct rule_actions *actions)
2658 if (actions && ovs_refcount_unref(&actions->ref_count) == 1) {
2659 ovs_refcount_destroy(&actions->ref_count);
2660 free(actions->ofpacts);
2665 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2666 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2668 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2669 OVS_REQUIRES(ofproto_mutex)
2671 return (port == OFPP_ANY
2672 || ofpacts_output_to_port(rule->actions->ofpacts,
2673 rule->actions->ofpacts_len, port));
2676 /* Returns true if 'rule' has group and equals group_id. */
2678 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2679 OVS_REQUIRES(ofproto_mutex)
2681 return (group_id == OFPG11_ANY
2682 || ofpacts_output_to_group(rule->actions->ofpacts,
2683 rule->actions->ofpacts_len, group_id));
2686 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2687 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2689 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2690 OVS_REQUIRES(ofproto_mutex)
2692 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2697 case OFOPERATION_ADD:
2698 case OFOPERATION_DELETE:
2701 case OFOPERATION_MODIFY:
2702 case OFOPERATION_REPLACE:
2703 return ofpacts_output_to_port(op->actions->ofpacts,
2704 op->actions->ofpacts_len, out_port);
2711 rule_execute_destroy(struct rule_execute *e)
2713 ofproto_rule_unref(e->rule);
2714 list_remove(&e->list_node);
2718 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2719 * by passing them to the ofproto provider. */
2721 run_rule_executes(struct ofproto *ofproto)
2722 OVS_EXCLUDED(ofproto_mutex)
2724 struct rule_execute *e, *next;
2725 struct list executes;
2727 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2728 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2731 flow_extract(e->packet, NULL, &flow);
2732 flow.in_port.ofp_port = e->in_port;
2733 ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet);
2735 rule_execute_destroy(e);
2739 /* Destroys and discards all "rule_execute" operations queued up in
2740 * ofproto->rule_executes. */
2742 destroy_rule_executes(struct ofproto *ofproto)
2744 struct rule_execute *e, *next;
2745 struct list executes;
2747 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2748 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2749 ofpbuf_delete(e->packet);
2750 rule_execute_destroy(e);
2754 /* Returns true if 'rule' should be hidden from the controller.
2756 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2757 * (e.g. by in-band control) and are intentionally hidden from the
2760 ofproto_rule_is_hidden(const struct rule *rule)
2762 return rule->cr.priority > UINT16_MAX;
2765 static enum oftable_flags
2766 rule_get_flags(const struct rule *rule)
2768 return rule->ofproto->tables[rule->table_id].flags;
2772 rule_is_modifiable(const struct rule *rule)
2774 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2778 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2780 ofconn_send_reply(ofconn, make_echo_reply(oh));
2785 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2787 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2788 struct ofputil_switch_features features;
2789 struct ofport *port;
2795 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2797 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2799 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2800 * if present, are always at the end.) */
2801 n_tables = ofproto->n_tables;
2802 for (i = 0; i < ofproto->n_tables; i++) {
2803 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2809 features.datapath_id = ofproto->datapath_id;
2810 features.n_buffers = pktbuf_capacity();
2811 features.n_tables = n_tables;
2812 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2813 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2815 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2817 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2818 features.auxiliary_id = 0;
2819 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2821 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2822 ofputil_put_switch_features_port(&port->pp, b);
2825 ofconn_send_reply(ofconn, b);
2830 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2832 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2833 struct ofp_switch_config *osc;
2834 enum ofp_config_flags flags;
2838 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2839 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2840 flags = ofproto->frag_handling;
2841 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2842 if (oh->version < OFP13_VERSION
2843 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2844 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2846 osc->flags = htons(flags);
2847 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2848 ofconn_send_reply(ofconn, buf);
2854 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2856 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2857 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2858 uint16_t flags = ntohs(osc->flags);
2860 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2861 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2862 enum ofp_config_flags cur = ofproto->frag_handling;
2863 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2865 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2867 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2868 ofproto->frag_handling = next;
2870 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2872 ofputil_frag_handling_to_string(next));
2876 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2877 ofconn_set_invalid_ttl_to_controller(ofconn,
2878 (oh->version < OFP13_VERSION
2879 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2881 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2886 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2887 * error message code for the caller to propagate upward. Otherwise, returns
2890 * The log message mentions 'msg_type'. */
2892 reject_slave_controller(struct ofconn *ofconn)
2894 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2895 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2896 return OFPERR_OFPBRC_EPERM;
2902 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
2905 * - If they use a meter, then 'ofproto' has that meter configured.
2907 * - If they use any groups, then 'ofproto' has that group configured.
2909 * Returns 0 if successful, otherwise an OpenFlow error. */
2911 ofproto_check_ofpacts(struct ofproto *ofproto,
2912 const struct ofpact ofpacts[], size_t ofpacts_len)
2914 const struct ofpact *a;
2917 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2918 if (mid && get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2919 return OFPERR_OFPMMFC_INVALID_METER;
2922 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2923 if (a->type == OFPACT_GROUP
2924 && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) {
2925 return OFPERR_OFPBAC_BAD_OUT_GROUP;
2933 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2935 struct ofproto *p = ofconn_get_ofproto(ofconn);
2936 struct ofputil_packet_out po;
2937 struct ofpbuf *payload;
2938 uint64_t ofpacts_stub[1024 / 8];
2939 struct ofpbuf ofpacts;
2943 COVERAGE_INC(ofproto_packet_out);
2945 error = reject_slave_controller(ofconn);
2950 /* Decode message. */
2951 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2952 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2954 goto exit_free_ofpacts;
2956 if (ofp_to_u16(po.in_port) >= p->max_ports
2957 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2958 error = OFPERR_OFPBRC_BAD_PORT;
2959 goto exit_free_ofpacts;
2963 if (po.buffer_id != UINT32_MAX) {
2964 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2965 if (error || !payload) {
2966 goto exit_free_ofpacts;
2969 /* Ensure that the L3 header is 32-bit aligned. */
2970 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2973 /* Verify actions against packet, then send packet if successful. */
2974 flow_extract(payload, NULL, &flow);
2975 flow.in_port.ofp_port = po.in_port;
2976 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len);
2978 error = p->ofproto_class->packet_out(p, payload, &flow,
2979 po.ofpacts, po.ofpacts_len);
2981 ofpbuf_delete(payload);
2984 ofpbuf_uninit(&ofpacts);
2990 update_port_config(struct ofconn *ofconn, struct ofport *port,
2991 enum ofputil_port_config config,
2992 enum ofputil_port_config mask)
2994 enum ofputil_port_config toggle = (config ^ port->pp.config) & mask;
2996 if (toggle & OFPUTIL_PC_PORT_DOWN
2997 && (config & OFPUTIL_PC_PORT_DOWN
2998 ? netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL)
2999 : netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL))) {
3000 /* We tried to bring the port up or down, but it failed, so don't
3001 * update the "down" bit. */
3002 toggle &= ~OFPUTIL_PC_PORT_DOWN;
3006 enum ofputil_port_config old_config = port->pp.config;
3007 port->pp.config ^= toggle;
3008 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
3009 connmgr_send_port_status(port->ofproto->connmgr, ofconn, &port->pp,
3015 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3017 struct ofproto *p = ofconn_get_ofproto(ofconn);
3018 struct ofputil_port_mod pm;
3019 struct ofport *port;
3022 error = reject_slave_controller(ofconn);
3027 error = ofputil_decode_port_mod(oh, &pm);
3032 port = ofproto_get_port(p, pm.port_no);
3034 return OFPERR_OFPPMFC_BAD_PORT;
3035 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
3036 return OFPERR_OFPPMFC_BAD_HW_ADDR;
3038 update_port_config(ofconn, port, pm.config, pm.mask);
3040 netdev_set_advertisements(port->netdev, pm.advertise);
3047 handle_desc_stats_request(struct ofconn *ofconn,
3048 const struct ofp_header *request)
3050 static const char *default_mfr_desc = "Nicira, Inc.";
3051 static const char *default_hw_desc = "Open vSwitch";
3052 static const char *default_sw_desc = VERSION;
3053 static const char *default_serial_desc = "None";
3054 static const char *default_dp_desc = "None";
3056 struct ofproto *p = ofconn_get_ofproto(ofconn);
3057 struct ofp_desc_stats *ods;
3060 msg = ofpraw_alloc_stats_reply(request, 0);
3061 ods = ofpbuf_put_zeros(msg, sizeof *ods);
3062 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
3063 sizeof ods->mfr_desc);
3064 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
3065 sizeof ods->hw_desc);
3066 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
3067 sizeof ods->sw_desc);
3068 ovs_strlcpy(ods->serial_num,
3069 p->serial_desc ? p->serial_desc : default_serial_desc,
3070 sizeof ods->serial_num);
3071 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
3072 sizeof ods->dp_desc);
3073 ofconn_send_reply(ofconn, msg);
3079 handle_table_stats_request(struct ofconn *ofconn,
3080 const struct ofp_header *request)
3082 struct ofproto *p = ofconn_get_ofproto(ofconn);
3083 struct ofp12_table_stats *ots;
3088 /* Set up default values.
3090 * ofp12_table_stats is used as a generic structure as
3091 * it is able to hold all the fields for ofp10_table_stats
3092 * and ofp11_table_stats (and of course itself).
3094 ots = xcalloc(p->n_tables, sizeof *ots);
3095 for (i = 0; i < p->n_tables; i++) {
3096 ots[i].table_id = i;
3097 sprintf(ots[i].name, "table%"PRIuSIZE, i);
3098 ots[i].match = htonll(OFPXMT13_MASK);
3099 ots[i].wildcards = htonll(OFPXMT13_MASK);
3100 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
3101 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
3102 ots[i].write_setfields = htonll(OFPXMT13_MASK);
3103 ots[i].apply_setfields = htonll(OFPXMT13_MASK);
3104 ots[i].metadata_match = OVS_BE64_MAX;
3105 ots[i].metadata_write = OVS_BE64_MAX;
3106 ots[i].instructions = htonl(OFPIT11_ALL);
3107 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
3108 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
3109 fat_rwlock_rdlock(&p->tables[i].cls.rwlock);
3110 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
3111 fat_rwlock_unlock(&p->tables[i].cls.rwlock);
3114 p->ofproto_class->get_tables(p, ots);
3116 /* Post-process the tables, dropping hidden tables. */
3117 n_tables = p->n_tables;
3118 for (i = 0; i < p->n_tables; i++) {
3119 const struct oftable *table = &p->tables[i];
3121 if (table->flags & OFTABLE_HIDDEN) {
3127 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
3130 if (table->max_flows < ntohl(ots[i].max_entries)) {
3131 ots[i].max_entries = htonl(table->max_flows);
3135 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
3136 ofconn_send_reply(ofconn, msg);
3144 append_port_stat(struct ofport *port, struct list *replies)
3146 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
3148 calc_duration(port->created, time_msec(),
3149 &ops.duration_sec, &ops.duration_nsec);
3151 /* Intentionally ignore return value, since errors will set
3152 * 'stats' to all-1s, which is correct for OpenFlow, and
3153 * netdev_get_stats() will log errors. */
3154 ofproto_port_get_stats(port, &ops.stats);
3156 ofputil_append_port_stat(replies, &ops);
3160 handle_port_stats_request(struct ofconn *ofconn,
3161 const struct ofp_header *request)
3163 struct ofproto *p = ofconn_get_ofproto(ofconn);
3164 struct ofport *port;
3165 struct list replies;
3169 error = ofputil_decode_port_stats_request(request, &port_no);
3174 ofpmp_init(&replies, request);
3175 if (port_no != OFPP_ANY) {
3176 port = ofproto_get_port(p, port_no);
3178 append_port_stat(port, &replies);
3181 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3182 append_port_stat(port, &replies);
3186 ofconn_send_replies(ofconn, &replies);
3191 handle_port_desc_stats_request(struct ofconn *ofconn,
3192 const struct ofp_header *request)
3194 struct ofproto *p = ofconn_get_ofproto(ofconn);
3195 enum ofp_version version;
3196 struct ofport *port;
3197 struct list replies;
3199 ofpmp_init(&replies, request);
3201 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
3202 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3203 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
3206 ofconn_send_replies(ofconn, &replies);
3211 hash_cookie(ovs_be64 cookie)
3213 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
3214 (OVS_FORCE uint64_t)cookie);
3218 cookies_insert(struct ofproto *ofproto, struct rule *rule)
3219 OVS_REQUIRES(ofproto_mutex)
3221 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3222 hash_cookie(rule->flow_cookie));
3226 cookies_remove(struct ofproto *ofproto, struct rule *rule)
3227 OVS_REQUIRES(ofproto_mutex)
3229 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3233 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
3234 ovs_be64 new_cookie)
3235 OVS_REQUIRES(ofproto_mutex)
3237 if (new_cookie != rule->flow_cookie) {
3238 cookies_remove(ofproto, rule);
3240 ovs_mutex_lock(&rule->mutex);
3241 rule->flow_cookie = new_cookie;
3242 ovs_mutex_unlock(&rule->mutex);
3244 cookies_insert(ofproto, rule);
3249 calc_duration(long long int start, long long int now,
3250 uint32_t *sec, uint32_t *nsec)
3252 long long int msecs = now - start;
3253 *sec = msecs / 1000;
3254 *nsec = (msecs % 1000) * (1000 * 1000);
3257 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3258 * true if 'table_id' is OK, false otherwise. */
3260 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3262 return table_id == OFPTT_ALL || table_id < ofproto->n_tables;
3265 static struct oftable *
3266 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
3268 struct oftable *table;
3270 for (table = &ofproto->tables[table_id];
3271 table < &ofproto->tables[ofproto->n_tables];
3273 if (!(table->flags & OFTABLE_HIDDEN)) {
3281 static struct oftable *
3282 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
3284 if (table_id == 0xff) {
3285 return next_visible_table(ofproto, 0);
3286 } else if (table_id < ofproto->n_tables) {
3287 return &ofproto->tables[table_id];
3293 static struct oftable *
3294 next_matching_table(const struct ofproto *ofproto,
3295 const struct oftable *table, uint8_t table_id)
3297 return (table_id == 0xff
3298 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
3302 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3304 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3305 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3307 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3308 * only once, for that table. (This can be used to access tables marked
3311 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3312 * entered at all. (Perhaps you should have validated TABLE_ID with
3313 * check_table_id().)
3315 * All parameters are evaluated multiple times.
3317 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3318 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3320 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3322 /* Initializes 'criteria' in a straightforward way based on the other
3325 * For "loose" matching, the 'priority' parameter is unimportant and may be
3328 rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
3329 const struct match *match, unsigned int priority,
3330 ovs_be64 cookie, ovs_be64 cookie_mask,
3331 ofp_port_t out_port, uint32_t out_group)
3333 criteria->table_id = table_id;
3334 cls_rule_init(&criteria->cr, match, priority);
3335 criteria->cookie = cookie;
3336 criteria->cookie_mask = cookie_mask;
3337 criteria->out_port = out_port;
3338 criteria->out_group = out_group;
3342 rule_criteria_destroy(struct rule_criteria *criteria)
3344 cls_rule_destroy(&criteria->cr);
3348 rule_collection_init(struct rule_collection *rules)
3350 rules->rules = rules->stub;
3352 rules->capacity = ARRAY_SIZE(rules->stub);
3356 rule_collection_add(struct rule_collection *rules, struct rule *rule)
3358 if (rules->n >= rules->capacity) {
3359 size_t old_size, new_size;
3361 old_size = rules->capacity * sizeof *rules->rules;
3362 rules->capacity *= 2;
3363 new_size = rules->capacity * sizeof *rules->rules;
3365 if (rules->rules == rules->stub) {
3366 rules->rules = xmalloc(new_size);
3367 memcpy(rules->rules, rules->stub, old_size);
3369 rules->rules = xrealloc(rules->rules, new_size);
3373 rules->rules[rules->n++] = rule;
3377 rule_collection_ref(struct rule_collection *rules)
3378 OVS_REQUIRES(ofproto_mutex)
3382 for (i = 0; i < rules->n; i++) {
3383 ofproto_rule_ref(rules->rules[i]);
3388 rule_collection_unref(struct rule_collection *rules)
3392 for (i = 0; i < rules->n; i++) {
3393 ofproto_rule_unref(rules->rules[i]);
3398 rule_collection_destroy(struct rule_collection *rules)
3400 if (rules->rules != rules->stub) {
3406 collect_rule(struct rule *rule, const struct rule_criteria *c,
3407 struct rule_collection *rules)
3408 OVS_REQUIRES(ofproto_mutex)
3410 /* We ordinarily want to skip hidden rules, but there has to be a way for
3411 * code internal to OVS to modify and delete them, so if the criteria
3412 * specify a priority that can only be for a hidden flow, then allow hidden
3413 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
3414 * with hidden flows because OpenFlow uses only a 16-bit field to specify
3416 if (ofproto_rule_is_hidden(rule) && c->cr.priority <= UINT16_MAX) {
3418 } else if (rule->pending) {
3419 return OFPROTO_POSTPONE;
3421 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3422 && ofproto_rule_has_out_port(rule, c->out_port)
3423 && ofproto_rule_has_out_group(rule, c->out_group)
3424 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)) {
3425 rule_collection_add(rules, rule);
3431 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3432 * on classifiers rules are done in the "loose" way required for OpenFlow
3433 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3436 * Hidden rules are always omitted.
3438 * Returns 0 on success, otherwise an OpenFlow error code. */
3440 collect_rules_loose(struct ofproto *ofproto,
3441 const struct rule_criteria *criteria,
3442 struct rule_collection *rules)
3443 OVS_REQUIRES(ofproto_mutex)
3445 struct oftable *table;
3446 enum ofperr error = 0;
3448 rule_collection_init(rules);
3450 if (!check_table_id(ofproto, criteria->table_id)) {
3451 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3455 if (criteria->cookie_mask == OVS_BE64_MAX) {
3458 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3459 hash_cookie(criteria->cookie),
3460 &ofproto->cookies) {
3461 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
3462 error = collect_rule(rule, criteria, rules);
3469 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3470 struct cls_cursor cursor;
3473 fat_rwlock_rdlock(&table->cls.rwlock);
3474 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
3475 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3476 error = collect_rule(rule, criteria, rules);
3481 fat_rwlock_unlock(&table->cls.rwlock);
3487 rule_collection_destroy(rules);
3492 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3493 * on classifiers rules are done in the "strict" way required for OpenFlow
3494 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3495 * rules on list 'rules'.
3497 * Hidden rules are always omitted.
3499 * Returns 0 on success, otherwise an OpenFlow error code. */
3501 collect_rules_strict(struct ofproto *ofproto,
3502 const struct rule_criteria *criteria,
3503 struct rule_collection *rules)
3504 OVS_REQUIRES(ofproto_mutex)
3506 struct oftable *table;
3509 rule_collection_init(rules);
3511 if (!check_table_id(ofproto, criteria->table_id)) {
3512 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3516 if (criteria->cookie_mask == OVS_BE64_MAX) {
3519 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3520 hash_cookie(criteria->cookie),
3521 &ofproto->cookies) {
3522 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
3523 error = collect_rule(rule, criteria, rules);
3530 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3533 fat_rwlock_rdlock(&table->cls.rwlock);
3534 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3535 &table->cls, &criteria->cr));
3536 fat_rwlock_unlock(&table->cls.rwlock);
3538 error = collect_rule(rule, criteria, rules);
3548 rule_collection_destroy(rules);
3553 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3554 * forced into the range of a uint16_t. */
3556 age_secs(long long int age_ms)
3558 return (age_ms < 0 ? 0
3559 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3560 : (unsigned int) age_ms / 1000);
3564 handle_flow_stats_request(struct ofconn *ofconn,
3565 const struct ofp_header *request)
3566 OVS_EXCLUDED(ofproto_mutex)
3568 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3569 struct ofputil_flow_stats_request fsr;
3570 struct rule_criteria criteria;
3571 struct rule_collection rules;
3572 struct list replies;
3576 error = ofputil_decode_flow_stats_request(&fsr, request);
3581 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3582 fsr.cookie_mask, fsr.out_port, fsr.out_group);
3584 ovs_mutex_lock(&ofproto_mutex);
3585 error = collect_rules_loose(ofproto, &criteria, &rules);
3586 rule_criteria_destroy(&criteria);
3588 rule_collection_ref(&rules);
3590 ovs_mutex_unlock(&ofproto_mutex);
3596 ofpmp_init(&replies, request);
3597 for (i = 0; i < rules.n; i++) {
3598 struct rule *rule = rules.rules[i];
3599 long long int now = time_msec();
3600 struct ofputil_flow_stats fs;
3601 long long int created, used, modified;
3602 struct rule_actions *actions;
3603 enum ofputil_flow_mod_flags flags;
3605 ovs_mutex_lock(&rule->mutex);
3606 fs.cookie = rule->flow_cookie;
3607 fs.idle_timeout = rule->idle_timeout;
3608 fs.hard_timeout = rule->hard_timeout;
3609 created = rule->created;
3610 modified = rule->modified;
3611 actions = rule_get_actions__(rule);
3612 flags = rule->flags;
3613 ovs_mutex_unlock(&rule->mutex);
3615 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3616 &fs.byte_count, &used);
3618 minimatch_expand(&rule->cr.match, &fs.match);
3619 fs.table_id = rule->table_id;
3620 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
3621 fs.priority = rule->cr.priority;
3622 fs.idle_age = age_secs(now - used);
3623 fs.hard_age = age_secs(now - modified);
3624 fs.ofpacts = actions->ofpacts;
3625 fs.ofpacts_len = actions->ofpacts_len;
3628 ofputil_append_flow_stats_reply(&fs, &replies);
3630 rule_actions_unref(actions);
3633 rule_collection_unref(&rules);
3634 rule_collection_destroy(&rules);
3636 ofconn_send_replies(ofconn, &replies);
3642 flow_stats_ds(struct rule *rule, struct ds *results)
3644 uint64_t packet_count, byte_count;
3645 struct rule_actions *actions;
3646 long long int created, used;
3648 rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3649 &byte_count, &used);
3651 ovs_mutex_lock(&rule->mutex);
3652 actions = rule_get_actions__(rule);
3653 created = rule->created;
3654 ovs_mutex_unlock(&rule->mutex);
3656 if (rule->table_id != 0) {
3657 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3659 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
3660 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3661 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3662 cls_rule_format(&rule->cr, results);
3663 ds_put_char(results, ',');
3665 ds_put_cstr(results, "actions=");
3666 ofpacts_format(actions->ofpacts, actions->ofpacts_len, results);
3668 ds_put_cstr(results, "\n");
3670 rule_actions_unref(actions);
3673 /* Adds a pretty-printed description of all flows to 'results', including
3674 * hidden flows (e.g., set up by in-band control). */
3676 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3678 struct oftable *table;
3680 OFPROTO_FOR_EACH_TABLE (table, p) {
3681 struct cls_cursor cursor;
3684 fat_rwlock_rdlock(&table->cls.rwlock);
3685 cls_cursor_init(&cursor, &table->cls, NULL);
3686 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3687 flow_stats_ds(rule, results);
3689 fat_rwlock_unlock(&table->cls.rwlock);
3693 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3694 * '*engine_type' and '*engine_id', respectively. */
3696 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3697 uint8_t *engine_type, uint8_t *engine_id)
3699 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3702 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3703 * true if the port's CFM status was successfully stored into '*status'.
3704 * Returns false if the port did not have CFM configured, in which case
3705 * '*status' is indeterminate.
3707 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3709 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3710 struct ofproto_cfm_status *status)
3712 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3714 && ofproto->ofproto_class->get_cfm_status
3715 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3719 handle_aggregate_stats_request(struct ofconn *ofconn,
3720 const struct ofp_header *oh)
3721 OVS_EXCLUDED(ofproto_mutex)
3723 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3724 struct ofputil_flow_stats_request request;
3725 struct ofputil_aggregate_stats stats;
3726 bool unknown_packets, unknown_bytes;
3727 struct rule_criteria criteria;
3728 struct rule_collection rules;
3729 struct ofpbuf *reply;
3733 error = ofputil_decode_flow_stats_request(&request, oh);
3738 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3739 request.cookie, request.cookie_mask,
3740 request.out_port, request.out_group);
3742 ovs_mutex_lock(&ofproto_mutex);
3743 error = collect_rules_loose(ofproto, &criteria, &rules);
3744 rule_criteria_destroy(&criteria);
3746 rule_collection_ref(&rules);
3748 ovs_mutex_unlock(&ofproto_mutex);
3754 memset(&stats, 0, sizeof stats);
3755 unknown_packets = unknown_bytes = false;
3756 for (i = 0; i < rules.n; i++) {
3757 struct rule *rule = rules.rules[i];
3758 uint64_t packet_count;
3759 uint64_t byte_count;
3762 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3763 &byte_count, &used);
3765 if (packet_count == UINT64_MAX) {
3766 unknown_packets = true;
3768 stats.packet_count += packet_count;
3771 if (byte_count == UINT64_MAX) {
3772 unknown_bytes = true;
3774 stats.byte_count += byte_count;
3779 if (unknown_packets) {
3780 stats.packet_count = UINT64_MAX;
3782 if (unknown_bytes) {
3783 stats.byte_count = UINT64_MAX;
3786 rule_collection_unref(&rules);
3787 rule_collection_destroy(&rules);
3789 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3790 ofconn_send_reply(ofconn, reply);
3795 struct queue_stats_cbdata {
3796 struct ofport *ofport;
3797 struct list replies;
3802 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3803 const struct netdev_queue_stats *stats)
3805 struct ofputil_queue_stats oqs;
3807 oqs.port_no = cbdata->ofport->pp.port_no;
3808 oqs.queue_id = queue_id;
3809 oqs.tx_bytes = stats->tx_bytes;
3810 oqs.tx_packets = stats->tx_packets;
3811 oqs.tx_errors = stats->tx_errors;
3812 if (stats->created != LLONG_MIN) {
3813 calc_duration(stats->created, cbdata->now,
3814 &oqs.duration_sec, &oqs.duration_nsec);
3816 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3818 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3822 handle_queue_stats_dump_cb(uint32_t queue_id,
3823 struct netdev_queue_stats *stats,
3826 struct queue_stats_cbdata *cbdata = cbdata_;
3828 put_queue_stats(cbdata, queue_id, stats);
3832 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3833 struct queue_stats_cbdata *cbdata)
3835 cbdata->ofport = port;
3836 if (queue_id == OFPQ_ALL) {
3837 netdev_dump_queue_stats(port->netdev,
3838 handle_queue_stats_dump_cb, cbdata);
3840 struct netdev_queue_stats stats;
3842 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3843 put_queue_stats(cbdata, queue_id, &stats);
3845 return OFPERR_OFPQOFC_BAD_QUEUE;
3852 handle_queue_stats_request(struct ofconn *ofconn,
3853 const struct ofp_header *rq)
3855 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3856 struct queue_stats_cbdata cbdata;
3857 struct ofport *port;
3859 struct ofputil_queue_stats_request oqsr;
3861 COVERAGE_INC(ofproto_queue_req);
3863 ofpmp_init(&cbdata.replies, rq);
3864 cbdata.now = time_msec();
3866 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3871 if (oqsr.port_no == OFPP_ANY) {
3872 error = OFPERR_OFPQOFC_BAD_QUEUE;
3873 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3874 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3879 port = ofproto_get_port(ofproto, oqsr.port_no);
3881 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3882 : OFPERR_OFPQOFC_BAD_PORT);
3885 ofconn_send_replies(ofconn, &cbdata.replies);
3887 ofpbuf_list_delete(&cbdata.replies);
3894 is_flow_deletion_pending(const struct ofproto *ofproto,
3895 const struct cls_rule *cls_rule,
3897 OVS_REQUIRES(ofproto_mutex)
3899 if (!hmap_is_empty(&ofproto->deletions)) {
3900 struct ofoperation *op;
3902 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3903 cls_rule_hash(cls_rule, table_id),
3904 &ofproto->deletions) {
3905 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3915 should_evict_a_rule(struct oftable *table, unsigned int extra_space)
3916 OVS_REQUIRES(ofproto_mutex)
3917 OVS_NO_THREAD_SAFETY_ANALYSIS
3919 return classifier_count(&table->cls) + extra_space > table->max_flows;
3923 evict_rules_from_table(struct ofproto *ofproto, struct oftable *table,
3924 unsigned int extra_space)
3925 OVS_REQUIRES(ofproto_mutex)
3927 while (should_evict_a_rule(table, extra_space)) {
3930 if (!choose_rule_to_evict(table, &rule)) {
3931 return OFPERR_OFPFMFC_TABLE_FULL;
3932 } else if (rule->pending) {
3933 return OFPROTO_POSTPONE;
3935 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
3936 delete_flow__(rule, group, OFPRR_EVICTION);
3937 ofopgroup_submit(group);
3944 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3945 * in which no matching flow already exists in the flow table.
3947 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3948 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3949 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3950 * initiated now but may be retried later.
3952 * The caller retains ownership of 'fm->ofpacts'.
3954 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3957 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3958 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3959 OVS_REQUIRES(ofproto_mutex)
3961 struct oftable *table;
3962 struct ofopgroup *group;
3968 if (!check_table_id(ofproto, fm->table_id)) {
3969 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3974 if (fm->table_id == 0xff) {
3975 if (ofproto->ofproto_class->rule_choose_table) {
3976 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3982 ovs_assert(table_id < ofproto->n_tables);
3986 } else if (fm->table_id < ofproto->n_tables) {
3987 table_id = fm->table_id;
3989 return OFPERR_OFPBRC_BAD_TABLE_ID;
3992 table = &ofproto->tables[table_id];
3994 if (table->flags & OFTABLE_READONLY) {
3995 return OFPERR_OFPBRC_EPERM;
3998 cls_rule_init(&cr, &fm->match, fm->priority);
4000 /* Transform "add" into "modify" if there's an existing identical flow. */
4001 fat_rwlock_rdlock(&table->cls.rwlock);
4002 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
4003 fat_rwlock_unlock(&table->cls.rwlock);
4005 cls_rule_destroy(&cr);
4006 if (!rule_is_modifiable(rule)) {
4007 return OFPERR_OFPBRC_EPERM;
4008 } else if (rule->pending) {
4009 return OFPROTO_POSTPONE;
4011 struct rule_collection rules;
4013 rule_collection_init(&rules);
4014 rule_collection_add(&rules, rule);
4015 fm->modify_cookie = true;
4016 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4017 rule_collection_destroy(&rules);
4023 /* Serialize against pending deletion. */
4024 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
4025 cls_rule_destroy(&cr);
4026 return OFPROTO_POSTPONE;
4029 /* Check for overlap, if requested. */
4030 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
4033 fat_rwlock_rdlock(&table->cls.rwlock);
4034 overlaps = classifier_rule_overlaps(&table->cls, &cr);
4035 fat_rwlock_unlock(&table->cls.rwlock);
4038 cls_rule_destroy(&cr);
4039 return OFPERR_OFPFMFC_OVERLAP;
4043 /* If necessary, evict an existing rule to clear out space. */
4044 error = evict_rules_from_table(ofproto, table, 1);
4046 cls_rule_destroy(&cr);
4050 /* Allocate new rule. */
4051 rule = ofproto->ofproto_class->rule_alloc();
4053 cls_rule_destroy(&cr);
4054 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
4055 ofproto->name, ovs_strerror(error));
4059 /* Initialize base state. */
4060 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4061 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
4062 ovs_refcount_init(&rule->ref_count);
4063 rule->pending = NULL;
4064 rule->flow_cookie = fm->new_cookie;
4065 rule->created = rule->modified = time_msec();
4067 ovs_mutex_init(&rule->mutex);
4068 ovs_mutex_lock(&rule->mutex);
4069 rule->idle_timeout = fm->idle_timeout;
4070 rule->hard_timeout = fm->hard_timeout;
4071 ovs_mutex_unlock(&rule->mutex);
4073 *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
4074 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4075 rule->actions = rule_actions_create(ofproto, fm->ofpacts, fm->ofpacts_len);
4076 list_init(&rule->meter_list_node);
4077 rule->eviction_group = NULL;
4078 list_init(&rule->expirable);
4079 rule->monitor_flags = 0;
4080 rule->add_seqno = 0;
4081 rule->modify_seqno = 0;
4083 /* Construct rule, initializing derived state. */
4084 error = ofproto->ofproto_class->rule_construct(rule);
4086 ofproto_rule_destroy__(rule);
4091 oftable_insert_rule(rule);
4093 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4094 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
4095 ofproto->ofproto_class->rule_insert(rule);
4096 ofopgroup_submit(group);
4101 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4103 /* Modifies the rules listed in 'rules', changing their actions to match those
4106 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4109 * Returns 0 on success, otherwise an OpenFlow error code. */
4111 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4112 struct ofputil_flow_mod *fm, const struct ofp_header *request,
4113 const struct rule_collection *rules)
4114 OVS_REQUIRES(ofproto_mutex)
4116 enum ofoperation_type type;
4117 struct ofopgroup *group;
4121 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
4122 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4123 error = OFPERR_OFPBRC_EPERM;
4124 for (i = 0; i < rules->n; i++) {
4125 struct rule *rule = rules->rules[i];
4126 struct ofoperation *op;
4127 bool actions_changed;
4128 bool reset_counters;
4130 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
4132 if (rule_is_modifiable(rule)) {
4133 /* At least one rule is modifiable, don't report EPERM error. */
4139 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
4140 rule->actions->ofpacts,
4141 rule->actions->ofpacts_len);
4143 op = ofoperation_create(group, rule, type, 0);
4145 if (fm->modify_cookie && fm->new_cookie != OVS_BE64_MAX) {
4146 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
4148 if (type == OFOPERATION_REPLACE) {
4149 ovs_mutex_lock(&rule->mutex);
4150 rule->idle_timeout = fm->idle_timeout;
4151 rule->hard_timeout = fm->hard_timeout;
4152 ovs_mutex_unlock(&rule->mutex);
4154 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4155 if (fm->idle_timeout || fm->hard_timeout) {
4156 if (!rule->eviction_group) {
4157 eviction_group_add_rule(rule);
4160 eviction_group_remove_rule(rule);
4164 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
4165 if (actions_changed || reset_counters) {
4166 struct rule_actions *new_actions;
4168 op->actions = rule->actions;
4169 new_actions = rule_actions_create(ofproto,
4170 fm->ofpacts, fm->ofpacts_len);
4172 ovs_mutex_lock(&rule->mutex);
4173 rule->actions = new_actions;
4174 ovs_mutex_unlock(&rule->mutex);
4176 rule->ofproto->ofproto_class->rule_modify_actions(rule,
4179 ofoperation_complete(op, 0);
4182 ofopgroup_submit(group);
4188 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
4189 struct ofputil_flow_mod *fm, const struct ofp_header *request)
4190 OVS_REQUIRES(ofproto_mutex)
4192 if (fm->cookie_mask != htonll(0) || fm->new_cookie == OVS_BE64_MAX) {
4195 return add_flow(ofproto, ofconn, fm, request);
4198 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4201 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4204 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4205 struct ofputil_flow_mod *fm,
4206 const struct ofp_header *request)
4207 OVS_REQUIRES(ofproto_mutex)
4209 struct rule_criteria criteria;
4210 struct rule_collection rules;
4213 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4214 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4215 error = collect_rules_loose(ofproto, &criteria, &rules);
4216 rule_criteria_destroy(&criteria);
4219 error = (rules.n > 0
4220 ? modify_flows__(ofproto, ofconn, fm, request, &rules)
4221 : modify_flows_add(ofproto, ofconn, fm, request));
4224 rule_collection_destroy(&rules);
4229 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
4232 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4235 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4236 struct ofputil_flow_mod *fm,
4237 const struct ofp_header *request)
4238 OVS_REQUIRES(ofproto_mutex)
4240 struct rule_criteria criteria;
4241 struct rule_collection rules;
4244 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4245 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4246 error = collect_rules_strict(ofproto, &criteria, &rules);
4247 rule_criteria_destroy(&criteria);
4251 error = modify_flows_add(ofproto, ofconn, fm, request);
4252 } else if (rules.n == 1) {
4253 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4257 rule_collection_destroy(&rules);
4262 /* OFPFC_DELETE implementation. */
4265 delete_flow__(struct rule *rule, struct ofopgroup *group,
4266 enum ofp_flow_removed_reason reason)
4267 OVS_REQUIRES(ofproto_mutex)
4269 struct ofproto *ofproto = rule->ofproto;
4271 ofproto_rule_send_removed(rule, reason);
4273 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
4274 oftable_remove_rule(rule);
4275 ofproto->ofproto_class->rule_delete(rule);
4278 /* Deletes the rules listed in 'rules'.
4280 * Returns 0 on success, otherwise an OpenFlow error code. */
4282 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4283 const struct ofp_header *request,
4284 const struct rule_collection *rules,
4285 enum ofp_flow_removed_reason reason)
4286 OVS_REQUIRES(ofproto_mutex)
4288 struct ofopgroup *group;
4291 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
4292 for (i = 0; i < rules->n; i++) {
4293 delete_flow__(rules->rules[i], group, reason);
4295 ofopgroup_submit(group);
4300 /* Implements OFPFC_DELETE. */
4302 delete_flows_loose(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, 0,
4312 fm->cookie, fm->cookie_mask,
4313 fm->out_port, fm->out_group);
4314 error = collect_rules_loose(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);
4325 /* Implements OFPFC_DELETE_STRICT. */
4327 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4328 const struct ofputil_flow_mod *fm,
4329 const struct ofp_header *request)
4330 OVS_REQUIRES(ofproto_mutex)
4332 struct rule_criteria criteria;
4333 struct rule_collection rules;
4336 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4337 fm->cookie, fm->cookie_mask,
4338 fm->out_port, fm->out_group);
4339 error = collect_rules_strict(ofproto, &criteria, &rules);
4340 rule_criteria_destroy(&criteria);
4342 if (!error && rules.n > 0) {
4343 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4345 rule_collection_destroy(&rules);
4351 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
4352 OVS_REQUIRES(ofproto_mutex)
4354 struct ofputil_flow_removed fr;
4357 if (ofproto_rule_is_hidden(rule) ||
4358 !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
4362 minimatch_expand(&rule->cr.match, &fr.match);
4363 fr.priority = rule->cr.priority;
4364 fr.cookie = rule->flow_cookie;
4366 fr.table_id = rule->table_id;
4367 calc_duration(rule->created, time_msec(),
4368 &fr.duration_sec, &fr.duration_nsec);
4369 ovs_mutex_lock(&rule->mutex);
4370 fr.idle_timeout = rule->idle_timeout;
4371 fr.hard_timeout = rule->hard_timeout;
4372 ovs_mutex_unlock(&rule->mutex);
4373 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
4374 &fr.byte_count, &used);
4376 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
4379 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4380 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4383 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
4386 * ofproto implementation ->run() functions should use this function to expire
4387 * OpenFlow flows. */
4389 ofproto_rule_expire(struct rule *rule, uint8_t reason)
4390 OVS_REQUIRES(ofproto_mutex)
4392 struct ofproto *ofproto = rule->ofproto;
4394 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
4395 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
4397 ofproto_rule_delete__(ofproto, rule, reason);
4400 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4401 * means "infinite". */
4403 reduce_timeout(uint16_t max, uint16_t *timeout)
4405 if (max && (!*timeout || *timeout > max)) {
4410 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4411 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4412 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4414 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4416 ofproto_rule_reduce_timeouts(struct rule *rule,
4417 uint16_t idle_timeout, uint16_t hard_timeout)
4418 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
4420 if (!idle_timeout && !hard_timeout) {
4424 ovs_mutex_lock(&ofproto_mutex);
4425 if (list_is_empty(&rule->expirable)) {
4426 list_insert(&rule->ofproto->expirable, &rule->expirable);
4428 ovs_mutex_unlock(&ofproto_mutex);
4430 ovs_mutex_lock(&rule->mutex);
4431 reduce_timeout(idle_timeout, &rule->idle_timeout);
4432 reduce_timeout(hard_timeout, &rule->hard_timeout);
4433 ovs_mutex_unlock(&rule->mutex);
4437 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4438 OVS_EXCLUDED(ofproto_mutex)
4440 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4441 struct ofputil_flow_mod fm;
4442 uint64_t ofpacts_stub[1024 / 8];
4443 struct ofpbuf ofpacts;
4447 error = reject_slave_controller(ofconn);
4452 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4453 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
4455 u16_to_ofp(ofproto->max_ports),
4458 error = ofproto_check_ofpacts(ofproto, fm.ofpacts, fm.ofpacts_len);
4461 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
4464 goto exit_free_ofpacts;
4467 /* Record the operation for logging a summary report. */
4468 switch (fm.command) {
4474 case OFPFC_MODIFY_STRICT:
4475 ofproto->n_modify++;
4479 case OFPFC_DELETE_STRICT:
4480 ofproto->n_delete++;
4485 if (ofproto->next_op_report == LLONG_MAX) {
4486 ofproto->first_op = now;
4487 ofproto->next_op_report = MAX(now + 10 * 1000,
4488 ofproto->op_backoff);
4489 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
4491 ofproto->last_op = now;
4494 ofpbuf_uninit(&ofpacts);
4500 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
4501 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
4502 OVS_EXCLUDED(ofproto_mutex)
4506 ovs_mutex_lock(&ofproto_mutex);
4507 if (ofproto->n_pending < 50) {
4508 switch (fm->command) {
4510 error = add_flow(ofproto, ofconn, fm, oh);
4514 error = modify_flows_loose(ofproto, ofconn, fm, oh);
4517 case OFPFC_MODIFY_STRICT:
4518 error = modify_flow_strict(ofproto, ofconn, fm, oh);
4522 error = delete_flows_loose(ofproto, ofconn, fm, oh);
4525 case OFPFC_DELETE_STRICT:
4526 error = delete_flow_strict(ofproto, ofconn, fm, oh);
4530 if (fm->command > 0xff) {
4531 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4532 "flow_mod_table_id extension is not enabled",
4535 error = OFPERR_OFPFMFC_BAD_COMMAND;
4539 ovs_assert(!list_is_empty(&ofproto->pending));
4540 error = OFPROTO_POSTPONE;
4542 ovs_mutex_unlock(&ofproto_mutex);
4544 run_rule_executes(ofproto);
4549 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4551 struct ofputil_role_request request;
4552 struct ofputil_role_request reply;
4556 error = ofputil_decode_role_message(oh, &request);
4561 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4562 if (ofconn_get_role(ofconn) != request.role
4563 && ofconn_has_pending_opgroups(ofconn)) {
4564 return OFPROTO_POSTPONE;
4567 if (request.have_generation_id
4568 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4569 return OFPERR_OFPRRFC_STALE;
4572 ofconn_set_role(ofconn, request.role);
4575 reply.role = ofconn_get_role(ofconn);
4576 reply.have_generation_id = ofconn_get_master_election_id(
4577 ofconn, &reply.generation_id);
4578 buf = ofputil_encode_role_reply(oh, &reply);
4579 ofconn_send_reply(ofconn, buf);
4585 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4586 const struct ofp_header *oh)
4588 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4589 enum ofputil_protocol cur, next;
4591 cur = ofconn_get_protocol(ofconn);
4592 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4593 ofconn_set_protocol(ofconn, next);
4599 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4601 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4602 enum ofputil_protocol cur, next;
4603 enum ofputil_protocol next_base;
4605 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4607 return OFPERR_OFPBRC_EPERM;
4610 cur = ofconn_get_protocol(ofconn);
4611 next = ofputil_protocol_set_base(cur, next_base);
4612 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4613 /* Avoid sending async messages in surprising protocol. */
4614 return OFPROTO_POSTPONE;
4617 ofconn_set_protocol(ofconn, next);
4622 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4623 const struct ofp_header *oh)
4625 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4628 format = ntohl(msg->format);
4629 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4630 return OFPERR_OFPBRC_EPERM;
4633 if (format != ofconn_get_packet_in_format(ofconn)
4634 && ofconn_has_pending_opgroups(ofconn)) {
4635 /* Avoid sending async message in surprsing packet in format. */
4636 return OFPROTO_POSTPONE;
4639 ofconn_set_packet_in_format(ofconn, format);
4644 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4646 const struct nx_async_config *msg = ofpmsg_body(oh);
4647 uint32_t master[OAM_N_TYPES];
4648 uint32_t slave[OAM_N_TYPES];
4650 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4651 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4652 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4654 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4655 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4656 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4658 ofconn_set_async_config(ofconn, master, slave);
4659 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4660 !ofconn_get_miss_send_len(ofconn)) {
4661 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4668 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4671 uint32_t master[OAM_N_TYPES];
4672 uint32_t slave[OAM_N_TYPES];
4673 struct nx_async_config *msg;
4675 ofconn_get_async_config(ofconn, master, slave);
4676 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4677 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4679 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4680 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4681 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4683 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4684 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4685 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4687 ofconn_send_reply(ofconn, buf);
4693 handle_nxt_set_controller_id(struct ofconn *ofconn,
4694 const struct ofp_header *oh)
4696 const struct nx_controller_id *nci = ofpmsg_body(oh);
4698 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4699 return OFPERR_NXBRC_MUST_BE_ZERO;
4702 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4707 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4711 if (ofconn_has_pending_opgroups(ofconn)) {
4712 return OFPROTO_POSTPONE;
4715 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4716 ? OFPRAW_OFPT10_BARRIER_REPLY
4717 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4718 ofconn_send_reply(ofconn, buf);
4723 ofproto_compose_flow_refresh_update(const struct rule *rule,
4724 enum nx_flow_monitor_flags flags,
4726 OVS_REQUIRES(ofproto_mutex)
4728 struct ofoperation *op = rule->pending;
4729 const struct rule_actions *actions;
4730 struct ofputil_flow_update fu;
4733 if (op && op->type == OFOPERATION_ADD) {
4734 /* We'll report the final flow when the operation completes. Reporting
4735 * it now would cause a duplicate report later. */
4739 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4740 ? NXFME_ADDED : NXFME_MODIFIED);
4742 ovs_mutex_lock(&rule->mutex);
4743 fu.idle_timeout = rule->idle_timeout;
4744 fu.hard_timeout = rule->hard_timeout;
4745 ovs_mutex_unlock(&rule->mutex);
4746 fu.table_id = rule->table_id;
4747 fu.cookie = rule->flow_cookie;
4748 minimatch_expand(&rule->cr.match, &match);
4750 fu.priority = rule->cr.priority;
4752 if (!(flags & NXFMF_ACTIONS)) {
4755 actions = rule->actions;
4757 /* An operation is in progress. Use the previous version of the flow's
4758 * actions, so that when the operation commits we report the change. */
4760 case OFOPERATION_ADD:
4763 case OFOPERATION_MODIFY:
4764 case OFOPERATION_REPLACE:
4765 actions = op->actions ? op->actions : rule->actions;
4768 case OFOPERATION_DELETE:
4769 actions = rule->actions;
4776 fu.ofpacts = actions ? actions->ofpacts : NULL;
4777 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
4779 if (list_is_empty(msgs)) {
4780 ofputil_start_flow_update(msgs);
4782 ofputil_append_flow_update(&fu, msgs);
4786 ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4788 OVS_REQUIRES(ofproto_mutex)
4792 for (i = 0; i < rules->n; i++) {
4793 struct rule *rule = rules->rules[i];
4794 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4795 rule->monitor_flags = 0;
4797 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4802 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4803 struct rule *rule, uint64_t seqno,
4804 struct rule_collection *rules)
4805 OVS_REQUIRES(ofproto_mutex)
4807 enum nx_flow_monitor_flags update;
4809 if (ofproto_rule_is_hidden(rule)) {
4814 ? ofoperation_has_out_port(rule->pending, m->out_port)
4815 : ofproto_rule_has_out_port(rule, m->out_port))) {
4820 if (rule->add_seqno > seqno) {
4821 update = NXFMF_ADD | NXFMF_MODIFY;
4822 } else if (rule->modify_seqno > seqno) {
4823 update = NXFMF_MODIFY;
4828 if (!(m->flags & update)) {
4832 update = NXFMF_INITIAL;
4835 if (!rule->monitor_flags) {
4836 rule_collection_add(rules, rule);
4838 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4842 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4844 struct rule_collection *rules)
4845 OVS_REQUIRES(ofproto_mutex)
4847 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4848 const struct ofoperation *op;
4849 const struct oftable *table;
4850 struct cls_rule target;
4852 cls_rule_init_from_minimatch(&target, &m->match, 0);
4853 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4854 struct cls_cursor cursor;
4857 fat_rwlock_rdlock(&table->cls.rwlock);
4858 cls_cursor_init(&cursor, &table->cls, &target);
4859 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4860 ovs_assert(!rule->pending); /* XXX */
4861 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4863 fat_rwlock_unlock(&table->cls.rwlock);
4866 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4867 struct rule *rule = op->rule;
4869 if (((m->table_id == 0xff
4870 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4871 : m->table_id == rule->table_id))
4872 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4873 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4876 cls_rule_destroy(&target);
4880 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4881 struct rule_collection *rules)
4882 OVS_REQUIRES(ofproto_mutex)
4884 if (m->flags & NXFMF_INITIAL) {
4885 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4890 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4891 uint64_t seqno, struct rule_collection *rules)
4892 OVS_REQUIRES(ofproto_mutex)
4894 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4898 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4899 OVS_EXCLUDED(ofproto_mutex)
4901 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4902 struct ofmonitor **monitors;
4903 size_t n_monitors, allocated_monitors;
4904 struct rule_collection rules;
4905 struct list replies;
4911 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4913 n_monitors = allocated_monitors = 0;
4915 ovs_mutex_lock(&ofproto_mutex);
4917 struct ofputil_flow_monitor_request request;
4918 struct ofmonitor *m;
4921 retval = ofputil_decode_flow_monitor_request(&request, &b);
4922 if (retval == EOF) {
4924 } else if (retval) {
4929 if (request.table_id != 0xff
4930 && request.table_id >= ofproto->n_tables) {
4931 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4935 error = ofmonitor_create(&request, ofconn, &m);
4940 if (n_monitors >= allocated_monitors) {
4941 monitors = x2nrealloc(monitors, &allocated_monitors,
4944 monitors[n_monitors++] = m;
4947 rule_collection_init(&rules);
4948 for (i = 0; i < n_monitors; i++) {
4949 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4952 ofpmp_init(&replies, oh);
4953 ofmonitor_compose_refresh_updates(&rules, &replies);
4954 ovs_mutex_unlock(&ofproto_mutex);
4956 rule_collection_destroy(&rules);
4958 ofconn_send_replies(ofconn, &replies);
4964 for (i = 0; i < n_monitors; i++) {
4965 ofmonitor_destroy(monitors[i]);
4968 ovs_mutex_unlock(&ofproto_mutex);
4974 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4975 OVS_EXCLUDED(ofproto_mutex)
4977 struct ofmonitor *m;
4981 id = ofputil_decode_flow_monitor_cancel(oh);
4983 ovs_mutex_lock(&ofproto_mutex);
4984 m = ofmonitor_lookup(ofconn, id);
4986 ofmonitor_destroy(m);
4989 error = OFPERR_NXBRC_FM_BAD_ID;
4991 ovs_mutex_unlock(&ofproto_mutex);
4996 /* Meters implementation.
4998 * Meter table entry, indexed by the OpenFlow meter_id.
4999 * These are always dynamically allocated to allocate enough space for
5001 * 'created' is used to compute the duration for meter stats.
5002 * 'list rules' is needed so that we can delete the dependent rules when the
5003 * meter table entry is deleted.
5004 * 'provider_meter_id' is for the provider's private use.
5007 long long int created; /* Time created. */
5008 struct list rules; /* List of "struct rule_dpif"s. */
5009 ofproto_meter_id provider_meter_id;
5010 uint16_t flags; /* Meter flags. */
5011 uint16_t n_bands; /* Number of meter bands. */
5012 struct ofputil_meter_band *bands;
5016 * This is used in instruction validation at flow set-up time,
5017 * as flows may not use non-existing meters.
5018 * Return value of UINT32_MAX signifies an invalid meter.
5021 get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id)
5023 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
5024 const struct meter *meter = ofproto->meters[of_meter_id];
5026 return meter->provider_meter_id.uint32;
5033 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
5037 meter->flags = config->flags;
5038 meter->n_bands = config->n_bands;
5039 meter->bands = xmemdup(config->bands,
5040 config->n_bands * sizeof *meter->bands);
5043 static struct meter *
5044 meter_create(const struct ofputil_meter_config *config,
5045 ofproto_meter_id provider_meter_id)
5047 struct meter *meter;
5049 meter = xzalloc(sizeof *meter);
5050 meter->provider_meter_id = provider_meter_id;
5051 meter->created = time_msec();
5052 list_init(&meter->rules);
5054 meter_update(meter, config);
5060 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
5061 OVS_REQUIRES(ofproto_mutex)
5064 for (mid = first; mid <= last; ++mid) {
5065 struct meter *meter = ofproto->meters[mid];
5067 ofproto->meters[mid] = NULL;
5068 ofproto->ofproto_class->meter_del(ofproto,
5069 meter->provider_meter_id);
5077 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5079 ofproto_meter_id provider_meter_id = { UINT32_MAX };
5080 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
5084 return OFPERR_OFPMMFC_METER_EXISTS;
5087 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
5090 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
5091 *meterp = meter_create(&mm->meter, provider_meter_id);
5097 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5099 struct meter *meter = ofproto->meters[mm->meter.meter_id];
5101 uint32_t provider_meter_id;
5104 return OFPERR_OFPMMFC_UNKNOWN_METER;
5107 provider_meter_id = meter->provider_meter_id.uint32;
5108 error = ofproto->ofproto_class->meter_set(ofproto,
5109 &meter->provider_meter_id,
5111 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
5113 meter_update(meter, &mm->meter);
5119 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
5120 struct ofputil_meter_mod *mm)
5121 OVS_EXCLUDED(ofproto_mutex)
5123 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5124 uint32_t meter_id = mm->meter.meter_id;
5125 struct rule_collection rules;
5126 enum ofperr error = 0;
5127 uint32_t first, last;
5129 if (meter_id == OFPM13_ALL) {
5131 last = ofproto->meter_features.max_meters;
5133 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
5136 first = last = meter_id;
5139 /* First delete the rules that use this meter. If any of those rules are
5140 * currently being modified, postpone the whole operation until later. */
5141 rule_collection_init(&rules);
5142 ovs_mutex_lock(&ofproto_mutex);
5143 for (meter_id = first; meter_id <= last; ++meter_id) {
5144 struct meter *meter = ofproto->meters[meter_id];
5145 if (meter && !list_is_empty(&meter->rules)) {
5148 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
5149 if (rule->pending) {
5150 error = OFPROTO_POSTPONE;
5153 rule_collection_add(&rules, rule);
5158 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
5161 /* Delete the meters. */
5162 meter_delete(ofproto, first, last);
5165 ovs_mutex_unlock(&ofproto_mutex);
5166 rule_collection_destroy(&rules);
5172 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5174 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5175 struct ofputil_meter_mod mm;
5176 uint64_t bands_stub[256 / 8];
5177 struct ofpbuf bands;
5181 error = reject_slave_controller(ofconn);
5186 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5188 error = ofputil_decode_meter_mod(oh, &mm, &bands);
5190 goto exit_free_bands;
5193 meter_id = mm.meter.meter_id;
5195 if (mm.command != OFPMC13_DELETE) {
5196 /* Fails also when meters are not implemented by the provider. */
5197 if (meter_id == 0 || meter_id > OFPM13_MAX) {
5198 error = OFPERR_OFPMMFC_INVALID_METER;
5199 goto exit_free_bands;
5200 } else if (meter_id > ofproto->meter_features.max_meters) {
5201 error = OFPERR_OFPMMFC_OUT_OF_METERS;
5202 goto exit_free_bands;
5204 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
5205 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
5206 goto exit_free_bands;
5210 switch (mm.command) {
5212 error = handle_add_meter(ofproto, &mm);
5215 case OFPMC13_MODIFY:
5216 error = handle_modify_meter(ofproto, &mm);
5219 case OFPMC13_DELETE:
5220 error = handle_delete_meter(ofconn, oh, &mm);
5224 error = OFPERR_OFPMMFC_BAD_COMMAND;
5229 ofpbuf_uninit(&bands);
5234 handle_meter_features_request(struct ofconn *ofconn,
5235 const struct ofp_header *request)
5237 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5238 struct ofputil_meter_features features;
5241 if (ofproto->ofproto_class->meter_get_features) {
5242 ofproto->ofproto_class->meter_get_features(ofproto, &features);
5244 memset(&features, 0, sizeof features);
5246 b = ofputil_encode_meter_features_reply(&features, request);
5248 ofconn_send_reply(ofconn, b);
5253 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
5256 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5257 struct list replies;
5258 uint64_t bands_stub[256 / 8];
5259 struct ofpbuf bands;
5260 uint32_t meter_id, first, last;
5262 ofputil_decode_meter_request(request, &meter_id);
5264 if (meter_id == OFPM13_ALL) {
5266 last = ofproto->meter_features.max_meters;
5268 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
5269 !ofproto->meters[meter_id]) {
5270 return OFPERR_OFPMMFC_UNKNOWN_METER;
5272 first = last = meter_id;
5275 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5276 ofpmp_init(&replies, request);
5278 for (meter_id = first; meter_id <= last; ++meter_id) {
5279 struct meter *meter = ofproto->meters[meter_id];
5281 continue; /* Skip non-existing meters. */
5283 if (type == OFPTYPE_METER_STATS_REQUEST) {
5284 struct ofputil_meter_stats stats;
5286 stats.meter_id = meter_id;
5288 /* Provider sets the packet and byte counts, we do the rest. */
5289 stats.flow_count = list_size(&meter->rules);
5290 calc_duration(meter->created, time_msec(),
5291 &stats.duration_sec, &stats.duration_nsec);
5292 stats.n_bands = meter->n_bands;
5293 ofpbuf_clear(&bands);
5295 = ofpbuf_put_uninit(&bands,
5296 meter->n_bands * sizeof *stats.bands);
5298 if (!ofproto->ofproto_class->meter_get(ofproto,
5299 meter->provider_meter_id,
5301 ofputil_append_meter_stats(&replies, &stats);
5303 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
5304 struct ofputil_meter_config config;
5306 config.meter_id = meter_id;
5307 config.flags = meter->flags;
5308 config.n_bands = meter->n_bands;
5309 config.bands = meter->bands;
5310 ofputil_append_meter_config(&replies, &config);
5314 ofconn_send_replies(ofconn, &replies);
5315 ofpbuf_uninit(&bands);
5320 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5321 struct ofgroup **group)
5322 OVS_TRY_RDLOCK(true, (*group)->rwlock)
5324 ovs_rwlock_rdlock(&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_rdlock(&(*group)->rwlock);
5329 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5333 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5338 ofproto_group_release(struct ofgroup *group)
5339 OVS_RELEASES(group->rwlock)
5341 ovs_rwlock_unlock(&group->rwlock);
5345 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
5346 struct ofgroup **group)
5347 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
5348 OVS_TRY_WRLOCK(true, (*group)->rwlock)
5350 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5351 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5352 hash_int(group_id, 0), &ofproto->groups) {
5353 if ((*group)->group_id == group_id) {
5354 ovs_rwlock_wrlock(&(*group)->rwlock);
5358 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5363 ofproto_group_exists__(const struct ofproto *ofproto, uint32_t group_id)
5364 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5366 struct ofgroup *grp;
5368 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
5369 hash_int(group_id, 0), &ofproto->groups) {
5370 if (grp->group_id == group_id) {
5378 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
5379 OVS_EXCLUDED(ofproto->groups_rwlock)
5383 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5384 exists = ofproto_group_exists__(ofproto, group_id);
5385 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5391 group_get_ref_count(struct ofgroup *group)
5392 OVS_EXCLUDED(ofproto_mutex)
5394 struct ofproto *ofproto = group->ofproto;
5395 struct rule_criteria criteria;
5396 struct rule_collection rules;
5401 match_init_catchall(&match);
5402 rule_criteria_init(&criteria, 0xff, &match, 0, htonll(0), htonll(0),
5403 OFPP_ANY, group->group_id);
5404 ovs_mutex_lock(&ofproto_mutex);
5405 error = collect_rules_loose(ofproto, &criteria, &rules);
5406 ovs_mutex_unlock(&ofproto_mutex);
5407 rule_criteria_destroy(&criteria);
5409 count = !error && rules.n < UINT32_MAX ? rules.n : UINT32_MAX;
5411 rule_collection_destroy(&rules);
5416 append_group_stats(struct ofgroup *group, struct list *replies)
5417 OVS_REQ_RDLOCK(group->rwlock)
5419 struct ofputil_group_stats ogs;
5420 struct ofproto *ofproto = group->ofproto;
5421 long long int now = time_msec();
5424 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
5426 /* Provider sets the packet and byte counts, we do the rest. */
5427 ogs.ref_count = group_get_ref_count(group);
5428 ogs.n_buckets = group->n_buckets;
5430 error = (ofproto->ofproto_class->group_get_stats
5431 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
5434 ogs.packet_count = UINT64_MAX;
5435 ogs.byte_count = UINT64_MAX;
5436 memset(ogs.bucket_stats, 0xff,
5437 ogs.n_buckets * sizeof *ogs.bucket_stats);
5440 ogs.group_id = group->group_id;
5441 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
5443 ofputil_append_group_stats(replies, &ogs);
5445 free(ogs.bucket_stats);
5449 handle_group_stats_request(struct ofconn *ofconn,
5450 const struct ofp_header *request)
5452 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5453 struct list replies;
5455 struct ofgroup *group;
5458 error = ofputil_decode_group_stats_request(request, &group_id);
5463 ofpmp_init(&replies, request);
5465 if (group_id == OFPG_ALL) {
5466 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5467 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5468 ovs_rwlock_rdlock(&group->rwlock);
5469 append_group_stats(group, &replies);
5470 ovs_rwlock_unlock(&group->rwlock);
5472 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5474 if (ofproto_group_lookup(ofproto, group_id, &group)) {
5475 append_group_stats(group, &replies);
5476 ofproto_group_release(group);
5480 ofconn_send_replies(ofconn, &replies);
5486 handle_group_desc_stats_request(struct ofconn *ofconn,
5487 const struct ofp_header *request)
5489 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5490 struct list replies;
5491 struct ofputil_group_desc gds;
5492 struct ofgroup *group;
5494 ofpmp_init(&replies, request);
5496 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5497 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5498 gds.group_id = group->group_id;
5499 gds.type = group->type;
5500 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
5502 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5504 ofconn_send_replies(ofconn, &replies);
5510 handle_group_features_stats_request(struct ofconn *ofconn,
5511 const struct ofp_header *request)
5513 struct ofproto *p = ofconn_get_ofproto(ofconn);
5516 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5518 ofconn_send_reply(ofconn, msg);
5525 handle_queue_get_config_request(struct ofconn *ofconn,
5526 const struct ofp_header *oh)
5528 struct ofproto *p = ofconn_get_ofproto(ofconn);
5529 struct netdev_queue_dump queue_dump;
5530 struct ofport *ofport;
5531 unsigned int queue_id;
5532 struct ofpbuf *reply;
5533 struct smap details;
5537 error = ofputil_decode_queue_get_config_request(oh, &request);
5542 ofport = ofproto_get_port(p, request);
5544 return OFPERR_OFPQOFC_BAD_PORT;
5547 reply = ofputil_encode_queue_get_config_reply(oh);
5549 smap_init(&details);
5550 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, ofport->netdev) {
5551 struct ofputil_queue_config queue;
5553 /* None of the existing queues have compatible properties, so we
5554 * hard-code omitting min_rate and max_rate. */
5555 queue.queue_id = queue_id;
5556 queue.min_rate = UINT16_MAX;
5557 queue.max_rate = UINT16_MAX;
5558 ofputil_append_queue_get_config_reply(reply, &queue);
5560 smap_destroy(&details);
5562 ofconn_send_reply(ofconn, reply);
5567 /* Implements OFPGC11_ADD
5568 * in which no matching flow already exists in the flow table.
5570 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
5571 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
5572 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
5573 * initiated now but may be retried later.
5575 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
5576 * ownership remains with the caller.
5578 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5581 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5583 struct ofgroup *ofgroup;
5586 if (gm->group_id > OFPG_MAX) {
5587 return OFPERR_OFPGMFC_INVALID_GROUP;
5589 if (gm->type > OFPGT11_FF) {
5590 return OFPERR_OFPGMFC_BAD_TYPE;
5593 /* Allocate new group and initialize it. */
5594 ofgroup = ofproto->ofproto_class->group_alloc();
5596 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
5597 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5600 ovs_rwlock_init(&ofgroup->rwlock);
5601 ofgroup->ofproto = ofproto;
5602 ofgroup->group_id = gm->group_id;
5603 ofgroup->type = gm->type;
5604 ofgroup->created = ofgroup->modified = time_msec();
5606 list_move(&ofgroup->buckets, &gm->buckets);
5607 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5609 /* Construct called BEFORE any locks are held. */
5610 error = ofproto->ofproto_class->group_construct(ofgroup);
5615 /* We wrlock as late as possible to minimize the time we jam any other
5616 * threads: No visible state changes before acquiring the lock. */
5617 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5619 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5620 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5624 if (ofproto_group_exists__(ofproto, gm->group_id)) {
5625 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5630 /* Insert new group. */
5631 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5632 hash_int(ofgroup->group_id, 0));
5633 ofproto->n_groups[ofgroup->type]++;
5635 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5640 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5641 ofproto->ofproto_class->group_destruct(ofgroup);
5643 ofputil_bucket_list_destroy(&ofgroup->buckets);
5644 ofproto->ofproto_class->group_dealloc(ofgroup);
5649 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5652 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5655 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5657 struct ofgroup *ofgroup;
5658 struct ofgroup *victim;
5661 if (gm->group_id > OFPG_MAX) {
5662 return OFPERR_OFPGMFC_INVALID_GROUP;
5665 if (gm->type > OFPGT11_FF) {
5666 return OFPERR_OFPGMFC_BAD_TYPE;
5669 victim = ofproto->ofproto_class->group_alloc();
5671 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5672 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5675 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
5676 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5679 /* Both group's and its container's write locks held now.
5680 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5681 if (ofgroup->type != gm->type
5682 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5683 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5688 list_move(&victim->buckets, &ofgroup->buckets);
5690 ofgroup->type = gm->type;
5691 list_move(&ofgroup->buckets, &gm->buckets);
5692 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5694 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
5696 ofputil_bucket_list_destroy(&victim->buckets);
5697 ofproto->n_groups[victim->type]--;
5698 ofproto->n_groups[ofgroup->type]++;
5699 ofgroup->modified = time_msec();
5701 ofputil_bucket_list_destroy(&ofgroup->buckets);
5704 list_move(&ofgroup->buckets, &victim->buckets);
5708 ovs_rwlock_unlock(&ofgroup->rwlock);
5709 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5711 ofproto->ofproto_class->group_dealloc(victim);
5716 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5717 OVS_RELEASES(ofproto->groups_rwlock)
5720 struct ofputil_flow_mod fm;
5722 /* Delete all flow entries containing this group in a group action */
5723 match_init_catchall(&match);
5724 flow_mod_init(&fm, &match, 0, NULL, 0, OFPFC_DELETE);
5725 fm.out_group = ofgroup->group_id;
5726 handle_flow_mod__(ofproto, NULL, &fm, NULL);
5728 /* Must wait until existing readers are done,
5729 * while holding the container's write lock at the same time. */
5730 ovs_rwlock_wrlock(&ofgroup->rwlock);
5731 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5732 /* No-one can find this group any more. */
5733 ofproto->n_groups[ofgroup->type]--;
5734 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5736 ofproto->ofproto_class->group_destruct(ofgroup);
5737 ofputil_bucket_list_destroy(&ofgroup->buckets);
5738 ovs_rwlock_unlock(&ofgroup->rwlock);
5739 ovs_rwlock_destroy(&ofgroup->rwlock);
5740 ofproto->ofproto_class->group_dealloc(ofgroup);
5743 /* Implements OFPGC_DELETE. */
5745 delete_group(struct ofproto *ofproto, uint32_t group_id)
5747 struct ofgroup *ofgroup;
5749 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5750 if (group_id == OFPG_ALL) {
5752 struct hmap_node *node = hmap_first(&ofproto->groups);
5756 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5757 delete_group__(ofproto, ofgroup);
5758 /* Lock for each node separately, so that we will not jam the
5759 * other threads for too long time. */
5760 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5763 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5764 hash_int(group_id, 0), &ofproto->groups) {
5765 if (ofgroup->group_id == group_id) {
5766 delete_group__(ofproto, ofgroup);
5771 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5775 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5777 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5778 struct ofputil_group_mod gm;
5781 error = reject_slave_controller(ofconn);
5786 error = ofputil_decode_group_mod(oh, &gm);
5791 switch (gm.command) {
5793 return add_group(ofproto, &gm);
5795 case OFPGC11_MODIFY:
5796 return modify_group(ofproto, &gm);
5798 case OFPGC11_DELETE:
5799 delete_group(ofproto, gm.group_id);
5803 if (gm.command > OFPGC11_DELETE) {
5804 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5805 ofproto->name, gm.command);
5807 return OFPERR_OFPGMFC_BAD_COMMAND;
5812 table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
5814 /* XXX Reject all configurations because none are currently supported */
5815 return OFPERR_OFPTMFC_BAD_CONFIG;
5817 if (tm->table_id == OFPTT_ALL) {
5819 for (i = 0; i < ofproto->n_tables; i++) {
5820 atomic_store(&ofproto->tables[i].config,
5821 (unsigned int)tm->config);
5823 } else if (!check_table_id(ofproto, tm->table_id)) {
5824 return OFPERR_OFPTMFC_BAD_TABLE;
5826 atomic_store(&ofproto->tables[tm->table_id].config,
5827 (unsigned int)tm->config);
5834 handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5836 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5837 struct ofputil_table_mod tm;
5840 error = reject_slave_controller(ofconn);
5845 error = ofputil_decode_table_mod(oh, &tm);
5850 return table_mod(ofproto, &tm);
5854 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5855 OVS_EXCLUDED(ofproto_mutex)
5857 const struct ofp_header *oh = msg->data;
5861 error = ofptype_decode(&type, oh);
5865 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
5866 && ofpmp_more(oh)) {
5867 /* We have no buffer implementation for multipart requests.
5868 * Report overflow for requests which consists of multiple
5870 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
5874 /* OpenFlow requests. */
5875 case OFPTYPE_ECHO_REQUEST:
5876 return handle_echo_request(ofconn, oh);
5878 case OFPTYPE_FEATURES_REQUEST:
5879 return handle_features_request(ofconn, oh);
5881 case OFPTYPE_GET_CONFIG_REQUEST:
5882 return handle_get_config_request(ofconn, oh);
5884 case OFPTYPE_SET_CONFIG:
5885 return handle_set_config(ofconn, oh);
5887 case OFPTYPE_PACKET_OUT:
5888 return handle_packet_out(ofconn, oh);
5890 case OFPTYPE_PORT_MOD:
5891 return handle_port_mod(ofconn, oh);
5893 case OFPTYPE_FLOW_MOD:
5894 return handle_flow_mod(ofconn, oh);
5896 case OFPTYPE_GROUP_MOD:
5897 return handle_group_mod(ofconn, oh);
5899 case OFPTYPE_TABLE_MOD:
5900 return handle_table_mod(ofconn, oh);
5902 case OFPTYPE_METER_MOD:
5903 return handle_meter_mod(ofconn, oh);
5905 case OFPTYPE_BARRIER_REQUEST:
5906 return handle_barrier_request(ofconn, oh);
5908 case OFPTYPE_ROLE_REQUEST:
5909 return handle_role_request(ofconn, oh);
5911 /* OpenFlow replies. */
5912 case OFPTYPE_ECHO_REPLY:
5915 /* Nicira extension requests. */
5916 case OFPTYPE_FLOW_MOD_TABLE_ID:
5917 return handle_nxt_flow_mod_table_id(ofconn, oh);
5919 case OFPTYPE_SET_FLOW_FORMAT:
5920 return handle_nxt_set_flow_format(ofconn, oh);
5922 case OFPTYPE_SET_PACKET_IN_FORMAT:
5923 return handle_nxt_set_packet_in_format(ofconn, oh);
5925 case OFPTYPE_SET_CONTROLLER_ID:
5926 return handle_nxt_set_controller_id(ofconn, oh);
5928 case OFPTYPE_FLOW_AGE:
5929 /* Nothing to do. */
5932 case OFPTYPE_FLOW_MONITOR_CANCEL:
5933 return handle_flow_monitor_cancel(ofconn, oh);
5935 case OFPTYPE_SET_ASYNC_CONFIG:
5936 return handle_nxt_set_async_config(ofconn, oh);
5938 case OFPTYPE_GET_ASYNC_REQUEST:
5939 return handle_nxt_get_async_request(ofconn, oh);
5941 /* Statistics requests. */
5942 case OFPTYPE_DESC_STATS_REQUEST:
5943 return handle_desc_stats_request(ofconn, oh);
5945 case OFPTYPE_FLOW_STATS_REQUEST:
5946 return handle_flow_stats_request(ofconn, oh);
5948 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5949 return handle_aggregate_stats_request(ofconn, oh);
5951 case OFPTYPE_TABLE_STATS_REQUEST:
5952 return handle_table_stats_request(ofconn, oh);
5954 case OFPTYPE_PORT_STATS_REQUEST:
5955 return handle_port_stats_request(ofconn, oh);
5957 case OFPTYPE_QUEUE_STATS_REQUEST:
5958 return handle_queue_stats_request(ofconn, oh);
5960 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5961 return handle_port_desc_stats_request(ofconn, oh);
5963 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5964 return handle_flow_monitor_request(ofconn, oh);
5966 case OFPTYPE_METER_STATS_REQUEST:
5967 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5968 return handle_meter_request(ofconn, oh, type);
5970 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5971 return handle_meter_features_request(ofconn, oh);
5973 case OFPTYPE_GROUP_STATS_REQUEST:
5974 return handle_group_stats_request(ofconn, oh);
5976 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5977 return handle_group_desc_stats_request(ofconn, oh);
5979 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5980 return handle_group_features_stats_request(ofconn, oh);
5982 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5983 return handle_queue_get_config_request(ofconn, oh);
5987 case OFPTYPE_FEATURES_REPLY:
5988 case OFPTYPE_GET_CONFIG_REPLY:
5989 case OFPTYPE_PACKET_IN:
5990 case OFPTYPE_FLOW_REMOVED:
5991 case OFPTYPE_PORT_STATUS:
5992 case OFPTYPE_BARRIER_REPLY:
5993 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5994 case OFPTYPE_DESC_STATS_REPLY:
5995 case OFPTYPE_FLOW_STATS_REPLY:
5996 case OFPTYPE_QUEUE_STATS_REPLY:
5997 case OFPTYPE_PORT_STATS_REPLY:
5998 case OFPTYPE_TABLE_STATS_REPLY:
5999 case OFPTYPE_AGGREGATE_STATS_REPLY:
6000 case OFPTYPE_PORT_DESC_STATS_REPLY:
6001 case OFPTYPE_ROLE_REPLY:
6002 case OFPTYPE_FLOW_MONITOR_PAUSED:
6003 case OFPTYPE_FLOW_MONITOR_RESUMED:
6004 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
6005 case OFPTYPE_GET_ASYNC_REPLY:
6006 case OFPTYPE_GROUP_STATS_REPLY:
6007 case OFPTYPE_GROUP_DESC_STATS_REPLY:
6008 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
6009 case OFPTYPE_METER_STATS_REPLY:
6010 case OFPTYPE_METER_CONFIG_STATS_REPLY:
6011 case OFPTYPE_METER_FEATURES_STATS_REPLY:
6012 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
6013 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
6014 case OFPTYPE_ROLE_STATUS:
6016 if (ofpmsg_is_stat_request(oh)) {
6017 return OFPERR_OFPBRC_BAD_STAT;
6019 return OFPERR_OFPBRC_BAD_TYPE;
6025 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
6026 OVS_EXCLUDED(ofproto_mutex)
6028 int error = handle_openflow__(ofconn, ofp_msg);
6029 if (error && error != OFPROTO_POSTPONE) {
6030 ofconn_send_error(ofconn, ofp_msg->data, error);
6032 COVERAGE_INC(ofproto_recv_openflow);
6033 return error != OFPROTO_POSTPONE;
6036 /* Asynchronous operations. */
6038 /* Creates and returns a new ofopgroup that is not associated with any
6039 * OpenFlow connection.
6041 * The caller should add operations to the returned group with
6042 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6043 static struct ofopgroup *
6044 ofopgroup_create_unattached(struct ofproto *ofproto)
6045 OVS_REQUIRES(ofproto_mutex)
6047 struct ofopgroup *group = xzalloc(sizeof *group);
6048 group->ofproto = ofproto;
6049 list_init(&group->ofproto_node);
6050 list_init(&group->ops);
6051 list_init(&group->ofconn_node);
6055 /* Creates and returns a new ofopgroup for 'ofproto'.
6057 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
6058 * connection. The 'request' and 'buffer_id' arguments are ignored.
6060 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
6061 * If the ofopgroup eventually fails, then the error reply will include
6062 * 'request'. If the ofopgroup eventually succeeds, then the packet with
6063 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
6065 * The caller should add operations to the returned group with
6066 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6067 static struct ofopgroup *
6068 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
6069 const struct ofp_header *request, uint32_t buffer_id)
6070 OVS_REQUIRES(ofproto_mutex)
6072 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
6074 size_t request_len = ntohs(request->length);
6076 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
6078 ofconn_add_opgroup(ofconn, &group->ofconn_node);
6079 group->ofconn = ofconn;
6080 group->request = xmemdup(request, MIN(request_len, 64));
6081 group->buffer_id = buffer_id;
6086 /* Submits 'group' for processing.
6088 * If 'group' contains no operations (e.g. none were ever added, or all of the
6089 * ones that were added completed synchronously), then it is destroyed
6090 * immediately. Otherwise it is added to the ofproto's list of pending
6093 ofopgroup_submit(struct ofopgroup *group)
6094 OVS_REQUIRES(ofproto_mutex)
6096 if (!group->n_running) {
6097 ofopgroup_complete(group);
6099 list_push_back(&group->ofproto->pending, &group->ofproto_node);
6100 group->ofproto->n_pending++;
6105 ofopgroup_complete(struct ofopgroup *group)
6106 OVS_REQUIRES(ofproto_mutex)
6108 struct ofproto *ofproto = group->ofproto;
6110 struct ofconn *abbrev_ofconn;
6111 ovs_be32 abbrev_xid;
6113 struct ofoperation *op, *next_op;
6116 ovs_assert(!group->n_running);
6119 LIST_FOR_EACH (op, group_node, &group->ops) {
6126 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
6127 LIST_FOR_EACH (op, group_node, &group->ops) {
6128 if (op->type != OFOPERATION_DELETE) {
6129 struct ofpbuf *packet;
6132 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
6135 struct rule_execute *re;
6139 ofproto_rule_ref(op->rule);
6141 re = xmalloc(sizeof *re);
6142 re->rule = op->rule;
6143 re->in_port = in_port;
6144 re->packet = packet;
6146 if (!guarded_list_push_back(&ofproto->rule_executes,
6147 &re->list_node, 1024)) {
6148 ofproto_rule_unref(op->rule);
6149 ofpbuf_delete(re->packet);
6158 if (!error && !list_is_empty(&group->ofconn_node)) {
6159 abbrev_ofconn = group->ofconn;
6160 abbrev_xid = group->request->xid;
6162 abbrev_ofconn = NULL;
6163 abbrev_xid = htonl(0);
6165 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
6166 struct rule *rule = op->rule;
6168 /* We generally want to report the change to active OpenFlow flow
6169 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
6171 - The operation failed.
6173 - The affected rule is not visible to controllers.
6175 - The operation's only effect was to update rule->modified. */
6177 || ofproto_rule_is_hidden(rule)
6178 || (op->type == OFOPERATION_MODIFY
6180 && rule->flow_cookie == op->flow_cookie))) {
6181 /* Check that we can just cast from ofoperation_type to
6182 * nx_flow_update_event. */
6183 enum nx_flow_update_event event_type;
6186 case OFOPERATION_ADD:
6187 case OFOPERATION_REPLACE:
6188 event_type = NXFME_ADDED;
6191 case OFOPERATION_DELETE:
6192 event_type = NXFME_DELETED;
6195 case OFOPERATION_MODIFY:
6196 event_type = NXFME_MODIFIED;
6203 ofmonitor_report(ofproto->connmgr, rule, event_type,
6204 op->reason, abbrev_ofconn, abbrev_xid);
6207 rule->pending = NULL;
6210 case OFOPERATION_ADD:
6214 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
6215 if (vid_mask == VLAN_VID_MASK) {
6216 if (ofproto->vlan_bitmap) {
6217 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
6218 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
6219 bitmap_set1(ofproto->vlan_bitmap, vid);
6220 ofproto->vlans_changed = true;
6223 ofproto->vlans_changed = true;
6227 oftable_remove_rule(rule);
6228 ofproto_rule_unref(rule);
6232 case OFOPERATION_DELETE:
6233 ovs_assert(!op->error);
6234 ofproto_rule_unref(rule);
6238 case OFOPERATION_MODIFY:
6239 case OFOPERATION_REPLACE:
6241 long long int now = time_msec();
6243 ovs_mutex_lock(&rule->mutex);
6244 rule->modified = now;
6245 if (op->type == OFOPERATION_REPLACE) {
6246 rule->created = now;
6248 ovs_mutex_unlock(&rule->mutex);
6250 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
6251 ovs_mutex_lock(&rule->mutex);
6252 rule->idle_timeout = op->idle_timeout;
6253 rule->hard_timeout = op->hard_timeout;
6254 ovs_mutex_unlock(&rule->mutex);
6256 struct rule_actions *old_actions;
6258 ovs_mutex_lock(&rule->mutex);
6259 old_actions = rule->actions;
6260 rule->actions = op->actions;
6261 ovs_mutex_unlock(&rule->mutex);
6264 rule_actions_unref(old_actions);
6266 rule->flags = op->flags;
6274 ofoperation_destroy(op);
6277 ofmonitor_flush(ofproto->connmgr);
6279 if (!list_is_empty(&group->ofproto_node)) {
6280 ovs_assert(ofproto->n_pending > 0);
6281 ofproto->n_pending--;
6282 list_remove(&group->ofproto_node);
6284 if (!list_is_empty(&group->ofconn_node)) {
6285 list_remove(&group->ofconn_node);
6287 ofconn_send_error(group->ofconn, group->request, error);
6289 connmgr_retry(ofproto->connmgr);
6291 free(group->request);
6295 /* Initiates a new operation on 'rule', of the specified 'type', within
6296 * 'group'. Prior to calling, 'rule' must not have any pending operation.
6298 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
6299 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
6301 * Returns the newly created ofoperation (which is also available as
6302 * rule->pending). */
6303 static struct ofoperation *
6304 ofoperation_create(struct ofopgroup *group, struct rule *rule,
6305 enum ofoperation_type type,
6306 enum ofp_flow_removed_reason reason)
6307 OVS_REQUIRES(ofproto_mutex)
6309 struct ofproto *ofproto = group->ofproto;
6310 struct ofoperation *op;
6312 ovs_assert(!rule->pending);
6314 op = rule->pending = xzalloc(sizeof *op);
6316 list_push_back(&group->ops, &op->group_node);
6319 op->reason = reason;
6320 op->flow_cookie = rule->flow_cookie;
6321 ovs_mutex_lock(&rule->mutex);
6322 op->idle_timeout = rule->idle_timeout;
6323 op->hard_timeout = rule->hard_timeout;
6324 ovs_mutex_unlock(&rule->mutex);
6325 op->flags = rule->flags;
6329 if (type == OFOPERATION_DELETE) {
6330 hmap_insert(&ofproto->deletions, &op->hmap_node,
6331 cls_rule_hash(&rule->cr, rule->table_id));
6338 ofoperation_destroy(struct ofoperation *op)
6339 OVS_REQUIRES(ofproto_mutex)
6341 struct ofopgroup *group = op->group;
6344 op->rule->pending = NULL;
6346 if (op->type == OFOPERATION_DELETE) {
6347 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
6349 list_remove(&op->group_node);
6350 rule_actions_unref(op->actions);
6354 /* Indicates that 'op' completed with status 'error', which is either 0 to
6355 * indicate success or an OpenFlow error code on failure.
6357 * If 'error' is 0, indicating success, the operation will be committed
6358 * permanently to the flow table.
6360 * If 'error' is nonzero, then generally the operation will be rolled back:
6362 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
6363 * restores the original rule. The caller must have uninitialized any
6364 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
6365 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
6366 * and 7 for the new rule, calling its ->rule_dealloc() function.
6368 * - If 'op' is a "modify flow" operation, ofproto restores the original
6371 * - 'op' must not be a "delete flow" operation. Removing a rule is not
6372 * allowed to fail. It must always succeed.
6374 * Please see the large comment in ofproto/ofproto-provider.h titled
6375 * "Asynchronous Operation Support" for more information. */
6377 ofoperation_complete(struct ofoperation *op, enum ofperr error)
6379 struct ofopgroup *group = op->group;
6381 ovs_assert(group->n_running > 0);
6382 ovs_assert(!error || op->type != OFOPERATION_DELETE);
6385 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
6386 /* This function can be called from ->rule_construct(), in which case
6387 * ofproto_mutex is held, or it can be called from ->run(), in which
6388 * case ofproto_mutex is not held. But only in the latter case can we
6389 * arrive here, so we can safely take ofproto_mutex now. */
6390 ovs_mutex_lock(&ofproto_mutex);
6391 ovs_assert(op->rule->pending == op);
6392 ofopgroup_complete(group);
6393 ovs_mutex_unlock(&ofproto_mutex);
6398 pick_datapath_id(const struct ofproto *ofproto)
6400 const struct ofport *port;
6402 port = ofproto_get_port(ofproto, OFPP_LOCAL);
6404 uint8_t ea[ETH_ADDR_LEN];
6407 error = netdev_get_etheraddr(port->netdev, ea);
6409 return eth_addr_to_uint64(ea);
6411 VLOG_WARN("%s: could not get MAC address for %s (%s)",
6412 ofproto->name, netdev_get_name(port->netdev),
6413 ovs_strerror(error));
6415 return ofproto->fallback_dpid;
6419 pick_fallback_dpid(void)
6421 uint8_t ea[ETH_ADDR_LEN];
6422 eth_addr_nicira_random(ea);
6423 return eth_addr_to_uint64(ea);
6426 /* Table overflow policy. */
6428 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
6429 * to NULL if the table is not configured to evict rules or if the table
6430 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
6431 * or with no timeouts are not evictable.) */
6433 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
6434 OVS_REQUIRES(ofproto_mutex)
6436 struct eviction_group *evg;
6439 if (!table->eviction_fields) {
6443 /* In the common case, the outer and inner loops here will each be entered
6446 * - The inner loop normally "return"s in its first iteration. If the
6447 * eviction group has any evictable rules, then it always returns in
6450 * - The outer loop only iterates more than once if the largest eviction
6451 * group has no evictable rules.
6453 * - The outer loop can exit only if table's 'max_flows' is all filled up
6454 * by unevictable rules. */
6455 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
6458 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
6467 /* Searches 'ofproto' for tables that have more flows than their configured
6468 * maximum and that have flow eviction enabled, and evicts as many flows as
6469 * necessary and currently feasible from them.
6471 * This triggers only when an OpenFlow table has N flows in it and then the
6472 * client configures a maximum number of flows less than N. */
6474 ofproto_evict(struct ofproto *ofproto)
6476 struct oftable *table;
6478 ovs_mutex_lock(&ofproto_mutex);
6479 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
6480 evict_rules_from_table(ofproto, table, 0);
6482 ovs_mutex_unlock(&ofproto_mutex);
6485 /* Eviction groups. */
6487 /* Returns the priority to use for an eviction_group that contains 'n_rules'
6488 * rules. The priority contains low-order random bits to ensure that eviction
6489 * groups with the same number of rules are prioritized randomly. */
6491 eviction_group_priority(size_t n_rules)
6493 uint16_t size = MIN(UINT16_MAX, n_rules);
6494 return (size << 16) | random_uint16();
6497 /* Updates 'evg', an eviction_group within 'table', following a change that
6498 * adds or removes rules in 'evg'. */
6500 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
6501 OVS_REQUIRES(ofproto_mutex)
6503 heap_change(&table->eviction_groups_by_size, &evg->size_node,
6504 eviction_group_priority(heap_count(&evg->rules)));
6507 /* Destroys 'evg', an eviction_group within 'table':
6509 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
6510 * rules themselves, just removes them from the eviction group.)
6512 * - Removes 'evg' from 'table'.
6516 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
6517 OVS_REQUIRES(ofproto_mutex)
6519 while (!heap_is_empty(&evg->rules)) {
6522 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
6523 rule->eviction_group = NULL;
6525 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
6526 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
6527 heap_destroy(&evg->rules);
6531 /* Removes 'rule' from its eviction group, if any. */
6533 eviction_group_remove_rule(struct rule *rule)
6534 OVS_REQUIRES(ofproto_mutex)
6536 if (rule->eviction_group) {
6537 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6538 struct eviction_group *evg = rule->eviction_group;
6540 rule->eviction_group = NULL;
6541 heap_remove(&evg->rules, &rule->evg_node);
6542 if (heap_is_empty(&evg->rules)) {
6543 eviction_group_destroy(table, evg);
6545 eviction_group_resized(table, evg);
6550 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6551 * returns the hash value. */
6553 eviction_group_hash_rule(struct rule *rule)
6554 OVS_REQUIRES(ofproto_mutex)
6556 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6557 const struct mf_subfield *sf;
6561 hash = table->eviction_group_id_basis;
6562 miniflow_expand(&rule->cr.match.flow, &flow);
6563 for (sf = table->eviction_fields;
6564 sf < &table->eviction_fields[table->n_eviction_fields];
6567 if (mf_are_prereqs_ok(sf->field, &flow)) {
6568 union mf_value value;
6570 mf_get_value(sf->field, &flow, &value);
6572 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
6574 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
6575 unsigned int start = sf->ofs + sf->n_bits;
6576 bitwise_zero(&value, sf->field->n_bytes, start,
6577 sf->field->n_bytes * 8 - start);
6579 hash = hash_bytes(&value, sf->field->n_bytes, hash);
6581 hash = hash_int(hash, 0);
6588 /* Returns an eviction group within 'table' with the given 'id', creating one
6590 static struct eviction_group *
6591 eviction_group_find(struct oftable *table, uint32_t id)
6592 OVS_REQUIRES(ofproto_mutex)
6594 struct eviction_group *evg;
6596 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6600 evg = xmalloc(sizeof *evg);
6601 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6602 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6603 eviction_group_priority(0));
6604 heap_init(&evg->rules);
6609 /* Returns an eviction priority for 'rule'. The return value should be
6610 * interpreted so that higher priorities make a rule more attractive candidates
6612 * Called only if have a timeout. */
6614 rule_eviction_priority(struct ofproto *ofproto, struct rule *rule)
6615 OVS_REQUIRES(ofproto_mutex)
6617 long long int expiration = LLONG_MAX;
6618 long long int modified;
6619 uint32_t expiration_offset;
6621 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
6622 ovs_mutex_lock(&rule->mutex);
6623 modified = rule->modified;
6624 ovs_mutex_unlock(&rule->mutex);
6626 if (rule->hard_timeout) {
6627 expiration = modified + rule->hard_timeout * 1000;
6629 if (rule->idle_timeout) {
6630 uint64_t packets, bytes;
6632 long long int idle_expiration;
6634 ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used);
6635 idle_expiration = used + rule->idle_timeout * 1000;
6636 expiration = MIN(expiration, idle_expiration);
6639 if (expiration == LLONG_MAX) {
6643 /* Calculate the time of expiration as a number of (approximate) seconds
6644 * after program startup.
6646 * This should work OK for program runs that last UINT32_MAX seconds or
6647 * less. Therefore, please restart OVS at least once every 136 years. */
6648 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6650 /* Invert the expiration offset because we're using a max-heap. */
6651 return UINT32_MAX - expiration_offset;
6654 /* Adds 'rule' to an appropriate eviction group for its oftable's
6655 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6656 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6659 * The caller must ensure that 'rule' is not already in an eviction group. */
6661 eviction_group_add_rule(struct rule *rule)
6662 OVS_REQUIRES(ofproto_mutex)
6664 struct ofproto *ofproto = rule->ofproto;
6665 struct oftable *table = &ofproto->tables[rule->table_id];
6668 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
6669 * so no additional protection is needed. */
6670 has_timeout = rule->hard_timeout || rule->idle_timeout;
6672 if (table->eviction_fields && has_timeout) {
6673 struct eviction_group *evg;
6675 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6677 rule->eviction_group = evg;
6678 heap_insert(&evg->rules, &rule->evg_node,
6679 rule_eviction_priority(ofproto, rule));
6680 eviction_group_resized(table, evg);
6686 /* Initializes 'table'. */
6688 oftable_init(struct oftable *table)
6690 memset(table, 0, sizeof *table);
6691 classifier_init(&table->cls, flow_segment_u32s);
6692 table->max_flows = UINT_MAX;
6693 atomic_init(&table->config, (unsigned int)OFPTC11_TABLE_MISS_CONTROLLER);
6696 /* Destroys 'table', including its classifier and eviction groups.
6698 * The caller is responsible for freeing 'table' itself. */
6700 oftable_destroy(struct oftable *table)
6702 fat_rwlock_rdlock(&table->cls.rwlock);
6703 ovs_assert(classifier_is_empty(&table->cls));
6704 fat_rwlock_unlock(&table->cls.rwlock);
6705 oftable_disable_eviction(table);
6706 classifier_destroy(&table->cls);
6708 atomic_destroy(&table->config);
6711 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6712 * string, then 'table' will use its default name.
6714 * This only affects the name exposed for a table exposed through the OpenFlow
6715 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6717 oftable_set_name(struct oftable *table, const char *name)
6719 if (name && name[0]) {
6720 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6721 if (!table->name || strncmp(name, table->name, len)) {
6723 table->name = xmemdup0(name, len);
6731 /* oftables support a choice of two policies when adding a rule would cause the
6732 * number of flows in the table to exceed the configured maximum number: either
6733 * they can refuse to add the new flow or they can evict some existing flow.
6734 * This function configures the former policy on 'table'. */
6736 oftable_disable_eviction(struct oftable *table)
6737 OVS_REQUIRES(ofproto_mutex)
6739 if (table->eviction_fields) {
6740 struct eviction_group *evg, *next;
6742 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6743 &table->eviction_groups_by_id) {
6744 eviction_group_destroy(table, evg);
6746 hmap_destroy(&table->eviction_groups_by_id);
6747 heap_destroy(&table->eviction_groups_by_size);
6749 free(table->eviction_fields);
6750 table->eviction_fields = NULL;
6751 table->n_eviction_fields = 0;
6755 /* oftables support a choice of two policies when adding a rule would cause the
6756 * number of flows in the table to exceed the configured maximum number: either
6757 * they can refuse to add the new flow or they can evict some existing flow.
6758 * This function configures the latter policy on 'table', with fairness based
6759 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6760 * 'n_fields' as 0 disables fairness.) */
6762 oftable_enable_eviction(struct oftable *table,
6763 const struct mf_subfield *fields, size_t n_fields)
6764 OVS_REQUIRES(ofproto_mutex)
6766 struct cls_cursor cursor;
6769 if (table->eviction_fields
6770 && n_fields == table->n_eviction_fields
6772 || !memcmp(fields, table->eviction_fields,
6773 n_fields * sizeof *fields))) {
6778 oftable_disable_eviction(table);
6780 table->n_eviction_fields = n_fields;
6781 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6783 table->eviction_group_id_basis = random_uint32();
6784 hmap_init(&table->eviction_groups_by_id);
6785 heap_init(&table->eviction_groups_by_size);
6787 fat_rwlock_rdlock(&table->cls.rwlock);
6788 cls_cursor_init(&cursor, &table->cls, NULL);
6789 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6790 eviction_group_add_rule(rule);
6792 fat_rwlock_unlock(&table->cls.rwlock);
6795 /* Removes 'rule' from the oftable that contains it. */
6797 oftable_remove_rule__(struct ofproto *ofproto, struct rule *rule)
6798 OVS_REQUIRES(ofproto_mutex)
6800 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
6802 fat_rwlock_wrlock(&cls->rwlock);
6803 classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
6804 fat_rwlock_unlock(&cls->rwlock);
6806 cookies_remove(ofproto, rule);
6808 eviction_group_remove_rule(rule);
6809 if (!list_is_empty(&rule->expirable)) {
6810 list_remove(&rule->expirable);
6812 if (!list_is_empty(&rule->meter_list_node)) {
6813 list_remove(&rule->meter_list_node);
6814 list_init(&rule->meter_list_node);
6819 oftable_remove_rule(struct rule *rule)
6820 OVS_REQUIRES(ofproto_mutex)
6822 oftable_remove_rule__(rule->ofproto, rule);
6825 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6826 * the same cls_rule. */
6828 oftable_insert_rule(struct rule *rule)
6829 OVS_REQUIRES(ofproto_mutex)
6831 struct ofproto *ofproto = rule->ofproto;
6832 struct oftable *table = &ofproto->tables[rule->table_id];
6835 ovs_mutex_lock(&rule->mutex);
6836 may_expire = rule->hard_timeout || rule->idle_timeout;
6837 ovs_mutex_unlock(&rule->mutex);
6840 list_insert(&ofproto->expirable, &rule->expirable);
6843 cookies_insert(ofproto, rule);
6845 if (rule->actions->provider_meter_id != UINT32_MAX) {
6846 uint32_t meter_id = ofpacts_get_meter(rule->actions->ofpacts,
6847 rule->actions->ofpacts_len);
6848 struct meter *meter = ofproto->meters[meter_id];
6849 list_insert(&meter->rules, &rule->meter_list_node);
6851 fat_rwlock_wrlock(&table->cls.rwlock);
6852 classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
6853 fat_rwlock_unlock(&table->cls.rwlock);
6854 eviction_group_add_rule(rule);
6857 /* unixctl commands. */
6860 ofproto_lookup(const char *name)
6862 struct ofproto *ofproto;
6864 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6866 if (!strcmp(ofproto->name, name)) {
6874 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6875 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6877 struct ofproto *ofproto;
6881 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6882 ds_put_format(&results, "%s\n", ofproto->name);
6884 unixctl_command_reply(conn, ds_cstr(&results));
6885 ds_destroy(&results);
6889 ofproto_unixctl_init(void)
6891 static bool registered;
6897 unixctl_command_register("ofproto/list", "", 0, 0,
6898 ofproto_unixctl_list, NULL);
6901 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6903 * This is deprecated. It is only for compatibility with broken device drivers
6904 * in old versions of Linux that do not properly support VLANs when VLAN
6905 * devices are not used. When broken device drivers are no longer in
6906 * widespread use, we will delete these interfaces. */
6908 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6909 * (exactly) by an OpenFlow rule in 'ofproto'. */
6911 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6913 const struct oftable *oftable;
6915 free(ofproto->vlan_bitmap);
6916 ofproto->vlan_bitmap = bitmap_allocate(4096);
6917 ofproto->vlans_changed = false;
6919 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6920 const struct cls_subtable *table;
6922 fat_rwlock_rdlock(&oftable->cls.rwlock);
6923 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.subtables) {
6924 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6925 const struct cls_rule *rule;
6927 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6928 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6929 bitmap_set1(vlan_bitmap, vid);
6930 bitmap_set1(ofproto->vlan_bitmap, vid);
6934 fat_rwlock_unlock(&oftable->cls.rwlock);
6938 /* Returns true if new VLANs have come into use by the flow table since the
6939 * last call to ofproto_get_vlan_usage().
6941 * We don't track when old VLANs stop being used. */
6943 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6945 return ofproto->vlans_changed;
6948 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6949 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6950 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6951 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6952 * then the VLAN device is un-enslaved. */
6954 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6955 ofp_port_t realdev_ofp_port, int vid)
6957 struct ofport *ofport;
6960 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6962 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6964 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6965 ofproto->name, vlandev_ofp_port);
6969 if (!ofproto->ofproto_class->set_realdev) {
6970 if (!vlandev_ofp_port) {
6973 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6977 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6979 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6980 ofproto->name, vlandev_ofp_port,
6981 netdev_get_name(ofport->netdev), ovs_strerror(error));