2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
31 #include "fail-open.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
45 #include "ofp-actions.h"
46 #include "ofp-parse.h"
47 #include "ofp-print.h"
48 #include "ofproto-dpif-governor.h"
49 #include "ofproto-dpif-sflow.h"
50 #include "poll-loop.h"
55 #include "unaligned.h"
57 #include "vlan-bitmap.h"
60 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
62 COVERAGE_DEFINE(ofproto_dpif_expired);
63 COVERAGE_DEFINE(ofproto_dpif_xlate);
64 COVERAGE_DEFINE(facet_changed_rule);
65 COVERAGE_DEFINE(facet_revalidate);
66 COVERAGE_DEFINE(facet_unexpected);
67 COVERAGE_DEFINE(facet_suppress);
69 /* Maximum depth of flow table recursion (due to resubmit actions) in a
70 * flow translation. */
71 #define MAX_RESUBMIT_RECURSION 64
73 /* Number of implemented OpenFlow tables. */
74 enum { N_TABLES = 255 };
75 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
76 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
88 * - Do include packets and bytes from facets that have been deleted or
89 * whose own statistics have been folded into the rule.
91 * - Do include packets and bytes sent "by hand" that were accounted to
92 * the rule without any facet being involved (this is a rare corner
93 * case in rule_execute()).
95 * - Do not include packet or bytes that can be obtained from any facet's
96 * packet_count or byte_count member or that can be obtained from the
97 * datapath by, e.g., dpif_flow_get() for any subfacet.
99 uint64_t packet_count; /* Number of packets received. */
100 uint64_t byte_count; /* Number of bytes received. */
102 tag_type tag; /* Caches rule_calculate_tag() result. */
104 struct list facets; /* List of "struct facet"s. */
107 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
109 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
112 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
113 const struct flow *);
114 static struct rule_dpif *rule_dpif_lookup__(struct ofproto_dpif *,
117 static struct rule_dpif *rule_dpif_miss_rule(struct ofproto_dpif *ofproto,
118 const struct flow *flow);
120 static void rule_credit_stats(struct rule_dpif *,
121 const struct dpif_flow_stats *);
122 static void flow_push_stats(struct facet *, const struct dpif_flow_stats *);
123 static tag_type rule_calculate_tag(const struct flow *,
124 const struct minimask *, uint32_t basis);
125 static void rule_invalidate(const struct rule_dpif *);
127 #define MAX_MIRRORS 32
128 typedef uint32_t mirror_mask_t;
129 #define MIRROR_MASK_C(X) UINT32_C(X)
130 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
132 struct ofproto_dpif *ofproto; /* Owning ofproto. */
133 size_t idx; /* In ofproto's "mirrors" array. */
134 void *aux; /* Key supplied by ofproto's client. */
135 char *name; /* Identifier for log messages. */
137 /* Selection criteria. */
138 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
139 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
140 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
142 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
143 struct ofbundle *out; /* Output port or NULL. */
144 int out_vlan; /* Output VLAN or -1. */
145 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
148 int64_t packet_count; /* Number of packets sent. */
149 int64_t byte_count; /* Number of bytes sent. */
152 static void mirror_destroy(struct ofmirror *);
153 static void update_mirror_stats(struct ofproto_dpif *ofproto,
154 mirror_mask_t mirrors,
155 uint64_t packets, uint64_t bytes);
158 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
159 struct ofproto_dpif *ofproto; /* Owning ofproto. */
160 void *aux; /* Key supplied by ofproto's client. */
161 char *name; /* Identifier for log messages. */
164 struct list ports; /* Contains "struct ofport"s. */
165 enum port_vlan_mode vlan_mode; /* VLAN mode */
166 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
167 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
168 * NULL if all VLANs are trunked. */
169 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
170 struct bond *bond; /* Nonnull iff more than one port. */
171 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
174 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
176 /* Port mirroring info. */
177 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
178 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
179 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
182 static void bundle_remove(struct ofport *);
183 static void bundle_update(struct ofbundle *);
184 static void bundle_destroy(struct ofbundle *);
185 static void bundle_del_port(struct ofport_dpif *);
186 static void bundle_run(struct ofbundle *);
187 static void bundle_wait(struct ofbundle *);
188 static struct ofbundle *lookup_input_bundle(const struct ofproto_dpif *,
189 uint16_t in_port, bool warn,
190 struct ofport_dpif **in_ofportp);
192 /* A controller may use OFPP_NONE as the ingress port to indicate that
193 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
194 * when an input bundle is needed for validation (e.g., mirroring or
195 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
196 * any 'port' structs, so care must be taken when dealing with it. */
197 static struct ofbundle ofpp_none_bundle = {
199 .vlan_mode = PORT_VLAN_TRUNK
202 static void stp_run(struct ofproto_dpif *ofproto);
203 static void stp_wait(struct ofproto_dpif *ofproto);
204 static int set_stp_port(struct ofport *,
205 const struct ofproto_port_stp_settings *);
207 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
209 struct action_xlate_ctx {
210 /* action_xlate_ctx_init() initializes these members. */
213 struct ofproto_dpif *ofproto;
215 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
216 * this flow when actions change header fields. */
219 /* stack for the push and pop actions.
220 * Each stack element is of the type "union mf_subvalue". */
222 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
224 /* The packet corresponding to 'flow', or a null pointer if we are
225 * revalidating without a packet to refer to. */
226 const struct ofpbuf *packet;
228 /* Should OFPP_NORMAL update the MAC learning table? Should "learn"
229 * actions update the flow table?
231 * We want to update these tables if we are actually processing a packet,
232 * or if we are accounting for packets that the datapath has processed, but
233 * not if we are just revalidating. */
236 /* The rule that we are currently translating, or NULL. */
237 struct rule_dpif *rule;
239 /* Union of the set of TCP flags seen so far in this flow. (Used only by
240 * NXAST_FIN_TIMEOUT. Set to zero to avoid updating updating rules'
244 /* If nonnull, flow translation calls this function just before executing a
245 * resubmit or OFPP_TABLE action. In addition, disables logging of traces
246 * when the recursion depth is exceeded.
248 * 'rule' is the rule being submitted into. It will be null if the
249 * resubmit or OFPP_TABLE action didn't find a matching rule.
251 * This is normally null so the client has to set it manually after
252 * calling action_xlate_ctx_init(). */
253 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *rule);
255 /* If nonnull, flow translation calls this function to report some
256 * significant decision, e.g. to explain why OFPP_NORMAL translation
257 * dropped a packet. */
258 void (*report_hook)(struct action_xlate_ctx *, const char *s);
260 /* If nonnull, flow translation credits the specified statistics to each
261 * rule reached through a resubmit or OFPP_TABLE action.
263 * This is normally null so the client has to set it manually after
264 * calling action_xlate_ctx_init(). */
265 const struct dpif_flow_stats *resubmit_stats;
267 /* xlate_actions() initializes and uses these members. The client might want
268 * to look at them after it returns. */
270 struct ofpbuf *odp_actions; /* Datapath actions. */
271 tag_type tags; /* Tags associated with actions. */
272 enum slow_path_reason slow; /* 0 if fast path may be used. */
273 bool has_learn; /* Actions include NXAST_LEARN? */
274 bool has_normal; /* Actions output to OFPP_NORMAL? */
275 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
276 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
277 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
279 /* xlate_actions() initializes and uses these members, but the client has no
280 * reason to look at them. */
282 int recurse; /* Recursion level, via xlate_table_action. */
283 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
284 struct flow base_flow; /* Flow at the last commit. */
285 uint32_t orig_skb_priority; /* Priority when packet arrived. */
286 uint8_t table_id; /* OpenFlow table ID where flow was found. */
287 uint32_t sflow_n_outputs; /* Number of output ports. */
288 uint32_t sflow_odp_port; /* Output port for composing sFlow action. */
289 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
290 bool exit; /* No further actions should be processed. */
293 /* Initial values of fields of the packet that may be changed during
294 * flow processing and needed later. */
295 struct initial_vals {
296 /* This is the value of vlan_tci in the packet as actually received from
297 * dpif. This is the same as the facet's flow.vlan_tci unless the packet
298 * was received via a VLAN splinter. In that case, this value is 0
299 * (because the packet as actually received from the dpif had no 802.1Q
300 * tag) but the facet's flow.vlan_tci is set to the VLAN that the splinter
303 * This member should be removed when the VLAN splinters feature is no
307 /* If received on a tunnel, the IP TOS value of the tunnel. */
308 uint8_t tunnel_ip_tos;
311 static void action_xlate_ctx_init(struct action_xlate_ctx *,
312 struct ofproto_dpif *, const struct flow *,
313 const struct initial_vals *initial_vals,
315 uint8_t tcp_flags, const struct ofpbuf *);
316 static void xlate_actions(struct action_xlate_ctx *,
317 const struct ofpact *ofpacts, size_t ofpacts_len,
318 struct ofpbuf *odp_actions);
319 static void xlate_actions_for_side_effects(struct action_xlate_ctx *,
320 const struct ofpact *ofpacts,
322 static void xlate_table_action(struct action_xlate_ctx *, uint16_t in_port,
323 uint8_t table_id, bool may_packet_in);
325 static size_t put_userspace_action(const struct ofproto_dpif *,
326 struct ofpbuf *odp_actions,
328 const union user_action_cookie *);
330 static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
331 enum slow_path_reason,
332 uint64_t *stub, size_t stub_size,
333 const struct nlattr **actionsp,
334 size_t *actions_lenp);
336 static void xlate_report(struct action_xlate_ctx *ctx, const char *s);
338 /* A subfacet (see "struct subfacet" below) has three possible installation
341 * - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
342 * case just after the subfacet is created, just before the subfacet is
343 * destroyed, or if the datapath returns an error when we try to install a
346 * - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
348 * - SF_SLOW_PATH: An action that sends every packet for the subfacet through
349 * ofproto_dpif is installed in the datapath.
352 SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
353 SF_FAST_PATH, /* Full actions are installed. */
354 SF_SLOW_PATH, /* Send-to-userspace action is installed. */
357 static const char *subfacet_path_to_string(enum subfacet_path);
359 /* A dpif flow and actions associated with a facet.
361 * See also the large comment on struct facet. */
364 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
365 struct list list_node; /* In struct facet's 'facets' list. */
366 struct facet *facet; /* Owning facet. */
368 enum odp_key_fitness key_fitness;
372 long long int used; /* Time last used; time created if not used. */
373 long long int created; /* Time created. */
375 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
376 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
380 * These should be essentially identical for every subfacet in a facet, but
381 * may differ in trivial ways due to VLAN splinters. */
382 size_t actions_len; /* Number of bytes in actions[]. */
383 struct nlattr *actions; /* Datapath actions. */
385 enum slow_path_reason slow; /* 0 if fast path may be used. */
386 enum subfacet_path path; /* Installed in datapath? */
388 /* Initial values of the packet that may be needed later. */
389 struct initial_vals initial_vals;
391 /* Datapath port the packet arrived on. This is needed to remove
392 * flows for ports that are no longer part of the bridge. Since the
393 * flow definition only has the OpenFlow port number and the port is
394 * no longer part of the bridge, we can't determine the datapath port
395 * number needed to delete the flow from the datapath. */
396 uint32_t odp_in_port;
399 #define SUBFACET_DESTROY_MAX_BATCH 50
401 static struct subfacet *subfacet_create(struct facet *, struct flow_miss *miss,
403 static struct subfacet *subfacet_find(struct ofproto_dpif *,
404 const struct nlattr *key, size_t key_len,
406 static void subfacet_destroy(struct subfacet *);
407 static void subfacet_destroy__(struct subfacet *);
408 static void subfacet_destroy_batch(struct ofproto_dpif *,
409 struct subfacet **, int n);
410 static void subfacet_reset_dp_stats(struct subfacet *,
411 struct dpif_flow_stats *);
412 static void subfacet_update_time(struct subfacet *, long long int used);
413 static void subfacet_update_stats(struct subfacet *,
414 const struct dpif_flow_stats *);
415 static void subfacet_make_actions(struct subfacet *,
416 const struct ofpbuf *packet,
417 struct ofpbuf *odp_actions);
418 static int subfacet_install(struct subfacet *,
419 const struct nlattr *actions, size_t actions_len,
420 struct dpif_flow_stats *, enum slow_path_reason);
421 static void subfacet_uninstall(struct subfacet *);
423 static enum subfacet_path subfacet_want_path(enum slow_path_reason);
425 /* An exact-match instantiation of an OpenFlow flow.
427 * A facet associates a "struct flow", which represents the Open vSwitch
428 * userspace idea of an exact-match flow, with one or more subfacets. Each
429 * subfacet tracks the datapath's idea of the exact-match flow equivalent to
430 * the facet. When the kernel module (or other dpif implementation) and Open
431 * vSwitch userspace agree on the definition of a flow key, there is exactly
432 * one subfacet per facet. If the dpif implementation supports more-specific
433 * flow matching than userspace, however, a facet can have more than one
434 * subfacet, each of which corresponds to some distinction in flow that
435 * userspace simply doesn't understand.
437 * Flow expiration works in terms of subfacets, so a facet must have at least
438 * one subfacet or it will never expire, leaking memory. */
441 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
442 struct list list_node; /* In owning rule's 'facets' list. */
443 struct rule_dpif *rule; /* Owning rule. */
446 struct list subfacets;
447 long long int used; /* Time last used; time created if not used. */
454 * - Do include packets and bytes sent "by hand", e.g. with
457 * - Do include packets and bytes that were obtained from the datapath
458 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
459 * DPIF_FP_ZERO_STATS).
461 * - Do not include packets or bytes that can be obtained from the
462 * datapath for any existing subfacet.
464 uint64_t packet_count; /* Number of packets received. */
465 uint64_t byte_count; /* Number of bytes received. */
467 /* Resubmit statistics. */
468 uint64_t prev_packet_count; /* Number of packets from last stats push. */
469 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
470 long long int prev_used; /* Used time from last stats push. */
473 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
474 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
475 uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
477 /* Properties of datapath actions.
479 * Every subfacet has its own actions because actions can differ slightly
480 * between splintered and non-splintered subfacets due to the VLAN tag
481 * being initially different (present vs. absent). All of them have these
482 * properties in common so we just store one copy of them here. */
483 bool has_learn; /* Actions include NXAST_LEARN? */
484 bool has_normal; /* Actions output to OFPP_NORMAL? */
485 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
486 tag_type tags; /* Tags that would require revalidation. */
487 mirror_mask_t mirrors; /* Bitmap of dependent mirrors. */
489 /* Storage for a single subfacet, to reduce malloc() time and space
490 * overhead. (A facet always has at least one subfacet and in the common
491 * case has exactly one subfacet. However, 'one_subfacet' may not
492 * always be valid, since it could have been removed after newer
493 * subfacets were pushed onto the 'subfacets' list.) */
494 struct subfacet one_subfacet;
496 long long int learn_rl; /* Rate limiter for facet_learn(). */
499 static struct facet *facet_create(struct rule_dpif *,
500 const struct flow *, uint32_t hash);
501 static void facet_remove(struct facet *);
502 static void facet_free(struct facet *);
504 static struct facet *facet_find(struct ofproto_dpif *,
505 const struct flow *, uint32_t hash);
506 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
507 const struct flow *, uint32_t hash);
508 static void facet_revalidate(struct facet *);
509 static bool facet_check_consistency(struct facet *);
511 static void facet_flush_stats(struct facet *);
513 static void facet_update_time(struct facet *, long long int used);
514 static void facet_reset_counters(struct facet *);
515 static void facet_push_stats(struct facet *);
516 static void facet_learn(struct facet *);
517 static void facet_account(struct facet *);
519 static struct subfacet *facet_get_subfacet(struct facet *);
521 static bool facet_is_controller_flow(struct facet *);
524 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
528 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
529 struct list bundle_node; /* In struct ofbundle's "ports" list. */
530 struct cfm *cfm; /* Connectivity Fault Management, if any. */
531 tag_type tag; /* Tag associated with this port. */
532 bool may_enable; /* May be enabled in bonds. */
533 long long int carrier_seq; /* Carrier status changes. */
534 struct tnl_port *tnl_port; /* Tunnel handle, or null. */
537 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
538 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
539 long long int stp_state_entered;
541 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
543 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
545 * This is deprecated. It is only for compatibility with broken device
546 * drivers in old versions of Linux that do not properly support VLANs when
547 * VLAN devices are not used. When broken device drivers are no longer in
548 * widespread use, we will delete these interfaces. */
549 uint16_t realdev_ofp_port;
553 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
554 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
555 * traffic egressing the 'ofport' with that priority should be marked with. */
556 struct priority_to_dscp {
557 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
558 uint32_t priority; /* Priority of this queue (see struct flow). */
560 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
563 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
565 * This is deprecated. It is only for compatibility with broken device drivers
566 * in old versions of Linux that do not properly support VLANs when VLAN
567 * devices are not used. When broken device drivers are no longer in
568 * widespread use, we will delete these interfaces. */
569 struct vlan_splinter {
570 struct hmap_node realdev_vid_node;
571 struct hmap_node vlandev_node;
572 uint16_t realdev_ofp_port;
573 uint16_t vlandev_ofp_port;
577 static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
578 uint32_t realdev, ovs_be16 vlan_tci);
579 static bool vsp_adjust_flow(const struct ofproto_dpif *, struct flow *);
580 static void vsp_remove(struct ofport_dpif *);
581 static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
583 static uint32_t ofp_port_to_odp_port(const struct ofproto_dpif *,
585 static uint16_t odp_port_to_ofp_port(const struct ofproto_dpif *,
588 static struct ofport_dpif *
589 ofport_dpif_cast(const struct ofport *ofport)
591 ovs_assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
592 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
595 static void port_run(struct ofport_dpif *);
596 static void port_run_fast(struct ofport_dpif *);
597 static void port_wait(struct ofport_dpif *);
598 static int set_cfm(struct ofport *, const struct cfm_settings *);
599 static void ofport_clear_priorities(struct ofport_dpif *);
601 struct dpif_completion {
602 struct list list_node;
603 struct ofoperation *op;
606 /* Extra information about a classifier table.
607 * Currently used just for optimized flow revalidation. */
609 /* If either of these is nonnull, then this table has a form that allows
610 * flows to be tagged to avoid revalidating most flows for the most common
611 * kinds of flow table changes. */
612 struct cls_table *catchall_table; /* Table that wildcards all fields. */
613 struct cls_table *other_table; /* Table with any other wildcard set. */
614 uint32_t basis; /* Keeps each table's tags separate. */
617 /* Reasons that we might need to revalidate every facet, and corresponding
620 * A value of 0 means that there is no need to revalidate.
622 * It would be nice to have some cleaner way to integrate with coverage
623 * counters, but with only a few reasons I guess this is good enough for
625 enum revalidate_reason {
626 REV_RECONFIGURE = 1, /* Switch configuration changed. */
627 REV_STP, /* Spanning tree protocol port status change. */
628 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
629 REV_FLOW_TABLE, /* Flow table changed. */
630 REV_INCONSISTENCY /* Facet self-check failed. */
632 COVERAGE_DEFINE(rev_reconfigure);
633 COVERAGE_DEFINE(rev_stp);
634 COVERAGE_DEFINE(rev_port_toggled);
635 COVERAGE_DEFINE(rev_flow_table);
636 COVERAGE_DEFINE(rev_inconsistency);
638 /* Drop keys are odp flow keys which have drop flows installed in the kernel.
639 * These are datapath flows which have no associated ofproto, if they did we
640 * would use facets. */
642 struct hmap_node hmap_node;
647 /* All datapaths of a given type share a single dpif backer instance. */
652 struct timer next_expiration;
653 struct hmap odp_to_ofport_map; /* ODP port to ofport mapping. */
655 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
657 /* Facet revalidation flags applying to facets which use this backer. */
658 enum revalidate_reason need_revalidate; /* Revalidate every facet. */
659 struct tag_set revalidate_set; /* Revalidate only matching facets. */
661 struct hmap drop_keys; /* Set of dropped odp keys. */
664 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
665 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
667 static void drop_key_clear(struct dpif_backer *);
668 static struct ofport_dpif *
669 odp_port_to_ofport(const struct dpif_backer *, uint32_t odp_port);
671 static void dpif_stats_update_hit_count(struct ofproto_dpif *ofproto,
673 struct avg_subfacet_rates {
674 double add_rate; /* Moving average of new flows created per minute. */
675 double del_rate; /* Moving average of flows deleted per minute. */
677 static void show_dp_rates(struct ds *ds, const char *heading,
678 const struct avg_subfacet_rates *rates);
679 static void exp_mavg(double *avg, int base, double new);
681 struct ofproto_dpif {
682 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
684 struct dpif_backer *backer;
686 /* Special OpenFlow rules. */
687 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
688 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
694 struct netflow *netflow;
695 struct dpif_sflow *sflow;
696 struct hmap bundles; /* Contains "struct ofbundle"s. */
697 struct mac_learning *ml;
698 struct ofmirror *mirrors[MAX_MIRRORS];
700 bool has_bonded_bundles;
704 struct hmap subfacets;
705 struct governor *governor;
706 long long int consistency_rl;
709 struct table_dpif tables[N_TABLES];
711 /* Support for debugging async flow mods. */
712 struct list completions;
714 bool has_bundle_action; /* True when the first bundle action appears. */
715 struct netdev_stats stats; /* To account packets generated and consumed in
720 long long int stp_last_tick;
722 /* VLAN splinters. */
723 struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
724 struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
727 struct sset ports; /* Set of standard port names. */
728 struct sset ghost_ports; /* Ports with no datapath port. */
729 struct sset port_poll_set; /* Queued names for port_poll() reply. */
730 int port_poll_errno; /* Last errno for port_poll() reply. */
732 /* Per ofproto's dpif stats. */
736 /* Subfacet statistics.
738 * These keep track of the total number of subfacets added and deleted and
739 * flow life span. They are useful for computing the flow rates stats
740 * exposed via "ovs-appctl dpif/show". The goal is to learn about
741 * traffic patterns in ways that we can use later to improve Open vSwitch
742 * performance in new situations. */
743 long long int created; /* Time when it is created. */
744 unsigned int max_n_subfacet; /* Maximum number of flows */
746 /* The average number of subfacets... */
747 struct avg_subfacet_rates hourly; /* ...over the last hour. */
748 struct avg_subfacet_rates daily; /* ...over the last day. */
749 long long int last_minute; /* Last time 'hourly' was updated. */
751 /* Number of subfacets added or deleted since 'last_minute'. */
752 unsigned int subfacet_add_count;
753 unsigned int subfacet_del_count;
755 /* Number of subfacets added or deleted from 'created' to 'last_minute.' */
756 unsigned long long int total_subfacet_add_count;
757 unsigned long long int total_subfacet_del_count;
759 /* Sum of the number of milliseconds that each subfacet existed,
760 * over the subfacets that have been added and then later deleted. */
761 unsigned long long int total_subfacet_life_span;
763 /* Incremented by the number of currently existing subfacets, each
764 * time we pull statistics from the kernel. */
765 unsigned long long int total_subfacet_count;
767 /* Number of times we pull statistics from the kernel. */
768 unsigned long long int n_update_stats;
770 static unsigned long long int avg_subfacet_life_span(
771 const struct ofproto_dpif *);
772 static double avg_subfacet_count(const struct ofproto_dpif *ofproto);
773 static void update_moving_averages(struct ofproto_dpif *ofproto);
774 static void dpif_stats_update_hit_count(struct ofproto_dpif *ofproto,
776 static void update_max_subfacet_count(struct ofproto_dpif *ofproto);
778 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
779 * for debugging the asynchronous flow_mod implementation.) */
782 /* All existing ofproto_dpif instances, indexed by ->up.name. */
783 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
785 static void ofproto_dpif_unixctl_init(void);
787 static struct ofproto_dpif *
788 ofproto_dpif_cast(const struct ofproto *ofproto)
790 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
791 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
794 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *,
796 static struct ofport_dpif *get_odp_port(const struct ofproto_dpif *,
798 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
799 const struct ofpbuf *,
800 const struct initial_vals *, struct ds *);
802 /* Packet processing. */
803 static void update_learning_table(struct ofproto_dpif *,
804 const struct flow *, int vlan,
807 #define FLOW_MISS_MAX_BATCH 50
808 static int handle_upcalls(struct dpif_backer *, unsigned int max_batch);
810 /* Flow expiration. */
811 static int expire(struct dpif_backer *);
814 static void send_netflow_active_timeouts(struct ofproto_dpif *);
817 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
818 static size_t compose_sflow_action(const struct ofproto_dpif *,
819 struct ofpbuf *odp_actions,
820 const struct flow *, uint32_t odp_port);
821 static void add_mirror_actions(struct action_xlate_ctx *ctx,
822 const struct flow *flow);
823 /* Global variables. */
824 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
826 /* Initial mappings of port to bridge mappings. */
827 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
829 /* Factory functions. */
832 init(const struct shash *iface_hints)
834 struct shash_node *node;
836 /* Make a local copy, since we don't own 'iface_hints' elements. */
837 SHASH_FOR_EACH(node, iface_hints) {
838 const struct iface_hint *orig_hint = node->data;
839 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
841 new_hint->br_name = xstrdup(orig_hint->br_name);
842 new_hint->br_type = xstrdup(orig_hint->br_type);
843 new_hint->ofp_port = orig_hint->ofp_port;
845 shash_add(&init_ofp_ports, node->name, new_hint);
850 enumerate_types(struct sset *types)
852 dp_enumerate_types(types);
856 enumerate_names(const char *type, struct sset *names)
858 struct ofproto_dpif *ofproto;
861 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
862 if (strcmp(type, ofproto->up.type)) {
865 sset_add(names, ofproto->up.name);
872 del(const char *type, const char *name)
877 error = dpif_open(name, type, &dpif);
879 error = dpif_delete(dpif);
886 port_open_type(const char *datapath_type, const char *port_type)
888 return dpif_port_open_type(datapath_type, port_type);
891 /* Type functions. */
893 static struct ofproto_dpif *
894 lookup_ofproto_dpif_by_port_name(const char *name)
896 struct ofproto_dpif *ofproto;
898 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
899 if (sset_contains(&ofproto->ports, name)) {
908 type_run(const char *type)
910 struct dpif_backer *backer;
914 backer = shash_find_data(&all_dpif_backers, type);
916 /* This is not necessarily a problem, since backers are only
917 * created on demand. */
921 dpif_run(backer->dpif);
923 if (backer->need_revalidate
924 || !tag_set_is_empty(&backer->revalidate_set)) {
925 struct tag_set revalidate_set = backer->revalidate_set;
926 bool need_revalidate = backer->need_revalidate;
927 struct ofproto_dpif *ofproto;
928 struct simap_node *node;
929 struct simap tmp_backers;
931 /* Handle tunnel garbage collection. */
932 simap_init(&tmp_backers);
933 simap_swap(&backer->tnl_backers, &tmp_backers);
935 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
936 struct ofport_dpif *iter;
938 if (backer != ofproto->backer) {
942 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
945 if (!iter->tnl_port) {
949 dp_port = netdev_vport_get_dpif_port(iter->up.netdev);
950 node = simap_find(&tmp_backers, dp_port);
952 simap_put(&backer->tnl_backers, dp_port, node->data);
953 simap_delete(&tmp_backers, node);
954 node = simap_find(&backer->tnl_backers, dp_port);
956 node = simap_find(&backer->tnl_backers, dp_port);
958 uint32_t odp_port = UINT32_MAX;
960 if (!dpif_port_add(backer->dpif, iter->up.netdev,
962 simap_put(&backer->tnl_backers, dp_port, odp_port);
963 node = simap_find(&backer->tnl_backers, dp_port);
968 iter->odp_port = node ? node->data : OVSP_NONE;
969 if (tnl_port_reconfigure(&iter->up, iter->odp_port,
971 backer->need_revalidate = REV_RECONFIGURE;
976 SIMAP_FOR_EACH (node, &tmp_backers) {
977 dpif_port_del(backer->dpif, node->data);
979 simap_destroy(&tmp_backers);
981 switch (backer->need_revalidate) {
982 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
983 case REV_STP: COVERAGE_INC(rev_stp); break;
984 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
985 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
986 case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
989 if (backer->need_revalidate) {
990 /* Clear the drop_keys in case we should now be accepting some
991 * formerly dropped flows. */
992 drop_key_clear(backer);
995 /* Clear the revalidation flags. */
996 tag_set_init(&backer->revalidate_set);
997 backer->need_revalidate = 0;
999 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1000 struct facet *facet, *next;
1002 if (ofproto->backer != backer) {
1006 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
1008 || tag_set_intersects(&revalidate_set, facet->tags)) {
1009 facet_revalidate(facet);
1015 if (timer_expired(&backer->next_expiration)) {
1016 int delay = expire(backer);
1017 timer_set_duration(&backer->next_expiration, delay);
1020 /* Check for port changes in the dpif. */
1021 while ((error = dpif_port_poll(backer->dpif, &devname)) == 0) {
1022 struct ofproto_dpif *ofproto;
1023 struct dpif_port port;
1025 /* Don't report on the datapath's device. */
1026 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
1030 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
1031 &all_ofproto_dpifs) {
1032 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
1037 ofproto = lookup_ofproto_dpif_by_port_name(devname);
1038 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
1039 /* The port was removed. If we know the datapath,
1040 * report it through poll_set(). If we don't, it may be
1041 * notifying us of a removal we initiated, so ignore it.
1042 * If there's a pending ENOBUFS, let it stand, since
1043 * everything will be reevaluated. */
1044 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
1045 sset_add(&ofproto->port_poll_set, devname);
1046 ofproto->port_poll_errno = 0;
1048 } else if (!ofproto) {
1049 /* The port was added, but we don't know with which
1050 * ofproto we should associate it. Delete it. */
1051 dpif_port_del(backer->dpif, port.port_no);
1053 dpif_port_destroy(&port);
1059 if (error != EAGAIN) {
1060 struct ofproto_dpif *ofproto;
1062 /* There was some sort of error, so propagate it to all
1063 * ofprotos that use this backer. */
1064 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
1065 &all_ofproto_dpifs) {
1066 if (ofproto->backer == backer) {
1067 sset_clear(&ofproto->port_poll_set);
1068 ofproto->port_poll_errno = error;
1077 type_run_fast(const char *type)
1079 struct dpif_backer *backer;
1082 backer = shash_find_data(&all_dpif_backers, type);
1084 /* This is not necessarily a problem, since backers are only
1085 * created on demand. */
1089 /* Handle one or more batches of upcalls, until there's nothing left to do
1090 * or until we do a fixed total amount of work.
1092 * We do work in batches because it can be much cheaper to set up a number
1093 * of flows and fire off their patches all at once. We do multiple batches
1094 * because in some cases handling a packet can cause another packet to be
1095 * queued almost immediately as part of the return flow. Both
1096 * optimizations can make major improvements on some benchmarks and
1097 * presumably for real traffic as well. */
1099 while (work < FLOW_MISS_MAX_BATCH) {
1100 int retval = handle_upcalls(backer, FLOW_MISS_MAX_BATCH - work);
1111 type_wait(const char *type)
1113 struct dpif_backer *backer;
1115 backer = shash_find_data(&all_dpif_backers, type);
1117 /* This is not necessarily a problem, since backers are only
1118 * created on demand. */
1122 timer_wait(&backer->next_expiration);
1125 /* Basic life-cycle. */
1127 static int add_internal_flows(struct ofproto_dpif *);
1129 static struct ofproto *
1132 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
1133 return &ofproto->up;
1137 dealloc(struct ofproto *ofproto_)
1139 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1144 close_dpif_backer(struct dpif_backer *backer)
1146 struct shash_node *node;
1148 ovs_assert(backer->refcount > 0);
1150 if (--backer->refcount) {
1154 drop_key_clear(backer);
1155 hmap_destroy(&backer->drop_keys);
1157 simap_destroy(&backer->tnl_backers);
1158 hmap_destroy(&backer->odp_to_ofport_map);
1159 node = shash_find(&all_dpif_backers, backer->type);
1161 shash_delete(&all_dpif_backers, node);
1162 dpif_close(backer->dpif);
1167 /* Datapath port slated for removal from datapath. */
1168 struct odp_garbage {
1169 struct list list_node;
1174 open_dpif_backer(const char *type, struct dpif_backer **backerp)
1176 struct dpif_backer *backer;
1177 struct dpif_port_dump port_dump;
1178 struct dpif_port port;
1179 struct shash_node *node;
1180 struct list garbage_list;
1181 struct odp_garbage *garbage, *next;
1187 backer = shash_find_data(&all_dpif_backers, type);
1194 backer_name = xasprintf("ovs-%s", type);
1196 /* Remove any existing datapaths, since we assume we're the only
1197 * userspace controlling the datapath. */
1199 dp_enumerate_names(type, &names);
1200 SSET_FOR_EACH(name, &names) {
1201 struct dpif *old_dpif;
1203 /* Don't remove our backer if it exists. */
1204 if (!strcmp(name, backer_name)) {
1208 if (dpif_open(name, type, &old_dpif)) {
1209 VLOG_WARN("couldn't open old datapath %s to remove it", name);
1211 dpif_delete(old_dpif);
1212 dpif_close(old_dpif);
1215 sset_destroy(&names);
1217 backer = xmalloc(sizeof *backer);
1219 error = dpif_create_and_open(backer_name, type, &backer->dpif);
1222 VLOG_ERR("failed to open datapath of type %s: %s", type,
1228 backer->type = xstrdup(type);
1229 backer->refcount = 1;
1230 hmap_init(&backer->odp_to_ofport_map);
1231 hmap_init(&backer->drop_keys);
1232 timer_set_duration(&backer->next_expiration, 1000);
1233 backer->need_revalidate = 0;
1234 simap_init(&backer->tnl_backers);
1235 tag_set_init(&backer->revalidate_set);
1238 dpif_flow_flush(backer->dpif);
1240 /* Loop through the ports already on the datapath and remove any
1241 * that we don't need anymore. */
1242 list_init(&garbage_list);
1243 dpif_port_dump_start(&port_dump, backer->dpif);
1244 while (dpif_port_dump_next(&port_dump, &port)) {
1245 node = shash_find(&init_ofp_ports, port.name);
1246 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
1247 garbage = xmalloc(sizeof *garbage);
1248 garbage->odp_port = port.port_no;
1249 list_push_front(&garbage_list, &garbage->list_node);
1252 dpif_port_dump_done(&port_dump);
1254 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
1255 dpif_port_del(backer->dpif, garbage->odp_port);
1256 list_remove(&garbage->list_node);
1260 shash_add(&all_dpif_backers, type, backer);
1262 error = dpif_recv_set(backer->dpif, true);
1264 VLOG_ERR("failed to listen on datapath of type %s: %s",
1265 type, strerror(error));
1266 close_dpif_backer(backer);
1274 construct(struct ofproto *ofproto_)
1276 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1277 struct shash_node *node, *next;
1282 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1287 max_ports = dpif_get_max_ports(ofproto->backer->dpif);
1288 ofproto_init_max_ports(ofproto_, MIN(max_ports, OFPP_MAX));
1290 ofproto->n_matches = 0;
1292 ofproto->netflow = NULL;
1293 ofproto->sflow = NULL;
1294 ofproto->stp = NULL;
1295 hmap_init(&ofproto->bundles);
1296 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1297 for (i = 0; i < MAX_MIRRORS; i++) {
1298 ofproto->mirrors[i] = NULL;
1300 ofproto->has_bonded_bundles = false;
1302 hmap_init(&ofproto->facets);
1303 hmap_init(&ofproto->subfacets);
1304 ofproto->governor = NULL;
1305 ofproto->consistency_rl = LLONG_MIN;
1307 for (i = 0; i < N_TABLES; i++) {
1308 struct table_dpif *table = &ofproto->tables[i];
1310 table->catchall_table = NULL;
1311 table->other_table = NULL;
1312 table->basis = random_uint32();
1315 list_init(&ofproto->completions);
1317 ofproto_dpif_unixctl_init();
1319 ofproto->has_mirrors = false;
1320 ofproto->has_bundle_action = false;
1322 hmap_init(&ofproto->vlandev_map);
1323 hmap_init(&ofproto->realdev_vid_map);
1325 sset_init(&ofproto->ports);
1326 sset_init(&ofproto->ghost_ports);
1327 sset_init(&ofproto->port_poll_set);
1328 ofproto->port_poll_errno = 0;
1330 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1331 struct iface_hint *iface_hint = node->data;
1333 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1334 /* Check if the datapath already has this port. */
1335 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1336 sset_add(&ofproto->ports, node->name);
1339 free(iface_hint->br_name);
1340 free(iface_hint->br_type);
1342 shash_delete(&init_ofp_ports, node);
1346 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1347 hash_string(ofproto->up.name, 0));
1348 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1350 ofproto_init_tables(ofproto_, N_TABLES);
1351 error = add_internal_flows(ofproto);
1352 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1355 ofproto->n_missed = 0;
1357 ofproto->max_n_subfacet = 0;
1358 ofproto->created = time_msec();
1359 ofproto->last_minute = ofproto->created;
1360 memset(&ofproto->hourly, 0, sizeof ofproto->hourly);
1361 memset(&ofproto->daily, 0, sizeof ofproto->daily);
1362 ofproto->subfacet_add_count = 0;
1363 ofproto->subfacet_del_count = 0;
1364 ofproto->total_subfacet_add_count = 0;
1365 ofproto->total_subfacet_del_count = 0;
1366 ofproto->total_subfacet_life_span = 0;
1367 ofproto->total_subfacet_count = 0;
1368 ofproto->n_update_stats = 0;
1374 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1375 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1377 struct ofputil_flow_mod fm;
1380 match_init_catchall(&fm.match);
1382 match_set_reg(&fm.match, 0, id);
1383 fm.new_cookie = htonll(0);
1384 fm.cookie = htonll(0);
1385 fm.cookie_mask = htonll(0);
1386 fm.table_id = TBL_INTERNAL;
1387 fm.command = OFPFC_ADD;
1388 fm.idle_timeout = 0;
1389 fm.hard_timeout = 0;
1393 fm.ofpacts = ofpacts->data;
1394 fm.ofpacts_len = ofpacts->size;
1396 error = ofproto_flow_mod(&ofproto->up, &fm);
1398 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1399 id, ofperr_to_string(error));
1403 *rulep = rule_dpif_lookup__(ofproto, &fm.match.flow, TBL_INTERNAL);
1404 ovs_assert(*rulep != NULL);
1410 add_internal_flows(struct ofproto_dpif *ofproto)
1412 struct ofpact_controller *controller;
1413 uint64_t ofpacts_stub[128 / 8];
1414 struct ofpbuf ofpacts;
1418 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1421 controller = ofpact_put_CONTROLLER(&ofpacts);
1422 controller->max_len = UINT16_MAX;
1423 controller->controller_id = 0;
1424 controller->reason = OFPR_NO_MATCH;
1425 ofpact_pad(&ofpacts);
1427 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1432 ofpbuf_clear(&ofpacts);
1433 error = add_internal_flow(ofproto, id++, &ofpacts,
1434 &ofproto->no_packet_in_rule);
1439 complete_operations(struct ofproto_dpif *ofproto)
1441 struct dpif_completion *c, *next;
1443 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
1444 ofoperation_complete(c->op, 0);
1445 list_remove(&c->list_node);
1451 destruct(struct ofproto *ofproto_)
1453 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1454 struct rule_dpif *rule, *next_rule;
1455 struct oftable *table;
1458 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1459 complete_operations(ofproto);
1461 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1462 struct cls_cursor cursor;
1464 cls_cursor_init(&cursor, &table->cls, NULL);
1465 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1466 ofproto_rule_destroy(&rule->up);
1470 for (i = 0; i < MAX_MIRRORS; i++) {
1471 mirror_destroy(ofproto->mirrors[i]);
1474 netflow_destroy(ofproto->netflow);
1475 dpif_sflow_destroy(ofproto->sflow);
1476 hmap_destroy(&ofproto->bundles);
1477 mac_learning_destroy(ofproto->ml);
1479 hmap_destroy(&ofproto->facets);
1480 hmap_destroy(&ofproto->subfacets);
1481 governor_destroy(ofproto->governor);
1483 hmap_destroy(&ofproto->vlandev_map);
1484 hmap_destroy(&ofproto->realdev_vid_map);
1486 sset_destroy(&ofproto->ports);
1487 sset_destroy(&ofproto->ghost_ports);
1488 sset_destroy(&ofproto->port_poll_set);
1490 close_dpif_backer(ofproto->backer);
1494 run_fast(struct ofproto *ofproto_)
1496 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1497 struct ofport_dpif *ofport;
1499 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1500 port_run_fast(ofport);
1507 run(struct ofproto *ofproto_)
1509 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1510 struct ofport_dpif *ofport;
1511 struct ofbundle *bundle;
1515 complete_operations(ofproto);
1518 error = run_fast(ofproto_);
1523 if (ofproto->netflow) {
1524 if (netflow_run(ofproto->netflow)) {
1525 send_netflow_active_timeouts(ofproto);
1528 if (ofproto->sflow) {
1529 dpif_sflow_run(ofproto->sflow);
1532 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1535 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1540 mac_learning_run(ofproto->ml, &ofproto->backer->revalidate_set);
1542 /* Check the consistency of a random facet, to aid debugging. */
1543 if (time_msec() >= ofproto->consistency_rl
1544 && !hmap_is_empty(&ofproto->facets)
1545 && !ofproto->backer->need_revalidate) {
1546 struct facet *facet;
1548 ofproto->consistency_rl = time_msec() + 250;
1550 facet = CONTAINER_OF(hmap_random_node(&ofproto->facets),
1551 struct facet, hmap_node);
1552 if (!tag_set_intersects(&ofproto->backer->revalidate_set,
1554 if (!facet_check_consistency(facet)) {
1555 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
1560 if (ofproto->governor) {
1563 governor_run(ofproto->governor);
1565 /* If the governor has shrunk to its minimum size and the number of
1566 * subfacets has dwindled, then drop the governor entirely.
1568 * For hysteresis, the number of subfacets to drop the governor is
1569 * smaller than the number needed to trigger its creation. */
1570 n_subfacets = hmap_count(&ofproto->subfacets);
1571 if (n_subfacets * 4 < ofproto->up.flow_eviction_threshold
1572 && governor_is_idle(ofproto->governor)) {
1573 governor_destroy(ofproto->governor);
1574 ofproto->governor = NULL;
1582 wait(struct ofproto *ofproto_)
1584 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1585 struct ofport_dpif *ofport;
1586 struct ofbundle *bundle;
1588 if (!clogged && !list_is_empty(&ofproto->completions)) {
1589 poll_immediate_wake();
1592 dpif_wait(ofproto->backer->dpif);
1593 dpif_recv_wait(ofproto->backer->dpif);
1594 if (ofproto->sflow) {
1595 dpif_sflow_wait(ofproto->sflow);
1597 if (!tag_set_is_empty(&ofproto->backer->revalidate_set)) {
1598 poll_immediate_wake();
1600 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1603 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1604 bundle_wait(bundle);
1606 if (ofproto->netflow) {
1607 netflow_wait(ofproto->netflow);
1609 mac_learning_wait(ofproto->ml);
1611 if (ofproto->backer->need_revalidate) {
1612 /* Shouldn't happen, but if it does just go around again. */
1613 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1614 poll_immediate_wake();
1616 if (ofproto->governor) {
1617 governor_wait(ofproto->governor);
1622 get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1624 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1626 simap_increase(usage, "facets", hmap_count(&ofproto->facets));
1627 simap_increase(usage, "subfacets", hmap_count(&ofproto->subfacets));
1631 flush(struct ofproto *ofproto_)
1633 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1634 struct subfacet *subfacet, *next_subfacet;
1635 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
1639 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
1640 &ofproto->subfacets) {
1641 if (subfacet->path != SF_NOT_INSTALLED) {
1642 batch[n_batch++] = subfacet;
1643 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
1644 subfacet_destroy_batch(ofproto, batch, n_batch);
1648 subfacet_destroy(subfacet);
1653 subfacet_destroy_batch(ofproto, batch, n_batch);
1658 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1659 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1661 *arp_match_ip = true;
1662 *actions = (OFPUTIL_A_OUTPUT |
1663 OFPUTIL_A_SET_VLAN_VID |
1664 OFPUTIL_A_SET_VLAN_PCP |
1665 OFPUTIL_A_STRIP_VLAN |
1666 OFPUTIL_A_SET_DL_SRC |
1667 OFPUTIL_A_SET_DL_DST |
1668 OFPUTIL_A_SET_NW_SRC |
1669 OFPUTIL_A_SET_NW_DST |
1670 OFPUTIL_A_SET_NW_TOS |
1671 OFPUTIL_A_SET_TP_SRC |
1672 OFPUTIL_A_SET_TP_DST |
1677 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1679 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1680 struct dpif_dp_stats s;
1682 strcpy(ots->name, "classifier");
1684 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1686 ots->lookup_count = htonll(s.n_hit + s.n_missed);
1687 ots->matched_count = htonll(s.n_hit + ofproto->n_matches);
1690 static struct ofport *
1693 struct ofport_dpif *port = xmalloc(sizeof *port);
1698 port_dealloc(struct ofport *port_)
1700 struct ofport_dpif *port = ofport_dpif_cast(port_);
1705 port_construct(struct ofport *port_)
1707 struct ofport_dpif *port = ofport_dpif_cast(port_);
1708 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1709 const struct netdev *netdev = port->up.netdev;
1710 struct dpif_port dpif_port;
1713 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1714 port->bundle = NULL;
1716 port->tag = tag_create_random();
1717 port->may_enable = true;
1718 port->stp_port = NULL;
1719 port->stp_state = STP_DISABLED;
1720 port->tnl_port = NULL;
1721 hmap_init(&port->priorities);
1722 port->realdev_ofp_port = 0;
1723 port->vlandev_vid = 0;
1724 port->carrier_seq = netdev_get_carrier_resets(netdev);
1726 if (netdev_vport_is_patch(netdev)) {
1727 /* XXX By bailing out here, we don't do required sFlow work. */
1728 port->odp_port = OVSP_NONE;
1732 error = dpif_port_query_by_name(ofproto->backer->dpif,
1733 netdev_vport_get_dpif_port(netdev),
1739 port->odp_port = dpif_port.port_no;
1741 if (netdev_get_tunnel_config(netdev)) {
1742 port->tnl_port = tnl_port_add(&port->up, port->odp_port);
1744 /* Sanity-check that a mapping doesn't already exist. This
1745 * shouldn't happen for non-tunnel ports. */
1746 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1747 VLOG_ERR("port %s already has an OpenFlow port number",
1749 dpif_port_destroy(&dpif_port);
1753 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1754 hash_int(port->odp_port, 0));
1756 dpif_port_destroy(&dpif_port);
1758 if (ofproto->sflow) {
1759 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1766 port_destruct(struct ofport *port_)
1768 struct ofport_dpif *port = ofport_dpif_cast(port_);
1769 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1770 const char *dp_port_name = netdev_vport_get_dpif_port(port->up.netdev);
1771 const char *devname = netdev_get_name(port->up.netdev);
1773 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1774 /* The underlying device is still there, so delete it. This
1775 * happens when the ofproto is being destroyed, since the caller
1776 * assumes that removal of attached ports will happen as part of
1778 if (!port->tnl_port) {
1779 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1781 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1784 if (port->odp_port != OVSP_NONE && !port->tnl_port) {
1785 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1788 tnl_port_del(port->tnl_port);
1789 sset_find_and_delete(&ofproto->ports, devname);
1790 sset_find_and_delete(&ofproto->ghost_ports, devname);
1791 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1792 bundle_remove(port_);
1793 set_cfm(port_, NULL);
1794 if (ofproto->sflow) {
1795 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1798 ofport_clear_priorities(port);
1799 hmap_destroy(&port->priorities);
1803 port_modified(struct ofport *port_)
1805 struct ofport_dpif *port = ofport_dpif_cast(port_);
1807 if (port->bundle && port->bundle->bond) {
1808 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1813 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1815 struct ofport_dpif *port = ofport_dpif_cast(port_);
1816 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1817 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1819 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1820 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1821 OFPUTIL_PC_NO_PACKET_IN)) {
1822 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1824 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1825 bundle_update(port->bundle);
1831 set_sflow(struct ofproto *ofproto_,
1832 const struct ofproto_sflow_options *sflow_options)
1834 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1835 struct dpif_sflow *ds = ofproto->sflow;
1837 if (sflow_options) {
1839 struct ofport_dpif *ofport;
1841 ds = ofproto->sflow = dpif_sflow_create();
1842 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1843 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1845 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1847 dpif_sflow_set_options(ds, sflow_options);
1850 dpif_sflow_destroy(ds);
1851 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1852 ofproto->sflow = NULL;
1859 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1861 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1868 struct ofproto_dpif *ofproto;
1870 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1871 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1872 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
1875 if (cfm_configure(ofport->cfm, s)) {
1881 cfm_destroy(ofport->cfm);
1887 get_cfm_status(const struct ofport *ofport_,
1888 struct ofproto_cfm_status *status)
1890 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1893 status->faults = cfm_get_fault(ofport->cfm);
1894 status->remote_opstate = cfm_get_opup(ofport->cfm);
1895 status->health = cfm_get_health(ofport->cfm);
1896 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1903 /* Spanning Tree. */
1906 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1908 struct ofproto_dpif *ofproto = ofproto_;
1909 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1910 struct ofport_dpif *ofport;
1912 ofport = stp_port_get_aux(sp);
1914 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1915 ofproto->up.name, port_num);
1917 struct eth_header *eth = pkt->l2;
1919 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1920 if (eth_addr_is_zero(eth->eth_src)) {
1921 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1922 "with unknown MAC", ofproto->up.name, port_num);
1924 send_packet(ofport, pkt);
1930 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1932 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1934 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1936 /* Only revalidate flows if the configuration changed. */
1937 if (!s != !ofproto->stp) {
1938 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1942 if (!ofproto->stp) {
1943 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1944 send_bpdu_cb, ofproto);
1945 ofproto->stp_last_tick = time_msec();
1948 stp_set_bridge_id(ofproto->stp, s->system_id);
1949 stp_set_bridge_priority(ofproto->stp, s->priority);
1950 stp_set_hello_time(ofproto->stp, s->hello_time);
1951 stp_set_max_age(ofproto->stp, s->max_age);
1952 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1954 struct ofport *ofport;
1956 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1957 set_stp_port(ofport, NULL);
1960 stp_destroy(ofproto->stp);
1961 ofproto->stp = NULL;
1968 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1970 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1974 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1975 s->designated_root = stp_get_designated_root(ofproto->stp);
1976 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1985 update_stp_port_state(struct ofport_dpif *ofport)
1987 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1988 enum stp_state state;
1990 /* Figure out new state. */
1991 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1995 if (ofport->stp_state != state) {
1996 enum ofputil_port_state of_state;
1999 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
2000 netdev_get_name(ofport->up.netdev),
2001 stp_state_name(ofport->stp_state),
2002 stp_state_name(state));
2003 if (stp_learn_in_state(ofport->stp_state)
2004 != stp_learn_in_state(state)) {
2005 /* xxx Learning action flows should also be flushed. */
2006 mac_learning_flush(ofproto->ml,
2007 &ofproto->backer->revalidate_set);
2009 fwd_change = stp_forward_in_state(ofport->stp_state)
2010 != stp_forward_in_state(state);
2012 ofproto->backer->need_revalidate = REV_STP;
2013 ofport->stp_state = state;
2014 ofport->stp_state_entered = time_msec();
2016 if (fwd_change && ofport->bundle) {
2017 bundle_update(ofport->bundle);
2020 /* Update the STP state bits in the OpenFlow port description. */
2021 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2022 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2023 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2024 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2025 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2027 ofproto_port_set_state(&ofport->up, of_state);
2031 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2032 * caller is responsible for assigning STP port numbers and ensuring
2033 * there are no duplicates. */
2035 set_stp_port(struct ofport *ofport_,
2036 const struct ofproto_port_stp_settings *s)
2038 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2039 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2040 struct stp_port *sp = ofport->stp_port;
2042 if (!s || !s->enable) {
2044 ofport->stp_port = NULL;
2045 stp_port_disable(sp);
2046 update_stp_port_state(ofport);
2049 } else if (sp && stp_port_no(sp) != s->port_num
2050 && ofport == stp_port_get_aux(sp)) {
2051 /* The port-id changed, so disable the old one if it's not
2052 * already in use by another port. */
2053 stp_port_disable(sp);
2056 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2057 stp_port_enable(sp);
2059 stp_port_set_aux(sp, ofport);
2060 stp_port_set_priority(sp, s->priority);
2061 stp_port_set_path_cost(sp, s->path_cost);
2063 update_stp_port_state(ofport);
2069 get_stp_port_status(struct ofport *ofport_,
2070 struct ofproto_port_stp_status *s)
2072 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2073 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2074 struct stp_port *sp = ofport->stp_port;
2076 if (!ofproto->stp || !sp) {
2082 s->port_id = stp_port_get_id(sp);
2083 s->state = stp_port_get_state(sp);
2084 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2085 s->role = stp_port_get_role(sp);
2086 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2092 stp_run(struct ofproto_dpif *ofproto)
2095 long long int now = time_msec();
2096 long long int elapsed = now - ofproto->stp_last_tick;
2097 struct stp_port *sp;
2100 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2101 ofproto->stp_last_tick = now;
2103 while (stp_get_changed_port(ofproto->stp, &sp)) {
2104 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2107 update_stp_port_state(ofport);
2111 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2112 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
2118 stp_wait(struct ofproto_dpif *ofproto)
2121 poll_timer_wait(1000);
2125 /* Returns true if STP should process 'flow'. */
2127 stp_should_process_flow(const struct flow *flow)
2129 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
2133 stp_process_packet(const struct ofport_dpif *ofport,
2134 const struct ofpbuf *packet)
2136 struct ofpbuf payload = *packet;
2137 struct eth_header *eth = payload.data;
2138 struct stp_port *sp = ofport->stp_port;
2140 /* Sink packets on ports that have STP disabled when the bridge has
2142 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
2146 /* Trim off padding on payload. */
2147 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
2148 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
2151 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
2152 stp_received_bpdu(sp, payload.data, payload.size);
2156 static struct priority_to_dscp *
2157 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
2159 struct priority_to_dscp *pdscp;
2162 hash = hash_int(priority, 0);
2163 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
2164 if (pdscp->priority == priority) {
2172 ofport_clear_priorities(struct ofport_dpif *ofport)
2174 struct priority_to_dscp *pdscp, *next;
2176 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
2177 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
2183 set_queues(struct ofport *ofport_,
2184 const struct ofproto_port_queue *qdscp_list,
2187 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2188 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2189 struct hmap new = HMAP_INITIALIZER(&new);
2192 for (i = 0; i < n_qdscp; i++) {
2193 struct priority_to_dscp *pdscp;
2197 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
2198 if (dpif_queue_to_priority(ofproto->backer->dpif, qdscp_list[i].queue,
2203 pdscp = get_priority(ofport, priority);
2205 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
2207 pdscp = xmalloc(sizeof *pdscp);
2208 pdscp->priority = priority;
2210 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2213 if (pdscp->dscp != dscp) {
2215 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2218 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
2221 if (!hmap_is_empty(&ofport->priorities)) {
2222 ofport_clear_priorities(ofport);
2223 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2226 hmap_swap(&new, &ofport->priorities);
2234 /* Expires all MAC learning entries associated with 'bundle' and forces its
2235 * ofproto to revalidate every flow.
2237 * Normally MAC learning entries are removed only from the ofproto associated
2238 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2239 * are removed from every ofproto. When patch ports and SLB bonds are in use
2240 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2241 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2242 * with the host from which it migrated. */
2244 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2246 struct ofproto_dpif *ofproto = bundle->ofproto;
2247 struct mac_learning *ml = ofproto->ml;
2248 struct mac_entry *mac, *next_mac;
2250 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2251 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2252 if (mac->port.p == bundle) {
2254 struct ofproto_dpif *o;
2256 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2258 struct mac_entry *e;
2260 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan,
2263 mac_learning_expire(o->ml, e);
2269 mac_learning_expire(ml, mac);
2274 static struct ofbundle *
2275 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2277 struct ofbundle *bundle;
2279 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2280 &ofproto->bundles) {
2281 if (bundle->aux == aux) {
2288 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
2289 * ones that are found to 'bundles'. */
2291 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
2292 void **auxes, size_t n_auxes,
2293 struct hmapx *bundles)
2297 hmapx_init(bundles);
2298 for (i = 0; i < n_auxes; i++) {
2299 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
2301 hmapx_add(bundles, bundle);
2307 bundle_update(struct ofbundle *bundle)
2309 struct ofport_dpif *port;
2311 bundle->floodable = true;
2312 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2313 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2314 || !stp_forward_in_state(port->stp_state)) {
2315 bundle->floodable = false;
2322 bundle_del_port(struct ofport_dpif *port)
2324 struct ofbundle *bundle = port->bundle;
2326 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2328 list_remove(&port->bundle_node);
2329 port->bundle = NULL;
2332 lacp_slave_unregister(bundle->lacp, port);
2335 bond_slave_unregister(bundle->bond, port);
2338 bundle_update(bundle);
2342 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
2343 struct lacp_slave_settings *lacp)
2345 struct ofport_dpif *port;
2347 port = get_ofp_port(bundle->ofproto, ofp_port);
2352 if (port->bundle != bundle) {
2353 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2355 bundle_del_port(port);
2358 port->bundle = bundle;
2359 list_push_back(&bundle->ports, &port->bundle_node);
2360 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2361 || !stp_forward_in_state(port->stp_state)) {
2362 bundle->floodable = false;
2366 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2367 lacp_slave_register(bundle->lacp, port, lacp);
2374 bundle_destroy(struct ofbundle *bundle)
2376 struct ofproto_dpif *ofproto;
2377 struct ofport_dpif *port, *next_port;
2384 ofproto = bundle->ofproto;
2385 for (i = 0; i < MAX_MIRRORS; i++) {
2386 struct ofmirror *m = ofproto->mirrors[i];
2388 if (m->out == bundle) {
2390 } else if (hmapx_find_and_delete(&m->srcs, bundle)
2391 || hmapx_find_and_delete(&m->dsts, bundle)) {
2392 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2397 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2398 bundle_del_port(port);
2401 bundle_flush_macs(bundle, true);
2402 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2404 free(bundle->trunks);
2405 lacp_destroy(bundle->lacp);
2406 bond_destroy(bundle->bond);
2411 bundle_set(struct ofproto *ofproto_, void *aux,
2412 const struct ofproto_bundle_settings *s)
2414 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2415 bool need_flush = false;
2416 struct ofport_dpif *port;
2417 struct ofbundle *bundle;
2418 unsigned long *trunks;
2424 bundle_destroy(bundle_lookup(ofproto, aux));
2428 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2429 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2431 bundle = bundle_lookup(ofproto, aux);
2433 bundle = xmalloc(sizeof *bundle);
2435 bundle->ofproto = ofproto;
2436 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2437 hash_pointer(aux, 0));
2439 bundle->name = NULL;
2441 list_init(&bundle->ports);
2442 bundle->vlan_mode = PORT_VLAN_TRUNK;
2444 bundle->trunks = NULL;
2445 bundle->use_priority_tags = s->use_priority_tags;
2446 bundle->lacp = NULL;
2447 bundle->bond = NULL;
2449 bundle->floodable = true;
2451 bundle->src_mirrors = 0;
2452 bundle->dst_mirrors = 0;
2453 bundle->mirror_out = 0;
2456 if (!bundle->name || strcmp(s->name, bundle->name)) {
2458 bundle->name = xstrdup(s->name);
2463 if (!bundle->lacp) {
2464 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2465 bundle->lacp = lacp_create();
2467 lacp_configure(bundle->lacp, s->lacp);
2469 lacp_destroy(bundle->lacp);
2470 bundle->lacp = NULL;
2473 /* Update set of ports. */
2475 for (i = 0; i < s->n_slaves; i++) {
2476 if (!bundle_add_port(bundle, s->slaves[i],
2477 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2481 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2482 struct ofport_dpif *next_port;
2484 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2485 for (i = 0; i < s->n_slaves; i++) {
2486 if (s->slaves[i] == port->up.ofp_port) {
2491 bundle_del_port(port);
2495 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2497 if (list_is_empty(&bundle->ports)) {
2498 bundle_destroy(bundle);
2502 /* Set VLAN tagging mode */
2503 if (s->vlan_mode != bundle->vlan_mode
2504 || s->use_priority_tags != bundle->use_priority_tags) {
2505 bundle->vlan_mode = s->vlan_mode;
2506 bundle->use_priority_tags = s->use_priority_tags;
2511 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2512 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2514 if (vlan != bundle->vlan) {
2515 bundle->vlan = vlan;
2519 /* Get trunked VLANs. */
2520 switch (s->vlan_mode) {
2521 case PORT_VLAN_ACCESS:
2525 case PORT_VLAN_TRUNK:
2526 trunks = CONST_CAST(unsigned long *, s->trunks);
2529 case PORT_VLAN_NATIVE_UNTAGGED:
2530 case PORT_VLAN_NATIVE_TAGGED:
2531 if (vlan != 0 && (!s->trunks
2532 || !bitmap_is_set(s->trunks, vlan)
2533 || bitmap_is_set(s->trunks, 0))) {
2534 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2536 trunks = bitmap_clone(s->trunks, 4096);
2538 trunks = bitmap_allocate1(4096);
2540 bitmap_set1(trunks, vlan);
2541 bitmap_set0(trunks, 0);
2543 trunks = CONST_CAST(unsigned long *, s->trunks);
2550 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2551 free(bundle->trunks);
2552 if (trunks == s->trunks) {
2553 bundle->trunks = vlan_bitmap_clone(trunks);
2555 bundle->trunks = trunks;
2560 if (trunks != s->trunks) {
2565 if (!list_is_short(&bundle->ports)) {
2566 bundle->ofproto->has_bonded_bundles = true;
2568 if (bond_reconfigure(bundle->bond, s->bond)) {
2569 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2572 bundle->bond = bond_create(s->bond);
2573 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2576 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2577 bond_slave_register(bundle->bond, port, port->up.netdev);
2580 bond_destroy(bundle->bond);
2581 bundle->bond = NULL;
2584 /* If we changed something that would affect MAC learning, un-learn
2585 * everything on this port and force flow revalidation. */
2587 bundle_flush_macs(bundle, false);
2594 bundle_remove(struct ofport *port_)
2596 struct ofport_dpif *port = ofport_dpif_cast(port_);
2597 struct ofbundle *bundle = port->bundle;
2600 bundle_del_port(port);
2601 if (list_is_empty(&bundle->ports)) {
2602 bundle_destroy(bundle);
2603 } else if (list_is_short(&bundle->ports)) {
2604 bond_destroy(bundle->bond);
2605 bundle->bond = NULL;
2611 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2613 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2614 struct ofport_dpif *port = port_;
2615 uint8_t ea[ETH_ADDR_LEN];
2618 error = netdev_get_etheraddr(port->up.netdev, ea);
2620 struct ofpbuf packet;
2623 ofpbuf_init(&packet, 0);
2624 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2626 memcpy(packet_pdu, pdu, pdu_size);
2628 send_packet(port, &packet);
2629 ofpbuf_uninit(&packet);
2631 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2632 "%s (%s)", port->bundle->name,
2633 netdev_get_name(port->up.netdev), strerror(error));
2638 bundle_send_learning_packets(struct ofbundle *bundle)
2640 struct ofproto_dpif *ofproto = bundle->ofproto;
2641 int error, n_packets, n_errors;
2642 struct mac_entry *e;
2644 error = n_packets = n_errors = 0;
2645 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2646 if (e->port.p != bundle) {
2647 struct ofpbuf *learning_packet;
2648 struct ofport_dpif *port;
2652 /* The assignment to "port" is unnecessary but makes "grep"ing for
2653 * struct ofport_dpif more effective. */
2654 learning_packet = bond_compose_learning_packet(bundle->bond,
2658 ret = send_packet(port, learning_packet);
2659 ofpbuf_delete(learning_packet);
2669 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2670 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2671 "packets, last error was: %s",
2672 bundle->name, n_errors, n_packets, strerror(error));
2674 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2675 bundle->name, n_packets);
2680 bundle_run(struct ofbundle *bundle)
2683 lacp_run(bundle->lacp, send_pdu_cb);
2686 struct ofport_dpif *port;
2688 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2689 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2692 bond_run(bundle->bond, &bundle->ofproto->backer->revalidate_set,
2693 lacp_status(bundle->lacp));
2694 if (bond_should_send_learning_packets(bundle->bond)) {
2695 bundle_send_learning_packets(bundle);
2701 bundle_wait(struct ofbundle *bundle)
2704 lacp_wait(bundle->lacp);
2707 bond_wait(bundle->bond);
2714 mirror_scan(struct ofproto_dpif *ofproto)
2718 for (idx = 0; idx < MAX_MIRRORS; idx++) {
2719 if (!ofproto->mirrors[idx]) {
2726 static struct ofmirror *
2727 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
2731 for (i = 0; i < MAX_MIRRORS; i++) {
2732 struct ofmirror *mirror = ofproto->mirrors[i];
2733 if (mirror && mirror->aux == aux) {
2741 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
2743 mirror_update_dups(struct ofproto_dpif *ofproto)
2747 for (i = 0; i < MAX_MIRRORS; i++) {
2748 struct ofmirror *m = ofproto->mirrors[i];
2751 m->dup_mirrors = MIRROR_MASK_C(1) << i;
2755 for (i = 0; i < MAX_MIRRORS; i++) {
2756 struct ofmirror *m1 = ofproto->mirrors[i];
2763 for (j = i + 1; j < MAX_MIRRORS; j++) {
2764 struct ofmirror *m2 = ofproto->mirrors[j];
2766 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
2767 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
2768 m2->dup_mirrors |= m1->dup_mirrors;
2775 mirror_set(struct ofproto *ofproto_, void *aux,
2776 const struct ofproto_mirror_settings *s)
2778 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2779 mirror_mask_t mirror_bit;
2780 struct ofbundle *bundle;
2781 struct ofmirror *mirror;
2782 struct ofbundle *out;
2783 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
2784 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
2787 mirror = mirror_lookup(ofproto, aux);
2789 mirror_destroy(mirror);
2795 idx = mirror_scan(ofproto);
2797 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
2799 ofproto->up.name, MAX_MIRRORS, s->name);
2803 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
2804 mirror->ofproto = ofproto;
2807 mirror->out_vlan = -1;
2808 mirror->name = NULL;
2811 if (!mirror->name || strcmp(s->name, mirror->name)) {
2813 mirror->name = xstrdup(s->name);
2816 /* Get the new configuration. */
2817 if (s->out_bundle) {
2818 out = bundle_lookup(ofproto, s->out_bundle);
2820 mirror_destroy(mirror);
2826 out_vlan = s->out_vlan;
2828 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
2829 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
2831 /* If the configuration has not changed, do nothing. */
2832 if (hmapx_equals(&srcs, &mirror->srcs)
2833 && hmapx_equals(&dsts, &mirror->dsts)
2834 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
2835 && mirror->out == out
2836 && mirror->out_vlan == out_vlan)
2838 hmapx_destroy(&srcs);
2839 hmapx_destroy(&dsts);
2843 hmapx_swap(&srcs, &mirror->srcs);
2844 hmapx_destroy(&srcs);
2846 hmapx_swap(&dsts, &mirror->dsts);
2847 hmapx_destroy(&dsts);
2849 free(mirror->vlans);
2850 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
2853 mirror->out_vlan = out_vlan;
2855 /* Update bundles. */
2856 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2857 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
2858 if (hmapx_contains(&mirror->srcs, bundle)) {
2859 bundle->src_mirrors |= mirror_bit;
2861 bundle->src_mirrors &= ~mirror_bit;
2864 if (hmapx_contains(&mirror->dsts, bundle)) {
2865 bundle->dst_mirrors |= mirror_bit;
2867 bundle->dst_mirrors &= ~mirror_bit;
2870 if (mirror->out == bundle) {
2871 bundle->mirror_out |= mirror_bit;
2873 bundle->mirror_out &= ~mirror_bit;
2877 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2878 ofproto->has_mirrors = true;
2879 mac_learning_flush(ofproto->ml,
2880 &ofproto->backer->revalidate_set);
2881 mirror_update_dups(ofproto);
2887 mirror_destroy(struct ofmirror *mirror)
2889 struct ofproto_dpif *ofproto;
2890 mirror_mask_t mirror_bit;
2891 struct ofbundle *bundle;
2898 ofproto = mirror->ofproto;
2899 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2900 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
2902 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2903 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2904 bundle->src_mirrors &= ~mirror_bit;
2905 bundle->dst_mirrors &= ~mirror_bit;
2906 bundle->mirror_out &= ~mirror_bit;
2909 hmapx_destroy(&mirror->srcs);
2910 hmapx_destroy(&mirror->dsts);
2911 free(mirror->vlans);
2913 ofproto->mirrors[mirror->idx] = NULL;
2917 mirror_update_dups(ofproto);
2919 ofproto->has_mirrors = false;
2920 for (i = 0; i < MAX_MIRRORS; i++) {
2921 if (ofproto->mirrors[i]) {
2922 ofproto->has_mirrors = true;
2929 mirror_get_stats(struct ofproto *ofproto_, void *aux,
2930 uint64_t *packets, uint64_t *bytes)
2932 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2933 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2936 *packets = *bytes = UINT64_MAX;
2940 *packets = mirror->packet_count;
2941 *bytes = mirror->byte_count;
2947 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2949 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2950 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2951 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
2957 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2959 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2960 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2961 return bundle && bundle->mirror_out != 0;
2965 forward_bpdu_changed(struct ofproto *ofproto_)
2967 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2968 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2972 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2975 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2976 mac_learning_set_idle_time(ofproto->ml, idle_time);
2977 mac_learning_set_max_entries(ofproto->ml, max_entries);
2982 static struct ofport_dpif *
2983 get_ofp_port(const struct ofproto_dpif *ofproto, uint16_t ofp_port)
2985 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2986 return ofport ? ofport_dpif_cast(ofport) : NULL;
2989 static struct ofport_dpif *
2990 get_odp_port(const struct ofproto_dpif *ofproto, uint32_t odp_port)
2992 struct ofport_dpif *port = odp_port_to_ofport(ofproto->backer, odp_port);
2993 return port && &ofproto->up == port->up.ofproto ? port : NULL;
2997 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2998 struct ofproto_port *ofproto_port,
2999 struct dpif_port *dpif_port)
3001 ofproto_port->name = dpif_port->name;
3002 ofproto_port->type = dpif_port->type;
3003 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3006 static struct ofport_dpif *
3007 ofport_get_peer(const struct ofport_dpif *ofport_dpif)
3009 const struct ofproto_dpif *ofproto;
3012 peer = netdev_vport_patch_peer(ofport_dpif->up.netdev);
3017 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3018 struct ofport *ofport;
3020 ofport = shash_find_data(&ofproto->up.port_by_name, peer);
3021 if (ofport && ofport->ofproto->ofproto_class == &ofproto_dpif_class) {
3022 return ofport_dpif_cast(ofport);
3029 port_run_fast(struct ofport_dpif *ofport)
3031 if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) {
3032 struct ofpbuf packet;
3034 ofpbuf_init(&packet, 0);
3035 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr);
3036 send_packet(ofport, &packet);
3037 ofpbuf_uninit(&packet);
3042 port_run(struct ofport_dpif *ofport)
3044 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3045 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3046 bool enable = netdev_get_carrier(ofport->up.netdev);
3048 ofport->carrier_seq = carrier_seq;
3050 port_run_fast(ofport);
3052 if (ofport->tnl_port
3053 && tnl_port_reconfigure(&ofport->up, ofport->odp_port,
3054 &ofport->tnl_port)) {
3055 ofproto_dpif_cast(ofport->up.ofproto)->backer->need_revalidate = true;
3059 int cfm_opup = cfm_get_opup(ofport->cfm);
3061 cfm_run(ofport->cfm);
3062 enable = enable && !cfm_get_fault(ofport->cfm);
3064 if (cfm_opup >= 0) {
3065 enable = enable && cfm_opup;
3069 if (ofport->bundle) {
3070 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3071 if (carrier_changed) {
3072 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3076 if (ofport->may_enable != enable) {
3077 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3079 if (ofproto->has_bundle_action) {
3080 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3084 ofport->may_enable = enable;
3088 port_wait(struct ofport_dpif *ofport)
3091 cfm_wait(ofport->cfm);
3096 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3097 struct ofproto_port *ofproto_port)
3099 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3100 struct dpif_port dpif_port;
3103 if (sset_contains(&ofproto->ghost_ports, devname)) {
3104 const char *type = netdev_get_type_from_name(devname);
3106 /* We may be called before ofproto->up.port_by_name is populated with
3107 * the appropriate ofport. For this reason, we must get the name and
3108 * type from the netdev layer directly. */
3110 const struct ofport *ofport;
3112 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3113 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3114 ofproto_port->name = xstrdup(devname);
3115 ofproto_port->type = xstrdup(type);
3121 if (!sset_contains(&ofproto->ports, devname)) {
3124 error = dpif_port_query_by_name(ofproto->backer->dpif,
3125 devname, &dpif_port);
3127 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3133 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3135 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3136 const char *dp_port_name = netdev_vport_get_dpif_port(netdev);
3137 const char *devname = netdev_get_name(netdev);
3139 if (netdev_vport_is_patch(netdev)) {
3140 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3144 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3145 uint32_t port_no = UINT32_MAX;
3148 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3152 if (netdev_get_tunnel_config(netdev)) {
3153 simap_put(&ofproto->backer->tnl_backers, dp_port_name, port_no);
3157 if (netdev_get_tunnel_config(netdev)) {
3158 sset_add(&ofproto->ghost_ports, devname);
3160 sset_add(&ofproto->ports, devname);
3166 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
3168 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3169 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
3176 sset_find_and_delete(&ofproto->ghost_ports,
3177 netdev_get_name(ofport->up.netdev));
3178 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3179 if (!ofport->tnl_port) {
3180 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3182 /* The caller is going to close ofport->up.netdev. If this is a
3183 * bonded port, then the bond is using that netdev, so remove it
3184 * from the bond. The client will need to reconfigure everything
3185 * after deleting ports, so then the slave will get re-added. */
3186 bundle_remove(&ofport->up);
3193 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3195 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3198 error = netdev_get_stats(ofport->up.netdev, stats);
3200 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3201 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3203 /* ofproto->stats.tx_packets represents packets that we created
3204 * internally and sent to some port (e.g. packets sent with
3205 * send_packet()). Account for them as if they had come from
3206 * OFPP_LOCAL and got forwarded. */
3208 if (stats->rx_packets != UINT64_MAX) {
3209 stats->rx_packets += ofproto->stats.tx_packets;
3212 if (stats->rx_bytes != UINT64_MAX) {
3213 stats->rx_bytes += ofproto->stats.tx_bytes;
3216 /* ofproto->stats.rx_packets represents packets that were received on
3217 * some port and we processed internally and dropped (e.g. STP).
3218 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3220 if (stats->tx_packets != UINT64_MAX) {
3221 stats->tx_packets += ofproto->stats.rx_packets;
3224 if (stats->tx_bytes != UINT64_MAX) {
3225 stats->tx_bytes += ofproto->stats.rx_bytes;
3232 /* Account packets for LOCAL port. */
3234 ofproto_update_local_port_stats(const struct ofproto *ofproto_,
3235 size_t tx_size, size_t rx_size)
3237 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3240 ofproto->stats.rx_packets++;
3241 ofproto->stats.rx_bytes += rx_size;
3244 ofproto->stats.tx_packets++;
3245 ofproto->stats.tx_bytes += tx_size;
3249 struct port_dump_state {
3254 struct ofproto_port port;
3259 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3261 *statep = xzalloc(sizeof(struct port_dump_state));
3266 port_dump_next(const struct ofproto *ofproto_, void *state_,
3267 struct ofproto_port *port)
3269 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3270 struct port_dump_state *state = state_;
3271 const struct sset *sset;
3272 struct sset_node *node;
3274 if (state->has_port) {
3275 ofproto_port_destroy(&state->port);
3276 state->has_port = false;
3278 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3279 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3282 error = port_query_by_name(ofproto_, node->name, &state->port);
3284 *port = state->port;
3285 state->has_port = true;
3287 } else if (error != ENODEV) {
3292 if (!state->ghost) {
3293 state->ghost = true;
3296 return port_dump_next(ofproto_, state_, port);
3303 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3305 struct port_dump_state *state = state_;
3307 if (state->has_port) {
3308 ofproto_port_destroy(&state->port);
3315 port_poll(const struct ofproto *ofproto_, char **devnamep)
3317 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3319 if (ofproto->port_poll_errno) {
3320 int error = ofproto->port_poll_errno;
3321 ofproto->port_poll_errno = 0;
3325 if (sset_is_empty(&ofproto->port_poll_set)) {
3329 *devnamep = sset_pop(&ofproto->port_poll_set);
3334 port_poll_wait(const struct ofproto *ofproto_)
3336 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3337 dpif_port_poll_wait(ofproto->backer->dpif);
3341 port_is_lacp_current(const struct ofport *ofport_)
3343 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3344 return (ofport->bundle && ofport->bundle->lacp
3345 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3349 /* Upcall handling. */
3351 /* Flow miss batching.
3353 * Some dpifs implement operations faster when you hand them off in a batch.
3354 * To allow batching, "struct flow_miss" queues the dpif-related work needed
3355 * for a given flow. Each "struct flow_miss" corresponds to sending one or
3356 * more packets, plus possibly installing the flow in the dpif.
3358 * So far we only batch the operations that affect flow setup time the most.
3359 * It's possible to batch more than that, but the benefit might be minimal. */
3361 struct hmap_node hmap_node;
3362 struct ofproto_dpif *ofproto;
3364 enum odp_key_fitness key_fitness;
3365 const struct nlattr *key;
3367 struct initial_vals initial_vals;
3368 struct list packets;
3369 enum dpif_upcall_type upcall_type;
3370 uint32_t odp_in_port;
3373 struct flow_miss_op {
3374 struct dpif_op dpif_op;
3375 void *garbage; /* Pointer to pass to free(), NULL if none. */
3376 uint64_t stub[1024 / 8]; /* Temporary buffer. */
3379 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
3380 * OpenFlow controller as necessary according to their individual
3381 * configurations. */
3383 send_packet_in_miss(struct ofproto_dpif *ofproto, const struct ofpbuf *packet,
3384 const struct flow *flow)
3386 struct ofputil_packet_in pin;
3388 pin.packet = packet->data;
3389 pin.packet_len = packet->size;
3390 pin.reason = OFPR_NO_MATCH;
3391 pin.controller_id = 0;
3396 pin.send_len = 0; /* not used for flow table misses */
3398 flow_get_metadata(flow, &pin.fmd);
3400 connmgr_send_packet_in(ofproto->up.connmgr, &pin);
3403 static enum slow_path_reason
3404 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
3405 const struct ofport_dpif *ofport, const struct ofpbuf *packet)
3409 } else if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
3411 cfm_process_heartbeat(ofport->cfm, packet);
3414 } else if (ofport->bundle && ofport->bundle->lacp
3415 && flow->dl_type == htons(ETH_TYPE_LACP)) {
3417 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
3420 } else if (ofproto->stp && stp_should_process_flow(flow)) {
3422 stp_process_packet(ofport, packet);
3430 static struct flow_miss *
3431 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
3432 const struct flow *flow, uint32_t hash)
3434 struct flow_miss *miss;
3436 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
3437 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
3445 /* Partially Initializes 'op' as an "execute" operation for 'miss' and
3446 * 'packet'. The caller must initialize op->actions and op->actions_len. If
3447 * 'miss' is associated with a subfacet the caller must also initialize the
3448 * returned op->subfacet, and if anything needs to be freed after processing
3449 * the op, the caller must initialize op->garbage also. */
3451 init_flow_miss_execute_op(struct flow_miss *miss, struct ofpbuf *packet,
3452 struct flow_miss_op *op)
3454 if (miss->flow.vlan_tci != miss->initial_vals.vlan_tci) {
3455 /* This packet was received on a VLAN splinter port. We
3456 * added a VLAN to the packet to make the packet resemble
3457 * the flow, but the actions were composed assuming that
3458 * the packet contained no VLAN. So, we must remove the
3459 * VLAN header from the packet before trying to execute the
3461 eth_pop_vlan(packet);
3465 op->dpif_op.type = DPIF_OP_EXECUTE;
3466 op->dpif_op.u.execute.key = miss->key;
3467 op->dpif_op.u.execute.key_len = miss->key_len;
3468 op->dpif_op.u.execute.packet = packet;
3471 /* Helper for handle_flow_miss_without_facet() and
3472 * handle_flow_miss_with_facet(). */
3474 handle_flow_miss_common(struct rule_dpif *rule,
3475 struct ofpbuf *packet, const struct flow *flow)
3477 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3479 ofproto->n_matches++;
3481 if (rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
3483 * Extra-special case for fail-open mode.
3485 * We are in fail-open mode and the packet matched the fail-open
3486 * rule, but we are connected to a controller too. We should send
3487 * the packet up to the controller in the hope that it will try to
3488 * set up a flow and thereby allow us to exit fail-open.
3490 * See the top-level comment in fail-open.c for more information.
3492 send_packet_in_miss(ofproto, packet, flow);
3496 /* Figures out whether a flow that missed in 'ofproto', whose details are in
3497 * 'miss', is likely to be worth tracking in detail in userspace and (usually)
3498 * installing a datapath flow. The answer is usually "yes" (a return value of
3499 * true). However, for short flows the cost of bookkeeping is much higher than
3500 * the benefits, so when the datapath holds a large number of flows we impose
3501 * some heuristics to decide which flows are likely to be worth tracking. */
3503 flow_miss_should_make_facet(struct ofproto_dpif *ofproto,
3504 struct flow_miss *miss, uint32_t hash)
3506 if (!ofproto->governor) {
3509 n_subfacets = hmap_count(&ofproto->subfacets);
3510 if (n_subfacets * 2 <= ofproto->up.flow_eviction_threshold) {
3514 ofproto->governor = governor_create(ofproto->up.name);
3517 return governor_should_install_flow(ofproto->governor, hash,
3518 list_size(&miss->packets));
3521 /* Handles 'miss', which matches 'rule', without creating a facet or subfacet
3522 * or creating any datapath flow. May add an "execute" operation to 'ops' and
3523 * increment '*n_ops'. */
3525 handle_flow_miss_without_facet(struct flow_miss *miss,
3526 struct rule_dpif *rule,
3527 struct flow_miss_op *ops, size_t *n_ops)
3529 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3530 long long int now = time_msec();
3531 struct action_xlate_ctx ctx;
3532 struct ofpbuf *packet;
3534 LIST_FOR_EACH (packet, list_node, &miss->packets) {
3535 struct flow_miss_op *op = &ops[*n_ops];
3536 struct dpif_flow_stats stats;
3537 struct ofpbuf odp_actions;
3539 COVERAGE_INC(facet_suppress);
3541 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
3543 dpif_flow_stats_extract(&miss->flow, packet, now, &stats);
3544 rule_credit_stats(rule, &stats);
3546 action_xlate_ctx_init(&ctx, ofproto, &miss->flow,
3547 &miss->initial_vals, rule, 0, packet);
3548 ctx.resubmit_stats = &stats;
3549 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len,
3552 if (odp_actions.size) {
3553 struct dpif_execute *execute = &op->dpif_op.u.execute;
3555 init_flow_miss_execute_op(miss, packet, op);
3556 execute->actions = odp_actions.data;
3557 execute->actions_len = odp_actions.size;
3558 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
3562 ofpbuf_uninit(&odp_actions);
3567 /* Handles 'miss', which matches 'facet'. May add any required datapath
3568 * operations to 'ops', incrementing '*n_ops' for each new op.
3570 * All of the packets in 'miss' are considered to have arrived at time 'now'.
3571 * This is really important only for new facets: if we just called time_msec()
3572 * here, then the new subfacet or its packets could look (occasionally) as
3573 * though it was used some time after the facet was used. That can make a
3574 * one-packet flow look like it has a nonzero duration, which looks odd in
3575 * e.g. NetFlow statistics. */
3577 handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
3579 struct flow_miss_op *ops, size_t *n_ops)
3581 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3582 enum subfacet_path want_path;
3583 struct subfacet *subfacet;
3584 struct ofpbuf *packet;
3586 subfacet = subfacet_create(facet, miss, now);
3588 LIST_FOR_EACH (packet, list_node, &miss->packets) {
3589 struct flow_miss_op *op = &ops[*n_ops];
3590 struct dpif_flow_stats stats;
3591 struct ofpbuf odp_actions;
3593 handle_flow_miss_common(facet->rule, packet, &miss->flow);
3595 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
3596 if (!subfacet->actions || subfacet->slow) {
3597 subfacet_make_actions(subfacet, packet, &odp_actions);
3600 dpif_flow_stats_extract(&facet->flow, packet, now, &stats);
3601 subfacet_update_stats(subfacet, &stats);
3603 if (subfacet->actions_len) {
3604 struct dpif_execute *execute = &op->dpif_op.u.execute;
3606 init_flow_miss_execute_op(miss, packet, op);
3607 if (!subfacet->slow) {
3608 execute->actions = subfacet->actions;
3609 execute->actions_len = subfacet->actions_len;
3610 ofpbuf_uninit(&odp_actions);
3612 execute->actions = odp_actions.data;
3613 execute->actions_len = odp_actions.size;
3614 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
3619 ofpbuf_uninit(&odp_actions);
3623 want_path = subfacet_want_path(subfacet->slow);
3624 if (miss->upcall_type == DPIF_UC_MISS || subfacet->path != want_path) {
3625 struct flow_miss_op *op = &ops[(*n_ops)++];
3626 struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
3628 subfacet->path = want_path;
3631 op->dpif_op.type = DPIF_OP_FLOW_PUT;
3632 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3633 put->key = miss->key;
3634 put->key_len = miss->key_len;
3635 if (want_path == SF_FAST_PATH) {
3636 put->actions = subfacet->actions;
3637 put->actions_len = subfacet->actions_len;
3639 compose_slow_path(ofproto, &facet->flow, subfacet->slow,
3640 op->stub, sizeof op->stub,
3641 &put->actions, &put->actions_len);
3647 /* Handles flow miss 'miss'. May add any required datapath operations
3648 * to 'ops', incrementing '*n_ops' for each new op. */
3650 handle_flow_miss(struct flow_miss *miss, struct flow_miss_op *ops,
3653 struct ofproto_dpif *ofproto = miss->ofproto;
3654 struct facet *facet;
3658 /* The caller must ensure that miss->hmap_node.hash contains
3659 * flow_hash(miss->flow, 0). */
3660 hash = miss->hmap_node.hash;
3662 facet = facet_lookup_valid(ofproto, &miss->flow, hash);
3664 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &miss->flow);
3666 if (!flow_miss_should_make_facet(ofproto, miss, hash)) {
3667 handle_flow_miss_without_facet(miss, rule, ops, n_ops);
3671 facet = facet_create(rule, &miss->flow, hash);
3676 handle_flow_miss_with_facet(miss, facet, now, ops, n_ops);
3679 static struct drop_key *
3680 drop_key_lookup(const struct dpif_backer *backer, const struct nlattr *key,
3683 struct drop_key *drop_key;
3685 HMAP_FOR_EACH_WITH_HASH (drop_key, hmap_node, hash_bytes(key, key_len, 0),
3686 &backer->drop_keys) {
3687 if (drop_key->key_len == key_len
3688 && !memcmp(drop_key->key, key, key_len)) {
3696 drop_key_clear(struct dpif_backer *backer)
3698 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3699 struct drop_key *drop_key, *next;
3701 HMAP_FOR_EACH_SAFE (drop_key, next, hmap_node, &backer->drop_keys) {
3704 error = dpif_flow_del(backer->dpif, drop_key->key, drop_key->key_len,
3706 if (error && !VLOG_DROP_WARN(&rl)) {
3707 struct ds ds = DS_EMPTY_INITIALIZER;
3708 odp_flow_key_format(drop_key->key, drop_key->key_len, &ds);
3709 VLOG_WARN("Failed to delete drop key (%s) (%s)", strerror(error),
3714 hmap_remove(&backer->drop_keys, &drop_key->hmap_node);
3715 free(drop_key->key);
3720 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
3721 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
3722 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
3723 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
3724 * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that
3725 * 'packet' ingressed.
3727 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
3728 * 'flow''s in_port to OFPP_NONE.
3730 * This function does post-processing on data returned from
3731 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
3732 * of the upcall processing logic. In particular, if the extracted in_port is
3733 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
3734 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
3735 * a VLAN header onto 'packet' (if it is nonnull).
3737 * Optionally, if 'initial_vals' is nonnull, sets 'initial_vals->vlan_tci'
3738 * to the VLAN TCI with which the packet was really received, that is, the
3739 * actual VLAN TCI extracted by odp_flow_key_to_flow(). (This differs from
3740 * the value returned in flow->vlan_tci only for packets received on
3741 * VLAN splinters.) Also, if received on an IP tunnel, sets
3742 * 'initial_vals->tunnel_ip_tos' to the tunnel's IP TOS.
3744 * Similarly, this function also includes some logic to help with tunnels. It
3745 * may modify 'flow' as necessary to make the tunneling implementation
3746 * transparent to the upcall processing logic.
3748 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
3749 * or some other positive errno if there are other problems. */
3751 ofproto_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
3752 const struct nlattr *key, size_t key_len,
3753 struct flow *flow, enum odp_key_fitness *fitnessp,
3754 struct ofproto_dpif **ofproto, uint32_t *odp_in_port,
3755 struct initial_vals *initial_vals)
3757 const struct ofport_dpif *port;
3758 enum odp_key_fitness fitness;
3761 fitness = odp_flow_key_to_flow(key, key_len, flow);
3762 if (fitness == ODP_FIT_ERROR) {
3768 initial_vals->vlan_tci = flow->vlan_tci;
3769 initial_vals->tunnel_ip_tos = flow->tunnel.ip_tos;
3773 *odp_in_port = flow->in_port;
3776 if (tnl_port_should_receive(flow)) {
3777 const struct ofport *ofport = tnl_port_receive(flow);
3779 flow->in_port = OFPP_NONE;
3782 port = ofport_dpif_cast(ofport);
3784 /* We can't reproduce 'key' from 'flow'. */
3785 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
3787 /* XXX: Since the tunnel module is not scoped per backer, it's
3788 * theoretically possible that we'll receive an ofport belonging to an
3789 * entirely different datapath. In practice, this can't happen because
3790 * no platforms has two separate datapaths which each support
3792 ovs_assert(ofproto_dpif_cast(port->up.ofproto)->backer == backer);
3794 port = odp_port_to_ofport(backer, flow->in_port);
3796 flow->in_port = OFPP_NONE;
3800 flow->in_port = port->up.ofp_port;
3801 if (vsp_adjust_flow(ofproto_dpif_cast(port->up.ofproto), flow)) {
3803 /* Make the packet resemble the flow, so that it gets sent to
3804 * an OpenFlow controller properly, so that it looks correct
3805 * for sFlow, and so that flow_extract() will get the correct
3806 * vlan_tci if it is called on 'packet'.
3808 * The allocated space inside 'packet' probably also contains
3809 * 'key', that is, both 'packet' and 'key' are probably part of
3810 * a struct dpif_upcall (see the large comment on that
3811 * structure definition), so pushing data on 'packet' is in
3812 * general not a good idea since it could overwrite 'key' or
3813 * free it as a side effect. However, it's OK in this special
3814 * case because we know that 'packet' is inside a Netlink
3815 * attribute: pushing 4 bytes will just overwrite the 4-byte
3816 * "struct nlattr", which is fine since we don't need that
3817 * header anymore. */
3818 eth_push_vlan(packet, flow->vlan_tci);
3820 /* We can't reproduce 'key' from 'flow'. */
3821 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
3827 *ofproto = ofproto_dpif_cast(port->up.ofproto);
3832 *fitnessp = fitness;
3838 handle_miss_upcalls(struct dpif_backer *backer, struct dpif_upcall *upcalls,
3841 struct dpif_upcall *upcall;
3842 struct flow_miss *miss;
3843 struct flow_miss misses[FLOW_MISS_MAX_BATCH];
3844 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
3845 struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
3855 /* Construct the to-do list.
3857 * This just amounts to extracting the flow from each packet and sticking
3858 * the packets that have the same flow in the same "flow_miss" structure so
3859 * that we can process them together. */
3862 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
3863 struct flow_miss *miss = &misses[n_misses];
3864 struct flow_miss *existing_miss;
3865 struct ofproto_dpif *ofproto;
3866 uint32_t odp_in_port;
3871 error = ofproto_receive(backer, upcall->packet, upcall->key,
3872 upcall->key_len, &flow, &miss->key_fitness,
3873 &ofproto, &odp_in_port, &miss->initial_vals);
3874 if (error == ENODEV) {
3875 struct drop_key *drop_key;
3877 /* Received packet on port for which we couldn't associate
3878 * an ofproto. This can happen if a port is removed while
3879 * traffic is being received. Print a rate-limited message
3880 * in case it happens frequently. Install a drop flow so
3881 * that future packets of the flow are inexpensively dropped
3883 VLOG_INFO_RL(&rl, "received packet on unassociated port %"PRIu32,
3886 drop_key = drop_key_lookup(backer, upcall->key, upcall->key_len);
3888 drop_key = xmalloc(sizeof *drop_key);
3889 drop_key->key = xmemdup(upcall->key, upcall->key_len);
3890 drop_key->key_len = upcall->key_len;
3892 hmap_insert(&backer->drop_keys, &drop_key->hmap_node,
3893 hash_bytes(drop_key->key, drop_key->key_len, 0));
3894 dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
3895 drop_key->key, drop_key->key_len, NULL, 0, NULL);
3903 ofproto->n_missed++;
3904 flow_extract(upcall->packet, flow.skb_priority, flow.skb_mark,
3905 &flow.tunnel, flow.in_port, &miss->flow);
3907 /* Add other packets to a to-do list. */
3908 hash = flow_hash(&miss->flow, 0);
3909 existing_miss = flow_miss_find(&todo, ofproto, &miss->flow, hash);
3910 if (!existing_miss) {
3911 hmap_insert(&todo, &miss->hmap_node, hash);
3912 miss->ofproto = ofproto;
3913 miss->key = upcall->key;
3914 miss->key_len = upcall->key_len;
3915 miss->upcall_type = upcall->type;
3916 miss->odp_in_port = odp_in_port;
3917 list_init(&miss->packets);
3921 miss = existing_miss;
3923 list_push_back(&miss->packets, &upcall->packet->list_node);
3926 /* Process each element in the to-do list, constructing the set of
3927 * operations to batch. */
3929 HMAP_FOR_EACH (miss, hmap_node, &todo) {
3930 handle_flow_miss(miss, flow_miss_ops, &n_ops);
3932 ovs_assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
3934 /* Execute batch. */
3935 for (i = 0; i < n_ops; i++) {
3936 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3938 dpif_operate(backer->dpif, dpif_ops, n_ops);
3941 for (i = 0; i < n_ops; i++) {
3942 free(flow_miss_ops[i].garbage);
3944 hmap_destroy(&todo);
3947 static enum { SFLOW_UPCALL, MISS_UPCALL, BAD_UPCALL }
3948 classify_upcall(const struct dpif_upcall *upcall)
3950 union user_action_cookie cookie;
3952 /* First look at the upcall type. */
3953 switch (upcall->type) {
3954 case DPIF_UC_ACTION:
3960 case DPIF_N_UC_TYPES:
3962 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3966 /* "action" upcalls need a closer look. */
3967 if (!upcall->userdata) {
3968 VLOG_WARN_RL(&rl, "action upcall missing cookie");
3971 if (nl_attr_get_size(upcall->userdata) != sizeof(cookie)) {
3972 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %zu",
3973 nl_attr_get_size(upcall->userdata));
3976 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof(cookie));
3977 switch (cookie.type) {
3978 case USER_ACTION_COOKIE_SFLOW:
3979 return SFLOW_UPCALL;
3981 case USER_ACTION_COOKIE_SLOW_PATH:
3984 case USER_ACTION_COOKIE_UNSPEC:
3986 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64,
3987 nl_attr_get_u64(upcall->userdata));
3993 handle_sflow_upcall(struct dpif_backer *backer,
3994 const struct dpif_upcall *upcall)
3996 struct ofproto_dpif *ofproto;
3997 union user_action_cookie cookie;
3999 uint32_t odp_in_port;
4001 if (ofproto_receive(backer, upcall->packet, upcall->key, upcall->key_len,
4002 &flow, NULL, &ofproto, &odp_in_port, NULL)
4003 || !ofproto->sflow) {
4007 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof(cookie));
4008 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
4009 odp_in_port, &cookie);
4013 handle_upcalls(struct dpif_backer *backer, unsigned int max_batch)
4015 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
4016 struct ofpbuf miss_bufs[FLOW_MISS_MAX_BATCH];
4017 uint64_t miss_buf_stubs[FLOW_MISS_MAX_BATCH][4096 / 8];
4022 ovs_assert(max_batch <= FLOW_MISS_MAX_BATCH);
4025 for (n_processed = 0; n_processed < max_batch; n_processed++) {
4026 struct dpif_upcall *upcall = &misses[n_misses];
4027 struct ofpbuf *buf = &miss_bufs[n_misses];
4030 ofpbuf_use_stub(buf, miss_buf_stubs[n_misses],
4031 sizeof miss_buf_stubs[n_misses]);
4032 error = dpif_recv(backer->dpif, upcall, buf);
4038 switch (classify_upcall(upcall)) {
4040 /* Handle it later. */
4045 handle_sflow_upcall(backer, upcall);
4055 /* Handle deferred MISS_UPCALL processing. */
4056 handle_miss_upcalls(backer, misses, n_misses);
4057 for (i = 0; i < n_misses; i++) {
4058 ofpbuf_uninit(&miss_bufs[i]);
4064 /* Flow expiration. */
4066 static int subfacet_max_idle(const struct ofproto_dpif *);
4067 static void update_stats(struct dpif_backer *);
4068 static void rule_expire(struct rule_dpif *);
4069 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
4071 /* This function is called periodically by run(). Its job is to collect
4072 * updates for the flows that have been installed into the datapath, most
4073 * importantly when they last were used, and then use that information to
4074 * expire flows that have not been used recently.
4076 * Returns the number of milliseconds after which it should be called again. */
4078 expire(struct dpif_backer *backer)
4080 struct ofproto_dpif *ofproto;
4081 int max_idle = INT32_MAX;
4083 /* Periodically clear out the drop keys in an effort to keep them
4084 * relatively few. */
4085 drop_key_clear(backer);
4087 /* Update stats for each flow in the backer. */
4088 update_stats(backer);
4090 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4091 struct rule *rule, *next_rule;
4094 if (ofproto->backer != backer) {
4098 /* Keep track of the max number of flows per ofproto_dpif. */
4099 update_max_subfacet_count(ofproto);
4101 /* Expire subfacets that have been idle too long. */
4102 dp_max_idle = subfacet_max_idle(ofproto);
4103 expire_subfacets(ofproto, dp_max_idle);
4105 max_idle = MIN(max_idle, dp_max_idle);
4107 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
4109 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
4110 &ofproto->up.expirable) {
4111 rule_expire(rule_dpif_cast(rule));
4114 /* All outstanding data in existing flows has been accounted, so it's a
4115 * good time to do bond rebalancing. */
4116 if (ofproto->has_bonded_bundles) {
4117 struct ofbundle *bundle;
4119 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4121 bond_rebalance(bundle->bond, &backer->revalidate_set);
4127 return MIN(max_idle, 1000);
4130 /* Updates flow table statistics given that the datapath just reported 'stats'
4131 * as 'subfacet''s statistics. */
4133 update_subfacet_stats(struct subfacet *subfacet,
4134 const struct dpif_flow_stats *stats)
4136 struct facet *facet = subfacet->facet;
4138 if (stats->n_packets >= subfacet->dp_packet_count) {
4139 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
4140 facet->packet_count += extra;
4142 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
4145 if (stats->n_bytes >= subfacet->dp_byte_count) {
4146 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
4148 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
4151 subfacet->dp_packet_count = stats->n_packets;
4152 subfacet->dp_byte_count = stats->n_bytes;
4154 facet->tcp_flags |= stats->tcp_flags;
4156 subfacet_update_time(subfacet, stats->used);
4157 if (facet->accounted_bytes < facet->byte_count) {
4159 facet_account(facet);
4160 facet->accounted_bytes = facet->byte_count;
4162 facet_push_stats(facet);
4165 /* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
4166 * about, or a flow that shouldn't be installed but was anyway. Delete it. */
4168 delete_unexpected_flow(struct ofproto_dpif *ofproto,
4169 const struct nlattr *key, size_t key_len)
4171 if (!VLOG_DROP_WARN(&rl)) {
4175 odp_flow_key_format(key, key_len, &s);
4176 VLOG_WARN("unexpected flow on %s: %s", ofproto->up.name, ds_cstr(&s));
4180 COVERAGE_INC(facet_unexpected);
4181 dpif_flow_del(ofproto->backer->dpif, key, key_len, NULL);
4184 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
4186 * This function also pushes statistics updates to rules which each facet
4187 * resubmits into. Generally these statistics will be accurate. However, if a
4188 * facet changes the rule it resubmits into at some time in between
4189 * update_stats() runs, it is possible that statistics accrued to the
4190 * old rule will be incorrectly attributed to the new rule. This could be
4191 * avoided by calling update_stats() whenever rules are created or
4192 * deleted. However, the performance impact of making so many calls to the
4193 * datapath do not justify the benefit of having perfectly accurate statistics.
4195 * In addition, this function maintains per ofproto flow hit counts. The patch
4196 * port is not treated specially. e.g. A packet ingress from br0 patched into
4197 * br1 will increase the hit count of br0 by 1, however, does not affect
4198 * the hit or miss counts of br1.
4201 update_stats(struct dpif_backer *backer)
4203 const struct dpif_flow_stats *stats;
4204 struct dpif_flow_dump dump;
4205 const struct nlattr *key;
4208 dpif_flow_dump_start(&dump, backer->dpif);
4209 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
4211 struct subfacet *subfacet;
4212 struct ofproto_dpif *ofproto;
4213 struct ofport_dpif *ofport;
4216 if (ofproto_receive(backer, NULL, key, key_len, &flow, NULL, &ofproto,
4221 ofproto->total_subfacet_count += hmap_count(&ofproto->subfacets);
4222 ofproto->n_update_stats++;
4223 update_moving_averages(ofproto);
4225 ofport = get_ofp_port(ofproto, flow.in_port);
4226 if (ofport && ofport->tnl_port) {
4227 netdev_vport_inc_rx(ofport->up.netdev, stats);
4230 key_hash = odp_flow_key_hash(key, key_len);
4231 subfacet = subfacet_find(ofproto, key, key_len, key_hash);
4232 switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
4234 /* Update ofproto_dpif's hit count. */
4235 if (stats->n_packets > subfacet->dp_packet_count) {
4236 uint64_t delta = stats->n_packets - subfacet->dp_packet_count;
4237 dpif_stats_update_hit_count(ofproto, delta);
4240 update_subfacet_stats(subfacet, stats);
4244 /* Stats are updated per-packet. */
4247 case SF_NOT_INSTALLED:
4249 delete_unexpected_flow(ofproto, key, key_len);
4253 dpif_flow_dump_done(&dump);
4256 /* Calculates and returns the number of milliseconds of idle time after which
4257 * subfacets should expire from the datapath. When a subfacet expires, we fold
4258 * its statistics into its facet, and when a facet's last subfacet expires, we
4259 * fold its statistic into its rule. */
4261 subfacet_max_idle(const struct ofproto_dpif *ofproto)
4264 * Idle time histogram.
4266 * Most of the time a switch has a relatively small number of subfacets.
4267 * When this is the case we might as well keep statistics for all of them
4268 * in userspace and to cache them in the kernel datapath for performance as
4271 * As the number of subfacets increases, the memory required to maintain
4272 * statistics about them in userspace and in the kernel becomes
4273 * significant. However, with a large number of subfacets it is likely
4274 * that only a few of them are "heavy hitters" that consume a large amount
4275 * of bandwidth. At this point, only heavy hitters are worth caching in
4276 * the kernel and maintaining in userspaces; other subfacets we can
4279 * The technique used to compute the idle time is to build a histogram with
4280 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
4281 * that is installed in the kernel gets dropped in the appropriate bucket.
4282 * After the histogram has been built, we compute the cutoff so that only
4283 * the most-recently-used 1% of subfacets (but at least
4284 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
4285 * the most-recently-used bucket of subfacets is kept, so actually an
4286 * arbitrary number of subfacets can be kept in any given expiration run
4287 * (though the next run will delete most of those unless they receive
4290 * This requires a second pass through the subfacets, in addition to the
4291 * pass made by update_stats(), because the former function never looks at
4292 * uninstallable subfacets.
4294 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
4295 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
4296 int buckets[N_BUCKETS] = { 0 };
4297 int total, subtotal, bucket;
4298 struct subfacet *subfacet;
4302 total = hmap_count(&ofproto->subfacets);
4303 if (total <= ofproto->up.flow_eviction_threshold) {
4304 return N_BUCKETS * BUCKET_WIDTH;
4307 /* Build histogram. */
4309 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
4310 long long int idle = now - subfacet->used;
4311 int bucket = (idle <= 0 ? 0
4312 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4313 : (unsigned int) idle / BUCKET_WIDTH);
4317 /* Find the first bucket whose flows should be expired. */
4318 subtotal = bucket = 0;
4320 subtotal += buckets[bucket++];
4321 } while (bucket < N_BUCKETS &&
4322 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
4324 if (VLOG_IS_DBG_ENABLED()) {
4328 ds_put_cstr(&s, "keep");
4329 for (i = 0; i < N_BUCKETS; i++) {
4331 ds_put_cstr(&s, ", drop");
4334 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4337 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
4341 return bucket * BUCKET_WIDTH;
4345 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
4347 /* Cutoff time for most flows. */
4348 long long int normal_cutoff = time_msec() - dp_max_idle;
4350 /* We really want to keep flows for special protocols around, so use a more
4351 * conservative cutoff. */
4352 long long int special_cutoff = time_msec() - 10000;
4354 struct subfacet *subfacet, *next_subfacet;
4355 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
4359 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
4360 &ofproto->subfacets) {
4361 long long int cutoff;
4363 cutoff = (subfacet->slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)
4366 if (subfacet->used < cutoff) {
4367 if (subfacet->path != SF_NOT_INSTALLED) {
4368 batch[n_batch++] = subfacet;
4369 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
4370 subfacet_destroy_batch(ofproto, batch, n_batch);
4374 subfacet_destroy(subfacet);
4380 subfacet_destroy_batch(ofproto, batch, n_batch);
4384 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4385 * then delete it entirely. */
4387 rule_expire(struct rule_dpif *rule)
4389 struct facet *facet, *next_facet;
4393 if (rule->up.pending) {
4394 /* We'll have to expire it later. */
4398 /* Has 'rule' expired? */
4400 if (rule->up.hard_timeout
4401 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
4402 reason = OFPRR_HARD_TIMEOUT;
4403 } else if (rule->up.idle_timeout
4404 && now > rule->up.used + rule->up.idle_timeout * 1000) {
4405 reason = OFPRR_IDLE_TIMEOUT;
4410 COVERAGE_INC(ofproto_dpif_expired);
4412 /* Update stats. (This is a no-op if the rule expired due to an idle
4413 * timeout, because that only happens when the rule has no facets left.) */
4414 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4415 facet_remove(facet);
4418 /* Get rid of the rule. */
4419 ofproto_rule_expire(&rule->up, reason);
4424 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
4426 * The caller must already have determined that no facet with an identical
4427 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
4428 * the ofproto's classifier table.
4430 * 'hash' must be the return value of flow_hash(flow, 0).
4432 * The facet will initially have no subfacets. The caller should create (at
4433 * least) one subfacet with subfacet_create(). */
4434 static struct facet *
4435 facet_create(struct rule_dpif *rule, const struct flow *flow, uint32_t hash)
4437 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4438 struct facet *facet;
4440 facet = xzalloc(sizeof *facet);
4441 facet->used = time_msec();
4442 hmap_insert(&ofproto->facets, &facet->hmap_node, hash);
4443 list_push_back(&rule->facets, &facet->list_node);
4445 facet->flow = *flow;
4446 list_init(&facet->subfacets);
4447 netflow_flow_init(&facet->nf_flow);
4448 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
4450 facet->learn_rl = time_msec() + 500;
4456 facet_free(struct facet *facet)
4461 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
4462 * 'packet', which arrived on 'in_port'. */
4464 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
4465 const struct nlattr *odp_actions, size_t actions_len,
4466 struct ofpbuf *packet)
4468 struct odputil_keybuf keybuf;
4472 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4473 odp_flow_key_from_flow(&key, flow,
4474 ofp_port_to_odp_port(ofproto, flow->in_port));
4476 error = dpif_execute(ofproto->backer->dpif, key.data, key.size,
4477 odp_actions, actions_len, packet);
4481 /* Remove 'facet' from 'ofproto' and free up the associated memory:
4483 * - If 'facet' was installed in the datapath, uninstalls it and updates its
4484 * rule's statistics, via subfacet_uninstall().
4486 * - Removes 'facet' from its rule and from ofproto->facets.
4489 facet_remove(struct facet *facet)
4491 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4492 struct subfacet *subfacet, *next_subfacet;
4494 ovs_assert(!list_is_empty(&facet->subfacets));
4496 /* First uninstall all of the subfacets to get final statistics. */
4497 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4498 subfacet_uninstall(subfacet);
4501 /* Flush the final stats to the rule.
4503 * This might require us to have at least one subfacet around so that we
4504 * can use its actions for accounting in facet_account(), which is why we
4505 * have uninstalled but not yet destroyed the subfacets. */
4506 facet_flush_stats(facet);
4508 /* Now we're really all done so destroy everything. */
4509 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
4510 &facet->subfacets) {
4511 subfacet_destroy__(subfacet);
4513 hmap_remove(&ofproto->facets, &facet->hmap_node);
4514 list_remove(&facet->list_node);
4518 /* Feed information from 'facet' back into the learning table to keep it in
4519 * sync with what is actually flowing through the datapath. */
4521 facet_learn(struct facet *facet)
4523 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4524 struct subfacet *subfacet= CONTAINER_OF(list_front(&facet->subfacets),
4525 struct subfacet, list_node);
4526 struct action_xlate_ctx ctx;
4528 if (time_msec() < facet->learn_rl) {
4532 facet->learn_rl = time_msec() + 500;
4534 if (!facet->has_learn
4535 && !facet->has_normal
4536 && (!facet->has_fin_timeout
4537 || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
4541 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
4542 &subfacet->initial_vals,
4543 facet->rule, facet->tcp_flags, NULL);
4544 ctx.may_learn = true;
4545 xlate_actions_for_side_effects(&ctx, facet->rule->up.ofpacts,
4546 facet->rule->up.ofpacts_len);
4550 facet_account(struct facet *facet)
4552 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4553 struct subfacet *subfacet = facet_get_subfacet(facet);
4554 const struct nlattr *a;
4559 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
4562 n_bytes = facet->byte_count - facet->accounted_bytes;
4564 /* This loop feeds byte counters to bond_account() for rebalancing to use
4565 * as a basis. We also need to track the actual VLAN on which the packet
4566 * is going to be sent to ensure that it matches the one passed to
4567 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
4570 * We use the actions from an arbitrary subfacet because they should all
4571 * be equally valid for our purpose. */
4572 vlan_tci = facet->flow.vlan_tci;
4573 NL_ATTR_FOR_EACH_UNSAFE (a, left,
4574 subfacet->actions, subfacet->actions_len) {
4575 const struct ovs_action_push_vlan *vlan;
4576 struct ofport_dpif *port;
4578 switch (nl_attr_type(a)) {
4579 case OVS_ACTION_ATTR_OUTPUT:
4580 port = get_odp_port(ofproto, nl_attr_get_u32(a));
4581 if (port && port->bundle && port->bundle->bond) {
4582 bond_account(port->bundle->bond, &facet->flow,
4583 vlan_tci_to_vid(vlan_tci), n_bytes);
4587 case OVS_ACTION_ATTR_POP_VLAN:
4588 vlan_tci = htons(0);
4591 case OVS_ACTION_ATTR_PUSH_VLAN:
4592 vlan = nl_attr_get(a);
4593 vlan_tci = vlan->vlan_tci;
4599 /* Returns true if the only action for 'facet' is to send to the controller.
4600 * (We don't report NetFlow expiration messages for such facets because they
4601 * are just part of the control logic for the network, not real traffic). */
4603 facet_is_controller_flow(struct facet *facet)
4606 const struct rule *rule = &facet->rule->up;
4607 const struct ofpact *ofpacts = rule->ofpacts;
4608 size_t ofpacts_len = rule->ofpacts_len;
4610 if (ofpacts_len > 0 &&
4611 ofpacts->type == OFPACT_CONTROLLER &&
4612 ofpact_next(ofpacts) >= ofpact_end(ofpacts, ofpacts_len)) {
4619 /* Folds all of 'facet''s statistics into its rule. Also updates the
4620 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
4621 * 'facet''s statistics in the datapath should have been zeroed and folded into
4622 * its packet and byte counts before this function is called. */
4624 facet_flush_stats(struct facet *facet)
4626 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4627 struct subfacet *subfacet;
4629 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4630 ovs_assert(!subfacet->dp_byte_count);
4631 ovs_assert(!subfacet->dp_packet_count);
4634 facet_push_stats(facet);
4635 if (facet->accounted_bytes < facet->byte_count) {
4636 facet_account(facet);
4637 facet->accounted_bytes = facet->byte_count;
4640 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
4641 struct ofexpired expired;
4642 expired.flow = facet->flow;
4643 expired.packet_count = facet->packet_count;
4644 expired.byte_count = facet->byte_count;
4645 expired.used = facet->used;
4646 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4649 facet->rule->packet_count += facet->packet_count;
4650 facet->rule->byte_count += facet->byte_count;
4652 /* Reset counters to prevent double counting if 'facet' ever gets
4654 facet_reset_counters(facet);
4656 netflow_flow_clear(&facet->nf_flow);
4657 facet->tcp_flags = 0;
4660 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
4661 * Returns it if found, otherwise a null pointer.
4663 * 'hash' must be the return value of flow_hash(flow, 0).
4665 * The returned facet might need revalidation; use facet_lookup_valid()
4666 * instead if that is important. */
4667 static struct facet *
4668 facet_find(struct ofproto_dpif *ofproto,
4669 const struct flow *flow, uint32_t hash)
4671 struct facet *facet;
4673 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, hash, &ofproto->facets) {
4674 if (flow_equal(flow, &facet->flow)) {
4682 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
4683 * Returns it if found, otherwise a null pointer.
4685 * 'hash' must be the return value of flow_hash(flow, 0).
4687 * The returned facet is guaranteed to be valid. */
4688 static struct facet *
4689 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow,
4692 struct facet *facet;
4694 facet = facet_find(ofproto, flow, hash);
4696 && (ofproto->backer->need_revalidate
4697 || tag_set_intersects(&ofproto->backer->revalidate_set,
4699 facet_revalidate(facet);
4701 /* facet_revalidate() may have destroyed 'facet'. */
4702 facet = facet_find(ofproto, flow, hash);
4708 /* Return a subfacet from 'facet'. A facet consists of one or more
4709 * subfacets, and this function returns one of them. */
4710 static struct subfacet *facet_get_subfacet(struct facet *facet)
4712 return CONTAINER_OF(list_front(&facet->subfacets), struct subfacet,
4717 subfacet_path_to_string(enum subfacet_path path)
4720 case SF_NOT_INSTALLED:
4721 return "not installed";
4723 return "in fast path";
4725 return "in slow path";
4731 /* Returns the path in which a subfacet should be installed if its 'slow'
4732 * member has the specified value. */
4733 static enum subfacet_path
4734 subfacet_want_path(enum slow_path_reason slow)
4736 return slow ? SF_SLOW_PATH : SF_FAST_PATH;
4739 /* Returns true if 'subfacet' needs to have its datapath flow updated,
4740 * supposing that its actions have been recalculated as 'want_actions' and that
4741 * 'slow' is nonzero iff 'subfacet' should be in the slow path. */
4743 subfacet_should_install(struct subfacet *subfacet, enum slow_path_reason slow,
4744 const struct ofpbuf *want_actions)
4746 enum subfacet_path want_path = subfacet_want_path(slow);
4747 return (want_path != subfacet->path
4748 || (want_path == SF_FAST_PATH
4749 && (subfacet->actions_len != want_actions->size
4750 || memcmp(subfacet->actions, want_actions->data,
4751 subfacet->actions_len))));
4755 facet_check_consistency(struct facet *facet)
4757 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
4759 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4761 uint64_t odp_actions_stub[1024 / 8];
4762 struct ofpbuf odp_actions;
4764 struct rule_dpif *rule;
4765 struct subfacet *subfacet;
4766 bool may_log = false;
4769 /* Check the rule for consistency. */
4770 rule = rule_dpif_lookup(ofproto, &facet->flow);
4771 ok = rule == facet->rule;
4773 may_log = !VLOG_DROP_WARN(&rl);
4778 flow_format(&s, &facet->flow);
4779 ds_put_format(&s, ": facet associated with wrong rule (was "
4780 "table=%"PRIu8",", facet->rule->up.table_id);
4781 cls_rule_format(&facet->rule->up.cr, &s);
4782 ds_put_format(&s, ") (should have been table=%"PRIu8",",
4784 cls_rule_format(&rule->up.cr, &s);
4785 ds_put_char(&s, ')');
4787 VLOG_WARN("%s", ds_cstr(&s));
4792 /* Check the datapath actions for consistency. */
4793 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4794 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4795 enum subfacet_path want_path;
4796 struct action_xlate_ctx ctx;
4799 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
4800 &subfacet->initial_vals, rule, 0, NULL);
4801 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len,
4804 if (subfacet->path == SF_NOT_INSTALLED) {
4805 /* This only happens if the datapath reported an error when we
4806 * tried to install the flow. Don't flag another error here. */
4810 want_path = subfacet_want_path(subfacet->slow);
4811 if (want_path == SF_SLOW_PATH && subfacet->path == SF_SLOW_PATH) {
4812 /* The actions for slow-path flows may legitimately vary from one
4813 * packet to the next. We're done. */
4817 if (!subfacet_should_install(subfacet, subfacet->slow, &odp_actions)) {
4821 /* Inconsistency! */
4823 may_log = !VLOG_DROP_WARN(&rl);
4827 /* Rate-limited, skip reporting. */
4832 odp_flow_key_format(subfacet->key, subfacet->key_len, &s);
4834 ds_put_cstr(&s, ": inconsistency in subfacet");
4835 if (want_path != subfacet->path) {
4836 enum odp_key_fitness fitness = subfacet->key_fitness;
4838 ds_put_format(&s, " (%s, fitness=%s)",
4839 subfacet_path_to_string(subfacet->path),
4840 odp_key_fitness_to_string(fitness));
4841 ds_put_format(&s, " (should have been %s)",
4842 subfacet_path_to_string(want_path));
4843 } else if (want_path == SF_FAST_PATH) {
4844 ds_put_cstr(&s, " (actions were: ");
4845 format_odp_actions(&s, subfacet->actions,
4846 subfacet->actions_len);
4847 ds_put_cstr(&s, ") (correct actions: ");
4848 format_odp_actions(&s, odp_actions.data, odp_actions.size);
4849 ds_put_char(&s, ')');
4851 ds_put_cstr(&s, " (actions: ");
4852 format_odp_actions(&s, subfacet->actions,
4853 subfacet->actions_len);
4854 ds_put_char(&s, ')');
4856 VLOG_WARN("%s", ds_cstr(&s));
4859 ofpbuf_uninit(&odp_actions);
4864 /* Re-searches the classifier for 'facet':
4866 * - If the rule found is different from 'facet''s current rule, moves
4867 * 'facet' to the new rule and recompiles its actions.
4869 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
4870 * where it is and recompiles its actions anyway.
4872 * - If any of 'facet''s subfacets correspond to a new flow according to
4873 * ofproto_receive(), 'facet' is removed. */
4875 facet_revalidate(struct facet *facet)
4877 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4879 struct nlattr *odp_actions;
4882 struct actions *new_actions;
4884 struct action_xlate_ctx ctx;
4885 uint64_t odp_actions_stub[1024 / 8];
4886 struct ofpbuf odp_actions;
4888 struct rule_dpif *new_rule;
4889 struct subfacet *subfacet;
4892 COVERAGE_INC(facet_revalidate);
4894 /* Check that child subfacets still correspond to this facet. Tunnel
4895 * configuration changes could cause a subfacet's OpenFlow in_port to
4897 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4898 struct ofproto_dpif *recv_ofproto;
4899 struct flow recv_flow;
4902 error = ofproto_receive(ofproto->backer, NULL, subfacet->key,
4903 subfacet->key_len, &recv_flow, NULL,
4904 &recv_ofproto, NULL, NULL);
4906 || recv_ofproto != ofproto
4907 || memcmp(&recv_flow, &facet->flow, sizeof recv_flow)) {
4908 facet_remove(facet);
4913 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
4915 /* Calculate new datapath actions.
4917 * We do not modify any 'facet' state yet, because we might need to, e.g.,
4918 * emit a NetFlow expiration and, if so, we need to have the old state
4919 * around to properly compose it. */
4921 /* If the datapath actions changed or the installability changed,
4922 * then we need to talk to the datapath. */
4925 memset(&ctx, 0, sizeof ctx);
4926 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4927 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4928 enum slow_path_reason slow;
4930 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
4931 &subfacet->initial_vals, new_rule, 0, NULL);
4932 xlate_actions(&ctx, new_rule->up.ofpacts, new_rule->up.ofpacts_len,
4935 slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4936 if (subfacet_should_install(subfacet, slow, &odp_actions)) {
4937 struct dpif_flow_stats stats;
4939 subfacet_install(subfacet,
4940 odp_actions.data, odp_actions.size, &stats, slow);
4941 subfacet_update_stats(subfacet, &stats);
4944 new_actions = xcalloc(list_size(&facet->subfacets),
4945 sizeof *new_actions);
4947 new_actions[i].odp_actions = xmemdup(odp_actions.data,
4949 new_actions[i].actions_len = odp_actions.size;
4954 ofpbuf_uninit(&odp_actions);
4957 facet_flush_stats(facet);
4960 /* Update 'facet' now that we've taken care of all the old state. */
4961 facet->tags = ctx.tags;
4962 facet->nf_flow.output_iface = ctx.nf_output_iface;
4963 facet->has_learn = ctx.has_learn;
4964 facet->has_normal = ctx.has_normal;
4965 facet->has_fin_timeout = ctx.has_fin_timeout;
4966 facet->mirrors = ctx.mirrors;
4969 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4970 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4972 if (new_actions && new_actions[i].odp_actions) {
4973 free(subfacet->actions);
4974 subfacet->actions = new_actions[i].odp_actions;
4975 subfacet->actions_len = new_actions[i].actions_len;
4981 if (facet->rule != new_rule) {
4982 COVERAGE_INC(facet_changed_rule);
4983 list_remove(&facet->list_node);
4984 list_push_back(&new_rule->facets, &facet->list_node);
4985 facet->rule = new_rule;
4986 facet->used = new_rule->up.created;
4987 facet->prev_used = facet->used;
4991 /* Updates 'facet''s used time. Caller is responsible for calling
4992 * facet_push_stats() to update the flows which 'facet' resubmits into. */
4994 facet_update_time(struct facet *facet, long long int used)
4996 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4997 if (used > facet->used) {
4999 ofproto_rule_update_used(&facet->rule->up, used);
5000 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
5005 facet_reset_counters(struct facet *facet)
5007 facet->packet_count = 0;
5008 facet->byte_count = 0;
5009 facet->prev_packet_count = 0;
5010 facet->prev_byte_count = 0;
5011 facet->accounted_bytes = 0;
5015 facet_push_stats(struct facet *facet)
5017 struct dpif_flow_stats stats;
5019 ovs_assert(facet->packet_count >= facet->prev_packet_count);
5020 ovs_assert(facet->byte_count >= facet->prev_byte_count);
5021 ovs_assert(facet->used >= facet->prev_used);
5023 stats.n_packets = facet->packet_count - facet->prev_packet_count;
5024 stats.n_bytes = facet->byte_count - facet->prev_byte_count;
5025 stats.used = facet->used;
5026 stats.tcp_flags = 0;
5028 if (stats.n_packets || stats.n_bytes || facet->used > facet->prev_used) {
5029 facet->prev_packet_count = facet->packet_count;
5030 facet->prev_byte_count = facet->byte_count;
5031 facet->prev_used = facet->used;
5033 flow_push_stats(facet, &stats);
5035 update_mirror_stats(ofproto_dpif_cast(facet->rule->up.ofproto),
5036 facet->mirrors, stats.n_packets, stats.n_bytes);
5041 rule_credit_stats(struct rule_dpif *rule, const struct dpif_flow_stats *stats)
5043 rule->packet_count += stats->n_packets;
5044 rule->byte_count += stats->n_bytes;
5045 ofproto_rule_update_used(&rule->up, stats->used);
5048 /* Pushes flow statistics to the rules which 'facet->flow' resubmits
5049 * into given 'facet->rule''s actions and mirrors. */
5051 flow_push_stats(struct facet *facet, const struct dpif_flow_stats *stats)
5053 struct rule_dpif *rule = facet->rule;
5054 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5055 struct subfacet *subfacet = facet_get_subfacet(facet);
5056 struct action_xlate_ctx ctx;
5058 ofproto_rule_update_used(&rule->up, stats->used);
5060 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
5061 &subfacet->initial_vals, rule, 0, NULL);
5062 ctx.resubmit_stats = stats;
5063 xlate_actions_for_side_effects(&ctx, rule->up.ofpacts,
5064 rule->up.ofpacts_len);
5069 static struct subfacet *
5070 subfacet_find(struct ofproto_dpif *ofproto,
5071 const struct nlattr *key, size_t key_len, uint32_t key_hash)
5073 struct subfacet *subfacet;
5075 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
5076 &ofproto->subfacets) {
5077 if (subfacet->key_len == key_len
5078 && !memcmp(key, subfacet->key, key_len)) {
5086 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
5087 * 'key_fitness', 'key', and 'key_len' members in 'miss'. Returns the
5088 * existing subfacet if there is one, otherwise creates and returns a
5091 * If the returned subfacet is new, then subfacet->actions will be NULL, in
5092 * which case the caller must populate the actions with
5093 * subfacet_make_actions(). */
5094 static struct subfacet *
5095 subfacet_create(struct facet *facet, struct flow_miss *miss,
5098 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
5099 enum odp_key_fitness key_fitness = miss->key_fitness;
5100 const struct nlattr *key = miss->key;
5101 size_t key_len = miss->key_len;
5103 struct subfacet *subfacet;
5105 key_hash = odp_flow_key_hash(key, key_len);
5107 if (list_is_empty(&facet->subfacets)) {
5108 subfacet = &facet->one_subfacet;
5110 subfacet = subfacet_find(ofproto, key, key_len, key_hash);
5112 if (subfacet->facet == facet) {
5116 /* This shouldn't happen. */
5117 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
5118 subfacet_destroy(subfacet);
5121 subfacet = xmalloc(sizeof *subfacet);
5124 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
5125 list_push_back(&facet->subfacets, &subfacet->list_node);
5126 subfacet->facet = facet;
5127 subfacet->key_fitness = key_fitness;
5128 subfacet->key = xmemdup(key, key_len);
5129 subfacet->key_len = key_len;
5130 subfacet->used = now;
5131 subfacet->created = now;
5132 subfacet->dp_packet_count = 0;
5133 subfacet->dp_byte_count = 0;
5134 subfacet->actions_len = 0;
5135 subfacet->actions = NULL;
5136 subfacet->slow = (subfacet->key_fitness == ODP_FIT_TOO_LITTLE
5139 subfacet->path = SF_NOT_INSTALLED;
5140 subfacet->initial_vals = miss->initial_vals;
5141 subfacet->odp_in_port = miss->odp_in_port;
5143 ofproto->subfacet_add_count++;
5147 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
5148 * its facet within 'ofproto', and frees it. */
5150 subfacet_destroy__(struct subfacet *subfacet)
5152 struct facet *facet = subfacet->facet;
5153 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
5155 /* Update ofproto stats before uninstall the subfacet. */
5156 ofproto->subfacet_del_count++;
5157 ofproto->total_subfacet_life_span += (time_msec() - subfacet->created);
5159 subfacet_uninstall(subfacet);
5160 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
5161 list_remove(&subfacet->list_node);
5162 free(subfacet->key);
5163 free(subfacet->actions);
5164 if (subfacet != &facet->one_subfacet) {
5169 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
5170 * last remaining subfacet in its facet destroys the facet too. */
5172 subfacet_destroy(struct subfacet *subfacet)
5174 struct facet *facet = subfacet->facet;
5176 if (list_is_singleton(&facet->subfacets)) {
5177 /* facet_remove() needs at least one subfacet (it will remove it). */
5178 facet_remove(facet);
5180 subfacet_destroy__(subfacet);
5185 subfacet_destroy_batch(struct ofproto_dpif *ofproto,
5186 struct subfacet **subfacets, int n)
5188 struct dpif_op ops[SUBFACET_DESTROY_MAX_BATCH];
5189 struct dpif_op *opsp[SUBFACET_DESTROY_MAX_BATCH];
5190 struct dpif_flow_stats stats[SUBFACET_DESTROY_MAX_BATCH];
5193 for (i = 0; i < n; i++) {
5194 ops[i].type = DPIF_OP_FLOW_DEL;
5195 ops[i].u.flow_del.key = subfacets[i]->key;
5196 ops[i].u.flow_del.key_len = subfacets[i]->key_len;
5197 ops[i].u.flow_del.stats = &stats[i];
5201 dpif_operate(ofproto->backer->dpif, opsp, n);
5202 for (i = 0; i < n; i++) {
5203 subfacet_reset_dp_stats(subfacets[i], &stats[i]);
5204 subfacets[i]->path = SF_NOT_INSTALLED;
5205 subfacet_destroy(subfacets[i]);
5209 /* Composes the datapath actions for 'subfacet' based on its rule's actions.
5210 * Translates the actions into 'odp_actions', which the caller must have
5211 * initialized and is responsible for uninitializing. */
5213 subfacet_make_actions(struct subfacet *subfacet, const struct ofpbuf *packet,
5214 struct ofpbuf *odp_actions)
5216 struct facet *facet = subfacet->facet;
5217 struct rule_dpif *rule = facet->rule;
5218 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5220 struct action_xlate_ctx ctx;
5222 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
5223 &subfacet->initial_vals, rule, 0, packet);
5224 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len, odp_actions);
5225 facet->tags = ctx.tags;
5226 facet->has_learn = ctx.has_learn;
5227 facet->has_normal = ctx.has_normal;
5228 facet->has_fin_timeout = ctx.has_fin_timeout;
5229 facet->nf_flow.output_iface = ctx.nf_output_iface;
5230 facet->mirrors = ctx.mirrors;
5232 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
5233 if (subfacet->actions_len != odp_actions->size
5234 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
5235 free(subfacet->actions);
5236 subfacet->actions_len = odp_actions->size;
5237 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
5241 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
5242 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
5243 * in the datapath will be zeroed and 'stats' will be updated with traffic new
5244 * since 'subfacet' was last updated.
5246 * Returns 0 if successful, otherwise a positive errno value. */
5248 subfacet_install(struct subfacet *subfacet,
5249 const struct nlattr *actions, size_t actions_len,
5250 struct dpif_flow_stats *stats,
5251 enum slow_path_reason slow)
5253 struct facet *facet = subfacet->facet;
5254 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
5255 enum subfacet_path path = subfacet_want_path(slow);
5256 uint64_t slow_path_stub[128 / 8];
5257 enum dpif_flow_put_flags flags;
5260 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
5262 flags |= DPIF_FP_ZERO_STATS;
5265 if (path == SF_SLOW_PATH) {
5266 compose_slow_path(ofproto, &facet->flow, slow,
5267 slow_path_stub, sizeof slow_path_stub,
5268 &actions, &actions_len);
5271 ret = dpif_flow_put(ofproto->backer->dpif, flags, subfacet->key,
5272 subfacet->key_len, actions, actions_len, stats);
5275 subfacet_reset_dp_stats(subfacet, stats);
5279 subfacet->path = path;
5285 subfacet_reinstall(struct subfacet *subfacet, struct dpif_flow_stats *stats)
5287 return subfacet_install(subfacet, subfacet->actions, subfacet->actions_len,
5288 stats, subfacet->slow);
5291 /* If 'subfacet' is installed in the datapath, uninstalls it. */
5293 subfacet_uninstall(struct subfacet *subfacet)
5295 if (subfacet->path != SF_NOT_INSTALLED) {
5296 struct rule_dpif *rule = subfacet->facet->rule;
5297 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5298 struct dpif_flow_stats stats;
5301 error = dpif_flow_del(ofproto->backer->dpif, subfacet->key,
5302 subfacet->key_len, &stats);
5303 subfacet_reset_dp_stats(subfacet, &stats);
5305 subfacet_update_stats(subfacet, &stats);
5307 subfacet->path = SF_NOT_INSTALLED;
5309 ovs_assert(subfacet->dp_packet_count == 0);
5310 ovs_assert(subfacet->dp_byte_count == 0);
5314 /* Resets 'subfacet''s datapath statistics counters. This should be called
5315 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
5316 * non-null, it should contain the statistics returned by dpif when 'subfacet'
5317 * was reset in the datapath. 'stats' will be modified to include only
5318 * statistics new since 'subfacet' was last updated. */
5320 subfacet_reset_dp_stats(struct subfacet *subfacet,
5321 struct dpif_flow_stats *stats)
5324 && subfacet->dp_packet_count <= stats->n_packets
5325 && subfacet->dp_byte_count <= stats->n_bytes) {
5326 stats->n_packets -= subfacet->dp_packet_count;
5327 stats->n_bytes -= subfacet->dp_byte_count;
5330 subfacet->dp_packet_count = 0;
5331 subfacet->dp_byte_count = 0;
5334 /* Updates 'subfacet''s used time. The caller is responsible for calling
5335 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
5337 subfacet_update_time(struct subfacet *subfacet, long long int used)
5339 if (used > subfacet->used) {
5340 subfacet->used = used;
5341 facet_update_time(subfacet->facet, used);
5345 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
5347 * Because of the meaning of a subfacet's counters, it only makes sense to do
5348 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
5349 * represents a packet that was sent by hand or if it represents statistics
5350 * that have been cleared out of the datapath. */
5352 subfacet_update_stats(struct subfacet *subfacet,
5353 const struct dpif_flow_stats *stats)
5355 if (stats->n_packets || stats->used > subfacet->used) {
5356 struct facet *facet = subfacet->facet;
5358 subfacet_update_time(subfacet, stats->used);
5359 facet->packet_count += stats->n_packets;
5360 facet->byte_count += stats->n_bytes;
5361 facet->tcp_flags |= stats->tcp_flags;
5362 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
5368 static struct rule_dpif *
5369 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
5371 struct rule_dpif *rule;
5373 rule = rule_dpif_lookup__(ofproto, flow, 0);
5378 return rule_dpif_miss_rule(ofproto, flow);
5381 static struct rule_dpif *
5382 rule_dpif_lookup__(struct ofproto_dpif *ofproto, const struct flow *flow,
5385 struct cls_rule *cls_rule;
5386 struct classifier *cls;
5388 if (table_id >= N_TABLES) {
5392 cls = &ofproto->up.tables[table_id].cls;
5393 if (flow->nw_frag & FLOW_NW_FRAG_ANY
5394 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
5395 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
5396 * are unavailable. */
5397 struct flow ofpc_normal_flow = *flow;
5398 ofpc_normal_flow.tp_src = htons(0);
5399 ofpc_normal_flow.tp_dst = htons(0);
5400 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
5402 cls_rule = classifier_lookup(cls, flow);
5404 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
5407 static struct rule_dpif *
5408 rule_dpif_miss_rule(struct ofproto_dpif *ofproto, const struct flow *flow)
5410 struct ofport_dpif *port;
5412 port = get_ofp_port(ofproto, flow->in_port);
5414 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, flow->in_port);
5415 return ofproto->miss_rule;
5418 if (port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN) {
5419 return ofproto->no_packet_in_rule;
5421 return ofproto->miss_rule;
5425 complete_operation(struct rule_dpif *rule)
5427 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5429 rule_invalidate(rule);
5431 struct dpif_completion *c = xmalloc(sizeof *c);
5432 c->op = rule->up.pending;
5433 list_push_back(&ofproto->completions, &c->list_node);
5435 ofoperation_complete(rule->up.pending, 0);
5439 static struct rule *
5442 struct rule_dpif *rule = xmalloc(sizeof *rule);
5447 rule_dealloc(struct rule *rule_)
5449 struct rule_dpif *rule = rule_dpif_cast(rule_);
5454 rule_construct(struct rule *rule_)
5456 struct rule_dpif *rule = rule_dpif_cast(rule_);
5457 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5458 struct rule_dpif *victim;
5461 rule->packet_count = 0;
5462 rule->byte_count = 0;
5464 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
5465 if (victim && !list_is_empty(&victim->facets)) {
5466 struct facet *facet;
5468 rule->facets = victim->facets;
5469 list_moved(&rule->facets);
5470 LIST_FOR_EACH (facet, list_node, &rule->facets) {
5471 /* XXX: We're only clearing our local counters here. It's possible
5472 * that quite a few packets are unaccounted for in the datapath
5473 * statistics. These will be accounted to the new rule instead of
5474 * cleared as required. This could be fixed by clearing out the
5475 * datapath statistics for this facet, but currently it doesn't
5477 facet_reset_counters(facet);
5481 /* Must avoid list_moved() in this case. */
5482 list_init(&rule->facets);
5485 table_id = rule->up.table_id;
5487 rule->tag = victim->tag;
5488 } else if (table_id == 0) {
5493 miniflow_expand(&rule->up.cr.match.flow, &flow);
5494 rule->tag = rule_calculate_tag(&flow, &rule->up.cr.match.mask,
5495 ofproto->tables[table_id].basis);
5498 complete_operation(rule);
5503 rule_destruct(struct rule *rule_)
5505 struct rule_dpif *rule = rule_dpif_cast(rule_);
5506 struct facet *facet, *next_facet;
5508 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
5509 facet_revalidate(facet);
5512 complete_operation(rule);
5516 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
5518 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule_->ofproto);
5519 struct rule_dpif *rule = rule_dpif_cast(rule_);
5520 struct facet *facet;
5522 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5523 facet_push_stats(facet);
5526 /* Start from historical data for 'rule' itself that are no longer tracked
5527 * in facets. This counts, for example, facets that have expired. */
5528 *packets = rule->packet_count;
5529 *bytes = rule->byte_count;
5531 /* Add any statistics that are tracked by facets. This includes
5532 * statistical data recently updated by ofproto_update_stats() as well as
5533 * stats for packets that were executed "by hand" via dpif_execute(). */
5534 LIST_FOR_EACH (facet, list_node, &rule->facets) {
5535 *packets += facet->packet_count;
5536 *bytes += facet->byte_count;
5541 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
5542 struct ofpbuf *packet)
5544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5545 struct initial_vals initial_vals;
5546 struct dpif_flow_stats stats;
5547 struct action_xlate_ctx ctx;
5548 uint64_t odp_actions_stub[1024 / 8];
5549 struct ofpbuf odp_actions;
5551 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
5552 rule_credit_stats(rule, &stats);
5554 initial_vals.vlan_tci = flow->vlan_tci;
5555 initial_vals.tunnel_ip_tos = flow->tunnel.ip_tos;
5556 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
5557 action_xlate_ctx_init(&ctx, ofproto, flow, &initial_vals,
5558 rule, stats.tcp_flags, packet);
5559 ctx.resubmit_stats = &stats;
5560 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len, &odp_actions);
5562 execute_odp_actions(ofproto, flow, odp_actions.data,
5563 odp_actions.size, packet);
5565 ofpbuf_uninit(&odp_actions);
5569 rule_execute(struct rule *rule, const struct flow *flow,
5570 struct ofpbuf *packet)
5572 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
5573 ofpbuf_delete(packet);
5578 rule_modify_actions(struct rule *rule_)
5580 struct rule_dpif *rule = rule_dpif_cast(rule_);
5582 complete_operation(rule);
5585 /* Sends 'packet' out 'ofport'.
5586 * May modify 'packet'.
5587 * Returns 0 if successful, otherwise a positive errno value. */
5589 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
5591 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
5592 uint64_t odp_actions_stub[1024 / 8];
5593 struct ofpbuf key, odp_actions;
5594 struct odputil_keybuf keybuf;
5599 flow_extract(packet, 0, 0, NULL, OFPP_LOCAL, &flow);
5600 if (netdev_vport_is_patch(ofport->up.netdev)) {
5601 struct ofproto_dpif *peer_ofproto;
5602 struct dpif_flow_stats stats;
5603 struct ofport_dpif *peer;
5604 struct rule_dpif *rule;
5606 peer = ofport_get_peer(ofport);
5611 dpif_flow_stats_extract(&flow, packet, time_msec(), &stats);
5612 netdev_vport_inc_tx(ofport->up.netdev, &stats);
5613 netdev_vport_inc_rx(peer->up.netdev, &stats);
5615 flow.in_port = peer->up.ofp_port;
5616 peer_ofproto = ofproto_dpif_cast(peer->up.ofproto);
5617 rule = rule_dpif_lookup(peer_ofproto, &flow);
5618 rule_dpif_execute(rule, &flow, packet);
5623 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
5625 if (ofport->tnl_port) {
5626 struct dpif_flow_stats stats;
5628 odp_port = tnl_port_send(ofport->tnl_port, &flow);
5629 if (odp_port == OVSP_NONE) {
5633 dpif_flow_stats_extract(&flow, packet, time_msec(), &stats);
5634 netdev_vport_inc_tx(ofport->up.netdev, &stats);
5635 odp_put_tunnel_action(&flow.tunnel, &odp_actions);
5636 odp_put_skb_mark_action(flow.skb_mark, &odp_actions);
5638 odp_port = vsp_realdev_to_vlandev(ofproto, ofport->odp_port,
5640 if (odp_port != ofport->odp_port) {
5641 eth_pop_vlan(packet);
5642 flow.vlan_tci = htons(0);
5646 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5647 odp_flow_key_from_flow(&key, &flow,
5648 ofp_port_to_odp_port(ofproto, flow.in_port));
5650 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
5652 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
5653 error = dpif_execute(ofproto->backer->dpif,
5655 odp_actions.data, odp_actions.size,
5657 ofpbuf_uninit(&odp_actions);
5660 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
5661 ofproto->up.name, odp_port, strerror(error));
5663 ofproto_update_local_port_stats(ofport->up.ofproto, packet->size, 0);
5667 /* OpenFlow to datapath action translation. */
5669 static bool may_receive(const struct ofport_dpif *, struct action_xlate_ctx *);
5670 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
5671 struct action_xlate_ctx *);
5672 static void xlate_normal(struct action_xlate_ctx *);
5674 /* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
5675 * The action will state 'slow' as the reason that the action is in the slow
5676 * path. (This is purely informational: it allows a human viewing "ovs-dpctl
5677 * dump-flows" output to see why a flow is in the slow path.)
5679 * The 'stub_size' bytes in 'stub' will be used to store the action.
5680 * 'stub_size' must be large enough for the action.
5682 * The action and its size will be stored in '*actionsp' and '*actions_lenp',
5685 compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
5686 enum slow_path_reason slow,
5687 uint64_t *stub, size_t stub_size,
5688 const struct nlattr **actionsp, size_t *actions_lenp)
5690 union user_action_cookie cookie;
5693 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
5694 cookie.slow_path.unused = 0;
5695 cookie.slow_path.reason = slow;
5697 ofpbuf_use_stack(&buf, stub, stub_size);
5698 if (slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)) {
5699 uint32_t pid = dpif_port_get_pid(ofproto->backer->dpif, UINT32_MAX);
5700 odp_put_userspace_action(pid, &cookie, sizeof cookie, &buf);
5702 put_userspace_action(ofproto, &buf, flow, &cookie);
5704 *actionsp = buf.data;
5705 *actions_lenp = buf.size;
5709 put_userspace_action(const struct ofproto_dpif *ofproto,
5710 struct ofpbuf *odp_actions,
5711 const struct flow *flow,
5712 const union user_action_cookie *cookie)
5716 pid = dpif_port_get_pid(ofproto->backer->dpif,
5717 ofp_port_to_odp_port(ofproto, flow->in_port));
5719 return odp_put_userspace_action(pid, cookie, sizeof *cookie, odp_actions);
5723 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
5724 ovs_be16 vlan_tci, uint32_t odp_port,
5725 unsigned int n_outputs, union user_action_cookie *cookie)
5729 cookie->type = USER_ACTION_COOKIE_SFLOW;
5730 cookie->sflow.vlan_tci = vlan_tci;
5732 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
5733 * port information") for the interpretation of cookie->output. */
5734 switch (n_outputs) {
5736 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
5737 cookie->sflow.output = 0x40000000 | 256;
5741 ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
5743 cookie->sflow.output = ifindex;
5748 /* 0x80000000 means "multiple output ports. */
5749 cookie->sflow.output = 0x80000000 | n_outputs;
5754 /* Compose SAMPLE action for sFlow. */
5756 compose_sflow_action(const struct ofproto_dpif *ofproto,
5757 struct ofpbuf *odp_actions,
5758 const struct flow *flow,
5761 uint32_t probability;
5762 union user_action_cookie cookie;
5763 size_t sample_offset, actions_offset;
5766 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
5770 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
5772 /* Number of packets out of UINT_MAX to sample. */
5773 probability = dpif_sflow_get_probability(ofproto->sflow);
5774 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
5776 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
5777 compose_sflow_cookie(ofproto, htons(0), odp_port,
5778 odp_port == OVSP_NONE ? 0 : 1, &cookie);
5779 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
5781 nl_msg_end_nested(odp_actions, actions_offset);
5782 nl_msg_end_nested(odp_actions, sample_offset);
5783 return cookie_offset;
5786 /* SAMPLE action must be first action in any given list of actions.
5787 * At this point we do not have all information required to build it. So try to
5788 * build sample action as complete as possible. */
5790 add_sflow_action(struct action_xlate_ctx *ctx)
5792 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
5794 &ctx->flow, OVSP_NONE);
5795 ctx->sflow_odp_port = 0;
5796 ctx->sflow_n_outputs = 0;
5799 /* Fix SAMPLE action according to data collected while composing ODP actions.
5800 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
5801 * USERSPACE action's user-cookie which is required for sflow. */
5803 fix_sflow_action(struct action_xlate_ctx *ctx)
5805 const struct flow *base = &ctx->base_flow;
5806 union user_action_cookie *cookie;
5808 if (!ctx->user_cookie_offset) {
5812 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
5814 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
5816 compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
5817 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
5821 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
5824 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
5825 ovs_be16 flow_vlan_tci = ctx->flow.vlan_tci;
5826 ovs_be64 flow_tun_id = ctx->flow.tunnel.tun_id;
5827 uint8_t flow_nw_tos = ctx->flow.nw_tos;
5828 struct priority_to_dscp *pdscp;
5829 uint32_t out_port, odp_port;
5831 /* If 'struct flow' gets additional metadata, we'll need to zero it out
5832 * before traversing a patch port. */
5833 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
5836 xlate_report(ctx, "Nonexistent output port");
5838 } else if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD) {
5839 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
5841 } else if (check_stp && !stp_forward_in_state(ofport->stp_state)) {
5842 xlate_report(ctx, "STP not in forwarding state, skipping output");
5846 if (netdev_vport_is_patch(ofport->up.netdev)) {
5847 struct ofport_dpif *peer = ofport_get_peer(ofport);
5848 struct flow old_flow = ctx->flow;
5849 const struct ofproto_dpif *peer_ofproto;
5850 enum slow_path_reason special;
5851 struct ofport_dpif *in_port;
5854 xlate_report(ctx, "Nonexistent patch port peer");
5858 peer_ofproto = ofproto_dpif_cast(peer->up.ofproto);
5859 if (peer_ofproto->backer != ctx->ofproto->backer) {
5860 xlate_report(ctx, "Patch port peer on a different datapath");
5864 ctx->ofproto = ofproto_dpif_cast(peer->up.ofproto);
5865 ctx->flow.in_port = peer->up.ofp_port;
5866 ctx->flow.metadata = htonll(0);
5867 memset(&ctx->flow.tunnel, 0, sizeof ctx->flow.tunnel);
5868 memset(ctx->flow.regs, 0, sizeof ctx->flow.regs);
5870 in_port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
5871 special = process_special(ctx->ofproto, &ctx->flow, in_port,
5874 ctx->slow |= special;
5875 } else if (!in_port || may_receive(in_port, ctx)) {
5876 if (!in_port || stp_forward_in_state(in_port->stp_state)) {
5877 xlate_table_action(ctx, ctx->flow.in_port, 0, true);
5879 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
5880 * learning action look at the packet, then drop it. */
5881 struct flow old_base_flow = ctx->base_flow;
5882 size_t old_size = ctx->odp_actions->size;
5883 xlate_table_action(ctx, ctx->flow.in_port, 0, true);
5884 ctx->base_flow = old_base_flow;
5885 ctx->odp_actions->size = old_size;
5889 ctx->flow = old_flow;
5890 ctx->ofproto = ofproto_dpif_cast(ofport->up.ofproto);
5892 if (ctx->resubmit_stats) {
5893 netdev_vport_inc_tx(ofport->up.netdev, ctx->resubmit_stats);
5894 netdev_vport_inc_rx(peer->up.netdev, ctx->resubmit_stats);
5900 pdscp = get_priority(ofport, ctx->flow.skb_priority);
5902 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
5903 ctx->flow.nw_tos |= pdscp->dscp;
5906 if (ofport->tnl_port) {
5907 odp_port = tnl_port_send(ofport->tnl_port, &ctx->flow);
5908 if (odp_port == OVSP_NONE) {
5909 xlate_report(ctx, "Tunneling decided against output");
5913 if (ctx->resubmit_stats) {
5914 netdev_vport_inc_tx(ofport->up.netdev, ctx->resubmit_stats);
5916 out_port = odp_port;
5917 commit_odp_tunnel_action(&ctx->flow, &ctx->base_flow,
5920 odp_port = ofport->odp_port;
5921 out_port = vsp_realdev_to_vlandev(ctx->ofproto, odp_port,
5922 ctx->flow.vlan_tci);
5923 if (out_port != odp_port) {
5924 ctx->flow.vlan_tci = htons(0);
5926 ctx->flow.skb_mark &= ~IPSEC_MARK;
5928 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
5929 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
5931 ctx->sflow_odp_port = odp_port;
5932 ctx->sflow_n_outputs++;
5933 ctx->nf_output_iface = ofp_port;
5934 ctx->flow.tunnel.tun_id = flow_tun_id;
5935 ctx->flow.vlan_tci = flow_vlan_tci;
5936 ctx->flow.nw_tos = flow_nw_tos;
5940 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
5942 compose_output_action__(ctx, ofp_port, true);
5946 tag_the_flow(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5948 struct ofproto_dpif *ofproto = ctx->ofproto;
5949 uint8_t table_id = ctx->table_id;
5951 if (table_id > 0 && table_id < N_TABLES) {
5952 struct table_dpif *table = &ofproto->tables[table_id];
5953 if (table->other_table) {
5954 ctx->tags |= (rule && rule->tag
5956 : rule_calculate_tag(&ctx->flow,
5957 &table->other_table->mask,
5963 /* Common rule processing in one place to avoid duplicating code. */
5964 static struct rule_dpif *
5965 ctx_rule_hooks(struct action_xlate_ctx *ctx, struct rule_dpif *rule,
5968 if (ctx->resubmit_hook) {
5969 ctx->resubmit_hook(ctx, rule);
5971 if (rule == NULL && may_packet_in) {
5973 * check if table configuration flags
5974 * OFPTC_TABLE_MISS_CONTROLLER, default.
5975 * OFPTC_TABLE_MISS_CONTINUE,
5976 * OFPTC_TABLE_MISS_DROP
5977 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
5979 rule = rule_dpif_miss_rule(ctx->ofproto, &ctx->flow);
5981 if (rule && ctx->resubmit_stats) {
5982 rule_credit_stats(rule, ctx->resubmit_stats);
5988 xlate_table_action(struct action_xlate_ctx *ctx,
5989 uint16_t in_port, uint8_t table_id, bool may_packet_in)
5991 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
5992 struct rule_dpif *rule;
5993 uint16_t old_in_port = ctx->flow.in_port;
5994 uint8_t old_table_id = ctx->table_id;
5996 ctx->table_id = table_id;
5998 /* Look up a flow with 'in_port' as the input port. */
5999 ctx->flow.in_port = in_port;
6000 rule = rule_dpif_lookup__(ctx->ofproto, &ctx->flow, table_id);
6002 tag_the_flow(ctx, rule);
6004 /* Restore the original input port. Otherwise OFPP_NORMAL and
6005 * OFPP_IN_PORT will have surprising behavior. */
6006 ctx->flow.in_port = old_in_port;
6008 rule = ctx_rule_hooks(ctx, rule, may_packet_in);
6011 struct rule_dpif *old_rule = ctx->rule;
6015 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
6016 ctx->rule = old_rule;
6020 ctx->table_id = old_table_id;
6022 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
6024 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
6025 MAX_RESUBMIT_RECURSION);
6026 ctx->max_resubmit_trigger = true;
6031 xlate_ofpact_resubmit(struct action_xlate_ctx *ctx,
6032 const struct ofpact_resubmit *resubmit)
6037 in_port = resubmit->in_port;
6038 if (in_port == OFPP_IN_PORT) {
6039 in_port = ctx->flow.in_port;
6042 table_id = resubmit->table_id;
6043 if (table_id == 255) {
6044 table_id = ctx->table_id;
6047 xlate_table_action(ctx, in_port, table_id, false);
6051 flood_packets(struct action_xlate_ctx *ctx, bool all)
6053 struct ofport_dpif *ofport;
6055 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
6056 uint16_t ofp_port = ofport->up.ofp_port;
6058 if (ofp_port == ctx->flow.in_port) {
6063 compose_output_action__(ctx, ofp_port, false);
6064 } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
6065 compose_output_action(ctx, ofp_port);
6069 ctx->nf_output_iface = NF_OUT_FLOOD;
6073 execute_controller_action(struct action_xlate_ctx *ctx, int len,
6074 enum ofp_packet_in_reason reason,
6075 uint16_t controller_id)
6077 struct ofputil_packet_in pin;
6078 struct ofpbuf *packet;
6080 ctx->slow |= SLOW_CONTROLLER;
6085 packet = ofpbuf_clone(ctx->packet);
6087 if (packet->l2 && packet->l3) {
6088 struct eth_header *eh;
6089 uint16_t mpls_depth;
6091 eth_pop_vlan(packet);
6094 memcpy(eh->eth_src, ctx->flow.dl_src, sizeof eh->eth_src);
6095 memcpy(eh->eth_dst, ctx->flow.dl_dst, sizeof eh->eth_dst);
6097 if (ctx->flow.vlan_tci & htons(VLAN_CFI)) {
6098 eth_push_vlan(packet, ctx->flow.vlan_tci);
6101 mpls_depth = eth_mpls_depth(packet);
6103 if (mpls_depth < ctx->flow.mpls_depth) {
6104 push_mpls(packet, ctx->flow.dl_type, ctx->flow.mpls_lse);
6105 } else if (mpls_depth > ctx->flow.mpls_depth) {
6106 pop_mpls(packet, ctx->flow.dl_type);
6107 } else if (mpls_depth) {
6108 set_mpls_lse(packet, ctx->flow.mpls_lse);
6112 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
6113 packet_set_ipv4(packet, ctx->flow.nw_src, ctx->flow.nw_dst,
6114 ctx->flow.nw_tos, ctx->flow.nw_ttl);
6118 if (ctx->flow.nw_proto == IPPROTO_TCP) {
6119 packet_set_tcp_port(packet, ctx->flow.tp_src,
6121 } else if (ctx->flow.nw_proto == IPPROTO_UDP) {
6122 packet_set_udp_port(packet, ctx->flow.tp_src,
6129 pin.packet = packet->data;
6130 pin.packet_len = packet->size;
6131 pin.reason = reason;
6132 pin.controller_id = controller_id;
6133 pin.table_id = ctx->table_id;
6134 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
6137 flow_get_metadata(&ctx->flow, &pin.fmd);
6139 connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
6140 ofpbuf_delete(packet);
6144 execute_mpls_push_action(struct action_xlate_ctx *ctx, ovs_be16 eth_type)
6146 ovs_assert(eth_type_mpls(eth_type));
6148 if (ctx->base_flow.mpls_depth) {
6149 ctx->flow.mpls_lse &= ~htonl(MPLS_BOS_MASK);
6150 ctx->flow.mpls_depth++;
6155 if (ctx->flow.dl_type == htons(ETH_TYPE_IPV6)) {
6156 label = htonl(0x2); /* IPV6 Explicit Null. */
6158 label = htonl(0x0); /* IPV4 Explicit Null. */
6160 tc = (ctx->flow.nw_tos & IP_DSCP_MASK) >> 2;
6161 ttl = ctx->flow.nw_ttl ? ctx->flow.nw_ttl : 0x40;
6162 ctx->flow.mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
6163 ctx->flow.mpls_depth = 1;
6165 ctx->flow.dl_type = eth_type;
6169 execute_mpls_pop_action(struct action_xlate_ctx *ctx, ovs_be16 eth_type)
6171 ovs_assert(eth_type_mpls(ctx->flow.dl_type));
6172 ovs_assert(!eth_type_mpls(eth_type));
6174 if (ctx->flow.mpls_depth) {
6175 ctx->flow.mpls_depth--;
6176 ctx->flow.mpls_lse = htonl(0);
6177 if (!ctx->flow.mpls_depth) {
6178 ctx->flow.dl_type = eth_type;
6184 compose_dec_ttl(struct action_xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
6186 if (ctx->flow.dl_type != htons(ETH_TYPE_IP) &&
6187 ctx->flow.dl_type != htons(ETH_TYPE_IPV6)) {
6191 if (ctx->flow.nw_ttl > 1) {
6197 for (i = 0; i < ids->n_controllers; i++) {
6198 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
6202 /* Stop processing for current table. */
6208 execute_set_mpls_ttl_action(struct action_xlate_ctx *ctx, uint8_t ttl)
6210 if (!eth_type_mpls(ctx->flow.dl_type)) {
6214 set_mpls_lse_ttl(&ctx->flow.mpls_lse, ttl);
6219 execute_dec_mpls_ttl_action(struct action_xlate_ctx *ctx)
6221 uint8_t ttl = mpls_lse_to_ttl(ctx->flow.mpls_lse);
6223 if (!eth_type_mpls(ctx->flow.dl_type)) {
6229 set_mpls_lse_ttl(&ctx->flow.mpls_lse, ttl);
6232 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
6234 /* Stop processing for current table. */
6240 xlate_output_action(struct action_xlate_ctx *ctx,
6241 uint16_t port, uint16_t max_len, bool may_packet_in)
6243 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
6245 ctx->nf_output_iface = NF_OUT_DROP;
6249 compose_output_action(ctx, ctx->flow.in_port);
6252 xlate_table_action(ctx, ctx->flow.in_port, 0, may_packet_in);
6258 flood_packets(ctx, false);
6261 flood_packets(ctx, true);
6263 case OFPP_CONTROLLER:
6264 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
6270 if (port != ctx->flow.in_port) {
6271 compose_output_action(ctx, port);
6273 xlate_report(ctx, "skipping output to input port");
6278 if (prev_nf_output_iface == NF_OUT_FLOOD) {
6279 ctx->nf_output_iface = NF_OUT_FLOOD;
6280 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
6281 ctx->nf_output_iface = prev_nf_output_iface;
6282 } else if (prev_nf_output_iface != NF_OUT_DROP &&
6283 ctx->nf_output_iface != NF_OUT_FLOOD) {
6284 ctx->nf_output_iface = NF_OUT_MULTI;
6289 xlate_output_reg_action(struct action_xlate_ctx *ctx,
6290 const struct ofpact_output_reg *or)
6292 uint64_t port = mf_get_subfield(&or->src, &ctx->flow);
6293 if (port <= UINT16_MAX) {
6294 xlate_output_action(ctx, port, or->max_len, false);
6299 xlate_enqueue_action(struct action_xlate_ctx *ctx,
6300 const struct ofpact_enqueue *enqueue)
6302 uint16_t ofp_port = enqueue->port;
6303 uint32_t queue_id = enqueue->queue;
6304 uint32_t flow_priority, priority;
6307 /* Translate queue to priority. */
6308 error = dpif_queue_to_priority(ctx->ofproto->backer->dpif,
6309 queue_id, &priority);
6311 /* Fall back to ordinary output action. */
6312 xlate_output_action(ctx, enqueue->port, 0, false);
6316 /* Check output port. */
6317 if (ofp_port == OFPP_IN_PORT) {
6318 ofp_port = ctx->flow.in_port;
6319 } else if (ofp_port == ctx->flow.in_port) {
6323 /* Add datapath actions. */
6324 flow_priority = ctx->flow.skb_priority;
6325 ctx->flow.skb_priority = priority;
6326 compose_output_action(ctx, ofp_port);
6327 ctx->flow.skb_priority = flow_priority;
6329 /* Update NetFlow output port. */
6330 if (ctx->nf_output_iface == NF_OUT_DROP) {
6331 ctx->nf_output_iface = ofp_port;
6332 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
6333 ctx->nf_output_iface = NF_OUT_MULTI;
6338 xlate_set_queue_action(struct action_xlate_ctx *ctx, uint32_t queue_id)
6340 uint32_t skb_priority;
6342 if (!dpif_queue_to_priority(ctx->ofproto->backer->dpif,
6343 queue_id, &skb_priority)) {
6344 ctx->flow.skb_priority = skb_priority;
6346 /* Couldn't translate queue to a priority. Nothing to do. A warning
6347 * has already been logged. */
6351 struct xlate_reg_state {
6357 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
6359 struct ofproto_dpif *ofproto = ofproto_;
6360 struct ofport_dpif *port;
6370 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
6373 port = get_ofp_port(ofproto, ofp_port);
6374 return port ? port->may_enable : false;
6379 xlate_bundle_action(struct action_xlate_ctx *ctx,
6380 const struct ofpact_bundle *bundle)
6384 port = bundle_execute(bundle, &ctx->flow, slave_enabled_cb, ctx->ofproto);
6385 if (bundle->dst.field) {
6386 nxm_reg_load(&bundle->dst, port, &ctx->flow);
6388 xlate_output_action(ctx, port, 0, false);
6393 xlate_learn_action(struct action_xlate_ctx *ctx,
6394 const struct ofpact_learn *learn)
6396 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
6397 struct ofputil_flow_mod fm;
6398 uint64_t ofpacts_stub[1024 / 8];
6399 struct ofpbuf ofpacts;
6402 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
6403 learn_execute(learn, &ctx->flow, &fm, &ofpacts);
6405 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
6406 if (error && !VLOG_DROP_WARN(&rl)) {
6407 VLOG_WARN("learning action failed to modify flow table (%s)",
6408 ofperr_get_name(error));
6411 ofpbuf_uninit(&ofpacts);
6414 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
6415 * means "infinite". */
6417 reduce_timeout(uint16_t max, uint16_t *timeout)
6419 if (max && (!*timeout || *timeout > max)) {
6425 xlate_fin_timeout(struct action_xlate_ctx *ctx,
6426 const struct ofpact_fin_timeout *oft)
6428 if (ctx->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
6429 struct rule_dpif *rule = ctx->rule;
6431 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
6432 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
6437 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
6439 if (port->up.pp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
6440 ? OFPUTIL_PC_NO_RECV_STP
6441 : OFPUTIL_PC_NO_RECV)) {
6445 /* Only drop packets here if both forwarding and learning are
6446 * disabled. If just learning is enabled, we need to have
6447 * OFPP_NORMAL and the learning action have a look at the packet
6448 * before we can drop it. */
6449 if (!stp_forward_in_state(port->stp_state)
6450 && !stp_learn_in_state(port->stp_state)) {
6458 tunnel_ecn_ok(struct action_xlate_ctx *ctx)
6460 if (is_ip_any(&ctx->base_flow)
6461 && (ctx->base_flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
6462 if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
6463 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
6464 " but is not ECN capable");
6467 /* Set the ECN CE value in the tunneled packet. */
6468 ctx->flow.nw_tos |= IP_ECN_CE;
6476 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
6477 struct action_xlate_ctx *ctx)
6479 bool was_evictable = true;
6480 const struct ofpact *a;
6483 /* Don't let the rule we're working on get evicted underneath us. */
6484 was_evictable = ctx->rule->up.evictable;
6485 ctx->rule->up.evictable = false;
6488 do_xlate_actions_again:
6489 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6490 struct ofpact_controller *controller;
6491 const struct ofpact_metadata *metadata;
6499 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
6500 ofpact_get_OUTPUT(a)->max_len, true);
6503 case OFPACT_CONTROLLER:
6504 controller = ofpact_get_CONTROLLER(a);
6505 execute_controller_action(ctx, controller->max_len,
6507 controller->controller_id);
6510 case OFPACT_ENQUEUE:
6511 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
6514 case OFPACT_SET_VLAN_VID:
6515 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
6516 ctx->flow.vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
6520 case OFPACT_SET_VLAN_PCP:
6521 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
6522 ctx->flow.vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
6527 case OFPACT_STRIP_VLAN:
6528 ctx->flow.vlan_tci = htons(0);
6531 case OFPACT_PUSH_VLAN:
6532 /* XXX 802.1AD(QinQ) */
6533 ctx->flow.vlan_tci = htons(VLAN_CFI);
6536 case OFPACT_SET_ETH_SRC:
6537 memcpy(ctx->flow.dl_src, ofpact_get_SET_ETH_SRC(a)->mac,
6541 case OFPACT_SET_ETH_DST:
6542 memcpy(ctx->flow.dl_dst, ofpact_get_SET_ETH_DST(a)->mac,
6546 case OFPACT_SET_IPV4_SRC:
6547 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
6548 ctx->flow.nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
6552 case OFPACT_SET_IPV4_DST:
6553 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
6554 ctx->flow.nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
6558 case OFPACT_SET_IPV4_DSCP:
6559 /* OpenFlow 1.0 only supports IPv4. */
6560 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
6561 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
6562 ctx->flow.nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
6566 case OFPACT_SET_L4_SRC_PORT:
6567 if (is_ip_any(&ctx->flow)) {
6568 ctx->flow.tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
6572 case OFPACT_SET_L4_DST_PORT:
6573 if (is_ip_any(&ctx->flow)) {
6574 ctx->flow.tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
6578 case OFPACT_RESUBMIT:
6579 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
6582 case OFPACT_SET_TUNNEL:
6583 ctx->flow.tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
6586 case OFPACT_SET_QUEUE:
6587 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
6590 case OFPACT_POP_QUEUE:
6591 ctx->flow.skb_priority = ctx->orig_skb_priority;
6594 case OFPACT_REG_MOVE:
6595 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), &ctx->flow);
6598 case OFPACT_REG_LOAD:
6599 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), &ctx->flow);
6602 case OFPACT_STACK_PUSH:
6603 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), &ctx->flow,
6607 case OFPACT_STACK_POP:
6608 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), &ctx->flow,
6612 case OFPACT_PUSH_MPLS:
6613 execute_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
6616 case OFPACT_POP_MPLS:
6617 execute_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
6620 case OFPACT_SET_MPLS_TTL:
6621 if (execute_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl)) {
6626 case OFPACT_DEC_MPLS_TTL:
6627 if (execute_dec_mpls_ttl_action(ctx)) {
6632 case OFPACT_DEC_TTL:
6633 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
6639 /* Nothing to do. */
6642 case OFPACT_MULTIPATH:
6643 multipath_execute(ofpact_get_MULTIPATH(a), &ctx->flow);
6647 ctx->ofproto->has_bundle_action = true;
6648 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
6651 case OFPACT_OUTPUT_REG:
6652 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
6656 ctx->has_learn = true;
6657 if (ctx->may_learn) {
6658 xlate_learn_action(ctx, ofpact_get_LEARN(a));
6666 case OFPACT_FIN_TIMEOUT:
6667 ctx->has_fin_timeout = true;
6668 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
6671 case OFPACT_CLEAR_ACTIONS:
6673 * Nothing to do because writa-actions is not supported for now.
6674 * When writa-actions is supported, clear-actions also must
6675 * be supported at the same time.
6679 case OFPACT_WRITE_METADATA:
6680 metadata = ofpact_get_WRITE_METADATA(a);
6681 ctx->flow.metadata &= ~metadata->mask;
6682 ctx->flow.metadata |= metadata->metadata & metadata->mask;
6685 case OFPACT_GOTO_TABLE: {
6686 /* It is assumed that goto-table is the last action. */
6687 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
6688 struct rule_dpif *rule;
6690 ovs_assert(ctx->table_id < ogt->table_id);
6692 ctx->table_id = ogt->table_id;
6694 /* Look up a flow from the new table. */
6695 rule = rule_dpif_lookup__(ctx->ofproto, &ctx->flow, ctx->table_id);
6697 tag_the_flow(ctx, rule);
6699 rule = ctx_rule_hooks(ctx, rule, true);
6703 ctx->rule->up.evictable = was_evictable;
6706 was_evictable = rule->up.evictable;
6707 rule->up.evictable = false;
6709 /* Tail recursion removal. */
6710 ofpacts = rule->up.ofpacts;
6711 ofpacts_len = rule->up.ofpacts_len;
6712 goto do_xlate_actions_again;
6721 ctx->rule->up.evictable = was_evictable;
6726 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
6727 struct ofproto_dpif *ofproto, const struct flow *flow,
6728 const struct initial_vals *initial_vals,
6729 struct rule_dpif *rule,
6730 uint8_t tcp_flags, const struct ofpbuf *packet)
6732 ovs_be64 initial_tun_id = flow->tunnel.tun_id;
6734 /* Flow initialization rules:
6735 * - 'base_flow' must match the kernel's view of the packet at the
6736 * time that action processing starts. 'flow' represents any
6737 * transformations we wish to make through actions.
6738 * - By default 'base_flow' and 'flow' are the same since the input
6739 * packet matches the output before any actions are applied.
6740 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
6741 * of the received packet as seen by the kernel. If we later output
6742 * to another device without any modifications this will cause us to
6743 * insert a new tag since the original one was stripped off by the
6745 * - Tunnel 'flow' is largely cleared when transitioning between
6746 * the input and output stages since it does not make sense to output
6747 * a packet with the exact headers that it was received with (i.e.
6748 * the destination IP is us). The one exception is the tun_id, which
6749 * is preserved to allow use in later resubmit lookups and loads into
6751 * - Tunnel 'base_flow' is completely cleared since that is what the
6752 * kernel does. If we wish to maintain the original values an action
6753 * needs to be generated. */
6755 ctx->ofproto = ofproto;
6757 memset(&ctx->flow.tunnel, 0, sizeof ctx->flow.tunnel);
6758 ctx->base_flow = ctx->flow;
6759 ctx->base_flow.vlan_tci = initial_vals->vlan_tci;
6760 ctx->base_flow.tunnel.ip_tos = initial_vals->tunnel_ip_tos;
6761 ctx->flow.tunnel.tun_id = initial_tun_id;
6763 ctx->packet = packet;
6764 ctx->may_learn = packet != NULL;
6765 ctx->tcp_flags = tcp_flags;
6766 ctx->resubmit_hook = NULL;
6767 ctx->report_hook = NULL;
6768 ctx->resubmit_stats = NULL;
6771 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
6772 * into datapath actions in 'odp_actions', using 'ctx'. */
6774 xlate_actions(struct action_xlate_ctx *ctx,
6775 const struct ofpact *ofpacts, size_t ofpacts_len,
6776 struct ofpbuf *odp_actions)
6778 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
6779 * that in the future we always keep a copy of the original flow for
6780 * tracing purposes. */
6781 static bool hit_resubmit_limit;
6783 enum slow_path_reason special;
6784 struct ofport_dpif *in_port;
6785 struct flow orig_flow;
6787 COVERAGE_INC(ofproto_dpif_xlate);
6789 ofpbuf_clear(odp_actions);
6790 ofpbuf_reserve(odp_actions, NL_A_U32_SIZE);
6792 ctx->odp_actions = odp_actions;
6795 ctx->has_learn = false;
6796 ctx->has_normal = false;
6797 ctx->has_fin_timeout = false;
6798 ctx->nf_output_iface = NF_OUT_DROP;
6801 ctx->max_resubmit_trigger = false;
6802 ctx->orig_skb_priority = ctx->flow.skb_priority;
6806 ofpbuf_use_stub(&ctx->stack, ctx->init_stack, sizeof ctx->init_stack);
6808 if (ctx->ofproto->has_mirrors || hit_resubmit_limit) {
6809 /* Do this conditionally because the copy is expensive enough that it
6810 * shows up in profiles. */
6811 orig_flow = ctx->flow;
6814 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
6815 switch (ctx->ofproto->up.frag_handling) {
6816 case OFPC_FRAG_NORMAL:
6817 /* We must pretend that transport ports are unavailable. */
6818 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
6819 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
6822 case OFPC_FRAG_DROP:
6825 case OFPC_FRAG_REASM:
6828 case OFPC_FRAG_NX_MATCH:
6829 /* Nothing to do. */
6832 case OFPC_INVALID_TTL_TO_CONTROLLER:
6837 in_port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
6838 special = process_special(ctx->ofproto, &ctx->flow, in_port, ctx->packet);
6840 ctx->slow |= special;
6842 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
6843 struct initial_vals initial_vals;
6844 uint32_t local_odp_port;
6846 initial_vals.vlan_tci = ctx->base_flow.vlan_tci;
6847 initial_vals.tunnel_ip_tos = ctx->base_flow.tunnel.ip_tos;
6849 add_sflow_action(ctx);
6851 if (tunnel_ecn_ok(ctx) && (!in_port || may_receive(in_port, ctx))) {
6852 do_xlate_actions(ofpacts, ofpacts_len, ctx);
6854 /* We've let OFPP_NORMAL and the learning action look at the
6855 * packet, so drop it now if forwarding is disabled. */
6856 if (in_port && !stp_forward_in_state(in_port->stp_state)) {
6857 ofpbuf_clear(ctx->odp_actions);
6858 add_sflow_action(ctx);
6862 if (ctx->max_resubmit_trigger && !ctx->resubmit_hook) {
6863 if (!hit_resubmit_limit) {
6864 /* We didn't record the original flow. Make sure we do from
6866 hit_resubmit_limit = true;
6867 } else if (!VLOG_DROP_ERR(&trace_rl)) {
6868 struct ds ds = DS_EMPTY_INITIALIZER;
6870 ofproto_trace(ctx->ofproto, &orig_flow, ctx->packet,
6871 &initial_vals, &ds);
6872 VLOG_ERR("Trace triggered by excessive resubmit "
6873 "recursion:\n%s", ds_cstr(&ds));
6878 local_odp_port = ofp_port_to_odp_port(ctx->ofproto, OFPP_LOCAL);
6879 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
6881 ctx->odp_actions->data,
6882 ctx->odp_actions->size)) {
6883 ctx->slow |= SLOW_IN_BAND;
6885 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
6887 compose_output_action(ctx, OFPP_LOCAL);
6890 if (ctx->ofproto->has_mirrors) {
6891 add_mirror_actions(ctx, &orig_flow);
6893 fix_sflow_action(ctx);
6896 ofpbuf_uninit(&ctx->stack);
6899 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
6900 * into datapath actions, using 'ctx', and discards the datapath actions. */
6902 xlate_actions_for_side_effects(struct action_xlate_ctx *ctx,
6903 const struct ofpact *ofpacts,
6906 uint64_t odp_actions_stub[1024 / 8];
6907 struct ofpbuf odp_actions;
6909 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
6910 xlate_actions(ctx, ofpacts, ofpacts_len, &odp_actions);
6911 ofpbuf_uninit(&odp_actions);
6915 xlate_report(struct action_xlate_ctx *ctx, const char *s)
6917 if (ctx->report_hook) {
6918 ctx->report_hook(ctx, s);
6922 /* OFPP_NORMAL implementation. */
6924 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
6926 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
6927 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
6928 * the bundle on which the packet was received, returns the VLAN to which the
6931 * Both 'vid' and the return value are in the range 0...4095. */
6933 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
6935 switch (in_bundle->vlan_mode) {
6936 case PORT_VLAN_ACCESS:
6937 return in_bundle->vlan;
6940 case PORT_VLAN_TRUNK:
6943 case PORT_VLAN_NATIVE_UNTAGGED:
6944 case PORT_VLAN_NATIVE_TAGGED:
6945 return vid ? vid : in_bundle->vlan;
6952 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
6953 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
6956 * 'vid' should be the VID obtained from the 802.1Q header that was received as
6957 * part of a packet (specify 0 if there was no 802.1Q header), in the range
6960 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
6962 /* Allow any VID on the OFPP_NONE port. */
6963 if (in_bundle == &ofpp_none_bundle) {
6967 switch (in_bundle->vlan_mode) {
6968 case PORT_VLAN_ACCESS:
6971 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6972 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
6973 "packet received on port %s configured as VLAN "
6974 "%"PRIu16" access port",
6975 in_bundle->ofproto->up.name, vid,
6976 in_bundle->name, in_bundle->vlan);
6982 case PORT_VLAN_NATIVE_UNTAGGED:
6983 case PORT_VLAN_NATIVE_TAGGED:
6985 /* Port must always carry its native VLAN. */
6989 case PORT_VLAN_TRUNK:
6990 if (!ofbundle_includes_vlan(in_bundle, vid)) {
6992 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6993 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
6994 "received on port %s not configured for trunking "
6996 in_bundle->ofproto->up.name, vid,
6997 in_bundle->name, vid);
7009 /* Given 'vlan', the VLAN that a packet belongs to, and
7010 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
7011 * that should be included in the 802.1Q header. (If the return value is 0,
7012 * then the 802.1Q header should only be included in the packet if there is a
7015 * Both 'vlan' and the return value are in the range 0...4095. */
7017 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
7019 switch (out_bundle->vlan_mode) {
7020 case PORT_VLAN_ACCESS:
7023 case PORT_VLAN_TRUNK:
7024 case PORT_VLAN_NATIVE_TAGGED:
7027 case PORT_VLAN_NATIVE_UNTAGGED:
7028 return vlan == out_bundle->vlan ? 0 : vlan;
7036 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
7039 struct ofport_dpif *port;
7041 ovs_be16 tci, old_tci;
7043 vid = output_vlan_to_vid(out_bundle, vlan);
7044 if (!out_bundle->bond) {
7045 port = ofbundle_get_a_port(out_bundle);
7047 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
7050 /* No slaves enabled, so drop packet. */
7055 old_tci = ctx->flow.vlan_tci;
7057 if (tci || out_bundle->use_priority_tags) {
7058 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
7060 tci |= htons(VLAN_CFI);
7063 ctx->flow.vlan_tci = tci;
7065 compose_output_action(ctx, port->up.ofp_port);
7066 ctx->flow.vlan_tci = old_tci;
7070 mirror_mask_ffs(mirror_mask_t mask)
7072 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
7077 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
7079 return (bundle->vlan_mode != PORT_VLAN_ACCESS
7080 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
7084 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
7086 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
7089 /* Returns an arbitrary interface within 'bundle'. */
7090 static struct ofport_dpif *
7091 ofbundle_get_a_port(const struct ofbundle *bundle)
7093 return CONTAINER_OF(list_front(&bundle->ports),
7094 struct ofport_dpif, bundle_node);
7098 vlan_is_mirrored(const struct ofmirror *m, int vlan)
7100 return !m->vlans || bitmap_is_set(m->vlans, vlan);
7104 add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
7106 struct ofproto_dpif *ofproto = ctx->ofproto;
7107 mirror_mask_t mirrors;
7108 struct ofbundle *in_bundle;
7111 const struct nlattr *a;
7114 in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
7115 ctx->packet != NULL, NULL);
7119 mirrors = in_bundle->src_mirrors;
7121 /* Drop frames on bundles reserved for mirroring. */
7122 if (in_bundle->mirror_out) {
7123 if (ctx->packet != NULL) {
7124 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7125 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
7126 "%s, which is reserved exclusively for mirroring",
7127 ctx->ofproto->up.name, in_bundle->name);
7133 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
7134 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
7137 vlan = input_vid_to_vlan(in_bundle, vid);
7139 /* Look at the output ports to check for destination selections. */
7141 NL_ATTR_FOR_EACH (a, left, ctx->odp_actions->data,
7142 ctx->odp_actions->size) {
7143 enum ovs_action_attr type = nl_attr_type(a);
7144 struct ofport_dpif *ofport;
7146 if (type != OVS_ACTION_ATTR_OUTPUT) {
7150 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
7151 if (ofport && ofport->bundle) {
7152 mirrors |= ofport->bundle->dst_mirrors;
7160 /* Restore the original packet before adding the mirror actions. */
7161 ctx->flow = *orig_flow;
7166 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
7168 if (!vlan_is_mirrored(m, vlan)) {
7169 mirrors = zero_rightmost_1bit(mirrors);
7173 mirrors &= ~m->dup_mirrors;
7174 ctx->mirrors |= m->dup_mirrors;
7176 output_normal(ctx, m->out, vlan);
7177 } else if (vlan != m->out_vlan
7178 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
7179 struct ofbundle *bundle;
7181 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
7182 if (ofbundle_includes_vlan(bundle, m->out_vlan)
7183 && !bundle->mirror_out) {
7184 output_normal(ctx, bundle, m->out_vlan);
7192 update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
7193 uint64_t packets, uint64_t bytes)
7199 for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
7202 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
7205 /* In normal circumstances 'm' will not be NULL. However,
7206 * if mirrors are reconfigured, we can temporarily get out
7207 * of sync in facet_revalidate(). We could "correct" the
7208 * mirror list before reaching here, but doing that would
7209 * not properly account the traffic stats we've currently
7210 * accumulated for previous mirror configuration. */
7214 m->packet_count += packets;
7215 m->byte_count += bytes;
7219 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
7220 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
7221 * indicate this; newer upstream kernels use gratuitous ARP requests. */
7223 is_gratuitous_arp(const struct flow *flow)
7225 return (flow->dl_type == htons(ETH_TYPE_ARP)
7226 && eth_addr_is_broadcast(flow->dl_dst)
7227 && (flow->nw_proto == ARP_OP_REPLY
7228 || (flow->nw_proto == ARP_OP_REQUEST
7229 && flow->nw_src == flow->nw_dst)));
7233 update_learning_table(struct ofproto_dpif *ofproto,
7234 const struct flow *flow, int vlan,
7235 struct ofbundle *in_bundle)
7237 struct mac_entry *mac;
7239 /* Don't learn the OFPP_NONE port. */
7240 if (in_bundle == &ofpp_none_bundle) {
7244 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
7248 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
7249 if (is_gratuitous_arp(flow)) {
7250 /* We don't want to learn from gratuitous ARP packets that are
7251 * reflected back over bond slaves so we lock the learning table. */
7252 if (!in_bundle->bond) {
7253 mac_entry_set_grat_arp_lock(mac);
7254 } else if (mac_entry_is_grat_arp_locked(mac)) {
7259 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
7260 /* The log messages here could actually be useful in debugging,
7261 * so keep the rate limit relatively high. */
7262 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
7263 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
7264 "on port %s in VLAN %d",
7265 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
7266 in_bundle->name, vlan);
7268 mac->port.p = in_bundle;
7269 tag_set_add(&ofproto->backer->revalidate_set,
7270 mac_learning_changed(ofproto->ml, mac));
7274 static struct ofbundle *
7275 lookup_input_bundle(const struct ofproto_dpif *ofproto, uint16_t in_port,
7276 bool warn, struct ofport_dpif **in_ofportp)
7278 struct ofport_dpif *ofport;
7280 /* Find the port and bundle for the received packet. */
7281 ofport = get_ofp_port(ofproto, in_port);
7283 *in_ofportp = ofport;
7285 if (ofport && ofport->bundle) {
7286 return ofport->bundle;
7289 /* Special-case OFPP_NONE, which a controller may use as the ingress
7290 * port for traffic that it is sourcing. */
7291 if (in_port == OFPP_NONE) {
7292 return &ofpp_none_bundle;
7295 /* Odd. A few possible reasons here:
7297 * - We deleted a port but there are still a few packets queued up
7300 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
7301 * we don't know about.
7303 * - The ofproto client didn't configure the port as part of a bundle.
7304 * This is particularly likely to happen if a packet was received on the
7305 * port after it was created, but before the client had a chance to
7306 * configure its bundle.
7309 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7311 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
7312 "port %"PRIu16, ofproto->up.name, in_port);
7317 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
7318 * dropped. Returns true if they may be forwarded, false if they should be
7321 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
7322 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
7324 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
7325 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
7326 * checked by input_vid_is_valid().
7328 * May also add tags to '*tags', although the current implementation only does
7329 * so in one special case.
7332 is_admissible(struct action_xlate_ctx *ctx, struct ofport_dpif *in_port,
7335 struct ofproto_dpif *ofproto = ctx->ofproto;
7336 struct flow *flow = &ctx->flow;
7337 struct ofbundle *in_bundle = in_port->bundle;
7339 /* Drop frames for reserved multicast addresses
7340 * only if forward_bpdu option is absent. */
7341 if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
7342 xlate_report(ctx, "packet has reserved destination MAC, dropping");
7346 if (in_bundle->bond) {
7347 struct mac_entry *mac;
7349 switch (bond_check_admissibility(in_bundle->bond, in_port,
7350 flow->dl_dst, &ctx->tags)) {
7355 xlate_report(ctx, "bonding refused admissibility, dropping");
7358 case BV_DROP_IF_MOVED:
7359 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
7360 if (mac && mac->port.p != in_bundle &&
7361 (!is_gratuitous_arp(flow)
7362 || mac_entry_is_grat_arp_locked(mac))) {
7363 xlate_report(ctx, "SLB bond thinks this packet looped back, "
7375 xlate_normal(struct action_xlate_ctx *ctx)
7377 struct ofport_dpif *in_port;
7378 struct ofbundle *in_bundle;
7379 struct mac_entry *mac;
7383 ctx->has_normal = true;
7385 in_bundle = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
7386 ctx->packet != NULL, &in_port);
7388 xlate_report(ctx, "no input bundle, dropping");
7392 /* Drop malformed frames. */
7393 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
7394 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
7395 if (ctx->packet != NULL) {
7396 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7397 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
7398 "VLAN tag received on port %s",
7399 ctx->ofproto->up.name, in_bundle->name);
7401 xlate_report(ctx, "partial VLAN tag, dropping");
7405 /* Drop frames on bundles reserved for mirroring. */
7406 if (in_bundle->mirror_out) {
7407 if (ctx->packet != NULL) {
7408 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7409 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
7410 "%s, which is reserved exclusively for mirroring",
7411 ctx->ofproto->up.name, in_bundle->name);
7413 xlate_report(ctx, "input port is mirror output port, dropping");
7418 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
7419 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
7420 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
7423 vlan = input_vid_to_vlan(in_bundle, vid);
7425 /* Check other admissibility requirements. */
7426 if (in_port && !is_admissible(ctx, in_port, vlan)) {
7430 /* Learn source MAC. */
7431 if (ctx->may_learn) {
7432 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
7435 /* Determine output bundle. */
7436 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
7439 if (mac->port.p != in_bundle) {
7440 xlate_report(ctx, "forwarding to learned port");
7441 output_normal(ctx, mac->port.p, vlan);
7443 xlate_report(ctx, "learned port is input port, dropping");
7446 struct ofbundle *bundle;
7448 xlate_report(ctx, "no learned MAC for destination, flooding");
7449 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
7450 if (bundle != in_bundle
7451 && ofbundle_includes_vlan(bundle, vlan)
7452 && bundle->floodable
7453 && !bundle->mirror_out) {
7454 output_normal(ctx, bundle, vlan);
7457 ctx->nf_output_iface = NF_OUT_FLOOD;
7461 /* Optimized flow revalidation.
7463 * It's a difficult problem, in general, to tell which facets need to have
7464 * their actions recalculated whenever the OpenFlow flow table changes. We
7465 * don't try to solve that general problem: for most kinds of OpenFlow flow
7466 * table changes, we recalculate the actions for every facet. This is
7467 * relatively expensive, but it's good enough if the OpenFlow flow table
7468 * doesn't change very often.
7470 * However, we can expect one particular kind of OpenFlow flow table change to
7471 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
7472 * of CPU on revalidating every facet whenever MAC learning modifies the flow
7473 * table, we add a special case that applies to flow tables in which every rule
7474 * has the same form (that is, the same wildcards), except that the table is
7475 * also allowed to have a single "catch-all" flow that matches all packets. We
7476 * optimize this case by tagging all of the facets that resubmit into the table
7477 * and invalidating the same tag whenever a flow changes in that table. The
7478 * end result is that we revalidate just the facets that need it (and sometimes
7479 * a few more, but not all of the facets or even all of the facets that
7480 * resubmit to the table modified by MAC learning). */
7482 /* Calculates the tag to use for 'flow' and mask 'mask' when it is inserted
7483 * into an OpenFlow table with the given 'basis'. */
7485 rule_calculate_tag(const struct flow *flow, const struct minimask *mask,
7488 if (minimask_is_catchall(mask)) {
7491 uint32_t hash = flow_hash_in_minimask(flow, mask, secret);
7492 return tag_create_deterministic(hash);
7496 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
7497 * taggability of that table.
7499 * This function must be called after *each* change to a flow table. If you
7500 * skip calling it on some changes then the pointer comparisons at the end can
7501 * be invalid if you get unlucky. For example, if a flow removal causes a
7502 * cls_table to be destroyed and then a flow insertion causes a cls_table with
7503 * different wildcards to be created with the same address, then this function
7504 * will incorrectly skip revalidation. */
7506 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
7508 struct table_dpif *table = &ofproto->tables[table_id];
7509 const struct oftable *oftable = &ofproto->up.tables[table_id];
7510 struct cls_table *catchall, *other;
7511 struct cls_table *t;
7513 catchall = other = NULL;
7515 switch (hmap_count(&oftable->cls.tables)) {
7517 /* We could tag this OpenFlow table but it would make the logic a
7518 * little harder and it's a corner case that doesn't seem worth it
7524 HMAP_FOR_EACH (t, hmap_node, &oftable->cls.tables) {
7525 if (cls_table_is_catchall(t)) {
7527 } else if (!other) {
7530 /* Indicate that we can't tag this by setting both tables to
7531 * NULL. (We know that 'catchall' is already NULL.) */
7538 /* Can't tag this table. */
7542 if (table->catchall_table != catchall || table->other_table != other) {
7543 table->catchall_table = catchall;
7544 table->other_table = other;
7545 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
7549 /* Given 'rule' that has changed in some way (either it is a rule being
7550 * inserted, a rule being deleted, or a rule whose actions are being
7551 * modified), marks facets for revalidation to ensure that packets will be
7552 * forwarded correctly according to the new state of the flow table.
7554 * This function must be called after *each* change to a flow table. See
7555 * the comment on table_update_taggable() for more information. */
7557 rule_invalidate(const struct rule_dpif *rule)
7559 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
7561 table_update_taggable(ofproto, rule->up.table_id);
7563 if (!ofproto->backer->need_revalidate) {
7564 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
7566 if (table->other_table && rule->tag) {
7567 tag_set_add(&ofproto->backer->revalidate_set, rule->tag);
7569 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
7575 set_frag_handling(struct ofproto *ofproto_,
7576 enum ofp_config_flags frag_handling)
7578 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7579 if (frag_handling != OFPC_FRAG_REASM) {
7580 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7588 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
7589 const struct flow *flow,
7590 const struct ofpact *ofpacts, size_t ofpacts_len)
7592 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7593 struct initial_vals initial_vals;
7594 struct odputil_keybuf keybuf;
7595 struct dpif_flow_stats stats;
7599 struct action_xlate_ctx ctx;
7600 uint64_t odp_actions_stub[1024 / 8];
7601 struct ofpbuf odp_actions;
7603 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
7604 odp_flow_key_from_flow(&key, flow,
7605 ofp_port_to_odp_port(ofproto, flow->in_port));
7607 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
7609 initial_vals.vlan_tci = flow->vlan_tci;
7610 initial_vals.tunnel_ip_tos = 0;
7611 action_xlate_ctx_init(&ctx, ofproto, flow, &initial_vals, NULL,
7612 packet_get_tcp_flags(packet, flow), packet);
7613 ctx.resubmit_stats = &stats;
7615 ofpbuf_use_stub(&odp_actions,
7616 odp_actions_stub, sizeof odp_actions_stub);
7617 xlate_actions(&ctx, ofpacts, ofpacts_len, &odp_actions);
7618 dpif_execute(ofproto->backer->dpif, key.data, key.size,
7619 odp_actions.data, odp_actions.size, packet);
7620 ofpbuf_uninit(&odp_actions);
7628 set_netflow(struct ofproto *ofproto_,
7629 const struct netflow_options *netflow_options)
7631 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7633 if (netflow_options) {
7634 if (!ofproto->netflow) {
7635 ofproto->netflow = netflow_create();
7637 return netflow_set_options(ofproto->netflow, netflow_options);
7639 netflow_destroy(ofproto->netflow);
7640 ofproto->netflow = NULL;
7646 get_netflow_ids(const struct ofproto *ofproto_,
7647 uint8_t *engine_type, uint8_t *engine_id)
7649 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7651 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
7655 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
7657 if (!facet_is_controller_flow(facet) &&
7658 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
7659 struct subfacet *subfacet;
7660 struct ofexpired expired;
7662 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
7663 if (subfacet->path == SF_FAST_PATH) {
7664 struct dpif_flow_stats stats;
7666 subfacet_reinstall(subfacet, &stats);
7667 subfacet_update_stats(subfacet, &stats);
7671 expired.flow = facet->flow;
7672 expired.packet_count = facet->packet_count;
7673 expired.byte_count = facet->byte_count;
7674 expired.used = facet->used;
7675 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
7680 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
7682 struct facet *facet;
7684 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
7685 send_active_timeout(ofproto, facet);
7689 static struct ofproto_dpif *
7690 ofproto_dpif_lookup(const char *name)
7692 struct ofproto_dpif *ofproto;
7694 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
7695 hash_string(name, 0), &all_ofproto_dpifs) {
7696 if (!strcmp(ofproto->up.name, name)) {
7704 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
7705 const char *argv[], void *aux OVS_UNUSED)
7707 struct ofproto_dpif *ofproto;
7710 ofproto = ofproto_dpif_lookup(argv[1]);
7712 unixctl_command_reply_error(conn, "no such bridge");
7715 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
7717 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
7718 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
7722 unixctl_command_reply(conn, "table successfully flushed");
7726 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
7727 const char *argv[], void *aux OVS_UNUSED)
7729 struct ds ds = DS_EMPTY_INITIALIZER;
7730 const struct ofproto_dpif *ofproto;
7731 const struct mac_entry *e;
7733 ofproto = ofproto_dpif_lookup(argv[1]);
7735 unixctl_command_reply_error(conn, "no such bridge");
7739 ds_put_cstr(&ds, " port VLAN MAC Age\n");
7740 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
7741 struct ofbundle *bundle = e->port.p;
7742 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
7743 ofbundle_get_a_port(bundle)->odp_port,
7744 e->vlan, ETH_ADDR_ARGS(e->mac),
7745 mac_entry_age(ofproto->ml, e));
7747 unixctl_command_reply(conn, ds_cstr(&ds));
7752 struct action_xlate_ctx ctx;
7758 trace_format_rule(struct ds *result, uint8_t table_id, int level,
7759 const struct rule_dpif *rule)
7761 ds_put_char_multiple(result, '\t', level);
7763 ds_put_cstr(result, "No match\n");
7767 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
7768 table_id, ntohll(rule->up.flow_cookie));
7769 cls_rule_format(&rule->up.cr, result);
7770 ds_put_char(result, '\n');
7772 ds_put_char_multiple(result, '\t', level);
7773 ds_put_cstr(result, "OpenFlow ");
7774 ofpacts_format(rule->up.ofpacts, rule->up.ofpacts_len, result);
7775 ds_put_char(result, '\n');
7779 trace_format_flow(struct ds *result, int level, const char *title,
7780 struct trace_ctx *trace)
7782 ds_put_char_multiple(result, '\t', level);
7783 ds_put_format(result, "%s: ", title);
7784 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
7785 ds_put_cstr(result, "unchanged");
7787 flow_format(result, &trace->ctx.flow);
7788 trace->flow = trace->ctx.flow;
7790 ds_put_char(result, '\n');
7794 trace_format_regs(struct ds *result, int level, const char *title,
7795 struct trace_ctx *trace)
7799 ds_put_char_multiple(result, '\t', level);
7800 ds_put_format(result, "%s:", title);
7801 for (i = 0; i < FLOW_N_REGS; i++) {
7802 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
7804 ds_put_char(result, '\n');
7808 trace_format_odp(struct ds *result, int level, const char *title,
7809 struct trace_ctx *trace)
7811 struct ofpbuf *odp_actions = trace->ctx.odp_actions;
7813 ds_put_char_multiple(result, '\t', level);
7814 ds_put_format(result, "%s: ", title);
7815 format_odp_actions(result, odp_actions->data, odp_actions->size);
7816 ds_put_char(result, '\n');
7820 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
7822 struct trace_ctx *trace = CONTAINER_OF(ctx, struct trace_ctx, ctx);
7823 struct ds *result = trace->result;
7825 ds_put_char(result, '\n');
7826 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
7827 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
7828 trace_format_odp(result, ctx->recurse + 1, "Resubmitted odp", trace);
7829 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
7833 trace_report(struct action_xlate_ctx *ctx, const char *s)
7835 struct trace_ctx *trace = CONTAINER_OF(ctx, struct trace_ctx, ctx);
7836 struct ds *result = trace->result;
7838 ds_put_char_multiple(result, '\t', ctx->recurse);
7839 ds_put_cstr(result, s);
7840 ds_put_char(result, '\n');
7844 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
7845 void *aux OVS_UNUSED)
7847 const char *dpname = argv[1];
7848 struct ofproto_dpif *ofproto;
7849 struct ofpbuf odp_key;
7850 struct ofpbuf *packet;
7851 struct initial_vals initial_vals;
7857 ofpbuf_init(&odp_key, 0);
7860 ofproto = ofproto_dpif_lookup(dpname);
7862 unixctl_command_reply_error(conn, "Unknown ofproto (use ofproto/list "
7866 if (argc == 3 || (argc == 4 && !strcmp(argv[3], "-generate"))) {
7867 /* ofproto/trace dpname flow [-generate] */
7868 const char *flow_s = argv[2];
7869 const char *generate_s = argv[3];
7871 /* Allow 'flow_s' to be either a datapath flow or an OpenFlow-like
7872 * flow. We guess which type it is based on whether 'flow_s' contains
7873 * an '(', since a datapath flow always contains '(') but an
7874 * OpenFlow-like flow should not (in fact it's allowed but I believe
7875 * that's not documented anywhere).
7877 * An alternative would be to try to parse 'flow_s' both ways, but then
7878 * it would be tricky giving a sensible error message. After all, do
7879 * you just say "syntax error" or do you present both error messages?
7880 * Both choices seem lousy. */
7881 if (strchr(flow_s, '(')) {
7884 /* Convert string to datapath key. */
7885 ofpbuf_init(&odp_key, 0);
7886 error = odp_flow_key_from_string(flow_s, NULL, &odp_key);
7888 unixctl_command_reply_error(conn, "Bad flow syntax");
7892 /* The user might have specified the wrong ofproto but within the
7893 * same backer. That's OK, ofproto_receive() can find the right
7895 if (ofproto_receive(ofproto->backer, NULL, odp_key.data,
7896 odp_key.size, &flow, NULL, &ofproto, NULL,
7898 unixctl_command_reply_error(conn, "Invalid flow");
7901 ds_put_format(&result, "Bridge: %s\n", ofproto->up.name);
7905 error_s = parse_ofp_exact_flow(&flow, argv[2]);
7907 unixctl_command_reply_error(conn, error_s);
7912 initial_vals.vlan_tci = flow.vlan_tci;
7913 initial_vals.tunnel_ip_tos = flow.tunnel.ip_tos;
7916 /* Generate a packet, if requested. */
7918 packet = ofpbuf_new(0);
7919 flow_compose(packet, &flow);
7921 } else if (argc == 7) {
7922 /* ofproto/trace dpname priority tun_id in_port mark packet */
7923 const char *priority_s = argv[2];
7924 const char *tun_id_s = argv[3];
7925 const char *in_port_s = argv[4];
7926 const char *mark_s = argv[5];
7927 const char *packet_s = argv[6];
7928 uint32_t in_port = atoi(in_port_s);
7929 ovs_be64 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
7930 uint32_t priority = atoi(priority_s);
7931 uint32_t mark = atoi(mark_s);
7934 msg = eth_from_hex(packet_s, &packet);
7936 unixctl_command_reply_error(conn, msg);
7940 ds_put_cstr(&result, "Packet: ");
7941 s = ofp_packet_to_string(packet->data, packet->size);
7942 ds_put_cstr(&result, s);
7945 flow_extract(packet, priority, mark, NULL, in_port, &flow);
7946 flow.tunnel.tun_id = tun_id;
7947 initial_vals.vlan_tci = flow.vlan_tci;
7948 initial_vals.tunnel_ip_tos = flow.tunnel.ip_tos;
7950 unixctl_command_reply_error(conn, "Bad command syntax");
7954 ofproto_trace(ofproto, &flow, packet, &initial_vals, &result);
7955 unixctl_command_reply(conn, ds_cstr(&result));
7958 ds_destroy(&result);
7959 ofpbuf_delete(packet);
7960 ofpbuf_uninit(&odp_key);
7964 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
7965 const struct ofpbuf *packet,
7966 const struct initial_vals *initial_vals, struct ds *ds)
7968 struct rule_dpif *rule;
7970 ds_put_cstr(ds, "Flow: ");
7971 flow_format(ds, flow);
7972 ds_put_char(ds, '\n');
7974 rule = rule_dpif_lookup(ofproto, flow);
7976 trace_format_rule(ds, 0, 0, rule);
7977 if (rule == ofproto->miss_rule) {
7978 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
7979 } else if (rule == ofproto->no_packet_in_rule) {
7980 ds_put_cstr(ds, "\nNo match, packets dropped because "
7981 "OFPPC_NO_PACKET_IN is set on in_port.\n");
7985 uint64_t odp_actions_stub[1024 / 8];
7986 struct ofpbuf odp_actions;
7988 struct trace_ctx trace;
7991 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
7994 ofpbuf_use_stub(&odp_actions,
7995 odp_actions_stub, sizeof odp_actions_stub);
7996 action_xlate_ctx_init(&trace.ctx, ofproto, flow, initial_vals,
7997 rule, tcp_flags, packet);
7998 trace.ctx.resubmit_hook = trace_resubmit;
7999 trace.ctx.report_hook = trace_report;
8000 xlate_actions(&trace.ctx, rule->up.ofpacts, rule->up.ofpacts_len,
8003 ds_put_char(ds, '\n');
8004 trace_format_flow(ds, 0, "Final flow", &trace);
8005 ds_put_cstr(ds, "Datapath actions: ");
8006 format_odp_actions(ds, odp_actions.data, odp_actions.size);
8007 ofpbuf_uninit(&odp_actions);
8009 if (trace.ctx.slow) {
8010 enum slow_path_reason slow;
8012 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
8013 "slow path because it:");
8014 for (slow = trace.ctx.slow; slow; ) {
8015 enum slow_path_reason bit = rightmost_1bit(slow);
8019 ds_put_cstr(ds, "\n\t- Consists of CFM packets.");
8022 ds_put_cstr(ds, "\n\t- Consists of LACP packets.");
8025 ds_put_cstr(ds, "\n\t- Consists of STP packets.");
8028 ds_put_cstr(ds, "\n\t- Needs in-band special case "
8031 ds_put_cstr(ds, "\n\t (The datapath actions are "
8032 "incomplete--for complete actions, "
8033 "please supply a packet.)");
8036 case SLOW_CONTROLLER:
8037 ds_put_cstr(ds, "\n\t- Sends \"packet-in\" messages "
8038 "to the OpenFlow controller.");
8041 ds_put_cstr(ds, "\n\t- Needs more specific matching "
8042 "than the datapath supports.");
8049 if (slow & ~SLOW_MATCH) {
8050 ds_put_cstr(ds, "\nThe datapath actions above do not reflect "
8051 "the special slow-path processing.");
8058 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
8059 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
8062 unixctl_command_reply(conn, NULL);
8066 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
8067 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
8070 unixctl_command_reply(conn, NULL);
8073 /* Runs a self-check of flow translations in 'ofproto'. Appends a message to
8074 * 'reply' describing the results. */
8076 ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
8078 struct facet *facet;
8082 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
8083 if (!facet_check_consistency(facet)) {
8088 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
8092 ds_put_format(reply, "%s: self-check failed (%d errors)\n",
8093 ofproto->up.name, errors);
8095 ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
8100 ofproto_dpif_self_check(struct unixctl_conn *conn,
8101 int argc, const char *argv[], void *aux OVS_UNUSED)
8103 struct ds reply = DS_EMPTY_INITIALIZER;
8104 struct ofproto_dpif *ofproto;
8107 ofproto = ofproto_dpif_lookup(argv[1]);
8109 unixctl_command_reply_error(conn, "Unknown ofproto (use "
8110 "ofproto/list for help)");
8113 ofproto_dpif_self_check__(ofproto, &reply);
8115 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
8116 ofproto_dpif_self_check__(ofproto, &reply);
8120 unixctl_command_reply(conn, ds_cstr(&reply));
8124 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
8125 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
8126 * to destroy 'ofproto_shash' and free the returned value. */
8127 static const struct shash_node **
8128 get_ofprotos(struct shash *ofproto_shash)
8130 const struct ofproto_dpif *ofproto;
8132 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
8133 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
8134 shash_add_nocopy(ofproto_shash, name, ofproto);
8137 return shash_sort(ofproto_shash);
8141 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
8142 const char *argv[] OVS_UNUSED,
8143 void *aux OVS_UNUSED)
8145 struct ds ds = DS_EMPTY_INITIALIZER;
8146 struct shash ofproto_shash;
8147 const struct shash_node **sorted_ofprotos;
8150 shash_init(&ofproto_shash);
8151 sorted_ofprotos = get_ofprotos(&ofproto_shash);
8152 for (i = 0; i < shash_count(&ofproto_shash); i++) {
8153 const struct shash_node *node = sorted_ofprotos[i];
8154 ds_put_format(&ds, "%s\n", node->name);
8157 shash_destroy(&ofproto_shash);
8158 free(sorted_ofprotos);
8160 unixctl_command_reply(conn, ds_cstr(&ds));
8165 show_dp_format(const struct ofproto_dpif *ofproto, struct ds *ds)
8167 const struct shash_node **ports;
8169 struct avg_subfacet_rates lifetime;
8170 unsigned long long int minutes;
8171 const int min_ms = 60 * 1000; /* milliseconds in one minute. */
8173 minutes = (time_msec() - ofproto->created) / min_ms;
8176 lifetime.add_rate = (double)ofproto->total_subfacet_add_count
8178 lifetime.del_rate = (double)ofproto->total_subfacet_del_count
8181 lifetime.add_rate = 0.0;
8182 lifetime.del_rate = 0.0;
8185 ds_put_format(ds, "%s (%s):\n", ofproto->up.name,
8186 dpif_name(ofproto->backer->dpif));
8188 "\tlookups: hit:%"PRIu64" missed:%"PRIu64"\n",
8189 ofproto->n_hit, ofproto->n_missed);
8190 ds_put_format(ds, "\tflows: cur: %zu, avg: %5.3f, max: %d,"
8191 " life span: %llu(ms)\n",
8192 hmap_count(&ofproto->subfacets),
8193 avg_subfacet_count(ofproto),
8194 ofproto->max_n_subfacet,
8195 avg_subfacet_life_span(ofproto));
8196 if (minutes >= 60) {
8197 show_dp_rates(ds, "\t\thourly avg:", &ofproto->hourly);
8199 if (minutes >= 60 * 24) {
8200 show_dp_rates(ds, "\t\tdaily avg:", &ofproto->daily);
8202 show_dp_rates(ds, "\t\toverall avg:", &lifetime);
8204 ports = shash_sort(&ofproto->up.port_by_name);
8205 for (i = 0; i < shash_count(&ofproto->up.port_by_name); i++) {
8206 const struct shash_node *node = ports[i];
8207 struct ofport *ofport = node->data;
8208 const char *name = netdev_get_name(ofport->netdev);
8209 const char *type = netdev_get_type(ofport->netdev);
8212 ds_put_format(ds, "\t%s %u/", name, ofport->ofp_port);
8214 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
8215 if (odp_port != OVSP_NONE) {
8216 ds_put_format(ds, "%"PRIu32":", odp_port);
8218 ds_put_cstr(ds, "none:");
8221 if (strcmp(type, "system")) {
8222 struct netdev *netdev;
8225 ds_put_format(ds, " (%s", type);
8227 error = netdev_open(name, type, &netdev);
8232 error = netdev_get_config(netdev, &config);
8234 const struct smap_node **nodes;
8237 nodes = smap_sort(&config);
8238 for (i = 0; i < smap_count(&config); i++) {
8239 const struct smap_node *node = nodes[i];
8240 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
8241 node->key, node->value);
8245 smap_destroy(&config);
8247 netdev_close(netdev);
8249 ds_put_char(ds, ')');
8251 ds_put_char(ds, '\n');
8257 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc,
8258 const char *argv[], void *aux OVS_UNUSED)
8260 struct ds ds = DS_EMPTY_INITIALIZER;
8261 const struct ofproto_dpif *ofproto;
8265 for (i = 1; i < argc; i++) {
8266 ofproto = ofproto_dpif_lookup(argv[i]);
8268 ds_put_format(&ds, "Unknown bridge %s (use dpif/dump-dps "
8269 "for help)", argv[i]);
8270 unixctl_command_reply_error(conn, ds_cstr(&ds));
8273 show_dp_format(ofproto, &ds);
8276 struct shash ofproto_shash;
8277 const struct shash_node **sorted_ofprotos;
8280 shash_init(&ofproto_shash);
8281 sorted_ofprotos = get_ofprotos(&ofproto_shash);
8282 for (i = 0; i < shash_count(&ofproto_shash); i++) {
8283 const struct shash_node *node = sorted_ofprotos[i];
8284 show_dp_format(node->data, &ds);
8287 shash_destroy(&ofproto_shash);
8288 free(sorted_ofprotos);
8291 unixctl_command_reply(conn, ds_cstr(&ds));
8296 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
8297 int argc OVS_UNUSED, const char *argv[],
8298 void *aux OVS_UNUSED)
8300 struct ds ds = DS_EMPTY_INITIALIZER;
8301 const struct ofproto_dpif *ofproto;
8302 struct subfacet *subfacet;
8304 ofproto = ofproto_dpif_lookup(argv[1]);
8306 unixctl_command_reply_error(conn, "no such bridge");
8310 update_stats(ofproto->backer);
8312 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
8313 odp_flow_key_format(subfacet->key, subfacet->key_len, &ds);
8315 ds_put_format(&ds, ", packets:%"PRIu64", bytes:%"PRIu64", used:",
8316 subfacet->dp_packet_count, subfacet->dp_byte_count);
8317 if (subfacet->used) {
8318 ds_put_format(&ds, "%.3fs",
8319 (time_msec() - subfacet->used) / 1000.0);
8321 ds_put_format(&ds, "never");
8323 if (subfacet->facet->tcp_flags) {
8324 ds_put_cstr(&ds, ", flags:");
8325 packet_format_tcp_flags(&ds, subfacet->facet->tcp_flags);
8328 ds_put_cstr(&ds, ", actions:");
8329 if (subfacet->slow) {
8330 uint64_t slow_path_stub[128 / 8];
8331 const struct nlattr *actions;
8334 compose_slow_path(ofproto, &subfacet->facet->flow, subfacet->slow,
8335 slow_path_stub, sizeof slow_path_stub,
8336 &actions, &actions_len);
8337 format_odp_actions(&ds, actions, actions_len);
8339 format_odp_actions(&ds, subfacet->actions, subfacet->actions_len);
8341 ds_put_char(&ds, '\n');
8344 unixctl_command_reply(conn, ds_cstr(&ds));
8349 ofproto_unixctl_dpif_del_flows(struct unixctl_conn *conn,
8350 int argc OVS_UNUSED, const char *argv[],
8351 void *aux OVS_UNUSED)
8353 struct ds ds = DS_EMPTY_INITIALIZER;
8354 struct ofproto_dpif *ofproto;
8356 ofproto = ofproto_dpif_lookup(argv[1]);
8358 unixctl_command_reply_error(conn, "no such bridge");
8362 flush(&ofproto->up);
8364 unixctl_command_reply(conn, ds_cstr(&ds));
8369 ofproto_dpif_unixctl_init(void)
8371 static bool registered;
8377 unixctl_command_register(
8379 "bridge {priority tun_id in_port mark packet | odp_flow [-generate]}",
8380 2, 6, ofproto_unixctl_trace, NULL);
8381 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
8382 ofproto_unixctl_fdb_flush, NULL);
8383 unixctl_command_register("fdb/show", "bridge", 1, 1,
8384 ofproto_unixctl_fdb_show, NULL);
8385 unixctl_command_register("ofproto/clog", "", 0, 0,
8386 ofproto_dpif_clog, NULL);
8387 unixctl_command_register("ofproto/unclog", "", 0, 0,
8388 ofproto_dpif_unclog, NULL);
8389 unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
8390 ofproto_dpif_self_check, NULL);
8391 unixctl_command_register("dpif/dump-dps", "", 0, 0,
8392 ofproto_unixctl_dpif_dump_dps, NULL);
8393 unixctl_command_register("dpif/show", "[bridge]", 0, INT_MAX,
8394 ofproto_unixctl_dpif_show, NULL);
8395 unixctl_command_register("dpif/dump-flows", "bridge", 1, 1,
8396 ofproto_unixctl_dpif_dump_flows, NULL);
8397 unixctl_command_register("dpif/del-flows", "bridge", 1, 1,
8398 ofproto_unixctl_dpif_del_flows, NULL);
8401 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
8403 * This is deprecated. It is only for compatibility with broken device drivers
8404 * in old versions of Linux that do not properly support VLANs when VLAN
8405 * devices are not used. When broken device drivers are no longer in
8406 * widespread use, we will delete these interfaces. */
8409 set_realdev(struct ofport *ofport_, uint16_t realdev_ofp_port, int vid)
8411 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
8412 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
8414 if (realdev_ofp_port == ofport->realdev_ofp_port
8415 && vid == ofport->vlandev_vid) {
8419 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8421 if (ofport->realdev_ofp_port) {
8424 if (realdev_ofp_port && ofport->bundle) {
8425 /* vlandevs are enslaved to their realdevs, so they are not allowed to
8426 * themselves be part of a bundle. */
8427 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
8430 ofport->realdev_ofp_port = realdev_ofp_port;
8431 ofport->vlandev_vid = vid;
8433 if (realdev_ofp_port) {
8434 vsp_add(ofport, realdev_ofp_port, vid);
8441 hash_realdev_vid(uint16_t realdev_ofp_port, int vid)
8443 return hash_2words(realdev_ofp_port, vid);
8446 /* Returns the ODP port number of the Linux VLAN device that corresponds to
8447 * 'vlan_tci' on the network device with port number 'realdev_odp_port' in
8448 * 'ofproto'. For example, given 'realdev_odp_port' of eth0 and 'vlan_tci' 9,
8449 * it would return the port number of eth0.9.
8451 * Unless VLAN splinters are enabled for port 'realdev_odp_port', this
8452 * function just returns its 'realdev_odp_port' argument. */
8454 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
8455 uint32_t realdev_odp_port, ovs_be16 vlan_tci)
8457 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
8458 uint16_t realdev_ofp_port;
8459 int vid = vlan_tci_to_vid(vlan_tci);
8460 const struct vlan_splinter *vsp;
8462 realdev_ofp_port = odp_port_to_ofp_port(ofproto, realdev_odp_port);
8463 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
8464 hash_realdev_vid(realdev_ofp_port, vid),
8465 &ofproto->realdev_vid_map) {
8466 if (vsp->realdev_ofp_port == realdev_ofp_port
8467 && vsp->vid == vid) {
8468 return ofp_port_to_odp_port(ofproto, vsp->vlandev_ofp_port);
8472 return realdev_odp_port;
8475 static struct vlan_splinter *
8476 vlandev_find(const struct ofproto_dpif *ofproto, uint16_t vlandev_ofp_port)
8478 struct vlan_splinter *vsp;
8480 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node, hash_int(vlandev_ofp_port, 0),
8481 &ofproto->vlandev_map) {
8482 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
8490 /* Returns the OpenFlow port number of the "real" device underlying the Linux
8491 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
8492 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
8493 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
8494 * eth0 and store 9 in '*vid'.
8496 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
8497 * VLAN device. Unless VLAN splinters are enabled, this is what this function
8500 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
8501 uint16_t vlandev_ofp_port, int *vid)
8503 if (!hmap_is_empty(&ofproto->vlandev_map)) {
8504 const struct vlan_splinter *vsp;
8506 vsp = vlandev_find(ofproto, vlandev_ofp_port);
8511 return vsp->realdev_ofp_port;
8517 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
8518 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
8519 * 'flow->in_port' to the "real" device backing the VLAN device, sets
8520 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
8521 * always the case unless VLAN splinters are enabled), returns false without
8522 * making any changes. */
8524 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
8529 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
8534 /* Cause the flow to be processed as if it came in on the real device with
8535 * the VLAN device's VLAN ID. */
8536 flow->in_port = realdev;
8537 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
8542 vsp_remove(struct ofport_dpif *port)
8544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
8545 struct vlan_splinter *vsp;
8547 vsp = vlandev_find(ofproto, port->up.ofp_port);
8549 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
8550 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
8553 port->realdev_ofp_port = 0;
8555 VLOG_ERR("missing vlan device record");
8560 vsp_add(struct ofport_dpif *port, uint16_t realdev_ofp_port, int vid)
8562 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
8564 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
8565 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
8566 == realdev_ofp_port)) {
8567 struct vlan_splinter *vsp;
8569 vsp = xmalloc(sizeof *vsp);
8570 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
8571 hash_int(port->up.ofp_port, 0));
8572 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
8573 hash_realdev_vid(realdev_ofp_port, vid));
8574 vsp->realdev_ofp_port = realdev_ofp_port;
8575 vsp->vlandev_ofp_port = port->up.ofp_port;
8578 port->realdev_ofp_port = realdev_ofp_port;
8580 VLOG_ERR("duplicate vlan device record");
8585 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, uint16_t ofp_port)
8587 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
8588 return ofport ? ofport->odp_port : OVSP_NONE;
8591 static struct ofport_dpif *
8592 odp_port_to_ofport(const struct dpif_backer *backer, uint32_t odp_port)
8594 struct ofport_dpif *port;
8596 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node,
8597 hash_int(odp_port, 0),
8598 &backer->odp_to_ofport_map) {
8599 if (port->odp_port == odp_port) {
8608 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, uint32_t odp_port)
8610 struct ofport_dpif *port;
8612 port = odp_port_to_ofport(ofproto->backer, odp_port);
8613 if (port && &ofproto->up == port->up.ofproto) {
8614 return port->up.ofp_port;
8619 static unsigned long long int
8620 avg_subfacet_life_span(const struct ofproto_dpif *ofproto)
8622 unsigned long long int dc;
8623 unsigned long long int avg;
8625 dc = ofproto->total_subfacet_del_count + ofproto->subfacet_del_count;
8626 avg = dc ? ofproto->total_subfacet_life_span / dc : 0;
8632 avg_subfacet_count(const struct ofproto_dpif *ofproto)
8636 if (ofproto->n_update_stats) {
8637 avg_c = (double)ofproto->total_subfacet_count
8638 / ofproto->n_update_stats;
8645 show_dp_rates(struct ds *ds, const char *heading,
8646 const struct avg_subfacet_rates *rates)
8648 ds_put_format(ds, "%s add rate: %5.3f/min, del rate: %5.3f/min\n",
8649 heading, rates->add_rate, rates->del_rate);
8653 update_max_subfacet_count(struct ofproto_dpif *ofproto)
8655 ofproto->max_n_subfacet = MAX(ofproto->max_n_subfacet,
8656 hmap_count(&ofproto->subfacets));
8659 /* Compute exponentially weighted moving average, adding 'new' as the newest,
8660 * most heavily weighted element. 'base' designates the rate of decay: after
8661 * 'base' further updates, 'new''s weight in the EWMA decays to about 1/e
8664 exp_mavg(double *avg, int base, double new)
8666 *avg = (*avg * (base - 1) + new) / base;
8670 update_moving_averages(struct ofproto_dpif *ofproto)
8672 const int min_ms = 60 * 1000; /* milliseconds in one minute. */
8674 /* Update hourly averages on the minute boundaries. */
8675 if (time_msec() - ofproto->last_minute >= min_ms) {
8676 exp_mavg(&ofproto->hourly.add_rate, 60, ofproto->subfacet_add_count);
8677 exp_mavg(&ofproto->hourly.del_rate, 60, ofproto->subfacet_del_count);
8679 /* Update daily averages on the hour boundaries. */
8680 if ((ofproto->last_minute - ofproto->created) / min_ms % 60 == 59) {
8681 exp_mavg(&ofproto->daily.add_rate, 24, ofproto->hourly.add_rate);
8682 exp_mavg(&ofproto->daily.del_rate, 24, ofproto->hourly.del_rate);
8685 ofproto->total_subfacet_add_count += ofproto->subfacet_add_count;
8686 ofproto->total_subfacet_del_count += ofproto->subfacet_del_count;
8687 ofproto->subfacet_add_count = 0;
8688 ofproto->subfacet_del_count = 0;
8689 ofproto->last_minute += min_ms;
8694 dpif_stats_update_hit_count(struct ofproto_dpif *ofproto, uint64_t delta)
8696 ofproto->n_hit += delta;
8699 const struct ofproto_class ofproto_dpif_class = {
8734 port_is_lacp_current,
8735 NULL, /* rule_choose_table */
8742 rule_modify_actions,
8753 get_stp_port_status,
8760 is_mirror_output_bundle,
8761 forward_bpdu_changed,
8762 set_mac_table_config,