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 32
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 enum port_vlan_mode vlan_mode; /* VLAN mode */
142 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
143 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
144 * NULL if all VLANs are trunked. */
145 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
146 struct bond *bond; /* Nonnull iff more than one port. */
149 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
151 /* Port mirroring info. */
152 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
153 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
154 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
157 static void bundle_remove(struct ofport *);
158 static void bundle_update(struct ofbundle *);
159 static void bundle_destroy(struct ofbundle *);
160 static void bundle_del_port(struct ofport_dpif *);
161 static void bundle_run(struct ofbundle *);
162 static void bundle_wait(struct ofbundle *);
164 static void stp_run(struct ofproto_dpif *ofproto);
165 static void stp_wait(struct ofproto_dpif *ofproto);
167 struct action_xlate_ctx {
168 /* action_xlate_ctx_init() initializes these members. */
171 struct ofproto_dpif *ofproto;
173 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
174 * this flow when actions change header fields. */
177 /* The packet corresponding to 'flow', or a null pointer if we are
178 * revalidating without a packet to refer to. */
179 const struct ofpbuf *packet;
181 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
182 * want to execute them if we are actually processing a packet, or if we
183 * are accounting for packets that the datapath has processed, but not if
184 * we are just revalidating. */
187 /* If nonnull, called just before executing a resubmit action.
189 * This is normally null so the client has to set it manually after
190 * calling action_xlate_ctx_init(). */
191 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
193 /* xlate_actions() initializes and uses these members. The client might want
194 * to look at them after it returns. */
196 struct ofpbuf *odp_actions; /* Datapath actions. */
197 tag_type tags; /* Tags associated with actions. */
198 bool may_set_up_flow; /* True ordinarily; false if the actions must
199 * be reassessed for every packet. */
200 bool has_learn; /* Actions include NXAST_LEARN? */
201 bool has_normal; /* Actions output to OFPP_NORMAL? */
202 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
204 /* xlate_actions() initializes and uses these members, but the client has no
205 * reason to look at them. */
207 int recurse; /* Recursion level, via xlate_table_action. */
208 uint32_t priority; /* Current flow priority. 0 if none. */
209 struct flow base_flow; /* Flow at the last commit. */
210 uint32_t base_priority; /* Priority at the last commit. */
211 uint8_t table_id; /* OpenFlow table ID where flow was found. */
212 uint32_t sflow_n_outputs; /* Number of output ports. */
213 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
214 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
217 static void action_xlate_ctx_init(struct action_xlate_ctx *,
218 struct ofproto_dpif *, const struct flow *,
219 const struct ofpbuf *);
220 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
221 const union ofp_action *in, size_t n_in);
223 /* An exact-match instantiation of an OpenFlow flow. */
225 long long int used; /* Time last used; time created if not used. */
229 * - Do include packets and bytes sent "by hand", e.g. with
232 * - Do include packets and bytes that were obtained from the datapath
233 * when its statistics were reset (e.g. dpif_flow_put() with
234 * DPIF_FP_ZERO_STATS).
236 uint64_t packet_count; /* Number of packets received. */
237 uint64_t byte_count; /* Number of bytes received. */
239 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
240 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
242 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
243 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
244 long long int rs_used; /* Used time pushed to resubmit children. */
246 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
248 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
249 struct list list_node; /* In owning rule's 'facets' list. */
250 struct rule_dpif *rule; /* Owning rule. */
251 struct flow flow; /* Exact-match flow. */
252 bool installed; /* Installed in datapath? */
253 bool may_install; /* True ordinarily; false if actions must
254 * be reassessed for every packet. */
255 bool has_learn; /* Actions include NXAST_LEARN? */
256 bool has_normal; /* Actions output to OFPP_NORMAL? */
257 size_t actions_len; /* Number of bytes in actions[]. */
258 struct nlattr *actions; /* Datapath actions. */
259 tag_type tags; /* Tags. */
260 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
263 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
264 static void facet_remove(struct ofproto_dpif *, struct facet *);
265 static void facet_free(struct facet *);
267 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
268 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
269 const struct flow *);
270 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
272 static bool execute_controller_action(struct ofproto_dpif *,
274 const struct nlattr *odp_actions,
276 struct ofpbuf *packet, bool clone);
277 static void facet_execute(struct ofproto_dpif *, struct facet *,
278 struct ofpbuf *packet);
280 static int facet_put__(struct ofproto_dpif *, struct facet *,
281 const struct nlattr *actions, size_t actions_len,
282 struct dpif_flow_stats *);
283 static void facet_install(struct ofproto_dpif *, struct facet *,
285 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
286 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
288 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
289 const struct ofpbuf *packet);
290 static void facet_update_time(struct ofproto_dpif *, struct facet *,
292 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
293 const struct dpif_flow_stats *);
294 static void facet_reset_counters(struct facet *);
295 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
296 static void facet_push_stats(struct facet *);
297 static void facet_account(struct ofproto_dpif *, struct facet *);
299 static bool facet_is_controller_flow(struct facet *);
301 static void flow_push_stats(const struct rule_dpif *,
302 struct flow *, uint64_t packets, uint64_t bytes,
305 static uint32_t rule_calculate_tag(const struct flow *,
306 const struct flow_wildcards *,
308 static void rule_invalidate(const struct rule_dpif *);
314 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
315 struct list bundle_node; /* In struct ofbundle's "ports" list. */
316 struct cfm *cfm; /* Connectivity Fault Management, if any. */
317 tag_type tag; /* Tag associated with this port. */
318 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
319 bool may_enable; /* May be enabled in bonds. */
321 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
322 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
323 long long int stp_state_entered;
326 static struct ofport_dpif *
327 ofport_dpif_cast(const struct ofport *ofport)
329 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
330 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
333 static void port_run(struct ofport_dpif *);
334 static void port_wait(struct ofport_dpif *);
335 static int set_cfm(struct ofport *, const struct cfm_settings *);
337 struct dpif_completion {
338 struct list list_node;
339 struct ofoperation *op;
342 /* Extra information about a classifier table.
343 * Currently used just for optimized flow revalidation. */
345 /* If either of these is nonnull, then this table has a form that allows
346 * flows to be tagged to avoid revalidating most flows for the most common
347 * kinds of flow table changes. */
348 struct cls_table *catchall_table; /* Table that wildcards all fields. */
349 struct cls_table *other_table; /* Table with any other wildcard set. */
350 uint32_t basis; /* Keeps each table's tags separate. */
353 struct ofproto_dpif {
362 struct netflow *netflow;
363 struct dpif_sflow *sflow;
364 struct hmap bundles; /* Contains "struct ofbundle"s. */
365 struct mac_learning *ml;
366 struct ofmirror *mirrors[MAX_MIRRORS];
367 bool has_bonded_bundles;
370 struct timer next_expiration;
376 struct table_dpif tables[N_TABLES];
377 bool need_revalidate;
378 struct tag_set revalidate_set;
380 /* Support for debugging async flow mods. */
381 struct list completions;
383 bool has_bundle_action; /* True when the first bundle action appears. */
387 long long int stp_last_tick;
390 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
391 * for debugging the asynchronous flow_mod implementation.) */
394 static void ofproto_dpif_unixctl_init(void);
396 static struct ofproto_dpif *
397 ofproto_dpif_cast(const struct ofproto *ofproto)
399 assert(ofproto->ofproto_class == &ofproto_dpif_class);
400 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
403 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
405 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
408 /* Packet processing. */
409 static void update_learning_table(struct ofproto_dpif *,
410 const struct flow *, int vlan,
412 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
413 bool have_packet, tag_type *, int *vlanp,
414 struct ofbundle **in_bundlep);
417 #define FLOW_MISS_MAX_BATCH 50
418 static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
420 /* Flow expiration. */
421 static int expire(struct ofproto_dpif *);
424 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
425 const struct ofpbuf *packet);
427 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
428 const struct flow *, uint32_t odp_port);
429 /* Global variables. */
430 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
432 /* Factory functions. */
435 enumerate_types(struct sset *types)
437 dp_enumerate_types(types);
441 enumerate_names(const char *type, struct sset *names)
443 return dp_enumerate_names(type, names);
447 del(const char *type, const char *name)
452 error = dpif_open(name, type, &dpif);
454 error = dpif_delete(dpif);
460 /* Basic life-cycle. */
462 static struct ofproto *
465 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
470 dealloc(struct ofproto *ofproto_)
472 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
477 construct(struct ofproto *ofproto_, int *n_tablesp)
479 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
480 const char *name = ofproto->up.name;
484 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
486 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
490 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
491 ofproto->n_matches = 0;
493 dpif_flow_flush(ofproto->dpif);
494 dpif_recv_purge(ofproto->dpif);
496 error = dpif_recv_set_mask(ofproto->dpif,
497 ((1u << DPIF_UC_MISS) |
498 (1u << DPIF_UC_ACTION)));
500 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
501 dpif_close(ofproto->dpif);
505 ofproto->netflow = NULL;
506 ofproto->sflow = NULL;
508 hmap_init(&ofproto->bundles);
509 ofproto->ml = mac_learning_create();
510 for (i = 0; i < MAX_MIRRORS; i++) {
511 ofproto->mirrors[i] = NULL;
513 ofproto->has_bonded_bundles = false;
515 timer_set_duration(&ofproto->next_expiration, 1000);
517 hmap_init(&ofproto->facets);
519 for (i = 0; i < N_TABLES; i++) {
520 struct table_dpif *table = &ofproto->tables[i];
522 table->catchall_table = NULL;
523 table->other_table = NULL;
524 table->basis = random_uint32();
526 ofproto->need_revalidate = false;
527 tag_set_init(&ofproto->revalidate_set);
529 list_init(&ofproto->completions);
531 ofproto_dpif_unixctl_init();
533 ofproto->has_bundle_action = false;
535 *n_tablesp = N_TABLES;
540 complete_operations(struct ofproto_dpif *ofproto)
542 struct dpif_completion *c, *next;
544 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
545 ofoperation_complete(c->op, 0);
546 list_remove(&c->list_node);
552 destruct(struct ofproto *ofproto_)
554 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
555 struct rule_dpif *rule, *next_rule;
556 struct classifier *table;
559 complete_operations(ofproto);
561 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
562 struct cls_cursor cursor;
564 cls_cursor_init(&cursor, table, NULL);
565 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
566 ofproto_rule_destroy(&rule->up);
570 for (i = 0; i < MAX_MIRRORS; i++) {
571 mirror_destroy(ofproto->mirrors[i]);
574 netflow_destroy(ofproto->netflow);
575 dpif_sflow_destroy(ofproto->sflow);
576 hmap_destroy(&ofproto->bundles);
577 mac_learning_destroy(ofproto->ml);
579 hmap_destroy(&ofproto->facets);
581 dpif_close(ofproto->dpif);
585 run_fast(struct ofproto *ofproto_)
587 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
590 /* Handle one or more batches of upcalls, until there's nothing left to do
591 * or until we do a fixed total amount of work.
593 * We do work in batches because it can be much cheaper to set up a number
594 * of flows and fire off their patches all at once. We do multiple batches
595 * because in some cases handling a packet can cause another packet to be
596 * queued almost immediately as part of the return flow. Both
597 * optimizations can make major improvements on some benchmarks and
598 * presumably for real traffic as well. */
600 while (work < FLOW_MISS_MAX_BATCH) {
601 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
611 run(struct ofproto *ofproto_)
613 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
614 struct ofport_dpif *ofport;
615 struct ofbundle *bundle;
619 complete_operations(ofproto);
621 dpif_run(ofproto->dpif);
623 error = run_fast(ofproto_);
628 if (timer_expired(&ofproto->next_expiration)) {
629 int delay = expire(ofproto);
630 timer_set_duration(&ofproto->next_expiration, delay);
633 if (ofproto->netflow) {
634 netflow_run(ofproto->netflow);
636 if (ofproto->sflow) {
637 dpif_sflow_run(ofproto->sflow);
640 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
643 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
648 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
650 /* Now revalidate if there's anything to do. */
651 if (ofproto->need_revalidate
652 || !tag_set_is_empty(&ofproto->revalidate_set)) {
653 struct tag_set revalidate_set = ofproto->revalidate_set;
654 bool revalidate_all = ofproto->need_revalidate;
655 struct facet *facet, *next;
657 /* Clear the revalidation flags. */
658 tag_set_init(&ofproto->revalidate_set);
659 ofproto->need_revalidate = false;
661 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
663 || tag_set_intersects(&revalidate_set, facet->tags)) {
664 facet_revalidate(ofproto, facet);
673 wait(struct ofproto *ofproto_)
675 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
676 struct ofport_dpif *ofport;
677 struct ofbundle *bundle;
679 if (!clogged && !list_is_empty(&ofproto->completions)) {
680 poll_immediate_wake();
683 dpif_wait(ofproto->dpif);
684 dpif_recv_wait(ofproto->dpif);
685 if (ofproto->sflow) {
686 dpif_sflow_wait(ofproto->sflow);
688 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
689 poll_immediate_wake();
691 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
694 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
697 mac_learning_wait(ofproto->ml);
699 if (ofproto->need_revalidate) {
700 /* Shouldn't happen, but if it does just go around again. */
701 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
702 poll_immediate_wake();
704 timer_wait(&ofproto->next_expiration);
709 flush(struct ofproto *ofproto_)
711 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
712 struct facet *facet, *next_facet;
714 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
715 /* Mark the facet as not installed so that facet_remove() doesn't
716 * bother trying to uninstall it. There is no point in uninstalling it
717 * individually since we are about to blow away all the facets with
718 * dpif_flow_flush(). */
719 facet->installed = false;
720 facet->dp_packet_count = 0;
721 facet->dp_byte_count = 0;
722 facet_remove(ofproto, facet);
724 dpif_flow_flush(ofproto->dpif);
728 get_features(struct ofproto *ofproto_ OVS_UNUSED,
729 bool *arp_match_ip, uint32_t *actions)
731 *arp_match_ip = true;
732 *actions = ((1u << OFPAT_OUTPUT) |
733 (1u << OFPAT_SET_VLAN_VID) |
734 (1u << OFPAT_SET_VLAN_PCP) |
735 (1u << OFPAT_STRIP_VLAN) |
736 (1u << OFPAT_SET_DL_SRC) |
737 (1u << OFPAT_SET_DL_DST) |
738 (1u << OFPAT_SET_NW_SRC) |
739 (1u << OFPAT_SET_NW_DST) |
740 (1u << OFPAT_SET_NW_TOS) |
741 (1u << OFPAT_SET_TP_SRC) |
742 (1u << OFPAT_SET_TP_DST) |
743 (1u << OFPAT_ENQUEUE));
747 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
749 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
750 struct dpif_dp_stats s;
752 strcpy(ots->name, "classifier");
754 dpif_get_dp_stats(ofproto->dpif, &s);
755 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
756 put_32aligned_be64(&ots->matched_count,
757 htonll(s.n_hit + ofproto->n_matches));
761 set_netflow(struct ofproto *ofproto_,
762 const struct netflow_options *netflow_options)
764 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
766 if (netflow_options) {
767 if (!ofproto->netflow) {
768 ofproto->netflow = netflow_create();
770 return netflow_set_options(ofproto->netflow, netflow_options);
772 netflow_destroy(ofproto->netflow);
773 ofproto->netflow = NULL;
778 static struct ofport *
781 struct ofport_dpif *port = xmalloc(sizeof *port);
786 port_dealloc(struct ofport *port_)
788 struct ofport_dpif *port = ofport_dpif_cast(port_);
793 port_construct(struct ofport *port_)
795 struct ofport_dpif *port = ofport_dpif_cast(port_);
796 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
798 ofproto->need_revalidate = true;
799 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
802 port->tag = tag_create_random();
803 port->may_enable = true;
804 port->stp_port = NULL;
805 port->stp_state = STP_DISABLED;
807 if (ofproto->sflow) {
808 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
809 netdev_get_name(port->up.netdev));
816 port_destruct(struct ofport *port_)
818 struct ofport_dpif *port = ofport_dpif_cast(port_);
819 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
821 ofproto->need_revalidate = true;
822 bundle_remove(port_);
823 set_cfm(port_, NULL);
824 if (ofproto->sflow) {
825 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
830 port_modified(struct ofport *port_)
832 struct ofport_dpif *port = ofport_dpif_cast(port_);
834 if (port->bundle && port->bundle->bond) {
835 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
840 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
842 struct ofport_dpif *port = ofport_dpif_cast(port_);
843 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
844 ovs_be32 changed = old_config ^ port->up.opp.config;
846 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
847 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
848 ofproto->need_revalidate = true;
850 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
851 bundle_update(port->bundle);
857 set_sflow(struct ofproto *ofproto_,
858 const struct ofproto_sflow_options *sflow_options)
860 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
861 struct dpif_sflow *ds = ofproto->sflow;
865 struct ofport_dpif *ofport;
867 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
868 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
869 dpif_sflow_add_port(ds, ofport->odp_port,
870 netdev_get_name(ofport->up.netdev));
872 ofproto->need_revalidate = true;
874 dpif_sflow_set_options(ds, sflow_options);
877 dpif_sflow_destroy(ds);
878 ofproto->need_revalidate = true;
879 ofproto->sflow = NULL;
886 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
888 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
895 struct ofproto_dpif *ofproto;
897 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
898 ofproto->need_revalidate = true;
899 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
902 if (cfm_configure(ofport->cfm, s)) {
908 cfm_destroy(ofport->cfm);
914 get_cfm_fault(const struct ofport *ofport_)
916 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
918 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
922 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
925 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
928 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
938 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
940 struct ofproto_dpif *ofproto = ofproto_;
941 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
942 struct ofport_dpif *ofport;
944 ofport = stp_port_get_aux(sp);
946 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
947 ofproto->up.name, port_num);
949 struct eth_header *eth = pkt->l2;
951 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
952 if (eth_addr_is_zero(eth->eth_src)) {
953 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
954 "with unknown MAC", ofproto->up.name, port_num);
956 int error = netdev_send(ofport->up.netdev, pkt);
958 VLOG_WARN_RL(&rl, "%s: sending BPDU on port %s failed (%s)",
960 netdev_get_name(ofport->up.netdev),
968 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
970 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
972 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
974 /* Only revalidate flows if the configuration changed. */
975 if (!s != !ofproto->stp) {
976 ofproto->need_revalidate = true;
981 ofproto->stp = stp_create(ofproto_->name, s->system_id,
982 send_bpdu_cb, ofproto);
983 ofproto->stp_last_tick = time_msec();
986 stp_set_bridge_id(ofproto->stp, s->system_id);
987 stp_set_bridge_priority(ofproto->stp, s->priority);
988 stp_set_hello_time(ofproto->stp, s->hello_time);
989 stp_set_max_age(ofproto->stp, s->max_age);
990 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
992 stp_destroy(ofproto->stp);
1000 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1002 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1006 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1007 s->designated_root = stp_get_designated_root(ofproto->stp);
1008 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1017 update_stp_port_state(struct ofport_dpif *ofport)
1019 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1020 enum stp_state state;
1022 /* Figure out new state. */
1023 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1027 if (ofport->stp_state != state) {
1031 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1032 netdev_get_name(ofport->up.netdev),
1033 stp_state_name(ofport->stp_state),
1034 stp_state_name(state));
1035 if (stp_learn_in_state(ofport->stp_state)
1036 != stp_learn_in_state(state)) {
1037 /* xxx Learning action flows should also be flushed. */
1038 mac_learning_flush(ofproto->ml);
1040 fwd_change = stp_forward_in_state(ofport->stp_state)
1041 != stp_forward_in_state(state);
1043 ofproto->need_revalidate = true;
1044 ofport->stp_state = state;
1045 ofport->stp_state_entered = time_msec();
1047 if (fwd_change && ofport->bundle) {
1048 bundle_update(ofport->bundle);
1051 /* Update the STP state bits in the OpenFlow port description. */
1052 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1053 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1054 : state == STP_LEARNING ? OFPPS_STP_LEARN
1055 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1056 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1058 ofproto_port_set_state(&ofport->up, of_state);
1062 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1063 * caller is responsible for assigning STP port numbers and ensuring
1064 * there are no duplicates. */
1066 set_stp_port(struct ofport *ofport_,
1067 const struct ofproto_port_stp_settings *s)
1069 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1070 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1071 struct stp_port *sp = ofport->stp_port;
1073 if (!s || !s->enable) {
1075 ofport->stp_port = NULL;
1076 stp_port_disable(sp);
1077 update_stp_port_state(ofport);
1080 } else if (sp && stp_port_no(sp) != s->port_num
1081 && ofport == stp_port_get_aux(sp)) {
1082 /* The port-id changed, so disable the old one if it's not
1083 * already in use by another port. */
1084 stp_port_disable(sp);
1087 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1088 stp_port_enable(sp);
1090 stp_port_set_aux(sp, ofport);
1091 stp_port_set_priority(sp, s->priority);
1092 stp_port_set_path_cost(sp, s->path_cost);
1094 update_stp_port_state(ofport);
1100 get_stp_port_status(struct ofport *ofport_,
1101 struct ofproto_port_stp_status *s)
1103 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1104 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1105 struct stp_port *sp = ofport->stp_port;
1107 if (!ofproto->stp || !sp) {
1113 s->port_id = stp_port_get_id(sp);
1114 s->state = stp_port_get_state(sp);
1115 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1116 s->role = stp_port_get_role(sp);
1117 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1123 stp_run(struct ofproto_dpif *ofproto)
1126 long long int now = time_msec();
1127 long long int elapsed = now - ofproto->stp_last_tick;
1128 struct stp_port *sp;
1131 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1132 ofproto->stp_last_tick = now;
1134 while (stp_get_changed_port(ofproto->stp, &sp)) {
1135 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1138 update_stp_port_state(ofport);
1145 stp_wait(struct ofproto_dpif *ofproto)
1148 poll_timer_wait(1000);
1152 /* Returns true if STP should process 'flow'. */
1154 stp_should_process_flow(const struct flow *flow)
1156 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1160 stp_process_packet(const struct ofport_dpif *ofport,
1161 const struct ofpbuf *packet)
1163 struct ofpbuf payload = *packet;
1164 struct eth_header *eth = payload.data;
1165 struct stp_port *sp = ofport->stp_port;
1167 /* Sink packets on ports that have STP disabled when the bridge has
1169 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1173 /* Trim off padding on payload. */
1174 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1175 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1178 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1179 stp_received_bpdu(sp, payload.data, payload.size);
1185 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1186 * to revalidate every flow. */
1188 bundle_flush_macs(struct ofbundle *bundle)
1190 struct ofproto_dpif *ofproto = bundle->ofproto;
1191 struct mac_learning *ml = ofproto->ml;
1192 struct mac_entry *mac, *next_mac;
1194 ofproto->need_revalidate = true;
1195 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1196 if (mac->port.p == bundle) {
1197 mac_learning_expire(ml, mac);
1202 static struct ofbundle *
1203 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1205 struct ofbundle *bundle;
1207 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1208 &ofproto->bundles) {
1209 if (bundle->aux == aux) {
1216 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1217 * ones that are found to 'bundles'. */
1219 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1220 void **auxes, size_t n_auxes,
1221 struct hmapx *bundles)
1225 hmapx_init(bundles);
1226 for (i = 0; i < n_auxes; i++) {
1227 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1229 hmapx_add(bundles, bundle);
1235 bundle_update(struct ofbundle *bundle)
1237 struct ofport_dpif *port;
1239 bundle->floodable = true;
1240 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1241 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1242 || !stp_forward_in_state(port->stp_state)) {
1243 bundle->floodable = false;
1250 bundle_del_port(struct ofport_dpif *port)
1252 struct ofbundle *bundle = port->bundle;
1254 bundle->ofproto->need_revalidate = true;
1256 list_remove(&port->bundle_node);
1257 port->bundle = NULL;
1260 lacp_slave_unregister(bundle->lacp, port);
1263 bond_slave_unregister(bundle->bond, port);
1266 bundle_update(bundle);
1270 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1271 struct lacp_slave_settings *lacp,
1272 uint32_t bond_stable_id)
1274 struct ofport_dpif *port;
1276 port = get_ofp_port(bundle->ofproto, ofp_port);
1281 if (port->bundle != bundle) {
1282 bundle->ofproto->need_revalidate = true;
1284 bundle_del_port(port);
1287 port->bundle = bundle;
1288 list_push_back(&bundle->ports, &port->bundle_node);
1289 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1290 || !stp_forward_in_state(port->stp_state)) {
1291 bundle->floodable = false;
1295 port->bundle->ofproto->need_revalidate = true;
1296 lacp_slave_register(bundle->lacp, port, lacp);
1299 port->bond_stable_id = bond_stable_id;
1305 bundle_destroy(struct ofbundle *bundle)
1307 struct ofproto_dpif *ofproto;
1308 struct ofport_dpif *port, *next_port;
1315 ofproto = bundle->ofproto;
1316 for (i = 0; i < MAX_MIRRORS; i++) {
1317 struct ofmirror *m = ofproto->mirrors[i];
1319 if (m->out == bundle) {
1321 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1322 || hmapx_find_and_delete(&m->dsts, bundle)) {
1323 ofproto->need_revalidate = true;
1328 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1329 bundle_del_port(port);
1332 bundle_flush_macs(bundle);
1333 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1335 free(bundle->trunks);
1336 lacp_destroy(bundle->lacp);
1337 bond_destroy(bundle->bond);
1342 bundle_set(struct ofproto *ofproto_, void *aux,
1343 const struct ofproto_bundle_settings *s)
1345 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1346 bool need_flush = false;
1347 struct ofport_dpif *port;
1348 struct ofbundle *bundle;
1349 unsigned long *trunks;
1355 bundle_destroy(bundle_lookup(ofproto, aux));
1359 assert(s->n_slaves == 1 || s->bond != NULL);
1360 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1362 bundle = bundle_lookup(ofproto, aux);
1364 bundle = xmalloc(sizeof *bundle);
1366 bundle->ofproto = ofproto;
1367 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1368 hash_pointer(aux, 0));
1370 bundle->name = NULL;
1372 list_init(&bundle->ports);
1373 bundle->vlan_mode = PORT_VLAN_TRUNK;
1375 bundle->trunks = NULL;
1376 bundle->lacp = NULL;
1377 bundle->bond = NULL;
1379 bundle->floodable = true;
1381 bundle->src_mirrors = 0;
1382 bundle->dst_mirrors = 0;
1383 bundle->mirror_out = 0;
1386 if (!bundle->name || strcmp(s->name, bundle->name)) {
1388 bundle->name = xstrdup(s->name);
1393 if (!bundle->lacp) {
1394 ofproto->need_revalidate = true;
1395 bundle->lacp = lacp_create();
1397 lacp_configure(bundle->lacp, s->lacp);
1399 lacp_destroy(bundle->lacp);
1400 bundle->lacp = NULL;
1403 /* Update set of ports. */
1405 for (i = 0; i < s->n_slaves; i++) {
1406 if (!bundle_add_port(bundle, s->slaves[i],
1407 s->lacp ? &s->lacp_slaves[i] : NULL,
1408 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1412 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1413 struct ofport_dpif *next_port;
1415 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1416 for (i = 0; i < s->n_slaves; i++) {
1417 if (s->slaves[i] == port->up.ofp_port) {
1422 bundle_del_port(port);
1426 assert(list_size(&bundle->ports) <= s->n_slaves);
1428 if (list_is_empty(&bundle->ports)) {
1429 bundle_destroy(bundle);
1433 /* Set VLAN tagging mode */
1434 if (s->vlan_mode != bundle->vlan_mode) {
1435 bundle->vlan_mode = s->vlan_mode;
1440 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1441 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1443 if (vlan != bundle->vlan) {
1444 bundle->vlan = vlan;
1448 /* Get trunked VLANs. */
1449 switch (s->vlan_mode) {
1450 case PORT_VLAN_ACCESS:
1454 case PORT_VLAN_TRUNK:
1455 trunks = (unsigned long *) s->trunks;
1458 case PORT_VLAN_NATIVE_UNTAGGED:
1459 case PORT_VLAN_NATIVE_TAGGED:
1460 if (vlan != 0 && (!s->trunks
1461 || !bitmap_is_set(s->trunks, vlan)
1462 || bitmap_is_set(s->trunks, 0))) {
1463 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1465 trunks = bitmap_clone(s->trunks, 4096);
1467 trunks = bitmap_allocate1(4096);
1469 bitmap_set1(trunks, vlan);
1470 bitmap_set0(trunks, 0);
1472 trunks = (unsigned long *) s->trunks;
1479 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1480 free(bundle->trunks);
1481 if (trunks == s->trunks) {
1482 bundle->trunks = vlan_bitmap_clone(trunks);
1484 bundle->trunks = trunks;
1489 if (trunks != s->trunks) {
1494 if (!list_is_short(&bundle->ports)) {
1495 bundle->ofproto->has_bonded_bundles = true;
1497 if (bond_reconfigure(bundle->bond, s->bond)) {
1498 ofproto->need_revalidate = true;
1501 bundle->bond = bond_create(s->bond);
1502 ofproto->need_revalidate = true;
1505 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1506 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1510 bond_destroy(bundle->bond);
1511 bundle->bond = NULL;
1514 /* If we changed something that would affect MAC learning, un-learn
1515 * everything on this port and force flow revalidation. */
1517 bundle_flush_macs(bundle);
1524 bundle_remove(struct ofport *port_)
1526 struct ofport_dpif *port = ofport_dpif_cast(port_);
1527 struct ofbundle *bundle = port->bundle;
1530 bundle_del_port(port);
1531 if (list_is_empty(&bundle->ports)) {
1532 bundle_destroy(bundle);
1533 } else if (list_is_short(&bundle->ports)) {
1534 bond_destroy(bundle->bond);
1535 bundle->bond = NULL;
1541 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1543 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1544 struct ofport_dpif *port = port_;
1545 uint8_t ea[ETH_ADDR_LEN];
1548 error = netdev_get_etheraddr(port->up.netdev, ea);
1550 struct ofpbuf packet;
1553 ofpbuf_init(&packet, 0);
1554 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1556 memcpy(packet_pdu, pdu, pdu_size);
1558 error = netdev_send(port->up.netdev, &packet);
1560 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1561 "(%s)", port->bundle->name,
1562 netdev_get_name(port->up.netdev), strerror(error));
1564 ofpbuf_uninit(&packet);
1566 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1567 "%s (%s)", port->bundle->name,
1568 netdev_get_name(port->up.netdev), strerror(error));
1573 bundle_send_learning_packets(struct ofbundle *bundle)
1575 struct ofproto_dpif *ofproto = bundle->ofproto;
1576 int error, n_packets, n_errors;
1577 struct mac_entry *e;
1579 error = n_packets = n_errors = 0;
1580 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1581 if (e->port.p != bundle) {
1582 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1592 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1593 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1594 "packets, last error was: %s",
1595 bundle->name, n_errors, n_packets, strerror(error));
1597 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1598 bundle->name, n_packets);
1603 bundle_run(struct ofbundle *bundle)
1606 lacp_run(bundle->lacp, send_pdu_cb);
1609 struct ofport_dpif *port;
1611 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1612 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1615 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1616 lacp_negotiated(bundle->lacp));
1617 if (bond_should_send_learning_packets(bundle->bond)) {
1618 bundle_send_learning_packets(bundle);
1624 bundle_wait(struct ofbundle *bundle)
1627 lacp_wait(bundle->lacp);
1630 bond_wait(bundle->bond);
1637 mirror_scan(struct ofproto_dpif *ofproto)
1641 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1642 if (!ofproto->mirrors[idx]) {
1649 static struct ofmirror *
1650 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1654 for (i = 0; i < MAX_MIRRORS; i++) {
1655 struct ofmirror *mirror = ofproto->mirrors[i];
1656 if (mirror && mirror->aux == aux) {
1665 mirror_set(struct ofproto *ofproto_, void *aux,
1666 const struct ofproto_mirror_settings *s)
1668 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1669 mirror_mask_t mirror_bit;
1670 struct ofbundle *bundle;
1671 struct ofmirror *mirror;
1672 struct ofbundle *out;
1673 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1674 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1677 mirror = mirror_lookup(ofproto, aux);
1679 mirror_destroy(mirror);
1685 idx = mirror_scan(ofproto);
1687 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1689 ofproto->up.name, MAX_MIRRORS, s->name);
1693 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1694 mirror->ofproto = ofproto;
1697 mirror->out_vlan = -1;
1698 mirror->name = NULL;
1701 if (!mirror->name || strcmp(s->name, mirror->name)) {
1703 mirror->name = xstrdup(s->name);
1706 /* Get the new configuration. */
1707 if (s->out_bundle) {
1708 out = bundle_lookup(ofproto, s->out_bundle);
1710 mirror_destroy(mirror);
1716 out_vlan = s->out_vlan;
1718 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1719 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1721 /* If the configuration has not changed, do nothing. */
1722 if (hmapx_equals(&srcs, &mirror->srcs)
1723 && hmapx_equals(&dsts, &mirror->dsts)
1724 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1725 && mirror->out == out
1726 && mirror->out_vlan == out_vlan)
1728 hmapx_destroy(&srcs);
1729 hmapx_destroy(&dsts);
1733 hmapx_swap(&srcs, &mirror->srcs);
1734 hmapx_destroy(&srcs);
1736 hmapx_swap(&dsts, &mirror->dsts);
1737 hmapx_destroy(&dsts);
1739 free(mirror->vlans);
1740 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1743 mirror->out_vlan = out_vlan;
1745 /* Update bundles. */
1746 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1747 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1748 if (hmapx_contains(&mirror->srcs, bundle)) {
1749 bundle->src_mirrors |= mirror_bit;
1751 bundle->src_mirrors &= ~mirror_bit;
1754 if (hmapx_contains(&mirror->dsts, bundle)) {
1755 bundle->dst_mirrors |= mirror_bit;
1757 bundle->dst_mirrors &= ~mirror_bit;
1760 if (mirror->out == bundle) {
1761 bundle->mirror_out |= mirror_bit;
1763 bundle->mirror_out &= ~mirror_bit;
1767 ofproto->need_revalidate = true;
1768 mac_learning_flush(ofproto->ml);
1774 mirror_destroy(struct ofmirror *mirror)
1776 struct ofproto_dpif *ofproto;
1777 mirror_mask_t mirror_bit;
1778 struct ofbundle *bundle;
1784 ofproto = mirror->ofproto;
1785 ofproto->need_revalidate = true;
1786 mac_learning_flush(ofproto->ml);
1788 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1789 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1790 bundle->src_mirrors &= ~mirror_bit;
1791 bundle->dst_mirrors &= ~mirror_bit;
1792 bundle->mirror_out &= ~mirror_bit;
1795 hmapx_destroy(&mirror->srcs);
1796 hmapx_destroy(&mirror->dsts);
1797 free(mirror->vlans);
1799 ofproto->mirrors[mirror->idx] = NULL;
1805 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1807 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1808 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1809 ofproto->need_revalidate = true;
1810 mac_learning_flush(ofproto->ml);
1816 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
1818 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1819 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1820 return bundle && bundle->mirror_out != 0;
1824 forward_bpdu_changed(struct ofproto *ofproto_)
1826 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1827 /* Revalidate cached flows whenever forward_bpdu option changes. */
1828 ofproto->need_revalidate = true;
1833 static struct ofport_dpif *
1834 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1836 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1837 return ofport ? ofport_dpif_cast(ofport) : NULL;
1840 static struct ofport_dpif *
1841 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1843 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1847 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1848 struct dpif_port *dpif_port)
1850 ofproto_port->name = dpif_port->name;
1851 ofproto_port->type = dpif_port->type;
1852 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1856 port_run(struct ofport_dpif *ofport)
1858 bool enable = netdev_get_carrier(ofport->up.netdev);
1861 cfm_run(ofport->cfm);
1863 if (cfm_should_send_ccm(ofport->cfm)) {
1864 struct ofpbuf packet;
1866 ofpbuf_init(&packet, 0);
1867 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1868 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1869 ofport->odp_port, &packet);
1870 ofpbuf_uninit(&packet);
1873 enable = enable && !cfm_get_fault(ofport->cfm)
1874 && cfm_get_opup(ofport->cfm);
1877 if (ofport->bundle) {
1878 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1881 if (ofport->may_enable != enable) {
1882 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1884 if (ofproto->has_bundle_action) {
1885 ofproto->need_revalidate = true;
1889 ofport->may_enable = enable;
1893 port_wait(struct ofport_dpif *ofport)
1896 cfm_wait(ofport->cfm);
1901 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1902 struct ofproto_port *ofproto_port)
1904 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1905 struct dpif_port dpif_port;
1908 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1910 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1916 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1918 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1922 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1924 *ofp_portp = odp_port_to_ofp_port(odp_port);
1930 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1932 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1935 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1937 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1939 /* The caller is going to close ofport->up.netdev. If this is a
1940 * bonded port, then the bond is using that netdev, so remove it
1941 * from the bond. The client will need to reconfigure everything
1942 * after deleting ports, so then the slave will get re-added. */
1943 bundle_remove(&ofport->up);
1949 struct port_dump_state {
1950 struct dpif_port_dump dump;
1955 port_dump_start(const struct ofproto *ofproto_, void **statep)
1957 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1958 struct port_dump_state *state;
1960 *statep = state = xmalloc(sizeof *state);
1961 dpif_port_dump_start(&state->dump, ofproto->dpif);
1962 state->done = false;
1967 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1968 struct ofproto_port *port)
1970 struct port_dump_state *state = state_;
1971 struct dpif_port dpif_port;
1973 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1974 ofproto_port_from_dpif_port(port, &dpif_port);
1977 int error = dpif_port_dump_done(&state->dump);
1979 return error ? error : EOF;
1984 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1986 struct port_dump_state *state = state_;
1989 dpif_port_dump_done(&state->dump);
1996 port_poll(const struct ofproto *ofproto_, char **devnamep)
1998 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1999 return dpif_port_poll(ofproto->dpif, devnamep);
2003 port_poll_wait(const struct ofproto *ofproto_)
2005 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2006 dpif_port_poll_wait(ofproto->dpif);
2010 port_is_lacp_current(const struct ofport *ofport_)
2012 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2013 return (ofport->bundle && ofport->bundle->lacp
2014 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2018 /* Upcall handling. */
2020 /* Flow miss batching.
2022 * Some dpifs implement operations faster when you hand them off in a batch.
2023 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2024 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2025 * more packets, plus possibly installing the flow in the dpif.
2027 * So far we only batch the operations that affect flow setup time the most.
2028 * It's possible to batch more than that, but the benefit might be minimal. */
2030 struct hmap_node hmap_node;
2032 const struct nlattr *key;
2034 struct list packets;
2037 struct flow_miss_op {
2038 union dpif_op dpif_op;
2039 struct facet *facet;
2042 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2043 * OpenFlow controller as necessary according to their individual
2046 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2047 * ownership is transferred to this function. */
2049 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2050 const struct flow *flow, bool clone)
2052 struct ofputil_packet_in pin;
2054 pin.packet = packet;
2055 pin.in_port = flow->in_port;
2056 pin.reason = OFPR_NO_MATCH;
2057 pin.buffer_id = 0; /* not yet known */
2058 pin.send_len = 0; /* not used for flow table misses */
2059 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2060 clone ? NULL : packet);
2063 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2064 * OpenFlow controller as necessary according to their individual
2067 * 'send_len' should be the number of bytes of 'packet' to send to the
2068 * controller, as specified in the action that caused the packet to be sent.
2070 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2071 * Otherwise, ownership is transferred to this function. */
2073 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2074 uint64_t userdata, const struct flow *flow, bool clone)
2076 struct ofputil_packet_in pin;
2077 struct user_action_cookie cookie;
2079 memcpy(&cookie, &userdata, sizeof(cookie));
2081 pin.packet = packet;
2082 pin.in_port = flow->in_port;
2083 pin.reason = OFPR_ACTION;
2084 pin.buffer_id = 0; /* not yet known */
2085 pin.send_len = cookie.data;
2086 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2087 clone ? NULL : packet);
2091 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2092 const struct ofpbuf *packet)
2094 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2100 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2102 cfm_process_heartbeat(ofport->cfm, packet);
2105 } else if (ofport->bundle && ofport->bundle->lacp
2106 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2108 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2111 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2113 stp_process_packet(ofport, packet);
2120 static struct flow_miss *
2121 flow_miss_create(struct hmap *todo, const struct flow *flow,
2122 const struct nlattr *key, size_t key_len)
2124 uint32_t hash = flow_hash(flow, 0);
2125 struct flow_miss *miss;
2127 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2128 if (flow_equal(&miss->flow, flow)) {
2133 miss = xmalloc(sizeof *miss);
2134 hmap_insert(todo, &miss->hmap_node, hash);
2137 miss->key_len = key_len;
2138 list_init(&miss->packets);
2143 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2144 struct flow_miss_op *ops, size_t *n_ops)
2146 const struct flow *flow = &miss->flow;
2147 struct ofpbuf *packet, *next_packet;
2148 struct facet *facet;
2150 facet = facet_lookup_valid(ofproto, flow);
2152 struct rule_dpif *rule;
2154 rule = rule_dpif_lookup(ofproto, flow, 0);
2156 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2157 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2159 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2160 COVERAGE_INC(ofproto_dpif_no_packet_in);
2161 /* XXX install 'drop' flow entry */
2165 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2169 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2171 list_remove(&packet->list_node);
2172 send_packet_in_miss(ofproto, packet, flow, false);
2178 facet = facet_create(rule, flow);
2181 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2182 list_remove(&packet->list_node);
2183 ofproto->n_matches++;
2185 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2187 * Extra-special case for fail-open mode.
2189 * We are in fail-open mode and the packet matched the fail-open
2190 * rule, but we are connected to a controller too. We should send
2191 * the packet up to the controller in the hope that it will try to
2192 * set up a flow and thereby allow us to exit fail-open.
2194 * See the top-level comment in fail-open.c for more information.
2196 send_packet_in_miss(ofproto, packet, flow, true);
2199 if (!facet->may_install) {
2200 facet_make_actions(ofproto, facet, packet);
2202 if (!execute_controller_action(ofproto, &facet->flow,
2203 facet->actions, facet->actions_len,
2205 struct flow_miss_op *op = &ops[(*n_ops)++];
2206 struct dpif_execute *execute = &op->dpif_op.execute;
2209 execute->type = DPIF_OP_EXECUTE;
2210 execute->key = miss->key;
2211 execute->key_len = miss->key_len;
2213 = (facet->may_install
2215 : xmemdup(facet->actions, facet->actions_len));
2216 execute->actions_len = facet->actions_len;
2217 execute->packet = packet;
2221 if (facet->may_install) {
2222 struct flow_miss_op *op = &ops[(*n_ops)++];
2223 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2226 put->type = DPIF_OP_FLOW_PUT;
2227 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2228 put->key = miss->key;
2229 put->key_len = miss->key_len;
2230 put->actions = facet->actions;
2231 put->actions_len = facet->actions_len;
2237 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2240 struct dpif_upcall *upcall;
2241 struct flow_miss *miss, *next_miss;
2242 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2243 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2252 /* Construct the to-do list.
2254 * This just amounts to extracting the flow from each packet and sticking
2255 * the packets that have the same flow in the same "flow_miss" structure so
2256 * that we can process them together. */
2258 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2259 struct flow_miss *miss;
2262 /* Obtain in_port and tun_id, at least, then set 'flow''s header
2264 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2265 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
2267 /* Handle 802.1ag, LACP, and STP specially. */
2268 if (process_special(ofproto, &flow, upcall->packet)) {
2269 ofpbuf_delete(upcall->packet);
2270 ofproto->n_matches++;
2274 /* Add other packets to a to-do list. */
2275 miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len);
2276 list_push_back(&miss->packets, &upcall->packet->list_node);
2279 /* Process each element in the to-do list, constructing the set of
2280 * operations to batch. */
2282 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2283 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2284 ofpbuf_list_delete(&miss->packets);
2285 hmap_remove(&todo, &miss->hmap_node);
2288 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2289 hmap_destroy(&todo);
2291 /* Execute batch. */
2292 for (i = 0; i < n_ops; i++) {
2293 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2295 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2297 /* Free memory and update facets. */
2298 for (i = 0; i < n_ops; i++) {
2299 struct flow_miss_op *op = &flow_miss_ops[i];
2300 struct dpif_execute *execute;
2301 struct dpif_flow_put *put;
2303 switch (op->dpif_op.type) {
2304 case DPIF_OP_EXECUTE:
2305 execute = &op->dpif_op.execute;
2306 if (op->facet->actions != execute->actions) {
2307 free((struct nlattr *) execute->actions);
2309 ofpbuf_delete((struct ofpbuf *) execute->packet);
2312 case DPIF_OP_FLOW_PUT:
2313 put = &op->dpif_op.flow_put;
2315 op->facet->installed = true;
2323 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2324 struct dpif_upcall *upcall)
2327 struct user_action_cookie cookie;
2329 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2331 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2332 if (ofproto->sflow) {
2333 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2334 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2336 ofpbuf_delete(upcall->packet);
2338 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2339 COVERAGE_INC(ofproto_dpif_ctlr_action);
2340 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2341 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2344 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2349 handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
2351 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
2355 assert (max_batch <= FLOW_MISS_MAX_BATCH);
2358 for (i = 0; i < max_batch; i++) {
2359 struct dpif_upcall *upcall = &misses[n_misses];
2362 error = dpif_recv(ofproto->dpif, upcall);
2367 switch (upcall->type) {
2368 case DPIF_UC_ACTION:
2369 handle_userspace_upcall(ofproto, upcall);
2373 /* Handle it later. */
2377 case DPIF_N_UC_TYPES:
2379 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
2385 handle_miss_upcalls(ofproto, misses, n_misses);
2390 /* Flow expiration. */
2392 static int facet_max_idle(const struct ofproto_dpif *);
2393 static void update_stats(struct ofproto_dpif *);
2394 static void rule_expire(struct rule_dpif *);
2395 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
2397 /* This function is called periodically by run(). Its job is to collect
2398 * updates for the flows that have been installed into the datapath, most
2399 * importantly when they last were used, and then use that information to
2400 * expire flows that have not been used recently.
2402 * Returns the number of milliseconds after which it should be called again. */
2404 expire(struct ofproto_dpif *ofproto)
2406 struct rule_dpif *rule, *next_rule;
2407 struct classifier *table;
2410 /* Update stats for each flow in the datapath. */
2411 update_stats(ofproto);
2413 /* Expire facets that have been idle too long. */
2414 dp_max_idle = facet_max_idle(ofproto);
2415 expire_facets(ofproto, dp_max_idle);
2417 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2418 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2419 struct cls_cursor cursor;
2421 cls_cursor_init(&cursor, table, NULL);
2422 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2427 /* All outstanding data in existing flows has been accounted, so it's a
2428 * good time to do bond rebalancing. */
2429 if (ofproto->has_bonded_bundles) {
2430 struct ofbundle *bundle;
2432 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2434 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2439 return MIN(dp_max_idle, 1000);
2442 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2444 * This function also pushes statistics updates to rules which each facet
2445 * resubmits into. Generally these statistics will be accurate. However, if a
2446 * facet changes the rule it resubmits into at some time in between
2447 * update_stats() runs, it is possible that statistics accrued to the
2448 * old rule will be incorrectly attributed to the new rule. This could be
2449 * avoided by calling update_stats() whenever rules are created or
2450 * deleted. However, the performance impact of making so many calls to the
2451 * datapath do not justify the benefit of having perfectly accurate statistics.
2454 update_stats(struct ofproto_dpif *p)
2456 const struct dpif_flow_stats *stats;
2457 struct dpif_flow_dump dump;
2458 const struct nlattr *key;
2461 dpif_flow_dump_start(&dump, p->dpif);
2462 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2463 struct facet *facet;
2466 if (odp_flow_key_to_flow(key, key_len, &flow)) {
2470 odp_flow_key_format(key, key_len, &s);
2471 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
2477 facet = facet_find(p, &flow);
2479 if (facet && facet->installed) {
2481 if (stats->n_packets >= facet->dp_packet_count) {
2482 uint64_t extra = stats->n_packets - facet->dp_packet_count;
2483 facet->packet_count += extra;
2485 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2488 if (stats->n_bytes >= facet->dp_byte_count) {
2489 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
2491 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2494 facet->dp_packet_count = stats->n_packets;
2495 facet->dp_byte_count = stats->n_bytes;
2497 facet_update_time(p, facet, stats->used);
2498 facet_account(p, facet);
2499 facet_push_stats(facet);
2501 /* There's a flow in the datapath that we know nothing about.
2503 COVERAGE_INC(facet_unexpected);
2504 dpif_flow_del(p->dpif, key, key_len, NULL);
2507 dpif_flow_dump_done(&dump);
2510 /* Calculates and returns the number of milliseconds of idle time after which
2511 * facets should expire from the datapath and we should fold their statistics
2512 * into their parent rules in userspace. */
2514 facet_max_idle(const struct ofproto_dpif *ofproto)
2517 * Idle time histogram.
2519 * Most of the time a switch has a relatively small number of facets. When
2520 * this is the case we might as well keep statistics for all of them in
2521 * userspace and to cache them in the kernel datapath for performance as
2524 * As the number of facets increases, the memory required to maintain
2525 * statistics about them in userspace and in the kernel becomes
2526 * significant. However, with a large number of facets it is likely that
2527 * only a few of them are "heavy hitters" that consume a large amount of
2528 * bandwidth. At this point, only heavy hitters are worth caching in the
2529 * kernel and maintaining in userspaces; other facets we can discard.
2531 * The technique used to compute the idle time is to build a histogram with
2532 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
2533 * that is installed in the kernel gets dropped in the appropriate bucket.
2534 * After the histogram has been built, we compute the cutoff so that only
2535 * the most-recently-used 1% of facets (but at least
2536 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2537 * the most-recently-used bucket of facets is kept, so actually an
2538 * arbitrary number of facets can be kept in any given expiration run
2539 * (though the next run will delete most of those unless they receive
2542 * This requires a second pass through the facets, in addition to the pass
2543 * made by update_stats(), because the former function never looks
2544 * at uninstallable facets.
2546 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2547 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2548 int buckets[N_BUCKETS] = { 0 };
2549 int total, subtotal, bucket;
2550 struct facet *facet;
2554 total = hmap_count(&ofproto->facets);
2555 if (total <= ofproto->up.flow_eviction_threshold) {
2556 return N_BUCKETS * BUCKET_WIDTH;
2559 /* Build histogram. */
2561 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
2562 long long int idle = now - facet->used;
2563 int bucket = (idle <= 0 ? 0
2564 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2565 : (unsigned int) idle / BUCKET_WIDTH);
2569 /* Find the first bucket whose flows should be expired. */
2570 subtotal = bucket = 0;
2572 subtotal += buckets[bucket++];
2573 } while (bucket < N_BUCKETS &&
2574 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2576 if (VLOG_IS_DBG_ENABLED()) {
2580 ds_put_cstr(&s, "keep");
2581 for (i = 0; i < N_BUCKETS; i++) {
2583 ds_put_cstr(&s, ", drop");
2586 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2589 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2593 return bucket * BUCKET_WIDTH;
2597 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2599 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2600 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2601 struct ofexpired expired;
2603 if (facet->installed) {
2604 struct dpif_flow_stats stats;
2606 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2608 facet_update_stats(ofproto, facet, &stats);
2611 expired.flow = facet->flow;
2612 expired.packet_count = facet->packet_count;
2613 expired.byte_count = facet->byte_count;
2614 expired.used = facet->used;
2615 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2620 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2622 long long int cutoff = time_msec() - dp_max_idle;
2623 struct facet *facet, *next_facet;
2625 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2626 facet_active_timeout(ofproto, facet);
2627 if (facet->used < cutoff) {
2628 facet_remove(ofproto, facet);
2633 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2634 * then delete it entirely. */
2636 rule_expire(struct rule_dpif *rule)
2638 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2639 struct facet *facet, *next_facet;
2643 /* Has 'rule' expired? */
2645 if (rule->up.hard_timeout
2646 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2647 reason = OFPRR_HARD_TIMEOUT;
2648 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2649 && now > rule->used + rule->up.idle_timeout * 1000) {
2650 reason = OFPRR_IDLE_TIMEOUT;
2655 COVERAGE_INC(ofproto_dpif_expired);
2657 /* Update stats. (This is a no-op if the rule expired due to an idle
2658 * timeout, because that only happens when the rule has no facets left.) */
2659 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2660 facet_remove(ofproto, facet);
2663 /* Get rid of the rule. */
2664 ofproto_rule_expire(&rule->up, reason);
2669 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2671 * The caller must already have determined that no facet with an identical
2672 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2673 * the ofproto's classifier table.
2675 * The facet will initially have no ODP actions. The caller should fix that
2676 * by calling facet_make_actions(). */
2677 static struct facet *
2678 facet_create(struct rule_dpif *rule, const struct flow *flow)
2680 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2681 struct facet *facet;
2683 facet = xzalloc(sizeof *facet);
2684 facet->used = time_msec();
2685 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2686 list_push_back(&rule->facets, &facet->list_node);
2688 facet->flow = *flow;
2689 netflow_flow_init(&facet->nf_flow);
2690 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2696 facet_free(struct facet *facet)
2698 free(facet->actions);
2702 /* If the 'actions_len' bytes of actions in 'odp_actions' are just a single
2703 * OVS_ACTION_ATTR_USERSPACE action, executes it internally and returns true.
2704 * Otherwise, returns false without doing anything.
2706 * If 'clone' is true, the caller always retains ownership of 'packet'.
2707 * Otherwise, ownership is transferred to this function if it returns true. */
2709 execute_controller_action(struct ofproto_dpif *ofproto,
2710 const struct flow *flow,
2711 const struct nlattr *odp_actions, size_t actions_len,
2712 struct ofpbuf *packet, bool clone)
2715 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2716 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2717 /* As an optimization, avoid a round-trip from userspace to kernel to
2718 * userspace. This also avoids possibly filling up kernel packet
2719 * buffers along the way.
2721 * This optimization will not accidentally catch sFlow
2722 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2723 * inside OVS_ACTION_ATTR_SAMPLE. */
2724 const struct nlattr *nla;
2726 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2727 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2735 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2736 * 'packet', which arrived on 'in_port'.
2738 * Takes ownership of 'packet'. */
2740 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2741 const struct nlattr *odp_actions, size_t actions_len,
2742 struct ofpbuf *packet)
2744 struct odputil_keybuf keybuf;
2748 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2753 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2754 odp_flow_key_from_flow(&key, flow);
2756 error = dpif_execute(ofproto->dpif, key.data, key.size,
2757 odp_actions, actions_len, packet);
2759 ofpbuf_delete(packet);
2763 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2764 * statistics appropriately. 'packet' must have at least sizeof(struct
2765 * ofp_packet_in) bytes of headroom.
2767 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2768 * applying flow_extract() to 'packet' would yield the same flow as
2771 * 'facet' must have accurately composed datapath actions; that is, it must
2772 * not be in need of revalidation.
2774 * Takes ownership of 'packet'. */
2776 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2777 struct ofpbuf *packet)
2779 struct dpif_flow_stats stats;
2781 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2783 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2784 stats.used = time_msec();
2785 if (execute_odp_actions(ofproto, &facet->flow,
2786 facet->actions, facet->actions_len, packet)) {
2787 facet_update_stats(ofproto, facet, &stats);
2791 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2793 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2794 * rule's statistics, via facet_uninstall().
2796 * - Removes 'facet' from its rule and from ofproto->facets.
2799 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2801 facet_uninstall(ofproto, facet);
2802 facet_flush_stats(ofproto, facet);
2803 hmap_remove(&ofproto->facets, &facet->hmap_node);
2804 list_remove(&facet->list_node);
2808 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2810 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2811 const struct ofpbuf *packet)
2813 const struct rule_dpif *rule = facet->rule;
2814 struct ofpbuf *odp_actions;
2815 struct action_xlate_ctx ctx;
2817 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2818 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2819 facet->tags = ctx.tags;
2820 facet->may_install = ctx.may_set_up_flow;
2821 facet->has_learn = ctx.has_learn;
2822 facet->has_normal = ctx.has_normal;
2823 facet->nf_flow.output_iface = ctx.nf_output_iface;
2825 if (facet->actions_len != odp_actions->size
2826 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2827 free(facet->actions);
2828 facet->actions_len = odp_actions->size;
2829 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2832 ofpbuf_delete(odp_actions);
2835 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2836 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2837 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2838 * since 'facet' was last updated.
2840 * Returns 0 if successful, otherwise a positive errno value.*/
2842 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2843 const struct nlattr *actions, size_t actions_len,
2844 struct dpif_flow_stats *stats)
2846 struct odputil_keybuf keybuf;
2847 enum dpif_flow_put_flags flags;
2851 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2853 flags |= DPIF_FP_ZERO_STATS;
2856 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2857 odp_flow_key_from_flow(&key, &facet->flow);
2859 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2860 actions, actions_len, stats);
2863 facet_reset_dp_stats(facet, stats);
2869 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2870 * 'zero_stats' is true, clears any existing statistics from the datapath for
2873 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2875 struct dpif_flow_stats stats;
2877 if (facet->may_install
2878 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2879 zero_stats ? &stats : NULL)) {
2880 facet->installed = true;
2885 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2888 const struct nlattr *a;
2892 if (facet->byte_count <= facet->accounted_bytes) {
2895 n_bytes = facet->byte_count - facet->accounted_bytes;
2896 facet->accounted_bytes = facet->byte_count;
2898 /* Feed information from the active flows back into the learning table to
2899 * ensure that table is always in sync with what is actually flowing
2900 * through the datapath. */
2901 if (facet->has_learn || facet->has_normal) {
2902 struct action_xlate_ctx ctx;
2904 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2905 ctx.may_learn = true;
2906 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2907 facet->rule->up.n_actions));
2910 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2914 /* This loop feeds byte counters to bond_account() for rebalancing to use
2915 * as a basis. We also need to track the actual VLAN on which the packet
2916 * is going to be sent to ensure that it matches the one passed to
2917 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2919 vlan_tci = facet->flow.vlan_tci;
2920 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2921 struct ofport_dpif *port;
2923 switch (nl_attr_type(a)) {
2924 const struct nlattr *nested;
2925 case OVS_ACTION_ATTR_OUTPUT:
2926 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2927 if (port && port->bundle && port->bundle->bond) {
2928 bond_account(port->bundle->bond, &facet->flow,
2929 vlan_tci_to_vid(vlan_tci), n_bytes);
2933 case OVS_ACTION_ATTR_POP:
2934 if (nl_attr_get_u16(a) == OVS_KEY_ATTR_8021Q) {
2935 vlan_tci = htons(0);
2939 case OVS_ACTION_ATTR_PUSH:
2940 nested = nl_attr_get(a);
2941 if (nl_attr_type(nested) == OVS_KEY_ATTR_8021Q) {
2942 const struct ovs_key_8021q *q_key;
2944 q_key = nl_attr_get_unspec(nested, sizeof(*q_key));
2945 vlan_tci = q_key->q_tci;
2952 /* If 'rule' is installed in the datapath, uninstalls it. */
2954 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2956 if (facet->installed) {
2957 struct odputil_keybuf keybuf;
2958 struct dpif_flow_stats stats;
2962 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2963 odp_flow_key_from_flow(&key, &facet->flow);
2965 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2966 facet_reset_dp_stats(facet, &stats);
2968 facet_update_stats(p, facet, &stats);
2970 facet->installed = false;
2972 assert(facet->dp_packet_count == 0);
2973 assert(facet->dp_byte_count == 0);
2977 /* Returns true if the only action for 'facet' is to send to the controller.
2978 * (We don't report NetFlow expiration messages for such facets because they
2979 * are just part of the control logic for the network, not real traffic). */
2981 facet_is_controller_flow(struct facet *facet)
2984 && facet->rule->up.n_actions == 1
2985 && action_outputs_to_port(&facet->rule->up.actions[0],
2986 htons(OFPP_CONTROLLER)));
2989 /* Resets 'facet''s datapath statistics counters. This should be called when
2990 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2991 * it should contain the statistics returned by dpif when 'facet' was reset in
2992 * the datapath. 'stats' will be modified to only included statistics new
2993 * since 'facet' was last updated. */
2995 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2997 if (stats && facet->dp_packet_count <= stats->n_packets
2998 && facet->dp_byte_count <= stats->n_bytes) {
2999 stats->n_packets -= facet->dp_packet_count;
3000 stats->n_bytes -= facet->dp_byte_count;
3003 facet->dp_packet_count = 0;
3004 facet->dp_byte_count = 0;
3007 /* Folds all of 'facet''s statistics into its rule. Also updates the
3008 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3009 * 'facet''s statistics in the datapath should have been zeroed and folded into
3010 * its packet and byte counts before this function is called. */
3012 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3014 assert(!facet->dp_byte_count);
3015 assert(!facet->dp_packet_count);
3017 facet_push_stats(facet);
3018 facet_account(ofproto, facet);
3020 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3021 struct ofexpired expired;
3022 expired.flow = facet->flow;
3023 expired.packet_count = facet->packet_count;
3024 expired.byte_count = facet->byte_count;
3025 expired.used = facet->used;
3026 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3029 facet->rule->packet_count += facet->packet_count;
3030 facet->rule->byte_count += facet->byte_count;
3032 /* Reset counters to prevent double counting if 'facet' ever gets
3034 facet_reset_counters(facet);
3036 netflow_flow_clear(&facet->nf_flow);
3039 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3040 * Returns it if found, otherwise a null pointer.
3042 * The returned facet might need revalidation; use facet_lookup_valid()
3043 * instead if that is important. */
3044 static struct facet *
3045 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3047 struct facet *facet;
3049 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3051 if (flow_equal(flow, &facet->flow)) {
3059 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3060 * Returns it if found, otherwise a null pointer.
3062 * The returned facet is guaranteed to be valid. */
3063 static struct facet *
3064 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3066 struct facet *facet = facet_find(ofproto, flow);
3068 /* The facet we found might not be valid, since we could be in need of
3069 * revalidation. If it is not valid, don't return it. */
3071 && (ofproto->need_revalidate
3072 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3073 && !facet_revalidate(ofproto, facet)) {
3074 COVERAGE_INC(facet_invalidated);
3081 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3083 * - If the rule found is different from 'facet''s current rule, moves
3084 * 'facet' to the new rule and recompiles its actions.
3086 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3087 * where it is and recompiles its actions anyway.
3089 * - If there is none, destroys 'facet'.
3091 * Returns true if 'facet' still exists, false if it has been destroyed. */
3093 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3095 struct action_xlate_ctx ctx;
3096 struct ofpbuf *odp_actions;
3097 struct rule_dpif *new_rule;
3098 bool actions_changed;
3100 COVERAGE_INC(facet_revalidate);
3102 /* Determine the new rule. */
3103 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3105 /* No new rule, so delete the facet. */
3106 facet_remove(ofproto, facet);
3110 /* Calculate new datapath actions.
3112 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3113 * emit a NetFlow expiration and, if so, we need to have the old state
3114 * around to properly compose it. */
3115 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3116 odp_actions = xlate_actions(&ctx,
3117 new_rule->up.actions, new_rule->up.n_actions);
3118 actions_changed = (facet->actions_len != odp_actions->size
3119 || memcmp(facet->actions, odp_actions->data,
3120 facet->actions_len));
3122 /* If the datapath actions changed or the installability changed,
3123 * then we need to talk to the datapath. */
3124 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
3125 if (ctx.may_set_up_flow) {
3126 struct dpif_flow_stats stats;
3128 facet_put__(ofproto, facet,
3129 odp_actions->data, odp_actions->size, &stats);
3130 facet_update_stats(ofproto, facet, &stats);
3132 facet_uninstall(ofproto, facet);
3135 /* The datapath flow is gone or has zeroed stats, so push stats out of
3136 * 'facet' into 'rule'. */
3137 facet_flush_stats(ofproto, facet);
3140 /* Update 'facet' now that we've taken care of all the old state. */
3141 facet->tags = ctx.tags;
3142 facet->nf_flow.output_iface = ctx.nf_output_iface;
3143 facet->may_install = ctx.may_set_up_flow;
3144 facet->has_learn = ctx.has_learn;
3145 facet->has_normal = ctx.has_normal;
3146 if (actions_changed) {
3147 free(facet->actions);
3148 facet->actions_len = odp_actions->size;
3149 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
3151 if (facet->rule != new_rule) {
3152 COVERAGE_INC(facet_changed_rule);
3153 list_remove(&facet->list_node);
3154 list_push_back(&new_rule->facets, &facet->list_node);
3155 facet->rule = new_rule;
3156 facet->used = new_rule->up.created;
3157 facet->rs_used = facet->used;
3160 ofpbuf_delete(odp_actions);
3165 /* Updates 'facet''s used time. Caller is responsible for calling
3166 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3168 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3171 if (used > facet->used) {
3173 if (used > facet->rule->used) {
3174 facet->rule->used = used;
3176 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3180 /* Folds the statistics from 'stats' into the counters in 'facet'.
3182 * Because of the meaning of a facet's counters, it only makes sense to do this
3183 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3184 * packet that was sent by hand or if it represents statistics that have been
3185 * cleared out of the datapath. */
3187 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
3188 const struct dpif_flow_stats *stats)
3190 if (stats->n_packets || stats->used > facet->used) {
3191 facet_update_time(ofproto, facet, stats->used);
3192 facet->packet_count += stats->n_packets;
3193 facet->byte_count += stats->n_bytes;
3194 facet_push_stats(facet);
3195 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3200 facet_reset_counters(struct facet *facet)
3202 facet->packet_count = 0;
3203 facet->byte_count = 0;
3204 facet->rs_packet_count = 0;
3205 facet->rs_byte_count = 0;
3206 facet->accounted_bytes = 0;
3210 facet_push_stats(struct facet *facet)
3212 uint64_t rs_packets, rs_bytes;
3214 assert(facet->packet_count >= facet->rs_packet_count);
3215 assert(facet->byte_count >= facet->rs_byte_count);
3216 assert(facet->used >= facet->rs_used);
3218 rs_packets = facet->packet_count - facet->rs_packet_count;
3219 rs_bytes = facet->byte_count - facet->rs_byte_count;
3221 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3222 facet->rs_packet_count = facet->packet_count;
3223 facet->rs_byte_count = facet->byte_count;
3224 facet->rs_used = facet->used;
3226 flow_push_stats(facet->rule, &facet->flow,
3227 rs_packets, rs_bytes, facet->used);
3231 struct ofproto_push {
3232 struct action_xlate_ctx ctx;
3239 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3241 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3244 rule->packet_count += push->packets;
3245 rule->byte_count += push->bytes;
3246 rule->used = MAX(push->used, rule->used);
3250 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3251 * 'rule''s actions. */
3253 flow_push_stats(const struct rule_dpif *rule,
3254 struct flow *flow, uint64_t packets, uint64_t bytes,
3257 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3258 struct ofproto_push push;
3260 push.packets = packets;
3264 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3265 push.ctx.resubmit_hook = push_resubmit;
3266 ofpbuf_delete(xlate_actions(&push.ctx,
3267 rule->up.actions, rule->up.n_actions));
3272 static struct rule_dpif *
3273 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3276 struct cls_rule *cls_rule;
3277 struct classifier *cls;
3279 if (table_id >= N_TABLES) {
3283 cls = &ofproto->up.tables[table_id];
3284 if (flow->tos_frag & FLOW_FRAG_ANY
3285 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3286 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3287 * are unavailable. */
3288 struct flow ofpc_normal_flow = *flow;
3289 ofpc_normal_flow.tp_src = htons(0);
3290 ofpc_normal_flow.tp_dst = htons(0);
3291 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3293 cls_rule = classifier_lookup(cls, flow);
3295 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3299 complete_operation(struct rule_dpif *rule)
3301 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3303 rule_invalidate(rule);
3305 struct dpif_completion *c = xmalloc(sizeof *c);
3306 c->op = rule->up.pending;
3307 list_push_back(&ofproto->completions, &c->list_node);
3309 ofoperation_complete(rule->up.pending, 0);
3313 static struct rule *
3316 struct rule_dpif *rule = xmalloc(sizeof *rule);
3321 rule_dealloc(struct rule *rule_)
3323 struct rule_dpif *rule = rule_dpif_cast(rule_);
3328 rule_construct(struct rule *rule_)
3330 struct rule_dpif *rule = rule_dpif_cast(rule_);
3331 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3332 struct rule_dpif *victim;
3336 error = validate_actions(rule->up.actions, rule->up.n_actions,
3337 &rule->up.cr.flow, ofproto->max_ports);
3342 rule->used = rule->up.created;
3343 rule->packet_count = 0;
3344 rule->byte_count = 0;
3346 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3347 if (victim && !list_is_empty(&victim->facets)) {
3348 struct facet *facet;
3350 rule->facets = victim->facets;
3351 list_moved(&rule->facets);
3352 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3353 /* XXX: We're only clearing our local counters here. It's possible
3354 * that quite a few packets are unaccounted for in the datapath
3355 * statistics. These will be accounted to the new rule instead of
3356 * cleared as required. This could be fixed by clearing out the
3357 * datapath statistics for this facet, but currently it doesn't
3359 facet_reset_counters(facet);
3363 /* Must avoid list_moved() in this case. */
3364 list_init(&rule->facets);
3367 table_id = rule->up.table_id;
3368 rule->tag = (victim ? victim->tag
3370 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3371 ofproto->tables[table_id].basis));
3373 complete_operation(rule);
3378 rule_destruct(struct rule *rule_)
3380 struct rule_dpif *rule = rule_dpif_cast(rule_);
3381 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3382 struct facet *facet, *next_facet;
3384 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3385 facet_revalidate(ofproto, facet);
3388 complete_operation(rule);
3392 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3394 struct rule_dpif *rule = rule_dpif_cast(rule_);
3395 struct facet *facet;
3397 /* Start from historical data for 'rule' itself that are no longer tracked
3398 * in facets. This counts, for example, facets that have expired. */
3399 *packets = rule->packet_count;
3400 *bytes = rule->byte_count;
3402 /* Add any statistics that are tracked by facets. This includes
3403 * statistical data recently updated by ofproto_update_stats() as well as
3404 * stats for packets that were executed "by hand" via dpif_execute(). */
3405 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3406 *packets += facet->packet_count;
3407 *bytes += facet->byte_count;
3412 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
3414 struct rule_dpif *rule = rule_dpif_cast(rule_);
3415 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3416 struct action_xlate_ctx ctx;
3417 struct ofpbuf *odp_actions;
3418 struct facet *facet;
3421 /* First look for a related facet. If we find one, account it to that. */
3422 facet = facet_lookup_valid(ofproto, flow);
3423 if (facet && facet->rule == rule) {
3424 if (!facet->may_install) {
3425 facet_make_actions(ofproto, facet, packet);
3427 facet_execute(ofproto, facet, packet);
3431 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
3432 * create a new facet for it and use that. */
3433 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
3434 facet = facet_create(rule, flow);
3435 facet_make_actions(ofproto, facet, packet);
3436 facet_execute(ofproto, facet, packet);
3437 facet_install(ofproto, facet, true);
3441 /* We can't account anything to a facet. If we were to try, then that
3442 * facet would have a non-matching rule, busting our invariants. */
3443 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3444 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3445 size = packet->size;
3446 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3447 odp_actions->size, packet)) {
3448 rule->used = time_msec();
3449 rule->packet_count++;
3450 rule->byte_count += size;
3451 flow_push_stats(rule, flow, 1, size, rule->used);
3453 ofpbuf_delete(odp_actions);
3459 rule_modify_actions(struct rule *rule_)
3461 struct rule_dpif *rule = rule_dpif_cast(rule_);
3462 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3465 error = validate_actions(rule->up.actions, rule->up.n_actions,
3466 &rule->up.cr.flow, ofproto->max_ports);
3468 ofoperation_complete(rule->up.pending, error);
3472 complete_operation(rule);
3475 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.
3476 * Returns 0 if successful, otherwise a positive errno value. */
3478 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
3479 const struct ofpbuf *packet)
3481 struct ofpbuf key, odp_actions;
3482 struct odputil_keybuf keybuf;
3486 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
3487 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3488 odp_flow_key_from_flow(&key, &flow);
3490 ofpbuf_init(&odp_actions, 32);
3491 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3493 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3494 error = dpif_execute(ofproto->dpif,
3496 odp_actions.data, odp_actions.size,
3498 ofpbuf_uninit(&odp_actions);
3501 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3502 ofproto->up.name, odp_port, strerror(error));
3507 /* OpenFlow to datapath action translation. */
3509 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3510 struct action_xlate_ctx *ctx);
3511 static void xlate_normal(struct action_xlate_ctx *);
3514 put_userspace_action(const struct ofproto_dpif *ofproto,
3515 struct ofpbuf *odp_actions,
3516 const struct flow *flow,
3517 const struct user_action_cookie *cookie)
3522 pid = dpif_port_get_pid(ofproto->dpif,
3523 ofp_port_to_odp_port(flow->in_port));
3525 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3526 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3527 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3528 cookie, sizeof *cookie);
3529 nl_msg_end_nested(odp_actions, offset);
3531 return odp_actions->size - NLA_ALIGN(sizeof *cookie);
3534 /* Compose SAMPLE action for sFlow. */
3536 compose_sflow_action(const struct ofproto_dpif *ofproto,
3537 struct ofpbuf *odp_actions,
3538 const struct flow *flow,
3541 uint32_t port_ifindex;
3542 uint32_t probability;
3543 struct user_action_cookie cookie;
3544 size_t sample_offset, actions_offset;
3545 int cookie_offset, n_output;
3547 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3551 if (odp_port == OVSP_NONE) {
3555 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3559 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3561 /* Number of packets out of UINT_MAX to sample. */
3562 probability = dpif_sflow_get_probability(ofproto->sflow);
3563 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3565 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3567 cookie.type = USER_ACTION_COOKIE_SFLOW;
3568 cookie.data = port_ifindex;
3569 cookie.n_output = n_output;
3570 cookie.vlan_tci = 0;
3571 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3573 nl_msg_end_nested(odp_actions, actions_offset);
3574 nl_msg_end_nested(odp_actions, sample_offset);
3575 return cookie_offset;
3578 /* SAMPLE action must be first action in any given list of actions.
3579 * At this point we do not have all information required to build it. So try to
3580 * build sample action as complete as possible. */
3582 add_sflow_action(struct action_xlate_ctx *ctx)
3584 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3586 &ctx->flow, OVSP_NONE);
3587 ctx->sflow_odp_port = 0;
3588 ctx->sflow_n_outputs = 0;
3591 /* Fix SAMPLE action according to data collected while composing ODP actions.
3592 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3593 * USERSPACE action's user-cookie which is required for sflow. */
3595 fix_sflow_action(struct action_xlate_ctx *ctx)
3597 const struct flow *base = &ctx->base_flow;
3598 struct user_action_cookie *cookie;
3600 if (!ctx->user_cookie_offset) {
3604 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3606 assert(cookie != NULL);
3607 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3609 if (ctx->sflow_n_outputs) {
3610 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3611 ctx->sflow_odp_port);
3613 if (ctx->sflow_n_outputs >= 255) {
3614 cookie->n_output = 255;
3616 cookie->n_output = ctx->sflow_n_outputs;
3618 cookie->vlan_tci = base->vlan_tci;
3622 commit_action__(struct ofpbuf *odp_actions,
3623 enum ovs_action_attr act_type,
3624 enum ovs_key_attr key_type,
3625 const void *key, size_t key_size)
3627 size_t offset = nl_msg_start_nested(odp_actions, act_type);
3629 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3630 nl_msg_end_nested(odp_actions, offset);
3634 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3635 struct ofpbuf *odp_actions)
3637 if (base->tun_id == flow->tun_id) {
3640 base->tun_id = flow->tun_id;
3642 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3643 OVS_KEY_ATTR_TUN_ID, &base->tun_id, sizeof(base->tun_id));
3647 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3648 struct ofpbuf *odp_actions)
3650 struct ovs_key_ethernet eth_key;
3652 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3653 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3657 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3658 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3660 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3661 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3663 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3664 OVS_KEY_ATTR_ETHERNET, ð_key, sizeof(eth_key));
3668 commit_vlan_action(struct action_xlate_ctx *ctx, ovs_be16 new_tci)
3670 struct flow *base = &ctx->base_flow;
3672 if (base->vlan_tci == new_tci) {
3676 if (base->vlan_tci & htons(VLAN_CFI)) {
3677 nl_msg_put_u16(ctx->odp_actions, OVS_ACTION_ATTR_POP,
3678 OVS_KEY_ATTR_8021Q);
3681 if (new_tci & htons(VLAN_CFI)) {
3682 struct ovs_key_8021q q_key;
3684 q_key.q_tpid = htons(ETH_TYPE_VLAN);
3685 q_key.q_tci = new_tci & ~htons(VLAN_CFI);
3687 commit_action__(ctx->odp_actions, OVS_ACTION_ATTR_PUSH,
3688 OVS_KEY_ATTR_8021Q, &q_key, sizeof(q_key));
3690 base->vlan_tci = new_tci;
3694 commit_set_nw_action(const struct flow *flow, struct flow *base,
3695 struct ofpbuf *odp_actions)
3697 int frag = base->tos_frag & FLOW_FRAG_MASK;
3698 struct ovs_key_ipv4 ipv4_key;
3700 if (base->dl_type != htons(ETH_TYPE_IP) ||
3701 !base->nw_src || !base->nw_dst) {
3705 if (base->nw_src == flow->nw_src &&
3706 base->nw_dst == flow->nw_dst &&
3707 base->tos_frag == flow->tos_frag) {
3712 memset(&ipv4_key, 0, sizeof(ipv4_key));
3713 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3714 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3715 ipv4_key.ipv4_proto = base->nw_proto;
3716 ipv4_key.ipv4_tos = flow->tos_frag & IP_DSCP_MASK;
3717 ipv4_key.ipv4_frag = (frag == 0 ? OVS_FRAG_TYPE_NONE
3718 : frag == FLOW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
3719 : OVS_FRAG_TYPE_LATER);
3721 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3722 OVS_KEY_ATTR_IPV4, &ipv4_key, sizeof(ipv4_key));
3726 commit_set_port_action(const struct flow *flow, struct flow *base,
3727 struct ofpbuf *odp_actions)
3729 if (!base->tp_src || !base->tp_dst) {
3733 if (base->tp_src == flow->tp_src &&
3734 base->tp_dst == flow->tp_dst) {
3738 if (flow->nw_proto == IPPROTO_TCP) {
3739 struct ovs_key_tcp port_key;
3741 port_key.tcp_src = base->tp_src = flow->tp_src;
3742 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3744 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3745 OVS_KEY_ATTR_TCP, &port_key, sizeof(port_key));
3747 } else if (flow->nw_proto == IPPROTO_UDP) {
3748 struct ovs_key_udp port_key;
3750 port_key.udp_src = base->tp_src = flow->tp_src;
3751 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3753 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3754 OVS_KEY_ATTR_UDP, &port_key, sizeof(port_key));
3759 commit_priority_action(struct action_xlate_ctx *ctx)
3761 if (ctx->base_priority == ctx->priority) {
3765 if (ctx->priority) {
3766 nl_msg_put_u32(ctx->odp_actions,
3767 OVS_ACTION_ATTR_SET_PRIORITY, ctx->priority);
3769 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
3771 ctx->base_priority = ctx->priority;
3775 commit_odp_actions(struct action_xlate_ctx *ctx)
3777 const struct flow *flow = &ctx->flow;
3778 struct flow *base = &ctx->base_flow;
3779 struct ofpbuf *odp_actions = ctx->odp_actions;
3781 commit_set_tun_id_action(flow, base, odp_actions);
3782 commit_set_ether_addr_action(flow, base, odp_actions);
3783 commit_vlan_action(ctx, flow->vlan_tci);
3784 commit_set_nw_action(flow, base, odp_actions);
3785 commit_set_port_action(flow, base, odp_actions);
3786 commit_priority_action(ctx);
3790 compose_output_action(struct action_xlate_ctx *ctx, uint16_t odp_port)
3792 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3793 ctx->sflow_odp_port = odp_port;
3794 ctx->sflow_n_outputs++;
3798 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
3800 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
3801 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
3804 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
3805 || !stp_forward_in_state(ofport->stp_state)) {
3806 /* Forwarding disabled on port. */
3811 * We don't have an ofport record for this port, but it doesn't hurt to
3812 * allow forwarding to it anyhow. Maybe such a port will appear later
3813 * and we're pre-populating the flow table.
3817 commit_odp_actions(ctx);
3818 compose_output_action(ctx, odp_port);
3819 ctx->nf_output_iface = ofp_port;
3823 xlate_table_action(struct action_xlate_ctx *ctx,
3824 uint16_t in_port, uint8_t table_id)
3826 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3827 struct ofproto_dpif *ofproto = ctx->ofproto;
3828 struct rule_dpif *rule;
3829 uint16_t old_in_port;
3830 uint8_t old_table_id;
3832 old_table_id = ctx->table_id;
3833 ctx->table_id = table_id;
3835 /* Look up a flow with 'in_port' as the input port. */
3836 old_in_port = ctx->flow.in_port;
3837 ctx->flow.in_port = in_port;
3838 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3841 if (table_id > 0 && table_id < N_TABLES) {
3842 struct table_dpif *table = &ofproto->tables[table_id];
3843 if (table->other_table) {
3846 : rule_calculate_tag(&ctx->flow,
3847 &table->other_table->wc,
3852 /* Restore the original input port. Otherwise OFPP_NORMAL and
3853 * OFPP_IN_PORT will have surprising behavior. */
3854 ctx->flow.in_port = old_in_port;
3856 if (ctx->resubmit_hook) {
3857 ctx->resubmit_hook(ctx, rule);
3862 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3866 ctx->table_id = old_table_id;
3868 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3870 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3871 MAX_RESUBMIT_RECURSION);
3876 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3877 const struct nx_action_resubmit *nar)
3882 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3884 : ntohs(nar->in_port));
3885 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3887 xlate_table_action(ctx, in_port, table_id);
3891 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3893 struct ofport_dpif *ofport;
3895 commit_odp_actions(ctx);
3896 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3897 uint16_t ofp_port = ofport->up.ofp_port;
3898 if (ofp_port != ctx->flow.in_port
3899 && !(ofport->up.opp.config & mask)
3900 && stp_forward_in_state(ofport->stp_state)) {
3901 compose_output_action(ctx, ofport->odp_port);
3905 ctx->nf_output_iface = NF_OUT_FLOOD;
3909 compose_controller_action(struct action_xlate_ctx *ctx, int len)
3911 struct user_action_cookie cookie;
3913 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
3915 cookie.n_output = 0;
3916 cookie.vlan_tci = 0;
3917 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
3921 xlate_output_action__(struct action_xlate_ctx *ctx,
3922 uint16_t port, uint16_t max_len)
3924 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3926 ctx->nf_output_iface = NF_OUT_DROP;
3930 add_output_action(ctx, ctx->flow.in_port);
3933 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3939 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3942 flood_packets(ctx, htonl(0));
3944 case OFPP_CONTROLLER:
3945 commit_odp_actions(ctx);
3946 compose_controller_action(ctx, max_len);
3949 add_output_action(ctx, OFPP_LOCAL);
3954 if (port != ctx->flow.in_port) {
3955 add_output_action(ctx, port);
3960 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3961 ctx->nf_output_iface = NF_OUT_FLOOD;
3962 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3963 ctx->nf_output_iface = prev_nf_output_iface;
3964 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3965 ctx->nf_output_iface != NF_OUT_FLOOD) {
3966 ctx->nf_output_iface = NF_OUT_MULTI;
3971 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3972 const struct nx_action_output_reg *naor)
3976 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3978 if (ofp_port <= UINT16_MAX) {
3979 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3984 xlate_output_action(struct action_xlate_ctx *ctx,
3985 const struct ofp_action_output *oao)
3987 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3991 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3992 const struct ofp_action_enqueue *oae)
3994 uint16_t ofp_port, odp_port;
3995 uint32_t ctx_priority, priority;
3998 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4001 /* Fall back to ordinary output action. */
4002 xlate_output_action__(ctx, ntohs(oae->port), 0);
4006 /* Figure out datapath output port. */
4007 ofp_port = ntohs(oae->port);
4008 if (ofp_port == OFPP_IN_PORT) {
4009 ofp_port = ctx->flow.in_port;
4010 } else if (ofp_port == ctx->flow.in_port) {
4013 odp_port = ofp_port_to_odp_port(ofp_port);
4015 /* Add datapath actions. */
4016 ctx_priority = ctx->priority;
4017 ctx->priority = priority;
4018 add_output_action(ctx, odp_port);
4019 ctx->priority = ctx_priority;
4021 /* Update NetFlow output port. */
4022 if (ctx->nf_output_iface == NF_OUT_DROP) {
4023 ctx->nf_output_iface = odp_port;
4024 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4025 ctx->nf_output_iface = NF_OUT_MULTI;
4030 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4031 const struct nx_action_set_queue *nasq)
4036 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4039 /* Couldn't translate queue to a priority, so ignore. A warning
4040 * has already been logged. */
4044 ctx->priority = priority;
4047 struct xlate_reg_state {
4053 xlate_autopath(struct action_xlate_ctx *ctx,
4054 const struct nx_action_autopath *naa)
4056 uint16_t ofp_port = ntohl(naa->id);
4057 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4059 if (!port || !port->bundle) {
4060 ofp_port = OFPP_NONE;
4061 } else if (port->bundle->bond) {
4062 /* Autopath does not support VLAN hashing. */
4063 struct ofport_dpif *slave = bond_choose_output_slave(
4064 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4066 ofp_port = slave->up.ofp_port;
4069 autopath_execute(naa, &ctx->flow, ofp_port);
4073 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4075 struct ofproto_dpif *ofproto = ofproto_;
4076 struct ofport_dpif *port;
4086 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4089 port = get_ofp_port(ofproto, ofp_port);
4090 return port ? port->may_enable : false;
4095 xlate_learn_action(struct action_xlate_ctx *ctx,
4096 const struct nx_action_learn *learn)
4098 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4099 struct ofputil_flow_mod fm;
4102 learn_execute(learn, &ctx->flow, &fm);
4104 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4105 if (error && !VLOG_DROP_WARN(&rl)) {
4106 char *msg = ofputil_error_to_string(error);
4107 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4115 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4117 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4118 ? htonl(OFPPC_NO_RECV_STP)
4119 : htonl(OFPPC_NO_RECV))) {
4123 /* Only drop packets here if both forwarding and learning are
4124 * disabled. If just learning is enabled, we need to have
4125 * OFPP_NORMAL and the learning action have a look at the packet
4126 * before we can drop it. */
4127 if (!stp_forward_in_state(port->stp_state)
4128 && !stp_learn_in_state(port->stp_state)) {
4136 do_xlate_actions(const union ofp_action *in, size_t n_in,
4137 struct action_xlate_ctx *ctx)
4139 const struct ofport_dpif *port;
4140 const union ofp_action *ia;
4143 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4144 if (port && !may_receive(port, ctx)) {
4145 /* Drop this flow. */
4149 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4150 const struct ofp_action_dl_addr *oada;
4151 const struct nx_action_resubmit *nar;
4152 const struct nx_action_set_tunnel *nast;
4153 const struct nx_action_set_queue *nasq;
4154 const struct nx_action_multipath *nam;
4155 const struct nx_action_autopath *naa;
4156 const struct nx_action_bundle *nab;
4157 const struct nx_action_output_reg *naor;
4158 enum ofputil_action_code code;
4161 code = ofputil_decode_action_unsafe(ia);
4163 case OFPUTIL_OFPAT_OUTPUT:
4164 xlate_output_action(ctx, &ia->output);
4167 case OFPUTIL_OFPAT_SET_VLAN_VID:
4168 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4169 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4172 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4173 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4174 ctx->flow.vlan_tci |= htons(
4175 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4178 case OFPUTIL_OFPAT_STRIP_VLAN:
4179 ctx->flow.vlan_tci = htons(0);
4182 case OFPUTIL_OFPAT_SET_DL_SRC:
4183 oada = ((struct ofp_action_dl_addr *) ia);
4184 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4187 case OFPUTIL_OFPAT_SET_DL_DST:
4188 oada = ((struct ofp_action_dl_addr *) ia);
4189 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4192 case OFPUTIL_OFPAT_SET_NW_SRC:
4193 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4196 case OFPUTIL_OFPAT_SET_NW_DST:
4197 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4200 case OFPUTIL_OFPAT_SET_NW_TOS:
4201 ctx->flow.tos_frag &= ~IP_DSCP_MASK;
4202 ctx->flow.tos_frag |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4205 case OFPUTIL_OFPAT_SET_TP_SRC:
4206 ctx->flow.tp_src = ia->tp_port.tp_port;
4209 case OFPUTIL_OFPAT_SET_TP_DST:
4210 ctx->flow.tp_dst = ia->tp_port.tp_port;
4213 case OFPUTIL_OFPAT_ENQUEUE:
4214 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4217 case OFPUTIL_NXAST_RESUBMIT:
4218 nar = (const struct nx_action_resubmit *) ia;
4219 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4222 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4223 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4226 case OFPUTIL_NXAST_SET_TUNNEL:
4227 nast = (const struct nx_action_set_tunnel *) ia;
4228 tun_id = htonll(ntohl(nast->tun_id));
4229 ctx->flow.tun_id = tun_id;
4232 case OFPUTIL_NXAST_SET_QUEUE:
4233 nasq = (const struct nx_action_set_queue *) ia;
4234 xlate_set_queue_action(ctx, nasq);
4237 case OFPUTIL_NXAST_POP_QUEUE:
4241 case OFPUTIL_NXAST_REG_MOVE:
4242 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4246 case OFPUTIL_NXAST_REG_LOAD:
4247 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4251 case OFPUTIL_NXAST_NOTE:
4252 /* Nothing to do. */
4255 case OFPUTIL_NXAST_SET_TUNNEL64:
4256 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4257 ctx->flow.tun_id = tun_id;
4260 case OFPUTIL_NXAST_MULTIPATH:
4261 nam = (const struct nx_action_multipath *) ia;
4262 multipath_execute(nam, &ctx->flow);
4265 case OFPUTIL_NXAST_AUTOPATH:
4266 naa = (const struct nx_action_autopath *) ia;
4267 xlate_autopath(ctx, naa);
4270 case OFPUTIL_NXAST_BUNDLE:
4271 ctx->ofproto->has_bundle_action = true;
4272 nab = (const struct nx_action_bundle *) ia;
4273 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4278 case OFPUTIL_NXAST_BUNDLE_LOAD:
4279 ctx->ofproto->has_bundle_action = true;
4280 nab = (const struct nx_action_bundle *) ia;
4281 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4285 case OFPUTIL_NXAST_OUTPUT_REG:
4286 naor = (const struct nx_action_output_reg *) ia;
4287 xlate_output_reg_action(ctx, naor);
4290 case OFPUTIL_NXAST_LEARN:
4291 ctx->has_learn = true;
4292 if (ctx->may_learn) {
4293 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4299 /* We've let OFPP_NORMAL and the learning action look at the packet,
4300 * so drop it now if forwarding is disabled. */
4301 if (port && !stp_forward_in_state(port->stp_state)) {
4302 ofpbuf_clear(ctx->odp_actions);
4303 add_sflow_action(ctx);
4308 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4309 struct ofproto_dpif *ofproto, const struct flow *flow,
4310 const struct ofpbuf *packet)
4312 ctx->ofproto = ofproto;
4314 ctx->packet = packet;
4315 ctx->may_learn = packet != NULL;
4316 ctx->resubmit_hook = NULL;
4319 static struct ofpbuf *
4320 xlate_actions(struct action_xlate_ctx *ctx,
4321 const union ofp_action *in, size_t n_in)
4323 COVERAGE_INC(ofproto_dpif_xlate);
4325 ctx->odp_actions = ofpbuf_new(512);
4326 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4328 ctx->may_set_up_flow = true;
4329 ctx->has_learn = false;
4330 ctx->has_normal = false;
4331 ctx->nf_output_iface = NF_OUT_DROP;
4334 ctx->base_priority = 0;
4335 ctx->base_flow = ctx->flow;
4336 ctx->base_flow.tun_id = 0;
4339 if (ctx->flow.tos_frag & FLOW_FRAG_ANY) {
4340 switch (ctx->ofproto->up.frag_handling) {
4341 case OFPC_FRAG_NORMAL:
4342 /* We must pretend that transport ports are unavailable. */
4343 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4344 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4347 case OFPC_FRAG_DROP:
4348 return ctx->odp_actions;
4350 case OFPC_FRAG_REASM:
4353 case OFPC_FRAG_NX_MATCH:
4354 /* Nothing to do. */
4359 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4360 ctx->may_set_up_flow = false;
4361 return ctx->odp_actions;
4363 add_sflow_action(ctx);
4364 do_xlate_actions(in, n_in, ctx);
4366 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4367 ctx->odp_actions->data,
4368 ctx->odp_actions->size)) {
4369 ctx->may_set_up_flow = false;
4371 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4373 compose_output_action(ctx, OVSP_LOCAL);
4376 fix_sflow_action(ctx);
4379 return ctx->odp_actions;
4382 /* OFPP_NORMAL implementation. */
4385 struct ofport_dpif *port;
4390 struct dst builtin[32];
4392 size_t n, allocated;
4395 static void dst_set_init(struct dst_set *);
4396 static void dst_set_add(struct dst_set *, const struct dst *);
4397 static void dst_set_free(struct dst_set *);
4399 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4401 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4402 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4403 * the bundle on which the packet was received, returns the VLAN to which the
4406 * Both 'vid' and the return value are in the range 0...4095. */
4408 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4410 switch (in_bundle->vlan_mode) {
4411 case PORT_VLAN_ACCESS:
4412 return in_bundle->vlan;
4415 case PORT_VLAN_TRUNK:
4418 case PORT_VLAN_NATIVE_UNTAGGED:
4419 case PORT_VLAN_NATIVE_TAGGED:
4420 return vid ? vid : in_bundle->vlan;
4427 /* Given 'vlan', the VLAN that a packet belongs to, and
4428 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4429 * that should be included in the 802.1Q header. (If the return value is 0,
4430 * then the 802.1Q header should only be included in the packet if there is a
4433 * Both 'vlan' and the return value are in the range 0...4095. */
4435 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4437 switch (out_bundle->vlan_mode) {
4438 case PORT_VLAN_ACCESS:
4441 case PORT_VLAN_TRUNK:
4442 case PORT_VLAN_NATIVE_TAGGED:
4445 case PORT_VLAN_NATIVE_UNTAGGED:
4446 return vlan == out_bundle->vlan ? 0 : vlan;
4454 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
4455 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
4459 vlan = input_vid_to_vlan(in_bundle, vlan_tci_to_vid(ctx->flow.vlan_tci));
4460 dst->vid = output_vlan_to_vid(out_bundle, vlan);
4462 dst->port = (!out_bundle->bond
4463 ? ofbundle_get_a_port(out_bundle)
4464 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4465 dst->vid, &ctx->tags));
4466 return dst->port != NULL;
4470 mirror_mask_ffs(mirror_mask_t mask)
4472 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4477 dst_set_init(struct dst_set *set)
4479 set->dsts = set->builtin;
4481 set->allocated = ARRAY_SIZE(set->builtin);
4485 dst_set_add(struct dst_set *set, const struct dst *dst)
4487 if (set->n >= set->allocated) {
4488 size_t new_allocated;
4489 struct dst *new_dsts;
4491 new_allocated = set->allocated * 2;
4492 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
4493 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
4497 set->dsts = new_dsts;
4498 set->allocated = new_allocated;
4500 set->dsts[set->n++] = *dst;
4504 dst_set_free(struct dst_set *set)
4506 if (set->dsts != set->builtin) {
4512 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
4515 for (i = 0; i < set->n; i++) {
4516 if (set->dsts[i].vid == test->vid
4517 && set->dsts[i].port == test->port) {
4525 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4527 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4528 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4532 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4534 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4537 /* Returns an arbitrary interface within 'bundle'. */
4538 static struct ofport_dpif *
4539 ofbundle_get_a_port(const struct ofbundle *bundle)
4541 return CONTAINER_OF(list_front(&bundle->ports),
4542 struct ofport_dpif, bundle_node);
4546 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4547 const struct ofbundle *in_bundle,
4548 const struct ofbundle *out_bundle, struct dst_set *set)
4552 if (out_bundle == OFBUNDLE_FLOOD) {
4553 struct ofbundle *bundle;
4555 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4556 if (bundle != in_bundle
4557 && ofbundle_includes_vlan(bundle, vlan)
4558 && bundle->floodable
4559 && !bundle->mirror_out
4560 && set_dst(ctx, &dst, in_bundle, bundle)) {
4561 dst_set_add(set, &dst);
4564 ctx->nf_output_iface = NF_OUT_FLOOD;
4565 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
4566 dst_set_add(set, &dst);
4567 ctx->nf_output_iface = dst.port->odp_port;
4572 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4574 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4577 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4578 * to a VLAN. In general most packets may be mirrored but we want to drop
4579 * protocols that may confuse switches. */
4581 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4583 /* If you change this function's behavior, please update corresponding
4584 * documentation in vswitch.xml at the same time. */
4585 if (dst[0] != 0x01) {
4586 /* All the currently banned MACs happen to start with 01 currently, so
4587 * this is a quick way to eliminate most of the good ones. */
4589 if (eth_addr_is_reserved(dst)) {
4590 /* Drop STP, IEEE pause frames, and other reserved protocols
4591 * (01-80-c2-00-00-0x). */
4595 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4597 if ((dst[3] & 0xfe) == 0xcc &&
4598 (dst[4] & 0xfe) == 0xcc &&
4599 (dst[5] & 0xfe) == 0xcc) {
4600 /* Drop the following protocols plus others following the same
4603 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4604 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4605 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4609 if (!(dst[3] | dst[4] | dst[5])) {
4610 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4619 compose_mirror_dsts(struct action_xlate_ctx *ctx,
4620 uint16_t vlan, const struct ofbundle *in_bundle,
4621 struct dst_set *set)
4623 struct ofproto_dpif *ofproto = ctx->ofproto;
4624 mirror_mask_t mirrors;
4628 mirrors = in_bundle->src_mirrors;
4629 for (i = 0; i < set->n; i++) {
4630 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
4637 flow_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4639 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4640 if (vlan_is_mirrored(m, vlan)) {
4644 if (set_dst(ctx, &dst, in_bundle, m->out)
4645 && !dst_is_duplicate(set, &dst)) {
4646 dst_set_add(set, &dst);
4648 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
4649 struct ofbundle *bundle;
4651 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4652 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4653 && set_dst(ctx, &dst, in_bundle, bundle))
4655 /* set_dst() got dst->vid from the input packet's VLAN,
4656 * not from m->out_vlan, so recompute it. */
4657 dst.vid = output_vlan_to_vid(bundle, m->out_vlan);
4659 if (dst_is_duplicate(set, &dst)) {
4663 if (bundle == in_bundle && dst.vid == flow_vid) {
4664 /* Don't send out input port on same VLAN. */
4667 dst_set_add(set, &dst);
4672 mirrors &= mirrors - 1;
4677 compose_dst_output_action(struct action_xlate_ctx *ctx, const struct dst *dst)
4681 tci = htons(dst->vid);
4683 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4684 tci |= htons(VLAN_CFI);
4686 commit_vlan_action(ctx, tci);
4688 compose_output_action(ctx, dst->port->odp_port);
4692 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
4693 const struct ofbundle *in_bundle,
4694 const struct ofbundle *out_bundle)
4696 uint16_t initial_vid;
4697 const struct dst *dst;
4701 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
4702 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
4708 /* Output all the packets we can without having to change the VLAN. */
4709 commit_odp_actions(ctx);
4710 initial_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4711 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4712 if (dst->vid == initial_vid) {
4713 compose_dst_output_action(ctx, dst);
4717 /* Then output the rest. */
4718 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4719 if (dst->vid != initial_vid) {
4720 compose_dst_output_action(ctx, dst);
4727 /* Returns the effective vlan of a packet, taking into account both the
4728 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
4729 * the packet is untagged and -1 indicates it has an invalid header and
4730 * should be dropped. */
4732 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
4733 struct ofbundle *in_bundle, bool have_packet)
4735 int vlan = vlan_tci_to_vid(flow->vlan_tci);
4737 if (in_bundle->vlan_mode == PORT_VLAN_ACCESS) {
4738 /* Drop tagged packet on access port */
4740 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4741 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4742 "packet received on port %s configured with "
4743 "implicit VLAN %"PRIu16,
4744 ofproto->up.name, vlan,
4745 in_bundle->name, in_bundle->vlan);
4748 } else if (ofbundle_includes_vlan(in_bundle, vlan)) {
4751 /* Drop packets from a VLAN not member of the trunk */
4753 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4754 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4755 "packet received on port %s not configured for "
4757 ofproto->up.name, vlan, in_bundle->name, vlan);
4762 if (in_bundle->vlan_mode != PORT_VLAN_TRUNK) {
4763 return in_bundle->vlan;
4765 return ofbundle_includes_vlan(in_bundle, 0) ? 0 : -1;
4770 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4771 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4772 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4774 is_gratuitous_arp(const struct flow *flow)
4776 return (flow->dl_type == htons(ETH_TYPE_ARP)
4777 && eth_addr_is_broadcast(flow->dl_dst)
4778 && (flow->nw_proto == ARP_OP_REPLY
4779 || (flow->nw_proto == ARP_OP_REQUEST
4780 && flow->nw_src == flow->nw_dst)));
4784 update_learning_table(struct ofproto_dpif *ofproto,
4785 const struct flow *flow, int vlan,
4786 struct ofbundle *in_bundle)
4788 struct mac_entry *mac;
4790 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4794 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4795 if (is_gratuitous_arp(flow)) {
4796 /* We don't want to learn from gratuitous ARP packets that are
4797 * reflected back over bond slaves so we lock the learning table. */
4798 if (!in_bundle->bond) {
4799 mac_entry_set_grat_arp_lock(mac);
4800 } else if (mac_entry_is_grat_arp_locked(mac)) {
4805 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4806 /* The log messages here could actually be useful in debugging,
4807 * so keep the rate limit relatively high. */
4808 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4809 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4810 "on port %s in VLAN %d",
4811 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4812 in_bundle->name, vlan);
4814 mac->port.p = in_bundle;
4815 tag_set_add(&ofproto->revalidate_set,
4816 mac_learning_changed(ofproto->ml, mac));
4820 /* Determines whether packets in 'flow' within 'br' should be forwarded or
4821 * dropped. Returns true if they may be forwarded, false if they should be
4824 * If 'have_packet' is true, it indicates that the caller is processing a
4825 * received packet. If 'have_packet' is false, then the caller is just
4826 * revalidating an existing flow because configuration has changed. Either
4827 * way, 'have_packet' only affects logging (there is no point in logging errors
4828 * during revalidation).
4830 * Sets '*in_portp' to the input port. This will be a null pointer if
4831 * flow->in_port does not designate a known input port (in which case
4832 * is_admissible() returns false).
4834 * When returning true, sets '*vlanp' to the effective VLAN of the input
4835 * packet, as returned by flow_get_vlan().
4837 * May also add tags to '*tags', although the current implementation only does
4838 * so in one special case.
4841 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
4843 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
4845 struct ofport_dpif *in_port;
4846 struct ofbundle *in_bundle;
4849 /* Find the port and bundle for the received packet. */
4850 in_port = get_ofp_port(ofproto, flow->in_port);
4851 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
4852 if (!in_port || !in_bundle) {
4853 /* No interface? Something fishy... */
4855 /* Odd. A few possible reasons here:
4857 * - We deleted a port but there are still a few packets queued up
4860 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4861 * we don't know about.
4863 * - Packet arrived on the local port but the local port is not
4866 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4868 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4870 ofproto->up.name, flow->in_port);
4875 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
4880 /* Drop frames for reserved multicast addresses only if forward_bpdu
4881 * option is absent. */
4882 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
4886 /* Drop frames on bundles reserved for mirroring. */
4887 if (in_bundle->mirror_out) {
4889 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4890 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4891 "%s, which is reserved exclusively for mirroring",
4892 ofproto->up.name, in_bundle->name);
4897 if (in_bundle->bond) {
4898 struct mac_entry *mac;
4900 switch (bond_check_admissibility(in_bundle->bond, in_port,
4901 flow->dl_dst, tags)) {
4908 case BV_DROP_IF_MOVED:
4909 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
4910 if (mac && mac->port.p != in_bundle &&
4911 (!is_gratuitous_arp(flow)
4912 || mac_entry_is_grat_arp_locked(mac))) {
4923 xlate_normal(struct action_xlate_ctx *ctx)
4925 struct ofbundle *in_bundle;
4926 struct ofbundle *out_bundle;
4927 struct mac_entry *mac;
4930 ctx->has_normal = true;
4932 /* Check whether we should drop packets in this flow. */
4933 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
4934 &ctx->tags, &vlan, &in_bundle)) {
4939 /* Learn source MAC. */
4940 if (ctx->may_learn) {
4941 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
4944 /* Determine output bundle. */
4945 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
4948 out_bundle = mac->port.p;
4949 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4950 /* If we are revalidating but don't have a learning entry then eject
4951 * the flow. Installing a flow that floods packets opens up a window
4952 * of time where we could learn from a packet reflected on a bond and
4953 * blackhole packets before the learning table is updated to reflect
4954 * the correct port. */
4955 ctx->may_set_up_flow = false;
4958 out_bundle = OFBUNDLE_FLOOD;
4961 /* Don't send packets out their input bundles. */
4962 if (in_bundle == out_bundle) {
4968 compose_actions(ctx, vlan, in_bundle, out_bundle);
4972 /* Optimized flow revalidation.
4974 * It's a difficult problem, in general, to tell which facets need to have
4975 * their actions recalculated whenever the OpenFlow flow table changes. We
4976 * don't try to solve that general problem: for most kinds of OpenFlow flow
4977 * table changes, we recalculate the actions for every facet. This is
4978 * relatively expensive, but it's good enough if the OpenFlow flow table
4979 * doesn't change very often.
4981 * However, we can expect one particular kind of OpenFlow flow table change to
4982 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4983 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4984 * table, we add a special case that applies to flow tables in which every rule
4985 * has the same form (that is, the same wildcards), except that the table is
4986 * also allowed to have a single "catch-all" flow that matches all packets. We
4987 * optimize this case by tagging all of the facets that resubmit into the table
4988 * and invalidating the same tag whenever a flow changes in that table. The
4989 * end result is that we revalidate just the facets that need it (and sometimes
4990 * a few more, but not all of the facets or even all of the facets that
4991 * resubmit to the table modified by MAC learning). */
4993 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
4994 * into an OpenFlow table with the given 'basis'. */
4996 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
4999 if (flow_wildcards_is_catchall(wc)) {
5002 struct flow tag_flow = *flow;
5003 flow_zero_wildcards(&tag_flow, wc);
5004 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5008 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5009 * taggability of that table.
5011 * This function must be called after *each* change to a flow table. If you
5012 * skip calling it on some changes then the pointer comparisons at the end can
5013 * be invalid if you get unlucky. For example, if a flow removal causes a
5014 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5015 * different wildcards to be created with the same address, then this function
5016 * will incorrectly skip revalidation. */
5018 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5020 struct table_dpif *table = &ofproto->tables[table_id];
5021 const struct classifier *cls = &ofproto->up.tables[table_id];
5022 struct cls_table *catchall, *other;
5023 struct cls_table *t;
5025 catchall = other = NULL;
5027 switch (hmap_count(&cls->tables)) {
5029 /* We could tag this OpenFlow table but it would make the logic a
5030 * little harder and it's a corner case that doesn't seem worth it
5036 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5037 if (cls_table_is_catchall(t)) {
5039 } else if (!other) {
5042 /* Indicate that we can't tag this by setting both tables to
5043 * NULL. (We know that 'catchall' is already NULL.) */
5050 /* Can't tag this table. */
5054 if (table->catchall_table != catchall || table->other_table != other) {
5055 table->catchall_table = catchall;
5056 table->other_table = other;
5057 ofproto->need_revalidate = true;
5061 /* Given 'rule' that has changed in some way (either it is a rule being
5062 * inserted, a rule being deleted, or a rule whose actions are being
5063 * modified), marks facets for revalidation to ensure that packets will be
5064 * forwarded correctly according to the new state of the flow table.
5066 * This function must be called after *each* change to a flow table. See
5067 * the comment on table_update_taggable() for more information. */
5069 rule_invalidate(const struct rule_dpif *rule)
5071 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5073 table_update_taggable(ofproto, rule->up.table_id);
5075 if (!ofproto->need_revalidate) {
5076 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5078 if (table->other_table && rule->tag) {
5079 tag_set_add(&ofproto->revalidate_set, rule->tag);
5081 ofproto->need_revalidate = true;
5087 set_frag_handling(struct ofproto *ofproto_,
5088 enum ofp_config_flags frag_handling)
5090 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5092 if (frag_handling != OFPC_FRAG_REASM) {
5093 ofproto->need_revalidate = true;
5101 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5102 const struct flow *flow,
5103 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5105 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5108 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5109 ofproto->max_ports);
5111 struct odputil_keybuf keybuf;
5112 struct action_xlate_ctx ctx;
5113 struct ofpbuf *odp_actions;
5116 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5117 odp_flow_key_from_flow(&key, flow);
5119 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
5120 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5121 dpif_execute(ofproto->dpif, key.data, key.size,
5122 odp_actions->data, odp_actions->size, packet);
5123 ofpbuf_delete(odp_actions);
5129 get_netflow_ids(const struct ofproto *ofproto_,
5130 uint8_t *engine_type, uint8_t *engine_id)
5132 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5134 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5137 static struct ofproto_dpif *
5138 ofproto_dpif_lookup(const char *name)
5140 struct ofproto *ofproto = ofproto_lookup(name);
5141 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5142 ? ofproto_dpif_cast(ofproto)
5147 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5148 const char *args, void *aux OVS_UNUSED)
5150 struct ds ds = DS_EMPTY_INITIALIZER;
5151 const struct ofproto_dpif *ofproto;
5152 const struct mac_entry *e;
5154 ofproto = ofproto_dpif_lookup(args);
5156 unixctl_command_reply(conn, 501, "no such bridge");
5160 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5161 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5162 struct ofbundle *bundle = e->port.p;
5163 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5164 ofbundle_get_a_port(bundle)->odp_port,
5165 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5167 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5171 struct ofproto_trace {
5172 struct action_xlate_ctx ctx;
5178 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5179 const struct rule_dpif *rule)
5181 ds_put_char_multiple(result, '\t', level);
5183 ds_put_cstr(result, "No match\n");
5187 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5188 table_id, ntohll(rule->up.flow_cookie));
5189 cls_rule_format(&rule->up.cr, result);
5190 ds_put_char(result, '\n');
5192 ds_put_char_multiple(result, '\t', level);
5193 ds_put_cstr(result, "OpenFlow ");
5194 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5195 ds_put_char(result, '\n');
5199 trace_format_flow(struct ds *result, int level, const char *title,
5200 struct ofproto_trace *trace)
5202 ds_put_char_multiple(result, '\t', level);
5203 ds_put_format(result, "%s: ", title);
5204 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5205 ds_put_cstr(result, "unchanged");
5207 flow_format(result, &trace->ctx.flow);
5208 trace->flow = trace->ctx.flow;
5210 ds_put_char(result, '\n');
5214 trace_format_regs(struct ds *result, int level, const char *title,
5215 struct ofproto_trace *trace)
5219 ds_put_char_multiple(result, '\t', level);
5220 ds_put_format(result, "%s:", title);
5221 for (i = 0; i < FLOW_N_REGS; i++) {
5222 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5224 ds_put_char(result, '\n');
5228 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5230 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5231 struct ds *result = trace->result;
5233 ds_put_char(result, '\n');
5234 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5235 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5236 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5240 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5241 void *aux OVS_UNUSED)
5243 char *dpname, *arg1, *arg2, *arg3;
5244 char *args = xstrdup(args_);
5245 char *save_ptr = NULL;
5246 struct ofproto_dpif *ofproto;
5247 struct ofpbuf odp_key;
5248 struct ofpbuf *packet;
5249 struct rule_dpif *rule;
5255 ofpbuf_init(&odp_key, 0);
5258 dpname = strtok_r(args, " ", &save_ptr);
5259 arg1 = strtok_r(NULL, " ", &save_ptr);
5260 arg2 = strtok_r(NULL, " ", &save_ptr);
5261 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5262 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5263 /* ofproto/trace dpname flow [-generate] */
5266 /* Convert string to datapath key. */
5267 ofpbuf_init(&odp_key, 0);
5268 error = odp_flow_key_from_string(arg1, &odp_key);
5270 unixctl_command_reply(conn, 501, "Bad flow syntax");
5274 /* Convert odp_key to flow. */
5275 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
5277 unixctl_command_reply(conn, 501, "Invalid flow");
5281 /* Generate a packet, if requested. */
5283 packet = ofpbuf_new(0);
5284 flow_compose(packet, &flow);
5286 } else if (dpname && arg1 && arg2 && arg3) {
5287 /* ofproto/trace dpname tun_id in_port packet */
5291 tun_id = htonll(strtoull(arg1, NULL, 0));
5292 in_port = ofp_port_to_odp_port(atoi(arg2));
5294 packet = ofpbuf_new(strlen(args) / 2);
5295 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
5296 arg3 += strspn(arg3, " ");
5297 if (*arg3 != '\0') {
5298 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5301 if (packet->size < ETH_HEADER_LEN) {
5302 unixctl_command_reply(conn, 501,
5303 "Packet data too short for Ethernet");
5307 ds_put_cstr(&result, "Packet: ");
5308 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5309 ds_put_cstr(&result, s);
5312 flow_extract(packet, tun_id, in_port, &flow);
5314 unixctl_command_reply(conn, 501, "Bad command syntax");
5318 ofproto = ofproto_dpif_lookup(dpname);
5320 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5325 ds_put_cstr(&result, "Flow: ");
5326 flow_format(&result, &flow);
5327 ds_put_char(&result, '\n');
5329 rule = rule_dpif_lookup(ofproto, &flow, 0);
5330 trace_format_rule(&result, 0, 0, rule);
5332 struct ofproto_trace trace;
5333 struct ofpbuf *odp_actions;
5335 trace.result = &result;
5337 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5338 trace.ctx.resubmit_hook = trace_resubmit;
5339 odp_actions = xlate_actions(&trace.ctx,
5340 rule->up.actions, rule->up.n_actions);
5342 ds_put_char(&result, '\n');
5343 trace_format_flow(&result, 0, "Final flow", &trace);
5344 ds_put_cstr(&result, "Datapath actions: ");
5345 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5346 ofpbuf_delete(odp_actions);
5348 if (!trace.ctx.may_set_up_flow) {
5350 ds_put_cstr(&result, "\nThis flow is not cachable.");
5352 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5353 "for complete actions, please supply a packet.");
5358 unixctl_command_reply(conn, 200, ds_cstr(&result));
5361 ds_destroy(&result);
5362 ofpbuf_delete(packet);
5363 ofpbuf_uninit(&odp_key);
5368 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5369 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5372 unixctl_command_reply(conn, 200, NULL);
5376 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5377 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5380 unixctl_command_reply(conn, 200, NULL);
5384 ofproto_dpif_unixctl_init(void)
5386 static bool registered;
5392 unixctl_command_register("ofproto/trace",
5393 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5394 ofproto_unixctl_trace, NULL);
5395 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5397 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5398 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5401 const struct ofproto_class ofproto_dpif_class = {
5429 port_is_lacp_current,
5430 NULL, /* rule_choose_table */
5437 rule_modify_actions,
5445 get_cfm_remote_mpids,
5449 get_stp_port_status,
5454 is_mirror_output_bundle,
5455 forward_bpdu_changed,