2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-provider.h"
26 #include "byte-order.h"
31 #include "dynamic-string.h"
32 #include "fail-open.h"
36 #include "mac-learning.h"
37 #include "multipath.h"
44 #include "ofp-print.h"
45 #include "ofproto-dpif-sflow.h"
46 #include "poll-loop.h"
48 #include "unaligned.h"
50 #include "vlan-bitmap.h"
53 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
55 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
56 COVERAGE_DEFINE(ofproto_dpif_expired);
57 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
58 COVERAGE_DEFINE(ofproto_dpif_xlate);
59 COVERAGE_DEFINE(facet_changed_rule);
60 COVERAGE_DEFINE(facet_invalidated);
61 COVERAGE_DEFINE(facet_revalidate);
62 COVERAGE_DEFINE(facet_unexpected);
64 /* Maximum depth of flow table recursion (due to resubmit actions) in a
65 * flow translation. */
66 #define MAX_RESUBMIT_RECURSION 32
68 /* Number of implemented OpenFlow tables. */
69 enum { N_TABLES = 255 };
70 BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255);
78 long long int used; /* Time last used; time created if not used. */
82 * - Do include packets and bytes from facets that have been deleted or
83 * whose own statistics have been folded into the rule.
85 * - Do include packets and bytes sent "by hand" that were accounted to
86 * the rule without any facet being involved (this is a rare corner
87 * case in rule_execute()).
89 * - Do not include packet or bytes that can be obtained from any facet's
90 * packet_count or byte_count member or that can be obtained from the
91 * datapath by, e.g., dpif_flow_get() for any facet.
93 uint64_t packet_count; /* Number of packets received. */
94 uint64_t byte_count; /* Number of bytes received. */
96 tag_type tag; /* Caches rule_calculate_tag() result. */
98 struct list facets; /* List of "struct facet"s. */
101 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
103 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
106 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
107 const struct flow *, uint8_t table);
109 #define MAX_MIRRORS 32
110 typedef uint32_t mirror_mask_t;
111 #define MIRROR_MASK_C(X) UINT32_C(X)
112 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
114 struct ofproto_dpif *ofproto; /* Owning ofproto. */
115 size_t idx; /* In ofproto's "mirrors" array. */
116 void *aux; /* Key supplied by ofproto's client. */
117 char *name; /* Identifier for log messages. */
119 /* Selection criteria. */
120 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
121 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
122 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
124 /* Output (mutually exclusive). */
125 struct ofbundle *out; /* Output port or NULL. */
126 int out_vlan; /* Output VLAN or -1. */
129 static void mirror_destroy(struct ofmirror *);
131 /* A group of one or more OpenFlow ports. */
132 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
134 struct ofproto_dpif *ofproto; /* Owning ofproto. */
135 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
136 void *aux; /* Key supplied by ofproto's client. */
137 char *name; /* Identifier for log messages. */
140 struct list ports; /* Contains "struct ofport"s. */
141 enum port_vlan_mode vlan_mode; /* VLAN mode */
142 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
143 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
144 * NULL if all VLANs are trunked. */
145 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
146 struct bond *bond; /* Nonnull iff more than one port. */
149 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
151 /* Port mirroring info. */
152 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
153 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
154 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
157 static void bundle_remove(struct ofport *);
158 static void bundle_update(struct ofbundle *);
159 static void bundle_destroy(struct ofbundle *);
160 static void bundle_del_port(struct ofport_dpif *);
161 static void bundle_run(struct ofbundle *);
162 static void bundle_wait(struct ofbundle *);
164 static void stp_run(struct ofproto_dpif *ofproto);
165 static void stp_wait(struct ofproto_dpif *ofproto);
167 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
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 struct flow base_flow; /* Flow at the last commit. */
211 uint32_t original_priority; /* Priority when packet arrived. */
212 uint8_t table_id; /* OpenFlow table ID where flow was found. */
213 uint32_t sflow_n_outputs; /* Number of output ports. */
214 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
215 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
216 bool exit; /* No further actions should be processed. */
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);
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 void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
421 static void handle_miss_upcalls(struct ofproto_dpif *,
422 struct dpif_upcall *, size_t n);
424 /* Flow expiration. */
425 static int expire(struct ofproto_dpif *);
428 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
429 const struct ofpbuf *packet);
431 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
432 const struct flow *, uint32_t odp_port);
433 /* Global variables. */
434 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
436 /* Factory functions. */
439 enumerate_types(struct sset *types)
441 dp_enumerate_types(types);
445 enumerate_names(const char *type, struct sset *names)
447 return dp_enumerate_names(type, names);
451 del(const char *type, const char *name)
456 error = dpif_open(name, type, &dpif);
458 error = dpif_delete(dpif);
464 /* Basic life-cycle. */
466 static struct ofproto *
469 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
474 dealloc(struct ofproto *ofproto_)
476 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
481 construct(struct ofproto *ofproto_, int *n_tablesp)
483 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
484 const char *name = ofproto->up.name;
488 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
490 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
494 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
495 ofproto->n_matches = 0;
497 dpif_flow_flush(ofproto->dpif);
498 dpif_recv_purge(ofproto->dpif);
500 error = dpif_recv_set_mask(ofproto->dpif,
501 ((1u << DPIF_UC_MISS) |
502 (1u << DPIF_UC_ACTION)));
504 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
505 dpif_close(ofproto->dpif);
509 ofproto->netflow = NULL;
510 ofproto->sflow = NULL;
512 hmap_init(&ofproto->bundles);
513 ofproto->ml = mac_learning_create();
514 for (i = 0; i < MAX_MIRRORS; i++) {
515 ofproto->mirrors[i] = NULL;
517 ofproto->has_bonded_bundles = false;
519 timer_set_duration(&ofproto->next_expiration, 1000);
521 hmap_init(&ofproto->facets);
523 for (i = 0; i < N_TABLES; i++) {
524 struct table_dpif *table = &ofproto->tables[i];
526 table->catchall_table = NULL;
527 table->other_table = NULL;
528 table->basis = random_uint32();
530 ofproto->need_revalidate = false;
531 tag_set_init(&ofproto->revalidate_set);
533 list_init(&ofproto->completions);
535 ofproto_dpif_unixctl_init();
537 ofproto->has_bundle_action = false;
539 *n_tablesp = N_TABLES;
544 complete_operations(struct ofproto_dpif *ofproto)
546 struct dpif_completion *c, *next;
548 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
549 ofoperation_complete(c->op, 0);
550 list_remove(&c->list_node);
556 destruct(struct ofproto *ofproto_)
558 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
559 struct rule_dpif *rule, *next_rule;
560 struct classifier *table;
563 complete_operations(ofproto);
565 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
566 struct cls_cursor cursor;
568 cls_cursor_init(&cursor, table, NULL);
569 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
570 ofproto_rule_destroy(&rule->up);
574 for (i = 0; i < MAX_MIRRORS; i++) {
575 mirror_destroy(ofproto->mirrors[i]);
578 netflow_destroy(ofproto->netflow);
579 dpif_sflow_destroy(ofproto->sflow);
580 hmap_destroy(&ofproto->bundles);
581 mac_learning_destroy(ofproto->ml);
583 hmap_destroy(&ofproto->facets);
585 dpif_close(ofproto->dpif);
589 run(struct ofproto *ofproto_)
591 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
592 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
593 struct ofport_dpif *ofport;
594 struct ofbundle *bundle;
599 complete_operations(ofproto);
601 dpif_run(ofproto->dpif);
604 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
605 struct dpif_upcall *upcall = &misses[n_misses];
608 error = dpif_recv(ofproto->dpif, upcall);
610 if (error == ENODEV && n_misses == 0) {
616 if (upcall->type == DPIF_UC_MISS) {
617 /* Handle it later. */
620 handle_upcall(ofproto, upcall);
624 handle_miss_upcalls(ofproto, misses, n_misses);
626 if (timer_expired(&ofproto->next_expiration)) {
627 int delay = expire(ofproto);
628 timer_set_duration(&ofproto->next_expiration, delay);
631 if (ofproto->netflow) {
632 netflow_run(ofproto->netflow);
634 if (ofproto->sflow) {
635 dpif_sflow_run(ofproto->sflow);
638 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
641 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
646 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
648 /* Now revalidate if there's anything to do. */
649 if (ofproto->need_revalidate
650 || !tag_set_is_empty(&ofproto->revalidate_set)) {
651 struct tag_set revalidate_set = ofproto->revalidate_set;
652 bool revalidate_all = ofproto->need_revalidate;
653 struct facet *facet, *next;
655 /* Clear the revalidation flags. */
656 tag_set_init(&ofproto->revalidate_set);
657 ofproto->need_revalidate = false;
659 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
661 || tag_set_intersects(&revalidate_set, facet->tags)) {
662 facet_revalidate(ofproto, facet);
671 wait(struct ofproto *ofproto_)
673 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
674 struct ofport_dpif *ofport;
675 struct ofbundle *bundle;
677 if (!clogged && !list_is_empty(&ofproto->completions)) {
678 poll_immediate_wake();
681 dpif_wait(ofproto->dpif);
682 dpif_recv_wait(ofproto->dpif);
683 if (ofproto->sflow) {
684 dpif_sflow_wait(ofproto->sflow);
686 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
687 poll_immediate_wake();
689 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
692 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
695 mac_learning_wait(ofproto->ml);
697 if (ofproto->need_revalidate) {
698 /* Shouldn't happen, but if it does just go around again. */
699 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
700 poll_immediate_wake();
702 timer_wait(&ofproto->next_expiration);
707 flush(struct ofproto *ofproto_)
709 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
710 struct facet *facet, *next_facet;
712 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
713 /* Mark the facet as not installed so that facet_remove() doesn't
714 * bother trying to uninstall it. There is no point in uninstalling it
715 * individually since we are about to blow away all the facets with
716 * dpif_flow_flush(). */
717 facet->installed = false;
718 facet->dp_packet_count = 0;
719 facet->dp_byte_count = 0;
720 facet_remove(ofproto, facet);
722 dpif_flow_flush(ofproto->dpif);
726 get_features(struct ofproto *ofproto_ OVS_UNUSED,
727 bool *arp_match_ip, uint32_t *actions)
729 *arp_match_ip = true;
730 *actions = ((1u << OFPAT_OUTPUT) |
731 (1u << OFPAT_SET_VLAN_VID) |
732 (1u << OFPAT_SET_VLAN_PCP) |
733 (1u << OFPAT_STRIP_VLAN) |
734 (1u << OFPAT_SET_DL_SRC) |
735 (1u << OFPAT_SET_DL_DST) |
736 (1u << OFPAT_SET_NW_SRC) |
737 (1u << OFPAT_SET_NW_DST) |
738 (1u << OFPAT_SET_NW_TOS) |
739 (1u << OFPAT_SET_TP_SRC) |
740 (1u << OFPAT_SET_TP_DST) |
741 (1u << OFPAT_ENQUEUE));
745 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
747 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
748 struct dpif_dp_stats s;
750 strcpy(ots->name, "classifier");
752 dpif_get_dp_stats(ofproto->dpif, &s);
753 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
754 put_32aligned_be64(&ots->matched_count,
755 htonll(s.n_hit + ofproto->n_matches));
759 set_netflow(struct ofproto *ofproto_,
760 const struct netflow_options *netflow_options)
762 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
764 if (netflow_options) {
765 if (!ofproto->netflow) {
766 ofproto->netflow = netflow_create();
768 return netflow_set_options(ofproto->netflow, netflow_options);
770 netflow_destroy(ofproto->netflow);
771 ofproto->netflow = NULL;
776 static struct ofport *
779 struct ofport_dpif *port = xmalloc(sizeof *port);
784 port_dealloc(struct ofport *port_)
786 struct ofport_dpif *port = ofport_dpif_cast(port_);
791 port_construct(struct ofport *port_)
793 struct ofport_dpif *port = ofport_dpif_cast(port_);
794 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
796 ofproto->need_revalidate = true;
797 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
800 port->tag = tag_create_random();
801 port->may_enable = true;
802 port->stp_port = NULL;
803 port->stp_state = STP_DISABLED;
805 if (ofproto->sflow) {
806 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
807 netdev_get_name(port->up.netdev));
814 port_destruct(struct ofport *port_)
816 struct ofport_dpif *port = ofport_dpif_cast(port_);
817 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
819 ofproto->need_revalidate = true;
820 bundle_remove(port_);
821 set_cfm(port_, NULL);
822 if (ofproto->sflow) {
823 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
828 port_modified(struct ofport *port_)
830 struct ofport_dpif *port = ofport_dpif_cast(port_);
832 if (port->bundle && port->bundle->bond) {
833 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
838 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
840 struct ofport_dpif *port = ofport_dpif_cast(port_);
841 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
842 ovs_be32 changed = old_config ^ port->up.opp.config;
844 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
845 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
846 ofproto->need_revalidate = true;
848 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
849 bundle_update(port->bundle);
855 set_sflow(struct ofproto *ofproto_,
856 const struct ofproto_sflow_options *sflow_options)
858 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
859 struct dpif_sflow *ds = ofproto->sflow;
863 struct ofport_dpif *ofport;
865 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
866 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
867 dpif_sflow_add_port(ds, ofport->odp_port,
868 netdev_get_name(ofport->up.netdev));
870 ofproto->need_revalidate = true;
872 dpif_sflow_set_options(ds, sflow_options);
875 dpif_sflow_destroy(ds);
876 ofproto->need_revalidate = true;
877 ofproto->sflow = NULL;
884 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
886 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
893 struct ofproto_dpif *ofproto;
895 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
896 ofproto->need_revalidate = true;
897 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
900 if (cfm_configure(ofport->cfm, s)) {
906 cfm_destroy(ofport->cfm);
912 get_cfm_fault(const struct ofport *ofport_)
914 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
916 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
920 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
923 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
926 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
936 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
938 struct ofproto_dpif *ofproto = ofproto_;
939 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
940 struct ofport_dpif *ofport;
942 ofport = stp_port_get_aux(sp);
944 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
945 ofproto->up.name, port_num);
947 struct eth_header *eth = pkt->l2;
949 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
950 if (eth_addr_is_zero(eth->eth_src)) {
951 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
952 "with unknown MAC", ofproto->up.name, port_num);
954 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
955 ofport->odp_port, pkt);
961 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
963 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
965 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
967 /* Only revalidate flows if the configuration changed. */
968 if (!s != !ofproto->stp) {
969 ofproto->need_revalidate = true;
974 ofproto->stp = stp_create(ofproto_->name, s->system_id,
975 send_bpdu_cb, ofproto);
976 ofproto->stp_last_tick = time_msec();
979 stp_set_bridge_id(ofproto->stp, s->system_id);
980 stp_set_bridge_priority(ofproto->stp, s->priority);
981 stp_set_hello_time(ofproto->stp, s->hello_time);
982 stp_set_max_age(ofproto->stp, s->max_age);
983 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
985 stp_destroy(ofproto->stp);
993 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
995 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
999 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1000 s->designated_root = stp_get_designated_root(ofproto->stp);
1001 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1010 update_stp_port_state(struct ofport_dpif *ofport)
1012 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1013 enum stp_state state;
1015 /* Figure out new state. */
1016 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1020 if (ofport->stp_state != state) {
1024 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1025 netdev_get_name(ofport->up.netdev),
1026 stp_state_name(ofport->stp_state),
1027 stp_state_name(state));
1028 if (stp_learn_in_state(ofport->stp_state)
1029 != stp_learn_in_state(state)) {
1030 /* xxx Learning action flows should also be flushed. */
1031 mac_learning_flush(ofproto->ml);
1033 fwd_change = stp_forward_in_state(ofport->stp_state)
1034 != stp_forward_in_state(state);
1036 ofproto->need_revalidate = true;
1037 ofport->stp_state = state;
1038 ofport->stp_state_entered = time_msec();
1040 if (fwd_change && ofport->bundle) {
1041 bundle_update(ofport->bundle);
1044 /* Update the STP state bits in the OpenFlow port description. */
1045 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1046 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1047 : state == STP_LEARNING ? OFPPS_STP_LEARN
1048 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1049 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1051 ofproto_port_set_state(&ofport->up, of_state);
1055 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1056 * caller is responsible for assigning STP port numbers and ensuring
1057 * there are no duplicates. */
1059 set_stp_port(struct ofport *ofport_,
1060 const struct ofproto_port_stp_settings *s)
1062 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1063 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1064 struct stp_port *sp = ofport->stp_port;
1066 if (!s || !s->enable) {
1068 ofport->stp_port = NULL;
1069 stp_port_disable(sp);
1070 update_stp_port_state(ofport);
1073 } else if (sp && stp_port_no(sp) != s->port_num
1074 && ofport == stp_port_get_aux(sp)) {
1075 /* The port-id changed, so disable the old one if it's not
1076 * already in use by another port. */
1077 stp_port_disable(sp);
1080 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1081 stp_port_enable(sp);
1083 stp_port_set_aux(sp, ofport);
1084 stp_port_set_priority(sp, s->priority);
1085 stp_port_set_path_cost(sp, s->path_cost);
1087 update_stp_port_state(ofport);
1093 get_stp_port_status(struct ofport *ofport_,
1094 struct ofproto_port_stp_status *s)
1096 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1097 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1098 struct stp_port *sp = ofport->stp_port;
1100 if (!ofproto->stp || !sp) {
1106 s->port_id = stp_port_get_id(sp);
1107 s->state = stp_port_get_state(sp);
1108 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1109 s->role = stp_port_get_role(sp);
1110 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1116 stp_run(struct ofproto_dpif *ofproto)
1119 long long int now = time_msec();
1120 long long int elapsed = now - ofproto->stp_last_tick;
1121 struct stp_port *sp;
1124 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1125 ofproto->stp_last_tick = now;
1127 while (stp_get_changed_port(ofproto->stp, &sp)) {
1128 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1131 update_stp_port_state(ofport);
1138 stp_wait(struct ofproto_dpif *ofproto)
1141 poll_timer_wait(1000);
1145 /* Returns true if STP should process 'flow'. */
1147 stp_should_process_flow(const struct flow *flow)
1149 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1153 stp_process_packet(const struct ofport_dpif *ofport,
1154 const struct ofpbuf *packet)
1156 struct ofpbuf payload = *packet;
1157 struct eth_header *eth = payload.data;
1158 struct stp_port *sp = ofport->stp_port;
1160 /* Sink packets on ports that have STP disabled when the bridge has
1162 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1166 /* Trim off padding on payload. */
1167 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1168 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1171 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1172 stp_received_bpdu(sp, payload.data, payload.size);
1178 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1179 * to revalidate every flow. */
1181 bundle_flush_macs(struct ofbundle *bundle)
1183 struct ofproto_dpif *ofproto = bundle->ofproto;
1184 struct mac_learning *ml = ofproto->ml;
1185 struct mac_entry *mac, *next_mac;
1187 ofproto->need_revalidate = true;
1188 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1189 if (mac->port.p == bundle) {
1190 mac_learning_expire(ml, mac);
1195 static struct ofbundle *
1196 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1198 struct ofbundle *bundle;
1200 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1201 &ofproto->bundles) {
1202 if (bundle->aux == aux) {
1209 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1210 * ones that are found to 'bundles'. */
1212 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1213 void **auxes, size_t n_auxes,
1214 struct hmapx *bundles)
1218 hmapx_init(bundles);
1219 for (i = 0; i < n_auxes; i++) {
1220 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1222 hmapx_add(bundles, bundle);
1228 bundle_update(struct ofbundle *bundle)
1230 struct ofport_dpif *port;
1232 bundle->floodable = true;
1233 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1234 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1235 || !stp_forward_in_state(port->stp_state)) {
1236 bundle->floodable = false;
1243 bundle_del_port(struct ofport_dpif *port)
1245 struct ofbundle *bundle = port->bundle;
1247 bundle->ofproto->need_revalidate = true;
1249 list_remove(&port->bundle_node);
1250 port->bundle = NULL;
1253 lacp_slave_unregister(bundle->lacp, port);
1256 bond_slave_unregister(bundle->bond, port);
1259 bundle_update(bundle);
1263 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1264 struct lacp_slave_settings *lacp,
1265 uint32_t bond_stable_id)
1267 struct ofport_dpif *port;
1269 port = get_ofp_port(bundle->ofproto, ofp_port);
1274 if (port->bundle != bundle) {
1275 bundle->ofproto->need_revalidate = true;
1277 bundle_del_port(port);
1280 port->bundle = bundle;
1281 list_push_back(&bundle->ports, &port->bundle_node);
1282 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1283 || !stp_forward_in_state(port->stp_state)) {
1284 bundle->floodable = false;
1288 port->bundle->ofproto->need_revalidate = true;
1289 lacp_slave_register(bundle->lacp, port, lacp);
1292 port->bond_stable_id = bond_stable_id;
1298 bundle_destroy(struct ofbundle *bundle)
1300 struct ofproto_dpif *ofproto;
1301 struct ofport_dpif *port, *next_port;
1308 ofproto = bundle->ofproto;
1309 for (i = 0; i < MAX_MIRRORS; i++) {
1310 struct ofmirror *m = ofproto->mirrors[i];
1312 if (m->out == bundle) {
1314 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1315 || hmapx_find_and_delete(&m->dsts, bundle)) {
1316 ofproto->need_revalidate = true;
1321 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1322 bundle_del_port(port);
1325 bundle_flush_macs(bundle);
1326 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1328 free(bundle->trunks);
1329 lacp_destroy(bundle->lacp);
1330 bond_destroy(bundle->bond);
1335 bundle_set(struct ofproto *ofproto_, void *aux,
1336 const struct ofproto_bundle_settings *s)
1338 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1339 bool need_flush = false;
1340 struct ofport_dpif *port;
1341 struct ofbundle *bundle;
1342 unsigned long *trunks;
1348 bundle_destroy(bundle_lookup(ofproto, aux));
1352 assert(s->n_slaves == 1 || s->bond != NULL);
1353 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1355 bundle = bundle_lookup(ofproto, aux);
1357 bundle = xmalloc(sizeof *bundle);
1359 bundle->ofproto = ofproto;
1360 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1361 hash_pointer(aux, 0));
1363 bundle->name = NULL;
1365 list_init(&bundle->ports);
1366 bundle->vlan_mode = PORT_VLAN_TRUNK;
1368 bundle->trunks = NULL;
1369 bundle->lacp = NULL;
1370 bundle->bond = NULL;
1372 bundle->floodable = true;
1374 bundle->src_mirrors = 0;
1375 bundle->dst_mirrors = 0;
1376 bundle->mirror_out = 0;
1379 if (!bundle->name || strcmp(s->name, bundle->name)) {
1381 bundle->name = xstrdup(s->name);
1386 if (!bundle->lacp) {
1387 ofproto->need_revalidate = true;
1388 bundle->lacp = lacp_create();
1390 lacp_configure(bundle->lacp, s->lacp);
1392 lacp_destroy(bundle->lacp);
1393 bundle->lacp = NULL;
1396 /* Update set of ports. */
1398 for (i = 0; i < s->n_slaves; i++) {
1399 if (!bundle_add_port(bundle, s->slaves[i],
1400 s->lacp ? &s->lacp_slaves[i] : NULL,
1401 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1405 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1406 struct ofport_dpif *next_port;
1408 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1409 for (i = 0; i < s->n_slaves; i++) {
1410 if (s->slaves[i] == port->up.ofp_port) {
1415 bundle_del_port(port);
1419 assert(list_size(&bundle->ports) <= s->n_slaves);
1421 if (list_is_empty(&bundle->ports)) {
1422 bundle_destroy(bundle);
1426 /* Set VLAN tagging mode */
1427 if (s->vlan_mode != bundle->vlan_mode) {
1428 bundle->vlan_mode = s->vlan_mode;
1433 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1434 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1436 if (vlan != bundle->vlan) {
1437 bundle->vlan = vlan;
1441 /* Get trunked VLANs. */
1442 switch (s->vlan_mode) {
1443 case PORT_VLAN_ACCESS:
1447 case PORT_VLAN_TRUNK:
1448 trunks = (unsigned long *) s->trunks;
1451 case PORT_VLAN_NATIVE_UNTAGGED:
1452 case PORT_VLAN_NATIVE_TAGGED:
1453 if (vlan != 0 && (!s->trunks
1454 || !bitmap_is_set(s->trunks, vlan)
1455 || bitmap_is_set(s->trunks, 0))) {
1456 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1458 trunks = bitmap_clone(s->trunks, 4096);
1460 trunks = bitmap_allocate1(4096);
1462 bitmap_set1(trunks, vlan);
1463 bitmap_set0(trunks, 0);
1465 trunks = (unsigned long *) s->trunks;
1472 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1473 free(bundle->trunks);
1474 if (trunks == s->trunks) {
1475 bundle->trunks = vlan_bitmap_clone(trunks);
1477 bundle->trunks = trunks;
1482 if (trunks != s->trunks) {
1487 if (!list_is_short(&bundle->ports)) {
1488 bundle->ofproto->has_bonded_bundles = true;
1490 if (bond_reconfigure(bundle->bond, s->bond)) {
1491 ofproto->need_revalidate = true;
1494 bundle->bond = bond_create(s->bond);
1495 ofproto->need_revalidate = true;
1498 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1499 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1503 bond_destroy(bundle->bond);
1504 bundle->bond = NULL;
1507 /* If we changed something that would affect MAC learning, un-learn
1508 * everything on this port and force flow revalidation. */
1510 bundle_flush_macs(bundle);
1517 bundle_remove(struct ofport *port_)
1519 struct ofport_dpif *port = ofport_dpif_cast(port_);
1520 struct ofbundle *bundle = port->bundle;
1523 bundle_del_port(port);
1524 if (list_is_empty(&bundle->ports)) {
1525 bundle_destroy(bundle);
1526 } else if (list_is_short(&bundle->ports)) {
1527 bond_destroy(bundle->bond);
1528 bundle->bond = NULL;
1534 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1536 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1537 struct ofport_dpif *port = port_;
1538 uint8_t ea[ETH_ADDR_LEN];
1541 error = netdev_get_etheraddr(port->up.netdev, ea);
1543 struct ofpbuf packet;
1546 ofpbuf_init(&packet, 0);
1547 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1549 memcpy(packet_pdu, pdu, pdu_size);
1551 send_packet(ofproto_dpif_cast(port->up.ofproto), port->odp_port,
1553 ofpbuf_uninit(&packet);
1555 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1556 "%s (%s)", port->bundle->name,
1557 netdev_get_name(port->up.netdev), strerror(error));
1562 bundle_send_learning_packets(struct ofbundle *bundle)
1564 struct ofproto_dpif *ofproto = bundle->ofproto;
1565 int error, n_packets, n_errors;
1566 struct mac_entry *e;
1568 error = n_packets = n_errors = 0;
1569 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1570 if (e->port.p != bundle) {
1571 struct ofpbuf *learning_packet;
1572 struct ofport_dpif *port;
1575 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1578 ret = send_packet(ofproto_dpif_cast(port->up.ofproto),
1579 port->odp_port, learning_packet);
1580 ofpbuf_delete(learning_packet);
1590 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1591 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1592 "packets, last error was: %s",
1593 bundle->name, n_errors, n_packets, strerror(error));
1595 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1596 bundle->name, n_packets);
1601 bundle_run(struct ofbundle *bundle)
1604 lacp_run(bundle->lacp, send_pdu_cb);
1607 struct ofport_dpif *port;
1609 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1610 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1613 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1614 lacp_negotiated(bundle->lacp));
1615 if (bond_should_send_learning_packets(bundle->bond)) {
1616 bundle_send_learning_packets(bundle);
1622 bundle_wait(struct ofbundle *bundle)
1625 lacp_wait(bundle->lacp);
1628 bond_wait(bundle->bond);
1635 mirror_scan(struct ofproto_dpif *ofproto)
1639 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1640 if (!ofproto->mirrors[idx]) {
1647 static struct ofmirror *
1648 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1652 for (i = 0; i < MAX_MIRRORS; i++) {
1653 struct ofmirror *mirror = ofproto->mirrors[i];
1654 if (mirror && mirror->aux == aux) {
1663 mirror_set(struct ofproto *ofproto_, void *aux,
1664 const struct ofproto_mirror_settings *s)
1666 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1667 mirror_mask_t mirror_bit;
1668 struct ofbundle *bundle;
1669 struct ofmirror *mirror;
1670 struct ofbundle *out;
1671 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1672 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1675 mirror = mirror_lookup(ofproto, aux);
1677 mirror_destroy(mirror);
1683 idx = mirror_scan(ofproto);
1685 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1687 ofproto->up.name, MAX_MIRRORS, s->name);
1691 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1692 mirror->ofproto = ofproto;
1695 mirror->out_vlan = -1;
1696 mirror->name = NULL;
1699 if (!mirror->name || strcmp(s->name, mirror->name)) {
1701 mirror->name = xstrdup(s->name);
1704 /* Get the new configuration. */
1705 if (s->out_bundle) {
1706 out = bundle_lookup(ofproto, s->out_bundle);
1708 mirror_destroy(mirror);
1714 out_vlan = s->out_vlan;
1716 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1717 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1719 /* If the configuration has not changed, do nothing. */
1720 if (hmapx_equals(&srcs, &mirror->srcs)
1721 && hmapx_equals(&dsts, &mirror->dsts)
1722 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1723 && mirror->out == out
1724 && mirror->out_vlan == out_vlan)
1726 hmapx_destroy(&srcs);
1727 hmapx_destroy(&dsts);
1731 hmapx_swap(&srcs, &mirror->srcs);
1732 hmapx_destroy(&srcs);
1734 hmapx_swap(&dsts, &mirror->dsts);
1735 hmapx_destroy(&dsts);
1737 free(mirror->vlans);
1738 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1741 mirror->out_vlan = out_vlan;
1743 /* Update bundles. */
1744 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1745 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1746 if (hmapx_contains(&mirror->srcs, bundle)) {
1747 bundle->src_mirrors |= mirror_bit;
1749 bundle->src_mirrors &= ~mirror_bit;
1752 if (hmapx_contains(&mirror->dsts, bundle)) {
1753 bundle->dst_mirrors |= mirror_bit;
1755 bundle->dst_mirrors &= ~mirror_bit;
1758 if (mirror->out == bundle) {
1759 bundle->mirror_out |= mirror_bit;
1761 bundle->mirror_out &= ~mirror_bit;
1765 ofproto->need_revalidate = true;
1766 mac_learning_flush(ofproto->ml);
1772 mirror_destroy(struct ofmirror *mirror)
1774 struct ofproto_dpif *ofproto;
1775 mirror_mask_t mirror_bit;
1776 struct ofbundle *bundle;
1782 ofproto = mirror->ofproto;
1783 ofproto->need_revalidate = true;
1784 mac_learning_flush(ofproto->ml);
1786 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1787 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1788 bundle->src_mirrors &= ~mirror_bit;
1789 bundle->dst_mirrors &= ~mirror_bit;
1790 bundle->mirror_out &= ~mirror_bit;
1793 hmapx_destroy(&mirror->srcs);
1794 hmapx_destroy(&mirror->dsts);
1795 free(mirror->vlans);
1797 ofproto->mirrors[mirror->idx] = NULL;
1803 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1805 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1806 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1807 ofproto->need_revalidate = true;
1808 mac_learning_flush(ofproto->ml);
1814 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
1816 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1817 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1818 return bundle && bundle->mirror_out != 0;
1822 forward_bpdu_changed(struct ofproto *ofproto_)
1824 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1825 /* Revalidate cached flows whenever forward_bpdu option changes. */
1826 ofproto->need_revalidate = true;
1831 static struct ofport_dpif *
1832 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1834 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1835 return ofport ? ofport_dpif_cast(ofport) : NULL;
1838 static struct ofport_dpif *
1839 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1841 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1845 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1846 struct dpif_port *dpif_port)
1848 ofproto_port->name = dpif_port->name;
1849 ofproto_port->type = dpif_port->type;
1850 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1854 port_run(struct ofport_dpif *ofport)
1856 bool enable = netdev_get_carrier(ofport->up.netdev);
1859 cfm_run(ofport->cfm);
1861 if (cfm_should_send_ccm(ofport->cfm)) {
1862 struct ofpbuf packet;
1864 ofpbuf_init(&packet, 0);
1865 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1866 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1867 ofport->odp_port, &packet);
1868 ofpbuf_uninit(&packet);
1871 enable = enable && !cfm_get_fault(ofport->cfm)
1872 && cfm_get_opup(ofport->cfm);
1875 if (ofport->bundle) {
1876 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1879 if (ofport->may_enable != enable) {
1880 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1882 if (ofproto->has_bundle_action) {
1883 ofproto->need_revalidate = true;
1887 ofport->may_enable = enable;
1891 port_wait(struct ofport_dpif *ofport)
1894 cfm_wait(ofport->cfm);
1899 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1900 struct ofproto_port *ofproto_port)
1902 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1903 struct dpif_port dpif_port;
1906 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1908 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1914 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1916 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1920 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1922 *ofp_portp = odp_port_to_ofp_port(odp_port);
1928 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1930 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1933 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1935 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1937 /* The caller is going to close ofport->up.netdev. If this is a
1938 * bonded port, then the bond is using that netdev, so remove it
1939 * from the bond. The client will need to reconfigure everything
1940 * after deleting ports, so then the slave will get re-added. */
1941 bundle_remove(&ofport->up);
1947 struct port_dump_state {
1948 struct dpif_port_dump dump;
1953 port_dump_start(const struct ofproto *ofproto_, void **statep)
1955 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1956 struct port_dump_state *state;
1958 *statep = state = xmalloc(sizeof *state);
1959 dpif_port_dump_start(&state->dump, ofproto->dpif);
1960 state->done = false;
1965 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1966 struct ofproto_port *port)
1968 struct port_dump_state *state = state_;
1969 struct dpif_port dpif_port;
1971 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1972 ofproto_port_from_dpif_port(port, &dpif_port);
1975 int error = dpif_port_dump_done(&state->dump);
1977 return error ? error : EOF;
1982 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1984 struct port_dump_state *state = state_;
1987 dpif_port_dump_done(&state->dump);
1994 port_poll(const struct ofproto *ofproto_, char **devnamep)
1996 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1997 return dpif_port_poll(ofproto->dpif, devnamep);
2001 port_poll_wait(const struct ofproto *ofproto_)
2003 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2004 dpif_port_poll_wait(ofproto->dpif);
2008 port_is_lacp_current(const struct ofport *ofport_)
2010 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2011 return (ofport->bundle && ofport->bundle->lacp
2012 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2016 /* Upcall handling. */
2018 /* Flow miss batching.
2020 * Some dpifs implement operations faster when you hand them off in a batch.
2021 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2022 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2023 * more packets, plus possibly installing the flow in the dpif.
2025 * So far we only batch the operations that affect flow setup time the most.
2026 * It's possible to batch more than that, but the benefit might be minimal. */
2028 struct hmap_node hmap_node;
2030 const struct nlattr *key;
2032 struct list packets;
2035 struct flow_miss_op {
2036 union dpif_op dpif_op;
2037 struct facet *facet;
2040 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2041 * OpenFlow controller as necessary according to their individual
2044 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2045 * ownership is transferred to this function. */
2047 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2048 const struct flow *flow, bool clone)
2050 struct ofputil_packet_in pin;
2052 pin.packet = packet;
2053 pin.in_port = flow->in_port;
2054 pin.reason = OFPR_NO_MATCH;
2055 pin.buffer_id = 0; /* not yet known */
2056 pin.send_len = 0; /* not used for flow table misses */
2057 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2058 clone ? NULL : packet);
2061 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2062 * OpenFlow controller as necessary according to their individual
2065 * 'send_len' should be the number of bytes of 'packet' to send to the
2066 * controller, as specified in the action that caused the packet to be sent.
2068 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2069 * Otherwise, ownership is transferred to this function. */
2071 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2072 uint64_t userdata, const struct flow *flow, bool clone)
2074 struct ofputil_packet_in pin;
2075 struct user_action_cookie cookie;
2077 memcpy(&cookie, &userdata, sizeof(cookie));
2079 pin.packet = packet;
2080 pin.in_port = flow->in_port;
2081 pin.reason = OFPR_ACTION;
2082 pin.buffer_id = 0; /* not yet known */
2083 pin.send_len = cookie.data;
2084 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2085 clone ? NULL : packet);
2089 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2090 const struct ofpbuf *packet)
2092 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2098 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2100 cfm_process_heartbeat(ofport->cfm, packet);
2103 } else if (ofport->bundle && ofport->bundle->lacp
2104 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2106 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2109 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2111 stp_process_packet(ofport, packet);
2118 static struct flow_miss *
2119 flow_miss_create(struct hmap *todo, const struct flow *flow,
2120 const struct nlattr *key, size_t key_len)
2122 uint32_t hash = flow_hash(flow, 0);
2123 struct flow_miss *miss;
2125 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2126 if (flow_equal(&miss->flow, flow)) {
2131 miss = xmalloc(sizeof *miss);
2132 hmap_insert(todo, &miss->hmap_node, hash);
2135 miss->key_len = key_len;
2136 list_init(&miss->packets);
2141 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2142 struct flow_miss_op *ops, size_t *n_ops)
2144 const struct flow *flow = &miss->flow;
2145 struct ofpbuf *packet, *next_packet;
2146 struct facet *facet;
2148 facet = facet_lookup_valid(ofproto, flow);
2150 struct rule_dpif *rule;
2152 rule = rule_dpif_lookup(ofproto, flow, 0);
2154 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2155 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2157 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2158 COVERAGE_INC(ofproto_dpif_no_packet_in);
2159 /* XXX install 'drop' flow entry */
2163 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2167 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2169 list_remove(&packet->list_node);
2170 send_packet_in_miss(ofproto, packet, flow, false);
2176 facet = facet_create(rule, flow);
2179 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2180 list_remove(&packet->list_node);
2181 ofproto->n_matches++;
2183 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2185 * Extra-special case for fail-open mode.
2187 * We are in fail-open mode and the packet matched the fail-open
2188 * rule, but we are connected to a controller too. We should send
2189 * the packet up to the controller in the hope that it will try to
2190 * set up a flow and thereby allow us to exit fail-open.
2192 * See the top-level comment in fail-open.c for more information.
2194 send_packet_in_miss(ofproto, packet, flow, true);
2197 if (!facet->may_install) {
2198 facet_make_actions(ofproto, facet, packet);
2200 if (!execute_controller_action(ofproto, &facet->flow,
2201 facet->actions, facet->actions_len,
2203 struct flow_miss_op *op = &ops[(*n_ops)++];
2204 struct dpif_execute *execute = &op->dpif_op.execute;
2207 execute->type = DPIF_OP_EXECUTE;
2208 execute->key = miss->key;
2209 execute->key_len = miss->key_len;
2211 = (facet->may_install
2213 : xmemdup(facet->actions, facet->actions_len));
2214 execute->actions_len = facet->actions_len;
2215 execute->packet = packet;
2219 if (facet->may_install) {
2220 struct flow_miss_op *op = &ops[(*n_ops)++];
2221 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2224 put->type = DPIF_OP_FLOW_PUT;
2225 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2226 put->key = miss->key;
2227 put->key_len = miss->key_len;
2228 put->actions = facet->actions;
2229 put->actions_len = facet->actions_len;
2235 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2238 struct dpif_upcall *upcall;
2239 struct flow_miss *miss, *next_miss;
2240 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2241 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2250 /* Construct the to-do list.
2252 * This just amounts to extracting the flow from each packet and sticking
2253 * the packets that have the same flow in the same "flow_miss" structure so
2254 * that we can process them together. */
2256 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2257 struct flow_miss *miss;
2260 /* Obtain in_port and tun_id, at least, then set 'flow''s header
2262 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2263 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2264 flow.in_port, &flow);
2266 /* Handle 802.1ag, LACP, and STP specially. */
2267 if (process_special(ofproto, &flow, upcall->packet)) {
2268 ofpbuf_delete(upcall->packet);
2269 ofproto->n_matches++;
2273 /* Add other packets to a to-do list. */
2274 miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len);
2275 list_push_back(&miss->packets, &upcall->packet->list_node);
2278 /* Process each element in the to-do list, constructing the set of
2279 * operations to batch. */
2281 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2282 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2283 ofpbuf_list_delete(&miss->packets);
2284 hmap_remove(&todo, &miss->hmap_node);
2287 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2288 hmap_destroy(&todo);
2290 /* Execute batch. */
2291 for (i = 0; i < n_ops; i++) {
2292 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2294 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2296 /* Free memory and update facets. */
2297 for (i = 0; i < n_ops; i++) {
2298 struct flow_miss_op *op = &flow_miss_ops[i];
2299 struct dpif_execute *execute;
2300 struct dpif_flow_put *put;
2302 switch (op->dpif_op.type) {
2303 case DPIF_OP_EXECUTE:
2304 execute = &op->dpif_op.execute;
2305 if (op->facet->actions != execute->actions) {
2306 free((struct nlattr *) execute->actions);
2308 ofpbuf_delete((struct ofpbuf *) execute->packet);
2311 case DPIF_OP_FLOW_PUT:
2312 put = &op->dpif_op.flow_put;
2314 op->facet->installed = true;
2322 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2323 struct dpif_upcall *upcall)
2326 struct user_action_cookie cookie;
2328 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2330 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2331 if (ofproto->sflow) {
2332 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2333 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2335 ofpbuf_delete(upcall->packet);
2337 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2338 COVERAGE_INC(ofproto_dpif_ctlr_action);
2339 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2340 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2343 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2348 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2350 switch (upcall->type) {
2351 case DPIF_UC_ACTION:
2352 handle_userspace_upcall(ofproto, upcall);
2356 /* The caller handles these. */
2359 case DPIF_N_UC_TYPES:
2361 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2366 /* Flow expiration. */
2368 static int facet_max_idle(const struct ofproto_dpif *);
2369 static void update_stats(struct ofproto_dpif *);
2370 static void rule_expire(struct rule_dpif *);
2371 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
2373 /* This function is called periodically by run(). Its job is to collect
2374 * updates for the flows that have been installed into the datapath, most
2375 * importantly when they last were used, and then use that information to
2376 * expire flows that have not been used recently.
2378 * Returns the number of milliseconds after which it should be called again. */
2380 expire(struct ofproto_dpif *ofproto)
2382 struct rule_dpif *rule, *next_rule;
2383 struct classifier *table;
2386 /* Update stats for each flow in the datapath. */
2387 update_stats(ofproto);
2389 /* Expire facets that have been idle too long. */
2390 dp_max_idle = facet_max_idle(ofproto);
2391 expire_facets(ofproto, dp_max_idle);
2393 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2394 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2395 struct cls_cursor cursor;
2397 cls_cursor_init(&cursor, table, NULL);
2398 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2403 /* All outstanding data in existing flows has been accounted, so it's a
2404 * good time to do bond rebalancing. */
2405 if (ofproto->has_bonded_bundles) {
2406 struct ofbundle *bundle;
2408 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2410 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2415 return MIN(dp_max_idle, 1000);
2418 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2420 * This function also pushes statistics updates to rules which each facet
2421 * resubmits into. Generally these statistics will be accurate. However, if a
2422 * facet changes the rule it resubmits into at some time in between
2423 * update_stats() runs, it is possible that statistics accrued to the
2424 * old rule will be incorrectly attributed to the new rule. This could be
2425 * avoided by calling update_stats() whenever rules are created or
2426 * deleted. However, the performance impact of making so many calls to the
2427 * datapath do not justify the benefit of having perfectly accurate statistics.
2430 update_stats(struct ofproto_dpif *p)
2432 const struct dpif_flow_stats *stats;
2433 struct dpif_flow_dump dump;
2434 const struct nlattr *key;
2437 dpif_flow_dump_start(&dump, p->dpif);
2438 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2439 struct facet *facet;
2442 if (odp_flow_key_to_flow(key, key_len, &flow)) {
2446 odp_flow_key_format(key, key_len, &s);
2447 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
2453 facet = facet_find(p, &flow);
2455 if (facet && facet->installed) {
2457 if (stats->n_packets >= facet->dp_packet_count) {
2458 uint64_t extra = stats->n_packets - facet->dp_packet_count;
2459 facet->packet_count += extra;
2461 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2464 if (stats->n_bytes >= facet->dp_byte_count) {
2465 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
2467 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2470 facet->dp_packet_count = stats->n_packets;
2471 facet->dp_byte_count = stats->n_bytes;
2473 facet_update_time(p, facet, stats->used);
2474 facet_account(p, facet);
2475 facet_push_stats(facet);
2477 /* There's a flow in the datapath that we know nothing about.
2479 COVERAGE_INC(facet_unexpected);
2480 dpif_flow_del(p->dpif, key, key_len, NULL);
2483 dpif_flow_dump_done(&dump);
2486 /* Calculates and returns the number of milliseconds of idle time after which
2487 * facets should expire from the datapath and we should fold their statistics
2488 * into their parent rules in userspace. */
2490 facet_max_idle(const struct ofproto_dpif *ofproto)
2493 * Idle time histogram.
2495 * Most of the time a switch has a relatively small number of facets. When
2496 * this is the case we might as well keep statistics for all of them in
2497 * userspace and to cache them in the kernel datapath for performance as
2500 * As the number of facets increases, the memory required to maintain
2501 * statistics about them in userspace and in the kernel becomes
2502 * significant. However, with a large number of facets it is likely that
2503 * only a few of them are "heavy hitters" that consume a large amount of
2504 * bandwidth. At this point, only heavy hitters are worth caching in the
2505 * kernel and maintaining in userspaces; other facets we can discard.
2507 * The technique used to compute the idle time is to build a histogram with
2508 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
2509 * that is installed in the kernel gets dropped in the appropriate bucket.
2510 * After the histogram has been built, we compute the cutoff so that only
2511 * the most-recently-used 1% of facets (but at least
2512 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2513 * the most-recently-used bucket of facets is kept, so actually an
2514 * arbitrary number of facets can be kept in any given expiration run
2515 * (though the next run will delete most of those unless they receive
2518 * This requires a second pass through the facets, in addition to the pass
2519 * made by update_stats(), because the former function never looks
2520 * at uninstallable facets.
2522 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2523 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2524 int buckets[N_BUCKETS] = { 0 };
2525 int total, subtotal, bucket;
2526 struct facet *facet;
2530 total = hmap_count(&ofproto->facets);
2531 if (total <= ofproto->up.flow_eviction_threshold) {
2532 return N_BUCKETS * BUCKET_WIDTH;
2535 /* Build histogram. */
2537 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
2538 long long int idle = now - facet->used;
2539 int bucket = (idle <= 0 ? 0
2540 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2541 : (unsigned int) idle / BUCKET_WIDTH);
2545 /* Find the first bucket whose flows should be expired. */
2546 subtotal = bucket = 0;
2548 subtotal += buckets[bucket++];
2549 } while (bucket < N_BUCKETS &&
2550 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2552 if (VLOG_IS_DBG_ENABLED()) {
2556 ds_put_cstr(&s, "keep");
2557 for (i = 0; i < N_BUCKETS; i++) {
2559 ds_put_cstr(&s, ", drop");
2562 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2565 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2569 return bucket * BUCKET_WIDTH;
2573 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2575 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2576 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2577 struct ofexpired expired;
2579 if (facet->installed) {
2580 struct dpif_flow_stats stats;
2582 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2584 facet_update_stats(ofproto, facet, &stats);
2587 expired.flow = facet->flow;
2588 expired.packet_count = facet->packet_count;
2589 expired.byte_count = facet->byte_count;
2590 expired.used = facet->used;
2591 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2596 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2598 long long int cutoff = time_msec() - dp_max_idle;
2599 struct facet *facet, *next_facet;
2601 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2602 facet_active_timeout(ofproto, facet);
2603 if (facet->used < cutoff) {
2604 facet_remove(ofproto, facet);
2609 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2610 * then delete it entirely. */
2612 rule_expire(struct rule_dpif *rule)
2614 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2615 struct facet *facet, *next_facet;
2619 /* Has 'rule' expired? */
2621 if (rule->up.hard_timeout
2622 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2623 reason = OFPRR_HARD_TIMEOUT;
2624 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2625 && now > rule->used + rule->up.idle_timeout * 1000) {
2626 reason = OFPRR_IDLE_TIMEOUT;
2631 COVERAGE_INC(ofproto_dpif_expired);
2633 /* Update stats. (This is a no-op if the rule expired due to an idle
2634 * timeout, because that only happens when the rule has no facets left.) */
2635 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2636 facet_remove(ofproto, facet);
2639 /* Get rid of the rule. */
2640 ofproto_rule_expire(&rule->up, reason);
2645 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2647 * The caller must already have determined that no facet with an identical
2648 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2649 * the ofproto's classifier table.
2651 * The facet will initially have no ODP actions. The caller should fix that
2652 * by calling facet_make_actions(). */
2653 static struct facet *
2654 facet_create(struct rule_dpif *rule, const struct flow *flow)
2656 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2657 struct facet *facet;
2659 facet = xzalloc(sizeof *facet);
2660 facet->used = time_msec();
2661 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2662 list_push_back(&rule->facets, &facet->list_node);
2664 facet->flow = *flow;
2665 netflow_flow_init(&facet->nf_flow);
2666 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2672 facet_free(struct facet *facet)
2674 free(facet->actions);
2679 execute_controller_action(struct ofproto_dpif *ofproto,
2680 const struct flow *flow,
2681 const struct nlattr *odp_actions, size_t actions_len,
2682 struct ofpbuf *packet)
2685 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2686 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2687 /* As an optimization, avoid a round-trip from userspace to kernel to
2688 * userspace. This also avoids possibly filling up kernel packet
2689 * buffers along the way.
2691 * This optimization will not accidentally catch sFlow
2692 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2693 * inside OVS_ACTION_ATTR_SAMPLE. */
2694 const struct nlattr *nla;
2696 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2697 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2705 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2706 * 'packet', which arrived on 'in_port'.
2708 * Takes ownership of 'packet'. */
2710 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2711 const struct nlattr *odp_actions, size_t actions_len,
2712 struct ofpbuf *packet)
2714 struct odputil_keybuf keybuf;
2718 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2723 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2724 odp_flow_key_from_flow(&key, flow);
2726 error = dpif_execute(ofproto->dpif, key.data, key.size,
2727 odp_actions, actions_len, packet);
2729 ofpbuf_delete(packet);
2733 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2734 * statistics appropriately. 'packet' must have at least sizeof(struct
2735 * ofp_packet_in) bytes of headroom.
2737 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2738 * applying flow_extract() to 'packet' would yield the same flow as
2741 * 'facet' must have accurately composed datapath actions; that is, it must
2742 * not be in need of revalidation.
2744 * Takes ownership of 'packet'. */
2746 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2747 struct ofpbuf *packet)
2749 struct dpif_flow_stats stats;
2751 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2753 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2754 stats.used = time_msec();
2755 if (execute_odp_actions(ofproto, &facet->flow,
2756 facet->actions, facet->actions_len, packet)) {
2757 facet_update_stats(ofproto, facet, &stats);
2761 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2763 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2764 * rule's statistics, via facet_uninstall().
2766 * - Removes 'facet' from its rule and from ofproto->facets.
2769 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2771 facet_uninstall(ofproto, facet);
2772 facet_flush_stats(ofproto, facet);
2773 hmap_remove(&ofproto->facets, &facet->hmap_node);
2774 list_remove(&facet->list_node);
2778 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2780 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2781 const struct ofpbuf *packet)
2783 const struct rule_dpif *rule = facet->rule;
2784 struct ofpbuf *odp_actions;
2785 struct action_xlate_ctx ctx;
2787 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2788 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2789 facet->tags = ctx.tags;
2790 facet->may_install = ctx.may_set_up_flow;
2791 facet->has_learn = ctx.has_learn;
2792 facet->has_normal = ctx.has_normal;
2793 facet->nf_flow.output_iface = ctx.nf_output_iface;
2795 if (facet->actions_len != odp_actions->size
2796 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2797 free(facet->actions);
2798 facet->actions_len = odp_actions->size;
2799 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2802 ofpbuf_delete(odp_actions);
2805 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2806 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2807 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2808 * since 'facet' was last updated.
2810 * Returns 0 if successful, otherwise a positive errno value.*/
2812 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2813 const struct nlattr *actions, size_t actions_len,
2814 struct dpif_flow_stats *stats)
2816 struct odputil_keybuf keybuf;
2817 enum dpif_flow_put_flags flags;
2821 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2823 flags |= DPIF_FP_ZERO_STATS;
2826 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2827 odp_flow_key_from_flow(&key, &facet->flow);
2829 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2830 actions, actions_len, stats);
2833 facet_reset_dp_stats(facet, stats);
2839 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2840 * 'zero_stats' is true, clears any existing statistics from the datapath for
2843 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2845 struct dpif_flow_stats stats;
2847 if (facet->may_install
2848 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2849 zero_stats ? &stats : NULL)) {
2850 facet->installed = true;
2855 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2858 const struct nlattr *a;
2862 if (facet->byte_count <= facet->accounted_bytes) {
2865 n_bytes = facet->byte_count - facet->accounted_bytes;
2866 facet->accounted_bytes = facet->byte_count;
2868 /* Feed information from the active flows back into the learning table to
2869 * ensure that table is always in sync with what is actually flowing
2870 * through the datapath. */
2871 if (facet->has_learn || facet->has_normal) {
2872 struct action_xlate_ctx ctx;
2874 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2875 ctx.may_learn = true;
2876 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2877 facet->rule->up.n_actions));
2880 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2884 /* This loop feeds byte counters to bond_account() for rebalancing to use
2885 * as a basis. We also need to track the actual VLAN on which the packet
2886 * is going to be sent to ensure that it matches the one passed to
2887 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2889 vlan_tci = facet->flow.vlan_tci;
2890 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2891 const struct ovs_action_push_vlan *vlan;
2892 struct ofport_dpif *port;
2894 switch (nl_attr_type(a)) {
2895 case OVS_ACTION_ATTR_OUTPUT:
2896 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2897 if (port && port->bundle && port->bundle->bond) {
2898 bond_account(port->bundle->bond, &facet->flow,
2899 vlan_tci_to_vid(vlan_tci), n_bytes);
2903 case OVS_ACTION_ATTR_POP_VLAN:
2904 vlan_tci = htons(0);
2907 case OVS_ACTION_ATTR_PUSH_VLAN:
2908 vlan = nl_attr_get(a);
2909 vlan_tci = vlan->vlan_tci;
2915 /* If 'rule' is installed in the datapath, uninstalls it. */
2917 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2919 if (facet->installed) {
2920 struct odputil_keybuf keybuf;
2921 struct dpif_flow_stats stats;
2925 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2926 odp_flow_key_from_flow(&key, &facet->flow);
2928 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2929 facet_reset_dp_stats(facet, &stats);
2931 facet_update_stats(p, facet, &stats);
2933 facet->installed = false;
2935 assert(facet->dp_packet_count == 0);
2936 assert(facet->dp_byte_count == 0);
2940 /* Returns true if the only action for 'facet' is to send to the controller.
2941 * (We don't report NetFlow expiration messages for such facets because they
2942 * are just part of the control logic for the network, not real traffic). */
2944 facet_is_controller_flow(struct facet *facet)
2947 && facet->rule->up.n_actions == 1
2948 && action_outputs_to_port(&facet->rule->up.actions[0],
2949 htons(OFPP_CONTROLLER)));
2952 /* Resets 'facet''s datapath statistics counters. This should be called when
2953 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2954 * it should contain the statistics returned by dpif when 'facet' was reset in
2955 * the datapath. 'stats' will be modified to only included statistics new
2956 * since 'facet' was last updated. */
2958 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2960 if (stats && facet->dp_packet_count <= stats->n_packets
2961 && facet->dp_byte_count <= stats->n_bytes) {
2962 stats->n_packets -= facet->dp_packet_count;
2963 stats->n_bytes -= facet->dp_byte_count;
2966 facet->dp_packet_count = 0;
2967 facet->dp_byte_count = 0;
2970 /* Folds all of 'facet''s statistics into its rule. Also updates the
2971 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2972 * 'facet''s statistics in the datapath should have been zeroed and folded into
2973 * its packet and byte counts before this function is called. */
2975 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2977 assert(!facet->dp_byte_count);
2978 assert(!facet->dp_packet_count);
2980 facet_push_stats(facet);
2981 facet_account(ofproto, facet);
2983 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2984 struct ofexpired expired;
2985 expired.flow = facet->flow;
2986 expired.packet_count = facet->packet_count;
2987 expired.byte_count = facet->byte_count;
2988 expired.used = facet->used;
2989 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2992 facet->rule->packet_count += facet->packet_count;
2993 facet->rule->byte_count += facet->byte_count;
2995 /* Reset counters to prevent double counting if 'facet' ever gets
2997 facet_reset_counters(facet);
2999 netflow_flow_clear(&facet->nf_flow);
3002 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3003 * Returns it if found, otherwise a null pointer.
3005 * The returned facet might need revalidation; use facet_lookup_valid()
3006 * instead if that is important. */
3007 static struct facet *
3008 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3010 struct facet *facet;
3012 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3014 if (flow_equal(flow, &facet->flow)) {
3022 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3023 * Returns it if found, otherwise a null pointer.
3025 * The returned facet is guaranteed to be valid. */
3026 static struct facet *
3027 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3029 struct facet *facet = facet_find(ofproto, flow);
3031 /* The facet we found might not be valid, since we could be in need of
3032 * revalidation. If it is not valid, don't return it. */
3034 && (ofproto->need_revalidate
3035 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3036 && !facet_revalidate(ofproto, facet)) {
3037 COVERAGE_INC(facet_invalidated);
3044 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3046 * - If the rule found is different from 'facet''s current rule, moves
3047 * 'facet' to the new rule and recompiles its actions.
3049 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3050 * where it is and recompiles its actions anyway.
3052 * - If there is none, destroys 'facet'.
3054 * Returns true if 'facet' still exists, false if it has been destroyed. */
3056 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3058 struct action_xlate_ctx ctx;
3059 struct ofpbuf *odp_actions;
3060 struct rule_dpif *new_rule;
3061 bool actions_changed;
3063 COVERAGE_INC(facet_revalidate);
3065 /* Determine the new rule. */
3066 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3068 /* No new rule, so delete the facet. */
3069 facet_remove(ofproto, facet);
3073 /* Calculate new datapath actions.
3075 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3076 * emit a NetFlow expiration and, if so, we need to have the old state
3077 * around to properly compose it. */
3078 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3079 odp_actions = xlate_actions(&ctx,
3080 new_rule->up.actions, new_rule->up.n_actions);
3081 actions_changed = (facet->actions_len != odp_actions->size
3082 || memcmp(facet->actions, odp_actions->data,
3083 facet->actions_len));
3085 /* If the datapath actions changed or the installability changed,
3086 * then we need to talk to the datapath. */
3087 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
3088 if (ctx.may_set_up_flow) {
3089 struct dpif_flow_stats stats;
3091 facet_put__(ofproto, facet,
3092 odp_actions->data, odp_actions->size, &stats);
3093 facet_update_stats(ofproto, facet, &stats);
3095 facet_uninstall(ofproto, facet);
3098 /* The datapath flow is gone or has zeroed stats, so push stats out of
3099 * 'facet' into 'rule'. */
3100 facet_flush_stats(ofproto, facet);
3103 /* Update 'facet' now that we've taken care of all the old state. */
3104 facet->tags = ctx.tags;
3105 facet->nf_flow.output_iface = ctx.nf_output_iface;
3106 facet->may_install = ctx.may_set_up_flow;
3107 facet->has_learn = ctx.has_learn;
3108 facet->has_normal = ctx.has_normal;
3109 if (actions_changed) {
3110 free(facet->actions);
3111 facet->actions_len = odp_actions->size;
3112 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
3114 if (facet->rule != new_rule) {
3115 COVERAGE_INC(facet_changed_rule);
3116 list_remove(&facet->list_node);
3117 list_push_back(&new_rule->facets, &facet->list_node);
3118 facet->rule = new_rule;
3119 facet->used = new_rule->up.created;
3120 facet->rs_used = facet->used;
3123 ofpbuf_delete(odp_actions);
3128 /* Updates 'facet''s used time. Caller is responsible for calling
3129 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3131 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3134 if (used > facet->used) {
3136 if (used > facet->rule->used) {
3137 facet->rule->used = used;
3139 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3143 /* Folds the statistics from 'stats' into the counters in 'facet'.
3145 * Because of the meaning of a facet's counters, it only makes sense to do this
3146 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3147 * packet that was sent by hand or if it represents statistics that have been
3148 * cleared out of the datapath. */
3150 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
3151 const struct dpif_flow_stats *stats)
3153 if (stats->n_packets || stats->used > facet->used) {
3154 facet_update_time(ofproto, facet, stats->used);
3155 facet->packet_count += stats->n_packets;
3156 facet->byte_count += stats->n_bytes;
3157 facet_push_stats(facet);
3158 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3163 facet_reset_counters(struct facet *facet)
3165 facet->packet_count = 0;
3166 facet->byte_count = 0;
3167 facet->rs_packet_count = 0;
3168 facet->rs_byte_count = 0;
3169 facet->accounted_bytes = 0;
3173 facet_push_stats(struct facet *facet)
3175 uint64_t rs_packets, rs_bytes;
3177 assert(facet->packet_count >= facet->rs_packet_count);
3178 assert(facet->byte_count >= facet->rs_byte_count);
3179 assert(facet->used >= facet->rs_used);
3181 rs_packets = facet->packet_count - facet->rs_packet_count;
3182 rs_bytes = facet->byte_count - facet->rs_byte_count;
3184 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3185 facet->rs_packet_count = facet->packet_count;
3186 facet->rs_byte_count = facet->byte_count;
3187 facet->rs_used = facet->used;
3189 flow_push_stats(facet->rule, &facet->flow,
3190 rs_packets, rs_bytes, facet->used);
3194 struct ofproto_push {
3195 struct action_xlate_ctx ctx;
3202 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3204 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3207 rule->packet_count += push->packets;
3208 rule->byte_count += push->bytes;
3209 rule->used = MAX(push->used, rule->used);
3213 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3214 * 'rule''s actions. */
3216 flow_push_stats(const struct rule_dpif *rule,
3217 struct flow *flow, uint64_t packets, uint64_t bytes,
3220 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3221 struct ofproto_push push;
3223 push.packets = packets;
3227 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3228 push.ctx.resubmit_hook = push_resubmit;
3229 ofpbuf_delete(xlate_actions(&push.ctx,
3230 rule->up.actions, rule->up.n_actions));
3235 static struct rule_dpif *
3236 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3239 struct cls_rule *cls_rule;
3240 struct classifier *cls;
3242 if (table_id >= N_TABLES) {
3246 cls = &ofproto->up.tables[table_id];
3247 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3248 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3249 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3250 * are unavailable. */
3251 struct flow ofpc_normal_flow = *flow;
3252 ofpc_normal_flow.tp_src = htons(0);
3253 ofpc_normal_flow.tp_dst = htons(0);
3254 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3256 cls_rule = classifier_lookup(cls, flow);
3258 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3262 complete_operation(struct rule_dpif *rule)
3264 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3266 rule_invalidate(rule);
3268 struct dpif_completion *c = xmalloc(sizeof *c);
3269 c->op = rule->up.pending;
3270 list_push_back(&ofproto->completions, &c->list_node);
3272 ofoperation_complete(rule->up.pending, 0);
3276 static struct rule *
3279 struct rule_dpif *rule = xmalloc(sizeof *rule);
3284 rule_dealloc(struct rule *rule_)
3286 struct rule_dpif *rule = rule_dpif_cast(rule_);
3291 rule_construct(struct rule *rule_)
3293 struct rule_dpif *rule = rule_dpif_cast(rule_);
3294 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3295 struct rule_dpif *victim;
3299 error = validate_actions(rule->up.actions, rule->up.n_actions,
3300 &rule->up.cr.flow, ofproto->max_ports);
3305 rule->used = rule->up.created;
3306 rule->packet_count = 0;
3307 rule->byte_count = 0;
3309 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3310 if (victim && !list_is_empty(&victim->facets)) {
3311 struct facet *facet;
3313 rule->facets = victim->facets;
3314 list_moved(&rule->facets);
3315 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3316 /* XXX: We're only clearing our local counters here. It's possible
3317 * that quite a few packets are unaccounted for in the datapath
3318 * statistics. These will be accounted to the new rule instead of
3319 * cleared as required. This could be fixed by clearing out the
3320 * datapath statistics for this facet, but currently it doesn't
3322 facet_reset_counters(facet);
3326 /* Must avoid list_moved() in this case. */
3327 list_init(&rule->facets);
3330 table_id = rule->up.table_id;
3331 rule->tag = (victim ? victim->tag
3333 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3334 ofproto->tables[table_id].basis));
3336 complete_operation(rule);
3341 rule_destruct(struct rule *rule_)
3343 struct rule_dpif *rule = rule_dpif_cast(rule_);
3344 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3345 struct facet *facet, *next_facet;
3347 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3348 facet_revalidate(ofproto, facet);
3351 complete_operation(rule);
3355 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3357 struct rule_dpif *rule = rule_dpif_cast(rule_);
3358 struct facet *facet;
3360 /* Start from historical data for 'rule' itself that are no longer tracked
3361 * in facets. This counts, for example, facets that have expired. */
3362 *packets = rule->packet_count;
3363 *bytes = rule->byte_count;
3365 /* Add any statistics that are tracked by facets. This includes
3366 * statistical data recently updated by ofproto_update_stats() as well as
3367 * stats for packets that were executed "by hand" via dpif_execute(). */
3368 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3369 *packets += facet->packet_count;
3370 *bytes += facet->byte_count;
3375 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
3377 struct rule_dpif *rule = rule_dpif_cast(rule_);
3378 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3379 struct action_xlate_ctx ctx;
3380 struct ofpbuf *odp_actions;
3381 struct facet *facet;
3384 /* First look for a related facet. If we find one, account it to that. */
3385 facet = facet_lookup_valid(ofproto, flow);
3386 if (facet && facet->rule == rule) {
3387 if (!facet->may_install) {
3388 facet_make_actions(ofproto, facet, packet);
3390 facet_execute(ofproto, facet, packet);
3394 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
3395 * create a new facet for it and use that. */
3396 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
3397 facet = facet_create(rule, flow);
3398 facet_make_actions(ofproto, facet, packet);
3399 facet_execute(ofproto, facet, packet);
3400 facet_install(ofproto, facet, true);
3404 /* We can't account anything to a facet. If we were to try, then that
3405 * facet would have a non-matching rule, busting our invariants. */
3406 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3407 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3408 size = packet->size;
3409 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3410 odp_actions->size, packet)) {
3411 rule->used = time_msec();
3412 rule->packet_count++;
3413 rule->byte_count += size;
3414 flow_push_stats(rule, flow, 1, size, rule->used);
3416 ofpbuf_delete(odp_actions);
3422 rule_modify_actions(struct rule *rule_)
3424 struct rule_dpif *rule = rule_dpif_cast(rule_);
3425 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3428 error = validate_actions(rule->up.actions, rule->up.n_actions,
3429 &rule->up.cr.flow, ofproto->max_ports);
3431 ofoperation_complete(rule->up.pending, error);
3435 complete_operation(rule);
3438 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.
3439 * Returns 0 if successful, otherwise a positive errno value. */
3441 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
3442 const struct ofpbuf *packet)
3444 struct ofpbuf key, odp_actions;
3445 struct odputil_keybuf keybuf;
3449 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3450 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3451 odp_flow_key_from_flow(&key, &flow);
3453 ofpbuf_init(&odp_actions, 32);
3454 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3456 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3457 error = dpif_execute(ofproto->dpif,
3459 odp_actions.data, odp_actions.size,
3461 ofpbuf_uninit(&odp_actions);
3464 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3465 ofproto->up.name, odp_port, strerror(error));
3470 /* OpenFlow to datapath action translation. */
3472 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3473 struct action_xlate_ctx *ctx);
3474 static void xlate_normal(struct action_xlate_ctx *);
3477 put_userspace_action(const struct ofproto_dpif *ofproto,
3478 struct ofpbuf *odp_actions,
3479 const struct flow *flow,
3480 const struct user_action_cookie *cookie)
3485 pid = dpif_port_get_pid(ofproto->dpif,
3486 ofp_port_to_odp_port(flow->in_port));
3488 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3489 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3490 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3491 cookie, sizeof *cookie);
3492 nl_msg_end_nested(odp_actions, offset);
3494 return odp_actions->size - NLA_ALIGN(sizeof *cookie);
3497 /* Compose SAMPLE action for sFlow. */
3499 compose_sflow_action(const struct ofproto_dpif *ofproto,
3500 struct ofpbuf *odp_actions,
3501 const struct flow *flow,
3504 uint32_t port_ifindex;
3505 uint32_t probability;
3506 struct user_action_cookie cookie;
3507 size_t sample_offset, actions_offset;
3508 int cookie_offset, n_output;
3510 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3514 if (odp_port == OVSP_NONE) {
3518 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3522 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3524 /* Number of packets out of UINT_MAX to sample. */
3525 probability = dpif_sflow_get_probability(ofproto->sflow);
3526 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3528 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3530 cookie.type = USER_ACTION_COOKIE_SFLOW;
3531 cookie.data = port_ifindex;
3532 cookie.n_output = n_output;
3533 cookie.vlan_tci = 0;
3534 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3536 nl_msg_end_nested(odp_actions, actions_offset);
3537 nl_msg_end_nested(odp_actions, sample_offset);
3538 return cookie_offset;
3541 /* SAMPLE action must be first action in any given list of actions.
3542 * At this point we do not have all information required to build it. So try to
3543 * build sample action as complete as possible. */
3545 add_sflow_action(struct action_xlate_ctx *ctx)
3547 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3549 &ctx->flow, OVSP_NONE);
3550 ctx->sflow_odp_port = 0;
3551 ctx->sflow_n_outputs = 0;
3554 /* Fix SAMPLE action according to data collected while composing ODP actions.
3555 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3556 * USERSPACE action's user-cookie which is required for sflow. */
3558 fix_sflow_action(struct action_xlate_ctx *ctx)
3560 const struct flow *base = &ctx->base_flow;
3561 struct user_action_cookie *cookie;
3563 if (!ctx->user_cookie_offset) {
3567 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3569 assert(cookie != NULL);
3570 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3572 if (ctx->sflow_n_outputs) {
3573 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3574 ctx->sflow_odp_port);
3576 if (ctx->sflow_n_outputs >= 255) {
3577 cookie->n_output = 255;
3579 cookie->n_output = ctx->sflow_n_outputs;
3581 cookie->vlan_tci = base->vlan_tci;
3585 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3586 const void *key, size_t key_size)
3588 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3589 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3590 nl_msg_end_nested(odp_actions, offset);
3594 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3595 struct ofpbuf *odp_actions)
3597 if (base->tun_id == flow->tun_id) {
3600 base->tun_id = flow->tun_id;
3602 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
3603 &base->tun_id, sizeof(base->tun_id));
3607 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3608 struct ofpbuf *odp_actions)
3610 struct ovs_key_ethernet eth_key;
3612 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3613 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3617 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3618 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3620 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3621 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3623 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3624 ð_key, sizeof(eth_key));
3628 commit_vlan_action(struct action_xlate_ctx *ctx, ovs_be16 new_tci)
3630 struct flow *base = &ctx->base_flow;
3632 if (base->vlan_tci == new_tci) {
3636 if (base->vlan_tci & htons(VLAN_CFI)) {
3637 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3640 if (new_tci & htons(VLAN_CFI)) {
3641 struct ovs_action_push_vlan vlan;
3643 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3644 vlan.vlan_tci = new_tci;
3645 nl_msg_put_unspec(ctx->odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3646 &vlan, sizeof vlan);
3648 base->vlan_tci = new_tci;
3652 commit_set_nw_action(const struct flow *flow, struct flow *base,
3653 struct ofpbuf *odp_actions)
3655 struct ovs_key_ipv4 ipv4_key;
3657 if (base->dl_type != htons(ETH_TYPE_IP) ||
3658 !base->nw_src || !base->nw_dst) {
3662 if (base->nw_src == flow->nw_src &&
3663 base->nw_dst == flow->nw_dst &&
3664 base->nw_tos == flow->nw_tos &&
3665 base->nw_ttl == flow->nw_ttl &&
3666 base->nw_frag == flow->nw_frag) {
3670 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3671 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3672 ipv4_key.ipv4_proto = base->nw_proto;
3673 ipv4_key.ipv4_tos = flow->nw_tos;
3674 ipv4_key.ipv4_ttl = flow->nw_ttl;
3675 ipv4_key.ipv4_frag = (base->nw_frag == 0 ? OVS_FRAG_TYPE_NONE
3676 : base->nw_frag == FLOW_NW_FRAG_ANY
3677 ? OVS_FRAG_TYPE_FIRST : OVS_FRAG_TYPE_LATER);
3679 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3680 &ipv4_key, sizeof(ipv4_key));
3684 commit_set_port_action(const struct flow *flow, struct flow *base,
3685 struct ofpbuf *odp_actions)
3687 if (!base->tp_src || !base->tp_dst) {
3691 if (base->tp_src == flow->tp_src &&
3692 base->tp_dst == flow->tp_dst) {
3696 if (flow->nw_proto == IPPROTO_TCP) {
3697 struct ovs_key_tcp port_key;
3699 port_key.tcp_src = base->tp_src = flow->tp_src;
3700 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3702 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3703 &port_key, sizeof(port_key));
3705 } else if (flow->nw_proto == IPPROTO_UDP) {
3706 struct ovs_key_udp port_key;
3708 port_key.udp_src = base->tp_src = flow->tp_src;
3709 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3711 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3712 &port_key, sizeof(port_key));
3717 commit_set_priority_action(const struct flow *flow, struct flow *base,
3718 struct ofpbuf *odp_actions)
3720 if (base->priority == flow->priority) {
3723 base->priority = flow->priority;
3725 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3726 &base->priority, sizeof(base->priority));
3730 commit_odp_actions(struct action_xlate_ctx *ctx)
3732 const struct flow *flow = &ctx->flow;
3733 struct flow *base = &ctx->base_flow;
3734 struct ofpbuf *odp_actions = ctx->odp_actions;
3736 commit_set_tun_id_action(flow, base, odp_actions);
3737 commit_set_ether_addr_action(flow, base, odp_actions);
3738 commit_vlan_action(ctx, flow->vlan_tci);
3739 commit_set_nw_action(flow, base, odp_actions);
3740 commit_set_port_action(flow, base, odp_actions);
3741 commit_set_priority_action(flow, base, odp_actions);
3745 compose_output_action(struct action_xlate_ctx *ctx, uint16_t odp_port)
3747 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3748 ctx->sflow_odp_port = odp_port;
3749 ctx->sflow_n_outputs++;
3753 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
3755 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
3756 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
3759 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
3760 || !stp_forward_in_state(ofport->stp_state)) {
3761 /* Forwarding disabled on port. */
3766 * We don't have an ofport record for this port, but it doesn't hurt to
3767 * allow forwarding to it anyhow. Maybe such a port will appear later
3768 * and we're pre-populating the flow table.
3772 commit_odp_actions(ctx);
3773 compose_output_action(ctx, odp_port);
3774 ctx->nf_output_iface = ofp_port;
3778 xlate_table_action(struct action_xlate_ctx *ctx,
3779 uint16_t in_port, uint8_t table_id)
3781 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3782 struct ofproto_dpif *ofproto = ctx->ofproto;
3783 struct rule_dpif *rule;
3784 uint16_t old_in_port;
3785 uint8_t old_table_id;
3787 old_table_id = ctx->table_id;
3788 ctx->table_id = table_id;
3790 /* Look up a flow with 'in_port' as the input port. */
3791 old_in_port = ctx->flow.in_port;
3792 ctx->flow.in_port = in_port;
3793 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3796 if (table_id > 0 && table_id < N_TABLES) {
3797 struct table_dpif *table = &ofproto->tables[table_id];
3798 if (table->other_table) {
3801 : rule_calculate_tag(&ctx->flow,
3802 &table->other_table->wc,
3807 /* Restore the original input port. Otherwise OFPP_NORMAL and
3808 * OFPP_IN_PORT will have surprising behavior. */
3809 ctx->flow.in_port = old_in_port;
3811 if (ctx->resubmit_hook) {
3812 ctx->resubmit_hook(ctx, rule);
3817 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3821 ctx->table_id = old_table_id;
3823 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3825 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3826 MAX_RESUBMIT_RECURSION);
3831 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3832 const struct nx_action_resubmit *nar)
3837 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3839 : ntohs(nar->in_port));
3840 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3842 xlate_table_action(ctx, in_port, table_id);
3846 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3848 struct ofport_dpif *ofport;
3850 commit_odp_actions(ctx);
3851 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3852 uint16_t ofp_port = ofport->up.ofp_port;
3853 if (ofp_port != ctx->flow.in_port
3854 && !(ofport->up.opp.config & mask)
3855 && stp_forward_in_state(ofport->stp_state)) {
3856 compose_output_action(ctx, ofport->odp_port);
3860 ctx->nf_output_iface = NF_OUT_FLOOD;
3864 compose_controller_action(struct action_xlate_ctx *ctx, int len)
3866 struct user_action_cookie cookie;
3868 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
3870 cookie.n_output = 0;
3871 cookie.vlan_tci = 0;
3872 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
3876 xlate_output_action__(struct action_xlate_ctx *ctx,
3877 uint16_t port, uint16_t max_len)
3879 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3881 ctx->nf_output_iface = NF_OUT_DROP;
3885 add_output_action(ctx, ctx->flow.in_port);
3888 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3894 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3897 flood_packets(ctx, htonl(0));
3899 case OFPP_CONTROLLER:
3900 commit_odp_actions(ctx);
3901 compose_controller_action(ctx, max_len);
3904 add_output_action(ctx, OFPP_LOCAL);
3909 if (port != ctx->flow.in_port) {
3910 add_output_action(ctx, port);
3915 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3916 ctx->nf_output_iface = NF_OUT_FLOOD;
3917 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3918 ctx->nf_output_iface = prev_nf_output_iface;
3919 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3920 ctx->nf_output_iface != NF_OUT_FLOOD) {
3921 ctx->nf_output_iface = NF_OUT_MULTI;
3926 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3927 const struct nx_action_output_reg *naor)
3931 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3933 if (ofp_port <= UINT16_MAX) {
3934 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3939 xlate_output_action(struct action_xlate_ctx *ctx,
3940 const struct ofp_action_output *oao)
3942 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3946 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3947 const struct ofp_action_enqueue *oae)
3949 uint16_t ofp_port, odp_port;
3950 uint32_t flow_priority, priority;
3953 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3956 /* Fall back to ordinary output action. */
3957 xlate_output_action__(ctx, ntohs(oae->port), 0);
3961 /* Figure out datapath output port. */
3962 ofp_port = ntohs(oae->port);
3963 if (ofp_port == OFPP_IN_PORT) {
3964 ofp_port = ctx->flow.in_port;
3965 } else if (ofp_port == ctx->flow.in_port) {
3968 odp_port = ofp_port_to_odp_port(ofp_port);
3970 /* Add datapath actions. */
3971 flow_priority = ctx->flow.priority;
3972 ctx->flow.priority = priority;
3973 add_output_action(ctx, odp_port);
3974 ctx->flow.priority = flow_priority;
3976 /* Update NetFlow output port. */
3977 if (ctx->nf_output_iface == NF_OUT_DROP) {
3978 ctx->nf_output_iface = odp_port;
3979 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3980 ctx->nf_output_iface = NF_OUT_MULTI;
3985 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3986 const struct nx_action_set_queue *nasq)
3991 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3994 /* Couldn't translate queue to a priority, so ignore. A warning
3995 * has already been logged. */
3999 ctx->flow.priority = priority;
4002 struct xlate_reg_state {
4008 xlate_autopath(struct action_xlate_ctx *ctx,
4009 const struct nx_action_autopath *naa)
4011 uint16_t ofp_port = ntohl(naa->id);
4012 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4014 if (!port || !port->bundle) {
4015 ofp_port = OFPP_NONE;
4016 } else if (port->bundle->bond) {
4017 /* Autopath does not support VLAN hashing. */
4018 struct ofport_dpif *slave = bond_choose_output_slave(
4019 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4021 ofp_port = slave->up.ofp_port;
4024 autopath_execute(naa, &ctx->flow, ofp_port);
4028 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4030 struct ofproto_dpif *ofproto = ofproto_;
4031 struct ofport_dpif *port;
4041 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4044 port = get_ofp_port(ofproto, ofp_port);
4045 return port ? port->may_enable : false;
4050 xlate_learn_action(struct action_xlate_ctx *ctx,
4051 const struct nx_action_learn *learn)
4053 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4054 struct ofputil_flow_mod fm;
4057 learn_execute(learn, &ctx->flow, &fm);
4059 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4060 if (error && !VLOG_DROP_WARN(&rl)) {
4061 char *msg = ofputil_error_to_string(error);
4062 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4070 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4072 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4073 ? htonl(OFPPC_NO_RECV_STP)
4074 : htonl(OFPPC_NO_RECV))) {
4078 /* Only drop packets here if both forwarding and learning are
4079 * disabled. If just learning is enabled, we need to have
4080 * OFPP_NORMAL and the learning action have a look at the packet
4081 * before we can drop it. */
4082 if (!stp_forward_in_state(port->stp_state)
4083 && !stp_learn_in_state(port->stp_state)) {
4091 do_xlate_actions(const union ofp_action *in, size_t n_in,
4092 struct action_xlate_ctx *ctx)
4094 const struct ofport_dpif *port;
4095 const union ofp_action *ia;
4098 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4099 if (port && !may_receive(port, ctx)) {
4100 /* Drop this flow. */
4104 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4105 const struct ofp_action_dl_addr *oada;
4106 const struct nx_action_resubmit *nar;
4107 const struct nx_action_set_tunnel *nast;
4108 const struct nx_action_set_queue *nasq;
4109 const struct nx_action_multipath *nam;
4110 const struct nx_action_autopath *naa;
4111 const struct nx_action_bundle *nab;
4112 const struct nx_action_output_reg *naor;
4113 enum ofputil_action_code code;
4120 code = ofputil_decode_action_unsafe(ia);
4122 case OFPUTIL_OFPAT_OUTPUT:
4123 xlate_output_action(ctx, &ia->output);
4126 case OFPUTIL_OFPAT_SET_VLAN_VID:
4127 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4128 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4131 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4132 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4133 ctx->flow.vlan_tci |= htons(
4134 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4137 case OFPUTIL_OFPAT_STRIP_VLAN:
4138 ctx->flow.vlan_tci = htons(0);
4141 case OFPUTIL_OFPAT_SET_DL_SRC:
4142 oada = ((struct ofp_action_dl_addr *) ia);
4143 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4146 case OFPUTIL_OFPAT_SET_DL_DST:
4147 oada = ((struct ofp_action_dl_addr *) ia);
4148 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4151 case OFPUTIL_OFPAT_SET_NW_SRC:
4152 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4155 case OFPUTIL_OFPAT_SET_NW_DST:
4156 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4159 case OFPUTIL_OFPAT_SET_NW_TOS:
4160 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4161 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4164 case OFPUTIL_OFPAT_SET_TP_SRC:
4165 ctx->flow.tp_src = ia->tp_port.tp_port;
4168 case OFPUTIL_OFPAT_SET_TP_DST:
4169 ctx->flow.tp_dst = ia->tp_port.tp_port;
4172 case OFPUTIL_OFPAT_ENQUEUE:
4173 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4176 case OFPUTIL_NXAST_RESUBMIT:
4177 nar = (const struct nx_action_resubmit *) ia;
4178 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4181 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4182 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4185 case OFPUTIL_NXAST_SET_TUNNEL:
4186 nast = (const struct nx_action_set_tunnel *) ia;
4187 tun_id = htonll(ntohl(nast->tun_id));
4188 ctx->flow.tun_id = tun_id;
4191 case OFPUTIL_NXAST_SET_QUEUE:
4192 nasq = (const struct nx_action_set_queue *) ia;
4193 xlate_set_queue_action(ctx, nasq);
4196 case OFPUTIL_NXAST_POP_QUEUE:
4197 ctx->flow.priority = ctx->original_priority;
4200 case OFPUTIL_NXAST_REG_MOVE:
4201 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4205 case OFPUTIL_NXAST_REG_LOAD:
4206 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4210 case OFPUTIL_NXAST_NOTE:
4211 /* Nothing to do. */
4214 case OFPUTIL_NXAST_SET_TUNNEL64:
4215 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4216 ctx->flow.tun_id = tun_id;
4219 case OFPUTIL_NXAST_MULTIPATH:
4220 nam = (const struct nx_action_multipath *) ia;
4221 multipath_execute(nam, &ctx->flow);
4224 case OFPUTIL_NXAST_AUTOPATH:
4225 naa = (const struct nx_action_autopath *) ia;
4226 xlate_autopath(ctx, naa);
4229 case OFPUTIL_NXAST_BUNDLE:
4230 ctx->ofproto->has_bundle_action = true;
4231 nab = (const struct nx_action_bundle *) ia;
4232 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4237 case OFPUTIL_NXAST_BUNDLE_LOAD:
4238 ctx->ofproto->has_bundle_action = true;
4239 nab = (const struct nx_action_bundle *) ia;
4240 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4244 case OFPUTIL_NXAST_OUTPUT_REG:
4245 naor = (const struct nx_action_output_reg *) ia;
4246 xlate_output_reg_action(ctx, naor);
4249 case OFPUTIL_NXAST_LEARN:
4250 ctx->has_learn = true;
4251 if (ctx->may_learn) {
4252 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4256 case OFPUTIL_NXAST_EXIT:
4262 /* We've let OFPP_NORMAL and the learning action look at the packet,
4263 * so drop it now if forwarding is disabled. */
4264 if (port && !stp_forward_in_state(port->stp_state)) {
4265 ofpbuf_clear(ctx->odp_actions);
4266 add_sflow_action(ctx);
4271 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4272 struct ofproto_dpif *ofproto, const struct flow *flow,
4273 const struct ofpbuf *packet)
4275 ctx->ofproto = ofproto;
4277 ctx->packet = packet;
4278 ctx->may_learn = packet != NULL;
4279 ctx->resubmit_hook = NULL;
4282 static struct ofpbuf *
4283 xlate_actions(struct action_xlate_ctx *ctx,
4284 const union ofp_action *in, size_t n_in)
4286 COVERAGE_INC(ofproto_dpif_xlate);
4288 ctx->odp_actions = ofpbuf_new(512);
4289 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4291 ctx->may_set_up_flow = true;
4292 ctx->has_learn = false;
4293 ctx->has_normal = false;
4294 ctx->nf_output_iface = NF_OUT_DROP;
4296 ctx->original_priority = ctx->flow.priority;
4297 ctx->base_flow = ctx->flow;
4298 ctx->base_flow.tun_id = 0;
4302 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4303 switch (ctx->ofproto->up.frag_handling) {
4304 case OFPC_FRAG_NORMAL:
4305 /* We must pretend that transport ports are unavailable. */
4306 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4307 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4310 case OFPC_FRAG_DROP:
4311 return ctx->odp_actions;
4313 case OFPC_FRAG_REASM:
4316 case OFPC_FRAG_NX_MATCH:
4317 /* Nothing to do. */
4322 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4323 ctx->may_set_up_flow = false;
4324 return ctx->odp_actions;
4326 add_sflow_action(ctx);
4327 do_xlate_actions(in, n_in, ctx);
4329 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4330 ctx->odp_actions->data,
4331 ctx->odp_actions->size)) {
4332 ctx->may_set_up_flow = false;
4334 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4336 compose_output_action(ctx, OVSP_LOCAL);
4339 fix_sflow_action(ctx);
4342 return ctx->odp_actions;
4345 /* OFPP_NORMAL implementation. */
4348 struct ofport_dpif *port;
4353 struct dst builtin[32];
4355 size_t n, allocated;
4358 static void dst_set_init(struct dst_set *);
4359 static void dst_set_add(struct dst_set *, const struct dst *);
4360 static void dst_set_free(struct dst_set *);
4362 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4364 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4365 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4366 * the bundle on which the packet was received, returns the VLAN to which the
4369 * Both 'vid' and the return value are in the range 0...4095. */
4371 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4373 switch (in_bundle->vlan_mode) {
4374 case PORT_VLAN_ACCESS:
4375 return in_bundle->vlan;
4378 case PORT_VLAN_TRUNK:
4381 case PORT_VLAN_NATIVE_UNTAGGED:
4382 case PORT_VLAN_NATIVE_TAGGED:
4383 return vid ? vid : in_bundle->vlan;
4390 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4391 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4394 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4395 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4398 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4400 switch (in_bundle->vlan_mode) {
4401 case PORT_VLAN_ACCESS:
4404 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4405 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4406 "packet received on port %s configured as VLAN "
4407 "%"PRIu16" access port",
4408 in_bundle->ofproto->up.name, vid,
4409 in_bundle->name, in_bundle->vlan);
4415 case PORT_VLAN_NATIVE_UNTAGGED:
4416 case PORT_VLAN_NATIVE_TAGGED:
4418 /* Port must always carry its native VLAN. */
4422 case PORT_VLAN_TRUNK:
4423 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4425 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4426 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4427 "received on port %s not configured for trunking "
4429 in_bundle->ofproto->up.name, vid,
4430 in_bundle->name, vid);
4442 /* Given 'vlan', the VLAN that a packet belongs to, and
4443 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4444 * that should be included in the 802.1Q header. (If the return value is 0,
4445 * then the 802.1Q header should only be included in the packet if there is a
4448 * Both 'vlan' and the return value are in the range 0...4095. */
4450 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4452 switch (out_bundle->vlan_mode) {
4453 case PORT_VLAN_ACCESS:
4456 case PORT_VLAN_TRUNK:
4457 case PORT_VLAN_NATIVE_TAGGED:
4460 case PORT_VLAN_NATIVE_UNTAGGED:
4461 return vlan == out_bundle->vlan ? 0 : vlan;
4469 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
4470 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
4474 vlan = input_vid_to_vlan(in_bundle, vlan_tci_to_vid(ctx->flow.vlan_tci));
4475 dst->vid = output_vlan_to_vid(out_bundle, vlan);
4477 dst->port = (!out_bundle->bond
4478 ? ofbundle_get_a_port(out_bundle)
4479 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4480 dst->vid, &ctx->tags));
4481 return dst->port != NULL;
4485 mirror_mask_ffs(mirror_mask_t mask)
4487 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4492 dst_set_init(struct dst_set *set)
4494 set->dsts = set->builtin;
4496 set->allocated = ARRAY_SIZE(set->builtin);
4500 dst_set_add(struct dst_set *set, const struct dst *dst)
4502 if (set->n >= set->allocated) {
4503 size_t new_allocated;
4504 struct dst *new_dsts;
4506 new_allocated = set->allocated * 2;
4507 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
4508 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
4512 set->dsts = new_dsts;
4513 set->allocated = new_allocated;
4515 set->dsts[set->n++] = *dst;
4519 dst_set_free(struct dst_set *set)
4521 if (set->dsts != set->builtin) {
4527 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
4530 for (i = 0; i < set->n; i++) {
4531 if (set->dsts[i].vid == test->vid
4532 && set->dsts[i].port == test->port) {
4540 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4542 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4543 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4547 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4549 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4552 /* Returns an arbitrary interface within 'bundle'. */
4553 static struct ofport_dpif *
4554 ofbundle_get_a_port(const struct ofbundle *bundle)
4556 return CONTAINER_OF(list_front(&bundle->ports),
4557 struct ofport_dpif, bundle_node);
4561 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4562 const struct ofbundle *in_bundle,
4563 const struct ofbundle *out_bundle, struct dst_set *set)
4567 if (out_bundle == OFBUNDLE_FLOOD) {
4568 struct ofbundle *bundle;
4570 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4571 if (bundle != in_bundle
4572 && ofbundle_includes_vlan(bundle, vlan)
4573 && bundle->floodable
4574 && !bundle->mirror_out
4575 && set_dst(ctx, &dst, in_bundle, bundle)) {
4576 dst_set_add(set, &dst);
4579 ctx->nf_output_iface = NF_OUT_FLOOD;
4580 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
4581 dst_set_add(set, &dst);
4582 ctx->nf_output_iface = dst.port->odp_port;
4587 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4589 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4592 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4593 * to a VLAN. In general most packets may be mirrored but we want to drop
4594 * protocols that may confuse switches. */
4596 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4598 /* If you change this function's behavior, please update corresponding
4599 * documentation in vswitch.xml at the same time. */
4600 if (dst[0] != 0x01) {
4601 /* All the currently banned MACs happen to start with 01 currently, so
4602 * this is a quick way to eliminate most of the good ones. */
4604 if (eth_addr_is_reserved(dst)) {
4605 /* Drop STP, IEEE pause frames, and other reserved protocols
4606 * (01-80-c2-00-00-0x). */
4610 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4612 if ((dst[3] & 0xfe) == 0xcc &&
4613 (dst[4] & 0xfe) == 0xcc &&
4614 (dst[5] & 0xfe) == 0xcc) {
4615 /* Drop the following protocols plus others following the same
4618 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4619 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4620 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4624 if (!(dst[3] | dst[4] | dst[5])) {
4625 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4634 compose_mirror_dsts(struct action_xlate_ctx *ctx,
4635 uint16_t vlan, const struct ofbundle *in_bundle,
4636 struct dst_set *set)
4638 struct ofproto_dpif *ofproto = ctx->ofproto;
4639 mirror_mask_t mirrors;
4643 mirrors = in_bundle->src_mirrors;
4644 for (i = 0; i < set->n; i++) {
4645 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
4652 flow_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4653 for (; mirrors; mirrors &= mirrors - 1) {
4654 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4655 if (vlan_is_mirrored(m, vlan)) {
4659 if (set_dst(ctx, &dst, in_bundle, m->out)
4660 && !dst_is_duplicate(set, &dst)) {
4661 dst_set_add(set, &dst);
4663 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
4664 struct ofbundle *bundle;
4666 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4667 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4668 && !bundle->mirror_out
4669 && set_dst(ctx, &dst, in_bundle, bundle))
4671 /* set_dst() got dst->vid from the input packet's VLAN,
4672 * not from m->out_vlan, so recompute it. */
4673 dst.vid = output_vlan_to_vid(bundle, m->out_vlan);
4675 if (dst_is_duplicate(set, &dst)) {
4679 if (bundle == in_bundle && dst.vid == flow_vid) {
4680 /* Don't send out input port on same VLAN. */
4683 dst_set_add(set, &dst);
4692 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
4693 const struct ofbundle *in_bundle,
4694 const struct ofbundle *out_bundle)
4696 uint16_t initial_vid, cur_vid;
4697 const struct dst *dst;
4701 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
4702 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
4708 /* Output all the packets we can without having to change the VLAN. */
4709 commit_odp_actions(ctx);
4710 initial_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4711 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4712 if (dst->vid != initial_vid) {
4715 compose_output_action(ctx, dst->port->odp_port);
4718 /* Then output the rest. */
4719 cur_vid = initial_vid;
4720 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4721 if (dst->vid == initial_vid) {
4724 if (dst->vid != cur_vid) {
4727 tci = htons(dst->vid);
4728 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4730 tci |= htons(VLAN_CFI);
4732 commit_vlan_action(ctx, tci);
4736 compose_output_action(ctx, dst->port->odp_port);
4742 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4743 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4744 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4746 is_gratuitous_arp(const struct flow *flow)
4748 return (flow->dl_type == htons(ETH_TYPE_ARP)
4749 && eth_addr_is_broadcast(flow->dl_dst)
4750 && (flow->nw_proto == ARP_OP_REPLY
4751 || (flow->nw_proto == ARP_OP_REQUEST
4752 && flow->nw_src == flow->nw_dst)));
4756 update_learning_table(struct ofproto_dpif *ofproto,
4757 const struct flow *flow, int vlan,
4758 struct ofbundle *in_bundle)
4760 struct mac_entry *mac;
4762 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4766 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4767 if (is_gratuitous_arp(flow)) {
4768 /* We don't want to learn from gratuitous ARP packets that are
4769 * reflected back over bond slaves so we lock the learning table. */
4770 if (!in_bundle->bond) {
4771 mac_entry_set_grat_arp_lock(mac);
4772 } else if (mac_entry_is_grat_arp_locked(mac)) {
4777 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4778 /* The log messages here could actually be useful in debugging,
4779 * so keep the rate limit relatively high. */
4780 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4781 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4782 "on port %s in VLAN %d",
4783 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4784 in_bundle->name, vlan);
4786 mac->port.p = in_bundle;
4787 tag_set_add(&ofproto->revalidate_set,
4788 mac_learning_changed(ofproto->ml, mac));
4792 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
4793 * dropped. Returns true if they may be forwarded, false if they should be
4796 * If 'have_packet' is true, it indicates that the caller is processing a
4797 * received packet. If 'have_packet' is false, then the caller is just
4798 * revalidating an existing flow because configuration has changed. Either
4799 * way, 'have_packet' only affects logging (there is no point in logging errors
4800 * during revalidation).
4802 * Sets '*in_bundlep' to the input bundle. This will be a null pointer if
4803 * flow->in_port does not designate a known input port (in which case
4804 * is_admissible() returns false).
4806 * When returning true, sets '*vlanp' to the effective VLAN of the input
4807 * packet, as returned by input_vid_to_vlan().
4809 * May also add tags to '*tags', although the current implementation only does
4810 * so in one special case.
4813 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
4815 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
4817 struct ofport_dpif *in_port;
4818 struct ofbundle *in_bundle;
4824 /* Find the port and bundle for the received packet. */
4825 in_port = get_ofp_port(ofproto, flow->in_port);
4826 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
4827 if (!in_port || !in_bundle) {
4828 /* No interface? Something fishy... */
4830 /* Odd. A few possible reasons here:
4832 * - We deleted a port but there are still a few packets queued up
4835 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4836 * we don't know about.
4838 * - Packet arrived on the local port but the local port is not
4841 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4843 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4845 ofproto->up.name, flow->in_port);
4850 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
4851 !(flow->vlan_tci & htons(VLAN_CFI))) {
4852 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4853 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
4854 "VLAN tag received on port %s",
4855 ofproto->up.name, in_bundle->name);
4859 vid = vlan_tci_to_vid(flow->vlan_tci);
4860 if (!input_vid_is_valid(vid, in_bundle, have_packet)) {
4863 *vlanp = vlan = input_vid_to_vlan(in_bundle, vid);
4865 /* Drop frames for reserved multicast addresses only if forward_bpdu
4866 * option is absent. */
4867 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
4871 /* Drop frames on bundles reserved for mirroring. */
4872 if (in_bundle->mirror_out) {
4874 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4875 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4876 "%s, which is reserved exclusively for mirroring",
4877 ofproto->up.name, in_bundle->name);
4882 if (in_bundle->bond) {
4883 struct mac_entry *mac;
4885 switch (bond_check_admissibility(in_bundle->bond, in_port,
4886 flow->dl_dst, tags)) {
4893 case BV_DROP_IF_MOVED:
4894 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
4895 if (mac && mac->port.p != in_bundle &&
4896 (!is_gratuitous_arp(flow)
4897 || mac_entry_is_grat_arp_locked(mac))) {
4908 xlate_normal(struct action_xlate_ctx *ctx)
4910 struct ofbundle *in_bundle;
4911 struct ofbundle *out_bundle;
4912 struct mac_entry *mac;
4915 ctx->has_normal = true;
4917 /* Check whether we should drop packets in this flow. */
4918 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
4919 &ctx->tags, &vlan, &in_bundle)) {
4924 /* Learn source MAC. */
4925 if (ctx->may_learn) {
4926 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
4929 /* Determine output bundle. */
4930 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
4933 out_bundle = mac->port.p;
4934 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4935 /* If we are revalidating but don't have a learning entry then eject
4936 * the flow. Installing a flow that floods packets opens up a window
4937 * of time where we could learn from a packet reflected on a bond and
4938 * blackhole packets before the learning table is updated to reflect
4939 * the correct port. */
4940 ctx->may_set_up_flow = false;
4943 out_bundle = OFBUNDLE_FLOOD;
4946 /* Don't send packets out their input bundles. */
4947 if (in_bundle == out_bundle) {
4953 compose_actions(ctx, vlan, in_bundle, out_bundle);
4957 /* Optimized flow revalidation.
4959 * It's a difficult problem, in general, to tell which facets need to have
4960 * their actions recalculated whenever the OpenFlow flow table changes. We
4961 * don't try to solve that general problem: for most kinds of OpenFlow flow
4962 * table changes, we recalculate the actions for every facet. This is
4963 * relatively expensive, but it's good enough if the OpenFlow flow table
4964 * doesn't change very often.
4966 * However, we can expect one particular kind of OpenFlow flow table change to
4967 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4968 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4969 * table, we add a special case that applies to flow tables in which every rule
4970 * has the same form (that is, the same wildcards), except that the table is
4971 * also allowed to have a single "catch-all" flow that matches all packets. We
4972 * optimize this case by tagging all of the facets that resubmit into the table
4973 * and invalidating the same tag whenever a flow changes in that table. The
4974 * end result is that we revalidate just the facets that need it (and sometimes
4975 * a few more, but not all of the facets or even all of the facets that
4976 * resubmit to the table modified by MAC learning). */
4978 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
4979 * into an OpenFlow table with the given 'basis'. */
4981 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
4984 if (flow_wildcards_is_catchall(wc)) {
4987 struct flow tag_flow = *flow;
4988 flow_zero_wildcards(&tag_flow, wc);
4989 return tag_create_deterministic(flow_hash(&tag_flow, secret));
4993 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
4994 * taggability of that table.
4996 * This function must be called after *each* change to a flow table. If you
4997 * skip calling it on some changes then the pointer comparisons at the end can
4998 * be invalid if you get unlucky. For example, if a flow removal causes a
4999 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5000 * different wildcards to be created with the same address, then this function
5001 * will incorrectly skip revalidation. */
5003 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5005 struct table_dpif *table = &ofproto->tables[table_id];
5006 const struct classifier *cls = &ofproto->up.tables[table_id];
5007 struct cls_table *catchall, *other;
5008 struct cls_table *t;
5010 catchall = other = NULL;
5012 switch (hmap_count(&cls->tables)) {
5014 /* We could tag this OpenFlow table but it would make the logic a
5015 * little harder and it's a corner case that doesn't seem worth it
5021 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5022 if (cls_table_is_catchall(t)) {
5024 } else if (!other) {
5027 /* Indicate that we can't tag this by setting both tables to
5028 * NULL. (We know that 'catchall' is already NULL.) */
5035 /* Can't tag this table. */
5039 if (table->catchall_table != catchall || table->other_table != other) {
5040 table->catchall_table = catchall;
5041 table->other_table = other;
5042 ofproto->need_revalidate = true;
5046 /* Given 'rule' that has changed in some way (either it is a rule being
5047 * inserted, a rule being deleted, or a rule whose actions are being
5048 * modified), marks facets for revalidation to ensure that packets will be
5049 * forwarded correctly according to the new state of the flow table.
5051 * This function must be called after *each* change to a flow table. See
5052 * the comment on table_update_taggable() for more information. */
5054 rule_invalidate(const struct rule_dpif *rule)
5056 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5058 table_update_taggable(ofproto, rule->up.table_id);
5060 if (!ofproto->need_revalidate) {
5061 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5063 if (table->other_table && rule->tag) {
5064 tag_set_add(&ofproto->revalidate_set, rule->tag);
5066 ofproto->need_revalidate = true;
5072 set_frag_handling(struct ofproto *ofproto_,
5073 enum ofp_config_flags frag_handling)
5075 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5077 if (frag_handling != OFPC_FRAG_REASM) {
5078 ofproto->need_revalidate = true;
5086 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5087 const struct flow *flow,
5088 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5090 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5093 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5094 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5097 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5098 ofproto->max_ports);
5100 struct odputil_keybuf keybuf;
5101 struct action_xlate_ctx ctx;
5102 struct ofpbuf *odp_actions;
5105 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5106 odp_flow_key_from_flow(&key, flow);
5108 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
5109 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5110 dpif_execute(ofproto->dpif, key.data, key.size,
5111 odp_actions->data, odp_actions->size, packet);
5112 ofpbuf_delete(odp_actions);
5118 get_netflow_ids(const struct ofproto *ofproto_,
5119 uint8_t *engine_type, uint8_t *engine_id)
5121 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5123 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5126 static struct ofproto_dpif *
5127 ofproto_dpif_lookup(const char *name)
5129 struct ofproto *ofproto = ofproto_lookup(name);
5130 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5131 ? ofproto_dpif_cast(ofproto)
5136 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5137 const char *args, void *aux OVS_UNUSED)
5139 const struct ofproto_dpif *ofproto;
5141 ofproto = ofproto_dpif_lookup(args);
5143 unixctl_command_reply(conn, 501, "no such bridge");
5146 mac_learning_flush(ofproto->ml);
5148 unixctl_command_reply(conn, 200, "table successfully flushed");
5152 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5153 const char *args, void *aux OVS_UNUSED)
5155 struct ds ds = DS_EMPTY_INITIALIZER;
5156 const struct ofproto_dpif *ofproto;
5157 const struct mac_entry *e;
5159 ofproto = ofproto_dpif_lookup(args);
5161 unixctl_command_reply(conn, 501, "no such bridge");
5165 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5166 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5167 struct ofbundle *bundle = e->port.p;
5168 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5169 ofbundle_get_a_port(bundle)->odp_port,
5170 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5172 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5176 struct ofproto_trace {
5177 struct action_xlate_ctx ctx;
5183 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5184 const struct rule_dpif *rule)
5186 ds_put_char_multiple(result, '\t', level);
5188 ds_put_cstr(result, "No match\n");
5192 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5193 table_id, ntohll(rule->up.flow_cookie));
5194 cls_rule_format(&rule->up.cr, result);
5195 ds_put_char(result, '\n');
5197 ds_put_char_multiple(result, '\t', level);
5198 ds_put_cstr(result, "OpenFlow ");
5199 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5200 ds_put_char(result, '\n');
5204 trace_format_flow(struct ds *result, int level, const char *title,
5205 struct ofproto_trace *trace)
5207 ds_put_char_multiple(result, '\t', level);
5208 ds_put_format(result, "%s: ", title);
5209 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5210 ds_put_cstr(result, "unchanged");
5212 flow_format(result, &trace->ctx.flow);
5213 trace->flow = trace->ctx.flow;
5215 ds_put_char(result, '\n');
5219 trace_format_regs(struct ds *result, int level, const char *title,
5220 struct ofproto_trace *trace)
5224 ds_put_char_multiple(result, '\t', level);
5225 ds_put_format(result, "%s:", title);
5226 for (i = 0; i < FLOW_N_REGS; i++) {
5227 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5229 ds_put_char(result, '\n');
5233 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5235 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5236 struct ds *result = trace->result;
5238 ds_put_char(result, '\n');
5239 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5240 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5241 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5245 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5246 void *aux OVS_UNUSED)
5248 char *dpname, *arg1, *arg2, *arg3, *arg4;
5249 char *args = xstrdup(args_);
5250 char *save_ptr = NULL;
5251 struct ofproto_dpif *ofproto;
5252 struct ofpbuf odp_key;
5253 struct ofpbuf *packet;
5254 struct rule_dpif *rule;
5260 ofpbuf_init(&odp_key, 0);
5263 dpname = strtok_r(args, " ", &save_ptr);
5264 arg1 = strtok_r(NULL, " ", &save_ptr);
5265 arg2 = strtok_r(NULL, " ", &save_ptr);
5266 arg3 = strtok_r(NULL, " ", &save_ptr);
5267 arg4 = 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 && arg4) {
5293 /* ofproto/trace dpname priority tun_id in_port packet */
5298 priority = atoi(arg1);
5299 tun_id = htonll(strtoull(arg2, NULL, 0));
5300 in_port = ofp_port_to_odp_port(atoi(arg3));
5302 packet = ofpbuf_new(strlen(args) / 2);
5303 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5304 arg4 += strspn(arg4, " ");
5305 if (*arg4 != '\0') {
5306 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5309 if (packet->size < ETH_HEADER_LEN) {
5310 unixctl_command_reply(conn, 501,
5311 "Packet data too short for Ethernet");
5315 ds_put_cstr(&result, "Packet: ");
5316 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5317 ds_put_cstr(&result, s);
5320 flow_extract(packet, priority, tun_id, in_port, &flow);
5322 unixctl_command_reply(conn, 501, "Bad command syntax");
5326 ofproto = ofproto_dpif_lookup(dpname);
5328 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5333 ds_put_cstr(&result, "Flow: ");
5334 flow_format(&result, &flow);
5335 ds_put_char(&result, '\n');
5337 rule = rule_dpif_lookup(ofproto, &flow, 0);
5338 trace_format_rule(&result, 0, 0, rule);
5340 struct ofproto_trace trace;
5341 struct ofpbuf *odp_actions;
5343 trace.result = &result;
5345 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5346 trace.ctx.resubmit_hook = trace_resubmit;
5347 odp_actions = xlate_actions(&trace.ctx,
5348 rule->up.actions, rule->up.n_actions);
5350 ds_put_char(&result, '\n');
5351 trace_format_flow(&result, 0, "Final flow", &trace);
5352 ds_put_cstr(&result, "Datapath actions: ");
5353 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5354 ofpbuf_delete(odp_actions);
5356 if (!trace.ctx.may_set_up_flow) {
5358 ds_put_cstr(&result, "\nThis flow is not cachable.");
5360 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5361 "for complete actions, please supply a packet.");
5366 unixctl_command_reply(conn, 200, ds_cstr(&result));
5369 ds_destroy(&result);
5370 ofpbuf_delete(packet);
5371 ofpbuf_uninit(&odp_key);
5376 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5377 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5380 unixctl_command_reply(conn, 200, NULL);
5384 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5385 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5388 unixctl_command_reply(conn, 200, NULL);
5392 ofproto_dpif_unixctl_init(void)
5394 static bool registered;
5400 unixctl_command_register("ofproto/trace",
5401 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5402 ofproto_unixctl_trace, NULL);
5403 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5405 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5407 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5408 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5411 const struct ofproto_class ofproto_dpif_class = {
5438 port_is_lacp_current,
5439 NULL, /* rule_choose_table */
5446 rule_modify_actions,
5454 get_cfm_remote_mpids,
5458 get_stp_port_status,
5463 is_mirror_output_bundle,
5464 forward_bpdu_changed,