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