2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include <sys/socket.h>
24 #include <netinet/in.h>
28 #include "byte-order.h"
30 #include "classifier.h"
34 #include "dynamic-string.h"
35 #include "fail-open.h"
39 #include "mac-learning.h"
40 #include "multipath.h"
46 #include "ofp-print.h"
48 #include "ofproto-sflow.h"
50 #include "openflow/nicira-ext.h"
51 #include "openflow/openflow.h"
52 #include "openvswitch/datapath-protocol.h"
56 #include "poll-loop.h"
60 #include "stream-ssl.h"
64 #include "unaligned.h"
69 VLOG_DEFINE_THIS_MODULE(ofproto);
71 COVERAGE_DEFINE(facet_changed_rule);
72 COVERAGE_DEFINE(facet_revalidate);
73 COVERAGE_DEFINE(odp_overflow);
74 COVERAGE_DEFINE(ofproto_agg_request);
75 COVERAGE_DEFINE(ofproto_costly_flags);
76 COVERAGE_DEFINE(ofproto_ctlr_action);
77 COVERAGE_DEFINE(ofproto_del_rule);
78 COVERAGE_DEFINE(ofproto_error);
79 COVERAGE_DEFINE(ofproto_expiration);
80 COVERAGE_DEFINE(ofproto_expired);
81 COVERAGE_DEFINE(ofproto_flows_req);
82 COVERAGE_DEFINE(ofproto_flush);
83 COVERAGE_DEFINE(ofproto_invalidated);
84 COVERAGE_DEFINE(ofproto_no_packet_in);
85 COVERAGE_DEFINE(ofproto_ofp2odp);
86 COVERAGE_DEFINE(ofproto_packet_in);
87 COVERAGE_DEFINE(ofproto_packet_out);
88 COVERAGE_DEFINE(ofproto_queue_req);
89 COVERAGE_DEFINE(ofproto_recv_openflow);
90 COVERAGE_DEFINE(ofproto_reinit_ports);
91 COVERAGE_DEFINE(ofproto_unexpected_rule);
92 COVERAGE_DEFINE(ofproto_uninstallable);
93 COVERAGE_DEFINE(ofproto_update_port);
95 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
96 * flow translation. */
97 #define MAX_RESUBMIT_RECURSION 16
102 struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
103 struct netdev *netdev;
104 struct ofp_phy_port opp; /* In host byte order. */
106 struct cfm *cfm; /* Connectivity Fault Management, if any. */
109 static void ofport_free(struct ofport *);
110 static void ofport_run(struct ofproto *, struct ofport *);
111 static void ofport_wait(struct ofport *);
113 struct action_xlate_ctx {
114 /* action_xlate_ctx_init() initializes these members. */
117 struct ofproto *ofproto;
119 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
120 * this flow when actions change header fields. */
123 /* The packet corresponding to 'flow', or a null pointer if we are
124 * revalidating without a packet to refer to. */
125 const struct ofpbuf *packet;
127 /* If nonnull, called just before executing a resubmit action.
129 * This is normally null so the client has to set it manually after
130 * calling action_xlate_ctx_init(). */
131 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule *);
133 /* If true, the speciality of 'flow' should be checked before executing
134 * its actions. If special_cb returns false on 'flow' rendered
135 * uninstallable and no actions will be executed. */
138 /* xlate_actions() initializes and uses these members. The client might want
139 * to look at them after it returns. */
141 struct ofpbuf *odp_actions; /* Datapath actions. */
142 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
143 bool may_set_up_flow; /* True ordinarily; false if the actions must
144 * be reassessed for every packet. */
145 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
147 /* xlate_actions() initializes and uses these members, but the client has no
148 * reason to look at them. */
150 int recurse; /* Recursion level, via xlate_table_action. */
151 int last_pop_priority; /* Offset in 'odp_actions' just past most
152 * recent ODP_ACTION_ATTR_SET_PRIORITY. */
155 static void action_xlate_ctx_init(struct action_xlate_ctx *,
156 struct ofproto *, const struct flow *,
157 const struct ofpbuf *);
158 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
159 const union ofp_action *in, size_t n_in);
161 /* An OpenFlow flow. */
163 long long int used; /* Time last used; time created if not used. */
164 long long int created; /* Creation time. */
168 * - Do include packets and bytes from facets that have been deleted or
169 * whose own statistics have been folded into the rule.
171 * - Do include packets and bytes sent "by hand" that were accounted to
172 * the rule without any facet being involved (this is a rare corner
173 * case in rule_execute()).
175 * - Do not include packet or bytes that can be obtained from any facet's
176 * packet_count or byte_count member or that can be obtained from the
177 * datapath by, e.g., dpif_flow_get() for any facet.
179 uint64_t packet_count; /* Number of packets received. */
180 uint64_t byte_count; /* Number of bytes received. */
182 ovs_be64 flow_cookie; /* Controller-issued identifier. */
184 struct cls_rule cr; /* In owning ofproto's classifier. */
185 uint16_t idle_timeout; /* In seconds from time of last use. */
186 uint16_t hard_timeout; /* In seconds from time of creation. */
187 bool send_flow_removed; /* Send a flow removed message? */
188 int n_actions; /* Number of elements in actions[]. */
189 union ofp_action *actions; /* OpenFlow actions. */
190 struct list facets; /* List of "struct facet"s. */
193 static struct rule *rule_from_cls_rule(const struct cls_rule *);
194 static bool rule_is_hidden(const struct rule *);
196 static struct rule *rule_create(const struct cls_rule *,
197 const union ofp_action *, size_t n_actions,
198 uint16_t idle_timeout, uint16_t hard_timeout,
199 ovs_be64 flow_cookie, bool send_flow_removed);
200 static void rule_destroy(struct ofproto *, struct rule *);
201 static void rule_free(struct rule *);
203 static struct rule *rule_lookup(struct ofproto *, const struct flow *);
204 static void rule_insert(struct ofproto *, struct rule *);
205 static void rule_remove(struct ofproto *, struct rule *);
207 static void rule_send_removed(struct ofproto *, struct rule *, uint8_t reason);
208 static void rule_get_stats(const struct rule *, uint64_t *packets,
211 /* An exact-match instantiation of an OpenFlow flow. */
213 long long int used; /* Time last used; time created if not used. */
217 * - Do include packets and bytes sent "by hand", e.g. with
220 * - Do include packets and bytes that were obtained from the datapath
221 * when a flow was deleted (e.g. dpif_flow_del()) or when its
222 * statistics were reset (e.g. dpif_flow_put() with
223 * DPIF_FP_ZERO_STATS).
225 * - Do not include any packets or bytes that can currently be obtained
226 * from the datapath by, e.g., dpif_flow_get().
228 uint64_t packet_count; /* Number of packets received. */
229 uint64_t byte_count; /* Number of bytes received. */
231 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
232 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
234 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
235 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
236 long long int rs_used; /* Used time pushed to resubmit children. */
238 /* Number of bytes passed to account_cb. This may include bytes that can
239 * currently obtained from the datapath (thus, it can be greater than
241 uint64_t accounted_bytes;
243 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
244 struct list list_node; /* In owning rule's 'facets' list. */
245 struct rule *rule; /* Owning rule. */
246 struct flow flow; /* Exact-match flow. */
247 bool installed; /* Installed in datapath? */
248 bool may_install; /* True ordinarily; false if actions must
249 * be reassessed for every packet. */
250 size_t actions_len; /* Number of bytes in actions[]. */
251 struct nlattr *actions; /* Datapath actions. */
252 tag_type tags; /* Tags (set only by hooks). */
253 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
256 static struct facet *facet_create(struct ofproto *, struct rule *,
258 const struct ofpbuf *packet);
259 static void facet_remove(struct ofproto *, struct facet *);
260 static void facet_free(struct facet *);
262 static struct facet *facet_lookup_valid(struct ofproto *, const struct flow *);
263 static bool facet_revalidate(struct ofproto *, struct facet *);
265 static void facet_install(struct ofproto *, struct facet *, bool zero_stats);
266 static void facet_uninstall(struct ofproto *, struct facet *);
267 static void facet_flush_stats(struct ofproto *, struct facet *);
269 static void facet_make_actions(struct ofproto *, struct facet *,
270 const struct ofpbuf *packet);
271 static void facet_update_stats(struct ofproto *, struct facet *,
272 const struct dpif_flow_stats *);
273 static void facet_push_stats(struct ofproto *, struct facet *);
275 static void send_packet_in(struct ofproto *, struct dpif_upcall *,
276 const struct flow *, bool clone);
280 uint64_t datapath_id; /* Datapath ID. */
281 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
282 char *mfr_desc; /* Manufacturer. */
283 char *hw_desc; /* Hardware. */
284 char *sw_desc; /* Software version. */
285 char *serial_desc; /* Serial number. */
286 char *dp_desc; /* Datapath description. */
290 struct netdev_monitor *netdev_monitor;
291 struct hmap ports; /* Contains "struct ofport"s. */
292 struct shash port_by_name;
296 struct netflow *netflow;
297 struct ofproto_sflow *sflow;
300 struct classifier cls;
301 struct timer next_expiration;
305 bool need_revalidate;
306 struct tag_set revalidate_set;
308 /* OpenFlow connections. */
309 struct connmgr *connmgr;
311 /* Hooks for ovs-vswitchd. */
312 const struct ofhooks *ofhooks;
315 /* Used by default ofhooks. */
316 struct mac_learning *ml;
319 /* Map from dpif name to struct ofproto, for use by unixctl commands. */
320 static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos);
322 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
324 static const struct ofhooks default_ofhooks;
326 static uint64_t pick_datapath_id(const struct ofproto *);
327 static uint64_t pick_fallback_dpid(void);
329 static void ofproto_flush_flows__(struct ofproto *);
330 static int ofproto_expire(struct ofproto *);
331 static void flow_push_stats(struct ofproto *, const struct rule *,
332 struct flow *, uint64_t packets, uint64_t bytes,
335 static void handle_upcall(struct ofproto *, struct dpif_upcall *);
337 static void handle_openflow(struct ofconn *, struct ofpbuf *);
339 static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
340 static void update_port(struct ofproto *, const char *devname);
341 static int init_ports(struct ofproto *);
342 static void reinit_ports(struct ofproto *);
344 static void ofproto_unixctl_init(void);
347 ofproto_create(const char *datapath, const char *datapath_type,
348 const struct ofhooks *ofhooks, void *aux,
349 struct ofproto **ofprotop)
351 char local_name[IF_NAMESIZE];
358 ofproto_unixctl_init();
360 /* Connect to datapath and start listening for messages. */
361 error = dpif_open(datapath, datapath_type, &dpif);
363 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
366 error = dpif_recv_set_mask(dpif,
367 ((1u << DPIF_UC_MISS) |
368 (1u << DPIF_UC_ACTION) |
369 (1u << DPIF_UC_SAMPLE)));
371 VLOG_ERR("failed to listen on datapath %s: %s",
372 datapath, strerror(error));
376 dpif_flow_flush(dpif);
377 dpif_recv_purge(dpif);
379 error = dpif_port_get_name(dpif, ODPP_LOCAL,
380 local_name, sizeof local_name);
382 VLOG_ERR("%s: cannot get name of datapath local port (%s)",
383 datapath, strerror(error));
387 /* Initialize settings. */
388 p = xzalloc(sizeof *p);
389 p->fallback_dpid = pick_fallback_dpid();
390 p->datapath_id = p->fallback_dpid;
391 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
392 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
393 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
394 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
395 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
397 /* Initialize datapath. */
399 p->netdev_monitor = netdev_monitor_create();
400 hmap_init(&p->ports);
401 shash_init(&p->port_by_name);
402 p->max_ports = dpif_get_max_ports(dpif);
404 /* Initialize submodules. */
408 /* Initialize flow table. */
409 classifier_init(&p->cls);
410 timer_set_duration(&p->next_expiration, 1000);
412 /* Initialize facet table. */
413 hmap_init(&p->facets);
414 p->need_revalidate = false;
415 tag_set_init(&p->revalidate_set);
417 /* Initialize hooks. */
419 p->ofhooks = ofhooks;
423 p->ofhooks = &default_ofhooks;
425 p->ml = mac_learning_create();
428 /* Pick final datapath ID. */
429 p->datapath_id = pick_datapath_id(p);
430 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
432 shash_add_once(&all_ofprotos, dpif_name(p->dpif), p);
434 /* Initialize OpenFlow connections. */
435 p->connmgr = connmgr_create(p, datapath, local_name);
442 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
444 uint64_t old_dpid = p->datapath_id;
445 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
446 if (p->datapath_id != old_dpid) {
447 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
449 /* Force all active connections to reconnect, since there is no way to
450 * notify a controller that the datapath ID has changed. */
451 ofproto_reconnect_controllers(p);
456 ofproto_set_controllers(struct ofproto *p,
457 const struct ofproto_controller *controllers,
458 size_t n_controllers)
460 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
464 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
466 connmgr_set_fail_mode(p->connmgr, fail_mode);
469 /* Drops the connections between 'ofproto' and all of its controllers, forcing
470 * them to reconnect. */
472 ofproto_reconnect_controllers(struct ofproto *ofproto)
474 connmgr_reconnect(ofproto->connmgr);
477 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
478 * in-band control should guarantee access, in the same way that in-band
479 * control guarantees access to OpenFlow controllers. */
481 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
482 const struct sockaddr_in *extras, size_t n)
484 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
487 /* Sets the OpenFlow queue used by flows set up by in-band control on
488 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
489 * flows will use the default queue. */
491 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
493 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
497 ofproto_set_desc(struct ofproto *p,
498 const char *mfr_desc, const char *hw_desc,
499 const char *sw_desc, const char *serial_desc,
502 struct ofp_desc_stats *ods;
505 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
506 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
507 sizeof ods->mfr_desc);
510 p->mfr_desc = xstrdup(mfr_desc);
513 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
514 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
515 sizeof ods->hw_desc);
518 p->hw_desc = xstrdup(hw_desc);
521 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
522 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
523 sizeof ods->sw_desc);
526 p->sw_desc = xstrdup(sw_desc);
529 if (strlen(serial_desc) >= sizeof ods->serial_num) {
530 VLOG_WARN("truncating serial_desc, must be less than %zu "
532 sizeof ods->serial_num);
534 free(p->serial_desc);
535 p->serial_desc = xstrdup(serial_desc);
538 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
539 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
540 sizeof ods->dp_desc);
543 p->dp_desc = xstrdup(dp_desc);
548 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
550 return connmgr_set_snoops(ofproto->connmgr, snoops);
554 ofproto_set_netflow(struct ofproto *ofproto,
555 const struct netflow_options *nf_options)
557 if (nf_options && !sset_is_empty(&nf_options->collectors)) {
558 if (!ofproto->netflow) {
559 ofproto->netflow = netflow_create();
561 return netflow_set_options(ofproto->netflow, nf_options);
563 netflow_destroy(ofproto->netflow);
564 ofproto->netflow = NULL;
570 ofproto_set_sflow(struct ofproto *ofproto,
571 const struct ofproto_sflow_options *oso)
573 struct ofproto_sflow *os = ofproto->sflow;
576 struct ofport *ofport;
578 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
579 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
580 ofproto_sflow_add_port(os, ofport->odp_port,
581 netdev_get_name(ofport->netdev));
584 ofproto_sflow_set_options(os, oso);
586 ofproto_sflow_destroy(os);
587 ofproto->sflow = NULL;
591 /* Connectivity Fault Management configuration. */
593 /* Clears the CFM configuration from 'port_no' on 'ofproto'. */
595 ofproto_iface_clear_cfm(struct ofproto *ofproto, uint32_t port_no)
597 struct ofport *ofport = get_port(ofproto, port_no);
598 if (ofport && ofport->cfm){
599 cfm_destroy(ofport->cfm);
604 /* Configures connectivity fault management on 'port_no' in 'ofproto'. Takes
605 * basic configuration from the configuration members in 'cfm', and the set of
606 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
607 * Ignores the statistics members of 'cfm'.
609 * This function has no effect if 'ofproto' does not have a port 'port_no'. */
611 ofproto_iface_set_cfm(struct ofproto *ofproto, uint32_t port_no,
612 const struct cfm *cfm,
613 const uint16_t *remote_mps, size_t n_remote_mps)
615 struct ofport *ofport;
617 ofport = get_port(ofproto, port_no);
619 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
620 dpif_name(ofproto->dpif), port_no);
625 ofport->cfm = cfm_create();
628 ofport->cfm->mpid = cfm->mpid;
629 ofport->cfm->interval = cfm->interval;
630 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
632 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
634 if (!cfm_configure(ofport->cfm)) {
635 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed",
636 dpif_name(ofproto->dpif), port_no,
637 netdev_get_name(ofport->netdev));
638 cfm_destroy(ofport->cfm);
643 /* Returns the connectivity fault management object associated with 'port_no'
644 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
645 * 'port_no' or if that port does not have CFM configured. The caller must not
646 * modify or destroy the returned object. */
648 ofproto_iface_get_cfm(struct ofproto *ofproto, uint32_t port_no)
650 struct ofport *ofport = get_port(ofproto, port_no);
651 return ofport ? ofport->cfm : NULL;
655 ofproto_get_datapath_id(const struct ofproto *ofproto)
657 return ofproto->datapath_id;
661 ofproto_has_primary_controller(const struct ofproto *ofproto)
663 return connmgr_has_controllers(ofproto->connmgr);
666 enum ofproto_fail_mode
667 ofproto_get_fail_mode(const struct ofproto *p)
669 return connmgr_get_fail_mode(p->connmgr);
673 ofproto_has_snoops(const struct ofproto *ofproto)
675 return connmgr_has_snoops(ofproto->connmgr);
679 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
681 connmgr_get_snoops(ofproto->connmgr, snoops);
685 ofproto_destroy(struct ofproto *p)
687 struct ofport *ofport, *next_ofport;
693 shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif));
695 ofproto_flush_flows__(p);
696 connmgr_destroy(p->connmgr);
697 classifier_destroy(&p->cls);
698 hmap_destroy(&p->facets);
701 netdev_monitor_destroy(p->netdev_monitor);
702 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
703 hmap_remove(&p->ports, &ofport->hmap_node);
706 shash_destroy(&p->port_by_name);
708 netflow_destroy(p->netflow);
709 ofproto_sflow_destroy(p->sflow);
711 mac_learning_destroy(p->ml);
716 free(p->serial_desc);
719 hmap_destroy(&p->ports);
725 ofproto_run(struct ofproto *p)
727 int error = ofproto_run1(p);
729 error = ofproto_run2(p, false);
735 process_port_change(struct ofproto *ofproto, int error, char *devname)
737 if (error == ENOBUFS) {
738 reinit_ports(ofproto);
740 update_port(ofproto, devname);
746 ofproto_run1(struct ofproto *p)
748 struct ofport *ofport;
753 if (shash_is_empty(&p->port_by_name)) {
757 for (i = 0; i < 50; i++) {
758 struct dpif_upcall packet;
760 error = dpif_recv(p->dpif, &packet);
762 if (error == ENODEV) {
763 /* Someone destroyed the datapath behind our back. The caller
764 * better destroy us and give up, because we're just going to
765 * spin from here on out. */
766 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
767 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
774 handle_upcall(p, &packet);
777 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
778 process_port_change(p, error, devname);
780 while ((error = netdev_monitor_poll(p->netdev_monitor,
781 &devname)) != EAGAIN) {
782 process_port_change(p, error, devname);
785 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
786 ofport_run(p, ofport);
789 connmgr_run(p->connmgr, handle_openflow);
791 if (timer_expired(&p->next_expiration)) {
792 int delay = ofproto_expire(p);
793 timer_set_duration(&p->next_expiration, delay);
794 COVERAGE_INC(ofproto_expiration);
798 netflow_run(p->netflow);
801 ofproto_sflow_run(p->sflow);
808 ofproto_run2(struct ofproto *p, bool revalidate_all)
810 /* Figure out what we need to revalidate now, if anything. */
811 struct tag_set revalidate_set = p->revalidate_set;
812 if (p->need_revalidate) {
813 revalidate_all = true;
816 /* Clear the revalidation flags. */
817 tag_set_init(&p->revalidate_set);
818 p->need_revalidate = false;
820 /* Now revalidate if there's anything to do. */
821 if (revalidate_all || !tag_set_is_empty(&revalidate_set)) {
822 struct facet *facet, *next;
824 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) {
826 || tag_set_intersects(&revalidate_set, facet->tags)) {
827 facet_revalidate(p, facet);
836 ofproto_wait(struct ofproto *p)
838 struct ofport *ofport;
840 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
843 dpif_recv_wait(p->dpif);
844 dpif_port_poll_wait(p->dpif);
845 netdev_monitor_poll_wait(p->netdev_monitor);
847 ofproto_sflow_wait(p->sflow);
849 if (!tag_set_is_empty(&p->revalidate_set)) {
850 poll_immediate_wake();
852 if (p->need_revalidate) {
853 /* Shouldn't happen, but if it does just go around again. */
854 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
855 poll_immediate_wake();
857 timer_wait(&p->next_expiration);
859 connmgr_wait(p->connmgr);
863 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
865 tag_set_add(&ofproto->revalidate_set, tag);
869 ofproto_get_revalidate_set(struct ofproto *ofproto)
871 return &ofproto->revalidate_set;
875 ofproto_is_alive(const struct ofproto *p)
877 return connmgr_has_controllers(p->connmgr);
881 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
884 connmgr_get_controller_info(ofproto->connmgr, info);
888 ofproto_free_ofproto_controller_info(struct shash *info)
890 struct shash_node *node;
892 SHASH_FOR_EACH (node, info) {
893 struct ofproto_controller_info *cinfo = node->data;
894 while (cinfo->pairs.n) {
895 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
902 /* Deletes port number 'odp_port' from the datapath for 'ofproto'.
904 * This is almost the same as calling dpif_port_del() directly on the
905 * datapath, but it also makes 'ofproto' close its open netdev for the port
906 * (if any). This makes it possible to create a new netdev of a different
907 * type under the same name, which otherwise the netdev library would refuse
908 * to do because of the conflict. (The netdev would eventually get closed on
909 * the next trip through ofproto_run(), but this interface is more direct.)
911 * Returns 0 if successful, otherwise a positive errno. */
913 ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port)
915 struct ofport *ofport = get_port(ofproto, odp_port);
916 const char *name = ofport ? ofport->opp.name : "<unknown>";
919 error = dpif_port_del(ofproto->dpif, odp_port);
921 VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)",
922 dpif_name(ofproto->dpif), odp_port, name, strerror(error));
924 /* 'name' is ofport->opp.name and update_port() is going to destroy
925 * 'ofport'. Just in case update_port() refers to 'name' after it
926 * destroys 'ofport', make a copy of it around the update_port()
928 char *devname = xstrdup(name);
929 update_port(ofproto, devname);
935 /* Checks if 'ofproto' thinks 'odp_port' should be included in floods. Returns
936 * true if 'odp_port' exists and should be included, false otherwise. */
938 ofproto_port_is_floodable(struct ofproto *ofproto, uint16_t odp_port)
940 struct ofport *ofport = get_port(ofproto, odp_port);
941 return ofport && !(ofport->opp.config & OFPPC_NO_FLOOD);
944 /* Sends 'packet' out of port 'port_no' within 'p'. If 'vlan_tci' is zero the
945 * packet will not have any 802.1Q hader; if it is nonzero, then the packet
946 * will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
948 * Returns 0 if successful, otherwise a positive errno value. */
950 ofproto_send_packet(struct ofproto *ofproto,
951 uint32_t port_no, uint16_t vlan_tci,
952 const struct ofpbuf *packet)
954 struct ofpbuf odp_actions;
957 ofpbuf_init(&odp_actions, 32);
959 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
960 ntohs(vlan_tci & ~VLAN_CFI));
962 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, port_no);
963 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
965 ofpbuf_uninit(&odp_actions);
968 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
969 dpif_name(ofproto->dpif), port_no, strerror(error));
974 /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
975 * performs the 'n_actions' actions in 'actions'. The new flow will not
978 * If cls_rule->priority is in the range of priorities supported by OpenFlow
979 * (0...65535, inclusive) then the flow will be visible to OpenFlow
980 * controllers; otherwise, it will be hidden.
982 * The caller retains ownership of 'cls_rule' and 'actions'. */
984 ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
985 const union ofp_action *actions, size_t n_actions)
988 rule = rule_create(cls_rule, actions, n_actions, 0, 0, 0, false);
989 rule_insert(p, rule);
993 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
997 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
1000 rule_remove(ofproto, rule);
1005 ofproto_flush_flows__(struct ofproto *ofproto)
1007 struct facet *facet, *next_facet;
1008 struct rule *rule, *next_rule;
1009 struct cls_cursor cursor;
1011 COVERAGE_INC(ofproto_flush);
1013 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1014 /* Mark the facet as not installed so that facet_remove() doesn't
1015 * bother trying to uninstall it. There is no point in uninstalling it
1016 * individually since we are about to blow away all the facets with
1017 * dpif_flow_flush(). */
1018 facet->installed = false;
1019 facet->dp_packet_count = 0;
1020 facet->dp_byte_count = 0;
1021 facet_remove(ofproto, facet);
1024 cls_cursor_init(&cursor, &ofproto->cls, NULL);
1025 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1026 rule_remove(ofproto, rule);
1029 dpif_flow_flush(ofproto->dpif);
1033 ofproto_flush_flows(struct ofproto *ofproto)
1035 ofproto_flush_flows__(ofproto);
1036 connmgr_flushed(ofproto->connmgr);
1040 reinit_ports(struct ofproto *p)
1042 struct dpif_port_dump dump;
1043 struct sset devnames;
1044 struct ofport *ofport;
1045 struct dpif_port dpif_port;
1046 const char *devname;
1048 COVERAGE_INC(ofproto_reinit_ports);
1050 sset_init(&devnames);
1051 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1052 sset_add(&devnames, ofport->opp.name);
1054 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1055 sset_add(&devnames, dpif_port.name);
1058 SSET_FOR_EACH (devname, &devnames) {
1059 update_port(p, devname);
1061 sset_destroy(&devnames);
1064 static struct ofport *
1065 make_ofport(const struct dpif_port *dpif_port)
1067 struct netdev_options netdev_options;
1068 enum netdev_flags flags;
1069 struct ofport *ofport;
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 ofport = xzalloc(sizeof *ofport);
1088 ofport->netdev = netdev;
1089 ofport->odp_port = dpif_port->port_no;
1090 ofport->opp.port_no = odp_port_to_ofp_port(dpif_port->port_no);
1091 netdev_get_etheraddr(netdev, ofport->opp.hw_addr);
1092 ovs_strlcpy(ofport->opp.name, dpif_port->name, sizeof ofport->opp.name);
1094 netdev_get_flags(netdev, &flags);
1095 ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
1097 ofport->opp.state = netdev_get_carrier(netdev) ? 0 : OFPPS_LINK_DOWN;
1099 netdev_get_features(netdev,
1100 &ofport->opp.curr, &ofport->opp.advertised,
1101 &ofport->opp.supported, &ofport->opp.peer);
1106 ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port)
1108 if (get_port(p, dpif_port->port_no)) {
1109 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1110 dpif_port->port_no);
1112 } else if (shash_find(&p->port_by_name, dpif_port->name)) {
1113 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1122 ofport_equal(const struct ofport *a_, const struct ofport *b_)
1124 const struct ofp_phy_port *a = &a_->opp;
1125 const struct ofp_phy_port *b = &b_->opp;
1127 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1128 return (a->port_no == b->port_no
1129 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1130 && !strcmp(a->name, b->name)
1131 && a->state == b->state
1132 && a->config == b->config
1133 && a->curr == b->curr
1134 && a->advertised == b->advertised
1135 && a->supported == b->supported
1136 && a->peer == b->peer);
1140 ofport_install(struct ofproto *p, struct ofport *ofport)
1142 const char *netdev_name = ofport->opp.name;
1144 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1145 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
1146 shash_add(&p->port_by_name, netdev_name, ofport);
1148 ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
1153 ofport_remove(struct ofproto *p, struct ofport *ofport)
1155 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1156 hmap_remove(&p->ports, &ofport->hmap_node);
1157 shash_delete(&p->port_by_name,
1158 shash_find(&p->port_by_name, ofport->opp.name));
1160 ofproto_sflow_del_port(p->sflow, ofport->odp_port);
1165 ofport_run(struct ofproto *ofproto, struct ofport *ofport)
1168 cfm_run(ofport->cfm);
1170 if (cfm_should_send_ccm(ofport->cfm)) {
1171 struct ofpbuf packet;
1174 ofpbuf_init(&packet, 0);
1175 ccm = eth_compose(&packet, eth_addr_ccm, ofport->opp.hw_addr,
1176 ETH_TYPE_CFM, sizeof *ccm);
1177 cfm_compose_ccm(ofport->cfm, ccm);
1178 ofproto_send_packet(ofproto, ofport->odp_port, 0, &packet);
1179 ofpbuf_uninit(&packet);
1185 ofport_wait(struct ofport *ofport)
1188 cfm_wait(ofport->cfm);
1193 ofport_free(struct ofport *ofport)
1196 cfm_destroy(ofport->cfm);
1197 netdev_close(ofport->netdev);
1202 static struct ofport *
1203 get_port(const struct ofproto *ofproto, uint16_t odp_port)
1205 struct ofport *port;
1207 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
1208 hash_int(odp_port, 0), &ofproto->ports) {
1209 if (port->odp_port == odp_port) {
1217 update_port(struct ofproto *p, const char *devname)
1219 struct dpif_port dpif_port;
1220 struct ofport *old_ofport;
1221 struct ofport *new_ofport;
1224 COVERAGE_INC(ofproto_update_port);
1226 /* Query the datapath for port information. */
1227 error = dpif_port_query_by_name(p->dpif, devname, &dpif_port);
1229 /* Find the old ofport. */
1230 old_ofport = shash_find_data(&p->port_by_name, devname);
1233 /* There's no port named 'devname' but there might be a port with
1234 * the same port number. This could happen if a port is deleted
1235 * and then a new one added in its place very quickly, or if a port
1236 * is renamed. In the former case we want to send an OFPPR_DELETE
1237 * and an OFPPR_ADD, and in the latter case we want to send a
1238 * single OFPPR_MODIFY. We can distinguish the cases by comparing
1239 * the old port's ifindex against the new port, or perhaps less
1240 * reliably but more portably by comparing the old port's MAC
1241 * against the new port's MAC. However, this code isn't that smart
1242 * and always sends an OFPPR_MODIFY (XXX). */
1243 old_ofport = get_port(p, dpif_port.port_no);
1245 } else if (error != ENOENT && error != ENODEV) {
1246 VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error "
1247 "%s", strerror(error));
1251 /* Create a new ofport. */
1252 new_ofport = !error ? make_ofport(&dpif_port) : NULL;
1254 /* Eliminate a few pathological cases. */
1255 if (!old_ofport && !new_ofport) {
1257 } else if (old_ofport && new_ofport) {
1258 /* Most of the 'config' bits are OpenFlow soft state, but
1259 * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
1260 * OpenFlow bits from old_ofport. (make_ofport() only sets
1261 * OFPPC_PORT_DOWN and leaves the other bits 0.) */
1262 new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN;
1264 if (ofport_equal(old_ofport, new_ofport)) {
1265 /* False alarm--no change. */
1266 ofport_free(new_ofport);
1271 /* Now deal with the normal cases. */
1273 ofport_remove(p, old_ofport);
1276 ofport_install(p, new_ofport);
1278 connmgr_send_port_status(p->connmgr,
1279 new_ofport ? &new_ofport->opp : &old_ofport->opp,
1280 (!old_ofport ? OFPPR_ADD
1281 : !new_ofport ? OFPPR_DELETE
1283 ofport_free(old_ofport);
1286 dpif_port_destroy(&dpif_port);
1290 init_ports(struct ofproto *p)
1292 struct dpif_port_dump dump;
1293 struct dpif_port dpif_port;
1295 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1296 if (!ofport_conflicts(p, &dpif_port)) {
1297 struct ofport *ofport = make_ofport(&dpif_port);
1299 ofport_install(p, ofport);
1307 /* Returns true if 'rule' should be hidden from the controller.
1309 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1310 * (e.g. by in-band control) and are intentionally hidden from the
1313 rule_is_hidden(const struct rule *rule)
1315 return rule->cr.priority > UINT16_MAX;
1318 /* Creates and returns a new rule initialized as specified.
1320 * The caller is responsible for inserting the rule into the classifier (with
1321 * rule_insert()). */
1322 static struct rule *
1323 rule_create(const struct cls_rule *cls_rule,
1324 const union ofp_action *actions, size_t n_actions,
1325 uint16_t idle_timeout, uint16_t hard_timeout,
1326 ovs_be64 flow_cookie, bool send_flow_removed)
1328 struct rule *rule = xzalloc(sizeof *rule);
1329 rule->cr = *cls_rule;
1330 rule->idle_timeout = idle_timeout;
1331 rule->hard_timeout = hard_timeout;
1332 rule->flow_cookie = flow_cookie;
1333 rule->used = rule->created = time_msec();
1334 rule->send_flow_removed = send_flow_removed;
1335 list_init(&rule->facets);
1336 if (n_actions > 0) {
1337 rule->n_actions = n_actions;
1338 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1344 static struct rule *
1345 rule_from_cls_rule(const struct cls_rule *cls_rule)
1347 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1351 rule_free(struct rule *rule)
1353 free(rule->actions);
1357 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
1358 * destroying any that no longer has a rule (which is probably all of them).
1360 * The caller must have already removed 'rule' from the classifier. */
1362 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1364 struct facet *facet, *next_facet;
1365 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1366 facet_revalidate(ofproto, facet);
1371 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1372 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1375 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
1377 const union ofp_action *oa;
1378 struct actions_iterator i;
1380 if (out_port == htons(OFPP_NONE)) {
1383 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1384 oa = actions_next(&i)) {
1385 if (action_outputs_to_port(oa, out_port)) {
1392 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1393 * 'packet', which arrived on 'in_port'.
1395 * Takes ownership of 'packet'. */
1397 execute_odp_actions(struct ofproto *ofproto, const struct flow *flow,
1398 const struct nlattr *odp_actions, size_t actions_len,
1399 struct ofpbuf *packet)
1401 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1402 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1403 /* As an optimization, avoid a round-trip from userspace to kernel to
1404 * userspace. This also avoids possibly filling up kernel packet
1405 * buffers along the way. */
1406 struct dpif_upcall upcall;
1408 upcall.type = DPIF_UC_ACTION;
1409 upcall.packet = packet;
1412 upcall.userdata = nl_attr_get_u64(odp_actions);
1413 upcall.sample_pool = 0;
1414 upcall.actions = NULL;
1415 upcall.actions_len = 0;
1417 send_packet_in(ofproto, &upcall, flow, false);
1423 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
1424 ofpbuf_delete(packet);
1429 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
1430 * statistics appropriately. 'packet' must have at least sizeof(struct
1431 * ofp_packet_in) bytes of headroom.
1433 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
1434 * applying flow_extract() to 'packet' would yield the same flow as
1437 * 'facet' must have accurately composed ODP actions; that is, it must not be
1438 * in need of revalidation.
1440 * Takes ownership of 'packet'. */
1442 facet_execute(struct ofproto *ofproto, struct facet *facet,
1443 struct ofpbuf *packet)
1445 struct dpif_flow_stats stats;
1447 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1449 flow_extract_stats(&facet->flow, packet, &stats);
1450 stats.used = time_msec();
1451 if (execute_odp_actions(ofproto, &facet->flow,
1452 facet->actions, facet->actions_len, packet)) {
1453 facet_update_stats(ofproto, facet, &stats);
1457 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
1458 * statistics (or the statistics for one of its facets) appropriately.
1459 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
1461 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1462 * with statistics for 'packet' either way.
1464 * Takes ownership of 'packet'. */
1466 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
1467 struct ofpbuf *packet)
1469 struct action_xlate_ctx ctx;
1470 struct ofpbuf *odp_actions;
1471 struct facet *facet;
1475 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1477 flow_extract(packet, 0, in_port, &flow);
1479 /* First look for a related facet. If we find one, account it to that. */
1480 facet = facet_lookup_valid(ofproto, &flow);
1481 if (facet && facet->rule == rule) {
1482 facet_execute(ofproto, facet, packet);
1486 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
1487 * create a new facet for it and use that. */
1488 if (rule_lookup(ofproto, &flow) == rule) {
1489 facet = facet_create(ofproto, rule, &flow, packet);
1490 facet_execute(ofproto, facet, packet);
1491 facet_install(ofproto, facet, true);
1495 /* We can't account anything to a facet. If we were to try, then that
1496 * facet would have a non-matching rule, busting our invariants. */
1497 action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
1498 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1499 size = packet->size;
1500 if (execute_odp_actions(ofproto, &flow, odp_actions->data,
1501 odp_actions->size, packet)) {
1502 rule->used = time_msec();
1503 rule->packet_count++;
1504 rule->byte_count += size;
1505 flow_push_stats(ofproto, rule, &flow, 1, size, rule->used);
1507 ofpbuf_delete(odp_actions);
1510 /* Inserts 'rule' into 'p''s flow table. */
1512 rule_insert(struct ofproto *p, struct rule *rule)
1514 struct rule *displaced_rule;
1516 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1517 if (displaced_rule) {
1518 rule_destroy(p, displaced_rule);
1520 p->need_revalidate = true;
1523 /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a
1524 * 'flow' and an example 'packet' within that flow.
1526 * The caller must already have determined that no facet with an identical
1527 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1528 * 'ofproto''s classifier table. */
1529 static struct facet *
1530 facet_create(struct ofproto *ofproto, struct rule *rule,
1531 const struct flow *flow, const struct ofpbuf *packet)
1533 struct facet *facet;
1535 facet = xzalloc(sizeof *facet);
1536 facet->used = time_msec();
1537 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1538 list_push_back(&rule->facets, &facet->list_node);
1540 facet->flow = *flow;
1541 netflow_flow_init(&facet->nf_flow);
1542 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1544 facet_make_actions(ofproto, facet, packet);
1550 facet_free(struct facet *facet)
1552 free(facet->actions);
1556 /* Remove 'rule' from 'ofproto' and free up the associated memory:
1558 * - Removes 'rule' from the classifier.
1560 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
1561 * destroys them), via rule_destroy().
1564 rule_remove(struct ofproto *ofproto, struct rule *rule)
1566 COVERAGE_INC(ofproto_del_rule);
1567 ofproto->need_revalidate = true;
1568 classifier_remove(&ofproto->cls, &rule->cr);
1569 rule_destroy(ofproto, rule);
1572 /* Remove 'facet' from 'ofproto' and free up the associated memory:
1574 * - If 'facet' was installed in the datapath, uninstalls it and updates its
1575 * rule's statistics, via facet_uninstall().
1577 * - Removes 'facet' from its rule and from ofproto->facets.
1580 facet_remove(struct ofproto *ofproto, struct facet *facet)
1582 facet_uninstall(ofproto, facet);
1583 facet_flush_stats(ofproto, facet);
1584 hmap_remove(&ofproto->facets, &facet->hmap_node);
1585 list_remove(&facet->list_node);
1589 /* Composes the ODP actions for 'facet' based on its rule's actions. */
1591 facet_make_actions(struct ofproto *p, struct facet *facet,
1592 const struct ofpbuf *packet)
1594 const struct rule *rule = facet->rule;
1595 struct ofpbuf *odp_actions;
1596 struct action_xlate_ctx ctx;
1598 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
1599 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
1600 facet->tags = ctx.tags;
1601 facet->may_install = ctx.may_set_up_flow;
1602 facet->nf_flow.output_iface = ctx.nf_output_iface;
1604 if (facet->actions_len != odp_actions->size
1605 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
1606 free(facet->actions);
1607 facet->actions_len = odp_actions->size;
1608 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1611 ofpbuf_delete(odp_actions);
1615 facet_put__(struct ofproto *ofproto, struct facet *facet,
1616 const struct nlattr *actions, size_t actions_len,
1617 struct dpif_flow_stats *stats)
1619 struct odputil_keybuf keybuf;
1620 enum dpif_flow_put_flags flags;
1623 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1625 flags |= DPIF_FP_ZERO_STATS;
1626 facet->dp_packet_count = 0;
1627 facet->dp_byte_count = 0;
1630 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1631 odp_flow_key_from_flow(&key, &facet->flow);
1633 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
1634 actions, actions_len, stats);
1637 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
1638 * 'zero_stats' is true, clears any existing statistics from the datapath for
1641 facet_install(struct ofproto *p, struct facet *facet, bool zero_stats)
1643 struct dpif_flow_stats stats;
1645 if (facet->may_install
1646 && !facet_put__(p, facet, facet->actions, facet->actions_len,
1647 zero_stats ? &stats : NULL)) {
1648 facet->installed = true;
1652 /* Ensures that the bytes in 'facet', plus 'extra_bytes', have been passed up
1653 * to the accounting hook function in the ofhooks structure. */
1655 facet_account(struct ofproto *ofproto,
1656 struct facet *facet, uint64_t extra_bytes)
1658 uint64_t total_bytes = facet->byte_count + extra_bytes;
1660 if (ofproto->ofhooks->account_flow_cb
1661 && total_bytes > facet->accounted_bytes)
1663 ofproto->ofhooks->account_flow_cb(
1664 &facet->flow, facet->tags, facet->actions, facet->actions_len,
1665 total_bytes - facet->accounted_bytes, ofproto->aux);
1666 facet->accounted_bytes = total_bytes;
1670 /* If 'rule' is installed in the datapath, uninstalls it. */
1672 facet_uninstall(struct ofproto *p, struct facet *facet)
1674 if (facet->installed) {
1675 struct odputil_keybuf keybuf;
1676 struct dpif_flow_stats stats;
1679 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1680 odp_flow_key_from_flow(&key, &facet->flow);
1682 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
1683 facet_update_stats(p, facet, &stats);
1685 facet->installed = false;
1686 facet->dp_packet_count = 0;
1687 facet->dp_byte_count = 0;
1689 assert(facet->dp_packet_count == 0);
1690 assert(facet->dp_byte_count == 0);
1694 /* Returns true if the only action for 'facet' is to send to the controller.
1695 * (We don't report NetFlow expiration messages for such facets because they
1696 * are just part of the control logic for the network, not real traffic). */
1698 facet_is_controller_flow(struct facet *facet)
1701 && facet->rule->n_actions == 1
1702 && action_outputs_to_port(&facet->rule->actions[0],
1703 htons(OFPP_CONTROLLER)));
1706 /* Folds all of 'facet''s statistics into its rule. Also updates the
1707 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
1708 * 'facet''s statistics in the datapath should have been zeroed and folded into
1709 * its packet and byte counts before this function is called. */
1711 facet_flush_stats(struct ofproto *ofproto, struct facet *facet)
1713 assert(!facet->dp_byte_count);
1714 assert(!facet->dp_packet_count);
1716 facet_push_stats(ofproto, facet);
1717 facet_account(ofproto, facet, 0);
1719 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
1720 struct ofexpired expired;
1721 expired.flow = facet->flow;
1722 expired.packet_count = facet->packet_count;
1723 expired.byte_count = facet->byte_count;
1724 expired.used = facet->used;
1725 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1728 facet->rule->packet_count += facet->packet_count;
1729 facet->rule->byte_count += facet->byte_count;
1731 /* Reset counters to prevent double counting if 'facet' ever gets
1733 facet->packet_count = 0;
1734 facet->byte_count = 0;
1735 facet->rs_packet_count = 0;
1736 facet->rs_byte_count = 0;
1737 facet->accounted_bytes = 0;
1739 netflow_flow_clear(&facet->nf_flow);
1742 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
1743 * Returns it if found, otherwise a null pointer.
1745 * The returned facet might need revalidation; use facet_lookup_valid()
1746 * instead if that is important. */
1747 static struct facet *
1748 facet_find(struct ofproto *ofproto, const struct flow *flow)
1750 struct facet *facet;
1752 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
1754 if (flow_equal(flow, &facet->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 is guaranteed to be valid. */
1766 static struct facet *
1767 facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
1769 struct facet *facet = facet_find(ofproto, flow);
1771 /* The facet we found might not be valid, since we could be in need of
1772 * revalidation. If it is not valid, don't return it. */
1774 && ofproto->need_revalidate
1775 && !facet_revalidate(ofproto, facet)) {
1776 COVERAGE_INC(ofproto_invalidated);
1783 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
1785 * - If the rule found is different from 'facet''s current rule, moves
1786 * 'facet' to the new rule and recompiles its actions.
1788 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
1789 * where it is and recompiles its actions anyway.
1791 * - If there is none, destroys 'facet'.
1793 * Returns true if 'facet' still exists, false if it has been destroyed. */
1795 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
1797 struct action_xlate_ctx ctx;
1798 struct ofpbuf *odp_actions;
1799 struct rule *new_rule;
1800 bool actions_changed;
1802 COVERAGE_INC(facet_revalidate);
1804 /* Determine the new rule. */
1805 new_rule = rule_lookup(ofproto, &facet->flow);
1807 /* No new rule, so delete the facet. */
1808 facet_remove(ofproto, facet);
1812 /* Calculate new ODP actions.
1814 * We do not modify any 'facet' state yet, because we might need to, e.g.,
1815 * emit a NetFlow expiration and, if so, we need to have the old state
1816 * around to properly compose it. */
1817 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
1818 odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
1819 actions_changed = (facet->actions_len != odp_actions->size
1820 || memcmp(facet->actions, odp_actions->data,
1821 facet->actions_len));
1823 /* If the ODP actions changed or the installability changed, then we need
1824 * to talk to the datapath. */
1825 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
1826 if (ctx.may_set_up_flow) {
1827 struct dpif_flow_stats stats;
1829 facet_put__(ofproto, facet,
1830 odp_actions->data, odp_actions->size, &stats);
1831 facet_update_stats(ofproto, facet, &stats);
1833 facet_uninstall(ofproto, facet);
1836 /* The datapath flow is gone or has zeroed stats, so push stats out of
1837 * 'facet' into 'rule'. */
1838 facet_flush_stats(ofproto, facet);
1841 /* Update 'facet' now that we've taken care of all the old state. */
1842 facet->tags = ctx.tags;
1843 facet->nf_flow.output_iface = ctx.nf_output_iface;
1844 facet->may_install = ctx.may_set_up_flow;
1845 if (actions_changed) {
1846 free(facet->actions);
1847 facet->actions_len = odp_actions->size;
1848 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
1850 if (facet->rule != new_rule) {
1851 COVERAGE_INC(facet_changed_rule);
1852 list_remove(&facet->list_node);
1853 list_push_back(&new_rule->facets, &facet->list_node);
1854 facet->rule = new_rule;
1855 facet->used = new_rule->created;
1856 facet->rs_used = facet->used;
1859 ofpbuf_delete(odp_actions);
1865 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1868 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
1870 COVERAGE_INC(ofproto_error);
1871 ofconn_send_reply(ofconn, buf);
1876 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
1878 ofconn_send_reply(ofconn, make_echo_reply(oh));
1883 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
1885 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1886 struct ofp_switch_features *osf;
1888 struct ofport *port;
1890 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1891 osf->datapath_id = htonll(ofproto->datapath_id);
1892 osf->n_buffers = htonl(pktbuf_capacity());
1894 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1895 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
1896 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1897 (1u << OFPAT_SET_VLAN_VID) |
1898 (1u << OFPAT_SET_VLAN_PCP) |
1899 (1u << OFPAT_STRIP_VLAN) |
1900 (1u << OFPAT_SET_DL_SRC) |
1901 (1u << OFPAT_SET_DL_DST) |
1902 (1u << OFPAT_SET_NW_SRC) |
1903 (1u << OFPAT_SET_NW_DST) |
1904 (1u << OFPAT_SET_NW_TOS) |
1905 (1u << OFPAT_SET_TP_SRC) |
1906 (1u << OFPAT_SET_TP_DST) |
1907 (1u << OFPAT_ENQUEUE));
1909 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
1910 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1913 ofconn_send_reply(ofconn, buf);
1918 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
1920 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1922 struct ofp_switch_config *osc;
1926 /* Figure out flags. */
1927 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
1928 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1931 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1932 osc->flags = htons(flags);
1933 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
1934 ofconn_send_reply(ofconn, buf);
1940 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
1942 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
1943 uint16_t flags = ntohs(osc->flags);
1945 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1946 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
1947 switch (flags & OFPC_FRAG_MASK) {
1948 case OFPC_FRAG_NORMAL:
1949 dpif_set_drop_frags(ofproto->dpif, false);
1951 case OFPC_FRAG_DROP:
1952 dpif_set_drop_frags(ofproto->dpif, true);
1955 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1961 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
1966 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
1967 struct action_xlate_ctx *ctx);
1970 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
1972 const struct ofport *ofport = get_port(ctx->ofproto, port);
1975 if (ofport->opp.config & OFPPC_NO_FWD) {
1976 /* Forwarding disabled on port. */
1981 * We don't have an ofport record for this port, but it doesn't hurt to
1982 * allow forwarding to it anyhow. Maybe such a port will appear later
1983 * and we're pre-populating the flow table.
1987 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port);
1988 ctx->nf_output_iface = port;
1991 static struct rule *
1992 rule_lookup(struct ofproto *ofproto, const struct flow *flow)
1994 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
1998 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2000 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2001 uint16_t old_in_port;
2004 /* Look up a flow with 'in_port' as the input port. Then restore the
2005 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2006 * have surprising behavior). */
2007 old_in_port = ctx->flow.in_port;
2008 ctx->flow.in_port = in_port;
2009 rule = rule_lookup(ctx->ofproto, &ctx->flow);
2010 ctx->flow.in_port = old_in_port;
2012 if (ctx->resubmit_hook) {
2013 ctx->resubmit_hook(ctx, rule);
2018 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2022 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2024 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2025 MAX_RESUBMIT_RECURSION);
2030 flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, uint32_t mask,
2031 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2033 struct ofport *ofport;
2035 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
2036 uint16_t odp_port = ofport->odp_port;
2037 if (odp_port != odp_in_port && !(ofport->opp.config & mask)) {
2038 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2041 *nf_output_iface = NF_OUT_FLOOD;
2045 xlate_output_action__(struct action_xlate_ctx *ctx,
2046 uint16_t port, uint16_t max_len)
2049 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2051 ctx->nf_output_iface = NF_OUT_DROP;
2055 add_output_action(ctx, ctx->flow.in_port);
2058 xlate_table_action(ctx, ctx->flow.in_port);
2061 if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
2062 ctx->odp_actions, &ctx->tags,
2063 &ctx->nf_output_iface,
2064 ctx->ofproto->aux)) {
2065 COVERAGE_INC(ofproto_uninstallable);
2066 ctx->may_set_up_flow = false;
2070 flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD,
2071 &ctx->nf_output_iface, ctx->odp_actions);
2074 flood_packets(ctx->ofproto, ctx->flow.in_port, 0,
2075 &ctx->nf_output_iface, ctx->odp_actions);
2077 case OFPP_CONTROLLER:
2078 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2081 add_output_action(ctx, ODPP_LOCAL);
2084 odp_port = ofp_port_to_odp_port(port);
2085 if (odp_port != ctx->flow.in_port) {
2086 add_output_action(ctx, odp_port);
2091 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2092 ctx->nf_output_iface = NF_OUT_FLOOD;
2093 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2094 ctx->nf_output_iface = prev_nf_output_iface;
2095 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2096 ctx->nf_output_iface != NF_OUT_FLOOD) {
2097 ctx->nf_output_iface = NF_OUT_MULTI;
2102 xlate_output_action(struct action_xlate_ctx *ctx,
2103 const struct ofp_action_output *oao)
2105 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2108 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2109 * optimization, because we're going to add another action that sets the
2110 * priority immediately after, or because there are no actions following the
2113 remove_pop_action(struct action_xlate_ctx *ctx)
2115 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2116 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2117 ctx->last_pop_priority = -1;
2122 add_pop_action(struct action_xlate_ctx *ctx)
2124 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2125 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2126 ctx->last_pop_priority = ctx->odp_actions->size;
2131 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2132 const struct ofp_action_enqueue *oae)
2134 uint16_t ofp_port, odp_port;
2138 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2141 /* Fall back to ordinary output action. */
2142 xlate_output_action__(ctx, ntohs(oae->port), 0);
2146 /* Figure out ODP output port. */
2147 ofp_port = ntohs(oae->port);
2148 if (ofp_port != OFPP_IN_PORT) {
2149 odp_port = ofp_port_to_odp_port(ofp_port);
2151 odp_port = ctx->flow.in_port;
2154 /* Add ODP actions. */
2155 remove_pop_action(ctx);
2156 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2157 add_output_action(ctx, odp_port);
2158 add_pop_action(ctx);
2160 /* Update NetFlow output port. */
2161 if (ctx->nf_output_iface == NF_OUT_DROP) {
2162 ctx->nf_output_iface = odp_port;
2163 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2164 ctx->nf_output_iface = NF_OUT_MULTI;
2169 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2170 const struct nx_action_set_queue *nasq)
2175 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2178 /* Couldn't translate queue to a priority, so ignore. A warning
2179 * has already been logged. */
2183 remove_pop_action(ctx);
2184 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2188 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2190 ovs_be16 tci = ctx->flow.vlan_tci;
2191 if (!(tci & htons(VLAN_CFI))) {
2192 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2194 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2195 tci & ~htons(VLAN_CFI));
2199 struct xlate_reg_state {
2205 save_reg_state(const struct action_xlate_ctx *ctx,
2206 struct xlate_reg_state *state)
2208 state->vlan_tci = ctx->flow.vlan_tci;
2209 state->tun_id = ctx->flow.tun_id;
2213 update_reg_state(struct action_xlate_ctx *ctx,
2214 const struct xlate_reg_state *state)
2216 if (ctx->flow.vlan_tci != state->vlan_tci) {
2217 xlate_set_dl_tci(ctx);
2219 if (ctx->flow.tun_id != state->tun_id) {
2220 nl_msg_put_be64(ctx->odp_actions,
2221 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2226 xlate_nicira_action(struct action_xlate_ctx *ctx,
2227 const struct nx_action_header *nah)
2229 const struct nx_action_resubmit *nar;
2230 const struct nx_action_set_tunnel *nast;
2231 const struct nx_action_set_queue *nasq;
2232 const struct nx_action_multipath *nam;
2233 const struct nx_action_autopath *naa;
2234 enum nx_action_subtype subtype = ntohs(nah->subtype);
2235 const struct ofhooks *ofhooks = ctx->ofproto->ofhooks;
2236 struct xlate_reg_state state;
2237 uint16_t autopath_port;
2240 assert(nah->vendor == htonl(NX_VENDOR_ID));
2242 case NXAST_RESUBMIT:
2243 nar = (const struct nx_action_resubmit *) nah;
2244 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2247 case NXAST_SET_TUNNEL:
2248 nast = (const struct nx_action_set_tunnel *) nah;
2249 tun_id = htonll(ntohl(nast->tun_id));
2250 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2251 ctx->flow.tun_id = tun_id;
2254 case NXAST_DROP_SPOOFED_ARP:
2255 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2256 nl_msg_put_flag(ctx->odp_actions,
2257 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2261 case NXAST_SET_QUEUE:
2262 nasq = (const struct nx_action_set_queue *) nah;
2263 xlate_set_queue_action(ctx, nasq);
2266 case NXAST_POP_QUEUE:
2267 add_pop_action(ctx);
2270 case NXAST_REG_MOVE:
2271 save_reg_state(ctx, &state);
2272 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2274 update_reg_state(ctx, &state);
2277 case NXAST_REG_LOAD:
2278 save_reg_state(ctx, &state);
2279 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2281 update_reg_state(ctx, &state);
2285 /* Nothing to do. */
2288 case NXAST_SET_TUNNEL64:
2289 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2290 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2291 ctx->flow.tun_id = tun_id;
2294 case NXAST_MULTIPATH:
2295 nam = (const struct nx_action_multipath *) nah;
2296 multipath_execute(nam, &ctx->flow);
2299 case NXAST_AUTOPATH:
2300 naa = (const struct nx_action_autopath *) nah;
2301 autopath_port = (ofhooks->autopath_cb
2302 ? ofhooks->autopath_cb(&ctx->flow, ntohl(naa->id),
2303 &ctx->tags, ctx->ofproto->aux)
2305 autopath_execute(naa, &ctx->flow, autopath_port);
2308 /* If you add a new action here that modifies flow data, don't forget to
2309 * update the flow key in ctx->flow at the same time. */
2311 case NXAST_SNAT__OBSOLETE:
2313 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2319 do_xlate_actions(const union ofp_action *in, size_t n_in,
2320 struct action_xlate_ctx *ctx)
2322 struct actions_iterator iter;
2323 const union ofp_action *ia;
2324 const struct ofport *port;
2326 port = get_port(ctx->ofproto, ctx->flow.in_port);
2327 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2328 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2329 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
2330 /* Drop this flow. */
2334 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2335 enum ofp_action_type type = ntohs(ia->type);
2336 const struct ofp_action_dl_addr *oada;
2340 xlate_output_action(ctx, &ia->output);
2343 case OFPAT_SET_VLAN_VID:
2344 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2345 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2346 xlate_set_dl_tci(ctx);
2349 case OFPAT_SET_VLAN_PCP:
2350 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2351 ctx->flow.vlan_tci |= htons(
2352 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2353 xlate_set_dl_tci(ctx);
2356 case OFPAT_STRIP_VLAN:
2357 ctx->flow.vlan_tci = htons(0);
2358 xlate_set_dl_tci(ctx);
2361 case OFPAT_SET_DL_SRC:
2362 oada = ((struct ofp_action_dl_addr *) ia);
2363 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2364 oada->dl_addr, ETH_ADDR_LEN);
2365 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
2368 case OFPAT_SET_DL_DST:
2369 oada = ((struct ofp_action_dl_addr *) ia);
2370 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2371 oada->dl_addr, ETH_ADDR_LEN);
2372 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
2375 case OFPAT_SET_NW_SRC:
2376 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
2377 ia->nw_addr.nw_addr);
2378 ctx->flow.nw_src = ia->nw_addr.nw_addr;
2381 case OFPAT_SET_NW_DST:
2382 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
2383 ia->nw_addr.nw_addr);
2384 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
2387 case OFPAT_SET_NW_TOS:
2388 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
2390 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
2393 case OFPAT_SET_TP_SRC:
2394 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
2395 ia->tp_port.tp_port);
2396 ctx->flow.tp_src = ia->tp_port.tp_port;
2399 case OFPAT_SET_TP_DST:
2400 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
2401 ia->tp_port.tp_port);
2402 ctx->flow.tp_dst = ia->tp_port.tp_port;
2406 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2410 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
2414 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
2421 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
2422 struct ofproto *ofproto, const struct flow *flow,
2423 const struct ofpbuf *packet)
2425 ctx->ofproto = ofproto;
2427 ctx->packet = packet;
2428 ctx->resubmit_hook = NULL;
2429 ctx->check_special = true;
2433 ofproto_process_cfm(struct ofproto *ofproto, const struct flow *flow,
2434 const struct ofpbuf *packet)
2436 struct ofport *ofport;
2438 ofport = get_port(ofproto, flow->in_port);
2439 if (ofport && ofport->cfm) {
2440 cfm_process_heartbeat(ofport->cfm, packet);
2444 static struct ofpbuf *
2445 xlate_actions(struct action_xlate_ctx *ctx,
2446 const union ofp_action *in, size_t n_in)
2448 COVERAGE_INC(ofproto_ofp2odp);
2450 ctx->odp_actions = ofpbuf_new(512);
2452 ctx->may_set_up_flow = true;
2453 ctx->nf_output_iface = NF_OUT_DROP;
2455 ctx->last_pop_priority = -1;
2457 if (ctx->check_special && cfm_should_process_flow(&ctx->flow)) {
2459 ofproto_process_cfm(ctx->ofproto, &ctx->flow, ctx->packet);
2461 ctx->may_set_up_flow = false;
2462 } else if (ctx->check_special
2463 && ctx->ofproto->ofhooks->special_cb
2464 && !ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet,
2465 ctx->ofproto->aux)) {
2466 ctx->may_set_up_flow = false;
2468 do_xlate_actions(in, n_in, ctx);
2471 remove_pop_action(ctx);
2473 /* Check with in-band control to see if we're allowed to set up this
2475 if (!connmgr_may_set_up_flow(ctx->ofproto->connmgr, &ctx->flow,
2476 ctx->odp_actions->data,
2477 ctx->odp_actions->size)) {
2478 ctx->may_set_up_flow = false;
2481 return ctx->odp_actions;
2484 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2485 * error message code (composed with ofp_mkerr()) for the caller to propagate
2486 * upward. Otherwise, returns 0.
2488 * The log message mentions 'msg_type'. */
2490 reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
2492 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2493 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
2494 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
2495 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
2498 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2505 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2507 struct ofproto *p = ofconn_get_ofproto(ofconn);
2508 struct ofp_packet_out *opo;
2509 struct ofpbuf payload, *buffer;
2510 union ofp_action *ofp_actions;
2511 struct action_xlate_ctx ctx;
2512 struct ofpbuf *odp_actions;
2513 struct ofpbuf request;
2515 size_t n_ofp_actions;
2519 COVERAGE_INC(ofproto_packet_out);
2521 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
2526 /* Get ofp_packet_out. */
2527 ofpbuf_use_const(&request, oh, ntohs(oh->length));
2528 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
2531 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
2532 &ofp_actions, &n_ofp_actions);
2538 if (opo->buffer_id != htonl(UINT32_MAX)) {
2539 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
2541 if (error || !buffer) {
2550 /* Extract flow, check actions. */
2551 flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
2553 error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
2559 action_xlate_ctx_init(&ctx, p, &flow, &payload);
2560 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
2561 dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
2562 ofpbuf_delete(odp_actions);
2565 ofpbuf_delete(buffer);
2570 update_port_config(struct ofproto *p, struct ofport *port,
2571 uint32_t config, uint32_t mask)
2573 mask &= config ^ port->opp.config;
2574 if (mask & OFPPC_PORT_DOWN) {
2575 if (config & OFPPC_PORT_DOWN) {
2576 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2578 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2581 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
2582 OFPPC_NO_FWD | OFPPC_NO_FLOOD)
2583 if (mask & REVALIDATE_BITS) {
2584 COVERAGE_INC(ofproto_costly_flags);
2585 port->opp.config ^= mask & REVALIDATE_BITS;
2586 p->need_revalidate = true;
2588 #undef REVALIDATE_BITS
2589 if (mask & OFPPC_NO_PACKET_IN) {
2590 port->opp.config ^= OFPPC_NO_PACKET_IN;
2595 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2597 struct ofproto *p = ofconn_get_ofproto(ofconn);
2598 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
2599 struct ofport *port;
2602 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
2607 port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
2609 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2610 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2611 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2613 update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
2614 if (opm->advertise) {
2615 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2621 static struct ofpbuf *
2622 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
2624 struct ofp_stats_reply *osr;
2627 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2628 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2630 osr->flags = htons(0);
2634 static struct ofpbuf *
2635 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
2637 const struct ofp_stats_request *osr
2638 = (const struct ofp_stats_request *) request;
2639 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
2643 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
2644 struct ofpbuf **msgp)
2646 struct ofpbuf *msg = *msgp;
2647 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2648 if (nbytes + msg->size > UINT16_MAX) {
2649 struct ofp_stats_reply *reply = msg->data;
2650 reply->flags = htons(OFPSF_REPLY_MORE);
2651 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
2652 ofconn_send_reply(ofconn, msg);
2654 return ofpbuf_put_uninit(*msgp, nbytes);
2657 static struct ofpbuf *
2658 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
2660 struct nicira_stats_msg *nsm;
2663 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
2664 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
2665 nsm->type = htons(OFPST_VENDOR);
2666 nsm->flags = htons(0);
2667 nsm->vendor = htonl(NX_VENDOR_ID);
2668 nsm->subtype = subtype;
2672 static struct ofpbuf *
2673 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
2675 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
2679 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
2680 struct ofpbuf **msgp)
2682 struct ofpbuf *msg = *msgp;
2683 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
2684 if (nbytes + msg->size > UINT16_MAX) {
2685 struct nicira_stats_msg *reply = msg->data;
2686 reply->flags = htons(OFPSF_REPLY_MORE);
2687 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
2688 ofconn_send_reply(ofconn, msg);
2690 ofpbuf_prealloc_tailroom(*msgp, nbytes);
2694 handle_desc_stats_request(struct ofconn *ofconn,
2695 const struct ofp_header *request)
2697 struct ofproto *p = ofconn_get_ofproto(ofconn);
2698 struct ofp_desc_stats *ods;
2701 msg = start_ofp_stats_reply(request, sizeof *ods);
2702 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
2703 memset(ods, 0, sizeof *ods);
2704 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
2705 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
2706 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
2707 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
2708 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2709 ofconn_send_reply(ofconn, msg);
2715 handle_table_stats_request(struct ofconn *ofconn,
2716 const struct ofp_header *request)
2718 struct ofproto *p = ofconn_get_ofproto(ofconn);
2719 struct ofp_table_stats *ots;
2722 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
2724 /* Classifier table. */
2725 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
2726 memset(ots, 0, sizeof *ots);
2727 strcpy(ots->name, "classifier");
2728 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
2729 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
2730 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
2731 ots->active_count = htonl(classifier_count(&p->cls));
2732 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
2733 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
2735 ofconn_send_reply(ofconn, msg);
2740 append_port_stat(struct ofport *port, struct ofconn *ofconn,
2741 struct ofpbuf **msgp)
2743 struct netdev_stats stats;
2744 struct ofp_port_stats *ops;
2746 /* Intentionally ignore return value, since errors will set
2747 * 'stats' to all-1s, which is correct for OpenFlow, and
2748 * netdev_get_stats() will log errors. */
2749 netdev_get_stats(port->netdev, &stats);
2751 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
2752 ops->port_no = htons(port->opp.port_no);
2753 memset(ops->pad, 0, sizeof ops->pad);
2754 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
2755 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
2756 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
2757 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
2758 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
2759 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
2760 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
2761 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
2762 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
2763 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
2764 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
2765 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
2769 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2771 struct ofproto *p = ofconn_get_ofproto(ofconn);
2772 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
2773 struct ofp_port_stats *ops;
2775 struct ofport *port;
2777 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
2778 if (psr->port_no != htons(OFPP_NONE)) {
2779 port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
2781 append_port_stat(port, ofconn, &msg);
2784 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2785 append_port_stat(port, ofconn, &msg);
2789 ofconn_send_reply(ofconn, msg);
2794 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
2796 long long int msecs = time_msec() - start;
2797 *sec = msecs / 1000;
2798 *nsec = (msecs % 1000) * (1000 * 1000);
2802 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
2806 calc_flow_duration__(start, &sec, &nsec);
2807 *sec_be = htonl(sec);
2808 *nsec_be = htonl(nsec);
2812 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
2813 ovs_be16 out_port, struct ofpbuf **replyp)
2815 struct ofp_flow_stats *ofs;
2816 uint64_t packet_count, byte_count;
2818 size_t act_len, len;
2820 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2824 act_len = sizeof *rule->actions * rule->n_actions;
2825 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2827 rule_get_stats(rule, &packet_count, &byte_count);
2829 ofs = append_ofp_stats_reply(len, ofconn, replyp);
2830 ofs->length = htons(len);
2833 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
2834 &ofs->match, rule->flow_cookie, &cookie);
2835 put_32aligned_be64(&ofs->cookie, cookie);
2836 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
2837 ofs->priority = htons(rule->cr.priority);
2838 ofs->idle_timeout = htons(rule->idle_timeout);
2839 ofs->hard_timeout = htons(rule->hard_timeout);
2840 memset(ofs->pad2, 0, sizeof ofs->pad2);
2841 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
2842 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
2843 if (rule->n_actions > 0) {
2844 memcpy(ofs->actions, rule->actions, act_len);
2849 is_valid_table(uint8_t table_id)
2851 if (table_id == 0 || table_id == 0xff) {
2854 /* It would probably be better to reply with an error but there doesn't
2855 * seem to be any appropriate value, so that might just be
2857 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
2864 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
2866 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
2867 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2868 struct ofpbuf *reply;
2870 COVERAGE_INC(ofproto_flows_req);
2871 reply = start_ofp_stats_reply(oh, 1024);
2872 if (is_valid_table(fsr->table_id)) {
2873 struct cls_cursor cursor;
2874 struct cls_rule target;
2877 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
2879 cls_cursor_init(&cursor, &ofproto->cls, &target);
2880 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2881 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
2884 ofconn_send_reply(ofconn, reply);
2890 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
2891 ovs_be16 out_port, struct ofpbuf **replyp)
2893 struct nx_flow_stats *nfs;
2894 uint64_t packet_count, byte_count;
2895 size_t act_len, start_len;
2896 struct ofpbuf *reply;
2898 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
2902 rule_get_stats(rule, &packet_count, &byte_count);
2904 act_len = sizeof *rule->actions * rule->n_actions;
2906 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
2907 start_len = (*replyp)->size;
2910 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
2913 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
2914 nfs->cookie = rule->flow_cookie;
2915 nfs->priority = htons(rule->cr.priority);
2916 nfs->idle_timeout = htons(rule->idle_timeout);
2917 nfs->hard_timeout = htons(rule->hard_timeout);
2918 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
2919 memset(nfs->pad2, 0, sizeof nfs->pad2);
2920 nfs->packet_count = htonll(packet_count);
2921 nfs->byte_count = htonll(byte_count);
2922 if (rule->n_actions > 0) {
2923 ofpbuf_put(reply, rule->actions, act_len);
2925 nfs->length = htons(reply->size - start_len);
2929 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
2931 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2932 struct nx_flow_stats_request *nfsr;
2933 struct cls_rule target;
2934 struct ofpbuf *reply;
2938 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2940 /* Dissect the message. */
2941 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2942 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
2947 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2950 COVERAGE_INC(ofproto_flows_req);
2951 reply = start_nxstats_reply(&nfsr->nsm, 1024);
2952 if (is_valid_table(nfsr->table_id)) {
2953 struct cls_cursor cursor;
2956 cls_cursor_init(&cursor, &ofproto->cls, &target);
2957 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2958 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
2961 ofconn_send_reply(ofconn, reply);
2967 flow_stats_ds(struct rule *rule, struct ds *results)
2969 uint64_t packet_count, byte_count;
2970 size_t act_len = sizeof *rule->actions * rule->n_actions;
2972 rule_get_stats(rule, &packet_count, &byte_count);
2974 ds_put_format(results, "duration=%llds, ",
2975 (time_msec() - rule->created) / 1000);
2976 ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0);
2977 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2978 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2979 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2980 cls_rule_format(&rule->cr, results);
2981 ds_put_char(results, ',');
2983 ofp_print_actions(results, &rule->actions->header, act_len);
2985 ds_put_cstr(results, "drop");
2987 ds_put_cstr(results, "\n");
2990 /* Adds a pretty-printed description of all flows to 'results', including
2991 * hidden flows (e.g., set up by in-band control). */
2993 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
2995 struct cls_cursor cursor;
2998 cls_cursor_init(&cursor, &p->cls, NULL);
2999 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3000 flow_stats_ds(rule, results);
3005 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
3006 ovs_be16 out_port, uint8_t table_id,
3007 struct ofp_aggregate_stats_reply *oasr)
3009 uint64_t total_packets = 0;
3010 uint64_t total_bytes = 0;
3013 COVERAGE_INC(ofproto_agg_request);
3015 if (is_valid_table(table_id)) {
3016 struct cls_cursor cursor;
3019 cls_cursor_init(&cursor, &ofproto->cls, target);
3020 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3021 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
3022 uint64_t packet_count;
3023 uint64_t byte_count;
3025 rule_get_stats(rule, &packet_count, &byte_count);
3027 total_packets += packet_count;
3028 total_bytes += byte_count;
3034 oasr->flow_count = htonl(n_flows);
3035 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
3036 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
3037 memset(oasr->pad, 0, sizeof oasr->pad);
3041 handle_aggregate_stats_request(struct ofconn *ofconn,
3042 const struct ofp_header *oh)
3044 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
3045 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3046 struct ofp_aggregate_stats_reply *reply;
3047 struct cls_rule target;
3050 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
3053 msg = start_ofp_stats_reply(oh, sizeof *reply);
3054 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
3055 query_aggregate_stats(ofproto, &target, request->out_port,
3056 request->table_id, reply);
3057 ofconn_send_reply(ofconn, msg);
3062 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
3064 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3065 struct nx_aggregate_stats_request *request;
3066 struct ofp_aggregate_stats_reply *reply;
3067 struct cls_rule target;
3072 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3074 /* Dissect the message. */
3075 request = ofpbuf_pull(&b, sizeof *request);
3076 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
3081 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3085 COVERAGE_INC(ofproto_flows_req);
3086 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
3087 reply = ofpbuf_put_uninit(buf, sizeof *reply);
3088 query_aggregate_stats(ofproto, &target, request->out_port,
3089 request->table_id, reply);
3090 ofconn_send_reply(ofconn, buf);
3095 struct queue_stats_cbdata {
3096 struct ofconn *ofconn;
3097 struct ofport *ofport;
3102 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3103 const struct netdev_queue_stats *stats)
3105 struct ofp_queue_stats *reply;
3107 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
3108 reply->port_no = htons(cbdata->ofport->opp.port_no);
3109 memset(reply->pad, 0, sizeof reply->pad);
3110 reply->queue_id = htonl(queue_id);
3111 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
3112 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
3113 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
3117 handle_queue_stats_dump_cb(uint32_t queue_id,
3118 struct netdev_queue_stats *stats,
3121 struct queue_stats_cbdata *cbdata = cbdata_;
3123 put_queue_stats(cbdata, queue_id, stats);
3127 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3128 struct queue_stats_cbdata *cbdata)
3130 cbdata->ofport = port;
3131 if (queue_id == OFPQ_ALL) {
3132 netdev_dump_queue_stats(port->netdev,
3133 handle_queue_stats_dump_cb, cbdata);
3135 struct netdev_queue_stats stats;
3137 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3138 put_queue_stats(cbdata, queue_id, &stats);
3144 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
3146 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3147 const struct ofp_queue_stats_request *qsr;
3148 struct queue_stats_cbdata cbdata;
3149 struct ofport *port;
3150 unsigned int port_no;
3153 qsr = ofputil_stats_body(oh);
3155 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3158 COVERAGE_INC(ofproto_queue_req);
3160 cbdata.ofconn = ofconn;
3161 cbdata.msg = start_ofp_stats_reply(oh, 128);
3163 port_no = ntohs(qsr->port_no);
3164 queue_id = ntohl(qsr->queue_id);
3165 if (port_no == OFPP_ALL) {
3166 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3167 handle_queue_stats_for_port(port, queue_id, &cbdata);
3169 } else if (port_no < ofproto->max_ports) {
3170 port = get_port(ofproto, ofp_port_to_odp_port(port_no));
3172 handle_queue_stats_for_port(port, queue_id, &cbdata);
3175 ofpbuf_delete(cbdata.msg);
3176 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
3178 ofconn_send_reply(ofconn, cbdata.msg);
3183 /* Updates 'facet''s used time. Caller is responsible for calling
3184 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3186 facet_update_time(struct ofproto *ofproto, struct facet *facet,
3189 if (used > facet->used) {
3191 if (used > facet->rule->used) {
3192 facet->rule->used = used;
3194 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3198 /* Folds the statistics from 'stats' into the counters in 'facet'.
3200 * Because of the meaning of a facet's counters, it only makes sense to do this
3201 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3202 * packet that was sent by hand or if it represents statistics that have been
3203 * cleared out of the datapath. */
3205 facet_update_stats(struct ofproto *ofproto, struct facet *facet,
3206 const struct dpif_flow_stats *stats)
3208 if (stats->n_packets || stats->used > facet->used) {
3209 facet_update_time(ofproto, facet, stats->used);
3210 facet->packet_count += stats->n_packets;
3211 facet->byte_count += stats->n_bytes;
3212 facet_push_stats(ofproto, facet);
3213 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3218 facet_push_stats(struct ofproto *ofproto, struct facet *facet)
3220 uint64_t rs_packets, rs_bytes;
3222 assert(facet->packet_count >= facet->rs_packet_count);
3223 assert(facet->byte_count >= facet->rs_byte_count);
3224 assert(facet->used >= facet->rs_used);
3226 rs_packets = facet->packet_count - facet->rs_packet_count;
3227 rs_bytes = facet->byte_count - facet->rs_byte_count;
3229 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3230 facet->rs_packet_count = facet->packet_count;
3231 facet->rs_byte_count = facet->byte_count;
3232 facet->rs_used = facet->used;
3234 flow_push_stats(ofproto, facet->rule, &facet->flow,
3235 rs_packets, rs_bytes, facet->used);
3239 struct ofproto_push {
3240 struct action_xlate_ctx ctx;
3247 push_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
3249 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3252 rule->packet_count += push->packets;
3253 rule->byte_count += push->bytes;
3254 rule->used = MAX(push->used, rule->used);
3258 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3259 * 'rule''s actions. */
3261 flow_push_stats(struct ofproto *ofproto, const struct rule *rule,
3262 struct flow *flow, uint64_t packets, uint64_t bytes,
3265 struct ofproto_push push;
3267 push.packets = packets;
3271 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3272 push.ctx.resubmit_hook = push_resubmit;
3273 ofpbuf_delete(xlate_actions(&push.ctx, rule->actions, rule->n_actions));
3276 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3277 * in which no matching flow already exists in the flow table.
3279 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3280 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
3281 * OpenFlow error code as encoded by ofp_mkerr() on failure.
3283 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3286 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
3288 struct ofproto *p = ofconn_get_ofproto(ofconn);
3289 struct ofpbuf *packet;
3294 if (fm->flags & OFPFF_CHECK_OVERLAP
3295 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
3296 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
3300 if (fm->buffer_id != UINT32_MAX) {
3301 error = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id,
3305 in_port = UINT16_MAX;
3308 rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
3309 fm->idle_timeout, fm->hard_timeout, fm->cookie,
3310 fm->flags & OFPFF_SEND_FLOW_REM);
3311 rule_insert(p, rule);
3313 rule_execute(p, rule, in_port, packet);
3318 static struct rule *
3319 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
3321 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
3325 send_buffered_packet(struct ofconn *ofconn,
3326 struct rule *rule, uint32_t buffer_id)
3328 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3329 struct ofpbuf *packet;
3333 if (buffer_id == UINT32_MAX) {
3337 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
3342 rule_execute(ofproto, rule, in_port, packet);
3347 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3349 struct modify_flows_cbdata {
3350 struct ofproto *ofproto;
3351 const struct flow_mod *fm;
3355 static int modify_flow(struct ofproto *, const struct flow_mod *,
3358 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
3359 * encoded by ofp_mkerr() on failure.
3361 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3364 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
3366 struct ofproto *p = ofconn_get_ofproto(ofconn);
3367 struct rule *match = NULL;
3368 struct cls_cursor cursor;
3371 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3372 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3373 if (!rule_is_hidden(rule)) {
3375 modify_flow(p, fm, rule);
3380 /* This credits the packet to whichever flow happened to match last.
3381 * That's weird. Maybe we should do a lookup for the flow that
3382 * actually matches the packet? Who knows. */
3383 send_buffered_packet(ofconn, match, fm->buffer_id);
3386 return add_flow(ofconn, fm);
3390 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3391 * code as encoded by ofp_mkerr() on failure.
3393 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3396 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
3398 struct ofproto *p = ofconn_get_ofproto(ofconn);
3399 struct rule *rule = find_flow_strict(p, fm);
3400 if (rule && !rule_is_hidden(rule)) {
3401 modify_flow(p, fm, rule);
3402 return send_buffered_packet(ofconn, rule, fm->buffer_id);
3404 return add_flow(ofconn, fm);
3408 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
3409 * been identified as a flow in 'p''s flow table to be modified, by changing
3410 * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
3411 * ofp_action[] structures). */
3413 modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
3415 size_t actions_len = fm->n_actions * sizeof *rule->actions;
3417 rule->flow_cookie = fm->cookie;
3419 /* If the actions are the same, do nothing. */
3420 if (fm->n_actions == rule->n_actions
3422 || !memcmp(fm->actions, rule->actions, actions_len))) {
3426 /* Replace actions. */
3427 free(rule->actions);
3428 rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
3429 rule->n_actions = fm->n_actions;
3431 p->need_revalidate = true;
3436 /* OFPFC_DELETE implementation. */
3438 static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
3440 /* Implements OFPFC_DELETE. */
3442 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
3444 struct rule *rule, *next_rule;
3445 struct cls_cursor cursor;
3447 cls_cursor_init(&cursor, &p->cls, &fm->cr);
3448 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3449 delete_flow(p, rule, htons(fm->out_port));
3453 /* Implements OFPFC_DELETE_STRICT. */
3455 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
3457 struct rule *rule = find_flow_strict(p, fm);
3459 delete_flow(p, rule, htons(fm->out_port));
3463 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
3464 * been identified as a flow to delete from 'p''s flow table, by deleting the
3465 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
3468 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
3469 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
3470 * specified 'out_port'. */
3472 delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
3474 if (rule_is_hidden(rule)) {
3478 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
3482 rule_send_removed(p, rule, OFPRR_DELETE);
3483 rule_remove(p, rule);
3487 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3489 struct ofproto *p = ofconn_get_ofproto(ofconn);
3493 error = reject_slave_controller(ofconn, "flow_mod");
3498 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
3503 /* We do not support the emergency flow cache. It will hopefully get
3504 * dropped from OpenFlow in the near future. */
3505 if (fm.flags & OFPFF_EMERG) {
3506 /* There isn't a good fit for an error code, so just state that the
3507 * flow table is full. */
3508 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
3511 error = validate_actions(fm.actions, fm.n_actions,
3512 &fm.cr.flow, p->max_ports);
3517 switch (fm.command) {
3519 return add_flow(ofconn, &fm);
3522 return modify_flows_loose(ofconn, &fm);
3524 case OFPFC_MODIFY_STRICT:
3525 return modify_flow_strict(ofconn, &fm);
3528 delete_flows_loose(p, &fm);
3531 case OFPFC_DELETE_STRICT:
3532 delete_flow_strict(p, &fm);
3536 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
3541 handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
3543 const struct nxt_tun_id_cookie *msg
3544 = (const struct nxt_tun_id_cookie *) oh;
3545 enum nx_flow_format flow_format;
3547 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
3548 ofconn_set_flow_format(ofconn, flow_format);
3554 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3556 struct nx_role_request *nrr = (struct nx_role_request *) oh;
3557 struct nx_role_request *reply;
3561 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
3562 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
3563 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3566 role = ntohl(nrr->role);
3567 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
3568 && role != NX_ROLE_SLAVE) {
3569 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
3571 /* There's no good error code for this. */
3572 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
3575 ofconn_set_role(ofconn, role);
3577 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
3578 reply->role = htonl(role);
3579 ofconn_send_reply(ofconn, buf);
3585 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
3587 const struct nxt_set_flow_format *msg
3588 = (const struct nxt_set_flow_format *) oh;
3591 format = ntohl(msg->format);
3592 if (format == NXFF_OPENFLOW10
3593 || format == NXFF_TUN_ID_FROM_COOKIE
3594 || format == NXFF_NXM) {
3595 ofconn_set_flow_format(ofconn, format);
3598 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3603 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
3605 struct ofp_header *ob;
3608 /* Currently, everything executes synchronously, so we can just
3609 * immediately send the barrier reply. */
3610 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
3611 ofconn_send_reply(ofconn, buf);
3616 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
3618 const struct ofp_header *oh = msg->data;
3619 const struct ofputil_msg_type *type;
3622 error = ofputil_decode_msg_type(oh, &type);
3627 switch (ofputil_msg_type_code(type)) {
3628 /* OpenFlow requests. */
3629 case OFPUTIL_OFPT_ECHO_REQUEST:
3630 return handle_echo_request(ofconn, oh);
3632 case OFPUTIL_OFPT_FEATURES_REQUEST:
3633 return handle_features_request(ofconn, oh);
3635 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
3636 return handle_get_config_request(ofconn, oh);
3638 case OFPUTIL_OFPT_SET_CONFIG:
3639 return handle_set_config(ofconn, msg->data);
3641 case OFPUTIL_OFPT_PACKET_OUT:
3642 return handle_packet_out(ofconn, oh);
3644 case OFPUTIL_OFPT_PORT_MOD:
3645 return handle_port_mod(ofconn, oh);
3647 case OFPUTIL_OFPT_FLOW_MOD:
3648 return handle_flow_mod(ofconn, oh);
3650 case OFPUTIL_OFPT_BARRIER_REQUEST:
3651 return handle_barrier_request(ofconn, oh);
3653 /* OpenFlow replies. */
3654 case OFPUTIL_OFPT_ECHO_REPLY:
3657 /* Nicira extension requests. */
3658 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
3659 return handle_tun_id_from_cookie(ofconn, oh);
3661 case OFPUTIL_NXT_ROLE_REQUEST:
3662 return handle_role_request(ofconn, oh);
3664 case OFPUTIL_NXT_SET_FLOW_FORMAT:
3665 return handle_nxt_set_flow_format(ofconn, oh);
3667 case OFPUTIL_NXT_FLOW_MOD:
3668 return handle_flow_mod(ofconn, oh);
3670 /* OpenFlow statistics requests. */
3671 case OFPUTIL_OFPST_DESC_REQUEST:
3672 return handle_desc_stats_request(ofconn, oh);
3674 case OFPUTIL_OFPST_FLOW_REQUEST:
3675 return handle_flow_stats_request(ofconn, oh);
3677 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
3678 return handle_aggregate_stats_request(ofconn, oh);
3680 case OFPUTIL_OFPST_TABLE_REQUEST:
3681 return handle_table_stats_request(ofconn, oh);
3683 case OFPUTIL_OFPST_PORT_REQUEST:
3684 return handle_port_stats_request(ofconn, oh);
3686 case OFPUTIL_OFPST_QUEUE_REQUEST:
3687 return handle_queue_stats_request(ofconn, oh);
3689 /* Nicira extension statistics requests. */
3690 case OFPUTIL_NXST_FLOW_REQUEST:
3691 return handle_nxst_flow(ofconn, oh);
3693 case OFPUTIL_NXST_AGGREGATE_REQUEST:
3694 return handle_nxst_aggregate(ofconn, oh);
3696 case OFPUTIL_INVALID:
3697 case OFPUTIL_OFPT_HELLO:
3698 case OFPUTIL_OFPT_ERROR:
3699 case OFPUTIL_OFPT_FEATURES_REPLY:
3700 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
3701 case OFPUTIL_OFPT_PACKET_IN:
3702 case OFPUTIL_OFPT_FLOW_REMOVED:
3703 case OFPUTIL_OFPT_PORT_STATUS:
3704 case OFPUTIL_OFPT_BARRIER_REPLY:
3705 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
3706 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
3707 case OFPUTIL_OFPST_DESC_REPLY:
3708 case OFPUTIL_OFPST_FLOW_REPLY:
3709 case OFPUTIL_OFPST_QUEUE_REPLY:
3710 case OFPUTIL_OFPST_PORT_REPLY:
3711 case OFPUTIL_OFPST_TABLE_REPLY:
3712 case OFPUTIL_OFPST_AGGREGATE_REPLY:
3713 case OFPUTIL_NXT_ROLE_REPLY:
3714 case OFPUTIL_NXT_FLOW_REMOVED:
3715 case OFPUTIL_NXST_FLOW_REPLY:
3716 case OFPUTIL_NXST_AGGREGATE_REPLY:
3718 if (VLOG_IS_WARN_ENABLED()) {
3719 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3720 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3723 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
3724 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
3726 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3732 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
3734 int error = handle_openflow__(ofconn, ofp_msg);
3736 send_error_oh(ofconn, ofp_msg->data, error);
3738 COVERAGE_INC(ofproto_recv_openflow);
3742 handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3744 struct facet *facet;
3747 /* Obtain in_port and tun_id, at least. */
3748 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3750 /* Set header pointers in 'flow'. */
3751 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
3753 if (cfm_should_process_flow(&flow)) {
3754 ofproto_process_cfm(p, &flow, upcall->packet);
3755 ofpbuf_delete(upcall->packet);
3757 } else if (p->ofhooks->special_cb
3758 && !p->ofhooks->special_cb(&flow, upcall->packet, p->aux)) {
3759 ofpbuf_delete(upcall->packet);
3763 /* Check with in-band control to see if this packet should be sent
3764 * to the local port regardless of the flow table. */
3765 if (connmgr_msg_in_hook(p->connmgr, &flow, upcall->packet)) {
3766 ofproto_send_packet(p, ODPP_LOCAL, 0, upcall->packet);
3769 facet = facet_lookup_valid(p, &flow);
3771 struct rule *rule = rule_lookup(p, &flow);
3773 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3774 struct ofport *port = get_port(p, flow.in_port);
3776 if (port->opp.config & OFPPC_NO_PACKET_IN) {
3777 COVERAGE_INC(ofproto_no_packet_in);
3778 /* XXX install 'drop' flow entry */
3779 ofpbuf_delete(upcall->packet);
3783 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
3787 COVERAGE_INC(ofproto_packet_in);
3788 send_packet_in(p, upcall, &flow, false);
3792 facet = facet_create(p, rule, &flow, upcall->packet);
3793 } else if (!facet->may_install) {
3794 /* The facet is not installable, that is, we need to process every
3795 * packet, so process the current packet's actions into 'facet'. */
3796 facet_make_actions(p, facet, upcall->packet);
3799 if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) {
3801 * Extra-special case for fail-open mode.
3803 * We are in fail-open mode and the packet matched the fail-open rule,
3804 * but we are connected to a controller too. We should send the packet
3805 * up to the controller in the hope that it will try to set up a flow
3806 * and thereby allow us to exit fail-open.
3808 * See the top-level comment in fail-open.c for more information.
3810 send_packet_in(p, upcall, &flow, true);
3813 facet_execute(p, facet, upcall->packet);
3814 facet_install(p, facet, false);
3818 handle_upcall(struct ofproto *p, struct dpif_upcall *upcall)
3822 switch (upcall->type) {
3823 case DPIF_UC_ACTION:
3824 COVERAGE_INC(ofproto_ctlr_action);
3825 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3826 send_packet_in(p, upcall, &flow, false);
3829 case DPIF_UC_SAMPLE:
3831 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
3832 ofproto_sflow_received(p->sflow, upcall, &flow);
3834 ofpbuf_delete(upcall->packet);
3838 handle_miss_upcall(p, upcall);
3841 case DPIF_N_UC_TYPES:
3843 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3848 /* Flow expiration. */
3850 static int ofproto_dp_max_idle(const struct ofproto *);
3851 static void ofproto_update_stats(struct ofproto *);
3852 static void rule_expire(struct ofproto *, struct rule *);
3853 static void ofproto_expire_facets(struct ofproto *, int dp_max_idle);
3855 /* This function is called periodically by ofproto_run(). Its job is to
3856 * collect updates for the flows that have been installed into the datapath,
3857 * most importantly when they last were used, and then use that information to
3858 * expire flows that have not been used recently.
3860 * Returns the number of milliseconds after which it should be called again. */
3862 ofproto_expire(struct ofproto *ofproto)
3864 struct rule *rule, *next_rule;
3865 struct cls_cursor cursor;
3868 /* Update stats for each flow in the datapath. */
3869 ofproto_update_stats(ofproto);
3871 /* Expire facets that have been idle too long. */
3872 dp_max_idle = ofproto_dp_max_idle(ofproto);
3873 ofproto_expire_facets(ofproto, dp_max_idle);
3875 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3876 cls_cursor_init(&cursor, &ofproto->cls, NULL);
3877 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
3878 rule_expire(ofproto, rule);
3881 /* Let the hook know that we're at a stable point: all outstanding data
3882 * in existing flows has been accounted to the account_cb. Thus, the
3883 * hook can now reasonably do operations that depend on having accurate
3884 * flow volume accounting (currently, that's just bond rebalancing). */
3885 if (ofproto->ofhooks->account_checkpoint_cb) {
3886 ofproto->ofhooks->account_checkpoint_cb(ofproto->aux);
3889 return MIN(dp_max_idle, 1000);
3892 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3894 * This function also pushes statistics updates to rules which each facet
3895 * resubmits into. Generally these statistics will be accurate. However, if a
3896 * facet changes the rule it resubmits into at some time in between
3897 * ofproto_update_stats() runs, it is possible that statistics accrued to the
3898 * old rule will be incorrectly attributed to the new rule. This could be
3899 * avoided by calling ofproto_update_stats() whenever rules are created or
3900 * deleted. However, the performance impact of making so many calls to the
3901 * datapath do not justify the benefit of having perfectly accurate statistics.
3904 ofproto_update_stats(struct ofproto *p)
3906 const struct dpif_flow_stats *stats;
3907 struct dpif_flow_dump dump;
3908 const struct nlattr *key;
3911 dpif_flow_dump_start(&dump, p->dpif);
3912 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3913 struct facet *facet;
3916 if (odp_flow_key_to_flow(key, key_len, &flow)) {
3920 odp_flow_key_format(key, key_len, &s);
3921 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
3927 facet = facet_find(p, &flow);
3929 if (facet && facet->installed) {
3931 if (stats->n_packets >= facet->dp_packet_count) {
3932 facet->packet_count += stats->n_packets - facet->dp_packet_count;
3934 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3937 if (stats->n_bytes >= facet->dp_byte_count) {
3938 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
3940 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3943 facet->dp_packet_count = stats->n_packets;
3944 facet->dp_byte_count = stats->n_bytes;
3946 facet_update_time(p, facet, stats->used);
3947 facet_account(p, facet, stats->n_bytes);
3948 facet_push_stats(p, facet);
3950 /* There's a flow in the datapath that we know nothing about.
3952 COVERAGE_INC(ofproto_unexpected_rule);
3953 dpif_flow_del(p->dpif, key, key_len, NULL);
3956 dpif_flow_dump_done(&dump);
3959 /* Calculates and returns the number of milliseconds of idle time after which
3960 * facets should expire from the datapath and we should fold their statistics
3961 * into their parent rules in userspace. */
3963 ofproto_dp_max_idle(const struct ofproto *ofproto)
3966 * Idle time histogram.
3968 * Most of the time a switch has a relatively small number of facets. When
3969 * this is the case we might as well keep statistics for all of them in
3970 * userspace and to cache them in the kernel datapath for performance as
3973 * As the number of facets increases, the memory required to maintain
3974 * statistics about them in userspace and in the kernel becomes
3975 * significant. However, with a large number of facets it is likely that
3976 * only a few of them are "heavy hitters" that consume a large amount of
3977 * bandwidth. At this point, only heavy hitters are worth caching in the
3978 * kernel and maintaining in userspaces; other facets we can discard.
3980 * The technique used to compute the idle time is to build a histogram with
3981 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
3982 * that is installed in the kernel gets dropped in the appropriate bucket.
3983 * After the histogram has been built, we compute the cutoff so that only
3984 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
3985 * cached. At least the most-recently-used bucket of facets is kept, so
3986 * actually an arbitrary number of facets can be kept in any given
3987 * expiration run (though the next run will delete most of those unless
3988 * they receive additional data).
3990 * This requires a second pass through the facets, in addition to the pass
3991 * made by ofproto_update_stats(), because the former function never looks
3992 * at uninstallable facets.
3994 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
3995 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3996 int buckets[N_BUCKETS] = { 0 };
3997 struct facet *facet;
4002 total = hmap_count(&ofproto->facets);
4003 if (total <= 1000) {
4004 return N_BUCKETS * BUCKET_WIDTH;
4007 /* Build histogram. */
4009 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
4010 long long int idle = now - facet->used;
4011 int bucket = (idle <= 0 ? 0
4012 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4013 : (unsigned int) idle / BUCKET_WIDTH);
4017 /* Find the first bucket whose flows should be expired. */
4018 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
4019 if (buckets[bucket]) {
4022 subtotal += buckets[bucket++];
4023 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
4028 if (VLOG_IS_DBG_ENABLED()) {
4032 ds_put_cstr(&s, "keep");
4033 for (i = 0; i < N_BUCKETS; i++) {
4035 ds_put_cstr(&s, ", drop");
4038 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4041 VLOG_INFO("%s: %s (msec:count)",
4042 dpif_name(ofproto->dpif), ds_cstr(&s));
4046 return bucket * BUCKET_WIDTH;
4050 facet_active_timeout(struct ofproto *ofproto, struct facet *facet)
4052 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
4053 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
4054 struct ofexpired expired;
4056 if (facet->installed) {
4057 struct dpif_flow_stats stats;
4059 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
4061 facet_update_stats(ofproto, facet, &stats);
4064 expired.flow = facet->flow;
4065 expired.packet_count = facet->packet_count;
4066 expired.byte_count = facet->byte_count;
4067 expired.used = facet->used;
4068 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4073 ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle)
4075 long long int cutoff = time_msec() - dp_max_idle;
4076 struct facet *facet, *next_facet;
4078 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
4079 facet_active_timeout(ofproto, facet);
4080 if (facet->used < cutoff) {
4081 facet_remove(ofproto, facet);
4086 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4087 * then delete it entirely. */
4089 rule_expire(struct ofproto *ofproto, struct rule *rule)
4091 struct facet *facet, *next_facet;
4095 /* Has 'rule' expired? */
4097 if (rule->hard_timeout
4098 && now > rule->created + rule->hard_timeout * 1000) {
4099 reason = OFPRR_HARD_TIMEOUT;
4100 } else if (rule->idle_timeout && list_is_empty(&rule->facets)
4101 && now >rule->used + rule->idle_timeout * 1000) {
4102 reason = OFPRR_IDLE_TIMEOUT;
4107 COVERAGE_INC(ofproto_expired);
4109 /* Update stats. (This is a no-op if the rule expired due to an idle
4110 * timeout, because that only happens when the rule has no facets left.) */
4111 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4112 facet_remove(ofproto, facet);
4115 /* Get rid of the rule. */
4116 if (!rule_is_hidden(rule)) {
4117 rule_send_removed(ofproto, rule, reason);
4119 rule_remove(ofproto, rule);
4123 rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason)
4125 struct ofputil_flow_removed fr;
4127 if (!rule->send_flow_removed) {
4132 fr.cookie = rule->flow_cookie;
4134 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
4135 fr.idle_timeout = rule->idle_timeout;
4136 fr.packet_count = rule->packet_count;
4137 fr.byte_count = rule->byte_count;
4139 connmgr_send_flow_removed(p->connmgr, &fr);
4142 /* Obtains statistics for 'rule' and stores them in '*packets' and '*bytes'.
4143 * The returned statistics include statistics for all of 'rule''s facets. */
4145 rule_get_stats(const struct rule *rule, uint64_t *packets, uint64_t *bytes)
4148 struct facet *facet;
4150 /* Start from historical data for 'rule' itself that are no longer tracked
4151 * in facets. This counts, for example, facets that have expired. */
4152 p = rule->packet_count;
4153 b = rule->byte_count;
4155 /* Add any statistics that are tracked by facets. This includes
4156 * statistical data recently updated by ofproto_update_stats() as well as
4157 * stats for packets that were executed "by hand" via dpif_execute(). */
4158 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4159 p += facet->packet_count;
4160 b += facet->byte_count;
4167 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
4168 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
4169 * their individual configurations.
4171 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
4172 * Otherwise, ownership is transferred to this function. */
4174 send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall,
4175 const struct flow *flow, bool clone)
4177 struct ofputil_packet_in pin;
4179 pin.packet = upcall->packet;
4180 pin.in_port = odp_port_to_ofp_port(flow->in_port);
4181 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
4182 pin.buffer_id = 0; /* not yet known */
4183 pin.send_len = upcall->userdata;
4184 connmgr_send_packet_in(ofproto->connmgr, upcall, flow,
4185 clone ? NULL : upcall->packet);
4189 pick_datapath_id(const struct ofproto *ofproto)
4191 const struct ofport *port;
4193 port = get_port(ofproto, ODPP_LOCAL);
4195 uint8_t ea[ETH_ADDR_LEN];
4198 error = netdev_get_etheraddr(port->netdev, ea);
4200 return eth_addr_to_uint64(ea);
4202 VLOG_WARN("could not get MAC address for %s (%s)",
4203 netdev_get_name(port->netdev), strerror(error));
4205 return ofproto->fallback_dpid;
4209 pick_fallback_dpid(void)
4211 uint8_t ea[ETH_ADDR_LEN];
4212 eth_addr_nicira_random(ea);
4213 return eth_addr_to_uint64(ea);
4217 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
4218 void *aux OVS_UNUSED)
4220 const struct shash_node *node;
4224 SHASH_FOR_EACH (node, &all_ofprotos) {
4225 ds_put_format(&results, "%s\n", node->name);
4227 unixctl_command_reply(conn, 200, ds_cstr(&results));
4228 ds_destroy(&results);
4231 struct ofproto_trace {
4232 struct action_xlate_ctx ctx;
4238 trace_format_rule(struct ds *result, int level, const struct rule *rule)
4240 ds_put_char_multiple(result, '\t', level);
4242 ds_put_cstr(result, "No match\n");
4246 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
4247 ntohll(rule->flow_cookie));
4248 cls_rule_format(&rule->cr, result);
4249 ds_put_char(result, '\n');
4251 ds_put_char_multiple(result, '\t', level);
4252 ds_put_cstr(result, "OpenFlow ");
4253 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
4254 rule->n_actions * sizeof *rule->actions);
4255 ds_put_char(result, '\n');
4259 trace_format_flow(struct ds *result, int level, const char *title,
4260 struct ofproto_trace *trace)
4262 ds_put_char_multiple(result, '\t', level);
4263 ds_put_format(result, "%s: ", title);
4264 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4265 ds_put_cstr(result, "unchanged");
4267 flow_format(result, &trace->ctx.flow);
4268 trace->flow = trace->ctx.flow;
4270 ds_put_char(result, '\n');
4274 trace_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
4276 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4277 struct ds *result = trace->result;
4279 ds_put_char(result, '\n');
4280 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4281 trace_format_rule(result, ctx->recurse + 1, rule);
4285 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4286 void *aux OVS_UNUSED)
4288 char *dpname, *in_port_s, *tun_id_s, *packet_s;
4289 char *args = xstrdup(args_);
4290 char *save_ptr = NULL;
4291 struct ofproto *ofproto;
4292 struct ofpbuf packet;
4300 ofpbuf_init(&packet, strlen(args) / 2);
4303 dpname = strtok_r(args, " ", &save_ptr);
4304 tun_id_s = strtok_r(NULL, " ", &save_ptr);
4305 in_port_s = strtok_r(NULL, " ", &save_ptr);
4306 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4307 if (!dpname || !in_port_s || !packet_s) {
4308 unixctl_command_reply(conn, 501, "Bad command syntax");
4312 ofproto = shash_find_data(&all_ofprotos, dpname);
4314 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4319 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
4320 in_port = ofp_port_to_odp_port(atoi(in_port_s));
4322 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
4323 packet_s += strspn(packet_s, " ");
4324 if (*packet_s != '\0') {
4325 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4328 if (packet.size < ETH_HEADER_LEN) {
4329 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
4333 ds_put_cstr(&result, "Packet: ");
4334 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
4335 ds_put_cstr(&result, s);
4338 flow_extract(&packet, tun_id, in_port, &flow);
4339 ds_put_cstr(&result, "Flow: ");
4340 flow_format(&result, &flow);
4341 ds_put_char(&result, '\n');
4343 rule = rule_lookup(ofproto, &flow);
4344 trace_format_rule(&result, 0, rule);
4346 struct ofproto_trace trace;
4347 struct ofpbuf *odp_actions;
4349 trace.result = &result;
4351 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
4352 trace.ctx.resubmit_hook = trace_resubmit;
4353 odp_actions = xlate_actions(&trace.ctx,
4354 rule->actions, rule->n_actions);
4356 ds_put_char(&result, '\n');
4357 trace_format_flow(&result, 0, "Final flow", &trace);
4358 ds_put_cstr(&result, "Datapath actions: ");
4359 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4360 ofpbuf_delete(odp_actions);
4363 unixctl_command_reply(conn, 200, ds_cstr(&result));
4366 ds_destroy(&result);
4367 ofpbuf_uninit(&packet);
4372 ofproto_unixctl_init(void)
4374 static bool registered;
4380 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
4381 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4385 default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
4386 struct ofpbuf *odp_actions, tag_type *tags,
4387 uint16_t *nf_output_iface, void *ofproto_)
4389 struct ofproto *ofproto = ofproto_;
4390 struct mac_entry *dst_mac;
4392 /* Drop frames for reserved multicast addresses. */
4393 if (eth_addr_is_reserved(flow->dl_dst)) {
4397 /* Learn source MAC (but don't try to learn from revalidation). */
4399 && mac_learning_may_learn(ofproto->ml, flow->dl_src, 0)) {
4400 struct mac_entry *src_mac;
4402 src_mac = mac_learning_insert(ofproto->ml, flow->dl_src, 0);
4403 if (mac_entry_is_new(src_mac) || src_mac->port.i != flow->in_port) {
4404 /* The log messages here could actually be useful in debugging,
4405 * so keep the rate limit relatively high. */
4406 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4407 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
4408 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
4410 ofproto_revalidate(ofproto,
4411 mac_learning_changed(ofproto->ml, src_mac));
4412 src_mac->port.i = flow->in_port;
4416 /* Determine output port. */
4417 dst_mac = mac_learning_lookup(ofproto->ml, flow->dl_dst, 0, tags);
4419 flood_packets(ofproto, flow->in_port, OFPPC_NO_FLOOD,
4420 nf_output_iface, odp_actions);
4422 int out_port = dst_mac->port.i;
4423 if (out_port != flow->in_port) {
4424 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, out_port);
4425 *nf_output_iface = out_port;
4434 static const struct ofhooks default_ofhooks = {
4435 default_normal_ofhook_cb,