]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif.c
ofproto-dpif: Separately track the initial VLAN TCI of arriving packets.
[mirror_ovs.git] / ofproto / ofproto-dpif.c
CommitLineData
abe529af
BP
1/*
2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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
BP
36#include "mac-learning.h"
37#include "multipath.h"
38#include "netdev.h"
39#include "netlink.h"
40#include "nx-match.h"
41#include "odp-util.h"
42#include "ofp-util.h"
43#include "ofpbuf.h"
44#include "ofp-print.h"
bae473fe 45#include "ofproto-dpif-sflow.h"
abe529af
BP
46#include "poll-loop.h"
47#include "timer.h"
6c1491fb 48#include "unaligned.h"
abe529af
BP
49#include "unixctl.h"
50#include "vlan-bitmap.h"
51#include "vlog.h"
52
53VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
54
55COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
56COVERAGE_DEFINE(ofproto_dpif_expired);
57COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
58COVERAGE_DEFINE(ofproto_dpif_xlate);
59COVERAGE_DEFINE(facet_changed_rule);
60COVERAGE_DEFINE(facet_invalidated);
61COVERAGE_DEFINE(facet_revalidate);
62COVERAGE_DEFINE(facet_unexpected);
63
29901626 64/* Maximum depth of flow table recursion (due to resubmit actions) in a
abe529af 65 * flow translation. */
642a5c05 66#define MAX_RESUBMIT_RECURSION 32
abe529af 67
9cdaaebe
BP
68/* Number of implemented OpenFlow tables. */
69enum { N_TABLES = 255 };
70BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255);
71
abe529af
BP
72struct ofport_dpif;
73struct ofproto_dpif;
74
75struct rule_dpif {
76 struct rule up;
77
78 long long int used; /* Time last used; time created if not used. */
79
80 /* These statistics:
81 *
82 * - Do include packets and bytes from facets that have been deleted or
83 * whose own statistics have been folded into the rule.
84 *
85 * - Do include packets and bytes sent "by hand" that were accounted to
86 * the rule without any facet being involved (this is a rare corner
87 * case in rule_execute()).
88 *
89 * - Do not include packet or bytes that can be obtained from any facet's
90 * packet_count or byte_count member or that can be obtained from the
b0f7b9b5 91 * datapath by, e.g., dpif_flow_get() for any subfacet.
abe529af
BP
92 */
93 uint64_t packet_count; /* Number of packets received. */
94 uint64_t byte_count; /* Number of bytes received. */
95
54a9cbc9
BP
96 tag_type tag; /* Caches rule_calculate_tag() result. */
97
abe529af
BP
98 struct list facets; /* List of "struct facet"s. */
99};
100
101static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
102{
103 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
104}
105
29901626
BP
106static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
107 const struct flow *, uint8_t table);
abe529af 108
b0f7b9b5
BP
109static void flow_push_stats(const struct rule_dpif *, const struct flow *,
110 uint64_t packets, uint64_t bytes,
111 long long int used);
112
113static uint32_t rule_calculate_tag(const struct flow *,
114 const struct flow_wildcards *,
115 uint32_t basis);
116static void rule_invalidate(const struct rule_dpif *);
117
abe529af
BP
118#define MAX_MIRRORS 32
119typedef uint32_t mirror_mask_t;
120#define MIRROR_MASK_C(X) UINT32_C(X)
121BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
122struct ofmirror {
123 struct ofproto_dpif *ofproto; /* Owning ofproto. */
124 size_t idx; /* In ofproto's "mirrors" array. */
125 void *aux; /* Key supplied by ofproto's client. */
126 char *name; /* Identifier for log messages. */
127
128 /* Selection criteria. */
129 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
130 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
131 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
132
9ba15e2a 133 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
abe529af
BP
134 struct ofbundle *out; /* Output port or NULL. */
135 int out_vlan; /* Output VLAN or -1. */
9ba15e2a 136 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
abe529af
BP
137};
138
139static void mirror_destroy(struct ofmirror *);
140
141/* A group of one or more OpenFlow ports. */
142#define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
143struct ofbundle {
144 struct ofproto_dpif *ofproto; /* Owning ofproto. */
145 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
146 void *aux; /* Key supplied by ofproto's client. */
147 char *name; /* Identifier for log messages. */
148
149 /* Configuration. */
150 struct list ports; /* Contains "struct ofport"s. */
ecac4ebf 151 enum port_vlan_mode vlan_mode; /* VLAN mode */
abe529af
BP
152 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
153 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
154 * NULL if all VLANs are trunked. */
155 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
156 struct bond *bond; /* Nonnull iff more than one port. */
5e9ceccd 157 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
abe529af
BP
158
159 /* Status. */
160 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
161
162 /* Port mirroring info. */
163 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
164 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
165 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
166};
167
168static void bundle_remove(struct ofport *);
7bde8dd8 169static void bundle_update(struct ofbundle *);
abe529af
BP
170static void bundle_destroy(struct ofbundle *);
171static void bundle_del_port(struct ofport_dpif *);
172static void bundle_run(struct ofbundle *);
173static void bundle_wait(struct ofbundle *);
174
21f7563c
JP
175static void stp_run(struct ofproto_dpif *ofproto);
176static void stp_wait(struct ofproto_dpif *ofproto);
177
5da5ec37
BP
178static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
179
abe529af
BP
180struct action_xlate_ctx {
181/* action_xlate_ctx_init() initializes these members. */
182
183 /* The ofproto. */
184 struct ofproto_dpif *ofproto;
185
186 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
187 * this flow when actions change header fields. */
188 struct flow flow;
189
190 /* The packet corresponding to 'flow', or a null pointer if we are
191 * revalidating without a packet to refer to. */
192 const struct ofpbuf *packet;
193
75a75043
BP
194 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
195 * want to execute them if we are actually processing a packet, or if we
196 * are accounting for packets that the datapath has processed, but not if
197 * we are just revalidating. */
198 bool may_learn;
199
abe529af
BP
200 /* If nonnull, called just before executing a resubmit action.
201 *
202 * This is normally null so the client has to set it manually after
203 * calling action_xlate_ctx_init(). */
204 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
205
abe529af
BP
206/* xlate_actions() initializes and uses these members. The client might want
207 * to look at them after it returns. */
208
209 struct ofpbuf *odp_actions; /* Datapath actions. */
75a75043 210 tag_type tags; /* Tags associated with actions. */
abe529af
BP
211 bool may_set_up_flow; /* True ordinarily; false if the actions must
212 * be reassessed for every packet. */
75a75043
BP
213 bool has_learn; /* Actions include NXAST_LEARN? */
214 bool has_normal; /* Actions output to OFPP_NORMAL? */
abe529af
BP
215 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
216
217/* xlate_actions() initializes and uses these members, but the client has no
218 * reason to look at them. */
219
220 int recurse; /* Recursion level, via xlate_table_action. */
b3e9b2ed 221 struct flow base_flow; /* Flow at the last commit. */
abff858b 222 uint32_t original_priority; /* Priority when packet arrived. */
29901626 223 uint8_t table_id; /* OpenFlow table ID where flow was found. */
6ff686f2
PS
224 uint32_t sflow_n_outputs; /* Number of output ports. */
225 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
226 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
848e8809 227 bool exit; /* No further actions should be processed. */
abe529af
BP
228};
229
230static void action_xlate_ctx_init(struct action_xlate_ctx *,
231 struct ofproto_dpif *, const struct flow *,
e84173dc 232 ovs_be16 initial_tci, const struct ofpbuf *);
abe529af
BP
233static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
234 const union ofp_action *in, size_t n_in);
235
b0f7b9b5
BP
236/* An exact-match instantiation of an OpenFlow flow.
237 *
238 * A facet associates a "struct flow", which represents the Open vSwitch
b95fc6ba
BP
239 * userspace idea of an exact-match flow, with one or more subfacets. Each
240 * subfacet tracks the datapath's idea of the exact-match flow equivalent to
241 * the facet. When the kernel module (or other dpif implementation) and Open
242 * vSwitch userspace agree on the definition of a flow key, there is exactly
243 * one subfacet per facet. If the dpif implementation supports more-specific
244 * flow matching than userspace, however, a facet can have more than one
245 * subfacet, each of which corresponds to some distinction in flow that
246 * userspace simply doesn't understand.
b0f7b9b5
BP
247 *
248 * Flow expiration works in terms of subfacets, so a facet must have at least
249 * one subfacet or it will never expire, leaking memory. */
abe529af 250struct facet {
b0f7b9b5
BP
251 /* Owners. */
252 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
253 struct list list_node; /* In owning rule's 'facets' list. */
254 struct rule_dpif *rule; /* Owning rule. */
255
256 /* Owned data. */
257 struct list subfacets;
abe529af
BP
258 long long int used; /* Time last used; time created if not used. */
259
b0f7b9b5
BP
260 /* Key. */
261 struct flow flow;
262
abe529af
BP
263 /* These statistics:
264 *
265 * - Do include packets and bytes sent "by hand", e.g. with
266 * dpif_execute().
267 *
268 * - Do include packets and bytes that were obtained from the datapath
b0f7b9b5 269 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
abe529af 270 * DPIF_FP_ZERO_STATS).
b0f7b9b5
BP
271 *
272 * - Do not include packets or bytes that can be obtained from the
273 * datapath for any existing subfacet.
abe529af
BP
274 */
275 uint64_t packet_count; /* Number of packets received. */
276 uint64_t byte_count; /* Number of bytes received. */
277
b0f7b9b5 278 /* Resubmit statistics. */
abe529af
BP
279 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
280 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
281 long long int rs_used; /* Used time pushed to resubmit children. */
282
b0f7b9b5 283 /* Accounting. */
907a4c5e 284 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
b0f7b9b5 285 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
abe529af 286
b95fc6ba
BP
287 /* Properties of datapath actions.
288 *
289 * Every subfacet has its own actions because actions can differ slightly
290 * between splintered and non-splintered subfacets due to the VLAN tag
291 * being initially different (present vs. absent). All of them have these
292 * properties in common so we just store one copy of them here. */
b0f7b9b5 293 bool may_install; /* Reassess actions for every packet? */
75a75043
BP
294 bool has_learn; /* Actions include NXAST_LEARN? */
295 bool has_normal; /* Actions output to OFPP_NORMAL? */
b0f7b9b5 296 tag_type tags; /* Tags that would require revalidation. */
abe529af
BP
297};
298
f3827897 299static struct facet *facet_create(struct rule_dpif *, const struct flow *);
abe529af
BP
300static void facet_remove(struct ofproto_dpif *, struct facet *);
301static void facet_free(struct facet *);
302
303static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
304static struct facet *facet_lookup_valid(struct ofproto_dpif *,
305 const struct flow *);
306static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
307
3d9e05f8
BP
308static bool execute_controller_action(struct ofproto_dpif *,
309 const struct flow *,
310 const struct nlattr *odp_actions,
311 size_t actions_len,
312 struct ofpbuf *packet);
b0f7b9b5 313
abe529af
BP
314static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
315
abe529af
BP
316static void facet_update_time(struct ofproto_dpif *, struct facet *,
317 long long int used);
bbb5d219 318static void facet_reset_counters(struct facet *);
abe529af 319static void facet_push_stats(struct facet *);
55af77bb 320static void facet_account(struct ofproto_dpif *, struct facet *);
abe529af
BP
321
322static bool facet_is_controller_flow(struct facet *);
323
b95fc6ba 324/* A dpif flow and actions associated with a facet.
b0f7b9b5
BP
325 *
326 * See also the large comment on struct facet. */
327struct subfacet {
328 /* Owners. */
329 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
330 struct list list_node; /* In struct facet's 'facets' list. */
331 struct facet *facet; /* Owning facet. */
332
333 /* Key.
334 *
335 * To save memory in the common case, 'key' is NULL if 'key_fitness' is
336 * ODP_FIT_PERFECT, that is, odp_flow_key_from_flow() can accurately
337 * regenerate the ODP flow key from ->facet->flow. */
338 enum odp_key_fitness key_fitness;
339 struct nlattr *key;
340 int key_len;
abe529af 341
b0f7b9b5
BP
342 long long int used; /* Time last used; time created if not used. */
343
344 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
345 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
346
b95fc6ba
BP
347 /* Datapath actions.
348 *
349 * These should be essentially identical for every subfacet in a facet, but
350 * may differ in trivial ways due to VLAN splinters. */
351 size_t actions_len; /* Number of bytes in actions[]. */
352 struct nlattr *actions; /* Datapath actions. */
353
b0f7b9b5 354 bool installed; /* Installed in datapath? */
e84173dc
BP
355
356 /* This value is normally the same as ->facet->flow.vlan_tci. Only VLAN
357 * splinters can cause it to differ. This value should be removed when
358 * the VLAN splinters feature is no longer needed. */
359 ovs_be16 initial_tci; /* Initial VLAN TCI value. */
b0f7b9b5
BP
360};
361
362static struct subfacet *subfacet_create(struct ofproto_dpif *, struct facet *,
363 enum odp_key_fitness,
364 const struct nlattr *key,
e84173dc 365 size_t key_len, ovs_be16 initial_tci);
b0f7b9b5
BP
366static struct subfacet *subfacet_find(struct ofproto_dpif *,
367 const struct nlattr *key, size_t key_len,
368 const struct flow *);
369static void subfacet_destroy(struct ofproto_dpif *, struct subfacet *);
370static void subfacet_destroy__(struct ofproto_dpif *, struct subfacet *);
371static void subfacet_reset_dp_stats(struct subfacet *,
372 struct dpif_flow_stats *);
373static void subfacet_update_time(struct ofproto_dpif *, struct subfacet *,
374 long long int used);
375static void subfacet_update_stats(struct ofproto_dpif *, struct subfacet *,
376 const struct dpif_flow_stats *);
b95fc6ba
BP
377static void subfacet_make_actions(struct ofproto_dpif *, struct subfacet *,
378 const struct ofpbuf *packet);
b0f7b9b5
BP
379static int subfacet_install(struct ofproto_dpif *, struct subfacet *,
380 const struct nlattr *actions, size_t actions_len,
381 struct dpif_flow_stats *);
382static void subfacet_uninstall(struct ofproto_dpif *, struct subfacet *);
54a9cbc9 383
abe529af
BP
384struct ofport_dpif {
385 struct ofport up;
386
387 uint32_t odp_port;
388 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
389 struct list bundle_node; /* In struct ofbundle's "ports" list. */
390 struct cfm *cfm; /* Connectivity Fault Management, if any. */
391 tag_type tag; /* Tag associated with this port. */
00794817 392 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
015e08bc 393 bool may_enable; /* May be enabled in bonds. */
21f7563c
JP
394
395 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
396 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
397 long long int stp_state_entered;
8b36f51e
EJ
398
399 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
400};
401
402/* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
403 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
404 * traffic egressing the 'ofport' with that priority should be marked with. */
405struct priority_to_dscp {
406 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
407 uint32_t priority; /* Priority of this queue (see struct flow). */
408
409 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
abe529af
BP
410};
411
412static struct ofport_dpif *
413ofport_dpif_cast(const struct ofport *ofport)
414{
415 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
416 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
417}
418
419static void port_run(struct ofport_dpif *);
420static void port_wait(struct ofport_dpif *);
a5610457 421static int set_cfm(struct ofport *, const struct cfm_settings *);
8b36f51e 422static void ofport_clear_priorities(struct ofport_dpif *);
abe529af 423
7ee20df1
BP
424struct dpif_completion {
425 struct list list_node;
426 struct ofoperation *op;
427};
428
54a9cbc9
BP
429/* Extra information about a classifier table.
430 * Currently used just for optimized flow revalidation. */
431struct table_dpif {
432 /* If either of these is nonnull, then this table has a form that allows
433 * flows to be tagged to avoid revalidating most flows for the most common
434 * kinds of flow table changes. */
435 struct cls_table *catchall_table; /* Table that wildcards all fields. */
436 struct cls_table *other_table; /* Table with any other wildcard set. */
437 uint32_t basis; /* Keeps each table's tags separate. */
438};
439
abe529af
BP
440struct ofproto_dpif {
441 struct ofproto up;
442 struct dpif *dpif;
443 int max_ports;
444
6c1491fb
BP
445 /* Statistics. */
446 uint64_t n_matches;
447
abe529af
BP
448 /* Bridging. */
449 struct netflow *netflow;
bae473fe 450 struct dpif_sflow *sflow;
abe529af
BP
451 struct hmap bundles; /* Contains "struct ofbundle"s. */
452 struct mac_learning *ml;
453 struct ofmirror *mirrors[MAX_MIRRORS];
454 bool has_bonded_bundles;
455
456 /* Expiration. */
457 struct timer next_expiration;
458
459 /* Facets. */
460 struct hmap facets;
b0f7b9b5 461 struct hmap subfacets;
54a9cbc9
BP
462
463 /* Revalidation. */
464 struct table_dpif tables[N_TABLES];
abe529af
BP
465 bool need_revalidate;
466 struct tag_set revalidate_set;
7ee20df1
BP
467
468 /* Support for debugging async flow mods. */
469 struct list completions;
daff3353
EJ
470
471 bool has_bundle_action; /* True when the first bundle action appears. */
21f7563c
JP
472
473 /* Spanning tree. */
474 struct stp *stp;
475 long long int stp_last_tick;
abe529af
BP
476};
477
7ee20df1
BP
478/* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
479 * for debugging the asynchronous flow_mod implementation.) */
480static bool clogged;
481
abe529af
BP
482static void ofproto_dpif_unixctl_init(void);
483
484static struct ofproto_dpif *
485ofproto_dpif_cast(const struct ofproto *ofproto)
486{
487 assert(ofproto->ofproto_class == &ofproto_dpif_class);
488 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
489}
490
491static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
492 uint16_t ofp_port);
493static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
494 uint32_t odp_port);
495
496/* Packet processing. */
497static void update_learning_table(struct ofproto_dpif *,
498 const struct flow *, int vlan,
499 struct ofbundle *);
501f8d1f
BP
500/* Upcalls. */
501#define FLOW_MISS_MAX_BATCH 50
395e68ce 502
abe529af 503static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
501f8d1f
BP
504static void handle_miss_upcalls(struct ofproto_dpif *,
505 struct dpif_upcall *, size_t n);
abe529af
BP
506
507/* Flow expiration. */
508static int expire(struct ofproto_dpif *);
509
6fca1ffb
BP
510/* NetFlow. */
511static void send_netflow_active_timeouts(struct ofproto_dpif *);
512
abe529af 513/* Utilities. */
97d6520b 514static int send_packet(const struct ofport_dpif *,
abe529af 515 const struct ofpbuf *packet);
6ff686f2
PS
516static size_t
517compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
518 const struct flow *, uint32_t odp_port);
abe529af
BP
519/* Global variables. */
520static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
521\f
522/* Factory functions. */
523
524static void
525enumerate_types(struct sset *types)
526{
527 dp_enumerate_types(types);
528}
529
530static int
531enumerate_names(const char *type, struct sset *names)
532{
533 return dp_enumerate_names(type, names);
534}
535
536static int
537del(const char *type, const char *name)
538{
539 struct dpif *dpif;
540 int error;
541
542 error = dpif_open(name, type, &dpif);
543 if (!error) {
544 error = dpif_delete(dpif);
545 dpif_close(dpif);
546 }
547 return error;
548}
549\f
550/* Basic life-cycle. */
551
552static struct ofproto *
553alloc(void)
554{
555 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
556 return &ofproto->up;
557}
558
559static void
560dealloc(struct ofproto *ofproto_)
561{
562 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
563 free(ofproto);
564}
565
566static int
073e2a6f 567construct(struct ofproto *ofproto_, int *n_tablesp)
abe529af
BP
568{
569 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
570 const char *name = ofproto->up.name;
571 int error;
572 int i;
573
574 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
575 if (error) {
576 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
577 return error;
578 }
579
580 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
6c1491fb 581 ofproto->n_matches = 0;
abe529af 582
be8194bb
JG
583 dpif_flow_flush(ofproto->dpif);
584 dpif_recv_purge(ofproto->dpif);
585
abe529af
BP
586 error = dpif_recv_set_mask(ofproto->dpif,
587 ((1u << DPIF_UC_MISS) |
6ff686f2 588 (1u << DPIF_UC_ACTION)));
abe529af
BP
589 if (error) {
590 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
591 dpif_close(ofproto->dpif);
592 return error;
593 }
abe529af
BP
594
595 ofproto->netflow = NULL;
596 ofproto->sflow = NULL;
21f7563c 597 ofproto->stp = NULL;
abe529af
BP
598 hmap_init(&ofproto->bundles);
599 ofproto->ml = mac_learning_create();
600 for (i = 0; i < MAX_MIRRORS; i++) {
601 ofproto->mirrors[i] = NULL;
602 }
603 ofproto->has_bonded_bundles = false;
604
605 timer_set_duration(&ofproto->next_expiration, 1000);
606
607 hmap_init(&ofproto->facets);
b0f7b9b5 608 hmap_init(&ofproto->subfacets);
54a9cbc9
BP
609
610 for (i = 0; i < N_TABLES; i++) {
611 struct table_dpif *table = &ofproto->tables[i];
612
613 table->catchall_table = NULL;
614 table->other_table = NULL;
615 table->basis = random_uint32();
616 }
abe529af
BP
617 ofproto->need_revalidate = false;
618 tag_set_init(&ofproto->revalidate_set);
619
7ee20df1
BP
620 list_init(&ofproto->completions);
621
abe529af
BP
622 ofproto_dpif_unixctl_init();
623
daff3353
EJ
624 ofproto->has_bundle_action = false;
625
9cdaaebe 626 *n_tablesp = N_TABLES;
abe529af
BP
627 return 0;
628}
629
7ee20df1
BP
630static void
631complete_operations(struct ofproto_dpif *ofproto)
632{
633 struct dpif_completion *c, *next;
634
635 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
636 ofoperation_complete(c->op, 0);
637 list_remove(&c->list_node);
638 free(c);
639 }
640}
641
abe529af
BP
642static void
643destruct(struct ofproto *ofproto_)
644{
645 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7ee20df1 646 struct rule_dpif *rule, *next_rule;
0697b5c3 647 struct classifier *table;
abe529af
BP
648 int i;
649
7ee20df1
BP
650 complete_operations(ofproto);
651
0697b5c3
BP
652 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
653 struct cls_cursor cursor;
654
655 cls_cursor_init(&cursor, table, NULL);
656 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
657 ofproto_rule_destroy(&rule->up);
658 }
7ee20df1
BP
659 }
660
abe529af
BP
661 for (i = 0; i < MAX_MIRRORS; i++) {
662 mirror_destroy(ofproto->mirrors[i]);
663 }
664
665 netflow_destroy(ofproto->netflow);
bae473fe 666 dpif_sflow_destroy(ofproto->sflow);
abe529af
BP
667 hmap_destroy(&ofproto->bundles);
668 mac_learning_destroy(ofproto->ml);
669
670 hmap_destroy(&ofproto->facets);
b0f7b9b5 671 hmap_destroy(&ofproto->subfacets);
abe529af
BP
672
673 dpif_close(ofproto->dpif);
674}
675
676static int
677run(struct ofproto *ofproto_)
678{
679 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
501f8d1f 680 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
abe529af
BP
681 struct ofport_dpif *ofport;
682 struct ofbundle *bundle;
501f8d1f 683 size_t n_misses;
abe529af
BP
684 int i;
685
7ee20df1
BP
686 if (!clogged) {
687 complete_operations(ofproto);
688 }
abe529af
BP
689 dpif_run(ofproto->dpif);
690
501f8d1f
BP
691 n_misses = 0;
692 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
693 struct dpif_upcall *upcall = &misses[n_misses];
abe529af
BP
694 int error;
695
501f8d1f 696 error = dpif_recv(ofproto->dpif, upcall);
abe529af 697 if (error) {
501f8d1f 698 if (error == ENODEV && n_misses == 0) {
abe529af
BP
699 return error;
700 }
701 break;
702 }
703
501f8d1f
BP
704 if (upcall->type == DPIF_UC_MISS) {
705 /* Handle it later. */
706 n_misses++;
707 } else {
708 handle_upcall(ofproto, upcall);
709 }
abe529af
BP
710 }
711
501f8d1f
BP
712 handle_miss_upcalls(ofproto, misses, n_misses);
713
abe529af
BP
714 if (timer_expired(&ofproto->next_expiration)) {
715 int delay = expire(ofproto);
716 timer_set_duration(&ofproto->next_expiration, delay);
717 }
718
719 if (ofproto->netflow) {
6fca1ffb
BP
720 if (netflow_run(ofproto->netflow)) {
721 send_netflow_active_timeouts(ofproto);
722 }
abe529af
BP
723 }
724 if (ofproto->sflow) {
bae473fe 725 dpif_sflow_run(ofproto->sflow);
abe529af
BP
726 }
727
728 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
729 port_run(ofport);
730 }
731 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
732 bundle_run(bundle);
733 }
734
21f7563c 735 stp_run(ofproto);
1c313b88
BP
736 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
737
abe529af
BP
738 /* Now revalidate if there's anything to do. */
739 if (ofproto->need_revalidate
740 || !tag_set_is_empty(&ofproto->revalidate_set)) {
741 struct tag_set revalidate_set = ofproto->revalidate_set;
742 bool revalidate_all = ofproto->need_revalidate;
743 struct facet *facet, *next;
744
745 /* Clear the revalidation flags. */
746 tag_set_init(&ofproto->revalidate_set);
747 ofproto->need_revalidate = false;
748
749 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
750 if (revalidate_all
751 || tag_set_intersects(&revalidate_set, facet->tags)) {
752 facet_revalidate(ofproto, facet);
753 }
754 }
755 }
756
757 return 0;
758}
759
760static void
761wait(struct ofproto *ofproto_)
762{
763 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
764 struct ofport_dpif *ofport;
765 struct ofbundle *bundle;
766
7ee20df1
BP
767 if (!clogged && !list_is_empty(&ofproto->completions)) {
768 poll_immediate_wake();
769 }
770
abe529af
BP
771 dpif_wait(ofproto->dpif);
772 dpif_recv_wait(ofproto->dpif);
773 if (ofproto->sflow) {
bae473fe 774 dpif_sflow_wait(ofproto->sflow);
abe529af
BP
775 }
776 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
777 poll_immediate_wake();
778 }
779 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
780 port_wait(ofport);
781 }
782 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
783 bundle_wait(bundle);
784 }
6fca1ffb
BP
785 if (ofproto->netflow) {
786 netflow_wait(ofproto->netflow);
787 }
1c313b88 788 mac_learning_wait(ofproto->ml);
21f7563c 789 stp_wait(ofproto);
abe529af
BP
790 if (ofproto->need_revalidate) {
791 /* Shouldn't happen, but if it does just go around again. */
792 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
793 poll_immediate_wake();
794 } else {
795 timer_wait(&ofproto->next_expiration);
796 }
797}
798
799static void
800flush(struct ofproto *ofproto_)
801{
802 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
803 struct facet *facet, *next_facet;
804
805 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
806 /* Mark the facet as not installed so that facet_remove() doesn't
807 * bother trying to uninstall it. There is no point in uninstalling it
808 * individually since we are about to blow away all the facets with
809 * dpif_flow_flush(). */
b0f7b9b5
BP
810 struct subfacet *subfacet;
811
812 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
813 subfacet->installed = false;
814 subfacet->dp_packet_count = 0;
815 subfacet->dp_byte_count = 0;
816 }
abe529af
BP
817 facet_remove(ofproto, facet);
818 }
819 dpif_flow_flush(ofproto->dpif);
820}
821
6c1491fb
BP
822static void
823get_features(struct ofproto *ofproto_ OVS_UNUSED,
824 bool *arp_match_ip, uint32_t *actions)
825{
826 *arp_match_ip = true;
827 *actions = ((1u << OFPAT_OUTPUT) |
828 (1u << OFPAT_SET_VLAN_VID) |
829 (1u << OFPAT_SET_VLAN_PCP) |
830 (1u << OFPAT_STRIP_VLAN) |
831 (1u << OFPAT_SET_DL_SRC) |
832 (1u << OFPAT_SET_DL_DST) |
833 (1u << OFPAT_SET_NW_SRC) |
834 (1u << OFPAT_SET_NW_DST) |
835 (1u << OFPAT_SET_NW_TOS) |
836 (1u << OFPAT_SET_TP_SRC) |
837 (1u << OFPAT_SET_TP_DST) |
838 (1u << OFPAT_ENQUEUE));
839}
840
841static void
842get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
843{
844 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
a8d9304d 845 struct dpif_dp_stats s;
6c1491fb
BP
846
847 strcpy(ots->name, "classifier");
848
849 dpif_get_dp_stats(ofproto->dpif, &s);
850 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
851 put_32aligned_be64(&ots->matched_count,
852 htonll(s.n_hit + ofproto->n_matches));
853}
854
abe529af
BP
855static struct ofport *
856port_alloc(void)
857{
858 struct ofport_dpif *port = xmalloc(sizeof *port);
859 return &port->up;
860}
861
862static void
863port_dealloc(struct ofport *port_)
864{
865 struct ofport_dpif *port = ofport_dpif_cast(port_);
866 free(port);
867}
868
869static int
870port_construct(struct ofport *port_)
871{
872 struct ofport_dpif *port = ofport_dpif_cast(port_);
873 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
874
f11c28c4 875 ofproto->need_revalidate = true;
abe529af
BP
876 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
877 port->bundle = NULL;
878 port->cfm = NULL;
879 port->tag = tag_create_random();
d5ffa7f2 880 port->may_enable = true;
21f7563c
JP
881 port->stp_port = NULL;
882 port->stp_state = STP_DISABLED;
8b36f51e 883 hmap_init(&port->priorities);
abe529af
BP
884
885 if (ofproto->sflow) {
bae473fe
JP
886 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
887 netdev_get_name(port->up.netdev));
abe529af
BP
888 }
889
890 return 0;
891}
892
893static void
894port_destruct(struct ofport *port_)
895{
896 struct ofport_dpif *port = ofport_dpif_cast(port_);
897 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
898
f11c28c4 899 ofproto->need_revalidate = true;
abe529af 900 bundle_remove(port_);
a5610457 901 set_cfm(port_, NULL);
abe529af 902 if (ofproto->sflow) {
bae473fe 903 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
abe529af 904 }
8b36f51e
EJ
905
906 ofport_clear_priorities(port);
907 hmap_destroy(&port->priorities);
abe529af
BP
908}
909
910static void
911port_modified(struct ofport *port_)
912{
913 struct ofport_dpif *port = ofport_dpif_cast(port_);
914
915 if (port->bundle && port->bundle->bond) {
916 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
917 }
918}
919
920static void
921port_reconfigured(struct ofport *port_, ovs_be32 old_config)
922{
923 struct ofport_dpif *port = ofport_dpif_cast(port_);
924 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
925 ovs_be32 changed = old_config ^ port->up.opp.config;
926
927 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
928 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
929 ofproto->need_revalidate = true;
7bde8dd8
JP
930
931 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
932 bundle_update(port->bundle);
933 }
abe529af
BP
934 }
935}
936
937static int
938set_sflow(struct ofproto *ofproto_,
939 const struct ofproto_sflow_options *sflow_options)
940{
941 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bae473fe 942 struct dpif_sflow *ds = ofproto->sflow;
6ff686f2 943
abe529af 944 if (sflow_options) {
bae473fe 945 if (!ds) {
abe529af
BP
946 struct ofport_dpif *ofport;
947
bae473fe 948 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
abe529af 949 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
bae473fe
JP
950 dpif_sflow_add_port(ds, ofport->odp_port,
951 netdev_get_name(ofport->up.netdev));
abe529af 952 }
6ff686f2 953 ofproto->need_revalidate = true;
abe529af 954 }
bae473fe 955 dpif_sflow_set_options(ds, sflow_options);
abe529af 956 } else {
6ff686f2
PS
957 if (ds) {
958 dpif_sflow_destroy(ds);
959 ofproto->need_revalidate = true;
960 ofproto->sflow = NULL;
961 }
abe529af
BP
962 }
963 return 0;
964}
965
966static int
a5610457 967set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
abe529af
BP
968{
969 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
970 int error;
971
a5610457 972 if (!s) {
abe529af
BP
973 error = 0;
974 } else {
975 if (!ofport->cfm) {
8c977421
EJ
976 struct ofproto_dpif *ofproto;
977
978 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
979 ofproto->need_revalidate = true;
6f629657 980 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
abe529af
BP
981 }
982
a5610457 983 if (cfm_configure(ofport->cfm, s)) {
abe529af
BP
984 return 0;
985 }
986
987 error = EINVAL;
988 }
989 cfm_destroy(ofport->cfm);
990 ofport->cfm = NULL;
991 return error;
992}
993
994static int
a5610457 995get_cfm_fault(const struct ofport *ofport_)
abe529af
BP
996{
997 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
a5610457
EJ
998
999 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
abe529af 1000}
1de11730
EJ
1001
1002static int
1003get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1004 size_t *n_rmps)
1005{
1006 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1007
1008 if (ofport->cfm) {
1009 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1010 return 0;
1011 } else {
1012 return -1;
1013 }
1014}
abe529af 1015\f
21f7563c
JP
1016/* Spanning Tree. */
1017
1018static void
1019send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1020{
1021 struct ofproto_dpif *ofproto = ofproto_;
1022 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1023 struct ofport_dpif *ofport;
1024
1025 ofport = stp_port_get_aux(sp);
1026 if (!ofport) {
1027 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1028 ofproto->up.name, port_num);
1029 } else {
1030 struct eth_header *eth = pkt->l2;
1031
1032 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1033 if (eth_addr_is_zero(eth->eth_src)) {
1034 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1035 "with unknown MAC", ofproto->up.name, port_num);
1036 } else {
97d6520b 1037 send_packet(ofport, pkt);
21f7563c
JP
1038 }
1039 }
1040 ofpbuf_delete(pkt);
1041}
1042
1043/* Configures STP on 'ofproto_' using the settings defined in 's'. */
1044static int
1045set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1046{
1047 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1048
1049 /* Only revalidate flows if the configuration changed. */
1050 if (!s != !ofproto->stp) {
1051 ofproto->need_revalidate = true;
1052 }
1053
1054 if (s) {
1055 if (!ofproto->stp) {
1056 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1057 send_bpdu_cb, ofproto);
1058 ofproto->stp_last_tick = time_msec();
1059 }
1060
1061 stp_set_bridge_id(ofproto->stp, s->system_id);
1062 stp_set_bridge_priority(ofproto->stp, s->priority);
1063 stp_set_hello_time(ofproto->stp, s->hello_time);
1064 stp_set_max_age(ofproto->stp, s->max_age);
1065 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1066 } else {
1067 stp_destroy(ofproto->stp);
1068 ofproto->stp = NULL;
1069 }
1070
1071 return 0;
1072}
1073
1074static int
1075get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1076{
1077 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1078
1079 if (ofproto->stp) {
1080 s->enabled = true;
1081 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1082 s->designated_root = stp_get_designated_root(ofproto->stp);
1083 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1084 } else {
1085 s->enabled = false;
1086 }
1087
1088 return 0;
1089}
1090
1091static void
1092update_stp_port_state(struct ofport_dpif *ofport)
1093{
1094 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1095 enum stp_state state;
1096
1097 /* Figure out new state. */
1098 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1099 : STP_DISABLED;
1100
1101 /* Update state. */
1102 if (ofport->stp_state != state) {
1103 ovs_be32 of_state;
1104 bool fwd_change;
1105
1106 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1107 netdev_get_name(ofport->up.netdev),
1108 stp_state_name(ofport->stp_state),
1109 stp_state_name(state));
1110 if (stp_learn_in_state(ofport->stp_state)
1111 != stp_learn_in_state(state)) {
1112 /* xxx Learning action flows should also be flushed. */
1113 mac_learning_flush(ofproto->ml);
1114 }
1115 fwd_change = stp_forward_in_state(ofport->stp_state)
1116 != stp_forward_in_state(state);
1117
1118 ofproto->need_revalidate = true;
1119 ofport->stp_state = state;
1120 ofport->stp_state_entered = time_msec();
1121
b308140a 1122 if (fwd_change && ofport->bundle) {
21f7563c
JP
1123 bundle_update(ofport->bundle);
1124 }
1125
1126 /* Update the STP state bits in the OpenFlow port description. */
1127 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1128 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1129 : state == STP_LEARNING ? OFPPS_STP_LEARN
1130 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1131 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1132 : 0);
1133 ofproto_port_set_state(&ofport->up, of_state);
1134 }
1135}
1136
1137/* Configures STP on 'ofport_' using the settings defined in 's'. The
1138 * caller is responsible for assigning STP port numbers and ensuring
1139 * there are no duplicates. */
1140static int
1141set_stp_port(struct ofport *ofport_,
1142 const struct ofproto_port_stp_settings *s)
1143{
1144 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1145 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1146 struct stp_port *sp = ofport->stp_port;
1147
1148 if (!s || !s->enable) {
1149 if (sp) {
1150 ofport->stp_port = NULL;
1151 stp_port_disable(sp);
ecd12731 1152 update_stp_port_state(ofport);
21f7563c
JP
1153 }
1154 return 0;
1155 } else if (sp && stp_port_no(sp) != s->port_num
1156 && ofport == stp_port_get_aux(sp)) {
1157 /* The port-id changed, so disable the old one if it's not
1158 * already in use by another port. */
1159 stp_port_disable(sp);
1160 }
1161
1162 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1163 stp_port_enable(sp);
1164
1165 stp_port_set_aux(sp, ofport);
1166 stp_port_set_priority(sp, s->priority);
1167 stp_port_set_path_cost(sp, s->path_cost);
1168
1169 update_stp_port_state(ofport);
1170
1171 return 0;
1172}
1173
1174static int
1175get_stp_port_status(struct ofport *ofport_,
1176 struct ofproto_port_stp_status *s)
1177{
1178 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1179 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1180 struct stp_port *sp = ofport->stp_port;
1181
1182 if (!ofproto->stp || !sp) {
1183 s->enabled = false;
1184 return 0;
1185 }
1186
1187 s->enabled = true;
1188 s->port_id = stp_port_get_id(sp);
1189 s->state = stp_port_get_state(sp);
1190 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1191 s->role = stp_port_get_role(sp);
80740385 1192 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
21f7563c
JP
1193
1194 return 0;
1195}
1196
1197static void
1198stp_run(struct ofproto_dpif *ofproto)
1199{
1200 if (ofproto->stp) {
1201 long long int now = time_msec();
1202 long long int elapsed = now - ofproto->stp_last_tick;
1203 struct stp_port *sp;
1204
1205 if (elapsed > 0) {
1206 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1207 ofproto->stp_last_tick = now;
1208 }
1209 while (stp_get_changed_port(ofproto->stp, &sp)) {
1210 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1211
1212 if (ofport) {
1213 update_stp_port_state(ofport);
1214 }
1215 }
1216 }
1217}
1218
1219static void
1220stp_wait(struct ofproto_dpif *ofproto)
1221{
1222 if (ofproto->stp) {
1223 poll_timer_wait(1000);
1224 }
1225}
1226
1227/* Returns true if STP should process 'flow'. */
1228static bool
1229stp_should_process_flow(const struct flow *flow)
1230{
1231 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1232}
1233
1234static void
1235stp_process_packet(const struct ofport_dpif *ofport,
1236 const struct ofpbuf *packet)
1237{
1238 struct ofpbuf payload = *packet;
1239 struct eth_header *eth = payload.data;
1240 struct stp_port *sp = ofport->stp_port;
1241
1242 /* Sink packets on ports that have STP disabled when the bridge has
1243 * STP enabled. */
1244 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1245 return;
1246 }
1247
1248 /* Trim off padding on payload. */
c573540b
BP
1249 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1250 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
21f7563c
JP
1251 }
1252
1253 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1254 stp_received_bpdu(sp, payload.data, payload.size);
1255 }
1256}
1257\f
8b36f51e
EJ
1258static struct priority_to_dscp *
1259get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1260{
1261 struct priority_to_dscp *pdscp;
1262 uint32_t hash;
1263
1264 hash = hash_int(priority, 0);
1265 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1266 if (pdscp->priority == priority) {
1267 return pdscp;
1268 }
1269 }
1270 return NULL;
1271}
1272
1273static void
1274ofport_clear_priorities(struct ofport_dpif *ofport)
1275{
1276 struct priority_to_dscp *pdscp, *next;
1277
1278 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1279 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1280 free(pdscp);
1281 }
1282}
1283
1284static int
1285set_queues(struct ofport *ofport_,
1286 const struct ofproto_port_queue *qdscp_list,
1287 size_t n_qdscp)
1288{
1289 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1290 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1291 struct hmap new = HMAP_INITIALIZER(&new);
1292 size_t i;
1293
1294 for (i = 0; i < n_qdscp; i++) {
1295 struct priority_to_dscp *pdscp;
1296 uint32_t priority;
1297 uint8_t dscp;
1298
1299 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1300 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1301 &priority)) {
1302 continue;
1303 }
1304
1305 pdscp = get_priority(ofport, priority);
1306 if (pdscp) {
1307 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1308 } else {
1309 pdscp = xmalloc(sizeof *pdscp);
1310 pdscp->priority = priority;
1311 pdscp->dscp = dscp;
1312 ofproto->need_revalidate = true;
1313 }
1314
1315 if (pdscp->dscp != dscp) {
1316 pdscp->dscp = dscp;
1317 ofproto->need_revalidate = true;
1318 }
1319
1320 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1321 }
1322
1323 if (!hmap_is_empty(&ofport->priorities)) {
1324 ofport_clear_priorities(ofport);
1325 ofproto->need_revalidate = true;
1326 }
1327
1328 hmap_swap(&new, &ofport->priorities);
1329 hmap_destroy(&new);
1330
1331 return 0;
1332}
1333\f
abe529af
BP
1334/* Bundles. */
1335
1336/* Expires all MAC learning entries associated with 'port' and forces ofproto
1337 * to revalidate every flow. */
1338static void
1339bundle_flush_macs(struct ofbundle *bundle)
1340{
1341 struct ofproto_dpif *ofproto = bundle->ofproto;
1342 struct mac_learning *ml = ofproto->ml;
1343 struct mac_entry *mac, *next_mac;
1344
1345 ofproto->need_revalidate = true;
1346 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1347 if (mac->port.p == bundle) {
1348 mac_learning_expire(ml, mac);
1349 }
1350 }
1351}
1352
1353static struct ofbundle *
1354bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1355{
1356 struct ofbundle *bundle;
1357
1358 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1359 &ofproto->bundles) {
1360 if (bundle->aux == aux) {
1361 return bundle;
1362 }
1363 }
1364 return NULL;
1365}
1366
1367/* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1368 * ones that are found to 'bundles'. */
1369static void
1370bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1371 void **auxes, size_t n_auxes,
1372 struct hmapx *bundles)
1373{
1374 size_t i;
1375
1376 hmapx_init(bundles);
1377 for (i = 0; i < n_auxes; i++) {
1378 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1379 if (bundle) {
1380 hmapx_add(bundles, bundle);
1381 }
1382 }
1383}
1384
7bde8dd8
JP
1385static void
1386bundle_update(struct ofbundle *bundle)
1387{
1388 struct ofport_dpif *port;
1389
1390 bundle->floodable = true;
1391 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
5e48dc2b 1392 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
7bde8dd8
JP
1393 bundle->floodable = false;
1394 break;
1395 }
1396 }
1397}
1398
abe529af
BP
1399static void
1400bundle_del_port(struct ofport_dpif *port)
1401{
1402 struct ofbundle *bundle = port->bundle;
1403
6f77f4ae
BP
1404 bundle->ofproto->need_revalidate = true;
1405
abe529af
BP
1406 list_remove(&port->bundle_node);
1407 port->bundle = NULL;
1408
1409 if (bundle->lacp) {
1410 lacp_slave_unregister(bundle->lacp, port);
1411 }
1412 if (bundle->bond) {
1413 bond_slave_unregister(bundle->bond, port);
1414 }
1415
7bde8dd8 1416 bundle_update(bundle);
abe529af
BP
1417}
1418
1419static bool
1420bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
00794817
BP
1421 struct lacp_slave_settings *lacp,
1422 uint32_t bond_stable_id)
abe529af
BP
1423{
1424 struct ofport_dpif *port;
1425
1426 port = get_ofp_port(bundle->ofproto, ofp_port);
1427 if (!port) {
1428 return false;
1429 }
1430
1431 if (port->bundle != bundle) {
6f77f4ae 1432 bundle->ofproto->need_revalidate = true;
abe529af
BP
1433 if (port->bundle) {
1434 bundle_del_port(port);
1435 }
1436
1437 port->bundle = bundle;
1438 list_push_back(&bundle->ports, &port->bundle_node);
5e48dc2b 1439 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
abe529af
BP
1440 bundle->floodable = false;
1441 }
1442 }
1443 if (lacp) {
4a86aece 1444 port->bundle->ofproto->need_revalidate = true;
abe529af
BP
1445 lacp_slave_register(bundle->lacp, port, lacp);
1446 }
1447
00794817
BP
1448 port->bond_stable_id = bond_stable_id;
1449
abe529af
BP
1450 return true;
1451}
1452
1453static void
1454bundle_destroy(struct ofbundle *bundle)
1455{
1456 struct ofproto_dpif *ofproto;
1457 struct ofport_dpif *port, *next_port;
1458 int i;
1459
1460 if (!bundle) {
1461 return;
1462 }
1463
1464 ofproto = bundle->ofproto;
1465 for (i = 0; i < MAX_MIRRORS; i++) {
1466 struct ofmirror *m = ofproto->mirrors[i];
1467 if (m) {
1468 if (m->out == bundle) {
1469 mirror_destroy(m);
1470 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1471 || hmapx_find_and_delete(&m->dsts, bundle)) {
1472 ofproto->need_revalidate = true;
1473 }
1474 }
1475 }
1476
1477 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1478 bundle_del_port(port);
1479 }
1480
1481 bundle_flush_macs(bundle);
1482 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1483 free(bundle->name);
1484 free(bundle->trunks);
1485 lacp_destroy(bundle->lacp);
1486 bond_destroy(bundle->bond);
1487 free(bundle);
1488}
1489
1490static int
1491bundle_set(struct ofproto *ofproto_, void *aux,
1492 const struct ofproto_bundle_settings *s)
1493{
1494 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1495 bool need_flush = false;
abe529af
BP
1496 struct ofport_dpif *port;
1497 struct ofbundle *bundle;
ecac4ebf
BP
1498 unsigned long *trunks;
1499 int vlan;
abe529af
BP
1500 size_t i;
1501 bool ok;
1502
1503 if (!s) {
1504 bundle_destroy(bundle_lookup(ofproto, aux));
1505 return 0;
1506 }
1507
1508 assert(s->n_slaves == 1 || s->bond != NULL);
1509 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1510
1511 bundle = bundle_lookup(ofproto, aux);
1512 if (!bundle) {
1513 bundle = xmalloc(sizeof *bundle);
1514
1515 bundle->ofproto = ofproto;
1516 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1517 hash_pointer(aux, 0));
1518 bundle->aux = aux;
1519 bundle->name = NULL;
1520
1521 list_init(&bundle->ports);
ecac4ebf 1522 bundle->vlan_mode = PORT_VLAN_TRUNK;
abe529af
BP
1523 bundle->vlan = -1;
1524 bundle->trunks = NULL;
5e9ceccd 1525 bundle->use_priority_tags = s->use_priority_tags;
abe529af
BP
1526 bundle->lacp = NULL;
1527 bundle->bond = NULL;
1528
1529 bundle->floodable = true;
1530
1531 bundle->src_mirrors = 0;
1532 bundle->dst_mirrors = 0;
1533 bundle->mirror_out = 0;
1534 }
1535
1536 if (!bundle->name || strcmp(s->name, bundle->name)) {
1537 free(bundle->name);
1538 bundle->name = xstrdup(s->name);
1539 }
1540
1541 /* LACP. */
1542 if (s->lacp) {
1543 if (!bundle->lacp) {
8c977421 1544 ofproto->need_revalidate = true;
abe529af
BP
1545 bundle->lacp = lacp_create();
1546 }
1547 lacp_configure(bundle->lacp, s->lacp);
1548 } else {
1549 lacp_destroy(bundle->lacp);
1550 bundle->lacp = NULL;
1551 }
1552
1553 /* Update set of ports. */
1554 ok = true;
1555 for (i = 0; i < s->n_slaves; i++) {
1556 if (!bundle_add_port(bundle, s->slaves[i],
00794817
BP
1557 s->lacp ? &s->lacp_slaves[i] : NULL,
1558 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
abe529af
BP
1559 ok = false;
1560 }
1561 }
1562 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1563 struct ofport_dpif *next_port;
1564
1565 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1566 for (i = 0; i < s->n_slaves; i++) {
56c769ab 1567 if (s->slaves[i] == port->up.ofp_port) {
abe529af
BP
1568 goto found;
1569 }
1570 }
1571
1572 bundle_del_port(port);
1573 found: ;
1574 }
1575 }
1576 assert(list_size(&bundle->ports) <= s->n_slaves);
1577
1578 if (list_is_empty(&bundle->ports)) {
1579 bundle_destroy(bundle);
1580 return EINVAL;
1581 }
1582
ecac4ebf 1583 /* Set VLAN tagging mode */
5e9ceccd
BP
1584 if (s->vlan_mode != bundle->vlan_mode
1585 || s->use_priority_tags != bundle->use_priority_tags) {
ecac4ebf 1586 bundle->vlan_mode = s->vlan_mode;
5e9ceccd 1587 bundle->use_priority_tags = s->use_priority_tags;
ecac4ebf
BP
1588 need_flush = true;
1589 }
1590
abe529af 1591 /* Set VLAN tag. */
ecac4ebf
BP
1592 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1593 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1594 : 0);
1595 if (vlan != bundle->vlan) {
1596 bundle->vlan = vlan;
abe529af
BP
1597 need_flush = true;
1598 }
1599
1600 /* Get trunked VLANs. */
ecac4ebf
BP
1601 switch (s->vlan_mode) {
1602 case PORT_VLAN_ACCESS:
1603 trunks = NULL;
1604 break;
1605
1606 case PORT_VLAN_TRUNK:
1607 trunks = (unsigned long *) s->trunks;
1608 break;
1609
1610 case PORT_VLAN_NATIVE_UNTAGGED:
1611 case PORT_VLAN_NATIVE_TAGGED:
1612 if (vlan != 0 && (!s->trunks
1613 || !bitmap_is_set(s->trunks, vlan)
1614 || bitmap_is_set(s->trunks, 0))) {
1615 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1616 if (s->trunks) {
1617 trunks = bitmap_clone(s->trunks, 4096);
1618 } else {
1619 trunks = bitmap_allocate1(4096);
1620 }
1621 bitmap_set1(trunks, vlan);
1622 bitmap_set0(trunks, 0);
1623 } else {
1624 trunks = (unsigned long *) s->trunks;
1625 }
1626 break;
1627
1628 default:
1629 NOT_REACHED();
1630 }
abe529af
BP
1631 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1632 free(bundle->trunks);
ecac4ebf
BP
1633 if (trunks == s->trunks) {
1634 bundle->trunks = vlan_bitmap_clone(trunks);
1635 } else {
1636 bundle->trunks = trunks;
1637 trunks = NULL;
1638 }
abe529af
BP
1639 need_flush = true;
1640 }
ecac4ebf
BP
1641 if (trunks != s->trunks) {
1642 free(trunks);
1643 }
abe529af
BP
1644
1645 /* Bonding. */
1646 if (!list_is_short(&bundle->ports)) {
1647 bundle->ofproto->has_bonded_bundles = true;
1648 if (bundle->bond) {
1649 if (bond_reconfigure(bundle->bond, s->bond)) {
1650 ofproto->need_revalidate = true;
1651 }
1652 } else {
1653 bundle->bond = bond_create(s->bond);
6f77f4ae 1654 ofproto->need_revalidate = true;
abe529af
BP
1655 }
1656
1657 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
00794817 1658 bond_slave_register(bundle->bond, port, port->bond_stable_id,
abe529af
BP
1659 port->up.netdev);
1660 }
1661 } else {
1662 bond_destroy(bundle->bond);
1663 bundle->bond = NULL;
1664 }
1665
1666 /* If we changed something that would affect MAC learning, un-learn
1667 * everything on this port and force flow revalidation. */
1668 if (need_flush) {
1669 bundle_flush_macs(bundle);
1670 }
1671
1672 return 0;
1673}
1674
1675static void
1676bundle_remove(struct ofport *port_)
1677{
1678 struct ofport_dpif *port = ofport_dpif_cast(port_);
1679 struct ofbundle *bundle = port->bundle;
1680
1681 if (bundle) {
1682 bundle_del_port(port);
1683 if (list_is_empty(&bundle->ports)) {
1684 bundle_destroy(bundle);
1685 } else if (list_is_short(&bundle->ports)) {
1686 bond_destroy(bundle->bond);
1687 bundle->bond = NULL;
1688 }
1689 }
1690}
1691
1692static void
5f877369 1693send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
abe529af
BP
1694{
1695 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1696 struct ofport_dpif *port = port_;
1697 uint8_t ea[ETH_ADDR_LEN];
1698 int error;
1699
1700 error = netdev_get_etheraddr(port->up.netdev, ea);
1701 if (!error) {
abe529af 1702 struct ofpbuf packet;
5f877369 1703 void *packet_pdu;
abe529af
BP
1704
1705 ofpbuf_init(&packet, 0);
1706 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
5f877369
EJ
1707 pdu_size);
1708 memcpy(packet_pdu, pdu, pdu_size);
1709
97d6520b 1710 send_packet(port, &packet);
abe529af
BP
1711 ofpbuf_uninit(&packet);
1712 } else {
1713 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1714 "%s (%s)", port->bundle->name,
1715 netdev_get_name(port->up.netdev), strerror(error));
1716 }
1717}
1718
1719static void
1720bundle_send_learning_packets(struct ofbundle *bundle)
1721{
1722 struct ofproto_dpif *ofproto = bundle->ofproto;
1723 int error, n_packets, n_errors;
1724 struct mac_entry *e;
1725
1726 error = n_packets = n_errors = 0;
1727 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1728 if (e->port.p != bundle) {
ea131871
JG
1729 struct ofpbuf *learning_packet;
1730 struct ofport_dpif *port;
1731 int ret;
1732
1733 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1734 e->vlan,
1735 (void **)&port);
97d6520b 1736 ret = send_packet(port, learning_packet);
ea131871 1737 ofpbuf_delete(learning_packet);
abe529af
BP
1738 if (ret) {
1739 error = ret;
1740 n_errors++;
1741 }
1742 n_packets++;
1743 }
1744 }
1745
1746 if (n_errors) {
1747 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1748 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1749 "packets, last error was: %s",
1750 bundle->name, n_errors, n_packets, strerror(error));
1751 } else {
1752 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1753 bundle->name, n_packets);
1754 }
1755}
1756
1757static void
1758bundle_run(struct ofbundle *bundle)
1759{
1760 if (bundle->lacp) {
1761 lacp_run(bundle->lacp, send_pdu_cb);
1762 }
1763 if (bundle->bond) {
1764 struct ofport_dpif *port;
1765
1766 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
015e08bc 1767 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
abe529af
BP
1768 }
1769
1770 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1771 lacp_negotiated(bundle->lacp));
1772 if (bond_should_send_learning_packets(bundle->bond)) {
1773 bundle_send_learning_packets(bundle);
1774 }
1775 }
1776}
1777
1778static void
1779bundle_wait(struct ofbundle *bundle)
1780{
1781 if (bundle->lacp) {
1782 lacp_wait(bundle->lacp);
1783 }
1784 if (bundle->bond) {
1785 bond_wait(bundle->bond);
1786 }
1787}
1788\f
1789/* Mirrors. */
1790
1791static int
1792mirror_scan(struct ofproto_dpif *ofproto)
1793{
1794 int idx;
1795
1796 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1797 if (!ofproto->mirrors[idx]) {
1798 return idx;
1799 }
1800 }
1801 return -1;
1802}
1803
1804static struct ofmirror *
1805mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1806{
1807 int i;
1808
1809 for (i = 0; i < MAX_MIRRORS; i++) {
1810 struct ofmirror *mirror = ofproto->mirrors[i];
1811 if (mirror && mirror->aux == aux) {
1812 return mirror;
1813 }
1814 }
1815
1816 return NULL;
1817}
1818
9ba15e2a
BP
1819/* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1820static void
1821mirror_update_dups(struct ofproto_dpif *ofproto)
1822{
1823 int i;
1824
1825 for (i = 0; i < MAX_MIRRORS; i++) {
1826 struct ofmirror *m = ofproto->mirrors[i];
1827
1828 if (m) {
1829 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1830 }
1831 }
1832
1833 for (i = 0; i < MAX_MIRRORS; i++) {
1834 struct ofmirror *m1 = ofproto->mirrors[i];
1835 int j;
1836
1837 if (!m1) {
1838 continue;
1839 }
1840
1841 for (j = i + 1; j < MAX_MIRRORS; j++) {
1842 struct ofmirror *m2 = ofproto->mirrors[j];
1843
edb0540b 1844 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
9ba15e2a
BP
1845 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1846 m2->dup_mirrors |= m1->dup_mirrors;
1847 }
1848 }
1849 }
1850}
1851
abe529af
BP
1852static int
1853mirror_set(struct ofproto *ofproto_, void *aux,
1854 const struct ofproto_mirror_settings *s)
1855{
1856 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1857 mirror_mask_t mirror_bit;
1858 struct ofbundle *bundle;
1859 struct ofmirror *mirror;
1860 struct ofbundle *out;
1861 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1862 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1863 int out_vlan;
1864
1865 mirror = mirror_lookup(ofproto, aux);
1866 if (!s) {
1867 mirror_destroy(mirror);
1868 return 0;
1869 }
1870 if (!mirror) {
1871 int idx;
1872
1873 idx = mirror_scan(ofproto);
1874 if (idx < 0) {
1875 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1876 "cannot create %s",
1877 ofproto->up.name, MAX_MIRRORS, s->name);
1878 return EFBIG;
1879 }
1880
1881 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1882 mirror->ofproto = ofproto;
1883 mirror->idx = idx;
8b28d864 1884 mirror->aux = aux;
abe529af
BP
1885 mirror->out_vlan = -1;
1886 mirror->name = NULL;
1887 }
1888
1889 if (!mirror->name || strcmp(s->name, mirror->name)) {
1890 free(mirror->name);
1891 mirror->name = xstrdup(s->name);
1892 }
1893
1894 /* Get the new configuration. */
1895 if (s->out_bundle) {
1896 out = bundle_lookup(ofproto, s->out_bundle);
1897 if (!out) {
1898 mirror_destroy(mirror);
1899 return EINVAL;
1900 }
1901 out_vlan = -1;
1902 } else {
1903 out = NULL;
1904 out_vlan = s->out_vlan;
1905 }
1906 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1907 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1908
1909 /* If the configuration has not changed, do nothing. */
1910 if (hmapx_equals(&srcs, &mirror->srcs)
1911 && hmapx_equals(&dsts, &mirror->dsts)
1912 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1913 && mirror->out == out
1914 && mirror->out_vlan == out_vlan)
1915 {
1916 hmapx_destroy(&srcs);
1917 hmapx_destroy(&dsts);
1918 return 0;
1919 }
1920
1921 hmapx_swap(&srcs, &mirror->srcs);
1922 hmapx_destroy(&srcs);
1923
1924 hmapx_swap(&dsts, &mirror->dsts);
1925 hmapx_destroy(&dsts);
1926
1927 free(mirror->vlans);
1928 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1929
1930 mirror->out = out;
1931 mirror->out_vlan = out_vlan;
1932
1933 /* Update bundles. */
1934 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1935 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1936 if (hmapx_contains(&mirror->srcs, bundle)) {
1937 bundle->src_mirrors |= mirror_bit;
1938 } else {
1939 bundle->src_mirrors &= ~mirror_bit;
1940 }
1941
1942 if (hmapx_contains(&mirror->dsts, bundle)) {
1943 bundle->dst_mirrors |= mirror_bit;
1944 } else {
1945 bundle->dst_mirrors &= ~mirror_bit;
1946 }
1947
1948 if (mirror->out == bundle) {
1949 bundle->mirror_out |= mirror_bit;
1950 } else {
1951 bundle->mirror_out &= ~mirror_bit;
1952 }
1953 }
1954
1955 ofproto->need_revalidate = true;
1956 mac_learning_flush(ofproto->ml);
9ba15e2a 1957 mirror_update_dups(ofproto);
abe529af
BP
1958
1959 return 0;
1960}
1961
1962static void
1963mirror_destroy(struct ofmirror *mirror)
1964{
1965 struct ofproto_dpif *ofproto;
1966 mirror_mask_t mirror_bit;
1967 struct ofbundle *bundle;
1968
1969 if (!mirror) {
1970 return;
1971 }
1972
1973 ofproto = mirror->ofproto;
1974 ofproto->need_revalidate = true;
1975 mac_learning_flush(ofproto->ml);
1976
1977 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1978 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1979 bundle->src_mirrors &= ~mirror_bit;
1980 bundle->dst_mirrors &= ~mirror_bit;
1981 bundle->mirror_out &= ~mirror_bit;
1982 }
1983
1984 hmapx_destroy(&mirror->srcs);
1985 hmapx_destroy(&mirror->dsts);
1986 free(mirror->vlans);
1987
1988 ofproto->mirrors[mirror->idx] = NULL;
1989 free(mirror->name);
1990 free(mirror);
9ba15e2a
BP
1991
1992 mirror_update_dups(ofproto);
abe529af
BP
1993}
1994
1995static int
1996set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1997{
1998 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1999 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2000 ofproto->need_revalidate = true;
2001 mac_learning_flush(ofproto->ml);
2002 }
2003 return 0;
2004}
2005
2006static bool
b4affc74 2007is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
abe529af
BP
2008{
2009 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2010 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2011 return bundle && bundle->mirror_out != 0;
2012}
8402c74b
SS
2013
2014static void
b53055f4 2015forward_bpdu_changed(struct ofproto *ofproto_)
8402c74b
SS
2016{
2017 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2018 /* Revalidate cached flows whenever forward_bpdu option changes. */
2019 ofproto->need_revalidate = true;
2020}
abe529af
BP
2021\f
2022/* Ports. */
2023
2024static struct ofport_dpif *
2025get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
2026{
7df6a8bd
BP
2027 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2028 return ofport ? ofport_dpif_cast(ofport) : NULL;
abe529af
BP
2029}
2030
2031static struct ofport_dpif *
2032get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
2033{
2034 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2035}
2036
2037static void
2038ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2039 struct dpif_port *dpif_port)
2040{
2041 ofproto_port->name = dpif_port->name;
2042 ofproto_port->type = dpif_port->type;
2043 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2044}
2045
2046static void
2047port_run(struct ofport_dpif *ofport)
2048{
015e08bc
EJ
2049 bool enable = netdev_get_carrier(ofport->up.netdev);
2050
abe529af
BP
2051 if (ofport->cfm) {
2052 cfm_run(ofport->cfm);
2053
2054 if (cfm_should_send_ccm(ofport->cfm)) {
2055 struct ofpbuf packet;
abe529af
BP
2056
2057 ofpbuf_init(&packet, 0);
c0a2e71d 2058 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
97d6520b 2059 send_packet(ofport, &packet);
abe529af
BP
2060 ofpbuf_uninit(&packet);
2061 }
015e08bc 2062
86dc6501
EJ
2063 enable = enable && !cfm_get_fault(ofport->cfm)
2064 && cfm_get_opup(ofport->cfm);
abe529af 2065 }
015e08bc
EJ
2066
2067 if (ofport->bundle) {
2068 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2069 }
2070
daff3353
EJ
2071 if (ofport->may_enable != enable) {
2072 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2073
2074 if (ofproto->has_bundle_action) {
2075 ofproto->need_revalidate = true;
2076 }
2077 }
2078
015e08bc 2079 ofport->may_enable = enable;
abe529af
BP
2080}
2081
2082static void
2083port_wait(struct ofport_dpif *ofport)
2084{
2085 if (ofport->cfm) {
2086 cfm_wait(ofport->cfm);
2087 }
2088}
2089
2090static int
2091port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2092 struct ofproto_port *ofproto_port)
2093{
2094 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2095 struct dpif_port dpif_port;
2096 int error;
2097
2098 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2099 if (!error) {
2100 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2101 }
2102 return error;
2103}
2104
2105static int
2106port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2107{
2108 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2109 uint16_t odp_port;
2110 int error;
2111
2112 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2113 if (!error) {
2114 *ofp_portp = odp_port_to_ofp_port(odp_port);
2115 }
2116 return error;
2117}
2118
2119static int
2120port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2121{
2122 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2123 int error;
2124
2125 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2126 if (!error) {
2127 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2128 if (ofport) {
2129 /* The caller is going to close ofport->up.netdev. If this is a
2130 * bonded port, then the bond is using that netdev, so remove it
2131 * from the bond. The client will need to reconfigure everything
2132 * after deleting ports, so then the slave will get re-added. */
2133 bundle_remove(&ofport->up);
2134 }
2135 }
2136 return error;
2137}
2138
2139struct port_dump_state {
2140 struct dpif_port_dump dump;
2141 bool done;
2142};
2143
2144static int
2145port_dump_start(const struct ofproto *ofproto_, void **statep)
2146{
2147 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2148 struct port_dump_state *state;
2149
2150 *statep = state = xmalloc(sizeof *state);
2151 dpif_port_dump_start(&state->dump, ofproto->dpif);
2152 state->done = false;
2153 return 0;
2154}
2155
2156static int
2157port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2158 struct ofproto_port *port)
2159{
2160 struct port_dump_state *state = state_;
2161 struct dpif_port dpif_port;
2162
2163 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2164 ofproto_port_from_dpif_port(port, &dpif_port);
2165 return 0;
2166 } else {
2167 int error = dpif_port_dump_done(&state->dump);
2168 state->done = true;
2169 return error ? error : EOF;
2170 }
2171}
2172
2173static int
2174port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2175{
2176 struct port_dump_state *state = state_;
2177
2178 if (!state->done) {
2179 dpif_port_dump_done(&state->dump);
2180 }
2181 free(state);
2182 return 0;
2183}
2184
2185static int
2186port_poll(const struct ofproto *ofproto_, char **devnamep)
2187{
2188 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2189 return dpif_port_poll(ofproto->dpif, devnamep);
2190}
2191
2192static void
2193port_poll_wait(const struct ofproto *ofproto_)
2194{
2195 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2196 dpif_port_poll_wait(ofproto->dpif);
2197}
2198
2199static int
2200port_is_lacp_current(const struct ofport *ofport_)
2201{
2202 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2203 return (ofport->bundle && ofport->bundle->lacp
2204 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2205 : -1);
2206}
2207\f
2208/* Upcall handling. */
2209
501f8d1f
BP
2210/* Flow miss batching.
2211 *
2212 * Some dpifs implement operations faster when you hand them off in a batch.
2213 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2214 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2215 * more packets, plus possibly installing the flow in the dpif.
2216 *
2217 * So far we only batch the operations that affect flow setup time the most.
2218 * It's possible to batch more than that, but the benefit might be minimal. */
2219struct flow_miss {
2220 struct hmap_node hmap_node;
2221 struct flow flow;
b0f7b9b5 2222 enum odp_key_fitness key_fitness;
501f8d1f
BP
2223 const struct nlattr *key;
2224 size_t key_len;
e84173dc 2225 ovs_be16 initial_tci;
501f8d1f
BP
2226 struct list packets;
2227};
2228
2229struct flow_miss_op {
2230 union dpif_op dpif_op;
b0f7b9b5 2231 struct subfacet *subfacet;
501f8d1f
BP
2232};
2233
62cd7072
BP
2234/* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2235 * OpenFlow controller as necessary according to their individual
2236 * configurations.
2237 *
2238 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2239 * ownership is transferred to this function. */
2240static void
2241send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2242 const struct flow *flow, bool clone)
2243{
2244 struct ofputil_packet_in pin;
2245
2246 pin.packet = packet;
2247 pin.in_port = flow->in_port;
2248 pin.reason = OFPR_NO_MATCH;
2249 pin.buffer_id = 0; /* not yet known */
2250 pin.send_len = 0; /* not used for flow table misses */
2251 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2252 clone ? NULL : packet);
2253}
2254
2255/* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2256 * OpenFlow controller as necessary according to their individual
2257 * configurations.
2258 *
2259 * 'send_len' should be the number of bytes of 'packet' to send to the
2260 * controller, as specified in the action that caused the packet to be sent.
abe529af
BP
2261 *
2262 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2263 * Otherwise, ownership is transferred to this function. */
2264static void
62cd7072
BP
2265send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2266 uint64_t userdata, const struct flow *flow, bool clone)
abe529af
BP
2267{
2268 struct ofputil_packet_in pin;
6ff686f2 2269 struct user_action_cookie cookie;
abe529af 2270
62cd7072
BP
2271 memcpy(&cookie, &userdata, sizeof(cookie));
2272
2273 pin.packet = packet;
abe529af 2274 pin.in_port = flow->in_port;
62cd7072 2275 pin.reason = OFPR_ACTION;
abe529af 2276 pin.buffer_id = 0; /* not yet known */
6ff686f2 2277 pin.send_len = cookie.data;
78bd1cd0 2278 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
62cd7072 2279 clone ? NULL : packet);
abe529af
BP
2280}
2281
2282static bool
2283process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2284 const struct ofpbuf *packet)
2285{
b6e001b6
EJ
2286 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2287
2288 if (!ofport) {
2289 return false;
2290 }
2291
ef9819b5 2292 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
b6e001b6 2293 if (packet) {
abe529af
BP
2294 cfm_process_heartbeat(ofport->cfm, packet);
2295 }
2296 return true;
b6e001b6
EJ
2297 } else if (ofport->bundle && ofport->bundle->lacp
2298 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2299 if (packet) {
2300 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
abe529af 2301 }
da37ebac 2302 return true;
21f7563c
JP
2303 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2304 if (packet) {
2305 stp_process_packet(ofport, packet);
2306 }
2307 return true;
abe529af
BP
2308 }
2309 return false;
2310}
2311
501f8d1f
BP
2312static struct flow_miss *
2313flow_miss_create(struct hmap *todo, const struct flow *flow,
b0f7b9b5 2314 enum odp_key_fitness key_fitness,
e84173dc
BP
2315 const struct nlattr *key, size_t key_len,
2316 ovs_be16 initial_tci)
abe529af 2317{
501f8d1f
BP
2318 uint32_t hash = flow_hash(flow, 0);
2319 struct flow_miss *miss;
abe529af 2320
501f8d1f
BP
2321 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2322 if (flow_equal(&miss->flow, flow)) {
2323 return miss;
2324 }
2325 }
abe529af 2326
501f8d1f
BP
2327 miss = xmalloc(sizeof *miss);
2328 hmap_insert(todo, &miss->hmap_node, hash);
2329 miss->flow = *flow;
b0f7b9b5 2330 miss->key_fitness = key_fitness;
501f8d1f
BP
2331 miss->key = key;
2332 miss->key_len = key_len;
e84173dc 2333 miss->initial_tci = initial_tci;
501f8d1f
BP
2334 list_init(&miss->packets);
2335 return miss;
2336}
abe529af 2337
501f8d1f
BP
2338static void
2339handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2340 struct flow_miss_op *ops, size_t *n_ops)
2341{
2342 const struct flow *flow = &miss->flow;
2343 struct ofpbuf *packet, *next_packet;
b0f7b9b5 2344 struct subfacet *subfacet;
501f8d1f 2345 struct facet *facet;
abe529af 2346
501f8d1f 2347 facet = facet_lookup_valid(ofproto, flow);
abe529af 2348 if (!facet) {
501f8d1f
BP
2349 struct rule_dpif *rule;
2350
2351 rule = rule_dpif_lookup(ofproto, flow, 0);
abe529af
BP
2352 if (!rule) {
2353 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
501f8d1f 2354 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
abe529af
BP
2355 if (port) {
2356 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2357 COVERAGE_INC(ofproto_dpif_no_packet_in);
2358 /* XXX install 'drop' flow entry */
abe529af
BP
2359 return;
2360 }
2361 } else {
2362 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
501f8d1f
BP
2363 flow->in_port);
2364 }
2365
2366 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2367 &miss->packets) {
2368 list_remove(&packet->list_node);
2369 send_packet_in_miss(ofproto, packet, flow, false);
abe529af
BP
2370 }
2371
abe529af
BP
2372 return;
2373 }
2374
501f8d1f 2375 facet = facet_create(rule, flow);
abe529af
BP
2376 }
2377
b0f7b9b5 2378 subfacet = subfacet_create(ofproto, facet,
e84173dc
BP
2379 miss->key_fitness, miss->key, miss->key_len,
2380 miss->initial_tci);
b0f7b9b5 2381
501f8d1f
BP
2382 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2383 list_remove(&packet->list_node);
2384 ofproto->n_matches++;
2385
2386 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2387 /*
2388 * Extra-special case for fail-open mode.
2389 *
2390 * We are in fail-open mode and the packet matched the fail-open
2391 * rule, but we are connected to a controller too. We should send
2392 * the packet up to the controller in the hope that it will try to
2393 * set up a flow and thereby allow us to exit fail-open.
2394 *
2395 * See the top-level comment in fail-open.c for more information.
2396 */
2397 send_packet_in_miss(ofproto, packet, flow, true);
2398 }
2399
b95fc6ba
BP
2400 if (!facet->may_install || !subfacet->actions) {
2401 subfacet_make_actions(ofproto, subfacet, packet);
501f8d1f
BP
2402 }
2403 if (!execute_controller_action(ofproto, &facet->flow,
b95fc6ba
BP
2404 subfacet->actions,
2405 subfacet->actions_len, packet)) {
501f8d1f
BP
2406 struct flow_miss_op *op = &ops[(*n_ops)++];
2407 struct dpif_execute *execute = &op->dpif_op.execute;
2408
b0f7b9b5 2409 op->subfacet = subfacet;
501f8d1f
BP
2410 execute->type = DPIF_OP_EXECUTE;
2411 execute->key = miss->key;
2412 execute->key_len = miss->key_len;
2413 execute->actions
2414 = (facet->may_install
b95fc6ba
BP
2415 ? subfacet->actions
2416 : xmemdup(subfacet->actions, subfacet->actions_len));
2417 execute->actions_len = subfacet->actions_len;
501f8d1f
BP
2418 execute->packet = packet;
2419 }
2420 }
2421
b0f7b9b5 2422 if (facet->may_install && subfacet->key_fitness != ODP_FIT_TOO_LITTLE) {
501f8d1f
BP
2423 struct flow_miss_op *op = &ops[(*n_ops)++];
2424 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2425
b0f7b9b5 2426 op->subfacet = subfacet;
501f8d1f
BP
2427 put->type = DPIF_OP_FLOW_PUT;
2428 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2429 put->key = miss->key;
2430 put->key_len = miss->key_len;
b95fc6ba
BP
2431 put->actions = subfacet->actions;
2432 put->actions_len = subfacet->actions_len;
501f8d1f
BP
2433 put->stats = NULL;
2434 }
2435}
2436
e84173dc
BP
2437static enum odp_key_fitness
2438ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto OVS_UNUSED,
2439 const struct nlattr *key, size_t key_len,
2440 struct flow *flow, ovs_be16 *initial_tci)
2441{
2442 enum odp_key_fitness fitness;
2443
2444 fitness = odp_flow_key_to_flow(key, key_len, flow);
2445 if (fitness == ODP_FIT_ERROR) {
2446 return fitness;
2447 }
2448 *initial_tci = flow->vlan_tci;
2449
2450 return fitness;
2451}
2452
501f8d1f
BP
2453static void
2454handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2455 size_t n_upcalls)
2456{
2457 struct dpif_upcall *upcall;
2458 struct flow_miss *miss, *next_miss;
2459 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2460 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2461 struct hmap todo;
2462 size_t n_ops;
2463 size_t i;
2464
2465 if (!n_upcalls) {
2466 return;
2467 }
2468
2469 /* Construct the to-do list.
2470 *
2471 * This just amounts to extracting the flow from each packet and sticking
2472 * the packets that have the same flow in the same "flow_miss" structure so
2473 * that we can process them together. */
2474 hmap_init(&todo);
2475 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
b0f7b9b5 2476 enum odp_key_fitness fitness;
501f8d1f 2477 struct flow_miss *miss;
e84173dc 2478 ovs_be16 initial_tci;
501f8d1f
BP
2479 struct flow flow;
2480
b0f7b9b5
BP
2481 /* Obtain metadata and check userspace/kernel agreement on flow match,
2482 * then set 'flow''s header pointers. */
e84173dc
BP
2483 fitness = ofproto_dpif_extract_flow_key(ofproto,
2484 upcall->key, upcall->key_len,
2485 &flow, &initial_tci);
b0f7b9b5
BP
2486 if (fitness == ODP_FIT_ERROR) {
2487 continue;
2488 }
abff858b
PS
2489 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2490 flow.in_port, &flow);
501f8d1f 2491
21f7563c 2492 /* Handle 802.1ag, LACP, and STP specially. */
501f8d1f
BP
2493 if (process_special(ofproto, &flow, upcall->packet)) {
2494 ofpbuf_delete(upcall->packet);
2495 ofproto->n_matches++;
2496 continue;
2497 }
2498
2499 /* Add other packets to a to-do list. */
b0f7b9b5 2500 miss = flow_miss_create(&todo, &flow, fitness,
e84173dc 2501 upcall->key, upcall->key_len, initial_tci);
501f8d1f
BP
2502 list_push_back(&miss->packets, &upcall->packet->list_node);
2503 }
2504
2505 /* Process each element in the to-do list, constructing the set of
2506 * operations to batch. */
2507 n_ops = 0;
2508 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2509 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2510 ofpbuf_list_delete(&miss->packets);
2511 hmap_remove(&todo, &miss->hmap_node);
2512 free(miss);
abe529af 2513 }
501f8d1f
BP
2514 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2515 hmap_destroy(&todo);
2516
2517 /* Execute batch. */
2518 for (i = 0; i < n_ops; i++) {
2519 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2520 }
2521 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2522
2523 /* Free memory and update facets. */
2524 for (i = 0; i < n_ops; i++) {
2525 struct flow_miss_op *op = &flow_miss_ops[i];
2526 struct dpif_execute *execute;
2527 struct dpif_flow_put *put;
2528
2529 switch (op->dpif_op.type) {
2530 case DPIF_OP_EXECUTE:
2531 execute = &op->dpif_op.execute;
b95fc6ba 2532 if (op->subfacet->actions != execute->actions) {
501f8d1f
BP
2533 free((struct nlattr *) execute->actions);
2534 }
2535 ofpbuf_delete((struct ofpbuf *) execute->packet);
2536 break;
abe529af 2537
501f8d1f
BP
2538 case DPIF_OP_FLOW_PUT:
2539 put = &op->dpif_op.flow_put;
2540 if (!put->error) {
b0f7b9b5 2541 op->subfacet->installed = true;
501f8d1f
BP
2542 }
2543 break;
2544 }
2545 }
abe529af
BP
2546}
2547
2548static void
6ff686f2
PS
2549handle_userspace_upcall(struct ofproto_dpif *ofproto,
2550 struct dpif_upcall *upcall)
abe529af 2551{
6ff686f2 2552 struct user_action_cookie cookie;
e84173dc
BP
2553 enum odp_key_fitness fitness;
2554 ovs_be16 initial_tci;
2555 struct flow flow;
abe529af 2556
6ff686f2 2557 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
abe529af 2558
e84173dc
BP
2559 fitness = ofproto_dpif_extract_flow_key(ofproto, upcall->key,
2560 upcall->key_len, &flow,
2561 &initial_tci);
2562 if (fitness == ODP_FIT_ERROR) {
2563 return;
2564 }
2565
6ff686f2 2566 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
abe529af 2567 if (ofproto->sflow) {
e84173dc
BP
2568 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
2569 &cookie);
abe529af
BP
2570 }
2571 ofpbuf_delete(upcall->packet);
6ff686f2
PS
2572 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2573 COVERAGE_INC(ofproto_dpif_ctlr_action);
62cd7072
BP
2574 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2575 &flow, false);
6ff686f2
PS
2576 } else {
2577 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2578 }
2579}
2580
2581static void
2582handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2583{
2584 switch (upcall->type) {
2585 case DPIF_UC_ACTION:
2586 handle_userspace_upcall(ofproto, upcall);
abe529af
BP
2587 break;
2588
2589 case DPIF_UC_MISS:
501f8d1f
BP
2590 /* The caller handles these. */
2591 NOT_REACHED();
abe529af
BP
2592
2593 case DPIF_N_UC_TYPES:
2594 default:
2595 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2596 break;
2597 }
2598}
2599\f
2600/* Flow expiration. */
2601
b0f7b9b5 2602static int subfacet_max_idle(const struct ofproto_dpif *);
abe529af
BP
2603static void update_stats(struct ofproto_dpif *);
2604static void rule_expire(struct rule_dpif *);
b0f7b9b5 2605static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
abe529af
BP
2606
2607/* This function is called periodically by run(). Its job is to collect
2608 * updates for the flows that have been installed into the datapath, most
2609 * importantly when they last were used, and then use that information to
2610 * expire flows that have not been used recently.
2611 *
2612 * Returns the number of milliseconds after which it should be called again. */
2613static int
2614expire(struct ofproto_dpif *ofproto)
2615{
2616 struct rule_dpif *rule, *next_rule;
0697b5c3 2617 struct classifier *table;
abe529af
BP
2618 int dp_max_idle;
2619
2620 /* Update stats for each flow in the datapath. */
2621 update_stats(ofproto);
2622
b0f7b9b5
BP
2623 /* Expire subfacets that have been idle too long. */
2624 dp_max_idle = subfacet_max_idle(ofproto);
2625 expire_subfacets(ofproto, dp_max_idle);
abe529af
BP
2626
2627 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
0697b5c3
BP
2628 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2629 struct cls_cursor cursor;
2630
2631 cls_cursor_init(&cursor, table, NULL);
2632 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2633 rule_expire(rule);
2634 }
abe529af
BP
2635 }
2636
2637 /* All outstanding data in existing flows has been accounted, so it's a
2638 * good time to do bond rebalancing. */
2639 if (ofproto->has_bonded_bundles) {
2640 struct ofbundle *bundle;
2641
2642 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2643 if (bundle->bond) {
2644 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2645 }
2646 }
2647 }
2648
2649 return MIN(dp_max_idle, 1000);
2650}
2651
2652/* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2653 *
2654 * This function also pushes statistics updates to rules which each facet
2655 * resubmits into. Generally these statistics will be accurate. However, if a
2656 * facet changes the rule it resubmits into at some time in between
2657 * update_stats() runs, it is possible that statistics accrued to the
2658 * old rule will be incorrectly attributed to the new rule. This could be
2659 * avoided by calling update_stats() whenever rules are created or
2660 * deleted. However, the performance impact of making so many calls to the
2661 * datapath do not justify the benefit of having perfectly accurate statistics.
2662 */
2663static void
2664update_stats(struct ofproto_dpif *p)
2665{
2666 const struct dpif_flow_stats *stats;
2667 struct dpif_flow_dump dump;
2668 const struct nlattr *key;
2669 size_t key_len;
2670
2671 dpif_flow_dump_start(&dump, p->dpif);
2672 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
b0f7b9b5
BP
2673 enum odp_key_fitness fitness;
2674 struct subfacet *subfacet;
abe529af
BP
2675 struct flow flow;
2676
b0f7b9b5
BP
2677 fitness = odp_flow_key_to_flow(key, key_len, &flow);
2678 if (fitness == ODP_FIT_ERROR) {
abe529af
BP
2679 continue;
2680 }
abe529af 2681
b0f7b9b5
BP
2682 subfacet = subfacet_find(p, key, key_len, &flow);
2683 if (subfacet && subfacet->installed) {
2684 struct facet *facet = subfacet->facet;
abe529af 2685
b0f7b9b5
BP
2686 if (stats->n_packets >= subfacet->dp_packet_count) {
2687 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
abe529af
BP
2688 facet->packet_count += extra;
2689 } else {
2690 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2691 }
2692
b0f7b9b5
BP
2693 if (stats->n_bytes >= subfacet->dp_byte_count) {
2694 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
abe529af
BP
2695 } else {
2696 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2697 }
2698
b0f7b9b5
BP
2699 subfacet->dp_packet_count = stats->n_packets;
2700 subfacet->dp_byte_count = stats->n_bytes;
abe529af 2701
b0f7b9b5 2702 subfacet_update_time(p, subfacet, stats->used);
55af77bb 2703 facet_account(p, facet);
abe529af
BP
2704 facet_push_stats(facet);
2705 } else {
b0f7b9b5
BP
2706 /* There's a flow in the datapath that we know nothing about, or a
2707 * flow that shouldn't be installed but was anyway. Delete it. */
abe529af
BP
2708 COVERAGE_INC(facet_unexpected);
2709 dpif_flow_del(p->dpif, key, key_len, NULL);
2710 }
2711 }
2712 dpif_flow_dump_done(&dump);
2713}
2714
2715/* Calculates and returns the number of milliseconds of idle time after which
b0f7b9b5
BP
2716 * subfacets should expire from the datapath. When a subfacet expires, we fold
2717 * its statistics into its facet, and when a facet's last subfacet expires, we
2718 * fold its statistic into its rule. */
abe529af 2719static int
b0f7b9b5 2720subfacet_max_idle(const struct ofproto_dpif *ofproto)
abe529af
BP
2721{
2722 /*
2723 * Idle time histogram.
2724 *
b0f7b9b5
BP
2725 * Most of the time a switch has a relatively small number of subfacets.
2726 * When this is the case we might as well keep statistics for all of them
2727 * in userspace and to cache them in the kernel datapath for performance as
abe529af
BP
2728 * well.
2729 *
b0f7b9b5 2730 * As the number of subfacets increases, the memory required to maintain
abe529af 2731 * statistics about them in userspace and in the kernel becomes
b0f7b9b5
BP
2732 * significant. However, with a large number of subfacets it is likely
2733 * that only a few of them are "heavy hitters" that consume a large amount
2734 * of bandwidth. At this point, only heavy hitters are worth caching in
2735 * the kernel and maintaining in userspaces; other subfacets we can
2736 * discard.
abe529af
BP
2737 *
2738 * The technique used to compute the idle time is to build a histogram with
b0f7b9b5 2739 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
abe529af
BP
2740 * that is installed in the kernel gets dropped in the appropriate bucket.
2741 * After the histogram has been built, we compute the cutoff so that only
b0f7b9b5 2742 * the most-recently-used 1% of subfacets (but at least
084f5290 2743 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
b0f7b9b5
BP
2744 * the most-recently-used bucket of subfacets is kept, so actually an
2745 * arbitrary number of subfacets can be kept in any given expiration run
084f5290
SH
2746 * (though the next run will delete most of those unless they receive
2747 * additional data).
abe529af 2748 *
b0f7b9b5
BP
2749 * This requires a second pass through the subfacets, in addition to the
2750 * pass made by update_stats(), because the former function never looks at
2751 * uninstallable subfacets.
abe529af
BP
2752 */
2753 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2754 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2755 int buckets[N_BUCKETS] = { 0 };
f11c1ef4 2756 int total, subtotal, bucket;
b0f7b9b5 2757 struct subfacet *subfacet;
abe529af
BP
2758 long long int now;
2759 int i;
2760
b0f7b9b5 2761 total = hmap_count(&ofproto->subfacets);
084f5290 2762 if (total <= ofproto->up.flow_eviction_threshold) {
abe529af
BP
2763 return N_BUCKETS * BUCKET_WIDTH;
2764 }
2765
2766 /* Build histogram. */
2767 now = time_msec();
b0f7b9b5
BP
2768 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
2769 long long int idle = now - subfacet->used;
abe529af
BP
2770 int bucket = (idle <= 0 ? 0
2771 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2772 : (unsigned int) idle / BUCKET_WIDTH);
2773 buckets[bucket]++;
2774 }
2775
2776 /* Find the first bucket whose flows should be expired. */
f11c1ef4
SH
2777 subtotal = bucket = 0;
2778 do {
2779 subtotal += buckets[bucket++];
084f5290
SH
2780 } while (bucket < N_BUCKETS &&
2781 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
abe529af
BP
2782
2783 if (VLOG_IS_DBG_ENABLED()) {
2784 struct ds s;
2785
2786 ds_init(&s);
2787 ds_put_cstr(&s, "keep");
2788 for (i = 0; i < N_BUCKETS; i++) {
2789 if (i == bucket) {
2790 ds_put_cstr(&s, ", drop");
2791 }
2792 if (buckets[i]) {
2793 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2794 }
2795 }
2796 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2797 ds_destroy(&s);
2798 }
2799
2800 return bucket * BUCKET_WIDTH;
2801}
2802
abe529af 2803static void
b0f7b9b5 2804expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
abe529af
BP
2805{
2806 long long int cutoff = time_msec() - dp_max_idle;
b0f7b9b5 2807 struct subfacet *subfacet, *next_subfacet;
abe529af 2808
b0f7b9b5
BP
2809 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
2810 &ofproto->subfacets) {
2811 if (subfacet->used < cutoff) {
2812 subfacet_destroy(ofproto, subfacet);
abe529af
BP
2813 }
2814 }
2815}
2816
2817/* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2818 * then delete it entirely. */
2819static void
2820rule_expire(struct rule_dpif *rule)
2821{
2822 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2823 struct facet *facet, *next_facet;
2824 long long int now;
2825 uint8_t reason;
2826
2827 /* Has 'rule' expired? */
2828 now = time_msec();
2829 if (rule->up.hard_timeout
308881af 2830 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
abe529af
BP
2831 reason = OFPRR_HARD_TIMEOUT;
2832 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2833 && now > rule->used + rule->up.idle_timeout * 1000) {
2834 reason = OFPRR_IDLE_TIMEOUT;
2835 } else {
2836 return;
2837 }
2838
2839 COVERAGE_INC(ofproto_dpif_expired);
2840
2841 /* Update stats. (This is a no-op if the rule expired due to an idle
2842 * timeout, because that only happens when the rule has no facets left.) */
2843 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2844 facet_remove(ofproto, facet);
2845 }
2846
2847 /* Get rid of the rule. */
2848 ofproto_rule_expire(&rule->up, reason);
2849}
2850\f
2851/* Facets. */
2852
f3827897 2853/* Creates and returns a new facet owned by 'rule', given a 'flow'.
abe529af
BP
2854 *
2855 * The caller must already have determined that no facet with an identical
2856 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
f3827897
BP
2857 * the ofproto's classifier table.
2858 *
b0f7b9b5
BP
2859 * The facet will initially have no subfacets. The caller should create (at
2860 * least) one subfacet with subfacet_create(). */
abe529af 2861static struct facet *
f3827897 2862facet_create(struct rule_dpif *rule, const struct flow *flow)
abe529af
BP
2863{
2864 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2865 struct facet *facet;
2866
2867 facet = xzalloc(sizeof *facet);
2868 facet->used = time_msec();
2869 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2870 list_push_back(&rule->facets, &facet->list_node);
2871 facet->rule = rule;
2872 facet->flow = *flow;
b0f7b9b5 2873 list_init(&facet->subfacets);
abe529af
BP
2874 netflow_flow_init(&facet->nf_flow);
2875 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2876
abe529af
BP
2877 return facet;
2878}
2879
2880static void
2881facet_free(struct facet *facet)
2882{
abe529af
BP
2883 free(facet);
2884}
2885
abe529af 2886static bool
3d9e05f8
BP
2887execute_controller_action(struct ofproto_dpif *ofproto,
2888 const struct flow *flow,
2889 const struct nlattr *odp_actions, size_t actions_len,
2890 struct ofpbuf *packet)
2891{
2892 if (actions_len
2893 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2894 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
62cd7072
BP
2895 /* As an optimization, avoid a round-trip from userspace to kernel to
2896 * userspace. This also avoids possibly filling up kernel packet
2897 * buffers along the way.
2898 *
2899 * This optimization will not accidentally catch sFlow
2900 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2901 * inside OVS_ACTION_ATTR_SAMPLE. */
2902 const struct nlattr *nla;
2903
2904 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2905 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2906 false);
2907 return true;
3d9e05f8
BP
2908 } else {
2909 return false;
2910 }
2911}
2912
2913/* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2914 * 'packet', which arrived on 'in_port'.
2915 *
2916 * Takes ownership of 'packet'. */
2917static bool
2918execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2919 const struct nlattr *odp_actions, size_t actions_len,
2920 struct ofpbuf *packet)
2921{
2922 struct odputil_keybuf keybuf;
2923 struct ofpbuf key;
2924 int error;
2925
2926 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2927 packet)) {
2928 return true;
6ff686f2 2929 }
abe529af 2930
6ff686f2
PS
2931 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2932 odp_flow_key_from_flow(&key, flow);
80e5eed9 2933
6ff686f2
PS
2934 error = dpif_execute(ofproto->dpif, key.data, key.size,
2935 odp_actions, actions_len, packet);
80e5eed9 2936
6ff686f2
PS
2937 ofpbuf_delete(packet);
2938 return !error;
abe529af
BP
2939}
2940
abe529af
BP
2941/* Remove 'facet' from 'ofproto' and free up the associated memory:
2942 *
2943 * - If 'facet' was installed in the datapath, uninstalls it and updates its
b0f7b9b5 2944 * rule's statistics, via subfacet_uninstall().
abe529af
BP
2945 *
2946 * - Removes 'facet' from its rule and from ofproto->facets.
2947 */
2948static void
2949facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2950{
b0f7b9b5
BP
2951 struct subfacet *subfacet, *next_subfacet;
2952
2953 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
2954 &facet->subfacets) {
2955 subfacet_destroy__(ofproto, subfacet);
2956 }
2957
abe529af
BP
2958 facet_flush_stats(ofproto, facet);
2959 hmap_remove(&ofproto->facets, &facet->hmap_node);
2960 list_remove(&facet->list_node);
2961 facet_free(facet);
2962}
2963
abe529af 2964static void
55af77bb 2965facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
abe529af 2966{
55af77bb 2967 uint64_t n_bytes;
b95fc6ba 2968 struct subfacet *subfacet;
abe529af 2969 const struct nlattr *a;
abe529af 2970 unsigned int left;
d78be13b 2971 ovs_be16 vlan_tci;
abe529af 2972
55af77bb 2973 if (facet->byte_count <= facet->accounted_bytes) {
abe529af
BP
2974 return;
2975 }
55af77bb
EJ
2976 n_bytes = facet->byte_count - facet->accounted_bytes;
2977 facet->accounted_bytes = facet->byte_count;
abe529af 2978
75a75043 2979 /* Feed information from the active flows back into the learning table to
abe529af
BP
2980 * ensure that table is always in sync with what is actually flowing
2981 * through the datapath. */
75a75043
BP
2982 if (facet->has_learn || facet->has_normal) {
2983 struct action_xlate_ctx ctx;
abe529af 2984
e84173dc
BP
2985 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
2986 facet->flow.vlan_tci, NULL);
75a75043
BP
2987 ctx.may_learn = true;
2988 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2989 facet->rule->up.n_actions));
2990 }
abe529af 2991
75a75043 2992 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
abe529af
BP
2993 return;
2994 }
d78be13b
BP
2995
2996 /* This loop feeds byte counters to bond_account() for rebalancing to use
2997 * as a basis. We also need to track the actual VLAN on which the packet
2998 * is going to be sent to ensure that it matches the one passed to
2999 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
b95fc6ba
BP
3000 * hash bucket.)
3001 *
3002 * We use the actions from an arbitrary subfacet because they should all
3003 * be equally valid for our purpose. */
3004 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3005 struct subfacet, list_node);
d78be13b 3006 vlan_tci = facet->flow.vlan_tci;
b95fc6ba
BP
3007 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3008 subfacet->actions, subfacet->actions_len) {
fea393b1 3009 const struct ovs_action_push_vlan *vlan;
d78be13b 3010 struct ofport_dpif *port;
abe529af 3011
d78be13b 3012 switch (nl_attr_type(a)) {
df2c07f4 3013 case OVS_ACTION_ATTR_OUTPUT:
abe529af
BP
3014 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3015 if (port && port->bundle && port->bundle->bond) {
d78be13b 3016 bond_account(port->bundle->bond, &facet->flow,
dc155bff 3017 vlan_tci_to_vid(vlan_tci), n_bytes);
abe529af 3018 }
d78be13b
BP
3019 break;
3020
fea393b1
BP
3021 case OVS_ACTION_ATTR_POP_VLAN:
3022 vlan_tci = htons(0);
d78be13b
BP
3023 break;
3024
fea393b1
BP
3025 case OVS_ACTION_ATTR_PUSH_VLAN:
3026 vlan = nl_attr_get(a);
3027 vlan_tci = vlan->vlan_tci;
d78be13b 3028 break;
abe529af
BP
3029 }
3030 }
3031}
3032
abe529af
BP
3033/* Returns true if the only action for 'facet' is to send to the controller.
3034 * (We don't report NetFlow expiration messages for such facets because they
3035 * are just part of the control logic for the network, not real traffic). */
3036static bool
3037facet_is_controller_flow(struct facet *facet)
3038{
3039 return (facet
3040 && facet->rule->up.n_actions == 1
3041 && action_outputs_to_port(&facet->rule->up.actions[0],
3042 htons(OFPP_CONTROLLER)));
3043}
3044
3045/* Folds all of 'facet''s statistics into its rule. Also updates the
3046 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3047 * 'facet''s statistics in the datapath should have been zeroed and folded into
3048 * its packet and byte counts before this function is called. */
3049static void
3050facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3051{
b0f7b9b5
BP
3052 struct subfacet *subfacet;
3053
3054 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3055 assert(!subfacet->dp_byte_count);
3056 assert(!subfacet->dp_packet_count);
3057 }
abe529af
BP
3058
3059 facet_push_stats(facet);
55af77bb 3060 facet_account(ofproto, facet);
abe529af
BP
3061
3062 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3063 struct ofexpired expired;
3064 expired.flow = facet->flow;
3065 expired.packet_count = facet->packet_count;
3066 expired.byte_count = facet->byte_count;
3067 expired.used = facet->used;
3068 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3069 }
3070
3071 facet->rule->packet_count += facet->packet_count;
3072 facet->rule->byte_count += facet->byte_count;
3073
3074 /* Reset counters to prevent double counting if 'facet' ever gets
3075 * reinstalled. */
bbb5d219 3076 facet_reset_counters(facet);
abe529af
BP
3077
3078 netflow_flow_clear(&facet->nf_flow);
3079}
3080
3081/* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3082 * Returns it if found, otherwise a null pointer.
3083 *
3084 * The returned facet might need revalidation; use facet_lookup_valid()
3085 * instead if that is important. */
3086static struct facet *
3087facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3088{
3089 struct facet *facet;
3090
3091 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3092 &ofproto->facets) {
3093 if (flow_equal(flow, &facet->flow)) {
3094 return facet;
3095 }
3096 }
3097
3098 return NULL;
3099}
3100
3101/* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3102 * Returns it if found, otherwise a null pointer.
3103 *
3104 * The returned facet is guaranteed to be valid. */
3105static struct facet *
3106facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3107{
3108 struct facet *facet = facet_find(ofproto, flow);
3109
3110 /* The facet we found might not be valid, since we could be in need of
3111 * revalidation. If it is not valid, don't return it. */
3112 if (facet
0e4b3771
BP
3113 && (ofproto->need_revalidate
3114 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
abe529af
BP
3115 && !facet_revalidate(ofproto, facet)) {
3116 COVERAGE_INC(facet_invalidated);
3117 return NULL;
3118 }
3119
3120 return facet;
3121}
3122
3123/* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3124 *
3125 * - If the rule found is different from 'facet''s current rule, moves
3126 * 'facet' to the new rule and recompiles its actions.
3127 *
3128 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3129 * where it is and recompiles its actions anyway.
3130 *
3131 * - If there is none, destroys 'facet'.
3132 *
3133 * Returns true if 'facet' still exists, false if it has been destroyed. */
3134static bool
3135facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3136{
b95fc6ba
BP
3137 struct actions {
3138 struct nlattr *odp_actions;
3139 size_t actions_len;
3140 };
3141 struct actions *new_actions;
3142
abe529af 3143 struct action_xlate_ctx ctx;
abe529af 3144 struct rule_dpif *new_rule;
b0f7b9b5 3145 struct subfacet *subfacet;
abe529af 3146 bool actions_changed;
b95fc6ba 3147 int i;
abe529af
BP
3148
3149 COVERAGE_INC(facet_revalidate);
3150
3151 /* Determine the new rule. */
29901626 3152 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
abe529af
BP
3153 if (!new_rule) {
3154 /* No new rule, so delete the facet. */
3155 facet_remove(ofproto, facet);
3156 return false;
3157 }
3158
df2c07f4 3159 /* Calculate new datapath actions.
abe529af
BP
3160 *
3161 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3162 * emit a NetFlow expiration and, if so, we need to have the old state
3163 * around to properly compose it. */
abe529af 3164
df2c07f4
JP
3165 /* If the datapath actions changed or the installability changed,
3166 * then we need to talk to the datapath. */
b95fc6ba
BP
3167 i = 0;
3168 new_actions = NULL;
3169 memset(&ctx, 0, sizeof ctx);
b0f7b9b5 3170 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
b95fc6ba
BP
3171 struct ofpbuf *odp_actions;
3172 bool should_install;
3173
e84173dc
BP
3174 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3175 subfacet->initial_tci, NULL);
b95fc6ba
BP
3176 odp_actions = xlate_actions(&ctx, new_rule->up.actions,
3177 new_rule->up.n_actions);
3178 actions_changed = (subfacet->actions_len != odp_actions->size
3179 || memcmp(subfacet->actions, odp_actions->data,
3180 subfacet->actions_len));
3181
3182 should_install = (ctx.may_set_up_flow
3183 && subfacet->key_fitness != ODP_FIT_TOO_LITTLE);
b0f7b9b5
BP
3184 if (actions_changed || should_install != subfacet->installed) {
3185 if (should_install) {
3186 struct dpif_flow_stats stats;
3187
3188 subfacet_install(ofproto, subfacet,
3189 odp_actions->data, odp_actions->size, &stats);
3190 subfacet_update_stats(ofproto, subfacet, &stats);
3191 } else {
3192 subfacet_uninstall(ofproto, subfacet);
3193 }
b95fc6ba
BP
3194
3195 if (!new_actions) {
3196 new_actions = xcalloc(list_size(&facet->subfacets),
3197 sizeof *new_actions);
3198 }
3199 new_actions[i].odp_actions = xmemdup(odp_actions->data,
3200 odp_actions->size);
3201 new_actions[i].actions_len = odp_actions->size;
abe529af 3202 }
b95fc6ba
BP
3203
3204 ofpbuf_delete(odp_actions);
3205 i++;
b0f7b9b5 3206 }
b95fc6ba 3207 if (new_actions) {
abe529af
BP
3208 facet_flush_stats(ofproto, facet);
3209 }
3210
3211 /* Update 'facet' now that we've taken care of all the old state. */
3212 facet->tags = ctx.tags;
3213 facet->nf_flow.output_iface = ctx.nf_output_iface;
3214 facet->may_install = ctx.may_set_up_flow;
75a75043
BP
3215 facet->has_learn = ctx.has_learn;
3216 facet->has_normal = ctx.has_normal;
b95fc6ba
BP
3217 if (new_actions) {
3218 i = 0;
3219 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3220 if (new_actions[i].odp_actions) {
3221 free(subfacet->actions);
3222 subfacet->actions = new_actions[i].odp_actions;
3223 subfacet->actions_len = new_actions[i].actions_len;
3224 }
3225 i++;
3226 }
3227 free(new_actions);
abe529af
BP
3228 }
3229 if (facet->rule != new_rule) {
3230 COVERAGE_INC(facet_changed_rule);
3231 list_remove(&facet->list_node);
3232 list_push_back(&new_rule->facets, &facet->list_node);
3233 facet->rule = new_rule;
3234 facet->used = new_rule->up.created;
3235 facet->rs_used = facet->used;
3236 }
3237
abe529af
BP
3238 return true;
3239}
3240
3241/* Updates 'facet''s used time. Caller is responsible for calling
3242 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3243static void
3244facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3245 long long int used)
3246{
3247 if (used > facet->used) {
3248 facet->used = used;
3249 if (used > facet->rule->used) {
3250 facet->rule->used = used;
3251 }
3252 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3253 }
3254}
3255
bbb5d219
EJ
3256static void
3257facet_reset_counters(struct facet *facet)
3258{
3259 facet->packet_count = 0;
3260 facet->byte_count = 0;
3261 facet->rs_packet_count = 0;
3262 facet->rs_byte_count = 0;
3263 facet->accounted_bytes = 0;
3264}
3265
abe529af
BP
3266static void
3267facet_push_stats(struct facet *facet)
3268{
3269 uint64_t rs_packets, rs_bytes;
3270
3271 assert(facet->packet_count >= facet->rs_packet_count);
3272 assert(facet->byte_count >= facet->rs_byte_count);
3273 assert(facet->used >= facet->rs_used);
3274
3275 rs_packets = facet->packet_count - facet->rs_packet_count;
3276 rs_bytes = facet->byte_count - facet->rs_byte_count;
3277
3278 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3279 facet->rs_packet_count = facet->packet_count;
3280 facet->rs_byte_count = facet->byte_count;
3281 facet->rs_used = facet->used;
3282
3283 flow_push_stats(facet->rule, &facet->flow,
3284 rs_packets, rs_bytes, facet->used);
3285 }
3286}
3287
3288struct ofproto_push {
3289 struct action_xlate_ctx ctx;
3290 uint64_t packets;
3291 uint64_t bytes;
3292 long long int used;
3293};
3294
3295static void
3296push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3297{
3298 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3299
3300 if (rule) {
3301 rule->packet_count += push->packets;
3302 rule->byte_count += push->bytes;
3303 rule->used = MAX(push->used, rule->used);
3304 }
3305}
3306
3307/* Pushes flow statistics to the rules which 'flow' resubmits into given
3308 * 'rule''s actions. */
3309static void
3310flow_push_stats(const struct rule_dpif *rule,
59d0f2c8 3311 const struct flow *flow, uint64_t packets, uint64_t bytes,
abe529af
BP
3312 long long int used)
3313{
3314 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3315 struct ofproto_push push;
3316
3317 push.packets = packets;
3318 push.bytes = bytes;
3319 push.used = used;
3320
e84173dc 3321 action_xlate_ctx_init(&push.ctx, ofproto, flow, flow->vlan_tci, NULL);
abe529af
BP
3322 push.ctx.resubmit_hook = push_resubmit;
3323 ofpbuf_delete(xlate_actions(&push.ctx,
3324 rule->up.actions, rule->up.n_actions));
3325}
3326\f
b0f7b9b5
BP
3327/* Subfacets. */
3328
3329static struct subfacet *
3330subfacet_find__(struct ofproto_dpif *ofproto,
3331 const struct nlattr *key, size_t key_len, uint32_t key_hash,
3332 const struct flow *flow)
3333{
3334 struct subfacet *subfacet;
3335
3336 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
3337 &ofproto->subfacets) {
3338 if (subfacet->key
3339 ? (subfacet->key_len == key_len
3340 && !memcmp(key, subfacet->key, key_len))
3341 : flow_equal(flow, &subfacet->facet->flow)) {
3342 return subfacet;
3343 }
3344 }
3345
3346 return NULL;
3347}
3348
3349/* Searches 'facet' (within 'ofproto') for a subfacet with the specified
3350 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
b95fc6ba
BP
3351 * there is one, otherwise creates and returns a new subfacet.
3352 *
3353 * If the returned subfacet is new, then subfacet->actions will be NULL, in
3354 * which case the caller must populate the actions with
3355 * subfacet_make_actions(). */
b0f7b9b5
BP
3356static struct subfacet *
3357subfacet_create(struct ofproto_dpif *ofproto, struct facet *facet,
3358 enum odp_key_fitness key_fitness,
e84173dc 3359 const struct nlattr *key, size_t key_len, ovs_be16 initial_tci)
b0f7b9b5
BP
3360{
3361 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3362 struct subfacet *subfacet;
3363
3364 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
3365 if (subfacet) {
3366 if (subfacet->facet == facet) {
3367 return subfacet;
3368 }
3369
3370 /* This shouldn't happen. */
3371 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
3372 subfacet_destroy(ofproto, subfacet);
3373 }
3374
3375 subfacet = xzalloc(sizeof *subfacet);
3376 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
3377 list_push_back(&facet->subfacets, &subfacet->list_node);
3378 subfacet->facet = facet;
3379 subfacet->used = time_msec();
3380 subfacet->key_fitness = key_fitness;
3381 if (key_fitness != ODP_FIT_PERFECT) {
3382 subfacet->key = xmemdup(key, key_len);
3383 subfacet->key_len = key_len;
3384 }
3385 subfacet->installed = false;
e84173dc 3386 subfacet->initial_tci = initial_tci;
b0f7b9b5
BP
3387
3388 return subfacet;
3389}
3390
3391/* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
3392 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
3393static struct subfacet *
3394subfacet_find(struct ofproto_dpif *ofproto,
3395 const struct nlattr *key, size_t key_len,
3396 const struct flow *flow)
3397{
3398 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3399
3400 return subfacet_find__(ofproto, key, key_len, key_hash, flow);
3401}
3402
3403/* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
3404 * its facet within 'ofproto', and frees it. */
3405static void
3406subfacet_destroy__(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3407{
3408 subfacet_uninstall(ofproto, subfacet);
3409 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
3410 list_remove(&subfacet->list_node);
3411 free(subfacet->key);
b95fc6ba 3412 free(subfacet->actions);
b0f7b9b5
BP
3413 free(subfacet);
3414}
3415
3416/* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
3417 * last remaining subfacet in its facet destroys the facet too. */
3418static void
3419subfacet_destroy(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3420{
3421 struct facet *facet = subfacet->facet;
3422
3423 subfacet_destroy__(ofproto, subfacet);
3424 if (list_is_empty(&facet->subfacets)) {
3425 facet_remove(ofproto, facet);
3426 }
3427}
3428
3429/* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
3430 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
3431 * for use as temporary storage. */
3432static void
3433subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
3434 struct ofpbuf *key)
3435{
3436 if (!subfacet->key) {
3437 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
3438 odp_flow_key_from_flow(key, &subfacet->facet->flow);
3439 } else {
3440 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
3441 }
3442}
3443
b95fc6ba
BP
3444/* Composes the datapath actions for 'subfacet' based on its rule's actions. */
3445static void
3446subfacet_make_actions(struct ofproto_dpif *p, struct subfacet *subfacet,
3447 const struct ofpbuf *packet)
3448{
3449 struct facet *facet = subfacet->facet;
3450 const struct rule_dpif *rule = facet->rule;
3451 struct ofpbuf *odp_actions;
3452 struct action_xlate_ctx ctx;
3453
e84173dc
BP
3454 action_xlate_ctx_init(&ctx, p, &facet->flow, subfacet->initial_tci,
3455 packet);
b95fc6ba
BP
3456 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3457 facet->tags = ctx.tags;
3458 facet->may_install = ctx.may_set_up_flow;
3459 facet->has_learn = ctx.has_learn;
3460 facet->has_normal = ctx.has_normal;
3461 facet->nf_flow.output_iface = ctx.nf_output_iface;
3462
3463 if (subfacet->actions_len != odp_actions->size
3464 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
3465 free(subfacet->actions);
3466 subfacet->actions_len = odp_actions->size;
3467 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
3468 }
3469
3470 ofpbuf_delete(odp_actions);
3471}
3472
b0f7b9b5
BP
3473/* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
3474 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
3475 * in the datapath will be zeroed and 'stats' will be updated with traffic new
3476 * since 'subfacet' was last updated.
3477 *
3478 * Returns 0 if successful, otherwise a positive errno value. */
3479static int
3480subfacet_install(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3481 const struct nlattr *actions, size_t actions_len,
3482 struct dpif_flow_stats *stats)
3483{
3484 struct odputil_keybuf keybuf;
3485 enum dpif_flow_put_flags flags;
3486 struct ofpbuf key;
3487 int ret;
3488
3489 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3490 if (stats) {
3491 flags |= DPIF_FP_ZERO_STATS;
3492 }
3493
3494 subfacet_get_key(subfacet, &keybuf, &key);
3495 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
3496 actions, actions_len, stats);
3497
3498 if (stats) {
3499 subfacet_reset_dp_stats(subfacet, stats);
3500 }
3501
3502 return ret;
3503}
3504
3505/* If 'subfacet' is installed in the datapath, uninstalls it. */
3506static void
3507subfacet_uninstall(struct ofproto_dpif *p, struct subfacet *subfacet)
3508{
3509 if (subfacet->installed) {
3510 struct odputil_keybuf keybuf;
3511 struct dpif_flow_stats stats;
3512 struct ofpbuf key;
3513 int error;
3514
3515 subfacet_get_key(subfacet, &keybuf, &key);
3516 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
3517 subfacet_reset_dp_stats(subfacet, &stats);
3518 if (!error) {
3519 subfacet_update_stats(p, subfacet, &stats);
3520 }
3521 subfacet->installed = false;
3522 } else {
3523 assert(subfacet->dp_packet_count == 0);
3524 assert(subfacet->dp_byte_count == 0);
3525 }
3526}
3527
3528/* Resets 'subfacet''s datapath statistics counters. This should be called
3529 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
3530 * non-null, it should contain the statistics returned by dpif when 'subfacet'
3531 * was reset in the datapath. 'stats' will be modified to include only
3532 * statistics new since 'subfacet' was last updated. */
3533static void
3534subfacet_reset_dp_stats(struct subfacet *subfacet,
3535 struct dpif_flow_stats *stats)
3536{
3537 if (stats
3538 && subfacet->dp_packet_count <= stats->n_packets
3539 && subfacet->dp_byte_count <= stats->n_bytes) {
3540 stats->n_packets -= subfacet->dp_packet_count;
3541 stats->n_bytes -= subfacet->dp_byte_count;
3542 }
3543
3544 subfacet->dp_packet_count = 0;
3545 subfacet->dp_byte_count = 0;
3546}
3547
3548/* Updates 'subfacet''s used time. The caller is responsible for calling
3549 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
3550static void
3551subfacet_update_time(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3552 long long int used)
3553{
3554 if (used > subfacet->used) {
3555 subfacet->used = used;
3556 facet_update_time(ofproto, subfacet->facet, used);
3557 }
3558}
3559
3560/* Folds the statistics from 'stats' into the counters in 'subfacet'.
3561 *
3562 * Because of the meaning of a subfacet's counters, it only makes sense to do
3563 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
3564 * represents a packet that was sent by hand or if it represents statistics
3565 * that have been cleared out of the datapath. */
3566static void
3567subfacet_update_stats(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3568 const struct dpif_flow_stats *stats)
3569{
3570 if (stats->n_packets || stats->used > subfacet->used) {
3571 struct facet *facet = subfacet->facet;
3572
3573 subfacet_update_time(ofproto, subfacet, stats->used);
3574 facet->packet_count += stats->n_packets;
3575 facet->byte_count += stats->n_bytes;
3576 facet_push_stats(facet);
3577 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3578 }
3579}
3580\f
abe529af
BP
3581/* Rules. */
3582
3583static struct rule_dpif *
29901626
BP
3584rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3585 uint8_t table_id)
abe529af 3586{
7257b535
BP
3587 struct cls_rule *cls_rule;
3588 struct classifier *cls;
3589
9cdaaebe
BP
3590 if (table_id >= N_TABLES) {
3591 return NULL;
3592 }
3593
7257b535 3594 cls = &ofproto->up.tables[table_id];
eadef313 3595 if (flow->nw_frag & FLOW_NW_FRAG_ANY
7257b535
BP
3596 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3597 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3598 * are unavailable. */
3599 struct flow ofpc_normal_flow = *flow;
3600 ofpc_normal_flow.tp_src = htons(0);
3601 ofpc_normal_flow.tp_dst = htons(0);
3602 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3603 } else {
3604 cls_rule = classifier_lookup(cls, flow);
3605 }
3606 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
abe529af
BP
3607}
3608
7ee20df1
BP
3609static void
3610complete_operation(struct rule_dpif *rule)
3611{
3612 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3613
54a9cbc9 3614 rule_invalidate(rule);
7ee20df1
BP
3615 if (clogged) {
3616 struct dpif_completion *c = xmalloc(sizeof *c);
3617 c->op = rule->up.pending;
3618 list_push_back(&ofproto->completions, &c->list_node);
3619 } else {
3620 ofoperation_complete(rule->up.pending, 0);
3621 }
3622}
3623
abe529af
BP
3624static struct rule *
3625rule_alloc(void)
3626{
3627 struct rule_dpif *rule = xmalloc(sizeof *rule);
3628 return &rule->up;
3629}
3630
3631static void
3632rule_dealloc(struct rule *rule_)
3633{
3634 struct rule_dpif *rule = rule_dpif_cast(rule_);
3635 free(rule);
3636}
3637
3638static int
3639rule_construct(struct rule *rule_)
3640{
3641 struct rule_dpif *rule = rule_dpif_cast(rule_);
3642 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
7ee20df1 3643 struct rule_dpif *victim;
54a9cbc9 3644 uint8_t table_id;
5bf0e941
BP
3645 int error;
3646
3647 error = validate_actions(rule->up.actions, rule->up.n_actions,
3648 &rule->up.cr.flow, ofproto->max_ports);
3649 if (error) {
3650 return error;
3651 }
abe529af
BP
3652
3653 rule->used = rule->up.created;
3654 rule->packet_count = 0;
3655 rule->byte_count = 0;
abe529af 3656
7ee20df1
BP
3657 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3658 if (victim && !list_is_empty(&victim->facets)) {
3659 struct facet *facet;
3660
3661 rule->facets = victim->facets;
3662 list_moved(&rule->facets);
3663 LIST_FOR_EACH (facet, list_node, &rule->facets) {
bbb5d219
EJ
3664 /* XXX: We're only clearing our local counters here. It's possible
3665 * that quite a few packets are unaccounted for in the datapath
3666 * statistics. These will be accounted to the new rule instead of
3667 * cleared as required. This could be fixed by clearing out the
3668 * datapath statistics for this facet, but currently it doesn't
3669 * seem worth it. */
3670 facet_reset_counters(facet);
7ee20df1
BP
3671 facet->rule = rule;
3672 }
3673 } else {
3674 /* Must avoid list_moved() in this case. */
3675 list_init(&rule->facets);
3676 }
abe529af 3677
54a9cbc9
BP
3678 table_id = rule->up.table_id;
3679 rule->tag = (victim ? victim->tag
3680 : table_id == 0 ? 0
3681 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3682 ofproto->tables[table_id].basis));
3683
7ee20df1 3684 complete_operation(rule);
abe529af
BP
3685 return 0;
3686}
3687
3688static void
3689rule_destruct(struct rule *rule_)
3690{
3691 struct rule_dpif *rule = rule_dpif_cast(rule_);
3692 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3693 struct facet *facet, *next_facet;
3694
abe529af
BP
3695 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3696 facet_revalidate(ofproto, facet);
3697 }
7ee20df1
BP
3698
3699 complete_operation(rule);
abe529af
BP
3700}
3701
3702static void
3703rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3704{
3705 struct rule_dpif *rule = rule_dpif_cast(rule_);
3706 struct facet *facet;
3707
3708 /* Start from historical data for 'rule' itself that are no longer tracked
3709 * in facets. This counts, for example, facets that have expired. */
3710 *packets = rule->packet_count;
3711 *bytes = rule->byte_count;
3712
3713 /* Add any statistics that are tracked by facets. This includes
3714 * statistical data recently updated by ofproto_update_stats() as well as
3715 * stats for packets that were executed "by hand" via dpif_execute(). */
3716 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3717 *packets += facet->packet_count;
3718 *bytes += facet->byte_count;
3719 }
3720}
3721
5bf0e941 3722static int
59d0f2c8
BP
3723rule_execute(struct rule *rule_, const struct flow *flow,
3724 struct ofpbuf *packet)
abe529af
BP
3725{
3726 struct rule_dpif *rule = rule_dpif_cast(rule_);
3727 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3728 struct action_xlate_ctx ctx;
3729 struct ofpbuf *odp_actions;
abe529af
BP
3730 size_t size;
3731
e84173dc 3732 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
abe529af
BP
3733 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3734 size = packet->size;
3735 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3736 odp_actions->size, packet)) {
3737 rule->used = time_msec();
3738 rule->packet_count++;
3739 rule->byte_count += size;
3740 flow_push_stats(rule, flow, 1, size, rule->used);
3741 }
3742 ofpbuf_delete(odp_actions);
5bf0e941
BP
3743
3744 return 0;
abe529af
BP
3745}
3746
7ee20df1
BP
3747static void
3748rule_modify_actions(struct rule *rule_)
abe529af
BP
3749{
3750 struct rule_dpif *rule = rule_dpif_cast(rule_);
3751 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3752 int error;
3753
7ee20df1
BP
3754 error = validate_actions(rule->up.actions, rule->up.n_actions,
3755 &rule->up.cr.flow, ofproto->max_ports);
3756 if (error) {
3757 ofoperation_complete(rule->up.pending, error);
3758 return;
abe529af 3759 }
7ee20df1
BP
3760
3761 complete_operation(rule);
abe529af
BP
3762}
3763\f
97d6520b 3764/* Sends 'packet' out 'ofport'.
abe529af
BP
3765 * Returns 0 if successful, otherwise a positive errno value. */
3766static int
97d6520b 3767send_packet(const struct ofport_dpif *ofport, const struct ofpbuf *packet)
abe529af 3768{
97d6520b
BP
3769 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3770 uint16_t odp_port = ofport->odp_port;
80e5eed9
BP
3771 struct ofpbuf key, odp_actions;
3772 struct odputil_keybuf keybuf;
3773 struct flow flow;
abe529af
BP
3774 int error;
3775
abff858b 3776 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
80e5eed9
BP
3777 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3778 odp_flow_key_from_flow(&key, &flow);
3779
abe529af 3780 ofpbuf_init(&odp_actions, 32);
6ff686f2
PS
3781 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3782
df2c07f4 3783 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
80e5eed9
BP
3784 error = dpif_execute(ofproto->dpif,
3785 key.data, key.size,
3786 odp_actions.data, odp_actions.size,
abe529af
BP
3787 packet);
3788 ofpbuf_uninit(&odp_actions);
3789
3790 if (error) {
3791 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3792 ofproto->up.name, odp_port, strerror(error));
3793 }
3794 return error;
3795}
3796\f
df2c07f4 3797/* OpenFlow to datapath action translation. */
abe529af
BP
3798
3799static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3800 struct action_xlate_ctx *ctx);
4cd78906 3801static void xlate_normal(struct action_xlate_ctx *);
abe529af 3802
98403001
BP
3803static size_t
3804put_userspace_action(const struct ofproto_dpif *ofproto,
3805 struct ofpbuf *odp_actions,
3806 const struct flow *flow,
3807 const struct user_action_cookie *cookie)
3808{
98403001
BP
3809 uint32_t pid;
3810
3811 pid = dpif_port_get_pid(ofproto->dpif,
3812 ofp_port_to_odp_port(flow->in_port));
3813
39db78a0 3814 return odp_put_userspace_action(pid, cookie, odp_actions);
98403001
BP
3815}
3816
6ff686f2
PS
3817/* Compose SAMPLE action for sFlow. */
3818static size_t
3819compose_sflow_action(const struct ofproto_dpif *ofproto,
3820 struct ofpbuf *odp_actions,
3821 const struct flow *flow,
3822 uint32_t odp_port)
3823{
3824 uint32_t port_ifindex;
3825 uint32_t probability;
98403001 3826 struct user_action_cookie cookie;
6ff686f2 3827 size_t sample_offset, actions_offset;
98403001 3828 int cookie_offset, n_output;
6ff686f2
PS
3829
3830 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3831 return 0;
3832 }
3833
3834 if (odp_port == OVSP_NONE) {
3835 port_ifindex = 0;
3836 n_output = 0;
3837 } else {
3838 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3839 n_output = 1;
3840 }
3841
3842 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3843
3844 /* Number of packets out of UINT_MAX to sample. */
3845 probability = dpif_sflow_get_probability(ofproto->sflow);
3846 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3847
3848 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3849
98403001
BP
3850 cookie.type = USER_ACTION_COOKIE_SFLOW;
3851 cookie.data = port_ifindex;
3852 cookie.n_output = n_output;
3853 cookie.vlan_tci = 0;
3854 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
6ff686f2
PS
3855
3856 nl_msg_end_nested(odp_actions, actions_offset);
3857 nl_msg_end_nested(odp_actions, sample_offset);
98403001 3858 return cookie_offset;
6ff686f2
PS
3859}
3860
3861/* SAMPLE action must be first action in any given list of actions.
3862 * At this point we do not have all information required to build it. So try to
3863 * build sample action as complete as possible. */
3864static void
3865add_sflow_action(struct action_xlate_ctx *ctx)
3866{
3867 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3868 ctx->odp_actions,
3869 &ctx->flow, OVSP_NONE);
3870 ctx->sflow_odp_port = 0;
3871 ctx->sflow_n_outputs = 0;
3872}
3873
3874/* Fix SAMPLE action according to data collected while composing ODP actions.
3875 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3876 * USERSPACE action's user-cookie which is required for sflow. */
3877static void
3878fix_sflow_action(struct action_xlate_ctx *ctx)
3879{
3880 const struct flow *base = &ctx->base_flow;
3881 struct user_action_cookie *cookie;
3882
3883 if (!ctx->user_cookie_offset) {
3884 return;
3885 }
3886
3887 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3888 sizeof(*cookie));
3889 assert(cookie != NULL);
3890 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3891
3892 if (ctx->sflow_n_outputs) {
3893 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3894 ctx->sflow_odp_port);
3895 }
3896 if (ctx->sflow_n_outputs >= 255) {
3897 cookie->n_output = 255;
3898 } else {
3899 cookie->n_output = ctx->sflow_n_outputs;
3900 }
3901 cookie->vlan_tci = base->vlan_tci;
3902}
3903
823518f1 3904static void
fea393b1
BP
3905commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3906 const void *key, size_t key_size)
823518f1 3907{
fea393b1 3908 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4edb9ae9
PS
3909 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3910 nl_msg_end_nested(odp_actions, offset);
3911}
3912
3913static void
3914commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3915 struct ofpbuf *odp_actions)
3916{
3917 if (base->tun_id == flow->tun_id) {
3918 return;
823518f1 3919 }
4edb9ae9
PS
3920 base->tun_id = flow->tun_id;
3921
fea393b1
BP
3922 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
3923 &base->tun_id, sizeof(base->tun_id));
823518f1
BP
3924}
3925
b3e9b2ed 3926static void
4edb9ae9
PS
3927commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3928 struct ofpbuf *odp_actions)
3929{
3930 struct ovs_key_ethernet eth_key;
3931
3932 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3933 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3934 return;
3935 }
3936
3937 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3938 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3939
3940 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3941 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3942
fea393b1
BP
3943 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3944 &eth_key, sizeof(eth_key));
4edb9ae9
PS
3945}
3946
3947static void
81b1afb1
EJ
3948commit_vlan_action(const struct flow *flow, struct flow *base,
3949 struct ofpbuf *odp_actions)
b3e9b2ed 3950{
81b1afb1 3951 if (base->vlan_tci == flow->vlan_tci) {
4edb9ae9 3952 return;
b3e9b2ed
EJ
3953 }
3954
4edb9ae9 3955 if (base->vlan_tci & htons(VLAN_CFI)) {
81b1afb1 3956 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
b3e9b2ed
EJ
3957 }
3958
81b1afb1 3959 if (flow->vlan_tci & htons(VLAN_CFI)) {
fea393b1 3960 struct ovs_action_push_vlan vlan;
4edb9ae9 3961
fea393b1 3962 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
81b1afb1
EJ
3963 vlan.vlan_tci = flow->vlan_tci;
3964 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
fea393b1 3965 &vlan, sizeof vlan);
b3e9b2ed 3966 }
81b1afb1 3967 base->vlan_tci = flow->vlan_tci;
4edb9ae9 3968}
b3e9b2ed 3969
4edb9ae9
PS
3970static void
3971commit_set_nw_action(const struct flow *flow, struct flow *base,
3972 struct ofpbuf *odp_actions)
3973{
3974 struct ovs_key_ipv4 ipv4_key;
3975
3976 if (base->dl_type != htons(ETH_TYPE_IP) ||
3977 !base->nw_src || !base->nw_dst) {
3978 return;
3979 }
3980
3981 if (base->nw_src == flow->nw_src &&
3982 base->nw_dst == flow->nw_dst &&
eadef313 3983 base->nw_tos == flow->nw_tos &&
a61680c6 3984 base->nw_ttl == flow->nw_ttl &&
eadef313 3985 base->nw_frag == flow->nw_frag) {
4edb9ae9 3986 return;
150a9f15
BP
3987 }
3988
4edb9ae9
PS
3989 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3990 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
be51a446
EJ
3991 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3992 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
4edb9ae9 3993 ipv4_key.ipv4_proto = base->nw_proto;
eadef313
JP
3994 ipv4_key.ipv4_frag = (base->nw_frag == 0 ? OVS_FRAG_TYPE_NONE
3995 : base->nw_frag == FLOW_NW_FRAG_ANY
3996 ? OVS_FRAG_TYPE_FIRST : OVS_FRAG_TYPE_LATER);
4edb9ae9 3997
fea393b1
BP
3998 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3999 &ipv4_key, sizeof(ipv4_key));
4edb9ae9
PS
4000}
4001
4002static void
4003commit_set_port_action(const struct flow *flow, struct flow *base,
4004 struct ofpbuf *odp_actions)
4005{
4006 if (!base->tp_src || !base->tp_dst) {
4007 return;
b3e9b2ed
EJ
4008 }
4009
4edb9ae9
PS
4010 if (base->tp_src == flow->tp_src &&
4011 base->tp_dst == flow->tp_dst) {
4012 return;
b3e9b2ed
EJ
4013 }
4014
4edb9ae9
PS
4015 if (flow->nw_proto == IPPROTO_TCP) {
4016 struct ovs_key_tcp port_key;
4017
4018 port_key.tcp_src = base->tp_src = flow->tp_src;
4019 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
4020
fea393b1
BP
4021 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
4022 &port_key, sizeof(port_key));
4edb9ae9
PS
4023
4024 } else if (flow->nw_proto == IPPROTO_UDP) {
4025 struct ovs_key_udp port_key;
4026
4027 port_key.udp_src = base->tp_src = flow->tp_src;
4028 port_key.udp_dst = base->tp_dst = flow->tp_dst;
4029
fea393b1
BP
4030 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
4031 &port_key, sizeof(port_key));
b3e9b2ed 4032 }
4edb9ae9 4033}
b3e9b2ed 4034
4edb9ae9 4035static void
abff858b
PS
4036commit_set_priority_action(const struct flow *flow, struct flow *base,
4037 struct ofpbuf *odp_actions)
4edb9ae9 4038{
abff858b 4039 if (base->priority == flow->priority) {
4edb9ae9 4040 return;
b3e9b2ed 4041 }
abff858b 4042 base->priority = flow->priority;
b3e9b2ed 4043
fea393b1
BP
4044 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
4045 &base->priority, sizeof(base->priority));
4edb9ae9
PS
4046}
4047
4048static void
4049commit_odp_actions(struct action_xlate_ctx *ctx)
4050{
4051 const struct flow *flow = &ctx->flow;
4052 struct flow *base = &ctx->base_flow;
4053 struct ofpbuf *odp_actions = ctx->odp_actions;
4054
4055 commit_set_tun_id_action(flow, base, odp_actions);
4056 commit_set_ether_addr_action(flow, base, odp_actions);
81b1afb1 4057 commit_vlan_action(flow, base, odp_actions);
4edb9ae9
PS
4058 commit_set_nw_action(flow, base, odp_actions);
4059 commit_set_port_action(flow, base, odp_actions);
abff858b 4060 commit_set_priority_action(flow, base, odp_actions);
b3e9b2ed
EJ
4061}
4062
6ff686f2 4063static void
81b1afb1
EJ
4064compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4065 bool check_stp)
6ff686f2 4066{
d59906fb 4067 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
5e48dc2b 4068 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
8b36f51e 4069 uint8_t flow_nw_tos = ctx->flow.nw_tos;
d59906fb 4070
81b1afb1 4071 if (ofport) {
8b36f51e
EJ
4072 struct priority_to_dscp *pdscp;
4073
81b1afb1
EJ
4074 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
4075 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4076 return;
4077 }
8b36f51e
EJ
4078
4079 pdscp = get_priority(ofport, ctx->flow.priority);
4080 if (pdscp) {
4081 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4082 ctx->flow.nw_tos |= pdscp->dscp;
4083 }
81b1afb1
EJ
4084 } else {
4085 /* We may not have an ofport record for this port, but it doesn't hurt
4086 * to allow forwarding to it anyhow. Maybe such a port will appear
4087 * later and we're pre-populating the flow table. */
d59906fb
EJ
4088 }
4089
81b1afb1 4090 commit_odp_actions(ctx);
6ff686f2
PS
4091 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
4092 ctx->sflow_odp_port = odp_port;
4093 ctx->sflow_n_outputs++;
81b1afb1 4094 ctx->nf_output_iface = ofp_port;
8b36f51e 4095 ctx->flow.nw_tos = flow_nw_tos;
6ff686f2
PS
4096}
4097
abe529af 4098static void
5e48dc2b 4099compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
abe529af 4100{
81b1afb1 4101 compose_output_action__(ctx, ofp_port, true);
abe529af
BP
4102}
4103
4104static void
29901626
BP
4105xlate_table_action(struct action_xlate_ctx *ctx,
4106 uint16_t in_port, uint8_t table_id)
abe529af
BP
4107{
4108 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
54a9cbc9 4109 struct ofproto_dpif *ofproto = ctx->ofproto;
abe529af
BP
4110 struct rule_dpif *rule;
4111 uint16_t old_in_port;
29901626
BP
4112 uint8_t old_table_id;
4113
4114 old_table_id = ctx->table_id;
4115 ctx->table_id = table_id;
abe529af 4116
54a9cbc9 4117 /* Look up a flow with 'in_port' as the input port. */
abe529af
BP
4118 old_in_port = ctx->flow.in_port;
4119 ctx->flow.in_port = in_port;
54a9cbc9
BP
4120 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
4121
4122 /* Tag the flow. */
4123 if (table_id > 0 && table_id < N_TABLES) {
4124 struct table_dpif *table = &ofproto->tables[table_id];
4125 if (table->other_table) {
4126 ctx->tags |= (rule
4127 ? rule->tag
4128 : rule_calculate_tag(&ctx->flow,
4129 &table->other_table->wc,
4130 table->basis));
4131 }
4132 }
4133
4134 /* Restore the original input port. Otherwise OFPP_NORMAL and
4135 * OFPP_IN_PORT will have surprising behavior. */
abe529af
BP
4136 ctx->flow.in_port = old_in_port;
4137
4138 if (ctx->resubmit_hook) {
4139 ctx->resubmit_hook(ctx, rule);
4140 }
4141
4142 if (rule) {
4143 ctx->recurse++;
4144 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
4145 ctx->recurse--;
4146 }
29901626
BP
4147
4148 ctx->table_id = old_table_id;
abe529af
BP
4149 } else {
4150 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
4151
29901626 4152 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
abe529af
BP
4153 MAX_RESUBMIT_RECURSION);
4154 }
4155}
4156
29901626
BP
4157static void
4158xlate_resubmit_table(struct action_xlate_ctx *ctx,
4159 const struct nx_action_resubmit *nar)
4160{
4161 uint16_t in_port;
4162 uint8_t table_id;
4163
4164 in_port = (nar->in_port == htons(OFPP_IN_PORT)
4165 ? ctx->flow.in_port
4166 : ntohs(nar->in_port));
4167 table_id = nar->table == 255 ? ctx->table_id : nar->table;
4168
4169 xlate_table_action(ctx, in_port, table_id);
4170}
4171
abe529af 4172static void
d59906fb 4173flood_packets(struct action_xlate_ctx *ctx, bool all)
abe529af
BP
4174{
4175 struct ofport_dpif *ofport;
4176
b3e9b2ed
EJ
4177 commit_odp_actions(ctx);
4178 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
abe529af 4179 uint16_t ofp_port = ofport->up.ofp_port;
d59906fb
EJ
4180
4181 if (ofp_port == ctx->flow.in_port) {
4182 continue;
4183 }
4184
5e48dc2b 4185 if (all) {
81b1afb1 4186 compose_output_action__(ctx, ofp_port, false);
5e48dc2b
EJ
4187 } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) {
4188 compose_output_action(ctx, ofp_port);
abe529af
BP
4189 }
4190 }
b3e9b2ed
EJ
4191
4192 ctx->nf_output_iface = NF_OUT_FLOOD;
abe529af
BP
4193}
4194
6ff686f2 4195static void
98403001 4196compose_controller_action(struct action_xlate_ctx *ctx, int len)
6ff686f2
PS
4197{
4198 struct user_action_cookie cookie;
4199
81b1afb1 4200 commit_odp_actions(ctx);
6ff686f2
PS
4201 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
4202 cookie.data = len;
4203 cookie.n_output = 0;
4204 cookie.vlan_tci = 0;
98403001 4205 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
6ff686f2
PS
4206}
4207
abe529af
BP
4208static void
4209xlate_output_action__(struct action_xlate_ctx *ctx,
4210 uint16_t port, uint16_t max_len)
4211{
4212 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
4213
4214 ctx->nf_output_iface = NF_OUT_DROP;
4215
4216 switch (port) {
4217 case OFPP_IN_PORT:
81b1afb1 4218 compose_output_action(ctx, ctx->flow.in_port);
abe529af
BP
4219 break;
4220 case OFPP_TABLE:
29901626 4221 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
abe529af
BP
4222 break;
4223 case OFPP_NORMAL:
4224 xlate_normal(ctx);
4225 break;
4226 case OFPP_FLOOD:
d59906fb 4227 flood_packets(ctx, false);
abe529af
BP
4228 break;
4229 case OFPP_ALL:
d59906fb 4230 flood_packets(ctx, true);
abe529af
BP
4231 break;
4232 case OFPP_CONTROLLER:
98403001 4233 compose_controller_action(ctx, max_len);
abe529af
BP
4234 break;
4235 case OFPP_LOCAL:
81b1afb1 4236 compose_output_action(ctx, OFPP_LOCAL);
abe529af 4237 break;
e81d2933
EJ
4238 case OFPP_NONE:
4239 break;
abe529af
BP
4240 default:
4241 if (port != ctx->flow.in_port) {
81b1afb1 4242 compose_output_action(ctx, port);
abe529af
BP
4243 }
4244 break;
4245 }
4246
4247 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4248 ctx->nf_output_iface = NF_OUT_FLOOD;
4249 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4250 ctx->nf_output_iface = prev_nf_output_iface;
4251 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4252 ctx->nf_output_iface != NF_OUT_FLOOD) {
4253 ctx->nf_output_iface = NF_OUT_MULTI;
4254 }
4255}
4256
f694937d
EJ
4257static void
4258xlate_output_reg_action(struct action_xlate_ctx *ctx,
4259 const struct nx_action_output_reg *naor)
4260{
4261 uint64_t ofp_port;
4262
4263 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
4264
4265 if (ofp_port <= UINT16_MAX) {
4266 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
4267 }
4268}
4269
abe529af
BP
4270static void
4271xlate_output_action(struct action_xlate_ctx *ctx,
4272 const struct ofp_action_output *oao)
4273{
4274 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
4275}
4276
abe529af
BP
4277static void
4278xlate_enqueue_action(struct action_xlate_ctx *ctx,
4279 const struct ofp_action_enqueue *oae)
4280{
e479e41e 4281 uint16_t ofp_port;
abff858b 4282 uint32_t flow_priority, priority;
abe529af
BP
4283 int error;
4284
4285 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4286 &priority);
4287 if (error) {
4288 /* Fall back to ordinary output action. */
4289 xlate_output_action__(ctx, ntohs(oae->port), 0);
4290 return;
4291 }
4292
df2c07f4 4293 /* Figure out datapath output port. */
abe529af
BP
4294 ofp_port = ntohs(oae->port);
4295 if (ofp_port == OFPP_IN_PORT) {
4296 ofp_port = ctx->flow.in_port;
8ba855c1
BP
4297 } else if (ofp_port == ctx->flow.in_port) {
4298 return;
abe529af 4299 }
abe529af 4300
df2c07f4 4301 /* Add datapath actions. */
abff858b
PS
4302 flow_priority = ctx->flow.priority;
4303 ctx->flow.priority = priority;
81b1afb1 4304 compose_output_action(ctx, ofp_port);
abff858b 4305 ctx->flow.priority = flow_priority;
abe529af
BP
4306
4307 /* Update NetFlow output port. */
4308 if (ctx->nf_output_iface == NF_OUT_DROP) {
4b23aebf 4309 ctx->nf_output_iface = ofp_port;
abe529af
BP
4310 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4311 ctx->nf_output_iface = NF_OUT_MULTI;
4312 }
4313}
4314
4315static void
4316xlate_set_queue_action(struct action_xlate_ctx *ctx,
4317 const struct nx_action_set_queue *nasq)
4318{
4319 uint32_t priority;
4320 int error;
4321
4322 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4323 &priority);
4324 if (error) {
4325 /* Couldn't translate queue to a priority, so ignore. A warning
4326 * has already been logged. */
4327 return;
4328 }
4329
abff858b 4330 ctx->flow.priority = priority;
abe529af
BP
4331}
4332
4333struct xlate_reg_state {
4334 ovs_be16 vlan_tci;
4335 ovs_be64 tun_id;
4336};
4337
abe529af
BP
4338static void
4339xlate_autopath(struct action_xlate_ctx *ctx,
4340 const struct nx_action_autopath *naa)
4341{
4342 uint16_t ofp_port = ntohl(naa->id);
4343 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4344
4345 if (!port || !port->bundle) {
4346 ofp_port = OFPP_NONE;
4347 } else if (port->bundle->bond) {
4348 /* Autopath does not support VLAN hashing. */
4349 struct ofport_dpif *slave = bond_choose_output_slave(
dc155bff 4350 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
abe529af
BP
4351 if (slave) {
4352 ofp_port = slave->up.ofp_port;
4353 }
4354 }
4355 autopath_execute(naa, &ctx->flow, ofp_port);
4356}
4357
daff3353
EJ
4358static bool
4359slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4360{
4361 struct ofproto_dpif *ofproto = ofproto_;
4362 struct ofport_dpif *port;
4363
4364 switch (ofp_port) {
4365 case OFPP_IN_PORT:
4366 case OFPP_TABLE:
4367 case OFPP_NORMAL:
4368 case OFPP_FLOOD:
4369 case OFPP_ALL:
439e4d8c 4370 case OFPP_NONE:
daff3353
EJ
4371 return true;
4372 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4373 return false;
4374 default:
4375 port = get_ofp_port(ofproto, ofp_port);
4376 return port ? port->may_enable : false;
4377 }
4378}
4379
75a75043
BP
4380static void
4381xlate_learn_action(struct action_xlate_ctx *ctx,
4382 const struct nx_action_learn *learn)
4383{
4384 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4385 struct ofputil_flow_mod fm;
4386 int error;
4387
4388 learn_execute(learn, &ctx->flow, &fm);
4389
4390 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4391 if (error && !VLOG_DROP_WARN(&rl)) {
4392 char *msg = ofputil_error_to_string(error);
4393 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4394 free(msg);
4395 }
4396
4397 free(fm.actions);
4398}
4399
21f7563c
JP
4400static bool
4401may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4402{
4403 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4404 ? htonl(OFPPC_NO_RECV_STP)
4405 : htonl(OFPPC_NO_RECV))) {
4406 return false;
4407 }
4408
4409 /* Only drop packets here if both forwarding and learning are
4410 * disabled. If just learning is enabled, we need to have
4411 * OFPP_NORMAL and the learning action have a look at the packet
4412 * before we can drop it. */
4413 if (!stp_forward_in_state(port->stp_state)
4414 && !stp_learn_in_state(port->stp_state)) {
4415 return false;
4416 }
4417
4418 return true;
4419}
4420
abe529af
BP
4421static void
4422do_xlate_actions(const union ofp_action *in, size_t n_in,
4423 struct action_xlate_ctx *ctx)
4424{
4425 const struct ofport_dpif *port;
abe529af 4426 const union ofp_action *ia;
b4b8c781 4427 size_t left;
abe529af
BP
4428
4429 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
21f7563c 4430 if (port && !may_receive(port, ctx)) {
abe529af
BP
4431 /* Drop this flow. */
4432 return;
4433 }
4434
b4b8c781 4435 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
abe529af 4436 const struct ofp_action_dl_addr *oada;
38f2e360
BP
4437 const struct nx_action_resubmit *nar;
4438 const struct nx_action_set_tunnel *nast;
4439 const struct nx_action_set_queue *nasq;
4440 const struct nx_action_multipath *nam;
4441 const struct nx_action_autopath *naa;
daff3353 4442 const struct nx_action_bundle *nab;
f694937d 4443 const struct nx_action_output_reg *naor;
38f2e360
BP
4444 enum ofputil_action_code code;
4445 ovs_be64 tun_id;
4446
848e8809
EJ
4447 if (ctx->exit) {
4448 break;
4449 }
4450
38f2e360
BP
4451 code = ofputil_decode_action_unsafe(ia);
4452 switch (code) {
4453 case OFPUTIL_OFPAT_OUTPUT:
abe529af
BP
4454 xlate_output_action(ctx, &ia->output);
4455 break;
4456
38f2e360 4457 case OFPUTIL_OFPAT_SET_VLAN_VID:
abe529af
BP
4458 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4459 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
abe529af
BP
4460 break;
4461
38f2e360 4462 case OFPUTIL_OFPAT_SET_VLAN_PCP:
abe529af
BP
4463 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4464 ctx->flow.vlan_tci |= htons(
4465 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
abe529af
BP
4466 break;
4467
38f2e360 4468 case OFPUTIL_OFPAT_STRIP_VLAN:
abe529af 4469 ctx->flow.vlan_tci = htons(0);
abe529af
BP
4470 break;
4471
38f2e360 4472 case OFPUTIL_OFPAT_SET_DL_SRC:
abe529af 4473 oada = ((struct ofp_action_dl_addr *) ia);
abe529af
BP
4474 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4475 break;
4476
38f2e360 4477 case OFPUTIL_OFPAT_SET_DL_DST:
abe529af 4478 oada = ((struct ofp_action_dl_addr *) ia);
abe529af
BP
4479 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4480 break;
4481
38f2e360 4482 case OFPUTIL_OFPAT_SET_NW_SRC:
abe529af
BP
4483 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4484 break;
4485
38f2e360 4486 case OFPUTIL_OFPAT_SET_NW_DST:
abe529af
BP
4487 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4488 break;
4489
38f2e360 4490 case OFPUTIL_OFPAT_SET_NW_TOS:
eadef313
JP
4491 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4492 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
abe529af
BP
4493 break;
4494
38f2e360 4495 case OFPUTIL_OFPAT_SET_TP_SRC:
abe529af
BP
4496 ctx->flow.tp_src = ia->tp_port.tp_port;
4497 break;
4498
38f2e360 4499 case OFPUTIL_OFPAT_SET_TP_DST:
abe529af
BP
4500 ctx->flow.tp_dst = ia->tp_port.tp_port;
4501 break;
4502
38f2e360
BP
4503 case OFPUTIL_OFPAT_ENQUEUE:
4504 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4505 break;
4506
4507 case OFPUTIL_NXAST_RESUBMIT:
4508 nar = (const struct nx_action_resubmit *) ia;
29901626
BP
4509 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4510 break;
4511
4512 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4513 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
abe529af
BP
4514 break;
4515
38f2e360
BP
4516 case OFPUTIL_NXAST_SET_TUNNEL:
4517 nast = (const struct nx_action_set_tunnel *) ia;
4518 tun_id = htonll(ntohl(nast->tun_id));
4519 ctx->flow.tun_id = tun_id;
4520 break;
4521
4522 case OFPUTIL_NXAST_SET_QUEUE:
4523 nasq = (const struct nx_action_set_queue *) ia;
4524 xlate_set_queue_action(ctx, nasq);
4525 break;
4526
4527 case OFPUTIL_NXAST_POP_QUEUE:
abff858b 4528 ctx->flow.priority = ctx->original_priority;
38f2e360
BP
4529 break;
4530
4531 case OFPUTIL_NXAST_REG_MOVE:
4532 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4533 &ctx->flow);
4534 break;
4535
4536 case OFPUTIL_NXAST_REG_LOAD:
4537 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4538 &ctx->flow);
4539 break;
4540
4541 case OFPUTIL_NXAST_NOTE:
4542 /* Nothing to do. */
4543 break;
4544
4545 case OFPUTIL_NXAST_SET_TUNNEL64:
4546 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4547 ctx->flow.tun_id = tun_id;
4548 break;
4549
4550 case OFPUTIL_NXAST_MULTIPATH:
4551 nam = (const struct nx_action_multipath *) ia;
4552 multipath_execute(nam, &ctx->flow);
abe529af
BP
4553 break;
4554
38f2e360
BP
4555 case OFPUTIL_NXAST_AUTOPATH:
4556 naa = (const struct nx_action_autopath *) ia;
4557 xlate_autopath(ctx, naa);
abe529af 4558 break;
daff3353
EJ
4559
4560 case OFPUTIL_NXAST_BUNDLE:
4561 ctx->ofproto->has_bundle_action = true;
4562 nab = (const struct nx_action_bundle *) ia;
4563 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4564 slave_enabled_cb,
4565 ctx->ofproto), 0);
4566 break;
a368bb53
EJ
4567
4568 case OFPUTIL_NXAST_BUNDLE_LOAD:
4569 ctx->ofproto->has_bundle_action = true;
4570 nab = (const struct nx_action_bundle *) ia;
4571 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4572 ctx->ofproto);
4573 break;
f694937d
EJ
4574
4575 case OFPUTIL_NXAST_OUTPUT_REG:
4576 naor = (const struct nx_action_output_reg *) ia;
4577 xlate_output_reg_action(ctx, naor);
4578 break;
75a75043
BP
4579
4580 case OFPUTIL_NXAST_LEARN:
4581 ctx->has_learn = true;
4582 if (ctx->may_learn) {
4583 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4584 }
4585 break;
848e8809
EJ
4586
4587 case OFPUTIL_NXAST_EXIT:
4588 ctx->exit = true;
4589 break;
abe529af
BP
4590 }
4591 }
21f7563c
JP
4592
4593 /* We've let OFPP_NORMAL and the learning action look at the packet,
4594 * so drop it now if forwarding is disabled. */
4595 if (port && !stp_forward_in_state(port->stp_state)) {
4596 ofpbuf_clear(ctx->odp_actions);
4597 add_sflow_action(ctx);
4598 }
abe529af
BP
4599}
4600
4601static void
4602action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4603 struct ofproto_dpif *ofproto, const struct flow *flow,
e84173dc 4604 ovs_be16 initial_tci, const struct ofpbuf *packet)
abe529af
BP
4605{
4606 ctx->ofproto = ofproto;
4607 ctx->flow = *flow;
e84173dc
BP
4608 ctx->base_flow = ctx->flow;
4609 ctx->base_flow.tun_id = 0;
4610 ctx->base_flow.vlan_tci = initial_tci;
abe529af 4611 ctx->packet = packet;
75a75043 4612 ctx->may_learn = packet != NULL;
abe529af 4613 ctx->resubmit_hook = NULL;
abe529af
BP
4614}
4615
4616static struct ofpbuf *
4617xlate_actions(struct action_xlate_ctx *ctx,
4618 const union ofp_action *in, size_t n_in)
4619{
4620 COVERAGE_INC(ofproto_dpif_xlate);
4621
4622 ctx->odp_actions = ofpbuf_new(512);
b6848f13 4623 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
97e42c92
BP
4624 ctx->tags = 0;
4625 ctx->may_set_up_flow = true;
4626 ctx->has_learn = false;
4627 ctx->has_normal = false;
4628 ctx->nf_output_iface = NF_OUT_DROP;
4629 ctx->recurse = 0;
abff858b 4630 ctx->original_priority = ctx->flow.priority;
97e42c92 4631 ctx->table_id = 0;
848e8809 4632 ctx->exit = false;
7257b535 4633
eadef313 4634 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
7257b535
BP
4635 switch (ctx->ofproto->up.frag_handling) {
4636 case OFPC_FRAG_NORMAL:
4637 /* We must pretend that transport ports are unavailable. */
97e42c92
BP
4638 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4639 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
7257b535
BP
4640 break;
4641
4642 case OFPC_FRAG_DROP:
4643 return ctx->odp_actions;
4644
4645 case OFPC_FRAG_REASM:
4646 NOT_REACHED();
4647
4648 case OFPC_FRAG_NX_MATCH:
4649 /* Nothing to do. */
4650 break;
4651 }
4652 }
4653
fc08b7a2 4654 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
abe529af 4655 ctx->may_set_up_flow = false;
b6848f13 4656 return ctx->odp_actions;
abe529af 4657 } else {
6ff686f2 4658 add_sflow_action(ctx);
abe529af 4659 do_xlate_actions(in, n_in, ctx);
abe529af 4660
b6848f13
BP
4661 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4662 ctx->odp_actions->data,
4663 ctx->odp_actions->size)) {
4664 ctx->may_set_up_flow = false;
4665 if (ctx->packet
4666 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4667 ctx->packet)) {
5e48dc2b 4668 compose_output_action(ctx, OFPP_LOCAL);
b6848f13
BP
4669 }
4670 }
a7c4eaf6 4671 fix_sflow_action(ctx);
abe529af
BP
4672 }
4673
4674 return ctx->odp_actions;
4675}
4676\f
4677/* OFPP_NORMAL implementation. */
4678
abe529af
BP
4679static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4680
ecac4ebf
BP
4681/* Given 'vid', the VID obtained from the 802.1Q header that was received as
4682 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4683 * the bundle on which the packet was received, returns the VLAN to which the
4684 * packet belongs.
4685 *
4686 * Both 'vid' and the return value are in the range 0...4095. */
4687static uint16_t
4688input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4689{
4690 switch (in_bundle->vlan_mode) {
4691 case PORT_VLAN_ACCESS:
4692 return in_bundle->vlan;
4693 break;
4694
4695 case PORT_VLAN_TRUNK:
4696 return vid;
4697
4698 case PORT_VLAN_NATIVE_UNTAGGED:
4699 case PORT_VLAN_NATIVE_TAGGED:
4700 return vid ? vid : in_bundle->vlan;
4701
4702 default:
4703 NOT_REACHED();
4704 }
4705}
4706
5da5ec37
BP
4707/* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4708 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4709 * a warning.
4710 *
4711 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4712 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4713 * 0...4095. */
4714static bool
4715input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4716{
4717 switch (in_bundle->vlan_mode) {
4718 case PORT_VLAN_ACCESS:
4719 if (vid) {
4720 if (warn) {
4721 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4722 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4723 "packet received on port %s configured as VLAN "
4724 "%"PRIu16" access port",
4725 in_bundle->ofproto->up.name, vid,
4726 in_bundle->name, in_bundle->vlan);
4727 }
4728 return false;
4729 }
4730 return true;
4731
4732 case PORT_VLAN_NATIVE_UNTAGGED:
4733 case PORT_VLAN_NATIVE_TAGGED:
4734 if (!vid) {
4735 /* Port must always carry its native VLAN. */
4736 return true;
4737 }
4738 /* Fall through. */
4739 case PORT_VLAN_TRUNK:
4740 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4741 if (warn) {
4742 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4743 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4744 "received on port %s not configured for trunking "
4745 "VLAN %"PRIu16,
4746 in_bundle->ofproto->up.name, vid,
4747 in_bundle->name, vid);
4748 }
4749 return false;
4750 }
4751 return true;
4752
4753 default:
4754 NOT_REACHED();
4755 }
4756
4757}
4758
ecac4ebf
BP
4759/* Given 'vlan', the VLAN that a packet belongs to, and
4760 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4761 * that should be included in the 802.1Q header. (If the return value is 0,
4762 * then the 802.1Q header should only be included in the packet if there is a
4763 * nonzero PCP.)
4764 *
4765 * Both 'vlan' and the return value are in the range 0...4095. */
4766static uint16_t
4767output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4768{
4769 switch (out_bundle->vlan_mode) {
4770 case PORT_VLAN_ACCESS:
4771 return 0;
4772
4773 case PORT_VLAN_TRUNK:
4774 case PORT_VLAN_NATIVE_TAGGED:
4775 return vlan;
4776
4777 case PORT_VLAN_NATIVE_UNTAGGED:
4778 return vlan == out_bundle->vlan ? 0 : vlan;
4779
4780 default:
4781 NOT_REACHED();
4782 }
4783}
4784
395e68ce
BP
4785static void
4786output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
4787 uint16_t vlan)
abe529af 4788{
395e68ce
BP
4789 struct ofport_dpif *port;
4790 uint16_t vid;
81b1afb1 4791 ovs_be16 tci, old_tci;
ecac4ebf 4792
395e68ce
BP
4793 vid = output_vlan_to_vid(out_bundle, vlan);
4794 if (!out_bundle->bond) {
4795 port = ofbundle_get_a_port(out_bundle);
4796 } else {
4797 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4798 vid, &ctx->tags);
4799 if (!port) {
4800 /* No slaves enabled, so drop packet. */
4801 return;
4802 }
4803 }
abe529af 4804
81b1afb1 4805 old_tci = ctx->flow.vlan_tci;
5e9ceccd
BP
4806 tci = htons(vid);
4807 if (tci || out_bundle->use_priority_tags) {
4808 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4809 if (tci) {
4810 tci |= htons(VLAN_CFI);
4811 }
395e68ce 4812 }
81b1afb1 4813 ctx->flow.vlan_tci = tci;
395e68ce 4814
5e48dc2b 4815 compose_output_action(ctx, port->up.ofp_port);
81b1afb1 4816 ctx->flow.vlan_tci = old_tci;
abe529af
BP
4817}
4818
4819static int
4820mirror_mask_ffs(mirror_mask_t mask)
4821{
4822 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4823 return ffs(mask);
4824}
4825
abe529af
BP
4826static bool
4827ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4828{
ecac4ebf 4829 return (bundle->vlan_mode != PORT_VLAN_ACCESS
fc3d7408 4830 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
abe529af
BP
4831}
4832
4833static bool
4834ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4835{
4836 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4837}
4838
4839/* Returns an arbitrary interface within 'bundle'. */
4840static struct ofport_dpif *
4841ofbundle_get_a_port(const struct ofbundle *bundle)
4842{
4843 return CONTAINER_OF(list_front(&bundle->ports),
4844 struct ofport_dpif, bundle_node);
4845}
4846
395e68ce 4847static mirror_mask_t
abe529af
BP
4848compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4849 const struct ofbundle *in_bundle,
395e68ce 4850 const struct ofbundle *out_bundle)
abe529af 4851{
395e68ce 4852 mirror_mask_t dst_mirrors = 0;
abe529af
BP
4853
4854 if (out_bundle == OFBUNDLE_FLOOD) {
4855 struct ofbundle *bundle;
4856
4857 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4858 if (bundle != in_bundle
4859 && ofbundle_includes_vlan(bundle, vlan)
4860 && bundle->floodable
395e68ce
BP
4861 && !bundle->mirror_out) {
4862 output_normal(ctx, bundle, vlan);
4863 dst_mirrors |= bundle->dst_mirrors;
abe529af
BP
4864 }
4865 }
4866 ctx->nf_output_iface = NF_OUT_FLOOD;
395e68ce
BP
4867 } else if (out_bundle) {
4868 output_normal(ctx, out_bundle, vlan);
4869 dst_mirrors = out_bundle->dst_mirrors;
abe529af 4870 }
395e68ce
BP
4871
4872 return dst_mirrors;
abe529af
BP
4873}
4874
4875static bool
4876vlan_is_mirrored(const struct ofmirror *m, int vlan)
4877{
fc3d7408 4878 return !m->vlans || bitmap_is_set(m->vlans, vlan);
abe529af
BP
4879}
4880
07817dfe
BP
4881/* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4882 * to a VLAN. In general most packets may be mirrored but we want to drop
4883 * protocols that may confuse switches. */
4884static bool
4885eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4886{
4887 /* If you change this function's behavior, please update corresponding
4888 * documentation in vswitch.xml at the same time. */
4889 if (dst[0] != 0x01) {
4890 /* All the currently banned MACs happen to start with 01 currently, so
4891 * this is a quick way to eliminate most of the good ones. */
4892 } else {
4893 if (eth_addr_is_reserved(dst)) {
4894 /* Drop STP, IEEE pause frames, and other reserved protocols
4895 * (01-80-c2-00-00-0x). */
4896 return false;
4897 }
4898
4899 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4900 /* Cisco OUI. */
4901 if ((dst[3] & 0xfe) == 0xcc &&
4902 (dst[4] & 0xfe) == 0xcc &&
4903 (dst[5] & 0xfe) == 0xcc) {
4904 /* Drop the following protocols plus others following the same
4905 pattern:
4906
4907 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4908 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4909 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4910 return false;
4911 }
4912
4913 if (!(dst[3] | dst[4] | dst[5])) {
4914 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4915 return false;
4916 }
4917 }
4918 }
4919 return true;
4920}
4921
abe529af 4922static void
395e68ce
BP
4923output_mirrors(struct action_xlate_ctx *ctx,
4924 uint16_t vlan, const struct ofbundle *in_bundle,
4925 mirror_mask_t dst_mirrors)
abe529af
BP
4926{
4927 struct ofproto_dpif *ofproto = ctx->ofproto;
4928 mirror_mask_t mirrors;
abe529af 4929
395e68ce 4930 mirrors = in_bundle->src_mirrors | dst_mirrors;
abe529af
BP
4931 if (!mirrors) {
4932 return;
4933 }
4934
9ba15e2a
BP
4935 while (mirrors) {
4936 struct ofmirror *m;
9ba15e2a
BP
4937
4938 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4939
4940 if (!vlan_is_mirrored(m, vlan)) {
4941 mirrors &= mirrors - 1;
4942 continue;
4943 }
4944
4945 mirrors &= ~m->dup_mirrors;
4946 if (m->out) {
395e68ce 4947 output_normal(ctx, m->out, vlan);
9ba15e2a
BP
4948 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)
4949 && vlan != m->out_vlan) {
4950 struct ofbundle *bundle;
4951
4952 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4953 if (ofbundle_includes_vlan(bundle, m->out_vlan)
395e68ce
BP
4954 && !bundle->mirror_out) {
4955 output_normal(ctx, bundle, m->out_vlan);
abe529af
BP
4956 }
4957 }
4958 }
abe529af
BP
4959 }
4960}
4961
abe529af
BP
4962/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4963 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4964 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4965static bool
4966is_gratuitous_arp(const struct flow *flow)
4967{
4968 return (flow->dl_type == htons(ETH_TYPE_ARP)
4969 && eth_addr_is_broadcast(flow->dl_dst)
4970 && (flow->nw_proto == ARP_OP_REPLY
4971 || (flow->nw_proto == ARP_OP_REQUEST
4972 && flow->nw_src == flow->nw_dst)));
4973}
4974
4975static void
4976update_learning_table(struct ofproto_dpif *ofproto,
4977 const struct flow *flow, int vlan,
4978 struct ofbundle *in_bundle)
4979{
4980 struct mac_entry *mac;
4981
4982 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4983 return;
4984 }
4985
4986 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4987 if (is_gratuitous_arp(flow)) {
4988 /* We don't want to learn from gratuitous ARP packets that are
4989 * reflected back over bond slaves so we lock the learning table. */
4990 if (!in_bundle->bond) {
4991 mac_entry_set_grat_arp_lock(mac);
4992 } else if (mac_entry_is_grat_arp_locked(mac)) {
4993 return;
4994 }
4995 }
4996
4997 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4998 /* The log messages here could actually be useful in debugging,
4999 * so keep the rate limit relatively high. */
5000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
5001 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
5002 "on port %s in VLAN %d",
5003 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
5004 in_bundle->name, vlan);
5005
5006 mac->port.p = in_bundle;
5007 tag_set_add(&ofproto->revalidate_set,
5008 mac_learning_changed(ofproto->ml, mac));
5009 }
5010}
5011
395e68ce
BP
5012static struct ofport_dpif *
5013lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn)
5014{
5015 struct ofport_dpif *ofport;
5016
5017 /* Find the port and bundle for the received packet. */
5018 ofport = get_ofp_port(ofproto, in_port);
5019 if (ofport && ofport->bundle) {
5020 return ofport;
5021 }
5022
5023 /* Odd. A few possible reasons here:
5024 *
5025 * - We deleted a port but there are still a few packets queued up
5026 * from it.
5027 *
5028 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
5029 * we don't know about.
5030 *
5031 * - The ofproto client didn't configure the port as part of a bundle.
5032 */
5033 if (warn) {
5034 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5035
5036 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
5037 "port %"PRIu16, ofproto->up.name, in_port);
5038 }
5039 return NULL;
5040}
5041
5da5ec37 5042/* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
abe529af
BP
5043 * dropped. Returns true if they may be forwarded, false if they should be
5044 * dropped.
5045 *
395e68ce
BP
5046 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
5047 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
abe529af 5048 *
395e68ce
BP
5049 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
5050 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
5051 * checked by input_vid_is_valid().
abe529af
BP
5052 *
5053 * May also add tags to '*tags', although the current implementation only does
5054 * so in one special case.
5055 */
5056static bool
5057is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
395e68ce 5058 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
abe529af 5059{
395e68ce 5060 struct ofbundle *in_bundle = in_port->bundle;
abe529af 5061
395e68ce
BP
5062 /* Drop frames for reserved multicast addresses
5063 * only if forward_bpdu option is absent. */
21f7563c 5064 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
abe529af
BP
5065 return false;
5066 }
5067
abe529af
BP
5068 if (in_bundle->bond) {
5069 struct mac_entry *mac;
5070
5071 switch (bond_check_admissibility(in_bundle->bond, in_port,
5072 flow->dl_dst, tags)) {
5073 case BV_ACCEPT:
5074 break;
5075
5076 case BV_DROP:
5077 return false;
5078
5079 case BV_DROP_IF_MOVED:
5080 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
5081 if (mac && mac->port.p != in_bundle &&
5082 (!is_gratuitous_arp(flow)
5083 || mac_entry_is_grat_arp_locked(mac))) {
5084 return false;
5085 }
5086 break;
5087 }
5088 }
5089
5090 return true;
5091}
5092
4cd78906 5093static void
abe529af
BP
5094xlate_normal(struct action_xlate_ctx *ctx)
5095{
395e68ce
BP
5096 mirror_mask_t dst_mirrors = 0;
5097 struct ofport_dpif *in_port;
abe529af
BP
5098 struct ofbundle *in_bundle;
5099 struct ofbundle *out_bundle;
5100 struct mac_entry *mac;
395e68ce
BP
5101 uint16_t vlan;
5102 uint16_t vid;
abe529af 5103
75a75043
BP
5104 ctx->has_normal = true;
5105
395e68ce
BP
5106 /* Obtain in_port from ctx->flow.in_port.
5107 *
5108 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
5109 in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
5110 ctx->packet != NULL);
5111 if (!in_port) {
5112 return;
5113 }
5114 in_bundle = in_port->bundle;
5115
5116 /* Drop malformed frames. */
5117 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
5118 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
5119 if (ctx->packet != NULL) {
5120 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5121 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
5122 "VLAN tag received on port %s",
5123 ctx->ofproto->up.name, in_bundle->name);
5124 }
5125 return;
5126 }
5127
5128 /* Drop frames on bundles reserved for mirroring. */
5129 if (in_bundle->mirror_out) {
5130 if (ctx->packet != NULL) {
5131 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5132 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5133 "%s, which is reserved exclusively for mirroring",
5134 ctx->ofproto->up.name, in_bundle->name);
5135 }
5136 return;
5137 }
5138
5139 /* Check VLAN. */
5140 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
5141 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5142 return;
5143 }
5144 vlan = input_vid_to_vlan(in_bundle, vid);
5145
5146 /* Check other admissibility requirements. */
5147 if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
5148 output_mirrors(ctx, vlan, in_bundle, 0);
5149 return;
abe529af
BP
5150 }
5151
75a75043
BP
5152 /* Learn source MAC. */
5153 if (ctx->may_learn) {
abe529af
BP
5154 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
5155 }
5156
5157 /* Determine output bundle. */
5158 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
5159 &ctx->tags);
5160 if (mac) {
5161 out_bundle = mac->port.p;
5162 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
5163 /* If we are revalidating but don't have a learning entry then eject
5164 * the flow. Installing a flow that floods packets opens up a window
5165 * of time where we could learn from a packet reflected on a bond and
5166 * blackhole packets before the learning table is updated to reflect
5167 * the correct port. */
4cd78906
BP
5168 ctx->may_set_up_flow = false;
5169 return;
abe529af
BP
5170 } else {
5171 out_bundle = OFBUNDLE_FLOOD;
5172 }
5173
5174 /* Don't send packets out their input bundles. */
395e68ce
BP
5175 if (in_bundle != out_bundle) {
5176 dst_mirrors = compose_dsts(ctx, vlan, in_bundle, out_bundle);
abe529af 5177 }
395e68ce 5178 output_mirrors(ctx, vlan, in_bundle, dst_mirrors);
abe529af
BP
5179}
5180\f
54a9cbc9
BP
5181/* Optimized flow revalidation.
5182 *
5183 * It's a difficult problem, in general, to tell which facets need to have
5184 * their actions recalculated whenever the OpenFlow flow table changes. We
5185 * don't try to solve that general problem: for most kinds of OpenFlow flow
5186 * table changes, we recalculate the actions for every facet. This is
5187 * relatively expensive, but it's good enough if the OpenFlow flow table
5188 * doesn't change very often.
5189 *
5190 * However, we can expect one particular kind of OpenFlow flow table change to
5191 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
5192 * of CPU on revalidating every facet whenever MAC learning modifies the flow
5193 * table, we add a special case that applies to flow tables in which every rule
5194 * has the same form (that is, the same wildcards), except that the table is
5195 * also allowed to have a single "catch-all" flow that matches all packets. We
5196 * optimize this case by tagging all of the facets that resubmit into the table
5197 * and invalidating the same tag whenever a flow changes in that table. The
5198 * end result is that we revalidate just the facets that need it (and sometimes
5199 * a few more, but not all of the facets or even all of the facets that
5200 * resubmit to the table modified by MAC learning). */
5201
5202/* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5203 * into an OpenFlow table with the given 'basis'. */
5204static uint32_t
5205rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5206 uint32_t secret)
5207{
5208 if (flow_wildcards_is_catchall(wc)) {
5209 return 0;
5210 } else {
5211 struct flow tag_flow = *flow;
5212 flow_zero_wildcards(&tag_flow, wc);
5213 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5214 }
5215}
5216
5217/* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5218 * taggability of that table.
5219 *
5220 * This function must be called after *each* change to a flow table. If you
5221 * skip calling it on some changes then the pointer comparisons at the end can
5222 * be invalid if you get unlucky. For example, if a flow removal causes a
5223 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5224 * different wildcards to be created with the same address, then this function
5225 * will incorrectly skip revalidation. */
5226static void
5227table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5228{
5229 struct table_dpif *table = &ofproto->tables[table_id];
5230 const struct classifier *cls = &ofproto->up.tables[table_id];
5231 struct cls_table *catchall, *other;
5232 struct cls_table *t;
5233
5234 catchall = other = NULL;
5235
5236 switch (hmap_count(&cls->tables)) {
5237 case 0:
5238 /* We could tag this OpenFlow table but it would make the logic a
5239 * little harder and it's a corner case that doesn't seem worth it
5240 * yet. */
5241 break;
5242
5243 case 1:
5244 case 2:
5245 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5246 if (cls_table_is_catchall(t)) {
5247 catchall = t;
5248 } else if (!other) {
5249 other = t;
5250 } else {
5251 /* Indicate that we can't tag this by setting both tables to
5252 * NULL. (We know that 'catchall' is already NULL.) */
5253 other = NULL;
5254 }
5255 }
5256 break;
5257
5258 default:
5259 /* Can't tag this table. */
5260 break;
5261 }
5262
5263 if (table->catchall_table != catchall || table->other_table != other) {
5264 table->catchall_table = catchall;
5265 table->other_table = other;
5266 ofproto->need_revalidate = true;
5267 }
5268}
5269
5270/* Given 'rule' that has changed in some way (either it is a rule being
5271 * inserted, a rule being deleted, or a rule whose actions are being
5272 * modified), marks facets for revalidation to ensure that packets will be
5273 * forwarded correctly according to the new state of the flow table.
5274 *
5275 * This function must be called after *each* change to a flow table. See
5276 * the comment on table_update_taggable() for more information. */
5277static void
5278rule_invalidate(const struct rule_dpif *rule)
5279{
5280 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5281
5282 table_update_taggable(ofproto, rule->up.table_id);
5283
5284 if (!ofproto->need_revalidate) {
5285 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5286
5287 if (table->other_table && rule->tag) {
5288 tag_set_add(&ofproto->revalidate_set, rule->tag);
5289 } else {
5290 ofproto->need_revalidate = true;
5291 }
5292 }
5293}
5294\f
abe529af 5295static bool
7257b535
BP
5296set_frag_handling(struct ofproto *ofproto_,
5297 enum ofp_config_flags frag_handling)
abe529af
BP
5298{
5299 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 5300
7257b535
BP
5301 if (frag_handling != OFPC_FRAG_REASM) {
5302 ofproto->need_revalidate = true;
5303 return true;
5304 } else {
5305 return false;
5306 }
abe529af
BP
5307}
5308
5309static int
5310packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5311 const struct flow *flow,
5312 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5313{
5314 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5315 int error;
5316
e1154f71
BP
5317 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5318 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5319 }
5320
abe529af
BP
5321 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5322 ofproto->max_ports);
5323 if (!error) {
80e5eed9 5324 struct odputil_keybuf keybuf;
abe529af
BP
5325 struct action_xlate_ctx ctx;
5326 struct ofpbuf *odp_actions;
80e5eed9
BP
5327 struct ofpbuf key;
5328
5329 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5330 odp_flow_key_from_flow(&key, flow);
abe529af 5331
e84173dc 5332 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
abe529af 5333 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
80e5eed9
BP
5334 dpif_execute(ofproto->dpif, key.data, key.size,
5335 odp_actions->data, odp_actions->size, packet);
abe529af
BP
5336 ofpbuf_delete(odp_actions);
5337 }
5338 return error;
5339}
6fca1ffb
BP
5340\f
5341/* NetFlow. */
5342
5343static int
5344set_netflow(struct ofproto *ofproto_,
5345 const struct netflow_options *netflow_options)
5346{
5347 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5348
5349 if (netflow_options) {
5350 if (!ofproto->netflow) {
5351 ofproto->netflow = netflow_create();
5352 }
5353 return netflow_set_options(ofproto->netflow, netflow_options);
5354 } else {
5355 netflow_destroy(ofproto->netflow);
5356 ofproto->netflow = NULL;
5357 return 0;
5358 }
5359}
abe529af
BP
5360
5361static void
5362get_netflow_ids(const struct ofproto *ofproto_,
5363 uint8_t *engine_type, uint8_t *engine_id)
5364{
5365 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5366
5367 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5368}
6fca1ffb
BP
5369
5370static void
5371send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5372{
5373 if (!facet_is_controller_flow(facet) &&
5374 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
b0f7b9b5 5375 struct subfacet *subfacet;
6fca1ffb
BP
5376 struct ofexpired expired;
5377
b0f7b9b5
BP
5378 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5379 if (subfacet->installed) {
5380 struct dpif_flow_stats stats;
6fca1ffb 5381
b95fc6ba
BP
5382 subfacet_install(ofproto, subfacet, subfacet->actions,
5383 subfacet->actions_len, &stats);
b0f7b9b5
BP
5384 subfacet_update_stats(ofproto, subfacet, &stats);
5385 }
6fca1ffb
BP
5386 }
5387
5388 expired.flow = facet->flow;
5389 expired.packet_count = facet->packet_count;
5390 expired.byte_count = facet->byte_count;
5391 expired.used = facet->used;
5392 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5393 }
5394}
5395
5396static void
5397send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5398{
5399 struct facet *facet;
5400
5401 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5402 send_active_timeout(ofproto, facet);
5403 }
5404}
abe529af
BP
5405\f
5406static struct ofproto_dpif *
5407ofproto_dpif_lookup(const char *name)
5408{
5409 struct ofproto *ofproto = ofproto_lookup(name);
5410 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5411 ? ofproto_dpif_cast(ofproto)
5412 : NULL);
5413}
5414
f0a3aa2e
AA
5415static void
5416ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5417 const char *args, void *aux OVS_UNUSED)
5418{
5419 const struct ofproto_dpif *ofproto;
5420
5421 ofproto = ofproto_dpif_lookup(args);
5422 if (!ofproto) {
5423 unixctl_command_reply(conn, 501, "no such bridge");
5424 return;
5425 }
5426 mac_learning_flush(ofproto->ml);
5427
5428 unixctl_command_reply(conn, 200, "table successfully flushed");
5429}
5430
abe529af
BP
5431static void
5432ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5433 const char *args, void *aux OVS_UNUSED)
5434{
5435 struct ds ds = DS_EMPTY_INITIALIZER;
5436 const struct ofproto_dpif *ofproto;
5437 const struct mac_entry *e;
5438
5439 ofproto = ofproto_dpif_lookup(args);
5440 if (!ofproto) {
5441 unixctl_command_reply(conn, 501, "no such bridge");
5442 return;
5443 }
5444
5445 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5446 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5447 struct ofbundle *bundle = e->port.p;
5448 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5449 ofbundle_get_a_port(bundle)->odp_port,
5450 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5451 }
5452 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5453 ds_destroy(&ds);
5454}
5455
5456struct ofproto_trace {
5457 struct action_xlate_ctx ctx;
5458 struct flow flow;
5459 struct ds *result;
5460};
5461
5462static void
29901626
BP
5463trace_format_rule(struct ds *result, uint8_t table_id, int level,
5464 const struct rule_dpif *rule)
abe529af
BP
5465{
5466 ds_put_char_multiple(result, '\t', level);
5467 if (!rule) {
5468 ds_put_cstr(result, "No match\n");
5469 return;
5470 }
5471
29901626
BP
5472 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5473 table_id, ntohll(rule->up.flow_cookie));
79feb7df 5474 cls_rule_format(&rule->up.cr, result);
abe529af
BP
5475 ds_put_char(result, '\n');
5476
5477 ds_put_char_multiple(result, '\t', level);
5478 ds_put_cstr(result, "OpenFlow ");
79feb7df 5479 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
abe529af
BP
5480 ds_put_char(result, '\n');
5481}
5482
5483static void
5484trace_format_flow(struct ds *result, int level, const char *title,
5485 struct ofproto_trace *trace)
5486{
5487 ds_put_char_multiple(result, '\t', level);
5488 ds_put_format(result, "%s: ", title);
5489 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5490 ds_put_cstr(result, "unchanged");
5491 } else {
5492 flow_format(result, &trace->ctx.flow);
5493 trace->flow = trace->ctx.flow;
5494 }
5495 ds_put_char(result, '\n');
5496}
5497
eb9e1c26
EJ
5498static void
5499trace_format_regs(struct ds *result, int level, const char *title,
5500 struct ofproto_trace *trace)
5501{
5502 size_t i;
5503
5504 ds_put_char_multiple(result, '\t', level);
5505 ds_put_format(result, "%s:", title);
5506 for (i = 0; i < FLOW_N_REGS; i++) {
5507 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5508 }
5509 ds_put_char(result, '\n');
5510}
5511
abe529af
BP
5512static void
5513trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5514{
5515 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5516 struct ds *result = trace->result;
5517
5518 ds_put_char(result, '\n');
5519 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
eb9e1c26 5520 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
29901626 5521 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
abe529af
BP
5522}
5523
5524static void
5525ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5526 void *aux OVS_UNUSED)
5527{
abff858b 5528 char *dpname, *arg1, *arg2, *arg3, *arg4;
abe529af
BP
5529 char *args = xstrdup(args_);
5530 char *save_ptr = NULL;
5531 struct ofproto_dpif *ofproto;
876b0e1c
BP
5532 struct ofpbuf odp_key;
5533 struct ofpbuf *packet;
abe529af 5534 struct rule_dpif *rule;
e84173dc 5535 ovs_be16 initial_tci;
abe529af
BP
5536 struct ds result;
5537 struct flow flow;
abe529af
BP
5538 char *s;
5539
876b0e1c
BP
5540 packet = NULL;
5541 ofpbuf_init(&odp_key, 0);
abe529af
BP
5542 ds_init(&result);
5543
5544 dpname = strtok_r(args, " ", &save_ptr);
e84173dc
BP
5545 if (!dpname) {
5546 unixctl_command_reply(conn, 501, "Bad command syntax");
5547 goto exit;
5548 }
5549
5550 ofproto = ofproto_dpif_lookup(dpname);
5551 if (!ofproto) {
5552 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5553 "for help)");
5554 goto exit;
5555 }
876b0e1c
BP
5556 arg1 = strtok_r(NULL, " ", &save_ptr);
5557 arg2 = strtok_r(NULL, " ", &save_ptr);
abff858b
PS
5558 arg3 = strtok_r(NULL, " ", &save_ptr);
5559 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
8b3b8dd1
BP
5560 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5561 /* ofproto/trace dpname flow [-generate] */
876b0e1c
BP
5562 int error;
5563
df2c07f4 5564 /* Convert string to datapath key. */
876b0e1c 5565 ofpbuf_init(&odp_key, 0);
b2a60db8 5566 error = odp_flow_key_from_string(arg1, NULL, &odp_key);
876b0e1c
BP
5567 if (error) {
5568 unixctl_command_reply(conn, 501, "Bad flow syntax");
5569 goto exit;
5570 }
5571
5572 /* Convert odp_key to flow. */
e84173dc
BP
5573 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
5574 odp_key.size, &flow,
5575 &initial_tci);
5576 if (error == ODP_FIT_ERROR) {
876b0e1c
BP
5577 unixctl_command_reply(conn, 501, "Invalid flow");
5578 goto exit;
5579 }
8b3b8dd1
BP
5580
5581 /* Generate a packet, if requested. */
5582 if (arg2) {
5583 packet = ofpbuf_new(0);
5584 flow_compose(packet, &flow);
5585 }
abff858b
PS
5586 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5587 /* ofproto/trace dpname priority tun_id in_port packet */
876b0e1c
BP
5588 uint16_t in_port;
5589 ovs_be64 tun_id;
abff858b 5590 uint32_t priority;
876b0e1c 5591
abff858b
PS
5592 priority = atoi(arg1);
5593 tun_id = htonll(strtoull(arg2, NULL, 0));
5594 in_port = ofp_port_to_odp_port(atoi(arg3));
876b0e1c
BP
5595
5596 packet = ofpbuf_new(strlen(args) / 2);
abff858b
PS
5597 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5598 arg4 += strspn(arg4, " ");
5599 if (*arg4 != '\0') {
876b0e1c
BP
5600 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5601 goto exit;
5602 }
5603 if (packet->size < ETH_HEADER_LEN) {
5604 unixctl_command_reply(conn, 501,
5605 "Packet data too short for Ethernet");
5606 goto exit;
5607 }
5608
5609 ds_put_cstr(&result, "Packet: ");
5610 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5611 ds_put_cstr(&result, s);
5612 free(s);
5613
abff858b 5614 flow_extract(packet, priority, tun_id, in_port, &flow);
e84173dc 5615 initial_tci = flow.vlan_tci;
876b0e1c 5616 } else {
abe529af
BP
5617 unixctl_command_reply(conn, 501, "Bad command syntax");
5618 goto exit;
5619 }
5620
abe529af
BP
5621 ds_put_cstr(&result, "Flow: ");
5622 flow_format(&result, &flow);
5623 ds_put_char(&result, '\n');
5624
29901626
BP
5625 rule = rule_dpif_lookup(ofproto, &flow, 0);
5626 trace_format_rule(&result, 0, 0, rule);
abe529af
BP
5627 if (rule) {
5628 struct ofproto_trace trace;
5629 struct ofpbuf *odp_actions;
5630
5631 trace.result = &result;
5632 trace.flow = flow;
e84173dc 5633 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, initial_tci, packet);
abe529af
BP
5634 trace.ctx.resubmit_hook = trace_resubmit;
5635 odp_actions = xlate_actions(&trace.ctx,
5636 rule->up.actions, rule->up.n_actions);
5637
5638 ds_put_char(&result, '\n');
5639 trace_format_flow(&result, 0, "Final flow", &trace);
5640 ds_put_cstr(&result, "Datapath actions: ");
5641 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5642 ofpbuf_delete(odp_actions);
876b0e1c
BP
5643
5644 if (!trace.ctx.may_set_up_flow) {
5645 if (packet) {
5646 ds_put_cstr(&result, "\nThis flow is not cachable.");
5647 } else {
5648 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5649 "for complete actions, please supply a packet.");
5650 }
5651 }
abe529af
BP
5652 }
5653
5654 unixctl_command_reply(conn, 200, ds_cstr(&result));
5655
5656exit:
5657 ds_destroy(&result);
876b0e1c
BP
5658 ofpbuf_delete(packet);
5659 ofpbuf_uninit(&odp_key);
abe529af
BP
5660 free(args);
5661}
5662
7ee20df1
BP
5663static void
5664ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5665 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5666{
5667 clogged = true;
5668 unixctl_command_reply(conn, 200, NULL);
5669}
5670
5671static void
5672ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5673 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5674{
5675 clogged = false;
5676 unixctl_command_reply(conn, 200, NULL);
5677}
5678
abe529af
BP
5679static void
5680ofproto_dpif_unixctl_init(void)
5681{
5682 static bool registered;
5683 if (registered) {
5684 return;
5685 }
5686 registered = true;
5687
7ff2009a
JP
5688 unixctl_command_register("ofproto/trace",
5689 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5690 ofproto_unixctl_trace, NULL);
f0a3aa2e
AA
5691 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5692 NULL);
7ff2009a 5693 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
e29749d5 5694 NULL);
7ff2009a
JP
5695 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5696 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
abe529af
BP
5697}
5698\f
5699const struct ofproto_class ofproto_dpif_class = {
5700 enumerate_types,
5701 enumerate_names,
5702 del,
5703 alloc,
5704 construct,
5705 destruct,
5706 dealloc,
5707 run,
5708 wait,
5709 flush,
6c1491fb
BP
5710 get_features,
5711 get_tables,
abe529af
BP
5712 port_alloc,
5713 port_construct,
5714 port_destruct,
5715 port_dealloc,
5716 port_modified,
5717 port_reconfigured,
5718 port_query_by_name,
5719 port_add,
5720 port_del,
5721 port_dump_start,
5722 port_dump_next,
5723 port_dump_done,
5724 port_poll,
5725 port_poll_wait,
5726 port_is_lacp_current,
0ab6decf 5727 NULL, /* rule_choose_table */
abe529af
BP
5728 rule_alloc,
5729 rule_construct,
5730 rule_destruct,
5731 rule_dealloc,
abe529af
BP
5732 rule_get_stats,
5733 rule_execute,
5734 rule_modify_actions,
7257b535 5735 set_frag_handling,
abe529af
BP
5736 packet_out,
5737 set_netflow,
5738 get_netflow_ids,
5739 set_sflow,
5740 set_cfm,
a5610457 5741 get_cfm_fault,
1de11730 5742 get_cfm_remote_mpids,
21f7563c
JP
5743 set_stp,
5744 get_stp_status,
5745 set_stp_port,
5746 get_stp_port_status,
8b36f51e 5747 set_queues,
abe529af
BP
5748 bundle_set,
5749 bundle_remove,
5750 mirror_set,
5751 set_flood_vlans,
5752 is_mirror_output_bundle,
8402c74b 5753 forward_bpdu_changed,
abe529af 5754};