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 tag_type tag; /* Caches rule_calculate_tag() result. */
98 struct list facets; /* List of "struct facet"s. */
101 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
103 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
106 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
107 const struct flow *, uint8_t table);
109 #define MAX_MIRRORS 32
110 typedef uint32_t mirror_mask_t;
111 #define MIRROR_MASK_C(X) UINT32_C(X)
112 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
114 struct ofproto_dpif *ofproto; /* Owning ofproto. */
115 size_t idx; /* In ofproto's "mirrors" array. */
116 void *aux; /* Key supplied by ofproto's client. */
117 char *name; /* Identifier for log messages. */
119 /* Selection criteria. */
120 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
121 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
122 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
124 /* Output (mutually exclusive). */
125 struct ofbundle *out; /* Output port or NULL. */
126 int out_vlan; /* Output VLAN or -1. */
129 static void mirror_destroy(struct ofmirror *);
131 /* A group of one or more OpenFlow ports. */
132 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
134 struct ofproto_dpif *ofproto; /* Owning ofproto. */
135 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
136 void *aux; /* Key supplied by ofproto's client. */
137 char *name; /* Identifier for log messages. */
140 struct list ports; /* Contains "struct ofport"s. */
141 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
142 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
143 * NULL if all VLANs are trunked. */
144 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
145 struct bond *bond; /* Nonnull iff more than one port. */
148 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
150 /* Port mirroring info. */
151 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
152 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
153 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
156 static void bundle_remove(struct ofport *);
157 static void bundle_destroy(struct ofbundle *);
158 static void bundle_del_port(struct ofport_dpif *);
159 static void bundle_run(struct ofbundle *);
160 static void bundle_wait(struct ofbundle *);
162 struct action_xlate_ctx {
163 /* action_xlate_ctx_init() initializes these members. */
166 struct ofproto_dpif *ofproto;
168 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
169 * this flow when actions change header fields. */
172 /* The packet corresponding to 'flow', or a null pointer if we are
173 * revalidating without a packet to refer to. */
174 const struct ofpbuf *packet;
176 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
177 * want to execute them if we are actually processing a packet, or if we
178 * are accounting for packets that the datapath has processed, but not if
179 * we are just revalidating. */
182 /* If nonnull, called just before executing a resubmit action.
184 * This is normally null so the client has to set it manually after
185 * calling action_xlate_ctx_init(). */
186 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
188 /* xlate_actions() initializes and uses these members. The client might want
189 * to look at them after it returns. */
191 struct ofpbuf *odp_actions; /* Datapath actions. */
192 tag_type tags; /* Tags associated with actions. */
193 bool may_set_up_flow; /* True ordinarily; false if the actions must
194 * be reassessed for every packet. */
195 bool has_learn; /* Actions include NXAST_LEARN? */
196 bool has_normal; /* Actions output to OFPP_NORMAL? */
197 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
199 /* xlate_actions() initializes and uses these members, but the client has no
200 * reason to look at them. */
202 int recurse; /* Recursion level, via xlate_table_action. */
203 uint32_t priority; /* Current flow priority. 0 if none. */
204 struct flow base_flow; /* Flow at the last commit. */
205 uint32_t base_priority; /* Priority at the last commit. */
206 uint8_t table_id; /* OpenFlow table ID where flow was found. */
209 static void action_xlate_ctx_init(struct action_xlate_ctx *,
210 struct ofproto_dpif *, const struct flow *,
211 const struct ofpbuf *);
212 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
213 const union ofp_action *in, size_t n_in);
215 /* An exact-match instantiation of an OpenFlow flow. */
217 long long int used; /* Time last used; time created if not used. */
221 * - Do include packets and bytes sent "by hand", e.g. with
224 * - Do include packets and bytes that were obtained from the datapath
225 * when its statistics were reset (e.g. dpif_flow_put() with
226 * DPIF_FP_ZERO_STATS).
228 uint64_t packet_count; /* Number of packets received. */
229 uint64_t byte_count; /* Number of bytes received. */
231 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
232 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
234 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
235 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
236 long long int rs_used; /* Used time pushed to resubmit children. */
238 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
240 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
241 struct list list_node; /* In owning rule's 'facets' list. */
242 struct rule_dpif *rule; /* Owning rule. */
243 struct flow flow; /* Exact-match flow. */
244 bool installed; /* Installed in datapath? */
245 bool may_install; /* True ordinarily; false if actions must
246 * be reassessed for every packet. */
247 bool has_learn; /* Actions include NXAST_LEARN? */
248 bool has_normal; /* Actions output to OFPP_NORMAL? */
249 size_t actions_len; /* Number of bytes in actions[]. */
250 struct nlattr *actions; /* Datapath actions. */
251 tag_type tags; /* Tags. */
252 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
255 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
256 const struct ofpbuf *packet);
257 static void facet_remove(struct ofproto_dpif *, struct facet *);
258 static void facet_free(struct facet *);
260 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
261 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
262 const struct flow *);
263 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
265 static void facet_execute(struct ofproto_dpif *, struct facet *,
266 struct ofpbuf *packet);
268 static int facet_put__(struct ofproto_dpif *, struct facet *,
269 const struct nlattr *actions, size_t actions_len,
270 struct dpif_flow_stats *);
271 static void facet_install(struct ofproto_dpif *, struct facet *,
273 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
274 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
276 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
277 const struct ofpbuf *packet);
278 static void facet_update_time(struct ofproto_dpif *, struct facet *,
280 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
281 const struct dpif_flow_stats *);
282 static void facet_reset_counters(struct facet *);
283 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
284 static void facet_push_stats(struct facet *);
285 static void facet_account(struct ofproto_dpif *, struct facet *);
287 static bool facet_is_controller_flow(struct facet *);
289 static void flow_push_stats(const struct rule_dpif *,
290 struct flow *, uint64_t packets, uint64_t bytes,
293 static uint32_t rule_calculate_tag(const struct flow *,
294 const struct flow_wildcards *,
296 static void rule_invalidate(const struct rule_dpif *);
302 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
303 struct list bundle_node; /* In struct ofbundle's "ports" list. */
304 struct cfm *cfm; /* Connectivity Fault Management, if any. */
305 tag_type tag; /* Tag associated with this port. */
306 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
307 bool may_enable; /* May be enabled in bonds. */
310 static struct ofport_dpif *
311 ofport_dpif_cast(const struct ofport *ofport)
313 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
314 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
317 static void port_run(struct ofport_dpif *);
318 static void port_wait(struct ofport_dpif *);
319 static int set_cfm(struct ofport *, const struct cfm_settings *);
321 struct dpif_completion {
322 struct list list_node;
323 struct ofoperation *op;
326 /* Extra information about a classifier table.
327 * Currently used just for optimized flow revalidation. */
329 /* If either of these is nonnull, then this table has a form that allows
330 * flows to be tagged to avoid revalidating most flows for the most common
331 * kinds of flow table changes. */
332 struct cls_table *catchall_table; /* Table that wildcards all fields. */
333 struct cls_table *other_table; /* Table with any other wildcard set. */
334 uint32_t basis; /* Keeps each table's tags separate. */
337 struct ofproto_dpif {
346 struct netflow *netflow;
347 struct dpif_sflow *sflow;
348 struct hmap bundles; /* Contains "struct ofbundle"s. */
349 struct mac_learning *ml;
350 struct ofmirror *mirrors[MAX_MIRRORS];
351 bool has_bonded_bundles;
354 struct timer next_expiration;
360 struct table_dpif tables[N_TABLES];
361 bool need_revalidate;
362 struct tag_set revalidate_set;
364 /* Support for debugging async flow mods. */
365 struct list completions;
367 bool has_bundle_action; /* True when the first bundle action appears. */
370 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
371 * for debugging the asynchronous flow_mod implementation.) */
374 static void ofproto_dpif_unixctl_init(void);
376 static struct ofproto_dpif *
377 ofproto_dpif_cast(const struct ofproto *ofproto)
379 assert(ofproto->ofproto_class == &ofproto_dpif_class);
380 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
383 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
385 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
388 /* Packet processing. */
389 static void update_learning_table(struct ofproto_dpif *,
390 const struct flow *, int vlan,
392 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
393 bool have_packet, tag_type *, int *vlanp,
394 struct ofbundle **in_bundlep);
395 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
397 /* Flow expiration. */
398 static int expire(struct ofproto_dpif *);
401 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
402 const struct ofpbuf *packet);
404 /* Global variables. */
405 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
407 /* Factory functions. */
410 enumerate_types(struct sset *types)
412 dp_enumerate_types(types);
416 enumerate_names(const char *type, struct sset *names)
418 return dp_enumerate_names(type, names);
422 del(const char *type, const char *name)
427 error = dpif_open(name, type, &dpif);
429 error = dpif_delete(dpif);
435 /* Basic life-cycle. */
437 static struct ofproto *
440 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
445 dealloc(struct ofproto *ofproto_)
447 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
452 construct(struct ofproto *ofproto_, int *n_tablesp)
454 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
455 const char *name = ofproto->up.name;
459 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
461 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
465 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
466 ofproto->n_matches = 0;
468 error = dpif_recv_set_mask(ofproto->dpif,
469 ((1u << DPIF_UC_MISS) |
470 (1u << DPIF_UC_ACTION) |
471 (1u << DPIF_UC_SAMPLE)));
473 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
474 dpif_close(ofproto->dpif);
477 dpif_flow_flush(ofproto->dpif);
478 dpif_recv_purge(ofproto->dpif);
480 ofproto->netflow = NULL;
481 ofproto->sflow = NULL;
482 hmap_init(&ofproto->bundles);
483 ofproto->ml = mac_learning_create();
484 for (i = 0; i < MAX_MIRRORS; i++) {
485 ofproto->mirrors[i] = NULL;
487 ofproto->has_bonded_bundles = false;
489 timer_set_duration(&ofproto->next_expiration, 1000);
491 hmap_init(&ofproto->facets);
493 for (i = 0; i < N_TABLES; i++) {
494 struct table_dpif *table = &ofproto->tables[i];
496 table->catchall_table = NULL;
497 table->other_table = NULL;
498 table->basis = random_uint32();
500 ofproto->need_revalidate = false;
501 tag_set_init(&ofproto->revalidate_set);
503 list_init(&ofproto->completions);
505 ofproto_dpif_unixctl_init();
507 ofproto->has_bundle_action = false;
509 *n_tablesp = N_TABLES;
514 complete_operations(struct ofproto_dpif *ofproto)
516 struct dpif_completion *c, *next;
518 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
519 ofoperation_complete(c->op, 0);
520 list_remove(&c->list_node);
526 destruct(struct ofproto *ofproto_)
528 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
529 struct rule_dpif *rule, *next_rule;
530 struct classifier *table;
533 complete_operations(ofproto);
535 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
536 struct cls_cursor cursor;
538 cls_cursor_init(&cursor, table, NULL);
539 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
540 ofproto_rule_destroy(&rule->up);
544 for (i = 0; i < MAX_MIRRORS; i++) {
545 mirror_destroy(ofproto->mirrors[i]);
548 netflow_destroy(ofproto->netflow);
549 dpif_sflow_destroy(ofproto->sflow);
550 hmap_destroy(&ofproto->bundles);
551 mac_learning_destroy(ofproto->ml);
553 hmap_destroy(&ofproto->facets);
555 dpif_close(ofproto->dpif);
559 run(struct ofproto *ofproto_)
561 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
562 struct ofport_dpif *ofport;
563 struct ofbundle *bundle;
567 complete_operations(ofproto);
569 dpif_run(ofproto->dpif);
571 for (i = 0; i < 50; i++) {
572 struct dpif_upcall packet;
575 error = dpif_recv(ofproto->dpif, &packet);
577 if (error == ENODEV) {
578 /* Datapath destroyed. */
584 handle_upcall(ofproto, &packet);
587 if (timer_expired(&ofproto->next_expiration)) {
588 int delay = expire(ofproto);
589 timer_set_duration(&ofproto->next_expiration, delay);
592 if (ofproto->netflow) {
593 netflow_run(ofproto->netflow);
595 if (ofproto->sflow) {
596 dpif_sflow_run(ofproto->sflow);
599 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
602 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
606 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
608 /* Now revalidate if there's anything to do. */
609 if (ofproto->need_revalidate
610 || !tag_set_is_empty(&ofproto->revalidate_set)) {
611 struct tag_set revalidate_set = ofproto->revalidate_set;
612 bool revalidate_all = ofproto->need_revalidate;
613 struct facet *facet, *next;
615 /* Clear the revalidation flags. */
616 tag_set_init(&ofproto->revalidate_set);
617 ofproto->need_revalidate = false;
619 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
621 || tag_set_intersects(&revalidate_set, facet->tags)) {
622 facet_revalidate(ofproto, facet);
631 wait(struct ofproto *ofproto_)
633 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
634 struct ofport_dpif *ofport;
635 struct ofbundle *bundle;
637 if (!clogged && !list_is_empty(&ofproto->completions)) {
638 poll_immediate_wake();
641 dpif_wait(ofproto->dpif);
642 dpif_recv_wait(ofproto->dpif);
643 if (ofproto->sflow) {
644 dpif_sflow_wait(ofproto->sflow);
646 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
647 poll_immediate_wake();
649 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
652 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
655 mac_learning_wait(ofproto->ml);
656 if (ofproto->need_revalidate) {
657 /* Shouldn't happen, but if it does just go around again. */
658 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
659 poll_immediate_wake();
661 timer_wait(&ofproto->next_expiration);
666 flush(struct ofproto *ofproto_)
668 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
669 struct facet *facet, *next_facet;
671 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
672 /* Mark the facet as not installed so that facet_remove() doesn't
673 * bother trying to uninstall it. There is no point in uninstalling it
674 * individually since we are about to blow away all the facets with
675 * dpif_flow_flush(). */
676 facet->installed = false;
677 facet->dp_packet_count = 0;
678 facet->dp_byte_count = 0;
679 facet_remove(ofproto, facet);
681 dpif_flow_flush(ofproto->dpif);
685 get_features(struct ofproto *ofproto_ OVS_UNUSED,
686 bool *arp_match_ip, uint32_t *actions)
688 *arp_match_ip = true;
689 *actions = ((1u << OFPAT_OUTPUT) |
690 (1u << OFPAT_SET_VLAN_VID) |
691 (1u << OFPAT_SET_VLAN_PCP) |
692 (1u << OFPAT_STRIP_VLAN) |
693 (1u << OFPAT_SET_DL_SRC) |
694 (1u << OFPAT_SET_DL_DST) |
695 (1u << OFPAT_SET_NW_SRC) |
696 (1u << OFPAT_SET_NW_DST) |
697 (1u << OFPAT_SET_NW_TOS) |
698 (1u << OFPAT_SET_TP_SRC) |
699 (1u << OFPAT_SET_TP_DST) |
700 (1u << OFPAT_ENQUEUE));
704 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
706 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
707 struct ovs_dp_stats s;
709 strcpy(ots->name, "classifier");
711 dpif_get_dp_stats(ofproto->dpif, &s);
712 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
713 put_32aligned_be64(&ots->matched_count,
714 htonll(s.n_hit + ofproto->n_matches));
718 set_netflow(struct ofproto *ofproto_,
719 const struct netflow_options *netflow_options)
721 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
723 if (netflow_options) {
724 if (!ofproto->netflow) {
725 ofproto->netflow = netflow_create();
727 return netflow_set_options(ofproto->netflow, netflow_options);
729 netflow_destroy(ofproto->netflow);
730 ofproto->netflow = NULL;
735 static struct ofport *
738 struct ofport_dpif *port = xmalloc(sizeof *port);
743 port_dealloc(struct ofport *port_)
745 struct ofport_dpif *port = ofport_dpif_cast(port_);
750 port_construct(struct ofport *port_)
752 struct ofport_dpif *port = ofport_dpif_cast(port_);
753 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
755 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
758 port->tag = tag_create_random();
759 port->may_enable = true;
761 if (ofproto->sflow) {
762 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
763 netdev_get_name(port->up.netdev));
770 port_destruct(struct ofport *port_)
772 struct ofport_dpif *port = ofport_dpif_cast(port_);
773 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
775 bundle_remove(port_);
776 set_cfm(port_, NULL);
777 if (ofproto->sflow) {
778 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
783 port_modified(struct ofport *port_)
785 struct ofport_dpif *port = ofport_dpif_cast(port_);
787 if (port->bundle && port->bundle->bond) {
788 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
793 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
795 struct ofport_dpif *port = ofport_dpif_cast(port_);
796 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
797 ovs_be32 changed = old_config ^ port->up.opp.config;
799 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
800 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
801 ofproto->need_revalidate = true;
806 set_sflow(struct ofproto *ofproto_,
807 const struct ofproto_sflow_options *sflow_options)
809 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
810 struct dpif_sflow *ds = ofproto->sflow;
813 struct ofport_dpif *ofport;
815 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
816 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
817 dpif_sflow_add_port(ds, ofport->odp_port,
818 netdev_get_name(ofport->up.netdev));
821 dpif_sflow_set_options(ds, sflow_options);
823 dpif_sflow_destroy(ds);
824 ofproto->sflow = NULL;
830 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
832 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
839 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
842 if (cfm_configure(ofport->cfm, s)) {
848 cfm_destroy(ofport->cfm);
854 get_cfm_fault(const struct ofport *ofport_)
856 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
858 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
862 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
865 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
868 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
877 /* Expires all MAC learning entries associated with 'port' and forces ofproto
878 * to revalidate every flow. */
880 bundle_flush_macs(struct ofbundle *bundle)
882 struct ofproto_dpif *ofproto = bundle->ofproto;
883 struct mac_learning *ml = ofproto->ml;
884 struct mac_entry *mac, *next_mac;
886 ofproto->need_revalidate = true;
887 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
888 if (mac->port.p == bundle) {
889 mac_learning_expire(ml, mac);
894 static struct ofbundle *
895 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
897 struct ofbundle *bundle;
899 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
901 if (bundle->aux == aux) {
908 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
909 * ones that are found to 'bundles'. */
911 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
912 void **auxes, size_t n_auxes,
913 struct hmapx *bundles)
918 for (i = 0; i < n_auxes; i++) {
919 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
921 hmapx_add(bundles, bundle);
927 bundle_del_port(struct ofport_dpif *port)
929 struct ofbundle *bundle = port->bundle;
931 bundle->ofproto->need_revalidate = true;
933 list_remove(&port->bundle_node);
937 lacp_slave_unregister(bundle->lacp, port);
940 bond_slave_unregister(bundle->bond, port);
943 bundle->floodable = true;
944 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
945 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
946 bundle->floodable = false;
952 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
953 struct lacp_slave_settings *lacp,
954 uint32_t bond_stable_id)
956 struct ofport_dpif *port;
958 port = get_ofp_port(bundle->ofproto, ofp_port);
963 if (port->bundle != bundle) {
964 bundle->ofproto->need_revalidate = true;
966 bundle_del_port(port);
969 port->bundle = bundle;
970 list_push_back(&bundle->ports, &port->bundle_node);
971 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
972 bundle->floodable = false;
976 lacp_slave_register(bundle->lacp, port, lacp);
979 port->bond_stable_id = bond_stable_id;
985 bundle_destroy(struct ofbundle *bundle)
987 struct ofproto_dpif *ofproto;
988 struct ofport_dpif *port, *next_port;
995 ofproto = bundle->ofproto;
996 for (i = 0; i < MAX_MIRRORS; i++) {
997 struct ofmirror *m = ofproto->mirrors[i];
999 if (m->out == bundle) {
1001 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1002 || hmapx_find_and_delete(&m->dsts, bundle)) {
1003 ofproto->need_revalidate = true;
1008 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1009 bundle_del_port(port);
1012 bundle_flush_macs(bundle);
1013 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1015 free(bundle->trunks);
1016 lacp_destroy(bundle->lacp);
1017 bond_destroy(bundle->bond);
1022 bundle_set(struct ofproto *ofproto_, void *aux,
1023 const struct ofproto_bundle_settings *s)
1025 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1026 bool need_flush = false;
1027 const unsigned long *trunks;
1028 struct ofport_dpif *port;
1029 struct ofbundle *bundle;
1034 bundle_destroy(bundle_lookup(ofproto, aux));
1038 assert(s->n_slaves == 1 || s->bond != NULL);
1039 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1041 bundle = bundle_lookup(ofproto, aux);
1043 bundle = xmalloc(sizeof *bundle);
1045 bundle->ofproto = ofproto;
1046 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1047 hash_pointer(aux, 0));
1049 bundle->name = NULL;
1051 list_init(&bundle->ports);
1053 bundle->trunks = NULL;
1054 bundle->lacp = NULL;
1055 bundle->bond = NULL;
1057 bundle->floodable = true;
1059 bundle->src_mirrors = 0;
1060 bundle->dst_mirrors = 0;
1061 bundle->mirror_out = 0;
1064 if (!bundle->name || strcmp(s->name, bundle->name)) {
1066 bundle->name = xstrdup(s->name);
1071 if (!bundle->lacp) {
1072 bundle->lacp = lacp_create();
1074 lacp_configure(bundle->lacp, s->lacp);
1076 lacp_destroy(bundle->lacp);
1077 bundle->lacp = NULL;
1080 /* Update set of ports. */
1082 for (i = 0; i < s->n_slaves; i++) {
1083 if (!bundle_add_port(bundle, s->slaves[i],
1084 s->lacp ? &s->lacp_slaves[i] : NULL,
1085 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1089 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1090 struct ofport_dpif *next_port;
1092 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1093 for (i = 0; i < s->n_slaves; i++) {
1094 if (s->slaves[i] == port->up.ofp_port) {
1099 bundle_del_port(port);
1103 assert(list_size(&bundle->ports) <= s->n_slaves);
1105 if (list_is_empty(&bundle->ports)) {
1106 bundle_destroy(bundle);
1111 if (s->vlan != bundle->vlan) {
1112 bundle->vlan = s->vlan;
1116 /* Get trunked VLANs. */
1117 trunks = s->vlan == -1 ? NULL : s->trunks;
1118 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1119 free(bundle->trunks);
1120 bundle->trunks = vlan_bitmap_clone(trunks);
1125 if (!list_is_short(&bundle->ports)) {
1126 bundle->ofproto->has_bonded_bundles = true;
1128 if (bond_reconfigure(bundle->bond, s->bond)) {
1129 ofproto->need_revalidate = true;
1132 bundle->bond = bond_create(s->bond);
1133 ofproto->need_revalidate = true;
1136 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1137 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1141 bond_destroy(bundle->bond);
1142 bundle->bond = NULL;
1145 /* If we changed something that would affect MAC learning, un-learn
1146 * everything on this port and force flow revalidation. */
1148 bundle_flush_macs(bundle);
1155 bundle_remove(struct ofport *port_)
1157 struct ofport_dpif *port = ofport_dpif_cast(port_);
1158 struct ofbundle *bundle = port->bundle;
1161 bundle_del_port(port);
1162 if (list_is_empty(&bundle->ports)) {
1163 bundle_destroy(bundle);
1164 } else if (list_is_short(&bundle->ports)) {
1165 bond_destroy(bundle->bond);
1166 bundle->bond = NULL;
1172 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1174 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1175 struct ofport_dpif *port = port_;
1176 uint8_t ea[ETH_ADDR_LEN];
1179 error = netdev_get_etheraddr(port->up.netdev, ea);
1181 struct ofpbuf packet;
1184 ofpbuf_init(&packet, 0);
1185 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1187 memcpy(packet_pdu, pdu, pdu_size);
1189 error = netdev_send(port->up.netdev, &packet);
1191 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1192 "(%s)", port->bundle->name,
1193 netdev_get_name(port->up.netdev), strerror(error));
1195 ofpbuf_uninit(&packet);
1197 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1198 "%s (%s)", port->bundle->name,
1199 netdev_get_name(port->up.netdev), strerror(error));
1204 bundle_send_learning_packets(struct ofbundle *bundle)
1206 struct ofproto_dpif *ofproto = bundle->ofproto;
1207 int error, n_packets, n_errors;
1208 struct mac_entry *e;
1210 error = n_packets = n_errors = 0;
1211 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1212 if (e->port.p != bundle) {
1213 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1223 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1224 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1225 "packets, last error was: %s",
1226 bundle->name, n_errors, n_packets, strerror(error));
1228 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1229 bundle->name, n_packets);
1234 bundle_run(struct ofbundle *bundle)
1237 lacp_run(bundle->lacp, send_pdu_cb);
1240 struct ofport_dpif *port;
1242 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1243 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1246 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1247 lacp_negotiated(bundle->lacp));
1248 if (bond_should_send_learning_packets(bundle->bond)) {
1249 bundle_send_learning_packets(bundle);
1255 bundle_wait(struct ofbundle *bundle)
1258 lacp_wait(bundle->lacp);
1261 bond_wait(bundle->bond);
1268 mirror_scan(struct ofproto_dpif *ofproto)
1272 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1273 if (!ofproto->mirrors[idx]) {
1280 static struct ofmirror *
1281 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1285 for (i = 0; i < MAX_MIRRORS; i++) {
1286 struct ofmirror *mirror = ofproto->mirrors[i];
1287 if (mirror && mirror->aux == aux) {
1296 mirror_set(struct ofproto *ofproto_, void *aux,
1297 const struct ofproto_mirror_settings *s)
1299 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1300 mirror_mask_t mirror_bit;
1301 struct ofbundle *bundle;
1302 struct ofmirror *mirror;
1303 struct ofbundle *out;
1304 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1305 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1308 mirror = mirror_lookup(ofproto, aux);
1310 mirror_destroy(mirror);
1316 idx = mirror_scan(ofproto);
1318 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1320 ofproto->up.name, MAX_MIRRORS, s->name);
1324 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1325 mirror->ofproto = ofproto;
1328 mirror->out_vlan = -1;
1329 mirror->name = NULL;
1332 if (!mirror->name || strcmp(s->name, mirror->name)) {
1334 mirror->name = xstrdup(s->name);
1337 /* Get the new configuration. */
1338 if (s->out_bundle) {
1339 out = bundle_lookup(ofproto, s->out_bundle);
1341 mirror_destroy(mirror);
1347 out_vlan = s->out_vlan;
1349 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1350 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1352 /* If the configuration has not changed, do nothing. */
1353 if (hmapx_equals(&srcs, &mirror->srcs)
1354 && hmapx_equals(&dsts, &mirror->dsts)
1355 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1356 && mirror->out == out
1357 && mirror->out_vlan == out_vlan)
1359 hmapx_destroy(&srcs);
1360 hmapx_destroy(&dsts);
1364 hmapx_swap(&srcs, &mirror->srcs);
1365 hmapx_destroy(&srcs);
1367 hmapx_swap(&dsts, &mirror->dsts);
1368 hmapx_destroy(&dsts);
1370 free(mirror->vlans);
1371 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1374 mirror->out_vlan = out_vlan;
1376 /* Update bundles. */
1377 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1378 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1379 if (hmapx_contains(&mirror->srcs, bundle)) {
1380 bundle->src_mirrors |= mirror_bit;
1382 bundle->src_mirrors &= ~mirror_bit;
1385 if (hmapx_contains(&mirror->dsts, bundle)) {
1386 bundle->dst_mirrors |= mirror_bit;
1388 bundle->dst_mirrors &= ~mirror_bit;
1391 if (mirror->out == bundle) {
1392 bundle->mirror_out |= mirror_bit;
1394 bundle->mirror_out &= ~mirror_bit;
1398 ofproto->need_revalidate = true;
1399 mac_learning_flush(ofproto->ml);
1405 mirror_destroy(struct ofmirror *mirror)
1407 struct ofproto_dpif *ofproto;
1408 mirror_mask_t mirror_bit;
1409 struct ofbundle *bundle;
1415 ofproto = mirror->ofproto;
1416 ofproto->need_revalidate = true;
1417 mac_learning_flush(ofproto->ml);
1419 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1420 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1421 bundle->src_mirrors &= ~mirror_bit;
1422 bundle->dst_mirrors &= ~mirror_bit;
1423 bundle->mirror_out &= ~mirror_bit;
1426 hmapx_destroy(&mirror->srcs);
1427 hmapx_destroy(&mirror->dsts);
1428 free(mirror->vlans);
1430 ofproto->mirrors[mirror->idx] = NULL;
1436 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1438 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1439 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1440 ofproto->need_revalidate = true;
1441 mac_learning_flush(ofproto->ml);
1447 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1449 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1450 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1451 return bundle && bundle->mirror_out != 0;
1455 forward_bpdu_changed(struct ofproto *ofproto_)
1457 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1458 /* Revalidate cached flows whenever forward_bpdu option changes. */
1459 ofproto->need_revalidate = true;
1464 static struct ofport_dpif *
1465 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1467 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1468 return ofport ? ofport_dpif_cast(ofport) : NULL;
1471 static struct ofport_dpif *
1472 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1474 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1478 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1479 struct dpif_port *dpif_port)
1481 ofproto_port->name = dpif_port->name;
1482 ofproto_port->type = dpif_port->type;
1483 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1487 port_run(struct ofport_dpif *ofport)
1489 bool enable = netdev_get_carrier(ofport->up.netdev);
1492 cfm_run(ofport->cfm);
1494 if (cfm_should_send_ccm(ofport->cfm)) {
1495 struct ofpbuf packet;
1497 ofpbuf_init(&packet, 0);
1498 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1499 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1500 ofport->odp_port, &packet);
1501 ofpbuf_uninit(&packet);
1504 enable = enable && !cfm_get_fault(ofport->cfm);
1507 if (ofport->bundle) {
1508 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1511 if (ofport->may_enable != enable) {
1512 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1514 if (ofproto->has_bundle_action) {
1515 ofproto->need_revalidate = true;
1519 ofport->may_enable = enable;
1523 port_wait(struct ofport_dpif *ofport)
1526 cfm_wait(ofport->cfm);
1531 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1532 struct ofproto_port *ofproto_port)
1534 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1535 struct dpif_port dpif_port;
1538 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1540 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1546 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1548 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1552 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1554 *ofp_portp = odp_port_to_ofp_port(odp_port);
1560 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1562 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1565 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1567 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1569 /* The caller is going to close ofport->up.netdev. If this is a
1570 * bonded port, then the bond is using that netdev, so remove it
1571 * from the bond. The client will need to reconfigure everything
1572 * after deleting ports, so then the slave will get re-added. */
1573 bundle_remove(&ofport->up);
1579 struct port_dump_state {
1580 struct dpif_port_dump dump;
1585 port_dump_start(const struct ofproto *ofproto_, void **statep)
1587 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1588 struct port_dump_state *state;
1590 *statep = state = xmalloc(sizeof *state);
1591 dpif_port_dump_start(&state->dump, ofproto->dpif);
1592 state->done = false;
1597 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1598 struct ofproto_port *port)
1600 struct port_dump_state *state = state_;
1601 struct dpif_port dpif_port;
1603 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1604 ofproto_port_from_dpif_port(port, &dpif_port);
1607 int error = dpif_port_dump_done(&state->dump);
1609 return error ? error : EOF;
1614 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1616 struct port_dump_state *state = state_;
1619 dpif_port_dump_done(&state->dump);
1626 port_poll(const struct ofproto *ofproto_, char **devnamep)
1628 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1629 return dpif_port_poll(ofproto->dpif, devnamep);
1633 port_poll_wait(const struct ofproto *ofproto_)
1635 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1636 dpif_port_poll_wait(ofproto->dpif);
1640 port_is_lacp_current(const struct ofport *ofport_)
1642 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1643 return (ofport->bundle && ofport->bundle->lacp
1644 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1648 /* Upcall handling. */
1650 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1651 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1652 * their individual configurations.
1654 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1655 * Otherwise, ownership is transferred to this function. */
1657 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1658 const struct flow *flow, bool clone)
1660 struct ofputil_packet_in pin;
1662 pin.packet = upcall->packet;
1663 pin.in_port = flow->in_port;
1664 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1665 pin.buffer_id = 0; /* not yet known */
1666 pin.send_len = upcall->userdata;
1667 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1668 clone ? NULL : upcall->packet);
1672 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1673 const struct ofpbuf *packet)
1675 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1681 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
1683 cfm_process_heartbeat(ofport->cfm, packet);
1686 } else if (ofport->bundle && ofport->bundle->lacp
1687 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1689 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
1697 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1699 struct facet *facet;
1702 /* Obtain in_port and tun_id, at least. */
1703 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1705 /* Set header pointers in 'flow'. */
1706 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1708 /* Handle 802.1ag and LACP. */
1709 if (process_special(ofproto, &flow, upcall->packet)) {
1710 ofpbuf_delete(upcall->packet);
1711 ofproto->n_matches++;
1715 /* Check with in-band control to see if this packet should be sent
1716 * to the local port regardless of the flow table. */
1717 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1718 send_packet(ofproto, OVSP_LOCAL, upcall->packet);
1721 facet = facet_lookup_valid(ofproto, &flow);
1723 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow, 0);
1725 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1726 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1728 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1729 COVERAGE_INC(ofproto_dpif_no_packet_in);
1730 /* XXX install 'drop' flow entry */
1731 ofpbuf_delete(upcall->packet);
1735 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1739 send_packet_in(ofproto, upcall, &flow, false);
1743 facet = facet_create(rule, &flow, upcall->packet);
1744 } else if (!facet->may_install) {
1745 /* The facet is not installable, that is, we need to process every
1746 * packet, so process the current packet's actions into 'facet'. */
1747 facet_make_actions(ofproto, facet, upcall->packet);
1750 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1752 * Extra-special case for fail-open mode.
1754 * We are in fail-open mode and the packet matched the fail-open rule,
1755 * but we are connected to a controller too. We should send the packet
1756 * up to the controller in the hope that it will try to set up a flow
1757 * and thereby allow us to exit fail-open.
1759 * See the top-level comment in fail-open.c for more information.
1761 send_packet_in(ofproto, upcall, &flow, true);
1764 facet_execute(ofproto, facet, upcall->packet);
1765 facet_install(ofproto, facet, false);
1766 ofproto->n_matches++;
1770 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1774 switch (upcall->type) {
1775 case DPIF_UC_ACTION:
1776 COVERAGE_INC(ofproto_dpif_ctlr_action);
1777 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1778 send_packet_in(ofproto, upcall, &flow, false);
1781 case DPIF_UC_SAMPLE:
1782 if (ofproto->sflow) {
1783 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1784 dpif_sflow_received(ofproto->sflow, upcall, &flow);
1786 ofpbuf_delete(upcall->packet);
1790 handle_miss_upcall(ofproto, upcall);
1793 case DPIF_N_UC_TYPES:
1795 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1800 /* Flow expiration. */
1802 static int facet_max_idle(const struct ofproto_dpif *);
1803 static void update_stats(struct ofproto_dpif *);
1804 static void rule_expire(struct rule_dpif *);
1805 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1807 /* This function is called periodically by run(). Its job is to collect
1808 * updates for the flows that have been installed into the datapath, most
1809 * importantly when they last were used, and then use that information to
1810 * expire flows that have not been used recently.
1812 * Returns the number of milliseconds after which it should be called again. */
1814 expire(struct ofproto_dpif *ofproto)
1816 struct rule_dpif *rule, *next_rule;
1817 struct classifier *table;
1820 /* Update stats for each flow in the datapath. */
1821 update_stats(ofproto);
1823 /* Expire facets that have been idle too long. */
1824 dp_max_idle = facet_max_idle(ofproto);
1825 expire_facets(ofproto, dp_max_idle);
1827 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1828 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1829 struct cls_cursor cursor;
1831 cls_cursor_init(&cursor, table, NULL);
1832 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1837 /* All outstanding data in existing flows has been accounted, so it's a
1838 * good time to do bond rebalancing. */
1839 if (ofproto->has_bonded_bundles) {
1840 struct ofbundle *bundle;
1842 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1844 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1849 return MIN(dp_max_idle, 1000);
1852 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1854 * This function also pushes statistics updates to rules which each facet
1855 * resubmits into. Generally these statistics will be accurate. However, if a
1856 * facet changes the rule it resubmits into at some time in between
1857 * update_stats() runs, it is possible that statistics accrued to the
1858 * old rule will be incorrectly attributed to the new rule. This could be
1859 * avoided by calling update_stats() whenever rules are created or
1860 * deleted. However, the performance impact of making so many calls to the
1861 * datapath do not justify the benefit of having perfectly accurate statistics.
1864 update_stats(struct ofproto_dpif *p)
1866 const struct dpif_flow_stats *stats;
1867 struct dpif_flow_dump dump;
1868 const struct nlattr *key;
1871 dpif_flow_dump_start(&dump, p->dpif);
1872 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1873 struct facet *facet;
1876 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1880 odp_flow_key_format(key, key_len, &s);
1881 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
1887 facet = facet_find(p, &flow);
1889 if (facet && facet->installed) {
1891 if (stats->n_packets >= facet->dp_packet_count) {
1892 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1893 facet->packet_count += extra;
1895 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1898 if (stats->n_bytes >= facet->dp_byte_count) {
1899 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1901 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1904 facet->dp_packet_count = stats->n_packets;
1905 facet->dp_byte_count = stats->n_bytes;
1907 facet_update_time(p, facet, stats->used);
1908 facet_account(p, facet);
1909 facet_push_stats(facet);
1911 /* There's a flow in the datapath that we know nothing about.
1913 COVERAGE_INC(facet_unexpected);
1914 dpif_flow_del(p->dpif, key, key_len, NULL);
1917 dpif_flow_dump_done(&dump);
1920 /* Calculates and returns the number of milliseconds of idle time after which
1921 * facets should expire from the datapath and we should fold their statistics
1922 * into their parent rules in userspace. */
1924 facet_max_idle(const struct ofproto_dpif *ofproto)
1927 * Idle time histogram.
1929 * Most of the time a switch has a relatively small number of facets. When
1930 * this is the case we might as well keep statistics for all of them in
1931 * userspace and to cache them in the kernel datapath for performance as
1934 * As the number of facets increases, the memory required to maintain
1935 * statistics about them in userspace and in the kernel becomes
1936 * significant. However, with a large number of facets it is likely that
1937 * only a few of them are "heavy hitters" that consume a large amount of
1938 * bandwidth. At this point, only heavy hitters are worth caching in the
1939 * kernel and maintaining in userspaces; other facets we can discard.
1941 * The technique used to compute the idle time is to build a histogram with
1942 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1943 * that is installed in the kernel gets dropped in the appropriate bucket.
1944 * After the histogram has been built, we compute the cutoff so that only
1945 * the most-recently-used 1% of facets (but at least
1946 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1947 * the most-recently-used bucket of facets is kept, so actually an
1948 * arbitrary number of facets can be kept in any given expiration run
1949 * (though the next run will delete most of those unless they receive
1952 * This requires a second pass through the facets, in addition to the pass
1953 * made by update_stats(), because the former function never looks
1954 * at uninstallable facets.
1956 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1957 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1958 int buckets[N_BUCKETS] = { 0 };
1959 int total, subtotal, bucket;
1960 struct facet *facet;
1964 total = hmap_count(&ofproto->facets);
1965 if (total <= ofproto->up.flow_eviction_threshold) {
1966 return N_BUCKETS * BUCKET_WIDTH;
1969 /* Build histogram. */
1971 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1972 long long int idle = now - facet->used;
1973 int bucket = (idle <= 0 ? 0
1974 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1975 : (unsigned int) idle / BUCKET_WIDTH);
1979 /* Find the first bucket whose flows should be expired. */
1980 subtotal = bucket = 0;
1982 subtotal += buckets[bucket++];
1983 } while (bucket < N_BUCKETS &&
1984 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
1986 if (VLOG_IS_DBG_ENABLED()) {
1990 ds_put_cstr(&s, "keep");
1991 for (i = 0; i < N_BUCKETS; i++) {
1993 ds_put_cstr(&s, ", drop");
1996 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1999 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2003 return bucket * BUCKET_WIDTH;
2007 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2009 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2010 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2011 struct ofexpired expired;
2013 if (facet->installed) {
2014 struct dpif_flow_stats stats;
2016 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2018 facet_update_stats(ofproto, facet, &stats);
2021 expired.flow = facet->flow;
2022 expired.packet_count = facet->packet_count;
2023 expired.byte_count = facet->byte_count;
2024 expired.used = facet->used;
2025 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2030 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2032 long long int cutoff = time_msec() - dp_max_idle;
2033 struct facet *facet, *next_facet;
2035 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2036 facet_active_timeout(ofproto, facet);
2037 if (facet->used < cutoff) {
2038 facet_remove(ofproto, facet);
2043 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2044 * then delete it entirely. */
2046 rule_expire(struct rule_dpif *rule)
2048 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2049 struct facet *facet, *next_facet;
2053 /* Has 'rule' expired? */
2055 if (rule->up.hard_timeout
2056 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2057 reason = OFPRR_HARD_TIMEOUT;
2058 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2059 && now > rule->used + rule->up.idle_timeout * 1000) {
2060 reason = OFPRR_IDLE_TIMEOUT;
2065 COVERAGE_INC(ofproto_dpif_expired);
2067 /* Update stats. (This is a no-op if the rule expired due to an idle
2068 * timeout, because that only happens when the rule has no facets left.) */
2069 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2070 facet_remove(ofproto, facet);
2073 /* Get rid of the rule. */
2074 ofproto_rule_expire(&rule->up, reason);
2079 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2080 * example 'packet' within that flow.
2082 * The caller must already have determined that no facet with an identical
2083 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2084 * the ofproto's classifier table. */
2085 static struct facet *
2086 facet_create(struct rule_dpif *rule, const struct flow *flow,
2087 const struct ofpbuf *packet)
2089 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2090 struct facet *facet;
2092 facet = xzalloc(sizeof *facet);
2093 facet->used = time_msec();
2094 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2095 list_push_back(&rule->facets, &facet->list_node);
2097 facet->flow = *flow;
2098 netflow_flow_init(&facet->nf_flow);
2099 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2101 facet_make_actions(ofproto, facet, packet);
2107 facet_free(struct facet *facet)
2109 free(facet->actions);
2113 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2114 * 'packet', which arrived on 'in_port'.
2116 * Takes ownership of 'packet'. */
2118 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2119 const struct nlattr *odp_actions, size_t actions_len,
2120 struct ofpbuf *packet)
2122 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2123 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE) {
2124 /* As an optimization, avoid a round-trip from userspace to kernel to
2125 * userspace. This also avoids possibly filling up kernel packet
2126 * buffers along the way. */
2127 struct dpif_upcall upcall;
2129 upcall.type = DPIF_UC_ACTION;
2130 upcall.packet = packet;
2133 upcall.userdata = nl_attr_get_u64(odp_actions);
2134 upcall.sample_pool = 0;
2135 upcall.actions = NULL;
2136 upcall.actions_len = 0;
2138 send_packet_in(ofproto, &upcall, flow, false);
2142 struct odputil_keybuf keybuf;
2146 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2147 odp_flow_key_from_flow(&key, flow);
2149 error = dpif_execute(ofproto->dpif, key.data, key.size,
2150 odp_actions, actions_len, packet);
2152 ofpbuf_delete(packet);
2157 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2158 * statistics appropriately. 'packet' must have at least sizeof(struct
2159 * ofp_packet_in) bytes of headroom.
2161 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2162 * applying flow_extract() to 'packet' would yield the same flow as
2165 * 'facet' must have accurately composed datapath actions; that is, it must
2166 * not be in need of revalidation.
2168 * Takes ownership of 'packet'. */
2170 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2171 struct ofpbuf *packet)
2173 struct dpif_flow_stats stats;
2175 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2177 flow_extract_stats(&facet->flow, packet, &stats);
2178 stats.used = time_msec();
2179 if (execute_odp_actions(ofproto, &facet->flow,
2180 facet->actions, facet->actions_len, packet)) {
2181 facet_update_stats(ofproto, facet, &stats);
2185 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2187 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2188 * rule's statistics, via facet_uninstall().
2190 * - Removes 'facet' from its rule and from ofproto->facets.
2193 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2195 facet_uninstall(ofproto, facet);
2196 facet_flush_stats(ofproto, facet);
2197 hmap_remove(&ofproto->facets, &facet->hmap_node);
2198 list_remove(&facet->list_node);
2202 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2204 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2205 const struct ofpbuf *packet)
2207 const struct rule_dpif *rule = facet->rule;
2208 struct ofpbuf *odp_actions;
2209 struct action_xlate_ctx ctx;
2211 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2212 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2213 facet->tags = ctx.tags;
2214 facet->may_install = ctx.may_set_up_flow;
2215 facet->has_learn = ctx.has_learn;
2216 facet->has_normal = ctx.has_normal;
2217 facet->nf_flow.output_iface = ctx.nf_output_iface;
2219 if (facet->actions_len != odp_actions->size
2220 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2221 free(facet->actions);
2222 facet->actions_len = odp_actions->size;
2223 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2226 ofpbuf_delete(odp_actions);
2229 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2230 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2231 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2232 * since 'facet' was last updated.
2234 * Returns 0 if successful, otherwise a positive errno value.*/
2236 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2237 const struct nlattr *actions, size_t actions_len,
2238 struct dpif_flow_stats *stats)
2240 struct odputil_keybuf keybuf;
2241 enum dpif_flow_put_flags flags;
2245 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2247 flags |= DPIF_FP_ZERO_STATS;
2250 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2251 odp_flow_key_from_flow(&key, &facet->flow);
2253 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2254 actions, actions_len, stats);
2257 facet_reset_dp_stats(facet, stats);
2263 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2264 * 'zero_stats' is true, clears any existing statistics from the datapath for
2267 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2269 struct dpif_flow_stats stats;
2271 if (facet->may_install
2272 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2273 zero_stats ? &stats : NULL)) {
2274 facet->installed = true;
2279 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2281 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2285 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2288 const struct nlattr *a;
2292 if (facet->byte_count <= facet->accounted_bytes) {
2295 n_bytes = facet->byte_count - facet->accounted_bytes;
2296 facet->accounted_bytes = facet->byte_count;
2298 /* Feed information from the active flows back into the learning table to
2299 * ensure that table is always in sync with what is actually flowing
2300 * through the datapath. */
2301 if (facet->has_learn || facet->has_normal) {
2302 struct action_xlate_ctx ctx;
2304 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2305 ctx.may_learn = true;
2306 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2307 facet->rule->up.n_actions));
2310 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2314 /* This loop feeds byte counters to bond_account() for rebalancing to use
2315 * as a basis. We also need to track the actual VLAN on which the packet
2316 * is going to be sent to ensure that it matches the one passed to
2317 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2319 vlan_tci = facet->flow.vlan_tci;
2320 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2321 struct ofport_dpif *port;
2323 switch (nl_attr_type(a)) {
2324 case OVS_ACTION_ATTR_OUTPUT:
2325 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2326 if (port && port->bundle && port->bundle->bond) {
2327 bond_account(port->bundle->bond, &facet->flow,
2328 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2332 case OVS_ACTION_ATTR_POP_VLAN:
2333 vlan_tci = htons(0);
2336 case OVS_ACTION_ATTR_PUSH_VLAN:
2337 vlan_tci = nl_attr_get_be16(a);
2343 /* If 'rule' is installed in the datapath, uninstalls it. */
2345 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2347 if (facet->installed) {
2348 struct odputil_keybuf keybuf;
2349 struct dpif_flow_stats stats;
2353 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2354 odp_flow_key_from_flow(&key, &facet->flow);
2356 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2357 facet_reset_dp_stats(facet, &stats);
2359 facet_update_stats(p, facet, &stats);
2361 facet->installed = false;
2363 assert(facet->dp_packet_count == 0);
2364 assert(facet->dp_byte_count == 0);
2368 /* Returns true if the only action for 'facet' is to send to the controller.
2369 * (We don't report NetFlow expiration messages for such facets because they
2370 * are just part of the control logic for the network, not real traffic). */
2372 facet_is_controller_flow(struct facet *facet)
2375 && facet->rule->up.n_actions == 1
2376 && action_outputs_to_port(&facet->rule->up.actions[0],
2377 htons(OFPP_CONTROLLER)));
2380 /* Resets 'facet''s datapath statistics counters. This should be called when
2381 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2382 * it should contain the statistics returned by dpif when 'facet' was reset in
2383 * the datapath. 'stats' will be modified to only included statistics new
2384 * since 'facet' was last updated. */
2386 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2388 if (stats && facet->dp_packet_count <= stats->n_packets
2389 && facet->dp_byte_count <= stats->n_bytes) {
2390 stats->n_packets -= facet->dp_packet_count;
2391 stats->n_bytes -= facet->dp_byte_count;
2394 facet->dp_packet_count = 0;
2395 facet->dp_byte_count = 0;
2398 /* Folds all of 'facet''s statistics into its rule. Also updates the
2399 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2400 * 'facet''s statistics in the datapath should have been zeroed and folded into
2401 * its packet and byte counts before this function is called. */
2403 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2405 assert(!facet->dp_byte_count);
2406 assert(!facet->dp_packet_count);
2408 facet_push_stats(facet);
2409 facet_account(ofproto, facet);
2411 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2412 struct ofexpired expired;
2413 expired.flow = facet->flow;
2414 expired.packet_count = facet->packet_count;
2415 expired.byte_count = facet->byte_count;
2416 expired.used = facet->used;
2417 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2420 facet->rule->packet_count += facet->packet_count;
2421 facet->rule->byte_count += facet->byte_count;
2423 /* Reset counters to prevent double counting if 'facet' ever gets
2425 facet_reset_counters(facet);
2427 netflow_flow_clear(&facet->nf_flow);
2430 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2431 * Returns it if found, otherwise a null pointer.
2433 * The returned facet might need revalidation; use facet_lookup_valid()
2434 * instead if that is important. */
2435 static struct facet *
2436 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2438 struct facet *facet;
2440 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2442 if (flow_equal(flow, &facet->flow)) {
2450 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2451 * Returns it if found, otherwise a null pointer.
2453 * The returned facet is guaranteed to be valid. */
2454 static struct facet *
2455 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2457 struct facet *facet = facet_find(ofproto, flow);
2459 /* The facet we found might not be valid, since we could be in need of
2460 * revalidation. If it is not valid, don't return it. */
2462 && ofproto->need_revalidate
2463 && !facet_revalidate(ofproto, facet)) {
2464 COVERAGE_INC(facet_invalidated);
2471 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2473 * - If the rule found is different from 'facet''s current rule, moves
2474 * 'facet' to the new rule and recompiles its actions.
2476 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2477 * where it is and recompiles its actions anyway.
2479 * - If there is none, destroys 'facet'.
2481 * Returns true if 'facet' still exists, false if it has been destroyed. */
2483 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2485 struct action_xlate_ctx ctx;
2486 struct ofpbuf *odp_actions;
2487 struct rule_dpif *new_rule;
2488 bool actions_changed;
2490 COVERAGE_INC(facet_revalidate);
2492 /* Determine the new rule. */
2493 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
2495 /* No new rule, so delete the facet. */
2496 facet_remove(ofproto, facet);
2500 /* Calculate new datapath actions.
2502 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2503 * emit a NetFlow expiration and, if so, we need to have the old state
2504 * around to properly compose it. */
2505 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2506 odp_actions = xlate_actions(&ctx,
2507 new_rule->up.actions, new_rule->up.n_actions);
2508 actions_changed = (facet->actions_len != odp_actions->size
2509 || memcmp(facet->actions, odp_actions->data,
2510 facet->actions_len));
2512 /* If the datapath actions changed or the installability changed,
2513 * then we need to talk to the datapath. */
2514 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2515 if (ctx.may_set_up_flow) {
2516 struct dpif_flow_stats stats;
2518 facet_put__(ofproto, facet,
2519 odp_actions->data, odp_actions->size, &stats);
2520 facet_update_stats(ofproto, facet, &stats);
2522 facet_uninstall(ofproto, facet);
2525 /* The datapath flow is gone or has zeroed stats, so push stats out of
2526 * 'facet' into 'rule'. */
2527 facet_flush_stats(ofproto, facet);
2530 /* Update 'facet' now that we've taken care of all the old state. */
2531 facet->tags = ctx.tags;
2532 facet->nf_flow.output_iface = ctx.nf_output_iface;
2533 facet->may_install = ctx.may_set_up_flow;
2534 facet->has_learn = ctx.has_learn;
2535 facet->has_normal = ctx.has_normal;
2536 if (actions_changed) {
2537 free(facet->actions);
2538 facet->actions_len = odp_actions->size;
2539 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2541 if (facet->rule != new_rule) {
2542 COVERAGE_INC(facet_changed_rule);
2543 list_remove(&facet->list_node);
2544 list_push_back(&new_rule->facets, &facet->list_node);
2545 facet->rule = new_rule;
2546 facet->used = new_rule->up.created;
2547 facet->rs_used = facet->used;
2550 ofpbuf_delete(odp_actions);
2555 /* Updates 'facet''s used time. Caller is responsible for calling
2556 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2558 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2561 if (used > facet->used) {
2563 if (used > facet->rule->used) {
2564 facet->rule->used = used;
2566 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2570 /* Folds the statistics from 'stats' into the counters in 'facet'.
2572 * Because of the meaning of a facet's counters, it only makes sense to do this
2573 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2574 * packet that was sent by hand or if it represents statistics that have been
2575 * cleared out of the datapath. */
2577 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2578 const struct dpif_flow_stats *stats)
2580 if (stats->n_packets || stats->used > facet->used) {
2581 facet_update_time(ofproto, facet, stats->used);
2582 facet->packet_count += stats->n_packets;
2583 facet->byte_count += stats->n_bytes;
2584 facet_push_stats(facet);
2585 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2590 facet_reset_counters(struct facet *facet)
2592 facet->packet_count = 0;
2593 facet->byte_count = 0;
2594 facet->rs_packet_count = 0;
2595 facet->rs_byte_count = 0;
2596 facet->accounted_bytes = 0;
2600 facet_push_stats(struct facet *facet)
2602 uint64_t rs_packets, rs_bytes;
2604 assert(facet->packet_count >= facet->rs_packet_count);
2605 assert(facet->byte_count >= facet->rs_byte_count);
2606 assert(facet->used >= facet->rs_used);
2608 rs_packets = facet->packet_count - facet->rs_packet_count;
2609 rs_bytes = facet->byte_count - facet->rs_byte_count;
2611 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2612 facet->rs_packet_count = facet->packet_count;
2613 facet->rs_byte_count = facet->byte_count;
2614 facet->rs_used = facet->used;
2616 flow_push_stats(facet->rule, &facet->flow,
2617 rs_packets, rs_bytes, facet->used);
2621 struct ofproto_push {
2622 struct action_xlate_ctx ctx;
2629 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2631 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2634 rule->packet_count += push->packets;
2635 rule->byte_count += push->bytes;
2636 rule->used = MAX(push->used, rule->used);
2640 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2641 * 'rule''s actions. */
2643 flow_push_stats(const struct rule_dpif *rule,
2644 struct flow *flow, uint64_t packets, uint64_t bytes,
2647 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2648 struct ofproto_push push;
2650 push.packets = packets;
2654 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2655 push.ctx.resubmit_hook = push_resubmit;
2656 ofpbuf_delete(xlate_actions(&push.ctx,
2657 rule->up.actions, rule->up.n_actions));
2662 static struct rule_dpif *
2663 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2666 if (table_id >= N_TABLES) {
2670 return rule_dpif_cast(rule_from_cls_rule(
2671 classifier_lookup(&ofproto->up.tables[table_id],
2676 complete_operation(struct rule_dpif *rule)
2678 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2680 rule_invalidate(rule);
2682 struct dpif_completion *c = xmalloc(sizeof *c);
2683 c->op = rule->up.pending;
2684 list_push_back(&ofproto->completions, &c->list_node);
2686 ofoperation_complete(rule->up.pending, 0);
2690 static struct rule *
2693 struct rule_dpif *rule = xmalloc(sizeof *rule);
2698 rule_dealloc(struct rule *rule_)
2700 struct rule_dpif *rule = rule_dpif_cast(rule_);
2705 rule_construct(struct rule *rule_)
2707 struct rule_dpif *rule = rule_dpif_cast(rule_);
2708 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2709 struct rule_dpif *victim;
2713 error = validate_actions(rule->up.actions, rule->up.n_actions,
2714 &rule->up.cr.flow, ofproto->max_ports);
2719 rule->used = rule->up.created;
2720 rule->packet_count = 0;
2721 rule->byte_count = 0;
2723 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2724 if (victim && !list_is_empty(&victim->facets)) {
2725 struct facet *facet;
2727 rule->facets = victim->facets;
2728 list_moved(&rule->facets);
2729 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2730 /* XXX: We're only clearing our local counters here. It's possible
2731 * that quite a few packets are unaccounted for in the datapath
2732 * statistics. These will be accounted to the new rule instead of
2733 * cleared as required. This could be fixed by clearing out the
2734 * datapath statistics for this facet, but currently it doesn't
2736 facet_reset_counters(facet);
2740 /* Must avoid list_moved() in this case. */
2741 list_init(&rule->facets);
2744 table_id = rule->up.table_id;
2745 rule->tag = (victim ? victim->tag
2747 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
2748 ofproto->tables[table_id].basis));
2750 complete_operation(rule);
2755 rule_destruct(struct rule *rule_)
2757 struct rule_dpif *rule = rule_dpif_cast(rule_);
2758 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2759 struct facet *facet, *next_facet;
2761 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2762 facet_revalidate(ofproto, facet);
2765 complete_operation(rule);
2769 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2771 struct rule_dpif *rule = rule_dpif_cast(rule_);
2772 struct facet *facet;
2774 /* Start from historical data for 'rule' itself that are no longer tracked
2775 * in facets. This counts, for example, facets that have expired. */
2776 *packets = rule->packet_count;
2777 *bytes = rule->byte_count;
2779 /* Add any statistics that are tracked by facets. This includes
2780 * statistical data recently updated by ofproto_update_stats() as well as
2781 * stats for packets that were executed "by hand" via dpif_execute(). */
2782 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2783 *packets += facet->packet_count;
2784 *bytes += facet->byte_count;
2789 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2791 struct rule_dpif *rule = rule_dpif_cast(rule_);
2792 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2793 struct action_xlate_ctx ctx;
2794 struct ofpbuf *odp_actions;
2795 struct facet *facet;
2798 /* First look for a related facet. If we find one, account it to that. */
2799 facet = facet_lookup_valid(ofproto, flow);
2800 if (facet && facet->rule == rule) {
2801 facet_execute(ofproto, facet, packet);
2805 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2806 * create a new facet for it and use that. */
2807 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
2808 facet = facet_create(rule, flow, packet);
2809 facet_execute(ofproto, facet, packet);
2810 facet_install(ofproto, facet, true);
2814 /* We can't account anything to a facet. If we were to try, then that
2815 * facet would have a non-matching rule, busting our invariants. */
2816 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2817 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2818 size = packet->size;
2819 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2820 odp_actions->size, packet)) {
2821 rule->used = time_msec();
2822 rule->packet_count++;
2823 rule->byte_count += size;
2824 flow_push_stats(rule, flow, 1, size, rule->used);
2826 ofpbuf_delete(odp_actions);
2832 rule_modify_actions(struct rule *rule_)
2834 struct rule_dpif *rule = rule_dpif_cast(rule_);
2835 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2838 error = validate_actions(rule->up.actions, rule->up.n_actions,
2839 &rule->up.cr.flow, ofproto->max_ports);
2841 ofoperation_complete(rule->up.pending, error);
2845 complete_operation(rule);
2848 /* Sends 'packet' out of port 'odp_port' within 'p'.
2849 * Returns 0 if successful, otherwise a positive errno value. */
2851 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2852 const struct ofpbuf *packet)
2854 struct ofpbuf key, odp_actions;
2855 struct odputil_keybuf keybuf;
2859 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2860 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2861 odp_flow_key_from_flow(&key, &flow);
2863 ofpbuf_init(&odp_actions, 32);
2864 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2865 error = dpif_execute(ofproto->dpif,
2867 odp_actions.data, odp_actions.size,
2869 ofpbuf_uninit(&odp_actions);
2872 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2873 ofproto->up.name, odp_port, strerror(error));
2878 /* OpenFlow to datapath action translation. */
2880 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2881 struct action_xlate_ctx *ctx);
2882 static void xlate_normal(struct action_xlate_ctx *);
2885 commit_odp_actions(struct action_xlate_ctx *ctx)
2887 const struct flow *flow = &ctx->flow;
2888 struct flow *base = &ctx->base_flow;
2889 struct ofpbuf *odp_actions = ctx->odp_actions;
2891 if (base->tun_id != flow->tun_id) {
2892 nl_msg_put_be64(odp_actions, OVS_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
2893 base->tun_id = flow->tun_id;
2896 if (base->nw_src != flow->nw_src) {
2897 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
2898 base->nw_src = flow->nw_src;
2901 if (base->nw_dst != flow->nw_dst) {
2902 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
2903 base->nw_dst = flow->nw_dst;
2906 if (base->nw_tos != flow->nw_tos) {
2907 nl_msg_put_u8(odp_actions, OVS_ACTION_ATTR_SET_NW_TOS, flow->nw_tos);
2908 base->nw_tos = flow->nw_tos;
2911 if (base->vlan_tci != flow->vlan_tci) {
2912 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2913 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2915 if (base->vlan_tci != htons(0)) {
2916 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2918 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2919 flow->vlan_tci & ~htons(VLAN_CFI));
2921 base->vlan_tci = flow->vlan_tci;
2924 if (base->tp_src != flow->tp_src) {
2925 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
2926 base->tp_src = flow->tp_src;
2929 if (base->tp_dst != flow->tp_dst) {
2930 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
2931 base->tp_dst = flow->tp_dst;
2934 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
2935 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_SRC,
2936 flow->dl_src, ETH_ADDR_LEN);
2937 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2940 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2941 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_DST,
2942 flow->dl_dst, ETH_ADDR_LEN);
2943 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2946 if (ctx->base_priority != ctx->priority) {
2947 if (ctx->priority) {
2948 nl_msg_put_u32(odp_actions, OVS_ACTION_ATTR_SET_PRIORITY,
2951 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
2953 ctx->base_priority = ctx->priority;
2958 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2960 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2961 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2964 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2965 /* Forwarding disabled on port. */
2970 * We don't have an ofport record for this port, but it doesn't hurt to
2971 * allow forwarding to it anyhow. Maybe such a port will appear later
2972 * and we're pre-populating the flow table.
2976 commit_odp_actions(ctx);
2977 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
2978 ctx->nf_output_iface = ofp_port;
2982 xlate_table_action(struct action_xlate_ctx *ctx,
2983 uint16_t in_port, uint8_t table_id)
2985 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2986 struct ofproto_dpif *ofproto = ctx->ofproto;
2987 struct rule_dpif *rule;
2988 uint16_t old_in_port;
2989 uint8_t old_table_id;
2991 old_table_id = ctx->table_id;
2992 ctx->table_id = table_id;
2994 /* Look up a flow with 'in_port' as the input port. */
2995 old_in_port = ctx->flow.in_port;
2996 ctx->flow.in_port = in_port;
2997 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3000 if (table_id > 0 && table_id < N_TABLES) {
3001 struct table_dpif *table = &ofproto->tables[table_id];
3002 if (table->other_table) {
3005 : rule_calculate_tag(&ctx->flow,
3006 &table->other_table->wc,
3011 /* Restore the original input port. Otherwise OFPP_NORMAL and
3012 * OFPP_IN_PORT will have surprising behavior. */
3013 ctx->flow.in_port = old_in_port;
3015 if (ctx->resubmit_hook) {
3016 ctx->resubmit_hook(ctx, rule);
3021 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3025 ctx->table_id = old_table_id;
3027 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3029 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3030 MAX_RESUBMIT_RECURSION);
3035 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3036 const struct nx_action_resubmit *nar)
3041 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3043 : ntohs(nar->in_port));
3044 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3046 xlate_table_action(ctx, in_port, table_id);
3050 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3052 struct ofport_dpif *ofport;
3054 commit_odp_actions(ctx);
3055 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3056 uint16_t ofp_port = ofport->up.ofp_port;
3057 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
3058 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT,
3063 ctx->nf_output_iface = NF_OUT_FLOOD;
3067 xlate_output_action__(struct action_xlate_ctx *ctx,
3068 uint16_t port, uint16_t max_len)
3070 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3072 ctx->nf_output_iface = NF_OUT_DROP;
3076 add_output_action(ctx, ctx->flow.in_port);
3079 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3085 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3088 flood_packets(ctx, htonl(0));
3090 case OFPP_CONTROLLER:
3091 commit_odp_actions(ctx);
3092 nl_msg_put_u64(ctx->odp_actions, OVS_ACTION_ATTR_USERSPACE, max_len);
3095 add_output_action(ctx, OFPP_LOCAL);
3100 if (port != ctx->flow.in_port) {
3101 add_output_action(ctx, port);
3106 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3107 ctx->nf_output_iface = NF_OUT_FLOOD;
3108 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3109 ctx->nf_output_iface = prev_nf_output_iface;
3110 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3111 ctx->nf_output_iface != NF_OUT_FLOOD) {
3112 ctx->nf_output_iface = NF_OUT_MULTI;
3117 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3118 const struct nx_action_output_reg *naor)
3122 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3124 if (ofp_port <= UINT16_MAX) {
3125 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3130 xlate_output_action(struct action_xlate_ctx *ctx,
3131 const struct ofp_action_output *oao)
3133 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3137 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3138 const struct ofp_action_enqueue *oae)
3140 uint16_t ofp_port, odp_port;
3141 uint32_t ctx_priority, priority;
3144 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3147 /* Fall back to ordinary output action. */
3148 xlate_output_action__(ctx, ntohs(oae->port), 0);
3152 /* Figure out datapath output port. */
3153 ofp_port = ntohs(oae->port);
3154 if (ofp_port == OFPP_IN_PORT) {
3155 ofp_port = ctx->flow.in_port;
3157 odp_port = ofp_port_to_odp_port(ofp_port);
3159 /* Add datapath actions. */
3160 ctx_priority = ctx->priority;
3161 ctx->priority = priority;
3162 add_output_action(ctx, odp_port);
3163 ctx->priority = ctx_priority;
3165 /* Update NetFlow output port. */
3166 if (ctx->nf_output_iface == NF_OUT_DROP) {
3167 ctx->nf_output_iface = odp_port;
3168 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3169 ctx->nf_output_iface = NF_OUT_MULTI;
3174 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3175 const struct nx_action_set_queue *nasq)
3180 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3183 /* Couldn't translate queue to a priority, so ignore. A warning
3184 * has already been logged. */
3188 ctx->priority = priority;
3191 struct xlate_reg_state {
3197 xlate_autopath(struct action_xlate_ctx *ctx,
3198 const struct nx_action_autopath *naa)
3200 uint16_t ofp_port = ntohl(naa->id);
3201 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3203 if (!port || !port->bundle) {
3204 ofp_port = OFPP_NONE;
3205 } else if (port->bundle->bond) {
3206 /* Autopath does not support VLAN hashing. */
3207 struct ofport_dpif *slave = bond_choose_output_slave(
3208 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3210 ofp_port = slave->up.ofp_port;
3213 autopath_execute(naa, &ctx->flow, ofp_port);
3217 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3219 struct ofproto_dpif *ofproto = ofproto_;
3220 struct ofport_dpif *port;
3230 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3233 port = get_ofp_port(ofproto, ofp_port);
3234 return port ? port->may_enable : false;
3239 xlate_learn_action(struct action_xlate_ctx *ctx,
3240 const struct nx_action_learn *learn)
3242 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
3243 struct ofputil_flow_mod fm;
3246 learn_execute(learn, &ctx->flow, &fm);
3248 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
3249 if (error && !VLOG_DROP_WARN(&rl)) {
3250 char *msg = ofputil_error_to_string(error);
3251 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
3259 do_xlate_actions(const union ofp_action *in, size_t n_in,
3260 struct action_xlate_ctx *ctx)
3262 const struct ofport_dpif *port;
3263 const union ofp_action *ia;
3266 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3268 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3269 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3270 ? htonl(OFPPC_NO_RECV_STP)
3271 : htonl(OFPPC_NO_RECV))) {
3272 /* Drop this flow. */
3276 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3277 const struct ofp_action_dl_addr *oada;
3278 const struct nx_action_resubmit *nar;
3279 const struct nx_action_set_tunnel *nast;
3280 const struct nx_action_set_queue *nasq;
3281 const struct nx_action_multipath *nam;
3282 const struct nx_action_autopath *naa;
3283 const struct nx_action_bundle *nab;
3284 const struct nx_action_output_reg *naor;
3285 enum ofputil_action_code code;
3288 code = ofputil_decode_action_unsafe(ia);
3290 case OFPUTIL_OFPAT_OUTPUT:
3291 xlate_output_action(ctx, &ia->output);
3294 case OFPUTIL_OFPAT_SET_VLAN_VID:
3295 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3296 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3299 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3300 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3301 ctx->flow.vlan_tci |= htons(
3302 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3305 case OFPUTIL_OFPAT_STRIP_VLAN:
3306 ctx->flow.vlan_tci = htons(0);
3309 case OFPUTIL_OFPAT_SET_DL_SRC:
3310 oada = ((struct ofp_action_dl_addr *) ia);
3311 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3314 case OFPUTIL_OFPAT_SET_DL_DST:
3315 oada = ((struct ofp_action_dl_addr *) ia);
3316 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3319 case OFPUTIL_OFPAT_SET_NW_SRC:
3320 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3323 case OFPUTIL_OFPAT_SET_NW_DST:
3324 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3327 case OFPUTIL_OFPAT_SET_NW_TOS:
3328 ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK;
3331 case OFPUTIL_OFPAT_SET_TP_SRC:
3332 ctx->flow.tp_src = ia->tp_port.tp_port;
3335 case OFPUTIL_OFPAT_SET_TP_DST:
3336 ctx->flow.tp_dst = ia->tp_port.tp_port;
3339 case OFPUTIL_OFPAT_ENQUEUE:
3340 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3343 case OFPUTIL_NXAST_RESUBMIT:
3344 nar = (const struct nx_action_resubmit *) ia;
3345 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
3348 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3349 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
3352 case OFPUTIL_NXAST_SET_TUNNEL:
3353 nast = (const struct nx_action_set_tunnel *) ia;
3354 tun_id = htonll(ntohl(nast->tun_id));
3355 ctx->flow.tun_id = tun_id;
3358 case OFPUTIL_NXAST_SET_QUEUE:
3359 nasq = (const struct nx_action_set_queue *) ia;
3360 xlate_set_queue_action(ctx, nasq);
3363 case OFPUTIL_NXAST_POP_QUEUE:
3367 case OFPUTIL_NXAST_REG_MOVE:
3368 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3372 case OFPUTIL_NXAST_REG_LOAD:
3373 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3377 case OFPUTIL_NXAST_NOTE:
3378 /* Nothing to do. */
3381 case OFPUTIL_NXAST_SET_TUNNEL64:
3382 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3383 ctx->flow.tun_id = tun_id;
3386 case OFPUTIL_NXAST_MULTIPATH:
3387 nam = (const struct nx_action_multipath *) ia;
3388 multipath_execute(nam, &ctx->flow);
3391 case OFPUTIL_NXAST_AUTOPATH:
3392 naa = (const struct nx_action_autopath *) ia;
3393 xlate_autopath(ctx, naa);
3396 case OFPUTIL_NXAST_BUNDLE:
3397 ctx->ofproto->has_bundle_action = true;
3398 nab = (const struct nx_action_bundle *) ia;
3399 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3404 case OFPUTIL_NXAST_BUNDLE_LOAD:
3405 ctx->ofproto->has_bundle_action = true;
3406 nab = (const struct nx_action_bundle *) ia;
3407 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3411 case OFPUTIL_NXAST_OUTPUT_REG:
3412 naor = (const struct nx_action_output_reg *) ia;
3413 xlate_output_reg_action(ctx, naor);
3416 case OFPUTIL_NXAST_LEARN:
3417 ctx->has_learn = true;
3418 if (ctx->may_learn) {
3419 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
3427 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3428 struct ofproto_dpif *ofproto, const struct flow *flow,
3429 const struct ofpbuf *packet)
3431 ctx->ofproto = ofproto;
3433 ctx->packet = packet;
3434 ctx->may_learn = packet != NULL;
3435 ctx->resubmit_hook = NULL;
3438 static struct ofpbuf *
3439 xlate_actions(struct action_xlate_ctx *ctx,
3440 const union ofp_action *in, size_t n_in)
3442 COVERAGE_INC(ofproto_dpif_xlate);
3444 ctx->odp_actions = ofpbuf_new(512);
3446 ctx->may_set_up_flow = true;
3447 ctx->has_learn = false;
3448 ctx->has_normal = false;
3449 ctx->nf_output_iface = NF_OUT_DROP;
3452 ctx->base_priority = 0;
3453 ctx->base_flow = ctx->flow;
3454 ctx->base_flow.tun_id = 0;
3457 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3458 ctx->may_set_up_flow = false;
3460 do_xlate_actions(in, n_in, ctx);
3463 /* Check with in-band control to see if we're allowed to set up this
3465 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3466 ctx->odp_actions->data,
3467 ctx->odp_actions->size)) {
3468 ctx->may_set_up_flow = false;
3471 return ctx->odp_actions;
3474 /* OFPP_NORMAL implementation. */
3477 struct ofport_dpif *port;
3482 struct dst builtin[32];
3484 size_t n, allocated;
3487 static void dst_set_init(struct dst_set *);
3488 static void dst_set_add(struct dst_set *, const struct dst *);
3489 static void dst_set_free(struct dst_set *);
3491 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3494 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3495 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3497 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3498 : in_bundle->vlan >= 0 ? in_bundle->vlan
3499 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3500 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3502 dst->port = (!out_bundle->bond
3503 ? ofbundle_get_a_port(out_bundle)
3504 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3505 dst->vlan, &ctx->tags));
3507 return dst->port != NULL;
3511 mirror_mask_ffs(mirror_mask_t mask)
3513 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3518 dst_set_init(struct dst_set *set)
3520 set->dsts = set->builtin;
3522 set->allocated = ARRAY_SIZE(set->builtin);
3526 dst_set_add(struct dst_set *set, const struct dst *dst)
3528 if (set->n >= set->allocated) {
3529 size_t new_allocated;
3530 struct dst *new_dsts;
3532 new_allocated = set->allocated * 2;
3533 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3534 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3538 set->dsts = new_dsts;
3539 set->allocated = new_allocated;
3541 set->dsts[set->n++] = *dst;
3545 dst_set_free(struct dst_set *set)
3547 if (set->dsts != set->builtin) {
3553 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3556 for (i = 0; i < set->n; i++) {
3557 if (set->dsts[i].vlan == test->vlan
3558 && set->dsts[i].port == test->port) {
3566 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3568 return (bundle->vlan < 0
3569 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
3573 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3575 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3578 /* Returns an arbitrary interface within 'bundle'. */
3579 static struct ofport_dpif *
3580 ofbundle_get_a_port(const struct ofbundle *bundle)
3582 return CONTAINER_OF(list_front(&bundle->ports),
3583 struct ofport_dpif, bundle_node);
3587 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3588 const struct ofbundle *in_bundle,
3589 const struct ofbundle *out_bundle, struct dst_set *set)
3593 if (out_bundle == OFBUNDLE_FLOOD) {
3594 struct ofbundle *bundle;
3596 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3597 if (bundle != in_bundle
3598 && ofbundle_includes_vlan(bundle, vlan)
3599 && bundle->floodable
3600 && !bundle->mirror_out
3601 && set_dst(ctx, &dst, in_bundle, bundle)) {
3602 dst_set_add(set, &dst);
3605 ctx->nf_output_iface = NF_OUT_FLOOD;
3606 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3607 dst_set_add(set, &dst);
3608 ctx->nf_output_iface = dst.port->odp_port;
3613 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3615 return !m->vlans || bitmap_is_set(m->vlans, vlan);
3618 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3619 * to a VLAN. In general most packets may be mirrored but we want to drop
3620 * protocols that may confuse switches. */
3622 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
3624 /* If you change this function's behavior, please update corresponding
3625 * documentation in vswitch.xml at the same time. */
3626 if (dst[0] != 0x01) {
3627 /* All the currently banned MACs happen to start with 01 currently, so
3628 * this is a quick way to eliminate most of the good ones. */
3630 if (eth_addr_is_reserved(dst)) {
3631 /* Drop STP, IEEE pause frames, and other reserved protocols
3632 * (01-80-c2-00-00-0x). */
3636 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
3638 if ((dst[3] & 0xfe) == 0xcc &&
3639 (dst[4] & 0xfe) == 0xcc &&
3640 (dst[5] & 0xfe) == 0xcc) {
3641 /* Drop the following protocols plus others following the same
3644 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3645 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3646 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3650 if (!(dst[3] | dst[4] | dst[5])) {
3651 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3660 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3661 uint16_t vlan, const struct ofbundle *in_bundle,
3662 struct dst_set *set)
3664 struct ofproto_dpif *ofproto = ctx->ofproto;
3665 mirror_mask_t mirrors;
3669 mirrors = in_bundle->src_mirrors;
3670 for (i = 0; i < set->n; i++) {
3671 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3678 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3679 if (flow_vlan == 0) {
3680 flow_vlan = OFP_VLAN_NONE;
3684 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3685 if (vlan_is_mirrored(m, vlan)) {
3689 if (set_dst(ctx, &dst, in_bundle, m->out)
3690 && !dst_is_duplicate(set, &dst)) {
3691 dst_set_add(set, &dst);
3693 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
3694 struct ofbundle *bundle;
3696 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3697 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3698 && set_dst(ctx, &dst, in_bundle, bundle))
3700 if (bundle->vlan < 0) {
3701 dst.vlan = m->out_vlan;
3703 if (dst_is_duplicate(set, &dst)) {
3707 /* Use the vlan tag on the original flow instead of
3708 * the one passed in the vlan parameter. This ensures
3709 * that we compare the vlan from before any implicit
3710 * tagging tags place. This is necessary because
3711 * dst->vlan is the final vlan, after removing implicit
3713 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3714 /* Don't send out input port on same VLAN. */
3717 dst_set_add(set, &dst);
3722 mirrors &= mirrors - 1;
3727 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3728 const struct ofbundle *in_bundle,
3729 const struct ofbundle *out_bundle)
3731 uint16_t initial_vlan, cur_vlan;
3732 const struct dst *dst;
3736 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3737 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3739 /* Output all the packets we can without having to change the VLAN. */
3740 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3741 if (initial_vlan == 0) {
3742 initial_vlan = OFP_VLAN_NONE;
3744 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3745 if (dst->vlan != initial_vlan) {
3748 nl_msg_put_u32(ctx->odp_actions,
3749 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3752 /* Then output the rest. */
3753 cur_vlan = initial_vlan;
3754 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3755 if (dst->vlan == initial_vlan) {
3758 if (dst->vlan != cur_vlan) {
3759 if (dst->vlan == OFP_VLAN_NONE) {
3760 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3764 if (cur_vlan != OFP_VLAN_NONE) {
3765 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3767 tci = htons(dst->vlan & VLAN_VID_MASK);
3768 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3769 nl_msg_put_be16(ctx->odp_actions,
3770 OVS_ACTION_ATTR_PUSH_VLAN, tci);
3772 cur_vlan = dst->vlan;
3774 nl_msg_put_u32(ctx->odp_actions,
3775 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3781 /* Returns the effective vlan of a packet, taking into account both the
3782 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3783 * the packet is untagged and -1 indicates it has an invalid header and
3784 * should be dropped. */
3786 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3787 struct ofbundle *in_bundle, bool have_packet)
3789 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3790 if (in_bundle->vlan >= 0) {
3793 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3794 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3795 "packet received on port %s configured with "
3796 "implicit VLAN %"PRIu16,
3797 ofproto->up.name, vlan,
3798 in_bundle->name, in_bundle->vlan);
3802 vlan = in_bundle->vlan;
3804 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3806 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3807 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3808 "packet received on port %s not configured for "
3810 ofproto->up.name, vlan, in_bundle->name, vlan);
3819 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3820 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3821 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3823 is_gratuitous_arp(const struct flow *flow)
3825 return (flow->dl_type == htons(ETH_TYPE_ARP)
3826 && eth_addr_is_broadcast(flow->dl_dst)
3827 && (flow->nw_proto == ARP_OP_REPLY
3828 || (flow->nw_proto == ARP_OP_REQUEST
3829 && flow->nw_src == flow->nw_dst)));
3833 update_learning_table(struct ofproto_dpif *ofproto,
3834 const struct flow *flow, int vlan,
3835 struct ofbundle *in_bundle)
3837 struct mac_entry *mac;
3839 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3843 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3844 if (is_gratuitous_arp(flow)) {
3845 /* We don't want to learn from gratuitous ARP packets that are
3846 * reflected back over bond slaves so we lock the learning table. */
3847 if (!in_bundle->bond) {
3848 mac_entry_set_grat_arp_lock(mac);
3849 } else if (mac_entry_is_grat_arp_locked(mac)) {
3854 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3855 /* The log messages here could actually be useful in debugging,
3856 * so keep the rate limit relatively high. */
3857 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3858 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3859 "on port %s in VLAN %d",
3860 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3861 in_bundle->name, vlan);
3863 mac->port.p = in_bundle;
3864 tag_set_add(&ofproto->revalidate_set,
3865 mac_learning_changed(ofproto->ml, mac));
3869 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3870 * dropped. Returns true if they may be forwarded, false if they should be
3873 * If 'have_packet' is true, it indicates that the caller is processing a
3874 * received packet. If 'have_packet' is false, then the caller is just
3875 * revalidating an existing flow because configuration has changed. Either
3876 * way, 'have_packet' only affects logging (there is no point in logging errors
3877 * during revalidation).
3879 * Sets '*in_portp' to the input port. This will be a null pointer if
3880 * flow->in_port does not designate a known input port (in which case
3881 * is_admissible() returns false).
3883 * When returning true, sets '*vlanp' to the effective VLAN of the input
3884 * packet, as returned by flow_get_vlan().
3886 * May also add tags to '*tags', although the current implementation only does
3887 * so in one special case.
3890 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3892 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3894 struct ofport_dpif *in_port;
3895 struct ofbundle *in_bundle;
3898 /* Find the port and bundle for the received packet. */
3899 in_port = get_ofp_port(ofproto, flow->in_port);
3900 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3901 if (!in_port || !in_bundle) {
3902 /* No interface? Something fishy... */
3904 /* Odd. A few possible reasons here:
3906 * - We deleted a port but there are still a few packets queued up
3909 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3910 * we don't know about.
3912 * - Packet arrived on the local port but the local port is not
3915 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3917 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3919 ofproto->up.name, flow->in_port);
3924 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3929 /* Drop frames for reserved multicast addresses
3930 * only if forward_bpdu option is absent. */
3931 if (eth_addr_is_reserved(flow->dl_dst) &&
3932 !ofproto->up.forward_bpdu) {
3936 /* Drop frames on bundles reserved for mirroring. */
3937 if (in_bundle->mirror_out) {
3939 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3940 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3941 "%s, which is reserved exclusively for mirroring",
3942 ofproto->up.name, in_bundle->name);
3947 if (in_bundle->bond) {
3948 struct mac_entry *mac;
3950 switch (bond_check_admissibility(in_bundle->bond, in_port,
3951 flow->dl_dst, tags)) {
3958 case BV_DROP_IF_MOVED:
3959 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3960 if (mac && mac->port.p != in_bundle &&
3961 (!is_gratuitous_arp(flow)
3962 || mac_entry_is_grat_arp_locked(mac))) {
3973 xlate_normal(struct action_xlate_ctx *ctx)
3975 struct ofbundle *in_bundle;
3976 struct ofbundle *out_bundle;
3977 struct mac_entry *mac;
3980 ctx->has_normal = true;
3982 /* Check whether we should drop packets in this flow. */
3983 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3984 &ctx->tags, &vlan, &in_bundle)) {
3989 /* Learn source MAC. */
3990 if (ctx->may_learn) {
3991 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3994 /* Determine output bundle. */
3995 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3998 out_bundle = mac->port.p;
3999 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4000 /* If we are revalidating but don't have a learning entry then eject
4001 * the flow. Installing a flow that floods packets opens up a window
4002 * of time where we could learn from a packet reflected on a bond and
4003 * blackhole packets before the learning table is updated to reflect
4004 * the correct port. */
4005 ctx->may_set_up_flow = false;
4008 out_bundle = OFBUNDLE_FLOOD;
4011 /* Don't send packets out their input bundles. */
4012 if (in_bundle == out_bundle) {
4018 compose_actions(ctx, vlan, in_bundle, out_bundle);
4022 /* Optimized flow revalidation.
4024 * It's a difficult problem, in general, to tell which facets need to have
4025 * their actions recalculated whenever the OpenFlow flow table changes. We
4026 * don't try to solve that general problem: for most kinds of OpenFlow flow
4027 * table changes, we recalculate the actions for every facet. This is
4028 * relatively expensive, but it's good enough if the OpenFlow flow table
4029 * doesn't change very often.
4031 * However, we can expect one particular kind of OpenFlow flow table change to
4032 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4033 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4034 * table, we add a special case that applies to flow tables in which every rule
4035 * has the same form (that is, the same wildcards), except that the table is
4036 * also allowed to have a single "catch-all" flow that matches all packets. We
4037 * optimize this case by tagging all of the facets that resubmit into the table
4038 * and invalidating the same tag whenever a flow changes in that table. The
4039 * end result is that we revalidate just the facets that need it (and sometimes
4040 * a few more, but not all of the facets or even all of the facets that
4041 * resubmit to the table modified by MAC learning). */
4043 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
4044 * into an OpenFlow table with the given 'basis'. */
4046 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
4049 if (flow_wildcards_is_catchall(wc)) {
4052 struct flow tag_flow = *flow;
4053 flow_zero_wildcards(&tag_flow, wc);
4054 return tag_create_deterministic(flow_hash(&tag_flow, secret));
4058 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
4059 * taggability of that table.
4061 * This function must be called after *each* change to a flow table. If you
4062 * skip calling it on some changes then the pointer comparisons at the end can
4063 * be invalid if you get unlucky. For example, if a flow removal causes a
4064 * cls_table to be destroyed and then a flow insertion causes a cls_table with
4065 * different wildcards to be created with the same address, then this function
4066 * will incorrectly skip revalidation. */
4068 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
4070 struct table_dpif *table = &ofproto->tables[table_id];
4071 const struct classifier *cls = &ofproto->up.tables[table_id];
4072 struct cls_table *catchall, *other;
4073 struct cls_table *t;
4075 catchall = other = NULL;
4077 switch (hmap_count(&cls->tables)) {
4079 /* We could tag this OpenFlow table but it would make the logic a
4080 * little harder and it's a corner case that doesn't seem worth it
4086 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
4087 if (cls_table_is_catchall(t)) {
4089 } else if (!other) {
4092 /* Indicate that we can't tag this by setting both tables to
4093 * NULL. (We know that 'catchall' is already NULL.) */
4100 /* Can't tag this table. */
4104 if (table->catchall_table != catchall || table->other_table != other) {
4105 table->catchall_table = catchall;
4106 table->other_table = other;
4107 ofproto->need_revalidate = true;
4111 /* Given 'rule' that has changed in some way (either it is a rule being
4112 * inserted, a rule being deleted, or a rule whose actions are being
4113 * modified), marks facets for revalidation to ensure that packets will be
4114 * forwarded correctly according to the new state of the flow table.
4116 * This function must be called after *each* change to a flow table. See
4117 * the comment on table_update_taggable() for more information. */
4119 rule_invalidate(const struct rule_dpif *rule)
4121 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4123 table_update_taggable(ofproto, rule->up.table_id);
4125 if (!ofproto->need_revalidate) {
4126 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
4128 if (table->other_table && rule->tag) {
4129 tag_set_add(&ofproto->revalidate_set, rule->tag);
4131 ofproto->need_revalidate = true;
4137 get_drop_frags(struct ofproto *ofproto_)
4139 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4142 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
4147 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
4149 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4151 dpif_set_drop_frags(ofproto->dpif, drop_frags);
4155 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
4156 const struct flow *flow,
4157 const union ofp_action *ofp_actions, size_t n_ofp_actions)
4159 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4162 error = validate_actions(ofp_actions, n_ofp_actions, flow,
4163 ofproto->max_ports);
4165 struct odputil_keybuf keybuf;
4166 struct action_xlate_ctx ctx;
4167 struct ofpbuf *odp_actions;
4170 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4171 odp_flow_key_from_flow(&key, flow);
4173 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
4174 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
4175 dpif_execute(ofproto->dpif, key.data, key.size,
4176 odp_actions->data, odp_actions->size, packet);
4177 ofpbuf_delete(odp_actions);
4183 get_netflow_ids(const struct ofproto *ofproto_,
4184 uint8_t *engine_type, uint8_t *engine_id)
4186 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4188 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
4191 static struct ofproto_dpif *
4192 ofproto_dpif_lookup(const char *name)
4194 struct ofproto *ofproto = ofproto_lookup(name);
4195 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
4196 ? ofproto_dpif_cast(ofproto)
4201 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
4202 const char *args, void *aux OVS_UNUSED)
4204 struct ds ds = DS_EMPTY_INITIALIZER;
4205 const struct ofproto_dpif *ofproto;
4206 const struct mac_entry *e;
4208 ofproto = ofproto_dpif_lookup(args);
4210 unixctl_command_reply(conn, 501, "no such bridge");
4214 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4215 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4216 struct ofbundle *bundle = e->port.p;
4217 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
4218 ofbundle_get_a_port(bundle)->odp_port,
4219 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
4221 unixctl_command_reply(conn, 200, ds_cstr(&ds));
4225 struct ofproto_trace {
4226 struct action_xlate_ctx ctx;
4232 trace_format_rule(struct ds *result, uint8_t table_id, int level,
4233 const struct rule_dpif *rule)
4235 ds_put_char_multiple(result, '\t', level);
4237 ds_put_cstr(result, "No match\n");
4241 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4242 table_id, ntohll(rule->up.flow_cookie));
4243 cls_rule_format(&rule->up.cr, result);
4244 ds_put_char(result, '\n');
4246 ds_put_char_multiple(result, '\t', level);
4247 ds_put_cstr(result, "OpenFlow ");
4248 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
4249 ds_put_char(result, '\n');
4253 trace_format_flow(struct ds *result, int level, const char *title,
4254 struct ofproto_trace *trace)
4256 ds_put_char_multiple(result, '\t', level);
4257 ds_put_format(result, "%s: ", title);
4258 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4259 ds_put_cstr(result, "unchanged");
4261 flow_format(result, &trace->ctx.flow);
4262 trace->flow = trace->ctx.flow;
4264 ds_put_char(result, '\n');
4268 trace_format_regs(struct ds *result, int level, const char *title,
4269 struct ofproto_trace *trace)
4273 ds_put_char_multiple(result, '\t', level);
4274 ds_put_format(result, "%s:", title);
4275 for (i = 0; i < FLOW_N_REGS; i++) {
4276 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
4278 ds_put_char(result, '\n');
4282 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
4284 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4285 struct ds *result = trace->result;
4287 ds_put_char(result, '\n');
4288 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4289 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
4290 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
4294 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4295 void *aux OVS_UNUSED)
4297 char *dpname, *arg1, *arg2, *arg3;
4298 char *args = xstrdup(args_);
4299 char *save_ptr = NULL;
4300 struct ofproto_dpif *ofproto;
4301 struct ofpbuf odp_key;
4302 struct ofpbuf *packet;
4303 struct rule_dpif *rule;
4309 ofpbuf_init(&odp_key, 0);
4312 dpname = strtok_r(args, " ", &save_ptr);
4313 arg1 = strtok_r(NULL, " ", &save_ptr);
4314 arg2 = strtok_r(NULL, " ", &save_ptr);
4315 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4316 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
4317 /* ofproto/trace dpname flow [-generate] */
4320 /* Convert string to datapath key. */
4321 ofpbuf_init(&odp_key, 0);
4322 error = odp_flow_key_from_string(arg1, &odp_key);
4324 unixctl_command_reply(conn, 501, "Bad flow syntax");
4328 /* Convert odp_key to flow. */
4329 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
4331 unixctl_command_reply(conn, 501, "Invalid flow");
4335 /* Generate a packet, if requested. */
4337 packet = ofpbuf_new(0);
4338 flow_compose(packet, &flow);
4340 } else if (dpname && arg1 && arg2 && arg3) {
4341 /* ofproto/trace dpname tun_id in_port packet */
4345 tun_id = htonll(strtoull(arg1, NULL, 0));
4346 in_port = ofp_port_to_odp_port(atoi(arg2));
4348 packet = ofpbuf_new(strlen(args) / 2);
4349 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
4350 arg3 += strspn(arg3, " ");
4351 if (*arg3 != '\0') {
4352 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4355 if (packet->size < ETH_HEADER_LEN) {
4356 unixctl_command_reply(conn, 501,
4357 "Packet data too short for Ethernet");
4361 ds_put_cstr(&result, "Packet: ");
4362 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
4363 ds_put_cstr(&result, s);
4366 flow_extract(packet, tun_id, in_port, &flow);
4368 unixctl_command_reply(conn, 501, "Bad command syntax");
4372 ofproto = ofproto_dpif_lookup(dpname);
4374 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4379 ds_put_cstr(&result, "Flow: ");
4380 flow_format(&result, &flow);
4381 ds_put_char(&result, '\n');
4383 rule = rule_dpif_lookup(ofproto, &flow, 0);
4384 trace_format_rule(&result, 0, 0, rule);
4386 struct ofproto_trace trace;
4387 struct ofpbuf *odp_actions;
4389 trace.result = &result;
4391 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
4392 trace.ctx.resubmit_hook = trace_resubmit;
4393 odp_actions = xlate_actions(&trace.ctx,
4394 rule->up.actions, rule->up.n_actions);
4396 ds_put_char(&result, '\n');
4397 trace_format_flow(&result, 0, "Final flow", &trace);
4398 ds_put_cstr(&result, "Datapath actions: ");
4399 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4400 ofpbuf_delete(odp_actions);
4402 if (!trace.ctx.may_set_up_flow) {
4404 ds_put_cstr(&result, "\nThis flow is not cachable.");
4406 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
4407 "for complete actions, please supply a packet.");
4412 unixctl_command_reply(conn, 200, ds_cstr(&result));
4415 ds_destroy(&result);
4416 ofpbuf_delete(packet);
4417 ofpbuf_uninit(&odp_key);
4422 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
4423 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4426 unixctl_command_reply(conn, 200, NULL);
4430 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
4431 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4434 unixctl_command_reply(conn, 200, NULL);
4438 ofproto_dpif_unixctl_init(void)
4440 static bool registered;
4446 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4447 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
4449 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
4450 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
4453 const struct ofproto_class ofproto_dpif_class = {
4480 port_is_lacp_current,
4481 NULL, /* rule_choose_table */
4488 rule_modify_actions,
4497 get_cfm_remote_mpids,
4502 is_mirror_output_bundle,
4503 forward_bpdu_changed,