2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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 "meta-flow.h"
38 #include "multipath.h"
45 #include "ofp-actions.h"
46 #include "ofp-parse.h"
47 #include "ofp-print.h"
48 #include "ofproto-dpif-governor.h"
49 #include "ofproto-dpif-sflow.h"
50 #include "poll-loop.h"
53 #include "unaligned.h"
55 #include "vlan-bitmap.h"
58 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
60 COVERAGE_DEFINE(ofproto_dpif_expired);
61 COVERAGE_DEFINE(ofproto_dpif_xlate);
62 COVERAGE_DEFINE(facet_changed_rule);
63 COVERAGE_DEFINE(facet_revalidate);
64 COVERAGE_DEFINE(facet_unexpected);
65 COVERAGE_DEFINE(facet_suppress);
67 /* Maximum depth of flow table recursion (due to resubmit actions) in a
68 * flow translation. */
69 #define MAX_RESUBMIT_RECURSION 64
71 /* Number of implemented OpenFlow tables. */
72 enum { N_TABLES = 255 };
73 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
74 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
84 * - Do include packets and bytes from facets that have been deleted or
85 * whose own statistics have been folded into the rule.
87 * - Do include packets and bytes sent "by hand" that were accounted to
88 * the rule without any facet being involved (this is a rare corner
89 * case in rule_execute()).
91 * - Do not include packet or bytes that can be obtained from any facet's
92 * packet_count or byte_count member or that can be obtained from the
93 * datapath by, e.g., dpif_flow_get() for any subfacet.
95 uint64_t packet_count; /* Number of packets received. */
96 uint64_t byte_count; /* Number of bytes received. */
98 tag_type tag; /* Caches rule_calculate_tag() result. */
100 struct list facets; /* List of "struct facet"s. */
103 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
105 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
108 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
109 const struct flow *);
110 static struct rule_dpif *rule_dpif_lookup__(struct ofproto_dpif *,
113 static struct rule_dpif *rule_dpif_miss_rule(struct ofproto_dpif *ofproto,
114 const struct flow *flow);
116 static void rule_credit_stats(struct rule_dpif *,
117 const struct dpif_flow_stats *);
118 static void flow_push_stats(struct rule_dpif *, const struct flow *,
119 const struct dpif_flow_stats *);
120 static tag_type rule_calculate_tag(const struct flow *,
121 const struct minimask *, uint32_t basis);
122 static void rule_invalidate(const struct rule_dpif *);
124 #define MAX_MIRRORS 32
125 typedef uint32_t mirror_mask_t;
126 #define MIRROR_MASK_C(X) UINT32_C(X)
127 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
129 struct ofproto_dpif *ofproto; /* Owning ofproto. */
130 size_t idx; /* In ofproto's "mirrors" array. */
131 void *aux; /* Key supplied by ofproto's client. */
132 char *name; /* Identifier for log messages. */
134 /* Selection criteria. */
135 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
136 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
137 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
139 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
140 struct ofbundle *out; /* Output port or NULL. */
141 int out_vlan; /* Output VLAN or -1. */
142 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
145 int64_t packet_count; /* Number of packets sent. */
146 int64_t byte_count; /* Number of bytes sent. */
149 static void mirror_destroy(struct ofmirror *);
150 static void update_mirror_stats(struct ofproto_dpif *ofproto,
151 mirror_mask_t mirrors,
152 uint64_t packets, uint64_t bytes);
155 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
156 struct ofproto_dpif *ofproto; /* Owning ofproto. */
157 void *aux; /* Key supplied by ofproto's client. */
158 char *name; /* Identifier for log messages. */
161 struct list ports; /* Contains "struct ofport"s. */
162 enum port_vlan_mode vlan_mode; /* VLAN mode */
163 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
164 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
165 * NULL if all VLANs are trunked. */
166 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
167 struct bond *bond; /* Nonnull iff more than one port. */
168 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
171 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
173 /* Port mirroring info. */
174 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
175 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
176 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
179 static void bundle_remove(struct ofport *);
180 static void bundle_update(struct ofbundle *);
181 static void bundle_destroy(struct ofbundle *);
182 static void bundle_del_port(struct ofport_dpif *);
183 static void bundle_run(struct ofbundle *);
184 static void bundle_wait(struct ofbundle *);
185 static struct ofbundle *lookup_input_bundle(const struct ofproto_dpif *,
186 uint16_t in_port, bool warn,
187 struct ofport_dpif **in_ofportp);
189 /* A controller may use OFPP_NONE as the ingress port to indicate that
190 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
191 * when an input bundle is needed for validation (e.g., mirroring or
192 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
193 * any 'port' structs, so care must be taken when dealing with it. */
194 static struct ofbundle ofpp_none_bundle = {
196 .vlan_mode = PORT_VLAN_TRUNK
199 static void stp_run(struct ofproto_dpif *ofproto);
200 static void stp_wait(struct ofproto_dpif *ofproto);
201 static int set_stp_port(struct ofport *,
202 const struct ofproto_port_stp_settings *);
204 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
206 struct action_xlate_ctx {
207 /* action_xlate_ctx_init() initializes these members. */
210 struct ofproto_dpif *ofproto;
212 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
213 * this flow when actions change header fields. */
216 /* The packet corresponding to 'flow', or a null pointer if we are
217 * revalidating without a packet to refer to. */
218 const struct ofpbuf *packet;
220 /* Should OFPP_NORMAL update the MAC learning table? Should "learn"
221 * actions update the flow table?
223 * We want to update these tables if we are actually processing a packet,
224 * or if we are accounting for packets that the datapath has processed, but
225 * not if we are just revalidating. */
228 /* The rule that we are currently translating, or NULL. */
229 struct rule_dpif *rule;
231 /* Union of the set of TCP flags seen so far in this flow. (Used only by
232 * NXAST_FIN_TIMEOUT. Set to zero to avoid updating updating rules'
236 /* If nonnull, flow translation calls this function just before executing a
237 * resubmit or OFPP_TABLE action. In addition, disables logging of traces
238 * when the recursion depth is exceeded.
240 * 'rule' is the rule being submitted into. It will be null if the
241 * resubmit or OFPP_TABLE action didn't find a matching rule.
243 * This is normally null so the client has to set it manually after
244 * calling action_xlate_ctx_init(). */
245 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *rule);
247 /* If nonnull, flow translation calls this function to report some
248 * significant decision, e.g. to explain why OFPP_NORMAL translation
249 * dropped a packet. */
250 void (*report_hook)(struct action_xlate_ctx *, const char *s);
252 /* If nonnull, flow translation credits the specified statistics to each
253 * rule reached through a resubmit or OFPP_TABLE action.
255 * This is normally null so the client has to set it manually after
256 * calling action_xlate_ctx_init(). */
257 const struct dpif_flow_stats *resubmit_stats;
259 /* xlate_actions() initializes and uses these members. The client might want
260 * to look at them after it returns. */
262 struct ofpbuf *odp_actions; /* Datapath actions. */
263 tag_type tags; /* Tags associated with actions. */
264 enum slow_path_reason slow; /* 0 if fast path may be used. */
265 bool has_learn; /* Actions include NXAST_LEARN? */
266 bool has_normal; /* Actions output to OFPP_NORMAL? */
267 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
268 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
269 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
271 /* xlate_actions() initializes and uses these members, but the client has no
272 * reason to look at them. */
274 int recurse; /* Recursion level, via xlate_table_action. */
275 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
276 struct flow base_flow; /* Flow at the last commit. */
277 uint32_t orig_skb_priority; /* Priority when packet arrived. */
278 uint8_t table_id; /* OpenFlow table ID where flow was found. */
279 uint32_t sflow_n_outputs; /* Number of output ports. */
280 uint32_t sflow_odp_port; /* Output port for composing sFlow action. */
281 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
282 bool exit; /* No further actions should be processed. */
283 struct flow orig_flow; /* Copy of original flow. */
286 static void action_xlate_ctx_init(struct action_xlate_ctx *,
287 struct ofproto_dpif *, const struct flow *,
288 ovs_be16 initial_tci, struct rule_dpif *,
289 uint8_t tcp_flags, const struct ofpbuf *);
290 static void xlate_actions(struct action_xlate_ctx *,
291 const struct ofpact *ofpacts, size_t ofpacts_len,
292 struct ofpbuf *odp_actions);
293 static void xlate_actions_for_side_effects(struct action_xlate_ctx *,
294 const struct ofpact *ofpacts,
297 static size_t put_userspace_action(const struct ofproto_dpif *,
298 struct ofpbuf *odp_actions,
300 const union user_action_cookie *);
302 static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
303 enum slow_path_reason,
304 uint64_t *stub, size_t stub_size,
305 const struct nlattr **actionsp,
306 size_t *actions_lenp);
308 static void xlate_report(struct action_xlate_ctx *ctx, const char *s);
310 /* A subfacet (see "struct subfacet" below) has three possible installation
313 * - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
314 * case just after the subfacet is created, just before the subfacet is
315 * destroyed, or if the datapath returns an error when we try to install a
318 * - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
320 * - SF_SLOW_PATH: An action that sends every packet for the subfacet through
321 * ofproto_dpif is installed in the datapath.
324 SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
325 SF_FAST_PATH, /* Full actions are installed. */
326 SF_SLOW_PATH, /* Send-to-userspace action is installed. */
329 static const char *subfacet_path_to_string(enum subfacet_path);
331 /* A dpif flow and actions associated with a facet.
333 * See also the large comment on struct facet. */
336 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
337 struct list list_node; /* In struct facet's 'facets' list. */
338 struct facet *facet; /* Owning facet. */
342 * To save memory in the common case, 'key' is NULL if 'key_fitness' is
343 * ODP_FIT_PERFECT, that is, odp_flow_key_from_flow() can accurately
344 * regenerate the ODP flow key from ->facet->flow. */
345 enum odp_key_fitness key_fitness;
349 long long int used; /* Time last used; time created if not used. */
351 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
352 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
356 * These should be essentially identical for every subfacet in a facet, but
357 * may differ in trivial ways due to VLAN splinters. */
358 size_t actions_len; /* Number of bytes in actions[]. */
359 struct nlattr *actions; /* Datapath actions. */
361 enum slow_path_reason slow; /* 0 if fast path may be used. */
362 enum subfacet_path path; /* Installed in datapath? */
364 /* This value is normally the same as ->facet->flow.vlan_tci. Only VLAN
365 * splinters can cause it to differ. This value should be removed when
366 * the VLAN splinters feature is no longer needed. */
367 ovs_be16 initial_tci; /* Initial VLAN TCI value. */
370 static struct subfacet *subfacet_create(struct facet *, enum odp_key_fitness,
371 const struct nlattr *key,
372 size_t key_len, ovs_be16 initial_tci,
374 static struct subfacet *subfacet_find(struct ofproto_dpif *,
375 const struct nlattr *key, size_t key_len);
376 static void subfacet_destroy(struct subfacet *);
377 static void subfacet_destroy__(struct subfacet *);
378 static void subfacet_get_key(struct subfacet *, struct odputil_keybuf *,
380 static void subfacet_reset_dp_stats(struct subfacet *,
381 struct dpif_flow_stats *);
382 static void subfacet_update_time(struct subfacet *, long long int used);
383 static void subfacet_update_stats(struct subfacet *,
384 const struct dpif_flow_stats *);
385 static void subfacet_make_actions(struct subfacet *,
386 const struct ofpbuf *packet,
387 struct ofpbuf *odp_actions);
388 static int subfacet_install(struct subfacet *,
389 const struct nlattr *actions, size_t actions_len,
390 struct dpif_flow_stats *, enum slow_path_reason);
391 static void subfacet_uninstall(struct subfacet *);
393 static enum subfacet_path subfacet_want_path(enum slow_path_reason);
395 /* An exact-match instantiation of an OpenFlow flow.
397 * A facet associates a "struct flow", which represents the Open vSwitch
398 * userspace idea of an exact-match flow, with one or more subfacets. Each
399 * subfacet tracks the datapath's idea of the exact-match flow equivalent to
400 * the facet. When the kernel module (or other dpif implementation) and Open
401 * vSwitch userspace agree on the definition of a flow key, there is exactly
402 * one subfacet per facet. If the dpif implementation supports more-specific
403 * flow matching than userspace, however, a facet can have more than one
404 * subfacet, each of which corresponds to some distinction in flow that
405 * userspace simply doesn't understand.
407 * Flow expiration works in terms of subfacets, so a facet must have at least
408 * one subfacet or it will never expire, leaking memory. */
411 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
412 struct list list_node; /* In owning rule's 'facets' list. */
413 struct rule_dpif *rule; /* Owning rule. */
416 struct list subfacets;
417 long long int used; /* Time last used; time created if not used. */
424 * - Do include packets and bytes sent "by hand", e.g. with
427 * - Do include packets and bytes that were obtained from the datapath
428 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
429 * DPIF_FP_ZERO_STATS).
431 * - Do not include packets or bytes that can be obtained from the
432 * datapath for any existing subfacet.
434 uint64_t packet_count; /* Number of packets received. */
435 uint64_t byte_count; /* Number of bytes received. */
437 /* Resubmit statistics. */
438 uint64_t prev_packet_count; /* Number of packets from last stats push. */
439 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
440 long long int prev_used; /* Used time from last stats push. */
443 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
444 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
445 uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
447 /* Properties of datapath actions.
449 * Every subfacet has its own actions because actions can differ slightly
450 * between splintered and non-splintered subfacets due to the VLAN tag
451 * being initially different (present vs. absent). All of them have these
452 * properties in common so we just store one copy of them here. */
453 bool has_learn; /* Actions include NXAST_LEARN? */
454 bool has_normal; /* Actions output to OFPP_NORMAL? */
455 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
456 tag_type tags; /* Tags that would require revalidation. */
457 mirror_mask_t mirrors; /* Bitmap of dependent mirrors. */
459 /* Storage for a single subfacet, to reduce malloc() time and space
460 * overhead. (A facet always has at least one subfacet and in the common
461 * case has exactly one subfacet.) */
462 struct subfacet one_subfacet;
465 static struct facet *facet_create(struct rule_dpif *,
466 const struct flow *, uint32_t hash);
467 static void facet_remove(struct facet *);
468 static void facet_free(struct facet *);
470 static struct facet *facet_find(struct ofproto_dpif *,
471 const struct flow *, uint32_t hash);
472 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
473 const struct flow *, uint32_t hash);
474 static void facet_revalidate(struct facet *);
475 static bool facet_check_consistency(struct facet *);
477 static void facet_flush_stats(struct facet *);
479 static void facet_update_time(struct facet *, long long int used);
480 static void facet_reset_counters(struct facet *);
481 static void facet_push_stats(struct facet *);
482 static void facet_learn(struct facet *);
483 static void facet_account(struct facet *);
485 static bool facet_is_controller_flow(struct facet *);
488 struct hmap_node odp_port_node; /* In ofproto-dpif's "odp_to_ofport_map". */
492 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
493 struct list bundle_node; /* In struct ofbundle's "ports" list. */
494 struct cfm *cfm; /* Connectivity Fault Management, if any. */
495 tag_type tag; /* Tag associated with this port. */
496 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
497 bool may_enable; /* May be enabled in bonds. */
498 long long int carrier_seq; /* Carrier status changes. */
501 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
502 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
503 long long int stp_state_entered;
505 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
507 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
509 * This is deprecated. It is only for compatibility with broken device
510 * drivers in old versions of Linux that do not properly support VLANs when
511 * VLAN devices are not used. When broken device drivers are no longer in
512 * widespread use, we will delete these interfaces. */
513 uint16_t realdev_ofp_port;
517 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
518 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
519 * traffic egressing the 'ofport' with that priority should be marked with. */
520 struct priority_to_dscp {
521 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
522 uint32_t priority; /* Priority of this queue (see struct flow). */
524 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
527 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
529 * This is deprecated. It is only for compatibility with broken device drivers
530 * in old versions of Linux that do not properly support VLANs when VLAN
531 * devices are not used. When broken device drivers are no longer in
532 * widespread use, we will delete these interfaces. */
533 struct vlan_splinter {
534 struct hmap_node realdev_vid_node;
535 struct hmap_node vlandev_node;
536 uint16_t realdev_ofp_port;
537 uint16_t vlandev_ofp_port;
541 static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
542 uint32_t realdev, ovs_be16 vlan_tci);
543 static bool vsp_adjust_flow(const struct ofproto_dpif *, struct flow *);
544 static void vsp_remove(struct ofport_dpif *);
545 static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
547 static uint32_t ofp_port_to_odp_port(const struct ofproto_dpif *,
549 static uint16_t odp_port_to_ofp_port(const struct ofproto_dpif *,
552 static struct ofport_dpif *
553 ofport_dpif_cast(const struct ofport *ofport)
555 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
556 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
559 static void port_run(struct ofport_dpif *);
560 static void port_run_fast(struct ofport_dpif *);
561 static void port_wait(struct ofport_dpif *);
562 static int set_cfm(struct ofport *, const struct cfm_settings *);
563 static void ofport_clear_priorities(struct ofport_dpif *);
565 struct dpif_completion {
566 struct list list_node;
567 struct ofoperation *op;
570 /* Extra information about a classifier table.
571 * Currently used just for optimized flow revalidation. */
573 /* If either of these is nonnull, then this table has a form that allows
574 * flows to be tagged to avoid revalidating most flows for the most common
575 * kinds of flow table changes. */
576 struct cls_table *catchall_table; /* Table that wildcards all fields. */
577 struct cls_table *other_table; /* Table with any other wildcard set. */
578 uint32_t basis; /* Keeps each table's tags separate. */
581 /* Reasons that we might need to revalidate every facet, and corresponding
584 * A value of 0 means that there is no need to revalidate.
586 * It would be nice to have some cleaner way to integrate with coverage
587 * counters, but with only a few reasons I guess this is good enough for
589 enum revalidate_reason {
590 REV_RECONFIGURE = 1, /* Switch configuration changed. */
591 REV_STP, /* Spanning tree protocol port status change. */
592 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
593 REV_FLOW_TABLE, /* Flow table changed. */
594 REV_INCONSISTENCY /* Facet self-check failed. */
596 COVERAGE_DEFINE(rev_reconfigure);
597 COVERAGE_DEFINE(rev_stp);
598 COVERAGE_DEFINE(rev_port_toggled);
599 COVERAGE_DEFINE(rev_flow_table);
600 COVERAGE_DEFINE(rev_inconsistency);
602 struct ofproto_dpif {
603 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
607 /* Special OpenFlow rules. */
608 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
609 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
615 struct netflow *netflow;
616 struct dpif_sflow *sflow;
617 struct hmap bundles; /* Contains "struct ofbundle"s. */
618 struct mac_learning *ml;
619 struct ofmirror *mirrors[MAX_MIRRORS];
621 bool has_bonded_bundles;
624 struct timer next_expiration;
628 struct hmap subfacets;
629 struct governor *governor;
632 struct table_dpif tables[N_TABLES];
633 enum revalidate_reason need_revalidate;
634 struct tag_set revalidate_set;
636 /* Support for debugging async flow mods. */
637 struct list completions;
639 bool has_bundle_action; /* True when the first bundle action appears. */
640 struct netdev_stats stats; /* To account packets generated and consumed in
645 long long int stp_last_tick;
647 /* VLAN splinters. */
648 struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
649 struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
651 /* ODP port to ofp_port mapping. */
652 struct hmap odp_to_ofport_map;
655 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
656 * for debugging the asynchronous flow_mod implementation.) */
659 /* All existing ofproto_dpif instances, indexed by ->up.name. */
660 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
662 static void ofproto_dpif_unixctl_init(void);
664 static struct ofproto_dpif *
665 ofproto_dpif_cast(const struct ofproto *ofproto)
667 assert(ofproto->ofproto_class == &ofproto_dpif_class);
668 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
671 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *,
673 static struct ofport_dpif *get_odp_port(const struct ofproto_dpif *,
675 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
676 const struct ofpbuf *, ovs_be16 initial_tci,
679 /* Packet processing. */
680 static void update_learning_table(struct ofproto_dpif *,
681 const struct flow *, int vlan,
684 #define FLOW_MISS_MAX_BATCH 50
685 static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
687 /* Flow expiration. */
688 static int expire(struct ofproto_dpif *);
691 static void send_netflow_active_timeouts(struct ofproto_dpif *);
694 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
695 static size_t compose_sflow_action(const struct ofproto_dpif *,
696 struct ofpbuf *odp_actions,
697 const struct flow *, uint32_t odp_port);
698 static void add_mirror_actions(struct action_xlate_ctx *ctx,
699 const struct flow *flow);
700 /* Global variables. */
701 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
703 /* Factory functions. */
706 init(const struct shash *iface_hints OVS_UNUSED)
711 enumerate_types(struct sset *types)
713 dp_enumerate_types(types);
717 enumerate_names(const char *type, struct sset *names)
719 return dp_enumerate_names(type, names);
723 del(const char *type, const char *name)
728 error = dpif_open(name, type, &dpif);
730 error = dpif_delete(dpif);
736 /* Basic life-cycle. */
738 static int add_internal_flows(struct ofproto_dpif *);
740 static struct ofproto *
743 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
748 dealloc(struct ofproto *ofproto_)
750 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
755 construct(struct ofproto *ofproto_)
757 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
758 const char *name = ofproto->up.name;
763 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
765 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
769 max_ports = dpif_get_max_ports(ofproto->dpif);
770 ofproto_init_max_ports(ofproto_, MIN(max_ports, OFPP_MAX));
772 ofproto->n_matches = 0;
774 dpif_flow_flush(ofproto->dpif);
775 dpif_recv_purge(ofproto->dpif);
777 error = dpif_recv_set(ofproto->dpif, true);
779 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
780 dpif_close(ofproto->dpif);
784 ofproto->netflow = NULL;
785 ofproto->sflow = NULL;
787 hmap_init(&ofproto->bundles);
788 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
789 for (i = 0; i < MAX_MIRRORS; i++) {
790 ofproto->mirrors[i] = NULL;
792 ofproto->has_bonded_bundles = false;
794 timer_set_duration(&ofproto->next_expiration, 1000);
796 hmap_init(&ofproto->facets);
797 hmap_init(&ofproto->subfacets);
798 ofproto->governor = NULL;
800 for (i = 0; i < N_TABLES; i++) {
801 struct table_dpif *table = &ofproto->tables[i];
803 table->catchall_table = NULL;
804 table->other_table = NULL;
805 table->basis = random_uint32();
807 ofproto->need_revalidate = 0;
808 tag_set_init(&ofproto->revalidate_set);
810 list_init(&ofproto->completions);
812 ofproto_dpif_unixctl_init();
814 ofproto->has_mirrors = false;
815 ofproto->has_bundle_action = false;
817 hmap_init(&ofproto->vlandev_map);
818 hmap_init(&ofproto->realdev_vid_map);
820 hmap_init(&ofproto->odp_to_ofport_map);
822 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
823 hash_string(ofproto->up.name, 0));
824 memset(&ofproto->stats, 0, sizeof ofproto->stats);
826 ofproto_init_tables(ofproto_, N_TABLES);
827 error = add_internal_flows(ofproto);
828 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
834 add_internal_flow(struct ofproto_dpif *ofproto, int id,
835 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
837 struct ofputil_flow_mod fm;
840 match_init_catchall(&fm.match);
842 match_set_reg(&fm.match, 0, id);
843 fm.new_cookie = htonll(0);
844 fm.cookie = htonll(0);
845 fm.cookie_mask = htonll(0);
846 fm.table_id = TBL_INTERNAL;
847 fm.command = OFPFC_ADD;
853 fm.ofpacts = ofpacts->data;
854 fm.ofpacts_len = ofpacts->size;
856 error = ofproto_flow_mod(&ofproto->up, &fm);
858 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
859 id, ofperr_to_string(error));
863 *rulep = rule_dpif_lookup__(ofproto, &fm.match.flow, TBL_INTERNAL);
864 assert(*rulep != NULL);
870 add_internal_flows(struct ofproto_dpif *ofproto)
872 struct ofpact_controller *controller;
873 uint64_t ofpacts_stub[128 / 8];
874 struct ofpbuf ofpacts;
878 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
881 controller = ofpact_put_CONTROLLER(&ofpacts);
882 controller->max_len = UINT16_MAX;
883 controller->controller_id = 0;
884 controller->reason = OFPR_NO_MATCH;
885 ofpact_pad(&ofpacts);
887 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
892 ofpbuf_clear(&ofpacts);
893 error = add_internal_flow(ofproto, id++, &ofpacts,
894 &ofproto->no_packet_in_rule);
899 complete_operations(struct ofproto_dpif *ofproto)
901 struct dpif_completion *c, *next;
903 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
904 ofoperation_complete(c->op, 0);
905 list_remove(&c->list_node);
911 destruct(struct ofproto *ofproto_)
913 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
914 struct rule_dpif *rule, *next_rule;
915 struct oftable *table;
918 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
919 complete_operations(ofproto);
921 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
922 struct cls_cursor cursor;
924 cls_cursor_init(&cursor, &table->cls, NULL);
925 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
926 ofproto_rule_destroy(&rule->up);
930 for (i = 0; i < MAX_MIRRORS; i++) {
931 mirror_destroy(ofproto->mirrors[i]);
934 netflow_destroy(ofproto->netflow);
935 dpif_sflow_destroy(ofproto->sflow);
936 hmap_destroy(&ofproto->bundles);
937 mac_learning_destroy(ofproto->ml);
939 hmap_destroy(&ofproto->facets);
940 hmap_destroy(&ofproto->subfacets);
941 governor_destroy(ofproto->governor);
943 hmap_destroy(&ofproto->vlandev_map);
944 hmap_destroy(&ofproto->realdev_vid_map);
946 hmap_destroy(&ofproto->odp_to_ofport_map);
948 dpif_close(ofproto->dpif);
952 run_fast(struct ofproto *ofproto_)
954 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
955 struct ofport_dpif *ofport;
958 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
959 port_run_fast(ofport);
962 /* Handle one or more batches of upcalls, until there's nothing left to do
963 * or until we do a fixed total amount of work.
965 * We do work in batches because it can be much cheaper to set up a number
966 * of flows and fire off their patches all at once. We do multiple batches
967 * because in some cases handling a packet can cause another packet to be
968 * queued almost immediately as part of the return flow. Both
969 * optimizations can make major improvements on some benchmarks and
970 * presumably for real traffic as well. */
972 while (work < FLOW_MISS_MAX_BATCH) {
973 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
983 run(struct ofproto *ofproto_)
985 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
986 struct ofport_dpif *ofport;
987 struct ofbundle *bundle;
991 complete_operations(ofproto);
993 dpif_run(ofproto->dpif);
995 error = run_fast(ofproto_);
1000 if (timer_expired(&ofproto->next_expiration)) {
1001 int delay = expire(ofproto);
1002 timer_set_duration(&ofproto->next_expiration, delay);
1005 if (ofproto->netflow) {
1006 if (netflow_run(ofproto->netflow)) {
1007 send_netflow_active_timeouts(ofproto);
1010 if (ofproto->sflow) {
1011 dpif_sflow_run(ofproto->sflow);
1014 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1017 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1022 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
1024 /* Now revalidate if there's anything to do. */
1025 if (ofproto->need_revalidate
1026 || !tag_set_is_empty(&ofproto->revalidate_set)) {
1027 struct tag_set revalidate_set = ofproto->revalidate_set;
1028 bool revalidate_all = ofproto->need_revalidate;
1029 struct facet *facet;
1031 switch (ofproto->need_revalidate) {
1032 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
1033 case REV_STP: COVERAGE_INC(rev_stp); break;
1034 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
1035 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
1036 case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
1039 /* Clear the revalidation flags. */
1040 tag_set_init(&ofproto->revalidate_set);
1041 ofproto->need_revalidate = 0;
1043 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1045 || tag_set_intersects(&revalidate_set, facet->tags)) {
1046 facet_revalidate(facet);
1051 /* Check the consistency of a random facet, to aid debugging. */
1052 if (!hmap_is_empty(&ofproto->facets) && !ofproto->need_revalidate) {
1053 struct facet *facet;
1055 facet = CONTAINER_OF(hmap_random_node(&ofproto->facets),
1056 struct facet, hmap_node);
1057 if (!tag_set_intersects(&ofproto->revalidate_set, facet->tags)) {
1058 if (!facet_check_consistency(facet)) {
1059 ofproto->need_revalidate = REV_INCONSISTENCY;
1064 if (ofproto->governor) {
1067 governor_run(ofproto->governor);
1069 /* If the governor has shrunk to its minimum size and the number of
1070 * subfacets has dwindled, then drop the governor entirely.
1072 * For hysteresis, the number of subfacets to drop the governor is
1073 * smaller than the number needed to trigger its creation. */
1074 n_subfacets = hmap_count(&ofproto->subfacets);
1075 if (n_subfacets * 4 < ofproto->up.flow_eviction_threshold
1076 && governor_is_idle(ofproto->governor)) {
1077 governor_destroy(ofproto->governor);
1078 ofproto->governor = NULL;
1086 wait(struct ofproto *ofproto_)
1088 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1089 struct ofport_dpif *ofport;
1090 struct ofbundle *bundle;
1092 if (!clogged && !list_is_empty(&ofproto->completions)) {
1093 poll_immediate_wake();
1096 dpif_wait(ofproto->dpif);
1097 dpif_recv_wait(ofproto->dpif);
1098 if (ofproto->sflow) {
1099 dpif_sflow_wait(ofproto->sflow);
1101 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
1102 poll_immediate_wake();
1104 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1107 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1108 bundle_wait(bundle);
1110 if (ofproto->netflow) {
1111 netflow_wait(ofproto->netflow);
1113 mac_learning_wait(ofproto->ml);
1115 if (ofproto->need_revalidate) {
1116 /* Shouldn't happen, but if it does just go around again. */
1117 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1118 poll_immediate_wake();
1120 timer_wait(&ofproto->next_expiration);
1122 if (ofproto->governor) {
1123 governor_wait(ofproto->governor);
1128 get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1130 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1132 simap_increase(usage, "facets", hmap_count(&ofproto->facets));
1133 simap_increase(usage, "subfacets", hmap_count(&ofproto->subfacets));
1137 flush(struct ofproto *ofproto_)
1139 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1140 struct facet *facet, *next_facet;
1142 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1143 /* Mark the facet as not installed so that facet_remove() doesn't
1144 * bother trying to uninstall it. There is no point in uninstalling it
1145 * individually since we are about to blow away all the facets with
1146 * dpif_flow_flush(). */
1147 struct subfacet *subfacet;
1149 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
1150 subfacet->path = SF_NOT_INSTALLED;
1151 subfacet->dp_packet_count = 0;
1152 subfacet->dp_byte_count = 0;
1154 facet_remove(facet);
1156 dpif_flow_flush(ofproto->dpif);
1160 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1161 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1163 *arp_match_ip = true;
1164 *actions = (OFPUTIL_A_OUTPUT |
1165 OFPUTIL_A_SET_VLAN_VID |
1166 OFPUTIL_A_SET_VLAN_PCP |
1167 OFPUTIL_A_STRIP_VLAN |
1168 OFPUTIL_A_SET_DL_SRC |
1169 OFPUTIL_A_SET_DL_DST |
1170 OFPUTIL_A_SET_NW_SRC |
1171 OFPUTIL_A_SET_NW_DST |
1172 OFPUTIL_A_SET_NW_TOS |
1173 OFPUTIL_A_SET_TP_SRC |
1174 OFPUTIL_A_SET_TP_DST |
1179 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1181 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1182 struct dpif_dp_stats s;
1184 strcpy(ots->name, "classifier");
1186 dpif_get_dp_stats(ofproto->dpif, &s);
1187 ots->lookup_count = htonll(s.n_hit + s.n_missed);
1188 ots->matched_count = htonll(s.n_hit + ofproto->n_matches);
1191 static struct ofport *
1194 struct ofport_dpif *port = xmalloc(sizeof *port);
1199 port_dealloc(struct ofport *port_)
1201 struct ofport_dpif *port = ofport_dpif_cast(port_);
1206 port_construct(struct ofport *port_)
1208 struct ofport_dpif *port = ofport_dpif_cast(port_);
1209 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1210 struct dpif_port dpif_port;
1213 ofproto->need_revalidate = REV_RECONFIGURE;
1214 port->bundle = NULL;
1216 port->tag = tag_create_random();
1217 port->may_enable = true;
1218 port->stp_port = NULL;
1219 port->stp_state = STP_DISABLED;
1220 hmap_init(&port->priorities);
1221 port->realdev_ofp_port = 0;
1222 port->vlandev_vid = 0;
1223 port->carrier_seq = netdev_get_carrier_resets(port->up.netdev);
1225 error = dpif_port_query_by_name(ofproto->dpif,
1226 netdev_get_name(port->up.netdev),
1232 port->odp_port = dpif_port.port_no;
1234 /* Sanity-check that a mapping doesn't already exist. This
1235 * shouldn't happen. */
1236 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1237 VLOG_ERR("port %s already has an OpenFlow port number\n",
1242 hmap_insert(&ofproto->odp_to_ofport_map, &port->odp_port_node,
1243 hash_int(port->odp_port, 0));
1245 if (ofproto->sflow) {
1246 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1253 port_destruct(struct ofport *port_)
1255 struct ofport_dpif *port = ofport_dpif_cast(port_);
1256 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1258 hmap_remove(&ofproto->odp_to_ofport_map, &port->odp_port_node);
1259 ofproto->need_revalidate = REV_RECONFIGURE;
1260 bundle_remove(port_);
1261 set_cfm(port_, NULL);
1262 if (ofproto->sflow) {
1263 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1266 ofport_clear_priorities(port);
1267 hmap_destroy(&port->priorities);
1271 port_modified(struct ofport *port_)
1273 struct ofport_dpif *port = ofport_dpif_cast(port_);
1275 if (port->bundle && port->bundle->bond) {
1276 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1281 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1283 struct ofport_dpif *port = ofport_dpif_cast(port_);
1284 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1285 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1287 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1288 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1289 OFPUTIL_PC_NO_PACKET_IN)) {
1290 ofproto->need_revalidate = REV_RECONFIGURE;
1292 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1293 bundle_update(port->bundle);
1299 set_sflow(struct ofproto *ofproto_,
1300 const struct ofproto_sflow_options *sflow_options)
1302 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1303 struct dpif_sflow *ds = ofproto->sflow;
1305 if (sflow_options) {
1307 struct ofport_dpif *ofport;
1309 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
1310 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1311 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1313 ofproto->need_revalidate = REV_RECONFIGURE;
1315 dpif_sflow_set_options(ds, sflow_options);
1318 dpif_sflow_destroy(ds);
1319 ofproto->need_revalidate = REV_RECONFIGURE;
1320 ofproto->sflow = NULL;
1327 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1329 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1336 struct ofproto_dpif *ofproto;
1338 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1339 ofproto->need_revalidate = REV_RECONFIGURE;
1340 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
1343 if (cfm_configure(ofport->cfm, s)) {
1349 cfm_destroy(ofport->cfm);
1355 get_cfm_fault(const struct ofport *ofport_)
1357 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1359 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
1363 get_cfm_opup(const struct ofport *ofport_)
1365 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1367 return ofport->cfm ? cfm_get_opup(ofport->cfm) : -1;
1371 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1374 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1377 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1385 get_cfm_health(const struct ofport *ofport_)
1387 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1389 return ofport->cfm ? cfm_get_health(ofport->cfm) : -1;
1392 /* Spanning Tree. */
1395 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1397 struct ofproto_dpif *ofproto = ofproto_;
1398 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1399 struct ofport_dpif *ofport;
1401 ofport = stp_port_get_aux(sp);
1403 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1404 ofproto->up.name, port_num);
1406 struct eth_header *eth = pkt->l2;
1408 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1409 if (eth_addr_is_zero(eth->eth_src)) {
1410 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1411 "with unknown MAC", ofproto->up.name, port_num);
1413 send_packet(ofport, pkt);
1419 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1421 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1423 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1425 /* Only revalidate flows if the configuration changed. */
1426 if (!s != !ofproto->stp) {
1427 ofproto->need_revalidate = REV_RECONFIGURE;
1431 if (!ofproto->stp) {
1432 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1433 send_bpdu_cb, ofproto);
1434 ofproto->stp_last_tick = time_msec();
1437 stp_set_bridge_id(ofproto->stp, s->system_id);
1438 stp_set_bridge_priority(ofproto->stp, s->priority);
1439 stp_set_hello_time(ofproto->stp, s->hello_time);
1440 stp_set_max_age(ofproto->stp, s->max_age);
1441 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1443 struct ofport *ofport;
1445 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1446 set_stp_port(ofport, NULL);
1449 stp_destroy(ofproto->stp);
1450 ofproto->stp = NULL;
1457 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1459 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1463 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1464 s->designated_root = stp_get_designated_root(ofproto->stp);
1465 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1474 update_stp_port_state(struct ofport_dpif *ofport)
1476 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1477 enum stp_state state;
1479 /* Figure out new state. */
1480 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1484 if (ofport->stp_state != state) {
1485 enum ofputil_port_state of_state;
1488 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1489 netdev_get_name(ofport->up.netdev),
1490 stp_state_name(ofport->stp_state),
1491 stp_state_name(state));
1492 if (stp_learn_in_state(ofport->stp_state)
1493 != stp_learn_in_state(state)) {
1494 /* xxx Learning action flows should also be flushed. */
1495 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
1497 fwd_change = stp_forward_in_state(ofport->stp_state)
1498 != stp_forward_in_state(state);
1500 ofproto->need_revalidate = REV_STP;
1501 ofport->stp_state = state;
1502 ofport->stp_state_entered = time_msec();
1504 if (fwd_change && ofport->bundle) {
1505 bundle_update(ofport->bundle);
1508 /* Update the STP state bits in the OpenFlow port description. */
1509 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1510 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1511 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1512 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1513 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1515 ofproto_port_set_state(&ofport->up, of_state);
1519 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1520 * caller is responsible for assigning STP port numbers and ensuring
1521 * there are no duplicates. */
1523 set_stp_port(struct ofport *ofport_,
1524 const struct ofproto_port_stp_settings *s)
1526 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1527 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1528 struct stp_port *sp = ofport->stp_port;
1530 if (!s || !s->enable) {
1532 ofport->stp_port = NULL;
1533 stp_port_disable(sp);
1534 update_stp_port_state(ofport);
1537 } else if (sp && stp_port_no(sp) != s->port_num
1538 && ofport == stp_port_get_aux(sp)) {
1539 /* The port-id changed, so disable the old one if it's not
1540 * already in use by another port. */
1541 stp_port_disable(sp);
1544 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1545 stp_port_enable(sp);
1547 stp_port_set_aux(sp, ofport);
1548 stp_port_set_priority(sp, s->priority);
1549 stp_port_set_path_cost(sp, s->path_cost);
1551 update_stp_port_state(ofport);
1557 get_stp_port_status(struct ofport *ofport_,
1558 struct ofproto_port_stp_status *s)
1560 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1561 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1562 struct stp_port *sp = ofport->stp_port;
1564 if (!ofproto->stp || !sp) {
1570 s->port_id = stp_port_get_id(sp);
1571 s->state = stp_port_get_state(sp);
1572 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1573 s->role = stp_port_get_role(sp);
1574 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1580 stp_run(struct ofproto_dpif *ofproto)
1583 long long int now = time_msec();
1584 long long int elapsed = now - ofproto->stp_last_tick;
1585 struct stp_port *sp;
1588 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1589 ofproto->stp_last_tick = now;
1591 while (stp_get_changed_port(ofproto->stp, &sp)) {
1592 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1595 update_stp_port_state(ofport);
1599 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1600 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
1606 stp_wait(struct ofproto_dpif *ofproto)
1609 poll_timer_wait(1000);
1613 /* Returns true if STP should process 'flow'. */
1615 stp_should_process_flow(const struct flow *flow)
1617 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1621 stp_process_packet(const struct ofport_dpif *ofport,
1622 const struct ofpbuf *packet)
1624 struct ofpbuf payload = *packet;
1625 struct eth_header *eth = payload.data;
1626 struct stp_port *sp = ofport->stp_port;
1628 /* Sink packets on ports that have STP disabled when the bridge has
1630 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1634 /* Trim off padding on payload. */
1635 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1636 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1639 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1640 stp_received_bpdu(sp, payload.data, payload.size);
1644 static struct priority_to_dscp *
1645 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1647 struct priority_to_dscp *pdscp;
1650 hash = hash_int(priority, 0);
1651 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1652 if (pdscp->priority == priority) {
1660 ofport_clear_priorities(struct ofport_dpif *ofport)
1662 struct priority_to_dscp *pdscp, *next;
1664 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1665 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1671 set_queues(struct ofport *ofport_,
1672 const struct ofproto_port_queue *qdscp_list,
1675 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1676 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1677 struct hmap new = HMAP_INITIALIZER(&new);
1680 for (i = 0; i < n_qdscp; i++) {
1681 struct priority_to_dscp *pdscp;
1685 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1686 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1691 pdscp = get_priority(ofport, priority);
1693 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1695 pdscp = xmalloc(sizeof *pdscp);
1696 pdscp->priority = priority;
1698 ofproto->need_revalidate = REV_RECONFIGURE;
1701 if (pdscp->dscp != dscp) {
1703 ofproto->need_revalidate = REV_RECONFIGURE;
1706 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1709 if (!hmap_is_empty(&ofport->priorities)) {
1710 ofport_clear_priorities(ofport);
1711 ofproto->need_revalidate = REV_RECONFIGURE;
1714 hmap_swap(&new, &ofport->priorities);
1722 /* Expires all MAC learning entries associated with 'bundle' and forces its
1723 * ofproto to revalidate every flow.
1725 * Normally MAC learning entries are removed only from the ofproto associated
1726 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
1727 * are removed from every ofproto. When patch ports and SLB bonds are in use
1728 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
1729 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
1730 * with the host from which it migrated. */
1732 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
1734 struct ofproto_dpif *ofproto = bundle->ofproto;
1735 struct mac_learning *ml = ofproto->ml;
1736 struct mac_entry *mac, *next_mac;
1738 ofproto->need_revalidate = REV_RECONFIGURE;
1739 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1740 if (mac->port.p == bundle) {
1742 struct ofproto_dpif *o;
1744 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1746 struct mac_entry *e;
1748 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan,
1751 tag_set_add(&o->revalidate_set, e->tag);
1752 mac_learning_expire(o->ml, e);
1758 mac_learning_expire(ml, mac);
1763 static struct ofbundle *
1764 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1766 struct ofbundle *bundle;
1768 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1769 &ofproto->bundles) {
1770 if (bundle->aux == aux) {
1777 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1778 * ones that are found to 'bundles'. */
1780 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1781 void **auxes, size_t n_auxes,
1782 struct hmapx *bundles)
1786 hmapx_init(bundles);
1787 for (i = 0; i < n_auxes; i++) {
1788 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1790 hmapx_add(bundles, bundle);
1796 bundle_update(struct ofbundle *bundle)
1798 struct ofport_dpif *port;
1800 bundle->floodable = true;
1801 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1802 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1803 || !stp_forward_in_state(port->stp_state)) {
1804 bundle->floodable = false;
1811 bundle_del_port(struct ofport_dpif *port)
1813 struct ofbundle *bundle = port->bundle;
1815 bundle->ofproto->need_revalidate = REV_RECONFIGURE;
1817 list_remove(&port->bundle_node);
1818 port->bundle = NULL;
1821 lacp_slave_unregister(bundle->lacp, port);
1824 bond_slave_unregister(bundle->bond, port);
1827 bundle_update(bundle);
1831 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1832 struct lacp_slave_settings *lacp,
1833 uint32_t bond_stable_id)
1835 struct ofport_dpif *port;
1837 port = get_ofp_port(bundle->ofproto, ofp_port);
1842 if (port->bundle != bundle) {
1843 bundle->ofproto->need_revalidate = REV_RECONFIGURE;
1845 bundle_del_port(port);
1848 port->bundle = bundle;
1849 list_push_back(&bundle->ports, &port->bundle_node);
1850 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1851 || !stp_forward_in_state(port->stp_state)) {
1852 bundle->floodable = false;
1856 port->bundle->ofproto->need_revalidate = REV_RECONFIGURE;
1857 lacp_slave_register(bundle->lacp, port, lacp);
1860 port->bond_stable_id = bond_stable_id;
1866 bundle_destroy(struct ofbundle *bundle)
1868 struct ofproto_dpif *ofproto;
1869 struct ofport_dpif *port, *next_port;
1876 ofproto = bundle->ofproto;
1877 for (i = 0; i < MAX_MIRRORS; i++) {
1878 struct ofmirror *m = ofproto->mirrors[i];
1880 if (m->out == bundle) {
1882 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1883 || hmapx_find_and_delete(&m->dsts, bundle)) {
1884 ofproto->need_revalidate = REV_RECONFIGURE;
1889 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1890 bundle_del_port(port);
1893 bundle_flush_macs(bundle, true);
1894 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1896 free(bundle->trunks);
1897 lacp_destroy(bundle->lacp);
1898 bond_destroy(bundle->bond);
1903 bundle_set(struct ofproto *ofproto_, void *aux,
1904 const struct ofproto_bundle_settings *s)
1906 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1907 bool need_flush = false;
1908 struct ofport_dpif *port;
1909 struct ofbundle *bundle;
1910 unsigned long *trunks;
1916 bundle_destroy(bundle_lookup(ofproto, aux));
1920 assert(s->n_slaves == 1 || s->bond != NULL);
1921 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1923 bundle = bundle_lookup(ofproto, aux);
1925 bundle = xmalloc(sizeof *bundle);
1927 bundle->ofproto = ofproto;
1928 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1929 hash_pointer(aux, 0));
1931 bundle->name = NULL;
1933 list_init(&bundle->ports);
1934 bundle->vlan_mode = PORT_VLAN_TRUNK;
1936 bundle->trunks = NULL;
1937 bundle->use_priority_tags = s->use_priority_tags;
1938 bundle->lacp = NULL;
1939 bundle->bond = NULL;
1941 bundle->floodable = true;
1943 bundle->src_mirrors = 0;
1944 bundle->dst_mirrors = 0;
1945 bundle->mirror_out = 0;
1948 if (!bundle->name || strcmp(s->name, bundle->name)) {
1950 bundle->name = xstrdup(s->name);
1955 if (!bundle->lacp) {
1956 ofproto->need_revalidate = REV_RECONFIGURE;
1957 bundle->lacp = lacp_create();
1959 lacp_configure(bundle->lacp, s->lacp);
1961 lacp_destroy(bundle->lacp);
1962 bundle->lacp = NULL;
1965 /* Update set of ports. */
1967 for (i = 0; i < s->n_slaves; i++) {
1968 if (!bundle_add_port(bundle, s->slaves[i],
1969 s->lacp ? &s->lacp_slaves[i] : NULL,
1970 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1974 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1975 struct ofport_dpif *next_port;
1977 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1978 for (i = 0; i < s->n_slaves; i++) {
1979 if (s->slaves[i] == port->up.ofp_port) {
1984 bundle_del_port(port);
1988 assert(list_size(&bundle->ports) <= s->n_slaves);
1990 if (list_is_empty(&bundle->ports)) {
1991 bundle_destroy(bundle);
1995 /* Set VLAN tagging mode */
1996 if (s->vlan_mode != bundle->vlan_mode
1997 || s->use_priority_tags != bundle->use_priority_tags) {
1998 bundle->vlan_mode = s->vlan_mode;
1999 bundle->use_priority_tags = s->use_priority_tags;
2004 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2005 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2007 if (vlan != bundle->vlan) {
2008 bundle->vlan = vlan;
2012 /* Get trunked VLANs. */
2013 switch (s->vlan_mode) {
2014 case PORT_VLAN_ACCESS:
2018 case PORT_VLAN_TRUNK:
2019 trunks = CONST_CAST(unsigned long *, s->trunks);
2022 case PORT_VLAN_NATIVE_UNTAGGED:
2023 case PORT_VLAN_NATIVE_TAGGED:
2024 if (vlan != 0 && (!s->trunks
2025 || !bitmap_is_set(s->trunks, vlan)
2026 || bitmap_is_set(s->trunks, 0))) {
2027 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2029 trunks = bitmap_clone(s->trunks, 4096);
2031 trunks = bitmap_allocate1(4096);
2033 bitmap_set1(trunks, vlan);
2034 bitmap_set0(trunks, 0);
2036 trunks = CONST_CAST(unsigned long *, s->trunks);
2043 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2044 free(bundle->trunks);
2045 if (trunks == s->trunks) {
2046 bundle->trunks = vlan_bitmap_clone(trunks);
2048 bundle->trunks = trunks;
2053 if (trunks != s->trunks) {
2058 if (!list_is_short(&bundle->ports)) {
2059 bundle->ofproto->has_bonded_bundles = true;
2061 if (bond_reconfigure(bundle->bond, s->bond)) {
2062 ofproto->need_revalidate = REV_RECONFIGURE;
2065 bundle->bond = bond_create(s->bond);
2066 ofproto->need_revalidate = REV_RECONFIGURE;
2069 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2070 bond_slave_register(bundle->bond, port, port->bond_stable_id,
2074 bond_destroy(bundle->bond);
2075 bundle->bond = NULL;
2078 /* If we changed something that would affect MAC learning, un-learn
2079 * everything on this port and force flow revalidation. */
2081 bundle_flush_macs(bundle, false);
2088 bundle_remove(struct ofport *port_)
2090 struct ofport_dpif *port = ofport_dpif_cast(port_);
2091 struct ofbundle *bundle = port->bundle;
2094 bundle_del_port(port);
2095 if (list_is_empty(&bundle->ports)) {
2096 bundle_destroy(bundle);
2097 } else if (list_is_short(&bundle->ports)) {
2098 bond_destroy(bundle->bond);
2099 bundle->bond = NULL;
2105 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2107 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2108 struct ofport_dpif *port = port_;
2109 uint8_t ea[ETH_ADDR_LEN];
2112 error = netdev_get_etheraddr(port->up.netdev, ea);
2114 struct ofpbuf packet;
2117 ofpbuf_init(&packet, 0);
2118 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2120 memcpy(packet_pdu, pdu, pdu_size);
2122 send_packet(port, &packet);
2123 ofpbuf_uninit(&packet);
2125 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2126 "%s (%s)", port->bundle->name,
2127 netdev_get_name(port->up.netdev), strerror(error));
2132 bundle_send_learning_packets(struct ofbundle *bundle)
2134 struct ofproto_dpif *ofproto = bundle->ofproto;
2135 int error, n_packets, n_errors;
2136 struct mac_entry *e;
2138 error = n_packets = n_errors = 0;
2139 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2140 if (e->port.p != bundle) {
2141 struct ofpbuf *learning_packet;
2142 struct ofport_dpif *port;
2146 /* The assignment to "port" is unnecessary but makes "grep"ing for
2147 * struct ofport_dpif more effective. */
2148 learning_packet = bond_compose_learning_packet(bundle->bond,
2152 ret = send_packet(port, learning_packet);
2153 ofpbuf_delete(learning_packet);
2163 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2164 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2165 "packets, last error was: %s",
2166 bundle->name, n_errors, n_packets, strerror(error));
2168 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2169 bundle->name, n_packets);
2174 bundle_run(struct ofbundle *bundle)
2177 lacp_run(bundle->lacp, send_pdu_cb);
2180 struct ofport_dpif *port;
2182 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2183 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2186 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
2187 lacp_status(bundle->lacp));
2188 if (bond_should_send_learning_packets(bundle->bond)) {
2189 bundle_send_learning_packets(bundle);
2195 bundle_wait(struct ofbundle *bundle)
2198 lacp_wait(bundle->lacp);
2201 bond_wait(bundle->bond);
2208 mirror_scan(struct ofproto_dpif *ofproto)
2212 for (idx = 0; idx < MAX_MIRRORS; idx++) {
2213 if (!ofproto->mirrors[idx]) {
2220 static struct ofmirror *
2221 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
2225 for (i = 0; i < MAX_MIRRORS; i++) {
2226 struct ofmirror *mirror = ofproto->mirrors[i];
2227 if (mirror && mirror->aux == aux) {
2235 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
2237 mirror_update_dups(struct ofproto_dpif *ofproto)
2241 for (i = 0; i < MAX_MIRRORS; i++) {
2242 struct ofmirror *m = ofproto->mirrors[i];
2245 m->dup_mirrors = MIRROR_MASK_C(1) << i;
2249 for (i = 0; i < MAX_MIRRORS; i++) {
2250 struct ofmirror *m1 = ofproto->mirrors[i];
2257 for (j = i + 1; j < MAX_MIRRORS; j++) {
2258 struct ofmirror *m2 = ofproto->mirrors[j];
2260 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
2261 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
2262 m2->dup_mirrors |= m1->dup_mirrors;
2269 mirror_set(struct ofproto *ofproto_, void *aux,
2270 const struct ofproto_mirror_settings *s)
2272 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2273 mirror_mask_t mirror_bit;
2274 struct ofbundle *bundle;
2275 struct ofmirror *mirror;
2276 struct ofbundle *out;
2277 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
2278 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
2281 mirror = mirror_lookup(ofproto, aux);
2283 mirror_destroy(mirror);
2289 idx = mirror_scan(ofproto);
2291 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
2293 ofproto->up.name, MAX_MIRRORS, s->name);
2297 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
2298 mirror->ofproto = ofproto;
2301 mirror->out_vlan = -1;
2302 mirror->name = NULL;
2305 if (!mirror->name || strcmp(s->name, mirror->name)) {
2307 mirror->name = xstrdup(s->name);
2310 /* Get the new configuration. */
2311 if (s->out_bundle) {
2312 out = bundle_lookup(ofproto, s->out_bundle);
2314 mirror_destroy(mirror);
2320 out_vlan = s->out_vlan;
2322 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
2323 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
2325 /* If the configuration has not changed, do nothing. */
2326 if (hmapx_equals(&srcs, &mirror->srcs)
2327 && hmapx_equals(&dsts, &mirror->dsts)
2328 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
2329 && mirror->out == out
2330 && mirror->out_vlan == out_vlan)
2332 hmapx_destroy(&srcs);
2333 hmapx_destroy(&dsts);
2337 hmapx_swap(&srcs, &mirror->srcs);
2338 hmapx_destroy(&srcs);
2340 hmapx_swap(&dsts, &mirror->dsts);
2341 hmapx_destroy(&dsts);
2343 free(mirror->vlans);
2344 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
2347 mirror->out_vlan = out_vlan;
2349 /* Update bundles. */
2350 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2351 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
2352 if (hmapx_contains(&mirror->srcs, bundle)) {
2353 bundle->src_mirrors |= mirror_bit;
2355 bundle->src_mirrors &= ~mirror_bit;
2358 if (hmapx_contains(&mirror->dsts, bundle)) {
2359 bundle->dst_mirrors |= mirror_bit;
2361 bundle->dst_mirrors &= ~mirror_bit;
2364 if (mirror->out == bundle) {
2365 bundle->mirror_out |= mirror_bit;
2367 bundle->mirror_out &= ~mirror_bit;
2371 ofproto->need_revalidate = REV_RECONFIGURE;
2372 ofproto->has_mirrors = true;
2373 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
2374 mirror_update_dups(ofproto);
2380 mirror_destroy(struct ofmirror *mirror)
2382 struct ofproto_dpif *ofproto;
2383 mirror_mask_t mirror_bit;
2384 struct ofbundle *bundle;
2391 ofproto = mirror->ofproto;
2392 ofproto->need_revalidate = REV_RECONFIGURE;
2393 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
2395 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2396 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2397 bundle->src_mirrors &= ~mirror_bit;
2398 bundle->dst_mirrors &= ~mirror_bit;
2399 bundle->mirror_out &= ~mirror_bit;
2402 hmapx_destroy(&mirror->srcs);
2403 hmapx_destroy(&mirror->dsts);
2404 free(mirror->vlans);
2406 ofproto->mirrors[mirror->idx] = NULL;
2410 mirror_update_dups(ofproto);
2412 ofproto->has_mirrors = false;
2413 for (i = 0; i < MAX_MIRRORS; i++) {
2414 if (ofproto->mirrors[i]) {
2415 ofproto->has_mirrors = true;
2422 mirror_get_stats(struct ofproto *ofproto_, void *aux,
2423 uint64_t *packets, uint64_t *bytes)
2425 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2426 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2429 *packets = *bytes = UINT64_MAX;
2433 *packets = mirror->packet_count;
2434 *bytes = mirror->byte_count;
2440 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2442 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2443 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2444 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
2450 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2452 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2453 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2454 return bundle && bundle->mirror_out != 0;
2458 forward_bpdu_changed(struct ofproto *ofproto_)
2460 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2461 ofproto->need_revalidate = REV_RECONFIGURE;
2465 set_mac_idle_time(struct ofproto *ofproto_, unsigned int idle_time)
2467 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2468 mac_learning_set_idle_time(ofproto->ml, idle_time);
2473 static struct ofport_dpif *
2474 get_ofp_port(const struct ofproto_dpif *ofproto, uint16_t ofp_port)
2476 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2477 return ofport ? ofport_dpif_cast(ofport) : NULL;
2480 static struct ofport_dpif *
2481 get_odp_port(const struct ofproto_dpif *ofproto, uint32_t odp_port)
2483 return get_ofp_port(ofproto, odp_port_to_ofp_port(ofproto, odp_port));
2487 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2488 struct ofproto_port *ofproto_port,
2489 struct dpif_port *dpif_port)
2491 ofproto_port->name = dpif_port->name;
2492 ofproto_port->type = dpif_port->type;
2493 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2497 port_run_fast(struct ofport_dpif *ofport)
2499 if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) {
2500 struct ofpbuf packet;
2502 ofpbuf_init(&packet, 0);
2503 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr);
2504 send_packet(ofport, &packet);
2505 ofpbuf_uninit(&packet);
2510 port_run(struct ofport_dpif *ofport)
2512 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2513 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2514 bool enable = netdev_get_carrier(ofport->up.netdev);
2516 ofport->carrier_seq = carrier_seq;
2518 port_run_fast(ofport);
2520 int cfm_opup = cfm_get_opup(ofport->cfm);
2522 cfm_run(ofport->cfm);
2523 enable = enable && !cfm_get_fault(ofport->cfm);
2525 if (cfm_opup >= 0) {
2526 enable = enable && cfm_opup;
2530 if (ofport->bundle) {
2531 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2532 if (carrier_changed) {
2533 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2537 if (ofport->may_enable != enable) {
2538 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2540 if (ofproto->has_bundle_action) {
2541 ofproto->need_revalidate = REV_PORT_TOGGLED;
2545 ofport->may_enable = enable;
2549 port_wait(struct ofport_dpif *ofport)
2552 cfm_wait(ofport->cfm);
2557 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2558 struct ofproto_port *ofproto_port)
2560 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2561 struct dpif_port dpif_port;
2564 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2566 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2572 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2574 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2575 uint32_t odp_port = UINT32_MAX;
2577 return dpif_port_add(ofproto->dpif, netdev, &odp_port);
2581 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2583 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2584 uint32_t odp_port = ofp_port_to_odp_port(ofproto, ofp_port);
2587 if (odp_port != OFPP_NONE) {
2588 error = dpif_port_del(ofproto->dpif, odp_port);
2591 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2593 /* The caller is going to close ofport->up.netdev. If this is a
2594 * bonded port, then the bond is using that netdev, so remove it
2595 * from the bond. The client will need to reconfigure everything
2596 * after deleting ports, so then the slave will get re-added. */
2597 bundle_remove(&ofport->up);
2604 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2606 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2609 error = netdev_get_stats(ofport->up.netdev, stats);
2611 if (!error && ofport->odp_port == OVSP_LOCAL) {
2612 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2614 /* ofproto->stats.tx_packets represents packets that we created
2615 * internally and sent to some port (e.g. packets sent with
2616 * send_packet()). Account for them as if they had come from
2617 * OFPP_LOCAL and got forwarded. */
2619 if (stats->rx_packets != UINT64_MAX) {
2620 stats->rx_packets += ofproto->stats.tx_packets;
2623 if (stats->rx_bytes != UINT64_MAX) {
2624 stats->rx_bytes += ofproto->stats.tx_bytes;
2627 /* ofproto->stats.rx_packets represents packets that were received on
2628 * some port and we processed internally and dropped (e.g. STP).
2629 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2631 if (stats->tx_packets != UINT64_MAX) {
2632 stats->tx_packets += ofproto->stats.rx_packets;
2635 if (stats->tx_bytes != UINT64_MAX) {
2636 stats->tx_bytes += ofproto->stats.rx_bytes;
2643 /* Account packets for LOCAL port. */
2645 ofproto_update_local_port_stats(const struct ofproto *ofproto_,
2646 size_t tx_size, size_t rx_size)
2648 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2651 ofproto->stats.rx_packets++;
2652 ofproto->stats.rx_bytes += rx_size;
2655 ofproto->stats.tx_packets++;
2656 ofproto->stats.tx_bytes += tx_size;
2660 struct port_dump_state {
2661 struct dpif_port_dump dump;
2666 port_dump_start(const struct ofproto *ofproto_, void **statep)
2668 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2669 struct port_dump_state *state;
2671 *statep = state = xmalloc(sizeof *state);
2672 dpif_port_dump_start(&state->dump, ofproto->dpif);
2673 state->done = false;
2678 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2679 struct ofproto_port *port)
2681 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2682 struct port_dump_state *state = state_;
2683 struct dpif_port dpif_port;
2685 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2686 ofproto_port_from_dpif_port(ofproto, port, &dpif_port);
2689 int error = dpif_port_dump_done(&state->dump);
2691 return error ? error : EOF;
2696 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2698 struct port_dump_state *state = state_;
2701 dpif_port_dump_done(&state->dump);
2708 port_poll(const struct ofproto *ofproto_, char **devnamep)
2710 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2711 return dpif_port_poll(ofproto->dpif, devnamep);
2715 port_poll_wait(const struct ofproto *ofproto_)
2717 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2718 dpif_port_poll_wait(ofproto->dpif);
2722 port_is_lacp_current(const struct ofport *ofport_)
2724 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2725 return (ofport->bundle && ofport->bundle->lacp
2726 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2730 /* Upcall handling. */
2732 /* Flow miss batching.
2734 * Some dpifs implement operations faster when you hand them off in a batch.
2735 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2736 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2737 * more packets, plus possibly installing the flow in the dpif.
2739 * So far we only batch the operations that affect flow setup time the most.
2740 * It's possible to batch more than that, but the benefit might be minimal. */
2742 struct hmap_node hmap_node;
2744 enum odp_key_fitness key_fitness;
2745 const struct nlattr *key;
2747 ovs_be16 initial_tci;
2748 struct list packets;
2749 enum dpif_upcall_type upcall_type;
2752 struct flow_miss_op {
2753 struct dpif_op dpif_op;
2754 struct subfacet *subfacet; /* Subfacet */
2755 void *garbage; /* Pointer to pass to free(), NULL if none. */
2756 uint64_t stub[1024 / 8]; /* Temporary buffer. */
2759 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2760 * OpenFlow controller as necessary according to their individual
2761 * configurations. */
2763 send_packet_in_miss(struct ofproto_dpif *ofproto, const struct ofpbuf *packet,
2764 const struct flow *flow)
2766 struct ofputil_packet_in pin;
2768 pin.packet = packet->data;
2769 pin.packet_len = packet->size;
2770 pin.reason = OFPR_NO_MATCH;
2771 pin.controller_id = 0;
2776 pin.send_len = 0; /* not used for flow table misses */
2778 flow_get_metadata(flow, &pin.fmd);
2780 connmgr_send_packet_in(ofproto->up.connmgr, &pin);
2783 static enum slow_path_reason
2784 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2785 const struct ofpbuf *packet)
2787 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2793 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2795 cfm_process_heartbeat(ofport->cfm, packet);
2798 } else if (ofport->bundle && ofport->bundle->lacp
2799 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2801 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2804 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2806 stp_process_packet(ofport, packet);
2813 static struct flow_miss *
2814 flow_miss_find(struct hmap *todo, const struct flow *flow, uint32_t hash)
2816 struct flow_miss *miss;
2818 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2819 if (flow_equal(&miss->flow, flow)) {
2827 /* Partially Initializes 'op' as an "execute" operation for 'miss' and
2828 * 'packet'. The caller must initialize op->actions and op->actions_len. If
2829 * 'miss' is associated with a subfacet the caller must also initialize the
2830 * returned op->subfacet, and if anything needs to be freed after processing
2831 * the op, the caller must initialize op->garbage also. */
2833 init_flow_miss_execute_op(struct flow_miss *miss, struct ofpbuf *packet,
2834 struct flow_miss_op *op)
2836 if (miss->flow.vlan_tci != miss->initial_tci) {
2837 /* This packet was received on a VLAN splinter port. We
2838 * added a VLAN to the packet to make the packet resemble
2839 * the flow, but the actions were composed assuming that
2840 * the packet contained no VLAN. So, we must remove the
2841 * VLAN header from the packet before trying to execute the
2843 eth_pop_vlan(packet);
2846 op->subfacet = NULL;
2848 op->dpif_op.type = DPIF_OP_EXECUTE;
2849 op->dpif_op.u.execute.key = miss->key;
2850 op->dpif_op.u.execute.key_len = miss->key_len;
2851 op->dpif_op.u.execute.packet = packet;
2854 /* Helper for handle_flow_miss_without_facet() and
2855 * handle_flow_miss_with_facet(). */
2857 handle_flow_miss_common(struct rule_dpif *rule,
2858 struct ofpbuf *packet, const struct flow *flow)
2860 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2862 ofproto->n_matches++;
2864 if (rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2866 * Extra-special case for fail-open mode.
2868 * We are in fail-open mode and the packet matched the fail-open
2869 * rule, but we are connected to a controller too. We should send
2870 * the packet up to the controller in the hope that it will try to
2871 * set up a flow and thereby allow us to exit fail-open.
2873 * See the top-level comment in fail-open.c for more information.
2875 send_packet_in_miss(ofproto, packet, flow);
2879 /* Figures out whether a flow that missed in 'ofproto', whose details are in
2880 * 'miss', is likely to be worth tracking in detail in userspace and (usually)
2881 * installing a datapath flow. The answer is usually "yes" (a return value of
2882 * true). However, for short flows the cost of bookkeeping is much higher than
2883 * the benefits, so when the datapath holds a large number of flows we impose
2884 * some heuristics to decide which flows are likely to be worth tracking. */
2886 flow_miss_should_make_facet(struct ofproto_dpif *ofproto,
2887 struct flow_miss *miss, uint32_t hash)
2889 if (!ofproto->governor) {
2892 n_subfacets = hmap_count(&ofproto->subfacets);
2893 if (n_subfacets * 2 <= ofproto->up.flow_eviction_threshold) {
2897 ofproto->governor = governor_create(ofproto->up.name);
2900 return governor_should_install_flow(ofproto->governor, hash,
2901 list_size(&miss->packets));
2904 /* Handles 'miss', which matches 'rule', without creating a facet or subfacet
2905 * or creating any datapath flow. May add an "execute" operation to 'ops' and
2906 * increment '*n_ops'. */
2908 handle_flow_miss_without_facet(struct flow_miss *miss,
2909 struct rule_dpif *rule,
2910 struct flow_miss_op *ops, size_t *n_ops)
2912 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2913 long long int now = time_msec();
2914 struct action_xlate_ctx ctx;
2915 struct ofpbuf *packet;
2917 LIST_FOR_EACH (packet, list_node, &miss->packets) {
2918 struct flow_miss_op *op = &ops[*n_ops];
2919 struct dpif_flow_stats stats;
2920 struct ofpbuf odp_actions;
2922 COVERAGE_INC(facet_suppress);
2924 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
2926 dpif_flow_stats_extract(&miss->flow, packet, now, &stats);
2927 rule_credit_stats(rule, &stats);
2929 action_xlate_ctx_init(&ctx, ofproto, &miss->flow, miss->initial_tci,
2931 ctx.resubmit_stats = &stats;
2932 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len,
2935 if (odp_actions.size) {
2936 struct dpif_execute *execute = &op->dpif_op.u.execute;
2938 init_flow_miss_execute_op(miss, packet, op);
2939 execute->actions = odp_actions.data;
2940 execute->actions_len = odp_actions.size;
2941 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
2945 ofpbuf_uninit(&odp_actions);
2950 /* Handles 'miss', which matches 'facet'. May add any required datapath
2951 * operations to 'ops', incrementing '*n_ops' for each new op.
2953 * All of the packets in 'miss' are considered to have arrived at time 'now'.
2954 * This is really important only for new facets: if we just called time_msec()
2955 * here, then the new subfacet or its packets could look (occasionally) as
2956 * though it was used some time after the facet was used. That can make a
2957 * one-packet flow look like it has a nonzero duration, which looks odd in
2958 * e.g. NetFlow statistics. */
2960 handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
2962 struct flow_miss_op *ops, size_t *n_ops)
2964 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
2965 enum subfacet_path want_path;
2966 struct subfacet *subfacet;
2967 struct ofpbuf *packet;
2969 subfacet = subfacet_create(facet,
2970 miss->key_fitness, miss->key, miss->key_len,
2971 miss->initial_tci, now);
2973 LIST_FOR_EACH (packet, list_node, &miss->packets) {
2974 struct flow_miss_op *op = &ops[*n_ops];
2975 struct dpif_flow_stats stats;
2976 struct ofpbuf odp_actions;
2978 handle_flow_miss_common(facet->rule, packet, &miss->flow);
2980 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
2981 if (!subfacet->actions || subfacet->slow) {
2982 subfacet_make_actions(subfacet, packet, &odp_actions);
2985 dpif_flow_stats_extract(&facet->flow, packet, now, &stats);
2986 subfacet_update_stats(subfacet, &stats);
2988 if (subfacet->actions_len) {
2989 struct dpif_execute *execute = &op->dpif_op.u.execute;
2991 init_flow_miss_execute_op(miss, packet, op);
2992 op->subfacet = subfacet;
2993 if (!subfacet->slow) {
2994 execute->actions = subfacet->actions;
2995 execute->actions_len = subfacet->actions_len;
2996 ofpbuf_uninit(&odp_actions);
2998 execute->actions = odp_actions.data;
2999 execute->actions_len = odp_actions.size;
3000 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
3005 ofpbuf_uninit(&odp_actions);
3009 want_path = subfacet_want_path(subfacet->slow);
3010 if (miss->upcall_type == DPIF_UC_MISS || subfacet->path != want_path) {
3011 struct flow_miss_op *op = &ops[(*n_ops)++];
3012 struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
3014 op->subfacet = subfacet;
3016 op->dpif_op.type = DPIF_OP_FLOW_PUT;
3017 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3018 put->key = miss->key;
3019 put->key_len = miss->key_len;
3020 if (want_path == SF_FAST_PATH) {
3021 put->actions = subfacet->actions;
3022 put->actions_len = subfacet->actions_len;
3024 compose_slow_path(ofproto, &facet->flow, subfacet->slow,
3025 op->stub, sizeof op->stub,
3026 &put->actions, &put->actions_len);
3032 /* Handles flow miss 'miss' on 'ofproto'. May add any required datapath
3033 * operations to 'ops', incrementing '*n_ops' for each new op. */
3035 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
3036 struct flow_miss_op *ops, size_t *n_ops)
3038 struct facet *facet;
3042 /* The caller must ensure that miss->hmap_node.hash contains
3043 * flow_hash(miss->flow, 0). */
3044 hash = miss->hmap_node.hash;
3046 facet = facet_lookup_valid(ofproto, &miss->flow, hash);
3048 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &miss->flow);
3050 if (!flow_miss_should_make_facet(ofproto, miss, hash)) {
3051 handle_flow_miss_without_facet(miss, rule, ops, n_ops);
3055 facet = facet_create(rule, &miss->flow, hash);
3060 handle_flow_miss_with_facet(miss, facet, now, ops, n_ops);
3063 /* Like odp_flow_key_to_flow(), this function converts the 'key_len' bytes of
3064 * OVS_KEY_ATTR_* attributes in 'key' to a flow structure in 'flow' and returns
3065 * an ODP_FIT_* value that indicates how well 'key' fits our expectations for
3066 * what a flow key should contain.
3068 * This function also includes some logic to help make VLAN splinters
3069 * transparent to the rest of the upcall processing logic. In particular, if
3070 * the extracted in_port is a VLAN splinter port, it replaces flow->in_port by
3071 * the "real" port, sets flow->vlan_tci correctly for the VLAN of the VLAN
3072 * splinter port, and pushes a VLAN header onto 'packet' (if it is nonnull).
3074 * Sets '*initial_tci' to the VLAN TCI with which the packet was really
3075 * received, that is, the actual VLAN TCI extracted by odp_flow_key_to_flow().
3076 * (This differs from the value returned in flow->vlan_tci only for packets
3077 * received on VLAN splinters.)
3079 static enum odp_key_fitness
3080 ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto,
3081 const struct nlattr *key, size_t key_len,
3082 struct flow *flow, ovs_be16 *initial_tci,
3083 struct ofpbuf *packet)
3085 enum odp_key_fitness fitness;
3087 fitness = odp_flow_key_to_flow(key, key_len, flow);
3088 flow->in_port = odp_port_to_ofp_port(ofproto, flow->in_port);
3089 if (fitness == ODP_FIT_ERROR) {
3092 *initial_tci = flow->vlan_tci;
3094 if (vsp_adjust_flow(ofproto, flow)) {
3096 /* Make the packet resemble the flow, so that it gets sent to an
3097 * OpenFlow controller properly, so that it looks correct for
3098 * sFlow, and so that flow_extract() will get the correct vlan_tci
3099 * if it is called on 'packet'.
3101 * The allocated space inside 'packet' probably also contains
3102 * 'key', that is, both 'packet' and 'key' are probably part of a
3103 * struct dpif_upcall (see the large comment on that structure
3104 * definition), so pushing data on 'packet' is in general not a
3105 * good idea since it could overwrite 'key' or free it as a side
3106 * effect. However, it's OK in this special case because we know
3107 * that 'packet' is inside a Netlink attribute: pushing 4 bytes
3108 * will just overwrite the 4-byte "struct nlattr", which is fine
3109 * since we don't need that header anymore. */
3110 eth_push_vlan(packet, flow->vlan_tci);
3113 /* Let the caller know that we can't reproduce 'key' from 'flow'. */
3114 if (fitness == ODP_FIT_PERFECT) {
3115 fitness = ODP_FIT_TOO_MUCH;
3123 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
3126 struct dpif_upcall *upcall;
3127 struct flow_miss *miss;
3128 struct flow_miss misses[FLOW_MISS_MAX_BATCH];
3129 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
3130 struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
3140 /* Construct the to-do list.
3142 * This just amounts to extracting the flow from each packet and sticking
3143 * the packets that have the same flow in the same "flow_miss" structure so
3144 * that we can process them together. */
3147 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
3148 struct flow_miss *miss = &misses[n_misses];
3149 struct flow_miss *existing_miss;
3153 /* Obtain metadata and check userspace/kernel agreement on flow match,
3154 * then set 'flow''s header pointers. */
3155 miss->key_fitness = ofproto_dpif_extract_flow_key(
3156 ofproto, upcall->key, upcall->key_len,
3157 &flow, &miss->initial_tci, upcall->packet);
3158 if (miss->key_fitness == ODP_FIT_ERROR) {
3161 flow_extract(upcall->packet, flow.skb_priority,
3162 &flow.tunnel, flow.in_port, &miss->flow);
3164 /* Add other packets to a to-do list. */
3165 hash = flow_hash(&miss->flow, 0);
3166 existing_miss = flow_miss_find(&todo, &miss->flow, hash);
3167 if (!existing_miss) {
3168 hmap_insert(&todo, &miss->hmap_node, hash);
3169 miss->key = upcall->key;
3170 miss->key_len = upcall->key_len;
3171 miss->upcall_type = upcall->type;
3172 list_init(&miss->packets);
3176 miss = existing_miss;
3178 list_push_back(&miss->packets, &upcall->packet->list_node);
3181 /* Process each element in the to-do list, constructing the set of
3182 * operations to batch. */
3184 HMAP_FOR_EACH (miss, hmap_node, &todo) {
3185 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
3187 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
3189 /* Execute batch. */
3190 for (i = 0; i < n_ops; i++) {
3191 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3193 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
3195 /* Free memory and update facets. */
3196 for (i = 0; i < n_ops; i++) {
3197 struct flow_miss_op *op = &flow_miss_ops[i];
3199 switch (op->dpif_op.type) {
3200 case DPIF_OP_EXECUTE:
3203 case DPIF_OP_FLOW_PUT:
3204 if (!op->dpif_op.error) {
3205 op->subfacet->path = subfacet_want_path(op->subfacet->slow);
3209 case DPIF_OP_FLOW_DEL:
3215 hmap_destroy(&todo);
3218 static enum { SFLOW_UPCALL, MISS_UPCALL, BAD_UPCALL }
3219 classify_upcall(const struct dpif_upcall *upcall)
3221 union user_action_cookie cookie;
3223 /* First look at the upcall type. */
3224 switch (upcall->type) {
3225 case DPIF_UC_ACTION:
3231 case DPIF_N_UC_TYPES:
3233 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3237 /* "action" upcalls need a closer look. */
3238 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
3239 switch (cookie.type) {
3240 case USER_ACTION_COOKIE_SFLOW:
3241 return SFLOW_UPCALL;
3243 case USER_ACTION_COOKIE_SLOW_PATH:
3246 case USER_ACTION_COOKIE_UNSPEC:
3248 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
3254 handle_sflow_upcall(struct ofproto_dpif *ofproto,
3255 const struct dpif_upcall *upcall)
3257 union user_action_cookie cookie;
3258 enum odp_key_fitness fitness;
3259 ovs_be16 initial_tci;
3261 uint32_t odp_in_port;
3263 fitness = ofproto_dpif_extract_flow_key(ofproto, upcall->key,
3264 upcall->key_len, &flow,
3265 &initial_tci, upcall->packet);
3266 if (fitness == ODP_FIT_ERROR) {
3270 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
3271 odp_in_port = ofp_port_to_odp_port(ofproto, flow.in_port);
3272 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
3273 odp_in_port, &cookie);
3277 handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
3279 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
3280 struct ofpbuf miss_bufs[FLOW_MISS_MAX_BATCH];
3281 uint64_t miss_buf_stubs[FLOW_MISS_MAX_BATCH][4096 / 8];
3286 assert(max_batch <= FLOW_MISS_MAX_BATCH);
3289 for (n_processed = 0; n_processed < max_batch; n_processed++) {
3290 struct dpif_upcall *upcall = &misses[n_misses];
3291 struct ofpbuf *buf = &miss_bufs[n_misses];
3294 ofpbuf_use_stub(buf, miss_buf_stubs[n_misses],
3295 sizeof miss_buf_stubs[n_misses]);
3296 error = dpif_recv(ofproto->dpif, upcall, buf);
3302 switch (classify_upcall(upcall)) {
3304 /* Handle it later. */
3309 if (ofproto->sflow) {
3310 handle_sflow_upcall(ofproto, upcall);
3321 /* Handle deferred MISS_UPCALL processing. */
3322 handle_miss_upcalls(ofproto, misses, n_misses);
3323 for (i = 0; i < n_misses; i++) {
3324 ofpbuf_uninit(&miss_bufs[i]);
3330 /* Flow expiration. */
3332 static int subfacet_max_idle(const struct ofproto_dpif *);
3333 static void update_stats(struct ofproto_dpif *);
3334 static void rule_expire(struct rule_dpif *);
3335 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
3337 /* This function is called periodically by run(). Its job is to collect
3338 * updates for the flows that have been installed into the datapath, most
3339 * importantly when they last were used, and then use that information to
3340 * expire flows that have not been used recently.
3342 * Returns the number of milliseconds after which it should be called again. */
3344 expire(struct ofproto_dpif *ofproto)
3346 struct rule_dpif *rule, *next_rule;
3347 struct oftable *table;
3350 /* Update stats for each flow in the datapath. */
3351 update_stats(ofproto);
3353 /* Expire subfacets that have been idle too long. */
3354 dp_max_idle = subfacet_max_idle(ofproto);
3355 expire_subfacets(ofproto, dp_max_idle);
3357 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3358 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
3359 struct cls_cursor cursor;
3361 cls_cursor_init(&cursor, &table->cls, NULL);
3362 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
3367 /* All outstanding data in existing flows has been accounted, so it's a
3368 * good time to do bond rebalancing. */
3369 if (ofproto->has_bonded_bundles) {
3370 struct ofbundle *bundle;
3372 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3374 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
3379 return MIN(dp_max_idle, 1000);
3382 /* Updates flow table statistics given that the datapath just reported 'stats'
3383 * as 'subfacet''s statistics. */
3385 update_subfacet_stats(struct subfacet *subfacet,
3386 const struct dpif_flow_stats *stats)
3388 struct facet *facet = subfacet->facet;
3390 if (stats->n_packets >= subfacet->dp_packet_count) {
3391 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
3392 facet->packet_count += extra;
3394 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3397 if (stats->n_bytes >= subfacet->dp_byte_count) {
3398 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
3400 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3403 subfacet->dp_packet_count = stats->n_packets;
3404 subfacet->dp_byte_count = stats->n_bytes;
3406 facet->tcp_flags |= stats->tcp_flags;
3408 subfacet_update_time(subfacet, stats->used);
3409 if (facet->accounted_bytes < facet->byte_count) {
3411 facet_account(facet);
3412 facet->accounted_bytes = facet->byte_count;
3414 facet_push_stats(facet);
3417 /* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
3418 * about, or a flow that shouldn't be installed but was anyway. Delete it. */
3420 delete_unexpected_flow(struct dpif *dpif,
3421 const struct nlattr *key, size_t key_len)
3423 if (!VLOG_DROP_WARN(&rl)) {
3427 odp_flow_key_format(key, key_len, &s);
3428 VLOG_WARN("unexpected flow from datapath %s", ds_cstr(&s));
3432 COVERAGE_INC(facet_unexpected);
3433 dpif_flow_del(dpif, key, key_len, NULL);
3436 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3438 * This function also pushes statistics updates to rules which each facet
3439 * resubmits into. Generally these statistics will be accurate. However, if a
3440 * facet changes the rule it resubmits into at some time in between
3441 * update_stats() runs, it is possible that statistics accrued to the
3442 * old rule will be incorrectly attributed to the new rule. This could be
3443 * avoided by calling update_stats() whenever rules are created or
3444 * deleted. However, the performance impact of making so many calls to the
3445 * datapath do not justify the benefit of having perfectly accurate statistics.
3448 update_stats(struct ofproto_dpif *p)
3450 const struct dpif_flow_stats *stats;
3451 struct dpif_flow_dump dump;
3452 const struct nlattr *key;
3455 dpif_flow_dump_start(&dump, p->dpif);
3456 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3457 struct subfacet *subfacet;
3459 subfacet = subfacet_find(p, key, key_len);
3460 switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
3462 update_subfacet_stats(subfacet, stats);
3466 /* Stats are updated per-packet. */
3469 case SF_NOT_INSTALLED:
3471 delete_unexpected_flow(p->dpif, key, key_len);
3475 dpif_flow_dump_done(&dump);
3478 /* Calculates and returns the number of milliseconds of idle time after which
3479 * subfacets should expire from the datapath. When a subfacet expires, we fold
3480 * its statistics into its facet, and when a facet's last subfacet expires, we
3481 * fold its statistic into its rule. */
3483 subfacet_max_idle(const struct ofproto_dpif *ofproto)
3486 * Idle time histogram.
3488 * Most of the time a switch has a relatively small number of subfacets.
3489 * When this is the case we might as well keep statistics for all of them
3490 * in userspace and to cache them in the kernel datapath for performance as
3493 * As the number of subfacets increases, the memory required to maintain
3494 * statistics about them in userspace and in the kernel becomes
3495 * significant. However, with a large number of subfacets it is likely
3496 * that only a few of them are "heavy hitters" that consume a large amount
3497 * of bandwidth. At this point, only heavy hitters are worth caching in
3498 * the kernel and maintaining in userspaces; other subfacets we can
3501 * The technique used to compute the idle time is to build a histogram with
3502 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
3503 * that is installed in the kernel gets dropped in the appropriate bucket.
3504 * After the histogram has been built, we compute the cutoff so that only
3505 * the most-recently-used 1% of subfacets (but at least
3506 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
3507 * the most-recently-used bucket of subfacets is kept, so actually an
3508 * arbitrary number of subfacets can be kept in any given expiration run
3509 * (though the next run will delete most of those unless they receive
3512 * This requires a second pass through the subfacets, in addition to the
3513 * pass made by update_stats(), because the former function never looks at
3514 * uninstallable subfacets.
3516 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
3517 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3518 int buckets[N_BUCKETS] = { 0 };
3519 int total, subtotal, bucket;
3520 struct subfacet *subfacet;
3524 total = hmap_count(&ofproto->subfacets);
3525 if (total <= ofproto->up.flow_eviction_threshold) {
3526 return N_BUCKETS * BUCKET_WIDTH;
3529 /* Build histogram. */
3531 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
3532 long long int idle = now - subfacet->used;
3533 int bucket = (idle <= 0 ? 0
3534 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
3535 : (unsigned int) idle / BUCKET_WIDTH);
3539 /* Find the first bucket whose flows should be expired. */
3540 subtotal = bucket = 0;
3542 subtotal += buckets[bucket++];
3543 } while (bucket < N_BUCKETS &&
3544 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
3546 if (VLOG_IS_DBG_ENABLED()) {
3550 ds_put_cstr(&s, "keep");
3551 for (i = 0; i < N_BUCKETS; i++) {
3553 ds_put_cstr(&s, ", drop");
3556 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
3559 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
3563 return bucket * BUCKET_WIDTH;
3566 enum { EXPIRE_MAX_BATCH = 50 };
3569 expire_batch(struct ofproto_dpif *ofproto, struct subfacet **subfacets, int n)
3571 struct odputil_keybuf keybufs[EXPIRE_MAX_BATCH];
3572 struct dpif_op ops[EXPIRE_MAX_BATCH];
3573 struct dpif_op *opsp[EXPIRE_MAX_BATCH];
3574 struct ofpbuf keys[EXPIRE_MAX_BATCH];
3575 struct dpif_flow_stats stats[EXPIRE_MAX_BATCH];
3578 for (i = 0; i < n; i++) {
3579 ops[i].type = DPIF_OP_FLOW_DEL;
3580 subfacet_get_key(subfacets[i], &keybufs[i], &keys[i]);
3581 ops[i].u.flow_del.key = keys[i].data;
3582 ops[i].u.flow_del.key_len = keys[i].size;
3583 ops[i].u.flow_del.stats = &stats[i];
3587 dpif_operate(ofproto->dpif, opsp, n);
3588 for (i = 0; i < n; i++) {
3589 subfacet_reset_dp_stats(subfacets[i], &stats[i]);
3590 subfacets[i]->path = SF_NOT_INSTALLED;
3591 subfacet_destroy(subfacets[i]);
3596 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
3598 /* Cutoff time for most flows. */
3599 long long int normal_cutoff = time_msec() - dp_max_idle;
3601 /* We really want to keep flows for special protocols around, so use a more
3602 * conservative cutoff. */
3603 long long int special_cutoff = time_msec() - 10000;
3605 struct subfacet *subfacet, *next_subfacet;
3606 struct subfacet *batch[EXPIRE_MAX_BATCH];
3610 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
3611 &ofproto->subfacets) {
3612 long long int cutoff;
3614 cutoff = (subfacet->slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)
3617 if (subfacet->used < cutoff) {
3618 if (subfacet->path != SF_NOT_INSTALLED) {
3619 batch[n_batch++] = subfacet;
3620 if (n_batch >= EXPIRE_MAX_BATCH) {
3621 expire_batch(ofproto, batch, n_batch);
3625 subfacet_destroy(subfacet);
3631 expire_batch(ofproto, batch, n_batch);
3635 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3636 * then delete it entirely. */
3638 rule_expire(struct rule_dpif *rule)
3640 struct facet *facet, *next_facet;
3644 if (rule->up.pending) {
3645 /* We'll have to expire it later. */
3649 /* Has 'rule' expired? */
3651 if (rule->up.hard_timeout
3652 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
3653 reason = OFPRR_HARD_TIMEOUT;
3654 } else if (rule->up.idle_timeout
3655 && now > rule->up.used + rule->up.idle_timeout * 1000) {
3656 reason = OFPRR_IDLE_TIMEOUT;
3661 COVERAGE_INC(ofproto_dpif_expired);
3663 /* Update stats. (This is a no-op if the rule expired due to an idle
3664 * timeout, because that only happens when the rule has no facets left.) */
3665 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3666 facet_remove(facet);
3669 /* Get rid of the rule. */
3670 ofproto_rule_expire(&rule->up, reason);
3675 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
3677 * The caller must already have determined that no facet with an identical
3678 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
3679 * the ofproto's classifier table.
3681 * 'hash' must be the return value of flow_hash(flow, 0).
3683 * The facet will initially have no subfacets. The caller should create (at
3684 * least) one subfacet with subfacet_create(). */
3685 static struct facet *
3686 facet_create(struct rule_dpif *rule, const struct flow *flow, uint32_t hash)
3688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3689 struct facet *facet;
3691 facet = xzalloc(sizeof *facet);
3692 facet->used = time_msec();
3693 hmap_insert(&ofproto->facets, &facet->hmap_node, hash);
3694 list_push_back(&rule->facets, &facet->list_node);
3696 facet->flow = *flow;
3697 list_init(&facet->subfacets);
3698 netflow_flow_init(&facet->nf_flow);
3699 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
3705 facet_free(struct facet *facet)
3710 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
3711 * 'packet', which arrived on 'in_port'.
3713 * Takes ownership of 'packet'. */
3715 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
3716 const struct nlattr *odp_actions, size_t actions_len,
3717 struct ofpbuf *packet)
3719 struct odputil_keybuf keybuf;
3723 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3724 odp_flow_key_from_flow(&key, flow,
3725 ofp_port_to_odp_port(ofproto, flow->in_port));
3727 error = dpif_execute(ofproto->dpif, key.data, key.size,
3728 odp_actions, actions_len, packet);
3730 ofpbuf_delete(packet);
3734 /* Remove 'facet' from 'ofproto' and free up the associated memory:
3736 * - If 'facet' was installed in the datapath, uninstalls it and updates its
3737 * rule's statistics, via subfacet_uninstall().
3739 * - Removes 'facet' from its rule and from ofproto->facets.
3742 facet_remove(struct facet *facet)
3744 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3745 struct subfacet *subfacet, *next_subfacet;
3747 assert(!list_is_empty(&facet->subfacets));
3749 /* First uninstall all of the subfacets to get final statistics. */
3750 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3751 subfacet_uninstall(subfacet);
3754 /* Flush the final stats to the rule.
3756 * This might require us to have at least one subfacet around so that we
3757 * can use its actions for accounting in facet_account(), which is why we
3758 * have uninstalled but not yet destroyed the subfacets. */
3759 facet_flush_stats(facet);
3761 /* Now we're really all done so destroy everything. */
3762 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
3763 &facet->subfacets) {
3764 subfacet_destroy__(subfacet);
3766 hmap_remove(&ofproto->facets, &facet->hmap_node);
3767 list_remove(&facet->list_node);
3771 /* Feed information from 'facet' back into the learning table to keep it in
3772 * sync with what is actually flowing through the datapath. */
3774 facet_learn(struct facet *facet)
3776 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3777 struct action_xlate_ctx ctx;
3779 if (!facet->has_learn
3780 && !facet->has_normal
3781 && (!facet->has_fin_timeout
3782 || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
3786 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3787 facet->flow.vlan_tci,
3788 facet->rule, facet->tcp_flags, NULL);
3789 ctx.may_learn = true;
3790 xlate_actions_for_side_effects(&ctx, facet->rule->up.ofpacts,
3791 facet->rule->up.ofpacts_len);
3795 facet_account(struct facet *facet)
3797 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3798 struct subfacet *subfacet;
3799 const struct nlattr *a;
3804 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
3807 n_bytes = facet->byte_count - facet->accounted_bytes;
3809 /* This loop feeds byte counters to bond_account() for rebalancing to use
3810 * as a basis. We also need to track the actual VLAN on which the packet
3811 * is going to be sent to ensure that it matches the one passed to
3812 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
3815 * We use the actions from an arbitrary subfacet because they should all
3816 * be equally valid for our purpose. */
3817 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3818 struct subfacet, list_node);
3819 vlan_tci = facet->flow.vlan_tci;
3820 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3821 subfacet->actions, subfacet->actions_len) {
3822 const struct ovs_action_push_vlan *vlan;
3823 struct ofport_dpif *port;
3825 switch (nl_attr_type(a)) {
3826 case OVS_ACTION_ATTR_OUTPUT:
3827 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3828 if (port && port->bundle && port->bundle->bond) {
3829 bond_account(port->bundle->bond, &facet->flow,
3830 vlan_tci_to_vid(vlan_tci), n_bytes);
3834 case OVS_ACTION_ATTR_POP_VLAN:
3835 vlan_tci = htons(0);
3838 case OVS_ACTION_ATTR_PUSH_VLAN:
3839 vlan = nl_attr_get(a);
3840 vlan_tci = vlan->vlan_tci;
3846 /* Returns true if the only action for 'facet' is to send to the controller.
3847 * (We don't report NetFlow expiration messages for such facets because they
3848 * are just part of the control logic for the network, not real traffic). */
3850 facet_is_controller_flow(struct facet *facet)
3853 const struct rule *rule = &facet->rule->up;
3854 const struct ofpact *ofpacts = rule->ofpacts;
3855 size_t ofpacts_len = rule->ofpacts_len;
3857 if (ofpacts_len > 0 &&
3858 ofpacts->type == OFPACT_CONTROLLER &&
3859 ofpact_next(ofpacts) >= ofpact_end(ofpacts, ofpacts_len)) {
3866 /* Folds all of 'facet''s statistics into its rule. Also updates the
3867 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3868 * 'facet''s statistics in the datapath should have been zeroed and folded into
3869 * its packet and byte counts before this function is called. */
3871 facet_flush_stats(struct facet *facet)
3873 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3874 struct subfacet *subfacet;
3876 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3877 assert(!subfacet->dp_byte_count);
3878 assert(!subfacet->dp_packet_count);
3881 facet_push_stats(facet);
3882 if (facet->accounted_bytes < facet->byte_count) {
3883 facet_account(facet);
3884 facet->accounted_bytes = facet->byte_count;
3887 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3888 struct ofexpired expired;
3889 expired.flow = facet->flow;
3890 expired.packet_count = facet->packet_count;
3891 expired.byte_count = facet->byte_count;
3892 expired.used = facet->used;
3893 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3896 facet->rule->packet_count += facet->packet_count;
3897 facet->rule->byte_count += facet->byte_count;
3899 /* Reset counters to prevent double counting if 'facet' ever gets
3901 facet_reset_counters(facet);
3903 netflow_flow_clear(&facet->nf_flow);
3904 facet->tcp_flags = 0;
3907 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3908 * Returns it if found, otherwise a null pointer.
3910 * 'hash' must be the return value of flow_hash(flow, 0).
3912 * The returned facet might need revalidation; use facet_lookup_valid()
3913 * instead if that is important. */
3914 static struct facet *
3915 facet_find(struct ofproto_dpif *ofproto,
3916 const struct flow *flow, uint32_t hash)
3918 struct facet *facet;
3920 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, hash, &ofproto->facets) {
3921 if (flow_equal(flow, &facet->flow)) {
3929 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3930 * Returns it if found, otherwise a null pointer.
3932 * 'hash' must be the return value of flow_hash(flow, 0).
3934 * The returned facet is guaranteed to be valid. */
3935 static struct facet *
3936 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow,
3939 struct facet *facet;
3941 facet = facet_find(ofproto, flow, hash);
3943 && (ofproto->need_revalidate
3944 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))) {
3945 facet_revalidate(facet);
3952 subfacet_path_to_string(enum subfacet_path path)
3955 case SF_NOT_INSTALLED:
3956 return "not installed";
3958 return "in fast path";
3960 return "in slow path";
3966 /* Returns the path in which a subfacet should be installed if its 'slow'
3967 * member has the specified value. */
3968 static enum subfacet_path
3969 subfacet_want_path(enum slow_path_reason slow)
3971 return slow ? SF_SLOW_PATH : SF_FAST_PATH;
3974 /* Returns true if 'subfacet' needs to have its datapath flow updated,
3975 * supposing that its actions have been recalculated as 'want_actions' and that
3976 * 'slow' is nonzero iff 'subfacet' should be in the slow path. */
3978 subfacet_should_install(struct subfacet *subfacet, enum slow_path_reason slow,
3979 const struct ofpbuf *want_actions)
3981 enum subfacet_path want_path = subfacet_want_path(slow);
3982 return (want_path != subfacet->path
3983 || (want_path == SF_FAST_PATH
3984 && (subfacet->actions_len != want_actions->size
3985 || memcmp(subfacet->actions, want_actions->data,
3986 subfacet->actions_len))));
3990 facet_check_consistency(struct facet *facet)
3992 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3994 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3996 uint64_t odp_actions_stub[1024 / 8];
3997 struct ofpbuf odp_actions;
3999 struct rule_dpif *rule;
4000 struct subfacet *subfacet;
4001 bool may_log = false;
4004 /* Check the rule for consistency. */
4005 rule = rule_dpif_lookup(ofproto, &facet->flow);
4006 ok = rule == facet->rule;
4008 may_log = !VLOG_DROP_WARN(&rl);
4013 flow_format(&s, &facet->flow);
4014 ds_put_format(&s, ": facet associated with wrong rule (was "
4015 "table=%"PRIu8",", facet->rule->up.table_id);
4016 cls_rule_format(&facet->rule->up.cr, &s);
4017 ds_put_format(&s, ") (should have been table=%"PRIu8",",
4019 cls_rule_format(&rule->up.cr, &s);
4020 ds_put_char(&s, ')');
4022 VLOG_WARN("%s", ds_cstr(&s));
4027 /* Check the datapath actions for consistency. */
4028 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4029 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4030 enum subfacet_path want_path;
4031 struct odputil_keybuf keybuf;
4032 struct action_xlate_ctx ctx;
4036 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
4037 subfacet->initial_tci, rule, 0, NULL);
4038 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len,
4041 if (subfacet->path == SF_NOT_INSTALLED) {
4042 /* This only happens if the datapath reported an error when we
4043 * tried to install the flow. Don't flag another error here. */
4047 want_path = subfacet_want_path(subfacet->slow);
4048 if (want_path == SF_SLOW_PATH && subfacet->path == SF_SLOW_PATH) {
4049 /* The actions for slow-path flows may legitimately vary from one
4050 * packet to the next. We're done. */
4054 if (!subfacet_should_install(subfacet, subfacet->slow, &odp_actions)) {
4058 /* Inconsistency! */
4060 may_log = !VLOG_DROP_WARN(&rl);
4064 /* Rate-limited, skip reporting. */
4069 subfacet_get_key(subfacet, &keybuf, &key);
4070 odp_flow_key_format(key.data, key.size, &s);
4072 ds_put_cstr(&s, ": inconsistency in subfacet");
4073 if (want_path != subfacet->path) {
4074 enum odp_key_fitness fitness = subfacet->key_fitness;
4076 ds_put_format(&s, " (%s, fitness=%s)",
4077 subfacet_path_to_string(subfacet->path),
4078 odp_key_fitness_to_string(fitness));
4079 ds_put_format(&s, " (should have been %s)",
4080 subfacet_path_to_string(want_path));
4081 } else if (want_path == SF_FAST_PATH) {
4082 ds_put_cstr(&s, " (actions were: ");
4083 format_odp_actions(&s, subfacet->actions,
4084 subfacet->actions_len);
4085 ds_put_cstr(&s, ") (correct actions: ");
4086 format_odp_actions(&s, odp_actions.data, odp_actions.size);
4087 ds_put_char(&s, ')');
4089 ds_put_cstr(&s, " (actions: ");
4090 format_odp_actions(&s, subfacet->actions,
4091 subfacet->actions_len);
4092 ds_put_char(&s, ')');
4094 VLOG_WARN("%s", ds_cstr(&s));
4097 ofpbuf_uninit(&odp_actions);
4102 /* Re-searches the classifier for 'facet':
4104 * - If the rule found is different from 'facet''s current rule, moves
4105 * 'facet' to the new rule and recompiles its actions.
4107 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
4108 * where it is and recompiles its actions anyway. */
4110 facet_revalidate(struct facet *facet)
4112 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4114 struct nlattr *odp_actions;
4117 struct actions *new_actions;
4119 struct action_xlate_ctx ctx;
4120 uint64_t odp_actions_stub[1024 / 8];
4121 struct ofpbuf odp_actions;
4123 struct rule_dpif *new_rule;
4124 struct subfacet *subfacet;
4127 COVERAGE_INC(facet_revalidate);
4129 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
4131 /* Calculate new datapath actions.
4133 * We do not modify any 'facet' state yet, because we might need to, e.g.,
4134 * emit a NetFlow expiration and, if so, we need to have the old state
4135 * around to properly compose it. */
4137 /* If the datapath actions changed or the installability changed,
4138 * then we need to talk to the datapath. */
4141 memset(&ctx, 0, sizeof ctx);
4142 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4143 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4144 enum slow_path_reason slow;
4146 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
4147 subfacet->initial_tci, new_rule, 0, NULL);
4148 xlate_actions(&ctx, new_rule->up.ofpacts, new_rule->up.ofpacts_len,
4151 slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4152 if (subfacet_should_install(subfacet, slow, &odp_actions)) {
4153 struct dpif_flow_stats stats;
4155 subfacet_install(subfacet,
4156 odp_actions.data, odp_actions.size, &stats, slow);
4157 subfacet_update_stats(subfacet, &stats);
4160 new_actions = xcalloc(list_size(&facet->subfacets),
4161 sizeof *new_actions);
4163 new_actions[i].odp_actions = xmemdup(odp_actions.data,
4165 new_actions[i].actions_len = odp_actions.size;
4170 ofpbuf_uninit(&odp_actions);
4173 facet_flush_stats(facet);
4176 /* Update 'facet' now that we've taken care of all the old state. */
4177 facet->tags = ctx.tags;
4178 facet->nf_flow.output_iface = ctx.nf_output_iface;
4179 facet->has_learn = ctx.has_learn;
4180 facet->has_normal = ctx.has_normal;
4181 facet->has_fin_timeout = ctx.has_fin_timeout;
4182 facet->mirrors = ctx.mirrors;
4185 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4186 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4188 if (new_actions && new_actions[i].odp_actions) {
4189 free(subfacet->actions);
4190 subfacet->actions = new_actions[i].odp_actions;
4191 subfacet->actions_len = new_actions[i].actions_len;
4197 if (facet->rule != new_rule) {
4198 COVERAGE_INC(facet_changed_rule);
4199 list_remove(&facet->list_node);
4200 list_push_back(&new_rule->facets, &facet->list_node);
4201 facet->rule = new_rule;
4202 facet->used = new_rule->up.created;
4203 facet->prev_used = facet->used;
4207 /* Updates 'facet''s used time. Caller is responsible for calling
4208 * facet_push_stats() to update the flows which 'facet' resubmits into. */
4210 facet_update_time(struct facet *facet, long long int used)
4212 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4213 if (used > facet->used) {
4215 ofproto_rule_update_used(&facet->rule->up, used);
4216 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
4221 facet_reset_counters(struct facet *facet)
4223 facet->packet_count = 0;
4224 facet->byte_count = 0;
4225 facet->prev_packet_count = 0;
4226 facet->prev_byte_count = 0;
4227 facet->accounted_bytes = 0;
4231 facet_push_stats(struct facet *facet)
4233 struct dpif_flow_stats stats;
4235 assert(facet->packet_count >= facet->prev_packet_count);
4236 assert(facet->byte_count >= facet->prev_byte_count);
4237 assert(facet->used >= facet->prev_used);
4239 stats.n_packets = facet->packet_count - facet->prev_packet_count;
4240 stats.n_bytes = facet->byte_count - facet->prev_byte_count;
4241 stats.used = facet->used;
4242 stats.tcp_flags = 0;
4244 if (stats.n_packets || stats.n_bytes || facet->used > facet->prev_used) {
4245 facet->prev_packet_count = facet->packet_count;
4246 facet->prev_byte_count = facet->byte_count;
4247 facet->prev_used = facet->used;
4249 flow_push_stats(facet->rule, &facet->flow, &stats);
4251 update_mirror_stats(ofproto_dpif_cast(facet->rule->up.ofproto),
4252 facet->mirrors, stats.n_packets, stats.n_bytes);
4257 rule_credit_stats(struct rule_dpif *rule, const struct dpif_flow_stats *stats)
4259 rule->packet_count += stats->n_packets;
4260 rule->byte_count += stats->n_bytes;
4261 ofproto_rule_update_used(&rule->up, stats->used);
4264 /* Pushes flow statistics to the rules which 'flow' resubmits into given
4265 * 'rule''s actions and mirrors. */
4267 flow_push_stats(struct rule_dpif *rule,
4268 const struct flow *flow, const struct dpif_flow_stats *stats)
4270 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4271 struct action_xlate_ctx ctx;
4273 ofproto_rule_update_used(&rule->up, stats->used);
4275 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, rule,
4277 ctx.resubmit_stats = stats;
4278 xlate_actions_for_side_effects(&ctx, rule->up.ofpacts,
4279 rule->up.ofpacts_len);
4284 static struct subfacet *
4285 subfacet_find__(struct ofproto_dpif *ofproto,
4286 const struct nlattr *key, size_t key_len, uint32_t key_hash,
4287 const struct flow *flow)
4289 struct subfacet *subfacet;
4291 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
4292 &ofproto->subfacets) {
4294 ? (subfacet->key_len == key_len
4295 && !memcmp(key, subfacet->key, key_len))
4296 : flow_equal(flow, &subfacet->facet->flow)) {
4304 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
4305 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
4306 * there is one, otherwise creates and returns a new subfacet.
4308 * If the returned subfacet is new, then subfacet->actions will be NULL, in
4309 * which case the caller must populate the actions with
4310 * subfacet_make_actions(). */
4311 static struct subfacet *
4312 subfacet_create(struct facet *facet, enum odp_key_fitness key_fitness,
4313 const struct nlattr *key, size_t key_len,
4314 ovs_be16 initial_tci, long long int now)
4316 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4317 uint32_t key_hash = odp_flow_key_hash(key, key_len);
4318 struct subfacet *subfacet;
4320 if (list_is_empty(&facet->subfacets)) {
4321 subfacet = &facet->one_subfacet;
4323 subfacet = subfacet_find__(ofproto, key, key_len, key_hash,
4326 if (subfacet->facet == facet) {
4330 /* This shouldn't happen. */
4331 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
4332 subfacet_destroy(subfacet);
4335 subfacet = xmalloc(sizeof *subfacet);
4338 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
4339 list_push_back(&facet->subfacets, &subfacet->list_node);
4340 subfacet->facet = facet;
4341 subfacet->key_fitness = key_fitness;
4342 if (key_fitness != ODP_FIT_PERFECT) {
4343 subfacet->key = xmemdup(key, key_len);
4344 subfacet->key_len = key_len;
4346 subfacet->key = NULL;
4347 subfacet->key_len = 0;
4349 subfacet->used = now;
4350 subfacet->dp_packet_count = 0;
4351 subfacet->dp_byte_count = 0;
4352 subfacet->actions_len = 0;
4353 subfacet->actions = NULL;
4354 subfacet->slow = (subfacet->key_fitness == ODP_FIT_TOO_LITTLE
4357 subfacet->path = SF_NOT_INSTALLED;
4358 subfacet->initial_tci = initial_tci;
4363 /* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
4364 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
4365 static struct subfacet *
4366 subfacet_find(struct ofproto_dpif *ofproto,
4367 const struct nlattr *key, size_t key_len)
4369 uint32_t key_hash = odp_flow_key_hash(key, key_len);
4370 enum odp_key_fitness fitness;
4373 fitness = odp_flow_key_to_flow(key, key_len, &flow);
4374 flow.in_port = odp_port_to_ofp_port(ofproto, flow.in_port);
4375 if (fitness == ODP_FIT_ERROR) {
4379 return subfacet_find__(ofproto, key, key_len, key_hash, &flow);
4382 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
4383 * its facet within 'ofproto', and frees it. */
4385 subfacet_destroy__(struct subfacet *subfacet)
4387 struct facet *facet = subfacet->facet;
4388 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4390 subfacet_uninstall(subfacet);
4391 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
4392 list_remove(&subfacet->list_node);
4393 free(subfacet->key);
4394 free(subfacet->actions);
4395 if (subfacet != &facet->one_subfacet) {
4400 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
4401 * last remaining subfacet in its facet destroys the facet too. */
4403 subfacet_destroy(struct subfacet *subfacet)
4405 struct facet *facet = subfacet->facet;
4407 if (list_is_singleton(&facet->subfacets)) {
4408 /* facet_remove() needs at least one subfacet (it will remove it). */
4409 facet_remove(facet);
4411 subfacet_destroy__(subfacet);
4415 /* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
4416 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
4417 * for use as temporary storage. */
4419 subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
4423 if (!subfacet->key) {
4424 struct ofproto_dpif *ofproto;
4425 struct flow *flow = &subfacet->facet->flow;
4427 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
4428 ofproto = ofproto_dpif_cast(subfacet->facet->rule->up.ofproto);
4429 odp_flow_key_from_flow(key, flow,
4430 ofp_port_to_odp_port(ofproto, flow->in_port));
4432 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
4436 /* Composes the datapath actions for 'subfacet' based on its rule's actions.
4437 * Translates the actions into 'odp_actions', which the caller must have
4438 * initialized and is responsible for uninitializing. */
4440 subfacet_make_actions(struct subfacet *subfacet, const struct ofpbuf *packet,
4441 struct ofpbuf *odp_actions)
4443 struct facet *facet = subfacet->facet;
4444 struct rule_dpif *rule = facet->rule;
4445 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4447 struct action_xlate_ctx ctx;
4449 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, subfacet->initial_tci,
4451 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len, odp_actions);
4452 facet->tags = ctx.tags;
4453 facet->has_learn = ctx.has_learn;
4454 facet->has_normal = ctx.has_normal;
4455 facet->has_fin_timeout = ctx.has_fin_timeout;
4456 facet->nf_flow.output_iface = ctx.nf_output_iface;
4457 facet->mirrors = ctx.mirrors;
4459 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4460 if (subfacet->actions_len != odp_actions->size
4461 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
4462 free(subfacet->actions);
4463 subfacet->actions_len = odp_actions->size;
4464 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
4468 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
4469 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
4470 * in the datapath will be zeroed and 'stats' will be updated with traffic new
4471 * since 'subfacet' was last updated.
4473 * Returns 0 if successful, otherwise a positive errno value. */
4475 subfacet_install(struct subfacet *subfacet,
4476 const struct nlattr *actions, size_t actions_len,
4477 struct dpif_flow_stats *stats,
4478 enum slow_path_reason slow)
4480 struct facet *facet = subfacet->facet;
4481 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4482 enum subfacet_path path = subfacet_want_path(slow);
4483 uint64_t slow_path_stub[128 / 8];
4484 struct odputil_keybuf keybuf;
4485 enum dpif_flow_put_flags flags;
4489 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
4491 flags |= DPIF_FP_ZERO_STATS;
4494 if (path == SF_SLOW_PATH) {
4495 compose_slow_path(ofproto, &facet->flow, slow,
4496 slow_path_stub, sizeof slow_path_stub,
4497 &actions, &actions_len);
4500 subfacet_get_key(subfacet, &keybuf, &key);
4501 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
4502 actions, actions_len, stats);
4505 subfacet_reset_dp_stats(subfacet, stats);
4509 subfacet->path = path;
4515 subfacet_reinstall(struct subfacet *subfacet, struct dpif_flow_stats *stats)
4517 return subfacet_install(subfacet, subfacet->actions, subfacet->actions_len,
4518 stats, subfacet->slow);
4521 /* If 'subfacet' is installed in the datapath, uninstalls it. */
4523 subfacet_uninstall(struct subfacet *subfacet)
4525 if (subfacet->path != SF_NOT_INSTALLED) {
4526 struct rule_dpif *rule = subfacet->facet->rule;
4527 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4528 struct odputil_keybuf keybuf;
4529 struct dpif_flow_stats stats;
4533 subfacet_get_key(subfacet, &keybuf, &key);
4534 error = dpif_flow_del(ofproto->dpif, key.data, key.size, &stats);
4535 subfacet_reset_dp_stats(subfacet, &stats);
4537 subfacet_update_stats(subfacet, &stats);
4539 subfacet->path = SF_NOT_INSTALLED;
4541 assert(subfacet->dp_packet_count == 0);
4542 assert(subfacet->dp_byte_count == 0);
4546 /* Resets 'subfacet''s datapath statistics counters. This should be called
4547 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
4548 * non-null, it should contain the statistics returned by dpif when 'subfacet'
4549 * was reset in the datapath. 'stats' will be modified to include only
4550 * statistics new since 'subfacet' was last updated. */
4552 subfacet_reset_dp_stats(struct subfacet *subfacet,
4553 struct dpif_flow_stats *stats)
4556 && subfacet->dp_packet_count <= stats->n_packets
4557 && subfacet->dp_byte_count <= stats->n_bytes) {
4558 stats->n_packets -= subfacet->dp_packet_count;
4559 stats->n_bytes -= subfacet->dp_byte_count;
4562 subfacet->dp_packet_count = 0;
4563 subfacet->dp_byte_count = 0;
4566 /* Updates 'subfacet''s used time. The caller is responsible for calling
4567 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
4569 subfacet_update_time(struct subfacet *subfacet, long long int used)
4571 if (used > subfacet->used) {
4572 subfacet->used = used;
4573 facet_update_time(subfacet->facet, used);
4577 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
4579 * Because of the meaning of a subfacet's counters, it only makes sense to do
4580 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
4581 * represents a packet that was sent by hand or if it represents statistics
4582 * that have been cleared out of the datapath. */
4584 subfacet_update_stats(struct subfacet *subfacet,
4585 const struct dpif_flow_stats *stats)
4587 if (stats->n_packets || stats->used > subfacet->used) {
4588 struct facet *facet = subfacet->facet;
4590 subfacet_update_time(subfacet, stats->used);
4591 facet->packet_count += stats->n_packets;
4592 facet->byte_count += stats->n_bytes;
4593 facet->tcp_flags |= stats->tcp_flags;
4594 facet_push_stats(facet);
4595 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
4601 static struct rule_dpif *
4602 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
4604 struct rule_dpif *rule;
4606 rule = rule_dpif_lookup__(ofproto, flow, 0);
4611 return rule_dpif_miss_rule(ofproto, flow);
4614 static struct rule_dpif *
4615 rule_dpif_lookup__(struct ofproto_dpif *ofproto, const struct flow *flow,
4618 struct cls_rule *cls_rule;
4619 struct classifier *cls;
4621 if (table_id >= N_TABLES) {
4625 cls = &ofproto->up.tables[table_id].cls;
4626 if (flow->nw_frag & FLOW_NW_FRAG_ANY
4627 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
4628 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
4629 * are unavailable. */
4630 struct flow ofpc_normal_flow = *flow;
4631 ofpc_normal_flow.tp_src = htons(0);
4632 ofpc_normal_flow.tp_dst = htons(0);
4633 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
4635 cls_rule = classifier_lookup(cls, flow);
4637 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
4640 static struct rule_dpif *
4641 rule_dpif_miss_rule(struct ofproto_dpif *ofproto, const struct flow *flow)
4643 struct ofport_dpif *port;
4645 port = get_ofp_port(ofproto, flow->in_port);
4647 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, flow->in_port);
4648 return ofproto->miss_rule;
4651 if (port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN) {
4652 return ofproto->no_packet_in_rule;
4654 return ofproto->miss_rule;
4658 complete_operation(struct rule_dpif *rule)
4660 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4662 rule_invalidate(rule);
4664 struct dpif_completion *c = xmalloc(sizeof *c);
4665 c->op = rule->up.pending;
4666 list_push_back(&ofproto->completions, &c->list_node);
4668 ofoperation_complete(rule->up.pending, 0);
4672 static struct rule *
4675 struct rule_dpif *rule = xmalloc(sizeof *rule);
4680 rule_dealloc(struct rule *rule_)
4682 struct rule_dpif *rule = rule_dpif_cast(rule_);
4687 rule_construct(struct rule *rule_)
4689 struct rule_dpif *rule = rule_dpif_cast(rule_);
4690 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4691 struct rule_dpif *victim;
4694 rule->packet_count = 0;
4695 rule->byte_count = 0;
4697 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
4698 if (victim && !list_is_empty(&victim->facets)) {
4699 struct facet *facet;
4701 rule->facets = victim->facets;
4702 list_moved(&rule->facets);
4703 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4704 /* XXX: We're only clearing our local counters here. It's possible
4705 * that quite a few packets are unaccounted for in the datapath
4706 * statistics. These will be accounted to the new rule instead of
4707 * cleared as required. This could be fixed by clearing out the
4708 * datapath statistics for this facet, but currently it doesn't
4710 facet_reset_counters(facet);
4714 /* Must avoid list_moved() in this case. */
4715 list_init(&rule->facets);
4718 table_id = rule->up.table_id;
4720 rule->tag = victim->tag;
4721 } else if (table_id == 0) {
4726 miniflow_expand(&rule->up.cr.match.flow, &flow);
4727 rule->tag = rule_calculate_tag(&flow, &rule->up.cr.match.mask,
4728 ofproto->tables[table_id].basis);
4731 complete_operation(rule);
4736 rule_destruct(struct rule *rule_)
4738 struct rule_dpif *rule = rule_dpif_cast(rule_);
4739 struct facet *facet, *next_facet;
4741 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4742 facet_revalidate(facet);
4745 complete_operation(rule);
4749 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
4751 struct rule_dpif *rule = rule_dpif_cast(rule_);
4752 struct facet *facet;
4754 /* Start from historical data for 'rule' itself that are no longer tracked
4755 * in facets. This counts, for example, facets that have expired. */
4756 *packets = rule->packet_count;
4757 *bytes = rule->byte_count;
4759 /* Add any statistics that are tracked by facets. This includes
4760 * statistical data recently updated by ofproto_update_stats() as well as
4761 * stats for packets that were executed "by hand" via dpif_execute(). */
4762 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4763 *packets += facet->packet_count;
4764 *bytes += facet->byte_count;
4769 rule_execute(struct rule *rule_, const struct flow *flow,
4770 struct ofpbuf *packet)
4772 struct rule_dpif *rule = rule_dpif_cast(rule_);
4773 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4775 struct dpif_flow_stats stats;
4777 struct action_xlate_ctx ctx;
4778 uint64_t odp_actions_stub[1024 / 8];
4779 struct ofpbuf odp_actions;
4781 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
4782 rule_credit_stats(rule, &stats);
4784 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4785 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci,
4786 rule, stats.tcp_flags, packet);
4787 ctx.resubmit_stats = &stats;
4788 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len, &odp_actions);
4790 execute_odp_actions(ofproto, flow, odp_actions.data,
4791 odp_actions.size, packet);
4793 ofpbuf_uninit(&odp_actions);
4799 rule_modify_actions(struct rule *rule_)
4801 struct rule_dpif *rule = rule_dpif_cast(rule_);
4803 complete_operation(rule);
4806 /* Sends 'packet' out 'ofport'.
4807 * May modify 'packet'.
4808 * Returns 0 if successful, otherwise a positive errno value. */
4810 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4812 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4813 struct ofpbuf key, odp_actions;
4814 struct odputil_keybuf keybuf;
4819 flow_extract(packet, 0, NULL, 0, &flow);
4820 odp_port = vsp_realdev_to_vlandev(ofproto, ofport->odp_port,
4822 if (odp_port != ofport->odp_port) {
4823 eth_pop_vlan(packet);
4824 flow.vlan_tci = htons(0);
4827 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4828 odp_flow_key_from_flow(&key, &flow,
4829 ofp_port_to_odp_port(ofproto, flow.in_port));
4831 ofpbuf_init(&odp_actions, 32);
4832 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
4834 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
4835 error = dpif_execute(ofproto->dpif,
4837 odp_actions.data, odp_actions.size,
4839 ofpbuf_uninit(&odp_actions);
4842 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
4843 ofproto->up.name, odp_port, strerror(error));
4845 ofproto_update_local_port_stats(ofport->up.ofproto, packet->size, 0);
4849 /* OpenFlow to datapath action translation. */
4851 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
4852 struct action_xlate_ctx *);
4853 static void xlate_normal(struct action_xlate_ctx *);
4855 /* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
4856 * The action will state 'slow' as the reason that the action is in the slow
4857 * path. (This is purely informational: it allows a human viewing "ovs-dpctl
4858 * dump-flows" output to see why a flow is in the slow path.)
4860 * The 'stub_size' bytes in 'stub' will be used to store the action.
4861 * 'stub_size' must be large enough for the action.
4863 * The action and its size will be stored in '*actionsp' and '*actions_lenp',
4866 compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
4867 enum slow_path_reason slow,
4868 uint64_t *stub, size_t stub_size,
4869 const struct nlattr **actionsp, size_t *actions_lenp)
4871 union user_action_cookie cookie;
4874 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
4875 cookie.slow_path.unused = 0;
4876 cookie.slow_path.reason = slow;
4878 ofpbuf_use_stack(&buf, stub, stub_size);
4879 if (slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)) {
4880 uint32_t pid = dpif_port_get_pid(ofproto->dpif, UINT16_MAX);
4881 odp_put_userspace_action(pid, &cookie, &buf);
4883 put_userspace_action(ofproto, &buf, flow, &cookie);
4885 *actionsp = buf.data;
4886 *actions_lenp = buf.size;
4890 put_userspace_action(const struct ofproto_dpif *ofproto,
4891 struct ofpbuf *odp_actions,
4892 const struct flow *flow,
4893 const union user_action_cookie *cookie)
4897 pid = dpif_port_get_pid(ofproto->dpif,
4898 ofp_port_to_odp_port(ofproto, flow->in_port));
4900 return odp_put_userspace_action(pid, cookie, odp_actions);
4904 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
4905 ovs_be16 vlan_tci, uint32_t odp_port,
4906 unsigned int n_outputs, union user_action_cookie *cookie)
4910 cookie->type = USER_ACTION_COOKIE_SFLOW;
4911 cookie->sflow.vlan_tci = vlan_tci;
4913 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
4914 * port information") for the interpretation of cookie->output. */
4915 switch (n_outputs) {
4917 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
4918 cookie->sflow.output = 0x40000000 | 256;
4922 ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
4924 cookie->sflow.output = ifindex;
4929 /* 0x80000000 means "multiple output ports. */
4930 cookie->sflow.output = 0x80000000 | n_outputs;
4935 /* Compose SAMPLE action for sFlow. */
4937 compose_sflow_action(const struct ofproto_dpif *ofproto,
4938 struct ofpbuf *odp_actions,
4939 const struct flow *flow,
4942 uint32_t probability;
4943 union user_action_cookie cookie;
4944 size_t sample_offset, actions_offset;
4947 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
4951 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
4953 /* Number of packets out of UINT_MAX to sample. */
4954 probability = dpif_sflow_get_probability(ofproto->sflow);
4955 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
4957 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
4958 compose_sflow_cookie(ofproto, htons(0), odp_port,
4959 odp_port == OVSP_NONE ? 0 : 1, &cookie);
4960 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
4962 nl_msg_end_nested(odp_actions, actions_offset);
4963 nl_msg_end_nested(odp_actions, sample_offset);
4964 return cookie_offset;
4967 /* SAMPLE action must be first action in any given list of actions.
4968 * At this point we do not have all information required to build it. So try to
4969 * build sample action as complete as possible. */
4971 add_sflow_action(struct action_xlate_ctx *ctx)
4973 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
4975 &ctx->flow, OVSP_NONE);
4976 ctx->sflow_odp_port = 0;
4977 ctx->sflow_n_outputs = 0;
4980 /* Fix SAMPLE action according to data collected while composing ODP actions.
4981 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
4982 * USERSPACE action's user-cookie which is required for sflow. */
4984 fix_sflow_action(struct action_xlate_ctx *ctx)
4986 const struct flow *base = &ctx->base_flow;
4987 union user_action_cookie *cookie;
4989 if (!ctx->user_cookie_offset) {
4993 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
4995 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
4997 compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
4998 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
5002 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
5005 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
5006 uint32_t odp_port = ofp_port_to_odp_port(ctx->ofproto, ofp_port);
5007 ovs_be16 flow_vlan_tci = ctx->flow.vlan_tci;
5008 uint8_t flow_nw_tos = ctx->flow.nw_tos;
5012 struct priority_to_dscp *pdscp;
5014 if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD) {
5015 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
5017 } else if (check_stp && !stp_forward_in_state(ofport->stp_state)) {
5018 xlate_report(ctx, "STP not in forwarding state, skipping output");
5022 pdscp = get_priority(ofport, ctx->flow.skb_priority);
5024 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
5025 ctx->flow.nw_tos |= pdscp->dscp;
5028 /* We may not have an ofport record for this port, but it doesn't hurt
5029 * to allow forwarding to it anyhow. Maybe such a port will appear
5030 * later and we're pre-populating the flow table. */
5033 out_port = vsp_realdev_to_vlandev(ctx->ofproto, odp_port,
5034 ctx->flow.vlan_tci);
5035 if (out_port != odp_port) {
5036 ctx->flow.vlan_tci = htons(0);
5038 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
5039 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
5041 ctx->sflow_odp_port = odp_port;
5042 ctx->sflow_n_outputs++;
5043 ctx->nf_output_iface = ofp_port;
5044 ctx->flow.vlan_tci = flow_vlan_tci;
5045 ctx->flow.nw_tos = flow_nw_tos;
5049 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
5051 compose_output_action__(ctx, ofp_port, true);
5055 xlate_table_action(struct action_xlate_ctx *ctx,
5056 uint16_t in_port, uint8_t table_id, bool may_packet_in)
5058 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
5059 struct ofproto_dpif *ofproto = ctx->ofproto;
5060 struct rule_dpif *rule;
5061 uint16_t old_in_port;
5062 uint8_t old_table_id;
5064 old_table_id = ctx->table_id;
5065 ctx->table_id = table_id;
5067 /* Look up a flow with 'in_port' as the input port. */
5068 old_in_port = ctx->flow.in_port;
5069 ctx->flow.in_port = in_port;
5070 rule = rule_dpif_lookup__(ofproto, &ctx->flow, table_id);
5073 if (table_id > 0 && table_id < N_TABLES) {
5074 struct table_dpif *table = &ofproto->tables[table_id];
5075 if (table->other_table) {
5076 ctx->tags |= (rule && rule->tag
5078 : rule_calculate_tag(&ctx->flow,
5079 &table->other_table->mask,
5084 /* Restore the original input port. Otherwise OFPP_NORMAL and
5085 * OFPP_IN_PORT will have surprising behavior. */
5086 ctx->flow.in_port = old_in_port;
5088 if (ctx->resubmit_hook) {
5089 ctx->resubmit_hook(ctx, rule);
5092 if (rule == NULL && may_packet_in) {
5094 * check if table configuration flags
5095 * OFPTC_TABLE_MISS_CONTROLLER, default.
5096 * OFPTC_TABLE_MISS_CONTINUE,
5097 * OFPTC_TABLE_MISS_DROP
5098 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
5100 rule = rule_dpif_miss_rule(ofproto, &ctx->flow);
5104 struct rule_dpif *old_rule = ctx->rule;
5106 if (ctx->resubmit_stats) {
5107 rule_credit_stats(rule, ctx->resubmit_stats);
5112 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
5113 ctx->rule = old_rule;
5117 ctx->table_id = old_table_id;
5119 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
5121 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
5122 MAX_RESUBMIT_RECURSION);
5123 ctx->max_resubmit_trigger = true;
5128 xlate_ofpact_resubmit(struct action_xlate_ctx *ctx,
5129 const struct ofpact_resubmit *resubmit)
5134 in_port = resubmit->in_port;
5135 if (in_port == OFPP_IN_PORT) {
5136 in_port = ctx->flow.in_port;
5139 table_id = resubmit->table_id;
5140 if (table_id == 255) {
5141 table_id = ctx->table_id;
5144 xlate_table_action(ctx, in_port, table_id, false);
5148 flood_packets(struct action_xlate_ctx *ctx, bool all)
5150 struct ofport_dpif *ofport;
5152 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
5153 uint16_t ofp_port = ofport->up.ofp_port;
5155 if (ofp_port == ctx->flow.in_port) {
5160 compose_output_action__(ctx, ofp_port, false);
5161 } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
5162 compose_output_action(ctx, ofp_port);
5166 ctx->nf_output_iface = NF_OUT_FLOOD;
5170 execute_controller_action(struct action_xlate_ctx *ctx, int len,
5171 enum ofp_packet_in_reason reason,
5172 uint16_t controller_id)
5174 struct ofputil_packet_in pin;
5175 struct ofpbuf *packet;
5177 ctx->slow |= SLOW_CONTROLLER;
5182 packet = ofpbuf_clone(ctx->packet);
5184 if (packet->l2 && packet->l3) {
5185 struct eth_header *eh;
5187 eth_pop_vlan(packet);
5190 /* If the Ethernet type is less than ETH_TYPE_MIN, it's likely an 802.2
5191 * LLC frame. Calculating the Ethernet type of these frames is more
5192 * trouble than seems appropriate for a simple assertion. */
5193 assert(ntohs(eh->eth_type) < ETH_TYPE_MIN
5194 || eh->eth_type == ctx->flow.dl_type);
5196 memcpy(eh->eth_src, ctx->flow.dl_src, sizeof eh->eth_src);
5197 memcpy(eh->eth_dst, ctx->flow.dl_dst, sizeof eh->eth_dst);
5199 if (ctx->flow.vlan_tci & htons(VLAN_CFI)) {
5200 eth_push_vlan(packet, ctx->flow.vlan_tci);
5204 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
5205 packet_set_ipv4(packet, ctx->flow.nw_src, ctx->flow.nw_dst,
5206 ctx->flow.nw_tos, ctx->flow.nw_ttl);
5210 if (ctx->flow.nw_proto == IPPROTO_TCP) {
5211 packet_set_tcp_port(packet, ctx->flow.tp_src,
5213 } else if (ctx->flow.nw_proto == IPPROTO_UDP) {
5214 packet_set_udp_port(packet, ctx->flow.tp_src,
5221 pin.packet = packet->data;
5222 pin.packet_len = packet->size;
5223 pin.reason = reason;
5224 pin.controller_id = controller_id;
5225 pin.table_id = ctx->table_id;
5226 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
5229 flow_get_metadata(&ctx->flow, &pin.fmd);
5231 connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
5232 ofpbuf_delete(packet);
5236 compose_dec_ttl(struct action_xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
5238 if (ctx->flow.dl_type != htons(ETH_TYPE_IP) &&
5239 ctx->flow.dl_type != htons(ETH_TYPE_IPV6)) {
5243 if (ctx->flow.nw_ttl > 1) {
5249 for (i = 0; i < ids->n_controllers; i++) {
5250 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
5254 /* Stop processing for current table. */
5260 xlate_output_action(struct action_xlate_ctx *ctx,
5261 uint16_t port, uint16_t max_len, bool may_packet_in)
5263 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
5265 ctx->nf_output_iface = NF_OUT_DROP;
5269 compose_output_action(ctx, ctx->flow.in_port);
5272 xlate_table_action(ctx, ctx->flow.in_port, 0, may_packet_in);
5278 flood_packets(ctx, false);
5281 flood_packets(ctx, true);
5283 case OFPP_CONTROLLER:
5284 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
5290 if (port != ctx->flow.in_port) {
5291 compose_output_action(ctx, port);
5293 xlate_report(ctx, "skipping output to input port");
5298 if (prev_nf_output_iface == NF_OUT_FLOOD) {
5299 ctx->nf_output_iface = NF_OUT_FLOOD;
5300 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
5301 ctx->nf_output_iface = prev_nf_output_iface;
5302 } else if (prev_nf_output_iface != NF_OUT_DROP &&
5303 ctx->nf_output_iface != NF_OUT_FLOOD) {
5304 ctx->nf_output_iface = NF_OUT_MULTI;
5309 xlate_output_reg_action(struct action_xlate_ctx *ctx,
5310 const struct ofpact_output_reg *or)
5312 uint64_t port = mf_get_subfield(&or->src, &ctx->flow);
5313 if (port <= UINT16_MAX) {
5314 xlate_output_action(ctx, port, or->max_len, false);
5319 xlate_enqueue_action(struct action_xlate_ctx *ctx,
5320 const struct ofpact_enqueue *enqueue)
5322 uint16_t ofp_port = enqueue->port;
5323 uint32_t queue_id = enqueue->queue;
5324 uint32_t flow_priority, priority;
5327 /* Translate queue to priority. */
5328 error = dpif_queue_to_priority(ctx->ofproto->dpif, queue_id, &priority);
5330 /* Fall back to ordinary output action. */
5331 xlate_output_action(ctx, enqueue->port, 0, false);
5335 /* Check output port. */
5336 if (ofp_port == OFPP_IN_PORT) {
5337 ofp_port = ctx->flow.in_port;
5338 } else if (ofp_port == ctx->flow.in_port) {
5342 /* Add datapath actions. */
5343 flow_priority = ctx->flow.skb_priority;
5344 ctx->flow.skb_priority = priority;
5345 compose_output_action(ctx, ofp_port);
5346 ctx->flow.skb_priority = flow_priority;
5348 /* Update NetFlow output port. */
5349 if (ctx->nf_output_iface == NF_OUT_DROP) {
5350 ctx->nf_output_iface = ofp_port;
5351 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
5352 ctx->nf_output_iface = NF_OUT_MULTI;
5357 xlate_set_queue_action(struct action_xlate_ctx *ctx, uint32_t queue_id)
5359 uint32_t skb_priority;
5361 if (!dpif_queue_to_priority(ctx->ofproto->dpif, queue_id, &skb_priority)) {
5362 ctx->flow.skb_priority = skb_priority;
5364 /* Couldn't translate queue to a priority. Nothing to do. A warning
5365 * has already been logged. */
5369 struct xlate_reg_state {
5375 xlate_autopath(struct action_xlate_ctx *ctx,
5376 const struct ofpact_autopath *ap)
5378 uint16_t ofp_port = ap->port;
5379 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
5381 if (!port || !port->bundle) {
5382 ofp_port = OFPP_NONE;
5383 } else if (port->bundle->bond) {
5384 /* Autopath does not support VLAN hashing. */
5385 struct ofport_dpif *slave = bond_choose_output_slave(
5386 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
5388 ofp_port = slave->up.ofp_port;
5391 nxm_reg_load(&ap->dst, ofp_port, &ctx->flow);
5395 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
5397 struct ofproto_dpif *ofproto = ofproto_;
5398 struct ofport_dpif *port;
5408 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
5411 port = get_ofp_port(ofproto, ofp_port);
5412 return port ? port->may_enable : false;
5417 xlate_bundle_action(struct action_xlate_ctx *ctx,
5418 const struct ofpact_bundle *bundle)
5422 port = bundle_execute(bundle, &ctx->flow, slave_enabled_cb, ctx->ofproto);
5423 if (bundle->dst.field) {
5424 nxm_reg_load(&bundle->dst, port, &ctx->flow);
5426 xlate_output_action(ctx, port, 0, false);
5431 xlate_learn_action(struct action_xlate_ctx *ctx,
5432 const struct ofpact_learn *learn)
5434 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
5435 struct ofputil_flow_mod fm;
5436 uint64_t ofpacts_stub[1024 / 8];
5437 struct ofpbuf ofpacts;
5440 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
5441 learn_execute(learn, &ctx->flow, &fm, &ofpacts);
5443 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
5444 if (error && !VLOG_DROP_WARN(&rl)) {
5445 VLOG_WARN("learning action failed to modify flow table (%s)",
5446 ofperr_get_name(error));
5449 ofpbuf_uninit(&ofpacts);
5452 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5453 * means "infinite". */
5455 reduce_timeout(uint16_t max, uint16_t *timeout)
5457 if (max && (!*timeout || *timeout > max)) {
5463 xlate_fin_timeout(struct action_xlate_ctx *ctx,
5464 const struct ofpact_fin_timeout *oft)
5466 if (ctx->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
5467 struct rule_dpif *rule = ctx->rule;
5469 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
5470 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
5475 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
5477 if (port->up.pp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
5478 ? OFPUTIL_PC_NO_RECV_STP
5479 : OFPUTIL_PC_NO_RECV)) {
5483 /* Only drop packets here if both forwarding and learning are
5484 * disabled. If just learning is enabled, we need to have
5485 * OFPP_NORMAL and the learning action have a look at the packet
5486 * before we can drop it. */
5487 if (!stp_forward_in_state(port->stp_state)
5488 && !stp_learn_in_state(port->stp_state)) {
5496 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
5497 struct action_xlate_ctx *ctx)
5499 const struct ofport_dpif *port;
5500 bool was_evictable = true;
5501 const struct ofpact *a;
5503 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
5504 if (port && !may_receive(port, ctx)) {
5505 /* Drop this flow. */
5510 /* Don't let the rule we're working on get evicted underneath us. */
5511 was_evictable = ctx->rule->up.evictable;
5512 ctx->rule->up.evictable = false;
5514 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5515 struct ofpact_controller *controller;
5516 const struct ofpact_metadata *metadata;
5524 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
5525 ofpact_get_OUTPUT(a)->max_len, true);
5528 case OFPACT_CONTROLLER:
5529 controller = ofpact_get_CONTROLLER(a);
5530 execute_controller_action(ctx, controller->max_len,
5532 controller->controller_id);
5535 case OFPACT_ENQUEUE:
5536 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
5539 case OFPACT_SET_VLAN_VID:
5540 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
5541 ctx->flow.vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
5545 case OFPACT_SET_VLAN_PCP:
5546 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
5547 ctx->flow.vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
5552 case OFPACT_STRIP_VLAN:
5553 ctx->flow.vlan_tci = htons(0);
5556 case OFPACT_PUSH_VLAN:
5557 /* TODO:XXX 802.1AD(QinQ) */
5558 ctx->flow.vlan_tci = htons(VLAN_CFI);
5561 case OFPACT_SET_ETH_SRC:
5562 memcpy(ctx->flow.dl_src, ofpact_get_SET_ETH_SRC(a)->mac,
5566 case OFPACT_SET_ETH_DST:
5567 memcpy(ctx->flow.dl_dst, ofpact_get_SET_ETH_DST(a)->mac,
5571 case OFPACT_SET_IPV4_SRC:
5572 ctx->flow.nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
5575 case OFPACT_SET_IPV4_DST:
5576 ctx->flow.nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
5579 case OFPACT_SET_IPV4_DSCP:
5580 /* OpenFlow 1.0 only supports IPv4. */
5581 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
5582 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
5583 ctx->flow.nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
5587 case OFPACT_SET_L4_SRC_PORT:
5588 ctx->flow.tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
5591 case OFPACT_SET_L4_DST_PORT:
5592 ctx->flow.tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
5595 case OFPACT_RESUBMIT:
5596 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
5599 case OFPACT_SET_TUNNEL:
5600 ctx->flow.tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
5603 case OFPACT_SET_QUEUE:
5604 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
5607 case OFPACT_POP_QUEUE:
5608 ctx->flow.skb_priority = ctx->orig_skb_priority;
5611 case OFPACT_REG_MOVE:
5612 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), &ctx->flow);
5615 case OFPACT_REG_LOAD:
5616 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), &ctx->flow);
5619 case OFPACT_DEC_TTL:
5620 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
5626 /* Nothing to do. */
5629 case OFPACT_MULTIPATH:
5630 multipath_execute(ofpact_get_MULTIPATH(a), &ctx->flow);
5633 case OFPACT_AUTOPATH:
5634 xlate_autopath(ctx, ofpact_get_AUTOPATH(a));
5638 ctx->ofproto->has_bundle_action = true;
5639 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
5642 case OFPACT_OUTPUT_REG:
5643 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
5647 ctx->has_learn = true;
5648 if (ctx->may_learn) {
5649 xlate_learn_action(ctx, ofpact_get_LEARN(a));
5657 case OFPACT_FIN_TIMEOUT:
5658 ctx->has_fin_timeout = true;
5659 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
5662 case OFPACT_CLEAR_ACTIONS:
5664 * Nothing to do because writa-actions is not supported for now.
5665 * When writa-actions is supported, clear-actions also must
5666 * be supported at the same time.
5670 case OFPACT_WRITE_METADATA:
5671 metadata = ofpact_get_WRITE_METADATA(a);
5672 ctx->flow.metadata &= ~metadata->mask;
5673 ctx->flow.metadata |= metadata->metadata & metadata->mask;
5676 case OFPACT_GOTO_TABLE: {
5677 /* TODO:XXX remove recursion */
5678 /* It is assumed that goto-table is last action */
5679 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
5680 assert(ctx->table_id < ogt->table_id);
5681 xlate_table_action(ctx, ctx->flow.in_port, ogt->table_id, true);
5688 /* We've let OFPP_NORMAL and the learning action look at the packet,
5689 * so drop it now if forwarding is disabled. */
5690 if (port && !stp_forward_in_state(port->stp_state)) {
5691 ofpbuf_clear(ctx->odp_actions);
5692 add_sflow_action(ctx);
5695 ctx->rule->up.evictable = was_evictable;
5700 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
5701 struct ofproto_dpif *ofproto, const struct flow *flow,
5702 ovs_be16 initial_tci, struct rule_dpif *rule,
5703 uint8_t tcp_flags, const struct ofpbuf *packet)
5705 ctx->ofproto = ofproto;
5707 ctx->base_flow = ctx->flow;
5708 memset(&ctx->base_flow.tunnel, 0, sizeof ctx->base_flow.tunnel);
5709 ctx->base_flow.vlan_tci = initial_tci;
5711 ctx->packet = packet;
5712 ctx->may_learn = packet != NULL;
5713 ctx->tcp_flags = tcp_flags;
5714 ctx->resubmit_hook = NULL;
5715 ctx->report_hook = NULL;
5716 ctx->resubmit_stats = NULL;
5719 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
5720 * into datapath actions in 'odp_actions', using 'ctx'. */
5722 xlate_actions(struct action_xlate_ctx *ctx,
5723 const struct ofpact *ofpacts, size_t ofpacts_len,
5724 struct ofpbuf *odp_actions)
5726 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
5727 * that in the future we always keep a copy of the original flow for
5728 * tracing purposes. */
5729 static bool hit_resubmit_limit;
5731 enum slow_path_reason special;
5733 COVERAGE_INC(ofproto_dpif_xlate);
5735 ofpbuf_clear(odp_actions);
5736 ofpbuf_reserve(odp_actions, NL_A_U32_SIZE);
5738 ctx->odp_actions = odp_actions;
5741 ctx->has_learn = false;
5742 ctx->has_normal = false;
5743 ctx->has_fin_timeout = false;
5744 ctx->nf_output_iface = NF_OUT_DROP;
5747 ctx->max_resubmit_trigger = false;
5748 ctx->orig_skb_priority = ctx->flow.skb_priority;
5752 if (ctx->ofproto->has_mirrors || hit_resubmit_limit) {
5753 /* Do this conditionally because the copy is expensive enough that it
5754 * shows up in profiles.
5756 * We keep orig_flow in 'ctx' only because I couldn't make GCC 4.4
5757 * believe that I wasn't using it without initializing it if I kept it
5758 * in a local variable. */
5759 ctx->orig_flow = ctx->flow;
5762 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
5763 switch (ctx->ofproto->up.frag_handling) {
5764 case OFPC_FRAG_NORMAL:
5765 /* We must pretend that transport ports are unavailable. */
5766 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
5767 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
5770 case OFPC_FRAG_DROP:
5773 case OFPC_FRAG_REASM:
5776 case OFPC_FRAG_NX_MATCH:
5777 /* Nothing to do. */
5780 case OFPC_INVALID_TTL_TO_CONTROLLER:
5785 special = process_special(ctx->ofproto, &ctx->flow, ctx->packet);
5787 ctx->slow |= special;
5789 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
5790 ovs_be16 initial_tci = ctx->base_flow.vlan_tci;
5792 add_sflow_action(ctx);
5793 do_xlate_actions(ofpacts, ofpacts_len, ctx);
5795 if (ctx->max_resubmit_trigger && !ctx->resubmit_hook) {
5796 if (!hit_resubmit_limit) {
5797 /* We didn't record the original flow. Make sure we do from
5799 hit_resubmit_limit = true;
5800 } else if (!VLOG_DROP_ERR(&trace_rl)) {
5801 struct ds ds = DS_EMPTY_INITIALIZER;
5803 ofproto_trace(ctx->ofproto, &ctx->orig_flow, ctx->packet,
5805 VLOG_ERR("Trace triggered by excessive resubmit "
5806 "recursion:\n%s", ds_cstr(&ds));
5811 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
5812 ctx->odp_actions->data,
5813 ctx->odp_actions->size)) {
5814 ctx->slow |= SLOW_IN_BAND;
5816 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
5818 compose_output_action(ctx, OFPP_LOCAL);
5821 if (ctx->ofproto->has_mirrors) {
5822 add_mirror_actions(ctx, &ctx->orig_flow);
5824 fix_sflow_action(ctx);
5828 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
5829 * into datapath actions, using 'ctx', and discards the datapath actions. */
5831 xlate_actions_for_side_effects(struct action_xlate_ctx *ctx,
5832 const struct ofpact *ofpacts,
5835 uint64_t odp_actions_stub[1024 / 8];
5836 struct ofpbuf odp_actions;
5838 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
5839 xlate_actions(ctx, ofpacts, ofpacts_len, &odp_actions);
5840 ofpbuf_uninit(&odp_actions);
5844 xlate_report(struct action_xlate_ctx *ctx, const char *s)
5846 if (ctx->report_hook) {
5847 ctx->report_hook(ctx, s);
5851 /* OFPP_NORMAL implementation. */
5853 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
5855 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
5856 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
5857 * the bundle on which the packet was received, returns the VLAN to which the
5860 * Both 'vid' and the return value are in the range 0...4095. */
5862 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
5864 switch (in_bundle->vlan_mode) {
5865 case PORT_VLAN_ACCESS:
5866 return in_bundle->vlan;
5869 case PORT_VLAN_TRUNK:
5872 case PORT_VLAN_NATIVE_UNTAGGED:
5873 case PORT_VLAN_NATIVE_TAGGED:
5874 return vid ? vid : in_bundle->vlan;
5881 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
5882 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
5885 * 'vid' should be the VID obtained from the 802.1Q header that was received as
5886 * part of a packet (specify 0 if there was no 802.1Q header), in the range
5889 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
5891 /* Allow any VID on the OFPP_NONE port. */
5892 if (in_bundle == &ofpp_none_bundle) {
5896 switch (in_bundle->vlan_mode) {
5897 case PORT_VLAN_ACCESS:
5900 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5901 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
5902 "packet received on port %s configured as VLAN "
5903 "%"PRIu16" access port",
5904 in_bundle->ofproto->up.name, vid,
5905 in_bundle->name, in_bundle->vlan);
5911 case PORT_VLAN_NATIVE_UNTAGGED:
5912 case PORT_VLAN_NATIVE_TAGGED:
5914 /* Port must always carry its native VLAN. */
5918 case PORT_VLAN_TRUNK:
5919 if (!ofbundle_includes_vlan(in_bundle, vid)) {
5921 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5922 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
5923 "received on port %s not configured for trunking "
5925 in_bundle->ofproto->up.name, vid,
5926 in_bundle->name, vid);
5938 /* Given 'vlan', the VLAN that a packet belongs to, and
5939 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
5940 * that should be included in the 802.1Q header. (If the return value is 0,
5941 * then the 802.1Q header should only be included in the packet if there is a
5944 * Both 'vlan' and the return value are in the range 0...4095. */
5946 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
5948 switch (out_bundle->vlan_mode) {
5949 case PORT_VLAN_ACCESS:
5952 case PORT_VLAN_TRUNK:
5953 case PORT_VLAN_NATIVE_TAGGED:
5956 case PORT_VLAN_NATIVE_UNTAGGED:
5957 return vlan == out_bundle->vlan ? 0 : vlan;
5965 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
5968 struct ofport_dpif *port;
5970 ovs_be16 tci, old_tci;
5972 vid = output_vlan_to_vid(out_bundle, vlan);
5973 if (!out_bundle->bond) {
5974 port = ofbundle_get_a_port(out_bundle);
5976 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
5979 /* No slaves enabled, so drop packet. */
5984 old_tci = ctx->flow.vlan_tci;
5986 if (tci || out_bundle->use_priority_tags) {
5987 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
5989 tci |= htons(VLAN_CFI);
5992 ctx->flow.vlan_tci = tci;
5994 compose_output_action(ctx, port->up.ofp_port);
5995 ctx->flow.vlan_tci = old_tci;
5999 mirror_mask_ffs(mirror_mask_t mask)
6001 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
6006 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
6008 return (bundle->vlan_mode != PORT_VLAN_ACCESS
6009 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
6013 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
6015 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
6018 /* Returns an arbitrary interface within 'bundle'. */
6019 static struct ofport_dpif *
6020 ofbundle_get_a_port(const struct ofbundle *bundle)
6022 return CONTAINER_OF(list_front(&bundle->ports),
6023 struct ofport_dpif, bundle_node);
6027 vlan_is_mirrored(const struct ofmirror *m, int vlan)
6029 return !m->vlans || bitmap_is_set(m->vlans, vlan);
6033 add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
6035 struct ofproto_dpif *ofproto = ctx->ofproto;
6036 mirror_mask_t mirrors;
6037 struct ofbundle *in_bundle;
6040 const struct nlattr *a;
6043 in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
6044 ctx->packet != NULL, NULL);
6048 mirrors = in_bundle->src_mirrors;
6050 /* Drop frames on bundles reserved for mirroring. */
6051 if (in_bundle->mirror_out) {
6052 if (ctx->packet != NULL) {
6053 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6054 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
6055 "%s, which is reserved exclusively for mirroring",
6056 ctx->ofproto->up.name, in_bundle->name);
6062 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
6063 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
6066 vlan = input_vid_to_vlan(in_bundle, vid);
6068 /* Look at the output ports to check for destination selections. */
6070 NL_ATTR_FOR_EACH (a, left, ctx->odp_actions->data,
6071 ctx->odp_actions->size) {
6072 enum ovs_action_attr type = nl_attr_type(a);
6073 struct ofport_dpif *ofport;
6075 if (type != OVS_ACTION_ATTR_OUTPUT) {
6079 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
6080 if (ofport && ofport->bundle) {
6081 mirrors |= ofport->bundle->dst_mirrors;
6089 /* Restore the original packet before adding the mirror actions. */
6090 ctx->flow = *orig_flow;
6095 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
6097 if (!vlan_is_mirrored(m, vlan)) {
6098 mirrors = zero_rightmost_1bit(mirrors);
6102 mirrors &= ~m->dup_mirrors;
6103 ctx->mirrors |= m->dup_mirrors;
6105 output_normal(ctx, m->out, vlan);
6106 } else if (vlan != m->out_vlan
6107 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
6108 struct ofbundle *bundle;
6110 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
6111 if (ofbundle_includes_vlan(bundle, m->out_vlan)
6112 && !bundle->mirror_out) {
6113 output_normal(ctx, bundle, m->out_vlan);
6121 update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
6122 uint64_t packets, uint64_t bytes)
6128 for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
6131 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
6134 /* In normal circumstances 'm' will not be NULL. However,
6135 * if mirrors are reconfigured, we can temporarily get out
6136 * of sync in facet_revalidate(). We could "correct" the
6137 * mirror list before reaching here, but doing that would
6138 * not properly account the traffic stats we've currently
6139 * accumulated for previous mirror configuration. */
6143 m->packet_count += packets;
6144 m->byte_count += bytes;
6148 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
6149 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
6150 * indicate this; newer upstream kernels use gratuitous ARP requests. */
6152 is_gratuitous_arp(const struct flow *flow)
6154 return (flow->dl_type == htons(ETH_TYPE_ARP)
6155 && eth_addr_is_broadcast(flow->dl_dst)
6156 && (flow->nw_proto == ARP_OP_REPLY
6157 || (flow->nw_proto == ARP_OP_REQUEST
6158 && flow->nw_src == flow->nw_dst)));
6162 update_learning_table(struct ofproto_dpif *ofproto,
6163 const struct flow *flow, int vlan,
6164 struct ofbundle *in_bundle)
6166 struct mac_entry *mac;
6168 /* Don't learn the OFPP_NONE port. */
6169 if (in_bundle == &ofpp_none_bundle) {
6173 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
6177 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
6178 if (is_gratuitous_arp(flow)) {
6179 /* We don't want to learn from gratuitous ARP packets that are
6180 * reflected back over bond slaves so we lock the learning table. */
6181 if (!in_bundle->bond) {
6182 mac_entry_set_grat_arp_lock(mac);
6183 } else if (mac_entry_is_grat_arp_locked(mac)) {
6188 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
6189 /* The log messages here could actually be useful in debugging,
6190 * so keep the rate limit relatively high. */
6191 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
6192 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
6193 "on port %s in VLAN %d",
6194 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
6195 in_bundle->name, vlan);
6197 mac->port.p = in_bundle;
6198 tag_set_add(&ofproto->revalidate_set,
6199 mac_learning_changed(ofproto->ml, mac));
6203 static struct ofbundle *
6204 lookup_input_bundle(const struct ofproto_dpif *ofproto, uint16_t in_port,
6205 bool warn, struct ofport_dpif **in_ofportp)
6207 struct ofport_dpif *ofport;
6209 /* Find the port and bundle for the received packet. */
6210 ofport = get_ofp_port(ofproto, in_port);
6212 *in_ofportp = ofport;
6214 if (ofport && ofport->bundle) {
6215 return ofport->bundle;
6218 /* Special-case OFPP_NONE, which a controller may use as the ingress
6219 * port for traffic that it is sourcing. */
6220 if (in_port == OFPP_NONE) {
6221 return &ofpp_none_bundle;
6224 /* Odd. A few possible reasons here:
6226 * - We deleted a port but there are still a few packets queued up
6229 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
6230 * we don't know about.
6232 * - The ofproto client didn't configure the port as part of a bundle.
6233 * This is particularly likely to happen if a packet was received on the
6234 * port after it was created, but before the client had a chance to
6235 * configure its bundle.
6238 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6240 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
6241 "port %"PRIu16, ofproto->up.name, in_port);
6246 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
6247 * dropped. Returns true if they may be forwarded, false if they should be
6250 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
6251 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
6253 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
6254 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
6255 * checked by input_vid_is_valid().
6257 * May also add tags to '*tags', although the current implementation only does
6258 * so in one special case.
6261 is_admissible(struct action_xlate_ctx *ctx, struct ofport_dpif *in_port,
6264 struct ofproto_dpif *ofproto = ctx->ofproto;
6265 struct flow *flow = &ctx->flow;
6266 struct ofbundle *in_bundle = in_port->bundle;
6268 /* Drop frames for reserved multicast addresses
6269 * only if forward_bpdu option is absent. */
6270 if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
6271 xlate_report(ctx, "packet has reserved destination MAC, dropping");
6275 if (in_bundle->bond) {
6276 struct mac_entry *mac;
6278 switch (bond_check_admissibility(in_bundle->bond, in_port,
6279 flow->dl_dst, &ctx->tags)) {
6284 xlate_report(ctx, "bonding refused admissibility, dropping");
6287 case BV_DROP_IF_MOVED:
6288 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
6289 if (mac && mac->port.p != in_bundle &&
6290 (!is_gratuitous_arp(flow)
6291 || mac_entry_is_grat_arp_locked(mac))) {
6292 xlate_report(ctx, "SLB bond thinks this packet looped back, "
6304 xlate_normal(struct action_xlate_ctx *ctx)
6306 struct ofport_dpif *in_port;
6307 struct ofbundle *in_bundle;
6308 struct mac_entry *mac;
6312 ctx->has_normal = true;
6314 in_bundle = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
6315 ctx->packet != NULL, &in_port);
6317 xlate_report(ctx, "no input bundle, dropping");
6321 /* Drop malformed frames. */
6322 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
6323 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
6324 if (ctx->packet != NULL) {
6325 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6326 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
6327 "VLAN tag received on port %s",
6328 ctx->ofproto->up.name, in_bundle->name);
6330 xlate_report(ctx, "partial VLAN tag, dropping");
6334 /* Drop frames on bundles reserved for mirroring. */
6335 if (in_bundle->mirror_out) {
6336 if (ctx->packet != NULL) {
6337 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6338 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
6339 "%s, which is reserved exclusively for mirroring",
6340 ctx->ofproto->up.name, in_bundle->name);
6342 xlate_report(ctx, "input port is mirror output port, dropping");
6347 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
6348 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
6349 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
6352 vlan = input_vid_to_vlan(in_bundle, vid);
6354 /* Check other admissibility requirements. */
6355 if (in_port && !is_admissible(ctx, in_port, vlan)) {
6359 /* Learn source MAC. */
6360 if (ctx->may_learn) {
6361 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
6364 /* Determine output bundle. */
6365 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
6368 if (mac->port.p != in_bundle) {
6369 xlate_report(ctx, "forwarding to learned port");
6370 output_normal(ctx, mac->port.p, vlan);
6372 xlate_report(ctx, "learned port is input port, dropping");
6375 struct ofbundle *bundle;
6377 xlate_report(ctx, "no learned MAC for destination, flooding");
6378 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
6379 if (bundle != in_bundle
6380 && ofbundle_includes_vlan(bundle, vlan)
6381 && bundle->floodable
6382 && !bundle->mirror_out) {
6383 output_normal(ctx, bundle, vlan);
6386 ctx->nf_output_iface = NF_OUT_FLOOD;
6390 /* Optimized flow revalidation.
6392 * It's a difficult problem, in general, to tell which facets need to have
6393 * their actions recalculated whenever the OpenFlow flow table changes. We
6394 * don't try to solve that general problem: for most kinds of OpenFlow flow
6395 * table changes, we recalculate the actions for every facet. This is
6396 * relatively expensive, but it's good enough if the OpenFlow flow table
6397 * doesn't change very often.
6399 * However, we can expect one particular kind of OpenFlow flow table change to
6400 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
6401 * of CPU on revalidating every facet whenever MAC learning modifies the flow
6402 * table, we add a special case that applies to flow tables in which every rule
6403 * has the same form (that is, the same wildcards), except that the table is
6404 * also allowed to have a single "catch-all" flow that matches all packets. We
6405 * optimize this case by tagging all of the facets that resubmit into the table
6406 * and invalidating the same tag whenever a flow changes in that table. The
6407 * end result is that we revalidate just the facets that need it (and sometimes
6408 * a few more, but not all of the facets or even all of the facets that
6409 * resubmit to the table modified by MAC learning). */
6411 /* Calculates the tag to use for 'flow' and mask 'mask' when it is inserted
6412 * into an OpenFlow table with the given 'basis'. */
6414 rule_calculate_tag(const struct flow *flow, const struct minimask *mask,
6417 if (minimask_is_catchall(mask)) {
6420 uint32_t hash = flow_hash_in_minimask(flow, mask, secret);
6421 return tag_create_deterministic(hash);
6425 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
6426 * taggability of that table.
6428 * This function must be called after *each* change to a flow table. If you
6429 * skip calling it on some changes then the pointer comparisons at the end can
6430 * be invalid if you get unlucky. For example, if a flow removal causes a
6431 * cls_table to be destroyed and then a flow insertion causes a cls_table with
6432 * different wildcards to be created with the same address, then this function
6433 * will incorrectly skip revalidation. */
6435 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
6437 struct table_dpif *table = &ofproto->tables[table_id];
6438 const struct oftable *oftable = &ofproto->up.tables[table_id];
6439 struct cls_table *catchall, *other;
6440 struct cls_table *t;
6442 catchall = other = NULL;
6444 switch (hmap_count(&oftable->cls.tables)) {
6446 /* We could tag this OpenFlow table but it would make the logic a
6447 * little harder and it's a corner case that doesn't seem worth it
6453 HMAP_FOR_EACH (t, hmap_node, &oftable->cls.tables) {
6454 if (cls_table_is_catchall(t)) {
6456 } else if (!other) {
6459 /* Indicate that we can't tag this by setting both tables to
6460 * NULL. (We know that 'catchall' is already NULL.) */
6467 /* Can't tag this table. */
6471 if (table->catchall_table != catchall || table->other_table != other) {
6472 table->catchall_table = catchall;
6473 table->other_table = other;
6474 ofproto->need_revalidate = REV_FLOW_TABLE;
6478 /* Given 'rule' that has changed in some way (either it is a rule being
6479 * inserted, a rule being deleted, or a rule whose actions are being
6480 * modified), marks facets for revalidation to ensure that packets will be
6481 * forwarded correctly according to the new state of the flow table.
6483 * This function must be called after *each* change to a flow table. See
6484 * the comment on table_update_taggable() for more information. */
6486 rule_invalidate(const struct rule_dpif *rule)
6488 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
6490 table_update_taggable(ofproto, rule->up.table_id);
6492 if (!ofproto->need_revalidate) {
6493 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
6495 if (table->other_table && rule->tag) {
6496 tag_set_add(&ofproto->revalidate_set, rule->tag);
6498 ofproto->need_revalidate = REV_FLOW_TABLE;
6504 set_frag_handling(struct ofproto *ofproto_,
6505 enum ofp_config_flags frag_handling)
6507 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6509 if (frag_handling != OFPC_FRAG_REASM) {
6510 ofproto->need_revalidate = REV_RECONFIGURE;
6518 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
6519 const struct flow *flow,
6520 const struct ofpact *ofpacts, size_t ofpacts_len)
6522 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6523 struct odputil_keybuf keybuf;
6524 struct dpif_flow_stats stats;
6528 struct action_xlate_ctx ctx;
6529 uint64_t odp_actions_stub[1024 / 8];
6530 struct ofpbuf odp_actions;
6532 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
6533 odp_flow_key_from_flow(&key, flow,
6534 ofp_port_to_odp_port(ofproto, flow->in_port));
6536 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
6538 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, NULL,
6539 packet_get_tcp_flags(packet, flow), packet);
6540 ctx.resubmit_stats = &stats;
6542 ofpbuf_use_stub(&odp_actions,
6543 odp_actions_stub, sizeof odp_actions_stub);
6544 xlate_actions(&ctx, ofpacts, ofpacts_len, &odp_actions);
6545 dpif_execute(ofproto->dpif, key.data, key.size,
6546 odp_actions.data, odp_actions.size, packet);
6547 ofpbuf_uninit(&odp_actions);
6555 set_netflow(struct ofproto *ofproto_,
6556 const struct netflow_options *netflow_options)
6558 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6560 if (netflow_options) {
6561 if (!ofproto->netflow) {
6562 ofproto->netflow = netflow_create();
6564 return netflow_set_options(ofproto->netflow, netflow_options);
6566 netflow_destroy(ofproto->netflow);
6567 ofproto->netflow = NULL;
6573 get_netflow_ids(const struct ofproto *ofproto_,
6574 uint8_t *engine_type, uint8_t *engine_id)
6576 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6578 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
6582 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
6584 if (!facet_is_controller_flow(facet) &&
6585 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
6586 struct subfacet *subfacet;
6587 struct ofexpired expired;
6589 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
6590 if (subfacet->path == SF_FAST_PATH) {
6591 struct dpif_flow_stats stats;
6593 subfacet_reinstall(subfacet, &stats);
6594 subfacet_update_stats(subfacet, &stats);
6598 expired.flow = facet->flow;
6599 expired.packet_count = facet->packet_count;
6600 expired.byte_count = facet->byte_count;
6601 expired.used = facet->used;
6602 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
6607 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
6609 struct facet *facet;
6611 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
6612 send_active_timeout(ofproto, facet);
6616 static struct ofproto_dpif *
6617 ofproto_dpif_lookup(const char *name)
6619 struct ofproto_dpif *ofproto;
6621 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
6622 hash_string(name, 0), &all_ofproto_dpifs) {
6623 if (!strcmp(ofproto->up.name, name)) {
6631 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
6632 const char *argv[], void *aux OVS_UNUSED)
6634 struct ofproto_dpif *ofproto;
6637 ofproto = ofproto_dpif_lookup(argv[1]);
6639 unixctl_command_reply_error(conn, "no such bridge");
6642 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
6644 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6645 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
6649 unixctl_command_reply(conn, "table successfully flushed");
6653 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
6654 const char *argv[], void *aux OVS_UNUSED)
6656 struct ds ds = DS_EMPTY_INITIALIZER;
6657 const struct ofproto_dpif *ofproto;
6658 const struct mac_entry *e;
6660 ofproto = ofproto_dpif_lookup(argv[1]);
6662 unixctl_command_reply_error(conn, "no such bridge");
6666 ds_put_cstr(&ds, " port VLAN MAC Age\n");
6667 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
6668 struct ofbundle *bundle = e->port.p;
6669 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
6670 ofbundle_get_a_port(bundle)->odp_port,
6671 e->vlan, ETH_ADDR_ARGS(e->mac),
6672 mac_entry_age(ofproto->ml, e));
6674 unixctl_command_reply(conn, ds_cstr(&ds));
6679 struct action_xlate_ctx ctx;
6685 trace_format_rule(struct ds *result, uint8_t table_id, int level,
6686 const struct rule_dpif *rule)
6688 ds_put_char_multiple(result, '\t', level);
6690 ds_put_cstr(result, "No match\n");
6694 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
6695 table_id, ntohll(rule->up.flow_cookie));
6696 cls_rule_format(&rule->up.cr, result);
6697 ds_put_char(result, '\n');
6699 ds_put_char_multiple(result, '\t', level);
6700 ds_put_cstr(result, "OpenFlow ");
6701 ofpacts_format(rule->up.ofpacts, rule->up.ofpacts_len, result);
6702 ds_put_char(result, '\n');
6706 trace_format_flow(struct ds *result, int level, const char *title,
6707 struct trace_ctx *trace)
6709 ds_put_char_multiple(result, '\t', level);
6710 ds_put_format(result, "%s: ", title);
6711 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
6712 ds_put_cstr(result, "unchanged");
6714 flow_format(result, &trace->ctx.flow);
6715 trace->flow = trace->ctx.flow;
6717 ds_put_char(result, '\n');
6721 trace_format_regs(struct ds *result, int level, const char *title,
6722 struct trace_ctx *trace)
6726 ds_put_char_multiple(result, '\t', level);
6727 ds_put_format(result, "%s:", title);
6728 for (i = 0; i < FLOW_N_REGS; i++) {
6729 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
6731 ds_put_char(result, '\n');
6735 trace_format_odp(struct ds *result, int level, const char *title,
6736 struct trace_ctx *trace)
6738 struct ofpbuf *odp_actions = trace->ctx.odp_actions;
6740 ds_put_char_multiple(result, '\t', level);
6741 ds_put_format(result, "%s: ", title);
6742 format_odp_actions(result, odp_actions->data, odp_actions->size);
6743 ds_put_char(result, '\n');
6747 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
6749 struct trace_ctx *trace = CONTAINER_OF(ctx, struct trace_ctx, ctx);
6750 struct ds *result = trace->result;
6752 ds_put_char(result, '\n');
6753 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
6754 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
6755 trace_format_odp(result, ctx->recurse + 1, "Resubmitted odp", trace);
6756 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
6760 trace_report(struct action_xlate_ctx *ctx, const char *s)
6762 struct trace_ctx *trace = CONTAINER_OF(ctx, struct trace_ctx, ctx);
6763 struct ds *result = trace->result;
6765 ds_put_char_multiple(result, '\t', ctx->recurse);
6766 ds_put_cstr(result, s);
6767 ds_put_char(result, '\n');
6771 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
6772 void *aux OVS_UNUSED)
6774 const char *dpname = argv[1];
6775 struct ofproto_dpif *ofproto;
6776 struct ofpbuf odp_key;
6777 struct ofpbuf *packet;
6778 ovs_be16 initial_tci;
6784 ofpbuf_init(&odp_key, 0);
6787 ofproto = ofproto_dpif_lookup(dpname);
6789 unixctl_command_reply_error(conn, "Unknown ofproto (use ofproto/list "
6793 if (argc == 3 || (argc == 4 && !strcmp(argv[3], "-generate"))) {
6794 /* ofproto/trace dpname flow [-generate] */
6795 const char *flow_s = argv[2];
6796 const char *generate_s = argv[3];
6798 /* Allow 'flow_s' to be either a datapath flow or an OpenFlow-like
6799 * flow. We guess which type it is based on whether 'flow_s' contains
6800 * an '(', since a datapath flow always contains '(') but an
6801 * OpenFlow-like flow should not (in fact it's allowed but I believe
6802 * that's not documented anywhere).
6804 * An alternative would be to try to parse 'flow_s' both ways, but then
6805 * it would be tricky giving a sensible error message. After all, do
6806 * you just say "syntax error" or do you present both error messages?
6807 * Both choices seem lousy. */
6808 if (strchr(flow_s, '(')) {
6811 /* Convert string to datapath key. */
6812 ofpbuf_init(&odp_key, 0);
6813 error = odp_flow_key_from_string(flow_s, NULL, &odp_key);
6815 unixctl_command_reply_error(conn, "Bad flow syntax");
6819 /* Convert odp_key to flow. */
6820 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
6821 odp_key.size, &flow,
6822 &initial_tci, NULL);
6823 if (error == ODP_FIT_ERROR) {
6824 unixctl_command_reply_error(conn, "Invalid flow");
6830 error_s = parse_ofp_exact_flow(&flow, argv[2]);
6832 unixctl_command_reply_error(conn, error_s);
6837 initial_tci = flow.vlan_tci;
6838 vsp_adjust_flow(ofproto, &flow);
6841 /* Generate a packet, if requested. */
6843 packet = ofpbuf_new(0);
6844 flow_compose(packet, &flow);
6846 } else if (argc == 6) {
6847 /* ofproto/trace dpname priority tun_id in_port packet */
6848 const char *priority_s = argv[2];
6849 const char *tun_id_s = argv[3];
6850 const char *in_port_s = argv[4];
6851 const char *packet_s = argv[5];
6852 uint32_t in_port = atoi(in_port_s);
6853 ovs_be64 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
6854 uint32_t priority = atoi(priority_s);
6857 msg = eth_from_hex(packet_s, &packet);
6859 unixctl_command_reply_error(conn, msg);
6863 ds_put_cstr(&result, "Packet: ");
6864 s = ofp_packet_to_string(packet->data, packet->size);
6865 ds_put_cstr(&result, s);
6868 flow_extract(packet, priority, NULL, in_port, &flow);
6869 flow.tunnel.tun_id = tun_id;
6870 initial_tci = flow.vlan_tci;
6872 unixctl_command_reply_error(conn, "Bad command syntax");
6876 ofproto_trace(ofproto, &flow, packet, initial_tci, &result);
6877 unixctl_command_reply(conn, ds_cstr(&result));
6880 ds_destroy(&result);
6881 ofpbuf_delete(packet);
6882 ofpbuf_uninit(&odp_key);
6886 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
6887 const struct ofpbuf *packet, ovs_be16 initial_tci,
6890 struct rule_dpif *rule;
6892 ds_put_cstr(ds, "Flow: ");
6893 flow_format(ds, flow);
6894 ds_put_char(ds, '\n');
6896 rule = rule_dpif_lookup(ofproto, flow);
6898 trace_format_rule(ds, 0, 0, rule);
6899 if (rule == ofproto->miss_rule) {
6900 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
6901 } else if (rule == ofproto->no_packet_in_rule) {
6902 ds_put_cstr(ds, "\nNo match, packets dropped because "
6903 "OFPPC_NO_PACKET_IN is set on in_port.\n");
6907 uint64_t odp_actions_stub[1024 / 8];
6908 struct ofpbuf odp_actions;
6910 struct trace_ctx trace;
6913 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
6916 ofpbuf_use_stub(&odp_actions,
6917 odp_actions_stub, sizeof odp_actions_stub);
6918 action_xlate_ctx_init(&trace.ctx, ofproto, flow, initial_tci,
6919 rule, tcp_flags, packet);
6920 trace.ctx.resubmit_hook = trace_resubmit;
6921 trace.ctx.report_hook = trace_report;
6922 xlate_actions(&trace.ctx, rule->up.ofpacts, rule->up.ofpacts_len,
6925 ds_put_char(ds, '\n');
6926 trace_format_flow(ds, 0, "Final flow", &trace);
6927 ds_put_cstr(ds, "Datapath actions: ");
6928 format_odp_actions(ds, odp_actions.data, odp_actions.size);
6929 ofpbuf_uninit(&odp_actions);
6931 if (trace.ctx.slow) {
6932 enum slow_path_reason slow;
6934 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
6935 "slow path because it:");
6936 for (slow = trace.ctx.slow; slow; ) {
6937 enum slow_path_reason bit = rightmost_1bit(slow);
6941 ds_put_cstr(ds, "\n\t- Consists of CFM packets.");
6944 ds_put_cstr(ds, "\n\t- Consists of LACP packets.");
6947 ds_put_cstr(ds, "\n\t- Consists of STP packets.");
6950 ds_put_cstr(ds, "\n\t- Needs in-band special case "
6953 ds_put_cstr(ds, "\n\t (The datapath actions are "
6954 "incomplete--for complete actions, "
6955 "please supply a packet.)");
6958 case SLOW_CONTROLLER:
6959 ds_put_cstr(ds, "\n\t- Sends \"packet-in\" messages "
6960 "to the OpenFlow controller.");
6963 ds_put_cstr(ds, "\n\t- Needs more specific matching "
6964 "than the datapath supports.");
6971 if (slow & ~SLOW_MATCH) {
6972 ds_put_cstr(ds, "\nThe datapath actions above do not reflect "
6973 "the special slow-path processing.");
6980 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6981 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6984 unixctl_command_reply(conn, NULL);
6988 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6989 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6992 unixctl_command_reply(conn, NULL);
6995 /* Runs a self-check of flow translations in 'ofproto'. Appends a message to
6996 * 'reply' describing the results. */
6998 ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
7000 struct facet *facet;
7004 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
7005 if (!facet_check_consistency(facet)) {
7010 ofproto->need_revalidate = REV_INCONSISTENCY;
7014 ds_put_format(reply, "%s: self-check failed (%d errors)\n",
7015 ofproto->up.name, errors);
7017 ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
7022 ofproto_dpif_self_check(struct unixctl_conn *conn,
7023 int argc, const char *argv[], void *aux OVS_UNUSED)
7025 struct ds reply = DS_EMPTY_INITIALIZER;
7026 struct ofproto_dpif *ofproto;
7029 ofproto = ofproto_dpif_lookup(argv[1]);
7031 unixctl_command_reply_error(conn, "Unknown ofproto (use "
7032 "ofproto/list for help)");
7035 ofproto_dpif_self_check__(ofproto, &reply);
7037 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
7038 ofproto_dpif_self_check__(ofproto, &reply);
7042 unixctl_command_reply(conn, ds_cstr(&reply));
7047 ofproto_dpif_unixctl_init(void)
7049 static bool registered;
7055 unixctl_command_register(
7057 "bridge {tun_id in_port packet | odp_flow [-generate]}",
7058 2, 5, ofproto_unixctl_trace, NULL);
7059 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
7060 ofproto_unixctl_fdb_flush, NULL);
7061 unixctl_command_register("fdb/show", "bridge", 1, 1,
7062 ofproto_unixctl_fdb_show, NULL);
7063 unixctl_command_register("ofproto/clog", "", 0, 0,
7064 ofproto_dpif_clog, NULL);
7065 unixctl_command_register("ofproto/unclog", "", 0, 0,
7066 ofproto_dpif_unclog, NULL);
7067 unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
7068 ofproto_dpif_self_check, NULL);
7071 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
7073 * This is deprecated. It is only for compatibility with broken device drivers
7074 * in old versions of Linux that do not properly support VLANs when VLAN
7075 * devices are not used. When broken device drivers are no longer in
7076 * widespread use, we will delete these interfaces. */
7079 set_realdev(struct ofport *ofport_, uint16_t realdev_ofp_port, int vid)
7081 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
7082 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
7084 if (realdev_ofp_port == ofport->realdev_ofp_port
7085 && vid == ofport->vlandev_vid) {
7089 ofproto->need_revalidate = REV_RECONFIGURE;
7091 if (ofport->realdev_ofp_port) {
7094 if (realdev_ofp_port && ofport->bundle) {
7095 /* vlandevs are enslaved to their realdevs, so they are not allowed to
7096 * themselves be part of a bundle. */
7097 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
7100 ofport->realdev_ofp_port = realdev_ofp_port;
7101 ofport->vlandev_vid = vid;
7103 if (realdev_ofp_port) {
7104 vsp_add(ofport, realdev_ofp_port, vid);
7111 hash_realdev_vid(uint16_t realdev_ofp_port, int vid)
7113 return hash_2words(realdev_ofp_port, vid);
7116 /* Returns the ODP port number of the Linux VLAN device that corresponds to
7117 * 'vlan_tci' on the network device with port number 'realdev_odp_port' in
7118 * 'ofproto'. For example, given 'realdev_odp_port' of eth0 and 'vlan_tci' 9,
7119 * it would return the port number of eth0.9.
7121 * Unless VLAN splinters are enabled for port 'realdev_odp_port', this
7122 * function just returns its 'realdev_odp_port' argument. */
7124 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
7125 uint32_t realdev_odp_port, ovs_be16 vlan_tci)
7127 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
7128 uint16_t realdev_ofp_port;
7129 int vid = vlan_tci_to_vid(vlan_tci);
7130 const struct vlan_splinter *vsp;
7132 realdev_ofp_port = odp_port_to_ofp_port(ofproto, realdev_odp_port);
7133 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
7134 hash_realdev_vid(realdev_ofp_port, vid),
7135 &ofproto->realdev_vid_map) {
7136 if (vsp->realdev_ofp_port == realdev_ofp_port
7137 && vsp->vid == vid) {
7138 return ofp_port_to_odp_port(ofproto, vsp->vlandev_ofp_port);
7142 return realdev_odp_port;
7145 static struct vlan_splinter *
7146 vlandev_find(const struct ofproto_dpif *ofproto, uint16_t vlandev_ofp_port)
7148 struct vlan_splinter *vsp;
7150 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node, hash_int(vlandev_ofp_port, 0),
7151 &ofproto->vlandev_map) {
7152 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
7160 /* Returns the OpenFlow port number of the "real" device underlying the Linux
7161 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
7162 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
7163 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
7164 * eth0 and store 9 in '*vid'.
7166 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
7167 * VLAN device. Unless VLAN splinters are enabled, this is what this function
7170 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
7171 uint16_t vlandev_ofp_port, int *vid)
7173 if (!hmap_is_empty(&ofproto->vlandev_map)) {
7174 const struct vlan_splinter *vsp;
7176 vsp = vlandev_find(ofproto, vlandev_ofp_port);
7181 return vsp->realdev_ofp_port;
7187 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
7188 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
7189 * 'flow->in_port' to the "real" device backing the VLAN device, sets
7190 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
7191 * always the case unless VLAN splinters are enabled), returns false without
7192 * making any changes. */
7194 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
7199 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
7204 /* Cause the flow to be processed as if it came in on the real device with
7205 * the VLAN device's VLAN ID. */
7206 flow->in_port = realdev;
7207 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
7212 vsp_remove(struct ofport_dpif *port)
7214 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
7215 struct vlan_splinter *vsp;
7217 vsp = vlandev_find(ofproto, port->up.ofp_port);
7219 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
7220 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
7223 port->realdev_ofp_port = 0;
7225 VLOG_ERR("missing vlan device record");
7230 vsp_add(struct ofport_dpif *port, uint16_t realdev_ofp_port, int vid)
7232 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
7234 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
7235 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
7236 == realdev_ofp_port)) {
7237 struct vlan_splinter *vsp;
7239 vsp = xmalloc(sizeof *vsp);
7240 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
7241 hash_int(port->up.ofp_port, 0));
7242 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
7243 hash_realdev_vid(realdev_ofp_port, vid));
7244 vsp->realdev_ofp_port = realdev_ofp_port;
7245 vsp->vlandev_ofp_port = port->up.ofp_port;
7248 port->realdev_ofp_port = realdev_ofp_port;
7250 VLOG_ERR("duplicate vlan device record");
7255 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, uint16_t ofp_port)
7257 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
7258 return ofport ? ofport->odp_port : OVSP_NONE;
7262 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, uint32_t odp_port)
7264 struct ofport_dpif *port;
7266 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node,
7267 hash_int(odp_port, 0),
7268 &ofproto->odp_to_ofport_map) {
7269 if (port->odp_port == odp_port) {
7270 return port->up.ofp_port;
7277 const struct ofproto_class ofproto_dpif_class = {
7308 port_is_lacp_current,
7309 NULL, /* rule_choose_table */
7316 rule_modify_actions,
7325 get_cfm_remote_mpids,
7330 get_stp_port_status,
7337 is_mirror_output_bundle,
7338 forward_bpdu_changed,