2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include <sys/socket.h>
24 #include <netinet/in.h>
27 #include "byte-order.h"
29 #include "classifier.h"
33 #include "dynamic-string.h"
34 #include "fail-open.h"
38 #include "mac-learning.h"
39 #include "multipath.h"
45 #include "ofp-print.h"
47 #include "ofproto-sflow.h"
49 #include "openflow/nicira-ext.h"
50 #include "openflow/openflow.h"
51 #include "openvswitch/datapath-protocol.h"
55 #include "poll-loop.h"
59 #include "stream-ssl.h"
63 #include "unaligned.h"
68 VLOG_DEFINE_THIS_MODULE(ofproto);
70 COVERAGE_DEFINE(facet_changed_rule);
71 COVERAGE_DEFINE(facet_revalidate);
72 COVERAGE_DEFINE(odp_overflow);
73 COVERAGE_DEFINE(ofproto_agg_request);
74 COVERAGE_DEFINE(ofproto_costly_flags);
75 COVERAGE_DEFINE(ofproto_ctlr_action);
76 COVERAGE_DEFINE(ofproto_del_rule);
77 COVERAGE_DEFINE(ofproto_error);
78 COVERAGE_DEFINE(ofproto_expiration);
79 COVERAGE_DEFINE(ofproto_expired);
80 COVERAGE_DEFINE(ofproto_flows_req);
81 COVERAGE_DEFINE(ofproto_flush);
82 COVERAGE_DEFINE(ofproto_invalidated);
83 COVERAGE_DEFINE(ofproto_no_packet_in);
84 COVERAGE_DEFINE(ofproto_ofp2odp);
85 COVERAGE_DEFINE(ofproto_packet_in);
86 COVERAGE_DEFINE(ofproto_packet_out);
87 COVERAGE_DEFINE(ofproto_queue_req);
88 COVERAGE_DEFINE(ofproto_recv_openflow);
89 COVERAGE_DEFINE(ofproto_reinit_ports);
90 COVERAGE_DEFINE(ofproto_unexpected_rule);
91 COVERAGE_DEFINE(ofproto_uninstallable);
92 COVERAGE_DEFINE(ofproto_update_port);
94 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
95 * flow translation. */
96 #define MAX_RESUBMIT_RECURSION 16
101 struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
102 struct netdev *netdev;
103 struct ofp_phy_port opp; /* In host byte order. */
105 struct cfm *cfm; /* Connectivity Fault Management, if any. */
108 static void ofport_free(struct ofport *);
109 static void ofport_run(struct ofproto *, struct ofport *);
110 static void ofport_wait(struct ofport *);
112 struct action_xlate_ctx {
113 /* action_xlate_ctx_init() initializes these members. */
116 struct ofproto *ofproto;
118 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
119 * this flow when actions change header fields. */
122 /* The packet corresponding to 'flow', or a null pointer if we are
123 * revalidating without a packet to refer to. */
124 const struct ofpbuf *packet;
126 /* If nonnull, called just before executing a resubmit action.
128 * This is normally null so the client has to set it manually after
129 * calling action_xlate_ctx_init(). */
130 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule *);
132 /* If true, the speciality of 'flow' should be checked before executing
133 * its actions. If special_cb returns false on 'flow' rendered
134 * uninstallable and no actions will be executed. */
137 /* xlate_actions() initializes and uses these members. The client might want
138 * to look at them after it returns. */
140 struct ofpbuf *odp_actions; /* Datapath actions. */
141 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
142 bool may_set_up_flow; /* True ordinarily; false if the actions must
143 * be reassessed for every packet. */
144 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
146 /* xlate_actions() initializes and uses these members, but the client has no
147 * reason to look at them. */
149 int recurse; /* Recursion level, via xlate_table_action. */
150 int last_pop_priority; /* Offset in 'odp_actions' just past most
151 * recent ODP_ACTION_ATTR_SET_PRIORITY. */
154 static void action_xlate_ctx_init(struct action_xlate_ctx *,
155 struct ofproto *, const struct flow *,
156 const struct ofpbuf *);
157 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
158 const union ofp_action *in, size_t n_in);
160 /* An OpenFlow flow. */
162 long long int used; /* Time last used; time created if not used. */
163 long long int created; /* Creation time. */
167 * - Do include packets and bytes from facets that have been deleted or
168 * whose own statistics have been folded into the rule.
170 * - Do include packets and bytes sent "by hand" that were accounted to
171 * the rule without any facet being involved (this is a rare corner
172 * case in rule_execute()).
174 * - Do not include packet or bytes that can be obtained from any facet's
175 * packet_count or byte_count member or that can be obtained from the
176 * datapath by, e.g., dpif_flow_get() for any facet.
178 uint64_t packet_count; /* Number of packets received. */
179 uint64_t byte_count; /* Number of bytes received. */
181 ovs_be64 flow_cookie; /* Controller-issued identifier. */
183 struct cls_rule cr; /* In owning ofproto's classifier. */
184 uint16_t idle_timeout; /* In seconds from time of last use. */
185 uint16_t hard_timeout; /* In seconds from time of creation. */
186 bool send_flow_removed; /* Send a flow removed message? */
187 int n_actions; /* Number of elements in actions[]. */
188 union ofp_action *actions; /* OpenFlow actions. */
189 struct list facets; /* List of "struct facet"s. */
192 static struct rule *rule_from_cls_rule(const struct cls_rule *);
193 static bool rule_is_hidden(const struct rule *);
195 static struct rule *rule_create(const struct cls_rule *,
196 const union ofp_action *, size_t n_actions,
197 uint16_t idle_timeout, uint16_t hard_timeout,
198 ovs_be64 flow_cookie, bool send_flow_removed);
199 static void rule_destroy(struct ofproto *, struct rule *);
200 static void rule_free(struct rule *);
202 static struct rule *rule_lookup(struct ofproto *, const struct flow *);
203 static void rule_insert(struct ofproto *, struct rule *);
204 static void rule_remove(struct ofproto *, struct rule *);
206 static void rule_send_removed(struct ofproto *, struct rule *, uint8_t reason);
207 static void rule_get_stats(const struct rule *, uint64_t *packets,
210 /* An exact-match instantiation of an OpenFlow flow. */
212 long long int used; /* Time last used; time created if not used. */
216 * - Do include packets and bytes sent "by hand", e.g. with
219 * - Do include packets and bytes that were obtained from the datapath
220 * when a flow was deleted (e.g. dpif_flow_del()) or when its
221 * statistics were reset (e.g. dpif_flow_put() with
222 * DPIF_FP_ZERO_STATS).
224 * - Do not include any packets or bytes that can currently be obtained
225 * from the datapath by, e.g., dpif_flow_get().
227 uint64_t packet_count; /* Number of packets received. */
228 uint64_t byte_count; /* Number of bytes received. */
230 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
231 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
233 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
234 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
235 long long int rs_used; /* Used time pushed to resubmit children. */
237 /* Number of bytes passed to account_cb. This may include bytes that can
238 * currently obtained from the datapath (thus, it can be greater than
240 uint64_t accounted_bytes;
242 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
243 struct list list_node; /* In owning rule's 'facets' list. */
244 struct rule *rule; /* Owning rule. */
245 struct flow flow; /* Exact-match flow. */
246 bool installed; /* Installed in datapath? */
247 bool may_install; /* True ordinarily; false if actions must
248 * be reassessed for every packet. */
249 size_t actions_len; /* Number of bytes in actions[]. */
250 struct nlattr *actions; /* Datapath actions. */
251 tag_type tags; /* Tags (set only by hooks). */
252 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
255 static struct facet *facet_create(struct ofproto *, struct rule *,
257 const struct ofpbuf *packet);
258 static void facet_remove(struct ofproto *, struct facet *);
259 static void facet_free(struct facet *);
261 static struct facet *facet_lookup_valid(struct ofproto *, const struct flow *);
262 static bool facet_revalidate(struct ofproto *, struct facet *);
264 static void facet_install(struct ofproto *, struct facet *, bool zero_stats);
265 static void facet_uninstall(struct ofproto *, struct facet *);
266 static void facet_flush_stats(struct ofproto *, struct facet *);
268 static void facet_make_actions(struct ofproto *, struct facet *,
269 const struct ofpbuf *packet);
270 static void facet_update_stats(struct ofproto *, struct facet *,
271 const struct dpif_flow_stats *);
272 static void facet_push_stats(struct ofproto *, struct facet *);
274 static void send_packet_in(struct ofproto *, struct dpif_upcall *,
275 const struct flow *, bool clone);
279 uint64_t datapath_id; /* Datapath ID. */
280 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
281 char *mfr_desc; /* Manufacturer. */
282 char *hw_desc; /* Hardware. */
283 char *sw_desc; /* Software version. */
284 char *serial_desc; /* Serial number. */
285 char *dp_desc; /* Datapath description. */
289 struct netdev_monitor *netdev_monitor;
290 struct hmap ports; /* Contains "struct ofport"s. */
291 struct shash port_by_name;
295 struct netflow *netflow;
296 struct ofproto_sflow *sflow;
299 struct classifier cls;
300 struct timer next_expiration;
304 bool need_revalidate;
305 struct tag_set revalidate_set;
307 /* OpenFlow connections. */
308 struct connmgr *connmgr;
310 /* Hooks for ovs-vswitchd. */
311 const struct ofhooks *ofhooks;
314 /* Used by default ofhooks. */
315 struct mac_learning *ml;
318 /* Map from dpif name to struct ofproto, for use by unixctl commands. */
319 static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos);
321 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
323 static const struct ofhooks default_ofhooks;
325 static uint64_t pick_datapath_id(const struct ofproto *);
326 static uint64_t pick_fallback_dpid(void);
328 static void ofproto_flush_flows__(struct ofproto *);
329 static int ofproto_expire(struct ofproto *);
330 static void flow_push_stats(struct ofproto *, const struct rule *,
331 struct flow *, uint64_t packets, uint64_t bytes,
334 static void handle_upcall(struct ofproto *, struct dpif_upcall *);
336 static void handle_openflow(struct ofconn *, struct ofpbuf *);
338 static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
339 static void update_port(struct ofproto *, const char *devname);
340 static int init_ports(struct ofproto *);
341 static void reinit_ports(struct ofproto *);
343 static void ofproto_unixctl_init(void);
346 ofproto_create(const char *datapath, const char *datapath_type,
347 const struct ofhooks *ofhooks, void *aux,
348 struct ofproto **ofprotop)
350 char local_name[IF_NAMESIZE];
357 ofproto_unixctl_init();
359 /* Connect to datapath and start listening for messages. */
360 error = dpif_open(datapath, datapath_type, &dpif);
362 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
365 error = dpif_recv_set_mask(dpif,
366 ((1u << DPIF_UC_MISS) |
367 (1u << DPIF_UC_ACTION) |
368 (1u << DPIF_UC_SAMPLE)));
370 VLOG_ERR("failed to listen on datapath %s: %s",
371 datapath, strerror(error));
375 dpif_flow_flush(dpif);
376 dpif_recv_purge(dpif);
378 error = dpif_port_get_name(dpif, ODPP_LOCAL,
379 local_name, sizeof local_name);
381 VLOG_ERR("%s: cannot get name of datapath local port (%s)",
382 datapath, strerror(error));
386 /* Initialize settings. */
387 p = xzalloc(sizeof *p);
388 p->fallback_dpid = pick_fallback_dpid();
389 p->datapath_id = p->fallback_dpid;
390 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
391 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
392 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
393 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
394 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
396 /* Initialize datapath. */
398 p->netdev_monitor = netdev_monitor_create();
399 hmap_init(&p->ports);
400 shash_init(&p->port_by_name);
401 p->max_ports = dpif_get_max_ports(dpif);
403 /* Initialize submodules. */
407 /* Initialize flow table. */
408 classifier_init(&p->cls);
409 timer_set_duration(&p->next_expiration, 1000);
411 /* Initialize facet table. */
412 hmap_init(&p->facets);
413 p->need_revalidate = false;
414 tag_set_init(&p->revalidate_set);
416 /* Initialize hooks. */
418 p->ofhooks = ofhooks;
422 p->ofhooks = &default_ofhooks;
424 p->ml = mac_learning_create();
427 /* Pick final datapath ID. */
428 p->datapath_id = pick_datapath_id(p);
429 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
431 shash_add_once(&all_ofprotos, dpif_name(p->dpif), p);
433 /* Initialize OpenFlow connections. */
434 p->connmgr = connmgr_create(p, datapath, local_name);
441 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
443 uint64_t old_dpid = p->datapath_id;
444 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
445 if (p->datapath_id != old_dpid) {
446 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
448 /* Force all active connections to reconnect, since there is no way to
449 * notify a controller that the datapath ID has changed. */
450 ofproto_reconnect_controllers(p);
455 ofproto_set_controllers(struct ofproto *p,
456 const struct ofproto_controller *controllers,
457 size_t n_controllers)
459 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
463 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
465 connmgr_set_fail_mode(p->connmgr, fail_mode);
468 /* Drops the connections between 'ofproto' and all of its controllers, forcing
469 * them to reconnect. */
471 ofproto_reconnect_controllers(struct ofproto *ofproto)
473 connmgr_reconnect(ofproto->connmgr);
476 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
477 * in-band control should guarantee access, in the same way that in-band
478 * control guarantees access to OpenFlow controllers. */
480 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
481 const struct sockaddr_in *extras, size_t n)
483 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
486 /* Sets the OpenFlow queue used by flows set up by in-band control on
487 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
488 * flows will use the default queue. */
490 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
492 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
496 ofproto_set_desc(struct ofproto *p,
497 const char *mfr_desc, const char *hw_desc,
498 const char *sw_desc, const char *serial_desc,
501 struct ofp_desc_stats *ods;
504 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
505 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
506 sizeof ods->mfr_desc);
509 p->mfr_desc = xstrdup(mfr_desc);
512 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
513 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
514 sizeof ods->hw_desc);
517 p->hw_desc = xstrdup(hw_desc);
520 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
521 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
522 sizeof ods->sw_desc);
525 p->sw_desc = xstrdup(sw_desc);
528 if (strlen(serial_desc) >= sizeof ods->serial_num) {
529 VLOG_WARN("truncating serial_desc, must be less than %zu "
531 sizeof ods->serial_num);
533 free(p->serial_desc);
534 p->serial_desc = xstrdup(serial_desc);
537 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
538 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
539 sizeof ods->dp_desc);
542 p->dp_desc = xstrdup(dp_desc);
547 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
549 return connmgr_set_snoops(ofproto->connmgr, snoops);
553 ofproto_set_netflow(struct ofproto *ofproto,
554 const struct netflow_options *nf_options)
556 if (nf_options && !sset_is_empty(&nf_options->collectors)) {
557 if (!ofproto->netflow) {
558 ofproto->netflow = netflow_create();
560 return netflow_set_options(ofproto->netflow, nf_options);
562 netflow_destroy(ofproto->netflow);
563 ofproto->netflow = NULL;
569 ofproto_set_sflow(struct ofproto *ofproto,
570 const struct ofproto_sflow_options *oso)
572 struct ofproto_sflow *os = ofproto->sflow;
575 struct ofport *ofport;
577 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
578 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
579 ofproto_sflow_add_port(os, ofport->odp_port,
580 netdev_get_name(ofport->netdev));
583 ofproto_sflow_set_options(os, oso);
585 ofproto_sflow_destroy(os);
586 ofproto->sflow = NULL;
590 /* Connectivity Fault Management configuration. */
592 /* Clears the CFM configuration from 'port_no' on 'ofproto'. */
594 ofproto_iface_clear_cfm(struct ofproto *ofproto, uint32_t port_no)
596 struct ofport *ofport = get_port(ofproto, port_no);
597 if (ofport && ofport->cfm){
598 cfm_destroy(ofport->cfm);
603 /* Configures connectivity fault management on 'port_no' in 'ofproto'. Takes
604 * basic configuration from the configuration members in 'cfm', and the set of
605 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
606 * Ignores the statistics members of 'cfm'.
608 * This function has no effect if 'ofproto' does not have a port 'port_no'. */
610 ofproto_iface_set_cfm(struct ofproto *ofproto, uint32_t port_no,
611 const struct cfm *cfm,
612 const uint16_t *remote_mps, size_t n_remote_mps)
614 struct ofport *ofport;
616 ofport = get_port(ofproto, port_no);
618 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
619 dpif_name(ofproto->dpif), port_no);
624 ofport->cfm = cfm_create();
627 ofport->cfm->mpid = cfm->mpid;
628 ofport->cfm->interval = cfm->interval;
629 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
631 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
633 if (!cfm_configure(ofport->cfm)) {
634 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed",
635 dpif_name(ofproto->dpif), port_no,
636 netdev_get_name(ofport->netdev));
637 cfm_destroy(ofport->cfm);
642 /* Returns the connectivity fault management object associated with 'port_no'
643 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
644 * 'port_no' or if that port does not have CFM configured. The caller must not
645 * modify or destroy the returned object. */
647 ofproto_iface_get_cfm(struct ofproto *ofproto, uint32_t port_no)
649 struct ofport *ofport = get_port(ofproto, port_no);
650 return ofport ? ofport->cfm : NULL;
654 ofproto_get_datapath_id(const struct ofproto *ofproto)
656 return ofproto->datapath_id;
660 ofproto_has_primary_controller(const struct ofproto *ofproto)
662 return connmgr_has_controllers(ofproto->connmgr);
665 enum ofproto_fail_mode
666 ofproto_get_fail_mode(const struct ofproto *p)
668 return connmgr_get_fail_mode(p->connmgr);
672 ofproto_has_snoops(const struct ofproto *ofproto)
674 return connmgr_has_snoops(ofproto->connmgr);
678 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
680 connmgr_get_snoops(ofproto->connmgr, snoops);
684 ofproto_destroy(struct ofproto *p)
686 struct ofport *ofport, *next_ofport;
692 shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif));
694 ofproto_flush_flows__(p);
695 connmgr_destroy(p->connmgr);
696 classifier_destroy(&p->cls);
697 hmap_destroy(&p->facets);
700 netdev_monitor_destroy(p->netdev_monitor);
701 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
702 hmap_remove(&p->ports, &ofport->hmap_node);
705 shash_destroy(&p->port_by_name);
707 netflow_destroy(p->netflow);
708 ofproto_sflow_destroy(p->sflow);
710 mac_learning_destroy(p->ml);
715 free(p->serial_desc);
718 hmap_destroy(&p->ports);
724 ofproto_run(struct ofproto *p)
726 int error = ofproto_run1(p);
728 error = ofproto_run2(p, false);
734 process_port_change(struct ofproto *ofproto, int error, char *devname)
736 if (error == ENOBUFS) {
737 reinit_ports(ofproto);
739 update_port(ofproto, devname);
745 ofproto_run1(struct ofproto *p)
747 struct ofport *ofport;
752 if (shash_is_empty(&p->port_by_name)) {
756 for (i = 0; i < 50; i++) {
757 struct dpif_upcall packet;
759 error = dpif_recv(p->dpif, &packet);
761 if (error == ENODEV) {
762 /* Someone destroyed the datapath behind our back. The caller
763 * better destroy us and give up, because we're just going to
764 * spin from here on out. */
765 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
766 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
773 handle_upcall(p, &packet);
776 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
777 process_port_change(p, error, devname);
779 while ((error = netdev_monitor_poll(p->netdev_monitor,
780 &devname)) != EAGAIN) {
781 process_port_change(p, error, devname);
784 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
785 ofport_run(p, ofport);
788 connmgr_run(p->connmgr, handle_openflow);
790 if (timer_expired(&p->next_expiration)) {
791 int delay = ofproto_expire(p);
792 timer_set_duration(&p->next_expiration, delay);
793 COVERAGE_INC(ofproto_expiration);
797 netflow_run(p->netflow);
800 ofproto_sflow_run(p->sflow);
807 ofproto_run2(struct ofproto *p, bool revalidate_all)
809 /* Figure out what we need to revalidate now, if anything. */
810 struct tag_set revalidate_set = p->revalidate_set;
811 if (p->need_revalidate) {
812 revalidate_all = true;
815 /* Clear the revalidation flags. */
816 tag_set_init(&p->revalidate_set);
817 p->need_revalidate = false;
819 /* Now revalidate if there's anything to do. */
820 if (revalidate_all || !tag_set_is_empty(&revalidate_set)) {
821 struct facet *facet, *next;
823 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) {
825 || tag_set_intersects(&revalidate_set, facet->tags)) {
826 facet_revalidate(p, facet);
835 ofproto_wait(struct ofproto *p)
837 struct ofport *ofport;
839 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
842 dpif_recv_wait(p->dpif);
843 dpif_port_poll_wait(p->dpif);
844 netdev_monitor_poll_wait(p->netdev_monitor);
846 ofproto_sflow_wait(p->sflow);
848 if (!tag_set_is_empty(&p->revalidate_set)) {
849 poll_immediate_wake();
851 if (p->need_revalidate) {
852 /* Shouldn't happen, but if it does just go around again. */
853 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
854 poll_immediate_wake();
856 timer_wait(&p->next_expiration);
858 connmgr_wait(p->connmgr);
862 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
864 tag_set_add(&ofproto->revalidate_set, tag);
868 ofproto_get_revalidate_set(struct ofproto *ofproto)
870 return &ofproto->revalidate_set;
874 ofproto_is_alive(const struct ofproto *p)
876 return connmgr_has_controllers(p->connmgr);
880 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
883 connmgr_get_controller_info(ofproto->connmgr, info);
887 ofproto_free_ofproto_controller_info(struct shash *info)
889 struct shash_node *node;
891 SHASH_FOR_EACH (node, info) {
892 struct ofproto_controller_info *cinfo = node->data;
893 while (cinfo->pairs.n) {
894 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
901 /* Deletes port number 'odp_port' from the datapath for 'ofproto'.
903 * This is almost the same as calling dpif_port_del() directly on the
904 * datapath, but it also makes 'ofproto' close its open netdev for the port
905 * (if any). This makes it possible to create a new netdev of a different
906 * type under the same name, which otherwise the netdev library would refuse
907 * to do because of the conflict. (The netdev would eventually get closed on
908 * the next trip through ofproto_run(), but this interface is more direct.)
910 * Returns 0 if successful, otherwise a positive errno. */
912 ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port)
914 struct ofport *ofport = get_port(ofproto, odp_port);
915 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
918 error = dpif_port_del(ofproto->dpif, odp_port);
920 VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)",
921 dpif_name(ofproto->dpif), odp_port, name, strerror(error));
923 /* 'name' is the netdev's name and update_port() is going to close the
924 * netdev. Just in case update_port() refers to 'name' after it
925 * destroys 'ofport', make a copy of it around the update_port()
927 char *devname = xstrdup(name);
928 update_port(ofproto, devname);
934 /* Checks if 'ofproto' thinks 'odp_port' should be included in floods. Returns
935 * true if 'odp_port' exists and should be included, false otherwise. */
937 ofproto_port_is_floodable(struct ofproto *ofproto, uint16_t odp_port)
939 struct ofport *ofport = get_port(ofproto, odp_port);
940 return ofport && !(ofport->opp.config & OFPPC_NO_FLOOD);
943 /* Sends 'packet' out of port 'port_no' within 'p'. If 'vlan_tci' is zero the
944 * packet will not have any 802.1Q hader; if it is nonzero, then the packet
945 * will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
947 * Returns 0 if successful, otherwise a positive errno value. */
949 ofproto_send_packet(struct ofproto *ofproto,
950 uint32_t port_no, uint16_t vlan_tci,
951 const struct ofpbuf *packet)
953 struct ofpbuf odp_actions;
956 ofpbuf_init(&odp_actions, 32);
958 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
959 ntohs(vlan_tci & ~VLAN_CFI));
961 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, port_no);
962 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
964 ofpbuf_uninit(&odp_actions);
967 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
968 dpif_name(ofproto->dpif), port_no, strerror(error));
973 /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
974 * performs the 'n_actions' actions in 'actions'. The new flow will not
977 * If cls_rule->priority is in the range of priorities supported by OpenFlow
978 * (0...65535, inclusive) then the flow will be visible to OpenFlow
979 * controllers; otherwise, it will be hidden.
981 * The caller retains ownership of 'cls_rule' and 'actions'. */
983 ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
984 const union ofp_action *actions, size_t n_actions)
987 rule = rule_create(cls_rule, actions, n_actions, 0, 0, 0, false);
988 rule_insert(p, rule);
992 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
996 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
999 rule_remove(ofproto, rule);
1004 ofproto_flush_flows__(struct ofproto *ofproto)
1006 struct facet *facet, *next_facet;
1007 struct rule *rule, *next_rule;
1008 struct cls_cursor cursor;
1010 COVERAGE_INC(ofproto_flush);
1012 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1013 /* Mark the facet as not installed so that facet_remove() doesn't
1014 * bother trying to uninstall it. There is no point in uninstalling it
1015 * individually since we are about to blow away all the facets with
1016 * dpif_flow_flush(). */
1017 facet->installed = false;
1018 facet->dp_packet_count = 0;
1019 facet->dp_byte_count = 0;
1020 facet_remove(ofproto, facet);
1023 cls_cursor_init(&cursor, &ofproto->cls, NULL);
1024 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1025 rule_remove(ofproto, rule);
1028 dpif_flow_flush(ofproto->dpif);
1032 ofproto_flush_flows(struct ofproto *ofproto)
1034 ofproto_flush_flows__(ofproto);
1035 connmgr_flushed(ofproto->connmgr);
1039 reinit_ports(struct ofproto *p)
1041 struct dpif_port_dump dump;
1042 struct sset devnames;
1043 struct ofport *ofport;
1044 struct dpif_port dpif_port;
1045 const char *devname;
1047 COVERAGE_INC(ofproto_reinit_ports);
1049 sset_init(&devnames);
1050 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1051 sset_add(&devnames, netdev_get_name(ofport->netdev));
1053 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1054 sset_add(&devnames, dpif_port.name);
1057 SSET_FOR_EACH (devname, &devnames) {
1058 update_port(p, devname);
1060 sset_destroy(&devnames);
1063 /* Opens and returns a netdev for 'dpif_port', or a null pointer if the netdev
1064 * cannot be opened. On success, also fills in 'opp', in *HOST* byte order. */
1065 static struct netdev *
1066 ofport_open(const struct dpif_port *dpif_port, struct ofp_phy_port *opp)
1068 struct netdev_options netdev_options;
1069 enum netdev_flags flags;
1070 struct netdev *netdev;
1073 memset(&netdev_options, 0, sizeof netdev_options);
1074 netdev_options.name = dpif_port->name;
1075 netdev_options.type = dpif_port->type;
1076 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
1078 error = netdev_open(&netdev_options, &netdev);
1080 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1081 "cannot be opened (%s)",
1082 dpif_port->name, dpif_port->port_no,
1083 dpif_port->name, strerror(error));
1087 netdev_get_flags(netdev, &flags);
1089 opp->port_no = odp_port_to_ofp_port(dpif_port->port_no);
1090 netdev_get_etheraddr(netdev, opp->hw_addr);
1091 ovs_strzcpy(opp->name, dpif_port->name, sizeof opp->name);
1092 opp->config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
1093 opp->state = netdev_get_carrier(netdev) ? 0 : OFPPS_LINK_DOWN;
1094 netdev_get_features(netdev, &opp->curr, &opp->advertised,
1095 &opp->supported, &opp->peer);
1100 ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port)
1102 if (get_port(p, dpif_port->port_no)) {
1103 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1104 dpif_port->port_no);
1106 } else if (shash_find(&p->port_by_name, dpif_port->name)) {
1107 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1115 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1116 * port number, and 'config' bits other than OFPPC_PORT_DOWN are
1119 ofport_equal(const struct ofp_phy_port *a, const struct ofp_phy_port *b)
1121 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1122 return (!memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1123 && a->state == b->state
1124 && !((a->config ^ b->config) & OFPPC_PORT_DOWN)
1125 && a->curr == b->curr
1126 && a->advertised == b->advertised
1127 && a->supported == b->supported
1128 && a->peer == b->peer);
1131 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1132 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1133 * one with the same name or port number). */
1135 ofport_install(struct ofproto *p,
1136 struct netdev *netdev, const struct ofp_phy_port *opp)
1138 const char *netdev_name = netdev_get_name(netdev);
1139 struct ofport *ofport;
1141 connmgr_send_port_status(p->connmgr, opp, OFPPR_ADD);
1143 /* Create ofport. */
1144 ofport = xmalloc(sizeof *ofport);
1145 ofport->netdev = netdev;
1147 ofport->odp_port = ofp_port_to_odp_port(opp->port_no);
1150 /* Add port to 'p'. */
1151 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1152 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
1153 shash_add(&p->port_by_name, netdev_name, ofport);
1155 ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
1159 /* Removes 'ofport' from 'p' and destroys it. */
1161 ofport_remove(struct ofproto *p, struct ofport *ofport)
1163 connmgr_send_port_status(p->connmgr, &ofport->opp, OFPPR_DELETE);
1165 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1166 hmap_remove(&p->ports, &ofport->hmap_node);
1167 shash_delete(&p->port_by_name,
1168 shash_find(&p->port_by_name,
1169 netdev_get_name(ofport->netdev)));
1171 ofproto_sflow_del_port(p->sflow, ofport->odp_port);
1174 ofport_free(ofport);
1177 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1180 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1182 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1184 ofport_remove(ofproto, port);
1188 /* Updates 'port' within 'ofproto' with the new 'netdev' and 'opp'.
1190 * Does not handle a name or port number change. The caller must implement
1191 * such a change as a delete followed by an add. */
1193 ofport_modified(struct ofproto *ofproto, struct ofport *port,
1194 struct netdev *netdev, struct ofp_phy_port *opp)
1196 memcpy(port->opp.hw_addr, opp->hw_addr, ETH_ADDR_LEN);
1197 port->opp.config = ((port->opp.config & ~OFPPC_PORT_DOWN)
1198 | (opp->config & OFPPC_PORT_DOWN));
1199 port->opp.state = opp->state;
1200 port->opp.curr = opp->curr;
1201 port->opp.advertised = opp->advertised;
1202 port->opp.supported = opp->supported;
1203 port->opp.peer = opp->peer;
1205 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1206 netdev_monitor_add(ofproto->netdev_monitor, netdev);
1208 netdev_close(port->netdev);
1209 port->netdev = netdev;
1211 connmgr_send_port_status(ofproto->connmgr, &port->opp, OFPPR_MODIFY);
1215 ofport_run(struct ofproto *ofproto, struct ofport *ofport)
1218 cfm_run(ofport->cfm);
1220 if (cfm_should_send_ccm(ofport->cfm)) {
1221 struct ofpbuf packet;
1224 ofpbuf_init(&packet, 0);
1225 ccm = compose_packet(&packet, eth_addr_ccm, ofport->opp.hw_addr,
1226 ETH_TYPE_CFM, sizeof *ccm);
1227 cfm_compose_ccm(ofport->cfm, ccm);
1228 ofproto_send_packet(ofproto, ofport->odp_port, 0, &packet);
1229 ofpbuf_uninit(&packet);
1235 ofport_wait(struct ofport *ofport)
1238 cfm_wait(ofport->cfm);
1243 ofport_free(struct ofport *ofport)
1246 cfm_destroy(ofport->cfm);
1247 netdev_close(ofport->netdev);
1252 static struct ofport *
1253 get_port(const struct ofproto *ofproto, uint16_t odp_port)
1255 struct ofport *port;
1257 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1258 hash_int(odp_port, 0), &ofproto->ports) {
1259 if (port->odp_port == odp_port) {
1267 update_port(struct ofproto *ofproto, const char *name)
1269 struct dpif_port dpif_port;
1270 struct ofp_phy_port opp;
1271 struct netdev *netdev;
1272 struct ofport *port;
1274 COVERAGE_INC(ofproto_update_port);
1276 /* Fetch 'name''s location and properties from the datapath. */
1277 netdev = (!dpif_port_query_by_name(ofproto->dpif, name, &dpif_port)
1278 ? ofport_open(&dpif_port, &opp)
1281 port = get_port(ofproto, dpif_port.port_no);
1282 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
1283 /* 'name' hasn't changed location. Any properties changed? */
1284 if (!ofport_equal(&port->opp, &opp)) {
1285 ofport_modified(ofproto, port, netdev, &opp);
1287 netdev_close(netdev);
1290 /* If 'port' is nonnull then its name differs from 'name' and thus
1291 * we should delete it. If we think there's a port named 'name'
1292 * then its port number must be wrong now so delete it too. */
1294 ofport_remove(ofproto, port);
1296 ofport_remove_with_name(ofproto, name);
1297 ofport_install(ofproto, netdev, &opp);
1300 /* Any port named 'name' is gone now. */
1301 ofport_remove_with_name(ofproto, name);
1303 dpif_port_destroy(&dpif_port);
1307 init_ports(struct ofproto *p)
1309 struct dpif_port_dump dump;
1310 struct dpif_port dpif_port;
1312 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1313 if (!ofport_conflicts(p, &dpif_port)) {
1314 struct ofp_phy_port opp;
1315 struct netdev *netdev;
1317 netdev = ofport_open(&dpif_port, &opp);
1319 ofport_install(p, netdev, &opp);
1327 /* Returns true if 'rule' should be hidden from the controller.
1329 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1330 * (e.g. by in-band control) and are intentionally hidden from the
1333 rule_is_hidden(const struct rule *rule)
1335 return rule->cr.priority > UINT16_MAX;
1338 /* Creates and returns a new rule initialized as specified.
1340 * The caller is responsible for inserting the rule into the classifier (with
1341 * rule_insert()). */
1342 static struct rule *
1343 rule_create(const struct cls_rule *cls_rule,
1344 const union ofp_action *actions, size_t n_actions,
1345 uint16_t idle_timeout, uint16_t hard_timeout,
1346 ovs_be64 flow_cookie, bool send_flow_removed)
1348 struct rule *rule = xzalloc(sizeof *rule);
1349 rule->cr = *cls_rule;
1350 rule->idle_timeout = idle_timeout;
1351 rule->hard_timeout = hard_timeout;
1352 rule->flow_cookie = flow_cookie;
1353 rule->used = rule->created = time_msec();
1354 rule->send_flow_removed = send_flow_removed;
1355 list_init(&rule->facets);
1356 if (n_actions > 0) {
1357 rule->n_actions = n_actions;
1358 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1364 static struct rule *
1365 rule_from_cls_rule(const struct cls_rule *cls_rule)
1367 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1371 rule_free(struct rule *rule)
1373 free(rule->actions);
1377 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
1378 * destroying any that no longer has a rule (which is probably all of them).
1380 * The caller must have already removed 'rule' from the classifier. */
1382 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1384 struct facet *facet, *next_facet;
1385 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1386 facet_revalidate(ofproto, facet);
1391 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1392 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1395 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
1397 const union ofp_action *oa;
1398 struct actions_iterator i;
1400 if (out_port == htons(OFPP_NONE)) {
1403 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1404 oa = actions_next(&i)) {
1405 if (action_outputs_to_port(oa, out_port)) {
1412 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1413 * 'packet', which arrived on 'in_port'.
1415 * Takes ownership of 'packet'. */
1417 execute_odp_actions(struct ofproto *ofproto, const struct flow *flow,
1418 const struct nlattr *odp_actions, size_t actions_len,
1419 struct ofpbuf *packet)
1421 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1422 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1423 /* As an optimization, avoid a round-trip from userspace to kernel to
1424 * userspace. This also avoids possibly filling up kernel packet
1425 * buffers along the way. */
1426 struct dpif_upcall upcall;
1428 upcall.type = DPIF_UC_ACTION;
1429 upcall.packet = packet;
1432 upcall.userdata = nl_attr_get_u64(odp_actions);
1433 upcall.sample_pool = 0;
1434 upcall.actions = NULL;
1435 upcall.actions_len = 0;
1437 send_packet_in(ofproto, &upcall, flow, false);
1443 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
1444 ofpbuf_delete(packet);
1449 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
1450 * statistics appropriately. 'packet' must have at least sizeof(struct
1451 * ofp_packet_in) bytes of headroom.
1453 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
1454 * applying flow_extract() to 'packet' would yield the same flow as
1457 * 'facet' must have accurately composed ODP actions; that is, it must not be
1458 * in need of revalidation.
1460 * Takes ownership of 'packet'. */
1462 facet_execute(struct ofproto *ofproto, struct facet *facet,
1463 struct ofpbuf *packet)
1465 struct dpif_flow_stats stats;
1467 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1469 flow_extract_stats(&facet->flow, packet, &stats);
1470 stats.used = time_msec();
1471 if (execute_odp_actions(ofproto, &facet->flow,
1472 facet->actions, facet->actions_len, packet)) {
1473 facet_update_stats(ofproto, facet, &stats);
1477 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1478 * statistics (or the statistics for one of its facets) appropriately.
1479 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
1481 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1482 * with statistics for 'packet' either way.
1484 * Takes ownership of 'packet'. */
1486 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
1487 struct ofpbuf *packet)
1489 struct action_xlate_ctx ctx;
1490 struct ofpbuf *odp_actions;
1491 struct facet *facet;
1495 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1497 flow_extract(packet, 0, in_port, &flow);
1499 /* First look for a related facet. If we find one, account it to that. */
1500 facet = facet_lookup_valid(ofproto, &flow);
1501 if (facet && facet->rule == rule) {
1502 facet_execute(ofproto, facet, packet);
1506 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
1507 * create a new facet for it and use that. */
1508 if (rule_lookup(ofproto, &flow) == rule) {
1509 facet = facet_create(ofproto, rule, &flow, packet);
1510 facet_execute(ofproto, facet, packet);
1511 facet_install(ofproto, facet, true);
1515 /* We can't account anything to a facet. If we were to try, then that
1516 * facet would have a non-matching rule, busting our invariants. */
1517 action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
1518 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1519 size = packet->size;
1520 if (execute_odp_actions(ofproto, &flow, odp_actions->data,
1521 odp_actions->size, packet)) {
1522 rule->used = time_msec();
1523 rule->packet_count++;
1524 rule->byte_count += size;
1525 flow_push_stats(ofproto, rule, &flow, 1, size, rule->used);
1527 ofpbuf_delete(odp_actions);
1530 /* Inserts 'rule' into 'p''s flow table. */
1532 rule_insert(struct ofproto *p, struct rule *rule)
1534 struct rule *displaced_rule;
1536 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1537 if (displaced_rule) {
1538 rule_destroy(p, displaced_rule);
1540 p->need_revalidate = true;
1543 /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a
1544 * 'flow' and an example 'packet' within that flow.
1546 * The caller must already have determined that no facet with an identical
1547 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1548 * 'ofproto''s classifier table. */
1549 static struct facet *
1550 facet_create(struct ofproto *ofproto, struct rule *rule,
1551 const struct flow *flow, const struct ofpbuf *packet)
1553 struct facet *facet;
1555 facet = xzalloc(sizeof *facet);
1556 facet->used = time_msec();
1557 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1558 list_push_back(&rule->facets, &facet->list_node);
1560 facet->flow = *flow;
1561 netflow_flow_init(&facet->nf_flow);
1562 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1564 facet_make_actions(ofproto, facet, packet);
1570 facet_free(struct facet *facet)
1572 free(facet->actions);
1576 /* Remove 'rule' from 'ofproto' and free up the associated memory:
1578 * - Removes 'rule' from the classifier.
1580 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
1581 * destroys them), via rule_destroy().
1584 rule_remove(struct ofproto *ofproto, struct rule *rule)
1586 COVERAGE_INC(ofproto_del_rule);
1587 ofproto->need_revalidate = true;
1588 classifier_remove(&ofproto->cls, &rule->cr);
1589 rule_destroy(ofproto, rule);
1592 /* Remove 'facet' from 'ofproto' and free up the associated memory:
1594 * - If 'facet' was installed in the datapath, uninstalls it and updates its
1595 * rule's statistics, via facet_uninstall().
1597 * - Removes 'facet' from its rule and from ofproto->facets.
1600 facet_remove(struct ofproto *ofproto, struct facet *facet)
1602 facet_uninstall(ofproto, facet);
1603 facet_flush_stats(ofproto, facet);
1604 hmap_remove(&ofproto->facets, &facet->hmap_node);
1605 list_remove(&facet->list_node);
1609 /* Composes the ODP actions for 'facet' based on its rule's actions. */
1611 facet_make_actions(struct ofproto *p, struct facet *facet,
1612 const struct ofpbuf *packet)
1614 const struct rule *rule = facet->rule;
1615 struct ofpbuf *odp_actions;
1616 struct action_xlate_ctx ctx;
1618 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
1619 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1620 facet->tags = ctx.tags;
1621 facet->may_install = ctx.may_set_up_flow;
1622 facet->nf_flow.output_iface = ctx.nf_output_iface;
1624 if (facet->actions_len != odp_actions->size
1625 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
1626 free(facet->actions);
1627 facet->actions_len = odp_actions->size;
1628 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1631 ofpbuf_delete(odp_actions);
1635 facet_put__(struct ofproto *ofproto, struct facet *facet,
1636 const struct nlattr *actions, size_t actions_len,
1637 struct dpif_flow_stats *stats)
1639 struct odputil_keybuf keybuf;
1640 enum dpif_flow_put_flags flags;
1643 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1645 flags |= DPIF_FP_ZERO_STATS;
1646 facet->dp_packet_count = 0;
1647 facet->dp_byte_count = 0;
1650 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1651 odp_flow_key_from_flow(&key, &facet->flow);
1653 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
1654 actions, actions_len, stats);
1657 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
1658 * 'zero_stats' is true, clears any existing statistics from the datapath for
1661 facet_install(struct ofproto *p, struct facet *facet, bool zero_stats)
1663 struct dpif_flow_stats stats;
1665 if (facet->may_install
1666 && !facet_put__(p, facet, facet->actions, facet->actions_len,
1667 zero_stats ? &stats : NULL)) {
1668 facet->installed = true;
1672 /* Ensures that the bytes in 'facet', plus 'extra_bytes', have been passed up
1673 * to the accounting hook function in the ofhooks structure. */
1675 facet_account(struct ofproto *ofproto,
1676 struct facet *facet, uint64_t extra_bytes)
1678 uint64_t total_bytes = facet->byte_count + extra_bytes;
1680 if (ofproto->ofhooks->account_flow_cb
1681 && total_bytes > facet->accounted_bytes)
1683 ofproto->ofhooks->account_flow_cb(
1684 &facet->flow, facet->tags, facet->actions, facet->actions_len,
1685 total_bytes - facet->accounted_bytes, ofproto->aux);
1686 facet->accounted_bytes = total_bytes;
1690 /* If 'rule' is installed in the datapath, uninstalls it. */
1692 facet_uninstall(struct ofproto *p, struct facet *facet)
1694 if (facet->installed) {
1695 struct odputil_keybuf keybuf;
1696 struct dpif_flow_stats stats;
1699 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1700 odp_flow_key_from_flow(&key, &facet->flow);
1702 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
1703 facet_update_stats(p, facet, &stats);
1705 facet->installed = false;
1706 facet->dp_packet_count = 0;
1707 facet->dp_byte_count = 0;
1709 assert(facet->dp_packet_count == 0);
1710 assert(facet->dp_byte_count == 0);
1714 /* Returns true if the only action for 'facet' is to send to the controller.
1715 * (We don't report NetFlow expiration messages for such facets because they
1716 * are just part of the control logic for the network, not real traffic). */
1718 facet_is_controller_flow(struct facet *facet)
1721 && facet->rule->n_actions == 1
1722 && action_outputs_to_port(&facet->rule->actions[0],
1723 htons(OFPP_CONTROLLER)));
1726 /* Folds all of 'facet''s statistics into its rule. Also updates the
1727 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
1728 * 'facet''s statistics in the datapath should have been zeroed and folded into
1729 * its packet and byte counts before this function is called. */
1731 facet_flush_stats(struct ofproto *ofproto, struct facet *facet)
1733 assert(!facet->dp_byte_count);
1734 assert(!facet->dp_packet_count);
1736 facet_push_stats(ofproto, facet);
1737 facet_account(ofproto, facet, 0);
1739 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
1740 struct ofexpired expired;
1741 expired.flow = facet->flow;
1742 expired.packet_count = facet->packet_count;
1743 expired.byte_count = facet->byte_count;
1744 expired.used = facet->used;
1745 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1748 facet->rule->packet_count += facet->packet_count;
1749 facet->rule->byte_count += facet->byte_count;
1751 /* Reset counters to prevent double counting if 'facet' ever gets
1753 facet->packet_count = 0;
1754 facet->byte_count = 0;
1755 facet->rs_packet_count = 0;
1756 facet->rs_byte_count = 0;
1757 facet->accounted_bytes = 0;
1759 netflow_flow_clear(&facet->nf_flow);
1762 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1763 * Returns it if found, otherwise a null pointer.
1765 * The returned facet might need revalidation; use facet_lookup_valid()
1766 * instead if that is important. */
1767 static struct facet *
1768 facet_find(struct ofproto *ofproto, const struct flow *flow)
1770 struct facet *facet;
1772 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
1774 if (flow_equal(flow, &facet->flow)) {
1782 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1783 * Returns it if found, otherwise a null pointer.
1785 * The returned facet is guaranteed to be valid. */
1786 static struct facet *
1787 facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
1789 struct facet *facet = facet_find(ofproto, flow);
1791 /* The facet we found might not be valid, since we could be in need of
1792 * revalidation. If it is not valid, don't return it. */
1794 && ofproto->need_revalidate
1795 && !facet_revalidate(ofproto, facet)) {
1796 COVERAGE_INC(ofproto_invalidated);
1803 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
1805 * - If the rule found is different from 'facet''s current rule, moves
1806 * 'facet' to the new rule and recompiles its actions.
1808 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
1809 * where it is and recompiles its actions anyway.
1811 * - If there is none, destroys 'facet'.
1813 * Returns true if 'facet' still exists, false if it has been destroyed. */
1815 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
1817 struct action_xlate_ctx ctx;
1818 struct ofpbuf *odp_actions;
1819 struct rule *new_rule;
1820 bool actions_changed;
1822 COVERAGE_INC(facet_revalidate);
1824 /* Determine the new rule. */
1825 new_rule = rule_lookup(ofproto, &facet->flow);
1827 /* No new rule, so delete the facet. */
1828 facet_remove(ofproto, facet);
1832 /* Calculate new ODP actions.
1834 * We do not modify any 'facet' state yet, because we might need to, e.g.,
1835 * emit a NetFlow expiration and, if so, we need to have the old state
1836 * around to properly compose it. */
1837 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
1838 odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
1839 actions_changed = (facet->actions_len != odp_actions->size
1840 || memcmp(facet->actions, odp_actions->data,
1841 facet->actions_len));
1843 /* If the ODP actions changed or the installability changed, then we need
1844 * to talk to the datapath. */
1845 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
1846 if (ctx.may_set_up_flow) {
1847 struct dpif_flow_stats stats;
1849 facet_put__(ofproto, facet,
1850 odp_actions->data, odp_actions->size, &stats);
1851 facet_update_stats(ofproto, facet, &stats);
1853 facet_uninstall(ofproto, facet);
1856 /* The datapath flow is gone or has zeroed stats, so push stats out of
1857 * 'facet' into 'rule'. */
1858 facet_flush_stats(ofproto, facet);
1861 /* Update 'facet' now that we've taken care of all the old state. */
1862 facet->tags = ctx.tags;
1863 facet->nf_flow.output_iface = ctx.nf_output_iface;
1864 facet->may_install = ctx.may_set_up_flow;
1865 if (actions_changed) {
1866 free(facet->actions);
1867 facet->actions_len = odp_actions->size;
1868 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1870 if (facet->rule != new_rule) {
1871 COVERAGE_INC(facet_changed_rule);
1872 list_remove(&facet->list_node);
1873 list_push_back(&new_rule->facets, &facet->list_node);
1874 facet->rule = new_rule;
1875 facet->used = new_rule->created;
1876 facet->rs_used = facet->used;
1879 ofpbuf_delete(odp_actions);
1885 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1888 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
1890 COVERAGE_INC(ofproto_error);
1891 ofconn_send_reply(ofconn, buf);
1896 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1898 ofconn_send_reply(ofconn, make_echo_reply(oh));
1903 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1905 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1906 struct ofp_switch_features *osf;
1908 struct ofport *port;
1910 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1911 osf->datapath_id = htonll(ofproto->datapath_id);
1912 osf->n_buffers = htonl(pktbuf_capacity());
1914 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1915 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
1916 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1917 (1u << OFPAT_SET_VLAN_VID) |
1918 (1u << OFPAT_SET_VLAN_PCP) |
1919 (1u << OFPAT_STRIP_VLAN) |
1920 (1u << OFPAT_SET_DL_SRC) |
1921 (1u << OFPAT_SET_DL_DST) |
1922 (1u << OFPAT_SET_NW_SRC) |
1923 (1u << OFPAT_SET_NW_DST) |
1924 (1u << OFPAT_SET_NW_TOS) |
1925 (1u << OFPAT_SET_TP_SRC) |
1926 (1u << OFPAT_SET_TP_DST) |
1927 (1u << OFPAT_ENQUEUE));
1929 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1930 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1933 ofconn_send_reply(ofconn, buf);
1938 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1940 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1942 struct ofp_switch_config *osc;
1946 /* Figure out flags. */
1947 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
1948 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1951 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1952 osc->flags = htons(flags);
1953 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1954 ofconn_send_reply(ofconn, buf);
1960 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1962 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1963 uint16_t flags = ntohs(osc->flags);
1965 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1966 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1967 switch (flags & OFPC_FRAG_MASK) {
1968 case OFPC_FRAG_NORMAL:
1969 dpif_set_drop_frags(ofproto->dpif, false);
1971 case OFPC_FRAG_DROP:
1972 dpif_set_drop_frags(ofproto->dpif, true);
1975 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1981 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
1986 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
1987 struct action_xlate_ctx *ctx);
1990 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
1992 const struct ofport *ofport = get_port(ctx->ofproto, port);
1995 if (ofport->opp.config & OFPPC_NO_FWD) {
1996 /* Forwarding disabled on port. */
2001 * We don't have an ofport record for this port, but it doesn't hurt to
2002 * allow forwarding to it anyhow. Maybe such a port will appear later
2003 * and we're pre-populating the flow table.
2007 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port);
2008 ctx->nf_output_iface = port;
2011 static struct rule *
2012 rule_lookup(struct ofproto *ofproto, const struct flow *flow)
2014 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
2018 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2020 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2021 uint16_t old_in_port;
2024 /* Look up a flow with 'in_port' as the input port. Then restore the
2025 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2026 * have surprising behavior). */
2027 old_in_port = ctx->flow.in_port;
2028 ctx->flow.in_port = in_port;
2029 rule = rule_lookup(ctx->ofproto, &ctx->flow);
2030 ctx->flow.in_port = old_in_port;
2032 if (ctx->resubmit_hook) {
2033 ctx->resubmit_hook(ctx, rule);
2038 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2042 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2044 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2045 MAX_RESUBMIT_RECURSION);
2050 flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, uint32_t mask,
2051 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2053 struct ofport *ofport;
2055 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
2056 uint16_t odp_port = ofport->odp_port;
2057 if (odp_port != odp_in_port && !(ofport->opp.config & mask)) {
2058 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2061 *nf_output_iface = NF_OUT_FLOOD;
2065 xlate_output_action__(struct action_xlate_ctx *ctx,
2066 uint16_t port, uint16_t max_len)
2069 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2071 ctx->nf_output_iface = NF_OUT_DROP;
2075 add_output_action(ctx, ctx->flow.in_port);
2078 xlate_table_action(ctx, ctx->flow.in_port);
2081 if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
2082 ctx->odp_actions, &ctx->tags,
2083 &ctx->nf_output_iface,
2084 ctx->ofproto->aux)) {
2085 COVERAGE_INC(ofproto_uninstallable);
2086 ctx->may_set_up_flow = false;
2090 flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD,
2091 &ctx->nf_output_iface, ctx->odp_actions);
2094 flood_packets(ctx->ofproto, ctx->flow.in_port, 0,
2095 &ctx->nf_output_iface, ctx->odp_actions);
2097 case OFPP_CONTROLLER:
2098 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2101 add_output_action(ctx, ODPP_LOCAL);
2104 odp_port = ofp_port_to_odp_port(port);
2105 if (odp_port != ctx->flow.in_port) {
2106 add_output_action(ctx, odp_port);
2111 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2112 ctx->nf_output_iface = NF_OUT_FLOOD;
2113 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2114 ctx->nf_output_iface = prev_nf_output_iface;
2115 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2116 ctx->nf_output_iface != NF_OUT_FLOOD) {
2117 ctx->nf_output_iface = NF_OUT_MULTI;
2122 xlate_output_action(struct action_xlate_ctx *ctx,
2123 const struct ofp_action_output *oao)
2125 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2128 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2129 * optimization, because we're going to add another action that sets the
2130 * priority immediately after, or because there are no actions following the
2133 remove_pop_action(struct action_xlate_ctx *ctx)
2135 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2136 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2137 ctx->last_pop_priority = -1;
2142 add_pop_action(struct action_xlate_ctx *ctx)
2144 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2145 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2146 ctx->last_pop_priority = ctx->odp_actions->size;
2151 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2152 const struct ofp_action_enqueue *oae)
2154 uint16_t ofp_port, odp_port;
2158 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2161 /* Fall back to ordinary output action. */
2162 xlate_output_action__(ctx, ntohs(oae->port), 0);
2166 /* Figure out ODP output port. */
2167 ofp_port = ntohs(oae->port);
2168 if (ofp_port != OFPP_IN_PORT) {
2169 odp_port = ofp_port_to_odp_port(ofp_port);
2171 odp_port = ctx->flow.in_port;
2174 /* Add ODP actions. */
2175 remove_pop_action(ctx);
2176 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2177 add_output_action(ctx, odp_port);
2178 add_pop_action(ctx);
2180 /* Update NetFlow output port. */
2181 if (ctx->nf_output_iface == NF_OUT_DROP) {
2182 ctx->nf_output_iface = odp_port;
2183 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2184 ctx->nf_output_iface = NF_OUT_MULTI;
2189 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2190 const struct nx_action_set_queue *nasq)
2195 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2198 /* Couldn't translate queue to a priority, so ignore. A warning
2199 * has already been logged. */
2203 remove_pop_action(ctx);
2204 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2208 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2210 ovs_be16 tci = ctx->flow.vlan_tci;
2211 if (!(tci & htons(VLAN_CFI))) {
2212 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2214 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2215 tci & ~htons(VLAN_CFI));
2219 struct xlate_reg_state {
2225 save_reg_state(const struct action_xlate_ctx *ctx,
2226 struct xlate_reg_state *state)
2228 state->vlan_tci = ctx->flow.vlan_tci;
2229 state->tun_id = ctx->flow.tun_id;
2233 update_reg_state(struct action_xlate_ctx *ctx,
2234 const struct xlate_reg_state *state)
2236 if (ctx->flow.vlan_tci != state->vlan_tci) {
2237 xlate_set_dl_tci(ctx);
2239 if (ctx->flow.tun_id != state->tun_id) {
2240 nl_msg_put_be64(ctx->odp_actions,
2241 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2246 xlate_nicira_action(struct action_xlate_ctx *ctx,
2247 const struct nx_action_header *nah)
2249 const struct nx_action_resubmit *nar;
2250 const struct nx_action_set_tunnel *nast;
2251 const struct nx_action_set_queue *nasq;
2252 const struct nx_action_multipath *nam;
2253 enum nx_action_subtype subtype = ntohs(nah->subtype);
2254 struct xlate_reg_state state;
2257 assert(nah->vendor == htonl(NX_VENDOR_ID));
2259 case NXAST_RESUBMIT:
2260 nar = (const struct nx_action_resubmit *) nah;
2261 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2264 case NXAST_SET_TUNNEL:
2265 nast = (const struct nx_action_set_tunnel *) nah;
2266 tun_id = htonll(ntohl(nast->tun_id));
2267 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2268 ctx->flow.tun_id = tun_id;
2271 case NXAST_DROP_SPOOFED_ARP:
2272 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2273 nl_msg_put_flag(ctx->odp_actions,
2274 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2278 case NXAST_SET_QUEUE:
2279 nasq = (const struct nx_action_set_queue *) nah;
2280 xlate_set_queue_action(ctx, nasq);
2283 case NXAST_POP_QUEUE:
2284 add_pop_action(ctx);
2287 case NXAST_REG_MOVE:
2288 save_reg_state(ctx, &state);
2289 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2291 update_reg_state(ctx, &state);
2294 case NXAST_REG_LOAD:
2295 save_reg_state(ctx, &state);
2296 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2298 update_reg_state(ctx, &state);
2302 /* Nothing to do. */
2305 case NXAST_SET_TUNNEL64:
2306 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2307 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2308 ctx->flow.tun_id = tun_id;
2311 case NXAST_MULTIPATH:
2312 nam = (const struct nx_action_multipath *) nah;
2313 multipath_execute(nam, &ctx->flow);
2316 /* If you add a new action here that modifies flow data, don't forget to
2317 * update the flow key in ctx->flow at the same time. */
2319 case NXAST_SNAT__OBSOLETE:
2321 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2327 do_xlate_actions(const union ofp_action *in, size_t n_in,
2328 struct action_xlate_ctx *ctx)
2330 struct actions_iterator iter;
2331 const union ofp_action *ia;
2332 const struct ofport *port;
2334 port = get_port(ctx->ofproto, ctx->flow.in_port);
2335 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2336 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2337 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
2338 /* Drop this flow. */
2342 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2343 enum ofp_action_type type = ntohs(ia->type);
2344 const struct ofp_action_dl_addr *oada;
2348 xlate_output_action(ctx, &ia->output);
2351 case OFPAT_SET_VLAN_VID:
2352 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2353 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2354 xlate_set_dl_tci(ctx);
2357 case OFPAT_SET_VLAN_PCP:
2358 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2359 ctx->flow.vlan_tci |= htons(
2360 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2361 xlate_set_dl_tci(ctx);
2364 case OFPAT_STRIP_VLAN:
2365 ctx->flow.vlan_tci = htons(0);
2366 xlate_set_dl_tci(ctx);
2369 case OFPAT_SET_DL_SRC:
2370 oada = ((struct ofp_action_dl_addr *) ia);
2371 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2372 oada->dl_addr, ETH_ADDR_LEN);
2373 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
2376 case OFPAT_SET_DL_DST:
2377 oada = ((struct ofp_action_dl_addr *) ia);
2378 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2379 oada->dl_addr, ETH_ADDR_LEN);
2380 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
2383 case OFPAT_SET_NW_SRC:
2384 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
2385 ia->nw_addr.nw_addr);
2386 ctx->flow.nw_src = ia->nw_addr.nw_addr;
2389 case OFPAT_SET_NW_DST:
2390 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
2391 ia->nw_addr.nw_addr);
2392 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
2395 case OFPAT_SET_NW_TOS:
2396 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
2398 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
2401 case OFPAT_SET_TP_SRC:
2402 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
2403 ia->tp_port.tp_port);
2404 ctx->flow.tp_src = ia->tp_port.tp_port;
2407 case OFPAT_SET_TP_DST:
2408 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
2409 ia->tp_port.tp_port);
2410 ctx->flow.tp_dst = ia->tp_port.tp_port;
2414 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2418 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
2422 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
2429 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
2430 struct ofproto *ofproto, const struct flow *flow,
2431 const struct ofpbuf *packet)
2433 ctx->ofproto = ofproto;
2435 ctx->packet = packet;
2436 ctx->resubmit_hook = NULL;
2437 ctx->check_special = true;
2441 ofproto_process_cfm(struct ofproto *ofproto, const struct flow *flow,
2442 const struct ofpbuf *packet)
2444 struct ofport *ofport;
2446 ofport = get_port(ofproto, flow->in_port);
2447 if (ofport && ofport->cfm) {
2448 cfm_process_heartbeat(ofport->cfm, packet);
2452 static struct ofpbuf *
2453 xlate_actions(struct action_xlate_ctx *ctx,
2454 const union ofp_action *in, size_t n_in)
2456 COVERAGE_INC(ofproto_ofp2odp);
2458 ctx->odp_actions = ofpbuf_new(512);
2460 ctx->may_set_up_flow = true;
2461 ctx->nf_output_iface = NF_OUT_DROP;
2463 ctx->last_pop_priority = -1;
2465 if (ctx->check_special && cfm_should_process_flow(&ctx->flow)) {
2467 ofproto_process_cfm(ctx->ofproto, &ctx->flow, ctx->packet);
2469 ctx->may_set_up_flow = false;
2470 } else if (ctx->check_special
2471 && ctx->ofproto->ofhooks->special_cb
2472 && !ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet,
2473 ctx->ofproto->aux)) {
2474 ctx->may_set_up_flow = false;
2476 do_xlate_actions(in, n_in, ctx);
2479 remove_pop_action(ctx);
2481 /* Check with in-band control to see if we're allowed to set up this
2483 if (!connmgr_may_set_up_flow(ctx->ofproto->connmgr, &ctx->flow,
2484 ctx->odp_actions->data,
2485 ctx->odp_actions->size)) {
2486 ctx->may_set_up_flow = false;
2489 return ctx->odp_actions;
2492 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2493 * error message code (composed with ofp_mkerr()) for the caller to propagate
2494 * upward. Otherwise, returns 0.
2496 * The log message mentions 'msg_type'. */
2498 reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
2500 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2501 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
2502 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
2503 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
2506 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2513 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2515 struct ofproto *p = ofconn_get_ofproto(ofconn);
2516 struct ofp_packet_out *opo;
2517 struct ofpbuf payload, *buffer;
2518 union ofp_action *ofp_actions;
2519 struct action_xlate_ctx ctx;
2520 struct ofpbuf *odp_actions;
2521 struct ofpbuf request;
2523 size_t n_ofp_actions;
2527 COVERAGE_INC(ofproto_packet_out);
2529 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
2534 /* Get ofp_packet_out. */
2535 ofpbuf_use_const(&request, oh, ntohs(oh->length));
2536 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
2539 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
2540 &ofp_actions, &n_ofp_actions);
2546 if (opo->buffer_id != htonl(UINT32_MAX)) {
2547 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
2549 if (error || !buffer) {
2558 /* Extract flow, check actions. */
2559 flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
2561 error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
2567 action_xlate_ctx_init(&ctx, p, &flow, &payload);
2568 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
2569 dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
2570 ofpbuf_delete(odp_actions);
2573 ofpbuf_delete(buffer);
2578 update_port_config(struct ofproto *p, struct ofport *port,
2579 uint32_t config, uint32_t mask)
2581 mask &= config ^ port->opp.config;
2582 if (mask & OFPPC_PORT_DOWN) {
2583 if (config & OFPPC_PORT_DOWN) {
2584 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2586 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2589 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
2590 OFPPC_NO_FWD | OFPPC_NO_FLOOD)
2591 if (mask & REVALIDATE_BITS) {
2592 COVERAGE_INC(ofproto_costly_flags);
2593 port->opp.config ^= mask & REVALIDATE_BITS;
2594 p->need_revalidate = true;
2596 #undef REVALIDATE_BITS
2597 if (mask & OFPPC_NO_PACKET_IN) {
2598 port->opp.config ^= OFPPC_NO_PACKET_IN;
2603 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2605 struct ofproto *p = ofconn_get_ofproto(ofconn);
2606 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
2607 struct ofport *port;
2610 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
2615 port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
2617 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2618 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2619 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2621 update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
2622 if (opm->advertise) {
2623 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2629 static struct ofpbuf *
2630 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
2632 struct ofp_stats_reply *osr;
2635 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2636 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2638 osr->flags = htons(0);
2642 static struct ofpbuf *
2643 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
2645 const struct ofp_stats_request *osr
2646 = (const struct ofp_stats_request *) request;
2647 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
2651 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
2652 struct ofpbuf **msgp)
2654 struct ofpbuf *msg = *msgp;
2655 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2656 if (nbytes + msg->size > UINT16_MAX) {
2657 struct ofp_stats_reply *reply = msg->data;
2658 reply->flags = htons(OFPSF_REPLY_MORE);
2659 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
2660 ofconn_send_reply(ofconn, msg);
2662 return ofpbuf_put_uninit(*msgp, nbytes);
2665 static struct ofpbuf *
2666 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
2668 struct nicira_stats_msg *nsm;
2671 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
2672 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
2673 nsm->type = htons(OFPST_VENDOR);
2674 nsm->flags = htons(0);
2675 nsm->vendor = htonl(NX_VENDOR_ID);
2676 nsm->subtype = subtype;
2680 static struct ofpbuf *
2681 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
2683 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
2687 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
2688 struct ofpbuf **msgp)
2690 struct ofpbuf *msg = *msgp;
2691 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
2692 if (nbytes + msg->size > UINT16_MAX) {
2693 struct nicira_stats_msg *reply = msg->data;
2694 reply->flags = htons(OFPSF_REPLY_MORE);
2695 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
2696 ofconn_send_reply(ofconn, msg);
2698 ofpbuf_prealloc_tailroom(*msgp, nbytes);
2702 handle_desc_stats_request(struct ofconn *ofconn,
2703 const struct ofp_header *request)
2705 struct ofproto *p = ofconn_get_ofproto(ofconn);
2706 struct ofp_desc_stats *ods;
2709 msg = start_ofp_stats_reply(request, sizeof *ods);
2710 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
2711 memset(ods, 0, sizeof *ods);
2712 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
2713 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
2714 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
2715 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
2716 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2717 ofconn_send_reply(ofconn, msg);
2723 handle_table_stats_request(struct ofconn *ofconn,
2724 const struct ofp_header *request)
2726 struct ofproto *p = ofconn_get_ofproto(ofconn);
2727 struct ofp_table_stats *ots;
2730 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
2732 /* Classifier table. */
2733 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
2734 memset(ots, 0, sizeof *ots);
2735 strcpy(ots->name, "classifier");
2736 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
2737 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
2738 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
2739 ots->active_count = htonl(classifier_count(&p->cls));
2740 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
2741 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
2743 ofconn_send_reply(ofconn, msg);
2748 append_port_stat(struct ofport *port, struct ofconn *ofconn,
2749 struct ofpbuf **msgp)
2751 struct netdev_stats stats;
2752 struct ofp_port_stats *ops;
2754 /* Intentionally ignore return value, since errors will set
2755 * 'stats' to all-1s, which is correct for OpenFlow, and
2756 * netdev_get_stats() will log errors. */
2757 netdev_get_stats(port->netdev, &stats);
2759 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
2760 ops->port_no = htons(port->opp.port_no);
2761 memset(ops->pad, 0, sizeof ops->pad);
2762 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
2763 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
2764 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
2765 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
2766 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
2767 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
2768 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
2769 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
2770 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
2771 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
2772 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
2773 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
2777 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2779 struct ofproto *p = ofconn_get_ofproto(ofconn);
2780 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
2781 struct ofp_port_stats *ops;
2783 struct ofport *port;
2785 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
2786 if (psr->port_no != htons(OFPP_NONE)) {
2787 port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
2789 append_port_stat(port, ofconn, &msg);
2792 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2793 append_port_stat(port, ofconn, &msg);
2797 ofconn_send_reply(ofconn, msg);
2802 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
2804 long long int msecs = time_msec() - start;
2805 *sec = msecs / 1000;
2806 *nsec = (msecs % 1000) * (1000 * 1000);
2810 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
2814 calc_flow_duration__(start, &sec, &nsec);
2815 *sec_be = htonl(sec);
2816 *nsec_be = htonl(nsec);
2820 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
2821 ovs_be16 out_port, struct ofpbuf **replyp)
2823 struct ofp_flow_stats *ofs;
2824 uint64_t packet_count, byte_count;
2826 size_t act_len, len;
2828 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2832 act_len = sizeof *rule->actions * rule->n_actions;
2833 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2835 rule_get_stats(rule, &packet_count, &byte_count);
2837 ofs = append_ofp_stats_reply(len, ofconn, replyp);
2838 ofs->length = htons(len);
2841 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
2842 &ofs->match, rule->flow_cookie, &cookie);
2843 put_32aligned_be64(&ofs->cookie, cookie);
2844 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
2845 ofs->priority = htons(rule->cr.priority);
2846 ofs->idle_timeout = htons(rule->idle_timeout);
2847 ofs->hard_timeout = htons(rule->hard_timeout);
2848 memset(ofs->pad2, 0, sizeof ofs->pad2);
2849 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
2850 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
2851 if (rule->n_actions > 0) {
2852 memcpy(ofs->actions, rule->actions, act_len);
2857 is_valid_table(uint8_t table_id)
2859 if (table_id == 0 || table_id == 0xff) {
2862 /* It would probably be better to reply with an error but there doesn't
2863 * seem to be any appropriate value, so that might just be
2865 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
2872 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2874 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
2875 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2876 struct ofpbuf *reply;
2878 COVERAGE_INC(ofproto_flows_req);
2879 reply = start_ofp_stats_reply(oh, 1024);
2880 if (is_valid_table(fsr->table_id)) {
2881 struct cls_cursor cursor;
2882 struct cls_rule target;
2885 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
2887 cls_cursor_init(&cursor, &ofproto->cls, &target);
2888 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2889 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
2892 ofconn_send_reply(ofconn, reply);
2898 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
2899 ovs_be16 out_port, struct ofpbuf **replyp)
2901 struct nx_flow_stats *nfs;
2902 uint64_t packet_count, byte_count;
2903 size_t act_len, start_len;
2904 struct ofpbuf *reply;
2906 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2910 rule_get_stats(rule, &packet_count, &byte_count);
2912 act_len = sizeof *rule->actions * rule->n_actions;
2914 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
2915 start_len = (*replyp)->size;
2918 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
2921 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
2922 nfs->cookie = rule->flow_cookie;
2923 nfs->priority = htons(rule->cr.priority);
2924 nfs->idle_timeout = htons(rule->idle_timeout);
2925 nfs->hard_timeout = htons(rule->hard_timeout);
2926 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
2927 memset(nfs->pad2, 0, sizeof nfs->pad2);
2928 nfs->packet_count = htonll(packet_count);
2929 nfs->byte_count = htonll(byte_count);
2930 if (rule->n_actions > 0) {
2931 ofpbuf_put(reply, rule->actions, act_len);
2933 nfs->length = htons(reply->size - start_len);
2937 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
2939 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2940 struct nx_flow_stats_request *nfsr;
2941 struct cls_rule target;
2942 struct ofpbuf *reply;
2946 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2948 /* Dissect the message. */
2949 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2950 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
2955 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2958 COVERAGE_INC(ofproto_flows_req);
2959 reply = start_nxstats_reply(&nfsr->nsm, 1024);
2960 if (is_valid_table(nfsr->table_id)) {
2961 struct cls_cursor cursor;
2964 cls_cursor_init(&cursor, &ofproto->cls, &target);
2965 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2966 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
2969 ofconn_send_reply(ofconn, reply);
2975 flow_stats_ds(struct rule *rule, struct ds *results)
2977 uint64_t packet_count, byte_count;
2978 size_t act_len = sizeof *rule->actions * rule->n_actions;
2980 rule_get_stats(rule, &packet_count, &byte_count);
2982 ds_put_format(results, "duration=%llds, ",
2983 (time_msec() - rule->created) / 1000);
2984 ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0);
2985 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2986 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2987 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2988 cls_rule_format(&rule->cr, results);
2989 ds_put_char(results, ',');
2991 ofp_print_actions(results, &rule->actions->header, act_len);
2993 ds_put_cstr(results, "drop");
2995 ds_put_cstr(results, "\n");
2998 /* Adds a pretty-printed description of all flows to 'results', including
2999 * hidden flows (e.g., set up by in-band control). */
3001 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3003 struct cls_cursor cursor;
3006 cls_cursor_init(&cursor, &p->cls, NULL);
3007 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3008 flow_stats_ds(rule, results);
3013 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
3014 ovs_be16 out_port, uint8_t table_id,
3015 struct ofp_aggregate_stats_reply *oasr)
3017 uint64_t total_packets = 0;
3018 uint64_t total_bytes = 0;
3021 COVERAGE_INC(ofproto_agg_request);
3023 if (is_valid_table(table_id)) {
3024 struct cls_cursor cursor;
3027 cls_cursor_init(&cursor, &ofproto->cls, target);
3028 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3029 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
3030 uint64_t packet_count;
3031 uint64_t byte_count;
3033 rule_get_stats(rule, &packet_count, &byte_count);
3035 total_packets += packet_count;
3036 total_bytes += byte_count;
3042 oasr->flow_count = htonl(n_flows);
3043 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
3044 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
3045 memset(oasr->pad, 0, sizeof oasr->pad);
3049 handle_aggregate_stats_request(struct ofconn *ofconn,
3050 const struct ofp_header *oh)
3052 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
3053 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3054 struct ofp_aggregate_stats_reply *reply;
3055 struct cls_rule target;
3058 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
3061 msg = start_ofp_stats_reply(oh, sizeof *reply);
3062 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
3063 query_aggregate_stats(ofproto, &target, request->out_port,
3064 request->table_id, reply);
3065 ofconn_send_reply(ofconn, msg);
3070 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
3072 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3073 struct nx_aggregate_stats_request *request;
3074 struct ofp_aggregate_stats_reply *reply;
3075 struct cls_rule target;
3080 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3082 /* Dissect the message. */
3083 request = ofpbuf_pull(&b, sizeof *request);
3084 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
3089 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3093 COVERAGE_INC(ofproto_flows_req);
3094 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
3095 reply = ofpbuf_put_uninit(buf, sizeof *reply);
3096 query_aggregate_stats(ofproto, &target, request->out_port,
3097 request->table_id, reply);
3098 ofconn_send_reply(ofconn, buf);
3103 struct queue_stats_cbdata {
3104 struct ofconn *ofconn;
3105 struct ofport *ofport;
3110 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3111 const struct netdev_queue_stats *stats)
3113 struct ofp_queue_stats *reply;
3115 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
3116 reply->port_no = htons(cbdata->ofport->opp.port_no);
3117 memset(reply->pad, 0, sizeof reply->pad);
3118 reply->queue_id = htonl(queue_id);
3119 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
3120 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
3121 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
3125 handle_queue_stats_dump_cb(uint32_t queue_id,
3126 struct netdev_queue_stats *stats,
3129 struct queue_stats_cbdata *cbdata = cbdata_;
3131 put_queue_stats(cbdata, queue_id, stats);
3135 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3136 struct queue_stats_cbdata *cbdata)
3138 cbdata->ofport = port;
3139 if (queue_id == OFPQ_ALL) {
3140 netdev_dump_queue_stats(port->netdev,
3141 handle_queue_stats_dump_cb, cbdata);
3143 struct netdev_queue_stats stats;
3145 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3146 put_queue_stats(cbdata, queue_id, &stats);
3152 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
3154 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3155 const struct ofp_queue_stats_request *qsr;
3156 struct queue_stats_cbdata cbdata;
3157 struct ofport *port;
3158 unsigned int port_no;
3161 qsr = ofputil_stats_body(oh);
3163 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3166 COVERAGE_INC(ofproto_queue_req);
3168 cbdata.ofconn = ofconn;
3169 cbdata.msg = start_ofp_stats_reply(oh, 128);
3171 port_no = ntohs(qsr->port_no);
3172 queue_id = ntohl(qsr->queue_id);
3173 if (port_no == OFPP_ALL) {
3174 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3175 handle_queue_stats_for_port(port, queue_id, &cbdata);
3177 } else if (port_no < ofproto->max_ports) {
3178 port = get_port(ofproto, ofp_port_to_odp_port(port_no));
3180 handle_queue_stats_for_port(port, queue_id, &cbdata);
3183 ofpbuf_delete(cbdata.msg);
3184 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
3186 ofconn_send_reply(ofconn, cbdata.msg);
3191 /* Updates 'facet''s used time. Caller is responsible for calling
3192 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3194 facet_update_time(struct ofproto *ofproto, struct facet *facet,
3197 if (used > facet->used) {
3199 if (used > facet->rule->used) {
3200 facet->rule->used = used;
3202 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3206 /* Folds the statistics from 'stats' into the counters in 'facet'.
3208 * Because of the meaning of a facet's counters, it only makes sense to do this
3209 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3210 * packet that was sent by hand or if it represents statistics that have been
3211 * cleared out of the datapath. */
3213 facet_update_stats(struct ofproto *ofproto, struct facet *facet,
3214 const struct dpif_flow_stats *stats)
3216 if (stats->n_packets || stats->used > facet->used) {
3217 facet_update_time(ofproto, facet, stats->used);
3218 facet->packet_count += stats->n_packets;
3219 facet->byte_count += stats->n_bytes;
3220 facet_push_stats(ofproto, facet);
3221 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3226 facet_push_stats(struct ofproto *ofproto, struct facet *facet)
3228 uint64_t rs_packets, rs_bytes;
3230 assert(facet->packet_count >= facet->rs_packet_count);
3231 assert(facet->byte_count >= facet->rs_byte_count);
3232 assert(facet->used >= facet->rs_used);
3234 rs_packets = facet->packet_count - facet->rs_packet_count;
3235 rs_bytes = facet->byte_count - facet->rs_byte_count;
3237 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3238 facet->rs_packet_count = facet->packet_count;
3239 facet->rs_byte_count = facet->byte_count;
3240 facet->rs_used = facet->used;
3242 flow_push_stats(ofproto, facet->rule, &facet->flow,
3243 rs_packets, rs_bytes, facet->used);
3247 struct ofproto_push {
3248 struct action_xlate_ctx ctx;
3255 push_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
3257 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3260 rule->packet_count += push->packets;
3261 rule->byte_count += push->bytes;
3262 rule->used = MAX(push->used, rule->used);
3266 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3267 * 'rule''s actions. */
3269 flow_push_stats(struct ofproto *ofproto, const struct rule *rule,
3270 struct flow *flow, uint64_t packets, uint64_t bytes,
3273 struct ofproto_push push;
3275 push.packets = packets;
3279 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3280 push.ctx.resubmit_hook = push_resubmit;
3281 ofpbuf_delete(xlate_actions(&push.ctx, rule->actions, rule->n_actions));
3284 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3285 * in which no matching flow already exists in the flow table.
3287 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3288 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
3289 * OpenFlow error code as encoded by ofp_mkerr() on failure.
3291 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3294 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
3296 struct ofproto *p = ofconn_get_ofproto(ofconn);
3297 struct ofpbuf *packet;
3302 if (fm->flags & OFPFF_CHECK_OVERLAP
3303 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
3304 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
3308 if (fm->buffer_id != UINT32_MAX) {
3309 error = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id,
3313 in_port = UINT16_MAX;
3316 rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
3317 fm->idle_timeout, fm->hard_timeout, fm->cookie,
3318 fm->flags & OFPFF_SEND_FLOW_REM);
3319 rule_insert(p, rule);
3321 rule_execute(p, rule, in_port, packet);
3326 static struct rule *
3327 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
3329 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
3333 send_buffered_packet(struct ofconn *ofconn,
3334 struct rule *rule, uint32_t buffer_id)
3336 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3337 struct ofpbuf *packet;
3341 if (buffer_id == UINT32_MAX) {
3345 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
3350 rule_execute(ofproto, rule, in_port, packet);
3355 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3357 struct modify_flows_cbdata {
3358 struct ofproto *ofproto;
3359 const struct flow_mod *fm;
3363 static int modify_flow(struct ofproto *, const struct flow_mod *,
3366 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
3367 * encoded by ofp_mkerr() on failure.
3369 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3372 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
3374 struct ofproto *p = ofconn_get_ofproto(ofconn);
3375 struct rule *match = NULL;
3376 struct cls_cursor cursor;
3379 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3380 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3381 if (!rule_is_hidden(rule)) {
3383 modify_flow(p, fm, rule);
3388 /* This credits the packet to whichever flow happened to match last.
3389 * That's weird. Maybe we should do a lookup for the flow that
3390 * actually matches the packet? Who knows. */
3391 send_buffered_packet(ofconn, match, fm->buffer_id);
3394 return add_flow(ofconn, fm);
3398 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3399 * code as encoded by ofp_mkerr() on failure.
3401 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3404 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
3406 struct ofproto *p = ofconn_get_ofproto(ofconn);
3407 struct rule *rule = find_flow_strict(p, fm);
3408 if (rule && !rule_is_hidden(rule)) {
3409 modify_flow(p, fm, rule);
3410 return send_buffered_packet(ofconn, rule, fm->buffer_id);
3412 return add_flow(ofconn, fm);
3416 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
3417 * been identified as a flow in 'p''s flow table to be modified, by changing
3418 * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
3419 * ofp_action[] structures). */
3421 modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
3423 size_t actions_len = fm->n_actions * sizeof *rule->actions;
3425 rule->flow_cookie = fm->cookie;
3427 /* If the actions are the same, do nothing. */
3428 if (fm->n_actions == rule->n_actions
3430 || !memcmp(fm->actions, rule->actions, actions_len))) {
3434 /* Replace actions. */
3435 free(rule->actions);
3436 rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
3437 rule->n_actions = fm->n_actions;
3439 p->need_revalidate = true;
3444 /* OFPFC_DELETE implementation. */
3446 static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
3448 /* Implements OFPFC_DELETE. */
3450 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
3452 struct rule *rule, *next_rule;
3453 struct cls_cursor cursor;
3455 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3456 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3457 delete_flow(p, rule, htons(fm->out_port));
3461 /* Implements OFPFC_DELETE_STRICT. */
3463 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
3465 struct rule *rule = find_flow_strict(p, fm);
3467 delete_flow(p, rule, htons(fm->out_port));
3471 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
3472 * been identified as a flow to delete from 'p''s flow table, by deleting the
3473 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
3476 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
3477 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
3478 * specified 'out_port'. */
3480 delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
3482 if (rule_is_hidden(rule)) {
3486 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
3490 rule_send_removed(p, rule, OFPRR_DELETE);
3491 rule_remove(p, rule);
3495 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3497 struct ofproto *p = ofconn_get_ofproto(ofconn);
3501 error = reject_slave_controller(ofconn, "flow_mod");
3506 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
3511 /* We do not support the emergency flow cache. It will hopefully get
3512 * dropped from OpenFlow in the near future. */
3513 if (fm.flags & OFPFF_EMERG) {
3514 /* There isn't a good fit for an error code, so just state that the
3515 * flow table is full. */
3516 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
3519 error = validate_actions(fm.actions, fm.n_actions,
3520 &fm.cr.flow, p->max_ports);
3525 switch (fm.command) {
3527 return add_flow(ofconn, &fm);
3530 return modify_flows_loose(ofconn, &fm);
3532 case OFPFC_MODIFY_STRICT:
3533 return modify_flow_strict(ofconn, &fm);
3536 delete_flows_loose(p, &fm);
3539 case OFPFC_DELETE_STRICT:
3540 delete_flow_strict(p, &fm);
3544 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
3549 handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
3551 const struct nxt_tun_id_cookie *msg
3552 = (const struct nxt_tun_id_cookie *) oh;
3553 enum nx_flow_format flow_format;
3555 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
3556 ofconn_set_flow_format(ofconn, flow_format);
3562 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3564 struct nx_role_request *nrr = (struct nx_role_request *) oh;
3565 struct nx_role_request *reply;
3569 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
3570 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
3571 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3574 role = ntohl(nrr->role);
3575 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
3576 && role != NX_ROLE_SLAVE) {
3577 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
3579 /* There's no good error code for this. */
3580 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
3583 ofconn_set_role(ofconn, role);
3585 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
3586 reply->role = htonl(role);
3587 ofconn_send_reply(ofconn, buf);
3593 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
3595 const struct nxt_set_flow_format *msg
3596 = (const struct nxt_set_flow_format *) oh;
3599 format = ntohl(msg->format);
3600 if (format == NXFF_OPENFLOW10
3601 || format == NXFF_TUN_ID_FROM_COOKIE
3602 || format == NXFF_NXM) {
3603 ofconn_set_flow_format(ofconn, format);
3606 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3611 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
3613 struct ofp_header *ob;
3616 /* Currently, everything executes synchronously, so we can just
3617 * immediately send the barrier reply. */
3618 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
3619 ofconn_send_reply(ofconn, buf);
3624 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
3626 const struct ofp_header *oh = msg->data;
3627 const struct ofputil_msg_type *type;
3630 error = ofputil_decode_msg_type(oh, &type);
3635 switch (ofputil_msg_type_code(type)) {
3636 /* OpenFlow requests. */
3637 case OFPUTIL_OFPT_ECHO_REQUEST:
3638 return handle_echo_request(ofconn, oh);
3640 case OFPUTIL_OFPT_FEATURES_REQUEST:
3641 return handle_features_request(ofconn, oh);
3643 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
3644 return handle_get_config_request(ofconn, oh);
3646 case OFPUTIL_OFPT_SET_CONFIG:
3647 return handle_set_config(ofconn, msg->data);
3649 case OFPUTIL_OFPT_PACKET_OUT:
3650 return handle_packet_out(ofconn, oh);
3652 case OFPUTIL_OFPT_PORT_MOD:
3653 return handle_port_mod(ofconn, oh);
3655 case OFPUTIL_OFPT_FLOW_MOD:
3656 return handle_flow_mod(ofconn, oh);
3658 case OFPUTIL_OFPT_BARRIER_REQUEST:
3659 return handle_barrier_request(ofconn, oh);
3661 /* OpenFlow replies. */
3662 case OFPUTIL_OFPT_ECHO_REPLY:
3665 /* Nicira extension requests. */
3666 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
3667 return handle_tun_id_from_cookie(ofconn, oh);
3669 case OFPUTIL_NXT_ROLE_REQUEST:
3670 return handle_role_request(ofconn, oh);
3672 case OFPUTIL_NXT_SET_FLOW_FORMAT:
3673 return handle_nxt_set_flow_format(ofconn, oh);
3675 case OFPUTIL_NXT_FLOW_MOD:
3676 return handle_flow_mod(ofconn, oh);
3678 /* OpenFlow statistics requests. */
3679 case OFPUTIL_OFPST_DESC_REQUEST:
3680 return handle_desc_stats_request(ofconn, oh);
3682 case OFPUTIL_OFPST_FLOW_REQUEST:
3683 return handle_flow_stats_request(ofconn, oh);
3685 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
3686 return handle_aggregate_stats_request(ofconn, oh);
3688 case OFPUTIL_OFPST_TABLE_REQUEST:
3689 return handle_table_stats_request(ofconn, oh);
3691 case OFPUTIL_OFPST_PORT_REQUEST:
3692 return handle_port_stats_request(ofconn, oh);
3694 case OFPUTIL_OFPST_QUEUE_REQUEST:
3695 return handle_queue_stats_request(ofconn, oh);
3697 /* Nicira extension statistics requests. */
3698 case OFPUTIL_NXST_FLOW_REQUEST:
3699 return handle_nxst_flow(ofconn, oh);
3701 case OFPUTIL_NXST_AGGREGATE_REQUEST:
3702 return handle_nxst_aggregate(ofconn, oh);
3704 case OFPUTIL_INVALID:
3705 case OFPUTIL_OFPT_HELLO:
3706 case OFPUTIL_OFPT_ERROR:
3707 case OFPUTIL_OFPT_FEATURES_REPLY:
3708 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
3709 case OFPUTIL_OFPT_PACKET_IN:
3710 case OFPUTIL_OFPT_FLOW_REMOVED:
3711 case OFPUTIL_OFPT_PORT_STATUS:
3712 case OFPUTIL_OFPT_BARRIER_REPLY:
3713 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
3714 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
3715 case OFPUTIL_OFPST_DESC_REPLY:
3716 case OFPUTIL_OFPST_FLOW_REPLY:
3717 case OFPUTIL_OFPST_QUEUE_REPLY:
3718 case OFPUTIL_OFPST_PORT_REPLY:
3719 case OFPUTIL_OFPST_TABLE_REPLY:
3720 case OFPUTIL_OFPST_AGGREGATE_REPLY:
3721 case OFPUTIL_NXT_ROLE_REPLY:
3722 case OFPUTIL_NXT_FLOW_REMOVED:
3723 case OFPUTIL_NXST_FLOW_REPLY:
3724 case OFPUTIL_NXST_AGGREGATE_REPLY:
3726 if (VLOG_IS_WARN_ENABLED()) {
3727 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3728 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3731 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
3732 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
3734 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3740 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
3742 int error = handle_openflow__(ofconn, ofp_msg);
3744 send_error_oh(ofconn, ofp_msg->data, error);
3746 COVERAGE_INC(ofproto_recv_openflow);
3750 handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3752 struct facet *facet;
3755 /* Obtain in_port and tun_id, at least. */
3756 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3758 /* Set header pointers in 'flow'. */
3759 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
3761 if (cfm_should_process_flow(&flow)) {
3762 ofproto_process_cfm(p, &flow, upcall->packet);
3763 ofpbuf_delete(upcall->packet);
3765 } else if (p->ofhooks->special_cb
3766 && !p->ofhooks->special_cb(&flow, upcall->packet, p->aux)) {
3767 ofpbuf_delete(upcall->packet);
3771 /* Check with in-band control to see if this packet should be sent
3772 * to the local port regardless of the flow table. */
3773 if (connmgr_msg_in_hook(p->connmgr, &flow, upcall->packet)) {
3774 ofproto_send_packet(p, ODPP_LOCAL, 0, upcall->packet);
3777 facet = facet_lookup_valid(p, &flow);
3779 struct rule *rule = rule_lookup(p, &flow);
3781 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3782 struct ofport *port = get_port(p, flow.in_port);
3784 if (port->opp.config & OFPPC_NO_PACKET_IN) {
3785 COVERAGE_INC(ofproto_no_packet_in);
3786 /* XXX install 'drop' flow entry */
3787 ofpbuf_delete(upcall->packet);
3791 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
3795 COVERAGE_INC(ofproto_packet_in);
3796 send_packet_in(p, upcall, &flow, false);
3800 facet = facet_create(p, rule, &flow, upcall->packet);
3801 } else if (!facet->may_install) {
3802 /* The facet is not installable, that is, we need to process every
3803 * packet, so process the current packet's actions into 'facet'. */
3804 facet_make_actions(p, facet, upcall->packet);
3807 if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) {
3809 * Extra-special case for fail-open mode.
3811 * We are in fail-open mode and the packet matched the fail-open rule,
3812 * but we are connected to a controller too. We should send the packet
3813 * up to the controller in the hope that it will try to set up a flow
3814 * and thereby allow us to exit fail-open.
3816 * See the top-level comment in fail-open.c for more information.
3818 send_packet_in(p, upcall, &flow, true);
3821 facet_execute(p, facet, upcall->packet);
3822 facet_install(p, facet, false);
3826 handle_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3830 switch (upcall->type) {
3831 case DPIF_UC_ACTION:
3832 COVERAGE_INC(ofproto_ctlr_action);
3833 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3834 send_packet_in(p, upcall, &flow, false);
3837 case DPIF_UC_SAMPLE:
3839 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3840 ofproto_sflow_received(p->sflow, upcall, &flow);
3842 ofpbuf_delete(upcall->packet);
3846 handle_miss_upcall(p, upcall);
3849 case DPIF_N_UC_TYPES:
3851 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3856 /* Flow expiration. */
3858 static int ofproto_dp_max_idle(const struct ofproto *);
3859 static void ofproto_update_stats(struct ofproto *);
3860 static void rule_expire(struct ofproto *, struct rule *);
3861 static void ofproto_expire_facets(struct ofproto *, int dp_max_idle);
3863 /* This function is called periodically by ofproto_run(). Its job is to
3864 * collect updates for the flows that have been installed into the datapath,
3865 * most importantly when they last were used, and then use that information to
3866 * expire flows that have not been used recently.
3868 * Returns the number of milliseconds after which it should be called again. */
3870 ofproto_expire(struct ofproto *ofproto)
3872 struct rule *rule, *next_rule;
3873 struct cls_cursor cursor;
3876 /* Update stats for each flow in the datapath. */
3877 ofproto_update_stats(ofproto);
3879 /* Expire facets that have been idle too long. */
3880 dp_max_idle = ofproto_dp_max_idle(ofproto);
3881 ofproto_expire_facets(ofproto, dp_max_idle);
3883 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3884 cls_cursor_init(&cursor, &ofproto->cls, NULL);
3885 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3886 rule_expire(ofproto, rule);
3889 /* Let the hook know that we're at a stable point: all outstanding data
3890 * in existing flows has been accounted to the account_cb. Thus, the
3891 * hook can now reasonably do operations that depend on having accurate
3892 * flow volume accounting (currently, that's just bond rebalancing). */
3893 if (ofproto->ofhooks->account_checkpoint_cb) {
3894 ofproto->ofhooks->account_checkpoint_cb(ofproto->aux);
3897 return MIN(dp_max_idle, 1000);
3900 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3902 * This function also pushes statistics updates to rules which each facet
3903 * resubmits into. Generally these statistics will be accurate. However, if a
3904 * facet changes the rule it resubmits into at some time in between
3905 * ofproto_update_stats() runs, it is possible that statistics accrued to the
3906 * old rule will be incorrectly attributed to the new rule. This could be
3907 * avoided by calling ofproto_update_stats() whenever rules are created or
3908 * deleted. However, the performance impact of making so many calls to the
3909 * datapath do not justify the benefit of having perfectly accurate statistics.
3912 ofproto_update_stats(struct ofproto *p)
3914 const struct dpif_flow_stats *stats;
3915 struct dpif_flow_dump dump;
3916 const struct nlattr *key;
3919 dpif_flow_dump_start(&dump, p->dpif);
3920 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3921 struct facet *facet;
3924 if (odp_flow_key_to_flow(key, key_len, &flow)) {
3928 odp_flow_key_format(key, key_len, &s);
3929 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
3935 facet = facet_find(p, &flow);
3937 if (facet && facet->installed) {
3939 if (stats->n_packets >= facet->dp_packet_count) {
3940 facet->packet_count += stats->n_packets - facet->dp_packet_count;
3942 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3945 if (stats->n_bytes >= facet->dp_byte_count) {
3946 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
3948 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3951 facet->dp_packet_count = stats->n_packets;
3952 facet->dp_byte_count = stats->n_bytes;
3954 facet_update_time(p, facet, stats->used);
3955 facet_account(p, facet, stats->n_bytes);
3956 facet_push_stats(p, facet);
3958 /* There's a flow in the datapath that we know nothing about.
3960 COVERAGE_INC(ofproto_unexpected_rule);
3961 dpif_flow_del(p->dpif, key, key_len, NULL);
3964 dpif_flow_dump_done(&dump);
3967 /* Calculates and returns the number of milliseconds of idle time after which
3968 * facets should expire from the datapath and we should fold their statistics
3969 * into their parent rules in userspace. */
3971 ofproto_dp_max_idle(const struct ofproto *ofproto)
3974 * Idle time histogram.
3976 * Most of the time a switch has a relatively small number of facets. When
3977 * this is the case we might as well keep statistics for all of them in
3978 * userspace and to cache them in the kernel datapath for performance as
3981 * As the number of facets increases, the memory required to maintain
3982 * statistics about them in userspace and in the kernel becomes
3983 * significant. However, with a large number of facets it is likely that
3984 * only a few of them are "heavy hitters" that consume a large amount of
3985 * bandwidth. At this point, only heavy hitters are worth caching in the
3986 * kernel and maintaining in userspaces; other facets we can discard.
3988 * The technique used to compute the idle time is to build a histogram with
3989 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
3990 * that is installed in the kernel gets dropped in the appropriate bucket.
3991 * After the histogram has been built, we compute the cutoff so that only
3992 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
3993 * cached. At least the most-recently-used bucket of facets is kept, so
3994 * actually an arbitrary number of facets can be kept in any given
3995 * expiration run (though the next run will delete most of those unless
3996 * they receive additional data).
3998 * This requires a second pass through the facets, in addition to the pass
3999 * made by ofproto_update_stats(), because the former function never looks
4000 * at uninstallable facets.
4002 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
4003 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
4004 int buckets[N_BUCKETS] = { 0 };
4005 struct facet *facet;
4010 total = hmap_count(&ofproto->facets);
4011 if (total <= 1000) {
4012 return N_BUCKETS * BUCKET_WIDTH;
4015 /* Build histogram. */
4017 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
4018 long long int idle = now - facet->used;
4019 int bucket = (idle <= 0 ? 0
4020 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4021 : (unsigned int) idle / BUCKET_WIDTH);
4025 /* Find the first bucket whose flows should be expired. */
4026 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
4027 if (buckets[bucket]) {
4030 subtotal += buckets[bucket++];
4031 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
4036 if (VLOG_IS_DBG_ENABLED()) {
4040 ds_put_cstr(&s, "keep");
4041 for (i = 0; i < N_BUCKETS; i++) {
4043 ds_put_cstr(&s, ", drop");
4046 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4049 VLOG_INFO("%s: %s (msec:count)",
4050 dpif_name(ofproto->dpif), ds_cstr(&s));
4054 return bucket * BUCKET_WIDTH;
4058 facet_active_timeout(struct ofproto *ofproto, struct facet *facet)
4060 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
4061 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
4062 struct ofexpired expired;
4064 if (facet->installed) {
4065 struct dpif_flow_stats stats;
4067 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
4069 facet_update_stats(ofproto, facet, &stats);
4072 expired.flow = facet->flow;
4073 expired.packet_count = facet->packet_count;
4074 expired.byte_count = facet->byte_count;
4075 expired.used = facet->used;
4076 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4081 ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle)
4083 long long int cutoff = time_msec() - dp_max_idle;
4084 struct facet *facet, *next_facet;
4086 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
4087 facet_active_timeout(ofproto, facet);
4088 if (facet->used < cutoff) {
4089 facet_remove(ofproto, facet);
4094 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4095 * then delete it entirely. */
4097 rule_expire(struct ofproto *ofproto, struct rule *rule)
4099 struct facet *facet, *next_facet;
4103 /* Has 'rule' expired? */
4105 if (rule->hard_timeout
4106 && now > rule->created + rule->hard_timeout * 1000) {
4107 reason = OFPRR_HARD_TIMEOUT;
4108 } else if (rule->idle_timeout && list_is_empty(&rule->facets)
4109 && now >rule->used + rule->idle_timeout * 1000) {
4110 reason = OFPRR_IDLE_TIMEOUT;
4115 COVERAGE_INC(ofproto_expired);
4117 /* Update stats. (This is a no-op if the rule expired due to an idle
4118 * timeout, because that only happens when the rule has no facets left.) */
4119 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4120 facet_remove(ofproto, facet);
4123 /* Get rid of the rule. */
4124 if (!rule_is_hidden(rule)) {
4125 rule_send_removed(ofproto, rule, reason);
4127 rule_remove(ofproto, rule);
4131 rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason)
4133 struct ofputil_flow_removed fr;
4135 if (!rule->send_flow_removed) {
4140 fr.cookie = rule->flow_cookie;
4142 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
4143 fr.idle_timeout = rule->idle_timeout;
4144 fr.packet_count = rule->packet_count;
4145 fr.byte_count = rule->byte_count;
4147 connmgr_send_flow_removed(p->connmgr, &fr);
4150 /* Obtains statistics for 'rule' and stores them in '*packets' and '*bytes'.
4151 * The returned statistics include statistics for all of 'rule''s facets. */
4153 rule_get_stats(const struct rule *rule, uint64_t *packets, uint64_t *bytes)
4156 struct facet *facet;
4158 /* Start from historical data for 'rule' itself that are no longer tracked
4159 * in facets. This counts, for example, facets that have expired. */
4160 p = rule->packet_count;
4161 b = rule->byte_count;
4163 /* Add any statistics that are tracked by facets. This includes
4164 * statistical data recently updated by ofproto_update_stats() as well as
4165 * stats for packets that were executed "by hand" via dpif_execute(). */
4166 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4167 p += facet->packet_count;
4168 b += facet->byte_count;
4175 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
4176 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
4177 * their individual configurations.
4179 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
4180 * Otherwise, ownership is transferred to this function. */
4182 send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall,
4183 const struct flow *flow, bool clone)
4185 struct ofputil_packet_in pin;
4187 pin.packet = upcall->packet;
4188 pin.in_port = odp_port_to_ofp_port(flow->in_port);
4189 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
4190 pin.buffer_id = 0; /* not yet known */
4191 pin.send_len = upcall->userdata;
4192 connmgr_send_packet_in(ofproto->connmgr, upcall, flow,
4193 clone ? NULL : upcall->packet);
4197 pick_datapath_id(const struct ofproto *ofproto)
4199 const struct ofport *port;
4201 port = get_port(ofproto, ODPP_LOCAL);
4203 uint8_t ea[ETH_ADDR_LEN];
4206 error = netdev_get_etheraddr(port->netdev, ea);
4208 return eth_addr_to_uint64(ea);
4210 VLOG_WARN("could not get MAC address for %s (%s)",
4211 netdev_get_name(port->netdev), strerror(error));
4213 return ofproto->fallback_dpid;
4217 pick_fallback_dpid(void)
4219 uint8_t ea[ETH_ADDR_LEN];
4220 eth_addr_nicira_random(ea);
4221 return eth_addr_to_uint64(ea);
4225 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
4226 void *aux OVS_UNUSED)
4228 const struct shash_node *node;
4232 SHASH_FOR_EACH (node, &all_ofprotos) {
4233 ds_put_format(&results, "%s\n", node->name);
4235 unixctl_command_reply(conn, 200, ds_cstr(&results));
4236 ds_destroy(&results);
4239 struct ofproto_trace {
4240 struct action_xlate_ctx ctx;
4246 trace_format_rule(struct ds *result, int level, const struct rule *rule)
4248 ds_put_char_multiple(result, '\t', level);
4250 ds_put_cstr(result, "No match\n");
4254 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
4255 ntohll(rule->flow_cookie));
4256 cls_rule_format(&rule->cr, result);
4257 ds_put_char(result, '\n');
4259 ds_put_char_multiple(result, '\t', level);
4260 ds_put_cstr(result, "OpenFlow ");
4261 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
4262 rule->n_actions * sizeof *rule->actions);
4263 ds_put_char(result, '\n');
4267 trace_format_flow(struct ds *result, int level, const char *title,
4268 struct ofproto_trace *trace)
4270 ds_put_char_multiple(result, '\t', level);
4271 ds_put_format(result, "%s: ", title);
4272 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4273 ds_put_cstr(result, "unchanged");
4275 flow_format(result, &trace->ctx.flow);
4276 trace->flow = trace->ctx.flow;
4278 ds_put_char(result, '\n');
4282 trace_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
4284 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4285 struct ds *result = trace->result;
4287 ds_put_char(result, '\n');
4288 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4289 trace_format_rule(result, ctx->recurse + 1, rule);
4293 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4294 void *aux OVS_UNUSED)
4296 char *dpname, *in_port_s, *tun_id_s, *packet_s;
4297 char *args = xstrdup(args_);
4298 char *save_ptr = NULL;
4299 struct ofproto *ofproto;
4300 struct ofpbuf packet;
4308 ofpbuf_init(&packet, strlen(args) / 2);
4311 dpname = strtok_r(args, " ", &save_ptr);
4312 tun_id_s = strtok_r(NULL, " ", &save_ptr);
4313 in_port_s = strtok_r(NULL, " ", &save_ptr);
4314 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4315 if (!dpname || !in_port_s || !packet_s) {
4316 unixctl_command_reply(conn, 501, "Bad command syntax");
4320 ofproto = shash_find_data(&all_ofprotos, dpname);
4322 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4327 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
4328 in_port = ofp_port_to_odp_port(atoi(in_port_s));
4330 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
4331 packet_s += strspn(packet_s, " ");
4332 if (*packet_s != '\0') {
4333 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4336 if (packet.size < ETH_HEADER_LEN) {
4337 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
4341 ds_put_cstr(&result, "Packet: ");
4342 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
4343 ds_put_cstr(&result, s);
4346 flow_extract(&packet, tun_id, in_port, &flow);
4347 ds_put_cstr(&result, "Flow: ");
4348 flow_format(&result, &flow);
4349 ds_put_char(&result, '\n');
4351 rule = rule_lookup(ofproto, &flow);
4352 trace_format_rule(&result, 0, rule);
4354 struct ofproto_trace trace;
4355 struct ofpbuf *odp_actions;
4357 trace.result = &result;
4359 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
4360 trace.ctx.resubmit_hook = trace_resubmit;
4361 odp_actions = xlate_actions(&trace.ctx,
4362 rule->actions, rule->n_actions);
4364 ds_put_char(&result, '\n');
4365 trace_format_flow(&result, 0, "Final flow", &trace);
4366 ds_put_cstr(&result, "Datapath actions: ");
4367 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4368 ofpbuf_delete(odp_actions);
4371 unixctl_command_reply(conn, 200, ds_cstr(&result));
4374 ds_destroy(&result);
4375 ofpbuf_uninit(&packet);
4380 ofproto_unixctl_init(void)
4382 static bool registered;
4388 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
4389 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4393 default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
4394 struct ofpbuf *odp_actions, tag_type *tags,
4395 uint16_t *nf_output_iface, void *ofproto_)
4397 struct ofproto *ofproto = ofproto_;
4398 struct mac_entry *dst_mac;
4400 /* Drop frames for reserved multicast addresses. */
4401 if (eth_addr_is_reserved(flow->dl_dst)) {
4405 /* Learn source MAC (but don't try to learn from revalidation). */
4407 && mac_learning_may_learn(ofproto->ml, flow->dl_src, 0)) {
4408 struct mac_entry *src_mac;
4410 src_mac = mac_learning_insert(ofproto->ml, flow->dl_src, 0);
4411 if (mac_entry_is_new(src_mac) || src_mac->port.i != flow->in_port) {
4412 /* The log messages here could actually be useful in debugging,
4413 * so keep the rate limit relatively high. */
4414 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4415 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
4416 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
4418 ofproto_revalidate(ofproto,
4419 mac_learning_changed(ofproto->ml, src_mac));
4420 src_mac->port.i = flow->in_port;
4424 /* Determine output port. */
4425 dst_mac = mac_learning_lookup(ofproto->ml, flow->dl_dst, 0, tags);
4427 flood_packets(ofproto, flow->in_port, OFPPC_NO_FLOOD,
4428 nf_output_iface, odp_actions);
4430 int out_port = dst_mac->port.i;
4431 if (out_port != flow->in_port) {
4432 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, out_port);
4433 *nf_output_iface = out_port;
4442 static const struct ofhooks default_ofhooks = {
4443 default_normal_ofhook_cb,