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>
27 #include "byte-order.h"
29 #include "classifier.h"
33 #include "dynamic-string.h"
34 #include "fail-open.h"
38 #include "mac-learning.h"
39 #include "multipath.h"
45 #include "ofp-print.h"
47 #include "ofproto-sflow.h"
49 #include "openflow/nicira-ext.h"
50 #include "openflow/openflow.h"
51 #include "openvswitch/datapath-protocol.h"
55 #include "poll-loop.h"
59 #include "stream-ssl.h"
63 #include "unaligned.h"
68 VLOG_DEFINE_THIS_MODULE(ofproto);
70 COVERAGE_DEFINE(facet_changed_rule);
71 COVERAGE_DEFINE(facet_revalidate);
72 COVERAGE_DEFINE(odp_overflow);
73 COVERAGE_DEFINE(ofproto_agg_request);
74 COVERAGE_DEFINE(ofproto_costly_flags);
75 COVERAGE_DEFINE(ofproto_ctlr_action);
76 COVERAGE_DEFINE(ofproto_del_rule);
77 COVERAGE_DEFINE(ofproto_error);
78 COVERAGE_DEFINE(ofproto_expiration);
79 COVERAGE_DEFINE(ofproto_expired);
80 COVERAGE_DEFINE(ofproto_flows_req);
81 COVERAGE_DEFINE(ofproto_flush);
82 COVERAGE_DEFINE(ofproto_invalidated);
83 COVERAGE_DEFINE(ofproto_no_packet_in);
84 COVERAGE_DEFINE(ofproto_ofp2odp);
85 COVERAGE_DEFINE(ofproto_packet_in);
86 COVERAGE_DEFINE(ofproto_packet_out);
87 COVERAGE_DEFINE(ofproto_queue_req);
88 COVERAGE_DEFINE(ofproto_recv_openflow);
89 COVERAGE_DEFINE(ofproto_reinit_ports);
90 COVERAGE_DEFINE(ofproto_unexpected_rule);
91 COVERAGE_DEFINE(ofproto_uninstallable);
92 COVERAGE_DEFINE(ofproto_update_port);
94 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
95 * flow translation. */
96 #define MAX_RESUBMIT_RECURSION 16
101 struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
102 struct netdev *netdev;
103 struct ofp_phy_port opp; /* In host byte order. */
105 struct cfm *cfm; /* Connectivity Fault Management, if any. */
108 static void ofport_free(struct ofport *);
109 static void ofport_run(struct ofproto *, struct ofport *);
110 static void ofport_wait(struct ofport *);
112 struct action_xlate_ctx {
113 /* action_xlate_ctx_init() initializes these members. */
116 struct ofproto *ofproto;
118 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
119 * this flow when actions change header fields. */
122 /* The packet corresponding to 'flow', or a null pointer if we are
123 * revalidating without a packet to refer to. */
124 const struct ofpbuf *packet;
126 /* If nonnull, called just before executing a resubmit action.
128 * This is normally null so the client has to set it manually after
129 * calling action_xlate_ctx_init(). */
130 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule *);
132 /* If true, the speciality of 'flow' should be checked before executing
133 * its actions. If special_cb returns false on 'flow' rendered
134 * uninstallable and no actions will be executed. */
137 /* xlate_actions() initializes and uses these members. The client might want
138 * to look at them after it returns. */
140 struct ofpbuf *odp_actions; /* Datapath actions. */
141 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
142 bool may_set_up_flow; /* True ordinarily; false if the actions must
143 * be reassessed for every packet. */
144 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
146 /* xlate_actions() initializes and uses these members, but the client has no
147 * reason to look at them. */
149 int recurse; /* Recursion level, via xlate_table_action. */
150 int last_pop_priority; /* Offset in 'odp_actions' just past most
151 * recent ODP_ACTION_ATTR_SET_PRIORITY. */
154 static void action_xlate_ctx_init(struct action_xlate_ctx *,
155 struct ofproto *, const struct flow *,
156 const struct ofpbuf *);
157 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
158 const union ofp_action *in, size_t n_in);
160 /* An OpenFlow flow. */
162 long long int used; /* Time last used; time created if not used. */
163 long long int created; /* Creation time. */
167 * - Do include packets and bytes from facets that have been deleted or
168 * whose own statistics have been folded into the rule.
170 * - Do include packets and bytes sent "by hand" that were accounted to
171 * the rule without any facet being involved (this is a rare corner
172 * case in rule_execute()).
174 * - Do not include packet or bytes that can be obtained from any facet's
175 * packet_count or byte_count member or that can be obtained from the
176 * datapath by, e.g., dpif_flow_get() for any facet.
178 uint64_t packet_count; /* Number of packets received. */
179 uint64_t byte_count; /* Number of bytes received. */
181 ovs_be64 flow_cookie; /* Controller-issued identifier. */
183 struct cls_rule cr; /* In owning ofproto's classifier. */
184 uint16_t idle_timeout; /* In seconds from time of last use. */
185 uint16_t hard_timeout; /* In seconds from time of creation. */
186 bool send_flow_removed; /* Send a flow removed message? */
187 int n_actions; /* Number of elements in actions[]. */
188 union ofp_action *actions; /* OpenFlow actions. */
189 struct list facets; /* List of "struct facet"s. */
192 static struct rule *rule_from_cls_rule(const struct cls_rule *);
193 static bool rule_is_hidden(const struct rule *);
195 static struct rule *rule_create(const struct cls_rule *,
196 const union ofp_action *, size_t n_actions,
197 uint16_t idle_timeout, uint16_t hard_timeout,
198 ovs_be64 flow_cookie, bool send_flow_removed);
199 static void rule_destroy(struct ofproto *, struct rule *);
200 static void rule_free(struct rule *);
202 static struct rule *rule_lookup(struct ofproto *, const struct flow *);
203 static void rule_insert(struct ofproto *, struct rule *);
204 static void rule_remove(struct ofproto *, struct rule *);
206 static void rule_send_removed(struct ofproto *, struct rule *, uint8_t reason);
207 static void rule_get_stats(const struct rule *, uint64_t *packets,
210 /* An exact-match instantiation of an OpenFlow flow. */
212 long long int used; /* Time last used; time created if not used. */
216 * - Do include packets and bytes sent "by hand", e.g. with
219 * - Do include packets and bytes that were obtained from the datapath
220 * when a flow was deleted (e.g. dpif_flow_del()) or when its
221 * statistics were reset (e.g. dpif_flow_put() with
222 * DPIF_FP_ZERO_STATS).
224 * - Do not include any packets or bytes that can currently be obtained
225 * from the datapath by, e.g., dpif_flow_get().
227 uint64_t packet_count; /* Number of packets received. */
228 uint64_t byte_count; /* Number of bytes received. */
230 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
231 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
233 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
234 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
235 long long int rs_used; /* Used time pushed to resubmit children. */
237 /* Number of bytes passed to account_cb. This may include bytes that can
238 * currently obtained from the datapath (thus, it can be greater than
240 uint64_t accounted_bytes;
242 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
243 struct list list_node; /* In owning rule's 'facets' list. */
244 struct rule *rule; /* Owning rule. */
245 struct flow flow; /* Exact-match flow. */
246 bool installed; /* Installed in datapath? */
247 bool may_install; /* True ordinarily; false if actions must
248 * be reassessed for every packet. */
249 size_t actions_len; /* Number of bytes in actions[]. */
250 struct nlattr *actions; /* Datapath actions. */
251 tag_type tags; /* Tags (set only by hooks). */
252 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
255 static struct facet *facet_create(struct ofproto *, struct rule *,
257 const struct ofpbuf *packet);
258 static void facet_remove(struct ofproto *, struct facet *);
259 static void facet_free(struct facet *);
261 static struct facet *facet_lookup_valid(struct ofproto *, const struct flow *);
262 static bool facet_revalidate(struct ofproto *, struct facet *);
264 static void facet_install(struct ofproto *, struct facet *, bool zero_stats);
265 static void facet_uninstall(struct ofproto *, struct facet *);
266 static void facet_flush_stats(struct ofproto *, struct facet *);
268 static void facet_make_actions(struct ofproto *, struct facet *,
269 const struct ofpbuf *packet);
270 static void facet_update_stats(struct ofproto *, struct facet *,
271 const struct dpif_flow_stats *);
272 static void facet_push_stats(struct ofproto *, struct facet *);
274 static void send_packet_in(struct ofproto *, struct dpif_upcall *,
275 const struct flow *, bool clone);
279 uint64_t datapath_id; /* Datapath ID. */
280 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
281 char *mfr_desc; /* Manufacturer. */
282 char *hw_desc; /* Hardware. */
283 char *sw_desc; /* Software version. */
284 char *serial_desc; /* Serial number. */
285 char *dp_desc; /* Datapath description. */
289 struct netdev_monitor *netdev_monitor;
290 struct hmap ports; /* Contains "struct ofport"s. */
291 struct shash port_by_name;
295 struct netflow *netflow;
296 struct ofproto_sflow *sflow;
299 struct classifier cls;
300 struct timer next_expiration;
304 bool need_revalidate;
305 struct tag_set revalidate_set;
307 /* OpenFlow connections. */
308 struct connmgr *connmgr;
310 /* Hooks for ovs-vswitchd. */
311 const struct ofhooks *ofhooks;
314 /* Used by default ofhooks. */
315 struct mac_learning *ml;
318 /* Map from dpif name to struct ofproto, for use by unixctl commands. */
319 static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos);
321 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
323 static const struct ofhooks default_ofhooks;
325 static uint64_t pick_datapath_id(const struct ofproto *);
326 static uint64_t pick_fallback_dpid(void);
328 static void ofproto_flush_flows__(struct ofproto *);
329 static int ofproto_expire(struct ofproto *);
330 static void flow_push_stats(struct ofproto *, const struct rule *,
331 struct flow *, uint64_t packets, uint64_t bytes,
334 static void handle_upcall(struct ofproto *, struct dpif_upcall *);
336 static void handle_openflow(struct ofconn *, struct ofpbuf *);
338 static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
339 static void update_port(struct ofproto *, const char *devname);
340 static int init_ports(struct ofproto *);
341 static void reinit_ports(struct ofproto *);
343 static void ofproto_unixctl_init(void);
346 ofproto_create(const char *datapath, const char *datapath_type,
347 const struct ofhooks *ofhooks, void *aux,
348 struct ofproto **ofprotop)
350 char local_name[IF_NAMESIZE];
357 ofproto_unixctl_init();
359 /* Connect to datapath and start listening for messages. */
360 error = dpif_open(datapath, datapath_type, &dpif);
362 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
365 error = dpif_recv_set_mask(dpif,
366 ((1u << DPIF_UC_MISS) |
367 (1u << DPIF_UC_ACTION) |
368 (1u << DPIF_UC_SAMPLE)));
370 VLOG_ERR("failed to listen on datapath %s: %s",
371 datapath, strerror(error));
375 dpif_flow_flush(dpif);
376 dpif_recv_purge(dpif);
378 error = dpif_port_get_name(dpif, ODPP_LOCAL,
379 local_name, sizeof local_name);
381 VLOG_ERR("%s: cannot get name of datapath local port (%s)",
382 datapath, strerror(error));
386 /* Initialize settings. */
387 p = xzalloc(sizeof *p);
388 p->fallback_dpid = pick_fallback_dpid();
389 p->datapath_id = p->fallback_dpid;
390 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
391 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
392 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
393 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
394 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
396 /* Initialize datapath. */
398 p->netdev_monitor = netdev_monitor_create();
399 hmap_init(&p->ports);
400 shash_init(&p->port_by_name);
401 p->max_ports = dpif_get_max_ports(dpif);
403 /* Initialize submodules. */
407 /* Initialize flow table. */
408 classifier_init(&p->cls);
409 timer_set_duration(&p->next_expiration, 1000);
411 /* Initialize facet table. */
412 hmap_init(&p->facets);
413 p->need_revalidate = false;
414 tag_set_init(&p->revalidate_set);
416 /* Initialize hooks. */
418 p->ofhooks = ofhooks;
422 p->ofhooks = &default_ofhooks;
424 p->ml = mac_learning_create();
427 /* Pick final datapath ID. */
428 p->datapath_id = pick_datapath_id(p);
429 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
431 shash_add_once(&all_ofprotos, dpif_name(p->dpif), p);
433 /* Initialize OpenFlow connections. */
434 p->connmgr = connmgr_create(p, datapath, local_name);
441 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
443 uint64_t old_dpid = p->datapath_id;
444 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
445 if (p->datapath_id != old_dpid) {
446 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
448 /* Force all active connections to reconnect, since there is no way to
449 * notify a controller that the datapath ID has changed. */
450 ofproto_reconnect_controllers(p);
455 ofproto_set_controllers(struct ofproto *p,
456 const struct ofproto_controller *controllers,
457 size_t n_controllers)
459 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
463 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
465 connmgr_set_fail_mode(p->connmgr, fail_mode);
468 /* Drops the connections between 'ofproto' and all of its controllers, forcing
469 * them to reconnect. */
471 ofproto_reconnect_controllers(struct ofproto *ofproto)
473 connmgr_reconnect(ofproto->connmgr);
476 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
477 * in-band control should guarantee access, in the same way that in-band
478 * control guarantees access to OpenFlow controllers. */
480 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
481 const struct sockaddr_in *extras, size_t n)
483 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
486 /* Sets the OpenFlow queue used by flows set up by in-band control on
487 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
488 * flows will use the default queue. */
490 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
492 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
496 ofproto_set_desc(struct ofproto *p,
497 const char *mfr_desc, const char *hw_desc,
498 const char *sw_desc, const char *serial_desc,
501 struct ofp_desc_stats *ods;
504 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
505 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
506 sizeof ods->mfr_desc);
509 p->mfr_desc = xstrdup(mfr_desc);
512 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
513 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
514 sizeof ods->hw_desc);
517 p->hw_desc = xstrdup(hw_desc);
520 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
521 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
522 sizeof ods->sw_desc);
525 p->sw_desc = xstrdup(sw_desc);
528 if (strlen(serial_desc) >= sizeof ods->serial_num) {
529 VLOG_WARN("truncating serial_desc, must be less than %zu "
531 sizeof ods->serial_num);
533 free(p->serial_desc);
534 p->serial_desc = xstrdup(serial_desc);
537 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
538 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
539 sizeof ods->dp_desc);
542 p->dp_desc = xstrdup(dp_desc);
547 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
549 return connmgr_set_snoops(ofproto->connmgr, snoops);
553 ofproto_set_netflow(struct ofproto *ofproto,
554 const struct netflow_options *nf_options)
556 if (nf_options && !sset_is_empty(&nf_options->collectors)) {
557 if (!ofproto->netflow) {
558 ofproto->netflow = netflow_create();
560 return netflow_set_options(ofproto->netflow, nf_options);
562 netflow_destroy(ofproto->netflow);
563 ofproto->netflow = NULL;
569 ofproto_set_sflow(struct ofproto *ofproto,
570 const struct ofproto_sflow_options *oso)
572 struct ofproto_sflow *os = ofproto->sflow;
575 struct ofport *ofport;
577 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
578 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
579 ofproto_sflow_add_port(os, ofport->odp_port,
580 netdev_get_name(ofport->netdev));
583 ofproto_sflow_set_options(os, oso);
585 ofproto_sflow_destroy(os);
586 ofproto->sflow = NULL;
590 /* Connectivity Fault Management configuration. */
592 /* Clears the CFM configuration from 'port_no' on 'ofproto'. */
594 ofproto_iface_clear_cfm(struct ofproto *ofproto, uint32_t port_no)
596 struct ofport *ofport = get_port(ofproto, port_no);
597 if (ofport && ofport->cfm){
598 cfm_destroy(ofport->cfm);
603 /* Configures connectivity fault management on 'port_no' in 'ofproto'. Takes
604 * basic configuration from the configuration members in 'cfm', and the set of
605 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
606 * Ignores the statistics members of 'cfm'.
608 * This function has no effect if 'ofproto' does not have a port 'port_no'. */
610 ofproto_iface_set_cfm(struct ofproto *ofproto, uint32_t port_no,
611 const struct cfm *cfm,
612 const uint16_t *remote_mps, size_t n_remote_mps)
614 struct ofport *ofport;
616 ofport = get_port(ofproto, port_no);
618 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
619 dpif_name(ofproto->dpif), port_no);
624 ofport->cfm = cfm_create();
627 ofport->cfm->mpid = cfm->mpid;
628 ofport->cfm->interval = cfm->interval;
629 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
631 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
633 if (!cfm_configure(ofport->cfm)) {
634 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed",
635 dpif_name(ofproto->dpif), port_no,
636 netdev_get_name(ofport->netdev));
637 cfm_destroy(ofport->cfm);
642 /* Returns the connectivity fault management object associated with 'port_no'
643 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
644 * 'port_no' or if that port does not have CFM configured. The caller must not
645 * modify or destroy the returned object. */
647 ofproto_iface_get_cfm(struct ofproto *ofproto, uint32_t port_no)
649 struct ofport *ofport = get_port(ofproto, port_no);
650 return ofport ? ofport->cfm : NULL;
654 ofproto_get_datapath_id(const struct ofproto *ofproto)
656 return ofproto->datapath_id;
660 ofproto_has_primary_controller(const struct ofproto *ofproto)
662 return connmgr_has_controllers(ofproto->connmgr);
665 enum ofproto_fail_mode
666 ofproto_get_fail_mode(const struct ofproto *p)
668 return connmgr_get_fail_mode(p->connmgr);
672 ofproto_has_snoops(const struct ofproto *ofproto)
674 return connmgr_has_snoops(ofproto->connmgr);
678 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
680 connmgr_get_snoops(ofproto->connmgr, snoops);
684 ofproto_destroy(struct ofproto *p)
686 struct ofport *ofport, *next_ofport;
692 shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif));
694 ofproto_flush_flows__(p);
695 connmgr_destroy(p->connmgr);
696 classifier_destroy(&p->cls);
697 hmap_destroy(&p->facets);
700 netdev_monitor_destroy(p->netdev_monitor);
701 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
702 hmap_remove(&p->ports, &ofport->hmap_node);
705 shash_destroy(&p->port_by_name);
707 netflow_destroy(p->netflow);
708 ofproto_sflow_destroy(p->sflow);
710 mac_learning_destroy(p->ml);
715 free(p->serial_desc);
718 hmap_destroy(&p->ports);
724 ofproto_run(struct ofproto *p)
726 int error = ofproto_run1(p);
728 error = ofproto_run2(p, false);
734 process_port_change(struct ofproto *ofproto, int error, char *devname)
736 if (error == ENOBUFS) {
737 reinit_ports(ofproto);
739 update_port(ofproto, devname);
745 ofproto_run1(struct ofproto *p)
747 struct ofport *ofport;
752 if (shash_is_empty(&p->port_by_name)) {
756 for (i = 0; i < 50; i++) {
757 struct dpif_upcall packet;
759 error = dpif_recv(p->dpif, &packet);
761 if (error == ENODEV) {
762 /* Someone destroyed the datapath behind our back. The caller
763 * better destroy us and give up, because we're just going to
764 * spin from here on out. */
765 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
766 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
773 handle_upcall(p, &packet);
776 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
777 process_port_change(p, error, devname);
779 while ((error = netdev_monitor_poll(p->netdev_monitor,
780 &devname)) != EAGAIN) {
781 process_port_change(p, error, devname);
784 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
785 ofport_run(p, ofport);
788 connmgr_run(p->connmgr, handle_openflow);
790 if (timer_expired(&p->next_expiration)) {
791 int delay = ofproto_expire(p);
792 timer_set_duration(&p->next_expiration, delay);
793 COVERAGE_INC(ofproto_expiration);
797 netflow_run(p->netflow);
800 ofproto_sflow_run(p->sflow);
807 ofproto_run2(struct ofproto *p, bool revalidate_all)
809 /* Figure out what we need to revalidate now, if anything. */
810 struct tag_set revalidate_set = p->revalidate_set;
811 if (p->need_revalidate) {
812 revalidate_all = true;
815 /* Clear the revalidation flags. */
816 tag_set_init(&p->revalidate_set);
817 p->need_revalidate = false;
819 /* Now revalidate if there's anything to do. */
820 if (revalidate_all || !tag_set_is_empty(&revalidate_set)) {
821 struct facet *facet, *next;
823 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) {
825 || tag_set_intersects(&revalidate_set, facet->tags)) {
826 facet_revalidate(p, facet);
835 ofproto_wait(struct ofproto *p)
837 struct ofport *ofport;
839 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
842 dpif_recv_wait(p->dpif);
843 dpif_port_poll_wait(p->dpif);
844 netdev_monitor_poll_wait(p->netdev_monitor);
846 ofproto_sflow_wait(p->sflow);
848 if (!tag_set_is_empty(&p->revalidate_set)) {
849 poll_immediate_wake();
851 if (p->need_revalidate) {
852 /* Shouldn't happen, but if it does just go around again. */
853 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
854 poll_immediate_wake();
856 timer_wait(&p->next_expiration);
858 connmgr_wait(p->connmgr);
862 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
864 tag_set_add(&ofproto->revalidate_set, tag);
868 ofproto_get_revalidate_set(struct ofproto *ofproto)
870 return &ofproto->revalidate_set;
874 ofproto_is_alive(const struct ofproto *p)
876 return connmgr_has_controllers(p->connmgr);
880 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
883 connmgr_get_controller_info(ofproto->connmgr, info);
887 ofproto_free_ofproto_controller_info(struct shash *info)
889 struct shash_node *node;
891 SHASH_FOR_EACH (node, info) {
892 struct ofproto_controller_info *cinfo = node->data;
893 while (cinfo->pairs.n) {
894 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
901 /* Deletes port number 'odp_port' from the datapath for 'ofproto'.
903 * This is almost the same as calling dpif_port_del() directly on the
904 * datapath, but it also makes 'ofproto' close its open netdev for the port
905 * (if any). This makes it possible to create a new netdev of a different
906 * type under the same name, which otherwise the netdev library would refuse
907 * to do because of the conflict. (The netdev would eventually get closed on
908 * the next trip through ofproto_run(), but this interface is more direct.)
910 * Returns 0 if successful, otherwise a positive errno. */
912 ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port)
914 struct ofport *ofport = get_port(ofproto, odp_port);
915 const char *name = ofport ? ofport->opp.name : "<unknown>";
918 error = dpif_port_del(ofproto->dpif, odp_port);
920 VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)",
921 dpif_name(ofproto->dpif), odp_port, name, strerror(error));
923 /* 'name' is ofport->opp.name and update_port() is going to destroy
924 * 'ofport'. Just in case update_port() refers to 'name' after it
925 * destroys 'ofport', make a copy of it around the update_port()
927 char *devname = xstrdup(name);
928 update_port(ofproto, devname);
934 /* Checks if 'ofproto' thinks 'odp_port' should be included in floods. Returns
935 * true if 'odp_port' exists and should be included, false otherwise. */
937 ofproto_port_is_floodable(struct ofproto *ofproto, uint16_t odp_port)
939 struct ofport *ofport = get_port(ofproto, odp_port);
940 return ofport && !(ofport->opp.config & OFPPC_NO_FLOOD);
943 /* Sends 'packet' out of port 'port_no' within 'p'. If 'vlan_tci' is zero the
944 * packet will not have any 802.1Q hader; if it is nonzero, then the packet
945 * will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
947 * Returns 0 if successful, otherwise a positive errno value. */
949 ofproto_send_packet(struct ofproto *ofproto,
950 uint32_t port_no, uint16_t vlan_tci,
951 const struct ofpbuf *packet)
953 struct ofpbuf odp_actions;
956 ofpbuf_init(&odp_actions, 32);
958 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
959 ntohs(vlan_tci & ~VLAN_CFI));
961 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, port_no);
962 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
964 ofpbuf_uninit(&odp_actions);
967 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
968 dpif_name(ofproto->dpif), port_no, strerror(error));
973 /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
974 * performs the 'n_actions' actions in 'actions'. The new flow will not
977 * If cls_rule->priority is in the range of priorities supported by OpenFlow
978 * (0...65535, inclusive) then the flow will be visible to OpenFlow
979 * controllers; otherwise, it will be hidden.
981 * The caller retains ownership of 'cls_rule' and 'actions'. */
983 ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
984 const union ofp_action *actions, size_t n_actions)
987 rule = rule_create(cls_rule, actions, n_actions, 0, 0, 0, false);
988 rule_insert(p, rule);
992 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
996 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
999 rule_remove(ofproto, rule);
1004 ofproto_flush_flows__(struct ofproto *ofproto)
1006 struct facet *facet, *next_facet;
1007 struct rule *rule, *next_rule;
1008 struct cls_cursor cursor;
1010 COVERAGE_INC(ofproto_flush);
1012 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1013 /* Mark the facet as not installed so that facet_remove() doesn't
1014 * bother trying to uninstall it. There is no point in uninstalling it
1015 * individually since we are about to blow away all the facets with
1016 * dpif_flow_flush(). */
1017 facet->installed = false;
1018 facet->dp_packet_count = 0;
1019 facet->dp_byte_count = 0;
1020 facet_remove(ofproto, facet);
1023 cls_cursor_init(&cursor, &ofproto->cls, NULL);
1024 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1025 rule_remove(ofproto, rule);
1028 dpif_flow_flush(ofproto->dpif);
1032 ofproto_flush_flows(struct ofproto *ofproto)
1034 ofproto_flush_flows__(ofproto);
1035 connmgr_flushed(ofproto->connmgr);
1039 reinit_ports(struct ofproto *p)
1041 struct dpif_port_dump dump;
1042 struct sset devnames;
1043 struct ofport *ofport;
1044 struct dpif_port dpif_port;
1045 const char *devname;
1047 COVERAGE_INC(ofproto_reinit_ports);
1049 sset_init(&devnames);
1050 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1051 sset_add(&devnames, ofport->opp.name);
1053 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1054 sset_add(&devnames, dpif_port.name);
1057 SSET_FOR_EACH (devname, &devnames) {
1058 update_port(p, devname);
1060 sset_destroy(&devnames);
1063 static struct ofport *
1064 make_ofport(const struct dpif_port *dpif_port)
1066 struct netdev_options netdev_options;
1067 enum netdev_flags flags;
1068 struct ofport *ofport;
1069 struct netdev *netdev;
1072 memset(&netdev_options, 0, sizeof netdev_options);
1073 netdev_options.name = dpif_port->name;
1074 netdev_options.type = dpif_port->type;
1075 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
1077 error = netdev_open(&netdev_options, &netdev);
1079 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1080 "cannot be opened (%s)",
1081 dpif_port->name, dpif_port->port_no,
1082 dpif_port->name, strerror(error));
1086 ofport = xzalloc(sizeof *ofport);
1087 ofport->netdev = netdev;
1088 ofport->odp_port = dpif_port->port_no;
1089 ofport->opp.port_no = odp_port_to_ofp_port(dpif_port->port_no);
1090 netdev_get_etheraddr(netdev, ofport->opp.hw_addr);
1091 ovs_strlcpy(ofport->opp.name, dpif_port->name, sizeof ofport->opp.name);
1093 netdev_get_flags(netdev, &flags);
1094 ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
1096 ofport->opp.state = netdev_get_carrier(netdev) ? 0 : OFPPS_LINK_DOWN;
1098 netdev_get_features(netdev,
1099 &ofport->opp.curr, &ofport->opp.advertised,
1100 &ofport->opp.supported, &ofport->opp.peer);
1105 ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port)
1107 if (get_port(p, dpif_port->port_no)) {
1108 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1109 dpif_port->port_no);
1111 } else if (shash_find(&p->port_by_name, dpif_port->name)) {
1112 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1121 ofport_equal(const struct ofport *a_, const struct ofport *b_)
1123 const struct ofp_phy_port *a = &a_->opp;
1124 const struct ofp_phy_port *b = &b_->opp;
1126 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1127 return (a->port_no == b->port_no
1128 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1129 && !strcmp(a->name, b->name)
1130 && a->state == b->state
1131 && a->config == b->config
1132 && a->curr == b->curr
1133 && a->advertised == b->advertised
1134 && a->supported == b->supported
1135 && a->peer == b->peer);
1139 ofport_install(struct ofproto *p, struct ofport *ofport)
1141 const char *netdev_name = ofport->opp.name;
1143 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1144 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
1145 shash_add(&p->port_by_name, netdev_name, ofport);
1147 ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
1152 ofport_remove(struct ofproto *p, struct ofport *ofport)
1154 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1155 hmap_remove(&p->ports, &ofport->hmap_node);
1156 shash_delete(&p->port_by_name,
1157 shash_find(&p->port_by_name, ofport->opp.name));
1159 ofproto_sflow_del_port(p->sflow, ofport->odp_port);
1164 ofport_run(struct ofproto *ofproto, struct ofport *ofport)
1167 cfm_run(ofport->cfm);
1169 if (cfm_should_send_ccm(ofport->cfm)) {
1170 struct ofpbuf packet;
1173 ofpbuf_init(&packet, 0);
1174 ccm = compose_packet(&packet, eth_addr_ccm, ofport->opp.hw_addr,
1175 ETH_TYPE_CFM, sizeof *ccm);
1176 cfm_compose_ccm(ofport->cfm, ccm);
1177 ofproto_send_packet(ofproto, ofport->odp_port, 0, &packet);
1178 ofpbuf_uninit(&packet);
1184 ofport_wait(struct ofport *ofport)
1187 cfm_wait(ofport->cfm);
1192 ofport_free(struct ofport *ofport)
1195 cfm_destroy(ofport->cfm);
1196 netdev_close(ofport->netdev);
1201 static struct ofport *
1202 get_port(const struct ofproto *ofproto, uint16_t odp_port)
1204 struct ofport *port;
1206 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1207 hash_int(odp_port, 0), &ofproto->ports) {
1208 if (port->odp_port == odp_port) {
1216 update_port(struct ofproto *p, const char *devname)
1218 struct dpif_port dpif_port;
1219 struct ofport *old_ofport;
1220 struct ofport *new_ofport;
1223 COVERAGE_INC(ofproto_update_port);
1225 /* Query the datapath for port information. */
1226 error = dpif_port_query_by_name(p->dpif, devname, &dpif_port);
1228 /* Find the old ofport. */
1229 old_ofport = shash_find_data(&p->port_by_name, devname);
1232 /* There's no port named 'devname' but there might be a port with
1233 * the same port number. This could happen if a port is deleted
1234 * and then a new one added in its place very quickly, or if a port
1235 * is renamed. In the former case we want to send an OFPPR_DELETE
1236 * and an OFPPR_ADD, and in the latter case we want to send a
1237 * single OFPPR_MODIFY. We can distinguish the cases by comparing
1238 * the old port's ifindex against the new port, or perhaps less
1239 * reliably but more portably by comparing the old port's MAC
1240 * against the new port's MAC. However, this code isn't that smart
1241 * and always sends an OFPPR_MODIFY (XXX). */
1242 old_ofport = get_port(p, dpif_port.port_no);
1244 } else if (error != ENOENT && error != ENODEV) {
1245 VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error "
1246 "%s", strerror(error));
1250 /* Create a new ofport. */
1251 new_ofport = !error ? make_ofport(&dpif_port) : NULL;
1253 /* Eliminate a few pathological cases. */
1254 if (!old_ofport && !new_ofport) {
1256 } else if (old_ofport && new_ofport) {
1257 /* Most of the 'config' bits are OpenFlow soft state, but
1258 * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
1259 * OpenFlow bits from old_ofport. (make_ofport() only sets
1260 * OFPPC_PORT_DOWN and leaves the other bits 0.) */
1261 new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN;
1263 if (ofport_equal(old_ofport, new_ofport)) {
1264 /* False alarm--no change. */
1265 ofport_free(new_ofport);
1270 /* Now deal with the normal cases. */
1272 ofport_remove(p, old_ofport);
1275 ofport_install(p, new_ofport);
1277 connmgr_send_port_status(p->connmgr,
1278 new_ofport ? &new_ofport->opp : &old_ofport->opp,
1279 (!old_ofport ? OFPPR_ADD
1280 : !new_ofport ? OFPPR_DELETE
1282 ofport_free(old_ofport);
1285 dpif_port_destroy(&dpif_port);
1289 init_ports(struct ofproto *p)
1291 struct dpif_port_dump dump;
1292 struct dpif_port dpif_port;
1294 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1295 if (!ofport_conflicts(p, &dpif_port)) {
1296 struct ofport *ofport = make_ofport(&dpif_port);
1298 ofport_install(p, ofport);
1306 /* Returns true if 'rule' should be hidden from the controller.
1308 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1309 * (e.g. by in-band control) and are intentionally hidden from the
1312 rule_is_hidden(const struct rule *rule)
1314 return rule->cr.priority > UINT16_MAX;
1317 /* Creates and returns a new rule initialized as specified.
1319 * The caller is responsible for inserting the rule into the classifier (with
1320 * rule_insert()). */
1321 static struct rule *
1322 rule_create(const struct cls_rule *cls_rule,
1323 const union ofp_action *actions, size_t n_actions,
1324 uint16_t idle_timeout, uint16_t hard_timeout,
1325 ovs_be64 flow_cookie, bool send_flow_removed)
1327 struct rule *rule = xzalloc(sizeof *rule);
1328 rule->cr = *cls_rule;
1329 rule->idle_timeout = idle_timeout;
1330 rule->hard_timeout = hard_timeout;
1331 rule->flow_cookie = flow_cookie;
1332 rule->used = rule->created = time_msec();
1333 rule->send_flow_removed = send_flow_removed;
1334 list_init(&rule->facets);
1335 if (n_actions > 0) {
1336 rule->n_actions = n_actions;
1337 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1343 static struct rule *
1344 rule_from_cls_rule(const struct cls_rule *cls_rule)
1346 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1350 rule_free(struct rule *rule)
1352 free(rule->actions);
1356 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
1357 * destroying any that no longer has a rule (which is probably all of them).
1359 * The caller must have already removed 'rule' from the classifier. */
1361 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1363 struct facet *facet, *next_facet;
1364 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1365 facet_revalidate(ofproto, facet);
1370 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1371 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1374 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
1376 const union ofp_action *oa;
1377 struct actions_iterator i;
1379 if (out_port == htons(OFPP_NONE)) {
1382 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1383 oa = actions_next(&i)) {
1384 if (action_outputs_to_port(oa, out_port)) {
1391 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1392 * 'packet', which arrived on 'in_port'.
1394 * Takes ownership of 'packet'. */
1396 execute_odp_actions(struct ofproto *ofproto, const struct flow *flow,
1397 const struct nlattr *odp_actions, size_t actions_len,
1398 struct ofpbuf *packet)
1400 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1401 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1402 /* As an optimization, avoid a round-trip from userspace to kernel to
1403 * userspace. This also avoids possibly filling up kernel packet
1404 * buffers along the way. */
1405 struct dpif_upcall upcall;
1407 upcall.type = DPIF_UC_ACTION;
1408 upcall.packet = packet;
1411 upcall.userdata = nl_attr_get_u64(odp_actions);
1412 upcall.sample_pool = 0;
1413 upcall.actions = NULL;
1414 upcall.actions_len = 0;
1416 send_packet_in(ofproto, &upcall, flow, false);
1422 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
1423 ofpbuf_delete(packet);
1428 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
1429 * statistics appropriately. 'packet' must have at least sizeof(struct
1430 * ofp_packet_in) bytes of headroom.
1432 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
1433 * applying flow_extract() to 'packet' would yield the same flow as
1436 * 'facet' must have accurately composed ODP actions; that is, it must not be
1437 * in need of revalidation.
1439 * Takes ownership of 'packet'. */
1441 facet_execute(struct ofproto *ofproto, struct facet *facet,
1442 struct ofpbuf *packet)
1444 struct dpif_flow_stats stats;
1446 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1448 flow_extract_stats(&facet->flow, packet, &stats);
1449 stats.used = time_msec();
1450 if (execute_odp_actions(ofproto, &facet->flow,
1451 facet->actions, facet->actions_len, packet)) {
1452 facet_update_stats(ofproto, facet, &stats);
1456 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1457 * statistics (or the statistics for one of its facets) appropriately.
1458 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
1460 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1461 * with statistics for 'packet' either way.
1463 * Takes ownership of 'packet'. */
1465 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
1466 struct ofpbuf *packet)
1468 struct action_xlate_ctx ctx;
1469 struct ofpbuf *odp_actions;
1470 struct facet *facet;
1474 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1476 flow_extract(packet, 0, in_port, &flow);
1478 /* First look for a related facet. If we find one, account it to that. */
1479 facet = facet_lookup_valid(ofproto, &flow);
1480 if (facet && facet->rule == rule) {
1481 facet_execute(ofproto, facet, packet);
1485 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
1486 * create a new facet for it and use that. */
1487 if (rule_lookup(ofproto, &flow) == rule) {
1488 facet = facet_create(ofproto, rule, &flow, packet);
1489 facet_execute(ofproto, facet, packet);
1490 facet_install(ofproto, facet, true);
1494 /* We can't account anything to a facet. If we were to try, then that
1495 * facet would have a non-matching rule, busting our invariants. */
1496 action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
1497 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1498 size = packet->size;
1499 if (execute_odp_actions(ofproto, &flow, odp_actions->data,
1500 odp_actions->size, packet)) {
1501 rule->used = time_msec();
1502 rule->packet_count++;
1503 rule->byte_count += size;
1504 flow_push_stats(ofproto, rule, &flow, 1, size, rule->used);
1506 ofpbuf_delete(odp_actions);
1509 /* Inserts 'rule' into 'p''s flow table. */
1511 rule_insert(struct ofproto *p, struct rule *rule)
1513 struct rule *displaced_rule;
1515 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1516 if (displaced_rule) {
1517 rule_destroy(p, displaced_rule);
1519 p->need_revalidate = true;
1522 /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a
1523 * 'flow' and an example 'packet' within that flow.
1525 * The caller must already have determined that no facet with an identical
1526 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1527 * 'ofproto''s classifier table. */
1528 static struct facet *
1529 facet_create(struct ofproto *ofproto, struct rule *rule,
1530 const struct flow *flow, const struct ofpbuf *packet)
1532 struct facet *facet;
1534 facet = xzalloc(sizeof *facet);
1535 facet->used = time_msec();
1536 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1537 list_push_back(&rule->facets, &facet->list_node);
1539 facet->flow = *flow;
1540 netflow_flow_init(&facet->nf_flow);
1541 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1543 facet_make_actions(ofproto, facet, packet);
1549 facet_free(struct facet *facet)
1551 free(facet->actions);
1555 /* Remove 'rule' from 'ofproto' and free up the associated memory:
1557 * - Removes 'rule' from the classifier.
1559 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
1560 * destroys them), via rule_destroy().
1563 rule_remove(struct ofproto *ofproto, struct rule *rule)
1565 COVERAGE_INC(ofproto_del_rule);
1566 ofproto->need_revalidate = true;
1567 classifier_remove(&ofproto->cls, &rule->cr);
1568 rule_destroy(ofproto, rule);
1571 /* Remove 'facet' from 'ofproto' and free up the associated memory:
1573 * - If 'facet' was installed in the datapath, uninstalls it and updates its
1574 * rule's statistics, via facet_uninstall().
1576 * - Removes 'facet' from its rule and from ofproto->facets.
1579 facet_remove(struct ofproto *ofproto, struct facet *facet)
1581 facet_uninstall(ofproto, facet);
1582 facet_flush_stats(ofproto, facet);
1583 hmap_remove(&ofproto->facets, &facet->hmap_node);
1584 list_remove(&facet->list_node);
1588 /* Composes the ODP actions for 'facet' based on its rule's actions. */
1590 facet_make_actions(struct ofproto *p, struct facet *facet,
1591 const struct ofpbuf *packet)
1593 const struct rule *rule = facet->rule;
1594 struct ofpbuf *odp_actions;
1595 struct action_xlate_ctx ctx;
1597 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
1598 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1599 facet->tags = ctx.tags;
1600 facet->may_install = ctx.may_set_up_flow;
1601 facet->nf_flow.output_iface = ctx.nf_output_iface;
1603 if (facet->actions_len != odp_actions->size
1604 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
1605 free(facet->actions);
1606 facet->actions_len = odp_actions->size;
1607 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1610 ofpbuf_delete(odp_actions);
1614 facet_put__(struct ofproto *ofproto, struct facet *facet,
1615 const struct nlattr *actions, size_t actions_len,
1616 struct dpif_flow_stats *stats)
1618 struct odputil_keybuf keybuf;
1619 enum dpif_flow_put_flags flags;
1622 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1624 flags |= DPIF_FP_ZERO_STATS;
1625 facet->dp_packet_count = 0;
1626 facet->dp_byte_count = 0;
1629 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1630 odp_flow_key_from_flow(&key, &facet->flow);
1632 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
1633 actions, actions_len, stats);
1636 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
1637 * 'zero_stats' is true, clears any existing statistics from the datapath for
1640 facet_install(struct ofproto *p, struct facet *facet, bool zero_stats)
1642 struct dpif_flow_stats stats;
1644 if (facet->may_install
1645 && !facet_put__(p, facet, facet->actions, facet->actions_len,
1646 zero_stats ? &stats : NULL)) {
1647 facet->installed = true;
1651 /* Ensures that the bytes in 'facet', plus 'extra_bytes', have been passed up
1652 * to the accounting hook function in the ofhooks structure. */
1654 facet_account(struct ofproto *ofproto,
1655 struct facet *facet, uint64_t extra_bytes)
1657 uint64_t total_bytes = facet->byte_count + extra_bytes;
1659 if (ofproto->ofhooks->account_flow_cb
1660 && total_bytes > facet->accounted_bytes)
1662 ofproto->ofhooks->account_flow_cb(
1663 &facet->flow, facet->tags, facet->actions, facet->actions_len,
1664 total_bytes - facet->accounted_bytes, ofproto->aux);
1665 facet->accounted_bytes = total_bytes;
1669 /* If 'rule' is installed in the datapath, uninstalls it. */
1671 facet_uninstall(struct ofproto *p, struct facet *facet)
1673 if (facet->installed) {
1674 struct odputil_keybuf keybuf;
1675 struct dpif_flow_stats stats;
1678 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1679 odp_flow_key_from_flow(&key, &facet->flow);
1681 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
1682 facet_update_stats(p, facet, &stats);
1684 facet->installed = false;
1685 facet->dp_packet_count = 0;
1686 facet->dp_byte_count = 0;
1688 assert(facet->dp_packet_count == 0);
1689 assert(facet->dp_byte_count == 0);
1693 /* Returns true if the only action for 'facet' is to send to the controller.
1694 * (We don't report NetFlow expiration messages for such facets because they
1695 * are just part of the control logic for the network, not real traffic). */
1697 facet_is_controller_flow(struct facet *facet)
1700 && facet->rule->n_actions == 1
1701 && action_outputs_to_port(&facet->rule->actions[0],
1702 htons(OFPP_CONTROLLER)));
1705 /* Folds all of 'facet''s statistics into its rule. Also updates the
1706 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
1707 * 'facet''s statistics in the datapath should have been zeroed and folded into
1708 * its packet and byte counts before this function is called. */
1710 facet_flush_stats(struct ofproto *ofproto, struct facet *facet)
1712 assert(!facet->dp_byte_count);
1713 assert(!facet->dp_packet_count);
1715 facet_push_stats(ofproto, facet);
1716 facet_account(ofproto, facet, 0);
1718 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
1719 struct ofexpired expired;
1720 expired.flow = facet->flow;
1721 expired.packet_count = facet->packet_count;
1722 expired.byte_count = facet->byte_count;
1723 expired.used = facet->used;
1724 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1727 facet->rule->packet_count += facet->packet_count;
1728 facet->rule->byte_count += facet->byte_count;
1730 /* Reset counters to prevent double counting if 'facet' ever gets
1732 facet->packet_count = 0;
1733 facet->byte_count = 0;
1734 facet->rs_packet_count = 0;
1735 facet->rs_byte_count = 0;
1736 facet->accounted_bytes = 0;
1738 netflow_flow_clear(&facet->nf_flow);
1741 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1742 * Returns it if found, otherwise a null pointer.
1744 * The returned facet might need revalidation; use facet_lookup_valid()
1745 * instead if that is important. */
1746 static struct facet *
1747 facet_find(struct ofproto *ofproto, const struct flow *flow)
1749 struct facet *facet;
1751 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
1753 if (flow_equal(flow, &facet->flow)) {
1761 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1762 * Returns it if found, otherwise a null pointer.
1764 * The returned facet is guaranteed to be valid. */
1765 static struct facet *
1766 facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
1768 struct facet *facet = facet_find(ofproto, flow);
1770 /* The facet we found might not be valid, since we could be in need of
1771 * revalidation. If it is not valid, don't return it. */
1773 && ofproto->need_revalidate
1774 && !facet_revalidate(ofproto, facet)) {
1775 COVERAGE_INC(ofproto_invalidated);
1782 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
1784 * - If the rule found is different from 'facet''s current rule, moves
1785 * 'facet' to the new rule and recompiles its actions.
1787 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
1788 * where it is and recompiles its actions anyway.
1790 * - If there is none, destroys 'facet'.
1792 * Returns true if 'facet' still exists, false if it has been destroyed. */
1794 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
1796 struct action_xlate_ctx ctx;
1797 struct ofpbuf *odp_actions;
1798 struct rule *new_rule;
1799 bool actions_changed;
1801 COVERAGE_INC(facet_revalidate);
1803 /* Determine the new rule. */
1804 new_rule = rule_lookup(ofproto, &facet->flow);
1806 /* No new rule, so delete the facet. */
1807 facet_remove(ofproto, facet);
1811 /* Calculate new ODP actions.
1813 * We do not modify any 'facet' state yet, because we might need to, e.g.,
1814 * emit a NetFlow expiration and, if so, we need to have the old state
1815 * around to properly compose it. */
1816 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
1817 odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
1818 actions_changed = (facet->actions_len != odp_actions->size
1819 || memcmp(facet->actions, odp_actions->data,
1820 facet->actions_len));
1822 /* If the ODP actions changed or the installability changed, then we need
1823 * to talk to the datapath. */
1824 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
1825 if (ctx.may_set_up_flow) {
1826 struct dpif_flow_stats stats;
1828 facet_put__(ofproto, facet,
1829 odp_actions->data, odp_actions->size, &stats);
1830 facet_update_stats(ofproto, facet, &stats);
1832 facet_uninstall(ofproto, facet);
1835 /* The datapath flow is gone or has zeroed stats, so push stats out of
1836 * 'facet' into 'rule'. */
1837 facet_flush_stats(ofproto, facet);
1840 /* Update 'facet' now that we've taken care of all the old state. */
1841 facet->tags = ctx.tags;
1842 facet->nf_flow.output_iface = ctx.nf_output_iface;
1843 facet->may_install = ctx.may_set_up_flow;
1844 if (actions_changed) {
1845 free(facet->actions);
1846 facet->actions_len = odp_actions->size;
1847 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1849 if (facet->rule != new_rule) {
1850 COVERAGE_INC(facet_changed_rule);
1851 list_remove(&facet->list_node);
1852 list_push_back(&new_rule->facets, &facet->list_node);
1853 facet->rule = new_rule;
1854 facet->used = new_rule->created;
1855 facet->rs_used = facet->used;
1858 ofpbuf_delete(odp_actions);
1864 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1867 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
1869 COVERAGE_INC(ofproto_error);
1870 ofconn_send_reply(ofconn, buf);
1875 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1877 ofconn_send_reply(ofconn, make_echo_reply(oh));
1882 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1884 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1885 struct ofp_switch_features *osf;
1887 struct ofport *port;
1889 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1890 osf->datapath_id = htonll(ofproto->datapath_id);
1891 osf->n_buffers = htonl(pktbuf_capacity());
1893 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1894 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
1895 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1896 (1u << OFPAT_SET_VLAN_VID) |
1897 (1u << OFPAT_SET_VLAN_PCP) |
1898 (1u << OFPAT_STRIP_VLAN) |
1899 (1u << OFPAT_SET_DL_SRC) |
1900 (1u << OFPAT_SET_DL_DST) |
1901 (1u << OFPAT_SET_NW_SRC) |
1902 (1u << OFPAT_SET_NW_DST) |
1903 (1u << OFPAT_SET_NW_TOS) |
1904 (1u << OFPAT_SET_TP_SRC) |
1905 (1u << OFPAT_SET_TP_DST) |
1906 (1u << OFPAT_ENQUEUE));
1908 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1909 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1912 ofconn_send_reply(ofconn, buf);
1917 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1919 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1921 struct ofp_switch_config *osc;
1925 /* Figure out flags. */
1926 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
1927 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1930 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1931 osc->flags = htons(flags);
1932 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1933 ofconn_send_reply(ofconn, buf);
1939 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1941 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1942 uint16_t flags = ntohs(osc->flags);
1944 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1945 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1946 switch (flags & OFPC_FRAG_MASK) {
1947 case OFPC_FRAG_NORMAL:
1948 dpif_set_drop_frags(ofproto->dpif, false);
1950 case OFPC_FRAG_DROP:
1951 dpif_set_drop_frags(ofproto->dpif, true);
1954 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1960 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
1965 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
1966 struct action_xlate_ctx *ctx);
1969 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
1971 const struct ofport *ofport = get_port(ctx->ofproto, port);
1974 if (ofport->opp.config & OFPPC_NO_FWD) {
1975 /* Forwarding disabled on port. */
1980 * We don't have an ofport record for this port, but it doesn't hurt to
1981 * allow forwarding to it anyhow. Maybe such a port will appear later
1982 * and we're pre-populating the flow table.
1986 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port);
1987 ctx->nf_output_iface = port;
1990 static struct rule *
1991 rule_lookup(struct ofproto *ofproto, const struct flow *flow)
1993 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
1997 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
1999 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2000 uint16_t old_in_port;
2003 /* Look up a flow with 'in_port' as the input port. Then restore the
2004 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2005 * have surprising behavior). */
2006 old_in_port = ctx->flow.in_port;
2007 ctx->flow.in_port = in_port;
2008 rule = rule_lookup(ctx->ofproto, &ctx->flow);
2009 ctx->flow.in_port = old_in_port;
2011 if (ctx->resubmit_hook) {
2012 ctx->resubmit_hook(ctx, rule);
2017 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2021 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2023 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2024 MAX_RESUBMIT_RECURSION);
2029 flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, uint32_t mask,
2030 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2032 struct ofport *ofport;
2034 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
2035 uint16_t odp_port = ofport->odp_port;
2036 if (odp_port != odp_in_port && !(ofport->opp.config & mask)) {
2037 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2040 *nf_output_iface = NF_OUT_FLOOD;
2044 xlate_output_action__(struct action_xlate_ctx *ctx,
2045 uint16_t port, uint16_t max_len)
2048 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2050 ctx->nf_output_iface = NF_OUT_DROP;
2054 add_output_action(ctx, ctx->flow.in_port);
2057 xlate_table_action(ctx, ctx->flow.in_port);
2060 if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
2061 ctx->odp_actions, &ctx->tags,
2062 &ctx->nf_output_iface,
2063 ctx->ofproto->aux)) {
2064 COVERAGE_INC(ofproto_uninstallable);
2065 ctx->may_set_up_flow = false;
2069 flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD,
2070 &ctx->nf_output_iface, ctx->odp_actions);
2073 flood_packets(ctx->ofproto, ctx->flow.in_port, 0,
2074 &ctx->nf_output_iface, ctx->odp_actions);
2076 case OFPP_CONTROLLER:
2077 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2080 add_output_action(ctx, ODPP_LOCAL);
2083 odp_port = ofp_port_to_odp_port(port);
2084 if (odp_port != ctx->flow.in_port) {
2085 add_output_action(ctx, odp_port);
2090 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2091 ctx->nf_output_iface = NF_OUT_FLOOD;
2092 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2093 ctx->nf_output_iface = prev_nf_output_iface;
2094 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2095 ctx->nf_output_iface != NF_OUT_FLOOD) {
2096 ctx->nf_output_iface = NF_OUT_MULTI;
2101 xlate_output_action(struct action_xlate_ctx *ctx,
2102 const struct ofp_action_output *oao)
2104 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2107 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2108 * optimization, because we're going to add another action that sets the
2109 * priority immediately after, or because there are no actions following the
2112 remove_pop_action(struct action_xlate_ctx *ctx)
2114 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2115 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2116 ctx->last_pop_priority = -1;
2121 add_pop_action(struct action_xlate_ctx *ctx)
2123 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2124 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2125 ctx->last_pop_priority = ctx->odp_actions->size;
2130 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2131 const struct ofp_action_enqueue *oae)
2133 uint16_t ofp_port, odp_port;
2137 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2140 /* Fall back to ordinary output action. */
2141 xlate_output_action__(ctx, ntohs(oae->port), 0);
2145 /* Figure out ODP output port. */
2146 ofp_port = ntohs(oae->port);
2147 if (ofp_port != OFPP_IN_PORT) {
2148 odp_port = ofp_port_to_odp_port(ofp_port);
2150 odp_port = ctx->flow.in_port;
2153 /* Add ODP actions. */
2154 remove_pop_action(ctx);
2155 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2156 add_output_action(ctx, odp_port);
2157 add_pop_action(ctx);
2159 /* Update NetFlow output port. */
2160 if (ctx->nf_output_iface == NF_OUT_DROP) {
2161 ctx->nf_output_iface = odp_port;
2162 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2163 ctx->nf_output_iface = NF_OUT_MULTI;
2168 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2169 const struct nx_action_set_queue *nasq)
2174 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2177 /* Couldn't translate queue to a priority, so ignore. A warning
2178 * has already been logged. */
2182 remove_pop_action(ctx);
2183 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2187 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2189 ovs_be16 tci = ctx->flow.vlan_tci;
2190 if (!(tci & htons(VLAN_CFI))) {
2191 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2193 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2194 tci & ~htons(VLAN_CFI));
2198 struct xlate_reg_state {
2204 save_reg_state(const struct action_xlate_ctx *ctx,
2205 struct xlate_reg_state *state)
2207 state->vlan_tci = ctx->flow.vlan_tci;
2208 state->tun_id = ctx->flow.tun_id;
2212 update_reg_state(struct action_xlate_ctx *ctx,
2213 const struct xlate_reg_state *state)
2215 if (ctx->flow.vlan_tci != state->vlan_tci) {
2216 xlate_set_dl_tci(ctx);
2218 if (ctx->flow.tun_id != state->tun_id) {
2219 nl_msg_put_be64(ctx->odp_actions,
2220 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2225 xlate_nicira_action(struct action_xlate_ctx *ctx,
2226 const struct nx_action_header *nah)
2228 const struct nx_action_resubmit *nar;
2229 const struct nx_action_set_tunnel *nast;
2230 const struct nx_action_set_queue *nasq;
2231 const struct nx_action_multipath *nam;
2232 enum nx_action_subtype subtype = ntohs(nah->subtype);
2233 struct xlate_reg_state state;
2236 assert(nah->vendor == htonl(NX_VENDOR_ID));
2238 case NXAST_RESUBMIT:
2239 nar = (const struct nx_action_resubmit *) nah;
2240 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2243 case NXAST_SET_TUNNEL:
2244 nast = (const struct nx_action_set_tunnel *) nah;
2245 tun_id = htonll(ntohl(nast->tun_id));
2246 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2247 ctx->flow.tun_id = tun_id;
2250 case NXAST_DROP_SPOOFED_ARP:
2251 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2252 nl_msg_put_flag(ctx->odp_actions,
2253 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2257 case NXAST_SET_QUEUE:
2258 nasq = (const struct nx_action_set_queue *) nah;
2259 xlate_set_queue_action(ctx, nasq);
2262 case NXAST_POP_QUEUE:
2263 add_pop_action(ctx);
2266 case NXAST_REG_MOVE:
2267 save_reg_state(ctx, &state);
2268 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2270 update_reg_state(ctx, &state);
2273 case NXAST_REG_LOAD:
2274 save_reg_state(ctx, &state);
2275 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2277 update_reg_state(ctx, &state);
2281 /* Nothing to do. */
2284 case NXAST_SET_TUNNEL64:
2285 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2286 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2287 ctx->flow.tun_id = tun_id;
2290 case NXAST_MULTIPATH:
2291 nam = (const struct nx_action_multipath *) nah;
2292 multipath_execute(nam, &ctx->flow);
2295 /* If you add a new action here that modifies flow data, don't forget to
2296 * update the flow key in ctx->flow at the same time. */
2298 case NXAST_SNAT__OBSOLETE:
2300 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2306 do_xlate_actions(const union ofp_action *in, size_t n_in,
2307 struct action_xlate_ctx *ctx)
2309 struct actions_iterator iter;
2310 const union ofp_action *ia;
2311 const struct ofport *port;
2313 port = get_port(ctx->ofproto, ctx->flow.in_port);
2314 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2315 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2316 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
2317 /* Drop this flow. */
2321 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2322 enum ofp_action_type type = ntohs(ia->type);
2323 const struct ofp_action_dl_addr *oada;
2327 xlate_output_action(ctx, &ia->output);
2330 case OFPAT_SET_VLAN_VID:
2331 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2332 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2333 xlate_set_dl_tci(ctx);
2336 case OFPAT_SET_VLAN_PCP:
2337 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2338 ctx->flow.vlan_tci |= htons(
2339 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2340 xlate_set_dl_tci(ctx);
2343 case OFPAT_STRIP_VLAN:
2344 ctx->flow.vlan_tci = htons(0);
2345 xlate_set_dl_tci(ctx);
2348 case OFPAT_SET_DL_SRC:
2349 oada = ((struct ofp_action_dl_addr *) ia);
2350 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2351 oada->dl_addr, ETH_ADDR_LEN);
2352 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
2355 case OFPAT_SET_DL_DST:
2356 oada = ((struct ofp_action_dl_addr *) ia);
2357 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2358 oada->dl_addr, ETH_ADDR_LEN);
2359 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
2362 case OFPAT_SET_NW_SRC:
2363 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
2364 ia->nw_addr.nw_addr);
2365 ctx->flow.nw_src = ia->nw_addr.nw_addr;
2368 case OFPAT_SET_NW_DST:
2369 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
2370 ia->nw_addr.nw_addr);
2371 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
2374 case OFPAT_SET_NW_TOS:
2375 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
2377 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
2380 case OFPAT_SET_TP_SRC:
2381 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
2382 ia->tp_port.tp_port);
2383 ctx->flow.tp_src = ia->tp_port.tp_port;
2386 case OFPAT_SET_TP_DST:
2387 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
2388 ia->tp_port.tp_port);
2389 ctx->flow.tp_dst = ia->tp_port.tp_port;
2393 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2397 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
2401 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
2408 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
2409 struct ofproto *ofproto, const struct flow *flow,
2410 const struct ofpbuf *packet)
2412 ctx->ofproto = ofproto;
2414 ctx->packet = packet;
2415 ctx->resubmit_hook = NULL;
2416 ctx->check_special = true;
2420 ofproto_process_cfm(struct ofproto *ofproto, const struct flow *flow,
2421 const struct ofpbuf *packet)
2423 struct ofport *ofport;
2425 ofport = get_port(ofproto, flow->in_port);
2426 if (ofport && ofport->cfm) {
2427 cfm_process_heartbeat(ofport->cfm, packet);
2431 static struct ofpbuf *
2432 xlate_actions(struct action_xlate_ctx *ctx,
2433 const union ofp_action *in, size_t n_in)
2435 COVERAGE_INC(ofproto_ofp2odp);
2437 ctx->odp_actions = ofpbuf_new(512);
2439 ctx->may_set_up_flow = true;
2440 ctx->nf_output_iface = NF_OUT_DROP;
2442 ctx->last_pop_priority = -1;
2444 if (ctx->check_special && cfm_should_process_flow(&ctx->flow)) {
2446 ofproto_process_cfm(ctx->ofproto, &ctx->flow, ctx->packet);
2448 ctx->may_set_up_flow = false;
2449 } else if (ctx->check_special
2450 && ctx->ofproto->ofhooks->special_cb
2451 && !ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet,
2452 ctx->ofproto->aux)) {
2453 ctx->may_set_up_flow = false;
2455 do_xlate_actions(in, n_in, ctx);
2458 remove_pop_action(ctx);
2460 /* Check with in-band control to see if we're allowed to set up this
2462 if (!connmgr_may_set_up_flow(ctx->ofproto->connmgr, &ctx->flow,
2463 ctx->odp_actions->data,
2464 ctx->odp_actions->size)) {
2465 ctx->may_set_up_flow = false;
2468 return ctx->odp_actions;
2471 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2472 * error message code (composed with ofp_mkerr()) for the caller to propagate
2473 * upward. Otherwise, returns 0.
2475 * The log message mentions 'msg_type'. */
2477 reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
2479 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2480 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
2481 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
2482 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
2485 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2492 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2494 struct ofproto *p = ofconn_get_ofproto(ofconn);
2495 struct ofp_packet_out *opo;
2496 struct ofpbuf payload, *buffer;
2497 union ofp_action *ofp_actions;
2498 struct action_xlate_ctx ctx;
2499 struct ofpbuf *odp_actions;
2500 struct ofpbuf request;
2502 size_t n_ofp_actions;
2506 COVERAGE_INC(ofproto_packet_out);
2508 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
2513 /* Get ofp_packet_out. */
2514 ofpbuf_use_const(&request, oh, ntohs(oh->length));
2515 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
2518 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
2519 &ofp_actions, &n_ofp_actions);
2525 if (opo->buffer_id != htonl(UINT32_MAX)) {
2526 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
2528 if (error || !buffer) {
2537 /* Extract flow, check actions. */
2538 flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
2540 error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
2546 action_xlate_ctx_init(&ctx, p, &flow, &payload);
2547 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
2548 dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
2549 ofpbuf_delete(odp_actions);
2552 ofpbuf_delete(buffer);
2557 update_port_config(struct ofproto *p, struct ofport *port,
2558 uint32_t config, uint32_t mask)
2560 mask &= config ^ port->opp.config;
2561 if (mask & OFPPC_PORT_DOWN) {
2562 if (config & OFPPC_PORT_DOWN) {
2563 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2565 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2568 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
2569 OFPPC_NO_FWD | OFPPC_NO_FLOOD)
2570 if (mask & REVALIDATE_BITS) {
2571 COVERAGE_INC(ofproto_costly_flags);
2572 port->opp.config ^= mask & REVALIDATE_BITS;
2573 p->need_revalidate = true;
2575 #undef REVALIDATE_BITS
2576 if (mask & OFPPC_NO_PACKET_IN) {
2577 port->opp.config ^= OFPPC_NO_PACKET_IN;
2582 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2584 struct ofproto *p = ofconn_get_ofproto(ofconn);
2585 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
2586 struct ofport *port;
2589 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
2594 port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
2596 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2597 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2598 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2600 update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
2601 if (opm->advertise) {
2602 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2608 static struct ofpbuf *
2609 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
2611 struct ofp_stats_reply *osr;
2614 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2615 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2617 osr->flags = htons(0);
2621 static struct ofpbuf *
2622 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
2624 const struct ofp_stats_request *osr
2625 = (const struct ofp_stats_request *) request;
2626 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
2630 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
2631 struct ofpbuf **msgp)
2633 struct ofpbuf *msg = *msgp;
2634 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2635 if (nbytes + msg->size > UINT16_MAX) {
2636 struct ofp_stats_reply *reply = msg->data;
2637 reply->flags = htons(OFPSF_REPLY_MORE);
2638 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
2639 ofconn_send_reply(ofconn, msg);
2641 return ofpbuf_put_uninit(*msgp, nbytes);
2644 static struct ofpbuf *
2645 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
2647 struct nicira_stats_msg *nsm;
2650 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
2651 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
2652 nsm->type = htons(OFPST_VENDOR);
2653 nsm->flags = htons(0);
2654 nsm->vendor = htonl(NX_VENDOR_ID);
2655 nsm->subtype = subtype;
2659 static struct ofpbuf *
2660 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
2662 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
2666 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
2667 struct ofpbuf **msgp)
2669 struct ofpbuf *msg = *msgp;
2670 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
2671 if (nbytes + msg->size > UINT16_MAX) {
2672 struct nicira_stats_msg *reply = msg->data;
2673 reply->flags = htons(OFPSF_REPLY_MORE);
2674 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
2675 ofconn_send_reply(ofconn, msg);
2677 ofpbuf_prealloc_tailroom(*msgp, nbytes);
2681 handle_desc_stats_request(struct ofconn *ofconn,
2682 const struct ofp_header *request)
2684 struct ofproto *p = ofconn_get_ofproto(ofconn);
2685 struct ofp_desc_stats *ods;
2688 msg = start_ofp_stats_reply(request, sizeof *ods);
2689 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
2690 memset(ods, 0, sizeof *ods);
2691 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
2692 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
2693 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
2694 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
2695 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2696 ofconn_send_reply(ofconn, msg);
2702 handle_table_stats_request(struct ofconn *ofconn,
2703 const struct ofp_header *request)
2705 struct ofproto *p = ofconn_get_ofproto(ofconn);
2706 struct ofp_table_stats *ots;
2709 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
2711 /* Classifier table. */
2712 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
2713 memset(ots, 0, sizeof *ots);
2714 strcpy(ots->name, "classifier");
2715 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
2716 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
2717 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
2718 ots->active_count = htonl(classifier_count(&p->cls));
2719 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
2720 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
2722 ofconn_send_reply(ofconn, msg);
2727 append_port_stat(struct ofport *port, struct ofconn *ofconn,
2728 struct ofpbuf **msgp)
2730 struct netdev_stats stats;
2731 struct ofp_port_stats *ops;
2733 /* Intentionally ignore return value, since errors will set
2734 * 'stats' to all-1s, which is correct for OpenFlow, and
2735 * netdev_get_stats() will log errors. */
2736 netdev_get_stats(port->netdev, &stats);
2738 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
2739 ops->port_no = htons(port->opp.port_no);
2740 memset(ops->pad, 0, sizeof ops->pad);
2741 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
2742 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
2743 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
2744 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
2745 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
2746 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
2747 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
2748 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
2749 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
2750 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
2751 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
2752 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
2756 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2758 struct ofproto *p = ofconn_get_ofproto(ofconn);
2759 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
2760 struct ofp_port_stats *ops;
2762 struct ofport *port;
2764 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
2765 if (psr->port_no != htons(OFPP_NONE)) {
2766 port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
2768 append_port_stat(port, ofconn, &msg);
2771 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2772 append_port_stat(port, ofconn, &msg);
2776 ofconn_send_reply(ofconn, msg);
2781 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
2783 long long int msecs = time_msec() - start;
2784 *sec = msecs / 1000;
2785 *nsec = (msecs % 1000) * (1000 * 1000);
2789 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
2793 calc_flow_duration__(start, &sec, &nsec);
2794 *sec_be = htonl(sec);
2795 *nsec_be = htonl(nsec);
2799 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
2800 ovs_be16 out_port, struct ofpbuf **replyp)
2802 struct ofp_flow_stats *ofs;
2803 uint64_t packet_count, byte_count;
2805 size_t act_len, len;
2807 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2811 act_len = sizeof *rule->actions * rule->n_actions;
2812 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2814 rule_get_stats(rule, &packet_count, &byte_count);
2816 ofs = append_ofp_stats_reply(len, ofconn, replyp);
2817 ofs->length = htons(len);
2820 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
2821 &ofs->match, rule->flow_cookie, &cookie);
2822 put_32aligned_be64(&ofs->cookie, cookie);
2823 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
2824 ofs->priority = htons(rule->cr.priority);
2825 ofs->idle_timeout = htons(rule->idle_timeout);
2826 ofs->hard_timeout = htons(rule->hard_timeout);
2827 memset(ofs->pad2, 0, sizeof ofs->pad2);
2828 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
2829 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
2830 if (rule->n_actions > 0) {
2831 memcpy(ofs->actions, rule->actions, act_len);
2836 is_valid_table(uint8_t table_id)
2838 if (table_id == 0 || table_id == 0xff) {
2841 /* It would probably be better to reply with an error but there doesn't
2842 * seem to be any appropriate value, so that might just be
2844 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
2851 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2853 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
2854 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2855 struct ofpbuf *reply;
2857 COVERAGE_INC(ofproto_flows_req);
2858 reply = start_ofp_stats_reply(oh, 1024);
2859 if (is_valid_table(fsr->table_id)) {
2860 struct cls_cursor cursor;
2861 struct cls_rule target;
2864 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
2866 cls_cursor_init(&cursor, &ofproto->cls, &target);
2867 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2868 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
2871 ofconn_send_reply(ofconn, reply);
2877 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
2878 ovs_be16 out_port, struct ofpbuf **replyp)
2880 struct nx_flow_stats *nfs;
2881 uint64_t packet_count, byte_count;
2882 size_t act_len, start_len;
2883 struct ofpbuf *reply;
2885 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2889 rule_get_stats(rule, &packet_count, &byte_count);
2891 act_len = sizeof *rule->actions * rule->n_actions;
2893 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
2894 start_len = (*replyp)->size;
2897 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
2900 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
2901 nfs->cookie = rule->flow_cookie;
2902 nfs->priority = htons(rule->cr.priority);
2903 nfs->idle_timeout = htons(rule->idle_timeout);
2904 nfs->hard_timeout = htons(rule->hard_timeout);
2905 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
2906 memset(nfs->pad2, 0, sizeof nfs->pad2);
2907 nfs->packet_count = htonll(packet_count);
2908 nfs->byte_count = htonll(byte_count);
2909 if (rule->n_actions > 0) {
2910 ofpbuf_put(reply, rule->actions, act_len);
2912 nfs->length = htons(reply->size - start_len);
2916 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
2918 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2919 struct nx_flow_stats_request *nfsr;
2920 struct cls_rule target;
2921 struct ofpbuf *reply;
2925 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2927 /* Dissect the message. */
2928 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2929 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
2934 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2937 COVERAGE_INC(ofproto_flows_req);
2938 reply = start_nxstats_reply(&nfsr->nsm, 1024);
2939 if (is_valid_table(nfsr->table_id)) {
2940 struct cls_cursor cursor;
2943 cls_cursor_init(&cursor, &ofproto->cls, &target);
2944 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2945 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
2948 ofconn_send_reply(ofconn, reply);
2954 flow_stats_ds(struct rule *rule, struct ds *results)
2956 uint64_t packet_count, byte_count;
2957 size_t act_len = sizeof *rule->actions * rule->n_actions;
2959 rule_get_stats(rule, &packet_count, &byte_count);
2961 ds_put_format(results, "duration=%llds, ",
2962 (time_msec() - rule->created) / 1000);
2963 ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0);
2964 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2965 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2966 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2967 cls_rule_format(&rule->cr, results);
2968 ds_put_char(results, ',');
2970 ofp_print_actions(results, &rule->actions->header, act_len);
2972 ds_put_cstr(results, "drop");
2974 ds_put_cstr(results, "\n");
2977 /* Adds a pretty-printed description of all flows to 'results', including
2978 * hidden flows (e.g., set up by in-band control). */
2980 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
2982 struct cls_cursor cursor;
2985 cls_cursor_init(&cursor, &p->cls, NULL);
2986 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2987 flow_stats_ds(rule, results);
2992 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
2993 ovs_be16 out_port, uint8_t table_id,
2994 struct ofp_aggregate_stats_reply *oasr)
2996 uint64_t total_packets = 0;
2997 uint64_t total_bytes = 0;
3000 COVERAGE_INC(ofproto_agg_request);
3002 if (is_valid_table(table_id)) {
3003 struct cls_cursor cursor;
3006 cls_cursor_init(&cursor, &ofproto->cls, target);
3007 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3008 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
3009 uint64_t packet_count;
3010 uint64_t byte_count;
3012 rule_get_stats(rule, &packet_count, &byte_count);
3014 total_packets += packet_count;
3015 total_bytes += byte_count;
3021 oasr->flow_count = htonl(n_flows);
3022 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
3023 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
3024 memset(oasr->pad, 0, sizeof oasr->pad);
3028 handle_aggregate_stats_request(struct ofconn *ofconn,
3029 const struct ofp_header *oh)
3031 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
3032 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3033 struct ofp_aggregate_stats_reply *reply;
3034 struct cls_rule target;
3037 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
3040 msg = start_ofp_stats_reply(oh, sizeof *reply);
3041 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
3042 query_aggregate_stats(ofproto, &target, request->out_port,
3043 request->table_id, reply);
3044 ofconn_send_reply(ofconn, msg);
3049 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
3051 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3052 struct nx_aggregate_stats_request *request;
3053 struct ofp_aggregate_stats_reply *reply;
3054 struct cls_rule target;
3059 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3061 /* Dissect the message. */
3062 request = ofpbuf_pull(&b, sizeof *request);
3063 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
3068 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3072 COVERAGE_INC(ofproto_flows_req);
3073 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
3074 reply = ofpbuf_put_uninit(buf, sizeof *reply);
3075 query_aggregate_stats(ofproto, &target, request->out_port,
3076 request->table_id, reply);
3077 ofconn_send_reply(ofconn, buf);
3082 struct queue_stats_cbdata {
3083 struct ofconn *ofconn;
3084 struct ofport *ofport;
3089 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3090 const struct netdev_queue_stats *stats)
3092 struct ofp_queue_stats *reply;
3094 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
3095 reply->port_no = htons(cbdata->ofport->opp.port_no);
3096 memset(reply->pad, 0, sizeof reply->pad);
3097 reply->queue_id = htonl(queue_id);
3098 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
3099 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
3100 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
3104 handle_queue_stats_dump_cb(uint32_t queue_id,
3105 struct netdev_queue_stats *stats,
3108 struct queue_stats_cbdata *cbdata = cbdata_;
3110 put_queue_stats(cbdata, queue_id, stats);
3114 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3115 struct queue_stats_cbdata *cbdata)
3117 cbdata->ofport = port;
3118 if (queue_id == OFPQ_ALL) {
3119 netdev_dump_queue_stats(port->netdev,
3120 handle_queue_stats_dump_cb, cbdata);
3122 struct netdev_queue_stats stats;
3124 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3125 put_queue_stats(cbdata, queue_id, &stats);
3131 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
3133 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3134 const struct ofp_queue_stats_request *qsr;
3135 struct queue_stats_cbdata cbdata;
3136 struct ofport *port;
3137 unsigned int port_no;
3140 qsr = ofputil_stats_body(oh);
3142 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3145 COVERAGE_INC(ofproto_queue_req);
3147 cbdata.ofconn = ofconn;
3148 cbdata.msg = start_ofp_stats_reply(oh, 128);
3150 port_no = ntohs(qsr->port_no);
3151 queue_id = ntohl(qsr->queue_id);
3152 if (port_no == OFPP_ALL) {
3153 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3154 handle_queue_stats_for_port(port, queue_id, &cbdata);
3156 } else if (port_no < ofproto->max_ports) {
3157 port = get_port(ofproto, ofp_port_to_odp_port(port_no));
3159 handle_queue_stats_for_port(port, queue_id, &cbdata);
3162 ofpbuf_delete(cbdata.msg);
3163 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
3165 ofconn_send_reply(ofconn, cbdata.msg);
3170 /* Updates 'facet''s used time. Caller is responsible for calling
3171 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3173 facet_update_time(struct ofproto *ofproto, struct facet *facet,
3176 if (used > facet->used) {
3178 if (used > facet->rule->used) {
3179 facet->rule->used = used;
3181 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3185 /* Folds the statistics from 'stats' into the counters in 'facet'.
3187 * Because of the meaning of a facet's counters, it only makes sense to do this
3188 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3189 * packet that was sent by hand or if it represents statistics that have been
3190 * cleared out of the datapath. */
3192 facet_update_stats(struct ofproto *ofproto, struct facet *facet,
3193 const struct dpif_flow_stats *stats)
3195 if (stats->n_packets || stats->used > facet->used) {
3196 facet_update_time(ofproto, facet, stats->used);
3197 facet->packet_count += stats->n_packets;
3198 facet->byte_count += stats->n_bytes;
3199 facet_push_stats(ofproto, facet);
3200 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3205 facet_push_stats(struct ofproto *ofproto, struct facet *facet)
3207 uint64_t rs_packets, rs_bytes;
3209 assert(facet->packet_count >= facet->rs_packet_count);
3210 assert(facet->byte_count >= facet->rs_byte_count);
3211 assert(facet->used >= facet->rs_used);
3213 rs_packets = facet->packet_count - facet->rs_packet_count;
3214 rs_bytes = facet->byte_count - facet->rs_byte_count;
3216 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3217 facet->rs_packet_count = facet->packet_count;
3218 facet->rs_byte_count = facet->byte_count;
3219 facet->rs_used = facet->used;
3221 flow_push_stats(ofproto, facet->rule, &facet->flow,
3222 rs_packets, rs_bytes, facet->used);
3226 struct ofproto_push {
3227 struct action_xlate_ctx ctx;
3234 push_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
3236 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3239 rule->packet_count += push->packets;
3240 rule->byte_count += push->bytes;
3241 rule->used = MAX(push->used, rule->used);
3245 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3246 * 'rule''s actions. */
3248 flow_push_stats(struct ofproto *ofproto, const struct rule *rule,
3249 struct flow *flow, uint64_t packets, uint64_t bytes,
3252 struct ofproto_push push;
3254 push.packets = packets;
3258 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3259 push.ctx.resubmit_hook = push_resubmit;
3260 ofpbuf_delete(xlate_actions(&push.ctx, rule->actions, rule->n_actions));
3263 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3264 * in which no matching flow already exists in the flow table.
3266 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3267 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
3268 * OpenFlow error code as encoded by ofp_mkerr() on failure.
3270 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3273 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
3275 struct ofproto *p = ofconn_get_ofproto(ofconn);
3276 struct ofpbuf *packet;
3281 if (fm->flags & OFPFF_CHECK_OVERLAP
3282 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
3283 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
3287 if (fm->buffer_id != UINT32_MAX) {
3288 error = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id,
3292 in_port = UINT16_MAX;
3295 rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
3296 fm->idle_timeout, fm->hard_timeout, fm->cookie,
3297 fm->flags & OFPFF_SEND_FLOW_REM);
3298 rule_insert(p, rule);
3300 rule_execute(p, rule, in_port, packet);
3305 static struct rule *
3306 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
3308 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
3312 send_buffered_packet(struct ofconn *ofconn,
3313 struct rule *rule, uint32_t buffer_id)
3315 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3316 struct ofpbuf *packet;
3320 if (buffer_id == UINT32_MAX) {
3324 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
3329 rule_execute(ofproto, rule, in_port, packet);
3334 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3336 struct modify_flows_cbdata {
3337 struct ofproto *ofproto;
3338 const struct flow_mod *fm;
3342 static int modify_flow(struct ofproto *, const struct flow_mod *,
3345 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
3346 * encoded by ofp_mkerr() on failure.
3348 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3351 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
3353 struct ofproto *p = ofconn_get_ofproto(ofconn);
3354 struct rule *match = NULL;
3355 struct cls_cursor cursor;
3358 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3359 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3360 if (!rule_is_hidden(rule)) {
3362 modify_flow(p, fm, rule);
3367 /* This credits the packet to whichever flow happened to match last.
3368 * That's weird. Maybe we should do a lookup for the flow that
3369 * actually matches the packet? Who knows. */
3370 send_buffered_packet(ofconn, match, fm->buffer_id);
3373 return add_flow(ofconn, fm);
3377 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3378 * code as encoded by ofp_mkerr() on failure.
3380 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3383 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
3385 struct ofproto *p = ofconn_get_ofproto(ofconn);
3386 struct rule *rule = find_flow_strict(p, fm);
3387 if (rule && !rule_is_hidden(rule)) {
3388 modify_flow(p, fm, rule);
3389 return send_buffered_packet(ofconn, rule, fm->buffer_id);
3391 return add_flow(ofconn, fm);
3395 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
3396 * been identified as a flow in 'p''s flow table to be modified, by changing
3397 * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
3398 * ofp_action[] structures). */
3400 modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
3402 size_t actions_len = fm->n_actions * sizeof *rule->actions;
3404 rule->flow_cookie = fm->cookie;
3406 /* If the actions are the same, do nothing. */
3407 if (fm->n_actions == rule->n_actions
3409 || !memcmp(fm->actions, rule->actions, actions_len))) {
3413 /* Replace actions. */
3414 free(rule->actions);
3415 rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
3416 rule->n_actions = fm->n_actions;
3418 p->need_revalidate = true;
3423 /* OFPFC_DELETE implementation. */
3425 static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
3427 /* Implements OFPFC_DELETE. */
3429 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
3431 struct rule *rule, *next_rule;
3432 struct cls_cursor cursor;
3434 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3435 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3436 delete_flow(p, rule, htons(fm->out_port));
3440 /* Implements OFPFC_DELETE_STRICT. */
3442 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
3444 struct rule *rule = find_flow_strict(p, fm);
3446 delete_flow(p, rule, htons(fm->out_port));
3450 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
3451 * been identified as a flow to delete from 'p''s flow table, by deleting the
3452 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
3455 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
3456 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
3457 * specified 'out_port'. */
3459 delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
3461 if (rule_is_hidden(rule)) {
3465 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
3469 rule_send_removed(p, rule, OFPRR_DELETE);
3470 rule_remove(p, rule);
3474 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3476 struct ofproto *p = ofconn_get_ofproto(ofconn);
3480 error = reject_slave_controller(ofconn, "flow_mod");
3485 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
3490 /* We do not support the emergency flow cache. It will hopefully get
3491 * dropped from OpenFlow in the near future. */
3492 if (fm.flags & OFPFF_EMERG) {
3493 /* There isn't a good fit for an error code, so just state that the
3494 * flow table is full. */
3495 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
3498 error = validate_actions(fm.actions, fm.n_actions,
3499 &fm.cr.flow, p->max_ports);
3504 switch (fm.command) {
3506 return add_flow(ofconn, &fm);
3509 return modify_flows_loose(ofconn, &fm);
3511 case OFPFC_MODIFY_STRICT:
3512 return modify_flow_strict(ofconn, &fm);
3515 delete_flows_loose(p, &fm);
3518 case OFPFC_DELETE_STRICT:
3519 delete_flow_strict(p, &fm);
3523 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
3528 handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
3530 const struct nxt_tun_id_cookie *msg
3531 = (const struct nxt_tun_id_cookie *) oh;
3532 enum nx_flow_format flow_format;
3534 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
3535 ofconn_set_flow_format(ofconn, flow_format);
3541 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3543 struct nx_role_request *nrr = (struct nx_role_request *) oh;
3544 struct nx_role_request *reply;
3548 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
3549 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
3550 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3553 role = ntohl(nrr->role);
3554 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
3555 && role != NX_ROLE_SLAVE) {
3556 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
3558 /* There's no good error code for this. */
3559 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
3562 ofconn_set_role(ofconn, role);
3564 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
3565 reply->role = htonl(role);
3566 ofconn_send_reply(ofconn, buf);
3572 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
3574 const struct nxt_set_flow_format *msg
3575 = (const struct nxt_set_flow_format *) oh;
3578 format = ntohl(msg->format);
3579 if (format == NXFF_OPENFLOW10
3580 || format == NXFF_TUN_ID_FROM_COOKIE
3581 || format == NXFF_NXM) {
3582 ofconn_set_flow_format(ofconn, format);
3585 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3590 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
3592 struct ofp_header *ob;
3595 /* Currently, everything executes synchronously, so we can just
3596 * immediately send the barrier reply. */
3597 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
3598 ofconn_send_reply(ofconn, buf);
3603 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
3605 const struct ofp_header *oh = msg->data;
3606 const struct ofputil_msg_type *type;
3609 error = ofputil_decode_msg_type(oh, &type);
3614 switch (ofputil_msg_type_code(type)) {
3615 /* OpenFlow requests. */
3616 case OFPUTIL_OFPT_ECHO_REQUEST:
3617 return handle_echo_request(ofconn, oh);
3619 case OFPUTIL_OFPT_FEATURES_REQUEST:
3620 return handle_features_request(ofconn, oh);
3622 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
3623 return handle_get_config_request(ofconn, oh);
3625 case OFPUTIL_OFPT_SET_CONFIG:
3626 return handle_set_config(ofconn, msg->data);
3628 case OFPUTIL_OFPT_PACKET_OUT:
3629 return handle_packet_out(ofconn, oh);
3631 case OFPUTIL_OFPT_PORT_MOD:
3632 return handle_port_mod(ofconn, oh);
3634 case OFPUTIL_OFPT_FLOW_MOD:
3635 return handle_flow_mod(ofconn, oh);
3637 case OFPUTIL_OFPT_BARRIER_REQUEST:
3638 return handle_barrier_request(ofconn, oh);
3640 /* OpenFlow replies. */
3641 case OFPUTIL_OFPT_ECHO_REPLY:
3644 /* Nicira extension requests. */
3645 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
3646 return handle_tun_id_from_cookie(ofconn, oh);
3648 case OFPUTIL_NXT_ROLE_REQUEST:
3649 return handle_role_request(ofconn, oh);
3651 case OFPUTIL_NXT_SET_FLOW_FORMAT:
3652 return handle_nxt_set_flow_format(ofconn, oh);
3654 case OFPUTIL_NXT_FLOW_MOD:
3655 return handle_flow_mod(ofconn, oh);
3657 /* OpenFlow statistics requests. */
3658 case OFPUTIL_OFPST_DESC_REQUEST:
3659 return handle_desc_stats_request(ofconn, oh);
3661 case OFPUTIL_OFPST_FLOW_REQUEST:
3662 return handle_flow_stats_request(ofconn, oh);
3664 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
3665 return handle_aggregate_stats_request(ofconn, oh);
3667 case OFPUTIL_OFPST_TABLE_REQUEST:
3668 return handle_table_stats_request(ofconn, oh);
3670 case OFPUTIL_OFPST_PORT_REQUEST:
3671 return handle_port_stats_request(ofconn, oh);
3673 case OFPUTIL_OFPST_QUEUE_REQUEST:
3674 return handle_queue_stats_request(ofconn, oh);
3676 /* Nicira extension statistics requests. */
3677 case OFPUTIL_NXST_FLOW_REQUEST:
3678 return handle_nxst_flow(ofconn, oh);
3680 case OFPUTIL_NXST_AGGREGATE_REQUEST:
3681 return handle_nxst_aggregate(ofconn, oh);
3683 case OFPUTIL_INVALID:
3684 case OFPUTIL_OFPT_HELLO:
3685 case OFPUTIL_OFPT_ERROR:
3686 case OFPUTIL_OFPT_FEATURES_REPLY:
3687 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
3688 case OFPUTIL_OFPT_PACKET_IN:
3689 case OFPUTIL_OFPT_FLOW_REMOVED:
3690 case OFPUTIL_OFPT_PORT_STATUS:
3691 case OFPUTIL_OFPT_BARRIER_REPLY:
3692 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
3693 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
3694 case OFPUTIL_OFPST_DESC_REPLY:
3695 case OFPUTIL_OFPST_FLOW_REPLY:
3696 case OFPUTIL_OFPST_QUEUE_REPLY:
3697 case OFPUTIL_OFPST_PORT_REPLY:
3698 case OFPUTIL_OFPST_TABLE_REPLY:
3699 case OFPUTIL_OFPST_AGGREGATE_REPLY:
3700 case OFPUTIL_NXT_ROLE_REPLY:
3701 case OFPUTIL_NXT_FLOW_REMOVED:
3702 case OFPUTIL_NXST_FLOW_REPLY:
3703 case OFPUTIL_NXST_AGGREGATE_REPLY:
3705 if (VLOG_IS_WARN_ENABLED()) {
3706 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3707 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3710 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
3711 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
3713 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3719 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
3721 int error = handle_openflow__(ofconn, ofp_msg);
3723 send_error_oh(ofconn, ofp_msg->data, error);
3725 COVERAGE_INC(ofproto_recv_openflow);
3729 handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3731 struct facet *facet;
3734 /* Obtain in_port and tun_id, at least. */
3735 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3737 /* Set header pointers in 'flow'. */
3738 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
3740 if (cfm_should_process_flow(&flow)) {
3741 ofproto_process_cfm(p, &flow, upcall->packet);
3742 ofpbuf_delete(upcall->packet);
3744 } else if (p->ofhooks->special_cb
3745 && !p->ofhooks->special_cb(&flow, upcall->packet, p->aux)) {
3746 ofpbuf_delete(upcall->packet);
3750 /* Check with in-band control to see if this packet should be sent
3751 * to the local port regardless of the flow table. */
3752 if (connmgr_msg_in_hook(p->connmgr, &flow, upcall->packet)) {
3753 ofproto_send_packet(p, ODPP_LOCAL, 0, upcall->packet);
3756 facet = facet_lookup_valid(p, &flow);
3758 struct rule *rule = rule_lookup(p, &flow);
3760 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3761 struct ofport *port = get_port(p, flow.in_port);
3763 if (port->opp.config & OFPPC_NO_PACKET_IN) {
3764 COVERAGE_INC(ofproto_no_packet_in);
3765 /* XXX install 'drop' flow entry */
3766 ofpbuf_delete(upcall->packet);
3770 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
3774 COVERAGE_INC(ofproto_packet_in);
3775 send_packet_in(p, upcall, &flow, false);
3779 facet = facet_create(p, rule, &flow, upcall->packet);
3780 } else if (!facet->may_install) {
3781 /* The facet is not installable, that is, we need to process every
3782 * packet, so process the current packet's actions into 'facet'. */
3783 facet_make_actions(p, facet, upcall->packet);
3786 if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) {
3788 * Extra-special case for fail-open mode.
3790 * We are in fail-open mode and the packet matched the fail-open rule,
3791 * but we are connected to a controller too. We should send the packet
3792 * up to the controller in the hope that it will try to set up a flow
3793 * and thereby allow us to exit fail-open.
3795 * See the top-level comment in fail-open.c for more information.
3797 send_packet_in(p, upcall, &flow, true);
3800 facet_execute(p, facet, upcall->packet);
3801 facet_install(p, facet, false);
3805 handle_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3809 switch (upcall->type) {
3810 case DPIF_UC_ACTION:
3811 COVERAGE_INC(ofproto_ctlr_action);
3812 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3813 send_packet_in(p, upcall, &flow, false);
3816 case DPIF_UC_SAMPLE:
3818 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3819 ofproto_sflow_received(p->sflow, upcall, &flow);
3821 ofpbuf_delete(upcall->packet);
3825 handle_miss_upcall(p, upcall);
3828 case DPIF_N_UC_TYPES:
3830 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3835 /* Flow expiration. */
3837 static int ofproto_dp_max_idle(const struct ofproto *);
3838 static void ofproto_update_stats(struct ofproto *);
3839 static void rule_expire(struct ofproto *, struct rule *);
3840 static void ofproto_expire_facets(struct ofproto *, int dp_max_idle);
3842 /* This function is called periodically by ofproto_run(). Its job is to
3843 * collect updates for the flows that have been installed into the datapath,
3844 * most importantly when they last were used, and then use that information to
3845 * expire flows that have not been used recently.
3847 * Returns the number of milliseconds after which it should be called again. */
3849 ofproto_expire(struct ofproto *ofproto)
3851 struct rule *rule, *next_rule;
3852 struct cls_cursor cursor;
3855 /* Update stats for each flow in the datapath. */
3856 ofproto_update_stats(ofproto);
3858 /* Expire facets that have been idle too long. */
3859 dp_max_idle = ofproto_dp_max_idle(ofproto);
3860 ofproto_expire_facets(ofproto, dp_max_idle);
3862 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3863 cls_cursor_init(&cursor, &ofproto->cls, NULL);
3864 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3865 rule_expire(ofproto, rule);
3868 /* Let the hook know that we're at a stable point: all outstanding data
3869 * in existing flows has been accounted to the account_cb. Thus, the
3870 * hook can now reasonably do operations that depend on having accurate
3871 * flow volume accounting (currently, that's just bond rebalancing). */
3872 if (ofproto->ofhooks->account_checkpoint_cb) {
3873 ofproto->ofhooks->account_checkpoint_cb(ofproto->aux);
3876 return MIN(dp_max_idle, 1000);
3879 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3881 * This function also pushes statistics updates to rules which each facet
3882 * resubmits into. Generally these statistics will be accurate. However, if a
3883 * facet changes the rule it resubmits into at some time in between
3884 * ofproto_update_stats() runs, it is possible that statistics accrued to the
3885 * old rule will be incorrectly attributed to the new rule. This could be
3886 * avoided by calling ofproto_update_stats() whenever rules are created or
3887 * deleted. However, the performance impact of making so many calls to the
3888 * datapath do not justify the benefit of having perfectly accurate statistics.
3891 ofproto_update_stats(struct ofproto *p)
3893 const struct dpif_flow_stats *stats;
3894 struct dpif_flow_dump dump;
3895 const struct nlattr *key;
3898 dpif_flow_dump_start(&dump, p->dpif);
3899 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3900 struct facet *facet;
3903 if (odp_flow_key_to_flow(key, key_len, &flow)) {
3907 odp_flow_key_format(key, key_len, &s);
3908 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
3914 facet = facet_find(p, &flow);
3916 if (facet && facet->installed) {
3918 if (stats->n_packets >= facet->dp_packet_count) {
3919 facet->packet_count += stats->n_packets - facet->dp_packet_count;
3921 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3924 if (stats->n_bytes >= facet->dp_byte_count) {
3925 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
3927 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3930 facet->dp_packet_count = stats->n_packets;
3931 facet->dp_byte_count = stats->n_bytes;
3933 facet_update_time(p, facet, stats->used);
3934 facet_account(p, facet, stats->n_bytes);
3935 facet_push_stats(p, facet);
3937 /* There's a flow in the datapath that we know nothing about.
3939 COVERAGE_INC(ofproto_unexpected_rule);
3940 dpif_flow_del(p->dpif, key, key_len, NULL);
3943 dpif_flow_dump_done(&dump);
3946 /* Calculates and returns the number of milliseconds of idle time after which
3947 * facets should expire from the datapath and we should fold their statistics
3948 * into their parent rules in userspace. */
3950 ofproto_dp_max_idle(const struct ofproto *ofproto)
3953 * Idle time histogram.
3955 * Most of the time a switch has a relatively small number of facets. When
3956 * this is the case we might as well keep statistics for all of them in
3957 * userspace and to cache them in the kernel datapath for performance as
3960 * As the number of facets increases, the memory required to maintain
3961 * statistics about them in userspace and in the kernel becomes
3962 * significant. However, with a large number of facets it is likely that
3963 * only a few of them are "heavy hitters" that consume a large amount of
3964 * bandwidth. At this point, only heavy hitters are worth caching in the
3965 * kernel and maintaining in userspaces; other facets we can discard.
3967 * The technique used to compute the idle time is to build a histogram with
3968 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
3969 * that is installed in the kernel gets dropped in the appropriate bucket.
3970 * After the histogram has been built, we compute the cutoff so that only
3971 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
3972 * cached. At least the most-recently-used bucket of facets is kept, so
3973 * actually an arbitrary number of facets can be kept in any given
3974 * expiration run (though the next run will delete most of those unless
3975 * they receive additional data).
3977 * This requires a second pass through the facets, in addition to the pass
3978 * made by ofproto_update_stats(), because the former function never looks
3979 * at uninstallable facets.
3981 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
3982 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3983 int buckets[N_BUCKETS] = { 0 };
3984 struct facet *facet;
3989 total = hmap_count(&ofproto->facets);
3990 if (total <= 1000) {
3991 return N_BUCKETS * BUCKET_WIDTH;
3994 /* Build histogram. */
3996 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
3997 long long int idle = now - facet->used;
3998 int bucket = (idle <= 0 ? 0
3999 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4000 : (unsigned int) idle / BUCKET_WIDTH);
4004 /* Find the first bucket whose flows should be expired. */
4005 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
4006 if (buckets[bucket]) {
4009 subtotal += buckets[bucket++];
4010 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
4015 if (VLOG_IS_DBG_ENABLED()) {
4019 ds_put_cstr(&s, "keep");
4020 for (i = 0; i < N_BUCKETS; i++) {
4022 ds_put_cstr(&s, ", drop");
4025 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4028 VLOG_INFO("%s: %s (msec:count)",
4029 dpif_name(ofproto->dpif), ds_cstr(&s));
4033 return bucket * BUCKET_WIDTH;
4037 facet_active_timeout(struct ofproto *ofproto, struct facet *facet)
4039 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
4040 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
4041 struct ofexpired expired;
4043 if (facet->installed) {
4044 struct dpif_flow_stats stats;
4046 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
4048 facet_update_stats(ofproto, facet, &stats);
4051 expired.flow = facet->flow;
4052 expired.packet_count = facet->packet_count;
4053 expired.byte_count = facet->byte_count;
4054 expired.used = facet->used;
4055 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4060 ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle)
4062 long long int cutoff = time_msec() - dp_max_idle;
4063 struct facet *facet, *next_facet;
4065 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
4066 facet_active_timeout(ofproto, facet);
4067 if (facet->used < cutoff) {
4068 facet_remove(ofproto, facet);
4073 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4074 * then delete it entirely. */
4076 rule_expire(struct ofproto *ofproto, struct rule *rule)
4078 struct facet *facet, *next_facet;
4082 /* Has 'rule' expired? */
4084 if (rule->hard_timeout
4085 && now > rule->created + rule->hard_timeout * 1000) {
4086 reason = OFPRR_HARD_TIMEOUT;
4087 } else if (rule->idle_timeout && list_is_empty(&rule->facets)
4088 && now >rule->used + rule->idle_timeout * 1000) {
4089 reason = OFPRR_IDLE_TIMEOUT;
4094 COVERAGE_INC(ofproto_expired);
4096 /* Update stats. (This is a no-op if the rule expired due to an idle
4097 * timeout, because that only happens when the rule has no facets left.) */
4098 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4099 facet_remove(ofproto, facet);
4102 /* Get rid of the rule. */
4103 if (!rule_is_hidden(rule)) {
4104 rule_send_removed(ofproto, rule, reason);
4106 rule_remove(ofproto, rule);
4110 rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason)
4112 struct ofputil_flow_removed fr;
4114 if (!rule->send_flow_removed) {
4119 fr.cookie = rule->flow_cookie;
4121 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
4122 fr.idle_timeout = rule->idle_timeout;
4123 fr.packet_count = rule->packet_count;
4124 fr.byte_count = rule->byte_count;
4126 connmgr_send_flow_removed(p->connmgr, &fr);
4129 /* Obtains statistics for 'rule' and stores them in '*packets' and '*bytes'.
4130 * The returned statistics include statistics for all of 'rule''s facets. */
4132 rule_get_stats(const struct rule *rule, uint64_t *packets, uint64_t *bytes)
4135 struct facet *facet;
4137 /* Start from historical data for 'rule' itself that are no longer tracked
4138 * in facets. This counts, for example, facets that have expired. */
4139 p = rule->packet_count;
4140 b = rule->byte_count;
4142 /* Add any statistics that are tracked by facets. This includes
4143 * statistical data recently updated by ofproto_update_stats() as well as
4144 * stats for packets that were executed "by hand" via dpif_execute(). */
4145 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4146 p += facet->packet_count;
4147 b += facet->byte_count;
4154 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
4155 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
4156 * their individual configurations.
4158 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
4159 * Otherwise, ownership is transferred to this function. */
4161 send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall,
4162 const struct flow *flow, bool clone)
4164 struct ofputil_packet_in pin;
4166 pin.packet = upcall->packet;
4167 pin.in_port = odp_port_to_ofp_port(flow->in_port);
4168 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
4169 pin.buffer_id = 0; /* not yet known */
4170 pin.send_len = upcall->userdata;
4171 connmgr_send_packet_in(ofproto->connmgr, upcall, flow,
4172 clone ? NULL : upcall->packet);
4176 pick_datapath_id(const struct ofproto *ofproto)
4178 const struct ofport *port;
4180 port = get_port(ofproto, ODPP_LOCAL);
4182 uint8_t ea[ETH_ADDR_LEN];
4185 error = netdev_get_etheraddr(port->netdev, ea);
4187 return eth_addr_to_uint64(ea);
4189 VLOG_WARN("could not get MAC address for %s (%s)",
4190 netdev_get_name(port->netdev), strerror(error));
4192 return ofproto->fallback_dpid;
4196 pick_fallback_dpid(void)
4198 uint8_t ea[ETH_ADDR_LEN];
4199 eth_addr_nicira_random(ea);
4200 return eth_addr_to_uint64(ea);
4204 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
4205 void *aux OVS_UNUSED)
4207 const struct shash_node *node;
4211 SHASH_FOR_EACH (node, &all_ofprotos) {
4212 ds_put_format(&results, "%s\n", node->name);
4214 unixctl_command_reply(conn, 200, ds_cstr(&results));
4215 ds_destroy(&results);
4218 struct ofproto_trace {
4219 struct action_xlate_ctx ctx;
4225 trace_format_rule(struct ds *result, int level, const struct rule *rule)
4227 ds_put_char_multiple(result, '\t', level);
4229 ds_put_cstr(result, "No match\n");
4233 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
4234 ntohll(rule->flow_cookie));
4235 cls_rule_format(&rule->cr, result);
4236 ds_put_char(result, '\n');
4238 ds_put_char_multiple(result, '\t', level);
4239 ds_put_cstr(result, "OpenFlow ");
4240 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
4241 rule->n_actions * sizeof *rule->actions);
4242 ds_put_char(result, '\n');
4246 trace_format_flow(struct ds *result, int level, const char *title,
4247 struct ofproto_trace *trace)
4249 ds_put_char_multiple(result, '\t', level);
4250 ds_put_format(result, "%s: ", title);
4251 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4252 ds_put_cstr(result, "unchanged");
4254 flow_format(result, &trace->ctx.flow);
4255 trace->flow = trace->ctx.flow;
4257 ds_put_char(result, '\n');
4261 trace_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
4263 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4264 struct ds *result = trace->result;
4266 ds_put_char(result, '\n');
4267 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4268 trace_format_rule(result, ctx->recurse + 1, rule);
4272 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4273 void *aux OVS_UNUSED)
4275 char *dpname, *in_port_s, *tun_id_s, *packet_s;
4276 char *args = xstrdup(args_);
4277 char *save_ptr = NULL;
4278 struct ofproto *ofproto;
4279 struct ofpbuf packet;
4287 ofpbuf_init(&packet, strlen(args) / 2);
4290 dpname = strtok_r(args, " ", &save_ptr);
4291 tun_id_s = strtok_r(NULL, " ", &save_ptr);
4292 in_port_s = strtok_r(NULL, " ", &save_ptr);
4293 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4294 if (!dpname || !in_port_s || !packet_s) {
4295 unixctl_command_reply(conn, 501, "Bad command syntax");
4299 ofproto = shash_find_data(&all_ofprotos, dpname);
4301 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4306 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
4307 in_port = ofp_port_to_odp_port(atoi(in_port_s));
4309 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
4310 packet_s += strspn(packet_s, " ");
4311 if (*packet_s != '\0') {
4312 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4315 if (packet.size < ETH_HEADER_LEN) {
4316 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
4320 ds_put_cstr(&result, "Packet: ");
4321 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
4322 ds_put_cstr(&result, s);
4325 flow_extract(&packet, tun_id, in_port, &flow);
4326 ds_put_cstr(&result, "Flow: ");
4327 flow_format(&result, &flow);
4328 ds_put_char(&result, '\n');
4330 rule = rule_lookup(ofproto, &flow);
4331 trace_format_rule(&result, 0, rule);
4333 struct ofproto_trace trace;
4334 struct ofpbuf *odp_actions;
4336 trace.result = &result;
4338 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
4339 trace.ctx.resubmit_hook = trace_resubmit;
4340 odp_actions = xlate_actions(&trace.ctx,
4341 rule->actions, rule->n_actions);
4343 ds_put_char(&result, '\n');
4344 trace_format_flow(&result, 0, "Final flow", &trace);
4345 ds_put_cstr(&result, "Datapath actions: ");
4346 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4347 ofpbuf_delete(odp_actions);
4350 unixctl_command_reply(conn, 200, ds_cstr(&result));
4353 ds_destroy(&result);
4354 ofpbuf_uninit(&packet);
4359 ofproto_unixctl_init(void)
4361 static bool registered;
4367 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
4368 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4372 default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
4373 struct ofpbuf *odp_actions, tag_type *tags,
4374 uint16_t *nf_output_iface, void *ofproto_)
4376 struct ofproto *ofproto = ofproto_;
4377 struct mac_entry *dst_mac;
4379 /* Drop frames for reserved multicast addresses. */
4380 if (eth_addr_is_reserved(flow->dl_dst)) {
4384 /* Learn source MAC (but don't try to learn from revalidation). */
4386 && mac_learning_may_learn(ofproto->ml, flow->dl_src, 0)) {
4387 struct mac_entry *src_mac;
4389 src_mac = mac_learning_insert(ofproto->ml, flow->dl_src, 0);
4390 if (mac_entry_is_new(src_mac) || src_mac->port.i != flow->in_port) {
4391 /* The log messages here could actually be useful in debugging,
4392 * so keep the rate limit relatively high. */
4393 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4394 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
4395 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
4397 ofproto_revalidate(ofproto,
4398 mac_learning_changed(ofproto->ml, src_mac));
4399 src_mac->port.i = flow->in_port;
4403 /* Determine output port. */
4404 dst_mac = mac_learning_lookup(ofproto->ml, flow->dl_dst, 0, tags);
4406 flood_packets(ofproto, flow->in_port, OFPPC_NO_FLOOD,
4407 nf_output_iface, odp_actions);
4409 int out_port = dst_mac->port.i;
4410 if (out_port != flow->in_port) {
4411 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, out_port);
4412 *nf_output_iface = out_port;
4421 static const struct ofhooks default_ofhooks = {
4422 default_normal_ofhook_cb,