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