]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif.c
flow: Remove superfluous casts.
[ovs.git] / ofproto / ofproto-dpif.c
CommitLineData
abe529af 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
abe529af
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18
5bee6e26 19#include "ofproto/ofproto-provider.h"
abe529af
BP
20
21#include <errno.h>
22
23#include "autopath.h"
24#include "bond.h"
daff3353 25#include "bundle.h"
abe529af
BP
26#include "byte-order.h"
27#include "connmgr.h"
28#include "coverage.h"
29#include "cfm.h"
30#include "dpif.h"
31#include "dynamic-string.h"
32#include "fail-open.h"
33#include "hmapx.h"
34#include "lacp.h"
75a75043 35#include "learn.h"
abe529af 36#include "mac-learning.h"
816fd533 37#include "meta-flow.h"
abe529af
BP
38#include "multipath.h"
39#include "netdev.h"
40#include "netlink.h"
41#include "nx-match.h"
42#include "odp-util.h"
43#include "ofp-util.h"
44#include "ofpbuf.h"
f25d0cf3 45#include "ofp-actions.h"
31a19d69 46#include "ofp-parse.h"
abe529af 47#include "ofp-print.h"
9d6ac44e 48#include "ofproto-dpif-governor.h"
bae473fe 49#include "ofproto-dpif-sflow.h"
abe529af 50#include "poll-loop.h"
0d085684 51#include "simap.h"
abe529af 52#include "timer.h"
6c1491fb 53#include "unaligned.h"
abe529af
BP
54#include "unixctl.h"
55#include "vlan-bitmap.h"
56#include "vlog.h"
57
58VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
59
abe529af 60COVERAGE_DEFINE(ofproto_dpif_expired);
abe529af
BP
61COVERAGE_DEFINE(ofproto_dpif_xlate);
62COVERAGE_DEFINE(facet_changed_rule);
abe529af
BP
63COVERAGE_DEFINE(facet_revalidate);
64COVERAGE_DEFINE(facet_unexpected);
9d6ac44e 65COVERAGE_DEFINE(facet_suppress);
abe529af 66
29901626 67/* Maximum depth of flow table recursion (due to resubmit actions) in a
abe529af 68 * flow translation. */
642a5c05 69#define MAX_RESUBMIT_RECURSION 32
abe529af 70
9cdaaebe
BP
71/* Number of implemented OpenFlow tables. */
72enum { N_TABLES = 255 };
c57b2226
BP
73enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
74BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
9cdaaebe 75
abe529af
BP
76struct ofport_dpif;
77struct ofproto_dpif;
78
79struct rule_dpif {
80 struct rule up;
81
abe529af
BP
82 /* These statistics:
83 *
84 * - Do include packets and bytes from facets that have been deleted or
85 * whose own statistics have been folded into the rule.
86 *
87 * - Do include packets and bytes sent "by hand" that were accounted to
88 * the rule without any facet being involved (this is a rare corner
89 * case in rule_execute()).
90 *
91 * - Do not include packet or bytes that can be obtained from any facet's
92 * packet_count or byte_count member or that can be obtained from the
b0f7b9b5 93 * datapath by, e.g., dpif_flow_get() for any subfacet.
abe529af
BP
94 */
95 uint64_t packet_count; /* Number of packets received. */
96 uint64_t byte_count; /* Number of bytes received. */
97
54a9cbc9
BP
98 tag_type tag; /* Caches rule_calculate_tag() result. */
99
abe529af
BP
100 struct list facets; /* List of "struct facet"s. */
101};
102
103static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
104{
105 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
106}
107
29901626 108static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
c57b2226
BP
109 const struct flow *);
110static struct rule_dpif *rule_dpif_lookup__(struct ofproto_dpif *,
111 const struct flow *,
112 uint8_t table);
abe529af 113
112bc5f4
BP
114static void rule_credit_stats(struct rule_dpif *,
115 const struct dpif_flow_stats *);
18b2a258 116static void flow_push_stats(struct rule_dpif *, const struct flow *,
112bc5f4 117 const struct dpif_flow_stats *);
822d9414 118static tag_type rule_calculate_tag(const struct flow *,
b0f7b9b5
BP
119 const struct flow_wildcards *,
120 uint32_t basis);
121static void rule_invalidate(const struct rule_dpif *);
122
abe529af
BP
123#define MAX_MIRRORS 32
124typedef uint32_t mirror_mask_t;
125#define MIRROR_MASK_C(X) UINT32_C(X)
126BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
127struct ofmirror {
128 struct ofproto_dpif *ofproto; /* Owning ofproto. */
129 size_t idx; /* In ofproto's "mirrors" array. */
130 void *aux; /* Key supplied by ofproto's client. */
131 char *name; /* Identifier for log messages. */
132
133 /* Selection criteria. */
134 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
135 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
136 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
137
9ba15e2a 138 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
abe529af
BP
139 struct ofbundle *out; /* Output port or NULL. */
140 int out_vlan; /* Output VLAN or -1. */
9ba15e2a 141 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
9d24de3b
JP
142
143 /* Counters. */
144 int64_t packet_count; /* Number of packets sent. */
145 int64_t byte_count; /* Number of bytes sent. */
abe529af
BP
146};
147
148static void mirror_destroy(struct ofmirror *);
9d24de3b
JP
149static void update_mirror_stats(struct ofproto_dpif *ofproto,
150 mirror_mask_t mirrors,
151 uint64_t packets, uint64_t bytes);
abe529af 152
abe529af 153struct ofbundle {
abe529af 154 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
6e492d81 155 struct ofproto_dpif *ofproto; /* Owning ofproto. */
abe529af
BP
156 void *aux; /* Key supplied by ofproto's client. */
157 char *name; /* Identifier for log messages. */
158
159 /* Configuration. */
160 struct list ports; /* Contains "struct ofport"s. */
ecac4ebf 161 enum port_vlan_mode vlan_mode; /* VLAN mode */
abe529af
BP
162 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
163 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
164 * NULL if all VLANs are trunked. */
165 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
166 struct bond *bond; /* Nonnull iff more than one port. */
5e9ceccd 167 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
abe529af
BP
168
169 /* Status. */
9e1fd49b 170 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
abe529af
BP
171
172 /* Port mirroring info. */
173 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
174 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
175 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
176};
177
178static void bundle_remove(struct ofport *);
7bde8dd8 179static void bundle_update(struct ofbundle *);
abe529af
BP
180static void bundle_destroy(struct ofbundle *);
181static void bundle_del_port(struct ofport_dpif *);
182static void bundle_run(struct ofbundle *);
183static void bundle_wait(struct ofbundle *);
4acbc98d 184static struct ofbundle *lookup_input_bundle(const struct ofproto_dpif *,
70c2fd56
BP
185 uint16_t in_port, bool warn,
186 struct ofport_dpif **in_ofportp);
abe529af 187
33158a18
JP
188/* A controller may use OFPP_NONE as the ingress port to indicate that
189 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
190 * when an input bundle is needed for validation (e.g., mirroring or
191 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
192 * any 'port' structs, so care must be taken when dealing with it. */
193static struct ofbundle ofpp_none_bundle = {
194 .name = "OFPP_NONE",
195 .vlan_mode = PORT_VLAN_TRUNK
196};
197
21f7563c
JP
198static void stp_run(struct ofproto_dpif *ofproto);
199static void stp_wait(struct ofproto_dpif *ofproto);
851bf71d
EJ
200static int set_stp_port(struct ofport *,
201 const struct ofproto_port_stp_settings *);
21f7563c 202
5da5ec37
BP
203static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
204
abe529af
BP
205struct action_xlate_ctx {
206/* action_xlate_ctx_init() initializes these members. */
207
208 /* The ofproto. */
209 struct ofproto_dpif *ofproto;
210
211 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
212 * this flow when actions change header fields. */
213 struct flow flow;
214
215 /* The packet corresponding to 'flow', or a null pointer if we are
216 * revalidating without a packet to refer to. */
217 const struct ofpbuf *packet;
218
3de9590b
BP
219 /* Should OFPP_NORMAL update the MAC learning table? Should "learn"
220 * actions update the flow table?
221 *
222 * We want to update these tables if we are actually processing a packet,
223 * or if we are accounting for packets that the datapath has processed, but
224 * not if we are just revalidating. */
225 bool may_learn;
75a75043 226
18b2a258
BP
227 /* The rule that we are currently translating, or NULL. */
228 struct rule_dpif *rule;
54834960 229
0e553d9c
BP
230 /* Union of the set of TCP flags seen so far in this flow. (Used only by
231 * NXAST_FIN_TIMEOUT. Set to zero to avoid updating updating rules'
232 * timeouts.) */
233 uint8_t tcp_flags;
234
112bc5f4
BP
235 /* If nonnull, flow translation calls this function just before executing a
236 * resubmit or OFPP_TABLE action. In addition, disables logging of traces
237 * when the recursion depth is exceeded.
238 *
239 * 'rule' is the rule being submitted into. It will be null if the
240 * resubmit or OFPP_TABLE action didn't find a matching rule.
241 *
242 * This is normally null so the client has to set it manually after
243 * calling action_xlate_ctx_init(). */
244 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *rule);
245
479df176
BP
246 /* If nonnull, flow translation calls this function to report some
247 * significant decision, e.g. to explain why OFPP_NORMAL translation
248 * dropped a packet. */
249 void (*report_hook)(struct action_xlate_ctx *, const char *s);
250
112bc5f4
BP
251 /* If nonnull, flow translation credits the specified statistics to each
252 * rule reached through a resubmit or OFPP_TABLE action.
abe529af
BP
253 *
254 * This is normally null so the client has to set it manually after
255 * calling action_xlate_ctx_init(). */
112bc5f4 256 const struct dpif_flow_stats *resubmit_stats;
abe529af 257
abe529af
BP
258/* xlate_actions() initializes and uses these members. The client might want
259 * to look at them after it returns. */
260
261 struct ofpbuf *odp_actions; /* Datapath actions. */
75a75043 262 tag_type tags; /* Tags associated with actions. */
6a7e895f 263 enum slow_path_reason slow; /* 0 if fast path may be used. */
75a75043
BP
264 bool has_learn; /* Actions include NXAST_LEARN? */
265 bool has_normal; /* Actions output to OFPP_NORMAL? */
0e553d9c 266 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
abe529af 267 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
9d24de3b 268 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
abe529af
BP
269
270/* xlate_actions() initializes and uses these members, but the client has no
271 * reason to look at them. */
272
273 int recurse; /* Recursion level, via xlate_table_action. */
6a6455e5 274 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
b3e9b2ed 275 struct flow base_flow; /* Flow at the last commit. */
deedf7e7 276 uint32_t orig_skb_priority; /* Priority when packet arrived. */
29901626 277 uint8_t table_id; /* OpenFlow table ID where flow was found. */
6ff686f2
PS
278 uint32_t sflow_n_outputs; /* Number of output ports. */
279 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
280 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
848e8809 281 bool exit; /* No further actions should be processed. */
ccb7c863 282 struct flow orig_flow; /* Copy of original flow. */
abe529af
BP
283};
284
285static void action_xlate_ctx_init(struct action_xlate_ctx *,
286 struct ofproto_dpif *, const struct flow *,
18b2a258 287 ovs_be16 initial_tci, struct rule_dpif *,
0e553d9c 288 uint8_t tcp_flags, const struct ofpbuf *);
050ac423 289static void xlate_actions(struct action_xlate_ctx *,
f25d0cf3 290 const struct ofpact *ofpacts, size_t ofpacts_len,
050ac423
BP
291 struct ofpbuf *odp_actions);
292static void xlate_actions_for_side_effects(struct action_xlate_ctx *,
f25d0cf3
BP
293 const struct ofpact *ofpacts,
294 size_t ofpacts_len);
abe529af 295
6a7e895f
BP
296static size_t put_userspace_action(const struct ofproto_dpif *,
297 struct ofpbuf *odp_actions,
298 const struct flow *,
299 const union user_action_cookie *);
300
301static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
302 enum slow_path_reason,
303 uint64_t *stub, size_t stub_size,
304 const struct nlattr **actionsp,
305 size_t *actions_lenp);
306
479df176
BP
307static void xlate_report(struct action_xlate_ctx *ctx, const char *s);
308
6a7e895f
BP
309/* A subfacet (see "struct subfacet" below) has three possible installation
310 * states:
311 *
312 * - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
313 * case just after the subfacet is created, just before the subfacet is
314 * destroyed, or if the datapath returns an error when we try to install a
315 * subfacet.
316 *
317 * - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
318 *
319 * - SF_SLOW_PATH: An action that sends every packet for the subfacet through
320 * ofproto_dpif is installed in the datapath.
321 */
322enum subfacet_path {
323 SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
324 SF_FAST_PATH, /* Full actions are installed. */
325 SF_SLOW_PATH, /* Send-to-userspace action is installed. */
326};
327
328static const char *subfacet_path_to_string(enum subfacet_path);
329
5f5fbd17
BP
330/* A dpif flow and actions associated with a facet.
331 *
332 * See also the large comment on struct facet. */
333struct subfacet {
334 /* Owners. */
335 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
336 struct list list_node; /* In struct facet's 'facets' list. */
337 struct facet *facet; /* Owning facet. */
338
339 /* Key.
340 *
341 * To save memory in the common case, 'key' is NULL if 'key_fitness' is
342 * ODP_FIT_PERFECT, that is, odp_flow_key_from_flow() can accurately
343 * regenerate the ODP flow key from ->facet->flow. */
344 enum odp_key_fitness key_fitness;
345 struct nlattr *key;
346 int key_len;
347
348 long long int used; /* Time last used; time created if not used. */
349
350 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
351 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
352
353 /* Datapath actions.
354 *
355 * These should be essentially identical for every subfacet in a facet, but
356 * may differ in trivial ways due to VLAN splinters. */
357 size_t actions_len; /* Number of bytes in actions[]. */
358 struct nlattr *actions; /* Datapath actions. */
359
6a7e895f
BP
360 enum slow_path_reason slow; /* 0 if fast path may be used. */
361 enum subfacet_path path; /* Installed in datapath? */
5f5fbd17
BP
362
363 /* This value is normally the same as ->facet->flow.vlan_tci. Only VLAN
364 * splinters can cause it to differ. This value should be removed when
365 * the VLAN splinters feature is no longer needed. */
366 ovs_be16 initial_tci; /* Initial VLAN TCI value. */
367};
368
369static struct subfacet *subfacet_create(struct facet *, enum odp_key_fitness,
370 const struct nlattr *key,
371 size_t key_len, ovs_be16 initial_tci);
372static struct subfacet *subfacet_find(struct ofproto_dpif *,
373 const struct nlattr *key, size_t key_len);
374static void subfacet_destroy(struct subfacet *);
375static void subfacet_destroy__(struct subfacet *);
376static void subfacet_get_key(struct subfacet *, struct odputil_keybuf *,
377 struct ofpbuf *key);
378static void subfacet_reset_dp_stats(struct subfacet *,
379 struct dpif_flow_stats *);
380static void subfacet_update_time(struct subfacet *, long long int used);
381static void subfacet_update_stats(struct subfacet *,
382 const struct dpif_flow_stats *);
383static void subfacet_make_actions(struct subfacet *,
5fe20d5d
BP
384 const struct ofpbuf *packet,
385 struct ofpbuf *odp_actions);
5f5fbd17
BP
386static int subfacet_install(struct subfacet *,
387 const struct nlattr *actions, size_t actions_len,
6a7e895f 388 struct dpif_flow_stats *, enum slow_path_reason);
5f5fbd17
BP
389static void subfacet_uninstall(struct subfacet *);
390
6a7e895f
BP
391static enum subfacet_path subfacet_want_path(enum slow_path_reason);
392
b0f7b9b5
BP
393/* An exact-match instantiation of an OpenFlow flow.
394 *
395 * A facet associates a "struct flow", which represents the Open vSwitch
b95fc6ba
BP
396 * userspace idea of an exact-match flow, with one or more subfacets. Each
397 * subfacet tracks the datapath's idea of the exact-match flow equivalent to
398 * the facet. When the kernel module (or other dpif implementation) and Open
399 * vSwitch userspace agree on the definition of a flow key, there is exactly
400 * one subfacet per facet. If the dpif implementation supports more-specific
401 * flow matching than userspace, however, a facet can have more than one
402 * subfacet, each of which corresponds to some distinction in flow that
403 * userspace simply doesn't understand.
b0f7b9b5
BP
404 *
405 * Flow expiration works in terms of subfacets, so a facet must have at least
406 * one subfacet or it will never expire, leaking memory. */
abe529af 407struct facet {
b0f7b9b5
BP
408 /* Owners. */
409 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
410 struct list list_node; /* In owning rule's 'facets' list. */
411 struct rule_dpif *rule; /* Owning rule. */
412
413 /* Owned data. */
414 struct list subfacets;
abe529af
BP
415 long long int used; /* Time last used; time created if not used. */
416
b0f7b9b5
BP
417 /* Key. */
418 struct flow flow;
419
abe529af
BP
420 /* These statistics:
421 *
422 * - Do include packets and bytes sent "by hand", e.g. with
423 * dpif_execute().
424 *
425 * - Do include packets and bytes that were obtained from the datapath
b0f7b9b5 426 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
abe529af 427 * DPIF_FP_ZERO_STATS).
b0f7b9b5
BP
428 *
429 * - Do not include packets or bytes that can be obtained from the
430 * datapath for any existing subfacet.
abe529af
BP
431 */
432 uint64_t packet_count; /* Number of packets received. */
433 uint64_t byte_count; /* Number of bytes received. */
434
b0f7b9b5 435 /* Resubmit statistics. */
9d24de3b
JP
436 uint64_t prev_packet_count; /* Number of packets from last stats push. */
437 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
438 long long int prev_used; /* Used time from last stats push. */
abe529af 439
b0f7b9b5 440 /* Accounting. */
907a4c5e 441 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
b0f7b9b5 442 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
0e553d9c 443 uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
abe529af 444
b95fc6ba
BP
445 /* Properties of datapath actions.
446 *
447 * Every subfacet has its own actions because actions can differ slightly
448 * between splintered and non-splintered subfacets due to the VLAN tag
449 * being initially different (present vs. absent). All of them have these
450 * properties in common so we just store one copy of them here. */
75a75043
BP
451 bool has_learn; /* Actions include NXAST_LEARN? */
452 bool has_normal; /* Actions output to OFPP_NORMAL? */
0e553d9c 453 bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
b0f7b9b5 454 tag_type tags; /* Tags that would require revalidation. */
9d24de3b 455 mirror_mask_t mirrors; /* Bitmap of dependent mirrors. */
26cd7e34
BP
456
457 /* Storage for a single subfacet, to reduce malloc() time and space
458 * overhead. (A facet always has at least one subfacet and in the common
459 * case has exactly one subfacet.) */
460 struct subfacet one_subfacet;
abe529af
BP
461};
462
2b459b83
BP
463static struct facet *facet_create(struct rule_dpif *,
464 const struct flow *, uint32_t hash);
15baa734 465static void facet_remove(struct facet *);
abe529af
BP
466static void facet_free(struct facet *);
467
2b459b83
BP
468static struct facet *facet_find(struct ofproto_dpif *,
469 const struct flow *, uint32_t hash);
abe529af 470static struct facet *facet_lookup_valid(struct ofproto_dpif *,
2b459b83 471 const struct flow *, uint32_t hash);
c57b2226 472static void facet_revalidate(struct facet *);
6814e51f 473static bool facet_check_consistency(struct facet *);
abe529af 474
15baa734 475static void facet_flush_stats(struct facet *);
abe529af 476
15baa734 477static void facet_update_time(struct facet *, long long int used);
bbb5d219 478static void facet_reset_counters(struct facet *);
abe529af 479static void facet_push_stats(struct facet *);
3de9590b
BP
480static void facet_learn(struct facet *);
481static void facet_account(struct facet *);
abe529af
BP
482
483static bool facet_is_controller_flow(struct facet *);
484
abe529af
BP
485struct ofport_dpif {
486 struct ofport up;
487
488 uint32_t odp_port;
489 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
490 struct list bundle_node; /* In struct ofbundle's "ports" list. */
491 struct cfm *cfm; /* Connectivity Fault Management, if any. */
492 tag_type tag; /* Tag associated with this port. */
00794817 493 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
015e08bc 494 bool may_enable; /* May be enabled in bonds. */
3e5b3fdb 495 long long int carrier_seq; /* Carrier status changes. */
21f7563c 496
52a90c29 497 /* Spanning tree. */
21f7563c
JP
498 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
499 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
500 long long int stp_state_entered;
8b36f51e
EJ
501
502 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
52a90c29
BP
503
504 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
505 *
506 * This is deprecated. It is only for compatibility with broken device
507 * drivers in old versions of Linux that do not properly support VLANs when
508 * VLAN devices are not used. When broken device drivers are no longer in
509 * widespread use, we will delete these interfaces. */
510 uint16_t realdev_ofp_port;
511 int vlandev_vid;
8b36f51e
EJ
512};
513
514/* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
515 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
516 * traffic egressing the 'ofport' with that priority should be marked with. */
517struct priority_to_dscp {
518 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
519 uint32_t priority; /* Priority of this queue (see struct flow). */
520
521 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
abe529af
BP
522};
523
52a90c29
BP
524/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
525 *
526 * This is deprecated. It is only for compatibility with broken device drivers
527 * in old versions of Linux that do not properly support VLANs when VLAN
528 * devices are not used. When broken device drivers are no longer in
529 * widespread use, we will delete these interfaces. */
530struct vlan_splinter {
531 struct hmap_node realdev_vid_node;
532 struct hmap_node vlandev_node;
533 uint16_t realdev_ofp_port;
534 uint16_t vlandev_ofp_port;
535 int vid;
536};
537
538static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
539 uint32_t realdev, ovs_be16 vlan_tci);
b98d8985 540static bool vsp_adjust_flow(const struct ofproto_dpif *, struct flow *);
52a90c29
BP
541static void vsp_remove(struct ofport_dpif *);
542static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
543
abe529af
BP
544static struct ofport_dpif *
545ofport_dpif_cast(const struct ofport *ofport)
546{
547 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
548 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
549}
550
551static void port_run(struct ofport_dpif *);
0aa66d6e 552static void port_run_fast(struct ofport_dpif *);
abe529af 553static void port_wait(struct ofport_dpif *);
a5610457 554static int set_cfm(struct ofport *, const struct cfm_settings *);
8b36f51e 555static void ofport_clear_priorities(struct ofport_dpif *);
abe529af 556
7ee20df1
BP
557struct dpif_completion {
558 struct list list_node;
559 struct ofoperation *op;
560};
561
54a9cbc9
BP
562/* Extra information about a classifier table.
563 * Currently used just for optimized flow revalidation. */
564struct table_dpif {
565 /* If either of these is nonnull, then this table has a form that allows
566 * flows to be tagged to avoid revalidating most flows for the most common
567 * kinds of flow table changes. */
568 struct cls_table *catchall_table; /* Table that wildcards all fields. */
569 struct cls_table *other_table; /* Table with any other wildcard set. */
570 uint32_t basis; /* Keeps each table's tags separate. */
571};
572
3c4a309c
BP
573/* Reasons that we might need to revalidate every facet, and corresponding
574 * coverage counters.
575 *
576 * A value of 0 means that there is no need to revalidate.
577 *
578 * It would be nice to have some cleaner way to integrate with coverage
579 * counters, but with only a few reasons I guess this is good enough for
580 * now. */
581enum revalidate_reason {
582 REV_RECONFIGURE = 1, /* Switch configuration changed. */
583 REV_STP, /* Spanning tree protocol port status change. */
584 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
585 REV_FLOW_TABLE, /* Flow table changed. */
586 REV_INCONSISTENCY /* Facet self-check failed. */
587};
588COVERAGE_DEFINE(rev_reconfigure);
589COVERAGE_DEFINE(rev_stp);
590COVERAGE_DEFINE(rev_port_toggled);
591COVERAGE_DEFINE(rev_flow_table);
592COVERAGE_DEFINE(rev_inconsistency);
593
abe529af 594struct ofproto_dpif {
b44a10b7 595 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
abe529af
BP
596 struct ofproto up;
597 struct dpif *dpif;
598 int max_ports;
599
c57b2226
BP
600 /* Special OpenFlow rules. */
601 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
602 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
603
6c1491fb
BP
604 /* Statistics. */
605 uint64_t n_matches;
606
abe529af
BP
607 /* Bridging. */
608 struct netflow *netflow;
bae473fe 609 struct dpif_sflow *sflow;
abe529af
BP
610 struct hmap bundles; /* Contains "struct ofbundle"s. */
611 struct mac_learning *ml;
612 struct ofmirror *mirrors[MAX_MIRRORS];
ccb7c863 613 bool has_mirrors;
abe529af
BP
614 bool has_bonded_bundles;
615
616 /* Expiration. */
617 struct timer next_expiration;
618
619 /* Facets. */
620 struct hmap facets;
b0f7b9b5 621 struct hmap subfacets;
9d6ac44e 622 struct governor *governor;
54a9cbc9
BP
623
624 /* Revalidation. */
625 struct table_dpif tables[N_TABLES];
3c4a309c 626 enum revalidate_reason need_revalidate;
abe529af 627 struct tag_set revalidate_set;
7ee20df1
BP
628
629 /* Support for debugging async flow mods. */
630 struct list completions;
daff3353
EJ
631
632 bool has_bundle_action; /* True when the first bundle action appears. */
6527c598
PS
633 struct netdev_stats stats; /* To account packets generated and consumed in
634 * userspace. */
21f7563c
JP
635
636 /* Spanning tree. */
637 struct stp *stp;
638 long long int stp_last_tick;
52a90c29
BP
639
640 /* VLAN splinters. */
641 struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
642 struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
abe529af
BP
643};
644
7ee20df1
BP
645/* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
646 * for debugging the asynchronous flow_mod implementation.) */
647static bool clogged;
648
b44a10b7
BP
649/* All existing ofproto_dpif instances, indexed by ->up.name. */
650static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
651
abe529af
BP
652static void ofproto_dpif_unixctl_init(void);
653
654static struct ofproto_dpif *
655ofproto_dpif_cast(const struct ofproto *ofproto)
656{
657 assert(ofproto->ofproto_class == &ofproto_dpif_class);
658 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
659}
660
4acbc98d 661static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *,
abe529af 662 uint16_t ofp_port);
4acbc98d 663static struct ofport_dpif *get_odp_port(const struct ofproto_dpif *,
abe529af 664 uint32_t odp_port);
6a6455e5
EJ
665static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
666 const struct ofpbuf *, ovs_be16 initial_tci,
667 struct ds *);
abe529af
BP
668
669/* Packet processing. */
670static void update_learning_table(struct ofproto_dpif *,
671 const struct flow *, int vlan,
672 struct ofbundle *);
501f8d1f
BP
673/* Upcalls. */
674#define FLOW_MISS_MAX_BATCH 50
9b16c439 675static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
abe529af
BP
676
677/* Flow expiration. */
678static int expire(struct ofproto_dpif *);
679
6fca1ffb
BP
680/* NetFlow. */
681static void send_netflow_active_timeouts(struct ofproto_dpif *);
682
abe529af 683/* Utilities. */
52a90c29 684static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
6a7d1a39
BP
685static size_t compose_sflow_action(const struct ofproto_dpif *,
686 struct ofpbuf *odp_actions,
687 const struct flow *, uint32_t odp_port);
c06bba01
JP
688static void add_mirror_actions(struct action_xlate_ctx *ctx,
689 const struct flow *flow);
abe529af
BP
690/* Global variables. */
691static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
692\f
693/* Factory functions. */
694
695static void
696enumerate_types(struct sset *types)
697{
698 dp_enumerate_types(types);
699}
700
701static int
702enumerate_names(const char *type, struct sset *names)
703{
704 return dp_enumerate_names(type, names);
705}
706
707static int
708del(const char *type, const char *name)
709{
710 struct dpif *dpif;
711 int error;
712
713 error = dpif_open(name, type, &dpif);
714 if (!error) {
715 error = dpif_delete(dpif);
716 dpif_close(dpif);
717 }
718 return error;
719}
720\f
721/* Basic life-cycle. */
722
c57b2226
BP
723static int add_internal_flows(struct ofproto_dpif *);
724
abe529af
BP
725static struct ofproto *
726alloc(void)
727{
728 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
729 return &ofproto->up;
730}
731
732static void
733dealloc(struct ofproto *ofproto_)
734{
735 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
736 free(ofproto);
737}
738
739static int
0f5f95a9 740construct(struct ofproto *ofproto_)
abe529af
BP
741{
742 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
743 const char *name = ofproto->up.name;
744 int error;
745 int i;
746
747 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
748 if (error) {
749 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
750 return error;
751 }
752
753 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
6c1491fb 754 ofproto->n_matches = 0;
abe529af 755
be8194bb
JG
756 dpif_flow_flush(ofproto->dpif);
757 dpif_recv_purge(ofproto->dpif);
758
a12b3ead 759 error = dpif_recv_set(ofproto->dpif, true);
abe529af
BP
760 if (error) {
761 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
762 dpif_close(ofproto->dpif);
763 return error;
764 }
abe529af
BP
765
766 ofproto->netflow = NULL;
767 ofproto->sflow = NULL;
21f7563c 768 ofproto->stp = NULL;
abe529af 769 hmap_init(&ofproto->bundles);
e764773c 770 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
abe529af
BP
771 for (i = 0; i < MAX_MIRRORS; i++) {
772 ofproto->mirrors[i] = NULL;
773 }
774 ofproto->has_bonded_bundles = false;
775
776 timer_set_duration(&ofproto->next_expiration, 1000);
777
778 hmap_init(&ofproto->facets);
b0f7b9b5 779 hmap_init(&ofproto->subfacets);
9d6ac44e 780 ofproto->governor = NULL;
54a9cbc9
BP
781
782 for (i = 0; i < N_TABLES; i++) {
783 struct table_dpif *table = &ofproto->tables[i];
784
785 table->catchall_table = NULL;
786 table->other_table = NULL;
787 table->basis = random_uint32();
788 }
3c4a309c 789 ofproto->need_revalidate = 0;
abe529af
BP
790 tag_set_init(&ofproto->revalidate_set);
791
7ee20df1
BP
792 list_init(&ofproto->completions);
793
abe529af
BP
794 ofproto_dpif_unixctl_init();
795
ccb7c863 796 ofproto->has_mirrors = false;
daff3353
EJ
797 ofproto->has_bundle_action = false;
798
52a90c29
BP
799 hmap_init(&ofproto->vlandev_map);
800 hmap_init(&ofproto->realdev_vid_map);
801
b44a10b7
BP
802 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
803 hash_string(ofproto->up.name, 0));
6527c598 804 memset(&ofproto->stats, 0, sizeof ofproto->stats);
0f5f95a9
BP
805
806 ofproto_init_tables(ofproto_, N_TABLES);
c57b2226
BP
807 error = add_internal_flows(ofproto);
808 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
809
810 return error;
811}
812
813static int
814add_internal_flow(struct ofproto_dpif *ofproto, int id,
f25d0cf3 815 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
c57b2226
BP
816{
817 struct ofputil_flow_mod fm;
818 int error;
819
820 cls_rule_init_catchall(&fm.cr, 0);
821 cls_rule_set_reg(&fm.cr, 0, id);
623e1caf 822 fm.new_cookie = htonll(0);
c57b2226
BP
823 fm.cookie = htonll(0);
824 fm.cookie_mask = htonll(0);
825 fm.table_id = TBL_INTERNAL;
826 fm.command = OFPFC_ADD;
827 fm.idle_timeout = 0;
828 fm.hard_timeout = 0;
829 fm.buffer_id = 0;
830 fm.out_port = 0;
831 fm.flags = 0;
f25d0cf3
BP
832 fm.ofpacts = ofpacts->data;
833 fm.ofpacts_len = ofpacts->size;
c57b2226
BP
834
835 error = ofproto_flow_mod(&ofproto->up, &fm);
836 if (error) {
837 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
838 id, ofperr_to_string(error));
839 return error;
840 }
841
842 *rulep = rule_dpif_lookup__(ofproto, &fm.cr.flow, TBL_INTERNAL);
843 assert(*rulep != NULL);
0f5f95a9 844
abe529af
BP
845 return 0;
846}
847
c57b2226
BP
848static int
849add_internal_flows(struct ofproto_dpif *ofproto)
850{
f25d0cf3
BP
851 struct ofpact_controller *controller;
852 uint64_t ofpacts_stub[128 / 8];
853 struct ofpbuf ofpacts;
c57b2226
BP
854 int error;
855 int id;
856
f25d0cf3 857 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
c57b2226
BP
858 id = 1;
859
f25d0cf3
BP
860 controller = ofpact_put_CONTROLLER(&ofpacts);
861 controller->max_len = UINT16_MAX;
862 controller->controller_id = 0;
863 controller->reason = OFPR_NO_MATCH;
864 ofpact_pad(&ofpacts);
865
866 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
c57b2226
BP
867 if (error) {
868 return error;
869 }
870
f25d0cf3
BP
871 ofpbuf_clear(&ofpacts);
872 error = add_internal_flow(ofproto, id++, &ofpacts,
c57b2226
BP
873 &ofproto->no_packet_in_rule);
874 return error;
875}
876
7ee20df1
BP
877static void
878complete_operations(struct ofproto_dpif *ofproto)
879{
880 struct dpif_completion *c, *next;
881
882 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
883 ofoperation_complete(c->op, 0);
884 list_remove(&c->list_node);
885 free(c);
886 }
887}
888
abe529af
BP
889static void
890destruct(struct ofproto *ofproto_)
891{
892 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7ee20df1 893 struct rule_dpif *rule, *next_rule;
d0918789 894 struct oftable *table;
abe529af
BP
895 int i;
896
b44a10b7 897 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
7ee20df1
BP
898 complete_operations(ofproto);
899
0697b5c3
BP
900 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
901 struct cls_cursor cursor;
902
d0918789 903 cls_cursor_init(&cursor, &table->cls, NULL);
0697b5c3
BP
904 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
905 ofproto_rule_destroy(&rule->up);
906 }
7ee20df1
BP
907 }
908
abe529af
BP
909 for (i = 0; i < MAX_MIRRORS; i++) {
910 mirror_destroy(ofproto->mirrors[i]);
911 }
912
913 netflow_destroy(ofproto->netflow);
bae473fe 914 dpif_sflow_destroy(ofproto->sflow);
abe529af
BP
915 hmap_destroy(&ofproto->bundles);
916 mac_learning_destroy(ofproto->ml);
917
918 hmap_destroy(&ofproto->facets);
b0f7b9b5 919 hmap_destroy(&ofproto->subfacets);
9d6ac44e 920 governor_destroy(ofproto->governor);
abe529af 921
52a90c29
BP
922 hmap_destroy(&ofproto->vlandev_map);
923 hmap_destroy(&ofproto->realdev_vid_map);
924
abe529af
BP
925 dpif_close(ofproto->dpif);
926}
927
928static int
5fcc0d00 929run_fast(struct ofproto *ofproto_)
abe529af
BP
930{
931 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
0aa66d6e 932 struct ofport_dpif *ofport;
9b16c439 933 unsigned int work;
abe529af 934
0aa66d6e
EJ
935 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
936 port_run_fast(ofport);
937 }
938
9b16c439
BP
939 /* Handle one or more batches of upcalls, until there's nothing left to do
940 * or until we do a fixed total amount of work.
941 *
942 * We do work in batches because it can be much cheaper to set up a number
943 * of flows and fire off their patches all at once. We do multiple batches
944 * because in some cases handling a packet can cause another packet to be
945 * queued almost immediately as part of the return flow. Both
946 * optimizations can make major improvements on some benchmarks and
947 * presumably for real traffic as well. */
948 work = 0;
949 while (work < FLOW_MISS_MAX_BATCH) {
950 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
5fcc0d00 951 if (retval <= 0) {
9b16c439 952 return -retval;
501f8d1f 953 }
5fcc0d00
BP
954 work += retval;
955 }
956 return 0;
957}
958
959static int
960run(struct ofproto *ofproto_)
961{
962 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
963 struct ofport_dpif *ofport;
964 struct ofbundle *bundle;
965 int error;
966
967 if (!clogged) {
968 complete_operations(ofproto);
969 }
970 dpif_run(ofproto->dpif);
971
972 error = run_fast(ofproto_);
973 if (error) {
974 return error;
abe529af
BP
975 }
976
977 if (timer_expired(&ofproto->next_expiration)) {
978 int delay = expire(ofproto);
979 timer_set_duration(&ofproto->next_expiration, delay);
980 }
981
982 if (ofproto->netflow) {
6fca1ffb
BP
983 if (netflow_run(ofproto->netflow)) {
984 send_netflow_active_timeouts(ofproto);
985 }
abe529af
BP
986 }
987 if (ofproto->sflow) {
bae473fe 988 dpif_sflow_run(ofproto->sflow);
abe529af
BP
989 }
990
991 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
992 port_run(ofport);
993 }
994 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
995 bundle_run(bundle);
996 }
997
21f7563c 998 stp_run(ofproto);
1c313b88
BP
999 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
1000
abe529af
BP
1001 /* Now revalidate if there's anything to do. */
1002 if (ofproto->need_revalidate
1003 || !tag_set_is_empty(&ofproto->revalidate_set)) {
1004 struct tag_set revalidate_set = ofproto->revalidate_set;
1005 bool revalidate_all = ofproto->need_revalidate;
c57b2226 1006 struct facet *facet;
abe529af 1007
3c4a309c
BP
1008 switch (ofproto->need_revalidate) {
1009 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
1010 case REV_STP: COVERAGE_INC(rev_stp); break;
1011 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
1012 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
1013 case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
1014 }
1015
abe529af
BP
1016 /* Clear the revalidation flags. */
1017 tag_set_init(&ofproto->revalidate_set);
3c4a309c 1018 ofproto->need_revalidate = 0;
abe529af 1019
c57b2226 1020 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
abe529af
BP
1021 if (revalidate_all
1022 || tag_set_intersects(&revalidate_set, facet->tags)) {
15baa734 1023 facet_revalidate(facet);
abe529af
BP
1024 }
1025 }
1026 }
1027
6814e51f
BP
1028 /* Check the consistency of a random facet, to aid debugging. */
1029 if (!hmap_is_empty(&ofproto->facets) && !ofproto->need_revalidate) {
1030 struct facet *facet;
1031
1032 facet = CONTAINER_OF(hmap_random_node(&ofproto->facets),
1033 struct facet, hmap_node);
1034 if (!tag_set_intersects(&ofproto->revalidate_set, facet->tags)) {
1035 if (!facet_check_consistency(facet)) {
3c4a309c 1036 ofproto->need_revalidate = REV_INCONSISTENCY;
6814e51f
BP
1037 }
1038 }
1039 }
1040
9d6ac44e
BP
1041 if (ofproto->governor) {
1042 size_t n_subfacets;
1043
1044 governor_run(ofproto->governor);
1045
1046 /* If the governor has shrunk to its minimum size and the number of
1047 * subfacets has dwindled, then drop the governor entirely.
1048 *
1049 * For hysteresis, the number of subfacets to drop the governor is
1050 * smaller than the number needed to trigger its creation. */
1051 n_subfacets = hmap_count(&ofproto->subfacets);
1052 if (n_subfacets * 4 < ofproto->up.flow_eviction_threshold
1053 && governor_is_idle(ofproto->governor)) {
1054 governor_destroy(ofproto->governor);
1055 ofproto->governor = NULL;
1056 }
1057 }
1058
abe529af
BP
1059 return 0;
1060}
1061
1062static void
1063wait(struct ofproto *ofproto_)
1064{
1065 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1066 struct ofport_dpif *ofport;
1067 struct ofbundle *bundle;
1068
7ee20df1
BP
1069 if (!clogged && !list_is_empty(&ofproto->completions)) {
1070 poll_immediate_wake();
1071 }
1072
abe529af
BP
1073 dpif_wait(ofproto->dpif);
1074 dpif_recv_wait(ofproto->dpif);
1075 if (ofproto->sflow) {
bae473fe 1076 dpif_sflow_wait(ofproto->sflow);
abe529af
BP
1077 }
1078 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
1079 poll_immediate_wake();
1080 }
1081 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1082 port_wait(ofport);
1083 }
1084 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1085 bundle_wait(bundle);
1086 }
6fca1ffb
BP
1087 if (ofproto->netflow) {
1088 netflow_wait(ofproto->netflow);
1089 }
1c313b88 1090 mac_learning_wait(ofproto->ml);
21f7563c 1091 stp_wait(ofproto);
abe529af
BP
1092 if (ofproto->need_revalidate) {
1093 /* Shouldn't happen, but if it does just go around again. */
1094 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1095 poll_immediate_wake();
1096 } else {
1097 timer_wait(&ofproto->next_expiration);
1098 }
9d6ac44e
BP
1099 if (ofproto->governor) {
1100 governor_wait(ofproto->governor);
1101 }
abe529af
BP
1102}
1103
0d085684
BP
1104static void
1105get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1106{
1107 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1108
1109 simap_increase(usage, "facets", hmap_count(&ofproto->facets));
1110 simap_increase(usage, "subfacets", hmap_count(&ofproto->subfacets));
1111}
1112
abe529af
BP
1113static void
1114flush(struct ofproto *ofproto_)
1115{
1116 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1117 struct facet *facet, *next_facet;
1118
1119 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1120 /* Mark the facet as not installed so that facet_remove() doesn't
1121 * bother trying to uninstall it. There is no point in uninstalling it
1122 * individually since we are about to blow away all the facets with
1123 * dpif_flow_flush(). */
b0f7b9b5
BP
1124 struct subfacet *subfacet;
1125
1126 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
6a7e895f 1127 subfacet->path = SF_NOT_INSTALLED;
b0f7b9b5
BP
1128 subfacet->dp_packet_count = 0;
1129 subfacet->dp_byte_count = 0;
1130 }
15baa734 1131 facet_remove(facet);
abe529af
BP
1132 }
1133 dpif_flow_flush(ofproto->dpif);
1134}
1135
6c1491fb
BP
1136static void
1137get_features(struct ofproto *ofproto_ OVS_UNUSED,
9e1fd49b 1138 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
6c1491fb
BP
1139{
1140 *arp_match_ip = true;
9e1fd49b
BP
1141 *actions = (OFPUTIL_A_OUTPUT |
1142 OFPUTIL_A_SET_VLAN_VID |
1143 OFPUTIL_A_SET_VLAN_PCP |
1144 OFPUTIL_A_STRIP_VLAN |
1145 OFPUTIL_A_SET_DL_SRC |
1146 OFPUTIL_A_SET_DL_DST |
1147 OFPUTIL_A_SET_NW_SRC |
1148 OFPUTIL_A_SET_NW_DST |
1149 OFPUTIL_A_SET_NW_TOS |
1150 OFPUTIL_A_SET_TP_SRC |
1151 OFPUTIL_A_SET_TP_DST |
1152 OFPUTIL_A_ENQUEUE);
6c1491fb
BP
1153}
1154
1155static void
e2b9ac44 1156get_tables(struct ofproto *ofproto_, struct ofp10_table_stats *ots)
6c1491fb
BP
1157{
1158 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
a8d9304d 1159 struct dpif_dp_stats s;
6c1491fb
BP
1160
1161 strcpy(ots->name, "classifier");
1162
1163 dpif_get_dp_stats(ofproto->dpif, &s);
1164 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
1165 put_32aligned_be64(&ots->matched_count,
1166 htonll(s.n_hit + ofproto->n_matches));
1167}
1168
abe529af
BP
1169static struct ofport *
1170port_alloc(void)
1171{
1172 struct ofport_dpif *port = xmalloc(sizeof *port);
1173 return &port->up;
1174}
1175
1176static void
1177port_dealloc(struct ofport *port_)
1178{
1179 struct ofport_dpif *port = ofport_dpif_cast(port_);
1180 free(port);
1181}
1182
1183static int
1184port_construct(struct ofport *port_)
1185{
1186 struct ofport_dpif *port = ofport_dpif_cast(port_);
1187 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1188
3c4a309c 1189 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1190 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
1191 port->bundle = NULL;
1192 port->cfm = NULL;
1193 port->tag = tag_create_random();
d5ffa7f2 1194 port->may_enable = true;
21f7563c
JP
1195 port->stp_port = NULL;
1196 port->stp_state = STP_DISABLED;
8b36f51e 1197 hmap_init(&port->priorities);
52a90c29
BP
1198 port->realdev_ofp_port = 0;
1199 port->vlandev_vid = 0;
3e5b3fdb 1200 port->carrier_seq = netdev_get_carrier_resets(port->up.netdev);
abe529af
BP
1201
1202 if (ofproto->sflow) {
392c7182 1203 dpif_sflow_add_port(ofproto->sflow, port_);
abe529af
BP
1204 }
1205
1206 return 0;
1207}
1208
1209static void
1210port_destruct(struct ofport *port_)
1211{
1212 struct ofport_dpif *port = ofport_dpif_cast(port_);
1213 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1214
3c4a309c 1215 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af 1216 bundle_remove(port_);
a5610457 1217 set_cfm(port_, NULL);
abe529af 1218 if (ofproto->sflow) {
bae473fe 1219 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
abe529af 1220 }
8b36f51e
EJ
1221
1222 ofport_clear_priorities(port);
1223 hmap_destroy(&port->priorities);
abe529af
BP
1224}
1225
1226static void
1227port_modified(struct ofport *port_)
1228{
1229 struct ofport_dpif *port = ofport_dpif_cast(port_);
1230
1231 if (port->bundle && port->bundle->bond) {
1232 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1233 }
1234}
1235
1236static void
9e1fd49b 1237port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
abe529af
BP
1238{
1239 struct ofport_dpif *port = ofport_dpif_cast(port_);
1240 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
9e1fd49b 1241 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
abe529af 1242
9e1fd49b 1243 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
c57b2226
BP
1244 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1245 OFPUTIL_PC_NO_PACKET_IN)) {
3c4a309c 1246 ofproto->need_revalidate = REV_RECONFIGURE;
7bde8dd8 1247
9e1fd49b 1248 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
7bde8dd8
JP
1249 bundle_update(port->bundle);
1250 }
abe529af
BP
1251 }
1252}
1253
1254static int
1255set_sflow(struct ofproto *ofproto_,
1256 const struct ofproto_sflow_options *sflow_options)
1257{
1258 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bae473fe 1259 struct dpif_sflow *ds = ofproto->sflow;
6ff686f2 1260
abe529af 1261 if (sflow_options) {
bae473fe 1262 if (!ds) {
abe529af
BP
1263 struct ofport_dpif *ofport;
1264
bae473fe 1265 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
abe529af 1266 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
392c7182 1267 dpif_sflow_add_port(ds, &ofport->up);
abe529af 1268 }
3c4a309c 1269 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af 1270 }
bae473fe 1271 dpif_sflow_set_options(ds, sflow_options);
abe529af 1272 } else {
6ff686f2
PS
1273 if (ds) {
1274 dpif_sflow_destroy(ds);
3c4a309c 1275 ofproto->need_revalidate = REV_RECONFIGURE;
6ff686f2
PS
1276 ofproto->sflow = NULL;
1277 }
abe529af
BP
1278 }
1279 return 0;
1280}
1281
1282static int
a5610457 1283set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
abe529af
BP
1284{
1285 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1286 int error;
1287
a5610457 1288 if (!s) {
abe529af
BP
1289 error = 0;
1290 } else {
1291 if (!ofport->cfm) {
8c977421
EJ
1292 struct ofproto_dpif *ofproto;
1293
1294 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3c4a309c 1295 ofproto->need_revalidate = REV_RECONFIGURE;
6f629657 1296 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
abe529af
BP
1297 }
1298
a5610457 1299 if (cfm_configure(ofport->cfm, s)) {
abe529af
BP
1300 return 0;
1301 }
1302
1303 error = EINVAL;
1304 }
1305 cfm_destroy(ofport->cfm);
1306 ofport->cfm = NULL;
1307 return error;
1308}
1309
1310static int
a5610457 1311get_cfm_fault(const struct ofport *ofport_)
abe529af
BP
1312{
1313 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
a5610457
EJ
1314
1315 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
abe529af 1316}
1de11730
EJ
1317
1318static int
1319get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1320 size_t *n_rmps)
1321{
1322 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1323
1324 if (ofport->cfm) {
1325 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1326 return 0;
1327 } else {
1328 return -1;
1329 }
1330}
3967a833
MM
1331
1332static int
1333get_cfm_health(const struct ofport *ofport_)
1334{
1335 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1336
1337 return ofport->cfm ? cfm_get_health(ofport->cfm) : -1;
1338}
abe529af 1339\f
21f7563c
JP
1340/* Spanning Tree. */
1341
1342static void
1343send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1344{
1345 struct ofproto_dpif *ofproto = ofproto_;
1346 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1347 struct ofport_dpif *ofport;
1348
1349 ofport = stp_port_get_aux(sp);
1350 if (!ofport) {
1351 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1352 ofproto->up.name, port_num);
1353 } else {
1354 struct eth_header *eth = pkt->l2;
1355
1356 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1357 if (eth_addr_is_zero(eth->eth_src)) {
1358 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1359 "with unknown MAC", ofproto->up.name, port_num);
1360 } else {
97d6520b 1361 send_packet(ofport, pkt);
21f7563c
JP
1362 }
1363 }
1364 ofpbuf_delete(pkt);
1365}
1366
1367/* Configures STP on 'ofproto_' using the settings defined in 's'. */
1368static int
1369set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1370{
1371 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1372
1373 /* Only revalidate flows if the configuration changed. */
1374 if (!s != !ofproto->stp) {
3c4a309c 1375 ofproto->need_revalidate = REV_RECONFIGURE;
21f7563c
JP
1376 }
1377
1378 if (s) {
1379 if (!ofproto->stp) {
1380 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1381 send_bpdu_cb, ofproto);
1382 ofproto->stp_last_tick = time_msec();
1383 }
1384
1385 stp_set_bridge_id(ofproto->stp, s->system_id);
1386 stp_set_bridge_priority(ofproto->stp, s->priority);
1387 stp_set_hello_time(ofproto->stp, s->hello_time);
1388 stp_set_max_age(ofproto->stp, s->max_age);
1389 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1390 } else {
851bf71d
EJ
1391 struct ofport *ofport;
1392
1393 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1394 set_stp_port(ofport, NULL);
1395 }
1396
21f7563c
JP
1397 stp_destroy(ofproto->stp);
1398 ofproto->stp = NULL;
1399 }
1400
1401 return 0;
1402}
1403
1404static int
1405get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1406{
1407 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1408
1409 if (ofproto->stp) {
1410 s->enabled = true;
1411 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1412 s->designated_root = stp_get_designated_root(ofproto->stp);
1413 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1414 } else {
1415 s->enabled = false;
1416 }
1417
1418 return 0;
1419}
1420
1421static void
1422update_stp_port_state(struct ofport_dpif *ofport)
1423{
1424 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1425 enum stp_state state;
1426
1427 /* Figure out new state. */
1428 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1429 : STP_DISABLED;
1430
1431 /* Update state. */
1432 if (ofport->stp_state != state) {
9e1fd49b 1433 enum ofputil_port_state of_state;
21f7563c
JP
1434 bool fwd_change;
1435
1436 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1437 netdev_get_name(ofport->up.netdev),
1438 stp_state_name(ofport->stp_state),
1439 stp_state_name(state));
1440 if (stp_learn_in_state(ofport->stp_state)
1441 != stp_learn_in_state(state)) {
1442 /* xxx Learning action flows should also be flushed. */
d0040604 1443 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
21f7563c
JP
1444 }
1445 fwd_change = stp_forward_in_state(ofport->stp_state)
1446 != stp_forward_in_state(state);
1447
3c4a309c 1448 ofproto->need_revalidate = REV_STP;
21f7563c
JP
1449 ofport->stp_state = state;
1450 ofport->stp_state_entered = time_msec();
1451
b308140a 1452 if (fwd_change && ofport->bundle) {
21f7563c
JP
1453 bundle_update(ofport->bundle);
1454 }
1455
1456 /* Update the STP state bits in the OpenFlow port description. */
9e1fd49b
BP
1457 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1458 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1459 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1460 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1461 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1462 : 0);
21f7563c
JP
1463 ofproto_port_set_state(&ofport->up, of_state);
1464 }
1465}
1466
1467/* Configures STP on 'ofport_' using the settings defined in 's'. The
1468 * caller is responsible for assigning STP port numbers and ensuring
1469 * there are no duplicates. */
1470static int
1471set_stp_port(struct ofport *ofport_,
1472 const struct ofproto_port_stp_settings *s)
1473{
1474 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1475 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1476 struct stp_port *sp = ofport->stp_port;
1477
1478 if (!s || !s->enable) {
1479 if (sp) {
1480 ofport->stp_port = NULL;
1481 stp_port_disable(sp);
ecd12731 1482 update_stp_port_state(ofport);
21f7563c
JP
1483 }
1484 return 0;
1485 } else if (sp && stp_port_no(sp) != s->port_num
1486 && ofport == stp_port_get_aux(sp)) {
1487 /* The port-id changed, so disable the old one if it's not
1488 * already in use by another port. */
1489 stp_port_disable(sp);
1490 }
1491
1492 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1493 stp_port_enable(sp);
1494
1495 stp_port_set_aux(sp, ofport);
1496 stp_port_set_priority(sp, s->priority);
1497 stp_port_set_path_cost(sp, s->path_cost);
1498
1499 update_stp_port_state(ofport);
1500
1501 return 0;
1502}
1503
1504static int
1505get_stp_port_status(struct ofport *ofport_,
1506 struct ofproto_port_stp_status *s)
1507{
1508 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1509 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1510 struct stp_port *sp = ofport->stp_port;
1511
1512 if (!ofproto->stp || !sp) {
1513 s->enabled = false;
1514 return 0;
1515 }
1516
1517 s->enabled = true;
1518 s->port_id = stp_port_get_id(sp);
1519 s->state = stp_port_get_state(sp);
1520 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1521 s->role = stp_port_get_role(sp);
80740385 1522 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
21f7563c
JP
1523
1524 return 0;
1525}
1526
1527static void
1528stp_run(struct ofproto_dpif *ofproto)
1529{
1530 if (ofproto->stp) {
1531 long long int now = time_msec();
1532 long long int elapsed = now - ofproto->stp_last_tick;
1533 struct stp_port *sp;
1534
1535 if (elapsed > 0) {
1536 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1537 ofproto->stp_last_tick = now;
1538 }
1539 while (stp_get_changed_port(ofproto->stp, &sp)) {
1540 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1541
1542 if (ofport) {
1543 update_stp_port_state(ofport);
1544 }
1545 }
6ae50723
EJ
1546
1547 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1548 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
1549 }
21f7563c
JP
1550 }
1551}
1552
1553static void
1554stp_wait(struct ofproto_dpif *ofproto)
1555{
1556 if (ofproto->stp) {
1557 poll_timer_wait(1000);
1558 }
1559}
1560
1561/* Returns true if STP should process 'flow'. */
1562static bool
1563stp_should_process_flow(const struct flow *flow)
1564{
1565 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1566}
1567
1568static void
1569stp_process_packet(const struct ofport_dpif *ofport,
1570 const struct ofpbuf *packet)
1571{
1572 struct ofpbuf payload = *packet;
1573 struct eth_header *eth = payload.data;
1574 struct stp_port *sp = ofport->stp_port;
1575
1576 /* Sink packets on ports that have STP disabled when the bridge has
1577 * STP enabled. */
1578 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1579 return;
1580 }
1581
1582 /* Trim off padding on payload. */
c573540b
BP
1583 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1584 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
21f7563c
JP
1585 }
1586
1587 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1588 stp_received_bpdu(sp, payload.data, payload.size);
1589 }
1590}
1591\f
8b36f51e
EJ
1592static struct priority_to_dscp *
1593get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1594{
1595 struct priority_to_dscp *pdscp;
1596 uint32_t hash;
1597
1598 hash = hash_int(priority, 0);
1599 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1600 if (pdscp->priority == priority) {
1601 return pdscp;
1602 }
1603 }
1604 return NULL;
1605}
1606
1607static void
1608ofport_clear_priorities(struct ofport_dpif *ofport)
1609{
1610 struct priority_to_dscp *pdscp, *next;
1611
1612 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1613 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1614 free(pdscp);
1615 }
1616}
1617
1618static int
1619set_queues(struct ofport *ofport_,
1620 const struct ofproto_port_queue *qdscp_list,
1621 size_t n_qdscp)
1622{
1623 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1624 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1625 struct hmap new = HMAP_INITIALIZER(&new);
1626 size_t i;
1627
1628 for (i = 0; i < n_qdscp; i++) {
1629 struct priority_to_dscp *pdscp;
1630 uint32_t priority;
1631 uint8_t dscp;
1632
1633 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1634 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1635 &priority)) {
1636 continue;
1637 }
1638
1639 pdscp = get_priority(ofport, priority);
1640 if (pdscp) {
1641 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1642 } else {
1643 pdscp = xmalloc(sizeof *pdscp);
1644 pdscp->priority = priority;
1645 pdscp->dscp = dscp;
3c4a309c 1646 ofproto->need_revalidate = REV_RECONFIGURE;
8b36f51e
EJ
1647 }
1648
1649 if (pdscp->dscp != dscp) {
1650 pdscp->dscp = dscp;
3c4a309c 1651 ofproto->need_revalidate = REV_RECONFIGURE;
8b36f51e
EJ
1652 }
1653
1654 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1655 }
1656
1657 if (!hmap_is_empty(&ofport->priorities)) {
1658 ofport_clear_priorities(ofport);
3c4a309c 1659 ofproto->need_revalidate = REV_RECONFIGURE;
8b36f51e
EJ
1660 }
1661
1662 hmap_swap(&new, &ofport->priorities);
1663 hmap_destroy(&new);
1664
1665 return 0;
1666}
1667\f
abe529af
BP
1668/* Bundles. */
1669
b44a10b7
BP
1670/* Expires all MAC learning entries associated with 'bundle' and forces its
1671 * ofproto to revalidate every flow.
1672 *
1673 * Normally MAC learning entries are removed only from the ofproto associated
1674 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
1675 * are removed from every ofproto. When patch ports and SLB bonds are in use
1676 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
1677 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
1678 * with the host from which it migrated. */
abe529af 1679static void
b44a10b7 1680bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
abe529af
BP
1681{
1682 struct ofproto_dpif *ofproto = bundle->ofproto;
1683 struct mac_learning *ml = ofproto->ml;
1684 struct mac_entry *mac, *next_mac;
1685
3c4a309c 1686 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1687 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1688 if (mac->port.p == bundle) {
b44a10b7
BP
1689 if (all_ofprotos) {
1690 struct ofproto_dpif *o;
1691
1692 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1693 if (o != ofproto) {
1694 struct mac_entry *e;
1695
1696 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan,
1697 NULL);
1698 if (e) {
1699 tag_set_add(&o->revalidate_set, e->tag);
1700 mac_learning_expire(o->ml, e);
1701 }
1702 }
1703 }
1704 }
1705
abe529af
BP
1706 mac_learning_expire(ml, mac);
1707 }
1708 }
1709}
1710
1711static struct ofbundle *
1712bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1713{
1714 struct ofbundle *bundle;
1715
1716 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1717 &ofproto->bundles) {
1718 if (bundle->aux == aux) {
1719 return bundle;
1720 }
1721 }
1722 return NULL;
1723}
1724
1725/* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1726 * ones that are found to 'bundles'. */
1727static void
1728bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1729 void **auxes, size_t n_auxes,
1730 struct hmapx *bundles)
1731{
1732 size_t i;
1733
1734 hmapx_init(bundles);
1735 for (i = 0; i < n_auxes; i++) {
1736 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1737 if (bundle) {
1738 hmapx_add(bundles, bundle);
1739 }
1740 }
1741}
1742
7bde8dd8
JP
1743static void
1744bundle_update(struct ofbundle *bundle)
1745{
1746 struct ofport_dpif *port;
1747
1748 bundle->floodable = true;
1749 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
9e1fd49b
BP
1750 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1751 || !stp_forward_in_state(port->stp_state)) {
7bde8dd8
JP
1752 bundle->floodable = false;
1753 break;
1754 }
1755 }
1756}
1757
abe529af
BP
1758static void
1759bundle_del_port(struct ofport_dpif *port)
1760{
1761 struct ofbundle *bundle = port->bundle;
1762
3c4a309c 1763 bundle->ofproto->need_revalidate = REV_RECONFIGURE;
6f77f4ae 1764
abe529af
BP
1765 list_remove(&port->bundle_node);
1766 port->bundle = NULL;
1767
1768 if (bundle->lacp) {
1769 lacp_slave_unregister(bundle->lacp, port);
1770 }
1771 if (bundle->bond) {
1772 bond_slave_unregister(bundle->bond, port);
1773 }
1774
7bde8dd8 1775 bundle_update(bundle);
abe529af
BP
1776}
1777
1778static bool
1779bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
00794817
BP
1780 struct lacp_slave_settings *lacp,
1781 uint32_t bond_stable_id)
abe529af
BP
1782{
1783 struct ofport_dpif *port;
1784
1785 port = get_ofp_port(bundle->ofproto, ofp_port);
1786 if (!port) {
1787 return false;
1788 }
1789
1790 if (port->bundle != bundle) {
3c4a309c 1791 bundle->ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1792 if (port->bundle) {
1793 bundle_del_port(port);
1794 }
1795
1796 port->bundle = bundle;
1797 list_push_back(&bundle->ports, &port->bundle_node);
9e1fd49b
BP
1798 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1799 || !stp_forward_in_state(port->stp_state)) {
abe529af
BP
1800 bundle->floodable = false;
1801 }
1802 }
1803 if (lacp) {
3c4a309c 1804 port->bundle->ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1805 lacp_slave_register(bundle->lacp, port, lacp);
1806 }
1807
00794817
BP
1808 port->bond_stable_id = bond_stable_id;
1809
abe529af
BP
1810 return true;
1811}
1812
1813static void
1814bundle_destroy(struct ofbundle *bundle)
1815{
1816 struct ofproto_dpif *ofproto;
1817 struct ofport_dpif *port, *next_port;
1818 int i;
1819
1820 if (!bundle) {
1821 return;
1822 }
1823
1824 ofproto = bundle->ofproto;
1825 for (i = 0; i < MAX_MIRRORS; i++) {
1826 struct ofmirror *m = ofproto->mirrors[i];
1827 if (m) {
1828 if (m->out == bundle) {
1829 mirror_destroy(m);
1830 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1831 || hmapx_find_and_delete(&m->dsts, bundle)) {
3c4a309c 1832 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1833 }
1834 }
1835 }
1836
1837 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1838 bundle_del_port(port);
1839 }
1840
b44a10b7 1841 bundle_flush_macs(bundle, true);
abe529af
BP
1842 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1843 free(bundle->name);
1844 free(bundle->trunks);
1845 lacp_destroy(bundle->lacp);
1846 bond_destroy(bundle->bond);
1847 free(bundle);
1848}
1849
1850static int
1851bundle_set(struct ofproto *ofproto_, void *aux,
1852 const struct ofproto_bundle_settings *s)
1853{
1854 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1855 bool need_flush = false;
abe529af
BP
1856 struct ofport_dpif *port;
1857 struct ofbundle *bundle;
ecac4ebf
BP
1858 unsigned long *trunks;
1859 int vlan;
abe529af
BP
1860 size_t i;
1861 bool ok;
1862
1863 if (!s) {
1864 bundle_destroy(bundle_lookup(ofproto, aux));
1865 return 0;
1866 }
1867
1868 assert(s->n_slaves == 1 || s->bond != NULL);
1869 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1870
1871 bundle = bundle_lookup(ofproto, aux);
1872 if (!bundle) {
1873 bundle = xmalloc(sizeof *bundle);
1874
1875 bundle->ofproto = ofproto;
1876 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1877 hash_pointer(aux, 0));
1878 bundle->aux = aux;
1879 bundle->name = NULL;
1880
1881 list_init(&bundle->ports);
ecac4ebf 1882 bundle->vlan_mode = PORT_VLAN_TRUNK;
abe529af
BP
1883 bundle->vlan = -1;
1884 bundle->trunks = NULL;
5e9ceccd 1885 bundle->use_priority_tags = s->use_priority_tags;
abe529af
BP
1886 bundle->lacp = NULL;
1887 bundle->bond = NULL;
1888
1889 bundle->floodable = true;
1890
1891 bundle->src_mirrors = 0;
1892 bundle->dst_mirrors = 0;
1893 bundle->mirror_out = 0;
1894 }
1895
1896 if (!bundle->name || strcmp(s->name, bundle->name)) {
1897 free(bundle->name);
1898 bundle->name = xstrdup(s->name);
1899 }
1900
1901 /* LACP. */
1902 if (s->lacp) {
1903 if (!bundle->lacp) {
3c4a309c 1904 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1905 bundle->lacp = lacp_create();
1906 }
1907 lacp_configure(bundle->lacp, s->lacp);
1908 } else {
1909 lacp_destroy(bundle->lacp);
1910 bundle->lacp = NULL;
1911 }
1912
1913 /* Update set of ports. */
1914 ok = true;
1915 for (i = 0; i < s->n_slaves; i++) {
1916 if (!bundle_add_port(bundle, s->slaves[i],
00794817
BP
1917 s->lacp ? &s->lacp_slaves[i] : NULL,
1918 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
abe529af
BP
1919 ok = false;
1920 }
1921 }
1922 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1923 struct ofport_dpif *next_port;
1924
1925 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1926 for (i = 0; i < s->n_slaves; i++) {
56c769ab 1927 if (s->slaves[i] == port->up.ofp_port) {
abe529af
BP
1928 goto found;
1929 }
1930 }
1931
1932 bundle_del_port(port);
1933 found: ;
1934 }
1935 }
1936 assert(list_size(&bundle->ports) <= s->n_slaves);
1937
1938 if (list_is_empty(&bundle->ports)) {
1939 bundle_destroy(bundle);
1940 return EINVAL;
1941 }
1942
ecac4ebf 1943 /* Set VLAN tagging mode */
5e9ceccd
BP
1944 if (s->vlan_mode != bundle->vlan_mode
1945 || s->use_priority_tags != bundle->use_priority_tags) {
ecac4ebf 1946 bundle->vlan_mode = s->vlan_mode;
5e9ceccd 1947 bundle->use_priority_tags = s->use_priority_tags;
ecac4ebf
BP
1948 need_flush = true;
1949 }
1950
abe529af 1951 /* Set VLAN tag. */
ecac4ebf
BP
1952 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1953 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1954 : 0);
1955 if (vlan != bundle->vlan) {
1956 bundle->vlan = vlan;
abe529af
BP
1957 need_flush = true;
1958 }
1959
1960 /* Get trunked VLANs. */
ecac4ebf
BP
1961 switch (s->vlan_mode) {
1962 case PORT_VLAN_ACCESS:
1963 trunks = NULL;
1964 break;
1965
1966 case PORT_VLAN_TRUNK:
1967 trunks = (unsigned long *) s->trunks;
1968 break;
1969
1970 case PORT_VLAN_NATIVE_UNTAGGED:
1971 case PORT_VLAN_NATIVE_TAGGED:
1972 if (vlan != 0 && (!s->trunks
1973 || !bitmap_is_set(s->trunks, vlan)
1974 || bitmap_is_set(s->trunks, 0))) {
1975 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1976 if (s->trunks) {
1977 trunks = bitmap_clone(s->trunks, 4096);
1978 } else {
1979 trunks = bitmap_allocate1(4096);
1980 }
1981 bitmap_set1(trunks, vlan);
1982 bitmap_set0(trunks, 0);
1983 } else {
1984 trunks = (unsigned long *) s->trunks;
1985 }
1986 break;
1987
1988 default:
1989 NOT_REACHED();
1990 }
abe529af
BP
1991 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1992 free(bundle->trunks);
ecac4ebf
BP
1993 if (trunks == s->trunks) {
1994 bundle->trunks = vlan_bitmap_clone(trunks);
1995 } else {
1996 bundle->trunks = trunks;
1997 trunks = NULL;
1998 }
abe529af
BP
1999 need_flush = true;
2000 }
ecac4ebf
BP
2001 if (trunks != s->trunks) {
2002 free(trunks);
2003 }
abe529af
BP
2004
2005 /* Bonding. */
2006 if (!list_is_short(&bundle->ports)) {
2007 bundle->ofproto->has_bonded_bundles = true;
2008 if (bundle->bond) {
2009 if (bond_reconfigure(bundle->bond, s->bond)) {
3c4a309c 2010 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2011 }
2012 } else {
2013 bundle->bond = bond_create(s->bond);
3c4a309c 2014 ofproto->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2015 }
2016
2017 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
00794817 2018 bond_slave_register(bundle->bond, port, port->bond_stable_id,
abe529af
BP
2019 port->up.netdev);
2020 }
2021 } else {
2022 bond_destroy(bundle->bond);
2023 bundle->bond = NULL;
2024 }
2025
2026 /* If we changed something that would affect MAC learning, un-learn
2027 * everything on this port and force flow revalidation. */
2028 if (need_flush) {
b44a10b7 2029 bundle_flush_macs(bundle, false);
abe529af
BP
2030 }
2031
2032 return 0;
2033}
2034
2035static void
2036bundle_remove(struct ofport *port_)
2037{
2038 struct ofport_dpif *port = ofport_dpif_cast(port_);
2039 struct ofbundle *bundle = port->bundle;
2040
2041 if (bundle) {
2042 bundle_del_port(port);
2043 if (list_is_empty(&bundle->ports)) {
2044 bundle_destroy(bundle);
2045 } else if (list_is_short(&bundle->ports)) {
2046 bond_destroy(bundle->bond);
2047 bundle->bond = NULL;
2048 }
2049 }
2050}
2051
2052static void
5f877369 2053send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
abe529af
BP
2054{
2055 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2056 struct ofport_dpif *port = port_;
2057 uint8_t ea[ETH_ADDR_LEN];
2058 int error;
2059
2060 error = netdev_get_etheraddr(port->up.netdev, ea);
2061 if (!error) {
abe529af 2062 struct ofpbuf packet;
5f877369 2063 void *packet_pdu;
abe529af
BP
2064
2065 ofpbuf_init(&packet, 0);
2066 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
5f877369
EJ
2067 pdu_size);
2068 memcpy(packet_pdu, pdu, pdu_size);
2069
97d6520b 2070 send_packet(port, &packet);
abe529af
BP
2071 ofpbuf_uninit(&packet);
2072 } else {
2073 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2074 "%s (%s)", port->bundle->name,
2075 netdev_get_name(port->up.netdev), strerror(error));
2076 }
2077}
2078
2079static void
2080bundle_send_learning_packets(struct ofbundle *bundle)
2081{
2082 struct ofproto_dpif *ofproto = bundle->ofproto;
2083 int error, n_packets, n_errors;
2084 struct mac_entry *e;
2085
2086 error = n_packets = n_errors = 0;
2087 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2088 if (e->port.p != bundle) {
ea131871
JG
2089 struct ofpbuf *learning_packet;
2090 struct ofport_dpif *port;
4dd1e3ca 2091 void *port_void;
ea131871
JG
2092 int ret;
2093
4dd1e3ca
BP
2094 /* The assignment to "port" is unnecessary but makes "grep"ing for
2095 * struct ofport_dpif more effective. */
2096 learning_packet = bond_compose_learning_packet(bundle->bond,
2097 e->mac, e->vlan,
2098 &port_void);
2099 port = port_void;
97d6520b 2100 ret = send_packet(port, learning_packet);
ea131871 2101 ofpbuf_delete(learning_packet);
abe529af
BP
2102 if (ret) {
2103 error = ret;
2104 n_errors++;
2105 }
2106 n_packets++;
2107 }
2108 }
2109
2110 if (n_errors) {
2111 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2112 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2113 "packets, last error was: %s",
2114 bundle->name, n_errors, n_packets, strerror(error));
2115 } else {
2116 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2117 bundle->name, n_packets);
2118 }
2119}
2120
2121static void
2122bundle_run(struct ofbundle *bundle)
2123{
2124 if (bundle->lacp) {
2125 lacp_run(bundle->lacp, send_pdu_cb);
2126 }
2127 if (bundle->bond) {
2128 struct ofport_dpif *port;
2129
2130 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
015e08bc 2131 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
abe529af
BP
2132 }
2133
2134 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
bdebeece 2135 lacp_status(bundle->lacp));
abe529af
BP
2136 if (bond_should_send_learning_packets(bundle->bond)) {
2137 bundle_send_learning_packets(bundle);
2138 }
2139 }
2140}
2141
2142static void
2143bundle_wait(struct ofbundle *bundle)
2144{
2145 if (bundle->lacp) {
2146 lacp_wait(bundle->lacp);
2147 }
2148 if (bundle->bond) {
2149 bond_wait(bundle->bond);
2150 }
2151}
2152\f
2153/* Mirrors. */
2154
2155static int
2156mirror_scan(struct ofproto_dpif *ofproto)
2157{
2158 int idx;
2159
2160 for (idx = 0; idx < MAX_MIRRORS; idx++) {
2161 if (!ofproto->mirrors[idx]) {
2162 return idx;
2163 }
2164 }
2165 return -1;
2166}
2167
2168static struct ofmirror *
2169mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
2170{
2171 int i;
2172
2173 for (i = 0; i < MAX_MIRRORS; i++) {
2174 struct ofmirror *mirror = ofproto->mirrors[i];
2175 if (mirror && mirror->aux == aux) {
2176 return mirror;
2177 }
2178 }
2179
2180 return NULL;
2181}
2182
9ba15e2a
BP
2183/* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
2184static void
2185mirror_update_dups(struct ofproto_dpif *ofproto)
2186{
2187 int i;
2188
2189 for (i = 0; i < MAX_MIRRORS; i++) {
2190 struct ofmirror *m = ofproto->mirrors[i];
2191
2192 if (m) {
2193 m->dup_mirrors = MIRROR_MASK_C(1) << i;
2194 }
2195 }
2196
2197 for (i = 0; i < MAX_MIRRORS; i++) {
2198 struct ofmirror *m1 = ofproto->mirrors[i];
2199 int j;
2200
2201 if (!m1) {
2202 continue;
2203 }
2204
2205 for (j = i + 1; j < MAX_MIRRORS; j++) {
2206 struct ofmirror *m2 = ofproto->mirrors[j];
2207
edb0540b 2208 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
9ba15e2a
BP
2209 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
2210 m2->dup_mirrors |= m1->dup_mirrors;
2211 }
2212 }
2213 }
2214}
2215
abe529af
BP
2216static int
2217mirror_set(struct ofproto *ofproto_, void *aux,
2218 const struct ofproto_mirror_settings *s)
2219{
2220 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2221 mirror_mask_t mirror_bit;
2222 struct ofbundle *bundle;
2223 struct ofmirror *mirror;
2224 struct ofbundle *out;
2225 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
2226 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
2227 int out_vlan;
2228
2229 mirror = mirror_lookup(ofproto, aux);
2230 if (!s) {
2231 mirror_destroy(mirror);
2232 return 0;
2233 }
2234 if (!mirror) {
2235 int idx;
2236
2237 idx = mirror_scan(ofproto);
2238 if (idx < 0) {
2239 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
2240 "cannot create %s",
2241 ofproto->up.name, MAX_MIRRORS, s->name);
2242 return EFBIG;
2243 }
2244
2245 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
2246 mirror->ofproto = ofproto;
2247 mirror->idx = idx;
8b28d864 2248 mirror->aux = aux;
abe529af
BP
2249 mirror->out_vlan = -1;
2250 mirror->name = NULL;
2251 }
2252
2253 if (!mirror->name || strcmp(s->name, mirror->name)) {
2254 free(mirror->name);
2255 mirror->name = xstrdup(s->name);
2256 }
2257
2258 /* Get the new configuration. */
2259 if (s->out_bundle) {
2260 out = bundle_lookup(ofproto, s->out_bundle);
2261 if (!out) {
2262 mirror_destroy(mirror);
2263 return EINVAL;
2264 }
2265 out_vlan = -1;
2266 } else {
2267 out = NULL;
2268 out_vlan = s->out_vlan;
2269 }
2270 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
2271 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
2272
2273 /* If the configuration has not changed, do nothing. */
2274 if (hmapx_equals(&srcs, &mirror->srcs)
2275 && hmapx_equals(&dsts, &mirror->dsts)
2276 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
2277 && mirror->out == out
2278 && mirror->out_vlan == out_vlan)
2279 {
2280 hmapx_destroy(&srcs);
2281 hmapx_destroy(&dsts);
2282 return 0;
2283 }
2284
2285 hmapx_swap(&srcs, &mirror->srcs);
2286 hmapx_destroy(&srcs);
2287
2288 hmapx_swap(&dsts, &mirror->dsts);
2289 hmapx_destroy(&dsts);
2290
2291 free(mirror->vlans);
2292 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
2293
2294 mirror->out = out;
2295 mirror->out_vlan = out_vlan;
2296
2297 /* Update bundles. */
2298 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2299 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
2300 if (hmapx_contains(&mirror->srcs, bundle)) {
2301 bundle->src_mirrors |= mirror_bit;
2302 } else {
2303 bundle->src_mirrors &= ~mirror_bit;
2304 }
2305
2306 if (hmapx_contains(&mirror->dsts, bundle)) {
2307 bundle->dst_mirrors |= mirror_bit;
2308 } else {
2309 bundle->dst_mirrors &= ~mirror_bit;
2310 }
2311
2312 if (mirror->out == bundle) {
2313 bundle->mirror_out |= mirror_bit;
2314 } else {
2315 bundle->mirror_out &= ~mirror_bit;
2316 }
2317 }
2318
3c4a309c 2319 ofproto->need_revalidate = REV_RECONFIGURE;
ccb7c863 2320 ofproto->has_mirrors = true;
d0040604 2321 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
9ba15e2a 2322 mirror_update_dups(ofproto);
abe529af
BP
2323
2324 return 0;
2325}
2326
2327static void
2328mirror_destroy(struct ofmirror *mirror)
2329{
2330 struct ofproto_dpif *ofproto;
2331 mirror_mask_t mirror_bit;
2332 struct ofbundle *bundle;
ccb7c863 2333 int i;
abe529af
BP
2334
2335 if (!mirror) {
2336 return;
2337 }
2338
2339 ofproto = mirror->ofproto;
3c4a309c 2340 ofproto->need_revalidate = REV_RECONFIGURE;
d0040604 2341 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
abe529af
BP
2342
2343 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2344 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2345 bundle->src_mirrors &= ~mirror_bit;
2346 bundle->dst_mirrors &= ~mirror_bit;
2347 bundle->mirror_out &= ~mirror_bit;
2348 }
2349
2350 hmapx_destroy(&mirror->srcs);
2351 hmapx_destroy(&mirror->dsts);
2352 free(mirror->vlans);
2353
2354 ofproto->mirrors[mirror->idx] = NULL;
2355 free(mirror->name);
2356 free(mirror);
9ba15e2a
BP
2357
2358 mirror_update_dups(ofproto);
ccb7c863
BP
2359
2360 ofproto->has_mirrors = false;
2361 for (i = 0; i < MAX_MIRRORS; i++) {
2362 if (ofproto->mirrors[i]) {
2363 ofproto->has_mirrors = true;
2364 break;
2365 }
2366 }
abe529af
BP
2367}
2368
9d24de3b
JP
2369static int
2370mirror_get_stats(struct ofproto *ofproto_, void *aux,
2371 uint64_t *packets, uint64_t *bytes)
2372{
2373 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2374 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2375
2376 if (!mirror) {
2377 *packets = *bytes = UINT64_MAX;
2378 return 0;
2379 }
2380
2381 *packets = mirror->packet_count;
2382 *bytes = mirror->byte_count;
2383
2384 return 0;
2385}
2386
abe529af
BP
2387static int
2388set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2389{
2390 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2391 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
d0040604 2392 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
abe529af
BP
2393 }
2394 return 0;
2395}
2396
2397static bool
b4affc74 2398is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
abe529af
BP
2399{
2400 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2401 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2402 return bundle && bundle->mirror_out != 0;
2403}
8402c74b
SS
2404
2405static void
b53055f4 2406forward_bpdu_changed(struct ofproto *ofproto_)
8402c74b
SS
2407{
2408 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3c4a309c 2409 ofproto->need_revalidate = REV_RECONFIGURE;
8402c74b 2410}
e764773c
BP
2411
2412static void
2413set_mac_idle_time(struct ofproto *ofproto_, unsigned int idle_time)
2414{
2415 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2416 mac_learning_set_idle_time(ofproto->ml, idle_time);
2417}
abe529af
BP
2418\f
2419/* Ports. */
2420
2421static struct ofport_dpif *
4acbc98d 2422get_ofp_port(const struct ofproto_dpif *ofproto, uint16_t ofp_port)
abe529af 2423{
7df6a8bd
BP
2424 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2425 return ofport ? ofport_dpif_cast(ofport) : NULL;
abe529af
BP
2426}
2427
2428static struct ofport_dpif *
4acbc98d 2429get_odp_port(const struct ofproto_dpif *ofproto, uint32_t odp_port)
abe529af
BP
2430{
2431 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2432}
2433
2434static void
2435ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2436 struct dpif_port *dpif_port)
2437{
2438 ofproto_port->name = dpif_port->name;
2439 ofproto_port->type = dpif_port->type;
2440 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2441}
2442
0aa66d6e
EJ
2443static void
2444port_run_fast(struct ofport_dpif *ofport)
2445{
2446 if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) {
2447 struct ofpbuf packet;
2448
2449 ofpbuf_init(&packet, 0);
2450 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr);
2451 send_packet(ofport, &packet);
2452 ofpbuf_uninit(&packet);
2453 }
2454}
2455
abe529af
BP
2456static void
2457port_run(struct ofport_dpif *ofport)
2458{
3e5b3fdb
EJ
2459 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2460 bool carrier_changed = carrier_seq != ofport->carrier_seq;
015e08bc
EJ
2461 bool enable = netdev_get_carrier(ofport->up.netdev);
2462
3e5b3fdb
EJ
2463 ofport->carrier_seq = carrier_seq;
2464
0aa66d6e 2465 port_run_fast(ofport);
abe529af
BP
2466 if (ofport->cfm) {
2467 cfm_run(ofport->cfm);
86dc6501
EJ
2468 enable = enable && !cfm_get_fault(ofport->cfm)
2469 && cfm_get_opup(ofport->cfm);
abe529af 2470 }
015e08bc
EJ
2471
2472 if (ofport->bundle) {
2473 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3e5b3fdb
EJ
2474 if (carrier_changed) {
2475 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2476 }
015e08bc
EJ
2477 }
2478
daff3353
EJ
2479 if (ofport->may_enable != enable) {
2480 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2481
2482 if (ofproto->has_bundle_action) {
3c4a309c 2483 ofproto->need_revalidate = REV_PORT_TOGGLED;
daff3353
EJ
2484 }
2485 }
2486
015e08bc 2487 ofport->may_enable = enable;
abe529af
BP
2488}
2489
2490static void
2491port_wait(struct ofport_dpif *ofport)
2492{
2493 if (ofport->cfm) {
2494 cfm_wait(ofport->cfm);
2495 }
2496}
2497
2498static int
2499port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2500 struct ofproto_port *ofproto_port)
2501{
2502 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2503 struct dpif_port dpif_port;
2504 int error;
2505
2506 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2507 if (!error) {
2508 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2509 }
2510 return error;
2511}
2512
2513static int
2514port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2515{
2516 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
232dfa4a 2517 uint16_t odp_port = UINT16_MAX;
abe529af
BP
2518 int error;
2519
2520 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2521 if (!error) {
2522 *ofp_portp = odp_port_to_ofp_port(odp_port);
2523 }
2524 return error;
2525}
2526
2527static int
2528port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2529{
2530 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2531 int error;
2532
2533 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2534 if (!error) {
2535 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2536 if (ofport) {
2537 /* The caller is going to close ofport->up.netdev. If this is a
2538 * bonded port, then the bond is using that netdev, so remove it
2539 * from the bond. The client will need to reconfigure everything
2540 * after deleting ports, so then the slave will get re-added. */
2541 bundle_remove(&ofport->up);
2542 }
2543 }
2544 return error;
2545}
2546
6527c598
PS
2547static int
2548port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2549{
2550 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2551 int error;
2552
2553 error = netdev_get_stats(ofport->up.netdev, stats);
2554
2555 if (!error && ofport->odp_port == OVSP_LOCAL) {
2556 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2557
2558 /* ofproto->stats.tx_packets represents packets that we created
2559 * internally and sent to some port (e.g. packets sent with
2560 * send_packet()). Account for them as if they had come from
2561 * OFPP_LOCAL and got forwarded. */
2562
2563 if (stats->rx_packets != UINT64_MAX) {
2564 stats->rx_packets += ofproto->stats.tx_packets;
2565 }
2566
2567 if (stats->rx_bytes != UINT64_MAX) {
2568 stats->rx_bytes += ofproto->stats.tx_bytes;
2569 }
2570
2571 /* ofproto->stats.rx_packets represents packets that were received on
2572 * some port and we processed internally and dropped (e.g. STP).
2573 * Account fro them as if they had been forwarded to OFPP_LOCAL. */
2574
2575 if (stats->tx_packets != UINT64_MAX) {
2576 stats->tx_packets += ofproto->stats.rx_packets;
2577 }
2578
2579 if (stats->tx_bytes != UINT64_MAX) {
2580 stats->tx_bytes += ofproto->stats.rx_bytes;
2581 }
2582 }
2583
2584 return error;
2585}
2586
2587/* Account packets for LOCAL port. */
2588static void
2589ofproto_update_local_port_stats(const struct ofproto *ofproto_,
2590 size_t tx_size, size_t rx_size)
2591{
2592 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2593
2594 if (rx_size) {
2595 ofproto->stats.rx_packets++;
2596 ofproto->stats.rx_bytes += rx_size;
2597 }
2598 if (tx_size) {
2599 ofproto->stats.tx_packets++;
2600 ofproto->stats.tx_bytes += tx_size;
2601 }
2602}
2603
abe529af
BP
2604struct port_dump_state {
2605 struct dpif_port_dump dump;
2606 bool done;
2607};
2608
2609static int
2610port_dump_start(const struct ofproto *ofproto_, void **statep)
2611{
2612 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2613 struct port_dump_state *state;
2614
2615 *statep = state = xmalloc(sizeof *state);
2616 dpif_port_dump_start(&state->dump, ofproto->dpif);
2617 state->done = false;
2618 return 0;
2619}
2620
2621static int
2622port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2623 struct ofproto_port *port)
2624{
2625 struct port_dump_state *state = state_;
2626 struct dpif_port dpif_port;
2627
2628 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2629 ofproto_port_from_dpif_port(port, &dpif_port);
2630 return 0;
2631 } else {
2632 int error = dpif_port_dump_done(&state->dump);
2633 state->done = true;
2634 return error ? error : EOF;
2635 }
2636}
2637
2638static int
2639port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2640{
2641 struct port_dump_state *state = state_;
2642
2643 if (!state->done) {
2644 dpif_port_dump_done(&state->dump);
2645 }
2646 free(state);
2647 return 0;
2648}
2649
2650static int
2651port_poll(const struct ofproto *ofproto_, char **devnamep)
2652{
2653 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2654 return dpif_port_poll(ofproto->dpif, devnamep);
2655}
2656
2657static void
2658port_poll_wait(const struct ofproto *ofproto_)
2659{
2660 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2661 dpif_port_poll_wait(ofproto->dpif);
2662}
2663
2664static int
2665port_is_lacp_current(const struct ofport *ofport_)
2666{
2667 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2668 return (ofport->bundle && ofport->bundle->lacp
2669 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2670 : -1);
2671}
2672\f
2673/* Upcall handling. */
2674
501f8d1f
BP
2675/* Flow miss batching.
2676 *
2677 * Some dpifs implement operations faster when you hand them off in a batch.
2678 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2679 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2680 * more packets, plus possibly installing the flow in the dpif.
2681 *
2682 * So far we only batch the operations that affect flow setup time the most.
2683 * It's possible to batch more than that, but the benefit might be minimal. */
2684struct flow_miss {
2685 struct hmap_node hmap_node;
2686 struct flow flow;
b0f7b9b5 2687 enum odp_key_fitness key_fitness;
501f8d1f
BP
2688 const struct nlattr *key;
2689 size_t key_len;
e84173dc 2690 ovs_be16 initial_tci;
501f8d1f 2691 struct list packets;
6a7e895f 2692 enum dpif_upcall_type upcall_type;
501f8d1f
BP
2693};
2694
2695struct flow_miss_op {
c2b565b5 2696 struct dpif_op dpif_op;
5fe20d5d
BP
2697 struct subfacet *subfacet; /* Subfacet */
2698 void *garbage; /* Pointer to pass to free(), NULL if none. */
2699 uint64_t stub[1024 / 8]; /* Temporary buffer. */
501f8d1f
BP
2700};
2701
62cd7072
BP
2702/* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2703 * OpenFlow controller as necessary according to their individual
29ebe880 2704 * configurations. */
62cd7072 2705static void
a39edbd4 2706send_packet_in_miss(struct ofproto_dpif *ofproto, const struct ofpbuf *packet,
29ebe880 2707 const struct flow *flow)
62cd7072
BP
2708{
2709 struct ofputil_packet_in pin;
2710
3e3252fa
EJ
2711 pin.packet = packet->data;
2712 pin.packet_len = packet->size;
62cd7072 2713 pin.reason = OFPR_NO_MATCH;
a7349929 2714 pin.controller_id = 0;
54834960
EJ
2715
2716 pin.table_id = 0;
2717 pin.cookie = 0;
2718
62cd7072 2719 pin.send_len = 0; /* not used for flow table misses */
5d6c3af0
EJ
2720
2721 flow_get_metadata(flow, &pin.fmd);
2722
2723 /* Registers aren't meaningful on a miss. */
2724 memset(pin.fmd.reg_masks, 0, sizeof pin.fmd.reg_masks);
2725
d8653c38 2726 connmgr_send_packet_in(ofproto->up.connmgr, &pin);
62cd7072
BP
2727}
2728
6a7e895f 2729static enum slow_path_reason
abe529af
BP
2730process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2731 const struct ofpbuf *packet)
2732{
b6e001b6
EJ
2733 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2734
2735 if (!ofport) {
6a7e895f 2736 return 0;
b6e001b6
EJ
2737 }
2738
ef9819b5 2739 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
b6e001b6 2740 if (packet) {
abe529af
BP
2741 cfm_process_heartbeat(ofport->cfm, packet);
2742 }
6a7e895f 2743 return SLOW_CFM;
b6e001b6
EJ
2744 } else if (ofport->bundle && ofport->bundle->lacp
2745 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2746 if (packet) {
2747 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
abe529af 2748 }
6a7e895f 2749 return SLOW_LACP;
21f7563c
JP
2750 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2751 if (packet) {
2752 stp_process_packet(ofport, packet);
2753 }
6a7e895f 2754 return SLOW_STP;
abe529af 2755 }
6a7e895f 2756 return 0;
abe529af
BP
2757}
2758
501f8d1f 2759static struct flow_miss *
b23cdad9 2760flow_miss_find(struct hmap *todo, const struct flow *flow, uint32_t hash)
abe529af 2761{
501f8d1f 2762 struct flow_miss *miss;
abe529af 2763
501f8d1f
BP
2764 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2765 if (flow_equal(&miss->flow, flow)) {
2766 return miss;
2767 }
2768 }
abe529af 2769
b23cdad9 2770 return NULL;
501f8d1f 2771}
abe529af 2772
9d6ac44e
BP
2773/* Partially Initializes 'op' as an "execute" operation for 'miss' and
2774 * 'packet'. The caller must initialize op->actions and op->actions_len. If
2775 * 'miss' is associated with a subfacet the caller must also initialize the
2776 * returned op->subfacet, and if anything needs to be freed after processing
2777 * the op, the caller must initialize op->garbage also. */
501f8d1f 2778static void
9d6ac44e
BP
2779init_flow_miss_execute_op(struct flow_miss *miss, struct ofpbuf *packet,
2780 struct flow_miss_op *op)
501f8d1f 2781{
9d6ac44e
BP
2782 if (miss->flow.vlan_tci != miss->initial_tci) {
2783 /* This packet was received on a VLAN splinter port. We
2784 * added a VLAN to the packet to make the packet resemble
2785 * the flow, but the actions were composed assuming that
2786 * the packet contained no VLAN. So, we must remove the
2787 * VLAN header from the packet before trying to execute the
2788 * actions. */
2789 eth_pop_vlan(packet);
2790 }
2791
2792 op->subfacet = NULL;
2793 op->garbage = NULL;
2794 op->dpif_op.type = DPIF_OP_EXECUTE;
2795 op->dpif_op.u.execute.key = miss->key;
2796 op->dpif_op.u.execute.key_len = miss->key_len;
2797 op->dpif_op.u.execute.packet = packet;
2798}
2799
2800/* Helper for handle_flow_miss_without_facet() and
2801 * handle_flow_miss_with_facet(). */
2802static void
2803handle_flow_miss_common(struct rule_dpif *rule,
2804 struct ofpbuf *packet, const struct flow *flow)
2805{
2806 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2807
2808 ofproto->n_matches++;
2809
2810 if (rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2811 /*
2812 * Extra-special case for fail-open mode.
2813 *
2814 * We are in fail-open mode and the packet matched the fail-open
2815 * rule, but we are connected to a controller too. We should send
2816 * the packet up to the controller in the hope that it will try to
2817 * set up a flow and thereby allow us to exit fail-open.
2818 *
2819 * See the top-level comment in fail-open.c for more information.
2820 */
2821 send_packet_in_miss(ofproto, packet, flow);
2822 }
2823}
2824
2825/* Figures out whether a flow that missed in 'ofproto', whose details are in
2826 * 'miss', is likely to be worth tracking in detail in userspace and (usually)
2827 * installing a datapath flow. The answer is usually "yes" (a return value of
2828 * true). However, for short flows the cost of bookkeeping is much higher than
2829 * the benefits, so when the datapath holds a large number of flows we impose
2830 * some heuristics to decide which flows are likely to be worth tracking. */
2831static bool
2832flow_miss_should_make_facet(struct ofproto_dpif *ofproto,
2833 struct flow_miss *miss, uint32_t hash)
2834{
2835 if (!ofproto->governor) {
2836 size_t n_subfacets;
2837
2838 n_subfacets = hmap_count(&ofproto->subfacets);
2839 if (n_subfacets * 2 <= ofproto->up.flow_eviction_threshold) {
2840 return true;
2841 }
2842
2843 ofproto->governor = governor_create(ofproto->up.name);
2844 }
2845
2846 return governor_should_install_flow(ofproto->governor, hash,
2847 list_size(&miss->packets));
2848}
2849
2850/* Handles 'miss', which matches 'rule', without creating a facet or subfacet
2851 * or creating any datapath flow. May add an "execute" operation to 'ops' and
2852 * increment '*n_ops'. */
2853static void
2854handle_flow_miss_without_facet(struct flow_miss *miss,
2855 struct rule_dpif *rule,
2856 struct flow_miss_op *ops, size_t *n_ops)
2857{
2858 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2859 struct action_xlate_ctx ctx;
530a1d91 2860 struct ofpbuf *packet;
2b459b83 2861
9d6ac44e
BP
2862 LIST_FOR_EACH (packet, list_node, &miss->packets) {
2863 struct flow_miss_op *op = &ops[*n_ops];
2864 struct dpif_flow_stats stats;
2865 struct ofpbuf odp_actions;
abe529af 2866
9d6ac44e 2867 COVERAGE_INC(facet_suppress);
501f8d1f 2868
9d6ac44e 2869 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
501f8d1f 2870
9d6ac44e
BP
2871 dpif_flow_stats_extract(&miss->flow, packet, &stats);
2872 rule_credit_stats(rule, &stats);
abe529af 2873
9d6ac44e
BP
2874 action_xlate_ctx_init(&ctx, ofproto, &miss->flow, miss->initial_tci,
2875 rule, 0, packet);
2876 ctx.resubmit_stats = &stats;
f25d0cf3 2877 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len,
9d6ac44e 2878 &odp_actions);
abe529af 2879
9d6ac44e
BP
2880 if (odp_actions.size) {
2881 struct dpif_execute *execute = &op->dpif_op.u.execute;
2882
2883 init_flow_miss_execute_op(miss, packet, op);
2884 execute->actions = odp_actions.data;
2885 execute->actions_len = odp_actions.size;
2886 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
2887
2888 (*n_ops)++;
2889 } else {
2890 ofpbuf_uninit(&odp_actions);
2891 }
abe529af 2892 }
9d6ac44e
BP
2893}
2894
2895/* Handles 'miss', which matches 'facet'. May add any required datapath
2896 * operations to 'ops', incrementing '*n_ops' for each new op. */
2897static void
2898handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
2899 struct flow_miss_op *ops, size_t *n_ops)
2900{
6a7e895f
BP
2901 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
2902 enum subfacet_path want_path;
9d6ac44e
BP
2903 struct subfacet *subfacet;
2904 struct ofpbuf *packet;
abe529af 2905
15baa734 2906 subfacet = subfacet_create(facet,
e84173dc
BP
2907 miss->key_fitness, miss->key, miss->key_len,
2908 miss->initial_tci);
b0f7b9b5 2909
530a1d91 2910 LIST_FOR_EACH (packet, list_node, &miss->packets) {
5fe20d5d 2911 struct flow_miss_op *op = &ops[*n_ops];
67d91f78 2912 struct dpif_flow_stats stats;
5fe20d5d 2913 struct ofpbuf odp_actions;
67d91f78 2914
9d6ac44e 2915 handle_flow_miss_common(facet->rule, packet, &miss->flow);
501f8d1f 2916
5fe20d5d 2917 ofpbuf_use_stub(&odp_actions, op->stub, sizeof op->stub);
6a7e895f 2918 if (!subfacet->actions || subfacet->slow) {
5fe20d5d 2919 subfacet_make_actions(subfacet, packet, &odp_actions);
501f8d1f 2920 }
67d91f78 2921
67d91f78 2922 dpif_flow_stats_extract(&facet->flow, packet, &stats);
15baa734 2923 subfacet_update_stats(subfacet, &stats);
67d91f78 2924
9d6ac44e
BP
2925 if (subfacet->actions_len) {
2926 struct dpif_execute *execute = &op->dpif_op.u.execute;
8338659a 2927
9d6ac44e
BP
2928 init_flow_miss_execute_op(miss, packet, op);
2929 op->subfacet = subfacet;
6a7e895f 2930 if (!subfacet->slow) {
9d6ac44e
BP
2931 execute->actions = subfacet->actions;
2932 execute->actions_len = subfacet->actions_len;
2933 ofpbuf_uninit(&odp_actions);
2934 } else {
2935 execute->actions = odp_actions.data;
2936 execute->actions_len = odp_actions.size;
2937 op->garbage = ofpbuf_get_uninit_pointer(&odp_actions);
2938 }
999fba59 2939
9d6ac44e 2940 (*n_ops)++;
5fe20d5d 2941 } else {
9d6ac44e 2942 ofpbuf_uninit(&odp_actions);
5fe20d5d 2943 }
501f8d1f
BP
2944 }
2945
6a7e895f
BP
2946 want_path = subfacet_want_path(subfacet->slow);
2947 if (miss->upcall_type == DPIF_UC_MISS || subfacet->path != want_path) {
501f8d1f 2948 struct flow_miss_op *op = &ops[(*n_ops)++];
c2b565b5 2949 struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
501f8d1f 2950
b0f7b9b5 2951 op->subfacet = subfacet;
5fe20d5d 2952 op->garbage = NULL;
c2b565b5 2953 op->dpif_op.type = DPIF_OP_FLOW_PUT;
501f8d1f
BP
2954 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2955 put->key = miss->key;
2956 put->key_len = miss->key_len;
6a7e895f
BP
2957 if (want_path == SF_FAST_PATH) {
2958 put->actions = subfacet->actions;
2959 put->actions_len = subfacet->actions_len;
2960 } else {
2961 compose_slow_path(ofproto, &facet->flow, subfacet->slow,
2962 op->stub, sizeof op->stub,
2963 &put->actions, &put->actions_len);
2964 }
501f8d1f
BP
2965 put->stats = NULL;
2966 }
2967}
2968
9d6ac44e
BP
2969/* Handles flow miss 'miss' on 'ofproto'. May add any required datapath
2970 * operations to 'ops', incrementing '*n_ops' for each new op. */
2971static void
2972handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2973 struct flow_miss_op *ops, size_t *n_ops)
2974{
2975 struct facet *facet;
2976 uint32_t hash;
2977
2978 /* The caller must ensure that miss->hmap_node.hash contains
2979 * flow_hash(miss->flow, 0). */
2980 hash = miss->hmap_node.hash;
2981
2982 facet = facet_lookup_valid(ofproto, &miss->flow, hash);
2983 if (!facet) {
c57b2226
BP
2984 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &miss->flow);
2985
2986 if (!flow_miss_should_make_facet(ofproto, miss, hash)) {
9d6ac44e
BP
2987 handle_flow_miss_without_facet(miss, rule, ops, n_ops);
2988 return;
2989 }
2990
2991 facet = facet_create(rule, &miss->flow, hash);
2992 }
2993 handle_flow_miss_with_facet(miss, facet, ops, n_ops);
2994}
2995
e2a6ca36
BP
2996/* Like odp_flow_key_to_flow(), this function converts the 'key_len' bytes of
2997 * OVS_KEY_ATTR_* attributes in 'key' to a flow structure in 'flow' and returns
2998 * an ODP_FIT_* value that indicates how well 'key' fits our expectations for
2999 * what a flow key should contain.
3000 *
3001 * This function also includes some logic to help make VLAN splinters
3002 * transparent to the rest of the upcall processing logic. In particular, if
3003 * the extracted in_port is a VLAN splinter port, it replaces flow->in_port by
3004 * the "real" port, sets flow->vlan_tci correctly for the VLAN of the VLAN
3005 * splinter port, and pushes a VLAN header onto 'packet' (if it is nonnull).
3006 *
3007 * Sets '*initial_tci' to the VLAN TCI with which the packet was really
3008 * received, that is, the actual VLAN TCI extracted by odp_flow_key_to_flow().
3009 * (This differs from the value returned in flow->vlan_tci only for packets
3010 * received on VLAN splinters.)
3011 */
e84173dc 3012static enum odp_key_fitness
52a90c29 3013ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto,
e84173dc 3014 const struct nlattr *key, size_t key_len,
e2a6ca36
BP
3015 struct flow *flow, ovs_be16 *initial_tci,
3016 struct ofpbuf *packet)
e84173dc
BP
3017{
3018 enum odp_key_fitness fitness;
3019
3020 fitness = odp_flow_key_to_flow(key, key_len, flow);
3021 if (fitness == ODP_FIT_ERROR) {
3022 return fitness;
3023 }
3024 *initial_tci = flow->vlan_tci;
3025
b98d8985 3026 if (vsp_adjust_flow(ofproto, flow)) {
e2a6ca36
BP
3027 if (packet) {
3028 /* Make the packet resemble the flow, so that it gets sent to an
3029 * OpenFlow controller properly, so that it looks correct for
3030 * sFlow, and so that flow_extract() will get the correct vlan_tci
3031 * if it is called on 'packet'.
3032 *
3033 * The allocated space inside 'packet' probably also contains
3034 * 'key', that is, both 'packet' and 'key' are probably part of a
3035 * struct dpif_upcall (see the large comment on that structure
3036 * definition), so pushing data on 'packet' is in general not a
3037 * good idea since it could overwrite 'key' or free it as a side
3038 * effect. However, it's OK in this special case because we know
3039 * that 'packet' is inside a Netlink attribute: pushing 4 bytes
3040 * will just overwrite the 4-byte "struct nlattr", which is fine
3041 * since we don't need that header anymore. */
3042 eth_push_vlan(packet, flow->vlan_tci);
3043 }
52a90c29
BP
3044
3045 /* Let the caller know that we can't reproduce 'key' from 'flow'. */
3046 if (fitness == ODP_FIT_PERFECT) {
3047 fitness = ODP_FIT_TOO_MUCH;
3048 }
3049 }
3050
e84173dc
BP
3051 return fitness;
3052}
3053
501f8d1f
BP
3054static void
3055handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
3056 size_t n_upcalls)
3057{
3058 struct dpif_upcall *upcall;
b23cdad9
BP
3059 struct flow_miss *miss;
3060 struct flow_miss misses[FLOW_MISS_MAX_BATCH];
501f8d1f 3061 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
c2b565b5 3062 struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
501f8d1f 3063 struct hmap todo;
b23cdad9 3064 int n_misses;
501f8d1f
BP
3065 size_t n_ops;
3066 size_t i;
3067
3068 if (!n_upcalls) {
3069 return;
3070 }
3071
3072 /* Construct the to-do list.
3073 *
3074 * This just amounts to extracting the flow from each packet and sticking
3075 * the packets that have the same flow in the same "flow_miss" structure so
3076 * that we can process them together. */
3077 hmap_init(&todo);
b23cdad9 3078 n_misses = 0;
501f8d1f 3079 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
b23cdad9
BP
3080 struct flow_miss *miss = &misses[n_misses];
3081 struct flow_miss *existing_miss;
3082 uint32_t hash;
501f8d1f 3083
b0f7b9b5
BP
3084 /* Obtain metadata and check userspace/kernel agreement on flow match,
3085 * then set 'flow''s header pointers. */
b23cdad9
BP
3086 miss->key_fitness = ofproto_dpif_extract_flow_key(
3087 ofproto, upcall->key, upcall->key_len,
3088 &miss->flow, &miss->initial_tci, upcall->packet);
3089 if (miss->key_fitness == ODP_FIT_ERROR) {
b0f7b9b5
BP
3090 continue;
3091 }
b23cdad9
BP
3092 flow_extract(upcall->packet, miss->flow.skb_priority,
3093 miss->flow.tun_id, miss->flow.in_port, &miss->flow);
501f8d1f 3094
501f8d1f 3095 /* Add other packets to a to-do list. */
b23cdad9
BP
3096 hash = flow_hash(&miss->flow, 0);
3097 existing_miss = flow_miss_find(&todo, &miss->flow, hash);
3098 if (!existing_miss) {
3099 hmap_insert(&todo, &miss->hmap_node, hash);
3100 miss->key = upcall->key;
3101 miss->key_len = upcall->key_len;
6a7e895f 3102 miss->upcall_type = upcall->type;
b23cdad9
BP
3103 list_init(&miss->packets);
3104
3105 n_misses++;
3106 } else {
3107 miss = existing_miss;
3108 }
501f8d1f
BP
3109 list_push_back(&miss->packets, &upcall->packet->list_node);
3110 }
3111
3112 /* Process each element in the to-do list, constructing the set of
3113 * operations to batch. */
3114 n_ops = 0;
33bb0caa 3115 HMAP_FOR_EACH (miss, hmap_node, &todo) {
501f8d1f 3116 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
abe529af 3117 }
501f8d1f 3118 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
501f8d1f
BP
3119
3120 /* Execute batch. */
3121 for (i = 0; i < n_ops; i++) {
3122 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3123 }
3124 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
3125
3126 /* Free memory and update facets. */
3127 for (i = 0; i < n_ops; i++) {
3128 struct flow_miss_op *op = &flow_miss_ops[i];
501f8d1f
BP
3129
3130 switch (op->dpif_op.type) {
3131 case DPIF_OP_EXECUTE:
501f8d1f 3132 break;
abe529af 3133
501f8d1f 3134 case DPIF_OP_FLOW_PUT:
c2b565b5 3135 if (!op->dpif_op.error) {
6a7e895f 3136 op->subfacet->path = subfacet_want_path(op->subfacet->slow);
501f8d1f
BP
3137 }
3138 break;
b99d3cee
BP
3139
3140 case DPIF_OP_FLOW_DEL:
3141 NOT_REACHED();
501f8d1f 3142 }
5fe20d5d
BP
3143
3144 free(op->garbage);
501f8d1f 3145 }
33bb0caa 3146 hmap_destroy(&todo);
abe529af
BP
3147}
3148
6a7e895f
BP
3149static enum { SFLOW_UPCALL, MISS_UPCALL, BAD_UPCALL }
3150classify_upcall(const struct dpif_upcall *upcall)
3151{
3152 union user_action_cookie cookie;
3153
3154 /* First look at the upcall type. */
3155 switch (upcall->type) {
3156 case DPIF_UC_ACTION:
3157 break;
3158
3159 case DPIF_UC_MISS:
3160 return MISS_UPCALL;
3161
3162 case DPIF_N_UC_TYPES:
3163 default:
3164 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3165 return BAD_UPCALL;
3166 }
3167
3168 /* "action" upcalls need a closer look. */
3169 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
3170 switch (cookie.type) {
3171 case USER_ACTION_COOKIE_SFLOW:
3172 return SFLOW_UPCALL;
3173
3174 case USER_ACTION_COOKIE_SLOW_PATH:
3175 return MISS_UPCALL;
3176
3177 case USER_ACTION_COOKIE_UNSPEC:
3178 default:
3179 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
3180 return BAD_UPCALL;
3181 }
3182}
3183
abe529af 3184static void
6a7e895f
BP
3185handle_sflow_upcall(struct ofproto_dpif *ofproto,
3186 const struct dpif_upcall *upcall)
abe529af 3187{
1673e0e4 3188 union user_action_cookie cookie;
e84173dc
BP
3189 enum odp_key_fitness fitness;
3190 ovs_be16 initial_tci;
3191 struct flow flow;
abe529af 3192
e84173dc
BP
3193 fitness = ofproto_dpif_extract_flow_key(ofproto, upcall->key,
3194 upcall->key_len, &flow,
e2a6ca36 3195 &initial_tci, upcall->packet);
e84173dc
BP
3196 if (fitness == ODP_FIT_ERROR) {
3197 return;
3198 }
3199
6a7e895f
BP
3200 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
3201 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
6ff686f2
PS
3202}
3203
9b16c439
BP
3204static int
3205handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
6ff686f2 3206{
9b16c439 3207 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
90a7c55e
BP
3208 struct ofpbuf miss_bufs[FLOW_MISS_MAX_BATCH];
3209 uint64_t miss_buf_stubs[FLOW_MISS_MAX_BATCH][4096 / 8];
3210 int n_processed;
9b16c439
BP
3211 int n_misses;
3212 int i;
abe529af 3213
90a7c55e 3214 assert(max_batch <= FLOW_MISS_MAX_BATCH);
abe529af 3215
9b16c439 3216 n_misses = 0;
90a7c55e 3217 for (n_processed = 0; n_processed < max_batch; n_processed++) {
9b16c439 3218 struct dpif_upcall *upcall = &misses[n_misses];
90a7c55e 3219 struct ofpbuf *buf = &miss_bufs[n_misses];
9b16c439
BP
3220 int error;
3221
90a7c55e
BP
3222 ofpbuf_use_stub(buf, miss_buf_stubs[n_misses],
3223 sizeof miss_buf_stubs[n_misses]);
3224 error = dpif_recv(ofproto->dpif, upcall, buf);
9b16c439 3225 if (error) {
90a7c55e 3226 ofpbuf_uninit(buf);
9b16c439
BP
3227 break;
3228 }
3229
6a7e895f
BP
3230 switch (classify_upcall(upcall)) {
3231 case MISS_UPCALL:
9b16c439
BP
3232 /* Handle it later. */
3233 n_misses++;
3234 break;
3235
6a7e895f
BP
3236 case SFLOW_UPCALL:
3237 if (ofproto->sflow) {
3238 handle_sflow_upcall(ofproto, upcall);
3239 }
3240 ofpbuf_uninit(buf);
3241 break;
3242
3243 case BAD_UPCALL:
3244 ofpbuf_uninit(buf);
9b16c439
BP
3245 break;
3246 }
abe529af 3247 }
9b16c439 3248
6a7e895f 3249 /* Handle deferred MISS_UPCALL processing. */
9b16c439 3250 handle_miss_upcalls(ofproto, misses, n_misses);
90a7c55e
BP
3251 for (i = 0; i < n_misses; i++) {
3252 ofpbuf_uninit(&miss_bufs[i]);
3253 }
9b16c439 3254
90a7c55e 3255 return n_processed;
abe529af
BP
3256}
3257\f
3258/* Flow expiration. */
3259
b0f7b9b5 3260static int subfacet_max_idle(const struct ofproto_dpif *);
abe529af
BP
3261static void update_stats(struct ofproto_dpif *);
3262static void rule_expire(struct rule_dpif *);
b0f7b9b5 3263static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
abe529af
BP
3264
3265/* This function is called periodically by run(). Its job is to collect
3266 * updates for the flows that have been installed into the datapath, most
3267 * importantly when they last were used, and then use that information to
3268 * expire flows that have not been used recently.
3269 *
3270 * Returns the number of milliseconds after which it should be called again. */
3271static int
3272expire(struct ofproto_dpif *ofproto)
3273{
3274 struct rule_dpif *rule, *next_rule;
d0918789 3275 struct oftable *table;
abe529af
BP
3276 int dp_max_idle;
3277
3278 /* Update stats for each flow in the datapath. */
3279 update_stats(ofproto);
3280
b0f7b9b5
BP
3281 /* Expire subfacets that have been idle too long. */
3282 dp_max_idle = subfacet_max_idle(ofproto);
3283 expire_subfacets(ofproto, dp_max_idle);
abe529af
BP
3284
3285 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
0697b5c3
BP
3286 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
3287 struct cls_cursor cursor;
3288
d0918789 3289 cls_cursor_init(&cursor, &table->cls, NULL);
0697b5c3
BP
3290 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
3291 rule_expire(rule);
3292 }
abe529af
BP
3293 }
3294
3295 /* All outstanding data in existing flows has been accounted, so it's a
3296 * good time to do bond rebalancing. */
3297 if (ofproto->has_bonded_bundles) {
3298 struct ofbundle *bundle;
3299
3300 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3301 if (bundle->bond) {
3302 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
3303 }
3304 }
3305 }
3306
3307 return MIN(dp_max_idle, 1000);
3308}
3309
a218c879
BP
3310/* Updates flow table statistics given that the datapath just reported 'stats'
3311 * as 'subfacet''s statistics. */
3312static void
3313update_subfacet_stats(struct subfacet *subfacet,
3314 const struct dpif_flow_stats *stats)
3315{
3316 struct facet *facet = subfacet->facet;
3317
3318 if (stats->n_packets >= subfacet->dp_packet_count) {
3319 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
3320 facet->packet_count += extra;
3321 } else {
3322 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3323 }
3324
3325 if (stats->n_bytes >= subfacet->dp_byte_count) {
3326 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
3327 } else {
3328 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3329 }
3330
3331 subfacet->dp_packet_count = stats->n_packets;
3332 subfacet->dp_byte_count = stats->n_bytes;
3333
3334 facet->tcp_flags |= stats->tcp_flags;
3335
3336 subfacet_update_time(subfacet, stats->used);
3337 if (facet->accounted_bytes < facet->byte_count) {
3338 facet_learn(facet);
3339 facet_account(facet);
3340 facet->accounted_bytes = facet->byte_count;
3341 }
3342 facet_push_stats(facet);
3343}
3344
3345/* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
3346 * about, or a flow that shouldn't be installed but was anyway. Delete it. */
3347static void
3348delete_unexpected_flow(struct dpif *dpif,
3349 const struct nlattr *key, size_t key_len)
3350{
3351 if (!VLOG_DROP_WARN(&rl)) {
3352 struct ds s;
3353
3354 ds_init(&s);
3355 odp_flow_key_format(key, key_len, &s);
3356 VLOG_WARN("unexpected flow from datapath %s", ds_cstr(&s));
3357 ds_destroy(&s);
3358 }
3359
3360 COVERAGE_INC(facet_unexpected);
3361 dpif_flow_del(dpif, key, key_len, NULL);
3362}
3363
abe529af
BP
3364/* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3365 *
3366 * This function also pushes statistics updates to rules which each facet
3367 * resubmits into. Generally these statistics will be accurate. However, if a
3368 * facet changes the rule it resubmits into at some time in between
3369 * update_stats() runs, it is possible that statistics accrued to the
3370 * old rule will be incorrectly attributed to the new rule. This could be
3371 * avoided by calling update_stats() whenever rules are created or
3372 * deleted. However, the performance impact of making so many calls to the
3373 * datapath do not justify the benefit of having perfectly accurate statistics.
3374 */
3375static void
3376update_stats(struct ofproto_dpif *p)
3377{
3378 const struct dpif_flow_stats *stats;
3379 struct dpif_flow_dump dump;
3380 const struct nlattr *key;
3381 size_t key_len;
3382
3383 dpif_flow_dump_start(&dump, p->dpif);
3384 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
b0f7b9b5 3385 struct subfacet *subfacet;
abe529af 3386
6a542738 3387 subfacet = subfacet_find(p, key, key_len);
6a7e895f
BP
3388 switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
3389 case SF_FAST_PATH:
a218c879 3390 update_subfacet_stats(subfacet, stats);
6a7e895f
BP
3391 break;
3392
3393 case SF_SLOW_PATH:
3394 /* Stats are updated per-packet. */
3395 break;
3396
3397 case SF_NOT_INSTALLED:
3398 default:
a218c879 3399 delete_unexpected_flow(p->dpif, key, key_len);
6a7e895f 3400 break;
abe529af
BP
3401 }
3402 }
3403 dpif_flow_dump_done(&dump);
3404}
3405
3406/* Calculates and returns the number of milliseconds of idle time after which
b0f7b9b5
BP
3407 * subfacets should expire from the datapath. When a subfacet expires, we fold
3408 * its statistics into its facet, and when a facet's last subfacet expires, we
3409 * fold its statistic into its rule. */
abe529af 3410static int
b0f7b9b5 3411subfacet_max_idle(const struct ofproto_dpif *ofproto)
abe529af
BP
3412{
3413 /*
3414 * Idle time histogram.
3415 *
b0f7b9b5
BP
3416 * Most of the time a switch has a relatively small number of subfacets.
3417 * When this is the case we might as well keep statistics for all of them
3418 * in userspace and to cache them in the kernel datapath for performance as
abe529af
BP
3419 * well.
3420 *
b0f7b9b5 3421 * As the number of subfacets increases, the memory required to maintain
abe529af 3422 * statistics about them in userspace and in the kernel becomes
b0f7b9b5
BP
3423 * significant. However, with a large number of subfacets it is likely
3424 * that only a few of them are "heavy hitters" that consume a large amount
3425 * of bandwidth. At this point, only heavy hitters are worth caching in
3426 * the kernel and maintaining in userspaces; other subfacets we can
3427 * discard.
abe529af
BP
3428 *
3429 * The technique used to compute the idle time is to build a histogram with
b0f7b9b5 3430 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
abe529af
BP
3431 * that is installed in the kernel gets dropped in the appropriate bucket.
3432 * After the histogram has been built, we compute the cutoff so that only
b0f7b9b5 3433 * the most-recently-used 1% of subfacets (but at least
084f5290 3434 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
b0f7b9b5
BP
3435 * the most-recently-used bucket of subfacets is kept, so actually an
3436 * arbitrary number of subfacets can be kept in any given expiration run
084f5290
SH
3437 * (though the next run will delete most of those unless they receive
3438 * additional data).
abe529af 3439 *
b0f7b9b5
BP
3440 * This requires a second pass through the subfacets, in addition to the
3441 * pass made by update_stats(), because the former function never looks at
3442 * uninstallable subfacets.
abe529af
BP
3443 */
3444 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
3445 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3446 int buckets[N_BUCKETS] = { 0 };
f11c1ef4 3447 int total, subtotal, bucket;
b0f7b9b5 3448 struct subfacet *subfacet;
abe529af
BP
3449 long long int now;
3450 int i;
3451
b0f7b9b5 3452 total = hmap_count(&ofproto->subfacets);
084f5290 3453 if (total <= ofproto->up.flow_eviction_threshold) {
abe529af
BP
3454 return N_BUCKETS * BUCKET_WIDTH;
3455 }
3456
3457 /* Build histogram. */
3458 now = time_msec();
b0f7b9b5
BP
3459 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
3460 long long int idle = now - subfacet->used;
abe529af
BP
3461 int bucket = (idle <= 0 ? 0
3462 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
3463 : (unsigned int) idle / BUCKET_WIDTH);
3464 buckets[bucket]++;
3465 }
3466
3467 /* Find the first bucket whose flows should be expired. */
f11c1ef4
SH
3468 subtotal = bucket = 0;
3469 do {
3470 subtotal += buckets[bucket++];
084f5290
SH
3471 } while (bucket < N_BUCKETS &&
3472 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
abe529af
BP
3473
3474 if (VLOG_IS_DBG_ENABLED()) {
3475 struct ds s;
3476
3477 ds_init(&s);
3478 ds_put_cstr(&s, "keep");
3479 for (i = 0; i < N_BUCKETS; i++) {
3480 if (i == bucket) {
3481 ds_put_cstr(&s, ", drop");
3482 }
3483 if (buckets[i]) {
3484 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
3485 }
3486 }
3487 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
3488 ds_destroy(&s);
3489 }
3490
3491 return bucket * BUCKET_WIDTH;
3492}
3493
b99d3cee
BP
3494enum { EXPIRE_MAX_BATCH = 50 };
3495
3496static void
3497expire_batch(struct ofproto_dpif *ofproto, struct subfacet **subfacets, int n)
3498{
3499 struct odputil_keybuf keybufs[EXPIRE_MAX_BATCH];
3500 struct dpif_op ops[EXPIRE_MAX_BATCH];
3501 struct dpif_op *opsp[EXPIRE_MAX_BATCH];
3502 struct ofpbuf keys[EXPIRE_MAX_BATCH];
3503 struct dpif_flow_stats stats[EXPIRE_MAX_BATCH];
3504 int i;
3505
3506 for (i = 0; i < n; i++) {
3507 ops[i].type = DPIF_OP_FLOW_DEL;
3508 subfacet_get_key(subfacets[i], &keybufs[i], &keys[i]);
3509 ops[i].u.flow_del.key = keys[i].data;
3510 ops[i].u.flow_del.key_len = keys[i].size;
3511 ops[i].u.flow_del.stats = &stats[i];
3512 opsp[i] = &ops[i];
3513 }
3514
3515 dpif_operate(ofproto->dpif, opsp, n);
3516 for (i = 0; i < n; i++) {
3517 subfacet_reset_dp_stats(subfacets[i], &stats[i]);
6a7e895f 3518 subfacets[i]->path = SF_NOT_INSTALLED;
b99d3cee
BP
3519 subfacet_destroy(subfacets[i]);
3520 }
3521}
3522
abe529af 3523static void
b0f7b9b5 3524expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
abe529af 3525{
625b0720
BP
3526 /* Cutoff time for most flows. */
3527 long long int normal_cutoff = time_msec() - dp_max_idle;
3528
3529 /* We really want to keep flows for special protocols around, so use a more
3530 * conservative cutoff. */
3531 long long int special_cutoff = time_msec() - 10000;
b99d3cee 3532
b0f7b9b5 3533 struct subfacet *subfacet, *next_subfacet;
b99d3cee
BP
3534 struct subfacet *batch[EXPIRE_MAX_BATCH];
3535 int n_batch;
abe529af 3536
b99d3cee 3537 n_batch = 0;
b0f7b9b5
BP
3538 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
3539 &ofproto->subfacets) {
625b0720
BP
3540 long long int cutoff;
3541
3542 cutoff = (subfacet->slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)
3543 ? special_cutoff
3544 : normal_cutoff);
b0f7b9b5 3545 if (subfacet->used < cutoff) {
6a7e895f 3546 if (subfacet->path != SF_NOT_INSTALLED) {
b99d3cee
BP
3547 batch[n_batch++] = subfacet;
3548 if (n_batch >= EXPIRE_MAX_BATCH) {
3549 expire_batch(ofproto, batch, n_batch);
3550 n_batch = 0;
3551 }
3552 } else {
3553 subfacet_destroy(subfacet);
3554 }
abe529af
BP
3555 }
3556 }
b99d3cee
BP
3557
3558 if (n_batch > 0) {
3559 expire_batch(ofproto, batch, n_batch);
3560 }
abe529af
BP
3561}
3562
3563/* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3564 * then delete it entirely. */
3565static void
3566rule_expire(struct rule_dpif *rule)
3567{
abe529af
BP
3568 struct facet *facet, *next_facet;
3569 long long int now;
3570 uint8_t reason;
3571
e2a3d183
BP
3572 if (rule->up.pending) {
3573 /* We'll have to expire it later. */
3574 return;
3575 }
3576
abe529af
BP
3577 /* Has 'rule' expired? */
3578 now = time_msec();
3579 if (rule->up.hard_timeout
308881af 3580 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
abe529af 3581 reason = OFPRR_HARD_TIMEOUT;
8ea6ac3e 3582 } else if (rule->up.idle_timeout
1745cd08 3583 && now > rule->up.used + rule->up.idle_timeout * 1000) {
abe529af
BP
3584 reason = OFPRR_IDLE_TIMEOUT;
3585 } else {
3586 return;
3587 }
3588
3589 COVERAGE_INC(ofproto_dpif_expired);
3590
3591 /* Update stats. (This is a no-op if the rule expired due to an idle
3592 * timeout, because that only happens when the rule has no facets left.) */
3593 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
15baa734 3594 facet_remove(facet);
abe529af
BP
3595 }
3596
3597 /* Get rid of the rule. */
3598 ofproto_rule_expire(&rule->up, reason);
3599}
3600\f
3601/* Facets. */
3602
f3827897 3603/* Creates and returns a new facet owned by 'rule', given a 'flow'.
abe529af
BP
3604 *
3605 * The caller must already have determined that no facet with an identical
3606 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
f3827897
BP
3607 * the ofproto's classifier table.
3608 *
2b459b83
BP
3609 * 'hash' must be the return value of flow_hash(flow, 0).
3610 *
b0f7b9b5
BP
3611 * The facet will initially have no subfacets. The caller should create (at
3612 * least) one subfacet with subfacet_create(). */
abe529af 3613static struct facet *
2b459b83 3614facet_create(struct rule_dpif *rule, const struct flow *flow, uint32_t hash)
abe529af
BP
3615{
3616 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3617 struct facet *facet;
3618
3619 facet = xzalloc(sizeof *facet);
3620 facet->used = time_msec();
2b459b83 3621 hmap_insert(&ofproto->facets, &facet->hmap_node, hash);
abe529af
BP
3622 list_push_back(&rule->facets, &facet->list_node);
3623 facet->rule = rule;
3624 facet->flow = *flow;
b0f7b9b5 3625 list_init(&facet->subfacets);
abe529af
BP
3626 netflow_flow_init(&facet->nf_flow);
3627 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
3628
abe529af
BP
3629 return facet;
3630}
3631
3632static void
3633facet_free(struct facet *facet)
3634{
abe529af
BP
3635 free(facet);
3636}
3637
3d9e05f8
BP
3638/* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
3639 * 'packet', which arrived on 'in_port'.
3640 *
3641 * Takes ownership of 'packet'. */
3642static bool
3643execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
3644 const struct nlattr *odp_actions, size_t actions_len,
3645 struct ofpbuf *packet)
3646{
3647 struct odputil_keybuf keybuf;
3648 struct ofpbuf key;
3649 int error;
3650
6ff686f2
PS
3651 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3652 odp_flow_key_from_flow(&key, flow);
80e5eed9 3653
6ff686f2
PS
3654 error = dpif_execute(ofproto->dpif, key.data, key.size,
3655 odp_actions, actions_len, packet);
80e5eed9 3656
6ff686f2
PS
3657 ofpbuf_delete(packet);
3658 return !error;
abe529af
BP
3659}
3660
abe529af
BP
3661/* Remove 'facet' from 'ofproto' and free up the associated memory:
3662 *
3663 * - If 'facet' was installed in the datapath, uninstalls it and updates its
b0f7b9b5 3664 * rule's statistics, via subfacet_uninstall().
abe529af
BP
3665 *
3666 * - Removes 'facet' from its rule and from ofproto->facets.
3667 */
3668static void
15baa734 3669facet_remove(struct facet *facet)
abe529af 3670{
15baa734 3671 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
b0f7b9b5
BP
3672 struct subfacet *subfacet, *next_subfacet;
3673
551a2f6c
BP
3674 assert(!list_is_empty(&facet->subfacets));
3675
3676 /* First uninstall all of the subfacets to get final statistics. */
3677 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
15baa734 3678 subfacet_uninstall(subfacet);
551a2f6c
BP
3679 }
3680
3681 /* Flush the final stats to the rule.
3682 *
3683 * This might require us to have at least one subfacet around so that we
3684 * can use its actions for accounting in facet_account(), which is why we
3685 * have uninstalled but not yet destroyed the subfacets. */
15baa734 3686 facet_flush_stats(facet);
551a2f6c
BP
3687
3688 /* Now we're really all done so destroy everything. */
b0f7b9b5
BP
3689 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
3690 &facet->subfacets) {
15baa734 3691 subfacet_destroy__(subfacet);
b0f7b9b5 3692 }
abe529af
BP
3693 hmap_remove(&ofproto->facets, &facet->hmap_node);
3694 list_remove(&facet->list_node);
3695 facet_free(facet);
3696}
3697
3de9590b
BP
3698/* Feed information from 'facet' back into the learning table to keep it in
3699 * sync with what is actually flowing through the datapath. */
abe529af 3700static void
3de9590b 3701facet_learn(struct facet *facet)
abe529af 3702{
15baa734 3703 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3de9590b 3704 struct action_xlate_ctx ctx;
abe529af 3705
3de9590b
BP
3706 if (!facet->has_learn
3707 && !facet->has_normal
3708 && (!facet->has_fin_timeout
3709 || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
abe529af
BP
3710 return;
3711 }
abe529af 3712
3de9590b
BP
3713 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3714 facet->flow.vlan_tci,
3715 facet->rule, facet->tcp_flags, NULL);
3716 ctx.may_learn = true;
f25d0cf3
BP
3717 xlate_actions_for_side_effects(&ctx, facet->rule->up.ofpacts,
3718 facet->rule->up.ofpacts_len);
3de9590b
BP
3719}
3720
3721static void
3722facet_account(struct facet *facet)
3723{
3724 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3725 struct subfacet *subfacet;
3726 const struct nlattr *a;
3727 unsigned int left;
3728 ovs_be16 vlan_tci;
3729 uint64_t n_bytes;
abe529af 3730
75a75043 3731 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
abe529af
BP
3732 return;
3733 }
3de9590b 3734 n_bytes = facet->byte_count - facet->accounted_bytes;
d78be13b
BP
3735
3736 /* This loop feeds byte counters to bond_account() for rebalancing to use
3737 * as a basis. We also need to track the actual VLAN on which the packet
3738 * is going to be sent to ensure that it matches the one passed to
3739 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
b95fc6ba
BP
3740 * hash bucket.)
3741 *
3742 * We use the actions from an arbitrary subfacet because they should all
3743 * be equally valid for our purpose. */
3744 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3745 struct subfacet, list_node);
d78be13b 3746 vlan_tci = facet->flow.vlan_tci;
b95fc6ba
BP
3747 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3748 subfacet->actions, subfacet->actions_len) {
fea393b1 3749 const struct ovs_action_push_vlan *vlan;
d78be13b 3750 struct ofport_dpif *port;
abe529af 3751
d78be13b 3752 switch (nl_attr_type(a)) {
df2c07f4 3753 case OVS_ACTION_ATTR_OUTPUT:
abe529af
BP
3754 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3755 if (port && port->bundle && port->bundle->bond) {
d78be13b 3756 bond_account(port->bundle->bond, &facet->flow,
dc155bff 3757 vlan_tci_to_vid(vlan_tci), n_bytes);
abe529af 3758 }
d78be13b
BP
3759 break;
3760
fea393b1
BP
3761 case OVS_ACTION_ATTR_POP_VLAN:
3762 vlan_tci = htons(0);
d78be13b
BP
3763 break;
3764
fea393b1
BP
3765 case OVS_ACTION_ATTR_PUSH_VLAN:
3766 vlan = nl_attr_get(a);
3767 vlan_tci = vlan->vlan_tci;
d78be13b 3768 break;
abe529af
BP
3769 }
3770 }
3771}
3772
abe529af
BP
3773/* Returns true if the only action for 'facet' is to send to the controller.
3774 * (We don't report NetFlow expiration messages for such facets because they
3775 * are just part of the control logic for the network, not real traffic). */
3776static bool
3777facet_is_controller_flow(struct facet *facet)
3778{
f25d0cf3
BP
3779 if (facet) {
3780 const struct rule *rule = &facet->rule->up;
3781 const struct ofpact *ofpacts = rule->ofpacts;
3782 size_t ofpacts_len = rule->ofpacts_len;
3783
3784 if (ofpacts->type == OFPACT_CONTROLLER &&
3785 ofpact_next(ofpacts) >= ofpact_end(ofpacts, ofpacts_len)) {
3786 return true;
3787 }
3788 }
3789 return false;
abe529af
BP
3790}
3791
3792/* Folds all of 'facet''s statistics into its rule. Also updates the
3793 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3794 * 'facet''s statistics in the datapath should have been zeroed and folded into
3795 * its packet and byte counts before this function is called. */
3796static void
15baa734 3797facet_flush_stats(struct facet *facet)
abe529af 3798{
15baa734 3799 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
b0f7b9b5
BP
3800 struct subfacet *subfacet;
3801
3802 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3803 assert(!subfacet->dp_byte_count);
3804 assert(!subfacet->dp_packet_count);
3805 }
abe529af
BP
3806
3807 facet_push_stats(facet);
3de9590b
BP
3808 if (facet->accounted_bytes < facet->byte_count) {
3809 facet_account(facet);
3810 facet->accounted_bytes = facet->byte_count;
3811 }
abe529af
BP
3812
3813 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3814 struct ofexpired expired;
3815 expired.flow = facet->flow;
3816 expired.packet_count = facet->packet_count;
3817 expired.byte_count = facet->byte_count;
3818 expired.used = facet->used;
3819 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3820 }
3821
3822 facet->rule->packet_count += facet->packet_count;
3823 facet->rule->byte_count += facet->byte_count;
3824
3825 /* Reset counters to prevent double counting if 'facet' ever gets
3826 * reinstalled. */
bbb5d219 3827 facet_reset_counters(facet);
abe529af
BP
3828
3829 netflow_flow_clear(&facet->nf_flow);
0e553d9c 3830 facet->tcp_flags = 0;
abe529af
BP
3831}
3832
3833/* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3834 * Returns it if found, otherwise a null pointer.
3835 *
2b459b83
BP
3836 * 'hash' must be the return value of flow_hash(flow, 0).
3837 *
abe529af
BP
3838 * The returned facet might need revalidation; use facet_lookup_valid()
3839 * instead if that is important. */
3840static struct facet *
2b459b83
BP
3841facet_find(struct ofproto_dpif *ofproto,
3842 const struct flow *flow, uint32_t hash)
abe529af
BP
3843{
3844 struct facet *facet;
3845
2b459b83 3846 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, hash, &ofproto->facets) {
abe529af
BP
3847 if (flow_equal(flow, &facet->flow)) {
3848 return facet;
3849 }
3850 }
3851
3852 return NULL;
3853}
3854
3855/* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3856 * Returns it if found, otherwise a null pointer.
3857 *
2b459b83
BP
3858 * 'hash' must be the return value of flow_hash(flow, 0).
3859 *
abe529af
BP
3860 * The returned facet is guaranteed to be valid. */
3861static struct facet *
2b459b83
BP
3862facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow,
3863 uint32_t hash)
abe529af 3864{
c57b2226 3865 struct facet *facet;
abe529af 3866
c57b2226 3867 facet = facet_find(ofproto, flow, hash);
abe529af 3868 if (facet
0e4b3771 3869 && (ofproto->need_revalidate
c57b2226
BP
3870 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))) {
3871 facet_revalidate(facet);
abe529af
BP
3872 }
3873
3874 return facet;
3875}
3876
6a7e895f
BP
3877static const char *
3878subfacet_path_to_string(enum subfacet_path path)
3879{
3880 switch (path) {
3881 case SF_NOT_INSTALLED:
3882 return "not installed";
3883 case SF_FAST_PATH:
3884 return "in fast path";
3885 case SF_SLOW_PATH:
3886 return "in slow path";
3887 default:
3888 return "<error>";
3889 }
3890}
3891
3892/* Returns the path in which a subfacet should be installed if its 'slow'
3893 * member has the specified value. */
3894static enum subfacet_path
3895subfacet_want_path(enum slow_path_reason slow)
3896{
3897 return slow ? SF_SLOW_PATH : SF_FAST_PATH;
3898}
3899
3900/* Returns true if 'subfacet' needs to have its datapath flow updated,
3901 * supposing that its actions have been recalculated as 'want_actions' and that
3902 * 'slow' is nonzero iff 'subfacet' should be in the slow path. */
3903static bool
3904subfacet_should_install(struct subfacet *subfacet, enum slow_path_reason slow,
3905 const struct ofpbuf *want_actions)
3906{
3907 enum subfacet_path want_path = subfacet_want_path(slow);
3908 return (want_path != subfacet->path
3909 || (want_path == SF_FAST_PATH
3910 && (subfacet->actions_len != want_actions->size
3911 || memcmp(subfacet->actions, want_actions->data,
3912 subfacet->actions_len))));
3913}
3914
6814e51f
BP
3915static bool
3916facet_check_consistency(struct facet *facet)
3917{
3918 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3919
3920 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3921
050ac423
BP
3922 uint64_t odp_actions_stub[1024 / 8];
3923 struct ofpbuf odp_actions;
3924
6814e51f
BP
3925 struct rule_dpif *rule;
3926 struct subfacet *subfacet;
c53e1132 3927 bool may_log = false;
6814e51f
BP
3928 bool ok;
3929
3930 /* Check the rule for consistency. */
c57b2226
BP
3931 rule = rule_dpif_lookup(ofproto, &facet->flow);
3932 ok = rule == facet->rule;
3933 if (!ok) {
c53e1132 3934 may_log = !VLOG_DROP_WARN(&rl);
c53e1132
BP
3935 if (may_log) {
3936 struct ds s;
6814e51f 3937
c53e1132
BP
3938 ds_init(&s);
3939 flow_format(&s, &facet->flow);
3940 ds_put_format(&s, ": facet associated with wrong rule (was "
3941 "table=%"PRIu8",", facet->rule->up.table_id);
3942 cls_rule_format(&facet->rule->up.cr, &s);
3943 ds_put_format(&s, ") (should have been table=%"PRIu8",",
3944 rule->up.table_id);
3945 cls_rule_format(&rule->up.cr, &s);
3946 ds_put_char(&s, ')');
6814e51f 3947
c53e1132
BP
3948 VLOG_WARN("%s", ds_cstr(&s));
3949 ds_destroy(&s);
3950 }
6814e51f
BP
3951 }
3952
3953 /* Check the datapath actions for consistency. */
050ac423 3954 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
6814e51f 3955 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
6a7e895f 3956 enum subfacet_path want_path;
9616614b 3957 struct odputil_keybuf keybuf;
6814e51f 3958 struct action_xlate_ctx ctx;
9616614b
BP
3959 struct ofpbuf key;
3960 struct ds s;
6814e51f
BP
3961
3962 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
0e553d9c 3963 subfacet->initial_tci, rule, 0, NULL);
f25d0cf3 3964 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len,
050ac423 3965 &odp_actions);
6814e51f 3966
6a7e895f
BP
3967 if (subfacet->path == SF_NOT_INSTALLED) {
3968 /* This only happens if the datapath reported an error when we
3969 * tried to install the flow. Don't flag another error here. */
3970 continue;
3971 }
3972
3973 want_path = subfacet_want_path(subfacet->slow);
3974 if (want_path == SF_SLOW_PATH && subfacet->path == SF_SLOW_PATH) {
3975 /* The actions for slow-path flows may legitimately vary from one
3976 * packet to the next. We're done. */
050ac423 3977 continue;
6814e51f
BP
3978 }
3979
6a7e895f 3980 if (!subfacet_should_install(subfacet, subfacet->slow, &odp_actions)) {
9616614b
BP
3981 continue;
3982 }
c53e1132 3983
9616614b
BP
3984 /* Inconsistency! */
3985 if (ok) {
3986 may_log = !VLOG_DROP_WARN(&rl);
3987 ok = false;
3988 }
3989 if (!may_log) {
3990 /* Rate-limited, skip reporting. */
3991 continue;
3992 }
c53e1132 3993
9616614b
BP
3994 ds_init(&s);
3995 subfacet_get_key(subfacet, &keybuf, &key);
3996 odp_flow_key_format(key.data, key.size, &s);
3997
3998 ds_put_cstr(&s, ": inconsistency in subfacet");
6a7e895f 3999 if (want_path != subfacet->path) {
9616614b
BP
4000 enum odp_key_fitness fitness = subfacet->key_fitness;
4001
6a7e895f
BP
4002 ds_put_format(&s, " (%s, fitness=%s)",
4003 subfacet_path_to_string(subfacet->path),
9616614b 4004 odp_key_fitness_to_string(fitness));
6a7e895f
BP
4005 ds_put_format(&s, " (should have been %s)",
4006 subfacet_path_to_string(want_path));
4007 } else if (want_path == SF_FAST_PATH) {
9616614b
BP
4008 ds_put_cstr(&s, " (actions were: ");
4009 format_odp_actions(&s, subfacet->actions,
4010 subfacet->actions_len);
4011 ds_put_cstr(&s, ") (correct actions: ");
4012 format_odp_actions(&s, odp_actions.data, odp_actions.size);
4013 ds_put_char(&s, ')');
4014 } else {
4015 ds_put_cstr(&s, " (actions: ");
4016 format_odp_actions(&s, subfacet->actions,
4017 subfacet->actions_len);
4018 ds_put_char(&s, ')');
6814e51f 4019 }
9616614b
BP
4020 VLOG_WARN("%s", ds_cstr(&s));
4021 ds_destroy(&s);
6814e51f 4022 }
050ac423 4023 ofpbuf_uninit(&odp_actions);
6814e51f
BP
4024
4025 return ok;
4026}
4027
15baa734 4028/* Re-searches the classifier for 'facet':
abe529af
BP
4029 *
4030 * - If the rule found is different from 'facet''s current rule, moves
4031 * 'facet' to the new rule and recompiles its actions.
4032 *
4033 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
c57b2226
BP
4034 * where it is and recompiles its actions anyway. */
4035static void
15baa734 4036facet_revalidate(struct facet *facet)
abe529af 4037{
15baa734 4038 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
b95fc6ba
BP
4039 struct actions {
4040 struct nlattr *odp_actions;
4041 size_t actions_len;
4042 };
4043 struct actions *new_actions;
4044
abe529af 4045 struct action_xlate_ctx ctx;
050ac423
BP
4046 uint64_t odp_actions_stub[1024 / 8];
4047 struct ofpbuf odp_actions;
4048
abe529af 4049 struct rule_dpif *new_rule;
b0f7b9b5 4050 struct subfacet *subfacet;
b95fc6ba 4051 int i;
abe529af
BP
4052
4053 COVERAGE_INC(facet_revalidate);
4054
c57b2226 4055 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
abe529af 4056
df2c07f4 4057 /* Calculate new datapath actions.
abe529af
BP
4058 *
4059 * We do not modify any 'facet' state yet, because we might need to, e.g.,
4060 * emit a NetFlow expiration and, if so, we need to have the old state
4061 * around to properly compose it. */
abe529af 4062
df2c07f4
JP
4063 /* If the datapath actions changed or the installability changed,
4064 * then we need to talk to the datapath. */
b95fc6ba
BP
4065 i = 0;
4066 new_actions = NULL;
4067 memset(&ctx, 0, sizeof ctx);
050ac423 4068 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
b0f7b9b5 4069 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
6a7e895f 4070 enum slow_path_reason slow;
b95fc6ba 4071
e84173dc 4072 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
0e553d9c 4073 subfacet->initial_tci, new_rule, 0, NULL);
f25d0cf3 4074 xlate_actions(&ctx, new_rule->up.ofpacts, new_rule->up.ofpacts_len,
050ac423 4075 &odp_actions);
b0f7b9b5 4076
6a7e895f
BP
4077 slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4078 if (subfacet_should_install(subfacet, slow, &odp_actions)) {
4079 struct dpif_flow_stats stats;
4080
4081 subfacet_install(subfacet,
4082 odp_actions.data, odp_actions.size, &stats, slow);
4083 subfacet_update_stats(subfacet, &stats);
b95fc6ba
BP
4084
4085 if (!new_actions) {
4086 new_actions = xcalloc(list_size(&facet->subfacets),
4087 sizeof *new_actions);
4088 }
050ac423
BP
4089 new_actions[i].odp_actions = xmemdup(odp_actions.data,
4090 odp_actions.size);
4091 new_actions[i].actions_len = odp_actions.size;
abe529af 4092 }
b95fc6ba 4093
b95fc6ba 4094 i++;
b0f7b9b5 4095 }
050ac423
BP
4096 ofpbuf_uninit(&odp_actions);
4097
b95fc6ba 4098 if (new_actions) {
15baa734 4099 facet_flush_stats(facet);
abe529af
BP
4100 }
4101
4102 /* Update 'facet' now that we've taken care of all the old state. */
4103 facet->tags = ctx.tags;
4104 facet->nf_flow.output_iface = ctx.nf_output_iface;
75a75043
BP
4105 facet->has_learn = ctx.has_learn;
4106 facet->has_normal = ctx.has_normal;
0e553d9c 4107 facet->has_fin_timeout = ctx.has_fin_timeout;
9d24de3b 4108 facet->mirrors = ctx.mirrors;
6a7e895f
BP
4109
4110 i = 0;
4111 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4112 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
4113
4114 if (new_actions && new_actions[i].odp_actions) {
4115 free(subfacet->actions);
4116 subfacet->actions = new_actions[i].odp_actions;
4117 subfacet->actions_len = new_actions[i].actions_len;
b95fc6ba 4118 }
6a7e895f 4119 i++;
abe529af 4120 }
6a7e895f
BP
4121 free(new_actions);
4122
abe529af
BP
4123 if (facet->rule != new_rule) {
4124 COVERAGE_INC(facet_changed_rule);
4125 list_remove(&facet->list_node);
4126 list_push_back(&new_rule->facets, &facet->list_node);
4127 facet->rule = new_rule;
4128 facet->used = new_rule->up.created;
9d24de3b 4129 facet->prev_used = facet->used;
abe529af 4130 }
abe529af
BP
4131}
4132
4133/* Updates 'facet''s used time. Caller is responsible for calling
4134 * facet_push_stats() to update the flows which 'facet' resubmits into. */
4135static void
15baa734 4136facet_update_time(struct facet *facet, long long int used)
abe529af 4137{
15baa734 4138 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
abe529af
BP
4139 if (used > facet->used) {
4140 facet->used = used;
1745cd08 4141 ofproto_rule_update_used(&facet->rule->up, used);
abe529af
BP
4142 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
4143 }
4144}
4145
bbb5d219
EJ
4146static void
4147facet_reset_counters(struct facet *facet)
4148{
4149 facet->packet_count = 0;
4150 facet->byte_count = 0;
9d24de3b
JP
4151 facet->prev_packet_count = 0;
4152 facet->prev_byte_count = 0;
bbb5d219
EJ
4153 facet->accounted_bytes = 0;
4154}
4155
abe529af
BP
4156static void
4157facet_push_stats(struct facet *facet)
4158{
112bc5f4 4159 struct dpif_flow_stats stats;
abe529af 4160
9d24de3b
JP
4161 assert(facet->packet_count >= facet->prev_packet_count);
4162 assert(facet->byte_count >= facet->prev_byte_count);
4163 assert(facet->used >= facet->prev_used);
abe529af 4164
112bc5f4
BP
4165 stats.n_packets = facet->packet_count - facet->prev_packet_count;
4166 stats.n_bytes = facet->byte_count - facet->prev_byte_count;
4167 stats.used = facet->used;
4168 stats.tcp_flags = 0;
abe529af 4169
112bc5f4 4170 if (stats.n_packets || stats.n_bytes || facet->used > facet->prev_used) {
9d24de3b
JP
4171 facet->prev_packet_count = facet->packet_count;
4172 facet->prev_byte_count = facet->byte_count;
4173 facet->prev_used = facet->used;
abe529af 4174
112bc5f4 4175 flow_push_stats(facet->rule, &facet->flow, &stats);
9d24de3b
JP
4176
4177 update_mirror_stats(ofproto_dpif_cast(facet->rule->up.ofproto),
112bc5f4 4178 facet->mirrors, stats.n_packets, stats.n_bytes);
abe529af
BP
4179 }
4180}
4181
abe529af 4182static void
112bc5f4 4183rule_credit_stats(struct rule_dpif *rule, const struct dpif_flow_stats *stats)
abe529af 4184{
112bc5f4
BP
4185 rule->packet_count += stats->n_packets;
4186 rule->byte_count += stats->n_bytes;
4187 ofproto_rule_update_used(&rule->up, stats->used);
abe529af
BP
4188}
4189
4190/* Pushes flow statistics to the rules which 'flow' resubmits into given
9d24de3b 4191 * 'rule''s actions and mirrors. */
abe529af 4192static void
18b2a258 4193flow_push_stats(struct rule_dpif *rule,
112bc5f4 4194 const struct flow *flow, const struct dpif_flow_stats *stats)
abe529af
BP
4195{
4196 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
112bc5f4 4197 struct action_xlate_ctx ctx;
abe529af 4198
112bc5f4 4199 ofproto_rule_update_used(&rule->up, stats->used);
f3b50afb 4200
112bc5f4 4201 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, rule,
0e553d9c 4202 0, NULL);
112bc5f4 4203 ctx.resubmit_stats = stats;
f25d0cf3
BP
4204 xlate_actions_for_side_effects(&ctx, rule->up.ofpacts,
4205 rule->up.ofpacts_len);
abe529af
BP
4206}
4207\f
b0f7b9b5
BP
4208/* Subfacets. */
4209
4210static struct subfacet *
4211subfacet_find__(struct ofproto_dpif *ofproto,
4212 const struct nlattr *key, size_t key_len, uint32_t key_hash,
4213 const struct flow *flow)
4214{
4215 struct subfacet *subfacet;
4216
4217 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
4218 &ofproto->subfacets) {
4219 if (subfacet->key
4220 ? (subfacet->key_len == key_len
4221 && !memcmp(key, subfacet->key, key_len))
4222 : flow_equal(flow, &subfacet->facet->flow)) {
4223 return subfacet;
4224 }
4225 }
4226
4227 return NULL;
4228}
4229
4230/* Searches 'facet' (within 'ofproto') for a subfacet with the specified
4231 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
b95fc6ba
BP
4232 * there is one, otherwise creates and returns a new subfacet.
4233 *
4234 * If the returned subfacet is new, then subfacet->actions will be NULL, in
4235 * which case the caller must populate the actions with
4236 * subfacet_make_actions(). */
b0f7b9b5 4237static struct subfacet *
15baa734 4238subfacet_create(struct facet *facet, enum odp_key_fitness key_fitness,
e84173dc 4239 const struct nlattr *key, size_t key_len, ovs_be16 initial_tci)
b0f7b9b5 4240{
15baa734 4241 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
b0f7b9b5
BP
4242 uint32_t key_hash = odp_flow_key_hash(key, key_len);
4243 struct subfacet *subfacet;
4244
4245 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
4246 if (subfacet) {
4247 if (subfacet->facet == facet) {
4248 return subfacet;
4249 }
4250
4251 /* This shouldn't happen. */
4252 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
15baa734 4253 subfacet_destroy(subfacet);
b0f7b9b5
BP
4254 }
4255
26cd7e34
BP
4256 subfacet = (list_is_empty(&facet->subfacets)
4257 ? &facet->one_subfacet
4258 : xmalloc(sizeof *subfacet));
b0f7b9b5
BP
4259 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
4260 list_push_back(&facet->subfacets, &subfacet->list_node);
4261 subfacet->facet = facet;
b0f7b9b5
BP
4262 subfacet->key_fitness = key_fitness;
4263 if (key_fitness != ODP_FIT_PERFECT) {
4264 subfacet->key = xmemdup(key, key_len);
4265 subfacet->key_len = key_len;
26cd7e34
BP
4266 } else {
4267 subfacet->key = NULL;
4268 subfacet->key_len = 0;
b0f7b9b5 4269 }
26cd7e34
BP
4270 subfacet->used = time_msec();
4271 subfacet->dp_packet_count = 0;
4272 subfacet->dp_byte_count = 0;
4273 subfacet->actions_len = 0;
4274 subfacet->actions = NULL;
6a7e895f
BP
4275 subfacet->slow = (subfacet->key_fitness == ODP_FIT_TOO_LITTLE
4276 ? SLOW_MATCH
4277 : 0);
4278 subfacet->path = SF_NOT_INSTALLED;
e84173dc 4279 subfacet->initial_tci = initial_tci;
b0f7b9b5
BP
4280
4281 return subfacet;
4282}
4283
4284/* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
4285 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
4286static struct subfacet *
4287subfacet_find(struct ofproto_dpif *ofproto,
6a542738 4288 const struct nlattr *key, size_t key_len)
b0f7b9b5
BP
4289{
4290 uint32_t key_hash = odp_flow_key_hash(key, key_len);
6a542738
PS
4291 enum odp_key_fitness fitness;
4292 struct flow flow;
4293
4294 fitness = odp_flow_key_to_flow(key, key_len, &flow);
4295 if (fitness == ODP_FIT_ERROR) {
4296 return NULL;
4297 }
b0f7b9b5 4298
6a542738 4299 return subfacet_find__(ofproto, key, key_len, key_hash, &flow);
b0f7b9b5
BP
4300}
4301
4302/* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
4303 * its facet within 'ofproto', and frees it. */
4304static void
15baa734 4305subfacet_destroy__(struct subfacet *subfacet)
b0f7b9b5 4306{
15baa734
BP
4307 struct facet *facet = subfacet->facet;
4308 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4309
4310 subfacet_uninstall(subfacet);
b0f7b9b5
BP
4311 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
4312 list_remove(&subfacet->list_node);
4313 free(subfacet->key);
b95fc6ba 4314 free(subfacet->actions);
26cd7e34
BP
4315 if (subfacet != &facet->one_subfacet) {
4316 free(subfacet);
4317 }
b0f7b9b5
BP
4318}
4319
4320/* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
4321 * last remaining subfacet in its facet destroys the facet too. */
4322static void
15baa734 4323subfacet_destroy(struct subfacet *subfacet)
b0f7b9b5
BP
4324{
4325 struct facet *facet = subfacet->facet;
4326
551a2f6c
BP
4327 if (list_is_singleton(&facet->subfacets)) {
4328 /* facet_remove() needs at least one subfacet (it will remove it). */
15baa734 4329 facet_remove(facet);
551a2f6c 4330 } else {
15baa734 4331 subfacet_destroy__(subfacet);
b0f7b9b5
BP
4332 }
4333}
4334
4335/* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
4336 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
4337 * for use as temporary storage. */
4338static void
4339subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
4340 struct ofpbuf *key)
4341{
4342 if (!subfacet->key) {
4343 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
4344 odp_flow_key_from_flow(key, &subfacet->facet->flow);
4345 } else {
4346 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
4347 }
4348}
4349
5fe20d5d
BP
4350/* Composes the datapath actions for 'subfacet' based on its rule's actions.
4351 * Translates the actions into 'odp_actions', which the caller must have
4352 * initialized and is responsible for uninitializing. */
b95fc6ba 4353static void
5fe20d5d
BP
4354subfacet_make_actions(struct subfacet *subfacet, const struct ofpbuf *packet,
4355 struct ofpbuf *odp_actions)
b95fc6ba
BP
4356{
4357 struct facet *facet = subfacet->facet;
18b2a258 4358 struct rule_dpif *rule = facet->rule;
15baa734 4359 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
050ac423 4360
b95fc6ba
BP
4361 struct action_xlate_ctx ctx;
4362
15baa734 4363 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, subfacet->initial_tci,
0e553d9c 4364 rule, 0, packet);
f25d0cf3 4365 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len, odp_actions);
b95fc6ba 4366 facet->tags = ctx.tags;
b95fc6ba
BP
4367 facet->has_learn = ctx.has_learn;
4368 facet->has_normal = ctx.has_normal;
0e553d9c 4369 facet->has_fin_timeout = ctx.has_fin_timeout;
b95fc6ba 4370 facet->nf_flow.output_iface = ctx.nf_output_iface;
9d24de3b 4371 facet->mirrors = ctx.mirrors;
b95fc6ba 4372
6a7e895f 4373 subfacet->slow = (subfacet->slow & SLOW_MATCH) | ctx.slow;
5fe20d5d
BP
4374 if (subfacet->actions_len != odp_actions->size
4375 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
b95fc6ba 4376 free(subfacet->actions);
5fe20d5d
BP
4377 subfacet->actions_len = odp_actions->size;
4378 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
b95fc6ba 4379 }
b95fc6ba
BP
4380}
4381
b0f7b9b5
BP
4382/* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
4383 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
4384 * in the datapath will be zeroed and 'stats' will be updated with traffic new
4385 * since 'subfacet' was last updated.
4386 *
4387 * Returns 0 if successful, otherwise a positive errno value. */
4388static int
15baa734 4389subfacet_install(struct subfacet *subfacet,
b0f7b9b5 4390 const struct nlattr *actions, size_t actions_len,
6a7e895f
BP
4391 struct dpif_flow_stats *stats,
4392 enum slow_path_reason slow)
b0f7b9b5 4393{
15baa734
BP
4394 struct facet *facet = subfacet->facet;
4395 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
6a7e895f
BP
4396 enum subfacet_path path = subfacet_want_path(slow);
4397 uint64_t slow_path_stub[128 / 8];
b0f7b9b5
BP
4398 struct odputil_keybuf keybuf;
4399 enum dpif_flow_put_flags flags;
4400 struct ofpbuf key;
4401 int ret;
4402
4403 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
4404 if (stats) {
4405 flags |= DPIF_FP_ZERO_STATS;
4406 }
4407
6a7e895f
BP
4408 if (path == SF_SLOW_PATH) {
4409 compose_slow_path(ofproto, &facet->flow, slow,
4410 slow_path_stub, sizeof slow_path_stub,
4411 &actions, &actions_len);
4412 }
4413
b0f7b9b5
BP
4414 subfacet_get_key(subfacet, &keybuf, &key);
4415 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
4416 actions, actions_len, stats);
4417
4418 if (stats) {
4419 subfacet_reset_dp_stats(subfacet, stats);
4420 }
4421
6a7e895f
BP
4422 if (!ret) {
4423 subfacet->path = path;
4424 }
b0f7b9b5
BP
4425 return ret;
4426}
4427
6a7e895f
BP
4428static int
4429subfacet_reinstall(struct subfacet *subfacet, struct dpif_flow_stats *stats)
4430{
4431 return subfacet_install(subfacet, subfacet->actions, subfacet->actions_len,
4432 stats, subfacet->slow);
4433}
4434
b0f7b9b5
BP
4435/* If 'subfacet' is installed in the datapath, uninstalls it. */
4436static void
15baa734 4437subfacet_uninstall(struct subfacet *subfacet)
b0f7b9b5 4438{
6a7e895f 4439 if (subfacet->path != SF_NOT_INSTALLED) {
15baa734
BP
4440 struct rule_dpif *rule = subfacet->facet->rule;
4441 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
b0f7b9b5
BP
4442 struct odputil_keybuf keybuf;
4443 struct dpif_flow_stats stats;
4444 struct ofpbuf key;
4445 int error;
4446
4447 subfacet_get_key(subfacet, &keybuf, &key);
15baa734 4448 error = dpif_flow_del(ofproto->dpif, key.data, key.size, &stats);
b0f7b9b5
BP
4449 subfacet_reset_dp_stats(subfacet, &stats);
4450 if (!error) {
15baa734 4451 subfacet_update_stats(subfacet, &stats);
b0f7b9b5 4452 }
6a7e895f 4453 subfacet->path = SF_NOT_INSTALLED;
b0f7b9b5
BP
4454 } else {
4455 assert(subfacet->dp_packet_count == 0);
4456 assert(subfacet->dp_byte_count == 0);
4457 }
4458}
4459
4460/* Resets 'subfacet''s datapath statistics counters. This should be called
4461 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
4462 * non-null, it should contain the statistics returned by dpif when 'subfacet'
4463 * was reset in the datapath. 'stats' will be modified to include only
4464 * statistics new since 'subfacet' was last updated. */
4465static void
4466subfacet_reset_dp_stats(struct subfacet *subfacet,
4467 struct dpif_flow_stats *stats)
4468{
4469 if (stats
4470 && subfacet->dp_packet_count <= stats->n_packets
4471 && subfacet->dp_byte_count <= stats->n_bytes) {
4472 stats->n_packets -= subfacet->dp_packet_count;
4473 stats->n_bytes -= subfacet->dp_byte_count;
4474 }
4475
4476 subfacet->dp_packet_count = 0;
4477 subfacet->dp_byte_count = 0;
4478}
4479
4480/* Updates 'subfacet''s used time. The caller is responsible for calling
4481 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
4482static void
15baa734 4483subfacet_update_time(struct subfacet *subfacet, long long int used)
b0f7b9b5
BP
4484{
4485 if (used > subfacet->used) {
4486 subfacet->used = used;
15baa734 4487 facet_update_time(subfacet->facet, used);
b0f7b9b5
BP
4488 }
4489}
4490
4491/* Folds the statistics from 'stats' into the counters in 'subfacet'.
4492 *
4493 * Because of the meaning of a subfacet's counters, it only makes sense to do
4494 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
4495 * represents a packet that was sent by hand or if it represents statistics
4496 * that have been cleared out of the datapath. */
4497static void
15baa734 4498subfacet_update_stats(struct subfacet *subfacet,
b0f7b9b5
BP
4499 const struct dpif_flow_stats *stats)
4500{
4501 if (stats->n_packets || stats->used > subfacet->used) {
4502 struct facet *facet = subfacet->facet;
4503
15baa734 4504 subfacet_update_time(subfacet, stats->used);
b0f7b9b5
BP
4505 facet->packet_count += stats->n_packets;
4506 facet->byte_count += stats->n_bytes;
0e553d9c 4507 facet->tcp_flags |= stats->tcp_flags;
b0f7b9b5
BP
4508 facet_push_stats(facet);
4509 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
4510 }
4511}
4512\f
abe529af
BP
4513/* Rules. */
4514
4515static struct rule_dpif *
c57b2226
BP
4516rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
4517{
4518 struct ofport_dpif *port;
4519 struct rule_dpif *rule;
4520
4521 rule = rule_dpif_lookup__(ofproto, flow, 0);
4522 if (rule) {
4523 return rule;
4524 }
4525
4526 port = get_ofp_port(ofproto, flow->in_port);
4527 if (!port) {
4528 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, flow->in_port);
4529 return ofproto->miss_rule;
4530 }
4531
4532 if (port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN) {
4533 return ofproto->no_packet_in_rule;
4534 }
4535 return ofproto->miss_rule;
4536}
4537
4538static struct rule_dpif *
4539rule_dpif_lookup__(struct ofproto_dpif *ofproto, const struct flow *flow,
4540 uint8_t table_id)
abe529af 4541{
7257b535
BP
4542 struct cls_rule *cls_rule;
4543 struct classifier *cls;
4544
9cdaaebe
BP
4545 if (table_id >= N_TABLES) {
4546 return NULL;
4547 }
4548
d0918789 4549 cls = &ofproto->up.tables[table_id].cls;
eadef313 4550 if (flow->nw_frag & FLOW_NW_FRAG_ANY
7257b535
BP
4551 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
4552 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
4553 * are unavailable. */
4554 struct flow ofpc_normal_flow = *flow;
4555 ofpc_normal_flow.tp_src = htons(0);
4556 ofpc_normal_flow.tp_dst = htons(0);
4557 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
4558 } else {
4559 cls_rule = classifier_lookup(cls, flow);
4560 }
4561 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
abe529af
BP
4562}
4563
7ee20df1
BP
4564static void
4565complete_operation(struct rule_dpif *rule)
4566{
4567 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4568
54a9cbc9 4569 rule_invalidate(rule);
7ee20df1
BP
4570 if (clogged) {
4571 struct dpif_completion *c = xmalloc(sizeof *c);
4572 c->op = rule->up.pending;
4573 list_push_back(&ofproto->completions, &c->list_node);
4574 } else {
4575 ofoperation_complete(rule->up.pending, 0);
4576 }
4577}
4578
abe529af
BP
4579static struct rule *
4580rule_alloc(void)
4581{
4582 struct rule_dpif *rule = xmalloc(sizeof *rule);
4583 return &rule->up;
4584}
4585
4586static void
4587rule_dealloc(struct rule *rule_)
4588{
4589 struct rule_dpif *rule = rule_dpif_cast(rule_);
4590 free(rule);
4591}
4592
90bf1e07 4593static enum ofperr
abe529af
BP
4594rule_construct(struct rule *rule_)
4595{
4596 struct rule_dpif *rule = rule_dpif_cast(rule_);
4597 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
7ee20df1 4598 struct rule_dpif *victim;
54a9cbc9 4599 uint8_t table_id;
90bf1e07 4600 enum ofperr error;
5bf0e941 4601
f25d0cf3
BP
4602 error = ofpacts_check(rule->up.ofpacts, rule->up.ofpacts_len,
4603 &rule->up.cr.flow, ofproto->max_ports);
5bf0e941
BP
4604 if (error) {
4605 return error;
4606 }
abe529af 4607
abe529af
BP
4608 rule->packet_count = 0;
4609 rule->byte_count = 0;
abe529af 4610
7ee20df1
BP
4611 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
4612 if (victim && !list_is_empty(&victim->facets)) {
4613 struct facet *facet;
4614
4615 rule->facets = victim->facets;
4616 list_moved(&rule->facets);
4617 LIST_FOR_EACH (facet, list_node, &rule->facets) {
bbb5d219
EJ
4618 /* XXX: We're only clearing our local counters here. It's possible
4619 * that quite a few packets are unaccounted for in the datapath
4620 * statistics. These will be accounted to the new rule instead of
4621 * cleared as required. This could be fixed by clearing out the
4622 * datapath statistics for this facet, but currently it doesn't
4623 * seem worth it. */
4624 facet_reset_counters(facet);
7ee20df1
BP
4625 facet->rule = rule;
4626 }
4627 } else {
4628 /* Must avoid list_moved() in this case. */
4629 list_init(&rule->facets);
4630 }
abe529af 4631
54a9cbc9
BP
4632 table_id = rule->up.table_id;
4633 rule->tag = (victim ? victim->tag
4634 : table_id == 0 ? 0
4635 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
4636 ofproto->tables[table_id].basis));
4637
7ee20df1 4638 complete_operation(rule);
abe529af
BP
4639 return 0;
4640}
4641
4642static void
4643rule_destruct(struct rule *rule_)
4644{
4645 struct rule_dpif *rule = rule_dpif_cast(rule_);
abe529af
BP
4646 struct facet *facet, *next_facet;
4647
abe529af 4648 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
15baa734 4649 facet_revalidate(facet);
abe529af 4650 }
7ee20df1
BP
4651
4652 complete_operation(rule);
abe529af
BP
4653}
4654
4655static void
4656rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
4657{
4658 struct rule_dpif *rule = rule_dpif_cast(rule_);
4659 struct facet *facet;
4660
4661 /* Start from historical data for 'rule' itself that are no longer tracked
4662 * in facets. This counts, for example, facets that have expired. */
4663 *packets = rule->packet_count;
4664 *bytes = rule->byte_count;
4665
4666 /* Add any statistics that are tracked by facets. This includes
4667 * statistical data recently updated by ofproto_update_stats() as well as
4668 * stats for packets that were executed "by hand" via dpif_execute(). */
4669 LIST_FOR_EACH (facet, list_node, &rule->facets) {
4670 *packets += facet->packet_count;
4671 *bytes += facet->byte_count;
4672 }
4673}
4674
90bf1e07 4675static enum ofperr
59d0f2c8
BP
4676rule_execute(struct rule *rule_, const struct flow *flow,
4677 struct ofpbuf *packet)
abe529af
BP
4678{
4679 struct rule_dpif *rule = rule_dpif_cast(rule_);
4680 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
050ac423 4681
112bc5f4 4682 struct dpif_flow_stats stats;
050ac423 4683
abe529af 4684 struct action_xlate_ctx ctx;
050ac423
BP
4685 uint64_t odp_actions_stub[1024 / 8];
4686 struct ofpbuf odp_actions;
abe529af 4687
112bc5f4
BP
4688 dpif_flow_stats_extract(flow, packet, &stats);
4689 rule_credit_stats(rule, &stats);
4690
050ac423 4691 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
54834960 4692 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci,
112bc5f4
BP
4693 rule, stats.tcp_flags, packet);
4694 ctx.resubmit_stats = &stats;
f25d0cf3 4695 xlate_actions(&ctx, rule->up.ofpacts, rule->up.ofpacts_len, &odp_actions);
112bc5f4
BP
4696
4697 execute_odp_actions(ofproto, flow, odp_actions.data,
4698 odp_actions.size, packet);
4699
050ac423 4700 ofpbuf_uninit(&odp_actions);
5bf0e941
BP
4701
4702 return 0;
abe529af
BP
4703}
4704
7ee20df1
BP
4705static void
4706rule_modify_actions(struct rule *rule_)
abe529af
BP
4707{
4708 struct rule_dpif *rule = rule_dpif_cast(rule_);
4709 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
90bf1e07 4710 enum ofperr error;
abe529af 4711
f25d0cf3
BP
4712 error = ofpacts_check(rule->up.ofpacts, rule->up.ofpacts_len,
4713 &rule->up.cr.flow, ofproto->max_ports);
7ee20df1
BP
4714 if (error) {
4715 ofoperation_complete(rule->up.pending, error);
4716 return;
abe529af 4717 }
7ee20df1
BP
4718
4719 complete_operation(rule);
abe529af
BP
4720}
4721\f
97d6520b 4722/* Sends 'packet' out 'ofport'.
52a90c29 4723 * May modify 'packet'.
abe529af
BP
4724 * Returns 0 if successful, otherwise a positive errno value. */
4725static int
52a90c29 4726send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
abe529af 4727{
97d6520b 4728 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
80e5eed9
BP
4729 struct ofpbuf key, odp_actions;
4730 struct odputil_keybuf keybuf;
52a90c29 4731 uint16_t odp_port;
80e5eed9 4732 struct flow flow;
abe529af
BP
4733 int error;
4734
abff858b 4735 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
52a90c29
BP
4736 odp_port = vsp_realdev_to_vlandev(ofproto, ofport->odp_port,
4737 flow.vlan_tci);
4738 if (odp_port != ofport->odp_port) {
4739 eth_pop_vlan(packet);
4740 flow.vlan_tci = htons(0);
4741 }
4742
80e5eed9
BP
4743 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4744 odp_flow_key_from_flow(&key, &flow);
4745
abe529af 4746 ofpbuf_init(&odp_actions, 32);
6ff686f2
PS
4747 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
4748
df2c07f4 4749 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
80e5eed9
BP
4750 error = dpif_execute(ofproto->dpif,
4751 key.data, key.size,
4752 odp_actions.data, odp_actions.size,
abe529af
BP
4753 packet);
4754 ofpbuf_uninit(&odp_actions);
4755
4756 if (error) {
4757 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
4758 ofproto->up.name, odp_port, strerror(error));
4759 }
6527c598 4760 ofproto_update_local_port_stats(ofport->up.ofproto, packet->size, 0);
abe529af
BP
4761 return error;
4762}
4763\f
df2c07f4 4764/* OpenFlow to datapath action translation. */
abe529af 4765
f25d0cf3
BP
4766static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
4767 struct action_xlate_ctx *);
4cd78906 4768static void xlate_normal(struct action_xlate_ctx *);
abe529af 4769
6a7e895f
BP
4770/* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
4771 * The action will state 'slow' as the reason that the action is in the slow
4772 * path. (This is purely informational: it allows a human viewing "ovs-dpctl
4773 * dump-flows" output to see why a flow is in the slow path.)
4774 *
4775 * The 'stub_size' bytes in 'stub' will be used to store the action.
4776 * 'stub_size' must be large enough for the action.
4777 *
4778 * The action and its size will be stored in '*actionsp' and '*actions_lenp',
4779 * respectively. */
4780static void
4781compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
4782 enum slow_path_reason slow,
4783 uint64_t *stub, size_t stub_size,
4784 const struct nlattr **actionsp, size_t *actions_lenp)
4785{
4786 union user_action_cookie cookie;
4787 struct ofpbuf buf;
4788
4789 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
4790 cookie.slow_path.unused = 0;
4791 cookie.slow_path.reason = slow;
4792
4793 ofpbuf_use_stack(&buf, stub, stub_size);
625b0720
BP
4794 if (slow & (SLOW_CFM | SLOW_LACP | SLOW_STP)) {
4795 uint32_t pid = dpif_port_get_pid(ofproto->dpif, UINT16_MAX);
4796 odp_put_userspace_action(pid, &cookie, &buf);
4797 } else {
4798 put_userspace_action(ofproto, &buf, flow, &cookie);
4799 }
6a7e895f
BP
4800 *actionsp = buf.data;
4801 *actions_lenp = buf.size;
4802}
4803
98403001
BP
4804static size_t
4805put_userspace_action(const struct ofproto_dpif *ofproto,
4806 struct ofpbuf *odp_actions,
4807 const struct flow *flow,
1673e0e4 4808 const union user_action_cookie *cookie)
98403001 4809{
98403001
BP
4810 uint32_t pid;
4811
4812 pid = dpif_port_get_pid(ofproto->dpif,
4813 ofp_port_to_odp_port(flow->in_port));
4814
39db78a0 4815 return odp_put_userspace_action(pid, cookie, odp_actions);
98403001
BP
4816}
4817
36fc5f18
BP
4818static void
4819compose_sflow_cookie(const struct ofproto_dpif *ofproto,
4820 ovs_be16 vlan_tci, uint32_t odp_port,
1673e0e4 4821 unsigned int n_outputs, union user_action_cookie *cookie)
36fc5f18
BP
4822{
4823 int ifindex;
4824
4825 cookie->type = USER_ACTION_COOKIE_SFLOW;
1673e0e4 4826 cookie->sflow.vlan_tci = vlan_tci;
36fc5f18
BP
4827
4828 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
4829 * port information") for the interpretation of cookie->output. */
4830 switch (n_outputs) {
4831 case 0:
4832 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1673e0e4 4833 cookie->sflow.output = 0x40000000 | 256;
36fc5f18
BP
4834 break;
4835
4836 case 1:
4837 ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
4838 if (ifindex) {
1673e0e4 4839 cookie->sflow.output = ifindex;
36fc5f18
BP
4840 break;
4841 }
4842 /* Fall through. */
4843 default:
4844 /* 0x80000000 means "multiple output ports. */
1673e0e4 4845 cookie->sflow.output = 0x80000000 | n_outputs;
36fc5f18
BP
4846 break;
4847 }
4848}
4849
6ff686f2
PS
4850/* Compose SAMPLE action for sFlow. */
4851static size_t
4852compose_sflow_action(const struct ofproto_dpif *ofproto,
4853 struct ofpbuf *odp_actions,
4854 const struct flow *flow,
4855 uint32_t odp_port)
4856{
6ff686f2 4857 uint32_t probability;
1673e0e4 4858 union user_action_cookie cookie;
6ff686f2 4859 size_t sample_offset, actions_offset;
36fc5f18 4860 int cookie_offset;
6ff686f2
PS
4861
4862 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
4863 return 0;
4864 }
4865
6ff686f2
PS
4866 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
4867
4868 /* Number of packets out of UINT_MAX to sample. */
4869 probability = dpif_sflow_get_probability(ofproto->sflow);
4870 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
4871
4872 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
36fc5f18
BP
4873 compose_sflow_cookie(ofproto, htons(0), odp_port,
4874 odp_port == OVSP_NONE ? 0 : 1, &cookie);
98403001 4875 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
6ff686f2
PS
4876
4877 nl_msg_end_nested(odp_actions, actions_offset);
4878 nl_msg_end_nested(odp_actions, sample_offset);
98403001 4879 return cookie_offset;
6ff686f2
PS
4880}
4881
4882/* SAMPLE action must be first action in any given list of actions.
4883 * At this point we do not have all information required to build it. So try to
4884 * build sample action as complete as possible. */
4885static void
4886add_sflow_action(struct action_xlate_ctx *ctx)
4887{
4888 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
4889 ctx->odp_actions,
4890 &ctx->flow, OVSP_NONE);
4891 ctx->sflow_odp_port = 0;
4892 ctx->sflow_n_outputs = 0;
4893}
4894
4895/* Fix SAMPLE action according to data collected while composing ODP actions.
4896 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
4897 * USERSPACE action's user-cookie which is required for sflow. */
4898static void
4899fix_sflow_action(struct action_xlate_ctx *ctx)
4900{
4901 const struct flow *base = &ctx->base_flow;
1673e0e4 4902 union user_action_cookie *cookie;
6ff686f2
PS
4903
4904 if (!ctx->user_cookie_offset) {
4905 return;
4906 }
4907
4908 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
36fc5f18 4909 sizeof(*cookie));
6ff686f2
PS
4910 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
4911
36fc5f18
BP
4912 compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
4913 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
6ff686f2
PS
4914}
4915
6ff686f2 4916static void
81b1afb1
EJ
4917compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4918 bool check_stp)
6ff686f2 4919{
d59906fb 4920 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
5e48dc2b 4921 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
52a90c29 4922 ovs_be16 flow_vlan_tci = ctx->flow.vlan_tci;
8b36f51e 4923 uint8_t flow_nw_tos = ctx->flow.nw_tos;
52a90c29 4924 uint16_t out_port;
d59906fb 4925
81b1afb1 4926 if (ofport) {
8b36f51e
EJ
4927 struct priority_to_dscp *pdscp;
4928
9e1fd49b 4929 if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD
81b1afb1
EJ
4930 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4931 return;
4932 }
8b36f51e 4933
deedf7e7 4934 pdscp = get_priority(ofport, ctx->flow.skb_priority);
8b36f51e
EJ
4935 if (pdscp) {
4936 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4937 ctx->flow.nw_tos |= pdscp->dscp;
4938 }
81b1afb1
EJ
4939 } else {
4940 /* We may not have an ofport record for this port, but it doesn't hurt
4941 * to allow forwarding to it anyhow. Maybe such a port will appear
4942 * later and we're pre-populating the flow table. */
d59906fb
EJ
4943 }
4944
52a90c29
BP
4945 out_port = vsp_realdev_to_vlandev(ctx->ofproto, odp_port,
4946 ctx->flow.vlan_tci);
4947 if (out_port != odp_port) {
4948 ctx->flow.vlan_tci = htons(0);
4949 }
5bbda0aa 4950 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
52a90c29
BP
4951 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
4952
6ff686f2
PS
4953 ctx->sflow_odp_port = odp_port;
4954 ctx->sflow_n_outputs++;
81b1afb1 4955 ctx->nf_output_iface = ofp_port;
52a90c29 4956 ctx->flow.vlan_tci = flow_vlan_tci;
8b36f51e 4957 ctx->flow.nw_tos = flow_nw_tos;
6ff686f2
PS
4958}
4959
abe529af 4960static void
5e48dc2b 4961compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
abe529af 4962{
81b1afb1 4963 compose_output_action__(ctx, ofp_port, true);
abe529af
BP
4964}
4965
4966static void
29901626
BP
4967xlate_table_action(struct action_xlate_ctx *ctx,
4968 uint16_t in_port, uint8_t table_id)
abe529af
BP
4969{
4970 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
54a9cbc9 4971 struct ofproto_dpif *ofproto = ctx->ofproto;
abe529af
BP
4972 struct rule_dpif *rule;
4973 uint16_t old_in_port;
29901626
BP
4974 uint8_t old_table_id;
4975
4976 old_table_id = ctx->table_id;
4977 ctx->table_id = table_id;
abe529af 4978
54a9cbc9 4979 /* Look up a flow with 'in_port' as the input port. */
abe529af
BP
4980 old_in_port = ctx->flow.in_port;
4981 ctx->flow.in_port = in_port;
c57b2226 4982 rule = rule_dpif_lookup__(ofproto, &ctx->flow, table_id);
54a9cbc9
BP
4983
4984 /* Tag the flow. */
4985 if (table_id > 0 && table_id < N_TABLES) {
4986 struct table_dpif *table = &ofproto->tables[table_id];
4987 if (table->other_table) {
33780682 4988 ctx->tags |= (rule && rule->tag
54a9cbc9
BP
4989 ? rule->tag
4990 : rule_calculate_tag(&ctx->flow,
4991 &table->other_table->wc,
4992 table->basis));
4993 }
4994 }
4995
4996 /* Restore the original input port. Otherwise OFPP_NORMAL and
4997 * OFPP_IN_PORT will have surprising behavior. */
abe529af
BP
4998 ctx->flow.in_port = old_in_port;
4999
5000 if (ctx->resubmit_hook) {
5001 ctx->resubmit_hook(ctx, rule);
5002 }
5003
5004 if (rule) {
18b2a258 5005 struct rule_dpif *old_rule = ctx->rule;
54834960 5006
112bc5f4
BP
5007 if (ctx->resubmit_stats) {
5008 rule_credit_stats(rule, ctx->resubmit_stats);
5009 }
5010
abe529af 5011 ctx->recurse++;
18b2a258 5012 ctx->rule = rule;
f25d0cf3 5013 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
18b2a258 5014 ctx->rule = old_rule;
abe529af
BP
5015 ctx->recurse--;
5016 }
29901626
BP
5017
5018 ctx->table_id = old_table_id;
abe529af
BP
5019 } else {
5020 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
5021
29901626 5022 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
abe529af 5023 MAX_RESUBMIT_RECURSION);
6a6455e5 5024 ctx->max_resubmit_trigger = true;
abe529af
BP
5025 }
5026}
5027
29901626 5028static void
f25d0cf3
BP
5029xlate_ofpact_resubmit(struct action_xlate_ctx *ctx,
5030 const struct ofpact_resubmit *resubmit)
29901626
BP
5031{
5032 uint16_t in_port;
5033 uint8_t table_id;
5034
f25d0cf3
BP
5035 in_port = resubmit->in_port;
5036 if (in_port == OFPP_IN_PORT) {
5037 in_port = ctx->flow.in_port;
5038 }
5039
5040 table_id = resubmit->table_id;
5041 if (table_id == 255) {
5042 table_id = ctx->table_id;
5043 }
29901626
BP
5044
5045 xlate_table_action(ctx, in_port, table_id);
5046}
5047
abe529af 5048static void
d59906fb 5049flood_packets(struct action_xlate_ctx *ctx, bool all)
abe529af
BP
5050{
5051 struct ofport_dpif *ofport;
5052
b3e9b2ed 5053 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
abe529af 5054 uint16_t ofp_port = ofport->up.ofp_port;
d59906fb
EJ
5055
5056 if (ofp_port == ctx->flow.in_port) {
5057 continue;
5058 }
5059
5e48dc2b 5060 if (all) {
81b1afb1 5061 compose_output_action__(ctx, ofp_port, false);
9e1fd49b 5062 } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
5e48dc2b 5063 compose_output_action(ctx, ofp_port);
abe529af
BP
5064 }
5065 }
b3e9b2ed
EJ
5066
5067 ctx->nf_output_iface = NF_OUT_FLOOD;
abe529af
BP
5068}
5069
6ff686f2 5070static void
f0fd1a17 5071execute_controller_action(struct action_xlate_ctx *ctx, int len,
a7349929
BP
5072 enum ofp_packet_in_reason reason,
5073 uint16_t controller_id)
6ff686f2 5074{
999fba59
EJ
5075 struct ofputil_packet_in pin;
5076 struct ofpbuf *packet;
6ff686f2 5077
6a7e895f 5078 ctx->slow |= SLOW_CONTROLLER;
999fba59
EJ
5079 if (!ctx->packet) {
5080 return;
5081 }
5082
5083 packet = ofpbuf_clone(ctx->packet);
5084
5085 if (packet->l2 && packet->l3) {
5086 struct eth_header *eh;
5087
5088 eth_pop_vlan(packet);
5089 eh = packet->l2;
0104aba8
EJ
5090
5091 /* If the Ethernet type is less than ETH_TYPE_MIN, it's likely an 802.2
5092 * LLC frame. Calculating the Ethernet type of these frames is more
5093 * trouble than seems appropriate for a simple assertion. */
5094 assert(ntohs(eh->eth_type) < ETH_TYPE_MIN
5095 || eh->eth_type == ctx->flow.dl_type);
5096
999fba59
EJ
5097 memcpy(eh->eth_src, ctx->flow.dl_src, sizeof eh->eth_src);
5098 memcpy(eh->eth_dst, ctx->flow.dl_dst, sizeof eh->eth_dst);
5099
5100 if (ctx->flow.vlan_tci & htons(VLAN_CFI)) {
5101 eth_push_vlan(packet, ctx->flow.vlan_tci);
5102 }
5103
5104 if (packet->l4) {
5105 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
5106 packet_set_ipv4(packet, ctx->flow.nw_src, ctx->flow.nw_dst,
5107 ctx->flow.nw_tos, ctx->flow.nw_ttl);
5108 }
5109
5110 if (packet->l7) {
5111 if (ctx->flow.nw_proto == IPPROTO_TCP) {
5112 packet_set_tcp_port(packet, ctx->flow.tp_src,
5113 ctx->flow.tp_dst);
5114 } else if (ctx->flow.nw_proto == IPPROTO_UDP) {
5115 packet_set_udp_port(packet, ctx->flow.tp_src,
5116 ctx->flow.tp_dst);
5117 }
5118 }
5119 }
5120 }
5121
5122 pin.packet = packet->data;
5123 pin.packet_len = packet->size;
f0fd1a17 5124 pin.reason = reason;
a7349929 5125 pin.controller_id = controller_id;
54834960 5126 pin.table_id = ctx->table_id;
18b2a258 5127 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
54834960 5128
999fba59 5129 pin.send_len = len;
999fba59
EJ
5130 flow_get_metadata(&ctx->flow, &pin.fmd);
5131
d8653c38 5132 connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
999fba59 5133 ofpbuf_delete(packet);
6ff686f2
PS
5134}
5135
f0fd1a17
PS
5136static bool
5137compose_dec_ttl(struct action_xlate_ctx *ctx)
5138{
5139 if (ctx->flow.dl_type != htons(ETH_TYPE_IP) &&
5140 ctx->flow.dl_type != htons(ETH_TYPE_IPV6)) {
5141 return false;
5142 }
5143
5144 if (ctx->flow.nw_ttl > 1) {
5145 ctx->flow.nw_ttl--;
5146 return false;
5147 } else {
a7349929 5148 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
f0fd1a17
PS
5149
5150 /* Stop processing for current table. */
5151 return true;
5152 }
5153}
5154
abe529af 5155static void
f25d0cf3
BP
5156xlate_output_action(struct action_xlate_ctx *ctx,
5157 uint16_t port, uint16_t max_len)
abe529af
BP
5158{
5159 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
5160
5161 ctx->nf_output_iface = NF_OUT_DROP;
5162
5163 switch (port) {
5164 case OFPP_IN_PORT:
81b1afb1 5165 compose_output_action(ctx, ctx->flow.in_port);
abe529af
BP
5166 break;
5167 case OFPP_TABLE:
333be161 5168 xlate_table_action(ctx, ctx->flow.in_port, 0);
abe529af
BP
5169 break;
5170 case OFPP_NORMAL:
5171 xlate_normal(ctx);
5172 break;
5173 case OFPP_FLOOD:
d59906fb 5174 flood_packets(ctx, false);
abe529af
BP
5175 break;
5176 case OFPP_ALL:
d59906fb 5177 flood_packets(ctx, true);
abe529af
BP
5178 break;
5179 case OFPP_CONTROLLER:
a7349929 5180 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
abe529af 5181 break;
e81d2933
EJ
5182 case OFPP_NONE:
5183 break;
a0fbe94a 5184 case OFPP_LOCAL:
abe529af
BP
5185 default:
5186 if (port != ctx->flow.in_port) {
81b1afb1 5187 compose_output_action(ctx, port);
abe529af
BP
5188 }
5189 break;
5190 }
5191
5192 if (prev_nf_output_iface == NF_OUT_FLOOD) {
5193 ctx->nf_output_iface = NF_OUT_FLOOD;
5194 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
5195 ctx->nf_output_iface = prev_nf_output_iface;
5196 } else if (prev_nf_output_iface != NF_OUT_DROP &&
5197 ctx->nf_output_iface != NF_OUT_FLOOD) {
5198 ctx->nf_output_iface = NF_OUT_MULTI;
5199 }
5200}
5201
f694937d
EJ
5202static void
5203xlate_output_reg_action(struct action_xlate_ctx *ctx,
f25d0cf3 5204 const struct ofpact_output_reg *or)
f694937d 5205{
f25d0cf3
BP
5206 uint64_t port = mf_get_subfield(&or->src, &ctx->flow);
5207 if (port <= UINT16_MAX) {
5208 xlate_output_action(ctx, port, or->max_len);
f694937d
EJ
5209 }
5210}
5211
abe529af
BP
5212static void
5213xlate_enqueue_action(struct action_xlate_ctx *ctx,
f25d0cf3 5214 const struct ofpact_enqueue *enqueue)
abe529af 5215{
f25d0cf3
BP
5216 uint16_t ofp_port = enqueue->port;
5217 uint32_t queue_id = enqueue->queue;
abff858b 5218 uint32_t flow_priority, priority;
abe529af
BP
5219 int error;
5220
f25d0cf3
BP
5221 /* Translate queue to priority. */
5222 error = dpif_queue_to_priority(ctx->ofproto->dpif, queue_id, &priority);
abe529af
BP
5223 if (error) {
5224 /* Fall back to ordinary output action. */
f25d0cf3 5225 xlate_output_action(ctx, enqueue->port, 0);
abe529af
BP
5226 return;
5227 }
5228
f25d0cf3 5229 /* Check output port. */
abe529af
BP
5230 if (ofp_port == OFPP_IN_PORT) {
5231 ofp_port = ctx->flow.in_port;
8ba855c1
BP
5232 } else if (ofp_port == ctx->flow.in_port) {
5233 return;
abe529af 5234 }
abe529af 5235
df2c07f4 5236 /* Add datapath actions. */
deedf7e7
BP
5237 flow_priority = ctx->flow.skb_priority;
5238 ctx->flow.skb_priority = priority;
81b1afb1 5239 compose_output_action(ctx, ofp_port);
deedf7e7 5240 ctx->flow.skb_priority = flow_priority;
abe529af
BP
5241
5242 /* Update NetFlow output port. */
5243 if (ctx->nf_output_iface == NF_OUT_DROP) {
4b23aebf 5244 ctx->nf_output_iface = ofp_port;
abe529af
BP
5245 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
5246 ctx->nf_output_iface = NF_OUT_MULTI;
5247 }
5248}
5249
5250static void
f25d0cf3 5251xlate_set_queue_action(struct action_xlate_ctx *ctx, uint32_t queue_id)
abe529af 5252{
f25d0cf3 5253 uint32_t skb_priority;
abe529af 5254
f25d0cf3
BP
5255 if (!dpif_queue_to_priority(ctx->ofproto->dpif, queue_id, &skb_priority)) {
5256 ctx->flow.skb_priority = skb_priority;
5257 } else {
5258 /* Couldn't translate queue to a priority. Nothing to do. A warning
abe529af 5259 * has already been logged. */
abe529af 5260 }
abe529af
BP
5261}
5262
5263struct xlate_reg_state {
5264 ovs_be16 vlan_tci;
5265 ovs_be64 tun_id;
5266};
5267
abe529af
BP
5268static void
5269xlate_autopath(struct action_xlate_ctx *ctx,
f25d0cf3 5270 const struct ofpact_autopath *ap)
abe529af 5271{
f25d0cf3 5272 uint16_t ofp_port = ap->port;
abe529af
BP
5273 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
5274
5275 if (!port || !port->bundle) {
5276 ofp_port = OFPP_NONE;
5277 } else if (port->bundle->bond) {
5278 /* Autopath does not support VLAN hashing. */
5279 struct ofport_dpif *slave = bond_choose_output_slave(
dc155bff 5280 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
abe529af
BP
5281 if (slave) {
5282 ofp_port = slave->up.ofp_port;
5283 }
5284 }
f25d0cf3 5285 nxm_reg_load(&ap->dst, ofp_port, &ctx->flow);
abe529af
BP
5286}
5287
daff3353
EJ
5288static bool
5289slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
5290{
5291 struct ofproto_dpif *ofproto = ofproto_;
5292 struct ofport_dpif *port;
5293
5294 switch (ofp_port) {
5295 case OFPP_IN_PORT:
5296 case OFPP_TABLE:
5297 case OFPP_NORMAL:
5298 case OFPP_FLOOD:
5299 case OFPP_ALL:
439e4d8c 5300 case OFPP_NONE:
daff3353
EJ
5301 return true;
5302 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
5303 return false;
5304 default:
5305 port = get_ofp_port(ofproto, ofp_port);
5306 return port ? port->may_enable : false;
5307 }
5308}
5309
f25d0cf3
BP
5310static void
5311xlate_bundle_action(struct action_xlate_ctx *ctx,
5312 const struct ofpact_bundle *bundle)
5313{
5314 uint16_t port;
5315
5316 port = bundle_execute(bundle, &ctx->flow, slave_enabled_cb, ctx->ofproto);
5317 if (bundle->dst.field) {
5318 nxm_reg_load(&bundle->dst, port, &ctx->flow);
5319 } else {
5320 xlate_output_action(ctx, port, 0);
5321 }
5322}
5323
75a75043
BP
5324static void
5325xlate_learn_action(struct action_xlate_ctx *ctx,
f25d0cf3 5326 const struct ofpact_learn *learn)
75a75043
BP
5327{
5328 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
5329 struct ofputil_flow_mod fm;
f25d0cf3
BP
5330 uint64_t ofpacts_stub[1024 / 8];
5331 struct ofpbuf ofpacts;
75a75043
BP
5332 int error;
5333
f25d0cf3
BP
5334 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
5335 learn_execute(learn, &ctx->flow, &fm, &ofpacts);
75a75043
BP
5336
5337 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
5338 if (error && !VLOG_DROP_WARN(&rl)) {
90bf1e07
BP
5339 VLOG_WARN("learning action failed to modify flow table (%s)",
5340 ofperr_get_name(error));
75a75043
BP
5341 }
5342
f25d0cf3 5343 ofpbuf_uninit(&ofpacts);
75a75043
BP
5344}
5345
0e553d9c
BP
5346/* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5347 * means "infinite". */
5348static void
5349reduce_timeout(uint16_t max, uint16_t *timeout)
5350{
5351 if (max && (!*timeout || *timeout > max)) {
5352 *timeout = max;
5353 }
5354}
5355
5356static void
5357xlate_fin_timeout(struct action_xlate_ctx *ctx,
f25d0cf3 5358 const struct ofpact_fin_timeout *oft)
0e553d9c
BP
5359{
5360 if (ctx->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
5361 struct rule_dpif *rule = ctx->rule;
5362
f25d0cf3
BP
5363 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
5364 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
0e553d9c
BP
5365 }
5366}
5367
21f7563c
JP
5368static bool
5369may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
5370{
9e1fd49b
BP
5371 if (port->up.pp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
5372 ? OFPUTIL_PC_NO_RECV_STP
5373 : OFPUTIL_PC_NO_RECV)) {
21f7563c
JP
5374 return false;
5375 }
5376
5377 /* Only drop packets here if both forwarding and learning are
5378 * disabled. If just learning is enabled, we need to have
5379 * OFPP_NORMAL and the learning action have a look at the packet
5380 * before we can drop it. */
5381 if (!stp_forward_in_state(port->stp_state)
5382 && !stp_learn_in_state(port->stp_state)) {
5383 return false;
5384 }
5385
5386 return true;
5387}
5388
abe529af 5389static void
f25d0cf3 5390do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
abe529af
BP
5391 struct action_xlate_ctx *ctx)
5392{
5393 const struct ofport_dpif *port;
254750ce 5394 bool was_evictable = true;
f25d0cf3 5395 const struct ofpact *a;
abe529af
BP
5396
5397 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
21f7563c 5398 if (port && !may_receive(port, ctx)) {
abe529af
BP
5399 /* Drop this flow. */
5400 return;
5401 }
5402
254750ce
BP
5403 if (ctx->rule) {
5404 /* Don't let the rule we're working on get evicted underneath us. */
5405 was_evictable = ctx->rule->up.evictable;
5406 ctx->rule->up.evictable = false;
5407 }
f25d0cf3
BP
5408 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5409 struct ofpact_controller *controller;
38f2e360 5410
848e8809
EJ
5411 if (ctx->exit) {
5412 break;
5413 }
5414
f25d0cf3
BP
5415 switch (a->type) {
5416 case OFPACT_OUTPUT:
5417 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
5418 ofpact_get_OUTPUT(a)->max_len);
5419 break;
5420
5421 case OFPACT_CONTROLLER:
5422 controller = ofpact_get_CONTROLLER(a);
5423 execute_controller_action(ctx, controller->max_len,
5424 controller->reason,
5425 controller->controller_id);
5426 break;
690a61c5 5427
f25d0cf3
BP
5428 case OFPACT_ENQUEUE:
5429 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
abe529af
BP
5430 break;
5431
f25d0cf3 5432 case OFPACT_SET_VLAN_VID:
abe529af 5433 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
f25d0cf3
BP
5434 ctx->flow.vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
5435 | htons(VLAN_CFI));
abe529af
BP
5436 break;
5437
f25d0cf3 5438 case OFPACT_SET_VLAN_PCP:
abe529af 5439 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
f25d0cf3
BP
5440 ctx->flow.vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
5441 << VLAN_PCP_SHIFT)
5442 | VLAN_CFI);
abe529af
BP
5443 break;
5444
f25d0cf3 5445 case OFPACT_STRIP_VLAN:
abe529af 5446 ctx->flow.vlan_tci = htons(0);
abe529af
BP
5447 break;
5448
f25d0cf3
BP
5449 case OFPACT_SET_ETH_SRC:
5450 memcpy(ctx->flow.dl_src, ofpact_get_SET_ETH_SRC(a)->mac,
5451 ETH_ADDR_LEN);
abe529af
BP
5452 break;
5453
f25d0cf3
BP
5454 case OFPACT_SET_ETH_DST:
5455 memcpy(ctx->flow.dl_dst, ofpact_get_SET_ETH_DST(a)->mac,
5456 ETH_ADDR_LEN);
abe529af
BP
5457 break;
5458
f25d0cf3
BP
5459 case OFPACT_SET_IPV4_SRC:
5460 ctx->flow.nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
abe529af
BP
5461 break;
5462
f25d0cf3
BP
5463 case OFPACT_SET_IPV4_DST:
5464 ctx->flow.nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
abe529af
BP
5465 break;
5466
f25d0cf3 5467 case OFPACT_SET_IPV4_DSCP:
c4f2731d
PS
5468 /* OpenFlow 1.0 only supports IPv4. */
5469 if (ctx->flow.dl_type == htons(ETH_TYPE_IP)) {
5470 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
f25d0cf3 5471 ctx->flow.nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
c4f2731d 5472 }
abe529af
BP
5473 break;
5474
f25d0cf3
BP
5475 case OFPACT_SET_L4_SRC_PORT:
5476 ctx->flow.tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
abe529af
BP
5477 break;
5478
f25d0cf3
BP
5479 case OFPACT_SET_L4_DST_PORT:
5480 ctx->flow.tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
abe529af
BP
5481 break;
5482
f25d0cf3
BP
5483 case OFPACT_RESUBMIT:
5484 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
38f2e360
BP
5485 break;
5486
f25d0cf3
BP
5487 case OFPACT_SET_TUNNEL:
5488 ctx->flow.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
29901626
BP
5489 break;
5490
f25d0cf3
BP
5491 case OFPACT_SET_QUEUE:
5492 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
abe529af
BP
5493 break;
5494
f25d0cf3 5495 case OFPACT_POP_QUEUE:
deedf7e7 5496 ctx->flow.skb_priority = ctx->orig_skb_priority;
38f2e360
BP
5497 break;
5498
f25d0cf3
BP
5499 case OFPACT_REG_MOVE:
5500 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), &ctx->flow);
38f2e360
BP
5501 break;
5502
f25d0cf3
BP
5503 case OFPACT_REG_LOAD:
5504 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), &ctx->flow);
38f2e360
BP
5505 break;
5506
f25d0cf3
BP
5507 case OFPACT_DEC_TTL:
5508 if (compose_dec_ttl(ctx)) {
5509 goto out;
5510 }
38f2e360
BP
5511 break;
5512
f25d0cf3
BP
5513 case OFPACT_NOTE:
5514 /* Nothing to do. */
abe529af
BP
5515 break;
5516
f25d0cf3
BP
5517 case OFPACT_MULTIPATH:
5518 multipath_execute(ofpact_get_MULTIPATH(a), &ctx->flow);
abe529af 5519 break;
daff3353 5520
f25d0cf3
BP
5521 case OFPACT_AUTOPATH:
5522 xlate_autopath(ctx, ofpact_get_AUTOPATH(a));
daff3353 5523 break;
a368bb53 5524
f25d0cf3 5525 case OFPACT_BUNDLE:
a368bb53 5526 ctx->ofproto->has_bundle_action = true;
f25d0cf3 5527 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
a368bb53 5528 break;
f694937d 5529
f25d0cf3
BP
5530 case OFPACT_OUTPUT_REG:
5531 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
f694937d 5532 break;
75a75043 5533
f25d0cf3 5534 case OFPACT_LEARN:
75a75043 5535 ctx->has_learn = true;
3de9590b 5536 if (ctx->may_learn) {
f25d0cf3 5537 xlate_learn_action(ctx, ofpact_get_LEARN(a));
75a75043
BP
5538 }
5539 break;
848e8809 5540
f25d0cf3 5541 case OFPACT_EXIT:
848e8809
EJ
5542 ctx->exit = true;
5543 break;
0e553d9c 5544
f25d0cf3 5545 case OFPACT_FIN_TIMEOUT:
0e553d9c 5546 ctx->has_fin_timeout = true;
f25d0cf3 5547 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
a7349929 5548 break;
abe529af
BP
5549 }
5550 }
21f7563c 5551
f0fd1a17 5552out:
21f7563c
JP
5553 /* We've let OFPP_NORMAL and the learning action look at the packet,
5554 * so drop it now if forwarding is disabled. */
5555 if (port && !stp_forward_in_state(port->stp_state)) {
5556 ofpbuf_clear(ctx->odp_actions);
5557 add_sflow_action(ctx);
5558 }
254750ce
BP
5559 if (ctx->rule) {
5560 ctx->rule->up.evictable = was_evictable;
5561 }
abe529af
BP
5562}
5563
5564static void
5565action_xlate_ctx_init(struct action_xlate_ctx *ctx,
5566 struct ofproto_dpif *ofproto, const struct flow *flow,
18b2a258 5567 ovs_be16 initial_tci, struct rule_dpif *rule,
0e553d9c 5568 uint8_t tcp_flags, const struct ofpbuf *packet)
abe529af
BP
5569{
5570 ctx->ofproto = ofproto;
5571 ctx->flow = *flow;
e84173dc
BP
5572 ctx->base_flow = ctx->flow;
5573 ctx->base_flow.tun_id = 0;
5574 ctx->base_flow.vlan_tci = initial_tci;
18b2a258 5575 ctx->rule = rule;
abe529af 5576 ctx->packet = packet;
3de9590b 5577 ctx->may_learn = packet != NULL;
0e553d9c 5578 ctx->tcp_flags = tcp_flags;
abe529af 5579 ctx->resubmit_hook = NULL;
479df176 5580 ctx->report_hook = NULL;
112bc5f4 5581 ctx->resubmit_stats = NULL;
abe529af
BP
5582}
5583
f25d0cf3
BP
5584/* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
5585 * into datapath actions in 'odp_actions', using 'ctx'. */
050ac423 5586static void
abe529af 5587xlate_actions(struct action_xlate_ctx *ctx,
f25d0cf3 5588 const struct ofpact *ofpacts, size_t ofpacts_len,
050ac423 5589 struct ofpbuf *odp_actions)
abe529af 5590{
43d50bc8
BP
5591 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
5592 * that in the future we always keep a copy of the original flow for
5593 * tracing purposes. */
5594 static bool hit_resubmit_limit;
5595
6a7e895f
BP
5596 enum slow_path_reason special;
5597
abe529af
BP
5598 COVERAGE_INC(ofproto_dpif_xlate);
5599
050ac423
BP
5600 ofpbuf_clear(odp_actions);
5601 ofpbuf_reserve(odp_actions, NL_A_U32_SIZE);
5602
5603 ctx->odp_actions = odp_actions;
97e42c92 5604 ctx->tags = 0;
6a7e895f 5605 ctx->slow = 0;
97e42c92
BP
5606 ctx->has_learn = false;
5607 ctx->has_normal = false;
0e553d9c 5608 ctx->has_fin_timeout = false;
97e42c92 5609 ctx->nf_output_iface = NF_OUT_DROP;
9d24de3b 5610 ctx->mirrors = 0;
97e42c92 5611 ctx->recurse = 0;
6a6455e5 5612 ctx->max_resubmit_trigger = false;
deedf7e7 5613 ctx->orig_skb_priority = ctx->flow.skb_priority;
97e42c92 5614 ctx->table_id = 0;
848e8809 5615 ctx->exit = false;
7257b535 5616
43d50bc8 5617 if (ctx->ofproto->has_mirrors || hit_resubmit_limit) {
ccb7c863
BP
5618 /* Do this conditionally because the copy is expensive enough that it
5619 * shows up in profiles.
5620 *
5621 * We keep orig_flow in 'ctx' only because I couldn't make GCC 4.4
5622 * believe that I wasn't using it without initializing it if I kept it
5623 * in a local variable. */
5624 ctx->orig_flow = ctx->flow;
5625 }
5626
eadef313 5627 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
7257b535
BP
5628 switch (ctx->ofproto->up.frag_handling) {
5629 case OFPC_FRAG_NORMAL:
5630 /* We must pretend that transport ports are unavailable. */
97e42c92
BP
5631 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
5632 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
7257b535
BP
5633 break;
5634
5635 case OFPC_FRAG_DROP:
050ac423 5636 return;
7257b535
BP
5637
5638 case OFPC_FRAG_REASM:
5639 NOT_REACHED();
5640
5641 case OFPC_FRAG_NX_MATCH:
5642 /* Nothing to do. */
5643 break;
f0fd1a17
PS
5644
5645 case OFPC_INVALID_TTL_TO_CONTROLLER:
5646 NOT_REACHED();
7257b535
BP
5647 }
5648 }
5649
6a7e895f
BP
5650 special = process_special(ctx->ofproto, &ctx->flow, ctx->packet);
5651 if (special) {
5652 ctx->slow |= special;
abe529af 5653 } else {
6a6455e5 5654 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
6a6455e5
EJ
5655 ovs_be16 initial_tci = ctx->base_flow.vlan_tci;
5656
6ff686f2 5657 add_sflow_action(ctx);
f25d0cf3 5658 do_xlate_actions(ofpacts, ofpacts_len, ctx);
abe529af 5659
43d50bc8
BP
5660 if (ctx->max_resubmit_trigger && !ctx->resubmit_hook) {
5661 if (!hit_resubmit_limit) {
5662 /* We didn't record the original flow. Make sure we do from
5663 * now on. */
5664 hit_resubmit_limit = true;
5665 } else if (!VLOG_DROP_ERR(&trace_rl)) {
5666 struct ds ds = DS_EMPTY_INITIALIZER;
5667
5668 ofproto_trace(ctx->ofproto, &ctx->orig_flow, ctx->packet,
5669 initial_tci, &ds);
5670 VLOG_ERR("Trace triggered by excessive resubmit "
5671 "recursion:\n%s", ds_cstr(&ds));
5672 ds_destroy(&ds);
5673 }
6a6455e5
EJ
5674 }
5675
b6848f13
BP
5676 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
5677 ctx->odp_actions->data,
5678 ctx->odp_actions->size)) {
6a7e895f 5679 ctx->slow |= SLOW_IN_BAND;
b6848f13
BP
5680 if (ctx->packet
5681 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
5682 ctx->packet)) {
5e48dc2b 5683 compose_output_action(ctx, OFPP_LOCAL);
b6848f13
BP
5684 }
5685 }
ccb7c863
BP
5686 if (ctx->ofproto->has_mirrors) {
5687 add_mirror_actions(ctx, &ctx->orig_flow);
5688 }
a7c4eaf6 5689 fix_sflow_action(ctx);
abe529af 5690 }
050ac423
BP
5691}
5692
f25d0cf3
BP
5693/* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
5694 * into datapath actions, using 'ctx', and discards the datapath actions. */
050ac423
BP
5695static void
5696xlate_actions_for_side_effects(struct action_xlate_ctx *ctx,
f25d0cf3
BP
5697 const struct ofpact *ofpacts,
5698 size_t ofpacts_len)
050ac423
BP
5699{
5700 uint64_t odp_actions_stub[1024 / 8];
5701 struct ofpbuf odp_actions;
abe529af 5702
050ac423 5703 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
f25d0cf3 5704 xlate_actions(ctx, ofpacts, ofpacts_len, &odp_actions);
050ac423 5705 ofpbuf_uninit(&odp_actions);
abe529af 5706}
479df176
BP
5707
5708static void
5709xlate_report(struct action_xlate_ctx *ctx, const char *s)
5710{
5711 if (ctx->report_hook) {
5712 ctx->report_hook(ctx, s);
5713 }
5714}
abe529af
BP
5715\f
5716/* OFPP_NORMAL implementation. */
5717
abe529af
BP
5718static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
5719
ecac4ebf
BP
5720/* Given 'vid', the VID obtained from the 802.1Q header that was received as
5721 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
5722 * the bundle on which the packet was received, returns the VLAN to which the
5723 * packet belongs.
5724 *
5725 * Both 'vid' and the return value are in the range 0...4095. */
5726static uint16_t
5727input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
5728{
5729 switch (in_bundle->vlan_mode) {
5730 case PORT_VLAN_ACCESS:
5731 return in_bundle->vlan;
5732 break;
5733
5734 case PORT_VLAN_TRUNK:
5735 return vid;
5736
5737 case PORT_VLAN_NATIVE_UNTAGGED:
5738 case PORT_VLAN_NATIVE_TAGGED:
5739 return vid ? vid : in_bundle->vlan;
5740
5741 default:
5742 NOT_REACHED();
5743 }
5744}
5745
5da5ec37
BP
5746/* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
5747 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
5748 * a warning.
5749 *
5750 * 'vid' should be the VID obtained from the 802.1Q header that was received as
5751 * part of a packet (specify 0 if there was no 802.1Q header), in the range
5752 * 0...4095. */
5753static bool
5754input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
5755{
33158a18
JP
5756 /* Allow any VID on the OFPP_NONE port. */
5757 if (in_bundle == &ofpp_none_bundle) {
5758 return true;
5759 }
5760
5da5ec37
BP
5761 switch (in_bundle->vlan_mode) {
5762 case PORT_VLAN_ACCESS:
5763 if (vid) {
5764 if (warn) {
5765 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5766 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
5767 "packet received on port %s configured as VLAN "
5768 "%"PRIu16" access port",
5769 in_bundle->ofproto->up.name, vid,
5770 in_bundle->name, in_bundle->vlan);
5771 }
5772 return false;
5773 }
5774 return true;
5775
5776 case PORT_VLAN_NATIVE_UNTAGGED:
5777 case PORT_VLAN_NATIVE_TAGGED:
5778 if (!vid) {
5779 /* Port must always carry its native VLAN. */
5780 return true;
5781 }
5782 /* Fall through. */
5783 case PORT_VLAN_TRUNK:
5784 if (!ofbundle_includes_vlan(in_bundle, vid)) {
5785 if (warn) {
5786 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5787 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
5788 "received on port %s not configured for trunking "
5789 "VLAN %"PRIu16,
5790 in_bundle->ofproto->up.name, vid,
5791 in_bundle->name, vid);
5792 }
5793 return false;
5794 }
5795 return true;
5796
5797 default:
5798 NOT_REACHED();
5799 }
5800
5801}
5802
ecac4ebf
BP
5803/* Given 'vlan', the VLAN that a packet belongs to, and
5804 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
5805 * that should be included in the 802.1Q header. (If the return value is 0,
5806 * then the 802.1Q header should only be included in the packet if there is a
5807 * nonzero PCP.)
5808 *
5809 * Both 'vlan' and the return value are in the range 0...4095. */
5810static uint16_t
5811output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
5812{
5813 switch (out_bundle->vlan_mode) {
5814 case PORT_VLAN_ACCESS:
5815 return 0;
5816
5817 case PORT_VLAN_TRUNK:
5818 case PORT_VLAN_NATIVE_TAGGED:
5819 return vlan;
5820
5821 case PORT_VLAN_NATIVE_UNTAGGED:
5822 return vlan == out_bundle->vlan ? 0 : vlan;
5823
5824 default:
5825 NOT_REACHED();
5826 }
5827}
5828
395e68ce
BP
5829static void
5830output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
5831 uint16_t vlan)
abe529af 5832{
395e68ce
BP
5833 struct ofport_dpif *port;
5834 uint16_t vid;
81b1afb1 5835 ovs_be16 tci, old_tci;
ecac4ebf 5836
395e68ce
BP
5837 vid = output_vlan_to_vid(out_bundle, vlan);
5838 if (!out_bundle->bond) {
5839 port = ofbundle_get_a_port(out_bundle);
5840 } else {
5841 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
5842 vid, &ctx->tags);
5843 if (!port) {
5844 /* No slaves enabled, so drop packet. */
5845 return;
5846 }
5847 }
abe529af 5848
81b1afb1 5849 old_tci = ctx->flow.vlan_tci;
5e9ceccd
BP
5850 tci = htons(vid);
5851 if (tci || out_bundle->use_priority_tags) {
5852 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
5853 if (tci) {
5854 tci |= htons(VLAN_CFI);
5855 }
395e68ce 5856 }
81b1afb1 5857 ctx->flow.vlan_tci = tci;
395e68ce 5858
5e48dc2b 5859 compose_output_action(ctx, port->up.ofp_port);
81b1afb1 5860 ctx->flow.vlan_tci = old_tci;
abe529af
BP
5861}
5862
5863static int
5864mirror_mask_ffs(mirror_mask_t mask)
5865{
5866 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
5867 return ffs(mask);
5868}
5869
abe529af
BP
5870static bool
5871ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
5872{
ecac4ebf 5873 return (bundle->vlan_mode != PORT_VLAN_ACCESS
fc3d7408 5874 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
abe529af
BP
5875}
5876
5877static bool
5878ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
5879{
5880 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
5881}
5882
5883/* Returns an arbitrary interface within 'bundle'. */
5884static struct ofport_dpif *
5885ofbundle_get_a_port(const struct ofbundle *bundle)
5886{
5887 return CONTAINER_OF(list_front(&bundle->ports),
5888 struct ofport_dpif, bundle_node);
5889}
5890
abe529af
BP
5891static bool
5892vlan_is_mirrored(const struct ofmirror *m, int vlan)
5893{
fc3d7408 5894 return !m->vlans || bitmap_is_set(m->vlans, vlan);
abe529af
BP
5895}
5896
5897static void
c06bba01 5898add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
abe529af
BP
5899{
5900 struct ofproto_dpif *ofproto = ctx->ofproto;
5901 mirror_mask_t mirrors;
c06bba01
JP
5902 struct ofbundle *in_bundle;
5903 uint16_t vlan;
5904 uint16_t vid;
5905 const struct nlattr *a;
5906 size_t left;
5907
3581c12c 5908 in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
70c2fd56 5909 ctx->packet != NULL, NULL);
3581c12c 5910 if (!in_bundle) {
c06bba01
JP
5911 return;
5912 }
c06bba01
JP
5913 mirrors = in_bundle->src_mirrors;
5914
5915 /* Drop frames on bundles reserved for mirroring. */
5916 if (in_bundle->mirror_out) {
5917 if (ctx->packet != NULL) {
5918 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5919 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5920 "%s, which is reserved exclusively for mirroring",
5921 ctx->ofproto->up.name, in_bundle->name);
5922 }
5923 return;
5924 }
5925
5926 /* Check VLAN. */
5927 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
5928 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5929 return;
5930 }
5931 vlan = input_vid_to_vlan(in_bundle, vid);
5932
5933 /* Look at the output ports to check for destination selections. */
5934
5935 NL_ATTR_FOR_EACH (a, left, ctx->odp_actions->data,
5936 ctx->odp_actions->size) {
5937 enum ovs_action_attr type = nl_attr_type(a);
5938 struct ofport_dpif *ofport;
5939
5940 if (type != OVS_ACTION_ATTR_OUTPUT) {
5941 continue;
5942 }
5943
5944 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
521472bc
BP
5945 if (ofport && ofport->bundle) {
5946 mirrors |= ofport->bundle->dst_mirrors;
5947 }
c06bba01 5948 }
abe529af
BP
5949
5950 if (!mirrors) {
5951 return;
5952 }
5953
c06bba01
JP
5954 /* Restore the original packet before adding the mirror actions. */
5955 ctx->flow = *orig_flow;
5956
9ba15e2a
BP
5957 while (mirrors) {
5958 struct ofmirror *m;
9ba15e2a
BP
5959
5960 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
5961
5962 if (!vlan_is_mirrored(m, vlan)) {
5963 mirrors &= mirrors - 1;
5964 continue;
5965 }
5966
5967 mirrors &= ~m->dup_mirrors;
9d24de3b 5968 ctx->mirrors |= m->dup_mirrors;
9ba15e2a 5969 if (m->out) {
395e68ce 5970 output_normal(ctx, m->out, vlan);
614ec445
EJ
5971 } else if (vlan != m->out_vlan
5972 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
9ba15e2a
BP
5973 struct ofbundle *bundle;
5974
5975 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
5976 if (ofbundle_includes_vlan(bundle, m->out_vlan)
395e68ce
BP
5977 && !bundle->mirror_out) {
5978 output_normal(ctx, bundle, m->out_vlan);
abe529af
BP
5979 }
5980 }
5981 }
abe529af
BP
5982 }
5983}
5984
9d24de3b
JP
5985static void
5986update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
5987 uint64_t packets, uint64_t bytes)
5988{
5989 if (!mirrors) {
5990 return;
5991 }
5992
5993 for (; mirrors; mirrors &= mirrors - 1) {
5994 struct ofmirror *m;
5995
5996 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
5997
5998 if (!m) {
5999 /* In normal circumstances 'm' will not be NULL. However,
6000 * if mirrors are reconfigured, we can temporarily get out
6001 * of sync in facet_revalidate(). We could "correct" the
6002 * mirror list before reaching here, but doing that would
6003 * not properly account the traffic stats we've currently
6004 * accumulated for previous mirror configuration. */
6005 continue;
6006 }
6007
6008 m->packet_count += packets;
6009 m->byte_count += bytes;
6010 }
6011}
6012
abe529af
BP
6013/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
6014 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
6015 * indicate this; newer upstream kernels use gratuitous ARP requests. */
6016static bool
6017is_gratuitous_arp(const struct flow *flow)
6018{
6019 return (flow->dl_type == htons(ETH_TYPE_ARP)
6020 && eth_addr_is_broadcast(flow->dl_dst)
6021 && (flow->nw_proto == ARP_OP_REPLY
6022 || (flow->nw_proto == ARP_OP_REQUEST
6023 && flow->nw_src == flow->nw_dst)));
6024}
6025
6026static void
6027update_learning_table(struct ofproto_dpif *ofproto,
6028 const struct flow *flow, int vlan,
6029 struct ofbundle *in_bundle)
6030{
6031 struct mac_entry *mac;
6032
33158a18
JP
6033 /* Don't learn the OFPP_NONE port. */
6034 if (in_bundle == &ofpp_none_bundle) {
6035 return;
6036 }
6037
abe529af
BP
6038 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
6039 return;
6040 }
6041
6042 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
6043 if (is_gratuitous_arp(flow)) {
6044 /* We don't want to learn from gratuitous ARP packets that are
6045 * reflected back over bond slaves so we lock the learning table. */
6046 if (!in_bundle->bond) {
6047 mac_entry_set_grat_arp_lock(mac);
6048 } else if (mac_entry_is_grat_arp_locked(mac)) {
6049 return;
6050 }
6051 }
6052
6053 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
6054 /* The log messages here could actually be useful in debugging,
6055 * so keep the rate limit relatively high. */
6056 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
6057 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
6058 "on port %s in VLAN %d",
6059 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
6060 in_bundle->name, vlan);
6061
6062 mac->port.p = in_bundle;
6063 tag_set_add(&ofproto->revalidate_set,
6064 mac_learning_changed(ofproto->ml, mac));
6065 }
6066}
6067
3581c12c 6068static struct ofbundle *
4acbc98d
SH
6069lookup_input_bundle(const struct ofproto_dpif *ofproto, uint16_t in_port,
6070 bool warn, struct ofport_dpif **in_ofportp)
395e68ce
BP
6071{
6072 struct ofport_dpif *ofport;
6073
6074 /* Find the port and bundle for the received packet. */
6075 ofport = get_ofp_port(ofproto, in_port);
70c2fd56
BP
6076 if (in_ofportp) {
6077 *in_ofportp = ofport;
6078 }
395e68ce 6079 if (ofport && ofport->bundle) {
3581c12c 6080 return ofport->bundle;
395e68ce
BP
6081 }
6082
70c2fd56
BP
6083 /* Special-case OFPP_NONE, which a controller may use as the ingress
6084 * port for traffic that it is sourcing. */
6085 if (in_port == OFPP_NONE) {
6086 return &ofpp_none_bundle;
6087 }
6088
395e68ce
BP
6089 /* Odd. A few possible reasons here:
6090 *
6091 * - We deleted a port but there are still a few packets queued up
6092 * from it.
6093 *
6094 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
6095 * we don't know about.
6096 *
6097 * - The ofproto client didn't configure the port as part of a bundle.
6b803ddc
EJ
6098 * This is particularly likely to happen if a packet was received on the
6099 * port after it was created, but before the client had a chance to
6100 * configure its bundle.
395e68ce
BP
6101 */
6102 if (warn) {
6103 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6104
6105 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
6106 "port %"PRIu16, ofproto->up.name, in_port);
6107 }
6108 return NULL;
6109}
6110
5da5ec37 6111/* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
abe529af
BP
6112 * dropped. Returns true if they may be forwarded, false if they should be
6113 * dropped.
6114 *
395e68ce
BP
6115 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
6116 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
abe529af 6117 *
395e68ce
BP
6118 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
6119 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
6120 * checked by input_vid_is_valid().
abe529af
BP
6121 *
6122 * May also add tags to '*tags', although the current implementation only does
6123 * so in one special case.
6124 */
6125static bool
479df176
BP
6126is_admissible(struct action_xlate_ctx *ctx, struct ofport_dpif *in_port,
6127 uint16_t vlan)
abe529af 6128{
479df176
BP
6129 struct ofproto_dpif *ofproto = ctx->ofproto;
6130 struct flow *flow = &ctx->flow;
395e68ce 6131 struct ofbundle *in_bundle = in_port->bundle;
abe529af 6132
395e68ce
BP
6133 /* Drop frames for reserved multicast addresses
6134 * only if forward_bpdu option is absent. */
614ec445 6135 if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
479df176 6136 xlate_report(ctx, "packet has reserved destination MAC, dropping");
abe529af
BP
6137 return false;
6138 }
6139
abe529af
BP
6140 if (in_bundle->bond) {
6141 struct mac_entry *mac;
6142
6143 switch (bond_check_admissibility(in_bundle->bond, in_port,
479df176 6144 flow->dl_dst, &ctx->tags)) {
abe529af
BP
6145 case BV_ACCEPT:
6146 break;
6147
6148 case BV_DROP:
479df176 6149 xlate_report(ctx, "bonding refused admissibility, dropping");
abe529af
BP
6150 return false;
6151
6152 case BV_DROP_IF_MOVED:
6153 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
6154 if (mac && mac->port.p != in_bundle &&
6155 (!is_gratuitous_arp(flow)
6156 || mac_entry_is_grat_arp_locked(mac))) {
479df176
BP
6157 xlate_report(ctx, "SLB bond thinks this packet looped back, "
6158 "dropping");
abe529af
BP
6159 return false;
6160 }
6161 break;
6162 }
6163 }
6164
6165 return true;
6166}
6167
4cd78906 6168static void
abe529af
BP
6169xlate_normal(struct action_xlate_ctx *ctx)
6170{
395e68ce 6171 struct ofport_dpif *in_port;
abe529af 6172 struct ofbundle *in_bundle;
abe529af 6173 struct mac_entry *mac;
395e68ce
BP
6174 uint16_t vlan;
6175 uint16_t vid;
abe529af 6176
75a75043
BP
6177 ctx->has_normal = true;
6178
3581c12c 6179 in_bundle = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
70c2fd56 6180 ctx->packet != NULL, &in_port);
3581c12c 6181 if (!in_bundle) {
479df176 6182 xlate_report(ctx, "no input bundle, dropping");
395e68ce
BP
6183 return;
6184 }
3581c12c 6185
395e68ce
BP
6186 /* Drop malformed frames. */
6187 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
6188 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
6189 if (ctx->packet != NULL) {
6190 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6191 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
6192 "VLAN tag received on port %s",
6193 ctx->ofproto->up.name, in_bundle->name);
6194 }
479df176 6195 xlate_report(ctx, "partial VLAN tag, dropping");
395e68ce
BP
6196 return;
6197 }
6198
6199 /* Drop frames on bundles reserved for mirroring. */
6200 if (in_bundle->mirror_out) {
6201 if (ctx->packet != NULL) {
6202 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6203 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
6204 "%s, which is reserved exclusively for mirroring",
6205 ctx->ofproto->up.name, in_bundle->name);
6206 }
479df176 6207 xlate_report(ctx, "input port is mirror output port, dropping");
395e68ce
BP
6208 return;
6209 }
6210
6211 /* Check VLAN. */
6212 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
6213 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
479df176 6214 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
395e68ce
BP
6215 return;
6216 }
6217 vlan = input_vid_to_vlan(in_bundle, vid);
6218
6219 /* Check other admissibility requirements. */
479df176 6220 if (in_port && !is_admissible(ctx, in_port, vlan)) {
395e68ce 6221 return;
abe529af
BP
6222 }
6223
75a75043 6224 /* Learn source MAC. */
3de9590b 6225 if (ctx->may_learn) {
abe529af
BP
6226 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
6227 }
6228
6229 /* Determine output bundle. */
6230 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
6231 &ctx->tags);
6232 if (mac) {
c06bba01 6233 if (mac->port.p != in_bundle) {
479df176 6234 xlate_report(ctx, "forwarding to learned port");
c06bba01 6235 output_normal(ctx, mac->port.p, vlan);
479df176
BP
6236 } else {
6237 xlate_report(ctx, "learned port is input port, dropping");
c06bba01 6238 }
abe529af 6239 } else {
c06bba01 6240 struct ofbundle *bundle;
abe529af 6241
479df176 6242 xlate_report(ctx, "no learned MAC for destination, flooding");
c06bba01
JP
6243 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
6244 if (bundle != in_bundle
6245 && ofbundle_includes_vlan(bundle, vlan)
6246 && bundle->floodable
6247 && !bundle->mirror_out) {
6248 output_normal(ctx, bundle, vlan);
6249 }
6250 }
6251 ctx->nf_output_iface = NF_OUT_FLOOD;
abe529af 6252 }
abe529af
BP
6253}
6254\f
54a9cbc9
BP
6255/* Optimized flow revalidation.
6256 *
6257 * It's a difficult problem, in general, to tell which facets need to have
6258 * their actions recalculated whenever the OpenFlow flow table changes. We
6259 * don't try to solve that general problem: for most kinds of OpenFlow flow
6260 * table changes, we recalculate the actions for every facet. This is
6261 * relatively expensive, but it's good enough if the OpenFlow flow table
6262 * doesn't change very often.
6263 *
6264 * However, we can expect one particular kind of OpenFlow flow table change to
6265 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
6266 * of CPU on revalidating every facet whenever MAC learning modifies the flow
6267 * table, we add a special case that applies to flow tables in which every rule
6268 * has the same form (that is, the same wildcards), except that the table is
6269 * also allowed to have a single "catch-all" flow that matches all packets. We
6270 * optimize this case by tagging all of the facets that resubmit into the table
6271 * and invalidating the same tag whenever a flow changes in that table. The
6272 * end result is that we revalidate just the facets that need it (and sometimes
6273 * a few more, but not all of the facets or even all of the facets that
6274 * resubmit to the table modified by MAC learning). */
6275
6276/* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
6277 * into an OpenFlow table with the given 'basis'. */
822d9414 6278static tag_type
54a9cbc9
BP
6279rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
6280 uint32_t secret)
6281{
6282 if (flow_wildcards_is_catchall(wc)) {
6283 return 0;
6284 } else {
6285 struct flow tag_flow = *flow;
6286 flow_zero_wildcards(&tag_flow, wc);
6287 return tag_create_deterministic(flow_hash(&tag_flow, secret));
6288 }
6289}
6290
6291/* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
6292 * taggability of that table.
6293 *
6294 * This function must be called after *each* change to a flow table. If you
6295 * skip calling it on some changes then the pointer comparisons at the end can
6296 * be invalid if you get unlucky. For example, if a flow removal causes a
6297 * cls_table to be destroyed and then a flow insertion causes a cls_table with
6298 * different wildcards to be created with the same address, then this function
6299 * will incorrectly skip revalidation. */
6300static void
6301table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
6302{
6303 struct table_dpif *table = &ofproto->tables[table_id];
d0918789 6304 const struct oftable *oftable = &ofproto->up.tables[table_id];
54a9cbc9
BP
6305 struct cls_table *catchall, *other;
6306 struct cls_table *t;
6307
6308 catchall = other = NULL;
6309
d0918789 6310 switch (hmap_count(&oftable->cls.tables)) {
54a9cbc9
BP
6311 case 0:
6312 /* We could tag this OpenFlow table but it would make the logic a
6313 * little harder and it's a corner case that doesn't seem worth it
6314 * yet. */
6315 break;
6316
6317 case 1:
6318 case 2:
d0918789 6319 HMAP_FOR_EACH (t, hmap_node, &oftable->cls.tables) {
54a9cbc9
BP
6320 if (cls_table_is_catchall(t)) {
6321 catchall = t;
6322 } else if (!other) {
6323 other = t;
6324 } else {
6325 /* Indicate that we can't tag this by setting both tables to
6326 * NULL. (We know that 'catchall' is already NULL.) */
6327 other = NULL;
6328 }
6329 }
6330 break;
6331
6332 default:
6333 /* Can't tag this table. */
6334 break;
6335 }
6336
6337 if (table->catchall_table != catchall || table->other_table != other) {
6338 table->catchall_table = catchall;
6339 table->other_table = other;
3c4a309c 6340 ofproto->need_revalidate = REV_FLOW_TABLE;
54a9cbc9
BP
6341 }
6342}
6343
6344/* Given 'rule' that has changed in some way (either it is a rule being
6345 * inserted, a rule being deleted, or a rule whose actions are being
6346 * modified), marks facets for revalidation to ensure that packets will be
6347 * forwarded correctly according to the new state of the flow table.
6348 *
6349 * This function must be called after *each* change to a flow table. See
6350 * the comment on table_update_taggable() for more information. */
6351static void
6352rule_invalidate(const struct rule_dpif *rule)
6353{
6354 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
6355
6356 table_update_taggable(ofproto, rule->up.table_id);
6357
6358 if (!ofproto->need_revalidate) {
6359 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
6360
6361 if (table->other_table && rule->tag) {
6362 tag_set_add(&ofproto->revalidate_set, rule->tag);
6363 } else {
3c4a309c 6364 ofproto->need_revalidate = REV_FLOW_TABLE;
54a9cbc9
BP
6365 }
6366 }
6367}
6368\f
abe529af 6369static bool
7257b535
BP
6370set_frag_handling(struct ofproto *ofproto_,
6371 enum ofp_config_flags frag_handling)
abe529af
BP
6372{
6373 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 6374
7257b535 6375 if (frag_handling != OFPC_FRAG_REASM) {
3c4a309c 6376 ofproto->need_revalidate = REV_RECONFIGURE;
7257b535
BP
6377 return true;
6378 } else {
6379 return false;
6380 }
abe529af
BP
6381}
6382
90bf1e07 6383static enum ofperr
abe529af
BP
6384packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
6385 const struct flow *flow,
f25d0cf3 6386 const struct ofpact *ofpacts, size_t ofpacts_len)
abe529af
BP
6387{
6388 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
90bf1e07 6389 enum ofperr error;
abe529af 6390
e1154f71 6391 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
90bf1e07 6392 return OFPERR_NXBRC_BAD_IN_PORT;
e1154f71
BP
6393 }
6394
f25d0cf3 6395 error = ofpacts_check(ofpacts, ofpacts_len, flow, ofproto->max_ports);
abe529af 6396 if (!error) {
80e5eed9 6397 struct odputil_keybuf keybuf;
112bc5f4
BP
6398 struct dpif_flow_stats stats;
6399
80e5eed9
BP
6400 struct ofpbuf key;
6401
112bc5f4 6402 struct action_xlate_ctx ctx;
050ac423
BP
6403 uint64_t odp_actions_stub[1024 / 8];
6404 struct ofpbuf odp_actions;
050ac423 6405
80e5eed9
BP
6406 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
6407 odp_flow_key_from_flow(&key, flow);
abe529af 6408
112bc5f4 6409 dpif_flow_stats_extract(flow, packet, &stats);
2284188b 6410
112bc5f4
BP
6411 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, NULL,
6412 packet_get_tcp_flags(packet, flow), packet);
6413 ctx.resubmit_stats = &stats;
2284188b 6414
050ac423
BP
6415 ofpbuf_use_stub(&odp_actions,
6416 odp_actions_stub, sizeof odp_actions_stub);
f25d0cf3 6417 xlate_actions(&ctx, ofpacts, ofpacts_len, &odp_actions);
80e5eed9 6418 dpif_execute(ofproto->dpif, key.data, key.size,
050ac423
BP
6419 odp_actions.data, odp_actions.size, packet);
6420 ofpbuf_uninit(&odp_actions);
abe529af
BP
6421 }
6422 return error;
6423}
6fca1ffb
BP
6424\f
6425/* NetFlow. */
6426
6427static int
6428set_netflow(struct ofproto *ofproto_,
6429 const struct netflow_options *netflow_options)
6430{
6431 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6432
6433 if (netflow_options) {
6434 if (!ofproto->netflow) {
6435 ofproto->netflow = netflow_create();
6436 }
6437 return netflow_set_options(ofproto->netflow, netflow_options);
6438 } else {
6439 netflow_destroy(ofproto->netflow);
6440 ofproto->netflow = NULL;
6441 return 0;
6442 }
6443}
abe529af
BP
6444
6445static void
6446get_netflow_ids(const struct ofproto *ofproto_,
6447 uint8_t *engine_type, uint8_t *engine_id)
6448{
6449 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
6450
6451 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
6452}
6fca1ffb
BP
6453
6454static void
6455send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
6456{
6457 if (!facet_is_controller_flow(facet) &&
6458 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
b0f7b9b5 6459 struct subfacet *subfacet;
6fca1ffb
BP
6460 struct ofexpired expired;
6461
b0f7b9b5 6462 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
6a7e895f 6463 if (subfacet->path == SF_FAST_PATH) {
b0f7b9b5 6464 struct dpif_flow_stats stats;
6fca1ffb 6465
6a7e895f 6466 subfacet_reinstall(subfacet, &stats);
15baa734 6467 subfacet_update_stats(subfacet, &stats);
b0f7b9b5 6468 }
6fca1ffb
BP
6469 }
6470
6471 expired.flow = facet->flow;
6472 expired.packet_count = facet->packet_count;
6473 expired.byte_count = facet->byte_count;
6474 expired.used = facet->used;
6475 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
6476 }
6477}
6478
6479static void
6480send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
6481{
6482 struct facet *facet;
6483
6484 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
6485 send_active_timeout(ofproto, facet);
6486 }
6487}
abe529af
BP
6488\f
6489static struct ofproto_dpif *
6490ofproto_dpif_lookup(const char *name)
6491{
b44a10b7
BP
6492 struct ofproto_dpif *ofproto;
6493
6494 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
6495 hash_string(name, 0), &all_ofproto_dpifs) {
6496 if (!strcmp(ofproto->up.name, name)) {
6497 return ofproto;
6498 }
6499 }
6500 return NULL;
abe529af
BP
6501}
6502
f0a3aa2e 6503static void
96e466a3 6504ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
0e15264f 6505 const char *argv[], void *aux OVS_UNUSED)
f0a3aa2e 6506{
490df1ef 6507 struct ofproto_dpif *ofproto;
f0a3aa2e 6508
96e466a3
EJ
6509 if (argc > 1) {
6510 ofproto = ofproto_dpif_lookup(argv[1]);
6511 if (!ofproto) {
bde9f75d 6512 unixctl_command_reply_error(conn, "no such bridge");
96e466a3
EJ
6513 return;
6514 }
d0040604 6515 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
96e466a3
EJ
6516 } else {
6517 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
d0040604 6518 mac_learning_flush(ofproto->ml, &ofproto->revalidate_set);
96e466a3 6519 }
f0a3aa2e 6520 }
f0a3aa2e 6521
bde9f75d 6522 unixctl_command_reply(conn, "table successfully flushed");
f0a3aa2e
AA
6523}
6524
abe529af 6525static void
0e15264f
BP
6526ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
6527 const char *argv[], void *aux OVS_UNUSED)
abe529af
BP
6528{
6529 struct ds ds = DS_EMPTY_INITIALIZER;
6530 const struct ofproto_dpif *ofproto;
6531 const struct mac_entry *e;
6532
0e15264f 6533 ofproto = ofproto_dpif_lookup(argv[1]);
abe529af 6534 if (!ofproto) {
bde9f75d 6535 unixctl_command_reply_error(conn, "no such bridge");
abe529af
BP
6536 return;
6537 }
6538
6539 ds_put_cstr(&ds, " port VLAN MAC Age\n");
6540 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
6541 struct ofbundle *bundle = e->port.p;
6542 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
6543 ofbundle_get_a_port(bundle)->odp_port,
e764773c
BP
6544 e->vlan, ETH_ADDR_ARGS(e->mac),
6545 mac_entry_age(ofproto->ml, e));
abe529af 6546 }
bde9f75d 6547 unixctl_command_reply(conn, ds_cstr(&ds));
abe529af
BP
6548 ds_destroy(&ds);
6549}
6550
6a6455e5 6551struct trace_ctx {
abe529af
BP
6552 struct action_xlate_ctx ctx;
6553 struct flow flow;
6554 struct ds *result;
6555};
6556
6557static void
29901626
BP
6558trace_format_rule(struct ds *result, uint8_t table_id, int level,
6559 const struct rule_dpif *rule)
abe529af
BP
6560{
6561 ds_put_char_multiple(result, '\t', level);
6562 if (!rule) {
6563 ds_put_cstr(result, "No match\n");
6564 return;
6565 }
6566
29901626
BP
6567 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
6568 table_id, ntohll(rule->up.flow_cookie));
79feb7df 6569 cls_rule_format(&rule->up.cr, result);
abe529af
BP
6570 ds_put_char(result, '\n');
6571
6572 ds_put_char_multiple(result, '\t', level);
6573 ds_put_cstr(result, "OpenFlow ");
f25d0cf3 6574 ofpacts_format(rule->up.ofpacts, rule->up.ofpacts_len, result);
abe529af
BP
6575 ds_put_char(result, '\n');
6576}
6577
6578static void
6579trace_format_flow(struct ds *result, int level, const char *title,
6a6455e5 6580 struct trace_ctx *trace)
abe529af
BP
6581{
6582 ds_put_char_multiple(result, '\t', level);
6583 ds_put_format(result, "%s: ", title);
6584 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
6585 ds_put_cstr(result, "unchanged");
6586 } else {
6587 flow_format(result, &trace->ctx.flow);
6588 trace->flow = trace->ctx.flow;
6589 }
6590 ds_put_char(result, '\n');
6591}
6592
eb9e1c26
EJ
6593static void
6594trace_format_regs(struct ds *result, int level, const char *title,
6a6455e5 6595 struct trace_ctx *trace)
eb9e1c26
EJ
6596{
6597 size_t i;
6598
6599 ds_put_char_multiple(result, '\t', level);
6600 ds_put_format(result, "%s:", title);
6601 for (i = 0; i < FLOW_N_REGS; i++) {
6602 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
6603 }
6604 ds_put_char(result, '\n');
6605}
6606
1ed8d352
EJ
6607static void
6608trace_format_odp(struct ds *result, int level, const char *title,
6a6455e5 6609 struct trace_ctx *trace)
1ed8d352
EJ
6610{
6611 struct ofpbuf *odp_actions = trace->ctx.odp_actions;
6612
6613 ds_put_char_multiple(result, '\t', level);
6614 ds_put_format(result, "%s: ", title);
6615 format_odp_actions(result, odp_actions->data, odp_actions->size);
6616 ds_put_char(result, '\n');
6617}
6618
abe529af
BP
6619static void
6620trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
6621{
6a6455e5 6622 struct trace_ctx *trace = CONTAINER_OF(ctx, struct trace_ctx, ctx);
abe529af
BP
6623 struct ds *result = trace->result;
6624
6625 ds_put_char(result, '\n');
6626 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
eb9e1c26 6627 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
1ed8d352 6628 trace_format_odp(result, ctx->recurse + 1, "Resubmitted odp", trace);
29901626 6629 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
abe529af
BP
6630}
6631
479df176
BP
6632static void
6633trace_report(struct action_xlate_ctx *ctx, const char *s)
6634{
6635 struct trace_ctx *trace = CONTAINER_OF(ctx, struct trace_ctx, ctx);
6636 struct ds *result = trace->result;
6637
6638 ds_put_char_multiple(result, '\t', ctx->recurse);
6639 ds_put_cstr(result, s);
6640 ds_put_char(result, '\n');
6641}
6642
abe529af 6643static void
0e15264f 6644ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
abe529af
BP
6645 void *aux OVS_UNUSED)
6646{
0e15264f 6647 const char *dpname = argv[1];
abe529af 6648 struct ofproto_dpif *ofproto;
876b0e1c
BP
6649 struct ofpbuf odp_key;
6650 struct ofpbuf *packet;
e84173dc 6651 ovs_be16 initial_tci;
abe529af
BP
6652 struct ds result;
6653 struct flow flow;
abe529af
BP
6654 char *s;
6655
876b0e1c
BP
6656 packet = NULL;
6657 ofpbuf_init(&odp_key, 0);
abe529af
BP
6658 ds_init(&result);
6659
e84173dc
BP
6660 ofproto = ofproto_dpif_lookup(dpname);
6661 if (!ofproto) {
bde9f75d
EJ
6662 unixctl_command_reply_error(conn, "Unknown ofproto (use ofproto/list "
6663 "for help)");
e84173dc
BP
6664 goto exit;
6665 }
0e15264f 6666 if (argc == 3 || (argc == 4 && !strcmp(argv[3], "-generate"))) {
8b3b8dd1 6667 /* ofproto/trace dpname flow [-generate] */
0e15264f
BP
6668 const char *flow_s = argv[2];
6669 const char *generate_s = argv[3];
876b0e1c 6670
31a19d69
BP
6671 /* Allow 'flow_s' to be either a datapath flow or an OpenFlow-like
6672 * flow. We guess which type it is based on whether 'flow_s' contains
6673 * an '(', since a datapath flow always contains '(') but an
6674 * OpenFlow-like flow should not (in fact it's allowed but I believe
6675 * that's not documented anywhere).
6676 *
6677 * An alternative would be to try to parse 'flow_s' both ways, but then
6678 * it would be tricky giving a sensible error message. After all, do
6679 * you just say "syntax error" or do you present both error messages?
6680 * Both choices seem lousy. */
6681 if (strchr(flow_s, '(')) {
6682 int error;
6683
6684 /* Convert string to datapath key. */
6685 ofpbuf_init(&odp_key, 0);
6686 error = odp_flow_key_from_string(flow_s, NULL, &odp_key);
6687 if (error) {
6688 unixctl_command_reply_error(conn, "Bad flow syntax");
6689 goto exit;
6690 }
876b0e1c 6691
31a19d69
BP
6692 /* Convert odp_key to flow. */
6693 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
6694 odp_key.size, &flow,
6695 &initial_tci, NULL);
6696 if (error == ODP_FIT_ERROR) {
6697 unixctl_command_reply_error(conn, "Invalid flow");
6698 goto exit;
6699 }
6700 } else {
6701 char *error_s;
6702
6703 error_s = parse_ofp_exact_flow(&flow, argv[2]);
6704 if (error_s) {
6705 unixctl_command_reply_error(conn, error_s);
6706 free(error_s);
6707 goto exit;
6708 }
6709
6710 initial_tci = flow.vlan_tci;
6711 vsp_adjust_flow(ofproto, &flow);
876b0e1c 6712 }
8b3b8dd1
BP
6713
6714 /* Generate a packet, if requested. */
0e15264f 6715 if (generate_s) {
8b3b8dd1
BP
6716 packet = ofpbuf_new(0);
6717 flow_compose(packet, &flow);
6718 }
0e15264f 6719 } else if (argc == 6) {
abff858b 6720 /* ofproto/trace dpname priority tun_id in_port packet */
0e15264f
BP
6721 const char *priority_s = argv[2];
6722 const char *tun_id_s = argv[3];
6723 const char *in_port_s = argv[4];
6724 const char *packet_s = argv[5];
6725 uint16_t in_port = ofp_port_to_odp_port(atoi(in_port_s));
6726 ovs_be64 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
6727 uint32_t priority = atoi(priority_s);
e22f1753 6728 const char *msg;
0e15264f 6729
e22f1753
BP
6730 msg = eth_from_hex(packet_s, &packet);
6731 if (msg) {
bde9f75d 6732 unixctl_command_reply_error(conn, msg);
876b0e1c
BP
6733 goto exit;
6734 }
6735
6736 ds_put_cstr(&result, "Packet: ");
c499c75d 6737 s = ofp_packet_to_string(packet->data, packet->size);
876b0e1c
BP
6738 ds_put_cstr(&result, s);
6739 free(s);
6740
abff858b 6741 flow_extract(packet, priority, tun_id, in_port, &flow);
e84173dc 6742 initial_tci = flow.vlan_tci;
876b0e1c 6743 } else {
bde9f75d 6744 unixctl_command_reply_error(conn, "Bad command syntax");
abe529af
BP
6745 goto exit;
6746 }
6747
6a6455e5
EJ
6748 ofproto_trace(ofproto, &flow, packet, initial_tci, &result);
6749 unixctl_command_reply(conn, ds_cstr(&result));
6750
6751exit:
6752 ds_destroy(&result);
6753 ofpbuf_delete(packet);
6754 ofpbuf_uninit(&odp_key);
6755}
6756
6757static void
6758ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
6759 const struct ofpbuf *packet, ovs_be16 initial_tci,
6760 struct ds *ds)
6761{
6762 struct rule_dpif *rule;
6763
6764 ds_put_cstr(ds, "Flow: ");
6765 flow_format(ds, flow);
6766 ds_put_char(ds, '\n');
abe529af 6767
c57b2226
BP
6768 rule = rule_dpif_lookup(ofproto, flow);
6769
6a6455e5 6770 trace_format_rule(ds, 0, 0, rule);
c57b2226
BP
6771 if (rule == ofproto->miss_rule) {
6772 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
6773 } else if (rule == ofproto->no_packet_in_rule) {
6774 ds_put_cstr(ds, "\nNo match, packets dropped because "
6775 "OFPPC_NO_PACKET_IN is set on in_port.\n");
6776 }
6777
abe529af 6778 if (rule) {
050ac423
BP
6779 uint64_t odp_actions_stub[1024 / 8];
6780 struct ofpbuf odp_actions;
6781
6a6455e5 6782 struct trace_ctx trace;
0e553d9c 6783 uint8_t tcp_flags;
abe529af 6784
6a6455e5
EJ
6785 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
6786 trace.result = ds;
6787 trace.flow = *flow;
050ac423
BP
6788 ofpbuf_use_stub(&odp_actions,
6789 odp_actions_stub, sizeof odp_actions_stub);
6a6455e5 6790 action_xlate_ctx_init(&trace.ctx, ofproto, flow, initial_tci,
0e553d9c 6791 rule, tcp_flags, packet);
abe529af 6792 trace.ctx.resubmit_hook = trace_resubmit;
479df176 6793 trace.ctx.report_hook = trace_report;
f25d0cf3 6794 xlate_actions(&trace.ctx, rule->up.ofpacts, rule->up.ofpacts_len,
050ac423 6795 &odp_actions);
abe529af 6796
6a6455e5
EJ
6797 ds_put_char(ds, '\n');
6798 trace_format_flow(ds, 0, "Final flow", &trace);
6799 ds_put_cstr(ds, "Datapath actions: ");
050ac423
BP
6800 format_odp_actions(ds, odp_actions.data, odp_actions.size);
6801 ofpbuf_uninit(&odp_actions);
876b0e1c 6802
6a7e895f
BP
6803 if (trace.ctx.slow) {
6804 enum slow_path_reason slow;
6805
6806 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
6807 "slow path because it:");
6808 for (slow = trace.ctx.slow; slow; ) {
6809 enum slow_path_reason bit = rightmost_1bit(slow);
6810
6811 switch (bit) {
6812 case SLOW_CFM:
6813 ds_put_cstr(ds, "\n\t- Consists of CFM packets.");
6814 break;
6815 case SLOW_LACP:
6816 ds_put_cstr(ds, "\n\t- Consists of LACP packets.");
6817 break;
6818 case SLOW_STP:
6819 ds_put_cstr(ds, "\n\t- Consists of STP packets.");
6820 break;
6821 case SLOW_IN_BAND:
6822 ds_put_cstr(ds, "\n\t- Needs in-band special case "
6823 "processing.");
6824 if (!packet) {
6825 ds_put_cstr(ds, "\n\t (The datapath actions are "
6826 "incomplete--for complete actions, "
6827 "please supply a packet.)");
6828 }
6829 break;
6830 case SLOW_CONTROLLER:
6831 ds_put_cstr(ds, "\n\t- Sends \"packet-in\" messages "
6832 "to the OpenFlow controller.");
6833 break;
6834 case SLOW_MATCH:
6835 ds_put_cstr(ds, "\n\t- Needs more specific matching "
6836 "than the datapath supports.");
6837 break;
6838 }
6839
6840 slow &= ~bit;
6841 }
6842
6843 if (slow & ~SLOW_MATCH) {
6844 ds_put_cstr(ds, "\nThe datapath actions above do not reflect "
6845 "the special slow-path processing.");
876b0e1c
BP
6846 }
6847 }
abe529af 6848 }
abe529af
BP
6849}
6850
7ee20df1 6851static void
0e15264f
BP
6852ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6853 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7ee20df1
BP
6854{
6855 clogged = true;
bde9f75d 6856 unixctl_command_reply(conn, NULL);
7ee20df1
BP
6857}
6858
6859static void
0e15264f
BP
6860ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6861 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7ee20df1
BP
6862{
6863 clogged = false;
bde9f75d 6864 unixctl_command_reply(conn, NULL);
7ee20df1
BP
6865}
6866
6814e51f
BP
6867/* Runs a self-check of flow translations in 'ofproto'. Appends a message to
6868 * 'reply' describing the results. */
6869static void
6870ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
6871{
6872 struct facet *facet;
6873 int errors;
6874
6875 errors = 0;
6876 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
6877 if (!facet_check_consistency(facet)) {
6878 errors++;
6879 }
6880 }
6881 if (errors) {
3c4a309c 6882 ofproto->need_revalidate = REV_INCONSISTENCY;
6814e51f
BP
6883 }
6884
6885 if (errors) {
6886 ds_put_format(reply, "%s: self-check failed (%d errors)\n",
6887 ofproto->up.name, errors);
6888 } else {
6889 ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
6890 }
6891}
6892
6893static void
6894ofproto_dpif_self_check(struct unixctl_conn *conn,
6895 int argc, const char *argv[], void *aux OVS_UNUSED)
6896{
6897 struct ds reply = DS_EMPTY_INITIALIZER;
6898 struct ofproto_dpif *ofproto;
6899
6900 if (argc > 1) {
6901 ofproto = ofproto_dpif_lookup(argv[1]);
6902 if (!ofproto) {
bde9f75d
EJ
6903 unixctl_command_reply_error(conn, "Unknown ofproto (use "
6904 "ofproto/list for help)");
6814e51f
BP
6905 return;
6906 }
6907 ofproto_dpif_self_check__(ofproto, &reply);
6908 } else {
6909 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6910 ofproto_dpif_self_check__(ofproto, &reply);
6911 }
6912 }
6913
bde9f75d 6914 unixctl_command_reply(conn, ds_cstr(&reply));
6814e51f
BP
6915 ds_destroy(&reply);
6916}
6917
abe529af
BP
6918static void
6919ofproto_dpif_unixctl_init(void)
6920{
6921 static bool registered;
6922 if (registered) {
6923 return;
6924 }
6925 registered = true;
6926
0e15264f
BP
6927 unixctl_command_register(
6928 "ofproto/trace",
6929 "bridge {tun_id in_port packet | odp_flow [-generate]}",
aa3080c9 6930 2, 5, ofproto_unixctl_trace, NULL);
96e466a3 6931 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
0e15264f
BP
6932 ofproto_unixctl_fdb_flush, NULL);
6933 unixctl_command_register("fdb/show", "bridge", 1, 1,
6934 ofproto_unixctl_fdb_show, NULL);
6935 unixctl_command_register("ofproto/clog", "", 0, 0,
6936 ofproto_dpif_clog, NULL);
6937 unixctl_command_register("ofproto/unclog", "", 0, 0,
6938 ofproto_dpif_unclog, NULL);
6814e51f
BP
6939 unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
6940 ofproto_dpif_self_check, NULL);
abe529af
BP
6941}
6942\f
52a90c29
BP
6943/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6944 *
6945 * This is deprecated. It is only for compatibility with broken device drivers
6946 * in old versions of Linux that do not properly support VLANs when VLAN
6947 * devices are not used. When broken device drivers are no longer in
6948 * widespread use, we will delete these interfaces. */
6949
6950static int
6951set_realdev(struct ofport *ofport_, uint16_t realdev_ofp_port, int vid)
6952{
6953 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
6954 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
6955
6956 if (realdev_ofp_port == ofport->realdev_ofp_port
6957 && vid == ofport->vlandev_vid) {
6958 return 0;
6959 }
6960
3c4a309c 6961 ofproto->need_revalidate = REV_RECONFIGURE;
52a90c29
BP
6962
6963 if (ofport->realdev_ofp_port) {
6964 vsp_remove(ofport);
6965 }
6966 if (realdev_ofp_port && ofport->bundle) {
6967 /* vlandevs are enslaved to their realdevs, so they are not allowed to
6968 * themselves be part of a bundle. */
6969 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
6970 }
6971
6972 ofport->realdev_ofp_port = realdev_ofp_port;
6973 ofport->vlandev_vid = vid;
6974
6975 if (realdev_ofp_port) {
6976 vsp_add(ofport, realdev_ofp_port, vid);
6977 }
6978
6979 return 0;
6980}
6981
6982static uint32_t
6983hash_realdev_vid(uint16_t realdev_ofp_port, int vid)
6984{
6985 return hash_2words(realdev_ofp_port, vid);
6986}
6987
40e05935
BP
6988/* Returns the ODP port number of the Linux VLAN device that corresponds to
6989 * 'vlan_tci' on the network device with port number 'realdev_odp_port' in
6990 * 'ofproto'. For example, given 'realdev_odp_port' of eth0 and 'vlan_tci' 9,
6991 * it would return the port number of eth0.9.
6992 *
6993 * Unless VLAN splinters are enabled for port 'realdev_odp_port', this
6994 * function just returns its 'realdev_odp_port' argument. */
52a90c29
BP
6995static uint32_t
6996vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
6997 uint32_t realdev_odp_port, ovs_be16 vlan_tci)
6998{
6999 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
7000 uint16_t realdev_ofp_port = odp_port_to_ofp_port(realdev_odp_port);
7001 int vid = vlan_tci_to_vid(vlan_tci);
7002 const struct vlan_splinter *vsp;
7003
7004 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
7005 hash_realdev_vid(realdev_ofp_port, vid),
7006 &ofproto->realdev_vid_map) {
7007 if (vsp->realdev_ofp_port == realdev_ofp_port
7008 && vsp->vid == vid) {
7009 return ofp_port_to_odp_port(vsp->vlandev_ofp_port);
7010 }
7011 }
7012 }
7013 return realdev_odp_port;
7014}
7015
7016static struct vlan_splinter *
7017vlandev_find(const struct ofproto_dpif *ofproto, uint16_t vlandev_ofp_port)
7018{
7019 struct vlan_splinter *vsp;
7020
7021 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node, hash_int(vlandev_ofp_port, 0),
7022 &ofproto->vlandev_map) {
7023 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
7024 return vsp;
7025 }
7026 }
7027
7028 return NULL;
7029}
7030
40e05935
BP
7031/* Returns the OpenFlow port number of the "real" device underlying the Linux
7032 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
7033 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
7034 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
7035 * eth0 and store 9 in '*vid'.
7036 *
7037 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
7038 * VLAN device. Unless VLAN splinters are enabled, this is what this function
7039 * always does.*/
52a90c29
BP
7040static uint16_t
7041vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
40e05935 7042 uint16_t vlandev_ofp_port, int *vid)
52a90c29
BP
7043{
7044 if (!hmap_is_empty(&ofproto->vlandev_map)) {
7045 const struct vlan_splinter *vsp;
7046
7047 vsp = vlandev_find(ofproto, vlandev_ofp_port);
7048 if (vsp) {
7049 if (vid) {
7050 *vid = vsp->vid;
7051 }
7052 return vsp->realdev_ofp_port;
7053 }
7054 }
7055 return 0;
7056}
7057
b98d8985
BP
7058/* Given 'flow', a flow representing a packet received on 'ofproto', checks
7059 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
7060 * 'flow->in_port' to the "real" device backing the VLAN device, sets
7061 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
7062 * always the case unless VLAN splinters are enabled), returns false without
7063 * making any changes. */
7064static bool
7065vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
7066{
7067 uint16_t realdev;
7068 int vid;
7069
7070 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
7071 if (!realdev) {
7072 return false;
7073 }
7074
7075 /* Cause the flow to be processed as if it came in on the real device with
7076 * the VLAN device's VLAN ID. */
7077 flow->in_port = realdev;
7078 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
7079 return true;
7080}
7081
52a90c29
BP
7082static void
7083vsp_remove(struct ofport_dpif *port)
7084{
7085 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
7086 struct vlan_splinter *vsp;
7087
7088 vsp = vlandev_find(ofproto, port->up.ofp_port);
7089 if (vsp) {
7090 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
7091 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
7092 free(vsp);
7093
7094 port->realdev_ofp_port = 0;
7095 } else {
7096 VLOG_ERR("missing vlan device record");
7097 }
7098}
7099
7100static void
7101vsp_add(struct ofport_dpif *port, uint16_t realdev_ofp_port, int vid)
7102{
7103 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
7104
7105 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
7106 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
7107 == realdev_ofp_port)) {
7108 struct vlan_splinter *vsp;
7109
7110 vsp = xmalloc(sizeof *vsp);
7111 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
7112 hash_int(port->up.ofp_port, 0));
7113 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
7114 hash_realdev_vid(realdev_ofp_port, vid));
7115 vsp->realdev_ofp_port = realdev_ofp_port;
7116 vsp->vlandev_ofp_port = port->up.ofp_port;
7117 vsp->vid = vid;
7118
7119 port->realdev_ofp_port = realdev_ofp_port;
7120 } else {
7121 VLOG_ERR("duplicate vlan device record");
7122 }
7123}
7124\f
abe529af
BP
7125const struct ofproto_class ofproto_dpif_class = {
7126 enumerate_types,
7127 enumerate_names,
7128 del,
7129 alloc,
7130 construct,
7131 destruct,
7132 dealloc,
7133 run,
5fcc0d00 7134 run_fast,
abe529af 7135 wait,
0d085684 7136 get_memory_usage,
abe529af 7137 flush,
6c1491fb
BP
7138 get_features,
7139 get_tables,
abe529af
BP
7140 port_alloc,
7141 port_construct,
7142 port_destruct,
7143 port_dealloc,
7144 port_modified,
7145 port_reconfigured,
7146 port_query_by_name,
7147 port_add,
7148 port_del,
6527c598 7149 port_get_stats,
abe529af
BP
7150 port_dump_start,
7151 port_dump_next,
7152 port_dump_done,
7153 port_poll,
7154 port_poll_wait,
7155 port_is_lacp_current,
0ab6decf 7156 NULL, /* rule_choose_table */
abe529af
BP
7157 rule_alloc,
7158 rule_construct,
7159 rule_destruct,
7160 rule_dealloc,
abe529af
BP
7161 rule_get_stats,
7162 rule_execute,
7163 rule_modify_actions,
7257b535 7164 set_frag_handling,
abe529af
BP
7165 packet_out,
7166 set_netflow,
7167 get_netflow_ids,
7168 set_sflow,
7169 set_cfm,
a5610457 7170 get_cfm_fault,
1de11730 7171 get_cfm_remote_mpids,
3967a833 7172 get_cfm_health,
21f7563c
JP
7173 set_stp,
7174 get_stp_status,
7175 set_stp_port,
7176 get_stp_port_status,
8b36f51e 7177 set_queues,
abe529af
BP
7178 bundle_set,
7179 bundle_remove,
7180 mirror_set,
9d24de3b 7181 mirror_get_stats,
abe529af
BP
7182 set_flood_vlans,
7183 is_mirror_output_bundle,
8402c74b 7184 forward_bpdu_changed,
e764773c 7185 set_mac_idle_time,
52a90c29 7186 set_realdev,
abe529af 7187};