2 * Copyright (c) 2009, 2010, 2011, 2012 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);
166 static int set_stp_port(struct ofport *,
167 const struct ofproto_port_stp_settings *);
169 struct action_xlate_ctx {
170 /* action_xlate_ctx_init() initializes these members. */
173 struct ofproto_dpif *ofproto;
175 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
176 * this flow when actions change header fields. */
179 /* The packet corresponding to 'flow', or a null pointer if we are
180 * revalidating without a packet to refer to. */
181 const struct ofpbuf *packet;
183 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
184 * want to execute them if we are actually processing a packet, or if we
185 * are accounting for packets that the datapath has processed, but not if
186 * we are just revalidating. */
189 /* If nonnull, called just before executing a resubmit action.
191 * This is normally null so the client has to set it manually after
192 * calling action_xlate_ctx_init(). */
193 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
195 /* xlate_actions() initializes and uses these members. The client might want
196 * to look at them after it returns. */
198 struct ofpbuf *odp_actions; /* Datapath actions. */
199 tag_type tags; /* Tags associated with actions. */
200 bool may_set_up_flow; /* True ordinarily; false if the actions must
201 * be reassessed for every packet. */
202 bool has_learn; /* Actions include NXAST_LEARN? */
203 bool has_normal; /* Actions output to OFPP_NORMAL? */
204 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
206 /* xlate_actions() initializes and uses these members, but the client has no
207 * reason to look at them. */
209 int recurse; /* Recursion level, via xlate_table_action. */
210 uint32_t priority; /* Current flow priority. 0 if none. */
211 struct flow base_flow; /* Flow at the last commit. */
212 uint32_t base_priority; /* Priority at the last commit. */
213 uint8_t table_id; /* OpenFlow table ID where flow was found. */
214 uint32_t sflow_n_outputs; /* Number of output ports. */
215 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
216 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
219 static void action_xlate_ctx_init(struct action_xlate_ctx *,
220 struct ofproto_dpif *, const struct flow *,
221 const struct ofpbuf *);
222 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
223 const union ofp_action *in, size_t n_in);
225 /* An exact-match instantiation of an OpenFlow flow. */
227 long long int used; /* Time last used; time created if not used. */
231 * - Do include packets and bytes sent "by hand", e.g. with
234 * - Do include packets and bytes that were obtained from the datapath
235 * when its statistics were reset (e.g. dpif_flow_put() with
236 * DPIF_FP_ZERO_STATS).
238 uint64_t packet_count; /* Number of packets received. */
239 uint64_t byte_count; /* Number of bytes received. */
241 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
242 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
244 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
245 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
246 long long int rs_used; /* Used time pushed to resubmit children. */
248 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
250 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
251 struct list list_node; /* In owning rule's 'facets' list. */
252 struct rule_dpif *rule; /* Owning rule. */
253 struct flow flow; /* Exact-match flow. */
254 bool installed; /* Installed in datapath? */
255 bool may_install; /* True ordinarily; false if actions must
256 * be reassessed for every packet. */
257 bool has_learn; /* Actions include NXAST_LEARN? */
258 bool has_normal; /* Actions output to OFPP_NORMAL? */
259 size_t actions_len; /* Number of bytes in actions[]. */
260 struct nlattr *actions; /* Datapath actions. */
261 tag_type tags; /* Tags. */
262 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
265 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
266 static void facet_remove(struct ofproto_dpif *, struct facet *);
267 static void facet_free(struct facet *);
269 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
270 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
271 const struct flow *);
272 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
274 static bool execute_controller_action(struct ofproto_dpif *,
276 const struct nlattr *odp_actions,
278 struct ofpbuf *packet, bool clone);
279 static void facet_execute(struct ofproto_dpif *, struct facet *,
280 struct ofpbuf *packet);
282 static int facet_put__(struct ofproto_dpif *, struct facet *,
283 const struct nlattr *actions, size_t actions_len,
284 struct dpif_flow_stats *);
285 static void facet_install(struct ofproto_dpif *, struct facet *,
287 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
288 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
290 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
291 const struct ofpbuf *packet);
292 static void facet_update_time(struct ofproto_dpif *, struct facet *,
294 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
295 const struct dpif_flow_stats *);
296 static void facet_reset_counters(struct facet *);
297 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
298 static void facet_push_stats(struct facet *);
299 static void facet_account(struct ofproto_dpif *, struct facet *);
301 static bool facet_is_controller_flow(struct facet *);
303 static void flow_push_stats(const struct rule_dpif *,
304 struct flow *, uint64_t packets, uint64_t bytes,
307 static uint32_t rule_calculate_tag(const struct flow *,
308 const struct flow_wildcards *,
310 static void rule_invalidate(const struct rule_dpif *);
316 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
317 struct list bundle_node; /* In struct ofbundle's "ports" list. */
318 struct cfm *cfm; /* Connectivity Fault Management, if any. */
319 tag_type tag; /* Tag associated with this port. */
320 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
321 bool may_enable; /* May be enabled in bonds. */
323 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
324 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
325 long long int stp_state_entered;
328 static struct ofport_dpif *
329 ofport_dpif_cast(const struct ofport *ofport)
331 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
332 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
335 static void port_run(struct ofport_dpif *);
336 static void port_wait(struct ofport_dpif *);
337 static int set_cfm(struct ofport *, const struct cfm_settings *);
339 struct dpif_completion {
340 struct list list_node;
341 struct ofoperation *op;
344 /* Extra information about a classifier table.
345 * Currently used just for optimized flow revalidation. */
347 /* If either of these is nonnull, then this table has a form that allows
348 * flows to be tagged to avoid revalidating most flows for the most common
349 * kinds of flow table changes. */
350 struct cls_table *catchall_table; /* Table that wildcards all fields. */
351 struct cls_table *other_table; /* Table with any other wildcard set. */
352 uint32_t basis; /* Keeps each table's tags separate. */
355 struct ofproto_dpif {
364 struct netflow *netflow;
365 struct dpif_sflow *sflow;
366 struct hmap bundles; /* Contains "struct ofbundle"s. */
367 struct mac_learning *ml;
368 struct ofmirror *mirrors[MAX_MIRRORS];
369 bool has_bonded_bundles;
372 struct timer next_expiration;
378 struct table_dpif tables[N_TABLES];
379 bool need_revalidate;
380 struct tag_set revalidate_set;
382 /* Support for debugging async flow mods. */
383 struct list completions;
385 bool has_bundle_action; /* True when the first bundle action appears. */
389 long long int stp_last_tick;
392 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
393 * for debugging the asynchronous flow_mod implementation.) */
396 static void ofproto_dpif_unixctl_init(void);
398 static struct ofproto_dpif *
399 ofproto_dpif_cast(const struct ofproto *ofproto)
401 assert(ofproto->ofproto_class == &ofproto_dpif_class);
402 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
405 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
407 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
410 /* Packet processing. */
411 static void update_learning_table(struct ofproto_dpif *,
412 const struct flow *, int vlan,
414 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
415 bool have_packet, tag_type *, int *vlanp,
416 struct ofbundle **in_bundlep);
419 #define FLOW_MISS_MAX_BATCH 50
420 static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
422 /* Flow expiration. */
423 static int expire(struct ofproto_dpif *);
426 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
427 const struct ofpbuf *packet);
429 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
430 const struct flow *, uint32_t odp_port);
431 /* Global variables. */
432 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
434 /* Factory functions. */
437 enumerate_types(struct sset *types)
439 dp_enumerate_types(types);
443 enumerate_names(const char *type, struct sset *names)
445 return dp_enumerate_names(type, names);
449 del(const char *type, const char *name)
454 error = dpif_open(name, type, &dpif);
456 error = dpif_delete(dpif);
462 /* Basic life-cycle. */
464 static struct ofproto *
467 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
472 dealloc(struct ofproto *ofproto_)
474 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
479 construct(struct ofproto *ofproto_, int *n_tablesp)
481 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
482 const char *name = ofproto->up.name;
486 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
488 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
492 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
493 ofproto->n_matches = 0;
495 dpif_flow_flush(ofproto->dpif);
496 dpif_recv_purge(ofproto->dpif);
498 error = dpif_recv_set_mask(ofproto->dpif,
499 ((1u << DPIF_UC_MISS) |
500 (1u << DPIF_UC_ACTION)));
502 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
503 dpif_close(ofproto->dpif);
507 ofproto->netflow = NULL;
508 ofproto->sflow = NULL;
510 hmap_init(&ofproto->bundles);
511 ofproto->ml = mac_learning_create();
512 for (i = 0; i < MAX_MIRRORS; i++) {
513 ofproto->mirrors[i] = NULL;
515 ofproto->has_bonded_bundles = false;
517 timer_set_duration(&ofproto->next_expiration, 1000);
519 hmap_init(&ofproto->facets);
521 for (i = 0; i < N_TABLES; i++) {
522 struct table_dpif *table = &ofproto->tables[i];
524 table->catchall_table = NULL;
525 table->other_table = NULL;
526 table->basis = random_uint32();
528 ofproto->need_revalidate = false;
529 tag_set_init(&ofproto->revalidate_set);
531 list_init(&ofproto->completions);
533 ofproto_dpif_unixctl_init();
535 ofproto->has_bundle_action = false;
537 *n_tablesp = N_TABLES;
542 complete_operations(struct ofproto_dpif *ofproto)
544 struct dpif_completion *c, *next;
546 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
547 ofoperation_complete(c->op, 0);
548 list_remove(&c->list_node);
554 destruct(struct ofproto *ofproto_)
556 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
557 struct rule_dpif *rule, *next_rule;
558 struct classifier *table;
561 complete_operations(ofproto);
563 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
564 struct cls_cursor cursor;
566 cls_cursor_init(&cursor, table, NULL);
567 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
568 ofproto_rule_destroy(&rule->up);
572 for (i = 0; i < MAX_MIRRORS; i++) {
573 mirror_destroy(ofproto->mirrors[i]);
576 netflow_destroy(ofproto->netflow);
577 dpif_sflow_destroy(ofproto->sflow);
578 hmap_destroy(&ofproto->bundles);
579 mac_learning_destroy(ofproto->ml);
581 hmap_destroy(&ofproto->facets);
583 dpif_close(ofproto->dpif);
587 run_fast(struct ofproto *ofproto_)
589 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
592 /* Handle one or more batches of upcalls, until there's nothing left to do
593 * or until we do a fixed total amount of work.
595 * We do work in batches because it can be much cheaper to set up a number
596 * of flows and fire off their patches all at once. We do multiple batches
597 * because in some cases handling a packet can cause another packet to be
598 * queued almost immediately as part of the return flow. Both
599 * optimizations can make major improvements on some benchmarks and
600 * presumably for real traffic as well. */
602 while (work < FLOW_MISS_MAX_BATCH) {
603 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
613 run(struct ofproto *ofproto_)
615 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
616 struct ofport_dpif *ofport;
617 struct ofbundle *bundle;
621 complete_operations(ofproto);
623 dpif_run(ofproto->dpif);
625 error = run_fast(ofproto_);
630 if (timer_expired(&ofproto->next_expiration)) {
631 int delay = expire(ofproto);
632 timer_set_duration(&ofproto->next_expiration, delay);
635 if (ofproto->netflow) {
636 netflow_run(ofproto->netflow);
638 if (ofproto->sflow) {
639 dpif_sflow_run(ofproto->sflow);
642 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
645 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
650 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
652 /* Now revalidate if there's anything to do. */
653 if (ofproto->need_revalidate
654 || !tag_set_is_empty(&ofproto->revalidate_set)) {
655 struct tag_set revalidate_set = ofproto->revalidate_set;
656 bool revalidate_all = ofproto->need_revalidate;
657 struct facet *facet, *next;
659 /* Clear the revalidation flags. */
660 tag_set_init(&ofproto->revalidate_set);
661 ofproto->need_revalidate = false;
663 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
665 || tag_set_intersects(&revalidate_set, facet->tags)) {
666 facet_revalidate(ofproto, facet);
675 wait(struct ofproto *ofproto_)
677 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
678 struct ofport_dpif *ofport;
679 struct ofbundle *bundle;
681 if (!clogged && !list_is_empty(&ofproto->completions)) {
682 poll_immediate_wake();
685 dpif_wait(ofproto->dpif);
686 dpif_recv_wait(ofproto->dpif);
687 if (ofproto->sflow) {
688 dpif_sflow_wait(ofproto->sflow);
690 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
691 poll_immediate_wake();
693 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
696 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
699 mac_learning_wait(ofproto->ml);
701 if (ofproto->need_revalidate) {
702 /* Shouldn't happen, but if it does just go around again. */
703 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
704 poll_immediate_wake();
706 timer_wait(&ofproto->next_expiration);
711 flush(struct ofproto *ofproto_)
713 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
714 struct facet *facet, *next_facet;
716 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
717 /* Mark the facet as not installed so that facet_remove() doesn't
718 * bother trying to uninstall it. There is no point in uninstalling it
719 * individually since we are about to blow away all the facets with
720 * dpif_flow_flush(). */
721 facet->installed = false;
722 facet->dp_packet_count = 0;
723 facet->dp_byte_count = 0;
724 facet_remove(ofproto, facet);
726 dpif_flow_flush(ofproto->dpif);
730 get_features(struct ofproto *ofproto_ OVS_UNUSED,
731 bool *arp_match_ip, uint32_t *actions)
733 *arp_match_ip = true;
734 *actions = ((1u << OFPAT_OUTPUT) |
735 (1u << OFPAT_SET_VLAN_VID) |
736 (1u << OFPAT_SET_VLAN_PCP) |
737 (1u << OFPAT_STRIP_VLAN) |
738 (1u << OFPAT_SET_DL_SRC) |
739 (1u << OFPAT_SET_DL_DST) |
740 (1u << OFPAT_SET_NW_SRC) |
741 (1u << OFPAT_SET_NW_DST) |
742 (1u << OFPAT_SET_NW_TOS) |
743 (1u << OFPAT_SET_TP_SRC) |
744 (1u << OFPAT_SET_TP_DST) |
745 (1u << OFPAT_ENQUEUE));
749 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
751 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
752 struct dpif_dp_stats s;
754 strcpy(ots->name, "classifier");
756 dpif_get_dp_stats(ofproto->dpif, &s);
757 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
758 put_32aligned_be64(&ots->matched_count,
759 htonll(s.n_hit + ofproto->n_matches));
763 set_netflow(struct ofproto *ofproto_,
764 const struct netflow_options *netflow_options)
766 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
768 if (netflow_options) {
769 if (!ofproto->netflow) {
770 ofproto->netflow = netflow_create();
772 return netflow_set_options(ofproto->netflow, netflow_options);
774 netflow_destroy(ofproto->netflow);
775 ofproto->netflow = NULL;
780 static struct ofport *
783 struct ofport_dpif *port = xmalloc(sizeof *port);
788 port_dealloc(struct ofport *port_)
790 struct ofport_dpif *port = ofport_dpif_cast(port_);
795 port_construct(struct ofport *port_)
797 struct ofport_dpif *port = ofport_dpif_cast(port_);
798 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
800 ofproto->need_revalidate = true;
801 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
804 port->tag = tag_create_random();
805 port->may_enable = true;
806 port->stp_port = NULL;
807 port->stp_state = STP_DISABLED;
809 if (ofproto->sflow) {
810 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
811 netdev_get_name(port->up.netdev));
818 port_destruct(struct ofport *port_)
820 struct ofport_dpif *port = ofport_dpif_cast(port_);
821 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
823 ofproto->need_revalidate = true;
824 bundle_remove(port_);
825 set_cfm(port_, NULL);
826 if (ofproto->sflow) {
827 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
832 port_modified(struct ofport *port_)
834 struct ofport_dpif *port = ofport_dpif_cast(port_);
836 if (port->bundle && port->bundle->bond) {
837 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
842 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
844 struct ofport_dpif *port = ofport_dpif_cast(port_);
845 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
846 ovs_be32 changed = old_config ^ port->up.opp.config;
848 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
849 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
850 ofproto->need_revalidate = true;
852 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
853 bundle_update(port->bundle);
859 set_sflow(struct ofproto *ofproto_,
860 const struct ofproto_sflow_options *sflow_options)
862 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
863 struct dpif_sflow *ds = ofproto->sflow;
867 struct ofport_dpif *ofport;
869 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
870 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
871 dpif_sflow_add_port(ds, ofport->odp_port,
872 netdev_get_name(ofport->up.netdev));
874 ofproto->need_revalidate = true;
876 dpif_sflow_set_options(ds, sflow_options);
879 dpif_sflow_destroy(ds);
880 ofproto->need_revalidate = true;
881 ofproto->sflow = NULL;
888 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
890 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
897 struct ofproto_dpif *ofproto;
899 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
900 ofproto->need_revalidate = true;
901 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
904 if (cfm_configure(ofport->cfm, s)) {
910 cfm_destroy(ofport->cfm);
916 get_cfm_fault(const struct ofport *ofport_)
918 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
920 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
924 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
927 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
930 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
940 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
942 struct ofproto_dpif *ofproto = ofproto_;
943 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
944 struct ofport_dpif *ofport;
946 ofport = stp_port_get_aux(sp);
948 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
949 ofproto->up.name, port_num);
951 struct eth_header *eth = pkt->l2;
953 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
954 if (eth_addr_is_zero(eth->eth_src)) {
955 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
956 "with unknown MAC", ofproto->up.name, port_num);
958 int error = netdev_send(ofport->up.netdev, pkt);
960 VLOG_WARN_RL(&rl, "%s: sending BPDU on port %s failed (%s)",
962 netdev_get_name(ofport->up.netdev),
970 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
972 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
974 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
976 /* Only revalidate flows if the configuration changed. */
977 if (!s != !ofproto->stp) {
978 ofproto->need_revalidate = true;
983 ofproto->stp = stp_create(ofproto_->name, s->system_id,
984 send_bpdu_cb, ofproto);
985 ofproto->stp_last_tick = time_msec();
988 stp_set_bridge_id(ofproto->stp, s->system_id);
989 stp_set_bridge_priority(ofproto->stp, s->priority);
990 stp_set_hello_time(ofproto->stp, s->hello_time);
991 stp_set_max_age(ofproto->stp, s->max_age);
992 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
994 struct ofport *ofport;
996 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
997 set_stp_port(ofport, NULL);
1000 stp_destroy(ofproto->stp);
1001 ofproto->stp = NULL;
1008 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1010 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1014 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1015 s->designated_root = stp_get_designated_root(ofproto->stp);
1016 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1025 update_stp_port_state(struct ofport_dpif *ofport)
1027 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1028 enum stp_state state;
1030 /* Figure out new state. */
1031 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1035 if (ofport->stp_state != state) {
1039 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1040 netdev_get_name(ofport->up.netdev),
1041 stp_state_name(ofport->stp_state),
1042 stp_state_name(state));
1043 if (stp_learn_in_state(ofport->stp_state)
1044 != stp_learn_in_state(state)) {
1045 /* xxx Learning action flows should also be flushed. */
1046 mac_learning_flush(ofproto->ml);
1048 fwd_change = stp_forward_in_state(ofport->stp_state)
1049 != stp_forward_in_state(state);
1051 ofproto->need_revalidate = true;
1052 ofport->stp_state = state;
1053 ofport->stp_state_entered = time_msec();
1055 if (fwd_change && ofport->bundle) {
1056 bundle_update(ofport->bundle);
1059 /* Update the STP state bits in the OpenFlow port description. */
1060 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1061 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1062 : state == STP_LEARNING ? OFPPS_STP_LEARN
1063 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1064 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1066 ofproto_port_set_state(&ofport->up, of_state);
1070 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1071 * caller is responsible for assigning STP port numbers and ensuring
1072 * there are no duplicates. */
1074 set_stp_port(struct ofport *ofport_,
1075 const struct ofproto_port_stp_settings *s)
1077 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1078 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1079 struct stp_port *sp = ofport->stp_port;
1081 if (!s || !s->enable) {
1083 ofport->stp_port = NULL;
1084 stp_port_disable(sp);
1085 update_stp_port_state(ofport);
1088 } else if (sp && stp_port_no(sp) != s->port_num
1089 && ofport == stp_port_get_aux(sp)) {
1090 /* The port-id changed, so disable the old one if it's not
1091 * already in use by another port. */
1092 stp_port_disable(sp);
1095 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1096 stp_port_enable(sp);
1098 stp_port_set_aux(sp, ofport);
1099 stp_port_set_priority(sp, s->priority);
1100 stp_port_set_path_cost(sp, s->path_cost);
1102 update_stp_port_state(ofport);
1108 get_stp_port_status(struct ofport *ofport_,
1109 struct ofproto_port_stp_status *s)
1111 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1112 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1113 struct stp_port *sp = ofport->stp_port;
1115 if (!ofproto->stp || !sp) {
1121 s->port_id = stp_port_get_id(sp);
1122 s->state = stp_port_get_state(sp);
1123 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1124 s->role = stp_port_get_role(sp);
1125 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1131 stp_run(struct ofproto_dpif *ofproto)
1134 long long int now = time_msec();
1135 long long int elapsed = now - ofproto->stp_last_tick;
1136 struct stp_port *sp;
1139 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1140 ofproto->stp_last_tick = now;
1142 while (stp_get_changed_port(ofproto->stp, &sp)) {
1143 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1146 update_stp_port_state(ofport);
1153 stp_wait(struct ofproto_dpif *ofproto)
1156 poll_timer_wait(1000);
1160 /* Returns true if STP should process 'flow'. */
1162 stp_should_process_flow(const struct flow *flow)
1164 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1168 stp_process_packet(const struct ofport_dpif *ofport,
1169 const struct ofpbuf *packet)
1171 struct ofpbuf payload = *packet;
1172 struct eth_header *eth = payload.data;
1173 struct stp_port *sp = ofport->stp_port;
1175 /* Sink packets on ports that have STP disabled when the bridge has
1177 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1181 /* Trim off padding on payload. */
1182 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1183 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1186 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1187 stp_received_bpdu(sp, payload.data, payload.size);
1193 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1194 * to revalidate every flow. */
1196 bundle_flush_macs(struct ofbundle *bundle)
1198 struct ofproto_dpif *ofproto = bundle->ofproto;
1199 struct mac_learning *ml = ofproto->ml;
1200 struct mac_entry *mac, *next_mac;
1202 ofproto->need_revalidate = true;
1203 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1204 if (mac->port.p == bundle) {
1205 mac_learning_expire(ml, mac);
1210 static struct ofbundle *
1211 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1213 struct ofbundle *bundle;
1215 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1216 &ofproto->bundles) {
1217 if (bundle->aux == aux) {
1224 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1225 * ones that are found to 'bundles'. */
1227 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1228 void **auxes, size_t n_auxes,
1229 struct hmapx *bundles)
1233 hmapx_init(bundles);
1234 for (i = 0; i < n_auxes; i++) {
1235 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1237 hmapx_add(bundles, bundle);
1243 bundle_update(struct ofbundle *bundle)
1245 struct ofport_dpif *port;
1247 bundle->floodable = true;
1248 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1249 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1250 || !stp_forward_in_state(port->stp_state)) {
1251 bundle->floodable = false;
1258 bundle_del_port(struct ofport_dpif *port)
1260 struct ofbundle *bundle = port->bundle;
1262 bundle->ofproto->need_revalidate = true;
1264 list_remove(&port->bundle_node);
1265 port->bundle = NULL;
1268 lacp_slave_unregister(bundle->lacp, port);
1271 bond_slave_unregister(bundle->bond, port);
1274 bundle_update(bundle);
1278 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1279 struct lacp_slave_settings *lacp,
1280 uint32_t bond_stable_id)
1282 struct ofport_dpif *port;
1284 port = get_ofp_port(bundle->ofproto, ofp_port);
1289 if (port->bundle != bundle) {
1290 bundle->ofproto->need_revalidate = true;
1292 bundle_del_port(port);
1295 port->bundle = bundle;
1296 list_push_back(&bundle->ports, &port->bundle_node);
1297 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1298 || !stp_forward_in_state(port->stp_state)) {
1299 bundle->floodable = false;
1303 port->bundle->ofproto->need_revalidate = true;
1304 lacp_slave_register(bundle->lacp, port, lacp);
1307 port->bond_stable_id = bond_stable_id;
1313 bundle_destroy(struct ofbundle *bundle)
1315 struct ofproto_dpif *ofproto;
1316 struct ofport_dpif *port, *next_port;
1323 ofproto = bundle->ofproto;
1324 for (i = 0; i < MAX_MIRRORS; i++) {
1325 struct ofmirror *m = ofproto->mirrors[i];
1327 if (m->out == bundle) {
1329 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1330 || hmapx_find_and_delete(&m->dsts, bundle)) {
1331 ofproto->need_revalidate = true;
1336 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1337 bundle_del_port(port);
1340 bundle_flush_macs(bundle);
1341 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1343 free(bundle->trunks);
1344 lacp_destroy(bundle->lacp);
1345 bond_destroy(bundle->bond);
1350 bundle_set(struct ofproto *ofproto_, void *aux,
1351 const struct ofproto_bundle_settings *s)
1353 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1354 bool need_flush = false;
1355 struct ofport_dpif *port;
1356 struct ofbundle *bundle;
1357 unsigned long *trunks;
1363 bundle_destroy(bundle_lookup(ofproto, aux));
1367 assert(s->n_slaves == 1 || s->bond != NULL);
1368 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1370 bundle = bundle_lookup(ofproto, aux);
1372 bundle = xmalloc(sizeof *bundle);
1374 bundle->ofproto = ofproto;
1375 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1376 hash_pointer(aux, 0));
1378 bundle->name = NULL;
1380 list_init(&bundle->ports);
1381 bundle->vlan_mode = PORT_VLAN_TRUNK;
1383 bundle->trunks = NULL;
1384 bundle->lacp = NULL;
1385 bundle->bond = NULL;
1387 bundle->floodable = true;
1389 bundle->src_mirrors = 0;
1390 bundle->dst_mirrors = 0;
1391 bundle->mirror_out = 0;
1394 if (!bundle->name || strcmp(s->name, bundle->name)) {
1396 bundle->name = xstrdup(s->name);
1401 if (!bundle->lacp) {
1402 ofproto->need_revalidate = true;
1403 bundle->lacp = lacp_create();
1405 lacp_configure(bundle->lacp, s->lacp);
1407 lacp_destroy(bundle->lacp);
1408 bundle->lacp = NULL;
1411 /* Update set of ports. */
1413 for (i = 0; i < s->n_slaves; i++) {
1414 if (!bundle_add_port(bundle, s->slaves[i],
1415 s->lacp ? &s->lacp_slaves[i] : NULL,
1416 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1420 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1421 struct ofport_dpif *next_port;
1423 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1424 for (i = 0; i < s->n_slaves; i++) {
1425 if (s->slaves[i] == port->up.ofp_port) {
1430 bundle_del_port(port);
1434 assert(list_size(&bundle->ports) <= s->n_slaves);
1436 if (list_is_empty(&bundle->ports)) {
1437 bundle_destroy(bundle);
1441 /* Set VLAN tagging mode */
1442 if (s->vlan_mode != bundle->vlan_mode) {
1443 bundle->vlan_mode = s->vlan_mode;
1448 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1449 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1451 if (vlan != bundle->vlan) {
1452 bundle->vlan = vlan;
1456 /* Get trunked VLANs. */
1457 switch (s->vlan_mode) {
1458 case PORT_VLAN_ACCESS:
1462 case PORT_VLAN_TRUNK:
1463 trunks = (unsigned long *) s->trunks;
1466 case PORT_VLAN_NATIVE_UNTAGGED:
1467 case PORT_VLAN_NATIVE_TAGGED:
1468 if (vlan != 0 && (!s->trunks
1469 || !bitmap_is_set(s->trunks, vlan)
1470 || bitmap_is_set(s->trunks, 0))) {
1471 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1473 trunks = bitmap_clone(s->trunks, 4096);
1475 trunks = bitmap_allocate1(4096);
1477 bitmap_set1(trunks, vlan);
1478 bitmap_set0(trunks, 0);
1480 trunks = (unsigned long *) s->trunks;
1487 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1488 free(bundle->trunks);
1489 if (trunks == s->trunks) {
1490 bundle->trunks = vlan_bitmap_clone(trunks);
1492 bundle->trunks = trunks;
1497 if (trunks != s->trunks) {
1502 if (!list_is_short(&bundle->ports)) {
1503 bundle->ofproto->has_bonded_bundles = true;
1505 if (bond_reconfigure(bundle->bond, s->bond)) {
1506 ofproto->need_revalidate = true;
1509 bundle->bond = bond_create(s->bond);
1510 ofproto->need_revalidate = true;
1513 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1514 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1518 bond_destroy(bundle->bond);
1519 bundle->bond = NULL;
1522 /* If we changed something that would affect MAC learning, un-learn
1523 * everything on this port and force flow revalidation. */
1525 bundle_flush_macs(bundle);
1532 bundle_remove(struct ofport *port_)
1534 struct ofport_dpif *port = ofport_dpif_cast(port_);
1535 struct ofbundle *bundle = port->bundle;
1538 bundle_del_port(port);
1539 if (list_is_empty(&bundle->ports)) {
1540 bundle_destroy(bundle);
1541 } else if (list_is_short(&bundle->ports)) {
1542 bond_destroy(bundle->bond);
1543 bundle->bond = NULL;
1549 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1551 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1552 struct ofport_dpif *port = port_;
1553 uint8_t ea[ETH_ADDR_LEN];
1556 error = netdev_get_etheraddr(port->up.netdev, ea);
1558 struct ofpbuf packet;
1561 ofpbuf_init(&packet, 0);
1562 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1564 memcpy(packet_pdu, pdu, pdu_size);
1566 error = netdev_send(port->up.netdev, &packet);
1568 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1569 "(%s)", port->bundle->name,
1570 netdev_get_name(port->up.netdev), strerror(error));
1572 ofpbuf_uninit(&packet);
1574 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1575 "%s (%s)", port->bundle->name,
1576 netdev_get_name(port->up.netdev), strerror(error));
1581 bundle_send_learning_packets(struct ofbundle *bundle)
1583 struct ofproto_dpif *ofproto = bundle->ofproto;
1584 int error, n_packets, n_errors;
1585 struct mac_entry *e;
1587 error = n_packets = n_errors = 0;
1588 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1589 if (e->port.p != bundle) {
1590 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1600 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1601 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1602 "packets, last error was: %s",
1603 bundle->name, n_errors, n_packets, strerror(error));
1605 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1606 bundle->name, n_packets);
1611 bundle_run(struct ofbundle *bundle)
1614 lacp_run(bundle->lacp, send_pdu_cb);
1617 struct ofport_dpif *port;
1619 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1620 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1623 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1624 lacp_negotiated(bundle->lacp));
1625 if (bond_should_send_learning_packets(bundle->bond)) {
1626 bundle_send_learning_packets(bundle);
1632 bundle_wait(struct ofbundle *bundle)
1635 lacp_wait(bundle->lacp);
1638 bond_wait(bundle->bond);
1645 mirror_scan(struct ofproto_dpif *ofproto)
1649 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1650 if (!ofproto->mirrors[idx]) {
1657 static struct ofmirror *
1658 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1662 for (i = 0; i < MAX_MIRRORS; i++) {
1663 struct ofmirror *mirror = ofproto->mirrors[i];
1664 if (mirror && mirror->aux == aux) {
1673 mirror_set(struct ofproto *ofproto_, void *aux,
1674 const struct ofproto_mirror_settings *s)
1676 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1677 mirror_mask_t mirror_bit;
1678 struct ofbundle *bundle;
1679 struct ofmirror *mirror;
1680 struct ofbundle *out;
1681 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1682 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1685 mirror = mirror_lookup(ofproto, aux);
1687 mirror_destroy(mirror);
1693 idx = mirror_scan(ofproto);
1695 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1697 ofproto->up.name, MAX_MIRRORS, s->name);
1701 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1702 mirror->ofproto = ofproto;
1705 mirror->out_vlan = -1;
1706 mirror->name = NULL;
1709 if (!mirror->name || strcmp(s->name, mirror->name)) {
1711 mirror->name = xstrdup(s->name);
1714 /* Get the new configuration. */
1715 if (s->out_bundle) {
1716 out = bundle_lookup(ofproto, s->out_bundle);
1718 mirror_destroy(mirror);
1724 out_vlan = s->out_vlan;
1726 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1727 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1729 /* If the configuration has not changed, do nothing. */
1730 if (hmapx_equals(&srcs, &mirror->srcs)
1731 && hmapx_equals(&dsts, &mirror->dsts)
1732 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1733 && mirror->out == out
1734 && mirror->out_vlan == out_vlan)
1736 hmapx_destroy(&srcs);
1737 hmapx_destroy(&dsts);
1741 hmapx_swap(&srcs, &mirror->srcs);
1742 hmapx_destroy(&srcs);
1744 hmapx_swap(&dsts, &mirror->dsts);
1745 hmapx_destroy(&dsts);
1747 free(mirror->vlans);
1748 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1751 mirror->out_vlan = out_vlan;
1753 /* Update bundles. */
1754 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1755 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1756 if (hmapx_contains(&mirror->srcs, bundle)) {
1757 bundle->src_mirrors |= mirror_bit;
1759 bundle->src_mirrors &= ~mirror_bit;
1762 if (hmapx_contains(&mirror->dsts, bundle)) {
1763 bundle->dst_mirrors |= mirror_bit;
1765 bundle->dst_mirrors &= ~mirror_bit;
1768 if (mirror->out == bundle) {
1769 bundle->mirror_out |= mirror_bit;
1771 bundle->mirror_out &= ~mirror_bit;
1775 ofproto->need_revalidate = true;
1776 mac_learning_flush(ofproto->ml);
1782 mirror_destroy(struct ofmirror *mirror)
1784 struct ofproto_dpif *ofproto;
1785 mirror_mask_t mirror_bit;
1786 struct ofbundle *bundle;
1792 ofproto = mirror->ofproto;
1793 ofproto->need_revalidate = true;
1794 mac_learning_flush(ofproto->ml);
1796 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1797 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1798 bundle->src_mirrors &= ~mirror_bit;
1799 bundle->dst_mirrors &= ~mirror_bit;
1800 bundle->mirror_out &= ~mirror_bit;
1803 hmapx_destroy(&mirror->srcs);
1804 hmapx_destroy(&mirror->dsts);
1805 free(mirror->vlans);
1807 ofproto->mirrors[mirror->idx] = NULL;
1813 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1815 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1816 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1817 ofproto->need_revalidate = true;
1818 mac_learning_flush(ofproto->ml);
1824 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
1826 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1827 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1828 return bundle && bundle->mirror_out != 0;
1832 forward_bpdu_changed(struct ofproto *ofproto_)
1834 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1835 /* Revalidate cached flows whenever forward_bpdu option changes. */
1836 ofproto->need_revalidate = true;
1841 static struct ofport_dpif *
1842 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1844 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1845 return ofport ? ofport_dpif_cast(ofport) : NULL;
1848 static struct ofport_dpif *
1849 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1851 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1855 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1856 struct dpif_port *dpif_port)
1858 ofproto_port->name = dpif_port->name;
1859 ofproto_port->type = dpif_port->type;
1860 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1864 port_run(struct ofport_dpif *ofport)
1866 bool enable = netdev_get_carrier(ofport->up.netdev);
1869 cfm_run(ofport->cfm);
1871 if (cfm_should_send_ccm(ofport->cfm)) {
1872 struct ofpbuf packet;
1874 ofpbuf_init(&packet, 0);
1875 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1876 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1877 ofport->odp_port, &packet);
1878 ofpbuf_uninit(&packet);
1881 enable = enable && !cfm_get_fault(ofport->cfm)
1882 && cfm_get_opup(ofport->cfm);
1885 if (ofport->bundle) {
1886 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1889 if (ofport->may_enable != enable) {
1890 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1892 if (ofproto->has_bundle_action) {
1893 ofproto->need_revalidate = true;
1897 ofport->may_enable = enable;
1901 port_wait(struct ofport_dpif *ofport)
1904 cfm_wait(ofport->cfm);
1909 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1910 struct ofproto_port *ofproto_port)
1912 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1913 struct dpif_port dpif_port;
1916 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1918 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1924 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1926 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1930 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1932 *ofp_portp = odp_port_to_ofp_port(odp_port);
1938 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1940 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1943 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1945 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1947 /* The caller is going to close ofport->up.netdev. If this is a
1948 * bonded port, then the bond is using that netdev, so remove it
1949 * from the bond. The client will need to reconfigure everything
1950 * after deleting ports, so then the slave will get re-added. */
1951 bundle_remove(&ofport->up);
1957 struct port_dump_state {
1958 struct dpif_port_dump dump;
1963 port_dump_start(const struct ofproto *ofproto_, void **statep)
1965 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1966 struct port_dump_state *state;
1968 *statep = state = xmalloc(sizeof *state);
1969 dpif_port_dump_start(&state->dump, ofproto->dpif);
1970 state->done = false;
1975 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1976 struct ofproto_port *port)
1978 struct port_dump_state *state = state_;
1979 struct dpif_port dpif_port;
1981 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1982 ofproto_port_from_dpif_port(port, &dpif_port);
1985 int error = dpif_port_dump_done(&state->dump);
1987 return error ? error : EOF;
1992 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1994 struct port_dump_state *state = state_;
1997 dpif_port_dump_done(&state->dump);
2004 port_poll(const struct ofproto *ofproto_, char **devnamep)
2006 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2007 return dpif_port_poll(ofproto->dpif, devnamep);
2011 port_poll_wait(const struct ofproto *ofproto_)
2013 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2014 dpif_port_poll_wait(ofproto->dpif);
2018 port_is_lacp_current(const struct ofport *ofport_)
2020 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2021 return (ofport->bundle && ofport->bundle->lacp
2022 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2026 /* Upcall handling. */
2028 /* Flow miss batching.
2030 * Some dpifs implement operations faster when you hand them off in a batch.
2031 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2032 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2033 * more packets, plus possibly installing the flow in the dpif.
2035 * So far we only batch the operations that affect flow setup time the most.
2036 * It's possible to batch more than that, but the benefit might be minimal. */
2038 struct hmap_node hmap_node;
2040 const struct nlattr *key;
2042 struct list packets;
2045 struct flow_miss_op {
2046 union dpif_op dpif_op;
2047 struct facet *facet;
2050 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2051 * OpenFlow controller as necessary according to their individual
2054 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2055 * ownership is transferred to this function. */
2057 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2058 const struct flow *flow, bool clone)
2060 struct ofputil_packet_in pin;
2062 pin.packet = packet;
2063 pin.in_port = flow->in_port;
2064 pin.reason = OFPR_NO_MATCH;
2065 pin.buffer_id = 0; /* not yet known */
2066 pin.send_len = 0; /* not used for flow table misses */
2067 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2068 clone ? NULL : packet);
2071 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2072 * OpenFlow controller as necessary according to their individual
2075 * 'send_len' should be the number of bytes of 'packet' to send to the
2076 * controller, as specified in the action that caused the packet to be sent.
2078 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2079 * Otherwise, ownership is transferred to this function. */
2081 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2082 uint64_t userdata, const struct flow *flow, bool clone)
2084 struct ofputil_packet_in pin;
2085 struct user_action_cookie cookie;
2087 memcpy(&cookie, &userdata, sizeof(cookie));
2089 pin.packet = packet;
2090 pin.in_port = flow->in_port;
2091 pin.reason = OFPR_ACTION;
2092 pin.buffer_id = 0; /* not yet known */
2093 pin.send_len = cookie.data;
2094 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2095 clone ? NULL : packet);
2099 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2100 const struct ofpbuf *packet)
2102 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2108 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2110 cfm_process_heartbeat(ofport->cfm, packet);
2113 } else if (ofport->bundle && ofport->bundle->lacp
2114 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2116 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2119 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2121 stp_process_packet(ofport, packet);
2128 static struct flow_miss *
2129 flow_miss_create(struct hmap *todo, const struct flow *flow,
2130 const struct nlattr *key, size_t key_len)
2132 uint32_t hash = flow_hash(flow, 0);
2133 struct flow_miss *miss;
2135 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2136 if (flow_equal(&miss->flow, flow)) {
2141 miss = xmalloc(sizeof *miss);
2142 hmap_insert(todo, &miss->hmap_node, hash);
2145 miss->key_len = key_len;
2146 list_init(&miss->packets);
2151 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2152 struct flow_miss_op *ops, size_t *n_ops)
2154 const struct flow *flow = &miss->flow;
2155 struct ofpbuf *packet, *next_packet;
2156 struct facet *facet;
2158 facet = facet_lookup_valid(ofproto, flow);
2160 struct rule_dpif *rule;
2162 rule = rule_dpif_lookup(ofproto, flow, 0);
2164 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2165 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2167 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2168 COVERAGE_INC(ofproto_dpif_no_packet_in);
2169 /* XXX install 'drop' flow entry */
2173 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2177 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2179 list_remove(&packet->list_node);
2180 send_packet_in_miss(ofproto, packet, flow, false);
2186 facet = facet_create(rule, flow);
2189 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2190 list_remove(&packet->list_node);
2191 ofproto->n_matches++;
2193 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2195 * Extra-special case for fail-open mode.
2197 * We are in fail-open mode and the packet matched the fail-open
2198 * rule, but we are connected to a controller too. We should send
2199 * the packet up to the controller in the hope that it will try to
2200 * set up a flow and thereby allow us to exit fail-open.
2202 * See the top-level comment in fail-open.c for more information.
2204 send_packet_in_miss(ofproto, packet, flow, true);
2207 if (!facet->may_install) {
2208 facet_make_actions(ofproto, facet, packet);
2210 if (!execute_controller_action(ofproto, &facet->flow,
2211 facet->actions, facet->actions_len,
2213 struct flow_miss_op *op = &ops[(*n_ops)++];
2214 struct dpif_execute *execute = &op->dpif_op.execute;
2217 execute->type = DPIF_OP_EXECUTE;
2218 execute->key = miss->key;
2219 execute->key_len = miss->key_len;
2221 = (facet->may_install
2223 : xmemdup(facet->actions, facet->actions_len));
2224 execute->actions_len = facet->actions_len;
2225 execute->packet = packet;
2229 if (facet->may_install) {
2230 struct flow_miss_op *op = &ops[(*n_ops)++];
2231 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2234 put->type = DPIF_OP_FLOW_PUT;
2235 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2236 put->key = miss->key;
2237 put->key_len = miss->key_len;
2238 put->actions = facet->actions;
2239 put->actions_len = facet->actions_len;
2245 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2248 struct dpif_upcall *upcall;
2249 struct flow_miss *miss, *next_miss;
2250 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2251 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2260 /* Construct the to-do list.
2262 * This just amounts to extracting the flow from each packet and sticking
2263 * the packets that have the same flow in the same "flow_miss" structure so
2264 * that we can process them together. */
2266 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2267 struct flow_miss *miss;
2270 /* Obtain in_port and tun_id, at least, then set 'flow''s header
2272 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2273 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
2275 /* Handle 802.1ag, LACP, and STP specially. */
2276 if (process_special(ofproto, &flow, upcall->packet)) {
2277 ofpbuf_delete(upcall->packet);
2278 ofproto->n_matches++;
2282 /* Add other packets to a to-do list. */
2283 miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len);
2284 list_push_back(&miss->packets, &upcall->packet->list_node);
2287 /* Process each element in the to-do list, constructing the set of
2288 * operations to batch. */
2290 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2291 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2292 ofpbuf_list_delete(&miss->packets);
2293 hmap_remove(&todo, &miss->hmap_node);
2296 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2297 hmap_destroy(&todo);
2299 /* Execute batch. */
2300 for (i = 0; i < n_ops; i++) {
2301 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2303 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2305 /* Free memory and update facets. */
2306 for (i = 0; i < n_ops; i++) {
2307 struct flow_miss_op *op = &flow_miss_ops[i];
2308 struct dpif_execute *execute;
2309 struct dpif_flow_put *put;
2311 switch (op->dpif_op.type) {
2312 case DPIF_OP_EXECUTE:
2313 execute = &op->dpif_op.execute;
2314 if (op->facet->actions != execute->actions) {
2315 free((struct nlattr *) execute->actions);
2317 ofpbuf_delete((struct ofpbuf *) execute->packet);
2320 case DPIF_OP_FLOW_PUT:
2321 put = &op->dpif_op.flow_put;
2323 op->facet->installed = true;
2331 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2332 struct dpif_upcall *upcall)
2335 struct user_action_cookie cookie;
2337 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2339 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2340 if (ofproto->sflow) {
2341 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2342 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2344 ofpbuf_delete(upcall->packet);
2346 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2347 COVERAGE_INC(ofproto_dpif_ctlr_action);
2348 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2349 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2352 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2357 handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
2359 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
2363 assert (max_batch <= FLOW_MISS_MAX_BATCH);
2366 for (i = 0; i < max_batch; i++) {
2367 struct dpif_upcall *upcall = &misses[n_misses];
2370 error = dpif_recv(ofproto->dpif, upcall);
2375 switch (upcall->type) {
2376 case DPIF_UC_ACTION:
2377 handle_userspace_upcall(ofproto, upcall);
2381 /* Handle it later. */
2385 case DPIF_N_UC_TYPES:
2387 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
2393 handle_miss_upcalls(ofproto, misses, n_misses);
2398 /* Flow expiration. */
2400 static int facet_max_idle(const struct ofproto_dpif *);
2401 static void update_stats(struct ofproto_dpif *);
2402 static void rule_expire(struct rule_dpif *);
2403 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
2405 /* This function is called periodically by run(). Its job is to collect
2406 * updates for the flows that have been installed into the datapath, most
2407 * importantly when they last were used, and then use that information to
2408 * expire flows that have not been used recently.
2410 * Returns the number of milliseconds after which it should be called again. */
2412 expire(struct ofproto_dpif *ofproto)
2414 struct rule_dpif *rule, *next_rule;
2415 struct classifier *table;
2418 /* Update stats for each flow in the datapath. */
2419 update_stats(ofproto);
2421 /* Expire facets that have been idle too long. */
2422 dp_max_idle = facet_max_idle(ofproto);
2423 expire_facets(ofproto, dp_max_idle);
2425 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2426 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2427 struct cls_cursor cursor;
2429 cls_cursor_init(&cursor, table, NULL);
2430 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2435 /* All outstanding data in existing flows has been accounted, so it's a
2436 * good time to do bond rebalancing. */
2437 if (ofproto->has_bonded_bundles) {
2438 struct ofbundle *bundle;
2440 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2442 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2447 return MIN(dp_max_idle, 1000);
2450 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2452 * This function also pushes statistics updates to rules which each facet
2453 * resubmits into. Generally these statistics will be accurate. However, if a
2454 * facet changes the rule it resubmits into at some time in between
2455 * update_stats() runs, it is possible that statistics accrued to the
2456 * old rule will be incorrectly attributed to the new rule. This could be
2457 * avoided by calling update_stats() whenever rules are created or
2458 * deleted. However, the performance impact of making so many calls to the
2459 * datapath do not justify the benefit of having perfectly accurate statistics.
2462 update_stats(struct ofproto_dpif *p)
2464 const struct dpif_flow_stats *stats;
2465 struct dpif_flow_dump dump;
2466 const struct nlattr *key;
2469 dpif_flow_dump_start(&dump, p->dpif);
2470 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2471 struct facet *facet;
2474 if (odp_flow_key_to_flow(key, key_len, &flow)) {
2478 odp_flow_key_format(key, key_len, &s);
2479 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
2485 facet = facet_find(p, &flow);
2487 if (facet && facet->installed) {
2489 if (stats->n_packets >= facet->dp_packet_count) {
2490 uint64_t extra = stats->n_packets - facet->dp_packet_count;
2491 facet->packet_count += extra;
2493 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2496 if (stats->n_bytes >= facet->dp_byte_count) {
2497 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
2499 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2502 facet->dp_packet_count = stats->n_packets;
2503 facet->dp_byte_count = stats->n_bytes;
2505 facet_update_time(p, facet, stats->used);
2506 facet_account(p, facet);
2507 facet_push_stats(facet);
2509 /* There's a flow in the datapath that we know nothing about.
2511 COVERAGE_INC(facet_unexpected);
2512 dpif_flow_del(p->dpif, key, key_len, NULL);
2515 dpif_flow_dump_done(&dump);
2518 /* Calculates and returns the number of milliseconds of idle time after which
2519 * facets should expire from the datapath and we should fold their statistics
2520 * into their parent rules in userspace. */
2522 facet_max_idle(const struct ofproto_dpif *ofproto)
2525 * Idle time histogram.
2527 * Most of the time a switch has a relatively small number of facets. When
2528 * this is the case we might as well keep statistics for all of them in
2529 * userspace and to cache them in the kernel datapath for performance as
2532 * As the number of facets increases, the memory required to maintain
2533 * statistics about them in userspace and in the kernel becomes
2534 * significant. However, with a large number of facets it is likely that
2535 * only a few of them are "heavy hitters" that consume a large amount of
2536 * bandwidth. At this point, only heavy hitters are worth caching in the
2537 * kernel and maintaining in userspaces; other facets we can discard.
2539 * The technique used to compute the idle time is to build a histogram with
2540 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
2541 * that is installed in the kernel gets dropped in the appropriate bucket.
2542 * After the histogram has been built, we compute the cutoff so that only
2543 * the most-recently-used 1% of facets (but at least
2544 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2545 * the most-recently-used bucket of facets is kept, so actually an
2546 * arbitrary number of facets can be kept in any given expiration run
2547 * (though the next run will delete most of those unless they receive
2550 * This requires a second pass through the facets, in addition to the pass
2551 * made by update_stats(), because the former function never looks
2552 * at uninstallable facets.
2554 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2555 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2556 int buckets[N_BUCKETS] = { 0 };
2557 int total, subtotal, bucket;
2558 struct facet *facet;
2562 total = hmap_count(&ofproto->facets);
2563 if (total <= ofproto->up.flow_eviction_threshold) {
2564 return N_BUCKETS * BUCKET_WIDTH;
2567 /* Build histogram. */
2569 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
2570 long long int idle = now - facet->used;
2571 int bucket = (idle <= 0 ? 0
2572 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2573 : (unsigned int) idle / BUCKET_WIDTH);
2577 /* Find the first bucket whose flows should be expired. */
2578 subtotal = bucket = 0;
2580 subtotal += buckets[bucket++];
2581 } while (bucket < N_BUCKETS &&
2582 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2584 if (VLOG_IS_DBG_ENABLED()) {
2588 ds_put_cstr(&s, "keep");
2589 for (i = 0; i < N_BUCKETS; i++) {
2591 ds_put_cstr(&s, ", drop");
2594 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2597 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2601 return bucket * BUCKET_WIDTH;
2605 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2607 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2608 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2609 struct ofexpired expired;
2611 if (facet->installed) {
2612 struct dpif_flow_stats stats;
2614 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2616 facet_update_stats(ofproto, facet, &stats);
2619 expired.flow = facet->flow;
2620 expired.packet_count = facet->packet_count;
2621 expired.byte_count = facet->byte_count;
2622 expired.used = facet->used;
2623 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2628 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2630 long long int cutoff = time_msec() - dp_max_idle;
2631 struct facet *facet, *next_facet;
2633 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2634 facet_active_timeout(ofproto, facet);
2635 if (facet->used < cutoff) {
2636 facet_remove(ofproto, facet);
2641 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2642 * then delete it entirely. */
2644 rule_expire(struct rule_dpif *rule)
2646 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2647 struct facet *facet, *next_facet;
2651 /* Has 'rule' expired? */
2653 if (rule->up.hard_timeout
2654 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2655 reason = OFPRR_HARD_TIMEOUT;
2656 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2657 && now > rule->used + rule->up.idle_timeout * 1000) {
2658 reason = OFPRR_IDLE_TIMEOUT;
2663 COVERAGE_INC(ofproto_dpif_expired);
2665 /* Update stats. (This is a no-op if the rule expired due to an idle
2666 * timeout, because that only happens when the rule has no facets left.) */
2667 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2668 facet_remove(ofproto, facet);
2671 /* Get rid of the rule. */
2672 ofproto_rule_expire(&rule->up, reason);
2677 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2679 * The caller must already have determined that no facet with an identical
2680 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2681 * the ofproto's classifier table.
2683 * The facet will initially have no ODP actions. The caller should fix that
2684 * by calling facet_make_actions(). */
2685 static struct facet *
2686 facet_create(struct rule_dpif *rule, const struct flow *flow)
2688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2689 struct facet *facet;
2691 facet = xzalloc(sizeof *facet);
2692 facet->used = time_msec();
2693 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2694 list_push_back(&rule->facets, &facet->list_node);
2696 facet->flow = *flow;
2697 netflow_flow_init(&facet->nf_flow);
2698 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2704 facet_free(struct facet *facet)
2706 free(facet->actions);
2710 /* If the 'actions_len' bytes of actions in 'odp_actions' are just a single
2711 * OVS_ACTION_ATTR_USERSPACE action, executes it internally and returns true.
2712 * Otherwise, returns false without doing anything.
2714 * If 'clone' is true, the caller always retains ownership of 'packet'.
2715 * Otherwise, ownership is transferred to this function if it returns true. */
2717 execute_controller_action(struct ofproto_dpif *ofproto,
2718 const struct flow *flow,
2719 const struct nlattr *odp_actions, size_t actions_len,
2720 struct ofpbuf *packet, bool clone)
2723 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2724 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2725 /* As an optimization, avoid a round-trip from userspace to kernel to
2726 * userspace. This also avoids possibly filling up kernel packet
2727 * buffers along the way.
2729 * This optimization will not accidentally catch sFlow
2730 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2731 * inside OVS_ACTION_ATTR_SAMPLE. */
2732 const struct nlattr *nla;
2734 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2735 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2743 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2744 * 'packet', which arrived on 'in_port'.
2746 * Takes ownership of 'packet'. */
2748 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2749 const struct nlattr *odp_actions, size_t actions_len,
2750 struct ofpbuf *packet)
2752 struct odputil_keybuf keybuf;
2756 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2761 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2762 odp_flow_key_from_flow(&key, flow);
2764 error = dpif_execute(ofproto->dpif, key.data, key.size,
2765 odp_actions, actions_len, packet);
2767 ofpbuf_delete(packet);
2771 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2772 * statistics appropriately. 'packet' must have at least sizeof(struct
2773 * ofp_packet_in) bytes of headroom.
2775 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2776 * applying flow_extract() to 'packet' would yield the same flow as
2779 * 'facet' must have accurately composed datapath actions; that is, it must
2780 * not be in need of revalidation.
2782 * Takes ownership of 'packet'. */
2784 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2785 struct ofpbuf *packet)
2787 struct dpif_flow_stats stats;
2789 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2791 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2792 stats.used = time_msec();
2793 if (execute_odp_actions(ofproto, &facet->flow,
2794 facet->actions, facet->actions_len, packet)) {
2795 facet_update_stats(ofproto, facet, &stats);
2799 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2801 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2802 * rule's statistics, via facet_uninstall().
2804 * - Removes 'facet' from its rule and from ofproto->facets.
2807 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2809 facet_uninstall(ofproto, facet);
2810 facet_flush_stats(ofproto, facet);
2811 hmap_remove(&ofproto->facets, &facet->hmap_node);
2812 list_remove(&facet->list_node);
2816 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2818 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2819 const struct ofpbuf *packet)
2821 const struct rule_dpif *rule = facet->rule;
2822 struct ofpbuf *odp_actions;
2823 struct action_xlate_ctx ctx;
2825 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2826 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2827 facet->tags = ctx.tags;
2828 facet->may_install = ctx.may_set_up_flow;
2829 facet->has_learn = ctx.has_learn;
2830 facet->has_normal = ctx.has_normal;
2831 facet->nf_flow.output_iface = ctx.nf_output_iface;
2833 if (facet->actions_len != odp_actions->size
2834 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2835 free(facet->actions);
2836 facet->actions_len = odp_actions->size;
2837 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2840 ofpbuf_delete(odp_actions);
2843 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2844 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2845 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2846 * since 'facet' was last updated.
2848 * Returns 0 if successful, otherwise a positive errno value.*/
2850 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2851 const struct nlattr *actions, size_t actions_len,
2852 struct dpif_flow_stats *stats)
2854 struct odputil_keybuf keybuf;
2855 enum dpif_flow_put_flags flags;
2859 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2861 flags |= DPIF_FP_ZERO_STATS;
2864 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2865 odp_flow_key_from_flow(&key, &facet->flow);
2867 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2868 actions, actions_len, stats);
2871 facet_reset_dp_stats(facet, stats);
2877 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2878 * 'zero_stats' is true, clears any existing statistics from the datapath for
2881 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2883 struct dpif_flow_stats stats;
2885 if (facet->may_install
2886 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2887 zero_stats ? &stats : NULL)) {
2888 facet->installed = true;
2893 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2896 const struct nlattr *a;
2900 if (facet->byte_count <= facet->accounted_bytes) {
2903 n_bytes = facet->byte_count - facet->accounted_bytes;
2904 facet->accounted_bytes = facet->byte_count;
2906 /* Feed information from the active flows back into the learning table to
2907 * ensure that table is always in sync with what is actually flowing
2908 * through the datapath. */
2909 if (facet->has_learn || facet->has_normal) {
2910 struct action_xlate_ctx ctx;
2912 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2913 ctx.may_learn = true;
2914 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2915 facet->rule->up.n_actions));
2918 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2922 /* This loop feeds byte counters to bond_account() for rebalancing to use
2923 * as a basis. We also need to track the actual VLAN on which the packet
2924 * is going to be sent to ensure that it matches the one passed to
2925 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2927 vlan_tci = facet->flow.vlan_tci;
2928 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2929 struct ofport_dpif *port;
2931 switch (nl_attr_type(a)) {
2932 const struct nlattr *nested;
2933 case OVS_ACTION_ATTR_OUTPUT:
2934 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2935 if (port && port->bundle && port->bundle->bond) {
2936 bond_account(port->bundle->bond, &facet->flow,
2937 vlan_tci_to_vid(vlan_tci), n_bytes);
2941 case OVS_ACTION_ATTR_POP:
2942 if (nl_attr_get_u16(a) == OVS_KEY_ATTR_8021Q) {
2943 vlan_tci = htons(0);
2947 case OVS_ACTION_ATTR_PUSH:
2948 nested = nl_attr_get(a);
2949 if (nl_attr_type(nested) == OVS_KEY_ATTR_8021Q) {
2950 const struct ovs_key_8021q *q_key;
2952 q_key = nl_attr_get_unspec(nested, sizeof(*q_key));
2953 vlan_tci = q_key->q_tci;
2960 /* If 'rule' is installed in the datapath, uninstalls it. */
2962 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2964 if (facet->installed) {
2965 struct odputil_keybuf keybuf;
2966 struct dpif_flow_stats stats;
2970 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2971 odp_flow_key_from_flow(&key, &facet->flow);
2973 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2974 facet_reset_dp_stats(facet, &stats);
2976 facet_update_stats(p, facet, &stats);
2978 facet->installed = false;
2980 assert(facet->dp_packet_count == 0);
2981 assert(facet->dp_byte_count == 0);
2985 /* Returns true if the only action for 'facet' is to send to the controller.
2986 * (We don't report NetFlow expiration messages for such facets because they
2987 * are just part of the control logic for the network, not real traffic). */
2989 facet_is_controller_flow(struct facet *facet)
2992 && facet->rule->up.n_actions == 1
2993 && action_outputs_to_port(&facet->rule->up.actions[0],
2994 htons(OFPP_CONTROLLER)));
2997 /* Resets 'facet''s datapath statistics counters. This should be called when
2998 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2999 * it should contain the statistics returned by dpif when 'facet' was reset in
3000 * the datapath. 'stats' will be modified to only included statistics new
3001 * since 'facet' was last updated. */
3003 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
3005 if (stats && facet->dp_packet_count <= stats->n_packets
3006 && facet->dp_byte_count <= stats->n_bytes) {
3007 stats->n_packets -= facet->dp_packet_count;
3008 stats->n_bytes -= facet->dp_byte_count;
3011 facet->dp_packet_count = 0;
3012 facet->dp_byte_count = 0;
3015 /* Folds all of 'facet''s statistics into its rule. Also updates the
3016 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3017 * 'facet''s statistics in the datapath should have been zeroed and folded into
3018 * its packet and byte counts before this function is called. */
3020 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3022 assert(!facet->dp_byte_count);
3023 assert(!facet->dp_packet_count);
3025 facet_push_stats(facet);
3026 facet_account(ofproto, facet);
3028 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3029 struct ofexpired expired;
3030 expired.flow = facet->flow;
3031 expired.packet_count = facet->packet_count;
3032 expired.byte_count = facet->byte_count;
3033 expired.used = facet->used;
3034 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3037 facet->rule->packet_count += facet->packet_count;
3038 facet->rule->byte_count += facet->byte_count;
3040 /* Reset counters to prevent double counting if 'facet' ever gets
3042 facet_reset_counters(facet);
3044 netflow_flow_clear(&facet->nf_flow);
3047 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3048 * Returns it if found, otherwise a null pointer.
3050 * The returned facet might need revalidation; use facet_lookup_valid()
3051 * instead if that is important. */
3052 static struct facet *
3053 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3055 struct facet *facet;
3057 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3059 if (flow_equal(flow, &facet->flow)) {
3067 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3068 * Returns it if found, otherwise a null pointer.
3070 * The returned facet is guaranteed to be valid. */
3071 static struct facet *
3072 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3074 struct facet *facet = facet_find(ofproto, flow);
3076 /* The facet we found might not be valid, since we could be in need of
3077 * revalidation. If it is not valid, don't return it. */
3079 && (ofproto->need_revalidate
3080 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3081 && !facet_revalidate(ofproto, facet)) {
3082 COVERAGE_INC(facet_invalidated);
3089 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3091 * - If the rule found is different from 'facet''s current rule, moves
3092 * 'facet' to the new rule and recompiles its actions.
3094 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3095 * where it is and recompiles its actions anyway.
3097 * - If there is none, destroys 'facet'.
3099 * Returns true if 'facet' still exists, false if it has been destroyed. */
3101 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3103 struct action_xlate_ctx ctx;
3104 struct ofpbuf *odp_actions;
3105 struct rule_dpif *new_rule;
3106 bool actions_changed;
3108 COVERAGE_INC(facet_revalidate);
3110 /* Determine the new rule. */
3111 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3113 /* No new rule, so delete the facet. */
3114 facet_remove(ofproto, facet);
3118 /* Calculate new datapath actions.
3120 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3121 * emit a NetFlow expiration and, if so, we need to have the old state
3122 * around to properly compose it. */
3123 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3124 odp_actions = xlate_actions(&ctx,
3125 new_rule->up.actions, new_rule->up.n_actions);
3126 actions_changed = (facet->actions_len != odp_actions->size
3127 || memcmp(facet->actions, odp_actions->data,
3128 facet->actions_len));
3130 /* If the datapath actions changed or the installability changed,
3131 * then we need to talk to the datapath. */
3132 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
3133 if (ctx.may_set_up_flow) {
3134 struct dpif_flow_stats stats;
3136 facet_put__(ofproto, facet,
3137 odp_actions->data, odp_actions->size, &stats);
3138 facet_update_stats(ofproto, facet, &stats);
3140 facet_uninstall(ofproto, facet);
3143 /* The datapath flow is gone or has zeroed stats, so push stats out of
3144 * 'facet' into 'rule'. */
3145 facet_flush_stats(ofproto, facet);
3148 /* Update 'facet' now that we've taken care of all the old state. */
3149 facet->tags = ctx.tags;
3150 facet->nf_flow.output_iface = ctx.nf_output_iface;
3151 facet->may_install = ctx.may_set_up_flow;
3152 facet->has_learn = ctx.has_learn;
3153 facet->has_normal = ctx.has_normal;
3154 if (actions_changed) {
3155 free(facet->actions);
3156 facet->actions_len = odp_actions->size;
3157 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
3159 if (facet->rule != new_rule) {
3160 COVERAGE_INC(facet_changed_rule);
3161 list_remove(&facet->list_node);
3162 list_push_back(&new_rule->facets, &facet->list_node);
3163 facet->rule = new_rule;
3164 facet->used = new_rule->up.created;
3165 facet->rs_used = facet->used;
3168 ofpbuf_delete(odp_actions);
3173 /* Updates 'facet''s used time. Caller is responsible for calling
3174 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3176 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3179 if (used > facet->used) {
3181 if (used > facet->rule->used) {
3182 facet->rule->used = used;
3184 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3188 /* Folds the statistics from 'stats' into the counters in 'facet'.
3190 * Because of the meaning of a facet's counters, it only makes sense to do this
3191 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3192 * packet that was sent by hand or if it represents statistics that have been
3193 * cleared out of the datapath. */
3195 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
3196 const struct dpif_flow_stats *stats)
3198 if (stats->n_packets || stats->used > facet->used) {
3199 facet_update_time(ofproto, facet, stats->used);
3200 facet->packet_count += stats->n_packets;
3201 facet->byte_count += stats->n_bytes;
3202 facet_push_stats(facet);
3203 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3208 facet_reset_counters(struct facet *facet)
3210 facet->packet_count = 0;
3211 facet->byte_count = 0;
3212 facet->rs_packet_count = 0;
3213 facet->rs_byte_count = 0;
3214 facet->accounted_bytes = 0;
3218 facet_push_stats(struct facet *facet)
3220 uint64_t rs_packets, rs_bytes;
3222 assert(facet->packet_count >= facet->rs_packet_count);
3223 assert(facet->byte_count >= facet->rs_byte_count);
3224 assert(facet->used >= facet->rs_used);
3226 rs_packets = facet->packet_count - facet->rs_packet_count;
3227 rs_bytes = facet->byte_count - facet->rs_byte_count;
3229 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3230 facet->rs_packet_count = facet->packet_count;
3231 facet->rs_byte_count = facet->byte_count;
3232 facet->rs_used = facet->used;
3234 flow_push_stats(facet->rule, &facet->flow,
3235 rs_packets, rs_bytes, facet->used);
3239 struct ofproto_push {
3240 struct action_xlate_ctx ctx;
3247 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3249 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3252 rule->packet_count += push->packets;
3253 rule->byte_count += push->bytes;
3254 rule->used = MAX(push->used, rule->used);
3258 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3259 * 'rule''s actions. */
3261 flow_push_stats(const struct rule_dpif *rule,
3262 struct flow *flow, uint64_t packets, uint64_t bytes,
3265 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3266 struct ofproto_push push;
3268 push.packets = packets;
3272 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3273 push.ctx.resubmit_hook = push_resubmit;
3274 ofpbuf_delete(xlate_actions(&push.ctx,
3275 rule->up.actions, rule->up.n_actions));
3280 static struct rule_dpif *
3281 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3284 struct cls_rule *cls_rule;
3285 struct classifier *cls;
3287 if (table_id >= N_TABLES) {
3291 cls = &ofproto->up.tables[table_id];
3292 if (flow->tos_frag & FLOW_FRAG_ANY
3293 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3294 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3295 * are unavailable. */
3296 struct flow ofpc_normal_flow = *flow;
3297 ofpc_normal_flow.tp_src = htons(0);
3298 ofpc_normal_flow.tp_dst = htons(0);
3299 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3301 cls_rule = classifier_lookup(cls, flow);
3303 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3307 complete_operation(struct rule_dpif *rule)
3309 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3311 rule_invalidate(rule);
3313 struct dpif_completion *c = xmalloc(sizeof *c);
3314 c->op = rule->up.pending;
3315 list_push_back(&ofproto->completions, &c->list_node);
3317 ofoperation_complete(rule->up.pending, 0);
3321 static struct rule *
3324 struct rule_dpif *rule = xmalloc(sizeof *rule);
3329 rule_dealloc(struct rule *rule_)
3331 struct rule_dpif *rule = rule_dpif_cast(rule_);
3336 rule_construct(struct rule *rule_)
3338 struct rule_dpif *rule = rule_dpif_cast(rule_);
3339 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3340 struct rule_dpif *victim;
3344 error = validate_actions(rule->up.actions, rule->up.n_actions,
3345 &rule->up.cr.flow, ofproto->max_ports);
3350 rule->used = rule->up.created;
3351 rule->packet_count = 0;
3352 rule->byte_count = 0;
3354 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3355 if (victim && !list_is_empty(&victim->facets)) {
3356 struct facet *facet;
3358 rule->facets = victim->facets;
3359 list_moved(&rule->facets);
3360 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3361 /* XXX: We're only clearing our local counters here. It's possible
3362 * that quite a few packets are unaccounted for in the datapath
3363 * statistics. These will be accounted to the new rule instead of
3364 * cleared as required. This could be fixed by clearing out the
3365 * datapath statistics for this facet, but currently it doesn't
3367 facet_reset_counters(facet);
3371 /* Must avoid list_moved() in this case. */
3372 list_init(&rule->facets);
3375 table_id = rule->up.table_id;
3376 rule->tag = (victim ? victim->tag
3378 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3379 ofproto->tables[table_id].basis));
3381 complete_operation(rule);
3386 rule_destruct(struct rule *rule_)
3388 struct rule_dpif *rule = rule_dpif_cast(rule_);
3389 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3390 struct facet *facet, *next_facet;
3392 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3393 facet_revalidate(ofproto, facet);
3396 complete_operation(rule);
3400 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3402 struct rule_dpif *rule = rule_dpif_cast(rule_);
3403 struct facet *facet;
3405 /* Start from historical data for 'rule' itself that are no longer tracked
3406 * in facets. This counts, for example, facets that have expired. */
3407 *packets = rule->packet_count;
3408 *bytes = rule->byte_count;
3410 /* Add any statistics that are tracked by facets. This includes
3411 * statistical data recently updated by ofproto_update_stats() as well as
3412 * stats for packets that were executed "by hand" via dpif_execute(). */
3413 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3414 *packets += facet->packet_count;
3415 *bytes += facet->byte_count;
3420 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
3422 struct rule_dpif *rule = rule_dpif_cast(rule_);
3423 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3424 struct action_xlate_ctx ctx;
3425 struct ofpbuf *odp_actions;
3426 struct facet *facet;
3429 /* First look for a related facet. If we find one, account it to that. */
3430 facet = facet_lookup_valid(ofproto, flow);
3431 if (facet && facet->rule == rule) {
3432 if (!facet->may_install) {
3433 facet_make_actions(ofproto, facet, packet);
3435 facet_execute(ofproto, facet, packet);
3439 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
3440 * create a new facet for it and use that. */
3441 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
3442 facet = facet_create(rule, flow);
3443 facet_make_actions(ofproto, facet, packet);
3444 facet_execute(ofproto, facet, packet);
3445 facet_install(ofproto, facet, true);
3449 /* We can't account anything to a facet. If we were to try, then that
3450 * facet would have a non-matching rule, busting our invariants. */
3451 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3452 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3453 size = packet->size;
3454 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3455 odp_actions->size, packet)) {
3456 rule->used = time_msec();
3457 rule->packet_count++;
3458 rule->byte_count += size;
3459 flow_push_stats(rule, flow, 1, size, rule->used);
3461 ofpbuf_delete(odp_actions);
3467 rule_modify_actions(struct rule *rule_)
3469 struct rule_dpif *rule = rule_dpif_cast(rule_);
3470 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3473 error = validate_actions(rule->up.actions, rule->up.n_actions,
3474 &rule->up.cr.flow, ofproto->max_ports);
3476 ofoperation_complete(rule->up.pending, error);
3480 complete_operation(rule);
3483 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.
3484 * Returns 0 if successful, otherwise a positive errno value. */
3486 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
3487 const struct ofpbuf *packet)
3489 struct ofpbuf key, odp_actions;
3490 struct odputil_keybuf keybuf;
3494 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
3495 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3496 odp_flow_key_from_flow(&key, &flow);
3498 ofpbuf_init(&odp_actions, 32);
3499 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3501 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3502 error = dpif_execute(ofproto->dpif,
3504 odp_actions.data, odp_actions.size,
3506 ofpbuf_uninit(&odp_actions);
3509 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3510 ofproto->up.name, odp_port, strerror(error));
3515 /* OpenFlow to datapath action translation. */
3517 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3518 struct action_xlate_ctx *ctx);
3519 static void xlate_normal(struct action_xlate_ctx *);
3522 put_userspace_action(const struct ofproto_dpif *ofproto,
3523 struct ofpbuf *odp_actions,
3524 const struct flow *flow,
3525 const struct user_action_cookie *cookie)
3530 pid = dpif_port_get_pid(ofproto->dpif,
3531 ofp_port_to_odp_port(flow->in_port));
3533 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3534 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3535 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3536 cookie, sizeof *cookie);
3537 nl_msg_end_nested(odp_actions, offset);
3539 return odp_actions->size - NLA_ALIGN(sizeof *cookie);
3542 /* Compose SAMPLE action for sFlow. */
3544 compose_sflow_action(const struct ofproto_dpif *ofproto,
3545 struct ofpbuf *odp_actions,
3546 const struct flow *flow,
3549 uint32_t port_ifindex;
3550 uint32_t probability;
3551 struct user_action_cookie cookie;
3552 size_t sample_offset, actions_offset;
3553 int cookie_offset, n_output;
3555 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3559 if (odp_port == OVSP_NONE) {
3563 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3567 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3569 /* Number of packets out of UINT_MAX to sample. */
3570 probability = dpif_sflow_get_probability(ofproto->sflow);
3571 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3573 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3575 cookie.type = USER_ACTION_COOKIE_SFLOW;
3576 cookie.data = port_ifindex;
3577 cookie.n_output = n_output;
3578 cookie.vlan_tci = 0;
3579 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3581 nl_msg_end_nested(odp_actions, actions_offset);
3582 nl_msg_end_nested(odp_actions, sample_offset);
3583 return cookie_offset;
3586 /* SAMPLE action must be first action in any given list of actions.
3587 * At this point we do not have all information required to build it. So try to
3588 * build sample action as complete as possible. */
3590 add_sflow_action(struct action_xlate_ctx *ctx)
3592 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3594 &ctx->flow, OVSP_NONE);
3595 ctx->sflow_odp_port = 0;
3596 ctx->sflow_n_outputs = 0;
3599 /* Fix SAMPLE action according to data collected while composing ODP actions.
3600 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3601 * USERSPACE action's user-cookie which is required for sflow. */
3603 fix_sflow_action(struct action_xlate_ctx *ctx)
3605 const struct flow *base = &ctx->base_flow;
3606 struct user_action_cookie *cookie;
3608 if (!ctx->user_cookie_offset) {
3612 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3614 assert(cookie != NULL);
3615 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3617 if (ctx->sflow_n_outputs) {
3618 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3619 ctx->sflow_odp_port);
3621 if (ctx->sflow_n_outputs >= 255) {
3622 cookie->n_output = 255;
3624 cookie->n_output = ctx->sflow_n_outputs;
3626 cookie->vlan_tci = base->vlan_tci;
3630 commit_action__(struct ofpbuf *odp_actions,
3631 enum ovs_action_attr act_type,
3632 enum ovs_key_attr key_type,
3633 const void *key, size_t key_size)
3635 size_t offset = nl_msg_start_nested(odp_actions, act_type);
3637 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3638 nl_msg_end_nested(odp_actions, offset);
3642 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3643 struct ofpbuf *odp_actions)
3645 if (base->tun_id == flow->tun_id) {
3648 base->tun_id = flow->tun_id;
3650 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3651 OVS_KEY_ATTR_TUN_ID, &base->tun_id, sizeof(base->tun_id));
3655 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3656 struct ofpbuf *odp_actions)
3658 struct ovs_key_ethernet eth_key;
3660 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3661 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3665 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3666 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3668 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3669 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3671 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3672 OVS_KEY_ATTR_ETHERNET, ð_key, sizeof(eth_key));
3676 commit_vlan_action(struct action_xlate_ctx *ctx, ovs_be16 new_tci)
3678 struct flow *base = &ctx->base_flow;
3680 if (base->vlan_tci == new_tci) {
3684 if (base->vlan_tci & htons(VLAN_CFI)) {
3685 nl_msg_put_u16(ctx->odp_actions, OVS_ACTION_ATTR_POP,
3686 OVS_KEY_ATTR_8021Q);
3689 if (new_tci & htons(VLAN_CFI)) {
3690 struct ovs_key_8021q q_key;
3692 q_key.q_tpid = htons(ETH_TYPE_VLAN);
3693 q_key.q_tci = new_tci & ~htons(VLAN_CFI);
3695 commit_action__(ctx->odp_actions, OVS_ACTION_ATTR_PUSH,
3696 OVS_KEY_ATTR_8021Q, &q_key, sizeof(q_key));
3698 base->vlan_tci = new_tci;
3702 commit_set_nw_action(const struct flow *flow, struct flow *base,
3703 struct ofpbuf *odp_actions)
3705 int frag = base->tos_frag & FLOW_FRAG_MASK;
3706 struct ovs_key_ipv4 ipv4_key;
3708 if (base->dl_type != htons(ETH_TYPE_IP) ||
3709 !base->nw_src || !base->nw_dst) {
3713 if (base->nw_src == flow->nw_src &&
3714 base->nw_dst == flow->nw_dst &&
3715 base->tos_frag == flow->tos_frag) {
3720 memset(&ipv4_key, 0, sizeof(ipv4_key));
3721 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3722 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3723 ipv4_key.ipv4_proto = base->nw_proto;
3724 ipv4_key.ipv4_tos = flow->tos_frag & IP_DSCP_MASK;
3725 ipv4_key.ipv4_frag = (frag == 0 ? OVS_FRAG_TYPE_NONE
3726 : frag == FLOW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
3727 : OVS_FRAG_TYPE_LATER);
3729 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3730 OVS_KEY_ATTR_IPV4, &ipv4_key, sizeof(ipv4_key));
3734 commit_set_port_action(const struct flow *flow, struct flow *base,
3735 struct ofpbuf *odp_actions)
3737 if (!base->tp_src || !base->tp_dst) {
3741 if (base->tp_src == flow->tp_src &&
3742 base->tp_dst == flow->tp_dst) {
3746 if (flow->nw_proto == IPPROTO_TCP) {
3747 struct ovs_key_tcp port_key;
3749 port_key.tcp_src = base->tp_src = flow->tp_src;
3750 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3752 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3753 OVS_KEY_ATTR_TCP, &port_key, sizeof(port_key));
3755 } else if (flow->nw_proto == IPPROTO_UDP) {
3756 struct ovs_key_udp port_key;
3758 port_key.udp_src = base->tp_src = flow->tp_src;
3759 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3761 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3762 OVS_KEY_ATTR_UDP, &port_key, sizeof(port_key));
3767 commit_priority_action(struct action_xlate_ctx *ctx)
3769 if (ctx->base_priority == ctx->priority) {
3773 if (ctx->priority) {
3774 nl_msg_put_u32(ctx->odp_actions,
3775 OVS_ACTION_ATTR_SET_PRIORITY, ctx->priority);
3777 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
3779 ctx->base_priority = ctx->priority;
3783 commit_odp_actions(struct action_xlate_ctx *ctx)
3785 const struct flow *flow = &ctx->flow;
3786 struct flow *base = &ctx->base_flow;
3787 struct ofpbuf *odp_actions = ctx->odp_actions;
3789 commit_set_tun_id_action(flow, base, odp_actions);
3790 commit_set_ether_addr_action(flow, base, odp_actions);
3791 commit_vlan_action(ctx, flow->vlan_tci);
3792 commit_set_nw_action(flow, base, odp_actions);
3793 commit_set_port_action(flow, base, odp_actions);
3794 commit_priority_action(ctx);
3798 compose_output_action(struct action_xlate_ctx *ctx, uint16_t odp_port)
3800 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3801 ctx->sflow_odp_port = odp_port;
3802 ctx->sflow_n_outputs++;
3806 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
3808 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
3809 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
3812 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
3813 || !stp_forward_in_state(ofport->stp_state)) {
3814 /* Forwarding disabled on port. */
3819 * We don't have an ofport record for this port, but it doesn't hurt to
3820 * allow forwarding to it anyhow. Maybe such a port will appear later
3821 * and we're pre-populating the flow table.
3825 commit_odp_actions(ctx);
3826 compose_output_action(ctx, odp_port);
3827 ctx->nf_output_iface = ofp_port;
3831 xlate_table_action(struct action_xlate_ctx *ctx,
3832 uint16_t in_port, uint8_t table_id)
3834 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3835 struct ofproto_dpif *ofproto = ctx->ofproto;
3836 struct rule_dpif *rule;
3837 uint16_t old_in_port;
3838 uint8_t old_table_id;
3840 old_table_id = ctx->table_id;
3841 ctx->table_id = table_id;
3843 /* Look up a flow with 'in_port' as the input port. */
3844 old_in_port = ctx->flow.in_port;
3845 ctx->flow.in_port = in_port;
3846 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3849 if (table_id > 0 && table_id < N_TABLES) {
3850 struct table_dpif *table = &ofproto->tables[table_id];
3851 if (table->other_table) {
3854 : rule_calculate_tag(&ctx->flow,
3855 &table->other_table->wc,
3860 /* Restore the original input port. Otherwise OFPP_NORMAL and
3861 * OFPP_IN_PORT will have surprising behavior. */
3862 ctx->flow.in_port = old_in_port;
3864 if (ctx->resubmit_hook) {
3865 ctx->resubmit_hook(ctx, rule);
3870 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3874 ctx->table_id = old_table_id;
3876 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3878 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3879 MAX_RESUBMIT_RECURSION);
3884 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3885 const struct nx_action_resubmit *nar)
3890 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3892 : ntohs(nar->in_port));
3893 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3895 xlate_table_action(ctx, in_port, table_id);
3899 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3901 struct ofport_dpif *ofport;
3903 commit_odp_actions(ctx);
3904 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3905 uint16_t ofp_port = ofport->up.ofp_port;
3906 if (ofp_port != ctx->flow.in_port
3907 && !(ofport->up.opp.config & mask)
3908 && stp_forward_in_state(ofport->stp_state)) {
3909 compose_output_action(ctx, ofport->odp_port);
3913 ctx->nf_output_iface = NF_OUT_FLOOD;
3917 compose_controller_action(struct action_xlate_ctx *ctx, int len)
3919 struct user_action_cookie cookie;
3921 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
3923 cookie.n_output = 0;
3924 cookie.vlan_tci = 0;
3925 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
3929 xlate_output_action__(struct action_xlate_ctx *ctx,
3930 uint16_t port, uint16_t max_len)
3932 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3934 ctx->nf_output_iface = NF_OUT_DROP;
3938 add_output_action(ctx, ctx->flow.in_port);
3941 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3947 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3950 flood_packets(ctx, htonl(0));
3952 case OFPP_CONTROLLER:
3953 commit_odp_actions(ctx);
3954 compose_controller_action(ctx, max_len);
3960 if (port != ctx->flow.in_port) {
3961 add_output_action(ctx, port);
3966 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3967 ctx->nf_output_iface = NF_OUT_FLOOD;
3968 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3969 ctx->nf_output_iface = prev_nf_output_iface;
3970 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3971 ctx->nf_output_iface != NF_OUT_FLOOD) {
3972 ctx->nf_output_iface = NF_OUT_MULTI;
3977 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3978 const struct nx_action_output_reg *naor)
3982 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3984 if (ofp_port <= UINT16_MAX) {
3985 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3990 xlate_output_action(struct action_xlate_ctx *ctx,
3991 const struct ofp_action_output *oao)
3993 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3997 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3998 const struct ofp_action_enqueue *oae)
4000 uint16_t ofp_port, odp_port;
4001 uint32_t ctx_priority, priority;
4004 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4007 /* Fall back to ordinary output action. */
4008 xlate_output_action__(ctx, ntohs(oae->port), 0);
4012 /* Figure out datapath output port. */
4013 ofp_port = ntohs(oae->port);
4014 if (ofp_port == OFPP_IN_PORT) {
4015 ofp_port = ctx->flow.in_port;
4016 } else if (ofp_port == ctx->flow.in_port) {
4019 odp_port = ofp_port_to_odp_port(ofp_port);
4021 /* Add datapath actions. */
4022 ctx_priority = ctx->priority;
4023 ctx->priority = priority;
4024 add_output_action(ctx, odp_port);
4025 ctx->priority = ctx_priority;
4027 /* Update NetFlow output port. */
4028 if (ctx->nf_output_iface == NF_OUT_DROP) {
4029 ctx->nf_output_iface = odp_port;
4030 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4031 ctx->nf_output_iface = NF_OUT_MULTI;
4036 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4037 const struct nx_action_set_queue *nasq)
4042 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4045 /* Couldn't translate queue to a priority, so ignore. A warning
4046 * has already been logged. */
4050 ctx->priority = priority;
4053 struct xlate_reg_state {
4059 xlate_autopath(struct action_xlate_ctx *ctx,
4060 const struct nx_action_autopath *naa)
4062 uint16_t ofp_port = ntohl(naa->id);
4063 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4065 if (!port || !port->bundle) {
4066 ofp_port = OFPP_NONE;
4067 } else if (port->bundle->bond) {
4068 /* Autopath does not support VLAN hashing. */
4069 struct ofport_dpif *slave = bond_choose_output_slave(
4070 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4072 ofp_port = slave->up.ofp_port;
4075 autopath_execute(naa, &ctx->flow, ofp_port);
4079 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4081 struct ofproto_dpif *ofproto = ofproto_;
4082 struct ofport_dpif *port;
4092 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4095 port = get_ofp_port(ofproto, ofp_port);
4096 return port ? port->may_enable : false;
4101 xlate_learn_action(struct action_xlate_ctx *ctx,
4102 const struct nx_action_learn *learn)
4104 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4105 struct ofputil_flow_mod fm;
4108 learn_execute(learn, &ctx->flow, &fm);
4110 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4111 if (error && !VLOG_DROP_WARN(&rl)) {
4112 char *msg = ofputil_error_to_string(error);
4113 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4121 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4123 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4124 ? htonl(OFPPC_NO_RECV_STP)
4125 : htonl(OFPPC_NO_RECV))) {
4129 /* Only drop packets here if both forwarding and learning are
4130 * disabled. If just learning is enabled, we need to have
4131 * OFPP_NORMAL and the learning action have a look at the packet
4132 * before we can drop it. */
4133 if (!stp_forward_in_state(port->stp_state)
4134 && !stp_learn_in_state(port->stp_state)) {
4142 do_xlate_actions(const union ofp_action *in, size_t n_in,
4143 struct action_xlate_ctx *ctx)
4145 const struct ofport_dpif *port;
4146 const union ofp_action *ia;
4149 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4150 if (port && !may_receive(port, ctx)) {
4151 /* Drop this flow. */
4155 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4156 const struct ofp_action_dl_addr *oada;
4157 const struct nx_action_resubmit *nar;
4158 const struct nx_action_set_tunnel *nast;
4159 const struct nx_action_set_queue *nasq;
4160 const struct nx_action_multipath *nam;
4161 const struct nx_action_autopath *naa;
4162 const struct nx_action_bundle *nab;
4163 const struct nx_action_output_reg *naor;
4164 enum ofputil_action_code code;
4167 code = ofputil_decode_action_unsafe(ia);
4169 case OFPUTIL_OFPAT_OUTPUT:
4170 xlate_output_action(ctx, &ia->output);
4173 case OFPUTIL_OFPAT_SET_VLAN_VID:
4174 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4175 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4178 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4179 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4180 ctx->flow.vlan_tci |= htons(
4181 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4184 case OFPUTIL_OFPAT_STRIP_VLAN:
4185 ctx->flow.vlan_tci = htons(0);
4188 case OFPUTIL_OFPAT_SET_DL_SRC:
4189 oada = ((struct ofp_action_dl_addr *) ia);
4190 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4193 case OFPUTIL_OFPAT_SET_DL_DST:
4194 oada = ((struct ofp_action_dl_addr *) ia);
4195 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4198 case OFPUTIL_OFPAT_SET_NW_SRC:
4199 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4202 case OFPUTIL_OFPAT_SET_NW_DST:
4203 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4206 case OFPUTIL_OFPAT_SET_NW_TOS:
4207 ctx->flow.tos_frag &= ~IP_DSCP_MASK;
4208 ctx->flow.tos_frag |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4211 case OFPUTIL_OFPAT_SET_TP_SRC:
4212 ctx->flow.tp_src = ia->tp_port.tp_port;
4215 case OFPUTIL_OFPAT_SET_TP_DST:
4216 ctx->flow.tp_dst = ia->tp_port.tp_port;
4219 case OFPUTIL_OFPAT_ENQUEUE:
4220 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4223 case OFPUTIL_NXAST_RESUBMIT:
4224 nar = (const struct nx_action_resubmit *) ia;
4225 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4228 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4229 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4232 case OFPUTIL_NXAST_SET_TUNNEL:
4233 nast = (const struct nx_action_set_tunnel *) ia;
4234 tun_id = htonll(ntohl(nast->tun_id));
4235 ctx->flow.tun_id = tun_id;
4238 case OFPUTIL_NXAST_SET_QUEUE:
4239 nasq = (const struct nx_action_set_queue *) ia;
4240 xlate_set_queue_action(ctx, nasq);
4243 case OFPUTIL_NXAST_POP_QUEUE:
4247 case OFPUTIL_NXAST_REG_MOVE:
4248 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4252 case OFPUTIL_NXAST_REG_LOAD:
4253 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4257 case OFPUTIL_NXAST_NOTE:
4258 /* Nothing to do. */
4261 case OFPUTIL_NXAST_SET_TUNNEL64:
4262 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4263 ctx->flow.tun_id = tun_id;
4266 case OFPUTIL_NXAST_MULTIPATH:
4267 nam = (const struct nx_action_multipath *) ia;
4268 multipath_execute(nam, &ctx->flow);
4271 case OFPUTIL_NXAST_AUTOPATH:
4272 naa = (const struct nx_action_autopath *) ia;
4273 xlate_autopath(ctx, naa);
4276 case OFPUTIL_NXAST_BUNDLE:
4277 ctx->ofproto->has_bundle_action = true;
4278 nab = (const struct nx_action_bundle *) ia;
4279 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4284 case OFPUTIL_NXAST_BUNDLE_LOAD:
4285 ctx->ofproto->has_bundle_action = true;
4286 nab = (const struct nx_action_bundle *) ia;
4287 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4291 case OFPUTIL_NXAST_OUTPUT_REG:
4292 naor = (const struct nx_action_output_reg *) ia;
4293 xlate_output_reg_action(ctx, naor);
4296 case OFPUTIL_NXAST_LEARN:
4297 ctx->has_learn = true;
4298 if (ctx->may_learn) {
4299 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4305 /* We've let OFPP_NORMAL and the learning action look at the packet,
4306 * so drop it now if forwarding is disabled. */
4307 if (port && !stp_forward_in_state(port->stp_state)) {
4308 ofpbuf_clear(ctx->odp_actions);
4309 add_sflow_action(ctx);
4314 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4315 struct ofproto_dpif *ofproto, const struct flow *flow,
4316 const struct ofpbuf *packet)
4318 ctx->ofproto = ofproto;
4320 ctx->packet = packet;
4321 ctx->may_learn = packet != NULL;
4322 ctx->resubmit_hook = NULL;
4325 static struct ofpbuf *
4326 xlate_actions(struct action_xlate_ctx *ctx,
4327 const union ofp_action *in, size_t n_in)
4329 COVERAGE_INC(ofproto_dpif_xlate);
4331 ctx->odp_actions = ofpbuf_new(512);
4332 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4334 ctx->may_set_up_flow = true;
4335 ctx->has_learn = false;
4336 ctx->has_normal = false;
4337 ctx->nf_output_iface = NF_OUT_DROP;
4340 ctx->base_priority = 0;
4341 ctx->base_flow = ctx->flow;
4342 ctx->base_flow.tun_id = 0;
4345 if (ctx->flow.tos_frag & FLOW_FRAG_ANY) {
4346 switch (ctx->ofproto->up.frag_handling) {
4347 case OFPC_FRAG_NORMAL:
4348 /* We must pretend that transport ports are unavailable. */
4349 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4350 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4353 case OFPC_FRAG_DROP:
4354 return ctx->odp_actions;
4356 case OFPC_FRAG_REASM:
4359 case OFPC_FRAG_NX_MATCH:
4360 /* Nothing to do. */
4365 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4366 ctx->may_set_up_flow = false;
4367 return ctx->odp_actions;
4369 add_sflow_action(ctx);
4370 do_xlate_actions(in, n_in, ctx);
4372 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4373 ctx->odp_actions->data,
4374 ctx->odp_actions->size)) {
4375 ctx->may_set_up_flow = false;
4377 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4379 compose_output_action(ctx, OVSP_LOCAL);
4382 fix_sflow_action(ctx);
4385 return ctx->odp_actions;
4388 /* OFPP_NORMAL implementation. */
4391 struct ofport_dpif *port;
4396 struct dst builtin[32];
4398 size_t n, allocated;
4401 static void dst_set_init(struct dst_set *);
4402 static void dst_set_add(struct dst_set *, const struct dst *);
4403 static void dst_set_free(struct dst_set *);
4405 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4407 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4408 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4409 * the bundle on which the packet was received, returns the VLAN to which the
4412 * Both 'vid' and the return value are in the range 0...4095. */
4414 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4416 switch (in_bundle->vlan_mode) {
4417 case PORT_VLAN_ACCESS:
4418 return in_bundle->vlan;
4421 case PORT_VLAN_TRUNK:
4424 case PORT_VLAN_NATIVE_UNTAGGED:
4425 case PORT_VLAN_NATIVE_TAGGED:
4426 return vid ? vid : in_bundle->vlan;
4433 /* Given 'vlan', the VLAN that a packet belongs to, and
4434 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4435 * that should be included in the 802.1Q header. (If the return value is 0,
4436 * then the 802.1Q header should only be included in the packet if there is a
4439 * Both 'vlan' and the return value are in the range 0...4095. */
4441 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4443 switch (out_bundle->vlan_mode) {
4444 case PORT_VLAN_ACCESS:
4447 case PORT_VLAN_TRUNK:
4448 case PORT_VLAN_NATIVE_TAGGED:
4451 case PORT_VLAN_NATIVE_UNTAGGED:
4452 return vlan == out_bundle->vlan ? 0 : vlan;
4460 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
4461 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
4465 vlan = input_vid_to_vlan(in_bundle, vlan_tci_to_vid(ctx->flow.vlan_tci));
4466 dst->vid = output_vlan_to_vid(out_bundle, vlan);
4468 dst->port = (!out_bundle->bond
4469 ? ofbundle_get_a_port(out_bundle)
4470 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4471 dst->vid, &ctx->tags));
4472 return dst->port != NULL;
4476 mirror_mask_ffs(mirror_mask_t mask)
4478 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4483 dst_set_init(struct dst_set *set)
4485 set->dsts = set->builtin;
4487 set->allocated = ARRAY_SIZE(set->builtin);
4491 dst_set_add(struct dst_set *set, const struct dst *dst)
4493 if (set->n >= set->allocated) {
4494 size_t new_allocated;
4495 struct dst *new_dsts;
4497 new_allocated = set->allocated * 2;
4498 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
4499 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
4503 set->dsts = new_dsts;
4504 set->allocated = new_allocated;
4506 set->dsts[set->n++] = *dst;
4510 dst_set_free(struct dst_set *set)
4512 if (set->dsts != set->builtin) {
4518 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
4521 for (i = 0; i < set->n; i++) {
4522 if (set->dsts[i].vid == test->vid
4523 && set->dsts[i].port == test->port) {
4531 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4533 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4534 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4538 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4540 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4543 /* Returns an arbitrary interface within 'bundle'. */
4544 static struct ofport_dpif *
4545 ofbundle_get_a_port(const struct ofbundle *bundle)
4547 return CONTAINER_OF(list_front(&bundle->ports),
4548 struct ofport_dpif, bundle_node);
4552 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4553 const struct ofbundle *in_bundle,
4554 const struct ofbundle *out_bundle, struct dst_set *set)
4558 if (out_bundle == OFBUNDLE_FLOOD) {
4559 struct ofbundle *bundle;
4561 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4562 if (bundle != in_bundle
4563 && ofbundle_includes_vlan(bundle, vlan)
4564 && bundle->floodable
4565 && !bundle->mirror_out
4566 && set_dst(ctx, &dst, in_bundle, bundle)) {
4567 dst_set_add(set, &dst);
4570 ctx->nf_output_iface = NF_OUT_FLOOD;
4571 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
4572 dst_set_add(set, &dst);
4573 ctx->nf_output_iface = dst.port->odp_port;
4578 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4580 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4583 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4584 * to a VLAN. In general most packets may be mirrored but we want to drop
4585 * protocols that may confuse switches. */
4587 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4589 /* If you change this function's behavior, please update corresponding
4590 * documentation in vswitch.xml at the same time. */
4591 if (dst[0] != 0x01) {
4592 /* All the currently banned MACs happen to start with 01 currently, so
4593 * this is a quick way to eliminate most of the good ones. */
4595 if (eth_addr_is_reserved(dst)) {
4596 /* Drop STP, IEEE pause frames, and other reserved protocols
4597 * (01-80-c2-00-00-0x). */
4601 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4603 if ((dst[3] & 0xfe) == 0xcc &&
4604 (dst[4] & 0xfe) == 0xcc &&
4605 (dst[5] & 0xfe) == 0xcc) {
4606 /* Drop the following protocols plus others following the same
4609 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4610 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4611 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4615 if (!(dst[3] | dst[4] | dst[5])) {
4616 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4625 compose_mirror_dsts(struct action_xlate_ctx *ctx,
4626 uint16_t vlan, const struct ofbundle *in_bundle,
4627 struct dst_set *set)
4629 struct ofproto_dpif *ofproto = ctx->ofproto;
4630 mirror_mask_t mirrors;
4634 mirrors = in_bundle->src_mirrors;
4635 for (i = 0; i < set->n; i++) {
4636 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
4643 flow_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4645 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4646 if (vlan_is_mirrored(m, vlan)) {
4650 if (set_dst(ctx, &dst, in_bundle, m->out)
4651 && !dst_is_duplicate(set, &dst)) {
4652 dst_set_add(set, &dst);
4654 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
4655 struct ofbundle *bundle;
4657 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4658 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4659 && set_dst(ctx, &dst, in_bundle, bundle))
4661 /* set_dst() got dst->vid from the input packet's VLAN,
4662 * not from m->out_vlan, so recompute it. */
4663 dst.vid = output_vlan_to_vid(bundle, m->out_vlan);
4665 if (dst_is_duplicate(set, &dst)) {
4669 if (bundle == in_bundle && dst.vid == flow_vid) {
4670 /* Don't send out input port on same VLAN. */
4673 dst_set_add(set, &dst);
4678 mirrors &= mirrors - 1;
4683 compose_dst_output_action(struct action_xlate_ctx *ctx, const struct dst *dst)
4687 tci = htons(dst->vid);
4689 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4690 tci |= htons(VLAN_CFI);
4692 commit_vlan_action(ctx, tci);
4694 compose_output_action(ctx, dst->port->odp_port);
4698 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
4699 const struct ofbundle *in_bundle,
4700 const struct ofbundle *out_bundle)
4702 uint16_t initial_vid;
4703 const struct dst *dst;
4707 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
4708 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
4714 /* Output all the packets we can without having to change the VLAN. */
4715 commit_odp_actions(ctx);
4716 initial_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4717 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4718 if (dst->vid == initial_vid) {
4719 compose_dst_output_action(ctx, dst);
4723 /* Then output the rest. */
4724 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4725 if (dst->vid != initial_vid) {
4726 compose_dst_output_action(ctx, dst);
4733 /* Returns the effective vlan of a packet, taking into account both the
4734 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
4735 * the packet is untagged and -1 indicates it has an invalid header and
4736 * should be dropped. */
4738 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
4739 struct ofbundle *in_bundle, bool have_packet)
4741 int vlan = vlan_tci_to_vid(flow->vlan_tci);
4743 if (in_bundle->vlan_mode == PORT_VLAN_ACCESS) {
4744 /* Drop tagged packet on access port */
4746 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4747 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4748 "packet received on port %s configured with "
4749 "implicit VLAN %"PRIu16,
4750 ofproto->up.name, vlan,
4751 in_bundle->name, in_bundle->vlan);
4754 } else if (ofbundle_includes_vlan(in_bundle, vlan)) {
4757 /* Drop packets from a VLAN not member of the trunk */
4759 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4760 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4761 "packet received on port %s not configured for "
4763 ofproto->up.name, vlan, in_bundle->name, vlan);
4768 if (in_bundle->vlan_mode != PORT_VLAN_TRUNK) {
4769 return in_bundle->vlan;
4771 return ofbundle_includes_vlan(in_bundle, 0) ? 0 : -1;
4776 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4777 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4778 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4780 is_gratuitous_arp(const struct flow *flow)
4782 return (flow->dl_type == htons(ETH_TYPE_ARP)
4783 && eth_addr_is_broadcast(flow->dl_dst)
4784 && (flow->nw_proto == ARP_OP_REPLY
4785 || (flow->nw_proto == ARP_OP_REQUEST
4786 && flow->nw_src == flow->nw_dst)));
4790 update_learning_table(struct ofproto_dpif *ofproto,
4791 const struct flow *flow, int vlan,
4792 struct ofbundle *in_bundle)
4794 struct mac_entry *mac;
4796 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4800 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4801 if (is_gratuitous_arp(flow)) {
4802 /* We don't want to learn from gratuitous ARP packets that are
4803 * reflected back over bond slaves so we lock the learning table. */
4804 if (!in_bundle->bond) {
4805 mac_entry_set_grat_arp_lock(mac);
4806 } else if (mac_entry_is_grat_arp_locked(mac)) {
4811 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4812 /* The log messages here could actually be useful in debugging,
4813 * so keep the rate limit relatively high. */
4814 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4815 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4816 "on port %s in VLAN %d",
4817 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4818 in_bundle->name, vlan);
4820 mac->port.p = in_bundle;
4821 tag_set_add(&ofproto->revalidate_set,
4822 mac_learning_changed(ofproto->ml, mac));
4826 /* Determines whether packets in 'flow' within 'br' should be forwarded or
4827 * dropped. Returns true if they may be forwarded, false if they should be
4830 * If 'have_packet' is true, it indicates that the caller is processing a
4831 * received packet. If 'have_packet' is false, then the caller is just
4832 * revalidating an existing flow because configuration has changed. Either
4833 * way, 'have_packet' only affects logging (there is no point in logging errors
4834 * during revalidation).
4836 * Sets '*in_portp' to the input port. This will be a null pointer if
4837 * flow->in_port does not designate a known input port (in which case
4838 * is_admissible() returns false).
4840 * When returning true, sets '*vlanp' to the effective VLAN of the input
4841 * packet, as returned by flow_get_vlan().
4843 * May also add tags to '*tags', although the current implementation only does
4844 * so in one special case.
4847 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
4849 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
4851 struct ofport_dpif *in_port;
4852 struct ofbundle *in_bundle;
4855 /* Find the port and bundle for the received packet. */
4856 in_port = get_ofp_port(ofproto, flow->in_port);
4857 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
4858 if (!in_port || !in_bundle) {
4859 /* No interface? Something fishy... */
4861 /* Odd. A few possible reasons here:
4863 * - We deleted a port but there are still a few packets queued up
4866 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4867 * we don't know about.
4869 * - Packet arrived on the local port but the local port is not
4872 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4874 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4876 ofproto->up.name, flow->in_port);
4881 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
4886 /* Drop frames for reserved multicast addresses only if forward_bpdu
4887 * option is absent. */
4888 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
4892 /* Drop frames on bundles reserved for mirroring. */
4893 if (in_bundle->mirror_out) {
4895 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4896 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4897 "%s, which is reserved exclusively for mirroring",
4898 ofproto->up.name, in_bundle->name);
4903 if (in_bundle->bond) {
4904 struct mac_entry *mac;
4906 switch (bond_check_admissibility(in_bundle->bond, in_port,
4907 flow->dl_dst, tags)) {
4914 case BV_DROP_IF_MOVED:
4915 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
4916 if (mac && mac->port.p != in_bundle &&
4917 (!is_gratuitous_arp(flow)
4918 || mac_entry_is_grat_arp_locked(mac))) {
4929 xlate_normal(struct action_xlate_ctx *ctx)
4931 struct ofbundle *in_bundle;
4932 struct ofbundle *out_bundle;
4933 struct mac_entry *mac;
4936 ctx->has_normal = true;
4938 /* Check whether we should drop packets in this flow. */
4939 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
4940 &ctx->tags, &vlan, &in_bundle)) {
4945 /* Learn source MAC. */
4946 if (ctx->may_learn) {
4947 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
4950 /* Determine output bundle. */
4951 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
4954 out_bundle = mac->port.p;
4955 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4956 /* If we are revalidating but don't have a learning entry then eject
4957 * the flow. Installing a flow that floods packets opens up a window
4958 * of time where we could learn from a packet reflected on a bond and
4959 * blackhole packets before the learning table is updated to reflect
4960 * the correct port. */
4961 ctx->may_set_up_flow = false;
4964 out_bundle = OFBUNDLE_FLOOD;
4967 /* Don't send packets out their input bundles. */
4968 if (in_bundle == out_bundle) {
4974 compose_actions(ctx, vlan, in_bundle, out_bundle);
4978 /* Optimized flow revalidation.
4980 * It's a difficult problem, in general, to tell which facets need to have
4981 * their actions recalculated whenever the OpenFlow flow table changes. We
4982 * don't try to solve that general problem: for most kinds of OpenFlow flow
4983 * table changes, we recalculate the actions for every facet. This is
4984 * relatively expensive, but it's good enough if the OpenFlow flow table
4985 * doesn't change very often.
4987 * However, we can expect one particular kind of OpenFlow flow table change to
4988 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4989 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4990 * table, we add a special case that applies to flow tables in which every rule
4991 * has the same form (that is, the same wildcards), except that the table is
4992 * also allowed to have a single "catch-all" flow that matches all packets. We
4993 * optimize this case by tagging all of the facets that resubmit into the table
4994 * and invalidating the same tag whenever a flow changes in that table. The
4995 * end result is that we revalidate just the facets that need it (and sometimes
4996 * a few more, but not all of the facets or even all of the facets that
4997 * resubmit to the table modified by MAC learning). */
4999 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5000 * into an OpenFlow table with the given 'basis'. */
5002 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5005 if (flow_wildcards_is_catchall(wc)) {
5008 struct flow tag_flow = *flow;
5009 flow_zero_wildcards(&tag_flow, wc);
5010 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5014 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5015 * taggability of that table.
5017 * This function must be called after *each* change to a flow table. If you
5018 * skip calling it on some changes then the pointer comparisons at the end can
5019 * be invalid if you get unlucky. For example, if a flow removal causes a
5020 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5021 * different wildcards to be created with the same address, then this function
5022 * will incorrectly skip revalidation. */
5024 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5026 struct table_dpif *table = &ofproto->tables[table_id];
5027 const struct classifier *cls = &ofproto->up.tables[table_id];
5028 struct cls_table *catchall, *other;
5029 struct cls_table *t;
5031 catchall = other = NULL;
5033 switch (hmap_count(&cls->tables)) {
5035 /* We could tag this OpenFlow table but it would make the logic a
5036 * little harder and it's a corner case that doesn't seem worth it
5042 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5043 if (cls_table_is_catchall(t)) {
5045 } else if (!other) {
5048 /* Indicate that we can't tag this by setting both tables to
5049 * NULL. (We know that 'catchall' is already NULL.) */
5056 /* Can't tag this table. */
5060 if (table->catchall_table != catchall || table->other_table != other) {
5061 table->catchall_table = catchall;
5062 table->other_table = other;
5063 ofproto->need_revalidate = true;
5067 /* Given 'rule' that has changed in some way (either it is a rule being
5068 * inserted, a rule being deleted, or a rule whose actions are being
5069 * modified), marks facets for revalidation to ensure that packets will be
5070 * forwarded correctly according to the new state of the flow table.
5072 * This function must be called after *each* change to a flow table. See
5073 * the comment on table_update_taggable() for more information. */
5075 rule_invalidate(const struct rule_dpif *rule)
5077 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5079 table_update_taggable(ofproto, rule->up.table_id);
5081 if (!ofproto->need_revalidate) {
5082 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5084 if (table->other_table && rule->tag) {
5085 tag_set_add(&ofproto->revalidate_set, rule->tag);
5087 ofproto->need_revalidate = true;
5093 set_frag_handling(struct ofproto *ofproto_,
5094 enum ofp_config_flags frag_handling)
5096 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5098 if (frag_handling != OFPC_FRAG_REASM) {
5099 ofproto->need_revalidate = true;
5107 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5108 const struct flow *flow,
5109 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5111 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5114 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5115 ofproto->max_ports);
5117 struct odputil_keybuf keybuf;
5118 struct action_xlate_ctx ctx;
5119 struct ofpbuf *odp_actions;
5122 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5123 odp_flow_key_from_flow(&key, flow);
5125 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
5126 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5127 dpif_execute(ofproto->dpif, key.data, key.size,
5128 odp_actions->data, odp_actions->size, packet);
5129 ofpbuf_delete(odp_actions);
5135 get_netflow_ids(const struct ofproto *ofproto_,
5136 uint8_t *engine_type, uint8_t *engine_id)
5138 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5140 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5143 static struct ofproto_dpif *
5144 ofproto_dpif_lookup(const char *name)
5146 struct ofproto *ofproto = ofproto_lookup(name);
5147 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5148 ? ofproto_dpif_cast(ofproto)
5153 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5154 const char *args, void *aux OVS_UNUSED)
5156 struct ds ds = DS_EMPTY_INITIALIZER;
5157 const struct ofproto_dpif *ofproto;
5158 const struct mac_entry *e;
5160 ofproto = ofproto_dpif_lookup(args);
5162 unixctl_command_reply(conn, 501, "no such bridge");
5166 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5167 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5168 struct ofbundle *bundle = e->port.p;
5169 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5170 ofbundle_get_a_port(bundle)->odp_port,
5171 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5173 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5177 struct ofproto_trace {
5178 struct action_xlate_ctx ctx;
5184 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5185 const struct rule_dpif *rule)
5187 ds_put_char_multiple(result, '\t', level);
5189 ds_put_cstr(result, "No match\n");
5193 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5194 table_id, ntohll(rule->up.flow_cookie));
5195 cls_rule_format(&rule->up.cr, result);
5196 ds_put_char(result, '\n');
5198 ds_put_char_multiple(result, '\t', level);
5199 ds_put_cstr(result, "OpenFlow ");
5200 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5201 ds_put_char(result, '\n');
5205 trace_format_flow(struct ds *result, int level, const char *title,
5206 struct ofproto_trace *trace)
5208 ds_put_char_multiple(result, '\t', level);
5209 ds_put_format(result, "%s: ", title);
5210 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5211 ds_put_cstr(result, "unchanged");
5213 flow_format(result, &trace->ctx.flow);
5214 trace->flow = trace->ctx.flow;
5216 ds_put_char(result, '\n');
5220 trace_format_regs(struct ds *result, int level, const char *title,
5221 struct ofproto_trace *trace)
5225 ds_put_char_multiple(result, '\t', level);
5226 ds_put_format(result, "%s:", title);
5227 for (i = 0; i < FLOW_N_REGS; i++) {
5228 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5230 ds_put_char(result, '\n');
5234 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5236 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5237 struct ds *result = trace->result;
5239 ds_put_char(result, '\n');
5240 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5241 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5242 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5246 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5247 void *aux OVS_UNUSED)
5249 char *dpname, *arg1, *arg2, *arg3;
5250 char *args = xstrdup(args_);
5251 char *save_ptr = NULL;
5252 struct ofproto_dpif *ofproto;
5253 struct ofpbuf odp_key;
5254 struct ofpbuf *packet;
5255 struct rule_dpif *rule;
5261 ofpbuf_init(&odp_key, 0);
5264 dpname = strtok_r(args, " ", &save_ptr);
5265 arg1 = strtok_r(NULL, " ", &save_ptr);
5266 arg2 = strtok_r(NULL, " ", &save_ptr);
5267 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5268 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5269 /* ofproto/trace dpname flow [-generate] */
5272 /* Convert string to datapath key. */
5273 ofpbuf_init(&odp_key, 0);
5274 error = odp_flow_key_from_string(arg1, &odp_key);
5276 unixctl_command_reply(conn, 501, "Bad flow syntax");
5280 /* Convert odp_key to flow. */
5281 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
5283 unixctl_command_reply(conn, 501, "Invalid flow");
5287 /* Generate a packet, if requested. */
5289 packet = ofpbuf_new(0);
5290 flow_compose(packet, &flow);
5292 } else if (dpname && arg1 && arg2 && arg3) {
5293 /* ofproto/trace dpname tun_id in_port packet */
5297 tun_id = htonll(strtoull(arg1, NULL, 0));
5298 in_port = ofp_port_to_odp_port(atoi(arg2));
5300 packet = ofpbuf_new(strlen(args) / 2);
5301 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
5302 arg3 += strspn(arg3, " ");
5303 if (*arg3 != '\0') {
5304 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5307 if (packet->size < ETH_HEADER_LEN) {
5308 unixctl_command_reply(conn, 501,
5309 "Packet data too short for Ethernet");
5313 ds_put_cstr(&result, "Packet: ");
5314 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5315 ds_put_cstr(&result, s);
5318 flow_extract(packet, tun_id, in_port, &flow);
5320 unixctl_command_reply(conn, 501, "Bad command syntax");
5324 ofproto = ofproto_dpif_lookup(dpname);
5326 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5331 ds_put_cstr(&result, "Flow: ");
5332 flow_format(&result, &flow);
5333 ds_put_char(&result, '\n');
5335 rule = rule_dpif_lookup(ofproto, &flow, 0);
5336 trace_format_rule(&result, 0, 0, rule);
5338 struct ofproto_trace trace;
5339 struct ofpbuf *odp_actions;
5341 trace.result = &result;
5343 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5344 trace.ctx.resubmit_hook = trace_resubmit;
5345 odp_actions = xlate_actions(&trace.ctx,
5346 rule->up.actions, rule->up.n_actions);
5348 ds_put_char(&result, '\n');
5349 trace_format_flow(&result, 0, "Final flow", &trace);
5350 ds_put_cstr(&result, "Datapath actions: ");
5351 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5352 ofpbuf_delete(odp_actions);
5354 if (!trace.ctx.may_set_up_flow) {
5356 ds_put_cstr(&result, "\nThis flow is not cachable.");
5358 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5359 "for complete actions, please supply a packet.");
5364 unixctl_command_reply(conn, 200, ds_cstr(&result));
5367 ds_destroy(&result);
5368 ofpbuf_delete(packet);
5369 ofpbuf_uninit(&odp_key);
5374 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5375 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5378 unixctl_command_reply(conn, 200, NULL);
5382 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5383 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5386 unixctl_command_reply(conn, 200, NULL);
5390 ofproto_dpif_unixctl_init(void)
5392 static bool registered;
5398 unixctl_command_register("ofproto/trace",
5399 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5400 ofproto_unixctl_trace, NULL);
5401 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5403 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5404 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5407 const struct ofproto_class ofproto_dpif_class = {
5435 port_is_lacp_current,
5436 NULL, /* rule_choose_table */
5443 rule_modify_actions,
5451 get_cfm_remote_mpids,
5455 get_stp_port_status,
5460 is_mirror_output_bundle,
5461 forward_bpdu_changed,