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
68 /* Number of implemented OpenFlow tables. */
69 enum { N_TABLES = 255 };
70 BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255);
78 long long int used; /* Time last used; time created if not used. */
82 * - Do include packets and bytes from facets that have been deleted or
83 * whose own statistics have been folded into the rule.
85 * - Do include packets and bytes sent "by hand" that were accounted to
86 * the rule without any facet being involved (this is a rare corner
87 * case in rule_execute()).
89 * - Do not include packet or bytes that can be obtained from any facet's
90 * packet_count or byte_count member or that can be obtained from the
91 * datapath by, e.g., dpif_flow_get() for any facet.
93 uint64_t packet_count; /* Number of packets received. */
94 uint64_t byte_count; /* Number of bytes received. */
96 struct list facets; /* List of "struct facet"s. */
99 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
101 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
104 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
105 const struct flow *, uint8_t table);
107 #define MAX_MIRRORS 32
108 typedef uint32_t mirror_mask_t;
109 #define MIRROR_MASK_C(X) UINT32_C(X)
110 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
112 struct ofproto_dpif *ofproto; /* Owning ofproto. */
113 size_t idx; /* In ofproto's "mirrors" array. */
114 void *aux; /* Key supplied by ofproto's client. */
115 char *name; /* Identifier for log messages. */
117 /* Selection criteria. */
118 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
119 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
120 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
122 /* Output (mutually exclusive). */
123 struct ofbundle *out; /* Output port or NULL. */
124 int out_vlan; /* Output VLAN or -1. */
127 static void mirror_destroy(struct ofmirror *);
129 /* A group of one or more OpenFlow ports. */
130 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
132 struct ofproto_dpif *ofproto; /* Owning ofproto. */
133 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
134 void *aux; /* Key supplied by ofproto's client. */
135 char *name; /* Identifier for log messages. */
138 struct list ports; /* Contains "struct ofport"s. */
139 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
140 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
141 * NULL if all VLANs are trunked. */
142 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
143 struct bond *bond; /* Nonnull iff more than one port. */
146 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
148 /* Port mirroring info. */
149 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
150 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
151 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
154 static void bundle_remove(struct ofport *);
155 static void bundle_destroy(struct ofbundle *);
156 static void bundle_del_port(struct ofport_dpif *);
157 static void bundle_run(struct ofbundle *);
158 static void bundle_wait(struct ofbundle *);
160 struct action_xlate_ctx {
161 /* action_xlate_ctx_init() initializes these members. */
164 struct ofproto_dpif *ofproto;
166 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
167 * this flow when actions change header fields. */
170 /* The packet corresponding to 'flow', or a null pointer if we are
171 * revalidating without a packet to refer to. */
172 const struct ofpbuf *packet;
174 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
175 * want to execute them if we are actually processing a packet, or if we
176 * are accounting for packets that the datapath has processed, but not if
177 * we are just revalidating. */
180 /* If nonnull, called just before executing a resubmit action.
182 * This is normally null so the client has to set it manually after
183 * calling action_xlate_ctx_init(). */
184 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
186 /* xlate_actions() initializes and uses these members. The client might want
187 * to look at them after it returns. */
189 struct ofpbuf *odp_actions; /* Datapath actions. */
190 tag_type tags; /* Tags associated with actions. */
191 bool may_set_up_flow; /* True ordinarily; false if the actions must
192 * be reassessed for every packet. */
193 bool has_learn; /* Actions include NXAST_LEARN? */
194 bool has_normal; /* Actions output to OFPP_NORMAL? */
195 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
197 /* xlate_actions() initializes and uses these members, but the client has no
198 * reason to look at them. */
200 int recurse; /* Recursion level, via xlate_table_action. */
201 uint32_t priority; /* Current flow priority. 0 if none. */
202 struct flow base_flow; /* Flow at the last commit. */
203 uint32_t base_priority; /* Priority at the last commit. */
204 uint8_t table_id; /* OpenFlow table ID where flow was found. */
207 static void action_xlate_ctx_init(struct action_xlate_ctx *,
208 struct ofproto_dpif *, const struct flow *,
209 const struct ofpbuf *);
210 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
211 const union ofp_action *in, size_t n_in);
213 /* An exact-match instantiation of an OpenFlow flow. */
215 long long int used; /* Time last used; time created if not used. */
219 * - Do include packets and bytes sent "by hand", e.g. with
222 * - Do include packets and bytes that were obtained from the datapath
223 * when its statistics were reset (e.g. dpif_flow_put() with
224 * DPIF_FP_ZERO_STATS).
226 uint64_t packet_count; /* Number of packets received. */
227 uint64_t byte_count; /* Number of bytes received. */
229 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
230 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
232 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
233 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
234 long long int rs_used; /* Used time pushed to resubmit children. */
236 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
238 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
239 struct list list_node; /* In owning rule's 'facets' list. */
240 struct rule_dpif *rule; /* Owning rule. */
241 struct flow flow; /* Exact-match flow. */
242 bool installed; /* Installed in datapath? */
243 bool may_install; /* True ordinarily; false if actions must
244 * be reassessed for every packet. */
245 bool has_learn; /* Actions include NXAST_LEARN? */
246 bool has_normal; /* Actions output to OFPP_NORMAL? */
247 size_t actions_len; /* Number of bytes in actions[]. */
248 struct nlattr *actions; /* Datapath actions. */
249 tag_type tags; /* Tags. */
250 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
253 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
254 const struct ofpbuf *packet);
255 static void facet_remove(struct ofproto_dpif *, struct facet *);
256 static void facet_free(struct facet *);
258 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
259 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
260 const struct flow *);
261 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
263 static void facet_execute(struct ofproto_dpif *, struct facet *,
264 struct ofpbuf *packet);
266 static int facet_put__(struct ofproto_dpif *, struct facet *,
267 const struct nlattr *actions, size_t actions_len,
268 struct dpif_flow_stats *);
269 static void facet_install(struct ofproto_dpif *, struct facet *,
271 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
272 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
274 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
275 const struct ofpbuf *packet);
276 static void facet_update_time(struct ofproto_dpif *, struct facet *,
278 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
279 const struct dpif_flow_stats *);
280 static void facet_reset_counters(struct facet *);
281 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
282 static void facet_push_stats(struct facet *);
283 static void facet_account(struct ofproto_dpif *, struct facet *);
285 static bool facet_is_controller_flow(struct facet *);
287 static void flow_push_stats(const struct rule_dpif *,
288 struct flow *, uint64_t packets, uint64_t bytes,
295 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
296 struct list bundle_node; /* In struct ofbundle's "ports" list. */
297 struct cfm *cfm; /* Connectivity Fault Management, if any. */
298 tag_type tag; /* Tag associated with this port. */
299 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
300 bool may_enable; /* May be enabled in bonds. */
303 static struct ofport_dpif *
304 ofport_dpif_cast(const struct ofport *ofport)
306 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
307 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
310 static void port_run(struct ofport_dpif *);
311 static void port_wait(struct ofport_dpif *);
312 static int set_cfm(struct ofport *, const struct cfm_settings *);
314 struct dpif_completion {
315 struct list list_node;
316 struct ofoperation *op;
319 struct ofproto_dpif {
328 struct netflow *netflow;
329 struct dpif_sflow *sflow;
330 struct hmap bundles; /* Contains "struct ofbundle"s. */
331 struct mac_learning *ml;
332 struct ofmirror *mirrors[MAX_MIRRORS];
333 bool has_bonded_bundles;
336 struct timer next_expiration;
340 bool need_revalidate;
341 struct tag_set revalidate_set;
343 /* Support for debugging async flow mods. */
344 struct list completions;
346 bool has_bundle_action; /* True when the first bundle action appears. */
349 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
350 * for debugging the asynchronous flow_mod implementation.) */
353 static void ofproto_dpif_unixctl_init(void);
355 static struct ofproto_dpif *
356 ofproto_dpif_cast(const struct ofproto *ofproto)
358 assert(ofproto->ofproto_class == &ofproto_dpif_class);
359 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
362 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
364 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
367 /* Packet processing. */
368 static void update_learning_table(struct ofproto_dpif *,
369 const struct flow *, int vlan,
371 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
372 bool have_packet, tag_type *, int *vlanp,
373 struct ofbundle **in_bundlep);
374 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
376 /* Flow expiration. */
377 static int expire(struct ofproto_dpif *);
380 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
381 const struct ofpbuf *packet);
383 /* Global variables. */
384 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
386 /* Factory functions. */
389 enumerate_types(struct sset *types)
391 dp_enumerate_types(types);
395 enumerate_names(const char *type, struct sset *names)
397 return dp_enumerate_names(type, names);
401 del(const char *type, const char *name)
406 error = dpif_open(name, type, &dpif);
408 error = dpif_delete(dpif);
414 /* Basic life-cycle. */
416 static struct ofproto *
419 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
424 dealloc(struct ofproto *ofproto_)
426 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
431 construct(struct ofproto *ofproto_, int *n_tablesp)
433 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
434 const char *name = ofproto->up.name;
438 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
440 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
444 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
445 ofproto->n_matches = 0;
447 error = dpif_recv_set_mask(ofproto->dpif,
448 ((1u << DPIF_UC_MISS) |
449 (1u << DPIF_UC_ACTION) |
450 (1u << DPIF_UC_SAMPLE)));
452 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
453 dpif_close(ofproto->dpif);
456 dpif_flow_flush(ofproto->dpif);
457 dpif_recv_purge(ofproto->dpif);
459 ofproto->netflow = NULL;
460 ofproto->sflow = NULL;
461 hmap_init(&ofproto->bundles);
462 ofproto->ml = mac_learning_create();
463 for (i = 0; i < MAX_MIRRORS; i++) {
464 ofproto->mirrors[i] = NULL;
466 ofproto->has_bonded_bundles = false;
468 timer_set_duration(&ofproto->next_expiration, 1000);
470 hmap_init(&ofproto->facets);
471 ofproto->need_revalidate = false;
472 tag_set_init(&ofproto->revalidate_set);
474 list_init(&ofproto->completions);
476 ofproto_dpif_unixctl_init();
478 ofproto->has_bundle_action = false;
480 *n_tablesp = N_TABLES;
485 complete_operations(struct ofproto_dpif *ofproto)
487 struct dpif_completion *c, *next;
489 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
490 ofoperation_complete(c->op, 0);
491 list_remove(&c->list_node);
497 destruct(struct ofproto *ofproto_)
499 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
500 struct rule_dpif *rule, *next_rule;
501 struct classifier *table;
504 complete_operations(ofproto);
506 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
507 struct cls_cursor cursor;
509 cls_cursor_init(&cursor, table, NULL);
510 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
511 ofproto_rule_destroy(&rule->up);
515 for (i = 0; i < MAX_MIRRORS; i++) {
516 mirror_destroy(ofproto->mirrors[i]);
519 netflow_destroy(ofproto->netflow);
520 dpif_sflow_destroy(ofproto->sflow);
521 hmap_destroy(&ofproto->bundles);
522 mac_learning_destroy(ofproto->ml);
524 hmap_destroy(&ofproto->facets);
526 dpif_close(ofproto->dpif);
530 run(struct ofproto *ofproto_)
532 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
533 struct ofport_dpif *ofport;
534 struct ofbundle *bundle;
538 complete_operations(ofproto);
540 dpif_run(ofproto->dpif);
542 for (i = 0; i < 50; i++) {
543 struct dpif_upcall packet;
546 error = dpif_recv(ofproto->dpif, &packet);
548 if (error == ENODEV) {
549 /* Datapath destroyed. */
555 handle_upcall(ofproto, &packet);
558 if (timer_expired(&ofproto->next_expiration)) {
559 int delay = expire(ofproto);
560 timer_set_duration(&ofproto->next_expiration, delay);
563 if (ofproto->netflow) {
564 netflow_run(ofproto->netflow);
566 if (ofproto->sflow) {
567 dpif_sflow_run(ofproto->sflow);
570 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
573 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
577 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
579 /* Now revalidate if there's anything to do. */
580 if (ofproto->need_revalidate
581 || !tag_set_is_empty(&ofproto->revalidate_set)) {
582 struct tag_set revalidate_set = ofproto->revalidate_set;
583 bool revalidate_all = ofproto->need_revalidate;
584 struct facet *facet, *next;
586 /* Clear the revalidation flags. */
587 tag_set_init(&ofproto->revalidate_set);
588 ofproto->need_revalidate = false;
590 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
592 || tag_set_intersects(&revalidate_set, facet->tags)) {
593 facet_revalidate(ofproto, facet);
602 wait(struct ofproto *ofproto_)
604 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
605 struct ofport_dpif *ofport;
606 struct ofbundle *bundle;
608 if (!clogged && !list_is_empty(&ofproto->completions)) {
609 poll_immediate_wake();
612 dpif_wait(ofproto->dpif);
613 dpif_recv_wait(ofproto->dpif);
614 if (ofproto->sflow) {
615 dpif_sflow_wait(ofproto->sflow);
617 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
618 poll_immediate_wake();
620 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
623 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
626 mac_learning_wait(ofproto->ml);
627 if (ofproto->need_revalidate) {
628 /* Shouldn't happen, but if it does just go around again. */
629 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
630 poll_immediate_wake();
632 timer_wait(&ofproto->next_expiration);
637 flush(struct ofproto *ofproto_)
639 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
640 struct facet *facet, *next_facet;
642 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
643 /* Mark the facet as not installed so that facet_remove() doesn't
644 * bother trying to uninstall it. There is no point in uninstalling it
645 * individually since we are about to blow away all the facets with
646 * dpif_flow_flush(). */
647 facet->installed = false;
648 facet->dp_packet_count = 0;
649 facet->dp_byte_count = 0;
650 facet_remove(ofproto, facet);
652 dpif_flow_flush(ofproto->dpif);
656 get_features(struct ofproto *ofproto_ OVS_UNUSED,
657 bool *arp_match_ip, uint32_t *actions)
659 *arp_match_ip = true;
660 *actions = ((1u << OFPAT_OUTPUT) |
661 (1u << OFPAT_SET_VLAN_VID) |
662 (1u << OFPAT_SET_VLAN_PCP) |
663 (1u << OFPAT_STRIP_VLAN) |
664 (1u << OFPAT_SET_DL_SRC) |
665 (1u << OFPAT_SET_DL_DST) |
666 (1u << OFPAT_SET_NW_SRC) |
667 (1u << OFPAT_SET_NW_DST) |
668 (1u << OFPAT_SET_NW_TOS) |
669 (1u << OFPAT_SET_TP_SRC) |
670 (1u << OFPAT_SET_TP_DST) |
671 (1u << OFPAT_ENQUEUE));
675 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
677 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
678 struct ovs_dp_stats s;
680 strcpy(ots->name, "classifier");
682 dpif_get_dp_stats(ofproto->dpif, &s);
683 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
684 put_32aligned_be64(&ots->matched_count,
685 htonll(s.n_hit + ofproto->n_matches));
689 set_netflow(struct ofproto *ofproto_,
690 const struct netflow_options *netflow_options)
692 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
694 if (netflow_options) {
695 if (!ofproto->netflow) {
696 ofproto->netflow = netflow_create();
698 return netflow_set_options(ofproto->netflow, netflow_options);
700 netflow_destroy(ofproto->netflow);
701 ofproto->netflow = NULL;
706 static struct ofport *
709 struct ofport_dpif *port = xmalloc(sizeof *port);
714 port_dealloc(struct ofport *port_)
716 struct ofport_dpif *port = ofport_dpif_cast(port_);
721 port_construct(struct ofport *port_)
723 struct ofport_dpif *port = ofport_dpif_cast(port_);
724 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
726 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
729 port->tag = tag_create_random();
730 port->may_enable = true;
732 if (ofproto->sflow) {
733 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
734 netdev_get_name(port->up.netdev));
741 port_destruct(struct ofport *port_)
743 struct ofport_dpif *port = ofport_dpif_cast(port_);
744 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
746 bundle_remove(port_);
747 set_cfm(port_, NULL);
748 if (ofproto->sflow) {
749 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
754 port_modified(struct ofport *port_)
756 struct ofport_dpif *port = ofport_dpif_cast(port_);
758 if (port->bundle && port->bundle->bond) {
759 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
764 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
766 struct ofport_dpif *port = ofport_dpif_cast(port_);
767 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
768 ovs_be32 changed = old_config ^ port->up.opp.config;
770 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
771 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
772 ofproto->need_revalidate = true;
777 set_sflow(struct ofproto *ofproto_,
778 const struct ofproto_sflow_options *sflow_options)
780 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
781 struct dpif_sflow *ds = ofproto->sflow;
784 struct ofport_dpif *ofport;
786 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
787 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
788 dpif_sflow_add_port(ds, ofport->odp_port,
789 netdev_get_name(ofport->up.netdev));
792 dpif_sflow_set_options(ds, sflow_options);
794 dpif_sflow_destroy(ds);
795 ofproto->sflow = NULL;
801 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
803 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
810 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
813 if (cfm_configure(ofport->cfm, s)) {
819 cfm_destroy(ofport->cfm);
825 get_cfm_fault(const struct ofport *ofport_)
827 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
829 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
833 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
836 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
839 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
848 /* Expires all MAC learning entries associated with 'port' and forces ofproto
849 * to revalidate every flow. */
851 bundle_flush_macs(struct ofbundle *bundle)
853 struct ofproto_dpif *ofproto = bundle->ofproto;
854 struct mac_learning *ml = ofproto->ml;
855 struct mac_entry *mac, *next_mac;
857 ofproto->need_revalidate = true;
858 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
859 if (mac->port.p == bundle) {
860 mac_learning_expire(ml, mac);
865 static struct ofbundle *
866 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
868 struct ofbundle *bundle;
870 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
872 if (bundle->aux == aux) {
879 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
880 * ones that are found to 'bundles'. */
882 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
883 void **auxes, size_t n_auxes,
884 struct hmapx *bundles)
889 for (i = 0; i < n_auxes; i++) {
890 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
892 hmapx_add(bundles, bundle);
898 bundle_del_port(struct ofport_dpif *port)
900 struct ofbundle *bundle = port->bundle;
902 bundle->ofproto->need_revalidate = true;
904 list_remove(&port->bundle_node);
908 lacp_slave_unregister(bundle->lacp, port);
911 bond_slave_unregister(bundle->bond, port);
914 bundle->floodable = true;
915 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
916 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
917 bundle->floodable = false;
923 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
924 struct lacp_slave_settings *lacp,
925 uint32_t bond_stable_id)
927 struct ofport_dpif *port;
929 port = get_ofp_port(bundle->ofproto, ofp_port);
934 if (port->bundle != bundle) {
935 bundle->ofproto->need_revalidate = true;
937 bundle_del_port(port);
940 port->bundle = bundle;
941 list_push_back(&bundle->ports, &port->bundle_node);
942 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
943 bundle->floodable = false;
947 lacp_slave_register(bundle->lacp, port, lacp);
950 port->bond_stable_id = bond_stable_id;
956 bundle_destroy(struct ofbundle *bundle)
958 struct ofproto_dpif *ofproto;
959 struct ofport_dpif *port, *next_port;
966 ofproto = bundle->ofproto;
967 for (i = 0; i < MAX_MIRRORS; i++) {
968 struct ofmirror *m = ofproto->mirrors[i];
970 if (m->out == bundle) {
972 } else if (hmapx_find_and_delete(&m->srcs, bundle)
973 || hmapx_find_and_delete(&m->dsts, bundle)) {
974 ofproto->need_revalidate = true;
979 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
980 bundle_del_port(port);
983 bundle_flush_macs(bundle);
984 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
986 free(bundle->trunks);
987 lacp_destroy(bundle->lacp);
988 bond_destroy(bundle->bond);
993 bundle_set(struct ofproto *ofproto_, void *aux,
994 const struct ofproto_bundle_settings *s)
996 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
997 bool need_flush = false;
998 const unsigned long *trunks;
999 struct ofport_dpif *port;
1000 struct ofbundle *bundle;
1005 bundle_destroy(bundle_lookup(ofproto, aux));
1009 assert(s->n_slaves == 1 || s->bond != NULL);
1010 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1012 bundle = bundle_lookup(ofproto, aux);
1014 bundle = xmalloc(sizeof *bundle);
1016 bundle->ofproto = ofproto;
1017 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1018 hash_pointer(aux, 0));
1020 bundle->name = NULL;
1022 list_init(&bundle->ports);
1024 bundle->trunks = NULL;
1025 bundle->lacp = NULL;
1026 bundle->bond = NULL;
1028 bundle->floodable = true;
1030 bundle->src_mirrors = 0;
1031 bundle->dst_mirrors = 0;
1032 bundle->mirror_out = 0;
1035 if (!bundle->name || strcmp(s->name, bundle->name)) {
1037 bundle->name = xstrdup(s->name);
1042 if (!bundle->lacp) {
1043 bundle->lacp = lacp_create();
1045 lacp_configure(bundle->lacp, s->lacp);
1047 lacp_destroy(bundle->lacp);
1048 bundle->lacp = NULL;
1051 /* Update set of ports. */
1053 for (i = 0; i < s->n_slaves; i++) {
1054 if (!bundle_add_port(bundle, s->slaves[i],
1055 s->lacp ? &s->lacp_slaves[i] : NULL,
1056 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1060 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1061 struct ofport_dpif *next_port;
1063 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1064 for (i = 0; i < s->n_slaves; i++) {
1065 if (s->slaves[i] == port->up.ofp_port) {
1070 bundle_del_port(port);
1074 assert(list_size(&bundle->ports) <= s->n_slaves);
1076 if (list_is_empty(&bundle->ports)) {
1077 bundle_destroy(bundle);
1082 if (s->vlan != bundle->vlan) {
1083 bundle->vlan = s->vlan;
1087 /* Get trunked VLANs. */
1088 trunks = s->vlan == -1 ? NULL : s->trunks;
1089 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1090 free(bundle->trunks);
1091 bundle->trunks = vlan_bitmap_clone(trunks);
1096 if (!list_is_short(&bundle->ports)) {
1097 bundle->ofproto->has_bonded_bundles = true;
1099 if (bond_reconfigure(bundle->bond, s->bond)) {
1100 ofproto->need_revalidate = true;
1103 bundle->bond = bond_create(s->bond);
1104 ofproto->need_revalidate = true;
1107 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1108 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1112 bond_destroy(bundle->bond);
1113 bundle->bond = NULL;
1116 /* If we changed something that would affect MAC learning, un-learn
1117 * everything on this port and force flow revalidation. */
1119 bundle_flush_macs(bundle);
1126 bundle_remove(struct ofport *port_)
1128 struct ofport_dpif *port = ofport_dpif_cast(port_);
1129 struct ofbundle *bundle = port->bundle;
1132 bundle_del_port(port);
1133 if (list_is_empty(&bundle->ports)) {
1134 bundle_destroy(bundle);
1135 } else if (list_is_short(&bundle->ports)) {
1136 bond_destroy(bundle->bond);
1137 bundle->bond = NULL;
1143 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1145 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1146 struct ofport_dpif *port = port_;
1147 uint8_t ea[ETH_ADDR_LEN];
1150 error = netdev_get_etheraddr(port->up.netdev, ea);
1152 struct ofpbuf packet;
1155 ofpbuf_init(&packet, 0);
1156 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1158 memcpy(packet_pdu, pdu, pdu_size);
1160 error = netdev_send(port->up.netdev, &packet);
1162 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1163 "(%s)", port->bundle->name,
1164 netdev_get_name(port->up.netdev), strerror(error));
1166 ofpbuf_uninit(&packet);
1168 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1169 "%s (%s)", port->bundle->name,
1170 netdev_get_name(port->up.netdev), strerror(error));
1175 bundle_send_learning_packets(struct ofbundle *bundle)
1177 struct ofproto_dpif *ofproto = bundle->ofproto;
1178 int error, n_packets, n_errors;
1179 struct mac_entry *e;
1181 error = n_packets = n_errors = 0;
1182 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1183 if (e->port.p != bundle) {
1184 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1194 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1195 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1196 "packets, last error was: %s",
1197 bundle->name, n_errors, n_packets, strerror(error));
1199 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1200 bundle->name, n_packets);
1205 bundle_run(struct ofbundle *bundle)
1208 lacp_run(bundle->lacp, send_pdu_cb);
1211 struct ofport_dpif *port;
1213 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1214 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1217 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1218 lacp_negotiated(bundle->lacp));
1219 if (bond_should_send_learning_packets(bundle->bond)) {
1220 bundle_send_learning_packets(bundle);
1226 bundle_wait(struct ofbundle *bundle)
1229 lacp_wait(bundle->lacp);
1232 bond_wait(bundle->bond);
1239 mirror_scan(struct ofproto_dpif *ofproto)
1243 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1244 if (!ofproto->mirrors[idx]) {
1251 static struct ofmirror *
1252 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1256 for (i = 0; i < MAX_MIRRORS; i++) {
1257 struct ofmirror *mirror = ofproto->mirrors[i];
1258 if (mirror && mirror->aux == aux) {
1267 mirror_set(struct ofproto *ofproto_, void *aux,
1268 const struct ofproto_mirror_settings *s)
1270 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1271 mirror_mask_t mirror_bit;
1272 struct ofbundle *bundle;
1273 struct ofmirror *mirror;
1274 struct ofbundle *out;
1275 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1276 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1279 mirror = mirror_lookup(ofproto, aux);
1281 mirror_destroy(mirror);
1287 idx = mirror_scan(ofproto);
1289 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1291 ofproto->up.name, MAX_MIRRORS, s->name);
1295 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1296 mirror->ofproto = ofproto;
1299 mirror->out_vlan = -1;
1300 mirror->name = NULL;
1303 if (!mirror->name || strcmp(s->name, mirror->name)) {
1305 mirror->name = xstrdup(s->name);
1308 /* Get the new configuration. */
1309 if (s->out_bundle) {
1310 out = bundle_lookup(ofproto, s->out_bundle);
1312 mirror_destroy(mirror);
1318 out_vlan = s->out_vlan;
1320 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1321 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1323 /* If the configuration has not changed, do nothing. */
1324 if (hmapx_equals(&srcs, &mirror->srcs)
1325 && hmapx_equals(&dsts, &mirror->dsts)
1326 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1327 && mirror->out == out
1328 && mirror->out_vlan == out_vlan)
1330 hmapx_destroy(&srcs);
1331 hmapx_destroy(&dsts);
1335 hmapx_swap(&srcs, &mirror->srcs);
1336 hmapx_destroy(&srcs);
1338 hmapx_swap(&dsts, &mirror->dsts);
1339 hmapx_destroy(&dsts);
1341 free(mirror->vlans);
1342 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1345 mirror->out_vlan = out_vlan;
1347 /* Update bundles. */
1348 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1349 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1350 if (hmapx_contains(&mirror->srcs, bundle)) {
1351 bundle->src_mirrors |= mirror_bit;
1353 bundle->src_mirrors &= ~mirror_bit;
1356 if (hmapx_contains(&mirror->dsts, bundle)) {
1357 bundle->dst_mirrors |= mirror_bit;
1359 bundle->dst_mirrors &= ~mirror_bit;
1362 if (mirror->out == bundle) {
1363 bundle->mirror_out |= mirror_bit;
1365 bundle->mirror_out &= ~mirror_bit;
1369 ofproto->need_revalidate = true;
1370 mac_learning_flush(ofproto->ml);
1376 mirror_destroy(struct ofmirror *mirror)
1378 struct ofproto_dpif *ofproto;
1379 mirror_mask_t mirror_bit;
1380 struct ofbundle *bundle;
1386 ofproto = mirror->ofproto;
1387 ofproto->need_revalidate = true;
1388 mac_learning_flush(ofproto->ml);
1390 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1391 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1392 bundle->src_mirrors &= ~mirror_bit;
1393 bundle->dst_mirrors &= ~mirror_bit;
1394 bundle->mirror_out &= ~mirror_bit;
1397 hmapx_destroy(&mirror->srcs);
1398 hmapx_destroy(&mirror->dsts);
1399 free(mirror->vlans);
1401 ofproto->mirrors[mirror->idx] = NULL;
1407 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1409 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1410 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1411 ofproto->need_revalidate = true;
1412 mac_learning_flush(ofproto->ml);
1418 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1420 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1421 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1422 return bundle && bundle->mirror_out != 0;
1426 forward_bpdu_changed(struct ofproto *ofproto_)
1428 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1429 /* Revalidate cached flows whenever forward_bpdu option changes. */
1430 ofproto->need_revalidate = true;
1435 static struct ofport_dpif *
1436 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1438 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1439 return ofport ? ofport_dpif_cast(ofport) : NULL;
1442 static struct ofport_dpif *
1443 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1445 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1449 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1450 struct dpif_port *dpif_port)
1452 ofproto_port->name = dpif_port->name;
1453 ofproto_port->type = dpif_port->type;
1454 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1458 port_run(struct ofport_dpif *ofport)
1460 bool enable = netdev_get_carrier(ofport->up.netdev);
1463 cfm_run(ofport->cfm);
1465 if (cfm_should_send_ccm(ofport->cfm)) {
1466 struct ofpbuf packet;
1468 ofpbuf_init(&packet, 0);
1469 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1470 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1471 ofport->odp_port, &packet);
1472 ofpbuf_uninit(&packet);
1475 enable = enable && !cfm_get_fault(ofport->cfm);
1478 if (ofport->bundle) {
1479 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1482 if (ofport->may_enable != enable) {
1483 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1485 if (ofproto->has_bundle_action) {
1486 ofproto->need_revalidate = true;
1490 ofport->may_enable = enable;
1494 port_wait(struct ofport_dpif *ofport)
1497 cfm_wait(ofport->cfm);
1502 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1503 struct ofproto_port *ofproto_port)
1505 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1506 struct dpif_port dpif_port;
1509 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1511 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1517 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1519 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1523 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1525 *ofp_portp = odp_port_to_ofp_port(odp_port);
1531 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1533 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1536 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1538 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1540 /* The caller is going to close ofport->up.netdev. If this is a
1541 * bonded port, then the bond is using that netdev, so remove it
1542 * from the bond. The client will need to reconfigure everything
1543 * after deleting ports, so then the slave will get re-added. */
1544 bundle_remove(&ofport->up);
1550 struct port_dump_state {
1551 struct dpif_port_dump dump;
1556 port_dump_start(const struct ofproto *ofproto_, void **statep)
1558 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1559 struct port_dump_state *state;
1561 *statep = state = xmalloc(sizeof *state);
1562 dpif_port_dump_start(&state->dump, ofproto->dpif);
1563 state->done = false;
1568 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1569 struct ofproto_port *port)
1571 struct port_dump_state *state = state_;
1572 struct dpif_port dpif_port;
1574 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1575 ofproto_port_from_dpif_port(port, &dpif_port);
1578 int error = dpif_port_dump_done(&state->dump);
1580 return error ? error : EOF;
1585 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1587 struct port_dump_state *state = state_;
1590 dpif_port_dump_done(&state->dump);
1597 port_poll(const struct ofproto *ofproto_, char **devnamep)
1599 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1600 return dpif_port_poll(ofproto->dpif, devnamep);
1604 port_poll_wait(const struct ofproto *ofproto_)
1606 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1607 dpif_port_poll_wait(ofproto->dpif);
1611 port_is_lacp_current(const struct ofport *ofport_)
1613 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1614 return (ofport->bundle && ofport->bundle->lacp
1615 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1619 /* Upcall handling. */
1621 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1622 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1623 * their individual configurations.
1625 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1626 * Otherwise, ownership is transferred to this function. */
1628 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1629 const struct flow *flow, bool clone)
1631 struct ofputil_packet_in pin;
1633 pin.packet = upcall->packet;
1634 pin.in_port = flow->in_port;
1635 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1636 pin.buffer_id = 0; /* not yet known */
1637 pin.send_len = upcall->userdata;
1638 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1639 clone ? NULL : upcall->packet);
1643 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1644 const struct ofpbuf *packet)
1646 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1652 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
1654 cfm_process_heartbeat(ofport->cfm, packet);
1657 } else if (ofport->bundle && ofport->bundle->lacp
1658 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1660 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
1668 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1670 struct facet *facet;
1673 /* Obtain in_port and tun_id, at least. */
1674 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1676 /* Set header pointers in 'flow'. */
1677 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1679 /* Handle 802.1ag and LACP. */
1680 if (process_special(ofproto, &flow, upcall->packet)) {
1681 ofpbuf_delete(upcall->packet);
1682 ofproto->n_matches++;
1686 /* Check with in-band control to see if this packet should be sent
1687 * to the local port regardless of the flow table. */
1688 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1689 send_packet(ofproto, OVSP_LOCAL, upcall->packet);
1692 facet = facet_lookup_valid(ofproto, &flow);
1694 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow, 0);
1696 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1697 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1699 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1700 COVERAGE_INC(ofproto_dpif_no_packet_in);
1701 /* XXX install 'drop' flow entry */
1702 ofpbuf_delete(upcall->packet);
1706 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1710 send_packet_in(ofproto, upcall, &flow, false);
1714 facet = facet_create(rule, &flow, upcall->packet);
1715 } else if (!facet->may_install) {
1716 /* The facet is not installable, that is, we need to process every
1717 * packet, so process the current packet's actions into 'facet'. */
1718 facet_make_actions(ofproto, facet, upcall->packet);
1721 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1723 * Extra-special case for fail-open mode.
1725 * We are in fail-open mode and the packet matched the fail-open rule,
1726 * but we are connected to a controller too. We should send the packet
1727 * up to the controller in the hope that it will try to set up a flow
1728 * and thereby allow us to exit fail-open.
1730 * See the top-level comment in fail-open.c for more information.
1732 send_packet_in(ofproto, upcall, &flow, true);
1735 facet_execute(ofproto, facet, upcall->packet);
1736 facet_install(ofproto, facet, false);
1737 ofproto->n_matches++;
1741 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1745 switch (upcall->type) {
1746 case DPIF_UC_ACTION:
1747 COVERAGE_INC(ofproto_dpif_ctlr_action);
1748 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1749 send_packet_in(ofproto, upcall, &flow, false);
1752 case DPIF_UC_SAMPLE:
1753 if (ofproto->sflow) {
1754 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1755 dpif_sflow_received(ofproto->sflow, upcall, &flow);
1757 ofpbuf_delete(upcall->packet);
1761 handle_miss_upcall(ofproto, upcall);
1764 case DPIF_N_UC_TYPES:
1766 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1771 /* Flow expiration. */
1773 static int facet_max_idle(const struct ofproto_dpif *);
1774 static void update_stats(struct ofproto_dpif *);
1775 static void rule_expire(struct rule_dpif *);
1776 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1778 /* This function is called periodically by run(). Its job is to collect
1779 * updates for the flows that have been installed into the datapath, most
1780 * importantly when they last were used, and then use that information to
1781 * expire flows that have not been used recently.
1783 * Returns the number of milliseconds after which it should be called again. */
1785 expire(struct ofproto_dpif *ofproto)
1787 struct rule_dpif *rule, *next_rule;
1788 struct classifier *table;
1791 /* Update stats for each flow in the datapath. */
1792 update_stats(ofproto);
1794 /* Expire facets that have been idle too long. */
1795 dp_max_idle = facet_max_idle(ofproto);
1796 expire_facets(ofproto, dp_max_idle);
1798 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1799 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1800 struct cls_cursor cursor;
1802 cls_cursor_init(&cursor, table, NULL);
1803 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1808 /* All outstanding data in existing flows has been accounted, so it's a
1809 * good time to do bond rebalancing. */
1810 if (ofproto->has_bonded_bundles) {
1811 struct ofbundle *bundle;
1813 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1815 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1820 return MIN(dp_max_idle, 1000);
1823 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1825 * This function also pushes statistics updates to rules which each facet
1826 * resubmits into. Generally these statistics will be accurate. However, if a
1827 * facet changes the rule it resubmits into at some time in between
1828 * update_stats() runs, it is possible that statistics accrued to the
1829 * old rule will be incorrectly attributed to the new rule. This could be
1830 * avoided by calling update_stats() whenever rules are created or
1831 * deleted. However, the performance impact of making so many calls to the
1832 * datapath do not justify the benefit of having perfectly accurate statistics.
1835 update_stats(struct ofproto_dpif *p)
1837 const struct dpif_flow_stats *stats;
1838 struct dpif_flow_dump dump;
1839 const struct nlattr *key;
1842 dpif_flow_dump_start(&dump, p->dpif);
1843 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1844 struct facet *facet;
1847 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1851 odp_flow_key_format(key, key_len, &s);
1852 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
1858 facet = facet_find(p, &flow);
1860 if (facet && facet->installed) {
1862 if (stats->n_packets >= facet->dp_packet_count) {
1863 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1864 facet->packet_count += extra;
1866 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1869 if (stats->n_bytes >= facet->dp_byte_count) {
1870 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1872 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1875 facet->dp_packet_count = stats->n_packets;
1876 facet->dp_byte_count = stats->n_bytes;
1878 facet_update_time(p, facet, stats->used);
1879 facet_account(p, facet);
1880 facet_push_stats(facet);
1882 /* There's a flow in the datapath that we know nothing about.
1884 COVERAGE_INC(facet_unexpected);
1885 dpif_flow_del(p->dpif, key, key_len, NULL);
1888 dpif_flow_dump_done(&dump);
1891 /* Calculates and returns the number of milliseconds of idle time after which
1892 * facets should expire from the datapath and we should fold their statistics
1893 * into their parent rules in userspace. */
1895 facet_max_idle(const struct ofproto_dpif *ofproto)
1898 * Idle time histogram.
1900 * Most of the time a switch has a relatively small number of facets. When
1901 * this is the case we might as well keep statistics for all of them in
1902 * userspace and to cache them in the kernel datapath for performance as
1905 * As the number of facets increases, the memory required to maintain
1906 * statistics about them in userspace and in the kernel becomes
1907 * significant. However, with a large number of facets it is likely that
1908 * only a few of them are "heavy hitters" that consume a large amount of
1909 * bandwidth. At this point, only heavy hitters are worth caching in the
1910 * kernel and maintaining in userspaces; other facets we can discard.
1912 * The technique used to compute the idle time is to build a histogram with
1913 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1914 * that is installed in the kernel gets dropped in the appropriate bucket.
1915 * After the histogram has been built, we compute the cutoff so that only
1916 * the most-recently-used 1% of facets (but at least
1917 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1918 * the most-recently-used bucket of facets is kept, so actually an
1919 * arbitrary number of facets can be kept in any given expiration run
1920 * (though the next run will delete most of those unless they receive
1923 * This requires a second pass through the facets, in addition to the pass
1924 * made by update_stats(), because the former function never looks
1925 * at uninstallable facets.
1927 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1928 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1929 int buckets[N_BUCKETS] = { 0 };
1930 int total, subtotal, bucket;
1931 struct facet *facet;
1935 total = hmap_count(&ofproto->facets);
1936 if (total <= ofproto->up.flow_eviction_threshold) {
1937 return N_BUCKETS * BUCKET_WIDTH;
1940 /* Build histogram. */
1942 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1943 long long int idle = now - facet->used;
1944 int bucket = (idle <= 0 ? 0
1945 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1946 : (unsigned int) idle / BUCKET_WIDTH);
1950 /* Find the first bucket whose flows should be expired. */
1951 subtotal = bucket = 0;
1953 subtotal += buckets[bucket++];
1954 } while (bucket < N_BUCKETS &&
1955 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
1957 if (VLOG_IS_DBG_ENABLED()) {
1961 ds_put_cstr(&s, "keep");
1962 for (i = 0; i < N_BUCKETS; i++) {
1964 ds_put_cstr(&s, ", drop");
1967 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1970 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1974 return bucket * BUCKET_WIDTH;
1978 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1980 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1981 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1982 struct ofexpired expired;
1984 if (facet->installed) {
1985 struct dpif_flow_stats stats;
1987 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1989 facet_update_stats(ofproto, facet, &stats);
1992 expired.flow = facet->flow;
1993 expired.packet_count = facet->packet_count;
1994 expired.byte_count = facet->byte_count;
1995 expired.used = facet->used;
1996 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2001 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2003 long long int cutoff = time_msec() - dp_max_idle;
2004 struct facet *facet, *next_facet;
2006 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2007 facet_active_timeout(ofproto, facet);
2008 if (facet->used < cutoff) {
2009 facet_remove(ofproto, facet);
2014 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2015 * then delete it entirely. */
2017 rule_expire(struct rule_dpif *rule)
2019 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2020 struct facet *facet, *next_facet;
2024 /* Has 'rule' expired? */
2026 if (rule->up.hard_timeout
2027 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2028 reason = OFPRR_HARD_TIMEOUT;
2029 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2030 && now > rule->used + rule->up.idle_timeout * 1000) {
2031 reason = OFPRR_IDLE_TIMEOUT;
2036 COVERAGE_INC(ofproto_dpif_expired);
2038 /* Update stats. (This is a no-op if the rule expired due to an idle
2039 * timeout, because that only happens when the rule has no facets left.) */
2040 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2041 facet_remove(ofproto, facet);
2044 /* Get rid of the rule. */
2045 ofproto_rule_expire(&rule->up, reason);
2050 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2051 * example 'packet' within that flow.
2053 * The caller must already have determined that no facet with an identical
2054 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2055 * the ofproto's classifier table. */
2056 static struct facet *
2057 facet_create(struct rule_dpif *rule, const struct flow *flow,
2058 const struct ofpbuf *packet)
2060 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2061 struct facet *facet;
2063 facet = xzalloc(sizeof *facet);
2064 facet->used = time_msec();
2065 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2066 list_push_back(&rule->facets, &facet->list_node);
2068 facet->flow = *flow;
2069 netflow_flow_init(&facet->nf_flow);
2070 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2072 facet_make_actions(ofproto, facet, packet);
2078 facet_free(struct facet *facet)
2080 free(facet->actions);
2084 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2085 * 'packet', which arrived on 'in_port'.
2087 * Takes ownership of 'packet'. */
2089 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2090 const struct nlattr *odp_actions, size_t actions_len,
2091 struct ofpbuf *packet)
2093 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2094 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE) {
2095 /* As an optimization, avoid a round-trip from userspace to kernel to
2096 * userspace. This also avoids possibly filling up kernel packet
2097 * buffers along the way. */
2098 struct dpif_upcall upcall;
2100 upcall.type = DPIF_UC_ACTION;
2101 upcall.packet = packet;
2104 upcall.userdata = nl_attr_get_u64(odp_actions);
2105 upcall.sample_pool = 0;
2106 upcall.actions = NULL;
2107 upcall.actions_len = 0;
2109 send_packet_in(ofproto, &upcall, flow, false);
2113 struct odputil_keybuf keybuf;
2117 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2118 odp_flow_key_from_flow(&key, flow);
2120 error = dpif_execute(ofproto->dpif, key.data, key.size,
2121 odp_actions, actions_len, packet);
2123 ofpbuf_delete(packet);
2128 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2129 * statistics appropriately. 'packet' must have at least sizeof(struct
2130 * ofp_packet_in) bytes of headroom.
2132 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2133 * applying flow_extract() to 'packet' would yield the same flow as
2136 * 'facet' must have accurately composed datapath actions; that is, it must
2137 * not be in need of revalidation.
2139 * Takes ownership of 'packet'. */
2141 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2142 struct ofpbuf *packet)
2144 struct dpif_flow_stats stats;
2146 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2148 flow_extract_stats(&facet->flow, packet, &stats);
2149 stats.used = time_msec();
2150 if (execute_odp_actions(ofproto, &facet->flow,
2151 facet->actions, facet->actions_len, packet)) {
2152 facet_update_stats(ofproto, facet, &stats);
2156 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2158 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2159 * rule's statistics, via facet_uninstall().
2161 * - Removes 'facet' from its rule and from ofproto->facets.
2164 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2166 facet_uninstall(ofproto, facet);
2167 facet_flush_stats(ofproto, facet);
2168 hmap_remove(&ofproto->facets, &facet->hmap_node);
2169 list_remove(&facet->list_node);
2173 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2175 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2176 const struct ofpbuf *packet)
2178 const struct rule_dpif *rule = facet->rule;
2179 struct ofpbuf *odp_actions;
2180 struct action_xlate_ctx ctx;
2182 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2183 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2184 facet->tags = ctx.tags;
2185 facet->may_install = ctx.may_set_up_flow;
2186 facet->has_learn = ctx.has_learn;
2187 facet->has_normal = ctx.has_normal;
2188 facet->nf_flow.output_iface = ctx.nf_output_iface;
2190 if (facet->actions_len != odp_actions->size
2191 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2192 free(facet->actions);
2193 facet->actions_len = odp_actions->size;
2194 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2197 ofpbuf_delete(odp_actions);
2200 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2201 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2202 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2203 * since 'facet' was last updated.
2205 * Returns 0 if successful, otherwise a positive errno value.*/
2207 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2208 const struct nlattr *actions, size_t actions_len,
2209 struct dpif_flow_stats *stats)
2211 struct odputil_keybuf keybuf;
2212 enum dpif_flow_put_flags flags;
2216 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2218 flags |= DPIF_FP_ZERO_STATS;
2221 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2222 odp_flow_key_from_flow(&key, &facet->flow);
2224 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2225 actions, actions_len, stats);
2228 facet_reset_dp_stats(facet, stats);
2234 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2235 * 'zero_stats' is true, clears any existing statistics from the datapath for
2238 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2240 struct dpif_flow_stats stats;
2242 if (facet->may_install
2243 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2244 zero_stats ? &stats : NULL)) {
2245 facet->installed = true;
2250 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2252 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2256 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2259 const struct nlattr *a;
2263 if (facet->byte_count <= facet->accounted_bytes) {
2266 n_bytes = facet->byte_count - facet->accounted_bytes;
2267 facet->accounted_bytes = facet->byte_count;
2269 /* Feed information from the active flows back into the learning table to
2270 * ensure that table is always in sync with what is actually flowing
2271 * through the datapath. */
2272 if (facet->has_learn || facet->has_normal) {
2273 struct action_xlate_ctx ctx;
2275 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2276 ctx.may_learn = true;
2277 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2278 facet->rule->up.n_actions));
2281 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2285 /* This loop feeds byte counters to bond_account() for rebalancing to use
2286 * as a basis. We also need to track the actual VLAN on which the packet
2287 * is going to be sent to ensure that it matches the one passed to
2288 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2290 vlan_tci = facet->flow.vlan_tci;
2291 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2292 struct ofport_dpif *port;
2294 switch (nl_attr_type(a)) {
2295 case OVS_ACTION_ATTR_OUTPUT:
2296 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2297 if (port && port->bundle && port->bundle->bond) {
2298 bond_account(port->bundle->bond, &facet->flow,
2299 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2303 case OVS_ACTION_ATTR_POP_VLAN:
2304 vlan_tci = htons(0);
2307 case OVS_ACTION_ATTR_PUSH_VLAN:
2308 vlan_tci = nl_attr_get_be16(a);
2314 /* If 'rule' is installed in the datapath, uninstalls it. */
2316 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2318 if (facet->installed) {
2319 struct odputil_keybuf keybuf;
2320 struct dpif_flow_stats stats;
2324 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2325 odp_flow_key_from_flow(&key, &facet->flow);
2327 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2328 facet_reset_dp_stats(facet, &stats);
2330 facet_update_stats(p, facet, &stats);
2332 facet->installed = false;
2334 assert(facet->dp_packet_count == 0);
2335 assert(facet->dp_byte_count == 0);
2339 /* Returns true if the only action for 'facet' is to send to the controller.
2340 * (We don't report NetFlow expiration messages for such facets because they
2341 * are just part of the control logic for the network, not real traffic). */
2343 facet_is_controller_flow(struct facet *facet)
2346 && facet->rule->up.n_actions == 1
2347 && action_outputs_to_port(&facet->rule->up.actions[0],
2348 htons(OFPP_CONTROLLER)));
2351 /* Resets 'facet''s datapath statistics counters. This should be called when
2352 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2353 * it should contain the statistics returned by dpif when 'facet' was reset in
2354 * the datapath. 'stats' will be modified to only included statistics new
2355 * since 'facet' was last updated. */
2357 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2359 if (stats && facet->dp_packet_count <= stats->n_packets
2360 && facet->dp_byte_count <= stats->n_bytes) {
2361 stats->n_packets -= facet->dp_packet_count;
2362 stats->n_bytes -= facet->dp_byte_count;
2365 facet->dp_packet_count = 0;
2366 facet->dp_byte_count = 0;
2369 /* Folds all of 'facet''s statistics into its rule. Also updates the
2370 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2371 * 'facet''s statistics in the datapath should have been zeroed and folded into
2372 * its packet and byte counts before this function is called. */
2374 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2376 assert(!facet->dp_byte_count);
2377 assert(!facet->dp_packet_count);
2379 facet_push_stats(facet);
2380 facet_account(ofproto, facet);
2382 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2383 struct ofexpired expired;
2384 expired.flow = facet->flow;
2385 expired.packet_count = facet->packet_count;
2386 expired.byte_count = facet->byte_count;
2387 expired.used = facet->used;
2388 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2391 facet->rule->packet_count += facet->packet_count;
2392 facet->rule->byte_count += facet->byte_count;
2394 /* Reset counters to prevent double counting if 'facet' ever gets
2396 facet_reset_counters(facet);
2398 netflow_flow_clear(&facet->nf_flow);
2401 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2402 * Returns it if found, otherwise a null pointer.
2404 * The returned facet might need revalidation; use facet_lookup_valid()
2405 * instead if that is important. */
2406 static struct facet *
2407 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2409 struct facet *facet;
2411 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2413 if (flow_equal(flow, &facet->flow)) {
2421 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2422 * Returns it if found, otherwise a null pointer.
2424 * The returned facet is guaranteed to be valid. */
2425 static struct facet *
2426 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2428 struct facet *facet = facet_find(ofproto, flow);
2430 /* The facet we found might not be valid, since we could be in need of
2431 * revalidation. If it is not valid, don't return it. */
2433 && ofproto->need_revalidate
2434 && !facet_revalidate(ofproto, facet)) {
2435 COVERAGE_INC(facet_invalidated);
2442 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2444 * - If the rule found is different from 'facet''s current rule, moves
2445 * 'facet' to the new rule and recompiles its actions.
2447 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2448 * where it is and recompiles its actions anyway.
2450 * - If there is none, destroys 'facet'.
2452 * Returns true if 'facet' still exists, false if it has been destroyed. */
2454 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2456 struct action_xlate_ctx ctx;
2457 struct ofpbuf *odp_actions;
2458 struct rule_dpif *new_rule;
2459 bool actions_changed;
2461 COVERAGE_INC(facet_revalidate);
2463 /* Determine the new rule. */
2464 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
2466 /* No new rule, so delete the facet. */
2467 facet_remove(ofproto, facet);
2471 /* Calculate new datapath actions.
2473 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2474 * emit a NetFlow expiration and, if so, we need to have the old state
2475 * around to properly compose it. */
2476 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2477 odp_actions = xlate_actions(&ctx,
2478 new_rule->up.actions, new_rule->up.n_actions);
2479 actions_changed = (facet->actions_len != odp_actions->size
2480 || memcmp(facet->actions, odp_actions->data,
2481 facet->actions_len));
2483 /* If the datapath actions changed or the installability changed,
2484 * then we need to talk to the datapath. */
2485 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2486 if (ctx.may_set_up_flow) {
2487 struct dpif_flow_stats stats;
2489 facet_put__(ofproto, facet,
2490 odp_actions->data, odp_actions->size, &stats);
2491 facet_update_stats(ofproto, facet, &stats);
2493 facet_uninstall(ofproto, facet);
2496 /* The datapath flow is gone or has zeroed stats, so push stats out of
2497 * 'facet' into 'rule'. */
2498 facet_flush_stats(ofproto, facet);
2501 /* Update 'facet' now that we've taken care of all the old state. */
2502 facet->tags = ctx.tags;
2503 facet->nf_flow.output_iface = ctx.nf_output_iface;
2504 facet->may_install = ctx.may_set_up_flow;
2505 facet->has_learn = ctx.has_learn;
2506 facet->has_normal = ctx.has_normal;
2507 if (actions_changed) {
2508 free(facet->actions);
2509 facet->actions_len = odp_actions->size;
2510 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2512 if (facet->rule != new_rule) {
2513 COVERAGE_INC(facet_changed_rule);
2514 list_remove(&facet->list_node);
2515 list_push_back(&new_rule->facets, &facet->list_node);
2516 facet->rule = new_rule;
2517 facet->used = new_rule->up.created;
2518 facet->rs_used = facet->used;
2521 ofpbuf_delete(odp_actions);
2526 /* Updates 'facet''s used time. Caller is responsible for calling
2527 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2529 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2532 if (used > facet->used) {
2534 if (used > facet->rule->used) {
2535 facet->rule->used = used;
2537 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2541 /* Folds the statistics from 'stats' into the counters in 'facet'.
2543 * Because of the meaning of a facet's counters, it only makes sense to do this
2544 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2545 * packet that was sent by hand or if it represents statistics that have been
2546 * cleared out of the datapath. */
2548 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2549 const struct dpif_flow_stats *stats)
2551 if (stats->n_packets || stats->used > facet->used) {
2552 facet_update_time(ofproto, facet, stats->used);
2553 facet->packet_count += stats->n_packets;
2554 facet->byte_count += stats->n_bytes;
2555 facet_push_stats(facet);
2556 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2561 facet_reset_counters(struct facet *facet)
2563 facet->packet_count = 0;
2564 facet->byte_count = 0;
2565 facet->rs_packet_count = 0;
2566 facet->rs_byte_count = 0;
2567 facet->accounted_bytes = 0;
2571 facet_push_stats(struct facet *facet)
2573 uint64_t rs_packets, rs_bytes;
2575 assert(facet->packet_count >= facet->rs_packet_count);
2576 assert(facet->byte_count >= facet->rs_byte_count);
2577 assert(facet->used >= facet->rs_used);
2579 rs_packets = facet->packet_count - facet->rs_packet_count;
2580 rs_bytes = facet->byte_count - facet->rs_byte_count;
2582 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2583 facet->rs_packet_count = facet->packet_count;
2584 facet->rs_byte_count = facet->byte_count;
2585 facet->rs_used = facet->used;
2587 flow_push_stats(facet->rule, &facet->flow,
2588 rs_packets, rs_bytes, facet->used);
2592 struct ofproto_push {
2593 struct action_xlate_ctx ctx;
2600 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2602 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2605 rule->packet_count += push->packets;
2606 rule->byte_count += push->bytes;
2607 rule->used = MAX(push->used, rule->used);
2611 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2612 * 'rule''s actions. */
2614 flow_push_stats(const struct rule_dpif *rule,
2615 struct flow *flow, uint64_t packets, uint64_t bytes,
2618 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2619 struct ofproto_push push;
2621 push.packets = packets;
2625 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2626 push.ctx.resubmit_hook = push_resubmit;
2627 ofpbuf_delete(xlate_actions(&push.ctx,
2628 rule->up.actions, rule->up.n_actions));
2633 static struct rule_dpif *
2634 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2637 if (table_id >= N_TABLES) {
2641 return rule_dpif_cast(rule_from_cls_rule(
2642 classifier_lookup(&ofproto->up.tables[table_id],
2647 complete_operation(struct rule_dpif *rule)
2649 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2651 ofproto->need_revalidate = true;
2653 struct dpif_completion *c = xmalloc(sizeof *c);
2654 c->op = rule->up.pending;
2655 list_push_back(&ofproto->completions, &c->list_node);
2657 ofoperation_complete(rule->up.pending, 0);
2661 static struct rule *
2664 struct rule_dpif *rule = xmalloc(sizeof *rule);
2669 rule_dealloc(struct rule *rule_)
2671 struct rule_dpif *rule = rule_dpif_cast(rule_);
2676 rule_construct(struct rule *rule_)
2678 struct rule_dpif *rule = rule_dpif_cast(rule_);
2679 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2680 struct rule_dpif *victim;
2683 error = validate_actions(rule->up.actions, rule->up.n_actions,
2684 &rule->up.cr.flow, ofproto->max_ports);
2689 rule->used = rule->up.created;
2690 rule->packet_count = 0;
2691 rule->byte_count = 0;
2693 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2694 if (victim && !list_is_empty(&victim->facets)) {
2695 struct facet *facet;
2697 rule->facets = victim->facets;
2698 list_moved(&rule->facets);
2699 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2700 /* XXX: We're only clearing our local counters here. It's possible
2701 * that quite a few packets are unaccounted for in the datapath
2702 * statistics. These will be accounted to the new rule instead of
2703 * cleared as required. This could be fixed by clearing out the
2704 * datapath statistics for this facet, but currently it doesn't
2706 facet_reset_counters(facet);
2710 /* Must avoid list_moved() in this case. */
2711 list_init(&rule->facets);
2714 complete_operation(rule);
2719 rule_destruct(struct rule *rule_)
2721 struct rule_dpif *rule = rule_dpif_cast(rule_);
2722 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2723 struct facet *facet, *next_facet;
2725 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2726 facet_revalidate(ofproto, facet);
2729 complete_operation(rule);
2733 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2735 struct rule_dpif *rule = rule_dpif_cast(rule_);
2736 struct facet *facet;
2738 /* Start from historical data for 'rule' itself that are no longer tracked
2739 * in facets. This counts, for example, facets that have expired. */
2740 *packets = rule->packet_count;
2741 *bytes = rule->byte_count;
2743 /* Add any statistics that are tracked by facets. This includes
2744 * statistical data recently updated by ofproto_update_stats() as well as
2745 * stats for packets that were executed "by hand" via dpif_execute(). */
2746 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2747 *packets += facet->packet_count;
2748 *bytes += facet->byte_count;
2753 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2755 struct rule_dpif *rule = rule_dpif_cast(rule_);
2756 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2757 struct action_xlate_ctx ctx;
2758 struct ofpbuf *odp_actions;
2759 struct facet *facet;
2762 /* First look for a related facet. If we find one, account it to that. */
2763 facet = facet_lookup_valid(ofproto, flow);
2764 if (facet && facet->rule == rule) {
2765 facet_execute(ofproto, facet, packet);
2769 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2770 * create a new facet for it and use that. */
2771 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
2772 facet = facet_create(rule, flow, packet);
2773 facet_execute(ofproto, facet, packet);
2774 facet_install(ofproto, facet, true);
2778 /* We can't account anything to a facet. If we were to try, then that
2779 * facet would have a non-matching rule, busting our invariants. */
2780 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2781 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2782 size = packet->size;
2783 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2784 odp_actions->size, packet)) {
2785 rule->used = time_msec();
2786 rule->packet_count++;
2787 rule->byte_count += size;
2788 flow_push_stats(rule, flow, 1, size, rule->used);
2790 ofpbuf_delete(odp_actions);
2796 rule_modify_actions(struct rule *rule_)
2798 struct rule_dpif *rule = rule_dpif_cast(rule_);
2799 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2802 error = validate_actions(rule->up.actions, rule->up.n_actions,
2803 &rule->up.cr.flow, ofproto->max_ports);
2805 ofoperation_complete(rule->up.pending, error);
2809 complete_operation(rule);
2812 /* Sends 'packet' out of port 'odp_port' within 'p'.
2813 * Returns 0 if successful, otherwise a positive errno value. */
2815 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2816 const struct ofpbuf *packet)
2818 struct ofpbuf key, odp_actions;
2819 struct odputil_keybuf keybuf;
2823 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2824 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2825 odp_flow_key_from_flow(&key, &flow);
2827 ofpbuf_init(&odp_actions, 32);
2828 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2829 error = dpif_execute(ofproto->dpif,
2831 odp_actions.data, odp_actions.size,
2833 ofpbuf_uninit(&odp_actions);
2836 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2837 ofproto->up.name, odp_port, strerror(error));
2842 /* OpenFlow to datapath action translation. */
2844 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2845 struct action_xlate_ctx *ctx);
2846 static void xlate_normal(struct action_xlate_ctx *);
2849 commit_odp_actions(struct action_xlate_ctx *ctx)
2851 const struct flow *flow = &ctx->flow;
2852 struct flow *base = &ctx->base_flow;
2853 struct ofpbuf *odp_actions = ctx->odp_actions;
2855 if (base->tun_id != flow->tun_id) {
2856 nl_msg_put_be64(odp_actions, OVS_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
2857 base->tun_id = flow->tun_id;
2860 if (base->nw_src != flow->nw_src) {
2861 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
2862 base->nw_src = flow->nw_src;
2865 if (base->nw_dst != flow->nw_dst) {
2866 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
2867 base->nw_dst = flow->nw_dst;
2870 if (base->nw_tos != flow->nw_tos) {
2871 nl_msg_put_u8(odp_actions, OVS_ACTION_ATTR_SET_NW_TOS, flow->nw_tos);
2872 base->nw_tos = flow->nw_tos;
2875 if (base->vlan_tci != flow->vlan_tci) {
2876 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2877 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2879 if (base->vlan_tci != htons(0)) {
2880 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2882 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2883 flow->vlan_tci & ~htons(VLAN_CFI));
2885 base->vlan_tci = flow->vlan_tci;
2888 if (base->tp_src != flow->tp_src) {
2889 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
2890 base->tp_src = flow->tp_src;
2893 if (base->tp_dst != flow->tp_dst) {
2894 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
2895 base->tp_dst = flow->tp_dst;
2898 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
2899 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_SRC,
2900 flow->dl_src, ETH_ADDR_LEN);
2901 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2904 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2905 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_DST,
2906 flow->dl_dst, ETH_ADDR_LEN);
2907 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2910 if (ctx->base_priority != ctx->priority) {
2911 if (ctx->priority) {
2912 nl_msg_put_u32(odp_actions, OVS_ACTION_ATTR_SET_PRIORITY,
2915 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
2917 ctx->base_priority = ctx->priority;
2922 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2924 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2925 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2928 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2929 /* Forwarding disabled on port. */
2934 * We don't have an ofport record for this port, but it doesn't hurt to
2935 * allow forwarding to it anyhow. Maybe such a port will appear later
2936 * and we're pre-populating the flow table.
2940 commit_odp_actions(ctx);
2941 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2942 ctx->nf_output_iface = ofp_port;
2946 xlate_table_action(struct action_xlate_ctx *ctx,
2947 uint16_t in_port, uint8_t table_id)
2949 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2950 struct rule_dpif *rule;
2951 uint16_t old_in_port;
2952 uint8_t old_table_id;
2954 old_table_id = ctx->table_id;
2955 ctx->table_id = table_id;
2957 /* Look up a flow with 'in_port' as the input port. Then restore the
2958 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2959 * have surprising behavior). */
2960 old_in_port = ctx->flow.in_port;
2961 ctx->flow.in_port = in_port;
2962 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow, table_id);
2963 ctx->flow.in_port = old_in_port;
2965 if (ctx->resubmit_hook) {
2966 ctx->resubmit_hook(ctx, rule);
2971 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2975 ctx->table_id = old_table_id;
2977 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2979 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
2980 MAX_RESUBMIT_RECURSION);
2985 xlate_resubmit_table(struct action_xlate_ctx *ctx,
2986 const struct nx_action_resubmit *nar)
2991 in_port = (nar->in_port == htons(OFPP_IN_PORT)
2993 : ntohs(nar->in_port));
2994 table_id = nar->table == 255 ? ctx->table_id : nar->table;
2996 xlate_table_action(ctx, in_port, table_id);
3000 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3002 struct ofport_dpif *ofport;
3004 commit_odp_actions(ctx);
3005 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3006 uint16_t ofp_port = ofport->up.ofp_port;
3007 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
3008 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT,
3013 ctx->nf_output_iface = NF_OUT_FLOOD;
3017 xlate_output_action__(struct action_xlate_ctx *ctx,
3018 uint16_t port, uint16_t max_len)
3020 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3022 ctx->nf_output_iface = NF_OUT_DROP;
3026 add_output_action(ctx, ctx->flow.in_port);
3029 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3035 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3038 flood_packets(ctx, htonl(0));
3040 case OFPP_CONTROLLER:
3041 commit_odp_actions(ctx);
3042 nl_msg_put_u64(ctx->odp_actions, OVS_ACTION_ATTR_USERSPACE, max_len);
3045 add_output_action(ctx, OFPP_LOCAL);
3050 if (port != ctx->flow.in_port) {
3051 add_output_action(ctx, port);
3056 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3057 ctx->nf_output_iface = NF_OUT_FLOOD;
3058 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3059 ctx->nf_output_iface = prev_nf_output_iface;
3060 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3061 ctx->nf_output_iface != NF_OUT_FLOOD) {
3062 ctx->nf_output_iface = NF_OUT_MULTI;
3067 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3068 const struct nx_action_output_reg *naor)
3072 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3074 if (ofp_port <= UINT16_MAX) {
3075 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3080 xlate_output_action(struct action_xlate_ctx *ctx,
3081 const struct ofp_action_output *oao)
3083 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3087 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3088 const struct ofp_action_enqueue *oae)
3090 uint16_t ofp_port, odp_port;
3091 uint32_t ctx_priority, priority;
3094 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3097 /* Fall back to ordinary output action. */
3098 xlate_output_action__(ctx, ntohs(oae->port), 0);
3102 /* Figure out datapath output port. */
3103 ofp_port = ntohs(oae->port);
3104 if (ofp_port == OFPP_IN_PORT) {
3105 ofp_port = ctx->flow.in_port;
3107 odp_port = ofp_port_to_odp_port(ofp_port);
3109 /* Add datapath actions. */
3110 ctx_priority = ctx->priority;
3111 ctx->priority = priority;
3112 add_output_action(ctx, odp_port);
3113 ctx->priority = ctx_priority;
3115 /* Update NetFlow output port. */
3116 if (ctx->nf_output_iface == NF_OUT_DROP) {
3117 ctx->nf_output_iface = odp_port;
3118 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3119 ctx->nf_output_iface = NF_OUT_MULTI;
3124 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3125 const struct nx_action_set_queue *nasq)
3130 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3133 /* Couldn't translate queue to a priority, so ignore. A warning
3134 * has already been logged. */
3138 ctx->priority = priority;
3141 struct xlate_reg_state {
3147 xlate_autopath(struct action_xlate_ctx *ctx,
3148 const struct nx_action_autopath *naa)
3150 uint16_t ofp_port = ntohl(naa->id);
3151 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3153 if (!port || !port->bundle) {
3154 ofp_port = OFPP_NONE;
3155 } else if (port->bundle->bond) {
3156 /* Autopath does not support VLAN hashing. */
3157 struct ofport_dpif *slave = bond_choose_output_slave(
3158 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3160 ofp_port = slave->up.ofp_port;
3163 autopath_execute(naa, &ctx->flow, ofp_port);
3167 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3169 struct ofproto_dpif *ofproto = ofproto_;
3170 struct ofport_dpif *port;
3180 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3183 port = get_ofp_port(ofproto, ofp_port);
3184 return port ? port->may_enable : false;
3189 xlate_learn_action(struct action_xlate_ctx *ctx,
3190 const struct nx_action_learn *learn)
3192 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
3193 struct ofputil_flow_mod fm;
3196 learn_execute(learn, &ctx->flow, &fm);
3198 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
3199 if (error && !VLOG_DROP_WARN(&rl)) {
3200 char *msg = ofputil_error_to_string(error);
3201 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
3209 do_xlate_actions(const union ofp_action *in, size_t n_in,
3210 struct action_xlate_ctx *ctx)
3212 const struct ofport_dpif *port;
3213 const union ofp_action *ia;
3216 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3218 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3219 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3220 ? htonl(OFPPC_NO_RECV_STP)
3221 : htonl(OFPPC_NO_RECV))) {
3222 /* Drop this flow. */
3226 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3227 const struct ofp_action_dl_addr *oada;
3228 const struct nx_action_resubmit *nar;
3229 const struct nx_action_set_tunnel *nast;
3230 const struct nx_action_set_queue *nasq;
3231 const struct nx_action_multipath *nam;
3232 const struct nx_action_autopath *naa;
3233 const struct nx_action_bundle *nab;
3234 const struct nx_action_output_reg *naor;
3235 enum ofputil_action_code code;
3238 code = ofputil_decode_action_unsafe(ia);
3240 case OFPUTIL_OFPAT_OUTPUT:
3241 xlate_output_action(ctx, &ia->output);
3244 case OFPUTIL_OFPAT_SET_VLAN_VID:
3245 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3246 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3249 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3250 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3251 ctx->flow.vlan_tci |= htons(
3252 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3255 case OFPUTIL_OFPAT_STRIP_VLAN:
3256 ctx->flow.vlan_tci = htons(0);
3259 case OFPUTIL_OFPAT_SET_DL_SRC:
3260 oada = ((struct ofp_action_dl_addr *) ia);
3261 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3264 case OFPUTIL_OFPAT_SET_DL_DST:
3265 oada = ((struct ofp_action_dl_addr *) ia);
3266 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3269 case OFPUTIL_OFPAT_SET_NW_SRC:
3270 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3273 case OFPUTIL_OFPAT_SET_NW_DST:
3274 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3277 case OFPUTIL_OFPAT_SET_NW_TOS:
3278 ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK;
3281 case OFPUTIL_OFPAT_SET_TP_SRC:
3282 ctx->flow.tp_src = ia->tp_port.tp_port;
3285 case OFPUTIL_OFPAT_SET_TP_DST:
3286 ctx->flow.tp_dst = ia->tp_port.tp_port;
3289 case OFPUTIL_OFPAT_ENQUEUE:
3290 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3293 case OFPUTIL_NXAST_RESUBMIT:
3294 nar = (const struct nx_action_resubmit *) ia;
3295 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
3298 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3299 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
3302 case OFPUTIL_NXAST_SET_TUNNEL:
3303 nast = (const struct nx_action_set_tunnel *) ia;
3304 tun_id = htonll(ntohl(nast->tun_id));
3305 ctx->flow.tun_id = tun_id;
3308 case OFPUTIL_NXAST_SET_QUEUE:
3309 nasq = (const struct nx_action_set_queue *) ia;
3310 xlate_set_queue_action(ctx, nasq);
3313 case OFPUTIL_NXAST_POP_QUEUE:
3317 case OFPUTIL_NXAST_REG_MOVE:
3318 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3322 case OFPUTIL_NXAST_REG_LOAD:
3323 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3327 case OFPUTIL_NXAST_NOTE:
3328 /* Nothing to do. */
3331 case OFPUTIL_NXAST_SET_TUNNEL64:
3332 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3333 ctx->flow.tun_id = tun_id;
3336 case OFPUTIL_NXAST_MULTIPATH:
3337 nam = (const struct nx_action_multipath *) ia;
3338 multipath_execute(nam, &ctx->flow);
3341 case OFPUTIL_NXAST_AUTOPATH:
3342 naa = (const struct nx_action_autopath *) ia;
3343 xlate_autopath(ctx, naa);
3346 case OFPUTIL_NXAST_BUNDLE:
3347 ctx->ofproto->has_bundle_action = true;
3348 nab = (const struct nx_action_bundle *) ia;
3349 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3354 case OFPUTIL_NXAST_BUNDLE_LOAD:
3355 ctx->ofproto->has_bundle_action = true;
3356 nab = (const struct nx_action_bundle *) ia;
3357 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3361 case OFPUTIL_NXAST_OUTPUT_REG:
3362 naor = (const struct nx_action_output_reg *) ia;
3363 xlate_output_reg_action(ctx, naor);
3366 case OFPUTIL_NXAST_LEARN:
3367 ctx->has_learn = true;
3368 if (ctx->may_learn) {
3369 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
3377 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3378 struct ofproto_dpif *ofproto, const struct flow *flow,
3379 const struct ofpbuf *packet)
3381 ctx->ofproto = ofproto;
3383 ctx->packet = packet;
3384 ctx->may_learn = packet != NULL;
3385 ctx->resubmit_hook = NULL;
3388 static struct ofpbuf *
3389 xlate_actions(struct action_xlate_ctx *ctx,
3390 const union ofp_action *in, size_t n_in)
3392 COVERAGE_INC(ofproto_dpif_xlate);
3394 ctx->odp_actions = ofpbuf_new(512);
3396 ctx->may_set_up_flow = true;
3397 ctx->has_learn = false;
3398 ctx->has_normal = false;
3399 ctx->nf_output_iface = NF_OUT_DROP;
3402 ctx->base_priority = 0;
3403 ctx->base_flow = ctx->flow;
3404 ctx->base_flow.tun_id = 0;
3407 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3408 ctx->may_set_up_flow = false;
3410 do_xlate_actions(in, n_in, ctx);
3413 /* Check with in-band control to see if we're allowed to set up this
3415 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3416 ctx->odp_actions->data,
3417 ctx->odp_actions->size)) {
3418 ctx->may_set_up_flow = false;
3421 return ctx->odp_actions;
3424 /* OFPP_NORMAL implementation. */
3427 struct ofport_dpif *port;
3432 struct dst builtin[32];
3434 size_t n, allocated;
3437 static void dst_set_init(struct dst_set *);
3438 static void dst_set_add(struct dst_set *, const struct dst *);
3439 static void dst_set_free(struct dst_set *);
3441 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3444 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3445 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3447 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3448 : in_bundle->vlan >= 0 ? in_bundle->vlan
3449 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3450 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3452 dst->port = (!out_bundle->bond
3453 ? ofbundle_get_a_port(out_bundle)
3454 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3455 dst->vlan, &ctx->tags));
3457 return dst->port != NULL;
3461 mirror_mask_ffs(mirror_mask_t mask)
3463 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3468 dst_set_init(struct dst_set *set)
3470 set->dsts = set->builtin;
3472 set->allocated = ARRAY_SIZE(set->builtin);
3476 dst_set_add(struct dst_set *set, const struct dst *dst)
3478 if (set->n >= set->allocated) {
3479 size_t new_allocated;
3480 struct dst *new_dsts;
3482 new_allocated = set->allocated * 2;
3483 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3484 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3488 set->dsts = new_dsts;
3489 set->allocated = new_allocated;
3491 set->dsts[set->n++] = *dst;
3495 dst_set_free(struct dst_set *set)
3497 if (set->dsts != set->builtin) {
3503 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3506 for (i = 0; i < set->n; i++) {
3507 if (set->dsts[i].vlan == test->vlan
3508 && set->dsts[i].port == test->port) {
3516 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3518 return (bundle->vlan < 0
3519 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
3523 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3525 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3528 /* Returns an arbitrary interface within 'bundle'. */
3529 static struct ofport_dpif *
3530 ofbundle_get_a_port(const struct ofbundle *bundle)
3532 return CONTAINER_OF(list_front(&bundle->ports),
3533 struct ofport_dpif, bundle_node);
3537 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3538 const struct ofbundle *in_bundle,
3539 const struct ofbundle *out_bundle, struct dst_set *set)
3543 if (out_bundle == OFBUNDLE_FLOOD) {
3544 struct ofbundle *bundle;
3546 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3547 if (bundle != in_bundle
3548 && ofbundle_includes_vlan(bundle, vlan)
3549 && bundle->floodable
3550 && !bundle->mirror_out
3551 && set_dst(ctx, &dst, in_bundle, bundle)) {
3552 dst_set_add(set, &dst);
3555 ctx->nf_output_iface = NF_OUT_FLOOD;
3556 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3557 dst_set_add(set, &dst);
3558 ctx->nf_output_iface = dst.port->odp_port;
3563 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3565 return !m->vlans || bitmap_is_set(m->vlans, vlan);
3568 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3569 * to a VLAN. In general most packets may be mirrored but we want to drop
3570 * protocols that may confuse switches. */
3572 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
3574 /* If you change this function's behavior, please update corresponding
3575 * documentation in vswitch.xml at the same time. */
3576 if (dst[0] != 0x01) {
3577 /* All the currently banned MACs happen to start with 01 currently, so
3578 * this is a quick way to eliminate most of the good ones. */
3580 if (eth_addr_is_reserved(dst)) {
3581 /* Drop STP, IEEE pause frames, and other reserved protocols
3582 * (01-80-c2-00-00-0x). */
3586 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
3588 if ((dst[3] & 0xfe) == 0xcc &&
3589 (dst[4] & 0xfe) == 0xcc &&
3590 (dst[5] & 0xfe) == 0xcc) {
3591 /* Drop the following protocols plus others following the same
3594 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3595 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3596 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3600 if (!(dst[3] | dst[4] | dst[5])) {
3601 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3610 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3611 uint16_t vlan, const struct ofbundle *in_bundle,
3612 struct dst_set *set)
3614 struct ofproto_dpif *ofproto = ctx->ofproto;
3615 mirror_mask_t mirrors;
3619 mirrors = in_bundle->src_mirrors;
3620 for (i = 0; i < set->n; i++) {
3621 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3628 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3629 if (flow_vlan == 0) {
3630 flow_vlan = OFP_VLAN_NONE;
3634 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3635 if (vlan_is_mirrored(m, vlan)) {
3639 if (set_dst(ctx, &dst, in_bundle, m->out)
3640 && !dst_is_duplicate(set, &dst)) {
3641 dst_set_add(set, &dst);
3643 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
3644 struct ofbundle *bundle;
3646 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3647 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3648 && set_dst(ctx, &dst, in_bundle, bundle))
3650 if (bundle->vlan < 0) {
3651 dst.vlan = m->out_vlan;
3653 if (dst_is_duplicate(set, &dst)) {
3657 /* Use the vlan tag on the original flow instead of
3658 * the one passed in the vlan parameter. This ensures
3659 * that we compare the vlan from before any implicit
3660 * tagging tags place. This is necessary because
3661 * dst->vlan is the final vlan, after removing implicit
3663 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3664 /* Don't send out input port on same VLAN. */
3667 dst_set_add(set, &dst);
3672 mirrors &= mirrors - 1;
3677 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3678 const struct ofbundle *in_bundle,
3679 const struct ofbundle *out_bundle)
3681 uint16_t initial_vlan, cur_vlan;
3682 const struct dst *dst;
3686 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3687 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3689 /* Output all the packets we can without having to change the VLAN. */
3690 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3691 if (initial_vlan == 0) {
3692 initial_vlan = OFP_VLAN_NONE;
3694 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3695 if (dst->vlan != initial_vlan) {
3698 nl_msg_put_u32(ctx->odp_actions,
3699 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3702 /* Then output the rest. */
3703 cur_vlan = initial_vlan;
3704 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3705 if (dst->vlan == initial_vlan) {
3708 if (dst->vlan != cur_vlan) {
3709 if (dst->vlan == OFP_VLAN_NONE) {
3710 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3714 if (cur_vlan != OFP_VLAN_NONE) {
3715 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3717 tci = htons(dst->vlan & VLAN_VID_MASK);
3718 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3719 nl_msg_put_be16(ctx->odp_actions,
3720 OVS_ACTION_ATTR_PUSH_VLAN, tci);
3722 cur_vlan = dst->vlan;
3724 nl_msg_put_u32(ctx->odp_actions,
3725 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3731 /* Returns the effective vlan of a packet, taking into account both the
3732 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3733 * the packet is untagged and -1 indicates it has an invalid header and
3734 * should be dropped. */
3736 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3737 struct ofbundle *in_bundle, bool have_packet)
3739 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3740 if (in_bundle->vlan >= 0) {
3743 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3744 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3745 "packet received on port %s configured with "
3746 "implicit VLAN %"PRIu16,
3747 ofproto->up.name, vlan,
3748 in_bundle->name, in_bundle->vlan);
3752 vlan = in_bundle->vlan;
3754 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3756 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3757 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3758 "packet received on port %s not configured for "
3760 ofproto->up.name, vlan, in_bundle->name, vlan);
3769 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3770 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3771 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3773 is_gratuitous_arp(const struct flow *flow)
3775 return (flow->dl_type == htons(ETH_TYPE_ARP)
3776 && eth_addr_is_broadcast(flow->dl_dst)
3777 && (flow->nw_proto == ARP_OP_REPLY
3778 || (flow->nw_proto == ARP_OP_REQUEST
3779 && flow->nw_src == flow->nw_dst)));
3783 update_learning_table(struct ofproto_dpif *ofproto,
3784 const struct flow *flow, int vlan,
3785 struct ofbundle *in_bundle)
3787 struct mac_entry *mac;
3789 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3793 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3794 if (is_gratuitous_arp(flow)) {
3795 /* We don't want to learn from gratuitous ARP packets that are
3796 * reflected back over bond slaves so we lock the learning table. */
3797 if (!in_bundle->bond) {
3798 mac_entry_set_grat_arp_lock(mac);
3799 } else if (mac_entry_is_grat_arp_locked(mac)) {
3804 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3805 /* The log messages here could actually be useful in debugging,
3806 * so keep the rate limit relatively high. */
3807 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3808 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3809 "on port %s in VLAN %d",
3810 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3811 in_bundle->name, vlan);
3813 mac->port.p = in_bundle;
3814 tag_set_add(&ofproto->revalidate_set,
3815 mac_learning_changed(ofproto->ml, mac));
3819 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3820 * dropped. Returns true if they may be forwarded, false if they should be
3823 * If 'have_packet' is true, it indicates that the caller is processing a
3824 * received packet. If 'have_packet' is false, then the caller is just
3825 * revalidating an existing flow because configuration has changed. Either
3826 * way, 'have_packet' only affects logging (there is no point in logging errors
3827 * during revalidation).
3829 * Sets '*in_portp' to the input port. This will be a null pointer if
3830 * flow->in_port does not designate a known input port (in which case
3831 * is_admissible() returns false).
3833 * When returning true, sets '*vlanp' to the effective VLAN of the input
3834 * packet, as returned by flow_get_vlan().
3836 * May also add tags to '*tags', although the current implementation only does
3837 * so in one special case.
3840 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3842 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3844 struct ofport_dpif *in_port;
3845 struct ofbundle *in_bundle;
3848 /* Find the port and bundle for the received packet. */
3849 in_port = get_ofp_port(ofproto, flow->in_port);
3850 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3851 if (!in_port || !in_bundle) {
3852 /* No interface? Something fishy... */
3854 /* Odd. A few possible reasons here:
3856 * - We deleted a port but there are still a few packets queued up
3859 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3860 * we don't know about.
3862 * - Packet arrived on the local port but the local port is not
3865 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3867 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3869 ofproto->up.name, flow->in_port);
3874 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3879 /* Drop frames for reserved multicast addresses
3880 * only if forward_bpdu option is absent. */
3881 if (eth_addr_is_reserved(flow->dl_dst) &&
3882 !ofproto->up.forward_bpdu) {
3886 /* Drop frames on bundles reserved for mirroring. */
3887 if (in_bundle->mirror_out) {
3889 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3890 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3891 "%s, which is reserved exclusively for mirroring",
3892 ofproto->up.name, in_bundle->name);
3897 if (in_bundle->bond) {
3898 struct mac_entry *mac;
3900 switch (bond_check_admissibility(in_bundle->bond, in_port,
3901 flow->dl_dst, tags)) {
3908 case BV_DROP_IF_MOVED:
3909 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3910 if (mac && mac->port.p != in_bundle &&
3911 (!is_gratuitous_arp(flow)
3912 || mac_entry_is_grat_arp_locked(mac))) {
3923 xlate_normal(struct action_xlate_ctx *ctx)
3925 struct ofbundle *in_bundle;
3926 struct ofbundle *out_bundle;
3927 struct mac_entry *mac;
3930 ctx->has_normal = true;
3932 /* Check whether we should drop packets in this flow. */
3933 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3934 &ctx->tags, &vlan, &in_bundle)) {
3939 /* Learn source MAC. */
3940 if (ctx->may_learn) {
3941 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3944 /* Determine output bundle. */
3945 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3948 out_bundle = mac->port.p;
3949 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3950 /* If we are revalidating but don't have a learning entry then eject
3951 * the flow. Installing a flow that floods packets opens up a window
3952 * of time where we could learn from a packet reflected on a bond and
3953 * blackhole packets before the learning table is updated to reflect
3954 * the correct port. */
3955 ctx->may_set_up_flow = false;
3958 out_bundle = OFBUNDLE_FLOOD;
3961 /* Don't send packets out their input bundles. */
3962 if (in_bundle == out_bundle) {
3968 compose_actions(ctx, vlan, in_bundle, out_bundle);
3973 get_drop_frags(struct ofproto *ofproto_)
3975 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3978 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3983 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3985 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3987 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3991 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3992 const struct flow *flow,
3993 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3995 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3998 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3999 ofproto->max_ports);
4001 struct odputil_keybuf keybuf;
4002 struct action_xlate_ctx ctx;
4003 struct ofpbuf *odp_actions;
4006 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4007 odp_flow_key_from_flow(&key, flow);
4009 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
4010 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
4011 dpif_execute(ofproto->dpif, key.data, key.size,
4012 odp_actions->data, odp_actions->size, packet);
4013 ofpbuf_delete(odp_actions);
4019 get_netflow_ids(const struct ofproto *ofproto_,
4020 uint8_t *engine_type, uint8_t *engine_id)
4022 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4024 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
4027 static struct ofproto_dpif *
4028 ofproto_dpif_lookup(const char *name)
4030 struct ofproto *ofproto = ofproto_lookup(name);
4031 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
4032 ? ofproto_dpif_cast(ofproto)
4037 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
4038 const char *args, void *aux OVS_UNUSED)
4040 struct ds ds = DS_EMPTY_INITIALIZER;
4041 const struct ofproto_dpif *ofproto;
4042 const struct mac_entry *e;
4044 ofproto = ofproto_dpif_lookup(args);
4046 unixctl_command_reply(conn, 501, "no such bridge");
4050 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4051 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4052 struct ofbundle *bundle = e->port.p;
4053 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
4054 ofbundle_get_a_port(bundle)->odp_port,
4055 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
4057 unixctl_command_reply(conn, 200, ds_cstr(&ds));
4061 struct ofproto_trace {
4062 struct action_xlate_ctx ctx;
4068 trace_format_rule(struct ds *result, uint8_t table_id, int level,
4069 const struct rule_dpif *rule)
4071 ds_put_char_multiple(result, '\t', level);
4073 ds_put_cstr(result, "No match\n");
4077 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4078 table_id, ntohll(rule->up.flow_cookie));
4079 cls_rule_format(&rule->up.cr, result);
4080 ds_put_char(result, '\n');
4082 ds_put_char_multiple(result, '\t', level);
4083 ds_put_cstr(result, "OpenFlow ");
4084 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
4085 ds_put_char(result, '\n');
4089 trace_format_flow(struct ds *result, int level, const char *title,
4090 struct ofproto_trace *trace)
4092 ds_put_char_multiple(result, '\t', level);
4093 ds_put_format(result, "%s: ", title);
4094 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4095 ds_put_cstr(result, "unchanged");
4097 flow_format(result, &trace->ctx.flow);
4098 trace->flow = trace->ctx.flow;
4100 ds_put_char(result, '\n');
4104 trace_format_regs(struct ds *result, int level, const char *title,
4105 struct ofproto_trace *trace)
4109 ds_put_char_multiple(result, '\t', level);
4110 ds_put_format(result, "%s:", title);
4111 for (i = 0; i < FLOW_N_REGS; i++) {
4112 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
4114 ds_put_char(result, '\n');
4118 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
4120 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4121 struct ds *result = trace->result;
4123 ds_put_char(result, '\n');
4124 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4125 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
4126 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
4130 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4131 void *aux OVS_UNUSED)
4133 char *dpname, *arg1, *arg2, *arg3;
4134 char *args = xstrdup(args_);
4135 char *save_ptr = NULL;
4136 struct ofproto_dpif *ofproto;
4137 struct ofpbuf odp_key;
4138 struct ofpbuf *packet;
4139 struct rule_dpif *rule;
4145 ofpbuf_init(&odp_key, 0);
4148 dpname = strtok_r(args, " ", &save_ptr);
4149 arg1 = strtok_r(NULL, " ", &save_ptr);
4150 arg2 = strtok_r(NULL, " ", &save_ptr);
4151 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4152 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
4153 /* ofproto/trace dpname flow [-generate] */
4156 /* Convert string to datapath key. */
4157 ofpbuf_init(&odp_key, 0);
4158 error = odp_flow_key_from_string(arg1, &odp_key);
4160 unixctl_command_reply(conn, 501, "Bad flow syntax");
4164 /* Convert odp_key to flow. */
4165 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
4167 unixctl_command_reply(conn, 501, "Invalid flow");
4171 /* Generate a packet, if requested. */
4173 packet = ofpbuf_new(0);
4174 flow_compose(packet, &flow);
4176 } else if (dpname && arg1 && arg2 && arg3) {
4177 /* ofproto/trace dpname tun_id in_port packet */
4181 tun_id = htonll(strtoull(arg1, NULL, 0));
4182 in_port = ofp_port_to_odp_port(atoi(arg2));
4184 packet = ofpbuf_new(strlen(args) / 2);
4185 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
4186 arg3 += strspn(arg3, " ");
4187 if (*arg3 != '\0') {
4188 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4191 if (packet->size < ETH_HEADER_LEN) {
4192 unixctl_command_reply(conn, 501,
4193 "Packet data too short for Ethernet");
4197 ds_put_cstr(&result, "Packet: ");
4198 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
4199 ds_put_cstr(&result, s);
4202 flow_extract(packet, tun_id, in_port, &flow);
4204 unixctl_command_reply(conn, 501, "Bad command syntax");
4208 ofproto = ofproto_dpif_lookup(dpname);
4210 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4215 ds_put_cstr(&result, "Flow: ");
4216 flow_format(&result, &flow);
4217 ds_put_char(&result, '\n');
4219 rule = rule_dpif_lookup(ofproto, &flow, 0);
4220 trace_format_rule(&result, 0, 0, rule);
4222 struct ofproto_trace trace;
4223 struct ofpbuf *odp_actions;
4225 trace.result = &result;
4227 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
4228 trace.ctx.resubmit_hook = trace_resubmit;
4229 odp_actions = xlate_actions(&trace.ctx,
4230 rule->up.actions, rule->up.n_actions);
4232 ds_put_char(&result, '\n');
4233 trace_format_flow(&result, 0, "Final flow", &trace);
4234 ds_put_cstr(&result, "Datapath actions: ");
4235 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4236 ofpbuf_delete(odp_actions);
4238 if (!trace.ctx.may_set_up_flow) {
4240 ds_put_cstr(&result, "\nThis flow is not cachable.");
4242 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
4243 "for complete actions, please supply a packet.");
4248 unixctl_command_reply(conn, 200, ds_cstr(&result));
4251 ds_destroy(&result);
4252 ofpbuf_delete(packet);
4253 ofpbuf_uninit(&odp_key);
4258 ofproto_dpif_clog(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_unclog(struct unixctl_conn *conn OVS_UNUSED,
4267 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4270 unixctl_command_reply(conn, 200, NULL);
4274 ofproto_dpif_unixctl_init(void)
4276 static bool registered;
4282 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4283 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
4285 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
4286 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
4289 const struct ofproto_class ofproto_dpif_class = {
4316 port_is_lacp_current,
4317 NULL, /* rule_choose_table */
4324 rule_modify_actions,
4333 get_cfm_remote_mpids,
4338 is_mirror_output_bundle,
4339 forward_bpdu_changed,