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"
58 #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 int ofproto_expire(struct ofproto *);
329 static void flow_push_stats(struct ofproto *, const struct rule *,
330 struct flow *, uint64_t packets, uint64_t bytes,
333 static void handle_upcall(struct ofproto *, struct dpif_upcall *);
335 static void handle_openflow(struct ofconn *, struct ofpbuf *);
337 static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
338 static void update_port(struct ofproto *, const char *devname);
339 static int init_ports(struct ofproto *);
340 static void reinit_ports(struct ofproto *);
342 static void ofproto_unixctl_init(void);
345 ofproto_create(const char *datapath, const char *datapath_type,
346 const struct ofhooks *ofhooks, void *aux,
347 struct ofproto **ofprotop)
349 char local_name[IF_NAMESIZE];
356 ofproto_unixctl_init();
358 /* Connect to datapath and start listening for messages. */
359 error = dpif_open(datapath, datapath_type, &dpif);
361 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
364 error = dpif_recv_set_mask(dpif,
365 ((1u << DPIF_UC_MISS) |
366 (1u << DPIF_UC_ACTION) |
367 (1u << DPIF_UC_SAMPLE)));
369 VLOG_ERR("failed to listen on datapath %s: %s",
370 datapath, strerror(error));
374 dpif_flow_flush(dpif);
375 dpif_recv_purge(dpif);
377 error = dpif_port_get_name(dpif, ODPP_LOCAL,
378 local_name, sizeof local_name);
380 VLOG_ERR("%s: cannot get name of datapath local port (%s)",
381 datapath, strerror(error));
385 /* Initialize settings. */
386 p = xzalloc(sizeof *p);
387 p->fallback_dpid = pick_fallback_dpid();
388 p->datapath_id = p->fallback_dpid;
389 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
390 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
391 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
392 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
393 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
395 /* Initialize datapath. */
397 p->netdev_monitor = netdev_monitor_create();
398 hmap_init(&p->ports);
399 shash_init(&p->port_by_name);
400 p->max_ports = dpif_get_max_ports(dpif);
402 /* Initialize submodules. */
406 /* Initialize flow table. */
407 classifier_init(&p->cls);
408 timer_set_duration(&p->next_expiration, 1000);
410 /* Initialize facet table. */
411 hmap_init(&p->facets);
412 p->need_revalidate = false;
413 tag_set_init(&p->revalidate_set);
415 /* Initialize hooks. */
417 p->ofhooks = ofhooks;
421 p->ofhooks = &default_ofhooks;
423 p->ml = mac_learning_create();
426 /* Pick final datapath ID. */
427 p->datapath_id = pick_datapath_id(p);
428 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
430 shash_add_once(&all_ofprotos, dpif_name(p->dpif), p);
432 /* Initialize OpenFlow connections. */
433 p->connmgr = connmgr_create(p, datapath, local_name);
440 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
442 uint64_t old_dpid = p->datapath_id;
443 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
444 if (p->datapath_id != old_dpid) {
445 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
447 /* Force all active connections to reconnect, since there is no way to
448 * notify a controller that the datapath ID has changed. */
449 ofproto_reconnect_controllers(p);
454 ofproto_set_controllers(struct ofproto *p,
455 const struct ofproto_controller *controllers,
456 size_t n_controllers)
458 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
462 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
464 connmgr_set_fail_mode(p->connmgr, fail_mode);
467 /* Drops the connections between 'ofproto' and all of its controllers, forcing
468 * them to reconnect. */
470 ofproto_reconnect_controllers(struct ofproto *ofproto)
472 connmgr_reconnect(ofproto->connmgr);
475 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
476 * in-band control should guarantee access, in the same way that in-band
477 * control guarantees access to OpenFlow controllers. */
479 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
480 const struct sockaddr_in *extras, size_t n)
482 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
485 /* Sets the OpenFlow queue used by flows set up by in-band control on
486 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
487 * flows will use the default queue. */
489 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
491 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
495 ofproto_set_desc(struct ofproto *p,
496 const char *mfr_desc, const char *hw_desc,
497 const char *sw_desc, const char *serial_desc,
500 struct ofp_desc_stats *ods;
503 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
504 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
505 sizeof ods->mfr_desc);
508 p->mfr_desc = xstrdup(mfr_desc);
511 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
512 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
513 sizeof ods->hw_desc);
516 p->hw_desc = xstrdup(hw_desc);
519 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
520 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
521 sizeof ods->sw_desc);
524 p->sw_desc = xstrdup(sw_desc);
527 if (strlen(serial_desc) >= sizeof ods->serial_num) {
528 VLOG_WARN("truncating serial_desc, must be less than %zu "
530 sizeof ods->serial_num);
532 free(p->serial_desc);
533 p->serial_desc = xstrdup(serial_desc);
536 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
537 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
538 sizeof ods->dp_desc);
541 p->dp_desc = xstrdup(dp_desc);
546 ofproto_set_snoops(struct ofproto *ofproto, const struct svec *snoops)
548 return connmgr_set_snoops(ofproto->connmgr, snoops);
552 ofproto_set_netflow(struct ofproto *ofproto,
553 const struct netflow_options *nf_options)
555 if (nf_options && nf_options->collectors.n) {
556 if (!ofproto->netflow) {
557 ofproto->netflow = netflow_create();
559 return netflow_set_options(ofproto->netflow, nf_options);
561 netflow_destroy(ofproto->netflow);
562 ofproto->netflow = NULL;
568 ofproto_set_sflow(struct ofproto *ofproto,
569 const struct ofproto_sflow_options *oso)
571 struct ofproto_sflow *os = ofproto->sflow;
574 struct ofport *ofport;
576 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
577 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
578 ofproto_sflow_add_port(os, ofport->odp_port,
579 netdev_get_name(ofport->netdev));
582 ofproto_sflow_set_options(os, oso);
584 ofproto_sflow_destroy(os);
585 ofproto->sflow = NULL;
589 /* Connectivity Fault Management configuration. */
591 /* Clears the CFM configuration from 'port_no' on 'ofproto'. */
593 ofproto_iface_clear_cfm(struct ofproto *ofproto, uint32_t port_no)
595 struct ofport *ofport = get_port(ofproto, port_no);
596 if (ofport && ofport->cfm){
597 cfm_destroy(ofport->cfm);
602 /* Configures connectivity fault management on 'port_no' in 'ofproto'. Takes
603 * basic configuration from the configuration members in 'cfm', and the set of
604 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
605 * Ignores the statistics members of 'cfm'.
607 * This function has no effect if 'ofproto' does not have a port 'port_no'. */
609 ofproto_iface_set_cfm(struct ofproto *ofproto, uint32_t port_no,
610 const struct cfm *cfm,
611 const uint16_t *remote_mps, size_t n_remote_mps)
613 struct ofport *ofport;
615 ofport = get_port(ofproto, port_no);
617 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
618 dpif_name(ofproto->dpif), port_no);
623 ofport->cfm = cfm_create();
626 ofport->cfm->mpid = cfm->mpid;
627 ofport->cfm->interval = cfm->interval;
628 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
630 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
632 if (!cfm_configure(ofport->cfm)) {
633 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed",
634 dpif_name(ofproto->dpif), port_no,
635 netdev_get_name(ofport->netdev));
636 cfm_destroy(ofport->cfm);
641 /* Returns the connectivity fault management object associated with 'port_no'
642 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
643 * 'port_no' or if that port does not have CFM configured. The caller must not
644 * modify or destroy the returned object. */
646 ofproto_iface_get_cfm(struct ofproto *ofproto, uint32_t port_no)
648 struct ofport *ofport = get_port(ofproto, port_no);
649 return ofport ? ofport->cfm : NULL;
653 ofproto_get_datapath_id(const struct ofproto *ofproto)
655 return ofproto->datapath_id;
659 ofproto_has_primary_controller(const struct ofproto *ofproto)
661 return connmgr_has_controllers(ofproto->connmgr);
664 enum ofproto_fail_mode
665 ofproto_get_fail_mode(const struct ofproto *p)
667 return connmgr_get_fail_mode(p->connmgr);
671 ofproto_get_snoops(const struct ofproto *ofproto, struct svec *snoops)
673 connmgr_get_snoops(ofproto->connmgr, snoops);
677 ofproto_destroy(struct ofproto *p)
679 struct ofport *ofport, *next_ofport;
685 shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif));
687 ofproto_flush_flows(p);
688 connmgr_destroy(p->connmgr);
689 classifier_destroy(&p->cls);
690 hmap_destroy(&p->facets);
693 netdev_monitor_destroy(p->netdev_monitor);
694 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
695 hmap_remove(&p->ports, &ofport->hmap_node);
698 shash_destroy(&p->port_by_name);
700 netflow_destroy(p->netflow);
701 ofproto_sflow_destroy(p->sflow);
703 mac_learning_destroy(p->ml);
708 free(p->serial_desc);
711 hmap_destroy(&p->ports);
717 ofproto_run(struct ofproto *p)
719 int error = ofproto_run1(p);
721 error = ofproto_run2(p, false);
727 process_port_change(struct ofproto *ofproto, int error, char *devname)
729 if (error == ENOBUFS) {
730 reinit_ports(ofproto);
732 update_port(ofproto, devname);
738 ofproto_run1(struct ofproto *p)
740 struct ofport *ofport;
745 if (shash_is_empty(&p->port_by_name)) {
749 for (i = 0; i < 50; i++) {
750 struct dpif_upcall packet;
752 error = dpif_recv(p->dpif, &packet);
754 if (error == ENODEV) {
755 /* Someone destroyed the datapath behind our back. The caller
756 * better destroy us and give up, because we're just going to
757 * spin from here on out. */
758 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
759 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
766 handle_upcall(p, &packet);
769 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
770 process_port_change(p, error, devname);
772 while ((error = netdev_monitor_poll(p->netdev_monitor,
773 &devname)) != EAGAIN) {
774 process_port_change(p, error, devname);
777 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
778 ofport_run(p, ofport);
781 connmgr_run(p->connmgr, handle_openflow);
783 if (timer_expired(&p->next_expiration)) {
784 int delay = ofproto_expire(p);
785 timer_set_duration(&p->next_expiration, delay);
786 COVERAGE_INC(ofproto_expiration);
790 netflow_run(p->netflow);
793 ofproto_sflow_run(p->sflow);
800 ofproto_run2(struct ofproto *p, bool revalidate_all)
802 /* Figure out what we need to revalidate now, if anything. */
803 struct tag_set revalidate_set = p->revalidate_set;
804 if (p->need_revalidate) {
805 revalidate_all = true;
808 /* Clear the revalidation flags. */
809 tag_set_init(&p->revalidate_set);
810 p->need_revalidate = false;
812 /* Now revalidate if there's anything to do. */
813 if (revalidate_all || !tag_set_is_empty(&revalidate_set)) {
814 struct facet *facet, *next;
816 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) {
818 || tag_set_intersects(&revalidate_set, facet->tags)) {
819 facet_revalidate(p, facet);
828 ofproto_wait(struct ofproto *p)
830 struct ofport *ofport;
832 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
835 dpif_recv_wait(p->dpif);
836 dpif_port_poll_wait(p->dpif);
837 netdev_monitor_poll_wait(p->netdev_monitor);
839 ofproto_sflow_wait(p->sflow);
841 if (!tag_set_is_empty(&p->revalidate_set)) {
842 poll_immediate_wake();
844 if (p->need_revalidate) {
845 /* Shouldn't happen, but if it does just go around again. */
846 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
847 poll_immediate_wake();
849 timer_wait(&p->next_expiration);
851 connmgr_wait(p->connmgr);
855 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
857 tag_set_add(&ofproto->revalidate_set, tag);
861 ofproto_get_revalidate_set(struct ofproto *ofproto)
863 return &ofproto->revalidate_set;
867 ofproto_is_alive(const struct ofproto *p)
869 return connmgr_has_controllers(p->connmgr);
873 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
876 connmgr_get_controller_info(ofproto->connmgr, info);
880 ofproto_free_ofproto_controller_info(struct shash *info)
882 struct shash_node *node;
884 SHASH_FOR_EACH (node, info) {
885 struct ofproto_controller_info *cinfo = node->data;
886 while (cinfo->pairs.n) {
887 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
894 /* Deletes port number 'odp_port' from the datapath for 'ofproto'.
896 * This is almost the same as calling dpif_port_del() directly on the
897 * datapath, but it also makes 'ofproto' close its open netdev for the port
898 * (if any). This makes it possible to create a new netdev of a different
899 * type under the same name, which otherwise the netdev library would refuse
900 * to do because of the conflict. (The netdev would eventually get closed on
901 * the next trip through ofproto_run(), but this interface is more direct.)
903 * Returns 0 if successful, otherwise a positive errno. */
905 ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port)
907 struct ofport *ofport = get_port(ofproto, odp_port);
908 const char *name = ofport ? ofport->opp.name : "<unknown>";
911 error = dpif_port_del(ofproto->dpif, odp_port);
913 VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)",
914 dpif_name(ofproto->dpif), odp_port, name, strerror(error));
916 /* 'name' is ofport->opp.name and update_port() is going to destroy
917 * 'ofport'. Just in case update_port() refers to 'name' after it
918 * destroys 'ofport', make a copy of it around the update_port()
920 char *devname = xstrdup(name);
921 update_port(ofproto, devname);
927 /* Checks if 'ofproto' thinks 'odp_port' should be included in floods. Returns
928 * true if 'odp_port' exists and should be included, false otherwise. */
930 ofproto_port_is_floodable(struct ofproto *ofproto, uint16_t odp_port)
932 struct ofport *ofport = get_port(ofproto, odp_port);
933 return ofport && !(ofport->opp.config & OFPPC_NO_FLOOD);
936 /* Sends 'packet' out of port 'port_no' within 'p'. If 'vlan_tci' is zero the
937 * packet will not have any 802.1Q hader; if it is nonzero, then the packet
938 * will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
940 * Returns 0 if successful, otherwise a positive errno value. */
942 ofproto_send_packet(struct ofproto *ofproto,
943 uint32_t port_no, uint16_t vlan_tci,
944 const struct ofpbuf *packet)
946 struct ofpbuf odp_actions;
949 ofpbuf_init(&odp_actions, 32);
951 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
952 ntohs(vlan_tci & ~VLAN_CFI));
954 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, port_no);
955 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
957 ofpbuf_uninit(&odp_actions);
960 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
961 dpif_name(ofproto->dpif), port_no, strerror(error));
966 /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
967 * performs the 'n_actions' actions in 'actions'. The new flow will not
970 * If cls_rule->priority is in the range of priorities supported by OpenFlow
971 * (0...65535, inclusive) then the flow will be visible to OpenFlow
972 * controllers; otherwise, it will be hidden.
974 * The caller retains ownership of 'cls_rule' and 'actions'. */
976 ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
977 const union ofp_action *actions, size_t n_actions)
980 rule = rule_create(cls_rule, actions, n_actions, 0, 0, 0, false);
981 rule_insert(p, rule);
985 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
989 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
992 rule_remove(ofproto, rule);
997 ofproto_flush_flows(struct ofproto *ofproto)
999 struct facet *facet, *next_facet;
1000 struct rule *rule, *next_rule;
1001 struct cls_cursor cursor;
1003 COVERAGE_INC(ofproto_flush);
1005 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1006 /* Mark the facet as not installed so that facet_remove() doesn't
1007 * bother trying to uninstall it. There is no point in uninstalling it
1008 * individually since we are about to blow away all the facets with
1009 * dpif_flow_flush(). */
1010 facet->installed = false;
1011 facet->dp_packet_count = 0;
1012 facet->dp_byte_count = 0;
1013 facet_remove(ofproto, facet);
1016 cls_cursor_init(&cursor, &ofproto->cls, NULL);
1017 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1018 rule_remove(ofproto, rule);
1021 dpif_flow_flush(ofproto->dpif);
1022 connmgr_flushed(ofproto->connmgr);
1026 reinit_ports(struct ofproto *p)
1028 struct dpif_port_dump dump;
1029 struct shash_node *node;
1030 struct shash devnames;
1031 struct ofport *ofport;
1032 struct dpif_port dpif_port;
1034 COVERAGE_INC(ofproto_reinit_ports);
1036 shash_init(&devnames);
1037 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1038 shash_add_once (&devnames, ofport->opp.name, NULL);
1040 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1041 shash_add_once (&devnames, dpif_port.name, NULL);
1044 SHASH_FOR_EACH (node, &devnames) {
1045 update_port(p, node->name);
1047 shash_destroy(&devnames);
1050 static struct ofport *
1051 make_ofport(const struct dpif_port *dpif_port)
1053 struct netdev_options netdev_options;
1054 enum netdev_flags flags;
1055 struct ofport *ofport;
1056 struct netdev *netdev;
1059 memset(&netdev_options, 0, sizeof netdev_options);
1060 netdev_options.name = dpif_port->name;
1061 netdev_options.type = dpif_port->type;
1062 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
1064 error = netdev_open(&netdev_options, &netdev);
1066 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1067 "cannot be opened (%s)",
1068 dpif_port->name, dpif_port->port_no,
1069 dpif_port->name, strerror(error));
1073 ofport = xzalloc(sizeof *ofport);
1074 ofport->netdev = netdev;
1075 ofport->odp_port = dpif_port->port_no;
1076 ofport->opp.port_no = odp_port_to_ofp_port(dpif_port->port_no);
1077 netdev_get_etheraddr(netdev, ofport->opp.hw_addr);
1078 ovs_strlcpy(ofport->opp.name, dpif_port->name, sizeof ofport->opp.name);
1080 netdev_get_flags(netdev, &flags);
1081 ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
1083 ofport->opp.state = netdev_get_carrier(netdev) ? 0 : OFPPS_LINK_DOWN;
1085 netdev_get_features(netdev,
1086 &ofport->opp.curr, &ofport->opp.advertised,
1087 &ofport->opp.supported, &ofport->opp.peer);
1092 ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port)
1094 if (get_port(p, dpif_port->port_no)) {
1095 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1096 dpif_port->port_no);
1098 } else if (shash_find(&p->port_by_name, dpif_port->name)) {
1099 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1108 ofport_equal(const struct ofport *a_, const struct ofport *b_)
1110 const struct ofp_phy_port *a = &a_->opp;
1111 const struct ofp_phy_port *b = &b_->opp;
1113 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1114 return (a->port_no == b->port_no
1115 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1116 && !strcmp(a->name, b->name)
1117 && a->state == b->state
1118 && a->config == b->config
1119 && a->curr == b->curr
1120 && a->advertised == b->advertised
1121 && a->supported == b->supported
1122 && a->peer == b->peer);
1126 ofport_install(struct ofproto *p, struct ofport *ofport)
1128 const char *netdev_name = ofport->opp.name;
1130 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1131 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
1132 shash_add(&p->port_by_name, netdev_name, ofport);
1134 ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
1139 ofport_remove(struct ofproto *p, struct ofport *ofport)
1141 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1142 hmap_remove(&p->ports, &ofport->hmap_node);
1143 shash_delete(&p->port_by_name,
1144 shash_find(&p->port_by_name, ofport->opp.name));
1146 ofproto_sflow_del_port(p->sflow, ofport->odp_port);
1151 ofport_run(struct ofproto *ofproto, struct ofport *ofport)
1154 cfm_run(ofport->cfm);
1156 if (cfm_should_send_ccm(ofport->cfm)) {
1157 struct ofpbuf packet;
1160 ofpbuf_init(&packet, 0);
1161 ccm = compose_packet(&packet, eth_addr_ccm, ofport->opp.hw_addr,
1162 ETH_TYPE_CFM, sizeof *ccm);
1163 cfm_compose_ccm(ofport->cfm, ccm);
1164 ofproto_send_packet(ofproto, ofport->odp_port, 0, &packet);
1165 ofpbuf_uninit(&packet);
1171 ofport_wait(struct ofport *ofport)
1174 cfm_wait(ofport->cfm);
1179 ofport_free(struct ofport *ofport)
1182 cfm_destroy(ofport->cfm);
1183 netdev_close(ofport->netdev);
1188 static struct ofport *
1189 get_port(const struct ofproto *ofproto, uint16_t odp_port)
1191 struct ofport *port;
1193 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1194 hash_int(odp_port, 0), &ofproto->ports) {
1195 if (port->odp_port == odp_port) {
1203 update_port(struct ofproto *p, const char *devname)
1205 struct dpif_port dpif_port;
1206 struct ofport *old_ofport;
1207 struct ofport *new_ofport;
1210 COVERAGE_INC(ofproto_update_port);
1212 /* Query the datapath for port information. */
1213 error = dpif_port_query_by_name(p->dpif, devname, &dpif_port);
1215 /* Find the old ofport. */
1216 old_ofport = shash_find_data(&p->port_by_name, devname);
1219 /* There's no port named 'devname' but there might be a port with
1220 * the same port number. This could happen if a port is deleted
1221 * and then a new one added in its place very quickly, or if a port
1222 * is renamed. In the former case we want to send an OFPPR_DELETE
1223 * and an OFPPR_ADD, and in the latter case we want to send a
1224 * single OFPPR_MODIFY. We can distinguish the cases by comparing
1225 * the old port's ifindex against the new port, or perhaps less
1226 * reliably but more portably by comparing the old port's MAC
1227 * against the new port's MAC. However, this code isn't that smart
1228 * and always sends an OFPPR_MODIFY (XXX). */
1229 old_ofport = get_port(p, dpif_port.port_no);
1231 } else if (error != ENOENT && error != ENODEV) {
1232 VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error "
1233 "%s", strerror(error));
1237 /* Create a new ofport. */
1238 new_ofport = !error ? make_ofport(&dpif_port) : NULL;
1240 /* Eliminate a few pathological cases. */
1241 if (!old_ofport && !new_ofport) {
1243 } else if (old_ofport && new_ofport) {
1244 /* Most of the 'config' bits are OpenFlow soft state, but
1245 * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
1246 * OpenFlow bits from old_ofport. (make_ofport() only sets
1247 * OFPPC_PORT_DOWN and leaves the other bits 0.) */
1248 new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN;
1250 if (ofport_equal(old_ofport, new_ofport)) {
1251 /* False alarm--no change. */
1252 ofport_free(new_ofport);
1257 /* Now deal with the normal cases. */
1259 ofport_remove(p, old_ofport);
1262 ofport_install(p, new_ofport);
1264 connmgr_send_port_status(p->connmgr,
1265 new_ofport ? &new_ofport->opp : &old_ofport->opp,
1266 (!old_ofport ? OFPPR_ADD
1267 : !new_ofport ? OFPPR_DELETE
1269 ofport_free(old_ofport);
1272 dpif_port_destroy(&dpif_port);
1276 init_ports(struct ofproto *p)
1278 struct dpif_port_dump dump;
1279 struct dpif_port dpif_port;
1281 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1282 if (!ofport_conflicts(p, &dpif_port)) {
1283 struct ofport *ofport = make_ofport(&dpif_port);
1285 ofport_install(p, ofport);
1293 /* Returns true if 'rule' should be hidden from the controller.
1295 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1296 * (e.g. by in-band control) and are intentionally hidden from the
1299 rule_is_hidden(const struct rule *rule)
1301 return rule->cr.priority > UINT16_MAX;
1304 /* Creates and returns a new rule initialized as specified.
1306 * The caller is responsible for inserting the rule into the classifier (with
1307 * rule_insert()). */
1308 static struct rule *
1309 rule_create(const struct cls_rule *cls_rule,
1310 const union ofp_action *actions, size_t n_actions,
1311 uint16_t idle_timeout, uint16_t hard_timeout,
1312 ovs_be64 flow_cookie, bool send_flow_removed)
1314 struct rule *rule = xzalloc(sizeof *rule);
1315 rule->cr = *cls_rule;
1316 rule->idle_timeout = idle_timeout;
1317 rule->hard_timeout = hard_timeout;
1318 rule->flow_cookie = flow_cookie;
1319 rule->used = rule->created = time_msec();
1320 rule->send_flow_removed = send_flow_removed;
1321 list_init(&rule->facets);
1322 if (n_actions > 0) {
1323 rule->n_actions = n_actions;
1324 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1330 static struct rule *
1331 rule_from_cls_rule(const struct cls_rule *cls_rule)
1333 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1337 rule_free(struct rule *rule)
1339 free(rule->actions);
1343 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
1344 * destroying any that no longer has a rule (which is probably all of them).
1346 * The caller must have already removed 'rule' from the classifier. */
1348 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1350 struct facet *facet, *next_facet;
1351 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1352 facet_revalidate(ofproto, facet);
1357 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1358 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1361 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
1363 const union ofp_action *oa;
1364 struct actions_iterator i;
1366 if (out_port == htons(OFPP_NONE)) {
1369 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1370 oa = actions_next(&i)) {
1371 if (action_outputs_to_port(oa, out_port)) {
1378 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1379 * 'packet', which arrived on 'in_port'.
1381 * Takes ownership of 'packet'. */
1383 execute_odp_actions(struct ofproto *ofproto, const struct flow *flow,
1384 const struct nlattr *odp_actions, size_t actions_len,
1385 struct ofpbuf *packet)
1387 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1388 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1389 /* As an optimization, avoid a round-trip from userspace to kernel to
1390 * userspace. This also avoids possibly filling up kernel packet
1391 * buffers along the way. */
1392 struct dpif_upcall upcall;
1394 upcall.type = DPIF_UC_ACTION;
1395 upcall.packet = packet;
1398 upcall.userdata = nl_attr_get_u64(odp_actions);
1399 upcall.sample_pool = 0;
1400 upcall.actions = NULL;
1401 upcall.actions_len = 0;
1403 send_packet_in(ofproto, &upcall, flow, false);
1409 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
1410 ofpbuf_delete(packet);
1415 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
1416 * statistics appropriately. 'packet' must have at least sizeof(struct
1417 * ofp_packet_in) bytes of headroom.
1419 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
1420 * applying flow_extract() to 'packet' would yield the same flow as
1423 * 'facet' must have accurately composed ODP actions; that is, it must not be
1424 * in need of revalidation.
1426 * Takes ownership of 'packet'. */
1428 facet_execute(struct ofproto *ofproto, struct facet *facet,
1429 struct ofpbuf *packet)
1431 struct dpif_flow_stats stats;
1433 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1435 flow_extract_stats(&facet->flow, packet, &stats);
1436 stats.used = time_msec();
1437 if (execute_odp_actions(ofproto, &facet->flow,
1438 facet->actions, facet->actions_len, packet)) {
1439 facet_update_stats(ofproto, facet, &stats);
1443 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1444 * statistics (or the statistics for one of its facets) appropriately.
1445 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
1447 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1448 * with statistics for 'packet' either way.
1450 * Takes ownership of 'packet'. */
1452 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
1453 struct ofpbuf *packet)
1455 struct action_xlate_ctx ctx;
1456 struct ofpbuf *odp_actions;
1457 struct facet *facet;
1461 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1463 flow_extract(packet, 0, in_port, &flow);
1465 /* First look for a related facet. If we find one, account it to that. */
1466 facet = facet_lookup_valid(ofproto, &flow);
1467 if (facet && facet->rule == rule) {
1468 facet_execute(ofproto, facet, packet);
1472 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
1473 * create a new facet for it and use that. */
1474 if (rule_lookup(ofproto, &flow) == rule) {
1475 facet = facet_create(ofproto, rule, &flow, packet);
1476 facet_execute(ofproto, facet, packet);
1477 facet_install(ofproto, facet, true);
1481 /* We can't account anything to a facet. If we were to try, then that
1482 * facet would have a non-matching rule, busting our invariants. */
1483 action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
1484 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1485 size = packet->size;
1486 if (execute_odp_actions(ofproto, &flow, odp_actions->data,
1487 odp_actions->size, packet)) {
1488 rule->used = time_msec();
1489 rule->packet_count++;
1490 rule->byte_count += size;
1491 flow_push_stats(ofproto, rule, &flow, 1, size, rule->used);
1493 ofpbuf_delete(odp_actions);
1496 /* Inserts 'rule' into 'p''s flow table. */
1498 rule_insert(struct ofproto *p, struct rule *rule)
1500 struct rule *displaced_rule;
1502 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1503 if (displaced_rule) {
1504 rule_destroy(p, displaced_rule);
1506 p->need_revalidate = true;
1509 /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a
1510 * 'flow' and an example 'packet' within that flow.
1512 * The caller must already have determined that no facet with an identical
1513 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1514 * 'ofproto''s classifier table. */
1515 static struct facet *
1516 facet_create(struct ofproto *ofproto, struct rule *rule,
1517 const struct flow *flow, const struct ofpbuf *packet)
1519 struct facet *facet;
1521 facet = xzalloc(sizeof *facet);
1522 facet->used = time_msec();
1523 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1524 list_push_back(&rule->facets, &facet->list_node);
1526 facet->flow = *flow;
1527 netflow_flow_init(&facet->nf_flow);
1528 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1530 facet_make_actions(ofproto, facet, packet);
1536 facet_free(struct facet *facet)
1538 free(facet->actions);
1542 /* Remove 'rule' from 'ofproto' and free up the associated memory:
1544 * - Removes 'rule' from the classifier.
1546 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
1547 * destroys them), via rule_destroy().
1550 rule_remove(struct ofproto *ofproto, struct rule *rule)
1552 COVERAGE_INC(ofproto_del_rule);
1553 ofproto->need_revalidate = true;
1554 classifier_remove(&ofproto->cls, &rule->cr);
1555 rule_destroy(ofproto, rule);
1558 /* Remove 'facet' from 'ofproto' and free up the associated memory:
1560 * - If 'facet' was installed in the datapath, uninstalls it and updates its
1561 * rule's statistics, via facet_uninstall().
1563 * - Removes 'facet' from its rule and from ofproto->facets.
1566 facet_remove(struct ofproto *ofproto, struct facet *facet)
1568 facet_uninstall(ofproto, facet);
1569 facet_flush_stats(ofproto, facet);
1570 hmap_remove(&ofproto->facets, &facet->hmap_node);
1571 list_remove(&facet->list_node);
1575 /* Composes the ODP actions for 'facet' based on its rule's actions. */
1577 facet_make_actions(struct ofproto *p, struct facet *facet,
1578 const struct ofpbuf *packet)
1580 const struct rule *rule = facet->rule;
1581 struct ofpbuf *odp_actions;
1582 struct action_xlate_ctx ctx;
1584 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
1585 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1586 facet->tags = ctx.tags;
1587 facet->may_install = ctx.may_set_up_flow;
1588 facet->nf_flow.output_iface = ctx.nf_output_iface;
1590 if (facet->actions_len != odp_actions->size
1591 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
1592 free(facet->actions);
1593 facet->actions_len = odp_actions->size;
1594 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1597 ofpbuf_delete(odp_actions);
1601 facet_put__(struct ofproto *ofproto, struct facet *facet,
1602 const struct nlattr *actions, size_t actions_len,
1603 struct dpif_flow_stats *stats)
1605 struct odputil_keybuf keybuf;
1606 enum dpif_flow_put_flags flags;
1609 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1611 flags |= DPIF_FP_ZERO_STATS;
1612 facet->dp_packet_count = 0;
1613 facet->dp_byte_count = 0;
1616 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1617 odp_flow_key_from_flow(&key, &facet->flow);
1619 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
1620 actions, actions_len, stats);
1623 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
1624 * 'zero_stats' is true, clears any existing statistics from the datapath for
1627 facet_install(struct ofproto *p, struct facet *facet, bool zero_stats)
1629 struct dpif_flow_stats stats;
1631 if (facet->may_install
1632 && !facet_put__(p, facet, facet->actions, facet->actions_len,
1633 zero_stats ? &stats : NULL)) {
1634 facet->installed = true;
1638 /* Ensures that the bytes in 'facet', plus 'extra_bytes', have been passed up
1639 * to the accounting hook function in the ofhooks structure. */
1641 facet_account(struct ofproto *ofproto,
1642 struct facet *facet, uint64_t extra_bytes)
1644 uint64_t total_bytes = facet->byte_count + extra_bytes;
1646 if (ofproto->ofhooks->account_flow_cb
1647 && total_bytes > facet->accounted_bytes)
1649 ofproto->ofhooks->account_flow_cb(
1650 &facet->flow, facet->tags, facet->actions, facet->actions_len,
1651 total_bytes - facet->accounted_bytes, ofproto->aux);
1652 facet->accounted_bytes = total_bytes;
1656 /* If 'rule' is installed in the datapath, uninstalls it. */
1658 facet_uninstall(struct ofproto *p, struct facet *facet)
1660 if (facet->installed) {
1661 struct odputil_keybuf keybuf;
1662 struct dpif_flow_stats stats;
1665 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1666 odp_flow_key_from_flow(&key, &facet->flow);
1668 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
1669 facet_update_stats(p, facet, &stats);
1671 facet->installed = false;
1672 facet->dp_packet_count = 0;
1673 facet->dp_byte_count = 0;
1675 assert(facet->dp_packet_count == 0);
1676 assert(facet->dp_byte_count == 0);
1680 /* Returns true if the only action for 'facet' is to send to the controller.
1681 * (We don't report NetFlow expiration messages for such facets because they
1682 * are just part of the control logic for the network, not real traffic). */
1684 facet_is_controller_flow(struct facet *facet)
1687 && facet->rule->n_actions == 1
1688 && action_outputs_to_port(&facet->rule->actions[0],
1689 htons(OFPP_CONTROLLER)));
1692 /* Folds all of 'facet''s statistics into its rule. Also updates the
1693 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
1694 * 'facet''s statistics in the datapath should have been zeroed and folded into
1695 * its packet and byte counts before this function is called. */
1697 facet_flush_stats(struct ofproto *ofproto, struct facet *facet)
1699 assert(!facet->dp_byte_count);
1700 assert(!facet->dp_packet_count);
1702 facet_push_stats(ofproto, facet);
1703 facet_account(ofproto, facet, 0);
1705 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
1706 struct ofexpired expired;
1707 expired.flow = facet->flow;
1708 expired.packet_count = facet->packet_count;
1709 expired.byte_count = facet->byte_count;
1710 expired.used = facet->used;
1711 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1714 facet->rule->packet_count += facet->packet_count;
1715 facet->rule->byte_count += facet->byte_count;
1717 /* Reset counters to prevent double counting if 'facet' ever gets
1719 facet->packet_count = 0;
1720 facet->byte_count = 0;
1721 facet->rs_packet_count = 0;
1722 facet->rs_byte_count = 0;
1723 facet->accounted_bytes = 0;
1725 netflow_flow_clear(&facet->nf_flow);
1728 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1729 * Returns it if found, otherwise a null pointer.
1731 * The returned facet might need revalidation; use facet_lookup_valid()
1732 * instead if that is important. */
1733 static struct facet *
1734 facet_find(struct ofproto *ofproto, const struct flow *flow)
1736 struct facet *facet;
1738 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
1740 if (flow_equal(flow, &facet->flow)) {
1748 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1749 * Returns it if found, otherwise a null pointer.
1751 * The returned facet is guaranteed to be valid. */
1752 static struct facet *
1753 facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
1755 struct facet *facet = facet_find(ofproto, flow);
1757 /* The facet we found might not be valid, since we could be in need of
1758 * revalidation. If it is not valid, don't return it. */
1760 && ofproto->need_revalidate
1761 && !facet_revalidate(ofproto, facet)) {
1762 COVERAGE_INC(ofproto_invalidated);
1769 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
1771 * - If the rule found is different from 'facet''s current rule, moves
1772 * 'facet' to the new rule and recompiles its actions.
1774 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
1775 * where it is and recompiles its actions anyway.
1777 * - If there is none, destroys 'facet'.
1779 * Returns true if 'facet' still exists, false if it has been destroyed. */
1781 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
1783 struct action_xlate_ctx ctx;
1784 struct ofpbuf *odp_actions;
1785 struct rule *new_rule;
1786 bool actions_changed;
1788 COVERAGE_INC(facet_revalidate);
1790 /* Determine the new rule. */
1791 new_rule = rule_lookup(ofproto, &facet->flow);
1793 /* No new rule, so delete the facet. */
1794 facet_remove(ofproto, facet);
1798 /* Calculate new ODP actions.
1800 * We do not modify any 'facet' state yet, because we might need to, e.g.,
1801 * emit a NetFlow expiration and, if so, we need to have the old state
1802 * around to properly compose it. */
1803 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
1804 odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
1805 actions_changed = (facet->actions_len != odp_actions->size
1806 || memcmp(facet->actions, odp_actions->data,
1807 facet->actions_len));
1809 /* If the ODP actions changed or the installability changed, then we need
1810 * to talk to the datapath. */
1811 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
1812 if (ctx.may_set_up_flow) {
1813 struct dpif_flow_stats stats;
1815 facet_put__(ofproto, facet,
1816 odp_actions->data, odp_actions->size, &stats);
1817 facet_update_stats(ofproto, facet, &stats);
1819 facet_uninstall(ofproto, facet);
1822 /* The datapath flow is gone or has zeroed stats, so push stats out of
1823 * 'facet' into 'rule'. */
1824 facet_flush_stats(ofproto, facet);
1827 /* Update 'facet' now that we've taken care of all the old state. */
1828 facet->tags = ctx.tags;
1829 facet->nf_flow.output_iface = ctx.nf_output_iface;
1830 facet->may_install = ctx.may_set_up_flow;
1831 if (actions_changed) {
1832 free(facet->actions);
1833 facet->actions_len = odp_actions->size;
1834 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1836 if (facet->rule != new_rule) {
1837 COVERAGE_INC(facet_changed_rule);
1838 list_remove(&facet->list_node);
1839 list_push_back(&new_rule->facets, &facet->list_node);
1840 facet->rule = new_rule;
1841 facet->used = new_rule->created;
1842 facet->rs_used = facet->used;
1845 ofpbuf_delete(odp_actions);
1851 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1854 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
1856 COVERAGE_INC(ofproto_error);
1857 ofconn_send_reply(ofconn, buf);
1862 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1864 ofconn_send_reply(ofconn, make_echo_reply(oh));
1869 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1871 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1872 struct ofp_switch_features *osf;
1874 struct ofport *port;
1876 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1877 osf->datapath_id = htonll(ofproto->datapath_id);
1878 osf->n_buffers = htonl(pktbuf_capacity());
1880 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1881 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
1882 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1883 (1u << OFPAT_SET_VLAN_VID) |
1884 (1u << OFPAT_SET_VLAN_PCP) |
1885 (1u << OFPAT_STRIP_VLAN) |
1886 (1u << OFPAT_SET_DL_SRC) |
1887 (1u << OFPAT_SET_DL_DST) |
1888 (1u << OFPAT_SET_NW_SRC) |
1889 (1u << OFPAT_SET_NW_DST) |
1890 (1u << OFPAT_SET_NW_TOS) |
1891 (1u << OFPAT_SET_TP_SRC) |
1892 (1u << OFPAT_SET_TP_DST) |
1893 (1u << OFPAT_ENQUEUE));
1895 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1896 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1899 ofconn_send_reply(ofconn, buf);
1904 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1906 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1908 struct ofp_switch_config *osc;
1912 /* Figure out flags. */
1913 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
1914 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1917 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1918 osc->flags = htons(flags);
1919 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1920 ofconn_send_reply(ofconn, buf);
1926 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1928 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1929 uint16_t flags = ntohs(osc->flags);
1931 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1932 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1933 switch (flags & OFPC_FRAG_MASK) {
1934 case OFPC_FRAG_NORMAL:
1935 dpif_set_drop_frags(ofproto->dpif, false);
1937 case OFPC_FRAG_DROP:
1938 dpif_set_drop_frags(ofproto->dpif, true);
1941 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1947 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
1952 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
1953 struct action_xlate_ctx *ctx);
1956 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
1958 const struct ofport *ofport = get_port(ctx->ofproto, port);
1961 if (ofport->opp.config & OFPPC_NO_FWD) {
1962 /* Forwarding disabled on port. */
1967 * We don't have an ofport record for this port, but it doesn't hurt to
1968 * allow forwarding to it anyhow. Maybe such a port will appear later
1969 * and we're pre-populating the flow table.
1973 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port);
1974 ctx->nf_output_iface = port;
1977 static struct rule *
1978 rule_lookup(struct ofproto *ofproto, const struct flow *flow)
1980 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
1984 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
1986 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
1987 uint16_t old_in_port;
1990 /* Look up a flow with 'in_port' as the input port. Then restore the
1991 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1992 * have surprising behavior). */
1993 old_in_port = ctx->flow.in_port;
1994 ctx->flow.in_port = in_port;
1995 rule = rule_lookup(ctx->ofproto, &ctx->flow);
1996 ctx->flow.in_port = old_in_port;
1998 if (ctx->resubmit_hook) {
1999 ctx->resubmit_hook(ctx, rule);
2004 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2008 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2010 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2011 MAX_RESUBMIT_RECURSION);
2016 flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, uint32_t mask,
2017 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2019 struct ofport *ofport;
2021 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
2022 uint16_t odp_port = ofport->odp_port;
2023 if (odp_port != odp_in_port && !(ofport->opp.config & mask)) {
2024 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2027 *nf_output_iface = NF_OUT_FLOOD;
2031 xlate_output_action__(struct action_xlate_ctx *ctx,
2032 uint16_t port, uint16_t max_len)
2035 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2037 ctx->nf_output_iface = NF_OUT_DROP;
2041 add_output_action(ctx, ctx->flow.in_port);
2044 xlate_table_action(ctx, ctx->flow.in_port);
2047 if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
2048 ctx->odp_actions, &ctx->tags,
2049 &ctx->nf_output_iface,
2050 ctx->ofproto->aux)) {
2051 COVERAGE_INC(ofproto_uninstallable);
2052 ctx->may_set_up_flow = false;
2056 flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD,
2057 &ctx->nf_output_iface, ctx->odp_actions);
2060 flood_packets(ctx->ofproto, ctx->flow.in_port, 0,
2061 &ctx->nf_output_iface, ctx->odp_actions);
2063 case OFPP_CONTROLLER:
2064 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2067 add_output_action(ctx, ODPP_LOCAL);
2070 odp_port = ofp_port_to_odp_port(port);
2071 if (odp_port != ctx->flow.in_port) {
2072 add_output_action(ctx, odp_port);
2077 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2078 ctx->nf_output_iface = NF_OUT_FLOOD;
2079 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2080 ctx->nf_output_iface = prev_nf_output_iface;
2081 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2082 ctx->nf_output_iface != NF_OUT_FLOOD) {
2083 ctx->nf_output_iface = NF_OUT_MULTI;
2088 xlate_output_action(struct action_xlate_ctx *ctx,
2089 const struct ofp_action_output *oao)
2091 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2094 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2095 * optimization, because we're going to add another action that sets the
2096 * priority immediately after, or because there are no actions following the
2099 remove_pop_action(struct action_xlate_ctx *ctx)
2101 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2102 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2103 ctx->last_pop_priority = -1;
2108 add_pop_action(struct action_xlate_ctx *ctx)
2110 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2111 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2112 ctx->last_pop_priority = ctx->odp_actions->size;
2117 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2118 const struct ofp_action_enqueue *oae)
2120 uint16_t ofp_port, odp_port;
2124 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2127 /* Fall back to ordinary output action. */
2128 xlate_output_action__(ctx, ntohs(oae->port), 0);
2132 /* Figure out ODP output port. */
2133 ofp_port = ntohs(oae->port);
2134 if (ofp_port != OFPP_IN_PORT) {
2135 odp_port = ofp_port_to_odp_port(ofp_port);
2137 odp_port = ctx->flow.in_port;
2140 /* Add ODP actions. */
2141 remove_pop_action(ctx);
2142 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2143 add_output_action(ctx, odp_port);
2144 add_pop_action(ctx);
2146 /* Update NetFlow output port. */
2147 if (ctx->nf_output_iface == NF_OUT_DROP) {
2148 ctx->nf_output_iface = odp_port;
2149 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2150 ctx->nf_output_iface = NF_OUT_MULTI;
2155 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2156 const struct nx_action_set_queue *nasq)
2161 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2164 /* Couldn't translate queue to a priority, so ignore. A warning
2165 * has already been logged. */
2169 remove_pop_action(ctx);
2170 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2174 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2176 ovs_be16 tci = ctx->flow.vlan_tci;
2177 if (!(tci & htons(VLAN_CFI))) {
2178 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2180 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2181 tci & ~htons(VLAN_CFI));
2185 struct xlate_reg_state {
2191 save_reg_state(const struct action_xlate_ctx *ctx,
2192 struct xlate_reg_state *state)
2194 state->vlan_tci = ctx->flow.vlan_tci;
2195 state->tun_id = ctx->flow.tun_id;
2199 update_reg_state(struct action_xlate_ctx *ctx,
2200 const struct xlate_reg_state *state)
2202 if (ctx->flow.vlan_tci != state->vlan_tci) {
2203 xlate_set_dl_tci(ctx);
2205 if (ctx->flow.tun_id != state->tun_id) {
2206 nl_msg_put_be64(ctx->odp_actions,
2207 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2212 xlate_nicira_action(struct action_xlate_ctx *ctx,
2213 const struct nx_action_header *nah)
2215 const struct nx_action_resubmit *nar;
2216 const struct nx_action_set_tunnel *nast;
2217 const struct nx_action_set_queue *nasq;
2218 const struct nx_action_multipath *nam;
2219 enum nx_action_subtype subtype = ntohs(nah->subtype);
2220 struct xlate_reg_state state;
2223 assert(nah->vendor == htonl(NX_VENDOR_ID));
2225 case NXAST_RESUBMIT:
2226 nar = (const struct nx_action_resubmit *) nah;
2227 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2230 case NXAST_SET_TUNNEL:
2231 nast = (const struct nx_action_set_tunnel *) nah;
2232 tun_id = htonll(ntohl(nast->tun_id));
2233 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2234 ctx->flow.tun_id = tun_id;
2237 case NXAST_DROP_SPOOFED_ARP:
2238 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2239 nl_msg_put_flag(ctx->odp_actions,
2240 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2244 case NXAST_SET_QUEUE:
2245 nasq = (const struct nx_action_set_queue *) nah;
2246 xlate_set_queue_action(ctx, nasq);
2249 case NXAST_POP_QUEUE:
2250 add_pop_action(ctx);
2253 case NXAST_REG_MOVE:
2254 save_reg_state(ctx, &state);
2255 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2257 update_reg_state(ctx, &state);
2260 case NXAST_REG_LOAD:
2261 save_reg_state(ctx, &state);
2262 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2264 update_reg_state(ctx, &state);
2268 /* Nothing to do. */
2271 case NXAST_SET_TUNNEL64:
2272 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2273 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2274 ctx->flow.tun_id = tun_id;
2277 case NXAST_MULTIPATH:
2278 nam = (const struct nx_action_multipath *) nah;
2279 multipath_execute(nam, &ctx->flow);
2282 /* If you add a new action here that modifies flow data, don't forget to
2283 * update the flow key in ctx->flow at the same time. */
2285 case NXAST_SNAT__OBSOLETE:
2287 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2293 do_xlate_actions(const union ofp_action *in, size_t n_in,
2294 struct action_xlate_ctx *ctx)
2296 struct actions_iterator iter;
2297 const union ofp_action *ia;
2298 const struct ofport *port;
2300 port = get_port(ctx->ofproto, ctx->flow.in_port);
2301 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2302 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2303 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
2304 /* Drop this flow. */
2308 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2309 enum ofp_action_type type = ntohs(ia->type);
2310 const struct ofp_action_dl_addr *oada;
2314 xlate_output_action(ctx, &ia->output);
2317 case OFPAT_SET_VLAN_VID:
2318 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2319 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2320 xlate_set_dl_tci(ctx);
2323 case OFPAT_SET_VLAN_PCP:
2324 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2325 ctx->flow.vlan_tci |= htons(
2326 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2327 xlate_set_dl_tci(ctx);
2330 case OFPAT_STRIP_VLAN:
2331 ctx->flow.vlan_tci = htons(0);
2332 xlate_set_dl_tci(ctx);
2335 case OFPAT_SET_DL_SRC:
2336 oada = ((struct ofp_action_dl_addr *) ia);
2337 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2338 oada->dl_addr, ETH_ADDR_LEN);
2339 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
2342 case OFPAT_SET_DL_DST:
2343 oada = ((struct ofp_action_dl_addr *) ia);
2344 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2345 oada->dl_addr, ETH_ADDR_LEN);
2346 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
2349 case OFPAT_SET_NW_SRC:
2350 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
2351 ia->nw_addr.nw_addr);
2352 ctx->flow.nw_src = ia->nw_addr.nw_addr;
2355 case OFPAT_SET_NW_DST:
2356 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
2357 ia->nw_addr.nw_addr);
2358 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
2361 case OFPAT_SET_NW_TOS:
2362 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
2364 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
2367 case OFPAT_SET_TP_SRC:
2368 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
2369 ia->tp_port.tp_port);
2370 ctx->flow.tp_src = ia->tp_port.tp_port;
2373 case OFPAT_SET_TP_DST:
2374 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
2375 ia->tp_port.tp_port);
2376 ctx->flow.tp_dst = ia->tp_port.tp_port;
2380 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2384 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
2388 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
2395 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
2396 struct ofproto *ofproto, const struct flow *flow,
2397 const struct ofpbuf *packet)
2399 ctx->ofproto = ofproto;
2401 ctx->packet = packet;
2402 ctx->resubmit_hook = NULL;
2403 ctx->check_special = true;
2407 ofproto_process_cfm(struct ofproto *ofproto, const struct flow *flow,
2408 const struct ofpbuf *packet)
2410 struct ofport *ofport;
2412 ofport = get_port(ofproto, flow->in_port);
2413 if (ofport && ofport->cfm) {
2414 cfm_process_heartbeat(ofport->cfm, packet);
2418 static struct ofpbuf *
2419 xlate_actions(struct action_xlate_ctx *ctx,
2420 const union ofp_action *in, size_t n_in)
2422 COVERAGE_INC(ofproto_ofp2odp);
2424 ctx->odp_actions = ofpbuf_new(512);
2426 ctx->may_set_up_flow = true;
2427 ctx->nf_output_iface = NF_OUT_DROP;
2429 ctx->last_pop_priority = -1;
2431 if (ctx->check_special && cfm_should_process_flow(&ctx->flow)) {
2433 ofproto_process_cfm(ctx->ofproto, &ctx->flow, ctx->packet);
2435 ctx->may_set_up_flow = false;
2436 } else if (ctx->check_special
2437 && ctx->ofproto->ofhooks->special_cb
2438 && !ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet,
2439 ctx->ofproto->aux)) {
2440 ctx->may_set_up_flow = false;
2442 do_xlate_actions(in, n_in, ctx);
2445 remove_pop_action(ctx);
2447 /* Check with in-band control to see if we're allowed to set up this
2449 if (!connmgr_may_set_up_flow(ctx->ofproto->connmgr, &ctx->flow,
2450 ctx->odp_actions->data,
2451 ctx->odp_actions->size)) {
2452 ctx->may_set_up_flow = false;
2455 return ctx->odp_actions;
2458 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2459 * error message code (composed with ofp_mkerr()) for the caller to propagate
2460 * upward. Otherwise, returns 0.
2462 * The log message mentions 'msg_type'. */
2464 reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
2466 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2467 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
2468 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
2469 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
2472 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2479 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2481 struct ofproto *p = ofconn_get_ofproto(ofconn);
2482 struct ofp_packet_out *opo;
2483 struct ofpbuf payload, *buffer;
2484 union ofp_action *ofp_actions;
2485 struct action_xlate_ctx ctx;
2486 struct ofpbuf *odp_actions;
2487 struct ofpbuf request;
2489 size_t n_ofp_actions;
2493 COVERAGE_INC(ofproto_packet_out);
2495 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
2500 /* Get ofp_packet_out. */
2501 ofpbuf_use_const(&request, oh, ntohs(oh->length));
2502 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
2505 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
2506 &ofp_actions, &n_ofp_actions);
2512 if (opo->buffer_id != htonl(UINT32_MAX)) {
2513 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
2515 if (error || !buffer) {
2524 /* Extract flow, check actions. */
2525 flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
2527 error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
2533 action_xlate_ctx_init(&ctx, p, &flow, &payload);
2534 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
2535 dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
2536 ofpbuf_delete(odp_actions);
2539 ofpbuf_delete(buffer);
2544 update_port_config(struct ofproto *p, struct ofport *port,
2545 uint32_t config, uint32_t mask)
2547 mask &= config ^ port->opp.config;
2548 if (mask & OFPPC_PORT_DOWN) {
2549 if (config & OFPPC_PORT_DOWN) {
2550 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2552 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2555 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
2556 OFPPC_NO_FWD | OFPPC_NO_FLOOD)
2557 if (mask & REVALIDATE_BITS) {
2558 COVERAGE_INC(ofproto_costly_flags);
2559 port->opp.config ^= mask & REVALIDATE_BITS;
2560 p->need_revalidate = true;
2562 #undef REVALIDATE_BITS
2563 if (mask & OFPPC_NO_PACKET_IN) {
2564 port->opp.config ^= OFPPC_NO_PACKET_IN;
2569 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2571 struct ofproto *p = ofconn_get_ofproto(ofconn);
2572 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
2573 struct ofport *port;
2576 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
2581 port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
2583 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2584 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2585 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2587 update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
2588 if (opm->advertise) {
2589 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2595 static struct ofpbuf *
2596 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
2598 struct ofp_stats_reply *osr;
2601 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2602 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2604 osr->flags = htons(0);
2608 static struct ofpbuf *
2609 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
2611 const struct ofp_stats_request *osr
2612 = (const struct ofp_stats_request *) request;
2613 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
2617 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
2618 struct ofpbuf **msgp)
2620 struct ofpbuf *msg = *msgp;
2621 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2622 if (nbytes + msg->size > UINT16_MAX) {
2623 struct ofp_stats_reply *reply = msg->data;
2624 reply->flags = htons(OFPSF_REPLY_MORE);
2625 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
2626 ofconn_send_reply(ofconn, msg);
2628 return ofpbuf_put_uninit(*msgp, nbytes);
2631 static struct ofpbuf *
2632 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
2634 struct nicira_stats_msg *nsm;
2637 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
2638 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
2639 nsm->type = htons(OFPST_VENDOR);
2640 nsm->flags = htons(0);
2641 nsm->vendor = htonl(NX_VENDOR_ID);
2642 nsm->subtype = subtype;
2646 static struct ofpbuf *
2647 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
2649 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
2653 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
2654 struct ofpbuf **msgp)
2656 struct ofpbuf *msg = *msgp;
2657 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
2658 if (nbytes + msg->size > UINT16_MAX) {
2659 struct nicira_stats_msg *reply = msg->data;
2660 reply->flags = htons(OFPSF_REPLY_MORE);
2661 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
2662 ofconn_send_reply(ofconn, msg);
2664 ofpbuf_prealloc_tailroom(*msgp, nbytes);
2668 handle_desc_stats_request(struct ofconn *ofconn,
2669 const struct ofp_header *request)
2671 struct ofproto *p = ofconn_get_ofproto(ofconn);
2672 struct ofp_desc_stats *ods;
2675 msg = start_ofp_stats_reply(request, sizeof *ods);
2676 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
2677 memset(ods, 0, sizeof *ods);
2678 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
2679 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
2680 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
2681 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
2682 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2683 ofconn_send_reply(ofconn, msg);
2689 handle_table_stats_request(struct ofconn *ofconn,
2690 const struct ofp_header *request)
2692 struct ofproto *p = ofconn_get_ofproto(ofconn);
2693 struct ofp_table_stats *ots;
2696 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
2698 /* Classifier table. */
2699 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
2700 memset(ots, 0, sizeof *ots);
2701 strcpy(ots->name, "classifier");
2702 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
2703 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
2704 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
2705 ots->active_count = htonl(classifier_count(&p->cls));
2706 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
2707 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
2709 ofconn_send_reply(ofconn, msg);
2714 append_port_stat(struct ofport *port, struct ofconn *ofconn,
2715 struct ofpbuf **msgp)
2717 struct netdev_stats stats;
2718 struct ofp_port_stats *ops;
2720 /* Intentionally ignore return value, since errors will set
2721 * 'stats' to all-1s, which is correct for OpenFlow, and
2722 * netdev_get_stats() will log errors. */
2723 netdev_get_stats(port->netdev, &stats);
2725 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
2726 ops->port_no = htons(port->opp.port_no);
2727 memset(ops->pad, 0, sizeof ops->pad);
2728 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
2729 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
2730 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
2731 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
2732 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
2733 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
2734 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
2735 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
2736 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
2737 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
2738 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
2739 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
2743 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2745 struct ofproto *p = ofconn_get_ofproto(ofconn);
2746 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
2747 struct ofp_port_stats *ops;
2749 struct ofport *port;
2751 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
2752 if (psr->port_no != htons(OFPP_NONE)) {
2753 port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
2755 append_port_stat(port, ofconn, &msg);
2758 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2759 append_port_stat(port, ofconn, &msg);
2763 ofconn_send_reply(ofconn, msg);
2768 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
2770 long long int msecs = time_msec() - start;
2771 *sec = msecs / 1000;
2772 *nsec = (msecs % 1000) * (1000 * 1000);
2776 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
2780 calc_flow_duration__(start, &sec, &nsec);
2781 *sec_be = htonl(sec);
2782 *nsec_be = htonl(nsec);
2786 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
2787 ovs_be16 out_port, struct ofpbuf **replyp)
2789 struct ofp_flow_stats *ofs;
2790 uint64_t packet_count, byte_count;
2792 size_t act_len, len;
2794 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2798 act_len = sizeof *rule->actions * rule->n_actions;
2799 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2801 rule_get_stats(rule, &packet_count, &byte_count);
2803 ofs = append_ofp_stats_reply(len, ofconn, replyp);
2804 ofs->length = htons(len);
2807 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
2808 &ofs->match, rule->flow_cookie, &cookie);
2809 put_32aligned_be64(&ofs->cookie, cookie);
2810 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
2811 ofs->priority = htons(rule->cr.priority);
2812 ofs->idle_timeout = htons(rule->idle_timeout);
2813 ofs->hard_timeout = htons(rule->hard_timeout);
2814 memset(ofs->pad2, 0, sizeof ofs->pad2);
2815 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
2816 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
2817 if (rule->n_actions > 0) {
2818 memcpy(ofs->actions, rule->actions, act_len);
2823 is_valid_table(uint8_t table_id)
2825 if (table_id == 0 || table_id == 0xff) {
2828 /* It would probably be better to reply with an error but there doesn't
2829 * seem to be any appropriate value, so that might just be
2831 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
2838 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2840 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
2841 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2842 struct ofpbuf *reply;
2844 COVERAGE_INC(ofproto_flows_req);
2845 reply = start_ofp_stats_reply(oh, 1024);
2846 if (is_valid_table(fsr->table_id)) {
2847 struct cls_cursor cursor;
2848 struct cls_rule target;
2851 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
2853 cls_cursor_init(&cursor, &ofproto->cls, &target);
2854 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2855 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
2858 ofconn_send_reply(ofconn, reply);
2864 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
2865 ovs_be16 out_port, struct ofpbuf **replyp)
2867 struct nx_flow_stats *nfs;
2868 uint64_t packet_count, byte_count;
2869 size_t act_len, start_len;
2870 struct ofpbuf *reply;
2872 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2876 rule_get_stats(rule, &packet_count, &byte_count);
2878 act_len = sizeof *rule->actions * rule->n_actions;
2880 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
2881 start_len = (*replyp)->size;
2884 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
2887 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
2888 nfs->cookie = rule->flow_cookie;
2889 nfs->priority = htons(rule->cr.priority);
2890 nfs->idle_timeout = htons(rule->idle_timeout);
2891 nfs->hard_timeout = htons(rule->hard_timeout);
2892 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
2893 memset(nfs->pad2, 0, sizeof nfs->pad2);
2894 nfs->packet_count = htonll(packet_count);
2895 nfs->byte_count = htonll(byte_count);
2896 if (rule->n_actions > 0) {
2897 ofpbuf_put(reply, rule->actions, act_len);
2899 nfs->length = htons(reply->size - start_len);
2903 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
2905 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2906 struct nx_flow_stats_request *nfsr;
2907 struct cls_rule target;
2908 struct ofpbuf *reply;
2912 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2914 /* Dissect the message. */
2915 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2916 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
2921 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2924 COVERAGE_INC(ofproto_flows_req);
2925 reply = start_nxstats_reply(&nfsr->nsm, 1024);
2926 if (is_valid_table(nfsr->table_id)) {
2927 struct cls_cursor cursor;
2930 cls_cursor_init(&cursor, &ofproto->cls, &target);
2931 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2932 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
2935 ofconn_send_reply(ofconn, reply);
2941 flow_stats_ds(struct rule *rule, struct ds *results)
2943 uint64_t packet_count, byte_count;
2944 size_t act_len = sizeof *rule->actions * rule->n_actions;
2946 rule_get_stats(rule, &packet_count, &byte_count);
2948 ds_put_format(results, "duration=%llds, ",
2949 (time_msec() - rule->created) / 1000);
2950 ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0);
2951 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2952 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2953 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2954 cls_rule_format(&rule->cr, results);
2955 ds_put_char(results, ',');
2957 ofp_print_actions(results, &rule->actions->header, act_len);
2959 ds_put_cstr(results, "drop");
2961 ds_put_cstr(results, "\n");
2964 /* Adds a pretty-printed description of all flows to 'results', including
2965 * hidden flows (e.g., set up by in-band control). */
2967 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
2969 struct cls_cursor cursor;
2972 cls_cursor_init(&cursor, &p->cls, NULL);
2973 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2974 flow_stats_ds(rule, results);
2979 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
2980 ovs_be16 out_port, uint8_t table_id,
2981 struct ofp_aggregate_stats_reply *oasr)
2983 uint64_t total_packets = 0;
2984 uint64_t total_bytes = 0;
2987 COVERAGE_INC(ofproto_agg_request);
2989 if (is_valid_table(table_id)) {
2990 struct cls_cursor cursor;
2993 cls_cursor_init(&cursor, &ofproto->cls, target);
2994 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2995 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
2996 uint64_t packet_count;
2997 uint64_t byte_count;
2999 rule_get_stats(rule, &packet_count, &byte_count);
3001 total_packets += packet_count;
3002 total_bytes += byte_count;
3008 oasr->flow_count = htonl(n_flows);
3009 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
3010 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
3011 memset(oasr->pad, 0, sizeof oasr->pad);
3015 handle_aggregate_stats_request(struct ofconn *ofconn,
3016 const struct ofp_header *oh)
3018 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
3019 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3020 struct ofp_aggregate_stats_reply *reply;
3021 struct cls_rule target;
3024 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
3027 msg = start_ofp_stats_reply(oh, sizeof *reply);
3028 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
3029 query_aggregate_stats(ofproto, &target, request->out_port,
3030 request->table_id, reply);
3031 ofconn_send_reply(ofconn, msg);
3036 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
3038 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3039 struct nx_aggregate_stats_request *request;
3040 struct ofp_aggregate_stats_reply *reply;
3041 struct cls_rule target;
3046 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3048 /* Dissect the message. */
3049 request = ofpbuf_pull(&b, sizeof *request);
3050 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
3055 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3059 COVERAGE_INC(ofproto_flows_req);
3060 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
3061 reply = ofpbuf_put_uninit(buf, sizeof *reply);
3062 query_aggregate_stats(ofproto, &target, request->out_port,
3063 request->table_id, reply);
3064 ofconn_send_reply(ofconn, buf);
3069 struct queue_stats_cbdata {
3070 struct ofconn *ofconn;
3071 struct ofport *ofport;
3076 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3077 const struct netdev_queue_stats *stats)
3079 struct ofp_queue_stats *reply;
3081 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
3082 reply->port_no = htons(cbdata->ofport->opp.port_no);
3083 memset(reply->pad, 0, sizeof reply->pad);
3084 reply->queue_id = htonl(queue_id);
3085 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
3086 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
3087 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
3091 handle_queue_stats_dump_cb(uint32_t queue_id,
3092 struct netdev_queue_stats *stats,
3095 struct queue_stats_cbdata *cbdata = cbdata_;
3097 put_queue_stats(cbdata, queue_id, stats);
3101 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3102 struct queue_stats_cbdata *cbdata)
3104 cbdata->ofport = port;
3105 if (queue_id == OFPQ_ALL) {
3106 netdev_dump_queue_stats(port->netdev,
3107 handle_queue_stats_dump_cb, cbdata);
3109 struct netdev_queue_stats stats;
3111 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3112 put_queue_stats(cbdata, queue_id, &stats);
3118 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
3120 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3121 const struct ofp_queue_stats_request *qsr;
3122 struct queue_stats_cbdata cbdata;
3123 struct ofport *port;
3124 unsigned int port_no;
3127 qsr = ofputil_stats_body(oh);
3129 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3132 COVERAGE_INC(ofproto_queue_req);
3134 cbdata.ofconn = ofconn;
3135 cbdata.msg = start_ofp_stats_reply(oh, 128);
3137 port_no = ntohs(qsr->port_no);
3138 queue_id = ntohl(qsr->queue_id);
3139 if (port_no == OFPP_ALL) {
3140 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3141 handle_queue_stats_for_port(port, queue_id, &cbdata);
3143 } else if (port_no < ofproto->max_ports) {
3144 port = get_port(ofproto, ofp_port_to_odp_port(port_no));
3146 handle_queue_stats_for_port(port, queue_id, &cbdata);
3149 ofpbuf_delete(cbdata.msg);
3150 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
3152 ofconn_send_reply(ofconn, cbdata.msg);
3157 /* Updates 'facet''s used time. Caller is responsible for calling
3158 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3160 facet_update_time(struct ofproto *ofproto, struct facet *facet,
3163 if (used > facet->used) {
3165 if (used > facet->rule->used) {
3166 facet->rule->used = used;
3168 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3172 /* Folds the statistics from 'stats' into the counters in 'facet'.
3174 * Because of the meaning of a facet's counters, it only makes sense to do this
3175 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3176 * packet that was sent by hand or if it represents statistics that have been
3177 * cleared out of the datapath. */
3179 facet_update_stats(struct ofproto *ofproto, struct facet *facet,
3180 const struct dpif_flow_stats *stats)
3182 if (stats->n_packets || stats->used > facet->used) {
3183 facet_update_time(ofproto, facet, stats->used);
3184 facet->packet_count += stats->n_packets;
3185 facet->byte_count += stats->n_bytes;
3186 facet_push_stats(ofproto, facet);
3187 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3192 facet_push_stats(struct ofproto *ofproto, struct facet *facet)
3194 uint64_t rs_packets, rs_bytes;
3196 assert(facet->packet_count >= facet->rs_packet_count);
3197 assert(facet->byte_count >= facet->rs_byte_count);
3198 assert(facet->used >= facet->rs_used);
3200 rs_packets = facet->packet_count - facet->rs_packet_count;
3201 rs_bytes = facet->byte_count - facet->rs_byte_count;
3203 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3204 facet->rs_packet_count = facet->packet_count;
3205 facet->rs_byte_count = facet->byte_count;
3206 facet->rs_used = facet->used;
3208 flow_push_stats(ofproto, facet->rule, &facet->flow,
3209 rs_packets, rs_bytes, facet->used);
3213 struct ofproto_push {
3214 struct action_xlate_ctx ctx;
3221 push_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
3223 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3226 rule->packet_count += push->packets;
3227 rule->byte_count += push->bytes;
3228 rule->used = MAX(push->used, rule->used);
3232 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3233 * 'rule''s actions. */
3235 flow_push_stats(struct ofproto *ofproto, const struct rule *rule,
3236 struct flow *flow, uint64_t packets, uint64_t bytes,
3239 struct ofproto_push push;
3241 push.packets = packets;
3245 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3246 push.ctx.resubmit_hook = push_resubmit;
3247 ofpbuf_delete(xlate_actions(&push.ctx, rule->actions, rule->n_actions));
3250 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3251 * in which no matching flow already exists in the flow table.
3253 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3254 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
3255 * OpenFlow error code as encoded by ofp_mkerr() on failure.
3257 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3260 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
3262 struct ofproto *p = ofconn_get_ofproto(ofconn);
3263 struct ofpbuf *packet;
3268 if (fm->flags & OFPFF_CHECK_OVERLAP
3269 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
3270 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
3274 if (fm->buffer_id != UINT32_MAX) {
3275 error = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id,
3279 in_port = UINT16_MAX;
3282 rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
3283 fm->idle_timeout, fm->hard_timeout, fm->cookie,
3284 fm->flags & OFPFF_SEND_FLOW_REM);
3285 rule_insert(p, rule);
3287 rule_execute(p, rule, in_port, packet);
3292 static struct rule *
3293 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
3295 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
3299 send_buffered_packet(struct ofconn *ofconn,
3300 struct rule *rule, uint32_t buffer_id)
3302 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3303 struct ofpbuf *packet;
3307 if (buffer_id == UINT32_MAX) {
3311 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
3316 rule_execute(ofproto, rule, in_port, packet);
3321 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3323 struct modify_flows_cbdata {
3324 struct ofproto *ofproto;
3325 const struct flow_mod *fm;
3329 static int modify_flow(struct ofproto *, const struct flow_mod *,
3332 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
3333 * encoded by ofp_mkerr() on failure.
3335 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3338 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
3340 struct ofproto *p = ofconn_get_ofproto(ofconn);
3341 struct rule *match = NULL;
3342 struct cls_cursor cursor;
3345 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3346 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3347 if (!rule_is_hidden(rule)) {
3349 modify_flow(p, fm, rule);
3354 /* This credits the packet to whichever flow happened to match last.
3355 * That's weird. Maybe we should do a lookup for the flow that
3356 * actually matches the packet? Who knows. */
3357 send_buffered_packet(ofconn, match, fm->buffer_id);
3360 return add_flow(ofconn, fm);
3364 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3365 * code as encoded by ofp_mkerr() on failure.
3367 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3370 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
3372 struct ofproto *p = ofconn_get_ofproto(ofconn);
3373 struct rule *rule = find_flow_strict(p, fm);
3374 if (rule && !rule_is_hidden(rule)) {
3375 modify_flow(p, fm, rule);
3376 return send_buffered_packet(ofconn, rule, fm->buffer_id);
3378 return add_flow(ofconn, fm);
3382 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
3383 * been identified as a flow in 'p''s flow table to be modified, by changing
3384 * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
3385 * ofp_action[] structures). */
3387 modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
3389 size_t actions_len = fm->n_actions * sizeof *rule->actions;
3391 rule->flow_cookie = fm->cookie;
3393 /* If the actions are the same, do nothing. */
3394 if (fm->n_actions == rule->n_actions
3396 || !memcmp(fm->actions, rule->actions, actions_len))) {
3400 /* Replace actions. */
3401 free(rule->actions);
3402 rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
3403 rule->n_actions = fm->n_actions;
3405 p->need_revalidate = true;
3410 /* OFPFC_DELETE implementation. */
3412 static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
3414 /* Implements OFPFC_DELETE. */
3416 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
3418 struct rule *rule, *next_rule;
3419 struct cls_cursor cursor;
3421 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3422 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3423 delete_flow(p, rule, htons(fm->out_port));
3427 /* Implements OFPFC_DELETE_STRICT. */
3429 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
3431 struct rule *rule = find_flow_strict(p, fm);
3433 delete_flow(p, rule, htons(fm->out_port));
3437 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
3438 * been identified as a flow to delete from 'p''s flow table, by deleting the
3439 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
3442 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
3443 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
3444 * specified 'out_port'. */
3446 delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
3448 if (rule_is_hidden(rule)) {
3452 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
3456 rule_send_removed(p, rule, OFPRR_DELETE);
3457 rule_remove(p, rule);
3461 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3463 struct ofproto *p = ofconn_get_ofproto(ofconn);
3467 error = reject_slave_controller(ofconn, "flow_mod");
3472 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
3477 /* We do not support the emergency flow cache. It will hopefully get
3478 * dropped from OpenFlow in the near future. */
3479 if (fm.flags & OFPFF_EMERG) {
3480 /* There isn't a good fit for an error code, so just state that the
3481 * flow table is full. */
3482 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
3485 error = validate_actions(fm.actions, fm.n_actions,
3486 &fm.cr.flow, p->max_ports);
3491 switch (fm.command) {
3493 return add_flow(ofconn, &fm);
3496 return modify_flows_loose(ofconn, &fm);
3498 case OFPFC_MODIFY_STRICT:
3499 return modify_flow_strict(ofconn, &fm);
3502 delete_flows_loose(p, &fm);
3505 case OFPFC_DELETE_STRICT:
3506 delete_flow_strict(p, &fm);
3510 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
3515 handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
3517 const struct nxt_tun_id_cookie *msg
3518 = (const struct nxt_tun_id_cookie *) oh;
3519 enum nx_flow_format flow_format;
3521 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
3522 ofconn_set_flow_format(ofconn, flow_format);
3528 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3530 struct nx_role_request *nrr = (struct nx_role_request *) oh;
3531 struct nx_role_request *reply;
3535 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
3536 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
3537 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3540 role = ntohl(nrr->role);
3541 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
3542 && role != NX_ROLE_SLAVE) {
3543 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
3545 /* There's no good error code for this. */
3546 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
3549 ofconn_set_role(ofconn, role);
3551 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
3552 reply->role = htonl(role);
3553 ofconn_send_reply(ofconn, buf);
3559 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
3561 const struct nxt_set_flow_format *msg
3562 = (const struct nxt_set_flow_format *) oh;
3565 format = ntohl(msg->format);
3566 if (format == NXFF_OPENFLOW10
3567 || format == NXFF_TUN_ID_FROM_COOKIE
3568 || format == NXFF_NXM) {
3569 ofconn_set_flow_format(ofconn, format);
3572 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3577 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
3579 struct ofp_header *ob;
3582 /* Currently, everything executes synchronously, so we can just
3583 * immediately send the barrier reply. */
3584 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
3585 ofconn_send_reply(ofconn, buf);
3590 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
3592 const struct ofp_header *oh = msg->data;
3593 const struct ofputil_msg_type *type;
3596 error = ofputil_decode_msg_type(oh, &type);
3601 switch (ofputil_msg_type_code(type)) {
3602 /* OpenFlow requests. */
3603 case OFPUTIL_OFPT_ECHO_REQUEST:
3604 return handle_echo_request(ofconn, oh);
3606 case OFPUTIL_OFPT_FEATURES_REQUEST:
3607 return handle_features_request(ofconn, oh);
3609 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
3610 return handle_get_config_request(ofconn, oh);
3612 case OFPUTIL_OFPT_SET_CONFIG:
3613 return handle_set_config(ofconn, msg->data);
3615 case OFPUTIL_OFPT_PACKET_OUT:
3616 return handle_packet_out(ofconn, oh);
3618 case OFPUTIL_OFPT_PORT_MOD:
3619 return handle_port_mod(ofconn, oh);
3621 case OFPUTIL_OFPT_FLOW_MOD:
3622 return handle_flow_mod(ofconn, oh);
3624 case OFPUTIL_OFPT_BARRIER_REQUEST:
3625 return handle_barrier_request(ofconn, oh);
3627 /* OpenFlow replies. */
3628 case OFPUTIL_OFPT_ECHO_REPLY:
3631 /* Nicira extension requests. */
3632 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
3633 return handle_tun_id_from_cookie(ofconn, oh);
3635 case OFPUTIL_NXT_ROLE_REQUEST:
3636 return handle_role_request(ofconn, oh);
3638 case OFPUTIL_NXT_SET_FLOW_FORMAT:
3639 return handle_nxt_set_flow_format(ofconn, oh);
3641 case OFPUTIL_NXT_FLOW_MOD:
3642 return handle_flow_mod(ofconn, oh);
3644 /* OpenFlow statistics requests. */
3645 case OFPUTIL_OFPST_DESC_REQUEST:
3646 return handle_desc_stats_request(ofconn, oh);
3648 case OFPUTIL_OFPST_FLOW_REQUEST:
3649 return handle_flow_stats_request(ofconn, oh);
3651 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
3652 return handle_aggregate_stats_request(ofconn, oh);
3654 case OFPUTIL_OFPST_TABLE_REQUEST:
3655 return handle_table_stats_request(ofconn, oh);
3657 case OFPUTIL_OFPST_PORT_REQUEST:
3658 return handle_port_stats_request(ofconn, oh);
3660 case OFPUTIL_OFPST_QUEUE_REQUEST:
3661 return handle_queue_stats_request(ofconn, oh);
3663 /* Nicira extension statistics requests. */
3664 case OFPUTIL_NXST_FLOW_REQUEST:
3665 return handle_nxst_flow(ofconn, oh);
3667 case OFPUTIL_NXST_AGGREGATE_REQUEST:
3668 return handle_nxst_aggregate(ofconn, oh);
3670 case OFPUTIL_INVALID:
3671 case OFPUTIL_OFPT_HELLO:
3672 case OFPUTIL_OFPT_ERROR:
3673 case OFPUTIL_OFPT_FEATURES_REPLY:
3674 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
3675 case OFPUTIL_OFPT_PACKET_IN:
3676 case OFPUTIL_OFPT_FLOW_REMOVED:
3677 case OFPUTIL_OFPT_PORT_STATUS:
3678 case OFPUTIL_OFPT_BARRIER_REPLY:
3679 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
3680 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
3681 case OFPUTIL_OFPST_DESC_REPLY:
3682 case OFPUTIL_OFPST_FLOW_REPLY:
3683 case OFPUTIL_OFPST_QUEUE_REPLY:
3684 case OFPUTIL_OFPST_PORT_REPLY:
3685 case OFPUTIL_OFPST_TABLE_REPLY:
3686 case OFPUTIL_OFPST_AGGREGATE_REPLY:
3687 case OFPUTIL_NXT_ROLE_REPLY:
3688 case OFPUTIL_NXT_FLOW_REMOVED:
3689 case OFPUTIL_NXST_FLOW_REPLY:
3690 case OFPUTIL_NXST_AGGREGATE_REPLY:
3692 if (VLOG_IS_WARN_ENABLED()) {
3693 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3694 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3697 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
3698 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
3700 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3706 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
3708 int error = handle_openflow__(ofconn, ofp_msg);
3710 send_error_oh(ofconn, ofp_msg->data, error);
3712 COVERAGE_INC(ofproto_recv_openflow);
3716 handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3718 struct facet *facet;
3721 /* Obtain in_port and tun_id, at least. */
3722 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3724 /* Set header pointers in 'flow'. */
3725 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
3727 if (cfm_should_process_flow(&flow)) {
3728 ofproto_process_cfm(p, &flow, upcall->packet);
3729 ofpbuf_delete(upcall->packet);
3731 } else if (p->ofhooks->special_cb
3732 && !p->ofhooks->special_cb(&flow, upcall->packet, p->aux)) {
3733 ofpbuf_delete(upcall->packet);
3737 /* Check with in-band control to see if this packet should be sent
3738 * to the local port regardless of the flow table. */
3739 if (connmgr_msg_in_hook(p->connmgr, &flow, upcall->packet)) {
3740 ofproto_send_packet(p, ODPP_LOCAL, 0, upcall->packet);
3743 facet = facet_lookup_valid(p, &flow);
3745 struct rule *rule = rule_lookup(p, &flow);
3747 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3748 struct ofport *port = get_port(p, flow.in_port);
3750 if (port->opp.config & OFPPC_NO_PACKET_IN) {
3751 COVERAGE_INC(ofproto_no_packet_in);
3752 /* XXX install 'drop' flow entry */
3753 ofpbuf_delete(upcall->packet);
3757 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
3761 COVERAGE_INC(ofproto_packet_in);
3762 send_packet_in(p, upcall, &flow, false);
3766 facet = facet_create(p, rule, &flow, upcall->packet);
3767 } else if (!facet->may_install) {
3768 /* The facet is not installable, that is, we need to process every
3769 * packet, so process the current packet's actions into 'facet'. */
3770 facet_make_actions(p, facet, upcall->packet);
3773 if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) {
3775 * Extra-special case for fail-open mode.
3777 * We are in fail-open mode and the packet matched the fail-open rule,
3778 * but we are connected to a controller too. We should send the packet
3779 * up to the controller in the hope that it will try to set up a flow
3780 * and thereby allow us to exit fail-open.
3782 * See the top-level comment in fail-open.c for more information.
3784 send_packet_in(p, upcall, &flow, true);
3787 facet_execute(p, facet, upcall->packet);
3788 facet_install(p, facet, false);
3792 handle_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3796 switch (upcall->type) {
3797 case DPIF_UC_ACTION:
3798 COVERAGE_INC(ofproto_ctlr_action);
3799 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3800 send_packet_in(p, upcall, &flow, false);
3803 case DPIF_UC_SAMPLE:
3805 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3806 ofproto_sflow_received(p->sflow, upcall, &flow);
3808 ofpbuf_delete(upcall->packet);
3812 handle_miss_upcall(p, upcall);
3815 case DPIF_N_UC_TYPES:
3817 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3822 /* Flow expiration. */
3824 static int ofproto_dp_max_idle(const struct ofproto *);
3825 static void ofproto_update_stats(struct ofproto *);
3826 static void rule_expire(struct ofproto *, struct rule *);
3827 static void ofproto_expire_facets(struct ofproto *, int dp_max_idle);
3829 /* This function is called periodically by ofproto_run(). Its job is to
3830 * collect updates for the flows that have been installed into the datapath,
3831 * most importantly when they last were used, and then use that information to
3832 * expire flows that have not been used recently.
3834 * Returns the number of milliseconds after which it should be called again. */
3836 ofproto_expire(struct ofproto *ofproto)
3838 struct rule *rule, *next_rule;
3839 struct cls_cursor cursor;
3842 /* Update stats for each flow in the datapath. */
3843 ofproto_update_stats(ofproto);
3845 /* Expire facets that have been idle too long. */
3846 dp_max_idle = ofproto_dp_max_idle(ofproto);
3847 ofproto_expire_facets(ofproto, dp_max_idle);
3849 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3850 cls_cursor_init(&cursor, &ofproto->cls, NULL);
3851 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3852 rule_expire(ofproto, rule);
3855 /* Let the hook know that we're at a stable point: all outstanding data
3856 * in existing flows has been accounted to the account_cb. Thus, the
3857 * hook can now reasonably do operations that depend on having accurate
3858 * flow volume accounting (currently, that's just bond rebalancing). */
3859 if (ofproto->ofhooks->account_checkpoint_cb) {
3860 ofproto->ofhooks->account_checkpoint_cb(ofproto->aux);
3863 return MIN(dp_max_idle, 1000);
3866 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3868 * This function also pushes statistics updates to rules which each facet
3869 * resubmits into. Generally these statistics will be accurate. However, if a
3870 * facet changes the rule it resubmits into at some time in between
3871 * ofproto_update_stats() runs, it is possible that statistics accrued to the
3872 * old rule will be incorrectly attributed to the new rule. This could be
3873 * avoided by calling ofproto_update_stats() whenever rules are created or
3874 * deleted. However, the performance impact of making so many calls to the
3875 * datapath do not justify the benefit of having perfectly accurate statistics.
3878 ofproto_update_stats(struct ofproto *p)
3880 const struct dpif_flow_stats *stats;
3881 struct dpif_flow_dump dump;
3882 const struct nlattr *key;
3885 dpif_flow_dump_start(&dump, p->dpif);
3886 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3887 struct facet *facet;
3890 if (odp_flow_key_to_flow(key, key_len, &flow)) {
3894 odp_flow_key_format(key, key_len, &s);
3895 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
3901 facet = facet_find(p, &flow);
3903 if (facet && facet->installed) {
3905 if (stats->n_packets >= facet->dp_packet_count) {
3906 facet->packet_count += stats->n_packets - facet->dp_packet_count;
3908 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3911 if (stats->n_bytes >= facet->dp_byte_count) {
3912 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
3914 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3917 facet->dp_packet_count = stats->n_packets;
3918 facet->dp_byte_count = stats->n_bytes;
3920 facet_update_time(p, facet, stats->used);
3921 facet_account(p, facet, stats->n_bytes);
3922 facet_push_stats(p, facet);
3924 /* There's a flow in the datapath that we know nothing about.
3926 COVERAGE_INC(ofproto_unexpected_rule);
3927 dpif_flow_del(p->dpif, key, key_len, NULL);
3930 dpif_flow_dump_done(&dump);
3933 /* Calculates and returns the number of milliseconds of idle time after which
3934 * facets should expire from the datapath and we should fold their statistics
3935 * into their parent rules in userspace. */
3937 ofproto_dp_max_idle(const struct ofproto *ofproto)
3940 * Idle time histogram.
3942 * Most of the time a switch has a relatively small number of facets. When
3943 * this is the case we might as well keep statistics for all of them in
3944 * userspace and to cache them in the kernel datapath for performance as
3947 * As the number of facets increases, the memory required to maintain
3948 * statistics about them in userspace and in the kernel becomes
3949 * significant. However, with a large number of facets it is likely that
3950 * only a few of them are "heavy hitters" that consume a large amount of
3951 * bandwidth. At this point, only heavy hitters are worth caching in the
3952 * kernel and maintaining in userspaces; other facets we can discard.
3954 * The technique used to compute the idle time is to build a histogram with
3955 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
3956 * that is installed in the kernel gets dropped in the appropriate bucket.
3957 * After the histogram has been built, we compute the cutoff so that only
3958 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
3959 * cached. At least the most-recently-used bucket of facets is kept, so
3960 * actually an arbitrary number of facets can be kept in any given
3961 * expiration run (though the next run will delete most of those unless
3962 * they receive additional data).
3964 * This requires a second pass through the facets, in addition to the pass
3965 * made by ofproto_update_stats(), because the former function never looks
3966 * at uninstallable facets.
3968 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
3969 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3970 int buckets[N_BUCKETS] = { 0 };
3971 struct facet *facet;
3976 total = hmap_count(&ofproto->facets);
3977 if (total <= 1000) {
3978 return N_BUCKETS * BUCKET_WIDTH;
3981 /* Build histogram. */
3983 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
3984 long long int idle = now - facet->used;
3985 int bucket = (idle <= 0 ? 0
3986 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
3987 : (unsigned int) idle / BUCKET_WIDTH);
3991 /* Find the first bucket whose flows should be expired. */
3992 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
3993 if (buckets[bucket]) {
3996 subtotal += buckets[bucket++];
3997 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
4002 if (VLOG_IS_DBG_ENABLED()) {
4006 ds_put_cstr(&s, "keep");
4007 for (i = 0; i < N_BUCKETS; i++) {
4009 ds_put_cstr(&s, ", drop");
4012 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4015 VLOG_INFO("%s: %s (msec:count)",
4016 dpif_name(ofproto->dpif), ds_cstr(&s));
4020 return bucket * BUCKET_WIDTH;
4024 facet_active_timeout(struct ofproto *ofproto, struct facet *facet)
4026 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
4027 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
4028 struct ofexpired expired;
4030 if (facet->installed) {
4031 struct dpif_flow_stats stats;
4033 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
4035 facet_update_stats(ofproto, facet, &stats);
4038 expired.flow = facet->flow;
4039 expired.packet_count = facet->packet_count;
4040 expired.byte_count = facet->byte_count;
4041 expired.used = facet->used;
4042 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4047 ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle)
4049 long long int cutoff = time_msec() - dp_max_idle;
4050 struct facet *facet, *next_facet;
4052 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
4053 facet_active_timeout(ofproto, facet);
4054 if (facet->used < cutoff) {
4055 facet_remove(ofproto, facet);
4060 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4061 * then delete it entirely. */
4063 rule_expire(struct ofproto *ofproto, struct rule *rule)
4065 struct facet *facet, *next_facet;
4069 /* Has 'rule' expired? */
4071 if (rule->hard_timeout
4072 && now > rule->created + rule->hard_timeout * 1000) {
4073 reason = OFPRR_HARD_TIMEOUT;
4074 } else if (rule->idle_timeout && list_is_empty(&rule->facets)
4075 && now >rule->used + rule->idle_timeout * 1000) {
4076 reason = OFPRR_IDLE_TIMEOUT;
4081 COVERAGE_INC(ofproto_expired);
4083 /* Update stats. (This is a no-op if the rule expired due to an idle
4084 * timeout, because that only happens when the rule has no facets left.) */
4085 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4086 facet_remove(ofproto, facet);
4089 /* Get rid of the rule. */
4090 if (!rule_is_hidden(rule)) {
4091 rule_send_removed(ofproto, rule, reason);
4093 rule_remove(ofproto, rule);
4097 rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason)
4099 struct ofputil_flow_removed fr;
4101 if (!rule->send_flow_removed) {
4106 fr.cookie = rule->flow_cookie;
4108 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
4109 fr.idle_timeout = rule->idle_timeout;
4110 fr.packet_count = rule->packet_count;
4111 fr.byte_count = rule->byte_count;
4113 connmgr_send_flow_removed(p->connmgr, &fr);
4116 /* Obtains statistics for 'rule' and stores them in '*packets' and '*bytes'.
4117 * The returned statistics include statistics for all of 'rule''s facets. */
4119 rule_get_stats(const struct rule *rule, uint64_t *packets, uint64_t *bytes)
4122 struct facet *facet;
4124 /* Start from historical data for 'rule' itself that are no longer tracked
4125 * in facets. This counts, for example, facets that have expired. */
4126 p = rule->packet_count;
4127 b = rule->byte_count;
4129 /* Add any statistics that are tracked by facets. This includes
4130 * statistical data recently updated by ofproto_update_stats() as well as
4131 * stats for packets that were executed "by hand" via dpif_execute(). */
4132 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4133 p += facet->packet_count;
4134 b += facet->byte_count;
4141 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
4142 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
4143 * their individual configurations.
4145 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
4146 * Otherwise, ownership is transferred to this function. */
4148 send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall,
4149 const struct flow *flow, bool clone)
4151 struct ofputil_packet_in pin;
4153 pin.packet = upcall->packet;
4154 pin.in_port = odp_port_to_ofp_port(flow->in_port);
4155 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
4156 pin.buffer_id = 0; /* not yet known */
4157 pin.send_len = upcall->userdata;
4158 connmgr_send_packet_in(ofproto->connmgr, upcall, flow,
4159 clone ? NULL : upcall->packet);
4163 pick_datapath_id(const struct ofproto *ofproto)
4165 const struct ofport *port;
4167 port = get_port(ofproto, ODPP_LOCAL);
4169 uint8_t ea[ETH_ADDR_LEN];
4172 error = netdev_get_etheraddr(port->netdev, ea);
4174 return eth_addr_to_uint64(ea);
4176 VLOG_WARN("could not get MAC address for %s (%s)",
4177 netdev_get_name(port->netdev), strerror(error));
4179 return ofproto->fallback_dpid;
4183 pick_fallback_dpid(void)
4185 uint8_t ea[ETH_ADDR_LEN];
4186 eth_addr_nicira_random(ea);
4187 return eth_addr_to_uint64(ea);
4191 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
4192 void *aux OVS_UNUSED)
4194 const struct shash_node *node;
4198 SHASH_FOR_EACH (node, &all_ofprotos) {
4199 ds_put_format(&results, "%s\n", node->name);
4201 unixctl_command_reply(conn, 200, ds_cstr(&results));
4202 ds_destroy(&results);
4205 struct ofproto_trace {
4206 struct action_xlate_ctx ctx;
4212 trace_format_rule(struct ds *result, int level, const struct rule *rule)
4214 ds_put_char_multiple(result, '\t', level);
4216 ds_put_cstr(result, "No match\n");
4220 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
4221 ntohll(rule->flow_cookie));
4222 cls_rule_format(&rule->cr, result);
4223 ds_put_char(result, '\n');
4225 ds_put_char_multiple(result, '\t', level);
4226 ds_put_cstr(result, "OpenFlow ");
4227 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
4228 rule->n_actions * sizeof *rule->actions);
4229 ds_put_char(result, '\n');
4233 trace_format_flow(struct ds *result, int level, const char *title,
4234 struct ofproto_trace *trace)
4236 ds_put_char_multiple(result, '\t', level);
4237 ds_put_format(result, "%s: ", title);
4238 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4239 ds_put_cstr(result, "unchanged");
4241 flow_format(result, &trace->ctx.flow);
4242 trace->flow = trace->ctx.flow;
4244 ds_put_char(result, '\n');
4248 trace_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
4250 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4251 struct ds *result = trace->result;
4253 ds_put_char(result, '\n');
4254 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4255 trace_format_rule(result, ctx->recurse + 1, rule);
4259 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4260 void *aux OVS_UNUSED)
4262 char *dpname, *in_port_s, *tun_id_s, *packet_s;
4263 char *args = xstrdup(args_);
4264 char *save_ptr = NULL;
4265 struct ofproto *ofproto;
4266 struct ofpbuf packet;
4274 ofpbuf_init(&packet, strlen(args) / 2);
4277 dpname = strtok_r(args, " ", &save_ptr);
4278 tun_id_s = strtok_r(NULL, " ", &save_ptr);
4279 in_port_s = strtok_r(NULL, " ", &save_ptr);
4280 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4281 if (!dpname || !in_port_s || !packet_s) {
4282 unixctl_command_reply(conn, 501, "Bad command syntax");
4286 ofproto = shash_find_data(&all_ofprotos, dpname);
4288 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4293 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
4294 in_port = ofp_port_to_odp_port(atoi(in_port_s));
4296 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
4297 packet_s += strspn(packet_s, " ");
4298 if (*packet_s != '\0') {
4299 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4302 if (packet.size < ETH_HEADER_LEN) {
4303 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
4307 ds_put_cstr(&result, "Packet: ");
4308 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
4309 ds_put_cstr(&result, s);
4312 flow_extract(&packet, tun_id, in_port, &flow);
4313 ds_put_cstr(&result, "Flow: ");
4314 flow_format(&result, &flow);
4315 ds_put_char(&result, '\n');
4317 rule = rule_lookup(ofproto, &flow);
4318 trace_format_rule(&result, 0, rule);
4320 struct ofproto_trace trace;
4321 struct ofpbuf *odp_actions;
4323 trace.result = &result;
4325 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
4326 trace.ctx.resubmit_hook = trace_resubmit;
4327 odp_actions = xlate_actions(&trace.ctx,
4328 rule->actions, rule->n_actions);
4330 ds_put_char(&result, '\n');
4331 trace_format_flow(&result, 0, "Final flow", &trace);
4332 ds_put_cstr(&result, "Datapath actions: ");
4333 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4334 ofpbuf_delete(odp_actions);
4337 unixctl_command_reply(conn, 200, ds_cstr(&result));
4340 ds_destroy(&result);
4341 ofpbuf_uninit(&packet);
4346 ofproto_unixctl_init(void)
4348 static bool registered;
4354 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
4355 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4359 default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
4360 struct ofpbuf *odp_actions, tag_type *tags,
4361 uint16_t *nf_output_iface, void *ofproto_)
4363 struct ofproto *ofproto = ofproto_;
4364 struct mac_entry *dst_mac;
4366 /* Drop frames for reserved multicast addresses. */
4367 if (eth_addr_is_reserved(flow->dl_dst)) {
4371 /* Learn source MAC (but don't try to learn from revalidation). */
4373 && mac_learning_may_learn(ofproto->ml, flow->dl_src, 0)) {
4374 struct mac_entry *src_mac;
4376 src_mac = mac_learning_insert(ofproto->ml, flow->dl_src, 0);
4377 if (mac_entry_is_new(src_mac) || src_mac->port.i != flow->in_port) {
4378 /* The log messages here could actually be useful in debugging,
4379 * so keep the rate limit relatively high. */
4380 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4381 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
4382 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
4384 ofproto_revalidate(ofproto,
4385 mac_learning_changed(ofproto->ml, src_mac));
4386 src_mac->port.i = flow->in_port;
4390 /* Determine output port. */
4391 dst_mac = mac_learning_lookup(ofproto->ml, flow->dl_dst, 0, tags);
4393 flood_packets(ofproto, flow->in_port, OFPPC_NO_FLOOD,
4394 nf_output_iface, odp_actions);
4396 int out_port = dst_mac->port.i;
4397 if (out_port != flow->in_port) {
4398 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, out_port);
4399 *nf_output_iface = out_port;
4408 static const struct ofhooks default_ofhooks = {
4409 default_normal_ofhook_cb,