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