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-parse.h"
46 #include "ofp-print.h"
47 #include "ofproto-dpif-governor.h"
48 #include "ofproto-dpif-sflow.h"
49 #include "poll-loop.h"
52 #include "unaligned.h"
54 #include "vlan-bitmap.h"
57 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
59 COVERAGE_DEFINE(ofproto_dpif_expired);
60 COVERAGE_DEFINE(ofproto_dpif_xlate);
61 COVERAGE_DEFINE(facet_changed_rule);
62 COVERAGE_DEFINE(facet_revalidate);
63 COVERAGE_DEFINE(facet_unexpected);
64 COVERAGE_DEFINE(facet_suppress);
66 /* Maximum depth of flow table recursion (due to resubmit actions) in a
67 * flow translation. */
68 #define MAX_RESUBMIT_RECURSION 32
70 /* Number of implemented OpenFlow tables. */
71 enum { N_TABLES = 255 };
72 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
73 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
83 * - Do include packets and bytes from facets that have been deleted or
84 * whose own statistics have been folded into the rule.
86 * - Do include packets and bytes sent "by hand" that were accounted to
87 * the rule without any facet being involved (this is a rare corner
88 * case in rule_execute()).
90 * - Do not include packet or bytes that can be obtained from any facet's
91 * packet_count or byte_count member or that can be obtained from the
92 * datapath by, e.g., dpif_flow_get() for any subfacet.
94 uint64_t packet_count; /* Number of packets received. */
95 uint64_t byte_count; /* Number of bytes received. */
97 tag_type tag; /* Caches rule_calculate_tag() result. */
99 struct list facets; /* List of "struct facet"s. */
102 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
104 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
107 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
108 const struct flow *);
109 static struct rule_dpif *rule_dpif_lookup__(struct ofproto_dpif *,
113 static void rule_credit_stats(struct rule_dpif *,
114 const struct dpif_flow_stats *);
115 static void flow_push_stats(struct rule_dpif *, const struct flow *,
116 const struct dpif_flow_stats *);
117 static tag_type rule_calculate_tag(const struct flow *,
118 const struct flow_wildcards *,
120 static void rule_invalidate(const struct rule_dpif *);
122 #define MAX_MIRRORS 32
123 typedef uint32_t mirror_mask_t;
124 #define MIRROR_MASK_C(X) UINT32_C(X)
125 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
127 struct ofproto_dpif *ofproto; /* Owning ofproto. */
128 size_t idx; /* In ofproto's "mirrors" array. */
129 void *aux; /* Key supplied by ofproto's client. */
130 char *name; /* Identifier for log messages. */
132 /* Selection criteria. */
133 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
134 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
135 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
137 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
138 struct ofbundle *out; /* Output port or NULL. */
139 int out_vlan; /* Output VLAN or -1. */
140 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
143 int64_t packet_count; /* Number of packets sent. */
144 int64_t byte_count; /* Number of bytes sent. */
147 static void mirror_destroy(struct ofmirror *);
148 static void update_mirror_stats(struct ofproto_dpif *ofproto,
149 mirror_mask_t mirrors,
150 uint64_t packets, uint64_t bytes);
153 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
154 struct ofproto_dpif *ofproto; /* Owning ofproto. */
155 void *aux; /* Key supplied by ofproto's client. */
156 char *name; /* Identifier for log messages. */
159 struct list ports; /* Contains "struct ofport"s. */
160 enum port_vlan_mode vlan_mode; /* VLAN mode */
161 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
162 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
163 * NULL if all VLANs are trunked. */
164 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
165 struct bond *bond; /* Nonnull iff more than one port. */
166 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
169 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
171 /* Port mirroring info. */
172 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
173 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
174 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
177 static void bundle_remove(struct ofport *);
178 static void bundle_update(struct ofbundle *);
179 static void bundle_destroy(struct ofbundle *);
180 static void bundle_del_port(struct ofport_dpif *);
181 static void bundle_run(struct ofbundle *);
182 static void bundle_wait(struct ofbundle *);
183 static struct ofbundle *lookup_input_bundle(const struct ofproto_dpif *,
184 uint16_t in_port, bool warn,
185 struct ofport_dpif **in_ofportp);
187 /* A controller may use OFPP_NONE as the ingress port to indicate that
188 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
189 * when an input bundle is needed for validation (e.g., mirroring or
190 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
191 * any 'port' structs, so care must be taken when dealing with it. */
192 static struct ofbundle ofpp_none_bundle = {
194 .vlan_mode = PORT_VLAN_TRUNK
197 static void stp_run(struct ofproto_dpif *ofproto);
198 static void stp_wait(struct ofproto_dpif *ofproto);
199 static int set_stp_port(struct ofport *,
200 const struct ofproto_port_stp_settings *);
202 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
204 struct action_xlate_ctx {
205 /* action_xlate_ctx_init() initializes these members. */
208 struct ofproto_dpif *ofproto;
210 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
211 * this flow when actions change header fields. */
214 /* The packet corresponding to 'flow', or a null pointer if we are
215 * revalidating without a packet to refer to. */
216 const struct ofpbuf *packet;
218 /* Should OFPP_NORMAL update the MAC learning table? Should "learn"
219 * actions update the flow table?
221 * We want to update these tables if we are actually processing a packet,
222 * or if we are accounting for packets that the datapath has processed, but
223 * not if we are just revalidating. */
226 /* The rule that we are currently translating, or NULL. */
227 struct rule_dpif *rule;
229 /* Union of the set of TCP flags seen so far in this flow. (Used only by
230 * NXAST_FIN_TIMEOUT. Set to zero to avoid updating updating rules'
234 /* If nonnull, flow translation calls this function just before executing a
235 * resubmit or OFPP_TABLE action. In addition, disables logging of traces
236 * when the recursion depth is exceeded.
238 * 'rule' is the rule being submitted into. It will be null if the
239 * resubmit or OFPP_TABLE action didn't find a matching rule.
241 * This is normally null so the client has to set it manually after
242 * calling action_xlate_ctx_init(). */
243 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *rule);
245 /* If nonnull, flow translation credits the specified statistics to each
246 * rule reached through a resubmit or OFPP_TABLE action.
248 * This is normally null so the client has to set it manually after
249 * calling action_xlate_ctx_init(). */
250 const struct dpif_flow_stats *resubmit_stats;
252 /* xlate_actions() initializes and uses these members. The client might want
253 * to look at them after it returns. */
255 struct ofpbuf *odp_actions; /* Datapath actions. */
256 tag_type tags; /* Tags associated with actions. */
257 enum slow_path_reason slow; /* 0 if fast path may be used. */
258 bool has_learn; /* Actions include NXAST_LEARN? */
259 bool has_normal; /* Actions output to OFPP_NORMAL? */
260 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
261 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
262 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
264 /* xlate_actions() initializes and uses these members, but the client has no
265 * reason to look at them. */
267 int recurse; /* Recursion level, via xlate_table_action. */
268 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
269 struct flow base_flow; /* Flow at the last commit. */
270 uint32_t orig_skb_priority; /* Priority when packet arrived. */
271 uint8_t table_id; /* OpenFlow table ID where flow was found. */
272 uint32_t sflow_n_outputs; /* Number of output ports. */
273 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
274 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
275 bool exit; /* No further actions should be processed. */
276 struct flow orig_flow; /* Copy of original flow. */
279 static void action_xlate_ctx_init(struct action_xlate_ctx *,
280 struct ofproto_dpif *, const struct flow *,
281 ovs_be16 initial_tci, struct rule_dpif *,
282 uint8_t tcp_flags, const struct ofpbuf *);
283 static void xlate_actions(struct action_xlate_ctx *,
284 const union ofp_action *in, size_t n_in,
285 struct ofpbuf *odp_actions);
286 static void xlate_actions_for_side_effects(struct action_xlate_ctx *,
287 const union ofp_action *in,
290 static size_t put_userspace_action(const struct ofproto_dpif *,
291 struct ofpbuf *odp_actions,
293 const union user_action_cookie *);
295 static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
296 enum slow_path_reason,
297 uint64_t *stub, size_t stub_size,
298 const struct nlattr **actionsp,
299 size_t *actions_lenp);
301 /* A subfacet (see "struct subfacet" below) has three possible installation
304 * - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
305 * case just after the subfacet is created, just before the subfacet is
306 * destroyed, or if the datapath returns an error when we try to install a
309 * - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
311 * - SF_SLOW_PATH: An action that sends every packet for the subfacet through
312 * ofproto_dpif is installed in the datapath.
315 SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
316 SF_FAST_PATH, /* Full actions are installed. */
317 SF_SLOW_PATH, /* Send-to-userspace action is installed. */
320 static const char *subfacet_path_to_string(enum subfacet_path);
322 /* A dpif flow and actions associated with a facet.
324 * See also the large comment on struct facet. */
327 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
328 struct list list_node; /* In struct facet's 'facets' list. */
329 struct facet *facet; /* Owning facet. */
333 * To save memory in the common case, 'key' is NULL if 'key_fitness' is
334 * ODP_FIT_PERFECT, that is, odp_flow_key_from_flow() can accurately
335 * regenerate the ODP flow key from ->facet->flow. */
336 enum odp_key_fitness key_fitness;
340 long long int used; /* Time last used; time created if not used. */
342 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
343 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
347 * These should be essentially identical for every subfacet in a facet, but
348 * may differ in trivial ways due to VLAN splinters. */
349 size_t actions_len; /* Number of bytes in actions[]. */
350 struct nlattr *actions; /* Datapath actions. */
352 enum slow_path_reason slow; /* 0 if fast path may be used. */
353 enum subfacet_path path; /* Installed in datapath? */
355 /* This value is normally the same as ->facet->flow.vlan_tci. Only VLAN
356 * splinters can cause it to differ. This value should be removed when
357 * the VLAN splinters feature is no longer needed. */
358 ovs_be16 initial_tci; /* Initial VLAN TCI value. */
361 static struct subfacet *subfacet_create(struct facet *, enum odp_key_fitness,
362 const struct nlattr *key,
363 size_t key_len, ovs_be16 initial_tci);
364 static struct subfacet *subfacet_find(struct ofproto_dpif *,
365 const struct nlattr *key, size_t key_len);
366 static void subfacet_destroy(struct subfacet *);
367 static void subfacet_destroy__(struct subfacet *);
368 static void subfacet_get_key(struct subfacet *, struct odputil_keybuf *,
370 static void subfacet_reset_dp_stats(struct subfacet *,
371 struct dpif_flow_stats *);
372 static void subfacet_update_time(struct subfacet *, long long int used);
373 static void subfacet_update_stats(struct subfacet *,
374 const struct dpif_flow_stats *);
375 static void subfacet_make_actions(struct subfacet *,
376 const struct ofpbuf *packet,
377 struct ofpbuf *odp_actions);
378 static int subfacet_install(struct subfacet *,
379 const struct nlattr *actions, size_t actions_len,
380 struct dpif_flow_stats *, enum slow_path_reason);
381 static void subfacet_uninstall(struct subfacet *);
383 static enum subfacet_path subfacet_want_path(enum slow_path_reason);
385 /* An exact-match instantiation of an OpenFlow flow.
387 * A facet associates a "struct flow", which represents the Open vSwitch
388 * userspace idea of an exact-match flow, with one or more subfacets. Each
389 * subfacet tracks the datapath's idea of the exact-match flow equivalent to
390 * the facet. When the kernel module (or other dpif implementation) and Open
391 * vSwitch userspace agree on the definition of a flow key, there is exactly
392 * one subfacet per facet. If the dpif implementation supports more-specific
393 * flow matching than userspace, however, a facet can have more than one
394 * subfacet, each of which corresponds to some distinction in flow that
395 * userspace simply doesn't understand.
397 * Flow expiration works in terms of subfacets, so a facet must have at least
398 * one subfacet or it will never expire, leaking memory. */
401 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
402 struct list list_node; /* In owning rule's 'facets' list. */
403 struct rule_dpif *rule; /* Owning rule. */
406 struct list subfacets;
407 long long int used; /* Time last used; time created if not used. */
414 * - Do include packets and bytes sent "by hand", e.g. with
417 * - Do include packets and bytes that were obtained from the datapath
418 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
419 * DPIF_FP_ZERO_STATS).
421 * - Do not include packets or bytes that can be obtained from the
422 * datapath for any existing subfacet.
424 uint64_t packet_count; /* Number of packets received. */
425 uint64_t byte_count; /* Number of bytes received. */
427 /* Resubmit statistics. */
428 uint64_t prev_packet_count; /* Number of packets from last stats push. */
429 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
430 long long int prev_used; /* Used time from last stats push. */
433 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
434 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
435 uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
437 /* Properties of datapath actions.
439 * Every subfacet has its own actions because actions can differ slightly
440 * between splintered and non-splintered subfacets due to the VLAN tag
441 * being initially different (present vs. absent). All of them have these
442 * properties in common so we just store one copy of them here. */
443 bool has_learn; /* Actions include NXAST_LEARN? */
444 bool has_normal; /* Actions output to OFPP_NORMAL? */
445 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
446 tag_type tags; /* Tags that would require revalidation. */
447 mirror_mask_t mirrors; /* Bitmap of dependent mirrors. */
449 /* Storage for a single subfacet, to reduce malloc() time and space
450 * overhead. (A facet always has at least one subfacet and in the common
451 * case has exactly one subfacet.) */
452 struct subfacet one_subfacet;
455 static struct facet *facet_create(struct rule_dpif *,
456 const struct flow *, uint32_t hash);
457 static void facet_remove(struct facet *);
458 static void facet_free(struct facet *);
460 static struct facet *facet_find(struct ofproto_dpif *,
461 const struct flow *, uint32_t hash);
462 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
463 const struct flow *, uint32_t hash);
464 static void facet_revalidate(struct facet *);
465 static bool facet_check_consistency(struct facet *);
467 static void facet_flush_stats(struct facet *);
469 static void facet_update_time(struct facet *, long long int used);
470 static void facet_reset_counters(struct facet *);
471 static void facet_push_stats(struct facet *);
472 static void facet_learn(struct facet *);
473 static void facet_account(struct facet *);
475 static bool facet_is_controller_flow(struct facet *);
481 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
482 struct list bundle_node; /* In struct ofbundle's "ports" list. */
483 struct cfm *cfm; /* Connectivity Fault Management, if any. */
484 tag_type tag; /* Tag associated with this port. */
485 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
486 bool may_enable; /* May be enabled in bonds. */
487 long long int carrier_seq; /* Carrier status changes. */
490 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
491 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
492 long long int stp_state_entered;
494 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
496 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
498 * This is deprecated. It is only for compatibility with broken device
499 * drivers in old versions of Linux that do not properly support VLANs when
500 * VLAN devices are not used. When broken device drivers are no longer in
501 * widespread use, we will delete these interfaces. */
502 uint16_t realdev_ofp_port;
506 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
507 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
508 * traffic egressing the 'ofport' with that priority should be marked with. */
509 struct priority_to_dscp {
510 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
511 uint32_t priority; /* Priority of this queue (see struct flow). */
513 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
516 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
518 * This is deprecated. It is only for compatibility with broken device drivers
519 * in old versions of Linux that do not properly support VLANs when VLAN
520 * devices are not used. When broken device drivers are no longer in
521 * widespread use, we will delete these interfaces. */
522 struct vlan_splinter {
523 struct hmap_node realdev_vid_node;
524 struct hmap_node vlandev_node;
525 uint16_t realdev_ofp_port;
526 uint16_t vlandev_ofp_port;
530 static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
531 uint32_t realdev, ovs_be16 vlan_tci);
532 static bool vsp_adjust_flow(const struct ofproto_dpif *, struct flow *);
533 static void vsp_remove(struct ofport_dpif *);
534 static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
536 static struct ofport_dpif *
537 ofport_dpif_cast(const struct ofport *ofport)
539 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
540 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
543 static void port_run(struct ofport_dpif *);
544 static void port_run_fast(struct ofport_dpif *);
545 static void port_wait(struct ofport_dpif *);
546 static int set_cfm(struct ofport *, const struct cfm_settings *);
547 static void ofport_clear_priorities(struct ofport_dpif *);
549 struct dpif_completion {
550 struct list list_node;
551 struct ofoperation *op;
554 /* Extra information about a classifier table.
555 * Currently used just for optimized flow revalidation. */
557 /* If either of these is nonnull, then this table has a form that allows
558 * flows to be tagged to avoid revalidating most flows for the most common
559 * kinds of flow table changes. */
560 struct cls_table *catchall_table; /* Table that wildcards all fields. */
561 struct cls_table *other_table; /* Table with any other wildcard set. */
562 uint32_t basis; /* Keeps each table's tags separate. */
565 /* Reasons that we might need to revalidate every facet, and corresponding
568 * A value of 0 means that there is no need to revalidate.
570 * It would be nice to have some cleaner way to integrate with coverage
571 * counters, but with only a few reasons I guess this is good enough for
573 enum revalidate_reason {
574 REV_RECONFIGURE = 1, /* Switch configuration changed. */
575 REV_STP, /* Spanning tree protocol port status change. */
576 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
577 REV_FLOW_TABLE, /* Flow table changed. */
578 REV_INCONSISTENCY /* Facet self-check failed. */
580 COVERAGE_DEFINE(rev_reconfigure);
581 COVERAGE_DEFINE(rev_stp);
582 COVERAGE_DEFINE(rev_port_toggled);
583 COVERAGE_DEFINE(rev_flow_table);
584 COVERAGE_DEFINE(rev_inconsistency);
586 struct ofproto_dpif {
587 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
592 /* Special OpenFlow rules. */
593 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
594 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
600 struct netflow *netflow;
601 struct dpif_sflow *sflow;
602 struct hmap bundles; /* Contains "struct ofbundle"s. */
603 struct mac_learning *ml;
604 struct ofmirror *mirrors[MAX_MIRRORS];
606 bool has_bonded_bundles;
609 struct timer next_expiration;
613 struct hmap subfacets;
614 struct governor *governor;
617 struct table_dpif tables[N_TABLES];
618 enum revalidate_reason need_revalidate;
619 struct tag_set revalidate_set;
621 /* Support for debugging async flow mods. */
622 struct list completions;
624 bool has_bundle_action; /* True when the first bundle action appears. */
625 struct netdev_stats stats; /* To account packets generated and consumed in
630 long long int stp_last_tick;
632 /* VLAN splinters. */
633 struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
634 struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
637 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
638 * for debugging the asynchronous flow_mod implementation.) */
641 /* All existing ofproto_dpif instances, indexed by ->up.name. */
642 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
644 static void ofproto_dpif_unixctl_init(void);
646 static struct ofproto_dpif *
647 ofproto_dpif_cast(const struct ofproto *ofproto)
649 assert(ofproto->ofproto_class == &ofproto_dpif_class);
650 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
653 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *,
655 static struct ofport_dpif *get_odp_port(const struct ofproto_dpif *,
657 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
658 const struct ofpbuf *, ovs_be16 initial_tci,
661 /* Packet processing. */
662 static void update_learning_table(struct ofproto_dpif *,
663 const struct flow *, int vlan,
666 #define FLOW_MISS_MAX_BATCH 50
667 static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
669 /* Flow expiration. */
670 static int expire(struct ofproto_dpif *);
673 static void send_netflow_active_timeouts(struct ofproto_dpif *);
676 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
677 static size_t compose_sflow_action(const struct ofproto_dpif *,
678 struct ofpbuf *odp_actions,
679 const struct flow *, uint32_t odp_port);
680 static void add_mirror_actions(struct action_xlate_ctx *ctx,
681 const struct flow *flow);
682 /* Global variables. */
683 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
685 /* Factory functions. */
688 enumerate_types(struct sset *types)
690 dp_enumerate_types(types);
694 enumerate_names(const char *type, struct sset *names)
696 return dp_enumerate_names(type, names);
700 del(const char *type, const char *name)
705 error = dpif_open(name, type, &dpif);
707 error = dpif_delete(dpif);
713 /* Basic life-cycle. */
715 static int add_internal_flows(struct ofproto_dpif *);
717 static struct ofproto *
720 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
725 dealloc(struct ofproto *ofproto_)
727 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
732 construct(struct ofproto *ofproto_)
734 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
735 const char *name = ofproto->up.name;
739 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
741 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
745 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
746 ofproto->n_matches = 0;
748 dpif_flow_flush(ofproto->dpif);
749 dpif_recv_purge(ofproto->dpif);
751 error = dpif_recv_set(ofproto->dpif, true);
753 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
754 dpif_close(ofproto->dpif);
758 ofproto->netflow = NULL;
759 ofproto->sflow = NULL;
761 hmap_init(&ofproto->bundles);
762 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
763 for (i = 0; i < MAX_MIRRORS; i++) {
764 ofproto->mirrors[i] = NULL;
766 ofproto->has_bonded_bundles = false;
768 timer_set_duration(&ofproto->next_expiration, 1000);
770 hmap_init(&ofproto->facets);
771 hmap_init(&ofproto->subfacets);
772 ofproto->governor = NULL;
774 for (i = 0; i < N_TABLES; i++) {
775 struct table_dpif *table = &ofproto->tables[i];
777 table->catchall_table = NULL;
778 table->other_table = NULL;
779 table->basis = random_uint32();
781 ofproto->need_revalidate = 0;
782 tag_set_init(&ofproto->revalidate_set);
784 list_init(&ofproto->completions);
786 ofproto_dpif_unixctl_init();
788 ofproto->has_mirrors = false;
789 ofproto->has_bundle_action = false;
791 hmap_init(&ofproto->vlandev_map);
792 hmap_init(&ofproto->realdev_vid_map);
794 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
795 hash_string(ofproto->up.name, 0));
796 memset(&ofproto->stats, 0, sizeof ofproto->stats);
798 ofproto_init_tables(ofproto_, N_TABLES);
799 error = add_internal_flows(ofproto);
800 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
806 add_internal_flow(struct ofproto_dpif *ofproto, int id,
807 const struct ofpbuf *actions, struct rule_dpif **rulep)
809 struct ofputil_flow_mod fm;
812 cls_rule_init_catchall(&fm.cr, 0);
813 cls_rule_set_reg(&fm.cr, 0, id);
814 fm.new_cookie = htonll(0);
815 fm.cookie = htonll(0);
816 fm.cookie_mask = htonll(0);
817 fm.table_id = TBL_INTERNAL;
818 fm.command = OFPFC_ADD;
824 fm.actions = actions->data;
825 fm.n_actions = actions->size / sizeof(union ofp_action);
827 error = ofproto_flow_mod(&ofproto->up, &fm);
829 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
830 id, ofperr_to_string(error));
834 *rulep = rule_dpif_lookup__(ofproto, &fm.cr.flow, TBL_INTERNAL);
835 assert(*rulep != NULL);
841 add_internal_flows(struct ofproto_dpif *ofproto)
843 struct nx_action_controller *nac;
844 uint64_t actions_stub[128 / 8];
845 struct ofpbuf actions;
849 ofpbuf_use_stack(&actions, actions_stub, sizeof actions_stub);
852 nac = ofputil_put_NXAST_CONTROLLER(&actions);
853 nac->max_len = htons(UINT16_MAX);
854 nac->controller_id = htons(0);
855 nac->reason = OFPR_NO_MATCH;
856 error = add_internal_flow(ofproto, id++, &actions, &ofproto->miss_rule);
861 ofpbuf_clear(&actions);
862 error = add_internal_flow(ofproto, id++, &actions,
863 &ofproto->no_packet_in_rule);
868 complete_operations(struct ofproto_dpif *ofproto)
870 struct dpif_completion *c, *next;
872 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
873 ofoperation_complete(c->op, 0);
874 list_remove(&c->list_node);
880 destruct(struct ofproto *ofproto_)
882 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
883 struct rule_dpif *rule, *next_rule;
884 struct oftable *table;
887 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
888 complete_operations(ofproto);
890 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
891 struct cls_cursor cursor;
893 cls_cursor_init(&cursor, &table->cls, NULL);
894 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
895 ofproto_rule_destroy(&rule->up);
899 for (i = 0; i < MAX_MIRRORS; i++) {
900 mirror_destroy(ofproto->mirrors[i]);
903 netflow_destroy(ofproto->netflow);
904 dpif_sflow_destroy(ofproto->sflow);
905 hmap_destroy(&ofproto->bundles);
906 mac_learning_destroy(ofproto->ml);
908 hmap_destroy(&ofproto->facets);
909 hmap_destroy(&ofproto->subfacets);
910 governor_destroy(ofproto->governor);
912 hmap_destroy(&ofproto->vlandev_map);
913 hmap_destroy(&ofproto->realdev_vid_map);
915 dpif_close(ofproto->dpif);
919 run_fast(struct ofproto *ofproto_)
921 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
922 struct ofport_dpif *ofport;
925 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
926 port_run_fast(ofport);
929 /* Handle one or more batches of upcalls, until there's nothing left to do
930 * or until we do a fixed total amount of work.
932 * We do work in batches because it can be much cheaper to set up a number
933 * of flows and fire off their patches all at once. We do multiple batches
934 * because in some cases handling a packet can cause another packet to be
935 * queued almost immediately as part of the return flow. Both
936 * optimizations can make major improvements on some benchmarks and
937 * presumably for real traffic as well. */
939 while (work < FLOW_MISS_MAX_BATCH) {
940 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
950 run(struct ofproto *ofproto_)
952 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
953 struct ofport_dpif *ofport;
954 struct ofbundle *bundle;
958 complete_operations(ofproto);
960 dpif_run(ofproto->dpif);
962 error = run_fast(ofproto_);
967 if (timer_expired(&ofproto->next_expiration)) {
968 int delay = expire(ofproto);
969 timer_set_duration(&ofproto->next_expiration, delay);
972 if (ofproto->netflow) {
973 if (netflow_run(ofproto->netflow)) {
974 send_netflow_active_timeouts(ofproto);
977 if (ofproto->sflow) {
978 dpif_sflow_run(ofproto->sflow);
981 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
984 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
989 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
991 /* Now revalidate if there's anything to do. */
992 if (ofproto->need_revalidate
993 || !tag_set_is_empty(&ofproto->revalidate_set)) {
994 struct tag_set revalidate_set = ofproto->revalidate_set;
995 bool revalidate_all = ofproto->need_revalidate;
998 switch (ofproto->need_revalidate) {
999 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
1000 case REV_STP: COVERAGE_INC(rev_stp); break;
1001 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
1002 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
1003 case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
1006 /* Clear the revalidation flags. */
1007 tag_set_init(&ofproto->revalidate_set);
1008 ofproto->need_revalidate = 0;
1010 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1012 || tag_set_intersects(&revalidate_set, facet->tags)) {
1013 facet_revalidate(facet);
1018 /* Check the consistency of a random facet, to aid debugging. */
1019 if (!hmap_is_empty(&ofproto->facets) && !ofproto->need_revalidate) {
1020 struct facet *facet;
1022 facet = CONTAINER_OF(hmap_random_node(&ofproto->facets),
1023 struct facet, hmap_node);
1024 if (!tag_set_intersects(&ofproto->revalidate_set, facet->tags)) {
1025 if (!facet_check_consistency(facet)) {
1026 ofproto->need_revalidate = REV_INCONSISTENCY;
1031 if (ofproto->governor) {
1034 governor_run(ofproto->governor);
1036 /* If the governor has shrunk to its minimum size and the number of
1037 * subfacets has dwindled, then drop the governor entirely.
1039 * For hysteresis, the number of subfacets to drop the governor is
1040 * smaller than the number needed to trigger its creation. */
1041 n_subfacets = hmap_count(&ofproto->subfacets);
1042 if (n_subfacets * 4 < ofproto->up.flow_eviction_threshold
1043 && governor_is_idle(ofproto->governor)) {
1044 governor_destroy(ofproto->governor);
1045 ofproto->governor = NULL;
1053 wait(struct ofproto *ofproto_)
1055 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1056 struct ofport_dpif *ofport;
1057 struct ofbundle *bundle;
1059 if (!clogged && !list_is_empty(&ofproto->completions)) {
1060 poll_immediate_wake();
1063 dpif_wait(ofproto->dpif);
1064 dpif_recv_wait(ofproto->dpif);
1065 if (ofproto->sflow) {
1066 dpif_sflow_wait(ofproto->sflow);
1068 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
1069 poll_immediate_wake();
1071 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1074 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1075 bundle_wait(bundle);
1077 if (ofproto->netflow) {
1078 netflow_wait(ofproto->netflow);
1080 mac_learning_wait(ofproto->ml);
1082 if (ofproto->need_revalidate) {
1083 /* Shouldn't happen, but if it does just go around again. */
1084 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1085 poll_immediate_wake();
1087 timer_wait(&ofproto->next_expiration);
1089 if (ofproto->governor) {
1090 governor_wait(ofproto->governor);
1095 get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1097 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1099 simap_increase(usage, "facets", hmap_count(&ofproto->facets));
1100 simap_increase(usage, "subfacets", hmap_count(&ofproto->subfacets));
1104 flush(struct ofproto *ofproto_)
1106 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1107 struct facet *facet, *next_facet;
1109 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1110 /* Mark the facet as not installed so that facet_remove() doesn't
1111 * bother trying to uninstall it. There is no point in uninstalling it
1112 * individually since we are about to blow away all the facets with
1113 * dpif_flow_flush(). */
1114 struct subfacet *subfacet;
1116 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
1117 subfacet->path = SF_NOT_INSTALLED;
1118 subfacet->dp_packet_count = 0;
1119 subfacet->dp_byte_count = 0;
1121 facet_remove(facet);
1123 dpif_flow_flush(ofproto->dpif);
1127 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1128 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1130 *arp_match_ip = true;
1131 *actions = (OFPUTIL_A_OUTPUT |
1132 OFPUTIL_A_SET_VLAN_VID |
1133 OFPUTIL_A_SET_VLAN_PCP |
1134 OFPUTIL_A_STRIP_VLAN |
1135 OFPUTIL_A_SET_DL_SRC |
1136 OFPUTIL_A_SET_DL_DST |
1137 OFPUTIL_A_SET_NW_SRC |
1138 OFPUTIL_A_SET_NW_DST |
1139 OFPUTIL_A_SET_NW_TOS |
1140 OFPUTIL_A_SET_TP_SRC |
1141 OFPUTIL_A_SET_TP_DST |
1146 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
1148 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1149 struct dpif_dp_stats s;
1151 strcpy(ots->name, "classifier");
1153 dpif_get_dp_stats(ofproto->dpif, &s);
1154 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
1155 put_32aligned_be64(&ots->matched_count,
1156 htonll(s.n_hit + ofproto->n_matches));
1159 static struct ofport *
1162 struct ofport_dpif *port = xmalloc(sizeof *port);
1167 port_dealloc(struct ofport *port_)
1169 struct ofport_dpif *port = ofport_dpif_cast(port_);
1174 port_construct(struct ofport *port_)
1176 struct ofport_dpif *port = ofport_dpif_cast(port_);
1177 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1179 ofproto->need_revalidate = REV_RECONFIGURE;
1180 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
1181 port->bundle = NULL;
1183 port->tag = tag_create_random();
1184 port->may_enable = true;
1185 port->stp_port = NULL;
1186 port->stp_state = STP_DISABLED;
1187 hmap_init(&port->priorities);
1188 port->realdev_ofp_port = 0;
1189 port->vlandev_vid = 0;
1190 port->carrier_seq = netdev_get_carrier_resets(port->up.netdev);
1192 if (ofproto->sflow) {
1193 dpif_sflow_add_port(ofproto->sflow, port_);
1200 port_destruct(struct ofport *port_)
1202 struct ofport_dpif *port = ofport_dpif_cast(port_);
1203 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1205 ofproto->need_revalidate = REV_RECONFIGURE;
1206 bundle_remove(port_);
1207 set_cfm(port_, NULL);
1208 if (ofproto->sflow) {
1209 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1212 ofport_clear_priorities(port);
1213 hmap_destroy(&port->priorities);
1217 port_modified(struct ofport *port_)
1219 struct ofport_dpif *port = ofport_dpif_cast(port_);
1221 if (port->bundle && port->bundle->bond) {
1222 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1227 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1229 struct ofport_dpif *port = ofport_dpif_cast(port_);
1230 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1231 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1233 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1234 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1235 OFPUTIL_PC_NO_PACKET_IN)) {
1236 ofproto->need_revalidate = REV_RECONFIGURE;
1238 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1239 bundle_update(port->bundle);
1245 set_sflow(struct ofproto *ofproto_,
1246 const struct ofproto_sflow_options *sflow_options)
1248 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1249 struct dpif_sflow *ds = ofproto->sflow;
1251 if (sflow_options) {
1253 struct ofport_dpif *ofport;
1255 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
1256 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1257 dpif_sflow_add_port(ds, &ofport->up);
1259 ofproto->need_revalidate = REV_RECONFIGURE;
1261 dpif_sflow_set_options(ds, sflow_options);
1264 dpif_sflow_destroy(ds);
1265 ofproto->need_revalidate = REV_RECONFIGURE;
1266 ofproto->sflow = NULL;
1273 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1275 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1282 struct ofproto_dpif *ofproto;
1284 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1285 ofproto->need_revalidate = REV_RECONFIGURE;
1286 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
1289 if (cfm_configure(ofport->cfm, s)) {
1295 cfm_destroy(ofport->cfm);
1301 get_cfm_fault(const struct ofport *ofport_)
1303 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1305 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
1309 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1312 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1315 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1323 get_cfm_health(const struct ofport *ofport_)
1325 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1327 return ofport->cfm ? cfm_get_health(ofport->cfm) : -1;
1330 /* Spanning Tree. */
1333 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1335 struct ofproto_dpif *ofproto = ofproto_;
1336 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1337 struct ofport_dpif *ofport;
1339 ofport = stp_port_get_aux(sp);
1341 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1342 ofproto->up.name, port_num);
1344 struct eth_header *eth = pkt->l2;
1346 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1347 if (eth_addr_is_zero(eth->eth_src)) {
1348 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1349 "with unknown MAC", ofproto->up.name, port_num);
1351 send_packet(ofport, pkt);
1357 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1359 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1361 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1363 /* Only revalidate flows if the configuration changed. */
1364 if (!s != !ofproto->stp) {
1365 ofproto->need_revalidate = REV_RECONFIGURE;
1369 if (!ofproto->stp) {
1370 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1371 send_bpdu_cb, ofproto);
1372 ofproto->stp_last_tick = time_msec();
1375 stp_set_bridge_id(ofproto->stp, s->system_id);
1376 stp_set_bridge_priority(ofproto->stp, s->priority);
1377 stp_set_hello_time(ofproto->stp, s->hello_time);
1378 stp_set_max_age(ofproto->stp, s->max_age);
1379 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1381 struct ofport *ofport;
1383 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1384 set_stp_port(ofport, NULL);
1387 stp_destroy(ofproto->stp);
1388 ofproto->stp = NULL;
1395 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1397 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1401 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1402 s->designated_root = stp_get_designated_root(ofproto->stp);
1403 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1412 update_stp_port_state(struct ofport_dpif *ofport)
1414 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1415 enum stp_state state;
1417 /* Figure out new state. */
1418 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1422 if (ofport->stp_state != state) {
1423 enum ofputil_port_state of_state;
1426 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1427 netdev_get_name(ofport->up.netdev),
1428 stp_state_name(ofport->stp_state),
1429 stp_state_name(state));
1430 if (stp_learn_in_state(ofport->stp_state)
1431 != stp_learn_in_state(state)) {
1432 /* xxx Learning action flows should also be flushed. */
1433 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
1435 fwd_change = stp_forward_in_state(ofport->stp_state)
1436 != stp_forward_in_state(state);
1438 ofproto->need_revalidate = REV_STP;
1439 ofport->stp_state = state;
1440 ofport->stp_state_entered = time_msec();
1442 if (fwd_change && ofport->bundle) {
1443 bundle_update(ofport->bundle);
1446 /* Update the STP state bits in the OpenFlow port description. */
1447 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1448 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1449 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1450 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1451 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1453 ofproto_port_set_state(&ofport->up, of_state);
1457 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1458 * caller is responsible for assigning STP port numbers and ensuring
1459 * there are no duplicates. */
1461 set_stp_port(struct ofport *ofport_,
1462 const struct ofproto_port_stp_settings *s)
1464 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1465 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1466 struct stp_port *sp = ofport->stp_port;
1468 if (!s || !s->enable) {
1470 ofport->stp_port = NULL;
1471 stp_port_disable(sp);
1472 update_stp_port_state(ofport);
1475 } else if (sp && stp_port_no(sp) != s->port_num
1476 && ofport == stp_port_get_aux(sp)) {
1477 /* The port-id changed, so disable the old one if it's not
1478 * already in use by another port. */
1479 stp_port_disable(sp);
1482 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1483 stp_port_enable(sp);
1485 stp_port_set_aux(sp, ofport);
1486 stp_port_set_priority(sp, s->priority);
1487 stp_port_set_path_cost(sp, s->path_cost);
1489 update_stp_port_state(ofport);
1495 get_stp_port_status(struct ofport *ofport_,
1496 struct ofproto_port_stp_status *s)
1498 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1499 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1500 struct stp_port *sp = ofport->stp_port;
1502 if (!ofproto->stp || !sp) {
1508 s->port_id = stp_port_get_id(sp);
1509 s->state = stp_port_get_state(sp);
1510 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1511 s->role = stp_port_get_role(sp);
1512 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1518 stp_run(struct ofproto_dpif *ofproto)
1521 long long int now = time_msec();
1522 long long int elapsed = now - ofproto->stp_last_tick;
1523 struct stp_port *sp;
1526 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1527 ofproto->stp_last_tick = now;
1529 while (stp_get_changed_port(ofproto->stp, &sp)) {
1530 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1533 update_stp_port_state(ofport);
1537 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1538 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
1544 stp_wait(struct ofproto_dpif *ofproto)
1547 poll_timer_wait(1000);
1551 /* Returns true if STP should process 'flow'. */
1553 stp_should_process_flow(const struct flow *flow)
1555 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1559 stp_process_packet(const struct ofport_dpif *ofport,
1560 const struct ofpbuf *packet)
1562 struct ofpbuf payload = *packet;
1563 struct eth_header *eth = payload.data;
1564 struct stp_port *sp = ofport->stp_port;
1566 /* Sink packets on ports that have STP disabled when the bridge has
1568 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1572 /* Trim off padding on payload. */
1573 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1574 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1577 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1578 stp_received_bpdu(sp, payload.data, payload.size);
1582 static struct priority_to_dscp *
1583 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1585 struct priority_to_dscp *pdscp;
1588 hash = hash_int(priority, 0);
1589 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1590 if (pdscp->priority == priority) {
1598 ofport_clear_priorities(struct ofport_dpif *ofport)
1600 struct priority_to_dscp *pdscp, *next;
1602 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1603 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1609 set_queues(struct ofport *ofport_,
1610 const struct ofproto_port_queue *qdscp_list,
1613 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1614 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1615 struct hmap new = HMAP_INITIALIZER(&new);
1618 for (i = 0; i < n_qdscp; i++) {
1619 struct priority_to_dscp *pdscp;
1623 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1624 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1629 pdscp = get_priority(ofport, priority);
1631 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1633 pdscp = xmalloc(sizeof *pdscp);
1634 pdscp->priority = priority;
1636 ofproto->need_revalidate = REV_RECONFIGURE;
1639 if (pdscp->dscp != dscp) {
1641 ofproto->need_revalidate = REV_RECONFIGURE;
1644 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1647 if (!hmap_is_empty(&ofport->priorities)) {
1648 ofport_clear_priorities(ofport);
1649 ofproto->need_revalidate = REV_RECONFIGURE;
1652 hmap_swap(&new, &ofport->priorities);
1660 /* Expires all MAC learning entries associated with 'bundle' and forces its
1661 * ofproto to revalidate every flow.
1663 * Normally MAC learning entries are removed only from the ofproto associated
1664 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
1665 * are removed from every ofproto. When patch ports and SLB bonds are in use
1666 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
1667 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
1668 * with the host from which it migrated. */
1670 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
1672 struct ofproto_dpif *ofproto = bundle->ofproto;
1673 struct mac_learning *ml = ofproto->ml;
1674 struct mac_entry *mac, *next_mac;
1676 ofproto->need_revalidate = REV_RECONFIGURE;
1677 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1678 if (mac->port.p == bundle) {
1680 struct ofproto_dpif *o;
1682 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1684 struct mac_entry *e;
1686 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan,
1689 tag_set_add(&o->revalidate_set, e->tag);
1690 mac_learning_expire(o->ml, e);
1696 mac_learning_expire(ml, mac);
1701 static struct ofbundle *
1702 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1704 struct ofbundle *bundle;
1706 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1707 &ofproto->bundles) {
1708 if (bundle->aux == aux) {
1715 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1716 * ones that are found to 'bundles'. */
1718 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1719 void **auxes, size_t n_auxes,
1720 struct hmapx *bundles)
1724 hmapx_init(bundles);
1725 for (i = 0; i < n_auxes; i++) {
1726 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1728 hmapx_add(bundles, bundle);
1734 bundle_update(struct ofbundle *bundle)
1736 struct ofport_dpif *port;
1738 bundle->floodable = true;
1739 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1740 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1741 || !stp_forward_in_state(port->stp_state)) {
1742 bundle->floodable = false;
1749 bundle_del_port(struct ofport_dpif *port)
1751 struct ofbundle *bundle = port->bundle;
1753 bundle->ofproto->need_revalidate = REV_RECONFIGURE;
1755 list_remove(&port->bundle_node);
1756 port->bundle = NULL;
1759 lacp_slave_unregister(bundle->lacp, port);
1762 bond_slave_unregister(bundle->bond, port);
1765 bundle_update(bundle);
1769 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1770 struct lacp_slave_settings *lacp,
1771 uint32_t bond_stable_id)
1773 struct ofport_dpif *port;
1775 port = get_ofp_port(bundle->ofproto, ofp_port);
1780 if (port->bundle != bundle) {
1781 bundle->ofproto->need_revalidate = REV_RECONFIGURE;
1783 bundle_del_port(port);
1786 port->bundle = bundle;
1787 list_push_back(&bundle->ports, &port->bundle_node);
1788 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1789 || !stp_forward_in_state(port->stp_state)) {
1790 bundle->floodable = false;
1794 port->bundle->ofproto->need_revalidate = REV_RECONFIGURE;
1795 lacp_slave_register(bundle->lacp, port, lacp);
1798 port->bond_stable_id = bond_stable_id;
1804 bundle_destroy(struct ofbundle *bundle)
1806 struct ofproto_dpif *ofproto;
1807 struct ofport_dpif *port, *next_port;
1814 ofproto = bundle->ofproto;
1815 for (i = 0; i < MAX_MIRRORS; i++) {
1816 struct ofmirror *m = ofproto->mirrors[i];
1818 if (m->out == bundle) {
1820 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1821 || hmapx_find_and_delete(&m->dsts, bundle)) {
1822 ofproto->need_revalidate = REV_RECONFIGURE;
1827 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1828 bundle_del_port(port);
1831 bundle_flush_macs(bundle, true);
1832 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1834 free(bundle->trunks);
1835 lacp_destroy(bundle->lacp);
1836 bond_destroy(bundle->bond);
1841 bundle_set(struct ofproto *ofproto_, void *aux,
1842 const struct ofproto_bundle_settings *s)
1844 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1845 bool need_flush = false;
1846 struct ofport_dpif *port;
1847 struct ofbundle *bundle;
1848 unsigned long *trunks;
1854 bundle_destroy(bundle_lookup(ofproto, aux));
1858 assert(s->n_slaves == 1 || s->bond != NULL);
1859 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1861 bundle = bundle_lookup(ofproto, aux);
1863 bundle = xmalloc(sizeof *bundle);
1865 bundle->ofproto = ofproto;
1866 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1867 hash_pointer(aux, 0));
1869 bundle->name = NULL;
1871 list_init(&bundle->ports);
1872 bundle->vlan_mode = PORT_VLAN_TRUNK;
1874 bundle->trunks = NULL;
1875 bundle->use_priority_tags = s->use_priority_tags;
1876 bundle->lacp = NULL;
1877 bundle->bond = NULL;
1879 bundle->floodable = true;
1881 bundle->src_mirrors = 0;
1882 bundle->dst_mirrors = 0;
1883 bundle->mirror_out = 0;
1886 if (!bundle->name || strcmp(s->name, bundle->name)) {
1888 bundle->name = xstrdup(s->name);
1893 if (!bundle->lacp) {
1894 ofproto->need_revalidate = REV_RECONFIGURE;
1895 bundle->lacp = lacp_create();
1897 lacp_configure(bundle->lacp, s->lacp);
1899 lacp_destroy(bundle->lacp);
1900 bundle->lacp = NULL;
1903 /* Update set of ports. */
1905 for (i = 0; i < s->n_slaves; i++) {
1906 if (!bundle_add_port(bundle, s->slaves[i],
1907 s->lacp ? &s->lacp_slaves[i] : NULL,
1908 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1912 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1913 struct ofport_dpif *next_port;
1915 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1916 for (i = 0; i < s->n_slaves; i++) {
1917 if (s->slaves[i] == port->up.ofp_port) {
1922 bundle_del_port(port);
1926 assert(list_size(&bundle->ports) <= s->n_slaves);
1928 if (list_is_empty(&bundle->ports)) {
1929 bundle_destroy(bundle);
1933 /* Set VLAN tagging mode */
1934 if (s->vlan_mode != bundle->vlan_mode
1935 || s->use_priority_tags != bundle->use_priority_tags) {
1936 bundle->vlan_mode = s->vlan_mode;
1937 bundle->use_priority_tags = s->use_priority_tags;
1942 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1943 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1945 if (vlan != bundle->vlan) {
1946 bundle->vlan = vlan;
1950 /* Get trunked VLANs. */
1951 switch (s->vlan_mode) {
1952 case PORT_VLAN_ACCESS:
1956 case PORT_VLAN_TRUNK:
1957 trunks = (unsigned long *) s->trunks;
1960 case PORT_VLAN_NATIVE_UNTAGGED:
1961 case PORT_VLAN_NATIVE_TAGGED:
1962 if (vlan != 0 && (!s->trunks
1963 || !bitmap_is_set(s->trunks, vlan)
1964 || bitmap_is_set(s->trunks, 0))) {
1965 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1967 trunks = bitmap_clone(s->trunks, 4096);
1969 trunks = bitmap_allocate1(4096);
1971 bitmap_set1(trunks, vlan);
1972 bitmap_set0(trunks, 0);
1974 trunks = (unsigned long *) s->trunks;
1981 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1982 free(bundle->trunks);
1983 if (trunks == s->trunks) {
1984 bundle->trunks = vlan_bitmap_clone(trunks);
1986 bundle->trunks = trunks;
1991 if (trunks != s->trunks) {
1996 if (!list_is_short(&bundle->ports)) {
1997 bundle->ofproto->has_bonded_bundles = true;
1999 if (bond_reconfigure(bundle->bond, s->bond)) {
2000 ofproto->need_revalidate = REV_RECONFIGURE;
2003 bundle->bond = bond_create(s->bond);
2004 ofproto->need_revalidate = REV_RECONFIGURE;
2007 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2008 bond_slave_register(bundle->bond, port, port->bond_stable_id,
2012 bond_destroy(bundle->bond);
2013 bundle->bond = NULL;
2016 /* If we changed something that would affect MAC learning, un-learn
2017 * everything on this port and force flow revalidation. */
2019 bundle_flush_macs(bundle, false);
2026 bundle_remove(struct ofport *port_)
2028 struct ofport_dpif *port = ofport_dpif_cast(port_);
2029 struct ofbundle *bundle = port->bundle;
2032 bundle_del_port(port);
2033 if (list_is_empty(&bundle->ports)) {
2034 bundle_destroy(bundle);
2035 } else if (list_is_short(&bundle->ports)) {
2036 bond_destroy(bundle->bond);
2037 bundle->bond = NULL;
2043 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2045 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2046 struct ofport_dpif *port = port_;
2047 uint8_t ea[ETH_ADDR_LEN];
2050 error = netdev_get_etheraddr(port->up.netdev, ea);
2052 struct ofpbuf packet;
2055 ofpbuf_init(&packet, 0);
2056 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2058 memcpy(packet_pdu, pdu, pdu_size);
2060 send_packet(port, &packet);
2061 ofpbuf_uninit(&packet);
2063 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2064 "%s (%s)", port->bundle->name,
2065 netdev_get_name(port->up.netdev), strerror(error));
2070 bundle_send_learning_packets(struct ofbundle *bundle)
2072 struct ofproto_dpif *ofproto = bundle->ofproto;
2073 int error, n_packets, n_errors;
2074 struct mac_entry *e;
2076 error = n_packets = n_errors = 0;
2077 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2078 if (e->port.p != bundle) {
2079 struct ofpbuf *learning_packet;
2080 struct ofport_dpif *port;
2084 /* The assignment to "port" is unnecessary but makes "grep"ing for
2085 * struct ofport_dpif more effective. */
2086 learning_packet = bond_compose_learning_packet(bundle->bond,
2090 ret = send_packet(port, learning_packet);
2091 ofpbuf_delete(learning_packet);
2101 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2102 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2103 "packets, last error was: %s",
2104 bundle->name, n_errors, n_packets, strerror(error));
2106 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2107 bundle->name, n_packets);
2112 bundle_run(struct ofbundle *bundle)
2115 lacp_run(bundle->lacp, send_pdu_cb);
2118 struct ofport_dpif *port;
2120 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2121 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2124 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
2125 lacp_status(bundle->lacp));
2126 if (bond_should_send_learning_packets(bundle->bond)) {
2127 bundle_send_learning_packets(bundle);
2133 bundle_wait(struct ofbundle *bundle)
2136 lacp_wait(bundle->lacp);
2139 bond_wait(bundle->bond);
2146 mirror_scan(struct ofproto_dpif *ofproto)
2150 for (idx = 0; idx < MAX_MIRRORS; idx++) {
2151 if (!ofproto->mirrors[idx]) {
2158 static struct ofmirror *
2159 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
2163 for (i = 0; i < MAX_MIRRORS; i++) {
2164 struct ofmirror *mirror = ofproto->mirrors[i];
2165 if (mirror && mirror->aux == aux) {
2173 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
2175 mirror_update_dups(struct ofproto_dpif *ofproto)
2179 for (i = 0; i < MAX_MIRRORS; i++) {
2180 struct ofmirror *m = ofproto->mirrors[i];
2183 m->dup_mirrors = MIRROR_MASK_C(1) << i;
2187 for (i = 0; i < MAX_MIRRORS; i++) {
2188 struct ofmirror *m1 = ofproto->mirrors[i];
2195 for (j = i + 1; j < MAX_MIRRORS; j++) {
2196 struct ofmirror *m2 = ofproto->mirrors[j];
2198 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
2199 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
2200 m2->dup_mirrors |= m1->dup_mirrors;
2207 mirror_set(struct ofproto *ofproto_, void *aux,
2208 const struct ofproto_mirror_settings *s)
2210 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2211 mirror_mask_t mirror_bit;
2212 struct ofbundle *bundle;
2213 struct ofmirror *mirror;
2214 struct ofbundle *out;
2215 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
2216 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
2219 mirror = mirror_lookup(ofproto, aux);
2221 mirror_destroy(mirror);
2227 idx = mirror_scan(ofproto);
2229 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
2231 ofproto->up.name, MAX_MIRRORS, s->name);
2235 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
2236 mirror->ofproto = ofproto;
2239 mirror->out_vlan = -1;
2240 mirror->name = NULL;
2243 if (!mirror->name || strcmp(s->name, mirror->name)) {
2245 mirror->name = xstrdup(s->name);
2248 /* Get the new configuration. */
2249 if (s->out_bundle) {
2250 out = bundle_lookup(ofproto, s->out_bundle);
2252 mirror_destroy(mirror);
2258 out_vlan = s->out_vlan;
2260 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
2261 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
2263 /* If the configuration has not changed, do nothing. */
2264 if (hmapx_equals(&srcs, &mirror->srcs)
2265 && hmapx_equals(&dsts, &mirror->dsts)
2266 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
2267 && mirror->out == out
2268 && mirror->out_vlan == out_vlan)
2270 hmapx_destroy(&srcs);
2271 hmapx_destroy(&dsts);
2275 hmapx_swap(&srcs, &mirror->srcs);
2276 hmapx_destroy(&srcs);
2278 hmapx_swap(&dsts, &mirror->dsts);
2279 hmapx_destroy(&dsts);
2281 free(mirror->vlans);
2282 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
2285 mirror->out_vlan = out_vlan;
2287 /* Update bundles. */
2288 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2289 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
2290 if (hmapx_contains(&mirror->srcs, bundle)) {
2291 bundle->src_mirrors |= mirror_bit;
2293 bundle->src_mirrors &= ~mirror_bit;
2296 if (hmapx_contains(&mirror->dsts, bundle)) {
2297 bundle->dst_mirrors |= mirror_bit;
2299 bundle->dst_mirrors &= ~mirror_bit;
2302 if (mirror->out == bundle) {
2303 bundle->mirror_out |= mirror_bit;
2305 bundle->mirror_out &= ~mirror_bit;
2309 ofproto->need_revalidate = REV_RECONFIGURE;
2310 ofproto->has_mirrors = true;
2311 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
2312 mirror_update_dups(ofproto);
2318 mirror_destroy(struct ofmirror *mirror)
2320 struct ofproto_dpif *ofproto;
2321 mirror_mask_t mirror_bit;
2322 struct ofbundle *bundle;
2329 ofproto = mirror->ofproto;
2330 ofproto->need_revalidate = REV_RECONFIGURE;
2331 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
2333 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2334 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2335 bundle->src_mirrors &= ~mirror_bit;
2336 bundle->dst_mirrors &= ~mirror_bit;
2337 bundle->mirror_out &= ~mirror_bit;
2340 hmapx_destroy(&mirror->srcs);
2341 hmapx_destroy(&mirror->dsts);
2342 free(mirror->vlans);
2344 ofproto->mirrors[mirror->idx] = NULL;
2348 mirror_update_dups(ofproto);
2350 ofproto->has_mirrors = false;
2351 for (i = 0; i < MAX_MIRRORS; i++) {
2352 if (ofproto->mirrors[i]) {
2353 ofproto->has_mirrors = true;
2360 mirror_get_stats(struct ofproto *ofproto_, void *aux,
2361 uint64_t *packets, uint64_t *bytes)
2363 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2364 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2367 *packets = *bytes = UINT64_MAX;
2371 *packets = mirror->packet_count;
2372 *bytes = mirror->byte_count;
2378 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2380 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2381 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2382 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
2388 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2390 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2391 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2392 return bundle && bundle->mirror_out != 0;
2396 forward_bpdu_changed(struct ofproto *ofproto_)
2398 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2399 ofproto->need_revalidate = REV_RECONFIGURE;
2403 set_mac_idle_time(struct ofproto *ofproto_, unsigned int idle_time)
2405 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2406 mac_learning_set_idle_time(ofproto->ml, idle_time);
2411 static struct ofport_dpif *
2412 get_ofp_port(const struct ofproto_dpif *ofproto, uint16_t ofp_port)
2414 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2415 return ofport ? ofport_dpif_cast(ofport) : NULL;
2418 static struct ofport_dpif *
2419 get_odp_port(const struct ofproto_dpif *ofproto, uint32_t odp_port)
2421 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2425 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2426 struct dpif_port *dpif_port)
2428 ofproto_port->name = dpif_port->name;
2429 ofproto_port->type = dpif_port->type;
2430 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2434 port_run_fast(struct ofport_dpif *ofport)
2436 if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) {
2437 struct ofpbuf packet;
2439 ofpbuf_init(&packet, 0);
2440 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr);
2441 send_packet(ofport, &packet);
2442 ofpbuf_uninit(&packet);
2447 port_run(struct ofport_dpif *ofport)
2449 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2450 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2451 bool enable = netdev_get_carrier(ofport->up.netdev);
2453 ofport->carrier_seq = carrier_seq;
2455 port_run_fast(ofport);
2457 cfm_run(ofport->cfm);
2458 enable = enable && !cfm_get_fault(ofport->cfm)
2459 && cfm_get_opup(ofport->cfm);
2462 if (ofport->bundle) {
2463 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2464 if (carrier_changed) {
2465 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2469 if (ofport->may_enable != enable) {
2470 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2472 if (ofproto->has_bundle_action) {
2473 ofproto->need_revalidate = REV_PORT_TOGGLED;
2477 ofport->may_enable = enable;
2481 port_wait(struct ofport_dpif *ofport)
2484 cfm_wait(ofport->cfm);
2489 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2490 struct ofproto_port *ofproto_port)
2492 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2493 struct dpif_port dpif_port;
2496 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2498 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2504 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2506 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2510 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2512 *ofp_portp = odp_port_to_ofp_port(odp_port);
2518 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2520 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2523 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2525 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2527 /* The caller is going to close ofport->up.netdev. If this is a
2528 * bonded port, then the bond is using that netdev, so remove it
2529 * from the bond. The client will need to reconfigure everything
2530 * after deleting ports, so then the slave will get re-added. */
2531 bundle_remove(&ofport->up);
2538 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2540 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2543 error = netdev_get_stats(ofport->up.netdev, stats);
2545 if (!error && ofport->odp_port == OVSP_LOCAL) {
2546 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2548 /* ofproto->stats.tx_packets represents packets that we created
2549 * internally and sent to some port (e.g. packets sent with
2550 * send_packet()). Account for them as if they had come from
2551 * OFPP_LOCAL and got forwarded. */
2553 if (stats->rx_packets != UINT64_MAX) {
2554 stats->rx_packets += ofproto->stats.tx_packets;
2557 if (stats->rx_bytes != UINT64_MAX) {
2558 stats->rx_bytes += ofproto->stats.tx_bytes;
2561 /* ofproto->stats.rx_packets represents packets that were received on
2562 * some port and we processed internally and dropped (e.g. STP).
2563 * Account fro them as if they had been forwarded to OFPP_LOCAL. */
2565 if (stats->tx_packets != UINT64_MAX) {
2566 stats->tx_packets += ofproto->stats.rx_packets;
2569 if (stats->tx_bytes != UINT64_MAX) {
2570 stats->tx_bytes += ofproto->stats.rx_bytes;
2577 /* Account packets for LOCAL port. */
2579 ofproto_update_local_port_stats(const struct ofproto *ofproto_,
2580 size_t tx_size, size_t rx_size)
2582 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2585 ofproto->stats.rx_packets++;
2586 ofproto->stats.rx_bytes += rx_size;
2589 ofproto->stats.tx_packets++;
2590 ofproto->stats.tx_bytes += tx_size;
2594 struct port_dump_state {
2595 struct dpif_port_dump dump;
2600 port_dump_start(const struct ofproto *ofproto_, void **statep)
2602 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2603 struct port_dump_state *state;
2605 *statep = state = xmalloc(sizeof *state);
2606 dpif_port_dump_start(&state->dump, ofproto->dpif);
2607 state->done = false;
2612 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2613 struct ofproto_port *port)
2615 struct port_dump_state *state = state_;
2616 struct dpif_port dpif_port;
2618 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2619 ofproto_port_from_dpif_port(port, &dpif_port);
2622 int error = dpif_port_dump_done(&state->dump);
2624 return error ? error : EOF;
2629 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2631 struct port_dump_state *state = state_;
2634 dpif_port_dump_done(&state->dump);
2641 port_poll(const struct ofproto *ofproto_, char **devnamep)
2643 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2644 return dpif_port_poll(ofproto->dpif, devnamep);
2648 port_poll_wait(const struct ofproto *ofproto_)
2650 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2651 dpif_port_poll_wait(ofproto->dpif);
2655 port_is_lacp_current(const struct ofport *ofport_)
2657 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2658 return (ofport->bundle && ofport->bundle->lacp
2659 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2663 /* Upcall handling. */
2665 /* Flow miss batching.
2667 * Some dpifs implement operations faster when you hand them off in a batch.
2668 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2669 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2670 * more packets, plus possibly installing the flow in the dpif.
2672 * So far we only batch the operations that affect flow setup time the most.
2673 * It's possible to batch more than that, but the benefit might be minimal. */
2675 struct hmap_node hmap_node;
2677 enum odp_key_fitness key_fitness;
2678 const struct nlattr *key;
2680 ovs_be16 initial_tci;
2681 struct list packets;
2682 enum dpif_upcall_type upcall_type;
2685 struct flow_miss_op {
2686 struct dpif_op dpif_op;
2687 struct subfacet *subfacet; /* Subfacet */
2688 void *garbage; /* Pointer to pass to free(), NULL if none. */
2689 uint64_t stub[1024 / 8]; /* Temporary buffer. */
2692 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2693 * OpenFlow controller as necessary according to their individual
2694 * configurations. */
2696 send_packet_in_miss(struct ofproto_dpif *ofproto, const struct ofpbuf *packet,
2697 const struct flow *flow)
2699 struct ofputil_packet_in pin;
2701 pin.packet = packet->data;
2702 pin.packet_len = packet->size;
2703 pin.reason = OFPR_NO_MATCH;
2704 pin.controller_id = 0;
2709 pin.send_len = 0; /* not used for flow table misses */
2711 flow_get_metadata(flow, &pin.fmd);
2713 /* Registers aren't meaningful on a miss. */
2714 memset(pin.fmd.reg_masks, 0, sizeof pin.fmd.reg_masks);
2716 connmgr_send_packet_in(ofproto->up.connmgr, &pin);
2719 static enum slow_path_reason
2720 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2721 const struct ofpbuf *packet)
2723 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2729 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2731 cfm_process_heartbeat(ofport->cfm, packet);
2734 } else if (ofport->bundle && ofport->bundle->lacp
2735 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2737 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2740 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2742 stp_process_packet(ofport, packet);
2749 static struct flow_miss *
2750 flow_miss_find(struct hmap *todo, const struct flow *flow, uint32_t hash)
2752 struct flow_miss *miss;
2754 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2755 if (flow_equal(&miss->flow, flow)) {
2763 /* Partially Initializes 'op' as an "execute" operation for 'miss' and
2764 * 'packet'. The caller must initialize op->actions and op->actions_len. If
2765 * 'miss' is associated with a subfacet the caller must also initialize the
2766 * returned op->subfacet, and if anything needs to be freed after processing
2767 * the op, the caller must initialize op->garbage also. */
2769 init_flow_miss_execute_op(struct flow_miss *miss, struct ofpbuf *packet,
2770 struct flow_miss_op *op)
2772 if (miss->flow.vlan_tci != miss->initial_tci) {
2773 /* This packet was received on a VLAN splinter port. We
2774 * added a VLAN to the packet to make the packet resemble
2775 * the flow, but the actions were composed assuming that
2776 * the packet contained no VLAN. So, we must remove the
2777 * VLAN header from the packet before trying to execute the
2779 eth_pop_vlan(packet);
2782 op->subfacet = NULL;
2784 op->dpif_op.type = DPIF_OP_EXECUTE;
2785 op->dpif_op.u.execute.key = miss->key;
2786 op->dpif_op.u.execute.key_len = miss->key_len;
2787 op->dpif_op.u.execute.packet = packet;
2790 /* Helper for handle_flow_miss_without_facet() and
2791 * handle_flow_miss_with_facet(). */
2793 handle_flow_miss_common(struct rule_dpif *rule,
2794 struct ofpbuf *packet, const struct flow *flow)
2796 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2798 ofproto->n_matches++;
2800 if (rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2802 * Extra-special case for fail-open mode.
2804 * We are in fail-open mode and the packet matched the fail-open
2805 * rule, but we are connected to a controller too. We should send
2806 * the packet up to the controller in the hope that it will try to
2807 * set up a flow and thereby allow us to exit fail-open.
2809 * See the top-level comment in fail-open.c for more information.
2811 send_packet_in_miss(ofproto, packet, flow);
2815 /* Figures out whether a flow that missed in 'ofproto', whose details are in
2816 * 'miss', is likely to be worth tracking in detail in userspace and (usually)
2817 * installing a datapath flow. The answer is usually "yes" (a return value of
2818 * true). However, for short flows the cost of bookkeeping is much higher than
2819 * the benefits, so when the datapath holds a large number of flows we impose
2820 * some heuristics to decide which flows are likely to be worth tracking. */
2822 flow_miss_should_make_facet(struct ofproto_dpif *ofproto,
2823 struct flow_miss *miss, uint32_t hash)
2825 if (!ofproto->governor) {
2828 n_subfacets = hmap_count(&ofproto->subfacets);
2829 if (n_subfacets * 2 <= ofproto->up.flow_eviction_threshold) {
2833 ofproto->governor = governor_create(ofproto->up.name);
2836 return governor_should_install_flow(ofproto->governor, hash,
2837 list_size(&miss->packets));
2840 /* Handles 'miss', which matches 'rule', without creating a facet or subfacet
2841 * or creating any datapath flow. May add an "execute" operation to 'ops' and
2842 * increment '*n_ops'. */
2844 handle_flow_miss_without_facet(struct flow_miss *miss,
2845 struct rule_dpif *rule,
2846 struct flow_miss_op *ops, size_t *n_ops)
2848 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2849 struct action_xlate_ctx ctx;
2850 struct ofpbuf *packet;
2852 LIST_FOR_EACH (packet, list_node, &miss->packets) {
2853 struct flow_miss_op *op = &ops[*n_ops];
2854 struct dpif_flow_stats stats;
2855 struct ofpbuf odp_actions;
2857 COVERAGE_INC(facet_suppress);
2859 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
2861 dpif_flow_stats_extract(&miss->flow, packet, &stats);
2862 rule_credit_stats(rule, &stats);
2864 action_xlate_ctx_init(&ctx, ofproto, &miss->flow, miss->initial_tci,
2866 ctx.resubmit_stats = &stats;
2867 xlate_actions(&ctx, rule->up.actions, rule->up.n_actions,
2870 if (odp_actions.size) {
2871 struct dpif_execute *execute = &op->dpif_op.u.execute;
2873 init_flow_miss_execute_op(miss, packet, op);
2874 execute->actions = odp_actions.data;
2875 execute->actions_len = odp_actions.size;
2876 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
2880 ofpbuf_uninit(&odp_actions);
2885 /* Handles 'miss', which matches 'facet'. May add any required datapath
2886 * operations to 'ops', incrementing '*n_ops' for each new op. */
2888 handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
2889 struct flow_miss_op *ops, size_t *n_ops)
2891 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
2892 enum subfacet_path want_path;
2893 struct subfacet *subfacet;
2894 struct ofpbuf *packet;
2896 subfacet = subfacet_create(facet,
2897 miss->key_fitness, miss->key, miss->key_len,
2900 LIST_FOR_EACH (packet, list_node, &miss->packets) {
2901 struct flow_miss_op *op = &ops[*n_ops];
2902 struct dpif_flow_stats stats;
2903 struct ofpbuf odp_actions;
2905 handle_flow_miss_common(facet->rule, packet, &miss->flow);
2907 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
2908 if (!subfacet->actions || subfacet->slow) {
2909 subfacet_make_actions(subfacet, packet, &odp_actions);
2912 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2913 subfacet_update_stats(subfacet, &stats);
2915 if (subfacet->actions_len) {
2916 struct dpif_execute *execute = &op->dpif_op.u.execute;
2918 init_flow_miss_execute_op(miss, packet, op);
2919 op->subfacet = subfacet;
2920 if (!subfacet->slow) {
2921 execute->actions = subfacet->actions;
2922 execute->actions_len = subfacet->actions_len;
2923 ofpbuf_uninit(&odp_actions);
2925 execute->actions = odp_actions.data;
2926 execute->actions_len = odp_actions.size;
2927 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
2932 ofpbuf_uninit(&odp_actions);
2936 want_path = subfacet_want_path(subfacet->slow);
2937 if (miss->upcall_type == DPIF_UC_MISS || subfacet->path != want_path) {
2938 struct flow_miss_op *op = &ops[(*n_ops)++];
2939 struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
2941 op->subfacet = subfacet;
2943 op->dpif_op.type = DPIF_OP_FLOW_PUT;
2944 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2945 put->key = miss->key;
2946 put->key_len = miss->key_len;
2947 if (want_path == SF_FAST_PATH) {
2948 put->actions = subfacet->actions;
2949 put->actions_len = subfacet->actions_len;
2951 compose_slow_path(ofproto, &facet->flow, subfacet->slow,
2952 op->stub, sizeof op->stub,
2953 &put->actions, &put->actions_len);
2959 /* Handles flow miss 'miss' on 'ofproto'. May add any required datapath
2960 * operations to 'ops', incrementing '*n_ops' for each new op. */
2962 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2963 struct flow_miss_op *ops, size_t *n_ops)
2965 struct facet *facet;
2968 /* The caller must ensure that miss->hmap_node.hash contains
2969 * flow_hash(miss->flow, 0). */
2970 hash = miss->hmap_node.hash;
2972 facet = facet_lookup_valid(ofproto, &miss->flow, hash);
2974 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &miss->flow);
2976 if (!flow_miss_should_make_facet(ofproto, miss, hash)) {
2977 handle_flow_miss_without_facet(miss, rule, ops, n_ops);
2981 facet = facet_create(rule, &miss->flow, hash);
2983 handle_flow_miss_with_facet(miss, facet, ops, n_ops);
2986 /* Like odp_flow_key_to_flow(), this function converts the 'key_len' bytes of
2987 * OVS_KEY_ATTR_* attributes in 'key' to a flow structure in 'flow' and returns
2988 * an ODP_FIT_* value that indicates how well 'key' fits our expectations for
2989 * what a flow key should contain.
2991 * This function also includes some logic to help make VLAN splinters
2992 * transparent to the rest of the upcall processing logic. In particular, if
2993 * the extracted in_port is a VLAN splinter port, it replaces flow->in_port by
2994 * the "real" port, sets flow->vlan_tci correctly for the VLAN of the VLAN
2995 * splinter port, and pushes a VLAN header onto 'packet' (if it is nonnull).
2997 * Sets '*initial_tci' to the VLAN TCI with which the packet was really
2998 * received, that is, the actual VLAN TCI extracted by odp_flow_key_to_flow().
2999 * (This differs from the value returned in flow->vlan_tci only for packets
3000 * received on VLAN splinters.)
3002 static enum odp_key_fitness
3003 ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto,
3004 const struct nlattr *key, size_t key_len,
3005 struct flow *flow, ovs_be16 *initial_tci,
3006 struct ofpbuf *packet)
3008 enum odp_key_fitness fitness;
3010 fitness = odp_flow_key_to_flow(key, key_len, flow);
3011 if (fitness == ODP_FIT_ERROR) {
3014 *initial_tci = flow->vlan_tci;
3016 if (vsp_adjust_flow(ofproto, flow)) {
3018 /* Make the packet resemble the flow, so that it gets sent to an
3019 * OpenFlow controller properly, so that it looks correct for
3020 * sFlow, and so that flow_extract() will get the correct vlan_tci
3021 * if it is called on 'packet'.
3023 * The allocated space inside 'packet' probably also contains
3024 * 'key', that is, both 'packet' and 'key' are probably part of a
3025 * struct dpif_upcall (see the large comment on that structure
3026 * definition), so pushing data on 'packet' is in general not a
3027 * good idea since it could overwrite 'key' or free it as a side
3028 * effect. However, it's OK in this special case because we know
3029 * that 'packet' is inside a Netlink attribute: pushing 4 bytes
3030 * will just overwrite the 4-byte "struct nlattr", which is fine
3031 * since we don't need that header anymore. */
3032 eth_push_vlan(packet, flow->vlan_tci);
3035 /* Let the caller know that we can't reproduce 'key' from 'flow'. */
3036 if (fitness == ODP_FIT_PERFECT) {
3037 fitness = ODP_FIT_TOO_MUCH;
3045 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
3048 struct dpif_upcall *upcall;
3049 struct flow_miss *miss;
3050 struct flow_miss misses[FLOW_MISS_MAX_BATCH];
3051 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
3052 struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
3062 /* Construct the to-do list.
3064 * This just amounts to extracting the flow from each packet and sticking
3065 * the packets that have the same flow in the same "flow_miss" structure so
3066 * that we can process them together. */
3069 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
3070 struct flow_miss *miss = &misses[n_misses];
3071 struct flow_miss *existing_miss;
3074 /* Obtain metadata and check userspace/kernel agreement on flow match,
3075 * then set 'flow''s header pointers. */
3076 miss->key_fitness = ofproto_dpif_extract_flow_key(
3077 ofproto, upcall->key, upcall->key_len,
3078 &miss->flow, &miss->initial_tci, upcall->packet);
3079 if (miss->key_fitness == ODP_FIT_ERROR) {
3082 flow_extract(upcall->packet, miss->flow.skb_priority,
3083 miss->flow.tun_id, miss->flow.in_port, &miss->flow);
3085 /* Add other packets to a to-do list. */
3086 hash = flow_hash(&miss->flow, 0);
3087 existing_miss = flow_miss_find(&todo, &miss->flow, hash);
3088 if (!existing_miss) {
3089 hmap_insert(&todo, &miss->hmap_node, hash);
3090 miss->key = upcall->key;
3091 miss->key_len = upcall->key_len;
3092 miss->upcall_type = upcall->type;
3093 list_init(&miss->packets);
3097 miss = existing_miss;
3099 list_push_back(&miss->packets, &upcall->packet->list_node);
3102 /* Process each element in the to-do list, constructing the set of
3103 * operations to batch. */
3105 HMAP_FOR_EACH (miss, hmap_node, &todo) {
3106 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
3108 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
3110 /* Execute batch. */
3111 for (i = 0; i < n_ops; i++) {
3112 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3114 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
3116 /* Free memory and update facets. */
3117 for (i = 0; i < n_ops; i++) {
3118 struct flow_miss_op *op = &flow_miss_ops[i];
3120 switch (op->dpif_op.type) {
3121 case DPIF_OP_EXECUTE:
3124 case DPIF_OP_FLOW_PUT:
3125 if (!op->dpif_op.error) {
3126 op->subfacet->path = subfacet_want_path(op->subfacet->slow);
3130 case DPIF_OP_FLOW_DEL:
3136 hmap_destroy(&todo);
3139 static enum { SFLOW_UPCALL, MISS_UPCALL, BAD_UPCALL }
3140 classify_upcall(const struct dpif_upcall *upcall)
3142 union user_action_cookie cookie;
3144 /* First look at the upcall type. */
3145 switch (upcall->type) {
3146 case DPIF_UC_ACTION:
3152 case DPIF_N_UC_TYPES:
3154 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3158 /* "action" upcalls need a closer look. */
3159 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
3160 switch (cookie.type) {
3161 case USER_ACTION_COOKIE_SFLOW:
3162 return SFLOW_UPCALL;
3164 case USER_ACTION_COOKIE_SLOW_PATH:
3167 case USER_ACTION_COOKIE_UNSPEC:
3169 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
3175 handle_sflow_upcall(struct ofproto_dpif *ofproto,
3176 const struct dpif_upcall *upcall)
3178 union user_action_cookie cookie;
3179 enum odp_key_fitness fitness;
3180 ovs_be16 initial_tci;
3183 fitness = ofproto_dpif_extract_flow_key(ofproto, upcall->key,
3184 upcall->key_len, &flow,
3185 &initial_tci, upcall->packet);
3186 if (fitness == ODP_FIT_ERROR) {
3190 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
3191 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
3195 handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
3197 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
3198 struct ofpbuf miss_bufs[FLOW_MISS_MAX_BATCH];
3199 uint64_t miss_buf_stubs[FLOW_MISS_MAX_BATCH][4096 / 8];
3204 assert(max_batch <= FLOW_MISS_MAX_BATCH);
3207 for (n_processed = 0; n_processed < max_batch; n_processed++) {
3208 struct dpif_upcall *upcall = &misses[n_misses];
3209 struct ofpbuf *buf = &miss_bufs[n_misses];
3212 ofpbuf_use_stub(buf, miss_buf_stubs[n_misses],
3213 sizeof miss_buf_stubs[n_misses]);
3214 error = dpif_recv(ofproto->dpif, upcall, buf);
3220 switch (classify_upcall(upcall)) {
3222 /* Handle it later. */
3227 if (ofproto->sflow) {
3228 handle_sflow_upcall(ofproto, upcall);
3239 /* Handle deferred MISS_UPCALL processing. */
3240 handle_miss_upcalls(ofproto, misses, n_misses);
3241 for (i = 0; i < n_misses; i++) {
3242 ofpbuf_uninit(&miss_bufs[i]);
3248 /* Flow expiration. */
3250 static int subfacet_max_idle(const struct ofproto_dpif *);
3251 static void update_stats(struct ofproto_dpif *);
3252 static void rule_expire(struct rule_dpif *);
3253 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
3255 /* This function is called periodically by run(). Its job is to collect
3256 * updates for the flows that have been installed into the datapath, most
3257 * importantly when they last were used, and then use that information to
3258 * expire flows that have not been used recently.
3260 * Returns the number of milliseconds after which it should be called again. */
3262 expire(struct ofproto_dpif *ofproto)
3264 struct rule_dpif *rule, *next_rule;
3265 struct oftable *table;
3268 /* Update stats for each flow in the datapath. */
3269 update_stats(ofproto);
3271 /* Expire subfacets that have been idle too long. */
3272 dp_max_idle = subfacet_max_idle(ofproto);
3273 expire_subfacets(ofproto, dp_max_idle);
3275 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
3276 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
3277 struct cls_cursor cursor;
3279 cls_cursor_init(&cursor, &table->cls, NULL);
3280 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
3285 /* All outstanding data in existing flows has been accounted, so it's a
3286 * good time to do bond rebalancing. */
3287 if (ofproto->has_bonded_bundles) {
3288 struct ofbundle *bundle;
3290 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3292 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
3297 return MIN(dp_max_idle, 1000);
3300 /* Updates flow table statistics given that the datapath just reported 'stats'
3301 * as 'subfacet''s statistics. */
3303 update_subfacet_stats(struct subfacet *subfacet,
3304 const struct dpif_flow_stats *stats)
3306 struct facet *facet = subfacet->facet;
3308 if (stats->n_packets >= subfacet->dp_packet_count) {
3309 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
3310 facet->packet_count += extra;
3312 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3315 if (stats->n_bytes >= subfacet->dp_byte_count) {
3316 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
3318 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3321 subfacet->dp_packet_count = stats->n_packets;
3322 subfacet->dp_byte_count = stats->n_bytes;
3324 facet->tcp_flags |= stats->tcp_flags;
3326 subfacet_update_time(subfacet, stats->used);
3327 if (facet->accounted_bytes < facet->byte_count) {
3329 facet_account(facet);
3330 facet->accounted_bytes = facet->byte_count;
3332 facet_push_stats(facet);
3335 /* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
3336 * about, or a flow that shouldn't be installed but was anyway. Delete it. */
3338 delete_unexpected_flow(struct dpif *dpif,
3339 const struct nlattr *key, size_t key_len)
3341 if (!VLOG_DROP_WARN(&rl)) {
3345 odp_flow_key_format(key, key_len, &s);
3346 VLOG_WARN("unexpected flow from datapath %s", ds_cstr(&s));
3350 COVERAGE_INC(facet_unexpected);
3351 dpif_flow_del(dpif, key, key_len, NULL);
3354 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3356 * This function also pushes statistics updates to rules which each facet
3357 * resubmits into. Generally these statistics will be accurate. However, if a
3358 * facet changes the rule it resubmits into at some time in between
3359 * update_stats() runs, it is possible that statistics accrued to the
3360 * old rule will be incorrectly attributed to the new rule. This could be
3361 * avoided by calling update_stats() whenever rules are created or
3362 * deleted. However, the performance impact of making so many calls to the
3363 * datapath do not justify the benefit of having perfectly accurate statistics.
3366 update_stats(struct ofproto_dpif *p)
3368 const struct dpif_flow_stats *stats;
3369 struct dpif_flow_dump dump;
3370 const struct nlattr *key;
3373 dpif_flow_dump_start(&dump, p->dpif);
3374 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
3375 struct subfacet *subfacet;
3377 subfacet = subfacet_find(p, key, key_len);
3378 switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
3380 update_subfacet_stats(subfacet, stats);
3384 /* Stats are updated per-packet. */
3387 case SF_NOT_INSTALLED:
3389 delete_unexpected_flow(p->dpif, key, key_len);
3393 dpif_flow_dump_done(&dump);
3396 /* Calculates and returns the number of milliseconds of idle time after which
3397 * subfacets should expire from the datapath. When a subfacet expires, we fold
3398 * its statistics into its facet, and when a facet's last subfacet expires, we
3399 * fold its statistic into its rule. */
3401 subfacet_max_idle(const struct ofproto_dpif *ofproto)
3404 * Idle time histogram.
3406 * Most of the time a switch has a relatively small number of subfacets.
3407 * When this is the case we might as well keep statistics for all of them
3408 * in userspace and to cache them in the kernel datapath for performance as
3411 * As the number of subfacets increases, the memory required to maintain
3412 * statistics about them in userspace and in the kernel becomes
3413 * significant. However, with a large number of subfacets it is likely
3414 * that only a few of them are "heavy hitters" that consume a large amount
3415 * of bandwidth. At this point, only heavy hitters are worth caching in
3416 * the kernel and maintaining in userspaces; other subfacets we can
3419 * The technique used to compute the idle time is to build a histogram with
3420 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
3421 * that is installed in the kernel gets dropped in the appropriate bucket.
3422 * After the histogram has been built, we compute the cutoff so that only
3423 * the most-recently-used 1% of subfacets (but at least
3424 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
3425 * the most-recently-used bucket of subfacets is kept, so actually an
3426 * arbitrary number of subfacets can be kept in any given expiration run
3427 * (though the next run will delete most of those unless they receive
3430 * This requires a second pass through the subfacets, in addition to the
3431 * pass made by update_stats(), because the former function never looks at
3432 * uninstallable subfacets.
3434 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
3435 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3436 int buckets[N_BUCKETS] = { 0 };
3437 int total, subtotal, bucket;
3438 struct subfacet *subfacet;
3442 total = hmap_count(&ofproto->subfacets);
3443 if (total <= ofproto->up.flow_eviction_threshold) {
3444 return N_BUCKETS * BUCKET_WIDTH;
3447 /* Build histogram. */
3449 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
3450 long long int idle = now - subfacet->used;
3451 int bucket = (idle <= 0 ? 0
3452 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
3453 : (unsigned int) idle / BUCKET_WIDTH);
3457 /* Find the first bucket whose flows should be expired. */
3458 subtotal = bucket = 0;
3460 subtotal += buckets[bucket++];
3461 } while (bucket < N_BUCKETS &&
3462 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
3464 if (VLOG_IS_DBG_ENABLED()) {
3468 ds_put_cstr(&s, "keep");
3469 for (i = 0; i < N_BUCKETS; i++) {
3471 ds_put_cstr(&s, ", drop");
3474 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
3477 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
3481 return bucket * BUCKET_WIDTH;
3484 enum { EXPIRE_MAX_BATCH = 50 };
3487 expire_batch(struct ofproto_dpif *ofproto, struct subfacet **subfacets, int n)
3489 struct odputil_keybuf keybufs[EXPIRE_MAX_BATCH];
3490 struct dpif_op ops[EXPIRE_MAX_BATCH];
3491 struct dpif_op *opsp[EXPIRE_MAX_BATCH];
3492 struct ofpbuf keys[EXPIRE_MAX_BATCH];
3493 struct dpif_flow_stats stats[EXPIRE_MAX_BATCH];
3496 for (i = 0; i < n; i++) {
3497 ops[i].type = DPIF_OP_FLOW_DEL;
3498 subfacet_get_key(subfacets[i], &keybufs[i], &keys[i]);
3499 ops[i].u.flow_del.key = keys[i].data;
3500 ops[i].u.flow_del.key_len = keys[i].size;
3501 ops[i].u.flow_del.stats = &stats[i];
3505 dpif_operate(ofproto->dpif, opsp, n);
3506 for (i = 0; i < n; i++) {
3507 subfacet_reset_dp_stats(subfacets[i], &stats[i]);
3508 subfacets[i]->path = SF_NOT_INSTALLED;
3509 subfacet_destroy(subfacets[i]);
3514 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
3516 /* Cutoff time for most flows. */
3517 long long int normal_cutoff = time_msec() - dp_max_idle;
3519 /* We really want to keep flows for special protocols around, so use a more
3520 * conservative cutoff. */
3521 long long int special_cutoff = time_msec() - 10000;
3523 struct subfacet *subfacet, *next_subfacet;
3524 struct subfacet *batch[EXPIRE_MAX_BATCH];
3528 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
3529 &ofproto->subfacets) {
3530 long long int cutoff;
3532 cutoff = (subfacet->slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)
3535 if (subfacet->used < cutoff) {
3536 if (subfacet->path != SF_NOT_INSTALLED) {
3537 batch[n_batch++] = subfacet;
3538 if (n_batch >= EXPIRE_MAX_BATCH) {
3539 expire_batch(ofproto, batch, n_batch);
3543 subfacet_destroy(subfacet);
3549 expire_batch(ofproto, batch, n_batch);
3553 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3554 * then delete it entirely. */
3556 rule_expire(struct rule_dpif *rule)
3558 struct facet *facet, *next_facet;
3562 /* Has 'rule' expired? */
3564 if (rule->up.hard_timeout
3565 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
3566 reason = OFPRR_HARD_TIMEOUT;
3567 } else if (rule->up.idle_timeout
3568 && now > rule->up.used + rule->up.idle_timeout * 1000) {
3569 reason = OFPRR_IDLE_TIMEOUT;
3574 COVERAGE_INC(ofproto_dpif_expired);
3576 /* Update stats. (This is a no-op if the rule expired due to an idle
3577 * timeout, because that only happens when the rule has no facets left.) */
3578 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3579 facet_remove(facet);
3582 /* Get rid of the rule. */
3583 ofproto_rule_expire(&rule->up, reason);
3588 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
3590 * The caller must already have determined that no facet with an identical
3591 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
3592 * the ofproto's classifier table.
3594 * 'hash' must be the return value of flow_hash(flow, 0).
3596 * The facet will initially have no subfacets. The caller should create (at
3597 * least) one subfacet with subfacet_create(). */
3598 static struct facet *
3599 facet_create(struct rule_dpif *rule, const struct flow *flow, uint32_t hash)
3601 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3602 struct facet *facet;
3604 facet = xzalloc(sizeof *facet);
3605 facet->used = time_msec();
3606 hmap_insert(&ofproto->facets, &facet->hmap_node, hash);
3607 list_push_back(&rule->facets, &facet->list_node);
3609 facet->flow = *flow;
3610 list_init(&facet->subfacets);
3611 netflow_flow_init(&facet->nf_flow);
3612 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
3618 facet_free(struct facet *facet)
3623 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
3624 * 'packet', which arrived on 'in_port'.
3626 * Takes ownership of 'packet'. */
3628 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
3629 const struct nlattr *odp_actions, size_t actions_len,
3630 struct ofpbuf *packet)
3632 struct odputil_keybuf keybuf;
3636 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3637 odp_flow_key_from_flow(&key, flow);
3639 error = dpif_execute(ofproto->dpif, key.data, key.size,
3640 odp_actions, actions_len, packet);
3642 ofpbuf_delete(packet);
3646 /* Remove 'facet' from 'ofproto' and free up the associated memory:
3648 * - If 'facet' was installed in the datapath, uninstalls it and updates its
3649 * rule's statistics, via subfacet_uninstall().
3651 * - Removes 'facet' from its rule and from ofproto->facets.
3654 facet_remove(struct facet *facet)
3656 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3657 struct subfacet *subfacet, *next_subfacet;
3659 assert(!list_is_empty(&facet->subfacets));
3661 /* First uninstall all of the subfacets to get final statistics. */
3662 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3663 subfacet_uninstall(subfacet);
3666 /* Flush the final stats to the rule.
3668 * This might require us to have at least one subfacet around so that we
3669 * can use its actions for accounting in facet_account(), which is why we
3670 * have uninstalled but not yet destroyed the subfacets. */
3671 facet_flush_stats(facet);
3673 /* Now we're really all done so destroy everything. */
3674 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
3675 &facet->subfacets) {
3676 subfacet_destroy__(subfacet);
3678 hmap_remove(&ofproto->facets, &facet->hmap_node);
3679 list_remove(&facet->list_node);
3683 /* Feed information from 'facet' back into the learning table to keep it in
3684 * sync with what is actually flowing through the datapath. */
3686 facet_learn(struct facet *facet)
3688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3689 struct action_xlate_ctx ctx;
3691 if (!facet->has_learn
3692 && !facet->has_normal
3693 && (!facet->has_fin_timeout
3694 || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
3698 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3699 facet->flow.vlan_tci,
3700 facet->rule, facet->tcp_flags, NULL);
3701 ctx.may_learn = true;
3702 xlate_actions_for_side_effects(&ctx, facet->rule->up.actions,
3703 facet->rule->up.n_actions);
3707 facet_account(struct facet *facet)
3709 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3710 struct subfacet *subfacet;
3711 const struct nlattr *a;
3716 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
3719 n_bytes = facet->byte_count - facet->accounted_bytes;
3721 /* This loop feeds byte counters to bond_account() for rebalancing to use
3722 * as a basis. We also need to track the actual VLAN on which the packet
3723 * is going to be sent to ensure that it matches the one passed to
3724 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
3727 * We use the actions from an arbitrary subfacet because they should all
3728 * be equally valid for our purpose. */
3729 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3730 struct subfacet, list_node);
3731 vlan_tci = facet->flow.vlan_tci;
3732 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3733 subfacet->actions, subfacet->actions_len) {
3734 const struct ovs_action_push_vlan *vlan;
3735 struct ofport_dpif *port;
3737 switch (nl_attr_type(a)) {
3738 case OVS_ACTION_ATTR_OUTPUT:
3739 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3740 if (port && port->bundle && port->bundle->bond) {
3741 bond_account(port->bundle->bond, &facet->flow,
3742 vlan_tci_to_vid(vlan_tci), n_bytes);
3746 case OVS_ACTION_ATTR_POP_VLAN:
3747 vlan_tci = htons(0);
3750 case OVS_ACTION_ATTR_PUSH_VLAN:
3751 vlan = nl_attr_get(a);
3752 vlan_tci = vlan->vlan_tci;
3758 /* Returns true if the only action for 'facet' is to send to the controller.
3759 * (We don't report NetFlow expiration messages for such facets because they
3760 * are just part of the control logic for the network, not real traffic). */
3762 facet_is_controller_flow(struct facet *facet)
3765 && facet->rule->up.n_actions == 1
3766 && action_outputs_to_port(&facet->rule->up.actions[0],
3767 htons(OFPP_CONTROLLER)));
3770 /* Folds all of 'facet''s statistics into its rule. Also updates the
3771 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3772 * 'facet''s statistics in the datapath should have been zeroed and folded into
3773 * its packet and byte counts before this function is called. */
3775 facet_flush_stats(struct facet *facet)
3777 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3778 struct subfacet *subfacet;
3780 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3781 assert(!subfacet->dp_byte_count);
3782 assert(!subfacet->dp_packet_count);
3785 facet_push_stats(facet);
3786 if (facet->accounted_bytes < facet->byte_count) {
3787 facet_account(facet);
3788 facet->accounted_bytes = facet->byte_count;
3791 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3792 struct ofexpired expired;
3793 expired.flow = facet->flow;
3794 expired.packet_count = facet->packet_count;
3795 expired.byte_count = facet->byte_count;
3796 expired.used = facet->used;
3797 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3800 facet->rule->packet_count += facet->packet_count;
3801 facet->rule->byte_count += facet->byte_count;
3803 /* Reset counters to prevent double counting if 'facet' ever gets
3805 facet_reset_counters(facet);
3807 netflow_flow_clear(&facet->nf_flow);
3808 facet->tcp_flags = 0;
3811 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3812 * Returns it if found, otherwise a null pointer.
3814 * 'hash' must be the return value of flow_hash(flow, 0).
3816 * The returned facet might need revalidation; use facet_lookup_valid()
3817 * instead if that is important. */
3818 static struct facet *
3819 facet_find(struct ofproto_dpif *ofproto,
3820 const struct flow *flow, uint32_t hash)
3822 struct facet *facet;
3824 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, hash, &ofproto->facets) {
3825 if (flow_equal(flow, &facet->flow)) {
3833 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3834 * Returns it if found, otherwise a null pointer.
3836 * 'hash' must be the return value of flow_hash(flow, 0).
3838 * The returned facet is guaranteed to be valid. */
3839 static struct facet *
3840 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow,
3843 struct facet *facet;
3845 facet = facet_find(ofproto, flow, hash);
3847 && (ofproto->need_revalidate
3848 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))) {
3849 facet_revalidate(facet);
3856 subfacet_path_to_string(enum subfacet_path path)
3859 case SF_NOT_INSTALLED:
3860 return "not installed";
3862 return "in fast path";
3864 return "in slow path";
3870 /* Returns the path in which a subfacet should be installed if its 'slow'
3871 * member has the specified value. */
3872 static enum subfacet_path
3873 subfacet_want_path(enum slow_path_reason slow)
3875 return slow ? SF_SLOW_PATH : SF_FAST_PATH;
3878 /* Returns true if 'subfacet' needs to have its datapath flow updated,
3879 * supposing that its actions have been recalculated as 'want_actions' and that
3880 * 'slow' is nonzero iff 'subfacet' should be in the slow path. */
3882 subfacet_should_install(struct subfacet *subfacet, enum slow_path_reason slow,
3883 const struct ofpbuf *want_actions)
3885 enum subfacet_path want_path = subfacet_want_path(slow);
3886 return (want_path != subfacet->path
3887 || (want_path == SF_FAST_PATH
3888 && (subfacet->actions_len != want_actions->size
3889 || memcmp(subfacet->actions, want_actions->data,
3890 subfacet->actions_len))));
3894 facet_check_consistency(struct facet *facet)
3896 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3898 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3900 uint64_t odp_actions_stub[1024 / 8];
3901 struct ofpbuf odp_actions;
3903 struct rule_dpif *rule;
3904 struct subfacet *subfacet;
3905 bool may_log = false;
3908 /* Check the rule for consistency. */
3909 rule = rule_dpif_lookup(ofproto, &facet->flow);
3910 ok = rule == facet->rule;
3912 may_log = !VLOG_DROP_WARN(&rl);
3917 flow_format(&s, &facet->flow);
3918 ds_put_format(&s, ": facet associated with wrong rule (was "
3919 "table=%"PRIu8",", facet->rule->up.table_id);
3920 cls_rule_format(&facet->rule->up.cr, &s);
3921 ds_put_format(&s, ") (should have been table=%"PRIu8",",
3923 cls_rule_format(&rule->up.cr, &s);
3924 ds_put_char(&s, ')');
3926 VLOG_WARN("%s", ds_cstr(&s));
3931 /* Check the datapath actions for consistency. */
3932 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
3933 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3934 enum subfacet_path want_path;
3935 struct odputil_keybuf keybuf;
3936 struct action_xlate_ctx ctx;
3940 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3941 subfacet->initial_tci, rule, 0, NULL);
3942 xlate_actions(&ctx, rule->up.actions, rule->up.n_actions,
3945 if (subfacet->path == SF_NOT_INSTALLED) {
3946 /* This only happens if the datapath reported an error when we
3947 * tried to install the flow. Don't flag another error here. */
3951 want_path = subfacet_want_path(subfacet->slow);
3952 if (want_path == SF_SLOW_PATH && subfacet->path == SF_SLOW_PATH) {
3953 /* The actions for slow-path flows may legitimately vary from one
3954 * packet to the next. We're done. */
3958 if (!subfacet_should_install(subfacet, subfacet->slow, &odp_actions)) {
3962 /* Inconsistency! */
3964 may_log = !VLOG_DROP_WARN(&rl);
3968 /* Rate-limited, skip reporting. */
3973 subfacet_get_key(subfacet, &keybuf, &key);
3974 odp_flow_key_format(key.data, key.size, &s);
3976 ds_put_cstr(&s, ": inconsistency in subfacet");
3977 if (want_path != subfacet->path) {
3978 enum odp_key_fitness fitness = subfacet->key_fitness;
3980 ds_put_format(&s, " (%s, fitness=%s)",
3981 subfacet_path_to_string(subfacet->path),
3982 odp_key_fitness_to_string(fitness));
3983 ds_put_format(&s, " (should have been %s)",
3984 subfacet_path_to_string(want_path));
3985 } else if (want_path == SF_FAST_PATH) {
3986 ds_put_cstr(&s, " (actions were: ");
3987 format_odp_actions(&s, subfacet->actions,
3988 subfacet->actions_len);
3989 ds_put_cstr(&s, ") (correct actions: ");
3990 format_odp_actions(&s, odp_actions.data, odp_actions.size);
3991 ds_put_char(&s, ')');
3993 ds_put_cstr(&s, " (actions: ");
3994 format_odp_actions(&s, subfacet->actions,
3995 subfacet->actions_len);
3996 ds_put_char(&s, ')');
3998 VLOG_WARN("%s", ds_cstr(&s));
4001 ofpbuf_uninit(&odp_actions);
4006 /* Re-searches the classifier for 'facet':
4008 * - If the rule found is different from 'facet''s current rule, moves
4009 * 'facet' to the new rule and recompiles its actions.
4011 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
4012 * where it is and recompiles its actions anyway. */
4014 facet_revalidate(struct facet *facet)
4016 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4018 struct nlattr *odp_actions;
4021 struct actions *new_actions;
4023 struct action_xlate_ctx ctx;
4024 uint64_t odp_actions_stub[1024 / 8];
4025 struct ofpbuf odp_actions;
4027 struct rule_dpif *new_rule;
4028 struct subfacet *subfacet;
4031 COVERAGE_INC(facet_revalidate);
4033 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
4035 /* Calculate new datapath actions.
4037 * We do not modify any 'facet' state yet, because we might need to, e.g.,
4038 * emit a NetFlow expiration and, if so, we need to have the old state
4039 * around to properly compose it. */
4041 /* If the datapath actions changed or the installability changed,
4042 * then we need to talk to the datapath. */
4045 memset(&ctx, 0, sizeof ctx);
4046 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4047 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4048 enum slow_path_reason slow;
4050 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
4051 subfacet->initial_tci, new_rule, 0, NULL);
4052 xlate_actions(&ctx, new_rule->up.actions, new_rule->up.n_actions,
4055 slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4056 if (subfacet_should_install(subfacet, slow, &odp_actions)) {
4057 struct dpif_flow_stats stats;
4059 subfacet_install(subfacet,
4060 odp_actions.data, odp_actions.size, &stats, slow);
4061 subfacet_update_stats(subfacet, &stats);
4064 new_actions = xcalloc(list_size(&facet->subfacets),
4065 sizeof *new_actions);
4067 new_actions[i].odp_actions = xmemdup(odp_actions.data,
4069 new_actions[i].actions_len = odp_actions.size;
4074 ofpbuf_uninit(&odp_actions);
4077 facet_flush_stats(facet);
4080 /* Update 'facet' now that we've taken care of all the old state. */
4081 facet->tags = ctx.tags;
4082 facet->nf_flow.output_iface = ctx.nf_output_iface;
4083 facet->has_learn = ctx.has_learn;
4084 facet->has_normal = ctx.has_normal;
4085 facet->has_fin_timeout = ctx.has_fin_timeout;
4086 facet->mirrors = ctx.mirrors;
4089 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4090 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4092 if (new_actions && new_actions[i].odp_actions) {
4093 free(subfacet->actions);
4094 subfacet->actions = new_actions[i].odp_actions;
4095 subfacet->actions_len = new_actions[i].actions_len;
4101 if (facet->rule != new_rule) {
4102 COVERAGE_INC(facet_changed_rule);
4103 list_remove(&facet->list_node);
4104 list_push_back(&new_rule->facets, &facet->list_node);
4105 facet->rule = new_rule;
4106 facet->used = new_rule->up.created;
4107 facet->prev_used = facet->used;
4111 /* Updates 'facet''s used time. Caller is responsible for calling
4112 * facet_push_stats() to update the flows which 'facet' resubmits into. */
4114 facet_update_time(struct facet *facet, long long int used)
4116 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4117 if (used > facet->used) {
4119 ofproto_rule_update_used(&facet->rule->up, used);
4120 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
4125 facet_reset_counters(struct facet *facet)
4127 facet->packet_count = 0;
4128 facet->byte_count = 0;
4129 facet->prev_packet_count = 0;
4130 facet->prev_byte_count = 0;
4131 facet->accounted_bytes = 0;
4135 facet_push_stats(struct facet *facet)
4137 struct dpif_flow_stats stats;
4139 assert(facet->packet_count >= facet->prev_packet_count);
4140 assert(facet->byte_count >= facet->prev_byte_count);
4141 assert(facet->used >= facet->prev_used);
4143 stats.n_packets = facet->packet_count - facet->prev_packet_count;
4144 stats.n_bytes = facet->byte_count - facet->prev_byte_count;
4145 stats.used = facet->used;
4146 stats.tcp_flags = 0;
4148 if (stats.n_packets || stats.n_bytes || facet->used > facet->prev_used) {
4149 facet->prev_packet_count = facet->packet_count;
4150 facet->prev_byte_count = facet->byte_count;
4151 facet->prev_used = facet->used;
4153 flow_push_stats(facet->rule, &facet->flow, &stats);
4155 update_mirror_stats(ofproto_dpif_cast(facet->rule->up.ofproto),
4156 facet->mirrors, stats.n_packets, stats.n_bytes);
4161 rule_credit_stats(struct rule_dpif *rule, const struct dpif_flow_stats *stats)
4163 rule->packet_count += stats->n_packets;
4164 rule->byte_count += stats->n_bytes;
4165 ofproto_rule_update_used(&rule->up, stats->used);
4168 /* Pushes flow statistics to the rules which 'flow' resubmits into given
4169 * 'rule''s actions and mirrors. */
4171 flow_push_stats(struct rule_dpif *rule,
4172 const struct flow *flow, const struct dpif_flow_stats *stats)
4174 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4175 struct action_xlate_ctx ctx;
4177 ofproto_rule_update_used(&rule->up, stats->used);
4179 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, rule,
4181 ctx.resubmit_stats = stats;
4182 xlate_actions_for_side_effects(&ctx, rule->up.actions, rule->up.n_actions);
4187 static struct subfacet *
4188 subfacet_find__(struct ofproto_dpif *ofproto,
4189 const struct nlattr *key, size_t key_len, uint32_t key_hash,
4190 const struct flow *flow)
4192 struct subfacet *subfacet;
4194 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
4195 &ofproto->subfacets) {
4197 ? (subfacet->key_len == key_len
4198 && !memcmp(key, subfacet->key, key_len))
4199 : flow_equal(flow, &subfacet->facet->flow)) {
4207 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
4208 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
4209 * there is one, otherwise creates and returns a new subfacet.
4211 * If the returned subfacet is new, then subfacet->actions will be NULL, in
4212 * which case the caller must populate the actions with
4213 * subfacet_make_actions(). */
4214 static struct subfacet *
4215 subfacet_create(struct facet *facet, enum odp_key_fitness key_fitness,
4216 const struct nlattr *key, size_t key_len, ovs_be16 initial_tci)
4218 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4219 uint32_t key_hash = odp_flow_key_hash(key, key_len);
4220 struct subfacet *subfacet;
4222 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
4224 if (subfacet->facet == facet) {
4228 /* This shouldn't happen. */
4229 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
4230 subfacet_destroy(subfacet);
4233 subfacet = (list_is_empty(&facet->subfacets)
4234 ? &facet->one_subfacet
4235 : xmalloc(sizeof *subfacet));
4236 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
4237 list_push_back(&facet->subfacets, &subfacet->list_node);
4238 subfacet->facet = facet;
4239 subfacet->key_fitness = key_fitness;
4240 if (key_fitness != ODP_FIT_PERFECT) {
4241 subfacet->key = xmemdup(key, key_len);
4242 subfacet->key_len = key_len;
4244 subfacet->key = NULL;
4245 subfacet->key_len = 0;
4247 subfacet->used = time_msec();
4248 subfacet->dp_packet_count = 0;
4249 subfacet->dp_byte_count = 0;
4250 subfacet->actions_len = 0;
4251 subfacet->actions = NULL;
4252 subfacet->slow = (subfacet->key_fitness == ODP_FIT_TOO_LITTLE
4255 subfacet->path = SF_NOT_INSTALLED;
4256 subfacet->initial_tci = initial_tci;
4261 /* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
4262 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
4263 static struct subfacet *
4264 subfacet_find(struct ofproto_dpif *ofproto,
4265 const struct nlattr *key, size_t key_len)
4267 uint32_t key_hash = odp_flow_key_hash(key, key_len);
4268 enum odp_key_fitness fitness;
4271 fitness = odp_flow_key_to_flow(key, key_len, &flow);
4272 if (fitness == ODP_FIT_ERROR) {
4276 return subfacet_find__(ofproto, key, key_len, key_hash, &flow);
4279 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
4280 * its facet within 'ofproto', and frees it. */
4282 subfacet_destroy__(struct subfacet *subfacet)
4284 struct facet *facet = subfacet->facet;
4285 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4287 subfacet_uninstall(subfacet);
4288 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
4289 list_remove(&subfacet->list_node);
4290 free(subfacet->key);
4291 free(subfacet->actions);
4292 if (subfacet != &facet->one_subfacet) {
4297 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
4298 * last remaining subfacet in its facet destroys the facet too. */
4300 subfacet_destroy(struct subfacet *subfacet)
4302 struct facet *facet = subfacet->facet;
4304 if (list_is_singleton(&facet->subfacets)) {
4305 /* facet_remove() needs at least one subfacet (it will remove it). */
4306 facet_remove(facet);
4308 subfacet_destroy__(subfacet);
4312 /* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
4313 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
4314 * for use as temporary storage. */
4316 subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
4319 if (!subfacet->key) {
4320 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
4321 odp_flow_key_from_flow(key, &subfacet->facet->flow);
4323 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
4327 /* Composes the datapath actions for 'subfacet' based on its rule's actions.
4328 * Translates the actions into 'odp_actions', which the caller must have
4329 * initialized and is responsible for uninitializing. */
4331 subfacet_make_actions(struct subfacet *subfacet, const struct ofpbuf *packet,
4332 struct ofpbuf *odp_actions)
4334 struct facet *facet = subfacet->facet;
4335 struct rule_dpif *rule = facet->rule;
4336 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4338 struct action_xlate_ctx ctx;
4340 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, subfacet->initial_tci,
4342 xlate_actions(&ctx, rule->up.actions, rule->up.n_actions, odp_actions);
4343 facet->tags = ctx.tags;
4344 facet->has_learn = ctx.has_learn;
4345 facet->has_normal = ctx.has_normal;
4346 facet->has_fin_timeout = ctx.has_fin_timeout;
4347 facet->nf_flow.output_iface = ctx.nf_output_iface;
4348 facet->mirrors = ctx.mirrors;
4350 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4351 if (subfacet->actions_len != odp_actions->size
4352 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
4353 free(subfacet->actions);
4354 subfacet->actions_len = odp_actions->size;
4355 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
4359 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
4360 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
4361 * in the datapath will be zeroed and 'stats' will be updated with traffic new
4362 * since 'subfacet' was last updated.
4364 * Returns 0 if successful, otherwise a positive errno value. */
4366 subfacet_install(struct subfacet *subfacet,
4367 const struct nlattr *actions, size_t actions_len,
4368 struct dpif_flow_stats *stats,
4369 enum slow_path_reason slow)
4371 struct facet *facet = subfacet->facet;
4372 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4373 enum subfacet_path path = subfacet_want_path(slow);
4374 uint64_t slow_path_stub[128 / 8];
4375 struct odputil_keybuf keybuf;
4376 enum dpif_flow_put_flags flags;
4380 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
4382 flags |= DPIF_FP_ZERO_STATS;
4385 if (path == SF_SLOW_PATH) {
4386 compose_slow_path(ofproto, &facet->flow, slow,
4387 slow_path_stub, sizeof slow_path_stub,
4388 &actions, &actions_len);
4391 subfacet_get_key(subfacet, &keybuf, &key);
4392 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
4393 actions, actions_len, stats);
4396 subfacet_reset_dp_stats(subfacet, stats);
4400 subfacet->path = path;
4406 subfacet_reinstall(struct subfacet *subfacet, struct dpif_flow_stats *stats)
4408 return subfacet_install(subfacet, subfacet->actions, subfacet->actions_len,
4409 stats, subfacet->slow);
4412 /* If 'subfacet' is installed in the datapath, uninstalls it. */
4414 subfacet_uninstall(struct subfacet *subfacet)
4416 if (subfacet->path != SF_NOT_INSTALLED) {
4417 struct rule_dpif *rule = subfacet->facet->rule;
4418 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4419 struct odputil_keybuf keybuf;
4420 struct dpif_flow_stats stats;
4424 subfacet_get_key(subfacet, &keybuf, &key);
4425 error = dpif_flow_del(ofproto->dpif, key.data, key.size, &stats);
4426 subfacet_reset_dp_stats(subfacet, &stats);
4428 subfacet_update_stats(subfacet, &stats);
4430 subfacet->path = SF_NOT_INSTALLED;
4432 assert(subfacet->dp_packet_count == 0);
4433 assert(subfacet->dp_byte_count == 0);
4437 /* Resets 'subfacet''s datapath statistics counters. This should be called
4438 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
4439 * non-null, it should contain the statistics returned by dpif when 'subfacet'
4440 * was reset in the datapath. 'stats' will be modified to include only
4441 * statistics new since 'subfacet' was last updated. */
4443 subfacet_reset_dp_stats(struct subfacet *subfacet,
4444 struct dpif_flow_stats *stats)
4447 && subfacet->dp_packet_count <= stats->n_packets
4448 && subfacet->dp_byte_count <= stats->n_bytes) {
4449 stats->n_packets -= subfacet->dp_packet_count;
4450 stats->n_bytes -= subfacet->dp_byte_count;
4453 subfacet->dp_packet_count = 0;
4454 subfacet->dp_byte_count = 0;
4457 /* Updates 'subfacet''s used time. The caller is responsible for calling
4458 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
4460 subfacet_update_time(struct subfacet *subfacet, long long int used)
4462 if (used > subfacet->used) {
4463 subfacet->used = used;
4464 facet_update_time(subfacet->facet, used);
4468 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
4470 * Because of the meaning of a subfacet's counters, it only makes sense to do
4471 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
4472 * represents a packet that was sent by hand or if it represents statistics
4473 * that have been cleared out of the datapath. */
4475 subfacet_update_stats(struct subfacet *subfacet,
4476 const struct dpif_flow_stats *stats)
4478 if (stats->n_packets || stats->used > subfacet->used) {
4479 struct facet *facet = subfacet->facet;
4481 subfacet_update_time(subfacet, stats->used);
4482 facet->packet_count += stats->n_packets;
4483 facet->byte_count += stats->n_bytes;
4484 facet->tcp_flags |= stats->tcp_flags;
4485 facet_push_stats(facet);
4486 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
4492 static struct rule_dpif *
4493 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
4495 struct ofport_dpif *port;
4496 struct rule_dpif *rule;
4498 rule = rule_dpif_lookup__(ofproto, flow, 0);
4503 port = get_ofp_port(ofproto, flow->in_port);
4505 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, flow->in_port);
4506 return ofproto->miss_rule;
4509 if (port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN) {
4510 return ofproto->no_packet_in_rule;
4512 return ofproto->miss_rule;
4515 static struct rule_dpif *
4516 rule_dpif_lookup__(struct ofproto_dpif *ofproto, const struct flow *flow,
4519 struct cls_rule *cls_rule;
4520 struct classifier *cls;
4522 if (table_id >= N_TABLES) {
4526 cls = &ofproto->up.tables[table_id].cls;
4527 if (flow->nw_frag & FLOW_NW_FRAG_ANY
4528 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
4529 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
4530 * are unavailable. */
4531 struct flow ofpc_normal_flow = *flow;
4532 ofpc_normal_flow.tp_src = htons(0);
4533 ofpc_normal_flow.tp_dst = htons(0);
4534 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
4536 cls_rule = classifier_lookup(cls, flow);
4538 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
4542 complete_operation(struct rule_dpif *rule)
4544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4546 rule_invalidate(rule);
4548 struct dpif_completion *c = xmalloc(sizeof *c);
4549 c->op = rule->up.pending;
4550 list_push_back(&ofproto->completions, &c->list_node);
4552 ofoperation_complete(rule->up.pending, 0);
4556 static struct rule *
4559 struct rule_dpif *rule = xmalloc(sizeof *rule);
4564 rule_dealloc(struct rule *rule_)
4566 struct rule_dpif *rule = rule_dpif_cast(rule_);
4571 rule_construct(struct rule *rule_)
4573 struct rule_dpif *rule = rule_dpif_cast(rule_);
4574 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4575 struct rule_dpif *victim;
4579 error = validate_actions(rule->up.actions, rule->up.n_actions,
4580 &rule->up.cr.flow, ofproto->max_ports);
4585 rule->packet_count = 0;
4586 rule->byte_count = 0;
4588 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
4589 if (victim && !list_is_empty(&victim->facets)) {
4590 struct facet *facet;
4592 rule->facets = victim->facets;
4593 list_moved(&rule->facets);
4594 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4595 /* XXX: We're only clearing our local counters here. It's possible
4596 * that quite a few packets are unaccounted for in the datapath
4597 * statistics. These will be accounted to the new rule instead of
4598 * cleared as required. This could be fixed by clearing out the
4599 * datapath statistics for this facet, but currently it doesn't
4601 facet_reset_counters(facet);
4605 /* Must avoid list_moved() in this case. */
4606 list_init(&rule->facets);
4609 table_id = rule->up.table_id;
4610 rule->tag = (victim ? victim->tag
4612 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
4613 ofproto->tables[table_id].basis));
4615 complete_operation(rule);
4620 rule_destruct(struct rule *rule_)
4622 struct rule_dpif *rule = rule_dpif_cast(rule_);
4623 struct facet *facet, *next_facet;
4625 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4626 facet_revalidate(facet);
4629 complete_operation(rule);
4633 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
4635 struct rule_dpif *rule = rule_dpif_cast(rule_);
4636 struct facet *facet;
4638 /* Start from historical data for 'rule' itself that are no longer tracked
4639 * in facets. This counts, for example, facets that have expired. */
4640 *packets = rule->packet_count;
4641 *bytes = rule->byte_count;
4643 /* Add any statistics that are tracked by facets. This includes
4644 * statistical data recently updated by ofproto_update_stats() as well as
4645 * stats for packets that were executed "by hand" via dpif_execute(). */
4646 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4647 *packets += facet->packet_count;
4648 *bytes += facet->byte_count;
4653 rule_execute(struct rule *rule_, const struct flow *flow,
4654 struct ofpbuf *packet)
4656 struct rule_dpif *rule = rule_dpif_cast(rule_);
4657 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4659 struct dpif_flow_stats stats;
4661 struct action_xlate_ctx ctx;
4662 uint64_t odp_actions_stub[1024 / 8];
4663 struct ofpbuf odp_actions;
4665 dpif_flow_stats_extract(flow, packet, &stats);
4666 rule_credit_stats(rule, &stats);
4668 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4669 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci,
4670 rule, stats.tcp_flags, packet);
4671 ctx.resubmit_stats = &stats;
4672 xlate_actions(&ctx, rule->up.actions, rule->up.n_actions, &odp_actions);
4674 execute_odp_actions(ofproto, flow, odp_actions.data,
4675 odp_actions.size, packet);
4677 ofpbuf_uninit(&odp_actions);
4683 rule_modify_actions(struct rule *rule_)
4685 struct rule_dpif *rule = rule_dpif_cast(rule_);
4686 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4689 error = validate_actions(rule->up.actions, rule->up.n_actions,
4690 &rule->up.cr.flow, ofproto->max_ports);
4692 ofoperation_complete(rule->up.pending, error);
4696 complete_operation(rule);
4699 /* Sends 'packet' out 'ofport'.
4700 * May modify 'packet'.
4701 * Returns 0 if successful, otherwise a positive errno value. */
4703 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4705 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4706 struct ofpbuf key, odp_actions;
4707 struct odputil_keybuf keybuf;
4712 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
4713 odp_port = vsp_realdev_to_vlandev(ofproto, ofport->odp_port,
4715 if (odp_port != ofport->odp_port) {
4716 eth_pop_vlan(packet);
4717 flow.vlan_tci = htons(0);
4720 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4721 odp_flow_key_from_flow(&key, &flow);
4723 ofpbuf_init(&odp_actions, 32);
4724 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
4726 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
4727 error = dpif_execute(ofproto->dpif,
4729 odp_actions.data, odp_actions.size,
4731 ofpbuf_uninit(&odp_actions);
4734 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
4735 ofproto->up.name, odp_port, strerror(error));
4737 ofproto_update_local_port_stats(ofport->up.ofproto, packet->size, 0);
4741 /* OpenFlow to datapath action translation. */
4743 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
4744 struct action_xlate_ctx *ctx);
4745 static void xlate_normal(struct action_xlate_ctx *);
4747 /* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
4748 * The action will state 'slow' as the reason that the action is in the slow
4749 * path. (This is purely informational: it allows a human viewing "ovs-dpctl
4750 * dump-flows" output to see why a flow is in the slow path.)
4752 * The 'stub_size' bytes in 'stub' will be used to store the action.
4753 * 'stub_size' must be large enough for the action.
4755 * The action and its size will be stored in '*actionsp' and '*actions_lenp',
4758 compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
4759 enum slow_path_reason slow,
4760 uint64_t *stub, size_t stub_size,
4761 const struct nlattr **actionsp, size_t *actions_lenp)
4763 union user_action_cookie cookie;
4766 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
4767 cookie.slow_path.unused = 0;
4768 cookie.slow_path.reason = slow;
4770 ofpbuf_use_stack(&buf, stub, stub_size);
4771 if (slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)) {
4772 uint32_t pid = dpif_port_get_pid(ofproto->dpif, UINT16_MAX);
4773 odp_put_userspace_action(pid, &cookie, &buf);
4775 put_userspace_action(ofproto, &buf, flow, &cookie);
4777 *actionsp = buf.data;
4778 *actions_lenp = buf.size;
4782 put_userspace_action(const struct ofproto_dpif *ofproto,
4783 struct ofpbuf *odp_actions,
4784 const struct flow *flow,
4785 const union user_action_cookie *cookie)
4789 pid = dpif_port_get_pid(ofproto->dpif,
4790 ofp_port_to_odp_port(flow->in_port));
4792 return odp_put_userspace_action(pid, cookie, odp_actions);
4796 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
4797 ovs_be16 vlan_tci, uint32_t odp_port,
4798 unsigned int n_outputs, union user_action_cookie *cookie)
4802 cookie->type = USER_ACTION_COOKIE_SFLOW;
4803 cookie->sflow.vlan_tci = vlan_tci;
4805 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
4806 * port information") for the interpretation of cookie->output. */
4807 switch (n_outputs) {
4809 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
4810 cookie->sflow.output = 0x40000000 | 256;
4814 ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
4816 cookie->sflow.output = ifindex;
4821 /* 0x80000000 means "multiple output ports. */
4822 cookie->sflow.output = 0x80000000 | n_outputs;
4827 /* Compose SAMPLE action for sFlow. */
4829 compose_sflow_action(const struct ofproto_dpif *ofproto,
4830 struct ofpbuf *odp_actions,
4831 const struct flow *flow,
4834 uint32_t probability;
4835 union user_action_cookie cookie;
4836 size_t sample_offset, actions_offset;
4839 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
4843 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
4845 /* Number of packets out of UINT_MAX to sample. */
4846 probability = dpif_sflow_get_probability(ofproto->sflow);
4847 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
4849 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
4850 compose_sflow_cookie(ofproto, htons(0), odp_port,
4851 odp_port == OVSP_NONE ? 0 : 1, &cookie);
4852 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
4854 nl_msg_end_nested(odp_actions, actions_offset);
4855 nl_msg_end_nested(odp_actions, sample_offset);
4856 return cookie_offset;
4859 /* SAMPLE action must be first action in any given list of actions.
4860 * At this point we do not have all information required to build it. So try to
4861 * build sample action as complete as possible. */
4863 add_sflow_action(struct action_xlate_ctx *ctx)
4865 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
4867 &ctx->flow, OVSP_NONE);
4868 ctx->sflow_odp_port = 0;
4869 ctx->sflow_n_outputs = 0;
4872 /* Fix SAMPLE action according to data collected while composing ODP actions.
4873 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
4874 * USERSPACE action's user-cookie which is required for sflow. */
4876 fix_sflow_action(struct action_xlate_ctx *ctx)
4878 const struct flow *base = &ctx->base_flow;
4879 union user_action_cookie *cookie;
4881 if (!ctx->user_cookie_offset) {
4885 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
4887 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
4889 compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
4890 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
4894 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4897 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
4898 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
4899 ovs_be16 flow_vlan_tci = ctx->flow.vlan_tci;
4900 uint8_t flow_nw_tos = ctx->flow.nw_tos;
4904 struct priority_to_dscp *pdscp;
4906 if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD
4907 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4911 pdscp = get_priority(ofport, ctx->flow.skb_priority);
4913 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4914 ctx->flow.nw_tos |= pdscp->dscp;
4917 /* We may not have an ofport record for this port, but it doesn't hurt
4918 * to allow forwarding to it anyhow. Maybe such a port will appear
4919 * later and we're pre-populating the flow table. */
4922 out_port = vsp_realdev_to_vlandev(ctx->ofproto, odp_port,
4923 ctx->flow.vlan_tci);
4924 if (out_port != odp_port) {
4925 ctx->flow.vlan_tci = htons(0);
4927 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
4928 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
4930 ctx->sflow_odp_port = odp_port;
4931 ctx->sflow_n_outputs++;
4932 ctx->nf_output_iface = ofp_port;
4933 ctx->flow.vlan_tci = flow_vlan_tci;
4934 ctx->flow.nw_tos = flow_nw_tos;
4938 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
4940 compose_output_action__(ctx, ofp_port, true);
4944 xlate_table_action(struct action_xlate_ctx *ctx,
4945 uint16_t in_port, uint8_t table_id)
4947 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
4948 struct ofproto_dpif *ofproto = ctx->ofproto;
4949 struct rule_dpif *rule;
4950 uint16_t old_in_port;
4951 uint8_t old_table_id;
4953 old_table_id = ctx->table_id;
4954 ctx->table_id = table_id;
4956 /* Look up a flow with 'in_port' as the input port. */
4957 old_in_port = ctx->flow.in_port;
4958 ctx->flow.in_port = in_port;
4959 rule = rule_dpif_lookup__(ofproto, &ctx->flow, table_id);
4962 if (table_id > 0 && table_id < N_TABLES) {
4963 struct table_dpif *table = &ofproto->tables[table_id];
4964 if (table->other_table) {
4965 ctx->tags |= (rule && rule->tag
4967 : rule_calculate_tag(&ctx->flow,
4968 &table->other_table->wc,
4973 /* Restore the original input port. Otherwise OFPP_NORMAL and
4974 * OFPP_IN_PORT will have surprising behavior. */
4975 ctx->flow.in_port = old_in_port;
4977 if (ctx->resubmit_hook) {
4978 ctx->resubmit_hook(ctx, rule);
4982 struct rule_dpif *old_rule = ctx->rule;
4984 if (ctx->resubmit_stats) {
4985 rule_credit_stats(rule, ctx->resubmit_stats);
4990 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
4991 ctx->rule = old_rule;
4995 ctx->table_id = old_table_id;
4997 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
4999 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
5000 MAX_RESUBMIT_RECURSION);
5001 ctx->max_resubmit_trigger = true;
5006 xlate_resubmit_table(struct action_xlate_ctx *ctx,
5007 const struct nx_action_resubmit *nar)
5012 in_port = (nar->in_port == htons(OFPP_IN_PORT)
5014 : ntohs(nar->in_port));
5015 table_id = nar->table == 255 ? ctx->table_id : nar->table;
5017 xlate_table_action(ctx, in_port, table_id);
5021 flood_packets(struct action_xlate_ctx *ctx, bool all)
5023 struct ofport_dpif *ofport;
5025 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
5026 uint16_t ofp_port = ofport->up.ofp_port;
5028 if (ofp_port == ctx->flow.in_port) {
5033 compose_output_action__(ctx, ofp_port, false);
5034 } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
5035 compose_output_action(ctx, ofp_port);
5039 ctx->nf_output_iface = NF_OUT_FLOOD;
5043 execute_controller_action(struct action_xlate_ctx *ctx, int len,
5044 enum ofp_packet_in_reason reason,
5045 uint16_t controller_id)
5047 struct ofputil_packet_in pin;
5048 struct ofpbuf *packet;
5050 ctx->slow |= SLOW_CONTROLLER;
5055 packet = ofpbuf_clone(ctx->packet);
5057 if (packet->l2 && packet->l3) {
5058 struct eth_header *eh;
5060 eth_pop_vlan(packet);
5063 /* If the Ethernet type is less than ETH_TYPE_MIN, it's likely an 802.2
5064 * LLC frame. Calculating the Ethernet type of these frames is more
5065 * trouble than seems appropriate for a simple assertion. */
5066 assert(ntohs(eh->eth_type) < ETH_TYPE_MIN
5067 || eh->eth_type == ctx->flow.dl_type);
5069 memcpy(eh->eth_src, ctx->flow.dl_src, sizeof eh->eth_src);
5070 memcpy(eh->eth_dst, ctx->flow.dl_dst, sizeof eh->eth_dst);
5072 if (ctx->flow.vlan_tci & htons(VLAN_CFI)) {
5073 eth_push_vlan(packet, ctx->flow.vlan_tci);
5077 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
5078 packet_set_ipv4(packet, ctx->flow.nw_src, ctx->flow.nw_dst,
5079 ctx->flow.nw_tos, ctx->flow.nw_ttl);
5083 if (ctx->flow.nw_proto == IPPROTO_TCP) {
5084 packet_set_tcp_port(packet, ctx->flow.tp_src,
5086 } else if (ctx->flow.nw_proto == IPPROTO_UDP) {
5087 packet_set_udp_port(packet, ctx->flow.tp_src,
5094 pin.packet = packet->data;
5095 pin.packet_len = packet->size;
5096 pin.reason = reason;
5097 pin.controller_id = controller_id;
5098 pin.table_id = ctx->table_id;
5099 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
5102 flow_get_metadata(&ctx->flow, &pin.fmd);
5104 connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
5105 ofpbuf_delete(packet);
5109 compose_dec_ttl(struct action_xlate_ctx *ctx)
5111 if (ctx->flow.dl_type != htons(ETH_TYPE_IP) &&
5112 ctx->flow.dl_type != htons(ETH_TYPE_IPV6)) {
5116 if (ctx->flow.nw_ttl > 1) {
5120 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
5122 /* Stop processing for current table. */
5128 xlate_output_action__(struct action_xlate_ctx *ctx,
5129 uint16_t port, uint16_t max_len)
5131 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
5133 ctx->nf_output_iface = NF_OUT_DROP;
5137 compose_output_action(ctx, ctx->flow.in_port);
5140 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
5146 flood_packets(ctx, false);
5149 flood_packets(ctx, true);
5151 case OFPP_CONTROLLER:
5152 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
5158 if (port != ctx->flow.in_port) {
5159 compose_output_action(ctx, port);
5164 if (prev_nf_output_iface == NF_OUT_FLOOD) {
5165 ctx->nf_output_iface = NF_OUT_FLOOD;
5166 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
5167 ctx->nf_output_iface = prev_nf_output_iface;
5168 } else if (prev_nf_output_iface != NF_OUT_DROP &&
5169 ctx->nf_output_iface != NF_OUT_FLOOD) {
5170 ctx->nf_output_iface = NF_OUT_MULTI;
5175 xlate_output_reg_action(struct action_xlate_ctx *ctx,
5176 const struct nx_action_output_reg *naor)
5178 struct mf_subfield src;
5181 nxm_decode(&src, naor->src, naor->ofs_nbits);
5182 ofp_port = mf_get_subfield(&src, &ctx->flow);
5184 if (ofp_port <= UINT16_MAX) {
5185 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
5190 xlate_output_action(struct action_xlate_ctx *ctx,
5191 const struct ofp_action_output *oao)
5193 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
5197 xlate_enqueue_action(struct action_xlate_ctx *ctx,
5198 const struct ofp_action_enqueue *oae)
5201 uint32_t flow_priority, priority;
5204 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
5207 /* Fall back to ordinary output action. */
5208 xlate_output_action__(ctx, ntohs(oae->port), 0);
5212 /* Figure out datapath output port. */
5213 ofp_port = ntohs(oae->port);
5214 if (ofp_port == OFPP_IN_PORT) {
5215 ofp_port = ctx->flow.in_port;
5216 } else if (ofp_port == ctx->flow.in_port) {
5220 /* Add datapath actions. */
5221 flow_priority = ctx->flow.skb_priority;
5222 ctx->flow.skb_priority = priority;
5223 compose_output_action(ctx, ofp_port);
5224 ctx->flow.skb_priority = flow_priority;
5226 /* Update NetFlow output port. */
5227 if (ctx->nf_output_iface == NF_OUT_DROP) {
5228 ctx->nf_output_iface = ofp_port;
5229 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
5230 ctx->nf_output_iface = NF_OUT_MULTI;
5235 xlate_set_queue_action(struct action_xlate_ctx *ctx,
5236 const struct nx_action_set_queue *nasq)
5241 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
5244 /* Couldn't translate queue to a priority, so ignore. A warning
5245 * has already been logged. */
5249 ctx->flow.skb_priority = priority;
5252 struct xlate_reg_state {
5258 xlate_autopath(struct action_xlate_ctx *ctx,
5259 const struct nx_action_autopath *naa)
5261 uint16_t ofp_port = ntohl(naa->id);
5262 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
5264 if (!port || !port->bundle) {
5265 ofp_port = OFPP_NONE;
5266 } else if (port->bundle->bond) {
5267 /* Autopath does not support VLAN hashing. */
5268 struct ofport_dpif *slave = bond_choose_output_slave(
5269 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
5271 ofp_port = slave->up.ofp_port;
5274 autopath_execute(naa, &ctx->flow, ofp_port);
5278 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
5280 struct ofproto_dpif *ofproto = ofproto_;
5281 struct ofport_dpif *port;
5291 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
5294 port = get_ofp_port(ofproto, ofp_port);
5295 return port ? port->may_enable : false;
5300 xlate_learn_action(struct action_xlate_ctx *ctx,
5301 const struct nx_action_learn *learn)
5303 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
5304 struct ofputil_flow_mod fm;
5307 learn_execute(learn, &ctx->flow, &fm);
5309 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
5310 if (error && !VLOG_DROP_WARN(&rl)) {
5311 VLOG_WARN("learning action failed to modify flow table (%s)",
5312 ofperr_get_name(error));
5318 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5319 * means "infinite". */
5321 reduce_timeout(uint16_t max, uint16_t *timeout)
5323 if (max && (!*timeout || *timeout > max)) {
5329 xlate_fin_timeout(struct action_xlate_ctx *ctx,
5330 const struct nx_action_fin_timeout *naft)
5332 if (ctx->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
5333 struct rule_dpif *rule = ctx->rule;
5335 reduce_timeout(ntohs(naft->fin_idle_timeout), &rule->up.idle_timeout);
5336 reduce_timeout(ntohs(naft->fin_hard_timeout), &rule->up.hard_timeout);
5341 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
5343 if (port->up.pp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
5344 ? OFPUTIL_PC_NO_RECV_STP
5345 : OFPUTIL_PC_NO_RECV)) {
5349 /* Only drop packets here if both forwarding and learning are
5350 * disabled. If just learning is enabled, we need to have
5351 * OFPP_NORMAL and the learning action have a look at the packet
5352 * before we can drop it. */
5353 if (!stp_forward_in_state(port->stp_state)
5354 && !stp_learn_in_state(port->stp_state)) {
5362 do_xlate_actions(const union ofp_action *in, size_t n_in,
5363 struct action_xlate_ctx *ctx)
5365 const struct ofport_dpif *port;
5366 const union ofp_action *ia;
5367 bool was_evictable = true;
5370 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
5371 if (port && !may_receive(port, ctx)) {
5372 /* Drop this flow. */
5377 /* Don't let the rule we're working on get evicted underneath us. */
5378 was_evictable = ctx->rule->up.evictable;
5379 ctx->rule->up.evictable = false;
5381 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
5382 const struct ofp_action_dl_addr *oada;
5383 const struct nx_action_resubmit *nar;
5384 const struct nx_action_set_tunnel *nast;
5385 const struct nx_action_set_queue *nasq;
5386 const struct nx_action_multipath *nam;
5387 const struct nx_action_autopath *naa;
5388 const struct nx_action_bundle *nab;
5389 const struct nx_action_output_reg *naor;
5390 const struct nx_action_controller *nac;
5391 enum ofputil_action_code code;
5398 code = ofputil_decode_action_unsafe(ia);
5400 case OFPUTIL_OFPAT10_OUTPUT:
5401 xlate_output_action(ctx, &ia->output);
5404 case OFPUTIL_OFPAT10_SET_VLAN_VID:
5405 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
5406 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
5409 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
5410 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
5411 ctx->flow.vlan_tci |= htons(
5412 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
5415 case OFPUTIL_OFPAT10_STRIP_VLAN:
5416 ctx->flow.vlan_tci = htons(0);
5419 case OFPUTIL_OFPAT10_SET_DL_SRC:
5420 oada = ((struct ofp_action_dl_addr *) ia);
5421 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
5424 case OFPUTIL_OFPAT10_SET_DL_DST:
5425 oada = ((struct ofp_action_dl_addr *) ia);
5426 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
5429 case OFPUTIL_OFPAT10_SET_NW_SRC:
5430 ctx->flow.nw_src = ia->nw_addr.nw_addr;
5433 case OFPUTIL_OFPAT10_SET_NW_DST:
5434 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
5437 case OFPUTIL_OFPAT10_SET_NW_TOS:
5438 /* OpenFlow 1.0 only supports IPv4. */
5439 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
5440 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
5441 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
5445 case OFPUTIL_OFPAT10_SET_TP_SRC:
5446 ctx->flow.tp_src = ia->tp_port.tp_port;
5449 case OFPUTIL_OFPAT10_SET_TP_DST:
5450 ctx->flow.tp_dst = ia->tp_port.tp_port;
5453 case OFPUTIL_OFPAT10_ENQUEUE:
5454 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
5457 case OFPUTIL_NXAST_RESUBMIT:
5458 nar = (const struct nx_action_resubmit *) ia;
5459 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
5462 case OFPUTIL_NXAST_RESUBMIT_TABLE:
5463 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
5466 case OFPUTIL_NXAST_SET_TUNNEL:
5467 nast = (const struct nx_action_set_tunnel *) ia;
5468 tun_id = htonll(ntohl(nast->tun_id));
5469 ctx->flow.tun_id = tun_id;
5472 case OFPUTIL_NXAST_SET_QUEUE:
5473 nasq = (const struct nx_action_set_queue *) ia;
5474 xlate_set_queue_action(ctx, nasq);
5477 case OFPUTIL_NXAST_POP_QUEUE:
5478 ctx->flow.skb_priority = ctx->orig_skb_priority;
5481 case OFPUTIL_NXAST_REG_MOVE:
5482 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
5486 case OFPUTIL_NXAST_REG_LOAD:
5487 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
5491 case OFPUTIL_NXAST_NOTE:
5492 /* Nothing to do. */
5495 case OFPUTIL_NXAST_SET_TUNNEL64:
5496 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
5497 ctx->flow.tun_id = tun_id;
5500 case OFPUTIL_NXAST_MULTIPATH:
5501 nam = (const struct nx_action_multipath *) ia;
5502 multipath_execute(nam, &ctx->flow);
5505 case OFPUTIL_NXAST_AUTOPATH:
5506 naa = (const struct nx_action_autopath *) ia;
5507 xlate_autopath(ctx, naa);
5510 case OFPUTIL_NXAST_BUNDLE:
5511 ctx->ofproto->has_bundle_action = true;
5512 nab = (const struct nx_action_bundle *) ia;
5513 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
5518 case OFPUTIL_NXAST_BUNDLE_LOAD:
5519 ctx->ofproto->has_bundle_action = true;
5520 nab = (const struct nx_action_bundle *) ia;
5521 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
5525 case OFPUTIL_NXAST_OUTPUT_REG:
5526 naor = (const struct nx_action_output_reg *) ia;
5527 xlate_output_reg_action(ctx, naor);
5530 case OFPUTIL_NXAST_LEARN:
5531 ctx->has_learn = true;
5532 if (ctx->may_learn) {
5533 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
5537 case OFPUTIL_NXAST_DEC_TTL:
5538 if (compose_dec_ttl(ctx)) {
5543 case OFPUTIL_NXAST_EXIT:
5547 case OFPUTIL_NXAST_FIN_TIMEOUT:
5548 ctx->has_fin_timeout = true;
5549 xlate_fin_timeout(ctx, (const struct nx_action_fin_timeout *) ia);
5552 case OFPUTIL_NXAST_CONTROLLER:
5553 nac = (const struct nx_action_controller *) ia;
5554 execute_controller_action(ctx, ntohs(nac->max_len), nac->reason,
5555 ntohs(nac->controller_id));
5561 /* We've let OFPP_NORMAL and the learning action look at the packet,
5562 * so drop it now if forwarding is disabled. */
5563 if (port && !stp_forward_in_state(port->stp_state)) {
5564 ofpbuf_clear(ctx->odp_actions);
5565 add_sflow_action(ctx);
5568 ctx->rule->up.evictable = was_evictable;
5573 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
5574 struct ofproto_dpif *ofproto, const struct flow *flow,
5575 ovs_be16 initial_tci, struct rule_dpif *rule,
5576 uint8_t tcp_flags, const struct ofpbuf *packet)
5578 ctx->ofproto = ofproto;
5580 ctx->base_flow = ctx->flow;
5581 ctx->base_flow.tun_id = 0;
5582 ctx->base_flow.vlan_tci = initial_tci;
5584 ctx->packet = packet;
5585 ctx->may_learn = packet != NULL;
5586 ctx->tcp_flags = tcp_flags;
5587 ctx->resubmit_hook = NULL;
5588 ctx->resubmit_stats = NULL;
5591 /* Translates the 'n_in' "union ofp_action"s in 'in' into datapath actions in
5592 * 'odp_actions', using 'ctx'. */
5594 xlate_actions(struct action_xlate_ctx *ctx,
5595 const union ofp_action *in, size_t n_in,
5596 struct ofpbuf *odp_actions)
5598 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
5599 * that in the future we always keep a copy of the original flow for
5600 * tracing purposes. */
5601 static bool hit_resubmit_limit;
5603 enum slow_path_reason special;
5605 COVERAGE_INC(ofproto_dpif_xlate);
5607 ofpbuf_clear(odp_actions);
5608 ofpbuf_reserve(odp_actions, NL_A_U32_SIZE);
5610 ctx->odp_actions = odp_actions;
5613 ctx->has_learn = false;
5614 ctx->has_normal = false;
5615 ctx->has_fin_timeout = false;
5616 ctx->nf_output_iface = NF_OUT_DROP;
5619 ctx->max_resubmit_trigger = false;
5620 ctx->orig_skb_priority = ctx->flow.skb_priority;
5624 if (ctx->ofproto->has_mirrors || hit_resubmit_limit) {
5625 /* Do this conditionally because the copy is expensive enough that it
5626 * shows up in profiles.
5628 * We keep orig_flow in 'ctx' only because I couldn't make GCC 4.4
5629 * believe that I wasn't using it without initializing it if I kept it
5630 * in a local variable. */
5631 ctx->orig_flow = ctx->flow;
5634 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
5635 switch (ctx->ofproto->up.frag_handling) {
5636 case OFPC_FRAG_NORMAL:
5637 /* We must pretend that transport ports are unavailable. */
5638 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
5639 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
5642 case OFPC_FRAG_DROP:
5645 case OFPC_FRAG_REASM:
5648 case OFPC_FRAG_NX_MATCH:
5649 /* Nothing to do. */
5652 case OFPC_INVALID_TTL_TO_CONTROLLER:
5657 special = process_special(ctx->ofproto, &ctx->flow, ctx->packet);
5659 ctx->slow |= special;
5661 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
5662 ovs_be16 initial_tci = ctx->base_flow.vlan_tci;
5664 add_sflow_action(ctx);
5665 do_xlate_actions(in, n_in, ctx);
5667 if (ctx->max_resubmit_trigger && !ctx->resubmit_hook) {
5668 if (!hit_resubmit_limit) {
5669 /* We didn't record the original flow. Make sure we do from
5671 hit_resubmit_limit = true;
5672 } else if (!VLOG_DROP_ERR(&trace_rl)) {
5673 struct ds ds = DS_EMPTY_INITIALIZER;
5675 ofproto_trace(ctx->ofproto, &ctx->orig_flow, ctx->packet,
5677 VLOG_ERR("Trace triggered by excessive resubmit "
5678 "recursion:\n%s", ds_cstr(&ds));
5683 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
5684 ctx->odp_actions->data,
5685 ctx->odp_actions->size)) {
5686 ctx->slow |= SLOW_IN_BAND;
5688 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
5690 compose_output_action(ctx, OFPP_LOCAL);
5693 if (ctx->ofproto->has_mirrors) {
5694 add_mirror_actions(ctx, &ctx->orig_flow);
5696 fix_sflow_action(ctx);
5700 /* Translates the 'n_in' "union ofp_action"s in 'in' into datapath actions,
5701 * using 'ctx', and discards the datapath actions. */
5703 xlate_actions_for_side_effects(struct action_xlate_ctx *ctx,
5704 const union ofp_action *in, size_t n_in)
5706 uint64_t odp_actions_stub[1024 / 8];
5707 struct ofpbuf odp_actions;
5709 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
5710 xlate_actions(ctx, in, n_in, &odp_actions);
5711 ofpbuf_uninit(&odp_actions);
5714 /* OFPP_NORMAL implementation. */
5716 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
5718 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
5719 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
5720 * the bundle on which the packet was received, returns the VLAN to which the
5723 * Both 'vid' and the return value are in the range 0...4095. */
5725 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
5727 switch (in_bundle->vlan_mode) {
5728 case PORT_VLAN_ACCESS:
5729 return in_bundle->vlan;
5732 case PORT_VLAN_TRUNK:
5735 case PORT_VLAN_NATIVE_UNTAGGED:
5736 case PORT_VLAN_NATIVE_TAGGED:
5737 return vid ? vid : in_bundle->vlan;
5744 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
5745 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
5748 * 'vid' should be the VID obtained from the 802.1Q header that was received as
5749 * part of a packet (specify 0 if there was no 802.1Q header), in the range
5752 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
5754 /* Allow any VID on the OFPP_NONE port. */
5755 if (in_bundle == &ofpp_none_bundle) {
5759 switch (in_bundle->vlan_mode) {
5760 case PORT_VLAN_ACCESS:
5763 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5764 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
5765 "packet received on port %s configured as VLAN "
5766 "%"PRIu16" access port",
5767 in_bundle->ofproto->up.name, vid,
5768 in_bundle->name, in_bundle->vlan);
5774 case PORT_VLAN_NATIVE_UNTAGGED:
5775 case PORT_VLAN_NATIVE_TAGGED:
5777 /* Port must always carry its native VLAN. */
5781 case PORT_VLAN_TRUNK:
5782 if (!ofbundle_includes_vlan(in_bundle, vid)) {
5784 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5785 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
5786 "received on port %s not configured for trunking "
5788 in_bundle->ofproto->up.name, vid,
5789 in_bundle->name, vid);
5801 /* Given 'vlan', the VLAN that a packet belongs to, and
5802 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
5803 * that should be included in the 802.1Q header. (If the return value is 0,
5804 * then the 802.1Q header should only be included in the packet if there is a
5807 * Both 'vlan' and the return value are in the range 0...4095. */
5809 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
5811 switch (out_bundle->vlan_mode) {
5812 case PORT_VLAN_ACCESS:
5815 case PORT_VLAN_TRUNK:
5816 case PORT_VLAN_NATIVE_TAGGED:
5819 case PORT_VLAN_NATIVE_UNTAGGED:
5820 return vlan == out_bundle->vlan ? 0 : vlan;
5828 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
5831 struct ofport_dpif *port;
5833 ovs_be16 tci, old_tci;
5835 vid = output_vlan_to_vid(out_bundle, vlan);
5836 if (!out_bundle->bond) {
5837 port = ofbundle_get_a_port(out_bundle);
5839 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
5842 /* No slaves enabled, so drop packet. */
5847 old_tci = ctx->flow.vlan_tci;
5849 if (tci || out_bundle->use_priority_tags) {
5850 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
5852 tci |= htons(VLAN_CFI);
5855 ctx->flow.vlan_tci = tci;
5857 compose_output_action(ctx, port->up.ofp_port);
5858 ctx->flow.vlan_tci = old_tci;
5862 mirror_mask_ffs(mirror_mask_t mask)
5864 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
5869 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
5871 return (bundle->vlan_mode != PORT_VLAN_ACCESS
5872 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
5876 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
5878 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
5881 /* Returns an arbitrary interface within 'bundle'. */
5882 static struct ofport_dpif *
5883 ofbundle_get_a_port(const struct ofbundle *bundle)
5885 return CONTAINER_OF(list_front(&bundle->ports),
5886 struct ofport_dpif, bundle_node);
5890 vlan_is_mirrored(const struct ofmirror *m, int vlan)
5892 return !m->vlans || bitmap_is_set(m->vlans, vlan);
5896 add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
5898 struct ofproto_dpif *ofproto = ctx->ofproto;
5899 mirror_mask_t mirrors;
5900 struct ofbundle *in_bundle;
5903 const struct nlattr *a;
5906 in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
5907 ctx->packet != NULL, NULL);
5911 mirrors = in_bundle->src_mirrors;
5913 /* Drop frames on bundles reserved for mirroring. */
5914 if (in_bundle->mirror_out) {
5915 if (ctx->packet != NULL) {
5916 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5917 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5918 "%s, which is reserved exclusively for mirroring",
5919 ctx->ofproto->up.name, in_bundle->name);
5925 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
5926 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5929 vlan = input_vid_to_vlan(in_bundle, vid);
5931 /* Look at the output ports to check for destination selections. */
5933 NL_ATTR_FOR_EACH (a, left, ctx->odp_actions->data,
5934 ctx->odp_actions->size) {
5935 enum ovs_action_attr type = nl_attr_type(a);
5936 struct ofport_dpif *ofport;
5938 if (type != OVS_ACTION_ATTR_OUTPUT) {
5942 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
5943 if (ofport && ofport->bundle) {
5944 mirrors |= ofport->bundle->dst_mirrors;
5952 /* Restore the original packet before adding the mirror actions. */
5953 ctx->flow = *orig_flow;
5958 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
5960 if (!vlan_is_mirrored(m, vlan)) {
5961 mirrors &= mirrors - 1;
5965 mirrors &= ~m->dup_mirrors;
5966 ctx->mirrors |= m->dup_mirrors;
5968 output_normal(ctx, m->out, vlan);
5969 } else if (vlan != m->out_vlan
5970 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
5971 struct ofbundle *bundle;
5973 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
5974 if (ofbundle_includes_vlan(bundle, m->out_vlan)
5975 && !bundle->mirror_out) {
5976 output_normal(ctx, bundle, m->out_vlan);
5984 update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
5985 uint64_t packets, uint64_t bytes)
5991 for (; mirrors; mirrors &= mirrors - 1) {
5994 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
5997 /* In normal circumstances 'm' will not be NULL. However,
5998 * if mirrors are reconfigured, we can temporarily get out
5999 * of sync in facet_revalidate(). We could "correct" the
6000 * mirror list before reaching here, but doing that would
6001 * not properly account the traffic stats we've currently
6002 * accumulated for previous mirror configuration. */
6006 m->packet_count += packets;
6007 m->byte_count += bytes;
6011 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
6012 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
6013 * indicate this; newer upstream kernels use gratuitous ARP requests. */
6015 is_gratuitous_arp(const struct flow *flow)
6017 return (flow->dl_type == htons(ETH_TYPE_ARP)
6018 && eth_addr_is_broadcast(flow->dl_dst)
6019 && (flow->nw_proto == ARP_OP_REPLY
6020 || (flow->nw_proto == ARP_OP_REQUEST
6021 && flow->nw_src == flow->nw_dst)));
6025 update_learning_table(struct ofproto_dpif *ofproto,
6026 const struct flow *flow, int vlan,
6027 struct ofbundle *in_bundle)
6029 struct mac_entry *mac;
6031 /* Don't learn the OFPP_NONE port. */
6032 if (in_bundle == &ofpp_none_bundle) {
6036 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
6040 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
6041 if (is_gratuitous_arp(flow)) {
6042 /* We don't want to learn from gratuitous ARP packets that are
6043 * reflected back over bond slaves so we lock the learning table. */
6044 if (!in_bundle->bond) {
6045 mac_entry_set_grat_arp_lock(mac);
6046 } else if (mac_entry_is_grat_arp_locked(mac)) {
6051 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
6052 /* The log messages here could actually be useful in debugging,
6053 * so keep the rate limit relatively high. */
6054 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
6055 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
6056 "on port %s in VLAN %d",
6057 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
6058 in_bundle->name, vlan);
6060 mac->port.p = in_bundle;
6061 tag_set_add(&ofproto->revalidate_set,
6062 mac_learning_changed(ofproto->ml, mac));
6066 static struct ofbundle *
6067 lookup_input_bundle(const struct ofproto_dpif *ofproto, uint16_t in_port,
6068 bool warn, struct ofport_dpif **in_ofportp)
6070 struct ofport_dpif *ofport;
6072 /* Find the port and bundle for the received packet. */
6073 ofport = get_ofp_port(ofproto, in_port);
6075 *in_ofportp = ofport;
6077 if (ofport && ofport->bundle) {
6078 return ofport->bundle;
6081 /* Special-case OFPP_NONE, which a controller may use as the ingress
6082 * port for traffic that it is sourcing. */
6083 if (in_port == OFPP_NONE) {
6084 return &ofpp_none_bundle;
6087 /* Odd. A few possible reasons here:
6089 * - We deleted a port but there are still a few packets queued up
6092 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
6093 * we don't know about.
6095 * - The ofproto client didn't configure the port as part of a bundle.
6096 * This is particularly likely to happen if a packet was received on the
6097 * port after it was created, but before the client had a chance to
6098 * configure its bundle.
6101 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6103 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
6104 "port %"PRIu16, ofproto->up.name, in_port);
6109 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
6110 * dropped. Returns true if they may be forwarded, false if they should be
6113 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
6114 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
6116 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
6117 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
6118 * checked by input_vid_is_valid().
6120 * May also add tags to '*tags', although the current implementation only does
6121 * so in one special case.
6124 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
6125 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
6127 struct ofbundle *in_bundle = in_port->bundle;
6129 /* Drop frames for reserved multicast addresses
6130 * only if forward_bpdu option is absent. */
6131 if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
6135 if (in_bundle->bond) {
6136 struct mac_entry *mac;
6138 switch (bond_check_admissibility(in_bundle->bond, in_port,
6139 flow->dl_dst, tags)) {
6146 case BV_DROP_IF_MOVED:
6147 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
6148 if (mac && mac->port.p != in_bundle &&
6149 (!is_gratuitous_arp(flow)
6150 || mac_entry_is_grat_arp_locked(mac))) {
6161 xlate_normal(struct action_xlate_ctx *ctx)
6163 struct ofport_dpif *in_port;
6164 struct ofbundle *in_bundle;
6165 struct mac_entry *mac;
6169 ctx->has_normal = true;
6171 in_bundle = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
6172 ctx->packet != NULL, &in_port);
6177 /* Drop malformed frames. */
6178 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
6179 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
6180 if (ctx->packet != NULL) {
6181 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6182 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
6183 "VLAN tag received on port %s",
6184 ctx->ofproto->up.name, in_bundle->name);
6189 /* Drop frames on bundles reserved for mirroring. */
6190 if (in_bundle->mirror_out) {
6191 if (ctx->packet != NULL) {
6192 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6193 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
6194 "%s, which is reserved exclusively for mirroring",
6195 ctx->ofproto->up.name, in_bundle->name);
6201 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
6202 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
6205 vlan = input_vid_to_vlan(in_bundle, vid);
6207 /* Check other admissibility requirements. */
6209 !is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
6213 /* Learn source MAC. */
6214 if (ctx->may_learn) {
6215 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
6218 /* Determine output bundle. */
6219 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
6222 if (mac->port.p != in_bundle) {
6223 output_normal(ctx, mac->port.p, vlan);
6226 struct ofbundle *bundle;
6228 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
6229 if (bundle != in_bundle
6230 && ofbundle_includes_vlan(bundle, vlan)
6231 && bundle->floodable
6232 && !bundle->mirror_out) {
6233 output_normal(ctx, bundle, vlan);
6236 ctx->nf_output_iface = NF_OUT_FLOOD;
6240 /* Optimized flow revalidation.
6242 * It's a difficult problem, in general, to tell which facets need to have
6243 * their actions recalculated whenever the OpenFlow flow table changes. We
6244 * don't try to solve that general problem: for most kinds of OpenFlow flow
6245 * table changes, we recalculate the actions for every facet. This is
6246 * relatively expensive, but it's good enough if the OpenFlow flow table
6247 * doesn't change very often.
6249 * However, we can expect one particular kind of OpenFlow flow table change to
6250 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
6251 * of CPU on revalidating every facet whenever MAC learning modifies the flow
6252 * table, we add a special case that applies to flow tables in which every rule
6253 * has the same form (that is, the same wildcards), except that the table is
6254 * also allowed to have a single "catch-all" flow that matches all packets. We
6255 * optimize this case by tagging all of the facets that resubmit into the table
6256 * and invalidating the same tag whenever a flow changes in that table. The
6257 * end result is that we revalidate just the facets that need it (and sometimes
6258 * a few more, but not all of the facets or even all of the facets that
6259 * resubmit to the table modified by MAC learning). */
6261 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
6262 * into an OpenFlow table with the given 'basis'. */
6264 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
6267 if (flow_wildcards_is_catchall(wc)) {
6270 struct flow tag_flow = *flow;
6271 flow_zero_wildcards(&tag_flow, wc);
6272 return tag_create_deterministic(flow_hash(&tag_flow, secret));
6276 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
6277 * taggability of that table.
6279 * This function must be called after *each* change to a flow table. If you
6280 * skip calling it on some changes then the pointer comparisons at the end can
6281 * be invalid if you get unlucky. For example, if a flow removal causes a
6282 * cls_table to be destroyed and then a flow insertion causes a cls_table with
6283 * different wildcards to be created with the same address, then this function
6284 * will incorrectly skip revalidation. */
6286 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
6288 struct table_dpif *table = &ofproto->tables[table_id];
6289 const struct oftable *oftable = &ofproto->up.tables[table_id];
6290 struct cls_table *catchall, *other;
6291 struct cls_table *t;
6293 catchall = other = NULL;
6295 switch (hmap_count(&oftable->cls.tables)) {
6297 /* We could tag this OpenFlow table but it would make the logic a
6298 * little harder and it's a corner case that doesn't seem worth it
6304 HMAP_FOR_EACH (t, hmap_node, &oftable->cls.tables) {
6305 if (cls_table_is_catchall(t)) {
6307 } else if (!other) {
6310 /* Indicate that we can't tag this by setting both tables to
6311 * NULL. (We know that 'catchall' is already NULL.) */
6318 /* Can't tag this table. */
6322 if (table->catchall_table != catchall || table->other_table != other) {
6323 table->catchall_table = catchall;
6324 table->other_table = other;
6325 ofproto->need_revalidate = REV_FLOW_TABLE;
6329 /* Given 'rule' that has changed in some way (either it is a rule being
6330 * inserted, a rule being deleted, or a rule whose actions are being
6331 * modified), marks facets for revalidation to ensure that packets will be
6332 * forwarded correctly according to the new state of the flow table.
6334 * This function must be called after *each* change to a flow table. See
6335 * the comment on table_update_taggable() for more information. */
6337 rule_invalidate(const struct rule_dpif *rule)
6339 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
6341 table_update_taggable(ofproto, rule->up.table_id);
6343 if (!ofproto->need_revalidate) {
6344 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
6346 if (table->other_table && rule->tag) {
6347 tag_set_add(&ofproto->revalidate_set, rule->tag);
6349 ofproto->need_revalidate = REV_FLOW_TABLE;
6355 set_frag_handling(struct ofproto *ofproto_,
6356 enum ofp_config_flags frag_handling)
6358 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6360 if (frag_handling != OFPC_FRAG_REASM) {
6361 ofproto->need_revalidate = REV_RECONFIGURE;
6369 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
6370 const struct flow *flow,
6371 const union ofp_action *ofp_actions, size_t n_ofp_actions)
6373 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6376 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
6377 return OFPERR_NXBRC_BAD_IN_PORT;
6380 error = validate_actions(ofp_actions, n_ofp_actions, flow,
6381 ofproto->max_ports);
6383 struct odputil_keybuf keybuf;
6384 struct dpif_flow_stats stats;
6388 struct action_xlate_ctx ctx;
6389 uint64_t odp_actions_stub[1024 / 8];
6390 struct ofpbuf odp_actions;
6392 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
6393 odp_flow_key_from_flow(&key, flow);
6395 dpif_flow_stats_extract(flow, packet, &stats);
6397 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, NULL,
6398 packet_get_tcp_flags(packet, flow), packet);
6399 ctx.resubmit_stats = &stats;
6401 ofpbuf_use_stub(&odp_actions,
6402 odp_actions_stub, sizeof odp_actions_stub);
6403 xlate_actions(&ctx, ofp_actions, n_ofp_actions, &odp_actions);
6404 dpif_execute(ofproto->dpif, key.data, key.size,
6405 odp_actions.data, odp_actions.size, packet);
6406 ofpbuf_uninit(&odp_actions);
6414 set_netflow(struct ofproto *ofproto_,
6415 const struct netflow_options *netflow_options)
6417 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6419 if (netflow_options) {
6420 if (!ofproto->netflow) {
6421 ofproto->netflow = netflow_create();
6423 return netflow_set_options(ofproto->netflow, netflow_options);
6425 netflow_destroy(ofproto->netflow);
6426 ofproto->netflow = NULL;
6432 get_netflow_ids(const struct ofproto *ofproto_,
6433 uint8_t *engine_type, uint8_t *engine_id)
6435 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6437 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
6441 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
6443 if (!facet_is_controller_flow(facet) &&
6444 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
6445 struct subfacet *subfacet;
6446 struct ofexpired expired;
6448 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
6449 if (subfacet->path == SF_FAST_PATH) {
6450 struct dpif_flow_stats stats;
6452 subfacet_reinstall(subfacet, &stats);
6453 subfacet_update_stats(subfacet, &stats);
6457 expired.flow = facet->flow;
6458 expired.packet_count = facet->packet_count;
6459 expired.byte_count = facet->byte_count;
6460 expired.used = facet->used;
6461 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
6466 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
6468 struct facet *facet;
6470 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
6471 send_active_timeout(ofproto, facet);
6475 static struct ofproto_dpif *
6476 ofproto_dpif_lookup(const char *name)
6478 struct ofproto_dpif *ofproto;
6480 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
6481 hash_string(name, 0), &all_ofproto_dpifs) {
6482 if (!strcmp(ofproto->up.name, name)) {
6490 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
6491 const char *argv[], void *aux OVS_UNUSED)
6493 struct ofproto_dpif *ofproto;
6496 ofproto = ofproto_dpif_lookup(argv[1]);
6498 unixctl_command_reply_error(conn, "no such bridge");
6501 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
6503 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6504 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
6508 unixctl_command_reply(conn, "table successfully flushed");
6512 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
6513 const char *argv[], void *aux OVS_UNUSED)
6515 struct ds ds = DS_EMPTY_INITIALIZER;
6516 const struct ofproto_dpif *ofproto;
6517 const struct mac_entry *e;
6519 ofproto = ofproto_dpif_lookup(argv[1]);
6521 unixctl_command_reply_error(conn, "no such bridge");
6525 ds_put_cstr(&ds, " port VLAN MAC Age\n");
6526 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
6527 struct ofbundle *bundle = e->port.p;
6528 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
6529 ofbundle_get_a_port(bundle)->odp_port,
6530 e->vlan, ETH_ADDR_ARGS(e->mac),
6531 mac_entry_age(ofproto->ml, e));
6533 unixctl_command_reply(conn, ds_cstr(&ds));
6538 struct action_xlate_ctx ctx;
6544 trace_format_rule(struct ds *result, uint8_t table_id, int level,
6545 const struct rule_dpif *rule)
6547 ds_put_char_multiple(result, '\t', level);
6549 ds_put_cstr(result, "No match\n");
6553 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
6554 table_id, ntohll(rule->up.flow_cookie));
6555 cls_rule_format(&rule->up.cr, result);
6556 ds_put_char(result, '\n');
6558 ds_put_char_multiple(result, '\t', level);
6559 ds_put_cstr(result, "OpenFlow ");
6560 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
6561 ds_put_char(result, '\n');
6565 trace_format_flow(struct ds *result, int level, const char *title,
6566 struct trace_ctx *trace)
6568 ds_put_char_multiple(result, '\t', level);
6569 ds_put_format(result, "%s: ", title);
6570 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
6571 ds_put_cstr(result, "unchanged");
6573 flow_format(result, &trace->ctx.flow);
6574 trace->flow = trace->ctx.flow;
6576 ds_put_char(result, '\n');
6580 trace_format_regs(struct ds *result, int level, const char *title,
6581 struct trace_ctx *trace)
6585 ds_put_char_multiple(result, '\t', level);
6586 ds_put_format(result, "%s:", title);
6587 for (i = 0; i < FLOW_N_REGS; i++) {
6588 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
6590 ds_put_char(result, '\n');
6594 trace_format_odp(struct ds *result, int level, const char *title,
6595 struct trace_ctx *trace)
6597 struct ofpbuf *odp_actions = trace->ctx.odp_actions;
6599 ds_put_char_multiple(result, '\t', level);
6600 ds_put_format(result, "%s: ", title);
6601 format_odp_actions(result, odp_actions->data, odp_actions->size);
6602 ds_put_char(result, '\n');
6606 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
6608 struct trace_ctx *trace = CONTAINER_OF(ctx, struct trace_ctx, ctx);
6609 struct ds *result = trace->result;
6611 ds_put_char(result, '\n');
6612 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
6613 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
6614 trace_format_odp(result, ctx->recurse + 1, "Resubmitted odp", trace);
6615 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
6619 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
6620 void *aux OVS_UNUSED)
6622 const char *dpname = argv[1];
6623 struct ofproto_dpif *ofproto;
6624 struct ofpbuf odp_key;
6625 struct ofpbuf *packet;
6626 ovs_be16 initial_tci;
6632 ofpbuf_init(&odp_key, 0);
6635 ofproto = ofproto_dpif_lookup(dpname);
6637 unixctl_command_reply_error(conn, "Unknown ofproto (use ofproto/list "
6641 if (argc == 3 || (argc == 4 && !strcmp(argv[3], "-generate"))) {
6642 /* ofproto/trace dpname flow [-generate] */
6643 const char *flow_s = argv[2];
6644 const char *generate_s = argv[3];
6646 /* Allow 'flow_s' to be either a datapath flow or an OpenFlow-like
6647 * flow. We guess which type it is based on whether 'flow_s' contains
6648 * an '(', since a datapath flow always contains '(') but an
6649 * OpenFlow-like flow should not (in fact it's allowed but I believe
6650 * that's not documented anywhere).
6652 * An alternative would be to try to parse 'flow_s' both ways, but then
6653 * it would be tricky giving a sensible error message. After all, do
6654 * you just say "syntax error" or do you present both error messages?
6655 * Both choices seem lousy. */
6656 if (strchr(flow_s, '(')) {
6659 /* Convert string to datapath key. */
6660 ofpbuf_init(&odp_key, 0);
6661 error = odp_flow_key_from_string(flow_s, NULL, &odp_key);
6663 unixctl_command_reply_error(conn, "Bad flow syntax");
6667 /* Convert odp_key to flow. */
6668 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
6669 odp_key.size, &flow,
6670 &initial_tci, NULL);
6671 if (error == ODP_FIT_ERROR) {
6672 unixctl_command_reply_error(conn, "Invalid flow");
6678 error_s = parse_ofp_exact_flow(&flow, argv[2]);
6680 unixctl_command_reply_error(conn, error_s);
6685 initial_tci = flow.vlan_tci;
6686 vsp_adjust_flow(ofproto, &flow);
6689 /* Generate a packet, if requested. */
6691 packet = ofpbuf_new(0);
6692 flow_compose(packet, &flow);
6694 } else if (argc == 6) {
6695 /* ofproto/trace dpname priority tun_id in_port packet */
6696 const char *priority_s = argv[2];
6697 const char *tun_id_s = argv[3];
6698 const char *in_port_s = argv[4];
6699 const char *packet_s = argv[5];
6700 uint16_t in_port = ofp_port_to_odp_port(atoi(in_port_s));
6701 ovs_be64 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
6702 uint32_t priority = atoi(priority_s);
6705 msg = eth_from_hex(packet_s, &packet);
6707 unixctl_command_reply_error(conn, msg);
6711 ds_put_cstr(&result, "Packet: ");
6712 s = ofp_packet_to_string(packet->data, packet->size);
6713 ds_put_cstr(&result, s);
6716 flow_extract(packet, priority, tun_id, in_port, &flow);
6717 initial_tci = flow.vlan_tci;
6719 unixctl_command_reply_error(conn, "Bad command syntax");
6723 ofproto_trace(ofproto, &flow, packet, initial_tci, &result);
6724 unixctl_command_reply(conn, ds_cstr(&result));
6727 ds_destroy(&result);
6728 ofpbuf_delete(packet);
6729 ofpbuf_uninit(&odp_key);
6733 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
6734 const struct ofpbuf *packet, ovs_be16 initial_tci,
6737 struct rule_dpif *rule;
6739 ds_put_cstr(ds, "Flow: ");
6740 flow_format(ds, flow);
6741 ds_put_char(ds, '\n');
6743 rule = rule_dpif_lookup(ofproto, flow);
6745 trace_format_rule(ds, 0, 0, rule);
6746 if (rule == ofproto->miss_rule) {
6747 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
6748 } else if (rule == ofproto->no_packet_in_rule) {
6749 ds_put_cstr(ds, "\nNo match, packets dropped because "
6750 "OFPPC_NO_PACKET_IN is set on in_port.\n");
6754 uint64_t odp_actions_stub[1024 / 8];
6755 struct ofpbuf odp_actions;
6757 struct trace_ctx trace;
6760 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
6763 ofpbuf_use_stub(&odp_actions,
6764 odp_actions_stub, sizeof odp_actions_stub);
6765 action_xlate_ctx_init(&trace.ctx, ofproto, flow, initial_tci,
6766 rule, tcp_flags, packet);
6767 trace.ctx.resubmit_hook = trace_resubmit;
6768 xlate_actions(&trace.ctx, rule->up.actions, rule->up.n_actions,
6771 ds_put_char(ds, '\n');
6772 trace_format_flow(ds, 0, "Final flow", &trace);
6773 ds_put_cstr(ds, "Datapath actions: ");
6774 format_odp_actions(ds, odp_actions.data, odp_actions.size);
6775 ofpbuf_uninit(&odp_actions);
6777 if (trace.ctx.slow) {
6778 enum slow_path_reason slow;
6780 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
6781 "slow path because it:");
6782 for (slow = trace.ctx.slow; slow; ) {
6783 enum slow_path_reason bit = rightmost_1bit(slow);
6787 ds_put_cstr(ds, "\n\t- Consists of CFM packets.");
6790 ds_put_cstr(ds, "\n\t- Consists of LACP packets.");
6793 ds_put_cstr(ds, "\n\t- Consists of STP packets.");
6796 ds_put_cstr(ds, "\n\t- Needs in-band special case "
6799 ds_put_cstr(ds, "\n\t (The datapath actions are "
6800 "incomplete--for complete actions, "
6801 "please supply a packet.)");
6804 case SLOW_CONTROLLER:
6805 ds_put_cstr(ds, "\n\t- Sends \"packet-in\" messages "
6806 "to the OpenFlow controller.");
6809 ds_put_cstr(ds, "\n\t- Needs more specific matching "
6810 "than the datapath supports.");
6817 if (slow & ~SLOW_MATCH) {
6818 ds_put_cstr(ds, "\nThe datapath actions above do not reflect "
6819 "the special slow-path processing.");
6826 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6827 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6830 unixctl_command_reply(conn, NULL);
6834 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6835 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6838 unixctl_command_reply(conn, NULL);
6841 /* Runs a self-check of flow translations in 'ofproto'. Appends a message to
6842 * 'reply' describing the results. */
6844 ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
6846 struct facet *facet;
6850 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
6851 if (!facet_check_consistency(facet)) {
6856 ofproto->need_revalidate = REV_INCONSISTENCY;
6860 ds_put_format(reply, "%s: self-check failed (%d errors)\n",
6861 ofproto->up.name, errors);
6863 ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
6868 ofproto_dpif_self_check(struct unixctl_conn *conn,
6869 int argc, const char *argv[], void *aux OVS_UNUSED)
6871 struct ds reply = DS_EMPTY_INITIALIZER;
6872 struct ofproto_dpif *ofproto;
6875 ofproto = ofproto_dpif_lookup(argv[1]);
6877 unixctl_command_reply_error(conn, "Unknown ofproto (use "
6878 "ofproto/list for help)");
6881 ofproto_dpif_self_check__(ofproto, &reply);
6883 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6884 ofproto_dpif_self_check__(ofproto, &reply);
6888 unixctl_command_reply(conn, ds_cstr(&reply));
6893 ofproto_dpif_unixctl_init(void)
6895 static bool registered;
6901 unixctl_command_register(
6903 "bridge {tun_id in_port packet | odp_flow [-generate]}",
6904 2, 5, ofproto_unixctl_trace, NULL);
6905 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
6906 ofproto_unixctl_fdb_flush, NULL);
6907 unixctl_command_register("fdb/show", "bridge", 1, 1,
6908 ofproto_unixctl_fdb_show, NULL);
6909 unixctl_command_register("ofproto/clog", "", 0, 0,
6910 ofproto_dpif_clog, NULL);
6911 unixctl_command_register("ofproto/unclog", "", 0, 0,
6912 ofproto_dpif_unclog, NULL);
6913 unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
6914 ofproto_dpif_self_check, NULL);
6917 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6919 * This is deprecated. It is only for compatibility with broken device drivers
6920 * in old versions of Linux that do not properly support VLANs when VLAN
6921 * devices are not used. When broken device drivers are no longer in
6922 * widespread use, we will delete these interfaces. */
6925 set_realdev(struct ofport *ofport_, uint16_t realdev_ofp_port, int vid)
6927 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
6928 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
6930 if (realdev_ofp_port == ofport->realdev_ofp_port
6931 && vid == ofport->vlandev_vid) {
6935 ofproto->need_revalidate = REV_RECONFIGURE;
6937 if (ofport->realdev_ofp_port) {
6940 if (realdev_ofp_port && ofport->bundle) {
6941 /* vlandevs are enslaved to their realdevs, so they are not allowed to
6942 * themselves be part of a bundle. */
6943 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
6946 ofport->realdev_ofp_port = realdev_ofp_port;
6947 ofport->vlandev_vid = vid;
6949 if (realdev_ofp_port) {
6950 vsp_add(ofport, realdev_ofp_port, vid);
6957 hash_realdev_vid(uint16_t realdev_ofp_port, int vid)
6959 return hash_2words(realdev_ofp_port, vid);
6962 /* Returns the ODP port number of the Linux VLAN device that corresponds to
6963 * 'vlan_tci' on the network device with port number 'realdev_odp_port' in
6964 * 'ofproto'. For example, given 'realdev_odp_port' of eth0 and 'vlan_tci' 9,
6965 * it would return the port number of eth0.9.
6967 * Unless VLAN splinters are enabled for port 'realdev_odp_port', this
6968 * function just returns its 'realdev_odp_port' argument. */
6970 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
6971 uint32_t realdev_odp_port, ovs_be16 vlan_tci)
6973 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
6974 uint16_t realdev_ofp_port = odp_port_to_ofp_port(realdev_odp_port);
6975 int vid = vlan_tci_to_vid(vlan_tci);
6976 const struct vlan_splinter *vsp;
6978 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
6979 hash_realdev_vid(realdev_ofp_port, vid),
6980 &ofproto->realdev_vid_map) {
6981 if (vsp->realdev_ofp_port == realdev_ofp_port
6982 && vsp->vid == vid) {
6983 return ofp_port_to_odp_port(vsp->vlandev_ofp_port);
6987 return realdev_odp_port;
6990 static struct vlan_splinter *
6991 vlandev_find(const struct ofproto_dpif *ofproto, uint16_t vlandev_ofp_port)
6993 struct vlan_splinter *vsp;
6995 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node, hash_int(vlandev_ofp_port, 0),
6996 &ofproto->vlandev_map) {
6997 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
7005 /* Returns the OpenFlow port number of the "real" device underlying the Linux
7006 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
7007 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
7008 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
7009 * eth0 and store 9 in '*vid'.
7011 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
7012 * VLAN device. Unless VLAN splinters are enabled, this is what this function
7015 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
7016 uint16_t vlandev_ofp_port, int *vid)
7018 if (!hmap_is_empty(&ofproto->vlandev_map)) {
7019 const struct vlan_splinter *vsp;
7021 vsp = vlandev_find(ofproto, vlandev_ofp_port);
7026 return vsp->realdev_ofp_port;
7032 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
7033 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
7034 * 'flow->in_port' to the "real" device backing the VLAN device, sets
7035 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
7036 * always the case unless VLAN splinters are enabled), returns false without
7037 * making any changes. */
7039 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
7044 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
7049 /* Cause the flow to be processed as if it came in on the real device with
7050 * the VLAN device's VLAN ID. */
7051 flow->in_port = realdev;
7052 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
7057 vsp_remove(struct ofport_dpif *port)
7059 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
7060 struct vlan_splinter *vsp;
7062 vsp = vlandev_find(ofproto, port->up.ofp_port);
7064 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
7065 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
7068 port->realdev_ofp_port = 0;
7070 VLOG_ERR("missing vlan device record");
7075 vsp_add(struct ofport_dpif *port, uint16_t realdev_ofp_port, int vid)
7077 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
7079 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
7080 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
7081 == realdev_ofp_port)) {
7082 struct vlan_splinter *vsp;
7084 vsp = xmalloc(sizeof *vsp);
7085 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
7086 hash_int(port->up.ofp_port, 0));
7087 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
7088 hash_realdev_vid(realdev_ofp_port, vid));
7089 vsp->realdev_ofp_port = realdev_ofp_port;
7090 vsp->vlandev_ofp_port = port->up.ofp_port;
7093 port->realdev_ofp_port = realdev_ofp_port;
7095 VLOG_ERR("duplicate vlan device record");
7099 const struct ofproto_class ofproto_dpif_class = {
7129 port_is_lacp_current,
7130 NULL, /* rule_choose_table */
7137 rule_modify_actions,
7145 get_cfm_remote_mpids,
7150 get_stp_port_status,
7157 is_mirror_output_bundle,
7158 forward_bpdu_changed,