2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-provider.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
31 #include "fail-open.h"
34 #include "mac-learning.h"
35 #include "multipath.h"
42 #include "ofp-print.h"
43 #include "ofproto-dpif-sflow.h"
44 #include "poll-loop.h"
46 #include "unaligned.h"
48 #include "vlan-bitmap.h"
51 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
53 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
54 COVERAGE_DEFINE(ofproto_dpif_expired);
55 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
56 COVERAGE_DEFINE(ofproto_dpif_xlate);
57 COVERAGE_DEFINE(facet_changed_rule);
58 COVERAGE_DEFINE(facet_invalidated);
59 COVERAGE_DEFINE(facet_revalidate);
60 COVERAGE_DEFINE(facet_unexpected);
62 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
63 * flow translation. */
64 #define MAX_RESUBMIT_RECURSION 16
72 long long int used; /* Time last used; time created if not used. */
76 * - Do include packets and bytes from facets that have been deleted or
77 * whose own statistics have been folded into the rule.
79 * - Do include packets and bytes sent "by hand" that were accounted to
80 * the rule without any facet being involved (this is a rare corner
81 * case in rule_execute()).
83 * - Do not include packet or bytes that can be obtained from any facet's
84 * packet_count or byte_count member or that can be obtained from the
85 * datapath by, e.g., dpif_flow_get() for any facet.
87 uint64_t packet_count; /* Number of packets received. */
88 uint64_t byte_count; /* Number of bytes received. */
90 struct list facets; /* List of "struct facet"s. */
93 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
95 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
98 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *ofproto,
99 const struct flow *flow);
101 #define MAX_MIRRORS 32
102 typedef uint32_t mirror_mask_t;
103 #define MIRROR_MASK_C(X) UINT32_C(X)
104 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
106 struct ofproto_dpif *ofproto; /* Owning ofproto. */
107 size_t idx; /* In ofproto's "mirrors" array. */
108 void *aux; /* Key supplied by ofproto's client. */
109 char *name; /* Identifier for log messages. */
111 /* Selection criteria. */
112 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
113 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
114 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
116 /* Output (mutually exclusive). */
117 struct ofbundle *out; /* Output port or NULL. */
118 int out_vlan; /* Output VLAN or -1. */
121 static void mirror_destroy(struct ofmirror *);
123 /* A group of one or more OpenFlow ports. */
124 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
126 struct ofproto_dpif *ofproto; /* Owning ofproto. */
127 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
128 void *aux; /* Key supplied by ofproto's client. */
129 char *name; /* Identifier for log messages. */
132 struct list ports; /* Contains "struct ofport"s. */
133 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
134 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
135 * NULL if all VLANs are trunked. */
136 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
137 struct bond *bond; /* Nonnull iff more than one port. */
140 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
142 /* Port mirroring info. */
143 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
144 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
145 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
148 static void bundle_remove(struct ofport *);
149 static void bundle_destroy(struct ofbundle *);
150 static void bundle_del_port(struct ofport_dpif *);
151 static void bundle_run(struct ofbundle *);
152 static void bundle_wait(struct ofbundle *);
154 struct action_xlate_ctx {
155 /* action_xlate_ctx_init() initializes these members. */
158 struct ofproto_dpif *ofproto;
160 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
161 * this flow when actions change header fields. */
164 /* The packet corresponding to 'flow', or a null pointer if we are
165 * revalidating without a packet to refer to. */
166 const struct ofpbuf *packet;
168 /* If nonnull, called just before executing a resubmit action.
170 * This is normally null so the client has to set it manually after
171 * calling action_xlate_ctx_init(). */
172 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
174 /* xlate_actions() initializes and uses these members. The client might want
175 * to look at them after it returns. */
177 struct ofpbuf *odp_actions; /* Datapath actions. */
178 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
179 bool may_set_up_flow; /* True ordinarily; false if the actions must
180 * be reassessed for every packet. */
181 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
183 /* xlate_actions() initializes and uses these members, but the client has no
184 * reason to look at them. */
186 int recurse; /* Recursion level, via xlate_table_action. */
187 uint32_t priority; /* Current flow priority. 0 if none. */
188 struct flow base_flow; /* Flow at the last commit. */
189 uint32_t base_priority; /* Priority at the last commit. */
192 static void action_xlate_ctx_init(struct action_xlate_ctx *,
193 struct ofproto_dpif *, const struct flow *,
194 const struct ofpbuf *);
195 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
196 const union ofp_action *in, size_t n_in);
198 /* An exact-match instantiation of an OpenFlow flow. */
200 long long int used; /* Time last used; time created if not used. */
204 * - Do include packets and bytes sent "by hand", e.g. with
207 * - Do include packets and bytes that were obtained from the datapath
208 * when a flow was deleted (e.g. dpif_flow_del()) or when its
209 * statistics were reset (e.g. dpif_flow_put() with
210 * DPIF_FP_ZERO_STATS).
212 * - Do not include any packets or bytes that can currently be obtained
213 * from the datapath by, e.g., dpif_flow_get().
215 uint64_t packet_count; /* Number of packets received. */
216 uint64_t byte_count; /* Number of bytes received. */
218 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
219 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
221 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
222 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
223 long long int rs_used; /* Used time pushed to resubmit children. */
225 /* Number of bytes passed to account_cb. This may include bytes that can
226 * currently obtained from the datapath (thus, it can be greater than
228 uint64_t accounted_bytes;
230 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
231 struct list list_node; /* In owning rule's 'facets' list. */
232 struct rule_dpif *rule; /* Owning rule. */
233 struct flow flow; /* Exact-match flow. */
234 bool installed; /* Installed in datapath? */
235 bool may_install; /* True ordinarily; false if actions must
236 * be reassessed for every packet. */
237 size_t actions_len; /* Number of bytes in actions[]. */
238 struct nlattr *actions; /* Datapath actions. */
239 tag_type tags; /* Tags. */
240 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
243 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
244 const struct ofpbuf *packet);
245 static void facet_remove(struct ofproto_dpif *, struct facet *);
246 static void facet_free(struct facet *);
248 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
249 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
250 const struct flow *);
251 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
253 static void facet_execute(struct ofproto_dpif *, struct facet *,
254 struct ofpbuf *packet);
256 static int facet_put__(struct ofproto_dpif *, struct facet *,
257 const struct nlattr *actions, size_t actions_len,
258 struct dpif_flow_stats *);
259 static void facet_install(struct ofproto_dpif *, struct facet *,
261 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
262 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
264 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
265 const struct ofpbuf *packet);
266 static void facet_update_time(struct ofproto_dpif *, struct facet *,
268 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
269 const struct dpif_flow_stats *);
270 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
271 static void facet_push_stats(struct facet *);
272 static void facet_account(struct ofproto_dpif *, struct facet *,
273 uint64_t extra_bytes);
275 static bool facet_is_controller_flow(struct facet *);
277 static void flow_push_stats(const struct rule_dpif *,
278 struct flow *, uint64_t packets, uint64_t bytes,
285 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
286 struct list bundle_node; /* In struct ofbundle's "ports" list. */
287 struct cfm *cfm; /* Connectivity Fault Management, if any. */
288 tag_type tag; /* Tag associated with this port. */
289 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
292 static struct ofport_dpif *
293 ofport_dpif_cast(const struct ofport *ofport)
295 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
296 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
299 static void port_run(struct ofport_dpif *);
300 static void port_wait(struct ofport_dpif *);
301 static int set_cfm(struct ofport *, const struct cfm_settings *);
303 struct dpif_completion {
304 struct list list_node;
305 struct ofoperation *op;
308 struct ofproto_dpif {
317 struct netflow *netflow;
318 struct dpif_sflow *sflow;
319 struct hmap bundles; /* Contains "struct ofbundle"s. */
320 struct mac_learning *ml;
321 struct ofmirror *mirrors[MAX_MIRRORS];
322 bool has_bonded_bundles;
325 struct timer next_expiration;
329 bool need_revalidate;
330 struct tag_set revalidate_set;
332 /* Support for debugging async flow mods. */
333 struct list completions;
336 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
337 * for debugging the asynchronous flow_mod implementation.) */
340 static void ofproto_dpif_unixctl_init(void);
342 static struct ofproto_dpif *
343 ofproto_dpif_cast(const struct ofproto *ofproto)
345 assert(ofproto->ofproto_class == &ofproto_dpif_class);
346 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
349 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
351 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
354 /* Packet processing. */
355 static void update_learning_table(struct ofproto_dpif *,
356 const struct flow *, int vlan,
358 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
359 bool have_packet, tag_type *, int *vlanp,
360 struct ofbundle **in_bundlep);
361 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
363 /* Flow expiration. */
364 static int expire(struct ofproto_dpif *);
367 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
368 const struct ofpbuf *packet);
370 /* Global variables. */
371 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
373 /* Factory functions. */
376 enumerate_types(struct sset *types)
378 dp_enumerate_types(types);
382 enumerate_names(const char *type, struct sset *names)
384 return dp_enumerate_names(type, names);
388 del(const char *type, const char *name)
393 error = dpif_open(name, type, &dpif);
395 error = dpif_delete(dpif);
401 /* Basic life-cycle. */
403 static struct ofproto *
406 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
411 dealloc(struct ofproto *ofproto_)
413 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
418 construct(struct ofproto *ofproto_)
420 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
421 const char *name = ofproto->up.name;
425 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
427 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
431 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
432 ofproto->n_matches = 0;
434 error = dpif_recv_set_mask(ofproto->dpif,
435 ((1u << DPIF_UC_MISS) |
436 (1u << DPIF_UC_ACTION) |
437 (1u << DPIF_UC_SAMPLE)));
439 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
440 dpif_close(ofproto->dpif);
443 dpif_flow_flush(ofproto->dpif);
444 dpif_recv_purge(ofproto->dpif);
446 ofproto->netflow = NULL;
447 ofproto->sflow = NULL;
448 hmap_init(&ofproto->bundles);
449 ofproto->ml = mac_learning_create();
450 for (i = 0; i < MAX_MIRRORS; i++) {
451 ofproto->mirrors[i] = NULL;
453 ofproto->has_bonded_bundles = false;
455 timer_set_duration(&ofproto->next_expiration, 1000);
457 hmap_init(&ofproto->facets);
458 ofproto->need_revalidate = false;
459 tag_set_init(&ofproto->revalidate_set);
461 list_init(&ofproto->completions);
463 ofproto->up.tables = xmalloc(sizeof *ofproto->up.tables);
464 classifier_init(&ofproto->up.tables[0]);
465 ofproto->up.n_tables = 1;
467 ofproto_dpif_unixctl_init();
473 complete_operations(struct ofproto_dpif *ofproto)
475 struct dpif_completion *c, *next;
477 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
478 ofoperation_complete(c->op, 0);
479 list_remove(&c->list_node);
485 destruct(struct ofproto *ofproto_)
487 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
488 struct rule_dpif *rule, *next_rule;
489 struct cls_cursor cursor;
492 complete_operations(ofproto);
494 cls_cursor_init(&cursor, &ofproto->up.tables[0], NULL);
495 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
496 ofproto_rule_destroy(&rule->up);
499 for (i = 0; i < MAX_MIRRORS; i++) {
500 mirror_destroy(ofproto->mirrors[i]);
503 netflow_destroy(ofproto->netflow);
504 dpif_sflow_destroy(ofproto->sflow);
505 hmap_destroy(&ofproto->bundles);
506 mac_learning_destroy(ofproto->ml);
508 hmap_destroy(&ofproto->facets);
510 dpif_close(ofproto->dpif);
514 run(struct ofproto *ofproto_)
516 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
517 struct ofport_dpif *ofport;
518 struct ofbundle *bundle;
522 complete_operations(ofproto);
524 dpif_run(ofproto->dpif);
526 for (i = 0; i < 50; i++) {
527 struct dpif_upcall packet;
530 error = dpif_recv(ofproto->dpif, &packet);
532 if (error == ENODEV) {
533 /* Datapath destroyed. */
539 handle_upcall(ofproto, &packet);
542 if (timer_expired(&ofproto->next_expiration)) {
543 int delay = expire(ofproto);
544 timer_set_duration(&ofproto->next_expiration, delay);
547 if (ofproto->netflow) {
548 netflow_run(ofproto->netflow);
550 if (ofproto->sflow) {
551 dpif_sflow_run(ofproto->sflow);
554 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
557 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
561 /* Now revalidate if there's anything to do. */
562 if (ofproto->need_revalidate
563 || !tag_set_is_empty(&ofproto->revalidate_set)) {
564 struct tag_set revalidate_set = ofproto->revalidate_set;
565 bool revalidate_all = ofproto->need_revalidate;
566 struct facet *facet, *next;
568 /* Clear the revalidation flags. */
569 tag_set_init(&ofproto->revalidate_set);
570 ofproto->need_revalidate = false;
572 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
574 || tag_set_intersects(&revalidate_set, facet->tags)) {
575 facet_revalidate(ofproto, facet);
584 wait(struct ofproto *ofproto_)
586 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
587 struct ofport_dpif *ofport;
588 struct ofbundle *bundle;
590 if (!clogged && !list_is_empty(&ofproto->completions)) {
591 poll_immediate_wake();
594 dpif_wait(ofproto->dpif);
595 dpif_recv_wait(ofproto->dpif);
596 if (ofproto->sflow) {
597 dpif_sflow_wait(ofproto->sflow);
599 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
600 poll_immediate_wake();
602 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
605 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
608 if (ofproto->need_revalidate) {
609 /* Shouldn't happen, but if it does just go around again. */
610 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
611 poll_immediate_wake();
613 timer_wait(&ofproto->next_expiration);
618 flush(struct ofproto *ofproto_)
620 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
621 struct facet *facet, *next_facet;
623 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
624 /* Mark the facet as not installed so that facet_remove() doesn't
625 * bother trying to uninstall it. There is no point in uninstalling it
626 * individually since we are about to blow away all the facets with
627 * dpif_flow_flush(). */
628 facet->installed = false;
629 facet->dp_packet_count = 0;
630 facet->dp_byte_count = 0;
631 facet_remove(ofproto, facet);
633 dpif_flow_flush(ofproto->dpif);
637 get_features(struct ofproto *ofproto_ OVS_UNUSED,
638 bool *arp_match_ip, uint32_t *actions)
640 *arp_match_ip = true;
641 *actions = ((1u << OFPAT_OUTPUT) |
642 (1u << OFPAT_SET_VLAN_VID) |
643 (1u << OFPAT_SET_VLAN_PCP) |
644 (1u << OFPAT_STRIP_VLAN) |
645 (1u << OFPAT_SET_DL_SRC) |
646 (1u << OFPAT_SET_DL_DST) |
647 (1u << OFPAT_SET_NW_SRC) |
648 (1u << OFPAT_SET_NW_DST) |
649 (1u << OFPAT_SET_NW_TOS) |
650 (1u << OFPAT_SET_TP_SRC) |
651 (1u << OFPAT_SET_TP_DST) |
652 (1u << OFPAT_ENQUEUE));
656 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
658 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
661 strcpy(ots->name, "classifier");
663 dpif_get_dp_stats(ofproto->dpif, &s);
664 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
665 put_32aligned_be64(&ots->matched_count,
666 htonll(s.n_hit + ofproto->n_matches));
670 set_netflow(struct ofproto *ofproto_,
671 const struct netflow_options *netflow_options)
673 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
675 if (netflow_options) {
676 if (!ofproto->netflow) {
677 ofproto->netflow = netflow_create();
679 return netflow_set_options(ofproto->netflow, netflow_options);
681 netflow_destroy(ofproto->netflow);
682 ofproto->netflow = NULL;
687 static struct ofport *
690 struct ofport_dpif *port = xmalloc(sizeof *port);
695 port_dealloc(struct ofport *port_)
697 struct ofport_dpif *port = ofport_dpif_cast(port_);
702 port_construct(struct ofport *port_)
704 struct ofport_dpif *port = ofport_dpif_cast(port_);
705 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
707 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
710 port->tag = tag_create_random();
712 if (ofproto->sflow) {
713 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
714 netdev_get_name(port->up.netdev));
721 port_destruct(struct ofport *port_)
723 struct ofport_dpif *port = ofport_dpif_cast(port_);
724 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
726 bundle_remove(port_);
727 set_cfm(port_, NULL);
728 if (ofproto->sflow) {
729 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
734 port_modified(struct ofport *port_)
736 struct ofport_dpif *port = ofport_dpif_cast(port_);
738 if (port->bundle && port->bundle->bond) {
739 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
744 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
746 struct ofport_dpif *port = ofport_dpif_cast(port_);
747 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
748 ovs_be32 changed = old_config ^ port->up.opp.config;
750 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
751 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
752 ofproto->need_revalidate = true;
757 set_sflow(struct ofproto *ofproto_,
758 const struct ofproto_sflow_options *sflow_options)
760 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
761 struct dpif_sflow *ds = ofproto->sflow;
764 struct ofport_dpif *ofport;
766 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
767 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
768 dpif_sflow_add_port(ds, ofport->odp_port,
769 netdev_get_name(ofport->up.netdev));
772 dpif_sflow_set_options(ds, sflow_options);
774 dpif_sflow_destroy(ds);
775 ofproto->sflow = NULL;
781 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
783 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
790 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
793 if (cfm_configure(ofport->cfm, s)) {
799 cfm_destroy(ofport->cfm);
805 get_cfm_fault(const struct ofport *ofport_)
807 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
809 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
814 /* Expires all MAC learning entries associated with 'port' and forces ofproto
815 * to revalidate every flow. */
817 bundle_flush_macs(struct ofbundle *bundle)
819 struct ofproto_dpif *ofproto = bundle->ofproto;
820 struct mac_learning *ml = ofproto->ml;
821 struct mac_entry *mac, *next_mac;
823 ofproto->need_revalidate = true;
824 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
825 if (mac->port.p == bundle) {
826 mac_learning_expire(ml, mac);
831 static struct ofbundle *
832 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
834 struct ofbundle *bundle;
836 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
838 if (bundle->aux == aux) {
845 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
846 * ones that are found to 'bundles'. */
848 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
849 void **auxes, size_t n_auxes,
850 struct hmapx *bundles)
855 for (i = 0; i < n_auxes; i++) {
856 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
858 hmapx_add(bundles, bundle);
864 bundle_del_port(struct ofport_dpif *port)
866 struct ofbundle *bundle = port->bundle;
868 bundle->ofproto->need_revalidate = true;
870 list_remove(&port->bundle_node);
874 lacp_slave_unregister(bundle->lacp, port);
877 bond_slave_unregister(bundle->bond, port);
880 bundle->floodable = true;
881 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
882 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
883 bundle->floodable = false;
889 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
890 struct lacp_slave_settings *lacp,
891 uint32_t bond_stable_id)
893 struct ofport_dpif *port;
895 port = get_ofp_port(bundle->ofproto, ofp_port);
900 if (port->bundle != bundle) {
901 bundle->ofproto->need_revalidate = true;
903 bundle_del_port(port);
906 port->bundle = bundle;
907 list_push_back(&bundle->ports, &port->bundle_node);
908 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
909 bundle->floodable = false;
913 lacp_slave_register(bundle->lacp, port, lacp);
916 port->bond_stable_id = bond_stable_id;
922 bundle_destroy(struct ofbundle *bundle)
924 struct ofproto_dpif *ofproto;
925 struct ofport_dpif *port, *next_port;
932 ofproto = bundle->ofproto;
933 for (i = 0; i < MAX_MIRRORS; i++) {
934 struct ofmirror *m = ofproto->mirrors[i];
936 if (m->out == bundle) {
938 } else if (hmapx_find_and_delete(&m->srcs, bundle)
939 || hmapx_find_and_delete(&m->dsts, bundle)) {
940 ofproto->need_revalidate = true;
945 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
946 bundle_del_port(port);
949 bundle_flush_macs(bundle);
950 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
952 free(bundle->trunks);
953 lacp_destroy(bundle->lacp);
954 bond_destroy(bundle->bond);
959 bundle_set(struct ofproto *ofproto_, void *aux,
960 const struct ofproto_bundle_settings *s)
962 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
963 bool need_flush = false;
964 const unsigned long *trunks;
965 struct ofport_dpif *port;
966 struct ofbundle *bundle;
971 bundle_destroy(bundle_lookup(ofproto, aux));
975 assert(s->n_slaves == 1 || s->bond != NULL);
976 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
978 bundle = bundle_lookup(ofproto, aux);
980 bundle = xmalloc(sizeof *bundle);
982 bundle->ofproto = ofproto;
983 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
984 hash_pointer(aux, 0));
988 list_init(&bundle->ports);
990 bundle->trunks = NULL;
994 bundle->floodable = true;
996 bundle->src_mirrors = 0;
997 bundle->dst_mirrors = 0;
998 bundle->mirror_out = 0;
1001 if (!bundle->name || strcmp(s->name, bundle->name)) {
1003 bundle->name = xstrdup(s->name);
1008 if (!bundle->lacp) {
1009 bundle->lacp = lacp_create();
1011 lacp_configure(bundle->lacp, s->lacp);
1013 lacp_destroy(bundle->lacp);
1014 bundle->lacp = NULL;
1017 /* Update set of ports. */
1019 for (i = 0; i < s->n_slaves; i++) {
1020 if (!bundle_add_port(bundle, s->slaves[i],
1021 s->lacp ? &s->lacp_slaves[i] : NULL,
1022 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1026 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1027 struct ofport_dpif *next_port;
1029 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1030 for (i = 0; i < s->n_slaves; i++) {
1031 if (s->slaves[i] == port->up.ofp_port) {
1036 bundle_del_port(port);
1040 assert(list_size(&bundle->ports) <= s->n_slaves);
1042 if (list_is_empty(&bundle->ports)) {
1043 bundle_destroy(bundle);
1048 if (s->vlan != bundle->vlan) {
1049 bundle->vlan = s->vlan;
1053 /* Get trunked VLANs. */
1054 trunks = s->vlan == -1 ? NULL : s->trunks;
1055 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1056 free(bundle->trunks);
1057 bundle->trunks = vlan_bitmap_clone(trunks);
1062 if (!list_is_short(&bundle->ports)) {
1063 bundle->ofproto->has_bonded_bundles = true;
1065 if (bond_reconfigure(bundle->bond, s->bond)) {
1066 ofproto->need_revalidate = true;
1069 bundle->bond = bond_create(s->bond);
1070 ofproto->need_revalidate = true;
1073 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1074 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1078 bond_destroy(bundle->bond);
1079 bundle->bond = NULL;
1082 /* If we changed something that would affect MAC learning, un-learn
1083 * everything on this port and force flow revalidation. */
1085 bundle_flush_macs(bundle);
1092 bundle_remove(struct ofport *port_)
1094 struct ofport_dpif *port = ofport_dpif_cast(port_);
1095 struct ofbundle *bundle = port->bundle;
1098 bundle_del_port(port);
1099 if (list_is_empty(&bundle->ports)) {
1100 bundle_destroy(bundle);
1101 } else if (list_is_short(&bundle->ports)) {
1102 bond_destroy(bundle->bond);
1103 bundle->bond = NULL;
1109 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1111 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1112 struct ofport_dpif *port = port_;
1113 uint8_t ea[ETH_ADDR_LEN];
1116 error = netdev_get_etheraddr(port->up.netdev, ea);
1118 struct lacp_pdu *packet_pdu;
1119 struct ofpbuf packet;
1121 ofpbuf_init(&packet, 0);
1122 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1123 sizeof *packet_pdu);
1125 error = netdev_send(port->up.netdev, &packet);
1127 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1128 "(%s)", port->bundle->name,
1129 netdev_get_name(port->up.netdev), strerror(error));
1131 ofpbuf_uninit(&packet);
1133 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1134 "%s (%s)", port->bundle->name,
1135 netdev_get_name(port->up.netdev), strerror(error));
1140 bundle_send_learning_packets(struct ofbundle *bundle)
1142 struct ofproto_dpif *ofproto = bundle->ofproto;
1143 int error, n_packets, n_errors;
1144 struct mac_entry *e;
1146 error = n_packets = n_errors = 0;
1147 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1148 if (e->port.p != bundle) {
1149 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1159 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1160 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1161 "packets, last error was: %s",
1162 bundle->name, n_errors, n_packets, strerror(error));
1164 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1165 bundle->name, n_packets);
1170 bundle_run(struct ofbundle *bundle)
1173 lacp_run(bundle->lacp, send_pdu_cb);
1176 struct ofport_dpif *port;
1178 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1179 bool may_enable = lacp_slave_may_enable(bundle->lacp, port);
1181 if (may_enable && port->cfm) {
1182 may_enable = !cfm_get_fault(port->cfm);
1184 bond_slave_set_may_enable(bundle->bond, port, may_enable);
1187 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1188 lacp_negotiated(bundle->lacp));
1189 if (bond_should_send_learning_packets(bundle->bond)) {
1190 bundle_send_learning_packets(bundle);
1196 bundle_wait(struct ofbundle *bundle)
1199 lacp_wait(bundle->lacp);
1202 bond_wait(bundle->bond);
1209 mirror_scan(struct ofproto_dpif *ofproto)
1213 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1214 if (!ofproto->mirrors[idx]) {
1221 static struct ofmirror *
1222 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1226 for (i = 0; i < MAX_MIRRORS; i++) {
1227 struct ofmirror *mirror = ofproto->mirrors[i];
1228 if (mirror && mirror->aux == aux) {
1237 mirror_set(struct ofproto *ofproto_, void *aux,
1238 const struct ofproto_mirror_settings *s)
1240 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1241 mirror_mask_t mirror_bit;
1242 struct ofbundle *bundle;
1243 struct ofmirror *mirror;
1244 struct ofbundle *out;
1245 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1246 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1249 mirror = mirror_lookup(ofproto, aux);
1251 mirror_destroy(mirror);
1257 idx = mirror_scan(ofproto);
1259 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1261 ofproto->up.name, MAX_MIRRORS, s->name);
1265 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1266 mirror->ofproto = ofproto;
1268 mirror->out_vlan = -1;
1269 mirror->name = NULL;
1272 if (!mirror->name || strcmp(s->name, mirror->name)) {
1274 mirror->name = xstrdup(s->name);
1277 /* Get the new configuration. */
1278 if (s->out_bundle) {
1279 out = bundle_lookup(ofproto, s->out_bundle);
1281 mirror_destroy(mirror);
1287 out_vlan = s->out_vlan;
1289 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1290 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1292 /* If the configuration has not changed, do nothing. */
1293 if (hmapx_equals(&srcs, &mirror->srcs)
1294 && hmapx_equals(&dsts, &mirror->dsts)
1295 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1296 && mirror->out == out
1297 && mirror->out_vlan == out_vlan)
1299 hmapx_destroy(&srcs);
1300 hmapx_destroy(&dsts);
1304 hmapx_swap(&srcs, &mirror->srcs);
1305 hmapx_destroy(&srcs);
1307 hmapx_swap(&dsts, &mirror->dsts);
1308 hmapx_destroy(&dsts);
1310 free(mirror->vlans);
1311 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1314 mirror->out_vlan = out_vlan;
1316 /* Update bundles. */
1317 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1318 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1319 if (hmapx_contains(&mirror->srcs, bundle)) {
1320 bundle->src_mirrors |= mirror_bit;
1322 bundle->src_mirrors &= ~mirror_bit;
1325 if (hmapx_contains(&mirror->dsts, bundle)) {
1326 bundle->dst_mirrors |= mirror_bit;
1328 bundle->dst_mirrors &= ~mirror_bit;
1331 if (mirror->out == bundle) {
1332 bundle->mirror_out |= mirror_bit;
1334 bundle->mirror_out &= ~mirror_bit;
1338 ofproto->need_revalidate = true;
1339 mac_learning_flush(ofproto->ml);
1345 mirror_destroy(struct ofmirror *mirror)
1347 struct ofproto_dpif *ofproto;
1348 mirror_mask_t mirror_bit;
1349 struct ofbundle *bundle;
1355 ofproto = mirror->ofproto;
1356 ofproto->need_revalidate = true;
1357 mac_learning_flush(ofproto->ml);
1359 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1360 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1361 bundle->src_mirrors &= ~mirror_bit;
1362 bundle->dst_mirrors &= ~mirror_bit;
1363 bundle->mirror_out &= ~mirror_bit;
1366 hmapx_destroy(&mirror->srcs);
1367 hmapx_destroy(&mirror->dsts);
1368 free(mirror->vlans);
1370 ofproto->mirrors[mirror->idx] = NULL;
1376 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1378 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1379 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1380 ofproto->need_revalidate = true;
1381 mac_learning_flush(ofproto->ml);
1387 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1389 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1390 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1391 return bundle && bundle->mirror_out != 0;
1396 static struct ofport_dpif *
1397 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1399 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1400 return ofport ? ofport_dpif_cast(ofport) : NULL;
1403 static struct ofport_dpif *
1404 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1406 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1410 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1411 struct dpif_port *dpif_port)
1413 ofproto_port->name = dpif_port->name;
1414 ofproto_port->type = dpif_port->type;
1415 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1419 port_run(struct ofport_dpif *ofport)
1422 cfm_run(ofport->cfm);
1424 if (cfm_should_send_ccm(ofport->cfm)) {
1425 struct ofpbuf packet;
1427 ofpbuf_init(&packet, 0);
1428 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1429 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1430 ofport->odp_port, &packet);
1431 ofpbuf_uninit(&packet);
1437 port_wait(struct ofport_dpif *ofport)
1440 cfm_wait(ofport->cfm);
1445 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1446 struct ofproto_port *ofproto_port)
1448 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1449 struct dpif_port dpif_port;
1452 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1454 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1460 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1462 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1466 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1468 *ofp_portp = odp_port_to_ofp_port(odp_port);
1474 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1476 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1479 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1481 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1483 /* The caller is going to close ofport->up.netdev. If this is a
1484 * bonded port, then the bond is using that netdev, so remove it
1485 * from the bond. The client will need to reconfigure everything
1486 * after deleting ports, so then the slave will get re-added. */
1487 bundle_remove(&ofport->up);
1493 struct port_dump_state {
1494 struct dpif_port_dump dump;
1499 port_dump_start(const struct ofproto *ofproto_, void **statep)
1501 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1502 struct port_dump_state *state;
1504 *statep = state = xmalloc(sizeof *state);
1505 dpif_port_dump_start(&state->dump, ofproto->dpif);
1506 state->done = false;
1511 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1512 struct ofproto_port *port)
1514 struct port_dump_state *state = state_;
1515 struct dpif_port dpif_port;
1517 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1518 ofproto_port_from_dpif_port(port, &dpif_port);
1521 int error = dpif_port_dump_done(&state->dump);
1523 return error ? error : EOF;
1528 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1530 struct port_dump_state *state = state_;
1533 dpif_port_dump_done(&state->dump);
1540 port_poll(const struct ofproto *ofproto_, char **devnamep)
1542 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1543 return dpif_port_poll(ofproto->dpif, devnamep);
1547 port_poll_wait(const struct ofproto *ofproto_)
1549 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1550 dpif_port_poll_wait(ofproto->dpif);
1554 port_is_lacp_current(const struct ofport *ofport_)
1556 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1557 return (ofport->bundle && ofport->bundle->lacp
1558 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1562 /* Upcall handling. */
1564 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1565 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1566 * their individual configurations.
1568 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1569 * Otherwise, ownership is transferred to this function. */
1571 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1572 const struct flow *flow, bool clone)
1574 struct ofputil_packet_in pin;
1576 pin.packet = upcall->packet;
1577 pin.in_port = flow->in_port;
1578 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1579 pin.buffer_id = 0; /* not yet known */
1580 pin.send_len = upcall->userdata;
1581 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1582 clone ? NULL : upcall->packet);
1586 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1587 const struct ofpbuf *packet)
1589 if (cfm_should_process_flow(flow)) {
1590 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1591 if (packet && ofport && ofport->cfm) {
1592 cfm_process_heartbeat(ofport->cfm, packet);
1595 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
1596 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1597 if (packet && port && port->bundle && port->bundle->lacp) {
1598 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
1600 lacp_process_pdu(port->bundle->lacp, port, pdu);
1609 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1611 struct facet *facet;
1614 /* Obtain in_port and tun_id, at least. */
1615 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1617 /* Set header pointers in 'flow'. */
1618 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1620 /* Handle 802.1ag and LACP. */
1621 if (process_special(ofproto, &flow, upcall->packet)) {
1622 ofpbuf_delete(upcall->packet);
1623 ofproto->n_matches++;
1627 /* Check with in-band control to see if this packet should be sent
1628 * to the local port regardless of the flow table. */
1629 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1630 send_packet(ofproto, ODPP_LOCAL, upcall->packet);
1633 facet = facet_lookup_valid(ofproto, &flow);
1635 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow);
1637 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1638 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1640 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1641 COVERAGE_INC(ofproto_dpif_no_packet_in);
1642 /* XXX install 'drop' flow entry */
1643 ofpbuf_delete(upcall->packet);
1647 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1651 send_packet_in(ofproto, upcall, &flow, false);
1655 facet = facet_create(rule, &flow, upcall->packet);
1656 } else if (!facet->may_install) {
1657 /* The facet is not installable, that is, we need to process every
1658 * packet, so process the current packet's actions into 'facet'. */
1659 facet_make_actions(ofproto, facet, upcall->packet);
1662 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1664 * Extra-special case for fail-open mode.
1666 * We are in fail-open mode and the packet matched the fail-open rule,
1667 * but we are connected to a controller too. We should send the packet
1668 * up to the controller in the hope that it will try to set up a flow
1669 * and thereby allow us to exit fail-open.
1671 * See the top-level comment in fail-open.c for more information.
1673 send_packet_in(ofproto, upcall, &flow, true);
1676 facet_execute(ofproto, facet, upcall->packet);
1677 facet_install(ofproto, facet, false);
1678 ofproto->n_matches++;
1682 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1686 switch (upcall->type) {
1687 case DPIF_UC_ACTION:
1688 COVERAGE_INC(ofproto_dpif_ctlr_action);
1689 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1690 send_packet_in(ofproto, upcall, &flow, false);
1693 case DPIF_UC_SAMPLE:
1694 if (ofproto->sflow) {
1695 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1696 dpif_sflow_received(ofproto->sflow, upcall, &flow);
1698 ofpbuf_delete(upcall->packet);
1702 handle_miss_upcall(ofproto, upcall);
1705 case DPIF_N_UC_TYPES:
1707 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1712 /* Flow expiration. */
1714 static int facet_max_idle(const struct ofproto_dpif *);
1715 static void update_stats(struct ofproto_dpif *);
1716 static void rule_expire(struct rule_dpif *);
1717 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1719 /* This function is called periodically by run(). Its job is to collect
1720 * updates for the flows that have been installed into the datapath, most
1721 * importantly when they last were used, and then use that information to
1722 * expire flows that have not been used recently.
1724 * Returns the number of milliseconds after which it should be called again. */
1726 expire(struct ofproto_dpif *ofproto)
1728 struct rule_dpif *rule, *next_rule;
1729 struct cls_cursor cursor;
1732 /* Update stats for each flow in the datapath. */
1733 update_stats(ofproto);
1735 /* Expire facets that have been idle too long. */
1736 dp_max_idle = facet_max_idle(ofproto);
1737 expire_facets(ofproto, dp_max_idle);
1739 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1740 cls_cursor_init(&cursor, &ofproto->up.tables[0], NULL);
1741 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1745 /* All outstanding data in existing flows has been accounted, so it's a
1746 * good time to do bond rebalancing. */
1747 if (ofproto->has_bonded_bundles) {
1748 struct ofbundle *bundle;
1750 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1752 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1757 return MIN(dp_max_idle, 1000);
1760 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1762 * This function also pushes statistics updates to rules which each facet
1763 * resubmits into. Generally these statistics will be accurate. However, if a
1764 * facet changes the rule it resubmits into at some time in between
1765 * update_stats() runs, it is possible that statistics accrued to the
1766 * old rule will be incorrectly attributed to the new rule. This could be
1767 * avoided by calling update_stats() whenever rules are created or
1768 * deleted. However, the performance impact of making so many calls to the
1769 * datapath do not justify the benefit of having perfectly accurate statistics.
1772 update_stats(struct ofproto_dpif *p)
1774 const struct dpif_flow_stats *stats;
1775 struct dpif_flow_dump dump;
1776 const struct nlattr *key;
1779 dpif_flow_dump_start(&dump, p->dpif);
1780 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1781 struct facet *facet;
1784 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1788 odp_flow_key_format(key, key_len, &s);
1789 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
1795 facet = facet_find(p, &flow);
1797 if (facet && facet->installed) {
1799 if (stats->n_packets >= facet->dp_packet_count) {
1800 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1801 facet->packet_count += extra;
1803 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1806 if (stats->n_bytes >= facet->dp_byte_count) {
1807 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1809 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1812 facet->dp_packet_count = stats->n_packets;
1813 facet->dp_byte_count = stats->n_bytes;
1815 facet_update_time(p, facet, stats->used);
1816 facet_account(p, facet, stats->n_bytes);
1817 facet_push_stats(facet);
1819 /* There's a flow in the datapath that we know nothing about.
1821 COVERAGE_INC(facet_unexpected);
1822 dpif_flow_del(p->dpif, key, key_len, NULL);
1825 dpif_flow_dump_done(&dump);
1828 /* Calculates and returns the number of milliseconds of idle time after which
1829 * facets should expire from the datapath and we should fold their statistics
1830 * into their parent rules in userspace. */
1832 facet_max_idle(const struct ofproto_dpif *ofproto)
1835 * Idle time histogram.
1837 * Most of the time a switch has a relatively small number of facets. When
1838 * this is the case we might as well keep statistics for all of them in
1839 * userspace and to cache them in the kernel datapath for performance as
1842 * As the number of facets increases, the memory required to maintain
1843 * statistics about them in userspace and in the kernel becomes
1844 * significant. However, with a large number of facets it is likely that
1845 * only a few of them are "heavy hitters" that consume a large amount of
1846 * bandwidth. At this point, only heavy hitters are worth caching in the
1847 * kernel and maintaining in userspaces; other facets we can discard.
1849 * The technique used to compute the idle time is to build a histogram with
1850 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1851 * that is installed in the kernel gets dropped in the appropriate bucket.
1852 * After the histogram has been built, we compute the cutoff so that only
1853 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
1854 * cached. At least the most-recently-used bucket of facets is kept, so
1855 * actually an arbitrary number of facets can be kept in any given
1856 * expiration run (though the next run will delete most of those unless
1857 * they receive additional data).
1859 * This requires a second pass through the facets, in addition to the pass
1860 * made by update_stats(), because the former function never looks
1861 * at uninstallable facets.
1863 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1864 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1865 int buckets[N_BUCKETS] = { 0 };
1866 int total, subtotal, bucket;
1867 struct facet *facet;
1871 total = hmap_count(&ofproto->facets);
1872 if (total <= 1000) {
1873 return N_BUCKETS * BUCKET_WIDTH;
1876 /* Build histogram. */
1878 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1879 long long int idle = now - facet->used;
1880 int bucket = (idle <= 0 ? 0
1881 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1882 : (unsigned int) idle / BUCKET_WIDTH);
1886 /* Find the first bucket whose flows should be expired. */
1887 subtotal = bucket = 0;
1889 subtotal += buckets[bucket++];
1890 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
1892 if (VLOG_IS_DBG_ENABLED()) {
1896 ds_put_cstr(&s, "keep");
1897 for (i = 0; i < N_BUCKETS; i++) {
1899 ds_put_cstr(&s, ", drop");
1902 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1905 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1909 return bucket * BUCKET_WIDTH;
1913 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1915 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1916 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1917 struct ofexpired expired;
1919 if (facet->installed) {
1920 struct dpif_flow_stats stats;
1922 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1924 facet_update_stats(ofproto, facet, &stats);
1927 expired.flow = facet->flow;
1928 expired.packet_count = facet->packet_count;
1929 expired.byte_count = facet->byte_count;
1930 expired.used = facet->used;
1931 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1936 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1938 long long int cutoff = time_msec() - dp_max_idle;
1939 struct facet *facet, *next_facet;
1941 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1942 facet_active_timeout(ofproto, facet);
1943 if (facet->used < cutoff) {
1944 facet_remove(ofproto, facet);
1949 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
1950 * then delete it entirely. */
1952 rule_expire(struct rule_dpif *rule)
1954 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1955 struct facet *facet, *next_facet;
1959 /* Has 'rule' expired? */
1961 if (rule->up.hard_timeout
1962 && now > rule->up.created + rule->up.hard_timeout * 1000) {
1963 reason = OFPRR_HARD_TIMEOUT;
1964 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
1965 && now > rule->used + rule->up.idle_timeout * 1000) {
1966 reason = OFPRR_IDLE_TIMEOUT;
1971 COVERAGE_INC(ofproto_dpif_expired);
1973 /* Update stats. (This is a no-op if the rule expired due to an idle
1974 * timeout, because that only happens when the rule has no facets left.) */
1975 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1976 facet_remove(ofproto, facet);
1979 /* Get rid of the rule. */
1980 ofproto_rule_expire(&rule->up, reason);
1985 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
1986 * example 'packet' within that flow.
1988 * The caller must already have determined that no facet with an identical
1989 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1990 * the ofproto's classifier table. */
1991 static struct facet *
1992 facet_create(struct rule_dpif *rule, const struct flow *flow,
1993 const struct ofpbuf *packet)
1995 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1996 struct facet *facet;
1998 facet = xzalloc(sizeof *facet);
1999 facet->used = time_msec();
2000 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2001 list_push_back(&rule->facets, &facet->list_node);
2003 facet->flow = *flow;
2004 netflow_flow_init(&facet->nf_flow);
2005 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2007 facet_make_actions(ofproto, facet, packet);
2013 facet_free(struct facet *facet)
2015 free(facet->actions);
2019 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2020 * 'packet', which arrived on 'in_port'.
2022 * Takes ownership of 'packet'. */
2024 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2025 const struct nlattr *odp_actions, size_t actions_len,
2026 struct ofpbuf *packet)
2028 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2029 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
2030 /* As an optimization, avoid a round-trip from userspace to kernel to
2031 * userspace. This also avoids possibly filling up kernel packet
2032 * buffers along the way. */
2033 struct dpif_upcall upcall;
2035 upcall.type = DPIF_UC_ACTION;
2036 upcall.packet = packet;
2039 upcall.userdata = nl_attr_get_u64(odp_actions);
2040 upcall.sample_pool = 0;
2041 upcall.actions = NULL;
2042 upcall.actions_len = 0;
2044 send_packet_in(ofproto, &upcall, flow, false);
2048 struct odputil_keybuf keybuf;
2052 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2053 odp_flow_key_from_flow(&key, flow);
2055 error = dpif_execute(ofproto->dpif, key.data, key.size,
2056 odp_actions, actions_len, packet);
2058 ofpbuf_delete(packet);
2063 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2064 * statistics appropriately. 'packet' must have at least sizeof(struct
2065 * ofp_packet_in) bytes of headroom.
2067 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2068 * applying flow_extract() to 'packet' would yield the same flow as
2071 * 'facet' must have accurately composed ODP actions; that is, it must not be
2072 * in need of revalidation.
2074 * Takes ownership of 'packet'. */
2076 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2077 struct ofpbuf *packet)
2079 struct dpif_flow_stats stats;
2081 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2083 flow_extract_stats(&facet->flow, packet, &stats);
2084 stats.used = time_msec();
2085 if (execute_odp_actions(ofproto, &facet->flow,
2086 facet->actions, facet->actions_len, packet)) {
2087 facet_update_stats(ofproto, facet, &stats);
2091 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2093 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2094 * rule's statistics, via facet_uninstall().
2096 * - Removes 'facet' from its rule and from ofproto->facets.
2099 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2101 facet_uninstall(ofproto, facet);
2102 facet_flush_stats(ofproto, facet);
2103 hmap_remove(&ofproto->facets, &facet->hmap_node);
2104 list_remove(&facet->list_node);
2108 /* Composes the ODP actions for 'facet' based on its rule's actions. */
2110 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2111 const struct ofpbuf *packet)
2113 const struct rule_dpif *rule = facet->rule;
2114 struct ofpbuf *odp_actions;
2115 struct action_xlate_ctx ctx;
2117 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2118 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2119 facet->tags = ctx.tags;
2120 facet->may_install = ctx.may_set_up_flow;
2121 facet->nf_flow.output_iface = ctx.nf_output_iface;
2123 if (facet->actions_len != odp_actions->size
2124 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2125 free(facet->actions);
2126 facet->actions_len = odp_actions->size;
2127 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2130 ofpbuf_delete(odp_actions);
2133 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2134 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2135 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2136 * since 'facet' was last updated.
2138 * Returns 0 if successful, otherwise a positive errno value.*/
2140 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2141 const struct nlattr *actions, size_t actions_len,
2142 struct dpif_flow_stats *stats)
2144 struct odputil_keybuf keybuf;
2145 enum dpif_flow_put_flags flags;
2149 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2151 flags |= DPIF_FP_ZERO_STATS;
2154 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2155 odp_flow_key_from_flow(&key, &facet->flow);
2157 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2158 actions, actions_len, stats);
2161 facet_reset_dp_stats(facet, stats);
2167 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2168 * 'zero_stats' is true, clears any existing statistics from the datapath for
2171 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2173 struct dpif_flow_stats stats;
2175 if (facet->may_install
2176 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2177 zero_stats ? &stats : NULL)) {
2178 facet->installed = true;
2183 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2185 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2189 facet_account(struct ofproto_dpif *ofproto,
2190 struct facet *facet, uint64_t extra_bytes)
2192 uint64_t total_bytes, n_bytes;
2193 struct ofbundle *in_bundle;
2194 const struct nlattr *a;
2200 total_bytes = facet->byte_count + extra_bytes;
2201 if (total_bytes <= facet->accounted_bytes) {
2204 n_bytes = total_bytes - facet->accounted_bytes;
2205 facet->accounted_bytes = total_bytes;
2207 /* Test that 'tags' is nonzero to ensure that only flows that include an
2208 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2209 * This works because OFPP_NORMAL always sets a nonzero tag value.
2211 * Feed information from the active flows back into the learning table to
2212 * ensure that table is always in sync with what is actually flowing
2213 * through the datapath. */
2215 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2216 &vlan, &in_bundle)) {
2220 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2222 if (!ofproto->has_bonded_bundles) {
2226 /* This loop feeds byte counters to bond_account() for rebalancing to use
2227 * as a basis. We also need to track the actual VLAN on which the packet
2228 * is going to be sent to ensure that it matches the one passed to
2229 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2231 vlan_tci = facet->flow.vlan_tci;
2232 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2233 struct ofport_dpif *port;
2235 switch (nl_attr_type(a)) {
2236 case ODP_ACTION_ATTR_OUTPUT:
2237 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2238 if (port && port->bundle && port->bundle->bond) {
2239 bond_account(port->bundle->bond, &facet->flow,
2240 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2244 case ODP_ACTION_ATTR_STRIP_VLAN:
2245 vlan_tci = htons(0);
2248 case ODP_ACTION_ATTR_SET_DL_TCI:
2249 vlan_tci = nl_attr_get_be16(a);
2255 /* If 'rule' is installed in the datapath, uninstalls it. */
2257 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2259 if (facet->installed) {
2260 struct odputil_keybuf keybuf;
2261 struct dpif_flow_stats stats;
2265 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2266 odp_flow_key_from_flow(&key, &facet->flow);
2268 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2269 facet_reset_dp_stats(facet, &stats);
2271 facet_update_stats(p, facet, &stats);
2273 facet->installed = false;
2275 assert(facet->dp_packet_count == 0);
2276 assert(facet->dp_byte_count == 0);
2280 /* Returns true if the only action for 'facet' is to send to the controller.
2281 * (We don't report NetFlow expiration messages for such facets because they
2282 * are just part of the control logic for the network, not real traffic). */
2284 facet_is_controller_flow(struct facet *facet)
2287 && facet->rule->up.n_actions == 1
2288 && action_outputs_to_port(&facet->rule->up.actions[0],
2289 htons(OFPP_CONTROLLER)));
2292 /* Resets 'facet''s datapath statistics counters. This should be called when
2293 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2294 * it should contain the statistics returned by dpif when 'facet' was reset in
2295 * the datapath. 'stats' will be modified to only included statistics new
2296 * since 'facet' was last updated. */
2298 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2300 if (stats && facet->dp_packet_count <= stats->n_packets
2301 && facet->dp_byte_count <= stats->n_bytes) {
2302 stats->n_packets -= facet->dp_packet_count;
2303 stats->n_bytes -= facet->dp_byte_count;
2306 facet->dp_packet_count = 0;
2307 facet->dp_byte_count = 0;
2310 /* Folds all of 'facet''s statistics into its rule. Also updates the
2311 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2312 * 'facet''s statistics in the datapath should have been zeroed and folded into
2313 * its packet and byte counts before this function is called. */
2315 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2317 assert(!facet->dp_byte_count);
2318 assert(!facet->dp_packet_count);
2320 facet_push_stats(facet);
2321 facet_account(ofproto, facet, 0);
2323 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2324 struct ofexpired expired;
2325 expired.flow = facet->flow;
2326 expired.packet_count = facet->packet_count;
2327 expired.byte_count = facet->byte_count;
2328 expired.used = facet->used;
2329 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2332 facet->rule->packet_count += facet->packet_count;
2333 facet->rule->byte_count += facet->byte_count;
2335 /* Reset counters to prevent double counting if 'facet' ever gets
2337 facet->packet_count = 0;
2338 facet->byte_count = 0;
2339 facet->rs_packet_count = 0;
2340 facet->rs_byte_count = 0;
2341 facet->accounted_bytes = 0;
2343 netflow_flow_clear(&facet->nf_flow);
2346 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2347 * Returns it if found, otherwise a null pointer.
2349 * The returned facet might need revalidation; use facet_lookup_valid()
2350 * instead if that is important. */
2351 static struct facet *
2352 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2354 struct facet *facet;
2356 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2358 if (flow_equal(flow, &facet->flow)) {
2366 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2367 * Returns it if found, otherwise a null pointer.
2369 * The returned facet is guaranteed to be valid. */
2370 static struct facet *
2371 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2373 struct facet *facet = facet_find(ofproto, flow);
2375 /* The facet we found might not be valid, since we could be in need of
2376 * revalidation. If it is not valid, don't return it. */
2378 && ofproto->need_revalidate
2379 && !facet_revalidate(ofproto, facet)) {
2380 COVERAGE_INC(facet_invalidated);
2387 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2389 * - If the rule found is different from 'facet''s current rule, moves
2390 * 'facet' to the new rule and recompiles its actions.
2392 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2393 * where it is and recompiles its actions anyway.
2395 * - If there is none, destroys 'facet'.
2397 * Returns true if 'facet' still exists, false if it has been destroyed. */
2399 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2401 struct action_xlate_ctx ctx;
2402 struct ofpbuf *odp_actions;
2403 struct rule_dpif *new_rule;
2404 bool actions_changed;
2406 COVERAGE_INC(facet_revalidate);
2408 /* Determine the new rule. */
2409 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
2411 /* No new rule, so delete the facet. */
2412 facet_remove(ofproto, facet);
2416 /* Calculate new ODP actions.
2418 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2419 * emit a NetFlow expiration and, if so, we need to have the old state
2420 * around to properly compose it. */
2421 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2422 odp_actions = xlate_actions(&ctx,
2423 new_rule->up.actions, new_rule->up.n_actions);
2424 actions_changed = (facet->actions_len != odp_actions->size
2425 || memcmp(facet->actions, odp_actions->data,
2426 facet->actions_len));
2428 /* If the ODP actions changed or the installability changed, then we need
2429 * to talk to the datapath. */
2430 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2431 if (ctx.may_set_up_flow) {
2432 struct dpif_flow_stats stats;
2434 facet_put__(ofproto, facet,
2435 odp_actions->data, odp_actions->size, &stats);
2436 facet_update_stats(ofproto, facet, &stats);
2438 facet_uninstall(ofproto, facet);
2441 /* The datapath flow is gone or has zeroed stats, so push stats out of
2442 * 'facet' into 'rule'. */
2443 facet_flush_stats(ofproto, facet);
2446 /* Update 'facet' now that we've taken care of all the old state. */
2447 facet->tags = ctx.tags;
2448 facet->nf_flow.output_iface = ctx.nf_output_iface;
2449 facet->may_install = ctx.may_set_up_flow;
2450 if (actions_changed) {
2451 free(facet->actions);
2452 facet->actions_len = odp_actions->size;
2453 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2455 if (facet->rule != new_rule) {
2456 COVERAGE_INC(facet_changed_rule);
2457 list_remove(&facet->list_node);
2458 list_push_back(&new_rule->facets, &facet->list_node);
2459 facet->rule = new_rule;
2460 facet->used = new_rule->up.created;
2461 facet->rs_used = facet->used;
2464 ofpbuf_delete(odp_actions);
2469 /* Updates 'facet''s used time. Caller is responsible for calling
2470 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2472 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2475 if (used > facet->used) {
2477 if (used > facet->rule->used) {
2478 facet->rule->used = used;
2480 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2484 /* Folds the statistics from 'stats' into the counters in 'facet'.
2486 * Because of the meaning of a facet's counters, it only makes sense to do this
2487 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2488 * packet that was sent by hand or if it represents statistics that have been
2489 * cleared out of the datapath. */
2491 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2492 const struct dpif_flow_stats *stats)
2494 if (stats->n_packets || stats->used > facet->used) {
2495 facet_update_time(ofproto, facet, stats->used);
2496 facet->packet_count += stats->n_packets;
2497 facet->byte_count += stats->n_bytes;
2498 facet_push_stats(facet);
2499 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2504 facet_push_stats(struct facet *facet)
2506 uint64_t rs_packets, rs_bytes;
2508 assert(facet->packet_count >= facet->rs_packet_count);
2509 assert(facet->byte_count >= facet->rs_byte_count);
2510 assert(facet->used >= facet->rs_used);
2512 rs_packets = facet->packet_count - facet->rs_packet_count;
2513 rs_bytes = facet->byte_count - facet->rs_byte_count;
2515 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2516 facet->rs_packet_count = facet->packet_count;
2517 facet->rs_byte_count = facet->byte_count;
2518 facet->rs_used = facet->used;
2520 flow_push_stats(facet->rule, &facet->flow,
2521 rs_packets, rs_bytes, facet->used);
2525 struct ofproto_push {
2526 struct action_xlate_ctx ctx;
2533 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2535 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2538 rule->packet_count += push->packets;
2539 rule->byte_count += push->bytes;
2540 rule->used = MAX(push->used, rule->used);
2544 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2545 * 'rule''s actions. */
2547 flow_push_stats(const struct rule_dpif *rule,
2548 struct flow *flow, uint64_t packets, uint64_t bytes,
2551 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2552 struct ofproto_push push;
2554 push.packets = packets;
2558 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2559 push.ctx.resubmit_hook = push_resubmit;
2560 ofpbuf_delete(xlate_actions(&push.ctx,
2561 rule->up.actions, rule->up.n_actions));
2566 static struct rule_dpif *
2567 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
2569 return rule_dpif_cast(rule_from_cls_rule(
2570 classifier_lookup(&ofproto->up.tables[0],
2575 complete_operation(struct rule_dpif *rule)
2577 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2579 ofproto->need_revalidate = true;
2581 struct dpif_completion *c = xmalloc(sizeof *c);
2582 c->op = rule->up.pending;
2583 list_push_back(&ofproto->completions, &c->list_node);
2585 ofoperation_complete(rule->up.pending, 0);
2589 static struct rule *
2592 struct rule_dpif *rule = xmalloc(sizeof *rule);
2597 rule_dealloc(struct rule *rule_)
2599 struct rule_dpif *rule = rule_dpif_cast(rule_);
2604 rule_construct(struct rule *rule_)
2606 struct rule_dpif *rule = rule_dpif_cast(rule_);
2607 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2608 struct rule_dpif *victim;
2611 error = validate_actions(rule->up.actions, rule->up.n_actions,
2612 &rule->up.cr.flow, ofproto->max_ports);
2617 rule->used = rule->up.created;
2618 rule->packet_count = 0;
2619 rule->byte_count = 0;
2621 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2622 if (victim && !list_is_empty(&victim->facets)) {
2623 struct facet *facet;
2625 rule->facets = victim->facets;
2626 list_moved(&rule->facets);
2627 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2631 /* Must avoid list_moved() in this case. */
2632 list_init(&rule->facets);
2635 complete_operation(rule);
2640 rule_destruct(struct rule *rule_)
2642 struct rule_dpif *rule = rule_dpif_cast(rule_);
2643 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2644 struct facet *facet, *next_facet;
2646 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2647 facet_revalidate(ofproto, facet);
2650 complete_operation(rule);
2654 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2656 struct rule_dpif *rule = rule_dpif_cast(rule_);
2657 struct facet *facet;
2659 /* Start from historical data for 'rule' itself that are no longer tracked
2660 * in facets. This counts, for example, facets that have expired. */
2661 *packets = rule->packet_count;
2662 *bytes = rule->byte_count;
2664 /* Add any statistics that are tracked by facets. This includes
2665 * statistical data recently updated by ofproto_update_stats() as well as
2666 * stats for packets that were executed "by hand" via dpif_execute(). */
2667 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2668 *packets += facet->packet_count;
2669 *bytes += facet->byte_count;
2674 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2676 struct rule_dpif *rule = rule_dpif_cast(rule_);
2677 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2678 struct action_xlate_ctx ctx;
2679 struct ofpbuf *odp_actions;
2680 struct facet *facet;
2683 /* First look for a related facet. If we find one, account it to that. */
2684 facet = facet_lookup_valid(ofproto, flow);
2685 if (facet && facet->rule == rule) {
2686 facet_execute(ofproto, facet, packet);
2690 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2691 * create a new facet for it and use that. */
2692 if (rule_dpif_lookup(ofproto, flow) == rule) {
2693 facet = facet_create(rule, flow, packet);
2694 facet_execute(ofproto, facet, packet);
2695 facet_install(ofproto, facet, true);
2699 /* We can't account anything to a facet. If we were to try, then that
2700 * facet would have a non-matching rule, busting our invariants. */
2701 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2702 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2703 size = packet->size;
2704 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2705 odp_actions->size, packet)) {
2706 rule->used = time_msec();
2707 rule->packet_count++;
2708 rule->byte_count += size;
2709 flow_push_stats(rule, flow, 1, size, rule->used);
2711 ofpbuf_delete(odp_actions);
2717 rule_modify_actions(struct rule *rule_)
2719 struct rule_dpif *rule = rule_dpif_cast(rule_);
2720 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2723 error = validate_actions(rule->up.actions, rule->up.n_actions,
2724 &rule->up.cr.flow, ofproto->max_ports);
2726 ofoperation_complete(rule->up.pending, error);
2730 complete_operation(rule);
2733 /* Sends 'packet' out of port 'odp_port' within 'p'.
2734 * Returns 0 if successful, otherwise a positive errno value. */
2736 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2737 const struct ofpbuf *packet)
2739 struct ofpbuf key, odp_actions;
2740 struct odputil_keybuf keybuf;
2744 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2745 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2746 odp_flow_key_from_flow(&key, &flow);
2748 ofpbuf_init(&odp_actions, 32);
2749 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2750 error = dpif_execute(ofproto->dpif,
2752 odp_actions.data, odp_actions.size,
2754 ofpbuf_uninit(&odp_actions);
2757 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2758 ofproto->up.name, odp_port, strerror(error));
2763 /* OpenFlow to ODP action translation. */
2765 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2766 struct action_xlate_ctx *ctx);
2767 static bool xlate_normal(struct action_xlate_ctx *);
2770 commit_odp_actions(struct action_xlate_ctx *ctx)
2772 const struct flow *flow = &ctx->flow;
2773 struct flow *base = &ctx->base_flow;
2774 struct ofpbuf *odp_actions = ctx->odp_actions;
2776 if (base->tun_id != flow->tun_id) {
2777 nl_msg_put_be64(odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
2778 base->tun_id = flow->tun_id;
2781 if (base->nw_src != flow->nw_src) {
2782 nl_msg_put_be32(odp_actions, ODP_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
2783 base->nw_src = flow->nw_src;
2786 if (base->nw_dst != flow->nw_dst) {
2787 nl_msg_put_be32(odp_actions, ODP_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
2788 base->nw_dst = flow->nw_dst;
2791 if (base->vlan_tci != flow->vlan_tci) {
2792 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2793 nl_msg_put_flag(odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2795 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2796 flow->vlan_tci & ~htons(VLAN_CFI));
2798 base->vlan_tci = flow->vlan_tci;
2801 if (base->tp_src != flow->tp_src) {
2802 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
2803 base->tp_src = flow->tp_src;
2806 if (base->tp_dst != flow->tp_dst) {
2807 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
2808 base->tp_dst = flow->tp_dst;
2811 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
2812 nl_msg_put_unspec(odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2813 flow->dl_src, ETH_ADDR_LEN);
2814 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2817 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2818 nl_msg_put_unspec(odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2819 flow->dl_dst, ETH_ADDR_LEN);
2820 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2823 if (ctx->base_priority != ctx->priority) {
2824 if (ctx->priority) {
2825 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_SET_PRIORITY,
2828 nl_msg_put_flag(odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2830 ctx->base_priority = ctx->priority;
2835 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2837 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2838 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2841 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2842 /* Forwarding disabled on port. */
2847 * We don't have an ofport record for this port, but it doesn't hurt to
2848 * allow forwarding to it anyhow. Maybe such a port will appear later
2849 * and we're pre-populating the flow table.
2853 commit_odp_actions(ctx);
2854 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2855 ctx->nf_output_iface = ofp_port;
2859 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2861 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2862 struct rule_dpif *rule;
2863 uint16_t old_in_port;
2865 /* Look up a flow with 'in_port' as the input port. Then restore the
2866 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2867 * have surprising behavior). */
2868 old_in_port = ctx->flow.in_port;
2869 ctx->flow.in_port = in_port;
2870 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow);
2871 ctx->flow.in_port = old_in_port;
2873 if (ctx->resubmit_hook) {
2874 ctx->resubmit_hook(ctx, rule);
2879 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2883 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2885 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2886 MAX_RESUBMIT_RECURSION);
2891 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
2893 struct ofport_dpif *ofport;
2895 commit_odp_actions(ctx);
2896 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
2897 uint16_t ofp_port = ofport->up.ofp_port;
2898 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
2899 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT,
2904 ctx->nf_output_iface = NF_OUT_FLOOD;
2908 xlate_output_action__(struct action_xlate_ctx *ctx,
2909 uint16_t port, uint16_t max_len)
2911 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2913 ctx->nf_output_iface = NF_OUT_DROP;
2917 add_output_action(ctx, ctx->flow.in_port);
2920 xlate_table_action(ctx, ctx->flow.in_port);
2926 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
2929 flood_packets(ctx, htonl(0));
2931 case OFPP_CONTROLLER:
2932 commit_odp_actions(ctx);
2933 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2936 add_output_action(ctx, OFPP_LOCAL);
2939 if (port != ctx->flow.in_port) {
2940 add_output_action(ctx, port);
2945 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2946 ctx->nf_output_iface = NF_OUT_FLOOD;
2947 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2948 ctx->nf_output_iface = prev_nf_output_iface;
2949 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2950 ctx->nf_output_iface != NF_OUT_FLOOD) {
2951 ctx->nf_output_iface = NF_OUT_MULTI;
2956 xlate_output_action(struct action_xlate_ctx *ctx,
2957 const struct ofp_action_output *oao)
2959 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2963 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2964 const struct ofp_action_enqueue *oae)
2966 uint16_t ofp_port, odp_port;
2967 uint32_t ctx_priority, priority;
2970 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2973 /* Fall back to ordinary output action. */
2974 xlate_output_action__(ctx, ntohs(oae->port), 0);
2978 /* Figure out ODP output port. */
2979 ofp_port = ntohs(oae->port);
2980 if (ofp_port == OFPP_IN_PORT) {
2981 ofp_port = ctx->flow.in_port;
2983 odp_port = ofp_port_to_odp_port(ofp_port);
2985 /* Add ODP actions. */
2986 ctx_priority = ctx->priority;
2987 ctx->priority = priority;
2988 add_output_action(ctx, odp_port);
2989 ctx->priority = ctx_priority;
2991 /* Update NetFlow output port. */
2992 if (ctx->nf_output_iface == NF_OUT_DROP) {
2993 ctx->nf_output_iface = odp_port;
2994 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2995 ctx->nf_output_iface = NF_OUT_MULTI;
3000 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3001 const struct nx_action_set_queue *nasq)
3006 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3009 /* Couldn't translate queue to a priority, so ignore. A warning
3010 * has already been logged. */
3014 ctx->priority = priority;
3017 struct xlate_reg_state {
3023 xlate_autopath(struct action_xlate_ctx *ctx,
3024 const struct nx_action_autopath *naa)
3026 uint16_t ofp_port = ntohl(naa->id);
3027 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3029 if (!port || !port->bundle) {
3030 ofp_port = OFPP_NONE;
3031 } else if (port->bundle->bond) {
3032 /* Autopath does not support VLAN hashing. */
3033 struct ofport_dpif *slave = bond_choose_output_slave(
3034 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3036 ofp_port = slave->up.ofp_port;
3039 autopath_execute(naa, &ctx->flow, ofp_port);
3043 do_xlate_actions(const union ofp_action *in, size_t n_in,
3044 struct action_xlate_ctx *ctx)
3046 const struct ofport_dpif *port;
3047 const union ofp_action *ia;
3050 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3052 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3053 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3054 ? htonl(OFPPC_NO_RECV_STP)
3055 : htonl(OFPPC_NO_RECV))) {
3056 /* Drop this flow. */
3060 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3061 const struct ofp_action_dl_addr *oada;
3062 const struct nx_action_resubmit *nar;
3063 const struct nx_action_set_tunnel *nast;
3064 const struct nx_action_set_queue *nasq;
3065 const struct nx_action_multipath *nam;
3066 const struct nx_action_autopath *naa;
3067 enum ofputil_action_code code;
3070 code = ofputil_decode_action_unsafe(ia);
3072 case OFPUTIL_OFPAT_OUTPUT:
3073 xlate_output_action(ctx, &ia->output);
3076 case OFPUTIL_OFPAT_SET_VLAN_VID:
3077 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3078 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3081 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3082 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3083 ctx->flow.vlan_tci |= htons(
3084 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3087 case OFPUTIL_OFPAT_STRIP_VLAN:
3088 ctx->flow.vlan_tci = htons(0);
3091 case OFPUTIL_OFPAT_SET_DL_SRC:
3092 oada = ((struct ofp_action_dl_addr *) ia);
3093 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3096 case OFPUTIL_OFPAT_SET_DL_DST:
3097 oada = ((struct ofp_action_dl_addr *) ia);
3098 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3101 case OFPUTIL_OFPAT_SET_NW_SRC:
3102 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3105 case OFPUTIL_OFPAT_SET_NW_DST:
3106 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3109 case OFPUTIL_OFPAT_SET_NW_TOS:
3110 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
3113 case OFPUTIL_OFPAT_SET_TP_SRC:
3114 ctx->flow.tp_src = ia->tp_port.tp_port;
3117 case OFPUTIL_OFPAT_SET_TP_DST:
3118 ctx->flow.tp_dst = ia->tp_port.tp_port;
3121 case OFPUTIL_OFPAT_ENQUEUE:
3122 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3125 case OFPUTIL_NXAST_RESUBMIT:
3126 nar = (const struct nx_action_resubmit *) ia;
3127 xlate_table_action(ctx, ntohs(nar->in_port));
3130 case OFPUTIL_NXAST_SET_TUNNEL:
3131 nast = (const struct nx_action_set_tunnel *) ia;
3132 tun_id = htonll(ntohl(nast->tun_id));
3133 ctx->flow.tun_id = tun_id;
3136 case OFPUTIL_NXAST_SET_QUEUE:
3137 nasq = (const struct nx_action_set_queue *) ia;
3138 xlate_set_queue_action(ctx, nasq);
3141 case OFPUTIL_NXAST_POP_QUEUE:
3145 case OFPUTIL_NXAST_REG_MOVE:
3146 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3150 case OFPUTIL_NXAST_REG_LOAD:
3151 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3155 case OFPUTIL_NXAST_NOTE:
3156 /* Nothing to do. */
3159 case OFPUTIL_NXAST_SET_TUNNEL64:
3160 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3161 ctx->flow.tun_id = tun_id;
3164 case OFPUTIL_NXAST_MULTIPATH:
3165 nam = (const struct nx_action_multipath *) ia;
3166 multipath_execute(nam, &ctx->flow);
3169 case OFPUTIL_NXAST_AUTOPATH:
3170 naa = (const struct nx_action_autopath *) ia;
3171 xlate_autopath(ctx, naa);
3178 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3179 struct ofproto_dpif *ofproto, const struct flow *flow,
3180 const struct ofpbuf *packet)
3182 ctx->ofproto = ofproto;
3184 ctx->packet = packet;
3185 ctx->resubmit_hook = NULL;
3188 static struct ofpbuf *
3189 xlate_actions(struct action_xlate_ctx *ctx,
3190 const union ofp_action *in, size_t n_in)
3192 COVERAGE_INC(ofproto_dpif_xlate);
3194 ctx->odp_actions = ofpbuf_new(512);
3196 ctx->may_set_up_flow = true;
3197 ctx->nf_output_iface = NF_OUT_DROP;
3200 ctx->base_priority = 0;
3201 ctx->base_flow = ctx->flow;
3203 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3204 ctx->may_set_up_flow = false;
3206 do_xlate_actions(in, n_in, ctx);
3209 /* Check with in-band control to see if we're allowed to set up this
3211 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3212 ctx->odp_actions->data,
3213 ctx->odp_actions->size)) {
3214 ctx->may_set_up_flow = false;
3217 return ctx->odp_actions;
3220 /* OFPP_NORMAL implementation. */
3223 struct ofport_dpif *port;
3228 struct dst builtin[32];
3230 size_t n, allocated;
3233 static void dst_set_init(struct dst_set *);
3234 static void dst_set_add(struct dst_set *, const struct dst *);
3235 static void dst_set_free(struct dst_set *);
3237 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3240 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3241 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3243 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3244 : in_bundle->vlan >= 0 ? in_bundle->vlan
3245 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3246 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3248 dst->port = (!out_bundle->bond
3249 ? ofbundle_get_a_port(out_bundle)
3250 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3251 dst->vlan, &ctx->tags));
3253 return dst->port != NULL;
3257 mirror_mask_ffs(mirror_mask_t mask)
3259 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3264 dst_set_init(struct dst_set *set)
3266 set->dsts = set->builtin;
3268 set->allocated = ARRAY_SIZE(set->builtin);
3272 dst_set_add(struct dst_set *set, const struct dst *dst)
3274 if (set->n >= set->allocated) {
3275 size_t new_allocated;
3276 struct dst *new_dsts;
3278 new_allocated = set->allocated * 2;
3279 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3280 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3284 set->dsts = new_dsts;
3285 set->allocated = new_allocated;
3287 set->dsts[set->n++] = *dst;
3291 dst_set_free(struct dst_set *set)
3293 if (set->dsts != set->builtin) {
3299 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3302 for (i = 0; i < set->n; i++) {
3303 if (set->dsts[i].vlan == test->vlan
3304 && set->dsts[i].port == test->port) {
3312 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3314 return bundle->vlan < 0 && vlan_bitmap_contains(bundle->trunks, vlan);
3318 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3320 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3323 /* Returns an arbitrary interface within 'bundle'. */
3324 static struct ofport_dpif *
3325 ofbundle_get_a_port(const struct ofbundle *bundle)
3327 return CONTAINER_OF(list_front(&bundle->ports),
3328 struct ofport_dpif, bundle_node);
3332 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3333 const struct ofbundle *in_bundle,
3334 const struct ofbundle *out_bundle, struct dst_set *set)
3338 if (out_bundle == OFBUNDLE_FLOOD) {
3339 struct ofbundle *bundle;
3341 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3342 if (bundle != in_bundle
3343 && ofbundle_includes_vlan(bundle, vlan)
3344 && bundle->floodable
3345 && !bundle->mirror_out
3346 && set_dst(ctx, &dst, in_bundle, bundle)) {
3347 dst_set_add(set, &dst);
3350 ctx->nf_output_iface = NF_OUT_FLOOD;
3351 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3352 dst_set_add(set, &dst);
3353 ctx->nf_output_iface = dst.port->odp_port;
3358 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3360 return vlan_bitmap_contains(m->vlans, vlan);
3364 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3365 uint16_t vlan, const struct ofbundle *in_bundle,
3366 struct dst_set *set)
3368 struct ofproto_dpif *ofproto = ctx->ofproto;
3369 mirror_mask_t mirrors;
3373 mirrors = in_bundle->src_mirrors;
3374 for (i = 0; i < set->n; i++) {
3375 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3382 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3383 if (flow_vlan == 0) {
3384 flow_vlan = OFP_VLAN_NONE;
3388 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3389 if (vlan_is_mirrored(m, vlan)) {
3393 if (set_dst(ctx, &dst, in_bundle, m->out)
3394 && !dst_is_duplicate(set, &dst)) {
3395 dst_set_add(set, &dst);
3398 struct ofbundle *bundle;
3400 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3401 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3402 && set_dst(ctx, &dst, in_bundle, bundle))
3404 if (bundle->vlan < 0) {
3405 dst.vlan = m->out_vlan;
3407 if (dst_is_duplicate(set, &dst)) {
3411 /* Use the vlan tag on the original flow instead of
3412 * the one passed in the vlan parameter. This ensures
3413 * that we compare the vlan from before any implicit
3414 * tagging tags place. This is necessary because
3415 * dst->vlan is the final vlan, after removing implicit
3417 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3418 /* Don't send out input port on same VLAN. */
3421 dst_set_add(set, &dst);
3426 mirrors &= mirrors - 1;
3431 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3432 const struct ofbundle *in_bundle,
3433 const struct ofbundle *out_bundle)
3435 uint16_t initial_vlan, cur_vlan;
3436 const struct dst *dst;
3440 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3441 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3443 /* Output all the packets we can without having to change the VLAN. */
3444 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3445 if (initial_vlan == 0) {
3446 initial_vlan = OFP_VLAN_NONE;
3448 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3449 if (dst->vlan != initial_vlan) {
3452 nl_msg_put_u32(ctx->odp_actions,
3453 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3456 /* Then output the rest. */
3457 cur_vlan = initial_vlan;
3458 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3459 if (dst->vlan == initial_vlan) {
3462 if (dst->vlan != cur_vlan) {
3463 if (dst->vlan == OFP_VLAN_NONE) {
3464 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3467 tci = htons(dst->vlan & VLAN_VID_MASK);
3468 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3469 nl_msg_put_be16(ctx->odp_actions,
3470 ODP_ACTION_ATTR_SET_DL_TCI, tci);
3472 cur_vlan = dst->vlan;
3474 nl_msg_put_u32(ctx->odp_actions,
3475 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3481 /* Returns the effective vlan of a packet, taking into account both the
3482 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3483 * the packet is untagged and -1 indicates it has an invalid header and
3484 * should be dropped. */
3486 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3487 struct ofbundle *in_bundle, bool have_packet)
3489 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3490 if (in_bundle->vlan >= 0) {
3493 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3494 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3495 "packet received on port %s configured with "
3496 "implicit VLAN %"PRIu16,
3497 ofproto->up.name, vlan,
3498 in_bundle->name, in_bundle->vlan);
3502 vlan = in_bundle->vlan;
3504 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3506 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3507 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3508 "packet received on port %s not configured for "
3510 ofproto->up.name, vlan, in_bundle->name, vlan);
3519 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3520 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3521 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3523 is_gratuitous_arp(const struct flow *flow)
3525 return (flow->dl_type == htons(ETH_TYPE_ARP)
3526 && eth_addr_is_broadcast(flow->dl_dst)
3527 && (flow->nw_proto == ARP_OP_REPLY
3528 || (flow->nw_proto == ARP_OP_REQUEST
3529 && flow->nw_src == flow->nw_dst)));
3533 update_learning_table(struct ofproto_dpif *ofproto,
3534 const struct flow *flow, int vlan,
3535 struct ofbundle *in_bundle)
3537 struct mac_entry *mac;
3539 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3543 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3544 if (is_gratuitous_arp(flow)) {
3545 /* We don't want to learn from gratuitous ARP packets that are
3546 * reflected back over bond slaves so we lock the learning table. */
3547 if (!in_bundle->bond) {
3548 mac_entry_set_grat_arp_lock(mac);
3549 } else if (mac_entry_is_grat_arp_locked(mac)) {
3554 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3555 /* The log messages here could actually be useful in debugging,
3556 * so keep the rate limit relatively high. */
3557 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3558 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3559 "on port %s in VLAN %d",
3560 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3561 in_bundle->name, vlan);
3563 mac->port.p = in_bundle;
3564 tag_set_add(&ofproto->revalidate_set,
3565 mac_learning_changed(ofproto->ml, mac));
3569 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3570 * dropped. Returns true if they may be forwarded, false if they should be
3573 * If 'have_packet' is true, it indicates that the caller is processing a
3574 * received packet. If 'have_packet' is false, then the caller is just
3575 * revalidating an existing flow because configuration has changed. Either
3576 * way, 'have_packet' only affects logging (there is no point in logging errors
3577 * during revalidation).
3579 * Sets '*in_portp' to the input port. This will be a null pointer if
3580 * flow->in_port does not designate a known input port (in which case
3581 * is_admissible() returns false).
3583 * When returning true, sets '*vlanp' to the effective VLAN of the input
3584 * packet, as returned by flow_get_vlan().
3586 * May also add tags to '*tags', although the current implementation only does
3587 * so in one special case.
3590 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3592 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3594 struct ofport_dpif *in_port;
3595 struct ofbundle *in_bundle;
3598 /* Find the port and bundle for the received packet. */
3599 in_port = get_ofp_port(ofproto, flow->in_port);
3600 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3601 if (!in_port || !in_bundle) {
3602 /* No interface? Something fishy... */
3604 /* Odd. A few possible reasons here:
3606 * - We deleted a port but there are still a few packets queued up
3609 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3610 * we don't know about.
3612 * - Packet arrived on the local port but the local port is not
3615 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3617 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3619 ofproto->up.name, flow->in_port);
3623 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3628 /* Drop frames for reserved multicast addresses. */
3629 if (eth_addr_is_reserved(flow->dl_dst)) {
3633 /* Drop frames on bundles reserved for mirroring. */
3634 if (in_bundle->mirror_out) {
3636 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3637 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3638 "%s, which is reserved exclusively for mirroring",
3639 ofproto->up.name, in_bundle->name);
3644 if (in_bundle->bond) {
3645 struct mac_entry *mac;
3647 switch (bond_check_admissibility(in_bundle->bond, in_port,
3648 flow->dl_dst, tags)) {
3655 case BV_DROP_IF_MOVED:
3656 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3657 if (mac && mac->port.p != in_bundle &&
3658 (!is_gratuitous_arp(flow)
3659 || mac_entry_is_grat_arp_locked(mac))) {
3669 /* If the composed actions may be applied to any packet in the given 'flow',
3670 * returns true. Otherwise, the actions should only be applied to 'packet', or
3671 * not at all, if 'packet' was NULL. */
3673 xlate_normal(struct action_xlate_ctx *ctx)
3675 struct ofbundle *in_bundle;
3676 struct ofbundle *out_bundle;
3677 struct mac_entry *mac;
3680 /* Check whether we should drop packets in this flow. */
3681 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3682 &ctx->tags, &vlan, &in_bundle)) {
3687 /* Learn source MAC (but don't try to learn from revalidation). */
3689 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3692 /* Determine output bundle. */
3693 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3696 out_bundle = mac->port.p;
3697 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3698 /* If we are revalidating but don't have a learning entry then eject
3699 * the flow. Installing a flow that floods packets opens up a window
3700 * of time where we could learn from a packet reflected on a bond and
3701 * blackhole packets before the learning table is updated to reflect
3702 * the correct port. */
3705 out_bundle = OFBUNDLE_FLOOD;
3708 /* Don't send packets out their input bundles. */
3709 if (in_bundle == out_bundle) {
3715 compose_actions(ctx, vlan, in_bundle, out_bundle);
3722 get_drop_frags(struct ofproto *ofproto_)
3724 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3727 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3732 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3734 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3736 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3740 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3741 const struct flow *flow,
3742 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3744 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3747 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3748 ofproto->max_ports);
3750 struct odputil_keybuf keybuf;
3751 struct action_xlate_ctx ctx;
3752 struct ofpbuf *odp_actions;
3755 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3756 odp_flow_key_from_flow(&key, flow);
3758 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3759 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3760 dpif_execute(ofproto->dpif, key.data, key.size,
3761 odp_actions->data, odp_actions->size, packet);
3762 ofpbuf_delete(odp_actions);
3768 get_netflow_ids(const struct ofproto *ofproto_,
3769 uint8_t *engine_type, uint8_t *engine_id)
3771 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3773 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3776 static struct ofproto_dpif *
3777 ofproto_dpif_lookup(const char *name)
3779 struct ofproto *ofproto = ofproto_lookup(name);
3780 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3781 ? ofproto_dpif_cast(ofproto)
3786 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3787 const char *args, void *aux OVS_UNUSED)
3789 struct ds ds = DS_EMPTY_INITIALIZER;
3790 const struct ofproto_dpif *ofproto;
3791 const struct mac_entry *e;
3793 ofproto = ofproto_dpif_lookup(args);
3795 unixctl_command_reply(conn, 501, "no such bridge");
3799 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3800 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3801 struct ofbundle *bundle = e->port.p;
3802 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
3803 ofbundle_get_a_port(bundle)->odp_port,
3804 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3806 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3810 struct ofproto_trace {
3811 struct action_xlate_ctx ctx;
3817 trace_format_rule(struct ds *result, int level, const struct rule *rule)
3819 ds_put_char_multiple(result, '\t', level);
3821 ds_put_cstr(result, "No match\n");
3825 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
3826 ntohll(rule->flow_cookie));
3827 cls_rule_format(&rule->cr, result);
3828 ds_put_char(result, '\n');
3830 ds_put_char_multiple(result, '\t', level);
3831 ds_put_cstr(result, "OpenFlow ");
3832 ofp_print_actions(result, rule->actions, rule->n_actions);
3833 ds_put_char(result, '\n');
3837 trace_format_flow(struct ds *result, int level, const char *title,
3838 struct ofproto_trace *trace)
3840 ds_put_char_multiple(result, '\t', level);
3841 ds_put_format(result, "%s: ", title);
3842 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
3843 ds_put_cstr(result, "unchanged");
3845 flow_format(result, &trace->ctx.flow);
3846 trace->flow = trace->ctx.flow;
3848 ds_put_char(result, '\n');
3852 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3854 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
3855 struct ds *result = trace->result;
3857 ds_put_char(result, '\n');
3858 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
3859 trace_format_rule(result, ctx->recurse + 1, &rule->up);
3863 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
3864 void *aux OVS_UNUSED)
3866 char *dpname, *in_port_s, *tun_id_s, *packet_s;
3867 char *args = xstrdup(args_);
3868 char *save_ptr = NULL;
3869 struct ofproto_dpif *ofproto;
3870 struct ofpbuf packet;
3871 struct rule_dpif *rule;
3878 ofpbuf_init(&packet, strlen(args) / 2);
3881 dpname = strtok_r(args, " ", &save_ptr);
3882 tun_id_s = strtok_r(NULL, " ", &save_ptr);
3883 in_port_s = strtok_r(NULL, " ", &save_ptr);
3884 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
3885 if (!dpname || !in_port_s || !packet_s) {
3886 unixctl_command_reply(conn, 501, "Bad command syntax");
3890 ofproto = ofproto_dpif_lookup(dpname);
3892 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
3897 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
3898 in_port = ofp_port_to_odp_port(atoi(in_port_s));
3900 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
3901 packet_s += strspn(packet_s, " ");
3902 if (*packet_s != '\0') {
3903 unixctl_command_reply(conn, 501, "Trailing garbage in command");
3906 if (packet.size < ETH_HEADER_LEN) {
3907 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
3911 ds_put_cstr(&result, "Packet: ");
3912 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
3913 ds_put_cstr(&result, s);
3916 flow_extract(&packet, tun_id, in_port, &flow);
3917 ds_put_cstr(&result, "Flow: ");
3918 flow_format(&result, &flow);
3919 ds_put_char(&result, '\n');
3921 rule = rule_dpif_lookup(ofproto, &flow);
3922 trace_format_rule(&result, 0, &rule->up);
3924 struct ofproto_trace trace;
3925 struct ofpbuf *odp_actions;
3927 trace.result = &result;
3929 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
3930 trace.ctx.resubmit_hook = trace_resubmit;
3931 odp_actions = xlate_actions(&trace.ctx,
3932 rule->up.actions, rule->up.n_actions);
3934 ds_put_char(&result, '\n');
3935 trace_format_flow(&result, 0, "Final flow", &trace);
3936 ds_put_cstr(&result, "Datapath actions: ");
3937 format_odp_actions(&result, odp_actions->data, odp_actions->size);
3938 ofpbuf_delete(odp_actions);
3941 unixctl_command_reply(conn, 200, ds_cstr(&result));
3944 ds_destroy(&result);
3945 ofpbuf_uninit(&packet);
3950 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
3951 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
3954 unixctl_command_reply(conn, 200, NULL);
3958 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
3959 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
3962 unixctl_command_reply(conn, 200, NULL);
3966 ofproto_dpif_unixctl_init(void)
3968 static bool registered;
3974 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
3975 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
3977 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
3978 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
3981 const struct ofproto_class ofproto_dpif_class = {
4008 port_is_lacp_current,
4009 NULL, /* rule_choose_table */
4016 rule_modify_actions,
4029 is_mirror_output_bundle,