2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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.
22 #include <sys/socket.h>
24 #include <netinet/in.h>
30 #include "byte-order.h"
32 #include "classifier.h"
35 #include "dynamic-string.h"
36 #include "fail-open.h"
42 #include "mac-learning.h"
43 #include "multipath.h"
49 #include "ofp-print.h"
51 #include "ofproto-sflow.h"
53 #include "openflow/nicira-ext.h"
54 #include "openflow/openflow.h"
55 #include "openvswitch/datapath-protocol.h"
59 #include "poll-loop.h"
64 #include "stream-ssl.h"
68 #include "unaligned.h"
71 #include "vlan-bitmap.h"
74 VLOG_DEFINE_THIS_MODULE(ofproto);
76 COVERAGE_DEFINE(odp_overflow);
77 COVERAGE_DEFINE(ofproto_agg_request);
78 COVERAGE_DEFINE(ofproto_costly_flags);
79 COVERAGE_DEFINE(ofproto_ctlr_action);
80 COVERAGE_DEFINE(ofproto_error);
81 COVERAGE_DEFINE(ofproto_expiration);
82 COVERAGE_DEFINE(ofproto_expired);
83 COVERAGE_DEFINE(ofproto_flows_req);
84 COVERAGE_DEFINE(ofproto_flush);
85 COVERAGE_DEFINE(ofproto_invalidated);
86 COVERAGE_DEFINE(ofproto_no_packet_in);
87 COVERAGE_DEFINE(ofproto_ofp2odp);
88 COVERAGE_DEFINE(ofproto_packet_in);
89 COVERAGE_DEFINE(ofproto_packet_out);
90 COVERAGE_DEFINE(ofproto_queue_req);
91 COVERAGE_DEFINE(ofproto_recv_openflow);
92 COVERAGE_DEFINE(ofproto_reinit_ports);
93 COVERAGE_DEFINE(ofproto_unexpected_rule);
94 COVERAGE_DEFINE(ofproto_uninstallable);
95 COVERAGE_DEFINE(ofproto_update_port);
97 static void ofport_destroy__(struct ofport *);
98 static void ofport_destroy(struct ofport *);
100 static int rule_create(struct ofproto *, const struct cls_rule *,
101 const union ofp_action *, size_t n_actions,
102 uint16_t idle_timeout, uint16_t hard_timeout,
103 ovs_be64 flow_cookie, bool send_flow_removed,
104 struct rule **rulep);
106 static uint64_t pick_datapath_id(const struct ofproto *);
107 static uint64_t pick_fallback_dpid(void);
109 static void ofproto_destroy__(struct ofproto *);
110 static void ofproto_flush_flows__(struct ofproto *);
112 static void ofproto_rule_destroy__(struct rule *);
113 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
114 static void ofproto_rule_remove(struct rule *);
116 static void handle_openflow(struct ofconn *, struct ofpbuf *);
118 static void update_port(struct ofproto *, const char *devname);
119 static int init_ports(struct ofproto *);
120 static void reinit_ports(struct ofproto *);
122 static void ofproto_unixctl_init(void);
124 /* All registered ofproto classes, in probe order. */
125 static const struct ofproto_class **ofproto_classes;
126 static size_t n_ofproto_classes;
127 static size_t allocated_ofproto_classes;
129 /* Map from dpif name to struct ofproto, for use by unixctl commands. */
130 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
132 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
135 ofproto_initialize(void)
141 ofproto_class_register(&ofproto_dpif_class);
145 /* 'type' should be a normalized datapath type, as returned by
146 * ofproto_normalize_type(). Returns the corresponding ofproto_class
147 * structure, or a null pointer if there is none registered for 'type'. */
148 static const struct ofproto_class *
149 ofproto_class_find__(const char *type)
153 ofproto_initialize();
154 for (i = 0; i < n_ofproto_classes; i++) {
155 const struct ofproto_class *class = ofproto_classes[i];
160 class->enumerate_types(&types);
161 found = sset_contains(&types, type);
162 sset_destroy(&types);
168 VLOG_WARN("unknown datapath type %s", type);
172 /* Registers a new ofproto class. After successful registration, new ofprotos
173 * of that type can be created using ofproto_create(). */
175 ofproto_class_register(const struct ofproto_class *new_class)
179 for (i = 0; i < n_ofproto_classes; i++) {
180 if (ofproto_classes[i] == new_class) {
185 if (n_ofproto_classes >= allocated_ofproto_classes) {
186 ofproto_classes = x2nrealloc(ofproto_classes,
187 &allocated_ofproto_classes,
188 sizeof *ofproto_classes);
190 ofproto_classes[n_ofproto_classes++] = new_class;
194 /* Unregisters a datapath provider. 'type' must have been previously
195 * registered and not currently be in use by any ofprotos. After
196 * unregistration new datapaths of that type cannot be opened using
197 * ofproto_create(). */
199 ofproto_class_unregister(const struct ofproto_class *class)
203 for (i = 0; i < n_ofproto_classes; i++) {
204 if (ofproto_classes[i] == class) {
205 for (i++; i < n_ofproto_classes; i++) {
206 ofproto_classes[i - 1] = ofproto_classes[i];
212 VLOG_WARN("attempted to unregister an ofproto class that is not "
217 /* Clears 'types' and enumerates all registered ofproto types into it. The
218 * caller must first initialize the sset. */
220 ofproto_enumerate_types(struct sset *types)
224 ofproto_initialize();
225 for (i = 0; i < n_ofproto_classes; i++) {
226 ofproto_classes[i]->enumerate_types(types);
230 /* Returns the fully spelled out name for the given ofproto 'type'.
232 * Normalized type string can be compared with strcmp(). Unnormalized type
233 * string might be the same even if they have different spellings. */
235 ofproto_normalize_type(const char *type)
237 return type && type[0] ? type : "system";
240 /* Clears 'names' and enumerates the names of all known created ofprotos with
241 * the given 'type'. The caller must first initialize the sset. Returns 0 if
242 * successful, otherwise a positive errno value.
244 * Some kinds of datapaths might not be practically enumerable. This is not
245 * considered an error. */
247 ofproto_enumerate_names(const char *type, struct sset *names)
249 const struct ofproto_class *class = ofproto_class_find__(type);
250 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
254 ofproto_create(const char *datapath_name, const char *datapath_type,
255 struct ofproto **ofprotop)
257 const struct ofproto_class *class;
258 struct ofproto *ofproto;
263 ofproto_initialize();
264 ofproto_unixctl_init();
266 datapath_type = ofproto_normalize_type(datapath_type);
267 class = ofproto_class_find__(datapath_type);
269 VLOG_WARN("could not create datapath %s of unknown type %s",
270 datapath_name, datapath_type);
274 ofproto = class->alloc();
276 VLOG_ERR("failed to allocate datapath %s of type %s",
277 datapath_name, datapath_type);
282 memset(ofproto, 0, sizeof *ofproto);
283 ofproto->ofproto_class = class;
284 ofproto->name = xstrdup(datapath_name);
285 ofproto->type = xstrdup(datapath_type);
286 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
287 hash_string(ofproto->name, 0));
288 ofproto->datapath_id = 0;
289 ofproto->fallback_dpid = pick_fallback_dpid();
290 ofproto->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
291 ofproto->hw_desc = xstrdup(DEFAULT_HW_DESC);
292 ofproto->sw_desc = xstrdup(DEFAULT_SW_DESC);
293 ofproto->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
294 ofproto->dp_desc = xstrdup(DEFAULT_DP_DESC);
295 ofproto->netdev_monitor = netdev_monitor_create();
296 hmap_init(&ofproto->ports);
297 shash_init(&ofproto->port_by_name);
298 classifier_init(&ofproto->cls);
299 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
301 error = ofproto->ofproto_class->construct(ofproto);
303 VLOG_ERR("failed to open datapath %s: %s",
304 datapath_name, strerror(error));
305 ofproto_destroy__(ofproto);
309 ofproto->datapath_id = pick_datapath_id(ofproto);
310 VLOG_INFO("using datapath ID %016"PRIx64, ofproto->datapath_id);
318 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
320 uint64_t old_dpid = p->datapath_id;
321 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
322 if (p->datapath_id != old_dpid) {
323 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
325 /* Force all active connections to reconnect, since there is no way to
326 * notify a controller that the datapath ID has changed. */
327 ofproto_reconnect_controllers(p);
332 ofproto_set_controllers(struct ofproto *p,
333 const struct ofproto_controller *controllers,
334 size_t n_controllers)
336 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
340 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
342 connmgr_set_fail_mode(p->connmgr, fail_mode);
345 /* Drops the connections between 'ofproto' and all of its controllers, forcing
346 * them to reconnect. */
348 ofproto_reconnect_controllers(struct ofproto *ofproto)
350 connmgr_reconnect(ofproto->connmgr);
353 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
354 * in-band control should guarantee access, in the same way that in-band
355 * control guarantees access to OpenFlow controllers. */
357 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
358 const struct sockaddr_in *extras, size_t n)
360 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
363 /* Sets the OpenFlow queue used by flows set up by in-band control on
364 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
365 * flows will use the default queue. */
367 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
369 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
373 ofproto_set_desc(struct ofproto *p,
374 const char *mfr_desc, const char *hw_desc,
375 const char *sw_desc, const char *serial_desc,
378 struct ofp_desc_stats *ods;
381 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
382 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
383 sizeof ods->mfr_desc);
386 p->mfr_desc = xstrdup(mfr_desc);
389 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
390 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
391 sizeof ods->hw_desc);
394 p->hw_desc = xstrdup(hw_desc);
397 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
398 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
399 sizeof ods->sw_desc);
402 p->sw_desc = xstrdup(sw_desc);
405 if (strlen(serial_desc) >= sizeof ods->serial_num) {
406 VLOG_WARN("truncating serial_desc, must be less than %zu "
408 sizeof ods->serial_num);
410 free(p->serial_desc);
411 p->serial_desc = xstrdup(serial_desc);
414 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
415 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
416 sizeof ods->dp_desc);
419 p->dp_desc = xstrdup(dp_desc);
424 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
426 return connmgr_set_snoops(ofproto->connmgr, snoops);
430 ofproto_set_netflow(struct ofproto *ofproto,
431 const struct netflow_options *nf_options)
433 if (nf_options && sset_is_empty(&nf_options->collectors)) {
437 if (ofproto->ofproto_class->set_netflow) {
438 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
440 return nf_options ? EOPNOTSUPP : 0;
445 ofproto_set_sflow(struct ofproto *ofproto,
446 const struct ofproto_sflow_options *oso)
448 if (oso && sset_is_empty(&oso->targets)) {
452 if (ofproto->ofproto_class->set_sflow) {
453 return ofproto->ofproto_class->set_sflow(ofproto, oso);
455 return oso ? EOPNOTSUPP : 0;
459 /* Connectivity Fault Management configuration. */
461 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
463 ofproto_port_clear_cfm(struct ofproto *ofproto, uint16_t ofp_port)
465 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
466 if (ofport && ofproto->ofproto_class->set_cfm) {
467 ofproto->ofproto_class->set_cfm(ofport, NULL, NULL, 0);
471 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
472 * basic configuration from the configuration members in 'cfm', and the set of
473 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
474 * Ignores the statistics members of 'cfm'.
476 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
478 ofproto_port_set_cfm(struct ofproto *ofproto, uint16_t ofp_port,
479 const struct cfm *cfm,
480 const uint16_t *remote_mps, size_t n_remote_mps)
482 struct ofport *ofport;
485 ofport = ofproto_get_port(ofproto, ofp_port);
487 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
488 ofproto->name, ofp_port);
492 error = (ofproto->ofproto_class->set_cfm
493 ? ofproto->ofproto_class->set_cfm(ofport, cfm,
494 remote_mps, n_remote_mps)
497 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
498 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
503 /* Returns the connectivity fault management object associated with 'ofp_port'
504 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
505 * 'ofp_port' or if that port does not have CFM configured. The caller must
506 * not modify or destroy the returned object. */
508 ofproto_port_get_cfm(struct ofproto *ofproto, uint16_t ofp_port)
510 struct ofport *ofport;
511 const struct cfm *cfm;
513 ofport = ofproto_get_port(ofproto, ofp_port);
515 && ofproto->ofproto_class->get_cfm
516 && !ofproto->ofproto_class->get_cfm(ofport, &cfm)) ? cfm : NULL;
519 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
520 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
521 * 0 if LACP partner information is not current (generally indicating a
522 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
524 ofproto_port_is_lacp_current(struct ofproto *ofproto, uint16_t ofp_port)
526 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
527 return (ofport && ofproto->ofproto_class->port_is_lacp_current
528 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
534 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
535 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
536 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
537 * configuration plus, if there is more than one slave, a bonding
540 * If 'aux' is already registered then this function updates its configuration
541 * to 's'. Otherwise, this function registers a new bundle.
543 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
546 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
547 const struct ofproto_bundle_settings *s)
549 return (ofproto->ofproto_class->bundle_set
550 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
554 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
555 * If no such bundle has been registered, this has no effect. */
557 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
559 return ofproto_bundle_register(ofproto, aux, NULL);
563 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
564 * If 'aux' is already registered then this function updates its configuration
565 * to 's'. Otherwise, this function registers a new mirror.
567 * Mirrors affect only the treatment of packets output to the OFPP_NORMAL
570 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
571 const struct ofproto_mirror_settings *s)
573 return (ofproto->ofproto_class->mirror_set
574 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
578 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
579 * If no mirror has been registered, this has no effect. */
581 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
583 return ofproto_mirror_register(ofproto, aux, NULL);
586 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
587 * which all packets are flooded, instead of using MAC learning. If
588 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
590 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
593 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
595 return (ofproto->ofproto_class->set_flood_vlans
596 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
600 /* Returns true if 'aux' is a registered bundle that is currently in use as the
601 * output for a mirror. */
603 ofproto_is_mirror_output_bundle(struct ofproto *ofproto, void *aux)
605 return (ofproto->ofproto_class->is_mirror_output_bundle
606 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
611 ofproto_has_snoops(const struct ofproto *ofproto)
613 return connmgr_has_snoops(ofproto->connmgr);
617 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
619 connmgr_get_snoops(ofproto->connmgr, snoops);
623 ofproto_destroy__(struct ofproto *ofproto)
625 connmgr_destroy(ofproto->connmgr);
627 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
629 free(ofproto->mfr_desc);
630 free(ofproto->hw_desc);
631 free(ofproto->sw_desc);
632 free(ofproto->serial_desc);
633 free(ofproto->dp_desc);
634 netdev_monitor_destroy(ofproto->netdev_monitor);
635 hmap_destroy(&ofproto->ports);
636 shash_destroy(&ofproto->port_by_name);
637 classifier_destroy(&ofproto->cls);
639 ofproto->ofproto_class->dealloc(ofproto);
643 ofproto_destroy(struct ofproto *p)
645 struct ofport *ofport, *next_ofport;
651 ofproto_flush_flows__(p);
652 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
653 hmap_remove(&p->ports, &ofport->hmap_node);
654 ofport_destroy(ofport);
657 p->ofproto_class->destruct(p);
658 ofproto_destroy__(p);
661 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
662 * kernel datapath, for example, this destroys the datapath in the kernel, and
663 * with the netdev-based datapath, it tears down the data structures that
664 * represent the datapath.
666 * The datapath should not be currently open as an ofproto. */
668 ofproto_delete(const char *name, const char *type)
670 const struct ofproto_class *class = ofproto_class_find__(type);
671 return (!class ? EAFNOSUPPORT
672 : !class->del ? EACCES
673 : class->del(type, name));
677 process_port_change(struct ofproto *ofproto, int error, char *devname)
679 if (error == ENOBUFS) {
680 reinit_ports(ofproto);
682 update_port(ofproto, devname);
688 ofproto_run(struct ofproto *p)
693 error = p->ofproto_class->run(p);
694 if (error == ENODEV) {
695 /* Someone destroyed the datapath behind our back. The caller
696 * better destroy us and give up, because we're just going to
697 * spin from here on out. */
698 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
699 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
704 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
705 process_port_change(p, error, devname);
707 while ((error = netdev_monitor_poll(p->netdev_monitor,
708 &devname)) != EAGAIN) {
709 process_port_change(p, error, devname);
712 connmgr_run(p->connmgr, handle_openflow);
718 ofproto_wait(struct ofproto *p)
720 p->ofproto_class->wait(p);
721 p->ofproto_class->port_poll_wait(p);
722 netdev_monitor_poll_wait(p->netdev_monitor);
723 connmgr_wait(p->connmgr);
727 ofproto_is_alive(const struct ofproto *p)
729 return connmgr_has_controllers(p->connmgr);
733 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
736 connmgr_get_controller_info(ofproto->connmgr, info);
740 ofproto_free_ofproto_controller_info(struct shash *info)
742 struct shash_node *node;
744 SHASH_FOR_EACH (node, info) {
745 struct ofproto_controller_info *cinfo = node->data;
746 while (cinfo->pairs.n) {
747 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
754 /* Makes a deep copy of 'old' into 'port'. */
756 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
758 port->name = xstrdup(old->name);
759 port->type = xstrdup(old->type);
760 port->ofp_port = old->ofp_port;
763 /* Frees memory allocated to members of 'ofproto_port'.
765 * Do not call this function on an ofproto_port obtained from
766 * ofproto_port_dump_next(): that function retains ownership of the data in the
769 ofproto_port_destroy(struct ofproto_port *ofproto_port)
771 free(ofproto_port->name);
772 free(ofproto_port->type);
775 /* Initializes 'dump' to begin dumping the ports in an ofproto.
777 * This function provides no status indication. An error status for the entire
778 * dump operation is provided when it is completed by calling
779 * ofproto_port_dump_done().
782 ofproto_port_dump_start(struct ofproto_port_dump *dump,
783 const struct ofproto *ofproto)
785 dump->ofproto = ofproto;
786 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
790 /* Attempts to retrieve another port from 'dump', which must have been created
791 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
792 * 'port' and returns true. On failure, returns false.
794 * Failure might indicate an actual error or merely that the last port has been
795 * dumped. An error status for the entire dump operation is provided when it
796 * is completed by calling ofproto_port_dump_done().
798 * The ofproto owns the data stored in 'port'. It will remain valid until at
799 * least the next time 'dump' is passed to ofproto_port_dump_next() or
800 * ofproto_port_dump_done(). */
802 ofproto_port_dump_next(struct ofproto_port_dump *dump,
803 struct ofproto_port *port)
805 const struct ofproto *ofproto = dump->ofproto;
811 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
814 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
820 /* Completes port table dump operation 'dump', which must have been created
821 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
822 * error-free, otherwise a positive errno value describing the problem. */
824 ofproto_port_dump_done(struct ofproto_port_dump *dump)
826 const struct ofproto *ofproto = dump->ofproto;
828 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
831 return dump->error == EOF ? 0 : dump->error;
834 /* Attempts to add 'netdev' as a port on 'ofproto'. If successful, returns 0
835 * and sets '*ofp_portp' to the new port's OpenFlow port number (if 'ofp_portp'
836 * is non-null). On failure, returns a positive errno value and sets
837 * '*ofp_portp' to OFPP_NONE (if 'ofp_portp' is non-null). */
839 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
845 error = ofproto->ofproto_class->port_add(ofproto, netdev, &ofp_port);
847 update_port(ofproto, netdev_get_name(netdev));
850 *ofp_portp = error ? OFPP_NONE : ofp_port;
855 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
856 * initializes '*port' appropriately; on failure, returns a positive errno
859 * The caller owns the data in 'ofproto_port' and must free it with
860 * ofproto_port_destroy() when it is no longer needed. */
862 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
863 struct ofproto_port *port)
867 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
869 memset(port, 0, sizeof *port);
874 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
875 * Returns 0 if successful, otherwise a positive errno. */
877 ofproto_port_del(struct ofproto *ofproto, uint16_t ofp_port)
879 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
880 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
883 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
884 if (!error && ofport) {
885 /* 'name' is the netdev's name and update_port() is going to close the
886 * netdev. Just in case update_port() refers to 'name' after it
887 * destroys 'ofport', make a copy of it around the update_port()
889 char *devname = xstrdup(name);
890 update_port(ofproto, devname);
896 /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
897 * performs the 'n_actions' actions in 'actions'. The new flow will not
900 * If cls_rule->priority is in the range of priorities supported by OpenFlow
901 * (0...65535, inclusive) then the flow will be visible to OpenFlow
902 * controllers; otherwise, it will be hidden.
904 * The caller retains ownership of 'cls_rule' and 'actions'. */
906 ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
907 const union ofp_action *actions, size_t n_actions)
910 rule_create(p, cls_rule, actions, n_actions, 0, 0, 0, false, &rule);
914 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
918 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
921 ofproto_rule_remove(rule);
926 ofproto_flush_flows__(struct ofproto *ofproto)
928 struct rule *rule, *next_rule;
929 struct cls_cursor cursor;
931 COVERAGE_INC(ofproto_flush);
933 if (ofproto->ofproto_class->flush) {
934 ofproto->ofproto_class->flush(ofproto);
937 cls_cursor_init(&cursor, &ofproto->cls, NULL);
938 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
939 ofproto_rule_remove(rule);
944 ofproto_flush_flows(struct ofproto *ofproto)
946 ofproto_flush_flows__(ofproto);
947 connmgr_flushed(ofproto->connmgr);
951 reinit_ports(struct ofproto *p)
953 struct ofproto_port_dump dump;
954 struct sset devnames;
955 struct ofport *ofport;
956 struct ofproto_port ofproto_port;
959 COVERAGE_INC(ofproto_reinit_ports);
961 sset_init(&devnames);
962 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
963 sset_add(&devnames, netdev_get_name(ofport->netdev));
965 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
966 sset_add(&devnames, ofproto_port.name);
969 SSET_FOR_EACH (devname, &devnames) {
970 update_port(p, devname);
972 sset_destroy(&devnames);
975 /* Opens and returns a netdev for 'ofproto_port', or a null pointer if the
976 * netdev cannot be opened. On success, also fills in 'opp'. */
977 static struct netdev *
978 ofport_open(const struct ofproto_port *ofproto_port, struct ofp_phy_port *opp)
980 uint32_t curr, advertised, supported, peer;
981 struct netdev_options netdev_options;
982 enum netdev_flags flags;
983 struct netdev *netdev;
986 memset(&netdev_options, 0, sizeof netdev_options);
987 netdev_options.name = ofproto_port->name;
988 netdev_options.type = ofproto_port->type;
989 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
991 error = netdev_open(&netdev_options, &netdev);
993 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
994 "cannot be opened (%s)",
995 ofproto_port->name, ofproto_port->ofp_port,
996 ofproto_port->name, strerror(error));
1000 netdev_get_flags(netdev, &flags);
1001 netdev_get_features(netdev, &curr, &advertised, &supported, &peer);
1003 opp->port_no = htons(ofproto_port->ofp_port);
1004 netdev_get_etheraddr(netdev, opp->hw_addr);
1005 ovs_strzcpy(opp->name, ofproto_port->name, sizeof opp->name);
1006 opp->config = flags & NETDEV_UP ? 0 : htonl(OFPPC_PORT_DOWN);
1007 opp->state = netdev_get_carrier(netdev) ? 0 : htonl(OFPPS_LINK_DOWN);
1008 opp->curr = htonl(curr);
1009 opp->advertised = htonl(advertised);
1010 opp->supported = htonl(supported);
1011 opp->peer = htonl(peer);
1016 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1017 * port number, and 'config' bits other than OFPPC_PORT_DOWN are
1020 ofport_equal(const struct ofp_phy_port *a, const struct ofp_phy_port *b)
1022 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1023 return (!memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1024 && a->state == b->state
1025 && !((a->config ^ b->config) & htonl(OFPPC_PORT_DOWN))
1026 && a->curr == b->curr
1027 && a->advertised == b->advertised
1028 && a->supported == b->supported
1029 && a->peer == b->peer);
1032 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1033 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1034 * one with the same name or port number). */
1036 ofport_install(struct ofproto *p,
1037 struct netdev *netdev, const struct ofp_phy_port *opp)
1039 const char *netdev_name = netdev_get_name(netdev);
1040 struct ofport *ofport;
1043 /* Create ofport. */
1044 ofport = p->ofproto_class->port_alloc();
1049 ofport->ofproto = p;
1050 ofport->netdev = netdev;
1052 ofport->ofp_port = ntohs(opp->port_no);
1054 /* Add port to 'p'. */
1055 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1056 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->ofp_port, 0));
1057 shash_add(&p->port_by_name, netdev_name, ofport);
1059 /* Let the ofproto_class initialize its private data. */
1060 error = p->ofproto_class->port_construct(ofport);
1064 connmgr_send_port_status(p->connmgr, opp, OFPPR_ADD);
1068 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
1069 p->name, netdev_name, strerror(error));
1071 ofport_destroy__(ofport);
1073 netdev_close(netdev);
1077 /* Removes 'ofport' from 'p' and destroys it. */
1079 ofport_remove(struct ofport *ofport)
1081 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->opp,
1083 ofport_destroy(ofport);
1086 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1089 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1091 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1093 ofport_remove(port);
1097 /* Updates 'port' within 'ofproto' with the new 'netdev' and 'opp'.
1099 * Does not handle a name or port number change. The caller must implement
1100 * such a change as a delete followed by an add. */
1102 ofport_modified(struct ofport *port, struct ofp_phy_port *opp)
1104 memcpy(port->opp.hw_addr, opp->hw_addr, ETH_ADDR_LEN);
1105 port->opp.config = ((port->opp.config & ~htonl(OFPPC_PORT_DOWN))
1106 | (opp->config & htonl(OFPPC_PORT_DOWN)));
1107 port->opp.state = opp->state;
1108 port->opp.curr = opp->curr;
1109 port->opp.advertised = opp->advertised;
1110 port->opp.supported = opp->supported;
1111 port->opp.peer = opp->peer;
1113 connmgr_send_port_status(port->ofproto->connmgr, &port->opp, OFPPR_MODIFY);
1117 ofproto_port_unregister(struct ofproto *ofproto, uint16_t ofp_port)
1119 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
1121 if (port->ofproto->ofproto_class->set_cfm) {
1122 port->ofproto->ofproto_class->set_cfm(port, NULL, NULL, 0);
1124 if (port->ofproto->ofproto_class->bundle_remove) {
1125 port->ofproto->ofproto_class->bundle_remove(port);
1131 ofport_destroy__(struct ofport *port)
1133 struct ofproto *ofproto = port->ofproto;
1134 const char *name = netdev_get_name(port->netdev);
1136 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1137 hmap_remove(&ofproto->ports, &port->hmap_node);
1138 shash_delete(&ofproto->port_by_name,
1139 shash_find(&ofproto->port_by_name, name));
1141 netdev_close(port->netdev);
1142 ofproto->ofproto_class->port_dealloc(port);
1146 ofport_destroy(struct ofport *port)
1149 port->ofproto->ofproto_class->port_destruct(port);
1150 ofport_destroy__(port);
1155 ofproto_get_port(const struct ofproto *ofproto, uint16_t ofp_port)
1157 struct ofport *port;
1159 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1160 hash_int(ofp_port, 0), &ofproto->ports) {
1161 if (port->ofp_port == ofp_port) {
1169 update_port(struct ofproto *ofproto, const char *name)
1171 struct ofproto_port ofproto_port;
1172 struct ofp_phy_port opp;
1173 struct netdev *netdev;
1174 struct ofport *port;
1176 COVERAGE_INC(ofproto_update_port);
1178 /* Fetch 'name''s location and properties from the datapath. */
1179 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
1180 ? ofport_open(&ofproto_port, &opp)
1183 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
1184 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
1185 /* 'name' hasn't changed location. Any properties changed? */
1186 if (!ofport_equal(&port->opp, &opp)) {
1187 ofport_modified(port, &opp);
1190 /* Install the newly opened netdev in case it has changed. */
1191 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1192 netdev_monitor_add(ofproto->netdev_monitor, netdev);
1194 netdev_close(port->netdev);
1195 port->netdev = netdev;
1197 if (port->ofproto->ofproto_class->port_modified) {
1198 port->ofproto->ofproto_class->port_modified(port);
1201 /* If 'port' is nonnull then its name differs from 'name' and thus
1202 * we should delete it. If we think there's a port named 'name'
1203 * then its port number must be wrong now so delete it too. */
1205 ofport_remove(port);
1207 ofport_remove_with_name(ofproto, name);
1208 ofport_install(ofproto, netdev, &opp);
1211 /* Any port named 'name' is gone now. */
1212 ofport_remove_with_name(ofproto, name);
1214 ofproto_port_destroy(&ofproto_port);
1218 init_ports(struct ofproto *p)
1220 struct ofproto_port_dump dump;
1221 struct ofproto_port ofproto_port;
1223 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1224 uint16_t ofp_port = ofproto_port.ofp_port;
1225 if (ofproto_get_port(p, ofp_port)) {
1226 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1228 } else if (shash_find(&p->port_by_name, ofproto_port.name)) {
1229 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1232 struct ofp_phy_port opp;
1233 struct netdev *netdev;
1235 netdev = ofport_open(&ofproto_port, &opp);
1237 ofport_install(p, netdev, &opp);
1245 /* Creates a new rule initialized as specified, inserts it into 'ofproto''s
1246 * flow table, and stores the new rule into '*rulep'. Returns 0 on success,
1247 * otherwise a positive errno value or OpenFlow error code. */
1249 rule_create(struct ofproto *ofproto, const struct cls_rule *cls_rule,
1250 const union ofp_action *actions, size_t n_actions,
1251 uint16_t idle_timeout, uint16_t hard_timeout,
1252 ovs_be64 flow_cookie, bool send_flow_removed,
1253 struct rule **rulep)
1258 rule = ofproto->ofproto_class->rule_alloc();
1264 rule->ofproto = ofproto;
1265 rule->cr = *cls_rule;
1266 rule->flow_cookie = flow_cookie;
1267 rule->created = time_msec();
1268 rule->idle_timeout = idle_timeout;
1269 rule->hard_timeout = hard_timeout;
1270 rule->send_flow_removed = send_flow_removed;
1271 if (n_actions > 0) {
1272 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1274 rule->actions = NULL;
1276 rule->n_actions = n_actions;
1278 error = ofproto->ofproto_class->rule_construct(rule);
1280 ofproto_rule_destroy__(rule);
1288 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
1289 ofproto->name, strerror(error));
1295 ofproto_rule_destroy__(struct rule *rule)
1297 free(rule->actions);
1298 rule->ofproto->ofproto_class->rule_dealloc(rule);
1301 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
1302 * destroying any that no longer has a rule (which is probably all of them).
1304 * The caller must have already removed 'rule' from the classifier. */
1306 ofproto_rule_destroy(struct rule *rule)
1308 rule->ofproto->ofproto_class->rule_destruct(rule);
1309 ofproto_rule_destroy__(rule);
1312 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1313 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1316 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
1318 const union ofp_action *oa;
1319 struct actions_iterator i;
1321 if (out_port == htons(OFPP_NONE)) {
1324 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1325 oa = actions_next(&i)) {
1326 if (action_outputs_to_port(oa, out_port)) {
1334 ofproto_rule_lookup(struct ofproto *ofproto, const struct flow *flow)
1336 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
1339 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1340 * statistics (or the statistics for one of its facets) appropriately.
1341 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
1343 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1344 * with statistics for 'packet' either way.
1346 * Takes ownership of 'packet'. */
1348 rule_execute(struct rule *rule, uint16_t in_port, struct ofpbuf *packet)
1352 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1354 flow_extract(packet, 0, in_port, &flow);
1355 rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet);
1358 /* Remove 'rule' from 'ofproto' and free up the associated memory:
1360 * - Removes 'rule' from the classifier.
1362 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
1363 * destroys them), via rule_destroy().
1366 ofproto_rule_remove(struct rule *rule)
1368 rule->ofproto->ofproto_class->rule_remove(rule);
1369 ofproto_rule_destroy(rule);
1372 /* Returns true if 'rule' should be hidden from the controller.
1374 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1375 * (e.g. by in-band control) and are intentionally hidden from the
1378 rule_is_hidden(const struct rule *rule)
1380 return rule->cr.priority > UINT16_MAX;
1384 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1387 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
1389 COVERAGE_INC(ofproto_error);
1390 ofconn_send_reply(ofconn, buf);
1395 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1397 ofconn_send_reply(ofconn, make_echo_reply(oh));
1402 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1404 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1405 struct ofp_switch_features *osf;
1407 struct ofport *port;
1409 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1410 osf->datapath_id = htonll(ofproto->datapath_id);
1411 osf->n_buffers = htonl(pktbuf_capacity());
1413 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1414 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
1415 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1416 (1u << OFPAT_SET_VLAN_VID) |
1417 (1u << OFPAT_SET_VLAN_PCP) |
1418 (1u << OFPAT_STRIP_VLAN) |
1419 (1u << OFPAT_SET_DL_SRC) |
1420 (1u << OFPAT_SET_DL_DST) |
1421 (1u << OFPAT_SET_NW_SRC) |
1422 (1u << OFPAT_SET_NW_DST) |
1423 (1u << OFPAT_SET_NW_TOS) |
1424 (1u << OFPAT_SET_TP_SRC) |
1425 (1u << OFPAT_SET_TP_DST) |
1426 (1u << OFPAT_ENQUEUE));
1428 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1429 ofpbuf_put(buf, &port->opp, sizeof port->opp);
1432 ofconn_send_reply(ofconn, buf);
1437 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1439 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1441 struct ofp_switch_config *osc;
1445 /* Figure out flags. */
1446 drop_frags = ofproto->ofproto_class->get_drop_frags(ofproto);
1447 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1450 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1451 osc->flags = htons(flags);
1452 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1453 ofconn_send_reply(ofconn, buf);
1459 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1461 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1462 uint16_t flags = ntohs(osc->flags);
1464 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1465 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1466 switch (flags & OFPC_FRAG_MASK) {
1467 case OFPC_FRAG_NORMAL:
1468 ofproto->ofproto_class->set_drop_frags(ofproto, false);
1470 case OFPC_FRAG_DROP:
1471 ofproto->ofproto_class->set_drop_frags(ofproto, true);
1474 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1480 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
1485 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
1486 * error message code (composed with ofp_mkerr()) for the caller to propagate
1487 * upward. Otherwise, returns 0.
1489 * The log message mentions 'msg_type'. */
1491 reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
1493 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1494 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
1495 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
1496 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
1499 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
1506 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
1508 struct ofproto *p = ofconn_get_ofproto(ofconn);
1509 struct ofp_packet_out *opo;
1510 struct ofpbuf payload, *buffer;
1511 union ofp_action *ofp_actions;
1512 struct ofpbuf request;
1514 size_t n_ofp_actions;
1518 COVERAGE_INC(ofproto_packet_out);
1520 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
1525 /* Get ofp_packet_out. */
1526 ofpbuf_use_const(&request, oh, ntohs(oh->length));
1527 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
1530 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
1531 &ofp_actions, &n_ofp_actions);
1537 if (opo->buffer_id != htonl(UINT32_MAX)) {
1538 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
1540 if (error || !buffer) {
1549 /* Send out packet. */
1550 flow_extract(&payload, 0, ntohs(opo->in_port), &flow);
1551 error = p->ofproto_class->packet_out(p, &payload, &flow,
1552 ofp_actions, n_ofp_actions);
1553 ofpbuf_delete(buffer);
1559 update_port_config(struct ofport *port, ovs_be32 config, ovs_be32 mask)
1561 ovs_be32 old_config = port->opp.config;
1563 mask &= config ^ port->opp.config;
1564 if (mask & htonl(OFPPC_PORT_DOWN)) {
1565 if (config & htonl(OFPPC_PORT_DOWN)) {
1566 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
1568 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
1572 port->opp.config ^= mask & (htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
1573 OFPPC_NO_FLOOD | OFPPC_NO_FWD |
1574 OFPPC_NO_PACKET_IN));
1575 if (port->opp.config != old_config) {
1576 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
1581 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
1583 struct ofproto *p = ofconn_get_ofproto(ofconn);
1584 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
1585 struct ofport *port;
1588 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
1593 port = ofproto_get_port(p, ntohs(opm->port_no));
1595 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
1596 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
1597 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
1599 update_port_config(port, opm->config, opm->mask);
1600 if (opm->advertise) {
1601 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
1607 static struct ofpbuf *
1608 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
1610 struct ofp_stats_reply *osr;
1613 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
1614 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
1616 osr->flags = htons(0);
1620 static struct ofpbuf *
1621 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
1623 const struct ofp_stats_request *osr
1624 = (const struct ofp_stats_request *) request;
1625 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
1629 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
1630 struct ofpbuf **msgp)
1632 struct ofpbuf *msg = *msgp;
1633 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
1634 if (nbytes + msg->size > UINT16_MAX) {
1635 struct ofp_stats_reply *reply = msg->data;
1636 reply->flags = htons(OFPSF_REPLY_MORE);
1637 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
1638 ofconn_send_reply(ofconn, msg);
1640 return ofpbuf_put_uninit(*msgp, nbytes);
1643 static struct ofpbuf *
1644 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
1646 struct nicira_stats_msg *nsm;
1649 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
1650 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
1651 nsm->type = htons(OFPST_VENDOR);
1652 nsm->flags = htons(0);
1653 nsm->vendor = htonl(NX_VENDOR_ID);
1654 nsm->subtype = subtype;
1658 static struct ofpbuf *
1659 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
1661 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
1665 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
1666 struct ofpbuf **msgp)
1668 struct ofpbuf *msg = *msgp;
1669 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
1670 if (nbytes + msg->size > UINT16_MAX) {
1671 struct nicira_stats_msg *reply = msg->data;
1672 reply->flags = htons(OFPSF_REPLY_MORE);
1673 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
1674 ofconn_send_reply(ofconn, msg);
1676 ofpbuf_prealloc_tailroom(*msgp, nbytes);
1680 handle_desc_stats_request(struct ofconn *ofconn,
1681 const struct ofp_header *request)
1683 struct ofproto *p = ofconn_get_ofproto(ofconn);
1684 struct ofp_desc_stats *ods;
1687 msg = start_ofp_stats_reply(request, sizeof *ods);
1688 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
1689 memset(ods, 0, sizeof *ods);
1690 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
1691 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
1692 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
1693 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
1694 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
1695 ofconn_send_reply(ofconn, msg);
1701 handle_table_stats_request(struct ofconn *ofconn,
1702 const struct ofp_header *request)
1704 struct ofproto *p = ofconn_get_ofproto(ofconn);
1705 struct ofp_table_stats *ots;
1708 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
1710 /* Classifier table. */
1711 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
1712 memset(ots, 0, sizeof *ots);
1713 strcpy(ots->name, "classifier");
1714 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
1715 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
1716 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
1717 ots->active_count = htonl(classifier_count(&p->cls));
1718 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
1719 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
1721 ofconn_send_reply(ofconn, msg);
1726 append_port_stat(struct ofport *port, struct ofconn *ofconn,
1727 struct ofpbuf **msgp)
1729 struct netdev_stats stats;
1730 struct ofp_port_stats *ops;
1732 /* Intentionally ignore return value, since errors will set
1733 * 'stats' to all-1s, which is correct for OpenFlow, and
1734 * netdev_get_stats() will log errors. */
1735 netdev_get_stats(port->netdev, &stats);
1737 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
1738 ops->port_no = port->opp.port_no;
1739 memset(ops->pad, 0, sizeof ops->pad);
1740 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
1741 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
1742 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
1743 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
1744 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
1745 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
1746 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
1747 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
1748 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
1749 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
1750 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
1751 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
1755 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
1757 struct ofproto *p = ofconn_get_ofproto(ofconn);
1758 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
1759 struct ofp_port_stats *ops;
1761 struct ofport *port;
1763 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
1764 if (psr->port_no != htons(OFPP_NONE)) {
1765 port = ofproto_get_port(p, ntohs(psr->port_no));
1767 append_port_stat(port, ofconn, &msg);
1770 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
1771 append_port_stat(port, ofconn, &msg);
1775 ofconn_send_reply(ofconn, msg);
1780 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
1782 long long int msecs = time_msec() - start;
1783 *sec = msecs / 1000;
1784 *nsec = (msecs % 1000) * (1000 * 1000);
1788 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
1792 calc_flow_duration__(start, &sec, &nsec);
1793 *sec_be = htonl(sec);
1794 *nsec_be = htonl(nsec);
1798 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
1799 ovs_be16 out_port, struct ofpbuf **replyp)
1801 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1802 struct ofp_flow_stats *ofs;
1803 uint64_t packet_count, byte_count;
1805 size_t act_len, len;
1807 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
1811 act_len = sizeof *rule->actions * rule->n_actions;
1812 len = offsetof(struct ofp_flow_stats, actions) + act_len;
1814 ofproto->ofproto_class->rule_get_stats(rule, &packet_count, &byte_count);
1816 ofs = append_ofp_stats_reply(len, ofconn, replyp);
1817 ofs->length = htons(len);
1820 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
1821 &ofs->match, rule->flow_cookie, &cookie);
1822 put_32aligned_be64(&ofs->cookie, cookie);
1823 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
1824 ofs->priority = htons(rule->cr.priority);
1825 ofs->idle_timeout = htons(rule->idle_timeout);
1826 ofs->hard_timeout = htons(rule->hard_timeout);
1827 memset(ofs->pad2, 0, sizeof ofs->pad2);
1828 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
1829 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
1830 if (rule->n_actions > 0) {
1831 memcpy(ofs->actions, rule->actions, act_len);
1836 is_valid_table(uint8_t table_id)
1838 if (table_id == 0 || table_id == 0xff) {
1841 /* It would probably be better to reply with an error but there doesn't
1842 * seem to be any appropriate value, so that might just be
1844 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
1851 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
1853 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
1854 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1855 struct ofpbuf *reply;
1857 COVERAGE_INC(ofproto_flows_req);
1858 reply = start_ofp_stats_reply(oh, 1024);
1859 if (is_valid_table(fsr->table_id)) {
1860 struct cls_cursor cursor;
1861 struct cls_rule target;
1864 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
1866 cls_cursor_init(&cursor, &ofproto->cls, &target);
1867 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1868 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
1871 ofconn_send_reply(ofconn, reply);
1877 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
1878 ovs_be16 out_port, struct ofpbuf **replyp)
1880 struct nx_flow_stats *nfs;
1881 uint64_t packet_count, byte_count;
1882 size_t act_len, start_len;
1883 struct ofpbuf *reply;
1885 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
1889 rule->ofproto->ofproto_class->rule_get_stats(rule,
1890 &packet_count, &byte_count);
1892 act_len = sizeof *rule->actions * rule->n_actions;
1894 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
1895 start_len = (*replyp)->size;
1898 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
1901 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
1902 nfs->cookie = rule->flow_cookie;
1903 nfs->priority = htons(rule->cr.priority);
1904 nfs->idle_timeout = htons(rule->idle_timeout);
1905 nfs->hard_timeout = htons(rule->hard_timeout);
1906 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
1907 memset(nfs->pad2, 0, sizeof nfs->pad2);
1908 nfs->packet_count = htonll(packet_count);
1909 nfs->byte_count = htonll(byte_count);
1910 if (rule->n_actions > 0) {
1911 ofpbuf_put(reply, rule->actions, act_len);
1913 nfs->length = htons(reply->size - start_len);
1917 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
1919 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1920 struct nx_flow_stats_request *nfsr;
1921 struct cls_rule target;
1922 struct ofpbuf *reply;
1926 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1928 /* Dissect the message. */
1929 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1930 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
1935 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1938 COVERAGE_INC(ofproto_flows_req);
1939 reply = start_nxstats_reply(&nfsr->nsm, 1024);
1940 if (is_valid_table(nfsr->table_id)) {
1941 struct cls_cursor cursor;
1944 cls_cursor_init(&cursor, &ofproto->cls, &target);
1945 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1946 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
1949 ofconn_send_reply(ofconn, reply);
1955 flow_stats_ds(struct rule *rule, struct ds *results)
1957 uint64_t packet_count, byte_count;
1958 size_t act_len = sizeof *rule->actions * rule->n_actions;
1960 rule->ofproto->ofproto_class->rule_get_stats(rule,
1961 &packet_count, &byte_count);
1963 ds_put_format(results, "duration=%llds, ",
1964 (time_msec() - rule->created) / 1000);
1965 ds_put_format(results, "priority=%u, ", rule->cr.priority);
1966 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
1967 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
1968 cls_rule_format(&rule->cr, results);
1969 ds_put_char(results, ',');
1971 ofp_print_actions(results, &rule->actions->header, act_len);
1973 ds_put_cstr(results, "drop");
1975 ds_put_cstr(results, "\n");
1978 /* Adds a pretty-printed description of all flows to 'results', including
1979 * hidden flows (e.g., set up by in-band control). */
1981 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
1983 struct cls_cursor cursor;
1986 cls_cursor_init(&cursor, &p->cls, NULL);
1987 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1988 flow_stats_ds(rule, results);
1992 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
1993 * '*engine_type' and '*engine_id', respectively. */
1995 ofproto_get_netflow_ids(const struct ofproto *ofproto,
1996 uint8_t *engine_type, uint8_t *engine_id)
1998 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
2002 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
2003 ovs_be16 out_port, uint8_t table_id,
2004 struct ofp_aggregate_stats_reply *oasr)
2006 uint64_t total_packets = 0;
2007 uint64_t total_bytes = 0;
2010 COVERAGE_INC(ofproto_agg_request);
2012 if (is_valid_table(table_id)) {
2013 struct cls_cursor cursor;
2016 cls_cursor_init(&cursor, &ofproto->cls, target);
2017 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2018 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
2019 uint64_t packet_count;
2020 uint64_t byte_count;
2022 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
2025 total_packets += packet_count;
2026 total_bytes += byte_count;
2032 oasr->flow_count = htonl(n_flows);
2033 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
2034 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
2035 memset(oasr->pad, 0, sizeof oasr->pad);
2039 handle_aggregate_stats_request(struct ofconn *ofconn,
2040 const struct ofp_header *oh)
2042 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
2043 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2044 struct ofp_aggregate_stats_reply *reply;
2045 struct cls_rule target;
2048 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
2051 msg = start_ofp_stats_reply(oh, sizeof *reply);
2052 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
2053 query_aggregate_stats(ofproto, &target, request->out_port,
2054 request->table_id, reply);
2055 ofconn_send_reply(ofconn, msg);
2060 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
2062 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2063 struct nx_aggregate_stats_request *request;
2064 struct ofp_aggregate_stats_reply *reply;
2065 struct cls_rule target;
2070 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2072 /* Dissect the message. */
2073 request = ofpbuf_pull(&b, sizeof *request);
2074 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
2079 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2083 COVERAGE_INC(ofproto_flows_req);
2084 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
2085 reply = ofpbuf_put_uninit(buf, sizeof *reply);
2086 query_aggregate_stats(ofproto, &target, request->out_port,
2087 request->table_id, reply);
2088 ofconn_send_reply(ofconn, buf);
2093 struct queue_stats_cbdata {
2094 struct ofconn *ofconn;
2095 struct ofport *ofport;
2100 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
2101 const struct netdev_queue_stats *stats)
2103 struct ofp_queue_stats *reply;
2105 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
2106 reply->port_no = cbdata->ofport->opp.port_no;
2107 memset(reply->pad, 0, sizeof reply->pad);
2108 reply->queue_id = htonl(queue_id);
2109 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
2110 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
2111 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
2115 handle_queue_stats_dump_cb(uint32_t queue_id,
2116 struct netdev_queue_stats *stats,
2119 struct queue_stats_cbdata *cbdata = cbdata_;
2121 put_queue_stats(cbdata, queue_id, stats);
2125 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
2126 struct queue_stats_cbdata *cbdata)
2128 cbdata->ofport = port;
2129 if (queue_id == OFPQ_ALL) {
2130 netdev_dump_queue_stats(port->netdev,
2131 handle_queue_stats_dump_cb, cbdata);
2133 struct netdev_queue_stats stats;
2135 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
2136 put_queue_stats(cbdata, queue_id, &stats);
2142 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2144 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2145 const struct ofp_queue_stats_request *qsr;
2146 struct queue_stats_cbdata cbdata;
2147 struct ofport *port;
2148 unsigned int port_no;
2151 qsr = ofputil_stats_body(oh);
2153 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2156 COVERAGE_INC(ofproto_queue_req);
2158 cbdata.ofconn = ofconn;
2159 cbdata.msg = start_ofp_stats_reply(oh, 128);
2161 port_no = ntohs(qsr->port_no);
2162 queue_id = ntohl(qsr->queue_id);
2163 if (port_no == OFPP_ALL) {
2164 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2165 handle_queue_stats_for_port(port, queue_id, &cbdata);
2167 } else if (port_no < OFPP_MAX) {
2168 port = ofproto_get_port(ofproto, port_no);
2170 handle_queue_stats_for_port(port, queue_id, &cbdata);
2173 ofpbuf_delete(cbdata.msg);
2174 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
2176 ofconn_send_reply(ofconn, cbdata.msg);
2181 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
2182 * in which no matching flow already exists in the flow table.
2184 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
2185 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
2186 * OpenFlow error code as encoded by ofp_mkerr() on failure.
2188 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
2191 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
2193 struct ofproto *p = ofconn_get_ofproto(ofconn);
2194 struct ofpbuf *packet;
2200 if (fm->flags & OFPFF_CHECK_OVERLAP
2201 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
2202 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
2205 buf_err = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id, &packet, &in_port);
2206 error = rule_create(p, &fm->cr, fm->actions, fm->n_actions,
2207 fm->idle_timeout, fm->hard_timeout, fm->cookie,
2208 fm->flags & OFPFF_SEND_FLOW_REM, &rule);
2210 ofpbuf_delete(packet);
2215 rule_execute(rule, in_port, packet);
2220 static struct rule *
2221 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
2223 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
2227 send_buffered_packet(struct ofconn *ofconn,
2228 struct rule *rule, uint32_t buffer_id)
2230 struct ofpbuf *packet;
2234 if (buffer_id == UINT32_MAX) {
2238 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
2243 rule_execute(rule, in_port, packet);
2248 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
2250 struct modify_flows_cbdata {
2251 struct ofproto *ofproto;
2252 const struct flow_mod *fm;
2256 static int modify_flow(const struct flow_mod *, struct rule *);
2258 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
2259 * encoded by ofp_mkerr() on failure.
2261 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
2264 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
2266 struct ofproto *p = ofconn_get_ofproto(ofconn);
2267 struct rule *match = NULL;
2268 struct cls_cursor cursor;
2273 cls_cursor_init(&cursor, &p->cls, &fm->cr);
2274 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2275 if (!rule_is_hidden(rule)) {
2276 int retval = modify_flow(fm, rule);
2288 /* This credits the packet to whichever flow happened to match last.
2289 * That's weird. Maybe we should do a lookup for the flow that
2290 * actually matches the packet? Who knows. */
2291 send_buffered_packet(ofconn, match, fm->buffer_id);
2294 return add_flow(ofconn, fm);
2298 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
2299 * code as encoded by ofp_mkerr() on failure.
2301 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
2304 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
2306 struct ofproto *p = ofconn_get_ofproto(ofconn);
2307 struct rule *rule = find_flow_strict(p, fm);
2308 if (rule && !rule_is_hidden(rule)) {
2309 int error = modify_flow(fm, rule);
2311 error = send_buffered_packet(ofconn, rule, fm->buffer_id);
2315 return add_flow(ofconn, fm);
2319 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
2320 * been identified as a flow to be modified, by changing the rule's actions to
2321 * match those in 'ofm' (which is followed by 'n_actions' ofp_action[]
2324 modify_flow(const struct flow_mod *fm, struct rule *rule)
2326 size_t actions_len = fm->n_actions * sizeof *rule->actions;
2329 if (fm->n_actions == rule->n_actions
2331 || !memcmp(fm->actions, rule->actions, actions_len))) {
2334 error = rule->ofproto->ofproto_class->rule_modify_actions(
2335 rule, fm->actions, fm->n_actions);
2337 free(rule->actions);
2338 rule->actions = (fm->n_actions
2339 ? xmemdup(fm->actions, actions_len)
2341 rule->n_actions = fm->n_actions;
2346 rule->flow_cookie = fm->cookie;
2352 /* OFPFC_DELETE implementation. */
2354 static void delete_flow(struct rule *, ovs_be16 out_port);
2356 /* Implements OFPFC_DELETE. */
2358 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
2360 struct rule *rule, *next_rule;
2361 struct cls_cursor cursor;
2363 cls_cursor_init(&cursor, &p->cls, &fm->cr);
2364 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
2365 delete_flow(rule, htons(fm->out_port));
2369 /* Implements OFPFC_DELETE_STRICT. */
2371 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
2373 struct rule *rule = find_flow_strict(p, fm);
2375 delete_flow(rule, htons(fm->out_port));
2379 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
2380 * been identified as a flow to delete from 'p''s flow table, by deleting the
2381 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
2384 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
2385 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
2386 * specified 'out_port'. */
2388 delete_flow(struct rule *rule, ovs_be16 out_port)
2390 if (rule_is_hidden(rule)) {
2394 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
2398 ofproto_rule_send_removed(rule, OFPRR_DELETE);
2399 ofproto_rule_remove(rule);
2403 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
2405 struct ofputil_flow_removed fr;
2407 if (rule_is_hidden(rule) || !rule->send_flow_removed) {
2412 fr.cookie = rule->flow_cookie;
2414 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
2415 fr.idle_timeout = rule->idle_timeout;
2416 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
2419 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
2422 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
2423 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
2426 * ofproto implementation ->run() functions should use this function to expire
2427 * OpenFlow flows. */
2429 ofproto_rule_expire(struct rule *rule, uint8_t reason)
2431 assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT);
2432 ofproto_rule_send_removed(rule, reason);
2433 ofproto_rule_remove(rule);
2437 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2439 struct ofproto *p = ofconn_get_ofproto(ofconn);
2443 error = reject_slave_controller(ofconn, "flow_mod");
2448 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
2453 /* We do not support the emergency flow cache. It will hopefully get
2454 * dropped from OpenFlow in the near future. */
2455 if (fm.flags & OFPFF_EMERG) {
2456 /* There isn't a good fit for an error code, so just state that the
2457 * flow table is full. */
2458 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
2461 switch (fm.command) {
2463 return add_flow(ofconn, &fm);
2466 return modify_flows_loose(ofconn, &fm);
2468 case OFPFC_MODIFY_STRICT:
2469 return modify_flow_strict(ofconn, &fm);
2472 delete_flows_loose(p, &fm);
2475 case OFPFC_DELETE_STRICT:
2476 delete_flow_strict(p, &fm);
2480 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
2485 handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
2487 const struct nxt_tun_id_cookie *msg
2488 = (const struct nxt_tun_id_cookie *) oh;
2489 enum nx_flow_format flow_format;
2491 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
2492 ofconn_set_flow_format(ofconn, flow_format);
2498 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
2500 struct nx_role_request *nrr = (struct nx_role_request *) oh;
2501 struct nx_role_request *reply;
2505 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
2506 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
2507 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2510 role = ntohl(nrr->role);
2511 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
2512 && role != NX_ROLE_SLAVE) {
2513 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
2515 /* There's no good error code for this. */
2516 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
2519 ofconn_set_role(ofconn, role);
2521 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
2522 reply->role = htonl(role);
2523 ofconn_send_reply(ofconn, buf);
2529 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
2531 const struct nxt_set_flow_format *msg
2532 = (const struct nxt_set_flow_format *) oh;
2535 format = ntohl(msg->format);
2536 if (format == NXFF_OPENFLOW10
2537 || format == NXFF_TUN_ID_FROM_COOKIE
2538 || format == NXFF_NXM) {
2539 ofconn_set_flow_format(ofconn, format);
2542 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2547 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
2549 struct ofp_header *ob;
2552 /* Currently, everything executes synchronously, so we can just
2553 * immediately send the barrier reply. */
2554 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
2555 ofconn_send_reply(ofconn, buf);
2560 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
2562 const struct ofp_header *oh = msg->data;
2563 const struct ofputil_msg_type *type;
2566 error = ofputil_decode_msg_type(oh, &type);
2571 switch (ofputil_msg_type_code(type)) {
2572 /* OpenFlow requests. */
2573 case OFPUTIL_OFPT_ECHO_REQUEST:
2574 return handle_echo_request(ofconn, oh);
2576 case OFPUTIL_OFPT_FEATURES_REQUEST:
2577 return handle_features_request(ofconn, oh);
2579 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
2580 return handle_get_config_request(ofconn, oh);
2582 case OFPUTIL_OFPT_SET_CONFIG:
2583 return handle_set_config(ofconn, msg->data);
2585 case OFPUTIL_OFPT_PACKET_OUT:
2586 return handle_packet_out(ofconn, oh);
2588 case OFPUTIL_OFPT_PORT_MOD:
2589 return handle_port_mod(ofconn, oh);
2591 case OFPUTIL_OFPT_FLOW_MOD:
2592 return handle_flow_mod(ofconn, oh);
2594 case OFPUTIL_OFPT_BARRIER_REQUEST:
2595 return handle_barrier_request(ofconn, oh);
2597 /* OpenFlow replies. */
2598 case OFPUTIL_OFPT_ECHO_REPLY:
2601 /* Nicira extension requests. */
2602 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
2603 return handle_tun_id_from_cookie(ofconn, oh);
2605 case OFPUTIL_NXT_ROLE_REQUEST:
2606 return handle_role_request(ofconn, oh);
2608 case OFPUTIL_NXT_SET_FLOW_FORMAT:
2609 return handle_nxt_set_flow_format(ofconn, oh);
2611 case OFPUTIL_NXT_FLOW_MOD:
2612 return handle_flow_mod(ofconn, oh);
2614 /* OpenFlow statistics requests. */
2615 case OFPUTIL_OFPST_DESC_REQUEST:
2616 return handle_desc_stats_request(ofconn, oh);
2618 case OFPUTIL_OFPST_FLOW_REQUEST:
2619 return handle_flow_stats_request(ofconn, oh);
2621 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
2622 return handle_aggregate_stats_request(ofconn, oh);
2624 case OFPUTIL_OFPST_TABLE_REQUEST:
2625 return handle_table_stats_request(ofconn, oh);
2627 case OFPUTIL_OFPST_PORT_REQUEST:
2628 return handle_port_stats_request(ofconn, oh);
2630 case OFPUTIL_OFPST_QUEUE_REQUEST:
2631 return handle_queue_stats_request(ofconn, oh);
2633 /* Nicira extension statistics requests. */
2634 case OFPUTIL_NXST_FLOW_REQUEST:
2635 return handle_nxst_flow(ofconn, oh);
2637 case OFPUTIL_NXST_AGGREGATE_REQUEST:
2638 return handle_nxst_aggregate(ofconn, oh);
2640 case OFPUTIL_INVALID:
2641 case OFPUTIL_OFPT_HELLO:
2642 case OFPUTIL_OFPT_ERROR:
2643 case OFPUTIL_OFPT_FEATURES_REPLY:
2644 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
2645 case OFPUTIL_OFPT_PACKET_IN:
2646 case OFPUTIL_OFPT_FLOW_REMOVED:
2647 case OFPUTIL_OFPT_PORT_STATUS:
2648 case OFPUTIL_OFPT_BARRIER_REPLY:
2649 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
2650 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
2651 case OFPUTIL_OFPST_DESC_REPLY:
2652 case OFPUTIL_OFPST_FLOW_REPLY:
2653 case OFPUTIL_OFPST_QUEUE_REPLY:
2654 case OFPUTIL_OFPST_PORT_REPLY:
2655 case OFPUTIL_OFPST_TABLE_REPLY:
2656 case OFPUTIL_OFPST_AGGREGATE_REPLY:
2657 case OFPUTIL_NXT_ROLE_REPLY:
2658 case OFPUTIL_NXT_FLOW_REMOVED:
2659 case OFPUTIL_NXST_FLOW_REPLY:
2660 case OFPUTIL_NXST_AGGREGATE_REPLY:
2662 if (VLOG_IS_WARN_ENABLED()) {
2663 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
2664 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
2667 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
2668 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
2670 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
2676 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
2678 int error = handle_openflow__(ofconn, ofp_msg);
2680 send_error_oh(ofconn, ofp_msg->data, error);
2682 COVERAGE_INC(ofproto_recv_openflow);
2686 pick_datapath_id(const struct ofproto *ofproto)
2688 const struct ofport *port;
2690 port = ofproto_get_port(ofproto, OFPP_LOCAL);
2692 uint8_t ea[ETH_ADDR_LEN];
2695 error = netdev_get_etheraddr(port->netdev, ea);
2697 return eth_addr_to_uint64(ea);
2699 VLOG_WARN("could not get MAC address for %s (%s)",
2700 netdev_get_name(port->netdev), strerror(error));
2702 return ofproto->fallback_dpid;
2706 pick_fallback_dpid(void)
2708 uint8_t ea[ETH_ADDR_LEN];
2709 eth_addr_nicira_random(ea);
2710 return eth_addr_to_uint64(ea);
2713 /* unixctl commands. */
2716 ofproto_lookup(const char *name)
2718 struct ofproto *ofproto;
2720 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
2722 if (!strcmp(ofproto->name, name)) {
2730 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
2731 void *aux OVS_UNUSED)
2733 struct ofproto *ofproto;
2737 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
2738 ds_put_format(&results, "%s\n", ofproto->name);
2740 unixctl_command_reply(conn, 200, ds_cstr(&results));
2741 ds_destroy(&results);
2745 ofproto_unixctl_init(void)
2747 static bool registered;
2753 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);