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>
28 #include "byte-order.h"
30 #include "classifier.h"
34 #include "dynamic-string.h"
35 #include "fail-open.h"
39 #include "mac-learning.h"
40 #include "multipath.h"
46 #include "ofp-print.h"
48 #include "ofproto-sflow.h"
50 #include "openflow/nicira-ext.h"
51 #include "openflow/openflow.h"
52 #include "openvswitch/datapath-protocol.h"
56 #include "poll-loop.h"
60 #include "stream-ssl.h"
64 #include "unaligned.h"
69 VLOG_DEFINE_THIS_MODULE(ofproto);
71 COVERAGE_DEFINE(facet_changed_rule);
72 COVERAGE_DEFINE(facet_revalidate);
73 COVERAGE_DEFINE(odp_overflow);
74 COVERAGE_DEFINE(ofproto_agg_request);
75 COVERAGE_DEFINE(ofproto_costly_flags);
76 COVERAGE_DEFINE(ofproto_ctlr_action);
77 COVERAGE_DEFINE(ofproto_del_rule);
78 COVERAGE_DEFINE(ofproto_error);
79 COVERAGE_DEFINE(ofproto_expiration);
80 COVERAGE_DEFINE(ofproto_expired);
81 COVERAGE_DEFINE(ofproto_flows_req);
82 COVERAGE_DEFINE(ofproto_flush);
83 COVERAGE_DEFINE(ofproto_invalidated);
84 COVERAGE_DEFINE(ofproto_no_packet_in);
85 COVERAGE_DEFINE(ofproto_ofp2odp);
86 COVERAGE_DEFINE(ofproto_packet_in);
87 COVERAGE_DEFINE(ofproto_packet_out);
88 COVERAGE_DEFINE(ofproto_queue_req);
89 COVERAGE_DEFINE(ofproto_recv_openflow);
90 COVERAGE_DEFINE(ofproto_reinit_ports);
91 COVERAGE_DEFINE(ofproto_unexpected_rule);
92 COVERAGE_DEFINE(ofproto_uninstallable);
93 COVERAGE_DEFINE(ofproto_update_port);
95 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
96 * flow translation. */
97 #define MAX_RESUBMIT_RECURSION 16
102 struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
103 struct netdev *netdev;
104 struct ofp_phy_port opp;
106 struct cfm *cfm; /* Connectivity Fault Management, if any. */
109 static void ofport_free(struct ofport *);
110 static void ofport_run(struct ofproto *, struct ofport *);
111 static void ofport_wait(struct ofport *);
113 struct action_xlate_ctx {
114 /* action_xlate_ctx_init() initializes these members. */
117 struct ofproto *ofproto;
119 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
120 * this flow when actions change header fields. */
123 /* The packet corresponding to 'flow', or a null pointer if we are
124 * revalidating without a packet to refer to. */
125 const struct ofpbuf *packet;
127 /* If nonnull, called just before executing a resubmit action.
129 * This is normally null so the client has to set it manually after
130 * calling action_xlate_ctx_init(). */
131 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule *);
133 /* If true, the speciality of 'flow' should be checked before executing
134 * its actions. If special_cb returns false on 'flow' rendered
135 * uninstallable and no actions will be executed. */
138 /* xlate_actions() initializes and uses these members. The client might want
139 * to look at them after it returns. */
141 struct ofpbuf *odp_actions; /* Datapath actions. */
142 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
143 bool may_set_up_flow; /* True ordinarily; false if the actions must
144 * be reassessed for every packet. */
145 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
147 /* xlate_actions() initializes and uses these members, but the client has no
148 * reason to look at them. */
150 int recurse; /* Recursion level, via xlate_table_action. */
151 int last_pop_priority; /* Offset in 'odp_actions' just past most
152 * recent ODP_ACTION_ATTR_SET_PRIORITY. */
155 static void action_xlate_ctx_init(struct action_xlate_ctx *,
156 struct ofproto *, const struct flow *,
157 const struct ofpbuf *);
158 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
159 const union ofp_action *in, size_t n_in);
161 /* An OpenFlow flow. */
163 long long int used; /* Time last used; time created if not used. */
164 long long int created; /* Creation time. */
168 * - Do include packets and bytes from facets that have been deleted or
169 * whose own statistics have been folded into the rule.
171 * - Do include packets and bytes sent "by hand" that were accounted to
172 * the rule without any facet being involved (this is a rare corner
173 * case in rule_execute()).
175 * - Do not include packet or bytes that can be obtained from any facet's
176 * packet_count or byte_count member or that can be obtained from the
177 * datapath by, e.g., dpif_flow_get() for any facet.
179 uint64_t packet_count; /* Number of packets received. */
180 uint64_t byte_count; /* Number of bytes received. */
182 ovs_be64 flow_cookie; /* Controller-issued identifier. */
184 struct cls_rule cr; /* In owning ofproto's classifier. */
185 uint16_t idle_timeout; /* In seconds from time of last use. */
186 uint16_t hard_timeout; /* In seconds from time of creation. */
187 bool send_flow_removed; /* Send a flow removed message? */
188 int n_actions; /* Number of elements in actions[]. */
189 union ofp_action *actions; /* OpenFlow actions. */
190 struct list facets; /* List of "struct facet"s. */
193 static struct rule *rule_from_cls_rule(const struct cls_rule *);
194 static bool rule_is_hidden(const struct rule *);
196 static struct rule *rule_create(const struct cls_rule *,
197 const union ofp_action *, size_t n_actions,
198 uint16_t idle_timeout, uint16_t hard_timeout,
199 ovs_be64 flow_cookie, bool send_flow_removed);
200 static void rule_destroy(struct ofproto *, struct rule *);
201 static void rule_free(struct rule *);
203 static struct rule *rule_lookup(struct ofproto *, const struct flow *);
204 static void rule_insert(struct ofproto *, struct rule *);
205 static void rule_remove(struct ofproto *, struct rule *);
207 static void rule_send_removed(struct ofproto *, struct rule *, uint8_t reason);
208 static void rule_get_stats(const struct rule *, uint64_t *packets,
211 /* An exact-match instantiation of an OpenFlow flow. */
213 long long int used; /* Time last used; time created if not used. */
217 * - Do include packets and bytes sent "by hand", e.g. with
220 * - Do include packets and bytes that were obtained from the datapath
221 * when a flow was deleted (e.g. dpif_flow_del()) or when its
222 * statistics were reset (e.g. dpif_flow_put() with
223 * DPIF_FP_ZERO_STATS).
225 * - Do not include any packets or bytes that can currently be obtained
226 * from the datapath by, e.g., dpif_flow_get().
228 uint64_t packet_count; /* Number of packets received. */
229 uint64_t byte_count; /* Number of bytes received. */
231 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
232 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
234 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
235 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
236 long long int rs_used; /* Used time pushed to resubmit children. */
238 /* Number of bytes passed to account_cb. This may include bytes that can
239 * currently obtained from the datapath (thus, it can be greater than
241 uint64_t accounted_bytes;
243 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
244 struct list list_node; /* In owning rule's 'facets' list. */
245 struct rule *rule; /* Owning rule. */
246 struct flow flow; /* Exact-match flow. */
247 bool installed; /* Installed in datapath? */
248 bool may_install; /* True ordinarily; false if actions must
249 * be reassessed for every packet. */
250 size_t actions_len; /* Number of bytes in actions[]. */
251 struct nlattr *actions; /* Datapath actions. */
252 tag_type tags; /* Tags (set only by hooks). */
253 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
256 static struct facet *facet_create(struct ofproto *, struct rule *,
258 const struct ofpbuf *packet);
259 static void facet_remove(struct ofproto *, struct facet *);
260 static void facet_free(struct facet *);
262 static struct facet *facet_lookup_valid(struct ofproto *, const struct flow *);
263 static bool facet_revalidate(struct ofproto *, struct facet *);
265 static void facet_install(struct ofproto *, struct facet *, bool zero_stats);
266 static void facet_uninstall(struct ofproto *, struct facet *);
267 static void facet_flush_stats(struct ofproto *, struct facet *);
269 static void facet_make_actions(struct ofproto *, struct facet *,
270 const struct ofpbuf *packet);
271 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
272 static void facet_update_stats(struct ofproto *, struct facet *,
273 const struct dpif_flow_stats *);
274 static void facet_push_stats(struct ofproto *, struct facet *);
276 static void send_packet_in(struct ofproto *, struct dpif_upcall *,
277 const struct flow *, bool clone);
281 uint64_t datapath_id; /* Datapath ID. */
282 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
283 char *mfr_desc; /* Manufacturer. */
284 char *hw_desc; /* Hardware. */
285 char *sw_desc; /* Software version. */
286 char *serial_desc; /* Serial number. */
287 char *dp_desc; /* Datapath description. */
291 struct netdev_monitor *netdev_monitor;
292 struct hmap ports; /* Contains "struct ofport"s. */
293 struct shash port_by_name;
297 struct netflow *netflow;
298 struct ofproto_sflow *sflow;
301 struct classifier cls;
302 struct timer next_expiration;
306 bool need_revalidate;
307 struct tag_set revalidate_set;
309 /* OpenFlow connections. */
310 struct connmgr *connmgr;
312 /* Hooks for ovs-vswitchd. */
313 const struct ofhooks *ofhooks;
316 /* Used by default ofhooks. */
317 struct mac_learning *ml;
320 /* Map from dpif name to struct ofproto, for use by unixctl commands. */
321 static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos);
323 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
325 static const struct ofhooks default_ofhooks;
327 static uint64_t pick_datapath_id(const struct ofproto *);
328 static uint64_t pick_fallback_dpid(void);
330 static void ofproto_flush_flows__(struct ofproto *);
331 static int ofproto_expire(struct ofproto *);
332 static void flow_push_stats(struct ofproto *, const struct rule *,
333 struct flow *, uint64_t packets, uint64_t bytes,
336 static void handle_upcall(struct ofproto *, struct dpif_upcall *);
338 static void handle_openflow(struct ofconn *, struct ofpbuf *);
340 static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
341 static void update_port(struct ofproto *, const char *devname);
342 static int init_ports(struct ofproto *);
343 static void reinit_ports(struct ofproto *);
345 static void ofproto_unixctl_init(void);
348 ofproto_create(const char *datapath, const char *datapath_type,
349 const struct ofhooks *ofhooks, void *aux,
350 struct ofproto **ofprotop)
352 char local_name[IF_NAMESIZE];
359 ofproto_unixctl_init();
361 /* Connect to datapath and start listening for messages. */
362 error = dpif_open(datapath, datapath_type, &dpif);
364 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
367 error = dpif_recv_set_mask(dpif,
368 ((1u << DPIF_UC_MISS) |
369 (1u << DPIF_UC_ACTION) |
370 (1u << DPIF_UC_SAMPLE)));
372 VLOG_ERR("failed to listen on datapath %s: %s",
373 datapath, strerror(error));
377 dpif_flow_flush(dpif);
378 dpif_recv_purge(dpif);
380 error = dpif_port_get_name(dpif, ODPP_LOCAL,
381 local_name, sizeof local_name);
383 VLOG_ERR("%s: cannot get name of datapath local port (%s)",
384 datapath, strerror(error));
388 /* Initialize settings. */
389 p = xzalloc(sizeof *p);
390 p->fallback_dpid = pick_fallback_dpid();
391 p->datapath_id = p->fallback_dpid;
392 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
393 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
394 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
395 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
396 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
398 /* Initialize datapath. */
400 p->netdev_monitor = netdev_monitor_create();
401 hmap_init(&p->ports);
402 shash_init(&p->port_by_name);
403 p->max_ports = dpif_get_max_ports(dpif);
405 /* Initialize submodules. */
409 /* Initialize flow table. */
410 classifier_init(&p->cls);
411 timer_set_duration(&p->next_expiration, 1000);
413 /* Initialize facet table. */
414 hmap_init(&p->facets);
415 p->need_revalidate = false;
416 tag_set_init(&p->revalidate_set);
418 /* Initialize hooks. */
420 p->ofhooks = ofhooks;
424 p->ofhooks = &default_ofhooks;
426 p->ml = mac_learning_create();
429 /* Pick final datapath ID. */
430 p->datapath_id = pick_datapath_id(p);
431 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
433 shash_add_once(&all_ofprotos, dpif_name(p->dpif), p);
435 /* Initialize OpenFlow connections. */
436 p->connmgr = connmgr_create(p, datapath, local_name);
443 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
445 uint64_t old_dpid = p->datapath_id;
446 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
447 if (p->datapath_id != old_dpid) {
448 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
450 /* Force all active connections to reconnect, since there is no way to
451 * notify a controller that the datapath ID has changed. */
452 ofproto_reconnect_controllers(p);
457 ofproto_set_controllers(struct ofproto *p,
458 const struct ofproto_controller *controllers,
459 size_t n_controllers)
461 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
465 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
467 connmgr_set_fail_mode(p->connmgr, fail_mode);
470 /* Drops the connections between 'ofproto' and all of its controllers, forcing
471 * them to reconnect. */
473 ofproto_reconnect_controllers(struct ofproto *ofproto)
475 connmgr_reconnect(ofproto->connmgr);
478 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
479 * in-band control should guarantee access, in the same way that in-band
480 * control guarantees access to OpenFlow controllers. */
482 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
483 const struct sockaddr_in *extras, size_t n)
485 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
488 /* Sets the OpenFlow queue used by flows set up by in-band control on
489 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
490 * flows will use the default queue. */
492 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
494 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
498 ofproto_set_desc(struct ofproto *p,
499 const char *mfr_desc, const char *hw_desc,
500 const char *sw_desc, const char *serial_desc,
503 struct ofp_desc_stats *ods;
506 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
507 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
508 sizeof ods->mfr_desc);
511 p->mfr_desc = xstrdup(mfr_desc);
514 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
515 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
516 sizeof ods->hw_desc);
519 p->hw_desc = xstrdup(hw_desc);
522 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
523 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
524 sizeof ods->sw_desc);
527 p->sw_desc = xstrdup(sw_desc);
530 if (strlen(serial_desc) >= sizeof ods->serial_num) {
531 VLOG_WARN("truncating serial_desc, must be less than %zu "
533 sizeof ods->serial_num);
535 free(p->serial_desc);
536 p->serial_desc = xstrdup(serial_desc);
539 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
540 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
541 sizeof ods->dp_desc);
544 p->dp_desc = xstrdup(dp_desc);
549 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
551 return connmgr_set_snoops(ofproto->connmgr, snoops);
555 ofproto_set_netflow(struct ofproto *ofproto,
556 const struct netflow_options *nf_options)
558 if (nf_options && !sset_is_empty(&nf_options->collectors)) {
559 if (!ofproto->netflow) {
560 ofproto->netflow = netflow_create();
562 return netflow_set_options(ofproto->netflow, nf_options);
564 netflow_destroy(ofproto->netflow);
565 ofproto->netflow = NULL;
571 ofproto_set_sflow(struct ofproto *ofproto,
572 const struct ofproto_sflow_options *oso)
574 struct ofproto_sflow *os = ofproto->sflow;
577 struct ofport *ofport;
579 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
580 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
581 ofproto_sflow_add_port(os, ofport->odp_port,
582 netdev_get_name(ofport->netdev));
585 ofproto_sflow_set_options(os, oso);
587 ofproto_sflow_destroy(os);
588 ofproto->sflow = NULL;
592 /* Connectivity Fault Management configuration. */
594 /* Clears the CFM configuration from 'port_no' on 'ofproto'. */
596 ofproto_iface_clear_cfm(struct ofproto *ofproto, uint32_t port_no)
598 struct ofport *ofport = get_port(ofproto, port_no);
599 if (ofport && ofport->cfm){
600 cfm_destroy(ofport->cfm);
605 /* Configures connectivity fault management on 'port_no' in 'ofproto'. Takes
606 * basic configuration from the configuration members in 'cfm', and the set of
607 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
608 * Ignores the statistics members of 'cfm'.
610 * This function has no effect if 'ofproto' does not have a port 'port_no'. */
612 ofproto_iface_set_cfm(struct ofproto *ofproto, uint32_t port_no,
613 const struct cfm *cfm,
614 const uint16_t *remote_mps, size_t n_remote_mps)
616 struct ofport *ofport;
618 ofport = get_port(ofproto, port_no);
620 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
621 dpif_name(ofproto->dpif), port_no);
626 ofport->cfm = cfm_create();
629 ofport->cfm->mpid = cfm->mpid;
630 ofport->cfm->interval = cfm->interval;
631 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
633 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
635 if (!cfm_configure(ofport->cfm)) {
636 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed",
637 dpif_name(ofproto->dpif), port_no,
638 netdev_get_name(ofport->netdev));
639 cfm_destroy(ofport->cfm);
644 /* Returns the connectivity fault management object associated with 'port_no'
645 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
646 * 'port_no' or if that port does not have CFM configured. The caller must not
647 * modify or destroy the returned object. */
649 ofproto_iface_get_cfm(struct ofproto *ofproto, uint32_t port_no)
651 struct ofport *ofport = get_port(ofproto, port_no);
652 return ofport ? ofport->cfm : NULL;
656 ofproto_get_datapath_id(const struct ofproto *ofproto)
658 return ofproto->datapath_id;
662 ofproto_has_primary_controller(const struct ofproto *ofproto)
664 return connmgr_has_controllers(ofproto->connmgr);
667 enum ofproto_fail_mode
668 ofproto_get_fail_mode(const struct ofproto *p)
670 return connmgr_get_fail_mode(p->connmgr);
674 ofproto_has_snoops(const struct ofproto *ofproto)
676 return connmgr_has_snoops(ofproto->connmgr);
680 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
682 connmgr_get_snoops(ofproto->connmgr, snoops);
686 ofproto_destroy(struct ofproto *p)
688 struct ofport *ofport, *next_ofport;
694 shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif));
696 ofproto_flush_flows__(p);
697 connmgr_destroy(p->connmgr);
698 classifier_destroy(&p->cls);
699 hmap_destroy(&p->facets);
702 netdev_monitor_destroy(p->netdev_monitor);
703 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
704 hmap_remove(&p->ports, &ofport->hmap_node);
707 shash_destroy(&p->port_by_name);
709 netflow_destroy(p->netflow);
710 ofproto_sflow_destroy(p->sflow);
712 mac_learning_destroy(p->ml);
717 free(p->serial_desc);
720 hmap_destroy(&p->ports);
726 ofproto_run(struct ofproto *p)
728 int error = ofproto_run1(p);
730 error = ofproto_run2(p, false);
736 process_port_change(struct ofproto *ofproto, int error, char *devname)
738 if (error == ENOBUFS) {
739 reinit_ports(ofproto);
741 update_port(ofproto, devname);
747 ofproto_run1(struct ofproto *p)
749 struct ofport *ofport;
754 if (shash_is_empty(&p->port_by_name)) {
758 for (i = 0; i < 50; i++) {
759 struct dpif_upcall packet;
761 error = dpif_recv(p->dpif, &packet);
763 if (error == ENODEV) {
764 /* Someone destroyed the datapath behind our back. The caller
765 * better destroy us and give up, because we're just going to
766 * spin from here on out. */
767 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
768 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
775 handle_upcall(p, &packet);
778 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
779 process_port_change(p, error, devname);
781 while ((error = netdev_monitor_poll(p->netdev_monitor,
782 &devname)) != EAGAIN) {
783 process_port_change(p, error, devname);
786 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
787 ofport_run(p, ofport);
790 connmgr_run(p->connmgr, handle_openflow);
792 if (timer_expired(&p->next_expiration)) {
793 int delay = ofproto_expire(p);
794 timer_set_duration(&p->next_expiration, delay);
795 COVERAGE_INC(ofproto_expiration);
799 netflow_run(p->netflow);
802 ofproto_sflow_run(p->sflow);
809 ofproto_run2(struct ofproto *p, bool revalidate_all)
811 /* Figure out what we need to revalidate now, if anything. */
812 struct tag_set revalidate_set = p->revalidate_set;
813 if (p->need_revalidate) {
814 revalidate_all = true;
817 /* Clear the revalidation flags. */
818 tag_set_init(&p->revalidate_set);
819 p->need_revalidate = false;
821 /* Now revalidate if there's anything to do. */
822 if (revalidate_all || !tag_set_is_empty(&revalidate_set)) {
823 struct facet *facet, *next;
825 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) {
827 || tag_set_intersects(&revalidate_set, facet->tags)) {
828 facet_revalidate(p, facet);
837 ofproto_wait(struct ofproto *p)
839 struct ofport *ofport;
841 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
844 dpif_recv_wait(p->dpif);
845 dpif_port_poll_wait(p->dpif);
846 netdev_monitor_poll_wait(p->netdev_monitor);
848 ofproto_sflow_wait(p->sflow);
850 if (!tag_set_is_empty(&p->revalidate_set)) {
851 poll_immediate_wake();
853 if (p->need_revalidate) {
854 /* Shouldn't happen, but if it does just go around again. */
855 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
856 poll_immediate_wake();
858 timer_wait(&p->next_expiration);
860 connmgr_wait(p->connmgr);
864 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
866 tag_set_add(&ofproto->revalidate_set, tag);
870 ofproto_get_revalidate_set(struct ofproto *ofproto)
872 return &ofproto->revalidate_set;
876 ofproto_is_alive(const struct ofproto *p)
878 return connmgr_has_controllers(p->connmgr);
882 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
885 connmgr_get_controller_info(ofproto->connmgr, info);
889 ofproto_free_ofproto_controller_info(struct shash *info)
891 struct shash_node *node;
893 SHASH_FOR_EACH (node, info) {
894 struct ofproto_controller_info *cinfo = node->data;
895 while (cinfo->pairs.n) {
896 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
903 /* Deletes port number 'odp_port' from the datapath for 'ofproto'.
905 * This is almost the same as calling dpif_port_del() directly on the
906 * datapath, but it also makes 'ofproto' close its open netdev for the port
907 * (if any). This makes it possible to create a new netdev of a different
908 * type under the same name, which otherwise the netdev library would refuse
909 * to do because of the conflict. (The netdev would eventually get closed on
910 * the next trip through ofproto_run(), but this interface is more direct.)
912 * Returns 0 if successful, otherwise a positive errno. */
914 ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port)
916 struct ofport *ofport = get_port(ofproto, odp_port);
917 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
920 error = dpif_port_del(ofproto->dpif, odp_port);
922 VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)",
923 dpif_name(ofproto->dpif), odp_port, name, strerror(error));
925 /* 'name' is the netdev's name and update_port() is going to close the
926 * netdev. Just in case update_port() refers to 'name' after it
927 * destroys 'ofport', make a copy of it around the update_port()
929 char *devname = xstrdup(name);
930 update_port(ofproto, devname);
936 /* Checks if 'ofproto' thinks 'odp_port' should be included in floods. Returns
937 * true if 'odp_port' exists and should be included, false otherwise. */
939 ofproto_port_is_floodable(struct ofproto *ofproto, uint16_t odp_port)
941 struct ofport *ofport = get_port(ofproto, odp_port);
942 return ofport && !(ofport->opp.config & htonl(OFPPC_NO_FLOOD));
945 /* Sends 'packet' out of port 'port_no' within 'p'.
947 * Returns 0 if successful, otherwise a positive errno value. */
949 ofproto_send_packet(struct ofproto *ofproto,
950 uint32_t port_no, const struct ofpbuf *packet)
952 struct ofpbuf odp_actions;
955 ofpbuf_init(&odp_actions, 32);
956 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, port_no);
957 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
959 ofpbuf_uninit(&odp_actions);
962 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
963 dpif_name(ofproto->dpif), port_no, strerror(error));
968 /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
969 * performs the 'n_actions' actions in 'actions'. The new flow will not
972 * If cls_rule->priority is in the range of priorities supported by OpenFlow
973 * (0...65535, inclusive) then the flow will be visible to OpenFlow
974 * controllers; otherwise, it will be hidden.
976 * The caller retains ownership of 'cls_rule' and 'actions'. */
978 ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
979 const union ofp_action *actions, size_t n_actions)
982 rule = rule_create(cls_rule, actions, n_actions, 0, 0, 0, false);
983 rule_insert(p, rule);
987 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
991 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
994 rule_remove(ofproto, rule);
999 ofproto_flush_flows__(struct ofproto *ofproto)
1001 struct facet *facet, *next_facet;
1002 struct rule *rule, *next_rule;
1003 struct cls_cursor cursor;
1005 COVERAGE_INC(ofproto_flush);
1007 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1008 /* Mark the facet as not installed so that facet_remove() doesn't
1009 * bother trying to uninstall it. There is no point in uninstalling it
1010 * individually since we are about to blow away all the facets with
1011 * dpif_flow_flush(). */
1012 facet->installed = false;
1013 facet->dp_packet_count = 0;
1014 facet->dp_byte_count = 0;
1015 facet_remove(ofproto, facet);
1018 cls_cursor_init(&cursor, &ofproto->cls, NULL);
1019 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1020 rule_remove(ofproto, rule);
1023 dpif_flow_flush(ofproto->dpif);
1027 ofproto_flush_flows(struct ofproto *ofproto)
1029 ofproto_flush_flows__(ofproto);
1030 connmgr_flushed(ofproto->connmgr);
1034 reinit_ports(struct ofproto *p)
1036 struct dpif_port_dump dump;
1037 struct sset devnames;
1038 struct ofport *ofport;
1039 struct dpif_port dpif_port;
1040 const char *devname;
1042 COVERAGE_INC(ofproto_reinit_ports);
1044 sset_init(&devnames);
1045 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1046 sset_add(&devnames, netdev_get_name(ofport->netdev));
1048 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1049 sset_add(&devnames, dpif_port.name);
1052 SSET_FOR_EACH (devname, &devnames) {
1053 update_port(p, devname);
1055 sset_destroy(&devnames);
1058 /* Opens and returns a netdev for 'dpif_port', or a null pointer if the netdev
1059 * cannot be opened. On success, also fills in 'opp'. */
1060 static struct netdev *
1061 ofport_open(const struct dpif_port *dpif_port, struct ofp_phy_port *opp)
1063 uint32_t curr, advertised, supported, peer;
1064 struct netdev_options netdev_options;
1065 enum netdev_flags flags;
1066 struct netdev *netdev;
1069 memset(&netdev_options, 0, sizeof netdev_options);
1070 netdev_options.name = dpif_port->name;
1071 netdev_options.type = dpif_port->type;
1072 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
1074 error = netdev_open(&netdev_options, &netdev);
1076 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1077 "cannot be opened (%s)",
1078 dpif_port->name, dpif_port->port_no,
1079 dpif_port->name, strerror(error));
1083 netdev_get_flags(netdev, &flags);
1084 netdev_get_features(netdev, &curr, &advertised, &supported, &peer);
1086 opp->port_no = htons(odp_port_to_ofp_port(dpif_port->port_no));
1087 netdev_get_etheraddr(netdev, opp->hw_addr);
1088 ovs_strzcpy(opp->name, dpif_port->name, sizeof opp->name);
1089 opp->config = flags & NETDEV_UP ? 0 : htonl(OFPPC_PORT_DOWN);
1090 opp->state = netdev_get_carrier(netdev) ? 0 : htonl(OFPPS_LINK_DOWN);
1091 opp->curr = htonl(curr);
1092 opp->advertised = htonl(advertised);
1093 opp->supported = htonl(supported);
1094 opp->peer = htonl(peer);
1100 ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port)
1102 if (get_port(p, dpif_port->port_no)) {
1103 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1104 dpif_port->port_no);
1106 } else if (shash_find(&p->port_by_name, dpif_port->name)) {
1107 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1115 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1116 * port number, and 'config' bits other than OFPPC_PORT_DOWN are
1119 ofport_equal(const struct ofp_phy_port *a, const struct ofp_phy_port *b)
1121 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1122 return (!memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1123 && a->state == b->state
1124 && !((a->config ^ b->config) & htonl(OFPPC_PORT_DOWN))
1125 && a->curr == b->curr
1126 && a->advertised == b->advertised
1127 && a->supported == b->supported
1128 && a->peer == b->peer);
1131 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1132 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1133 * one with the same name or port number). */
1135 ofport_install(struct ofproto *p,
1136 struct netdev *netdev, const struct ofp_phy_port *opp)
1138 const char *netdev_name = netdev_get_name(netdev);
1139 struct ofport *ofport;
1141 connmgr_send_port_status(p->connmgr, opp, OFPPR_ADD);
1143 /* Create ofport. */
1144 ofport = xmalloc(sizeof *ofport);
1145 ofport->netdev = netdev;
1147 ofport->odp_port = ofp_port_to_odp_port(ntohs(opp->port_no));
1150 /* Add port to 'p'. */
1151 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1152 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
1153 shash_add(&p->port_by_name, netdev_name, ofport);
1155 ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
1159 /* Removes 'ofport' from 'p' and destroys it. */
1161 ofport_remove(struct ofproto *p, struct ofport *ofport)
1163 connmgr_send_port_status(p->connmgr, &ofport->opp, OFPPR_DELETE);
1165 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1166 hmap_remove(&p->ports, &ofport->hmap_node);
1167 shash_delete(&p->port_by_name,
1168 shash_find(&p->port_by_name,
1169 netdev_get_name(ofport->netdev)));
1171 ofproto_sflow_del_port(p->sflow, ofport->odp_port);
1174 ofport_free(ofport);
1177 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1180 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1182 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1184 ofport_remove(ofproto, port);
1188 /* Updates 'port' within 'ofproto' with the new 'netdev' and 'opp'.
1190 * Does not handle a name or port number change. The caller must implement
1191 * such a change as a delete followed by an add. */
1193 ofport_modified(struct ofproto *ofproto, struct ofport *port,
1194 struct netdev *netdev, struct ofp_phy_port *opp)
1196 memcpy(port->opp.hw_addr, opp->hw_addr, ETH_ADDR_LEN);
1197 port->opp.config = ((port->opp.config & ~htonl(OFPPC_PORT_DOWN))
1198 | (opp->config & htonl(OFPPC_PORT_DOWN)));
1199 port->opp.state = opp->state;
1200 port->opp.curr = opp->curr;
1201 port->opp.advertised = opp->advertised;
1202 port->opp.supported = opp->supported;
1203 port->opp.peer = opp->peer;
1205 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1206 netdev_monitor_add(ofproto->netdev_monitor, netdev);
1208 netdev_close(port->netdev);
1209 port->netdev = netdev;
1211 connmgr_send_port_status(ofproto->connmgr, &port->opp, OFPPR_MODIFY);
1215 ofport_run(struct ofproto *ofproto, struct ofport *ofport)
1218 cfm_run(ofport->cfm);
1220 if (cfm_should_send_ccm(ofport->cfm)) {
1221 struct ofpbuf packet;
1224 ofpbuf_init(&packet, 0);
1225 ccm = eth_compose(&packet, eth_addr_ccm, ofport->opp.hw_addr,
1226 ETH_TYPE_CFM, sizeof *ccm);
1227 cfm_compose_ccm(ofport->cfm, ccm);
1228 ofproto_send_packet(ofproto, ofport->odp_port, &packet);
1229 ofpbuf_uninit(&packet);
1235 ofport_wait(struct ofport *ofport)
1238 cfm_wait(ofport->cfm);
1243 ofport_free(struct ofport *ofport)
1246 cfm_destroy(ofport->cfm);
1247 netdev_close(ofport->netdev);
1252 static struct ofport *
1253 get_port(const struct ofproto *ofproto, uint16_t odp_port)
1255 struct ofport *port;
1257 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1258 hash_int(odp_port, 0), &ofproto->ports) {
1259 if (port->odp_port == odp_port) {
1267 update_port(struct ofproto *ofproto, const char *name)
1269 struct dpif_port dpif_port;
1270 struct ofp_phy_port opp;
1271 struct netdev *netdev;
1272 struct ofport *port;
1274 COVERAGE_INC(ofproto_update_port);
1276 /* Fetch 'name''s location and properties from the datapath. */
1277 netdev = (!dpif_port_query_by_name(ofproto->dpif, name, &dpif_port)
1278 ? ofport_open(&dpif_port, &opp)
1281 port = get_port(ofproto, dpif_port.port_no);
1282 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
1283 /* 'name' hasn't changed location. Any properties changed? */
1284 if (!ofport_equal(&port->opp, &opp)) {
1285 ofport_modified(ofproto, port, netdev, &opp);
1287 netdev_close(netdev);
1290 /* If 'port' is nonnull then its name differs from 'name' and thus
1291 * we should delete it. If we think there's a port named 'name'
1292 * then its port number must be wrong now so delete it too. */
1294 ofport_remove(ofproto, port);
1296 ofport_remove_with_name(ofproto, name);
1297 ofport_install(ofproto, netdev, &opp);
1300 /* Any port named 'name' is gone now. */
1301 ofport_remove_with_name(ofproto, name);
1303 dpif_port_destroy(&dpif_port);
1307 init_ports(struct ofproto *p)
1309 struct dpif_port_dump dump;
1310 struct dpif_port dpif_port;
1312 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1313 if (!ofport_conflicts(p, &dpif_port)) {
1314 struct ofp_phy_port opp;
1315 struct netdev *netdev;
1317 netdev = ofport_open(&dpif_port, &opp);
1319 ofport_install(p, netdev, &opp);
1327 /* Returns true if 'rule' should be hidden from the controller.
1329 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1330 * (e.g. by in-band control) and are intentionally hidden from the
1333 rule_is_hidden(const struct rule *rule)
1335 return rule->cr.priority > UINT16_MAX;
1338 /* Creates and returns a new rule initialized as specified.
1340 * The caller is responsible for inserting the rule into the classifier (with
1341 * rule_insert()). */
1342 static struct rule *
1343 rule_create(const struct cls_rule *cls_rule,
1344 const union ofp_action *actions, size_t n_actions,
1345 uint16_t idle_timeout, uint16_t hard_timeout,
1346 ovs_be64 flow_cookie, bool send_flow_removed)
1348 struct rule *rule = xzalloc(sizeof *rule);
1349 rule->cr = *cls_rule;
1350 rule->idle_timeout = idle_timeout;
1351 rule->hard_timeout = hard_timeout;
1352 rule->flow_cookie = flow_cookie;
1353 rule->used = rule->created = time_msec();
1354 rule->send_flow_removed = send_flow_removed;
1355 list_init(&rule->facets);
1356 if (n_actions > 0) {
1357 rule->n_actions = n_actions;
1358 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1364 static struct rule *
1365 rule_from_cls_rule(const struct cls_rule *cls_rule)
1367 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1371 rule_free(struct rule *rule)
1373 free(rule->actions);
1377 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
1378 * destroying any that no longer has a rule (which is probably all of them).
1380 * The caller must have already removed 'rule' from the classifier. */
1382 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1384 struct facet *facet, *next_facet;
1385 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1386 facet_revalidate(ofproto, facet);
1391 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1392 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1395 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
1397 const union ofp_action *oa;
1398 struct actions_iterator i;
1400 if (out_port == htons(OFPP_NONE)) {
1403 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1404 oa = actions_next(&i)) {
1405 if (action_outputs_to_port(oa, out_port)) {
1412 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1413 * 'packet', which arrived on 'in_port'.
1415 * Takes ownership of 'packet'. */
1417 execute_odp_actions(struct ofproto *ofproto, const struct flow *flow,
1418 const struct nlattr *odp_actions, size_t actions_len,
1419 struct ofpbuf *packet)
1421 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1422 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1423 /* As an optimization, avoid a round-trip from userspace to kernel to
1424 * userspace. This also avoids possibly filling up kernel packet
1425 * buffers along the way. */
1426 struct dpif_upcall upcall;
1428 upcall.type = DPIF_UC_ACTION;
1429 upcall.packet = packet;
1432 upcall.userdata = nl_attr_get_u64(odp_actions);
1433 upcall.sample_pool = 0;
1434 upcall.actions = NULL;
1435 upcall.actions_len = 0;
1437 send_packet_in(ofproto, &upcall, flow, false);
1443 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
1444 ofpbuf_delete(packet);
1449 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
1450 * statistics appropriately. 'packet' must have at least sizeof(struct
1451 * ofp_packet_in) bytes of headroom.
1453 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
1454 * applying flow_extract() to 'packet' would yield the same flow as
1457 * 'facet' must have accurately composed ODP actions; that is, it must not be
1458 * in need of revalidation.
1460 * Takes ownership of 'packet'. */
1462 facet_execute(struct ofproto *ofproto, struct facet *facet,
1463 struct ofpbuf *packet)
1465 struct dpif_flow_stats stats;
1467 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1469 flow_extract_stats(&facet->flow, packet, &stats);
1470 stats.used = time_msec();
1471 if (execute_odp_actions(ofproto, &facet->flow,
1472 facet->actions, facet->actions_len, packet)) {
1473 facet_update_stats(ofproto, facet, &stats);
1477 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1478 * statistics (or the statistics for one of its facets) appropriately.
1479 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
1481 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1482 * with statistics for 'packet' either way.
1484 * Takes ownership of 'packet'. */
1486 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
1487 struct ofpbuf *packet)
1489 struct action_xlate_ctx ctx;
1490 struct ofpbuf *odp_actions;
1491 struct facet *facet;
1495 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1497 flow_extract(packet, 0, in_port, &flow);
1499 /* First look for a related facet. If we find one, account it to that. */
1500 facet = facet_lookup_valid(ofproto, &flow);
1501 if (facet && facet->rule == rule) {
1502 facet_execute(ofproto, facet, packet);
1506 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
1507 * create a new facet for it and use that. */
1508 if (rule_lookup(ofproto, &flow) == rule) {
1509 facet = facet_create(ofproto, rule, &flow, packet);
1510 facet_execute(ofproto, facet, packet);
1511 facet_install(ofproto, facet, true);
1515 /* We can't account anything to a facet. If we were to try, then that
1516 * facet would have a non-matching rule, busting our invariants. */
1517 action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
1518 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1519 size = packet->size;
1520 if (execute_odp_actions(ofproto, &flow, odp_actions->data,
1521 odp_actions->size, packet)) {
1522 rule->used = time_msec();
1523 rule->packet_count++;
1524 rule->byte_count += size;
1525 flow_push_stats(ofproto, rule, &flow, 1, size, rule->used);
1527 ofpbuf_delete(odp_actions);
1530 /* Inserts 'rule' into 'p''s flow table. */
1532 rule_insert(struct ofproto *p, struct rule *rule)
1534 struct rule *displaced_rule;
1536 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1537 if (displaced_rule) {
1538 rule_destroy(p, displaced_rule);
1540 p->need_revalidate = true;
1543 /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a
1544 * 'flow' and an example 'packet' within that flow.
1546 * The caller must already have determined that no facet with an identical
1547 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1548 * 'ofproto''s classifier table. */
1549 static struct facet *
1550 facet_create(struct ofproto *ofproto, struct rule *rule,
1551 const struct flow *flow, const struct ofpbuf *packet)
1553 struct facet *facet;
1555 facet = xzalloc(sizeof *facet);
1556 facet->used = time_msec();
1557 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1558 list_push_back(&rule->facets, &facet->list_node);
1560 facet->flow = *flow;
1561 netflow_flow_init(&facet->nf_flow);
1562 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1564 facet_make_actions(ofproto, facet, packet);
1570 facet_free(struct facet *facet)
1572 free(facet->actions);
1576 /* Remove 'rule' from 'ofproto' and free up the associated memory:
1578 * - Removes 'rule' from the classifier.
1580 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
1581 * destroys them), via rule_destroy().
1584 rule_remove(struct ofproto *ofproto, struct rule *rule)
1586 COVERAGE_INC(ofproto_del_rule);
1587 ofproto->need_revalidate = true;
1588 classifier_remove(&ofproto->cls, &rule->cr);
1589 rule_destroy(ofproto, rule);
1592 /* Remove 'facet' from 'ofproto' and free up the associated memory:
1594 * - If 'facet' was installed in the datapath, uninstalls it and updates its
1595 * rule's statistics, via facet_uninstall().
1597 * - Removes 'facet' from its rule and from ofproto->facets.
1600 facet_remove(struct ofproto *ofproto, struct facet *facet)
1602 facet_uninstall(ofproto, facet);
1603 facet_flush_stats(ofproto, facet);
1604 hmap_remove(&ofproto->facets, &facet->hmap_node);
1605 list_remove(&facet->list_node);
1609 /* Composes the ODP actions for 'facet' based on its rule's actions. */
1611 facet_make_actions(struct ofproto *p, struct facet *facet,
1612 const struct ofpbuf *packet)
1614 const struct rule *rule = facet->rule;
1615 struct ofpbuf *odp_actions;
1616 struct action_xlate_ctx ctx;
1618 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
1619 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1620 facet->tags = ctx.tags;
1621 facet->may_install = ctx.may_set_up_flow;
1622 facet->nf_flow.output_iface = ctx.nf_output_iface;
1624 if (facet->actions_len != odp_actions->size
1625 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
1626 free(facet->actions);
1627 facet->actions_len = odp_actions->size;
1628 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1631 ofpbuf_delete(odp_actions);
1634 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
1635 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
1636 * in the datapath will be zeroed and 'stats' will be updated with traffic new
1637 * since 'facet' was last updated.
1639 * Returns 0 if successful, otherwise a positive errno value.*/
1641 facet_put__(struct ofproto *ofproto, struct facet *facet,
1642 const struct nlattr *actions, size_t actions_len,
1643 struct dpif_flow_stats *stats)
1645 struct odputil_keybuf keybuf;
1646 enum dpif_flow_put_flags flags;
1650 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1652 flags |= DPIF_FP_ZERO_STATS;
1655 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1656 odp_flow_key_from_flow(&key, &facet->flow);
1658 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
1659 actions, actions_len, stats);
1662 facet_reset_dp_stats(facet, stats);
1668 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
1669 * 'zero_stats' is true, clears any existing statistics from the datapath for
1672 facet_install(struct ofproto *p, struct facet *facet, bool zero_stats)
1674 struct dpif_flow_stats stats;
1676 if (facet->may_install
1677 && !facet_put__(p, facet, facet->actions, facet->actions_len,
1678 zero_stats ? &stats : NULL)) {
1679 facet->installed = true;
1683 /* Ensures that the bytes in 'facet', plus 'extra_bytes', have been passed up
1684 * to the accounting hook function in the ofhooks structure. */
1686 facet_account(struct ofproto *ofproto,
1687 struct facet *facet, uint64_t extra_bytes)
1689 uint64_t total_bytes = facet->byte_count + extra_bytes;
1691 if (ofproto->ofhooks->account_flow_cb
1692 && total_bytes > facet->accounted_bytes)
1694 ofproto->ofhooks->account_flow_cb(
1695 &facet->flow, facet->tags, facet->actions, facet->actions_len,
1696 total_bytes - facet->accounted_bytes, ofproto->aux);
1697 facet->accounted_bytes = total_bytes;
1701 /* If 'rule' is installed in the datapath, uninstalls it. */
1703 facet_uninstall(struct ofproto *p, struct facet *facet)
1705 if (facet->installed) {
1706 struct odputil_keybuf keybuf;
1707 struct dpif_flow_stats stats;
1711 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1712 odp_flow_key_from_flow(&key, &facet->flow);
1714 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
1715 facet_reset_dp_stats(facet, &stats);
1717 facet_update_stats(p, facet, &stats);
1720 facet->installed = false;
1722 assert(facet->dp_packet_count == 0);
1723 assert(facet->dp_byte_count == 0);
1727 /* Returns true if the only action for 'facet' is to send to the controller.
1728 * (We don't report NetFlow expiration messages for such facets because they
1729 * are just part of the control logic for the network, not real traffic). */
1731 facet_is_controller_flow(struct facet *facet)
1734 && facet->rule->n_actions == 1
1735 && action_outputs_to_port(&facet->rule->actions[0],
1736 htons(OFPP_CONTROLLER)));
1739 /* Resets 'facet''s datapath statistics counters. This should be called when
1740 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
1741 * it should contain the statistics returned by dpif when 'facet' was reset in
1742 * the datapath. 'stats' will be modified to only included statistics new
1743 * since 'facet' was last updated. */
1745 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
1747 if (stats && facet->dp_packet_count <= stats->n_packets
1748 && facet->dp_byte_count <= stats->n_bytes) {
1749 stats->n_packets -= facet->dp_packet_count;
1750 stats->n_bytes -= facet->dp_byte_count;
1753 facet->dp_packet_count = 0;
1754 facet->dp_byte_count = 0;
1757 /* Folds all of 'facet''s statistics into its rule. Also updates the
1758 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
1759 * 'facet''s statistics in the datapath should have been zeroed and folded into
1760 * its packet and byte counts before this function is called. */
1762 facet_flush_stats(struct ofproto *ofproto, struct facet *facet)
1764 assert(!facet->dp_byte_count);
1765 assert(!facet->dp_packet_count);
1767 facet_push_stats(ofproto, facet);
1768 facet_account(ofproto, facet, 0);
1770 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
1771 struct ofexpired expired;
1772 expired.flow = facet->flow;
1773 expired.packet_count = facet->packet_count;
1774 expired.byte_count = facet->byte_count;
1775 expired.used = facet->used;
1776 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1779 facet->rule->packet_count += facet->packet_count;
1780 facet->rule->byte_count += facet->byte_count;
1782 /* Reset counters to prevent double counting if 'facet' ever gets
1784 facet->packet_count = 0;
1785 facet->byte_count = 0;
1786 facet->rs_packet_count = 0;
1787 facet->rs_byte_count = 0;
1788 facet->accounted_bytes = 0;
1790 netflow_flow_clear(&facet->nf_flow);
1793 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1794 * Returns it if found, otherwise a null pointer.
1796 * The returned facet might need revalidation; use facet_lookup_valid()
1797 * instead if that is important. */
1798 static struct facet *
1799 facet_find(struct ofproto *ofproto, const struct flow *flow)
1801 struct facet *facet;
1803 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
1805 if (flow_equal(flow, &facet->flow)) {
1813 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1814 * Returns it if found, otherwise a null pointer.
1816 * The returned facet is guaranteed to be valid. */
1817 static struct facet *
1818 facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
1820 struct facet *facet = facet_find(ofproto, flow);
1822 /* The facet we found might not be valid, since we could be in need of
1823 * revalidation. If it is not valid, don't return it. */
1825 && ofproto->need_revalidate
1826 && !facet_revalidate(ofproto, facet)) {
1827 COVERAGE_INC(ofproto_invalidated);
1834 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
1836 * - If the rule found is different from 'facet''s current rule, moves
1837 * 'facet' to the new rule and recompiles its actions.
1839 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
1840 * where it is and recompiles its actions anyway.
1842 * - If there is none, destroys 'facet'.
1844 * Returns true if 'facet' still exists, false if it has been destroyed. */
1846 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
1848 struct action_xlate_ctx ctx;
1849 struct ofpbuf *odp_actions;
1850 struct rule *new_rule;
1851 bool actions_changed;
1853 COVERAGE_INC(facet_revalidate);
1855 /* Determine the new rule. */
1856 new_rule = rule_lookup(ofproto, &facet->flow);
1858 /* No new rule, so delete the facet. */
1859 facet_remove(ofproto, facet);
1863 /* Calculate new ODP actions.
1865 * We do not modify any 'facet' state yet, because we might need to, e.g.,
1866 * emit a NetFlow expiration and, if so, we need to have the old state
1867 * around to properly compose it. */
1868 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
1869 odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
1870 actions_changed = (facet->actions_len != odp_actions->size
1871 || memcmp(facet->actions, odp_actions->data,
1872 facet->actions_len));
1874 /* If the ODP actions changed or the installability changed, then we need
1875 * to talk to the datapath. */
1876 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
1877 if (ctx.may_set_up_flow) {
1878 struct dpif_flow_stats stats;
1880 facet_put__(ofproto, facet,
1881 odp_actions->data, odp_actions->size, &stats);
1882 facet_update_stats(ofproto, facet, &stats);
1884 facet_uninstall(ofproto, facet);
1887 /* The datapath flow is gone or has zeroed stats, so push stats out of
1888 * 'facet' into 'rule'. */
1889 facet_flush_stats(ofproto, facet);
1892 /* Update 'facet' now that we've taken care of all the old state. */
1893 facet->tags = ctx.tags;
1894 facet->nf_flow.output_iface = ctx.nf_output_iface;
1895 facet->may_install = ctx.may_set_up_flow;
1896 if (actions_changed) {
1897 free(facet->actions);
1898 facet->actions_len = odp_actions->size;
1899 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1901 if (facet->rule != new_rule) {
1902 COVERAGE_INC(facet_changed_rule);
1903 list_remove(&facet->list_node);
1904 list_push_back(&new_rule->facets, &facet->list_node);
1905 facet->rule = new_rule;
1906 facet->used = new_rule->created;
1907 facet->rs_used = facet->used;
1910 ofpbuf_delete(odp_actions);
1916 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1919 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
1921 COVERAGE_INC(ofproto_error);
1922 ofconn_send_reply(ofconn, buf);
1927 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1929 ofconn_send_reply(ofconn, make_echo_reply(oh));
1934 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1936 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1937 struct ofp_switch_features *osf;
1939 struct ofport *port;
1941 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1942 osf->datapath_id = htonll(ofproto->datapath_id);
1943 osf->n_buffers = htonl(pktbuf_capacity());
1945 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1946 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
1947 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1948 (1u << OFPAT_SET_VLAN_VID) |
1949 (1u << OFPAT_SET_VLAN_PCP) |
1950 (1u << OFPAT_STRIP_VLAN) |
1951 (1u << OFPAT_SET_DL_SRC) |
1952 (1u << OFPAT_SET_DL_DST) |
1953 (1u << OFPAT_SET_NW_SRC) |
1954 (1u << OFPAT_SET_NW_DST) |
1955 (1u << OFPAT_SET_NW_TOS) |
1956 (1u << OFPAT_SET_TP_SRC) |
1957 (1u << OFPAT_SET_TP_DST) |
1958 (1u << OFPAT_ENQUEUE));
1960 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1961 ofpbuf_put(buf, &port->opp, sizeof port->opp);
1964 ofconn_send_reply(ofconn, buf);
1969 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1971 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1973 struct ofp_switch_config *osc;
1977 /* Figure out flags. */
1978 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
1979 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1982 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1983 osc->flags = htons(flags);
1984 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1985 ofconn_send_reply(ofconn, buf);
1991 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1993 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1994 uint16_t flags = ntohs(osc->flags);
1996 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1997 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1998 switch (flags & OFPC_FRAG_MASK) {
1999 case OFPC_FRAG_NORMAL:
2000 dpif_set_drop_frags(ofproto->dpif, false);
2002 case OFPC_FRAG_DROP:
2003 dpif_set_drop_frags(ofproto->dpif, true);
2006 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
2012 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2017 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2018 struct action_xlate_ctx *ctx);
2021 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
2023 const struct ofport *ofport = get_port(ctx->ofproto, port);
2026 if (ofport->opp.config & htonl(OFPPC_NO_FWD)) {
2027 /* Forwarding disabled on port. */
2032 * We don't have an ofport record for this port, but it doesn't hurt to
2033 * allow forwarding to it anyhow. Maybe such a port will appear later
2034 * and we're pre-populating the flow table.
2038 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port);
2039 ctx->nf_output_iface = port;
2042 static struct rule *
2043 rule_lookup(struct ofproto *ofproto, const struct flow *flow)
2045 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
2049 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2051 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2052 uint16_t old_in_port;
2055 /* Look up a flow with 'in_port' as the input port. Then restore the
2056 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2057 * have surprising behavior). */
2058 old_in_port = ctx->flow.in_port;
2059 ctx->flow.in_port = in_port;
2060 rule = rule_lookup(ctx->ofproto, &ctx->flow);
2061 ctx->flow.in_port = old_in_port;
2063 if (ctx->resubmit_hook) {
2064 ctx->resubmit_hook(ctx, rule);
2069 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2073 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2075 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2076 MAX_RESUBMIT_RECURSION);
2081 flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, ovs_be32 mask,
2082 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2084 struct ofport *ofport;
2086 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
2087 uint16_t odp_port = ofport->odp_port;
2088 if (odp_port != odp_in_port && !(ofport->opp.config & mask)) {
2089 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2092 *nf_output_iface = NF_OUT_FLOOD;
2096 xlate_output_action__(struct action_xlate_ctx *ctx,
2097 uint16_t port, uint16_t max_len)
2100 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2102 ctx->nf_output_iface = NF_OUT_DROP;
2106 add_output_action(ctx, ctx->flow.in_port);
2109 xlate_table_action(ctx, ctx->flow.in_port);
2112 if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
2113 ctx->odp_actions, &ctx->tags,
2114 &ctx->nf_output_iface,
2115 ctx->ofproto->aux)) {
2116 COVERAGE_INC(ofproto_uninstallable);
2117 ctx->may_set_up_flow = false;
2121 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(OFPPC_NO_FLOOD),
2122 &ctx->nf_output_iface, ctx->odp_actions);
2125 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(0),
2126 &ctx->nf_output_iface, ctx->odp_actions);
2128 case OFPP_CONTROLLER:
2129 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2132 add_output_action(ctx, ODPP_LOCAL);
2135 odp_port = ofp_port_to_odp_port(port);
2136 if (odp_port != ctx->flow.in_port) {
2137 add_output_action(ctx, odp_port);
2142 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2143 ctx->nf_output_iface = NF_OUT_FLOOD;
2144 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2145 ctx->nf_output_iface = prev_nf_output_iface;
2146 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2147 ctx->nf_output_iface != NF_OUT_FLOOD) {
2148 ctx->nf_output_iface = NF_OUT_MULTI;
2153 xlate_output_action(struct action_xlate_ctx *ctx,
2154 const struct ofp_action_output *oao)
2156 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2159 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2160 * optimization, because we're going to add another action that sets the
2161 * priority immediately after, or because there are no actions following the
2164 remove_pop_action(struct action_xlate_ctx *ctx)
2166 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2167 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2168 ctx->last_pop_priority = -1;
2173 add_pop_action(struct action_xlate_ctx *ctx)
2175 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2176 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2177 ctx->last_pop_priority = ctx->odp_actions->size;
2182 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2183 const struct ofp_action_enqueue *oae)
2185 uint16_t ofp_port, odp_port;
2189 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2192 /* Fall back to ordinary output action. */
2193 xlate_output_action__(ctx, ntohs(oae->port), 0);
2197 /* Figure out ODP output port. */
2198 ofp_port = ntohs(oae->port);
2199 if (ofp_port != OFPP_IN_PORT) {
2200 odp_port = ofp_port_to_odp_port(ofp_port);
2202 odp_port = ctx->flow.in_port;
2205 /* Add ODP actions. */
2206 remove_pop_action(ctx);
2207 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2208 add_output_action(ctx, odp_port);
2209 add_pop_action(ctx);
2211 /* Update NetFlow output port. */
2212 if (ctx->nf_output_iface == NF_OUT_DROP) {
2213 ctx->nf_output_iface = odp_port;
2214 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2215 ctx->nf_output_iface = NF_OUT_MULTI;
2220 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2221 const struct nx_action_set_queue *nasq)
2226 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2229 /* Couldn't translate queue to a priority, so ignore. A warning
2230 * has already been logged. */
2234 remove_pop_action(ctx);
2235 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2239 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2241 ovs_be16 tci = ctx->flow.vlan_tci;
2242 if (!(tci & htons(VLAN_CFI))) {
2243 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2245 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2246 tci & ~htons(VLAN_CFI));
2250 struct xlate_reg_state {
2256 save_reg_state(const struct action_xlate_ctx *ctx,
2257 struct xlate_reg_state *state)
2259 state->vlan_tci = ctx->flow.vlan_tci;
2260 state->tun_id = ctx->flow.tun_id;
2264 update_reg_state(struct action_xlate_ctx *ctx,
2265 const struct xlate_reg_state *state)
2267 if (ctx->flow.vlan_tci != state->vlan_tci) {
2268 xlate_set_dl_tci(ctx);
2270 if (ctx->flow.tun_id != state->tun_id) {
2271 nl_msg_put_be64(ctx->odp_actions,
2272 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2277 xlate_nicira_action(struct action_xlate_ctx *ctx,
2278 const struct nx_action_header *nah)
2280 const struct nx_action_resubmit *nar;
2281 const struct nx_action_set_tunnel *nast;
2282 const struct nx_action_set_queue *nasq;
2283 const struct nx_action_multipath *nam;
2284 const struct nx_action_autopath *naa;
2285 enum nx_action_subtype subtype = ntohs(nah->subtype);
2286 const struct ofhooks *ofhooks = ctx->ofproto->ofhooks;
2287 struct xlate_reg_state state;
2288 uint16_t autopath_port;
2291 assert(nah->vendor == htonl(NX_VENDOR_ID));
2293 case NXAST_RESUBMIT:
2294 nar = (const struct nx_action_resubmit *) nah;
2295 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2298 case NXAST_SET_TUNNEL:
2299 nast = (const struct nx_action_set_tunnel *) nah;
2300 tun_id = htonll(ntohl(nast->tun_id));
2301 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2302 ctx->flow.tun_id = tun_id;
2305 case NXAST_DROP_SPOOFED_ARP:
2306 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2307 nl_msg_put_flag(ctx->odp_actions,
2308 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2312 case NXAST_SET_QUEUE:
2313 nasq = (const struct nx_action_set_queue *) nah;
2314 xlate_set_queue_action(ctx, nasq);
2317 case NXAST_POP_QUEUE:
2318 add_pop_action(ctx);
2321 case NXAST_REG_MOVE:
2322 save_reg_state(ctx, &state);
2323 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2325 update_reg_state(ctx, &state);
2328 case NXAST_REG_LOAD:
2329 save_reg_state(ctx, &state);
2330 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2332 update_reg_state(ctx, &state);
2336 /* Nothing to do. */
2339 case NXAST_SET_TUNNEL64:
2340 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2341 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2342 ctx->flow.tun_id = tun_id;
2345 case NXAST_MULTIPATH:
2346 nam = (const struct nx_action_multipath *) nah;
2347 multipath_execute(nam, &ctx->flow);
2350 case NXAST_AUTOPATH:
2351 naa = (const struct nx_action_autopath *) nah;
2352 autopath_port = (ofhooks->autopath_cb
2353 ? ofhooks->autopath_cb(&ctx->flow, ntohl(naa->id),
2354 &ctx->tags, ctx->ofproto->aux)
2356 autopath_execute(naa, &ctx->flow, autopath_port);
2359 /* If you add a new action here that modifies flow data, don't forget to
2360 * update the flow key in ctx->flow at the same time. */
2362 case NXAST_SNAT__OBSOLETE:
2364 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2370 do_xlate_actions(const union ofp_action *in, size_t n_in,
2371 struct action_xlate_ctx *ctx)
2373 struct actions_iterator iter;
2374 const union ofp_action *ia;
2375 const struct ofport *port;
2377 port = get_port(ctx->ofproto, ctx->flow.in_port);
2378 if (port && port->opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2379 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2380 ? htonl(OFPPC_NO_RECV_STP)
2381 : htonl(OFPPC_NO_RECV))) {
2382 /* Drop this flow. */
2386 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2387 enum ofp_action_type type = ntohs(ia->type);
2388 const struct ofp_action_dl_addr *oada;
2392 xlate_output_action(ctx, &ia->output);
2395 case OFPAT_SET_VLAN_VID:
2396 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2397 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2398 xlate_set_dl_tci(ctx);
2401 case OFPAT_SET_VLAN_PCP:
2402 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2403 ctx->flow.vlan_tci |= htons(
2404 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2405 xlate_set_dl_tci(ctx);
2408 case OFPAT_STRIP_VLAN:
2409 ctx->flow.vlan_tci = htons(0);
2410 xlate_set_dl_tci(ctx);
2413 case OFPAT_SET_DL_SRC:
2414 oada = ((struct ofp_action_dl_addr *) ia);
2415 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2416 oada->dl_addr, ETH_ADDR_LEN);
2417 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
2420 case OFPAT_SET_DL_DST:
2421 oada = ((struct ofp_action_dl_addr *) ia);
2422 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2423 oada->dl_addr, ETH_ADDR_LEN);
2424 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
2427 case OFPAT_SET_NW_SRC:
2428 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
2429 ia->nw_addr.nw_addr);
2430 ctx->flow.nw_src = ia->nw_addr.nw_addr;
2433 case OFPAT_SET_NW_DST:
2434 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
2435 ia->nw_addr.nw_addr);
2436 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
2439 case OFPAT_SET_NW_TOS:
2440 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
2442 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
2445 case OFPAT_SET_TP_SRC:
2446 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
2447 ia->tp_port.tp_port);
2448 ctx->flow.tp_src = ia->tp_port.tp_port;
2451 case OFPAT_SET_TP_DST:
2452 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
2453 ia->tp_port.tp_port);
2454 ctx->flow.tp_dst = ia->tp_port.tp_port;
2458 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2462 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
2466 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
2473 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
2474 struct ofproto *ofproto, const struct flow *flow,
2475 const struct ofpbuf *packet)
2477 ctx->ofproto = ofproto;
2479 ctx->packet = packet;
2480 ctx->resubmit_hook = NULL;
2481 ctx->check_special = true;
2485 ofproto_process_cfm(struct ofproto *ofproto, const struct flow *flow,
2486 const struct ofpbuf *packet)
2488 struct ofport *ofport;
2490 ofport = get_port(ofproto, flow->in_port);
2491 if (ofport && ofport->cfm) {
2492 cfm_process_heartbeat(ofport->cfm, packet);
2496 static struct ofpbuf *
2497 xlate_actions(struct action_xlate_ctx *ctx,
2498 const union ofp_action *in, size_t n_in)
2500 COVERAGE_INC(ofproto_ofp2odp);
2502 ctx->odp_actions = ofpbuf_new(512);
2504 ctx->may_set_up_flow = true;
2505 ctx->nf_output_iface = NF_OUT_DROP;
2507 ctx->last_pop_priority = -1;
2509 if (ctx->check_special && cfm_should_process_flow(&ctx->flow)) {
2511 ofproto_process_cfm(ctx->ofproto, &ctx->flow, ctx->packet);
2513 ctx->may_set_up_flow = false;
2514 } else if (ctx->check_special
2515 && ctx->ofproto->ofhooks->special_cb
2516 && !ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet,
2517 ctx->ofproto->aux)) {
2518 ctx->may_set_up_flow = false;
2520 do_xlate_actions(in, n_in, ctx);
2523 remove_pop_action(ctx);
2525 /* Check with in-band control to see if we're allowed to set up this
2527 if (!connmgr_may_set_up_flow(ctx->ofproto->connmgr, &ctx->flow,
2528 ctx->odp_actions->data,
2529 ctx->odp_actions->size)) {
2530 ctx->may_set_up_flow = false;
2533 return ctx->odp_actions;
2536 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2537 * error message code (composed with ofp_mkerr()) for the caller to propagate
2538 * upward. Otherwise, returns 0.
2540 * The log message mentions 'msg_type'. */
2542 reject_slave_controller(struct ofconn *ofconn, const char *msg_type)
2544 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2545 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
2546 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
2547 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
2550 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2557 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2559 struct ofproto *p = ofconn_get_ofproto(ofconn);
2560 struct ofp_packet_out *opo;
2561 struct ofpbuf payload, *buffer;
2562 union ofp_action *ofp_actions;
2563 struct action_xlate_ctx ctx;
2564 struct ofpbuf *odp_actions;
2565 struct ofpbuf request;
2567 size_t n_ofp_actions;
2571 COVERAGE_INC(ofproto_packet_out);
2573 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
2578 /* Get ofp_packet_out. */
2579 ofpbuf_use_const(&request, oh, ntohs(oh->length));
2580 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
2583 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
2584 &ofp_actions, &n_ofp_actions);
2590 if (opo->buffer_id != htonl(UINT32_MAX)) {
2591 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
2593 if (error || !buffer) {
2602 /* Extract flow, check actions. */
2603 flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
2605 error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
2611 action_xlate_ctx_init(&ctx, p, &flow, &payload);
2612 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
2613 dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
2614 ofpbuf_delete(odp_actions);
2617 ofpbuf_delete(buffer);
2622 update_port_config(struct ofproto *p, struct ofport *port,
2623 ovs_be32 config, ovs_be32 mask)
2625 mask &= config ^ port->opp.config;
2626 if (mask & htonl(OFPPC_PORT_DOWN)) {
2627 if (config & htonl(OFPPC_PORT_DOWN)) {
2628 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2630 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2633 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
2634 OFPPC_NO_FWD | OFPPC_NO_FLOOD)
2635 if (mask & htonl(REVALIDATE_BITS)) {
2636 COVERAGE_INC(ofproto_costly_flags);
2637 port->opp.config ^= mask & htonl(REVALIDATE_BITS);
2638 p->need_revalidate = true;
2640 #undef REVALIDATE_BITS
2641 if (mask & htonl(OFPPC_NO_PACKET_IN)) {
2642 port->opp.config ^= htonl(OFPPC_NO_PACKET_IN);
2647 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2649 struct ofproto *p = ofconn_get_ofproto(ofconn);
2650 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
2651 struct ofport *port;
2654 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
2659 port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
2661 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2662 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2663 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2665 update_port_config(p, port, opm->config, opm->mask);
2666 if (opm->advertise) {
2667 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2673 static struct ofpbuf *
2674 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
2676 struct ofp_stats_reply *osr;
2679 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2680 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2682 osr->flags = htons(0);
2686 static struct ofpbuf *
2687 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
2689 const struct ofp_stats_request *osr
2690 = (const struct ofp_stats_request *) request;
2691 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
2695 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
2696 struct ofpbuf **msgp)
2698 struct ofpbuf *msg = *msgp;
2699 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2700 if (nbytes + msg->size > UINT16_MAX) {
2701 struct ofp_stats_reply *reply = msg->data;
2702 reply->flags = htons(OFPSF_REPLY_MORE);
2703 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
2704 ofconn_send_reply(ofconn, msg);
2706 return ofpbuf_put_uninit(*msgp, nbytes);
2709 static struct ofpbuf *
2710 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
2712 struct nicira_stats_msg *nsm;
2715 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
2716 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
2717 nsm->type = htons(OFPST_VENDOR);
2718 nsm->flags = htons(0);
2719 nsm->vendor = htonl(NX_VENDOR_ID);
2720 nsm->subtype = subtype;
2724 static struct ofpbuf *
2725 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
2727 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
2731 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
2732 struct ofpbuf **msgp)
2734 struct ofpbuf *msg = *msgp;
2735 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
2736 if (nbytes + msg->size > UINT16_MAX) {
2737 struct nicira_stats_msg *reply = msg->data;
2738 reply->flags = htons(OFPSF_REPLY_MORE);
2739 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
2740 ofconn_send_reply(ofconn, msg);
2742 ofpbuf_prealloc_tailroom(*msgp, nbytes);
2746 handle_desc_stats_request(struct ofconn *ofconn,
2747 const struct ofp_header *request)
2749 struct ofproto *p = ofconn_get_ofproto(ofconn);
2750 struct ofp_desc_stats *ods;
2753 msg = start_ofp_stats_reply(request, sizeof *ods);
2754 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
2755 memset(ods, 0, sizeof *ods);
2756 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
2757 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
2758 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
2759 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
2760 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2761 ofconn_send_reply(ofconn, msg);
2767 handle_table_stats_request(struct ofconn *ofconn,
2768 const struct ofp_header *request)
2770 struct ofproto *p = ofconn_get_ofproto(ofconn);
2771 struct ofp_table_stats *ots;
2774 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
2776 /* Classifier table. */
2777 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
2778 memset(ots, 0, sizeof *ots);
2779 strcpy(ots->name, "classifier");
2780 ots->wildcards = htonl(OFPFW_ALL);
2781 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
2782 ots->active_count = htonl(classifier_count(&p->cls));
2783 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
2784 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
2786 ofconn_send_reply(ofconn, msg);
2791 append_port_stat(struct ofport *port, struct ofconn *ofconn,
2792 struct ofpbuf **msgp)
2794 struct netdev_stats stats;
2795 struct ofp_port_stats *ops;
2797 /* Intentionally ignore return value, since errors will set
2798 * 'stats' to all-1s, which is correct for OpenFlow, and
2799 * netdev_get_stats() will log errors. */
2800 netdev_get_stats(port->netdev, &stats);
2802 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
2803 ops->port_no = port->opp.port_no;
2804 memset(ops->pad, 0, sizeof ops->pad);
2805 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
2806 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
2807 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
2808 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
2809 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
2810 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
2811 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
2812 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
2813 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
2814 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
2815 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
2816 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
2820 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2822 struct ofproto *p = ofconn_get_ofproto(ofconn);
2823 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
2824 struct ofp_port_stats *ops;
2826 struct ofport *port;
2828 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
2829 if (psr->port_no != htons(OFPP_NONE)) {
2830 port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
2832 append_port_stat(port, ofconn, &msg);
2835 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2836 append_port_stat(port, ofconn, &msg);
2840 ofconn_send_reply(ofconn, msg);
2845 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
2847 long long int msecs = time_msec() - start;
2848 *sec = msecs / 1000;
2849 *nsec = (msecs % 1000) * (1000 * 1000);
2853 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
2857 calc_flow_duration__(start, &sec, &nsec);
2858 *sec_be = htonl(sec);
2859 *nsec_be = htonl(nsec);
2863 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
2864 ovs_be16 out_port, struct ofpbuf **replyp)
2866 struct ofp_flow_stats *ofs;
2867 uint64_t packet_count, byte_count;
2868 size_t act_len, len;
2870 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2874 act_len = sizeof *rule->actions * rule->n_actions;
2875 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2877 rule_get_stats(rule, &packet_count, &byte_count);
2879 ofs = append_ofp_stats_reply(len, ofconn, replyp);
2880 ofs->length = htons(len);
2883 ofputil_cls_rule_to_match(&rule->cr, &ofs->match);
2884 put_32aligned_be64(&ofs->cookie, rule->flow_cookie);
2885 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
2886 ofs->priority = htons(rule->cr.priority);
2887 ofs->idle_timeout = htons(rule->idle_timeout);
2888 ofs->hard_timeout = htons(rule->hard_timeout);
2889 memset(ofs->pad2, 0, sizeof ofs->pad2);
2890 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
2891 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
2892 if (rule->n_actions > 0) {
2893 memcpy(ofs->actions, rule->actions, act_len);
2898 is_valid_table(uint8_t table_id)
2900 if (table_id == 0 || table_id == 0xff) {
2903 /* It would probably be better to reply with an error but there doesn't
2904 * seem to be any appropriate value, so that might just be
2906 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
2913 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2915 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
2916 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2917 struct ofpbuf *reply;
2919 COVERAGE_INC(ofproto_flows_req);
2920 reply = start_ofp_stats_reply(oh, 1024);
2921 if (is_valid_table(fsr->table_id)) {
2922 struct cls_cursor cursor;
2923 struct cls_rule target;
2926 ofputil_cls_rule_from_match(&fsr->match, 0, &target);
2927 cls_cursor_init(&cursor, &ofproto->cls, &target);
2928 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2929 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
2932 ofconn_send_reply(ofconn, reply);
2938 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
2939 ovs_be16 out_port, struct ofpbuf **replyp)
2941 struct nx_flow_stats *nfs;
2942 uint64_t packet_count, byte_count;
2943 size_t act_len, start_len;
2944 struct ofpbuf *reply;
2946 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2950 rule_get_stats(rule, &packet_count, &byte_count);
2952 act_len = sizeof *rule->actions * rule->n_actions;
2954 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
2955 start_len = (*replyp)->size;
2958 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
2961 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
2962 nfs->cookie = rule->flow_cookie;
2963 nfs->priority = htons(rule->cr.priority);
2964 nfs->idle_timeout = htons(rule->idle_timeout);
2965 nfs->hard_timeout = htons(rule->hard_timeout);
2966 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
2967 memset(nfs->pad2, 0, sizeof nfs->pad2);
2968 nfs->packet_count = htonll(packet_count);
2969 nfs->byte_count = htonll(byte_count);
2970 if (rule->n_actions > 0) {
2971 ofpbuf_put(reply, rule->actions, act_len);
2973 nfs->length = htons(reply->size - start_len);
2977 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
2979 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2980 struct nx_flow_stats_request *nfsr;
2981 struct cls_rule target;
2982 struct ofpbuf *reply;
2986 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2988 /* Dissect the message. */
2989 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2990 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
2995 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2998 COVERAGE_INC(ofproto_flows_req);
2999 reply = start_nxstats_reply(&nfsr->nsm, 1024);
3000 if (is_valid_table(nfsr->table_id)) {
3001 struct cls_cursor cursor;
3004 cls_cursor_init(&cursor, &ofproto->cls, &target);
3005 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3006 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
3009 ofconn_send_reply(ofconn, reply);
3015 flow_stats_ds(struct rule *rule, struct ds *results)
3017 uint64_t packet_count, byte_count;
3018 size_t act_len = sizeof *rule->actions * rule->n_actions;
3020 rule_get_stats(rule, &packet_count, &byte_count);
3022 ds_put_format(results, "duration=%llds, ",
3023 (time_msec() - rule->created) / 1000);
3024 ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0);
3025 ds_put_format(results, "priority=%u, ", rule->cr.priority);
3026 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3027 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3028 cls_rule_format(&rule->cr, results);
3029 ds_put_char(results, ',');
3031 ofp_print_actions(results, &rule->actions->header, act_len);
3033 ds_put_cstr(results, "drop");
3035 ds_put_cstr(results, "\n");
3038 /* Adds a pretty-printed description of all flows to 'results', including
3039 * hidden flows (e.g., set up by in-band control). */
3041 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3043 struct cls_cursor cursor;
3046 cls_cursor_init(&cursor, &p->cls, NULL);
3047 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3048 flow_stats_ds(rule, results);
3053 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
3054 ovs_be16 out_port, uint8_t table_id,
3055 struct ofp_aggregate_stats_reply *oasr)
3057 uint64_t total_packets = 0;
3058 uint64_t total_bytes = 0;
3061 COVERAGE_INC(ofproto_agg_request);
3063 if (is_valid_table(table_id)) {
3064 struct cls_cursor cursor;
3067 cls_cursor_init(&cursor, &ofproto->cls, target);
3068 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3069 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
3070 uint64_t packet_count;
3071 uint64_t byte_count;
3073 rule_get_stats(rule, &packet_count, &byte_count);
3075 total_packets += packet_count;
3076 total_bytes += byte_count;
3082 oasr->flow_count = htonl(n_flows);
3083 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
3084 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
3085 memset(oasr->pad, 0, sizeof oasr->pad);
3089 handle_aggregate_stats_request(struct ofconn *ofconn,
3090 const struct ofp_header *oh)
3092 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
3093 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3094 struct ofp_aggregate_stats_reply *reply;
3095 struct cls_rule target;
3098 ofputil_cls_rule_from_match(&request->match, 0, &target);
3100 msg = start_ofp_stats_reply(oh, sizeof *reply);
3101 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
3102 query_aggregate_stats(ofproto, &target, request->out_port,
3103 request->table_id, reply);
3104 ofconn_send_reply(ofconn, msg);
3109 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
3111 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3112 struct nx_aggregate_stats_request *request;
3113 struct ofp_aggregate_stats_reply *reply;
3114 struct cls_rule target;
3119 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3121 /* Dissect the message. */
3122 request = ofpbuf_pull(&b, sizeof *request);
3123 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
3128 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3132 COVERAGE_INC(ofproto_flows_req);
3133 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
3134 reply = ofpbuf_put_uninit(buf, sizeof *reply);
3135 query_aggregate_stats(ofproto, &target, request->out_port,
3136 request->table_id, reply);
3137 ofconn_send_reply(ofconn, buf);
3142 struct queue_stats_cbdata {
3143 struct ofconn *ofconn;
3144 struct ofport *ofport;
3149 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3150 const struct netdev_queue_stats *stats)
3152 struct ofp_queue_stats *reply;
3154 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
3155 reply->port_no = cbdata->ofport->opp.port_no;
3156 memset(reply->pad, 0, sizeof reply->pad);
3157 reply->queue_id = htonl(queue_id);
3158 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
3159 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
3160 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
3164 handle_queue_stats_dump_cb(uint32_t queue_id,
3165 struct netdev_queue_stats *stats,
3168 struct queue_stats_cbdata *cbdata = cbdata_;
3170 put_queue_stats(cbdata, queue_id, stats);
3174 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3175 struct queue_stats_cbdata *cbdata)
3177 cbdata->ofport = port;
3178 if (queue_id == OFPQ_ALL) {
3179 netdev_dump_queue_stats(port->netdev,
3180 handle_queue_stats_dump_cb, cbdata);
3182 struct netdev_queue_stats stats;
3184 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3185 put_queue_stats(cbdata, queue_id, &stats);
3191 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
3193 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3194 const struct ofp_queue_stats_request *qsr;
3195 struct queue_stats_cbdata cbdata;
3196 struct ofport *port;
3197 unsigned int port_no;
3200 qsr = ofputil_stats_body(oh);
3202 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3205 COVERAGE_INC(ofproto_queue_req);
3207 cbdata.ofconn = ofconn;
3208 cbdata.msg = start_ofp_stats_reply(oh, 128);
3210 port_no = ntohs(qsr->port_no);
3211 queue_id = ntohl(qsr->queue_id);
3212 if (port_no == OFPP_ALL) {
3213 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3214 handle_queue_stats_for_port(port, queue_id, &cbdata);
3216 } else if (port_no < ofproto->max_ports) {
3217 port = get_port(ofproto, ofp_port_to_odp_port(port_no));
3219 handle_queue_stats_for_port(port, queue_id, &cbdata);
3222 ofpbuf_delete(cbdata.msg);
3223 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
3225 ofconn_send_reply(ofconn, cbdata.msg);
3230 /* Updates 'facet''s used time. Caller is responsible for calling
3231 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3233 facet_update_time(struct ofproto *ofproto, struct facet *facet,
3236 if (used > facet->used) {
3238 if (used > facet->rule->used) {
3239 facet->rule->used = used;
3241 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3245 /* Folds the statistics from 'stats' into the counters in 'facet'.
3247 * Because of the meaning of a facet's counters, it only makes sense to do this
3248 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3249 * packet that was sent by hand or if it represents statistics that have been
3250 * cleared out of the datapath. */
3252 facet_update_stats(struct ofproto *ofproto, struct facet *facet,
3253 const struct dpif_flow_stats *stats)
3255 if (stats->n_packets || stats->used > facet->used) {
3256 facet_update_time(ofproto, facet, stats->used);
3257 facet->packet_count += stats->n_packets;
3258 facet->byte_count += stats->n_bytes;
3259 facet_push_stats(ofproto, facet);
3260 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3265 facet_push_stats(struct ofproto *ofproto, struct facet *facet)
3267 uint64_t rs_packets, rs_bytes;
3269 assert(facet->packet_count >= facet->rs_packet_count);
3270 assert(facet->byte_count >= facet->rs_byte_count);
3271 assert(facet->used >= facet->rs_used);
3273 rs_packets = facet->packet_count - facet->rs_packet_count;
3274 rs_bytes = facet->byte_count - facet->rs_byte_count;
3276 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3277 facet->rs_packet_count = facet->packet_count;
3278 facet->rs_byte_count = facet->byte_count;
3279 facet->rs_used = facet->used;
3281 flow_push_stats(ofproto, facet->rule, &facet->flow,
3282 rs_packets, rs_bytes, facet->used);
3286 struct ofproto_push {
3287 struct action_xlate_ctx ctx;
3294 push_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
3296 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3299 rule->packet_count += push->packets;
3300 rule->byte_count += push->bytes;
3301 rule->used = MAX(push->used, rule->used);
3305 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3306 * 'rule''s actions. */
3308 flow_push_stats(struct ofproto *ofproto, const struct rule *rule,
3309 struct flow *flow, uint64_t packets, uint64_t bytes,
3312 struct ofproto_push push;
3314 push.packets = packets;
3318 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3319 push.ctx.resubmit_hook = push_resubmit;
3320 ofpbuf_delete(xlate_actions(&push.ctx, rule->actions, rule->n_actions));
3323 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3324 * in which no matching flow already exists in the flow table.
3326 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3327 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
3328 * OpenFlow error code as encoded by ofp_mkerr() on failure.
3330 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3333 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
3335 struct ofproto *p = ofconn_get_ofproto(ofconn);
3336 struct ofpbuf *packet;
3341 if (fm->flags & OFPFF_CHECK_OVERLAP
3342 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
3343 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
3347 if (fm->buffer_id != UINT32_MAX) {
3348 error = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id,
3352 in_port = UINT16_MAX;
3355 rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
3356 fm->idle_timeout, fm->hard_timeout, fm->cookie,
3357 fm->flags & OFPFF_SEND_FLOW_REM);
3358 rule_insert(p, rule);
3360 rule_execute(p, rule, in_port, packet);
3365 static struct rule *
3366 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
3368 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
3372 send_buffered_packet(struct ofconn *ofconn,
3373 struct rule *rule, uint32_t buffer_id)
3375 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3376 struct ofpbuf *packet;
3380 if (buffer_id == UINT32_MAX) {
3384 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
3389 rule_execute(ofproto, rule, in_port, packet);
3394 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3396 struct modify_flows_cbdata {
3397 struct ofproto *ofproto;
3398 const struct flow_mod *fm;
3402 static int modify_flow(struct ofproto *, const struct flow_mod *,
3405 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
3406 * encoded by ofp_mkerr() on failure.
3408 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3411 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
3413 struct ofproto *p = ofconn_get_ofproto(ofconn);
3414 struct rule *match = NULL;
3415 struct cls_cursor cursor;
3418 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3419 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3420 if (!rule_is_hidden(rule)) {
3422 modify_flow(p, fm, rule);
3427 /* This credits the packet to whichever flow happened to match last.
3428 * That's weird. Maybe we should do a lookup for the flow that
3429 * actually matches the packet? Who knows. */
3430 send_buffered_packet(ofconn, match, fm->buffer_id);
3433 return add_flow(ofconn, fm);
3437 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3438 * code as encoded by ofp_mkerr() on failure.
3440 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3443 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
3445 struct ofproto *p = ofconn_get_ofproto(ofconn);
3446 struct rule *rule = find_flow_strict(p, fm);
3447 if (rule && !rule_is_hidden(rule)) {
3448 modify_flow(p, fm, rule);
3449 return send_buffered_packet(ofconn, rule, fm->buffer_id);
3451 return add_flow(ofconn, fm);
3455 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
3456 * been identified as a flow in 'p''s flow table to be modified, by changing
3457 * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
3458 * ofp_action[] structures). */
3460 modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
3462 size_t actions_len = fm->n_actions * sizeof *rule->actions;
3464 rule->flow_cookie = fm->cookie;
3466 /* If the actions are the same, do nothing. */
3467 if (fm->n_actions == rule->n_actions
3469 || !memcmp(fm->actions, rule->actions, actions_len))) {
3473 /* Replace actions. */
3474 free(rule->actions);
3475 rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
3476 rule->n_actions = fm->n_actions;
3478 p->need_revalidate = true;
3483 /* OFPFC_DELETE implementation. */
3485 static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
3487 /* Implements OFPFC_DELETE. */
3489 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
3491 struct rule *rule, *next_rule;
3492 struct cls_cursor cursor;
3494 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3495 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3496 delete_flow(p, rule, htons(fm->out_port));
3500 /* Implements OFPFC_DELETE_STRICT. */
3502 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
3504 struct rule *rule = find_flow_strict(p, fm);
3506 delete_flow(p, rule, htons(fm->out_port));
3510 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
3511 * been identified as a flow to delete from 'p''s flow table, by deleting the
3512 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
3515 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
3516 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
3517 * specified 'out_port'. */
3519 delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
3521 if (rule_is_hidden(rule)) {
3525 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
3529 rule_send_removed(p, rule, OFPRR_DELETE);
3530 rule_remove(p, rule);
3534 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3536 struct ofproto *p = ofconn_get_ofproto(ofconn);
3540 error = reject_slave_controller(ofconn, "flow_mod");
3545 error = ofputil_decode_flow_mod(&fm, oh);
3550 /* We do not support the emergency flow cache. It will hopefully get
3551 * dropped from OpenFlow in the near future. */
3552 if (fm.flags & OFPFF_EMERG) {
3553 /* There isn't a good fit for an error code, so just state that the
3554 * flow table is full. */
3555 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
3558 error = validate_actions(fm.actions, fm.n_actions,
3559 &fm.cr.flow, p->max_ports);
3564 switch (fm.command) {
3566 return add_flow(ofconn, &fm);
3569 return modify_flows_loose(ofconn, &fm);
3571 case OFPFC_MODIFY_STRICT:
3572 return modify_flow_strict(ofconn, &fm);
3575 delete_flows_loose(p, &fm);
3578 case OFPFC_DELETE_STRICT:
3579 delete_flow_strict(p, &fm);
3583 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
3588 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3590 struct nx_role_request *nrr = (struct nx_role_request *) oh;
3591 struct nx_role_request *reply;
3595 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
3596 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
3597 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3600 role = ntohl(nrr->role);
3601 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
3602 && role != NX_ROLE_SLAVE) {
3603 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
3605 /* There's no good error code for this. */
3606 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
3609 ofconn_set_role(ofconn, role);
3611 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
3612 reply->role = htonl(role);
3613 ofconn_send_reply(ofconn, buf);
3619 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
3621 const struct nxt_set_flow_format *msg
3622 = (const struct nxt_set_flow_format *) oh;
3625 format = ntohl(msg->format);
3626 if (format == NXFF_OPENFLOW10
3627 || format == NXFF_NXM) {
3628 ofconn_set_flow_format(ofconn, format);
3631 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3636 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
3638 struct ofp_header *ob;
3641 /* Currently, everything executes synchronously, so we can just
3642 * immediately send the barrier reply. */
3643 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
3644 ofconn_send_reply(ofconn, buf);
3649 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
3651 const struct ofp_header *oh = msg->data;
3652 const struct ofputil_msg_type *type;
3655 error = ofputil_decode_msg_type(oh, &type);
3660 switch (ofputil_msg_type_code(type)) {
3661 /* OpenFlow requests. */
3662 case OFPUTIL_OFPT_ECHO_REQUEST:
3663 return handle_echo_request(ofconn, oh);
3665 case OFPUTIL_OFPT_FEATURES_REQUEST:
3666 return handle_features_request(ofconn, oh);
3668 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
3669 return handle_get_config_request(ofconn, oh);
3671 case OFPUTIL_OFPT_SET_CONFIG:
3672 return handle_set_config(ofconn, msg->data);
3674 case OFPUTIL_OFPT_PACKET_OUT:
3675 return handle_packet_out(ofconn, oh);
3677 case OFPUTIL_OFPT_PORT_MOD:
3678 return handle_port_mod(ofconn, oh);
3680 case OFPUTIL_OFPT_FLOW_MOD:
3681 return handle_flow_mod(ofconn, oh);
3683 case OFPUTIL_OFPT_BARRIER_REQUEST:
3684 return handle_barrier_request(ofconn, oh);
3686 /* OpenFlow replies. */
3687 case OFPUTIL_OFPT_ECHO_REPLY:
3690 /* Nicira extension requests. */
3691 case OFPUTIL_NXT_ROLE_REQUEST:
3692 return handle_role_request(ofconn, oh);
3694 case OFPUTIL_NXT_SET_FLOW_FORMAT:
3695 return handle_nxt_set_flow_format(ofconn, oh);
3697 case OFPUTIL_NXT_FLOW_MOD:
3698 return handle_flow_mod(ofconn, oh);
3700 /* OpenFlow statistics requests. */
3701 case OFPUTIL_OFPST_DESC_REQUEST:
3702 return handle_desc_stats_request(ofconn, oh);
3704 case OFPUTIL_OFPST_FLOW_REQUEST:
3705 return handle_flow_stats_request(ofconn, oh);
3707 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
3708 return handle_aggregate_stats_request(ofconn, oh);
3710 case OFPUTIL_OFPST_TABLE_REQUEST:
3711 return handle_table_stats_request(ofconn, oh);
3713 case OFPUTIL_OFPST_PORT_REQUEST:
3714 return handle_port_stats_request(ofconn, oh);
3716 case OFPUTIL_OFPST_QUEUE_REQUEST:
3717 return handle_queue_stats_request(ofconn, oh);
3719 /* Nicira extension statistics requests. */
3720 case OFPUTIL_NXST_FLOW_REQUEST:
3721 return handle_nxst_flow(ofconn, oh);
3723 case OFPUTIL_NXST_AGGREGATE_REQUEST:
3724 return handle_nxst_aggregate(ofconn, oh);
3726 case OFPUTIL_INVALID:
3727 case OFPUTIL_OFPT_HELLO:
3728 case OFPUTIL_OFPT_ERROR:
3729 case OFPUTIL_OFPT_FEATURES_REPLY:
3730 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
3731 case OFPUTIL_OFPT_PACKET_IN:
3732 case OFPUTIL_OFPT_FLOW_REMOVED:
3733 case OFPUTIL_OFPT_PORT_STATUS:
3734 case OFPUTIL_OFPT_BARRIER_REPLY:
3735 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
3736 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
3737 case OFPUTIL_OFPST_DESC_REPLY:
3738 case OFPUTIL_OFPST_FLOW_REPLY:
3739 case OFPUTIL_OFPST_QUEUE_REPLY:
3740 case OFPUTIL_OFPST_PORT_REPLY:
3741 case OFPUTIL_OFPST_TABLE_REPLY:
3742 case OFPUTIL_OFPST_AGGREGATE_REPLY:
3743 case OFPUTIL_NXT_ROLE_REPLY:
3744 case OFPUTIL_NXT_FLOW_REMOVED:
3745 case OFPUTIL_NXST_FLOW_REPLY:
3746 case OFPUTIL_NXST_AGGREGATE_REPLY:
3748 if (VLOG_IS_WARN_ENABLED()) {
3749 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3750 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3753 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
3754 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
3756 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3762 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
3764 int error = handle_openflow__(ofconn, ofp_msg);
3766 send_error_oh(ofconn, ofp_msg->data, error);
3768 COVERAGE_INC(ofproto_recv_openflow);
3772 handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3774 struct facet *facet;
3777 /* Obtain in_port and tun_id, at least. */
3778 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3780 /* Set header pointers in 'flow'. */
3781 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
3783 if (cfm_should_process_flow(&flow)) {
3784 ofproto_process_cfm(p, &flow, upcall->packet);
3785 ofpbuf_delete(upcall->packet);
3787 } else if (p->ofhooks->special_cb
3788 && !p->ofhooks->special_cb(&flow, upcall->packet, p->aux)) {
3789 ofpbuf_delete(upcall->packet);
3793 /* Check with in-band control to see if this packet should be sent
3794 * to the local port regardless of the flow table. */
3795 if (connmgr_msg_in_hook(p->connmgr, &flow, upcall->packet)) {
3796 ofproto_send_packet(p, ODPP_LOCAL, upcall->packet);
3799 facet = facet_lookup_valid(p, &flow);
3801 struct rule *rule = rule_lookup(p, &flow);
3803 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3804 struct ofport *port = get_port(p, flow.in_port);
3806 if (port->opp.config & htonl(OFPPC_NO_PACKET_IN)) {
3807 COVERAGE_INC(ofproto_no_packet_in);
3808 /* XXX install 'drop' flow entry */
3809 ofpbuf_delete(upcall->packet);
3813 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
3817 COVERAGE_INC(ofproto_packet_in);
3818 send_packet_in(p, upcall, &flow, false);
3822 facet = facet_create(p, rule, &flow, upcall->packet);
3823 } else if (!facet->may_install) {
3824 /* The facet is not installable, that is, we need to process every
3825 * packet, so process the current packet's actions into 'facet'. */
3826 facet_make_actions(p, facet, upcall->packet);
3829 if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) {
3831 * Extra-special case for fail-open mode.
3833 * We are in fail-open mode and the packet matched the fail-open rule,
3834 * but we are connected to a controller too. We should send the packet
3835 * up to the controller in the hope that it will try to set up a flow
3836 * and thereby allow us to exit fail-open.
3838 * See the top-level comment in fail-open.c for more information.
3840 send_packet_in(p, upcall, &flow, true);
3843 facet_execute(p, facet, upcall->packet);
3844 facet_install(p, facet, false);
3848 handle_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3852 switch (upcall->type) {
3853 case DPIF_UC_ACTION:
3854 COVERAGE_INC(ofproto_ctlr_action);
3855 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3856 send_packet_in(p, upcall, &flow, false);
3859 case DPIF_UC_SAMPLE:
3861 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3862 ofproto_sflow_received(p->sflow, upcall, &flow);
3864 ofpbuf_delete(upcall->packet);
3868 handle_miss_upcall(p, upcall);
3871 case DPIF_N_UC_TYPES:
3873 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3878 /* Flow expiration. */
3880 static int ofproto_dp_max_idle(const struct ofproto *);
3881 static void ofproto_update_stats(struct ofproto *);
3882 static void rule_expire(struct ofproto *, struct rule *);
3883 static void ofproto_expire_facets(struct ofproto *, int dp_max_idle);
3885 /* This function is called periodically by ofproto_run(). Its job is to
3886 * collect updates for the flows that have been installed into the datapath,
3887 * most importantly when they last were used, and then use that information to
3888 * expire flows that have not been used recently.
3890 * Returns the number of milliseconds after which it should be called again. */
3892 ofproto_expire(struct ofproto *ofproto)
3894 struct rule *rule, *next_rule;
3895 struct cls_cursor cursor;
3898 /* Update stats for each flow in the datapath. */
3899 ofproto_update_stats(ofproto);
3901 /* Expire facets that have been idle too long. */
3902 dp_max_idle = ofproto_dp_max_idle(ofproto);
3903 ofproto_expire_facets(ofproto, dp_max_idle);
3905 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3906 cls_cursor_init(&cursor, &ofproto->cls, NULL);
3907 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3908 rule_expire(ofproto, rule);
3911 /* Let the hook know that we're at a stable point: all outstanding data
3912 * in existing flows has been accounted to the account_cb. Thus, the
3913 * hook can now reasonably do operations that depend on having accurate
3914 * flow volume accounting (currently, that's just bond rebalancing). */
3915 if (ofproto->ofhooks->account_checkpoint_cb) {
3916 ofproto->ofhooks->account_checkpoint_cb(ofproto->aux);
3919 return MIN(dp_max_idle, 1000);
3922 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3924 * This function also pushes statistics updates to rules which each facet
3925 * resubmits into. Generally these statistics will be accurate. However, if a
3926 * facet changes the rule it resubmits into at some time in between
3927 * ofproto_update_stats() runs, it is possible that statistics accrued to the
3928 * old rule will be incorrectly attributed to the new rule. This could be
3929 * avoided by calling ofproto_update_stats() whenever rules are created or
3930 * deleted. However, the performance impact of making so many calls to the
3931 * datapath do not justify the benefit of having perfectly accurate statistics.
3934 ofproto_update_stats(struct ofproto *p)
3936 const struct dpif_flow_stats *stats;
3937 struct dpif_flow_dump dump;
3938 const struct nlattr *key;
3941 dpif_flow_dump_start(&dump, p->dpif);
3942 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3943 struct facet *facet;
3946 if (odp_flow_key_to_flow(key, key_len, &flow)) {
3950 odp_flow_key_format(key, key_len, &s);
3951 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
3957 facet = facet_find(p, &flow);
3959 if (facet && facet->installed) {
3961 if (stats->n_packets >= facet->dp_packet_count) {
3962 facet->packet_count += stats->n_packets - facet->dp_packet_count;
3964 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3967 if (stats->n_bytes >= facet->dp_byte_count) {
3968 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
3970 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3973 facet->dp_packet_count = stats->n_packets;
3974 facet->dp_byte_count = stats->n_bytes;
3976 facet_update_time(p, facet, stats->used);
3977 facet_account(p, facet, stats->n_bytes);
3978 facet_push_stats(p, facet);
3980 /* There's a flow in the datapath that we know nothing about.
3982 COVERAGE_INC(ofproto_unexpected_rule);
3983 dpif_flow_del(p->dpif, key, key_len, NULL);
3986 dpif_flow_dump_done(&dump);
3989 /* Calculates and returns the number of milliseconds of idle time after which
3990 * facets should expire from the datapath and we should fold their statistics
3991 * into their parent rules in userspace. */
3993 ofproto_dp_max_idle(const struct ofproto *ofproto)
3996 * Idle time histogram.
3998 * Most of the time a switch has a relatively small number of facets. When
3999 * this is the case we might as well keep statistics for all of them in
4000 * userspace and to cache them in the kernel datapath for performance as
4003 * As the number of facets increases, the memory required to maintain
4004 * statistics about them in userspace and in the kernel becomes
4005 * significant. However, with a large number of facets it is likely that
4006 * only a few of them are "heavy hitters" that consume a large amount of
4007 * bandwidth. At this point, only heavy hitters are worth caching in the
4008 * kernel and maintaining in userspaces; other facets we can discard.
4010 * The technique used to compute the idle time is to build a histogram with
4011 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
4012 * that is installed in the kernel gets dropped in the appropriate bucket.
4013 * After the histogram has been built, we compute the cutoff so that only
4014 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
4015 * cached. At least the most-recently-used bucket of facets is kept, so
4016 * actually an arbitrary number of facets can be kept in any given
4017 * expiration run (though the next run will delete most of those unless
4018 * they receive additional data).
4020 * This requires a second pass through the facets, in addition to the pass
4021 * made by ofproto_update_stats(), because the former function never looks
4022 * at uninstallable facets.
4024 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
4025 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
4026 int buckets[N_BUCKETS] = { 0 };
4027 struct facet *facet;
4032 total = hmap_count(&ofproto->facets);
4033 if (total <= 1000) {
4034 return N_BUCKETS * BUCKET_WIDTH;
4037 /* Build histogram. */
4039 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
4040 long long int idle = now - facet->used;
4041 int bucket = (idle <= 0 ? 0
4042 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4043 : (unsigned int) idle / BUCKET_WIDTH);
4047 /* Find the first bucket whose flows should be expired. */
4048 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
4049 if (buckets[bucket]) {
4052 subtotal += buckets[bucket++];
4053 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
4058 if (VLOG_IS_DBG_ENABLED()) {
4062 ds_put_cstr(&s, "keep");
4063 for (i = 0; i < N_BUCKETS; i++) {
4065 ds_put_cstr(&s, ", drop");
4068 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4071 VLOG_INFO("%s: %s (msec:count)",
4072 dpif_name(ofproto->dpif), ds_cstr(&s));
4076 return bucket * BUCKET_WIDTH;
4080 facet_active_timeout(struct ofproto *ofproto, struct facet *facet)
4082 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
4083 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
4084 struct ofexpired expired;
4086 if (facet->installed) {
4087 struct dpif_flow_stats stats;
4089 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
4091 facet_update_stats(ofproto, facet, &stats);
4094 expired.flow = facet->flow;
4095 expired.packet_count = facet->packet_count;
4096 expired.byte_count = facet->byte_count;
4097 expired.used = facet->used;
4098 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4103 ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle)
4105 long long int cutoff = time_msec() - dp_max_idle;
4106 struct facet *facet, *next_facet;
4108 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
4109 facet_active_timeout(ofproto, facet);
4110 if (facet->used < cutoff) {
4111 facet_remove(ofproto, facet);
4116 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4117 * then delete it entirely. */
4119 rule_expire(struct ofproto *ofproto, struct rule *rule)
4121 struct facet *facet, *next_facet;
4125 /* Has 'rule' expired? */
4127 if (rule->hard_timeout
4128 && now > rule->created + rule->hard_timeout * 1000) {
4129 reason = OFPRR_HARD_TIMEOUT;
4130 } else if (rule->idle_timeout && list_is_empty(&rule->facets)
4131 && now >rule->used + rule->idle_timeout * 1000) {
4132 reason = OFPRR_IDLE_TIMEOUT;
4137 COVERAGE_INC(ofproto_expired);
4139 /* Update stats. (This is a no-op if the rule expired due to an idle
4140 * timeout, because that only happens when the rule has no facets left.) */
4141 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4142 facet_remove(ofproto, facet);
4145 /* Get rid of the rule. */
4146 if (!rule_is_hidden(rule)) {
4147 rule_send_removed(ofproto, rule, reason);
4149 rule_remove(ofproto, rule);
4153 rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason)
4155 struct ofputil_flow_removed fr;
4157 if (!rule->send_flow_removed) {
4162 fr.cookie = rule->flow_cookie;
4164 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
4165 fr.idle_timeout = rule->idle_timeout;
4166 fr.packet_count = rule->packet_count;
4167 fr.byte_count = rule->byte_count;
4169 connmgr_send_flow_removed(p->connmgr, &fr);
4172 /* Obtains statistics for 'rule' and stores them in '*packets' and '*bytes'.
4173 * The returned statistics include statistics for all of 'rule''s facets. */
4175 rule_get_stats(const struct rule *rule, uint64_t *packets, uint64_t *bytes)
4178 struct facet *facet;
4180 /* Start from historical data for 'rule' itself that are no longer tracked
4181 * in facets. This counts, for example, facets that have expired. */
4182 p = rule->packet_count;
4183 b = rule->byte_count;
4185 /* Add any statistics that are tracked by facets. This includes
4186 * statistical data recently updated by ofproto_update_stats() as well as
4187 * stats for packets that were executed "by hand" via dpif_execute(). */
4188 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4189 p += facet->packet_count;
4190 b += facet->byte_count;
4197 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
4198 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
4199 * their individual configurations.
4201 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
4202 * Otherwise, ownership is transferred to this function. */
4204 send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall,
4205 const struct flow *flow, bool clone)
4207 connmgr_send_packet_in(ofproto->connmgr, upcall, flow,
4208 clone ? NULL : upcall->packet);
4212 pick_datapath_id(const struct ofproto *ofproto)
4214 const struct ofport *port;
4216 port = get_port(ofproto, ODPP_LOCAL);
4218 uint8_t ea[ETH_ADDR_LEN];
4221 error = netdev_get_etheraddr(port->netdev, ea);
4223 return eth_addr_to_uint64(ea);
4225 VLOG_WARN("could not get MAC address for %s (%s)",
4226 netdev_get_name(port->netdev), strerror(error));
4228 return ofproto->fallback_dpid;
4232 pick_fallback_dpid(void)
4234 uint8_t ea[ETH_ADDR_LEN];
4235 eth_addr_nicira_random(ea);
4236 return eth_addr_to_uint64(ea);
4240 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
4241 void *aux OVS_UNUSED)
4243 const struct shash_node *node;
4247 SHASH_FOR_EACH (node, &all_ofprotos) {
4248 ds_put_format(&results, "%s\n", node->name);
4250 unixctl_command_reply(conn, 200, ds_cstr(&results));
4251 ds_destroy(&results);
4254 struct ofproto_trace {
4255 struct action_xlate_ctx ctx;
4261 trace_format_rule(struct ds *result, int level, const struct rule *rule)
4263 ds_put_char_multiple(result, '\t', level);
4265 ds_put_cstr(result, "No match\n");
4269 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
4270 ntohll(rule->flow_cookie));
4271 cls_rule_format(&rule->cr, result);
4272 ds_put_char(result, '\n');
4274 ds_put_char_multiple(result, '\t', level);
4275 ds_put_cstr(result, "OpenFlow ");
4276 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
4277 rule->n_actions * sizeof *rule->actions);
4278 ds_put_char(result, '\n');
4282 trace_format_flow(struct ds *result, int level, const char *title,
4283 struct ofproto_trace *trace)
4285 ds_put_char_multiple(result, '\t', level);
4286 ds_put_format(result, "%s: ", title);
4287 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4288 ds_put_cstr(result, "unchanged");
4290 flow_format(result, &trace->ctx.flow);
4291 trace->flow = trace->ctx.flow;
4293 ds_put_char(result, '\n');
4297 trace_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
4299 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4300 struct ds *result = trace->result;
4302 ds_put_char(result, '\n');
4303 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4304 trace_format_rule(result, ctx->recurse + 1, rule);
4308 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4309 void *aux OVS_UNUSED)
4311 char *dpname, *in_port_s, *tun_id_s, *packet_s;
4312 char *args = xstrdup(args_);
4313 char *save_ptr = NULL;
4314 struct ofproto *ofproto;
4315 struct ofpbuf packet;
4323 ofpbuf_init(&packet, strlen(args) / 2);
4326 dpname = strtok_r(args, " ", &save_ptr);
4327 tun_id_s = strtok_r(NULL, " ", &save_ptr);
4328 in_port_s = strtok_r(NULL, " ", &save_ptr);
4329 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4330 if (!dpname || !in_port_s || !packet_s) {
4331 unixctl_command_reply(conn, 501, "Bad command syntax");
4335 ofproto = shash_find_data(&all_ofprotos, dpname);
4337 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4342 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
4343 in_port = ofp_port_to_odp_port(atoi(in_port_s));
4345 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
4346 packet_s += strspn(packet_s, " ");
4347 if (*packet_s != '\0') {
4348 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4351 if (packet.size < ETH_HEADER_LEN) {
4352 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
4356 ds_put_cstr(&result, "Packet: ");
4357 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
4358 ds_put_cstr(&result, s);
4361 flow_extract(&packet, tun_id, in_port, &flow);
4362 ds_put_cstr(&result, "Flow: ");
4363 flow_format(&result, &flow);
4364 ds_put_char(&result, '\n');
4366 rule = rule_lookup(ofproto, &flow);
4367 trace_format_rule(&result, 0, rule);
4369 struct ofproto_trace trace;
4370 struct ofpbuf *odp_actions;
4372 trace.result = &result;
4374 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
4375 trace.ctx.resubmit_hook = trace_resubmit;
4376 odp_actions = xlate_actions(&trace.ctx,
4377 rule->actions, rule->n_actions);
4379 ds_put_char(&result, '\n');
4380 trace_format_flow(&result, 0, "Final flow", &trace);
4381 ds_put_cstr(&result, "Datapath actions: ");
4382 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4383 ofpbuf_delete(odp_actions);
4386 unixctl_command_reply(conn, 200, ds_cstr(&result));
4389 ds_destroy(&result);
4390 ofpbuf_uninit(&packet);
4395 ofproto_unixctl_init(void)
4397 static bool registered;
4403 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
4404 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4408 default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
4409 struct ofpbuf *odp_actions, tag_type *tags,
4410 uint16_t *nf_output_iface, void *ofproto_)
4412 struct ofproto *ofproto = ofproto_;
4413 struct mac_entry *dst_mac;
4415 /* Drop frames for reserved multicast addresses. */
4416 if (eth_addr_is_reserved(flow->dl_dst)) {
4420 /* Learn source MAC (but don't try to learn from revalidation). */
4422 && mac_learning_may_learn(ofproto->ml, flow->dl_src, 0)) {
4423 struct mac_entry *src_mac;
4425 src_mac = mac_learning_insert(ofproto->ml, flow->dl_src, 0);
4426 if (mac_entry_is_new(src_mac) || src_mac->port.i != flow->in_port) {
4427 /* The log messages here could actually be useful in debugging,
4428 * so keep the rate limit relatively high. */
4429 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4430 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
4431 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
4433 ofproto_revalidate(ofproto,
4434 mac_learning_changed(ofproto->ml, src_mac));
4435 src_mac->port.i = flow->in_port;
4439 /* Determine output port. */
4440 dst_mac = mac_learning_lookup(ofproto->ml, flow->dl_dst, 0, tags);
4442 flood_packets(ofproto, flow->in_port, htonl(OFPPC_NO_FLOOD),
4443 nf_output_iface, odp_actions);
4445 int out_port = dst_mac->port.i;
4446 if (out_port != flow->in_port) {
4447 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, out_port);
4448 *nf_output_iface = out_port;
4457 static const struct ofhooks default_ofhooks = {
4458 default_normal_ofhook_cb,