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; /* In host byte order. */
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_update_stats(struct ofproto *, struct facet *,
272 const struct dpif_flow_stats *);
273 static void facet_push_stats(struct ofproto *, struct facet *);
275 static void send_packet_in(struct ofproto *, struct dpif_upcall *,
276 const struct flow *, bool clone);
280 uint64_t datapath_id; /* Datapath ID. */
281 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
282 char *mfr_desc; /* Manufacturer. */
283 char *hw_desc; /* Hardware. */
284 char *sw_desc; /* Software version. */
285 char *serial_desc; /* Serial number. */
286 char *dp_desc; /* Datapath description. */
290 struct netdev_monitor *netdev_monitor;
291 struct hmap ports; /* Contains "struct ofport"s. */
292 struct shash port_by_name;
296 struct netflow *netflow;
297 struct ofproto_sflow *sflow;
300 struct classifier cls;
301 struct timer next_expiration;
305 bool need_revalidate;
306 struct tag_set revalidate_set;
308 /* OpenFlow connections. */
309 struct connmgr *connmgr;
311 /* Hooks for ovs-vswitchd. */
312 const struct ofhooks *ofhooks;
315 /* Used by default ofhooks. */
316 struct mac_learning *ml;
319 /* Map from dpif name to struct ofproto, for use by unixctl commands. */
320 static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos);
322 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
324 static const struct ofhooks default_ofhooks;
326 static uint64_t pick_datapath_id(const struct ofproto *);
327 static uint64_t pick_fallback_dpid(void);
329 static void ofproto_flush_flows__(struct ofproto *);
330 static int ofproto_expire(struct ofproto *);
331 static void flow_push_stats(struct ofproto *, const struct rule *,
332 struct flow *, uint64_t packets, uint64_t bytes,
335 static void handle_upcall(struct ofproto *, struct dpif_upcall *);
337 static void handle_openflow(struct ofconn *, struct ofpbuf *);
339 static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
340 static void update_port(struct ofproto *, const char *devname);
341 static int init_ports(struct ofproto *);
342 static void reinit_ports(struct ofproto *);
344 static void ofproto_unixctl_init(void);
347 ofproto_create(const char *datapath, const char *datapath_type,
348 const struct ofhooks *ofhooks, void *aux,
349 struct ofproto **ofprotop)
351 char local_name[IF_NAMESIZE];
358 ofproto_unixctl_init();
360 /* Connect to datapath and start listening for messages. */
361 error = dpif_open(datapath, datapath_type, &dpif);
363 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
366 error = dpif_recv_set_mask(dpif,
367 ((1u << DPIF_UC_MISS) |
368 (1u << DPIF_UC_ACTION) |
369 (1u << DPIF_UC_SAMPLE)));
371 VLOG_ERR("failed to listen on datapath %s: %s",
372 datapath, strerror(error));
376 dpif_flow_flush(dpif);
377 dpif_recv_purge(dpif);
379 error = dpif_port_get_name(dpif, ODPP_LOCAL,
380 local_name, sizeof local_name);
382 VLOG_ERR("%s: cannot get name of datapath local port (%s)",
383 datapath, strerror(error));
387 /* Initialize settings. */
388 p = xzalloc(sizeof *p);
389 p->fallback_dpid = pick_fallback_dpid();
390 p->datapath_id = p->fallback_dpid;
391 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
392 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
393 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
394 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
395 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
397 /* Initialize datapath. */
399 p->netdev_monitor = netdev_monitor_create();
400 hmap_init(&p->ports);
401 shash_init(&p->port_by_name);
402 p->max_ports = dpif_get_max_ports(dpif);
404 /* Initialize submodules. */
408 /* Initialize flow table. */
409 classifier_init(&p->cls);
410 timer_set_duration(&p->next_expiration, 1000);
412 /* Initialize facet table. */
413 hmap_init(&p->facets);
414 p->need_revalidate = false;
415 tag_set_init(&p->revalidate_set);
417 /* Initialize hooks. */
419 p->ofhooks = ofhooks;
423 p->ofhooks = &default_ofhooks;
425 p->ml = mac_learning_create();
428 /* Pick final datapath ID. */
429 p->datapath_id = pick_datapath_id(p);
430 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
432 shash_add_once(&all_ofprotos, dpif_name(p->dpif), p);
434 /* Initialize OpenFlow connections. */
435 p->connmgr = connmgr_create(p, datapath, local_name);
442 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
444 uint64_t old_dpid = p->datapath_id;
445 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
446 if (p->datapath_id != old_dpid) {
447 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
449 /* Force all active connections to reconnect, since there is no way to
450 * notify a controller that the datapath ID has changed. */
451 ofproto_reconnect_controllers(p);
456 ofproto_set_controllers(struct ofproto *p,
457 const struct ofproto_controller *controllers,
458 size_t n_controllers)
460 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
464 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
466 connmgr_set_fail_mode(p->connmgr, fail_mode);
469 /* Drops the connections between 'ofproto' and all of its controllers, forcing
470 * them to reconnect. */
472 ofproto_reconnect_controllers(struct ofproto *ofproto)
474 connmgr_reconnect(ofproto->connmgr);
477 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
478 * in-band control should guarantee access, in the same way that in-band
479 * control guarantees access to OpenFlow controllers. */
481 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
482 const struct sockaddr_in *extras, size_t n)
484 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
487 /* Sets the OpenFlow queue used by flows set up by in-band control on
488 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
489 * flows will use the default queue. */
491 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
493 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
497 ofproto_set_desc(struct ofproto *p,
498 const char *mfr_desc, const char *hw_desc,
499 const char *sw_desc, const char *serial_desc,
502 struct ofp_desc_stats *ods;
505 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
506 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
507 sizeof ods->mfr_desc);
510 p->mfr_desc = xstrdup(mfr_desc);
513 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
514 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
515 sizeof ods->hw_desc);
518 p->hw_desc = xstrdup(hw_desc);
521 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
522 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
523 sizeof ods->sw_desc);
526 p->sw_desc = xstrdup(sw_desc);
529 if (strlen(serial_desc) >= sizeof ods->serial_num) {
530 VLOG_WARN("truncating serial_desc, must be less than %zu "
532 sizeof ods->serial_num);
534 free(p->serial_desc);
535 p->serial_desc = xstrdup(serial_desc);
538 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
539 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
540 sizeof ods->dp_desc);
543 p->dp_desc = xstrdup(dp_desc);
548 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
550 return connmgr_set_snoops(ofproto->connmgr, snoops);
554 ofproto_set_netflow(struct ofproto *ofproto,
555 const struct netflow_options *nf_options)
557 if (nf_options && !sset_is_empty(&nf_options->collectors)) {
558 if (!ofproto->netflow) {
559 ofproto->netflow = netflow_create();
561 return netflow_set_options(ofproto->netflow, nf_options);
563 netflow_destroy(ofproto->netflow);
564 ofproto->netflow = NULL;
570 ofproto_set_sflow(struct ofproto *ofproto,
571 const struct ofproto_sflow_options *oso)
573 struct ofproto_sflow *os = ofproto->sflow;
576 struct ofport *ofport;
578 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
579 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
580 ofproto_sflow_add_port(os, ofport->odp_port,
581 netdev_get_name(ofport->netdev));
584 ofproto_sflow_set_options(os, oso);
586 ofproto_sflow_destroy(os);
587 ofproto->sflow = NULL;
591 /* Connectivity Fault Management configuration. */
593 /* Clears the CFM configuration from 'port_no' on 'ofproto'. */
595 ofproto_iface_clear_cfm(struct ofproto *ofproto, uint32_t port_no)
597 struct ofport *ofport = get_port(ofproto, port_no);
598 if (ofport && ofport->cfm){
599 cfm_destroy(ofport->cfm);
604 /* Configures connectivity fault management on 'port_no' in 'ofproto'. Takes
605 * basic configuration from the configuration members in 'cfm', and the set of
606 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
607 * Ignores the statistics members of 'cfm'.
609 * This function has no effect if 'ofproto' does not have a port 'port_no'. */
611 ofproto_iface_set_cfm(struct ofproto *ofproto, uint32_t port_no,
612 const struct cfm *cfm,
613 const uint16_t *remote_mps, size_t n_remote_mps)
615 struct ofport *ofport;
617 ofport = get_port(ofproto, port_no);
619 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
620 dpif_name(ofproto->dpif), port_no);
625 ofport->cfm = cfm_create();
628 ofport->cfm->mpid = cfm->mpid;
629 ofport->cfm->interval = cfm->interval;
630 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
632 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
634 if (!cfm_configure(ofport->cfm)) {
635 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed",
636 dpif_name(ofproto->dpif), port_no,
637 netdev_get_name(ofport->netdev));
638 cfm_destroy(ofport->cfm);
643 /* Returns the connectivity fault management object associated with 'port_no'
644 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
645 * 'port_no' or if that port does not have CFM configured. The caller must not
646 * modify or destroy the returned object. */
648 ofproto_iface_get_cfm(struct ofproto *ofproto, uint32_t port_no)
650 struct ofport *ofport = get_port(ofproto, port_no);
651 return ofport ? ofport->cfm : NULL;
655 ofproto_get_datapath_id(const struct ofproto *ofproto)
657 return ofproto->datapath_id;
660 enum ofproto_fail_mode
661 ofproto_get_fail_mode(const struct ofproto *p)
663 return connmgr_get_fail_mode(p->connmgr);
667 ofproto_has_snoops(const struct ofproto *ofproto)
669 return connmgr_has_snoops(ofproto->connmgr);
673 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
675 connmgr_get_snoops(ofproto->connmgr, snoops);
679 ofproto_destroy(struct ofproto *p)
681 struct ofport *ofport, *next_ofport;
687 shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif));
689 ofproto_flush_flows__(p);
690 connmgr_destroy(p->connmgr);
691 classifier_destroy(&p->cls);
692 hmap_destroy(&p->facets);
695 netdev_monitor_destroy(p->netdev_monitor);
696 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
697 hmap_remove(&p->ports, &ofport->hmap_node);
700 shash_destroy(&p->port_by_name);
702 netflow_destroy(p->netflow);
703 ofproto_sflow_destroy(p->sflow);
705 mac_learning_destroy(p->ml);
710 free(p->serial_desc);
713 hmap_destroy(&p->ports);
719 ofproto_run(struct ofproto *p)
721 int error = ofproto_run1(p);
723 error = ofproto_run2(p, false);
729 process_port_change(struct ofproto *ofproto, int error, char *devname)
731 if (error == ENOBUFS) {
732 reinit_ports(ofproto);
734 update_port(ofproto, devname);
740 ofproto_run1(struct ofproto *p)
742 struct ofport *ofport;
747 if (shash_is_empty(&p->port_by_name)) {
751 for (i = 0; i < 50; i++) {
752 struct dpif_upcall packet;
754 error = dpif_recv(p->dpif, &packet);
756 if (error == ENODEV) {
757 /* Someone destroyed the datapath behind our back. The caller
758 * better destroy us and give up, because we're just going to
759 * spin from here on out. */
760 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
761 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
768 handle_upcall(p, &packet);
771 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
772 process_port_change(p, error, devname);
774 while ((error = netdev_monitor_poll(p->netdev_monitor,
775 &devname)) != EAGAIN) {
776 process_port_change(p, error, devname);
779 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
780 ofport_run(p, ofport);
783 connmgr_run(p->connmgr, handle_openflow);
785 if (timer_expired(&p->next_expiration)) {
786 int delay = ofproto_expire(p);
787 timer_set_duration(&p->next_expiration, delay);
788 COVERAGE_INC(ofproto_expiration);
792 netflow_run(p->netflow);
795 ofproto_sflow_run(p->sflow);
802 ofproto_run2(struct ofproto *p, bool revalidate_all)
804 /* Figure out what we need to revalidate now, if anything. */
805 struct tag_set revalidate_set = p->revalidate_set;
806 if (p->need_revalidate) {
807 revalidate_all = true;
810 /* Clear the revalidation flags. */
811 tag_set_init(&p->revalidate_set);
812 p->need_revalidate = false;
814 /* Now revalidate if there's anything to do. */
815 if (revalidate_all || !tag_set_is_empty(&revalidate_set)) {
816 struct facet *facet, *next;
818 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) {
820 || tag_set_intersects(&revalidate_set, facet->tags)) {
821 facet_revalidate(p, facet);
830 ofproto_wait(struct ofproto *p)
832 struct ofport *ofport;
834 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
837 dpif_recv_wait(p->dpif);
838 dpif_port_poll_wait(p->dpif);
839 netdev_monitor_poll_wait(p->netdev_monitor);
841 ofproto_sflow_wait(p->sflow);
843 if (!tag_set_is_empty(&p->revalidate_set)) {
844 poll_immediate_wake();
846 if (p->need_revalidate) {
847 /* Shouldn't happen, but if it does just go around again. */
848 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
849 poll_immediate_wake();
851 timer_wait(&p->next_expiration);
853 connmgr_wait(p->connmgr);
857 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
859 tag_set_add(&ofproto->revalidate_set, tag);
863 ofproto_get_revalidate_set(struct ofproto *ofproto)
865 return &ofproto->revalidate_set;
869 ofproto_is_alive(const struct ofproto *p)
871 return connmgr_has_controllers(p->connmgr);
875 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
878 connmgr_get_controller_info(ofproto->connmgr, info);
882 ofproto_free_ofproto_controller_info(struct shash *info)
884 struct shash_node *node;
886 SHASH_FOR_EACH (node, info) {
887 struct ofproto_controller_info *cinfo = node->data;
888 while (cinfo->pairs.n) {
889 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
896 /* Deletes port number 'odp_port' from the datapath for 'ofproto'.
898 * This is almost the same as calling dpif_port_del() directly on the
899 * datapath, but it also makes 'ofproto' close its open netdev for the port
900 * (if any). This makes it possible to create a new netdev of a different
901 * type under the same name, which otherwise the netdev library would refuse
902 * to do because of the conflict. (The netdev would eventually get closed on
903 * the next trip through ofproto_run(), but this interface is more direct.)
905 * Returns 0 if successful, otherwise a positive errno. */
907 ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port)
909 struct ofport *ofport = get_port(ofproto, odp_port);
910 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
913 error = dpif_port_del(ofproto->dpif, odp_port);
915 VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)",
916 dpif_name(ofproto->dpif), odp_port, name, strerror(error));
918 /* 'name' is the netdev's name and update_port() is going to close the
919 * netdev. Just in case update_port() refers to 'name' after it
920 * destroys 'ofport', make a copy of it around the update_port()
922 char *devname = xstrdup(name);
923 update_port(ofproto, devname);
929 /* Checks if 'ofproto' thinks 'odp_port' should be included in floods. Returns
930 * true if 'odp_port' exists and should be included, false otherwise. */
932 ofproto_port_is_floodable(struct ofproto *ofproto, uint16_t odp_port)
934 struct ofport *ofport = get_port(ofproto, odp_port);
935 return ofport && !(ofport->opp.config & OFPPC_NO_FLOOD);
938 /* Sends 'packet' out of port 'port_no' within 'p'. If 'vlan_tci' is zero the
939 * packet will not have any 802.1Q hader; if it is nonzero, then the packet
940 * will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
942 * Returns 0 if successful, otherwise a positive errno value. */
944 ofproto_send_packet(struct ofproto *ofproto,
945 uint32_t port_no, uint16_t vlan_tci,
946 const struct ofpbuf *packet)
948 struct ofpbuf odp_actions;
951 ofpbuf_init(&odp_actions, 32);
953 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
954 ntohs(vlan_tci & ~VLAN_CFI));
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', in *HOST* byte order. */
1060 static struct netdev *
1061 ofport_open(const struct dpif_port *dpif_port, struct ofp_phy_port *opp)
1063 struct netdev_options netdev_options;
1064 enum netdev_flags flags;
1065 struct netdev *netdev;
1068 memset(&netdev_options, 0, sizeof netdev_options);
1069 netdev_options.name = dpif_port->name;
1070 netdev_options.type = dpif_port->type;
1071 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
1073 error = netdev_open(&netdev_options, &netdev);
1075 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1076 "cannot be opened (%s)",
1077 dpif_port->name, dpif_port->port_no,
1078 dpif_port->name, strerror(error));
1082 netdev_get_flags(netdev, &flags);
1084 opp->port_no = odp_port_to_ofp_port(dpif_port->port_no);
1085 netdev_get_etheraddr(netdev, opp->hw_addr);
1086 ovs_strzcpy(opp->name, dpif_port->name, sizeof opp->name);
1087 opp->config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
1088 opp->state = netdev_get_carrier(netdev) ? 0 : OFPPS_LINK_DOWN;
1089 netdev_get_features(netdev, &opp->curr, &opp->advertised,
1090 &opp->supported, &opp->peer);
1095 ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port)
1097 if (get_port(p, dpif_port->port_no)) {
1098 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1099 dpif_port->port_no);
1101 } else if (shash_find(&p->port_by_name, dpif_port->name)) {
1102 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1110 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1111 * port number, and 'config' bits other than OFPPC_PORT_DOWN are
1114 ofport_equal(const struct ofp_phy_port *a, const struct ofp_phy_port *b)
1116 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1117 return (!memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1118 && a->state == b->state
1119 && !((a->config ^ b->config) & OFPPC_PORT_DOWN)
1120 && a->curr == b->curr
1121 && a->advertised == b->advertised
1122 && a->supported == b->supported
1123 && a->peer == b->peer);
1126 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1127 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1128 * one with the same name or port number). */
1130 ofport_install(struct ofproto *p,
1131 struct netdev *netdev, const struct ofp_phy_port *opp)
1133 const char *netdev_name = netdev_get_name(netdev);
1134 struct ofport *ofport;
1136 connmgr_send_port_status(p->connmgr, opp, OFPPR_ADD);
1138 /* Create ofport. */
1139 ofport = xmalloc(sizeof *ofport);
1140 ofport->netdev = netdev;
1142 ofport->odp_port = ofp_port_to_odp_port(opp->port_no);
1145 /* Add port to 'p'. */
1146 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1147 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
1148 shash_add(&p->port_by_name, netdev_name, ofport);
1150 ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
1154 /* Removes 'ofport' from 'p' and destroys it. */
1156 ofport_remove(struct ofproto *p, struct ofport *ofport)
1158 connmgr_send_port_status(p->connmgr, &ofport->opp, OFPPR_DELETE);
1160 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1161 hmap_remove(&p->ports, &ofport->hmap_node);
1162 shash_delete(&p->port_by_name,
1163 shash_find(&p->port_by_name,
1164 netdev_get_name(ofport->netdev)));
1166 ofproto_sflow_del_port(p->sflow, ofport->odp_port);
1169 ofport_free(ofport);
1172 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1175 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1177 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1179 ofport_remove(ofproto, port);
1183 /* Updates 'port' within 'ofproto' with the new 'netdev' and 'opp'.
1185 * Does not handle a name or port number change. The caller must implement
1186 * such a change as a delete followed by an add. */
1188 ofport_modified(struct ofproto *ofproto, struct ofport *port,
1189 struct netdev *netdev, struct ofp_phy_port *opp)
1191 memcpy(port->opp.hw_addr, opp->hw_addr, ETH_ADDR_LEN);
1192 port->opp.config = ((port->opp.config & ~OFPPC_PORT_DOWN)
1193 | (opp->config & OFPPC_PORT_DOWN));
1194 port->opp.state = opp->state;
1195 port->opp.curr = opp->curr;
1196 port->opp.advertised = opp->advertised;
1197 port->opp.supported = opp->supported;
1198 port->opp.peer = opp->peer;
1200 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1201 netdev_monitor_add(ofproto->netdev_monitor, netdev);
1203 netdev_close(port->netdev);
1204 port->netdev = netdev;
1206 connmgr_send_port_status(ofproto->connmgr, &port->opp, OFPPR_MODIFY);
1210 ofport_run(struct ofproto *ofproto, struct ofport *ofport)
1213 cfm_run(ofport->cfm);
1215 if (cfm_should_send_ccm(ofport->cfm)) {
1216 struct ofpbuf packet;
1219 ofpbuf_init(&packet, 0);
1220 ccm = eth_compose(&packet, eth_addr_ccm, ofport->opp.hw_addr,
1221 ETH_TYPE_CFM, sizeof *ccm);
1222 cfm_compose_ccm(ofport->cfm, ccm);
1223 ofproto_send_packet(ofproto, ofport->odp_port, 0, &packet);
1224 ofpbuf_uninit(&packet);
1230 ofport_wait(struct ofport *ofport)
1233 cfm_wait(ofport->cfm);
1238 ofport_free(struct ofport *ofport)
1241 cfm_destroy(ofport->cfm);
1242 netdev_close(ofport->netdev);
1247 static struct ofport *
1248 get_port(const struct ofproto *ofproto, uint16_t odp_port)
1250 struct ofport *port;
1252 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1253 hash_int(odp_port, 0), &ofproto->ports) {
1254 if (port->odp_port == odp_port) {
1262 update_port(struct ofproto *ofproto, const char *name)
1264 struct dpif_port dpif_port;
1265 struct ofp_phy_port opp;
1266 struct netdev *netdev;
1267 struct ofport *port;
1269 COVERAGE_INC(ofproto_update_port);
1271 /* Fetch 'name''s location and properties from the datapath. */
1272 netdev = (!dpif_port_query_by_name(ofproto->dpif, name, &dpif_port)
1273 ? ofport_open(&dpif_port, &opp)
1276 port = get_port(ofproto, dpif_port.port_no);
1277 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
1278 /* 'name' hasn't changed location. Any properties changed? */
1279 if (!ofport_equal(&port->opp, &opp)) {
1280 ofport_modified(ofproto, port, netdev, &opp);
1282 netdev_close(netdev);
1285 /* If 'port' is nonnull then its name differs from 'name' and thus
1286 * we should delete it. If we think there's a port named 'name'
1287 * then its port number must be wrong now so delete it too. */
1289 ofport_remove(ofproto, port);
1291 ofport_remove_with_name(ofproto, name);
1292 ofport_install(ofproto, netdev, &opp);
1295 /* Any port named 'name' is gone now. */
1296 ofport_remove_with_name(ofproto, name);
1298 dpif_port_destroy(&dpif_port);
1302 init_ports(struct ofproto *p)
1304 struct dpif_port_dump dump;
1305 struct dpif_port dpif_port;
1307 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1308 if (!ofport_conflicts(p, &dpif_port)) {
1309 struct ofp_phy_port opp;
1310 struct netdev *netdev;
1312 netdev = ofport_open(&dpif_port, &opp);
1314 ofport_install(p, netdev, &opp);
1322 /* Returns true if 'rule' should be hidden from the controller.
1324 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1325 * (e.g. by in-band control) and are intentionally hidden from the
1328 rule_is_hidden(const struct rule *rule)
1330 return rule->cr.priority > UINT16_MAX;
1333 /* Creates and returns a new rule initialized as specified.
1335 * The caller is responsible for inserting the rule into the classifier (with
1336 * rule_insert()). */
1337 static struct rule *
1338 rule_create(const struct cls_rule *cls_rule,
1339 const union ofp_action *actions, size_t n_actions,
1340 uint16_t idle_timeout, uint16_t hard_timeout,
1341 ovs_be64 flow_cookie, bool send_flow_removed)
1343 struct rule *rule = xzalloc(sizeof *rule);
1344 rule->cr = *cls_rule;
1345 rule->idle_timeout = idle_timeout;
1346 rule->hard_timeout = hard_timeout;
1347 rule->flow_cookie = flow_cookie;
1348 rule->used = rule->created = time_msec();
1349 rule->send_flow_removed = send_flow_removed;
1350 list_init(&rule->facets);
1351 if (n_actions > 0) {
1352 rule->n_actions = n_actions;
1353 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1359 static struct rule *
1360 rule_from_cls_rule(const struct cls_rule *cls_rule)
1362 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1366 rule_free(struct rule *rule)
1368 free(rule->actions);
1372 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
1373 * destroying any that no longer has a rule (which is probably all of them).
1375 * The caller must have already removed 'rule' from the classifier. */
1377 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1379 struct facet *facet, *next_facet;
1380 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1381 facet_revalidate(ofproto, facet);
1386 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1387 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1390 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
1392 const union ofp_action *oa;
1393 struct actions_iterator i;
1395 if (out_port == htons(OFPP_NONE)) {
1398 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1399 oa = actions_next(&i)) {
1400 if (action_outputs_to_port(oa, out_port)) {
1407 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1408 * 'packet', which arrived on 'in_port'.
1410 * Takes ownership of 'packet'. */
1412 execute_odp_actions(struct ofproto *ofproto, const struct flow *flow,
1413 const struct nlattr *odp_actions, size_t actions_len,
1414 struct ofpbuf *packet)
1416 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1417 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1418 /* As an optimization, avoid a round-trip from userspace to kernel to
1419 * userspace. This also avoids possibly filling up kernel packet
1420 * buffers along the way. */
1421 struct dpif_upcall upcall;
1423 upcall.type = DPIF_UC_ACTION;
1424 upcall.packet = packet;
1427 upcall.userdata = nl_attr_get_u64(odp_actions);
1428 upcall.sample_pool = 0;
1429 upcall.actions = NULL;
1430 upcall.actions_len = 0;
1432 send_packet_in(ofproto, &upcall, flow, false);
1438 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
1439 ofpbuf_delete(packet);
1444 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
1445 * statistics appropriately. 'packet' must have at least sizeof(struct
1446 * ofp_packet_in) bytes of headroom.
1448 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
1449 * applying flow_extract() to 'packet' would yield the same flow as
1452 * 'facet' must have accurately composed ODP actions; that is, it must not be
1453 * in need of revalidation.
1455 * Takes ownership of 'packet'. */
1457 facet_execute(struct ofproto *ofproto, struct facet *facet,
1458 struct ofpbuf *packet)
1460 struct dpif_flow_stats stats;
1462 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1464 flow_extract_stats(&facet->flow, packet, &stats);
1465 stats.used = time_msec();
1466 if (execute_odp_actions(ofproto, &facet->flow,
1467 facet->actions, facet->actions_len, packet)) {
1468 facet_update_stats(ofproto, facet, &stats);
1472 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1473 * statistics (or the statistics for one of its facets) appropriately.
1474 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
1476 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1477 * with statistics for 'packet' either way.
1479 * Takes ownership of 'packet'. */
1481 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
1482 struct ofpbuf *packet)
1484 struct action_xlate_ctx ctx;
1485 struct ofpbuf *odp_actions;
1486 struct facet *facet;
1490 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1492 flow_extract(packet, 0, in_port, &flow);
1494 /* First look for a related facet. If we find one, account it to that. */
1495 facet = facet_lookup_valid(ofproto, &flow);
1496 if (facet && facet->rule == rule) {
1497 facet_execute(ofproto, facet, packet);
1501 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
1502 * create a new facet for it and use that. */
1503 if (rule_lookup(ofproto, &flow) == rule) {
1504 facet = facet_create(ofproto, rule, &flow, packet);
1505 facet_execute(ofproto, facet, packet);
1506 facet_install(ofproto, facet, true);
1510 /* We can't account anything to a facet. If we were to try, then that
1511 * facet would have a non-matching rule, busting our invariants. */
1512 action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
1513 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1514 size = packet->size;
1515 if (execute_odp_actions(ofproto, &flow, odp_actions->data,
1516 odp_actions->size, packet)) {
1517 rule->used = time_msec();
1518 rule->packet_count++;
1519 rule->byte_count += size;
1520 flow_push_stats(ofproto, rule, &flow, 1, size, rule->used);
1522 ofpbuf_delete(odp_actions);
1525 /* Inserts 'rule' into 'p''s flow table. */
1527 rule_insert(struct ofproto *p, struct rule *rule)
1529 struct rule *displaced_rule;
1531 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1532 if (displaced_rule) {
1533 rule_destroy(p, displaced_rule);
1535 p->need_revalidate = true;
1538 /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a
1539 * 'flow' and an example 'packet' within that flow.
1541 * The caller must already have determined that no facet with an identical
1542 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1543 * 'ofproto''s classifier table. */
1544 static struct facet *
1545 facet_create(struct ofproto *ofproto, struct rule *rule,
1546 const struct flow *flow, const struct ofpbuf *packet)
1548 struct facet *facet;
1550 facet = xzalloc(sizeof *facet);
1551 facet->used = time_msec();
1552 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1553 list_push_back(&rule->facets, &facet->list_node);
1555 facet->flow = *flow;
1556 netflow_flow_init(&facet->nf_flow);
1557 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1559 facet_make_actions(ofproto, facet, packet);
1565 facet_free(struct facet *facet)
1567 free(facet->actions);
1571 /* Remove 'rule' from 'ofproto' and free up the associated memory:
1573 * - Removes 'rule' from the classifier.
1575 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
1576 * destroys them), via rule_destroy().
1579 rule_remove(struct ofproto *ofproto, struct rule *rule)
1581 COVERAGE_INC(ofproto_del_rule);
1582 ofproto->need_revalidate = true;
1583 classifier_remove(&ofproto->cls, &rule->cr);
1584 rule_destroy(ofproto, rule);
1587 /* Remove 'facet' from 'ofproto' and free up the associated memory:
1589 * - If 'facet' was installed in the datapath, uninstalls it and updates its
1590 * rule's statistics, via facet_uninstall().
1592 * - Removes 'facet' from its rule and from ofproto->facets.
1595 facet_remove(struct ofproto *ofproto, struct facet *facet)
1597 facet_uninstall(ofproto, facet);
1598 facet_flush_stats(ofproto, facet);
1599 hmap_remove(&ofproto->facets, &facet->hmap_node);
1600 list_remove(&facet->list_node);
1604 /* Composes the ODP actions for 'facet' based on its rule's actions. */
1606 facet_make_actions(struct ofproto *p, struct facet *facet,
1607 const struct ofpbuf *packet)
1609 const struct rule *rule = facet->rule;
1610 struct ofpbuf *odp_actions;
1611 struct action_xlate_ctx ctx;
1613 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
1614 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1615 facet->tags = ctx.tags;
1616 facet->may_install = ctx.may_set_up_flow;
1617 facet->nf_flow.output_iface = ctx.nf_output_iface;
1619 if (facet->actions_len != odp_actions->size
1620 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
1621 free(facet->actions);
1622 facet->actions_len = odp_actions->size;
1623 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1626 ofpbuf_delete(odp_actions);
1630 facet_put__(struct ofproto *ofproto, struct facet *facet,
1631 const struct nlattr *actions, size_t actions_len,
1632 struct dpif_flow_stats *stats)
1634 struct odputil_keybuf keybuf;
1635 enum dpif_flow_put_flags flags;
1638 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1640 flags |= DPIF_FP_ZERO_STATS;
1641 facet->dp_packet_count = 0;
1642 facet->dp_byte_count = 0;
1645 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1646 odp_flow_key_from_flow(&key, &facet->flow);
1648 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
1649 actions, actions_len, stats);
1652 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
1653 * 'zero_stats' is true, clears any existing statistics from the datapath for
1656 facet_install(struct ofproto *p, struct facet *facet, bool zero_stats)
1658 struct dpif_flow_stats stats;
1660 if (facet->may_install
1661 && !facet_put__(p, facet, facet->actions, facet->actions_len,
1662 zero_stats ? &stats : NULL)) {
1663 facet->installed = true;
1667 /* Ensures that the bytes in 'facet', plus 'extra_bytes', have been passed up
1668 * to the accounting hook function in the ofhooks structure. */
1670 facet_account(struct ofproto *ofproto,
1671 struct facet *facet, uint64_t extra_bytes)
1673 uint64_t total_bytes = facet->byte_count + extra_bytes;
1675 if (ofproto->ofhooks->account_flow_cb
1676 && total_bytes > facet->accounted_bytes)
1678 ofproto->ofhooks->account_flow_cb(
1679 &facet->flow, facet->tags, facet->actions, facet->actions_len,
1680 total_bytes - facet->accounted_bytes, ofproto->aux);
1681 facet->accounted_bytes = total_bytes;
1685 /* If 'rule' is installed in the datapath, uninstalls it. */
1687 facet_uninstall(struct ofproto *p, struct facet *facet)
1689 if (facet->installed) {
1690 struct odputil_keybuf keybuf;
1691 struct dpif_flow_stats stats;
1694 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1695 odp_flow_key_from_flow(&key, &facet->flow);
1697 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
1698 facet_update_stats(p, facet, &stats);
1700 facet->installed = false;
1701 facet->dp_packet_count = 0;
1702 facet->dp_byte_count = 0;
1704 assert(facet->dp_packet_count == 0);
1705 assert(facet->dp_byte_count == 0);
1709 /* Returns true if the only action for 'facet' is to send to the controller.
1710 * (We don't report NetFlow expiration messages for such facets because they
1711 * are just part of the control logic for the network, not real traffic). */
1713 facet_is_controller_flow(struct facet *facet)
1716 && facet->rule->n_actions == 1
1717 && action_outputs_to_port(&facet->rule->actions[0],
1718 htons(OFPP_CONTROLLER)));
1721 /* Folds all of 'facet''s statistics into its rule. Also updates the
1722 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
1723 * 'facet''s statistics in the datapath should have been zeroed and folded into
1724 * its packet and byte counts before this function is called. */
1726 facet_flush_stats(struct ofproto *ofproto, struct facet *facet)
1728 assert(!facet->dp_byte_count);
1729 assert(!facet->dp_packet_count);
1731 facet_push_stats(ofproto, facet);
1732 facet_account(ofproto, facet, 0);
1734 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
1735 struct ofexpired expired;
1736 expired.flow = facet->flow;
1737 expired.packet_count = facet->packet_count;
1738 expired.byte_count = facet->byte_count;
1739 expired.used = facet->used;
1740 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1743 facet->rule->packet_count += facet->packet_count;
1744 facet->rule->byte_count += facet->byte_count;
1746 /* Reset counters to prevent double counting if 'facet' ever gets
1748 facet->packet_count = 0;
1749 facet->byte_count = 0;
1750 facet->rs_packet_count = 0;
1751 facet->rs_byte_count = 0;
1752 facet->accounted_bytes = 0;
1754 netflow_flow_clear(&facet->nf_flow);
1757 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1758 * Returns it if found, otherwise a null pointer.
1760 * The returned facet might need revalidation; use facet_lookup_valid()
1761 * instead if that is important. */
1762 static struct facet *
1763 facet_find(struct ofproto *ofproto, const struct flow *flow)
1765 struct facet *facet;
1767 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
1769 if (flow_equal(flow, &facet->flow)) {
1777 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1778 * Returns it if found, otherwise a null pointer.
1780 * The returned facet is guaranteed to be valid. */
1781 static struct facet *
1782 facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
1784 struct facet *facet = facet_find(ofproto, flow);
1786 /* The facet we found might not be valid, since we could be in need of
1787 * revalidation. If it is not valid, don't return it. */
1789 && ofproto->need_revalidate
1790 && !facet_revalidate(ofproto, facet)) {
1791 COVERAGE_INC(ofproto_invalidated);
1798 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
1800 * - If the rule found is different from 'facet''s current rule, moves
1801 * 'facet' to the new rule and recompiles its actions.
1803 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
1804 * where it is and recompiles its actions anyway.
1806 * - If there is none, destroys 'facet'.
1808 * Returns true if 'facet' still exists, false if it has been destroyed. */
1810 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
1812 struct action_xlate_ctx ctx;
1813 struct ofpbuf *odp_actions;
1814 struct rule *new_rule;
1815 bool actions_changed;
1817 COVERAGE_INC(facet_revalidate);
1819 /* Determine the new rule. */
1820 new_rule = rule_lookup(ofproto, &facet->flow);
1822 /* No new rule, so delete the facet. */
1823 facet_remove(ofproto, facet);
1827 /* Calculate new ODP actions.
1829 * We do not modify any 'facet' state yet, because we might need to, e.g.,
1830 * emit a NetFlow expiration and, if so, we need to have the old state
1831 * around to properly compose it. */
1832 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
1833 odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
1834 actions_changed = (facet->actions_len != odp_actions->size
1835 || memcmp(facet->actions, odp_actions->data,
1836 facet->actions_len));
1838 /* If the ODP actions changed or the installability changed, then we need
1839 * to talk to the datapath. */
1840 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
1841 if (ctx.may_set_up_flow) {
1842 struct dpif_flow_stats stats;
1844 facet_put__(ofproto, facet,
1845 odp_actions->data, odp_actions->size, &stats);
1846 facet_update_stats(ofproto, facet, &stats);
1848 facet_uninstall(ofproto, facet);
1851 /* The datapath flow is gone or has zeroed stats, so push stats out of
1852 * 'facet' into 'rule'. */
1853 facet_flush_stats(ofproto, facet);
1856 /* Update 'facet' now that we've taken care of all the old state. */
1857 facet->tags = ctx.tags;
1858 facet->nf_flow.output_iface = ctx.nf_output_iface;
1859 facet->may_install = ctx.may_set_up_flow;
1860 if (actions_changed) {
1861 free(facet->actions);
1862 facet->actions_len = odp_actions->size;
1863 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1865 if (facet->rule != new_rule) {
1866 COVERAGE_INC(facet_changed_rule);
1867 list_remove(&facet->list_node);
1868 list_push_back(&new_rule->facets, &facet->list_node);
1869 facet->rule = new_rule;
1870 facet->used = new_rule->created;
1871 facet->rs_used = facet->used;
1874 ofpbuf_delete(odp_actions);
1880 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1883 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
1885 COVERAGE_INC(ofproto_error);
1886 ofconn_send_reply(ofconn, buf);
1891 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1893 ofconn_send_reply(ofconn, make_echo_reply(oh));
1898 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1900 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1901 struct ofp_switch_features *osf;
1903 struct ofport *port;
1905 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1906 osf->datapath_id = htonll(ofproto->datapath_id);
1907 osf->n_buffers = htonl(pktbuf_capacity());
1909 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1910 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
1911 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1912 (1u << OFPAT_SET_VLAN_VID) |
1913 (1u << OFPAT_SET_VLAN_PCP) |
1914 (1u << OFPAT_STRIP_VLAN) |
1915 (1u << OFPAT_SET_DL_SRC) |
1916 (1u << OFPAT_SET_DL_DST) |
1917 (1u << OFPAT_SET_NW_SRC) |
1918 (1u << OFPAT_SET_NW_DST) |
1919 (1u << OFPAT_SET_NW_TOS) |
1920 (1u << OFPAT_SET_TP_SRC) |
1921 (1u << OFPAT_SET_TP_DST) |
1922 (1u << OFPAT_ENQUEUE));
1924 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1925 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1928 ofconn_send_reply(ofconn, buf);
1933 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1935 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1937 struct ofp_switch_config *osc;
1941 /* Figure out flags. */
1942 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
1943 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1946 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1947 osc->flags = htons(flags);
1948 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1949 ofconn_send_reply(ofconn, buf);
1955 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1957 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1958 uint16_t flags = ntohs(osc->flags);
1960 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1961 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1962 switch (flags & OFPC_FRAG_MASK) {
1963 case OFPC_FRAG_NORMAL:
1964 dpif_set_drop_frags(ofproto->dpif, false);
1966 case OFPC_FRAG_DROP:
1967 dpif_set_drop_frags(ofproto->dpif, true);
1970 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1976 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
1981 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
1982 struct action_xlate_ctx *ctx);
1985 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
1987 const struct ofport *ofport = get_port(ctx->ofproto, port);
1990 if (ofport->opp.config & OFPPC_NO_FWD) {
1991 /* Forwarding disabled on port. */
1996 * We don't have an ofport record for this port, but it doesn't hurt to
1997 * allow forwarding to it anyhow. Maybe such a port will appear later
1998 * and we're pre-populating the flow table.
2002 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port);
2003 ctx->nf_output_iface = port;
2006 static struct rule *
2007 rule_lookup(struct ofproto *ofproto, const struct flow *flow)
2009 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
2013 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2015 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2016 uint16_t old_in_port;
2019 /* Look up a flow with 'in_port' as the input port. Then restore the
2020 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2021 * have surprising behavior). */
2022 old_in_port = ctx->flow.in_port;
2023 ctx->flow.in_port = in_port;
2024 rule = rule_lookup(ctx->ofproto, &ctx->flow);
2025 ctx->flow.in_port = old_in_port;
2027 if (ctx->resubmit_hook) {
2028 ctx->resubmit_hook(ctx, rule);
2033 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2037 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2039 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2040 MAX_RESUBMIT_RECURSION);
2045 flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, uint32_t mask,
2046 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2048 struct ofport *ofport;
2050 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
2051 uint16_t odp_port = ofport->odp_port;
2052 if (odp_port != odp_in_port && !(ofport->opp.config & mask)) {
2053 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2056 *nf_output_iface = NF_OUT_FLOOD;
2060 xlate_output_action__(struct action_xlate_ctx *ctx,
2061 uint16_t port, uint16_t max_len)
2064 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2066 ctx->nf_output_iface = NF_OUT_DROP;
2070 add_output_action(ctx, ctx->flow.in_port);
2073 xlate_table_action(ctx, ctx->flow.in_port);
2076 if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
2077 ctx->odp_actions, &ctx->tags,
2078 &ctx->nf_output_iface,
2079 ctx->ofproto->aux)) {
2080 COVERAGE_INC(ofproto_uninstallable);
2081 ctx->may_set_up_flow = false;
2085 flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD,
2086 &ctx->nf_output_iface, ctx->odp_actions);
2089 flood_packets(ctx->ofproto, ctx->flow.in_port, 0,
2090 &ctx->nf_output_iface, ctx->odp_actions);
2092 case OFPP_CONTROLLER:
2093 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2096 add_output_action(ctx, ODPP_LOCAL);
2099 odp_port = ofp_port_to_odp_port(port);
2100 if (odp_port != ctx->flow.in_port) {
2101 add_output_action(ctx, odp_port);
2106 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2107 ctx->nf_output_iface = NF_OUT_FLOOD;
2108 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2109 ctx->nf_output_iface = prev_nf_output_iface;
2110 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2111 ctx->nf_output_iface != NF_OUT_FLOOD) {
2112 ctx->nf_output_iface = NF_OUT_MULTI;
2117 xlate_output_action(struct action_xlate_ctx *ctx,
2118 const struct ofp_action_output *oao)
2120 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2123 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2124 * optimization, because we're going to add another action that sets the
2125 * priority immediately after, or because there are no actions following the
2128 remove_pop_action(struct action_xlate_ctx *ctx)
2130 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2131 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2132 ctx->last_pop_priority = -1;
2137 add_pop_action(struct action_xlate_ctx *ctx)
2139 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2140 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2141 ctx->last_pop_priority = ctx->odp_actions->size;
2146 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2147 const struct ofp_action_enqueue *oae)
2149 uint16_t ofp_port, odp_port;
2153 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2156 /* Fall back to ordinary output action. */
2157 xlate_output_action__(ctx, ntohs(oae->port), 0);
2161 /* Figure out ODP output port. */
2162 ofp_port = ntohs(oae->port);
2163 if (ofp_port != OFPP_IN_PORT) {
2164 odp_port = ofp_port_to_odp_port(ofp_port);
2166 odp_port = ctx->flow.in_port;
2169 /* Add ODP actions. */
2170 remove_pop_action(ctx);
2171 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2172 add_output_action(ctx, odp_port);
2173 add_pop_action(ctx);
2175 /* Update NetFlow output port. */
2176 if (ctx->nf_output_iface == NF_OUT_DROP) {
2177 ctx->nf_output_iface = odp_port;
2178 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2179 ctx->nf_output_iface = NF_OUT_MULTI;
2184 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2185 const struct nx_action_set_queue *nasq)
2190 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2193 /* Couldn't translate queue to a priority, so ignore. A warning
2194 * has already been logged. */
2198 remove_pop_action(ctx);
2199 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2203 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2205 ovs_be16 tci = ctx->flow.vlan_tci;
2206 if (!(tci & htons(VLAN_CFI))) {
2207 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2209 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2210 tci & ~htons(VLAN_CFI));
2214 struct xlate_reg_state {
2220 save_reg_state(const struct action_xlate_ctx *ctx,
2221 struct xlate_reg_state *state)
2223 state->vlan_tci = ctx->flow.vlan_tci;
2224 state->tun_id = ctx->flow.tun_id;
2228 update_reg_state(struct action_xlate_ctx *ctx,
2229 const struct xlate_reg_state *state)
2231 if (ctx->flow.vlan_tci != state->vlan_tci) {
2232 xlate_set_dl_tci(ctx);
2234 if (ctx->flow.tun_id != state->tun_id) {
2235 nl_msg_put_be64(ctx->odp_actions,
2236 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2241 xlate_nicira_action(struct action_xlate_ctx *ctx,
2242 const struct nx_action_header *nah)
2244 const struct nx_action_resubmit *nar;
2245 const struct nx_action_set_tunnel *nast;
2246 const struct nx_action_set_queue *nasq;
2247 const struct nx_action_multipath *nam;
2248 const struct nx_action_autopath *naa;
2249 enum nx_action_subtype subtype = ntohs(nah->subtype);
2250 const struct ofhooks *ofhooks = ctx->ofproto->ofhooks;
2251 struct xlate_reg_state state;
2252 uint16_t autopath_port;
2255 assert(nah->vendor == htonl(NX_VENDOR_ID));
2257 case NXAST_RESUBMIT:
2258 nar = (const struct nx_action_resubmit *) nah;
2259 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2262 case NXAST_SET_TUNNEL:
2263 nast = (const struct nx_action_set_tunnel *) nah;
2264 tun_id = htonll(ntohl(nast->tun_id));
2265 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2266 ctx->flow.tun_id = tun_id;
2269 case NXAST_DROP_SPOOFED_ARP:
2270 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2271 nl_msg_put_flag(ctx->odp_actions,
2272 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2276 case NXAST_SET_QUEUE:
2277 nasq = (const struct nx_action_set_queue *) nah;
2278 xlate_set_queue_action(ctx, nasq);
2281 case NXAST_POP_QUEUE:
2282 add_pop_action(ctx);
2285 case NXAST_REG_MOVE:
2286 save_reg_state(ctx, &state);
2287 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2289 update_reg_state(ctx, &state);
2292 case NXAST_REG_LOAD:
2293 save_reg_state(ctx, &state);
2294 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2296 update_reg_state(ctx, &state);
2300 /* Nothing to do. */
2303 case NXAST_SET_TUNNEL64:
2304 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2305 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2306 ctx->flow.tun_id = tun_id;
2309 case NXAST_MULTIPATH:
2310 nam = (const struct nx_action_multipath *) nah;
2311 multipath_execute(nam, &ctx->flow);
2314 case NXAST_AUTOPATH:
2315 naa = (const struct nx_action_autopath *) nah;
2316 autopath_port = (ofhooks->autopath_cb
2317 ? ofhooks->autopath_cb(&ctx->flow, ntohl(naa->id),
2318 &ctx->tags, ctx->ofproto->aux)
2320 autopath_execute(naa, &ctx->flow, autopath_port);
2323 /* If you add a new action here that modifies flow data, don't forget to
2324 * update the flow key in ctx->flow at the same time. */
2326 case NXAST_SNAT__OBSOLETE:
2328 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2334 do_xlate_actions(const union ofp_action *in, size_t n_in,
2335 struct action_xlate_ctx *ctx)
2337 struct actions_iterator iter;
2338 const union ofp_action *ia;
2339 const struct ofport *port;
2341 port = get_port(ctx->ofproto, ctx->flow.in_port);
2342 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2343 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2344 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
2345 /* Drop this flow. */
2349 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2350 enum ofp_action_type type = ntohs(ia->type);
2351 const struct ofp_action_dl_addr *oada;
2355 xlate_output_action(ctx, &ia->output);
2358 case OFPAT_SET_VLAN_VID:
2359 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2360 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2361 xlate_set_dl_tci(ctx);
2364 case OFPAT_SET_VLAN_PCP:
2365 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2366 ctx->flow.vlan_tci |= htons(
2367 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2368 xlate_set_dl_tci(ctx);
2371 case OFPAT_STRIP_VLAN:
2372 ctx->flow.vlan_tci = htons(0);
2373 xlate_set_dl_tci(ctx);
2376 case OFPAT_SET_DL_SRC:
2377 oada = ((struct ofp_action_dl_addr *) ia);
2378 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2379 oada->dl_addr, ETH_ADDR_LEN);
2380 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
2383 case OFPAT_SET_DL_DST:
2384 oada = ((struct ofp_action_dl_addr *) ia);
2385 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2386 oada->dl_addr, ETH_ADDR_LEN);
2387 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
2390 case OFPAT_SET_NW_SRC:
2391 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
2392 ia->nw_addr.nw_addr);
2393 ctx->flow.nw_src = ia->nw_addr.nw_addr;
2396 case OFPAT_SET_NW_DST:
2397 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
2398 ia->nw_addr.nw_addr);
2399 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
2402 case OFPAT_SET_NW_TOS:
2403 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
2405 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
2408 case OFPAT_SET_TP_SRC:
2409 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
2410 ia->tp_port.tp_port);
2411 ctx->flow.tp_src = ia->tp_port.tp_port;
2414 case OFPAT_SET_TP_DST:
2415 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
2416 ia->tp_port.tp_port);
2417 ctx->flow.tp_dst = ia->tp_port.tp_port;
2421 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2425 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
2429 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
2436 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
2437 struct ofproto *ofproto, const struct flow *flow,
2438 const struct ofpbuf *packet)
2440 ctx->ofproto = ofproto;
2442 ctx->packet = packet;
2443 ctx->resubmit_hook = NULL;
2444 ctx->check_special = true;
2448 ofproto_process_cfm(struct ofproto *ofproto, const struct flow *flow,
2449 const struct ofpbuf *packet)
2451 struct ofport *ofport;
2453 ofport = get_port(ofproto, flow->in_port);
2454 if (ofport && ofport->cfm) {
2455 cfm_process_heartbeat(ofport->cfm, packet);
2459 static struct ofpbuf *
2460 xlate_actions(struct action_xlate_ctx *ctx,
2461 const union ofp_action *in, size_t n_in)
2463 COVERAGE_INC(ofproto_ofp2odp);
2465 ctx->odp_actions = ofpbuf_new(512);
2467 ctx->may_set_up_flow = true;
2468 ctx->nf_output_iface = NF_OUT_DROP;
2470 ctx->last_pop_priority = -1;
2472 if (ctx->check_special && cfm_should_process_flow(&ctx->flow)) {
2474 ofproto_process_cfm(ctx->ofproto, &ctx->flow, ctx->packet);
2476 ctx->may_set_up_flow = false;
2477 } else if (ctx->check_special
2478 && ctx->ofproto->ofhooks->special_cb
2479 && !ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet,
2480 ctx->ofproto->aux)) {
2481 ctx->may_set_up_flow = false;
2483 do_xlate_actions(in, n_in, ctx);
2486 remove_pop_action(ctx);
2488 /* Check with in-band control to see if we're allowed to set up this
2490 if (!connmgr_may_set_up_flow(ctx->ofproto->connmgr, &ctx->flow,
2491 ctx->odp_actions->data,
2492 ctx->odp_actions->size)) {
2493 ctx->may_set_up_flow = false;
2496 return ctx->odp_actions;
2499 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2500 * error message code (composed with ofp_mkerr()) for the caller to propagate
2501 * upward. Otherwise, returns 0.
2503 * The log message mentions 'msg_type'. */
2505 reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
2507 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2508 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
2509 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
2510 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
2513 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2520 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2522 struct ofproto *p = ofconn_get_ofproto(ofconn);
2523 struct ofp_packet_out *opo;
2524 struct ofpbuf payload, *buffer;
2525 union ofp_action *ofp_actions;
2526 struct action_xlate_ctx ctx;
2527 struct ofpbuf *odp_actions;
2528 struct ofpbuf request;
2530 size_t n_ofp_actions;
2534 COVERAGE_INC(ofproto_packet_out);
2536 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
2541 /* Get ofp_packet_out. */
2542 ofpbuf_use_const(&request, oh, ntohs(oh->length));
2543 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
2546 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
2547 &ofp_actions, &n_ofp_actions);
2553 if (opo->buffer_id != htonl(UINT32_MAX)) {
2554 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
2556 if (error || !buffer) {
2565 /* Extract flow, check actions. */
2566 flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
2568 error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
2574 action_xlate_ctx_init(&ctx, p, &flow, &payload);
2575 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
2576 dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
2577 ofpbuf_delete(odp_actions);
2580 ofpbuf_delete(buffer);
2585 update_port_config(struct ofproto *p, struct ofport *port,
2586 uint32_t config, uint32_t mask)
2588 mask &= config ^ port->opp.config;
2589 if (mask & OFPPC_PORT_DOWN) {
2590 if (config & OFPPC_PORT_DOWN) {
2591 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2593 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2596 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
2597 OFPPC_NO_FWD | OFPPC_NO_FLOOD)
2598 if (mask & REVALIDATE_BITS) {
2599 COVERAGE_INC(ofproto_costly_flags);
2600 port->opp.config ^= mask & REVALIDATE_BITS;
2601 p->need_revalidate = true;
2603 #undef REVALIDATE_BITS
2604 if (mask & OFPPC_NO_PACKET_IN) {
2605 port->opp.config ^= OFPPC_NO_PACKET_IN;
2610 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2612 struct ofproto *p = ofconn_get_ofproto(ofconn);
2613 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
2614 struct ofport *port;
2617 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
2622 port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
2624 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2625 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2626 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2628 update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
2629 if (opm->advertise) {
2630 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2636 static struct ofpbuf *
2637 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
2639 struct ofp_stats_reply *osr;
2642 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2643 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2645 osr->flags = htons(0);
2649 static struct ofpbuf *
2650 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
2652 const struct ofp_stats_request *osr
2653 = (const struct ofp_stats_request *) request;
2654 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
2658 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
2659 struct ofpbuf **msgp)
2661 struct ofpbuf *msg = *msgp;
2662 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2663 if (nbytes + msg->size > UINT16_MAX) {
2664 struct ofp_stats_reply *reply = msg->data;
2665 reply->flags = htons(OFPSF_REPLY_MORE);
2666 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
2667 ofconn_send_reply(ofconn, msg);
2669 return ofpbuf_put_uninit(*msgp, nbytes);
2672 static struct ofpbuf *
2673 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
2675 struct nicira_stats_msg *nsm;
2678 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
2679 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
2680 nsm->type = htons(OFPST_VENDOR);
2681 nsm->flags = htons(0);
2682 nsm->vendor = htonl(NX_VENDOR_ID);
2683 nsm->subtype = subtype;
2687 static struct ofpbuf *
2688 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
2690 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
2694 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
2695 struct ofpbuf **msgp)
2697 struct ofpbuf *msg = *msgp;
2698 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
2699 if (nbytes + msg->size > UINT16_MAX) {
2700 struct nicira_stats_msg *reply = msg->data;
2701 reply->flags = htons(OFPSF_REPLY_MORE);
2702 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
2703 ofconn_send_reply(ofconn, msg);
2705 ofpbuf_prealloc_tailroom(*msgp, nbytes);
2709 handle_desc_stats_request(struct ofconn *ofconn,
2710 const struct ofp_header *request)
2712 struct ofproto *p = ofconn_get_ofproto(ofconn);
2713 struct ofp_desc_stats *ods;
2716 msg = start_ofp_stats_reply(request, sizeof *ods);
2717 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
2718 memset(ods, 0, sizeof *ods);
2719 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
2720 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
2721 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
2722 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
2723 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2724 ofconn_send_reply(ofconn, msg);
2730 handle_table_stats_request(struct ofconn *ofconn,
2731 const struct ofp_header *request)
2733 struct ofproto *p = ofconn_get_ofproto(ofconn);
2734 struct ofp_table_stats *ots;
2737 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
2739 /* Classifier table. */
2740 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
2741 memset(ots, 0, sizeof *ots);
2742 strcpy(ots->name, "classifier");
2743 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
2744 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
2745 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
2746 ots->active_count = htonl(classifier_count(&p->cls));
2747 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
2748 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
2750 ofconn_send_reply(ofconn, msg);
2755 append_port_stat(struct ofport *port, struct ofconn *ofconn,
2756 struct ofpbuf **msgp)
2758 struct netdev_stats stats;
2759 struct ofp_port_stats *ops;
2761 /* Intentionally ignore return value, since errors will set
2762 * 'stats' to all-1s, which is correct for OpenFlow, and
2763 * netdev_get_stats() will log errors. */
2764 netdev_get_stats(port->netdev, &stats);
2766 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
2767 ops->port_no = htons(port->opp.port_no);
2768 memset(ops->pad, 0, sizeof ops->pad);
2769 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
2770 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
2771 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
2772 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
2773 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
2774 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
2775 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
2776 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
2777 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
2778 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
2779 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
2780 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
2784 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2786 struct ofproto *p = ofconn_get_ofproto(ofconn);
2787 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
2788 struct ofp_port_stats *ops;
2790 struct ofport *port;
2792 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
2793 if (psr->port_no != htons(OFPP_NONE)) {
2794 port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
2796 append_port_stat(port, ofconn, &msg);
2799 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2800 append_port_stat(port, ofconn, &msg);
2804 ofconn_send_reply(ofconn, msg);
2809 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
2811 long long int msecs = time_msec() - start;
2812 *sec = msecs / 1000;
2813 *nsec = (msecs % 1000) * (1000 * 1000);
2817 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
2821 calc_flow_duration__(start, &sec, &nsec);
2822 *sec_be = htonl(sec);
2823 *nsec_be = htonl(nsec);
2827 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
2828 ovs_be16 out_port, struct ofpbuf **replyp)
2830 struct ofp_flow_stats *ofs;
2831 uint64_t packet_count, byte_count;
2833 size_t act_len, len;
2835 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2839 act_len = sizeof *rule->actions * rule->n_actions;
2840 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2842 rule_get_stats(rule, &packet_count, &byte_count);
2844 ofs = append_ofp_stats_reply(len, ofconn, replyp);
2845 ofs->length = htons(len);
2848 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
2849 &ofs->match, rule->flow_cookie, &cookie);
2850 put_32aligned_be64(&ofs->cookie, cookie);
2851 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
2852 ofs->priority = htons(rule->cr.priority);
2853 ofs->idle_timeout = htons(rule->idle_timeout);
2854 ofs->hard_timeout = htons(rule->hard_timeout);
2855 memset(ofs->pad2, 0, sizeof ofs->pad2);
2856 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
2857 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
2858 if (rule->n_actions > 0) {
2859 memcpy(ofs->actions, rule->actions, act_len);
2864 is_valid_table(uint8_t table_id)
2866 if (table_id == 0 || table_id == 0xff) {
2869 /* It would probably be better to reply with an error but there doesn't
2870 * seem to be any appropriate value, so that might just be
2872 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
2879 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2881 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
2882 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2883 struct ofpbuf *reply;
2885 COVERAGE_INC(ofproto_flows_req);
2886 reply = start_ofp_stats_reply(oh, 1024);
2887 if (is_valid_table(fsr->table_id)) {
2888 struct cls_cursor cursor;
2889 struct cls_rule target;
2892 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
2894 cls_cursor_init(&cursor, &ofproto->cls, &target);
2895 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2896 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
2899 ofconn_send_reply(ofconn, reply);
2905 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
2906 ovs_be16 out_port, struct ofpbuf **replyp)
2908 struct nx_flow_stats *nfs;
2909 uint64_t packet_count, byte_count;
2910 size_t act_len, start_len;
2911 struct ofpbuf *reply;
2913 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2917 rule_get_stats(rule, &packet_count, &byte_count);
2919 act_len = sizeof *rule->actions * rule->n_actions;
2921 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
2922 start_len = (*replyp)->size;
2925 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
2928 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
2929 nfs->cookie = rule->flow_cookie;
2930 nfs->priority = htons(rule->cr.priority);
2931 nfs->idle_timeout = htons(rule->idle_timeout);
2932 nfs->hard_timeout = htons(rule->hard_timeout);
2933 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
2934 memset(nfs->pad2, 0, sizeof nfs->pad2);
2935 nfs->packet_count = htonll(packet_count);
2936 nfs->byte_count = htonll(byte_count);
2937 if (rule->n_actions > 0) {
2938 ofpbuf_put(reply, rule->actions, act_len);
2940 nfs->length = htons(reply->size - start_len);
2944 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
2946 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2947 struct nx_flow_stats_request *nfsr;
2948 struct cls_rule target;
2949 struct ofpbuf *reply;
2953 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2955 /* Dissect the message. */
2956 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2957 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
2962 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2965 COVERAGE_INC(ofproto_flows_req);
2966 reply = start_nxstats_reply(&nfsr->nsm, 1024);
2967 if (is_valid_table(nfsr->table_id)) {
2968 struct cls_cursor cursor;
2971 cls_cursor_init(&cursor, &ofproto->cls, &target);
2972 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2973 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
2976 ofconn_send_reply(ofconn, reply);
2982 flow_stats_ds(struct rule *rule, struct ds *results)
2984 uint64_t packet_count, byte_count;
2985 size_t act_len = sizeof *rule->actions * rule->n_actions;
2987 rule_get_stats(rule, &packet_count, &byte_count);
2989 ds_put_format(results, "duration=%llds, ",
2990 (time_msec() - rule->created) / 1000);
2991 ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0);
2992 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2993 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2994 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2995 cls_rule_format(&rule->cr, results);
2996 ds_put_char(results, ',');
2998 ofp_print_actions(results, &rule->actions->header, act_len);
3000 ds_put_cstr(results, "drop");
3002 ds_put_cstr(results, "\n");
3005 /* Adds a pretty-printed description of all flows to 'results', including
3006 * hidden flows (e.g., set up by in-band control). */
3008 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3010 struct cls_cursor cursor;
3013 cls_cursor_init(&cursor, &p->cls, NULL);
3014 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3015 flow_stats_ds(rule, results);
3020 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
3021 ovs_be16 out_port, uint8_t table_id,
3022 struct ofp_aggregate_stats_reply *oasr)
3024 uint64_t total_packets = 0;
3025 uint64_t total_bytes = 0;
3028 COVERAGE_INC(ofproto_agg_request);
3030 if (is_valid_table(table_id)) {
3031 struct cls_cursor cursor;
3034 cls_cursor_init(&cursor, &ofproto->cls, target);
3035 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3036 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
3037 uint64_t packet_count;
3038 uint64_t byte_count;
3040 rule_get_stats(rule, &packet_count, &byte_count);
3042 total_packets += packet_count;
3043 total_bytes += byte_count;
3049 oasr->flow_count = htonl(n_flows);
3050 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
3051 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
3052 memset(oasr->pad, 0, sizeof oasr->pad);
3056 handle_aggregate_stats_request(struct ofconn *ofconn,
3057 const struct ofp_header *oh)
3059 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
3060 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3061 struct ofp_aggregate_stats_reply *reply;
3062 struct cls_rule target;
3065 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
3068 msg = start_ofp_stats_reply(oh, sizeof *reply);
3069 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
3070 query_aggregate_stats(ofproto, &target, request->out_port,
3071 request->table_id, reply);
3072 ofconn_send_reply(ofconn, msg);
3077 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
3079 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3080 struct nx_aggregate_stats_request *request;
3081 struct ofp_aggregate_stats_reply *reply;
3082 struct cls_rule target;
3087 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3089 /* Dissect the message. */
3090 request = ofpbuf_pull(&b, sizeof *request);
3091 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
3096 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3100 COVERAGE_INC(ofproto_flows_req);
3101 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
3102 reply = ofpbuf_put_uninit(buf, sizeof *reply);
3103 query_aggregate_stats(ofproto, &target, request->out_port,
3104 request->table_id, reply);
3105 ofconn_send_reply(ofconn, buf);
3110 struct queue_stats_cbdata {
3111 struct ofconn *ofconn;
3112 struct ofport *ofport;
3117 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3118 const struct netdev_queue_stats *stats)
3120 struct ofp_queue_stats *reply;
3122 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
3123 reply->port_no = htons(cbdata->ofport->opp.port_no);
3124 memset(reply->pad, 0, sizeof reply->pad);
3125 reply->queue_id = htonl(queue_id);
3126 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
3127 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
3128 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
3132 handle_queue_stats_dump_cb(uint32_t queue_id,
3133 struct netdev_queue_stats *stats,
3136 struct queue_stats_cbdata *cbdata = cbdata_;
3138 put_queue_stats(cbdata, queue_id, stats);
3142 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3143 struct queue_stats_cbdata *cbdata)
3145 cbdata->ofport = port;
3146 if (queue_id == OFPQ_ALL) {
3147 netdev_dump_queue_stats(port->netdev,
3148 handle_queue_stats_dump_cb, cbdata);
3150 struct netdev_queue_stats stats;
3152 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3153 put_queue_stats(cbdata, queue_id, &stats);
3159 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
3161 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3162 const struct ofp_queue_stats_request *qsr;
3163 struct queue_stats_cbdata cbdata;
3164 struct ofport *port;
3165 unsigned int port_no;
3168 qsr = ofputil_stats_body(oh);
3170 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3173 COVERAGE_INC(ofproto_queue_req);
3175 cbdata.ofconn = ofconn;
3176 cbdata.msg = start_ofp_stats_reply(oh, 128);
3178 port_no = ntohs(qsr->port_no);
3179 queue_id = ntohl(qsr->queue_id);
3180 if (port_no == OFPP_ALL) {
3181 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3182 handle_queue_stats_for_port(port, queue_id, &cbdata);
3184 } else if (port_no < ofproto->max_ports) {
3185 port = get_port(ofproto, ofp_port_to_odp_port(port_no));
3187 handle_queue_stats_for_port(port, queue_id, &cbdata);
3190 ofpbuf_delete(cbdata.msg);
3191 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
3193 ofconn_send_reply(ofconn, cbdata.msg);
3198 /* Updates 'facet''s used time. Caller is responsible for calling
3199 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3201 facet_update_time(struct ofproto *ofproto, struct facet *facet,
3204 if (used > facet->used) {
3206 if (used > facet->rule->used) {
3207 facet->rule->used = used;
3209 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3213 /* Folds the statistics from 'stats' into the counters in 'facet'.
3215 * Because of the meaning of a facet's counters, it only makes sense to do this
3216 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3217 * packet that was sent by hand or if it represents statistics that have been
3218 * cleared out of the datapath. */
3220 facet_update_stats(struct ofproto *ofproto, struct facet *facet,
3221 const struct dpif_flow_stats *stats)
3223 if (stats->n_packets || stats->used > facet->used) {
3224 facet_update_time(ofproto, facet, stats->used);
3225 facet->packet_count += stats->n_packets;
3226 facet->byte_count += stats->n_bytes;
3227 facet_push_stats(ofproto, facet);
3228 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3233 facet_push_stats(struct ofproto *ofproto, struct facet *facet)
3235 uint64_t rs_packets, rs_bytes;
3237 assert(facet->packet_count >= facet->rs_packet_count);
3238 assert(facet->byte_count >= facet->rs_byte_count);
3239 assert(facet->used >= facet->rs_used);
3241 rs_packets = facet->packet_count - facet->rs_packet_count;
3242 rs_bytes = facet->byte_count - facet->rs_byte_count;
3244 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3245 facet->rs_packet_count = facet->packet_count;
3246 facet->rs_byte_count = facet->byte_count;
3247 facet->rs_used = facet->used;
3249 flow_push_stats(ofproto, facet->rule, &facet->flow,
3250 rs_packets, rs_bytes, facet->used);
3254 struct ofproto_push {
3255 struct action_xlate_ctx ctx;
3262 push_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
3264 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3267 rule->packet_count += push->packets;
3268 rule->byte_count += push->bytes;
3269 rule->used = MAX(push->used, rule->used);
3273 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3274 * 'rule''s actions. */
3276 flow_push_stats(struct ofproto *ofproto, const struct rule *rule,
3277 struct flow *flow, uint64_t packets, uint64_t bytes,
3280 struct ofproto_push push;
3282 push.packets = packets;
3286 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3287 push.ctx.resubmit_hook = push_resubmit;
3288 ofpbuf_delete(xlate_actions(&push.ctx, rule->actions, rule->n_actions));
3291 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3292 * in which no matching flow already exists in the flow table.
3294 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3295 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
3296 * OpenFlow error code as encoded by ofp_mkerr() on failure.
3298 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3301 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
3303 struct ofproto *p = ofconn_get_ofproto(ofconn);
3304 struct ofpbuf *packet;
3309 if (fm->flags & OFPFF_CHECK_OVERLAP
3310 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
3311 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
3315 if (fm->buffer_id != UINT32_MAX) {
3316 error = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id,
3320 in_port = UINT16_MAX;
3323 rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
3324 fm->idle_timeout, fm->hard_timeout, fm->cookie,
3325 fm->flags & OFPFF_SEND_FLOW_REM);
3326 rule_insert(p, rule);
3328 rule_execute(p, rule, in_port, packet);
3333 static struct rule *
3334 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
3336 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
3340 send_buffered_packet(struct ofconn *ofconn,
3341 struct rule *rule, uint32_t buffer_id)
3343 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3344 struct ofpbuf *packet;
3348 if (buffer_id == UINT32_MAX) {
3352 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
3357 rule_execute(ofproto, rule, in_port, packet);
3362 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3364 struct modify_flows_cbdata {
3365 struct ofproto *ofproto;
3366 const struct flow_mod *fm;
3370 static int modify_flow(struct ofproto *, const struct flow_mod *,
3373 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
3374 * encoded by ofp_mkerr() on failure.
3376 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3379 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
3381 struct ofproto *p = ofconn_get_ofproto(ofconn);
3382 struct rule *match = NULL;
3383 struct cls_cursor cursor;
3386 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3387 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3388 if (!rule_is_hidden(rule)) {
3390 modify_flow(p, fm, rule);
3395 /* This credits the packet to whichever flow happened to match last.
3396 * That's weird. Maybe we should do a lookup for the flow that
3397 * actually matches the packet? Who knows. */
3398 send_buffered_packet(ofconn, match, fm->buffer_id);
3401 return add_flow(ofconn, fm);
3405 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3406 * code as encoded by ofp_mkerr() on failure.
3408 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3411 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
3413 struct ofproto *p = ofconn_get_ofproto(ofconn);
3414 struct rule *rule = find_flow_strict(p, fm);
3415 if (rule && !rule_is_hidden(rule)) {
3416 modify_flow(p, fm, rule);
3417 return send_buffered_packet(ofconn, rule, fm->buffer_id);
3419 return add_flow(ofconn, fm);
3423 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
3424 * been identified as a flow in 'p''s flow table to be modified, by changing
3425 * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
3426 * ofp_action[] structures). */
3428 modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
3430 size_t actions_len = fm->n_actions * sizeof *rule->actions;
3432 rule->flow_cookie = fm->cookie;
3434 /* If the actions are the same, do nothing. */
3435 if (fm->n_actions == rule->n_actions
3437 || !memcmp(fm->actions, rule->actions, actions_len))) {
3441 /* Replace actions. */
3442 free(rule->actions);
3443 rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
3444 rule->n_actions = fm->n_actions;
3446 p->need_revalidate = true;
3451 /* OFPFC_DELETE implementation. */
3453 static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
3455 /* Implements OFPFC_DELETE. */
3457 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
3459 struct rule *rule, *next_rule;
3460 struct cls_cursor cursor;
3462 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3463 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3464 delete_flow(p, rule, htons(fm->out_port));
3468 /* Implements OFPFC_DELETE_STRICT. */
3470 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
3472 struct rule *rule = find_flow_strict(p, fm);
3474 delete_flow(p, rule, htons(fm->out_port));
3478 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
3479 * been identified as a flow to delete from 'p''s flow table, by deleting the
3480 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
3483 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
3484 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
3485 * specified 'out_port'. */
3487 delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
3489 if (rule_is_hidden(rule)) {
3493 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
3497 rule_send_removed(p, rule, OFPRR_DELETE);
3498 rule_remove(p, rule);
3502 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3504 struct ofproto *p = ofconn_get_ofproto(ofconn);
3508 error = reject_slave_controller(ofconn, "flow_mod");
3513 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
3518 /* We do not support the emergency flow cache. It will hopefully get
3519 * dropped from OpenFlow in the near future. */
3520 if (fm.flags & OFPFF_EMERG) {
3521 /* There isn't a good fit for an error code, so just state that the
3522 * flow table is full. */
3523 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
3526 error = validate_actions(fm.actions, fm.n_actions,
3527 &fm.cr.flow, p->max_ports);
3532 switch (fm.command) {
3534 return add_flow(ofconn, &fm);
3537 return modify_flows_loose(ofconn, &fm);
3539 case OFPFC_MODIFY_STRICT:
3540 return modify_flow_strict(ofconn, &fm);
3543 delete_flows_loose(p, &fm);
3546 case OFPFC_DELETE_STRICT:
3547 delete_flow_strict(p, &fm);
3551 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
3556 handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
3558 const struct nxt_tun_id_cookie *msg
3559 = (const struct nxt_tun_id_cookie *) oh;
3560 enum nx_flow_format flow_format;
3562 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
3563 ofconn_set_flow_format(ofconn, flow_format);
3569 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3571 struct nx_role_request *nrr = (struct nx_role_request *) oh;
3572 struct nx_role_request *reply;
3576 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
3577 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
3578 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3581 role = ntohl(nrr->role);
3582 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
3583 && role != NX_ROLE_SLAVE) {
3584 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
3586 /* There's no good error code for this. */
3587 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
3590 ofconn_set_role(ofconn, role);
3592 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
3593 reply->role = htonl(role);
3594 ofconn_send_reply(ofconn, buf);
3600 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
3602 const struct nxt_set_flow_format *msg
3603 = (const struct nxt_set_flow_format *) oh;
3606 format = ntohl(msg->format);
3607 if (format == NXFF_OPENFLOW10
3608 || format == NXFF_TUN_ID_FROM_COOKIE
3609 || format == NXFF_NXM) {
3610 ofconn_set_flow_format(ofconn, format);
3613 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3618 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
3620 struct ofp_header *ob;
3623 /* Currently, everything executes synchronously, so we can just
3624 * immediately send the barrier reply. */
3625 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
3626 ofconn_send_reply(ofconn, buf);
3631 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
3633 const struct ofp_header *oh = msg->data;
3634 const struct ofputil_msg_type *type;
3637 error = ofputil_decode_msg_type(oh, &type);
3642 switch (ofputil_msg_type_code(type)) {
3643 /* OpenFlow requests. */
3644 case OFPUTIL_OFPT_ECHO_REQUEST:
3645 return handle_echo_request(ofconn, oh);
3647 case OFPUTIL_OFPT_FEATURES_REQUEST:
3648 return handle_features_request(ofconn, oh);
3650 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
3651 return handle_get_config_request(ofconn, oh);
3653 case OFPUTIL_OFPT_SET_CONFIG:
3654 return handle_set_config(ofconn, msg->data);
3656 case OFPUTIL_OFPT_PACKET_OUT:
3657 return handle_packet_out(ofconn, oh);
3659 case OFPUTIL_OFPT_PORT_MOD:
3660 return handle_port_mod(ofconn, oh);
3662 case OFPUTIL_OFPT_FLOW_MOD:
3663 return handle_flow_mod(ofconn, oh);
3665 case OFPUTIL_OFPT_BARRIER_REQUEST:
3666 return handle_barrier_request(ofconn, oh);
3668 /* OpenFlow replies. */
3669 case OFPUTIL_OFPT_ECHO_REPLY:
3672 /* Nicira extension requests. */
3673 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
3674 return handle_tun_id_from_cookie(ofconn, oh);
3676 case OFPUTIL_NXT_ROLE_REQUEST:
3677 return handle_role_request(ofconn, oh);
3679 case OFPUTIL_NXT_SET_FLOW_FORMAT:
3680 return handle_nxt_set_flow_format(ofconn, oh);
3682 case OFPUTIL_NXT_FLOW_MOD:
3683 return handle_flow_mod(ofconn, oh);
3685 /* OpenFlow statistics requests. */
3686 case OFPUTIL_OFPST_DESC_REQUEST:
3687 return handle_desc_stats_request(ofconn, oh);
3689 case OFPUTIL_OFPST_FLOW_REQUEST:
3690 return handle_flow_stats_request(ofconn, oh);
3692 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
3693 return handle_aggregate_stats_request(ofconn, oh);
3695 case OFPUTIL_OFPST_TABLE_REQUEST:
3696 return handle_table_stats_request(ofconn, oh);
3698 case OFPUTIL_OFPST_PORT_REQUEST:
3699 return handle_port_stats_request(ofconn, oh);
3701 case OFPUTIL_OFPST_QUEUE_REQUEST:
3702 return handle_queue_stats_request(ofconn, oh);
3704 /* Nicira extension statistics requests. */
3705 case OFPUTIL_NXST_FLOW_REQUEST:
3706 return handle_nxst_flow(ofconn, oh);
3708 case OFPUTIL_NXST_AGGREGATE_REQUEST:
3709 return handle_nxst_aggregate(ofconn, oh);
3711 case OFPUTIL_INVALID:
3712 case OFPUTIL_OFPT_HELLO:
3713 case OFPUTIL_OFPT_ERROR:
3714 case OFPUTIL_OFPT_FEATURES_REPLY:
3715 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
3716 case OFPUTIL_OFPT_PACKET_IN:
3717 case OFPUTIL_OFPT_FLOW_REMOVED:
3718 case OFPUTIL_OFPT_PORT_STATUS:
3719 case OFPUTIL_OFPT_BARRIER_REPLY:
3720 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
3721 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
3722 case OFPUTIL_OFPST_DESC_REPLY:
3723 case OFPUTIL_OFPST_FLOW_REPLY:
3724 case OFPUTIL_OFPST_QUEUE_REPLY:
3725 case OFPUTIL_OFPST_PORT_REPLY:
3726 case OFPUTIL_OFPST_TABLE_REPLY:
3727 case OFPUTIL_OFPST_AGGREGATE_REPLY:
3728 case OFPUTIL_NXT_ROLE_REPLY:
3729 case OFPUTIL_NXT_FLOW_REMOVED:
3730 case OFPUTIL_NXST_FLOW_REPLY:
3731 case OFPUTIL_NXST_AGGREGATE_REPLY:
3733 if (VLOG_IS_WARN_ENABLED()) {
3734 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3735 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3738 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
3739 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
3741 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3747 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
3749 int error = handle_openflow__(ofconn, ofp_msg);
3751 send_error_oh(ofconn, ofp_msg->data, error);
3753 COVERAGE_INC(ofproto_recv_openflow);
3757 handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3759 struct facet *facet;
3762 /* Obtain in_port and tun_id, at least. */
3763 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3765 /* Set header pointers in 'flow'. */
3766 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
3768 if (cfm_should_process_flow(&flow)) {
3769 ofproto_process_cfm(p, &flow, upcall->packet);
3770 ofpbuf_delete(upcall->packet);
3772 } else if (p->ofhooks->special_cb
3773 && !p->ofhooks->special_cb(&flow, upcall->packet, p->aux)) {
3774 ofpbuf_delete(upcall->packet);
3778 /* Check with in-band control to see if this packet should be sent
3779 * to the local port regardless of the flow table. */
3780 if (connmgr_msg_in_hook(p->connmgr, &flow, upcall->packet)) {
3781 ofproto_send_packet(p, ODPP_LOCAL, 0, upcall->packet);
3784 facet = facet_lookup_valid(p, &flow);
3786 struct rule *rule = rule_lookup(p, &flow);
3788 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3789 struct ofport *port = get_port(p, flow.in_port);
3791 if (port->opp.config & OFPPC_NO_PACKET_IN) {
3792 COVERAGE_INC(ofproto_no_packet_in);
3793 /* XXX install 'drop' flow entry */
3794 ofpbuf_delete(upcall->packet);
3798 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
3802 COVERAGE_INC(ofproto_packet_in);
3803 send_packet_in(p, upcall, &flow, false);
3807 facet = facet_create(p, rule, &flow, upcall->packet);
3808 } else if (!facet->may_install) {
3809 /* The facet is not installable, that is, we need to process every
3810 * packet, so process the current packet's actions into 'facet'. */
3811 facet_make_actions(p, facet, upcall->packet);
3814 if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) {
3816 * Extra-special case for fail-open mode.
3818 * We are in fail-open mode and the packet matched the fail-open rule,
3819 * but we are connected to a controller too. We should send the packet
3820 * up to the controller in the hope that it will try to set up a flow
3821 * and thereby allow us to exit fail-open.
3823 * See the top-level comment in fail-open.c for more information.
3825 send_packet_in(p, upcall, &flow, true);
3828 facet_execute(p, facet, upcall->packet);
3829 facet_install(p, facet, false);
3833 handle_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3837 switch (upcall->type) {
3838 case DPIF_UC_ACTION:
3839 COVERAGE_INC(ofproto_ctlr_action);
3840 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3841 send_packet_in(p, upcall, &flow, false);
3844 case DPIF_UC_SAMPLE:
3846 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3847 ofproto_sflow_received(p->sflow, upcall, &flow);
3849 ofpbuf_delete(upcall->packet);
3853 handle_miss_upcall(p, upcall);
3856 case DPIF_N_UC_TYPES:
3858 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3863 /* Flow expiration. */
3865 static int ofproto_dp_max_idle(const struct ofproto *);
3866 static void ofproto_update_stats(struct ofproto *);
3867 static void rule_expire(struct ofproto *, struct rule *);
3868 static void ofproto_expire_facets(struct ofproto *, int dp_max_idle);
3870 /* This function is called periodically by ofproto_run(). Its job is to
3871 * collect updates for the flows that have been installed into the datapath,
3872 * most importantly when they last were used, and then use that information to
3873 * expire flows that have not been used recently.
3875 * Returns the number of milliseconds after which it should be called again. */
3877 ofproto_expire(struct ofproto *ofproto)
3879 struct rule *rule, *next_rule;
3880 struct cls_cursor cursor;
3883 /* Update stats for each flow in the datapath. */
3884 ofproto_update_stats(ofproto);
3886 /* Expire facets that have been idle too long. */
3887 dp_max_idle = ofproto_dp_max_idle(ofproto);
3888 ofproto_expire_facets(ofproto, dp_max_idle);
3890 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3891 cls_cursor_init(&cursor, &ofproto->cls, NULL);
3892 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3893 rule_expire(ofproto, rule);
3896 /* Let the hook know that we're at a stable point: all outstanding data
3897 * in existing flows has been accounted to the account_cb. Thus, the
3898 * hook can now reasonably do operations that depend on having accurate
3899 * flow volume accounting (currently, that's just bond rebalancing). */
3900 if (ofproto->ofhooks->account_checkpoint_cb) {
3901 ofproto->ofhooks->account_checkpoint_cb(ofproto->aux);
3904 return MIN(dp_max_idle, 1000);
3907 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3909 * This function also pushes statistics updates to rules which each facet
3910 * resubmits into. Generally these statistics will be accurate. However, if a
3911 * facet changes the rule it resubmits into at some time in between
3912 * ofproto_update_stats() runs, it is possible that statistics accrued to the
3913 * old rule will be incorrectly attributed to the new rule. This could be
3914 * avoided by calling ofproto_update_stats() whenever rules are created or
3915 * deleted. However, the performance impact of making so many calls to the
3916 * datapath do not justify the benefit of having perfectly accurate statistics.
3919 ofproto_update_stats(struct ofproto *p)
3921 const struct dpif_flow_stats *stats;
3922 struct dpif_flow_dump dump;
3923 const struct nlattr *key;
3926 dpif_flow_dump_start(&dump, p->dpif);
3927 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3928 struct facet *facet;
3931 if (odp_flow_key_to_flow(key, key_len, &flow)) {
3935 odp_flow_key_format(key, key_len, &s);
3936 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
3942 facet = facet_find(p, &flow);
3944 if (facet && facet->installed) {
3946 if (stats->n_packets >= facet->dp_packet_count) {
3947 facet->packet_count += stats->n_packets - facet->dp_packet_count;
3949 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3952 if (stats->n_bytes >= facet->dp_byte_count) {
3953 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
3955 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3958 facet->dp_packet_count = stats->n_packets;
3959 facet->dp_byte_count = stats->n_bytes;
3961 facet_update_time(p, facet, stats->used);
3962 facet_account(p, facet, stats->n_bytes);
3963 facet_push_stats(p, facet);
3965 /* There's a flow in the datapath that we know nothing about.
3967 COVERAGE_INC(ofproto_unexpected_rule);
3968 dpif_flow_del(p->dpif, key, key_len, NULL);
3971 dpif_flow_dump_done(&dump);
3974 /* Calculates and returns the number of milliseconds of idle time after which
3975 * facets should expire from the datapath and we should fold their statistics
3976 * into their parent rules in userspace. */
3978 ofproto_dp_max_idle(const struct ofproto *ofproto)
3981 * Idle time histogram.
3983 * Most of the time a switch has a relatively small number of facets. When
3984 * this is the case we might as well keep statistics for all of them in
3985 * userspace and to cache them in the kernel datapath for performance as
3988 * As the number of facets increases, the memory required to maintain
3989 * statistics about them in userspace and in the kernel becomes
3990 * significant. However, with a large number of facets it is likely that
3991 * only a few of them are "heavy hitters" that consume a large amount of
3992 * bandwidth. At this point, only heavy hitters are worth caching in the
3993 * kernel and maintaining in userspaces; other facets we can discard.
3995 * The technique used to compute the idle time is to build a histogram with
3996 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
3997 * that is installed in the kernel gets dropped in the appropriate bucket.
3998 * After the histogram has been built, we compute the cutoff so that only
3999 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
4000 * cached. At least the most-recently-used bucket of facets is kept, so
4001 * actually an arbitrary number of facets can be kept in any given
4002 * expiration run (though the next run will delete most of those unless
4003 * they receive additional data).
4005 * This requires a second pass through the facets, in addition to the pass
4006 * made by ofproto_update_stats(), because the former function never looks
4007 * at uninstallable facets.
4009 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
4010 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
4011 int buckets[N_BUCKETS] = { 0 };
4012 struct facet *facet;
4017 total = hmap_count(&ofproto->facets);
4018 if (total <= 1000) {
4019 return N_BUCKETS * BUCKET_WIDTH;
4022 /* Build histogram. */
4024 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
4025 long long int idle = now - facet->used;
4026 int bucket = (idle <= 0 ? 0
4027 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4028 : (unsigned int) idle / BUCKET_WIDTH);
4032 /* Find the first bucket whose flows should be expired. */
4033 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
4034 if (buckets[bucket]) {
4037 subtotal += buckets[bucket++];
4038 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
4043 if (VLOG_IS_DBG_ENABLED()) {
4047 ds_put_cstr(&s, "keep");
4048 for (i = 0; i < N_BUCKETS; i++) {
4050 ds_put_cstr(&s, ", drop");
4053 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4056 VLOG_INFO("%s: %s (msec:count)",
4057 dpif_name(ofproto->dpif), ds_cstr(&s));
4061 return bucket * BUCKET_WIDTH;
4065 facet_active_timeout(struct ofproto *ofproto, struct facet *facet)
4067 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
4068 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
4069 struct ofexpired expired;
4071 if (facet->installed) {
4072 struct dpif_flow_stats stats;
4074 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
4076 facet_update_stats(ofproto, facet, &stats);
4079 expired.flow = facet->flow;
4080 expired.packet_count = facet->packet_count;
4081 expired.byte_count = facet->byte_count;
4082 expired.used = facet->used;
4083 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4088 ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle)
4090 long long int cutoff = time_msec() - dp_max_idle;
4091 struct facet *facet, *next_facet;
4093 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
4094 facet_active_timeout(ofproto, facet);
4095 if (facet->used < cutoff) {
4096 facet_remove(ofproto, facet);
4101 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4102 * then delete it entirely. */
4104 rule_expire(struct ofproto *ofproto, struct rule *rule)
4106 struct facet *facet, *next_facet;
4110 /* Has 'rule' expired? */
4112 if (rule->hard_timeout
4113 && now > rule->created + rule->hard_timeout * 1000) {
4114 reason = OFPRR_HARD_TIMEOUT;
4115 } else if (rule->idle_timeout && list_is_empty(&rule->facets)
4116 && now >rule->used + rule->idle_timeout * 1000) {
4117 reason = OFPRR_IDLE_TIMEOUT;
4122 COVERAGE_INC(ofproto_expired);
4124 /* Update stats. (This is a no-op if the rule expired due to an idle
4125 * timeout, because that only happens when the rule has no facets left.) */
4126 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4127 facet_remove(ofproto, facet);
4130 /* Get rid of the rule. */
4131 if (!rule_is_hidden(rule)) {
4132 rule_send_removed(ofproto, rule, reason);
4134 rule_remove(ofproto, rule);
4138 rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason)
4140 struct ofputil_flow_removed fr;
4142 if (!rule->send_flow_removed) {
4147 fr.cookie = rule->flow_cookie;
4149 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
4150 fr.idle_timeout = rule->idle_timeout;
4151 fr.packet_count = rule->packet_count;
4152 fr.byte_count = rule->byte_count;
4154 connmgr_send_flow_removed(p->connmgr, &fr);
4157 /* Obtains statistics for 'rule' and stores them in '*packets' and '*bytes'.
4158 * The returned statistics include statistics for all of 'rule''s facets. */
4160 rule_get_stats(const struct rule *rule, uint64_t *packets, uint64_t *bytes)
4163 struct facet *facet;
4165 /* Start from historical data for 'rule' itself that are no longer tracked
4166 * in facets. This counts, for example, facets that have expired. */
4167 p = rule->packet_count;
4168 b = rule->byte_count;
4170 /* Add any statistics that are tracked by facets. This includes
4171 * statistical data recently updated by ofproto_update_stats() as well as
4172 * stats for packets that were executed "by hand" via dpif_execute(). */
4173 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4174 p += facet->packet_count;
4175 b += facet->byte_count;
4182 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
4183 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
4184 * their individual configurations.
4186 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
4187 * Otherwise, ownership is transferred to this function. */
4189 send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall,
4190 const struct flow *flow, bool clone)
4192 struct ofputil_packet_in pin;
4194 pin.packet = upcall->packet;
4195 pin.in_port = odp_port_to_ofp_port(flow->in_port);
4196 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
4197 pin.buffer_id = 0; /* not yet known */
4198 pin.send_len = upcall->userdata;
4199 connmgr_send_packet_in(ofproto->connmgr, upcall, flow,
4200 clone ? NULL : upcall->packet);
4204 pick_datapath_id(const struct ofproto *ofproto)
4206 const struct ofport *port;
4208 port = get_port(ofproto, ODPP_LOCAL);
4210 uint8_t ea[ETH_ADDR_LEN];
4213 error = netdev_get_etheraddr(port->netdev, ea);
4215 return eth_addr_to_uint64(ea);
4217 VLOG_WARN("could not get MAC address for %s (%s)",
4218 netdev_get_name(port->netdev), strerror(error));
4220 return ofproto->fallback_dpid;
4224 pick_fallback_dpid(void)
4226 uint8_t ea[ETH_ADDR_LEN];
4227 eth_addr_nicira_random(ea);
4228 return eth_addr_to_uint64(ea);
4232 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
4233 void *aux OVS_UNUSED)
4235 const struct shash_node *node;
4239 SHASH_FOR_EACH (node, &all_ofprotos) {
4240 ds_put_format(&results, "%s\n", node->name);
4242 unixctl_command_reply(conn, 200, ds_cstr(&results));
4243 ds_destroy(&results);
4246 struct ofproto_trace {
4247 struct action_xlate_ctx ctx;
4253 trace_format_rule(struct ds *result, int level, const struct rule *rule)
4255 ds_put_char_multiple(result, '\t', level);
4257 ds_put_cstr(result, "No match\n");
4261 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
4262 ntohll(rule->flow_cookie));
4263 cls_rule_format(&rule->cr, result);
4264 ds_put_char(result, '\n');
4266 ds_put_char_multiple(result, '\t', level);
4267 ds_put_cstr(result, "OpenFlow ");
4268 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
4269 rule->n_actions * sizeof *rule->actions);
4270 ds_put_char(result, '\n');
4274 trace_format_flow(struct ds *result, int level, const char *title,
4275 struct ofproto_trace *trace)
4277 ds_put_char_multiple(result, '\t', level);
4278 ds_put_format(result, "%s: ", title);
4279 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4280 ds_put_cstr(result, "unchanged");
4282 flow_format(result, &trace->ctx.flow);
4283 trace->flow = trace->ctx.flow;
4285 ds_put_char(result, '\n');
4289 trace_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
4291 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4292 struct ds *result = trace->result;
4294 ds_put_char(result, '\n');
4295 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4296 trace_format_rule(result, ctx->recurse + 1, rule);
4300 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4301 void *aux OVS_UNUSED)
4303 char *dpname, *in_port_s, *tun_id_s, *packet_s;
4304 char *args = xstrdup(args_);
4305 char *save_ptr = NULL;
4306 struct ofproto *ofproto;
4307 struct ofpbuf packet;
4315 ofpbuf_init(&packet, strlen(args) / 2);
4318 dpname = strtok_r(args, " ", &save_ptr);
4319 tun_id_s = strtok_r(NULL, " ", &save_ptr);
4320 in_port_s = strtok_r(NULL, " ", &save_ptr);
4321 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4322 if (!dpname || !in_port_s || !packet_s) {
4323 unixctl_command_reply(conn, 501, "Bad command syntax");
4327 ofproto = shash_find_data(&all_ofprotos, dpname);
4329 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4334 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
4335 in_port = ofp_port_to_odp_port(atoi(in_port_s));
4337 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
4338 packet_s += strspn(packet_s, " ");
4339 if (*packet_s != '\0') {
4340 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4343 if (packet.size < ETH_HEADER_LEN) {
4344 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
4348 ds_put_cstr(&result, "Packet: ");
4349 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
4350 ds_put_cstr(&result, s);
4353 flow_extract(&packet, tun_id, in_port, &flow);
4354 ds_put_cstr(&result, "Flow: ");
4355 flow_format(&result, &flow);
4356 ds_put_char(&result, '\n');
4358 rule = rule_lookup(ofproto, &flow);
4359 trace_format_rule(&result, 0, rule);
4361 struct ofproto_trace trace;
4362 struct ofpbuf *odp_actions;
4364 trace.result = &result;
4366 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
4367 trace.ctx.resubmit_hook = trace_resubmit;
4368 odp_actions = xlate_actions(&trace.ctx,
4369 rule->actions, rule->n_actions);
4371 ds_put_char(&result, '\n');
4372 trace_format_flow(&result, 0, "Final flow", &trace);
4373 ds_put_cstr(&result, "Datapath actions: ");
4374 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4375 ofpbuf_delete(odp_actions);
4378 unixctl_command_reply(conn, 200, ds_cstr(&result));
4381 ds_destroy(&result);
4382 ofpbuf_uninit(&packet);
4387 ofproto_unixctl_init(void)
4389 static bool registered;
4395 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
4396 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4400 default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
4401 struct ofpbuf *odp_actions, tag_type *tags,
4402 uint16_t *nf_output_iface, void *ofproto_)
4404 struct ofproto *ofproto = ofproto_;
4405 struct mac_entry *dst_mac;
4407 /* Drop frames for reserved multicast addresses. */
4408 if (eth_addr_is_reserved(flow->dl_dst)) {
4412 /* Learn source MAC (but don't try to learn from revalidation). */
4414 && mac_learning_may_learn(ofproto->ml, flow->dl_src, 0)) {
4415 struct mac_entry *src_mac;
4417 src_mac = mac_learning_insert(ofproto->ml, flow->dl_src, 0);
4418 if (mac_entry_is_new(src_mac) || src_mac->port.i != flow->in_port) {
4419 /* The log messages here could actually be useful in debugging,
4420 * so keep the rate limit relatively high. */
4421 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4422 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
4423 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
4425 ofproto_revalidate(ofproto,
4426 mac_learning_changed(ofproto->ml, src_mac));
4427 src_mac->port.i = flow->in_port;
4431 /* Determine output port. */
4432 dst_mac = mac_learning_lookup(ofproto->ml, flow->dl_dst, 0, tags);
4434 flood_packets(ofproto, flow->in_port, OFPPC_NO_FLOOD,
4435 nf_output_iface, odp_actions);
4437 int out_port = dst_mac->port.i;
4438 if (out_port != flow->in_port) {
4439 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, out_port);
4440 *nf_output_iface = out_port;
4449 static const struct ofhooks default_ofhooks = {
4450 default_normal_ofhook_cb,