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