2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 #include "byte-order.h"
27 #include "classifier.h"
30 #include "dynamic-string.h"
33 #include "meta-flow.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
39 #include "ofp-print.h"
42 #include "ofproto-provider.h"
43 #include "openflow/nicira-ext.h"
44 #include "openflow/openflow.h"
48 #include "poll-loop.h"
54 #include "unaligned.h"
58 VLOG_DEFINE_THIS_MODULE(ofproto);
60 COVERAGE_DEFINE(ofproto_error);
61 COVERAGE_DEFINE(ofproto_flush);
62 COVERAGE_DEFINE(ofproto_no_packet_in);
63 COVERAGE_DEFINE(ofproto_packet_out);
64 COVERAGE_DEFINE(ofproto_queue_req);
65 COVERAGE_DEFINE(ofproto_recv_openflow);
66 COVERAGE_DEFINE(ofproto_reinit_ports);
67 COVERAGE_DEFINE(ofproto_uninstallable);
68 COVERAGE_DEFINE(ofproto_update_port);
71 S_OPENFLOW, /* Processing OpenFlow commands. */
72 S_EVICT, /* Evicting flows from over-limit tables. */
73 S_FLUSH, /* Deleting all flow table rules. */
76 enum ofoperation_type {
83 /* A single OpenFlow request can execute any number of operations. The
84 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
85 * ofconn to which an error reply should be sent if necessary.
87 * ofproto initiates some operations internally. These operations are still
88 * assigned to groups but will not have an associated ofconn. */
90 struct ofproto *ofproto; /* Owning ofproto. */
91 struct list ofproto_node; /* In ofproto's "pending" list. */
92 struct list ops; /* List of "struct ofoperation"s. */
93 int n_running; /* Number of ops still pending. */
95 /* Data needed to send OpenFlow reply on failure or to send a buffered
98 * If list_is_empty(ofconn_node) then this ofopgroup never had an
99 * associated ofconn or its ofconn's connection dropped after it initiated
100 * the operation. In the latter case 'ofconn' is a wild pointer that
101 * refers to freed memory, so the 'ofconn' member must be used only if
102 * !list_is_empty(ofconn_node).
104 struct list ofconn_node; /* In ofconn's list of pending opgroups. */
105 struct ofconn *ofconn; /* ofconn for reply (but see note above). */
106 struct ofp_header *request; /* Original request (truncated at 64 bytes). */
107 uint32_t buffer_id; /* Buffer id from original request. */
110 static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *);
111 static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *,
112 const struct ofp_header *,
114 static void ofopgroup_submit(struct ofopgroup *);
115 static void ofopgroup_complete(struct ofopgroup *);
117 /* A single flow table operation. */
119 struct ofopgroup *group; /* Owning group. */
120 struct list group_node; /* In ofopgroup's "ops" list. */
121 struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */
122 struct rule *rule; /* Rule being operated upon. */
123 enum ofoperation_type type; /* Type of operation. */
125 /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions
127 struct ofpact *ofpacts;
131 /* OFOPERATION_DELETE. */
132 enum ofp_flow_removed_reason reason; /* Reason flow was removed. */
134 ovs_be64 flow_cookie; /* Rule's old flow cookie. */
135 uint16_t idle_timeout; /* Rule's old idle timeout. */
136 uint16_t hard_timeout; /* Rule's old hard timeout. */
137 bool send_flow_removed; /* Rule's old 'send_flow_removed'. */
138 enum ofperr error; /* 0 if no error. */
141 static struct ofoperation *ofoperation_create(struct ofopgroup *,
143 enum ofoperation_type,
144 enum ofp_flow_removed_reason);
145 static void ofoperation_destroy(struct ofoperation *);
148 static void oftable_init(struct oftable *);
149 static void oftable_destroy(struct oftable *);
151 static void oftable_set_name(struct oftable *, const char *name);
153 static void oftable_disable_eviction(struct oftable *);
154 static void oftable_enable_eviction(struct oftable *,
155 const struct mf_subfield *fields,
158 static void oftable_remove_rule(struct rule *rule) OVS_RELEASES(rule->rwlock);
159 static void oftable_remove_rule__(struct ofproto *ofproto,
160 struct classifier *cls, struct rule *rule)
161 OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->rwlock);
162 static void oftable_insert_rule(struct rule *);
164 /* A set of rules within a single OpenFlow table (oftable) that have the same
165 * values for the oftable's eviction_fields. A rule to be evicted, when one is
166 * needed, is taken from the eviction group that contains the greatest number
169 * An oftable owns any number of eviction groups, each of which contains any
172 * Membership in an eviction group is imprecise, based on the hash of the
173 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
174 * That is, if two rules have different eviction_fields, but those
175 * eviction_fields hash to the same value, then they will belong to the same
176 * eviction_group anyway.
178 * (When eviction is not enabled on an oftable, we don't track any eviction
179 * groups, to save time and space.) */
180 struct eviction_group {
181 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
182 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
183 struct heap rules; /* Contains "struct rule"s. */
186 static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
187 OVS_TRY_WRLOCK(true, (*rulep)->rwlock);
188 static void ofproto_evict(struct ofproto *);
189 static uint32_t rule_eviction_priority(struct rule *);
190 static void eviction_group_add_rule(struct rule *);
191 static void eviction_group_remove_rule(struct rule *);
194 static void ofport_destroy__(struct ofport *);
195 static void ofport_destroy(struct ofport *);
197 static void update_port(struct ofproto *, const char *devname);
198 static int init_ports(struct ofproto *);
199 static void reinit_ports(struct ofproto *);
202 static void ofproto_rule_destroy__(struct rule *);
203 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
204 static bool rule_is_modifiable(const struct rule *);
207 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
208 struct ofputil_flow_mod *,
209 const struct ofp_header *);
210 static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
211 struct ofputil_flow_mod *,
212 const struct ofp_header *, struct list *);
213 static void delete_flow__(struct rule *rule, struct ofopgroup *,
214 enum ofp_flow_removed_reason)
215 OVS_RELEASES(rule->rwlock);
216 static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
217 static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
218 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
219 struct ofputil_flow_mod *,
220 const struct ofp_header *);
221 static void calc_duration(long long int start, long long int now,
222 uint32_t *sec, uint32_t *nsec);
225 static uint64_t pick_datapath_id(const struct ofproto *);
226 static uint64_t pick_fallback_dpid(void);
227 static void ofproto_destroy__(struct ofproto *);
228 static void update_mtu(struct ofproto *, struct ofport *);
229 static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
232 static void ofproto_unixctl_init(void);
234 /* All registered ofproto classes, in probe order. */
235 static const struct ofproto_class **ofproto_classes;
236 static size_t n_ofproto_classes;
237 static size_t allocated_ofproto_classes;
239 unsigned flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT;
240 unsigned n_handler_threads;
241 enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO;
243 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
244 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
246 /* Initial mappings of port to OpenFlow number mappings. */
247 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
249 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
251 /* The default value of true waits for flow restore. */
252 static bool flow_restore_wait = true;
254 /* Must be called to initialize the ofproto library.
256 * The caller may pass in 'iface_hints', which contains an shash of
257 * "iface_hint" elements indexed by the interface's name. The provider
258 * may use these hints to describe the startup configuration in order to
259 * reinitialize its state. The caller owns the provided data, so a
260 * provider will make copies of anything required. An ofproto provider
261 * will remove any existing state that is not described by the hint, and
262 * may choose to remove it all. */
264 ofproto_init(const struct shash *iface_hints)
266 struct shash_node *node;
269 ofproto_class_register(&ofproto_dpif_class);
271 /* Make a local copy, since we don't own 'iface_hints' elements. */
272 SHASH_FOR_EACH(node, iface_hints) {
273 const struct iface_hint *orig_hint = node->data;
274 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
275 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
277 new_hint->br_name = xstrdup(orig_hint->br_name);
278 new_hint->br_type = xstrdup(br_type);
279 new_hint->ofp_port = orig_hint->ofp_port;
281 shash_add(&init_ofp_ports, node->name, new_hint);
284 for (i = 0; i < n_ofproto_classes; i++) {
285 ofproto_classes[i]->init(&init_ofp_ports);
289 /* 'type' should be a normalized datapath type, as returned by
290 * ofproto_normalize_type(). Returns the corresponding ofproto_class
291 * structure, or a null pointer if there is none registered for 'type'. */
292 static const struct ofproto_class *
293 ofproto_class_find__(const char *type)
297 for (i = 0; i < n_ofproto_classes; i++) {
298 const struct ofproto_class *class = ofproto_classes[i];
303 class->enumerate_types(&types);
304 found = sset_contains(&types, type);
305 sset_destroy(&types);
311 VLOG_WARN("unknown datapath type %s", type);
315 /* Registers a new ofproto class. After successful registration, new ofprotos
316 * of that type can be created using ofproto_create(). */
318 ofproto_class_register(const struct ofproto_class *new_class)
322 for (i = 0; i < n_ofproto_classes; i++) {
323 if (ofproto_classes[i] == new_class) {
328 if (n_ofproto_classes >= allocated_ofproto_classes) {
329 ofproto_classes = x2nrealloc(ofproto_classes,
330 &allocated_ofproto_classes,
331 sizeof *ofproto_classes);
333 ofproto_classes[n_ofproto_classes++] = new_class;
337 /* Unregisters a datapath provider. 'type' must have been previously
338 * registered and not currently be in use by any ofprotos. After
339 * unregistration new datapaths of that type cannot be opened using
340 * ofproto_create(). */
342 ofproto_class_unregister(const struct ofproto_class *class)
346 for (i = 0; i < n_ofproto_classes; i++) {
347 if (ofproto_classes[i] == class) {
348 for (i++; i < n_ofproto_classes; i++) {
349 ofproto_classes[i - 1] = ofproto_classes[i];
355 VLOG_WARN("attempted to unregister an ofproto class that is not "
360 /* Clears 'types' and enumerates all registered ofproto types into it. The
361 * caller must first initialize the sset. */
363 ofproto_enumerate_types(struct sset *types)
367 for (i = 0; i < n_ofproto_classes; i++) {
368 ofproto_classes[i]->enumerate_types(types);
372 /* Returns the fully spelled out name for the given ofproto 'type'.
374 * Normalized type string can be compared with strcmp(). Unnormalized type
375 * string might be the same even if they have different spellings. */
377 ofproto_normalize_type(const char *type)
379 return type && type[0] ? type : "system";
382 /* Clears 'names' and enumerates the names of all known created ofprotos with
383 * the given 'type'. The caller must first initialize the sset. Returns 0 if
384 * successful, otherwise a positive errno value.
386 * Some kinds of datapaths might not be practically enumerable. This is not
387 * considered an error. */
389 ofproto_enumerate_names(const char *type, struct sset *names)
391 const struct ofproto_class *class = ofproto_class_find__(type);
392 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
396 ofproto_create(const char *datapath_name, const char *datapath_type,
397 struct ofproto **ofprotop)
399 const struct ofproto_class *class;
400 struct ofproto *ofproto;
406 ofproto_unixctl_init();
408 datapath_type = ofproto_normalize_type(datapath_type);
409 class = ofproto_class_find__(datapath_type);
411 VLOG_WARN("could not create datapath %s of unknown type %s",
412 datapath_name, datapath_type);
416 ofproto = class->alloc();
418 VLOG_ERR("failed to allocate datapath %s of type %s",
419 datapath_name, datapath_type);
424 memset(ofproto, 0, sizeof *ofproto);
425 ofproto->ofproto_class = class;
426 ofproto->name = xstrdup(datapath_name);
427 ofproto->type = xstrdup(datapath_type);
428 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
429 hash_string(ofproto->name, 0));
430 ofproto->datapath_id = 0;
431 ofproto->forward_bpdu = false;
432 ofproto->fallback_dpid = pick_fallback_dpid();
433 ofproto->mfr_desc = NULL;
434 ofproto->hw_desc = NULL;
435 ofproto->sw_desc = NULL;
436 ofproto->serial_desc = NULL;
437 ofproto->dp_desc = NULL;
438 ofproto->frag_handling = OFPC_FRAG_NORMAL;
439 hmap_init(&ofproto->ports);
440 shash_init(&ofproto->port_by_name);
441 simap_init(&ofproto->ofp_requests);
442 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
443 ofproto->eviction_group_timer = LLONG_MIN;
444 ofproto->tables = NULL;
445 ofproto->n_tables = 0;
446 hindex_init(&ofproto->cookies);
447 list_init(&ofproto->expirable);
448 ovs_mutex_init_recursive(&ofproto->expirable_mutex);
449 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
450 ofproto->state = S_OPENFLOW;
451 list_init(&ofproto->pending);
452 ofproto->n_pending = 0;
453 hmap_init(&ofproto->deletions);
454 ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0;
455 ofproto->first_op = ofproto->last_op = LLONG_MIN;
456 ofproto->next_op_report = LLONG_MAX;
457 ofproto->op_backoff = LLONG_MIN;
458 ofproto->vlan_bitmap = NULL;
459 ofproto->vlans_changed = false;
460 ofproto->min_mtu = INT_MAX;
461 ovs_rwlock_init(&ofproto->groups_rwlock);
462 hmap_init(&ofproto->groups);
464 error = ofproto->ofproto_class->construct(ofproto);
466 VLOG_ERR("failed to open datapath %s: %s",
467 datapath_name, ovs_strerror(error));
468 ofproto_destroy__(ofproto);
472 /* The "max_ports" member should have been set by ->construct(ofproto).
473 * Port 0 is not a valid OpenFlow port, so mark that as unavailable. */
474 ofproto->ofp_port_ids = bitmap_allocate(ofproto->max_ports);
475 bitmap_set1(ofproto->ofp_port_ids, 0);
477 /* Check that hidden tables, if any, are at the end. */
478 ovs_assert(ofproto->n_tables);
479 for (i = 0; i + 1 < ofproto->n_tables; i++) {
480 enum oftable_flags flags = ofproto->tables[i].flags;
481 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
483 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
486 ofproto->datapath_id = pick_datapath_id(ofproto);
489 /* Initialize meters table. */
490 if (ofproto->ofproto_class->meter_get_features) {
491 ofproto->ofproto_class->meter_get_features(ofproto,
492 &ofproto->meter_features);
494 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
496 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
497 * sizeof(struct meter *));
503 /* Must be called (only) by an ofproto implementation in its constructor
504 * function. See the large comment on 'construct' in struct ofproto_class for
507 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
509 struct oftable *table;
511 ovs_assert(!ofproto->n_tables);
512 ovs_assert(n_tables >= 1 && n_tables <= 255);
514 ofproto->n_tables = n_tables;
515 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
516 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
521 /* To be optionally called (only) by an ofproto implementation in its
522 * constructor function. See the large comment on 'construct' in struct
523 * ofproto_class for details.
525 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
526 * will then ensure that actions passed into the ofproto implementation will
527 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
528 * function is not called, there will be no such restriction.
530 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
531 * the 'max_ports' restriction. */
533 ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
535 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
536 ofproto->max_ports = max_ports;
540 ofproto_get_datapath_id(const struct ofproto *ofproto)
542 return ofproto->datapath_id;
546 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
548 uint64_t old_dpid = p->datapath_id;
549 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
550 if (p->datapath_id != old_dpid) {
551 /* Force all active connections to reconnect, since there is no way to
552 * notify a controller that the datapath ID has changed. */
553 ofproto_reconnect_controllers(p);
558 ofproto_set_controllers(struct ofproto *p,
559 const struct ofproto_controller *controllers,
560 size_t n_controllers, uint32_t allowed_versions)
562 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
567 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
569 connmgr_set_fail_mode(p->connmgr, fail_mode);
572 /* Drops the connections between 'ofproto' and all of its controllers, forcing
573 * them to reconnect. */
575 ofproto_reconnect_controllers(struct ofproto *ofproto)
577 connmgr_reconnect(ofproto->connmgr);
580 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
581 * in-band control should guarantee access, in the same way that in-band
582 * control guarantees access to OpenFlow controllers. */
584 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
585 const struct sockaddr_in *extras, size_t n)
587 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
590 /* Sets the OpenFlow queue used by flows set up by in-band control on
591 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
592 * flows will use the default queue. */
594 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
596 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
599 /* Sets the number of flows at which eviction from the kernel flow table
602 ofproto_set_flow_eviction_threshold(unsigned threshold)
604 flow_eviction_threshold = MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN,
608 /* Sets the path for handling flow misses. */
610 ofproto_set_flow_miss_model(unsigned model)
612 flow_miss_model = model;
615 /* If forward_bpdu is true, the NORMAL action will forward frames with
616 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
617 * the NORMAL action will drop these frames. */
619 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
621 bool old_val = ofproto->forward_bpdu;
622 ofproto->forward_bpdu = forward_bpdu;
623 if (old_val != ofproto->forward_bpdu) {
624 if (ofproto->ofproto_class->forward_bpdu_changed) {
625 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
630 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
631 * 'idle_time', in seconds, and the maximum number of MAC table entries to
634 ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
637 if (ofproto->ofproto_class->set_mac_table_config) {
638 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
643 /* Sets number of upcall handler threads. The default is
644 * (number of online cores - 1). */
646 ofproto_set_n_handler_threads(unsigned limit)
649 n_handler_threads = limit;
651 n_handler_threads = MAX(1, sysconf(_SC_NPROCESSORS_ONLN) - 1);
656 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
659 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
663 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
665 return connmgr_set_snoops(ofproto->connmgr, snoops);
669 ofproto_set_netflow(struct ofproto *ofproto,
670 const struct netflow_options *nf_options)
672 if (nf_options && sset_is_empty(&nf_options->collectors)) {
676 if (ofproto->ofproto_class->set_netflow) {
677 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
679 return nf_options ? EOPNOTSUPP : 0;
684 ofproto_set_sflow(struct ofproto *ofproto,
685 const struct ofproto_sflow_options *oso)
687 if (oso && sset_is_empty(&oso->targets)) {
691 if (ofproto->ofproto_class->set_sflow) {
692 return ofproto->ofproto_class->set_sflow(ofproto, oso);
694 return oso ? EOPNOTSUPP : 0;
699 ofproto_set_ipfix(struct ofproto *ofproto,
700 const struct ofproto_ipfix_bridge_exporter_options *bo,
701 const struct ofproto_ipfix_flow_exporter_options *fo,
704 if (ofproto->ofproto_class->set_ipfix) {
705 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
707 return (bo || fo) ? EOPNOTSUPP : 0;
712 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
714 flow_restore_wait = flow_restore_wait_db;
718 ofproto_get_flow_restore_wait(void)
720 return flow_restore_wait;
724 /* Spanning Tree Protocol (STP) configuration. */
726 /* Configures STP on 'ofproto' using the settings defined in 's'. If
727 * 's' is NULL, disables STP.
729 * Returns 0 if successful, otherwise a positive errno value. */
731 ofproto_set_stp(struct ofproto *ofproto,
732 const struct ofproto_stp_settings *s)
734 return (ofproto->ofproto_class->set_stp
735 ? ofproto->ofproto_class->set_stp(ofproto, s)
739 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
740 * 'enabled' member of 's' is false, then the other members are not
743 * Returns 0 if successful, otherwise a positive errno value. */
745 ofproto_get_stp_status(struct ofproto *ofproto,
746 struct ofproto_stp_status *s)
748 return (ofproto->ofproto_class->get_stp_status
749 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
753 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
754 * in 's'. The caller is responsible for assigning STP port numbers
755 * (using the 'port_num' member in the range of 1 through 255, inclusive)
756 * and ensuring there are no duplicates. If the 's' is NULL, then STP
757 * is disabled on the port.
759 * Returns 0 if successful, otherwise a positive errno value.*/
761 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
762 const struct ofproto_port_stp_settings *s)
764 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
766 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
767 ofproto->name, ofp_port);
771 return (ofproto->ofproto_class->set_stp_port
772 ? ofproto->ofproto_class->set_stp_port(ofport, s)
776 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
777 * 's'. If the 'enabled' member in 's' is false, then the other members
778 * are not meaningful.
780 * Returns 0 if successful, otherwise a positive errno value.*/
782 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
783 struct ofproto_port_stp_status *s)
785 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
787 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
788 "port %"PRIu16, ofproto->name, ofp_port);
792 return (ofproto->ofproto_class->get_stp_port_status
793 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
797 /* Queue DSCP configuration. */
799 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
800 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
801 * to implement QoS. Instead, it is used to implement features which require
802 * knowledge of what queues exist on a port, and some basic information about
805 * Returns 0 if successful, otherwise a positive errno value. */
807 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
808 const struct ofproto_port_queue *queues,
811 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
814 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
815 ofproto->name, ofp_port);
819 return (ofproto->ofproto_class->set_queues
820 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
824 /* Connectivity Fault Management configuration. */
826 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
828 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
830 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
831 if (ofport && ofproto->ofproto_class->set_cfm) {
832 ofproto->ofproto_class->set_cfm(ofport, NULL);
836 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
837 * basic configuration from the configuration members in 'cfm', and the remote
838 * maintenance point ID from remote_mpid. Ignores the statistics members of
841 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
843 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
844 const struct cfm_settings *s)
846 struct ofport *ofport;
849 ofport = ofproto_get_port(ofproto, ofp_port);
851 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
852 ofproto->name, ofp_port);
856 /* XXX: For configuration simplicity, we only support one remote_mpid
857 * outside of the CFM module. It's not clear if this is the correct long
858 * term solution or not. */
859 error = (ofproto->ofproto_class->set_cfm
860 ? ofproto->ofproto_class->set_cfm(ofport, s)
863 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
864 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
865 ovs_strerror(error));
869 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
870 * 'ofproto' does not have a port 'ofp_port'. */
872 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
873 const struct smap *cfg)
875 struct ofport *ofport;
878 ofport = ofproto_get_port(ofproto, ofp_port);
880 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
881 ofproto->name, ofp_port);
885 error = (ofproto->ofproto_class->set_bfd
886 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
889 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
890 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
891 ovs_strerror(error));
895 /* Populates 'status' with key value pairs indicating the status of the BFD
896 * session on 'ofp_port'. This information is intended to be populated in the
897 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
900 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
903 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
904 return (ofport && ofproto->ofproto_class->get_bfd_status
905 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
909 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
910 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
911 * 0 if LACP partner information is not current (generally indicating a
912 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
914 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
916 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
917 return (ofport && ofproto->ofproto_class->port_is_lacp_current
918 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
924 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
925 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
926 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
927 * configuration plus, if there is more than one slave, a bonding
930 * If 'aux' is already registered then this function updates its configuration
931 * to 's'. Otherwise, this function registers a new bundle.
933 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
936 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
937 const struct ofproto_bundle_settings *s)
939 return (ofproto->ofproto_class->bundle_set
940 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
944 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
945 * If no such bundle has been registered, this has no effect. */
947 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
949 return ofproto_bundle_register(ofproto, aux, NULL);
953 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
954 * If 'aux' is already registered then this function updates its configuration
955 * to 's'. Otherwise, this function registers a new mirror. */
957 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
958 const struct ofproto_mirror_settings *s)
960 return (ofproto->ofproto_class->mirror_set
961 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
965 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
966 * If no mirror has been registered, this has no effect. */
968 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
970 return ofproto_mirror_register(ofproto, aux, NULL);
973 /* Retrieves statistics from mirror associated with client data pointer
974 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
975 * 'bytes', respectively. If a particular counters is not supported,
976 * the appropriate argument is set to UINT64_MAX. */
978 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
979 uint64_t *packets, uint64_t *bytes)
981 if (!ofproto->ofproto_class->mirror_get_stats) {
982 *packets = *bytes = UINT64_MAX;
986 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
990 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
991 * which all packets are flooded, instead of using MAC learning. If
992 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
994 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
997 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
999 return (ofproto->ofproto_class->set_flood_vlans
1000 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1004 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1005 * output for a mirror. */
1007 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1009 return (ofproto->ofproto_class->is_mirror_output_bundle
1010 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1014 /* Configuration of OpenFlow tables. */
1016 /* Returns the number of OpenFlow tables in 'ofproto'. */
1018 ofproto_get_n_tables(const struct ofproto *ofproto)
1020 return ofproto->n_tables;
1023 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1024 * settings from 's'. 'table_id' must be in the range 0 through the number of
1025 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1027 * For read-only tables, only the name may be configured. */
1029 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1030 const struct ofproto_table_settings *s)
1032 struct oftable *table;
1034 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1035 table = &ofproto->tables[table_id];
1037 oftable_set_name(table, s->name);
1039 if (table->flags & OFTABLE_READONLY) {
1044 oftable_enable_eviction(table, s->groups, s->n_groups);
1046 oftable_disable_eviction(table);
1049 table->max_flows = s->max_flows;
1050 ovs_rwlock_rdlock(&table->cls.rwlock);
1051 if (classifier_count(&table->cls) > table->max_flows
1052 && table->eviction_fields) {
1053 /* 'table' contains more flows than allowed. We might not be able to
1054 * evict them right away because of the asynchronous nature of flow
1055 * table changes. Schedule eviction for later. */
1056 switch (ofproto->state) {
1058 ofproto->state = S_EVICT;
1062 /* We're already deleting flows, nothing more to do. */
1066 ovs_rwlock_unlock(&table->cls.rwlock);
1070 ofproto_has_snoops(const struct ofproto *ofproto)
1072 return connmgr_has_snoops(ofproto->connmgr);
1076 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1078 connmgr_get_snoops(ofproto->connmgr, snoops);
1081 /* Deletes 'rule' from 'cls' within 'ofproto'.
1083 * The 'cls' argument is redundant (it is &ofproto->tables[rule->table_id].cls)
1084 * but it allows Clang to do better checking. */
1086 ofproto_delete_rule(struct ofproto *ofproto, struct classifier *cls,
1088 OVS_REQ_WRLOCK(cls->rwlock)
1090 struct ofopgroup *group;
1092 ovs_assert(!rule->pending);
1093 ovs_assert(cls == &ofproto->tables[rule->table_id].cls);
1095 group = ofopgroup_create_unattached(ofproto);
1096 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1097 ovs_rwlock_wrlock(&rule->rwlock);
1098 oftable_remove_rule__(ofproto, cls, rule);
1099 ofproto->ofproto_class->rule_delete(rule);
1100 ofopgroup_submit(group);
1104 ofproto_flush__(struct ofproto *ofproto)
1106 struct oftable *table;
1108 if (ofproto->ofproto_class->flush) {
1109 ofproto->ofproto_class->flush(ofproto);
1112 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1113 struct rule *rule, *next_rule;
1114 struct cls_cursor cursor;
1116 if (table->flags & OFTABLE_HIDDEN) {
1120 ovs_rwlock_wrlock(&table->cls.rwlock);
1121 cls_cursor_init(&cursor, &table->cls, NULL);
1122 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1123 if (!rule->pending) {
1124 ofproto_delete_rule(ofproto, &table->cls, rule);
1127 ovs_rwlock_unlock(&table->cls.rwlock);
1131 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1134 ofproto_destroy__(struct ofproto *ofproto)
1136 struct oftable *table;
1138 ovs_assert(list_is_empty(&ofproto->pending));
1139 ovs_assert(!ofproto->n_pending);
1141 if (ofproto->meters) {
1142 meter_delete(ofproto, 1, ofproto->meter_features.max_meters);
1143 free(ofproto->meters);
1146 delete_group(ofproto, OFPG_ALL);
1147 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1148 hmap_destroy(&ofproto->groups);
1150 connmgr_destroy(ofproto->connmgr);
1152 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1153 free(ofproto->name);
1154 free(ofproto->type);
1155 free(ofproto->mfr_desc);
1156 free(ofproto->hw_desc);
1157 free(ofproto->sw_desc);
1158 free(ofproto->serial_desc);
1159 free(ofproto->dp_desc);
1160 hmap_destroy(&ofproto->ports);
1161 shash_destroy(&ofproto->port_by_name);
1162 bitmap_free(ofproto->ofp_port_ids);
1163 simap_destroy(&ofproto->ofp_requests);
1165 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1166 oftable_destroy(table);
1168 free(ofproto->tables);
1170 hmap_destroy(&ofproto->deletions);
1172 free(ofproto->vlan_bitmap);
1174 ovs_mutex_destroy(&ofproto->expirable_mutex);
1175 ofproto->ofproto_class->dealloc(ofproto);
1179 ofproto_destroy(struct ofproto *p)
1181 struct ofport *ofport, *next_ofport;
1188 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1189 ofport_destroy(ofport);
1192 p->ofproto_class->destruct(p);
1193 ofproto_destroy__(p);
1196 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1197 * kernel datapath, for example, this destroys the datapath in the kernel, and
1198 * with the netdev-based datapath, it tears down the data structures that
1199 * represent the datapath.
1201 * The datapath should not be currently open as an ofproto. */
1203 ofproto_delete(const char *name, const char *type)
1205 const struct ofproto_class *class = ofproto_class_find__(type);
1206 return (!class ? EAFNOSUPPORT
1207 : !class->del ? EACCES
1208 : class->del(type, name));
1212 process_port_change(struct ofproto *ofproto, int error, char *devname)
1214 if (error == ENOBUFS) {
1215 reinit_ports(ofproto);
1216 } else if (!error) {
1217 update_port(ofproto, devname);
1223 ofproto_type_run(const char *datapath_type)
1225 const struct ofproto_class *class;
1228 datapath_type = ofproto_normalize_type(datapath_type);
1229 class = ofproto_class_find__(datapath_type);
1231 error = class->type_run ? class->type_run(datapath_type) : 0;
1232 if (error && error != EAGAIN) {
1233 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1234 datapath_type, ovs_strerror(error));
1240 ofproto_type_run_fast(const char *datapath_type)
1242 const struct ofproto_class *class;
1245 datapath_type = ofproto_normalize_type(datapath_type);
1246 class = ofproto_class_find__(datapath_type);
1248 error = class->type_run_fast ? class->type_run_fast(datapath_type) : 0;
1249 if (error && error != EAGAIN) {
1250 VLOG_ERR_RL(&rl, "%s: type_run_fast failed (%s)",
1251 datapath_type, ovs_strerror(error));
1257 ofproto_type_wait(const char *datapath_type)
1259 const struct ofproto_class *class;
1261 datapath_type = ofproto_normalize_type(datapath_type);
1262 class = ofproto_class_find__(datapath_type);
1264 if (class->type_wait) {
1265 class->type_wait(datapath_type);
1270 ofproto_run(struct ofproto *p)
1272 struct sset changed_netdevs;
1273 const char *changed_netdev;
1274 struct ofport *ofport;
1277 error = p->ofproto_class->run(p);
1278 if (error && error != EAGAIN) {
1279 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1282 /* Restore the eviction group heap invariant occasionally. */
1283 if (p->eviction_group_timer < time_msec()) {
1286 p->eviction_group_timer = time_msec() + 1000;
1288 for (i = 0; i < p->n_tables; i++) {
1289 struct oftable *table = &p->tables[i];
1290 struct eviction_group *evg;
1291 struct cls_cursor cursor;
1295 if (!table->eviction_fields) {
1299 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1300 heap_rebuild(&evg->rules);
1303 ovs_rwlock_rdlock(&table->cls.rwlock);
1304 cls_cursor_init(&cursor, &table->cls, &cr);
1305 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1306 if (!rule->eviction_group
1307 && (rule->idle_timeout || rule->hard_timeout)) {
1308 eviction_group_add_rule(rule);
1311 ovs_rwlock_unlock(&table->cls.rwlock);
1315 if (p->ofproto_class->port_poll) {
1318 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1319 process_port_change(p, error, devname);
1323 /* Update OpenFlow port status for any port whose netdev has changed.
1325 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1326 * destroyed, so it's not safe to update ports directly from the
1327 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1328 * need this two-phase approach. */
1329 sset_init(&changed_netdevs);
1330 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1331 unsigned int change_seq = netdev_change_seq(ofport->netdev);
1332 if (ofport->change_seq != change_seq) {
1333 ofport->change_seq = change_seq;
1334 sset_add(&changed_netdevs, netdev_get_name(ofport->netdev));
1337 SSET_FOR_EACH (changed_netdev, &changed_netdevs) {
1338 update_port(p, changed_netdev);
1340 sset_destroy(&changed_netdevs);
1344 connmgr_run(p->connmgr, handle_openflow);
1348 connmgr_run(p->connmgr, NULL);
1350 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1351 p->state = S_OPENFLOW;
1356 connmgr_run(p->connmgr, NULL);
1358 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1359 connmgr_flushed(p->connmgr);
1360 p->state = S_OPENFLOW;
1368 if (time_msec() >= p->next_op_report) {
1369 long long int ago = (time_msec() - p->first_op) / 1000;
1370 long long int interval = (p->last_op - p->first_op) / 1000;
1374 ds_put_format(&s, "%d flow_mods ",
1375 p->n_add + p->n_delete + p->n_modify);
1376 if (interval == ago) {
1377 ds_put_format(&s, "in the last %lld s", ago);
1378 } else if (interval) {
1379 ds_put_format(&s, "in the %lld s starting %lld s ago",
1382 ds_put_format(&s, "%lld s ago", ago);
1385 ds_put_cstr(&s, " (");
1387 ds_put_format(&s, "%d adds, ", p->n_add);
1390 ds_put_format(&s, "%d deletes, ", p->n_delete);
1393 ds_put_format(&s, "%d modifications, ", p->n_modify);
1396 ds_put_char(&s, ')');
1398 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1401 p->n_add = p->n_delete = p->n_modify = 0;
1402 p->next_op_report = LLONG_MAX;
1408 /* Performs periodic activity required by 'ofproto' that needs to be done
1409 * with the least possible latency.
1411 * It makes sense to call this function a couple of times per poll loop, to
1412 * provide a significant performance boost on some benchmarks with the
1413 * ofproto-dpif implementation. */
1415 ofproto_run_fast(struct ofproto *p)
1419 error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0;
1420 if (error && error != EAGAIN) {
1421 VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)",
1422 p->name, ovs_strerror(error));
1428 ofproto_wait(struct ofproto *p)
1430 struct ofport *ofport;
1432 p->ofproto_class->wait(p);
1433 if (p->ofproto_class->port_poll_wait) {
1434 p->ofproto_class->port_poll_wait(p);
1437 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1438 if (ofport->change_seq != netdev_change_seq(ofport->netdev)) {
1439 poll_immediate_wake();
1445 connmgr_wait(p->connmgr, true);
1450 connmgr_wait(p->connmgr, false);
1451 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1452 poll_immediate_wake();
1459 ofproto_is_alive(const struct ofproto *p)
1461 return connmgr_has_controllers(p->connmgr);
1464 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1465 * memory_report(). */
1467 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1469 const struct oftable *table;
1470 unsigned int n_rules;
1472 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1473 simap_increase(usage, "ops",
1474 ofproto->n_pending + hmap_count(&ofproto->deletions));
1477 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1478 ovs_rwlock_rdlock(&table->cls.rwlock);
1479 n_rules += classifier_count(&table->cls);
1480 ovs_rwlock_unlock(&table->cls.rwlock);
1482 simap_increase(usage, "rules", n_rules);
1484 if (ofproto->ofproto_class->get_memory_usage) {
1485 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1488 connmgr_get_memory_usage(ofproto->connmgr, usage);
1492 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1495 connmgr_get_controller_info(ofproto->connmgr, info);
1499 ofproto_free_ofproto_controller_info(struct shash *info)
1501 connmgr_free_controller_info(info);
1504 /* Makes a deep copy of 'old' into 'port'. */
1506 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1508 port->name = xstrdup(old->name);
1509 port->type = xstrdup(old->type);
1510 port->ofp_port = old->ofp_port;
1513 /* Frees memory allocated to members of 'ofproto_port'.
1515 * Do not call this function on an ofproto_port obtained from
1516 * ofproto_port_dump_next(): that function retains ownership of the data in the
1519 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1521 free(ofproto_port->name);
1522 free(ofproto_port->type);
1525 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1527 * This function provides no status indication. An error status for the entire
1528 * dump operation is provided when it is completed by calling
1529 * ofproto_port_dump_done().
1532 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1533 const struct ofproto *ofproto)
1535 dump->ofproto = ofproto;
1536 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1540 /* Attempts to retrieve another port from 'dump', which must have been created
1541 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1542 * 'port' and returns true. On failure, returns false.
1544 * Failure might indicate an actual error or merely that the last port has been
1545 * dumped. An error status for the entire dump operation is provided when it
1546 * is completed by calling ofproto_port_dump_done().
1548 * The ofproto owns the data stored in 'port'. It will remain valid until at
1549 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1550 * ofproto_port_dump_done(). */
1552 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1553 struct ofproto_port *port)
1555 const struct ofproto *ofproto = dump->ofproto;
1561 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1564 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1570 /* Completes port table dump operation 'dump', which must have been created
1571 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1572 * error-free, otherwise a positive errno value describing the problem. */
1574 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1576 const struct ofproto *ofproto = dump->ofproto;
1578 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1581 return dump->error == EOF ? 0 : dump->error;
1584 /* Returns the type to pass to netdev_open() when a datapath of type
1585 * 'datapath_type' has a port of type 'port_type', for a few special
1586 * cases when a netdev type differs from a port type. For example, when
1587 * using the userspace datapath, a port of type "internal" needs to be
1590 * Returns either 'type' itself or a string literal, which must not be
1593 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1595 const struct ofproto_class *class;
1597 datapath_type = ofproto_normalize_type(datapath_type);
1598 class = ofproto_class_find__(datapath_type);
1603 return (class->port_open_type
1604 ? class->port_open_type(datapath_type, port_type)
1608 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1609 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1610 * the port's OpenFlow port number.
1612 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1613 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1614 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1615 * 'ofp_portp' is non-null). */
1617 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1618 ofp_port_t *ofp_portp)
1620 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1623 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1625 const char *netdev_name = netdev_get_name(netdev);
1627 simap_put(&ofproto->ofp_requests, netdev_name,
1628 ofp_to_u16(ofp_port));
1629 update_port(ofproto, netdev_name);
1632 struct ofproto_port ofproto_port;
1634 ofproto_port_query_by_name(ofproto, netdev_get_name(netdev),
1636 *ofp_portp = error ? OFPP_NONE : ofproto_port.ofp_port;
1637 ofproto_port_destroy(&ofproto_port);
1642 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1643 * initializes '*port' appropriately; on failure, returns a positive errno
1646 * The caller owns the data in 'ofproto_port' and must free it with
1647 * ofproto_port_destroy() when it is no longer needed. */
1649 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1650 struct ofproto_port *port)
1654 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1656 memset(port, 0, sizeof *port);
1661 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1662 * Returns 0 if successful, otherwise a positive errno. */
1664 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1666 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1667 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1668 struct simap_node *ofp_request_node;
1671 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1672 if (ofp_request_node) {
1673 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1676 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1677 if (!error && ofport) {
1678 /* 'name' is the netdev's name and update_port() is going to close the
1679 * netdev. Just in case update_port() refers to 'name' after it
1680 * destroys 'ofport', make a copy of it around the update_port()
1682 char *devname = xstrdup(name);
1683 update_port(ofproto, devname);
1689 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1690 * performs the 'n_actions' actions in 'actions'. The new flow will not
1693 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1694 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1695 * controllers; otherwise, it will be hidden.
1697 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1699 * This is a helper function for in-band control and fail-open. */
1701 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1702 unsigned int priority,
1703 const struct ofpact *ofpacts, size_t ofpacts_len)
1705 const struct rule *rule;
1707 ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1708 rule = rule_from_cls_rule(classifier_find_match_exactly(
1709 &ofproto->tables[0].cls, match, priority));
1710 ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1711 if (!rule || !ofpacts_equal(rule->ofpacts, rule->ofpacts_len,
1712 ofpacts, ofpacts_len)) {
1713 struct ofputil_flow_mod fm;
1715 memset(&fm, 0, sizeof fm);
1717 fm.priority = priority;
1718 fm.buffer_id = UINT32_MAX;
1719 fm.ofpacts = xmemdup(ofpacts, ofpacts_len);
1720 fm.ofpacts_len = ofpacts_len;
1721 add_flow(ofproto, NULL, &fm, NULL);
1726 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1727 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1728 * operation cannot be initiated now but may be retried later.
1730 * This is a helper function for in-band control and fail-open. */
1732 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1734 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1737 /* Searches for a rule with matching criteria exactly equal to 'target' in
1738 * ofproto's table 0 and, if it finds one, deletes it.
1740 * This is a helper function for in-band control and fail-open. */
1742 ofproto_delete_flow(struct ofproto *ofproto,
1743 const struct match *target, unsigned int priority)
1745 struct classifier *cls = &ofproto->tables[0].cls;
1748 ovs_rwlock_rdlock(&cls->rwlock);
1749 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1751 ovs_rwlock_unlock(&cls->rwlock);
1753 /* No such rule -> success. */
1755 } else if (rule->pending) {
1756 /* An operation on the rule is already pending -> failure.
1757 * Caller must retry later if it's important. */
1760 /* Initiate deletion -> success. */
1761 ovs_rwlock_wrlock(&cls->rwlock);
1762 ofproto_delete_rule(ofproto, cls, rule);
1763 ovs_rwlock_unlock(&cls->rwlock);
1770 /* Starts the process of deleting all of the flows from all of ofproto's flow
1771 * tables and then reintroducing the flows required by in-band control and
1772 * fail-open. The process will complete in a later call to ofproto_run(). */
1774 ofproto_flush_flows(struct ofproto *ofproto)
1776 COVERAGE_INC(ofproto_flush);
1777 ofproto->state = S_FLUSH;
1781 reinit_ports(struct ofproto *p)
1783 struct ofproto_port_dump dump;
1784 struct sset devnames;
1785 struct ofport *ofport;
1786 struct ofproto_port ofproto_port;
1787 const char *devname;
1789 COVERAGE_INC(ofproto_reinit_ports);
1791 sset_init(&devnames);
1792 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1793 sset_add(&devnames, netdev_get_name(ofport->netdev));
1795 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1796 sset_add(&devnames, ofproto_port.name);
1799 SSET_FOR_EACH (devname, &devnames) {
1800 update_port(p, devname);
1802 sset_destroy(&devnames);
1806 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
1810 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
1811 port_idx = port_idx ? port_idx : UINT16_MAX;
1813 if (port_idx >= ofproto->max_ports
1814 || bitmap_is_set(ofproto->ofp_port_ids, port_idx)) {
1815 uint16_t end_port_no = ofproto->alloc_port_no;
1817 /* Search for a free OpenFlow port number. We try not to
1818 * immediately reuse them to prevent problems due to old
1821 if (++ofproto->alloc_port_no >= ofproto->max_ports) {
1822 ofproto->alloc_port_no = 0;
1824 if (!bitmap_is_set(ofproto->ofp_port_ids,
1825 ofproto->alloc_port_no)) {
1826 port_idx = ofproto->alloc_port_no;
1829 if (ofproto->alloc_port_no == end_port_no) {
1834 bitmap_set1(ofproto->ofp_port_ids, port_idx);
1835 return u16_to_ofp(port_idx);
1839 dealloc_ofp_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
1841 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
1842 bitmap_set0(ofproto->ofp_port_ids, ofp_to_u16(ofp_port));
1846 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
1847 * pointer if the netdev cannot be opened. On success, also fills in
1849 static struct netdev *
1850 ofport_open(struct ofproto *ofproto,
1851 struct ofproto_port *ofproto_port,
1852 struct ofputil_phy_port *pp)
1854 enum netdev_flags flags;
1855 struct netdev *netdev;
1858 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
1860 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
1861 "cannot be opened (%s)",
1863 ofproto_port->name, ofproto_port->ofp_port,
1864 ofproto_port->name, ovs_strerror(error));
1868 if (ofproto_port->ofp_port == OFPP_NONE) {
1869 if (!strcmp(ofproto->name, ofproto_port->name)) {
1870 ofproto_port->ofp_port = OFPP_LOCAL;
1872 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
1873 ofproto_port->name);
1876 pp->port_no = ofproto_port->ofp_port;
1877 netdev_get_etheraddr(netdev, pp->hw_addr);
1878 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
1879 netdev_get_flags(netdev, &flags);
1880 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
1881 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
1882 netdev_get_features(netdev, &pp->curr, &pp->advertised,
1883 &pp->supported, &pp->peer);
1884 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
1885 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
1890 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1891 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
1894 ofport_equal(const struct ofputil_phy_port *a,
1895 const struct ofputil_phy_port *b)
1897 return (eth_addr_equals(a->hw_addr, b->hw_addr)
1898 && a->state == b->state
1899 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
1900 && a->curr == b->curr
1901 && a->advertised == b->advertised
1902 && a->supported == b->supported
1903 && a->peer == b->peer
1904 && a->curr_speed == b->curr_speed
1905 && a->max_speed == b->max_speed);
1908 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1909 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1910 * one with the same name or port number). */
1912 ofport_install(struct ofproto *p,
1913 struct netdev *netdev, const struct ofputil_phy_port *pp)
1915 const char *netdev_name = netdev_get_name(netdev);
1916 struct ofport *ofport;
1919 /* Create ofport. */
1920 ofport = p->ofproto_class->port_alloc();
1925 ofport->ofproto = p;
1926 ofport->netdev = netdev;
1927 ofport->change_seq = netdev_change_seq(netdev);
1929 ofport->ofp_port = pp->port_no;
1930 ofport->created = time_msec();
1932 /* Add port to 'p'. */
1933 hmap_insert(&p->ports, &ofport->hmap_node,
1934 hash_ofp_port(ofport->ofp_port));
1935 shash_add(&p->port_by_name, netdev_name, ofport);
1937 update_mtu(p, ofport);
1939 /* Let the ofproto_class initialize its private data. */
1940 error = p->ofproto_class->port_construct(ofport);
1944 connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD);
1948 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
1949 p->name, netdev_name, ovs_strerror(error));
1951 ofport_destroy__(ofport);
1953 netdev_close(netdev);
1957 /* Removes 'ofport' from 'p' and destroys it. */
1959 ofport_remove(struct ofport *ofport)
1961 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp,
1963 ofport_destroy(ofport);
1966 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1969 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1971 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1973 ofport_remove(port);
1977 /* Updates 'port' with new 'pp' description.
1979 * Does not handle a name or port number change. The caller must implement
1980 * such a change as a delete followed by an add. */
1982 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
1984 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
1985 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
1986 | (pp->config & OFPUTIL_PC_PORT_DOWN));
1987 port->pp.state = pp->state;
1988 port->pp.curr = pp->curr;
1989 port->pp.advertised = pp->advertised;
1990 port->pp.supported = pp->supported;
1991 port->pp.peer = pp->peer;
1992 port->pp.curr_speed = pp->curr_speed;
1993 port->pp.max_speed = pp->max_speed;
1995 connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY);
1998 /* Update OpenFlow 'state' in 'port' and notify controller. */
2000 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2002 if (port->pp.state != state) {
2003 port->pp.state = state;
2004 connmgr_send_port_status(port->ofproto->connmgr, &port->pp,
2010 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2012 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2014 if (port->ofproto->ofproto_class->set_realdev) {
2015 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2017 if (port->ofproto->ofproto_class->set_stp_port) {
2018 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2020 if (port->ofproto->ofproto_class->set_cfm) {
2021 port->ofproto->ofproto_class->set_cfm(port, NULL);
2023 if (port->ofproto->ofproto_class->bundle_remove) {
2024 port->ofproto->ofproto_class->bundle_remove(port);
2030 ofport_destroy__(struct ofport *port)
2032 struct ofproto *ofproto = port->ofproto;
2033 const char *name = netdev_get_name(port->netdev);
2035 hmap_remove(&ofproto->ports, &port->hmap_node);
2036 shash_delete(&ofproto->port_by_name,
2037 shash_find(&ofproto->port_by_name, name));
2039 netdev_close(port->netdev);
2040 ofproto->ofproto_class->port_dealloc(port);
2044 ofport_destroy(struct ofport *port)
2047 dealloc_ofp_port(port->ofproto, port->ofp_port);
2048 port->ofproto->ofproto_class->port_destruct(port);
2049 ofport_destroy__(port);
2054 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2056 struct ofport *port;
2058 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2060 if (port->ofp_port == ofp_port) {
2068 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2070 struct ofproto *ofproto = port->ofproto;
2073 if (ofproto->ofproto_class->port_get_stats) {
2074 error = ofproto->ofproto_class->port_get_stats(port, stats);
2083 update_port(struct ofproto *ofproto, const char *name)
2085 struct ofproto_port ofproto_port;
2086 struct ofputil_phy_port pp;
2087 struct netdev *netdev;
2088 struct ofport *port;
2090 COVERAGE_INC(ofproto_update_port);
2092 /* Fetch 'name''s location and properties from the datapath. */
2093 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2094 ? ofport_open(ofproto, &ofproto_port, &pp)
2098 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2099 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2100 struct netdev *old_netdev = port->netdev;
2102 /* 'name' hasn't changed location. Any properties changed? */
2103 if (!ofport_equal(&port->pp, &pp)) {
2104 ofport_modified(port, &pp);
2107 update_mtu(ofproto, port);
2109 /* Install the newly opened netdev in case it has changed.
2110 * Don't close the old netdev yet in case port_modified has to
2111 * remove a retained reference to it.*/
2112 port->netdev = netdev;
2113 port->change_seq = netdev_change_seq(netdev);
2115 if (port->ofproto->ofproto_class->port_modified) {
2116 port->ofproto->ofproto_class->port_modified(port);
2119 netdev_close(old_netdev);
2121 /* If 'port' is nonnull then its name differs from 'name' and thus
2122 * we should delete it. If we think there's a port named 'name'
2123 * then its port number must be wrong now so delete it too. */
2125 ofport_remove(port);
2127 ofport_remove_with_name(ofproto, name);
2128 ofport_install(ofproto, netdev, &pp);
2131 /* Any port named 'name' is gone now. */
2132 ofport_remove_with_name(ofproto, name);
2134 ofproto_port_destroy(&ofproto_port);
2138 init_ports(struct ofproto *p)
2140 struct ofproto_port_dump dump;
2141 struct ofproto_port ofproto_port;
2142 struct shash_node *node, *next;
2144 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2145 const char *name = ofproto_port.name;
2147 if (shash_find(&p->port_by_name, name)) {
2148 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2151 struct ofputil_phy_port pp;
2152 struct netdev *netdev;
2154 /* Check if an OpenFlow port number had been requested. */
2155 node = shash_find(&init_ofp_ports, name);
2157 const struct iface_hint *iface_hint = node->data;
2158 simap_put(&p->ofp_requests, name,
2159 ofp_to_u16(iface_hint->ofp_port));
2162 netdev = ofport_open(p, &ofproto_port, &pp);
2164 ofport_install(p, netdev, &pp);
2165 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2166 p->alloc_port_no = MAX(p->alloc_port_no,
2167 ofp_to_u16(ofproto_port.ofp_port));
2173 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2174 struct iface_hint *iface_hint = node->data;
2176 if (!strcmp(iface_hint->br_name, p->name)) {
2177 free(iface_hint->br_name);
2178 free(iface_hint->br_type);
2180 shash_delete(&init_ofp_ports, node);
2187 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2188 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2190 find_min_mtu(struct ofproto *p)
2192 struct ofport *ofport;
2195 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2196 struct netdev *netdev = ofport->netdev;
2199 /* Skip any internal ports, since that's what we're trying to
2201 if (!strcmp(netdev_get_type(netdev), "internal")) {
2205 if (netdev_get_mtu(netdev, &dev_mtu)) {
2208 if (!mtu || dev_mtu < mtu) {
2213 return mtu ? mtu: ETH_PAYLOAD_MAX;
2216 /* Update MTU of all datapath devices on 'p' to the minimum of the
2217 * non-datapath ports in event of 'port' added or changed. */
2219 update_mtu(struct ofproto *p, struct ofport *port)
2221 struct ofport *ofport;
2222 struct netdev *netdev = port->netdev;
2223 int dev_mtu, old_min;
2225 if (netdev_get_mtu(netdev, &dev_mtu)) {
2229 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2230 if (dev_mtu > p->min_mtu) {
2231 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2232 dev_mtu = p->min_mtu;
2235 port->mtu = dev_mtu;
2239 /* For non-internal port find new min mtu. */
2240 old_min = p->min_mtu;
2241 port->mtu = dev_mtu;
2242 p->min_mtu = find_min_mtu(p);
2243 if (p->min_mtu == old_min) {
2247 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2248 struct netdev *netdev = ofport->netdev;
2250 if (!strcmp(netdev_get_type(netdev), "internal")) {
2251 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2252 ofport->mtu = p->min_mtu;
2259 ofproto_rule_destroy__(struct rule *rule)
2262 rule->ofproto->ofproto_class->rule_destruct(rule);
2263 cls_rule_destroy(&rule->cr);
2264 free(rule->ofpacts);
2265 ovs_mutex_destroy(&rule->timeout_mutex);
2266 ovs_rwlock_destroy(&rule->rwlock);
2267 rule->ofproto->ofproto_class->rule_dealloc(rule);
2271 /* This function allows an ofproto implementation to destroy any rules that
2272 * remain when its ->destruct() function is called.. This function implements
2273 * steps 4.4 and 4.5 in the section titled "Rule Life Cycle" in
2274 * ofproto-provider.h.
2276 * This function should only be called from an ofproto implementation's
2277 * ->destruct() function. It is not suitable elsewhere. */
2279 ofproto_rule_delete(struct ofproto *ofproto, struct classifier *cls,
2281 OVS_REQ_WRLOCK(cls->rwlock)
2283 ofproto_delete_rule(ofproto, cls, rule);
2286 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2287 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2289 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2291 return (port == OFPP_ANY
2292 || ofpacts_output_to_port(rule->ofpacts, rule->ofpacts_len, port));
2295 /* Returns true if 'rule' has group and equals group_id. */
2297 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2299 return (group_id == OFPG11_ANY
2300 || ofpacts_output_to_group(rule->ofpacts, rule->ofpacts_len, group_id));
2303 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2304 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2306 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2308 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2313 case OFOPERATION_ADD:
2314 case OFOPERATION_DELETE:
2317 case OFOPERATION_MODIFY:
2318 case OFOPERATION_REPLACE:
2319 return ofpacts_output_to_port(op->ofpacts, op->ofpacts_len, out_port);
2325 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
2326 * statistics appropriately.
2328 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
2329 * with statistics for 'packet' either way.
2331 * Takes ownership of 'packet'. */
2333 rule_execute(struct rule *rule, ofp_port_t in_port, struct ofpbuf *packet)
2336 union flow_in_port in_port_;
2338 in_port_.ofp_port = in_port;
2339 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
2340 return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet);
2343 /* Returns true if 'rule' should be hidden from the controller.
2345 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2346 * (e.g. by in-band control) and are intentionally hidden from the
2349 ofproto_rule_is_hidden(const struct rule *rule)
2351 return rule->cr.priority > UINT16_MAX;
2354 static enum oftable_flags
2355 rule_get_flags(const struct rule *rule)
2357 return rule->ofproto->tables[rule->table_id].flags;
2361 rule_is_modifiable(const struct rule *rule)
2363 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2367 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2369 ofconn_send_reply(ofconn, make_echo_reply(oh));
2374 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2376 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2377 struct ofputil_switch_features features;
2378 struct ofport *port;
2384 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2386 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2388 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2389 * if present, are always at the end.) */
2390 n_tables = ofproto->n_tables;
2391 for (i = 0; i < ofproto->n_tables; i++) {
2392 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2398 features.datapath_id = ofproto->datapath_id;
2399 features.n_buffers = pktbuf_capacity();
2400 features.n_tables = n_tables;
2401 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2402 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2404 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2406 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2407 features.auxiliary_id = 0;
2408 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2410 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2411 ofputil_put_switch_features_port(&port->pp, b);
2414 ofconn_send_reply(ofconn, b);
2419 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2421 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2422 struct ofp_switch_config *osc;
2423 enum ofp_config_flags flags;
2427 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2428 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2429 flags = ofproto->frag_handling;
2430 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2431 if (oh->version < OFP13_VERSION
2432 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2433 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2435 osc->flags = htons(flags);
2436 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2437 ofconn_send_reply(ofconn, buf);
2443 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2445 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2446 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2447 uint16_t flags = ntohs(osc->flags);
2449 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2450 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2451 enum ofp_config_flags cur = ofproto->frag_handling;
2452 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2454 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2456 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2457 ofproto->frag_handling = next;
2459 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2461 ofputil_frag_handling_to_string(next));
2465 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2466 ofconn_set_invalid_ttl_to_controller(ofconn,
2467 (oh->version < OFP13_VERSION
2468 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2470 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2475 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2476 * error message code for the caller to propagate upward. Otherwise, returns
2479 * The log message mentions 'msg_type'. */
2481 reject_slave_controller(struct ofconn *ofconn)
2483 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2484 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2485 return OFPERR_OFPBRC_EPERM;
2491 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2492 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2494 * This function relies on the order of 'ofpacts' being correct (as checked by
2495 * ofpacts_verify()). */
2497 find_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2499 const struct ofpact *a;
2501 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2502 enum ovs_instruction_type inst;
2504 inst = ovs_instruction_type_from_ofpact_type(a->type);
2505 if (a->type == OFPACT_METER) {
2506 return ofpact_get_METER(a)->meter_id;
2507 } else if (inst > OVSINST_OFPIT13_METER) {
2515 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate
2516 * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'.
2517 * 'flow' may be temporarily modified, but is restored at return.
2520 ofproto_check_ofpacts(struct ofproto *ofproto,
2521 const struct ofpact ofpacts[], size_t ofpacts_len,
2522 struct flow *flow, uint8_t table_id)
2527 error = ofpacts_check(ofpacts, ofpacts_len, flow,
2528 u16_to_ofp(ofproto->max_ports), table_id);
2533 mid = find_meter(ofpacts, ofpacts_len);
2534 if (mid && ofproto_get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2535 return OFPERR_OFPMMFC_INVALID_METER;
2541 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2543 struct ofproto *p = ofconn_get_ofproto(ofconn);
2544 struct ofputil_packet_out po;
2545 struct ofpbuf *payload;
2546 uint64_t ofpacts_stub[1024 / 8];
2547 struct ofpbuf ofpacts;
2549 union flow_in_port in_port_;
2552 COVERAGE_INC(ofproto_packet_out);
2554 error = reject_slave_controller(ofconn);
2559 /* Decode message. */
2560 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2561 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2563 goto exit_free_ofpacts;
2565 if (ofp_to_u16(po.in_port) >= p->max_ports
2566 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2567 error = OFPERR_OFPBRC_BAD_PORT;
2568 goto exit_free_ofpacts;
2573 if (po.buffer_id != UINT32_MAX) {
2574 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2575 if (error || !payload) {
2576 goto exit_free_ofpacts;
2579 /* Ensure that the L3 header is 32-bit aligned. */
2580 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2583 /* Verify actions against packet, then send packet if successful. */
2584 in_port_.ofp_port = po.in_port;
2585 flow_extract(payload, 0, 0, NULL, &in_port_, &flow);
2586 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len, &flow, 0);
2588 error = p->ofproto_class->packet_out(p, payload, &flow,
2589 po.ofpacts, po.ofpacts_len);
2591 ofpbuf_delete(payload);
2594 ofpbuf_uninit(&ofpacts);
2600 update_port_config(struct ofport *port,
2601 enum ofputil_port_config config,
2602 enum ofputil_port_config mask)
2604 enum ofputil_port_config old_config = port->pp.config;
2605 enum ofputil_port_config toggle;
2607 toggle = (config ^ port->pp.config) & mask;
2608 if (toggle & OFPUTIL_PC_PORT_DOWN) {
2609 if (config & OFPUTIL_PC_PORT_DOWN) {
2610 netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL);
2612 netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL);
2614 toggle &= ~OFPUTIL_PC_PORT_DOWN;
2617 port->pp.config ^= toggle;
2618 if (port->pp.config != old_config) {
2619 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2624 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2626 struct ofproto *p = ofconn_get_ofproto(ofconn);
2627 struct ofputil_port_mod pm;
2628 struct ofport *port;
2631 error = reject_slave_controller(ofconn);
2636 error = ofputil_decode_port_mod(oh, &pm);
2641 port = ofproto_get_port(p, pm.port_no);
2643 return OFPERR_OFPPMFC_BAD_PORT;
2644 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
2645 return OFPERR_OFPPMFC_BAD_HW_ADDR;
2647 update_port_config(port, pm.config, pm.mask);
2649 netdev_set_advertisements(port->netdev, pm.advertise);
2656 handle_desc_stats_request(struct ofconn *ofconn,
2657 const struct ofp_header *request)
2659 static const char *default_mfr_desc = "Nicira, Inc.";
2660 static const char *default_hw_desc = "Open vSwitch";
2661 static const char *default_sw_desc = VERSION;
2662 static const char *default_serial_desc = "None";
2663 static const char *default_dp_desc = "None";
2665 struct ofproto *p = ofconn_get_ofproto(ofconn);
2666 struct ofp_desc_stats *ods;
2669 msg = ofpraw_alloc_stats_reply(request, 0);
2670 ods = ofpbuf_put_zeros(msg, sizeof *ods);
2671 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
2672 sizeof ods->mfr_desc);
2673 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
2674 sizeof ods->hw_desc);
2675 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
2676 sizeof ods->sw_desc);
2677 ovs_strlcpy(ods->serial_num,
2678 p->serial_desc ? p->serial_desc : default_serial_desc,
2679 sizeof ods->serial_num);
2680 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
2681 sizeof ods->dp_desc);
2682 ofconn_send_reply(ofconn, msg);
2688 handle_table_stats_request(struct ofconn *ofconn,
2689 const struct ofp_header *request)
2691 struct ofproto *p = ofconn_get_ofproto(ofconn);
2692 struct ofp12_table_stats *ots;
2697 /* Set up default values.
2699 * ofp12_table_stats is used as a generic structure as
2700 * it is able to hold all the fields for ofp10_table_stats
2701 * and ofp11_table_stats (and of course itself).
2703 ots = xcalloc(p->n_tables, sizeof *ots);
2704 for (i = 0; i < p->n_tables; i++) {
2705 ots[i].table_id = i;
2706 sprintf(ots[i].name, "table%zu", i);
2707 ots[i].match = htonll(OFPXMT12_MASK);
2708 ots[i].wildcards = htonll(OFPXMT12_MASK);
2709 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
2710 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
2711 ots[i].write_setfields = htonll(OFPXMT12_MASK);
2712 ots[i].apply_setfields = htonll(OFPXMT12_MASK);
2713 ots[i].metadata_match = htonll(UINT64_MAX);
2714 ots[i].metadata_write = htonll(UINT64_MAX);
2715 ots[i].instructions = htonl(OFPIT11_ALL);
2716 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
2717 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
2718 ovs_rwlock_rdlock(&p->tables[i].cls.rwlock);
2719 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
2720 ovs_rwlock_unlock(&p->tables[i].cls.rwlock);
2723 p->ofproto_class->get_tables(p, ots);
2725 /* Post-process the tables, dropping hidden tables. */
2726 n_tables = p->n_tables;
2727 for (i = 0; i < p->n_tables; i++) {
2728 const struct oftable *table = &p->tables[i];
2730 if (table->flags & OFTABLE_HIDDEN) {
2736 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
2739 if (table->max_flows < ntohl(ots[i].max_entries)) {
2740 ots[i].max_entries = htonl(table->max_flows);
2744 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
2745 ofconn_send_reply(ofconn, msg);
2753 append_port_stat(struct ofport *port, struct list *replies)
2755 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
2757 calc_duration(port->created, time_msec(),
2758 &ops.duration_sec, &ops.duration_nsec);
2760 /* Intentionally ignore return value, since errors will set
2761 * 'stats' to all-1s, which is correct for OpenFlow, and
2762 * netdev_get_stats() will log errors. */
2763 ofproto_port_get_stats(port, &ops.stats);
2765 ofputil_append_port_stat(replies, &ops);
2769 handle_port_stats_request(struct ofconn *ofconn,
2770 const struct ofp_header *request)
2772 struct ofproto *p = ofconn_get_ofproto(ofconn);
2773 struct ofport *port;
2774 struct list replies;
2778 error = ofputil_decode_port_stats_request(request, &port_no);
2783 ofpmp_init(&replies, request);
2784 if (port_no != OFPP_ANY) {
2785 port = ofproto_get_port(p, port_no);
2787 append_port_stat(port, &replies);
2790 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2791 append_port_stat(port, &replies);
2795 ofconn_send_replies(ofconn, &replies);
2800 handle_port_desc_stats_request(struct ofconn *ofconn,
2801 const struct ofp_header *request)
2803 struct ofproto *p = ofconn_get_ofproto(ofconn);
2804 enum ofp_version version;
2805 struct ofport *port;
2806 struct list replies;
2808 ofpmp_init(&replies, request);
2810 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
2811 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2812 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
2815 ofconn_send_replies(ofconn, &replies);
2820 hash_cookie(ovs_be64 cookie)
2822 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
2823 (OVS_FORCE uint64_t)cookie);
2827 cookies_insert(struct ofproto *ofproto, struct rule *rule)
2829 hindex_insert(&ofproto->cookies, &rule->cookie_node,
2830 hash_cookie(rule->flow_cookie));
2834 cookies_remove(struct ofproto *ofproto, struct rule *rule)
2836 hindex_remove(&ofproto->cookies, &rule->cookie_node);
2840 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
2841 ovs_be64 new_cookie)
2843 if (new_cookie != rule->flow_cookie) {
2844 cookies_remove(ofproto, rule);
2846 ovs_rwlock_wrlock(&rule->rwlock);
2847 rule->flow_cookie = new_cookie;
2848 ovs_rwlock_unlock(&rule->rwlock);
2850 cookies_insert(ofproto, rule);
2855 calc_duration(long long int start, long long int now,
2856 uint32_t *sec, uint32_t *nsec)
2858 long long int msecs = now - start;
2859 *sec = msecs / 1000;
2860 *nsec = (msecs % 1000) * (1000 * 1000);
2863 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
2864 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
2866 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
2868 return (table_id == 0xff || table_id < ofproto->n_tables
2870 : OFPERR_OFPBRC_BAD_TABLE_ID);
2874 static struct oftable *
2875 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
2877 struct oftable *table;
2879 for (table = &ofproto->tables[table_id];
2880 table < &ofproto->tables[ofproto->n_tables];
2882 if (!(table->flags & OFTABLE_HIDDEN)) {
2890 static struct oftable *
2891 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
2893 if (table_id == 0xff) {
2894 return next_visible_table(ofproto, 0);
2895 } else if (table_id < ofproto->n_tables) {
2896 return &ofproto->tables[table_id];
2902 static struct oftable *
2903 next_matching_table(const struct ofproto *ofproto,
2904 const struct oftable *table, uint8_t table_id)
2906 return (table_id == 0xff
2907 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
2911 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
2913 * - If TABLE_ID is 0xff, this iterates over every classifier table in
2914 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
2916 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
2917 * only once, for that table. (This can be used to access tables marked
2920 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
2921 * entered at all. (Perhaps you should have validated TABLE_ID with
2922 * check_table_id().)
2924 * All parameters are evaluated multiple times.
2926 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
2927 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
2929 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
2931 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
2932 * 'table_id' is 0xff) that match 'match' in the "loose" way required for
2933 * OpenFlow OFPFC_MODIFY and OFPFC_DELETE requests and puts them on list
2936 * If 'out_port' is anything other than OFPP_ANY, then only rules that output
2937 * to 'out_port' are included.
2939 * Hidden rules are always omitted.
2941 * Returns 0 on success, otherwise an OpenFlow error code. */
2943 collect_rules_loose(struct ofproto *ofproto, uint8_t table_id,
2944 const struct match *match,
2945 ovs_be64 cookie, ovs_be64 cookie_mask,
2946 ofp_port_t out_port, uint32_t out_group,
2949 struct oftable *table;
2953 error = check_table_id(ofproto, table_id);
2959 cls_rule_init(&cr, match, 0);
2961 if (cookie_mask == htonll(UINT64_MAX)) {
2964 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, hash_cookie(cookie),
2965 &ofproto->cookies) {
2966 if (table_id != rule->table_id && table_id != 0xff) {
2969 if (ofproto_rule_is_hidden(rule)) {
2972 if (cls_rule_is_loose_match(&rule->cr, &cr.match)) {
2973 if (rule->pending) {
2974 error = OFPROTO_POSTPONE;
2977 if (rule->flow_cookie == cookie /* Hash collisions possible. */
2978 && ofproto_rule_has_out_port(rule, out_port)) {
2979 list_push_back(rules, &rule->ofproto_node);
2986 FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) {
2987 struct cls_cursor cursor;
2990 ovs_rwlock_rdlock(&table->cls.rwlock);
2991 cls_cursor_init(&cursor, &table->cls, &cr);
2992 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2993 if (rule->pending) {
2994 ovs_rwlock_unlock(&table->cls.rwlock);
2995 error = OFPROTO_POSTPONE;
2998 if (!ofproto_rule_is_hidden(rule)
2999 && ofproto_rule_has_out_port(rule, out_port)
3000 && ofproto_rule_has_out_group(rule, out_group)
3001 && !((rule->flow_cookie ^ cookie) & cookie_mask)) {
3002 list_push_back(rules, &rule->ofproto_node);
3005 ovs_rwlock_unlock(&table->cls.rwlock);
3009 cls_rule_destroy(&cr);
3013 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
3014 * 'table_id' is 0xff) that match 'match' in the "strict" way required for
3015 * OpenFlow OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests and puts them
3018 * If 'out_port' is anything other than OFPP_ANY, then only rules that output
3019 * to 'out_port' are included.
3021 * Hidden rules are always omitted.
3023 * Returns 0 on success, otherwise an OpenFlow error code. */
3025 collect_rules_strict(struct ofproto *ofproto, uint8_t table_id,
3026 const struct match *match, unsigned int priority,
3027 ovs_be64 cookie, ovs_be64 cookie_mask,
3028 ofp_port_t out_port, uint32_t out_group,
3031 struct oftable *table;
3035 error = check_table_id(ofproto, table_id);
3041 cls_rule_init(&cr, match, priority);
3043 if (cookie_mask == htonll(UINT64_MAX)) {
3046 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, hash_cookie(cookie),
3047 &ofproto->cookies) {
3048 if (table_id != rule->table_id && table_id != 0xff) {
3051 if (ofproto_rule_is_hidden(rule)) {
3054 if (cls_rule_equal(&rule->cr, &cr)) {
3055 if (rule->pending) {
3056 error = OFPROTO_POSTPONE;
3059 if (rule->flow_cookie == cookie /* Hash collisions possible. */
3060 && ofproto_rule_has_out_port(rule, out_port)) {
3061 list_push_back(rules, &rule->ofproto_node);
3068 FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) {
3071 ovs_rwlock_rdlock(&table->cls.rwlock);
3072 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls,
3074 ovs_rwlock_unlock(&table->cls.rwlock);
3076 if (rule->pending) {
3077 error = OFPROTO_POSTPONE;
3080 if (!ofproto_rule_is_hidden(rule)
3081 && ofproto_rule_has_out_port(rule, out_port)
3082 && ofproto_rule_has_out_group(rule, out_group)
3083 && !((rule->flow_cookie ^ cookie) & cookie_mask)) {
3084 list_push_back(rules, &rule->ofproto_node);
3090 cls_rule_destroy(&cr);
3094 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3095 * forced into the range of a uint16_t. */
3097 age_secs(long long int age_ms)
3099 return (age_ms < 0 ? 0
3100 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3101 : (unsigned int) age_ms / 1000);
3105 handle_flow_stats_request(struct ofconn *ofconn,
3106 const struct ofp_header *request)
3108 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3109 struct ofputil_flow_stats_request fsr;
3110 struct list replies;
3115 error = ofputil_decode_flow_stats_request(&fsr, request);
3120 error = collect_rules_loose(ofproto, fsr.table_id, &fsr.match,
3121 fsr.cookie, fsr.cookie_mask,
3122 fsr.out_port, fsr.out_group, &rules);
3127 ofpmp_init(&replies, request);
3128 LIST_FOR_EACH (rule, ofproto_node, &rules) {
3129 long long int now = time_msec();
3130 struct ofputil_flow_stats fs;
3132 minimatch_expand(&rule->cr.match, &fs.match);
3133 fs.priority = rule->cr.priority;
3134 fs.cookie = rule->flow_cookie;
3135 fs.table_id = rule->table_id;
3136 calc_duration(rule->created, now, &fs.duration_sec, &fs.duration_nsec);
3137 fs.idle_age = age_secs(now - rule->used);
3138 fs.hard_age = age_secs(now - rule->modified);
3139 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3141 fs.ofpacts = rule->ofpacts;
3142 fs.ofpacts_len = rule->ofpacts_len;
3144 ovs_mutex_lock(&rule->timeout_mutex);
3145 fs.idle_timeout = rule->idle_timeout;
3146 fs.hard_timeout = rule->hard_timeout;
3147 ovs_mutex_unlock(&rule->timeout_mutex);
3150 if (rule->send_flow_removed) {
3151 fs.flags |= OFPUTIL_FF_SEND_FLOW_REM;
3152 /* FIXME: Implement OFPUTIL_FF_NO_PKT_COUNTS and
3153 OFPUTIL_FF_NO_BYT_COUNTS. */
3155 ofputil_append_flow_stats_reply(&fs, &replies);
3157 ofconn_send_replies(ofconn, &replies);
3163 flow_stats_ds(struct rule *rule, struct ds *results)
3165 uint64_t packet_count, byte_count;
3167 rule->ofproto->ofproto_class->rule_get_stats(rule,
3168 &packet_count, &byte_count);
3170 if (rule->table_id != 0) {
3171 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3173 ds_put_format(results, "duration=%llds, ",
3174 (time_msec() - rule->created) / 1000);
3175 ds_put_format(results, "priority=%u, ", rule->cr.priority);
3176 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3177 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3178 cls_rule_format(&rule->cr, results);
3179 ds_put_char(results, ',');
3180 ofpacts_format(rule->ofpacts, rule->ofpacts_len, results);
3181 ds_put_cstr(results, "\n");
3184 /* Adds a pretty-printed description of all flows to 'results', including
3185 * hidden flows (e.g., set up by in-band control). */
3187 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3189 struct oftable *table;
3191 OFPROTO_FOR_EACH_TABLE (table, p) {
3192 struct cls_cursor cursor;
3195 ovs_rwlock_rdlock(&table->cls.rwlock);
3196 cls_cursor_init(&cursor, &table->cls, NULL);
3197 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3198 flow_stats_ds(rule, results);
3200 ovs_rwlock_unlock(&table->cls.rwlock);
3204 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3205 * '*engine_type' and '*engine_id', respectively. */
3207 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3208 uint8_t *engine_type, uint8_t *engine_id)
3210 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3213 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3214 * true if the port's CFM status was successfully stored into '*status'.
3215 * Returns false if the port did not have CFM configured, in which case
3216 * '*status' is indeterminate.
3218 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3220 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3221 struct ofproto_cfm_status *status)
3223 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3225 && ofproto->ofproto_class->get_cfm_status
3226 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3230 handle_aggregate_stats_request(struct ofconn *ofconn,
3231 const struct ofp_header *oh)
3233 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3234 struct ofputil_flow_stats_request request;
3235 struct ofputil_aggregate_stats stats;
3236 bool unknown_packets, unknown_bytes;
3237 struct ofpbuf *reply;
3242 error = ofputil_decode_flow_stats_request(&request, oh);
3247 error = collect_rules_loose(ofproto, request.table_id, &request.match,
3248 request.cookie, request.cookie_mask,
3249 request.out_port, request.out_group, &rules);
3254 memset(&stats, 0, sizeof stats);
3255 unknown_packets = unknown_bytes = false;
3256 LIST_FOR_EACH (rule, ofproto_node, &rules) {
3257 uint64_t packet_count;
3258 uint64_t byte_count;
3260 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3263 if (packet_count == UINT64_MAX) {
3264 unknown_packets = true;
3266 stats.packet_count += packet_count;
3269 if (byte_count == UINT64_MAX) {
3270 unknown_bytes = true;
3272 stats.byte_count += byte_count;
3277 if (unknown_packets) {
3278 stats.packet_count = UINT64_MAX;
3280 if (unknown_bytes) {
3281 stats.byte_count = UINT64_MAX;
3284 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3285 ofconn_send_reply(ofconn, reply);
3290 struct queue_stats_cbdata {
3291 struct ofport *ofport;
3292 struct list replies;
3297 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3298 const struct netdev_queue_stats *stats)
3300 struct ofputil_queue_stats oqs;
3302 oqs.port_no = cbdata->ofport->pp.port_no;
3303 oqs.queue_id = queue_id;
3304 oqs.tx_bytes = stats->tx_bytes;
3305 oqs.tx_packets = stats->tx_packets;
3306 oqs.tx_errors = stats->tx_errors;
3307 if (stats->created != LLONG_MIN) {
3308 calc_duration(stats->created, cbdata->now,
3309 &oqs.duration_sec, &oqs.duration_nsec);
3311 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3313 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3317 handle_queue_stats_dump_cb(uint32_t queue_id,
3318 struct netdev_queue_stats *stats,
3321 struct queue_stats_cbdata *cbdata = cbdata_;
3323 put_queue_stats(cbdata, queue_id, stats);
3327 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3328 struct queue_stats_cbdata *cbdata)
3330 cbdata->ofport = port;
3331 if (queue_id == OFPQ_ALL) {
3332 netdev_dump_queue_stats(port->netdev,
3333 handle_queue_stats_dump_cb, cbdata);
3335 struct netdev_queue_stats stats;
3337 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3338 put_queue_stats(cbdata, queue_id, &stats);
3340 return OFPERR_OFPQOFC_BAD_QUEUE;
3347 handle_queue_stats_request(struct ofconn *ofconn,
3348 const struct ofp_header *rq)
3350 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3351 struct queue_stats_cbdata cbdata;
3352 struct ofport *port;
3354 struct ofputil_queue_stats_request oqsr;
3356 COVERAGE_INC(ofproto_queue_req);
3358 ofpmp_init(&cbdata.replies, rq);
3359 cbdata.now = time_msec();
3361 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3366 if (oqsr.port_no == OFPP_ANY) {
3367 error = OFPERR_OFPQOFC_BAD_QUEUE;
3368 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3369 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3374 port = ofproto_get_port(ofproto, oqsr.port_no);
3376 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3377 : OFPERR_OFPQOFC_BAD_PORT);
3380 ofconn_send_replies(ofconn, &cbdata.replies);
3382 ofpbuf_list_delete(&cbdata.replies);
3389 is_flow_deletion_pending(const struct ofproto *ofproto,
3390 const struct cls_rule *cls_rule,
3393 if (!hmap_is_empty(&ofproto->deletions)) {
3394 struct ofoperation *op;
3396 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3397 cls_rule_hash(cls_rule, table_id),
3398 &ofproto->deletions) {
3399 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3409 evict_rule_from_table(struct ofproto *ofproto, struct oftable *table)
3414 ovs_rwlock_rdlock(&table->cls.rwlock);
3415 n_rules = classifier_count(&table->cls);
3416 ovs_rwlock_unlock(&table->cls.rwlock);
3418 if (n_rules < table->max_flows) {
3420 } else if (!choose_rule_to_evict(table, &rule)) {
3421 return OFPERR_OFPFMFC_TABLE_FULL;
3422 } else if (rule->pending) {
3423 ovs_rwlock_unlock(&rule->rwlock);
3424 return OFPROTO_POSTPONE;
3426 struct ofopgroup *group;
3428 group = ofopgroup_create_unattached(ofproto);
3429 delete_flow__(rule, group, OFPRR_EVICTION);
3430 ofopgroup_submit(group);
3436 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3437 * in which no matching flow already exists in the flow table.
3439 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3440 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3441 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3442 * initiated now but may be retried later.
3444 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
3445 * ownership remains with the caller.
3447 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3450 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3451 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3453 struct oftable *table;
3454 struct ofopgroup *group;
3460 error = check_table_id(ofproto, fm->table_id);
3466 if (fm->table_id == 0xff) {
3467 if (ofproto->ofproto_class->rule_choose_table) {
3468 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3474 ovs_assert(table_id < ofproto->n_tables);
3478 } else if (fm->table_id < ofproto->n_tables) {
3479 table_id = fm->table_id;
3481 return OFPERR_OFPBRC_BAD_TABLE_ID;
3484 table = &ofproto->tables[table_id];
3486 if (table->flags & OFTABLE_READONLY) {
3487 return OFPERR_OFPBRC_EPERM;
3490 cls_rule_init(&cr, &fm->match, fm->priority);
3492 /* Transform "add" into "modify" if there's an existing identical flow. */
3493 ovs_rwlock_rdlock(&table->cls.rwlock);
3494 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
3495 ovs_rwlock_unlock(&table->cls.rwlock);
3497 cls_rule_destroy(&cr);
3498 if (!rule_is_modifiable(rule)) {
3499 return OFPERR_OFPBRC_EPERM;
3500 } else if (rule->pending) {
3501 return OFPROTO_POSTPONE;
3506 list_push_back(&rules, &rule->ofproto_node);
3507 fm->modify_cookie = true;
3508 return modify_flows__(ofproto, ofconn, fm, request, &rules);
3512 /* Verify actions. */
3513 error = ofproto_check_ofpacts(ofproto, fm->ofpacts, fm->ofpacts_len,
3514 &fm->match.flow, table_id);
3516 cls_rule_destroy(&cr);
3520 /* Serialize against pending deletion. */
3521 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
3522 cls_rule_destroy(&cr);
3523 return OFPROTO_POSTPONE;
3526 /* Check for overlap, if requested. */
3527 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
3530 ovs_rwlock_rdlock(&table->cls.rwlock);
3531 overlaps = classifier_rule_overlaps(&table->cls, &cr);
3532 ovs_rwlock_unlock(&table->cls.rwlock);
3535 cls_rule_destroy(&cr);
3536 return OFPERR_OFPFMFC_OVERLAP;
3540 /* If necessary, evict an existing rule to clear out space. */
3541 error = evict_rule_from_table(ofproto, table);
3543 cls_rule_destroy(&cr);
3547 /* Allocate new rule. */
3548 rule = ofproto->ofproto_class->rule_alloc();
3550 cls_rule_destroy(&cr);
3551 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
3552 ofproto->name, ovs_strerror(error));
3556 /* Initialize base state. */
3557 rule->ofproto = ofproto;
3558 cls_rule_move(&rule->cr, &cr);
3559 rule->pending = NULL;
3560 rule->flow_cookie = fm->new_cookie;
3561 rule->created = rule->modified = rule->used = time_msec();
3563 ovs_mutex_init(&rule->timeout_mutex);
3564 ovs_mutex_lock(&rule->timeout_mutex);
3565 rule->idle_timeout = fm->idle_timeout;
3566 rule->hard_timeout = fm->hard_timeout;
3567 ovs_mutex_unlock(&rule->timeout_mutex);
3569 rule->table_id = table - ofproto->tables;
3570 rule->send_flow_removed = (fm->flags & OFPUTIL_FF_SEND_FLOW_REM) != 0;
3571 rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
3572 rule->ofpacts_len = fm->ofpacts_len;
3573 rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len);
3574 list_init(&rule->meter_list_node);
3575 rule->eviction_group = NULL;
3576 list_init(&rule->expirable);
3577 rule->monitor_flags = 0;
3578 rule->add_seqno = 0;
3579 rule->modify_seqno = 0;
3580 ovs_rwlock_init(&rule->rwlock);
3582 /* Construct rule, initializing derived state. */
3583 error = ofproto->ofproto_class->rule_construct(rule);
3585 ofproto_rule_destroy__(rule);
3590 oftable_insert_rule(rule);
3592 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3593 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
3594 ofproto->ofproto_class->rule_insert(rule);
3595 ofopgroup_submit(group);
3600 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3602 /* Modifies the rules listed in 'rules', changing their actions to match those
3605 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3608 * Returns 0 on success, otherwise an OpenFlow error code. */
3610 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
3611 struct ofputil_flow_mod *fm, const struct ofp_header *request,
3614 enum ofoperation_type type;
3615 struct ofopgroup *group;
3619 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
3620 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3621 error = OFPERR_OFPBRC_EPERM;
3622 LIST_FOR_EACH (rule, ofproto_node, rules) {
3623 struct ofoperation *op;
3624 bool actions_changed;
3625 bool reset_counters;
3627 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
3629 if (rule_is_modifiable(rule)) {
3630 /* At least one rule is modifiable, don't report EPERM error. */
3636 /* Verify actions. */
3637 error = ofpacts_check(fm->ofpacts, fm->ofpacts_len, &fm->match.flow,
3638 u16_to_ofp(ofproto->max_ports), rule->table_id);
3643 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
3644 rule->ofpacts, rule->ofpacts_len);
3646 op = ofoperation_create(group, rule, type, 0);
3648 if (fm->modify_cookie && fm->new_cookie != htonll(UINT64_MAX)) {
3649 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
3651 if (type == OFOPERATION_REPLACE) {
3652 ovs_mutex_lock(&rule->timeout_mutex);
3653 rule->idle_timeout = fm->idle_timeout;
3654 rule->hard_timeout = fm->hard_timeout;
3655 ovs_mutex_unlock(&rule->timeout_mutex);
3657 rule->send_flow_removed = (fm->flags
3658 & OFPUTIL_FF_SEND_FLOW_REM) != 0;
3660 if (fm->idle_timeout || fm->hard_timeout) {
3661 if (!rule->eviction_group) {
3662 eviction_group_add_rule(rule);
3665 eviction_group_remove_rule(rule);
3669 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
3670 if (actions_changed || reset_counters) {
3671 op->ofpacts = rule->ofpacts;
3672 op->ofpacts_len = rule->ofpacts_len;
3673 op->meter_id = rule->meter_id;
3675 ovs_rwlock_wrlock(&rule->rwlock);
3676 rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
3677 rule->ofpacts_len = fm->ofpacts_len;
3678 ovs_rwlock_unlock(&rule->rwlock);
3680 rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len);
3681 rule->ofproto->ofproto_class->rule_modify_actions(rule,
3684 ofoperation_complete(op, 0);
3687 ofopgroup_submit(group);
3693 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
3694 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3696 if (fm->cookie_mask != htonll(0) || fm->new_cookie == htonll(UINT64_MAX)) {
3699 return add_flow(ofproto, ofconn, fm, request);
3702 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
3705 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3708 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
3709 struct ofputil_flow_mod *fm,
3710 const struct ofp_header *request)
3715 error = collect_rules_loose(ofproto, fm->table_id, &fm->match,
3716 fm->cookie, fm->cookie_mask,
3717 OFPP_ANY, OFPG11_ANY, &rules);
3720 } else if (list_is_empty(&rules)) {
3721 return modify_flows_add(ofproto, ofconn, fm, request);
3723 return modify_flows__(ofproto, ofconn, fm, request, &rules);
3727 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3730 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3733 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3734 struct ofputil_flow_mod *fm,
3735 const struct ofp_header *request)
3740 error = collect_rules_strict(ofproto, fm->table_id, &fm->match,
3741 fm->priority, fm->cookie, fm->cookie_mask,
3742 OFPP_ANY, OFPG11_ANY, &rules);
3745 } else if (list_is_empty(&rules)) {
3746 return modify_flows_add(ofproto, ofconn, fm, request);
3748 return list_is_singleton(&rules) ? modify_flows__(ofproto, ofconn,
3749 fm, request, &rules)
3754 /* OFPFC_DELETE implementation. */
3757 delete_flow__(struct rule *rule, struct ofopgroup *group,
3758 enum ofp_flow_removed_reason reason)
3760 struct ofproto *ofproto = rule->ofproto;
3762 ofproto_rule_send_removed(rule, reason);
3764 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
3765 oftable_remove_rule(rule);
3766 ofproto->ofproto_class->rule_delete(rule);
3769 /* Deletes the rules listed in 'rules'.
3771 * Returns 0 on success, otherwise an OpenFlow error code. */
3773 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
3774 const struct ofp_header *request, struct list *rules,
3775 enum ofp_flow_removed_reason reason)
3777 struct rule *rule, *next;
3778 struct ofopgroup *group;
3780 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
3781 LIST_FOR_EACH_SAFE (rule, next, ofproto_node, rules) {
3782 ovs_rwlock_wrlock(&rule->rwlock);
3783 delete_flow__(rule, group, reason);
3785 ofopgroup_submit(group);
3790 /* Implements OFPFC_DELETE. */
3792 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
3793 const struct ofputil_flow_mod *fm,
3794 const struct ofp_header *request)
3799 error = collect_rules_loose(ofproto, fm->table_id, &fm->match,
3800 fm->cookie, fm->cookie_mask,
3801 fm->out_port, fm->out_group, &rules);
3802 return (error ? error
3803 : !list_is_empty(&rules) ? delete_flows__(ofproto, ofconn, request,
3804 &rules, OFPRR_DELETE)
3808 /* Implements OFPFC_DELETE_STRICT. */
3810 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3811 const struct ofputil_flow_mod *fm,
3812 const struct ofp_header *request)
3817 error = collect_rules_strict(ofproto, fm->table_id, &fm->match,
3818 fm->priority, fm->cookie, fm->cookie_mask,
3819 fm->out_port, fm->out_group, &rules);
3820 return (error ? error
3821 : list_is_singleton(&rules) ? delete_flows__(ofproto, ofconn,
3828 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
3830 struct ofputil_flow_removed fr;
3832 if (ofproto_rule_is_hidden(rule) || !rule->send_flow_removed) {
3836 minimatch_expand(&rule->cr.match, &fr.match);
3837 fr.priority = rule->cr.priority;
3838 fr.cookie = rule->flow_cookie;
3840 fr.table_id = rule->table_id;
3841 calc_duration(rule->created, time_msec(),
3842 &fr.duration_sec, &fr.duration_nsec);
3843 ovs_mutex_lock(&rule->timeout_mutex);
3844 fr.idle_timeout = rule->idle_timeout;
3845 fr.hard_timeout = rule->hard_timeout;
3846 ovs_mutex_unlock(&rule->timeout_mutex);
3847 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
3850 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
3853 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
3854 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
3857 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
3860 * ofproto implementation ->run() functions should use this function to expire
3861 * OpenFlow flows. */
3863 ofproto_rule_expire(struct rule *rule, uint8_t reason)
3865 struct ofproto *ofproto = rule->ofproto;
3866 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
3868 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
3869 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
3870 ofproto_rule_send_removed(rule, reason);
3872 ovs_rwlock_wrlock(&cls->rwlock);
3873 ofproto_delete_rule(ofproto, cls, rule);
3874 ovs_rwlock_unlock(&cls->rwlock);
3877 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
3878 * means "infinite". */
3880 reduce_timeout(uint16_t max, uint16_t *timeout)
3882 if (max && (!*timeout || *timeout > max)) {
3887 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
3888 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
3889 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
3891 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
3893 ofproto_rule_reduce_timeouts(struct rule *rule,
3894 uint16_t idle_timeout, uint16_t hard_timeout)
3895 OVS_EXCLUDED(rule->ofproto->expirable_mutex, rule->timeout_mutex)
3897 if (!idle_timeout && !hard_timeout) {
3901 ovs_mutex_lock(&rule->ofproto->expirable_mutex);
3902 if (list_is_empty(&rule->expirable)) {
3903 list_insert(&rule->ofproto->expirable, &rule->expirable);
3905 ovs_mutex_unlock(&rule->ofproto->expirable_mutex);
3907 ovs_mutex_lock(&rule->timeout_mutex);
3908 reduce_timeout(idle_timeout, &rule->idle_timeout);
3909 reduce_timeout(hard_timeout, &rule->hard_timeout);
3910 ovs_mutex_unlock(&rule->timeout_mutex);
3914 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3916 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3917 struct ofputil_flow_mod fm;
3918 uint64_t ofpacts_stub[1024 / 8];
3919 struct ofpbuf ofpacts;
3923 error = reject_slave_controller(ofconn);
3928 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3929 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
3932 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
3935 goto exit_free_ofpacts;
3938 /* Record the operation for logging a summary report. */
3939 switch (fm.command) {
3945 case OFPFC_MODIFY_STRICT:
3946 ofproto->n_modify++;
3950 case OFPFC_DELETE_STRICT:
3951 ofproto->n_delete++;
3956 if (ofproto->next_op_report == LLONG_MAX) {
3957 ofproto->first_op = now;
3958 ofproto->next_op_report = MAX(now + 10 * 1000,
3959 ofproto->op_backoff);
3960 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
3962 ofproto->last_op = now;
3965 ofpbuf_uninit(&ofpacts);
3971 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
3972 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
3974 if (ofproto->n_pending >= 50) {
3975 ovs_assert(!list_is_empty(&ofproto->pending));
3976 return OFPROTO_POSTPONE;
3979 switch (fm->command) {
3981 return add_flow(ofproto, ofconn, fm, oh);
3984 return modify_flows_loose(ofproto, ofconn, fm, oh);
3986 case OFPFC_MODIFY_STRICT:
3987 return modify_flow_strict(ofproto, ofconn, fm, oh);
3990 return delete_flows_loose(ofproto, ofconn, fm, oh);
3992 case OFPFC_DELETE_STRICT:
3993 return delete_flow_strict(ofproto, ofconn, fm, oh);
3996 if (fm->command > 0xff) {
3997 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
3998 "flow_mod_table_id extension is not enabled",
4001 return OFPERR_OFPFMFC_BAD_COMMAND;
4006 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4008 struct ofputil_role_request request;
4009 struct ofputil_role_request reply;
4013 error = ofputil_decode_role_message(oh, &request);
4018 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4019 if (ofconn_get_role(ofconn) != request.role
4020 && ofconn_has_pending_opgroups(ofconn)) {
4021 return OFPROTO_POSTPONE;
4024 if (request.have_generation_id
4025 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4026 return OFPERR_OFPRRFC_STALE;
4029 ofconn_set_role(ofconn, request.role);
4032 reply.role = ofconn_get_role(ofconn);
4033 reply.have_generation_id = ofconn_get_master_election_id(
4034 ofconn, &reply.generation_id);
4035 buf = ofputil_encode_role_reply(oh, &reply);
4036 ofconn_send_reply(ofconn, buf);
4042 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4043 const struct ofp_header *oh)
4045 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4046 enum ofputil_protocol cur, next;
4048 cur = ofconn_get_protocol(ofconn);
4049 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4050 ofconn_set_protocol(ofconn, next);
4056 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4058 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4059 enum ofputil_protocol cur, next;
4060 enum ofputil_protocol next_base;
4062 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4064 return OFPERR_OFPBRC_EPERM;
4067 cur = ofconn_get_protocol(ofconn);
4068 next = ofputil_protocol_set_base(cur, next_base);
4069 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4070 /* Avoid sending async messages in surprising protocol. */
4071 return OFPROTO_POSTPONE;
4074 ofconn_set_protocol(ofconn, next);
4079 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4080 const struct ofp_header *oh)
4082 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4085 format = ntohl(msg->format);
4086 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4087 return OFPERR_OFPBRC_EPERM;
4090 if (format != ofconn_get_packet_in_format(ofconn)
4091 && ofconn_has_pending_opgroups(ofconn)) {
4092 /* Avoid sending async message in surprsing packet in format. */
4093 return OFPROTO_POSTPONE;
4096 ofconn_set_packet_in_format(ofconn, format);
4101 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4103 const struct nx_async_config *msg = ofpmsg_body(oh);
4104 uint32_t master[OAM_N_TYPES];
4105 uint32_t slave[OAM_N_TYPES];
4107 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4108 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4109 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4111 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4112 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4113 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4115 ofconn_set_async_config(ofconn, master, slave);
4116 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4117 !ofconn_get_miss_send_len(ofconn)) {
4118 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4125 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4128 uint32_t master[OAM_N_TYPES];
4129 uint32_t slave[OAM_N_TYPES];
4130 struct nx_async_config *msg;
4132 ofconn_get_async_config(ofconn, master, slave);
4133 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4134 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4136 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4137 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4138 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4140 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4141 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4142 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4144 ofconn_send_reply(ofconn, buf);
4150 handle_nxt_set_controller_id(struct ofconn *ofconn,
4151 const struct ofp_header *oh)
4153 const struct nx_controller_id *nci = ofpmsg_body(oh);
4155 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4156 return OFPERR_NXBRC_MUST_BE_ZERO;
4159 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4164 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4168 if (ofconn_has_pending_opgroups(ofconn)) {
4169 return OFPROTO_POSTPONE;
4172 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4173 ? OFPRAW_OFPT10_BARRIER_REPLY
4174 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4175 ofconn_send_reply(ofconn, buf);
4180 ofproto_compose_flow_refresh_update(const struct rule *rule,
4181 enum nx_flow_monitor_flags flags,
4184 struct ofoperation *op = rule->pending;
4185 struct ofputil_flow_update fu;
4188 if (op && op->type == OFOPERATION_ADD) {
4189 /* We'll report the final flow when the operation completes. Reporting
4190 * it now would cause a duplicate report later. */
4194 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4195 ? NXFME_ADDED : NXFME_MODIFIED);
4197 ovs_mutex_lock(&rule->timeout_mutex);
4198 fu.idle_timeout = rule->idle_timeout;
4199 fu.hard_timeout = rule->hard_timeout;
4200 ovs_mutex_unlock(&rule->timeout_mutex);
4201 fu.table_id = rule->table_id;
4202 fu.cookie = rule->flow_cookie;
4203 minimatch_expand(&rule->cr.match, &match);
4205 fu.priority = rule->cr.priority;
4206 if (!(flags & NXFMF_ACTIONS)) {
4210 fu.ofpacts = rule->ofpacts;
4211 fu.ofpacts_len = rule->ofpacts_len;
4213 /* An operation is in progress. Use the previous version of the flow's
4214 * actions, so that when the operation commits we report the change. */
4216 case OFOPERATION_ADD:
4219 case OFOPERATION_MODIFY:
4220 case OFOPERATION_REPLACE:
4222 fu.ofpacts = op->ofpacts;
4223 fu.ofpacts_len = op->ofpacts_len;
4225 fu.ofpacts = rule->ofpacts;
4226 fu.ofpacts_len = rule->ofpacts_len;
4230 case OFOPERATION_DELETE:
4231 fu.ofpacts = rule->ofpacts;
4232 fu.ofpacts_len = rule->ofpacts_len;
4240 if (list_is_empty(msgs)) {
4241 ofputil_start_flow_update(msgs);
4243 ofputil_append_flow_update(&fu, msgs);
4247 ofmonitor_compose_refresh_updates(struct list *rules, struct list *msgs)
4251 LIST_FOR_EACH (rule, ofproto_node, rules) {
4252 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4253 rule->monitor_flags = 0;
4255 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4260 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4261 struct rule *rule, uint64_t seqno,
4264 enum nx_flow_monitor_flags update;
4266 if (ofproto_rule_is_hidden(rule)) {
4271 ? ofoperation_has_out_port(rule->pending, m->out_port)
4272 : ofproto_rule_has_out_port(rule, m->out_port))) {
4277 if (rule->add_seqno > seqno) {
4278 update = NXFMF_ADD | NXFMF_MODIFY;
4279 } else if (rule->modify_seqno > seqno) {
4280 update = NXFMF_MODIFY;
4285 if (!(m->flags & update)) {
4289 update = NXFMF_INITIAL;
4292 if (!rule->monitor_flags) {
4293 list_push_back(rules, &rule->ofproto_node);
4295 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4299 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4303 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4304 const struct ofoperation *op;
4305 const struct oftable *table;
4306 struct cls_rule target;
4308 cls_rule_init_from_minimatch(&target, &m->match, 0);
4309 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4310 struct cls_cursor cursor;
4313 ovs_rwlock_rdlock(&table->cls.rwlock);
4314 cls_cursor_init(&cursor, &table->cls, &target);
4315 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4316 ovs_assert(!rule->pending); /* XXX */
4317 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4319 ovs_rwlock_unlock(&table->cls.rwlock);
4322 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4323 struct rule *rule = op->rule;
4325 if (((m->table_id == 0xff
4326 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4327 : m->table_id == rule->table_id))
4328 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4329 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4332 cls_rule_destroy(&target);
4336 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4339 if (m->flags & NXFMF_INITIAL) {
4340 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4345 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4346 uint64_t seqno, struct list *rules)
4348 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4352 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4354 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4355 struct ofmonitor **monitors;
4356 size_t n_monitors, allocated_monitors;
4357 struct list replies;
4364 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4366 n_monitors = allocated_monitors = 0;
4368 struct ofputil_flow_monitor_request request;
4369 struct ofmonitor *m;
4372 retval = ofputil_decode_flow_monitor_request(&request, &b);
4373 if (retval == EOF) {
4375 } else if (retval) {
4380 if (request.table_id != 0xff
4381 && request.table_id >= ofproto->n_tables) {
4382 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4386 error = ofmonitor_create(&request, ofconn, &m);
4391 if (n_monitors >= allocated_monitors) {
4392 monitors = x2nrealloc(monitors, &allocated_monitors,
4395 monitors[n_monitors++] = m;
4399 for (i = 0; i < n_monitors; i++) {
4400 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4403 ofpmp_init(&replies, oh);
4404 ofmonitor_compose_refresh_updates(&rules, &replies);
4405 ofconn_send_replies(ofconn, &replies);
4412 for (i = 0; i < n_monitors; i++) {
4413 ofmonitor_destroy(monitors[i]);
4420 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4422 struct ofmonitor *m;
4425 id = ofputil_decode_flow_monitor_cancel(oh);
4426 m = ofmonitor_lookup(ofconn, id);
4428 return OFPERR_NXBRC_FM_BAD_ID;
4431 ofmonitor_destroy(m);
4435 /* Meters implementation.
4437 * Meter table entry, indexed by the OpenFlow meter_id.
4438 * These are always dynamically allocated to allocate enough space for
4440 * 'created' is used to compute the duration for meter stats.
4441 * 'list rules' is needed so that we can delete the dependent rules when the
4442 * meter table entry is deleted.
4443 * 'provider_meter_id' is for the provider's private use.
4446 long long int created; /* Time created. */
4447 struct list rules; /* List of "struct rule_dpif"s. */
4448 ofproto_meter_id provider_meter_id;
4449 uint16_t flags; /* Meter flags. */
4450 uint16_t n_bands; /* Number of meter bands. */
4451 struct ofputil_meter_band *bands;
4455 * This is used in instruction validation at flow set-up time,
4456 * as flows may not use non-existing meters.
4457 * This is also used by ofproto-providers to translate OpenFlow meter_ids
4458 * in METER instructions to the corresponding provider meter IDs.
4459 * Return value of UINT32_MAX signifies an invalid meter.
4462 ofproto_get_provider_meter_id(const struct ofproto * ofproto,
4463 uint32_t of_meter_id)
4465 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
4466 const struct meter *meter = ofproto->meters[of_meter_id];
4468 return meter->provider_meter_id.uint32;
4475 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
4479 meter->flags = config->flags;
4480 meter->n_bands = config->n_bands;
4481 meter->bands = xmemdup(config->bands,
4482 config->n_bands * sizeof *meter->bands);
4485 static struct meter *
4486 meter_create(const struct ofputil_meter_config *config,
4487 ofproto_meter_id provider_meter_id)
4489 struct meter *meter;
4491 meter = xzalloc(sizeof *meter);
4492 meter->provider_meter_id = provider_meter_id;
4493 meter->created = time_msec();
4494 list_init(&meter->rules);
4496 meter_update(meter, config);
4502 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
4505 for (mid = first; mid <= last; ++mid) {
4506 struct meter *meter = ofproto->meters[mid];
4508 ofproto->meters[mid] = NULL;
4509 ofproto->ofproto_class->meter_del(ofproto,
4510 meter->provider_meter_id);
4518 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4520 ofproto_meter_id provider_meter_id = { UINT32_MAX };
4521 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
4525 return OFPERR_OFPMMFC_METER_EXISTS;
4528 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
4531 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
4532 *meterp = meter_create(&mm->meter, provider_meter_id);
4538 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4540 struct meter *meter = ofproto->meters[mm->meter.meter_id];
4544 return OFPERR_OFPMMFC_UNKNOWN_METER;
4547 error = ofproto->ofproto_class->meter_set(ofproto,
4548 &meter->provider_meter_id,
4550 ovs_assert(meter->provider_meter_id.uint32 != UINT32_MAX);
4552 meter_update(meter, &mm->meter);
4558 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
4559 struct ofputil_meter_mod *mm)
4561 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4562 uint32_t meter_id = mm->meter.meter_id;
4563 uint32_t first, last;
4566 if (meter_id == OFPM13_ALL) {
4568 last = ofproto->meter_features.max_meters;
4570 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
4573 first = last = meter_id;
4576 /* First delete the rules that use this meter. If any of those rules are
4577 * currently being modified, postpone the whole operation until later. */
4579 for (meter_id = first; meter_id <= last; ++meter_id) {
4580 struct meter *meter = ofproto->meters[meter_id];
4581 if (meter && !list_is_empty(&meter->rules)) {
4584 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
4585 if (rule->pending) {
4586 return OFPROTO_POSTPONE;
4588 list_push_back(&rules, &rule->ofproto_node);
4592 if (!list_is_empty(&rules)) {
4593 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
4596 /* Delete the meters. */
4597 meter_delete(ofproto, first, last);
4603 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4605 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4606 struct ofputil_meter_mod mm;
4607 uint64_t bands_stub[256 / 8];
4608 struct ofpbuf bands;
4612 error = reject_slave_controller(ofconn);
4617 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4619 error = ofputil_decode_meter_mod(oh, &mm, &bands);
4621 goto exit_free_bands;
4624 meter_id = mm.meter.meter_id;
4626 if (mm.command != OFPMC13_DELETE) {
4627 /* Fails also when meters are not implemented by the provider. */
4628 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
4629 error = OFPERR_OFPMMFC_INVALID_METER;
4630 goto exit_free_bands;
4632 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
4633 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
4634 goto exit_free_bands;
4638 switch (mm.command) {
4640 error = handle_add_meter(ofproto, &mm);
4643 case OFPMC13_MODIFY:
4644 error = handle_modify_meter(ofproto, &mm);
4647 case OFPMC13_DELETE:
4648 error = handle_delete_meter(ofconn, oh, &mm);
4652 error = OFPERR_OFPMMFC_BAD_COMMAND;
4657 ofpbuf_uninit(&bands);
4662 handle_meter_features_request(struct ofconn *ofconn,
4663 const struct ofp_header *request)
4665 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4666 struct ofputil_meter_features features;
4669 if (ofproto->ofproto_class->meter_get_features) {
4670 ofproto->ofproto_class->meter_get_features(ofproto, &features);
4672 memset(&features, 0, sizeof features);
4674 b = ofputil_encode_meter_features_reply(&features, request);
4676 ofconn_send_reply(ofconn, b);
4681 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
4684 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4685 struct list replies;
4686 uint64_t bands_stub[256 / 8];
4687 struct ofpbuf bands;
4688 uint32_t meter_id, first, last;
4690 ofputil_decode_meter_request(request, &meter_id);
4692 if (meter_id == OFPM13_ALL) {
4694 last = ofproto->meter_features.max_meters;
4696 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
4697 !ofproto->meters[meter_id]) {
4698 return OFPERR_OFPMMFC_UNKNOWN_METER;
4700 first = last = meter_id;
4703 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4704 ofpmp_init(&replies, request);
4706 for (meter_id = first; meter_id <= last; ++meter_id) {
4707 struct meter *meter = ofproto->meters[meter_id];
4709 continue; /* Skip non-existing meters. */
4711 if (type == OFPTYPE_METER_STATS_REQUEST) {
4712 struct ofputil_meter_stats stats;
4714 stats.meter_id = meter_id;
4716 /* Provider sets the packet and byte counts, we do the rest. */
4717 stats.flow_count = list_size(&meter->rules);
4718 calc_duration(meter->created, time_msec(),
4719 &stats.duration_sec, &stats.duration_nsec);
4720 stats.n_bands = meter->n_bands;
4721 ofpbuf_clear(&bands);
4723 = ofpbuf_put_uninit(&bands,
4724 meter->n_bands * sizeof *stats.bands);
4726 if (!ofproto->ofproto_class->meter_get(ofproto,
4727 meter->provider_meter_id,
4729 ofputil_append_meter_stats(&replies, &stats);
4731 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
4732 struct ofputil_meter_config config;
4734 config.meter_id = meter_id;
4735 config.flags = meter->flags;
4736 config.n_bands = meter->n_bands;
4737 config.bands = meter->bands;
4738 ofputil_append_meter_config(&replies, &config);
4742 ofconn_send_replies(ofconn, &replies);
4743 ofpbuf_uninit(&bands);
4748 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
4749 struct ofgroup **group)
4750 OVS_TRY_RDLOCK(true, (*group)->rwlock)
4752 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4753 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
4754 hash_int(group_id, 0), &ofproto->groups) {
4755 if ((*group)->group_id == group_id) {
4756 ovs_rwlock_rdlock(&(*group)->rwlock);
4757 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4761 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4766 ofproto_group_release(struct ofgroup *group)
4767 OVS_RELEASES(group->rwlock)
4769 ovs_rwlock_unlock(&group->rwlock);
4773 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
4774 struct ofgroup **group)
4775 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
4776 OVS_TRY_WRLOCK(true, (*group)->rwlock)
4778 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
4779 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
4780 hash_int(group_id, 0), &ofproto->groups) {
4781 if ((*group)->group_id == group_id) {
4782 ovs_rwlock_wrlock(&(*group)->rwlock);
4786 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4791 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
4792 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
4794 struct ofgroup *grp;
4796 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
4797 hash_int(group_id, 0), &ofproto->groups) {
4798 if (grp->group_id == group_id) {
4806 append_group_stats(struct ofgroup *group, struct list *replies)
4807 OVS_REQ_RDLOCK(group->rwlock)
4809 struct ofputil_group_stats ogs;
4810 struct ofproto *ofproto = group->ofproto;
4811 long long int now = time_msec();
4814 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
4816 error = (ofproto->ofproto_class->group_get_stats
4817 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
4820 ogs.ref_count = UINT32_MAX;
4821 ogs.packet_count = UINT64_MAX;
4822 ogs.byte_count = UINT64_MAX;
4823 ogs.n_buckets = group->n_buckets;
4824 memset(ogs.bucket_stats, 0xff,
4825 ogs.n_buckets * sizeof *ogs.bucket_stats);
4828 ogs.group_id = group->group_id;
4829 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
4831 ofputil_append_group_stats(replies, &ogs);
4833 free(ogs.bucket_stats);
4837 handle_group_stats_request(struct ofconn *ofconn,
4838 const struct ofp_header *request)
4840 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4841 struct list replies;
4843 struct ofgroup *group;
4846 error = ofputil_decode_group_stats_request(request, &group_id);
4851 ofpmp_init(&replies, request);
4853 if (group_id == OFPG_ALL) {
4854 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4855 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
4856 ovs_rwlock_rdlock(&group->rwlock);
4857 append_group_stats(group, &replies);
4858 ovs_rwlock_unlock(&group->rwlock);
4860 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4862 if (ofproto_group_lookup(ofproto, group_id, &group)) {
4863 append_group_stats(group, &replies);
4864 ofproto_group_release(group);
4868 ofconn_send_replies(ofconn, &replies);
4874 handle_group_desc_stats_request(struct ofconn *ofconn,
4875 const struct ofp_header *request)
4877 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4878 struct list replies;
4879 struct ofputil_group_desc gds;
4880 struct ofgroup *group;
4882 ofpmp_init(&replies, request);
4884 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4885 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
4886 gds.group_id = group->group_id;
4887 gds.type = group->type;
4888 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
4890 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4892 ofconn_send_replies(ofconn, &replies);
4898 handle_group_features_stats_request(struct ofconn *ofconn,
4899 const struct ofp_header *request)
4901 struct ofproto *p = ofconn_get_ofproto(ofconn);
4904 msg = ofputil_encode_group_features_reply(&p->ogf, request);
4906 ofconn_send_reply(ofconn, msg);
4912 /* Implements OFPGC11_ADD
4913 * in which no matching flow already exists in the flow table.
4915 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
4916 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
4917 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
4918 * initiated now but may be retried later.
4920 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
4921 * ownership remains with the caller.
4923 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
4926 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
4928 struct ofgroup *ofgroup;
4931 if (gm->group_id > OFPG_MAX) {
4932 return OFPERR_OFPGMFC_INVALID_GROUP;
4934 if (gm->type > OFPGT11_FF) {
4935 return OFPERR_OFPGMFC_BAD_TYPE;
4938 /* Allocate new group and initialize it. */
4939 ofgroup = ofproto->ofproto_class->group_alloc();
4941 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
4942 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
4945 ovs_rwlock_init(&ofgroup->rwlock);
4946 ofgroup->ofproto = ofproto;
4947 ofgroup->group_id = gm->group_id;
4948 ofgroup->type = gm->type;
4949 ofgroup->created = ofgroup->modified = time_msec();
4951 list_move(&ofgroup->buckets, &gm->buckets);
4952 ofgroup->n_buckets = list_size(&ofgroup->buckets);
4954 /* Construct called BEFORE any locks are held. */
4955 error = ofproto->ofproto_class->group_construct(ofgroup);
4960 /* We wrlock as late as possible to minimize the time we jam any other
4961 * threads: No visible state changes before acquiring the lock. */
4962 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
4964 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
4965 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
4969 if (ofproto_group_exists(ofproto, gm->group_id)) {
4970 error = OFPERR_OFPGMFC_GROUP_EXISTS;
4975 /* Insert new group. */
4976 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
4977 hash_int(ofgroup->group_id, 0));
4978 ofproto->n_groups[ofgroup->type]++;
4980 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4985 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4986 ofproto->ofproto_class->group_destruct(ofgroup);
4988 ofputil_bucket_list_destroy(&ofgroup->buckets);
4989 ofproto->ofproto_class->group_dealloc(ofgroup);
4994 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4997 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5000 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5002 struct ofgroup *ofgroup;
5003 struct ofgroup *victim;
5006 if (gm->group_id > OFPG_MAX) {
5007 return OFPERR_OFPGMFC_INVALID_GROUP;
5010 if (gm->type > OFPGT11_FF) {
5011 return OFPERR_OFPGMFC_BAD_TYPE;
5014 victim = ofproto->ofproto_class->group_alloc();
5016 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5017 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5020 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
5021 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5024 /* Both group's and its container's write locks held now.
5025 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5026 if (ofgroup->type != gm->type
5027 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5028 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5033 list_move(&victim->buckets, &ofgroup->buckets);
5035 ofgroup->type = gm->type;
5036 list_move(&ofgroup->buckets, &gm->buckets);
5037 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5039 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
5041 ofputil_bucket_list_destroy(&victim->buckets);
5042 ofproto->n_groups[victim->type]--;
5043 ofproto->n_groups[ofgroup->type]++;
5044 ofgroup->modified = time_msec();
5046 ofputil_bucket_list_destroy(&ofgroup->buckets);
5049 list_move(&ofgroup->buckets, &victim->buckets);
5053 ovs_rwlock_unlock(&ofgroup->rwlock);
5054 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5056 ofproto->ofproto_class->group_dealloc(victim);
5061 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5062 OVS_RELEASES(ofproto->groups_rwlock)
5064 /* Must wait until existing readers are done,
5065 * while holding the container's write lock at the same time. */
5066 ovs_rwlock_wrlock(&ofgroup->rwlock);
5067 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5068 /* No-one can find this group any more. */
5069 ofproto->n_groups[ofgroup->type]--;
5070 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5072 ofproto->ofproto_class->group_destruct(ofgroup);
5073 ofputil_bucket_list_destroy(&ofgroup->buckets);
5074 ovs_rwlock_unlock(&ofgroup->rwlock);
5075 ovs_rwlock_destroy(&ofgroup->rwlock);
5076 ofproto->ofproto_class->group_dealloc(ofgroup);
5079 /* Implements OFPGC_DELETE. */
5081 delete_group(struct ofproto *ofproto, uint32_t group_id)
5083 struct ofgroup *ofgroup;
5085 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5086 if (group_id == OFPG_ALL) {
5088 struct hmap_node *node = hmap_first(&ofproto->groups);
5092 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5093 delete_group__(ofproto, ofgroup);
5094 /* Lock for each node separately, so that we will not jam the
5095 * other threads for too long time. */
5096 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5099 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5100 hash_int(group_id, 0), &ofproto->groups) {
5101 if (ofgroup->group_id == group_id) {
5102 delete_group__(ofproto, ofgroup);
5107 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5111 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5113 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5114 struct ofputil_group_mod gm;
5117 error = reject_slave_controller(ofconn);
5122 error = ofputil_decode_group_mod(oh, &gm);
5127 switch (gm.command) {
5129 return add_group(ofproto, &gm);
5131 case OFPGC11_MODIFY:
5132 return modify_group(ofproto, &gm);
5134 case OFPGC11_DELETE:
5135 delete_group(ofproto, gm.group_id);
5139 if (gm.command > OFPGC11_DELETE) {
5140 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5141 ofproto->name, gm.command);
5143 return OFPERR_OFPGMFC_BAD_COMMAND;
5148 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5150 const struct ofp_header *oh = msg->data;
5154 error = ofptype_decode(&type, oh);
5160 /* OpenFlow requests. */
5161 case OFPTYPE_ECHO_REQUEST:
5162 return handle_echo_request(ofconn, oh);
5164 case OFPTYPE_FEATURES_REQUEST:
5165 return handle_features_request(ofconn, oh);
5167 case OFPTYPE_GET_CONFIG_REQUEST:
5168 return handle_get_config_request(ofconn, oh);
5170 case OFPTYPE_SET_CONFIG:
5171 return handle_set_config(ofconn, oh);
5173 case OFPTYPE_PACKET_OUT:
5174 return handle_packet_out(ofconn, oh);
5176 case OFPTYPE_PORT_MOD:
5177 return handle_port_mod(ofconn, oh);
5179 case OFPTYPE_FLOW_MOD:
5180 return handle_flow_mod(ofconn, oh);
5182 case OFPTYPE_GROUP_MOD:
5183 return handle_group_mod(ofconn, oh);
5185 case OFPTYPE_METER_MOD:
5186 return handle_meter_mod(ofconn, oh);
5188 case OFPTYPE_BARRIER_REQUEST:
5189 return handle_barrier_request(ofconn, oh);
5191 case OFPTYPE_ROLE_REQUEST:
5192 return handle_role_request(ofconn, oh);
5194 /* OpenFlow replies. */
5195 case OFPTYPE_ECHO_REPLY:
5198 /* Nicira extension requests. */
5199 case OFPTYPE_FLOW_MOD_TABLE_ID:
5200 return handle_nxt_flow_mod_table_id(ofconn, oh);
5202 case OFPTYPE_SET_FLOW_FORMAT:
5203 return handle_nxt_set_flow_format(ofconn, oh);
5205 case OFPTYPE_SET_PACKET_IN_FORMAT:
5206 return handle_nxt_set_packet_in_format(ofconn, oh);
5208 case OFPTYPE_SET_CONTROLLER_ID:
5209 return handle_nxt_set_controller_id(ofconn, oh);
5211 case OFPTYPE_FLOW_AGE:
5212 /* Nothing to do. */
5215 case OFPTYPE_FLOW_MONITOR_CANCEL:
5216 return handle_flow_monitor_cancel(ofconn, oh);
5218 case OFPTYPE_SET_ASYNC_CONFIG:
5219 return handle_nxt_set_async_config(ofconn, oh);
5221 case OFPTYPE_GET_ASYNC_REQUEST:
5222 return handle_nxt_get_async_request(ofconn, oh);
5224 /* Statistics requests. */
5225 case OFPTYPE_DESC_STATS_REQUEST:
5226 return handle_desc_stats_request(ofconn, oh);
5228 case OFPTYPE_FLOW_STATS_REQUEST:
5229 return handle_flow_stats_request(ofconn, oh);
5231 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5232 return handle_aggregate_stats_request(ofconn, oh);
5234 case OFPTYPE_TABLE_STATS_REQUEST:
5235 return handle_table_stats_request(ofconn, oh);
5237 case OFPTYPE_PORT_STATS_REQUEST:
5238 return handle_port_stats_request(ofconn, oh);
5240 case OFPTYPE_QUEUE_STATS_REQUEST:
5241 return handle_queue_stats_request(ofconn, oh);
5243 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5244 return handle_port_desc_stats_request(ofconn, oh);
5246 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5247 return handle_flow_monitor_request(ofconn, oh);
5249 case OFPTYPE_METER_STATS_REQUEST:
5250 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5251 return handle_meter_request(ofconn, oh, type);
5253 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5254 return handle_meter_features_request(ofconn, oh);
5256 case OFPTYPE_GROUP_STATS_REQUEST:
5257 return handle_group_stats_request(ofconn, oh);
5259 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5260 return handle_group_desc_stats_request(ofconn, oh);
5262 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5263 return handle_group_features_stats_request(ofconn, oh);
5265 /* FIXME: Change the following once they are implemented: */
5266 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5267 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
5268 return OFPERR_OFPBRC_BAD_TYPE;
5272 case OFPTYPE_FEATURES_REPLY:
5273 case OFPTYPE_GET_CONFIG_REPLY:
5274 case OFPTYPE_PACKET_IN:
5275 case OFPTYPE_FLOW_REMOVED:
5276 case OFPTYPE_PORT_STATUS:
5277 case OFPTYPE_BARRIER_REPLY:
5278 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5279 case OFPTYPE_DESC_STATS_REPLY:
5280 case OFPTYPE_FLOW_STATS_REPLY:
5281 case OFPTYPE_QUEUE_STATS_REPLY:
5282 case OFPTYPE_PORT_STATS_REPLY:
5283 case OFPTYPE_TABLE_STATS_REPLY:
5284 case OFPTYPE_AGGREGATE_STATS_REPLY:
5285 case OFPTYPE_PORT_DESC_STATS_REPLY:
5286 case OFPTYPE_ROLE_REPLY:
5287 case OFPTYPE_FLOW_MONITOR_PAUSED:
5288 case OFPTYPE_FLOW_MONITOR_RESUMED:
5289 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
5290 case OFPTYPE_GET_ASYNC_REPLY:
5291 case OFPTYPE_GROUP_STATS_REPLY:
5292 case OFPTYPE_GROUP_DESC_STATS_REPLY:
5293 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
5294 case OFPTYPE_METER_STATS_REPLY:
5295 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5296 case OFPTYPE_METER_FEATURES_STATS_REPLY:
5297 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
5299 return OFPERR_OFPBRC_BAD_TYPE;
5304 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
5306 int error = handle_openflow__(ofconn, ofp_msg);
5307 if (error && error != OFPROTO_POSTPONE) {
5308 ofconn_send_error(ofconn, ofp_msg->data, error);
5310 COVERAGE_INC(ofproto_recv_openflow);
5311 return error != OFPROTO_POSTPONE;
5314 /* Asynchronous operations. */
5316 /* Creates and returns a new ofopgroup that is not associated with any
5317 * OpenFlow connection.
5319 * The caller should add operations to the returned group with
5320 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5321 static struct ofopgroup *
5322 ofopgroup_create_unattached(struct ofproto *ofproto)
5324 struct ofopgroup *group = xzalloc(sizeof *group);
5325 group->ofproto = ofproto;
5326 list_init(&group->ofproto_node);
5327 list_init(&group->ops);
5328 list_init(&group->ofconn_node);
5332 /* Creates and returns a new ofopgroup for 'ofproto'.
5334 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
5335 * connection. The 'request' and 'buffer_id' arguments are ignored.
5337 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
5338 * If the ofopgroup eventually fails, then the error reply will include
5339 * 'request'. If the ofopgroup eventually succeeds, then the packet with
5340 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
5342 * The caller should add operations to the returned group with
5343 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5344 static struct ofopgroup *
5345 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
5346 const struct ofp_header *request, uint32_t buffer_id)
5348 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
5350 size_t request_len = ntohs(request->length);
5352 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
5354 ofconn_add_opgroup(ofconn, &group->ofconn_node);
5355 group->ofconn = ofconn;
5356 group->request = xmemdup(request, MIN(request_len, 64));
5357 group->buffer_id = buffer_id;
5362 /* Submits 'group' for processing.
5364 * If 'group' contains no operations (e.g. none were ever added, or all of the
5365 * ones that were added completed synchronously), then it is destroyed
5366 * immediately. Otherwise it is added to the ofproto's list of pending
5369 ofopgroup_submit(struct ofopgroup *group)
5371 if (!group->n_running) {
5372 ofopgroup_complete(group);
5374 list_push_back(&group->ofproto->pending, &group->ofproto_node);
5375 group->ofproto->n_pending++;
5380 ofopgroup_complete(struct ofopgroup *group)
5382 struct ofproto *ofproto = group->ofproto;
5384 struct ofconn *abbrev_ofconn;
5385 ovs_be32 abbrev_xid;
5387 struct ofoperation *op, *next_op;
5390 ovs_assert(!group->n_running);
5393 LIST_FOR_EACH (op, group_node, &group->ops) {
5400 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
5401 LIST_FOR_EACH (op, group_node, &group->ops) {
5402 if (op->type != OFOPERATION_DELETE) {
5403 struct ofpbuf *packet;
5406 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
5410 error = rule_execute(op->rule, in_port, packet);
5417 if (!error && !list_is_empty(&group->ofconn_node)) {
5418 abbrev_ofconn = group->ofconn;
5419 abbrev_xid = group->request->xid;
5421 abbrev_ofconn = NULL;
5422 abbrev_xid = htonl(0);
5424 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
5425 struct rule *rule = op->rule;
5427 /* We generally want to report the change to active OpenFlow flow
5428 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
5430 - The operation failed.
5432 - The affected rule is not visible to controllers.
5434 - The operation's only effect was to update rule->modified. */
5436 || ofproto_rule_is_hidden(rule)
5437 || (op->type == OFOPERATION_MODIFY
5439 && rule->flow_cookie == op->flow_cookie))) {
5440 /* Check that we can just cast from ofoperation_type to
5441 * nx_flow_update_event. */
5442 enum nx_flow_update_event event_type;
5445 case OFOPERATION_ADD:
5446 case OFOPERATION_REPLACE:
5447 event_type = NXFME_ADDED;
5450 case OFOPERATION_DELETE:
5451 event_type = NXFME_DELETED;
5454 case OFOPERATION_MODIFY:
5455 event_type = NXFME_MODIFIED;
5462 ofmonitor_report(ofproto->connmgr, rule, event_type,
5463 op->reason, abbrev_ofconn, abbrev_xid);
5466 rule->pending = NULL;
5469 case OFOPERATION_ADD:
5473 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
5474 if (vid_mask == VLAN_VID_MASK) {
5475 if (ofproto->vlan_bitmap) {
5476 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
5477 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
5478 bitmap_set1(ofproto->vlan_bitmap, vid);
5479 ofproto->vlans_changed = true;
5482 ofproto->vlans_changed = true;
5486 ovs_rwlock_wrlock(&rule->rwlock);
5487 oftable_remove_rule(rule);
5488 ofproto_rule_destroy__(rule);
5492 case OFOPERATION_DELETE:
5493 ovs_assert(!op->error);
5494 ofproto_rule_destroy__(rule);
5498 case OFOPERATION_MODIFY:
5499 case OFOPERATION_REPLACE:
5501 long long int now = time_msec();
5503 rule->modified = now;
5504 if (op->type == OFOPERATION_REPLACE) {
5505 rule->created = rule->used = now;
5508 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
5509 ovs_mutex_lock(&rule->timeout_mutex);
5510 rule->idle_timeout = op->idle_timeout;
5511 rule->hard_timeout = op->hard_timeout;
5512 ovs_mutex_unlock(&rule->timeout_mutex);
5514 free(rule->ofpacts);
5516 ovs_rwlock_wrlock(&rule->rwlock);
5517 rule->ofpacts = op->ofpacts;
5518 rule->ofpacts_len = op->ofpacts_len;
5519 ovs_rwlock_unlock(&rule->rwlock);
5522 op->ofpacts_len = 0;
5524 rule->send_flow_removed = op->send_flow_removed;
5532 ofoperation_destroy(op);
5535 ofmonitor_flush(ofproto->connmgr);
5537 if (!list_is_empty(&group->ofproto_node)) {
5538 ovs_assert(ofproto->n_pending > 0);
5539 ofproto->n_pending--;
5540 list_remove(&group->ofproto_node);
5542 if (!list_is_empty(&group->ofconn_node)) {
5543 list_remove(&group->ofconn_node);
5545 ofconn_send_error(group->ofconn, group->request, error);
5547 connmgr_retry(ofproto->connmgr);
5549 free(group->request);
5553 /* Initiates a new operation on 'rule', of the specified 'type', within
5554 * 'group'. Prior to calling, 'rule' must not have any pending operation.
5556 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
5557 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
5559 * Returns the newly created ofoperation (which is also available as
5560 * rule->pending). */
5561 static struct ofoperation *
5562 ofoperation_create(struct ofopgroup *group, struct rule *rule,
5563 enum ofoperation_type type,
5564 enum ofp_flow_removed_reason reason)
5566 struct ofproto *ofproto = group->ofproto;
5567 struct ofoperation *op;
5569 ovs_assert(!rule->pending);
5571 op = rule->pending = xzalloc(sizeof *op);
5573 list_push_back(&group->ops, &op->group_node);
5576 op->reason = reason;
5577 op->flow_cookie = rule->flow_cookie;
5578 ovs_mutex_lock(&rule->timeout_mutex);
5579 op->idle_timeout = rule->idle_timeout;
5580 op->hard_timeout = rule->hard_timeout;
5581 ovs_mutex_unlock(&rule->timeout_mutex);
5582 op->send_flow_removed = rule->send_flow_removed;
5586 if (type == OFOPERATION_DELETE) {
5587 hmap_insert(&ofproto->deletions, &op->hmap_node,
5588 cls_rule_hash(&rule->cr, rule->table_id));
5595 ofoperation_destroy(struct ofoperation *op)
5597 struct ofopgroup *group = op->group;
5600 op->rule->pending = NULL;
5602 if (op->type == OFOPERATION_DELETE) {
5603 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
5605 list_remove(&op->group_node);
5610 /* Indicates that 'op' completed with status 'error', which is either 0 to
5611 * indicate success or an OpenFlow error code on failure.
5613 * If 'error' is 0, indicating success, the operation will be committed
5614 * permanently to the flow table.
5616 * If 'error' is nonzero, then generally the operation will be rolled back:
5618 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
5619 * restores the original rule. The caller must have uninitialized any
5620 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
5621 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
5622 * and 7 for the new rule, calling its ->rule_dealloc() function.
5624 * - If 'op' is a "modify flow" operation, ofproto restores the original
5627 * - 'op' must not be a "delete flow" operation. Removing a rule is not
5628 * allowed to fail. It must always succeed.
5630 * Please see the large comment in ofproto/ofproto-provider.h titled
5631 * "Asynchronous Operation Support" for more information. */
5633 ofoperation_complete(struct ofoperation *op, enum ofperr error)
5635 struct ofopgroup *group = op->group;
5637 ovs_assert(op->rule->pending == op);
5638 ovs_assert(group->n_running > 0);
5639 ovs_assert(!error || op->type != OFOPERATION_DELETE);
5642 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
5643 ofopgroup_complete(group);
5648 pick_datapath_id(const struct ofproto *ofproto)
5650 const struct ofport *port;
5652 port = ofproto_get_port(ofproto, OFPP_LOCAL);
5654 uint8_t ea[ETH_ADDR_LEN];
5657 error = netdev_get_etheraddr(port->netdev, ea);
5659 return eth_addr_to_uint64(ea);
5661 VLOG_WARN("%s: could not get MAC address for %s (%s)",
5662 ofproto->name, netdev_get_name(port->netdev),
5663 ovs_strerror(error));
5665 return ofproto->fallback_dpid;
5669 pick_fallback_dpid(void)
5671 uint8_t ea[ETH_ADDR_LEN];
5672 eth_addr_nicira_random(ea);
5673 return eth_addr_to_uint64(ea);
5676 /* Table overflow policy. */
5678 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
5679 * to NULL if the table is not configured to evict rules or if the table
5680 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
5681 * or with no timeouts are not evictable.) */
5683 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
5685 struct eviction_group *evg;
5688 if (!table->eviction_fields) {
5692 /* In the common case, the outer and inner loops here will each be entered
5695 * - The inner loop normally "return"s in its first iteration. If the
5696 * eviction group has any evictable rules, then it always returns in
5699 * - The outer loop only iterates more than once if the largest eviction
5700 * group has no evictable rules.
5702 * - The outer loop can exit only if table's 'max_flows' is all filled up
5703 * by unevictable rules. */
5704 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
5707 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
5708 if (!ovs_rwlock_trywrlock(&rule->rwlock)) {
5718 /* Searches 'ofproto' for tables that have more flows than their configured
5719 * maximum and that have flow eviction enabled, and evicts as many flows as
5720 * necessary and currently feasible from them.
5722 * This triggers only when an OpenFlow table has N flows in it and then the
5723 * client configures a maximum number of flows less than N. */
5725 ofproto_evict(struct ofproto *ofproto)
5727 struct ofopgroup *group;
5728 struct oftable *table;
5730 group = ofopgroup_create_unattached(ofproto);
5731 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
5732 while (table->eviction_fields) {
5736 ovs_rwlock_rdlock(&table->cls.rwlock);
5737 n_rules = classifier_count(&table->cls);
5738 ovs_rwlock_unlock(&table->cls.rwlock);
5740 if (n_rules <= table->max_flows) {
5744 if (!choose_rule_to_evict(table, &rule)) {
5748 if (rule->pending) {
5749 ovs_rwlock_unlock(&rule->rwlock);
5753 ofoperation_create(group, rule,
5754 OFOPERATION_DELETE, OFPRR_EVICTION);
5755 oftable_remove_rule(rule);
5756 ofproto->ofproto_class->rule_delete(rule);
5759 ofopgroup_submit(group);
5762 /* Eviction groups. */
5764 /* Returns the priority to use for an eviction_group that contains 'n_rules'
5765 * rules. The priority contains low-order random bits to ensure that eviction
5766 * groups with the same number of rules are prioritized randomly. */
5768 eviction_group_priority(size_t n_rules)
5770 uint16_t size = MIN(UINT16_MAX, n_rules);
5771 return (size << 16) | random_uint16();
5774 /* Updates 'evg', an eviction_group within 'table', following a change that
5775 * adds or removes rules in 'evg'. */
5777 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
5779 heap_change(&table->eviction_groups_by_size, &evg->size_node,
5780 eviction_group_priority(heap_count(&evg->rules)));
5783 /* Destroys 'evg', an eviction_group within 'table':
5785 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
5786 * rules themselves, just removes them from the eviction group.)
5788 * - Removes 'evg' from 'table'.
5792 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
5794 while (!heap_is_empty(&evg->rules)) {
5797 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
5798 rule->eviction_group = NULL;
5800 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
5801 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
5802 heap_destroy(&evg->rules);
5806 /* Removes 'rule' from its eviction group, if any. */
5808 eviction_group_remove_rule(struct rule *rule)
5810 if (rule->eviction_group) {
5811 struct oftable *table = &rule->ofproto->tables[rule->table_id];
5812 struct eviction_group *evg = rule->eviction_group;
5814 rule->eviction_group = NULL;
5815 heap_remove(&evg->rules, &rule->evg_node);
5816 if (heap_is_empty(&evg->rules)) {
5817 eviction_group_destroy(table, evg);
5819 eviction_group_resized(table, evg);
5824 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
5825 * returns the hash value. */
5827 eviction_group_hash_rule(struct rule *rule)
5829 struct oftable *table = &rule->ofproto->tables[rule->table_id];
5830 const struct mf_subfield *sf;
5834 hash = table->eviction_group_id_basis;
5835 miniflow_expand(&rule->cr.match.flow, &flow);
5836 for (sf = table->eviction_fields;
5837 sf < &table->eviction_fields[table->n_eviction_fields];
5840 if (mf_are_prereqs_ok(sf->field, &flow)) {
5841 union mf_value value;
5843 mf_get_value(sf->field, &flow, &value);
5845 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
5847 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
5848 unsigned int start = sf->ofs + sf->n_bits;
5849 bitwise_zero(&value, sf->field->n_bytes, start,
5850 sf->field->n_bytes * 8 - start);
5852 hash = hash_bytes(&value, sf->field->n_bytes, hash);
5854 hash = hash_int(hash, 0);
5861 /* Returns an eviction group within 'table' with the given 'id', creating one
5863 static struct eviction_group *
5864 eviction_group_find(struct oftable *table, uint32_t id)
5866 struct eviction_group *evg;
5868 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
5872 evg = xmalloc(sizeof *evg);
5873 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
5874 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
5875 eviction_group_priority(0));
5876 heap_init(&evg->rules);
5881 /* Returns an eviction priority for 'rule'. The return value should be
5882 * interpreted so that higher priorities make a rule more attractive candidates
5885 rule_eviction_priority(struct rule *rule)
5887 long long int hard_expiration;
5888 long long int idle_expiration;
5889 long long int expiration;
5890 uint32_t expiration_offset;
5892 /* Calculate time of expiration. */
5893 ovs_mutex_lock(&rule->timeout_mutex);
5894 hard_expiration = (rule->hard_timeout
5895 ? rule->modified + rule->hard_timeout * 1000
5897 idle_expiration = (rule->idle_timeout
5898 ? rule->used + rule->idle_timeout * 1000
5900 expiration = MIN(hard_expiration, idle_expiration);
5901 ovs_mutex_unlock(&rule->timeout_mutex);
5902 if (expiration == LLONG_MAX) {
5906 /* Calculate the time of expiration as a number of (approximate) seconds
5907 * after program startup.
5909 * This should work OK for program runs that last UINT32_MAX seconds or
5910 * less. Therefore, please restart OVS at least once every 136 years. */
5911 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
5913 /* Invert the expiration offset because we're using a max-heap. */
5914 return UINT32_MAX - expiration_offset;
5917 /* Adds 'rule' to an appropriate eviction group for its oftable's
5918 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
5919 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
5922 * The caller must ensure that 'rule' is not already in an eviction group. */
5924 eviction_group_add_rule(struct rule *rule)
5926 struct ofproto *ofproto = rule->ofproto;
5927 struct oftable *table = &ofproto->tables[rule->table_id];
5930 ovs_mutex_lock(&rule->timeout_mutex);
5931 has_timeout = rule->hard_timeout || rule->idle_timeout;
5932 ovs_mutex_unlock(&rule->timeout_mutex);
5934 if (table->eviction_fields && has_timeout) {
5935 struct eviction_group *evg;
5937 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
5939 rule->eviction_group = evg;
5940 heap_insert(&evg->rules, &rule->evg_node,
5941 rule_eviction_priority(rule));
5942 eviction_group_resized(table, evg);
5948 /* Initializes 'table'. */
5950 oftable_init(struct oftable *table)
5952 memset(table, 0, sizeof *table);
5953 classifier_init(&table->cls);
5954 table->max_flows = UINT_MAX;
5957 /* Destroys 'table', including its classifier and eviction groups.
5959 * The caller is responsible for freeing 'table' itself. */
5961 oftable_destroy(struct oftable *table)
5963 ovs_rwlock_rdlock(&table->cls.rwlock);
5964 ovs_assert(classifier_is_empty(&table->cls));
5965 ovs_rwlock_unlock(&table->cls.rwlock);
5966 oftable_disable_eviction(table);
5967 classifier_destroy(&table->cls);
5971 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
5972 * string, then 'table' will use its default name.
5974 * This only affects the name exposed for a table exposed through the OpenFlow
5975 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
5977 oftable_set_name(struct oftable *table, const char *name)
5979 if (name && name[0]) {
5980 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
5981 if (!table->name || strncmp(name, table->name, len)) {
5983 table->name = xmemdup0(name, len);
5991 /* oftables support a choice of two policies when adding a rule would cause the
5992 * number of flows in the table to exceed the configured maximum number: either
5993 * they can refuse to add the new flow or they can evict some existing flow.
5994 * This function configures the former policy on 'table'. */
5996 oftable_disable_eviction(struct oftable *table)
5998 if (table->eviction_fields) {
5999 struct eviction_group *evg, *next;
6001 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6002 &table->eviction_groups_by_id) {
6003 eviction_group_destroy(table, evg);
6005 hmap_destroy(&table->eviction_groups_by_id);
6006 heap_destroy(&table->eviction_groups_by_size);
6008 free(table->eviction_fields);
6009 table->eviction_fields = NULL;
6010 table->n_eviction_fields = 0;
6014 /* oftables support a choice of two policies when adding a rule would cause the
6015 * number of flows in the table to exceed the configured maximum number: either
6016 * they can refuse to add the new flow or they can evict some existing flow.
6017 * This function configures the latter policy on 'table', with fairness based
6018 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6019 * 'n_fields' as 0 disables fairness.) */
6021 oftable_enable_eviction(struct oftable *table,
6022 const struct mf_subfield *fields, size_t n_fields)
6024 struct cls_cursor cursor;
6027 if (table->eviction_fields
6028 && n_fields == table->n_eviction_fields
6030 || !memcmp(fields, table->eviction_fields,
6031 n_fields * sizeof *fields))) {
6036 oftable_disable_eviction(table);
6038 table->n_eviction_fields = n_fields;
6039 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6041 table->eviction_group_id_basis = random_uint32();
6042 hmap_init(&table->eviction_groups_by_id);
6043 heap_init(&table->eviction_groups_by_size);
6045 ovs_rwlock_rdlock(&table->cls.rwlock);
6046 cls_cursor_init(&cursor, &table->cls, NULL);
6047 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6048 eviction_group_add_rule(rule);
6050 ovs_rwlock_unlock(&table->cls.rwlock);
6053 /* Removes 'rule' from the oftable that contains it. */
6055 oftable_remove_rule__(struct ofproto *ofproto, struct classifier *cls,
6057 OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->rwlock)
6059 classifier_remove(cls, &rule->cr);
6060 cookies_remove(ofproto, rule);
6061 eviction_group_remove_rule(rule);
6062 ovs_mutex_lock(&ofproto->expirable_mutex);
6063 if (!list_is_empty(&rule->expirable)) {
6064 list_remove(&rule->expirable);
6066 ovs_mutex_unlock(&ofproto->expirable_mutex);
6067 if (!list_is_empty(&rule->meter_list_node)) {
6068 list_remove(&rule->meter_list_node);
6069 list_init(&rule->meter_list_node);
6071 ovs_rwlock_unlock(&rule->rwlock);
6075 oftable_remove_rule(struct rule *rule)
6077 struct ofproto *ofproto = rule->ofproto;
6078 struct oftable *table = &ofproto->tables[rule->table_id];
6080 ovs_rwlock_wrlock(&table->cls.rwlock);
6081 oftable_remove_rule__(ofproto, &table->cls, rule);
6082 ovs_rwlock_unlock(&table->cls.rwlock);
6085 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6086 * the same cls_rule. */
6088 oftable_insert_rule(struct rule *rule)
6090 struct ofproto *ofproto = rule->ofproto;
6091 struct oftable *table = &ofproto->tables[rule->table_id];
6094 ovs_mutex_lock(&rule->timeout_mutex);
6095 may_expire = rule->hard_timeout || rule->idle_timeout;
6096 ovs_mutex_unlock(&rule->timeout_mutex);
6099 ovs_mutex_lock(&ofproto->expirable_mutex);
6100 list_insert(&ofproto->expirable, &rule->expirable);
6101 ovs_mutex_unlock(&ofproto->expirable_mutex);
6103 cookies_insert(ofproto, rule);
6104 if (rule->meter_id) {
6105 struct meter *meter = ofproto->meters[rule->meter_id];
6106 list_insert(&meter->rules, &rule->meter_list_node);
6108 ovs_rwlock_wrlock(&table->cls.rwlock);
6109 classifier_insert(&table->cls, &rule->cr);
6110 ovs_rwlock_unlock(&table->cls.rwlock);
6111 eviction_group_add_rule(rule);
6114 /* unixctl commands. */
6117 ofproto_lookup(const char *name)
6119 struct ofproto *ofproto;
6121 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6123 if (!strcmp(ofproto->name, name)) {
6131 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6132 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6134 struct ofproto *ofproto;
6138 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6139 ds_put_format(&results, "%s\n", ofproto->name);
6141 unixctl_command_reply(conn, ds_cstr(&results));
6142 ds_destroy(&results);
6146 ofproto_unixctl_init(void)
6148 static bool registered;
6154 unixctl_command_register("ofproto/list", "", 0, 0,
6155 ofproto_unixctl_list, NULL);
6158 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6160 * This is deprecated. It is only for compatibility with broken device drivers
6161 * in old versions of Linux that do not properly support VLANs when VLAN
6162 * devices are not used. When broken device drivers are no longer in
6163 * widespread use, we will delete these interfaces. */
6165 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6166 * (exactly) by an OpenFlow rule in 'ofproto'. */
6168 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6170 const struct oftable *oftable;
6172 free(ofproto->vlan_bitmap);
6173 ofproto->vlan_bitmap = bitmap_allocate(4096);
6174 ofproto->vlans_changed = false;
6176 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6177 const struct cls_table *table;
6179 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.tables) {
6180 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6181 const struct cls_rule *rule;
6183 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6184 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6185 bitmap_set1(vlan_bitmap, vid);
6186 bitmap_set1(ofproto->vlan_bitmap, vid);
6193 /* Returns true if new VLANs have come into use by the flow table since the
6194 * last call to ofproto_get_vlan_usage().
6196 * We don't track when old VLANs stop being used. */
6198 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6200 return ofproto->vlans_changed;
6203 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6204 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6205 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6206 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6207 * then the VLAN device is un-enslaved. */
6209 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6210 ofp_port_t realdev_ofp_port, int vid)
6212 struct ofport *ofport;
6215 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6217 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6219 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6220 ofproto->name, vlandev_ofp_port);
6224 if (!ofproto->ofproto_class->set_realdev) {
6225 if (!vlandev_ofp_port) {
6228 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6232 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6234 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6235 ofproto->name, vlandev_ofp_port,
6236 netdev_get_name(ofport->netdev), ovs_strerror(error));