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"
50 #include "poll-loop.h"
58 #include "unaligned.h"
63 VLOG_DEFINE_THIS_MODULE(ofproto);
65 COVERAGE_DEFINE(ofproto_flush);
66 COVERAGE_DEFINE(ofproto_packet_out);
67 COVERAGE_DEFINE(ofproto_queue_req);
68 COVERAGE_DEFINE(ofproto_recv_openflow);
69 COVERAGE_DEFINE(ofproto_reinit_ports);
70 COVERAGE_DEFINE(ofproto_update_port);
72 /* Default fields to use for prefix tries in each flow table, unless something
73 * else is configured. */
74 const enum mf_field_id default_prefix_fields[2] =
75 { MFF_IPV4_DST, MFF_IPV4_SRC };
78 S_OPENFLOW, /* Processing OpenFlow commands. */
79 S_EVICT, /* Evicting flows from over-limit tables. */
80 S_FLUSH, /* Deleting all flow table rules. */
83 enum ofoperation_type {
90 /* A single OpenFlow request can execute any number of operations. The
91 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
92 * ofconn to which an error reply should be sent if necessary.
94 * ofproto initiates some operations internally. These operations are still
95 * assigned to groups but will not have an associated ofconn. */
97 struct ofproto *ofproto; /* Owning ofproto. */
98 struct list ofproto_node; /* In ofproto's "pending" list. */
99 struct list ops; /* List of "struct ofoperation"s. */
100 int n_running; /* Number of ops still pending. */
102 /* Data needed to send OpenFlow reply on failure or to send a buffered
105 * If list_is_empty(ofconn_node) then this ofopgroup never had an
106 * associated ofconn or its ofconn's connection dropped after it initiated
107 * the operation. In the latter case 'ofconn' is a wild pointer that
108 * refers to freed memory, so the 'ofconn' member must be used only if
109 * !list_is_empty(ofconn_node).
111 struct list ofconn_node; /* In ofconn's list of pending opgroups. */
112 struct ofconn *ofconn; /* ofconn for reply (but see note above). */
113 struct ofp_header *request; /* Original request (truncated at 64 bytes). */
114 uint32_t buffer_id; /* Buffer id from original request. */
117 static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *);
118 static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *,
119 const struct ofp_header *,
121 static void ofopgroup_submit(struct ofopgroup *);
122 static void ofopgroup_complete(struct ofopgroup *);
124 /* A single flow table operation. */
126 struct ofopgroup *group; /* Owning group. */
127 struct list group_node; /* In ofopgroup's "ops" list. */
128 struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */
129 struct rule *rule; /* Rule being operated upon. */
130 enum ofoperation_type type; /* Type of operation. */
132 /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions
134 const struct rule_actions *actions;
136 /* OFOPERATION_DELETE. */
137 enum ofp_flow_removed_reason reason; /* Reason flow was removed. */
139 ovs_be64 flow_cookie; /* Rule's old flow cookie. */
140 uint16_t idle_timeout; /* Rule's old idle timeout. */
141 uint16_t hard_timeout; /* Rule's old hard timeout. */
142 enum ofputil_flow_mod_flags flags; /* Rule's old flags. */
143 enum ofperr error; /* 0 if no error. */
146 static struct ofoperation *ofoperation_create(struct ofopgroup *,
148 enum ofoperation_type,
149 enum ofp_flow_removed_reason);
150 static void ofoperation_destroy(struct ofoperation *);
153 static void oftable_init(struct oftable *);
154 static void oftable_destroy(struct oftable *);
156 static void oftable_set_name(struct oftable *, const char *name);
158 static void oftable_disable_eviction(struct oftable *);
159 static void oftable_enable_eviction(struct oftable *,
160 const struct mf_subfield *fields,
163 static void oftable_remove_rule(struct rule *rule) OVS_REQUIRES(ofproto_mutex);
164 static void oftable_remove_rule__(struct ofproto *, struct rule *)
165 OVS_REQUIRES(ofproto_mutex);
167 /* A set of rules within a single OpenFlow table (oftable) that have the same
168 * values for the oftable's eviction_fields. A rule to be evicted, when one is
169 * needed, is taken from the eviction group that contains the greatest number
172 * An oftable owns any number of eviction groups, each of which contains any
175 * Membership in an eviction group is imprecise, based on the hash of the
176 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
177 * That is, if two rules have different eviction_fields, but those
178 * eviction_fields hash to the same value, then they will belong to the same
179 * eviction_group anyway.
181 * (When eviction is not enabled on an oftable, we don't track any eviction
182 * groups, to save time and space.) */
183 struct eviction_group {
184 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
185 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
186 struct heap rules; /* Contains "struct rule"s. */
189 static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
190 OVS_REQUIRES(ofproto_mutex);
191 static void ofproto_evict(struct ofproto *)
192 OVS_EXCLUDED(ofproto_mutex);
193 static uint32_t rule_eviction_priority(struct ofproto *ofproto, struct rule *)
194 OVS_REQUIRES(ofproto_mutex);;
195 static void eviction_group_add_rule(struct rule *)
196 OVS_REQUIRES(ofproto_mutex);
197 static void eviction_group_remove_rule(struct rule *)
198 OVS_REQUIRES(ofproto_mutex);
200 /* Criteria that flow_mod and other operations use for selecting rules on
201 * which to operate. */
202 struct rule_criteria {
203 /* An OpenFlow table or 255 for all tables. */
206 /* OpenFlow matching criteria. Interpreted different in "loose" way by
207 * collect_rules_loose() and "strict" way by collect_rules_strict(), as
208 * defined in the OpenFlow spec. */
211 /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's
212 * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'.
213 * The rule will not be selected if M is 1 and B != C. */
215 ovs_be64 cookie_mask;
217 /* Selection based on actions within a rule:
219 * If out_port != OFPP_ANY, selects only rules that output to out_port.
220 * If out_group != OFPG_ALL, select only rules that output to out_group. */
224 /* If true, collects only rules that are modifiable. */
226 bool include_readonly;
229 static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
230 const struct match *match,
231 unsigned int priority,
232 ovs_be64 cookie, ovs_be64 cookie_mask,
233 ofp_port_t out_port, uint32_t out_group);
234 static void rule_criteria_require_rw(struct rule_criteria *,
235 bool can_write_readonly);
236 static void rule_criteria_destroy(struct rule_criteria *);
238 /* A packet that needs to be passed to rule_execute().
240 * (We can't do this immediately from ofopgroup_complete() because that holds
241 * ofproto_mutex, which rule_execute() needs released.) */
242 struct rule_execute {
243 struct list list_node; /* In struct ofproto's "rule_executes" list. */
244 struct rule *rule; /* Owns a reference to the rule. */
246 struct ofpbuf *packet; /* Owns the packet. */
249 static void run_rule_executes(struct ofproto *) OVS_EXCLUDED(ofproto_mutex);
250 static void destroy_rule_executes(struct ofproto *);
253 static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex);
254 static void ofport_destroy(struct ofport *);
256 static void update_port(struct ofproto *, const char *devname);
257 static int init_ports(struct ofproto *);
258 static void reinit_ports(struct ofproto *);
260 static long long int ofport_get_usage(const struct ofproto *,
261 ofp_port_t ofp_port);
262 static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port,
263 long long int last_used);
264 static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port);
268 * Keeps track of the currently used and recently used ofport values and is
269 * used to prevent immediate recycling of ofport values. */
270 struct ofport_usage {
271 struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */
272 ofp_port_t ofp_port; /* OpenFlow port number. */
273 long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX
274 represents in-use ofports. */
278 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
279 static bool rule_is_modifiable(const struct rule *rule,
280 enum ofputil_flow_mod_flags flag);
283 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
284 struct ofputil_flow_mod *,
285 const struct ofp_header *);
286 static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
287 struct ofputil_flow_mod *,
288 const struct ofp_header *,
289 const struct rule_collection *);
290 static void delete_flow__(struct rule *rule, struct ofopgroup *,
291 enum ofp_flow_removed_reason)
292 OVS_REQUIRES(ofproto_mutex);
293 static bool ofproto_group_exists__(const struct ofproto *ofproto,
295 OVS_REQ_RDLOCK(ofproto->groups_rwlock);
296 static bool ofproto_group_exists(const struct ofproto *ofproto,
298 OVS_EXCLUDED(ofproto->groups_rwlock);
299 static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
300 static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
301 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
302 struct ofputil_flow_mod *,
303 const struct ofp_header *)
304 OVS_EXCLUDED(ofproto_mutex);
305 static void calc_duration(long long int start, long long int now,
306 uint32_t *sec, uint32_t *nsec);
309 static uint64_t pick_datapath_id(const struct ofproto *);
310 static uint64_t pick_fallback_dpid(void);
311 static void ofproto_destroy__(struct ofproto *);
312 static void update_mtu(struct ofproto *, struct ofport *);
313 static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
314 static void meter_insert_rule(struct rule *);
317 static void ofproto_unixctl_init(void);
319 /* All registered ofproto classes, in probe order. */
320 static const struct ofproto_class **ofproto_classes;
321 static size_t n_ofproto_classes;
322 static size_t allocated_ofproto_classes;
324 /* Global lock that protects all flow table operations. */
325 struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER;
327 unsigned ofproto_flow_limit = OFPROTO_FLOW_LIMIT_DEFAULT;
328 unsigned ofproto_max_idle = OFPROTO_MAX_IDLE_DEFAULT;
330 size_t n_handlers, n_revalidators;
332 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
333 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
335 /* Initial mappings of port to OpenFlow number mappings. */
336 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
338 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
340 /* The default value of true waits for flow restore. */
341 static bool flow_restore_wait = true;
343 /* Must be called to initialize the ofproto library.
345 * The caller may pass in 'iface_hints', which contains an shash of
346 * "iface_hint" elements indexed by the interface's name. The provider
347 * may use these hints to describe the startup configuration in order to
348 * reinitialize its state. The caller owns the provided data, so a
349 * provider will make copies of anything required. An ofproto provider
350 * will remove any existing state that is not described by the hint, and
351 * may choose to remove it all. */
353 ofproto_init(const struct shash *iface_hints)
355 struct shash_node *node;
358 ofproto_class_register(&ofproto_dpif_class);
360 /* Make a local copy, since we don't own 'iface_hints' elements. */
361 SHASH_FOR_EACH(node, iface_hints) {
362 const struct iface_hint *orig_hint = node->data;
363 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
364 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
366 new_hint->br_name = xstrdup(orig_hint->br_name);
367 new_hint->br_type = xstrdup(br_type);
368 new_hint->ofp_port = orig_hint->ofp_port;
370 shash_add(&init_ofp_ports, node->name, new_hint);
373 for (i = 0; i < n_ofproto_classes; i++) {
374 ofproto_classes[i]->init(&init_ofp_ports);
378 /* 'type' should be a normalized datapath type, as returned by
379 * ofproto_normalize_type(). Returns the corresponding ofproto_class
380 * structure, or a null pointer if there is none registered for 'type'. */
381 static const struct ofproto_class *
382 ofproto_class_find__(const char *type)
386 for (i = 0; i < n_ofproto_classes; i++) {
387 const struct ofproto_class *class = ofproto_classes[i];
392 class->enumerate_types(&types);
393 found = sset_contains(&types, type);
394 sset_destroy(&types);
400 VLOG_WARN("unknown datapath type %s", type);
404 /* Registers a new ofproto class. After successful registration, new ofprotos
405 * of that type can be created using ofproto_create(). */
407 ofproto_class_register(const struct ofproto_class *new_class)
411 for (i = 0; i < n_ofproto_classes; i++) {
412 if (ofproto_classes[i] == new_class) {
417 if (n_ofproto_classes >= allocated_ofproto_classes) {
418 ofproto_classes = x2nrealloc(ofproto_classes,
419 &allocated_ofproto_classes,
420 sizeof *ofproto_classes);
422 ofproto_classes[n_ofproto_classes++] = new_class;
426 /* Unregisters a datapath provider. 'type' must have been previously
427 * registered and not currently be in use by any ofprotos. After
428 * unregistration new datapaths of that type cannot be opened using
429 * ofproto_create(). */
431 ofproto_class_unregister(const struct ofproto_class *class)
435 for (i = 0; i < n_ofproto_classes; i++) {
436 if (ofproto_classes[i] == class) {
437 for (i++; i < n_ofproto_classes; i++) {
438 ofproto_classes[i - 1] = ofproto_classes[i];
444 VLOG_WARN("attempted to unregister an ofproto class that is not "
449 /* Clears 'types' and enumerates all registered ofproto types into it. The
450 * caller must first initialize the sset. */
452 ofproto_enumerate_types(struct sset *types)
457 for (i = 0; i < n_ofproto_classes; i++) {
458 ofproto_classes[i]->enumerate_types(types);
462 /* Returns the fully spelled out name for the given ofproto 'type'.
464 * Normalized type string can be compared with strcmp(). Unnormalized type
465 * string might be the same even if they have different spellings. */
467 ofproto_normalize_type(const char *type)
469 return type && type[0] ? type : "system";
472 /* Clears 'names' and enumerates the names of all known created ofprotos with
473 * the given 'type'. The caller must first initialize the sset. Returns 0 if
474 * successful, otherwise a positive errno value.
476 * Some kinds of datapaths might not be practically enumerable. This is not
477 * considered an error. */
479 ofproto_enumerate_names(const char *type, struct sset *names)
481 const struct ofproto_class *class = ofproto_class_find__(type);
482 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
486 ofproto_create(const char *datapath_name, const char *datapath_type,
487 struct ofproto **ofprotop)
489 const struct ofproto_class *class;
490 struct ofproto *ofproto;
496 ofproto_unixctl_init();
498 datapath_type = ofproto_normalize_type(datapath_type);
499 class = ofproto_class_find__(datapath_type);
501 VLOG_WARN("could not create datapath %s of unknown type %s",
502 datapath_name, datapath_type);
506 ofproto = class->alloc();
508 VLOG_ERR("failed to allocate datapath %s of type %s",
509 datapath_name, datapath_type);
514 ovs_mutex_lock(&ofproto_mutex);
515 memset(ofproto, 0, sizeof *ofproto);
516 ofproto->ofproto_class = class;
517 ofproto->name = xstrdup(datapath_name);
518 ofproto->type = xstrdup(datapath_type);
519 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
520 hash_string(ofproto->name, 0));
521 ofproto->datapath_id = 0;
522 ofproto->forward_bpdu = false;
523 ofproto->fallback_dpid = pick_fallback_dpid();
524 ofproto->mfr_desc = NULL;
525 ofproto->hw_desc = NULL;
526 ofproto->sw_desc = NULL;
527 ofproto->serial_desc = NULL;
528 ofproto->dp_desc = NULL;
529 ofproto->frag_handling = OFPC_FRAG_NORMAL;
530 hmap_init(&ofproto->ports);
531 hmap_init(&ofproto->ofport_usage);
532 shash_init(&ofproto->port_by_name);
533 simap_init(&ofproto->ofp_requests);
534 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
535 ofproto->eviction_group_timer = LLONG_MIN;
536 ofproto->tables = NULL;
537 ofproto->n_tables = 0;
538 hindex_init(&ofproto->cookies);
539 list_init(&ofproto->expirable);
540 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
541 ofproto->state = S_OPENFLOW;
542 list_init(&ofproto->pending);
543 ofproto->n_pending = 0;
544 hmap_init(&ofproto->deletions);
545 guarded_list_init(&ofproto->rule_executes);
546 ofproto->vlan_bitmap = NULL;
547 ofproto->vlans_changed = false;
548 ofproto->min_mtu = INT_MAX;
549 ovs_rwlock_init(&ofproto->groups_rwlock);
550 hmap_init(&ofproto->groups);
551 ovs_mutex_unlock(&ofproto_mutex);
552 ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS |
553 OFPGFC_SELECT_WEIGHT;
554 ofproto->ogf.max_groups[OFPGT11_ALL] = OFPG_MAX;
555 ofproto->ogf.max_groups[OFPGT11_SELECT] = OFPG_MAX;
556 ofproto->ogf.max_groups[OFPGT11_INDIRECT] = OFPG_MAX;
557 ofproto->ogf.max_groups[OFPGT11_FF] = OFPG_MAX;
558 ofproto->ogf.actions[0] =
559 (1 << OFPAT11_OUTPUT) |
560 (1 << OFPAT11_COPY_TTL_OUT) |
561 (1 << OFPAT11_COPY_TTL_IN) |
562 (1 << OFPAT11_SET_MPLS_TTL) |
563 (1 << OFPAT11_DEC_MPLS_TTL) |
564 (1 << OFPAT11_PUSH_VLAN) |
565 (1 << OFPAT11_POP_VLAN) |
566 (1 << OFPAT11_PUSH_MPLS) |
567 (1 << OFPAT11_POP_MPLS) |
568 (1 << OFPAT11_SET_QUEUE) |
569 (1 << OFPAT11_GROUP) |
570 (1 << OFPAT11_SET_NW_TTL) |
571 (1 << OFPAT11_DEC_NW_TTL) |
572 (1 << OFPAT12_SET_FIELD);
574 * (1 << OFPAT13_PUSH_PBB) |
575 * (1 << OFPAT13_POP_PBB) */
577 error = ofproto->ofproto_class->construct(ofproto);
579 VLOG_ERR("failed to open datapath %s: %s",
580 datapath_name, ovs_strerror(error));
581 ofproto_destroy__(ofproto);
585 /* Check that hidden tables, if any, are at the end. */
586 ovs_assert(ofproto->n_tables);
587 for (i = 0; i + 1 < ofproto->n_tables; i++) {
588 enum oftable_flags flags = ofproto->tables[i].flags;
589 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
591 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
594 ofproto->datapath_id = pick_datapath_id(ofproto);
597 /* Initialize meters table. */
598 if (ofproto->ofproto_class->meter_get_features) {
599 ofproto->ofproto_class->meter_get_features(ofproto,
600 &ofproto->meter_features);
602 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
604 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
605 * sizeof(struct meter *));
611 /* Must be called (only) by an ofproto implementation in its constructor
612 * function. See the large comment on 'construct' in struct ofproto_class for
615 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
617 struct oftable *table;
619 ovs_assert(!ofproto->n_tables);
620 ovs_assert(n_tables >= 1 && n_tables <= 255);
622 ofproto->n_tables = n_tables;
623 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
624 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
629 /* To be optionally called (only) by an ofproto implementation in its
630 * constructor function. See the large comment on 'construct' in struct
631 * ofproto_class for details.
633 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
634 * will then ensure that actions passed into the ofproto implementation will
635 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
636 * function is not called, there will be no such restriction.
638 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
639 * the 'max_ports' restriction. */
641 ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
643 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
644 ofproto->max_ports = max_ports;
648 ofproto_get_datapath_id(const struct ofproto *ofproto)
650 return ofproto->datapath_id;
654 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
656 uint64_t old_dpid = p->datapath_id;
657 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
658 if (p->datapath_id != old_dpid) {
659 /* Force all active connections to reconnect, since there is no way to
660 * notify a controller that the datapath ID has changed. */
661 ofproto_reconnect_controllers(p);
666 ofproto_set_controllers(struct ofproto *p,
667 const struct ofproto_controller *controllers,
668 size_t n_controllers, uint32_t allowed_versions)
670 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
675 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
677 connmgr_set_fail_mode(p->connmgr, fail_mode);
680 /* Drops the connections between 'ofproto' and all of its controllers, forcing
681 * them to reconnect. */
683 ofproto_reconnect_controllers(struct ofproto *ofproto)
685 connmgr_reconnect(ofproto->connmgr);
688 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
689 * in-band control should guarantee access, in the same way that in-band
690 * control guarantees access to OpenFlow controllers. */
692 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
693 const struct sockaddr_in *extras, size_t n)
695 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
698 /* Sets the OpenFlow queue used by flows set up by in-band control on
699 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
700 * flows will use the default queue. */
702 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
704 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
707 /* Sets the number of flows at which eviction from the kernel flow table
710 ofproto_set_flow_limit(unsigned limit)
712 ofproto_flow_limit = limit;
715 /* Sets the maximum idle time for flows in the datapath before they are
718 ofproto_set_max_idle(unsigned max_idle)
720 ofproto_max_idle = max_idle;
723 /* If forward_bpdu is true, the NORMAL action will forward frames with
724 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
725 * the NORMAL action will drop these frames. */
727 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
729 bool old_val = ofproto->forward_bpdu;
730 ofproto->forward_bpdu = forward_bpdu;
731 if (old_val != ofproto->forward_bpdu) {
732 if (ofproto->ofproto_class->forward_bpdu_changed) {
733 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
738 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
739 * 'idle_time', in seconds, and the maximum number of MAC table entries to
742 ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
745 if (ofproto->ofproto_class->set_mac_table_config) {
746 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
752 ofproto_set_threads(int n_handlers_, int n_revalidators_)
754 int threads = MAX(count_cpu_cores(), 2);
756 n_revalidators = MAX(n_revalidators_, 0);
757 n_handlers = MAX(n_handlers_, 0);
759 if (!n_revalidators) {
760 n_revalidators = n_handlers
761 ? MAX(threads - (int) n_handlers, 1)
766 n_handlers = MAX(threads - (int) n_revalidators, 1);
771 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
774 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
778 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
780 return connmgr_set_snoops(ofproto->connmgr, snoops);
784 ofproto_set_netflow(struct ofproto *ofproto,
785 const struct netflow_options *nf_options)
787 if (nf_options && sset_is_empty(&nf_options->collectors)) {
791 if (ofproto->ofproto_class->set_netflow) {
792 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
794 return nf_options ? EOPNOTSUPP : 0;
799 ofproto_set_sflow(struct ofproto *ofproto,
800 const struct ofproto_sflow_options *oso)
802 if (oso && sset_is_empty(&oso->targets)) {
806 if (ofproto->ofproto_class->set_sflow) {
807 return ofproto->ofproto_class->set_sflow(ofproto, oso);
809 return oso ? EOPNOTSUPP : 0;
814 ofproto_set_ipfix(struct ofproto *ofproto,
815 const struct ofproto_ipfix_bridge_exporter_options *bo,
816 const struct ofproto_ipfix_flow_exporter_options *fo,
819 if (ofproto->ofproto_class->set_ipfix) {
820 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
822 return (bo || fo) ? EOPNOTSUPP : 0;
827 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
829 flow_restore_wait = flow_restore_wait_db;
833 ofproto_get_flow_restore_wait(void)
835 return flow_restore_wait;
839 /* Spanning Tree Protocol (STP) configuration. */
841 /* Configures STP on 'ofproto' using the settings defined in 's'. If
842 * 's' is NULL, disables STP.
844 * Returns 0 if successful, otherwise a positive errno value. */
846 ofproto_set_stp(struct ofproto *ofproto,
847 const struct ofproto_stp_settings *s)
849 return (ofproto->ofproto_class->set_stp
850 ? ofproto->ofproto_class->set_stp(ofproto, s)
854 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
855 * 'enabled' member of 's' is false, then the other members are not
858 * Returns 0 if successful, otherwise a positive errno value. */
860 ofproto_get_stp_status(struct ofproto *ofproto,
861 struct ofproto_stp_status *s)
863 return (ofproto->ofproto_class->get_stp_status
864 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
868 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
869 * in 's'. The caller is responsible for assigning STP port numbers
870 * (using the 'port_num' member in the range of 1 through 255, inclusive)
871 * and ensuring there are no duplicates. If the 's' is NULL, then STP
872 * is disabled on the port.
874 * Returns 0 if successful, otherwise a positive errno value.*/
876 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
877 const struct ofproto_port_stp_settings *s)
879 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
881 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
882 ofproto->name, ofp_port);
886 return (ofproto->ofproto_class->set_stp_port
887 ? ofproto->ofproto_class->set_stp_port(ofport, s)
891 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
892 * 's'. If the 'enabled' member in 's' is false, then the other members
893 * are not meaningful.
895 * Returns 0 if successful, otherwise a positive errno value.*/
897 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
898 struct ofproto_port_stp_status *s)
900 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
902 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
903 "port %"PRIu16, ofproto->name, ofp_port);
907 return (ofproto->ofproto_class->get_stp_port_status
908 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
912 /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
913 * 's'. If the 'enabled' member in 's' is false, then the other members
914 * are not meaningful.
916 * Returns 0 if successful, otherwise a positive errno value.*/
918 ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port,
919 struct ofproto_port_stp_stats *s)
921 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
923 VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent "
924 "port %"PRIu16, ofproto->name, ofp_port);
928 return (ofproto->ofproto_class->get_stp_port_stats
929 ? ofproto->ofproto_class->get_stp_port_stats(ofport, s)
933 /* Queue DSCP configuration. */
935 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
936 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
937 * to implement QoS. Instead, it is used to implement features which require
938 * knowledge of what queues exist on a port, and some basic information about
941 * Returns 0 if successful, otherwise a positive errno value. */
943 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
944 const struct ofproto_port_queue *queues,
947 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
950 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
951 ofproto->name, ofp_port);
955 return (ofproto->ofproto_class->set_queues
956 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
960 /* Connectivity Fault Management configuration. */
962 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
964 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
966 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
967 if (ofport && ofproto->ofproto_class->set_cfm) {
968 ofproto->ofproto_class->set_cfm(ofport, NULL);
972 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
973 * basic configuration from the configuration members in 'cfm', and the remote
974 * maintenance point ID from remote_mpid. Ignores the statistics members of
977 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
979 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
980 const struct cfm_settings *s)
982 struct ofport *ofport;
985 ofport = ofproto_get_port(ofproto, ofp_port);
987 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
988 ofproto->name, ofp_port);
992 /* XXX: For configuration simplicity, we only support one remote_mpid
993 * outside of the CFM module. It's not clear if this is the correct long
994 * term solution or not. */
995 error = (ofproto->ofproto_class->set_cfm
996 ? ofproto->ofproto_class->set_cfm(ofport, s)
999 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
1000 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
1001 ovs_strerror(error));
1005 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
1006 * 'ofproto' does not have a port 'ofp_port'. */
1008 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
1009 const struct smap *cfg)
1011 struct ofport *ofport;
1014 ofport = ofproto_get_port(ofproto, ofp_port);
1016 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
1017 ofproto->name, ofp_port);
1021 error = (ofproto->ofproto_class->set_bfd
1022 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
1025 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
1026 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
1027 ovs_strerror(error));
1031 /* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on
1032 * success. Returns a negative number if there is no status change since
1033 * last update. Returns a positive errno otherwise. Has no effect if
1034 * 'ofp_port' is not an OpenFlow port in 'ofproto'.
1036 * The caller must provide and own '*status'. */
1038 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
1039 struct smap *status)
1041 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1042 return (ofport && ofproto->ofproto_class->get_bfd_status
1043 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
1047 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1048 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1049 * 0 if LACP partner information is not current (generally indicating a
1050 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1052 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
1054 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1055 return (ofport && ofproto->ofproto_class->port_is_lacp_current
1056 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
1062 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1063 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1064 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1065 * configuration plus, if there is more than one slave, a bonding
1068 * If 'aux' is already registered then this function updates its configuration
1069 * to 's'. Otherwise, this function registers a new bundle.
1071 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1074 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
1075 const struct ofproto_bundle_settings *s)
1077 return (ofproto->ofproto_class->bundle_set
1078 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
1082 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1083 * If no such bundle has been registered, this has no effect. */
1085 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
1087 return ofproto_bundle_register(ofproto, aux, NULL);
1091 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1092 * If 'aux' is already registered then this function updates its configuration
1093 * to 's'. Otherwise, this function registers a new mirror. */
1095 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1096 const struct ofproto_mirror_settings *s)
1098 return (ofproto->ofproto_class->mirror_set
1099 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1103 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1104 * If no mirror has been registered, this has no effect. */
1106 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
1108 return ofproto_mirror_register(ofproto, aux, NULL);
1111 /* Retrieves statistics from mirror associated with client data pointer
1112 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1113 * 'bytes', respectively. If a particular counters is not supported,
1114 * the appropriate argument is set to UINT64_MAX. */
1116 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1117 uint64_t *packets, uint64_t *bytes)
1119 if (!ofproto->ofproto_class->mirror_get_stats) {
1120 *packets = *bytes = UINT64_MAX;
1124 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1128 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1129 * which all packets are flooded, instead of using MAC learning. If
1130 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1132 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1135 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
1137 return (ofproto->ofproto_class->set_flood_vlans
1138 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1142 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1143 * output for a mirror. */
1145 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1147 return (ofproto->ofproto_class->is_mirror_output_bundle
1148 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1152 /* Configuration of OpenFlow tables. */
1154 /* Returns the number of OpenFlow tables in 'ofproto'. */
1156 ofproto_get_n_tables(const struct ofproto *ofproto)
1158 return ofproto->n_tables;
1161 /* Returns the number of Controller visible OpenFlow tables
1162 * in 'ofproto'. This number will exclude Hidden tables.
1163 * This funtion's return value should be less or equal to that of
1164 * ofproto_get_n_tables() . */
1166 ofproto_get_n_visible_tables(const struct ofproto *ofproto)
1168 uint8_t n = ofproto->n_tables;
1170 /* Count only non-hidden tables in the number of tables. (Hidden tables,
1171 * if present, are always at the end.) */
1172 while(n && (ofproto->tables[n - 1].flags & OFTABLE_HIDDEN)) {
1179 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1180 * settings from 's'. 'table_id' must be in the range 0 through the number of
1181 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1183 * For read-only tables, only the name may be configured. */
1185 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1186 const struct ofproto_table_settings *s)
1188 struct oftable *table;
1190 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1191 table = &ofproto->tables[table_id];
1193 oftable_set_name(table, s->name);
1195 if (table->flags & OFTABLE_READONLY) {
1200 oftable_enable_eviction(table, s->groups, s->n_groups);
1202 oftable_disable_eviction(table);
1205 table->max_flows = s->max_flows;
1206 fat_rwlock_wrlock(&table->cls.rwlock);
1207 if (classifier_count(&table->cls) > table->max_flows
1208 && table->eviction_fields) {
1209 /* 'table' contains more flows than allowed. We might not be able to
1210 * evict them right away because of the asynchronous nature of flow
1211 * table changes. Schedule eviction for later. */
1212 switch (ofproto->state) {
1214 ofproto->state = S_EVICT;
1218 /* We're already deleting flows, nothing more to do. */
1223 classifier_set_prefix_fields(&table->cls,
1224 s->prefix_fields, s->n_prefix_fields);
1226 fat_rwlock_unlock(&table->cls.rwlock);
1230 ofproto_has_snoops(const struct ofproto *ofproto)
1232 return connmgr_has_snoops(ofproto->connmgr);
1236 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1238 connmgr_get_snoops(ofproto->connmgr, snoops);
1242 ofproto_rule_delete__(struct ofproto *ofproto, struct rule *rule,
1244 OVS_REQUIRES(ofproto_mutex)
1246 struct ofopgroup *group;
1248 ovs_assert(!rule->pending);
1250 group = ofopgroup_create_unattached(ofproto);
1251 delete_flow__(rule, group, reason);
1252 ofopgroup_submit(group);
1255 /* Deletes 'rule' from 'ofproto'.
1257 * Within an ofproto implementation, this function allows an ofproto
1258 * implementation to destroy any rules that remain when its ->destruct()
1259 * function is called. This function is not suitable for use elsewhere in an
1260 * ofproto implementation.
1262 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1263 * Cycle" in ofproto-provider.h. */
1265 ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule)
1266 OVS_EXCLUDED(ofproto_mutex)
1268 struct ofopgroup *group;
1270 ovs_mutex_lock(&ofproto_mutex);
1271 ovs_assert(!rule->pending);
1273 group = ofopgroup_create_unattached(ofproto);
1274 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1275 oftable_remove_rule__(ofproto, rule);
1276 ofproto->ofproto_class->rule_delete(rule);
1277 ofopgroup_submit(group);
1279 ovs_mutex_unlock(&ofproto_mutex);
1283 ofproto_flush__(struct ofproto *ofproto)
1284 OVS_EXCLUDED(ofproto_mutex)
1286 struct oftable *table;
1288 if (ofproto->ofproto_class->flush) {
1289 ofproto->ofproto_class->flush(ofproto);
1292 ovs_mutex_lock(&ofproto_mutex);
1293 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1294 struct rule *rule, *next_rule;
1295 struct cls_cursor cursor;
1297 if (table->flags & OFTABLE_HIDDEN) {
1301 fat_rwlock_rdlock(&table->cls.rwlock);
1302 cls_cursor_init(&cursor, &table->cls, NULL);
1303 fat_rwlock_unlock(&table->cls.rwlock);
1304 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1305 if (!rule->pending) {
1306 ofproto_rule_delete__(ofproto, rule, OFPRR_DELETE);
1310 ovs_mutex_unlock(&ofproto_mutex);
1313 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1316 ofproto_destroy__(struct ofproto *ofproto)
1317 OVS_EXCLUDED(ofproto_mutex)
1319 struct oftable *table;
1321 ovs_assert(list_is_empty(&ofproto->pending));
1323 destroy_rule_executes(ofproto);
1324 delete_group(ofproto, OFPG_ALL);
1326 guarded_list_destroy(&ofproto->rule_executes);
1327 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1328 hmap_destroy(&ofproto->groups);
1330 connmgr_destroy(ofproto->connmgr);
1332 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1333 free(ofproto->name);
1334 free(ofproto->type);
1335 free(ofproto->mfr_desc);
1336 free(ofproto->hw_desc);
1337 free(ofproto->sw_desc);
1338 free(ofproto->serial_desc);
1339 free(ofproto->dp_desc);
1340 hmap_destroy(&ofproto->ports);
1341 hmap_destroy(&ofproto->ofport_usage);
1342 shash_destroy(&ofproto->port_by_name);
1343 simap_destroy(&ofproto->ofp_requests);
1345 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1346 oftable_destroy(table);
1348 free(ofproto->tables);
1350 hmap_destroy(&ofproto->deletions);
1352 ovs_assert(hindex_is_empty(&ofproto->cookies));
1353 hindex_destroy(&ofproto->cookies);
1355 free(ofproto->vlan_bitmap);
1357 ofproto->ofproto_class->dealloc(ofproto);
1361 ofproto_destroy(struct ofproto *p)
1362 OVS_EXCLUDED(ofproto_mutex)
1364 struct ofport *ofport, *next_ofport;
1365 struct ofport_usage *usage, *next_usage;
1372 meter_delete(p, 1, p->meter_features.max_meters);
1373 p->meter_features.max_meters = 0;
1379 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1380 ofport_destroy(ofport);
1383 HMAP_FOR_EACH_SAFE (usage, next_usage, hmap_node, &p->ofport_usage) {
1384 hmap_remove(&p->ofport_usage, &usage->hmap_node);
1388 p->ofproto_class->destruct(p);
1389 /* Destroying rules is deferred, must have 'ofproto' around for them. */
1390 ovsrcu_postpone(ofproto_destroy__, p);
1393 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1394 * kernel datapath, for example, this destroys the datapath in the kernel, and
1395 * with the netdev-based datapath, it tears down the data structures that
1396 * represent the datapath.
1398 * The datapath should not be currently open as an ofproto. */
1400 ofproto_delete(const char *name, const char *type)
1402 const struct ofproto_class *class = ofproto_class_find__(type);
1403 return (!class ? EAFNOSUPPORT
1404 : !class->del ? EACCES
1405 : class->del(type, name));
1409 process_port_change(struct ofproto *ofproto, int error, char *devname)
1411 if (error == ENOBUFS) {
1412 reinit_ports(ofproto);
1413 } else if (!error) {
1414 update_port(ofproto, devname);
1420 ofproto_type_run(const char *datapath_type)
1422 const struct ofproto_class *class;
1425 datapath_type = ofproto_normalize_type(datapath_type);
1426 class = ofproto_class_find__(datapath_type);
1428 error = class->type_run ? class->type_run(datapath_type) : 0;
1429 if (error && error != EAGAIN) {
1430 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1431 datapath_type, ovs_strerror(error));
1437 ofproto_type_wait(const char *datapath_type)
1439 const struct ofproto_class *class;
1441 datapath_type = ofproto_normalize_type(datapath_type);
1442 class = ofproto_class_find__(datapath_type);
1444 if (class->type_wait) {
1445 class->type_wait(datapath_type);
1450 any_pending_ops(const struct ofproto *p)
1451 OVS_EXCLUDED(ofproto_mutex)
1455 ovs_mutex_lock(&ofproto_mutex);
1456 b = !list_is_empty(&p->pending);
1457 ovs_mutex_unlock(&ofproto_mutex);
1463 ofproto_run(struct ofproto *p)
1468 error = p->ofproto_class->run(p);
1469 if (error && error != EAGAIN) {
1470 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1473 run_rule_executes(p);
1475 /* Restore the eviction group heap invariant occasionally. */
1476 if (p->eviction_group_timer < time_msec()) {
1479 p->eviction_group_timer = time_msec() + 1000;
1481 for (i = 0; i < p->n_tables; i++) {
1482 struct oftable *table = &p->tables[i];
1483 struct eviction_group *evg;
1484 struct cls_cursor cursor;
1487 if (!table->eviction_fields) {
1491 ovs_mutex_lock(&ofproto_mutex);
1492 fat_rwlock_rdlock(&table->cls.rwlock);
1493 cls_cursor_init(&cursor, &table->cls, NULL);
1494 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1495 if (rule->idle_timeout || rule->hard_timeout) {
1496 if (!rule->eviction_group) {
1497 eviction_group_add_rule(rule);
1499 heap_raw_change(&rule->evg_node,
1500 rule_eviction_priority(p, rule));
1504 fat_rwlock_unlock(&table->cls.rwlock);
1506 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1507 heap_rebuild(&evg->rules);
1509 ovs_mutex_unlock(&ofproto_mutex);
1513 if (p->ofproto_class->port_poll) {
1516 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1517 process_port_change(p, error, devname);
1521 new_seq = seq_read(connectivity_seq_get());
1522 if (new_seq != p->change_seq) {
1523 struct sset devnames;
1524 const char *devname;
1525 struct ofport *ofport;
1527 /* Update OpenFlow port status for any port whose netdev has changed.
1529 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1530 * destroyed, so it's not safe to update ports directly from the
1531 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1532 * need this two-phase approach. */
1533 sset_init(&devnames);
1534 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1535 uint64_t port_change_seq;
1537 port_change_seq = netdev_get_change_seq(ofport->netdev);
1538 if (ofport->change_seq != port_change_seq) {
1539 ofport->change_seq = port_change_seq;
1540 sset_add(&devnames, netdev_get_name(ofport->netdev));
1543 SSET_FOR_EACH (devname, &devnames) {
1544 update_port(p, devname);
1546 sset_destroy(&devnames);
1548 p->change_seq = new_seq;
1553 connmgr_run(p->connmgr, handle_openflow);
1557 connmgr_run(p->connmgr, NULL);
1559 if (!any_pending_ops(p)) {
1560 p->state = S_OPENFLOW;
1565 connmgr_run(p->connmgr, NULL);
1567 if (!any_pending_ops(p)) {
1568 connmgr_flushed(p->connmgr);
1569 p->state = S_OPENFLOW;
1581 ofproto_wait(struct ofproto *p)
1583 p->ofproto_class->wait(p);
1584 if (p->ofproto_class->port_poll_wait) {
1585 p->ofproto_class->port_poll_wait(p);
1587 seq_wait(connectivity_seq_get(), p->change_seq);
1591 connmgr_wait(p->connmgr, true);
1596 connmgr_wait(p->connmgr, false);
1597 if (!any_pending_ops(p)) {
1598 poll_immediate_wake();
1605 ofproto_is_alive(const struct ofproto *p)
1607 return connmgr_has_controllers(p->connmgr);
1610 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1611 * memory_report(). */
1613 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1615 const struct oftable *table;
1616 unsigned int n_rules;
1618 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1620 ovs_mutex_lock(&ofproto_mutex);
1621 simap_increase(usage, "ops",
1622 ofproto->n_pending + hmap_count(&ofproto->deletions));
1623 ovs_mutex_unlock(&ofproto_mutex);
1626 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1627 fat_rwlock_rdlock(&table->cls.rwlock);
1628 n_rules += classifier_count(&table->cls);
1629 fat_rwlock_unlock(&table->cls.rwlock);
1631 simap_increase(usage, "rules", n_rules);
1633 if (ofproto->ofproto_class->get_memory_usage) {
1634 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1637 connmgr_get_memory_usage(ofproto->connmgr, usage);
1641 ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage)
1643 const struct ofproto_class *class;
1645 datapath_type = ofproto_normalize_type(datapath_type);
1646 class = ofproto_class_find__(datapath_type);
1648 if (class && class->type_get_memory_usage) {
1649 class->type_get_memory_usage(datapath_type, usage);
1654 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1657 connmgr_get_controller_info(ofproto->connmgr, info);
1661 ofproto_free_ofproto_controller_info(struct shash *info)
1663 connmgr_free_controller_info(info);
1666 /* Makes a deep copy of 'old' into 'port'. */
1668 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1670 port->name = xstrdup(old->name);
1671 port->type = xstrdup(old->type);
1672 port->ofp_port = old->ofp_port;
1675 /* Frees memory allocated to members of 'ofproto_port'.
1677 * Do not call this function on an ofproto_port obtained from
1678 * ofproto_port_dump_next(): that function retains ownership of the data in the
1681 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1683 free(ofproto_port->name);
1684 free(ofproto_port->type);
1687 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1689 * This function provides no status indication. An error status for the entire
1690 * dump operation is provided when it is completed by calling
1691 * ofproto_port_dump_done().
1694 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1695 const struct ofproto *ofproto)
1697 dump->ofproto = ofproto;
1698 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1702 /* Attempts to retrieve another port from 'dump', which must have been created
1703 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1704 * 'port' and returns true. On failure, returns false.
1706 * Failure might indicate an actual error or merely that the last port has been
1707 * dumped. An error status for the entire dump operation is provided when it
1708 * is completed by calling ofproto_port_dump_done().
1710 * The ofproto owns the data stored in 'port'. It will remain valid until at
1711 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1712 * ofproto_port_dump_done(). */
1714 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1715 struct ofproto_port *port)
1717 const struct ofproto *ofproto = dump->ofproto;
1723 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1726 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1732 /* Completes port table dump operation 'dump', which must have been created
1733 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1734 * error-free, otherwise a positive errno value describing the problem. */
1736 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1738 const struct ofproto *ofproto = dump->ofproto;
1740 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1743 return dump->error == EOF ? 0 : dump->error;
1746 /* Returns the type to pass to netdev_open() when a datapath of type
1747 * 'datapath_type' has a port of type 'port_type', for a few special
1748 * cases when a netdev type differs from a port type. For example, when
1749 * using the userspace datapath, a port of type "internal" needs to be
1752 * Returns either 'type' itself or a string literal, which must not be
1755 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1757 const struct ofproto_class *class;
1759 datapath_type = ofproto_normalize_type(datapath_type);
1760 class = ofproto_class_find__(datapath_type);
1765 return (class->port_open_type
1766 ? class->port_open_type(datapath_type, port_type)
1770 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1771 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1772 * the port's OpenFlow port number.
1774 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1775 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1776 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1777 * 'ofp_portp' is non-null). */
1779 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1780 ofp_port_t *ofp_portp)
1782 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1785 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1787 const char *netdev_name = netdev_get_name(netdev);
1789 simap_put(&ofproto->ofp_requests, netdev_name,
1790 ofp_to_u16(ofp_port));
1791 update_port(ofproto, netdev_name);
1794 *ofp_portp = OFPP_NONE;
1796 struct ofproto_port ofproto_port;
1798 error = ofproto_port_query_by_name(ofproto,
1799 netdev_get_name(netdev),
1802 *ofp_portp = ofproto_port.ofp_port;
1803 ofproto_port_destroy(&ofproto_port);
1810 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1811 * initializes '*port' appropriately; on failure, returns a positive errno
1814 * The caller owns the data in 'ofproto_port' and must free it with
1815 * ofproto_port_destroy() when it is no longer needed. */
1817 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1818 struct ofproto_port *port)
1822 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1824 memset(port, 0, sizeof *port);
1829 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1830 * Returns 0 if successful, otherwise a positive errno. */
1832 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1834 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1835 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1836 struct simap_node *ofp_request_node;
1839 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1840 if (ofp_request_node) {
1841 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1844 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1845 if (!error && ofport) {
1846 /* 'name' is the netdev's name and update_port() is going to close the
1847 * netdev. Just in case update_port() refers to 'name' after it
1848 * destroys 'ofport', make a copy of it around the update_port()
1850 char *devname = xstrdup(name);
1851 update_port(ofproto, devname);
1858 flow_mod_init(struct ofputil_flow_mod *fm,
1859 const struct match *match, unsigned int priority,
1860 const struct ofpact *ofpacts, size_t ofpacts_len,
1861 enum ofp_flow_mod_command command)
1863 memset(fm, 0, sizeof *fm);
1865 fm->priority = priority;
1868 fm->modify_cookie = false;
1870 fm->command = command;
1871 fm->idle_timeout = 0;
1872 fm->hard_timeout = 0;
1873 fm->buffer_id = UINT32_MAX;
1874 fm->out_port = OFPP_ANY;
1875 fm->out_group = OFPG_ANY;
1877 fm->ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1878 fm->ofpacts_len = ofpacts_len;
1879 fm->delete_reason = OFPRR_DELETE;
1883 simple_flow_mod(struct ofproto *ofproto,
1884 const struct match *match, unsigned int priority,
1885 const struct ofpact *ofpacts, size_t ofpacts_len,
1886 enum ofp_flow_mod_command command)
1888 struct ofputil_flow_mod fm;
1890 flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command);
1892 return handle_flow_mod__(ofproto, NULL, &fm, NULL);
1895 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1896 * performs the 'n_actions' actions in 'actions'. The new flow will not
1899 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1900 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1901 * controllers; otherwise, it will be hidden.
1903 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1905 * This is a helper function for in-band control and fail-open. */
1907 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1908 unsigned int priority,
1909 const struct ofpact *ofpacts, size_t ofpacts_len)
1910 OVS_EXCLUDED(ofproto_mutex)
1912 const struct rule *rule;
1915 /* First do a cheap check whether the rule we're looking for already exists
1916 * with the actions that we want. If it does, then we're done. */
1917 fat_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1918 rule = rule_from_cls_rule(classifier_find_match_exactly(
1919 &ofproto->tables[0].cls, match, priority));
1921 const struct rule_actions *actions = rule_get_actions(rule);
1922 must_add = !ofpacts_equal(actions->ofpacts, actions->ofpacts_len,
1923 ofpacts, ofpacts_len);
1927 fat_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1929 /* If there's no such rule or the rule doesn't have the actions we want,
1930 * fall back to a executing a full flow mod. We can't optimize this at
1931 * all because we didn't take enough locks above to ensure that the flow
1932 * table didn't already change beneath us. */
1934 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1935 OFPFC_MODIFY_STRICT);
1939 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1940 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1941 * operation cannot be initiated now but may be retried later.
1943 * This is a helper function for in-band control and fail-open and the "learn"
1946 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1947 OVS_EXCLUDED(ofproto_mutex)
1949 /* Optimize for the most common case of a repeated learn action.
1950 * If an identical flow already exists we only need to update its
1951 * 'modified' time. */
1952 if (fm->command == OFPFC_MODIFY_STRICT && fm->table_id != OFPTT_ALL
1953 && !(fm->flags & OFPUTIL_FF_RESET_COUNTS)) {
1954 struct oftable *table = &ofproto->tables[fm->table_id];
1958 fat_rwlock_rdlock(&table->cls.rwlock);
1959 rule = rule_from_cls_rule(classifier_find_match_exactly(&table->cls,
1963 /* Reading many of the rule fields and writing on 'modified'
1964 * requires the rule->mutex. Also, rule->actions may change
1965 * if rule->mutex is not held. */
1966 const struct rule_actions *actions;
1968 ovs_mutex_lock(&rule->mutex);
1969 actions = rule_get_actions(rule);
1970 if (rule->idle_timeout == fm->idle_timeout
1971 && rule->hard_timeout == fm->hard_timeout
1972 && rule->flags == (fm->flags & OFPUTIL_FF_STATE)
1973 && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie))
1974 && ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
1975 actions->ofpacts, actions->ofpacts_len)) {
1976 /* Rule already exists and need not change, only update the
1977 modified timestamp. */
1978 rule->modified = time_msec();
1981 ovs_mutex_unlock(&rule->mutex);
1983 fat_rwlock_unlock(&table->cls.rwlock);
1990 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1993 /* Searches for a rule with matching criteria exactly equal to 'target' in
1994 * ofproto's table 0 and, if it finds one, deletes it.
1996 * This is a helper function for in-band control and fail-open. */
1998 ofproto_delete_flow(struct ofproto *ofproto,
1999 const struct match *target, unsigned int priority)
2000 OVS_EXCLUDED(ofproto_mutex)
2002 struct classifier *cls = &ofproto->tables[0].cls;
2005 /* First do a cheap check whether the rule we're looking for has already
2006 * been deleted. If so, then we're done. */
2007 fat_rwlock_rdlock(&cls->rwlock);
2008 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
2010 fat_rwlock_unlock(&cls->rwlock);
2015 /* Fall back to a executing a full flow mod. We can't optimize this at all
2016 * because we didn't take enough locks above to ensure that the flow table
2017 * didn't already change beneath us. */
2018 return simple_flow_mod(ofproto, target, priority, NULL, 0,
2019 OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE;
2022 /* Starts the process of deleting all of the flows from all of ofproto's flow
2023 * tables and then reintroducing the flows required by in-band control and
2024 * fail-open. The process will complete in a later call to ofproto_run(). */
2026 ofproto_flush_flows(struct ofproto *ofproto)
2028 COVERAGE_INC(ofproto_flush);
2029 ofproto->state = S_FLUSH;
2033 reinit_ports(struct ofproto *p)
2035 struct ofproto_port_dump dump;
2036 struct sset devnames;
2037 struct ofport *ofport;
2038 struct ofproto_port ofproto_port;
2039 const char *devname;
2041 COVERAGE_INC(ofproto_reinit_ports);
2043 sset_init(&devnames);
2044 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2045 sset_add(&devnames, netdev_get_name(ofport->netdev));
2047 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2048 sset_add(&devnames, ofproto_port.name);
2051 SSET_FOR_EACH (devname, &devnames) {
2052 update_port(p, devname);
2054 sset_destroy(&devnames);
2058 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
2062 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
2063 port_idx = port_idx ? port_idx : UINT16_MAX;
2065 if (port_idx >= ofproto->max_ports
2066 || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) {
2067 uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no;
2068 long long int last_used_at, lru = LLONG_MAX;
2070 /* Search for a free OpenFlow port number. We try not to
2071 * immediately reuse them to prevent problems due to old
2074 * We limit the automatically assigned port numbers to the lower half
2075 * of the port range, to reserve the upper half for assignment by
2078 if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) {
2079 ofproto->alloc_port_no = 1;
2081 last_used_at = ofport_get_usage(ofproto,
2082 u16_to_ofp(ofproto->alloc_port_no));
2083 if (!last_used_at) {
2084 port_idx = ofproto->alloc_port_no;
2086 } else if ( last_used_at < time_msec() - 60*60*1000) {
2087 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2088 * more than an hour ago, consider it usable. */
2089 ofport_remove_usage(ofproto,
2090 u16_to_ofp(ofproto->alloc_port_no));
2091 port_idx = ofproto->alloc_port_no;
2093 } else if (last_used_at < lru) {
2095 lru_ofport = ofproto->alloc_port_no;
2098 if (ofproto->alloc_port_no == end_port_no) {
2100 port_idx = lru_ofport;
2107 ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX);
2108 return u16_to_ofp(port_idx);
2112 dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port)
2114 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
2115 ofport_set_usage(ofproto, ofp_port, time_msec());
2119 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2120 * pointer if the netdev cannot be opened. On success, also fills in
2122 static struct netdev *
2123 ofport_open(struct ofproto *ofproto,
2124 struct ofproto_port *ofproto_port,
2125 struct ofputil_phy_port *pp)
2127 enum netdev_flags flags;
2128 struct netdev *netdev;
2131 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
2133 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
2134 "cannot be opened (%s)",
2136 ofproto_port->name, ofproto_port->ofp_port,
2137 ofproto_port->name, ovs_strerror(error));
2141 if (ofproto_port->ofp_port == OFPP_NONE) {
2142 if (!strcmp(ofproto->name, ofproto_port->name)) {
2143 ofproto_port->ofp_port = OFPP_LOCAL;
2145 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
2146 ofproto_port->name);
2149 pp->port_no = ofproto_port->ofp_port;
2150 netdev_get_etheraddr(netdev, pp->hw_addr);
2151 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
2152 netdev_get_flags(netdev, &flags);
2153 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
2154 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
2155 netdev_get_features(netdev, &pp->curr, &pp->advertised,
2156 &pp->supported, &pp->peer);
2157 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2158 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2163 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2164 * port number, and 'config' bits other than OFPUTIL_PC_PORT_DOWN are
2167 ofport_equal(const struct ofputil_phy_port *a,
2168 const struct ofputil_phy_port *b)
2170 return (eth_addr_equals(a->hw_addr, b->hw_addr)
2171 && a->state == b->state
2172 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
2173 && a->curr == b->curr
2174 && a->advertised == b->advertised
2175 && a->supported == b->supported
2176 && a->peer == b->peer
2177 && a->curr_speed == b->curr_speed
2178 && a->max_speed == b->max_speed);
2181 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2182 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2183 * one with the same name or port number). */
2185 ofport_install(struct ofproto *p,
2186 struct netdev *netdev, const struct ofputil_phy_port *pp)
2188 const char *netdev_name = netdev_get_name(netdev);
2189 struct ofport *ofport;
2192 /* Create ofport. */
2193 ofport = p->ofproto_class->port_alloc();
2198 ofport->ofproto = p;
2199 ofport->netdev = netdev;
2200 ofport->change_seq = netdev_get_change_seq(netdev);
2202 ofport->ofp_port = pp->port_no;
2203 ofport->created = time_msec();
2205 /* Add port to 'p'. */
2206 hmap_insert(&p->ports, &ofport->hmap_node,
2207 hash_ofp_port(ofport->ofp_port));
2208 shash_add(&p->port_by_name, netdev_name, ofport);
2210 update_mtu(p, ofport);
2212 /* Let the ofproto_class initialize its private data. */
2213 error = p->ofproto_class->port_construct(ofport);
2217 connmgr_send_port_status(p->connmgr, NULL, pp, OFPPR_ADD);
2221 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
2222 p->name, netdev_name, ovs_strerror(error));
2224 ofport_destroy__(ofport);
2226 netdev_close(netdev);
2230 /* Removes 'ofport' from 'p' and destroys it. */
2232 ofport_remove(struct ofport *ofport)
2234 connmgr_send_port_status(ofport->ofproto->connmgr, NULL, &ofport->pp,
2236 ofport_destroy(ofport);
2239 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2242 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2244 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2246 ofport_remove(port);
2250 /* Updates 'port' with new 'pp' description.
2252 * Does not handle a name or port number change. The caller must implement
2253 * such a change as a delete followed by an add. */
2255 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
2257 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2258 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2259 | (pp->config & OFPUTIL_PC_PORT_DOWN));
2260 port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN)
2261 | (pp->state & OFPUTIL_PS_LINK_DOWN));
2262 port->pp.curr = pp->curr;
2263 port->pp.advertised = pp->advertised;
2264 port->pp.supported = pp->supported;
2265 port->pp.peer = pp->peer;
2266 port->pp.curr_speed = pp->curr_speed;
2267 port->pp.max_speed = pp->max_speed;
2269 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2270 &port->pp, OFPPR_MODIFY);
2273 /* Update OpenFlow 'state' in 'port' and notify controller. */
2275 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2277 if (port->pp.state != state) {
2278 port->pp.state = state;
2279 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2280 &port->pp, OFPPR_MODIFY);
2285 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2287 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2289 if (port->ofproto->ofproto_class->set_realdev) {
2290 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2292 if (port->ofproto->ofproto_class->set_stp_port) {
2293 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2295 if (port->ofproto->ofproto_class->set_cfm) {
2296 port->ofproto->ofproto_class->set_cfm(port, NULL);
2298 if (port->ofproto->ofproto_class->bundle_remove) {
2299 port->ofproto->ofproto_class->bundle_remove(port);
2305 ofport_destroy__(struct ofport *port)
2307 struct ofproto *ofproto = port->ofproto;
2308 const char *name = netdev_get_name(port->netdev);
2310 hmap_remove(&ofproto->ports, &port->hmap_node);
2311 shash_delete(&ofproto->port_by_name,
2312 shash_find(&ofproto->port_by_name, name));
2314 netdev_close(port->netdev);
2315 ofproto->ofproto_class->port_dealloc(port);
2319 ofport_destroy(struct ofport *port)
2322 dealloc_ofp_port(port->ofproto, port->ofp_port);
2323 port->ofproto->ofproto_class->port_destruct(port);
2324 ofport_destroy__(port);
2329 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2331 struct ofport *port;
2333 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2335 if (port->ofp_port == ofp_port) {
2342 static long long int
2343 ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port)
2345 struct ofport_usage *usage;
2347 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2348 &ofproto->ofport_usage) {
2349 if (usage->ofp_port == ofp_port) {
2350 return usage->last_used;
2357 ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port,
2358 long long int last_used)
2360 struct ofport_usage *usage;
2361 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2362 &ofproto->ofport_usage) {
2363 if (usage->ofp_port == ofp_port) {
2364 usage->last_used = last_used;
2368 ovs_assert(last_used == LLONG_MAX);
2370 usage = xmalloc(sizeof *usage);
2371 usage->ofp_port = ofp_port;
2372 usage->last_used = last_used;
2373 hmap_insert(&ofproto->ofport_usage, &usage->hmap_node,
2374 hash_ofp_port(ofp_port));
2378 ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port)
2380 struct ofport_usage *usage;
2381 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2382 &ofproto->ofport_usage) {
2383 if (usage->ofp_port == ofp_port) {
2384 hmap_remove(&ofproto->ofport_usage, &usage->hmap_node);
2392 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2394 struct ofproto *ofproto = port->ofproto;
2397 if (ofproto->ofproto_class->port_get_stats) {
2398 error = ofproto->ofproto_class->port_get_stats(port, stats);
2407 update_port(struct ofproto *ofproto, const char *name)
2409 struct ofproto_port ofproto_port;
2410 struct ofputil_phy_port pp;
2411 struct netdev *netdev;
2412 struct ofport *port;
2414 COVERAGE_INC(ofproto_update_port);
2416 /* Fetch 'name''s location and properties from the datapath. */
2417 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2418 ? ofport_open(ofproto, &ofproto_port, &pp)
2422 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2423 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2424 struct netdev *old_netdev = port->netdev;
2426 /* 'name' hasn't changed location. Any properties changed? */
2427 if (!ofport_equal(&port->pp, &pp)) {
2428 ofport_modified(port, &pp);
2431 update_mtu(ofproto, port);
2433 /* Install the newly opened netdev in case it has changed.
2434 * Don't close the old netdev yet in case port_modified has to
2435 * remove a retained reference to it.*/
2436 port->netdev = netdev;
2437 port->change_seq = netdev_get_change_seq(netdev);
2439 if (port->ofproto->ofproto_class->port_modified) {
2440 port->ofproto->ofproto_class->port_modified(port);
2443 netdev_close(old_netdev);
2445 /* If 'port' is nonnull then its name differs from 'name' and thus
2446 * we should delete it. If we think there's a port named 'name'
2447 * then its port number must be wrong now so delete it too. */
2449 ofport_remove(port);
2451 ofport_remove_with_name(ofproto, name);
2452 ofport_install(ofproto, netdev, &pp);
2455 /* Any port named 'name' is gone now. */
2456 ofport_remove_with_name(ofproto, name);
2458 ofproto_port_destroy(&ofproto_port);
2462 init_ports(struct ofproto *p)
2464 struct ofproto_port_dump dump;
2465 struct ofproto_port ofproto_port;
2466 struct shash_node *node, *next;
2468 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2469 const char *name = ofproto_port.name;
2471 if (shash_find(&p->port_by_name, name)) {
2472 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2475 struct ofputil_phy_port pp;
2476 struct netdev *netdev;
2478 /* Check if an OpenFlow port number had been requested. */
2479 node = shash_find(&init_ofp_ports, name);
2481 const struct iface_hint *iface_hint = node->data;
2482 simap_put(&p->ofp_requests, name,
2483 ofp_to_u16(iface_hint->ofp_port));
2486 netdev = ofport_open(p, &ofproto_port, &pp);
2488 ofport_install(p, netdev, &pp);
2489 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2490 p->alloc_port_no = MAX(p->alloc_port_no,
2491 ofp_to_u16(ofproto_port.ofp_port));
2497 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2498 struct iface_hint *iface_hint = node->data;
2500 if (!strcmp(iface_hint->br_name, p->name)) {
2501 free(iface_hint->br_name);
2502 free(iface_hint->br_type);
2504 shash_delete(&init_ofp_ports, node);
2511 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2512 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2514 find_min_mtu(struct ofproto *p)
2516 struct ofport *ofport;
2519 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2520 struct netdev *netdev = ofport->netdev;
2523 /* Skip any internal ports, since that's what we're trying to
2525 if (!strcmp(netdev_get_type(netdev), "internal")) {
2529 if (netdev_get_mtu(netdev, &dev_mtu)) {
2532 if (!mtu || dev_mtu < mtu) {
2537 return mtu ? mtu: ETH_PAYLOAD_MAX;
2540 /* Update MTU of all datapath devices on 'p' to the minimum of the
2541 * non-datapath ports in event of 'port' added or changed. */
2543 update_mtu(struct ofproto *p, struct ofport *port)
2545 struct ofport *ofport;
2546 struct netdev *netdev = port->netdev;
2547 int dev_mtu, old_min;
2549 if (netdev_get_mtu(netdev, &dev_mtu)) {
2553 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2554 if (dev_mtu > p->min_mtu) {
2555 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2556 dev_mtu = p->min_mtu;
2559 port->mtu = dev_mtu;
2563 /* For non-internal port find new min mtu. */
2564 old_min = p->min_mtu;
2565 port->mtu = dev_mtu;
2566 p->min_mtu = find_min_mtu(p);
2567 if (p->min_mtu == old_min) {
2571 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2572 struct netdev *netdev = ofport->netdev;
2574 if (!strcmp(netdev_get_type(netdev), "internal")) {
2575 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2576 ofport->mtu = p->min_mtu;
2583 ofproto_rule_destroy__(struct rule *rule)
2584 OVS_NO_THREAD_SAFETY_ANALYSIS
2586 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2587 rule_actions_destroy(rule_get_actions(rule));
2588 ovs_mutex_destroy(&rule->mutex);
2589 rule->ofproto->ofproto_class->rule_dealloc(rule);
2593 rule_destroy_cb(struct rule *rule)
2595 rule->ofproto->ofproto_class->rule_destruct(rule);
2596 ofproto_rule_destroy__(rule);
2600 ofproto_rule_ref(struct rule *rule)
2603 ovs_refcount_ref(&rule->ref_count);
2607 /* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the
2608 * ref_count reaches 0.
2610 * Use of RCU allows short term use (between RCU quiescent periods) without
2611 * keeping a reference. A reference must be taken if the rule needs to
2612 * stay around accross the RCU quiescent periods. */
2614 ofproto_rule_unref(struct rule *rule)
2616 if (rule && ovs_refcount_unref(&rule->ref_count) == 1) {
2617 ovsrcu_postpone(rule_destroy_cb, rule);
2622 ofproto_group_ref(struct ofgroup *group)
2625 ovs_refcount_ref(&group->ref_count);
2630 ofproto_group_unref(struct ofgroup *group)
2632 if (group && ovs_refcount_unref(&group->ref_count) == 1) {
2633 group->ofproto->ofproto_class->group_destruct(group);
2634 ofputil_bucket_list_destroy(&group->buckets);
2635 group->ofproto->ofproto_class->group_dealloc(group);
2639 static uint32_t get_provider_meter_id(const struct ofproto *,
2640 uint32_t of_meter_id);
2642 /* Creates and returns a new 'struct rule_actions', whose actions are a copy
2643 * of from the 'ofpacts_len' bytes of 'ofpacts'. */
2644 const struct rule_actions *
2645 rule_actions_create(const struct ofproto *ofproto,
2646 const struct ofpact *ofpacts, size_t ofpacts_len)
2648 struct rule_actions *actions;
2650 actions = xmalloc(sizeof *actions + ofpacts_len);
2651 actions->ofpacts_len = ofpacts_len;
2652 actions->provider_meter_id
2653 = get_provider_meter_id(ofproto,
2654 ofpacts_get_meter(ofpacts, ofpacts_len));
2655 memcpy(actions->ofpacts, ofpacts, ofpacts_len);
2660 /* Free the actions after the RCU quiescent period is reached. */
2662 rule_actions_destroy(const struct rule_actions *actions)
2665 ovsrcu_postpone(free, CONST_CAST(struct rule_actions *, actions));
2669 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2670 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2672 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2673 OVS_REQUIRES(ofproto_mutex)
2675 if (port == OFPP_ANY) {
2678 const struct rule_actions *actions = rule_get_actions(rule);
2679 return ofpacts_output_to_port(actions->ofpacts,
2680 actions->ofpacts_len, port);
2684 /* Returns true if 'rule' has group and equals group_id. */
2686 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2687 OVS_REQUIRES(ofproto_mutex)
2689 if (group_id == OFPG_ANY) {
2692 const struct rule_actions *actions = rule_get_actions(rule);
2693 return ofpacts_output_to_group(actions->ofpacts,
2694 actions->ofpacts_len, group_id);
2698 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2699 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2701 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2702 OVS_REQUIRES(ofproto_mutex)
2704 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2709 case OFOPERATION_ADD:
2710 case OFOPERATION_DELETE:
2713 case OFOPERATION_MODIFY:
2714 case OFOPERATION_REPLACE:
2715 return ofpacts_output_to_port(op->actions->ofpacts,
2716 op->actions->ofpacts_len, out_port);
2723 rule_execute_destroy(struct rule_execute *e)
2725 ofproto_rule_unref(e->rule);
2726 list_remove(&e->list_node);
2730 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2731 * by passing them to the ofproto provider. */
2733 run_rule_executes(struct ofproto *ofproto)
2734 OVS_EXCLUDED(ofproto_mutex)
2736 struct rule_execute *e, *next;
2737 struct list executes;
2739 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2740 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2743 flow_extract(e->packet, NULL, &flow);
2744 flow.in_port.ofp_port = e->in_port;
2745 ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet);
2747 rule_execute_destroy(e);
2751 /* Destroys and discards all "rule_execute" operations queued up in
2752 * ofproto->rule_executes. */
2754 destroy_rule_executes(struct ofproto *ofproto)
2756 struct rule_execute *e, *next;
2757 struct list executes;
2759 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2760 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2761 ofpbuf_delete(e->packet);
2762 rule_execute_destroy(e);
2767 oftable_is_modifiable(const struct oftable *table,
2768 enum ofputil_flow_mod_flags flags)
2770 if (flags & OFPUTIL_FF_NO_READONLY) {
2774 return !(table->flags & OFTABLE_READONLY);
2778 rule_is_modifiable(const struct rule *rule, enum ofputil_flow_mod_flags flags)
2780 const struct oftable *rule_table;
2782 rule_table = &rule->ofproto->tables[rule->table_id];
2783 return oftable_is_modifiable(rule_table, flags);
2787 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2789 ofconn_send_reply(ofconn, make_echo_reply(oh));
2794 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2796 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2797 struct ofputil_switch_features features;
2798 struct ofport *port;
2802 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2804 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2806 features.datapath_id = ofproto->datapath_id;
2807 features.n_buffers = pktbuf_capacity();
2808 features.n_tables = ofproto_get_n_visible_tables(ofproto);
2809 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2810 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2812 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2814 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2815 features.auxiliary_id = 0;
2816 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2818 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2819 ofputil_put_switch_features_port(&port->pp, b);
2822 ofconn_send_reply(ofconn, b);
2827 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2829 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2830 struct ofp_switch_config *osc;
2831 enum ofp_config_flags flags;
2835 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2836 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2837 flags = ofproto->frag_handling;
2838 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2839 if (oh->version < OFP13_VERSION
2840 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2841 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2843 osc->flags = htons(flags);
2844 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2845 ofconn_send_reply(ofconn, buf);
2851 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2853 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2854 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2855 uint16_t flags = ntohs(osc->flags);
2857 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2858 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2859 enum ofp_config_flags cur = ofproto->frag_handling;
2860 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2862 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2864 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2865 ofproto->frag_handling = next;
2867 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2869 ofputil_frag_handling_to_string(next));
2873 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2874 ofconn_set_invalid_ttl_to_controller(ofconn,
2875 (oh->version < OFP13_VERSION
2876 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2878 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2883 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2884 * error message code for the caller to propagate upward. Otherwise, returns
2887 * The log message mentions 'msg_type'. */
2889 reject_slave_controller(struct ofconn *ofconn)
2891 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2892 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2893 return OFPERR_OFPBRC_EPERM;
2899 /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
2902 * - If they use a meter, then 'ofproto' has that meter configured.
2904 * - If they use any groups, then 'ofproto' has that group configured.
2906 * Returns 0 if successful, otherwise an OpenFlow error. */
2908 ofproto_check_ofpacts(struct ofproto *ofproto,
2909 const struct ofpact ofpacts[], size_t ofpacts_len)
2911 const struct ofpact *a;
2914 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2915 if (mid && get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2916 return OFPERR_OFPMMFC_INVALID_METER;
2919 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2920 if (a->type == OFPACT_GROUP
2921 && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) {
2922 return OFPERR_OFPBAC_BAD_OUT_GROUP;
2930 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2932 struct ofproto *p = ofconn_get_ofproto(ofconn);
2933 struct ofputil_packet_out po;
2934 struct ofpbuf *payload;
2935 uint64_t ofpacts_stub[1024 / 8];
2936 struct ofpbuf ofpacts;
2940 COVERAGE_INC(ofproto_packet_out);
2942 error = reject_slave_controller(ofconn);
2947 /* Decode message. */
2948 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2949 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2951 goto exit_free_ofpacts;
2953 if (ofp_to_u16(po.in_port) >= p->max_ports
2954 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2955 error = OFPERR_OFPBRC_BAD_PORT;
2956 goto exit_free_ofpacts;
2960 if (po.buffer_id != UINT32_MAX) {
2961 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2962 if (error || !payload) {
2963 goto exit_free_ofpacts;
2966 /* Ensure that the L3 header is 32-bit aligned. */
2967 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2970 /* Verify actions against packet, then send packet if successful. */
2971 flow_extract(payload, NULL, &flow);
2972 flow.in_port.ofp_port = po.in_port;
2973 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len);
2975 error = p->ofproto_class->packet_out(p, payload, &flow,
2976 po.ofpacts, po.ofpacts_len);
2978 ofpbuf_delete(payload);
2981 ofpbuf_uninit(&ofpacts);
2987 update_port_config(struct ofconn *ofconn, struct ofport *port,
2988 enum ofputil_port_config config,
2989 enum ofputil_port_config mask)
2991 enum ofputil_port_config toggle = (config ^ port->pp.config) & mask;
2993 if (toggle & OFPUTIL_PC_PORT_DOWN
2994 && (config & OFPUTIL_PC_PORT_DOWN
2995 ? netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL)
2996 : netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL))) {
2997 /* We tried to bring the port up or down, but it failed, so don't
2998 * update the "down" bit. */
2999 toggle &= ~OFPUTIL_PC_PORT_DOWN;
3003 enum ofputil_port_config old_config = port->pp.config;
3004 port->pp.config ^= toggle;
3005 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
3006 connmgr_send_port_status(port->ofproto->connmgr, ofconn, &port->pp,
3012 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3014 struct ofproto *p = ofconn_get_ofproto(ofconn);
3015 struct ofputil_port_mod pm;
3016 struct ofport *port;
3019 error = reject_slave_controller(ofconn);
3024 error = ofputil_decode_port_mod(oh, &pm, false);
3029 port = ofproto_get_port(p, pm.port_no);
3031 return OFPERR_OFPPMFC_BAD_PORT;
3032 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
3033 return OFPERR_OFPPMFC_BAD_HW_ADDR;
3035 update_port_config(ofconn, port, pm.config, pm.mask);
3037 netdev_set_advertisements(port->netdev, pm.advertise);
3044 handle_desc_stats_request(struct ofconn *ofconn,
3045 const struct ofp_header *request)
3047 static const char *default_mfr_desc = "Nicira, Inc.";
3048 static const char *default_hw_desc = "Open vSwitch";
3049 static const char *default_sw_desc = VERSION;
3050 static const char *default_serial_desc = "None";
3051 static const char *default_dp_desc = "None";
3053 struct ofproto *p = ofconn_get_ofproto(ofconn);
3054 struct ofp_desc_stats *ods;
3057 msg = ofpraw_alloc_stats_reply(request, 0);
3058 ods = ofpbuf_put_zeros(msg, sizeof *ods);
3059 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
3060 sizeof ods->mfr_desc);
3061 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
3062 sizeof ods->hw_desc);
3063 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
3064 sizeof ods->sw_desc);
3065 ovs_strlcpy(ods->serial_num,
3066 p->serial_desc ? p->serial_desc : default_serial_desc,
3067 sizeof ods->serial_num);
3068 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
3069 sizeof ods->dp_desc);
3070 ofconn_send_reply(ofconn, msg);
3076 handle_table_stats_request(struct ofconn *ofconn,
3077 const struct ofp_header *request)
3079 struct ofproto *p = ofconn_get_ofproto(ofconn);
3080 struct ofp12_table_stats *ots;
3085 /* Set up default values.
3087 * ofp12_table_stats is used as a generic structure as
3088 * it is able to hold all the fields for ofp10_table_stats
3089 * and ofp11_table_stats (and of course itself).
3091 ots = xcalloc(p->n_tables, sizeof *ots);
3092 for (i = 0; i < p->n_tables; i++) {
3093 ots[i].table_id = i;
3094 sprintf(ots[i].name, "table%"PRIuSIZE, i);
3095 ots[i].match = htonll(OFPXMT13_MASK);
3096 ots[i].wildcards = htonll(OFPXMT13_MASK);
3097 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
3098 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
3099 ots[i].write_setfields = htonll(OFPXMT13_MASK);
3100 ots[i].apply_setfields = htonll(OFPXMT13_MASK);
3101 ots[i].metadata_match = OVS_BE64_MAX;
3102 ots[i].metadata_write = OVS_BE64_MAX;
3103 ots[i].instructions = htonl(OFPIT11_ALL);
3104 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
3105 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
3106 fat_rwlock_rdlock(&p->tables[i].cls.rwlock);
3107 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
3108 fat_rwlock_unlock(&p->tables[i].cls.rwlock);
3111 p->ofproto_class->get_tables(p, ots);
3113 /* Post-process the tables, dropping hidden tables. */
3114 n_tables = p->n_tables;
3115 for (i = 0; i < p->n_tables; i++) {
3116 const struct oftable *table = &p->tables[i];
3118 if (table->flags & OFTABLE_HIDDEN) {
3124 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
3127 if (table->max_flows < ntohl(ots[i].max_entries)) {
3128 ots[i].max_entries = htonl(table->max_flows);
3132 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
3133 ofconn_send_reply(ofconn, msg);
3141 append_port_stat(struct ofport *port, struct list *replies)
3143 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
3145 calc_duration(port->created, time_msec(),
3146 &ops.duration_sec, &ops.duration_nsec);
3148 /* Intentionally ignore return value, since errors will set
3149 * 'stats' to all-1s, which is correct for OpenFlow, and
3150 * netdev_get_stats() will log errors. */
3151 ofproto_port_get_stats(port, &ops.stats);
3153 ofputil_append_port_stat(replies, &ops);
3157 handle_port_request(struct ofconn *ofconn,
3158 const struct ofp_header *request, ofp_port_t port_no,
3159 void (*cb)(struct ofport *, struct list *replies))
3161 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3162 struct ofport *port;
3163 struct list replies;
3165 ofpmp_init(&replies, request);
3166 if (port_no != OFPP_ANY) {
3167 port = ofproto_get_port(ofproto, port_no);
3172 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3177 ofconn_send_replies(ofconn, &replies);
3181 handle_port_stats_request(struct ofconn *ofconn,
3182 const struct ofp_header *request)
3187 error = ofputil_decode_port_stats_request(request, &port_no);
3189 handle_port_request(ofconn, request, port_no, append_port_stat);
3195 append_port_desc(struct ofport *port, struct list *replies)
3197 ofputil_append_port_desc_stats_reply(&port->pp, replies);
3201 handle_port_desc_stats_request(struct ofconn *ofconn,
3202 const struct ofp_header *request)
3207 error = ofputil_decode_port_desc_stats_request(request, &port_no);
3209 handle_port_request(ofconn, request, port_no, append_port_desc);
3215 hash_cookie(ovs_be64 cookie)
3217 return hash_uint64((OVS_FORCE uint64_t)cookie);
3221 cookies_insert(struct ofproto *ofproto, struct rule *rule)
3222 OVS_REQUIRES(ofproto_mutex)
3224 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3225 hash_cookie(rule->flow_cookie));
3229 cookies_remove(struct ofproto *ofproto, struct rule *rule)
3230 OVS_REQUIRES(ofproto_mutex)
3232 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3236 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
3237 ovs_be64 new_cookie)
3238 OVS_REQUIRES(ofproto_mutex)
3240 if (new_cookie != rule->flow_cookie) {
3241 cookies_remove(ofproto, rule);
3243 ovs_mutex_lock(&rule->mutex);
3244 rule->flow_cookie = new_cookie;
3245 ovs_mutex_unlock(&rule->mutex);
3247 cookies_insert(ofproto, rule);
3252 calc_duration(long long int start, long long int now,
3253 uint32_t *sec, uint32_t *nsec)
3255 long long int msecs = now - start;
3256 *sec = msecs / 1000;
3257 *nsec = (msecs % 1000) * (1000 * 1000);
3260 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3261 * true if 'table_id' is OK, false otherwise. */
3263 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3265 return table_id == OFPTT_ALL || table_id < ofproto->n_tables;
3268 static struct oftable *
3269 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
3271 struct oftable *table;
3273 for (table = &ofproto->tables[table_id];
3274 table < &ofproto->tables[ofproto->n_tables];
3276 if (!(table->flags & OFTABLE_HIDDEN)) {
3284 static struct oftable *
3285 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
3287 if (table_id == 0xff) {
3288 return next_visible_table(ofproto, 0);
3289 } else if (table_id < ofproto->n_tables) {
3290 return &ofproto->tables[table_id];
3296 static struct oftable *
3297 next_matching_table(const struct ofproto *ofproto,
3298 const struct oftable *table, uint8_t table_id)
3300 return (table_id == 0xff
3301 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
3305 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3307 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3308 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3310 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3311 * only once, for that table. (This can be used to access tables marked
3314 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3315 * entered at all. (Perhaps you should have validated TABLE_ID with
3316 * check_table_id().)
3318 * All parameters are evaluated multiple times.
3320 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3321 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3323 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3325 /* Initializes 'criteria' in a straightforward way based on the other
3328 * By default, the criteria include flows that are read-only, on the assumption
3329 * that the collected flows won't be modified. Call rule_criteria_require_rw()
3330 * if flows will be modified.
3332 * For "loose" matching, the 'priority' parameter is unimportant and may be
3335 rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
3336 const struct match *match, unsigned int priority,
3337 ovs_be64 cookie, ovs_be64 cookie_mask,
3338 ofp_port_t out_port, uint32_t out_group)
3340 criteria->table_id = table_id;
3341 cls_rule_init(&criteria->cr, match, priority);
3342 criteria->cookie = cookie;
3343 criteria->cookie_mask = cookie_mask;
3344 criteria->out_port = out_port;
3345 criteria->out_group = out_group;
3347 /* We ordinarily want to skip hidden rules, but there has to be a way for
3348 * code internal to OVS to modify and delete them, so if the criteria
3349 * specify a priority that can only be for a hidden flow, then allow hidden
3350 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
3351 * with hidden flows because OpenFlow uses only a 16-bit field to specify
3353 criteria->include_hidden = priority > UINT16_MAX;
3355 /* We assume that the criteria are being used to collect flows for reading
3356 * but not modification. Thus, we should collect read-only flows. */
3357 criteria->include_readonly = true;
3360 /* By default, criteria initialized by rule_criteria_init() will match flows
3361 * that are read-only, on the assumption that the collected flows won't be
3362 * modified. Call this function to match only flows that are be modifiable.
3364 * Specify 'can_write_readonly' as false in ordinary circumstances, true if the
3365 * caller has special privileges that allow it to modify even "read-only"
3368 rule_criteria_require_rw(struct rule_criteria *criteria,
3369 bool can_write_readonly)
3371 criteria->include_readonly = can_write_readonly;
3375 rule_criteria_destroy(struct rule_criteria *criteria)
3377 cls_rule_destroy(&criteria->cr);
3381 rule_collection_init(struct rule_collection *rules)
3383 rules->rules = rules->stub;
3385 rules->capacity = ARRAY_SIZE(rules->stub);
3389 rule_collection_add(struct rule_collection *rules, struct rule *rule)
3391 if (rules->n >= rules->capacity) {
3392 size_t old_size, new_size;
3394 old_size = rules->capacity * sizeof *rules->rules;
3395 rules->capacity *= 2;
3396 new_size = rules->capacity * sizeof *rules->rules;
3398 if (rules->rules == rules->stub) {
3399 rules->rules = xmalloc(new_size);
3400 memcpy(rules->rules, rules->stub, old_size);
3402 rules->rules = xrealloc(rules->rules, new_size);
3406 rules->rules[rules->n++] = rule;
3410 rule_collection_ref(struct rule_collection *rules)
3411 OVS_REQUIRES(ofproto_mutex)
3415 for (i = 0; i < rules->n; i++) {
3416 ofproto_rule_ref(rules->rules[i]);
3421 rule_collection_unref(struct rule_collection *rules)
3425 for (i = 0; i < rules->n; i++) {
3426 ofproto_rule_unref(rules->rules[i]);
3431 rule_collection_destroy(struct rule_collection *rules)
3433 if (rules->rules != rules->stub) {
3438 /* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This
3439 * function verifies most of the criteria in 'c' itself, but the caller must
3440 * check 'c->cr' itself.
3442 * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and
3443 * 'c' only includes modifiable rules).
3445 * Returns 0 ordinarily, but OFPROTO_POSTPONE if we would otherwise collect a
3446 * rule that has a pending operation. */
3448 collect_rule(struct rule *rule, const struct rule_criteria *c,
3449 struct rule_collection *rules, size_t *n_readonly)
3450 OVS_REQUIRES(ofproto_mutex)
3452 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3453 && ofproto_rule_has_out_port(rule, c->out_port)
3454 && ofproto_rule_has_out_group(rule, c->out_group)
3455 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)
3456 && (!rule_is_hidden(rule) || c->include_hidden)) {
3457 if (rule->pending) {
3458 return OFPROTO_POSTPONE;
3461 /* Rule matches all the criteria... */
3462 if (rule_is_modifiable(rule, 0) || c->include_readonly) {
3464 rule_collection_add(rules, rule);
3466 /* ...except it's read-only. */
3473 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3474 * on classifiers rules are done in the "loose" way required for OpenFlow
3475 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3478 * Returns 0 on success, otherwise an OpenFlow error code. */
3480 collect_rules_loose(struct ofproto *ofproto,
3481 const struct rule_criteria *criteria,
3482 struct rule_collection *rules)
3483 OVS_REQUIRES(ofproto_mutex)
3485 struct oftable *table;
3486 enum ofperr error = 0;
3487 size_t n_readonly = 0;
3489 rule_collection_init(rules);
3491 if (!check_table_id(ofproto, criteria->table_id)) {
3492 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3496 if (criteria->cookie_mask == OVS_BE64_MAX) {
3499 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3500 hash_cookie(criteria->cookie),
3501 &ofproto->cookies) {
3502 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
3503 error = collect_rule(rule, criteria, rules, &n_readonly);
3510 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3511 struct cls_cursor cursor;
3514 fat_rwlock_rdlock(&table->cls.rwlock);
3515 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
3516 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3517 error = collect_rule(rule, criteria, rules, &n_readonly);
3522 fat_rwlock_unlock(&table->cls.rwlock);
3527 if (!error && !rules->n && n_readonly) {
3528 /* We didn't find any rules to modify. We did find some read-only
3529 * rules that we're not allowed to modify, so report that. */
3530 error = OFPERR_OFPBRC_EPERM;
3533 rule_collection_destroy(rules);
3538 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3539 * on classifiers rules are done in the "strict" way required for OpenFlow
3540 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3541 * rules on list 'rules'.
3543 * Returns 0 on success, otherwise an OpenFlow error code. */
3545 collect_rules_strict(struct ofproto *ofproto,
3546 const struct rule_criteria *criteria,
3547 struct rule_collection *rules)
3548 OVS_REQUIRES(ofproto_mutex)
3550 struct oftable *table;
3551 size_t n_readonly = 0;
3554 rule_collection_init(rules);
3556 if (!check_table_id(ofproto, criteria->table_id)) {
3557 error = OFPERR_OFPBRC_BAD_TABLE_ID;
3561 if (criteria->cookie_mask == OVS_BE64_MAX) {
3564 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3565 hash_cookie(criteria->cookie),
3566 &ofproto->cookies) {
3567 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
3568 error = collect_rule(rule, criteria, rules, &n_readonly);
3575 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3578 fat_rwlock_rdlock(&table->cls.rwlock);
3579 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3580 &table->cls, &criteria->cr));
3581 fat_rwlock_unlock(&table->cls.rwlock);
3583 error = collect_rule(rule, criteria, rules, &n_readonly);
3593 rule_collection_destroy(rules);
3598 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3599 * forced into the range of a uint16_t. */
3601 age_secs(long long int age_ms)
3603 return (age_ms < 0 ? 0
3604 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3605 : (unsigned int) age_ms / 1000);
3609 handle_flow_stats_request(struct ofconn *ofconn,
3610 const struct ofp_header *request)
3611 OVS_EXCLUDED(ofproto_mutex)
3613 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3614 struct ofputil_flow_stats_request fsr;
3615 struct rule_criteria criteria;
3616 struct rule_collection rules;
3617 struct list replies;
3621 error = ofputil_decode_flow_stats_request(&fsr, request);
3626 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3627 fsr.cookie_mask, fsr.out_port, fsr.out_group);
3629 ovs_mutex_lock(&ofproto_mutex);
3630 error = collect_rules_loose(ofproto, &criteria, &rules);
3631 rule_criteria_destroy(&criteria);
3633 rule_collection_ref(&rules);
3635 ovs_mutex_unlock(&ofproto_mutex);
3641 ofpmp_init(&replies, request);
3642 for (i = 0; i < rules.n; i++) {
3643 struct rule *rule = rules.rules[i];
3644 long long int now = time_msec();
3645 struct ofputil_flow_stats fs;
3646 long long int created, used, modified;
3647 const struct rule_actions *actions;
3648 enum ofputil_flow_mod_flags flags;
3650 ovs_mutex_lock(&rule->mutex);
3651 fs.cookie = rule->flow_cookie;
3652 fs.idle_timeout = rule->idle_timeout;
3653 fs.hard_timeout = rule->hard_timeout;
3654 created = rule->created;
3655 modified = rule->modified;
3656 actions = rule_get_actions(rule);
3657 flags = rule->flags;
3658 ovs_mutex_unlock(&rule->mutex);
3660 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3661 &fs.byte_count, &used);
3663 minimatch_expand(&rule->cr.match, &fs.match);
3664 fs.table_id = rule->table_id;
3665 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
3666 fs.priority = rule->cr.priority;
3667 fs.idle_age = age_secs(now - used);
3668 fs.hard_age = age_secs(now - modified);
3669 fs.ofpacts = actions->ofpacts;
3670 fs.ofpacts_len = actions->ofpacts_len;
3673 ofputil_append_flow_stats_reply(&fs, &replies);
3676 rule_collection_unref(&rules);
3677 rule_collection_destroy(&rules);
3679 ofconn_send_replies(ofconn, &replies);
3685 flow_stats_ds(struct rule *rule, struct ds *results)
3687 uint64_t packet_count, byte_count;
3688 const struct rule_actions *actions;
3689 long long int created, used;
3691 rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3692 &byte_count, &used);
3694 ovs_mutex_lock(&rule->mutex);
3695 actions = rule_get_actions(rule);
3696 created = rule->created;
3697 ovs_mutex_unlock(&rule->mutex);
3699 if (rule->table_id != 0) {
3700 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3702 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
3703 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3704 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3705 cls_rule_format(&rule->cr, results);
3706 ds_put_char(results, ',');
3708 ds_put_cstr(results, "actions=");
3709 ofpacts_format(actions->ofpacts, actions->ofpacts_len, results);
3711 ds_put_cstr(results, "\n");
3714 /* Adds a pretty-printed description of all flows to 'results', including
3715 * hidden flows (e.g., set up by in-band control). */
3717 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3719 struct oftable *table;
3721 OFPROTO_FOR_EACH_TABLE (table, p) {
3722 struct cls_cursor cursor;
3725 fat_rwlock_rdlock(&table->cls.rwlock);
3726 cls_cursor_init(&cursor, &table->cls, NULL);
3727 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3728 flow_stats_ds(rule, results);
3730 fat_rwlock_unlock(&table->cls.rwlock);
3734 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3735 * '*engine_type' and '*engine_id', respectively. */
3737 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3738 uint8_t *engine_type, uint8_t *engine_id)
3740 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3743 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'.
3744 * Returns 0 if the port's CFM status was successfully stored into
3745 * '*status'. Returns positive errno if the port did not have CFM
3746 * configured. Returns negative number if there is no status change
3747 * since last update.
3749 * The caller must provide and own '*status', and must free 'status->rmps'.
3750 * '*status' is indeterminate if the return value is non-zero. */
3752 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3753 struct ofproto_cfm_status *status)
3755 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3756 return (ofport && ofproto->ofproto_class->get_cfm_status
3757 ? ofproto->ofproto_class->get_cfm_status(ofport, status)
3762 handle_aggregate_stats_request(struct ofconn *ofconn,
3763 const struct ofp_header *oh)
3764 OVS_EXCLUDED(ofproto_mutex)
3766 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3767 struct ofputil_flow_stats_request request;
3768 struct ofputil_aggregate_stats stats;
3769 bool unknown_packets, unknown_bytes;
3770 struct rule_criteria criteria;
3771 struct rule_collection rules;
3772 struct ofpbuf *reply;
3776 error = ofputil_decode_flow_stats_request(&request, oh);
3781 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3782 request.cookie, request.cookie_mask,
3783 request.out_port, request.out_group);
3785 ovs_mutex_lock(&ofproto_mutex);
3786 error = collect_rules_loose(ofproto, &criteria, &rules);
3787 rule_criteria_destroy(&criteria);
3789 rule_collection_ref(&rules);
3791 ovs_mutex_unlock(&ofproto_mutex);
3797 memset(&stats, 0, sizeof stats);
3798 unknown_packets = unknown_bytes = false;
3799 for (i = 0; i < rules.n; i++) {
3800 struct rule *rule = rules.rules[i];
3801 uint64_t packet_count;
3802 uint64_t byte_count;
3805 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3806 &byte_count, &used);
3808 if (packet_count == UINT64_MAX) {
3809 unknown_packets = true;
3811 stats.packet_count += packet_count;
3814 if (byte_count == UINT64_MAX) {
3815 unknown_bytes = true;
3817 stats.byte_count += byte_count;
3822 if (unknown_packets) {
3823 stats.packet_count = UINT64_MAX;
3825 if (unknown_bytes) {
3826 stats.byte_count = UINT64_MAX;
3829 rule_collection_unref(&rules);
3830 rule_collection_destroy(&rules);
3832 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3833 ofconn_send_reply(ofconn, reply);
3838 struct queue_stats_cbdata {
3839 struct ofport *ofport;
3840 struct list replies;
3845 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3846 const struct netdev_queue_stats *stats)
3848 struct ofputil_queue_stats oqs;
3850 oqs.port_no = cbdata->ofport->pp.port_no;
3851 oqs.queue_id = queue_id;
3852 oqs.tx_bytes = stats->tx_bytes;
3853 oqs.tx_packets = stats->tx_packets;
3854 oqs.tx_errors = stats->tx_errors;
3855 if (stats->created != LLONG_MIN) {
3856 calc_duration(stats->created, cbdata->now,
3857 &oqs.duration_sec, &oqs.duration_nsec);
3859 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3861 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3865 handle_queue_stats_dump_cb(uint32_t queue_id,
3866 struct netdev_queue_stats *stats,
3869 struct queue_stats_cbdata *cbdata = cbdata_;
3871 put_queue_stats(cbdata, queue_id, stats);
3875 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3876 struct queue_stats_cbdata *cbdata)
3878 cbdata->ofport = port;
3879 if (queue_id == OFPQ_ALL) {
3880 netdev_dump_queue_stats(port->netdev,
3881 handle_queue_stats_dump_cb, cbdata);
3883 struct netdev_queue_stats stats;
3885 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3886 put_queue_stats(cbdata, queue_id, &stats);
3888 return OFPERR_OFPQOFC_BAD_QUEUE;
3895 handle_queue_stats_request(struct ofconn *ofconn,
3896 const struct ofp_header *rq)
3898 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3899 struct queue_stats_cbdata cbdata;
3900 struct ofport *port;
3902 struct ofputil_queue_stats_request oqsr;
3904 COVERAGE_INC(ofproto_queue_req);
3906 ofpmp_init(&cbdata.replies, rq);
3907 cbdata.now = time_msec();
3909 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3914 if (oqsr.port_no == OFPP_ANY) {
3915 error = OFPERR_OFPQOFC_BAD_QUEUE;
3916 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3917 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3922 port = ofproto_get_port(ofproto, oqsr.port_no);
3924 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3925 : OFPERR_OFPQOFC_BAD_PORT);
3928 ofconn_send_replies(ofconn, &cbdata.replies);
3930 ofpbuf_list_delete(&cbdata.replies);
3937 is_flow_deletion_pending(const struct ofproto *ofproto,
3938 const struct cls_rule *cls_rule,
3940 OVS_REQUIRES(ofproto_mutex)
3942 if (!hmap_is_empty(&ofproto->deletions)) {
3943 struct ofoperation *op;
3945 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3946 cls_rule_hash(cls_rule, table_id),
3947 &ofproto->deletions) {
3948 if (op->rule->table_id == table_id
3949 && cls_rule_equal(cls_rule, &op->rule->cr)) {
3959 should_evict_a_rule(struct oftable *table, unsigned int extra_space)
3960 OVS_REQUIRES(ofproto_mutex)
3961 OVS_NO_THREAD_SAFETY_ANALYSIS
3963 return classifier_count(&table->cls) + extra_space > table->max_flows;
3967 evict_rules_from_table(struct ofproto *ofproto, struct oftable *table,
3968 unsigned int extra_space)
3969 OVS_REQUIRES(ofproto_mutex)
3971 while (should_evict_a_rule(table, extra_space)) {
3974 if (!choose_rule_to_evict(table, &rule)) {
3975 return OFPERR_OFPFMFC_TABLE_FULL;
3976 } else if (rule->pending) {
3977 return OFPROTO_POSTPONE;
3979 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
3980 delete_flow__(rule, group, OFPRR_EVICTION);
3981 ofopgroup_submit(group);
3988 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3989 * in which no matching flow already exists in the flow table.
3991 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3992 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3993 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3994 * initiated now but may be retried later.
3996 * The caller retains ownership of 'fm->ofpacts'.
3998 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
4001 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
4002 struct ofputil_flow_mod *fm, const struct ofp_header *request)
4003 OVS_REQUIRES(ofproto_mutex)
4005 const struct rule_actions *actions;
4006 struct ofopgroup *group;
4007 struct oftable *table;
4013 if (!check_table_id(ofproto, fm->table_id)) {
4014 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4019 if (fm->table_id == 0xff) {
4020 if (ofproto->ofproto_class->rule_choose_table) {
4021 error = ofproto->ofproto_class->rule_choose_table(ofproto,
4027 ovs_assert(table_id < ofproto->n_tables);
4031 } else if (fm->table_id < ofproto->n_tables) {
4032 table_id = fm->table_id;
4034 return OFPERR_OFPBRC_BAD_TABLE_ID;
4037 table = &ofproto->tables[table_id];
4039 if (!oftable_is_modifiable(table, fm->flags)) {
4040 return OFPERR_OFPBRC_EPERM;
4043 if (!(fm->flags & OFPUTIL_FF_HIDDEN_FIELDS)) {
4044 if (!match_has_default_hidden_fields(&fm->match)) {
4045 VLOG_WARN_RL(&rl, "%s: (add_flow) only internal flows can set "
4046 "non-default values to hidden fields", ofproto->name);
4047 return OFPERR_OFPBRC_EPERM;
4051 cls_rule_init(&cr, &fm->match, fm->priority);
4053 /* Transform "add" into "modify" if there's an existing identical flow. */
4054 fat_rwlock_rdlock(&table->cls.rwlock);
4055 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
4056 fat_rwlock_unlock(&table->cls.rwlock);
4058 cls_rule_destroy(&cr);
4059 if (!rule_is_modifiable(rule, fm->flags)) {
4060 return OFPERR_OFPBRC_EPERM;
4061 } else if (rule->pending) {
4062 return OFPROTO_POSTPONE;
4064 struct rule_collection rules;
4066 rule_collection_init(&rules);
4067 rule_collection_add(&rules, rule);
4068 fm->modify_cookie = true;
4069 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4070 rule_collection_destroy(&rules);
4076 /* Serialize against pending deletion. */
4077 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
4078 cls_rule_destroy(&cr);
4079 return OFPROTO_POSTPONE;
4082 /* Check for overlap, if requested. */
4083 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
4086 fat_rwlock_rdlock(&table->cls.rwlock);
4087 overlaps = classifier_rule_overlaps(&table->cls, &cr);
4088 fat_rwlock_unlock(&table->cls.rwlock);
4091 cls_rule_destroy(&cr);
4092 return OFPERR_OFPFMFC_OVERLAP;
4096 /* If necessary, evict an existing rule to clear out space. */
4097 error = evict_rules_from_table(ofproto, table, 1);
4099 cls_rule_destroy(&cr);
4103 /* Allocate new rule. */
4104 rule = ofproto->ofproto_class->rule_alloc();
4106 cls_rule_destroy(&cr);
4107 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
4108 ofproto->name, ovs_strerror(error));
4112 /* Initialize base state. */
4113 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4114 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
4115 ovs_refcount_init(&rule->ref_count);
4116 rule->pending = NULL;
4117 rule->flow_cookie = fm->new_cookie;
4118 rule->created = rule->modified = time_msec();
4120 ovs_mutex_init(&rule->mutex);
4121 ovs_mutex_lock(&rule->mutex);
4122 rule->idle_timeout = fm->idle_timeout;
4123 rule->hard_timeout = fm->hard_timeout;
4124 ovs_mutex_unlock(&rule->mutex);
4126 *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
4127 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4128 actions = rule_actions_create(ofproto, fm->ofpacts, fm->ofpacts_len);
4129 ovsrcu_set(&rule->actions, actions);
4130 list_init(&rule->meter_list_node);
4131 rule->eviction_group = NULL;
4132 list_init(&rule->expirable);
4133 rule->monitor_flags = 0;
4134 rule->add_seqno = 0;
4135 rule->modify_seqno = 0;
4137 /* Construct rule, initializing derived state. */
4138 error = ofproto->ofproto_class->rule_construct(rule);
4140 ofproto_rule_destroy__(rule);
4144 if (fm->hard_timeout || fm->idle_timeout) {
4145 list_insert(&ofproto->expirable, &rule->expirable);
4147 cookies_insert(ofproto, rule);
4148 eviction_group_add_rule(rule);
4149 if (actions->provider_meter_id != UINT32_MAX) {
4150 meter_insert_rule(rule);
4153 fat_rwlock_wrlock(&table->cls.rwlock);
4154 classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
4155 fat_rwlock_unlock(&table->cls.rwlock);
4157 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4158 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
4159 ofproto->ofproto_class->rule_insert(rule);
4160 ofopgroup_submit(group);
4165 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4167 /* Modifies the rules listed in 'rules', changing their actions to match those
4170 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4173 * Returns 0 on success, otherwise an OpenFlow error code. */
4175 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4176 struct ofputil_flow_mod *fm, const struct ofp_header *request,
4177 const struct rule_collection *rules)
4178 OVS_REQUIRES(ofproto_mutex)
4180 enum ofoperation_type type;
4181 struct ofopgroup *group;
4185 if (ofproto->ofproto_class->rule_premodify_actions) {
4186 for (i = 0; i < rules->n; i++) {
4187 struct rule *rule = rules->rules[i];
4189 error = ofproto->ofproto_class->rule_premodify_actions(
4190 rule, fm->ofpacts, fm->ofpacts_len);
4197 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
4198 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
4199 for (i = 0; i < rules->n; i++) {
4200 struct rule *rule = rules->rules[i];
4201 const struct rule_actions *actions;
4202 struct ofoperation *op;
4203 bool actions_changed;
4204 bool reset_counters;
4206 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
4209 actions = rule_get_actions(rule);
4210 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
4212 actions->ofpacts_len);
4214 op = ofoperation_create(group, rule, type, 0);
4216 if (fm->modify_cookie && fm->new_cookie != OVS_BE64_MAX) {
4217 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
4219 if (type == OFOPERATION_REPLACE) {
4220 ovs_mutex_lock(&rule->mutex);
4221 rule->idle_timeout = fm->idle_timeout;
4222 rule->hard_timeout = fm->hard_timeout;
4223 ovs_mutex_unlock(&rule->mutex);
4225 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4226 if (fm->idle_timeout || fm->hard_timeout) {
4227 if (!rule->eviction_group) {
4228 eviction_group_add_rule(rule);
4231 eviction_group_remove_rule(rule);
4235 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
4236 if (actions_changed || reset_counters) {
4237 const struct rule_actions *new_actions;
4239 op->actions = rule_get_actions(rule);
4240 new_actions = rule_actions_create(ofproto,
4241 fm->ofpacts, fm->ofpacts_len);
4243 ovsrcu_set(&rule->actions, new_actions);
4245 ofproto->ofproto_class->rule_modify_actions(rule, reset_counters);
4247 ofoperation_complete(op, 0);
4250 ofopgroup_submit(group);
4256 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
4257 struct ofputil_flow_mod *fm, const struct ofp_header *request)
4258 OVS_REQUIRES(ofproto_mutex)
4260 if (fm->cookie_mask != htonll(0) || fm->new_cookie == OVS_BE64_MAX) {
4263 return add_flow(ofproto, ofconn, fm, request);
4266 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4269 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4272 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4273 struct ofputil_flow_mod *fm,
4274 const struct ofp_header *request)
4275 OVS_REQUIRES(ofproto_mutex)
4277 struct rule_criteria criteria;
4278 struct rule_collection rules;
4281 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4282 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4283 rule_criteria_require_rw(&criteria,
4284 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
4285 error = collect_rules_loose(ofproto, &criteria, &rules);
4286 rule_criteria_destroy(&criteria);
4289 error = (rules.n > 0
4290 ? modify_flows__(ofproto, ofconn, fm, request, &rules)
4291 : modify_flows_add(ofproto, ofconn, fm, request));
4294 rule_collection_destroy(&rules);
4299 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
4302 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4305 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4306 struct ofputil_flow_mod *fm,
4307 const struct ofp_header *request)
4308 OVS_REQUIRES(ofproto_mutex)
4310 struct rule_criteria criteria;
4311 struct rule_collection rules;
4314 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4315 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
4316 rule_criteria_require_rw(&criteria,
4317 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
4318 error = collect_rules_strict(ofproto, &criteria, &rules);
4319 rule_criteria_destroy(&criteria);
4323 error = modify_flows_add(ofproto, ofconn, fm, request);
4324 } else if (rules.n == 1) {
4325 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
4329 rule_collection_destroy(&rules);
4334 /* OFPFC_DELETE implementation. */
4337 delete_flow__(struct rule *rule, struct ofopgroup *group,
4338 enum ofp_flow_removed_reason reason)
4339 OVS_REQUIRES(ofproto_mutex)
4341 struct ofproto *ofproto = rule->ofproto;
4343 ofproto_rule_send_removed(rule, reason);
4345 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
4346 oftable_remove_rule(rule);
4347 ofproto->ofproto_class->rule_delete(rule);
4350 /* Deletes the rules listed in 'rules'.
4352 * Returns 0 on success, otherwise an OpenFlow error code. */
4354 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4355 const struct ofp_header *request,
4356 const struct rule_collection *rules,
4357 enum ofp_flow_removed_reason reason)
4358 OVS_REQUIRES(ofproto_mutex)
4360 struct ofopgroup *group;
4363 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
4364 for (i = 0; i < rules->n; i++) {
4365 delete_flow__(rules->rules[i], group, reason);
4367 ofopgroup_submit(group);
4372 /* Implements OFPFC_DELETE. */
4374 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4375 const struct ofputil_flow_mod *fm,
4376 const struct ofp_header *request)
4377 OVS_REQUIRES(ofproto_mutex)
4379 struct rule_criteria criteria;
4380 struct rule_collection rules;
4383 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4384 fm->cookie, fm->cookie_mask,
4385 fm->out_port, fm->out_group);
4386 rule_criteria_require_rw(&criteria,
4387 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
4388 error = collect_rules_loose(ofproto, &criteria, &rules);
4389 rule_criteria_destroy(&criteria);
4391 if (!error && rules.n > 0) {
4392 error = delete_flows__(ofproto, ofconn, request, &rules,
4395 rule_collection_destroy(&rules);
4400 /* Implements OFPFC_DELETE_STRICT. */
4402 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4403 const struct ofputil_flow_mod *fm,
4404 const struct ofp_header *request)
4405 OVS_REQUIRES(ofproto_mutex)
4407 struct rule_criteria criteria;
4408 struct rule_collection rules;
4411 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4412 fm->cookie, fm->cookie_mask,
4413 fm->out_port, fm->out_group);
4414 rule_criteria_require_rw(&criteria,
4415 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
4416 error = collect_rules_strict(ofproto, &criteria, &rules);
4417 rule_criteria_destroy(&criteria);
4419 if (!error && rules.n > 0) {
4420 error = delete_flows__(ofproto, ofconn, request, &rules,
4423 rule_collection_destroy(&rules);
4429 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
4430 OVS_REQUIRES(ofproto_mutex)
4432 struct ofputil_flow_removed fr;
4435 if (rule_is_hidden(rule) || !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
4439 minimatch_expand(&rule->cr.match, &fr.match);
4440 fr.priority = rule->cr.priority;
4441 fr.cookie = rule->flow_cookie;
4443 fr.table_id = rule->table_id;
4444 calc_duration(rule->created, time_msec(),
4445 &fr.duration_sec, &fr.duration_nsec);
4446 ovs_mutex_lock(&rule->mutex);
4447 fr.idle_timeout = rule->idle_timeout;
4448 fr.hard_timeout = rule->hard_timeout;
4449 ovs_mutex_unlock(&rule->mutex);
4450 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
4451 &fr.byte_count, &used);
4453 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
4456 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4457 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4460 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
4463 * ofproto implementation ->run() functions should use this function to expire
4464 * OpenFlow flows. */
4466 ofproto_rule_expire(struct rule *rule, uint8_t reason)
4467 OVS_REQUIRES(ofproto_mutex)
4469 struct ofproto *ofproto = rule->ofproto;
4471 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
4472 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
4474 ofproto_rule_delete__(ofproto, rule, reason);
4477 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4478 * means "infinite". */
4480 reduce_timeout(uint16_t max, uint16_t *timeout)
4482 if (max && (!*timeout || *timeout > max)) {
4487 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4488 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4489 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4491 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4493 ofproto_rule_reduce_timeouts(struct rule *rule,
4494 uint16_t idle_timeout, uint16_t hard_timeout)
4495 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
4497 if (!idle_timeout && !hard_timeout) {
4501 ovs_mutex_lock(&ofproto_mutex);
4502 if (list_is_empty(&rule->expirable)) {
4503 list_insert(&rule->ofproto->expirable, &rule->expirable);
4505 ovs_mutex_unlock(&ofproto_mutex);
4507 ovs_mutex_lock(&rule->mutex);
4508 reduce_timeout(idle_timeout, &rule->idle_timeout);
4509 reduce_timeout(hard_timeout, &rule->hard_timeout);
4510 ovs_mutex_unlock(&rule->mutex);
4514 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4515 OVS_EXCLUDED(ofproto_mutex)
4517 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4518 struct ofputil_flow_mod fm;
4519 uint64_t ofpacts_stub[1024 / 8];
4520 struct ofpbuf ofpacts;
4523 error = reject_slave_controller(ofconn);
4528 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4529 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
4531 u16_to_ofp(ofproto->max_ports),
4534 error = ofproto_check_ofpacts(ofproto, fm.ofpacts, fm.ofpacts_len);
4537 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
4540 goto exit_free_ofpacts;
4543 ofconn_report_flow_mod(ofconn, fm.command);
4546 ofpbuf_uninit(&ofpacts);
4552 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
4553 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
4554 OVS_EXCLUDED(ofproto_mutex)
4558 ovs_mutex_lock(&ofproto_mutex);
4559 if (ofproto->n_pending < 50) {
4560 switch (fm->command) {
4562 error = add_flow(ofproto, ofconn, fm, oh);
4566 error = modify_flows_loose(ofproto, ofconn, fm, oh);
4569 case OFPFC_MODIFY_STRICT:
4570 error = modify_flow_strict(ofproto, ofconn, fm, oh);
4574 error = delete_flows_loose(ofproto, ofconn, fm, oh);
4577 case OFPFC_DELETE_STRICT:
4578 error = delete_flow_strict(ofproto, ofconn, fm, oh);
4582 if (fm->command > 0xff) {
4583 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4584 "flow_mod_table_id extension is not enabled",
4587 error = OFPERR_OFPFMFC_BAD_COMMAND;
4591 ovs_assert(!list_is_empty(&ofproto->pending));
4592 error = OFPROTO_POSTPONE;
4594 ovs_mutex_unlock(&ofproto_mutex);
4596 run_rule_executes(ofproto);
4601 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4603 struct ofputil_role_request request;
4604 struct ofputil_role_request reply;
4608 error = ofputil_decode_role_message(oh, &request);
4613 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4614 if (ofconn_get_role(ofconn) != request.role
4615 && ofconn_has_pending_opgroups(ofconn)) {
4616 return OFPROTO_POSTPONE;
4619 if (request.have_generation_id
4620 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4621 return OFPERR_OFPRRFC_STALE;
4624 ofconn_set_role(ofconn, request.role);
4627 reply.role = ofconn_get_role(ofconn);
4628 reply.have_generation_id = ofconn_get_master_election_id(
4629 ofconn, &reply.generation_id);
4630 buf = ofputil_encode_role_reply(oh, &reply);
4631 ofconn_send_reply(ofconn, buf);
4637 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4638 const struct ofp_header *oh)
4640 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4641 enum ofputil_protocol cur, next;
4643 cur = ofconn_get_protocol(ofconn);
4644 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4645 ofconn_set_protocol(ofconn, next);
4651 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4653 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4654 enum ofputil_protocol cur, next;
4655 enum ofputil_protocol next_base;
4657 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4659 return OFPERR_OFPBRC_EPERM;
4662 cur = ofconn_get_protocol(ofconn);
4663 next = ofputil_protocol_set_base(cur, next_base);
4664 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4665 /* Avoid sending async messages in surprising protocol. */
4666 return OFPROTO_POSTPONE;
4669 ofconn_set_protocol(ofconn, next);
4674 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4675 const struct ofp_header *oh)
4677 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4680 format = ntohl(msg->format);
4681 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4682 return OFPERR_OFPBRC_EPERM;
4685 if (format != ofconn_get_packet_in_format(ofconn)
4686 && ofconn_has_pending_opgroups(ofconn)) {
4687 /* Avoid sending async message in surprsing packet in format. */
4688 return OFPROTO_POSTPONE;
4691 ofconn_set_packet_in_format(ofconn, format);
4696 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4698 const struct nx_async_config *msg = ofpmsg_body(oh);
4699 uint32_t master[OAM_N_TYPES];
4700 uint32_t slave[OAM_N_TYPES];
4702 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4703 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4704 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4706 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4707 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4708 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4710 ofconn_set_async_config(ofconn, master, slave);
4711 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4712 !ofconn_get_miss_send_len(ofconn)) {
4713 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4720 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4723 uint32_t master[OAM_N_TYPES];
4724 uint32_t slave[OAM_N_TYPES];
4725 struct nx_async_config *msg;
4727 ofconn_get_async_config(ofconn, master, slave);
4728 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4729 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4731 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4732 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4733 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4735 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4736 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4737 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4739 ofconn_send_reply(ofconn, buf);
4745 handle_nxt_set_controller_id(struct ofconn *ofconn,
4746 const struct ofp_header *oh)
4748 const struct nx_controller_id *nci = ofpmsg_body(oh);
4750 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4751 return OFPERR_NXBRC_MUST_BE_ZERO;
4754 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4759 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4763 if (ofconn_has_pending_opgroups(ofconn)) {
4764 return OFPROTO_POSTPONE;
4767 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4768 ? OFPRAW_OFPT10_BARRIER_REPLY
4769 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4770 ofconn_send_reply(ofconn, buf);
4775 ofproto_compose_flow_refresh_update(const struct rule *rule,
4776 enum nx_flow_monitor_flags flags,
4778 OVS_REQUIRES(ofproto_mutex)
4780 struct ofoperation *op = rule->pending;
4781 const struct rule_actions *actions;
4782 struct ofputil_flow_update fu;
4785 if (op && op->type == OFOPERATION_ADD) {
4786 /* We'll report the final flow when the operation completes. Reporting
4787 * it now would cause a duplicate report later. */
4791 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4792 ? NXFME_ADDED : NXFME_MODIFIED);
4794 ovs_mutex_lock(&rule->mutex);
4795 fu.idle_timeout = rule->idle_timeout;
4796 fu.hard_timeout = rule->hard_timeout;
4797 ovs_mutex_unlock(&rule->mutex);
4798 fu.table_id = rule->table_id;
4799 fu.cookie = rule->flow_cookie;
4800 minimatch_expand(&rule->cr.match, &match);
4802 fu.priority = rule->cr.priority;
4804 if (!(flags & NXFMF_ACTIONS)) {
4807 actions = rule_get_actions(rule);
4809 /* An operation is in progress. Use the previous version of the flow's
4810 * actions, so that when the operation commits we report the change. */
4812 case OFOPERATION_ADD:
4815 case OFOPERATION_MODIFY:
4816 case OFOPERATION_REPLACE:
4817 actions = op->actions ? op->actions : rule_get_actions(rule);
4820 case OFOPERATION_DELETE:
4821 actions = rule_get_actions(rule);
4828 fu.ofpacts = actions ? actions->ofpacts : NULL;
4829 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
4831 if (list_is_empty(msgs)) {
4832 ofputil_start_flow_update(msgs);
4834 ofputil_append_flow_update(&fu, msgs);
4838 ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4840 OVS_REQUIRES(ofproto_mutex)
4844 for (i = 0; i < rules->n; i++) {
4845 struct rule *rule = rules->rules[i];
4846 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4847 rule->monitor_flags = 0;
4849 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4854 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4855 struct rule *rule, uint64_t seqno,
4856 struct rule_collection *rules)
4857 OVS_REQUIRES(ofproto_mutex)
4859 enum nx_flow_monitor_flags update;
4861 if (rule_is_hidden(rule)) {
4866 ? ofoperation_has_out_port(rule->pending, m->out_port)
4867 : ofproto_rule_has_out_port(rule, m->out_port))) {
4872 if (rule->add_seqno > seqno) {
4873 update = NXFMF_ADD | NXFMF_MODIFY;
4874 } else if (rule->modify_seqno > seqno) {
4875 update = NXFMF_MODIFY;
4880 if (!(m->flags & update)) {
4884 update = NXFMF_INITIAL;
4887 if (!rule->monitor_flags) {
4888 rule_collection_add(rules, rule);
4890 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4894 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4896 struct rule_collection *rules)
4897 OVS_REQUIRES(ofproto_mutex)
4899 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4900 const struct ofoperation *op;
4901 const struct oftable *table;
4902 struct cls_rule target;
4904 cls_rule_init_from_minimatch(&target, &m->match, 0);
4905 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4906 struct cls_cursor cursor;
4909 fat_rwlock_rdlock(&table->cls.rwlock);
4910 cls_cursor_init(&cursor, &table->cls, &target);
4911 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4912 ovs_assert(!rule->pending); /* XXX */
4913 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4915 fat_rwlock_unlock(&table->cls.rwlock);
4918 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4919 struct rule *rule = op->rule;
4921 if (((m->table_id == 0xff
4922 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4923 : m->table_id == rule->table_id))
4924 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4925 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4928 cls_rule_destroy(&target);
4932 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4933 struct rule_collection *rules)
4934 OVS_REQUIRES(ofproto_mutex)
4936 if (m->flags & NXFMF_INITIAL) {
4937 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4942 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4943 uint64_t seqno, struct rule_collection *rules)
4944 OVS_REQUIRES(ofproto_mutex)
4946 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4950 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4951 OVS_EXCLUDED(ofproto_mutex)
4953 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4954 struct ofmonitor **monitors;
4955 size_t n_monitors, allocated_monitors;
4956 struct rule_collection rules;
4957 struct list replies;
4963 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4965 n_monitors = allocated_monitors = 0;
4967 ovs_mutex_lock(&ofproto_mutex);
4969 struct ofputil_flow_monitor_request request;
4970 struct ofmonitor *m;
4973 retval = ofputil_decode_flow_monitor_request(&request, &b);
4974 if (retval == EOF) {
4976 } else if (retval) {
4981 if (request.table_id != 0xff
4982 && request.table_id >= ofproto->n_tables) {
4983 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4987 error = ofmonitor_create(&request, ofconn, &m);
4992 if (n_monitors >= allocated_monitors) {
4993 monitors = x2nrealloc(monitors, &allocated_monitors,
4996 monitors[n_monitors++] = m;
4999 rule_collection_init(&rules);
5000 for (i = 0; i < n_monitors; i++) {
5001 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
5004 ofpmp_init(&replies, oh);
5005 ofmonitor_compose_refresh_updates(&rules, &replies);
5006 ovs_mutex_unlock(&ofproto_mutex);
5008 rule_collection_destroy(&rules);
5010 ofconn_send_replies(ofconn, &replies);
5016 for (i = 0; i < n_monitors; i++) {
5017 ofmonitor_destroy(monitors[i]);
5020 ovs_mutex_unlock(&ofproto_mutex);
5026 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
5027 OVS_EXCLUDED(ofproto_mutex)
5029 struct ofmonitor *m;
5033 id = ofputil_decode_flow_monitor_cancel(oh);
5035 ovs_mutex_lock(&ofproto_mutex);
5036 m = ofmonitor_lookup(ofconn, id);
5038 ofmonitor_destroy(m);
5041 error = OFPERR_NXBRC_FM_BAD_ID;
5043 ovs_mutex_unlock(&ofproto_mutex);
5048 /* Meters implementation.
5050 * Meter table entry, indexed by the OpenFlow meter_id.
5051 * 'created' is used to compute the duration for meter stats.
5052 * 'list rules' is needed so that we can delete the dependent rules when the
5053 * meter table entry is deleted.
5054 * 'provider_meter_id' is for the provider's private use.
5057 long long int created; /* Time created. */
5058 struct list rules; /* List of "struct rule_dpif"s. */
5059 ofproto_meter_id provider_meter_id;
5060 uint16_t flags; /* Meter flags. */
5061 uint16_t n_bands; /* Number of meter bands. */
5062 struct ofputil_meter_band *bands;
5066 * This is used in instruction validation at flow set-up time,
5067 * as flows may not use non-existing meters.
5068 * Return value of UINT32_MAX signifies an invalid meter.
5071 get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id)
5073 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
5074 const struct meter *meter = ofproto->meters[of_meter_id];
5076 return meter->provider_meter_id.uint32;
5082 /* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
5085 meter_insert_rule(struct rule *rule)
5087 const struct rule_actions *a = rule_get_actions(rule);
5088 uint32_t meter_id = ofpacts_get_meter(a->ofpacts, a->ofpacts_len);
5089 struct meter *meter = rule->ofproto->meters[meter_id];
5091 list_insert(&meter->rules, &rule->meter_list_node);
5095 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
5099 meter->flags = config->flags;
5100 meter->n_bands = config->n_bands;
5101 meter->bands = xmemdup(config->bands,
5102 config->n_bands * sizeof *meter->bands);
5105 static struct meter *
5106 meter_create(const struct ofputil_meter_config *config,
5107 ofproto_meter_id provider_meter_id)
5109 struct meter *meter;
5111 meter = xzalloc(sizeof *meter);
5112 meter->provider_meter_id = provider_meter_id;
5113 meter->created = time_msec();
5114 list_init(&meter->rules);
5116 meter_update(meter, config);
5122 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
5123 OVS_REQUIRES(ofproto_mutex)
5126 for (mid = first; mid <= last; ++mid) {
5127 struct meter *meter = ofproto->meters[mid];
5129 ofproto->meters[mid] = NULL;
5130 ofproto->ofproto_class->meter_del(ofproto,
5131 meter->provider_meter_id);
5139 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5141 ofproto_meter_id provider_meter_id = { UINT32_MAX };
5142 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
5146 return OFPERR_OFPMMFC_METER_EXISTS;
5149 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
5152 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
5153 *meterp = meter_create(&mm->meter, provider_meter_id);
5159 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5161 struct meter *meter = ofproto->meters[mm->meter.meter_id];
5163 uint32_t provider_meter_id;
5166 return OFPERR_OFPMMFC_UNKNOWN_METER;
5169 provider_meter_id = meter->provider_meter_id.uint32;
5170 error = ofproto->ofproto_class->meter_set(ofproto,
5171 &meter->provider_meter_id,
5173 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
5175 meter_update(meter, &mm->meter);
5181 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
5182 struct ofputil_meter_mod *mm)
5183 OVS_EXCLUDED(ofproto_mutex)
5185 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5186 uint32_t meter_id = mm->meter.meter_id;
5187 struct rule_collection rules;
5188 enum ofperr error = 0;
5189 uint32_t first, last;
5191 if (meter_id == OFPM13_ALL) {
5193 last = ofproto->meter_features.max_meters;
5195 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
5198 first = last = meter_id;
5201 /* First delete the rules that use this meter. If any of those rules are
5202 * currently being modified, postpone the whole operation until later. */
5203 rule_collection_init(&rules);
5204 ovs_mutex_lock(&ofproto_mutex);
5205 for (meter_id = first; meter_id <= last; ++meter_id) {
5206 struct meter *meter = ofproto->meters[meter_id];
5207 if (meter && !list_is_empty(&meter->rules)) {
5210 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
5211 if (rule->pending) {
5212 error = OFPROTO_POSTPONE;
5215 rule_collection_add(&rules, rule);
5220 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
5223 /* Delete the meters. */
5224 meter_delete(ofproto, first, last);
5227 ovs_mutex_unlock(&ofproto_mutex);
5228 rule_collection_destroy(&rules);
5234 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5236 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5237 struct ofputil_meter_mod mm;
5238 uint64_t bands_stub[256 / 8];
5239 struct ofpbuf bands;
5243 error = reject_slave_controller(ofconn);
5248 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5250 error = ofputil_decode_meter_mod(oh, &mm, &bands);
5252 goto exit_free_bands;
5255 meter_id = mm.meter.meter_id;
5257 if (mm.command != OFPMC13_DELETE) {
5258 /* Fails also when meters are not implemented by the provider. */
5259 if (meter_id == 0 || meter_id > OFPM13_MAX) {
5260 error = OFPERR_OFPMMFC_INVALID_METER;
5261 goto exit_free_bands;
5262 } else if (meter_id > ofproto->meter_features.max_meters) {
5263 error = OFPERR_OFPMMFC_OUT_OF_METERS;
5264 goto exit_free_bands;
5266 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
5267 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
5268 goto exit_free_bands;
5272 switch (mm.command) {
5274 error = handle_add_meter(ofproto, &mm);
5277 case OFPMC13_MODIFY:
5278 error = handle_modify_meter(ofproto, &mm);
5281 case OFPMC13_DELETE:
5282 error = handle_delete_meter(ofconn, oh, &mm);
5286 error = OFPERR_OFPMMFC_BAD_COMMAND;
5291 ofpbuf_uninit(&bands);
5296 handle_meter_features_request(struct ofconn *ofconn,
5297 const struct ofp_header *request)
5299 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5300 struct ofputil_meter_features features;
5303 if (ofproto->ofproto_class->meter_get_features) {
5304 ofproto->ofproto_class->meter_get_features(ofproto, &features);
5306 memset(&features, 0, sizeof features);
5308 b = ofputil_encode_meter_features_reply(&features, request);
5310 ofconn_send_reply(ofconn, b);
5315 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
5318 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5319 struct list replies;
5320 uint64_t bands_stub[256 / 8];
5321 struct ofpbuf bands;
5322 uint32_t meter_id, first, last;
5324 ofputil_decode_meter_request(request, &meter_id);
5326 if (meter_id == OFPM13_ALL) {
5328 last = ofproto->meter_features.max_meters;
5330 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
5331 !ofproto->meters[meter_id]) {
5332 return OFPERR_OFPMMFC_UNKNOWN_METER;
5334 first = last = meter_id;
5337 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5338 ofpmp_init(&replies, request);
5340 for (meter_id = first; meter_id <= last; ++meter_id) {
5341 struct meter *meter = ofproto->meters[meter_id];
5343 continue; /* Skip non-existing meters. */
5345 if (type == OFPTYPE_METER_STATS_REQUEST) {
5346 struct ofputil_meter_stats stats;
5348 stats.meter_id = meter_id;
5350 /* Provider sets the packet and byte counts, we do the rest. */
5351 stats.flow_count = list_size(&meter->rules);
5352 calc_duration(meter->created, time_msec(),
5353 &stats.duration_sec, &stats.duration_nsec);
5354 stats.n_bands = meter->n_bands;
5355 ofpbuf_clear(&bands);
5357 = ofpbuf_put_uninit(&bands,
5358 meter->n_bands * sizeof *stats.bands);
5360 if (!ofproto->ofproto_class->meter_get(ofproto,
5361 meter->provider_meter_id,
5363 ofputil_append_meter_stats(&replies, &stats);
5365 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
5366 struct ofputil_meter_config config;
5368 config.meter_id = meter_id;
5369 config.flags = meter->flags;
5370 config.n_bands = meter->n_bands;
5371 config.bands = meter->bands;
5372 ofputil_append_meter_config(&replies, &config);
5376 ofconn_send_replies(ofconn, &replies);
5377 ofpbuf_uninit(&bands);
5382 ofproto_group_lookup__(const struct ofproto *ofproto, uint32_t group_id,
5383 struct ofgroup **group)
5384 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5386 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5387 hash_int(group_id, 0), &ofproto->groups) {
5388 if ((*group)->group_id == group_id) {
5396 /* If the group exists, this function increments the groups's reference count.
5398 * Make sure to call ofproto_group_unref() after no longer needing to maintain
5399 * a reference to the group. */
5401 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5402 struct ofgroup **group)
5406 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5407 found = ofproto_group_lookup__(ofproto, group_id, group);
5409 ofproto_group_ref(*group);
5411 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5416 ofproto_group_exists__(const struct ofproto *ofproto, uint32_t group_id)
5417 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5419 struct ofgroup *grp;
5421 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
5422 hash_int(group_id, 0), &ofproto->groups) {
5423 if (grp->group_id == group_id) {
5431 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
5432 OVS_EXCLUDED(ofproto->groups_rwlock)
5436 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5437 exists = ofproto_group_exists__(ofproto, group_id);
5438 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5444 group_get_ref_count(struct ofgroup *group)
5445 OVS_EXCLUDED(ofproto_mutex)
5447 struct ofproto *ofproto = CONST_CAST(struct ofproto *, group->ofproto);
5448 struct rule_criteria criteria;
5449 struct rule_collection rules;
5454 match_init_catchall(&match);
5455 rule_criteria_init(&criteria, 0xff, &match, 0, htonll(0), htonll(0),
5456 OFPP_ANY, group->group_id);
5457 ovs_mutex_lock(&ofproto_mutex);
5458 error = collect_rules_loose(ofproto, &criteria, &rules);
5459 ovs_mutex_unlock(&ofproto_mutex);
5460 rule_criteria_destroy(&criteria);
5462 count = !error && rules.n < UINT32_MAX ? rules.n : UINT32_MAX;
5464 rule_collection_destroy(&rules);
5469 append_group_stats(struct ofgroup *group, struct list *replies)
5471 struct ofputil_group_stats ogs;
5472 const struct ofproto *ofproto = group->ofproto;
5473 long long int now = time_msec();
5476 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
5478 /* Provider sets the packet and byte counts, we do the rest. */
5479 ogs.ref_count = group_get_ref_count(group);
5480 ogs.n_buckets = group->n_buckets;
5482 error = (ofproto->ofproto_class->group_get_stats
5483 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
5486 ogs.packet_count = UINT64_MAX;
5487 ogs.byte_count = UINT64_MAX;
5488 memset(ogs.bucket_stats, 0xff,
5489 ogs.n_buckets * sizeof *ogs.bucket_stats);
5492 ogs.group_id = group->group_id;
5493 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
5495 ofputil_append_group_stats(replies, &ogs);
5497 free(ogs.bucket_stats);
5501 handle_group_request(struct ofconn *ofconn,
5502 const struct ofp_header *request, uint32_t group_id,
5503 void (*cb)(struct ofgroup *, struct list *replies))
5505 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5506 struct ofgroup *group;
5507 struct list replies;
5509 ofpmp_init(&replies, request);
5510 if (group_id == OFPG_ALL) {
5511 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5512 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5513 cb(group, &replies);
5515 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5517 if (ofproto_group_lookup(ofproto, group_id, &group)) {
5518 cb(group, &replies);
5519 ofproto_group_unref(group);
5522 ofconn_send_replies(ofconn, &replies);
5526 handle_group_stats_request(struct ofconn *ofconn,
5527 const struct ofp_header *request)
5532 error = ofputil_decode_group_stats_request(request, &group_id);
5537 handle_group_request(ofconn, request, group_id, append_group_stats);
5542 append_group_desc(struct ofgroup *group, struct list *replies)
5544 struct ofputil_group_desc gds;
5546 gds.group_id = group->group_id;
5547 gds.type = group->type;
5548 ofputil_append_group_desc_reply(&gds, &group->buckets, replies);
5552 handle_group_desc_stats_request(struct ofconn *ofconn,
5553 const struct ofp_header *request)
5555 handle_group_request(ofconn, request,
5556 ofputil_decode_group_desc_request(request),
5562 handle_group_features_stats_request(struct ofconn *ofconn,
5563 const struct ofp_header *request)
5565 struct ofproto *p = ofconn_get_ofproto(ofconn);
5568 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5570 ofconn_send_reply(ofconn, msg);
5577 handle_queue_get_config_request(struct ofconn *ofconn,
5578 const struct ofp_header *oh)
5580 struct ofproto *p = ofconn_get_ofproto(ofconn);
5581 struct netdev_queue_dump queue_dump;
5582 struct ofport *ofport;
5583 unsigned int queue_id;
5584 struct ofpbuf *reply;
5585 struct smap details;
5589 error = ofputil_decode_queue_get_config_request(oh, &request);
5594 ofport = ofproto_get_port(p, request);
5596 return OFPERR_OFPQOFC_BAD_PORT;
5599 reply = ofputil_encode_queue_get_config_reply(oh);
5601 smap_init(&details);
5602 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, ofport->netdev) {
5603 struct ofputil_queue_config queue;
5605 /* None of the existing queues have compatible properties, so we
5606 * hard-code omitting min_rate and max_rate. */
5607 queue.queue_id = queue_id;
5608 queue.min_rate = UINT16_MAX;
5609 queue.max_rate = UINT16_MAX;
5610 ofputil_append_queue_get_config_reply(reply, &queue);
5612 smap_destroy(&details);
5614 ofconn_send_reply(ofconn, reply);
5620 init_group(struct ofproto *ofproto, struct ofputil_group_mod *gm,
5621 struct ofgroup **ofgroup)
5624 const long long int now = time_msec();
5626 if (gm->group_id > OFPG_MAX) {
5627 return OFPERR_OFPGMFC_INVALID_GROUP;
5629 if (gm->type > OFPGT11_FF) {
5630 return OFPERR_OFPGMFC_BAD_TYPE;
5633 *ofgroup = ofproto->ofproto_class->group_alloc();
5635 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5636 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5639 (*ofgroup)->ofproto = ofproto;
5640 *CONST_CAST(uint32_t *, &((*ofgroup)->group_id)) = gm->group_id;
5641 *CONST_CAST(enum ofp11_group_type *, &(*ofgroup)->type) = gm->type;
5642 *CONST_CAST(long long int *, &((*ofgroup)->created)) = now;
5643 *CONST_CAST(long long int *, &((*ofgroup)->modified)) = now;
5644 ovs_refcount_init(&(*ofgroup)->ref_count);
5646 list_move(&(*ofgroup)->buckets, &gm->buckets);
5647 *CONST_CAST(uint32_t *, &(*ofgroup)->n_buckets) =
5648 list_size(&(*ofgroup)->buckets);
5650 /* Construct called BEFORE any locks are held. */
5651 error = ofproto->ofproto_class->group_construct(*ofgroup);
5653 ofputil_bucket_list_destroy(&(*ofgroup)->buckets);
5654 ofproto->ofproto_class->group_dealloc(*ofgroup);
5659 /* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
5660 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
5663 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5665 struct ofgroup *ofgroup;
5668 /* Allocate new group and initialize it. */
5669 error = init_group(ofproto, gm, &ofgroup);
5674 /* We wrlock as late as possible to minimize the time we jam any other
5675 * threads: No visible state changes before acquiring the lock. */
5676 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5678 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5679 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5683 if (ofproto_group_exists__(ofproto, gm->group_id)) {
5684 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5689 /* Insert new group. */
5690 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5691 hash_int(ofgroup->group_id, 0));
5692 ofproto->n_groups[ofgroup->type]++;
5694 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5699 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5700 ofproto->ofproto_class->group_destruct(ofgroup);
5701 ofputil_bucket_list_destroy(&ofgroup->buckets);
5702 ofproto->ofproto_class->group_dealloc(ofgroup);
5707 /* Implements OFPGC11_MODIFY. Returns 0 on success or an OpenFlow error code
5710 * Note that the group is re-created and then replaces the old group in
5711 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
5712 * the xlate module hold a pointer to the group. */
5714 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5716 struct ofgroup *ofgroup, *new_ofgroup, *retiring;
5719 error = init_group(ofproto, gm, &new_ofgroup);
5724 retiring = new_ofgroup;
5726 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5727 if (!ofproto_group_lookup__(ofproto, gm->group_id, &ofgroup)) {
5728 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5732 /* Ofproto's group write lock is held now. */
5733 if (ofgroup->type != gm->type
5734 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5735 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5739 /* The group creation time does not change during modification. */
5740 *CONST_CAST(long long int *, &(new_ofgroup->created)) = ofgroup->created;
5741 *CONST_CAST(long long int *, &(new_ofgroup->modified)) = time_msec();
5743 error = ofproto->ofproto_class->group_modify(new_ofgroup);
5749 /* Replace ofgroup in ofproto's groups hash map with new_ofgroup. */
5750 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5751 hmap_insert(&ofproto->groups, &new_ofgroup->hmap_node,
5752 hash_int(new_ofgroup->group_id, 0));
5753 if (ofgroup->type != new_ofgroup->type) {
5754 ofproto->n_groups[ofgroup->type]--;
5755 ofproto->n_groups[new_ofgroup->type]++;
5759 ofproto_group_unref(retiring);
5760 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5765 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5766 OVS_RELEASES(ofproto->groups_rwlock)
5769 struct ofputil_flow_mod fm;
5771 /* Delete all flow entries containing this group in a group action */
5772 match_init_catchall(&match);
5773 flow_mod_init(&fm, &match, 0, NULL, 0, OFPFC_DELETE);
5774 fm.delete_reason = OFPRR_GROUP_DELETE;
5775 fm.out_group = ofgroup->group_id;
5776 handle_flow_mod__(ofproto, NULL, &fm, NULL);
5778 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5779 /* No-one can find this group any more. */
5780 ofproto->n_groups[ofgroup->type]--;
5781 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5782 ofproto_group_unref(ofgroup);
5785 /* Implements OFPGC11_DELETE. */
5787 delete_group(struct ofproto *ofproto, uint32_t group_id)
5789 struct ofgroup *ofgroup;
5791 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5792 if (group_id == OFPG_ALL) {
5794 struct hmap_node *node = hmap_first(&ofproto->groups);
5798 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5799 delete_group__(ofproto, ofgroup);
5800 /* Lock for each node separately, so that we will not jam the
5801 * other threads for too long time. */
5802 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5805 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5806 hash_int(group_id, 0), &ofproto->groups) {
5807 if (ofgroup->group_id == group_id) {
5808 delete_group__(ofproto, ofgroup);
5813 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5817 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5819 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5820 struct ofputil_group_mod gm;
5823 error = reject_slave_controller(ofconn);
5828 error = ofputil_decode_group_mod(oh, &gm);
5833 switch (gm.command) {
5835 return add_group(ofproto, &gm);
5837 case OFPGC11_MODIFY:
5838 return modify_group(ofproto, &gm);
5840 case OFPGC11_DELETE:
5841 delete_group(ofproto, gm.group_id);
5845 if (gm.command > OFPGC11_DELETE) {
5846 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5847 ofproto->name, gm.command);
5849 return OFPERR_OFPGMFC_BAD_COMMAND;
5853 enum ofproto_table_config
5854 ofproto_table_get_config(const struct ofproto *ofproto, uint8_t table_id)
5857 atomic_read(&ofproto->tables[table_id].config, &value);
5858 return (enum ofproto_table_config)value;
5862 table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
5864 /* Only accept currently supported configurations */
5865 if (tm->config & ~OFPTC11_TABLE_MISS_MASK) {
5866 return OFPERR_OFPTMFC_BAD_CONFIG;
5869 if (tm->table_id == OFPTT_ALL) {
5871 for (i = 0; i < ofproto->n_tables; i++) {
5872 atomic_store(&ofproto->tables[i].config,
5873 (unsigned int)tm->config);
5875 } else if (!check_table_id(ofproto, tm->table_id)) {
5876 return OFPERR_OFPTMFC_BAD_TABLE;
5878 atomic_store(&ofproto->tables[tm->table_id].config,
5879 (unsigned int)tm->config);
5886 handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5888 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5889 struct ofputil_table_mod tm;
5892 error = reject_slave_controller(ofconn);
5897 error = ofputil_decode_table_mod(oh, &tm);
5902 return table_mod(ofproto, &tm);
5906 handle_bundle_control(struct ofconn *ofconn, const struct ofp_header *oh)
5909 struct ofputil_bundle_ctrl_msg bctrl;
5911 struct ofputil_bundle_ctrl_msg reply;
5913 error = ofputil_decode_bundle_ctrl(oh, &bctrl);
5918 reply.bundle_id = bctrl.bundle_id;
5920 switch (bctrl.type) {
5921 case OFPBCT_OPEN_REQUEST:
5922 error = ofp_bundle_open(ofconn, bctrl.bundle_id, bctrl.flags);
5923 reply.type = OFPBCT_OPEN_REPLY;
5925 case OFPBCT_CLOSE_REQUEST:
5926 error = ofp_bundle_close(ofconn, bctrl.bundle_id, bctrl.flags);
5927 reply.type = OFPBCT_CLOSE_REPLY;;
5929 case OFPBCT_COMMIT_REQUEST:
5930 error = ofp_bundle_commit(ofconn, bctrl.bundle_id, bctrl.flags);
5931 reply.type = OFPBCT_COMMIT_REPLY;
5933 case OFPBCT_DISCARD_REQUEST:
5934 error = ofp_bundle_discard(ofconn, bctrl.bundle_id);
5935 reply.type = OFPBCT_DISCARD_REPLY;
5938 case OFPBCT_OPEN_REPLY:
5939 case OFPBCT_CLOSE_REPLY:
5940 case OFPBCT_COMMIT_REPLY:
5941 case OFPBCT_DISCARD_REPLY:
5942 return OFPERR_OFPBFC_BAD_TYPE;
5947 buf = ofputil_encode_bundle_ctrl_reply(oh, &reply);
5948 ofconn_send_reply(ofconn, buf);
5955 handle_bundle_add(struct ofconn *ofconn, const struct ofp_header *oh)
5958 struct ofputil_bundle_add_msg badd;
5960 error = ofputil_decode_bundle_add(oh, &badd);
5965 return ofp_bundle_add_message(ofconn, &badd);
5969 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5970 OVS_EXCLUDED(ofproto_mutex)
5972 const struct ofp_header *oh = ofpbuf_data(msg);
5976 error = ofptype_decode(&type, oh);
5980 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
5981 && ofpmp_more(oh)) {
5982 /* We have no buffer implementation for multipart requests.
5983 * Report overflow for requests which consists of multiple
5985 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
5989 /* OpenFlow requests. */
5990 case OFPTYPE_ECHO_REQUEST:
5991 return handle_echo_request(ofconn, oh);
5993 case OFPTYPE_FEATURES_REQUEST:
5994 return handle_features_request(ofconn, oh);
5996 case OFPTYPE_GET_CONFIG_REQUEST:
5997 return handle_get_config_request(ofconn, oh);
5999 case OFPTYPE_SET_CONFIG:
6000 return handle_set_config(ofconn, oh);
6002 case OFPTYPE_PACKET_OUT:
6003 return handle_packet_out(ofconn, oh);
6005 case OFPTYPE_PORT_MOD:
6006 return handle_port_mod(ofconn, oh);
6008 case OFPTYPE_FLOW_MOD:
6009 return handle_flow_mod(ofconn, oh);
6011 case OFPTYPE_GROUP_MOD:
6012 return handle_group_mod(ofconn, oh);
6014 case OFPTYPE_TABLE_MOD:
6015 return handle_table_mod(ofconn, oh);
6017 case OFPTYPE_METER_MOD:
6018 return handle_meter_mod(ofconn, oh);
6020 case OFPTYPE_BARRIER_REQUEST:
6021 return handle_barrier_request(ofconn, oh);
6023 case OFPTYPE_ROLE_REQUEST:
6024 return handle_role_request(ofconn, oh);
6026 /* OpenFlow replies. */
6027 case OFPTYPE_ECHO_REPLY:
6030 /* Nicira extension requests. */
6031 case OFPTYPE_FLOW_MOD_TABLE_ID:
6032 return handle_nxt_flow_mod_table_id(ofconn, oh);
6034 case OFPTYPE_SET_FLOW_FORMAT:
6035 return handle_nxt_set_flow_format(ofconn, oh);
6037 case OFPTYPE_SET_PACKET_IN_FORMAT:
6038 return handle_nxt_set_packet_in_format(ofconn, oh);
6040 case OFPTYPE_SET_CONTROLLER_ID:
6041 return handle_nxt_set_controller_id(ofconn, oh);
6043 case OFPTYPE_FLOW_AGE:
6044 /* Nothing to do. */
6047 case OFPTYPE_FLOW_MONITOR_CANCEL:
6048 return handle_flow_monitor_cancel(ofconn, oh);
6050 case OFPTYPE_SET_ASYNC_CONFIG:
6051 return handle_nxt_set_async_config(ofconn, oh);
6053 case OFPTYPE_GET_ASYNC_REQUEST:
6054 return handle_nxt_get_async_request(ofconn, oh);
6056 /* Statistics requests. */
6057 case OFPTYPE_DESC_STATS_REQUEST:
6058 return handle_desc_stats_request(ofconn, oh);
6060 case OFPTYPE_FLOW_STATS_REQUEST:
6061 return handle_flow_stats_request(ofconn, oh);
6063 case OFPTYPE_AGGREGATE_STATS_REQUEST:
6064 return handle_aggregate_stats_request(ofconn, oh);
6066 case OFPTYPE_TABLE_STATS_REQUEST:
6067 return handle_table_stats_request(ofconn, oh);
6069 case OFPTYPE_PORT_STATS_REQUEST:
6070 return handle_port_stats_request(ofconn, oh);
6072 case OFPTYPE_QUEUE_STATS_REQUEST:
6073 return handle_queue_stats_request(ofconn, oh);
6075 case OFPTYPE_PORT_DESC_STATS_REQUEST:
6076 return handle_port_desc_stats_request(ofconn, oh);
6078 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
6079 return handle_flow_monitor_request(ofconn, oh);
6081 case OFPTYPE_METER_STATS_REQUEST:
6082 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
6083 return handle_meter_request(ofconn, oh, type);
6085 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
6086 return handle_meter_features_request(ofconn, oh);
6088 case OFPTYPE_GROUP_STATS_REQUEST:
6089 return handle_group_stats_request(ofconn, oh);
6091 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
6092 return handle_group_desc_stats_request(ofconn, oh);
6094 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
6095 return handle_group_features_stats_request(ofconn, oh);
6097 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
6098 return handle_queue_get_config_request(ofconn, oh);
6100 case OFPTYPE_BUNDLE_CONTROL:
6101 return handle_bundle_control(ofconn, oh);
6103 case OFPTYPE_BUNDLE_ADD_MESSAGE:
6104 return handle_bundle_add(ofconn, oh);
6108 case OFPTYPE_FEATURES_REPLY:
6109 case OFPTYPE_GET_CONFIG_REPLY:
6110 case OFPTYPE_PACKET_IN:
6111 case OFPTYPE_FLOW_REMOVED:
6112 case OFPTYPE_PORT_STATUS:
6113 case OFPTYPE_BARRIER_REPLY:
6114 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
6115 case OFPTYPE_DESC_STATS_REPLY:
6116 case OFPTYPE_FLOW_STATS_REPLY:
6117 case OFPTYPE_QUEUE_STATS_REPLY:
6118 case OFPTYPE_PORT_STATS_REPLY:
6119 case OFPTYPE_TABLE_STATS_REPLY:
6120 case OFPTYPE_AGGREGATE_STATS_REPLY:
6121 case OFPTYPE_PORT_DESC_STATS_REPLY:
6122 case OFPTYPE_ROLE_REPLY:
6123 case OFPTYPE_FLOW_MONITOR_PAUSED:
6124 case OFPTYPE_FLOW_MONITOR_RESUMED:
6125 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
6126 case OFPTYPE_GET_ASYNC_REPLY:
6127 case OFPTYPE_GROUP_STATS_REPLY:
6128 case OFPTYPE_GROUP_DESC_STATS_REPLY:
6129 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
6130 case OFPTYPE_METER_STATS_REPLY:
6131 case OFPTYPE_METER_CONFIG_STATS_REPLY:
6132 case OFPTYPE_METER_FEATURES_STATS_REPLY:
6133 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
6134 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
6135 case OFPTYPE_ROLE_STATUS:
6137 if (ofpmsg_is_stat_request(oh)) {
6138 return OFPERR_OFPBRC_BAD_STAT;
6140 return OFPERR_OFPBRC_BAD_TYPE;
6146 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
6147 OVS_EXCLUDED(ofproto_mutex)
6149 int error = handle_openflow__(ofconn, ofp_msg);
6150 if (error && error != OFPROTO_POSTPONE) {
6151 ofconn_send_error(ofconn, ofpbuf_data(ofp_msg), error);
6153 COVERAGE_INC(ofproto_recv_openflow);
6154 return error != OFPROTO_POSTPONE;
6157 /* Asynchronous operations. */
6159 /* Creates and returns a new ofopgroup that is not associated with any
6160 * OpenFlow connection.
6162 * The caller should add operations to the returned group with
6163 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6164 static struct ofopgroup *
6165 ofopgroup_create_unattached(struct ofproto *ofproto)
6166 OVS_REQUIRES(ofproto_mutex)
6168 struct ofopgroup *group = xzalloc(sizeof *group);
6169 group->ofproto = ofproto;
6170 list_init(&group->ofproto_node);
6171 list_init(&group->ops);
6172 list_init(&group->ofconn_node);
6176 /* Creates and returns a new ofopgroup for 'ofproto'.
6178 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
6179 * connection. The 'request' and 'buffer_id' arguments are ignored.
6181 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
6182 * If the ofopgroup eventually fails, then the error reply will include
6183 * 'request'. If the ofopgroup eventually succeeds, then the packet with
6184 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
6186 * The caller should add operations to the returned group with
6187 * ofoperation_create() and then submit it with ofopgroup_submit(). */
6188 static struct ofopgroup *
6189 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
6190 const struct ofp_header *request, uint32_t buffer_id)
6191 OVS_REQUIRES(ofproto_mutex)
6193 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
6195 size_t request_len = ntohs(request->length);
6197 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
6199 ofconn_add_opgroup(ofconn, &group->ofconn_node);
6200 group->ofconn = ofconn;
6201 group->request = xmemdup(request, MIN(request_len, 64));
6202 group->buffer_id = buffer_id;
6207 /* Submits 'group' for processing.
6209 * If 'group' contains no operations (e.g. none were ever added, or all of the
6210 * ones that were added completed synchronously), then it is destroyed
6211 * immediately. Otherwise it is added to the ofproto's list of pending
6214 ofopgroup_submit(struct ofopgroup *group)
6215 OVS_REQUIRES(ofproto_mutex)
6217 if (!group->n_running) {
6218 ofopgroup_complete(group);
6220 list_push_back(&group->ofproto->pending, &group->ofproto_node);
6221 group->ofproto->n_pending++;
6226 ofopgroup_complete(struct ofopgroup *group)
6227 OVS_REQUIRES(ofproto_mutex)
6229 struct ofproto *ofproto = group->ofproto;
6231 struct ofconn *abbrev_ofconn;
6232 ovs_be32 abbrev_xid;
6234 struct ofoperation *op, *next_op;
6237 ovs_assert(!group->n_running);
6240 LIST_FOR_EACH (op, group_node, &group->ops) {
6247 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
6248 LIST_FOR_EACH (op, group_node, &group->ops) {
6249 if (op->type != OFOPERATION_DELETE) {
6250 struct ofpbuf *packet;
6253 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
6256 struct rule_execute *re;
6260 ofproto_rule_ref(op->rule);
6262 re = xmalloc(sizeof *re);
6263 re->rule = op->rule;
6264 re->in_port = in_port;
6265 re->packet = packet;
6267 if (!guarded_list_push_back(&ofproto->rule_executes,
6268 &re->list_node, 1024)) {
6269 ofproto_rule_unref(op->rule);
6270 ofpbuf_delete(re->packet);
6279 if (!error && !list_is_empty(&group->ofconn_node)) {
6280 abbrev_ofconn = group->ofconn;
6281 abbrev_xid = group->request->xid;
6283 abbrev_ofconn = NULL;
6284 abbrev_xid = htonl(0);
6286 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
6287 struct rule *rule = op->rule;
6289 /* We generally want to report the change to active OpenFlow flow
6290 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
6292 - The operation failed.
6294 - The affected rule is not visible to controllers.
6296 - The operation's only effect was to update rule->modified. */
6298 || rule_is_hidden(rule)
6299 || (op->type == OFOPERATION_MODIFY
6301 && rule->flow_cookie == op->flow_cookie))) {
6302 enum nx_flow_update_event event_type;
6305 case OFOPERATION_ADD:
6306 case OFOPERATION_REPLACE:
6307 event_type = NXFME_ADDED;
6310 case OFOPERATION_DELETE:
6311 event_type = NXFME_DELETED;
6314 case OFOPERATION_MODIFY:
6315 event_type = NXFME_MODIFIED;
6322 ofmonitor_report(ofproto->connmgr, rule, event_type,
6323 op->reason, abbrev_ofconn, abbrev_xid);
6326 rule->pending = NULL;
6328 ovs_assert(!op->error || op->type == OFOPERATION_ADD);
6330 case OFOPERATION_ADD:
6334 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
6335 if (vid_mask == VLAN_VID_MASK) {
6336 if (ofproto->vlan_bitmap) {
6337 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
6338 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
6339 bitmap_set1(ofproto->vlan_bitmap, vid);
6340 ofproto->vlans_changed = true;
6343 ofproto->vlans_changed = true;
6347 oftable_remove_rule(rule);
6348 ofproto_rule_unref(rule);
6352 case OFOPERATION_DELETE:
6353 ofproto_rule_unref(rule);
6357 case OFOPERATION_MODIFY:
6358 case OFOPERATION_REPLACE: {
6359 long long now = time_msec();
6361 ovs_mutex_lock(&rule->mutex);
6362 rule->modified = now;
6363 if (op->type == OFOPERATION_REPLACE) {
6364 rule->created = now;
6366 ovs_mutex_unlock(&rule->mutex);
6374 ofoperation_destroy(op);
6377 ofmonitor_flush(ofproto->connmgr);
6379 if (!list_is_empty(&group->ofproto_node)) {
6380 ovs_assert(ofproto->n_pending > 0);
6381 ofproto->n_pending--;
6382 list_remove(&group->ofproto_node);
6384 if (!list_is_empty(&group->ofconn_node)) {
6385 list_remove(&group->ofconn_node);
6387 ofconn_send_error(group->ofconn, group->request, error);
6389 connmgr_retry(ofproto->connmgr);
6391 free(group->request);
6395 /* Initiates a new operation on 'rule', of the specified 'type', within
6396 * 'group'. Prior to calling, 'rule' must not have any pending operation.
6398 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
6399 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
6401 * Returns the newly created ofoperation (which is also available as
6402 * rule->pending). */
6403 static struct ofoperation *
6404 ofoperation_create(struct ofopgroup *group, struct rule *rule,
6405 enum ofoperation_type type,
6406 enum ofp_flow_removed_reason reason)
6407 OVS_REQUIRES(ofproto_mutex)
6409 struct ofproto *ofproto = group->ofproto;
6410 struct ofoperation *op;
6412 ovs_assert(!rule->pending);
6414 op = rule->pending = xzalloc(sizeof *op);
6416 list_push_back(&group->ops, &op->group_node);
6419 op->reason = reason;
6420 op->flow_cookie = rule->flow_cookie;
6421 ovs_mutex_lock(&rule->mutex);
6422 op->idle_timeout = rule->idle_timeout;
6423 op->hard_timeout = rule->hard_timeout;
6424 ovs_mutex_unlock(&rule->mutex);
6425 op->flags = rule->flags;
6429 if (type == OFOPERATION_DELETE) {
6430 hmap_insert(&ofproto->deletions, &op->hmap_node,
6431 cls_rule_hash(&rule->cr, rule->table_id));
6438 ofoperation_destroy(struct ofoperation *op)
6439 OVS_REQUIRES(ofproto_mutex)
6441 struct ofopgroup *group = op->group;
6444 op->rule->pending = NULL;
6446 if (op->type == OFOPERATION_DELETE) {
6447 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
6449 list_remove(&op->group_node);
6450 rule_actions_destroy(op->actions);
6454 /* Indicates that 'op' completed with status 'error', which is either 0 to
6455 * indicate success or an OpenFlow error code on failure.
6457 * If 'error' is 0, indicating success, the operation will be committed
6458 * permanently to the flow table.
6460 * Flow modifications and deletions must always succeed. Flow additions may
6461 * fail, indicated by nonzero 'error'. If an "add flow" operation fails, this
6462 * function removes the new rule. The caller must have uninitialized any
6463 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
6464 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and and
6465 * 7 for the new rule, calling its ->rule_dealloc() function.
6467 * Please see the large comment in ofproto/ofproto-provider.h titled
6468 * "Asynchronous Operation Support" for more information. */
6470 ofoperation_complete(struct ofoperation *op, enum ofperr error)
6472 struct ofopgroup *group = op->group;
6474 ovs_assert(group->n_running > 0);
6475 ovs_assert(!error || op->type == OFOPERATION_ADD);
6478 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
6479 /* This function can be called from ->rule_construct(), in which case
6480 * ofproto_mutex is held, or it can be called from ->run(), in which
6481 * case ofproto_mutex is not held. But only in the latter case can we
6482 * arrive here, so we can safely take ofproto_mutex now. */
6483 ovs_mutex_lock(&ofproto_mutex);
6484 ovs_assert(op->rule->pending == op);
6485 ofopgroup_complete(group);
6486 ovs_mutex_unlock(&ofproto_mutex);
6491 pick_datapath_id(const struct ofproto *ofproto)
6493 const struct ofport *port;
6495 port = ofproto_get_port(ofproto, OFPP_LOCAL);
6497 uint8_t ea[ETH_ADDR_LEN];
6500 error = netdev_get_etheraddr(port->netdev, ea);
6502 return eth_addr_to_uint64(ea);
6504 VLOG_WARN("%s: could not get MAC address for %s (%s)",
6505 ofproto->name, netdev_get_name(port->netdev),
6506 ovs_strerror(error));
6508 return ofproto->fallback_dpid;
6512 pick_fallback_dpid(void)
6514 uint8_t ea[ETH_ADDR_LEN];
6515 eth_addr_nicira_random(ea);
6516 return eth_addr_to_uint64(ea);
6519 /* Table overflow policy. */
6521 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
6522 * to NULL if the table is not configured to evict rules or if the table
6523 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
6524 * or with no timeouts are not evictable.) */
6526 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
6527 OVS_REQUIRES(ofproto_mutex)
6529 struct eviction_group *evg;
6532 if (!table->eviction_fields) {
6536 /* In the common case, the outer and inner loops here will each be entered
6539 * - The inner loop normally "return"s in its first iteration. If the
6540 * eviction group has any evictable rules, then it always returns in
6543 * - The outer loop only iterates more than once if the largest eviction
6544 * group has no evictable rules.
6546 * - The outer loop can exit only if table's 'max_flows' is all filled up
6547 * by unevictable rules. */
6548 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
6551 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
6560 /* Searches 'ofproto' for tables that have more flows than their configured
6561 * maximum and that have flow eviction enabled, and evicts as many flows as
6562 * necessary and currently feasible from them.
6564 * This triggers only when an OpenFlow table has N flows in it and then the
6565 * client configures a maximum number of flows less than N. */
6567 ofproto_evict(struct ofproto *ofproto)
6569 struct oftable *table;
6571 ovs_mutex_lock(&ofproto_mutex);
6572 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
6573 evict_rules_from_table(ofproto, table, 0);
6575 ovs_mutex_unlock(&ofproto_mutex);
6578 /* Eviction groups. */
6580 /* Returns the priority to use for an eviction_group that contains 'n_rules'
6581 * rules. The priority contains low-order random bits to ensure that eviction
6582 * groups with the same number of rules are prioritized randomly. */
6584 eviction_group_priority(size_t n_rules)
6586 uint16_t size = MIN(UINT16_MAX, n_rules);
6587 return (size << 16) | random_uint16();
6590 /* Updates 'evg', an eviction_group within 'table', following a change that
6591 * adds or removes rules in 'evg'. */
6593 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
6594 OVS_REQUIRES(ofproto_mutex)
6596 heap_change(&table->eviction_groups_by_size, &evg->size_node,
6597 eviction_group_priority(heap_count(&evg->rules)));
6600 /* Destroys 'evg', an eviction_group within 'table':
6602 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
6603 * rules themselves, just removes them from the eviction group.)
6605 * - Removes 'evg' from 'table'.
6609 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
6610 OVS_REQUIRES(ofproto_mutex)
6612 while (!heap_is_empty(&evg->rules)) {
6615 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
6616 rule->eviction_group = NULL;
6618 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
6619 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
6620 heap_destroy(&evg->rules);
6624 /* Removes 'rule' from its eviction group, if any. */
6626 eviction_group_remove_rule(struct rule *rule)
6627 OVS_REQUIRES(ofproto_mutex)
6629 if (rule->eviction_group) {
6630 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6631 struct eviction_group *evg = rule->eviction_group;
6633 rule->eviction_group = NULL;
6634 heap_remove(&evg->rules, &rule->evg_node);
6635 if (heap_is_empty(&evg->rules)) {
6636 eviction_group_destroy(table, evg);
6638 eviction_group_resized(table, evg);
6643 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6644 * returns the hash value. */
6646 eviction_group_hash_rule(struct rule *rule)
6647 OVS_REQUIRES(ofproto_mutex)
6649 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6650 const struct mf_subfield *sf;
6654 hash = table->eviction_group_id_basis;
6655 miniflow_expand(&rule->cr.match.flow, &flow);
6656 for (sf = table->eviction_fields;
6657 sf < &table->eviction_fields[table->n_eviction_fields];
6660 if (mf_are_prereqs_ok(sf->field, &flow)) {
6661 union mf_value value;
6663 mf_get_value(sf->field, &flow, &value);
6665 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
6667 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
6668 unsigned int start = sf->ofs + sf->n_bits;
6669 bitwise_zero(&value, sf->field->n_bytes, start,
6670 sf->field->n_bytes * 8 - start);
6672 hash = hash_bytes(&value, sf->field->n_bytes, hash);
6674 hash = hash_int(hash, 0);
6681 /* Returns an eviction group within 'table' with the given 'id', creating one
6683 static struct eviction_group *
6684 eviction_group_find(struct oftable *table, uint32_t id)
6685 OVS_REQUIRES(ofproto_mutex)
6687 struct eviction_group *evg;
6689 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6693 evg = xmalloc(sizeof *evg);
6694 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6695 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6696 eviction_group_priority(0));
6697 heap_init(&evg->rules);
6702 /* Returns an eviction priority for 'rule'. The return value should be
6703 * interpreted so that higher priorities make a rule more attractive candidates
6705 * Called only if have a timeout. */
6707 rule_eviction_priority(struct ofproto *ofproto, struct rule *rule)
6708 OVS_REQUIRES(ofproto_mutex)
6710 long long int expiration = LLONG_MAX;
6711 long long int modified;
6712 uint32_t expiration_offset;
6714 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
6715 ovs_mutex_lock(&rule->mutex);
6716 modified = rule->modified;
6717 ovs_mutex_unlock(&rule->mutex);
6719 if (rule->hard_timeout) {
6720 expiration = modified + rule->hard_timeout * 1000;
6722 if (rule->idle_timeout) {
6723 uint64_t packets, bytes;
6725 long long int idle_expiration;
6727 ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used);
6728 idle_expiration = used + rule->idle_timeout * 1000;
6729 expiration = MIN(expiration, idle_expiration);
6732 if (expiration == LLONG_MAX) {
6736 /* Calculate the time of expiration as a number of (approximate) seconds
6737 * after program startup.
6739 * This should work OK for program runs that last UINT32_MAX seconds or
6740 * less. Therefore, please restart OVS at least once every 136 years. */
6741 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6743 /* Invert the expiration offset because we're using a max-heap. */
6744 return UINT32_MAX - expiration_offset;
6747 /* Adds 'rule' to an appropriate eviction group for its oftable's
6748 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6749 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6752 * The caller must ensure that 'rule' is not already in an eviction group. */
6754 eviction_group_add_rule(struct rule *rule)
6755 OVS_REQUIRES(ofproto_mutex)
6757 struct ofproto *ofproto = rule->ofproto;
6758 struct oftable *table = &ofproto->tables[rule->table_id];
6761 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
6762 * so no additional protection is needed. */
6763 has_timeout = rule->hard_timeout || rule->idle_timeout;
6765 if (table->eviction_fields && has_timeout) {
6766 struct eviction_group *evg;
6768 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6770 rule->eviction_group = evg;
6771 heap_insert(&evg->rules, &rule->evg_node,
6772 rule_eviction_priority(ofproto, rule));
6773 eviction_group_resized(table, evg);
6779 /* Initializes 'table'. */
6781 oftable_init(struct oftable *table)
6783 memset(table, 0, sizeof *table);
6784 classifier_init(&table->cls, flow_segment_u32s);
6785 table->max_flows = UINT_MAX;
6786 atomic_init(&table->config, (unsigned int)OFPROTO_TABLE_MISS_DEFAULT);
6788 fat_rwlock_wrlock(&table->cls.rwlock);
6789 classifier_set_prefix_fields(&table->cls, default_prefix_fields,
6790 ARRAY_SIZE(default_prefix_fields));
6791 fat_rwlock_unlock(&table->cls.rwlock);
6793 atomic_init(&table->n_matched, 0);
6794 atomic_init(&table->n_missed, 0);
6797 /* Destroys 'table', including its classifier and eviction groups.
6799 * The caller is responsible for freeing 'table' itself. */
6801 oftable_destroy(struct oftable *table)
6803 fat_rwlock_rdlock(&table->cls.rwlock);
6804 ovs_assert(classifier_is_empty(&table->cls));
6805 fat_rwlock_unlock(&table->cls.rwlock);
6806 oftable_disable_eviction(table);
6807 classifier_destroy(&table->cls);
6811 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6812 * string, then 'table' will use its default name.
6814 * This only affects the name exposed for a table exposed through the OpenFlow
6815 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6817 oftable_set_name(struct oftable *table, const char *name)
6819 if (name && name[0]) {
6820 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6821 if (!table->name || strncmp(name, table->name, len)) {
6823 table->name = xmemdup0(name, len);
6831 /* oftables support a choice of two policies when adding a rule would cause the
6832 * number of flows in the table to exceed the configured maximum number: either
6833 * they can refuse to add the new flow or they can evict some existing flow.
6834 * This function configures the former policy on 'table'. */
6836 oftable_disable_eviction(struct oftable *table)
6837 OVS_REQUIRES(ofproto_mutex)
6839 if (table->eviction_fields) {
6840 struct eviction_group *evg, *next;
6842 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6843 &table->eviction_groups_by_id) {
6844 eviction_group_destroy(table, evg);
6846 hmap_destroy(&table->eviction_groups_by_id);
6847 heap_destroy(&table->eviction_groups_by_size);
6849 free(table->eviction_fields);
6850 table->eviction_fields = NULL;
6851 table->n_eviction_fields = 0;
6855 /* oftables support a choice of two policies when adding a rule would cause the
6856 * number of flows in the table to exceed the configured maximum number: either
6857 * they can refuse to add the new flow or they can evict some existing flow.
6858 * This function configures the latter policy on 'table', with fairness based
6859 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6860 * 'n_fields' as 0 disables fairness.) */
6862 oftable_enable_eviction(struct oftable *table,
6863 const struct mf_subfield *fields, size_t n_fields)
6864 OVS_REQUIRES(ofproto_mutex)
6866 struct cls_cursor cursor;
6869 if (table->eviction_fields
6870 && n_fields == table->n_eviction_fields
6872 || !memcmp(fields, table->eviction_fields,
6873 n_fields * sizeof *fields))) {
6878 oftable_disable_eviction(table);
6880 table->n_eviction_fields = n_fields;
6881 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6883 table->eviction_group_id_basis = random_uint32();
6884 hmap_init(&table->eviction_groups_by_id);
6885 heap_init(&table->eviction_groups_by_size);
6887 fat_rwlock_rdlock(&table->cls.rwlock);
6888 cls_cursor_init(&cursor, &table->cls, NULL);
6889 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6890 eviction_group_add_rule(rule);
6892 fat_rwlock_unlock(&table->cls.rwlock);
6895 /* Removes 'rule' from the oftable that contains it. */
6897 oftable_remove_rule__(struct ofproto *ofproto, struct rule *rule)
6898 OVS_REQUIRES(ofproto_mutex)
6900 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
6902 fat_rwlock_wrlock(&cls->rwlock);
6903 classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
6904 fat_rwlock_unlock(&cls->rwlock);
6906 cookies_remove(ofproto, rule);
6908 eviction_group_remove_rule(rule);
6909 if (!list_is_empty(&rule->expirable)) {
6910 list_remove(&rule->expirable);
6912 if (!list_is_empty(&rule->meter_list_node)) {
6913 list_remove(&rule->meter_list_node);
6914 list_init(&rule->meter_list_node);
6919 oftable_remove_rule(struct rule *rule)
6920 OVS_REQUIRES(ofproto_mutex)
6922 oftable_remove_rule__(rule->ofproto, rule);
6925 /* unixctl commands. */
6928 ofproto_lookup(const char *name)
6930 struct ofproto *ofproto;
6932 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6934 if (!strcmp(ofproto->name, name)) {
6942 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6943 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6945 struct ofproto *ofproto;
6949 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6950 ds_put_format(&results, "%s\n", ofproto->name);
6952 unixctl_command_reply(conn, ds_cstr(&results));
6953 ds_destroy(&results);
6957 ofproto_unixctl_init(void)
6959 static bool registered;
6965 unixctl_command_register("ofproto/list", "", 0, 0,
6966 ofproto_unixctl_list, NULL);
6969 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6971 * This is deprecated. It is only for compatibility with broken device drivers
6972 * in old versions of Linux that do not properly support VLANs when VLAN
6973 * devices are not used. When broken device drivers are no longer in
6974 * widespread use, we will delete these interfaces. */
6976 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6977 * (exactly) by an OpenFlow rule in 'ofproto'. */
6979 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6982 struct cls_rule target;
6983 const struct oftable *oftable;
6985 match_init_catchall(&match);
6986 match_set_vlan_vid_masked(&match, htons(VLAN_CFI), htons(VLAN_CFI));
6987 cls_rule_init(&target, &match, 0);
6989 free(ofproto->vlan_bitmap);
6990 ofproto->vlan_bitmap = bitmap_allocate(4096);
6991 ofproto->vlans_changed = false;
6993 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6994 struct cls_cursor cursor;
6997 fat_rwlock_rdlock(&oftable->cls.rwlock);
6998 cls_cursor_init(&cursor, &oftable->cls, &target);
6999 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
7000 if (minimask_get_vid_mask(&rule->cr.match.mask) == VLAN_VID_MASK) {
7001 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
7003 bitmap_set1(vlan_bitmap, vid);
7004 bitmap_set1(ofproto->vlan_bitmap, vid);
7007 fat_rwlock_unlock(&oftable->cls.rwlock);
7010 cls_rule_destroy(&target);
7013 /* Returns true if new VLANs have come into use by the flow table since the
7014 * last call to ofproto_get_vlan_usage().
7016 * We don't track when old VLANs stop being used. */
7018 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
7020 return ofproto->vlans_changed;
7023 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
7024 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
7025 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
7026 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
7027 * then the VLAN device is un-enslaved. */
7029 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
7030 ofp_port_t realdev_ofp_port, int vid)
7032 struct ofport *ofport;
7035 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
7037 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
7039 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
7040 ofproto->name, vlandev_ofp_port);
7044 if (!ofproto->ofproto_class->set_realdev) {
7045 if (!vlandev_ofp_port) {
7048 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
7052 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
7054 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
7055 ofproto->name, vlandev_ofp_port,
7056 netdev_get_name(ofport->netdev), ovs_strerror(error));