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 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1455 heap_rebuild(&evg->rules);
1458 fat_rwlock_rdlock(&table->cls.rwlock);
1459 cls_cursor_init(&cursor, &table->cls, NULL);
1460 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1461 if (!rule->eviction_group
1462 && (rule->idle_timeout || rule->hard_timeout)) {
1463 eviction_group_add_rule(rule);
1466 fat_rwlock_unlock(&table->cls.rwlock);
1467 ovs_mutex_unlock(&ofproto_mutex);
1471 if (p->ofproto_class->port_poll) {
1474 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1475 process_port_change(p, error, devname);
1479 new_seq = seq_read(connectivity_seq_get());
1480 if (new_seq != p->change_seq) {
1481 struct sset devnames;
1482 const char *devname;
1483 struct ofport *ofport;
1485 /* Update OpenFlow port status for any port whose netdev has changed.
1487 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1488 * destroyed, so it's not safe to update ports directly from the
1489 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1490 * need this two-phase approach. */
1491 sset_init(&devnames);
1492 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1493 sset_add(&devnames, netdev_get_name(ofport->netdev));
1495 SSET_FOR_EACH (devname, &devnames) {
1496 update_port(p, devname);
1498 sset_destroy(&devnames);
1500 p->change_seq = new_seq;
1505 connmgr_run(p->connmgr, handle_openflow);
1509 connmgr_run(p->connmgr, NULL);
1511 if (!any_pending_ops(p)) {
1512 p->state = S_OPENFLOW;
1517 connmgr_run(p->connmgr, NULL);
1519 if (!any_pending_ops(p)) {
1520 connmgr_flushed(p->connmgr);
1521 p->state = S_OPENFLOW;
1529 if (time_msec() >= p->next_op_report) {
1530 long long int ago = (time_msec() - p->first_op) / 1000;
1531 long long int interval = (p->last_op - p->first_op) / 1000;
1535 ds_put_format(&s, "%d flow_mods ",
1536 p->n_add + p->n_delete + p->n_modify);
1537 if (interval == ago) {
1538 ds_put_format(&s, "in the last %lld s", ago);
1539 } else if (interval) {
1540 ds_put_format(&s, "in the %lld s starting %lld s ago",
1543 ds_put_format(&s, "%lld s ago", ago);
1546 ds_put_cstr(&s, " (");
1548 ds_put_format(&s, "%d adds, ", p->n_add);
1551 ds_put_format(&s, "%d deletes, ", p->n_delete);
1554 ds_put_format(&s, "%d modifications, ", p->n_modify);
1557 ds_put_char(&s, ')');
1559 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1562 p->n_add = p->n_delete = p->n_modify = 0;
1563 p->next_op_report = LLONG_MAX;
1570 ofproto_wait(struct ofproto *p)
1572 p->ofproto_class->wait(p);
1573 if (p->ofproto_class->port_poll_wait) {
1574 p->ofproto_class->port_poll_wait(p);
1576 seq_wait(connectivity_seq_get(), p->change_seq);
1580 connmgr_wait(p->connmgr, true);
1585 connmgr_wait(p->connmgr, false);
1586 if (!any_pending_ops(p)) {
1587 poll_immediate_wake();
1594 ofproto_is_alive(const struct ofproto *p)
1596 return connmgr_has_controllers(p->connmgr);
1599 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1600 * memory_report(). */
1602 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1604 const struct oftable *table;
1605 unsigned int n_rules;
1607 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1609 ovs_mutex_lock(&ofproto_mutex);
1610 simap_increase(usage, "ops",
1611 ofproto->n_pending + hmap_count(&ofproto->deletions));
1612 ovs_mutex_unlock(&ofproto_mutex);
1615 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1616 fat_rwlock_rdlock(&table->cls.rwlock);
1617 n_rules += classifier_count(&table->cls);
1618 fat_rwlock_unlock(&table->cls.rwlock);
1620 simap_increase(usage, "rules", n_rules);
1622 if (ofproto->ofproto_class->get_memory_usage) {
1623 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1626 connmgr_get_memory_usage(ofproto->connmgr, usage);
1630 ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage)
1632 const struct ofproto_class *class;
1634 datapath_type = ofproto_normalize_type(datapath_type);
1635 class = ofproto_class_find__(datapath_type);
1637 if (class && class->type_get_memory_usage) {
1638 class->type_get_memory_usage(datapath_type, usage);
1643 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1646 connmgr_get_controller_info(ofproto->connmgr, info);
1650 ofproto_free_ofproto_controller_info(struct shash *info)
1652 connmgr_free_controller_info(info);
1655 /* Makes a deep copy of 'old' into 'port'. */
1657 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1659 port->name = xstrdup(old->name);
1660 port->type = xstrdup(old->type);
1661 port->ofp_port = old->ofp_port;
1664 /* Frees memory allocated to members of 'ofproto_port'.
1666 * Do not call this function on an ofproto_port obtained from
1667 * ofproto_port_dump_next(): that function retains ownership of the data in the
1670 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1672 free(ofproto_port->name);
1673 free(ofproto_port->type);
1676 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1678 * This function provides no status indication. An error status for the entire
1679 * dump operation is provided when it is completed by calling
1680 * ofproto_port_dump_done().
1683 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1684 const struct ofproto *ofproto)
1686 dump->ofproto = ofproto;
1687 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1691 /* Attempts to retrieve another port from 'dump', which must have been created
1692 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1693 * 'port' and returns true. On failure, returns false.
1695 * Failure might indicate an actual error or merely that the last port has been
1696 * dumped. An error status for the entire dump operation is provided when it
1697 * is completed by calling ofproto_port_dump_done().
1699 * The ofproto owns the data stored in 'port'. It will remain valid until at
1700 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1701 * ofproto_port_dump_done(). */
1703 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1704 struct ofproto_port *port)
1706 const struct ofproto *ofproto = dump->ofproto;
1712 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1715 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1721 /* Completes port table dump operation 'dump', which must have been created
1722 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1723 * error-free, otherwise a positive errno value describing the problem. */
1725 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1727 const struct ofproto *ofproto = dump->ofproto;
1729 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1732 return dump->error == EOF ? 0 : dump->error;
1735 /* Returns the type to pass to netdev_open() when a datapath of type
1736 * 'datapath_type' has a port of type 'port_type', for a few special
1737 * cases when a netdev type differs from a port type. For example, when
1738 * using the userspace datapath, a port of type "internal" needs to be
1741 * Returns either 'type' itself or a string literal, which must not be
1744 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1746 const struct ofproto_class *class;
1748 datapath_type = ofproto_normalize_type(datapath_type);
1749 class = ofproto_class_find__(datapath_type);
1754 return (class->port_open_type
1755 ? class->port_open_type(datapath_type, port_type)
1759 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1760 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1761 * the port's OpenFlow port number.
1763 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1764 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1765 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1766 * 'ofp_portp' is non-null). */
1768 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1769 ofp_port_t *ofp_portp)
1771 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1774 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1776 const char *netdev_name = netdev_get_name(netdev);
1778 simap_put(&ofproto->ofp_requests, netdev_name,
1779 ofp_to_u16(ofp_port));
1780 update_port(ofproto, netdev_name);
1783 *ofp_portp = OFPP_NONE;
1785 struct ofproto_port ofproto_port;
1787 error = ofproto_port_query_by_name(ofproto,
1788 netdev_get_name(netdev),
1791 *ofp_portp = ofproto_port.ofp_port;
1792 ofproto_port_destroy(&ofproto_port);
1799 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1800 * initializes '*port' appropriately; on failure, returns a positive errno
1803 * The caller owns the data in 'ofproto_port' and must free it with
1804 * ofproto_port_destroy() when it is no longer needed. */
1806 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1807 struct ofproto_port *port)
1811 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1813 memset(port, 0, sizeof *port);
1818 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1819 * Returns 0 if successful, otherwise a positive errno. */
1821 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1823 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1824 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1825 struct simap_node *ofp_request_node;
1828 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1829 if (ofp_request_node) {
1830 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1833 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1834 if (!error && ofport) {
1835 /* 'name' is the netdev's name and update_port() is going to close the
1836 * netdev. Just in case update_port() refers to 'name' after it
1837 * destroys 'ofport', make a copy of it around the update_port()
1839 char *devname = xstrdup(name);
1840 update_port(ofproto, devname);
1847 flow_mod_init(struct ofputil_flow_mod *fm,
1848 const struct match *match, unsigned int priority,
1849 const struct ofpact *ofpacts, size_t ofpacts_len,
1850 enum ofp_flow_mod_command command)
1852 memset(fm, 0, sizeof *fm);
1854 fm->priority = priority;
1857 fm->modify_cookie = false;
1859 fm->command = command;
1860 fm->idle_timeout = 0;
1861 fm->hard_timeout = 0;
1862 fm->buffer_id = UINT32_MAX;
1863 fm->out_port = OFPP_ANY;
1864 fm->out_group = OFPG_ANY;
1866 fm->ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1867 fm->ofpacts_len = ofpacts_len;
1871 simple_flow_mod(struct ofproto *ofproto,
1872 const struct match *match, unsigned int priority,
1873 const struct ofpact *ofpacts, size_t ofpacts_len,
1874 enum ofp_flow_mod_command command)
1876 struct ofputil_flow_mod fm;
1878 flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command);
1880 return handle_flow_mod__(ofproto, NULL, &fm, NULL);
1883 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1884 * performs the 'n_actions' actions in 'actions'. The new flow will not
1887 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1888 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1889 * controllers; otherwise, it will be hidden.
1891 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1893 * This is a helper function for in-band control and fail-open. */
1895 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1896 unsigned int priority,
1897 const struct ofpact *ofpacts, size_t ofpacts_len)
1898 OVS_EXCLUDED(ofproto_mutex)
1900 const struct rule *rule;
1903 /* First do a cheap check whether the rule we're looking for already exists
1904 * with the actions that we want. If it does, then we're done. */
1905 fat_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1906 rule = rule_from_cls_rule(classifier_find_match_exactly(
1907 &ofproto->tables[0].cls, match, priority));
1909 ovs_mutex_lock(&rule->mutex);
1910 must_add = !ofpacts_equal(rule->actions->ofpacts,
1911 rule->actions->ofpacts_len,
1912 ofpacts, ofpacts_len);
1913 ovs_mutex_unlock(&rule->mutex);
1917 fat_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1919 /* If there's no such rule or the rule doesn't have the actions we want,
1920 * fall back to a executing a full flow mod. We can't optimize this at
1921 * all because we didn't take enough locks above to ensure that the flow
1922 * table didn't already change beneath us. */
1924 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1925 OFPFC_MODIFY_STRICT);
1929 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1930 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1931 * operation cannot be initiated now but may be retried later.
1933 * This is a helper function for in-band control and fail-open and the "learn"
1936 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1937 OVS_EXCLUDED(ofproto_mutex)
1939 /* Optimize for the most common case of a repeated learn action.
1940 * If an identical flow already exists we only need to update its
1941 * 'modified' time. */
1942 if (fm->command == OFPFC_MODIFY_STRICT && fm->table_id != OFPTT_ALL
1943 && !(fm->flags & OFPUTIL_FF_RESET_COUNTS)) {
1944 struct oftable *table = &ofproto->tables[fm->table_id];
1945 struct cls_rule match_rule;
1949 cls_rule_init(&match_rule, &fm->match, fm->priority);
1950 fat_rwlock_rdlock(&table->cls.rwlock);
1951 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls,
1954 /* Reading many of the rule fields and writing on 'modified'
1955 * requires the rule->mutex. Also, rule->actions may change
1956 * if rule->mutex is not held. */
1957 ovs_mutex_lock(&rule->mutex);
1958 if (rule->idle_timeout == fm->idle_timeout
1959 && rule->hard_timeout == fm->hard_timeout
1960 && rule->flags == (fm->flags & OFPUTIL_FF_STATE)
1961 && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie))
1962 && ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
1963 rule->actions->ofpacts,
1964 rule->actions->ofpacts_len)) {
1965 /* Rule already exists and need not change, only update the
1966 modified timestamp. */
1967 rule->modified = time_msec();
1970 ovs_mutex_unlock(&rule->mutex);
1972 fat_rwlock_unlock(&table->cls.rwlock);
1979 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1982 /* Searches for a rule with matching criteria exactly equal to 'target' in
1983 * ofproto's table 0 and, if it finds one, deletes it.
1985 * This is a helper function for in-band control and fail-open. */
1987 ofproto_delete_flow(struct ofproto *ofproto,
1988 const struct match *target, unsigned int priority)
1989 OVS_EXCLUDED(ofproto_mutex)
1991 struct classifier *cls = &ofproto->tables[0].cls;
1994 /* First do a cheap check whether the rule we're looking for has already
1995 * been deleted. If so, then we're done. */
1996 fat_rwlock_rdlock(&cls->rwlock);
1997 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1999 fat_rwlock_unlock(&cls->rwlock);
2004 /* Fall back to a executing a full flow mod. We can't optimize this at all
2005 * because we didn't take enough locks above to ensure that the flow table
2006 * didn't already change beneath us. */
2007 return simple_flow_mod(ofproto, target, priority, NULL, 0,
2008 OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE;
2011 /* Starts the process of deleting all of the flows from all of ofproto's flow
2012 * tables and then reintroducing the flows required by in-band control and
2013 * fail-open. The process will complete in a later call to ofproto_run(). */
2015 ofproto_flush_flows(struct ofproto *ofproto)
2017 COVERAGE_INC(ofproto_flush);
2018 ofproto->state = S_FLUSH;
2022 reinit_ports(struct ofproto *p)
2024 struct ofproto_port_dump dump;
2025 struct sset devnames;
2026 struct ofport *ofport;
2027 struct ofproto_port ofproto_port;
2028 const char *devname;
2030 COVERAGE_INC(ofproto_reinit_ports);
2032 sset_init(&devnames);
2033 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2034 sset_add(&devnames, netdev_get_name(ofport->netdev));
2036 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2037 sset_add(&devnames, ofproto_port.name);
2040 SSET_FOR_EACH (devname, &devnames) {
2041 update_port(p, devname);
2043 sset_destroy(&devnames);
2047 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
2051 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
2052 port_idx = port_idx ? port_idx : UINT16_MAX;
2054 if (port_idx >= ofproto->max_ports
2055 || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) {
2056 uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no;
2057 long long int last_used_at, lru = LLONG_MAX;
2059 /* Search for a free OpenFlow port number. We try not to
2060 * immediately reuse them to prevent problems due to old
2063 * We limit the automatically assigned port numbers to the lower half
2064 * of the port range, to reserve the upper half for assignment by
2067 if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) {
2068 ofproto->alloc_port_no = 1;
2070 last_used_at = ofport_get_usage(ofproto,
2071 u16_to_ofp(ofproto->alloc_port_no));
2072 if (!last_used_at) {
2073 port_idx = ofproto->alloc_port_no;
2075 } else if ( last_used_at < time_msec() - 60*60*1000) {
2076 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2077 * more than an hour ago, consider it usable. */
2078 ofport_remove_usage(ofproto,
2079 u16_to_ofp(ofproto->alloc_port_no));
2080 port_idx = ofproto->alloc_port_no;
2082 } else if (last_used_at < lru) {
2084 lru_ofport = ofproto->alloc_port_no;
2087 if (ofproto->alloc_port_no == end_port_no) {
2089 port_idx = lru_ofport;
2096 ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX);
2097 return u16_to_ofp(port_idx);
2101 dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port)
2103 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
2104 ofport_set_usage(ofproto, ofp_port, time_msec());
2108 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2109 * pointer if the netdev cannot be opened. On success, also fills in
2111 static struct netdev *
2112 ofport_open(struct ofproto *ofproto,
2113 struct ofproto_port *ofproto_port,
2114 struct ofputil_phy_port *pp)
2116 enum netdev_flags flags;
2117 struct netdev *netdev;
2120 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
2122 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
2123 "cannot be opened (%s)",
2125 ofproto_port->name, ofproto_port->ofp_port,
2126 ofproto_port->name, ovs_strerror(error));
2130 if (ofproto_port->ofp_port == OFPP_NONE) {
2131 if (!strcmp(ofproto->name, ofproto_port->name)) {
2132 ofproto_port->ofp_port = OFPP_LOCAL;
2134 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
2135 ofproto_port->name);
2138 pp->port_no = ofproto_port->ofp_port;
2139 netdev_get_etheraddr(netdev, pp->hw_addr);
2140 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
2141 netdev_get_flags(netdev, &flags);
2142 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
2143 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
2144 netdev_get_features(netdev, &pp->curr, &pp->advertised,
2145 &pp->supported, &pp->peer);
2146 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2147 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2152 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2153 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
2156 ofport_equal(const struct ofputil_phy_port *a,
2157 const struct ofputil_phy_port *b)
2159 return (eth_addr_equals(a->hw_addr, b->hw_addr)
2160 && a->state == b->state
2161 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
2162 && a->curr == b->curr
2163 && a->advertised == b->advertised
2164 && a->supported == b->supported
2165 && a->peer == b->peer
2166 && a->curr_speed == b->curr_speed
2167 && a->max_speed == b->max_speed);
2170 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2171 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2172 * one with the same name or port number). */
2174 ofport_install(struct ofproto *p,
2175 struct netdev *netdev, const struct ofputil_phy_port *pp)
2177 const char *netdev_name = netdev_get_name(netdev);
2178 struct ofport *ofport;
2181 /* Create ofport. */
2182 ofport = p->ofproto_class->port_alloc();
2187 ofport->ofproto = p;
2188 ofport->netdev = netdev;
2190 ofport->ofp_port = pp->port_no;
2191 ofport->created = time_msec();
2193 /* Add port to 'p'. */
2194 hmap_insert(&p->ports, &ofport->hmap_node,
2195 hash_ofp_port(ofport->ofp_port));
2196 shash_add(&p->port_by_name, netdev_name, ofport);
2198 update_mtu(p, ofport);
2200 /* Let the ofproto_class initialize its private data. */
2201 error = p->ofproto_class->port_construct(ofport);
2205 connmgr_send_port_status(p->connmgr, NULL, pp, OFPPR_ADD);
2209 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
2210 p->name, netdev_name, ovs_strerror(error));
2212 ofport_destroy__(ofport);
2214 netdev_close(netdev);
2218 /* Removes 'ofport' from 'p' and destroys it. */
2220 ofport_remove(struct ofport *ofport)
2222 connmgr_send_port_status(ofport->ofproto->connmgr, NULL, &ofport->pp,
2224 ofport_destroy(ofport);
2227 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2230 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2232 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2234 ofport_remove(port);
2238 /* Updates 'port' with new 'pp' description.
2240 * Does not handle a name or port number change. The caller must implement
2241 * such a change as a delete followed by an add. */
2243 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
2245 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2246 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2247 | (pp->config & OFPUTIL_PC_PORT_DOWN));
2248 port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN)
2249 | (pp->state & OFPUTIL_PS_LINK_DOWN));
2250 port->pp.curr = pp->curr;
2251 port->pp.advertised = pp->advertised;
2252 port->pp.supported = pp->supported;
2253 port->pp.peer = pp->peer;
2254 port->pp.curr_speed = pp->curr_speed;
2255 port->pp.max_speed = pp->max_speed;
2257 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2258 &port->pp, OFPPR_MODIFY);
2261 /* Update OpenFlow 'state' in 'port' and notify controller. */
2263 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2265 if (port->pp.state != state) {
2266 port->pp.state = state;
2267 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2268 &port->pp, OFPPR_MODIFY);
2273 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2275 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2277 if (port->ofproto->ofproto_class->set_realdev) {
2278 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2280 if (port->ofproto->ofproto_class->set_stp_port) {
2281 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2283 if (port->ofproto->ofproto_class->set_cfm) {
2284 port->ofproto->ofproto_class->set_cfm(port, NULL);
2286 if (port->ofproto->ofproto_class->bundle_remove) {
2287 port->ofproto->ofproto_class->bundle_remove(port);
2293 ofport_destroy__(struct ofport *port)
2295 struct ofproto *ofproto = port->ofproto;
2296 const char *name = netdev_get_name(port->netdev);
2298 hmap_remove(&ofproto->ports, &port->hmap_node);
2299 shash_delete(&ofproto->port_by_name,
2300 shash_find(&ofproto->port_by_name, name));
2302 netdev_close(port->netdev);
2303 ofproto->ofproto_class->port_dealloc(port);
2307 ofport_destroy(struct ofport *port)
2310 dealloc_ofp_port(port->ofproto, port->ofp_port);
2311 port->ofproto->ofproto_class->port_destruct(port);
2312 ofport_destroy__(port);
2317 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2319 struct ofport *port;
2321 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2323 if (port->ofp_port == ofp_port) {
2330 static long long int
2331 ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port)
2333 struct ofport_usage *usage;
2335 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2336 &ofproto->ofport_usage) {
2337 if (usage->ofp_port == ofp_port) {
2338 return usage->last_used;
2345 ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port,
2346 long long int last_used)
2348 struct ofport_usage *usage;
2349 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2350 &ofproto->ofport_usage) {
2351 if (usage->ofp_port == ofp_port) {
2352 usage->last_used = last_used;
2356 ovs_assert(last_used == LLONG_MAX);
2358 usage = xmalloc(sizeof *usage);
2359 usage->ofp_port = ofp_port;
2360 usage->last_used = last_used;
2361 hmap_insert(&ofproto->ofport_usage, &usage->hmap_node,
2362 hash_ofp_port(ofp_port));
2366 ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port)
2368 struct ofport_usage *usage;
2369 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2370 &ofproto->ofport_usage) {
2371 if (usage->ofp_port == ofp_port) {
2372 hmap_remove(&ofproto->ofport_usage, &usage->hmap_node);
2380 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2382 struct ofproto *ofproto = port->ofproto;
2385 if (ofproto->ofproto_class->port_get_stats) {
2386 error = ofproto->ofproto_class->port_get_stats(port, stats);
2395 update_port(struct ofproto *ofproto, const char *name)
2397 struct ofproto_port ofproto_port;
2398 struct ofputil_phy_port pp;
2399 struct netdev *netdev;
2400 struct ofport *port;
2402 COVERAGE_INC(ofproto_update_port);
2404 /* Fetch 'name''s location and properties from the datapath. */
2405 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2406 ? ofport_open(ofproto, &ofproto_port, &pp)
2410 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2411 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2412 struct netdev *old_netdev = port->netdev;
2414 /* 'name' hasn't changed location. Any properties changed? */
2415 if (!ofport_equal(&port->pp, &pp)) {
2416 ofport_modified(port, &pp);
2419 update_mtu(ofproto, port);
2421 /* Install the newly opened netdev in case it has changed.
2422 * Don't close the old netdev yet in case port_modified has to
2423 * remove a retained reference to it.*/
2424 port->netdev = netdev;
2426 if (port->ofproto->ofproto_class->port_modified) {
2427 port->ofproto->ofproto_class->port_modified(port);
2430 netdev_close(old_netdev);
2432 /* If 'port' is nonnull then its name differs from 'name' and thus
2433 * we should delete it. If we think there's a port named 'name'
2434 * then its port number must be wrong now so delete it too. */
2436 ofport_remove(port);
2438 ofport_remove_with_name(ofproto, name);
2439 ofport_install(ofproto, netdev, &pp);
2442 /* Any port named 'name' is gone now. */
2443 ofport_remove_with_name(ofproto, name);
2445 ofproto_port_destroy(&ofproto_port);
2449 init_ports(struct ofproto *p)
2451 struct ofproto_port_dump dump;
2452 struct ofproto_port ofproto_port;
2453 struct shash_node *node, *next;
2455 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2456 const char *name = ofproto_port.name;
2458 if (shash_find(&p->port_by_name, name)) {
2459 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2462 struct ofputil_phy_port pp;
2463 struct netdev *netdev;
2465 /* Check if an OpenFlow port number had been requested. */
2466 node = shash_find(&init_ofp_ports, name);
2468 const struct iface_hint *iface_hint = node->data;
2469 simap_put(&p->ofp_requests, name,
2470 ofp_to_u16(iface_hint->ofp_port));
2473 netdev = ofport_open(p, &ofproto_port, &pp);
2475 ofport_install(p, netdev, &pp);
2476 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2477 p->alloc_port_no = MAX(p->alloc_port_no,
2478 ofp_to_u16(ofproto_port.ofp_port));
2484 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2485 struct iface_hint *iface_hint = node->data;
2487 if (!strcmp(iface_hint->br_name, p->name)) {
2488 free(iface_hint->br_name);
2489 free(iface_hint->br_type);
2491 shash_delete(&init_ofp_ports, node);
2498 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2499 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2501 find_min_mtu(struct ofproto *p)
2503 struct ofport *ofport;
2506 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2507 struct netdev *netdev = ofport->netdev;
2510 /* Skip any internal ports, since that's what we're trying to
2512 if (!strcmp(netdev_get_type(netdev), "internal")) {
2516 if (netdev_get_mtu(netdev, &dev_mtu)) {
2519 if (!mtu || dev_mtu < mtu) {
2524 return mtu ? mtu: ETH_PAYLOAD_MAX;
2527 /* Update MTU of all datapath devices on 'p' to the minimum of the
2528 * non-datapath ports in event of 'port' added or changed. */
2530 update_mtu(struct ofproto *p, struct ofport *port)
2532 struct ofport *ofport;
2533 struct netdev *netdev = port->netdev;
2534 int dev_mtu, old_min;
2536 if (netdev_get_mtu(netdev, &dev_mtu)) {
2540 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2541 if (dev_mtu > p->min_mtu) {
2542 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2543 dev_mtu = p->min_mtu;
2546 port->mtu = dev_mtu;
2550 /* For non-internal port find new min mtu. */
2551 old_min = p->min_mtu;
2552 port->mtu = dev_mtu;
2553 p->min_mtu = find_min_mtu(p);
2554 if (p->min_mtu == old_min) {
2558 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2559 struct netdev *netdev = ofport->netdev;
2561 if (!strcmp(netdev_get_type(netdev), "internal")) {
2562 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2563 ofport->mtu = p->min_mtu;
2570 ofproto_rule_ref(struct rule *rule)
2573 ovs_refcount_ref(&rule->ref_count);
2578 ofproto_rule_unref(struct rule *rule)
2580 if (rule && ovs_refcount_unref(&rule->ref_count) == 1) {
2581 rule->ofproto->ofproto_class->rule_destruct(rule);
2582 ofproto_rule_destroy__(rule);
2586 struct rule_actions *
2587 rule_get_actions(const struct rule *rule)
2588 OVS_EXCLUDED(rule->mutex)
2590 struct rule_actions *actions;
2592 ovs_mutex_lock(&rule->mutex);
2593 actions = rule_get_actions__(rule);
2594 ovs_mutex_unlock(&rule->mutex);
2599 struct rule_actions *
2600 rule_get_actions__(const struct rule *rule)
2601 OVS_REQUIRES(rule->mutex)
2603 rule_actions_ref(rule->actions);
2604 return rule->actions;
2608 ofproto_rule_destroy__(struct rule *rule)
2609 OVS_NO_THREAD_SAFETY_ANALYSIS
2611 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2612 rule_actions_unref(rule->actions);
2613 ovs_mutex_destroy(&rule->mutex);
2614 ovs_refcount_destroy(&rule->ref_count);
2615 rule->ofproto->ofproto_class->rule_dealloc(rule);
2618 static uint32_t get_provider_meter_id(const struct ofproto *,
2619 uint32_t of_meter_id);
2621 /* Creates and returns a new 'struct rule_actions', with a ref_count of 1,
2622 * whose actions are a copy of from the 'ofpacts_len' bytes of 'ofpacts'. */
2623 struct rule_actions *
2624 rule_actions_create(const struct ofproto *ofproto,
2625 const struct ofpact *ofpacts, size_t ofpacts_len)
2627 struct rule_actions *actions;
2629 actions = xmalloc(sizeof *actions);
2630 ovs_refcount_init(&actions->ref_count);
2631 actions->ofpacts = xmemdup(ofpacts, ofpacts_len);
2632 actions->ofpacts_len = ofpacts_len;
2633 actions->provider_meter_id
2634 = get_provider_meter_id(ofproto,
2635 ofpacts_get_meter(ofpacts, ofpacts_len));
2640 /* Increments 'actions''s ref_count. */
2642 rule_actions_ref(struct rule_actions *actions)
2645 ovs_refcount_ref(&actions->ref_count);
2649 /* Decrements 'actions''s ref_count and frees 'actions' if the ref_count
2652 rule_actions_unref(struct rule_actions *actions)
2654 if (actions && ovs_refcount_unref(&actions->ref_count) == 1) {
2655 ovs_refcount_destroy(&actions->ref_count);
2656 free(actions->ofpacts);
2661 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2662 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2664 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2665 OVS_REQUIRES(ofproto_mutex)
2667 return (port == OFPP_ANY
2668 || ofpacts_output_to_port(rule->actions->ofpacts,
2669 rule->actions->ofpacts_len, port));
2672 /* Returns true if 'rule' has group and equals group_id. */
2674 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2675 OVS_REQUIRES(ofproto_mutex)
2677 return (group_id == OFPG11_ANY
2678 || ofpacts_output_to_group(rule->actions->ofpacts,
2679 rule->actions->ofpacts_len, group_id));
2682 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2683 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2685 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2686 OVS_REQUIRES(ofproto_mutex)
2688 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2693 case OFOPERATION_ADD:
2694 case OFOPERATION_DELETE:
2697 case OFOPERATION_MODIFY:
2698 case OFOPERATION_REPLACE:
2699 return ofpacts_output_to_port(op->actions->ofpacts,
2700 op->actions->ofpacts_len, out_port);
2707 rule_execute_destroy(struct rule_execute *e)
2709 ofproto_rule_unref(e->rule);
2710 list_remove(&e->list_node);
2714 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2715 * by passing them to the ofproto provider. */
2717 run_rule_executes(struct ofproto *ofproto)
2718 OVS_EXCLUDED(ofproto_mutex)
2720 struct rule_execute *e, *next;
2721 struct list executes;
2723 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2724 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2727 flow_extract(e->packet, NULL, &flow);
2728 flow.in_port.ofp_port = e->in_port;
2729 ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet);
2731 rule_execute_destroy(e);
2735 /* Destroys and discards all "rule_execute" operations queued up in
2736 * ofproto->rule_executes. */
2738 destroy_rule_executes(struct ofproto *ofproto)
2740 struct rule_execute *e, *next;
2741 struct list executes;
2743 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2744 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2745 ofpbuf_delete(e->packet);
2746 rule_execute_destroy(e);
2750 /* Returns true if 'rule' should be hidden from the controller.
2752 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2753 * (e.g. by in-band control) and are intentionally hidden from the
2756 ofproto_rule_is_hidden(const struct rule *rule)
2758 return rule->cr.priority > UINT16_MAX;
2761 static enum oftable_flags
2762 rule_get_flags(const struct rule *rule)
2764 return rule->ofproto->tables[rule->table_id].flags;
2768 rule_is_modifiable(const struct rule *rule)
2770 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2774 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2776 ofconn_send_reply(ofconn, make_echo_reply(oh));
2781 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2783 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2784 struct ofputil_switch_features features;
2785 struct ofport *port;
2791 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2793 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2795 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2796 * if present, are always at the end.) */
2797 n_tables = ofproto->n_tables;
2798 for (i = 0; i < ofproto->n_tables; i++) {
2799 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2805 features.datapath_id = ofproto->datapath_id;
2806 features.n_buffers = pktbuf_capacity();
2807 features.n_tables = n_tables;
2808 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2809 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2811 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2813 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2814 features.auxiliary_id = 0;
2815 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2817 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2818 ofputil_put_switch_features_port(&port->pp, b);
2821 ofconn_send_reply(ofconn, b);
2826 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2828 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2829 struct ofp_switch_config *osc;
2830 enum ofp_config_flags flags;
2834 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2835 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2836 flags = ofproto->frag_handling;
2837 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2838 if (oh->version < OFP13_VERSION
2839 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2840 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2842 osc->flags = htons(flags);
2843 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2844 ofconn_send_reply(ofconn, buf);
2850 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2852 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2853 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2854 uint16_t flags = ntohs(osc->flags);
2856 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2857 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2858 enum ofp_config_flags cur = ofproto->frag_handling;
2859 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2861 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2863 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2864 ofproto->frag_handling = next;
2866 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2868 ofputil_frag_handling_to_string(next));
2872 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2873 ofconn_set_invalid_ttl_to_controller(ofconn,
2874 (oh->version < OFP13_VERSION
2875 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2877 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2882 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2883 * error message code for the caller to propagate upward. Otherwise, returns
2886 * The log message mentions 'msg_type'. */
2888 reject_slave_controller(struct ofconn *ofconn)
2890 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2891 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2892 return OFPERR_OFPBRC_EPERM;
2898 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
2901 * - If they use a meter, then 'ofproto' has that meter configured.
2903 * - If they use any groups, then 'ofproto' has that group configured.
2905 * Returns 0 if successful, otherwise an OpenFlow error. */
2907 ofproto_check_ofpacts(struct ofproto *ofproto,
2908 const struct ofpact ofpacts[], size_t ofpacts_len)
2910 const struct ofpact *a;
2913 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2914 if (mid && get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2915 return OFPERR_OFPMMFC_INVALID_METER;
2918 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2919 if (a->type == OFPACT_GROUP
2920 && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) {
2921 return OFPERR_OFPBAC_BAD_OUT_GROUP;
2929 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2931 struct ofproto *p = ofconn_get_ofproto(ofconn);
2932 struct ofputil_packet_out po;
2933 struct ofpbuf *payload;
2934 uint64_t ofpacts_stub[1024 / 8];
2935 struct ofpbuf ofpacts;
2939 COVERAGE_INC(ofproto_packet_out);
2941 error = reject_slave_controller(ofconn);
2946 /* Decode message. */
2947 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2948 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2950 goto exit_free_ofpacts;
2952 if (ofp_to_u16(po.in_port) >= p->max_ports
2953 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2954 error = OFPERR_OFPBRC_BAD_PORT;
2955 goto exit_free_ofpacts;
2959 if (po.buffer_id != UINT32_MAX) {
2960 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2961 if (error || !payload) {
2962 goto exit_free_ofpacts;
2965 /* Ensure that the L3 header is 32-bit aligned. */
2966 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2969 /* Verify actions against packet, then send packet if successful. */
2970 flow_extract(payload, NULL, &flow);
2971 flow.in_port.ofp_port = po.in_port;
2972 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len);
2974 error = p->ofproto_class->packet_out(p, payload, &flow,
2975 po.ofpacts, po.ofpacts_len);
2977 ofpbuf_delete(payload);
2980 ofpbuf_uninit(&ofpacts);
2986 update_port_config(struct ofconn *ofconn, struct ofport *port,
2987 enum ofputil_port_config config,
2988 enum ofputil_port_config mask)
2990 enum ofputil_port_config toggle = (config ^ port->pp.config) & mask;
2992 if (toggle & OFPUTIL_PC_PORT_DOWN
2993 && (config & OFPUTIL_PC_PORT_DOWN
2994 ? netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL)
2995 : netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL))) {
2996 /* We tried to bring the port up or down, but it failed, so don't
2997 * update the "down" bit. */
2998 toggle &= ~OFPUTIL_PC_PORT_DOWN;
3002 enum ofputil_port_config old_config = port->pp.config;
3003 port->pp.config ^= toggle;
3004 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
3005 connmgr_send_port_status(port->ofproto->connmgr, ofconn, &port->pp,
3011 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3013 struct ofproto *p = ofconn_get_ofproto(ofconn);
3014 struct ofputil_port_mod pm;
3015 struct ofport *port;
3018 error = reject_slave_controller(ofconn);
3023 error = ofputil_decode_port_mod(oh, &pm);
3028 port = ofproto_get_port(p, pm.port_no);
3030 return OFPERR_OFPPMFC_BAD_PORT;
3031 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
3032 return OFPERR_OFPPMFC_BAD_HW_ADDR;
3034 update_port_config(ofconn, port, pm.config, pm.mask);
3036 netdev_set_advertisements(port->netdev, pm.advertise);
3043 handle_desc_stats_request(struct ofconn *ofconn,
3044 const struct ofp_header *request)
3046 static const char *default_mfr_desc = "Nicira, Inc.";
3047 static const char *default_hw_desc = "Open vSwitch";
3048 static const char *default_sw_desc = VERSION;
3049 static const char *default_serial_desc = "None";
3050 static const char *default_dp_desc = "None";
3052 struct ofproto *p = ofconn_get_ofproto(ofconn);
3053 struct ofp_desc_stats *ods;
3056 msg = ofpraw_alloc_stats_reply(request, 0);
3057 ods = ofpbuf_put_zeros(msg, sizeof *ods);
3058 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
3059 sizeof ods->mfr_desc);
3060 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
3061 sizeof ods->hw_desc);
3062 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
3063 sizeof ods->sw_desc);
3064 ovs_strlcpy(ods->serial_num,
3065 p->serial_desc ? p->serial_desc : default_serial_desc,
3066 sizeof ods->serial_num);
3067 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
3068 sizeof ods->dp_desc);
3069 ofconn_send_reply(ofconn, msg);
3075 handle_table_stats_request(struct ofconn *ofconn,
3076 const struct ofp_header *request)
3078 struct ofproto *p = ofconn_get_ofproto(ofconn);
3079 struct ofp12_table_stats *ots;
3084 /* Set up default values.
3086 * ofp12_table_stats is used as a generic structure as
3087 * it is able to hold all the fields for ofp10_table_stats
3088 * and ofp11_table_stats (and of course itself).
3090 ots = xcalloc(p->n_tables, sizeof *ots);
3091 for (i = 0; i < p->n_tables; i++) {
3092 ots[i].table_id = i;
3093 sprintf(ots[i].name, "table%"PRIuSIZE, i);
3094 ots[i].match = htonll(OFPXMT13_MASK);
3095 ots[i].wildcards = htonll(OFPXMT13_MASK);
3096 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
3097 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
3098 ots[i].write_setfields = htonll(OFPXMT13_MASK);
3099 ots[i].apply_setfields = htonll(OFPXMT13_MASK);
3100 ots[i].metadata_match = OVS_BE64_MAX;
3101 ots[i].metadata_write = OVS_BE64_MAX;
3102 ots[i].instructions = htonl(OFPIT11_ALL);
3103 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
3104 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
3105 fat_rwlock_rdlock(&p->tables[i].cls.rwlock);
3106 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
3107 fat_rwlock_unlock(&p->tables[i].cls.rwlock);
3110 p->ofproto_class->get_tables(p, ots);
3112 /* Post-process the tables, dropping hidden tables. */
3113 n_tables = p->n_tables;
3114 for (i = 0; i < p->n_tables; i++) {
3115 const struct oftable *table = &p->tables[i];
3117 if (table->flags & OFTABLE_HIDDEN) {
3123 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
3126 if (table->max_flows < ntohl(ots[i].max_entries)) {
3127 ots[i].max_entries = htonl(table->max_flows);
3131 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
3132 ofconn_send_reply(ofconn, msg);
3140 append_port_stat(struct ofport *port, struct list *replies)
3142 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
3144 calc_duration(port->created, time_msec(),
3145 &ops.duration_sec, &ops.duration_nsec);
3147 /* Intentionally ignore return value, since errors will set
3148 * 'stats' to all-1s, which is correct for OpenFlow, and
3149 * netdev_get_stats() will log errors. */
3150 ofproto_port_get_stats(port, &ops.stats);
3152 ofputil_append_port_stat(replies, &ops);
3156 handle_port_stats_request(struct ofconn *ofconn,
3157 const struct ofp_header *request)
3159 struct ofproto *p = ofconn_get_ofproto(ofconn);
3160 struct ofport *port;
3161 struct list replies;
3165 error = ofputil_decode_port_stats_request(request, &port_no);
3170 ofpmp_init(&replies, request);
3171 if (port_no != OFPP_ANY) {
3172 port = ofproto_get_port(p, port_no);
3174 append_port_stat(port, &replies);
3177 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3178 append_port_stat(port, &replies);
3182 ofconn_send_replies(ofconn, &replies);
3187 handle_port_desc_stats_request(struct ofconn *ofconn,
3188 const struct ofp_header *request)
3190 struct ofproto *p = ofconn_get_ofproto(ofconn);
3191 enum ofp_version version;
3192 struct ofport *port;
3193 struct list replies;
3195 ofpmp_init(&replies, request);
3197 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
3198 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
3199 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
3202 ofconn_send_replies(ofconn, &replies);
3207 hash_cookie(ovs_be64 cookie)
3209 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
3210 (OVS_FORCE uint64_t)cookie);
3214 cookies_insert(struct ofproto *ofproto, struct rule *rule)
3215 OVS_REQUIRES(ofproto_mutex)
3217 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3218 hash_cookie(rule->flow_cookie));
3222 cookies_remove(struct ofproto *ofproto, struct rule *rule)
3223 OVS_REQUIRES(ofproto_mutex)
3225 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3229 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
3230 ovs_be64 new_cookie)
3231 OVS_REQUIRES(ofproto_mutex)
3233 if (new_cookie != rule->flow_cookie) {
3234 cookies_remove(ofproto, rule);
3236 ovs_mutex_lock(&rule->mutex);
3237 rule->flow_cookie = new_cookie;
3238 ovs_mutex_unlock(&rule->mutex);
3240 cookies_insert(ofproto, rule);
3245 calc_duration(long long int start, long long int now,
3246 uint32_t *sec, uint32_t *nsec)
3248 long long int msecs = now - start;
3249 *sec = msecs / 1000;
3250 *nsec = (msecs % 1000) * (1000 * 1000);
3253 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3254 * true if 'table_id' is OK, false otherwise. */
3256 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3258 return table_id == OFPTT_ALL || table_id < ofproto->n_tables;
3261 static struct oftable *
3262 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
3264 struct oftable *table;
3266 for (table = &ofproto->tables[table_id];
3267 table < &ofproto->tables[ofproto->n_tables];
3269 if (!(table->flags & OFTABLE_HIDDEN)) {
3277 static struct oftable *
3278 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
3280 if (table_id == 0xff) {
3281 return next_visible_table(ofproto, 0);
3282 } else if (table_id < ofproto->n_tables) {
3283 return &ofproto->tables[table_id];
3289 static struct oftable *
3290 next_matching_table(const struct ofproto *ofproto,
3291 const struct oftable *table, uint8_t table_id)
3293 return (table_id == 0xff
3294 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
3298 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3300 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3301 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3303 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3304 * only once, for that table. (This can be used to access tables marked
3307 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3308 * entered at all. (Perhaps you should have validated TABLE_ID with
3309 * check_table_id().)
3311 * All parameters are evaluated multiple times.
3313 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3314 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3316 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3318 /* Initializes 'criteria' in a straightforward way based on the other
3321 * For "loose" matching, the 'priority' parameter is unimportant and may be
3324 rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
3325 const struct match *match, unsigned int priority,
3326 ovs_be64 cookie, ovs_be64 cookie_mask,
3327 ofp_port_t out_port, uint32_t out_group)
3329 criteria->table_id = table_id;
3330 cls_rule_init(&criteria->cr, match, priority);
3331 criteria->cookie = cookie;
3332 criteria->cookie_mask = cookie_mask;
3333 criteria->out_port = out_port;
3334 criteria->out_group = out_group;
3338 rule_criteria_destroy(struct rule_criteria *criteria)
3340 cls_rule_destroy(&criteria->cr);
3344 rule_collection_init(struct rule_collection *rules)
3346 rules->rules = rules->stub;
3348 rules->capacity = ARRAY_SIZE(rules->stub);
3352 rule_collection_add(struct rule_collection *rules, struct rule *rule)
3354 if (rules->n >= rules->capacity) {
3355 size_t old_size, new_size;
3357 old_size = rules->capacity * sizeof *rules->rules;
3358 rules->capacity *= 2;
3359 new_size = rules->capacity * sizeof *rules->rules;
3361 if (rules->rules == rules->stub) {
3362 rules->rules = xmalloc(new_size);
3363 memcpy(rules->rules, rules->stub, old_size);
3365 rules->rules = xrealloc(rules->rules, new_size);
3369 rules->rules[rules->n++] = rule;
3373 rule_collection_ref(struct rule_collection *rules)
3374 OVS_REQUIRES(ofproto_mutex)
3378 for (i = 0; i < rules->n; i++) {
3379 ofproto_rule_ref(rules->rules[i]);
3384 rule_collection_unref(struct rule_collection *rules)
3388 for (i = 0; i < rules->n; i++) {
3389 ofproto_rule_unref(rules->rules[i]);
3394 rule_collection_destroy(struct rule_collection *rules)
3396 if (rules->rules != rules->stub) {
3402 collect_rule(struct rule *rule, const struct rule_criteria *c,
3403 struct rule_collection *rules)
3404 OVS_REQUIRES(ofproto_mutex)
3406 /* We ordinarily want to skip hidden rules, but there has to be a way for
3407 * code internal to OVS to modify and delete them, so if the criteria
3408 * specify a priority that can only be for a hidden flow, then allow hidden
3409 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
3410 * with hidden flows because OpenFlow uses only a 16-bit field to specify
3412 if (ofproto_rule_is_hidden(rule) && c->cr.priority <= UINT16_MAX) {
3414 } else if (rule->pending) {
3415 return OFPROTO_POSTPONE;
3417 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3418 && ofproto_rule_has_out_port(rule, c->out_port)
3419 && ofproto_rule_has_out_group(rule, c->out_group)
3420 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)) {
3421 rule_collection_add(rules, rule);
3427 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3428 * on classifiers rules are done in the "loose" way required for OpenFlow
3429 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3432 * Hidden rules are always omitted.
3434 * Returns 0 on success, otherwise an OpenFlow error code. */
3436 collect_rules_loose(struct ofproto *ofproto,
3437 const struct rule_criteria *criteria,
3438 struct rule_collection *rules)
3439 OVS_REQUIRES(ofproto_mutex)
3441 struct oftable *table;
3442 enum ofperr error = 0;
3444 rule_collection_init(rules);
3446 if (!check_table_id(ofproto, criteria->table_id)) {
3447 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3451 if (criteria->cookie_mask == OVS_BE64_MAX) {
3454 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3455 hash_cookie(criteria->cookie),
3456 &ofproto->cookies) {
3457 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
3458 error = collect_rule(rule, criteria, rules);
3465 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3466 struct cls_cursor cursor;
3469 fat_rwlock_rdlock(&table->cls.rwlock);
3470 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
3471 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3472 error = collect_rule(rule, criteria, rules);
3477 fat_rwlock_unlock(&table->cls.rwlock);
3483 rule_collection_destroy(rules);
3488 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3489 * on classifiers rules are done in the "strict" way required for OpenFlow
3490 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3491 * rules on list 'rules'.
3493 * Hidden rules are always omitted.
3495 * Returns 0 on success, otherwise an OpenFlow error code. */
3497 collect_rules_strict(struct ofproto *ofproto,
3498 const struct rule_criteria *criteria,
3499 struct rule_collection *rules)
3500 OVS_REQUIRES(ofproto_mutex)
3502 struct oftable *table;
3505 rule_collection_init(rules);
3507 if (!check_table_id(ofproto, criteria->table_id)) {
3508 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3512 if (criteria->cookie_mask == OVS_BE64_MAX) {
3515 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3516 hash_cookie(criteria->cookie),
3517 &ofproto->cookies) {
3518 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
3519 error = collect_rule(rule, criteria, rules);
3526 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3529 fat_rwlock_rdlock(&table->cls.rwlock);
3530 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3531 &table->cls, &criteria->cr));
3532 fat_rwlock_unlock(&table->cls.rwlock);
3534 error = collect_rule(rule, criteria, rules);
3544 rule_collection_destroy(rules);
3549 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3550 * forced into the range of a uint16_t. */
3552 age_secs(long long int age_ms)
3554 return (age_ms < 0 ? 0
3555 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3556 : (unsigned int) age_ms / 1000);
3560 handle_flow_stats_request(struct ofconn *ofconn,
3561 const struct ofp_header *request)
3562 OVS_EXCLUDED(ofproto_mutex)
3564 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3565 struct ofputil_flow_stats_request fsr;
3566 struct rule_criteria criteria;
3567 struct rule_collection rules;
3568 struct list replies;
3572 error = ofputil_decode_flow_stats_request(&fsr, request);
3577 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3578 fsr.cookie_mask, fsr.out_port, fsr.out_group);
3580 ovs_mutex_lock(&ofproto_mutex);
3581 error = collect_rules_loose(ofproto, &criteria, &rules);
3582 rule_criteria_destroy(&criteria);
3584 rule_collection_ref(&rules);
3586 ovs_mutex_unlock(&ofproto_mutex);
3592 ofpmp_init(&replies, request);
3593 for (i = 0; i < rules.n; i++) {
3594 struct rule *rule = rules.rules[i];
3595 long long int now = time_msec();
3596 struct ofputil_flow_stats fs;
3597 long long int created, used, modified;
3598 struct rule_actions *actions;
3599 enum ofputil_flow_mod_flags flags;
3601 ovs_mutex_lock(&rule->mutex);
3602 fs.cookie = rule->flow_cookie;
3603 fs.idle_timeout = rule->idle_timeout;
3604 fs.hard_timeout = rule->hard_timeout;
3605 created = rule->created;
3606 modified = rule->modified;
3607 actions = rule_get_actions__(rule);
3608 flags = rule->flags;
3609 ovs_mutex_unlock(&rule->mutex);
3611 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3612 &fs.byte_count, &used);
3614 minimatch_expand(&rule->cr.match, &fs.match);
3615 fs.table_id = rule->table_id;
3616 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
3617 fs.priority = rule->cr.priority;
3618 fs.idle_age = age_secs(now - used);
3619 fs.hard_age = age_secs(now - modified);
3620 fs.ofpacts = actions->ofpacts;
3621 fs.ofpacts_len = actions->ofpacts_len;
3624 ofputil_append_flow_stats_reply(&fs, &replies);
3626 rule_actions_unref(actions);
3629 rule_collection_unref(&rules);
3630 rule_collection_destroy(&rules);
3632 ofconn_send_replies(ofconn, &replies);
3638 flow_stats_ds(struct rule *rule, struct ds *results)
3640 uint64_t packet_count, byte_count;
3641 struct rule_actions *actions;
3642 long long int created, used;
3644 rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3645 &byte_count, &used);
3647 ovs_mutex_lock(&rule->mutex);
3648 actions = rule_get_actions__(rule);
3649 created = rule->created;
3650 ovs_mutex_unlock(&rule->mutex);
3652 if (rule->table_id != 0) {
3653 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3655 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
3656 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3657 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3658 cls_rule_format(&rule->cr, results);
3659 ds_put_char(results, ',');
3661 ds_put_cstr(results, "actions=");
3662 ofpacts_format(actions->ofpacts, actions->ofpacts_len, results);
3664 ds_put_cstr(results, "\n");
3666 rule_actions_unref(actions);
3669 /* Adds a pretty-printed description of all flows to 'results', including
3670 * hidden flows (e.g., set up by in-band control). */
3672 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3674 struct oftable *table;
3676 OFPROTO_FOR_EACH_TABLE (table, p) {
3677 struct cls_cursor cursor;
3680 fat_rwlock_rdlock(&table->cls.rwlock);
3681 cls_cursor_init(&cursor, &table->cls, NULL);
3682 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3683 flow_stats_ds(rule, results);
3685 fat_rwlock_unlock(&table->cls.rwlock);
3689 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3690 * '*engine_type' and '*engine_id', respectively. */
3692 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3693 uint8_t *engine_type, uint8_t *engine_id)
3695 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3698 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3699 * true if the port's CFM status was successfully stored into '*status'.
3700 * Returns false if the port did not have CFM configured, in which case
3701 * '*status' is indeterminate.
3703 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3705 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3706 struct ofproto_cfm_status *status)
3708 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3710 && ofproto->ofproto_class->get_cfm_status
3711 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3715 handle_aggregate_stats_request(struct ofconn *ofconn,
3716 const struct ofp_header *oh)
3717 OVS_EXCLUDED(ofproto_mutex)
3719 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3720 struct ofputil_flow_stats_request request;
3721 struct ofputil_aggregate_stats stats;
3722 bool unknown_packets, unknown_bytes;
3723 struct rule_criteria criteria;
3724 struct rule_collection rules;
3725 struct ofpbuf *reply;
3729 error = ofputil_decode_flow_stats_request(&request, oh);
3734 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3735 request.cookie, request.cookie_mask,
3736 request.out_port, request.out_group);
3738 ovs_mutex_lock(&ofproto_mutex);
3739 error = collect_rules_loose(ofproto, &criteria, &rules);
3740 rule_criteria_destroy(&criteria);
3742 rule_collection_ref(&rules);
3744 ovs_mutex_unlock(&ofproto_mutex);
3750 memset(&stats, 0, sizeof stats);
3751 unknown_packets = unknown_bytes = false;
3752 for (i = 0; i < rules.n; i++) {
3753 struct rule *rule = rules.rules[i];
3754 uint64_t packet_count;
3755 uint64_t byte_count;
3758 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3759 &byte_count, &used);
3761 if (packet_count == UINT64_MAX) {
3762 unknown_packets = true;
3764 stats.packet_count += packet_count;
3767 if (byte_count == UINT64_MAX) {
3768 unknown_bytes = true;
3770 stats.byte_count += byte_count;
3775 if (unknown_packets) {
3776 stats.packet_count = UINT64_MAX;
3778 if (unknown_bytes) {
3779 stats.byte_count = UINT64_MAX;
3782 rule_collection_unref(&rules);
3783 rule_collection_destroy(&rules);
3785 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3786 ofconn_send_reply(ofconn, reply);
3791 struct queue_stats_cbdata {
3792 struct ofport *ofport;
3793 struct list replies;
3798 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3799 const struct netdev_queue_stats *stats)
3801 struct ofputil_queue_stats oqs;
3803 oqs.port_no = cbdata->ofport->pp.port_no;
3804 oqs.queue_id = queue_id;
3805 oqs.tx_bytes = stats->tx_bytes;
3806 oqs.tx_packets = stats->tx_packets;
3807 oqs.tx_errors = stats->tx_errors;
3808 if (stats->created != LLONG_MIN) {
3809 calc_duration(stats->created, cbdata->now,
3810 &oqs.duration_sec, &oqs.duration_nsec);
3812 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3814 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3818 handle_queue_stats_dump_cb(uint32_t queue_id,
3819 struct netdev_queue_stats *stats,
3822 struct queue_stats_cbdata *cbdata = cbdata_;
3824 put_queue_stats(cbdata, queue_id, stats);
3828 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3829 struct queue_stats_cbdata *cbdata)
3831 cbdata->ofport = port;
3832 if (queue_id == OFPQ_ALL) {
3833 netdev_dump_queue_stats(port->netdev,
3834 handle_queue_stats_dump_cb, cbdata);
3836 struct netdev_queue_stats stats;
3838 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3839 put_queue_stats(cbdata, queue_id, &stats);
3841 return OFPERR_OFPQOFC_BAD_QUEUE;
3848 handle_queue_stats_request(struct ofconn *ofconn,
3849 const struct ofp_header *rq)
3851 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3852 struct queue_stats_cbdata cbdata;
3853 struct ofport *port;
3855 struct ofputil_queue_stats_request oqsr;
3857 COVERAGE_INC(ofproto_queue_req);
3859 ofpmp_init(&cbdata.replies, rq);
3860 cbdata.now = time_msec();
3862 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3867 if (oqsr.port_no == OFPP_ANY) {
3868 error = OFPERR_OFPQOFC_BAD_QUEUE;
3869 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3870 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3875 port = ofproto_get_port(ofproto, oqsr.port_no);
3877 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3878 : OFPERR_OFPQOFC_BAD_PORT);
3881 ofconn_send_replies(ofconn, &cbdata.replies);
3883 ofpbuf_list_delete(&cbdata.replies);
3890 is_flow_deletion_pending(const struct ofproto *ofproto,
3891 const struct cls_rule *cls_rule,
3893 OVS_REQUIRES(ofproto_mutex)
3895 if (!hmap_is_empty(&ofproto->deletions)) {
3896 struct ofoperation *op;
3898 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3899 cls_rule_hash(cls_rule, table_id),
3900 &ofproto->deletions) {
3901 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3911 should_evict_a_rule(struct oftable *table, unsigned int extra_space)
3912 OVS_REQUIRES(ofproto_mutex)
3913 OVS_NO_THREAD_SAFETY_ANALYSIS
3915 return classifier_count(&table->cls) + extra_space > table->max_flows;
3919 evict_rules_from_table(struct ofproto *ofproto, struct oftable *table,
3920 unsigned int extra_space)
3921 OVS_REQUIRES(ofproto_mutex)
3923 while (should_evict_a_rule(table, extra_space)) {
3926 if (!choose_rule_to_evict(table, &rule)) {
3927 return OFPERR_OFPFMFC_TABLE_FULL;
3928 } else if (rule->pending) {
3929 return OFPROTO_POSTPONE;
3931 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
3932 delete_flow__(rule, group, OFPRR_EVICTION);
3933 ofopgroup_submit(group);
3940 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3941 * in which no matching flow already exists in the flow table.
3943 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3944 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3945 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3946 * initiated now but may be retried later.
3948 * The caller retains ownership of 'fm->ofpacts'.
3950 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3953 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3954 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3955 OVS_REQUIRES(ofproto_mutex)
3957 struct oftable *table;
3958 struct ofopgroup *group;
3964 if (!check_table_id(ofproto, fm->table_id)) {
3965 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3970 if (fm->table_id == 0xff) {
3971 if (ofproto->ofproto_class->rule_choose_table) {
3972 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3978 ovs_assert(table_id < ofproto->n_tables);
3982 } else if (fm->table_id < ofproto->n_tables) {
3983 table_id = fm->table_id;
3985 return OFPERR_OFPBRC_BAD_TABLE_ID;
3988 table = &ofproto->tables[table_id];
3990 if (table->flags & OFTABLE_READONLY) {
3991 return OFPERR_OFPBRC_EPERM;
3994 cls_rule_init(&cr, &fm->match, fm->priority);
3996 /* Transform "add" into "modify" if there's an existing identical flow. */
3997 fat_rwlock_rdlock(&table->cls.rwlock);
3998 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
3999 fat_rwlock_unlock(&table->cls.rwlock);
4001 cls_rule_destroy(&cr);
4002 if (!rule_is_modifiable(rule)) {
4003 return OFPERR_OFPBRC_EPERM;
4004 } else if (rule->pending) {
4005 return OFPROTO_POSTPONE;
4007 struct rule_collection rules;
4009 rule_collection_init(&rules);
4010 rule_collection_add(&rules, rule);
4011 fm->modify_cookie = true;
4012 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4013 rule_collection_destroy(&rules);
4019 /* Serialize against pending deletion. */
4020 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
4021 cls_rule_destroy(&cr);
4022 return OFPROTO_POSTPONE;
4025 /* Check for overlap, if requested. */
4026 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
4029 fat_rwlock_rdlock(&table->cls.rwlock);
4030 overlaps = classifier_rule_overlaps(&table->cls, &cr);
4031 fat_rwlock_unlock(&table->cls.rwlock);
4034 cls_rule_destroy(&cr);
4035 return OFPERR_OFPFMFC_OVERLAP;
4039 /* If necessary, evict an existing rule to clear out space. */
4040 error = evict_rules_from_table(ofproto, table, 1);
4042 cls_rule_destroy(&cr);
4046 /* Allocate new rule. */
4047 rule = ofproto->ofproto_class->rule_alloc();
4049 cls_rule_destroy(&cr);
4050 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
4051 ofproto->name, ovs_strerror(error));
4055 /* Initialize base state. */
4056 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4057 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
4058 ovs_refcount_init(&rule->ref_count);
4059 rule->pending = NULL;
4060 rule->flow_cookie = fm->new_cookie;
4061 rule->created = rule->modified = time_msec();
4063 ovs_mutex_init(&rule->mutex);
4064 ovs_mutex_lock(&rule->mutex);
4065 rule->idle_timeout = fm->idle_timeout;
4066 rule->hard_timeout = fm->hard_timeout;
4067 ovs_mutex_unlock(&rule->mutex);
4069 *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
4070 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4071 rule->actions = rule_actions_create(ofproto, fm->ofpacts, fm->ofpacts_len);
4072 list_init(&rule->meter_list_node);
4073 rule->eviction_group = NULL;
4074 list_init(&rule->expirable);
4075 rule->monitor_flags = 0;
4076 rule->add_seqno = 0;
4077 rule->modify_seqno = 0;
4079 /* Construct rule, initializing derived state. */
4080 error = ofproto->ofproto_class->rule_construct(rule);
4082 ofproto_rule_destroy__(rule);
4087 oftable_insert_rule(rule);
4089 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4090 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
4091 ofproto->ofproto_class->rule_insert(rule);
4092 ofopgroup_submit(group);
4097 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4099 /* Modifies the rules listed in 'rules', changing their actions to match those
4102 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4105 * Returns 0 on success, otherwise an OpenFlow error code. */
4107 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4108 struct ofputil_flow_mod *fm, const struct ofp_header *request,
4109 const struct rule_collection *rules)
4110 OVS_REQUIRES(ofproto_mutex)
4112 enum ofoperation_type type;
4113 struct ofopgroup *group;
4117 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
4118 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4119 error = OFPERR_OFPBRC_EPERM;
4120 for (i = 0; i < rules->n; i++) {
4121 struct rule *rule = rules->rules[i];
4122 struct ofoperation *op;
4123 bool actions_changed;
4124 bool reset_counters;
4126 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
4128 if (rule_is_modifiable(rule)) {
4129 /* At least one rule is modifiable, don't report EPERM error. */
4135 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
4136 rule->actions->ofpacts,
4137 rule->actions->ofpacts_len);
4139 op = ofoperation_create(group, rule, type, 0);
4141 if (fm->modify_cookie && fm->new_cookie != OVS_BE64_MAX) {
4142 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
4144 if (type == OFOPERATION_REPLACE) {
4145 ovs_mutex_lock(&rule->mutex);
4146 rule->idle_timeout = fm->idle_timeout;
4147 rule->hard_timeout = fm->hard_timeout;
4148 ovs_mutex_unlock(&rule->mutex);
4150 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4151 if (fm->idle_timeout || fm->hard_timeout) {
4152 if (!rule->eviction_group) {
4153 eviction_group_add_rule(rule);
4156 eviction_group_remove_rule(rule);
4160 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
4161 if (actions_changed || reset_counters) {
4162 struct rule_actions *new_actions;
4164 op->actions = rule->actions;
4165 new_actions = rule_actions_create(ofproto,
4166 fm->ofpacts, fm->ofpacts_len);
4168 ovs_mutex_lock(&rule->mutex);
4169 rule->actions = new_actions;
4170 ovs_mutex_unlock(&rule->mutex);
4172 rule->ofproto->ofproto_class->rule_modify_actions(rule,
4175 ofoperation_complete(op, 0);
4178 ofopgroup_submit(group);
4184 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
4185 struct ofputil_flow_mod *fm, const struct ofp_header *request)
4186 OVS_REQUIRES(ofproto_mutex)
4188 if (fm->cookie_mask != htonll(0) || fm->new_cookie == OVS_BE64_MAX) {
4191 return add_flow(ofproto, ofconn, fm, request);
4194 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4197 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4200 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4201 struct ofputil_flow_mod *fm,
4202 const struct ofp_header *request)
4203 OVS_REQUIRES(ofproto_mutex)
4205 struct rule_criteria criteria;
4206 struct rule_collection rules;
4209 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4210 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4211 error = collect_rules_loose(ofproto, &criteria, &rules);
4212 rule_criteria_destroy(&criteria);
4215 error = (rules.n > 0
4216 ? modify_flows__(ofproto, ofconn, fm, request, &rules)
4217 : modify_flows_add(ofproto, ofconn, fm, request));
4220 rule_collection_destroy(&rules);
4225 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
4228 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4231 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4232 struct ofputil_flow_mod *fm,
4233 const struct ofp_header *request)
4234 OVS_REQUIRES(ofproto_mutex)
4236 struct rule_criteria criteria;
4237 struct rule_collection rules;
4240 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4241 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4242 error = collect_rules_strict(ofproto, &criteria, &rules);
4243 rule_criteria_destroy(&criteria);
4247 error = modify_flows_add(ofproto, ofconn, fm, request);
4248 } else if (rules.n == 1) {
4249 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4253 rule_collection_destroy(&rules);
4258 /* OFPFC_DELETE implementation. */
4261 delete_flow__(struct rule *rule, struct ofopgroup *group,
4262 enum ofp_flow_removed_reason reason)
4263 OVS_REQUIRES(ofproto_mutex)
4265 struct ofproto *ofproto = rule->ofproto;
4267 ofproto_rule_send_removed(rule, reason);
4269 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
4270 oftable_remove_rule(rule);
4271 ofproto->ofproto_class->rule_delete(rule);
4274 /* Deletes the rules listed in 'rules'.
4276 * Returns 0 on success, otherwise an OpenFlow error code. */
4278 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4279 const struct ofp_header *request,
4280 const struct rule_collection *rules,
4281 enum ofp_flow_removed_reason reason)
4282 OVS_REQUIRES(ofproto_mutex)
4284 struct ofopgroup *group;
4287 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
4288 for (i = 0; i < rules->n; i++) {
4289 delete_flow__(rules->rules[i], group, reason);
4291 ofopgroup_submit(group);
4296 /* Implements OFPFC_DELETE. */
4298 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4299 const struct ofputil_flow_mod *fm,
4300 const struct ofp_header *request)
4301 OVS_REQUIRES(ofproto_mutex)
4303 struct rule_criteria criteria;
4304 struct rule_collection rules;
4307 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4308 fm->cookie, fm->cookie_mask,
4309 fm->out_port, fm->out_group);
4310 error = collect_rules_loose(ofproto, &criteria, &rules);
4311 rule_criteria_destroy(&criteria);
4313 if (!error && rules.n > 0) {
4314 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4316 rule_collection_destroy(&rules);
4321 /* Implements OFPFC_DELETE_STRICT. */
4323 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4324 const struct ofputil_flow_mod *fm,
4325 const struct ofp_header *request)
4326 OVS_REQUIRES(ofproto_mutex)
4328 struct rule_criteria criteria;
4329 struct rule_collection rules;
4332 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4333 fm->cookie, fm->cookie_mask,
4334 fm->out_port, fm->out_group);
4335 error = collect_rules_strict(ofproto, &criteria, &rules);
4336 rule_criteria_destroy(&criteria);
4338 if (!error && rules.n > 0) {
4339 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4341 rule_collection_destroy(&rules);
4347 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
4348 OVS_REQUIRES(ofproto_mutex)
4350 struct ofputil_flow_removed fr;
4353 if (ofproto_rule_is_hidden(rule) ||
4354 !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
4358 minimatch_expand(&rule->cr.match, &fr.match);
4359 fr.priority = rule->cr.priority;
4360 fr.cookie = rule->flow_cookie;
4362 fr.table_id = rule->table_id;
4363 calc_duration(rule->created, time_msec(),
4364 &fr.duration_sec, &fr.duration_nsec);
4365 ovs_mutex_lock(&rule->mutex);
4366 fr.idle_timeout = rule->idle_timeout;
4367 fr.hard_timeout = rule->hard_timeout;
4368 ovs_mutex_unlock(&rule->mutex);
4369 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
4370 &fr.byte_count, &used);
4372 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
4375 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4376 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4379 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
4382 * ofproto implementation ->run() functions should use this function to expire
4383 * OpenFlow flows. */
4385 ofproto_rule_expire(struct rule *rule, uint8_t reason)
4386 OVS_REQUIRES(ofproto_mutex)
4388 struct ofproto *ofproto = rule->ofproto;
4390 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
4391 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
4393 ofproto_rule_delete__(ofproto, rule, reason);
4396 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4397 * means "infinite". */
4399 reduce_timeout(uint16_t max, uint16_t *timeout)
4401 if (max && (!*timeout || *timeout > max)) {
4406 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4407 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4408 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4410 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4412 ofproto_rule_reduce_timeouts(struct rule *rule,
4413 uint16_t idle_timeout, uint16_t hard_timeout)
4414 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
4416 if (!idle_timeout && !hard_timeout) {
4420 ovs_mutex_lock(&ofproto_mutex);
4421 if (list_is_empty(&rule->expirable)) {
4422 list_insert(&rule->ofproto->expirable, &rule->expirable);
4424 ovs_mutex_unlock(&ofproto_mutex);
4426 ovs_mutex_lock(&rule->mutex);
4427 reduce_timeout(idle_timeout, &rule->idle_timeout);
4428 reduce_timeout(hard_timeout, &rule->hard_timeout);
4429 ovs_mutex_unlock(&rule->mutex);
4433 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4434 OVS_EXCLUDED(ofproto_mutex)
4436 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4437 struct ofputil_flow_mod fm;
4438 uint64_t ofpacts_stub[1024 / 8];
4439 struct ofpbuf ofpacts;
4443 error = reject_slave_controller(ofconn);
4448 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4449 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
4451 u16_to_ofp(ofproto->max_ports),
4454 error = ofproto_check_ofpacts(ofproto, fm.ofpacts, fm.ofpacts_len);
4457 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
4460 goto exit_free_ofpacts;
4463 /* Record the operation for logging a summary report. */
4464 switch (fm.command) {
4470 case OFPFC_MODIFY_STRICT:
4471 ofproto->n_modify++;
4475 case OFPFC_DELETE_STRICT:
4476 ofproto->n_delete++;
4481 if (ofproto->next_op_report == LLONG_MAX) {
4482 ofproto->first_op = now;
4483 ofproto->next_op_report = MAX(now + 10 * 1000,
4484 ofproto->op_backoff);
4485 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
4487 ofproto->last_op = now;
4490 ofpbuf_uninit(&ofpacts);
4496 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
4497 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
4498 OVS_EXCLUDED(ofproto_mutex)
4502 ovs_mutex_lock(&ofproto_mutex);
4503 if (ofproto->n_pending < 50) {
4504 switch (fm->command) {
4506 error = add_flow(ofproto, ofconn, fm, oh);
4510 error = modify_flows_loose(ofproto, ofconn, fm, oh);
4513 case OFPFC_MODIFY_STRICT:
4514 error = modify_flow_strict(ofproto, ofconn, fm, oh);
4518 error = delete_flows_loose(ofproto, ofconn, fm, oh);
4521 case OFPFC_DELETE_STRICT:
4522 error = delete_flow_strict(ofproto, ofconn, fm, oh);
4526 if (fm->command > 0xff) {
4527 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4528 "flow_mod_table_id extension is not enabled",
4531 error = OFPERR_OFPFMFC_BAD_COMMAND;
4535 ovs_assert(!list_is_empty(&ofproto->pending));
4536 error = OFPROTO_POSTPONE;
4538 ovs_mutex_unlock(&ofproto_mutex);
4540 run_rule_executes(ofproto);
4545 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4547 struct ofputil_role_request request;
4548 struct ofputil_role_request reply;
4552 error = ofputil_decode_role_message(oh, &request);
4557 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4558 if (ofconn_get_role(ofconn) != request.role
4559 && ofconn_has_pending_opgroups(ofconn)) {
4560 return OFPROTO_POSTPONE;
4563 if (request.have_generation_id
4564 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4565 return OFPERR_OFPRRFC_STALE;
4568 ofconn_set_role(ofconn, request.role);
4571 reply.role = ofconn_get_role(ofconn);
4572 reply.have_generation_id = ofconn_get_master_election_id(
4573 ofconn, &reply.generation_id);
4574 buf = ofputil_encode_role_reply(oh, &reply);
4575 ofconn_send_reply(ofconn, buf);
4581 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4582 const struct ofp_header *oh)
4584 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4585 enum ofputil_protocol cur, next;
4587 cur = ofconn_get_protocol(ofconn);
4588 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4589 ofconn_set_protocol(ofconn, next);
4595 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4597 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4598 enum ofputil_protocol cur, next;
4599 enum ofputil_protocol next_base;
4601 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4603 return OFPERR_OFPBRC_EPERM;
4606 cur = ofconn_get_protocol(ofconn);
4607 next = ofputil_protocol_set_base(cur, next_base);
4608 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4609 /* Avoid sending async messages in surprising protocol. */
4610 return OFPROTO_POSTPONE;
4613 ofconn_set_protocol(ofconn, next);
4618 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4619 const struct ofp_header *oh)
4621 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4624 format = ntohl(msg->format);
4625 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4626 return OFPERR_OFPBRC_EPERM;
4629 if (format != ofconn_get_packet_in_format(ofconn)
4630 && ofconn_has_pending_opgroups(ofconn)) {
4631 /* Avoid sending async message in surprsing packet in format. */
4632 return OFPROTO_POSTPONE;
4635 ofconn_set_packet_in_format(ofconn, format);
4640 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4642 const struct nx_async_config *msg = ofpmsg_body(oh);
4643 uint32_t master[OAM_N_TYPES];
4644 uint32_t slave[OAM_N_TYPES];
4646 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4647 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4648 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4650 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4651 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4652 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4654 ofconn_set_async_config(ofconn, master, slave);
4655 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4656 !ofconn_get_miss_send_len(ofconn)) {
4657 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4664 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4667 uint32_t master[OAM_N_TYPES];
4668 uint32_t slave[OAM_N_TYPES];
4669 struct nx_async_config *msg;
4671 ofconn_get_async_config(ofconn, master, slave);
4672 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4673 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4675 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4676 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4677 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4679 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4680 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4681 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4683 ofconn_send_reply(ofconn, buf);
4689 handle_nxt_set_controller_id(struct ofconn *ofconn,
4690 const struct ofp_header *oh)
4692 const struct nx_controller_id *nci = ofpmsg_body(oh);
4694 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4695 return OFPERR_NXBRC_MUST_BE_ZERO;
4698 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4703 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4707 if (ofconn_has_pending_opgroups(ofconn)) {
4708 return OFPROTO_POSTPONE;
4711 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4712 ? OFPRAW_OFPT10_BARRIER_REPLY
4713 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4714 ofconn_send_reply(ofconn, buf);
4719 ofproto_compose_flow_refresh_update(const struct rule *rule,
4720 enum nx_flow_monitor_flags flags,
4722 OVS_REQUIRES(ofproto_mutex)
4724 struct ofoperation *op = rule->pending;
4725 const struct rule_actions *actions;
4726 struct ofputil_flow_update fu;
4729 if (op && op->type == OFOPERATION_ADD) {
4730 /* We'll report the final flow when the operation completes. Reporting
4731 * it now would cause a duplicate report later. */
4735 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4736 ? NXFME_ADDED : NXFME_MODIFIED);
4738 ovs_mutex_lock(&rule->mutex);
4739 fu.idle_timeout = rule->idle_timeout;
4740 fu.hard_timeout = rule->hard_timeout;
4741 ovs_mutex_unlock(&rule->mutex);
4742 fu.table_id = rule->table_id;
4743 fu.cookie = rule->flow_cookie;
4744 minimatch_expand(&rule->cr.match, &match);
4746 fu.priority = rule->cr.priority;
4748 if (!(flags & NXFMF_ACTIONS)) {
4751 actions = rule->actions;
4753 /* An operation is in progress. Use the previous version of the flow's
4754 * actions, so that when the operation commits we report the change. */
4756 case OFOPERATION_ADD:
4759 case OFOPERATION_MODIFY:
4760 case OFOPERATION_REPLACE:
4761 actions = op->actions ? op->actions : rule->actions;
4764 case OFOPERATION_DELETE:
4765 actions = rule->actions;
4772 fu.ofpacts = actions ? actions->ofpacts : NULL;
4773 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
4775 if (list_is_empty(msgs)) {
4776 ofputil_start_flow_update(msgs);
4778 ofputil_append_flow_update(&fu, msgs);
4782 ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4784 OVS_REQUIRES(ofproto_mutex)
4788 for (i = 0; i < rules->n; i++) {
4789 struct rule *rule = rules->rules[i];
4790 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4791 rule->monitor_flags = 0;
4793 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4798 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4799 struct rule *rule, uint64_t seqno,
4800 struct rule_collection *rules)
4801 OVS_REQUIRES(ofproto_mutex)
4803 enum nx_flow_monitor_flags update;
4805 if (ofproto_rule_is_hidden(rule)) {
4810 ? ofoperation_has_out_port(rule->pending, m->out_port)
4811 : ofproto_rule_has_out_port(rule, m->out_port))) {
4816 if (rule->add_seqno > seqno) {
4817 update = NXFMF_ADD | NXFMF_MODIFY;
4818 } else if (rule->modify_seqno > seqno) {
4819 update = NXFMF_MODIFY;
4824 if (!(m->flags & update)) {
4828 update = NXFMF_INITIAL;
4831 if (!rule->monitor_flags) {
4832 rule_collection_add(rules, rule);
4834 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4838 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4840 struct rule_collection *rules)
4841 OVS_REQUIRES(ofproto_mutex)
4843 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4844 const struct ofoperation *op;
4845 const struct oftable *table;
4846 struct cls_rule target;
4848 cls_rule_init_from_minimatch(&target, &m->match, 0);
4849 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4850 struct cls_cursor cursor;
4853 fat_rwlock_rdlock(&table->cls.rwlock);
4854 cls_cursor_init(&cursor, &table->cls, &target);
4855 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4856 ovs_assert(!rule->pending); /* XXX */
4857 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4859 fat_rwlock_unlock(&table->cls.rwlock);
4862 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4863 struct rule *rule = op->rule;
4865 if (((m->table_id == 0xff
4866 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4867 : m->table_id == rule->table_id))
4868 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4869 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4872 cls_rule_destroy(&target);
4876 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4877 struct rule_collection *rules)
4878 OVS_REQUIRES(ofproto_mutex)
4880 if (m->flags & NXFMF_INITIAL) {
4881 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4886 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4887 uint64_t seqno, struct rule_collection *rules)
4888 OVS_REQUIRES(ofproto_mutex)
4890 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4894 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4895 OVS_EXCLUDED(ofproto_mutex)
4897 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4898 struct ofmonitor **monitors;
4899 size_t n_monitors, allocated_monitors;
4900 struct rule_collection rules;
4901 struct list replies;
4907 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4909 n_monitors = allocated_monitors = 0;
4911 ovs_mutex_lock(&ofproto_mutex);
4913 struct ofputil_flow_monitor_request request;
4914 struct ofmonitor *m;
4917 retval = ofputil_decode_flow_monitor_request(&request, &b);
4918 if (retval == EOF) {
4920 } else if (retval) {
4925 if (request.table_id != 0xff
4926 && request.table_id >= ofproto->n_tables) {
4927 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4931 error = ofmonitor_create(&request, ofconn, &m);
4936 if (n_monitors >= allocated_monitors) {
4937 monitors = x2nrealloc(monitors, &allocated_monitors,
4940 monitors[n_monitors++] = m;
4943 rule_collection_init(&rules);
4944 for (i = 0; i < n_monitors; i++) {
4945 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4948 ofpmp_init(&replies, oh);
4949 ofmonitor_compose_refresh_updates(&rules, &replies);
4950 ovs_mutex_unlock(&ofproto_mutex);
4952 rule_collection_destroy(&rules);
4954 ofconn_send_replies(ofconn, &replies);
4960 for (i = 0; i < n_monitors; i++) {
4961 ofmonitor_destroy(monitors[i]);
4964 ovs_mutex_unlock(&ofproto_mutex);
4970 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4971 OVS_EXCLUDED(ofproto_mutex)
4973 struct ofmonitor *m;
4977 id = ofputil_decode_flow_monitor_cancel(oh);
4979 ovs_mutex_lock(&ofproto_mutex);
4980 m = ofmonitor_lookup(ofconn, id);
4982 ofmonitor_destroy(m);
4985 error = OFPERR_NXBRC_FM_BAD_ID;
4987 ovs_mutex_unlock(&ofproto_mutex);
4992 /* Meters implementation.
4994 * Meter table entry, indexed by the OpenFlow meter_id.
4995 * These are always dynamically allocated to allocate enough space for
4997 * 'created' is used to compute the duration for meter stats.
4998 * 'list rules' is needed so that we can delete the dependent rules when the
4999 * meter table entry is deleted.
5000 * 'provider_meter_id' is for the provider's private use.
5003 long long int created; /* Time created. */
5004 struct list rules; /* List of "struct rule_dpif"s. */
5005 ofproto_meter_id provider_meter_id;
5006 uint16_t flags; /* Meter flags. */
5007 uint16_t n_bands; /* Number of meter bands. */
5008 struct ofputil_meter_band *bands;
5012 * This is used in instruction validation at flow set-up time,
5013 * as flows may not use non-existing meters.
5014 * Return value of UINT32_MAX signifies an invalid meter.
5017 get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id)
5019 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
5020 const struct meter *meter = ofproto->meters[of_meter_id];
5022 return meter->provider_meter_id.uint32;
5029 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
5033 meter->flags = config->flags;
5034 meter->n_bands = config->n_bands;
5035 meter->bands = xmemdup(config->bands,
5036 config->n_bands * sizeof *meter->bands);
5039 static struct meter *
5040 meter_create(const struct ofputil_meter_config *config,
5041 ofproto_meter_id provider_meter_id)
5043 struct meter *meter;
5045 meter = xzalloc(sizeof *meter);
5046 meter->provider_meter_id = provider_meter_id;
5047 meter->created = time_msec();
5048 list_init(&meter->rules);
5050 meter_update(meter, config);
5056 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
5057 OVS_REQUIRES(ofproto_mutex)
5060 for (mid = first; mid <= last; ++mid) {
5061 struct meter *meter = ofproto->meters[mid];
5063 ofproto->meters[mid] = NULL;
5064 ofproto->ofproto_class->meter_del(ofproto,
5065 meter->provider_meter_id);
5073 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5075 ofproto_meter_id provider_meter_id = { UINT32_MAX };
5076 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
5080 return OFPERR_OFPMMFC_METER_EXISTS;
5083 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
5086 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
5087 *meterp = meter_create(&mm->meter, provider_meter_id);
5093 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5095 struct meter *meter = ofproto->meters[mm->meter.meter_id];
5097 uint32_t provider_meter_id;
5100 return OFPERR_OFPMMFC_UNKNOWN_METER;
5103 provider_meter_id = meter->provider_meter_id.uint32;
5104 error = ofproto->ofproto_class->meter_set(ofproto,
5105 &meter->provider_meter_id,
5107 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
5109 meter_update(meter, &mm->meter);
5115 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
5116 struct ofputil_meter_mod *mm)
5117 OVS_EXCLUDED(ofproto_mutex)
5119 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5120 uint32_t meter_id = mm->meter.meter_id;
5121 struct rule_collection rules;
5122 enum ofperr error = 0;
5123 uint32_t first, last;
5125 if (meter_id == OFPM13_ALL) {
5127 last = ofproto->meter_features.max_meters;
5129 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
5132 first = last = meter_id;
5135 /* First delete the rules that use this meter. If any of those rules are
5136 * currently being modified, postpone the whole operation until later. */
5137 rule_collection_init(&rules);
5138 ovs_mutex_lock(&ofproto_mutex);
5139 for (meter_id = first; meter_id <= last; ++meter_id) {
5140 struct meter *meter = ofproto->meters[meter_id];
5141 if (meter && !list_is_empty(&meter->rules)) {
5144 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
5145 if (rule->pending) {
5146 error = OFPROTO_POSTPONE;
5149 rule_collection_add(&rules, rule);
5154 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
5157 /* Delete the meters. */
5158 meter_delete(ofproto, first, last);
5161 ovs_mutex_unlock(&ofproto_mutex);
5162 rule_collection_destroy(&rules);
5168 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5170 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5171 struct ofputil_meter_mod mm;
5172 uint64_t bands_stub[256 / 8];
5173 struct ofpbuf bands;
5177 error = reject_slave_controller(ofconn);
5182 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5184 error = ofputil_decode_meter_mod(oh, &mm, &bands);
5186 goto exit_free_bands;
5189 meter_id = mm.meter.meter_id;
5191 if (mm.command != OFPMC13_DELETE) {
5192 /* Fails also when meters are not implemented by the provider. */
5193 if (meter_id == 0 || meter_id > OFPM13_MAX) {
5194 error = OFPERR_OFPMMFC_INVALID_METER;
5195 goto exit_free_bands;
5196 } else if (meter_id > ofproto->meter_features.max_meters) {
5197 error = OFPERR_OFPMMFC_OUT_OF_METERS;
5198 goto exit_free_bands;
5200 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
5201 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
5202 goto exit_free_bands;
5206 switch (mm.command) {
5208 error = handle_add_meter(ofproto, &mm);
5211 case OFPMC13_MODIFY:
5212 error = handle_modify_meter(ofproto, &mm);
5215 case OFPMC13_DELETE:
5216 error = handle_delete_meter(ofconn, oh, &mm);
5220 error = OFPERR_OFPMMFC_BAD_COMMAND;
5225 ofpbuf_uninit(&bands);
5230 handle_meter_features_request(struct ofconn *ofconn,
5231 const struct ofp_header *request)
5233 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5234 struct ofputil_meter_features features;
5237 if (ofproto->ofproto_class->meter_get_features) {
5238 ofproto->ofproto_class->meter_get_features(ofproto, &features);
5240 memset(&features, 0, sizeof features);
5242 b = ofputil_encode_meter_features_reply(&features, request);
5244 ofconn_send_reply(ofconn, b);
5249 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
5252 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5253 struct list replies;
5254 uint64_t bands_stub[256 / 8];
5255 struct ofpbuf bands;
5256 uint32_t meter_id, first, last;
5258 ofputil_decode_meter_request(request, &meter_id);
5260 if (meter_id == OFPM13_ALL) {
5262 last = ofproto->meter_features.max_meters;
5264 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
5265 !ofproto->meters[meter_id]) {
5266 return OFPERR_OFPMMFC_UNKNOWN_METER;
5268 first = last = meter_id;
5271 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5272 ofpmp_init(&replies, request);
5274 for (meter_id = first; meter_id <= last; ++meter_id) {
5275 struct meter *meter = ofproto->meters[meter_id];
5277 continue; /* Skip non-existing meters. */
5279 if (type == OFPTYPE_METER_STATS_REQUEST) {
5280 struct ofputil_meter_stats stats;
5282 stats.meter_id = meter_id;
5284 /* Provider sets the packet and byte counts, we do the rest. */
5285 stats.flow_count = list_size(&meter->rules);
5286 calc_duration(meter->created, time_msec(),
5287 &stats.duration_sec, &stats.duration_nsec);
5288 stats.n_bands = meter->n_bands;
5289 ofpbuf_clear(&bands);
5291 = ofpbuf_put_uninit(&bands,
5292 meter->n_bands * sizeof *stats.bands);
5294 if (!ofproto->ofproto_class->meter_get(ofproto,
5295 meter->provider_meter_id,
5297 ofputil_append_meter_stats(&replies, &stats);
5299 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
5300 struct ofputil_meter_config config;
5302 config.meter_id = meter_id;
5303 config.flags = meter->flags;
5304 config.n_bands = meter->n_bands;
5305 config.bands = meter->bands;
5306 ofputil_append_meter_config(&replies, &config);
5310 ofconn_send_replies(ofconn, &replies);
5311 ofpbuf_uninit(&bands);
5316 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5317 struct ofgroup **group)
5318 OVS_TRY_RDLOCK(true, (*group)->rwlock)
5320 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5321 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5322 hash_int(group_id, 0), &ofproto->groups) {
5323 if ((*group)->group_id == group_id) {
5324 ovs_rwlock_rdlock(&(*group)->rwlock);
5325 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5329 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5334 ofproto_group_release(struct ofgroup *group)
5335 OVS_RELEASES(group->rwlock)
5337 ovs_rwlock_unlock(&group->rwlock);
5341 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
5342 struct ofgroup **group)
5343 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
5344 OVS_TRY_WRLOCK(true, (*group)->rwlock)
5346 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5347 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5348 hash_int(group_id, 0), &ofproto->groups) {
5349 if ((*group)->group_id == group_id) {
5350 ovs_rwlock_wrlock(&(*group)->rwlock);
5354 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5359 ofproto_group_exists__(const struct ofproto *ofproto, uint32_t group_id)
5360 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5362 struct ofgroup *grp;
5364 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
5365 hash_int(group_id, 0), &ofproto->groups) {
5366 if (grp->group_id == group_id) {
5374 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
5375 OVS_EXCLUDED(ofproto->groups_rwlock)
5379 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5380 exists = ofproto_group_exists__(ofproto, group_id);
5381 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5387 group_get_ref_count(struct ofgroup *group)
5388 OVS_EXCLUDED(ofproto_mutex)
5390 struct ofproto *ofproto = group->ofproto;
5391 struct rule_criteria criteria;
5392 struct rule_collection rules;
5397 match_init_catchall(&match);
5398 rule_criteria_init(&criteria, 0xff, &match, 0, htonll(0), htonll(0),
5399 OFPP_ANY, group->group_id);
5400 ovs_mutex_lock(&ofproto_mutex);
5401 error = collect_rules_loose(ofproto, &criteria, &rules);
5402 ovs_mutex_unlock(&ofproto_mutex);
5403 rule_criteria_destroy(&criteria);
5405 count = !error && rules.n < UINT32_MAX ? rules.n : UINT32_MAX;
5407 rule_collection_destroy(&rules);
5412 append_group_stats(struct ofgroup *group, struct list *replies)
5413 OVS_REQ_RDLOCK(group->rwlock)
5415 struct ofputil_group_stats ogs;
5416 struct ofproto *ofproto = group->ofproto;
5417 long long int now = time_msec();
5420 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
5422 /* Provider sets the packet and byte counts, we do the rest. */
5423 ogs.ref_count = group_get_ref_count(group);
5424 ogs.n_buckets = group->n_buckets;
5426 error = (ofproto->ofproto_class->group_get_stats
5427 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
5430 ogs.packet_count = UINT64_MAX;
5431 ogs.byte_count = UINT64_MAX;
5432 memset(ogs.bucket_stats, 0xff,
5433 ogs.n_buckets * sizeof *ogs.bucket_stats);
5436 ogs.group_id = group->group_id;
5437 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
5439 ofputil_append_group_stats(replies, &ogs);
5441 free(ogs.bucket_stats);
5445 handle_group_stats_request(struct ofconn *ofconn,
5446 const struct ofp_header *request)
5448 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5449 struct list replies;
5451 struct ofgroup *group;
5454 error = ofputil_decode_group_stats_request(request, &group_id);
5459 ofpmp_init(&replies, request);
5461 if (group_id == OFPG_ALL) {
5462 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5463 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5464 ovs_rwlock_rdlock(&group->rwlock);
5465 append_group_stats(group, &replies);
5466 ovs_rwlock_unlock(&group->rwlock);
5468 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5470 if (ofproto_group_lookup(ofproto, group_id, &group)) {
5471 append_group_stats(group, &replies);
5472 ofproto_group_release(group);
5476 ofconn_send_replies(ofconn, &replies);
5482 handle_group_desc_stats_request(struct ofconn *ofconn,
5483 const struct ofp_header *request)
5485 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5486 struct list replies;
5487 struct ofputil_group_desc gds;
5488 struct ofgroup *group;
5490 ofpmp_init(&replies, request);
5492 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5493 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5494 gds.group_id = group->group_id;
5495 gds.type = group->type;
5496 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
5498 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5500 ofconn_send_replies(ofconn, &replies);
5506 handle_group_features_stats_request(struct ofconn *ofconn,
5507 const struct ofp_header *request)
5509 struct ofproto *p = ofconn_get_ofproto(ofconn);
5512 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5514 ofconn_send_reply(ofconn, msg);
5521 handle_queue_get_config_request(struct ofconn *ofconn,
5522 const struct ofp_header *oh)
5524 struct ofproto *p = ofconn_get_ofproto(ofconn);
5525 struct netdev_queue_dump queue_dump;
5526 struct ofport *ofport;
5527 unsigned int queue_id;
5528 struct ofpbuf *reply;
5529 struct smap details;
5533 error = ofputil_decode_queue_get_config_request(oh, &request);
5538 ofport = ofproto_get_port(p, request);
5540 return OFPERR_OFPQOFC_BAD_PORT;
5543 reply = ofputil_encode_queue_get_config_reply(oh);
5545 smap_init(&details);
5546 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, ofport->netdev) {
5547 struct ofputil_queue_config queue;
5549 /* None of the existing queues have compatible properties, so we
5550 * hard-code omitting min_rate and max_rate. */
5551 queue.queue_id = queue_id;
5552 queue.min_rate = UINT16_MAX;
5553 queue.max_rate = UINT16_MAX;
5554 ofputil_append_queue_get_config_reply(reply, &queue);
5556 smap_destroy(&details);
5558 ofconn_send_reply(ofconn, reply);
5563 /* Implements OFPGC11_ADD
5564 * in which no matching flow already exists in the flow table.
5566 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
5567 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
5568 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
5569 * initiated now but may be retried later.
5571 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
5572 * ownership remains with the caller.
5574 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5577 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5579 struct ofgroup *ofgroup;
5582 if (gm->group_id > OFPG_MAX) {
5583 return OFPERR_OFPGMFC_INVALID_GROUP;
5585 if (gm->type > OFPGT11_FF) {
5586 return OFPERR_OFPGMFC_BAD_TYPE;
5589 /* Allocate new group and initialize it. */
5590 ofgroup = ofproto->ofproto_class->group_alloc();
5592 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
5593 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5596 ovs_rwlock_init(&ofgroup->rwlock);
5597 ofgroup->ofproto = ofproto;
5598 ofgroup->group_id = gm->group_id;
5599 ofgroup->type = gm->type;
5600 ofgroup->created = ofgroup->modified = time_msec();
5602 list_move(&ofgroup->buckets, &gm->buckets);
5603 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5605 /* Construct called BEFORE any locks are held. */
5606 error = ofproto->ofproto_class->group_construct(ofgroup);
5611 /* We wrlock as late as possible to minimize the time we jam any other
5612 * threads: No visible state changes before acquiring the lock. */
5613 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5615 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5616 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5620 if (ofproto_group_exists__(ofproto, gm->group_id)) {
5621 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5626 /* Insert new group. */
5627 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5628 hash_int(ofgroup->group_id, 0));
5629 ofproto->n_groups[ofgroup->type]++;
5631 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5636 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5637 ofproto->ofproto_class->group_destruct(ofgroup);
5639 ofputil_bucket_list_destroy(&ofgroup->buckets);
5640 ofproto->ofproto_class->group_dealloc(ofgroup);
5645 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5648 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5651 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5653 struct ofgroup *ofgroup;
5654 struct ofgroup *victim;
5657 if (gm->group_id > OFPG_MAX) {
5658 return OFPERR_OFPGMFC_INVALID_GROUP;
5661 if (gm->type > OFPGT11_FF) {
5662 return OFPERR_OFPGMFC_BAD_TYPE;
5665 victim = ofproto->ofproto_class->group_alloc();
5667 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5668 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5671 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
5672 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5675 /* Both group's and its container's write locks held now.
5676 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5677 if (ofgroup->type != gm->type
5678 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5679 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5684 list_move(&victim->buckets, &ofgroup->buckets);
5686 ofgroup->type = gm->type;
5687 list_move(&ofgroup->buckets, &gm->buckets);
5688 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5690 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
5692 ofputil_bucket_list_destroy(&victim->buckets);
5693 ofproto->n_groups[victim->type]--;
5694 ofproto->n_groups[ofgroup->type]++;
5695 ofgroup->modified = time_msec();
5697 ofputil_bucket_list_destroy(&ofgroup->buckets);
5700 list_move(&ofgroup->buckets, &victim->buckets);
5704 ovs_rwlock_unlock(&ofgroup->rwlock);
5705 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5707 ofproto->ofproto_class->group_dealloc(victim);
5712 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5713 OVS_RELEASES(ofproto->groups_rwlock)
5716 struct ofputil_flow_mod fm;
5718 /* Delete all flow entries containing this group in a group action */
5719 match_init_catchall(&match);
5720 flow_mod_init(&fm, &match, 0, NULL, 0, OFPFC_DELETE);
5721 fm.out_group = ofgroup->group_id;
5722 handle_flow_mod__(ofproto, NULL, &fm, NULL);
5724 /* Must wait until existing readers are done,
5725 * while holding the container's write lock at the same time. */
5726 ovs_rwlock_wrlock(&ofgroup->rwlock);
5727 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5728 /* No-one can find this group any more. */
5729 ofproto->n_groups[ofgroup->type]--;
5730 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5732 ofproto->ofproto_class->group_destruct(ofgroup);
5733 ofputil_bucket_list_destroy(&ofgroup->buckets);
5734 ovs_rwlock_unlock(&ofgroup->rwlock);
5735 ovs_rwlock_destroy(&ofgroup->rwlock);
5736 ofproto->ofproto_class->group_dealloc(ofgroup);
5739 /* Implements OFPGC_DELETE. */
5741 delete_group(struct ofproto *ofproto, uint32_t group_id)
5743 struct ofgroup *ofgroup;
5745 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5746 if (group_id == OFPG_ALL) {
5748 struct hmap_node *node = hmap_first(&ofproto->groups);
5752 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5753 delete_group__(ofproto, ofgroup);
5754 /* Lock for each node separately, so that we will not jam the
5755 * other threads for too long time. */
5756 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5759 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5760 hash_int(group_id, 0), &ofproto->groups) {
5761 if (ofgroup->group_id == group_id) {
5762 delete_group__(ofproto, ofgroup);
5767 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5771 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5773 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5774 struct ofputil_group_mod gm;
5777 error = reject_slave_controller(ofconn);
5782 error = ofputil_decode_group_mod(oh, &gm);
5787 switch (gm.command) {
5789 return add_group(ofproto, &gm);
5791 case OFPGC11_MODIFY:
5792 return modify_group(ofproto, &gm);
5794 case OFPGC11_DELETE:
5795 delete_group(ofproto, gm.group_id);
5799 if (gm.command > OFPGC11_DELETE) {
5800 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5801 ofproto->name, gm.command);
5803 return OFPERR_OFPGMFC_BAD_COMMAND;
5808 table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
5810 /* XXX Reject all configurations because none are currently supported */
5811 return OFPERR_OFPTMFC_BAD_CONFIG;
5813 if (tm->table_id == OFPTT_ALL) {
5815 for (i = 0; i < ofproto->n_tables; i++) {
5816 atomic_store(&ofproto->tables[i].config,
5817 (unsigned int)tm->config);
5819 } else if (!check_table_id(ofproto, tm->table_id)) {
5820 return OFPERR_OFPTMFC_BAD_TABLE;
5822 atomic_store(&ofproto->tables[tm->table_id].config,
5823 (unsigned int)tm->config);
5830 handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5832 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5833 struct ofputil_table_mod tm;
5836 error = reject_slave_controller(ofconn);
5841 error = ofputil_decode_table_mod(oh, &tm);
5846 return table_mod(ofproto, &tm);
5850 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5851 OVS_EXCLUDED(ofproto_mutex)
5853 const struct ofp_header *oh = msg->data;
5857 error = ofptype_decode(&type, oh);
5861 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
5862 && ofpmp_more(oh)) {
5863 /* We have no buffer implementation for multipart requests.
5864 * Report overflow for requests which consists of multiple
5866 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
5870 /* OpenFlow requests. */
5871 case OFPTYPE_ECHO_REQUEST:
5872 return handle_echo_request(ofconn, oh);
5874 case OFPTYPE_FEATURES_REQUEST:
5875 return handle_features_request(ofconn, oh);
5877 case OFPTYPE_GET_CONFIG_REQUEST:
5878 return handle_get_config_request(ofconn, oh);
5880 case OFPTYPE_SET_CONFIG:
5881 return handle_set_config(ofconn, oh);
5883 case OFPTYPE_PACKET_OUT:
5884 return handle_packet_out(ofconn, oh);
5886 case OFPTYPE_PORT_MOD:
5887 return handle_port_mod(ofconn, oh);
5889 case OFPTYPE_FLOW_MOD:
5890 return handle_flow_mod(ofconn, oh);
5892 case OFPTYPE_GROUP_MOD:
5893 return handle_group_mod(ofconn, oh);
5895 case OFPTYPE_TABLE_MOD:
5896 return handle_table_mod(ofconn, oh);
5898 case OFPTYPE_METER_MOD:
5899 return handle_meter_mod(ofconn, oh);
5901 case OFPTYPE_BARRIER_REQUEST:
5902 return handle_barrier_request(ofconn, oh);
5904 case OFPTYPE_ROLE_REQUEST:
5905 return handle_role_request(ofconn, oh);
5907 /* OpenFlow replies. */
5908 case OFPTYPE_ECHO_REPLY:
5911 /* Nicira extension requests. */
5912 case OFPTYPE_FLOW_MOD_TABLE_ID:
5913 return handle_nxt_flow_mod_table_id(ofconn, oh);
5915 case OFPTYPE_SET_FLOW_FORMAT:
5916 return handle_nxt_set_flow_format(ofconn, oh);
5918 case OFPTYPE_SET_PACKET_IN_FORMAT:
5919 return handle_nxt_set_packet_in_format(ofconn, oh);
5921 case OFPTYPE_SET_CONTROLLER_ID:
5922 return handle_nxt_set_controller_id(ofconn, oh);
5924 case OFPTYPE_FLOW_AGE:
5925 /* Nothing to do. */
5928 case OFPTYPE_FLOW_MONITOR_CANCEL:
5929 return handle_flow_monitor_cancel(ofconn, oh);
5931 case OFPTYPE_SET_ASYNC_CONFIG:
5932 return handle_nxt_set_async_config(ofconn, oh);
5934 case OFPTYPE_GET_ASYNC_REQUEST:
5935 return handle_nxt_get_async_request(ofconn, oh);
5937 /* Statistics requests. */
5938 case OFPTYPE_DESC_STATS_REQUEST:
5939 return handle_desc_stats_request(ofconn, oh);
5941 case OFPTYPE_FLOW_STATS_REQUEST:
5942 return handle_flow_stats_request(ofconn, oh);
5944 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5945 return handle_aggregate_stats_request(ofconn, oh);
5947 case OFPTYPE_TABLE_STATS_REQUEST:
5948 return handle_table_stats_request(ofconn, oh);
5950 case OFPTYPE_PORT_STATS_REQUEST:
5951 return handle_port_stats_request(ofconn, oh);
5953 case OFPTYPE_QUEUE_STATS_REQUEST:
5954 return handle_queue_stats_request(ofconn, oh);
5956 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5957 return handle_port_desc_stats_request(ofconn, oh);
5959 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5960 return handle_flow_monitor_request(ofconn, oh);
5962 case OFPTYPE_METER_STATS_REQUEST:
5963 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5964 return handle_meter_request(ofconn, oh, type);
5966 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5967 return handle_meter_features_request(ofconn, oh);
5969 case OFPTYPE_GROUP_STATS_REQUEST:
5970 return handle_group_stats_request(ofconn, oh);
5972 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5973 return handle_group_desc_stats_request(ofconn, oh);
5975 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5976 return handle_group_features_stats_request(ofconn, oh);
5978 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5979 return handle_queue_get_config_request(ofconn, oh);
5983 case OFPTYPE_FEATURES_REPLY:
5984 case OFPTYPE_GET_CONFIG_REPLY:
5985 case OFPTYPE_PACKET_IN:
5986 case OFPTYPE_FLOW_REMOVED:
5987 case OFPTYPE_PORT_STATUS:
5988 case OFPTYPE_BARRIER_REPLY:
5989 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5990 case OFPTYPE_DESC_STATS_REPLY:
5991 case OFPTYPE_FLOW_STATS_REPLY:
5992 case OFPTYPE_QUEUE_STATS_REPLY:
5993 case OFPTYPE_PORT_STATS_REPLY:
5994 case OFPTYPE_TABLE_STATS_REPLY:
5995 case OFPTYPE_AGGREGATE_STATS_REPLY:
5996 case OFPTYPE_PORT_DESC_STATS_REPLY:
5997 case OFPTYPE_ROLE_REPLY:
5998 case OFPTYPE_FLOW_MONITOR_PAUSED:
5999 case OFPTYPE_FLOW_MONITOR_RESUMED:
6000 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
6001 case OFPTYPE_GET_ASYNC_REPLY:
6002 case OFPTYPE_GROUP_STATS_REPLY:
6003 case OFPTYPE_GROUP_DESC_STATS_REPLY:
6004 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
6005 case OFPTYPE_METER_STATS_REPLY:
6006 case OFPTYPE_METER_CONFIG_STATS_REPLY:
6007 case OFPTYPE_METER_FEATURES_STATS_REPLY:
6008 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
6009 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
6010 case OFPTYPE_ROLE_STATUS:
6012 if (ofpmsg_is_stat_request(oh)) {
6013 return OFPERR_OFPBRC_BAD_STAT;
6015 return OFPERR_OFPBRC_BAD_TYPE;
6021 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
6022 OVS_EXCLUDED(ofproto_mutex)
6024 int error = handle_openflow__(ofconn, ofp_msg);
6025 if (error && error != OFPROTO_POSTPONE) {
6026 ofconn_send_error(ofconn, ofp_msg->data, error);
6028 COVERAGE_INC(ofproto_recv_openflow);
6029 return error != OFPROTO_POSTPONE;
6032 /* Asynchronous operations. */
6034 /* Creates and returns a new ofopgroup that is not associated with any
6035 * OpenFlow connection.
6037 * The caller should add operations to the returned group with
6038 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6039 static struct ofopgroup *
6040 ofopgroup_create_unattached(struct ofproto *ofproto)
6041 OVS_REQUIRES(ofproto_mutex)
6043 struct ofopgroup *group = xzalloc(sizeof *group);
6044 group->ofproto = ofproto;
6045 list_init(&group->ofproto_node);
6046 list_init(&group->ops);
6047 list_init(&group->ofconn_node);
6051 /* Creates and returns a new ofopgroup for 'ofproto'.
6053 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
6054 * connection. The 'request' and 'buffer_id' arguments are ignored.
6056 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
6057 * If the ofopgroup eventually fails, then the error reply will include
6058 * 'request'. If the ofopgroup eventually succeeds, then the packet with
6059 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
6061 * The caller should add operations to the returned group with
6062 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6063 static struct ofopgroup *
6064 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
6065 const struct ofp_header *request, uint32_t buffer_id)
6066 OVS_REQUIRES(ofproto_mutex)
6068 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
6070 size_t request_len = ntohs(request->length);
6072 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
6074 ofconn_add_opgroup(ofconn, &group->ofconn_node);
6075 group->ofconn = ofconn;
6076 group->request = xmemdup(request, MIN(request_len, 64));
6077 group->buffer_id = buffer_id;
6082 /* Submits 'group' for processing.
6084 * If 'group' contains no operations (e.g. none were ever added, or all of the
6085 * ones that were added completed synchronously), then it is destroyed
6086 * immediately. Otherwise it is added to the ofproto's list of pending
6089 ofopgroup_submit(struct ofopgroup *group)
6090 OVS_REQUIRES(ofproto_mutex)
6092 if (!group->n_running) {
6093 ofopgroup_complete(group);
6095 list_push_back(&group->ofproto->pending, &group->ofproto_node);
6096 group->ofproto->n_pending++;
6101 ofopgroup_complete(struct ofopgroup *group)
6102 OVS_REQUIRES(ofproto_mutex)
6104 struct ofproto *ofproto = group->ofproto;
6106 struct ofconn *abbrev_ofconn;
6107 ovs_be32 abbrev_xid;
6109 struct ofoperation *op, *next_op;
6112 ovs_assert(!group->n_running);
6115 LIST_FOR_EACH (op, group_node, &group->ops) {
6122 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
6123 LIST_FOR_EACH (op, group_node, &group->ops) {
6124 if (op->type != OFOPERATION_DELETE) {
6125 struct ofpbuf *packet;
6128 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
6131 struct rule_execute *re;
6135 ofproto_rule_ref(op->rule);
6137 re = xmalloc(sizeof *re);
6138 re->rule = op->rule;
6139 re->in_port = in_port;
6140 re->packet = packet;
6142 if (!guarded_list_push_back(&ofproto->rule_executes,
6143 &re->list_node, 1024)) {
6144 ofproto_rule_unref(op->rule);
6145 ofpbuf_delete(re->packet);
6154 if (!error && !list_is_empty(&group->ofconn_node)) {
6155 abbrev_ofconn = group->ofconn;
6156 abbrev_xid = group->request->xid;
6158 abbrev_ofconn = NULL;
6159 abbrev_xid = htonl(0);
6161 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
6162 struct rule *rule = op->rule;
6164 /* We generally want to report the change to active OpenFlow flow
6165 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
6167 - The operation failed.
6169 - The affected rule is not visible to controllers.
6171 - The operation's only effect was to update rule->modified. */
6173 || ofproto_rule_is_hidden(rule)
6174 || (op->type == OFOPERATION_MODIFY
6176 && rule->flow_cookie == op->flow_cookie))) {
6177 /* Check that we can just cast from ofoperation_type to
6178 * nx_flow_update_event. */
6179 enum nx_flow_update_event event_type;
6182 case OFOPERATION_ADD:
6183 case OFOPERATION_REPLACE:
6184 event_type = NXFME_ADDED;
6187 case OFOPERATION_DELETE:
6188 event_type = NXFME_DELETED;
6191 case OFOPERATION_MODIFY:
6192 event_type = NXFME_MODIFIED;
6199 ofmonitor_report(ofproto->connmgr, rule, event_type,
6200 op->reason, abbrev_ofconn, abbrev_xid);
6203 rule->pending = NULL;
6206 case OFOPERATION_ADD:
6210 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
6211 if (vid_mask == VLAN_VID_MASK) {
6212 if (ofproto->vlan_bitmap) {
6213 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
6214 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
6215 bitmap_set1(ofproto->vlan_bitmap, vid);
6216 ofproto->vlans_changed = true;
6219 ofproto->vlans_changed = true;
6223 oftable_remove_rule(rule);
6224 ofproto_rule_unref(rule);
6228 case OFOPERATION_DELETE:
6229 ovs_assert(!op->error);
6230 ofproto_rule_unref(rule);
6234 case OFOPERATION_MODIFY:
6235 case OFOPERATION_REPLACE:
6237 long long int now = time_msec();
6239 ovs_mutex_lock(&rule->mutex);
6240 rule->modified = now;
6241 if (op->type == OFOPERATION_REPLACE) {
6242 rule->created = now;
6244 ovs_mutex_unlock(&rule->mutex);
6246 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
6247 ovs_mutex_lock(&rule->mutex);
6248 rule->idle_timeout = op->idle_timeout;
6249 rule->hard_timeout = op->hard_timeout;
6250 ovs_mutex_unlock(&rule->mutex);
6252 struct rule_actions *old_actions;
6254 ovs_mutex_lock(&rule->mutex);
6255 old_actions = rule->actions;
6256 rule->actions = op->actions;
6257 ovs_mutex_unlock(&rule->mutex);
6260 rule_actions_unref(old_actions);
6262 rule->flags = op->flags;
6270 ofoperation_destroy(op);
6273 ofmonitor_flush(ofproto->connmgr);
6275 if (!list_is_empty(&group->ofproto_node)) {
6276 ovs_assert(ofproto->n_pending > 0);
6277 ofproto->n_pending--;
6278 list_remove(&group->ofproto_node);
6280 if (!list_is_empty(&group->ofconn_node)) {
6281 list_remove(&group->ofconn_node);
6283 ofconn_send_error(group->ofconn, group->request, error);
6285 connmgr_retry(ofproto->connmgr);
6287 free(group->request);
6291 /* Initiates a new operation on 'rule', of the specified 'type', within
6292 * 'group'. Prior to calling, 'rule' must not have any pending operation.
6294 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
6295 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
6297 * Returns the newly created ofoperation (which is also available as
6298 * rule->pending). */
6299 static struct ofoperation *
6300 ofoperation_create(struct ofopgroup *group, struct rule *rule,
6301 enum ofoperation_type type,
6302 enum ofp_flow_removed_reason reason)
6303 OVS_REQUIRES(ofproto_mutex)
6305 struct ofproto *ofproto = group->ofproto;
6306 struct ofoperation *op;
6308 ovs_assert(!rule->pending);
6310 op = rule->pending = xzalloc(sizeof *op);
6312 list_push_back(&group->ops, &op->group_node);
6315 op->reason = reason;
6316 op->flow_cookie = rule->flow_cookie;
6317 ovs_mutex_lock(&rule->mutex);
6318 op->idle_timeout = rule->idle_timeout;
6319 op->hard_timeout = rule->hard_timeout;
6320 ovs_mutex_unlock(&rule->mutex);
6321 op->flags = rule->flags;
6325 if (type == OFOPERATION_DELETE) {
6326 hmap_insert(&ofproto->deletions, &op->hmap_node,
6327 cls_rule_hash(&rule->cr, rule->table_id));
6334 ofoperation_destroy(struct ofoperation *op)
6335 OVS_REQUIRES(ofproto_mutex)
6337 struct ofopgroup *group = op->group;
6340 op->rule->pending = NULL;
6342 if (op->type == OFOPERATION_DELETE) {
6343 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
6345 list_remove(&op->group_node);
6346 rule_actions_unref(op->actions);
6350 /* Indicates that 'op' completed with status 'error', which is either 0 to
6351 * indicate success or an OpenFlow error code on failure.
6353 * If 'error' is 0, indicating success, the operation will be committed
6354 * permanently to the flow table.
6356 * If 'error' is nonzero, then generally the operation will be rolled back:
6358 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
6359 * restores the original rule. The caller must have uninitialized any
6360 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
6361 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
6362 * and 7 for the new rule, calling its ->rule_dealloc() function.
6364 * - If 'op' is a "modify flow" operation, ofproto restores the original
6367 * - 'op' must not be a "delete flow" operation. Removing a rule is not
6368 * allowed to fail. It must always succeed.
6370 * Please see the large comment in ofproto/ofproto-provider.h titled
6371 * "Asynchronous Operation Support" for more information. */
6373 ofoperation_complete(struct ofoperation *op, enum ofperr error)
6375 struct ofopgroup *group = op->group;
6377 ovs_assert(group->n_running > 0);
6378 ovs_assert(!error || op->type != OFOPERATION_DELETE);
6381 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
6382 /* This function can be called from ->rule_construct(), in which case
6383 * ofproto_mutex is held, or it can be called from ->run(), in which
6384 * case ofproto_mutex is not held. But only in the latter case can we
6385 * arrive here, so we can safely take ofproto_mutex now. */
6386 ovs_mutex_lock(&ofproto_mutex);
6387 ovs_assert(op->rule->pending == op);
6388 ofopgroup_complete(group);
6389 ovs_mutex_unlock(&ofproto_mutex);
6394 pick_datapath_id(const struct ofproto *ofproto)
6396 const struct ofport *port;
6398 port = ofproto_get_port(ofproto, OFPP_LOCAL);
6400 uint8_t ea[ETH_ADDR_LEN];
6403 error = netdev_get_etheraddr(port->netdev, ea);
6405 return eth_addr_to_uint64(ea);
6407 VLOG_WARN("%s: could not get MAC address for %s (%s)",
6408 ofproto->name, netdev_get_name(port->netdev),
6409 ovs_strerror(error));
6411 return ofproto->fallback_dpid;
6415 pick_fallback_dpid(void)
6417 uint8_t ea[ETH_ADDR_LEN];
6418 eth_addr_nicira_random(ea);
6419 return eth_addr_to_uint64(ea);
6422 /* Table overflow policy. */
6424 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
6425 * to NULL if the table is not configured to evict rules or if the table
6426 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
6427 * or with no timeouts are not evictable.) */
6429 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
6430 OVS_REQUIRES(ofproto_mutex)
6432 struct eviction_group *evg;
6435 if (!table->eviction_fields) {
6439 /* In the common case, the outer and inner loops here will each be entered
6442 * - The inner loop normally "return"s in its first iteration. If the
6443 * eviction group has any evictable rules, then it always returns in
6446 * - The outer loop only iterates more than once if the largest eviction
6447 * group has no evictable rules.
6449 * - The outer loop can exit only if table's 'max_flows' is all filled up
6450 * by unevictable rules. */
6451 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
6454 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
6463 /* Searches 'ofproto' for tables that have more flows than their configured
6464 * maximum and that have flow eviction enabled, and evicts as many flows as
6465 * necessary and currently feasible from them.
6467 * This triggers only when an OpenFlow table has N flows in it and then the
6468 * client configures a maximum number of flows less than N. */
6470 ofproto_evict(struct ofproto *ofproto)
6472 struct oftable *table;
6474 ovs_mutex_lock(&ofproto_mutex);
6475 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
6476 evict_rules_from_table(ofproto, table, 0);
6478 ovs_mutex_unlock(&ofproto_mutex);
6481 /* Eviction groups. */
6483 /* Returns the priority to use for an eviction_group that contains 'n_rules'
6484 * rules. The priority contains low-order random bits to ensure that eviction
6485 * groups with the same number of rules are prioritized randomly. */
6487 eviction_group_priority(size_t n_rules)
6489 uint16_t size = MIN(UINT16_MAX, n_rules);
6490 return (size << 16) | random_uint16();
6493 /* Updates 'evg', an eviction_group within 'table', following a change that
6494 * adds or removes rules in 'evg'. */
6496 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
6497 OVS_REQUIRES(ofproto_mutex)
6499 heap_change(&table->eviction_groups_by_size, &evg->size_node,
6500 eviction_group_priority(heap_count(&evg->rules)));
6503 /* Destroys 'evg', an eviction_group within 'table':
6505 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
6506 * rules themselves, just removes them from the eviction group.)
6508 * - Removes 'evg' from 'table'.
6512 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
6513 OVS_REQUIRES(ofproto_mutex)
6515 while (!heap_is_empty(&evg->rules)) {
6518 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
6519 rule->eviction_group = NULL;
6521 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
6522 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
6523 heap_destroy(&evg->rules);
6527 /* Removes 'rule' from its eviction group, if any. */
6529 eviction_group_remove_rule(struct rule *rule)
6530 OVS_REQUIRES(ofproto_mutex)
6532 if (rule->eviction_group) {
6533 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6534 struct eviction_group *evg = rule->eviction_group;
6536 rule->eviction_group = NULL;
6537 heap_remove(&evg->rules, &rule->evg_node);
6538 if (heap_is_empty(&evg->rules)) {
6539 eviction_group_destroy(table, evg);
6541 eviction_group_resized(table, evg);
6546 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6547 * returns the hash value. */
6549 eviction_group_hash_rule(struct rule *rule)
6550 OVS_REQUIRES(ofproto_mutex)
6552 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6553 const struct mf_subfield *sf;
6557 hash = table->eviction_group_id_basis;
6558 miniflow_expand(&rule->cr.match.flow, &flow);
6559 for (sf = table->eviction_fields;
6560 sf < &table->eviction_fields[table->n_eviction_fields];
6563 if (mf_are_prereqs_ok(sf->field, &flow)) {
6564 union mf_value value;
6566 mf_get_value(sf->field, &flow, &value);
6568 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
6570 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
6571 unsigned int start = sf->ofs + sf->n_bits;
6572 bitwise_zero(&value, sf->field->n_bytes, start,
6573 sf->field->n_bytes * 8 - start);
6575 hash = hash_bytes(&value, sf->field->n_bytes, hash);
6577 hash = hash_int(hash, 0);
6584 /* Returns an eviction group within 'table' with the given 'id', creating one
6586 static struct eviction_group *
6587 eviction_group_find(struct oftable *table, uint32_t id)
6588 OVS_REQUIRES(ofproto_mutex)
6590 struct eviction_group *evg;
6592 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6596 evg = xmalloc(sizeof *evg);
6597 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6598 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6599 eviction_group_priority(0));
6600 heap_init(&evg->rules);
6605 /* Returns an eviction priority for 'rule'. The return value should be
6606 * interpreted so that higher priorities make a rule more attractive candidates
6608 * Called only if have a timeout. */
6610 rule_eviction_priority(struct ofproto *ofproto, struct rule *rule)
6611 OVS_REQUIRES(ofproto_mutex)
6613 long long int expiration = LLONG_MAX;
6614 long long int modified;
6615 uint32_t expiration_offset;
6617 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
6618 ovs_mutex_lock(&rule->mutex);
6619 modified = rule->modified;
6620 ovs_mutex_unlock(&rule->mutex);
6622 if (rule->hard_timeout) {
6623 expiration = modified + rule->hard_timeout * 1000;
6625 if (rule->idle_timeout) {
6626 uint64_t packets, bytes;
6628 long long int idle_expiration;
6630 ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used);
6631 idle_expiration = used + rule->idle_timeout * 1000;
6632 expiration = MIN(expiration, idle_expiration);
6635 if (expiration == LLONG_MAX) {
6639 /* Calculate the time of expiration as a number of (approximate) seconds
6640 * after program startup.
6642 * This should work OK for program runs that last UINT32_MAX seconds or
6643 * less. Therefore, please restart OVS at least once every 136 years. */
6644 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6646 /* Invert the expiration offset because we're using a max-heap. */
6647 return UINT32_MAX - expiration_offset;
6650 /* Adds 'rule' to an appropriate eviction group for its oftable's
6651 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6652 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6655 * The caller must ensure that 'rule' is not already in an eviction group. */
6657 eviction_group_add_rule(struct rule *rule)
6658 OVS_REQUIRES(ofproto_mutex)
6660 struct ofproto *ofproto = rule->ofproto;
6661 struct oftable *table = &ofproto->tables[rule->table_id];
6664 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
6665 * so no additional protection is needed. */
6666 has_timeout = rule->hard_timeout || rule->idle_timeout;
6668 if (table->eviction_fields && has_timeout) {
6669 struct eviction_group *evg;
6671 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6673 rule->eviction_group = evg;
6674 heap_insert(&evg->rules, &rule->evg_node,
6675 rule_eviction_priority(ofproto, rule));
6676 eviction_group_resized(table, evg);
6682 /* Initializes 'table'. */
6684 oftable_init(struct oftable *table)
6686 memset(table, 0, sizeof *table);
6687 classifier_init(&table->cls, flow_segment_u32s);
6688 table->max_flows = UINT_MAX;
6689 atomic_init(&table->config, (unsigned int)OFPTC11_TABLE_MISS_CONTROLLER);
6692 /* Destroys 'table', including its classifier and eviction groups.
6694 * The caller is responsible for freeing 'table' itself. */
6696 oftable_destroy(struct oftable *table)
6698 fat_rwlock_rdlock(&table->cls.rwlock);
6699 ovs_assert(classifier_is_empty(&table->cls));
6700 fat_rwlock_unlock(&table->cls.rwlock);
6701 oftable_disable_eviction(table);
6702 classifier_destroy(&table->cls);
6704 atomic_destroy(&table->config);
6707 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6708 * string, then 'table' will use its default name.
6710 * This only affects the name exposed for a table exposed through the OpenFlow
6711 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6713 oftable_set_name(struct oftable *table, const char *name)
6715 if (name && name[0]) {
6716 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6717 if (!table->name || strncmp(name, table->name, len)) {
6719 table->name = xmemdup0(name, len);
6727 /* oftables support a choice of two policies when adding a rule would cause the
6728 * number of flows in the table to exceed the configured maximum number: either
6729 * they can refuse to add the new flow or they can evict some existing flow.
6730 * This function configures the former policy on 'table'. */
6732 oftable_disable_eviction(struct oftable *table)
6733 OVS_REQUIRES(ofproto_mutex)
6735 if (table->eviction_fields) {
6736 struct eviction_group *evg, *next;
6738 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6739 &table->eviction_groups_by_id) {
6740 eviction_group_destroy(table, evg);
6742 hmap_destroy(&table->eviction_groups_by_id);
6743 heap_destroy(&table->eviction_groups_by_size);
6745 free(table->eviction_fields);
6746 table->eviction_fields = NULL;
6747 table->n_eviction_fields = 0;
6751 /* oftables support a choice of two policies when adding a rule would cause the
6752 * number of flows in the table to exceed the configured maximum number: either
6753 * they can refuse to add the new flow or they can evict some existing flow.
6754 * This function configures the latter policy on 'table', with fairness based
6755 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6756 * 'n_fields' as 0 disables fairness.) */
6758 oftable_enable_eviction(struct oftable *table,
6759 const struct mf_subfield *fields, size_t n_fields)
6760 OVS_REQUIRES(ofproto_mutex)
6762 struct cls_cursor cursor;
6765 if (table->eviction_fields
6766 && n_fields == table->n_eviction_fields
6768 || !memcmp(fields, table->eviction_fields,
6769 n_fields * sizeof *fields))) {
6774 oftable_disable_eviction(table);
6776 table->n_eviction_fields = n_fields;
6777 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6779 table->eviction_group_id_basis = random_uint32();
6780 hmap_init(&table->eviction_groups_by_id);
6781 heap_init(&table->eviction_groups_by_size);
6783 fat_rwlock_rdlock(&table->cls.rwlock);
6784 cls_cursor_init(&cursor, &table->cls, NULL);
6785 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6786 eviction_group_add_rule(rule);
6788 fat_rwlock_unlock(&table->cls.rwlock);
6791 /* Removes 'rule' from the oftable that contains it. */
6793 oftable_remove_rule__(struct ofproto *ofproto, struct rule *rule)
6794 OVS_REQUIRES(ofproto_mutex)
6796 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
6798 fat_rwlock_wrlock(&cls->rwlock);
6799 classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
6800 fat_rwlock_unlock(&cls->rwlock);
6802 cookies_remove(ofproto, rule);
6804 eviction_group_remove_rule(rule);
6805 if (!list_is_empty(&rule->expirable)) {
6806 list_remove(&rule->expirable);
6808 if (!list_is_empty(&rule->meter_list_node)) {
6809 list_remove(&rule->meter_list_node);
6810 list_init(&rule->meter_list_node);
6815 oftable_remove_rule(struct rule *rule)
6816 OVS_REQUIRES(ofproto_mutex)
6818 oftable_remove_rule__(rule->ofproto, rule);
6821 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6822 * the same cls_rule. */
6824 oftable_insert_rule(struct rule *rule)
6825 OVS_REQUIRES(ofproto_mutex)
6827 struct ofproto *ofproto = rule->ofproto;
6828 struct oftable *table = &ofproto->tables[rule->table_id];
6831 ovs_mutex_lock(&rule->mutex);
6832 may_expire = rule->hard_timeout || rule->idle_timeout;
6833 ovs_mutex_unlock(&rule->mutex);
6836 list_insert(&ofproto->expirable, &rule->expirable);
6839 cookies_insert(ofproto, rule);
6841 if (rule->actions->provider_meter_id != UINT32_MAX) {
6842 uint32_t meter_id = ofpacts_get_meter(rule->actions->ofpacts,
6843 rule->actions->ofpacts_len);
6844 struct meter *meter = ofproto->meters[meter_id];
6845 list_insert(&meter->rules, &rule->meter_list_node);
6847 fat_rwlock_wrlock(&table->cls.rwlock);
6848 classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
6849 fat_rwlock_unlock(&table->cls.rwlock);
6850 eviction_group_add_rule(rule);
6853 /* unixctl commands. */
6856 ofproto_lookup(const char *name)
6858 struct ofproto *ofproto;
6860 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6862 if (!strcmp(ofproto->name, name)) {
6870 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6871 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6873 struct ofproto *ofproto;
6877 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6878 ds_put_format(&results, "%s\n", ofproto->name);
6880 unixctl_command_reply(conn, ds_cstr(&results));
6881 ds_destroy(&results);
6885 ofproto_unixctl_init(void)
6887 static bool registered;
6893 unixctl_command_register("ofproto/list", "", 0, 0,
6894 ofproto_unixctl_list, NULL);
6897 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6899 * This is deprecated. It is only for compatibility with broken device drivers
6900 * in old versions of Linux that do not properly support VLANs when VLAN
6901 * devices are not used. When broken device drivers are no longer in
6902 * widespread use, we will delete these interfaces. */
6904 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6905 * (exactly) by an OpenFlow rule in 'ofproto'. */
6907 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6909 const struct oftable *oftable;
6911 free(ofproto->vlan_bitmap);
6912 ofproto->vlan_bitmap = bitmap_allocate(4096);
6913 ofproto->vlans_changed = false;
6915 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6916 const struct cls_subtable *table;
6918 fat_rwlock_rdlock(&oftable->cls.rwlock);
6919 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.subtables) {
6920 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6921 const struct cls_rule *rule;
6923 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6924 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6925 bitmap_set1(vlan_bitmap, vid);
6926 bitmap_set1(ofproto->vlan_bitmap, vid);
6930 fat_rwlock_unlock(&oftable->cls.rwlock);
6934 /* Returns true if new VLANs have come into use by the flow table since the
6935 * last call to ofproto_get_vlan_usage().
6937 * We don't track when old VLANs stop being used. */
6939 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6941 return ofproto->vlans_changed;
6944 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6945 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6946 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6947 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6948 * then the VLAN device is un-enslaved. */
6950 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6951 ofp_port_t realdev_ofp_port, int vid)
6953 struct ofport *ofport;
6956 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6958 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6960 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6961 ofproto->name, vlandev_ofp_port);
6965 if (!ofproto->ofproto_class->set_realdev) {
6966 if (!vlandev_ofp_port) {
6969 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6973 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6975 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6976 ofproto->name, vlandev_ofp_port,
6977 netdev_get_name(ofport->netdev), ovs_strerror(error));