2 * Copyright (c) 2009, 2010, 2011, 2012 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.
25 #include "byte-order.h"
26 #include "classifier.h"
29 #include "dynamic-string.h"
32 #include "meta-flow.h"
35 #include "ofp-errors.h"
36 #include "ofp-print.h"
39 #include "ofproto-provider.h"
40 #include "openflow/nicira-ext.h"
41 #include "openflow/openflow.h"
45 #include "poll-loop.h"
51 #include "unaligned.h"
55 VLOG_DEFINE_THIS_MODULE(ofproto);
57 COVERAGE_DEFINE(ofproto_error);
58 COVERAGE_DEFINE(ofproto_flush);
59 COVERAGE_DEFINE(ofproto_no_packet_in);
60 COVERAGE_DEFINE(ofproto_packet_out);
61 COVERAGE_DEFINE(ofproto_queue_req);
62 COVERAGE_DEFINE(ofproto_recv_openflow);
63 COVERAGE_DEFINE(ofproto_reinit_ports);
64 COVERAGE_DEFINE(ofproto_uninstallable);
65 COVERAGE_DEFINE(ofproto_update_port);
68 S_OPENFLOW, /* Processing OpenFlow commands. */
69 S_EVICT, /* Evicting flows from over-limit tables. */
70 S_FLUSH, /* Deleting all flow table rules. */
73 enum ofoperation_type {
79 /* A single OpenFlow request can execute any number of operations. The
80 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
81 * ofconn to which an error reply should be sent if necessary.
83 * ofproto initiates some operations internally. These operations are still
84 * assigned to groups but will not have an associated ofconn. */
86 struct ofproto *ofproto; /* Owning ofproto. */
87 struct list ofproto_node; /* In ofproto's "pending" list. */
88 struct list ops; /* List of "struct ofoperation"s. */
90 /* Data needed to send OpenFlow reply on failure or to send a buffered
93 * If list_is_empty(ofconn_node) then this ofopgroup never had an
94 * associated ofconn or its ofconn's connection dropped after it initiated
95 * the operation. In the latter case 'ofconn' is a wild pointer that
96 * refers to freed memory, so the 'ofconn' member must be used only if
97 * !list_is_empty(ofconn_node).
99 struct list ofconn_node; /* In ofconn's list of pending opgroups. */
100 struct ofconn *ofconn; /* ofconn for reply (but see note above). */
101 struct ofp_header *request; /* Original request (truncated at 64 bytes). */
102 uint32_t buffer_id; /* Buffer id from original request. */
103 int error; /* 0 if no error yet, otherwise error code. */
106 static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *);
107 static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *,
108 const struct ofp_header *,
110 static void ofopgroup_submit(struct ofopgroup *);
111 static void ofopgroup_destroy(struct ofopgroup *);
113 /* A single flow table operation. */
115 struct ofopgroup *group; /* Owning group. */
116 struct list group_node; /* In ofopgroup's "ops" list. */
117 struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */
118 struct rule *rule; /* Rule being operated upon. */
119 enum ofoperation_type type; /* Type of operation. */
120 int status; /* -1 if pending, otherwise 0 or error code. */
121 struct rule *victim; /* OFOPERATION_ADDING: Replaced rule. */
122 union ofp_action *actions; /* OFOPERATION_MODIFYING: Replaced actions. */
123 int n_actions; /* OFOPERATION_MODIFYING: # of old actions. */
124 ovs_be64 flow_cookie; /* Rule's old flow cookie. */
127 static void ofoperation_create(struct ofopgroup *, struct rule *,
128 enum ofoperation_type);
129 static void ofoperation_destroy(struct ofoperation *);
132 static void oftable_init(struct oftable *);
133 static void oftable_destroy(struct oftable *);
135 static void oftable_set_name(struct oftable *, const char *name);
137 static void oftable_disable_eviction(struct oftable *);
138 static void oftable_enable_eviction(struct oftable *,
139 const struct mf_subfield *fields,
142 static void oftable_remove_rule(struct rule *);
143 static struct rule *oftable_replace_rule(struct rule *);
144 static void oftable_substitute_rule(struct rule *old, struct rule *new);
146 /* A set of rules within a single OpenFlow table (oftable) that have the same
147 * values for the oftable's eviction_fields. A rule to be evicted, when one is
148 * needed, is taken from the eviction group that contains the greatest number
151 * An oftable owns any number of eviction groups, each of which contains any
154 * Membership in an eviction group is imprecise, based on the hash of the
155 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
156 * That is, if two rules have different eviction_fields, but those
157 * eviction_fields hash to the same value, then they will belong to the same
158 * eviction_group anyway.
160 * (When eviction is not enabled on an oftable, we don't track any eviction
161 * groups, to save time and space.) */
162 struct eviction_group {
163 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
164 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
165 struct heap rules; /* Contains "struct rule"s. */
168 static struct rule *choose_rule_to_evict(struct oftable *);
169 static void ofproto_evict(struct ofproto *);
170 static uint32_t rule_eviction_priority(struct rule *);
173 static void ofport_destroy__(struct ofport *);
174 static void ofport_destroy(struct ofport *);
176 static void update_port(struct ofproto *, const char *devname);
177 static int init_ports(struct ofproto *);
178 static void reinit_ports(struct ofproto *);
181 static void ofproto_rule_destroy__(struct rule *);
182 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
183 static bool rule_is_modifiable(const struct rule *);
184 static bool rule_is_hidden(const struct rule *);
187 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
188 const struct ofputil_flow_mod *,
189 const struct ofp_header *);
190 static void delete_flow__(struct rule *, struct ofopgroup *);
191 static bool handle_openflow(struct ofconn *, struct ofpbuf *);
192 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
193 const struct ofputil_flow_mod *,
194 const struct ofp_header *);
197 static uint64_t pick_datapath_id(const struct ofproto *);
198 static uint64_t pick_fallback_dpid(void);
199 static void ofproto_destroy__(struct ofproto *);
200 static void update_mtu(struct ofproto *, struct ofport *);
203 static void ofproto_unixctl_init(void);
205 /* All registered ofproto classes, in probe order. */
206 static const struct ofproto_class **ofproto_classes;
207 static size_t n_ofproto_classes;
208 static size_t allocated_ofproto_classes;
210 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
211 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
213 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
216 ofproto_initialize(void)
222 ofproto_class_register(&ofproto_dpif_class);
226 /* 'type' should be a normalized datapath type, as returned by
227 * ofproto_normalize_type(). Returns the corresponding ofproto_class
228 * structure, or a null pointer if there is none registered for 'type'. */
229 static const struct ofproto_class *
230 ofproto_class_find__(const char *type)
234 ofproto_initialize();
235 for (i = 0; i < n_ofproto_classes; i++) {
236 const struct ofproto_class *class = ofproto_classes[i];
241 class->enumerate_types(&types);
242 found = sset_contains(&types, type);
243 sset_destroy(&types);
249 VLOG_WARN("unknown datapath type %s", type);
253 /* Registers a new ofproto class. After successful registration, new ofprotos
254 * of that type can be created using ofproto_create(). */
256 ofproto_class_register(const struct ofproto_class *new_class)
260 for (i = 0; i < n_ofproto_classes; i++) {
261 if (ofproto_classes[i] == new_class) {
266 if (n_ofproto_classes >= allocated_ofproto_classes) {
267 ofproto_classes = x2nrealloc(ofproto_classes,
268 &allocated_ofproto_classes,
269 sizeof *ofproto_classes);
271 ofproto_classes[n_ofproto_classes++] = new_class;
275 /* Unregisters a datapath provider. 'type' must have been previously
276 * registered and not currently be in use by any ofprotos. After
277 * unregistration new datapaths of that type cannot be opened using
278 * ofproto_create(). */
280 ofproto_class_unregister(const struct ofproto_class *class)
284 for (i = 0; i < n_ofproto_classes; i++) {
285 if (ofproto_classes[i] == class) {
286 for (i++; i < n_ofproto_classes; i++) {
287 ofproto_classes[i - 1] = ofproto_classes[i];
293 VLOG_WARN("attempted to unregister an ofproto class that is not "
298 /* Clears 'types' and enumerates all registered ofproto types into it. The
299 * caller must first initialize the sset. */
301 ofproto_enumerate_types(struct sset *types)
305 ofproto_initialize();
306 for (i = 0; i < n_ofproto_classes; i++) {
307 ofproto_classes[i]->enumerate_types(types);
311 /* Returns the fully spelled out name for the given ofproto 'type'.
313 * Normalized type string can be compared with strcmp(). Unnormalized type
314 * string might be the same even if they have different spellings. */
316 ofproto_normalize_type(const char *type)
318 return type && type[0] ? type : "system";
321 /* Clears 'names' and enumerates the names of all known created ofprotos with
322 * the given 'type'. The caller must first initialize the sset. Returns 0 if
323 * successful, otherwise a positive errno value.
325 * Some kinds of datapaths might not be practically enumerable. This is not
326 * considered an error. */
328 ofproto_enumerate_names(const char *type, struct sset *names)
330 const struct ofproto_class *class = ofproto_class_find__(type);
331 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
335 ofproto_create(const char *datapath_name, const char *datapath_type,
336 struct ofproto **ofprotop)
338 const struct ofproto_class *class;
339 struct ofproto *ofproto;
344 ofproto_initialize();
345 ofproto_unixctl_init();
347 datapath_type = ofproto_normalize_type(datapath_type);
348 class = ofproto_class_find__(datapath_type);
350 VLOG_WARN("could not create datapath %s of unknown type %s",
351 datapath_name, datapath_type);
355 ofproto = class->alloc();
357 VLOG_ERR("failed to allocate datapath %s of type %s",
358 datapath_name, datapath_type);
363 memset(ofproto, 0, sizeof *ofproto);
364 ofproto->ofproto_class = class;
365 ofproto->name = xstrdup(datapath_name);
366 ofproto->type = xstrdup(datapath_type);
367 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
368 hash_string(ofproto->name, 0));
369 ofproto->datapath_id = 0;
370 ofproto_set_flow_eviction_threshold(ofproto,
371 OFPROTO_FLOW_EVICTON_THRESHOLD_DEFAULT);
372 ofproto->forward_bpdu = false;
373 ofproto->fallback_dpid = pick_fallback_dpid();
374 ofproto->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
375 ofproto->hw_desc = xstrdup(DEFAULT_HW_DESC);
376 ofproto->sw_desc = xstrdup(DEFAULT_SW_DESC);
377 ofproto->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
378 ofproto->dp_desc = xstrdup(DEFAULT_DP_DESC);
379 ofproto->frag_handling = OFPC_FRAG_NORMAL;
380 hmap_init(&ofproto->ports);
381 shash_init(&ofproto->port_by_name);
382 ofproto->tables = NULL;
383 ofproto->n_tables = 0;
384 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
385 ofproto->state = S_OPENFLOW;
386 list_init(&ofproto->pending);
387 ofproto->n_pending = 0;
388 hmap_init(&ofproto->deletions);
389 ofproto->vlan_bitmap = NULL;
390 ofproto->vlans_changed = false;
391 ofproto->min_mtu = INT_MAX;
393 error = ofproto->ofproto_class->construct(ofproto);
395 VLOG_ERR("failed to open datapath %s: %s",
396 datapath_name, strerror(error));
397 ofproto_destroy__(ofproto);
401 assert(ofproto->n_tables);
403 ofproto->datapath_id = pick_datapath_id(ofproto);
404 VLOG_INFO("using datapath ID %016"PRIx64, ofproto->datapath_id);
412 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
414 struct oftable *table;
416 assert(!ofproto->n_tables);
417 assert(n_tables >= 1 && n_tables <= 255);
419 ofproto->n_tables = n_tables;
420 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
421 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
427 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
429 uint64_t old_dpid = p->datapath_id;
430 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
431 if (p->datapath_id != old_dpid) {
432 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
434 /* Force all active connections to reconnect, since there is no way to
435 * notify a controller that the datapath ID has changed. */
436 ofproto_reconnect_controllers(p);
441 ofproto_set_controllers(struct ofproto *p,
442 const struct ofproto_controller *controllers,
443 size_t n_controllers)
445 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
449 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
451 connmgr_set_fail_mode(p->connmgr, fail_mode);
454 /* Drops the connections between 'ofproto' and all of its controllers, forcing
455 * them to reconnect. */
457 ofproto_reconnect_controllers(struct ofproto *ofproto)
459 connmgr_reconnect(ofproto->connmgr);
462 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
463 * in-band control should guarantee access, in the same way that in-band
464 * control guarantees access to OpenFlow controllers. */
466 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
467 const struct sockaddr_in *extras, size_t n)
469 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
472 /* Sets the OpenFlow queue used by flows set up by in-band control on
473 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
474 * flows will use the default queue. */
476 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
478 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
481 /* Sets the number of flows at which eviction from the kernel flow table
484 ofproto_set_flow_eviction_threshold(struct ofproto *ofproto, unsigned threshold)
486 if (threshold < OFPROTO_FLOW_EVICTION_THRESHOLD_MIN) {
487 ofproto->flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_MIN;
489 ofproto->flow_eviction_threshold = threshold;
493 /* If forward_bpdu is true, the NORMAL action will forward frames with
494 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
495 * the NORMAL action will drop these frames. */
497 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
499 bool old_val = ofproto->forward_bpdu;
500 ofproto->forward_bpdu = forward_bpdu;
501 if (old_val != ofproto->forward_bpdu) {
502 if (ofproto->ofproto_class->forward_bpdu_changed) {
503 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
508 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
509 * 'idle_time', in seconds. */
511 ofproto_set_mac_idle_time(struct ofproto *ofproto, unsigned idle_time)
513 if (ofproto->ofproto_class->set_mac_idle_time) {
514 ofproto->ofproto_class->set_mac_idle_time(ofproto, idle_time);
519 ofproto_set_desc(struct ofproto *p,
520 const char *mfr_desc, const char *hw_desc,
521 const char *sw_desc, const char *serial_desc,
524 struct ofp_desc_stats *ods;
527 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
528 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
529 sizeof ods->mfr_desc);
532 p->mfr_desc = xstrdup(mfr_desc);
535 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
536 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
537 sizeof ods->hw_desc);
540 p->hw_desc = xstrdup(hw_desc);
543 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
544 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
545 sizeof ods->sw_desc);
548 p->sw_desc = xstrdup(sw_desc);
551 if (strlen(serial_desc) >= sizeof ods->serial_num) {
552 VLOG_WARN("truncating serial_desc, must be less than %zu "
554 sizeof ods->serial_num);
556 free(p->serial_desc);
557 p->serial_desc = xstrdup(serial_desc);
560 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
561 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
562 sizeof ods->dp_desc);
565 p->dp_desc = xstrdup(dp_desc);
570 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
572 return connmgr_set_snoops(ofproto->connmgr, snoops);
576 ofproto_set_netflow(struct ofproto *ofproto,
577 const struct netflow_options *nf_options)
579 if (nf_options && sset_is_empty(&nf_options->collectors)) {
583 if (ofproto->ofproto_class->set_netflow) {
584 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
586 return nf_options ? EOPNOTSUPP : 0;
591 ofproto_set_sflow(struct ofproto *ofproto,
592 const struct ofproto_sflow_options *oso)
594 if (oso && sset_is_empty(&oso->targets)) {
598 if (ofproto->ofproto_class->set_sflow) {
599 return ofproto->ofproto_class->set_sflow(ofproto, oso);
601 return oso ? EOPNOTSUPP : 0;
605 /* Spanning Tree Protocol (STP) configuration. */
607 /* Configures STP on 'ofproto' using the settings defined in 's'. If
608 * 's' is NULL, disables STP.
610 * Returns 0 if successful, otherwise a positive errno value. */
612 ofproto_set_stp(struct ofproto *ofproto,
613 const struct ofproto_stp_settings *s)
615 return (ofproto->ofproto_class->set_stp
616 ? ofproto->ofproto_class->set_stp(ofproto, s)
620 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
621 * 'enabled' member of 's' is false, then the other members are not
624 * Returns 0 if successful, otherwise a positive errno value. */
626 ofproto_get_stp_status(struct ofproto *ofproto,
627 struct ofproto_stp_status *s)
629 return (ofproto->ofproto_class->get_stp_status
630 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
634 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
635 * in 's'. The caller is responsible for assigning STP port numbers
636 * (using the 'port_num' member in the range of 1 through 255, inclusive)
637 * and ensuring there are no duplicates. If the 's' is NULL, then STP
638 * is disabled on the port.
640 * Returns 0 if successful, otherwise a positive errno value.*/
642 ofproto_port_set_stp(struct ofproto *ofproto, uint16_t ofp_port,
643 const struct ofproto_port_stp_settings *s)
645 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
647 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
648 ofproto->name, ofp_port);
652 return (ofproto->ofproto_class->set_stp_port
653 ? ofproto->ofproto_class->set_stp_port(ofport, s)
657 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
658 * 's'. If the 'enabled' member in 's' is false, then the other members
659 * are not meaningful.
661 * Returns 0 if successful, otherwise a positive errno value.*/
663 ofproto_port_get_stp_status(struct ofproto *ofproto, uint16_t ofp_port,
664 struct ofproto_port_stp_status *s)
666 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
668 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
669 "port %"PRIu16, ofproto->name, ofp_port);
673 return (ofproto->ofproto_class->get_stp_port_status
674 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
678 /* Queue DSCP configuration. */
680 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
681 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
682 * to implement QoS. Instead, it is used to implement features which require
683 * knowledge of what queues exist on a port, and some basic information about
686 * Returns 0 if successful, otherwise a positive errno value. */
688 ofproto_port_set_queues(struct ofproto *ofproto, uint16_t ofp_port,
689 const struct ofproto_port_queue *queues,
692 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
695 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
696 ofproto->name, ofp_port);
700 return (ofproto->ofproto_class->set_queues
701 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
705 /* Connectivity Fault Management configuration. */
707 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
709 ofproto_port_clear_cfm(struct ofproto *ofproto, uint16_t ofp_port)
711 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
712 if (ofport && ofproto->ofproto_class->set_cfm) {
713 ofproto->ofproto_class->set_cfm(ofport, NULL);
717 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
718 * basic configuration from the configuration members in 'cfm', and the remote
719 * maintenance point ID from remote_mpid. Ignores the statistics members of
722 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
724 ofproto_port_set_cfm(struct ofproto *ofproto, uint16_t ofp_port,
725 const struct cfm_settings *s)
727 struct ofport *ofport;
730 ofport = ofproto_get_port(ofproto, ofp_port);
732 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
733 ofproto->name, ofp_port);
737 /* XXX: For configuration simplicity, we only support one remote_mpid
738 * outside of the CFM module. It's not clear if this is the correct long
739 * term solution or not. */
740 error = (ofproto->ofproto_class->set_cfm
741 ? ofproto->ofproto_class->set_cfm(ofport, s)
744 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
745 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
750 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
751 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
752 * 0 if LACP partner information is not current (generally indicating a
753 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
755 ofproto_port_is_lacp_current(struct ofproto *ofproto, uint16_t ofp_port)
757 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
758 return (ofport && ofproto->ofproto_class->port_is_lacp_current
759 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
765 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
766 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
767 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
768 * configuration plus, if there is more than one slave, a bonding
771 * If 'aux' is already registered then this function updates its configuration
772 * to 's'. Otherwise, this function registers a new bundle.
774 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
777 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
778 const struct ofproto_bundle_settings *s)
780 return (ofproto->ofproto_class->bundle_set
781 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
785 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
786 * If no such bundle has been registered, this has no effect. */
788 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
790 return ofproto_bundle_register(ofproto, aux, NULL);
794 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
795 * If 'aux' is already registered then this function updates its configuration
796 * to 's'. Otherwise, this function registers a new mirror. */
798 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
799 const struct ofproto_mirror_settings *s)
801 return (ofproto->ofproto_class->mirror_set
802 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
806 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
807 * If no mirror has been registered, this has no effect. */
809 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
811 return ofproto_mirror_register(ofproto, aux, NULL);
814 /* Retrieves statistics from mirror associated with client data pointer
815 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
816 * 'bytes', respectively. If a particular counters is not supported,
817 * the appropriate argument is set to UINT64_MAX. */
819 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
820 uint64_t *packets, uint64_t *bytes)
822 if (!ofproto->ofproto_class->mirror_get_stats) {
823 *packets = *bytes = UINT64_MAX;
827 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
831 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
832 * which all packets are flooded, instead of using MAC learning. If
833 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
835 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
838 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
840 return (ofproto->ofproto_class->set_flood_vlans
841 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
845 /* Returns true if 'aux' is a registered bundle that is currently in use as the
846 * output for a mirror. */
848 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
850 return (ofproto->ofproto_class->is_mirror_output_bundle
851 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
855 /* Configuration of OpenFlow tables. */
857 /* Returns the number of OpenFlow tables in 'ofproto'. */
859 ofproto_get_n_tables(const struct ofproto *ofproto)
861 return ofproto->n_tables;
864 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
865 * settings from 's'. 'table_id' must be in the range 0 through the number of
866 * OpenFlow tables in 'ofproto' minus 1, inclusive.
868 * For read-only tables, only the name may be configured. */
870 ofproto_configure_table(struct ofproto *ofproto, int table_id,
871 const struct ofproto_table_settings *s)
873 struct oftable *table;
875 assert(table_id >= 0 && table_id < ofproto->n_tables);
876 table = &ofproto->tables[table_id];
878 oftable_set_name(table, s->name);
880 if (table->flags & OFTABLE_READONLY) {
885 oftable_enable_eviction(table, s->groups, s->n_groups);
887 oftable_disable_eviction(table);
890 table->max_flows = s->max_flows;
891 if (classifier_count(&table->cls) > table->max_flows
892 && table->eviction_fields) {
893 /* 'table' contains more flows than allowed. We might not be able to
894 * evict them right away because of the asynchronous nature of flow
895 * table changes. Schedule eviction for later. */
896 switch (ofproto->state) {
898 ofproto->state = S_EVICT;
902 /* We're already deleting flows, nothing more to do. */
909 ofproto_has_snoops(const struct ofproto *ofproto)
911 return connmgr_has_snoops(ofproto->connmgr);
915 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
917 connmgr_get_snoops(ofproto->connmgr, snoops);
921 ofproto_flush__(struct ofproto *ofproto)
923 struct ofopgroup *group;
924 struct oftable *table;
926 if (ofproto->ofproto_class->flush) {
927 ofproto->ofproto_class->flush(ofproto);
930 group = ofopgroup_create_unattached(ofproto);
931 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
932 struct rule *rule, *next_rule;
933 struct cls_cursor cursor;
935 if (table->flags & OFTABLE_HIDDEN) {
939 cls_cursor_init(&cursor, &table->cls, NULL);
940 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
941 if (!rule->pending) {
942 ofoperation_create(group, rule, OFOPERATION_DELETE);
943 oftable_remove_rule(rule);
944 ofproto->ofproto_class->rule_destruct(rule);
948 ofopgroup_submit(group);
952 ofproto_destroy__(struct ofproto *ofproto)
954 struct oftable *table;
956 assert(list_is_empty(&ofproto->pending));
957 assert(!ofproto->n_pending);
959 connmgr_destroy(ofproto->connmgr);
961 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
964 free(ofproto->mfr_desc);
965 free(ofproto->hw_desc);
966 free(ofproto->sw_desc);
967 free(ofproto->serial_desc);
968 free(ofproto->dp_desc);
969 hmap_destroy(&ofproto->ports);
970 shash_destroy(&ofproto->port_by_name);
972 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
973 oftable_destroy(table);
975 free(ofproto->tables);
977 hmap_destroy(&ofproto->deletions);
979 free(ofproto->vlan_bitmap);
981 ofproto->ofproto_class->dealloc(ofproto);
985 ofproto_destroy(struct ofproto *p)
987 struct ofport *ofport, *next_ofport;
994 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
995 ofport_destroy(ofport);
998 p->ofproto_class->destruct(p);
999 ofproto_destroy__(p);
1002 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1003 * kernel datapath, for example, this destroys the datapath in the kernel, and
1004 * with the netdev-based datapath, it tears down the data structures that
1005 * represent the datapath.
1007 * The datapath should not be currently open as an ofproto. */
1009 ofproto_delete(const char *name, const char *type)
1011 const struct ofproto_class *class = ofproto_class_find__(type);
1012 return (!class ? EAFNOSUPPORT
1013 : !class->del ? EACCES
1014 : class->del(type, name));
1018 process_port_change(struct ofproto *ofproto, int error, char *devname)
1020 if (error == ENOBUFS) {
1021 reinit_ports(ofproto);
1022 } else if (!error) {
1023 update_port(ofproto, devname);
1029 ofproto_run(struct ofproto *p)
1031 struct sset changed_netdevs;
1032 const char *changed_netdev;
1033 struct ofport *ofport;
1036 error = p->ofproto_class->run(p);
1037 if (error && error != EAGAIN) {
1038 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, strerror(error));
1041 if (p->ofproto_class->port_poll) {
1044 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1045 process_port_change(p, error, devname);
1049 /* Update OpenFlow port status for any port whose netdev has changed.
1051 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1052 * destroyed, so it's not safe to update ports directly from the
1053 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1054 * need this two-phase approach. */
1055 sset_init(&changed_netdevs);
1056 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1057 unsigned int change_seq = netdev_change_seq(ofport->netdev);
1058 if (ofport->change_seq != change_seq) {
1059 ofport->change_seq = change_seq;
1060 sset_add(&changed_netdevs, netdev_get_name(ofport->netdev));
1063 SSET_FOR_EACH (changed_netdev, &changed_netdevs) {
1064 update_port(p, changed_netdev);
1066 sset_destroy(&changed_netdevs);
1070 connmgr_run(p->connmgr, handle_openflow);
1074 connmgr_run(p->connmgr, NULL);
1076 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1077 p->state = S_OPENFLOW;
1082 connmgr_run(p->connmgr, NULL);
1084 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1085 connmgr_flushed(p->connmgr);
1086 p->state = S_OPENFLOW;
1097 /* Performs periodic activity required by 'ofproto' that needs to be done
1098 * with the least possible latency.
1100 * It makes sense to call this function a couple of times per poll loop, to
1101 * provide a significant performance boost on some benchmarks with the
1102 * ofproto-dpif implementation. */
1104 ofproto_run_fast(struct ofproto *p)
1108 error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0;
1109 if (error && error != EAGAIN) {
1110 VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)",
1111 p->name, strerror(error));
1117 ofproto_wait(struct ofproto *p)
1119 struct ofport *ofport;
1121 p->ofproto_class->wait(p);
1122 if (p->ofproto_class->port_poll_wait) {
1123 p->ofproto_class->port_poll_wait(p);
1126 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1127 if (ofport->change_seq != netdev_change_seq(ofport->netdev)) {
1128 poll_immediate_wake();
1134 connmgr_wait(p->connmgr, true);
1139 connmgr_wait(p->connmgr, false);
1140 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1141 poll_immediate_wake();
1148 ofproto_is_alive(const struct ofproto *p)
1150 return connmgr_has_controllers(p->connmgr);
1153 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1154 * memory_report(). */
1156 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1158 const struct oftable *table;
1159 unsigned int n_rules;
1161 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1162 simap_increase(usage, "ops",
1163 ofproto->n_pending + hmap_count(&ofproto->deletions));
1166 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1167 n_rules += classifier_count(&table->cls);
1169 simap_increase(usage, "rules", n_rules);
1171 if (ofproto->ofproto_class->get_memory_usage) {
1172 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1175 connmgr_get_memory_usage(ofproto->connmgr, usage);
1179 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1182 connmgr_get_controller_info(ofproto->connmgr, info);
1186 ofproto_free_ofproto_controller_info(struct shash *info)
1188 connmgr_free_controller_info(info);
1191 /* Makes a deep copy of 'old' into 'port'. */
1193 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1195 port->name = xstrdup(old->name);
1196 port->type = xstrdup(old->type);
1197 port->ofp_port = old->ofp_port;
1200 /* Frees memory allocated to members of 'ofproto_port'.
1202 * Do not call this function on an ofproto_port obtained from
1203 * ofproto_port_dump_next(): that function retains ownership of the data in the
1206 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1208 free(ofproto_port->name);
1209 free(ofproto_port->type);
1212 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1214 * This function provides no status indication. An error status for the entire
1215 * dump operation is provided when it is completed by calling
1216 * ofproto_port_dump_done().
1219 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1220 const struct ofproto *ofproto)
1222 dump->ofproto = ofproto;
1223 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1227 /* Attempts to retrieve another port from 'dump', which must have been created
1228 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1229 * 'port' and returns true. On failure, returns false.
1231 * Failure might indicate an actual error or merely that the last port has been
1232 * dumped. An error status for the entire dump operation is provided when it
1233 * is completed by calling ofproto_port_dump_done().
1235 * The ofproto owns the data stored in 'port'. It will remain valid until at
1236 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1237 * ofproto_port_dump_done(). */
1239 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1240 struct ofproto_port *port)
1242 const struct ofproto *ofproto = dump->ofproto;
1248 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1251 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1257 /* Completes port table dump operation 'dump', which must have been created
1258 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1259 * error-free, otherwise a positive errno value describing the problem. */
1261 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1263 const struct ofproto *ofproto = dump->ofproto;
1265 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1268 return dump->error == EOF ? 0 : dump->error;
1271 /* Attempts to add 'netdev' as a port on 'ofproto'. If successful, returns 0
1272 * and sets '*ofp_portp' to the new port's OpenFlow port number (if 'ofp_portp'
1273 * is non-null). On failure, returns a positive errno value and sets
1274 * '*ofp_portp' to OFPP_NONE (if 'ofp_portp' is non-null). */
1276 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1277 uint16_t *ofp_portp)
1282 error = ofproto->ofproto_class->port_add(ofproto, netdev, &ofp_port);
1284 update_port(ofproto, netdev_get_name(netdev));
1287 *ofp_portp = error ? OFPP_NONE : ofp_port;
1292 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1293 * initializes '*port' appropriately; on failure, returns a positive errno
1296 * The caller owns the data in 'ofproto_port' and must free it with
1297 * ofproto_port_destroy() when it is no longer needed. */
1299 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1300 struct ofproto_port *port)
1304 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1306 memset(port, 0, sizeof *port);
1311 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1312 * Returns 0 if successful, otherwise a positive errno. */
1314 ofproto_port_del(struct ofproto *ofproto, uint16_t ofp_port)
1316 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1317 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1320 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1321 if (!error && ofport) {
1322 /* 'name' is the netdev's name and update_port() is going to close the
1323 * netdev. Just in case update_port() refers to 'name' after it
1324 * destroys 'ofport', make a copy of it around the update_port()
1326 char *devname = xstrdup(name);
1327 update_port(ofproto, devname);
1333 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1334 * performs the 'n_actions' actions in 'actions'. The new flow will not
1337 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1338 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1339 * controllers; otherwise, it will be hidden.
1341 * The caller retains ownership of 'cls_rule' and 'actions'.
1343 * This is a helper function for in-band control and fail-open. */
1345 ofproto_add_flow(struct ofproto *ofproto, const struct cls_rule *cls_rule,
1346 const union ofp_action *actions, size_t n_actions)
1348 const struct rule *rule;
1350 rule = rule_from_cls_rule(classifier_find_rule_exactly(
1351 &ofproto->tables[0].cls, cls_rule));
1352 if (!rule || !ofputil_actions_equal(rule->actions, rule->n_actions,
1353 actions, n_actions)) {
1354 struct ofputil_flow_mod fm;
1356 memset(&fm, 0, sizeof fm);
1358 fm.buffer_id = UINT32_MAX;
1359 fm.actions = (union ofp_action *) actions;
1360 fm.n_actions = n_actions;
1361 add_flow(ofproto, NULL, &fm, NULL);
1365 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1366 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1367 * operation cannot be initiated now but may be retried later.
1369 * This is a helper function for in-band control and fail-open. */
1371 ofproto_flow_mod(struct ofproto *ofproto, const struct ofputil_flow_mod *fm)
1373 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1376 /* Searches for a rule with matching criteria exactly equal to 'target' in
1377 * ofproto's table 0 and, if it finds one, deletes it.
1379 * This is a helper function for in-band control and fail-open. */
1381 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
1385 rule = rule_from_cls_rule(classifier_find_rule_exactly(
1386 &ofproto->tables[0].cls, target));
1388 /* No such rule -> success. */
1390 } else if (rule->pending) {
1391 /* An operation on the rule is already pending -> failure.
1392 * Caller must retry later if it's important. */
1395 /* Initiate deletion -> success. */
1396 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
1397 ofoperation_create(group, rule, OFOPERATION_DELETE);
1398 oftable_remove_rule(rule);
1399 ofproto->ofproto_class->rule_destruct(rule);
1400 ofopgroup_submit(group);
1406 /* Starts the process of deleting all of the flows from all of ofproto's flow
1407 * tables and then reintroducing the flows required by in-band control and
1408 * fail-open. The process will complete in a later call to ofproto_run(). */
1410 ofproto_flush_flows(struct ofproto *ofproto)
1412 COVERAGE_INC(ofproto_flush);
1413 ofproto->state = S_FLUSH;
1417 reinit_ports(struct ofproto *p)
1419 struct ofproto_port_dump dump;
1420 struct sset devnames;
1421 struct ofport *ofport;
1422 struct ofproto_port ofproto_port;
1423 const char *devname;
1425 COVERAGE_INC(ofproto_reinit_ports);
1427 sset_init(&devnames);
1428 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1429 sset_add(&devnames, netdev_get_name(ofport->netdev));
1431 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1432 sset_add(&devnames, ofproto_port.name);
1435 SSET_FOR_EACH (devname, &devnames) {
1436 update_port(p, devname);
1438 sset_destroy(&devnames);
1441 /* Opens and returns a netdev for 'ofproto_port', or a null pointer if the
1442 * netdev cannot be opened. On success, also fills in 'opp'. */
1443 static struct netdev *
1444 ofport_open(const struct ofproto_port *ofproto_port,
1445 struct ofputil_phy_port *pp)
1447 enum netdev_flags flags;
1448 struct netdev *netdev;
1451 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
1453 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1454 "cannot be opened (%s)",
1455 ofproto_port->name, ofproto_port->ofp_port,
1456 ofproto_port->name, strerror(error));
1460 pp->port_no = ofproto_port->ofp_port;
1461 netdev_get_etheraddr(netdev, pp->hw_addr);
1462 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
1463 netdev_get_flags(netdev, &flags);
1464 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
1465 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
1466 netdev_get_features(netdev, &pp->curr, &pp->advertised,
1467 &pp->supported, &pp->peer);
1468 pp->curr_speed = netdev_features_to_bps(pp->curr);
1469 pp->max_speed = netdev_features_to_bps(pp->supported);
1474 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1475 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
1478 ofport_equal(const struct ofputil_phy_port *a,
1479 const struct ofputil_phy_port *b)
1481 return (eth_addr_equals(a->hw_addr, b->hw_addr)
1482 && a->state == b->state
1483 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
1484 && a->curr == b->curr
1485 && a->advertised == b->advertised
1486 && a->supported == b->supported
1487 && a->peer == b->peer
1488 && a->curr_speed == b->curr_speed
1489 && a->max_speed == b->max_speed);
1492 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1493 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1494 * one with the same name or port number). */
1496 ofport_install(struct ofproto *p,
1497 struct netdev *netdev, const struct ofputil_phy_port *pp)
1499 const char *netdev_name = netdev_get_name(netdev);
1500 struct ofport *ofport;
1503 /* Create ofport. */
1504 ofport = p->ofproto_class->port_alloc();
1509 ofport->ofproto = p;
1510 ofport->netdev = netdev;
1511 ofport->change_seq = netdev_change_seq(netdev);
1513 ofport->ofp_port = pp->port_no;
1515 /* Add port to 'p'. */
1516 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->ofp_port, 0));
1517 shash_add(&p->port_by_name, netdev_name, ofport);
1519 update_mtu(p, ofport);
1521 /* Let the ofproto_class initialize its private data. */
1522 error = p->ofproto_class->port_construct(ofport);
1526 connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD);
1530 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
1531 p->name, netdev_name, strerror(error));
1533 ofport_destroy__(ofport);
1535 netdev_close(netdev);
1539 /* Removes 'ofport' from 'p' and destroys it. */
1541 ofport_remove(struct ofport *ofport)
1543 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp,
1545 ofport_destroy(ofport);
1548 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1551 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1553 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1555 ofport_remove(port);
1559 /* Updates 'port' with new 'pp' description.
1561 * Does not handle a name or port number change. The caller must implement
1562 * such a change as a delete followed by an add. */
1564 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
1566 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
1567 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
1568 | (pp->config & OFPUTIL_PC_PORT_DOWN));
1569 port->pp.state = pp->state;
1570 port->pp.curr = pp->curr;
1571 port->pp.advertised = pp->advertised;
1572 port->pp.supported = pp->supported;
1573 port->pp.peer = pp->peer;
1574 port->pp.curr_speed = pp->curr_speed;
1575 port->pp.max_speed = pp->max_speed;
1577 connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY);
1580 /* Update OpenFlow 'state' in 'port' and notify controller. */
1582 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
1584 if (port->pp.state != state) {
1585 port->pp.state = state;
1586 connmgr_send_port_status(port->ofproto->connmgr, &port->pp,
1592 ofproto_port_unregister(struct ofproto *ofproto, uint16_t ofp_port)
1594 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
1596 if (port->ofproto->ofproto_class->set_realdev) {
1597 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
1599 if (port->ofproto->ofproto_class->set_stp_port) {
1600 port->ofproto->ofproto_class->set_stp_port(port, NULL);
1602 if (port->ofproto->ofproto_class->set_cfm) {
1603 port->ofproto->ofproto_class->set_cfm(port, NULL);
1605 if (port->ofproto->ofproto_class->bundle_remove) {
1606 port->ofproto->ofproto_class->bundle_remove(port);
1612 ofport_destroy__(struct ofport *port)
1614 struct ofproto *ofproto = port->ofproto;
1615 const char *name = netdev_get_name(port->netdev);
1617 hmap_remove(&ofproto->ports, &port->hmap_node);
1618 shash_delete(&ofproto->port_by_name,
1619 shash_find(&ofproto->port_by_name, name));
1621 netdev_close(port->netdev);
1622 ofproto->ofproto_class->port_dealloc(port);
1626 ofport_destroy(struct ofport *port)
1629 port->ofproto->ofproto_class->port_destruct(port);
1630 ofport_destroy__(port);
1635 ofproto_get_port(const struct ofproto *ofproto, uint16_t ofp_port)
1637 struct ofport *port;
1639 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1640 hash_int(ofp_port, 0), &ofproto->ports) {
1641 if (port->ofp_port == ofp_port) {
1649 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
1651 struct ofproto *ofproto = port->ofproto;
1654 if (ofproto->ofproto_class->port_get_stats) {
1655 error = ofproto->ofproto_class->port_get_stats(port, stats);
1664 update_port(struct ofproto *ofproto, const char *name)
1666 struct ofproto_port ofproto_port;
1667 struct ofputil_phy_port pp;
1668 struct netdev *netdev;
1669 struct ofport *port;
1671 COVERAGE_INC(ofproto_update_port);
1673 /* Fetch 'name''s location and properties from the datapath. */
1674 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
1675 ? ofport_open(&ofproto_port, &pp)
1678 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
1679 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
1680 struct netdev *old_netdev = port->netdev;
1682 /* 'name' hasn't changed location. Any properties changed? */
1683 if (!ofport_equal(&port->pp, &pp)) {
1684 ofport_modified(port, &pp);
1687 update_mtu(ofproto, port);
1689 /* Install the newly opened netdev in case it has changed.
1690 * Don't close the old netdev yet in case port_modified has to
1691 * remove a retained reference to it.*/
1692 port->netdev = netdev;
1693 port->change_seq = netdev_change_seq(netdev);
1695 if (port->ofproto->ofproto_class->port_modified) {
1696 port->ofproto->ofproto_class->port_modified(port);
1699 netdev_close(old_netdev);
1701 /* If 'port' is nonnull then its name differs from 'name' and thus
1702 * we should delete it. If we think there's a port named 'name'
1703 * then its port number must be wrong now so delete it too. */
1705 ofport_remove(port);
1707 ofport_remove_with_name(ofproto, name);
1708 ofport_install(ofproto, netdev, &pp);
1711 /* Any port named 'name' is gone now. */
1712 ofport_remove_with_name(ofproto, name);
1714 ofproto_port_destroy(&ofproto_port);
1718 init_ports(struct ofproto *p)
1720 struct ofproto_port_dump dump;
1721 struct ofproto_port ofproto_port;
1723 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1724 uint16_t ofp_port = ofproto_port.ofp_port;
1725 if (ofproto_get_port(p, ofp_port)) {
1726 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1728 } else if (shash_find(&p->port_by_name, ofproto_port.name)) {
1729 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1732 struct ofputil_phy_port pp;
1733 struct netdev *netdev;
1735 netdev = ofport_open(&ofproto_port, &pp);
1737 ofport_install(p, netdev, &pp);
1745 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
1746 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
1748 find_min_mtu(struct ofproto *p)
1750 struct ofport *ofport;
1753 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1754 struct netdev *netdev = ofport->netdev;
1757 /* Skip any internal ports, since that's what we're trying to
1759 if (!strcmp(netdev_get_type(netdev), "internal")) {
1763 if (netdev_get_mtu(netdev, &dev_mtu)) {
1766 if (!mtu || dev_mtu < mtu) {
1771 return mtu ? mtu: ETH_PAYLOAD_MAX;
1774 /* Update MTU of all datapath devices on 'p' to the minimum of the
1775 * non-datapath ports in event of 'port' added or changed. */
1777 update_mtu(struct ofproto *p, struct ofport *port)
1779 struct ofport *ofport;
1780 struct netdev *netdev = port->netdev;
1781 int dev_mtu, old_min;
1783 if (netdev_get_mtu(netdev, &dev_mtu)) {
1787 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
1788 if (dev_mtu > p->min_mtu) {
1789 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
1790 dev_mtu = p->min_mtu;
1793 port->mtu = dev_mtu;
1797 /* For non-internal port find new min mtu. */
1798 old_min = p->min_mtu;
1799 port->mtu = dev_mtu;
1800 p->min_mtu = find_min_mtu(p);
1801 if (p->min_mtu == old_min) {
1805 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1806 struct netdev *netdev = ofport->netdev;
1808 if (!strcmp(netdev_get_type(netdev), "internal")) {
1809 if (!netdev_set_mtu(netdev, p->min_mtu)) {
1810 ofport->mtu = p->min_mtu;
1817 ofproto_rule_destroy__(struct rule *rule)
1820 free(rule->actions);
1821 rule->ofproto->ofproto_class->rule_dealloc(rule);
1825 /* This function allows an ofproto implementation to destroy any rules that
1826 * remain when its ->destruct() function is called. The caller must have
1827 * already uninitialized any derived members of 'rule' (step 5 described in the
1828 * large comment in ofproto/ofproto-provider.h titled "Life Cycle").
1829 * This function implements steps 6 and 7.
1831 * This function should only be called from an ofproto implementation's
1832 * ->destruct() function. It is not suitable elsewhere. */
1834 ofproto_rule_destroy(struct rule *rule)
1836 assert(!rule->pending);
1837 oftable_remove_rule(rule);
1838 ofproto_rule_destroy__(rule);
1841 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1842 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1845 rule_has_out_port(const struct rule *rule, uint16_t out_port)
1847 const union ofp_action *oa;
1850 if (out_port == OFPP_NONE) {
1853 OFPUTIL_ACTION_FOR_EACH_UNSAFE (oa, left, rule->actions, rule->n_actions) {
1854 if (action_outputs_to_port(oa, htons(out_port))) {
1861 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1862 * statistics appropriately. 'packet' must have at least sizeof(struct
1863 * ofp_packet_in) bytes of headroom.
1865 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1866 * with statistics for 'packet' either way.
1868 * Takes ownership of 'packet'. */
1870 rule_execute(struct rule *rule, uint16_t in_port, struct ofpbuf *packet)
1874 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1876 flow_extract(packet, 0, 0, in_port, &flow);
1877 return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet);
1880 /* Returns true if 'rule' should be hidden from the controller.
1882 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1883 * (e.g. by in-band control) and are intentionally hidden from the
1886 rule_is_hidden(const struct rule *rule)
1888 return rule->cr.priority > UINT16_MAX;
1891 static enum oftable_flags
1892 rule_get_flags(const struct rule *rule)
1894 return rule->ofproto->tables[rule->table_id].flags;
1898 rule_is_modifiable(const struct rule *rule)
1900 return !(rule_get_flags(rule) & OFTABLE_READONLY);
1904 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1906 ofconn_send_reply(ofconn, make_echo_reply(oh));
1911 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1913 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1914 struct ofputil_switch_features features;
1915 struct ofport *port;
1919 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
1921 assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
1923 features.datapath_id = ofproto->datapath_id;
1924 features.n_buffers = pktbuf_capacity();
1925 features.n_tables = ofproto->n_tables;
1926 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
1927 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
1929 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
1932 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
1934 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1935 ofputil_put_switch_features_port(&port->pp, b);
1938 ofconn_send_reply(ofconn, b);
1943 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1945 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1946 struct ofp_switch_config *osc;
1947 enum ofp_config_flags flags;
1951 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1952 flags = ofproto->frag_handling;
1953 if (ofconn_get_invalid_ttl_to_controller(ofconn)) {
1954 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1956 osc->flags = htons(flags);
1957 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1958 ofconn_send_reply(ofconn, buf);
1964 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1966 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1967 uint16_t flags = ntohs(osc->flags);
1969 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
1970 || ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1971 enum ofp_config_flags cur = ofproto->frag_handling;
1972 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
1974 assert((cur & OFPC_FRAG_MASK) == cur);
1976 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
1977 ofproto->frag_handling = next;
1979 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
1981 ofputil_frag_handling_to_string(next));
1985 ofconn_set_invalid_ttl_to_controller(ofconn,
1986 (flags & OFPC_INVALID_TTL_TO_CONTROLLER));
1988 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
1993 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
1994 * error message code for the caller to propagate upward. Otherwise, returns
1997 * The log message mentions 'msg_type'. */
1999 reject_slave_controller(struct ofconn *ofconn)
2001 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2002 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
2003 return OFPERR_OFPBRC_EPERM;
2010 handle_packet_out(struct ofconn *ofconn, const struct ofp_packet_out *opo)
2012 struct ofproto *p = ofconn_get_ofproto(ofconn);
2013 struct ofputil_packet_out po;
2014 struct ofpbuf *payload;
2018 COVERAGE_INC(ofproto_packet_out);
2020 error = reject_slave_controller(ofconn);
2025 /* Decode message. */
2026 error = ofputil_decode_packet_out(&po, opo);
2032 if (po.buffer_id != UINT32_MAX) {
2033 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2034 if (error || !payload) {
2038 payload = xmalloc(sizeof *payload);
2039 ofpbuf_use_const(payload, po.packet, po.packet_len);
2042 /* Send out packet. */
2043 flow_extract(payload, 0, 0, po.in_port, &flow);
2044 error = p->ofproto_class->packet_out(p, payload, &flow,
2045 po.actions, po.n_actions);
2046 ofpbuf_delete(payload);
2052 update_port_config(struct ofport *port,
2053 enum ofputil_port_config config,
2054 enum ofputil_port_config mask)
2056 enum ofputil_port_config old_config = port->pp.config;
2057 enum ofputil_port_config toggle;
2059 toggle = (config ^ port->pp.config) & mask;
2060 if (toggle & OFPUTIL_PC_PORT_DOWN) {
2061 if (config & OFPUTIL_PC_PORT_DOWN) {
2062 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2064 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2066 toggle &= ~OFPUTIL_PC_PORT_DOWN;
2069 port->pp.config ^= toggle;
2070 if (port->pp.config != old_config) {
2071 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2076 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2078 struct ofproto *p = ofconn_get_ofproto(ofconn);
2079 struct ofputil_port_mod pm;
2080 struct ofport *port;
2083 error = reject_slave_controller(ofconn);
2088 error = ofputil_decode_port_mod(oh, &pm);
2093 port = ofproto_get_port(p, pm.port_no);
2095 return OFPERR_OFPPMFC_BAD_PORT;
2096 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
2097 return OFPERR_OFPPMFC_BAD_HW_ADDR;
2099 update_port_config(port, pm.config, pm.mask);
2101 netdev_set_advertisements(port->netdev, pm.advertise);
2108 handle_desc_stats_request(struct ofconn *ofconn,
2109 const struct ofp_stats_msg *request)
2111 struct ofproto *p = ofconn_get_ofproto(ofconn);
2112 struct ofp_desc_stats *ods;
2115 ods = ofputil_make_stats_reply(sizeof *ods, request, &msg);
2116 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
2117 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
2118 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
2119 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
2120 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2121 ofconn_send_reply(ofconn, msg);
2127 handle_table_stats_request(struct ofconn *ofconn,
2128 const struct ofp_stats_msg *request)
2130 struct ofproto *p = ofconn_get_ofproto(ofconn);
2131 struct ofp_table_stats *ots;
2135 ofputil_make_stats_reply(sizeof(struct ofp_stats_msg), request, &msg);
2137 ots = ofpbuf_put_zeros(msg, sizeof *ots * p->n_tables);
2138 for (i = 0; i < p->n_tables; i++) {
2139 ots[i].table_id = i;
2140 sprintf(ots[i].name, "table%zu", i);
2141 ots[i].wildcards = htonl(OFPFW_ALL);
2142 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
2143 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
2146 p->ofproto_class->get_tables(p, ots);
2148 for (i = 0; i < p->n_tables; i++) {
2149 const struct oftable *table = &p->tables[i];
2152 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
2155 if (table->max_flows < ntohl(ots[i].max_entries)) {
2156 ots[i].max_entries = htonl(table->max_flows);
2160 ofconn_send_reply(ofconn, msg);
2165 append_port_stat(struct ofport *port, struct list *replies)
2167 struct netdev_stats stats;
2168 struct ofp_port_stats *ops;
2170 /* Intentionally ignore return value, since errors will set
2171 * 'stats' to all-1s, which is correct for OpenFlow, and
2172 * netdev_get_stats() will log errors. */
2173 ofproto_port_get_stats(port, &stats);
2175 ops = ofputil_append_stats_reply(sizeof *ops, replies);
2176 ops->port_no = htons(port->pp.port_no);
2177 memset(ops->pad, 0, sizeof ops->pad);
2178 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
2179 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
2180 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
2181 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
2182 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
2183 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
2184 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
2185 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
2186 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
2187 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
2188 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
2189 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
2193 handle_port_stats_request(struct ofconn *ofconn,
2194 const struct ofp_port_stats_request *psr)
2196 struct ofproto *p = ofconn_get_ofproto(ofconn);
2197 struct ofport *port;
2198 struct list replies;
2200 ofputil_start_stats_reply(&psr->osm, &replies);
2201 if (psr->port_no != htons(OFPP_NONE)) {
2202 port = ofproto_get_port(p, ntohs(psr->port_no));
2204 append_port_stat(port, &replies);
2207 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2208 append_port_stat(port, &replies);
2212 ofconn_send_replies(ofconn, &replies);
2217 handle_port_desc_stats_request(struct ofconn *ofconn,
2218 const struct ofp_stats_msg *osm)
2220 struct ofproto *p = ofconn_get_ofproto(ofconn);
2221 struct ofport *port;
2222 struct list replies;
2224 ofputil_start_stats_reply(osm, &replies);
2226 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2227 ofputil_append_port_desc_stats_reply(ofconn_get_protocol(ofconn),
2228 &port->pp, &replies);
2231 ofconn_send_replies(ofconn, &replies);
2236 calc_flow_duration__(long long int start, long long int now,
2237 uint32_t *sec, uint32_t *nsec)
2239 long long int msecs = now - start;
2240 *sec = msecs / 1000;
2241 *nsec = (msecs % 1000) * (1000 * 1000);
2244 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
2245 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
2247 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
2249 return (table_id == 0xff || table_id < ofproto->n_tables
2251 : OFPERR_NXBRC_BAD_TABLE_ID);
2255 static struct oftable *
2256 next_visible_table(struct ofproto *ofproto, uint8_t table_id)
2258 struct oftable *table;
2260 for (table = &ofproto->tables[table_id];
2261 table < &ofproto->tables[ofproto->n_tables];
2263 if (!(table->flags & OFTABLE_HIDDEN)) {
2271 static struct oftable *
2272 first_matching_table(struct ofproto *ofproto, uint8_t table_id)
2274 if (table_id == 0xff) {
2275 return next_visible_table(ofproto, 0);
2276 } else if (table_id < ofproto->n_tables) {
2277 return &ofproto->tables[table_id];
2283 static struct oftable *
2284 next_matching_table(struct ofproto *ofproto,
2285 struct oftable *table, uint8_t table_id)
2287 return (table_id == 0xff
2288 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
2292 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
2294 * - If TABLE_ID is 0xff, this iterates over every classifier table in
2295 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
2297 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
2298 * only once, for that table. (This can be used to access tables marked
2301 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
2302 * entered at all. (Perhaps you should have validated TABLE_ID with
2303 * check_table_id().)
2305 * All parameters are evaluated multiple times.
2307 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
2308 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
2310 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
2312 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
2313 * 'table_id' is 0xff) that match 'match' in the "loose" way required for
2314 * OpenFlow OFPFC_MODIFY and OFPFC_DELETE requests and puts them on list
2317 * If 'out_port' is anything other than OFPP_NONE, then only rules that output
2318 * to 'out_port' are included.
2320 * Hidden rules are always omitted.
2322 * Returns 0 on success, otherwise an OpenFlow error code. */
2324 collect_rules_loose(struct ofproto *ofproto, uint8_t table_id,
2325 const struct cls_rule *match,
2326 ovs_be64 cookie, ovs_be64 cookie_mask,
2327 uint16_t out_port, struct list *rules)
2329 struct oftable *table;
2332 error = check_table_id(ofproto, table_id);
2338 FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) {
2339 struct cls_cursor cursor;
2342 cls_cursor_init(&cursor, &table->cls, match);
2343 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2344 if (rule->pending) {
2345 return OFPROTO_POSTPONE;
2347 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)
2348 && !((rule->flow_cookie ^ cookie) & cookie_mask)) {
2349 list_push_back(rules, &rule->ofproto_node);
2356 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
2357 * 'table_id' is 0xff) that match 'match' in the "strict" way required for
2358 * OpenFlow OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests and puts them
2361 * If 'out_port' is anything other than OFPP_NONE, then only rules that output
2362 * to 'out_port' are included.
2364 * Hidden rules are always omitted.
2366 * Returns 0 on success, otherwise an OpenFlow error code. */
2368 collect_rules_strict(struct ofproto *ofproto, uint8_t table_id,
2369 const struct cls_rule *match,
2370 ovs_be64 cookie, ovs_be64 cookie_mask,
2371 uint16_t out_port, struct list *rules)
2373 struct oftable *table;
2376 error = check_table_id(ofproto, table_id);
2382 FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) {
2385 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls,
2388 if (rule->pending) {
2389 return OFPROTO_POSTPONE;
2391 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)
2392 && !((rule->flow_cookie ^ cookie) & cookie_mask)) {
2393 list_push_back(rules, &rule->ofproto_node);
2400 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
2401 * forced into the range of a uint16_t. */
2403 age_secs(long long int age_ms)
2405 return (age_ms < 0 ? 0
2406 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
2407 : (unsigned int) age_ms / 1000);
2411 handle_flow_stats_request(struct ofconn *ofconn,
2412 const struct ofp_stats_msg *osm)
2414 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2415 struct ofputil_flow_stats_request fsr;
2416 struct list replies;
2421 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
2426 error = collect_rules_loose(ofproto, fsr.table_id, &fsr.match,
2427 fsr.cookie, fsr.cookie_mask,
2428 fsr.out_port, &rules);
2433 ofputil_start_stats_reply(osm, &replies);
2434 LIST_FOR_EACH (rule, ofproto_node, &rules) {
2435 long long int now = time_msec();
2436 struct ofputil_flow_stats fs;
2439 fs.cookie = rule->flow_cookie;
2440 fs.table_id = rule->table_id;
2441 calc_flow_duration__(rule->created, now, &fs.duration_sec,
2443 fs.idle_timeout = rule->idle_timeout;
2444 fs.hard_timeout = rule->hard_timeout;
2445 fs.idle_age = age_secs(now - rule->used);
2446 fs.hard_age = age_secs(now - rule->modified);
2447 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
2449 fs.actions = rule->actions;
2450 fs.n_actions = rule->n_actions;
2451 ofputil_append_flow_stats_reply(&fs, &replies);
2453 ofconn_send_replies(ofconn, &replies);
2459 flow_stats_ds(struct rule *rule, struct ds *results)
2461 uint64_t packet_count, byte_count;
2463 rule->ofproto->ofproto_class->rule_get_stats(rule,
2464 &packet_count, &byte_count);
2466 if (rule->table_id != 0) {
2467 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
2469 ds_put_format(results, "duration=%llds, ",
2470 (time_msec() - rule->created) / 1000);
2471 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2472 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2473 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2474 cls_rule_format(&rule->cr, results);
2475 ds_put_char(results, ',');
2476 if (rule->n_actions > 0) {
2477 ofp_print_actions(results, rule->actions, rule->n_actions);
2479 ds_put_cstr(results, "drop");
2481 ds_put_cstr(results, "\n");
2484 /* Adds a pretty-printed description of all flows to 'results', including
2485 * hidden flows (e.g., set up by in-band control). */
2487 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
2489 struct oftable *table;
2491 OFPROTO_FOR_EACH_TABLE (table, p) {
2492 struct cls_cursor cursor;
2495 cls_cursor_init(&cursor, &table->cls, NULL);
2496 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2497 flow_stats_ds(rule, results);
2502 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
2503 * '*engine_type' and '*engine_id', respectively. */
2505 ofproto_get_netflow_ids(const struct ofproto *ofproto,
2506 uint8_t *engine_type, uint8_t *engine_id)
2508 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
2511 /* Checks the fault status of CFM for 'ofp_port' within 'ofproto'. Returns a
2512 * bitmask of 'cfm_fault_reason's to indicate a CFM fault (generally
2513 * indicating a connectivity problem). Returns zero if CFM is not faulted,
2514 * and -1 if CFM is not enabled on 'port'. */
2516 ofproto_port_get_cfm_fault(const struct ofproto *ofproto, uint16_t ofp_port)
2518 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
2519 return (ofport && ofproto->ofproto_class->get_cfm_fault
2520 ? ofproto->ofproto_class->get_cfm_fault(ofport)
2524 /* Gets the MPIDs of the remote maintenance points broadcasting to 'ofp_port'
2525 * within 'ofproto'. Populates 'rmps' with an array of MPIDs owned by
2526 * 'ofproto', and 'n_rmps' with the number of MPIDs in 'rmps'. Returns a
2527 * number less than 0 if CFM is not enabled on 'ofp_port'. */
2529 ofproto_port_get_cfm_remote_mpids(const struct ofproto *ofproto,
2530 uint16_t ofp_port, const uint64_t **rmps,
2533 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
2537 return (ofport && ofproto->ofproto_class->get_cfm_remote_mpids
2538 ? ofproto->ofproto_class->get_cfm_remote_mpids(ofport, rmps,
2543 /* Checks the health of the CFM for 'ofp_port' within 'ofproto'. Returns an
2544 * integer value between 0 and 100 to indicate the health of the port as a
2545 * percentage which is the average of cfm health of all the remote_mpids or
2546 * returns -1 if CFM is not enabled on 'ofport'. */
2548 ofproto_port_get_cfm_health(const struct ofproto *ofproto, uint16_t ofp_port)
2550 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
2551 return (ofport && ofproto->ofproto_class->get_cfm_health
2552 ? ofproto->ofproto_class->get_cfm_health(ofport)
2557 handle_aggregate_stats_request(struct ofconn *ofconn,
2558 const struct ofp_stats_msg *osm)
2560 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2561 struct ofputil_flow_stats_request request;
2562 struct ofputil_aggregate_stats stats;
2563 bool unknown_packets, unknown_bytes;
2564 struct ofpbuf *reply;
2569 error = ofputil_decode_flow_stats_request(&request, &osm->header);
2574 error = collect_rules_loose(ofproto, request.table_id, &request.match,
2575 request.cookie, request.cookie_mask,
2576 request.out_port, &rules);
2581 memset(&stats, 0, sizeof stats);
2582 unknown_packets = unknown_bytes = false;
2583 LIST_FOR_EACH (rule, ofproto_node, &rules) {
2584 uint64_t packet_count;
2585 uint64_t byte_count;
2587 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
2590 if (packet_count == UINT64_MAX) {
2591 unknown_packets = true;
2593 stats.packet_count += packet_count;
2596 if (byte_count == UINT64_MAX) {
2597 unknown_bytes = true;
2599 stats.byte_count += byte_count;
2604 if (unknown_packets) {
2605 stats.packet_count = UINT64_MAX;
2607 if (unknown_bytes) {
2608 stats.byte_count = UINT64_MAX;
2611 reply = ofputil_encode_aggregate_stats_reply(&stats, osm);
2612 ofconn_send_reply(ofconn, reply);
2617 struct queue_stats_cbdata {
2618 struct ofport *ofport;
2619 struct list replies;
2623 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
2624 const struct netdev_queue_stats *stats)
2626 struct ofp_queue_stats *reply;
2628 reply = ofputil_append_stats_reply(sizeof *reply, &cbdata->replies);
2629 reply->port_no = htons(cbdata->ofport->pp.port_no);
2630 memset(reply->pad, 0, sizeof reply->pad);
2631 reply->queue_id = htonl(queue_id);
2632 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
2633 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
2634 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
2638 handle_queue_stats_dump_cb(uint32_t queue_id,
2639 struct netdev_queue_stats *stats,
2642 struct queue_stats_cbdata *cbdata = cbdata_;
2644 put_queue_stats(cbdata, queue_id, stats);
2648 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
2649 struct queue_stats_cbdata *cbdata)
2651 cbdata->ofport = port;
2652 if (queue_id == OFPQ_ALL) {
2653 netdev_dump_queue_stats(port->netdev,
2654 handle_queue_stats_dump_cb, cbdata);
2656 struct netdev_queue_stats stats;
2658 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
2659 put_queue_stats(cbdata, queue_id, &stats);
2665 handle_queue_stats_request(struct ofconn *ofconn,
2666 const struct ofp_queue_stats_request *qsr)
2668 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2669 struct queue_stats_cbdata cbdata;
2670 struct ofport *port;
2671 unsigned int port_no;
2674 COVERAGE_INC(ofproto_queue_req);
2676 ofputil_start_stats_reply(&qsr->osm, &cbdata.replies);
2678 port_no = ntohs(qsr->port_no);
2679 queue_id = ntohl(qsr->queue_id);
2680 if (port_no == OFPP_ALL) {
2681 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2682 handle_queue_stats_for_port(port, queue_id, &cbdata);
2684 } else if (port_no < OFPP_MAX) {
2685 port = ofproto_get_port(ofproto, port_no);
2687 handle_queue_stats_for_port(port, queue_id, &cbdata);
2690 ofpbuf_list_delete(&cbdata.replies);
2691 return OFPERR_OFPQOFC_BAD_PORT;
2693 ofconn_send_replies(ofconn, &cbdata.replies);
2699 is_flow_deletion_pending(const struct ofproto *ofproto,
2700 const struct cls_rule *cls_rule,
2703 if (!hmap_is_empty(&ofproto->deletions)) {
2704 struct ofoperation *op;
2706 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
2707 cls_rule_hash(cls_rule, table_id),
2708 &ofproto->deletions) {
2709 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
2718 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
2719 * in which no matching flow already exists in the flow table.
2721 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
2722 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
2723 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
2724 * initiated now but may be retried later.
2726 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
2729 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
2730 const struct ofputil_flow_mod *fm, const struct ofp_header *request)
2732 struct oftable *table;
2733 struct ofopgroup *group;
2734 struct rule *victim;
2738 error = check_table_id(ofproto, fm->table_id);
2744 if (fm->table_id == 0xff) {
2746 if (ofproto->ofproto_class->rule_choose_table) {
2747 error = ofproto->ofproto_class->rule_choose_table(ofproto, &fm->cr,
2752 assert(table_id < ofproto->n_tables);
2753 table = &ofproto->tables[table_id];
2755 table = &ofproto->tables[0];
2757 } else if (fm->table_id < ofproto->n_tables) {
2758 table = &ofproto->tables[fm->table_id];
2760 return OFPERR_NXFMFC_BAD_TABLE_ID;
2763 if (table->flags & OFTABLE_READONLY) {
2764 return OFPERR_OFPBRC_EPERM;
2767 /* Check for overlap, if requested. */
2768 if (fm->flags & OFPFF_CHECK_OVERLAP
2769 && classifier_rule_overlaps(&table->cls, &fm->cr)) {
2770 return OFPERR_OFPFMFC_OVERLAP;
2773 /* Serialize against pending deletion. */
2774 if (is_flow_deletion_pending(ofproto, &fm->cr, table - ofproto->tables)) {
2775 return OFPROTO_POSTPONE;
2778 /* Allocate new rule. */
2779 rule = ofproto->ofproto_class->rule_alloc();
2781 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
2782 ofproto->name, strerror(error));
2785 rule->ofproto = ofproto;
2787 rule->pending = NULL;
2788 rule->flow_cookie = fm->new_cookie;
2789 rule->created = rule->modified = rule->used = time_msec();
2790 rule->idle_timeout = fm->idle_timeout;
2791 rule->hard_timeout = fm->hard_timeout;
2792 rule->table_id = table - ofproto->tables;
2793 rule->send_flow_removed = (fm->flags & OFPFF_SEND_FLOW_REM) != 0;
2794 rule->actions = ofputil_actions_clone(fm->actions, fm->n_actions);
2795 rule->n_actions = fm->n_actions;
2796 rule->evictable = true;
2797 rule->eviction_group = NULL;
2799 /* Insert new rule. */
2800 victim = oftable_replace_rule(rule);
2801 if (victim && !rule_is_modifiable(victim)) {
2802 error = OFPERR_OFPBRC_EPERM;
2803 } else if (victim && victim->pending) {
2804 error = OFPROTO_POSTPONE;
2808 if (classifier_count(&table->cls) > table->max_flows) {
2811 was_evictable = rule->evictable;
2812 rule->evictable = false;
2813 evict = choose_rule_to_evict(table);
2814 rule->evictable = was_evictable;
2817 error = OFPERR_OFPFMFC_ALL_TABLES_FULL;
2819 } else if (evict->pending) {
2820 error = OFPROTO_POSTPONE;
2827 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
2828 ofoperation_create(group, rule, OFOPERATION_ADD);
2829 rule->pending->victim = victim;
2831 error = ofproto->ofproto_class->rule_construct(rule);
2833 ofoperation_destroy(rule->pending);
2835 delete_flow__(evict, group);
2837 ofopgroup_submit(group);
2841 /* Back out if an error occurred. */
2843 oftable_substitute_rule(rule, victim);
2844 ofproto_rule_destroy__(rule);
2849 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
2851 /* Modifies the rules listed in 'rules', changing their actions to match those
2854 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
2857 * Returns 0 on success, otherwise an OpenFlow error code. */
2859 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
2860 const struct ofputil_flow_mod *fm,
2861 const struct ofp_header *request, struct list *rules)
2863 struct ofopgroup *group;
2867 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
2868 error = OFPERR_OFPBRC_EPERM;
2869 LIST_FOR_EACH (rule, ofproto_node, rules) {
2870 if (rule_is_modifiable(rule)) {
2871 /* At least one rule is modifiable, don't report EPERM error. */
2877 if (!ofputil_actions_equal(fm->actions, fm->n_actions,
2878 rule->actions, rule->n_actions)) {
2879 ofoperation_create(group, rule, OFOPERATION_MODIFY);
2880 rule->pending->actions = rule->actions;
2881 rule->pending->n_actions = rule->n_actions;
2882 rule->actions = ofputil_actions_clone(fm->actions, fm->n_actions);
2883 rule->n_actions = fm->n_actions;
2884 ofproto->ofproto_class->rule_modify_actions(rule);
2886 rule->modified = time_msec();
2888 if (fm->new_cookie != htonll(UINT64_MAX)) {
2889 rule->flow_cookie = fm->new_cookie;
2892 ofopgroup_submit(group);
2897 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
2900 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
2903 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
2904 const struct ofputil_flow_mod *fm,
2905 const struct ofp_header *request)
2910 error = collect_rules_loose(ofproto, fm->table_id, &fm->cr,
2911 fm->cookie, fm->cookie_mask,
2915 } else if (list_is_empty(&rules)) {
2916 return fm->cookie_mask ? 0 : add_flow(ofproto, ofconn, fm, request);
2918 return modify_flows__(ofproto, ofconn, fm, request, &rules);
2922 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
2925 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
2928 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
2929 const struct ofputil_flow_mod *fm,
2930 const struct ofp_header *request)
2935 error = collect_rules_strict(ofproto, fm->table_id, &fm->cr,
2936 fm->cookie, fm->cookie_mask,
2941 } else if (list_is_empty(&rules)) {
2942 return fm->cookie_mask ? 0 : add_flow(ofproto, ofconn, fm, request);
2944 return list_is_singleton(&rules) ? modify_flows__(ofproto, ofconn,
2945 fm, request, &rules)
2950 /* OFPFC_DELETE implementation. */
2953 delete_flow__(struct rule *rule, struct ofopgroup *group)
2955 struct ofproto *ofproto = rule->ofproto;
2957 ofproto_rule_send_removed(rule, OFPRR_DELETE);
2959 ofoperation_create(group, rule, OFOPERATION_DELETE);
2960 oftable_remove_rule(rule);
2961 ofproto->ofproto_class->rule_destruct(rule);
2964 /* Deletes the rules listed in 'rules'.
2966 * Returns 0 on success, otherwise an OpenFlow error code. */
2968 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
2969 const struct ofp_header *request, struct list *rules)
2971 struct rule *rule, *next;
2972 struct ofopgroup *group;
2974 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
2975 LIST_FOR_EACH_SAFE (rule, next, ofproto_node, rules) {
2976 delete_flow__(rule, group);
2978 ofopgroup_submit(group);
2983 /* Implements OFPFC_DELETE. */
2985 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
2986 const struct ofputil_flow_mod *fm,
2987 const struct ofp_header *request)
2992 error = collect_rules_loose(ofproto, fm->table_id, &fm->cr,
2993 fm->cookie, fm->cookie_mask,
2994 fm->out_port, &rules);
2995 return (error ? error
2996 : !list_is_empty(&rules) ? delete_flows__(ofproto, ofconn, request,
3001 /* Implements OFPFC_DELETE_STRICT. */
3003 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3004 const struct ofputil_flow_mod *fm,
3005 const struct ofp_header *request)
3010 error = collect_rules_strict(ofproto, fm->table_id, &fm->cr,
3011 fm->cookie, fm->cookie_mask,
3012 fm->out_port, &rules);
3013 return (error ? error
3014 : list_is_singleton(&rules) ? delete_flows__(ofproto, ofconn,
3020 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
3022 struct ofputil_flow_removed fr;
3024 if (rule_is_hidden(rule) || !rule->send_flow_removed) {
3029 fr.cookie = rule->flow_cookie;
3031 calc_flow_duration__(rule->created, time_msec(),
3032 &fr.duration_sec, &fr.duration_nsec);
3033 fr.idle_timeout = rule->idle_timeout;
3034 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
3037 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
3041 ofproto_rule_update_used(struct rule *rule, long long int used)
3043 if (used > rule->used) {
3044 struct eviction_group *evg = rule->eviction_group;
3048 heap_change(&evg->rules, &rule->evg_node,
3049 rule_eviction_priority(rule));
3054 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
3055 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
3058 * ofproto implementation ->run() functions should use this function to expire
3059 * OpenFlow flows. */
3061 ofproto_rule_expire(struct rule *rule, uint8_t reason)
3063 struct ofproto *ofproto = rule->ofproto;
3064 struct ofopgroup *group;
3066 assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT);
3068 ofproto_rule_send_removed(rule, reason);
3070 group = ofopgroup_create_unattached(ofproto);
3071 ofoperation_create(group, rule, OFOPERATION_DELETE);
3072 oftable_remove_rule(rule);
3073 ofproto->ofproto_class->rule_destruct(rule);
3074 ofopgroup_submit(group);
3078 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3080 struct ofputil_flow_mod fm;
3083 error = reject_slave_controller(ofconn);
3088 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn));
3093 /* We do not support the emergency flow cache. It will hopefully get
3094 * dropped from OpenFlow in the near future. */
3095 if (fm.flags & OFPFF_EMERG) {
3096 /* There isn't a good fit for an error code, so just state that the
3097 * flow table is full. */
3098 return OFPERR_OFPFMFC_ALL_TABLES_FULL;
3101 return handle_flow_mod__(ofconn_get_ofproto(ofconn), ofconn, &fm, oh);
3105 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
3106 const struct ofputil_flow_mod *fm,
3107 const struct ofp_header *oh)
3109 if (ofproto->n_pending >= 50) {
3110 assert(!list_is_empty(&ofproto->pending));
3111 return OFPROTO_POSTPONE;
3114 switch (fm->command) {
3116 return add_flow(ofproto, ofconn, fm, oh);
3119 return modify_flows_loose(ofproto, ofconn, fm, oh);
3121 case OFPFC_MODIFY_STRICT:
3122 return modify_flow_strict(ofproto, ofconn, fm, oh);
3125 return delete_flows_loose(ofproto, ofconn, fm, oh);
3127 case OFPFC_DELETE_STRICT:
3128 return delete_flow_strict(ofproto, ofconn, fm, oh);
3131 if (fm->command > 0xff) {
3132 VLOG_WARN_RL(&rl, "flow_mod has explicit table_id but "
3133 "flow_mod_table_id extension is not enabled");
3135 return OFPERR_OFPFMFC_BAD_COMMAND;
3140 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3142 struct nx_role_request *nrr = (struct nx_role_request *) oh;
3143 struct nx_role_request *reply;
3147 role = ntohl(nrr->role);
3148 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
3149 && role != NX_ROLE_SLAVE) {
3150 return OFPERR_OFPRRFC_BAD_ROLE;
3153 if (ofconn_get_role(ofconn) != role
3154 && ofconn_has_pending_opgroups(ofconn)) {
3155 return OFPROTO_POSTPONE;
3158 ofconn_set_role(ofconn, role);
3160 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
3161 reply->role = htonl(role);
3162 ofconn_send_reply(ofconn, buf);
3168 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
3169 const struct ofp_header *oh)
3171 const struct nx_flow_mod_table_id *msg
3172 = (const struct nx_flow_mod_table_id *) oh;
3173 enum ofputil_protocol cur, next;
3175 cur = ofconn_get_protocol(ofconn);
3176 next = ofputil_protocol_set_tid(cur, msg->set != 0);
3177 ofconn_set_protocol(ofconn, next);
3183 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
3185 const struct nx_set_flow_format *msg
3186 = (const struct nx_set_flow_format *) oh;
3187 enum ofputil_protocol cur, next;
3188 enum ofputil_protocol next_base;
3190 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
3192 return OFPERR_OFPBRC_EPERM;
3195 cur = ofconn_get_protocol(ofconn);
3196 next = ofputil_protocol_set_base(cur, next_base);
3197 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
3198 /* Avoid sending async messages in surprising protocol. */
3199 return OFPROTO_POSTPONE;
3202 ofconn_set_protocol(ofconn, next);
3207 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
3208 const struct ofp_header *oh)
3210 const struct nx_set_packet_in_format *msg;
3213 msg = (const struct nx_set_packet_in_format *) oh;
3214 format = ntohl(msg->format);
3215 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
3216 return OFPERR_OFPBRC_EPERM;
3219 if (format != ofconn_get_packet_in_format(ofconn)
3220 && ofconn_has_pending_opgroups(ofconn)) {
3221 /* Avoid sending async message in surprsing packet in format. */
3222 return OFPROTO_POSTPONE;
3225 ofconn_set_packet_in_format(ofconn, format);
3230 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
3232 const struct nx_async_config *msg = (const struct nx_async_config *) oh;
3233 uint32_t master[OAM_N_TYPES];
3234 uint32_t slave[OAM_N_TYPES];
3236 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
3237 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
3238 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
3240 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
3241 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
3242 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
3244 ofconn_set_async_config(ofconn, master, slave);
3250 handle_nxt_set_controller_id(struct ofconn *ofconn,
3251 const struct ofp_header *oh)
3253 const struct nx_controller_id *nci;
3255 nci = (const struct nx_controller_id *) oh;
3256 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
3257 return OFPERR_NXBRC_MUST_BE_ZERO;
3260 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
3265 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
3269 if (ofconn_has_pending_opgroups(ofconn)) {
3270 return OFPROTO_POSTPONE;
3273 make_openflow_xid(sizeof *oh, OFPT10_BARRIER_REPLY, oh->xid, &buf);
3274 ofconn_send_reply(ofconn, buf);
3279 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
3281 const struct ofp_header *oh = msg->data;
3282 const struct ofputil_msg_type *type;
3285 error = ofputil_decode_msg_type(oh, &type);
3290 switch (ofputil_msg_type_code(type)) {
3291 /* OpenFlow requests. */
3292 case OFPUTIL_OFPT_ECHO_REQUEST:
3293 return handle_echo_request(ofconn, oh);
3295 case OFPUTIL_OFPT_FEATURES_REQUEST:
3296 return handle_features_request(ofconn, oh);
3298 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
3299 return handle_get_config_request(ofconn, oh);
3301 case OFPUTIL_OFPT_SET_CONFIG:
3302 return handle_set_config(ofconn, msg->data);
3304 case OFPUTIL_OFPT_PACKET_OUT:
3305 return handle_packet_out(ofconn, msg->data);
3307 case OFPUTIL_OFPT_PORT_MOD:
3308 return handle_port_mod(ofconn, oh);
3310 case OFPUTIL_OFPT_FLOW_MOD:
3311 return handle_flow_mod(ofconn, oh);
3313 case OFPUTIL_OFPT_BARRIER_REQUEST:
3314 return handle_barrier_request(ofconn, oh);
3316 /* OpenFlow replies. */
3317 case OFPUTIL_OFPT_ECHO_REPLY:
3320 /* Nicira extension requests. */
3321 case OFPUTIL_NXT_ROLE_REQUEST:
3322 return handle_role_request(ofconn, oh);
3324 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
3325 return handle_nxt_flow_mod_table_id(ofconn, oh);
3327 case OFPUTIL_NXT_SET_FLOW_FORMAT:
3328 return handle_nxt_set_flow_format(ofconn, oh);
3330 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
3331 return handle_nxt_set_packet_in_format(ofconn, oh);
3333 case OFPUTIL_NXT_SET_CONTROLLER_ID:
3334 return handle_nxt_set_controller_id(ofconn, oh);
3336 case OFPUTIL_NXT_FLOW_MOD:
3337 return handle_flow_mod(ofconn, oh);
3339 case OFPUTIL_NXT_FLOW_AGE:
3340 /* Nothing to do. */
3343 case OFPUTIL_NXT_SET_ASYNC_CONFIG:
3344 return handle_nxt_set_async_config(ofconn, oh);
3346 /* Statistics requests. */
3347 case OFPUTIL_OFPST_DESC_REQUEST:
3348 return handle_desc_stats_request(ofconn, msg->data);
3350 case OFPUTIL_OFPST_FLOW_REQUEST:
3351 case OFPUTIL_NXST_FLOW_REQUEST:
3352 return handle_flow_stats_request(ofconn, msg->data);
3354 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
3355 case OFPUTIL_NXST_AGGREGATE_REQUEST:
3356 return handle_aggregate_stats_request(ofconn, msg->data);
3358 case OFPUTIL_OFPST_TABLE_REQUEST:
3359 return handle_table_stats_request(ofconn, msg->data);
3361 case OFPUTIL_OFPST_PORT_REQUEST:
3362 return handle_port_stats_request(ofconn, msg->data);
3364 case OFPUTIL_OFPST_QUEUE_REQUEST:
3365 return handle_queue_stats_request(ofconn, msg->data);
3367 case OFPUTIL_OFPST_PORT_DESC_REQUEST:
3368 return handle_port_desc_stats_request(ofconn, msg->data);
3370 case OFPUTIL_MSG_INVALID:
3371 case OFPUTIL_OFPT_HELLO:
3372 case OFPUTIL_OFPT_ERROR:
3373 case OFPUTIL_OFPT_FEATURES_REPLY:
3374 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
3375 case OFPUTIL_OFPT_PACKET_IN:
3376 case OFPUTIL_OFPT_FLOW_REMOVED:
3377 case OFPUTIL_OFPT_PORT_STATUS:
3378 case OFPUTIL_OFPT_BARRIER_REPLY:
3379 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
3380 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
3381 case OFPUTIL_OFPST_DESC_REPLY:
3382 case OFPUTIL_OFPST_FLOW_REPLY:
3383 case OFPUTIL_OFPST_QUEUE_REPLY:
3384 case OFPUTIL_OFPST_PORT_REPLY:
3385 case OFPUTIL_OFPST_TABLE_REPLY:
3386 case OFPUTIL_OFPST_AGGREGATE_REPLY:
3387 case OFPUTIL_OFPST_PORT_DESC_REPLY:
3388 case OFPUTIL_NXT_ROLE_REPLY:
3389 case OFPUTIL_NXT_FLOW_REMOVED:
3390 case OFPUTIL_NXT_PACKET_IN:
3391 case OFPUTIL_NXST_FLOW_REPLY:
3392 case OFPUTIL_NXST_AGGREGATE_REPLY:
3394 return (oh->type == OFPT10_STATS_REQUEST ||
3395 oh->type == OFPT10_STATS_REPLY
3396 ? OFPERR_OFPBRC_BAD_STAT
3397 : OFPERR_OFPBRC_BAD_TYPE);
3402 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
3404 int error = handle_openflow__(ofconn, ofp_msg);
3405 if (error && error != OFPROTO_POSTPONE) {
3406 ofconn_send_error(ofconn, ofp_msg->data, error);
3408 COVERAGE_INC(ofproto_recv_openflow);
3409 return error != OFPROTO_POSTPONE;
3412 /* Asynchronous operations. */
3414 /* Creates and returns a new ofopgroup that is not associated with any
3415 * OpenFlow connection.
3417 * The caller should add operations to the returned group with
3418 * ofoperation_create() and then submit it with ofopgroup_submit(). */
3419 static struct ofopgroup *
3420 ofopgroup_create_unattached(struct ofproto *ofproto)
3422 struct ofopgroup *group = xzalloc(sizeof *group);
3423 group->ofproto = ofproto;
3424 list_init(&group->ofproto_node);
3425 list_init(&group->ops);
3426 list_init(&group->ofconn_node);
3430 /* Creates and returns a new ofopgroup for 'ofproto'.
3432 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
3433 * connection. The 'request' and 'buffer_id' arguments are ignored.
3435 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
3436 * If the ofopgroup eventually fails, then the error reply will include
3437 * 'request'. If the ofopgroup eventually succeeds, then the packet with
3438 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
3440 * The caller should add operations to the returned group with
3441 * ofoperation_create() and then submit it with ofopgroup_submit(). */
3442 static struct ofopgroup *
3443 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
3444 const struct ofp_header *request, uint32_t buffer_id)
3446 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
3448 size_t request_len = ntohs(request->length);
3450 assert(ofconn_get_ofproto(ofconn) == ofproto);
3452 ofconn_add_opgroup(ofconn, &group->ofconn_node);
3453 group->ofconn = ofconn;
3454 group->request = xmemdup(request, MIN(request_len, 64));
3455 group->buffer_id = buffer_id;
3460 /* Submits 'group' for processing.
3462 * If 'group' contains no operations (e.g. none were ever added, or all of the
3463 * ones that were added completed synchronously), then it is destroyed
3464 * immediately. Otherwise it is added to the ofproto's list of pending
3467 ofopgroup_submit(struct ofopgroup *group)
3469 if (list_is_empty(&group->ops)) {
3470 ofopgroup_destroy(group);
3472 list_push_back(&group->ofproto->pending, &group->ofproto_node);
3473 group->ofproto->n_pending++;
3478 ofopgroup_destroy(struct ofopgroup *group)
3480 assert(list_is_empty(&group->ops));
3481 if (!list_is_empty(&group->ofproto_node)) {
3482 assert(group->ofproto->n_pending > 0);
3483 group->ofproto->n_pending--;
3484 list_remove(&group->ofproto_node);
3486 if (!list_is_empty(&group->ofconn_node)) {
3487 list_remove(&group->ofconn_node);
3489 ofconn_send_error(group->ofconn, group->request, group->error);
3491 connmgr_retry(group->ofproto->connmgr);
3493 free(group->request);
3497 /* Initiates a new operation on 'rule', of the specified 'type', within
3498 * 'group'. Prior to calling, 'rule' must not have any pending operation. */
3500 ofoperation_create(struct ofopgroup *group, struct rule *rule,
3501 enum ofoperation_type type)
3503 struct ofoperation *op;
3505 assert(!rule->pending);
3507 op = rule->pending = xzalloc(sizeof *op);
3509 list_push_back(&group->ops, &op->group_node);
3513 op->flow_cookie = rule->flow_cookie;
3515 if (type == OFOPERATION_DELETE) {
3516 hmap_insert(&op->group->ofproto->deletions, &op->hmap_node,
3517 cls_rule_hash(&rule->cr, rule->table_id));
3522 ofoperation_destroy(struct ofoperation *op)
3524 struct ofopgroup *group = op->group;
3527 op->rule->pending = NULL;
3529 if (op->type == OFOPERATION_DELETE) {
3530 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
3532 list_remove(&op->group_node);
3536 if (list_is_empty(&group->ops) && !list_is_empty(&group->ofproto_node)) {
3537 ofopgroup_destroy(group);
3541 /* Indicates that 'op' completed with status 'error', which is either 0 to
3542 * indicate success or an OpenFlow error code on failure.
3544 * If 'error' is 0, indicating success, the operation will be committed
3545 * permanently to the flow table. There is one interesting subcase:
3547 * - If 'op' is an "add flow" operation that is replacing an existing rule in
3548 * the flow table (the "victim" rule) by a new one, then the caller must
3549 * have uninitialized any derived state in the victim rule, as in step 5 in
3550 * the "Life Cycle" in ofproto/ofproto-provider.h. ofoperation_complete()
3551 * performs steps 6 and 7 for the victim rule, most notably by calling its
3552 * ->rule_dealloc() function.
3554 * If 'error' is nonzero, then generally the operation will be rolled back:
3556 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
3557 * restores the original rule. The caller must have uninitialized any
3558 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
3559 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
3560 * and 7 for the new rule, calling its ->rule_dealloc() function.
3562 * - If 'op' is a "modify flow" operation, ofproto restores the original
3565 * - 'op' must not be a "delete flow" operation. Removing a rule is not
3566 * allowed to fail. It must always succeed.
3568 * Please see the large comment in ofproto/ofproto-provider.h titled
3569 * "Asynchronous Operation Support" for more information. */
3571 ofoperation_complete(struct ofoperation *op, enum ofperr error)
3573 struct ofopgroup *group = op->group;
3574 struct rule *rule = op->rule;
3575 struct ofproto *ofproto = rule->ofproto;
3577 assert(rule->pending == op);
3578 assert(op->status < 0);
3582 && op->type != OFOPERATION_DELETE
3584 && group->buffer_id != UINT32_MAX
3585 && list_is_singleton(&op->group_node)) {
3586 struct ofpbuf *packet;
3589 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
3593 error = rule_execute(rule, in_port, packet);
3596 if (!group->error) {
3597 group->error = error;
3601 case OFOPERATION_ADD:
3603 ofproto_rule_destroy__(op->victim);
3604 if ((rule->cr.wc.vlan_tci_mask & htons(VLAN_VID_MASK))
3605 == htons(VLAN_VID_MASK)) {
3606 if (ofproto->vlan_bitmap) {
3607 uint16_t vid = vlan_tci_to_vid(rule->cr.flow.vlan_tci);
3609 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
3610 bitmap_set1(ofproto->vlan_bitmap, vid);
3611 ofproto->vlans_changed = true;
3614 ofproto->vlans_changed = true;
3618 oftable_substitute_rule(rule, op->victim);
3619 ofproto_rule_destroy__(rule);
3624 case OFOPERATION_DELETE:
3626 ofproto_rule_destroy__(rule);
3630 case OFOPERATION_MODIFY:
3632 rule->modified = time_msec();
3634 free(rule->actions);
3635 rule->actions = op->actions;
3636 rule->n_actions = op->n_actions;
3644 ofoperation_destroy(op);
3648 ofoperation_get_victim(struct ofoperation *op)
3650 assert(op->type == OFOPERATION_ADD);
3655 pick_datapath_id(const struct ofproto *ofproto)
3657 const struct ofport *port;
3659 port = ofproto_get_port(ofproto, OFPP_LOCAL);
3661 uint8_t ea[ETH_ADDR_LEN];
3664 error = netdev_get_etheraddr(port->netdev, ea);
3666 return eth_addr_to_uint64(ea);
3668 VLOG_WARN("could not get MAC address for %s (%s)",
3669 netdev_get_name(port->netdev), strerror(error));
3671 return ofproto->fallback_dpid;
3675 pick_fallback_dpid(void)
3677 uint8_t ea[ETH_ADDR_LEN];
3678 eth_addr_nicira_random(ea);
3679 return eth_addr_to_uint64(ea);
3682 /* Table overflow policy. */
3684 /* Chooses and returns a rule to evict from 'table'. Returns NULL if the table
3685 * is not configured to evict rules or if the table contains no evictable
3686 * rules. (Rules with 'evictable' set to false or with no timeouts are not
3688 static struct rule *
3689 choose_rule_to_evict(struct oftable *table)
3691 struct eviction_group *evg;
3693 if (!table->eviction_fields) {
3697 /* In the common case, the outer and inner loops here will each be entered
3700 * - The inner loop normally "return"s in its first iteration. If the
3701 * eviction group has any evictable rules, then it always returns in
3704 * - The outer loop only iterates more than once if the largest eviction
3705 * group has no evictable rules.
3707 * - The outer loop can exit only if table's 'max_flows' is all filled up
3708 * by unevictable rules'. */
3709 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
3712 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
3713 if (rule->evictable) {
3722 /* Searches 'ofproto' for tables that have more flows than their configured
3723 * maximum and that have flow eviction enabled, and evicts as many flows as
3724 * necessary and currently feasible from them.
3726 * This triggers only when an OpenFlow table has N flows in it and then the
3727 * client configures a maximum number of flows less than N. */
3729 ofproto_evict(struct ofproto *ofproto)
3731 struct ofopgroup *group;
3732 struct oftable *table;
3734 group = ofopgroup_create_unattached(ofproto);
3735 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
3736 while (classifier_count(&table->cls) > table->max_flows
3737 && table->eviction_fields) {
3740 rule = choose_rule_to_evict(table);
3741 if (!rule || rule->pending) {
3745 ofoperation_create(group, rule, OFOPERATION_DELETE);
3746 oftable_remove_rule(rule);
3747 ofproto->ofproto_class->rule_destruct(rule);
3750 ofopgroup_submit(group);
3753 /* Eviction groups. */
3755 /* Returns the priority to use for an eviction_group that contains 'n_rules'
3756 * rules. The priority contains low-order random bits to ensure that eviction
3757 * groups with the same number of rules are prioritized randomly. */
3759 eviction_group_priority(size_t n_rules)
3761 uint16_t size = MIN(UINT16_MAX, n_rules);
3762 return (size << 16) | random_uint16();
3765 /* Updates 'evg', an eviction_group within 'table', following a change that
3766 * adds or removes rules in 'evg'. */
3768 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
3770 heap_change(&table->eviction_groups_by_size, &evg->size_node,
3771 eviction_group_priority(heap_count(&evg->rules)));
3774 /* Destroys 'evg', an eviction_group within 'table':
3776 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
3777 * rules themselves, just removes them from the eviction group.)
3779 * - Removes 'evg' from 'table'.
3783 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
3785 while (!heap_is_empty(&evg->rules)) {
3788 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
3789 rule->eviction_group = NULL;
3791 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
3792 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
3793 heap_destroy(&evg->rules);
3797 /* Removes 'rule' from its eviction group, if any. */
3799 eviction_group_remove_rule(struct rule *rule)
3801 if (rule->eviction_group) {
3802 struct oftable *table = &rule->ofproto->tables[rule->table_id];
3803 struct eviction_group *evg = rule->eviction_group;
3805 rule->eviction_group = NULL;
3806 heap_remove(&evg->rules, &rule->evg_node);
3807 if (heap_is_empty(&evg->rules)) {
3808 eviction_group_destroy(table, evg);
3810 eviction_group_resized(table, evg);
3815 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
3816 * returns the hash value. */
3818 eviction_group_hash_rule(struct rule *rule)
3820 struct oftable *table = &rule->ofproto->tables[rule->table_id];
3821 const struct mf_subfield *sf;
3824 hash = table->eviction_group_id_basis;
3825 for (sf = table->eviction_fields;
3826 sf < &table->eviction_fields[table->n_eviction_fields];
3829 if (mf_are_prereqs_ok(sf->field, &rule->cr.flow)) {
3830 union mf_value value;
3832 mf_get_value(sf->field, &rule->cr.flow, &value);
3834 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
3836 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
3837 unsigned int start = sf->ofs + sf->n_bits;
3838 bitwise_zero(&value, sf->field->n_bytes, start,
3839 sf->field->n_bytes * 8 - start);
3841 hash = hash_bytes(&value, sf->field->n_bytes, hash);
3843 hash = hash_int(hash, 0);
3850 /* Returns an eviction group within 'table' with the given 'id', creating one
3852 static struct eviction_group *
3853 eviction_group_find(struct oftable *table, uint32_t id)
3855 struct eviction_group *evg;
3857 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
3861 evg = xmalloc(sizeof *evg);
3862 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
3863 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
3864 eviction_group_priority(0));
3865 heap_init(&evg->rules);
3870 /* Returns an eviction priority for 'rule'. The return value should be
3871 * interpreted so that higher priorities make a rule more attractive candidates
3874 rule_eviction_priority(struct rule *rule)
3876 long long int hard_expiration;
3877 long long int idle_expiration;
3878 long long int expiration;
3879 uint32_t expiration_offset;
3881 /* Calculate time of expiration. */
3882 hard_expiration = (rule->hard_timeout
3883 ? rule->modified + rule->hard_timeout * 1000
3885 idle_expiration = (rule->idle_timeout
3886 ? rule->used + rule->idle_timeout * 1000
3888 expiration = MIN(hard_expiration, idle_expiration);
3889 if (expiration == LLONG_MAX) {
3893 /* Calculate the time of expiration as a number of (approximate) seconds
3894 * after program startup.
3896 * This should work OK for program runs that last UINT32_MAX seconds or
3897 * less. Therefore, please restart OVS at least once every 136 years. */
3898 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
3900 /* Invert the expiration offset because we're using a max-heap. */
3901 return UINT32_MAX - expiration_offset;
3904 /* Adds 'rule' to an appropriate eviction group for its oftable's
3905 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
3906 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
3909 * The caller must ensure that 'rule' is not already in an eviction group. */
3911 eviction_group_add_rule(struct rule *rule)
3913 struct ofproto *ofproto = rule->ofproto;
3914 struct oftable *table = &ofproto->tables[rule->table_id];
3916 if (table->eviction_fields
3917 && (rule->hard_timeout || rule->idle_timeout)) {
3918 struct eviction_group *evg;
3920 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
3922 rule->eviction_group = evg;
3923 heap_insert(&evg->rules, &rule->evg_node,
3924 rule_eviction_priority(rule));
3925 eviction_group_resized(table, evg);
3931 /* Initializes 'table'. */
3933 oftable_init(struct oftable *table)
3935 memset(table, 0, sizeof *table);
3936 classifier_init(&table->cls);
3937 table->max_flows = UINT_MAX;
3940 /* Destroys 'table', including its classifier and eviction groups.
3942 * The caller is responsible for freeing 'table' itself. */
3944 oftable_destroy(struct oftable *table)
3946 assert(classifier_is_empty(&table->cls));
3947 oftable_disable_eviction(table);
3948 classifier_destroy(&table->cls);
3952 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
3953 * string, then 'table' will use its default name.
3955 * This only affects the name exposed for a table exposed through the OpenFlow
3956 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
3958 oftable_set_name(struct oftable *table, const char *name)
3960 if (name && name[0]) {
3961 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
3962 if (!table->name || strncmp(name, table->name, len)) {
3964 table->name = xmemdup0(name, len);
3972 /* oftables support a choice of two policies when adding a rule would cause the
3973 * number of flows in the table to exceed the configured maximum number: either
3974 * they can refuse to add the new flow or they can evict some existing flow.
3975 * This function configures the former policy on 'table'. */
3977 oftable_disable_eviction(struct oftable *table)
3979 if (table->eviction_fields) {
3980 struct eviction_group *evg, *next;
3982 HMAP_FOR_EACH_SAFE (evg, next, id_node,
3983 &table->eviction_groups_by_id) {
3984 eviction_group_destroy(table, evg);
3986 hmap_destroy(&table->eviction_groups_by_id);
3987 heap_destroy(&table->eviction_groups_by_size);
3989 free(table->eviction_fields);
3990 table->eviction_fields = NULL;
3991 table->n_eviction_fields = 0;
3995 /* oftables support a choice of two policies when adding a rule would cause the
3996 * number of flows in the table to exceed the configured maximum number: either
3997 * they can refuse to add the new flow or they can evict some existing flow.
3998 * This function configures the latter policy on 'table', with fairness based
3999 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
4000 * 'n_fields' as 0 disables fairness.) */
4002 oftable_enable_eviction(struct oftable *table,
4003 const struct mf_subfield *fields, size_t n_fields)
4005 struct cls_cursor cursor;
4008 if (table->eviction_fields
4009 && n_fields == table->n_eviction_fields
4011 || !memcmp(fields, table->eviction_fields,
4012 n_fields * sizeof *fields))) {
4017 oftable_disable_eviction(table);
4019 table->n_eviction_fields = n_fields;
4020 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
4022 table->eviction_group_id_basis = random_uint32();
4023 hmap_init(&table->eviction_groups_by_id);
4024 heap_init(&table->eviction_groups_by_size);
4026 cls_cursor_init(&cursor, &table->cls, NULL);
4027 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4028 eviction_group_add_rule(rule);
4032 /* Removes 'rule' from the oftable that contains it. */
4034 oftable_remove_rule(struct rule *rule)
4036 struct ofproto *ofproto = rule->ofproto;
4037 struct oftable *table = &ofproto->tables[rule->table_id];
4039 classifier_remove(&table->cls, &rule->cr);
4040 eviction_group_remove_rule(rule);
4043 /* Inserts 'rule' into its oftable. Removes any existing rule from 'rule''s
4044 * oftable that has an identical cls_rule. Returns the rule that was removed,
4045 * if any, and otherwise NULL. */
4046 static struct rule *
4047 oftable_replace_rule(struct rule *rule)
4049 struct ofproto *ofproto = rule->ofproto;
4050 struct oftable *table = &ofproto->tables[rule->table_id];
4051 struct rule *victim;
4053 victim = rule_from_cls_rule(classifier_replace(&table->cls, &rule->cr));
4055 eviction_group_remove_rule(victim);
4057 eviction_group_add_rule(rule);
4061 /* Removes 'old' from its oftable then, if 'new' is nonnull, inserts 'new'. */
4063 oftable_substitute_rule(struct rule *old, struct rule *new)
4066 oftable_replace_rule(new);
4068 oftable_remove_rule(old);
4072 /* unixctl commands. */
4075 ofproto_lookup(const char *name)
4077 struct ofproto *ofproto;
4079 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
4081 if (!strcmp(ofproto->name, name)) {
4089 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
4090 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4092 struct ofproto *ofproto;
4096 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
4097 ds_put_format(&results, "%s\n", ofproto->name);
4099 unixctl_command_reply(conn, ds_cstr(&results));
4100 ds_destroy(&results);
4104 ofproto_unixctl_init(void)
4106 static bool registered;
4112 unixctl_command_register("ofproto/list", "", 0, 0,
4113 ofproto_unixctl_list, NULL);
4116 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4118 * This is deprecated. It is only for compatibility with broken device drivers
4119 * in old versions of Linux that do not properly support VLANs when VLAN
4120 * devices are not used. When broken device drivers are no longer in
4121 * widespread use, we will delete these interfaces. */
4123 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
4124 * (exactly) by an OpenFlow rule in 'ofproto'. */
4126 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
4128 const struct oftable *oftable;
4130 free(ofproto->vlan_bitmap);
4131 ofproto->vlan_bitmap = bitmap_allocate(4096);
4132 ofproto->vlans_changed = false;
4134 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
4135 const struct cls_table *table;
4137 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.tables) {
4138 if ((table->wc.vlan_tci_mask & htons(VLAN_VID_MASK))
4139 == htons(VLAN_VID_MASK)) {
4140 const struct cls_rule *rule;
4142 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
4143 uint16_t vid = vlan_tci_to_vid(rule->flow.vlan_tci);
4144 bitmap_set1(vlan_bitmap, vid);
4145 bitmap_set1(ofproto->vlan_bitmap, vid);
4152 /* Returns true if new VLANs have come into use by the flow table since the
4153 * last call to ofproto_get_vlan_usage().
4155 * We don't track when old VLANs stop being used. */
4157 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
4159 return ofproto->vlans_changed;
4162 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
4163 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
4164 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
4165 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
4166 * then the VLAN device is un-enslaved. */
4168 ofproto_port_set_realdev(struct ofproto *ofproto, uint16_t vlandev_ofp_port,
4169 uint16_t realdev_ofp_port, int vid)
4171 struct ofport *ofport;
4174 assert(vlandev_ofp_port != realdev_ofp_port);
4176 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
4178 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
4179 ofproto->name, vlandev_ofp_port);
4183 if (!ofproto->ofproto_class->set_realdev) {
4184 if (!vlandev_ofp_port) {
4187 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
4191 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
4193 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
4194 ofproto->name, vlandev_ofp_port,
4195 netdev_get_name(ofport->netdev), strerror(error));