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 (exactly one of out == NULL and out_vlan == -1 is true). */
125 struct ofbundle *out; /* Output port or NULL. */
126 int out_vlan; /* Output VLAN or -1. */
127 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
130 static void mirror_destroy(struct ofmirror *);
132 /* A group of one or more OpenFlow ports. */
133 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
135 struct ofproto_dpif *ofproto; /* Owning ofproto. */
136 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
137 void *aux; /* Key supplied by ofproto's client. */
138 char *name; /* Identifier for log messages. */
141 struct list ports; /* Contains "struct ofport"s. */
142 enum port_vlan_mode vlan_mode; /* VLAN mode */
143 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
144 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
145 * NULL if all VLANs are trunked. */
146 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
147 struct bond *bond; /* Nonnull iff more than one port. */
148 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
151 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
153 /* Port mirroring info. */
154 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
155 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
156 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
159 static void bundle_remove(struct ofport *);
160 static void bundle_update(struct ofbundle *);
161 static void bundle_destroy(struct ofbundle *);
162 static void bundle_del_port(struct ofport_dpif *);
163 static void bundle_run(struct ofbundle *);
164 static void bundle_wait(struct ofbundle *);
166 static void stp_run(struct ofproto_dpif *ofproto);
167 static void stp_wait(struct ofproto_dpif *ofproto);
169 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
171 struct action_xlate_ctx {
172 /* action_xlate_ctx_init() initializes these members. */
175 struct ofproto_dpif *ofproto;
177 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
178 * this flow when actions change header fields. */
181 /* The packet corresponding to 'flow', or a null pointer if we are
182 * revalidating without a packet to refer to. */
183 const struct ofpbuf *packet;
185 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
186 * want to execute them if we are actually processing a packet, or if we
187 * are accounting for packets that the datapath has processed, but not if
188 * we are just revalidating. */
191 /* If nonnull, called just before executing a resubmit action.
193 * This is normally null so the client has to set it manually after
194 * calling action_xlate_ctx_init(). */
195 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
197 /* xlate_actions() initializes and uses these members. The client might want
198 * to look at them after it returns. */
200 struct ofpbuf *odp_actions; /* Datapath actions. */
201 tag_type tags; /* Tags associated with actions. */
202 bool may_set_up_flow; /* True ordinarily; false if the actions must
203 * be reassessed for every packet. */
204 bool has_learn; /* Actions include NXAST_LEARN? */
205 bool has_normal; /* Actions output to OFPP_NORMAL? */
206 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
208 /* xlate_actions() initializes and uses these members, but the client has no
209 * reason to look at them. */
211 int recurse; /* Recursion level, via xlate_table_action. */
212 struct flow base_flow; /* Flow at the last commit. */
213 uint32_t original_priority; /* Priority when packet arrived. */
214 uint8_t table_id; /* OpenFlow table ID where flow was found. */
215 uint32_t sflow_n_outputs; /* Number of output ports. */
216 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
217 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
218 bool exit; /* No further actions should be processed. */
221 static void action_xlate_ctx_init(struct action_xlate_ctx *,
222 struct ofproto_dpif *, const struct flow *,
223 const struct ofpbuf *);
224 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
225 const union ofp_action *in, size_t n_in);
227 /* An exact-match instantiation of an OpenFlow flow. */
229 long long int used; /* Time last used; time created if not used. */
233 * - Do include packets and bytes sent "by hand", e.g. with
236 * - Do include packets and bytes that were obtained from the datapath
237 * when its statistics were reset (e.g. dpif_flow_put() with
238 * DPIF_FP_ZERO_STATS).
240 uint64_t packet_count; /* Number of packets received. */
241 uint64_t byte_count; /* Number of bytes received. */
243 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
244 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
246 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
247 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
248 long long int rs_used; /* Used time pushed to resubmit children. */
250 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
252 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
253 struct list list_node; /* In owning rule's 'facets' list. */
254 struct rule_dpif *rule; /* Owning rule. */
255 struct flow flow; /* Exact-match flow. */
256 bool installed; /* Installed in datapath? */
257 bool may_install; /* True ordinarily; false if actions must
258 * be reassessed for every packet. */
259 bool has_learn; /* Actions include NXAST_LEARN? */
260 bool has_normal; /* Actions output to OFPP_NORMAL? */
261 size_t actions_len; /* Number of bytes in actions[]. */
262 struct nlattr *actions; /* Datapath actions. */
263 tag_type tags; /* Tags. */
264 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
267 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
268 static void facet_remove(struct ofproto_dpif *, struct facet *);
269 static void facet_free(struct facet *);
271 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
272 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
273 const struct flow *);
274 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
276 static bool execute_controller_action(struct ofproto_dpif *,
278 const struct nlattr *odp_actions,
280 struct ofpbuf *packet);
281 static void facet_execute(struct ofproto_dpif *, struct facet *,
282 struct ofpbuf *packet);
284 static int facet_put__(struct ofproto_dpif *, struct facet *,
285 const struct nlattr *actions, size_t actions_len,
286 struct dpif_flow_stats *);
287 static void facet_install(struct ofproto_dpif *, struct facet *,
289 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
290 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
292 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
293 const struct ofpbuf *packet);
294 static void facet_update_time(struct ofproto_dpif *, struct facet *,
296 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
297 const struct dpif_flow_stats *);
298 static void facet_reset_counters(struct facet *);
299 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
300 static void facet_push_stats(struct facet *);
301 static void facet_account(struct ofproto_dpif *, struct facet *);
303 static bool facet_is_controller_flow(struct facet *);
305 static void flow_push_stats(const struct rule_dpif *, const struct flow *,
306 uint64_t packets, uint64_t bytes,
309 static uint32_t rule_calculate_tag(const struct flow *,
310 const struct flow_wildcards *,
312 static void rule_invalidate(const struct rule_dpif *);
318 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
319 struct list bundle_node; /* In struct ofbundle's "ports" list. */
320 struct cfm *cfm; /* Connectivity Fault Management, if any. */
321 tag_type tag; /* Tag associated with this port. */
322 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
323 bool may_enable; /* May be enabled in bonds. */
325 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
326 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
327 long long int stp_state_entered;
329 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
332 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
333 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
334 * traffic egressing the 'ofport' with that priority should be marked with. */
335 struct priority_to_dscp {
336 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
337 uint32_t priority; /* Priority of this queue (see struct flow). */
339 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
342 static struct ofport_dpif *
343 ofport_dpif_cast(const struct ofport *ofport)
345 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
346 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
349 static void port_run(struct ofport_dpif *);
350 static void port_wait(struct ofport_dpif *);
351 static int set_cfm(struct ofport *, const struct cfm_settings *);
352 static void ofport_clear_priorities(struct ofport_dpif *);
354 struct dpif_completion {
355 struct list list_node;
356 struct ofoperation *op;
359 /* Extra information about a classifier table.
360 * Currently used just for optimized flow revalidation. */
362 /* If either of these is nonnull, then this table has a form that allows
363 * flows to be tagged to avoid revalidating most flows for the most common
364 * kinds of flow table changes. */
365 struct cls_table *catchall_table; /* Table that wildcards all fields. */
366 struct cls_table *other_table; /* Table with any other wildcard set. */
367 uint32_t basis; /* Keeps each table's tags separate. */
370 struct ofproto_dpif {
379 struct netflow *netflow;
380 struct dpif_sflow *sflow;
381 struct hmap bundles; /* Contains "struct ofbundle"s. */
382 struct mac_learning *ml;
383 struct ofmirror *mirrors[MAX_MIRRORS];
384 bool has_bonded_bundles;
387 struct timer next_expiration;
393 struct table_dpif tables[N_TABLES];
394 bool need_revalidate;
395 struct tag_set revalidate_set;
397 /* Support for debugging async flow mods. */
398 struct list completions;
400 bool has_bundle_action; /* True when the first bundle action appears. */
404 long long int stp_last_tick;
407 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
408 * for debugging the asynchronous flow_mod implementation.) */
411 static void ofproto_dpif_unixctl_init(void);
413 static struct ofproto_dpif *
414 ofproto_dpif_cast(const struct ofproto *ofproto)
416 assert(ofproto->ofproto_class == &ofproto_dpif_class);
417 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
420 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
422 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
425 /* Packet processing. */
426 static void update_learning_table(struct ofproto_dpif *,
427 const struct flow *, int vlan,
430 #define FLOW_MISS_MAX_BATCH 50
432 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
433 static void handle_miss_upcalls(struct ofproto_dpif *,
434 struct dpif_upcall *, size_t n);
436 /* Flow expiration. */
437 static int expire(struct ofproto_dpif *);
440 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
441 const struct ofpbuf *packet);
443 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
444 const struct flow *, uint32_t odp_port);
445 /* Global variables. */
446 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
448 /* Factory functions. */
451 enumerate_types(struct sset *types)
453 dp_enumerate_types(types);
457 enumerate_names(const char *type, struct sset *names)
459 return dp_enumerate_names(type, names);
463 del(const char *type, const char *name)
468 error = dpif_open(name, type, &dpif);
470 error = dpif_delete(dpif);
476 /* Basic life-cycle. */
478 static struct ofproto *
481 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
486 dealloc(struct ofproto *ofproto_)
488 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
493 construct(struct ofproto *ofproto_, int *n_tablesp)
495 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
496 const char *name = ofproto->up.name;
500 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
502 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
506 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
507 ofproto->n_matches = 0;
509 dpif_flow_flush(ofproto->dpif);
510 dpif_recv_purge(ofproto->dpif);
512 error = dpif_recv_set_mask(ofproto->dpif,
513 ((1u << DPIF_UC_MISS) |
514 (1u << DPIF_UC_ACTION)));
516 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
517 dpif_close(ofproto->dpif);
521 ofproto->netflow = NULL;
522 ofproto->sflow = NULL;
524 hmap_init(&ofproto->bundles);
525 ofproto->ml = mac_learning_create();
526 for (i = 0; i < MAX_MIRRORS; i++) {
527 ofproto->mirrors[i] = NULL;
529 ofproto->has_bonded_bundles = false;
531 timer_set_duration(&ofproto->next_expiration, 1000);
533 hmap_init(&ofproto->facets);
535 for (i = 0; i < N_TABLES; i++) {
536 struct table_dpif *table = &ofproto->tables[i];
538 table->catchall_table = NULL;
539 table->other_table = NULL;
540 table->basis = random_uint32();
542 ofproto->need_revalidate = false;
543 tag_set_init(&ofproto->revalidate_set);
545 list_init(&ofproto->completions);
547 ofproto_dpif_unixctl_init();
549 ofproto->has_bundle_action = false;
551 *n_tablesp = N_TABLES;
556 complete_operations(struct ofproto_dpif *ofproto)
558 struct dpif_completion *c, *next;
560 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
561 ofoperation_complete(c->op, 0);
562 list_remove(&c->list_node);
568 destruct(struct ofproto *ofproto_)
570 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
571 struct rule_dpif *rule, *next_rule;
572 struct classifier *table;
575 complete_operations(ofproto);
577 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
578 struct cls_cursor cursor;
580 cls_cursor_init(&cursor, table, NULL);
581 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
582 ofproto_rule_destroy(&rule->up);
586 for (i = 0; i < MAX_MIRRORS; i++) {
587 mirror_destroy(ofproto->mirrors[i]);
590 netflow_destroy(ofproto->netflow);
591 dpif_sflow_destroy(ofproto->sflow);
592 hmap_destroy(&ofproto->bundles);
593 mac_learning_destroy(ofproto->ml);
595 hmap_destroy(&ofproto->facets);
597 dpif_close(ofproto->dpif);
601 run(struct ofproto *ofproto_)
603 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
604 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
605 struct ofport_dpif *ofport;
606 struct ofbundle *bundle;
611 complete_operations(ofproto);
613 dpif_run(ofproto->dpif);
616 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
617 struct dpif_upcall *upcall = &misses[n_misses];
620 error = dpif_recv(ofproto->dpif, upcall);
622 if (error == ENODEV && n_misses == 0) {
628 if (upcall->type == DPIF_UC_MISS) {
629 /* Handle it later. */
632 handle_upcall(ofproto, upcall);
636 handle_miss_upcalls(ofproto, misses, n_misses);
638 if (timer_expired(&ofproto->next_expiration)) {
639 int delay = expire(ofproto);
640 timer_set_duration(&ofproto->next_expiration, delay);
643 if (ofproto->netflow) {
644 netflow_run(ofproto->netflow);
646 if (ofproto->sflow) {
647 dpif_sflow_run(ofproto->sflow);
650 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
653 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
658 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
660 /* Now revalidate if there's anything to do. */
661 if (ofproto->need_revalidate
662 || !tag_set_is_empty(&ofproto->revalidate_set)) {
663 struct tag_set revalidate_set = ofproto->revalidate_set;
664 bool revalidate_all = ofproto->need_revalidate;
665 struct facet *facet, *next;
667 /* Clear the revalidation flags. */
668 tag_set_init(&ofproto->revalidate_set);
669 ofproto->need_revalidate = false;
671 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
673 || tag_set_intersects(&revalidate_set, facet->tags)) {
674 facet_revalidate(ofproto, facet);
683 wait(struct ofproto *ofproto_)
685 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
686 struct ofport_dpif *ofport;
687 struct ofbundle *bundle;
689 if (!clogged && !list_is_empty(&ofproto->completions)) {
690 poll_immediate_wake();
693 dpif_wait(ofproto->dpif);
694 dpif_recv_wait(ofproto->dpif);
695 if (ofproto->sflow) {
696 dpif_sflow_wait(ofproto->sflow);
698 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
699 poll_immediate_wake();
701 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
704 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
707 mac_learning_wait(ofproto->ml);
709 if (ofproto->need_revalidate) {
710 /* Shouldn't happen, but if it does just go around again. */
711 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
712 poll_immediate_wake();
714 timer_wait(&ofproto->next_expiration);
719 flush(struct ofproto *ofproto_)
721 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
722 struct facet *facet, *next_facet;
724 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
725 /* Mark the facet as not installed so that facet_remove() doesn't
726 * bother trying to uninstall it. There is no point in uninstalling it
727 * individually since we are about to blow away all the facets with
728 * dpif_flow_flush(). */
729 facet->installed = false;
730 facet->dp_packet_count = 0;
731 facet->dp_byte_count = 0;
732 facet_remove(ofproto, facet);
734 dpif_flow_flush(ofproto->dpif);
738 get_features(struct ofproto *ofproto_ OVS_UNUSED,
739 bool *arp_match_ip, uint32_t *actions)
741 *arp_match_ip = true;
742 *actions = ((1u << OFPAT_OUTPUT) |
743 (1u << OFPAT_SET_VLAN_VID) |
744 (1u << OFPAT_SET_VLAN_PCP) |
745 (1u << OFPAT_STRIP_VLAN) |
746 (1u << OFPAT_SET_DL_SRC) |
747 (1u << OFPAT_SET_DL_DST) |
748 (1u << OFPAT_SET_NW_SRC) |
749 (1u << OFPAT_SET_NW_DST) |
750 (1u << OFPAT_SET_NW_TOS) |
751 (1u << OFPAT_SET_TP_SRC) |
752 (1u << OFPAT_SET_TP_DST) |
753 (1u << OFPAT_ENQUEUE));
757 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
759 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
760 struct dpif_dp_stats s;
762 strcpy(ots->name, "classifier");
764 dpif_get_dp_stats(ofproto->dpif, &s);
765 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
766 put_32aligned_be64(&ots->matched_count,
767 htonll(s.n_hit + ofproto->n_matches));
771 set_netflow(struct ofproto *ofproto_,
772 const struct netflow_options *netflow_options)
774 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
776 if (netflow_options) {
777 if (!ofproto->netflow) {
778 ofproto->netflow = netflow_create();
780 return netflow_set_options(ofproto->netflow, netflow_options);
782 netflow_destroy(ofproto->netflow);
783 ofproto->netflow = NULL;
788 static struct ofport *
791 struct ofport_dpif *port = xmalloc(sizeof *port);
796 port_dealloc(struct ofport *port_)
798 struct ofport_dpif *port = ofport_dpif_cast(port_);
803 port_construct(struct ofport *port_)
805 struct ofport_dpif *port = ofport_dpif_cast(port_);
806 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
808 ofproto->need_revalidate = true;
809 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
812 port->tag = tag_create_random();
813 port->may_enable = true;
814 port->stp_port = NULL;
815 port->stp_state = STP_DISABLED;
816 hmap_init(&port->priorities);
818 if (ofproto->sflow) {
819 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
820 netdev_get_name(port->up.netdev));
827 port_destruct(struct ofport *port_)
829 struct ofport_dpif *port = ofport_dpif_cast(port_);
830 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
832 ofproto->need_revalidate = true;
833 bundle_remove(port_);
834 set_cfm(port_, NULL);
835 if (ofproto->sflow) {
836 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
839 ofport_clear_priorities(port);
840 hmap_destroy(&port->priorities);
844 port_modified(struct ofport *port_)
846 struct ofport_dpif *port = ofport_dpif_cast(port_);
848 if (port->bundle && port->bundle->bond) {
849 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
854 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
856 struct ofport_dpif *port = ofport_dpif_cast(port_);
857 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
858 ovs_be32 changed = old_config ^ port->up.opp.config;
860 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
861 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
862 ofproto->need_revalidate = true;
864 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
865 bundle_update(port->bundle);
871 set_sflow(struct ofproto *ofproto_,
872 const struct ofproto_sflow_options *sflow_options)
874 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
875 struct dpif_sflow *ds = ofproto->sflow;
879 struct ofport_dpif *ofport;
881 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
882 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
883 dpif_sflow_add_port(ds, ofport->odp_port,
884 netdev_get_name(ofport->up.netdev));
886 ofproto->need_revalidate = true;
888 dpif_sflow_set_options(ds, sflow_options);
891 dpif_sflow_destroy(ds);
892 ofproto->need_revalidate = true;
893 ofproto->sflow = NULL;
900 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
902 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
909 struct ofproto_dpif *ofproto;
911 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
912 ofproto->need_revalidate = true;
913 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
916 if (cfm_configure(ofport->cfm, s)) {
922 cfm_destroy(ofport->cfm);
928 get_cfm_fault(const struct ofport *ofport_)
930 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
932 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
936 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
939 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
942 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
952 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
954 struct ofproto_dpif *ofproto = ofproto_;
955 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
956 struct ofport_dpif *ofport;
958 ofport = stp_port_get_aux(sp);
960 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
961 ofproto->up.name, port_num);
963 struct eth_header *eth = pkt->l2;
965 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
966 if (eth_addr_is_zero(eth->eth_src)) {
967 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
968 "with unknown MAC", ofproto->up.name, port_num);
970 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
971 ofport->odp_port, pkt);
977 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
979 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
981 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
983 /* Only revalidate flows if the configuration changed. */
984 if (!s != !ofproto->stp) {
985 ofproto->need_revalidate = true;
990 ofproto->stp = stp_create(ofproto_->name, s->system_id,
991 send_bpdu_cb, ofproto);
992 ofproto->stp_last_tick = time_msec();
995 stp_set_bridge_id(ofproto->stp, s->system_id);
996 stp_set_bridge_priority(ofproto->stp, s->priority);
997 stp_set_hello_time(ofproto->stp, s->hello_time);
998 stp_set_max_age(ofproto->stp, s->max_age);
999 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1001 stp_destroy(ofproto->stp);
1002 ofproto->stp = NULL;
1009 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1011 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1015 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1016 s->designated_root = stp_get_designated_root(ofproto->stp);
1017 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1026 update_stp_port_state(struct ofport_dpif *ofport)
1028 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1029 enum stp_state state;
1031 /* Figure out new state. */
1032 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1036 if (ofport->stp_state != state) {
1040 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1041 netdev_get_name(ofport->up.netdev),
1042 stp_state_name(ofport->stp_state),
1043 stp_state_name(state));
1044 if (stp_learn_in_state(ofport->stp_state)
1045 != stp_learn_in_state(state)) {
1046 /* xxx Learning action flows should also be flushed. */
1047 mac_learning_flush(ofproto->ml);
1049 fwd_change = stp_forward_in_state(ofport->stp_state)
1050 != stp_forward_in_state(state);
1052 ofproto->need_revalidate = true;
1053 ofport->stp_state = state;
1054 ofport->stp_state_entered = time_msec();
1056 if (fwd_change && ofport->bundle) {
1057 bundle_update(ofport->bundle);
1060 /* Update the STP state bits in the OpenFlow port description. */
1061 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1062 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1063 : state == STP_LEARNING ? OFPPS_STP_LEARN
1064 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1065 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1067 ofproto_port_set_state(&ofport->up, of_state);
1071 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1072 * caller is responsible for assigning STP port numbers and ensuring
1073 * there are no duplicates. */
1075 set_stp_port(struct ofport *ofport_,
1076 const struct ofproto_port_stp_settings *s)
1078 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1079 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1080 struct stp_port *sp = ofport->stp_port;
1082 if (!s || !s->enable) {
1084 ofport->stp_port = NULL;
1085 stp_port_disable(sp);
1086 update_stp_port_state(ofport);
1089 } else if (sp && stp_port_no(sp) != s->port_num
1090 && ofport == stp_port_get_aux(sp)) {
1091 /* The port-id changed, so disable the old one if it's not
1092 * already in use by another port. */
1093 stp_port_disable(sp);
1096 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1097 stp_port_enable(sp);
1099 stp_port_set_aux(sp, ofport);
1100 stp_port_set_priority(sp, s->priority);
1101 stp_port_set_path_cost(sp, s->path_cost);
1103 update_stp_port_state(ofport);
1109 get_stp_port_status(struct ofport *ofport_,
1110 struct ofproto_port_stp_status *s)
1112 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1113 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1114 struct stp_port *sp = ofport->stp_port;
1116 if (!ofproto->stp || !sp) {
1122 s->port_id = stp_port_get_id(sp);
1123 s->state = stp_port_get_state(sp);
1124 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1125 s->role = stp_port_get_role(sp);
1126 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1132 stp_run(struct ofproto_dpif *ofproto)
1135 long long int now = time_msec();
1136 long long int elapsed = now - ofproto->stp_last_tick;
1137 struct stp_port *sp;
1140 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1141 ofproto->stp_last_tick = now;
1143 while (stp_get_changed_port(ofproto->stp, &sp)) {
1144 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1147 update_stp_port_state(ofport);
1154 stp_wait(struct ofproto_dpif *ofproto)
1157 poll_timer_wait(1000);
1161 /* Returns true if STP should process 'flow'. */
1163 stp_should_process_flow(const struct flow *flow)
1165 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1169 stp_process_packet(const struct ofport_dpif *ofport,
1170 const struct ofpbuf *packet)
1172 struct ofpbuf payload = *packet;
1173 struct eth_header *eth = payload.data;
1174 struct stp_port *sp = ofport->stp_port;
1176 /* Sink packets on ports that have STP disabled when the bridge has
1178 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1182 /* Trim off padding on payload. */
1183 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1184 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1187 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1188 stp_received_bpdu(sp, payload.data, payload.size);
1192 static struct priority_to_dscp *
1193 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1195 struct priority_to_dscp *pdscp;
1198 hash = hash_int(priority, 0);
1199 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1200 if (pdscp->priority == priority) {
1208 ofport_clear_priorities(struct ofport_dpif *ofport)
1210 struct priority_to_dscp *pdscp, *next;
1212 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1213 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1219 set_queues(struct ofport *ofport_,
1220 const struct ofproto_port_queue *qdscp_list,
1223 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1224 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1225 struct hmap new = HMAP_INITIALIZER(&new);
1228 for (i = 0; i < n_qdscp; i++) {
1229 struct priority_to_dscp *pdscp;
1233 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1234 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1239 pdscp = get_priority(ofport, priority);
1241 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1243 pdscp = xmalloc(sizeof *pdscp);
1244 pdscp->priority = priority;
1246 ofproto->need_revalidate = true;
1249 if (pdscp->dscp != dscp) {
1251 ofproto->need_revalidate = true;
1254 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1257 if (!hmap_is_empty(&ofport->priorities)) {
1258 ofport_clear_priorities(ofport);
1259 ofproto->need_revalidate = true;
1262 hmap_swap(&new, &ofport->priorities);
1270 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1271 * to revalidate every flow. */
1273 bundle_flush_macs(struct ofbundle *bundle)
1275 struct ofproto_dpif *ofproto = bundle->ofproto;
1276 struct mac_learning *ml = ofproto->ml;
1277 struct mac_entry *mac, *next_mac;
1279 ofproto->need_revalidate = true;
1280 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1281 if (mac->port.p == bundle) {
1282 mac_learning_expire(ml, mac);
1287 static struct ofbundle *
1288 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1290 struct ofbundle *bundle;
1292 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1293 &ofproto->bundles) {
1294 if (bundle->aux == aux) {
1301 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1302 * ones that are found to 'bundles'. */
1304 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1305 void **auxes, size_t n_auxes,
1306 struct hmapx *bundles)
1310 hmapx_init(bundles);
1311 for (i = 0; i < n_auxes; i++) {
1312 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1314 hmapx_add(bundles, bundle);
1320 bundle_update(struct ofbundle *bundle)
1322 struct ofport_dpif *port;
1324 bundle->floodable = true;
1325 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1326 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1327 bundle->floodable = false;
1334 bundle_del_port(struct ofport_dpif *port)
1336 struct ofbundle *bundle = port->bundle;
1338 bundle->ofproto->need_revalidate = true;
1340 list_remove(&port->bundle_node);
1341 port->bundle = NULL;
1344 lacp_slave_unregister(bundle->lacp, port);
1347 bond_slave_unregister(bundle->bond, port);
1350 bundle_update(bundle);
1354 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1355 struct lacp_slave_settings *lacp,
1356 uint32_t bond_stable_id)
1358 struct ofport_dpif *port;
1360 port = get_ofp_port(bundle->ofproto, ofp_port);
1365 if (port->bundle != bundle) {
1366 bundle->ofproto->need_revalidate = true;
1368 bundle_del_port(port);
1371 port->bundle = bundle;
1372 list_push_back(&bundle->ports, &port->bundle_node);
1373 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1374 bundle->floodable = false;
1378 port->bundle->ofproto->need_revalidate = true;
1379 lacp_slave_register(bundle->lacp, port, lacp);
1382 port->bond_stable_id = bond_stable_id;
1388 bundle_destroy(struct ofbundle *bundle)
1390 struct ofproto_dpif *ofproto;
1391 struct ofport_dpif *port, *next_port;
1398 ofproto = bundle->ofproto;
1399 for (i = 0; i < MAX_MIRRORS; i++) {
1400 struct ofmirror *m = ofproto->mirrors[i];
1402 if (m->out == bundle) {
1404 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1405 || hmapx_find_and_delete(&m->dsts, bundle)) {
1406 ofproto->need_revalidate = true;
1411 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1412 bundle_del_port(port);
1415 bundle_flush_macs(bundle);
1416 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1418 free(bundle->trunks);
1419 lacp_destroy(bundle->lacp);
1420 bond_destroy(bundle->bond);
1425 bundle_set(struct ofproto *ofproto_, void *aux,
1426 const struct ofproto_bundle_settings *s)
1428 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1429 bool need_flush = false;
1430 struct ofport_dpif *port;
1431 struct ofbundle *bundle;
1432 unsigned long *trunks;
1438 bundle_destroy(bundle_lookup(ofproto, aux));
1442 assert(s->n_slaves == 1 || s->bond != NULL);
1443 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1445 bundle = bundle_lookup(ofproto, aux);
1447 bundle = xmalloc(sizeof *bundle);
1449 bundle->ofproto = ofproto;
1450 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1451 hash_pointer(aux, 0));
1453 bundle->name = NULL;
1455 list_init(&bundle->ports);
1456 bundle->vlan_mode = PORT_VLAN_TRUNK;
1458 bundle->trunks = NULL;
1459 bundle->use_priority_tags = s->use_priority_tags;
1460 bundle->lacp = NULL;
1461 bundle->bond = NULL;
1463 bundle->floodable = true;
1465 bundle->src_mirrors = 0;
1466 bundle->dst_mirrors = 0;
1467 bundle->mirror_out = 0;
1470 if (!bundle->name || strcmp(s->name, bundle->name)) {
1472 bundle->name = xstrdup(s->name);
1477 if (!bundle->lacp) {
1478 ofproto->need_revalidate = true;
1479 bundle->lacp = lacp_create();
1481 lacp_configure(bundle->lacp, s->lacp);
1483 lacp_destroy(bundle->lacp);
1484 bundle->lacp = NULL;
1487 /* Update set of ports. */
1489 for (i = 0; i < s->n_slaves; i++) {
1490 if (!bundle_add_port(bundle, s->slaves[i],
1491 s->lacp ? &s->lacp_slaves[i] : NULL,
1492 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1496 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1497 struct ofport_dpif *next_port;
1499 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1500 for (i = 0; i < s->n_slaves; i++) {
1501 if (s->slaves[i] == port->up.ofp_port) {
1506 bundle_del_port(port);
1510 assert(list_size(&bundle->ports) <= s->n_slaves);
1512 if (list_is_empty(&bundle->ports)) {
1513 bundle_destroy(bundle);
1517 /* Set VLAN tagging mode */
1518 if (s->vlan_mode != bundle->vlan_mode
1519 || s->use_priority_tags != bundle->use_priority_tags) {
1520 bundle->vlan_mode = s->vlan_mode;
1521 bundle->use_priority_tags = s->use_priority_tags;
1526 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1527 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1529 if (vlan != bundle->vlan) {
1530 bundle->vlan = vlan;
1534 /* Get trunked VLANs. */
1535 switch (s->vlan_mode) {
1536 case PORT_VLAN_ACCESS:
1540 case PORT_VLAN_TRUNK:
1541 trunks = (unsigned long *) s->trunks;
1544 case PORT_VLAN_NATIVE_UNTAGGED:
1545 case PORT_VLAN_NATIVE_TAGGED:
1546 if (vlan != 0 && (!s->trunks
1547 || !bitmap_is_set(s->trunks, vlan)
1548 || bitmap_is_set(s->trunks, 0))) {
1549 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1551 trunks = bitmap_clone(s->trunks, 4096);
1553 trunks = bitmap_allocate1(4096);
1555 bitmap_set1(trunks, vlan);
1556 bitmap_set0(trunks, 0);
1558 trunks = (unsigned long *) s->trunks;
1565 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1566 free(bundle->trunks);
1567 if (trunks == s->trunks) {
1568 bundle->trunks = vlan_bitmap_clone(trunks);
1570 bundle->trunks = trunks;
1575 if (trunks != s->trunks) {
1580 if (!list_is_short(&bundle->ports)) {
1581 bundle->ofproto->has_bonded_bundles = true;
1583 if (bond_reconfigure(bundle->bond, s->bond)) {
1584 ofproto->need_revalidate = true;
1587 bundle->bond = bond_create(s->bond);
1588 ofproto->need_revalidate = true;
1591 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1592 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1596 bond_destroy(bundle->bond);
1597 bundle->bond = NULL;
1600 /* If we changed something that would affect MAC learning, un-learn
1601 * everything on this port and force flow revalidation. */
1603 bundle_flush_macs(bundle);
1610 bundle_remove(struct ofport *port_)
1612 struct ofport_dpif *port = ofport_dpif_cast(port_);
1613 struct ofbundle *bundle = port->bundle;
1616 bundle_del_port(port);
1617 if (list_is_empty(&bundle->ports)) {
1618 bundle_destroy(bundle);
1619 } else if (list_is_short(&bundle->ports)) {
1620 bond_destroy(bundle->bond);
1621 bundle->bond = NULL;
1627 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1629 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1630 struct ofport_dpif *port = port_;
1631 uint8_t ea[ETH_ADDR_LEN];
1634 error = netdev_get_etheraddr(port->up.netdev, ea);
1636 struct ofpbuf packet;
1639 ofpbuf_init(&packet, 0);
1640 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1642 memcpy(packet_pdu, pdu, pdu_size);
1644 send_packet(ofproto_dpif_cast(port->up.ofproto), port->odp_port,
1646 ofpbuf_uninit(&packet);
1648 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1649 "%s (%s)", port->bundle->name,
1650 netdev_get_name(port->up.netdev), strerror(error));
1655 bundle_send_learning_packets(struct ofbundle *bundle)
1657 struct ofproto_dpif *ofproto = bundle->ofproto;
1658 int error, n_packets, n_errors;
1659 struct mac_entry *e;
1661 error = n_packets = n_errors = 0;
1662 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1663 if (e->port.p != bundle) {
1664 struct ofpbuf *learning_packet;
1665 struct ofport_dpif *port;
1668 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1671 ret = send_packet(ofproto_dpif_cast(port->up.ofproto),
1672 port->odp_port, learning_packet);
1673 ofpbuf_delete(learning_packet);
1683 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1684 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1685 "packets, last error was: %s",
1686 bundle->name, n_errors, n_packets, strerror(error));
1688 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1689 bundle->name, n_packets);
1694 bundle_run(struct ofbundle *bundle)
1697 lacp_run(bundle->lacp, send_pdu_cb);
1700 struct ofport_dpif *port;
1702 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1703 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1706 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1707 lacp_negotiated(bundle->lacp));
1708 if (bond_should_send_learning_packets(bundle->bond)) {
1709 bundle_send_learning_packets(bundle);
1715 bundle_wait(struct ofbundle *bundle)
1718 lacp_wait(bundle->lacp);
1721 bond_wait(bundle->bond);
1728 mirror_scan(struct ofproto_dpif *ofproto)
1732 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1733 if (!ofproto->mirrors[idx]) {
1740 static struct ofmirror *
1741 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1745 for (i = 0; i < MAX_MIRRORS; i++) {
1746 struct ofmirror *mirror = ofproto->mirrors[i];
1747 if (mirror && mirror->aux == aux) {
1755 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1757 mirror_update_dups(struct ofproto_dpif *ofproto)
1761 for (i = 0; i < MAX_MIRRORS; i++) {
1762 struct ofmirror *m = ofproto->mirrors[i];
1765 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1769 for (i = 0; i < MAX_MIRRORS; i++) {
1770 struct ofmirror *m1 = ofproto->mirrors[i];
1777 for (j = i + 1; j < MAX_MIRRORS; j++) {
1778 struct ofmirror *m2 = ofproto->mirrors[j];
1780 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
1781 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1782 m2->dup_mirrors |= m1->dup_mirrors;
1789 mirror_set(struct ofproto *ofproto_, void *aux,
1790 const struct ofproto_mirror_settings *s)
1792 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1793 mirror_mask_t mirror_bit;
1794 struct ofbundle *bundle;
1795 struct ofmirror *mirror;
1796 struct ofbundle *out;
1797 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1798 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1801 mirror = mirror_lookup(ofproto, aux);
1803 mirror_destroy(mirror);
1809 idx = mirror_scan(ofproto);
1811 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1813 ofproto->up.name, MAX_MIRRORS, s->name);
1817 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1818 mirror->ofproto = ofproto;
1821 mirror->out_vlan = -1;
1822 mirror->name = NULL;
1825 if (!mirror->name || strcmp(s->name, mirror->name)) {
1827 mirror->name = xstrdup(s->name);
1830 /* Get the new configuration. */
1831 if (s->out_bundle) {
1832 out = bundle_lookup(ofproto, s->out_bundle);
1834 mirror_destroy(mirror);
1840 out_vlan = s->out_vlan;
1842 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1843 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1845 /* If the configuration has not changed, do nothing. */
1846 if (hmapx_equals(&srcs, &mirror->srcs)
1847 && hmapx_equals(&dsts, &mirror->dsts)
1848 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1849 && mirror->out == out
1850 && mirror->out_vlan == out_vlan)
1852 hmapx_destroy(&srcs);
1853 hmapx_destroy(&dsts);
1857 hmapx_swap(&srcs, &mirror->srcs);
1858 hmapx_destroy(&srcs);
1860 hmapx_swap(&dsts, &mirror->dsts);
1861 hmapx_destroy(&dsts);
1863 free(mirror->vlans);
1864 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1867 mirror->out_vlan = out_vlan;
1869 /* Update bundles. */
1870 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1871 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1872 if (hmapx_contains(&mirror->srcs, bundle)) {
1873 bundle->src_mirrors |= mirror_bit;
1875 bundle->src_mirrors &= ~mirror_bit;
1878 if (hmapx_contains(&mirror->dsts, bundle)) {
1879 bundle->dst_mirrors |= mirror_bit;
1881 bundle->dst_mirrors &= ~mirror_bit;
1884 if (mirror->out == bundle) {
1885 bundle->mirror_out |= mirror_bit;
1887 bundle->mirror_out &= ~mirror_bit;
1891 ofproto->need_revalidate = true;
1892 mac_learning_flush(ofproto->ml);
1893 mirror_update_dups(ofproto);
1899 mirror_destroy(struct ofmirror *mirror)
1901 struct ofproto_dpif *ofproto;
1902 mirror_mask_t mirror_bit;
1903 struct ofbundle *bundle;
1909 ofproto = mirror->ofproto;
1910 ofproto->need_revalidate = true;
1911 mac_learning_flush(ofproto->ml);
1913 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1914 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1915 bundle->src_mirrors &= ~mirror_bit;
1916 bundle->dst_mirrors &= ~mirror_bit;
1917 bundle->mirror_out &= ~mirror_bit;
1920 hmapx_destroy(&mirror->srcs);
1921 hmapx_destroy(&mirror->dsts);
1922 free(mirror->vlans);
1924 ofproto->mirrors[mirror->idx] = NULL;
1928 mirror_update_dups(ofproto);
1932 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1934 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1935 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1936 ofproto->need_revalidate = true;
1937 mac_learning_flush(ofproto->ml);
1943 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
1945 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1946 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1947 return bundle && bundle->mirror_out != 0;
1951 forward_bpdu_changed(struct ofproto *ofproto_)
1953 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1954 /* Revalidate cached flows whenever forward_bpdu option changes. */
1955 ofproto->need_revalidate = true;
1960 static struct ofport_dpif *
1961 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1963 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1964 return ofport ? ofport_dpif_cast(ofport) : NULL;
1967 static struct ofport_dpif *
1968 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1970 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1974 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1975 struct dpif_port *dpif_port)
1977 ofproto_port->name = dpif_port->name;
1978 ofproto_port->type = dpif_port->type;
1979 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1983 port_run(struct ofport_dpif *ofport)
1985 bool enable = netdev_get_carrier(ofport->up.netdev);
1988 cfm_run(ofport->cfm);
1990 if (cfm_should_send_ccm(ofport->cfm)) {
1991 struct ofpbuf packet;
1993 ofpbuf_init(&packet, 0);
1994 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1995 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1996 ofport->odp_port, &packet);
1997 ofpbuf_uninit(&packet);
2000 enable = enable && !cfm_get_fault(ofport->cfm)
2001 && cfm_get_opup(ofport->cfm);
2004 if (ofport->bundle) {
2005 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2008 if (ofport->may_enable != enable) {
2009 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2011 if (ofproto->has_bundle_action) {
2012 ofproto->need_revalidate = true;
2016 ofport->may_enable = enable;
2020 port_wait(struct ofport_dpif *ofport)
2023 cfm_wait(ofport->cfm);
2028 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2029 struct ofproto_port *ofproto_port)
2031 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2032 struct dpif_port dpif_port;
2035 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2037 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2043 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2045 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2049 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2051 *ofp_portp = odp_port_to_ofp_port(odp_port);
2057 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2059 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2062 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2064 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2066 /* The caller is going to close ofport->up.netdev. If this is a
2067 * bonded port, then the bond is using that netdev, so remove it
2068 * from the bond. The client will need to reconfigure everything
2069 * after deleting ports, so then the slave will get re-added. */
2070 bundle_remove(&ofport->up);
2076 struct port_dump_state {
2077 struct dpif_port_dump dump;
2082 port_dump_start(const struct ofproto *ofproto_, void **statep)
2084 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2085 struct port_dump_state *state;
2087 *statep = state = xmalloc(sizeof *state);
2088 dpif_port_dump_start(&state->dump, ofproto->dpif);
2089 state->done = false;
2094 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2095 struct ofproto_port *port)
2097 struct port_dump_state *state = state_;
2098 struct dpif_port dpif_port;
2100 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2101 ofproto_port_from_dpif_port(port, &dpif_port);
2104 int error = dpif_port_dump_done(&state->dump);
2106 return error ? error : EOF;
2111 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2113 struct port_dump_state *state = state_;
2116 dpif_port_dump_done(&state->dump);
2123 port_poll(const struct ofproto *ofproto_, char **devnamep)
2125 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2126 return dpif_port_poll(ofproto->dpif, devnamep);
2130 port_poll_wait(const struct ofproto *ofproto_)
2132 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2133 dpif_port_poll_wait(ofproto->dpif);
2137 port_is_lacp_current(const struct ofport *ofport_)
2139 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2140 return (ofport->bundle && ofport->bundle->lacp
2141 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2145 /* Upcall handling. */
2147 /* Flow miss batching.
2149 * Some dpifs implement operations faster when you hand them off in a batch.
2150 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2151 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2152 * more packets, plus possibly installing the flow in the dpif.
2154 * So far we only batch the operations that affect flow setup time the most.
2155 * It's possible to batch more than that, but the benefit might be minimal. */
2157 struct hmap_node hmap_node;
2159 const struct nlattr *key;
2161 struct list packets;
2164 struct flow_miss_op {
2165 union dpif_op dpif_op;
2166 struct facet *facet;
2169 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2170 * OpenFlow controller as necessary according to their individual
2173 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2174 * ownership is transferred to this function. */
2176 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2177 const struct flow *flow, bool clone)
2179 struct ofputil_packet_in pin;
2181 pin.packet = packet;
2182 pin.in_port = flow->in_port;
2183 pin.reason = OFPR_NO_MATCH;
2184 pin.buffer_id = 0; /* not yet known */
2185 pin.send_len = 0; /* not used for flow table misses */
2186 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2187 clone ? NULL : packet);
2190 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2191 * OpenFlow controller as necessary according to their individual
2194 * 'send_len' should be the number of bytes of 'packet' to send to the
2195 * controller, as specified in the action that caused the packet to be sent.
2197 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2198 * Otherwise, ownership is transferred to this function. */
2200 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2201 uint64_t userdata, const struct flow *flow, bool clone)
2203 struct ofputil_packet_in pin;
2204 struct user_action_cookie cookie;
2206 memcpy(&cookie, &userdata, sizeof(cookie));
2208 pin.packet = packet;
2209 pin.in_port = flow->in_port;
2210 pin.reason = OFPR_ACTION;
2211 pin.buffer_id = 0; /* not yet known */
2212 pin.send_len = cookie.data;
2213 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2214 clone ? NULL : packet);
2218 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2219 const struct ofpbuf *packet)
2221 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2227 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2229 cfm_process_heartbeat(ofport->cfm, packet);
2232 } else if (ofport->bundle && ofport->bundle->lacp
2233 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2235 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2238 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2240 stp_process_packet(ofport, packet);
2247 static struct flow_miss *
2248 flow_miss_create(struct hmap *todo, const struct flow *flow,
2249 const struct nlattr *key, size_t key_len)
2251 uint32_t hash = flow_hash(flow, 0);
2252 struct flow_miss *miss;
2254 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2255 if (flow_equal(&miss->flow, flow)) {
2260 miss = xmalloc(sizeof *miss);
2261 hmap_insert(todo, &miss->hmap_node, hash);
2264 miss->key_len = key_len;
2265 list_init(&miss->packets);
2270 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2271 struct flow_miss_op *ops, size_t *n_ops)
2273 const struct flow *flow = &miss->flow;
2274 struct ofpbuf *packet, *next_packet;
2275 struct facet *facet;
2277 facet = facet_lookup_valid(ofproto, flow);
2279 struct rule_dpif *rule;
2281 rule = rule_dpif_lookup(ofproto, flow, 0);
2283 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2284 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2286 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2287 COVERAGE_INC(ofproto_dpif_no_packet_in);
2288 /* XXX install 'drop' flow entry */
2292 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2296 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2298 list_remove(&packet->list_node);
2299 send_packet_in_miss(ofproto, packet, flow, false);
2305 facet = facet_create(rule, flow);
2308 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2309 list_remove(&packet->list_node);
2310 ofproto->n_matches++;
2312 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2314 * Extra-special case for fail-open mode.
2316 * We are in fail-open mode and the packet matched the fail-open
2317 * rule, but we are connected to a controller too. We should send
2318 * the packet up to the controller in the hope that it will try to
2319 * set up a flow and thereby allow us to exit fail-open.
2321 * See the top-level comment in fail-open.c for more information.
2323 send_packet_in_miss(ofproto, packet, flow, true);
2326 if (!facet->may_install) {
2327 facet_make_actions(ofproto, facet, packet);
2329 if (!execute_controller_action(ofproto, &facet->flow,
2330 facet->actions, facet->actions_len,
2332 struct flow_miss_op *op = &ops[(*n_ops)++];
2333 struct dpif_execute *execute = &op->dpif_op.execute;
2336 execute->type = DPIF_OP_EXECUTE;
2337 execute->key = miss->key;
2338 execute->key_len = miss->key_len;
2340 = (facet->may_install
2342 : xmemdup(facet->actions, facet->actions_len));
2343 execute->actions_len = facet->actions_len;
2344 execute->packet = packet;
2348 if (facet->may_install) {
2349 struct flow_miss_op *op = &ops[(*n_ops)++];
2350 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2353 put->type = DPIF_OP_FLOW_PUT;
2354 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2355 put->key = miss->key;
2356 put->key_len = miss->key_len;
2357 put->actions = facet->actions;
2358 put->actions_len = facet->actions_len;
2364 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2367 struct dpif_upcall *upcall;
2368 struct flow_miss *miss, *next_miss;
2369 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2370 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2379 /* Construct the to-do list.
2381 * This just amounts to extracting the flow from each packet and sticking
2382 * the packets that have the same flow in the same "flow_miss" structure so
2383 * that we can process them together. */
2385 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2386 struct flow_miss *miss;
2389 /* Obtain in_port and tun_id, at least, then set 'flow''s header
2391 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2392 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2393 flow.in_port, &flow);
2395 /* Handle 802.1ag, LACP, and STP specially. */
2396 if (process_special(ofproto, &flow, upcall->packet)) {
2397 ofpbuf_delete(upcall->packet);
2398 ofproto->n_matches++;
2402 /* Add other packets to a to-do list. */
2403 miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len);
2404 list_push_back(&miss->packets, &upcall->packet->list_node);
2407 /* Process each element in the to-do list, constructing the set of
2408 * operations to batch. */
2410 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2411 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2412 ofpbuf_list_delete(&miss->packets);
2413 hmap_remove(&todo, &miss->hmap_node);
2416 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2417 hmap_destroy(&todo);
2419 /* Execute batch. */
2420 for (i = 0; i < n_ops; i++) {
2421 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2423 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2425 /* Free memory and update facets. */
2426 for (i = 0; i < n_ops; i++) {
2427 struct flow_miss_op *op = &flow_miss_ops[i];
2428 struct dpif_execute *execute;
2429 struct dpif_flow_put *put;
2431 switch (op->dpif_op.type) {
2432 case DPIF_OP_EXECUTE:
2433 execute = &op->dpif_op.execute;
2434 if (op->facet->actions != execute->actions) {
2435 free((struct nlattr *) execute->actions);
2437 ofpbuf_delete((struct ofpbuf *) execute->packet);
2440 case DPIF_OP_FLOW_PUT:
2441 put = &op->dpif_op.flow_put;
2443 op->facet->installed = true;
2451 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2452 struct dpif_upcall *upcall)
2455 struct user_action_cookie cookie;
2457 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2459 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2460 if (ofproto->sflow) {
2461 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2462 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2464 ofpbuf_delete(upcall->packet);
2466 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2467 COVERAGE_INC(ofproto_dpif_ctlr_action);
2468 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2469 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2472 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2477 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2479 switch (upcall->type) {
2480 case DPIF_UC_ACTION:
2481 handle_userspace_upcall(ofproto, upcall);
2485 /* The caller handles these. */
2488 case DPIF_N_UC_TYPES:
2490 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2495 /* Flow expiration. */
2497 static int facet_max_idle(const struct ofproto_dpif *);
2498 static void update_stats(struct ofproto_dpif *);
2499 static void rule_expire(struct rule_dpif *);
2500 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
2502 /* This function is called periodically by run(). Its job is to collect
2503 * updates for the flows that have been installed into the datapath, most
2504 * importantly when they last were used, and then use that information to
2505 * expire flows that have not been used recently.
2507 * Returns the number of milliseconds after which it should be called again. */
2509 expire(struct ofproto_dpif *ofproto)
2511 struct rule_dpif *rule, *next_rule;
2512 struct classifier *table;
2515 /* Update stats for each flow in the datapath. */
2516 update_stats(ofproto);
2518 /* Expire facets that have been idle too long. */
2519 dp_max_idle = facet_max_idle(ofproto);
2520 expire_facets(ofproto, dp_max_idle);
2522 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2523 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2524 struct cls_cursor cursor;
2526 cls_cursor_init(&cursor, table, NULL);
2527 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2532 /* All outstanding data in existing flows has been accounted, so it's a
2533 * good time to do bond rebalancing. */
2534 if (ofproto->has_bonded_bundles) {
2535 struct ofbundle *bundle;
2537 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2539 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2544 return MIN(dp_max_idle, 1000);
2547 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2549 * This function also pushes statistics updates to rules which each facet
2550 * resubmits into. Generally these statistics will be accurate. However, if a
2551 * facet changes the rule it resubmits into at some time in between
2552 * update_stats() runs, it is possible that statistics accrued to the
2553 * old rule will be incorrectly attributed to the new rule. This could be
2554 * avoided by calling update_stats() whenever rules are created or
2555 * deleted. However, the performance impact of making so many calls to the
2556 * datapath do not justify the benefit of having perfectly accurate statistics.
2559 update_stats(struct ofproto_dpif *p)
2561 const struct dpif_flow_stats *stats;
2562 struct dpif_flow_dump dump;
2563 const struct nlattr *key;
2566 dpif_flow_dump_start(&dump, p->dpif);
2567 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2568 struct facet *facet;
2571 if (odp_flow_key_to_flow(key, key_len, &flow)) {
2575 odp_flow_key_format(key, key_len, &s);
2576 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
2582 facet = facet_find(p, &flow);
2584 if (facet && facet->installed) {
2586 if (stats->n_packets >= facet->dp_packet_count) {
2587 uint64_t extra = stats->n_packets - facet->dp_packet_count;
2588 facet->packet_count += extra;
2590 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2593 if (stats->n_bytes >= facet->dp_byte_count) {
2594 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
2596 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2599 facet->dp_packet_count = stats->n_packets;
2600 facet->dp_byte_count = stats->n_bytes;
2602 facet_update_time(p, facet, stats->used);
2603 facet_account(p, facet);
2604 facet_push_stats(facet);
2606 /* There's a flow in the datapath that we know nothing about.
2608 COVERAGE_INC(facet_unexpected);
2609 dpif_flow_del(p->dpif, key, key_len, NULL);
2612 dpif_flow_dump_done(&dump);
2615 /* Calculates and returns the number of milliseconds of idle time after which
2616 * facets should expire from the datapath and we should fold their statistics
2617 * into their parent rules in userspace. */
2619 facet_max_idle(const struct ofproto_dpif *ofproto)
2622 * Idle time histogram.
2624 * Most of the time a switch has a relatively small number of facets. When
2625 * this is the case we might as well keep statistics for all of them in
2626 * userspace and to cache them in the kernel datapath for performance as
2629 * As the number of facets increases, the memory required to maintain
2630 * statistics about them in userspace and in the kernel becomes
2631 * significant. However, with a large number of facets it is likely that
2632 * only a few of them are "heavy hitters" that consume a large amount of
2633 * bandwidth. At this point, only heavy hitters are worth caching in the
2634 * kernel and maintaining in userspaces; other facets we can discard.
2636 * The technique used to compute the idle time is to build a histogram with
2637 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
2638 * that is installed in the kernel gets dropped in the appropriate bucket.
2639 * After the histogram has been built, we compute the cutoff so that only
2640 * the most-recently-used 1% of facets (but at least
2641 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2642 * the most-recently-used bucket of facets is kept, so actually an
2643 * arbitrary number of facets can be kept in any given expiration run
2644 * (though the next run will delete most of those unless they receive
2647 * This requires a second pass through the facets, in addition to the pass
2648 * made by update_stats(), because the former function never looks
2649 * at uninstallable facets.
2651 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2652 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2653 int buckets[N_BUCKETS] = { 0 };
2654 int total, subtotal, bucket;
2655 struct facet *facet;
2659 total = hmap_count(&ofproto->facets);
2660 if (total <= ofproto->up.flow_eviction_threshold) {
2661 return N_BUCKETS * BUCKET_WIDTH;
2664 /* Build histogram. */
2666 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
2667 long long int idle = now - facet->used;
2668 int bucket = (idle <= 0 ? 0
2669 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2670 : (unsigned int) idle / BUCKET_WIDTH);
2674 /* Find the first bucket whose flows should be expired. */
2675 subtotal = bucket = 0;
2677 subtotal += buckets[bucket++];
2678 } while (bucket < N_BUCKETS &&
2679 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2681 if (VLOG_IS_DBG_ENABLED()) {
2685 ds_put_cstr(&s, "keep");
2686 for (i = 0; i < N_BUCKETS; i++) {
2688 ds_put_cstr(&s, ", drop");
2691 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2694 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2698 return bucket * BUCKET_WIDTH;
2702 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2704 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2705 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2706 struct ofexpired expired;
2708 if (facet->installed) {
2709 struct dpif_flow_stats stats;
2711 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2713 facet_update_stats(ofproto, facet, &stats);
2716 expired.flow = facet->flow;
2717 expired.packet_count = facet->packet_count;
2718 expired.byte_count = facet->byte_count;
2719 expired.used = facet->used;
2720 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2725 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2727 long long int cutoff = time_msec() - dp_max_idle;
2728 struct facet *facet, *next_facet;
2730 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2731 facet_active_timeout(ofproto, facet);
2732 if (facet->used < cutoff) {
2733 facet_remove(ofproto, facet);
2738 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2739 * then delete it entirely. */
2741 rule_expire(struct rule_dpif *rule)
2743 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2744 struct facet *facet, *next_facet;
2748 /* Has 'rule' expired? */
2750 if (rule->up.hard_timeout
2751 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2752 reason = OFPRR_HARD_TIMEOUT;
2753 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2754 && now > rule->used + rule->up.idle_timeout * 1000) {
2755 reason = OFPRR_IDLE_TIMEOUT;
2760 COVERAGE_INC(ofproto_dpif_expired);
2762 /* Update stats. (This is a no-op if the rule expired due to an idle
2763 * timeout, because that only happens when the rule has no facets left.) */
2764 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2765 facet_remove(ofproto, facet);
2768 /* Get rid of the rule. */
2769 ofproto_rule_expire(&rule->up, reason);
2774 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2776 * The caller must already have determined that no facet with an identical
2777 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2778 * the ofproto's classifier table.
2780 * The facet will initially have no ODP actions. The caller should fix that
2781 * by calling facet_make_actions(). */
2782 static struct facet *
2783 facet_create(struct rule_dpif *rule, const struct flow *flow)
2785 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2786 struct facet *facet;
2788 facet = xzalloc(sizeof *facet);
2789 facet->used = time_msec();
2790 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2791 list_push_back(&rule->facets, &facet->list_node);
2793 facet->flow = *flow;
2794 netflow_flow_init(&facet->nf_flow);
2795 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2801 facet_free(struct facet *facet)
2803 free(facet->actions);
2808 execute_controller_action(struct ofproto_dpif *ofproto,
2809 const struct flow *flow,
2810 const struct nlattr *odp_actions, size_t actions_len,
2811 struct ofpbuf *packet)
2814 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2815 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2816 /* As an optimization, avoid a round-trip from userspace to kernel to
2817 * userspace. This also avoids possibly filling up kernel packet
2818 * buffers along the way.
2820 * This optimization will not accidentally catch sFlow
2821 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2822 * inside OVS_ACTION_ATTR_SAMPLE. */
2823 const struct nlattr *nla;
2825 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2826 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2834 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2835 * 'packet', which arrived on 'in_port'.
2837 * Takes ownership of 'packet'. */
2839 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2840 const struct nlattr *odp_actions, size_t actions_len,
2841 struct ofpbuf *packet)
2843 struct odputil_keybuf keybuf;
2847 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2852 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2853 odp_flow_key_from_flow(&key, flow);
2855 error = dpif_execute(ofproto->dpif, key.data, key.size,
2856 odp_actions, actions_len, packet);
2858 ofpbuf_delete(packet);
2862 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2863 * statistics appropriately. 'packet' must have at least sizeof(struct
2864 * ofp_packet_in) bytes of headroom.
2866 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2867 * applying flow_extract() to 'packet' would yield the same flow as
2870 * 'facet' must have accurately composed datapath actions; that is, it must
2871 * not be in need of revalidation.
2873 * Takes ownership of 'packet'. */
2875 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2876 struct ofpbuf *packet)
2878 struct dpif_flow_stats stats;
2880 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2882 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2883 stats.used = time_msec();
2884 if (execute_odp_actions(ofproto, &facet->flow,
2885 facet->actions, facet->actions_len, packet)) {
2886 facet_update_stats(ofproto, facet, &stats);
2890 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2892 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2893 * rule's statistics, via facet_uninstall().
2895 * - Removes 'facet' from its rule and from ofproto->facets.
2898 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2900 facet_uninstall(ofproto, facet);
2901 facet_flush_stats(ofproto, facet);
2902 hmap_remove(&ofproto->facets, &facet->hmap_node);
2903 list_remove(&facet->list_node);
2907 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2909 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2910 const struct ofpbuf *packet)
2912 const struct rule_dpif *rule = facet->rule;
2913 struct ofpbuf *odp_actions;
2914 struct action_xlate_ctx ctx;
2916 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2917 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2918 facet->tags = ctx.tags;
2919 facet->may_install = ctx.may_set_up_flow;
2920 facet->has_learn = ctx.has_learn;
2921 facet->has_normal = ctx.has_normal;
2922 facet->nf_flow.output_iface = ctx.nf_output_iface;
2924 if (facet->actions_len != odp_actions->size
2925 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2926 free(facet->actions);
2927 facet->actions_len = odp_actions->size;
2928 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2931 ofpbuf_delete(odp_actions);
2934 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2935 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2936 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2937 * since 'facet' was last updated.
2939 * Returns 0 if successful, otherwise a positive errno value.*/
2941 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2942 const struct nlattr *actions, size_t actions_len,
2943 struct dpif_flow_stats *stats)
2945 struct odputil_keybuf keybuf;
2946 enum dpif_flow_put_flags flags;
2950 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2952 flags |= DPIF_FP_ZERO_STATS;
2955 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2956 odp_flow_key_from_flow(&key, &facet->flow);
2958 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2959 actions, actions_len, stats);
2962 facet_reset_dp_stats(facet, stats);
2968 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2969 * 'zero_stats' is true, clears any existing statistics from the datapath for
2972 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2974 struct dpif_flow_stats stats;
2976 if (facet->may_install
2977 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2978 zero_stats ? &stats : NULL)) {
2979 facet->installed = true;
2984 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2987 const struct nlattr *a;
2991 if (facet->byte_count <= facet->accounted_bytes) {
2994 n_bytes = facet->byte_count - facet->accounted_bytes;
2995 facet->accounted_bytes = facet->byte_count;
2997 /* Feed information from the active flows back into the learning table to
2998 * ensure that table is always in sync with what is actually flowing
2999 * through the datapath. */
3000 if (facet->has_learn || facet->has_normal) {
3001 struct action_xlate_ctx ctx;
3003 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3004 ctx.may_learn = true;
3005 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
3006 facet->rule->up.n_actions));
3009 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
3013 /* This loop feeds byte counters to bond_account() for rebalancing to use
3014 * as a basis. We also need to track the actual VLAN on which the packet
3015 * is going to be sent to ensure that it matches the one passed to
3016 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
3018 vlan_tci = facet->flow.vlan_tci;
3019 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
3020 const struct ovs_action_push_vlan *vlan;
3021 struct ofport_dpif *port;
3023 switch (nl_attr_type(a)) {
3024 case OVS_ACTION_ATTR_OUTPUT:
3025 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3026 if (port && port->bundle && port->bundle->bond) {
3027 bond_account(port->bundle->bond, &facet->flow,
3028 vlan_tci_to_vid(vlan_tci), n_bytes);
3032 case OVS_ACTION_ATTR_POP_VLAN:
3033 vlan_tci = htons(0);
3036 case OVS_ACTION_ATTR_PUSH_VLAN:
3037 vlan = nl_attr_get(a);
3038 vlan_tci = vlan->vlan_tci;
3044 /* If 'rule' is installed in the datapath, uninstalls it. */
3046 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
3048 if (facet->installed) {
3049 struct odputil_keybuf keybuf;
3050 struct dpif_flow_stats stats;
3054 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3055 odp_flow_key_from_flow(&key, &facet->flow);
3057 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
3058 facet_reset_dp_stats(facet, &stats);
3060 facet_update_stats(p, facet, &stats);
3062 facet->installed = false;
3064 assert(facet->dp_packet_count == 0);
3065 assert(facet->dp_byte_count == 0);
3069 /* Returns true if the only action for 'facet' is to send to the controller.
3070 * (We don't report NetFlow expiration messages for such facets because they
3071 * are just part of the control logic for the network, not real traffic). */
3073 facet_is_controller_flow(struct facet *facet)
3076 && facet->rule->up.n_actions == 1
3077 && action_outputs_to_port(&facet->rule->up.actions[0],
3078 htons(OFPP_CONTROLLER)));
3081 /* Resets 'facet''s datapath statistics counters. This should be called when
3082 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
3083 * it should contain the statistics returned by dpif when 'facet' was reset in
3084 * the datapath. 'stats' will be modified to only included statistics new
3085 * since 'facet' was last updated. */
3087 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
3089 if (stats && facet->dp_packet_count <= stats->n_packets
3090 && facet->dp_byte_count <= stats->n_bytes) {
3091 stats->n_packets -= facet->dp_packet_count;
3092 stats->n_bytes -= facet->dp_byte_count;
3095 facet->dp_packet_count = 0;
3096 facet->dp_byte_count = 0;
3099 /* Folds all of 'facet''s statistics into its rule. Also updates the
3100 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3101 * 'facet''s statistics in the datapath should have been zeroed and folded into
3102 * its packet and byte counts before this function is called. */
3104 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3106 assert(!facet->dp_byte_count);
3107 assert(!facet->dp_packet_count);
3109 facet_push_stats(facet);
3110 facet_account(ofproto, facet);
3112 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3113 struct ofexpired expired;
3114 expired.flow = facet->flow;
3115 expired.packet_count = facet->packet_count;
3116 expired.byte_count = facet->byte_count;
3117 expired.used = facet->used;
3118 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3121 facet->rule->packet_count += facet->packet_count;
3122 facet->rule->byte_count += facet->byte_count;
3124 /* Reset counters to prevent double counting if 'facet' ever gets
3126 facet_reset_counters(facet);
3128 netflow_flow_clear(&facet->nf_flow);
3131 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3132 * Returns it if found, otherwise a null pointer.
3134 * The returned facet might need revalidation; use facet_lookup_valid()
3135 * instead if that is important. */
3136 static struct facet *
3137 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3139 struct facet *facet;
3141 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3143 if (flow_equal(flow, &facet->flow)) {
3151 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3152 * Returns it if found, otherwise a null pointer.
3154 * The returned facet is guaranteed to be valid. */
3155 static struct facet *
3156 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3158 struct facet *facet = facet_find(ofproto, flow);
3160 /* The facet we found might not be valid, since we could be in need of
3161 * revalidation. If it is not valid, don't return it. */
3163 && (ofproto->need_revalidate
3164 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3165 && !facet_revalidate(ofproto, facet)) {
3166 COVERAGE_INC(facet_invalidated);
3173 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3175 * - If the rule found is different from 'facet''s current rule, moves
3176 * 'facet' to the new rule and recompiles its actions.
3178 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3179 * where it is and recompiles its actions anyway.
3181 * - If there is none, destroys 'facet'.
3183 * Returns true if 'facet' still exists, false if it has been destroyed. */
3185 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3187 struct action_xlate_ctx ctx;
3188 struct ofpbuf *odp_actions;
3189 struct rule_dpif *new_rule;
3190 bool actions_changed;
3192 COVERAGE_INC(facet_revalidate);
3194 /* Determine the new rule. */
3195 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3197 /* No new rule, so delete the facet. */
3198 facet_remove(ofproto, facet);
3202 /* Calculate new datapath actions.
3204 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3205 * emit a NetFlow expiration and, if so, we need to have the old state
3206 * around to properly compose it. */
3207 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3208 odp_actions = xlate_actions(&ctx,
3209 new_rule->up.actions, new_rule->up.n_actions);
3210 actions_changed = (facet->actions_len != odp_actions->size
3211 || memcmp(facet->actions, odp_actions->data,
3212 facet->actions_len));
3214 /* If the datapath actions changed or the installability changed,
3215 * then we need to talk to the datapath. */
3216 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
3217 if (ctx.may_set_up_flow) {
3218 struct dpif_flow_stats stats;
3220 facet_put__(ofproto, facet,
3221 odp_actions->data, odp_actions->size, &stats);
3222 facet_update_stats(ofproto, facet, &stats);
3224 facet_uninstall(ofproto, facet);
3227 /* The datapath flow is gone or has zeroed stats, so push stats out of
3228 * 'facet' into 'rule'. */
3229 facet_flush_stats(ofproto, facet);
3232 /* Update 'facet' now that we've taken care of all the old state. */
3233 facet->tags = ctx.tags;
3234 facet->nf_flow.output_iface = ctx.nf_output_iface;
3235 facet->may_install = ctx.may_set_up_flow;
3236 facet->has_learn = ctx.has_learn;
3237 facet->has_normal = ctx.has_normal;
3238 if (actions_changed) {
3239 free(facet->actions);
3240 facet->actions_len = odp_actions->size;
3241 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
3243 if (facet->rule != new_rule) {
3244 COVERAGE_INC(facet_changed_rule);
3245 list_remove(&facet->list_node);
3246 list_push_back(&new_rule->facets, &facet->list_node);
3247 facet->rule = new_rule;
3248 facet->used = new_rule->up.created;
3249 facet->rs_used = facet->used;
3252 ofpbuf_delete(odp_actions);
3257 /* Updates 'facet''s used time. Caller is responsible for calling
3258 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3260 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3263 if (used > facet->used) {
3265 if (used > facet->rule->used) {
3266 facet->rule->used = used;
3268 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3272 /* Folds the statistics from 'stats' into the counters in 'facet'.
3274 * Because of the meaning of a facet's counters, it only makes sense to do this
3275 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3276 * packet that was sent by hand or if it represents statistics that have been
3277 * cleared out of the datapath. */
3279 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
3280 const struct dpif_flow_stats *stats)
3282 if (stats->n_packets || stats->used > facet->used) {
3283 facet_update_time(ofproto, facet, stats->used);
3284 facet->packet_count += stats->n_packets;
3285 facet->byte_count += stats->n_bytes;
3286 facet_push_stats(facet);
3287 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3292 facet_reset_counters(struct facet *facet)
3294 facet->packet_count = 0;
3295 facet->byte_count = 0;
3296 facet->rs_packet_count = 0;
3297 facet->rs_byte_count = 0;
3298 facet->accounted_bytes = 0;
3302 facet_push_stats(struct facet *facet)
3304 uint64_t rs_packets, rs_bytes;
3306 assert(facet->packet_count >= facet->rs_packet_count);
3307 assert(facet->byte_count >= facet->rs_byte_count);
3308 assert(facet->used >= facet->rs_used);
3310 rs_packets = facet->packet_count - facet->rs_packet_count;
3311 rs_bytes = facet->byte_count - facet->rs_byte_count;
3313 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3314 facet->rs_packet_count = facet->packet_count;
3315 facet->rs_byte_count = facet->byte_count;
3316 facet->rs_used = facet->used;
3318 flow_push_stats(facet->rule, &facet->flow,
3319 rs_packets, rs_bytes, facet->used);
3323 struct ofproto_push {
3324 struct action_xlate_ctx ctx;
3331 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3333 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3336 rule->packet_count += push->packets;
3337 rule->byte_count += push->bytes;
3338 rule->used = MAX(push->used, rule->used);
3342 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3343 * 'rule''s actions. */
3345 flow_push_stats(const struct rule_dpif *rule,
3346 const struct flow *flow, uint64_t packets, uint64_t bytes,
3349 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3350 struct ofproto_push push;
3352 push.packets = packets;
3356 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3357 push.ctx.resubmit_hook = push_resubmit;
3358 ofpbuf_delete(xlate_actions(&push.ctx,
3359 rule->up.actions, rule->up.n_actions));
3364 static struct rule_dpif *
3365 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3368 struct cls_rule *cls_rule;
3369 struct classifier *cls;
3371 if (table_id >= N_TABLES) {
3375 cls = &ofproto->up.tables[table_id];
3376 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3377 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3378 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3379 * are unavailable. */
3380 struct flow ofpc_normal_flow = *flow;
3381 ofpc_normal_flow.tp_src = htons(0);
3382 ofpc_normal_flow.tp_dst = htons(0);
3383 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3385 cls_rule = classifier_lookup(cls, flow);
3387 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3391 complete_operation(struct rule_dpif *rule)
3393 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3395 rule_invalidate(rule);
3397 struct dpif_completion *c = xmalloc(sizeof *c);
3398 c->op = rule->up.pending;
3399 list_push_back(&ofproto->completions, &c->list_node);
3401 ofoperation_complete(rule->up.pending, 0);
3405 static struct rule *
3408 struct rule_dpif *rule = xmalloc(sizeof *rule);
3413 rule_dealloc(struct rule *rule_)
3415 struct rule_dpif *rule = rule_dpif_cast(rule_);
3420 rule_construct(struct rule *rule_)
3422 struct rule_dpif *rule = rule_dpif_cast(rule_);
3423 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3424 struct rule_dpif *victim;
3428 error = validate_actions(rule->up.actions, rule->up.n_actions,
3429 &rule->up.cr.flow, ofproto->max_ports);
3434 rule->used = rule->up.created;
3435 rule->packet_count = 0;
3436 rule->byte_count = 0;
3438 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3439 if (victim && !list_is_empty(&victim->facets)) {
3440 struct facet *facet;
3442 rule->facets = victim->facets;
3443 list_moved(&rule->facets);
3444 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3445 /* XXX: We're only clearing our local counters here. It's possible
3446 * that quite a few packets are unaccounted for in the datapath
3447 * statistics. These will be accounted to the new rule instead of
3448 * cleared as required. This could be fixed by clearing out the
3449 * datapath statistics for this facet, but currently it doesn't
3451 facet_reset_counters(facet);
3455 /* Must avoid list_moved() in this case. */
3456 list_init(&rule->facets);
3459 table_id = rule->up.table_id;
3460 rule->tag = (victim ? victim->tag
3462 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3463 ofproto->tables[table_id].basis));
3465 complete_operation(rule);
3470 rule_destruct(struct rule *rule_)
3472 struct rule_dpif *rule = rule_dpif_cast(rule_);
3473 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3474 struct facet *facet, *next_facet;
3476 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3477 facet_revalidate(ofproto, facet);
3480 complete_operation(rule);
3484 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3486 struct rule_dpif *rule = rule_dpif_cast(rule_);
3487 struct facet *facet;
3489 /* Start from historical data for 'rule' itself that are no longer tracked
3490 * in facets. This counts, for example, facets that have expired. */
3491 *packets = rule->packet_count;
3492 *bytes = rule->byte_count;
3494 /* Add any statistics that are tracked by facets. This includes
3495 * statistical data recently updated by ofproto_update_stats() as well as
3496 * stats for packets that were executed "by hand" via dpif_execute(). */
3497 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3498 *packets += facet->packet_count;
3499 *bytes += facet->byte_count;
3504 rule_execute(struct rule *rule_, const struct flow *flow,
3505 struct ofpbuf *packet)
3507 struct rule_dpif *rule = rule_dpif_cast(rule_);
3508 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3509 struct action_xlate_ctx ctx;
3510 struct ofpbuf *odp_actions;
3511 struct facet *facet;
3514 /* First look for a related facet. If we find one, account it to that. */
3515 facet = facet_lookup_valid(ofproto, flow);
3516 if (facet && facet->rule == rule) {
3517 if (!facet->may_install) {
3518 facet_make_actions(ofproto, facet, packet);
3520 facet_execute(ofproto, facet, packet);
3524 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
3525 * create a new facet for it and use that. */
3526 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
3527 facet = facet_create(rule, flow);
3528 facet_make_actions(ofproto, facet, packet);
3529 facet_execute(ofproto, facet, packet);
3530 facet_install(ofproto, facet, true);
3534 /* We can't account anything to a facet. If we were to try, then that
3535 * facet would have a non-matching rule, busting our invariants. */
3536 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3537 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3538 size = packet->size;
3539 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3540 odp_actions->size, packet)) {
3541 rule->used = time_msec();
3542 rule->packet_count++;
3543 rule->byte_count += size;
3544 flow_push_stats(rule, flow, 1, size, rule->used);
3546 ofpbuf_delete(odp_actions);
3552 rule_modify_actions(struct rule *rule_)
3554 struct rule_dpif *rule = rule_dpif_cast(rule_);
3555 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3558 error = validate_actions(rule->up.actions, rule->up.n_actions,
3559 &rule->up.cr.flow, ofproto->max_ports);
3561 ofoperation_complete(rule->up.pending, error);
3565 complete_operation(rule);
3568 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.
3569 * Returns 0 if successful, otherwise a positive errno value. */
3571 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
3572 const struct ofpbuf *packet)
3574 struct ofpbuf key, odp_actions;
3575 struct odputil_keybuf keybuf;
3579 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3580 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3581 odp_flow_key_from_flow(&key, &flow);
3583 ofpbuf_init(&odp_actions, 32);
3584 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3586 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3587 error = dpif_execute(ofproto->dpif,
3589 odp_actions.data, odp_actions.size,
3591 ofpbuf_uninit(&odp_actions);
3594 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3595 ofproto->up.name, odp_port, strerror(error));
3600 /* OpenFlow to datapath action translation. */
3602 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3603 struct action_xlate_ctx *ctx);
3604 static void xlate_normal(struct action_xlate_ctx *);
3607 put_userspace_action(const struct ofproto_dpif *ofproto,
3608 struct ofpbuf *odp_actions,
3609 const struct flow *flow,
3610 const struct user_action_cookie *cookie)
3614 pid = dpif_port_get_pid(ofproto->dpif,
3615 ofp_port_to_odp_port(flow->in_port));
3617 return odp_put_userspace_action(pid, cookie, odp_actions);
3620 /* Compose SAMPLE action for sFlow. */
3622 compose_sflow_action(const struct ofproto_dpif *ofproto,
3623 struct ofpbuf *odp_actions,
3624 const struct flow *flow,
3627 uint32_t port_ifindex;
3628 uint32_t probability;
3629 struct user_action_cookie cookie;
3630 size_t sample_offset, actions_offset;
3631 int cookie_offset, n_output;
3633 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3637 if (odp_port == OVSP_NONE) {
3641 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3645 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3647 /* Number of packets out of UINT_MAX to sample. */
3648 probability = dpif_sflow_get_probability(ofproto->sflow);
3649 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3651 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3653 cookie.type = USER_ACTION_COOKIE_SFLOW;
3654 cookie.data = port_ifindex;
3655 cookie.n_output = n_output;
3656 cookie.vlan_tci = 0;
3657 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3659 nl_msg_end_nested(odp_actions, actions_offset);
3660 nl_msg_end_nested(odp_actions, sample_offset);
3661 return cookie_offset;
3664 /* SAMPLE action must be first action in any given list of actions.
3665 * At this point we do not have all information required to build it. So try to
3666 * build sample action as complete as possible. */
3668 add_sflow_action(struct action_xlate_ctx *ctx)
3670 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3672 &ctx->flow, OVSP_NONE);
3673 ctx->sflow_odp_port = 0;
3674 ctx->sflow_n_outputs = 0;
3677 /* Fix SAMPLE action according to data collected while composing ODP actions.
3678 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3679 * USERSPACE action's user-cookie which is required for sflow. */
3681 fix_sflow_action(struct action_xlate_ctx *ctx)
3683 const struct flow *base = &ctx->base_flow;
3684 struct user_action_cookie *cookie;
3686 if (!ctx->user_cookie_offset) {
3690 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3692 assert(cookie != NULL);
3693 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3695 if (ctx->sflow_n_outputs) {
3696 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3697 ctx->sflow_odp_port);
3699 if (ctx->sflow_n_outputs >= 255) {
3700 cookie->n_output = 255;
3702 cookie->n_output = ctx->sflow_n_outputs;
3704 cookie->vlan_tci = base->vlan_tci;
3708 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3709 const void *key, size_t key_size)
3711 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3712 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3713 nl_msg_end_nested(odp_actions, offset);
3717 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3718 struct ofpbuf *odp_actions)
3720 if (base->tun_id == flow->tun_id) {
3723 base->tun_id = flow->tun_id;
3725 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
3726 &base->tun_id, sizeof(base->tun_id));
3730 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3731 struct ofpbuf *odp_actions)
3733 struct ovs_key_ethernet eth_key;
3735 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3736 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3740 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3741 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3743 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3744 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3746 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3747 ð_key, sizeof(eth_key));
3751 commit_vlan_action(const struct flow *flow, struct flow *base,
3752 struct ofpbuf *odp_actions)
3754 if (base->vlan_tci == flow->vlan_tci) {
3758 if (base->vlan_tci & htons(VLAN_CFI)) {
3759 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3762 if (flow->vlan_tci & htons(VLAN_CFI)) {
3763 struct ovs_action_push_vlan vlan;
3765 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3766 vlan.vlan_tci = flow->vlan_tci;
3767 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3768 &vlan, sizeof vlan);
3770 base->vlan_tci = flow->vlan_tci;
3774 commit_set_nw_action(const struct flow *flow, struct flow *base,
3775 struct ofpbuf *odp_actions)
3777 struct ovs_key_ipv4 ipv4_key;
3779 if (base->dl_type != htons(ETH_TYPE_IP) ||
3780 !base->nw_src || !base->nw_dst) {
3784 if (base->nw_src == flow->nw_src &&
3785 base->nw_dst == flow->nw_dst &&
3786 base->nw_tos == flow->nw_tos &&
3787 base->nw_ttl == flow->nw_ttl &&
3788 base->nw_frag == flow->nw_frag) {
3792 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3793 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3794 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3795 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3796 ipv4_key.ipv4_proto = base->nw_proto;
3797 ipv4_key.ipv4_frag = (base->nw_frag == 0 ? OVS_FRAG_TYPE_NONE
3798 : base->nw_frag == FLOW_NW_FRAG_ANY
3799 ? OVS_FRAG_TYPE_FIRST : OVS_FRAG_TYPE_LATER);
3801 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3802 &ipv4_key, sizeof(ipv4_key));
3806 commit_set_port_action(const struct flow *flow, struct flow *base,
3807 struct ofpbuf *odp_actions)
3809 if (!base->tp_src || !base->tp_dst) {
3813 if (base->tp_src == flow->tp_src &&
3814 base->tp_dst == flow->tp_dst) {
3818 if (flow->nw_proto == IPPROTO_TCP) {
3819 struct ovs_key_tcp port_key;
3821 port_key.tcp_src = base->tp_src = flow->tp_src;
3822 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3824 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3825 &port_key, sizeof(port_key));
3827 } else if (flow->nw_proto == IPPROTO_UDP) {
3828 struct ovs_key_udp port_key;
3830 port_key.udp_src = base->tp_src = flow->tp_src;
3831 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3833 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3834 &port_key, sizeof(port_key));
3839 commit_set_priority_action(const struct flow *flow, struct flow *base,
3840 struct ofpbuf *odp_actions)
3842 if (base->priority == flow->priority) {
3845 base->priority = flow->priority;
3847 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3848 &base->priority, sizeof(base->priority));
3852 commit_odp_actions(struct action_xlate_ctx *ctx)
3854 const struct flow *flow = &ctx->flow;
3855 struct flow *base = &ctx->base_flow;
3856 struct ofpbuf *odp_actions = ctx->odp_actions;
3858 commit_set_tun_id_action(flow, base, odp_actions);
3859 commit_set_ether_addr_action(flow, base, odp_actions);
3860 commit_vlan_action(flow, base, odp_actions);
3861 commit_set_nw_action(flow, base, odp_actions);
3862 commit_set_port_action(flow, base, odp_actions);
3863 commit_set_priority_action(flow, base, odp_actions);
3867 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
3870 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
3871 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
3872 uint8_t flow_nw_tos = ctx->flow.nw_tos;
3875 struct priority_to_dscp *pdscp;
3877 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
3878 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
3882 pdscp = get_priority(ofport, ctx->flow.priority);
3884 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
3885 ctx->flow.nw_tos |= pdscp->dscp;
3888 /* We may not have an ofport record for this port, but it doesn't hurt
3889 * to allow forwarding to it anyhow. Maybe such a port will appear
3890 * later and we're pre-populating the flow table. */
3893 commit_odp_actions(ctx);
3894 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3895 ctx->sflow_odp_port = odp_port;
3896 ctx->sflow_n_outputs++;
3897 ctx->nf_output_iface = ofp_port;
3898 ctx->flow.nw_tos = flow_nw_tos;
3902 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
3904 compose_output_action__(ctx, ofp_port, true);
3908 xlate_table_action(struct action_xlate_ctx *ctx,
3909 uint16_t in_port, uint8_t table_id)
3911 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3912 struct ofproto_dpif *ofproto = ctx->ofproto;
3913 struct rule_dpif *rule;
3914 uint16_t old_in_port;
3915 uint8_t old_table_id;
3917 old_table_id = ctx->table_id;
3918 ctx->table_id = table_id;
3920 /* Look up a flow with 'in_port' as the input port. */
3921 old_in_port = ctx->flow.in_port;
3922 ctx->flow.in_port = in_port;
3923 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3926 if (table_id > 0 && table_id < N_TABLES) {
3927 struct table_dpif *table = &ofproto->tables[table_id];
3928 if (table->other_table) {
3931 : rule_calculate_tag(&ctx->flow,
3932 &table->other_table->wc,
3937 /* Restore the original input port. Otherwise OFPP_NORMAL and
3938 * OFPP_IN_PORT will have surprising behavior. */
3939 ctx->flow.in_port = old_in_port;
3941 if (ctx->resubmit_hook) {
3942 ctx->resubmit_hook(ctx, rule);
3947 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3951 ctx->table_id = old_table_id;
3953 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3955 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3956 MAX_RESUBMIT_RECURSION);
3961 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3962 const struct nx_action_resubmit *nar)
3967 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3969 : ntohs(nar->in_port));
3970 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3972 xlate_table_action(ctx, in_port, table_id);
3976 flood_packets(struct action_xlate_ctx *ctx, bool all)
3978 struct ofport_dpif *ofport;
3980 commit_odp_actions(ctx);
3981 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3982 uint16_t ofp_port = ofport->up.ofp_port;
3984 if (ofp_port == ctx->flow.in_port) {
3989 compose_output_action__(ctx, ofp_port, false);
3990 } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) {
3991 compose_output_action(ctx, ofp_port);
3995 ctx->nf_output_iface = NF_OUT_FLOOD;
3999 compose_controller_action(struct action_xlate_ctx *ctx, int len)
4001 struct user_action_cookie cookie;
4003 commit_odp_actions(ctx);
4004 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
4006 cookie.n_output = 0;
4007 cookie.vlan_tci = 0;
4008 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
4012 xlate_output_action__(struct action_xlate_ctx *ctx,
4013 uint16_t port, uint16_t max_len)
4015 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
4017 ctx->nf_output_iface = NF_OUT_DROP;
4021 compose_output_action(ctx, ctx->flow.in_port);
4024 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
4030 flood_packets(ctx, false);
4033 flood_packets(ctx, true);
4035 case OFPP_CONTROLLER:
4036 compose_controller_action(ctx, max_len);
4039 compose_output_action(ctx, OFPP_LOCAL);
4044 if (port != ctx->flow.in_port) {
4045 compose_output_action(ctx, port);
4050 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4051 ctx->nf_output_iface = NF_OUT_FLOOD;
4052 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4053 ctx->nf_output_iface = prev_nf_output_iface;
4054 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4055 ctx->nf_output_iface != NF_OUT_FLOOD) {
4056 ctx->nf_output_iface = NF_OUT_MULTI;
4061 xlate_output_reg_action(struct action_xlate_ctx *ctx,
4062 const struct nx_action_output_reg *naor)
4066 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
4068 if (ofp_port <= UINT16_MAX) {
4069 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
4074 xlate_output_action(struct action_xlate_ctx *ctx,
4075 const struct ofp_action_output *oao)
4077 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
4081 xlate_enqueue_action(struct action_xlate_ctx *ctx,
4082 const struct ofp_action_enqueue *oae)
4085 uint32_t flow_priority, priority;
4088 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4091 /* Fall back to ordinary output action. */
4092 xlate_output_action__(ctx, ntohs(oae->port), 0);
4096 /* Figure out datapath output port. */
4097 ofp_port = ntohs(oae->port);
4098 if (ofp_port == OFPP_IN_PORT) {
4099 ofp_port = ctx->flow.in_port;
4100 } else if (ofp_port == ctx->flow.in_port) {
4104 /* Add datapath actions. */
4105 flow_priority = ctx->flow.priority;
4106 ctx->flow.priority = priority;
4107 compose_output_action(ctx, ofp_port);
4108 ctx->flow.priority = flow_priority;
4110 /* Update NetFlow output port. */
4111 if (ctx->nf_output_iface == NF_OUT_DROP) {
4112 ctx->nf_output_iface = ofp_port;
4113 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4114 ctx->nf_output_iface = NF_OUT_MULTI;
4119 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4120 const struct nx_action_set_queue *nasq)
4125 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4128 /* Couldn't translate queue to a priority, so ignore. A warning
4129 * has already been logged. */
4133 ctx->flow.priority = priority;
4136 struct xlate_reg_state {
4142 xlate_autopath(struct action_xlate_ctx *ctx,
4143 const struct nx_action_autopath *naa)
4145 uint16_t ofp_port = ntohl(naa->id);
4146 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4148 if (!port || !port->bundle) {
4149 ofp_port = OFPP_NONE;
4150 } else if (port->bundle->bond) {
4151 /* Autopath does not support VLAN hashing. */
4152 struct ofport_dpif *slave = bond_choose_output_slave(
4153 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4155 ofp_port = slave->up.ofp_port;
4158 autopath_execute(naa, &ctx->flow, ofp_port);
4162 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4164 struct ofproto_dpif *ofproto = ofproto_;
4165 struct ofport_dpif *port;
4175 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4178 port = get_ofp_port(ofproto, ofp_port);
4179 return port ? port->may_enable : false;
4184 xlate_learn_action(struct action_xlate_ctx *ctx,
4185 const struct nx_action_learn *learn)
4187 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4188 struct ofputil_flow_mod fm;
4191 learn_execute(learn, &ctx->flow, &fm);
4193 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4194 if (error && !VLOG_DROP_WARN(&rl)) {
4195 char *msg = ofputil_error_to_string(error);
4196 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4204 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4206 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4207 ? htonl(OFPPC_NO_RECV_STP)
4208 : htonl(OFPPC_NO_RECV))) {
4212 /* Only drop packets here if both forwarding and learning are
4213 * disabled. If just learning is enabled, we need to have
4214 * OFPP_NORMAL and the learning action have a look at the packet
4215 * before we can drop it. */
4216 if (!stp_forward_in_state(port->stp_state)
4217 && !stp_learn_in_state(port->stp_state)) {
4225 do_xlate_actions(const union ofp_action *in, size_t n_in,
4226 struct action_xlate_ctx *ctx)
4228 const struct ofport_dpif *port;
4229 const union ofp_action *ia;
4232 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4233 if (port && !may_receive(port, ctx)) {
4234 /* Drop this flow. */
4238 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4239 const struct ofp_action_dl_addr *oada;
4240 const struct nx_action_resubmit *nar;
4241 const struct nx_action_set_tunnel *nast;
4242 const struct nx_action_set_queue *nasq;
4243 const struct nx_action_multipath *nam;
4244 const struct nx_action_autopath *naa;
4245 const struct nx_action_bundle *nab;
4246 const struct nx_action_output_reg *naor;
4247 enum ofputil_action_code code;
4254 code = ofputil_decode_action_unsafe(ia);
4256 case OFPUTIL_OFPAT_OUTPUT:
4257 xlate_output_action(ctx, &ia->output);
4260 case OFPUTIL_OFPAT_SET_VLAN_VID:
4261 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4262 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4265 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4266 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4267 ctx->flow.vlan_tci |= htons(
4268 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4271 case OFPUTIL_OFPAT_STRIP_VLAN:
4272 ctx->flow.vlan_tci = htons(0);
4275 case OFPUTIL_OFPAT_SET_DL_SRC:
4276 oada = ((struct ofp_action_dl_addr *) ia);
4277 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4280 case OFPUTIL_OFPAT_SET_DL_DST:
4281 oada = ((struct ofp_action_dl_addr *) ia);
4282 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4285 case OFPUTIL_OFPAT_SET_NW_SRC:
4286 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4289 case OFPUTIL_OFPAT_SET_NW_DST:
4290 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4293 case OFPUTIL_OFPAT_SET_NW_TOS:
4294 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4295 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4298 case OFPUTIL_OFPAT_SET_TP_SRC:
4299 ctx->flow.tp_src = ia->tp_port.tp_port;
4302 case OFPUTIL_OFPAT_SET_TP_DST:
4303 ctx->flow.tp_dst = ia->tp_port.tp_port;
4306 case OFPUTIL_OFPAT_ENQUEUE:
4307 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4310 case OFPUTIL_NXAST_RESUBMIT:
4311 nar = (const struct nx_action_resubmit *) ia;
4312 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4315 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4316 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4319 case OFPUTIL_NXAST_SET_TUNNEL:
4320 nast = (const struct nx_action_set_tunnel *) ia;
4321 tun_id = htonll(ntohl(nast->tun_id));
4322 ctx->flow.tun_id = tun_id;
4325 case OFPUTIL_NXAST_SET_QUEUE:
4326 nasq = (const struct nx_action_set_queue *) ia;
4327 xlate_set_queue_action(ctx, nasq);
4330 case OFPUTIL_NXAST_POP_QUEUE:
4331 ctx->flow.priority = ctx->original_priority;
4334 case OFPUTIL_NXAST_REG_MOVE:
4335 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4339 case OFPUTIL_NXAST_REG_LOAD:
4340 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4344 case OFPUTIL_NXAST_NOTE:
4345 /* Nothing to do. */
4348 case OFPUTIL_NXAST_SET_TUNNEL64:
4349 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4350 ctx->flow.tun_id = tun_id;
4353 case OFPUTIL_NXAST_MULTIPATH:
4354 nam = (const struct nx_action_multipath *) ia;
4355 multipath_execute(nam, &ctx->flow);
4358 case OFPUTIL_NXAST_AUTOPATH:
4359 naa = (const struct nx_action_autopath *) ia;
4360 xlate_autopath(ctx, naa);
4363 case OFPUTIL_NXAST_BUNDLE:
4364 ctx->ofproto->has_bundle_action = true;
4365 nab = (const struct nx_action_bundle *) ia;
4366 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4371 case OFPUTIL_NXAST_BUNDLE_LOAD:
4372 ctx->ofproto->has_bundle_action = true;
4373 nab = (const struct nx_action_bundle *) ia;
4374 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4378 case OFPUTIL_NXAST_OUTPUT_REG:
4379 naor = (const struct nx_action_output_reg *) ia;
4380 xlate_output_reg_action(ctx, naor);
4383 case OFPUTIL_NXAST_LEARN:
4384 ctx->has_learn = true;
4385 if (ctx->may_learn) {
4386 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4390 case OFPUTIL_NXAST_EXIT:
4396 /* We've let OFPP_NORMAL and the learning action look at the packet,
4397 * so drop it now if forwarding is disabled. */
4398 if (port && !stp_forward_in_state(port->stp_state)) {
4399 ofpbuf_clear(ctx->odp_actions);
4400 add_sflow_action(ctx);
4405 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4406 struct ofproto_dpif *ofproto, const struct flow *flow,
4407 const struct ofpbuf *packet)
4409 ctx->ofproto = ofproto;
4411 ctx->packet = packet;
4412 ctx->may_learn = packet != NULL;
4413 ctx->resubmit_hook = NULL;
4416 static struct ofpbuf *
4417 xlate_actions(struct action_xlate_ctx *ctx,
4418 const union ofp_action *in, size_t n_in)
4420 COVERAGE_INC(ofproto_dpif_xlate);
4422 ctx->odp_actions = ofpbuf_new(512);
4423 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4425 ctx->may_set_up_flow = true;
4426 ctx->has_learn = false;
4427 ctx->has_normal = false;
4428 ctx->nf_output_iface = NF_OUT_DROP;
4430 ctx->original_priority = ctx->flow.priority;
4431 ctx->base_flow = ctx->flow;
4432 ctx->base_flow.tun_id = 0;
4436 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4437 switch (ctx->ofproto->up.frag_handling) {
4438 case OFPC_FRAG_NORMAL:
4439 /* We must pretend that transport ports are unavailable. */
4440 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4441 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4444 case OFPC_FRAG_DROP:
4445 return ctx->odp_actions;
4447 case OFPC_FRAG_REASM:
4450 case OFPC_FRAG_NX_MATCH:
4451 /* Nothing to do. */
4456 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4457 ctx->may_set_up_flow = false;
4458 return ctx->odp_actions;
4460 add_sflow_action(ctx);
4461 do_xlate_actions(in, n_in, ctx);
4463 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4464 ctx->odp_actions->data,
4465 ctx->odp_actions->size)) {
4466 ctx->may_set_up_flow = false;
4468 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4470 compose_output_action(ctx, OFPP_LOCAL);
4473 fix_sflow_action(ctx);
4476 return ctx->odp_actions;
4479 /* OFPP_NORMAL implementation. */
4481 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4483 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4484 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4485 * the bundle on which the packet was received, returns the VLAN to which the
4488 * Both 'vid' and the return value are in the range 0...4095. */
4490 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4492 switch (in_bundle->vlan_mode) {
4493 case PORT_VLAN_ACCESS:
4494 return in_bundle->vlan;
4497 case PORT_VLAN_TRUNK:
4500 case PORT_VLAN_NATIVE_UNTAGGED:
4501 case PORT_VLAN_NATIVE_TAGGED:
4502 return vid ? vid : in_bundle->vlan;
4509 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4510 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4513 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4514 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4517 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4519 switch (in_bundle->vlan_mode) {
4520 case PORT_VLAN_ACCESS:
4523 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4524 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4525 "packet received on port %s configured as VLAN "
4526 "%"PRIu16" access port",
4527 in_bundle->ofproto->up.name, vid,
4528 in_bundle->name, in_bundle->vlan);
4534 case PORT_VLAN_NATIVE_UNTAGGED:
4535 case PORT_VLAN_NATIVE_TAGGED:
4537 /* Port must always carry its native VLAN. */
4541 case PORT_VLAN_TRUNK:
4542 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4544 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4545 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4546 "received on port %s not configured for trunking "
4548 in_bundle->ofproto->up.name, vid,
4549 in_bundle->name, vid);
4561 /* Given 'vlan', the VLAN that a packet belongs to, and
4562 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4563 * that should be included in the 802.1Q header. (If the return value is 0,
4564 * then the 802.1Q header should only be included in the packet if there is a
4567 * Both 'vlan' and the return value are in the range 0...4095. */
4569 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4571 switch (out_bundle->vlan_mode) {
4572 case PORT_VLAN_ACCESS:
4575 case PORT_VLAN_TRUNK:
4576 case PORT_VLAN_NATIVE_TAGGED:
4579 case PORT_VLAN_NATIVE_UNTAGGED:
4580 return vlan == out_bundle->vlan ? 0 : vlan;
4588 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
4591 struct ofport_dpif *port;
4593 ovs_be16 tci, old_tci;
4595 vid = output_vlan_to_vid(out_bundle, vlan);
4596 if (!out_bundle->bond) {
4597 port = ofbundle_get_a_port(out_bundle);
4599 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4602 /* No slaves enabled, so drop packet. */
4607 old_tci = ctx->flow.vlan_tci;
4609 if (tci || out_bundle->use_priority_tags) {
4610 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4612 tci |= htons(VLAN_CFI);
4615 ctx->flow.vlan_tci = tci;
4617 compose_output_action(ctx, port->up.ofp_port);
4618 ctx->flow.vlan_tci = old_tci;
4622 mirror_mask_ffs(mirror_mask_t mask)
4624 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4629 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4631 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4632 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4636 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4638 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4641 /* Returns an arbitrary interface within 'bundle'. */
4642 static struct ofport_dpif *
4643 ofbundle_get_a_port(const struct ofbundle *bundle)
4645 return CONTAINER_OF(list_front(&bundle->ports),
4646 struct ofport_dpif, bundle_node);
4649 static mirror_mask_t
4650 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4651 const struct ofbundle *in_bundle,
4652 const struct ofbundle *out_bundle)
4654 mirror_mask_t dst_mirrors = 0;
4656 if (out_bundle == OFBUNDLE_FLOOD) {
4657 struct ofbundle *bundle;
4659 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4660 if (bundle != in_bundle
4661 && ofbundle_includes_vlan(bundle, vlan)
4662 && bundle->floodable
4663 && !bundle->mirror_out) {
4664 output_normal(ctx, bundle, vlan);
4665 dst_mirrors |= bundle->dst_mirrors;
4668 ctx->nf_output_iface = NF_OUT_FLOOD;
4669 } else if (out_bundle) {
4670 output_normal(ctx, out_bundle, vlan);
4671 dst_mirrors = out_bundle->dst_mirrors;
4678 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4680 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4683 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4684 * to a VLAN. In general most packets may be mirrored but we want to drop
4685 * protocols that may confuse switches. */
4687 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4689 /* If you change this function's behavior, please update corresponding
4690 * documentation in vswitch.xml at the same time. */
4691 if (dst[0] != 0x01) {
4692 /* All the currently banned MACs happen to start with 01 currently, so
4693 * this is a quick way to eliminate most of the good ones. */
4695 if (eth_addr_is_reserved(dst)) {
4696 /* Drop STP, IEEE pause frames, and other reserved protocols
4697 * (01-80-c2-00-00-0x). */
4701 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4703 if ((dst[3] & 0xfe) == 0xcc &&
4704 (dst[4] & 0xfe) == 0xcc &&
4705 (dst[5] & 0xfe) == 0xcc) {
4706 /* Drop the following protocols plus others following the same
4709 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4710 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4711 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4715 if (!(dst[3] | dst[4] | dst[5])) {
4716 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4725 output_mirrors(struct action_xlate_ctx *ctx,
4726 uint16_t vlan, const struct ofbundle *in_bundle,
4727 mirror_mask_t dst_mirrors)
4729 struct ofproto_dpif *ofproto = ctx->ofproto;
4730 mirror_mask_t mirrors;
4732 mirrors = in_bundle->src_mirrors | dst_mirrors;
4740 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4742 if (!vlan_is_mirrored(m, vlan)) {
4743 mirrors &= mirrors - 1;
4747 mirrors &= ~m->dup_mirrors;
4749 output_normal(ctx, m->out, vlan);
4750 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)
4751 && vlan != m->out_vlan) {
4752 struct ofbundle *bundle;
4754 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4755 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4756 && !bundle->mirror_out) {
4757 output_normal(ctx, bundle, m->out_vlan);
4764 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4765 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4766 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4768 is_gratuitous_arp(const struct flow *flow)
4770 return (flow->dl_type == htons(ETH_TYPE_ARP)
4771 && eth_addr_is_broadcast(flow->dl_dst)
4772 && (flow->nw_proto == ARP_OP_REPLY
4773 || (flow->nw_proto == ARP_OP_REQUEST
4774 && flow->nw_src == flow->nw_dst)));
4778 update_learning_table(struct ofproto_dpif *ofproto,
4779 const struct flow *flow, int vlan,
4780 struct ofbundle *in_bundle)
4782 struct mac_entry *mac;
4784 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4788 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4789 if (is_gratuitous_arp(flow)) {
4790 /* We don't want to learn from gratuitous ARP packets that are
4791 * reflected back over bond slaves so we lock the learning table. */
4792 if (!in_bundle->bond) {
4793 mac_entry_set_grat_arp_lock(mac);
4794 } else if (mac_entry_is_grat_arp_locked(mac)) {
4799 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4800 /* The log messages here could actually be useful in debugging,
4801 * so keep the rate limit relatively high. */
4802 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4803 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4804 "on port %s in VLAN %d",
4805 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4806 in_bundle->name, vlan);
4808 mac->port.p = in_bundle;
4809 tag_set_add(&ofproto->revalidate_set,
4810 mac_learning_changed(ofproto->ml, mac));
4814 static struct ofport_dpif *
4815 lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn)
4817 struct ofport_dpif *ofport;
4819 /* Find the port and bundle for the received packet. */
4820 ofport = get_ofp_port(ofproto, in_port);
4821 if (ofport && ofport->bundle) {
4825 /* Odd. A few possible reasons here:
4827 * - We deleted a port but there are still a few packets queued up
4830 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4831 * we don't know about.
4833 * - The ofproto client didn't configure the port as part of a bundle.
4836 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4838 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4839 "port %"PRIu16, ofproto->up.name, in_port);
4844 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
4845 * dropped. Returns true if they may be forwarded, false if they should be
4848 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
4849 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
4851 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
4852 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
4853 * checked by input_vid_is_valid().
4855 * May also add tags to '*tags', although the current implementation only does
4856 * so in one special case.
4859 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
4860 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
4862 struct ofbundle *in_bundle = in_port->bundle;
4864 /* Drop frames for reserved multicast addresses
4865 * only if forward_bpdu option is absent. */
4866 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
4870 if (in_bundle->bond) {
4871 struct mac_entry *mac;
4873 switch (bond_check_admissibility(in_bundle->bond, in_port,
4874 flow->dl_dst, tags)) {
4881 case BV_DROP_IF_MOVED:
4882 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
4883 if (mac && mac->port.p != in_bundle &&
4884 (!is_gratuitous_arp(flow)
4885 || mac_entry_is_grat_arp_locked(mac))) {
4896 xlate_normal(struct action_xlate_ctx *ctx)
4898 mirror_mask_t dst_mirrors = 0;
4899 struct ofport_dpif *in_port;
4900 struct ofbundle *in_bundle;
4901 struct ofbundle *out_bundle;
4902 struct mac_entry *mac;
4906 ctx->has_normal = true;
4908 /* Obtain in_port from ctx->flow.in_port.
4910 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
4911 in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
4912 ctx->packet != NULL);
4916 in_bundle = in_port->bundle;
4918 /* Drop malformed frames. */
4919 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
4920 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
4921 if (ctx->packet != NULL) {
4922 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4923 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
4924 "VLAN tag received on port %s",
4925 ctx->ofproto->up.name, in_bundle->name);
4930 /* Drop frames on bundles reserved for mirroring. */
4931 if (in_bundle->mirror_out) {
4932 if (ctx->packet != NULL) {
4933 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4934 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4935 "%s, which is reserved exclusively for mirroring",
4936 ctx->ofproto->up.name, in_bundle->name);
4942 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4943 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
4946 vlan = input_vid_to_vlan(in_bundle, vid);
4948 /* Check other admissibility requirements. */
4949 if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
4950 output_mirrors(ctx, vlan, in_bundle, 0);
4954 /* Learn source MAC. */
4955 if (ctx->may_learn) {
4956 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
4959 /* Determine output bundle. */
4960 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
4963 out_bundle = mac->port.p;
4964 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4965 /* If we are revalidating but don't have a learning entry then eject
4966 * the flow. Installing a flow that floods packets opens up a window
4967 * of time where we could learn from a packet reflected on a bond and
4968 * blackhole packets before the learning table is updated to reflect
4969 * the correct port. */
4970 ctx->may_set_up_flow = false;
4973 out_bundle = OFBUNDLE_FLOOD;
4976 /* Don't send packets out their input bundles. */
4977 if (in_bundle != out_bundle) {
4978 dst_mirrors = compose_dsts(ctx, vlan, in_bundle, out_bundle);
4980 output_mirrors(ctx, vlan, in_bundle, dst_mirrors);
4983 /* Optimized flow revalidation.
4985 * It's a difficult problem, in general, to tell which facets need to have
4986 * their actions recalculated whenever the OpenFlow flow table changes. We
4987 * don't try to solve that general problem: for most kinds of OpenFlow flow
4988 * table changes, we recalculate the actions for every facet. This is
4989 * relatively expensive, but it's good enough if the OpenFlow flow table
4990 * doesn't change very often.
4992 * However, we can expect one particular kind of OpenFlow flow table change to
4993 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4994 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4995 * table, we add a special case that applies to flow tables in which every rule
4996 * has the same form (that is, the same wildcards), except that the table is
4997 * also allowed to have a single "catch-all" flow that matches all packets. We
4998 * optimize this case by tagging all of the facets that resubmit into the table
4999 * and invalidating the same tag whenever a flow changes in that table. The
5000 * end result is that we revalidate just the facets that need it (and sometimes
5001 * a few more, but not all of the facets or even all of the facets that
5002 * resubmit to the table modified by MAC learning). */
5004 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5005 * into an OpenFlow table with the given 'basis'. */
5007 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5010 if (flow_wildcards_is_catchall(wc)) {
5013 struct flow tag_flow = *flow;
5014 flow_zero_wildcards(&tag_flow, wc);
5015 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5019 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5020 * taggability of that table.
5022 * This function must be called after *each* change to a flow table. If you
5023 * skip calling it on some changes then the pointer comparisons at the end can
5024 * be invalid if you get unlucky. For example, if a flow removal causes a
5025 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5026 * different wildcards to be created with the same address, then this function
5027 * will incorrectly skip revalidation. */
5029 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5031 struct table_dpif *table = &ofproto->tables[table_id];
5032 const struct classifier *cls = &ofproto->up.tables[table_id];
5033 struct cls_table *catchall, *other;
5034 struct cls_table *t;
5036 catchall = other = NULL;
5038 switch (hmap_count(&cls->tables)) {
5040 /* We could tag this OpenFlow table but it would make the logic a
5041 * little harder and it's a corner case that doesn't seem worth it
5047 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5048 if (cls_table_is_catchall(t)) {
5050 } else if (!other) {
5053 /* Indicate that we can't tag this by setting both tables to
5054 * NULL. (We know that 'catchall' is already NULL.) */
5061 /* Can't tag this table. */
5065 if (table->catchall_table != catchall || table->other_table != other) {
5066 table->catchall_table = catchall;
5067 table->other_table = other;
5068 ofproto->need_revalidate = true;
5072 /* Given 'rule' that has changed in some way (either it is a rule being
5073 * inserted, a rule being deleted, or a rule whose actions are being
5074 * modified), marks facets for revalidation to ensure that packets will be
5075 * forwarded correctly according to the new state of the flow table.
5077 * This function must be called after *each* change to a flow table. See
5078 * the comment on table_update_taggable() for more information. */
5080 rule_invalidate(const struct rule_dpif *rule)
5082 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5084 table_update_taggable(ofproto, rule->up.table_id);
5086 if (!ofproto->need_revalidate) {
5087 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5089 if (table->other_table && rule->tag) {
5090 tag_set_add(&ofproto->revalidate_set, rule->tag);
5092 ofproto->need_revalidate = true;
5098 set_frag_handling(struct ofproto *ofproto_,
5099 enum ofp_config_flags frag_handling)
5101 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5103 if (frag_handling != OFPC_FRAG_REASM) {
5104 ofproto->need_revalidate = true;
5112 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5113 const struct flow *flow,
5114 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5116 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5119 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5120 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5123 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5124 ofproto->max_ports);
5126 struct odputil_keybuf keybuf;
5127 struct action_xlate_ctx ctx;
5128 struct ofpbuf *odp_actions;
5131 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5132 odp_flow_key_from_flow(&key, flow);
5134 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
5135 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5136 dpif_execute(ofproto->dpif, key.data, key.size,
5137 odp_actions->data, odp_actions->size, packet);
5138 ofpbuf_delete(odp_actions);
5144 get_netflow_ids(const struct ofproto *ofproto_,
5145 uint8_t *engine_type, uint8_t *engine_id)
5147 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5149 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5152 static struct ofproto_dpif *
5153 ofproto_dpif_lookup(const char *name)
5155 struct ofproto *ofproto = ofproto_lookup(name);
5156 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5157 ? ofproto_dpif_cast(ofproto)
5162 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5163 const char *args, void *aux OVS_UNUSED)
5165 const struct ofproto_dpif *ofproto;
5167 ofproto = ofproto_dpif_lookup(args);
5169 unixctl_command_reply(conn, 501, "no such bridge");
5172 mac_learning_flush(ofproto->ml);
5174 unixctl_command_reply(conn, 200, "table successfully flushed");
5178 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5179 const char *args, void *aux OVS_UNUSED)
5181 struct ds ds = DS_EMPTY_INITIALIZER;
5182 const struct ofproto_dpif *ofproto;
5183 const struct mac_entry *e;
5185 ofproto = ofproto_dpif_lookup(args);
5187 unixctl_command_reply(conn, 501, "no such bridge");
5191 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5192 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5193 struct ofbundle *bundle = e->port.p;
5194 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5195 ofbundle_get_a_port(bundle)->odp_port,
5196 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5198 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5202 struct ofproto_trace {
5203 struct action_xlate_ctx ctx;
5209 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5210 const struct rule_dpif *rule)
5212 ds_put_char_multiple(result, '\t', level);
5214 ds_put_cstr(result, "No match\n");
5218 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5219 table_id, ntohll(rule->up.flow_cookie));
5220 cls_rule_format(&rule->up.cr, result);
5221 ds_put_char(result, '\n');
5223 ds_put_char_multiple(result, '\t', level);
5224 ds_put_cstr(result, "OpenFlow ");
5225 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5226 ds_put_char(result, '\n');
5230 trace_format_flow(struct ds *result, int level, const char *title,
5231 struct ofproto_trace *trace)
5233 ds_put_char_multiple(result, '\t', level);
5234 ds_put_format(result, "%s: ", title);
5235 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5236 ds_put_cstr(result, "unchanged");
5238 flow_format(result, &trace->ctx.flow);
5239 trace->flow = trace->ctx.flow;
5241 ds_put_char(result, '\n');
5245 trace_format_regs(struct ds *result, int level, const char *title,
5246 struct ofproto_trace *trace)
5250 ds_put_char_multiple(result, '\t', level);
5251 ds_put_format(result, "%s:", title);
5252 for (i = 0; i < FLOW_N_REGS; i++) {
5253 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5255 ds_put_char(result, '\n');
5259 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5261 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5262 struct ds *result = trace->result;
5264 ds_put_char(result, '\n');
5265 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5266 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5267 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5271 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5272 void *aux OVS_UNUSED)
5274 char *dpname, *arg1, *arg2, *arg3, *arg4;
5275 char *args = xstrdup(args_);
5276 char *save_ptr = NULL;
5277 struct ofproto_dpif *ofproto;
5278 struct ofpbuf odp_key;
5279 struct ofpbuf *packet;
5280 struct rule_dpif *rule;
5286 ofpbuf_init(&odp_key, 0);
5289 dpname = strtok_r(args, " ", &save_ptr);
5290 arg1 = strtok_r(NULL, " ", &save_ptr);
5291 arg2 = strtok_r(NULL, " ", &save_ptr);
5292 arg3 = strtok_r(NULL, " ", &save_ptr);
5293 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5294 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5295 /* ofproto/trace dpname flow [-generate] */
5298 /* Convert string to datapath key. */
5299 ofpbuf_init(&odp_key, 0);
5300 error = odp_flow_key_from_string(arg1, NULL, &odp_key);
5302 unixctl_command_reply(conn, 501, "Bad flow syntax");
5306 /* Convert odp_key to flow. */
5307 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
5309 unixctl_command_reply(conn, 501, "Invalid flow");
5313 /* Generate a packet, if requested. */
5315 packet = ofpbuf_new(0);
5316 flow_compose(packet, &flow);
5318 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5319 /* ofproto/trace dpname priority tun_id in_port packet */
5324 priority = atoi(arg1);
5325 tun_id = htonll(strtoull(arg2, NULL, 0));
5326 in_port = ofp_port_to_odp_port(atoi(arg3));
5328 packet = ofpbuf_new(strlen(args) / 2);
5329 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5330 arg4 += strspn(arg4, " ");
5331 if (*arg4 != '\0') {
5332 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5335 if (packet->size < ETH_HEADER_LEN) {
5336 unixctl_command_reply(conn, 501,
5337 "Packet data too short for Ethernet");
5341 ds_put_cstr(&result, "Packet: ");
5342 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5343 ds_put_cstr(&result, s);
5346 flow_extract(packet, priority, tun_id, in_port, &flow);
5348 unixctl_command_reply(conn, 501, "Bad command syntax");
5352 ofproto = ofproto_dpif_lookup(dpname);
5354 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5359 ds_put_cstr(&result, "Flow: ");
5360 flow_format(&result, &flow);
5361 ds_put_char(&result, '\n');
5363 rule = rule_dpif_lookup(ofproto, &flow, 0);
5364 trace_format_rule(&result, 0, 0, rule);
5366 struct ofproto_trace trace;
5367 struct ofpbuf *odp_actions;
5369 trace.result = &result;
5371 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5372 trace.ctx.resubmit_hook = trace_resubmit;
5373 odp_actions = xlate_actions(&trace.ctx,
5374 rule->up.actions, rule->up.n_actions);
5376 ds_put_char(&result, '\n');
5377 trace_format_flow(&result, 0, "Final flow", &trace);
5378 ds_put_cstr(&result, "Datapath actions: ");
5379 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5380 ofpbuf_delete(odp_actions);
5382 if (!trace.ctx.may_set_up_flow) {
5384 ds_put_cstr(&result, "\nThis flow is not cachable.");
5386 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5387 "for complete actions, please supply a packet.");
5392 unixctl_command_reply(conn, 200, ds_cstr(&result));
5395 ds_destroy(&result);
5396 ofpbuf_delete(packet);
5397 ofpbuf_uninit(&odp_key);
5402 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5403 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5406 unixctl_command_reply(conn, 200, NULL);
5410 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5411 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5414 unixctl_command_reply(conn, 200, NULL);
5418 ofproto_dpif_unixctl_init(void)
5420 static bool registered;
5426 unixctl_command_register("ofproto/trace",
5427 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5428 ofproto_unixctl_trace, NULL);
5429 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5431 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5433 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5434 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5437 const struct ofproto_class ofproto_dpif_class = {
5464 port_is_lacp_current,
5465 NULL, /* rule_choose_table */
5472 rule_modify_actions,
5480 get_cfm_remote_mpids,
5484 get_stp_port_status,
5490 is_mirror_output_bundle,
5491 forward_bpdu_changed,