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 its statistics were reset (e.g. dpif_flow_put() with
211 * DPIF_FP_ZERO_STATS).
213 uint64_t packet_count; /* Number of packets received. */
214 uint64_t byte_count; /* Number of bytes received. */
216 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
217 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
219 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
220 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
221 long long int rs_used; /* Used time pushed to resubmit children. */
223 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
225 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
226 struct list list_node; /* In owning rule's 'facets' list. */
227 struct rule_dpif *rule; /* Owning rule. */
228 struct flow flow; /* Exact-match flow. */
229 bool installed; /* Installed in datapath? */
230 bool may_install; /* True ordinarily; false if actions must
231 * be reassessed for every packet. */
232 size_t actions_len; /* Number of bytes in actions[]. */
233 struct nlattr *actions; /* Datapath actions. */
234 tag_type tags; /* Tags. */
235 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
238 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
239 const struct ofpbuf *packet);
240 static void facet_remove(struct ofproto_dpif *, struct facet *);
241 static void facet_free(struct facet *);
243 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
244 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
245 const struct flow *);
246 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
248 static void facet_execute(struct ofproto_dpif *, struct facet *,
249 struct ofpbuf *packet);
251 static int facet_put__(struct ofproto_dpif *, struct facet *,
252 const struct nlattr *actions, size_t actions_len,
253 struct dpif_flow_stats *);
254 static void facet_install(struct ofproto_dpif *, struct facet *,
256 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
257 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
259 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
260 const struct ofpbuf *packet);
261 static void facet_update_time(struct ofproto_dpif *, struct facet *,
263 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
264 const struct dpif_flow_stats *);
265 static void facet_reset_counters(struct facet *);
266 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
267 static void facet_push_stats(struct facet *);
268 static void facet_account(struct ofproto_dpif *, struct facet *);
270 static bool facet_is_controller_flow(struct facet *);
272 static void flow_push_stats(const struct rule_dpif *,
273 struct flow *, uint64_t packets, uint64_t bytes,
280 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
281 struct list bundle_node; /* In struct ofbundle's "ports" list. */
282 struct cfm *cfm; /* Connectivity Fault Management, if any. */
283 tag_type tag; /* Tag associated with this port. */
284 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
285 bool may_enable; /* May be enabled in bonds. */
288 static struct ofport_dpif *
289 ofport_dpif_cast(const struct ofport *ofport)
291 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
292 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
295 static void port_run(struct ofport_dpif *);
296 static void port_wait(struct ofport_dpif *);
297 static int set_cfm(struct ofport *, const struct cfm_settings *);
299 struct dpif_completion {
300 struct list list_node;
301 struct ofoperation *op;
304 struct ofproto_dpif {
313 struct netflow *netflow;
314 struct dpif_sflow *sflow;
315 struct hmap bundles; /* Contains "struct ofbundle"s. */
316 struct mac_learning *ml;
317 struct ofmirror *mirrors[MAX_MIRRORS];
318 bool has_bonded_bundles;
321 struct timer next_expiration;
325 bool need_revalidate;
326 struct tag_set revalidate_set;
328 /* Support for debugging async flow mods. */
329 struct list completions;
331 bool has_bundle_action; /* True when the first bundle action appears. */
334 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
335 * for debugging the asynchronous flow_mod implementation.) */
338 static void ofproto_dpif_unixctl_init(void);
340 static struct ofproto_dpif *
341 ofproto_dpif_cast(const struct ofproto *ofproto)
343 assert(ofproto->ofproto_class == &ofproto_dpif_class);
344 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
347 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
349 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
352 /* Packet processing. */
353 static void update_learning_table(struct ofproto_dpif *,
354 const struct flow *, int vlan,
356 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
357 bool have_packet, tag_type *, int *vlanp,
358 struct ofbundle **in_bundlep);
359 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
361 /* Flow expiration. */
362 static int expire(struct ofproto_dpif *);
365 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
366 const struct ofpbuf *packet);
368 /* Global variables. */
369 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
371 /* Factory functions. */
374 enumerate_types(struct sset *types)
376 dp_enumerate_types(types);
380 enumerate_names(const char *type, struct sset *names)
382 return dp_enumerate_names(type, names);
386 del(const char *type, const char *name)
391 error = dpif_open(name, type, &dpif);
393 error = dpif_delete(dpif);
399 /* Basic life-cycle. */
401 static struct ofproto *
404 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
409 dealloc(struct ofproto *ofproto_)
411 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
416 construct(struct ofproto *ofproto_, int *n_tablesp)
418 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
419 const char *name = ofproto->up.name;
423 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
425 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
429 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
430 ofproto->n_matches = 0;
432 error = dpif_recv_set_mask(ofproto->dpif,
433 ((1u << DPIF_UC_MISS) |
434 (1u << DPIF_UC_ACTION) |
435 (1u << DPIF_UC_SAMPLE)));
437 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
438 dpif_close(ofproto->dpif);
441 dpif_flow_flush(ofproto->dpif);
442 dpif_recv_purge(ofproto->dpif);
444 ofproto->netflow = NULL;
445 ofproto->sflow = NULL;
446 hmap_init(&ofproto->bundles);
447 ofproto->ml = mac_learning_create();
448 for (i = 0; i < MAX_MIRRORS; i++) {
449 ofproto->mirrors[i] = NULL;
451 ofproto->has_bonded_bundles = false;
453 timer_set_duration(&ofproto->next_expiration, 1000);
455 hmap_init(&ofproto->facets);
456 ofproto->need_revalidate = false;
457 tag_set_init(&ofproto->revalidate_set);
459 list_init(&ofproto->completions);
461 ofproto_dpif_unixctl_init();
463 ofproto->has_bundle_action = false;
470 complete_operations(struct ofproto_dpif *ofproto)
472 struct dpif_completion *c, *next;
474 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
475 ofoperation_complete(c->op, 0);
476 list_remove(&c->list_node);
482 destruct(struct ofproto *ofproto_)
484 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
485 struct rule_dpif *rule, *next_rule;
486 struct classifier *table;
489 complete_operations(ofproto);
491 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
492 struct cls_cursor cursor;
494 cls_cursor_init(&cursor, table, NULL);
495 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
496 ofproto_rule_destroy(&rule->up);
500 for (i = 0; i < MAX_MIRRORS; i++) {
501 mirror_destroy(ofproto->mirrors[i]);
504 netflow_destroy(ofproto->netflow);
505 dpif_sflow_destroy(ofproto->sflow);
506 hmap_destroy(&ofproto->bundles);
507 mac_learning_destroy(ofproto->ml);
509 hmap_destroy(&ofproto->facets);
511 dpif_close(ofproto->dpif);
515 run(struct ofproto *ofproto_)
517 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
518 struct ofport_dpif *ofport;
519 struct ofbundle *bundle;
523 complete_operations(ofproto);
525 dpif_run(ofproto->dpif);
527 for (i = 0; i < 50; i++) {
528 struct dpif_upcall packet;
531 error = dpif_recv(ofproto->dpif, &packet);
533 if (error == ENODEV) {
534 /* Datapath destroyed. */
540 handle_upcall(ofproto, &packet);
543 if (timer_expired(&ofproto->next_expiration)) {
544 int delay = expire(ofproto);
545 timer_set_duration(&ofproto->next_expiration, delay);
548 if (ofproto->netflow) {
549 netflow_run(ofproto->netflow);
551 if (ofproto->sflow) {
552 dpif_sflow_run(ofproto->sflow);
555 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
558 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
562 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
564 /* Now revalidate if there's anything to do. */
565 if (ofproto->need_revalidate
566 || !tag_set_is_empty(&ofproto->revalidate_set)) {
567 struct tag_set revalidate_set = ofproto->revalidate_set;
568 bool revalidate_all = ofproto->need_revalidate;
569 struct facet *facet, *next;
571 /* Clear the revalidation flags. */
572 tag_set_init(&ofproto->revalidate_set);
573 ofproto->need_revalidate = false;
575 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
577 || tag_set_intersects(&revalidate_set, facet->tags)) {
578 facet_revalidate(ofproto, facet);
587 wait(struct ofproto *ofproto_)
589 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
590 struct ofport_dpif *ofport;
591 struct ofbundle *bundle;
593 if (!clogged && !list_is_empty(&ofproto->completions)) {
594 poll_immediate_wake();
597 dpif_wait(ofproto->dpif);
598 dpif_recv_wait(ofproto->dpif);
599 if (ofproto->sflow) {
600 dpif_sflow_wait(ofproto->sflow);
602 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
603 poll_immediate_wake();
605 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
608 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
611 mac_learning_wait(ofproto->ml);
612 if (ofproto->need_revalidate) {
613 /* Shouldn't happen, but if it does just go around again. */
614 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
615 poll_immediate_wake();
617 timer_wait(&ofproto->next_expiration);
622 flush(struct ofproto *ofproto_)
624 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
625 struct facet *facet, *next_facet;
627 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
628 /* Mark the facet as not installed so that facet_remove() doesn't
629 * bother trying to uninstall it. There is no point in uninstalling it
630 * individually since we are about to blow away all the facets with
631 * dpif_flow_flush(). */
632 facet->installed = false;
633 facet->dp_packet_count = 0;
634 facet->dp_byte_count = 0;
635 facet_remove(ofproto, facet);
637 dpif_flow_flush(ofproto->dpif);
641 get_features(struct ofproto *ofproto_ OVS_UNUSED,
642 bool *arp_match_ip, uint32_t *actions)
644 *arp_match_ip = true;
645 *actions = ((1u << OFPAT_OUTPUT) |
646 (1u << OFPAT_SET_VLAN_VID) |
647 (1u << OFPAT_SET_VLAN_PCP) |
648 (1u << OFPAT_STRIP_VLAN) |
649 (1u << OFPAT_SET_DL_SRC) |
650 (1u << OFPAT_SET_DL_DST) |
651 (1u << OFPAT_SET_NW_SRC) |
652 (1u << OFPAT_SET_NW_DST) |
653 (1u << OFPAT_SET_NW_TOS) |
654 (1u << OFPAT_SET_TP_SRC) |
655 (1u << OFPAT_SET_TP_DST) |
656 (1u << OFPAT_ENQUEUE));
660 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
662 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
663 struct ovs_dp_stats s;
665 strcpy(ots->name, "classifier");
667 dpif_get_dp_stats(ofproto->dpif, &s);
668 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
669 put_32aligned_be64(&ots->matched_count,
670 htonll(s.n_hit + ofproto->n_matches));
674 set_netflow(struct ofproto *ofproto_,
675 const struct netflow_options *netflow_options)
677 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
679 if (netflow_options) {
680 if (!ofproto->netflow) {
681 ofproto->netflow = netflow_create();
683 return netflow_set_options(ofproto->netflow, netflow_options);
685 netflow_destroy(ofproto->netflow);
686 ofproto->netflow = NULL;
691 static struct ofport *
694 struct ofport_dpif *port = xmalloc(sizeof *port);
699 port_dealloc(struct ofport *port_)
701 struct ofport_dpif *port = ofport_dpif_cast(port_);
706 port_construct(struct ofport *port_)
708 struct ofport_dpif *port = ofport_dpif_cast(port_);
709 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
711 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
714 port->tag = tag_create_random();
715 port->may_enable = true;
717 if (ofproto->sflow) {
718 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
719 netdev_get_name(port->up.netdev));
726 port_destruct(struct ofport *port_)
728 struct ofport_dpif *port = ofport_dpif_cast(port_);
729 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
731 bundle_remove(port_);
732 set_cfm(port_, NULL);
733 if (ofproto->sflow) {
734 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
739 port_modified(struct ofport *port_)
741 struct ofport_dpif *port = ofport_dpif_cast(port_);
743 if (port->bundle && port->bundle->bond) {
744 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
749 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
751 struct ofport_dpif *port = ofport_dpif_cast(port_);
752 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
753 ovs_be32 changed = old_config ^ port->up.opp.config;
755 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
756 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
757 ofproto->need_revalidate = true;
762 set_sflow(struct ofproto *ofproto_,
763 const struct ofproto_sflow_options *sflow_options)
765 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
766 struct dpif_sflow *ds = ofproto->sflow;
769 struct ofport_dpif *ofport;
771 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
772 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
773 dpif_sflow_add_port(ds, ofport->odp_port,
774 netdev_get_name(ofport->up.netdev));
777 dpif_sflow_set_options(ds, sflow_options);
779 dpif_sflow_destroy(ds);
780 ofproto->sflow = NULL;
786 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
788 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
795 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
798 if (cfm_configure(ofport->cfm, s)) {
804 cfm_destroy(ofport->cfm);
810 get_cfm_fault(const struct ofport *ofport_)
812 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
814 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
819 /* Expires all MAC learning entries associated with 'port' and forces ofproto
820 * to revalidate every flow. */
822 bundle_flush_macs(struct ofbundle *bundle)
824 struct ofproto_dpif *ofproto = bundle->ofproto;
825 struct mac_learning *ml = ofproto->ml;
826 struct mac_entry *mac, *next_mac;
828 ofproto->need_revalidate = true;
829 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
830 if (mac->port.p == bundle) {
831 mac_learning_expire(ml, mac);
836 static struct ofbundle *
837 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
839 struct ofbundle *bundle;
841 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
843 if (bundle->aux == aux) {
850 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
851 * ones that are found to 'bundles'. */
853 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
854 void **auxes, size_t n_auxes,
855 struct hmapx *bundles)
860 for (i = 0; i < n_auxes; i++) {
861 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
863 hmapx_add(bundles, bundle);
869 bundle_del_port(struct ofport_dpif *port)
871 struct ofbundle *bundle = port->bundle;
873 bundle->ofproto->need_revalidate = true;
875 list_remove(&port->bundle_node);
879 lacp_slave_unregister(bundle->lacp, port);
882 bond_slave_unregister(bundle->bond, port);
885 bundle->floodable = true;
886 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
887 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
888 bundle->floodable = false;
894 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
895 struct lacp_slave_settings *lacp,
896 uint32_t bond_stable_id)
898 struct ofport_dpif *port;
900 port = get_ofp_port(bundle->ofproto, ofp_port);
905 if (port->bundle != bundle) {
906 bundle->ofproto->need_revalidate = true;
908 bundle_del_port(port);
911 port->bundle = bundle;
912 list_push_back(&bundle->ports, &port->bundle_node);
913 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
914 bundle->floodable = false;
918 lacp_slave_register(bundle->lacp, port, lacp);
921 port->bond_stable_id = bond_stable_id;
927 bundle_destroy(struct ofbundle *bundle)
929 struct ofproto_dpif *ofproto;
930 struct ofport_dpif *port, *next_port;
937 ofproto = bundle->ofproto;
938 for (i = 0; i < MAX_MIRRORS; i++) {
939 struct ofmirror *m = ofproto->mirrors[i];
941 if (m->out == bundle) {
943 } else if (hmapx_find_and_delete(&m->srcs, bundle)
944 || hmapx_find_and_delete(&m->dsts, bundle)) {
945 ofproto->need_revalidate = true;
950 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
951 bundle_del_port(port);
954 bundle_flush_macs(bundle);
955 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
957 free(bundle->trunks);
958 lacp_destroy(bundle->lacp);
959 bond_destroy(bundle->bond);
964 bundle_set(struct ofproto *ofproto_, void *aux,
965 const struct ofproto_bundle_settings *s)
967 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
968 bool need_flush = false;
969 const unsigned long *trunks;
970 struct ofport_dpif *port;
971 struct ofbundle *bundle;
976 bundle_destroy(bundle_lookup(ofproto, aux));
980 assert(s->n_slaves == 1 || s->bond != NULL);
981 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
983 bundle = bundle_lookup(ofproto, aux);
985 bundle = xmalloc(sizeof *bundle);
987 bundle->ofproto = ofproto;
988 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
989 hash_pointer(aux, 0));
993 list_init(&bundle->ports);
995 bundle->trunks = NULL;
999 bundle->floodable = true;
1001 bundle->src_mirrors = 0;
1002 bundle->dst_mirrors = 0;
1003 bundle->mirror_out = 0;
1006 if (!bundle->name || strcmp(s->name, bundle->name)) {
1008 bundle->name = xstrdup(s->name);
1013 if (!bundle->lacp) {
1014 bundle->lacp = lacp_create();
1016 lacp_configure(bundle->lacp, s->lacp);
1018 lacp_destroy(bundle->lacp);
1019 bundle->lacp = NULL;
1022 /* Update set of ports. */
1024 for (i = 0; i < s->n_slaves; i++) {
1025 if (!bundle_add_port(bundle, s->slaves[i],
1026 s->lacp ? &s->lacp_slaves[i] : NULL,
1027 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1031 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1032 struct ofport_dpif *next_port;
1034 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1035 for (i = 0; i < s->n_slaves; i++) {
1036 if (s->slaves[i] == port->up.ofp_port) {
1041 bundle_del_port(port);
1045 assert(list_size(&bundle->ports) <= s->n_slaves);
1047 if (list_is_empty(&bundle->ports)) {
1048 bundle_destroy(bundle);
1053 if (s->vlan != bundle->vlan) {
1054 bundle->vlan = s->vlan;
1058 /* Get trunked VLANs. */
1059 trunks = s->vlan == -1 ? NULL : s->trunks;
1060 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1061 free(bundle->trunks);
1062 bundle->trunks = vlan_bitmap_clone(trunks);
1067 if (!list_is_short(&bundle->ports)) {
1068 bundle->ofproto->has_bonded_bundles = true;
1070 if (bond_reconfigure(bundle->bond, s->bond)) {
1071 ofproto->need_revalidate = true;
1074 bundle->bond = bond_create(s->bond);
1075 ofproto->need_revalidate = true;
1078 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1079 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1083 bond_destroy(bundle->bond);
1084 bundle->bond = NULL;
1087 /* If we changed something that would affect MAC learning, un-learn
1088 * everything on this port and force flow revalidation. */
1090 bundle_flush_macs(bundle);
1097 bundle_remove(struct ofport *port_)
1099 struct ofport_dpif *port = ofport_dpif_cast(port_);
1100 struct ofbundle *bundle = port->bundle;
1103 bundle_del_port(port);
1104 if (list_is_empty(&bundle->ports)) {
1105 bundle_destroy(bundle);
1106 } else if (list_is_short(&bundle->ports)) {
1107 bond_destroy(bundle->bond);
1108 bundle->bond = NULL;
1114 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1116 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1117 struct ofport_dpif *port = port_;
1118 uint8_t ea[ETH_ADDR_LEN];
1121 error = netdev_get_etheraddr(port->up.netdev, ea);
1123 struct lacp_pdu *packet_pdu;
1124 struct ofpbuf packet;
1126 ofpbuf_init(&packet, 0);
1127 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1128 sizeof *packet_pdu);
1130 error = netdev_send(port->up.netdev, &packet);
1132 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1133 "(%s)", port->bundle->name,
1134 netdev_get_name(port->up.netdev), strerror(error));
1136 ofpbuf_uninit(&packet);
1138 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1139 "%s (%s)", port->bundle->name,
1140 netdev_get_name(port->up.netdev), strerror(error));
1145 bundle_send_learning_packets(struct ofbundle *bundle)
1147 struct ofproto_dpif *ofproto = bundle->ofproto;
1148 int error, n_packets, n_errors;
1149 struct mac_entry *e;
1151 error = n_packets = n_errors = 0;
1152 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1153 if (e->port.p != bundle) {
1154 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1164 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1165 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1166 "packets, last error was: %s",
1167 bundle->name, n_errors, n_packets, strerror(error));
1169 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1170 bundle->name, n_packets);
1175 bundle_run(struct ofbundle *bundle)
1178 lacp_run(bundle->lacp, send_pdu_cb);
1181 struct ofport_dpif *port;
1183 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1184 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1187 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1188 lacp_negotiated(bundle->lacp));
1189 if (bond_should_send_learning_packets(bundle->bond)) {
1190 bundle_send_learning_packets(bundle);
1196 bundle_wait(struct ofbundle *bundle)
1199 lacp_wait(bundle->lacp);
1202 bond_wait(bundle->bond);
1209 mirror_scan(struct ofproto_dpif *ofproto)
1213 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1214 if (!ofproto->mirrors[idx]) {
1221 static struct ofmirror *
1222 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1226 for (i = 0; i < MAX_MIRRORS; i++) {
1227 struct ofmirror *mirror = ofproto->mirrors[i];
1228 if (mirror && mirror->aux == aux) {
1237 mirror_set(struct ofproto *ofproto_, void *aux,
1238 const struct ofproto_mirror_settings *s)
1240 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1241 mirror_mask_t mirror_bit;
1242 struct ofbundle *bundle;
1243 struct ofmirror *mirror;
1244 struct ofbundle *out;
1245 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1246 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1249 mirror = mirror_lookup(ofproto, aux);
1251 mirror_destroy(mirror);
1257 idx = mirror_scan(ofproto);
1259 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1261 ofproto->up.name, MAX_MIRRORS, s->name);
1265 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1266 mirror->ofproto = ofproto;
1269 mirror->out_vlan = -1;
1270 mirror->name = NULL;
1273 if (!mirror->name || strcmp(s->name, mirror->name)) {
1275 mirror->name = xstrdup(s->name);
1278 /* Get the new configuration. */
1279 if (s->out_bundle) {
1280 out = bundle_lookup(ofproto, s->out_bundle);
1282 mirror_destroy(mirror);
1288 out_vlan = s->out_vlan;
1290 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1291 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1293 /* If the configuration has not changed, do nothing. */
1294 if (hmapx_equals(&srcs, &mirror->srcs)
1295 && hmapx_equals(&dsts, &mirror->dsts)
1296 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1297 && mirror->out == out
1298 && mirror->out_vlan == out_vlan)
1300 hmapx_destroy(&srcs);
1301 hmapx_destroy(&dsts);
1305 hmapx_swap(&srcs, &mirror->srcs);
1306 hmapx_destroy(&srcs);
1308 hmapx_swap(&dsts, &mirror->dsts);
1309 hmapx_destroy(&dsts);
1311 free(mirror->vlans);
1312 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1315 mirror->out_vlan = out_vlan;
1317 /* Update bundles. */
1318 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1319 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1320 if (hmapx_contains(&mirror->srcs, bundle)) {
1321 bundle->src_mirrors |= mirror_bit;
1323 bundle->src_mirrors &= ~mirror_bit;
1326 if (hmapx_contains(&mirror->dsts, bundle)) {
1327 bundle->dst_mirrors |= mirror_bit;
1329 bundle->dst_mirrors &= ~mirror_bit;
1332 if (mirror->out == bundle) {
1333 bundle->mirror_out |= mirror_bit;
1335 bundle->mirror_out &= ~mirror_bit;
1339 ofproto->need_revalidate = true;
1340 mac_learning_flush(ofproto->ml);
1346 mirror_destroy(struct ofmirror *mirror)
1348 struct ofproto_dpif *ofproto;
1349 mirror_mask_t mirror_bit;
1350 struct ofbundle *bundle;
1356 ofproto = mirror->ofproto;
1357 ofproto->need_revalidate = true;
1358 mac_learning_flush(ofproto->ml);
1360 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1361 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1362 bundle->src_mirrors &= ~mirror_bit;
1363 bundle->dst_mirrors &= ~mirror_bit;
1364 bundle->mirror_out &= ~mirror_bit;
1367 hmapx_destroy(&mirror->srcs);
1368 hmapx_destroy(&mirror->dsts);
1369 free(mirror->vlans);
1371 ofproto->mirrors[mirror->idx] = NULL;
1377 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1379 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1380 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1381 ofproto->need_revalidate = true;
1382 mac_learning_flush(ofproto->ml);
1388 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1390 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1391 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1392 return bundle && bundle->mirror_out != 0;
1396 forward_bpdu_changed(struct ofproto *ofproto_)
1398 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1399 /* Revalidate cached flows whenever forward_bpdu option changes. */
1400 ofproto->need_revalidate = true;
1405 static struct ofport_dpif *
1406 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1408 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1409 return ofport ? ofport_dpif_cast(ofport) : NULL;
1412 static struct ofport_dpif *
1413 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1415 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1419 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1420 struct dpif_port *dpif_port)
1422 ofproto_port->name = dpif_port->name;
1423 ofproto_port->type = dpif_port->type;
1424 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1428 port_run(struct ofport_dpif *ofport)
1430 bool enable = netdev_get_carrier(ofport->up.netdev);
1433 cfm_run(ofport->cfm);
1435 if (cfm_should_send_ccm(ofport->cfm)) {
1436 struct ofpbuf packet;
1438 ofpbuf_init(&packet, 0);
1439 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1440 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1441 ofport->odp_port, &packet);
1442 ofpbuf_uninit(&packet);
1445 enable = enable && !cfm_get_fault(ofport->cfm);
1448 if (ofport->bundle) {
1449 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1452 if (ofport->may_enable != enable) {
1453 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1455 if (ofproto->has_bundle_action) {
1456 ofproto->need_revalidate = true;
1460 ofport->may_enable = enable;
1464 port_wait(struct ofport_dpif *ofport)
1467 cfm_wait(ofport->cfm);
1472 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1473 struct ofproto_port *ofproto_port)
1475 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1476 struct dpif_port dpif_port;
1479 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1481 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1487 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1489 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1493 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1495 *ofp_portp = odp_port_to_ofp_port(odp_port);
1501 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1503 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1506 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1508 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1510 /* The caller is going to close ofport->up.netdev. If this is a
1511 * bonded port, then the bond is using that netdev, so remove it
1512 * from the bond. The client will need to reconfigure everything
1513 * after deleting ports, so then the slave will get re-added. */
1514 bundle_remove(&ofport->up);
1520 struct port_dump_state {
1521 struct dpif_port_dump dump;
1526 port_dump_start(const struct ofproto *ofproto_, void **statep)
1528 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1529 struct port_dump_state *state;
1531 *statep = state = xmalloc(sizeof *state);
1532 dpif_port_dump_start(&state->dump, ofproto->dpif);
1533 state->done = false;
1538 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1539 struct ofproto_port *port)
1541 struct port_dump_state *state = state_;
1542 struct dpif_port dpif_port;
1544 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1545 ofproto_port_from_dpif_port(port, &dpif_port);
1548 int error = dpif_port_dump_done(&state->dump);
1550 return error ? error : EOF;
1555 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1557 struct port_dump_state *state = state_;
1560 dpif_port_dump_done(&state->dump);
1567 port_poll(const struct ofproto *ofproto_, char **devnamep)
1569 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1570 return dpif_port_poll(ofproto->dpif, devnamep);
1574 port_poll_wait(const struct ofproto *ofproto_)
1576 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1577 dpif_port_poll_wait(ofproto->dpif);
1581 port_is_lacp_current(const struct ofport *ofport_)
1583 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1584 return (ofport->bundle && ofport->bundle->lacp
1585 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1589 /* Upcall handling. */
1591 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1592 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1593 * their individual configurations.
1595 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1596 * Otherwise, ownership is transferred to this function. */
1598 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1599 const struct flow *flow, bool clone)
1601 struct ofputil_packet_in pin;
1603 pin.packet = upcall->packet;
1604 pin.in_port = flow->in_port;
1605 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1606 pin.buffer_id = 0; /* not yet known */
1607 pin.send_len = upcall->userdata;
1608 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1609 clone ? NULL : upcall->packet);
1613 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1614 const struct ofpbuf *packet)
1616 if (cfm_should_process_flow(flow)) {
1617 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1618 if (packet && ofport && ofport->cfm) {
1619 cfm_process_heartbeat(ofport->cfm, packet);
1622 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
1623 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1624 if (packet && port && port->bundle && port->bundle->lacp) {
1625 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
1627 lacp_process_pdu(port->bundle->lacp, port, pdu);
1636 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1638 struct facet *facet;
1641 /* Obtain in_port and tun_id, at least. */
1642 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1644 /* Set header pointers in 'flow'. */
1645 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1647 /* Handle 802.1ag and LACP. */
1648 if (process_special(ofproto, &flow, upcall->packet)) {
1649 ofpbuf_delete(upcall->packet);
1650 ofproto->n_matches++;
1654 /* Check with in-band control to see if this packet should be sent
1655 * to the local port regardless of the flow table. */
1656 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1657 send_packet(ofproto, OVSP_LOCAL, upcall->packet);
1660 facet = facet_lookup_valid(ofproto, &flow);
1662 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow, 0);
1664 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1665 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1667 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1668 COVERAGE_INC(ofproto_dpif_no_packet_in);
1669 /* XXX install 'drop' flow entry */
1670 ofpbuf_delete(upcall->packet);
1674 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1678 send_packet_in(ofproto, upcall, &flow, false);
1682 facet = facet_create(rule, &flow, upcall->packet);
1683 } else if (!facet->may_install) {
1684 /* The facet is not installable, that is, we need to process every
1685 * packet, so process the current packet's actions into 'facet'. */
1686 facet_make_actions(ofproto, facet, upcall->packet);
1689 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1691 * Extra-special case for fail-open mode.
1693 * We are in fail-open mode and the packet matched the fail-open rule,
1694 * but we are connected to a controller too. We should send the packet
1695 * up to the controller in the hope that it will try to set up a flow
1696 * and thereby allow us to exit fail-open.
1698 * See the top-level comment in fail-open.c for more information.
1700 send_packet_in(ofproto, upcall, &flow, true);
1703 facet_execute(ofproto, facet, upcall->packet);
1704 facet_install(ofproto, facet, false);
1705 ofproto->n_matches++;
1709 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1713 switch (upcall->type) {
1714 case DPIF_UC_ACTION:
1715 COVERAGE_INC(ofproto_dpif_ctlr_action);
1716 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1717 send_packet_in(ofproto, upcall, &flow, false);
1720 case DPIF_UC_SAMPLE:
1721 if (ofproto->sflow) {
1722 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1723 dpif_sflow_received(ofproto->sflow, upcall, &flow);
1725 ofpbuf_delete(upcall->packet);
1729 handle_miss_upcall(ofproto, upcall);
1732 case DPIF_N_UC_TYPES:
1734 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1739 /* Flow expiration. */
1741 static int facet_max_idle(const struct ofproto_dpif *);
1742 static void update_stats(struct ofproto_dpif *);
1743 static void rule_expire(struct rule_dpif *);
1744 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1746 /* This function is called periodically by run(). Its job is to collect
1747 * updates for the flows that have been installed into the datapath, most
1748 * importantly when they last were used, and then use that information to
1749 * expire flows that have not been used recently.
1751 * Returns the number of milliseconds after which it should be called again. */
1753 expire(struct ofproto_dpif *ofproto)
1755 struct rule_dpif *rule, *next_rule;
1756 struct classifier *table;
1759 /* Update stats for each flow in the datapath. */
1760 update_stats(ofproto);
1762 /* Expire facets that have been idle too long. */
1763 dp_max_idle = facet_max_idle(ofproto);
1764 expire_facets(ofproto, dp_max_idle);
1766 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1767 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1768 struct cls_cursor cursor;
1770 cls_cursor_init(&cursor, table, NULL);
1771 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1776 /* All outstanding data in existing flows has been accounted, so it's a
1777 * good time to do bond rebalancing. */
1778 if (ofproto->has_bonded_bundles) {
1779 struct ofbundle *bundle;
1781 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1783 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1788 return MIN(dp_max_idle, 1000);
1791 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1793 * This function also pushes statistics updates to rules which each facet
1794 * resubmits into. Generally these statistics will be accurate. However, if a
1795 * facet changes the rule it resubmits into at some time in between
1796 * update_stats() runs, it is possible that statistics accrued to the
1797 * old rule will be incorrectly attributed to the new rule. This could be
1798 * avoided by calling update_stats() whenever rules are created or
1799 * deleted. However, the performance impact of making so many calls to the
1800 * datapath do not justify the benefit of having perfectly accurate statistics.
1803 update_stats(struct ofproto_dpif *p)
1805 const struct dpif_flow_stats *stats;
1806 struct dpif_flow_dump dump;
1807 const struct nlattr *key;
1810 dpif_flow_dump_start(&dump, p->dpif);
1811 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1812 struct facet *facet;
1815 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1819 odp_flow_key_format(key, key_len, &s);
1820 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
1826 facet = facet_find(p, &flow);
1828 if (facet && facet->installed) {
1830 if (stats->n_packets >= facet->dp_packet_count) {
1831 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1832 facet->packet_count += extra;
1834 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1837 if (stats->n_bytes >= facet->dp_byte_count) {
1838 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1840 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1843 facet->dp_packet_count = stats->n_packets;
1844 facet->dp_byte_count = stats->n_bytes;
1846 facet_update_time(p, facet, stats->used);
1847 facet_account(p, facet);
1848 facet_push_stats(facet);
1850 /* There's a flow in the datapath that we know nothing about.
1852 COVERAGE_INC(facet_unexpected);
1853 dpif_flow_del(p->dpif, key, key_len, NULL);
1856 dpif_flow_dump_done(&dump);
1859 /* Calculates and returns the number of milliseconds of idle time after which
1860 * facets should expire from the datapath and we should fold their statistics
1861 * into their parent rules in userspace. */
1863 facet_max_idle(const struct ofproto_dpif *ofproto)
1866 * Idle time histogram.
1868 * Most of the time a switch has a relatively small number of facets. When
1869 * this is the case we might as well keep statistics for all of them in
1870 * userspace and to cache them in the kernel datapath for performance as
1873 * As the number of facets increases, the memory required to maintain
1874 * statistics about them in userspace and in the kernel becomes
1875 * significant. However, with a large number of facets it is likely that
1876 * only a few of them are "heavy hitters" that consume a large amount of
1877 * bandwidth. At this point, only heavy hitters are worth caching in the
1878 * kernel and maintaining in userspaces; other facets we can discard.
1880 * The technique used to compute the idle time is to build a histogram with
1881 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1882 * that is installed in the kernel gets dropped in the appropriate bucket.
1883 * After the histogram has been built, we compute the cutoff so that only
1884 * the most-recently-used 1% of facets (but at least
1885 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1886 * the most-recently-used bucket of facets is kept, so actually an
1887 * arbitrary number of facets can be kept in any given expiration run
1888 * (though the next run will delete most of those unless they receive
1891 * This requires a second pass through the facets, in addition to the pass
1892 * made by update_stats(), because the former function never looks
1893 * at uninstallable facets.
1895 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1896 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1897 int buckets[N_BUCKETS] = { 0 };
1898 int total, subtotal, bucket;
1899 struct facet *facet;
1903 total = hmap_count(&ofproto->facets);
1904 if (total <= ofproto->up.flow_eviction_threshold) {
1905 return N_BUCKETS * BUCKET_WIDTH;
1908 /* Build histogram. */
1910 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1911 long long int idle = now - facet->used;
1912 int bucket = (idle <= 0 ? 0
1913 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1914 : (unsigned int) idle / BUCKET_WIDTH);
1918 /* Find the first bucket whose flows should be expired. */
1919 subtotal = bucket = 0;
1921 subtotal += buckets[bucket++];
1922 } while (bucket < N_BUCKETS &&
1923 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
1925 if (VLOG_IS_DBG_ENABLED()) {
1929 ds_put_cstr(&s, "keep");
1930 for (i = 0; i < N_BUCKETS; i++) {
1932 ds_put_cstr(&s, ", drop");
1935 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1938 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1942 return bucket * BUCKET_WIDTH;
1946 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1948 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1949 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1950 struct ofexpired expired;
1952 if (facet->installed) {
1953 struct dpif_flow_stats stats;
1955 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1957 facet_update_stats(ofproto, facet, &stats);
1960 expired.flow = facet->flow;
1961 expired.packet_count = facet->packet_count;
1962 expired.byte_count = facet->byte_count;
1963 expired.used = facet->used;
1964 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1969 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1971 long long int cutoff = time_msec() - dp_max_idle;
1972 struct facet *facet, *next_facet;
1974 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1975 facet_active_timeout(ofproto, facet);
1976 if (facet->used < cutoff) {
1977 facet_remove(ofproto, facet);
1982 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
1983 * then delete it entirely. */
1985 rule_expire(struct rule_dpif *rule)
1987 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1988 struct facet *facet, *next_facet;
1992 /* Has 'rule' expired? */
1994 if (rule->up.hard_timeout
1995 && now > rule->up.created + rule->up.hard_timeout * 1000) {
1996 reason = OFPRR_HARD_TIMEOUT;
1997 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
1998 && now > rule->used + rule->up.idle_timeout * 1000) {
1999 reason = OFPRR_IDLE_TIMEOUT;
2004 COVERAGE_INC(ofproto_dpif_expired);
2006 /* Update stats. (This is a no-op if the rule expired due to an idle
2007 * timeout, because that only happens when the rule has no facets left.) */
2008 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2009 facet_remove(ofproto, facet);
2012 /* Get rid of the rule. */
2013 ofproto_rule_expire(&rule->up, reason);
2018 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2019 * example 'packet' within that flow.
2021 * The caller must already have determined that no facet with an identical
2022 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2023 * the ofproto's classifier table. */
2024 static struct facet *
2025 facet_create(struct rule_dpif *rule, const struct flow *flow,
2026 const struct ofpbuf *packet)
2028 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2029 struct facet *facet;
2031 facet = xzalloc(sizeof *facet);
2032 facet->used = time_msec();
2033 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2034 list_push_back(&rule->facets, &facet->list_node);
2036 facet->flow = *flow;
2037 netflow_flow_init(&facet->nf_flow);
2038 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2040 facet_make_actions(ofproto, facet, packet);
2046 facet_free(struct facet *facet)
2048 free(facet->actions);
2052 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2053 * 'packet', which arrived on 'in_port'.
2055 * Takes ownership of 'packet'. */
2057 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2058 const struct nlattr *odp_actions, size_t actions_len,
2059 struct ofpbuf *packet)
2061 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2062 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE) {
2063 /* As an optimization, avoid a round-trip from userspace to kernel to
2064 * userspace. This also avoids possibly filling up kernel packet
2065 * buffers along the way. */
2066 struct dpif_upcall upcall;
2068 upcall.type = DPIF_UC_ACTION;
2069 upcall.packet = packet;
2072 upcall.userdata = nl_attr_get_u64(odp_actions);
2073 upcall.sample_pool = 0;
2074 upcall.actions = NULL;
2075 upcall.actions_len = 0;
2077 send_packet_in(ofproto, &upcall, flow, false);
2081 struct odputil_keybuf keybuf;
2085 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2086 odp_flow_key_from_flow(&key, flow);
2088 error = dpif_execute(ofproto->dpif, key.data, key.size,
2089 odp_actions, actions_len, packet);
2091 ofpbuf_delete(packet);
2096 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2097 * statistics appropriately. 'packet' must have at least sizeof(struct
2098 * ofp_packet_in) bytes of headroom.
2100 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2101 * applying flow_extract() to 'packet' would yield the same flow as
2104 * 'facet' must have accurately composed datapath actions; that is, it must
2105 * not be in need of revalidation.
2107 * Takes ownership of 'packet'. */
2109 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2110 struct ofpbuf *packet)
2112 struct dpif_flow_stats stats;
2114 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2116 flow_extract_stats(&facet->flow, packet, &stats);
2117 stats.used = time_msec();
2118 if (execute_odp_actions(ofproto, &facet->flow,
2119 facet->actions, facet->actions_len, packet)) {
2120 facet_update_stats(ofproto, facet, &stats);
2124 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2126 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2127 * rule's statistics, via facet_uninstall().
2129 * - Removes 'facet' from its rule and from ofproto->facets.
2132 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2134 facet_uninstall(ofproto, facet);
2135 facet_flush_stats(ofproto, facet);
2136 hmap_remove(&ofproto->facets, &facet->hmap_node);
2137 list_remove(&facet->list_node);
2141 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2143 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2144 const struct ofpbuf *packet)
2146 const struct rule_dpif *rule = facet->rule;
2147 struct ofpbuf *odp_actions;
2148 struct action_xlate_ctx ctx;
2150 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2151 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2152 facet->tags = ctx.tags;
2153 facet->may_install = ctx.may_set_up_flow;
2154 facet->nf_flow.output_iface = ctx.nf_output_iface;
2156 if (facet->actions_len != odp_actions->size
2157 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2158 free(facet->actions);
2159 facet->actions_len = odp_actions->size;
2160 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2163 ofpbuf_delete(odp_actions);
2166 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2167 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2168 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2169 * since 'facet' was last updated.
2171 * Returns 0 if successful, otherwise a positive errno value.*/
2173 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2174 const struct nlattr *actions, size_t actions_len,
2175 struct dpif_flow_stats *stats)
2177 struct odputil_keybuf keybuf;
2178 enum dpif_flow_put_flags flags;
2182 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2184 flags |= DPIF_FP_ZERO_STATS;
2187 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2188 odp_flow_key_from_flow(&key, &facet->flow);
2190 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2191 actions, actions_len, stats);
2194 facet_reset_dp_stats(facet, stats);
2200 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2201 * 'zero_stats' is true, clears any existing statistics from the datapath for
2204 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2206 struct dpif_flow_stats stats;
2208 if (facet->may_install
2209 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2210 zero_stats ? &stats : NULL)) {
2211 facet->installed = true;
2216 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2218 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2222 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2225 struct ofbundle *in_bundle;
2226 const struct nlattr *a;
2232 if (facet->byte_count <= facet->accounted_bytes) {
2235 n_bytes = facet->byte_count - facet->accounted_bytes;
2236 facet->accounted_bytes = facet->byte_count;
2238 /* Test that 'tags' is nonzero to ensure that only flows that include an
2239 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2240 * This works because OFPP_NORMAL always sets a nonzero tag value.
2242 * Feed information from the active flows back into the learning table to
2243 * ensure that table is always in sync with what is actually flowing
2244 * through the datapath. */
2246 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2247 &vlan, &in_bundle)) {
2251 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2253 if (!ofproto->has_bonded_bundles) {
2257 /* This loop feeds byte counters to bond_account() for rebalancing to use
2258 * as a basis. We also need to track the actual VLAN on which the packet
2259 * is going to be sent to ensure that it matches the one passed to
2260 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2262 vlan_tci = facet->flow.vlan_tci;
2263 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2264 struct ofport_dpif *port;
2266 switch (nl_attr_type(a)) {
2267 case OVS_ACTION_ATTR_OUTPUT:
2268 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2269 if (port && port->bundle && port->bundle->bond) {
2270 bond_account(port->bundle->bond, &facet->flow,
2271 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2275 case OVS_ACTION_ATTR_STRIP_VLAN:
2276 vlan_tci = htons(0);
2279 case OVS_ACTION_ATTR_SET_DL_TCI:
2280 vlan_tci = nl_attr_get_be16(a);
2286 /* If 'rule' is installed in the datapath, uninstalls it. */
2288 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2290 if (facet->installed) {
2291 struct odputil_keybuf keybuf;
2292 struct dpif_flow_stats stats;
2296 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2297 odp_flow_key_from_flow(&key, &facet->flow);
2299 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2300 facet_reset_dp_stats(facet, &stats);
2302 facet_update_stats(p, facet, &stats);
2304 facet->installed = false;
2306 assert(facet->dp_packet_count == 0);
2307 assert(facet->dp_byte_count == 0);
2311 /* Returns true if the only action for 'facet' is to send to the controller.
2312 * (We don't report NetFlow expiration messages for such facets because they
2313 * are just part of the control logic for the network, not real traffic). */
2315 facet_is_controller_flow(struct facet *facet)
2318 && facet->rule->up.n_actions == 1
2319 && action_outputs_to_port(&facet->rule->up.actions[0],
2320 htons(OFPP_CONTROLLER)));
2323 /* Resets 'facet''s datapath statistics counters. This should be called when
2324 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2325 * it should contain the statistics returned by dpif when 'facet' was reset in
2326 * the datapath. 'stats' will be modified to only included statistics new
2327 * since 'facet' was last updated. */
2329 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2331 if (stats && facet->dp_packet_count <= stats->n_packets
2332 && facet->dp_byte_count <= stats->n_bytes) {
2333 stats->n_packets -= facet->dp_packet_count;
2334 stats->n_bytes -= facet->dp_byte_count;
2337 facet->dp_packet_count = 0;
2338 facet->dp_byte_count = 0;
2341 /* Folds all of 'facet''s statistics into its rule. Also updates the
2342 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2343 * 'facet''s statistics in the datapath should have been zeroed and folded into
2344 * its packet and byte counts before this function is called. */
2346 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2348 assert(!facet->dp_byte_count);
2349 assert(!facet->dp_packet_count);
2351 facet_push_stats(facet);
2352 facet_account(ofproto, facet);
2354 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2355 struct ofexpired expired;
2356 expired.flow = facet->flow;
2357 expired.packet_count = facet->packet_count;
2358 expired.byte_count = facet->byte_count;
2359 expired.used = facet->used;
2360 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2363 facet->rule->packet_count += facet->packet_count;
2364 facet->rule->byte_count += facet->byte_count;
2366 /* Reset counters to prevent double counting if 'facet' ever gets
2368 facet_reset_counters(facet);
2370 netflow_flow_clear(&facet->nf_flow);
2373 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2374 * Returns it if found, otherwise a null pointer.
2376 * The returned facet might need revalidation; use facet_lookup_valid()
2377 * instead if that is important. */
2378 static struct facet *
2379 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2381 struct facet *facet;
2383 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2385 if (flow_equal(flow, &facet->flow)) {
2393 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2394 * Returns it if found, otherwise a null pointer.
2396 * The returned facet is guaranteed to be valid. */
2397 static struct facet *
2398 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2400 struct facet *facet = facet_find(ofproto, flow);
2402 /* The facet we found might not be valid, since we could be in need of
2403 * revalidation. If it is not valid, don't return it. */
2405 && ofproto->need_revalidate
2406 && !facet_revalidate(ofproto, facet)) {
2407 COVERAGE_INC(facet_invalidated);
2414 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2416 * - If the rule found is different from 'facet''s current rule, moves
2417 * 'facet' to the new rule and recompiles its actions.
2419 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2420 * where it is and recompiles its actions anyway.
2422 * - If there is none, destroys 'facet'.
2424 * Returns true if 'facet' still exists, false if it has been destroyed. */
2426 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2428 struct action_xlate_ctx ctx;
2429 struct ofpbuf *odp_actions;
2430 struct rule_dpif *new_rule;
2431 bool actions_changed;
2433 COVERAGE_INC(facet_revalidate);
2435 /* Determine the new rule. */
2436 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
2438 /* No new rule, so delete the facet. */
2439 facet_remove(ofproto, facet);
2443 /* Calculate new datapath actions.
2445 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2446 * emit a NetFlow expiration and, if so, we need to have the old state
2447 * around to properly compose it. */
2448 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2449 odp_actions = xlate_actions(&ctx,
2450 new_rule->up.actions, new_rule->up.n_actions);
2451 actions_changed = (facet->actions_len != odp_actions->size
2452 || memcmp(facet->actions, odp_actions->data,
2453 facet->actions_len));
2455 /* If the datapath actions changed or the installability changed,
2456 * then we need to talk to the datapath. */
2457 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2458 if (ctx.may_set_up_flow) {
2459 struct dpif_flow_stats stats;
2461 facet_put__(ofproto, facet,
2462 odp_actions->data, odp_actions->size, &stats);
2463 facet_update_stats(ofproto, facet, &stats);
2465 facet_uninstall(ofproto, facet);
2468 /* The datapath flow is gone or has zeroed stats, so push stats out of
2469 * 'facet' into 'rule'. */
2470 facet_flush_stats(ofproto, facet);
2473 /* Update 'facet' now that we've taken care of all the old state. */
2474 facet->tags = ctx.tags;
2475 facet->nf_flow.output_iface = ctx.nf_output_iface;
2476 facet->may_install = ctx.may_set_up_flow;
2477 if (actions_changed) {
2478 free(facet->actions);
2479 facet->actions_len = odp_actions->size;
2480 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2482 if (facet->rule != new_rule) {
2483 COVERAGE_INC(facet_changed_rule);
2484 list_remove(&facet->list_node);
2485 list_push_back(&new_rule->facets, &facet->list_node);
2486 facet->rule = new_rule;
2487 facet->used = new_rule->up.created;
2488 facet->rs_used = facet->used;
2491 ofpbuf_delete(odp_actions);
2496 /* Updates 'facet''s used time. Caller is responsible for calling
2497 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2499 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2502 if (used > facet->used) {
2504 if (used > facet->rule->used) {
2505 facet->rule->used = used;
2507 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2511 /* Folds the statistics from 'stats' into the counters in 'facet'.
2513 * Because of the meaning of a facet's counters, it only makes sense to do this
2514 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2515 * packet that was sent by hand or if it represents statistics that have been
2516 * cleared out of the datapath. */
2518 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2519 const struct dpif_flow_stats *stats)
2521 if (stats->n_packets || stats->used > facet->used) {
2522 facet_update_time(ofproto, facet, stats->used);
2523 facet->packet_count += stats->n_packets;
2524 facet->byte_count += stats->n_bytes;
2525 facet_push_stats(facet);
2526 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2531 facet_reset_counters(struct facet *facet)
2533 facet->packet_count = 0;
2534 facet->byte_count = 0;
2535 facet->rs_packet_count = 0;
2536 facet->rs_byte_count = 0;
2537 facet->accounted_bytes = 0;
2541 facet_push_stats(struct facet *facet)
2543 uint64_t rs_packets, rs_bytes;
2545 assert(facet->packet_count >= facet->rs_packet_count);
2546 assert(facet->byte_count >= facet->rs_byte_count);
2547 assert(facet->used >= facet->rs_used);
2549 rs_packets = facet->packet_count - facet->rs_packet_count;
2550 rs_bytes = facet->byte_count - facet->rs_byte_count;
2552 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2553 facet->rs_packet_count = facet->packet_count;
2554 facet->rs_byte_count = facet->byte_count;
2555 facet->rs_used = facet->used;
2557 flow_push_stats(facet->rule, &facet->flow,
2558 rs_packets, rs_bytes, facet->used);
2562 struct ofproto_push {
2563 struct action_xlate_ctx ctx;
2570 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2572 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2575 rule->packet_count += push->packets;
2576 rule->byte_count += push->bytes;
2577 rule->used = MAX(push->used, rule->used);
2581 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2582 * 'rule''s actions. */
2584 flow_push_stats(const struct rule_dpif *rule,
2585 struct flow *flow, uint64_t packets, uint64_t bytes,
2588 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2589 struct ofproto_push push;
2591 push.packets = packets;
2595 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2596 push.ctx.resubmit_hook = push_resubmit;
2597 ofpbuf_delete(xlate_actions(&push.ctx,
2598 rule->up.actions, rule->up.n_actions));
2603 static struct rule_dpif *
2604 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2607 return rule_dpif_cast(rule_from_cls_rule(
2608 classifier_lookup(&ofproto->up.tables[table_id],
2613 complete_operation(struct rule_dpif *rule)
2615 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2617 ofproto->need_revalidate = true;
2619 struct dpif_completion *c = xmalloc(sizeof *c);
2620 c->op = rule->up.pending;
2621 list_push_back(&ofproto->completions, &c->list_node);
2623 ofoperation_complete(rule->up.pending, 0);
2627 static struct rule *
2630 struct rule_dpif *rule = xmalloc(sizeof *rule);
2635 rule_dealloc(struct rule *rule_)
2637 struct rule_dpif *rule = rule_dpif_cast(rule_);
2642 rule_construct(struct rule *rule_)
2644 struct rule_dpif *rule = rule_dpif_cast(rule_);
2645 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2646 struct rule_dpif *victim;
2649 error = validate_actions(rule->up.actions, rule->up.n_actions,
2650 &rule->up.cr.flow, ofproto->max_ports);
2655 rule->used = rule->up.created;
2656 rule->packet_count = 0;
2657 rule->byte_count = 0;
2659 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2660 if (victim && !list_is_empty(&victim->facets)) {
2661 struct facet *facet;
2663 rule->facets = victim->facets;
2664 list_moved(&rule->facets);
2665 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2666 /* XXX: We're only clearing our local counters here. It's possible
2667 * that quite a few packets are unaccounted for in the datapath
2668 * statistics. These will be accounted to the new rule instead of
2669 * cleared as required. This could be fixed by clearing out the
2670 * datapath statistics for this facet, but currently it doesn't
2672 facet_reset_counters(facet);
2676 /* Must avoid list_moved() in this case. */
2677 list_init(&rule->facets);
2680 complete_operation(rule);
2685 rule_destruct(struct rule *rule_)
2687 struct rule_dpif *rule = rule_dpif_cast(rule_);
2688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2689 struct facet *facet, *next_facet;
2691 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2692 facet_revalidate(ofproto, facet);
2695 complete_operation(rule);
2699 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2701 struct rule_dpif *rule = rule_dpif_cast(rule_);
2702 struct facet *facet;
2704 /* Start from historical data for 'rule' itself that are no longer tracked
2705 * in facets. This counts, for example, facets that have expired. */
2706 *packets = rule->packet_count;
2707 *bytes = rule->byte_count;
2709 /* Add any statistics that are tracked by facets. This includes
2710 * statistical data recently updated by ofproto_update_stats() as well as
2711 * stats for packets that were executed "by hand" via dpif_execute(). */
2712 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2713 *packets += facet->packet_count;
2714 *bytes += facet->byte_count;
2719 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2721 struct rule_dpif *rule = rule_dpif_cast(rule_);
2722 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2723 struct action_xlate_ctx ctx;
2724 struct ofpbuf *odp_actions;
2725 struct facet *facet;
2728 /* First look for a related facet. If we find one, account it to that. */
2729 facet = facet_lookup_valid(ofproto, flow);
2730 if (facet && facet->rule == rule) {
2731 facet_execute(ofproto, facet, packet);
2735 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2736 * create a new facet for it and use that. */
2737 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
2738 facet = facet_create(rule, flow, packet);
2739 facet_execute(ofproto, facet, packet);
2740 facet_install(ofproto, facet, true);
2744 /* We can't account anything to a facet. If we were to try, then that
2745 * facet would have a non-matching rule, busting our invariants. */
2746 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2747 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2748 size = packet->size;
2749 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2750 odp_actions->size, packet)) {
2751 rule->used = time_msec();
2752 rule->packet_count++;
2753 rule->byte_count += size;
2754 flow_push_stats(rule, flow, 1, size, rule->used);
2756 ofpbuf_delete(odp_actions);
2762 rule_modify_actions(struct rule *rule_)
2764 struct rule_dpif *rule = rule_dpif_cast(rule_);
2765 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2768 error = validate_actions(rule->up.actions, rule->up.n_actions,
2769 &rule->up.cr.flow, ofproto->max_ports);
2771 ofoperation_complete(rule->up.pending, error);
2775 complete_operation(rule);
2778 /* Sends 'packet' out of port 'odp_port' within 'p'.
2779 * Returns 0 if successful, otherwise a positive errno value. */
2781 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2782 const struct ofpbuf *packet)
2784 struct ofpbuf key, odp_actions;
2785 struct odputil_keybuf keybuf;
2789 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2790 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2791 odp_flow_key_from_flow(&key, &flow);
2793 ofpbuf_init(&odp_actions, 32);
2794 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2795 error = dpif_execute(ofproto->dpif,
2797 odp_actions.data, odp_actions.size,
2799 ofpbuf_uninit(&odp_actions);
2802 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2803 ofproto->up.name, odp_port, strerror(error));
2808 /* OpenFlow to datapath action translation. */
2810 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2811 struct action_xlate_ctx *ctx);
2812 static void xlate_normal(struct action_xlate_ctx *);
2815 commit_odp_actions(struct action_xlate_ctx *ctx)
2817 const struct flow *flow = &ctx->flow;
2818 struct flow *base = &ctx->base_flow;
2819 struct ofpbuf *odp_actions = ctx->odp_actions;
2821 if (base->tun_id != flow->tun_id) {
2822 nl_msg_put_be64(odp_actions, OVS_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
2823 base->tun_id = flow->tun_id;
2826 if (base->nw_src != flow->nw_src) {
2827 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
2828 base->nw_src = flow->nw_src;
2831 if (base->nw_dst != flow->nw_dst) {
2832 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
2833 base->nw_dst = flow->nw_dst;
2836 if (base->nw_tos != flow->nw_tos) {
2837 nl_msg_put_u8(odp_actions, OVS_ACTION_ATTR_SET_NW_TOS, flow->nw_tos);
2838 base->nw_tos = flow->nw_tos;
2841 if (base->vlan_tci != flow->vlan_tci) {
2842 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2843 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_STRIP_VLAN);
2845 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_DL_TCI,
2846 flow->vlan_tci & ~htons(VLAN_CFI));
2848 base->vlan_tci = flow->vlan_tci;
2851 if (base->tp_src != flow->tp_src) {
2852 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
2853 base->tp_src = flow->tp_src;
2856 if (base->tp_dst != flow->tp_dst) {
2857 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
2858 base->tp_dst = flow->tp_dst;
2861 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
2862 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_SRC,
2863 flow->dl_src, ETH_ADDR_LEN);
2864 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2867 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2868 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_DST,
2869 flow->dl_dst, ETH_ADDR_LEN);
2870 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2873 if (ctx->base_priority != ctx->priority) {
2874 if (ctx->priority) {
2875 nl_msg_put_u32(odp_actions, OVS_ACTION_ATTR_SET_PRIORITY,
2878 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
2880 ctx->base_priority = ctx->priority;
2885 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2887 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2888 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2891 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2892 /* Forwarding disabled on port. */
2897 * We don't have an ofport record for this port, but it doesn't hurt to
2898 * allow forwarding to it anyhow. Maybe such a port will appear later
2899 * and we're pre-populating the flow table.
2903 commit_odp_actions(ctx);
2904 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2905 ctx->nf_output_iface = ofp_port;
2909 xlate_table_action(struct action_xlate_ctx *ctx,
2910 uint16_t in_port, uint8_t table_id)
2912 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2913 struct rule_dpif *rule;
2914 uint16_t old_in_port;
2915 uint8_t old_table_id;
2917 old_table_id = ctx->table_id;
2918 ctx->table_id = table_id;
2920 /* Look up a flow with 'in_port' as the input port. Then restore the
2921 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2922 * have surprising behavior). */
2923 old_in_port = ctx->flow.in_port;
2924 ctx->flow.in_port = in_port;
2925 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow, table_id);
2926 ctx->flow.in_port = old_in_port;
2928 if (ctx->resubmit_hook) {
2929 ctx->resubmit_hook(ctx, rule);
2934 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2938 ctx->table_id = old_table_id;
2940 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2942 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
2943 MAX_RESUBMIT_RECURSION);
2948 xlate_resubmit_table(struct action_xlate_ctx *ctx,
2949 const struct nx_action_resubmit *nar)
2954 in_port = (nar->in_port == htons(OFPP_IN_PORT)
2956 : ntohs(nar->in_port));
2957 table_id = nar->table == 255 ? ctx->table_id : nar->table;
2959 xlate_table_action(ctx, in_port, table_id);
2963 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
2965 struct ofport_dpif *ofport;
2967 commit_odp_actions(ctx);
2968 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
2969 uint16_t ofp_port = ofport->up.ofp_port;
2970 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
2971 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT,
2976 ctx->nf_output_iface = NF_OUT_FLOOD;
2980 xlate_output_action__(struct action_xlate_ctx *ctx,
2981 uint16_t port, uint16_t max_len)
2983 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2985 ctx->nf_output_iface = NF_OUT_DROP;
2989 add_output_action(ctx, ctx->flow.in_port);
2992 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
2998 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3001 flood_packets(ctx, htonl(0));
3003 case OFPP_CONTROLLER:
3004 commit_odp_actions(ctx);
3005 nl_msg_put_u64(ctx->odp_actions, OVS_ACTION_ATTR_USERSPACE, max_len);
3008 add_output_action(ctx, OFPP_LOCAL);
3013 if (port != ctx->flow.in_port) {
3014 add_output_action(ctx, port);
3019 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3020 ctx->nf_output_iface = NF_OUT_FLOOD;
3021 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3022 ctx->nf_output_iface = prev_nf_output_iface;
3023 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3024 ctx->nf_output_iface != NF_OUT_FLOOD) {
3025 ctx->nf_output_iface = NF_OUT_MULTI;
3030 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3031 const struct nx_action_output_reg *naor)
3035 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3037 if (ofp_port <= UINT16_MAX) {
3038 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3043 xlate_output_action(struct action_xlate_ctx *ctx,
3044 const struct ofp_action_output *oao)
3046 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3050 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3051 const struct ofp_action_enqueue *oae)
3053 uint16_t ofp_port, odp_port;
3054 uint32_t ctx_priority, priority;
3057 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3060 /* Fall back to ordinary output action. */
3061 xlate_output_action__(ctx, ntohs(oae->port), 0);
3065 /* Figure out datapath output port. */
3066 ofp_port = ntohs(oae->port);
3067 if (ofp_port == OFPP_IN_PORT) {
3068 ofp_port = ctx->flow.in_port;
3070 odp_port = ofp_port_to_odp_port(ofp_port);
3072 /* Add datapath actions. */
3073 ctx_priority = ctx->priority;
3074 ctx->priority = priority;
3075 add_output_action(ctx, odp_port);
3076 ctx->priority = ctx_priority;
3078 /* Update NetFlow output port. */
3079 if (ctx->nf_output_iface == NF_OUT_DROP) {
3080 ctx->nf_output_iface = odp_port;
3081 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3082 ctx->nf_output_iface = NF_OUT_MULTI;
3087 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3088 const struct nx_action_set_queue *nasq)
3093 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3096 /* Couldn't translate queue to a priority, so ignore. A warning
3097 * has already been logged. */
3101 ctx->priority = priority;
3104 struct xlate_reg_state {
3110 xlate_autopath(struct action_xlate_ctx *ctx,
3111 const struct nx_action_autopath *naa)
3113 uint16_t ofp_port = ntohl(naa->id);
3114 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3116 if (!port || !port->bundle) {
3117 ofp_port = OFPP_NONE;
3118 } else if (port->bundle->bond) {
3119 /* Autopath does not support VLAN hashing. */
3120 struct ofport_dpif *slave = bond_choose_output_slave(
3121 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3123 ofp_port = slave->up.ofp_port;
3126 autopath_execute(naa, &ctx->flow, ofp_port);
3130 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3132 struct ofproto_dpif *ofproto = ofproto_;
3133 struct ofport_dpif *port;
3143 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3146 port = get_ofp_port(ofproto, ofp_port);
3147 return port ? port->may_enable : false;
3152 do_xlate_actions(const union ofp_action *in, size_t n_in,
3153 struct action_xlate_ctx *ctx)
3155 const struct ofport_dpif *port;
3156 const union ofp_action *ia;
3159 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3161 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3162 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3163 ? htonl(OFPPC_NO_RECV_STP)
3164 : htonl(OFPPC_NO_RECV))) {
3165 /* Drop this flow. */
3169 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3170 const struct ofp_action_dl_addr *oada;
3171 const struct nx_action_resubmit *nar;
3172 const struct nx_action_set_tunnel *nast;
3173 const struct nx_action_set_queue *nasq;
3174 const struct nx_action_multipath *nam;
3175 const struct nx_action_autopath *naa;
3176 const struct nx_action_bundle *nab;
3177 const struct nx_action_output_reg *naor;
3178 enum ofputil_action_code code;
3181 code = ofputil_decode_action_unsafe(ia);
3183 case OFPUTIL_OFPAT_OUTPUT:
3184 xlate_output_action(ctx, &ia->output);
3187 case OFPUTIL_OFPAT_SET_VLAN_VID:
3188 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3189 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3192 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3193 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3194 ctx->flow.vlan_tci |= htons(
3195 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3198 case OFPUTIL_OFPAT_STRIP_VLAN:
3199 ctx->flow.vlan_tci = htons(0);
3202 case OFPUTIL_OFPAT_SET_DL_SRC:
3203 oada = ((struct ofp_action_dl_addr *) ia);
3204 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3207 case OFPUTIL_OFPAT_SET_DL_DST:
3208 oada = ((struct ofp_action_dl_addr *) ia);
3209 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3212 case OFPUTIL_OFPAT_SET_NW_SRC:
3213 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3216 case OFPUTIL_OFPAT_SET_NW_DST:
3217 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3220 case OFPUTIL_OFPAT_SET_NW_TOS:
3221 ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK;
3224 case OFPUTIL_OFPAT_SET_TP_SRC:
3225 ctx->flow.tp_src = ia->tp_port.tp_port;
3228 case OFPUTIL_OFPAT_SET_TP_DST:
3229 ctx->flow.tp_dst = ia->tp_port.tp_port;
3232 case OFPUTIL_OFPAT_ENQUEUE:
3233 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3236 case OFPUTIL_NXAST_RESUBMIT:
3237 nar = (const struct nx_action_resubmit *) ia;
3238 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
3241 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3242 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
3245 case OFPUTIL_NXAST_SET_TUNNEL:
3246 nast = (const struct nx_action_set_tunnel *) ia;
3247 tun_id = htonll(ntohl(nast->tun_id));
3248 ctx->flow.tun_id = tun_id;
3251 case OFPUTIL_NXAST_SET_QUEUE:
3252 nasq = (const struct nx_action_set_queue *) ia;
3253 xlate_set_queue_action(ctx, nasq);
3256 case OFPUTIL_NXAST_POP_QUEUE:
3260 case OFPUTIL_NXAST_REG_MOVE:
3261 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3265 case OFPUTIL_NXAST_REG_LOAD:
3266 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3270 case OFPUTIL_NXAST_NOTE:
3271 /* Nothing to do. */
3274 case OFPUTIL_NXAST_SET_TUNNEL64:
3275 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3276 ctx->flow.tun_id = tun_id;
3279 case OFPUTIL_NXAST_MULTIPATH:
3280 nam = (const struct nx_action_multipath *) ia;
3281 multipath_execute(nam, &ctx->flow);
3284 case OFPUTIL_NXAST_AUTOPATH:
3285 naa = (const struct nx_action_autopath *) ia;
3286 xlate_autopath(ctx, naa);
3289 case OFPUTIL_NXAST_BUNDLE:
3290 ctx->ofproto->has_bundle_action = true;
3291 nab = (const struct nx_action_bundle *) ia;
3292 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3297 case OFPUTIL_NXAST_BUNDLE_LOAD:
3298 ctx->ofproto->has_bundle_action = true;
3299 nab = (const struct nx_action_bundle *) ia;
3300 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3304 case OFPUTIL_NXAST_OUTPUT_REG:
3305 naor = (const struct nx_action_output_reg *) ia;
3306 xlate_output_reg_action(ctx, naor);
3313 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3314 struct ofproto_dpif *ofproto, const struct flow *flow,
3315 const struct ofpbuf *packet)
3317 ctx->ofproto = ofproto;
3319 ctx->packet = packet;
3320 ctx->resubmit_hook = NULL;
3323 static struct ofpbuf *
3324 xlate_actions(struct action_xlate_ctx *ctx,
3325 const union ofp_action *in, size_t n_in)
3327 COVERAGE_INC(ofproto_dpif_xlate);
3329 ctx->odp_actions = ofpbuf_new(512);
3331 ctx->may_set_up_flow = true;
3332 ctx->nf_output_iface = NF_OUT_DROP;
3335 ctx->base_priority = 0;
3336 ctx->base_flow = ctx->flow;
3339 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3340 ctx->may_set_up_flow = false;
3342 do_xlate_actions(in, n_in, ctx);
3345 /* Check with in-band control to see if we're allowed to set up this
3347 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3348 ctx->odp_actions->data,
3349 ctx->odp_actions->size)) {
3350 ctx->may_set_up_flow = false;
3353 return ctx->odp_actions;
3356 /* OFPP_NORMAL implementation. */
3359 struct ofport_dpif *port;
3364 struct dst builtin[32];
3366 size_t n, allocated;
3369 static void dst_set_init(struct dst_set *);
3370 static void dst_set_add(struct dst_set *, const struct dst *);
3371 static void dst_set_free(struct dst_set *);
3373 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3376 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3377 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3379 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3380 : in_bundle->vlan >= 0 ? in_bundle->vlan
3381 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3382 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3384 dst->port = (!out_bundle->bond
3385 ? ofbundle_get_a_port(out_bundle)
3386 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3387 dst->vlan, &ctx->tags));
3389 return dst->port != NULL;
3393 mirror_mask_ffs(mirror_mask_t mask)
3395 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3400 dst_set_init(struct dst_set *set)
3402 set->dsts = set->builtin;
3404 set->allocated = ARRAY_SIZE(set->builtin);
3408 dst_set_add(struct dst_set *set, const struct dst *dst)
3410 if (set->n >= set->allocated) {
3411 size_t new_allocated;
3412 struct dst *new_dsts;
3414 new_allocated = set->allocated * 2;
3415 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3416 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3420 set->dsts = new_dsts;
3421 set->allocated = new_allocated;
3423 set->dsts[set->n++] = *dst;
3427 dst_set_free(struct dst_set *set)
3429 if (set->dsts != set->builtin) {
3435 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3438 for (i = 0; i < set->n; i++) {
3439 if (set->dsts[i].vlan == test->vlan
3440 && set->dsts[i].port == test->port) {
3448 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3450 return (bundle->vlan < 0
3451 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
3455 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3457 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3460 /* Returns an arbitrary interface within 'bundle'. */
3461 static struct ofport_dpif *
3462 ofbundle_get_a_port(const struct ofbundle *bundle)
3464 return CONTAINER_OF(list_front(&bundle->ports),
3465 struct ofport_dpif, bundle_node);
3469 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3470 const struct ofbundle *in_bundle,
3471 const struct ofbundle *out_bundle, struct dst_set *set)
3475 if (out_bundle == OFBUNDLE_FLOOD) {
3476 struct ofbundle *bundle;
3478 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3479 if (bundle != in_bundle
3480 && ofbundle_includes_vlan(bundle, vlan)
3481 && bundle->floodable
3482 && !bundle->mirror_out
3483 && set_dst(ctx, &dst, in_bundle, bundle)) {
3484 dst_set_add(set, &dst);
3487 ctx->nf_output_iface = NF_OUT_FLOOD;
3488 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3489 dst_set_add(set, &dst);
3490 ctx->nf_output_iface = dst.port->odp_port;
3495 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3497 return !m->vlans || bitmap_is_set(m->vlans, vlan);
3500 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3501 * to a VLAN. In general most packets may be mirrored but we want to drop
3502 * protocols that may confuse switches. */
3504 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
3506 /* If you change this function's behavior, please update corresponding
3507 * documentation in vswitch.xml at the same time. */
3508 if (dst[0] != 0x01) {
3509 /* All the currently banned MACs happen to start with 01 currently, so
3510 * this is a quick way to eliminate most of the good ones. */
3512 if (eth_addr_is_reserved(dst)) {
3513 /* Drop STP, IEEE pause frames, and other reserved protocols
3514 * (01-80-c2-00-00-0x). */
3518 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
3520 if ((dst[3] & 0xfe) == 0xcc &&
3521 (dst[4] & 0xfe) == 0xcc &&
3522 (dst[5] & 0xfe) == 0xcc) {
3523 /* Drop the following protocols plus others following the same
3526 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3527 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3528 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3532 if (!(dst[3] | dst[4] | dst[5])) {
3533 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3542 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3543 uint16_t vlan, const struct ofbundle *in_bundle,
3544 struct dst_set *set)
3546 struct ofproto_dpif *ofproto = ctx->ofproto;
3547 mirror_mask_t mirrors;
3551 mirrors = in_bundle->src_mirrors;
3552 for (i = 0; i < set->n; i++) {
3553 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3560 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3561 if (flow_vlan == 0) {
3562 flow_vlan = OFP_VLAN_NONE;
3566 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3567 if (vlan_is_mirrored(m, vlan)) {
3571 if (set_dst(ctx, &dst, in_bundle, m->out)
3572 && !dst_is_duplicate(set, &dst)) {
3573 dst_set_add(set, &dst);
3575 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
3576 struct ofbundle *bundle;
3578 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3579 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3580 && set_dst(ctx, &dst, in_bundle, bundle))
3582 if (bundle->vlan < 0) {
3583 dst.vlan = m->out_vlan;
3585 if (dst_is_duplicate(set, &dst)) {
3589 /* Use the vlan tag on the original flow instead of
3590 * the one passed in the vlan parameter. This ensures
3591 * that we compare the vlan from before any implicit
3592 * tagging tags place. This is necessary because
3593 * dst->vlan is the final vlan, after removing implicit
3595 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3596 /* Don't send out input port on same VLAN. */
3599 dst_set_add(set, &dst);
3604 mirrors &= mirrors - 1;
3609 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3610 const struct ofbundle *in_bundle,
3611 const struct ofbundle *out_bundle)
3613 uint16_t initial_vlan, cur_vlan;
3614 const struct dst *dst;
3618 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3619 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3621 /* Output all the packets we can without having to change the VLAN. */
3622 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3623 if (initial_vlan == 0) {
3624 initial_vlan = OFP_VLAN_NONE;
3626 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3627 if (dst->vlan != initial_vlan) {
3630 nl_msg_put_u32(ctx->odp_actions,
3631 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3634 /* Then output the rest. */
3635 cur_vlan = initial_vlan;
3636 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3637 if (dst->vlan == initial_vlan) {
3640 if (dst->vlan != cur_vlan) {
3641 if (dst->vlan == OFP_VLAN_NONE) {
3642 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_STRIP_VLAN);
3645 tci = htons(dst->vlan & VLAN_VID_MASK);
3646 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3647 nl_msg_put_be16(ctx->odp_actions,
3648 OVS_ACTION_ATTR_SET_DL_TCI, tci);
3650 cur_vlan = dst->vlan;
3652 nl_msg_put_u32(ctx->odp_actions,
3653 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3659 /* Returns the effective vlan of a packet, taking into account both the
3660 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3661 * the packet is untagged and -1 indicates it has an invalid header and
3662 * should be dropped. */
3664 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3665 struct ofbundle *in_bundle, bool have_packet)
3667 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3668 if (in_bundle->vlan >= 0) {
3671 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3672 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3673 "packet received on port %s configured with "
3674 "implicit VLAN %"PRIu16,
3675 ofproto->up.name, vlan,
3676 in_bundle->name, in_bundle->vlan);
3680 vlan = in_bundle->vlan;
3682 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3684 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3685 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3686 "packet received on port %s not configured for "
3688 ofproto->up.name, vlan, in_bundle->name, vlan);
3697 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3698 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3699 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3701 is_gratuitous_arp(const struct flow *flow)
3703 return (flow->dl_type == htons(ETH_TYPE_ARP)
3704 && eth_addr_is_broadcast(flow->dl_dst)
3705 && (flow->nw_proto == ARP_OP_REPLY
3706 || (flow->nw_proto == ARP_OP_REQUEST
3707 && flow->nw_src == flow->nw_dst)));
3711 update_learning_table(struct ofproto_dpif *ofproto,
3712 const struct flow *flow, int vlan,
3713 struct ofbundle *in_bundle)
3715 struct mac_entry *mac;
3717 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3721 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3722 if (is_gratuitous_arp(flow)) {
3723 /* We don't want to learn from gratuitous ARP packets that are
3724 * reflected back over bond slaves so we lock the learning table. */
3725 if (!in_bundle->bond) {
3726 mac_entry_set_grat_arp_lock(mac);
3727 } else if (mac_entry_is_grat_arp_locked(mac)) {
3732 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3733 /* The log messages here could actually be useful in debugging,
3734 * so keep the rate limit relatively high. */
3735 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3736 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3737 "on port %s in VLAN %d",
3738 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3739 in_bundle->name, vlan);
3741 mac->port.p = in_bundle;
3742 tag_set_add(&ofproto->revalidate_set,
3743 mac_learning_changed(ofproto->ml, mac));
3747 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3748 * dropped. Returns true if they may be forwarded, false if they should be
3751 * If 'have_packet' is true, it indicates that the caller is processing a
3752 * received packet. If 'have_packet' is false, then the caller is just
3753 * revalidating an existing flow because configuration has changed. Either
3754 * way, 'have_packet' only affects logging (there is no point in logging errors
3755 * during revalidation).
3757 * Sets '*in_portp' to the input port. This will be a null pointer if
3758 * flow->in_port does not designate a known input port (in which case
3759 * is_admissible() returns false).
3761 * When returning true, sets '*vlanp' to the effective VLAN of the input
3762 * packet, as returned by flow_get_vlan().
3764 * May also add tags to '*tags', although the current implementation only does
3765 * so in one special case.
3768 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3770 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3772 struct ofport_dpif *in_port;
3773 struct ofbundle *in_bundle;
3776 /* Find the port and bundle for the received packet. */
3777 in_port = get_ofp_port(ofproto, flow->in_port);
3778 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3779 if (!in_port || !in_bundle) {
3780 /* No interface? Something fishy... */
3782 /* Odd. A few possible reasons here:
3784 * - We deleted a port but there are still a few packets queued up
3787 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3788 * we don't know about.
3790 * - Packet arrived on the local port but the local port is not
3793 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3795 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3797 ofproto->up.name, flow->in_port);
3801 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3806 /* Drop frames for reserved multicast addresses
3807 * only if forward_bpdu option is absent. */
3808 if (eth_addr_is_reserved(flow->dl_dst) &&
3809 !ofproto->up.forward_bpdu) {
3813 /* Drop frames on bundles reserved for mirroring. */
3814 if (in_bundle->mirror_out) {
3816 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3817 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3818 "%s, which is reserved exclusively for mirroring",
3819 ofproto->up.name, in_bundle->name);
3824 if (in_bundle->bond) {
3825 struct mac_entry *mac;
3827 switch (bond_check_admissibility(in_bundle->bond, in_port,
3828 flow->dl_dst, tags)) {
3835 case BV_DROP_IF_MOVED:
3836 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3837 if (mac && mac->port.p != in_bundle &&
3838 (!is_gratuitous_arp(flow)
3839 || mac_entry_is_grat_arp_locked(mac))) {
3850 xlate_normal(struct action_xlate_ctx *ctx)
3852 struct ofbundle *in_bundle;
3853 struct ofbundle *out_bundle;
3854 struct mac_entry *mac;
3857 /* Check whether we should drop packets in this flow. */
3858 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3859 &ctx->tags, &vlan, &in_bundle)) {
3864 /* Learn source MAC (but don't try to learn from revalidation). */
3866 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3869 /* Determine output bundle. */
3870 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3873 out_bundle = mac->port.p;
3874 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3875 /* If we are revalidating but don't have a learning entry then eject
3876 * the flow. Installing a flow that floods packets opens up a window
3877 * of time where we could learn from a packet reflected on a bond and
3878 * blackhole packets before the learning table is updated to reflect
3879 * the correct port. */
3880 ctx->may_set_up_flow = false;
3883 out_bundle = OFBUNDLE_FLOOD;
3886 /* Don't send packets out their input bundles. */
3887 if (in_bundle == out_bundle) {
3893 compose_actions(ctx, vlan, in_bundle, out_bundle);
3898 get_drop_frags(struct ofproto *ofproto_)
3900 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3903 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3908 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3910 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3912 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3916 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3917 const struct flow *flow,
3918 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3920 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3923 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3924 ofproto->max_ports);
3926 struct odputil_keybuf keybuf;
3927 struct action_xlate_ctx ctx;
3928 struct ofpbuf *odp_actions;
3931 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3932 odp_flow_key_from_flow(&key, flow);
3934 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3935 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3936 dpif_execute(ofproto->dpif, key.data, key.size,
3937 odp_actions->data, odp_actions->size, packet);
3938 ofpbuf_delete(odp_actions);
3944 get_netflow_ids(const struct ofproto *ofproto_,
3945 uint8_t *engine_type, uint8_t *engine_id)
3947 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3949 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3952 static struct ofproto_dpif *
3953 ofproto_dpif_lookup(const char *name)
3955 struct ofproto *ofproto = ofproto_lookup(name);
3956 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3957 ? ofproto_dpif_cast(ofproto)
3962 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3963 const char *args, void *aux OVS_UNUSED)
3965 struct ds ds = DS_EMPTY_INITIALIZER;
3966 const struct ofproto_dpif *ofproto;
3967 const struct mac_entry *e;
3969 ofproto = ofproto_dpif_lookup(args);
3971 unixctl_command_reply(conn, 501, "no such bridge");
3975 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3976 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3977 struct ofbundle *bundle = e->port.p;
3978 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
3979 ofbundle_get_a_port(bundle)->odp_port,
3980 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3982 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3986 struct ofproto_trace {
3987 struct action_xlate_ctx ctx;
3993 trace_format_rule(struct ds *result, uint8_t table_id, int level,
3994 const struct rule_dpif *rule)
3996 ds_put_char_multiple(result, '\t', level);
3998 ds_put_cstr(result, "No match\n");
4002 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4003 table_id, ntohll(rule->up.flow_cookie));
4004 cls_rule_format(&rule->up.cr, result);
4005 ds_put_char(result, '\n');
4007 ds_put_char_multiple(result, '\t', level);
4008 ds_put_cstr(result, "OpenFlow ");
4009 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
4010 ds_put_char(result, '\n');
4014 trace_format_flow(struct ds *result, int level, const char *title,
4015 struct ofproto_trace *trace)
4017 ds_put_char_multiple(result, '\t', level);
4018 ds_put_format(result, "%s: ", title);
4019 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4020 ds_put_cstr(result, "unchanged");
4022 flow_format(result, &trace->ctx.flow);
4023 trace->flow = trace->ctx.flow;
4025 ds_put_char(result, '\n');
4029 trace_format_regs(struct ds *result, int level, const char *title,
4030 struct ofproto_trace *trace)
4034 ds_put_char_multiple(result, '\t', level);
4035 ds_put_format(result, "%s:", title);
4036 for (i = 0; i < FLOW_N_REGS; i++) {
4037 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
4039 ds_put_char(result, '\n');
4043 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
4045 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4046 struct ds *result = trace->result;
4048 ds_put_char(result, '\n');
4049 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4050 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
4051 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
4055 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4056 void *aux OVS_UNUSED)
4058 char *dpname, *arg1, *arg2, *arg3;
4059 char *args = xstrdup(args_);
4060 char *save_ptr = NULL;
4061 struct ofproto_dpif *ofproto;
4062 struct ofpbuf odp_key;
4063 struct ofpbuf *packet;
4064 struct rule_dpif *rule;
4070 ofpbuf_init(&odp_key, 0);
4073 dpname = strtok_r(args, " ", &save_ptr);
4074 arg1 = strtok_r(NULL, " ", &save_ptr);
4075 arg2 = strtok_r(NULL, " ", &save_ptr);
4076 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4077 if (dpname && arg1 && !arg2 && !arg3) {
4078 /* ofproto/trace dpname flow */
4081 /* Convert string to datapath key. */
4082 ofpbuf_init(&odp_key, 0);
4083 error = odp_flow_key_from_string(arg1, &odp_key);
4085 unixctl_command_reply(conn, 501, "Bad flow syntax");
4089 /* Convert odp_key to flow. */
4090 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
4092 unixctl_command_reply(conn, 501, "Invalid flow");
4095 } else if (dpname && arg1 && arg2 && arg3) {
4096 /* ofproto/trace dpname tun_id in_port packet */
4100 tun_id = htonll(strtoull(arg1, NULL, 0));
4101 in_port = ofp_port_to_odp_port(atoi(arg2));
4103 packet = ofpbuf_new(strlen(args) / 2);
4104 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
4105 arg3 += strspn(arg3, " ");
4106 if (*arg3 != '\0') {
4107 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4110 if (packet->size < ETH_HEADER_LEN) {
4111 unixctl_command_reply(conn, 501,
4112 "Packet data too short for Ethernet");
4116 ds_put_cstr(&result, "Packet: ");
4117 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
4118 ds_put_cstr(&result, s);
4121 flow_extract(packet, tun_id, in_port, &flow);
4123 unixctl_command_reply(conn, 501, "Bad command syntax");
4127 ofproto = ofproto_dpif_lookup(dpname);
4129 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4134 ds_put_cstr(&result, "Flow: ");
4135 flow_format(&result, &flow);
4136 ds_put_char(&result, '\n');
4138 rule = rule_dpif_lookup(ofproto, &flow, 0);
4139 trace_format_rule(&result, 0, 0, rule);
4141 struct ofproto_trace trace;
4142 struct ofpbuf *odp_actions;
4144 trace.result = &result;
4146 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
4147 trace.ctx.resubmit_hook = trace_resubmit;
4148 odp_actions = xlate_actions(&trace.ctx,
4149 rule->up.actions, rule->up.n_actions);
4151 ds_put_char(&result, '\n');
4152 trace_format_flow(&result, 0, "Final flow", &trace);
4153 ds_put_cstr(&result, "Datapath actions: ");
4154 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4155 ofpbuf_delete(odp_actions);
4157 if (!trace.ctx.may_set_up_flow) {
4159 ds_put_cstr(&result, "\nThis flow is not cachable.");
4161 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
4162 "for complete actions, please supply a packet.");
4167 unixctl_command_reply(conn, 200, ds_cstr(&result));
4170 ds_destroy(&result);
4171 ofpbuf_delete(packet);
4172 ofpbuf_uninit(&odp_key);
4177 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
4178 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4181 unixctl_command_reply(conn, 200, NULL);
4185 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
4186 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4189 unixctl_command_reply(conn, 200, NULL);
4193 ofproto_dpif_unixctl_init(void)
4195 static bool registered;
4201 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4202 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
4204 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
4205 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
4208 const struct ofproto_class ofproto_dpif_class = {
4235 port_is_lacp_current,
4236 NULL, /* rule_choose_table */
4243 rule_modify_actions,
4256 is_mirror_output_bundle,
4257 forward_bpdu_changed,