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 rule_actions *actions;
129 /* OFOPERATION_DELETE. */
130 enum ofp_flow_removed_reason reason; /* Reason flow was removed. */
132 ovs_be64 flow_cookie; /* Rule's old flow cookie. */
133 uint16_t idle_timeout; /* Rule's old idle timeout. */
134 uint16_t hard_timeout; /* Rule's old hard timeout. */
135 enum ofputil_flow_mod_flags flags; /* Rule's old flags. */
136 enum ofperr error; /* 0 if no error. */
139 static struct ofoperation *ofoperation_create(struct ofopgroup *,
141 enum ofoperation_type,
142 enum ofp_flow_removed_reason);
143 static void ofoperation_destroy(struct ofoperation *);
146 static void oftable_init(struct oftable *);
147 static void oftable_destroy(struct oftable *);
149 static void oftable_set_name(struct oftable *, const char *name);
151 static void oftable_disable_eviction(struct oftable *);
152 static void oftable_enable_eviction(struct oftable *,
153 const struct mf_subfield *fields,
156 static void oftable_remove_rule(struct rule *rule) OVS_RELEASES(rule->mutex);
157 static void oftable_remove_rule__(struct ofproto *ofproto,
158 struct classifier *cls, struct rule *rule)
159 OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->mutex);
160 static void oftable_insert_rule(struct rule *);
162 /* A set of rules within a single OpenFlow table (oftable) that have the same
163 * values for the oftable's eviction_fields. A rule to be evicted, when one is
164 * needed, is taken from the eviction group that contains the greatest number
167 * An oftable owns any number of eviction groups, each of which contains any
170 * Membership in an eviction group is imprecise, based on the hash of the
171 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
172 * That is, if two rules have different eviction_fields, but those
173 * eviction_fields hash to the same value, then they will belong to the same
174 * eviction_group anyway.
176 * (When eviction is not enabled on an oftable, we don't track any eviction
177 * groups, to save time and space.) */
178 struct eviction_group {
179 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
180 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
181 struct heap rules; /* Contains "struct rule"s. */
184 static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
185 OVS_TRY_WRLOCK(true, (*rulep)->mutex);
186 static void ofproto_evict(struct ofproto *);
187 static uint32_t rule_eviction_priority(struct rule *);
188 static void eviction_group_add_rule(struct rule *);
189 static void eviction_group_remove_rule(struct rule *);
191 /* Criteria that flow_mod and other operations use for selecting rules on
192 * which to operate. */
193 struct rule_criteria {
194 /* An OpenFlow table or 255 for all tables. */
197 /* OpenFlow matching criteria. Interpreted different in "loose" way by
198 * collect_rules_loose() and "strict" way by collect_rules_strict(), as
199 * defined in the OpenFlow spec. */
202 /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's
203 * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'.
204 * The rule will not be selected if M is 1 and B != C. */
206 ovs_be64 cookie_mask;
208 /* Selection based on actions within a rule:
210 * If out_port != OFPP_ANY, selects only rules that output to out_port.
211 * If out_group != OFPG_ALL, select only rules that output to out_group. */
216 static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
217 const struct match *match,
218 unsigned int priority,
219 ovs_be64 cookie, ovs_be64 cookie_mask,
220 ofp_port_t out_port, uint32_t out_group);
221 static void rule_criteria_destroy(struct rule_criteria *);
223 /* A packet that needs to be passed to rule_execute(). */
224 struct rule_execute {
225 struct list list_node; /* In struct ofproto's "rule_executes" list. */
226 struct rule *rule; /* Owns a reference to the rule. */
228 struct ofpbuf *packet; /* Owns the packet. */
231 static void run_rule_executes(struct ofproto *);
232 static void destroy_rule_executes(struct ofproto *);
235 static void ofport_destroy__(struct ofport *);
236 static void ofport_destroy(struct ofport *);
238 static void update_port(struct ofproto *, const char *devname);
239 static int init_ports(struct ofproto *);
240 static void reinit_ports(struct ofproto *);
243 static void ofproto_rule_destroy__(struct rule *);
244 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
245 static bool rule_is_modifiable(const struct rule *);
248 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
249 struct ofputil_flow_mod *,
250 const struct ofp_header *);
251 static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
252 struct ofputil_flow_mod *,
253 const struct ofp_header *,
254 const struct rule_collection *);
255 static void delete_flow__(struct rule *rule, struct ofopgroup *,
256 enum ofp_flow_removed_reason)
257 OVS_RELEASES(rule->mutex);
258 static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
259 static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
260 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
261 struct ofputil_flow_mod *,
262 const struct ofp_header *);
263 static void calc_duration(long long int start, long long int now,
264 uint32_t *sec, uint32_t *nsec);
267 static uint64_t pick_datapath_id(const struct ofproto *);
268 static uint64_t pick_fallback_dpid(void);
269 static void ofproto_destroy__(struct ofproto *);
270 static void update_mtu(struct ofproto *, struct ofport *);
271 static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
274 static void ofproto_unixctl_init(void);
276 /* All registered ofproto classes, in probe order. */
277 static const struct ofproto_class **ofproto_classes;
278 static size_t n_ofproto_classes;
279 static size_t allocated_ofproto_classes;
281 struct ovs_mutex ofproto_mutex;
283 unsigned flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT;
284 unsigned n_handler_threads;
285 enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO;
287 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
288 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
290 /* Initial mappings of port to OpenFlow number mappings. */
291 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
293 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
295 /* The default value of true waits for flow restore. */
296 static bool flow_restore_wait = true;
298 /* Must be called to initialize the ofproto library.
300 * The caller may pass in 'iface_hints', which contains an shash of
301 * "iface_hint" elements indexed by the interface's name. The provider
302 * may use these hints to describe the startup configuration in order to
303 * reinitialize its state. The caller owns the provided data, so a
304 * provider will make copies of anything required. An ofproto provider
305 * will remove any existing state that is not described by the hint, and
306 * may choose to remove it all. */
308 ofproto_init(const struct shash *iface_hints)
310 struct shash_node *node;
313 ovs_mutex_init_recursive(&ofproto_mutex);
315 ofproto_class_register(&ofproto_dpif_class);
317 /* Make a local copy, since we don't own 'iface_hints' elements. */
318 SHASH_FOR_EACH(node, iface_hints) {
319 const struct iface_hint *orig_hint = node->data;
320 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
321 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
323 new_hint->br_name = xstrdup(orig_hint->br_name);
324 new_hint->br_type = xstrdup(br_type);
325 new_hint->ofp_port = orig_hint->ofp_port;
327 shash_add(&init_ofp_ports, node->name, new_hint);
330 for (i = 0; i < n_ofproto_classes; i++) {
331 ofproto_classes[i]->init(&init_ofp_ports);
335 /* 'type' should be a normalized datapath type, as returned by
336 * ofproto_normalize_type(). Returns the corresponding ofproto_class
337 * structure, or a null pointer if there is none registered for 'type'. */
338 static const struct ofproto_class *
339 ofproto_class_find__(const char *type)
343 for (i = 0; i < n_ofproto_classes; i++) {
344 const struct ofproto_class *class = ofproto_classes[i];
349 class->enumerate_types(&types);
350 found = sset_contains(&types, type);
351 sset_destroy(&types);
357 VLOG_WARN("unknown datapath type %s", type);
361 /* Registers a new ofproto class. After successful registration, new ofprotos
362 * of that type can be created using ofproto_create(). */
364 ofproto_class_register(const struct ofproto_class *new_class)
368 for (i = 0; i < n_ofproto_classes; i++) {
369 if (ofproto_classes[i] == new_class) {
374 if (n_ofproto_classes >= allocated_ofproto_classes) {
375 ofproto_classes = x2nrealloc(ofproto_classes,
376 &allocated_ofproto_classes,
377 sizeof *ofproto_classes);
379 ofproto_classes[n_ofproto_classes++] = new_class;
383 /* Unregisters a datapath provider. 'type' must have been previously
384 * registered and not currently be in use by any ofprotos. After
385 * unregistration new datapaths of that type cannot be opened using
386 * ofproto_create(). */
388 ofproto_class_unregister(const struct ofproto_class *class)
392 for (i = 0; i < n_ofproto_classes; i++) {
393 if (ofproto_classes[i] == class) {
394 for (i++; i < n_ofproto_classes; i++) {
395 ofproto_classes[i - 1] = ofproto_classes[i];
401 VLOG_WARN("attempted to unregister an ofproto class that is not "
406 /* Clears 'types' and enumerates all registered ofproto types into it. The
407 * caller must first initialize the sset. */
409 ofproto_enumerate_types(struct sset *types)
413 for (i = 0; i < n_ofproto_classes; i++) {
414 ofproto_classes[i]->enumerate_types(types);
418 /* Returns the fully spelled out name for the given ofproto 'type'.
420 * Normalized type string can be compared with strcmp(). Unnormalized type
421 * string might be the same even if they have different spellings. */
423 ofproto_normalize_type(const char *type)
425 return type && type[0] ? type : "system";
428 /* Clears 'names' and enumerates the names of all known created ofprotos with
429 * the given 'type'. The caller must first initialize the sset. Returns 0 if
430 * successful, otherwise a positive errno value.
432 * Some kinds of datapaths might not be practically enumerable. This is not
433 * considered an error. */
435 ofproto_enumerate_names(const char *type, struct sset *names)
437 const struct ofproto_class *class = ofproto_class_find__(type);
438 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
442 ofproto_create(const char *datapath_name, const char *datapath_type,
443 struct ofproto **ofprotop)
445 const struct ofproto_class *class;
446 struct ofproto *ofproto;
452 ofproto_unixctl_init();
454 datapath_type = ofproto_normalize_type(datapath_type);
455 class = ofproto_class_find__(datapath_type);
457 VLOG_WARN("could not create datapath %s of unknown type %s",
458 datapath_name, datapath_type);
462 ofproto = class->alloc();
464 VLOG_ERR("failed to allocate datapath %s of type %s",
465 datapath_name, datapath_type);
470 ovs_mutex_lock(&ofproto_mutex);
471 memset(ofproto, 0, sizeof *ofproto);
472 ofproto->ofproto_class = class;
473 ofproto->name = xstrdup(datapath_name);
474 ofproto->type = xstrdup(datapath_type);
475 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
476 hash_string(ofproto->name, 0));
477 ofproto->datapath_id = 0;
478 ofproto->forward_bpdu = false;
479 ofproto->fallback_dpid = pick_fallback_dpid();
480 ofproto->mfr_desc = NULL;
481 ofproto->hw_desc = NULL;
482 ofproto->sw_desc = NULL;
483 ofproto->serial_desc = NULL;
484 ofproto->dp_desc = NULL;
485 ofproto->frag_handling = OFPC_FRAG_NORMAL;
486 hmap_init(&ofproto->ports);
487 shash_init(&ofproto->port_by_name);
488 simap_init(&ofproto->ofp_requests);
489 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
490 ofproto->eviction_group_timer = LLONG_MIN;
491 ofproto->tables = NULL;
492 ofproto->n_tables = 0;
493 hindex_init(&ofproto->cookies);
494 list_init(&ofproto->expirable);
495 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
496 ofproto->state = S_OPENFLOW;
497 list_init(&ofproto->pending);
498 ofproto->n_pending = 0;
499 hmap_init(&ofproto->deletions);
500 guarded_list_init(&ofproto->rule_executes);
501 ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0;
502 ofproto->first_op = ofproto->last_op = LLONG_MIN;
503 ofproto->next_op_report = LLONG_MAX;
504 ofproto->op_backoff = LLONG_MIN;
505 ofproto->vlan_bitmap = NULL;
506 ofproto->vlans_changed = false;
507 ofproto->min_mtu = INT_MAX;
508 ovs_rwlock_init(&ofproto->groups_rwlock);
509 hmap_init(&ofproto->groups);
510 ovs_mutex_unlock(&ofproto_mutex);
512 error = ofproto->ofproto_class->construct(ofproto);
514 VLOG_ERR("failed to open datapath %s: %s",
515 datapath_name, ovs_strerror(error));
516 ofproto_destroy__(ofproto);
520 /* The "max_ports" member should have been set by ->construct(ofproto).
521 * Port 0 is not a valid OpenFlow port, so mark that as unavailable. */
522 ofproto->ofp_port_ids = bitmap_allocate(ofproto->max_ports);
523 bitmap_set1(ofproto->ofp_port_ids, 0);
525 /* Check that hidden tables, if any, are at the end. */
526 ovs_assert(ofproto->n_tables);
527 for (i = 0; i + 1 < ofproto->n_tables; i++) {
528 enum oftable_flags flags = ofproto->tables[i].flags;
529 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
531 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
534 ofproto->datapath_id = pick_datapath_id(ofproto);
537 /* Initialize meters table. */
538 if (ofproto->ofproto_class->meter_get_features) {
539 ofproto->ofproto_class->meter_get_features(ofproto,
540 &ofproto->meter_features);
542 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
544 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
545 * sizeof(struct meter *));
551 /* Must be called (only) by an ofproto implementation in its constructor
552 * function. See the large comment on 'construct' in struct ofproto_class for
555 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
557 struct oftable *table;
559 ovs_assert(!ofproto->n_tables);
560 ovs_assert(n_tables >= 1 && n_tables <= 255);
562 ofproto->n_tables = n_tables;
563 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
564 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
569 /* To be optionally called (only) by an ofproto implementation in its
570 * constructor function. See the large comment on 'construct' in struct
571 * ofproto_class for details.
573 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
574 * will then ensure that actions passed into the ofproto implementation will
575 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
576 * function is not called, there will be no such restriction.
578 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
579 * the 'max_ports' restriction. */
581 ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
583 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
584 ofproto->max_ports = max_ports;
588 ofproto_get_datapath_id(const struct ofproto *ofproto)
590 return ofproto->datapath_id;
594 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
596 uint64_t old_dpid = p->datapath_id;
597 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
598 if (p->datapath_id != old_dpid) {
599 /* Force all active connections to reconnect, since there is no way to
600 * notify a controller that the datapath ID has changed. */
601 ofproto_reconnect_controllers(p);
606 ofproto_set_controllers(struct ofproto *p,
607 const struct ofproto_controller *controllers,
608 size_t n_controllers, uint32_t allowed_versions)
610 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
615 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
617 connmgr_set_fail_mode(p->connmgr, fail_mode);
620 /* Drops the connections between 'ofproto' and all of its controllers, forcing
621 * them to reconnect. */
623 ofproto_reconnect_controllers(struct ofproto *ofproto)
625 connmgr_reconnect(ofproto->connmgr);
628 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
629 * in-band control should guarantee access, in the same way that in-band
630 * control guarantees access to OpenFlow controllers. */
632 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
633 const struct sockaddr_in *extras, size_t n)
635 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
638 /* Sets the OpenFlow queue used by flows set up by in-band control on
639 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
640 * flows will use the default queue. */
642 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
644 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
647 /* Sets the number of flows at which eviction from the kernel flow table
650 ofproto_set_flow_eviction_threshold(unsigned threshold)
652 flow_eviction_threshold = MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN,
656 /* Sets the path for handling flow misses. */
658 ofproto_set_flow_miss_model(unsigned model)
660 flow_miss_model = model;
663 /* If forward_bpdu is true, the NORMAL action will forward frames with
664 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
665 * the NORMAL action will drop these frames. */
667 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
669 bool old_val = ofproto->forward_bpdu;
670 ofproto->forward_bpdu = forward_bpdu;
671 if (old_val != ofproto->forward_bpdu) {
672 if (ofproto->ofproto_class->forward_bpdu_changed) {
673 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
678 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
679 * 'idle_time', in seconds, and the maximum number of MAC table entries to
682 ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
685 if (ofproto->ofproto_class->set_mac_table_config) {
686 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
691 /* Sets number of upcall handler threads. The default is
692 * (number of online cores - 2). */
694 ofproto_set_n_handler_threads(unsigned limit)
697 n_handler_threads = limit;
699 int n_proc = sysconf(_SC_NPROCESSORS_ONLN);
700 n_handler_threads = n_proc > 2 ? n_proc - 2 : 1;
705 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
708 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
712 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
714 return connmgr_set_snoops(ofproto->connmgr, snoops);
718 ofproto_set_netflow(struct ofproto *ofproto,
719 const struct netflow_options *nf_options)
721 if (nf_options && sset_is_empty(&nf_options->collectors)) {
725 if (ofproto->ofproto_class->set_netflow) {
726 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
728 return nf_options ? EOPNOTSUPP : 0;
733 ofproto_set_sflow(struct ofproto *ofproto,
734 const struct ofproto_sflow_options *oso)
736 if (oso && sset_is_empty(&oso->targets)) {
740 if (ofproto->ofproto_class->set_sflow) {
741 return ofproto->ofproto_class->set_sflow(ofproto, oso);
743 return oso ? EOPNOTSUPP : 0;
748 ofproto_set_ipfix(struct ofproto *ofproto,
749 const struct ofproto_ipfix_bridge_exporter_options *bo,
750 const struct ofproto_ipfix_flow_exporter_options *fo,
753 if (ofproto->ofproto_class->set_ipfix) {
754 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
756 return (bo || fo) ? EOPNOTSUPP : 0;
761 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
763 flow_restore_wait = flow_restore_wait_db;
767 ofproto_get_flow_restore_wait(void)
769 return flow_restore_wait;
773 /* Spanning Tree Protocol (STP) configuration. */
775 /* Configures STP on 'ofproto' using the settings defined in 's'. If
776 * 's' is NULL, disables STP.
778 * Returns 0 if successful, otherwise a positive errno value. */
780 ofproto_set_stp(struct ofproto *ofproto,
781 const struct ofproto_stp_settings *s)
783 return (ofproto->ofproto_class->set_stp
784 ? ofproto->ofproto_class->set_stp(ofproto, s)
788 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
789 * 'enabled' member of 's' is false, then the other members are not
792 * Returns 0 if successful, otherwise a positive errno value. */
794 ofproto_get_stp_status(struct ofproto *ofproto,
795 struct ofproto_stp_status *s)
797 return (ofproto->ofproto_class->get_stp_status
798 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
802 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
803 * in 's'. The caller is responsible for assigning STP port numbers
804 * (using the 'port_num' member in the range of 1 through 255, inclusive)
805 * and ensuring there are no duplicates. If the 's' is NULL, then STP
806 * is disabled on the port.
808 * Returns 0 if successful, otherwise a positive errno value.*/
810 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
811 const struct ofproto_port_stp_settings *s)
813 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
815 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
816 ofproto->name, ofp_port);
820 return (ofproto->ofproto_class->set_stp_port
821 ? ofproto->ofproto_class->set_stp_port(ofport, s)
825 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
826 * 's'. If the 'enabled' member in 's' is false, then the other members
827 * are not meaningful.
829 * Returns 0 if successful, otherwise a positive errno value.*/
831 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
832 struct ofproto_port_stp_status *s)
834 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
836 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
837 "port %"PRIu16, ofproto->name, ofp_port);
841 return (ofproto->ofproto_class->get_stp_port_status
842 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
846 /* Queue DSCP configuration. */
848 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
849 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
850 * to implement QoS. Instead, it is used to implement features which require
851 * knowledge of what queues exist on a port, and some basic information about
854 * Returns 0 if successful, otherwise a positive errno value. */
856 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
857 const struct ofproto_port_queue *queues,
860 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
863 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
864 ofproto->name, ofp_port);
868 return (ofproto->ofproto_class->set_queues
869 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
873 /* Connectivity Fault Management configuration. */
875 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
877 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
879 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
880 if (ofport && ofproto->ofproto_class->set_cfm) {
881 ofproto->ofproto_class->set_cfm(ofport, NULL);
885 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
886 * basic configuration from the configuration members in 'cfm', and the remote
887 * maintenance point ID from remote_mpid. Ignores the statistics members of
890 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
892 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
893 const struct cfm_settings *s)
895 struct ofport *ofport;
898 ofport = ofproto_get_port(ofproto, ofp_port);
900 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
901 ofproto->name, ofp_port);
905 /* XXX: For configuration simplicity, we only support one remote_mpid
906 * outside of the CFM module. It's not clear if this is the correct long
907 * term solution or not. */
908 error = (ofproto->ofproto_class->set_cfm
909 ? ofproto->ofproto_class->set_cfm(ofport, s)
912 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
913 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
914 ovs_strerror(error));
918 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
919 * 'ofproto' does not have a port 'ofp_port'. */
921 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
922 const struct smap *cfg)
924 struct ofport *ofport;
927 ofport = ofproto_get_port(ofproto, ofp_port);
929 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
930 ofproto->name, ofp_port);
934 error = (ofproto->ofproto_class->set_bfd
935 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
938 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
939 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
940 ovs_strerror(error));
944 /* Populates 'status' with key value pairs indicating the status of the BFD
945 * session on 'ofp_port'. This information is intended to be populated in the
946 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
949 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
952 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
953 return (ofport && ofproto->ofproto_class->get_bfd_status
954 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
958 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
959 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
960 * 0 if LACP partner information is not current (generally indicating a
961 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
963 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
965 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
966 return (ofport && ofproto->ofproto_class->port_is_lacp_current
967 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
973 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
974 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
975 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
976 * configuration plus, if there is more than one slave, a bonding
979 * If 'aux' is already registered then this function updates its configuration
980 * to 's'. Otherwise, this function registers a new bundle.
982 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
985 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
986 const struct ofproto_bundle_settings *s)
988 return (ofproto->ofproto_class->bundle_set
989 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
993 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
994 * If no such bundle has been registered, this has no effect. */
996 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
998 return ofproto_bundle_register(ofproto, aux, NULL);
1002 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1003 * If 'aux' is already registered then this function updates its configuration
1004 * to 's'. Otherwise, this function registers a new mirror. */
1006 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1007 const struct ofproto_mirror_settings *s)
1009 return (ofproto->ofproto_class->mirror_set
1010 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1014 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1015 * If no mirror has been registered, this has no effect. */
1017 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
1019 return ofproto_mirror_register(ofproto, aux, NULL);
1022 /* Retrieves statistics from mirror associated with client data pointer
1023 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1024 * 'bytes', respectively. If a particular counters is not supported,
1025 * the appropriate argument is set to UINT64_MAX. */
1027 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1028 uint64_t *packets, uint64_t *bytes)
1030 if (!ofproto->ofproto_class->mirror_get_stats) {
1031 *packets = *bytes = UINT64_MAX;
1035 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1039 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1040 * which all packets are flooded, instead of using MAC learning. If
1041 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1043 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1046 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
1048 return (ofproto->ofproto_class->set_flood_vlans
1049 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1053 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1054 * output for a mirror. */
1056 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1058 return (ofproto->ofproto_class->is_mirror_output_bundle
1059 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1063 /* Configuration of OpenFlow tables. */
1065 /* Returns the number of OpenFlow tables in 'ofproto'. */
1067 ofproto_get_n_tables(const struct ofproto *ofproto)
1069 return ofproto->n_tables;
1072 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1073 * settings from 's'. 'table_id' must be in the range 0 through the number of
1074 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1076 * For read-only tables, only the name may be configured. */
1078 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1079 const struct ofproto_table_settings *s)
1081 struct oftable *table;
1083 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1084 table = &ofproto->tables[table_id];
1086 oftable_set_name(table, s->name);
1088 if (table->flags & OFTABLE_READONLY) {
1093 oftable_enable_eviction(table, s->groups, s->n_groups);
1095 oftable_disable_eviction(table);
1098 table->max_flows = s->max_flows;
1099 ovs_rwlock_rdlock(&table->cls.rwlock);
1100 if (classifier_count(&table->cls) > table->max_flows
1101 && table->eviction_fields) {
1102 /* 'table' contains more flows than allowed. We might not be able to
1103 * evict them right away because of the asynchronous nature of flow
1104 * table changes. Schedule eviction for later. */
1105 switch (ofproto->state) {
1107 ofproto->state = S_EVICT;
1111 /* We're already deleting flows, nothing more to do. */
1115 ovs_rwlock_unlock(&table->cls.rwlock);
1119 ofproto_has_snoops(const struct ofproto *ofproto)
1121 return connmgr_has_snoops(ofproto->connmgr);
1125 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1127 connmgr_get_snoops(ofproto->connmgr, snoops);
1130 /* Deletes 'rule' from 'cls' within 'ofproto'.
1132 * Within an ofproto implementation, this function allows an ofproto
1133 * implementation to destroy any rules that remain when its ->destruct()
1134 * function is called. This function is not suitable for use elsewhere in an
1135 * ofproto implementation.
1137 * This function is also used internally in ofproto.c.
1139 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1140 * Cycle" in ofproto-provider.h.
1142 * The 'cls' argument is redundant (it is &ofproto->tables[rule->table_id].cls)
1143 * but it allows Clang to do better checking. */
1145 ofproto_rule_delete(struct ofproto *ofproto, struct classifier *cls,
1147 OVS_REQ_WRLOCK(cls->rwlock)
1149 struct ofopgroup *group;
1151 ovs_assert(!rule->pending);
1152 ovs_assert(cls == &ofproto->tables[rule->table_id].cls);
1154 group = ofopgroup_create_unattached(ofproto);
1155 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1156 ovs_mutex_lock(&rule->mutex);
1157 oftable_remove_rule__(ofproto, cls, rule);
1158 ofproto->ofproto_class->rule_delete(rule);
1159 ofopgroup_submit(group);
1163 ofproto_flush__(struct ofproto *ofproto)
1165 struct oftable *table;
1167 if (ofproto->ofproto_class->flush) {
1168 ofproto->ofproto_class->flush(ofproto);
1171 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1172 struct rule *rule, *next_rule;
1173 struct cls_cursor cursor;
1175 if (table->flags & OFTABLE_HIDDEN) {
1179 ovs_rwlock_wrlock(&table->cls.rwlock);
1180 cls_cursor_init(&cursor, &table->cls, NULL);
1181 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1182 if (!rule->pending) {
1183 ofproto_rule_delete(ofproto, &table->cls, rule);
1186 ovs_rwlock_unlock(&table->cls.rwlock);
1190 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1193 ofproto_destroy__(struct ofproto *ofproto)
1195 struct oftable *table;
1197 ovs_assert(list_is_empty(&ofproto->pending));
1198 ovs_assert(!ofproto->n_pending);
1200 destroy_rule_executes(ofproto);
1201 guarded_list_destroy(&ofproto->rule_executes);
1203 delete_group(ofproto, OFPG_ALL);
1204 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1205 hmap_destroy(&ofproto->groups);
1207 connmgr_destroy(ofproto->connmgr);
1209 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1210 free(ofproto->name);
1211 free(ofproto->type);
1212 free(ofproto->mfr_desc);
1213 free(ofproto->hw_desc);
1214 free(ofproto->sw_desc);
1215 free(ofproto->serial_desc);
1216 free(ofproto->dp_desc);
1217 hmap_destroy(&ofproto->ports);
1218 shash_destroy(&ofproto->port_by_name);
1219 bitmap_free(ofproto->ofp_port_ids);
1220 simap_destroy(&ofproto->ofp_requests);
1222 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1223 oftable_destroy(table);
1225 free(ofproto->tables);
1227 hmap_destroy(&ofproto->deletions);
1229 free(ofproto->vlan_bitmap);
1231 ofproto->ofproto_class->dealloc(ofproto);
1235 ofproto_destroy(struct ofproto *p)
1237 struct ofport *ofport, *next_ofport;
1244 meter_delete(p, 1, p->meter_features.max_meters);
1245 p->meter_features.max_meters = 0;
1251 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1252 ofport_destroy(ofport);
1255 p->ofproto_class->destruct(p);
1256 ofproto_destroy__(p);
1259 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1260 * kernel datapath, for example, this destroys the datapath in the kernel, and
1261 * with the netdev-based datapath, it tears down the data structures that
1262 * represent the datapath.
1264 * The datapath should not be currently open as an ofproto. */
1266 ofproto_delete(const char *name, const char *type)
1268 const struct ofproto_class *class = ofproto_class_find__(type);
1269 return (!class ? EAFNOSUPPORT
1270 : !class->del ? EACCES
1271 : class->del(type, name));
1275 process_port_change(struct ofproto *ofproto, int error, char *devname)
1277 if (error == ENOBUFS) {
1278 reinit_ports(ofproto);
1279 } else if (!error) {
1280 update_port(ofproto, devname);
1286 ofproto_type_run(const char *datapath_type)
1288 const struct ofproto_class *class;
1291 datapath_type = ofproto_normalize_type(datapath_type);
1292 class = ofproto_class_find__(datapath_type);
1294 error = class->type_run ? class->type_run(datapath_type) : 0;
1295 if (error && error != EAGAIN) {
1296 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1297 datapath_type, ovs_strerror(error));
1303 ofproto_type_run_fast(const char *datapath_type)
1305 const struct ofproto_class *class;
1308 datapath_type = ofproto_normalize_type(datapath_type);
1309 class = ofproto_class_find__(datapath_type);
1311 error = class->type_run_fast ? class->type_run_fast(datapath_type) : 0;
1312 if (error && error != EAGAIN) {
1313 VLOG_ERR_RL(&rl, "%s: type_run_fast failed (%s)",
1314 datapath_type, ovs_strerror(error));
1320 ofproto_type_wait(const char *datapath_type)
1322 const struct ofproto_class *class;
1324 datapath_type = ofproto_normalize_type(datapath_type);
1325 class = ofproto_class_find__(datapath_type);
1327 if (class->type_wait) {
1328 class->type_wait(datapath_type);
1333 any_pending_ops(const struct ofproto *p)
1335 return !list_is_empty(&p->pending);
1339 ofproto_run(struct ofproto *p)
1341 struct sset changed_netdevs;
1342 const char *changed_netdev;
1343 struct ofport *ofport;
1346 error = p->ofproto_class->run(p);
1347 if (error && error != EAGAIN) {
1348 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1351 run_rule_executes(p);
1353 /* Restore the eviction group heap invariant occasionally. */
1354 if (p->eviction_group_timer < time_msec()) {
1357 p->eviction_group_timer = time_msec() + 1000;
1359 for (i = 0; i < p->n_tables; i++) {
1360 struct oftable *table = &p->tables[i];
1361 struct eviction_group *evg;
1362 struct cls_cursor cursor;
1365 if (!table->eviction_fields) {
1369 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1370 heap_rebuild(&evg->rules);
1373 ovs_rwlock_rdlock(&table->cls.rwlock);
1374 cls_cursor_init(&cursor, &table->cls, NULL);
1375 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1376 if (!rule->eviction_group
1377 && (rule->idle_timeout || rule->hard_timeout)) {
1378 eviction_group_add_rule(rule);
1381 ovs_rwlock_unlock(&table->cls.rwlock);
1385 if (p->ofproto_class->port_poll) {
1388 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1389 process_port_change(p, error, devname);
1393 /* Update OpenFlow port status for any port whose netdev has changed.
1395 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1396 * destroyed, so it's not safe to update ports directly from the
1397 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1398 * need this two-phase approach. */
1399 sset_init(&changed_netdevs);
1400 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1401 unsigned int change_seq = netdev_change_seq(ofport->netdev);
1402 if (ofport->change_seq != change_seq) {
1403 ofport->change_seq = change_seq;
1404 sset_add(&changed_netdevs, netdev_get_name(ofport->netdev));
1407 SSET_FOR_EACH (changed_netdev, &changed_netdevs) {
1408 update_port(p, changed_netdev);
1410 sset_destroy(&changed_netdevs);
1414 connmgr_run(p->connmgr, handle_openflow);
1418 connmgr_run(p->connmgr, NULL);
1420 if (!any_pending_ops(p)) {
1421 p->state = S_OPENFLOW;
1426 connmgr_run(p->connmgr, NULL);
1428 if (!any_pending_ops(p)) {
1429 connmgr_flushed(p->connmgr);
1430 p->state = S_OPENFLOW;
1438 if (time_msec() >= p->next_op_report) {
1439 long long int ago = (time_msec() - p->first_op) / 1000;
1440 long long int interval = (p->last_op - p->first_op) / 1000;
1444 ds_put_format(&s, "%d flow_mods ",
1445 p->n_add + p->n_delete + p->n_modify);
1446 if (interval == ago) {
1447 ds_put_format(&s, "in the last %lld s", ago);
1448 } else if (interval) {
1449 ds_put_format(&s, "in the %lld s starting %lld s ago",
1452 ds_put_format(&s, "%lld s ago", ago);
1455 ds_put_cstr(&s, " (");
1457 ds_put_format(&s, "%d adds, ", p->n_add);
1460 ds_put_format(&s, "%d deletes, ", p->n_delete);
1463 ds_put_format(&s, "%d modifications, ", p->n_modify);
1466 ds_put_char(&s, ')');
1468 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1471 p->n_add = p->n_delete = p->n_modify = 0;
1472 p->next_op_report = LLONG_MAX;
1478 /* Performs periodic activity required by 'ofproto' that needs to be done
1479 * with the least possible latency.
1481 * It makes sense to call this function a couple of times per poll loop, to
1482 * provide a significant performance boost on some benchmarks with the
1483 * ofproto-dpif implementation. */
1485 ofproto_run_fast(struct ofproto *p)
1489 error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0;
1490 if (error && error != EAGAIN) {
1491 VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)",
1492 p->name, ovs_strerror(error));
1498 ofproto_wait(struct ofproto *p)
1500 struct ofport *ofport;
1502 p->ofproto_class->wait(p);
1503 if (p->ofproto_class->port_poll_wait) {
1504 p->ofproto_class->port_poll_wait(p);
1507 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1508 if (ofport->change_seq != netdev_change_seq(ofport->netdev)) {
1509 poll_immediate_wake();
1515 connmgr_wait(p->connmgr, true);
1520 connmgr_wait(p->connmgr, false);
1521 if (!any_pending_ops(p)) {
1522 poll_immediate_wake();
1529 ofproto_is_alive(const struct ofproto *p)
1531 return connmgr_has_controllers(p->connmgr);
1534 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1535 * memory_report(). */
1537 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1539 const struct oftable *table;
1540 unsigned int n_rules;
1542 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1543 simap_increase(usage, "ops",
1544 ofproto->n_pending + hmap_count(&ofproto->deletions));
1547 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1548 ovs_rwlock_rdlock(&table->cls.rwlock);
1549 n_rules += classifier_count(&table->cls);
1550 ovs_rwlock_unlock(&table->cls.rwlock);
1552 simap_increase(usage, "rules", n_rules);
1554 if (ofproto->ofproto_class->get_memory_usage) {
1555 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1558 connmgr_get_memory_usage(ofproto->connmgr, usage);
1562 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1565 connmgr_get_controller_info(ofproto->connmgr, info);
1569 ofproto_free_ofproto_controller_info(struct shash *info)
1571 connmgr_free_controller_info(info);
1574 /* Makes a deep copy of 'old' into 'port'. */
1576 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1578 port->name = xstrdup(old->name);
1579 port->type = xstrdup(old->type);
1580 port->ofp_port = old->ofp_port;
1583 /* Frees memory allocated to members of 'ofproto_port'.
1585 * Do not call this function on an ofproto_port obtained from
1586 * ofproto_port_dump_next(): that function retains ownership of the data in the
1589 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1591 free(ofproto_port->name);
1592 free(ofproto_port->type);
1595 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1597 * This function provides no status indication. An error status for the entire
1598 * dump operation is provided when it is completed by calling
1599 * ofproto_port_dump_done().
1602 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1603 const struct ofproto *ofproto)
1605 dump->ofproto = ofproto;
1606 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1610 /* Attempts to retrieve another port from 'dump', which must have been created
1611 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1612 * 'port' and returns true. On failure, returns false.
1614 * Failure might indicate an actual error or merely that the last port has been
1615 * dumped. An error status for the entire dump operation is provided when it
1616 * is completed by calling ofproto_port_dump_done().
1618 * The ofproto owns the data stored in 'port'. It will remain valid until at
1619 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1620 * ofproto_port_dump_done(). */
1622 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1623 struct ofproto_port *port)
1625 const struct ofproto *ofproto = dump->ofproto;
1631 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1634 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1640 /* Completes port table dump operation 'dump', which must have been created
1641 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1642 * error-free, otherwise a positive errno value describing the problem. */
1644 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1646 const struct ofproto *ofproto = dump->ofproto;
1648 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1651 return dump->error == EOF ? 0 : dump->error;
1654 /* Returns the type to pass to netdev_open() when a datapath of type
1655 * 'datapath_type' has a port of type 'port_type', for a few special
1656 * cases when a netdev type differs from a port type. For example, when
1657 * using the userspace datapath, a port of type "internal" needs to be
1660 * Returns either 'type' itself or a string literal, which must not be
1663 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1665 const struct ofproto_class *class;
1667 datapath_type = ofproto_normalize_type(datapath_type);
1668 class = ofproto_class_find__(datapath_type);
1673 return (class->port_open_type
1674 ? class->port_open_type(datapath_type, port_type)
1678 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1679 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1680 * the port's OpenFlow port number.
1682 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1683 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1684 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1685 * 'ofp_portp' is non-null). */
1687 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1688 ofp_port_t *ofp_portp)
1690 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1693 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1695 const char *netdev_name = netdev_get_name(netdev);
1697 simap_put(&ofproto->ofp_requests, netdev_name,
1698 ofp_to_u16(ofp_port));
1699 update_port(ofproto, netdev_name);
1702 struct ofproto_port ofproto_port;
1704 ofproto_port_query_by_name(ofproto, netdev_get_name(netdev),
1706 *ofp_portp = error ? OFPP_NONE : ofproto_port.ofp_port;
1707 ofproto_port_destroy(&ofproto_port);
1712 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1713 * initializes '*port' appropriately; on failure, returns a positive errno
1716 * The caller owns the data in 'ofproto_port' and must free it with
1717 * ofproto_port_destroy() when it is no longer needed. */
1719 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1720 struct ofproto_port *port)
1724 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1726 memset(port, 0, sizeof *port);
1731 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1732 * Returns 0 if successful, otherwise a positive errno. */
1734 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1736 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1737 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1738 struct simap_node *ofp_request_node;
1741 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1742 if (ofp_request_node) {
1743 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1746 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1747 if (!error && ofport) {
1748 /* 'name' is the netdev's name and update_port() is going to close the
1749 * netdev. Just in case update_port() refers to 'name' after it
1750 * destroys 'ofport', make a copy of it around the update_port()
1752 char *devname = xstrdup(name);
1753 update_port(ofproto, devname);
1760 simple_flow_mod(struct ofproto *ofproto,
1761 const struct match *match, unsigned int priority,
1762 const struct ofpact *ofpacts, size_t ofpacts_len,
1763 enum ofp_flow_mod_command command)
1765 struct ofputil_flow_mod fm;
1767 memset(&fm, 0, sizeof fm);
1769 fm.priority = priority;
1772 fm.modify_cookie = false;
1774 fm.command = command;
1775 fm.idle_timeout = 0;
1776 fm.hard_timeout = 0;
1777 fm.buffer_id = UINT32_MAX;
1778 fm.out_port = OFPP_ANY;
1779 fm.out_group = OFPG_ANY;
1781 fm.ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1782 fm.ofpacts_len = ofpacts_len;
1783 return handle_flow_mod__(ofproto, NULL, &fm, NULL);
1786 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1787 * performs the 'n_actions' actions in 'actions'. The new flow will not
1790 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1791 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1792 * controllers; otherwise, it will be hidden.
1794 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1796 * This is a helper function for in-band control and fail-open. */
1798 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1799 unsigned int priority,
1800 const struct ofpact *ofpacts, size_t ofpacts_len)
1802 const struct rule *rule;
1805 /* First do a cheap check whether the rule we're looking for already exists
1806 * with the actions that we want. If it does, then we're done. */
1807 ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1808 rule = rule_from_cls_rule(classifier_find_match_exactly(
1809 &ofproto->tables[0].cls, match, priority));
1811 ovs_mutex_lock(&rule->mutex);
1812 must_add = !ofpacts_equal(rule->actions->ofpacts,
1813 rule->actions->ofpacts_len,
1814 ofpacts, ofpacts_len);
1815 ovs_mutex_unlock(&rule->mutex);
1819 ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1821 /* If there's no such rule or the rule doesn't have the actions we want,
1822 * fall back to a executing a full flow mod. We can't optimize this at
1823 * all because we didn't take enough locks above to ensure that the flow
1824 * table didn't already change beneath us. */
1826 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1827 OFPFC_MODIFY_STRICT);
1831 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1832 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1833 * operation cannot be initiated now but may be retried later.
1835 * This is a helper function for in-band control and fail-open. */
1837 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1839 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1842 /* Searches for a rule with matching criteria exactly equal to 'target' in
1843 * ofproto's table 0 and, if it finds one, deletes it.
1845 * This is a helper function for in-band control and fail-open. */
1847 ofproto_delete_flow(struct ofproto *ofproto,
1848 const struct match *target, unsigned int priority)
1850 struct classifier *cls = &ofproto->tables[0].cls;
1853 /* First do a cheap check whether the rule we're looking for has already
1854 * been deleted. If so, then we're done. */
1855 ovs_rwlock_rdlock(&cls->rwlock);
1856 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1858 ovs_rwlock_unlock(&cls->rwlock);
1863 /* Fall back to a executing a full flow mod. We can't optimize this at all
1864 * because we didn't take enough locks above to ensure that the flow table
1865 * didn't already change beneath us. */
1866 return simple_flow_mod(ofproto, target, priority, NULL, 0,
1867 OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE;
1870 /* Starts the process of deleting all of the flows from all of ofproto's flow
1871 * tables and then reintroducing the flows required by in-band control and
1872 * fail-open. The process will complete in a later call to ofproto_run(). */
1874 ofproto_flush_flows(struct ofproto *ofproto)
1876 COVERAGE_INC(ofproto_flush);
1877 ofproto->state = S_FLUSH;
1881 reinit_ports(struct ofproto *p)
1883 struct ofproto_port_dump dump;
1884 struct sset devnames;
1885 struct ofport *ofport;
1886 struct ofproto_port ofproto_port;
1887 const char *devname;
1889 COVERAGE_INC(ofproto_reinit_ports);
1891 sset_init(&devnames);
1892 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1893 sset_add(&devnames, netdev_get_name(ofport->netdev));
1895 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1896 sset_add(&devnames, ofproto_port.name);
1899 SSET_FOR_EACH (devname, &devnames) {
1900 update_port(p, devname);
1902 sset_destroy(&devnames);
1906 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
1910 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
1911 port_idx = port_idx ? port_idx : UINT16_MAX;
1913 if (port_idx >= ofproto->max_ports
1914 || bitmap_is_set(ofproto->ofp_port_ids, port_idx)) {
1915 uint16_t end_port_no = ofproto->alloc_port_no;
1917 /* Search for a free OpenFlow port number. We try not to
1918 * immediately reuse them to prevent problems due to old
1921 if (++ofproto->alloc_port_no >= ofproto->max_ports) {
1922 ofproto->alloc_port_no = 0;
1924 if (!bitmap_is_set(ofproto->ofp_port_ids,
1925 ofproto->alloc_port_no)) {
1926 port_idx = ofproto->alloc_port_no;
1929 if (ofproto->alloc_port_no == end_port_no) {
1934 bitmap_set1(ofproto->ofp_port_ids, port_idx);
1935 return u16_to_ofp(port_idx);
1939 dealloc_ofp_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
1941 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
1942 bitmap_set0(ofproto->ofp_port_ids, ofp_to_u16(ofp_port));
1946 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
1947 * pointer if the netdev cannot be opened. On success, also fills in
1949 static struct netdev *
1950 ofport_open(struct ofproto *ofproto,
1951 struct ofproto_port *ofproto_port,
1952 struct ofputil_phy_port *pp)
1954 enum netdev_flags flags;
1955 struct netdev *netdev;
1958 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
1960 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
1961 "cannot be opened (%s)",
1963 ofproto_port->name, ofproto_port->ofp_port,
1964 ofproto_port->name, ovs_strerror(error));
1968 if (ofproto_port->ofp_port == OFPP_NONE) {
1969 if (!strcmp(ofproto->name, ofproto_port->name)) {
1970 ofproto_port->ofp_port = OFPP_LOCAL;
1972 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
1973 ofproto_port->name);
1976 pp->port_no = ofproto_port->ofp_port;
1977 netdev_get_etheraddr(netdev, pp->hw_addr);
1978 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
1979 netdev_get_flags(netdev, &flags);
1980 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
1981 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
1982 netdev_get_features(netdev, &pp->curr, &pp->advertised,
1983 &pp->supported, &pp->peer);
1984 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
1985 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
1990 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1991 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
1994 ofport_equal(const struct ofputil_phy_port *a,
1995 const struct ofputil_phy_port *b)
1997 return (eth_addr_equals(a->hw_addr, b->hw_addr)
1998 && a->state == b->state
1999 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
2000 && a->curr == b->curr
2001 && a->advertised == b->advertised
2002 && a->supported == b->supported
2003 && a->peer == b->peer
2004 && a->curr_speed == b->curr_speed
2005 && a->max_speed == b->max_speed);
2008 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2009 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2010 * one with the same name or port number). */
2012 ofport_install(struct ofproto *p,
2013 struct netdev *netdev, const struct ofputil_phy_port *pp)
2015 const char *netdev_name = netdev_get_name(netdev);
2016 struct ofport *ofport;
2019 /* Create ofport. */
2020 ofport = p->ofproto_class->port_alloc();
2025 ofport->ofproto = p;
2026 ofport->netdev = netdev;
2027 ofport->change_seq = netdev_change_seq(netdev);
2029 ofport->ofp_port = pp->port_no;
2030 ofport->created = time_msec();
2032 /* Add port to 'p'. */
2033 hmap_insert(&p->ports, &ofport->hmap_node,
2034 hash_ofp_port(ofport->ofp_port));
2035 shash_add(&p->port_by_name, netdev_name, ofport);
2037 update_mtu(p, ofport);
2039 /* Let the ofproto_class initialize its private data. */
2040 error = p->ofproto_class->port_construct(ofport);
2044 connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD);
2048 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
2049 p->name, netdev_name, ovs_strerror(error));
2051 ofport_destroy__(ofport);
2053 netdev_close(netdev);
2057 /* Removes 'ofport' from 'p' and destroys it. */
2059 ofport_remove(struct ofport *ofport)
2061 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp,
2063 ofport_destroy(ofport);
2066 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2069 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2071 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2073 ofport_remove(port);
2077 /* Updates 'port' with new 'pp' description.
2079 * Does not handle a name or port number change. The caller must implement
2080 * such a change as a delete followed by an add. */
2082 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
2084 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2085 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2086 | (pp->config & OFPUTIL_PC_PORT_DOWN));
2087 port->pp.state = pp->state;
2088 port->pp.curr = pp->curr;
2089 port->pp.advertised = pp->advertised;
2090 port->pp.supported = pp->supported;
2091 port->pp.peer = pp->peer;
2092 port->pp.curr_speed = pp->curr_speed;
2093 port->pp.max_speed = pp->max_speed;
2095 connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY);
2098 /* Update OpenFlow 'state' in 'port' and notify controller. */
2100 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
2102 if (port->pp.state != state) {
2103 port->pp.state = state;
2104 connmgr_send_port_status(port->ofproto->connmgr, &port->pp,
2110 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
2112 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2114 if (port->ofproto->ofproto_class->set_realdev) {
2115 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2117 if (port->ofproto->ofproto_class->set_stp_port) {
2118 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2120 if (port->ofproto->ofproto_class->set_cfm) {
2121 port->ofproto->ofproto_class->set_cfm(port, NULL);
2123 if (port->ofproto->ofproto_class->bundle_remove) {
2124 port->ofproto->ofproto_class->bundle_remove(port);
2130 ofport_destroy__(struct ofport *port)
2132 struct ofproto *ofproto = port->ofproto;
2133 const char *name = netdev_get_name(port->netdev);
2135 hmap_remove(&ofproto->ports, &port->hmap_node);
2136 shash_delete(&ofproto->port_by_name,
2137 shash_find(&ofproto->port_by_name, name));
2139 netdev_close(port->netdev);
2140 ofproto->ofproto_class->port_dealloc(port);
2144 ofport_destroy(struct ofport *port)
2147 dealloc_ofp_port(port->ofproto, port->ofp_port);
2148 port->ofproto->ofproto_class->port_destruct(port);
2149 ofport_destroy__(port);
2154 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2156 struct ofport *port;
2158 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2160 if (port->ofp_port == ofp_port) {
2168 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2170 struct ofproto *ofproto = port->ofproto;
2173 if (ofproto->ofproto_class->port_get_stats) {
2174 error = ofproto->ofproto_class->port_get_stats(port, stats);
2183 update_port(struct ofproto *ofproto, const char *name)
2185 struct ofproto_port ofproto_port;
2186 struct ofputil_phy_port pp;
2187 struct netdev *netdev;
2188 struct ofport *port;
2190 COVERAGE_INC(ofproto_update_port);
2192 /* Fetch 'name''s location and properties from the datapath. */
2193 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2194 ? ofport_open(ofproto, &ofproto_port, &pp)
2198 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2199 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2200 struct netdev *old_netdev = port->netdev;
2202 /* 'name' hasn't changed location. Any properties changed? */
2203 if (!ofport_equal(&port->pp, &pp)) {
2204 ofport_modified(port, &pp);
2207 update_mtu(ofproto, port);
2209 /* Install the newly opened netdev in case it has changed.
2210 * Don't close the old netdev yet in case port_modified has to
2211 * remove a retained reference to it.*/
2212 port->netdev = netdev;
2213 port->change_seq = netdev_change_seq(netdev);
2215 if (port->ofproto->ofproto_class->port_modified) {
2216 port->ofproto->ofproto_class->port_modified(port);
2219 netdev_close(old_netdev);
2221 /* If 'port' is nonnull then its name differs from 'name' and thus
2222 * we should delete it. If we think there's a port named 'name'
2223 * then its port number must be wrong now so delete it too. */
2225 ofport_remove(port);
2227 ofport_remove_with_name(ofproto, name);
2228 ofport_install(ofproto, netdev, &pp);
2231 /* Any port named 'name' is gone now. */
2232 ofport_remove_with_name(ofproto, name);
2234 ofproto_port_destroy(&ofproto_port);
2238 init_ports(struct ofproto *p)
2240 struct ofproto_port_dump dump;
2241 struct ofproto_port ofproto_port;
2242 struct shash_node *node, *next;
2244 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2245 const char *name = ofproto_port.name;
2247 if (shash_find(&p->port_by_name, name)) {
2248 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2251 struct ofputil_phy_port pp;
2252 struct netdev *netdev;
2254 /* Check if an OpenFlow port number had been requested. */
2255 node = shash_find(&init_ofp_ports, name);
2257 const struct iface_hint *iface_hint = node->data;
2258 simap_put(&p->ofp_requests, name,
2259 ofp_to_u16(iface_hint->ofp_port));
2262 netdev = ofport_open(p, &ofproto_port, &pp);
2264 ofport_install(p, netdev, &pp);
2265 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2266 p->alloc_port_no = MAX(p->alloc_port_no,
2267 ofp_to_u16(ofproto_port.ofp_port));
2273 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2274 struct iface_hint *iface_hint = node->data;
2276 if (!strcmp(iface_hint->br_name, p->name)) {
2277 free(iface_hint->br_name);
2278 free(iface_hint->br_type);
2280 shash_delete(&init_ofp_ports, node);
2287 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2288 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2290 find_min_mtu(struct ofproto *p)
2292 struct ofport *ofport;
2295 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2296 struct netdev *netdev = ofport->netdev;
2299 /* Skip any internal ports, since that's what we're trying to
2301 if (!strcmp(netdev_get_type(netdev), "internal")) {
2305 if (netdev_get_mtu(netdev, &dev_mtu)) {
2308 if (!mtu || dev_mtu < mtu) {
2313 return mtu ? mtu: ETH_PAYLOAD_MAX;
2316 /* Update MTU of all datapath devices on 'p' to the minimum of the
2317 * non-datapath ports in event of 'port' added or changed. */
2319 update_mtu(struct ofproto *p, struct ofport *port)
2321 struct ofport *ofport;
2322 struct netdev *netdev = port->netdev;
2323 int dev_mtu, old_min;
2325 if (netdev_get_mtu(netdev, &dev_mtu)) {
2329 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2330 if (dev_mtu > p->min_mtu) {
2331 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2332 dev_mtu = p->min_mtu;
2335 port->mtu = dev_mtu;
2339 /* For non-internal port find new min mtu. */
2340 old_min = p->min_mtu;
2341 port->mtu = dev_mtu;
2342 p->min_mtu = find_min_mtu(p);
2343 if (p->min_mtu == old_min) {
2347 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2348 struct netdev *netdev = ofport->netdev;
2350 if (!strcmp(netdev_get_type(netdev), "internal")) {
2351 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2352 ofport->mtu = p->min_mtu;
2359 ofproto_rule_ref(struct rule *rule)
2364 atomic_add(&rule->ref_count, 1, &orig);
2365 ovs_assert(orig != 0);
2370 ofproto_rule_unref(struct rule *rule)
2375 atomic_sub(&rule->ref_count, 1, &orig);
2377 rule->ofproto->ofproto_class->rule_destruct(rule);
2378 ofproto_rule_destroy__(rule);
2380 ovs_assert(orig != 0);
2386 ofproto_rule_destroy__(struct rule *rule)
2388 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2389 rule_actions_unref(rule->actions);
2390 ovs_mutex_destroy(&rule->mutex);
2391 rule->ofproto->ofproto_class->rule_dealloc(rule);
2394 /* Creates and returns a new 'struct rule_actions', with a ref_count of 1,
2395 * whose actions are a copy of from the 'ofpacts_len' bytes of 'ofpacts'. */
2396 struct rule_actions *
2397 rule_actions_create(const struct ofpact *ofpacts, size_t ofpacts_len)
2399 struct rule_actions *actions;
2401 actions = xmalloc(sizeof *actions);
2402 atomic_init(&actions->ref_count, 1);
2403 actions->ofpacts = xmemdup(ofpacts, ofpacts_len);
2404 actions->ofpacts_len = ofpacts_len;
2405 actions->meter_id = ofpacts_get_meter(ofpacts, ofpacts_len);
2409 /* Increments 'actions''s ref_count. */
2411 rule_actions_ref(struct rule_actions *actions)
2416 atomic_add(&actions->ref_count, 1, &orig);
2417 ovs_assert(orig != 0);
2421 /* Decrements 'actions''s ref_count and frees 'actions' if the ref_count
2424 rule_actions_unref(struct rule_actions *actions)
2429 atomic_sub(&actions->ref_count, 1, &orig);
2433 ovs_assert(orig != 0);
2438 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2439 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2441 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2443 return (port == OFPP_ANY
2444 || ofpacts_output_to_port(rule->actions->ofpacts,
2445 rule->actions->ofpacts_len, port));
2448 /* Returns true if 'rule' has group and equals group_id. */
2450 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2452 return (group_id == OFPG11_ANY
2453 || ofpacts_output_to_group(rule->actions->ofpacts,
2454 rule->actions->ofpacts_len, group_id));
2457 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2458 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2460 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2462 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2467 case OFOPERATION_ADD:
2468 case OFOPERATION_DELETE:
2471 case OFOPERATION_MODIFY:
2472 case OFOPERATION_REPLACE:
2473 return ofpacts_output_to_port(op->actions->ofpacts,
2474 op->actions->ofpacts_len, out_port);
2481 rule_execute_destroy(struct rule_execute *e)
2483 ofproto_rule_unref(e->rule);
2484 list_remove(&e->list_node);
2488 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2489 * by passing them to the ofproto provider. */
2491 run_rule_executes(struct ofproto *ofproto)
2493 struct rule_execute *e, *next;
2494 struct list executes;
2496 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2497 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2498 union flow_in_port in_port_;
2501 in_port_.ofp_port = e->in_port;
2502 flow_extract(e->packet, 0, 0, NULL, &in_port_, &flow);
2503 ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet);
2505 rule_execute_destroy(e);
2509 /* Destroys and discards all "rule_execute" operations queued up in
2510 * ofproto->rule_executes. */
2512 destroy_rule_executes(struct ofproto *ofproto)
2514 struct rule_execute *e, *next;
2515 struct list executes;
2517 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2518 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2519 ofpbuf_delete(e->packet);
2520 rule_execute_destroy(e);
2524 /* Returns true if 'rule' should be hidden from the controller.
2526 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2527 * (e.g. by in-band control) and are intentionally hidden from the
2530 ofproto_rule_is_hidden(const struct rule *rule)
2532 return rule->cr.priority > UINT16_MAX;
2535 static enum oftable_flags
2536 rule_get_flags(const struct rule *rule)
2538 return rule->ofproto->tables[rule->table_id].flags;
2542 rule_is_modifiable(const struct rule *rule)
2544 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2548 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2550 ofconn_send_reply(ofconn, make_echo_reply(oh));
2555 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2557 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2558 struct ofputil_switch_features features;
2559 struct ofport *port;
2565 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2567 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2569 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2570 * if present, are always at the end.) */
2571 n_tables = ofproto->n_tables;
2572 for (i = 0; i < ofproto->n_tables; i++) {
2573 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2579 features.datapath_id = ofproto->datapath_id;
2580 features.n_buffers = pktbuf_capacity();
2581 features.n_tables = n_tables;
2582 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2583 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2585 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2587 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2588 features.auxiliary_id = 0;
2589 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2591 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2592 ofputil_put_switch_features_port(&port->pp, b);
2595 ofconn_send_reply(ofconn, b);
2600 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2602 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2603 struct ofp_switch_config *osc;
2604 enum ofp_config_flags flags;
2608 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2609 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2610 flags = ofproto->frag_handling;
2611 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2612 if (oh->version < OFP13_VERSION
2613 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2614 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2616 osc->flags = htons(flags);
2617 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2618 ofconn_send_reply(ofconn, buf);
2624 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2626 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2627 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2628 uint16_t flags = ntohs(osc->flags);
2630 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2631 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2632 enum ofp_config_flags cur = ofproto->frag_handling;
2633 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2635 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2637 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2638 ofproto->frag_handling = next;
2640 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2642 ofputil_frag_handling_to_string(next));
2646 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2647 ofconn_set_invalid_ttl_to_controller(ofconn,
2648 (oh->version < OFP13_VERSION
2649 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2651 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2656 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2657 * error message code for the caller to propagate upward. Otherwise, returns
2660 * The log message mentions 'msg_type'. */
2662 reject_slave_controller(struct ofconn *ofconn)
2664 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2665 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2666 return OFPERR_OFPBRC_EPERM;
2672 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate
2673 * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'.
2674 * 'flow' may be temporarily modified, but is restored at return.
2677 ofproto_check_ofpacts(struct ofproto *ofproto,
2678 const struct ofpact ofpacts[], size_t ofpacts_len,
2679 struct flow *flow, uint8_t table_id)
2684 error = ofpacts_check(ofpacts, ofpacts_len, flow,
2685 u16_to_ofp(ofproto->max_ports), table_id);
2690 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2691 if (mid && ofproto_get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2692 return OFPERR_OFPMMFC_INVALID_METER;
2698 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2700 struct ofproto *p = ofconn_get_ofproto(ofconn);
2701 struct ofputil_packet_out po;
2702 struct ofpbuf *payload;
2703 uint64_t ofpacts_stub[1024 / 8];
2704 struct ofpbuf ofpacts;
2706 union flow_in_port in_port_;
2709 COVERAGE_INC(ofproto_packet_out);
2711 error = reject_slave_controller(ofconn);
2716 /* Decode message. */
2717 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2718 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2720 goto exit_free_ofpacts;
2722 if (ofp_to_u16(po.in_port) >= p->max_ports
2723 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2724 error = OFPERR_OFPBRC_BAD_PORT;
2725 goto exit_free_ofpacts;
2730 if (po.buffer_id != UINT32_MAX) {
2731 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2732 if (error || !payload) {
2733 goto exit_free_ofpacts;
2736 /* Ensure that the L3 header is 32-bit aligned. */
2737 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2740 /* Verify actions against packet, then send packet if successful. */
2741 in_port_.ofp_port = po.in_port;
2742 flow_extract(payload, 0, 0, NULL, &in_port_, &flow);
2743 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len, &flow, 0);
2745 error = p->ofproto_class->packet_out(p, payload, &flow,
2746 po.ofpacts, po.ofpacts_len);
2748 ofpbuf_delete(payload);
2751 ofpbuf_uninit(&ofpacts);
2757 update_port_config(struct ofport *port,
2758 enum ofputil_port_config config,
2759 enum ofputil_port_config mask)
2761 enum ofputil_port_config old_config = port->pp.config;
2762 enum ofputil_port_config toggle;
2764 toggle = (config ^ port->pp.config) & mask;
2765 if (toggle & OFPUTIL_PC_PORT_DOWN) {
2766 if (config & OFPUTIL_PC_PORT_DOWN) {
2767 netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL);
2769 netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL);
2771 toggle &= ~OFPUTIL_PC_PORT_DOWN;
2774 port->pp.config ^= toggle;
2775 if (port->pp.config != old_config) {
2776 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2781 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2783 struct ofproto *p = ofconn_get_ofproto(ofconn);
2784 struct ofputil_port_mod pm;
2785 struct ofport *port;
2788 error = reject_slave_controller(ofconn);
2793 error = ofputil_decode_port_mod(oh, &pm);
2798 port = ofproto_get_port(p, pm.port_no);
2800 return OFPERR_OFPPMFC_BAD_PORT;
2801 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
2802 return OFPERR_OFPPMFC_BAD_HW_ADDR;
2804 update_port_config(port, pm.config, pm.mask);
2806 netdev_set_advertisements(port->netdev, pm.advertise);
2813 handle_desc_stats_request(struct ofconn *ofconn,
2814 const struct ofp_header *request)
2816 static const char *default_mfr_desc = "Nicira, Inc.";
2817 static const char *default_hw_desc = "Open vSwitch";
2818 static const char *default_sw_desc = VERSION;
2819 static const char *default_serial_desc = "None";
2820 static const char *default_dp_desc = "None";
2822 struct ofproto *p = ofconn_get_ofproto(ofconn);
2823 struct ofp_desc_stats *ods;
2826 msg = ofpraw_alloc_stats_reply(request, 0);
2827 ods = ofpbuf_put_zeros(msg, sizeof *ods);
2828 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
2829 sizeof ods->mfr_desc);
2830 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
2831 sizeof ods->hw_desc);
2832 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
2833 sizeof ods->sw_desc);
2834 ovs_strlcpy(ods->serial_num,
2835 p->serial_desc ? p->serial_desc : default_serial_desc,
2836 sizeof ods->serial_num);
2837 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
2838 sizeof ods->dp_desc);
2839 ofconn_send_reply(ofconn, msg);
2845 handle_table_stats_request(struct ofconn *ofconn,
2846 const struct ofp_header *request)
2848 struct ofproto *p = ofconn_get_ofproto(ofconn);
2849 struct ofp12_table_stats *ots;
2854 /* Set up default values.
2856 * ofp12_table_stats is used as a generic structure as
2857 * it is able to hold all the fields for ofp10_table_stats
2858 * and ofp11_table_stats (and of course itself).
2860 ots = xcalloc(p->n_tables, sizeof *ots);
2861 for (i = 0; i < p->n_tables; i++) {
2862 ots[i].table_id = i;
2863 sprintf(ots[i].name, "table%zu", i);
2864 ots[i].match = htonll(OFPXMT12_MASK);
2865 ots[i].wildcards = htonll(OFPXMT12_MASK);
2866 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
2867 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
2868 ots[i].write_setfields = htonll(OFPXMT12_MASK);
2869 ots[i].apply_setfields = htonll(OFPXMT12_MASK);
2870 ots[i].metadata_match = htonll(UINT64_MAX);
2871 ots[i].metadata_write = htonll(UINT64_MAX);
2872 ots[i].instructions = htonl(OFPIT11_ALL);
2873 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
2874 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
2875 ovs_rwlock_rdlock(&p->tables[i].cls.rwlock);
2876 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
2877 ovs_rwlock_unlock(&p->tables[i].cls.rwlock);
2880 p->ofproto_class->get_tables(p, ots);
2882 /* Post-process the tables, dropping hidden tables. */
2883 n_tables = p->n_tables;
2884 for (i = 0; i < p->n_tables; i++) {
2885 const struct oftable *table = &p->tables[i];
2887 if (table->flags & OFTABLE_HIDDEN) {
2893 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
2896 if (table->max_flows < ntohl(ots[i].max_entries)) {
2897 ots[i].max_entries = htonl(table->max_flows);
2901 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
2902 ofconn_send_reply(ofconn, msg);
2910 append_port_stat(struct ofport *port, struct list *replies)
2912 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
2914 calc_duration(port->created, time_msec(),
2915 &ops.duration_sec, &ops.duration_nsec);
2917 /* Intentionally ignore return value, since errors will set
2918 * 'stats' to all-1s, which is correct for OpenFlow, and
2919 * netdev_get_stats() will log errors. */
2920 ofproto_port_get_stats(port, &ops.stats);
2922 ofputil_append_port_stat(replies, &ops);
2926 handle_port_stats_request(struct ofconn *ofconn,
2927 const struct ofp_header *request)
2929 struct ofproto *p = ofconn_get_ofproto(ofconn);
2930 struct ofport *port;
2931 struct list replies;
2935 error = ofputil_decode_port_stats_request(request, &port_no);
2940 ofpmp_init(&replies, request);
2941 if (port_no != OFPP_ANY) {
2942 port = ofproto_get_port(p, port_no);
2944 append_port_stat(port, &replies);
2947 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2948 append_port_stat(port, &replies);
2952 ofconn_send_replies(ofconn, &replies);
2957 handle_port_desc_stats_request(struct ofconn *ofconn,
2958 const struct ofp_header *request)
2960 struct ofproto *p = ofconn_get_ofproto(ofconn);
2961 enum ofp_version version;
2962 struct ofport *port;
2963 struct list replies;
2965 ofpmp_init(&replies, request);
2967 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
2968 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2969 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
2972 ofconn_send_replies(ofconn, &replies);
2977 hash_cookie(ovs_be64 cookie)
2979 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
2980 (OVS_FORCE uint64_t)cookie);
2984 cookies_insert(struct ofproto *ofproto, struct rule *rule)
2985 OVS_REQUIRES(ofproto_mutex)
2987 hindex_insert(&ofproto->cookies, &rule->cookie_node,
2988 hash_cookie(rule->flow_cookie));
2992 cookies_remove(struct ofproto *ofproto, struct rule *rule)
2993 OVS_REQUIRES(ofproto_mutex)
2995 hindex_remove(&ofproto->cookies, &rule->cookie_node);
2999 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
3000 ovs_be64 new_cookie)
3002 if (new_cookie != rule->flow_cookie) {
3003 ovs_mutex_lock(&ofproto_mutex);
3004 cookies_remove(ofproto, rule);
3006 ovs_mutex_lock(&rule->mutex);
3007 rule->flow_cookie = new_cookie;
3008 ovs_mutex_unlock(&rule->mutex);
3010 cookies_insert(ofproto, rule);
3011 ovs_mutex_unlock(&ofproto_mutex);
3016 calc_duration(long long int start, long long int now,
3017 uint32_t *sec, uint32_t *nsec)
3019 long long int msecs = now - start;
3020 *sec = msecs / 1000;
3021 *nsec = (msecs % 1000) * (1000 * 1000);
3024 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3025 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
3027 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3029 return (table_id == 0xff || table_id < ofproto->n_tables
3031 : OFPERR_OFPBRC_BAD_TABLE_ID);
3035 static struct oftable *
3036 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
3038 struct oftable *table;
3040 for (table = &ofproto->tables[table_id];
3041 table < &ofproto->tables[ofproto->n_tables];
3043 if (!(table->flags & OFTABLE_HIDDEN)) {
3051 static struct oftable *
3052 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
3054 if (table_id == 0xff) {
3055 return next_visible_table(ofproto, 0);
3056 } else if (table_id < ofproto->n_tables) {
3057 return &ofproto->tables[table_id];
3063 static struct oftable *
3064 next_matching_table(const struct ofproto *ofproto,
3065 const struct oftable *table, uint8_t table_id)
3067 return (table_id == 0xff
3068 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
3072 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3074 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3075 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3077 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3078 * only once, for that table. (This can be used to access tables marked
3081 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3082 * entered at all. (Perhaps you should have validated TABLE_ID with
3083 * check_table_id().)
3085 * All parameters are evaluated multiple times.
3087 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3088 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3090 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3092 /* Initializes 'criteria' in a straightforward way based on the other
3095 * For "loose" matching, the 'priority' parameter is unimportant and may be
3098 rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
3099 const struct match *match, unsigned int priority,
3100 ovs_be64 cookie, ovs_be64 cookie_mask,
3101 ofp_port_t out_port, uint32_t out_group)
3103 criteria->table_id = table_id;
3104 cls_rule_init(&criteria->cr, match, priority);
3105 criteria->cookie = cookie;
3106 criteria->cookie_mask = cookie_mask;
3107 criteria->out_port = out_port;
3108 criteria->out_group = out_group;
3112 rule_criteria_destroy(struct rule_criteria *criteria)
3114 cls_rule_destroy(&criteria->cr);
3118 rule_collection_init(struct rule_collection *rules)
3120 rules->rules = rules->stub;
3122 rules->capacity = ARRAY_SIZE(rules->stub);
3126 rule_collection_add(struct rule_collection *rules, struct rule *rule)
3128 if (rules->n >= rules->capacity) {
3129 size_t old_size, new_size;
3131 old_size = rules->capacity * sizeof *rules->rules;
3132 rules->capacity *= 2;
3133 new_size = rules->capacity * sizeof *rules->rules;
3135 if (rules->rules == rules->stub) {
3136 rules->rules = xmalloc(new_size);
3137 memcpy(rules->rules, rules->stub, old_size);
3139 rules->rules = xrealloc(rules->rules, new_size);
3143 rules->rules[rules->n++] = rule;
3147 rule_collection_destroy(struct rule_collection *rules)
3149 if (rules->rules != rules->stub) {
3155 collect_rule(struct rule *rule, const struct rule_criteria *c,
3156 struct rule_collection *rules)
3158 if (ofproto_rule_is_hidden(rule)) {
3160 } else if (rule->pending) {
3161 return OFPROTO_POSTPONE;
3163 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3164 && ofproto_rule_has_out_port(rule, c->out_port)
3165 && ofproto_rule_has_out_group(rule, c->out_group)
3166 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)) {
3167 rule_collection_add(rules, rule);
3173 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3174 * on classifiers rules are done in the "loose" way required for OpenFlow
3175 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3178 * Hidden rules are always omitted.
3180 * Returns 0 on success, otherwise an OpenFlow error code. */
3182 collect_rules_loose(struct ofproto *ofproto,
3183 const struct rule_criteria *criteria,
3184 struct rule_collection *rules)
3186 struct oftable *table;
3189 rule_collection_init(rules);
3191 error = check_table_id(ofproto, criteria->table_id);
3196 if (criteria->cookie_mask == htonll(UINT64_MAX)) {
3199 ovs_mutex_lock(&ofproto_mutex);
3200 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3201 hash_cookie(criteria->cookie),
3202 &ofproto->cookies) {
3203 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
3204 error = collect_rule(rule, criteria, rules);
3210 ovs_mutex_unlock(&ofproto_mutex);
3212 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3213 struct cls_cursor cursor;
3216 ovs_rwlock_rdlock(&table->cls.rwlock);
3217 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
3218 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3219 error = collect_rule(rule, criteria, rules);
3224 ovs_rwlock_unlock(&table->cls.rwlock);
3230 rule_collection_destroy(rules);
3235 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3236 * on classifiers rules are done in the "strict" way required for OpenFlow
3237 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3238 * rules on list 'rules'.
3240 * Hidden rules are always omitted.
3242 * Returns 0 on success, otherwise an OpenFlow error code. */
3244 collect_rules_strict(struct ofproto *ofproto,
3245 const struct rule_criteria *criteria,
3246 struct rule_collection *rules)
3248 struct oftable *table;
3251 rule_collection_init(rules);
3253 error = check_table_id(ofproto, criteria->table_id);
3258 if (criteria->cookie_mask == htonll(UINT64_MAX)) {
3261 ovs_mutex_lock(&ofproto_mutex);
3262 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3263 hash_cookie(criteria->cookie),
3264 &ofproto->cookies) {
3265 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
3266 error = collect_rule(rule, criteria, rules);
3272 ovs_mutex_unlock(&ofproto_mutex);
3274 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
3277 ovs_rwlock_rdlock(&table->cls.rwlock);
3278 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3279 &table->cls, &criteria->cr));
3280 ovs_rwlock_unlock(&table->cls.rwlock);
3282 error = collect_rule(rule, criteria, rules);
3292 rule_collection_destroy(rules);
3297 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3298 * forced into the range of a uint16_t. */
3300 age_secs(long long int age_ms)
3302 return (age_ms < 0 ? 0
3303 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3304 : (unsigned int) age_ms / 1000);
3308 handle_flow_stats_request(struct ofconn *ofconn,
3309 const struct ofp_header *request)
3311 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3312 struct ofputil_flow_stats_request fsr;
3313 struct rule_criteria criteria;
3314 struct rule_collection rules;
3315 struct list replies;
3319 error = ofputil_decode_flow_stats_request(&fsr, request);
3324 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3325 fsr.cookie_mask, fsr.out_port, fsr.out_group);
3326 error = collect_rules_loose(ofproto, &criteria, &rules);
3327 rule_criteria_destroy(&criteria);
3332 ofpmp_init(&replies, request);
3333 for (i = 0; i < rules.n; i++) {
3334 struct rule *rule = rules.rules[i];
3335 long long int now = time_msec();
3336 struct ofputil_flow_stats fs;
3338 minimatch_expand(&rule->cr.match, &fs.match);
3339 fs.priority = rule->cr.priority;
3340 fs.cookie = rule->flow_cookie;
3341 fs.table_id = rule->table_id;
3342 calc_duration(rule->created, now, &fs.duration_sec, &fs.duration_nsec);
3343 fs.idle_age = age_secs(now - rule->used);
3344 fs.hard_age = age_secs(now - rule->modified);
3345 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3347 fs.ofpacts = rule->actions->ofpacts;
3348 fs.ofpacts_len = rule->actions->ofpacts_len;
3350 ovs_mutex_lock(&rule->mutex);
3351 fs.idle_timeout = rule->idle_timeout;
3352 fs.hard_timeout = rule->hard_timeout;
3353 ovs_mutex_unlock(&rule->mutex);
3355 fs.flags = rule->flags;
3357 ofputil_append_flow_stats_reply(&fs, &replies);
3359 rule_collection_destroy(&rules);
3361 ofconn_send_replies(ofconn, &replies);
3367 flow_stats_ds(struct rule *rule, struct ds *results)
3369 uint64_t packet_count, byte_count;
3371 rule->ofproto->ofproto_class->rule_get_stats(rule,
3372 &packet_count, &byte_count);
3374 if (rule->table_id != 0) {
3375 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3377 ds_put_format(results, "duration=%llds, ",
3378 (time_msec() - rule->created) / 1000);
3379 ds_put_format(results, "priority=%u, ", rule->cr.priority);
3380 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3381 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3382 cls_rule_format(&rule->cr, results);
3383 ds_put_char(results, ',');
3384 ofpacts_format(rule->actions->ofpacts, rule->actions->ofpacts_len,
3386 ds_put_cstr(results, "\n");
3389 /* Adds a pretty-printed description of all flows to 'results', including
3390 * hidden flows (e.g., set up by in-band control). */
3392 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3394 struct oftable *table;
3396 OFPROTO_FOR_EACH_TABLE (table, p) {
3397 struct cls_cursor cursor;
3400 ovs_rwlock_rdlock(&table->cls.rwlock);
3401 cls_cursor_init(&cursor, &table->cls, NULL);
3402 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3403 flow_stats_ds(rule, results);
3405 ovs_rwlock_unlock(&table->cls.rwlock);
3409 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3410 * '*engine_type' and '*engine_id', respectively. */
3412 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3413 uint8_t *engine_type, uint8_t *engine_id)
3415 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3418 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3419 * true if the port's CFM status was successfully stored into '*status'.
3420 * Returns false if the port did not have CFM configured, in which case
3421 * '*status' is indeterminate.
3423 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3425 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3426 struct ofproto_cfm_status *status)
3428 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3430 && ofproto->ofproto_class->get_cfm_status
3431 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3435 handle_aggregate_stats_request(struct ofconn *ofconn,
3436 const struct ofp_header *oh)
3438 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3439 struct ofputil_flow_stats_request request;
3440 struct ofputil_aggregate_stats stats;
3441 bool unknown_packets, unknown_bytes;
3442 struct rule_criteria criteria;
3443 struct rule_collection rules;
3444 struct ofpbuf *reply;
3448 error = ofputil_decode_flow_stats_request(&request, oh);
3453 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3454 request.cookie, request.cookie_mask,
3455 request.out_port, request.out_group);
3456 error = collect_rules_loose(ofproto, &criteria, &rules);
3457 rule_criteria_destroy(&criteria);
3462 memset(&stats, 0, sizeof stats);
3463 unknown_packets = unknown_bytes = false;
3464 for (i = 0; i < rules.n; i++) {
3465 struct rule *rule = rules.rules[i];
3466 uint64_t packet_count;
3467 uint64_t byte_count;
3469 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3472 if (packet_count == UINT64_MAX) {
3473 unknown_packets = true;
3475 stats.packet_count += packet_count;
3478 if (byte_count == UINT64_MAX) {
3479 unknown_bytes = true;
3481 stats.byte_count += byte_count;
3486 if (unknown_packets) {
3487 stats.packet_count = UINT64_MAX;
3489 if (unknown_bytes) {
3490 stats.byte_count = UINT64_MAX;
3493 rule_collection_destroy(&rules);
3495 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3496 ofconn_send_reply(ofconn, reply);
3501 struct queue_stats_cbdata {
3502 struct ofport *ofport;
3503 struct list replies;
3508 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3509 const struct netdev_queue_stats *stats)
3511 struct ofputil_queue_stats oqs;
3513 oqs.port_no = cbdata->ofport->pp.port_no;
3514 oqs.queue_id = queue_id;
3515 oqs.tx_bytes = stats->tx_bytes;
3516 oqs.tx_packets = stats->tx_packets;
3517 oqs.tx_errors = stats->tx_errors;
3518 if (stats->created != LLONG_MIN) {
3519 calc_duration(stats->created, cbdata->now,
3520 &oqs.duration_sec, &oqs.duration_nsec);
3522 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3524 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3528 handle_queue_stats_dump_cb(uint32_t queue_id,
3529 struct netdev_queue_stats *stats,
3532 struct queue_stats_cbdata *cbdata = cbdata_;
3534 put_queue_stats(cbdata, queue_id, stats);
3538 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3539 struct queue_stats_cbdata *cbdata)
3541 cbdata->ofport = port;
3542 if (queue_id == OFPQ_ALL) {
3543 netdev_dump_queue_stats(port->netdev,
3544 handle_queue_stats_dump_cb, cbdata);
3546 struct netdev_queue_stats stats;
3548 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3549 put_queue_stats(cbdata, queue_id, &stats);
3551 return OFPERR_OFPQOFC_BAD_QUEUE;
3558 handle_queue_stats_request(struct ofconn *ofconn,
3559 const struct ofp_header *rq)
3561 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3562 struct queue_stats_cbdata cbdata;
3563 struct ofport *port;
3565 struct ofputil_queue_stats_request oqsr;
3567 COVERAGE_INC(ofproto_queue_req);
3569 ofpmp_init(&cbdata.replies, rq);
3570 cbdata.now = time_msec();
3572 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3577 if (oqsr.port_no == OFPP_ANY) {
3578 error = OFPERR_OFPQOFC_BAD_QUEUE;
3579 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3580 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3585 port = ofproto_get_port(ofproto, oqsr.port_no);
3587 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3588 : OFPERR_OFPQOFC_BAD_PORT);
3591 ofconn_send_replies(ofconn, &cbdata.replies);
3593 ofpbuf_list_delete(&cbdata.replies);
3600 is_flow_deletion_pending(const struct ofproto *ofproto,
3601 const struct cls_rule *cls_rule,
3604 if (!hmap_is_empty(&ofproto->deletions)) {
3605 struct ofoperation *op;
3607 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3608 cls_rule_hash(cls_rule, table_id),
3609 &ofproto->deletions) {
3610 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3620 should_evict_a_rule(struct oftable *table, unsigned int extra_space)
3624 ovs_rwlock_rdlock(&table->cls.rwlock);
3625 count = classifier_count(&table->cls);
3626 ovs_rwlock_unlock(&table->cls.rwlock);
3628 return count + extra_space > table->max_flows;
3632 evict_rules_from_table(struct ofproto *ofproto, struct oftable *table,
3633 unsigned int extra_space)
3635 while (should_evict_a_rule(table, extra_space)) {
3638 if (!choose_rule_to_evict(table, &rule)) {
3639 return OFPERR_OFPFMFC_TABLE_FULL;
3640 } else if (rule->pending) {
3641 ovs_mutex_unlock(&rule->mutex);
3642 return OFPROTO_POSTPONE;
3644 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
3645 ofoperation_create(group, rule,
3646 OFOPERATION_DELETE, OFPRR_EVICTION);
3647 oftable_remove_rule(rule);
3648 ofproto->ofproto_class->rule_delete(rule);
3655 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3656 * in which no matching flow already exists in the flow table.
3658 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3659 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3660 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3661 * initiated now but may be retried later.
3663 * The caller retains ownership of 'fm->ofpacts'.
3665 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3668 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3669 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3671 struct oftable *table;
3672 struct ofopgroup *group;
3678 error = check_table_id(ofproto, fm->table_id);
3684 if (fm->table_id == 0xff) {
3685 if (ofproto->ofproto_class->rule_choose_table) {
3686 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3692 ovs_assert(table_id < ofproto->n_tables);
3696 } else if (fm->table_id < ofproto->n_tables) {
3697 table_id = fm->table_id;
3699 return OFPERR_OFPBRC_BAD_TABLE_ID;
3702 table = &ofproto->tables[table_id];
3704 if (table->flags & OFTABLE_READONLY) {
3705 return OFPERR_OFPBRC_EPERM;
3708 cls_rule_init(&cr, &fm->match, fm->priority);
3710 /* Transform "add" into "modify" if there's an existing identical flow. */
3711 ovs_rwlock_rdlock(&table->cls.rwlock);
3712 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
3713 ovs_rwlock_unlock(&table->cls.rwlock);
3715 cls_rule_destroy(&cr);
3716 if (!rule_is_modifiable(rule)) {
3717 return OFPERR_OFPBRC_EPERM;
3718 } else if (rule->pending) {
3719 return OFPROTO_POSTPONE;
3721 struct rule_collection rules;
3723 rule_collection_init(&rules);
3724 rule_collection_add(&rules, rule);
3725 fm->modify_cookie = true;
3726 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
3727 rule_collection_destroy(&rules);
3733 /* Verify actions. */
3734 error = ofproto_check_ofpacts(ofproto, fm->ofpacts, fm->ofpacts_len,
3735 &fm->match.flow, table_id);
3737 cls_rule_destroy(&cr);
3741 /* Serialize against pending deletion. */
3742 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
3743 cls_rule_destroy(&cr);
3744 return OFPROTO_POSTPONE;
3747 /* Check for overlap, if requested. */
3748 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
3751 ovs_rwlock_rdlock(&table->cls.rwlock);
3752 overlaps = classifier_rule_overlaps(&table->cls, &cr);
3753 ovs_rwlock_unlock(&table->cls.rwlock);
3756 cls_rule_destroy(&cr);
3757 return OFPERR_OFPFMFC_OVERLAP;
3761 /* If necessary, evict an existing rule to clear out space. */
3762 error = evict_rules_from_table(ofproto, table, 1);
3764 cls_rule_destroy(&cr);
3768 /* Allocate new rule. */
3769 rule = ofproto->ofproto_class->rule_alloc();
3771 cls_rule_destroy(&cr);
3772 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
3773 ofproto->name, ovs_strerror(error));
3777 /* Initialize base state. */
3778 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
3779 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
3780 atomic_init(&rule->ref_count, 1);
3781 rule->pending = NULL;
3782 rule->flow_cookie = fm->new_cookie;
3783 rule->created = rule->modified = rule->used = time_msec();
3785 ovs_mutex_init(&rule->mutex);
3786 ovs_mutex_lock(&rule->mutex);
3787 rule->idle_timeout = fm->idle_timeout;
3788 rule->hard_timeout = fm->hard_timeout;
3789 ovs_mutex_unlock(&rule->mutex);
3791 *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
3792 rule->flags = fm->flags & OFPUTIL_FF_STATE;
3793 rule->actions = rule_actions_create(fm->ofpacts, fm->ofpacts_len);
3794 list_init(&rule->meter_list_node);
3795 rule->eviction_group = NULL;
3796 list_init(&rule->expirable);
3797 rule->monitor_flags = 0;
3798 rule->add_seqno = 0;
3799 rule->modify_seqno = 0;
3801 /* Construct rule, initializing derived state. */
3802 error = ofproto->ofproto_class->rule_construct(rule);
3804 ofproto_rule_destroy__(rule);
3809 oftable_insert_rule(rule);
3811 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3812 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
3813 ofproto->ofproto_class->rule_insert(rule);
3814 ofopgroup_submit(group);
3819 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3821 /* Modifies the rules listed in 'rules', changing their actions to match those
3824 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3827 * Returns 0 on success, otherwise an OpenFlow error code. */
3829 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
3830 struct ofputil_flow_mod *fm, const struct ofp_header *request,
3831 const struct rule_collection *rules)
3833 enum ofoperation_type type;
3834 struct ofopgroup *group;
3838 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
3839 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3840 error = OFPERR_OFPBRC_EPERM;
3841 for (i = 0; i < rules->n; i++) {
3842 struct rule *rule = rules->rules[i];
3843 struct ofoperation *op;
3844 bool actions_changed;
3845 bool reset_counters;
3847 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
3849 if (rule_is_modifiable(rule)) {
3850 /* At least one rule is modifiable, don't report EPERM error. */
3856 /* Verify actions. */
3857 error = ofpacts_check(fm->ofpacts, fm->ofpacts_len, &fm->match.flow,
3858 u16_to_ofp(ofproto->max_ports), rule->table_id);
3863 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
3864 rule->actions->ofpacts,
3865 rule->actions->ofpacts_len);
3867 op = ofoperation_create(group, rule, type, 0);
3869 if (fm->modify_cookie && fm->new_cookie != htonll(UINT64_MAX)) {
3870 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
3872 if (type == OFOPERATION_REPLACE) {
3873 ovs_mutex_lock(&rule->mutex);
3874 rule->idle_timeout = fm->idle_timeout;
3875 rule->hard_timeout = fm->hard_timeout;
3876 ovs_mutex_unlock(&rule->mutex);
3878 rule->flags = fm->flags & OFPUTIL_FF_STATE;
3879 if (fm->idle_timeout || fm->hard_timeout) {
3880 if (!rule->eviction_group) {
3881 eviction_group_add_rule(rule);
3884 eviction_group_remove_rule(rule);
3888 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
3889 if (actions_changed || reset_counters) {
3890 struct rule_actions *new_actions;
3892 op->actions = rule->actions;
3893 new_actions = rule_actions_create(fm->ofpacts, fm->ofpacts_len);
3895 ovs_mutex_lock(&rule->mutex);
3896 rule->actions = new_actions;
3897 ovs_mutex_unlock(&rule->mutex);
3899 rule->ofproto->ofproto_class->rule_modify_actions(rule,
3902 ofoperation_complete(op, 0);
3905 ofopgroup_submit(group);
3911 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
3912 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3914 if (fm->cookie_mask != htonll(0) || fm->new_cookie == htonll(UINT64_MAX)) {
3917 return add_flow(ofproto, ofconn, fm, request);
3920 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
3923 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3926 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
3927 struct ofputil_flow_mod *fm,
3928 const struct ofp_header *request)
3930 struct rule_criteria criteria;
3931 struct rule_collection rules;
3934 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
3935 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
3936 error = collect_rules_loose(ofproto, &criteria, &rules);
3937 rule_criteria_destroy(&criteria);
3940 error = (rules.n > 0
3941 ? modify_flows__(ofproto, ofconn, fm, request, &rules)
3942 : modify_flows_add(ofproto, ofconn, fm, request));
3945 rule_collection_destroy(&rules);
3950 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3953 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3956 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3957 struct ofputil_flow_mod *fm,
3958 const struct ofp_header *request)
3960 struct rule_criteria criteria;
3961 struct rule_collection rules;
3964 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
3965 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
3966 error = collect_rules_strict(ofproto, &criteria, &rules);
3967 rule_criteria_destroy(&criteria);
3971 error = modify_flows_add(ofproto, ofconn, fm, request);
3972 } else if (rules.n == 1) {
3973 error = modify_flows__(ofproto, ofconn, fm, request, &rules);
3977 rule_collection_destroy(&rules);
3982 /* OFPFC_DELETE implementation. */
3985 delete_flow__(struct rule *rule, struct ofopgroup *group,
3986 enum ofp_flow_removed_reason reason)
3988 struct ofproto *ofproto = rule->ofproto;
3990 ofproto_rule_send_removed(rule, reason);
3992 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
3993 oftable_remove_rule(rule);
3994 ofproto->ofproto_class->rule_delete(rule);
3997 /* Deletes the rules listed in 'rules'.
3999 * Returns 0 on success, otherwise an OpenFlow error code. */
4001 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
4002 const struct ofp_header *request,
4003 const struct rule_collection *rules,
4004 enum ofp_flow_removed_reason reason)
4006 struct ofopgroup *group;
4009 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
4010 for (i = 0; i < rules->n; i++) {
4011 struct rule *rule = rules->rules[i];
4012 ovs_mutex_lock(&rule->mutex);
4013 delete_flow__(rule, group, reason);
4015 ofopgroup_submit(group);
4020 /* Implements OFPFC_DELETE. */
4022 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
4023 const struct ofputil_flow_mod *fm,
4024 const struct ofp_header *request)
4026 struct rule_criteria criteria;
4027 struct rule_collection rules;
4030 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4031 fm->cookie, fm->cookie_mask,
4032 fm->out_port, fm->out_group);
4033 error = collect_rules_loose(ofproto, &criteria, &rules);
4034 rule_criteria_destroy(&criteria);
4036 if (!error && rules.n > 0) {
4037 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4039 rule_collection_destroy(&rules);
4044 /* Implements OFPFC_DELETE_STRICT. */
4046 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
4047 const struct ofputil_flow_mod *fm,
4048 const struct ofp_header *request)
4050 struct rule_criteria criteria;
4051 struct rule_collection rules;
4054 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4055 fm->cookie, fm->cookie_mask,
4056 fm->out_port, fm->out_group);
4057 error = collect_rules_strict(ofproto, &criteria, &rules);
4058 rule_criteria_destroy(&criteria);
4060 if (!error && rules.n > 0) {
4061 error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE);
4063 rule_collection_destroy(&rules);
4069 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
4071 struct ofputil_flow_removed fr;
4073 if (ofproto_rule_is_hidden(rule) ||
4074 !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
4078 minimatch_expand(&rule->cr.match, &fr.match);
4079 fr.priority = rule->cr.priority;
4080 fr.cookie = rule->flow_cookie;
4082 fr.table_id = rule->table_id;
4083 calc_duration(rule->created, time_msec(),
4084 &fr.duration_sec, &fr.duration_nsec);
4085 ovs_mutex_lock(&rule->mutex);
4086 fr.idle_timeout = rule->idle_timeout;
4087 fr.hard_timeout = rule->hard_timeout;
4088 ovs_mutex_unlock(&rule->mutex);
4089 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
4092 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
4095 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4096 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4099 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
4102 * ofproto implementation ->run() functions should use this function to expire
4103 * OpenFlow flows. */
4105 ofproto_rule_expire(struct rule *rule, uint8_t reason)
4107 struct ofproto *ofproto = rule->ofproto;
4108 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
4110 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
4111 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
4112 ofproto_rule_send_removed(rule, reason);
4114 ovs_rwlock_wrlock(&cls->rwlock);
4115 ofproto_rule_delete(ofproto, cls, rule);
4116 ovs_rwlock_unlock(&cls->rwlock);
4119 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4120 * means "infinite". */
4122 reduce_timeout(uint16_t max, uint16_t *timeout)
4124 if (max && (!*timeout || *timeout > max)) {
4129 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4130 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4131 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4133 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4135 ofproto_rule_reduce_timeouts(struct rule *rule,
4136 uint16_t idle_timeout, uint16_t hard_timeout)
4137 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
4139 if (!idle_timeout && !hard_timeout) {
4143 ovs_mutex_lock(&ofproto_mutex);
4144 if (list_is_empty(&rule->expirable)) {
4145 list_insert(&rule->ofproto->expirable, &rule->expirable);
4147 ovs_mutex_unlock(&ofproto_mutex);
4149 ovs_mutex_lock(&rule->mutex);
4150 reduce_timeout(idle_timeout, &rule->idle_timeout);
4151 reduce_timeout(hard_timeout, &rule->hard_timeout);
4152 ovs_mutex_unlock(&rule->mutex);
4156 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4158 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4159 struct ofputil_flow_mod fm;
4160 uint64_t ofpacts_stub[1024 / 8];
4161 struct ofpbuf ofpacts;
4165 error = reject_slave_controller(ofconn);
4170 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4171 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
4174 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
4177 goto exit_free_ofpacts;
4180 /* Record the operation for logging a summary report. */
4181 switch (fm.command) {
4187 case OFPFC_MODIFY_STRICT:
4188 ofproto->n_modify++;
4192 case OFPFC_DELETE_STRICT:
4193 ofproto->n_delete++;
4198 if (ofproto->next_op_report == LLONG_MAX) {
4199 ofproto->first_op = now;
4200 ofproto->next_op_report = MAX(now + 10 * 1000,
4201 ofproto->op_backoff);
4202 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
4204 ofproto->last_op = now;
4207 ofpbuf_uninit(&ofpacts);
4213 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
4214 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
4218 if (ofproto->n_pending < 50) {
4219 switch (fm->command) {
4221 error = add_flow(ofproto, ofconn, fm, oh);
4225 error = modify_flows_loose(ofproto, ofconn, fm, oh);
4228 case OFPFC_MODIFY_STRICT:
4229 error = modify_flow_strict(ofproto, ofconn, fm, oh);
4233 error = delete_flows_loose(ofproto, ofconn, fm, oh);
4236 case OFPFC_DELETE_STRICT:
4237 error = delete_flow_strict(ofproto, ofconn, fm, oh);
4241 if (fm->command > 0xff) {
4242 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4243 "flow_mod_table_id extension is not enabled",
4246 error = OFPERR_OFPFMFC_BAD_COMMAND;
4250 ovs_assert(!list_is_empty(&ofproto->pending));
4251 error = OFPROTO_POSTPONE;
4254 run_rule_executes(ofproto);
4259 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4261 struct ofputil_role_request request;
4262 struct ofputil_role_request reply;
4266 error = ofputil_decode_role_message(oh, &request);
4271 if (request.role != OFPCR12_ROLE_NOCHANGE) {
4272 if (ofconn_get_role(ofconn) != request.role
4273 && ofconn_has_pending_opgroups(ofconn)) {
4274 return OFPROTO_POSTPONE;
4277 if (request.have_generation_id
4278 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4279 return OFPERR_OFPRRFC_STALE;
4282 ofconn_set_role(ofconn, request.role);
4285 reply.role = ofconn_get_role(ofconn);
4286 reply.have_generation_id = ofconn_get_master_election_id(
4287 ofconn, &reply.generation_id);
4288 buf = ofputil_encode_role_reply(oh, &reply);
4289 ofconn_send_reply(ofconn, buf);
4295 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4296 const struct ofp_header *oh)
4298 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4299 enum ofputil_protocol cur, next;
4301 cur = ofconn_get_protocol(ofconn);
4302 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4303 ofconn_set_protocol(ofconn, next);
4309 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4311 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4312 enum ofputil_protocol cur, next;
4313 enum ofputil_protocol next_base;
4315 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4317 return OFPERR_OFPBRC_EPERM;
4320 cur = ofconn_get_protocol(ofconn);
4321 next = ofputil_protocol_set_base(cur, next_base);
4322 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4323 /* Avoid sending async messages in surprising protocol. */
4324 return OFPROTO_POSTPONE;
4327 ofconn_set_protocol(ofconn, next);
4332 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4333 const struct ofp_header *oh)
4335 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4338 format = ntohl(msg->format);
4339 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4340 return OFPERR_OFPBRC_EPERM;
4343 if (format != ofconn_get_packet_in_format(ofconn)
4344 && ofconn_has_pending_opgroups(ofconn)) {
4345 /* Avoid sending async message in surprsing packet in format. */
4346 return OFPROTO_POSTPONE;
4349 ofconn_set_packet_in_format(ofconn, format);
4354 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4356 const struct nx_async_config *msg = ofpmsg_body(oh);
4357 uint32_t master[OAM_N_TYPES];
4358 uint32_t slave[OAM_N_TYPES];
4360 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4361 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4362 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4364 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4365 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4366 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4368 ofconn_set_async_config(ofconn, master, slave);
4369 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4370 !ofconn_get_miss_send_len(ofconn)) {
4371 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4378 handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4381 uint32_t master[OAM_N_TYPES];
4382 uint32_t slave[OAM_N_TYPES];
4383 struct nx_async_config *msg;
4385 ofconn_get_async_config(ofconn, master, slave);
4386 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4387 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4389 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4390 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4391 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4393 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4394 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4395 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4397 ofconn_send_reply(ofconn, buf);
4403 handle_nxt_set_controller_id(struct ofconn *ofconn,
4404 const struct ofp_header *oh)
4406 const struct nx_controller_id *nci = ofpmsg_body(oh);
4408 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4409 return OFPERR_NXBRC_MUST_BE_ZERO;
4412 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4417 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4421 if (ofconn_has_pending_opgroups(ofconn)) {
4422 return OFPROTO_POSTPONE;
4425 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4426 ? OFPRAW_OFPT10_BARRIER_REPLY
4427 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4428 ofconn_send_reply(ofconn, buf);
4433 ofproto_compose_flow_refresh_update(const struct rule *rule,
4434 enum nx_flow_monitor_flags flags,
4437 struct ofoperation *op = rule->pending;
4438 const struct rule_actions *actions;
4439 struct ofputil_flow_update fu;
4442 if (op && op->type == OFOPERATION_ADD) {
4443 /* We'll report the final flow when the operation completes. Reporting
4444 * it now would cause a duplicate report later. */
4448 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4449 ? NXFME_ADDED : NXFME_MODIFIED);
4451 ovs_mutex_lock(&rule->mutex);
4452 fu.idle_timeout = rule->idle_timeout;
4453 fu.hard_timeout = rule->hard_timeout;
4454 ovs_mutex_unlock(&rule->mutex);
4455 fu.table_id = rule->table_id;
4456 fu.cookie = rule->flow_cookie;
4457 minimatch_expand(&rule->cr.match, &match);
4459 fu.priority = rule->cr.priority;
4461 if (!(flags & NXFMF_ACTIONS)) {
4464 actions = rule->actions;
4466 /* An operation is in progress. Use the previous version of the flow's
4467 * actions, so that when the operation commits we report the change. */
4469 case OFOPERATION_ADD:
4472 case OFOPERATION_MODIFY:
4473 case OFOPERATION_REPLACE:
4474 actions = op->actions ? op->actions : rule->actions;
4477 case OFOPERATION_DELETE:
4478 actions = rule->actions;
4485 fu.ofpacts = actions ? actions->ofpacts : NULL;
4486 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
4488 if (list_is_empty(msgs)) {
4489 ofputil_start_flow_update(msgs);
4491 ofputil_append_flow_update(&fu, msgs);
4495 ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4500 for (i = 0; i < rules->n; i++) {
4501 struct rule *rule = rules->rules[i];
4502 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4503 rule->monitor_flags = 0;
4505 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4510 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4511 struct rule *rule, uint64_t seqno,
4512 struct rule_collection *rules)
4514 enum nx_flow_monitor_flags update;
4516 if (ofproto_rule_is_hidden(rule)) {
4521 ? ofoperation_has_out_port(rule->pending, m->out_port)
4522 : ofproto_rule_has_out_port(rule, m->out_port))) {
4527 if (rule->add_seqno > seqno) {
4528 update = NXFMF_ADD | NXFMF_MODIFY;
4529 } else if (rule->modify_seqno > seqno) {
4530 update = NXFMF_MODIFY;
4535 if (!(m->flags & update)) {
4539 update = NXFMF_INITIAL;
4542 if (!rule->monitor_flags) {
4543 rule_collection_add(rules, rule);
4545 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4549 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4551 struct rule_collection *rules)
4553 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4554 const struct ofoperation *op;
4555 const struct oftable *table;
4556 struct cls_rule target;
4558 cls_rule_init_from_minimatch(&target, &m->match, 0);
4559 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4560 struct cls_cursor cursor;
4563 ovs_rwlock_rdlock(&table->cls.rwlock);
4564 cls_cursor_init(&cursor, &table->cls, &target);
4565 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4566 ovs_assert(!rule->pending); /* XXX */
4567 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4569 ovs_rwlock_unlock(&table->cls.rwlock);
4572 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4573 struct rule *rule = op->rule;
4575 if (((m->table_id == 0xff
4576 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4577 : m->table_id == rule->table_id))
4578 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4579 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4582 cls_rule_destroy(&target);
4586 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4587 struct rule_collection *rules)
4589 if (m->flags & NXFMF_INITIAL) {
4590 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4595 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4596 uint64_t seqno, struct rule_collection *rules)
4598 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4602 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4604 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4605 struct ofmonitor **monitors;
4606 size_t n_monitors, allocated_monitors;
4607 struct rule_collection rules;
4608 struct list replies;
4614 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4616 n_monitors = allocated_monitors = 0;
4618 struct ofputil_flow_monitor_request request;
4619 struct ofmonitor *m;
4622 retval = ofputil_decode_flow_monitor_request(&request, &b);
4623 if (retval == EOF) {
4625 } else if (retval) {
4630 if (request.table_id != 0xff
4631 && request.table_id >= ofproto->n_tables) {
4632 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4636 error = ofmonitor_create(&request, ofconn, &m);
4641 if (n_monitors >= allocated_monitors) {
4642 monitors = x2nrealloc(monitors, &allocated_monitors,
4645 monitors[n_monitors++] = m;
4648 rule_collection_init(&rules);
4649 for (i = 0; i < n_monitors; i++) {
4650 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4653 ofpmp_init(&replies, oh);
4654 ofmonitor_compose_refresh_updates(&rules, &replies);
4655 rule_collection_destroy(&rules);
4657 ofconn_send_replies(ofconn, &replies);
4664 for (i = 0; i < n_monitors; i++) {
4665 ofmonitor_destroy(monitors[i]);
4672 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4674 struct ofmonitor *m;
4677 id = ofputil_decode_flow_monitor_cancel(oh);
4678 m = ofmonitor_lookup(ofconn, id);
4680 return OFPERR_NXBRC_FM_BAD_ID;
4683 ofmonitor_destroy(m);
4687 /* Meters implementation.
4689 * Meter table entry, indexed by the OpenFlow meter_id.
4690 * These are always dynamically allocated to allocate enough space for
4692 * 'created' is used to compute the duration for meter stats.
4693 * 'list rules' is needed so that we can delete the dependent rules when the
4694 * meter table entry is deleted.
4695 * 'provider_meter_id' is for the provider's private use.
4698 long long int created; /* Time created. */
4699 struct list rules; /* List of "struct rule_dpif"s. */
4700 ofproto_meter_id provider_meter_id;
4701 uint16_t flags; /* Meter flags. */
4702 uint16_t n_bands; /* Number of meter bands. */
4703 struct ofputil_meter_band *bands;
4707 * This is used in instruction validation at flow set-up time,
4708 * as flows may not use non-existing meters.
4709 * This is also used by ofproto-providers to translate OpenFlow meter_ids
4710 * in METER instructions to the corresponding provider meter IDs.
4711 * Return value of UINT32_MAX signifies an invalid meter.
4714 ofproto_get_provider_meter_id(const struct ofproto * ofproto,
4715 uint32_t of_meter_id)
4717 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
4718 const struct meter *meter = ofproto->meters[of_meter_id];
4720 return meter->provider_meter_id.uint32;
4727 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
4731 meter->flags = config->flags;
4732 meter->n_bands = config->n_bands;
4733 meter->bands = xmemdup(config->bands,
4734 config->n_bands * sizeof *meter->bands);
4737 static struct meter *
4738 meter_create(const struct ofputil_meter_config *config,
4739 ofproto_meter_id provider_meter_id)
4741 struct meter *meter;
4743 meter = xzalloc(sizeof *meter);
4744 meter->provider_meter_id = provider_meter_id;
4745 meter->created = time_msec();
4746 list_init(&meter->rules);
4748 meter_update(meter, config);
4754 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
4757 for (mid = first; mid <= last; ++mid) {
4758 struct meter *meter = ofproto->meters[mid];
4760 ofproto->meters[mid] = NULL;
4761 ofproto->ofproto_class->meter_del(ofproto,
4762 meter->provider_meter_id);
4770 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4772 ofproto_meter_id provider_meter_id = { UINT32_MAX };
4773 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
4777 return OFPERR_OFPMMFC_METER_EXISTS;
4780 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
4783 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
4784 *meterp = meter_create(&mm->meter, provider_meter_id);
4790 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4792 struct meter *meter = ofproto->meters[mm->meter.meter_id];
4796 return OFPERR_OFPMMFC_UNKNOWN_METER;
4799 error = ofproto->ofproto_class->meter_set(ofproto,
4800 &meter->provider_meter_id,
4802 ovs_assert(meter->provider_meter_id.uint32 != UINT32_MAX);
4804 meter_update(meter, &mm->meter);
4810 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
4811 struct ofputil_meter_mod *mm)
4813 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4814 uint32_t meter_id = mm->meter.meter_id;
4815 struct rule_collection rules;
4816 enum ofperr error = 0;
4817 uint32_t first, last;
4819 if (meter_id == OFPM13_ALL) {
4821 last = ofproto->meter_features.max_meters;
4823 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
4826 first = last = meter_id;
4829 /* First delete the rules that use this meter. If any of those rules are
4830 * currently being modified, postpone the whole operation until later. */
4831 rule_collection_init(&rules);
4832 for (meter_id = first; meter_id <= last; ++meter_id) {
4833 struct meter *meter = ofproto->meters[meter_id];
4834 if (meter && !list_is_empty(&meter->rules)) {
4837 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
4838 if (rule->pending) {
4839 error = OFPROTO_POSTPONE;
4842 rule_collection_add(&rules, rule);
4847 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
4850 /* Delete the meters. */
4851 meter_delete(ofproto, first, last);
4854 rule_collection_destroy(&rules);
4860 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4862 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4863 struct ofputil_meter_mod mm;
4864 uint64_t bands_stub[256 / 8];
4865 struct ofpbuf bands;
4869 error = reject_slave_controller(ofconn);
4874 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4876 error = ofputil_decode_meter_mod(oh, &mm, &bands);
4878 goto exit_free_bands;
4881 meter_id = mm.meter.meter_id;
4883 if (mm.command != OFPMC13_DELETE) {
4884 /* Fails also when meters are not implemented by the provider. */
4885 if (meter_id == 0 || meter_id > OFPM13_MAX) {
4886 error = OFPERR_OFPMMFC_INVALID_METER;
4887 goto exit_free_bands;
4888 } else if (meter_id > ofproto->meter_features.max_meters) {
4889 error = OFPERR_OFPMMFC_OUT_OF_METERS;
4890 goto exit_free_bands;
4892 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
4893 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
4894 goto exit_free_bands;
4898 switch (mm.command) {
4900 error = handle_add_meter(ofproto, &mm);
4903 case OFPMC13_MODIFY:
4904 error = handle_modify_meter(ofproto, &mm);
4907 case OFPMC13_DELETE:
4908 error = handle_delete_meter(ofconn, oh, &mm);
4912 error = OFPERR_OFPMMFC_BAD_COMMAND;
4917 ofpbuf_uninit(&bands);
4922 handle_meter_features_request(struct ofconn *ofconn,
4923 const struct ofp_header *request)
4925 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4926 struct ofputil_meter_features features;
4929 if (ofproto->ofproto_class->meter_get_features) {
4930 ofproto->ofproto_class->meter_get_features(ofproto, &features);
4932 memset(&features, 0, sizeof features);
4934 b = ofputil_encode_meter_features_reply(&features, request);
4936 ofconn_send_reply(ofconn, b);
4941 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
4944 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4945 struct list replies;
4946 uint64_t bands_stub[256 / 8];
4947 struct ofpbuf bands;
4948 uint32_t meter_id, first, last;
4950 ofputil_decode_meter_request(request, &meter_id);
4952 if (meter_id == OFPM13_ALL) {
4954 last = ofproto->meter_features.max_meters;
4956 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
4957 !ofproto->meters[meter_id]) {
4958 return OFPERR_OFPMMFC_UNKNOWN_METER;
4960 first = last = meter_id;
4963 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4964 ofpmp_init(&replies, request);
4966 for (meter_id = first; meter_id <= last; ++meter_id) {
4967 struct meter *meter = ofproto->meters[meter_id];
4969 continue; /* Skip non-existing meters. */
4971 if (type == OFPTYPE_METER_STATS_REQUEST) {
4972 struct ofputil_meter_stats stats;
4974 stats.meter_id = meter_id;
4976 /* Provider sets the packet and byte counts, we do the rest. */
4977 stats.flow_count = list_size(&meter->rules);
4978 calc_duration(meter->created, time_msec(),
4979 &stats.duration_sec, &stats.duration_nsec);
4980 stats.n_bands = meter->n_bands;
4981 ofpbuf_clear(&bands);
4983 = ofpbuf_put_uninit(&bands,
4984 meter->n_bands * sizeof *stats.bands);
4986 if (!ofproto->ofproto_class->meter_get(ofproto,
4987 meter->provider_meter_id,
4989 ofputil_append_meter_stats(&replies, &stats);
4991 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
4992 struct ofputil_meter_config config;
4994 config.meter_id = meter_id;
4995 config.flags = meter->flags;
4996 config.n_bands = meter->n_bands;
4997 config.bands = meter->bands;
4998 ofputil_append_meter_config(&replies, &config);
5002 ofconn_send_replies(ofconn, &replies);
5003 ofpbuf_uninit(&bands);
5008 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5009 struct ofgroup **group)
5010 OVS_TRY_RDLOCK(true, (*group)->rwlock)
5012 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5013 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5014 hash_int(group_id, 0), &ofproto->groups) {
5015 if ((*group)->group_id == group_id) {
5016 ovs_rwlock_rdlock(&(*group)->rwlock);
5017 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5021 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5026 ofproto_group_release(struct ofgroup *group)
5027 OVS_RELEASES(group->rwlock)
5029 ovs_rwlock_unlock(&group->rwlock);
5033 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
5034 struct ofgroup **group)
5035 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
5036 OVS_TRY_WRLOCK(true, (*group)->rwlock)
5038 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5039 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5040 hash_int(group_id, 0), &ofproto->groups) {
5041 if ((*group)->group_id == group_id) {
5042 ovs_rwlock_wrlock(&(*group)->rwlock);
5046 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5051 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
5052 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5054 struct ofgroup *grp;
5056 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
5057 hash_int(group_id, 0), &ofproto->groups) {
5058 if (grp->group_id == group_id) {
5066 append_group_stats(struct ofgroup *group, struct list *replies)
5067 OVS_REQ_RDLOCK(group->rwlock)
5069 struct ofputil_group_stats ogs;
5070 struct ofproto *ofproto = group->ofproto;
5071 long long int now = time_msec();
5074 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
5076 error = (ofproto->ofproto_class->group_get_stats
5077 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
5080 ogs.ref_count = UINT32_MAX;
5081 ogs.packet_count = UINT64_MAX;
5082 ogs.byte_count = UINT64_MAX;
5083 ogs.n_buckets = group->n_buckets;
5084 memset(ogs.bucket_stats, 0xff,
5085 ogs.n_buckets * sizeof *ogs.bucket_stats);
5088 ogs.group_id = group->group_id;
5089 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
5091 ofputil_append_group_stats(replies, &ogs);
5093 free(ogs.bucket_stats);
5097 handle_group_stats_request(struct ofconn *ofconn,
5098 const struct ofp_header *request)
5100 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5101 struct list replies;
5103 struct ofgroup *group;
5106 error = ofputil_decode_group_stats_request(request, &group_id);
5111 ofpmp_init(&replies, request);
5113 if (group_id == OFPG_ALL) {
5114 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5115 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5116 ovs_rwlock_rdlock(&group->rwlock);
5117 append_group_stats(group, &replies);
5118 ovs_rwlock_unlock(&group->rwlock);
5120 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5122 if (ofproto_group_lookup(ofproto, group_id, &group)) {
5123 append_group_stats(group, &replies);
5124 ofproto_group_release(group);
5128 ofconn_send_replies(ofconn, &replies);
5134 handle_group_desc_stats_request(struct ofconn *ofconn,
5135 const struct ofp_header *request)
5137 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5138 struct list replies;
5139 struct ofputil_group_desc gds;
5140 struct ofgroup *group;
5142 ofpmp_init(&replies, request);
5144 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5145 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
5146 gds.group_id = group->group_id;
5147 gds.type = group->type;
5148 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
5150 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5152 ofconn_send_replies(ofconn, &replies);
5158 handle_group_features_stats_request(struct ofconn *ofconn,
5159 const struct ofp_header *request)
5161 struct ofproto *p = ofconn_get_ofproto(ofconn);
5164 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5166 ofconn_send_reply(ofconn, msg);
5172 /* Implements OFPGC11_ADD
5173 * in which no matching flow already exists in the flow table.
5175 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
5176 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
5177 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
5178 * initiated now but may be retried later.
5180 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
5181 * ownership remains with the caller.
5183 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5186 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5188 struct ofgroup *ofgroup;
5191 if (gm->group_id > OFPG_MAX) {
5192 return OFPERR_OFPGMFC_INVALID_GROUP;
5194 if (gm->type > OFPGT11_FF) {
5195 return OFPERR_OFPGMFC_BAD_TYPE;
5198 /* Allocate new group and initialize it. */
5199 ofgroup = ofproto->ofproto_class->group_alloc();
5201 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
5202 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5205 ovs_rwlock_init(&ofgroup->rwlock);
5206 ofgroup->ofproto = ofproto;
5207 ofgroup->group_id = gm->group_id;
5208 ofgroup->type = gm->type;
5209 ofgroup->created = ofgroup->modified = time_msec();
5211 list_move(&ofgroup->buckets, &gm->buckets);
5212 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5214 /* Construct called BEFORE any locks are held. */
5215 error = ofproto->ofproto_class->group_construct(ofgroup);
5220 /* We wrlock as late as possible to minimize the time we jam any other
5221 * threads: No visible state changes before acquiring the lock. */
5222 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5224 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5225 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5229 if (ofproto_group_exists(ofproto, gm->group_id)) {
5230 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5235 /* Insert new group. */
5236 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5237 hash_int(ofgroup->group_id, 0));
5238 ofproto->n_groups[ofgroup->type]++;
5240 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5245 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5246 ofproto->ofproto_class->group_destruct(ofgroup);
5248 ofputil_bucket_list_destroy(&ofgroup->buckets);
5249 ofproto->ofproto_class->group_dealloc(ofgroup);
5254 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5257 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5260 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5262 struct ofgroup *ofgroup;
5263 struct ofgroup *victim;
5266 if (gm->group_id > OFPG_MAX) {
5267 return OFPERR_OFPGMFC_INVALID_GROUP;
5270 if (gm->type > OFPGT11_FF) {
5271 return OFPERR_OFPGMFC_BAD_TYPE;
5274 victim = ofproto->ofproto_class->group_alloc();
5276 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5277 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5280 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
5281 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
5284 /* Both group's and its container's write locks held now.
5285 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5286 if (ofgroup->type != gm->type
5287 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5288 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5293 list_move(&victim->buckets, &ofgroup->buckets);
5295 ofgroup->type = gm->type;
5296 list_move(&ofgroup->buckets, &gm->buckets);
5297 ofgroup->n_buckets = list_size(&ofgroup->buckets);
5299 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
5301 ofputil_bucket_list_destroy(&victim->buckets);
5302 ofproto->n_groups[victim->type]--;
5303 ofproto->n_groups[ofgroup->type]++;
5304 ofgroup->modified = time_msec();
5306 ofputil_bucket_list_destroy(&ofgroup->buckets);
5309 list_move(&ofgroup->buckets, &victim->buckets);
5313 ovs_rwlock_unlock(&ofgroup->rwlock);
5314 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5316 ofproto->ofproto_class->group_dealloc(victim);
5321 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5322 OVS_RELEASES(ofproto->groups_rwlock)
5324 /* Must wait until existing readers are done,
5325 * while holding the container's write lock at the same time. */
5326 ovs_rwlock_wrlock(&ofgroup->rwlock);
5327 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5328 /* No-one can find this group any more. */
5329 ofproto->n_groups[ofgroup->type]--;
5330 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5332 ofproto->ofproto_class->group_destruct(ofgroup);
5333 ofputil_bucket_list_destroy(&ofgroup->buckets);
5334 ovs_rwlock_unlock(&ofgroup->rwlock);
5335 ovs_rwlock_destroy(&ofgroup->rwlock);
5336 ofproto->ofproto_class->group_dealloc(ofgroup);
5339 /* Implements OFPGC_DELETE. */
5341 delete_group(struct ofproto *ofproto, uint32_t group_id)
5343 struct ofgroup *ofgroup;
5345 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5346 if (group_id == OFPG_ALL) {
5348 struct hmap_node *node = hmap_first(&ofproto->groups);
5352 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5353 delete_group__(ofproto, ofgroup);
5354 /* Lock for each node separately, so that we will not jam the
5355 * other threads for too long time. */
5356 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5359 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5360 hash_int(group_id, 0), &ofproto->groups) {
5361 if (ofgroup->group_id == group_id) {
5362 delete_group__(ofproto, ofgroup);
5367 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5371 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5373 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5374 struct ofputil_group_mod gm;
5377 error = reject_slave_controller(ofconn);
5382 error = ofputil_decode_group_mod(oh, &gm);
5387 switch (gm.command) {
5389 return add_group(ofproto, &gm);
5391 case OFPGC11_MODIFY:
5392 return modify_group(ofproto, &gm);
5394 case OFPGC11_DELETE:
5395 delete_group(ofproto, gm.group_id);
5399 if (gm.command > OFPGC11_DELETE) {
5400 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5401 ofproto->name, gm.command);
5403 return OFPERR_OFPGMFC_BAD_COMMAND;
5408 handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5410 struct ofputil_table_mod tm;
5413 error = reject_slave_controller(ofconn);
5418 error = ofputil_decode_table_mod(oh, &tm);
5423 /* XXX Actual table mod support is not implemented yet. */
5428 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5430 const struct ofp_header *oh = msg->data;
5434 error = ofptype_decode(&type, oh);
5440 /* OpenFlow requests. */
5441 case OFPTYPE_ECHO_REQUEST:
5442 return handle_echo_request(ofconn, oh);
5444 case OFPTYPE_FEATURES_REQUEST:
5445 return handle_features_request(ofconn, oh);
5447 case OFPTYPE_GET_CONFIG_REQUEST:
5448 return handle_get_config_request(ofconn, oh);
5450 case OFPTYPE_SET_CONFIG:
5451 return handle_set_config(ofconn, oh);
5453 case OFPTYPE_PACKET_OUT:
5454 return handle_packet_out(ofconn, oh);
5456 case OFPTYPE_PORT_MOD:
5457 return handle_port_mod(ofconn, oh);
5459 case OFPTYPE_FLOW_MOD:
5460 return handle_flow_mod(ofconn, oh);
5462 case OFPTYPE_GROUP_MOD:
5463 return handle_group_mod(ofconn, oh);
5465 case OFPTYPE_TABLE_MOD:
5466 return handle_table_mod(ofconn, oh);
5468 case OFPTYPE_METER_MOD:
5469 return handle_meter_mod(ofconn, oh);
5471 case OFPTYPE_BARRIER_REQUEST:
5472 return handle_barrier_request(ofconn, oh);
5474 case OFPTYPE_ROLE_REQUEST:
5475 return handle_role_request(ofconn, oh);
5477 /* OpenFlow replies. */
5478 case OFPTYPE_ECHO_REPLY:
5481 /* Nicira extension requests. */
5482 case OFPTYPE_FLOW_MOD_TABLE_ID:
5483 return handle_nxt_flow_mod_table_id(ofconn, oh);
5485 case OFPTYPE_SET_FLOW_FORMAT:
5486 return handle_nxt_set_flow_format(ofconn, oh);
5488 case OFPTYPE_SET_PACKET_IN_FORMAT:
5489 return handle_nxt_set_packet_in_format(ofconn, oh);
5491 case OFPTYPE_SET_CONTROLLER_ID:
5492 return handle_nxt_set_controller_id(ofconn, oh);
5494 case OFPTYPE_FLOW_AGE:
5495 /* Nothing to do. */
5498 case OFPTYPE_FLOW_MONITOR_CANCEL:
5499 return handle_flow_monitor_cancel(ofconn, oh);
5501 case OFPTYPE_SET_ASYNC_CONFIG:
5502 return handle_nxt_set_async_config(ofconn, oh);
5504 case OFPTYPE_GET_ASYNC_REQUEST:
5505 return handle_nxt_get_async_request(ofconn, oh);
5507 /* Statistics requests. */
5508 case OFPTYPE_DESC_STATS_REQUEST:
5509 return handle_desc_stats_request(ofconn, oh);
5511 case OFPTYPE_FLOW_STATS_REQUEST:
5512 return handle_flow_stats_request(ofconn, oh);
5514 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5515 return handle_aggregate_stats_request(ofconn, oh);
5517 case OFPTYPE_TABLE_STATS_REQUEST:
5518 return handle_table_stats_request(ofconn, oh);
5520 case OFPTYPE_PORT_STATS_REQUEST:
5521 return handle_port_stats_request(ofconn, oh);
5523 case OFPTYPE_QUEUE_STATS_REQUEST:
5524 return handle_queue_stats_request(ofconn, oh);
5526 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5527 return handle_port_desc_stats_request(ofconn, oh);
5529 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5530 return handle_flow_monitor_request(ofconn, oh);
5532 case OFPTYPE_METER_STATS_REQUEST:
5533 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5534 return handle_meter_request(ofconn, oh, type);
5536 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5537 return handle_meter_features_request(ofconn, oh);
5539 case OFPTYPE_GROUP_STATS_REQUEST:
5540 return handle_group_stats_request(ofconn, oh);
5542 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5543 return handle_group_desc_stats_request(ofconn, oh);
5545 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5546 return handle_group_features_stats_request(ofconn, oh);
5548 /* FIXME: Change the following once they are implemented: */
5549 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5550 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
5551 return OFPERR_OFPBRC_BAD_TYPE;
5555 case OFPTYPE_FEATURES_REPLY:
5556 case OFPTYPE_GET_CONFIG_REPLY:
5557 case OFPTYPE_PACKET_IN:
5558 case OFPTYPE_FLOW_REMOVED:
5559 case OFPTYPE_PORT_STATUS:
5560 case OFPTYPE_BARRIER_REPLY:
5561 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5562 case OFPTYPE_DESC_STATS_REPLY:
5563 case OFPTYPE_FLOW_STATS_REPLY:
5564 case OFPTYPE_QUEUE_STATS_REPLY:
5565 case OFPTYPE_PORT_STATS_REPLY:
5566 case OFPTYPE_TABLE_STATS_REPLY:
5567 case OFPTYPE_AGGREGATE_STATS_REPLY:
5568 case OFPTYPE_PORT_DESC_STATS_REPLY:
5569 case OFPTYPE_ROLE_REPLY:
5570 case OFPTYPE_FLOW_MONITOR_PAUSED:
5571 case OFPTYPE_FLOW_MONITOR_RESUMED:
5572 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
5573 case OFPTYPE_GET_ASYNC_REPLY:
5574 case OFPTYPE_GROUP_STATS_REPLY:
5575 case OFPTYPE_GROUP_DESC_STATS_REPLY:
5576 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
5577 case OFPTYPE_METER_STATS_REPLY:
5578 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5579 case OFPTYPE_METER_FEATURES_STATS_REPLY:
5580 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
5582 return OFPERR_OFPBRC_BAD_TYPE;
5587 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
5589 int error = handle_openflow__(ofconn, ofp_msg);
5590 if (error && error != OFPROTO_POSTPONE) {
5591 ofconn_send_error(ofconn, ofp_msg->data, error);
5593 COVERAGE_INC(ofproto_recv_openflow);
5594 return error != OFPROTO_POSTPONE;
5597 /* Asynchronous operations. */
5599 /* Creates and returns a new ofopgroup that is not associated with any
5600 * OpenFlow connection.
5602 * The caller should add operations to the returned group with
5603 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5604 static struct ofopgroup *
5605 ofopgroup_create_unattached(struct ofproto *ofproto)
5607 struct ofopgroup *group = xzalloc(sizeof *group);
5608 group->ofproto = ofproto;
5609 list_init(&group->ofproto_node);
5610 list_init(&group->ops);
5611 list_init(&group->ofconn_node);
5615 /* Creates and returns a new ofopgroup for 'ofproto'.
5617 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
5618 * connection. The 'request' and 'buffer_id' arguments are ignored.
5620 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
5621 * If the ofopgroup eventually fails, then the error reply will include
5622 * 'request'. If the ofopgroup eventually succeeds, then the packet with
5623 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
5625 * The caller should add operations to the returned group with
5626 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5627 static struct ofopgroup *
5628 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
5629 const struct ofp_header *request, uint32_t buffer_id)
5631 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
5633 size_t request_len = ntohs(request->length);
5635 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
5637 ofconn_add_opgroup(ofconn, &group->ofconn_node);
5638 group->ofconn = ofconn;
5639 group->request = xmemdup(request, MIN(request_len, 64));
5640 group->buffer_id = buffer_id;
5645 /* Submits 'group' for processing.
5647 * If 'group' contains no operations (e.g. none were ever added, or all of the
5648 * ones that were added completed synchronously), then it is destroyed
5649 * immediately. Otherwise it is added to the ofproto's list of pending
5652 ofopgroup_submit(struct ofopgroup *group)
5654 if (!group->n_running) {
5655 ofopgroup_complete(group);
5657 list_push_back(&group->ofproto->pending, &group->ofproto_node);
5658 group->ofproto->n_pending++;
5663 ofopgroup_complete(struct ofopgroup *group)
5665 struct ofproto *ofproto = group->ofproto;
5667 struct ofconn *abbrev_ofconn;
5668 ovs_be32 abbrev_xid;
5670 struct ofoperation *op, *next_op;
5673 ovs_assert(!group->n_running);
5676 LIST_FOR_EACH (op, group_node, &group->ops) {
5683 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
5684 LIST_FOR_EACH (op, group_node, &group->ops) {
5685 if (op->type != OFOPERATION_DELETE) {
5686 struct ofpbuf *packet;
5689 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
5692 struct rule_execute *re;
5696 ofproto_rule_ref(op->rule);
5698 re = xmalloc(sizeof *re);
5699 re->rule = op->rule;
5700 re->in_port = in_port;
5701 re->packet = packet;
5703 if (!guarded_list_push_back(&ofproto->rule_executes,
5704 &re->list_node, 1024)) {
5705 ofproto_rule_unref(op->rule);
5706 ofpbuf_delete(re->packet);
5715 if (!error && !list_is_empty(&group->ofconn_node)) {
5716 abbrev_ofconn = group->ofconn;
5717 abbrev_xid = group->request->xid;
5719 abbrev_ofconn = NULL;
5720 abbrev_xid = htonl(0);
5722 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
5723 struct rule *rule = op->rule;
5725 /* We generally want to report the change to active OpenFlow flow
5726 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
5728 - The operation failed.
5730 - The affected rule is not visible to controllers.
5732 - The operation's only effect was to update rule->modified. */
5734 || ofproto_rule_is_hidden(rule)
5735 || (op->type == OFOPERATION_MODIFY
5737 && rule->flow_cookie == op->flow_cookie))) {
5738 /* Check that we can just cast from ofoperation_type to
5739 * nx_flow_update_event. */
5740 enum nx_flow_update_event event_type;
5743 case OFOPERATION_ADD:
5744 case OFOPERATION_REPLACE:
5745 event_type = NXFME_ADDED;
5748 case OFOPERATION_DELETE:
5749 event_type = NXFME_DELETED;
5752 case OFOPERATION_MODIFY:
5753 event_type = NXFME_MODIFIED;
5760 ofmonitor_report(ofproto->connmgr, rule, event_type,
5761 op->reason, abbrev_ofconn, abbrev_xid);
5764 rule->pending = NULL;
5767 case OFOPERATION_ADD:
5771 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
5772 if (vid_mask == VLAN_VID_MASK) {
5773 if (ofproto->vlan_bitmap) {
5774 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
5775 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
5776 bitmap_set1(ofproto->vlan_bitmap, vid);
5777 ofproto->vlans_changed = true;
5780 ofproto->vlans_changed = true;
5784 ovs_mutex_lock(&rule->mutex);
5785 oftable_remove_rule(rule);
5786 ofproto_rule_unref(rule);
5790 case OFOPERATION_DELETE:
5791 ovs_assert(!op->error);
5792 ofproto_rule_unref(rule);
5796 case OFOPERATION_MODIFY:
5797 case OFOPERATION_REPLACE:
5799 long long int now = time_msec();
5801 rule->modified = now;
5802 if (op->type == OFOPERATION_REPLACE) {
5803 rule->created = rule->used = now;
5806 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
5807 ovs_mutex_lock(&rule->mutex);
5808 rule->idle_timeout = op->idle_timeout;
5809 rule->hard_timeout = op->hard_timeout;
5810 ovs_mutex_unlock(&rule->mutex);
5812 struct rule_actions *old_actions;
5814 ovs_mutex_lock(&rule->mutex);
5815 old_actions = rule->actions;
5816 rule->actions = op->actions;
5817 ovs_mutex_unlock(&rule->mutex);
5820 rule_actions_unref(old_actions);
5822 rule->flags = op->flags;
5830 ofoperation_destroy(op);
5833 ofmonitor_flush(ofproto->connmgr);
5835 if (!list_is_empty(&group->ofproto_node)) {
5836 ovs_assert(ofproto->n_pending > 0);
5837 ofproto->n_pending--;
5838 list_remove(&group->ofproto_node);
5840 if (!list_is_empty(&group->ofconn_node)) {
5841 list_remove(&group->ofconn_node);
5843 ofconn_send_error(group->ofconn, group->request, error);
5845 connmgr_retry(ofproto->connmgr);
5847 free(group->request);
5851 /* Initiates a new operation on 'rule', of the specified 'type', within
5852 * 'group'. Prior to calling, 'rule' must not have any pending operation.
5854 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
5855 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
5857 * Returns the newly created ofoperation (which is also available as
5858 * rule->pending). */
5859 static struct ofoperation *
5860 ofoperation_create(struct ofopgroup *group, struct rule *rule,
5861 enum ofoperation_type type,
5862 enum ofp_flow_removed_reason reason)
5864 struct ofproto *ofproto = group->ofproto;
5865 struct ofoperation *op;
5867 ovs_assert(!rule->pending);
5869 op = rule->pending = xzalloc(sizeof *op);
5871 list_push_back(&group->ops, &op->group_node);
5874 op->reason = reason;
5875 op->flow_cookie = rule->flow_cookie;
5876 ovs_mutex_lock(&rule->mutex);
5877 op->idle_timeout = rule->idle_timeout;
5878 op->hard_timeout = rule->hard_timeout;
5879 ovs_mutex_unlock(&rule->mutex);
5880 op->flags = rule->flags;
5884 if (type == OFOPERATION_DELETE) {
5885 hmap_insert(&ofproto->deletions, &op->hmap_node,
5886 cls_rule_hash(&rule->cr, rule->table_id));
5893 ofoperation_destroy(struct ofoperation *op)
5895 struct ofopgroup *group = op->group;
5898 op->rule->pending = NULL;
5900 if (op->type == OFOPERATION_DELETE) {
5901 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
5903 list_remove(&op->group_node);
5904 rule_actions_unref(op->actions);
5908 /* Indicates that 'op' completed with status 'error', which is either 0 to
5909 * indicate success or an OpenFlow error code on failure.
5911 * If 'error' is 0, indicating success, the operation will be committed
5912 * permanently to the flow table.
5914 * If 'error' is nonzero, then generally the operation will be rolled back:
5916 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
5917 * restores the original rule. The caller must have uninitialized any
5918 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
5919 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
5920 * and 7 for the new rule, calling its ->rule_dealloc() function.
5922 * - If 'op' is a "modify flow" operation, ofproto restores the original
5925 * - 'op' must not be a "delete flow" operation. Removing a rule is not
5926 * allowed to fail. It must always succeed.
5928 * Please see the large comment in ofproto/ofproto-provider.h titled
5929 * "Asynchronous Operation Support" for more information. */
5931 ofoperation_complete(struct ofoperation *op, enum ofperr error)
5933 struct ofopgroup *group = op->group;
5935 ovs_assert(op->rule->pending == op);
5936 ovs_assert(group->n_running > 0);
5937 ovs_assert(!error || op->type != OFOPERATION_DELETE);
5940 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
5941 ofopgroup_complete(group);
5946 pick_datapath_id(const struct ofproto *ofproto)
5948 const struct ofport *port;
5950 port = ofproto_get_port(ofproto, OFPP_LOCAL);
5952 uint8_t ea[ETH_ADDR_LEN];
5955 error = netdev_get_etheraddr(port->netdev, ea);
5957 return eth_addr_to_uint64(ea);
5959 VLOG_WARN("%s: could not get MAC address for %s (%s)",
5960 ofproto->name, netdev_get_name(port->netdev),
5961 ovs_strerror(error));
5963 return ofproto->fallback_dpid;
5967 pick_fallback_dpid(void)
5969 uint8_t ea[ETH_ADDR_LEN];
5970 eth_addr_nicira_random(ea);
5971 return eth_addr_to_uint64(ea);
5974 /* Table overflow policy. */
5976 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
5977 * to NULL if the table is not configured to evict rules or if the table
5978 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
5979 * or with no timeouts are not evictable.) */
5981 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
5983 struct eviction_group *evg;
5986 if (!table->eviction_fields) {
5990 /* In the common case, the outer and inner loops here will each be entered
5993 * - The inner loop normally "return"s in its first iteration. If the
5994 * eviction group has any evictable rules, then it always returns in
5997 * - The outer loop only iterates more than once if the largest eviction
5998 * group has no evictable rules.
6000 * - The outer loop can exit only if table's 'max_flows' is all filled up
6001 * by unevictable rules. */
6002 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
6005 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
6006 if (!ovs_mutex_trylock(&rule->mutex)) {
6016 /* Searches 'ofproto' for tables that have more flows than their configured
6017 * maximum and that have flow eviction enabled, and evicts as many flows as
6018 * necessary and currently feasible from them.
6020 * This triggers only when an OpenFlow table has N flows in it and then the
6021 * client configures a maximum number of flows less than N. */
6023 ofproto_evict(struct ofproto *ofproto)
6025 struct oftable *table;
6027 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
6028 evict_rules_from_table(ofproto, table, 0);
6032 /* Eviction groups. */
6034 /* Returns the priority to use for an eviction_group that contains 'n_rules'
6035 * rules. The priority contains low-order random bits to ensure that eviction
6036 * groups with the same number of rules are prioritized randomly. */
6038 eviction_group_priority(size_t n_rules)
6040 uint16_t size = MIN(UINT16_MAX, n_rules);
6041 return (size << 16) | random_uint16();
6044 /* Updates 'evg', an eviction_group within 'table', following a change that
6045 * adds or removes rules in 'evg'. */
6047 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
6049 heap_change(&table->eviction_groups_by_size, &evg->size_node,
6050 eviction_group_priority(heap_count(&evg->rules)));
6053 /* Destroys 'evg', an eviction_group within 'table':
6055 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
6056 * rules themselves, just removes them from the eviction group.)
6058 * - Removes 'evg' from 'table'.
6062 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
6064 while (!heap_is_empty(&evg->rules)) {
6067 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
6068 rule->eviction_group = NULL;
6070 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
6071 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
6072 heap_destroy(&evg->rules);
6076 /* Removes 'rule' from its eviction group, if any. */
6078 eviction_group_remove_rule(struct rule *rule)
6080 if (rule->eviction_group) {
6081 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6082 struct eviction_group *evg = rule->eviction_group;
6084 rule->eviction_group = NULL;
6085 heap_remove(&evg->rules, &rule->evg_node);
6086 if (heap_is_empty(&evg->rules)) {
6087 eviction_group_destroy(table, evg);
6089 eviction_group_resized(table, evg);
6094 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6095 * returns the hash value. */
6097 eviction_group_hash_rule(struct rule *rule)
6099 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6100 const struct mf_subfield *sf;
6104 hash = table->eviction_group_id_basis;
6105 miniflow_expand(&rule->cr.match.flow, &flow);
6106 for (sf = table->eviction_fields;
6107 sf < &table->eviction_fields[table->n_eviction_fields];
6110 if (mf_are_prereqs_ok(sf->field, &flow)) {
6111 union mf_value value;
6113 mf_get_value(sf->field, &flow, &value);
6115 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
6117 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
6118 unsigned int start = sf->ofs + sf->n_bits;
6119 bitwise_zero(&value, sf->field->n_bytes, start,
6120 sf->field->n_bytes * 8 - start);
6122 hash = hash_bytes(&value, sf->field->n_bytes, hash);
6124 hash = hash_int(hash, 0);
6131 /* Returns an eviction group within 'table' with the given 'id', creating one
6133 static struct eviction_group *
6134 eviction_group_find(struct oftable *table, uint32_t id)
6136 struct eviction_group *evg;
6138 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6142 evg = xmalloc(sizeof *evg);
6143 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6144 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6145 eviction_group_priority(0));
6146 heap_init(&evg->rules);
6151 /* Returns an eviction priority for 'rule'. The return value should be
6152 * interpreted so that higher priorities make a rule more attractive candidates
6155 rule_eviction_priority(struct rule *rule)
6157 long long int hard_expiration;
6158 long long int idle_expiration;
6159 long long int expiration;
6160 uint32_t expiration_offset;
6162 /* Calculate time of expiration. */
6163 ovs_mutex_lock(&rule->mutex);
6164 hard_expiration = (rule->hard_timeout
6165 ? rule->modified + rule->hard_timeout * 1000
6167 idle_expiration = (rule->idle_timeout
6168 ? rule->used + rule->idle_timeout * 1000
6170 expiration = MIN(hard_expiration, idle_expiration);
6171 ovs_mutex_unlock(&rule->mutex);
6172 if (expiration == LLONG_MAX) {
6176 /* Calculate the time of expiration as a number of (approximate) seconds
6177 * after program startup.
6179 * This should work OK for program runs that last UINT32_MAX seconds or
6180 * less. Therefore, please restart OVS at least once every 136 years. */
6181 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6183 /* Invert the expiration offset because we're using a max-heap. */
6184 return UINT32_MAX - expiration_offset;
6187 /* Adds 'rule' to an appropriate eviction group for its oftable's
6188 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6189 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6192 * The caller must ensure that 'rule' is not already in an eviction group. */
6194 eviction_group_add_rule(struct rule *rule)
6196 struct ofproto *ofproto = rule->ofproto;
6197 struct oftable *table = &ofproto->tables[rule->table_id];
6200 ovs_mutex_lock(&rule->mutex);
6201 has_timeout = rule->hard_timeout || rule->idle_timeout;
6202 ovs_mutex_unlock(&rule->mutex);
6204 if (table->eviction_fields && has_timeout) {
6205 struct eviction_group *evg;
6207 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6209 rule->eviction_group = evg;
6210 heap_insert(&evg->rules, &rule->evg_node,
6211 rule_eviction_priority(rule));
6212 eviction_group_resized(table, evg);
6218 /* Initializes 'table'. */
6220 oftable_init(struct oftable *table)
6222 memset(table, 0, sizeof *table);
6223 classifier_init(&table->cls);
6224 table->max_flows = UINT_MAX;
6227 /* Destroys 'table', including its classifier and eviction groups.
6229 * The caller is responsible for freeing 'table' itself. */
6231 oftable_destroy(struct oftable *table)
6233 ovs_rwlock_rdlock(&table->cls.rwlock);
6234 ovs_assert(classifier_is_empty(&table->cls));
6235 ovs_rwlock_unlock(&table->cls.rwlock);
6236 oftable_disable_eviction(table);
6237 classifier_destroy(&table->cls);
6241 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6242 * string, then 'table' will use its default name.
6244 * This only affects the name exposed for a table exposed through the OpenFlow
6245 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6247 oftable_set_name(struct oftable *table, const char *name)
6249 if (name && name[0]) {
6250 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6251 if (!table->name || strncmp(name, table->name, len)) {
6253 table->name = xmemdup0(name, len);
6261 /* oftables support a choice of two policies when adding a rule would cause the
6262 * number of flows in the table to exceed the configured maximum number: either
6263 * they can refuse to add the new flow or they can evict some existing flow.
6264 * This function configures the former policy on 'table'. */
6266 oftable_disable_eviction(struct oftable *table)
6268 if (table->eviction_fields) {
6269 struct eviction_group *evg, *next;
6271 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6272 &table->eviction_groups_by_id) {
6273 eviction_group_destroy(table, evg);
6275 hmap_destroy(&table->eviction_groups_by_id);
6276 heap_destroy(&table->eviction_groups_by_size);
6278 free(table->eviction_fields);
6279 table->eviction_fields = NULL;
6280 table->n_eviction_fields = 0;
6284 /* oftables support a choice of two policies when adding a rule would cause the
6285 * number of flows in the table to exceed the configured maximum number: either
6286 * they can refuse to add the new flow or they can evict some existing flow.
6287 * This function configures the latter policy on 'table', with fairness based
6288 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6289 * 'n_fields' as 0 disables fairness.) */
6291 oftable_enable_eviction(struct oftable *table,
6292 const struct mf_subfield *fields, size_t n_fields)
6294 struct cls_cursor cursor;
6297 if (table->eviction_fields
6298 && n_fields == table->n_eviction_fields
6300 || !memcmp(fields, table->eviction_fields,
6301 n_fields * sizeof *fields))) {
6306 oftable_disable_eviction(table);
6308 table->n_eviction_fields = n_fields;
6309 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6311 table->eviction_group_id_basis = random_uint32();
6312 hmap_init(&table->eviction_groups_by_id);
6313 heap_init(&table->eviction_groups_by_size);
6315 ovs_rwlock_rdlock(&table->cls.rwlock);
6316 cls_cursor_init(&cursor, &table->cls, NULL);
6317 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6318 eviction_group_add_rule(rule);
6320 ovs_rwlock_unlock(&table->cls.rwlock);
6323 /* Removes 'rule' from the oftable that contains it. */
6325 oftable_remove_rule__(struct ofproto *ofproto, struct classifier *cls,
6327 OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->mutex)
6329 classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
6331 ovs_mutex_lock(&ofproto_mutex);
6332 cookies_remove(ofproto, rule);
6333 ovs_mutex_unlock(&ofproto_mutex);
6335 eviction_group_remove_rule(rule);
6336 ovs_mutex_lock(&ofproto_mutex);
6337 if (!list_is_empty(&rule->expirable)) {
6338 list_remove(&rule->expirable);
6340 ovs_mutex_unlock(&ofproto_mutex);
6341 if (!list_is_empty(&rule->meter_list_node)) {
6342 list_remove(&rule->meter_list_node);
6343 list_init(&rule->meter_list_node);
6345 ovs_mutex_unlock(&rule->mutex);
6349 oftable_remove_rule(struct rule *rule)
6351 struct ofproto *ofproto = rule->ofproto;
6352 struct oftable *table = &ofproto->tables[rule->table_id];
6354 ovs_rwlock_wrlock(&table->cls.rwlock);
6355 oftable_remove_rule__(ofproto, &table->cls, rule);
6356 ovs_rwlock_unlock(&table->cls.rwlock);
6359 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6360 * the same cls_rule. */
6362 oftable_insert_rule(struct rule *rule)
6364 struct ofproto *ofproto = rule->ofproto;
6365 struct oftable *table = &ofproto->tables[rule->table_id];
6368 ovs_mutex_lock(&rule->mutex);
6369 may_expire = rule->hard_timeout || rule->idle_timeout;
6370 ovs_mutex_unlock(&rule->mutex);
6373 ovs_mutex_lock(&ofproto_mutex);
6374 list_insert(&ofproto->expirable, &rule->expirable);
6375 ovs_mutex_unlock(&ofproto_mutex);
6378 ovs_mutex_lock(&ofproto_mutex);
6379 cookies_insert(ofproto, rule);
6380 ovs_mutex_unlock(&ofproto_mutex);
6382 if (rule->actions->meter_id) {
6383 struct meter *meter = ofproto->meters[rule->actions->meter_id];
6384 list_insert(&meter->rules, &rule->meter_list_node);
6386 ovs_rwlock_wrlock(&table->cls.rwlock);
6387 classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
6388 ovs_rwlock_unlock(&table->cls.rwlock);
6389 eviction_group_add_rule(rule);
6392 /* unixctl commands. */
6395 ofproto_lookup(const char *name)
6397 struct ofproto *ofproto;
6399 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6401 if (!strcmp(ofproto->name, name)) {
6409 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6410 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6412 struct ofproto *ofproto;
6416 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6417 ds_put_format(&results, "%s\n", ofproto->name);
6419 unixctl_command_reply(conn, ds_cstr(&results));
6420 ds_destroy(&results);
6424 ofproto_unixctl_init(void)
6426 static bool registered;
6432 unixctl_command_register("ofproto/list", "", 0, 0,
6433 ofproto_unixctl_list, NULL);
6436 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6438 * This is deprecated. It is only for compatibility with broken device drivers
6439 * in old versions of Linux that do not properly support VLANs when VLAN
6440 * devices are not used. When broken device drivers are no longer in
6441 * widespread use, we will delete these interfaces. */
6443 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6444 * (exactly) by an OpenFlow rule in 'ofproto'. */
6446 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6448 const struct oftable *oftable;
6450 free(ofproto->vlan_bitmap);
6451 ofproto->vlan_bitmap = bitmap_allocate(4096);
6452 ofproto->vlans_changed = false;
6454 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6455 const struct cls_table *table;
6457 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.tables) {
6458 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6459 const struct cls_rule *rule;
6461 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6462 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6463 bitmap_set1(vlan_bitmap, vid);
6464 bitmap_set1(ofproto->vlan_bitmap, vid);
6471 /* Returns true if new VLANs have come into use by the flow table since the
6472 * last call to ofproto_get_vlan_usage().
6474 * We don't track when old VLANs stop being used. */
6476 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6478 return ofproto->vlans_changed;
6481 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6482 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6483 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6484 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6485 * then the VLAN device is un-enslaved. */
6487 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6488 ofp_port_t realdev_ofp_port, int vid)
6490 struct ofport *ofport;
6493 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6495 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6497 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6498 ofproto->name, vlandev_ofp_port);
6502 if (!ofproto->ofproto_class->set_realdev) {
6503 if (!vlandev_ofp_port) {
6506 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6510 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6512 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6513 ofproto->name, vlandev_ofp_port,
6514 netdev_get_name(ofport->netdev), ovs_strerror(error));