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"
36 #include "mac-learning.h"
37 #include "multipath.h"
44 #include "ofp-print.h"
45 #include "ofproto-dpif-sflow.h"
46 #include "poll-loop.h"
48 #include "unaligned.h"
50 #include "vlan-bitmap.h"
53 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
55 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
56 COVERAGE_DEFINE(ofproto_dpif_expired);
57 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
58 COVERAGE_DEFINE(ofproto_dpif_xlate);
59 COVERAGE_DEFINE(facet_changed_rule);
60 COVERAGE_DEFINE(facet_invalidated);
61 COVERAGE_DEFINE(facet_revalidate);
62 COVERAGE_DEFINE(facet_unexpected);
64 /* Maximum depth of flow table recursion (due to resubmit actions) in a
65 * flow translation. */
66 #define MAX_RESUBMIT_RECURSION 16
74 long long int used; /* Time last used; time created if not used. */
78 * - Do include packets and bytes from facets that have been deleted or
79 * whose own statistics have been folded into the rule.
81 * - Do include packets and bytes sent "by hand" that were accounted to
82 * the rule without any facet being involved (this is a rare corner
83 * case in rule_execute()).
85 * - Do not include packet or bytes that can be obtained from any facet's
86 * packet_count or byte_count member or that can be obtained from the
87 * datapath by, e.g., dpif_flow_get() for any facet.
89 uint64_t packet_count; /* Number of packets received. */
90 uint64_t byte_count; /* Number of bytes received. */
92 struct list facets; /* List of "struct facet"s. */
95 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
97 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
100 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
101 const struct flow *, uint8_t table);
103 #define MAX_MIRRORS 32
104 typedef uint32_t mirror_mask_t;
105 #define MIRROR_MASK_C(X) UINT32_C(X)
106 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
108 struct ofproto_dpif *ofproto; /* Owning ofproto. */
109 size_t idx; /* In ofproto's "mirrors" array. */
110 void *aux; /* Key supplied by ofproto's client. */
111 char *name; /* Identifier for log messages. */
113 /* Selection criteria. */
114 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
115 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
116 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
118 /* Output (mutually exclusive). */
119 struct ofbundle *out; /* Output port or NULL. */
120 int out_vlan; /* Output VLAN or -1. */
123 static void mirror_destroy(struct ofmirror *);
125 /* A group of one or more OpenFlow ports. */
126 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
128 struct ofproto_dpif *ofproto; /* Owning ofproto. */
129 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
130 void *aux; /* Key supplied by ofproto's client. */
131 char *name; /* Identifier for log messages. */
134 struct list ports; /* Contains "struct ofport"s. */
135 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
136 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
137 * NULL if all VLANs are trunked. */
138 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
139 struct bond *bond; /* Nonnull iff more than one port. */
142 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
144 /* Port mirroring info. */
145 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
146 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
147 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
150 static void bundle_remove(struct ofport *);
151 static void bundle_destroy(struct ofbundle *);
152 static void bundle_del_port(struct ofport_dpif *);
153 static void bundle_run(struct ofbundle *);
154 static void bundle_wait(struct ofbundle *);
156 struct action_xlate_ctx {
157 /* action_xlate_ctx_init() initializes these members. */
160 struct ofproto_dpif *ofproto;
162 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
163 * this flow when actions change header fields. */
166 /* The packet corresponding to 'flow', or a null pointer if we are
167 * revalidating without a packet to refer to. */
168 const struct ofpbuf *packet;
170 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
171 * want to execute them if we are actually processing a packet, or if we
172 * are accounting for packets that the datapath has processed, but not if
173 * we are just revalidating. */
176 /* If nonnull, called just before executing a resubmit action.
178 * This is normally null so the client has to set it manually after
179 * calling action_xlate_ctx_init(). */
180 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
182 /* xlate_actions() initializes and uses these members. The client might want
183 * to look at them after it returns. */
185 struct ofpbuf *odp_actions; /* Datapath actions. */
186 tag_type tags; /* Tags associated with actions. */
187 bool may_set_up_flow; /* True ordinarily; false if the actions must
188 * be reassessed for every packet. */
189 bool has_learn; /* Actions include NXAST_LEARN? */
190 bool has_normal; /* Actions output to OFPP_NORMAL? */
191 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
193 /* xlate_actions() initializes and uses these members, but the client has no
194 * reason to look at them. */
196 int recurse; /* Recursion level, via xlate_table_action. */
197 uint32_t priority; /* Current flow priority. 0 if none. */
198 struct flow base_flow; /* Flow at the last commit. */
199 uint32_t base_priority; /* Priority at the last commit. */
200 uint8_t table_id; /* OpenFlow table ID where flow was found. */
203 static void action_xlate_ctx_init(struct action_xlate_ctx *,
204 struct ofproto_dpif *, const struct flow *,
205 const struct ofpbuf *);
206 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
207 const union ofp_action *in, size_t n_in);
209 /* An exact-match instantiation of an OpenFlow flow. */
211 long long int used; /* Time last used; time created if not used. */
215 * - Do include packets and bytes sent "by hand", e.g. with
218 * - Do include packets and bytes that were obtained from the datapath
219 * when its statistics were reset (e.g. dpif_flow_put() with
220 * DPIF_FP_ZERO_STATS).
222 uint64_t packet_count; /* Number of packets received. */
223 uint64_t byte_count; /* Number of bytes received. */
225 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
226 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
228 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
229 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
230 long long int rs_used; /* Used time pushed to resubmit children. */
232 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
234 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
235 struct list list_node; /* In owning rule's 'facets' list. */
236 struct rule_dpif *rule; /* Owning rule. */
237 struct flow flow; /* Exact-match flow. */
238 bool installed; /* Installed in datapath? */
239 bool may_install; /* True ordinarily; false if actions must
240 * be reassessed for every packet. */
241 bool has_learn; /* Actions include NXAST_LEARN? */
242 bool has_normal; /* Actions output to OFPP_NORMAL? */
243 size_t actions_len; /* Number of bytes in actions[]. */
244 struct nlattr *actions; /* Datapath actions. */
245 tag_type tags; /* Tags. */
246 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
249 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
250 const struct ofpbuf *packet);
251 static void facet_remove(struct ofproto_dpif *, struct facet *);
252 static void facet_free(struct facet *);
254 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
255 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
256 const struct flow *);
257 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
259 static void facet_execute(struct ofproto_dpif *, struct facet *,
260 struct ofpbuf *packet);
262 static int facet_put__(struct ofproto_dpif *, struct facet *,
263 const struct nlattr *actions, size_t actions_len,
264 struct dpif_flow_stats *);
265 static void facet_install(struct ofproto_dpif *, struct facet *,
267 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
268 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
270 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
271 const struct ofpbuf *packet);
272 static void facet_update_time(struct ofproto_dpif *, struct facet *,
274 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
275 const struct dpif_flow_stats *);
276 static void facet_reset_counters(struct facet *);
277 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
278 static void facet_push_stats(struct facet *);
279 static void facet_account(struct ofproto_dpif *, struct facet *);
281 static bool facet_is_controller_flow(struct facet *);
283 static void flow_push_stats(const struct rule_dpif *,
284 struct flow *, uint64_t packets, uint64_t bytes,
291 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
292 struct list bundle_node; /* In struct ofbundle's "ports" list. */
293 struct cfm *cfm; /* Connectivity Fault Management, if any. */
294 tag_type tag; /* Tag associated with this port. */
295 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
296 bool may_enable; /* May be enabled in bonds. */
299 static struct ofport_dpif *
300 ofport_dpif_cast(const struct ofport *ofport)
302 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
303 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
306 static void port_run(struct ofport_dpif *);
307 static void port_wait(struct ofport_dpif *);
308 static int set_cfm(struct ofport *, const struct cfm_settings *);
310 struct dpif_completion {
311 struct list list_node;
312 struct ofoperation *op;
315 struct ofproto_dpif {
324 struct netflow *netflow;
325 struct dpif_sflow *sflow;
326 struct hmap bundles; /* Contains "struct ofbundle"s. */
327 struct mac_learning *ml;
328 struct ofmirror *mirrors[MAX_MIRRORS];
329 bool has_bonded_bundles;
332 struct timer next_expiration;
336 bool need_revalidate;
337 struct tag_set revalidate_set;
339 /* Support for debugging async flow mods. */
340 struct list completions;
342 bool has_bundle_action; /* True when the first bundle action appears. */
345 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
346 * for debugging the asynchronous flow_mod implementation.) */
349 static void ofproto_dpif_unixctl_init(void);
351 static struct ofproto_dpif *
352 ofproto_dpif_cast(const struct ofproto *ofproto)
354 assert(ofproto->ofproto_class == &ofproto_dpif_class);
355 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
358 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
360 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
363 /* Packet processing. */
364 static void update_learning_table(struct ofproto_dpif *,
365 const struct flow *, int vlan,
367 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
368 bool have_packet, tag_type *, int *vlanp,
369 struct ofbundle **in_bundlep);
370 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
372 /* Flow expiration. */
373 static int expire(struct ofproto_dpif *);
376 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
377 const struct ofpbuf *packet);
379 /* Global variables. */
380 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
382 /* Factory functions. */
385 enumerate_types(struct sset *types)
387 dp_enumerate_types(types);
391 enumerate_names(const char *type, struct sset *names)
393 return dp_enumerate_names(type, names);
397 del(const char *type, const char *name)
402 error = dpif_open(name, type, &dpif);
404 error = dpif_delete(dpif);
410 /* Basic life-cycle. */
412 static struct ofproto *
415 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
420 dealloc(struct ofproto *ofproto_)
422 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
427 construct(struct ofproto *ofproto_, int *n_tablesp)
429 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
430 const char *name = ofproto->up.name;
434 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
436 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
440 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
441 ofproto->n_matches = 0;
443 error = dpif_recv_set_mask(ofproto->dpif,
444 ((1u << DPIF_UC_MISS) |
445 (1u << DPIF_UC_ACTION) |
446 (1u << DPIF_UC_SAMPLE)));
448 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
449 dpif_close(ofproto->dpif);
452 dpif_flow_flush(ofproto->dpif);
453 dpif_recv_purge(ofproto->dpif);
455 ofproto->netflow = NULL;
456 ofproto->sflow = NULL;
457 hmap_init(&ofproto->bundles);
458 ofproto->ml = mac_learning_create();
459 for (i = 0; i < MAX_MIRRORS; i++) {
460 ofproto->mirrors[i] = NULL;
462 ofproto->has_bonded_bundles = false;
464 timer_set_duration(&ofproto->next_expiration, 1000);
466 hmap_init(&ofproto->facets);
467 ofproto->need_revalidate = false;
468 tag_set_init(&ofproto->revalidate_set);
470 list_init(&ofproto->completions);
472 ofproto_dpif_unixctl_init();
474 ofproto->has_bundle_action = false;
481 complete_operations(struct ofproto_dpif *ofproto)
483 struct dpif_completion *c, *next;
485 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
486 ofoperation_complete(c->op, 0);
487 list_remove(&c->list_node);
493 destruct(struct ofproto *ofproto_)
495 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
496 struct rule_dpif *rule, *next_rule;
497 struct classifier *table;
500 complete_operations(ofproto);
502 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
503 struct cls_cursor cursor;
505 cls_cursor_init(&cursor, table, NULL);
506 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
507 ofproto_rule_destroy(&rule->up);
511 for (i = 0; i < MAX_MIRRORS; i++) {
512 mirror_destroy(ofproto->mirrors[i]);
515 netflow_destroy(ofproto->netflow);
516 dpif_sflow_destroy(ofproto->sflow);
517 hmap_destroy(&ofproto->bundles);
518 mac_learning_destroy(ofproto->ml);
520 hmap_destroy(&ofproto->facets);
522 dpif_close(ofproto->dpif);
526 run(struct ofproto *ofproto_)
528 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
529 struct ofport_dpif *ofport;
530 struct ofbundle *bundle;
534 complete_operations(ofproto);
536 dpif_run(ofproto->dpif);
538 for (i = 0; i < 50; i++) {
539 struct dpif_upcall packet;
542 error = dpif_recv(ofproto->dpif, &packet);
544 if (error == ENODEV) {
545 /* Datapath destroyed. */
551 handle_upcall(ofproto, &packet);
554 if (timer_expired(&ofproto->next_expiration)) {
555 int delay = expire(ofproto);
556 timer_set_duration(&ofproto->next_expiration, delay);
559 if (ofproto->netflow) {
560 netflow_run(ofproto->netflow);
562 if (ofproto->sflow) {
563 dpif_sflow_run(ofproto->sflow);
566 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
569 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
573 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
575 /* Now revalidate if there's anything to do. */
576 if (ofproto->need_revalidate
577 || !tag_set_is_empty(&ofproto->revalidate_set)) {
578 struct tag_set revalidate_set = ofproto->revalidate_set;
579 bool revalidate_all = ofproto->need_revalidate;
580 struct facet *facet, *next;
582 /* Clear the revalidation flags. */
583 tag_set_init(&ofproto->revalidate_set);
584 ofproto->need_revalidate = false;
586 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
588 || tag_set_intersects(&revalidate_set, facet->tags)) {
589 facet_revalidate(ofproto, facet);
598 wait(struct ofproto *ofproto_)
600 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
601 struct ofport_dpif *ofport;
602 struct ofbundle *bundle;
604 if (!clogged && !list_is_empty(&ofproto->completions)) {
605 poll_immediate_wake();
608 dpif_wait(ofproto->dpif);
609 dpif_recv_wait(ofproto->dpif);
610 if (ofproto->sflow) {
611 dpif_sflow_wait(ofproto->sflow);
613 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
614 poll_immediate_wake();
616 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
619 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
622 mac_learning_wait(ofproto->ml);
623 if (ofproto->need_revalidate) {
624 /* Shouldn't happen, but if it does just go around again. */
625 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
626 poll_immediate_wake();
628 timer_wait(&ofproto->next_expiration);
633 flush(struct ofproto *ofproto_)
635 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
636 struct facet *facet, *next_facet;
638 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
639 /* Mark the facet as not installed so that facet_remove() doesn't
640 * bother trying to uninstall it. There is no point in uninstalling it
641 * individually since we are about to blow away all the facets with
642 * dpif_flow_flush(). */
643 facet->installed = false;
644 facet->dp_packet_count = 0;
645 facet->dp_byte_count = 0;
646 facet_remove(ofproto, facet);
648 dpif_flow_flush(ofproto->dpif);
652 get_features(struct ofproto *ofproto_ OVS_UNUSED,
653 bool *arp_match_ip, uint32_t *actions)
655 *arp_match_ip = true;
656 *actions = ((1u << OFPAT_OUTPUT) |
657 (1u << OFPAT_SET_VLAN_VID) |
658 (1u << OFPAT_SET_VLAN_PCP) |
659 (1u << OFPAT_STRIP_VLAN) |
660 (1u << OFPAT_SET_DL_SRC) |
661 (1u << OFPAT_SET_DL_DST) |
662 (1u << OFPAT_SET_NW_SRC) |
663 (1u << OFPAT_SET_NW_DST) |
664 (1u << OFPAT_SET_NW_TOS) |
665 (1u << OFPAT_SET_TP_SRC) |
666 (1u << OFPAT_SET_TP_DST) |
667 (1u << OFPAT_ENQUEUE));
671 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
673 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
674 struct ovs_dp_stats s;
676 strcpy(ots->name, "classifier");
678 dpif_get_dp_stats(ofproto->dpif, &s);
679 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
680 put_32aligned_be64(&ots->matched_count,
681 htonll(s.n_hit + ofproto->n_matches));
685 set_netflow(struct ofproto *ofproto_,
686 const struct netflow_options *netflow_options)
688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
690 if (netflow_options) {
691 if (!ofproto->netflow) {
692 ofproto->netflow = netflow_create();
694 return netflow_set_options(ofproto->netflow, netflow_options);
696 netflow_destroy(ofproto->netflow);
697 ofproto->netflow = NULL;
702 static struct ofport *
705 struct ofport_dpif *port = xmalloc(sizeof *port);
710 port_dealloc(struct ofport *port_)
712 struct ofport_dpif *port = ofport_dpif_cast(port_);
717 port_construct(struct ofport *port_)
719 struct ofport_dpif *port = ofport_dpif_cast(port_);
720 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
722 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
725 port->tag = tag_create_random();
726 port->may_enable = true;
728 if (ofproto->sflow) {
729 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
730 netdev_get_name(port->up.netdev));
737 port_destruct(struct ofport *port_)
739 struct ofport_dpif *port = ofport_dpif_cast(port_);
740 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
742 bundle_remove(port_);
743 set_cfm(port_, NULL);
744 if (ofproto->sflow) {
745 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
750 port_modified(struct ofport *port_)
752 struct ofport_dpif *port = ofport_dpif_cast(port_);
754 if (port->bundle && port->bundle->bond) {
755 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
760 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
762 struct ofport_dpif *port = ofport_dpif_cast(port_);
763 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
764 ovs_be32 changed = old_config ^ port->up.opp.config;
766 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
767 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
768 ofproto->need_revalidate = true;
773 set_sflow(struct ofproto *ofproto_,
774 const struct ofproto_sflow_options *sflow_options)
776 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
777 struct dpif_sflow *ds = ofproto->sflow;
780 struct ofport_dpif *ofport;
782 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
783 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
784 dpif_sflow_add_port(ds, ofport->odp_port,
785 netdev_get_name(ofport->up.netdev));
788 dpif_sflow_set_options(ds, sflow_options);
790 dpif_sflow_destroy(ds);
791 ofproto->sflow = NULL;
797 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
799 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
806 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
809 if (cfm_configure(ofport->cfm, s)) {
815 cfm_destroy(ofport->cfm);
821 get_cfm_fault(const struct ofport *ofport_)
823 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
825 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
829 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
832 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
835 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
844 /* Expires all MAC learning entries associated with 'port' and forces ofproto
845 * to revalidate every flow. */
847 bundle_flush_macs(struct ofbundle *bundle)
849 struct ofproto_dpif *ofproto = bundle->ofproto;
850 struct mac_learning *ml = ofproto->ml;
851 struct mac_entry *mac, *next_mac;
853 ofproto->need_revalidate = true;
854 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
855 if (mac->port.p == bundle) {
856 mac_learning_expire(ml, mac);
861 static struct ofbundle *
862 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
864 struct ofbundle *bundle;
866 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
868 if (bundle->aux == aux) {
875 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
876 * ones that are found to 'bundles'. */
878 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
879 void **auxes, size_t n_auxes,
880 struct hmapx *bundles)
885 for (i = 0; i < n_auxes; i++) {
886 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
888 hmapx_add(bundles, bundle);
894 bundle_del_port(struct ofport_dpif *port)
896 struct ofbundle *bundle = port->bundle;
898 bundle->ofproto->need_revalidate = true;
900 list_remove(&port->bundle_node);
904 lacp_slave_unregister(bundle->lacp, port);
907 bond_slave_unregister(bundle->bond, port);
910 bundle->floodable = true;
911 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
912 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
913 bundle->floodable = false;
919 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
920 struct lacp_slave_settings *lacp,
921 uint32_t bond_stable_id)
923 struct ofport_dpif *port;
925 port = get_ofp_port(bundle->ofproto, ofp_port);
930 if (port->bundle != bundle) {
931 bundle->ofproto->need_revalidate = true;
933 bundle_del_port(port);
936 port->bundle = bundle;
937 list_push_back(&bundle->ports, &port->bundle_node);
938 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
939 bundle->floodable = false;
943 lacp_slave_register(bundle->lacp, port, lacp);
946 port->bond_stable_id = bond_stable_id;
952 bundle_destroy(struct ofbundle *bundle)
954 struct ofproto_dpif *ofproto;
955 struct ofport_dpif *port, *next_port;
962 ofproto = bundle->ofproto;
963 for (i = 0; i < MAX_MIRRORS; i++) {
964 struct ofmirror *m = ofproto->mirrors[i];
966 if (m->out == bundle) {
968 } else if (hmapx_find_and_delete(&m->srcs, bundle)
969 || hmapx_find_and_delete(&m->dsts, bundle)) {
970 ofproto->need_revalidate = true;
975 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
976 bundle_del_port(port);
979 bundle_flush_macs(bundle);
980 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
982 free(bundle->trunks);
983 lacp_destroy(bundle->lacp);
984 bond_destroy(bundle->bond);
989 bundle_set(struct ofproto *ofproto_, void *aux,
990 const struct ofproto_bundle_settings *s)
992 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
993 bool need_flush = false;
994 const unsigned long *trunks;
995 struct ofport_dpif *port;
996 struct ofbundle *bundle;
1001 bundle_destroy(bundle_lookup(ofproto, aux));
1005 assert(s->n_slaves == 1 || s->bond != NULL);
1006 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1008 bundle = bundle_lookup(ofproto, aux);
1010 bundle = xmalloc(sizeof *bundle);
1012 bundle->ofproto = ofproto;
1013 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1014 hash_pointer(aux, 0));
1016 bundle->name = NULL;
1018 list_init(&bundle->ports);
1020 bundle->trunks = NULL;
1021 bundle->lacp = NULL;
1022 bundle->bond = NULL;
1024 bundle->floodable = true;
1026 bundle->src_mirrors = 0;
1027 bundle->dst_mirrors = 0;
1028 bundle->mirror_out = 0;
1031 if (!bundle->name || strcmp(s->name, bundle->name)) {
1033 bundle->name = xstrdup(s->name);
1038 if (!bundle->lacp) {
1039 bundle->lacp = lacp_create();
1041 lacp_configure(bundle->lacp, s->lacp);
1043 lacp_destroy(bundle->lacp);
1044 bundle->lacp = NULL;
1047 /* Update set of ports. */
1049 for (i = 0; i < s->n_slaves; i++) {
1050 if (!bundle_add_port(bundle, s->slaves[i],
1051 s->lacp ? &s->lacp_slaves[i] : NULL,
1052 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1056 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1057 struct ofport_dpif *next_port;
1059 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1060 for (i = 0; i < s->n_slaves; i++) {
1061 if (s->slaves[i] == port->up.ofp_port) {
1066 bundle_del_port(port);
1070 assert(list_size(&bundle->ports) <= s->n_slaves);
1072 if (list_is_empty(&bundle->ports)) {
1073 bundle_destroy(bundle);
1078 if (s->vlan != bundle->vlan) {
1079 bundle->vlan = s->vlan;
1083 /* Get trunked VLANs. */
1084 trunks = s->vlan == -1 ? NULL : s->trunks;
1085 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1086 free(bundle->trunks);
1087 bundle->trunks = vlan_bitmap_clone(trunks);
1092 if (!list_is_short(&bundle->ports)) {
1093 bundle->ofproto->has_bonded_bundles = true;
1095 if (bond_reconfigure(bundle->bond, s->bond)) {
1096 ofproto->need_revalidate = true;
1099 bundle->bond = bond_create(s->bond);
1100 ofproto->need_revalidate = true;
1103 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1104 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1108 bond_destroy(bundle->bond);
1109 bundle->bond = NULL;
1112 /* If we changed something that would affect MAC learning, un-learn
1113 * everything on this port and force flow revalidation. */
1115 bundle_flush_macs(bundle);
1122 bundle_remove(struct ofport *port_)
1124 struct ofport_dpif *port = ofport_dpif_cast(port_);
1125 struct ofbundle *bundle = port->bundle;
1128 bundle_del_port(port);
1129 if (list_is_empty(&bundle->ports)) {
1130 bundle_destroy(bundle);
1131 } else if (list_is_short(&bundle->ports)) {
1132 bond_destroy(bundle->bond);
1133 bundle->bond = NULL;
1139 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1141 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1142 struct ofport_dpif *port = port_;
1143 uint8_t ea[ETH_ADDR_LEN];
1146 error = netdev_get_etheraddr(port->up.netdev, ea);
1148 struct ofpbuf packet;
1151 ofpbuf_init(&packet, 0);
1152 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1154 memcpy(packet_pdu, pdu, pdu_size);
1156 error = netdev_send(port->up.netdev, &packet);
1158 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1159 "(%s)", port->bundle->name,
1160 netdev_get_name(port->up.netdev), strerror(error));
1162 ofpbuf_uninit(&packet);
1164 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1165 "%s (%s)", port->bundle->name,
1166 netdev_get_name(port->up.netdev), strerror(error));
1171 bundle_send_learning_packets(struct ofbundle *bundle)
1173 struct ofproto_dpif *ofproto = bundle->ofproto;
1174 int error, n_packets, n_errors;
1175 struct mac_entry *e;
1177 error = n_packets = n_errors = 0;
1178 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1179 if (e->port.p != bundle) {
1180 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1190 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1191 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1192 "packets, last error was: %s",
1193 bundle->name, n_errors, n_packets, strerror(error));
1195 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1196 bundle->name, n_packets);
1201 bundle_run(struct ofbundle *bundle)
1204 lacp_run(bundle->lacp, send_pdu_cb);
1207 struct ofport_dpif *port;
1209 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1210 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1213 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1214 lacp_negotiated(bundle->lacp));
1215 if (bond_should_send_learning_packets(bundle->bond)) {
1216 bundle_send_learning_packets(bundle);
1222 bundle_wait(struct ofbundle *bundle)
1225 lacp_wait(bundle->lacp);
1228 bond_wait(bundle->bond);
1235 mirror_scan(struct ofproto_dpif *ofproto)
1239 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1240 if (!ofproto->mirrors[idx]) {
1247 static struct ofmirror *
1248 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1252 for (i = 0; i < MAX_MIRRORS; i++) {
1253 struct ofmirror *mirror = ofproto->mirrors[i];
1254 if (mirror && mirror->aux == aux) {
1263 mirror_set(struct ofproto *ofproto_, void *aux,
1264 const struct ofproto_mirror_settings *s)
1266 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1267 mirror_mask_t mirror_bit;
1268 struct ofbundle *bundle;
1269 struct ofmirror *mirror;
1270 struct ofbundle *out;
1271 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1272 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1275 mirror = mirror_lookup(ofproto, aux);
1277 mirror_destroy(mirror);
1283 idx = mirror_scan(ofproto);
1285 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1287 ofproto->up.name, MAX_MIRRORS, s->name);
1291 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1292 mirror->ofproto = ofproto;
1295 mirror->out_vlan = -1;
1296 mirror->name = NULL;
1299 if (!mirror->name || strcmp(s->name, mirror->name)) {
1301 mirror->name = xstrdup(s->name);
1304 /* Get the new configuration. */
1305 if (s->out_bundle) {
1306 out = bundle_lookup(ofproto, s->out_bundle);
1308 mirror_destroy(mirror);
1314 out_vlan = s->out_vlan;
1316 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1317 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1319 /* If the configuration has not changed, do nothing. */
1320 if (hmapx_equals(&srcs, &mirror->srcs)
1321 && hmapx_equals(&dsts, &mirror->dsts)
1322 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1323 && mirror->out == out
1324 && mirror->out_vlan == out_vlan)
1326 hmapx_destroy(&srcs);
1327 hmapx_destroy(&dsts);
1331 hmapx_swap(&srcs, &mirror->srcs);
1332 hmapx_destroy(&srcs);
1334 hmapx_swap(&dsts, &mirror->dsts);
1335 hmapx_destroy(&dsts);
1337 free(mirror->vlans);
1338 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1341 mirror->out_vlan = out_vlan;
1343 /* Update bundles. */
1344 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1345 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1346 if (hmapx_contains(&mirror->srcs, bundle)) {
1347 bundle->src_mirrors |= mirror_bit;
1349 bundle->src_mirrors &= ~mirror_bit;
1352 if (hmapx_contains(&mirror->dsts, bundle)) {
1353 bundle->dst_mirrors |= mirror_bit;
1355 bundle->dst_mirrors &= ~mirror_bit;
1358 if (mirror->out == bundle) {
1359 bundle->mirror_out |= mirror_bit;
1361 bundle->mirror_out &= ~mirror_bit;
1365 ofproto->need_revalidate = true;
1366 mac_learning_flush(ofproto->ml);
1372 mirror_destroy(struct ofmirror *mirror)
1374 struct ofproto_dpif *ofproto;
1375 mirror_mask_t mirror_bit;
1376 struct ofbundle *bundle;
1382 ofproto = mirror->ofproto;
1383 ofproto->need_revalidate = true;
1384 mac_learning_flush(ofproto->ml);
1386 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1387 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1388 bundle->src_mirrors &= ~mirror_bit;
1389 bundle->dst_mirrors &= ~mirror_bit;
1390 bundle->mirror_out &= ~mirror_bit;
1393 hmapx_destroy(&mirror->srcs);
1394 hmapx_destroy(&mirror->dsts);
1395 free(mirror->vlans);
1397 ofproto->mirrors[mirror->idx] = NULL;
1403 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1405 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1406 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1407 ofproto->need_revalidate = true;
1408 mac_learning_flush(ofproto->ml);
1414 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1416 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1417 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1418 return bundle && bundle->mirror_out != 0;
1422 forward_bpdu_changed(struct ofproto *ofproto_)
1424 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1425 /* Revalidate cached flows whenever forward_bpdu option changes. */
1426 ofproto->need_revalidate = true;
1431 static struct ofport_dpif *
1432 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1434 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1435 return ofport ? ofport_dpif_cast(ofport) : NULL;
1438 static struct ofport_dpif *
1439 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1441 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1445 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1446 struct dpif_port *dpif_port)
1448 ofproto_port->name = dpif_port->name;
1449 ofproto_port->type = dpif_port->type;
1450 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1454 port_run(struct ofport_dpif *ofport)
1456 bool enable = netdev_get_carrier(ofport->up.netdev);
1459 cfm_run(ofport->cfm);
1461 if (cfm_should_send_ccm(ofport->cfm)) {
1462 struct ofpbuf packet;
1464 ofpbuf_init(&packet, 0);
1465 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1466 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1467 ofport->odp_port, &packet);
1468 ofpbuf_uninit(&packet);
1471 enable = enable && !cfm_get_fault(ofport->cfm);
1474 if (ofport->bundle) {
1475 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1478 if (ofport->may_enable != enable) {
1479 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1481 if (ofproto->has_bundle_action) {
1482 ofproto->need_revalidate = true;
1486 ofport->may_enable = enable;
1490 port_wait(struct ofport_dpif *ofport)
1493 cfm_wait(ofport->cfm);
1498 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1499 struct ofproto_port *ofproto_port)
1501 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1502 struct dpif_port dpif_port;
1505 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1507 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1513 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1515 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1519 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1521 *ofp_portp = odp_port_to_ofp_port(odp_port);
1527 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1529 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1532 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1534 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1536 /* The caller is going to close ofport->up.netdev. If this is a
1537 * bonded port, then the bond is using that netdev, so remove it
1538 * from the bond. The client will need to reconfigure everything
1539 * after deleting ports, so then the slave will get re-added. */
1540 bundle_remove(&ofport->up);
1546 struct port_dump_state {
1547 struct dpif_port_dump dump;
1552 port_dump_start(const struct ofproto *ofproto_, void **statep)
1554 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1555 struct port_dump_state *state;
1557 *statep = state = xmalloc(sizeof *state);
1558 dpif_port_dump_start(&state->dump, ofproto->dpif);
1559 state->done = false;
1564 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1565 struct ofproto_port *port)
1567 struct port_dump_state *state = state_;
1568 struct dpif_port dpif_port;
1570 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1571 ofproto_port_from_dpif_port(port, &dpif_port);
1574 int error = dpif_port_dump_done(&state->dump);
1576 return error ? error : EOF;
1581 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1583 struct port_dump_state *state = state_;
1586 dpif_port_dump_done(&state->dump);
1593 port_poll(const struct ofproto *ofproto_, char **devnamep)
1595 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1596 return dpif_port_poll(ofproto->dpif, devnamep);
1600 port_poll_wait(const struct ofproto *ofproto_)
1602 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1603 dpif_port_poll_wait(ofproto->dpif);
1607 port_is_lacp_current(const struct ofport *ofport_)
1609 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1610 return (ofport->bundle && ofport->bundle->lacp
1611 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1615 /* Upcall handling. */
1617 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1618 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1619 * their individual configurations.
1621 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1622 * Otherwise, ownership is transferred to this function. */
1624 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1625 const struct flow *flow, bool clone)
1627 struct ofputil_packet_in pin;
1629 pin.packet = upcall->packet;
1630 pin.in_port = flow->in_port;
1631 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1632 pin.buffer_id = 0; /* not yet known */
1633 pin.send_len = upcall->userdata;
1634 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1635 clone ? NULL : upcall->packet);
1639 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1640 const struct ofpbuf *packet)
1642 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1648 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
1650 cfm_process_heartbeat(ofport->cfm, packet);
1653 } else if (ofport->bundle && ofport->bundle->lacp
1654 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1656 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
1664 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1666 struct facet *facet;
1669 /* Obtain in_port and tun_id, at least. */
1670 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1672 /* Set header pointers in 'flow'. */
1673 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1675 /* Handle 802.1ag and LACP. */
1676 if (process_special(ofproto, &flow, upcall->packet)) {
1677 ofpbuf_delete(upcall->packet);
1678 ofproto->n_matches++;
1682 /* Check with in-band control to see if this packet should be sent
1683 * to the local port regardless of the flow table. */
1684 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1685 send_packet(ofproto, OVSP_LOCAL, upcall->packet);
1688 facet = facet_lookup_valid(ofproto, &flow);
1690 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow, 0);
1692 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1693 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1695 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1696 COVERAGE_INC(ofproto_dpif_no_packet_in);
1697 /* XXX install 'drop' flow entry */
1698 ofpbuf_delete(upcall->packet);
1702 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1706 send_packet_in(ofproto, upcall, &flow, false);
1710 facet = facet_create(rule, &flow, upcall->packet);
1711 } else if (!facet->may_install) {
1712 /* The facet is not installable, that is, we need to process every
1713 * packet, so process the current packet's actions into 'facet'. */
1714 facet_make_actions(ofproto, facet, upcall->packet);
1717 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1719 * Extra-special case for fail-open mode.
1721 * We are in fail-open mode and the packet matched the fail-open rule,
1722 * but we are connected to a controller too. We should send the packet
1723 * up to the controller in the hope that it will try to set up a flow
1724 * and thereby allow us to exit fail-open.
1726 * See the top-level comment in fail-open.c for more information.
1728 send_packet_in(ofproto, upcall, &flow, true);
1731 facet_execute(ofproto, facet, upcall->packet);
1732 facet_install(ofproto, facet, false);
1733 ofproto->n_matches++;
1737 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1741 switch (upcall->type) {
1742 case DPIF_UC_ACTION:
1743 COVERAGE_INC(ofproto_dpif_ctlr_action);
1744 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1745 send_packet_in(ofproto, upcall, &flow, false);
1748 case DPIF_UC_SAMPLE:
1749 if (ofproto->sflow) {
1750 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1751 dpif_sflow_received(ofproto->sflow, upcall, &flow);
1753 ofpbuf_delete(upcall->packet);
1757 handle_miss_upcall(ofproto, upcall);
1760 case DPIF_N_UC_TYPES:
1762 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1767 /* Flow expiration. */
1769 static int facet_max_idle(const struct ofproto_dpif *);
1770 static void update_stats(struct ofproto_dpif *);
1771 static void rule_expire(struct rule_dpif *);
1772 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1774 /* This function is called periodically by run(). Its job is to collect
1775 * updates for the flows that have been installed into the datapath, most
1776 * importantly when they last were used, and then use that information to
1777 * expire flows that have not been used recently.
1779 * Returns the number of milliseconds after which it should be called again. */
1781 expire(struct ofproto_dpif *ofproto)
1783 struct rule_dpif *rule, *next_rule;
1784 struct classifier *table;
1787 /* Update stats for each flow in the datapath. */
1788 update_stats(ofproto);
1790 /* Expire facets that have been idle too long. */
1791 dp_max_idle = facet_max_idle(ofproto);
1792 expire_facets(ofproto, dp_max_idle);
1794 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1795 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1796 struct cls_cursor cursor;
1798 cls_cursor_init(&cursor, table, NULL);
1799 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1804 /* All outstanding data in existing flows has been accounted, so it's a
1805 * good time to do bond rebalancing. */
1806 if (ofproto->has_bonded_bundles) {
1807 struct ofbundle *bundle;
1809 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1811 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1816 return MIN(dp_max_idle, 1000);
1819 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1821 * This function also pushes statistics updates to rules which each facet
1822 * resubmits into. Generally these statistics will be accurate. However, if a
1823 * facet changes the rule it resubmits into at some time in between
1824 * update_stats() runs, it is possible that statistics accrued to the
1825 * old rule will be incorrectly attributed to the new rule. This could be
1826 * avoided by calling update_stats() whenever rules are created or
1827 * deleted. However, the performance impact of making so many calls to the
1828 * datapath do not justify the benefit of having perfectly accurate statistics.
1831 update_stats(struct ofproto_dpif *p)
1833 const struct dpif_flow_stats *stats;
1834 struct dpif_flow_dump dump;
1835 const struct nlattr *key;
1838 dpif_flow_dump_start(&dump, p->dpif);
1839 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1840 struct facet *facet;
1843 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1847 odp_flow_key_format(key, key_len, &s);
1848 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
1854 facet = facet_find(p, &flow);
1856 if (facet && facet->installed) {
1858 if (stats->n_packets >= facet->dp_packet_count) {
1859 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1860 facet->packet_count += extra;
1862 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1865 if (stats->n_bytes >= facet->dp_byte_count) {
1866 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1868 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1871 facet->dp_packet_count = stats->n_packets;
1872 facet->dp_byte_count = stats->n_bytes;
1874 facet_update_time(p, facet, stats->used);
1875 facet_account(p, facet);
1876 facet_push_stats(facet);
1878 /* There's a flow in the datapath that we know nothing about.
1880 COVERAGE_INC(facet_unexpected);
1881 dpif_flow_del(p->dpif, key, key_len, NULL);
1884 dpif_flow_dump_done(&dump);
1887 /* Calculates and returns the number of milliseconds of idle time after which
1888 * facets should expire from the datapath and we should fold their statistics
1889 * into their parent rules in userspace. */
1891 facet_max_idle(const struct ofproto_dpif *ofproto)
1894 * Idle time histogram.
1896 * Most of the time a switch has a relatively small number of facets. When
1897 * this is the case we might as well keep statistics for all of them in
1898 * userspace and to cache them in the kernel datapath for performance as
1901 * As the number of facets increases, the memory required to maintain
1902 * statistics about them in userspace and in the kernel becomes
1903 * significant. However, with a large number of facets it is likely that
1904 * only a few of them are "heavy hitters" that consume a large amount of
1905 * bandwidth. At this point, only heavy hitters are worth caching in the
1906 * kernel and maintaining in userspaces; other facets we can discard.
1908 * The technique used to compute the idle time is to build a histogram with
1909 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1910 * that is installed in the kernel gets dropped in the appropriate bucket.
1911 * After the histogram has been built, we compute the cutoff so that only
1912 * the most-recently-used 1% of facets (but at least
1913 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1914 * the most-recently-used bucket of facets is kept, so actually an
1915 * arbitrary number of facets can be kept in any given expiration run
1916 * (though the next run will delete most of those unless they receive
1919 * This requires a second pass through the facets, in addition to the pass
1920 * made by update_stats(), because the former function never looks
1921 * at uninstallable facets.
1923 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1924 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1925 int buckets[N_BUCKETS] = { 0 };
1926 int total, subtotal, bucket;
1927 struct facet *facet;
1931 total = hmap_count(&ofproto->facets);
1932 if (total <= ofproto->up.flow_eviction_threshold) {
1933 return N_BUCKETS * BUCKET_WIDTH;
1936 /* Build histogram. */
1938 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1939 long long int idle = now - facet->used;
1940 int bucket = (idle <= 0 ? 0
1941 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1942 : (unsigned int) idle / BUCKET_WIDTH);
1946 /* Find the first bucket whose flows should be expired. */
1947 subtotal = bucket = 0;
1949 subtotal += buckets[bucket++];
1950 } while (bucket < N_BUCKETS &&
1951 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
1953 if (VLOG_IS_DBG_ENABLED()) {
1957 ds_put_cstr(&s, "keep");
1958 for (i = 0; i < N_BUCKETS; i++) {
1960 ds_put_cstr(&s, ", drop");
1963 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1966 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1970 return bucket * BUCKET_WIDTH;
1974 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1976 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1977 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1978 struct ofexpired expired;
1980 if (facet->installed) {
1981 struct dpif_flow_stats stats;
1983 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1985 facet_update_stats(ofproto, facet, &stats);
1988 expired.flow = facet->flow;
1989 expired.packet_count = facet->packet_count;
1990 expired.byte_count = facet->byte_count;
1991 expired.used = facet->used;
1992 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1997 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1999 long long int cutoff = time_msec() - dp_max_idle;
2000 struct facet *facet, *next_facet;
2002 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2003 facet_active_timeout(ofproto, facet);
2004 if (facet->used < cutoff) {
2005 facet_remove(ofproto, facet);
2010 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2011 * then delete it entirely. */
2013 rule_expire(struct rule_dpif *rule)
2015 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2016 struct facet *facet, *next_facet;
2020 /* Has 'rule' expired? */
2022 if (rule->up.hard_timeout
2023 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2024 reason = OFPRR_HARD_TIMEOUT;
2025 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2026 && now > rule->used + rule->up.idle_timeout * 1000) {
2027 reason = OFPRR_IDLE_TIMEOUT;
2032 COVERAGE_INC(ofproto_dpif_expired);
2034 /* Update stats. (This is a no-op if the rule expired due to an idle
2035 * timeout, because that only happens when the rule has no facets left.) */
2036 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2037 facet_remove(ofproto, facet);
2040 /* Get rid of the rule. */
2041 ofproto_rule_expire(&rule->up, reason);
2046 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2047 * example 'packet' within that flow.
2049 * The caller must already have determined that no facet with an identical
2050 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2051 * the ofproto's classifier table. */
2052 static struct facet *
2053 facet_create(struct rule_dpif *rule, const struct flow *flow,
2054 const struct ofpbuf *packet)
2056 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2057 struct facet *facet;
2059 facet = xzalloc(sizeof *facet);
2060 facet->used = time_msec();
2061 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2062 list_push_back(&rule->facets, &facet->list_node);
2064 facet->flow = *flow;
2065 netflow_flow_init(&facet->nf_flow);
2066 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2068 facet_make_actions(ofproto, facet, packet);
2074 facet_free(struct facet *facet)
2076 free(facet->actions);
2080 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2081 * 'packet', which arrived on 'in_port'.
2083 * Takes ownership of 'packet'. */
2085 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2086 const struct nlattr *odp_actions, size_t actions_len,
2087 struct ofpbuf *packet)
2089 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2090 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE) {
2091 /* As an optimization, avoid a round-trip from userspace to kernel to
2092 * userspace. This also avoids possibly filling up kernel packet
2093 * buffers along the way. */
2094 struct dpif_upcall upcall;
2096 upcall.type = DPIF_UC_ACTION;
2097 upcall.packet = packet;
2100 upcall.userdata = nl_attr_get_u64(odp_actions);
2101 upcall.sample_pool = 0;
2102 upcall.actions = NULL;
2103 upcall.actions_len = 0;
2105 send_packet_in(ofproto, &upcall, flow, false);
2109 struct odputil_keybuf keybuf;
2113 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2114 odp_flow_key_from_flow(&key, flow);
2116 error = dpif_execute(ofproto->dpif, key.data, key.size,
2117 odp_actions, actions_len, packet);
2119 ofpbuf_delete(packet);
2124 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2125 * statistics appropriately. 'packet' must have at least sizeof(struct
2126 * ofp_packet_in) bytes of headroom.
2128 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2129 * applying flow_extract() to 'packet' would yield the same flow as
2132 * 'facet' must have accurately composed datapath actions; that is, it must
2133 * not be in need of revalidation.
2135 * Takes ownership of 'packet'. */
2137 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2138 struct ofpbuf *packet)
2140 struct dpif_flow_stats stats;
2142 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2144 flow_extract_stats(&facet->flow, packet, &stats);
2145 stats.used = time_msec();
2146 if (execute_odp_actions(ofproto, &facet->flow,
2147 facet->actions, facet->actions_len, packet)) {
2148 facet_update_stats(ofproto, facet, &stats);
2152 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2154 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2155 * rule's statistics, via facet_uninstall().
2157 * - Removes 'facet' from its rule and from ofproto->facets.
2160 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2162 facet_uninstall(ofproto, facet);
2163 facet_flush_stats(ofproto, facet);
2164 hmap_remove(&ofproto->facets, &facet->hmap_node);
2165 list_remove(&facet->list_node);
2169 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2171 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2172 const struct ofpbuf *packet)
2174 const struct rule_dpif *rule = facet->rule;
2175 struct ofpbuf *odp_actions;
2176 struct action_xlate_ctx ctx;
2178 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2179 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2180 facet->tags = ctx.tags;
2181 facet->may_install = ctx.may_set_up_flow;
2182 facet->has_learn = ctx.has_learn;
2183 facet->has_normal = ctx.has_normal;
2184 facet->nf_flow.output_iface = ctx.nf_output_iface;
2186 if (facet->actions_len != odp_actions->size
2187 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2188 free(facet->actions);
2189 facet->actions_len = odp_actions->size;
2190 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2193 ofpbuf_delete(odp_actions);
2196 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2197 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2198 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2199 * since 'facet' was last updated.
2201 * Returns 0 if successful, otherwise a positive errno value.*/
2203 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2204 const struct nlattr *actions, size_t actions_len,
2205 struct dpif_flow_stats *stats)
2207 struct odputil_keybuf keybuf;
2208 enum dpif_flow_put_flags flags;
2212 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2214 flags |= DPIF_FP_ZERO_STATS;
2217 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2218 odp_flow_key_from_flow(&key, &facet->flow);
2220 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2221 actions, actions_len, stats);
2224 facet_reset_dp_stats(facet, stats);
2230 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2231 * 'zero_stats' is true, clears any existing statistics from the datapath for
2234 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2236 struct dpif_flow_stats stats;
2238 if (facet->may_install
2239 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2240 zero_stats ? &stats : NULL)) {
2241 facet->installed = true;
2246 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2248 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2252 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2255 const struct nlattr *a;
2259 if (facet->byte_count <= facet->accounted_bytes) {
2262 n_bytes = facet->byte_count - facet->accounted_bytes;
2263 facet->accounted_bytes = facet->byte_count;
2265 /* Feed information from the active flows back into the learning table to
2266 * ensure that table is always in sync with what is actually flowing
2267 * through the datapath. */
2268 if (facet->has_learn || facet->has_normal) {
2269 struct action_xlate_ctx ctx;
2271 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2272 ctx.may_learn = true;
2273 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2274 facet->rule->up.n_actions));
2277 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2281 /* This loop feeds byte counters to bond_account() for rebalancing to use
2282 * as a basis. We also need to track the actual VLAN on which the packet
2283 * is going to be sent to ensure that it matches the one passed to
2284 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2286 vlan_tci = facet->flow.vlan_tci;
2287 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2288 struct ofport_dpif *port;
2290 switch (nl_attr_type(a)) {
2291 case OVS_ACTION_ATTR_OUTPUT:
2292 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2293 if (port && port->bundle && port->bundle->bond) {
2294 bond_account(port->bundle->bond, &facet->flow,
2295 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2299 case OVS_ACTION_ATTR_POP_VLAN:
2300 vlan_tci = htons(0);
2303 case OVS_ACTION_ATTR_PUSH_VLAN:
2304 vlan_tci = nl_attr_get_be16(a);
2310 /* If 'rule' is installed in the datapath, uninstalls it. */
2312 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2314 if (facet->installed) {
2315 struct odputil_keybuf keybuf;
2316 struct dpif_flow_stats stats;
2320 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2321 odp_flow_key_from_flow(&key, &facet->flow);
2323 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2324 facet_reset_dp_stats(facet, &stats);
2326 facet_update_stats(p, facet, &stats);
2328 facet->installed = false;
2330 assert(facet->dp_packet_count == 0);
2331 assert(facet->dp_byte_count == 0);
2335 /* Returns true if the only action for 'facet' is to send to the controller.
2336 * (We don't report NetFlow expiration messages for such facets because they
2337 * are just part of the control logic for the network, not real traffic). */
2339 facet_is_controller_flow(struct facet *facet)
2342 && facet->rule->up.n_actions == 1
2343 && action_outputs_to_port(&facet->rule->up.actions[0],
2344 htons(OFPP_CONTROLLER)));
2347 /* Resets 'facet''s datapath statistics counters. This should be called when
2348 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2349 * it should contain the statistics returned by dpif when 'facet' was reset in
2350 * the datapath. 'stats' will be modified to only included statistics new
2351 * since 'facet' was last updated. */
2353 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2355 if (stats && facet->dp_packet_count <= stats->n_packets
2356 && facet->dp_byte_count <= stats->n_bytes) {
2357 stats->n_packets -= facet->dp_packet_count;
2358 stats->n_bytes -= facet->dp_byte_count;
2361 facet->dp_packet_count = 0;
2362 facet->dp_byte_count = 0;
2365 /* Folds all of 'facet''s statistics into its rule. Also updates the
2366 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2367 * 'facet''s statistics in the datapath should have been zeroed and folded into
2368 * its packet and byte counts before this function is called. */
2370 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2372 assert(!facet->dp_byte_count);
2373 assert(!facet->dp_packet_count);
2375 facet_push_stats(facet);
2376 facet_account(ofproto, facet);
2378 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2379 struct ofexpired expired;
2380 expired.flow = facet->flow;
2381 expired.packet_count = facet->packet_count;
2382 expired.byte_count = facet->byte_count;
2383 expired.used = facet->used;
2384 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2387 facet->rule->packet_count += facet->packet_count;
2388 facet->rule->byte_count += facet->byte_count;
2390 /* Reset counters to prevent double counting if 'facet' ever gets
2392 facet_reset_counters(facet);
2394 netflow_flow_clear(&facet->nf_flow);
2397 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2398 * Returns it if found, otherwise a null pointer.
2400 * The returned facet might need revalidation; use facet_lookup_valid()
2401 * instead if that is important. */
2402 static struct facet *
2403 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2405 struct facet *facet;
2407 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2409 if (flow_equal(flow, &facet->flow)) {
2417 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2418 * Returns it if found, otherwise a null pointer.
2420 * The returned facet is guaranteed to be valid. */
2421 static struct facet *
2422 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2424 struct facet *facet = facet_find(ofproto, flow);
2426 /* The facet we found might not be valid, since we could be in need of
2427 * revalidation. If it is not valid, don't return it. */
2429 && ofproto->need_revalidate
2430 && !facet_revalidate(ofproto, facet)) {
2431 COVERAGE_INC(facet_invalidated);
2438 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2440 * - If the rule found is different from 'facet''s current rule, moves
2441 * 'facet' to the new rule and recompiles its actions.
2443 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2444 * where it is and recompiles its actions anyway.
2446 * - If there is none, destroys 'facet'.
2448 * Returns true if 'facet' still exists, false if it has been destroyed. */
2450 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2452 struct action_xlate_ctx ctx;
2453 struct ofpbuf *odp_actions;
2454 struct rule_dpif *new_rule;
2455 bool actions_changed;
2457 COVERAGE_INC(facet_revalidate);
2459 /* Determine the new rule. */
2460 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
2462 /* No new rule, so delete the facet. */
2463 facet_remove(ofproto, facet);
2467 /* Calculate new datapath actions.
2469 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2470 * emit a NetFlow expiration and, if so, we need to have the old state
2471 * around to properly compose it. */
2472 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2473 odp_actions = xlate_actions(&ctx,
2474 new_rule->up.actions, new_rule->up.n_actions);
2475 actions_changed = (facet->actions_len != odp_actions->size
2476 || memcmp(facet->actions, odp_actions->data,
2477 facet->actions_len));
2479 /* If the datapath actions changed or the installability changed,
2480 * then we need to talk to the datapath. */
2481 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2482 if (ctx.may_set_up_flow) {
2483 struct dpif_flow_stats stats;
2485 facet_put__(ofproto, facet,
2486 odp_actions->data, odp_actions->size, &stats);
2487 facet_update_stats(ofproto, facet, &stats);
2489 facet_uninstall(ofproto, facet);
2492 /* The datapath flow is gone or has zeroed stats, so push stats out of
2493 * 'facet' into 'rule'. */
2494 facet_flush_stats(ofproto, facet);
2497 /* Update 'facet' now that we've taken care of all the old state. */
2498 facet->tags = ctx.tags;
2499 facet->nf_flow.output_iface = ctx.nf_output_iface;
2500 facet->may_install = ctx.may_set_up_flow;
2501 facet->has_learn = ctx.has_learn;
2502 facet->has_normal = ctx.has_normal;
2503 if (actions_changed) {
2504 free(facet->actions);
2505 facet->actions_len = odp_actions->size;
2506 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2508 if (facet->rule != new_rule) {
2509 COVERAGE_INC(facet_changed_rule);
2510 list_remove(&facet->list_node);
2511 list_push_back(&new_rule->facets, &facet->list_node);
2512 facet->rule = new_rule;
2513 facet->used = new_rule->up.created;
2514 facet->rs_used = facet->used;
2517 ofpbuf_delete(odp_actions);
2522 /* Updates 'facet''s used time. Caller is responsible for calling
2523 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2525 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2528 if (used > facet->used) {
2530 if (used > facet->rule->used) {
2531 facet->rule->used = used;
2533 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2537 /* Folds the statistics from 'stats' into the counters in 'facet'.
2539 * Because of the meaning of a facet's counters, it only makes sense to do this
2540 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2541 * packet that was sent by hand or if it represents statistics that have been
2542 * cleared out of the datapath. */
2544 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2545 const struct dpif_flow_stats *stats)
2547 if (stats->n_packets || stats->used > facet->used) {
2548 facet_update_time(ofproto, facet, stats->used);
2549 facet->packet_count += stats->n_packets;
2550 facet->byte_count += stats->n_bytes;
2551 facet_push_stats(facet);
2552 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2557 facet_reset_counters(struct facet *facet)
2559 facet->packet_count = 0;
2560 facet->byte_count = 0;
2561 facet->rs_packet_count = 0;
2562 facet->rs_byte_count = 0;
2563 facet->accounted_bytes = 0;
2567 facet_push_stats(struct facet *facet)
2569 uint64_t rs_packets, rs_bytes;
2571 assert(facet->packet_count >= facet->rs_packet_count);
2572 assert(facet->byte_count >= facet->rs_byte_count);
2573 assert(facet->used >= facet->rs_used);
2575 rs_packets = facet->packet_count - facet->rs_packet_count;
2576 rs_bytes = facet->byte_count - facet->rs_byte_count;
2578 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2579 facet->rs_packet_count = facet->packet_count;
2580 facet->rs_byte_count = facet->byte_count;
2581 facet->rs_used = facet->used;
2583 flow_push_stats(facet->rule, &facet->flow,
2584 rs_packets, rs_bytes, facet->used);
2588 struct ofproto_push {
2589 struct action_xlate_ctx ctx;
2596 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2598 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2601 rule->packet_count += push->packets;
2602 rule->byte_count += push->bytes;
2603 rule->used = MAX(push->used, rule->used);
2607 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2608 * 'rule''s actions. */
2610 flow_push_stats(const struct rule_dpif *rule,
2611 struct flow *flow, uint64_t packets, uint64_t bytes,
2614 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2615 struct ofproto_push push;
2617 push.packets = packets;
2621 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2622 push.ctx.resubmit_hook = push_resubmit;
2623 ofpbuf_delete(xlate_actions(&push.ctx,
2624 rule->up.actions, rule->up.n_actions));
2629 static struct rule_dpif *
2630 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2633 return rule_dpif_cast(rule_from_cls_rule(
2634 classifier_lookup(&ofproto->up.tables[table_id],
2639 complete_operation(struct rule_dpif *rule)
2641 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2643 ofproto->need_revalidate = true;
2645 struct dpif_completion *c = xmalloc(sizeof *c);
2646 c->op = rule->up.pending;
2647 list_push_back(&ofproto->completions, &c->list_node);
2649 ofoperation_complete(rule->up.pending, 0);
2653 static struct rule *
2656 struct rule_dpif *rule = xmalloc(sizeof *rule);
2661 rule_dealloc(struct rule *rule_)
2663 struct rule_dpif *rule = rule_dpif_cast(rule_);
2668 rule_construct(struct rule *rule_)
2670 struct rule_dpif *rule = rule_dpif_cast(rule_);
2671 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2672 struct rule_dpif *victim;
2675 error = validate_actions(rule->up.actions, rule->up.n_actions,
2676 &rule->up.cr.flow, ofproto->max_ports);
2681 rule->used = rule->up.created;
2682 rule->packet_count = 0;
2683 rule->byte_count = 0;
2685 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2686 if (victim && !list_is_empty(&victim->facets)) {
2687 struct facet *facet;
2689 rule->facets = victim->facets;
2690 list_moved(&rule->facets);
2691 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2692 /* XXX: We're only clearing our local counters here. It's possible
2693 * that quite a few packets are unaccounted for in the datapath
2694 * statistics. These will be accounted to the new rule instead of
2695 * cleared as required. This could be fixed by clearing out the
2696 * datapath statistics for this facet, but currently it doesn't
2698 facet_reset_counters(facet);
2702 /* Must avoid list_moved() in this case. */
2703 list_init(&rule->facets);
2706 complete_operation(rule);
2711 rule_destruct(struct rule *rule_)
2713 struct rule_dpif *rule = rule_dpif_cast(rule_);
2714 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2715 struct facet *facet, *next_facet;
2717 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2718 facet_revalidate(ofproto, facet);
2721 complete_operation(rule);
2725 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2727 struct rule_dpif *rule = rule_dpif_cast(rule_);
2728 struct facet *facet;
2730 /* Start from historical data for 'rule' itself that are no longer tracked
2731 * in facets. This counts, for example, facets that have expired. */
2732 *packets = rule->packet_count;
2733 *bytes = rule->byte_count;
2735 /* Add any statistics that are tracked by facets. This includes
2736 * statistical data recently updated by ofproto_update_stats() as well as
2737 * stats for packets that were executed "by hand" via dpif_execute(). */
2738 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2739 *packets += facet->packet_count;
2740 *bytes += facet->byte_count;
2745 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2747 struct rule_dpif *rule = rule_dpif_cast(rule_);
2748 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2749 struct action_xlate_ctx ctx;
2750 struct ofpbuf *odp_actions;
2751 struct facet *facet;
2754 /* First look for a related facet. If we find one, account it to that. */
2755 facet = facet_lookup_valid(ofproto, flow);
2756 if (facet && facet->rule == rule) {
2757 facet_execute(ofproto, facet, packet);
2761 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2762 * create a new facet for it and use that. */
2763 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
2764 facet = facet_create(rule, flow, packet);
2765 facet_execute(ofproto, facet, packet);
2766 facet_install(ofproto, facet, true);
2770 /* We can't account anything to a facet. If we were to try, then that
2771 * facet would have a non-matching rule, busting our invariants. */
2772 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2773 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2774 size = packet->size;
2775 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2776 odp_actions->size, packet)) {
2777 rule->used = time_msec();
2778 rule->packet_count++;
2779 rule->byte_count += size;
2780 flow_push_stats(rule, flow, 1, size, rule->used);
2782 ofpbuf_delete(odp_actions);
2788 rule_modify_actions(struct rule *rule_)
2790 struct rule_dpif *rule = rule_dpif_cast(rule_);
2791 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2794 error = validate_actions(rule->up.actions, rule->up.n_actions,
2795 &rule->up.cr.flow, ofproto->max_ports);
2797 ofoperation_complete(rule->up.pending, error);
2801 complete_operation(rule);
2804 /* Sends 'packet' out of port 'odp_port' within 'p'.
2805 * Returns 0 if successful, otherwise a positive errno value. */
2807 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2808 const struct ofpbuf *packet)
2810 struct ofpbuf key, odp_actions;
2811 struct odputil_keybuf keybuf;
2815 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2816 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2817 odp_flow_key_from_flow(&key, &flow);
2819 ofpbuf_init(&odp_actions, 32);
2820 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2821 error = dpif_execute(ofproto->dpif,
2823 odp_actions.data, odp_actions.size,
2825 ofpbuf_uninit(&odp_actions);
2828 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2829 ofproto->up.name, odp_port, strerror(error));
2834 /* OpenFlow to datapath action translation. */
2836 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2837 struct action_xlate_ctx *ctx);
2838 static void xlate_normal(struct action_xlate_ctx *);
2841 commit_odp_actions(struct action_xlate_ctx *ctx)
2843 const struct flow *flow = &ctx->flow;
2844 struct flow *base = &ctx->base_flow;
2845 struct ofpbuf *odp_actions = ctx->odp_actions;
2847 if (base->tun_id != flow->tun_id) {
2848 nl_msg_put_be64(odp_actions, OVS_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
2849 base->tun_id = flow->tun_id;
2852 if (base->nw_src != flow->nw_src) {
2853 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
2854 base->nw_src = flow->nw_src;
2857 if (base->nw_dst != flow->nw_dst) {
2858 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
2859 base->nw_dst = flow->nw_dst;
2862 if (base->nw_tos != flow->nw_tos) {
2863 nl_msg_put_u8(odp_actions, OVS_ACTION_ATTR_SET_NW_TOS, flow->nw_tos);
2864 base->nw_tos = flow->nw_tos;
2867 if (base->vlan_tci != flow->vlan_tci) {
2868 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2869 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2871 if (base->vlan_tci != htons(0)) {
2872 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2874 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2875 flow->vlan_tci & ~htons(VLAN_CFI));
2877 base->vlan_tci = flow->vlan_tci;
2880 if (base->tp_src != flow->tp_src) {
2881 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
2882 base->tp_src = flow->tp_src;
2885 if (base->tp_dst != flow->tp_dst) {
2886 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
2887 base->tp_dst = flow->tp_dst;
2890 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
2891 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_SRC,
2892 flow->dl_src, ETH_ADDR_LEN);
2893 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2896 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2897 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_DST,
2898 flow->dl_dst, ETH_ADDR_LEN);
2899 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2902 if (ctx->base_priority != ctx->priority) {
2903 if (ctx->priority) {
2904 nl_msg_put_u32(odp_actions, OVS_ACTION_ATTR_SET_PRIORITY,
2907 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
2909 ctx->base_priority = ctx->priority;
2914 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2916 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2917 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2920 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2921 /* Forwarding disabled on port. */
2926 * We don't have an ofport record for this port, but it doesn't hurt to
2927 * allow forwarding to it anyhow. Maybe such a port will appear later
2928 * and we're pre-populating the flow table.
2932 commit_odp_actions(ctx);
2933 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2934 ctx->nf_output_iface = ofp_port;
2938 xlate_table_action(struct action_xlate_ctx *ctx,
2939 uint16_t in_port, uint8_t table_id)
2941 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2942 struct rule_dpif *rule;
2943 uint16_t old_in_port;
2944 uint8_t old_table_id;
2946 old_table_id = ctx->table_id;
2947 ctx->table_id = table_id;
2949 /* Look up a flow with 'in_port' as the input port. Then restore the
2950 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2951 * have surprising behavior). */
2952 old_in_port = ctx->flow.in_port;
2953 ctx->flow.in_port = in_port;
2954 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow, table_id);
2955 ctx->flow.in_port = old_in_port;
2957 if (ctx->resubmit_hook) {
2958 ctx->resubmit_hook(ctx, rule);
2963 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2967 ctx->table_id = old_table_id;
2969 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2971 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
2972 MAX_RESUBMIT_RECURSION);
2977 xlate_resubmit_table(struct action_xlate_ctx *ctx,
2978 const struct nx_action_resubmit *nar)
2983 in_port = (nar->in_port == htons(OFPP_IN_PORT)
2985 : ntohs(nar->in_port));
2986 table_id = nar->table == 255 ? ctx->table_id : nar->table;
2988 xlate_table_action(ctx, in_port, table_id);
2992 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
2994 struct ofport_dpif *ofport;
2996 commit_odp_actions(ctx);
2997 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
2998 uint16_t ofp_port = ofport->up.ofp_port;
2999 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
3000 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT,
3005 ctx->nf_output_iface = NF_OUT_FLOOD;
3009 xlate_output_action__(struct action_xlate_ctx *ctx,
3010 uint16_t port, uint16_t max_len)
3012 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3014 ctx->nf_output_iface = NF_OUT_DROP;
3018 add_output_action(ctx, ctx->flow.in_port);
3021 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3027 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3030 flood_packets(ctx, htonl(0));
3032 case OFPP_CONTROLLER:
3033 commit_odp_actions(ctx);
3034 nl_msg_put_u64(ctx->odp_actions, OVS_ACTION_ATTR_USERSPACE, max_len);
3037 add_output_action(ctx, OFPP_LOCAL);
3042 if (port != ctx->flow.in_port) {
3043 add_output_action(ctx, port);
3048 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3049 ctx->nf_output_iface = NF_OUT_FLOOD;
3050 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3051 ctx->nf_output_iface = prev_nf_output_iface;
3052 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3053 ctx->nf_output_iface != NF_OUT_FLOOD) {
3054 ctx->nf_output_iface = NF_OUT_MULTI;
3059 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3060 const struct nx_action_output_reg *naor)
3064 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3066 if (ofp_port <= UINT16_MAX) {
3067 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3072 xlate_output_action(struct action_xlate_ctx *ctx,
3073 const struct ofp_action_output *oao)
3075 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3079 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3080 const struct ofp_action_enqueue *oae)
3082 uint16_t ofp_port, odp_port;
3083 uint32_t ctx_priority, priority;
3086 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3089 /* Fall back to ordinary output action. */
3090 xlate_output_action__(ctx, ntohs(oae->port), 0);
3094 /* Figure out datapath output port. */
3095 ofp_port = ntohs(oae->port);
3096 if (ofp_port == OFPP_IN_PORT) {
3097 ofp_port = ctx->flow.in_port;
3099 odp_port = ofp_port_to_odp_port(ofp_port);
3101 /* Add datapath actions. */
3102 ctx_priority = ctx->priority;
3103 ctx->priority = priority;
3104 add_output_action(ctx, odp_port);
3105 ctx->priority = ctx_priority;
3107 /* Update NetFlow output port. */
3108 if (ctx->nf_output_iface == NF_OUT_DROP) {
3109 ctx->nf_output_iface = odp_port;
3110 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3111 ctx->nf_output_iface = NF_OUT_MULTI;
3116 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3117 const struct nx_action_set_queue *nasq)
3122 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3125 /* Couldn't translate queue to a priority, so ignore. A warning
3126 * has already been logged. */
3130 ctx->priority = priority;
3133 struct xlate_reg_state {
3139 xlate_autopath(struct action_xlate_ctx *ctx,
3140 const struct nx_action_autopath *naa)
3142 uint16_t ofp_port = ntohl(naa->id);
3143 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3145 if (!port || !port->bundle) {
3146 ofp_port = OFPP_NONE;
3147 } else if (port->bundle->bond) {
3148 /* Autopath does not support VLAN hashing. */
3149 struct ofport_dpif *slave = bond_choose_output_slave(
3150 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3152 ofp_port = slave->up.ofp_port;
3155 autopath_execute(naa, &ctx->flow, ofp_port);
3159 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3161 struct ofproto_dpif *ofproto = ofproto_;
3162 struct ofport_dpif *port;
3172 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3175 port = get_ofp_port(ofproto, ofp_port);
3176 return port ? port->may_enable : false;
3181 xlate_learn_action(struct action_xlate_ctx *ctx,
3182 const struct nx_action_learn *learn)
3184 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
3185 struct ofputil_flow_mod fm;
3188 learn_execute(learn, &ctx->flow, &fm);
3190 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
3191 if (error && !VLOG_DROP_WARN(&rl)) {
3192 char *msg = ofputil_error_to_string(error);
3193 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
3201 do_xlate_actions(const union ofp_action *in, size_t n_in,
3202 struct action_xlate_ctx *ctx)
3204 const struct ofport_dpif *port;
3205 const union ofp_action *ia;
3208 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3210 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3211 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3212 ? htonl(OFPPC_NO_RECV_STP)
3213 : htonl(OFPPC_NO_RECV))) {
3214 /* Drop this flow. */
3218 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3219 const struct ofp_action_dl_addr *oada;
3220 const struct nx_action_resubmit *nar;
3221 const struct nx_action_set_tunnel *nast;
3222 const struct nx_action_set_queue *nasq;
3223 const struct nx_action_multipath *nam;
3224 const struct nx_action_autopath *naa;
3225 const struct nx_action_bundle *nab;
3226 const struct nx_action_output_reg *naor;
3227 enum ofputil_action_code code;
3230 code = ofputil_decode_action_unsafe(ia);
3232 case OFPUTIL_OFPAT_OUTPUT:
3233 xlate_output_action(ctx, &ia->output);
3236 case OFPUTIL_OFPAT_SET_VLAN_VID:
3237 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3238 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3241 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3242 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3243 ctx->flow.vlan_tci |= htons(
3244 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3247 case OFPUTIL_OFPAT_STRIP_VLAN:
3248 ctx->flow.vlan_tci = htons(0);
3251 case OFPUTIL_OFPAT_SET_DL_SRC:
3252 oada = ((struct ofp_action_dl_addr *) ia);
3253 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3256 case OFPUTIL_OFPAT_SET_DL_DST:
3257 oada = ((struct ofp_action_dl_addr *) ia);
3258 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3261 case OFPUTIL_OFPAT_SET_NW_SRC:
3262 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3265 case OFPUTIL_OFPAT_SET_NW_DST:
3266 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3269 case OFPUTIL_OFPAT_SET_NW_TOS:
3270 ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK;
3273 case OFPUTIL_OFPAT_SET_TP_SRC:
3274 ctx->flow.tp_src = ia->tp_port.tp_port;
3277 case OFPUTIL_OFPAT_SET_TP_DST:
3278 ctx->flow.tp_dst = ia->tp_port.tp_port;
3281 case OFPUTIL_OFPAT_ENQUEUE:
3282 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3285 case OFPUTIL_NXAST_RESUBMIT:
3286 nar = (const struct nx_action_resubmit *) ia;
3287 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
3290 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3291 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
3294 case OFPUTIL_NXAST_SET_TUNNEL:
3295 nast = (const struct nx_action_set_tunnel *) ia;
3296 tun_id = htonll(ntohl(nast->tun_id));
3297 ctx->flow.tun_id = tun_id;
3300 case OFPUTIL_NXAST_SET_QUEUE:
3301 nasq = (const struct nx_action_set_queue *) ia;
3302 xlate_set_queue_action(ctx, nasq);
3305 case OFPUTIL_NXAST_POP_QUEUE:
3309 case OFPUTIL_NXAST_REG_MOVE:
3310 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3314 case OFPUTIL_NXAST_REG_LOAD:
3315 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3319 case OFPUTIL_NXAST_NOTE:
3320 /* Nothing to do. */
3323 case OFPUTIL_NXAST_SET_TUNNEL64:
3324 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3325 ctx->flow.tun_id = tun_id;
3328 case OFPUTIL_NXAST_MULTIPATH:
3329 nam = (const struct nx_action_multipath *) ia;
3330 multipath_execute(nam, &ctx->flow);
3333 case OFPUTIL_NXAST_AUTOPATH:
3334 naa = (const struct nx_action_autopath *) ia;
3335 xlate_autopath(ctx, naa);
3338 case OFPUTIL_NXAST_BUNDLE:
3339 ctx->ofproto->has_bundle_action = true;
3340 nab = (const struct nx_action_bundle *) ia;
3341 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3346 case OFPUTIL_NXAST_BUNDLE_LOAD:
3347 ctx->ofproto->has_bundle_action = true;
3348 nab = (const struct nx_action_bundle *) ia;
3349 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3353 case OFPUTIL_NXAST_OUTPUT_REG:
3354 naor = (const struct nx_action_output_reg *) ia;
3355 xlate_output_reg_action(ctx, naor);
3358 case OFPUTIL_NXAST_LEARN:
3359 ctx->has_learn = true;
3360 if (ctx->may_learn) {
3361 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
3369 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3370 struct ofproto_dpif *ofproto, const struct flow *flow,
3371 const struct ofpbuf *packet)
3373 ctx->ofproto = ofproto;
3375 ctx->packet = packet;
3376 ctx->may_learn = packet != NULL;
3377 ctx->resubmit_hook = NULL;
3380 static struct ofpbuf *
3381 xlate_actions(struct action_xlate_ctx *ctx,
3382 const union ofp_action *in, size_t n_in)
3384 COVERAGE_INC(ofproto_dpif_xlate);
3386 ctx->odp_actions = ofpbuf_new(512);
3388 ctx->may_set_up_flow = true;
3389 ctx->has_learn = false;
3390 ctx->has_normal = false;
3391 ctx->nf_output_iface = NF_OUT_DROP;
3394 ctx->base_priority = 0;
3395 ctx->base_flow = ctx->flow;
3396 ctx->base_flow.tun_id = 0;
3399 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3400 ctx->may_set_up_flow = false;
3402 do_xlate_actions(in, n_in, ctx);
3405 /* Check with in-band control to see if we're allowed to set up this
3407 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3408 ctx->odp_actions->data,
3409 ctx->odp_actions->size)) {
3410 ctx->may_set_up_flow = false;
3413 return ctx->odp_actions;
3416 /* OFPP_NORMAL implementation. */
3419 struct ofport_dpif *port;
3424 struct dst builtin[32];
3426 size_t n, allocated;
3429 static void dst_set_init(struct dst_set *);
3430 static void dst_set_add(struct dst_set *, const struct dst *);
3431 static void dst_set_free(struct dst_set *);
3433 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3436 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3437 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3439 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3440 : in_bundle->vlan >= 0 ? in_bundle->vlan
3441 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3442 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3444 dst->port = (!out_bundle->bond
3445 ? ofbundle_get_a_port(out_bundle)
3446 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3447 dst->vlan, &ctx->tags));
3449 return dst->port != NULL;
3453 mirror_mask_ffs(mirror_mask_t mask)
3455 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3460 dst_set_init(struct dst_set *set)
3462 set->dsts = set->builtin;
3464 set->allocated = ARRAY_SIZE(set->builtin);
3468 dst_set_add(struct dst_set *set, const struct dst *dst)
3470 if (set->n >= set->allocated) {
3471 size_t new_allocated;
3472 struct dst *new_dsts;
3474 new_allocated = set->allocated * 2;
3475 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3476 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3480 set->dsts = new_dsts;
3481 set->allocated = new_allocated;
3483 set->dsts[set->n++] = *dst;
3487 dst_set_free(struct dst_set *set)
3489 if (set->dsts != set->builtin) {
3495 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3498 for (i = 0; i < set->n; i++) {
3499 if (set->dsts[i].vlan == test->vlan
3500 && set->dsts[i].port == test->port) {
3508 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3510 return (bundle->vlan < 0
3511 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
3515 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3517 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3520 /* Returns an arbitrary interface within 'bundle'. */
3521 static struct ofport_dpif *
3522 ofbundle_get_a_port(const struct ofbundle *bundle)
3524 return CONTAINER_OF(list_front(&bundle->ports),
3525 struct ofport_dpif, bundle_node);
3529 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3530 const struct ofbundle *in_bundle,
3531 const struct ofbundle *out_bundle, struct dst_set *set)
3535 if (out_bundle == OFBUNDLE_FLOOD) {
3536 struct ofbundle *bundle;
3538 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3539 if (bundle != in_bundle
3540 && ofbundle_includes_vlan(bundle, vlan)
3541 && bundle->floodable
3542 && !bundle->mirror_out
3543 && set_dst(ctx, &dst, in_bundle, bundle)) {
3544 dst_set_add(set, &dst);
3547 ctx->nf_output_iface = NF_OUT_FLOOD;
3548 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3549 dst_set_add(set, &dst);
3550 ctx->nf_output_iface = dst.port->odp_port;
3555 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3557 return !m->vlans || bitmap_is_set(m->vlans, vlan);
3560 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3561 * to a VLAN. In general most packets may be mirrored but we want to drop
3562 * protocols that may confuse switches. */
3564 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
3566 /* If you change this function's behavior, please update corresponding
3567 * documentation in vswitch.xml at the same time. */
3568 if (dst[0] != 0x01) {
3569 /* All the currently banned MACs happen to start with 01 currently, so
3570 * this is a quick way to eliminate most of the good ones. */
3572 if (eth_addr_is_reserved(dst)) {
3573 /* Drop STP, IEEE pause frames, and other reserved protocols
3574 * (01-80-c2-00-00-0x). */
3578 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
3580 if ((dst[3] & 0xfe) == 0xcc &&
3581 (dst[4] & 0xfe) == 0xcc &&
3582 (dst[5] & 0xfe) == 0xcc) {
3583 /* Drop the following protocols plus others following the same
3586 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3587 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3588 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3592 if (!(dst[3] | dst[4] | dst[5])) {
3593 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3602 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3603 uint16_t vlan, const struct ofbundle *in_bundle,
3604 struct dst_set *set)
3606 struct ofproto_dpif *ofproto = ctx->ofproto;
3607 mirror_mask_t mirrors;
3611 mirrors = in_bundle->src_mirrors;
3612 for (i = 0; i < set->n; i++) {
3613 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3620 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3621 if (flow_vlan == 0) {
3622 flow_vlan = OFP_VLAN_NONE;
3626 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3627 if (vlan_is_mirrored(m, vlan)) {
3631 if (set_dst(ctx, &dst, in_bundle, m->out)
3632 && !dst_is_duplicate(set, &dst)) {
3633 dst_set_add(set, &dst);
3635 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
3636 struct ofbundle *bundle;
3638 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3639 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3640 && set_dst(ctx, &dst, in_bundle, bundle))
3642 if (bundle->vlan < 0) {
3643 dst.vlan = m->out_vlan;
3645 if (dst_is_duplicate(set, &dst)) {
3649 /* Use the vlan tag on the original flow instead of
3650 * the one passed in the vlan parameter. This ensures
3651 * that we compare the vlan from before any implicit
3652 * tagging tags place. This is necessary because
3653 * dst->vlan is the final vlan, after removing implicit
3655 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3656 /* Don't send out input port on same VLAN. */
3659 dst_set_add(set, &dst);
3664 mirrors &= mirrors - 1;
3669 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3670 const struct ofbundle *in_bundle,
3671 const struct ofbundle *out_bundle)
3673 uint16_t initial_vlan, cur_vlan;
3674 const struct dst *dst;
3678 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3679 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3681 /* Output all the packets we can without having to change the VLAN. */
3682 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3683 if (initial_vlan == 0) {
3684 initial_vlan = OFP_VLAN_NONE;
3686 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3687 if (dst->vlan != initial_vlan) {
3690 nl_msg_put_u32(ctx->odp_actions,
3691 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3694 /* Then output the rest. */
3695 cur_vlan = initial_vlan;
3696 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3697 if (dst->vlan == initial_vlan) {
3700 if (dst->vlan != cur_vlan) {
3701 if (dst->vlan == OFP_VLAN_NONE) {
3702 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3706 if (cur_vlan != OFP_VLAN_NONE) {
3707 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3709 tci = htons(dst->vlan & VLAN_VID_MASK);
3710 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3711 nl_msg_put_be16(ctx->odp_actions,
3712 OVS_ACTION_ATTR_PUSH_VLAN, tci);
3714 cur_vlan = dst->vlan;
3716 nl_msg_put_u32(ctx->odp_actions,
3717 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3723 /* Returns the effective vlan of a packet, taking into account both the
3724 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3725 * the packet is untagged and -1 indicates it has an invalid header and
3726 * should be dropped. */
3728 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3729 struct ofbundle *in_bundle, bool have_packet)
3731 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3732 if (in_bundle->vlan >= 0) {
3735 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3736 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3737 "packet received on port %s configured with "
3738 "implicit VLAN %"PRIu16,
3739 ofproto->up.name, vlan,
3740 in_bundle->name, in_bundle->vlan);
3744 vlan = in_bundle->vlan;
3746 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3748 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3749 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3750 "packet received on port %s not configured for "
3752 ofproto->up.name, vlan, in_bundle->name, vlan);
3761 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3762 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3763 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3765 is_gratuitous_arp(const struct flow *flow)
3767 return (flow->dl_type == htons(ETH_TYPE_ARP)
3768 && eth_addr_is_broadcast(flow->dl_dst)
3769 && (flow->nw_proto == ARP_OP_REPLY
3770 || (flow->nw_proto == ARP_OP_REQUEST
3771 && flow->nw_src == flow->nw_dst)));
3775 update_learning_table(struct ofproto_dpif *ofproto,
3776 const struct flow *flow, int vlan,
3777 struct ofbundle *in_bundle)
3779 struct mac_entry *mac;
3781 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3785 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3786 if (is_gratuitous_arp(flow)) {
3787 /* We don't want to learn from gratuitous ARP packets that are
3788 * reflected back over bond slaves so we lock the learning table. */
3789 if (!in_bundle->bond) {
3790 mac_entry_set_grat_arp_lock(mac);
3791 } else if (mac_entry_is_grat_arp_locked(mac)) {
3796 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3797 /* The log messages here could actually be useful in debugging,
3798 * so keep the rate limit relatively high. */
3799 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3800 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3801 "on port %s in VLAN %d",
3802 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3803 in_bundle->name, vlan);
3805 mac->port.p = in_bundle;
3806 tag_set_add(&ofproto->revalidate_set,
3807 mac_learning_changed(ofproto->ml, mac));
3811 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3812 * dropped. Returns true if they may be forwarded, false if they should be
3815 * If 'have_packet' is true, it indicates that the caller is processing a
3816 * received packet. If 'have_packet' is false, then the caller is just
3817 * revalidating an existing flow because configuration has changed. Either
3818 * way, 'have_packet' only affects logging (there is no point in logging errors
3819 * during revalidation).
3821 * Sets '*in_portp' to the input port. This will be a null pointer if
3822 * flow->in_port does not designate a known input port (in which case
3823 * is_admissible() returns false).
3825 * When returning true, sets '*vlanp' to the effective VLAN of the input
3826 * packet, as returned by flow_get_vlan().
3828 * May also add tags to '*tags', although the current implementation only does
3829 * so in one special case.
3832 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3834 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3836 struct ofport_dpif *in_port;
3837 struct ofbundle *in_bundle;
3840 /* Find the port and bundle for the received packet. */
3841 in_port = get_ofp_port(ofproto, flow->in_port);
3842 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3843 if (!in_port || !in_bundle) {
3844 /* No interface? Something fishy... */
3846 /* Odd. A few possible reasons here:
3848 * - We deleted a port but there are still a few packets queued up
3851 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3852 * we don't know about.
3854 * - Packet arrived on the local port but the local port is not
3857 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3859 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3861 ofproto->up.name, flow->in_port);
3866 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3871 /* Drop frames for reserved multicast addresses
3872 * only if forward_bpdu option is absent. */
3873 if (eth_addr_is_reserved(flow->dl_dst) &&
3874 !ofproto->up.forward_bpdu) {
3878 /* Drop frames on bundles reserved for mirroring. */
3879 if (in_bundle->mirror_out) {
3881 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3882 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3883 "%s, which is reserved exclusively for mirroring",
3884 ofproto->up.name, in_bundle->name);
3889 if (in_bundle->bond) {
3890 struct mac_entry *mac;
3892 switch (bond_check_admissibility(in_bundle->bond, in_port,
3893 flow->dl_dst, tags)) {
3900 case BV_DROP_IF_MOVED:
3901 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3902 if (mac && mac->port.p != in_bundle &&
3903 (!is_gratuitous_arp(flow)
3904 || mac_entry_is_grat_arp_locked(mac))) {
3915 xlate_normal(struct action_xlate_ctx *ctx)
3917 struct ofbundle *in_bundle;
3918 struct ofbundle *out_bundle;
3919 struct mac_entry *mac;
3922 ctx->has_normal = true;
3924 /* Check whether we should drop packets in this flow. */
3925 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3926 &ctx->tags, &vlan, &in_bundle)) {
3931 /* Learn source MAC. */
3932 if (ctx->may_learn) {
3933 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3936 /* Determine output bundle. */
3937 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3940 out_bundle = mac->port.p;
3941 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3942 /* If we are revalidating but don't have a learning entry then eject
3943 * the flow. Installing a flow that floods packets opens up a window
3944 * of time where we could learn from a packet reflected on a bond and
3945 * blackhole packets before the learning table is updated to reflect
3946 * the correct port. */
3947 ctx->may_set_up_flow = false;
3950 out_bundle = OFBUNDLE_FLOOD;
3953 /* Don't send packets out their input bundles. */
3954 if (in_bundle == out_bundle) {
3960 compose_actions(ctx, vlan, in_bundle, out_bundle);
3965 get_drop_frags(struct ofproto *ofproto_)
3967 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3970 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3975 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3977 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3979 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3983 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3984 const struct flow *flow,
3985 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3987 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3990 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3991 ofproto->max_ports);
3993 struct odputil_keybuf keybuf;
3994 struct action_xlate_ctx ctx;
3995 struct ofpbuf *odp_actions;
3998 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3999 odp_flow_key_from_flow(&key, flow);
4001 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
4002 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
4003 dpif_execute(ofproto->dpif, key.data, key.size,
4004 odp_actions->data, odp_actions->size, packet);
4005 ofpbuf_delete(odp_actions);
4011 get_netflow_ids(const struct ofproto *ofproto_,
4012 uint8_t *engine_type, uint8_t *engine_id)
4014 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4016 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
4019 static struct ofproto_dpif *
4020 ofproto_dpif_lookup(const char *name)
4022 struct ofproto *ofproto = ofproto_lookup(name);
4023 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
4024 ? ofproto_dpif_cast(ofproto)
4029 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
4030 const char *args, void *aux OVS_UNUSED)
4032 struct ds ds = DS_EMPTY_INITIALIZER;
4033 const struct ofproto_dpif *ofproto;
4034 const struct mac_entry *e;
4036 ofproto = ofproto_dpif_lookup(args);
4038 unixctl_command_reply(conn, 501, "no such bridge");
4042 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4043 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4044 struct ofbundle *bundle = e->port.p;
4045 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
4046 ofbundle_get_a_port(bundle)->odp_port,
4047 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
4049 unixctl_command_reply(conn, 200, ds_cstr(&ds));
4053 struct ofproto_trace {
4054 struct action_xlate_ctx ctx;
4060 trace_format_rule(struct ds *result, uint8_t table_id, int level,
4061 const struct rule_dpif *rule)
4063 ds_put_char_multiple(result, '\t', level);
4065 ds_put_cstr(result, "No match\n");
4069 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4070 table_id, ntohll(rule->up.flow_cookie));
4071 cls_rule_format(&rule->up.cr, result);
4072 ds_put_char(result, '\n');
4074 ds_put_char_multiple(result, '\t', level);
4075 ds_put_cstr(result, "OpenFlow ");
4076 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
4077 ds_put_char(result, '\n');
4081 trace_format_flow(struct ds *result, int level, const char *title,
4082 struct ofproto_trace *trace)
4084 ds_put_char_multiple(result, '\t', level);
4085 ds_put_format(result, "%s: ", title);
4086 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4087 ds_put_cstr(result, "unchanged");
4089 flow_format(result, &trace->ctx.flow);
4090 trace->flow = trace->ctx.flow;
4092 ds_put_char(result, '\n');
4096 trace_format_regs(struct ds *result, int level, const char *title,
4097 struct ofproto_trace *trace)
4101 ds_put_char_multiple(result, '\t', level);
4102 ds_put_format(result, "%s:", title);
4103 for (i = 0; i < FLOW_N_REGS; i++) {
4104 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
4106 ds_put_char(result, '\n');
4110 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
4112 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4113 struct ds *result = trace->result;
4115 ds_put_char(result, '\n');
4116 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4117 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
4118 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
4122 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4123 void *aux OVS_UNUSED)
4125 char *dpname, *arg1, *arg2, *arg3;
4126 char *args = xstrdup(args_);
4127 char *save_ptr = NULL;
4128 struct ofproto_dpif *ofproto;
4129 struct ofpbuf odp_key;
4130 struct ofpbuf *packet;
4131 struct rule_dpif *rule;
4137 ofpbuf_init(&odp_key, 0);
4140 dpname = strtok_r(args, " ", &save_ptr);
4141 arg1 = strtok_r(NULL, " ", &save_ptr);
4142 arg2 = strtok_r(NULL, " ", &save_ptr);
4143 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4144 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
4145 /* ofproto/trace dpname flow [-generate] */
4148 /* Convert string to datapath key. */
4149 ofpbuf_init(&odp_key, 0);
4150 error = odp_flow_key_from_string(arg1, &odp_key);
4152 unixctl_command_reply(conn, 501, "Bad flow syntax");
4156 /* Convert odp_key to flow. */
4157 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
4159 unixctl_command_reply(conn, 501, "Invalid flow");
4163 /* Generate a packet, if requested. */
4165 packet = ofpbuf_new(0);
4166 flow_compose(packet, &flow);
4168 } else if (dpname && arg1 && arg2 && arg3) {
4169 /* ofproto/trace dpname tun_id in_port packet */
4173 tun_id = htonll(strtoull(arg1, NULL, 0));
4174 in_port = ofp_port_to_odp_port(atoi(arg2));
4176 packet = ofpbuf_new(strlen(args) / 2);
4177 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
4178 arg3 += strspn(arg3, " ");
4179 if (*arg3 != '\0') {
4180 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4183 if (packet->size < ETH_HEADER_LEN) {
4184 unixctl_command_reply(conn, 501,
4185 "Packet data too short for Ethernet");
4189 ds_put_cstr(&result, "Packet: ");
4190 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
4191 ds_put_cstr(&result, s);
4194 flow_extract(packet, tun_id, in_port, &flow);
4196 unixctl_command_reply(conn, 501, "Bad command syntax");
4200 ofproto = ofproto_dpif_lookup(dpname);
4202 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4207 ds_put_cstr(&result, "Flow: ");
4208 flow_format(&result, &flow);
4209 ds_put_char(&result, '\n');
4211 rule = rule_dpif_lookup(ofproto, &flow, 0);
4212 trace_format_rule(&result, 0, 0, rule);
4214 struct ofproto_trace trace;
4215 struct ofpbuf *odp_actions;
4217 trace.result = &result;
4219 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
4220 trace.ctx.resubmit_hook = trace_resubmit;
4221 odp_actions = xlate_actions(&trace.ctx,
4222 rule->up.actions, rule->up.n_actions);
4224 ds_put_char(&result, '\n');
4225 trace_format_flow(&result, 0, "Final flow", &trace);
4226 ds_put_cstr(&result, "Datapath actions: ");
4227 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4228 ofpbuf_delete(odp_actions);
4230 if (!trace.ctx.may_set_up_flow) {
4232 ds_put_cstr(&result, "\nThis flow is not cachable.");
4234 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
4235 "for complete actions, please supply a packet.");
4240 unixctl_command_reply(conn, 200, ds_cstr(&result));
4243 ds_destroy(&result);
4244 ofpbuf_delete(packet);
4245 ofpbuf_uninit(&odp_key);
4250 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
4251 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4254 unixctl_command_reply(conn, 200, NULL);
4258 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
4259 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4262 unixctl_command_reply(conn, 200, NULL);
4266 ofproto_dpif_unixctl_init(void)
4268 static bool registered;
4274 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4275 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
4277 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
4278 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
4281 const struct ofproto_class ofproto_dpif_class = {
4308 port_is_lacp_current,
4309 NULL, /* rule_choose_table */
4316 rule_modify_actions,
4325 get_cfm_remote_mpids,
4330 is_mirror_output_bundle,
4331 forward_bpdu_changed,