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"
26 #include "byte-order.h"
31 #include "dynamic-string.h"
32 #include "fail-open.h"
35 #include "mac-learning.h"
36 #include "multipath.h"
43 #include "ofp-print.h"
44 #include "ofproto-dpif-sflow.h"
45 #include "poll-loop.h"
47 #include "unaligned.h"
49 #include "vlan-bitmap.h"
52 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
54 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
55 COVERAGE_DEFINE(ofproto_dpif_expired);
56 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
57 COVERAGE_DEFINE(ofproto_dpif_xlate);
58 COVERAGE_DEFINE(facet_changed_rule);
59 COVERAGE_DEFINE(facet_invalidated);
60 COVERAGE_DEFINE(facet_revalidate);
61 COVERAGE_DEFINE(facet_unexpected);
63 /* Maximum depth of flow table recursion (due to resubmit actions) in a
64 * flow translation. */
65 #define MAX_RESUBMIT_RECURSION 16
73 long long int used; /* Time last used; time created if not used. */
77 * - Do include packets and bytes from facets that have been deleted or
78 * whose own statistics have been folded into the rule.
80 * - Do include packets and bytes sent "by hand" that were accounted to
81 * the rule without any facet being involved (this is a rare corner
82 * case in rule_execute()).
84 * - Do not include packet or bytes that can be obtained from any facet's
85 * packet_count or byte_count member or that can be obtained from the
86 * datapath by, e.g., dpif_flow_get() for any facet.
88 uint64_t packet_count; /* Number of packets received. */
89 uint64_t byte_count; /* Number of bytes received. */
91 struct list facets; /* List of "struct facet"s. */
94 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
96 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
99 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
100 const struct flow *, uint8_t table);
102 #define MAX_MIRRORS 32
103 typedef uint32_t mirror_mask_t;
104 #define MIRROR_MASK_C(X) UINT32_C(X)
105 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
107 struct ofproto_dpif *ofproto; /* Owning ofproto. */
108 size_t idx; /* In ofproto's "mirrors" array. */
109 void *aux; /* Key supplied by ofproto's client. */
110 char *name; /* Identifier for log messages. */
112 /* Selection criteria. */
113 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
114 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
115 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
117 /* Output (mutually exclusive). */
118 struct ofbundle *out; /* Output port or NULL. */
119 int out_vlan; /* Output VLAN or -1. */
122 static void mirror_destroy(struct ofmirror *);
124 /* A group of one or more OpenFlow ports. */
125 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
127 struct ofproto_dpif *ofproto; /* Owning ofproto. */
128 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
129 void *aux; /* Key supplied by ofproto's client. */
130 char *name; /* Identifier for log messages. */
133 struct list ports; /* Contains "struct ofport"s. */
134 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
135 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
136 * NULL if all VLANs are trunked. */
137 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
138 struct bond *bond; /* Nonnull iff more than one port. */
141 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
143 /* Port mirroring info. */
144 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
145 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
146 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
149 static void bundle_remove(struct ofport *);
150 static void bundle_destroy(struct ofbundle *);
151 static void bundle_del_port(struct ofport_dpif *);
152 static void bundle_run(struct ofbundle *);
153 static void bundle_wait(struct ofbundle *);
155 struct action_xlate_ctx {
156 /* action_xlate_ctx_init() initializes these members. */
159 struct ofproto_dpif *ofproto;
161 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
162 * this flow when actions change header fields. */
165 /* The packet corresponding to 'flow', or a null pointer if we are
166 * revalidating without a packet to refer to. */
167 const struct ofpbuf *packet;
169 /* If nonnull, called just before executing a resubmit action.
171 * This is normally null so the client has to set it manually after
172 * calling action_xlate_ctx_init(). */
173 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
175 /* xlate_actions() initializes and uses these members. The client might want
176 * to look at them after it returns. */
178 struct ofpbuf *odp_actions; /* Datapath actions. */
179 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
180 bool may_set_up_flow; /* True ordinarily; false if the actions must
181 * be reassessed for every packet. */
182 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
184 /* xlate_actions() initializes and uses these members, but the client has no
185 * reason to look at them. */
187 int recurse; /* Recursion level, via xlate_table_action. */
188 uint32_t priority; /* Current flow priority. 0 if none. */
189 struct flow base_flow; /* Flow at the last commit. */
190 uint32_t base_priority; /* Priority at the last commit. */
191 uint8_t table_id; /* OpenFlow table ID where flow was found. */
194 static void action_xlate_ctx_init(struct action_xlate_ctx *,
195 struct ofproto_dpif *, const struct flow *,
196 const struct ofpbuf *);
197 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
198 const union ofp_action *in, size_t n_in);
200 /* An exact-match instantiation of an OpenFlow flow. */
202 long long int used; /* Time last used; time created if not used. */
206 * - Do include packets and bytes sent "by hand", e.g. with
209 * - Do include packets and bytes that were obtained from the datapath
210 * when a flow was deleted (e.g. dpif_flow_del()) or when its
211 * statistics were reset (e.g. dpif_flow_put() with
212 * DPIF_FP_ZERO_STATS).
214 * - Do not include any packets or bytes that can currently be obtained
215 * from the datapath by, e.g., dpif_flow_get().
217 uint64_t packet_count; /* Number of packets received. */
218 uint64_t byte_count; /* Number of bytes received. */
220 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
221 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
223 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
224 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
225 long long int rs_used; /* Used time pushed to resubmit children. */
227 /* Number of bytes passed to account_cb. This may include bytes that can
228 * currently obtained from the datapath (thus, it can be greater than
230 uint64_t accounted_bytes;
232 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
233 struct list list_node; /* In owning rule's 'facets' list. */
234 struct rule_dpif *rule; /* Owning rule. */
235 struct flow flow; /* Exact-match flow. */
236 bool installed; /* Installed in datapath? */
237 bool may_install; /* True ordinarily; false if actions must
238 * be reassessed for every packet. */
239 size_t actions_len; /* Number of bytes in actions[]. */
240 struct nlattr *actions; /* Datapath actions. */
241 tag_type tags; /* Tags. */
242 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
245 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
246 const struct ofpbuf *packet);
247 static void facet_remove(struct ofproto_dpif *, struct facet *);
248 static void facet_free(struct facet *);
250 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
251 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
252 const struct flow *);
253 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
255 static void facet_execute(struct ofproto_dpif *, struct facet *,
256 struct ofpbuf *packet);
258 static int facet_put__(struct ofproto_dpif *, struct facet *,
259 const struct nlattr *actions, size_t actions_len,
260 struct dpif_flow_stats *);
261 static void facet_install(struct ofproto_dpif *, struct facet *,
263 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
264 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
266 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
267 const struct ofpbuf *packet);
268 static void facet_update_time(struct ofproto_dpif *, struct facet *,
270 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
271 const struct dpif_flow_stats *);
272 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
273 static void facet_push_stats(struct facet *);
274 static void facet_account(struct ofproto_dpif *, struct facet *,
275 uint64_t extra_bytes);
277 static bool facet_is_controller_flow(struct facet *);
279 static void flow_push_stats(const struct rule_dpif *,
280 struct flow *, uint64_t packets, uint64_t bytes,
287 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
288 struct list bundle_node; /* In struct ofbundle's "ports" list. */
289 struct cfm *cfm; /* Connectivity Fault Management, if any. */
290 tag_type tag; /* Tag associated with this port. */
291 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
292 bool may_enable; /* May be enabled in bonds. */
295 static struct ofport_dpif *
296 ofport_dpif_cast(const struct ofport *ofport)
298 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
299 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
302 static void port_run(struct ofport_dpif *);
303 static void port_wait(struct ofport_dpif *);
304 static int set_cfm(struct ofport *, const struct cfm_settings *);
306 struct dpif_completion {
307 struct list list_node;
308 struct ofoperation *op;
311 struct ofproto_dpif {
320 struct netflow *netflow;
321 struct dpif_sflow *sflow;
322 struct hmap bundles; /* Contains "struct ofbundle"s. */
323 struct mac_learning *ml;
324 struct ofmirror *mirrors[MAX_MIRRORS];
325 bool has_bonded_bundles;
328 struct timer next_expiration;
332 bool need_revalidate;
333 struct tag_set revalidate_set;
335 /* Support for debugging async flow mods. */
336 struct list completions;
338 bool has_bundle_action; /* True when the first bundle action appears. */
341 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
342 * for debugging the asynchronous flow_mod implementation.) */
345 static void ofproto_dpif_unixctl_init(void);
347 static struct ofproto_dpif *
348 ofproto_dpif_cast(const struct ofproto *ofproto)
350 assert(ofproto->ofproto_class == &ofproto_dpif_class);
351 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
354 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
356 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
359 /* Packet processing. */
360 static void update_learning_table(struct ofproto_dpif *,
361 const struct flow *, int vlan,
363 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
364 bool have_packet, tag_type *, int *vlanp,
365 struct ofbundle **in_bundlep);
366 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
368 /* Flow expiration. */
369 static int expire(struct ofproto_dpif *);
372 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
373 const struct ofpbuf *packet);
375 /* Global variables. */
376 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
378 /* Factory functions. */
381 enumerate_types(struct sset *types)
383 dp_enumerate_types(types);
387 enumerate_names(const char *type, struct sset *names)
389 return dp_enumerate_names(type, names);
393 del(const char *type, const char *name)
398 error = dpif_open(name, type, &dpif);
400 error = dpif_delete(dpif);
406 /* Basic life-cycle. */
408 static struct ofproto *
411 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
416 dealloc(struct ofproto *ofproto_)
418 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
423 construct(struct ofproto *ofproto_, int *n_tablesp)
425 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
426 const char *name = ofproto->up.name;
430 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
432 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
436 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
437 ofproto->n_matches = 0;
439 error = dpif_recv_set_mask(ofproto->dpif,
440 ((1u << DPIF_UC_MISS) |
441 (1u << DPIF_UC_ACTION) |
442 (1u << DPIF_UC_SAMPLE)));
444 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
445 dpif_close(ofproto->dpif);
448 dpif_flow_flush(ofproto->dpif);
449 dpif_recv_purge(ofproto->dpif);
451 ofproto->netflow = NULL;
452 ofproto->sflow = NULL;
453 hmap_init(&ofproto->bundles);
454 ofproto->ml = mac_learning_create();
455 for (i = 0; i < MAX_MIRRORS; i++) {
456 ofproto->mirrors[i] = NULL;
458 ofproto->has_bonded_bundles = false;
460 timer_set_duration(&ofproto->next_expiration, 1000);
462 hmap_init(&ofproto->facets);
463 ofproto->need_revalidate = false;
464 tag_set_init(&ofproto->revalidate_set);
466 list_init(&ofproto->completions);
468 ofproto_dpif_unixctl_init();
470 ofproto->has_bundle_action = false;
477 complete_operations(struct ofproto_dpif *ofproto)
479 struct dpif_completion *c, *next;
481 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
482 ofoperation_complete(c->op, 0);
483 list_remove(&c->list_node);
489 destruct(struct ofproto *ofproto_)
491 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
492 struct rule_dpif *rule, *next_rule;
493 struct classifier *table;
496 complete_operations(ofproto);
498 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
499 struct cls_cursor cursor;
501 cls_cursor_init(&cursor, table, NULL);
502 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
503 ofproto_rule_destroy(&rule->up);
507 for (i = 0; i < MAX_MIRRORS; i++) {
508 mirror_destroy(ofproto->mirrors[i]);
511 netflow_destroy(ofproto->netflow);
512 dpif_sflow_destroy(ofproto->sflow);
513 hmap_destroy(&ofproto->bundles);
514 mac_learning_destroy(ofproto->ml);
516 hmap_destroy(&ofproto->facets);
518 dpif_close(ofproto->dpif);
522 run(struct ofproto *ofproto_)
524 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
525 struct ofport_dpif *ofport;
526 struct ofbundle *bundle;
530 complete_operations(ofproto);
532 dpif_run(ofproto->dpif);
534 for (i = 0; i < 50; i++) {
535 struct dpif_upcall packet;
538 error = dpif_recv(ofproto->dpif, &packet);
540 if (error == ENODEV) {
541 /* Datapath destroyed. */
547 handle_upcall(ofproto, &packet);
550 if (timer_expired(&ofproto->next_expiration)) {
551 int delay = expire(ofproto);
552 timer_set_duration(&ofproto->next_expiration, delay);
555 if (ofproto->netflow) {
556 netflow_run(ofproto->netflow);
558 if (ofproto->sflow) {
559 dpif_sflow_run(ofproto->sflow);
562 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
565 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
569 /* Now revalidate if there's anything to do. */
570 if (ofproto->need_revalidate
571 || !tag_set_is_empty(&ofproto->revalidate_set)) {
572 struct tag_set revalidate_set = ofproto->revalidate_set;
573 bool revalidate_all = ofproto->need_revalidate;
574 struct facet *facet, *next;
576 /* Clear the revalidation flags. */
577 tag_set_init(&ofproto->revalidate_set);
578 ofproto->need_revalidate = false;
580 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
582 || tag_set_intersects(&revalidate_set, facet->tags)) {
583 facet_revalidate(ofproto, facet);
592 wait(struct ofproto *ofproto_)
594 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
595 struct ofport_dpif *ofport;
596 struct ofbundle *bundle;
598 if (!clogged && !list_is_empty(&ofproto->completions)) {
599 poll_immediate_wake();
602 dpif_wait(ofproto->dpif);
603 dpif_recv_wait(ofproto->dpif);
604 if (ofproto->sflow) {
605 dpif_sflow_wait(ofproto->sflow);
607 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
608 poll_immediate_wake();
610 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
613 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
616 if (ofproto->need_revalidate) {
617 /* Shouldn't happen, but if it does just go around again. */
618 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
619 poll_immediate_wake();
621 timer_wait(&ofproto->next_expiration);
626 flush(struct ofproto *ofproto_)
628 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
629 struct facet *facet, *next_facet;
631 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
632 /* Mark the facet as not installed so that facet_remove() doesn't
633 * bother trying to uninstall it. There is no point in uninstalling it
634 * individually since we are about to blow away all the facets with
635 * dpif_flow_flush(). */
636 facet->installed = false;
637 facet->dp_packet_count = 0;
638 facet->dp_byte_count = 0;
639 facet_remove(ofproto, facet);
641 dpif_flow_flush(ofproto->dpif);
645 get_features(struct ofproto *ofproto_ OVS_UNUSED,
646 bool *arp_match_ip, uint32_t *actions)
648 *arp_match_ip = true;
649 *actions = ((1u << OFPAT_OUTPUT) |
650 (1u << OFPAT_SET_VLAN_VID) |
651 (1u << OFPAT_SET_VLAN_PCP) |
652 (1u << OFPAT_STRIP_VLAN) |
653 (1u << OFPAT_SET_DL_SRC) |
654 (1u << OFPAT_SET_DL_DST) |
655 (1u << OFPAT_SET_NW_SRC) |
656 (1u << OFPAT_SET_NW_DST) |
657 (1u << OFPAT_SET_NW_TOS) |
658 (1u << OFPAT_SET_TP_SRC) |
659 (1u << OFPAT_SET_TP_DST) |
660 (1u << OFPAT_ENQUEUE));
664 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
666 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
669 strcpy(ots->name, "classifier");
671 dpif_get_dp_stats(ofproto->dpif, &s);
672 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
673 put_32aligned_be64(&ots->matched_count,
674 htonll(s.n_hit + ofproto->n_matches));
678 set_netflow(struct ofproto *ofproto_,
679 const struct netflow_options *netflow_options)
681 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
683 if (netflow_options) {
684 if (!ofproto->netflow) {
685 ofproto->netflow = netflow_create();
687 return netflow_set_options(ofproto->netflow, netflow_options);
689 netflow_destroy(ofproto->netflow);
690 ofproto->netflow = NULL;
695 static struct ofport *
698 struct ofport_dpif *port = xmalloc(sizeof *port);
703 port_dealloc(struct ofport *port_)
705 struct ofport_dpif *port = ofport_dpif_cast(port_);
710 port_construct(struct ofport *port_)
712 struct ofport_dpif *port = ofport_dpif_cast(port_);
713 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
715 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
718 port->tag = tag_create_random();
719 port->may_enable = true;
721 if (ofproto->sflow) {
722 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
723 netdev_get_name(port->up.netdev));
730 port_destruct(struct ofport *port_)
732 struct ofport_dpif *port = ofport_dpif_cast(port_);
733 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
735 bundle_remove(port_);
736 set_cfm(port_, NULL);
737 if (ofproto->sflow) {
738 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
743 port_modified(struct ofport *port_)
745 struct ofport_dpif *port = ofport_dpif_cast(port_);
747 if (port->bundle && port->bundle->bond) {
748 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
753 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
755 struct ofport_dpif *port = ofport_dpif_cast(port_);
756 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
757 ovs_be32 changed = old_config ^ port->up.opp.config;
759 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
760 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
761 ofproto->need_revalidate = true;
766 set_sflow(struct ofproto *ofproto_,
767 const struct ofproto_sflow_options *sflow_options)
769 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
770 struct dpif_sflow *ds = ofproto->sflow;
773 struct ofport_dpif *ofport;
775 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
776 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
777 dpif_sflow_add_port(ds, ofport->odp_port,
778 netdev_get_name(ofport->up.netdev));
781 dpif_sflow_set_options(ds, sflow_options);
783 dpif_sflow_destroy(ds);
784 ofproto->sflow = NULL;
790 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
792 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
799 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
802 if (cfm_configure(ofport->cfm, s)) {
808 cfm_destroy(ofport->cfm);
814 get_cfm_fault(const struct ofport *ofport_)
816 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
818 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
823 /* Expires all MAC learning entries associated with 'port' and forces ofproto
824 * to revalidate every flow. */
826 bundle_flush_macs(struct ofbundle *bundle)
828 struct ofproto_dpif *ofproto = bundle->ofproto;
829 struct mac_learning *ml = ofproto->ml;
830 struct mac_entry *mac, *next_mac;
832 ofproto->need_revalidate = true;
833 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
834 if (mac->port.p == bundle) {
835 mac_learning_expire(ml, mac);
840 static struct ofbundle *
841 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
843 struct ofbundle *bundle;
845 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
847 if (bundle->aux == aux) {
854 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
855 * ones that are found to 'bundles'. */
857 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
858 void **auxes, size_t n_auxes,
859 struct hmapx *bundles)
864 for (i = 0; i < n_auxes; i++) {
865 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
867 hmapx_add(bundles, bundle);
873 bundle_del_port(struct ofport_dpif *port)
875 struct ofbundle *bundle = port->bundle;
877 bundle->ofproto->need_revalidate = true;
879 list_remove(&port->bundle_node);
883 lacp_slave_unregister(bundle->lacp, port);
886 bond_slave_unregister(bundle->bond, port);
889 bundle->floodable = true;
890 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
891 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
892 bundle->floodable = false;
898 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
899 struct lacp_slave_settings *lacp,
900 uint32_t bond_stable_id)
902 struct ofport_dpif *port;
904 port = get_ofp_port(bundle->ofproto, ofp_port);
909 if (port->bundle != bundle) {
910 bundle->ofproto->need_revalidate = true;
912 bundle_del_port(port);
915 port->bundle = bundle;
916 list_push_back(&bundle->ports, &port->bundle_node);
917 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
918 bundle->floodable = false;
922 lacp_slave_register(bundle->lacp, port, lacp);
925 port->bond_stable_id = bond_stable_id;
931 bundle_destroy(struct ofbundle *bundle)
933 struct ofproto_dpif *ofproto;
934 struct ofport_dpif *port, *next_port;
941 ofproto = bundle->ofproto;
942 for (i = 0; i < MAX_MIRRORS; i++) {
943 struct ofmirror *m = ofproto->mirrors[i];
945 if (m->out == bundle) {
947 } else if (hmapx_find_and_delete(&m->srcs, bundle)
948 || hmapx_find_and_delete(&m->dsts, bundle)) {
949 ofproto->need_revalidate = true;
954 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
955 bundle_del_port(port);
958 bundle_flush_macs(bundle);
959 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
961 free(bundle->trunks);
962 lacp_destroy(bundle->lacp);
963 bond_destroy(bundle->bond);
968 bundle_set(struct ofproto *ofproto_, void *aux,
969 const struct ofproto_bundle_settings *s)
971 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
972 bool need_flush = false;
973 const unsigned long *trunks;
974 struct ofport_dpif *port;
975 struct ofbundle *bundle;
980 bundle_destroy(bundle_lookup(ofproto, aux));
984 assert(s->n_slaves == 1 || s->bond != NULL);
985 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
987 bundle = bundle_lookup(ofproto, aux);
989 bundle = xmalloc(sizeof *bundle);
991 bundle->ofproto = ofproto;
992 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
993 hash_pointer(aux, 0));
997 list_init(&bundle->ports);
999 bundle->trunks = NULL;
1000 bundle->lacp = NULL;
1001 bundle->bond = NULL;
1003 bundle->floodable = true;
1005 bundle->src_mirrors = 0;
1006 bundle->dst_mirrors = 0;
1007 bundle->mirror_out = 0;
1010 if (!bundle->name || strcmp(s->name, bundle->name)) {
1012 bundle->name = xstrdup(s->name);
1017 if (!bundle->lacp) {
1018 bundle->lacp = lacp_create();
1020 lacp_configure(bundle->lacp, s->lacp);
1022 lacp_destroy(bundle->lacp);
1023 bundle->lacp = NULL;
1026 /* Update set of ports. */
1028 for (i = 0; i < s->n_slaves; i++) {
1029 if (!bundle_add_port(bundle, s->slaves[i],
1030 s->lacp ? &s->lacp_slaves[i] : NULL,
1031 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1035 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1036 struct ofport_dpif *next_port;
1038 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1039 for (i = 0; i < s->n_slaves; i++) {
1040 if (s->slaves[i] == port->up.ofp_port) {
1045 bundle_del_port(port);
1049 assert(list_size(&bundle->ports) <= s->n_slaves);
1051 if (list_is_empty(&bundle->ports)) {
1052 bundle_destroy(bundle);
1057 if (s->vlan != bundle->vlan) {
1058 bundle->vlan = s->vlan;
1062 /* Get trunked VLANs. */
1063 trunks = s->vlan == -1 ? NULL : s->trunks;
1064 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1065 free(bundle->trunks);
1066 bundle->trunks = vlan_bitmap_clone(trunks);
1071 if (!list_is_short(&bundle->ports)) {
1072 bundle->ofproto->has_bonded_bundles = true;
1074 if (bond_reconfigure(bundle->bond, s->bond)) {
1075 ofproto->need_revalidate = true;
1078 bundle->bond = bond_create(s->bond);
1079 ofproto->need_revalidate = true;
1082 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1083 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1087 bond_destroy(bundle->bond);
1088 bundle->bond = NULL;
1091 /* If we changed something that would affect MAC learning, un-learn
1092 * everything on this port and force flow revalidation. */
1094 bundle_flush_macs(bundle);
1101 bundle_remove(struct ofport *port_)
1103 struct ofport_dpif *port = ofport_dpif_cast(port_);
1104 struct ofbundle *bundle = port->bundle;
1107 bundle_del_port(port);
1108 if (list_is_empty(&bundle->ports)) {
1109 bundle_destroy(bundle);
1110 } else if (list_is_short(&bundle->ports)) {
1111 bond_destroy(bundle->bond);
1112 bundle->bond = NULL;
1118 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1120 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1121 struct ofport_dpif *port = port_;
1122 uint8_t ea[ETH_ADDR_LEN];
1125 error = netdev_get_etheraddr(port->up.netdev, ea);
1127 struct lacp_pdu *packet_pdu;
1128 struct ofpbuf packet;
1130 ofpbuf_init(&packet, 0);
1131 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1132 sizeof *packet_pdu);
1134 error = netdev_send(port->up.netdev, &packet);
1136 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1137 "(%s)", port->bundle->name,
1138 netdev_get_name(port->up.netdev), strerror(error));
1140 ofpbuf_uninit(&packet);
1142 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1143 "%s (%s)", port->bundle->name,
1144 netdev_get_name(port->up.netdev), strerror(error));
1149 bundle_send_learning_packets(struct ofbundle *bundle)
1151 struct ofproto_dpif *ofproto = bundle->ofproto;
1152 int error, n_packets, n_errors;
1153 struct mac_entry *e;
1155 error = n_packets = n_errors = 0;
1156 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1157 if (e->port.p != bundle) {
1158 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1168 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1169 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1170 "packets, last error was: %s",
1171 bundle->name, n_errors, n_packets, strerror(error));
1173 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1174 bundle->name, n_packets);
1179 bundle_run(struct ofbundle *bundle)
1182 lacp_run(bundle->lacp, send_pdu_cb);
1185 struct ofport_dpif *port;
1187 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1188 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1191 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1192 lacp_negotiated(bundle->lacp));
1193 if (bond_should_send_learning_packets(bundle->bond)) {
1194 bundle_send_learning_packets(bundle);
1200 bundle_wait(struct ofbundle *bundle)
1203 lacp_wait(bundle->lacp);
1206 bond_wait(bundle->bond);
1213 mirror_scan(struct ofproto_dpif *ofproto)
1217 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1218 if (!ofproto->mirrors[idx]) {
1225 static struct ofmirror *
1226 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1230 for (i = 0; i < MAX_MIRRORS; i++) {
1231 struct ofmirror *mirror = ofproto->mirrors[i];
1232 if (mirror && mirror->aux == aux) {
1241 mirror_set(struct ofproto *ofproto_, void *aux,
1242 const struct ofproto_mirror_settings *s)
1244 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1245 mirror_mask_t mirror_bit;
1246 struct ofbundle *bundle;
1247 struct ofmirror *mirror;
1248 struct ofbundle *out;
1249 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1250 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1253 mirror = mirror_lookup(ofproto, aux);
1255 mirror_destroy(mirror);
1261 idx = mirror_scan(ofproto);
1263 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1265 ofproto->up.name, MAX_MIRRORS, s->name);
1269 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1270 mirror->ofproto = ofproto;
1273 mirror->out_vlan = -1;
1274 mirror->name = NULL;
1277 if (!mirror->name || strcmp(s->name, mirror->name)) {
1279 mirror->name = xstrdup(s->name);
1282 /* Get the new configuration. */
1283 if (s->out_bundle) {
1284 out = bundle_lookup(ofproto, s->out_bundle);
1286 mirror_destroy(mirror);
1292 out_vlan = s->out_vlan;
1294 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1295 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1297 /* If the configuration has not changed, do nothing. */
1298 if (hmapx_equals(&srcs, &mirror->srcs)
1299 && hmapx_equals(&dsts, &mirror->dsts)
1300 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1301 && mirror->out == out
1302 && mirror->out_vlan == out_vlan)
1304 hmapx_destroy(&srcs);
1305 hmapx_destroy(&dsts);
1309 hmapx_swap(&srcs, &mirror->srcs);
1310 hmapx_destroy(&srcs);
1312 hmapx_swap(&dsts, &mirror->dsts);
1313 hmapx_destroy(&dsts);
1315 free(mirror->vlans);
1316 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1319 mirror->out_vlan = out_vlan;
1321 /* Update bundles. */
1322 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1323 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1324 if (hmapx_contains(&mirror->srcs, bundle)) {
1325 bundle->src_mirrors |= mirror_bit;
1327 bundle->src_mirrors &= ~mirror_bit;
1330 if (hmapx_contains(&mirror->dsts, bundle)) {
1331 bundle->dst_mirrors |= mirror_bit;
1333 bundle->dst_mirrors &= ~mirror_bit;
1336 if (mirror->out == bundle) {
1337 bundle->mirror_out |= mirror_bit;
1339 bundle->mirror_out &= ~mirror_bit;
1343 ofproto->need_revalidate = true;
1344 mac_learning_flush(ofproto->ml);
1350 mirror_destroy(struct ofmirror *mirror)
1352 struct ofproto_dpif *ofproto;
1353 mirror_mask_t mirror_bit;
1354 struct ofbundle *bundle;
1360 ofproto = mirror->ofproto;
1361 ofproto->need_revalidate = true;
1362 mac_learning_flush(ofproto->ml);
1364 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1365 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1366 bundle->src_mirrors &= ~mirror_bit;
1367 bundle->dst_mirrors &= ~mirror_bit;
1368 bundle->mirror_out &= ~mirror_bit;
1371 hmapx_destroy(&mirror->srcs);
1372 hmapx_destroy(&mirror->dsts);
1373 free(mirror->vlans);
1375 ofproto->mirrors[mirror->idx] = NULL;
1381 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1383 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1384 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1385 ofproto->need_revalidate = true;
1386 mac_learning_flush(ofproto->ml);
1392 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1394 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1395 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1396 return bundle && bundle->mirror_out != 0;
1401 static struct ofport_dpif *
1402 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1404 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1405 return ofport ? ofport_dpif_cast(ofport) : NULL;
1408 static struct ofport_dpif *
1409 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1411 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1415 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1416 struct dpif_port *dpif_port)
1418 ofproto_port->name = dpif_port->name;
1419 ofproto_port->type = dpif_port->type;
1420 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1424 port_run(struct ofport_dpif *ofport)
1426 bool enable = netdev_get_carrier(ofport->up.netdev);
1429 cfm_run(ofport->cfm);
1431 if (cfm_should_send_ccm(ofport->cfm)) {
1432 struct ofpbuf packet;
1434 ofpbuf_init(&packet, 0);
1435 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1436 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1437 ofport->odp_port, &packet);
1438 ofpbuf_uninit(&packet);
1441 enable = enable && !cfm_get_fault(ofport->cfm);
1444 if (ofport->bundle) {
1445 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1448 if (ofport->may_enable != enable) {
1449 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1451 if (ofproto->has_bundle_action) {
1452 ofproto->need_revalidate = true;
1456 ofport->may_enable = enable;
1460 port_wait(struct ofport_dpif *ofport)
1463 cfm_wait(ofport->cfm);
1468 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1469 struct ofproto_port *ofproto_port)
1471 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1472 struct dpif_port dpif_port;
1475 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1477 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1483 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1485 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1489 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1491 *ofp_portp = odp_port_to_ofp_port(odp_port);
1497 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1499 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1502 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1504 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1506 /* The caller is going to close ofport->up.netdev. If this is a
1507 * bonded port, then the bond is using that netdev, so remove it
1508 * from the bond. The client will need to reconfigure everything
1509 * after deleting ports, so then the slave will get re-added. */
1510 bundle_remove(&ofport->up);
1516 struct port_dump_state {
1517 struct dpif_port_dump dump;
1522 port_dump_start(const struct ofproto *ofproto_, void **statep)
1524 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1525 struct port_dump_state *state;
1527 *statep = state = xmalloc(sizeof *state);
1528 dpif_port_dump_start(&state->dump, ofproto->dpif);
1529 state->done = false;
1534 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1535 struct ofproto_port *port)
1537 struct port_dump_state *state = state_;
1538 struct dpif_port dpif_port;
1540 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1541 ofproto_port_from_dpif_port(port, &dpif_port);
1544 int error = dpif_port_dump_done(&state->dump);
1546 return error ? error : EOF;
1551 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1553 struct port_dump_state *state = state_;
1556 dpif_port_dump_done(&state->dump);
1563 port_poll(const struct ofproto *ofproto_, char **devnamep)
1565 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1566 return dpif_port_poll(ofproto->dpif, devnamep);
1570 port_poll_wait(const struct ofproto *ofproto_)
1572 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1573 dpif_port_poll_wait(ofproto->dpif);
1577 port_is_lacp_current(const struct ofport *ofport_)
1579 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1580 return (ofport->bundle && ofport->bundle->lacp
1581 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1585 /* Upcall handling. */
1587 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1588 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1589 * their individual configurations.
1591 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1592 * Otherwise, ownership is transferred to this function. */
1594 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1595 const struct flow *flow, bool clone)
1597 struct ofputil_packet_in pin;
1599 pin.packet = upcall->packet;
1600 pin.in_port = flow->in_port;
1601 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1602 pin.buffer_id = 0; /* not yet known */
1603 pin.send_len = upcall->userdata;
1604 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1605 clone ? NULL : upcall->packet);
1609 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1610 const struct ofpbuf *packet)
1612 if (cfm_should_process_flow(flow)) {
1613 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1614 if (packet && ofport && ofport->cfm) {
1615 cfm_process_heartbeat(ofport->cfm, packet);
1618 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
1619 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1620 if (packet && port && port->bundle && port->bundle->lacp) {
1621 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
1623 lacp_process_pdu(port->bundle->lacp, port, pdu);
1632 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1634 struct facet *facet;
1637 /* Obtain in_port and tun_id, at least. */
1638 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1640 /* Set header pointers in 'flow'. */
1641 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1643 /* Handle 802.1ag and LACP. */
1644 if (process_special(ofproto, &flow, upcall->packet)) {
1645 ofpbuf_delete(upcall->packet);
1646 ofproto->n_matches++;
1650 /* Check with in-band control to see if this packet should be sent
1651 * to the local port regardless of the flow table. */
1652 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1653 send_packet(ofproto, ODPP_LOCAL, upcall->packet);
1656 facet = facet_lookup_valid(ofproto, &flow);
1658 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow, 0);
1660 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1661 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1663 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1664 COVERAGE_INC(ofproto_dpif_no_packet_in);
1665 /* XXX install 'drop' flow entry */
1666 ofpbuf_delete(upcall->packet);
1670 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1674 send_packet_in(ofproto, upcall, &flow, false);
1678 facet = facet_create(rule, &flow, upcall->packet);
1679 } else if (!facet->may_install) {
1680 /* The facet is not installable, that is, we need to process every
1681 * packet, so process the current packet's actions into 'facet'. */
1682 facet_make_actions(ofproto, facet, upcall->packet);
1685 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1687 * Extra-special case for fail-open mode.
1689 * We are in fail-open mode and the packet matched the fail-open rule,
1690 * but we are connected to a controller too. We should send the packet
1691 * up to the controller in the hope that it will try to set up a flow
1692 * and thereby allow us to exit fail-open.
1694 * See the top-level comment in fail-open.c for more information.
1696 send_packet_in(ofproto, upcall, &flow, true);
1699 facet_execute(ofproto, facet, upcall->packet);
1700 facet_install(ofproto, facet, false);
1701 ofproto->n_matches++;
1705 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1709 switch (upcall->type) {
1710 case DPIF_UC_ACTION:
1711 COVERAGE_INC(ofproto_dpif_ctlr_action);
1712 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1713 send_packet_in(ofproto, upcall, &flow, false);
1716 case DPIF_UC_SAMPLE:
1717 if (ofproto->sflow) {
1718 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1719 dpif_sflow_received(ofproto->sflow, upcall, &flow);
1721 ofpbuf_delete(upcall->packet);
1725 handle_miss_upcall(ofproto, upcall);
1728 case DPIF_N_UC_TYPES:
1730 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1735 /* Flow expiration. */
1737 static int facet_max_idle(const struct ofproto_dpif *);
1738 static void update_stats(struct ofproto_dpif *);
1739 static void rule_expire(struct rule_dpif *);
1740 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1742 /* This function is called periodically by run(). Its job is to collect
1743 * updates for the flows that have been installed into the datapath, most
1744 * importantly when they last were used, and then use that information to
1745 * expire flows that have not been used recently.
1747 * Returns the number of milliseconds after which it should be called again. */
1749 expire(struct ofproto_dpif *ofproto)
1751 struct rule_dpif *rule, *next_rule;
1752 struct classifier *table;
1755 /* Update stats for each flow in the datapath. */
1756 update_stats(ofproto);
1758 /* Expire facets that have been idle too long. */
1759 dp_max_idle = facet_max_idle(ofproto);
1760 expire_facets(ofproto, dp_max_idle);
1762 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1763 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1764 struct cls_cursor cursor;
1766 cls_cursor_init(&cursor, table, NULL);
1767 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1772 /* All outstanding data in existing flows has been accounted, so it's a
1773 * good time to do bond rebalancing. */
1774 if (ofproto->has_bonded_bundles) {
1775 struct ofbundle *bundle;
1777 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1779 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1784 return MIN(dp_max_idle, 1000);
1787 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1789 * This function also pushes statistics updates to rules which each facet
1790 * resubmits into. Generally these statistics will be accurate. However, if a
1791 * facet changes the rule it resubmits into at some time in between
1792 * update_stats() runs, it is possible that statistics accrued to the
1793 * old rule will be incorrectly attributed to the new rule. This could be
1794 * avoided by calling update_stats() whenever rules are created or
1795 * deleted. However, the performance impact of making so many calls to the
1796 * datapath do not justify the benefit of having perfectly accurate statistics.
1799 update_stats(struct ofproto_dpif *p)
1801 const struct dpif_flow_stats *stats;
1802 struct dpif_flow_dump dump;
1803 const struct nlattr *key;
1806 dpif_flow_dump_start(&dump, p->dpif);
1807 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1808 struct facet *facet;
1811 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1815 odp_flow_key_format(key, key_len, &s);
1816 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
1822 facet = facet_find(p, &flow);
1824 if (facet && facet->installed) {
1826 if (stats->n_packets >= facet->dp_packet_count) {
1827 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1828 facet->packet_count += extra;
1830 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1833 if (stats->n_bytes >= facet->dp_byte_count) {
1834 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1836 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1839 facet->dp_packet_count = stats->n_packets;
1840 facet->dp_byte_count = stats->n_bytes;
1842 facet_update_time(p, facet, stats->used);
1843 facet_account(p, facet, stats->n_bytes);
1844 facet_push_stats(facet);
1846 /* There's a flow in the datapath that we know nothing about.
1848 COVERAGE_INC(facet_unexpected);
1849 dpif_flow_del(p->dpif, key, key_len, NULL);
1852 dpif_flow_dump_done(&dump);
1855 /* Calculates and returns the number of milliseconds of idle time after which
1856 * facets should expire from the datapath and we should fold their statistics
1857 * into their parent rules in userspace. */
1859 facet_max_idle(const struct ofproto_dpif *ofproto)
1862 * Idle time histogram.
1864 * Most of the time a switch has a relatively small number of facets. When
1865 * this is the case we might as well keep statistics for all of them in
1866 * userspace and to cache them in the kernel datapath for performance as
1869 * As the number of facets increases, the memory required to maintain
1870 * statistics about them in userspace and in the kernel becomes
1871 * significant. However, with a large number of facets it is likely that
1872 * only a few of them are "heavy hitters" that consume a large amount of
1873 * bandwidth. At this point, only heavy hitters are worth caching in the
1874 * kernel and maintaining in userspaces; other facets we can discard.
1876 * The technique used to compute the idle time is to build a histogram with
1877 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1878 * that is installed in the kernel gets dropped in the appropriate bucket.
1879 * After the histogram has been built, we compute the cutoff so that only
1880 * the most-recently-used 1% of facets (but at least
1881 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1882 * the most-recently-used bucket of facets is kept, so actually an
1883 * arbitrary number of facets can be kept in any given expiration run
1884 * (though the next run will delete most of those unless they receive
1887 * This requires a second pass through the facets, in addition to the pass
1888 * made by update_stats(), because the former function never looks
1889 * at uninstallable facets.
1891 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1892 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1893 int buckets[N_BUCKETS] = { 0 };
1894 int total, subtotal, bucket;
1895 struct facet *facet;
1899 total = hmap_count(&ofproto->facets);
1900 if (total <= ofproto->up.flow_eviction_threshold) {
1901 return N_BUCKETS * BUCKET_WIDTH;
1904 /* Build histogram. */
1906 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1907 long long int idle = now - facet->used;
1908 int bucket = (idle <= 0 ? 0
1909 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1910 : (unsigned int) idle / BUCKET_WIDTH);
1914 /* Find the first bucket whose flows should be expired. */
1915 subtotal = bucket = 0;
1917 subtotal += buckets[bucket++];
1918 } while (bucket < N_BUCKETS &&
1919 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
1921 if (VLOG_IS_DBG_ENABLED()) {
1925 ds_put_cstr(&s, "keep");
1926 for (i = 0; i < N_BUCKETS; i++) {
1928 ds_put_cstr(&s, ", drop");
1931 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1934 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1938 return bucket * BUCKET_WIDTH;
1942 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1944 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1945 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1946 struct ofexpired expired;
1948 if (facet->installed) {
1949 struct dpif_flow_stats stats;
1951 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1953 facet_update_stats(ofproto, facet, &stats);
1956 expired.flow = facet->flow;
1957 expired.packet_count = facet->packet_count;
1958 expired.byte_count = facet->byte_count;
1959 expired.used = facet->used;
1960 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1965 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1967 long long int cutoff = time_msec() - dp_max_idle;
1968 struct facet *facet, *next_facet;
1970 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1971 facet_active_timeout(ofproto, facet);
1972 if (facet->used < cutoff) {
1973 facet_remove(ofproto, facet);
1978 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
1979 * then delete it entirely. */
1981 rule_expire(struct rule_dpif *rule)
1983 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1984 struct facet *facet, *next_facet;
1988 /* Has 'rule' expired? */
1990 if (rule->up.hard_timeout
1991 && now > rule->up.created + rule->up.hard_timeout * 1000) {
1992 reason = OFPRR_HARD_TIMEOUT;
1993 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
1994 && now > rule->used + rule->up.idle_timeout * 1000) {
1995 reason = OFPRR_IDLE_TIMEOUT;
2000 COVERAGE_INC(ofproto_dpif_expired);
2002 /* Update stats. (This is a no-op if the rule expired due to an idle
2003 * timeout, because that only happens when the rule has no facets left.) */
2004 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2005 facet_remove(ofproto, facet);
2008 /* Get rid of the rule. */
2009 ofproto_rule_expire(&rule->up, reason);
2014 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2015 * example 'packet' within that flow.
2017 * The caller must already have determined that no facet with an identical
2018 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2019 * the ofproto's classifier table. */
2020 static struct facet *
2021 facet_create(struct rule_dpif *rule, const struct flow *flow,
2022 const struct ofpbuf *packet)
2024 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2025 struct facet *facet;
2027 facet = xzalloc(sizeof *facet);
2028 facet->used = time_msec();
2029 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2030 list_push_back(&rule->facets, &facet->list_node);
2032 facet->flow = *flow;
2033 netflow_flow_init(&facet->nf_flow);
2034 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2036 facet_make_actions(ofproto, facet, packet);
2042 facet_free(struct facet *facet)
2044 free(facet->actions);
2048 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2049 * 'packet', which arrived on 'in_port'.
2051 * Takes ownership of 'packet'. */
2053 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2054 const struct nlattr *odp_actions, size_t actions_len,
2055 struct ofpbuf *packet)
2057 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2058 && odp_actions->nla_type == ODP_ACTION_ATTR_USERSPACE) {
2059 /* As an optimization, avoid a round-trip from userspace to kernel to
2060 * userspace. This also avoids possibly filling up kernel packet
2061 * buffers along the way. */
2062 struct dpif_upcall upcall;
2064 upcall.type = DPIF_UC_ACTION;
2065 upcall.packet = packet;
2068 upcall.userdata = nl_attr_get_u64(odp_actions);
2069 upcall.sample_pool = 0;
2070 upcall.actions = NULL;
2071 upcall.actions_len = 0;
2073 send_packet_in(ofproto, &upcall, flow, false);
2077 struct odputil_keybuf keybuf;
2081 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2082 odp_flow_key_from_flow(&key, flow);
2084 error = dpif_execute(ofproto->dpif, key.data, key.size,
2085 odp_actions, actions_len, packet);
2087 ofpbuf_delete(packet);
2092 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2093 * statistics appropriately. 'packet' must have at least sizeof(struct
2094 * ofp_packet_in) bytes of headroom.
2096 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2097 * applying flow_extract() to 'packet' would yield the same flow as
2100 * 'facet' must have accurately composed ODP actions; that is, it must not be
2101 * in need of revalidation.
2103 * Takes ownership of 'packet'. */
2105 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2106 struct ofpbuf *packet)
2108 struct dpif_flow_stats stats;
2110 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2112 flow_extract_stats(&facet->flow, packet, &stats);
2113 stats.used = time_msec();
2114 if (execute_odp_actions(ofproto, &facet->flow,
2115 facet->actions, facet->actions_len, packet)) {
2116 facet_update_stats(ofproto, facet, &stats);
2120 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2122 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2123 * rule's statistics, via facet_uninstall().
2125 * - Removes 'facet' from its rule and from ofproto->facets.
2128 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2130 facet_uninstall(ofproto, facet);
2131 facet_flush_stats(ofproto, facet);
2132 hmap_remove(&ofproto->facets, &facet->hmap_node);
2133 list_remove(&facet->list_node);
2137 /* Composes the ODP actions for 'facet' based on its rule's actions. */
2139 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2140 const struct ofpbuf *packet)
2142 const struct rule_dpif *rule = facet->rule;
2143 struct ofpbuf *odp_actions;
2144 struct action_xlate_ctx ctx;
2146 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2147 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2148 facet->tags = ctx.tags;
2149 facet->may_install = ctx.may_set_up_flow;
2150 facet->nf_flow.output_iface = ctx.nf_output_iface;
2152 if (facet->actions_len != odp_actions->size
2153 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2154 free(facet->actions);
2155 facet->actions_len = odp_actions->size;
2156 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2159 ofpbuf_delete(odp_actions);
2162 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2163 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2164 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2165 * since 'facet' was last updated.
2167 * Returns 0 if successful, otherwise a positive errno value.*/
2169 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2170 const struct nlattr *actions, size_t actions_len,
2171 struct dpif_flow_stats *stats)
2173 struct odputil_keybuf keybuf;
2174 enum dpif_flow_put_flags flags;
2178 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2180 flags |= DPIF_FP_ZERO_STATS;
2183 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2184 odp_flow_key_from_flow(&key, &facet->flow);
2186 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2187 actions, actions_len, stats);
2190 facet_reset_dp_stats(facet, stats);
2196 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2197 * 'zero_stats' is true, clears any existing statistics from the datapath for
2200 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2202 struct dpif_flow_stats stats;
2204 if (facet->may_install
2205 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2206 zero_stats ? &stats : NULL)) {
2207 facet->installed = true;
2212 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2214 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2218 facet_account(struct ofproto_dpif *ofproto,
2219 struct facet *facet, uint64_t extra_bytes)
2221 uint64_t total_bytes, n_bytes;
2222 struct ofbundle *in_bundle;
2223 const struct nlattr *a;
2229 total_bytes = facet->byte_count + extra_bytes;
2230 if (total_bytes <= facet->accounted_bytes) {
2233 n_bytes = total_bytes - facet->accounted_bytes;
2234 facet->accounted_bytes = total_bytes;
2236 /* Test that 'tags' is nonzero to ensure that only flows that include an
2237 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2238 * This works because OFPP_NORMAL always sets a nonzero tag value.
2240 * Feed information from the active flows back into the learning table to
2241 * ensure that table is always in sync with what is actually flowing
2242 * through the datapath. */
2244 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2245 &vlan, &in_bundle)) {
2249 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2251 if (!ofproto->has_bonded_bundles) {
2255 /* This loop feeds byte counters to bond_account() for rebalancing to use
2256 * as a basis. We also need to track the actual VLAN on which the packet
2257 * is going to be sent to ensure that it matches the one passed to
2258 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2260 vlan_tci = facet->flow.vlan_tci;
2261 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2262 struct ofport_dpif *port;
2264 switch (nl_attr_type(a)) {
2265 case ODP_ACTION_ATTR_OUTPUT:
2266 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2267 if (port && port->bundle && port->bundle->bond) {
2268 bond_account(port->bundle->bond, &facet->flow,
2269 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2273 case ODP_ACTION_ATTR_STRIP_VLAN:
2274 vlan_tci = htons(0);
2277 case ODP_ACTION_ATTR_SET_DL_TCI:
2278 vlan_tci = nl_attr_get_be16(a);
2284 /* If 'rule' is installed in the datapath, uninstalls it. */
2286 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2288 if (facet->installed) {
2289 struct odputil_keybuf keybuf;
2290 struct dpif_flow_stats stats;
2294 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2295 odp_flow_key_from_flow(&key, &facet->flow);
2297 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2298 facet_reset_dp_stats(facet, &stats);
2300 facet_update_stats(p, facet, &stats);
2302 facet->installed = false;
2304 assert(facet->dp_packet_count == 0);
2305 assert(facet->dp_byte_count == 0);
2309 /* Returns true if the only action for 'facet' is to send to the controller.
2310 * (We don't report NetFlow expiration messages for such facets because they
2311 * are just part of the control logic for the network, not real traffic). */
2313 facet_is_controller_flow(struct facet *facet)
2316 && facet->rule->up.n_actions == 1
2317 && action_outputs_to_port(&facet->rule->up.actions[0],
2318 htons(OFPP_CONTROLLER)));
2321 /* Resets 'facet''s datapath statistics counters. This should be called when
2322 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2323 * it should contain the statistics returned by dpif when 'facet' was reset in
2324 * the datapath. 'stats' will be modified to only included statistics new
2325 * since 'facet' was last updated. */
2327 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2329 if (stats && facet->dp_packet_count <= stats->n_packets
2330 && facet->dp_byte_count <= stats->n_bytes) {
2331 stats->n_packets -= facet->dp_packet_count;
2332 stats->n_bytes -= facet->dp_byte_count;
2335 facet->dp_packet_count = 0;
2336 facet->dp_byte_count = 0;
2339 /* Folds all of 'facet''s statistics into its rule. Also updates the
2340 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2341 * 'facet''s statistics in the datapath should have been zeroed and folded into
2342 * its packet and byte counts before this function is called. */
2344 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2346 assert(!facet->dp_byte_count);
2347 assert(!facet->dp_packet_count);
2349 facet_push_stats(facet);
2350 facet_account(ofproto, facet, 0);
2352 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2353 struct ofexpired expired;
2354 expired.flow = facet->flow;
2355 expired.packet_count = facet->packet_count;
2356 expired.byte_count = facet->byte_count;
2357 expired.used = facet->used;
2358 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2361 facet->rule->packet_count += facet->packet_count;
2362 facet->rule->byte_count += facet->byte_count;
2364 /* Reset counters to prevent double counting if 'facet' ever gets
2366 facet->packet_count = 0;
2367 facet->byte_count = 0;
2368 facet->rs_packet_count = 0;
2369 facet->rs_byte_count = 0;
2370 facet->accounted_bytes = 0;
2372 netflow_flow_clear(&facet->nf_flow);
2375 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2376 * Returns it if found, otherwise a null pointer.
2378 * The returned facet might need revalidation; use facet_lookup_valid()
2379 * instead if that is important. */
2380 static struct facet *
2381 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2383 struct facet *facet;
2385 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2387 if (flow_equal(flow, &facet->flow)) {
2395 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2396 * Returns it if found, otherwise a null pointer.
2398 * The returned facet is guaranteed to be valid. */
2399 static struct facet *
2400 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2402 struct facet *facet = facet_find(ofproto, flow);
2404 /* The facet we found might not be valid, since we could be in need of
2405 * revalidation. If it is not valid, don't return it. */
2407 && ofproto->need_revalidate
2408 && !facet_revalidate(ofproto, facet)) {
2409 COVERAGE_INC(facet_invalidated);
2416 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2418 * - If the rule found is different from 'facet''s current rule, moves
2419 * 'facet' to the new rule and recompiles its actions.
2421 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2422 * where it is and recompiles its actions anyway.
2424 * - If there is none, destroys 'facet'.
2426 * Returns true if 'facet' still exists, false if it has been destroyed. */
2428 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2430 struct action_xlate_ctx ctx;
2431 struct ofpbuf *odp_actions;
2432 struct rule_dpif *new_rule;
2433 bool actions_changed;
2435 COVERAGE_INC(facet_revalidate);
2437 /* Determine the new rule. */
2438 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
2440 /* No new rule, so delete the facet. */
2441 facet_remove(ofproto, facet);
2445 /* Calculate new ODP actions.
2447 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2448 * emit a NetFlow expiration and, if so, we need to have the old state
2449 * around to properly compose it. */
2450 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2451 odp_actions = xlate_actions(&ctx,
2452 new_rule->up.actions, new_rule->up.n_actions);
2453 actions_changed = (facet->actions_len != odp_actions->size
2454 || memcmp(facet->actions, odp_actions->data,
2455 facet->actions_len));
2457 /* If the ODP actions changed or the installability changed, then we need
2458 * to talk to the datapath. */
2459 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2460 if (ctx.may_set_up_flow) {
2461 struct dpif_flow_stats stats;
2463 facet_put__(ofproto, facet,
2464 odp_actions->data, odp_actions->size, &stats);
2465 facet_update_stats(ofproto, facet, &stats);
2467 facet_uninstall(ofproto, facet);
2470 /* The datapath flow is gone or has zeroed stats, so push stats out of
2471 * 'facet' into 'rule'. */
2472 facet_flush_stats(ofproto, facet);
2475 /* Update 'facet' now that we've taken care of all the old state. */
2476 facet->tags = ctx.tags;
2477 facet->nf_flow.output_iface = ctx.nf_output_iface;
2478 facet->may_install = ctx.may_set_up_flow;
2479 if (actions_changed) {
2480 free(facet->actions);
2481 facet->actions_len = odp_actions->size;
2482 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2484 if (facet->rule != new_rule) {
2485 COVERAGE_INC(facet_changed_rule);
2486 list_remove(&facet->list_node);
2487 list_push_back(&new_rule->facets, &facet->list_node);
2488 facet->rule = new_rule;
2489 facet->used = new_rule->up.created;
2490 facet->rs_used = facet->used;
2493 ofpbuf_delete(odp_actions);
2498 /* Updates 'facet''s used time. Caller is responsible for calling
2499 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2501 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2504 if (used > facet->used) {
2506 if (used > facet->rule->used) {
2507 facet->rule->used = used;
2509 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2513 /* Folds the statistics from 'stats' into the counters in 'facet'.
2515 * Because of the meaning of a facet's counters, it only makes sense to do this
2516 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2517 * packet that was sent by hand or if it represents statistics that have been
2518 * cleared out of the datapath. */
2520 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2521 const struct dpif_flow_stats *stats)
2523 if (stats->n_packets || stats->used > facet->used) {
2524 facet_update_time(ofproto, facet, stats->used);
2525 facet->packet_count += stats->n_packets;
2526 facet->byte_count += stats->n_bytes;
2527 facet_push_stats(facet);
2528 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2533 facet_push_stats(struct facet *facet)
2535 uint64_t rs_packets, rs_bytes;
2537 assert(facet->packet_count >= facet->rs_packet_count);
2538 assert(facet->byte_count >= facet->rs_byte_count);
2539 assert(facet->used >= facet->rs_used);
2541 rs_packets = facet->packet_count - facet->rs_packet_count;
2542 rs_bytes = facet->byte_count - facet->rs_byte_count;
2544 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2545 facet->rs_packet_count = facet->packet_count;
2546 facet->rs_byte_count = facet->byte_count;
2547 facet->rs_used = facet->used;
2549 flow_push_stats(facet->rule, &facet->flow,
2550 rs_packets, rs_bytes, facet->used);
2554 struct ofproto_push {
2555 struct action_xlate_ctx ctx;
2562 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2564 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2567 rule->packet_count += push->packets;
2568 rule->byte_count += push->bytes;
2569 rule->used = MAX(push->used, rule->used);
2573 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2574 * 'rule''s actions. */
2576 flow_push_stats(const struct rule_dpif *rule,
2577 struct flow *flow, uint64_t packets, uint64_t bytes,
2580 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2581 struct ofproto_push push;
2583 push.packets = packets;
2587 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2588 push.ctx.resubmit_hook = push_resubmit;
2589 ofpbuf_delete(xlate_actions(&push.ctx,
2590 rule->up.actions, rule->up.n_actions));
2595 static struct rule_dpif *
2596 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2599 return rule_dpif_cast(rule_from_cls_rule(
2600 classifier_lookup(&ofproto->up.tables[table_id],
2605 complete_operation(struct rule_dpif *rule)
2607 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2609 ofproto->need_revalidate = true;
2611 struct dpif_completion *c = xmalloc(sizeof *c);
2612 c->op = rule->up.pending;
2613 list_push_back(&ofproto->completions, &c->list_node);
2615 ofoperation_complete(rule->up.pending, 0);
2619 static struct rule *
2622 struct rule_dpif *rule = xmalloc(sizeof *rule);
2627 rule_dealloc(struct rule *rule_)
2629 struct rule_dpif *rule = rule_dpif_cast(rule_);
2634 rule_construct(struct rule *rule_)
2636 struct rule_dpif *rule = rule_dpif_cast(rule_);
2637 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2638 struct rule_dpif *victim;
2641 error = validate_actions(rule->up.actions, rule->up.n_actions,
2642 &rule->up.cr.flow, ofproto->max_ports);
2647 rule->used = rule->up.created;
2648 rule->packet_count = 0;
2649 rule->byte_count = 0;
2651 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2652 if (victim && !list_is_empty(&victim->facets)) {
2653 struct facet *facet;
2655 rule->facets = victim->facets;
2656 list_moved(&rule->facets);
2657 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2661 /* Must avoid list_moved() in this case. */
2662 list_init(&rule->facets);
2665 complete_operation(rule);
2670 rule_destruct(struct rule *rule_)
2672 struct rule_dpif *rule = rule_dpif_cast(rule_);
2673 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2674 struct facet *facet, *next_facet;
2676 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2677 facet_revalidate(ofproto, facet);
2680 complete_operation(rule);
2684 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2686 struct rule_dpif *rule = rule_dpif_cast(rule_);
2687 struct facet *facet;
2689 /* Start from historical data for 'rule' itself that are no longer tracked
2690 * in facets. This counts, for example, facets that have expired. */
2691 *packets = rule->packet_count;
2692 *bytes = rule->byte_count;
2694 /* Add any statistics that are tracked by facets. This includes
2695 * statistical data recently updated by ofproto_update_stats() as well as
2696 * stats for packets that were executed "by hand" via dpif_execute(). */
2697 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2698 *packets += facet->packet_count;
2699 *bytes += facet->byte_count;
2704 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2706 struct rule_dpif *rule = rule_dpif_cast(rule_);
2707 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2708 struct action_xlate_ctx ctx;
2709 struct ofpbuf *odp_actions;
2710 struct facet *facet;
2713 /* First look for a related facet. If we find one, account it to that. */
2714 facet = facet_lookup_valid(ofproto, flow);
2715 if (facet && facet->rule == rule) {
2716 facet_execute(ofproto, facet, packet);
2720 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2721 * create a new facet for it and use that. */
2722 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
2723 facet = facet_create(rule, flow, packet);
2724 facet_execute(ofproto, facet, packet);
2725 facet_install(ofproto, facet, true);
2729 /* We can't account anything to a facet. If we were to try, then that
2730 * facet would have a non-matching rule, busting our invariants. */
2731 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2732 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2733 size = packet->size;
2734 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2735 odp_actions->size, packet)) {
2736 rule->used = time_msec();
2737 rule->packet_count++;
2738 rule->byte_count += size;
2739 flow_push_stats(rule, flow, 1, size, rule->used);
2741 ofpbuf_delete(odp_actions);
2747 rule_modify_actions(struct rule *rule_)
2749 struct rule_dpif *rule = rule_dpif_cast(rule_);
2750 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2753 error = validate_actions(rule->up.actions, rule->up.n_actions,
2754 &rule->up.cr.flow, ofproto->max_ports);
2756 ofoperation_complete(rule->up.pending, error);
2760 complete_operation(rule);
2763 /* Sends 'packet' out of port 'odp_port' within 'p'.
2764 * Returns 0 if successful, otherwise a positive errno value. */
2766 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2767 const struct ofpbuf *packet)
2769 struct ofpbuf key, odp_actions;
2770 struct odputil_keybuf keybuf;
2774 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2775 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2776 odp_flow_key_from_flow(&key, &flow);
2778 ofpbuf_init(&odp_actions, 32);
2779 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2780 error = dpif_execute(ofproto->dpif,
2782 odp_actions.data, odp_actions.size,
2784 ofpbuf_uninit(&odp_actions);
2787 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2788 ofproto->up.name, odp_port, strerror(error));
2793 /* OpenFlow to ODP action translation. */
2795 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2796 struct action_xlate_ctx *ctx);
2797 static void xlate_normal(struct action_xlate_ctx *);
2800 commit_odp_actions(struct action_xlate_ctx *ctx)
2802 const struct flow *flow = &ctx->flow;
2803 struct flow *base = &ctx->base_flow;
2804 struct ofpbuf *odp_actions = ctx->odp_actions;
2806 if (base->tun_id != flow->tun_id) {
2807 nl_msg_put_be64(odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
2808 base->tun_id = flow->tun_id;
2811 if (base->nw_src != flow->nw_src) {
2812 nl_msg_put_be32(odp_actions, ODP_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
2813 base->nw_src = flow->nw_src;
2816 if (base->nw_dst != flow->nw_dst) {
2817 nl_msg_put_be32(odp_actions, ODP_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
2818 base->nw_dst = flow->nw_dst;
2821 if (base->nw_tos != flow->nw_tos) {
2822 nl_msg_put_u8(odp_actions, ODP_ACTION_ATTR_SET_NW_TOS, flow->nw_tos);
2823 base->nw_tos = flow->nw_tos;
2826 if (base->vlan_tci != flow->vlan_tci) {
2827 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2828 nl_msg_put_flag(odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2830 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2831 flow->vlan_tci & ~htons(VLAN_CFI));
2833 base->vlan_tci = flow->vlan_tci;
2836 if (base->tp_src != flow->tp_src) {
2837 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
2838 base->tp_src = flow->tp_src;
2841 if (base->tp_dst != flow->tp_dst) {
2842 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
2843 base->tp_dst = flow->tp_dst;
2846 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
2847 nl_msg_put_unspec(odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2848 flow->dl_src, ETH_ADDR_LEN);
2849 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2852 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2853 nl_msg_put_unspec(odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2854 flow->dl_dst, ETH_ADDR_LEN);
2855 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2858 if (ctx->base_priority != ctx->priority) {
2859 if (ctx->priority) {
2860 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_SET_PRIORITY,
2863 nl_msg_put_flag(odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2865 ctx->base_priority = ctx->priority;
2870 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2872 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2873 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2876 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2877 /* Forwarding disabled on port. */
2882 * We don't have an ofport record for this port, but it doesn't hurt to
2883 * allow forwarding to it anyhow. Maybe such a port will appear later
2884 * and we're pre-populating the flow table.
2888 commit_odp_actions(ctx);
2889 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2890 ctx->nf_output_iface = ofp_port;
2894 xlate_table_action(struct action_xlate_ctx *ctx,
2895 uint16_t in_port, uint8_t table_id)
2897 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2898 struct rule_dpif *rule;
2899 uint16_t old_in_port;
2900 uint8_t old_table_id;
2902 old_table_id = ctx->table_id;
2903 ctx->table_id = table_id;
2905 /* Look up a flow with 'in_port' as the input port. Then restore the
2906 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2907 * have surprising behavior). */
2908 old_in_port = ctx->flow.in_port;
2909 ctx->flow.in_port = in_port;
2910 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow, table_id);
2911 ctx->flow.in_port = old_in_port;
2913 if (ctx->resubmit_hook) {
2914 ctx->resubmit_hook(ctx, rule);
2919 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2923 ctx->table_id = old_table_id;
2925 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2927 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
2928 MAX_RESUBMIT_RECURSION);
2933 xlate_resubmit_table(struct action_xlate_ctx *ctx,
2934 const struct nx_action_resubmit *nar)
2939 in_port = (nar->in_port == htons(OFPP_IN_PORT)
2941 : ntohs(nar->in_port));
2942 table_id = nar->table == 255 ? ctx->table_id : nar->table;
2944 xlate_table_action(ctx, in_port, table_id);
2948 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
2950 struct ofport_dpif *ofport;
2952 commit_odp_actions(ctx);
2953 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
2954 uint16_t ofp_port = ofport->up.ofp_port;
2955 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
2956 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT,
2961 ctx->nf_output_iface = NF_OUT_FLOOD;
2965 xlate_output_action__(struct action_xlate_ctx *ctx,
2966 uint16_t port, uint16_t max_len)
2968 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2970 ctx->nf_output_iface = NF_OUT_DROP;
2974 add_output_action(ctx, ctx->flow.in_port);
2977 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
2983 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
2986 flood_packets(ctx, htonl(0));
2988 case OFPP_CONTROLLER:
2989 commit_odp_actions(ctx);
2990 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_USERSPACE, max_len);
2993 add_output_action(ctx, OFPP_LOCAL);
2998 if (port != ctx->flow.in_port) {
2999 add_output_action(ctx, port);
3004 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3005 ctx->nf_output_iface = NF_OUT_FLOOD;
3006 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3007 ctx->nf_output_iface = prev_nf_output_iface;
3008 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3009 ctx->nf_output_iface != NF_OUT_FLOOD) {
3010 ctx->nf_output_iface = NF_OUT_MULTI;
3015 xlate_output_action(struct action_xlate_ctx *ctx,
3016 const struct ofp_action_output *oao)
3018 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3022 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3023 const struct ofp_action_enqueue *oae)
3025 uint16_t ofp_port, odp_port;
3026 uint32_t ctx_priority, priority;
3029 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3032 /* Fall back to ordinary output action. */
3033 xlate_output_action__(ctx, ntohs(oae->port), 0);
3037 /* Figure out ODP output port. */
3038 ofp_port = ntohs(oae->port);
3039 if (ofp_port == OFPP_IN_PORT) {
3040 ofp_port = ctx->flow.in_port;
3042 odp_port = ofp_port_to_odp_port(ofp_port);
3044 /* Add ODP actions. */
3045 ctx_priority = ctx->priority;
3046 ctx->priority = priority;
3047 add_output_action(ctx, odp_port);
3048 ctx->priority = ctx_priority;
3050 /* Update NetFlow output port. */
3051 if (ctx->nf_output_iface == NF_OUT_DROP) {
3052 ctx->nf_output_iface = odp_port;
3053 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3054 ctx->nf_output_iface = NF_OUT_MULTI;
3059 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3060 const struct nx_action_set_queue *nasq)
3065 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3068 /* Couldn't translate queue to a priority, so ignore. A warning
3069 * has already been logged. */
3073 ctx->priority = priority;
3076 struct xlate_reg_state {
3082 xlate_autopath(struct action_xlate_ctx *ctx,
3083 const struct nx_action_autopath *naa)
3085 uint16_t ofp_port = ntohl(naa->id);
3086 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3088 if (!port || !port->bundle) {
3089 ofp_port = OFPP_NONE;
3090 } else if (port->bundle->bond) {
3091 /* Autopath does not support VLAN hashing. */
3092 struct ofport_dpif *slave = bond_choose_output_slave(
3093 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3095 ofp_port = slave->up.ofp_port;
3098 autopath_execute(naa, &ctx->flow, ofp_port);
3102 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3104 struct ofproto_dpif *ofproto = ofproto_;
3105 struct ofport_dpif *port;
3115 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3118 port = get_ofp_port(ofproto, ofp_port);
3119 return port ? port->may_enable : false;
3124 do_xlate_actions(const union ofp_action *in, size_t n_in,
3125 struct action_xlate_ctx *ctx)
3127 const struct ofport_dpif *port;
3128 const union ofp_action *ia;
3131 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3133 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3134 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3135 ? htonl(OFPPC_NO_RECV_STP)
3136 : htonl(OFPPC_NO_RECV))) {
3137 /* Drop this flow. */
3141 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3142 const struct ofp_action_dl_addr *oada;
3143 const struct nx_action_resubmit *nar;
3144 const struct nx_action_set_tunnel *nast;
3145 const struct nx_action_set_queue *nasq;
3146 const struct nx_action_multipath *nam;
3147 const struct nx_action_autopath *naa;
3148 const struct nx_action_bundle *nab;
3149 enum ofputil_action_code code;
3152 code = ofputil_decode_action_unsafe(ia);
3154 case OFPUTIL_OFPAT_OUTPUT:
3155 xlate_output_action(ctx, &ia->output);
3158 case OFPUTIL_OFPAT_SET_VLAN_VID:
3159 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3160 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3163 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3164 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3165 ctx->flow.vlan_tci |= htons(
3166 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3169 case OFPUTIL_OFPAT_STRIP_VLAN:
3170 ctx->flow.vlan_tci = htons(0);
3173 case OFPUTIL_OFPAT_SET_DL_SRC:
3174 oada = ((struct ofp_action_dl_addr *) ia);
3175 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3178 case OFPUTIL_OFPAT_SET_DL_DST:
3179 oada = ((struct ofp_action_dl_addr *) ia);
3180 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3183 case OFPUTIL_OFPAT_SET_NW_SRC:
3184 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3187 case OFPUTIL_OFPAT_SET_NW_DST:
3188 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3191 case OFPUTIL_OFPAT_SET_NW_TOS:
3192 ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK;
3195 case OFPUTIL_OFPAT_SET_TP_SRC:
3196 ctx->flow.tp_src = ia->tp_port.tp_port;
3199 case OFPUTIL_OFPAT_SET_TP_DST:
3200 ctx->flow.tp_dst = ia->tp_port.tp_port;
3203 case OFPUTIL_OFPAT_ENQUEUE:
3204 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3207 case OFPUTIL_NXAST_RESUBMIT:
3208 nar = (const struct nx_action_resubmit *) ia;
3209 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
3212 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3213 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
3216 case OFPUTIL_NXAST_SET_TUNNEL:
3217 nast = (const struct nx_action_set_tunnel *) ia;
3218 tun_id = htonll(ntohl(nast->tun_id));
3219 ctx->flow.tun_id = tun_id;
3222 case OFPUTIL_NXAST_SET_QUEUE:
3223 nasq = (const struct nx_action_set_queue *) ia;
3224 xlate_set_queue_action(ctx, nasq);
3227 case OFPUTIL_NXAST_POP_QUEUE:
3231 case OFPUTIL_NXAST_REG_MOVE:
3232 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3236 case OFPUTIL_NXAST_REG_LOAD:
3237 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3241 case OFPUTIL_NXAST_NOTE:
3242 /* Nothing to do. */
3245 case OFPUTIL_NXAST_SET_TUNNEL64:
3246 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3247 ctx->flow.tun_id = tun_id;
3250 case OFPUTIL_NXAST_MULTIPATH:
3251 nam = (const struct nx_action_multipath *) ia;
3252 multipath_execute(nam, &ctx->flow);
3255 case OFPUTIL_NXAST_AUTOPATH:
3256 naa = (const struct nx_action_autopath *) ia;
3257 xlate_autopath(ctx, naa);
3260 case OFPUTIL_NXAST_BUNDLE:
3261 ctx->ofproto->has_bundle_action = true;
3262 nab = (const struct nx_action_bundle *) ia;
3263 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3268 case OFPUTIL_NXAST_BUNDLE_LOAD:
3269 ctx->ofproto->has_bundle_action = true;
3270 nab = (const struct nx_action_bundle *) ia;
3271 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3279 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3280 struct ofproto_dpif *ofproto, const struct flow *flow,
3281 const struct ofpbuf *packet)
3283 ctx->ofproto = ofproto;
3285 ctx->packet = packet;
3286 ctx->resubmit_hook = NULL;
3289 static struct ofpbuf *
3290 xlate_actions(struct action_xlate_ctx *ctx,
3291 const union ofp_action *in, size_t n_in)
3293 COVERAGE_INC(ofproto_dpif_xlate);
3295 ctx->odp_actions = ofpbuf_new(512);
3297 ctx->may_set_up_flow = true;
3298 ctx->nf_output_iface = NF_OUT_DROP;
3301 ctx->base_priority = 0;
3302 ctx->base_flow = ctx->flow;
3305 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3306 ctx->may_set_up_flow = false;
3308 do_xlate_actions(in, n_in, ctx);
3311 /* Check with in-band control to see if we're allowed to set up this
3313 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3314 ctx->odp_actions->data,
3315 ctx->odp_actions->size)) {
3316 ctx->may_set_up_flow = false;
3319 return ctx->odp_actions;
3322 /* OFPP_NORMAL implementation. */
3325 struct ofport_dpif *port;
3330 struct dst builtin[32];
3332 size_t n, allocated;
3335 static void dst_set_init(struct dst_set *);
3336 static void dst_set_add(struct dst_set *, const struct dst *);
3337 static void dst_set_free(struct dst_set *);
3339 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3342 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3343 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3345 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3346 : in_bundle->vlan >= 0 ? in_bundle->vlan
3347 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3348 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3350 dst->port = (!out_bundle->bond
3351 ? ofbundle_get_a_port(out_bundle)
3352 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3353 dst->vlan, &ctx->tags));
3355 return dst->port != NULL;
3359 mirror_mask_ffs(mirror_mask_t mask)
3361 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3366 dst_set_init(struct dst_set *set)
3368 set->dsts = set->builtin;
3370 set->allocated = ARRAY_SIZE(set->builtin);
3374 dst_set_add(struct dst_set *set, const struct dst *dst)
3376 if (set->n >= set->allocated) {
3377 size_t new_allocated;
3378 struct dst *new_dsts;
3380 new_allocated = set->allocated * 2;
3381 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3382 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3386 set->dsts = new_dsts;
3387 set->allocated = new_allocated;
3389 set->dsts[set->n++] = *dst;
3393 dst_set_free(struct dst_set *set)
3395 if (set->dsts != set->builtin) {
3401 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3404 for (i = 0; i < set->n; i++) {
3405 if (set->dsts[i].vlan == test->vlan
3406 && set->dsts[i].port == test->port) {
3414 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3416 return (bundle->vlan < 0
3417 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
3421 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3423 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3426 /* Returns an arbitrary interface within 'bundle'. */
3427 static struct ofport_dpif *
3428 ofbundle_get_a_port(const struct ofbundle *bundle)
3430 return CONTAINER_OF(list_front(&bundle->ports),
3431 struct ofport_dpif, bundle_node);
3435 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3436 const struct ofbundle *in_bundle,
3437 const struct ofbundle *out_bundle, struct dst_set *set)
3441 if (out_bundle == OFBUNDLE_FLOOD) {
3442 struct ofbundle *bundle;
3444 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3445 if (bundle != in_bundle
3446 && ofbundle_includes_vlan(bundle, vlan)
3447 && bundle->floodable
3448 && !bundle->mirror_out
3449 && set_dst(ctx, &dst, in_bundle, bundle)) {
3450 dst_set_add(set, &dst);
3453 ctx->nf_output_iface = NF_OUT_FLOOD;
3454 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3455 dst_set_add(set, &dst);
3456 ctx->nf_output_iface = dst.port->odp_port;
3461 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3463 return !m->vlans || bitmap_is_set(m->vlans, vlan);
3466 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3467 * to a VLAN. In general most packets may be mirrored but we want to drop
3468 * protocols that may confuse switches. */
3470 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
3472 /* If you change this function's behavior, please update corresponding
3473 * documentation in vswitch.xml at the same time. */
3474 if (dst[0] != 0x01) {
3475 /* All the currently banned MACs happen to start with 01 currently, so
3476 * this is a quick way to eliminate most of the good ones. */
3478 if (eth_addr_is_reserved(dst)) {
3479 /* Drop STP, IEEE pause frames, and other reserved protocols
3480 * (01-80-c2-00-00-0x). */
3484 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
3486 if ((dst[3] & 0xfe) == 0xcc &&
3487 (dst[4] & 0xfe) == 0xcc &&
3488 (dst[5] & 0xfe) == 0xcc) {
3489 /* Drop the following protocols plus others following the same
3492 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3493 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3494 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3498 if (!(dst[3] | dst[4] | dst[5])) {
3499 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3508 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3509 uint16_t vlan, const struct ofbundle *in_bundle,
3510 struct dst_set *set)
3512 struct ofproto_dpif *ofproto = ctx->ofproto;
3513 mirror_mask_t mirrors;
3517 mirrors = in_bundle->src_mirrors;
3518 for (i = 0; i < set->n; i++) {
3519 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3526 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3527 if (flow_vlan == 0) {
3528 flow_vlan = OFP_VLAN_NONE;
3532 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3533 if (vlan_is_mirrored(m, vlan)) {
3537 if (set_dst(ctx, &dst, in_bundle, m->out)
3538 && !dst_is_duplicate(set, &dst)) {
3539 dst_set_add(set, &dst);
3541 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
3542 struct ofbundle *bundle;
3544 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3545 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3546 && set_dst(ctx, &dst, in_bundle, bundle))
3548 if (bundle->vlan < 0) {
3549 dst.vlan = m->out_vlan;
3551 if (dst_is_duplicate(set, &dst)) {
3555 /* Use the vlan tag on the original flow instead of
3556 * the one passed in the vlan parameter. This ensures
3557 * that we compare the vlan from before any implicit
3558 * tagging tags place. This is necessary because
3559 * dst->vlan is the final vlan, after removing implicit
3561 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3562 /* Don't send out input port on same VLAN. */
3565 dst_set_add(set, &dst);
3570 mirrors &= mirrors - 1;
3575 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3576 const struct ofbundle *in_bundle,
3577 const struct ofbundle *out_bundle)
3579 uint16_t initial_vlan, cur_vlan;
3580 const struct dst *dst;
3584 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3585 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3587 /* Output all the packets we can without having to change the VLAN. */
3588 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3589 if (initial_vlan == 0) {
3590 initial_vlan = OFP_VLAN_NONE;
3592 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3593 if (dst->vlan != initial_vlan) {
3596 nl_msg_put_u32(ctx->odp_actions,
3597 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3600 /* Then output the rest. */
3601 cur_vlan = initial_vlan;
3602 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3603 if (dst->vlan == initial_vlan) {
3606 if (dst->vlan != cur_vlan) {
3607 if (dst->vlan == OFP_VLAN_NONE) {
3608 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3611 tci = htons(dst->vlan & VLAN_VID_MASK);
3612 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3613 nl_msg_put_be16(ctx->odp_actions,
3614 ODP_ACTION_ATTR_SET_DL_TCI, tci);
3616 cur_vlan = dst->vlan;
3618 nl_msg_put_u32(ctx->odp_actions,
3619 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3625 /* Returns the effective vlan of a packet, taking into account both the
3626 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3627 * the packet is untagged and -1 indicates it has an invalid header and
3628 * should be dropped. */
3630 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3631 struct ofbundle *in_bundle, bool have_packet)
3633 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3634 if (in_bundle->vlan >= 0) {
3637 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3638 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3639 "packet received on port %s configured with "
3640 "implicit VLAN %"PRIu16,
3641 ofproto->up.name, vlan,
3642 in_bundle->name, in_bundle->vlan);
3646 vlan = in_bundle->vlan;
3648 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3650 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3651 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3652 "packet received on port %s not configured for "
3654 ofproto->up.name, vlan, in_bundle->name, vlan);
3663 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3664 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3665 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3667 is_gratuitous_arp(const struct flow *flow)
3669 return (flow->dl_type == htons(ETH_TYPE_ARP)
3670 && eth_addr_is_broadcast(flow->dl_dst)
3671 && (flow->nw_proto == ARP_OP_REPLY
3672 || (flow->nw_proto == ARP_OP_REQUEST
3673 && flow->nw_src == flow->nw_dst)));
3677 update_learning_table(struct ofproto_dpif *ofproto,
3678 const struct flow *flow, int vlan,
3679 struct ofbundle *in_bundle)
3681 struct mac_entry *mac;
3683 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3687 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3688 if (is_gratuitous_arp(flow)) {
3689 /* We don't want to learn from gratuitous ARP packets that are
3690 * reflected back over bond slaves so we lock the learning table. */
3691 if (!in_bundle->bond) {
3692 mac_entry_set_grat_arp_lock(mac);
3693 } else if (mac_entry_is_grat_arp_locked(mac)) {
3698 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3699 /* The log messages here could actually be useful in debugging,
3700 * so keep the rate limit relatively high. */
3701 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3702 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3703 "on port %s in VLAN %d",
3704 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3705 in_bundle->name, vlan);
3707 mac->port.p = in_bundle;
3708 tag_set_add(&ofproto->revalidate_set,
3709 mac_learning_changed(ofproto->ml, mac));
3713 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3714 * dropped. Returns true if they may be forwarded, false if they should be
3717 * If 'have_packet' is true, it indicates that the caller is processing a
3718 * received packet. If 'have_packet' is false, then the caller is just
3719 * revalidating an existing flow because configuration has changed. Either
3720 * way, 'have_packet' only affects logging (there is no point in logging errors
3721 * during revalidation).
3723 * Sets '*in_portp' to the input port. This will be a null pointer if
3724 * flow->in_port does not designate a known input port (in which case
3725 * is_admissible() returns false).
3727 * When returning true, sets '*vlanp' to the effective VLAN of the input
3728 * packet, as returned by flow_get_vlan().
3730 * May also add tags to '*tags', although the current implementation only does
3731 * so in one special case.
3734 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3736 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3738 struct ofport_dpif *in_port;
3739 struct ofbundle *in_bundle;
3742 /* Find the port and bundle for the received packet. */
3743 in_port = get_ofp_port(ofproto, flow->in_port);
3744 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3745 if (!in_port || !in_bundle) {
3746 /* No interface? Something fishy... */
3748 /* Odd. A few possible reasons here:
3750 * - We deleted a port but there are still a few packets queued up
3753 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3754 * we don't know about.
3756 * - Packet arrived on the local port but the local port is not
3759 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3761 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3763 ofproto->up.name, flow->in_port);
3767 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3772 /* Drop frames for reserved multicast addresses. */
3773 if (eth_addr_is_reserved(flow->dl_dst)) {
3777 /* Drop frames on bundles reserved for mirroring. */
3778 if (in_bundle->mirror_out) {
3780 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3781 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3782 "%s, which is reserved exclusively for mirroring",
3783 ofproto->up.name, in_bundle->name);
3788 if (in_bundle->bond) {
3789 struct mac_entry *mac;
3791 switch (bond_check_admissibility(in_bundle->bond, in_port,
3792 flow->dl_dst, tags)) {
3799 case BV_DROP_IF_MOVED:
3800 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3801 if (mac && mac->port.p != in_bundle &&
3802 (!is_gratuitous_arp(flow)
3803 || mac_entry_is_grat_arp_locked(mac))) {
3814 xlate_normal(struct action_xlate_ctx *ctx)
3816 struct ofbundle *in_bundle;
3817 struct ofbundle *out_bundle;
3818 struct mac_entry *mac;
3821 /* Check whether we should drop packets in this flow. */
3822 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3823 &ctx->tags, &vlan, &in_bundle)) {
3828 /* Learn source MAC (but don't try to learn from revalidation). */
3830 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3833 /* Determine output bundle. */
3834 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3837 out_bundle = mac->port.p;
3838 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3839 /* If we are revalidating but don't have a learning entry then eject
3840 * the flow. Installing a flow that floods packets opens up a window
3841 * of time where we could learn from a packet reflected on a bond and
3842 * blackhole packets before the learning table is updated to reflect
3843 * the correct port. */
3844 ctx->may_set_up_flow = false;
3847 out_bundle = OFBUNDLE_FLOOD;
3850 /* Don't send packets out their input bundles. */
3851 if (in_bundle == out_bundle) {
3857 compose_actions(ctx, vlan, in_bundle, out_bundle);
3862 get_drop_frags(struct ofproto *ofproto_)
3864 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3867 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3872 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3874 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3876 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3880 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3881 const struct flow *flow,
3882 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3884 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3887 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3888 ofproto->max_ports);
3890 struct odputil_keybuf keybuf;
3891 struct action_xlate_ctx ctx;
3892 struct ofpbuf *odp_actions;
3895 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3896 odp_flow_key_from_flow(&key, flow);
3898 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3899 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3900 dpif_execute(ofproto->dpif, key.data, key.size,
3901 odp_actions->data, odp_actions->size, packet);
3902 ofpbuf_delete(odp_actions);
3908 get_netflow_ids(const struct ofproto *ofproto_,
3909 uint8_t *engine_type, uint8_t *engine_id)
3911 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3913 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3916 static struct ofproto_dpif *
3917 ofproto_dpif_lookup(const char *name)
3919 struct ofproto *ofproto = ofproto_lookup(name);
3920 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3921 ? ofproto_dpif_cast(ofproto)
3926 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3927 const char *args, void *aux OVS_UNUSED)
3929 struct ds ds = DS_EMPTY_INITIALIZER;
3930 const struct ofproto_dpif *ofproto;
3931 const struct mac_entry *e;
3933 ofproto = ofproto_dpif_lookup(args);
3935 unixctl_command_reply(conn, 501, "no such bridge");
3939 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3940 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3941 struct ofbundle *bundle = e->port.p;
3942 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
3943 ofbundle_get_a_port(bundle)->odp_port,
3944 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3946 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3950 struct ofproto_trace {
3951 struct action_xlate_ctx ctx;
3957 trace_format_rule(struct ds *result, uint8_t table_id, int level,
3958 const struct rule_dpif *rule)
3960 ds_put_char_multiple(result, '\t', level);
3962 ds_put_cstr(result, "No match\n");
3966 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3967 table_id, ntohll(rule->up.flow_cookie));
3968 cls_rule_format(&rule->up.cr, result);
3969 ds_put_char(result, '\n');
3971 ds_put_char_multiple(result, '\t', level);
3972 ds_put_cstr(result, "OpenFlow ");
3973 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
3974 ds_put_char(result, '\n');
3978 trace_format_flow(struct ds *result, int level, const char *title,
3979 struct ofproto_trace *trace)
3981 ds_put_char_multiple(result, '\t', level);
3982 ds_put_format(result, "%s: ", title);
3983 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
3984 ds_put_cstr(result, "unchanged");
3986 flow_format(result, &trace->ctx.flow);
3987 trace->flow = trace->ctx.flow;
3989 ds_put_char(result, '\n');
3993 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3995 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
3996 struct ds *result = trace->result;
3998 ds_put_char(result, '\n');
3999 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4000 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
4004 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4005 void *aux OVS_UNUSED)
4007 char *dpname, *arg1, *arg2, *arg3;
4008 char *args = xstrdup(args_);
4009 char *save_ptr = NULL;
4010 struct ofproto_dpif *ofproto;
4011 struct ofpbuf odp_key;
4012 struct ofpbuf *packet;
4013 struct rule_dpif *rule;
4019 ofpbuf_init(&odp_key, 0);
4022 dpname = strtok_r(args, " ", &save_ptr);
4023 arg1 = strtok_r(NULL, " ", &save_ptr);
4024 arg2 = strtok_r(NULL, " ", &save_ptr);
4025 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4026 if (dpname && arg1 && !arg2 && !arg3) {
4027 /* ofproto/trace dpname flow */
4030 /* Convert string to ODP key. */
4031 ofpbuf_init(&odp_key, 0);
4032 error = odp_flow_key_from_string(arg1, &odp_key);
4034 unixctl_command_reply(conn, 501, "Bad flow syntax");
4038 /* Convert odp_key to flow. */
4039 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
4041 unixctl_command_reply(conn, 501, "Invalid flow");
4044 } else if (dpname && arg1 && arg2 && arg3) {
4045 /* ofproto/trace dpname tun_id in_port packet */
4049 tun_id = htonll(strtoull(arg1, NULL, 0));
4050 in_port = ofp_port_to_odp_port(atoi(arg2));
4052 packet = ofpbuf_new(strlen(args) / 2);
4053 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
4054 arg3 += strspn(arg3, " ");
4055 if (*arg3 != '\0') {
4056 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4059 if (packet->size < ETH_HEADER_LEN) {
4060 unixctl_command_reply(conn, 501,
4061 "Packet data too short for Ethernet");
4065 ds_put_cstr(&result, "Packet: ");
4066 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
4067 ds_put_cstr(&result, s);
4070 flow_extract(packet, tun_id, in_port, &flow);
4072 unixctl_command_reply(conn, 501, "Bad command syntax");
4076 ofproto = ofproto_dpif_lookup(dpname);
4078 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4083 ds_put_cstr(&result, "Flow: ");
4084 flow_format(&result, &flow);
4085 ds_put_char(&result, '\n');
4087 rule = rule_dpif_lookup(ofproto, &flow, 0);
4088 trace_format_rule(&result, 0, 0, rule);
4090 struct ofproto_trace trace;
4091 struct ofpbuf *odp_actions;
4093 trace.result = &result;
4095 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
4096 trace.ctx.resubmit_hook = trace_resubmit;
4097 odp_actions = xlate_actions(&trace.ctx,
4098 rule->up.actions, rule->up.n_actions);
4100 ds_put_char(&result, '\n');
4101 trace_format_flow(&result, 0, "Final flow", &trace);
4102 ds_put_cstr(&result, "Datapath actions: ");
4103 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4104 ofpbuf_delete(odp_actions);
4106 if (!trace.ctx.may_set_up_flow) {
4108 ds_put_cstr(&result, "\nThis flow is not cachable.");
4110 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
4111 "for complete actions, please supply a packet.");
4116 unixctl_command_reply(conn, 200, ds_cstr(&result));
4119 ds_destroy(&result);
4120 ofpbuf_delete(packet);
4121 ofpbuf_uninit(&odp_key);
4126 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
4127 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4130 unixctl_command_reply(conn, 200, NULL);
4134 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
4135 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4138 unixctl_command_reply(conn, 200, NULL);
4142 ofproto_dpif_unixctl_init(void)
4144 static bool registered;
4150 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4151 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
4153 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
4154 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
4157 const struct ofproto_class ofproto_dpif_class = {
4184 port_is_lacp_current,
4185 NULL, /* rule_choose_table */
4192 rule_modify_actions,
4205 is_mirror_output_bundle,