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 enum ofputil_flow_mod_flags flags; /* Rule's old flags. */
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 *);
193 /* Criteria that flow_mod and other operations use for selecting rules on
194 * which to operate. */
195 struct rule_criteria {
196 /* An OpenFlow table or 255 for all tables. */
199 /* OpenFlow matching criteria. Interpreted different in "loose" way by
200 * collect_rules_loose() and "strict" way by collect_rules_strict(), as
201 * defined in the OpenFlow spec. */
204 /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's
205 * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'.
206 * The rule will not be selected if M is 1 and B != C. */
208 ovs_be64 cookie_mask;
210 /* Selection based on actions within a rule:
212 * If out_port != OFPP_ANY, selects only rules that output to out_port.
213 * If out_group != OFPG_ALL, select only rules that output to out_group. */
218 static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
219 const struct match *match,
220 unsigned int priority,
221 ovs_be64 cookie, ovs_be64 cookie_mask,
222 ofp_port_t out_port, uint32_t out_group);
223 static void rule_criteria_destroy(struct rule_criteria *);
226 static void ofport_destroy__(struct ofport *);
227 static void ofport_destroy(struct ofport *);
229 static void update_port(struct ofproto *, const char *devname);
230 static int init_ports(struct ofproto *);
231 static void reinit_ports(struct ofproto *);
234 static void ofproto_rule_destroy(struct rule *);
235 static void ofproto_rule_destroy__(struct rule *);
236 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
237 static bool rule_is_modifiable(const struct rule *);
240 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
241 struct ofputil_flow_mod *,
242 const struct ofp_header *);
243 static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
244 struct ofputil_flow_mod *,
245 const struct ofp_header *,
246 const struct rule_collection *);
247 static void delete_flow__(struct rule *rule, struct ofopgroup *,
248 enum ofp_flow_removed_reason)
249 OVS_RELEASES(rule->rwlock);
250 static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
251 static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
252 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
253 struct ofputil_flow_mod *,
254 const struct ofp_header *);
255 static void calc_duration(long long int start, long long int now,
256 uint32_t *sec, uint32_t *nsec);
259 static uint64_t pick_datapath_id(const struct ofproto *);
260 static uint64_t pick_fallback_dpid(void);
261 static void ofproto_destroy__(struct ofproto *);
262 static void update_mtu(struct ofproto *, struct ofport *);
263 static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
266 static void ofproto_unixctl_init(void);
268 /* All registered ofproto classes, in probe order. */
269 static const struct ofproto_class **ofproto_classes;
270 static size_t n_ofproto_classes;
271 static size_t allocated_ofproto_classes;
273 unsigned flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT;
274 unsigned n_handler_threads;
275 enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO;
277 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
278 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
280 /* Initial mappings of port to OpenFlow number mappings. */
281 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
283 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
285 /* The default value of true waits for flow restore. */
286 static bool flow_restore_wait = true;
288 /* Must be called to initialize the ofproto library.
290 * The caller may pass in 'iface_hints', which contains an shash of
291 * "iface_hint" elements indexed by the interface's name. The provider
292 * may use these hints to describe the startup configuration in order to
293 * reinitialize its state. The caller owns the provided data, so a
294 * provider will make copies of anything required. An ofproto provider
295 * will remove any existing state that is not described by the hint, and
296 * may choose to remove it all. */
298 ofproto_init(const struct shash *iface_hints)
300 struct shash_node *node;
303 ofproto_class_register(&ofproto_dpif_class);
305 /* Make a local copy, since we don't own 'iface_hints' elements. */
306 SHASH_FOR_EACH(node, iface_hints) {
307 const struct iface_hint *orig_hint = node->data;
308 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
309 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
311 new_hint->br_name = xstrdup(orig_hint->br_name);
312 new_hint->br_type = xstrdup(br_type);
313 new_hint->ofp_port = orig_hint->ofp_port;
315 shash_add(&init_ofp_ports, node->name, new_hint);
318 for (i = 0; i < n_ofproto_classes; i++) {
319 ofproto_classes[i]->init(&init_ofp_ports);
323 /* 'type' should be a normalized datapath type, as returned by
324 * ofproto_normalize_type(). Returns the corresponding ofproto_class
325 * structure, or a null pointer if there is none registered for 'type'. */
326 static const struct ofproto_class *
327 ofproto_class_find__(const char *type)
331 for (i = 0; i < n_ofproto_classes; i++) {
332 const struct ofproto_class *class = ofproto_classes[i];
337 class->enumerate_types(&types);
338 found = sset_contains(&types, type);
339 sset_destroy(&types);
345 VLOG_WARN("unknown datapath type %s", type);
349 /* Registers a new ofproto class. After successful registration, new ofprotos
350 * of that type can be created using ofproto_create(). */
352 ofproto_class_register(const struct ofproto_class *new_class)
356 for (i = 0; i < n_ofproto_classes; i++) {
357 if (ofproto_classes[i] == new_class) {
362 if (n_ofproto_classes >= allocated_ofproto_classes) {
363 ofproto_classes = x2nrealloc(ofproto_classes,
364 &allocated_ofproto_classes,
365 sizeof *ofproto_classes);
367 ofproto_classes[n_ofproto_classes++] = new_class;
371 /* Unregisters a datapath provider. 'type' must have been previously
372 * registered and not currently be in use by any ofprotos. After
373 * unregistration new datapaths of that type cannot be opened using
374 * ofproto_create(). */
376 ofproto_class_unregister(const struct ofproto_class *class)
380 for (i = 0; i < n_ofproto_classes; i++) {
381 if (ofproto_classes[i] == class) {
382 for (i++; i < n_ofproto_classes; i++) {
383 ofproto_classes[i - 1] = ofproto_classes[i];
389 VLOG_WARN("attempted to unregister an ofproto class that is not "
394 /* Clears 'types' and enumerates all registered ofproto types into it. The
395 * caller must first initialize the sset. */
397 ofproto_enumerate_types(struct sset *types)
401 for (i = 0; i < n_ofproto_classes; i++) {
402 ofproto_classes[i]->enumerate_types(types);
406 /* Returns the fully spelled out name for the given ofproto 'type'.
408 * Normalized type string can be compared with strcmp(). Unnormalized type
409 * string might be the same even if they have different spellings. */
411 ofproto_normalize_type(const char *type)
413 return type && type[0] ? type : "system";
416 /* Clears 'names' and enumerates the names of all known created ofprotos with
417 * the given 'type'. The caller must first initialize the sset. Returns 0 if
418 * successful, otherwise a positive errno value.
420 * Some kinds of datapaths might not be practically enumerable. This is not
421 * considered an error. */
423 ofproto_enumerate_names(const char *type, struct sset *names)
425 const struct ofproto_class *class = ofproto_class_find__(type);
426 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
430 ofproto_create(const char *datapath_name, const char *datapath_type,
431 struct ofproto **ofprotop)
433 const struct ofproto_class *class;
434 struct ofproto *ofproto;
440 ofproto_unixctl_init();
442 datapath_type = ofproto_normalize_type(datapath_type);
443 class = ofproto_class_find__(datapath_type);
445 VLOG_WARN("could not create datapath %s of unknown type %s",
446 datapath_name, datapath_type);
450 ofproto = class->alloc();
452 VLOG_ERR("failed to allocate datapath %s of type %s",
453 datapath_name, datapath_type);
458 memset(ofproto, 0, sizeof *ofproto);
459 ofproto->ofproto_class = class;
460 ofproto->name = xstrdup(datapath_name);
461 ofproto->type = xstrdup(datapath_type);
462 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
463 hash_string(ofproto->name, 0));
464 ofproto->datapath_id = 0;
465 ofproto->forward_bpdu = false;
466 ofproto->fallback_dpid = pick_fallback_dpid();
467 ofproto->mfr_desc = NULL;
468 ofproto->hw_desc = NULL;
469 ofproto->sw_desc = NULL;
470 ofproto->serial_desc = NULL;
471 ofproto->dp_desc = NULL;
472 ofproto->frag_handling = OFPC_FRAG_NORMAL;
473 hmap_init(&ofproto->ports);
474 shash_init(&ofproto->port_by_name);
475 simap_init(&ofproto->ofp_requests);
476 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
477 ofproto->eviction_group_timer = LLONG_MIN;
478 ofproto->tables = NULL;
479 ofproto->n_tables = 0;
480 hindex_init(&ofproto->cookies);
481 list_init(&ofproto->expirable);
482 ovs_mutex_init_recursive(&ofproto->expirable_mutex);
483 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
484 ofproto->state = S_OPENFLOW;
485 list_init(&ofproto->pending);
486 ofproto->n_pending = 0;
487 hmap_init(&ofproto->deletions);
488 ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0;
489 ofproto->first_op = ofproto->last_op = LLONG_MIN;
490 ofproto->next_op_report = LLONG_MAX;
491 ofproto->op_backoff = LLONG_MIN;
492 ofproto->vlan_bitmap = NULL;
493 ofproto->vlans_changed = false;
494 ofproto->min_mtu = INT_MAX;
495 ovs_rwlock_init(&ofproto->groups_rwlock);
496 hmap_init(&ofproto->groups);
498 error = ofproto->ofproto_class->construct(ofproto);
500 VLOG_ERR("failed to open datapath %s: %s",
501 datapath_name, ovs_strerror(error));
502 ofproto_destroy__(ofproto);
506 /* The "max_ports" member should have been set by ->construct(ofproto).
507 * Port 0 is not a valid OpenFlow port, so mark that as unavailable. */
508 ofproto->ofp_port_ids = bitmap_allocate(ofproto->max_ports);
509 bitmap_set1(ofproto->ofp_port_ids, 0);
511 /* Check that hidden tables, if any, are at the end. */
512 ovs_assert(ofproto->n_tables);
513 for (i = 0; i + 1 < ofproto->n_tables; i++) {
514 enum oftable_flags flags = ofproto->tables[i].flags;
515 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
517 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
520 ofproto->datapath_id = pick_datapath_id(ofproto);
523 /* Initialize meters table. */
524 if (ofproto->ofproto_class->meter_get_features) {
525 ofproto->ofproto_class->meter_get_features(ofproto,
526 &ofproto->meter_features);
528 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
530 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
531 * sizeof(struct meter *));
537 /* Must be called (only) by an ofproto implementation in its constructor
538 * function. See the large comment on 'construct' in struct ofproto_class for
541 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
543 struct oftable *table;
545 ovs_assert(!ofproto->n_tables);
546 ovs_assert(n_tables >= 1 && n_tables <= 255);
548 ofproto->n_tables = n_tables;
549 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
550 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
555 /* To be optionally called (only) by an ofproto implementation in its
556 * constructor function. See the large comment on 'construct' in struct
557 * ofproto_class for details.
559 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
560 * will then ensure that actions passed into the ofproto implementation will
561 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
562 * function is not called, there will be no such restriction.
564 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
565 * the 'max_ports' restriction. */
567 ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
569 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
570 ofproto->max_ports = max_ports;
574 ofproto_get_datapath_id(const struct ofproto *ofproto)
576 return ofproto->datapath_id;
580 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
582 uint64_t old_dpid = p->datapath_id;
583 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
584 if (p->datapath_id != old_dpid) {
585 /* Force all active connections to reconnect, since there is no way to
586 * notify a controller that the datapath ID has changed. */
587 ofproto_reconnect_controllers(p);
592 ofproto_set_controllers(struct ofproto *p,
593 const struct ofproto_controller *controllers,
594 size_t n_controllers, uint32_t allowed_versions)
596 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
601 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
603 connmgr_set_fail_mode(p->connmgr, fail_mode);
606 /* Drops the connections between 'ofproto' and all of its controllers, forcing
607 * them to reconnect. */
609 ofproto_reconnect_controllers(struct ofproto *ofproto)
611 connmgr_reconnect(ofproto->connmgr);
614 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
615 * in-band control should guarantee access, in the same way that in-band
616 * control guarantees access to OpenFlow controllers. */
618 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
619 const struct sockaddr_in *extras, size_t n)
621 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
624 /* Sets the OpenFlow queue used by flows set up by in-band control on
625 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
626 * flows will use the default queue. */
628 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
630 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
633 /* Sets the number of flows at which eviction from the kernel flow table
636 ofproto_set_flow_eviction_threshold(unsigned threshold)
638 flow_eviction_threshold = MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN,
642 /* Sets the path for handling flow misses. */
644 ofproto_set_flow_miss_model(unsigned model)
646 flow_miss_model = model;
649 /* If forward_bpdu is true, the NORMAL action will forward frames with
650 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
651 * the NORMAL action will drop these frames. */
653 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
655 bool old_val = ofproto->forward_bpdu;
656 ofproto->forward_bpdu = forward_bpdu;
657 if (old_val != ofproto->forward_bpdu) {
658 if (ofproto->ofproto_class->forward_bpdu_changed) {
659 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
664 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
665 * 'idle_time', in seconds, and the maximum number of MAC table entries to
668 ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
671 if (ofproto->ofproto_class->set_mac_table_config) {
672 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
677 /* Sets number of upcall handler threads. The default is
678 * (number of online cores - 2). */
680 ofproto_set_n_handler_threads(unsigned limit)
683 n_handler_threads = limit;
685 int n_proc = sysconf(_SC_NPROCESSORS_ONLN);
686 n_handler_threads = n_proc > 2 ? n_proc - 2 : 1;
691 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
694 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
698 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
700 return connmgr_set_snoops(ofproto->connmgr, snoops);
704 ofproto_set_netflow(struct ofproto *ofproto,
705 const struct netflow_options *nf_options)
707 if (nf_options && sset_is_empty(&nf_options->collectors)) {
711 if (ofproto->ofproto_class->set_netflow) {
712 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
714 return nf_options ? EOPNOTSUPP : 0;
719 ofproto_set_sflow(struct ofproto *ofproto,
720 const struct ofproto_sflow_options *oso)
722 if (oso && sset_is_empty(&oso->targets)) {
726 if (ofproto->ofproto_class->set_sflow) {
727 return ofproto->ofproto_class->set_sflow(ofproto, oso);
729 return oso ? EOPNOTSUPP : 0;
734 ofproto_set_ipfix(struct ofproto *ofproto,
735 const struct ofproto_ipfix_bridge_exporter_options *bo,
736 const struct ofproto_ipfix_flow_exporter_options *fo,
739 if (ofproto->ofproto_class->set_ipfix) {
740 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
742 return (bo || fo) ? EOPNOTSUPP : 0;
747 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
749 flow_restore_wait = flow_restore_wait_db;
753 ofproto_get_flow_restore_wait(void)
755 return flow_restore_wait;
759 /* Spanning Tree Protocol (STP) configuration. */
761 /* Configures STP on 'ofproto' using the settings defined in 's'. If
762 * 's' is NULL, disables STP.
764 * Returns 0 if successful, otherwise a positive errno value. */
766 ofproto_set_stp(struct ofproto *ofproto,
767 const struct ofproto_stp_settings *s)
769 return (ofproto->ofproto_class->set_stp
770 ? ofproto->ofproto_class->set_stp(ofproto, s)
774 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
775 * 'enabled' member of 's' is false, then the other members are not
778 * Returns 0 if successful, otherwise a positive errno value. */
780 ofproto_get_stp_status(struct ofproto *ofproto,
781 struct ofproto_stp_status *s)
783 return (ofproto->ofproto_class->get_stp_status
784 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
788 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
789 * in 's'. The caller is responsible for assigning STP port numbers
790 * (using the 'port_num' member in the range of 1 through 255, inclusive)
791 * and ensuring there are no duplicates. If the 's' is NULL, then STP
792 * is disabled on the port.
794 * Returns 0 if successful, otherwise a positive errno value.*/
796 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
797 const struct ofproto_port_stp_settings *s)
799 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
801 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
802 ofproto->name, ofp_port);
806 return (ofproto->ofproto_class->set_stp_port
807 ? ofproto->ofproto_class->set_stp_port(ofport, s)
811 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
812 * 's'. If the 'enabled' member in 's' is false, then the other members
813 * are not meaningful.
815 * Returns 0 if successful, otherwise a positive errno value.*/
817 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
818 struct ofproto_port_stp_status *s)
820 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
822 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
823 "port %"PRIu16, ofproto->name, ofp_port);
827 return (ofproto->ofproto_class->get_stp_port_status
828 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
832 /* Queue DSCP configuration. */
834 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
835 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
836 * to implement QoS. Instead, it is used to implement features which require
837 * knowledge of what queues exist on a port, and some basic information about
840 * Returns 0 if successful, otherwise a positive errno value. */
842 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
843 const struct ofproto_port_queue *queues,
846 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
849 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
850 ofproto->name, ofp_port);
854 return (ofproto->ofproto_class->set_queues
855 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
859 /* Connectivity Fault Management configuration. */
861 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
863 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
865 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
866 if (ofport && ofproto->ofproto_class->set_cfm) {
867 ofproto->ofproto_class->set_cfm(ofport, NULL);
871 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
872 * basic configuration from the configuration members in 'cfm', and the remote
873 * maintenance point ID from remote_mpid. Ignores the statistics members of
876 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
878 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
879 const struct cfm_settings *s)
881 struct ofport *ofport;
884 ofport = ofproto_get_port(ofproto, ofp_port);
886 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
887 ofproto->name, ofp_port);
891 /* XXX: For configuration simplicity, we only support one remote_mpid
892 * outside of the CFM module. It's not clear if this is the correct long
893 * term solution or not. */
894 error = (ofproto->ofproto_class->set_cfm
895 ? ofproto->ofproto_class->set_cfm(ofport, s)
898 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
899 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
900 ovs_strerror(error));
904 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
905 * 'ofproto' does not have a port 'ofp_port'. */
907 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
908 const struct smap *cfg)
910 struct ofport *ofport;
913 ofport = ofproto_get_port(ofproto, ofp_port);
915 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
916 ofproto->name, ofp_port);
920 error = (ofproto->ofproto_class->set_bfd
921 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
924 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
925 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
926 ovs_strerror(error));
930 /* Populates 'status' with key value pairs indicating the status of the BFD
931 * session on 'ofp_port'. This information is intended to be populated in the
932 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
935 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
938 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
939 return (ofport && ofproto->ofproto_class->get_bfd_status
940 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
944 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
945 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
946 * 0 if LACP partner information is not current (generally indicating a
947 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
949 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
951 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
952 return (ofport && ofproto->ofproto_class->port_is_lacp_current
953 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
959 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
960 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
961 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
962 * configuration plus, if there is more than one slave, a bonding
965 * If 'aux' is already registered then this function updates its configuration
966 * to 's'. Otherwise, this function registers a new bundle.
968 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
971 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
972 const struct ofproto_bundle_settings *s)
974 return (ofproto->ofproto_class->bundle_set
975 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
979 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
980 * If no such bundle has been registered, this has no effect. */
982 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
984 return ofproto_bundle_register(ofproto, aux, NULL);
988 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
989 * If 'aux' is already registered then this function updates its configuration
990 * to 's'. Otherwise, this function registers a new mirror. */
992 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
993 const struct ofproto_mirror_settings *s)
995 return (ofproto->ofproto_class->mirror_set
996 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1000 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1001 * If no mirror has been registered, this has no effect. */
1003 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
1005 return ofproto_mirror_register(ofproto, aux, NULL);
1008 /* Retrieves statistics from mirror associated with client data pointer
1009 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1010 * 'bytes', respectively. If a particular counters is not supported,
1011 * the appropriate argument is set to UINT64_MAX. */
1013 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1014 uint64_t *packets, uint64_t *bytes)
1016 if (!ofproto->ofproto_class->mirror_get_stats) {
1017 *packets = *bytes = UINT64_MAX;
1021 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1025 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1026 * which all packets are flooded, instead of using MAC learning. If
1027 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1029 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1032 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
1034 return (ofproto->ofproto_class->set_flood_vlans
1035 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1039 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1040 * output for a mirror. */
1042 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1044 return (ofproto->ofproto_class->is_mirror_output_bundle
1045 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1049 /* Configuration of OpenFlow tables. */
1051 /* Returns the number of OpenFlow tables in 'ofproto'. */
1053 ofproto_get_n_tables(const struct ofproto *ofproto)
1055 return ofproto->n_tables;
1058 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1059 * settings from 's'. 'table_id' must be in the range 0 through the number of
1060 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1062 * For read-only tables, only the name may be configured. */
1064 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1065 const struct ofproto_table_settings *s)
1067 struct oftable *table;
1069 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1070 table = &ofproto->tables[table_id];
1072 oftable_set_name(table, s->name);
1074 if (table->flags & OFTABLE_READONLY) {
1079 oftable_enable_eviction(table, s->groups, s->n_groups);
1081 oftable_disable_eviction(table);
1084 table->max_flows = s->max_flows;
1085 ovs_rwlock_rdlock(&table->cls.rwlock);
1086 if (classifier_count(&table->cls) > table->max_flows
1087 && table->eviction_fields) {
1088 /* 'table' contains more flows than allowed. We might not be able to
1089 * evict them right away because of the asynchronous nature of flow
1090 * table changes. Schedule eviction for later. */
1091 switch (ofproto->state) {
1093 ofproto->state = S_EVICT;
1097 /* We're already deleting flows, nothing more to do. */
1101 ovs_rwlock_unlock(&table->cls.rwlock);
1105 ofproto_has_snoops(const struct ofproto *ofproto)
1107 return connmgr_has_snoops(ofproto->connmgr);
1111 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1113 connmgr_get_snoops(ofproto->connmgr, snoops);
1116 /* Deletes 'rule' from 'cls' within 'ofproto'.
1118 * Within an ofproto implementation, this function allows an ofproto
1119 * implementation to destroy any rules that remain when its ->destruct()
1120 * function is called. This function is not suitable for use elsewhere in an
1121 * ofproto implementation.
1123 * This function is also used internally in ofproto.c.
1125 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1126 * Cycle" in ofproto-provider.h.
1128 * The 'cls' argument is redundant (it is &ofproto->tables[rule->table_id].cls)
1129 * but it allows Clang to do better checking. */
1131 ofproto_rule_delete(struct ofproto *ofproto, struct classifier *cls,
1133 OVS_REQ_WRLOCK(cls->rwlock)
1135 struct ofopgroup *group;
1137 ovs_assert(!rule->pending);
1138 ovs_assert(cls == &ofproto->tables[rule->table_id].cls);
1140 group = ofopgroup_create_unattached(ofproto);
1141 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1142 ovs_rwlock_wrlock(&rule->rwlock);
1143 oftable_remove_rule__(ofproto, cls, rule);
1144 ofproto->ofproto_class->rule_delete(rule);
1145 ofopgroup_submit(group);
1149 ofproto_flush__(struct ofproto *ofproto)
1151 struct oftable *table;
1153 if (ofproto->ofproto_class->flush) {
1154 ofproto->ofproto_class->flush(ofproto);
1157 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1158 struct rule *rule, *next_rule;
1159 struct cls_cursor cursor;
1161 if (table->flags & OFTABLE_HIDDEN) {
1165 ovs_rwlock_wrlock(&table->cls.rwlock);
1166 cls_cursor_init(&cursor, &table->cls, NULL);
1167 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1168 if (!rule->pending) {
1169 ofproto_rule_delete(ofproto, &table->cls, rule);
1172 ovs_rwlock_unlock(&table->cls.rwlock);
1176 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1179 ofproto_destroy__(struct ofproto *ofproto)
1181 struct oftable *table;
1183 ovs_assert(list_is_empty(&ofproto->pending));
1184 ovs_assert(!ofproto->n_pending);
1186 delete_group(ofproto, OFPG_ALL);
1187 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1188 hmap_destroy(&ofproto->groups);
1190 connmgr_destroy(ofproto->connmgr);
1192 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1193 free(ofproto->name);
1194 free(ofproto->type);
1195 free(ofproto->mfr_desc);
1196 free(ofproto->hw_desc);
1197 free(ofproto->sw_desc);
1198 free(ofproto->serial_desc);
1199 free(ofproto->dp_desc);
1200 hmap_destroy(&ofproto->ports);
1201 shash_destroy(&ofproto->port_by_name);
1202 bitmap_free(ofproto->ofp_port_ids);
1203 simap_destroy(&ofproto->ofp_requests);
1205 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1206 oftable_destroy(table);
1208 free(ofproto->tables);
1210 hmap_destroy(&ofproto->deletions);
1212 free(ofproto->vlan_bitmap);
1214 ovs_mutex_destroy(&ofproto->expirable_mutex);
1215 ofproto->ofproto_class->dealloc(ofproto);
1219 ofproto_destroy(struct ofproto *p)
1221 struct ofport *ofport, *next_ofport;
1228 meter_delete(p, 1, p->meter_features.max_meters);
1229 p->meter_features.max_meters = 0;
1235 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1236 ofport_destroy(ofport);
1239 p->ofproto_class->destruct(p);
1240 ofproto_destroy__(p);
1243 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1244 * kernel datapath, for example, this destroys the datapath in the kernel, and
1245 * with the netdev-based datapath, it tears down the data structures that
1246 * represent the datapath.
1248 * The datapath should not be currently open as an ofproto. */
1250 ofproto_delete(const char *name, const char *type)
1252 const struct ofproto_class *class = ofproto_class_find__(type);
1253 return (!class ? EAFNOSUPPORT
1254 : !class->del ? EACCES
1255 : class->del(type, name));
1259 process_port_change(struct ofproto *ofproto, int error, char *devname)
1261 if (error == ENOBUFS) {
1262 reinit_ports(ofproto);
1263 } else if (!error) {
1264 update_port(ofproto, devname);
1270 ofproto_type_run(const char *datapath_type)
1272 const struct ofproto_class *class;
1275 datapath_type = ofproto_normalize_type(datapath_type);
1276 class = ofproto_class_find__(datapath_type);
1278 error = class->type_run ? class->type_run(datapath_type) : 0;
1279 if (error && error != EAGAIN) {
1280 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1281 datapath_type, ovs_strerror(error));
1287 ofproto_type_run_fast(const char *datapath_type)
1289 const struct ofproto_class *class;
1292 datapath_type = ofproto_normalize_type(datapath_type);
1293 class = ofproto_class_find__(datapath_type);
1295 error = class->type_run_fast ? class->type_run_fast(datapath_type) : 0;
1296 if (error && error != EAGAIN) {
1297 VLOG_ERR_RL(&rl, "%s: type_run_fast failed (%s)",
1298 datapath_type, ovs_strerror(error));
1304 ofproto_type_wait(const char *datapath_type)
1306 const struct ofproto_class *class;
1308 datapath_type = ofproto_normalize_type(datapath_type);
1309 class = ofproto_class_find__(datapath_type);
1311 if (class->type_wait) {
1312 class->type_wait(datapath_type);
1317 ofproto_run(struct ofproto *p)
1319 struct sset changed_netdevs;
1320 const char *changed_netdev;
1321 struct ofport *ofport;
1324 error = p->ofproto_class->run(p);
1325 if (error && error != EAGAIN) {
1326 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1329 /* Restore the eviction group heap invariant occasionally. */
1330 if (p->eviction_group_timer < time_msec()) {
1333 p->eviction_group_timer = time_msec() + 1000;
1335 for (i = 0; i < p->n_tables; i++) {
1336 struct oftable *table = &p->tables[i];
1337 struct eviction_group *evg;
1338 struct cls_cursor cursor;
1341 if (!table->eviction_fields) {
1345 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1346 heap_rebuild(&evg->rules);
1349 ovs_rwlock_rdlock(&table->cls.rwlock);
1350 cls_cursor_init(&cursor, &table->cls, NULL);
1351 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1352 if (!rule->eviction_group
1353 && (rule->idle_timeout || rule->hard_timeout)) {
1354 eviction_group_add_rule(rule);
1357 ovs_rwlock_unlock(&table->cls.rwlock);
1361 if (p->ofproto_class->port_poll) {
1364 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1365 process_port_change(p, error, devname);
1369 /* Update OpenFlow port status for any port whose netdev has changed.
1371 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1372 * destroyed, so it's not safe to update ports directly from the
1373 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1374 * need this two-phase approach. */
1375 sset_init(&changed_netdevs);
1376 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1377 unsigned int change_seq = netdev_change_seq(ofport->netdev);
1378 if (ofport->change_seq != change_seq) {
1379 ofport->change_seq = change_seq;
1380 sset_add(&changed_netdevs, netdev_get_name(ofport->netdev));
1383 SSET_FOR_EACH (changed_netdev, &changed_netdevs) {
1384 update_port(p, changed_netdev);
1386 sset_destroy(&changed_netdevs);
1390 connmgr_run(p->connmgr, handle_openflow);
1394 connmgr_run(p->connmgr, NULL);
1396 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1397 p->state = S_OPENFLOW;
1402 connmgr_run(p->connmgr, NULL);
1404 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1405 connmgr_flushed(p->connmgr);
1406 p->state = S_OPENFLOW;
1414 if (time_msec() >= p->next_op_report) {
1415 long long int ago = (time_msec() - p->first_op) / 1000;
1416 long long int interval = (p->last_op - p->first_op) / 1000;
1420 ds_put_format(&s, "%d flow_mods ",
1421 p->n_add + p->n_delete + p->n_modify);
1422 if (interval == ago) {
1423 ds_put_format(&s, "in the last %lld s", ago);
1424 } else if (interval) {
1425 ds_put_format(&s, "in the %lld s starting %lld s ago",
1428 ds_put_format(&s, "%lld s ago", ago);
1431 ds_put_cstr(&s, " (");
1433 ds_put_format(&s, "%d adds, ", p->n_add);
1436 ds_put_format(&s, "%d deletes, ", p->n_delete);
1439 ds_put_format(&s, "%d modifications, ", p->n_modify);
1442 ds_put_char(&s, ')');
1444 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1447 p->n_add = p->n_delete = p->n_modify = 0;
1448 p->next_op_report = LLONG_MAX;
1454 /* Performs periodic activity required by 'ofproto' that needs to be done
1455 * with the least possible latency.
1457 * It makes sense to call this function a couple of times per poll loop, to
1458 * provide a significant performance boost on some benchmarks with the
1459 * ofproto-dpif implementation. */
1461 ofproto_run_fast(struct ofproto *p)
1465 error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0;
1466 if (error && error != EAGAIN) {
1467 VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)",
1468 p->name, ovs_strerror(error));
1474 ofproto_wait(struct ofproto *p)
1476 struct ofport *ofport;
1478 p->ofproto_class->wait(p);
1479 if (p->ofproto_class->port_poll_wait) {
1480 p->ofproto_class->port_poll_wait(p);
1483 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1484 if (ofport->change_seq != netdev_change_seq(ofport->netdev)) {
1485 poll_immediate_wake();
1491 connmgr_wait(p->connmgr, true);
1496 connmgr_wait(p->connmgr, false);
1497 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1498 poll_immediate_wake();
1505 ofproto_is_alive(const struct ofproto *p)
1507 return connmgr_has_controllers(p->connmgr);
1510 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1511 * memory_report(). */
1513 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1515 const struct oftable *table;
1516 unsigned int n_rules;
1518 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1519 simap_increase(usage, "ops",
1520 ofproto->n_pending + hmap_count(&ofproto->deletions));
1523 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1524 ovs_rwlock_rdlock(&table->cls.rwlock);
1525 n_rules += classifier_count(&table->cls);
1526 ovs_rwlock_unlock(&table->cls.rwlock);
1528 simap_increase(usage, "rules", n_rules);
1530 if (ofproto->ofproto_class->get_memory_usage) {
1531 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1534 connmgr_get_memory_usage(ofproto->connmgr, usage);
1538 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1541 connmgr_get_controller_info(ofproto->connmgr, info);
1545 ofproto_free_ofproto_controller_info(struct shash *info)
1547 connmgr_free_controller_info(info);
1550 /* Makes a deep copy of 'old' into 'port'. */
1552 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1554 port->name = xstrdup(old->name);
1555 port->type = xstrdup(old->type);
1556 port->ofp_port = old->ofp_port;
1559 /* Frees memory allocated to members of 'ofproto_port'.
1561 * Do not call this function on an ofproto_port obtained from
1562 * ofproto_port_dump_next(): that function retains ownership of the data in the
1565 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1567 free(ofproto_port->name);
1568 free(ofproto_port->type);
1571 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1573 * This function provides no status indication. An error status for the entire
1574 * dump operation is provided when it is completed by calling
1575 * ofproto_port_dump_done().
1578 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1579 const struct ofproto *ofproto)
1581 dump->ofproto = ofproto;
1582 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1586 /* Attempts to retrieve another port from 'dump', which must have been created
1587 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1588 * 'port' and returns true. On failure, returns false.
1590 * Failure might indicate an actual error or merely that the last port has been
1591 * dumped. An error status for the entire dump operation is provided when it
1592 * is completed by calling ofproto_port_dump_done().
1594 * The ofproto owns the data stored in 'port'. It will remain valid until at
1595 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1596 * ofproto_port_dump_done(). */
1598 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1599 struct ofproto_port *port)
1601 const struct ofproto *ofproto = dump->ofproto;
1607 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1610 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1616 /* Completes port table dump operation 'dump', which must have been created
1617 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1618 * error-free, otherwise a positive errno value describing the problem. */
1620 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1622 const struct ofproto *ofproto = dump->ofproto;
1624 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1627 return dump->error == EOF ? 0 : dump->error;
1630 /* Returns the type to pass to netdev_open() when a datapath of type
1631 * 'datapath_type' has a port of type 'port_type', for a few special
1632 * cases when a netdev type differs from a port type. For example, when
1633 * using the userspace datapath, a port of type "internal" needs to be
1636 * Returns either 'type' itself or a string literal, which must not be
1639 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1641 const struct ofproto_class *class;
1643 datapath_type = ofproto_normalize_type(datapath_type);
1644 class = ofproto_class_find__(datapath_type);
1649 return (class->port_open_type
1650 ? class->port_open_type(datapath_type, port_type)
1654 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1655 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1656 * the port's OpenFlow port number.
1658 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1659 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1660 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1661 * 'ofp_portp' is non-null). */
1663 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1664 ofp_port_t *ofp_portp)
1666 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1669 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1671 const char *netdev_name = netdev_get_name(netdev);
1673 simap_put(&ofproto->ofp_requests, netdev_name,
1674 ofp_to_u16(ofp_port));
1675 update_port(ofproto, netdev_name);
1678 struct ofproto_port ofproto_port;
1680 ofproto_port_query_by_name(ofproto, netdev_get_name(netdev),
1682 *ofp_portp = error ? OFPP_NONE : ofproto_port.ofp_port;
1683 ofproto_port_destroy(&ofproto_port);
1688 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1689 * initializes '*port' appropriately; on failure, returns a positive errno
1692 * The caller owns the data in 'ofproto_port' and must free it with
1693 * ofproto_port_destroy() when it is no longer needed. */
1695 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1696 struct ofproto_port *port)
1700 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1702 memset(port, 0, sizeof *port);
1707 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1708 * Returns 0 if successful, otherwise a positive errno. */
1710 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1712 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1713 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1714 struct simap_node *ofp_request_node;
1717 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1718 if (ofp_request_node) {
1719 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1722 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1723 if (!error && ofport) {
1724 /* 'name' is the netdev's name and update_port() is going to close the
1725 * netdev. Just in case update_port() refers to 'name' after it
1726 * destroys 'ofport', make a copy of it around the update_port()
1728 char *devname = xstrdup(name);
1729 update_port(ofproto, devname);
1736 simple_flow_mod(struct ofproto *ofproto,
1737 const struct match *match, unsigned int priority,
1738 const struct ofpact *ofpacts, size_t ofpacts_len,
1739 enum ofp_flow_mod_command command)
1741 struct ofputil_flow_mod fm;
1743 memset(&fm, 0, sizeof fm);
1745 fm.priority = priority;
1748 fm.modify_cookie = false;
1750 fm.command = command;
1751 fm.idle_timeout = 0;
1752 fm.hard_timeout = 0;
1753 fm.buffer_id = UINT32_MAX;
1754 fm.out_port = OFPP_ANY;
1755 fm.out_group = OFPG_ANY;
1757 fm.ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1758 fm.ofpacts_len = ofpacts_len;
1759 return handle_flow_mod__(ofproto, NULL, &fm, NULL);
1762 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1763 * performs the 'n_actions' actions in 'actions'. The new flow will not
1766 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1767 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1768 * controllers; otherwise, it will be hidden.
1770 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1772 * This is a helper function for in-band control and fail-open. */
1774 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1775 unsigned int priority,
1776 const struct ofpact *ofpacts, size_t ofpacts_len)
1778 const struct rule *rule;
1780 /* First do a cheap check whether the rule we're looking for already exists
1781 * with the actions that we want. If it does, then we're done. */
1782 ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1783 rule = rule_from_cls_rule(classifier_find_match_exactly(
1784 &ofproto->tables[0].cls, match, priority));
1785 ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1787 /* If there's no such rule or the rule doesn't have the actions we want,
1788 * fall back to a executing a full flow mod. We can't optimize this at
1789 * all because we didn't take enough locks above to ensure that the flow
1790 * table didn't already change beneath us. */
1791 if (!rule || !ofpacts_equal(rule->ofpacts, rule->ofpacts_len,
1792 ofpacts, ofpacts_len)) {
1793 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1794 OFPFC_MODIFY_STRICT);
1798 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1799 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1800 * operation cannot be initiated now but may be retried later.
1802 * This is a helper function for in-band control and fail-open. */
1804 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1806 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1809 /* Searches for a rule with matching criteria exactly equal to 'target' in
1810 * ofproto's table 0 and, if it finds one, deletes it.
1812 * This is a helper function for in-band control and fail-open. */
1814 ofproto_delete_flow(struct ofproto *ofproto,
1815 const struct match *target, unsigned int priority)
1817 struct classifier *cls = &ofproto->tables[0].cls;
1820 /* First do a cheap check whether the rule we're looking for has already
1821 * been deleted. If so, then we're done. */
1822 ovs_rwlock_rdlock(&cls->rwlock);
1823 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1825 ovs_rwlock_unlock(&cls->rwlock);
1830 /* Fall back to a executing a full flow mod. We can't optimize this at all
1831 * because we didn't take enough locks above to ensure that the flow table
1832 * didn't already change beneath us. */
1833 return simple_flow_mod(ofproto, target, priority, NULL, 0,
1834 OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE;
1837 /* Starts the process of deleting all of the flows from all of ofproto's flow
1838 * tables and then reintroducing the flows required by in-band control and
1839 * fail-open. The process will complete in a later call to ofproto_run(). */
1841 ofproto_flush_flows(struct ofproto *ofproto)
1843 COVERAGE_INC(ofproto_flush);
1844 ofproto->state = S_FLUSH;
1848 reinit_ports(struct ofproto *p)
1850 struct ofproto_port_dump dump;
1851 struct sset devnames;
1852 struct ofport *ofport;
1853 struct ofproto_port ofproto_port;
1854 const char *devname;
1856 COVERAGE_INC(ofproto_reinit_ports);
1858 sset_init(&devnames);
1859 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1860 sset_add(&devnames, netdev_get_name(ofport->netdev));
1862 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1863 sset_add(&devnames, ofproto_port.name);
1866 SSET_FOR_EACH (devname, &devnames) {
1867 update_port(p, devname);
1869 sset_destroy(&devnames);
1873 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
1877 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
1878 port_idx = port_idx ? port_idx : UINT16_MAX;
1880 if (port_idx >= ofproto->max_ports
1881 || bitmap_is_set(ofproto->ofp_port_ids, port_idx)) {
1882 uint16_t end_port_no = ofproto->alloc_port_no;
1884 /* Search for a free OpenFlow port number. We try not to
1885 * immediately reuse them to prevent problems due to old
1888 if (++ofproto->alloc_port_no >= ofproto->max_ports) {
1889 ofproto->alloc_port_no = 0;
1891 if (!bitmap_is_set(ofproto->ofp_port_ids,
1892 ofproto->alloc_port_no)) {
1893 port_idx = ofproto->alloc_port_no;
1896 if (ofproto->alloc_port_no == end_port_no) {
1901 bitmap_set1(ofproto->ofp_port_ids, port_idx);
1902 return u16_to_ofp(port_idx);
1906 dealloc_ofp_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
1908 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
1909 bitmap_set0(ofproto->ofp_port_ids, ofp_to_u16(ofp_port));
1913 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
1914 * pointer if the netdev cannot be opened. On success, also fills in
1916 static struct netdev *
1917 ofport_open(struct ofproto *ofproto,
1918 struct ofproto_port *ofproto_port,
1919 struct ofputil_phy_port *pp)
1921 enum netdev_flags flags;
1922 struct netdev *netdev;
1925 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
1927 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
1928 "cannot be opened (%s)",
1930 ofproto_port->name, ofproto_port->ofp_port,
1931 ofproto_port->name, ovs_strerror(error));
1935 if (ofproto_port->ofp_port == OFPP_NONE) {
1936 if (!strcmp(ofproto->name, ofproto_port->name)) {
1937 ofproto_port->ofp_port = OFPP_LOCAL;
1939 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
1940 ofproto_port->name);
1943 pp->port_no = ofproto_port->ofp_port;
1944 netdev_get_etheraddr(netdev, pp->hw_addr);
1945 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
1946 netdev_get_flags(netdev, &flags);
1947 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
1948 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
1949 netdev_get_features(netdev, &pp->curr, &pp->advertised,
1950 &pp->supported, &pp->peer);
1951 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
1952 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
1957 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1958 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
1961 ofport_equal(const struct ofputil_phy_port *a,
1962 const struct ofputil_phy_port *b)
1964 return (eth_addr_equals(a->hw_addr, b->hw_addr)
1965 && a->state == b->state
1966 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
1967 && a->curr == b->curr
1968 && a->advertised == b->advertised
1969 && a->supported == b->supported
1970 && a->peer == b->peer
1971 && a->curr_speed == b->curr_speed
1972 && a->max_speed == b->max_speed);
1975 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1976 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1977 * one with the same name or port number). */
1979 ofport_install(struct ofproto *p,
1980 struct netdev *netdev, const struct ofputil_phy_port *pp)
1982 const char *netdev_name = netdev_get_name(netdev);
1983 struct ofport *ofport;
1986 /* Create ofport. */
1987 ofport = p->ofproto_class->port_alloc();
1992 ofport->ofproto = p;
1993 ofport->netdev = netdev;
1994 ofport->change_seq = netdev_change_seq(netdev);
1996 ofport->ofp_port = pp->port_no;
1997 ofport->created = time_msec();
1999 /* Add port to 'p'. */
2000 hmap_insert(&p->ports, &ofport->hmap_node,
2001 hash_ofp_port(ofport->ofp_port));
2002 shash_add(&p->port_by_name, netdev_name, ofport);
2004 update_mtu(p, ofport);
2006 /* Let the ofproto_class initialize its private data. */
2007 error = p->ofproto_class->port_construct(ofport);
2011 connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD);
2015 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
2016 p->name, netdev_name, ovs_strerror(error));
2018 ofport_destroy__(ofport);
2020 netdev_close(netdev);
2024 /* Removes 'ofport' from 'p' and destroys it. */
2026 ofport_remove(struct ofport *ofport)
2028 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp,
2030 ofport_destroy(ofport);
2033 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2036 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2038 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2040 ofport_remove(port);
2044 /* Updates 'port' with new 'pp' description.
2046 * Does not handle a name or port number change. The caller must implement
2047 * such a change as a delete followed by an add. */
2049 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
2051 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2052 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2053 | (pp->config & OFPUTIL_PC_PORT_DOWN));
2054 port->pp.state = pp->state;
2055 port->pp.curr = pp->curr;
2056 port->pp.advertised = pp->advertised;
2057 port->pp.supported = pp->supported;
2058 port->pp.peer = pp->peer;
2059 port->pp.curr_speed = pp->curr_speed;
2060 port->pp.max_speed = pp->max_speed;
2062 connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY);
2065 /* Update OpenFlow 'state' in 'port' and notify controller. */
2067 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2069 if (port->pp.state != state) {
2070 port->pp.state = state;
2071 connmgr_send_port_status(port->ofproto->connmgr, &port->pp,
2077 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2079 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2081 if (port->ofproto->ofproto_class->set_realdev) {
2082 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2084 if (port->ofproto->ofproto_class->set_stp_port) {
2085 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2087 if (port->ofproto->ofproto_class->set_cfm) {
2088 port->ofproto->ofproto_class->set_cfm(port, NULL);
2090 if (port->ofproto->ofproto_class->bundle_remove) {
2091 port->ofproto->ofproto_class->bundle_remove(port);
2097 ofport_destroy__(struct ofport *port)
2099 struct ofproto *ofproto = port->ofproto;
2100 const char *name = netdev_get_name(port->netdev);
2102 hmap_remove(&ofproto->ports, &port->hmap_node);
2103 shash_delete(&ofproto->port_by_name,
2104 shash_find(&ofproto->port_by_name, name));
2106 netdev_close(port->netdev);
2107 ofproto->ofproto_class->port_dealloc(port);
2111 ofport_destroy(struct ofport *port)
2114 dealloc_ofp_port(port->ofproto, port->ofp_port);
2115 port->ofproto->ofproto_class->port_destruct(port);
2116 ofport_destroy__(port);
2121 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2123 struct ofport *port;
2125 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2127 if (port->ofp_port == ofp_port) {
2135 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2137 struct ofproto *ofproto = port->ofproto;
2140 if (ofproto->ofproto_class->port_get_stats) {
2141 error = ofproto->ofproto_class->port_get_stats(port, stats);
2150 update_port(struct ofproto *ofproto, const char *name)
2152 struct ofproto_port ofproto_port;
2153 struct ofputil_phy_port pp;
2154 struct netdev *netdev;
2155 struct ofport *port;
2157 COVERAGE_INC(ofproto_update_port);
2159 /* Fetch 'name''s location and properties from the datapath. */
2160 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2161 ? ofport_open(ofproto, &ofproto_port, &pp)
2165 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2166 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2167 struct netdev *old_netdev = port->netdev;
2169 /* 'name' hasn't changed location. Any properties changed? */
2170 if (!ofport_equal(&port->pp, &pp)) {
2171 ofport_modified(port, &pp);
2174 update_mtu(ofproto, port);
2176 /* Install the newly opened netdev in case it has changed.
2177 * Don't close the old netdev yet in case port_modified has to
2178 * remove a retained reference to it.*/
2179 port->netdev = netdev;
2180 port->change_seq = netdev_change_seq(netdev);
2182 if (port->ofproto->ofproto_class->port_modified) {
2183 port->ofproto->ofproto_class->port_modified(port);
2186 netdev_close(old_netdev);
2188 /* If 'port' is nonnull then its name differs from 'name' and thus
2189 * we should delete it. If we think there's a port named 'name'
2190 * then its port number must be wrong now so delete it too. */
2192 ofport_remove(port);
2194 ofport_remove_with_name(ofproto, name);
2195 ofport_install(ofproto, netdev, &pp);
2198 /* Any port named 'name' is gone now. */
2199 ofport_remove_with_name(ofproto, name);
2201 ofproto_port_destroy(&ofproto_port);
2205 init_ports(struct ofproto *p)
2207 struct ofproto_port_dump dump;
2208 struct ofproto_port ofproto_port;
2209 struct shash_node *node, *next;
2211 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2212 const char *name = ofproto_port.name;
2214 if (shash_find(&p->port_by_name, name)) {
2215 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2218 struct ofputil_phy_port pp;
2219 struct netdev *netdev;
2221 /* Check if an OpenFlow port number had been requested. */
2222 node = shash_find(&init_ofp_ports, name);
2224 const struct iface_hint *iface_hint = node->data;
2225 simap_put(&p->ofp_requests, name,
2226 ofp_to_u16(iface_hint->ofp_port));
2229 netdev = ofport_open(p, &ofproto_port, &pp);
2231 ofport_install(p, netdev, &pp);
2232 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2233 p->alloc_port_no = MAX(p->alloc_port_no,
2234 ofp_to_u16(ofproto_port.ofp_port));
2240 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2241 struct iface_hint *iface_hint = node->data;
2243 if (!strcmp(iface_hint->br_name, p->name)) {
2244 free(iface_hint->br_name);
2245 free(iface_hint->br_type);
2247 shash_delete(&init_ofp_ports, node);
2254 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2255 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2257 find_min_mtu(struct ofproto *p)
2259 struct ofport *ofport;
2262 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2263 struct netdev *netdev = ofport->netdev;
2266 /* Skip any internal ports, since that's what we're trying to
2268 if (!strcmp(netdev_get_type(netdev), "internal")) {
2272 if (netdev_get_mtu(netdev, &dev_mtu)) {
2275 if (!mtu || dev_mtu < mtu) {
2280 return mtu ? mtu: ETH_PAYLOAD_MAX;
2283 /* Update MTU of all datapath devices on 'p' to the minimum of the
2284 * non-datapath ports in event of 'port' added or changed. */
2286 update_mtu(struct ofproto *p, struct ofport *port)
2288 struct ofport *ofport;
2289 struct netdev *netdev = port->netdev;
2290 int dev_mtu, old_min;
2292 if (netdev_get_mtu(netdev, &dev_mtu)) {
2296 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2297 if (dev_mtu > p->min_mtu) {
2298 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2299 dev_mtu = p->min_mtu;
2302 port->mtu = dev_mtu;
2306 /* For non-internal port find new min mtu. */
2307 old_min = p->min_mtu;
2308 port->mtu = dev_mtu;
2309 p->min_mtu = find_min_mtu(p);
2310 if (p->min_mtu == old_min) {
2314 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2315 struct netdev *netdev = ofport->netdev;
2317 if (!strcmp(netdev_get_type(netdev), "internal")) {
2318 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2319 ofport->mtu = p->min_mtu;
2326 ofproto_rule_destroy(struct rule *rule)
2329 rule->ofproto->ofproto_class->rule_destruct(rule);
2330 ofproto_rule_destroy__(rule);
2335 ofproto_rule_destroy__(struct rule *rule)
2337 cls_rule_destroy(&rule->cr);
2338 free(rule->ofpacts);
2339 ovs_mutex_destroy(&rule->timeout_mutex);
2340 ovs_rwlock_destroy(&rule->rwlock);
2341 rule->ofproto->ofproto_class->rule_dealloc(rule);
2344 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2345 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2347 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2349 return (port == OFPP_ANY
2350 || ofpacts_output_to_port(rule->ofpacts, rule->ofpacts_len, port));
2353 /* Returns true if 'rule' has group and equals group_id. */
2355 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2357 return (group_id == OFPG11_ANY
2358 || ofpacts_output_to_group(rule->ofpacts, rule->ofpacts_len, group_id));
2361 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2362 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2364 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2366 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2371 case OFOPERATION_ADD:
2372 case OFOPERATION_DELETE:
2375 case OFOPERATION_MODIFY:
2376 case OFOPERATION_REPLACE:
2377 return ofpacts_output_to_port(op->ofpacts, op->ofpacts_len, out_port);
2383 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
2384 * statistics appropriately.
2386 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
2387 * with statistics for 'packet' either way.
2389 * Takes ownership of 'packet'. */
2391 rule_execute(struct rule *rule, ofp_port_t in_port, struct ofpbuf *packet)
2394 union flow_in_port in_port_;
2396 in_port_.ofp_port = in_port;
2397 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
2398 return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet);
2401 /* Returns true if 'rule' should be hidden from the controller.
2403 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2404 * (e.g. by in-band control) and are intentionally hidden from the
2407 ofproto_rule_is_hidden(const struct rule *rule)
2409 return rule->cr.priority > UINT16_MAX;
2412 static enum oftable_flags
2413 rule_get_flags(const struct rule *rule)
2415 return rule->ofproto->tables[rule->table_id].flags;
2419 rule_is_modifiable(const struct rule *rule)
2421 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2425 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2427 ofconn_send_reply(ofconn, make_echo_reply(oh));
2432 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2434 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2435 struct ofputil_switch_features features;
2436 struct ofport *port;
2442 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2444 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2446 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2447 * if present, are always at the end.) */
2448 n_tables = ofproto->n_tables;
2449 for (i = 0; i < ofproto->n_tables; i++) {
2450 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2456 features.datapath_id = ofproto->datapath_id;
2457 features.n_buffers = pktbuf_capacity();
2458 features.n_tables = n_tables;
2459 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2460 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2462 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2464 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2465 features.auxiliary_id = 0;
2466 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2468 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2469 ofputil_put_switch_features_port(&port->pp, b);
2472 ofconn_send_reply(ofconn, b);
2477 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2479 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2480 struct ofp_switch_config *osc;
2481 enum ofp_config_flags flags;
2485 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2486 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2487 flags = ofproto->frag_handling;
2488 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2489 if (oh->version < OFP13_VERSION
2490 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2491 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2493 osc->flags = htons(flags);
2494 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2495 ofconn_send_reply(ofconn, buf);
2501 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2503 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2504 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2505 uint16_t flags = ntohs(osc->flags);
2507 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2508 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2509 enum ofp_config_flags cur = ofproto->frag_handling;
2510 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2512 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2514 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2515 ofproto->frag_handling = next;
2517 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2519 ofputil_frag_handling_to_string(next));
2523 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2524 ofconn_set_invalid_ttl_to_controller(ofconn,
2525 (oh->version < OFP13_VERSION
2526 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2528 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2533 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2534 * error message code for the caller to propagate upward. Otherwise, returns
2537 * The log message mentions 'msg_type'. */
2539 reject_slave_controller(struct ofconn *ofconn)
2541 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2542 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2543 return OFPERR_OFPBRC_EPERM;
2549 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate
2550 * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'.
2551 * 'flow' may be temporarily modified, but is restored at return.
2554 ofproto_check_ofpacts(struct ofproto *ofproto,
2555 const struct ofpact ofpacts[], size_t ofpacts_len,
2556 struct flow *flow, uint8_t table_id)
2561 error = ofpacts_check(ofpacts, ofpacts_len, flow,
2562 u16_to_ofp(ofproto->max_ports), table_id);
2567 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2568 if (mid && ofproto_get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2569 return OFPERR_OFPMMFC_INVALID_METER;
2575 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2577 struct ofproto *p = ofconn_get_ofproto(ofconn);
2578 struct ofputil_packet_out po;
2579 struct ofpbuf *payload;
2580 uint64_t ofpacts_stub[1024 / 8];
2581 struct ofpbuf ofpacts;
2583 union flow_in_port in_port_;
2586 COVERAGE_INC(ofproto_packet_out);
2588 error = reject_slave_controller(ofconn);
2593 /* Decode message. */
2594 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2595 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2597 goto exit_free_ofpacts;
2599 if (ofp_to_u16(po.in_port) >= p->max_ports
2600 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2601 error = OFPERR_OFPBRC_BAD_PORT;
2602 goto exit_free_ofpacts;
2607 if (po.buffer_id != UINT32_MAX) {
2608 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2609 if (error || !payload) {
2610 goto exit_free_ofpacts;
2613 /* Ensure that the L3 header is 32-bit aligned. */
2614 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2617 /* Verify actions against packet, then send packet if successful. */
2618 in_port_.ofp_port = po.in_port;
2619 flow_extract(payload, 0, 0, NULL, &in_port_, &flow);
2620 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len, &flow, 0);
2622 error = p->ofproto_class->packet_out(p, payload, &flow,
2623 po.ofpacts, po.ofpacts_len);
2625 ofpbuf_delete(payload);
2628 ofpbuf_uninit(&ofpacts);
2634 update_port_config(struct ofport *port,
2635 enum ofputil_port_config config,
2636 enum ofputil_port_config mask)
2638 enum ofputil_port_config old_config = port->pp.config;
2639 enum ofputil_port_config toggle;
2641 toggle = (config ^ port->pp.config) & mask;
2642 if (toggle & OFPUTIL_PC_PORT_DOWN) {
2643 if (config & OFPUTIL_PC_PORT_DOWN) {
2644 netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL);
2646 netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL);
2648 toggle &= ~OFPUTIL_PC_PORT_DOWN;
2651 port->pp.config ^= toggle;
2652 if (port->pp.config != old_config) {
2653 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2658 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2660 struct ofproto *p = ofconn_get_ofproto(ofconn);
2661 struct ofputil_port_mod pm;
2662 struct ofport *port;
2665 error = reject_slave_controller(ofconn);
2670 error = ofputil_decode_port_mod(oh, &pm);
2675 port = ofproto_get_port(p, pm.port_no);
2677 return OFPERR_OFPPMFC_BAD_PORT;
2678 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
2679 return OFPERR_OFPPMFC_BAD_HW_ADDR;
2681 update_port_config(port, pm.config, pm.mask);
2683 netdev_set_advertisements(port->netdev, pm.advertise);
2690 handle_desc_stats_request(struct ofconn *ofconn,
2691 const struct ofp_header *request)
2693 static const char *default_mfr_desc = "Nicira, Inc.";
2694 static const char *default_hw_desc = "Open vSwitch";
2695 static const char *default_sw_desc = VERSION;
2696 static const char *default_serial_desc = "None";
2697 static const char *default_dp_desc = "None";
2699 struct ofproto *p = ofconn_get_ofproto(ofconn);
2700 struct ofp_desc_stats *ods;
2703 msg = ofpraw_alloc_stats_reply(request, 0);
2704 ods = ofpbuf_put_zeros(msg, sizeof *ods);
2705 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
2706 sizeof ods->mfr_desc);
2707 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
2708 sizeof ods->hw_desc);
2709 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
2710 sizeof ods->sw_desc);
2711 ovs_strlcpy(ods->serial_num,
2712 p->serial_desc ? p->serial_desc : default_serial_desc,
2713 sizeof ods->serial_num);
2714 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
2715 sizeof ods->dp_desc);
2716 ofconn_send_reply(ofconn, msg);
2722 handle_table_stats_request(struct ofconn *ofconn,
2723 const struct ofp_header *request)
2725 struct ofproto *p = ofconn_get_ofproto(ofconn);
2726 struct ofp12_table_stats *ots;
2731 /* Set up default values.
2733 * ofp12_table_stats is used as a generic structure as
2734 * it is able to hold all the fields for ofp10_table_stats
2735 * and ofp11_table_stats (and of course itself).
2737 ots = xcalloc(p->n_tables, sizeof *ots);
2738 for (i = 0; i < p->n_tables; i++) {
2739 ots[i].table_id = i;
2740 sprintf(ots[i].name, "table%zu", i);
2741 ots[i].match = htonll(OFPXMT12_MASK);
2742 ots[i].wildcards = htonll(OFPXMT12_MASK);
2743 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
2744 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
2745 ots[i].write_setfields = htonll(OFPXMT12_MASK);
2746 ots[i].apply_setfields = htonll(OFPXMT12_MASK);
2747 ots[i].metadata_match = htonll(UINT64_MAX);
2748 ots[i].metadata_write = htonll(UINT64_MAX);
2749 ots[i].instructions = htonl(OFPIT11_ALL);
2750 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
2751 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
2752 ovs_rwlock_rdlock(&p->tables[i].cls.rwlock);
2753 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
2754 ovs_rwlock_unlock(&p->tables[i].cls.rwlock);
2757 p->ofproto_class->get_tables(p, ots);
2759 /* Post-process the tables, dropping hidden tables. */
2760 n_tables = p->n_tables;
2761 for (i = 0; i < p->n_tables; i++) {
2762 const struct oftable *table = &p->tables[i];
2764 if (table->flags & OFTABLE_HIDDEN) {
2770 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
2773 if (table->max_flows < ntohl(ots[i].max_entries)) {
2774 ots[i].max_entries = htonl(table->max_flows);
2778 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
2779 ofconn_send_reply(ofconn, msg);
2787 append_port_stat(struct ofport *port, struct list *replies)
2789 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
2791 calc_duration(port->created, time_msec(),
2792 &ops.duration_sec, &ops.duration_nsec);
2794 /* Intentionally ignore return value, since errors will set
2795 * 'stats' to all-1s, which is correct for OpenFlow, and
2796 * netdev_get_stats() will log errors. */
2797 ofproto_port_get_stats(port, &ops.stats);
2799 ofputil_append_port_stat(replies, &ops);
2803 handle_port_stats_request(struct ofconn *ofconn,
2804 const struct ofp_header *request)
2806 struct ofproto *p = ofconn_get_ofproto(ofconn);
2807 struct ofport *port;
2808 struct list replies;
2812 error = ofputil_decode_port_stats_request(request, &port_no);
2817 ofpmp_init(&replies, request);
2818 if (port_no != OFPP_ANY) {
2819 port = ofproto_get_port(p, port_no);
2821 append_port_stat(port, &replies);
2824 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2825 append_port_stat(port, &replies);
2829 ofconn_send_replies(ofconn, &replies);
2834 handle_port_desc_stats_request(struct ofconn *ofconn,
2835 const struct ofp_header *request)
2837 struct ofproto *p = ofconn_get_ofproto(ofconn);
2838 enum ofp_version version;
2839 struct ofport *port;
2840 struct list replies;
2842 ofpmp_init(&replies, request);
2844 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
2845 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2846 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
2849 ofconn_send_replies(ofconn, &replies);
2854 hash_cookie(ovs_be64 cookie)
2856 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
2857 (OVS_FORCE uint64_t)cookie);
2861 cookies_insert(struct ofproto *ofproto, struct rule *rule)
2863 hindex_insert(&ofproto->cookies, &rule->cookie_node,
2864 hash_cookie(rule->flow_cookie));
2868 cookies_remove(struct ofproto *ofproto, struct rule *rule)
2870 hindex_remove(&ofproto->cookies, &rule->cookie_node);
2874 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
2875 ovs_be64 new_cookie)
2877 if (new_cookie != rule->flow_cookie) {
2878 cookies_remove(ofproto, rule);
2880 ovs_rwlock_wrlock(&rule->rwlock);
2881 rule->flow_cookie = new_cookie;
2882 ovs_rwlock_unlock(&rule->rwlock);
2884 cookies_insert(ofproto, rule);
2889 calc_duration(long long int start, long long int now,
2890 uint32_t *sec, uint32_t *nsec)
2892 long long int msecs = now - start;
2893 *sec = msecs / 1000;
2894 *nsec = (msecs % 1000) * (1000 * 1000);
2897 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
2898 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
2900 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
2902 return (table_id == 0xff || table_id < ofproto->n_tables
2904 : OFPERR_OFPBRC_BAD_TABLE_ID);
2908 static struct oftable *
2909 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
2911 struct oftable *table;
2913 for (table = &ofproto->tables[table_id];
2914 table < &ofproto->tables[ofproto->n_tables];
2916 if (!(table->flags & OFTABLE_HIDDEN)) {
2924 static struct oftable *
2925 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
2927 if (table_id == 0xff) {
2928 return next_visible_table(ofproto, 0);
2929 } else if (table_id < ofproto->n_tables) {
2930 return &ofproto->tables[table_id];
2936 static struct oftable *
2937 next_matching_table(const struct ofproto *ofproto,
2938 const struct oftable *table, uint8_t table_id)
2940 return (table_id == 0xff
2941 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
2945 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
2947 * - If TABLE_ID is 0xff, this iterates over every classifier table in
2948 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
2950 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
2951 * only once, for that table. (This can be used to access tables marked
2954 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
2955 * entered at all. (Perhaps you should have validated TABLE_ID with
2956 * check_table_id().)
2958 * All parameters are evaluated multiple times.
2960 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
2961 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
2963 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
2965 /* Initializes 'criteria' in a straightforward way based on the other
2968 * For "loose" matching, the 'priority' parameter is unimportant and may be
2971 rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
2972 const struct match *match, unsigned int priority,
2973 ovs_be64 cookie, ovs_be64 cookie_mask,
2974 ofp_port_t out_port, uint32_t out_group)
2976 criteria->table_id = table_id;
2977 cls_rule_init(&criteria->cr, match, priority);
2978 criteria->cookie = cookie;
2979 criteria->cookie_mask = cookie_mask;
2980 criteria->out_port = out_port;
2981 criteria->out_group = out_group;
2985 rule_criteria_destroy(struct rule_criteria *criteria)
2987 cls_rule_destroy(&criteria->cr);
2991 rule_collection_init(struct rule_collection *rules)
2993 rules->rules = rules->stub;
2995 rules->capacity = ARRAY_SIZE(rules->stub);
2999 rule_collection_add(struct rule_collection *rules, struct rule *rule)
3001 if (rules->n >= rules->capacity) {
3002 size_t old_size, new_size;
3004 old_size = rules->capacity * sizeof *rules->rules;
3005 rules->capacity *= 2;
3006 new_size = rules->capacity * sizeof *rules->rules;
3008 if (rules->rules == rules->stub) {
3009 rules->rules = xmalloc(new_size);
3010 memcpy(rules->rules, rules->stub, old_size);
3012 rules->rules = xrealloc(rules->rules, new_size);
3016 rules->rules[rules->n++] = rule;
3020 rule_collection_destroy(struct rule_collection *rules)
3022 if (rules->rules != rules->stub) {
3028 collect_rule(struct rule *rule, const struct rule_criteria *c,
3029 struct rule_collection *rules)
3031 if (ofproto_rule_is_hidden(rule)) {
3033 } else if (rule->pending) {
3034 return OFPROTO_POSTPONE;
3036 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3037 && ofproto_rule_has_out_port(rule, c->out_port)
3038 && ofproto_rule_has_out_group(rule, c->out_group)
3039 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)) {
3040 rule_collection_add(rules, rule);
3046 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3047 * on classifiers rules are done in the "loose" way required for OpenFlow
3048 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3051 * Hidden rules are always omitted.
3053 * Returns 0 on success, otherwise an OpenFlow error code. */
3055 collect_rules_loose(struct ofproto *ofproto,
3056 const struct rule_criteria *criteria,
3057 struct rule_collection *rules)
3059 struct oftable *table;
3062 rule_collection_init(rules);
3064 error = check_table_id(ofproto, criteria->table_id);
3069 if (criteria->cookie_mask == htonll(UINT64_MAX)) {
3072 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3073 hash_cookie(criteria->cookie),
3074 &ofproto->cookies) {
3075 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
3076 error = collect_rule(rule, criteria, rules);
3083 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3084 struct cls_cursor cursor;
3087 ovs_rwlock_rdlock(&table->cls.rwlock);
3088 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
3089 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3090 error = collect_rule(rule, criteria, rules);
3095 ovs_rwlock_unlock(&table->cls.rwlock);
3101 rule_collection_destroy(rules);
3106 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3107 * on classifiers rules are done in the "strict" way required for OpenFlow
3108 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3109 * rules on list 'rules'.
3111 * Hidden rules are always omitted.
3113 * Returns 0 on success, otherwise an OpenFlow error code. */
3115 collect_rules_strict(struct ofproto *ofproto,
3116 const struct rule_criteria *criteria,
3117 struct rule_collection *rules)
3119 struct oftable *table;
3122 rule_collection_init(rules);
3124 error = check_table_id(ofproto, criteria->table_id);
3129 if (criteria->cookie_mask == htonll(UINT64_MAX)) {
3132 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3133 hash_cookie(criteria->cookie),
3134 &ofproto->cookies) {
3135 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
3136 error = collect_rule(rule, criteria, rules);
3143 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3146 ovs_rwlock_rdlock(&table->cls.rwlock);
3147 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3148 &table->cls, &criteria->cr));
3149 ovs_rwlock_unlock(&table->cls.rwlock);
3151 error = collect_rule(rule, criteria, rules);
3161 rule_collection_destroy(rules);
3166 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3167 * forced into the range of a uint16_t. */
3169 age_secs(long long int age_ms)
3171 return (age_ms < 0 ? 0
3172 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3173 : (unsigned int) age_ms / 1000);
3177 handle_flow_stats_request(struct ofconn *ofconn,
3178 const struct ofp_header *request)
3180 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3181 struct ofputil_flow_stats_request fsr;
3182 struct rule_criteria criteria;
3183 struct rule_collection rules;
3184 struct list replies;
3188 error = ofputil_decode_flow_stats_request(&fsr, request);
3193 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3194 fsr.cookie_mask, fsr.out_port, fsr.out_group);
3195 error = collect_rules_loose(ofproto, &criteria, &rules);
3196 rule_criteria_destroy(&criteria);
3201 ofpmp_init(&replies, request);
3202 for (i = 0; i < rules.n; i++) {
3203 struct rule *rule = rules.rules[i];
3204 long long int now = time_msec();
3205 struct ofputil_flow_stats fs;
3207 minimatch_expand(&rule->cr.match, &fs.match);
3208 fs.priority = rule->cr.priority;
3209 fs.cookie = rule->flow_cookie;
3210 fs.table_id = rule->table_id;
3211 calc_duration(rule->created, now, &fs.duration_sec, &fs.duration_nsec);
3212 fs.idle_age = age_secs(now - rule->used);
3213 fs.hard_age = age_secs(now - rule->modified);
3214 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3216 fs.ofpacts = rule->ofpacts;
3217 fs.ofpacts_len = rule->ofpacts_len;
3219 ovs_mutex_lock(&rule->timeout_mutex);
3220 fs.idle_timeout = rule->idle_timeout;
3221 fs.hard_timeout = rule->hard_timeout;
3222 ovs_mutex_unlock(&rule->timeout_mutex);
3224 fs.flags = rule->flags;
3226 ofputil_append_flow_stats_reply(&fs, &replies);
3228 rule_collection_destroy(&rules);
3230 ofconn_send_replies(ofconn, &replies);
3236 flow_stats_ds(struct rule *rule, struct ds *results)
3238 uint64_t packet_count, byte_count;
3240 rule->ofproto->ofproto_class->rule_get_stats(rule,
3241 &packet_count, &byte_count);
3243 if (rule->table_id != 0) {
3244 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3246 ds_put_format(results, "duration=%llds, ",
3247 (time_msec() - rule->created) / 1000);
3248 ds_put_format(results, "priority=%u, ", rule->cr.priority);
3249 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3250 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3251 cls_rule_format(&rule->cr, results);
3252 ds_put_char(results, ',');
3253 ofpacts_format(rule->ofpacts, rule->ofpacts_len, results);
3254 ds_put_cstr(results, "\n");
3257 /* Adds a pretty-printed description of all flows to 'results', including
3258 * hidden flows (e.g., set up by in-band control). */
3260 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3262 struct oftable *table;
3264 OFPROTO_FOR_EACH_TABLE (table, p) {
3265 struct cls_cursor cursor;
3268 ovs_rwlock_rdlock(&table->cls.rwlock);
3269 cls_cursor_init(&cursor, &table->cls, NULL);
3270 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3271 flow_stats_ds(rule, results);
3273 ovs_rwlock_unlock(&table->cls.rwlock);
3277 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3278 * '*engine_type' and '*engine_id', respectively. */
3280 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3281 uint8_t *engine_type, uint8_t *engine_id)
3283 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3286 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3287 * true if the port's CFM status was successfully stored into '*status'.
3288 * Returns false if the port did not have CFM configured, in which case
3289 * '*status' is indeterminate.
3291 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3293 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3294 struct ofproto_cfm_status *status)
3296 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3298 && ofproto->ofproto_class->get_cfm_status
3299 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3303 handle_aggregate_stats_request(struct ofconn *ofconn,
3304 const struct ofp_header *oh)
3306 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3307 struct ofputil_flow_stats_request request;
3308 struct ofputil_aggregate_stats stats;
3309 bool unknown_packets, unknown_bytes;
3310 struct rule_criteria criteria;
3311 struct rule_collection rules;
3312 struct ofpbuf *reply;
3316 error = ofputil_decode_flow_stats_request(&request, oh);
3321 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3322 request.cookie, request.cookie_mask,
3323 request.out_port, request.out_group);
3324 error = collect_rules_loose(ofproto, &criteria, &rules);
3325 rule_criteria_destroy(&criteria);
3330 memset(&stats, 0, sizeof stats);
3331 unknown_packets = unknown_bytes = false;
3332 for (i = 0; i < rules.n; i++) {
3333 struct rule *rule = rules.rules[i];
3334 uint64_t packet_count;
3335 uint64_t byte_count;
3337 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3340 if (packet_count == UINT64_MAX) {
3341 unknown_packets = true;
3343 stats.packet_count += packet_count;
3346 if (byte_count == UINT64_MAX) {
3347 unknown_bytes = true;
3349 stats.byte_count += byte_count;
3354 if (unknown_packets) {
3355 stats.packet_count = UINT64_MAX;
3357 if (unknown_bytes) {
3358 stats.byte_count = UINT64_MAX;
3361 rule_collection_destroy(&rules);
3363 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3364 ofconn_send_reply(ofconn, reply);
3369 struct queue_stats_cbdata {
3370 struct ofport *ofport;
3371 struct list replies;
3376 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3377 const struct netdev_queue_stats *stats)
3379 struct ofputil_queue_stats oqs;
3381 oqs.port_no = cbdata->ofport->pp.port_no;
3382 oqs.queue_id = queue_id;
3383 oqs.tx_bytes = stats->tx_bytes;
3384 oqs.tx_packets = stats->tx_packets;
3385 oqs.tx_errors = stats->tx_errors;
3386 if (stats->created != LLONG_MIN) {
3387 calc_duration(stats->created, cbdata->now,
3388 &oqs.duration_sec, &oqs.duration_nsec);
3390 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3392 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3396 handle_queue_stats_dump_cb(uint32_t queue_id,
3397 struct netdev_queue_stats *stats,
3400 struct queue_stats_cbdata *cbdata = cbdata_;
3402 put_queue_stats(cbdata, queue_id, stats);
3406 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3407 struct queue_stats_cbdata *cbdata)
3409 cbdata->ofport = port;
3410 if (queue_id == OFPQ_ALL) {
3411 netdev_dump_queue_stats(port->netdev,
3412 handle_queue_stats_dump_cb, cbdata);
3414 struct netdev_queue_stats stats;
3416 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3417 put_queue_stats(cbdata, queue_id, &stats);
3419 return OFPERR_OFPQOFC_BAD_QUEUE;
3426 handle_queue_stats_request(struct ofconn *ofconn,
3427 const struct ofp_header *rq)
3429 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3430 struct queue_stats_cbdata cbdata;
3431 struct ofport *port;
3433 struct ofputil_queue_stats_request oqsr;
3435 COVERAGE_INC(ofproto_queue_req);
3437 ofpmp_init(&cbdata.replies, rq);
3438 cbdata.now = time_msec();
3440 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3445 if (oqsr.port_no == OFPP_ANY) {
3446 error = OFPERR_OFPQOFC_BAD_QUEUE;
3447 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3448 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3453 port = ofproto_get_port(ofproto, oqsr.port_no);
3455 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3456 : OFPERR_OFPQOFC_BAD_PORT);
3459 ofconn_send_replies(ofconn, &cbdata.replies);
3461 ofpbuf_list_delete(&cbdata.replies);
3468 is_flow_deletion_pending(const struct ofproto *ofproto,
3469 const struct cls_rule *cls_rule,
3472 if (!hmap_is_empty(&ofproto->deletions)) {
3473 struct ofoperation *op;
3475 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3476 cls_rule_hash(cls_rule, table_id),
3477 &ofproto->deletions) {
3478 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3488 evict_rule_from_table(struct ofproto *ofproto, struct oftable *table)
3493 ovs_rwlock_rdlock(&table->cls.rwlock);
3494 n_rules = classifier_count(&table->cls);
3495 ovs_rwlock_unlock(&table->cls.rwlock);
3497 if (n_rules < table->max_flows) {
3499 } else if (!choose_rule_to_evict(table, &rule)) {
3500 return OFPERR_OFPFMFC_TABLE_FULL;
3501 } else if (rule->pending) {
3502 ovs_rwlock_unlock(&rule->rwlock);
3503 return OFPROTO_POSTPONE;
3505 struct ofopgroup *group;
3507 group = ofopgroup_create_unattached(ofproto);
3508 delete_flow__(rule, group, OFPRR_EVICTION);
3509 ofopgroup_submit(group);
3515 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3516 * in which no matching flow already exists in the flow table.
3518 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3519 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3520 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3521 * initiated now but may be retried later.
3523 * The caller retains ownership of 'fm->ofpacts'.
3525 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3528 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3529 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3531 struct oftable *table;
3532 struct ofopgroup *group;
3538 error = check_table_id(ofproto, fm->table_id);
3544 if (fm->table_id == 0xff) {
3545 if (ofproto->ofproto_class->rule_choose_table) {
3546 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3552 ovs_assert(table_id < ofproto->n_tables);
3556 } else if (fm->table_id < ofproto->n_tables) {
3557 table_id = fm->table_id;
3559 return OFPERR_OFPBRC_BAD_TABLE_ID;
3562 table = &ofproto->tables[table_id];
3564 if (table->flags & OFTABLE_READONLY) {
3565 return OFPERR_OFPBRC_EPERM;
3568 cls_rule_init(&cr, &fm->match, fm->priority);
3570 /* Transform "add" into "modify" if there's an existing identical flow. */
3571 ovs_rwlock_rdlock(&table->cls.rwlock);
3572 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
3573 ovs_rwlock_unlock(&table->cls.rwlock);
3575 cls_rule_destroy(&cr);
3576 if (!rule_is_modifiable(rule)) {
3577 return OFPERR_OFPBRC_EPERM;
3578 } else if (rule->pending) {
3579 return OFPROTO_POSTPONE;
3581 struct rule_collection rules;
3583 rule_collection_init(&rules);
3584 rule_collection_add(&rules, rule);
3585 fm->modify_cookie = true;
3586 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
3587 rule_collection_destroy(&rules);
3593 /* Verify actions. */
3594 error = ofproto_check_ofpacts(ofproto, fm->ofpacts, fm->ofpacts_len,
3595 &fm->match.flow, table_id);
3597 cls_rule_destroy(&cr);
3601 /* Serialize against pending deletion. */
3602 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
3603 cls_rule_destroy(&cr);
3604 return OFPROTO_POSTPONE;
3607 /* Check for overlap, if requested. */
3608 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
3611 ovs_rwlock_rdlock(&table->cls.rwlock);
3612 overlaps = classifier_rule_overlaps(&table->cls, &cr);
3613 ovs_rwlock_unlock(&table->cls.rwlock);
3616 cls_rule_destroy(&cr);
3617 return OFPERR_OFPFMFC_OVERLAP;
3621 /* If necessary, evict an existing rule to clear out space. */
3622 error = evict_rule_from_table(ofproto, table);
3624 cls_rule_destroy(&cr);
3628 /* Allocate new rule. */
3629 rule = ofproto->ofproto_class->rule_alloc();
3631 cls_rule_destroy(&cr);
3632 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
3633 ofproto->name, ovs_strerror(error));
3637 /* Initialize base state. */
3638 rule->ofproto = ofproto;
3639 cls_rule_move(&rule->cr, &cr);
3640 rule->pending = NULL;
3641 rule->flow_cookie = fm->new_cookie;
3642 rule->created = rule->modified = rule->used = time_msec();
3644 ovs_mutex_init(&rule->timeout_mutex);
3645 ovs_mutex_lock(&rule->timeout_mutex);
3646 rule->idle_timeout = fm->idle_timeout;
3647 rule->hard_timeout = fm->hard_timeout;
3648 ovs_mutex_unlock(&rule->timeout_mutex);
3650 rule->table_id = table - ofproto->tables;
3651 rule->flags = fm->flags & OFPUTIL_FF_STATE;
3653 rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
3654 rule->ofpacts_len = fm->ofpacts_len;
3655 rule->meter_id = ofpacts_get_meter(rule->ofpacts, rule->ofpacts_len);
3656 list_init(&rule->meter_list_node);
3657 rule->eviction_group = NULL;
3658 list_init(&rule->expirable);
3659 rule->monitor_flags = 0;
3660 rule->add_seqno = 0;
3661 rule->modify_seqno = 0;
3662 ovs_rwlock_init(&rule->rwlock);
3664 /* Construct rule, initializing derived state. */
3665 error = ofproto->ofproto_class->rule_construct(rule);
3667 ofproto_rule_destroy__(rule);
3672 oftable_insert_rule(rule);
3674 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3675 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
3676 ofproto->ofproto_class->rule_insert(rule);
3677 ofopgroup_submit(group);
3682 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3684 /* Modifies the rules listed in 'rules', changing their actions to match those
3687 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3690 * Returns 0 on success, otherwise an OpenFlow error code. */
3692 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
3693 struct ofputil_flow_mod *fm, const struct ofp_header *request,
3694 const struct rule_collection *rules)
3696 enum ofoperation_type type;
3697 struct ofopgroup *group;
3701 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
3702 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3703 error = OFPERR_OFPBRC_EPERM;
3704 for (i = 0; i < rules->n; i++) {
3705 struct rule *rule = rules->rules[i];
3706 struct ofoperation *op;
3707 bool actions_changed;
3708 bool reset_counters;
3710 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
3712 if (rule_is_modifiable(rule)) {
3713 /* At least one rule is modifiable, don't report EPERM error. */
3719 /* Verify actions. */
3720 error = ofpacts_check(fm->ofpacts, fm->ofpacts_len, &fm->match.flow,
3721 u16_to_ofp(ofproto->max_ports), rule->table_id);
3726 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
3727 rule->ofpacts, rule->ofpacts_len);
3729 op = ofoperation_create(group, rule, type, 0);
3731 if (fm->modify_cookie && fm->new_cookie != htonll(UINT64_MAX)) {
3732 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
3734 if (type == OFOPERATION_REPLACE) {
3735 ovs_mutex_lock(&rule->timeout_mutex);
3736 rule->idle_timeout = fm->idle_timeout;
3737 rule->hard_timeout = fm->hard_timeout;
3738 ovs_mutex_unlock(&rule->timeout_mutex);
3740 rule->flags = fm->flags & OFPUTIL_FF_STATE;
3741 if (fm->idle_timeout || fm->hard_timeout) {
3742 if (!rule->eviction_group) {
3743 eviction_group_add_rule(rule);
3746 eviction_group_remove_rule(rule);
3750 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
3751 if (actions_changed || reset_counters) {
3752 op->ofpacts = rule->ofpacts;
3753 op->ofpacts_len = rule->ofpacts_len;
3754 op->meter_id = rule->meter_id;
3756 ovs_rwlock_wrlock(&rule->rwlock);
3757 rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
3758 rule->ofpacts_len = fm->ofpacts_len;
3759 ovs_rwlock_unlock(&rule->rwlock);
3761 rule->meter_id = ofpacts_get_meter(rule->ofpacts,
3763 rule->ofproto->ofproto_class->rule_modify_actions(rule,
3766 ofoperation_complete(op, 0);
3769 ofopgroup_submit(group);
3775 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
3776 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3778 if (fm->cookie_mask != htonll(0) || fm->new_cookie == htonll(UINT64_MAX)) {
3781 return add_flow(ofproto, ofconn, fm, request);
3784 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
3787 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3790 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
3791 struct ofputil_flow_mod *fm,
3792 const struct ofp_header *request)
3794 struct rule_criteria criteria;
3795 struct rule_collection rules;
3798 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
3799 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
3800 error = collect_rules_loose(ofproto, &criteria, &rules);
3801 rule_criteria_destroy(&criteria);
3804 error = (rules.n > 0
3805 ? modify_flows__(ofproto, ofconn, fm, request, &rules)
3806 : modify_flows_add(ofproto, ofconn, fm, request));
3809 rule_collection_destroy(&rules);
3814 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3817 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3820 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3821 struct ofputil_flow_mod *fm,
3822 const struct ofp_header *request)
3824 struct rule_criteria criteria;
3825 struct rule_collection rules;
3828 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
3829 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
3830 error = collect_rules_strict(ofproto, &criteria, &rules);
3831 rule_criteria_destroy(&criteria);
3835 error = modify_flows_add(ofproto, ofconn, fm, request);
3836 } else if (rules.n == 1) {
3837 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
3841 rule_collection_destroy(&rules);
3846 /* OFPFC_DELETE implementation. */
3849 delete_flow__(struct rule *rule, struct ofopgroup *group,
3850 enum ofp_flow_removed_reason reason)
3852 struct ofproto *ofproto = rule->ofproto;
3854 ofproto_rule_send_removed(rule, reason);
3856 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
3857 oftable_remove_rule(rule);
3858 ofproto->ofproto_class->rule_delete(rule);
3861 /* Deletes the rules listed in 'rules'.
3863 * Returns 0 on success, otherwise an OpenFlow error code. */
3865 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
3866 const struct ofp_header *request,
3867 const struct rule_collection *rules,
3868 enum ofp_flow_removed_reason reason)
3870 struct ofopgroup *group;
3873 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
3874 for (i = 0; i < rules->n; i++) {
3875 struct rule *rule = rules->rules[i];
3876 ovs_rwlock_wrlock(&rule->rwlock);
3877 delete_flow__(rule, group, reason);
3879 ofopgroup_submit(group);
3884 /* Implements OFPFC_DELETE. */
3886 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
3887 const struct ofputil_flow_mod *fm,
3888 const struct ofp_header *request)
3890 struct rule_criteria criteria;
3891 struct rule_collection rules;
3894 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
3895 fm->cookie, fm->cookie_mask,
3896 fm->out_port, fm->out_group);
3897 error = collect_rules_loose(ofproto, &criteria, &rules);
3898 rule_criteria_destroy(&criteria);
3900 if (!error && rules.n > 0) {
3901 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
3903 rule_collection_destroy(&rules);
3908 /* Implements OFPFC_DELETE_STRICT. */
3910 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3911 const struct ofputil_flow_mod *fm,
3912 const struct ofp_header *request)
3914 struct rule_criteria criteria;
3915 struct rule_collection rules;
3918 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
3919 fm->cookie, fm->cookie_mask,
3920 fm->out_port, fm->out_group);
3921 error = collect_rules_strict(ofproto, &criteria, &rules);
3922 rule_criteria_destroy(&criteria);
3924 if (!error && rules.n > 0) {
3925 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
3927 rule_collection_destroy(&rules);
3933 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
3935 struct ofputil_flow_removed fr;
3937 if (ofproto_rule_is_hidden(rule) ||
3938 !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
3942 minimatch_expand(&rule->cr.match, &fr.match);
3943 fr.priority = rule->cr.priority;
3944 fr.cookie = rule->flow_cookie;
3946 fr.table_id = rule->table_id;
3947 calc_duration(rule->created, time_msec(),
3948 &fr.duration_sec, &fr.duration_nsec);
3949 ovs_mutex_lock(&rule->timeout_mutex);
3950 fr.idle_timeout = rule->idle_timeout;
3951 fr.hard_timeout = rule->hard_timeout;
3952 ovs_mutex_unlock(&rule->timeout_mutex);
3953 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
3956 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
3959 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
3960 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
3963 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
3966 * ofproto implementation ->run() functions should use this function to expire
3967 * OpenFlow flows. */
3969 ofproto_rule_expire(struct rule *rule, uint8_t reason)
3971 struct ofproto *ofproto = rule->ofproto;
3972 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
3974 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
3975 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
3976 ofproto_rule_send_removed(rule, reason);
3978 ovs_rwlock_wrlock(&cls->rwlock);
3979 ofproto_rule_delete(ofproto, cls, rule);
3980 ovs_rwlock_unlock(&cls->rwlock);
3983 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
3984 * means "infinite". */
3986 reduce_timeout(uint16_t max, uint16_t *timeout)
3988 if (max && (!*timeout || *timeout > max)) {
3993 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
3994 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
3995 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
3997 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
3999 ofproto_rule_reduce_timeouts(struct rule *rule,
4000 uint16_t idle_timeout, uint16_t hard_timeout)
4001 OVS_EXCLUDED(rule->ofproto->expirable_mutex, rule->timeout_mutex)
4003 if (!idle_timeout && !hard_timeout) {
4007 ovs_mutex_lock(&rule->ofproto->expirable_mutex);
4008 if (list_is_empty(&rule->expirable)) {
4009 list_insert(&rule->ofproto->expirable, &rule->expirable);
4011 ovs_mutex_unlock(&rule->ofproto->expirable_mutex);
4013 ovs_mutex_lock(&rule->timeout_mutex);
4014 reduce_timeout(idle_timeout, &rule->idle_timeout);
4015 reduce_timeout(hard_timeout, &rule->hard_timeout);
4016 ovs_mutex_unlock(&rule->timeout_mutex);
4020 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4022 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4023 struct ofputil_flow_mod fm;
4024 uint64_t ofpacts_stub[1024 / 8];
4025 struct ofpbuf ofpacts;
4029 error = reject_slave_controller(ofconn);
4034 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4035 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
4038 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
4041 goto exit_free_ofpacts;
4044 /* Record the operation for logging a summary report. */
4045 switch (fm.command) {
4051 case OFPFC_MODIFY_STRICT:
4052 ofproto->n_modify++;
4056 case OFPFC_DELETE_STRICT:
4057 ofproto->n_delete++;
4062 if (ofproto->next_op_report == LLONG_MAX) {
4063 ofproto->first_op = now;
4064 ofproto->next_op_report = MAX(now + 10 * 1000,
4065 ofproto->op_backoff);
4066 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
4068 ofproto->last_op = now;
4071 ofpbuf_uninit(&ofpacts);
4077 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
4078 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
4080 if (ofproto->n_pending >= 50) {
4081 ovs_assert(!list_is_empty(&ofproto->pending));
4082 return OFPROTO_POSTPONE;
4085 switch (fm->command) {
4087 return add_flow(ofproto, ofconn, fm, oh);
4090 return modify_flows_loose(ofproto, ofconn, fm, oh);
4092 case OFPFC_MODIFY_STRICT:
4093 return modify_flow_strict(ofproto, ofconn, fm, oh);
4096 return delete_flows_loose(ofproto, ofconn, fm, oh);
4098 case OFPFC_DELETE_STRICT:
4099 return delete_flow_strict(ofproto, ofconn, fm, oh);
4102 if (fm->command > 0xff) {
4103 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4104 "flow_mod_table_id extension is not enabled",
4107 return OFPERR_OFPFMFC_BAD_COMMAND;
4112 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4114 struct ofputil_role_request request;
4115 struct ofputil_role_request reply;
4119 error = ofputil_decode_role_message(oh, &request);
4124 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4125 if (ofconn_get_role(ofconn) != request.role
4126 && ofconn_has_pending_opgroups(ofconn)) {
4127 return OFPROTO_POSTPONE;
4130 if (request.have_generation_id
4131 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4132 return OFPERR_OFPRRFC_STALE;
4135 ofconn_set_role(ofconn, request.role);
4138 reply.role = ofconn_get_role(ofconn);
4139 reply.have_generation_id = ofconn_get_master_election_id(
4140 ofconn, &reply.generation_id);
4141 buf = ofputil_encode_role_reply(oh, &reply);
4142 ofconn_send_reply(ofconn, buf);
4148 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4149 const struct ofp_header *oh)
4151 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4152 enum ofputil_protocol cur, next;
4154 cur = ofconn_get_protocol(ofconn);
4155 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4156 ofconn_set_protocol(ofconn, next);
4162 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4164 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4165 enum ofputil_protocol cur, next;
4166 enum ofputil_protocol next_base;
4168 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4170 return OFPERR_OFPBRC_EPERM;
4173 cur = ofconn_get_protocol(ofconn);
4174 next = ofputil_protocol_set_base(cur, next_base);
4175 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4176 /* Avoid sending async messages in surprising protocol. */
4177 return OFPROTO_POSTPONE;
4180 ofconn_set_protocol(ofconn, next);
4185 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4186 const struct ofp_header *oh)
4188 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4191 format = ntohl(msg->format);
4192 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4193 return OFPERR_OFPBRC_EPERM;
4196 if (format != ofconn_get_packet_in_format(ofconn)
4197 && ofconn_has_pending_opgroups(ofconn)) {
4198 /* Avoid sending async message in surprsing packet in format. */
4199 return OFPROTO_POSTPONE;
4202 ofconn_set_packet_in_format(ofconn, format);
4207 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4209 const struct nx_async_config *msg = ofpmsg_body(oh);
4210 uint32_t master[OAM_N_TYPES];
4211 uint32_t slave[OAM_N_TYPES];
4213 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4214 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4215 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4217 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4218 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4219 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4221 ofconn_set_async_config(ofconn, master, slave);
4222 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4223 !ofconn_get_miss_send_len(ofconn)) {
4224 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4231 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4234 uint32_t master[OAM_N_TYPES];
4235 uint32_t slave[OAM_N_TYPES];
4236 struct nx_async_config *msg;
4238 ofconn_get_async_config(ofconn, master, slave);
4239 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4240 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4242 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4243 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4244 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4246 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4247 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4248 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4250 ofconn_send_reply(ofconn, buf);
4256 handle_nxt_set_controller_id(struct ofconn *ofconn,
4257 const struct ofp_header *oh)
4259 const struct nx_controller_id *nci = ofpmsg_body(oh);
4261 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4262 return OFPERR_NXBRC_MUST_BE_ZERO;
4265 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4270 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4274 if (ofconn_has_pending_opgroups(ofconn)) {
4275 return OFPROTO_POSTPONE;
4278 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4279 ? OFPRAW_OFPT10_BARRIER_REPLY
4280 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4281 ofconn_send_reply(ofconn, buf);
4286 ofproto_compose_flow_refresh_update(const struct rule *rule,
4287 enum nx_flow_monitor_flags flags,
4290 struct ofoperation *op = rule->pending;
4291 struct ofputil_flow_update fu;
4294 if (op && op->type == OFOPERATION_ADD) {
4295 /* We'll report the final flow when the operation completes. Reporting
4296 * it now would cause a duplicate report later. */
4300 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4301 ? NXFME_ADDED : NXFME_MODIFIED);
4303 ovs_mutex_lock(&rule->timeout_mutex);
4304 fu.idle_timeout = rule->idle_timeout;
4305 fu.hard_timeout = rule->hard_timeout;
4306 ovs_mutex_unlock(&rule->timeout_mutex);
4307 fu.table_id = rule->table_id;
4308 fu.cookie = rule->flow_cookie;
4309 minimatch_expand(&rule->cr.match, &match);
4311 fu.priority = rule->cr.priority;
4312 if (!(flags & NXFMF_ACTIONS)) {
4316 fu.ofpacts = rule->ofpacts;
4317 fu.ofpacts_len = rule->ofpacts_len;
4319 /* An operation is in progress. Use the previous version of the flow's
4320 * actions, so that when the operation commits we report the change. */
4322 case OFOPERATION_ADD:
4325 case OFOPERATION_MODIFY:
4326 case OFOPERATION_REPLACE:
4328 fu.ofpacts = op->ofpacts;
4329 fu.ofpacts_len = op->ofpacts_len;
4331 fu.ofpacts = rule->ofpacts;
4332 fu.ofpacts_len = rule->ofpacts_len;
4336 case OFOPERATION_DELETE:
4337 fu.ofpacts = rule->ofpacts;
4338 fu.ofpacts_len = rule->ofpacts_len;
4346 if (list_is_empty(msgs)) {
4347 ofputil_start_flow_update(msgs);
4349 ofputil_append_flow_update(&fu, msgs);
4353 ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4358 for (i = 0; i < rules->n; i++) {
4359 struct rule *rule = rules->rules[i];
4360 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4361 rule->monitor_flags = 0;
4363 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4368 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4369 struct rule *rule, uint64_t seqno,
4370 struct rule_collection *rules)
4372 enum nx_flow_monitor_flags update;
4374 if (ofproto_rule_is_hidden(rule)) {
4379 ? ofoperation_has_out_port(rule->pending, m->out_port)
4380 : ofproto_rule_has_out_port(rule, m->out_port))) {
4385 if (rule->add_seqno > seqno) {
4386 update = NXFMF_ADD | NXFMF_MODIFY;
4387 } else if (rule->modify_seqno > seqno) {
4388 update = NXFMF_MODIFY;
4393 if (!(m->flags & update)) {
4397 update = NXFMF_INITIAL;
4400 if (!rule->monitor_flags) {
4401 rule_collection_add(rules, rule);
4403 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4407 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4409 struct rule_collection *rules)
4411 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4412 const struct ofoperation *op;
4413 const struct oftable *table;
4414 struct cls_rule target;
4416 cls_rule_init_from_minimatch(&target, &m->match, 0);
4417 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4418 struct cls_cursor cursor;
4421 ovs_rwlock_rdlock(&table->cls.rwlock);
4422 cls_cursor_init(&cursor, &table->cls, &target);
4423 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4424 ovs_assert(!rule->pending); /* XXX */
4425 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4427 ovs_rwlock_unlock(&table->cls.rwlock);
4430 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4431 struct rule *rule = op->rule;
4433 if (((m->table_id == 0xff
4434 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4435 : m->table_id == rule->table_id))
4436 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4437 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4440 cls_rule_destroy(&target);
4444 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4445 struct rule_collection *rules)
4447 if (m->flags & NXFMF_INITIAL) {
4448 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4453 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4454 uint64_t seqno, struct rule_collection *rules)
4456 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4460 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4462 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4463 struct ofmonitor **monitors;
4464 size_t n_monitors, allocated_monitors;
4465 struct rule_collection rules;
4466 struct list replies;
4472 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4474 n_monitors = allocated_monitors = 0;
4476 struct ofputil_flow_monitor_request request;
4477 struct ofmonitor *m;
4480 retval = ofputil_decode_flow_monitor_request(&request, &b);
4481 if (retval == EOF) {
4483 } else if (retval) {
4488 if (request.table_id != 0xff
4489 && request.table_id >= ofproto->n_tables) {
4490 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4494 error = ofmonitor_create(&request, ofconn, &m);
4499 if (n_monitors >= allocated_monitors) {
4500 monitors = x2nrealloc(monitors, &allocated_monitors,
4503 monitors[n_monitors++] = m;
4506 rule_collection_init(&rules);
4507 for (i = 0; i < n_monitors; i++) {
4508 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4511 ofpmp_init(&replies, oh);
4512 ofmonitor_compose_refresh_updates(&rules, &replies);
4513 rule_collection_destroy(&rules);
4515 ofconn_send_replies(ofconn, &replies);
4522 for (i = 0; i < n_monitors; i++) {
4523 ofmonitor_destroy(monitors[i]);
4530 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4532 struct ofmonitor *m;
4535 id = ofputil_decode_flow_monitor_cancel(oh);
4536 m = ofmonitor_lookup(ofconn, id);
4538 return OFPERR_NXBRC_FM_BAD_ID;
4541 ofmonitor_destroy(m);
4545 /* Meters implementation.
4547 * Meter table entry, indexed by the OpenFlow meter_id.
4548 * These are always dynamically allocated to allocate enough space for
4550 * 'created' is used to compute the duration for meter stats.
4551 * 'list rules' is needed so that we can delete the dependent rules when the
4552 * meter table entry is deleted.
4553 * 'provider_meter_id' is for the provider's private use.
4556 long long int created; /* Time created. */
4557 struct list rules; /* List of "struct rule_dpif"s. */
4558 ofproto_meter_id provider_meter_id;
4559 uint16_t flags; /* Meter flags. */
4560 uint16_t n_bands; /* Number of meter bands. */
4561 struct ofputil_meter_band *bands;
4565 * This is used in instruction validation at flow set-up time,
4566 * as flows may not use non-existing meters.
4567 * This is also used by ofproto-providers to translate OpenFlow meter_ids
4568 * in METER instructions to the corresponding provider meter IDs.
4569 * Return value of UINT32_MAX signifies an invalid meter.
4572 ofproto_get_provider_meter_id(const struct ofproto * ofproto,
4573 uint32_t of_meter_id)
4575 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
4576 const struct meter *meter = ofproto->meters[of_meter_id];
4578 return meter->provider_meter_id.uint32;
4585 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
4589 meter->flags = config->flags;
4590 meter->n_bands = config->n_bands;
4591 meter->bands = xmemdup(config->bands,
4592 config->n_bands * sizeof *meter->bands);
4595 static struct meter *
4596 meter_create(const struct ofputil_meter_config *config,
4597 ofproto_meter_id provider_meter_id)
4599 struct meter *meter;
4601 meter = xzalloc(sizeof *meter);
4602 meter->provider_meter_id = provider_meter_id;
4603 meter->created = time_msec();
4604 list_init(&meter->rules);
4606 meter_update(meter, config);
4612 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
4615 for (mid = first; mid <= last; ++mid) {
4616 struct meter *meter = ofproto->meters[mid];
4618 ofproto->meters[mid] = NULL;
4619 ofproto->ofproto_class->meter_del(ofproto,
4620 meter->provider_meter_id);
4628 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4630 ofproto_meter_id provider_meter_id = { UINT32_MAX };
4631 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
4635 return OFPERR_OFPMMFC_METER_EXISTS;
4638 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
4641 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
4642 *meterp = meter_create(&mm->meter, provider_meter_id);
4648 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4650 struct meter *meter = ofproto->meters[mm->meter.meter_id];
4654 return OFPERR_OFPMMFC_UNKNOWN_METER;
4657 error = ofproto->ofproto_class->meter_set(ofproto,
4658 &meter->provider_meter_id,
4660 ovs_assert(meter->provider_meter_id.uint32 != UINT32_MAX);
4662 meter_update(meter, &mm->meter);
4668 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
4669 struct ofputil_meter_mod *mm)
4671 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4672 uint32_t meter_id = mm->meter.meter_id;
4673 struct rule_collection rules;
4674 enum ofperr error = 0;
4675 uint32_t first, last;
4677 if (meter_id == OFPM13_ALL) {
4679 last = ofproto->meter_features.max_meters;
4681 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
4684 first = last = meter_id;
4687 /* First delete the rules that use this meter. If any of those rules are
4688 * currently being modified, postpone the whole operation until later. */
4689 rule_collection_init(&rules);
4690 for (meter_id = first; meter_id <= last; ++meter_id) {
4691 struct meter *meter = ofproto->meters[meter_id];
4692 if (meter && !list_is_empty(&meter->rules)) {
4695 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
4696 if (rule->pending) {
4697 error = OFPROTO_POSTPONE;
4700 rule_collection_add(&rules, rule);
4705 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
4708 /* Delete the meters. */
4709 meter_delete(ofproto, first, last);
4712 rule_collection_destroy(&rules);
4718 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4720 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4721 struct ofputil_meter_mod mm;
4722 uint64_t bands_stub[256 / 8];
4723 struct ofpbuf bands;
4727 error = reject_slave_controller(ofconn);
4732 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4734 error = ofputil_decode_meter_mod(oh, &mm, &bands);
4736 goto exit_free_bands;
4739 meter_id = mm.meter.meter_id;
4741 if (mm.command != OFPMC13_DELETE) {
4742 /* Fails also when meters are not implemented by the provider. */
4743 if (meter_id == 0 || meter_id > OFPM13_MAX) {
4744 error = OFPERR_OFPMMFC_INVALID_METER;
4745 goto exit_free_bands;
4746 } else if (meter_id > ofproto->meter_features.max_meters) {
4747 error = OFPERR_OFPMMFC_OUT_OF_METERS;
4748 goto exit_free_bands;
4750 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
4751 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
4752 goto exit_free_bands;
4756 switch (mm.command) {
4758 error = handle_add_meter(ofproto, &mm);
4761 case OFPMC13_MODIFY:
4762 error = handle_modify_meter(ofproto, &mm);
4765 case OFPMC13_DELETE:
4766 error = handle_delete_meter(ofconn, oh, &mm);
4770 error = OFPERR_OFPMMFC_BAD_COMMAND;
4775 ofpbuf_uninit(&bands);
4780 handle_meter_features_request(struct ofconn *ofconn,
4781 const struct ofp_header *request)
4783 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4784 struct ofputil_meter_features features;
4787 if (ofproto->ofproto_class->meter_get_features) {
4788 ofproto->ofproto_class->meter_get_features(ofproto, &features);
4790 memset(&features, 0, sizeof features);
4792 b = ofputil_encode_meter_features_reply(&features, request);
4794 ofconn_send_reply(ofconn, b);
4799 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
4802 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4803 struct list replies;
4804 uint64_t bands_stub[256 / 8];
4805 struct ofpbuf bands;
4806 uint32_t meter_id, first, last;
4808 ofputil_decode_meter_request(request, &meter_id);
4810 if (meter_id == OFPM13_ALL) {
4812 last = ofproto->meter_features.max_meters;
4814 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
4815 !ofproto->meters[meter_id]) {
4816 return OFPERR_OFPMMFC_UNKNOWN_METER;
4818 first = last = meter_id;
4821 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4822 ofpmp_init(&replies, request);
4824 for (meter_id = first; meter_id <= last; ++meter_id) {
4825 struct meter *meter = ofproto->meters[meter_id];
4827 continue; /* Skip non-existing meters. */
4829 if (type == OFPTYPE_METER_STATS_REQUEST) {
4830 struct ofputil_meter_stats stats;
4832 stats.meter_id = meter_id;
4834 /* Provider sets the packet and byte counts, we do the rest. */
4835 stats.flow_count = list_size(&meter->rules);
4836 calc_duration(meter->created, time_msec(),
4837 &stats.duration_sec, &stats.duration_nsec);
4838 stats.n_bands = meter->n_bands;
4839 ofpbuf_clear(&bands);
4841 = ofpbuf_put_uninit(&bands,
4842 meter->n_bands * sizeof *stats.bands);
4844 if (!ofproto->ofproto_class->meter_get(ofproto,
4845 meter->provider_meter_id,
4847 ofputil_append_meter_stats(&replies, &stats);
4849 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
4850 struct ofputil_meter_config config;
4852 config.meter_id = meter_id;
4853 config.flags = meter->flags;
4854 config.n_bands = meter->n_bands;
4855 config.bands = meter->bands;
4856 ofputil_append_meter_config(&replies, &config);
4860 ofconn_send_replies(ofconn, &replies);
4861 ofpbuf_uninit(&bands);
4866 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
4867 struct ofgroup **group)
4868 OVS_TRY_RDLOCK(true, (*group)->rwlock)
4870 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4871 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
4872 hash_int(group_id, 0), &ofproto->groups) {
4873 if ((*group)->group_id == group_id) {
4874 ovs_rwlock_rdlock(&(*group)->rwlock);
4875 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4879 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4884 ofproto_group_release(struct ofgroup *group)
4885 OVS_RELEASES(group->rwlock)
4887 ovs_rwlock_unlock(&group->rwlock);
4891 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
4892 struct ofgroup **group)
4893 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
4894 OVS_TRY_WRLOCK(true, (*group)->rwlock)
4896 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
4897 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
4898 hash_int(group_id, 0), &ofproto->groups) {
4899 if ((*group)->group_id == group_id) {
4900 ovs_rwlock_wrlock(&(*group)->rwlock);
4904 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4909 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
4910 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
4912 struct ofgroup *grp;
4914 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
4915 hash_int(group_id, 0), &ofproto->groups) {
4916 if (grp->group_id == group_id) {
4924 append_group_stats(struct ofgroup *group, struct list *replies)
4925 OVS_REQ_RDLOCK(group->rwlock)
4927 struct ofputil_group_stats ogs;
4928 struct ofproto *ofproto = group->ofproto;
4929 long long int now = time_msec();
4932 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
4934 error = (ofproto->ofproto_class->group_get_stats
4935 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
4938 ogs.ref_count = UINT32_MAX;
4939 ogs.packet_count = UINT64_MAX;
4940 ogs.byte_count = UINT64_MAX;
4941 ogs.n_buckets = group->n_buckets;
4942 memset(ogs.bucket_stats, 0xff,
4943 ogs.n_buckets * sizeof *ogs.bucket_stats);
4946 ogs.group_id = group->group_id;
4947 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
4949 ofputil_append_group_stats(replies, &ogs);
4951 free(ogs.bucket_stats);
4955 handle_group_stats_request(struct ofconn *ofconn,
4956 const struct ofp_header *request)
4958 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4959 struct list replies;
4961 struct ofgroup *group;
4964 error = ofputil_decode_group_stats_request(request, &group_id);
4969 ofpmp_init(&replies, request);
4971 if (group_id == OFPG_ALL) {
4972 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4973 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
4974 ovs_rwlock_rdlock(&group->rwlock);
4975 append_group_stats(group, &replies);
4976 ovs_rwlock_unlock(&group->rwlock);
4978 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4980 if (ofproto_group_lookup(ofproto, group_id, &group)) {
4981 append_group_stats(group, &replies);
4982 ofproto_group_release(group);
4986 ofconn_send_replies(ofconn, &replies);
4992 handle_group_desc_stats_request(struct ofconn *ofconn,
4993 const struct ofp_header *request)
4995 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4996 struct list replies;
4997 struct ofputil_group_desc gds;
4998 struct ofgroup *group;
5000 ofpmp_init(&replies, request);
5002 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5003 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5004 gds.group_id = group->group_id;
5005 gds.type = group->type;
5006 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
5008 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5010 ofconn_send_replies(ofconn, &replies);
5016 handle_group_features_stats_request(struct ofconn *ofconn,
5017 const struct ofp_header *request)
5019 struct ofproto *p = ofconn_get_ofproto(ofconn);
5022 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5024 ofconn_send_reply(ofconn, msg);
5030 /* Implements OFPGC11_ADD
5031 * in which no matching flow already exists in the flow table.
5033 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
5034 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
5035 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
5036 * initiated now but may be retried later.
5038 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
5039 * ownership remains with the caller.
5041 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5044 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5046 struct ofgroup *ofgroup;
5049 if (gm->group_id > OFPG_MAX) {
5050 return OFPERR_OFPGMFC_INVALID_GROUP;
5052 if (gm->type > OFPGT11_FF) {
5053 return OFPERR_OFPGMFC_BAD_TYPE;
5056 /* Allocate new group and initialize it. */
5057 ofgroup = ofproto->ofproto_class->group_alloc();
5059 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
5060 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5063 ovs_rwlock_init(&ofgroup->rwlock);
5064 ofgroup->ofproto = ofproto;
5065 ofgroup->group_id = gm->group_id;
5066 ofgroup->type = gm->type;
5067 ofgroup->created = ofgroup->modified = time_msec();
5069 list_move(&ofgroup->buckets, &gm->buckets);
5070 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5072 /* Construct called BEFORE any locks are held. */
5073 error = ofproto->ofproto_class->group_construct(ofgroup);
5078 /* We wrlock as late as possible to minimize the time we jam any other
5079 * threads: No visible state changes before acquiring the lock. */
5080 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5082 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5083 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5087 if (ofproto_group_exists(ofproto, gm->group_id)) {
5088 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5093 /* Insert new group. */
5094 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5095 hash_int(ofgroup->group_id, 0));
5096 ofproto->n_groups[ofgroup->type]++;
5098 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5103 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5104 ofproto->ofproto_class->group_destruct(ofgroup);
5106 ofputil_bucket_list_destroy(&ofgroup->buckets);
5107 ofproto->ofproto_class->group_dealloc(ofgroup);
5112 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5115 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5118 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5120 struct ofgroup *ofgroup;
5121 struct ofgroup *victim;
5124 if (gm->group_id > OFPG_MAX) {
5125 return OFPERR_OFPGMFC_INVALID_GROUP;
5128 if (gm->type > OFPGT11_FF) {
5129 return OFPERR_OFPGMFC_BAD_TYPE;
5132 victim = ofproto->ofproto_class->group_alloc();
5134 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5135 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5138 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
5139 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5142 /* Both group's and its container's write locks held now.
5143 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5144 if (ofgroup->type != gm->type
5145 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5146 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5151 list_move(&victim->buckets, &ofgroup->buckets);
5153 ofgroup->type = gm->type;
5154 list_move(&ofgroup->buckets, &gm->buckets);
5155 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5157 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
5159 ofputil_bucket_list_destroy(&victim->buckets);
5160 ofproto->n_groups[victim->type]--;
5161 ofproto->n_groups[ofgroup->type]++;
5162 ofgroup->modified = time_msec();
5164 ofputil_bucket_list_destroy(&ofgroup->buckets);
5167 list_move(&ofgroup->buckets, &victim->buckets);
5171 ovs_rwlock_unlock(&ofgroup->rwlock);
5172 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5174 ofproto->ofproto_class->group_dealloc(victim);
5179 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5180 OVS_RELEASES(ofproto->groups_rwlock)
5182 /* Must wait until existing readers are done,
5183 * while holding the container's write lock at the same time. */
5184 ovs_rwlock_wrlock(&ofgroup->rwlock);
5185 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5186 /* No-one can find this group any more. */
5187 ofproto->n_groups[ofgroup->type]--;
5188 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5190 ofproto->ofproto_class->group_destruct(ofgroup);
5191 ofputil_bucket_list_destroy(&ofgroup->buckets);
5192 ovs_rwlock_unlock(&ofgroup->rwlock);
5193 ovs_rwlock_destroy(&ofgroup->rwlock);
5194 ofproto->ofproto_class->group_dealloc(ofgroup);
5197 /* Implements OFPGC_DELETE. */
5199 delete_group(struct ofproto *ofproto, uint32_t group_id)
5201 struct ofgroup *ofgroup;
5203 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5204 if (group_id == OFPG_ALL) {
5206 struct hmap_node *node = hmap_first(&ofproto->groups);
5210 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5211 delete_group__(ofproto, ofgroup);
5212 /* Lock for each node separately, so that we will not jam the
5213 * other threads for too long time. */
5214 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5217 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5218 hash_int(group_id, 0), &ofproto->groups) {
5219 if (ofgroup->group_id == group_id) {
5220 delete_group__(ofproto, ofgroup);
5225 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5229 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5231 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5232 struct ofputil_group_mod gm;
5235 error = reject_slave_controller(ofconn);
5240 error = ofputil_decode_group_mod(oh, &gm);
5245 switch (gm.command) {
5247 return add_group(ofproto, &gm);
5249 case OFPGC11_MODIFY:
5250 return modify_group(ofproto, &gm);
5252 case OFPGC11_DELETE:
5253 delete_group(ofproto, gm.group_id);
5257 if (gm.command > OFPGC11_DELETE) {
5258 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5259 ofproto->name, gm.command);
5261 return OFPERR_OFPGMFC_BAD_COMMAND;
5266 handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5268 struct ofputil_table_mod tm;
5271 error = reject_slave_controller(ofconn);
5276 error = ofputil_decode_table_mod(oh, &tm);
5281 /* XXX Actual table mod support is not implemented yet. */
5286 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5288 const struct ofp_header *oh = msg->data;
5292 error = ofptype_decode(&type, oh);
5298 /* OpenFlow requests. */
5299 case OFPTYPE_ECHO_REQUEST:
5300 return handle_echo_request(ofconn, oh);
5302 case OFPTYPE_FEATURES_REQUEST:
5303 return handle_features_request(ofconn, oh);
5305 case OFPTYPE_GET_CONFIG_REQUEST:
5306 return handle_get_config_request(ofconn, oh);
5308 case OFPTYPE_SET_CONFIG:
5309 return handle_set_config(ofconn, oh);
5311 case OFPTYPE_PACKET_OUT:
5312 return handle_packet_out(ofconn, oh);
5314 case OFPTYPE_PORT_MOD:
5315 return handle_port_mod(ofconn, oh);
5317 case OFPTYPE_FLOW_MOD:
5318 return handle_flow_mod(ofconn, oh);
5320 case OFPTYPE_GROUP_MOD:
5321 return handle_group_mod(ofconn, oh);
5323 case OFPTYPE_TABLE_MOD:
5324 return handle_table_mod(ofconn, oh);
5326 case OFPTYPE_METER_MOD:
5327 return handle_meter_mod(ofconn, oh);
5329 case OFPTYPE_BARRIER_REQUEST:
5330 return handle_barrier_request(ofconn, oh);
5332 case OFPTYPE_ROLE_REQUEST:
5333 return handle_role_request(ofconn, oh);
5335 /* OpenFlow replies. */
5336 case OFPTYPE_ECHO_REPLY:
5339 /* Nicira extension requests. */
5340 case OFPTYPE_FLOW_MOD_TABLE_ID:
5341 return handle_nxt_flow_mod_table_id(ofconn, oh);
5343 case OFPTYPE_SET_FLOW_FORMAT:
5344 return handle_nxt_set_flow_format(ofconn, oh);
5346 case OFPTYPE_SET_PACKET_IN_FORMAT:
5347 return handle_nxt_set_packet_in_format(ofconn, oh);
5349 case OFPTYPE_SET_CONTROLLER_ID:
5350 return handle_nxt_set_controller_id(ofconn, oh);
5352 case OFPTYPE_FLOW_AGE:
5353 /* Nothing to do. */
5356 case OFPTYPE_FLOW_MONITOR_CANCEL:
5357 return handle_flow_monitor_cancel(ofconn, oh);
5359 case OFPTYPE_SET_ASYNC_CONFIG:
5360 return handle_nxt_set_async_config(ofconn, oh);
5362 case OFPTYPE_GET_ASYNC_REQUEST:
5363 return handle_nxt_get_async_request(ofconn, oh);
5365 /* Statistics requests. */
5366 case OFPTYPE_DESC_STATS_REQUEST:
5367 return handle_desc_stats_request(ofconn, oh);
5369 case OFPTYPE_FLOW_STATS_REQUEST:
5370 return handle_flow_stats_request(ofconn, oh);
5372 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5373 return handle_aggregate_stats_request(ofconn, oh);
5375 case OFPTYPE_TABLE_STATS_REQUEST:
5376 return handle_table_stats_request(ofconn, oh);
5378 case OFPTYPE_PORT_STATS_REQUEST:
5379 return handle_port_stats_request(ofconn, oh);
5381 case OFPTYPE_QUEUE_STATS_REQUEST:
5382 return handle_queue_stats_request(ofconn, oh);
5384 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5385 return handle_port_desc_stats_request(ofconn, oh);
5387 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5388 return handle_flow_monitor_request(ofconn, oh);
5390 case OFPTYPE_METER_STATS_REQUEST:
5391 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5392 return handle_meter_request(ofconn, oh, type);
5394 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5395 return handle_meter_features_request(ofconn, oh);
5397 case OFPTYPE_GROUP_STATS_REQUEST:
5398 return handle_group_stats_request(ofconn, oh);
5400 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5401 return handle_group_desc_stats_request(ofconn, oh);
5403 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5404 return handle_group_features_stats_request(ofconn, oh);
5406 /* FIXME: Change the following once they are implemented: */
5407 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5408 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
5409 return OFPERR_OFPBRC_BAD_TYPE;
5413 case OFPTYPE_FEATURES_REPLY:
5414 case OFPTYPE_GET_CONFIG_REPLY:
5415 case OFPTYPE_PACKET_IN:
5416 case OFPTYPE_FLOW_REMOVED:
5417 case OFPTYPE_PORT_STATUS:
5418 case OFPTYPE_BARRIER_REPLY:
5419 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5420 case OFPTYPE_DESC_STATS_REPLY:
5421 case OFPTYPE_FLOW_STATS_REPLY:
5422 case OFPTYPE_QUEUE_STATS_REPLY:
5423 case OFPTYPE_PORT_STATS_REPLY:
5424 case OFPTYPE_TABLE_STATS_REPLY:
5425 case OFPTYPE_AGGREGATE_STATS_REPLY:
5426 case OFPTYPE_PORT_DESC_STATS_REPLY:
5427 case OFPTYPE_ROLE_REPLY:
5428 case OFPTYPE_FLOW_MONITOR_PAUSED:
5429 case OFPTYPE_FLOW_MONITOR_RESUMED:
5430 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
5431 case OFPTYPE_GET_ASYNC_REPLY:
5432 case OFPTYPE_GROUP_STATS_REPLY:
5433 case OFPTYPE_GROUP_DESC_STATS_REPLY:
5434 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
5435 case OFPTYPE_METER_STATS_REPLY:
5436 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5437 case OFPTYPE_METER_FEATURES_STATS_REPLY:
5438 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
5440 return OFPERR_OFPBRC_BAD_TYPE;
5445 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
5447 int error = handle_openflow__(ofconn, ofp_msg);
5448 if (error && error != OFPROTO_POSTPONE) {
5449 ofconn_send_error(ofconn, ofp_msg->data, error);
5451 COVERAGE_INC(ofproto_recv_openflow);
5452 return error != OFPROTO_POSTPONE;
5455 /* Asynchronous operations. */
5457 /* Creates and returns a new ofopgroup that is not associated with any
5458 * OpenFlow connection.
5460 * The caller should add operations to the returned group with
5461 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5462 static struct ofopgroup *
5463 ofopgroup_create_unattached(struct ofproto *ofproto)
5465 struct ofopgroup *group = xzalloc(sizeof *group);
5466 group->ofproto = ofproto;
5467 list_init(&group->ofproto_node);
5468 list_init(&group->ops);
5469 list_init(&group->ofconn_node);
5473 /* Creates and returns a new ofopgroup for 'ofproto'.
5475 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
5476 * connection. The 'request' and 'buffer_id' arguments are ignored.
5478 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
5479 * If the ofopgroup eventually fails, then the error reply will include
5480 * 'request'. If the ofopgroup eventually succeeds, then the packet with
5481 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
5483 * The caller should add operations to the returned group with
5484 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5485 static struct ofopgroup *
5486 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
5487 const struct ofp_header *request, uint32_t buffer_id)
5489 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
5491 size_t request_len = ntohs(request->length);
5493 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
5495 ofconn_add_opgroup(ofconn, &group->ofconn_node);
5496 group->ofconn = ofconn;
5497 group->request = xmemdup(request, MIN(request_len, 64));
5498 group->buffer_id = buffer_id;
5503 /* Submits 'group' for processing.
5505 * If 'group' contains no operations (e.g. none were ever added, or all of the
5506 * ones that were added completed synchronously), then it is destroyed
5507 * immediately. Otherwise it is added to the ofproto's list of pending
5510 ofopgroup_submit(struct ofopgroup *group)
5512 if (!group->n_running) {
5513 ofopgroup_complete(group);
5515 list_push_back(&group->ofproto->pending, &group->ofproto_node);
5516 group->ofproto->n_pending++;
5521 ofopgroup_complete(struct ofopgroup *group)
5523 struct ofproto *ofproto = group->ofproto;
5525 struct ofconn *abbrev_ofconn;
5526 ovs_be32 abbrev_xid;
5528 struct ofoperation *op, *next_op;
5531 ovs_assert(!group->n_running);
5534 LIST_FOR_EACH (op, group_node, &group->ops) {
5541 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
5542 LIST_FOR_EACH (op, group_node, &group->ops) {
5543 if (op->type != OFOPERATION_DELETE) {
5544 struct ofpbuf *packet;
5547 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
5551 error = rule_execute(op->rule, in_port, packet);
5558 if (!error && !list_is_empty(&group->ofconn_node)) {
5559 abbrev_ofconn = group->ofconn;
5560 abbrev_xid = group->request->xid;
5562 abbrev_ofconn = NULL;
5563 abbrev_xid = htonl(0);
5565 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
5566 struct rule *rule = op->rule;
5568 /* We generally want to report the change to active OpenFlow flow
5569 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
5571 - The operation failed.
5573 - The affected rule is not visible to controllers.
5575 - The operation's only effect was to update rule->modified. */
5577 || ofproto_rule_is_hidden(rule)
5578 || (op->type == OFOPERATION_MODIFY
5580 && rule->flow_cookie == op->flow_cookie))) {
5581 /* Check that we can just cast from ofoperation_type to
5582 * nx_flow_update_event. */
5583 enum nx_flow_update_event event_type;
5586 case OFOPERATION_ADD:
5587 case OFOPERATION_REPLACE:
5588 event_type = NXFME_ADDED;
5591 case OFOPERATION_DELETE:
5592 event_type = NXFME_DELETED;
5595 case OFOPERATION_MODIFY:
5596 event_type = NXFME_MODIFIED;
5603 ofmonitor_report(ofproto->connmgr, rule, event_type,
5604 op->reason, abbrev_ofconn, abbrev_xid);
5607 rule->pending = NULL;
5610 case OFOPERATION_ADD:
5614 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
5615 if (vid_mask == VLAN_VID_MASK) {
5616 if (ofproto->vlan_bitmap) {
5617 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
5618 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
5619 bitmap_set1(ofproto->vlan_bitmap, vid);
5620 ofproto->vlans_changed = true;
5623 ofproto->vlans_changed = true;
5627 ovs_rwlock_wrlock(&rule->rwlock);
5628 oftable_remove_rule(rule);
5629 ofproto_rule_destroy(rule);
5633 case OFOPERATION_DELETE:
5634 ovs_assert(!op->error);
5635 ofproto_rule_destroy(rule);
5639 case OFOPERATION_MODIFY:
5640 case OFOPERATION_REPLACE:
5642 long long int now = time_msec();
5644 rule->modified = now;
5645 if (op->type == OFOPERATION_REPLACE) {
5646 rule->created = rule->used = now;
5649 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
5650 ovs_mutex_lock(&rule->timeout_mutex);
5651 rule->idle_timeout = op->idle_timeout;
5652 rule->hard_timeout = op->hard_timeout;
5653 ovs_mutex_unlock(&rule->timeout_mutex);
5655 free(rule->ofpacts);
5657 ovs_rwlock_wrlock(&rule->rwlock);
5658 rule->ofpacts = op->ofpacts;
5659 rule->ofpacts_len = op->ofpacts_len;
5660 ovs_rwlock_unlock(&rule->rwlock);
5663 op->ofpacts_len = 0;
5665 rule->flags = op->flags;
5673 ofoperation_destroy(op);
5676 ofmonitor_flush(ofproto->connmgr);
5678 if (!list_is_empty(&group->ofproto_node)) {
5679 ovs_assert(ofproto->n_pending > 0);
5680 ofproto->n_pending--;
5681 list_remove(&group->ofproto_node);
5683 if (!list_is_empty(&group->ofconn_node)) {
5684 list_remove(&group->ofconn_node);
5686 ofconn_send_error(group->ofconn, group->request, error);
5688 connmgr_retry(ofproto->connmgr);
5690 free(group->request);
5694 /* Initiates a new operation on 'rule', of the specified 'type', within
5695 * 'group'. Prior to calling, 'rule' must not have any pending operation.
5697 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
5698 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
5700 * Returns the newly created ofoperation (which is also available as
5701 * rule->pending). */
5702 static struct ofoperation *
5703 ofoperation_create(struct ofopgroup *group, struct rule *rule,
5704 enum ofoperation_type type,
5705 enum ofp_flow_removed_reason reason)
5707 struct ofproto *ofproto = group->ofproto;
5708 struct ofoperation *op;
5710 ovs_assert(!rule->pending);
5712 op = rule->pending = xzalloc(sizeof *op);
5714 list_push_back(&group->ops, &op->group_node);
5717 op->reason = reason;
5718 op->flow_cookie = rule->flow_cookie;
5719 ovs_mutex_lock(&rule->timeout_mutex);
5720 op->idle_timeout = rule->idle_timeout;
5721 op->hard_timeout = rule->hard_timeout;
5722 ovs_mutex_unlock(&rule->timeout_mutex);
5723 op->flags = rule->flags;
5727 if (type == OFOPERATION_DELETE) {
5728 hmap_insert(&ofproto->deletions, &op->hmap_node,
5729 cls_rule_hash(&rule->cr, rule->table_id));
5736 ofoperation_destroy(struct ofoperation *op)
5738 struct ofopgroup *group = op->group;
5741 op->rule->pending = NULL;
5743 if (op->type == OFOPERATION_DELETE) {
5744 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
5746 list_remove(&op->group_node);
5751 /* Indicates that 'op' completed with status 'error', which is either 0 to
5752 * indicate success or an OpenFlow error code on failure.
5754 * If 'error' is 0, indicating success, the operation will be committed
5755 * permanently to the flow table.
5757 * If 'error' is nonzero, then generally the operation will be rolled back:
5759 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
5760 * restores the original rule. The caller must have uninitialized any
5761 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
5762 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
5763 * and 7 for the new rule, calling its ->rule_dealloc() function.
5765 * - If 'op' is a "modify flow" operation, ofproto restores the original
5768 * - 'op' must not be a "delete flow" operation. Removing a rule is not
5769 * allowed to fail. It must always succeed.
5771 * Please see the large comment in ofproto/ofproto-provider.h titled
5772 * "Asynchronous Operation Support" for more information. */
5774 ofoperation_complete(struct ofoperation *op, enum ofperr error)
5776 struct ofopgroup *group = op->group;
5778 ovs_assert(op->rule->pending == op);
5779 ovs_assert(group->n_running > 0);
5780 ovs_assert(!error || op->type != OFOPERATION_DELETE);
5783 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
5784 ofopgroup_complete(group);
5789 pick_datapath_id(const struct ofproto *ofproto)
5791 const struct ofport *port;
5793 port = ofproto_get_port(ofproto, OFPP_LOCAL);
5795 uint8_t ea[ETH_ADDR_LEN];
5798 error = netdev_get_etheraddr(port->netdev, ea);
5800 return eth_addr_to_uint64(ea);
5802 VLOG_WARN("%s: could not get MAC address for %s (%s)",
5803 ofproto->name, netdev_get_name(port->netdev),
5804 ovs_strerror(error));
5806 return ofproto->fallback_dpid;
5810 pick_fallback_dpid(void)
5812 uint8_t ea[ETH_ADDR_LEN];
5813 eth_addr_nicira_random(ea);
5814 return eth_addr_to_uint64(ea);
5817 /* Table overflow policy. */
5819 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
5820 * to NULL if the table is not configured to evict rules or if the table
5821 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
5822 * or with no timeouts are not evictable.) */
5824 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
5826 struct eviction_group *evg;
5829 if (!table->eviction_fields) {
5833 /* In the common case, the outer and inner loops here will each be entered
5836 * - The inner loop normally "return"s in its first iteration. If the
5837 * eviction group has any evictable rules, then it always returns in
5840 * - The outer loop only iterates more than once if the largest eviction
5841 * group has no evictable rules.
5843 * - The outer loop can exit only if table's 'max_flows' is all filled up
5844 * by unevictable rules. */
5845 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
5848 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
5849 if (!ovs_rwlock_trywrlock(&rule->rwlock)) {
5859 /* Searches 'ofproto' for tables that have more flows than their configured
5860 * maximum and that have flow eviction enabled, and evicts as many flows as
5861 * necessary and currently feasible from them.
5863 * This triggers only when an OpenFlow table has N flows in it and then the
5864 * client configures a maximum number of flows less than N. */
5866 ofproto_evict(struct ofproto *ofproto)
5868 struct ofopgroup *group;
5869 struct oftable *table;
5871 group = ofopgroup_create_unattached(ofproto);
5872 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
5873 while (table->eviction_fields) {
5877 ovs_rwlock_rdlock(&table->cls.rwlock);
5878 n_rules = classifier_count(&table->cls);
5879 ovs_rwlock_unlock(&table->cls.rwlock);
5881 if (n_rules <= table->max_flows) {
5885 if (!choose_rule_to_evict(table, &rule)) {
5889 if (rule->pending) {
5890 ovs_rwlock_unlock(&rule->rwlock);
5894 ofoperation_create(group, rule,
5895 OFOPERATION_DELETE, OFPRR_EVICTION);
5896 oftable_remove_rule(rule);
5897 ofproto->ofproto_class->rule_delete(rule);
5900 ofopgroup_submit(group);
5903 /* Eviction groups. */
5905 /* Returns the priority to use for an eviction_group that contains 'n_rules'
5906 * rules. The priority contains low-order random bits to ensure that eviction
5907 * groups with the same number of rules are prioritized randomly. */
5909 eviction_group_priority(size_t n_rules)
5911 uint16_t size = MIN(UINT16_MAX, n_rules);
5912 return (size << 16) | random_uint16();
5915 /* Updates 'evg', an eviction_group within 'table', following a change that
5916 * adds or removes rules in 'evg'. */
5918 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
5920 heap_change(&table->eviction_groups_by_size, &evg->size_node,
5921 eviction_group_priority(heap_count(&evg->rules)));
5924 /* Destroys 'evg', an eviction_group within 'table':
5926 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
5927 * rules themselves, just removes them from the eviction group.)
5929 * - Removes 'evg' from 'table'.
5933 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
5935 while (!heap_is_empty(&evg->rules)) {
5938 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
5939 rule->eviction_group = NULL;
5941 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
5942 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
5943 heap_destroy(&evg->rules);
5947 /* Removes 'rule' from its eviction group, if any. */
5949 eviction_group_remove_rule(struct rule *rule)
5951 if (rule->eviction_group) {
5952 struct oftable *table = &rule->ofproto->tables[rule->table_id];
5953 struct eviction_group *evg = rule->eviction_group;
5955 rule->eviction_group = NULL;
5956 heap_remove(&evg->rules, &rule->evg_node);
5957 if (heap_is_empty(&evg->rules)) {
5958 eviction_group_destroy(table, evg);
5960 eviction_group_resized(table, evg);
5965 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
5966 * returns the hash value. */
5968 eviction_group_hash_rule(struct rule *rule)
5970 struct oftable *table = &rule->ofproto->tables[rule->table_id];
5971 const struct mf_subfield *sf;
5975 hash = table->eviction_group_id_basis;
5976 miniflow_expand(&rule->cr.match.flow, &flow);
5977 for (sf = table->eviction_fields;
5978 sf < &table->eviction_fields[table->n_eviction_fields];
5981 if (mf_are_prereqs_ok(sf->field, &flow)) {
5982 union mf_value value;
5984 mf_get_value(sf->field, &flow, &value);
5986 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
5988 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
5989 unsigned int start = sf->ofs + sf->n_bits;
5990 bitwise_zero(&value, sf->field->n_bytes, start,
5991 sf->field->n_bytes * 8 - start);
5993 hash = hash_bytes(&value, sf->field->n_bytes, hash);
5995 hash = hash_int(hash, 0);
6002 /* Returns an eviction group within 'table' with the given 'id', creating one
6004 static struct eviction_group *
6005 eviction_group_find(struct oftable *table, uint32_t id)
6007 struct eviction_group *evg;
6009 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6013 evg = xmalloc(sizeof *evg);
6014 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6015 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6016 eviction_group_priority(0));
6017 heap_init(&evg->rules);
6022 /* Returns an eviction priority for 'rule'. The return value should be
6023 * interpreted so that higher priorities make a rule more attractive candidates
6026 rule_eviction_priority(struct rule *rule)
6028 long long int hard_expiration;
6029 long long int idle_expiration;
6030 long long int expiration;
6031 uint32_t expiration_offset;
6033 /* Calculate time of expiration. */
6034 ovs_mutex_lock(&rule->timeout_mutex);
6035 hard_expiration = (rule->hard_timeout
6036 ? rule->modified + rule->hard_timeout * 1000
6038 idle_expiration = (rule->idle_timeout
6039 ? rule->used + rule->idle_timeout * 1000
6041 expiration = MIN(hard_expiration, idle_expiration);
6042 ovs_mutex_unlock(&rule->timeout_mutex);
6043 if (expiration == LLONG_MAX) {
6047 /* Calculate the time of expiration as a number of (approximate) seconds
6048 * after program startup.
6050 * This should work OK for program runs that last UINT32_MAX seconds or
6051 * less. Therefore, please restart OVS at least once every 136 years. */
6052 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6054 /* Invert the expiration offset because we're using a max-heap. */
6055 return UINT32_MAX - expiration_offset;
6058 /* Adds 'rule' to an appropriate eviction group for its oftable's
6059 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6060 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6063 * The caller must ensure that 'rule' is not already in an eviction group. */
6065 eviction_group_add_rule(struct rule *rule)
6067 struct ofproto *ofproto = rule->ofproto;
6068 struct oftable *table = &ofproto->tables[rule->table_id];
6071 ovs_mutex_lock(&rule->timeout_mutex);
6072 has_timeout = rule->hard_timeout || rule->idle_timeout;
6073 ovs_mutex_unlock(&rule->timeout_mutex);
6075 if (table->eviction_fields && has_timeout) {
6076 struct eviction_group *evg;
6078 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6080 rule->eviction_group = evg;
6081 heap_insert(&evg->rules, &rule->evg_node,
6082 rule_eviction_priority(rule));
6083 eviction_group_resized(table, evg);
6089 /* Initializes 'table'. */
6091 oftable_init(struct oftable *table)
6093 memset(table, 0, sizeof *table);
6094 classifier_init(&table->cls);
6095 table->max_flows = UINT_MAX;
6098 /* Destroys 'table', including its classifier and eviction groups.
6100 * The caller is responsible for freeing 'table' itself. */
6102 oftable_destroy(struct oftable *table)
6104 ovs_rwlock_rdlock(&table->cls.rwlock);
6105 ovs_assert(classifier_is_empty(&table->cls));
6106 ovs_rwlock_unlock(&table->cls.rwlock);
6107 oftable_disable_eviction(table);
6108 classifier_destroy(&table->cls);
6112 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6113 * string, then 'table' will use its default name.
6115 * This only affects the name exposed for a table exposed through the OpenFlow
6116 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6118 oftable_set_name(struct oftable *table, const char *name)
6120 if (name && name[0]) {
6121 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6122 if (!table->name || strncmp(name, table->name, len)) {
6124 table->name = xmemdup0(name, len);
6132 /* oftables support a choice of two policies when adding a rule would cause the
6133 * number of flows in the table to exceed the configured maximum number: either
6134 * they can refuse to add the new flow or they can evict some existing flow.
6135 * This function configures the former policy on 'table'. */
6137 oftable_disable_eviction(struct oftable *table)
6139 if (table->eviction_fields) {
6140 struct eviction_group *evg, *next;
6142 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6143 &table->eviction_groups_by_id) {
6144 eviction_group_destroy(table, evg);
6146 hmap_destroy(&table->eviction_groups_by_id);
6147 heap_destroy(&table->eviction_groups_by_size);
6149 free(table->eviction_fields);
6150 table->eviction_fields = NULL;
6151 table->n_eviction_fields = 0;
6155 /* oftables support a choice of two policies when adding a rule would cause the
6156 * number of flows in the table to exceed the configured maximum number: either
6157 * they can refuse to add the new flow or they can evict some existing flow.
6158 * This function configures the latter policy on 'table', with fairness based
6159 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6160 * 'n_fields' as 0 disables fairness.) */
6162 oftable_enable_eviction(struct oftable *table,
6163 const struct mf_subfield *fields, size_t n_fields)
6165 struct cls_cursor cursor;
6168 if (table->eviction_fields
6169 && n_fields == table->n_eviction_fields
6171 || !memcmp(fields, table->eviction_fields,
6172 n_fields * sizeof *fields))) {
6177 oftable_disable_eviction(table);
6179 table->n_eviction_fields = n_fields;
6180 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6182 table->eviction_group_id_basis = random_uint32();
6183 hmap_init(&table->eviction_groups_by_id);
6184 heap_init(&table->eviction_groups_by_size);
6186 ovs_rwlock_rdlock(&table->cls.rwlock);
6187 cls_cursor_init(&cursor, &table->cls, NULL);
6188 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6189 eviction_group_add_rule(rule);
6191 ovs_rwlock_unlock(&table->cls.rwlock);
6194 /* Removes 'rule' from the oftable that contains it. */
6196 oftable_remove_rule__(struct ofproto *ofproto, struct classifier *cls,
6198 OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->rwlock)
6200 classifier_remove(cls, &rule->cr);
6201 cookies_remove(ofproto, rule);
6202 eviction_group_remove_rule(rule);
6203 ovs_mutex_lock(&ofproto->expirable_mutex);
6204 if (!list_is_empty(&rule->expirable)) {
6205 list_remove(&rule->expirable);
6207 ovs_mutex_unlock(&ofproto->expirable_mutex);
6208 if (!list_is_empty(&rule->meter_list_node)) {
6209 list_remove(&rule->meter_list_node);
6210 list_init(&rule->meter_list_node);
6212 ovs_rwlock_unlock(&rule->rwlock);
6216 oftable_remove_rule(struct rule *rule)
6218 struct ofproto *ofproto = rule->ofproto;
6219 struct oftable *table = &ofproto->tables[rule->table_id];
6221 ovs_rwlock_wrlock(&table->cls.rwlock);
6222 oftable_remove_rule__(ofproto, &table->cls, rule);
6223 ovs_rwlock_unlock(&table->cls.rwlock);
6226 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6227 * the same cls_rule. */
6229 oftable_insert_rule(struct rule *rule)
6231 struct ofproto *ofproto = rule->ofproto;
6232 struct oftable *table = &ofproto->tables[rule->table_id];
6235 ovs_mutex_lock(&rule->timeout_mutex);
6236 may_expire = rule->hard_timeout || rule->idle_timeout;
6237 ovs_mutex_unlock(&rule->timeout_mutex);
6240 ovs_mutex_lock(&ofproto->expirable_mutex);
6241 list_insert(&ofproto->expirable, &rule->expirable);
6242 ovs_mutex_unlock(&ofproto->expirable_mutex);
6244 cookies_insert(ofproto, rule);
6245 if (rule->meter_id) {
6246 struct meter *meter = ofproto->meters[rule->meter_id];
6247 list_insert(&meter->rules, &rule->meter_list_node);
6249 ovs_rwlock_wrlock(&table->cls.rwlock);
6250 classifier_insert(&table->cls, &rule->cr);
6251 ovs_rwlock_unlock(&table->cls.rwlock);
6252 eviction_group_add_rule(rule);
6255 /* unixctl commands. */
6258 ofproto_lookup(const char *name)
6260 struct ofproto *ofproto;
6262 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6264 if (!strcmp(ofproto->name, name)) {
6272 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6273 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6275 struct ofproto *ofproto;
6279 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6280 ds_put_format(&results, "%s\n", ofproto->name);
6282 unixctl_command_reply(conn, ds_cstr(&results));
6283 ds_destroy(&results);
6287 ofproto_unixctl_init(void)
6289 static bool registered;
6295 unixctl_command_register("ofproto/list", "", 0, 0,
6296 ofproto_unixctl_list, NULL);
6299 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6301 * This is deprecated. It is only for compatibility with broken device drivers
6302 * in old versions of Linux that do not properly support VLANs when VLAN
6303 * devices are not used. When broken device drivers are no longer in
6304 * widespread use, we will delete these interfaces. */
6306 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6307 * (exactly) by an OpenFlow rule in 'ofproto'. */
6309 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6311 const struct oftable *oftable;
6313 free(ofproto->vlan_bitmap);
6314 ofproto->vlan_bitmap = bitmap_allocate(4096);
6315 ofproto->vlans_changed = false;
6317 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6318 const struct cls_table *table;
6320 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.tables) {
6321 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6322 const struct cls_rule *rule;
6324 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6325 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6326 bitmap_set1(vlan_bitmap, vid);
6327 bitmap_set1(ofproto->vlan_bitmap, vid);
6334 /* Returns true if new VLANs have come into use by the flow table since the
6335 * last call to ofproto_get_vlan_usage().
6337 * We don't track when old VLANs stop being used. */
6339 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6341 return ofproto->vlans_changed;
6344 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6345 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6346 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6347 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6348 * then the VLAN device is un-enslaved. */
6350 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6351 ofp_port_t realdev_ofp_port, int vid)
6353 struct ofport *ofport;
6356 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6358 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6360 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6361 ofproto->name, vlandev_ofp_port);
6365 if (!ofproto->ofproto_class->set_realdev) {
6366 if (!vlandev_ofp_port) {
6369 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6373 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6375 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6376 ofproto->name, vlandev_ofp_port,
6377 netdev_get_name(ofport->netdev), ovs_strerror(error));