]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif.c
meta-flow: New library for working with fields by id.
[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"
35#include "mac-learning.h"
36#include "multipath.h"
37#include "netdev.h"
38#include "netlink.h"
39#include "nx-match.h"
40#include "odp-util.h"
41#include "ofp-util.h"
42#include "ofpbuf.h"
43#include "ofp-print.h"
bae473fe 44#include "ofproto-dpif-sflow.h"
abe529af
BP
45#include "poll-loop.h"
46#include "timer.h"
6c1491fb 47#include "unaligned.h"
abe529af
BP
48#include "unixctl.h"
49#include "vlan-bitmap.h"
50#include "vlog.h"
51
52VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
53
54COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
55COVERAGE_DEFINE(ofproto_dpif_expired);
56COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
57COVERAGE_DEFINE(ofproto_dpif_xlate);
58COVERAGE_DEFINE(facet_changed_rule);
59COVERAGE_DEFINE(facet_invalidated);
60COVERAGE_DEFINE(facet_revalidate);
61COVERAGE_DEFINE(facet_unexpected);
62
29901626 63/* Maximum depth of flow table recursion (due to resubmit actions) in a
abe529af
BP
64 * flow translation. */
65#define MAX_RESUBMIT_RECURSION 16
66
67struct ofport_dpif;
68struct ofproto_dpif;
69
70struct rule_dpif {
71 struct rule up;
72
73 long long int used; /* Time last used; time created if not used. */
74
75 /* These statistics:
76 *
77 * - Do include packets and bytes from facets that have been deleted or
78 * whose own statistics have been folded into the rule.
79 *
80 * - Do include packets and bytes sent "by hand" that were accounted to
81 * the rule without any facet being involved (this is a rare corner
82 * case in rule_execute()).
83 *
84 * - Do not include packet or bytes that can be obtained from any facet's
85 * packet_count or byte_count member or that can be obtained from the
86 * datapath by, e.g., dpif_flow_get() for any facet.
87 */
88 uint64_t packet_count; /* Number of packets received. */
89 uint64_t byte_count; /* Number of bytes received. */
90
91 struct list facets; /* List of "struct facet"s. */
92};
93
94static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
95{
96 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
97}
98
29901626
BP
99static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
100 const struct flow *, uint8_t table);
abe529af
BP
101
102#define MAX_MIRRORS 32
103typedef uint32_t mirror_mask_t;
104#define MIRROR_MASK_C(X) UINT32_C(X)
105BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
106struct ofmirror {
107 struct ofproto_dpif *ofproto; /* Owning ofproto. */
108 size_t idx; /* In ofproto's "mirrors" array. */
109 void *aux; /* Key supplied by ofproto's client. */
110 char *name; /* Identifier for log messages. */
111
112 /* Selection criteria. */
113 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
114 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
115 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
116
117 /* Output (mutually exclusive). */
118 struct ofbundle *out; /* Output port or NULL. */
119 int out_vlan; /* Output VLAN or -1. */
120};
121
122static void mirror_destroy(struct ofmirror *);
123
124/* A group of one or more OpenFlow ports. */
125#define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
126struct ofbundle {
127 struct ofproto_dpif *ofproto; /* Owning ofproto. */
128 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
129 void *aux; /* Key supplied by ofproto's client. */
130 char *name; /* Identifier for log messages. */
131
132 /* Configuration. */
133 struct list ports; /* Contains "struct ofport"s. */
134 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
135 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
136 * NULL if all VLANs are trunked. */
137 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
138 struct bond *bond; /* Nonnull iff more than one port. */
139
140 /* Status. */
141 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
142
143 /* Port mirroring info. */
144 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
145 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
146 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
147};
148
149static void bundle_remove(struct ofport *);
150static void bundle_destroy(struct ofbundle *);
151static void bundle_del_port(struct ofport_dpif *);
152static void bundle_run(struct ofbundle *);
153static void bundle_wait(struct ofbundle *);
154
155struct action_xlate_ctx {
156/* action_xlate_ctx_init() initializes these members. */
157
158 /* The ofproto. */
159 struct ofproto_dpif *ofproto;
160
161 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
162 * this flow when actions change header fields. */
163 struct flow flow;
164
165 /* The packet corresponding to 'flow', or a null pointer if we are
166 * revalidating without a packet to refer to. */
167 const struct ofpbuf *packet;
168
169 /* If nonnull, called just before executing a resubmit action.
170 *
171 * This is normally null so the client has to set it manually after
172 * calling action_xlate_ctx_init(). */
173 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
174
abe529af
BP
175/* xlate_actions() initializes and uses these members. The client might want
176 * to look at them after it returns. */
177
178 struct ofpbuf *odp_actions; /* Datapath actions. */
179 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
180 bool may_set_up_flow; /* True ordinarily; false if the actions must
181 * be reassessed for every packet. */
182 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
183
184/* xlate_actions() initializes and uses these members, but the client has no
185 * reason to look at them. */
186
187 int recurse; /* Recursion level, via xlate_table_action. */
afabef2b 188 uint32_t priority; /* Current flow priority. 0 if none. */
b3e9b2ed
EJ
189 struct flow base_flow; /* Flow at the last commit. */
190 uint32_t base_priority; /* Priority at the last commit. */
29901626 191 uint8_t table_id; /* OpenFlow table ID where flow was found. */
abe529af
BP
192};
193
194static void action_xlate_ctx_init(struct action_xlate_ctx *,
195 struct ofproto_dpif *, const struct flow *,
196 const struct ofpbuf *);
197static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
198 const union ofp_action *in, size_t n_in);
199
200/* An exact-match instantiation of an OpenFlow flow. */
201struct facet {
202 long long int used; /* Time last used; time created if not used. */
203
204 /* These statistics:
205 *
206 * - Do include packets and bytes sent "by hand", e.g. with
207 * dpif_execute().
208 *
209 * - Do include packets and bytes that were obtained from the datapath
907a4c5e 210 * when its statistics were reset (e.g. dpif_flow_put() with
abe529af 211 * DPIF_FP_ZERO_STATS).
abe529af
BP
212 */
213 uint64_t packet_count; /* Number of packets received. */
214 uint64_t byte_count; /* Number of bytes received. */
215
216 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
217 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
218
219 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
220 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
221 long long int rs_used; /* Used time pushed to resubmit children. */
222
907a4c5e 223 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
abe529af
BP
224
225 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
226 struct list list_node; /* In owning rule's 'facets' list. */
227 struct rule_dpif *rule; /* Owning rule. */
228 struct flow flow; /* Exact-match flow. */
229 bool installed; /* Installed in datapath? */
230 bool may_install; /* True ordinarily; false if actions must
231 * be reassessed for every packet. */
232 size_t actions_len; /* Number of bytes in actions[]. */
233 struct nlattr *actions; /* Datapath actions. */
234 tag_type tags; /* Tags. */
235 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
236};
237
238static struct facet *facet_create(struct rule_dpif *, const struct flow *,
239 const struct ofpbuf *packet);
240static void facet_remove(struct ofproto_dpif *, struct facet *);
241static void facet_free(struct facet *);
242
243static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
244static struct facet *facet_lookup_valid(struct ofproto_dpif *,
245 const struct flow *);
246static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
247
248static void facet_execute(struct ofproto_dpif *, struct facet *,
249 struct ofpbuf *packet);
250
251static int facet_put__(struct ofproto_dpif *, struct facet *,
252 const struct nlattr *actions, size_t actions_len,
253 struct dpif_flow_stats *);
254static void facet_install(struct ofproto_dpif *, struct facet *,
255 bool zero_stats);
256static void facet_uninstall(struct ofproto_dpif *, struct facet *);
257static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
258
259static void facet_make_actions(struct ofproto_dpif *, struct facet *,
260 const struct ofpbuf *packet);
261static void facet_update_time(struct ofproto_dpif *, struct facet *,
262 long long int used);
263static void facet_update_stats(struct ofproto_dpif *, struct facet *,
264 const struct dpif_flow_stats *);
bbb5d219 265static void facet_reset_counters(struct facet *);
3a88e544 266static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
abe529af 267static void facet_push_stats(struct facet *);
55af77bb 268static void facet_account(struct ofproto_dpif *, struct facet *);
abe529af
BP
269
270static bool facet_is_controller_flow(struct facet *);
271
272static void flow_push_stats(const struct rule_dpif *,
273 struct flow *, uint64_t packets, uint64_t bytes,
274 long long int used);
275
276struct ofport_dpif {
277 struct ofport up;
278
279 uint32_t odp_port;
280 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
281 struct list bundle_node; /* In struct ofbundle's "ports" list. */
282 struct cfm *cfm; /* Connectivity Fault Management, if any. */
283 tag_type tag; /* Tag associated with this port. */
00794817 284 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
015e08bc 285 bool may_enable; /* May be enabled in bonds. */
abe529af
BP
286};
287
288static struct ofport_dpif *
289ofport_dpif_cast(const struct ofport *ofport)
290{
291 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
292 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
293}
294
295static void port_run(struct ofport_dpif *);
296static void port_wait(struct ofport_dpif *);
a5610457 297static int set_cfm(struct ofport *, const struct cfm_settings *);
abe529af 298
7ee20df1
BP
299struct dpif_completion {
300 struct list list_node;
301 struct ofoperation *op;
302};
303
abe529af
BP
304struct ofproto_dpif {
305 struct ofproto up;
306 struct dpif *dpif;
307 int max_ports;
308
6c1491fb
BP
309 /* Statistics. */
310 uint64_t n_matches;
311
abe529af
BP
312 /* Bridging. */
313 struct netflow *netflow;
bae473fe 314 struct dpif_sflow *sflow;
abe529af
BP
315 struct hmap bundles; /* Contains "struct ofbundle"s. */
316 struct mac_learning *ml;
317 struct ofmirror *mirrors[MAX_MIRRORS];
318 bool has_bonded_bundles;
319
320 /* Expiration. */
321 struct timer next_expiration;
322
323 /* Facets. */
324 struct hmap facets;
325 bool need_revalidate;
326 struct tag_set revalidate_set;
7ee20df1
BP
327
328 /* Support for debugging async flow mods. */
329 struct list completions;
daff3353
EJ
330
331 bool has_bundle_action; /* True when the first bundle action appears. */
abe529af
BP
332};
333
7ee20df1
BP
334/* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
335 * for debugging the asynchronous flow_mod implementation.) */
336static bool clogged;
337
abe529af
BP
338static void ofproto_dpif_unixctl_init(void);
339
340static struct ofproto_dpif *
341ofproto_dpif_cast(const struct ofproto *ofproto)
342{
343 assert(ofproto->ofproto_class == &ofproto_dpif_class);
344 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
345}
346
347static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
348 uint16_t ofp_port);
349static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
350 uint32_t odp_port);
351
352/* Packet processing. */
353static void update_learning_table(struct ofproto_dpif *,
354 const struct flow *, int vlan,
355 struct ofbundle *);
356static bool is_admissible(struct ofproto_dpif *, const struct flow *,
357 bool have_packet, tag_type *, int *vlanp,
358 struct ofbundle **in_bundlep);
359static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
360
361/* Flow expiration. */
362static int expire(struct ofproto_dpif *);
363
364/* Utilities. */
b2fda3ef 365static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
abe529af
BP
366 const struct ofpbuf *packet);
367
368/* Global variables. */
369static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
370\f
371/* Factory functions. */
372
373static void
374enumerate_types(struct sset *types)
375{
376 dp_enumerate_types(types);
377}
378
379static int
380enumerate_names(const char *type, struct sset *names)
381{
382 return dp_enumerate_names(type, names);
383}
384
385static int
386del(const char *type, const char *name)
387{
388 struct dpif *dpif;
389 int error;
390
391 error = dpif_open(name, type, &dpif);
392 if (!error) {
393 error = dpif_delete(dpif);
394 dpif_close(dpif);
395 }
396 return error;
397}
398\f
399/* Basic life-cycle. */
400
401static struct ofproto *
402alloc(void)
403{
404 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
405 return &ofproto->up;
406}
407
408static void
409dealloc(struct ofproto *ofproto_)
410{
411 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
412 free(ofproto);
413}
414
415static int
073e2a6f 416construct(struct ofproto *ofproto_, int *n_tablesp)
abe529af
BP
417{
418 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
419 const char *name = ofproto->up.name;
420 int error;
421 int i;
422
423 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
424 if (error) {
425 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
426 return error;
427 }
428
429 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
6c1491fb 430 ofproto->n_matches = 0;
abe529af
BP
431
432 error = dpif_recv_set_mask(ofproto->dpif,
433 ((1u << DPIF_UC_MISS) |
434 (1u << DPIF_UC_ACTION) |
435 (1u << DPIF_UC_SAMPLE)));
436 if (error) {
437 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
438 dpif_close(ofproto->dpif);
439 return error;
440 }
441 dpif_flow_flush(ofproto->dpif);
442 dpif_recv_purge(ofproto->dpif);
443
444 ofproto->netflow = NULL;
445 ofproto->sflow = NULL;
446 hmap_init(&ofproto->bundles);
447 ofproto->ml = mac_learning_create();
448 for (i = 0; i < MAX_MIRRORS; i++) {
449 ofproto->mirrors[i] = NULL;
450 }
451 ofproto->has_bonded_bundles = false;
452
453 timer_set_duration(&ofproto->next_expiration, 1000);
454
455 hmap_init(&ofproto->facets);
456 ofproto->need_revalidate = false;
457 tag_set_init(&ofproto->revalidate_set);
458
7ee20df1
BP
459 list_init(&ofproto->completions);
460
abe529af
BP
461 ofproto_dpif_unixctl_init();
462
daff3353
EJ
463 ofproto->has_bundle_action = false;
464
0697b5c3 465 *n_tablesp = 255;
abe529af
BP
466 return 0;
467}
468
7ee20df1
BP
469static void
470complete_operations(struct ofproto_dpif *ofproto)
471{
472 struct dpif_completion *c, *next;
473
474 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
475 ofoperation_complete(c->op, 0);
476 list_remove(&c->list_node);
477 free(c);
478 }
479}
480
abe529af
BP
481static void
482destruct(struct ofproto *ofproto_)
483{
484 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7ee20df1 485 struct rule_dpif *rule, *next_rule;
0697b5c3 486 struct classifier *table;
abe529af
BP
487 int i;
488
7ee20df1
BP
489 complete_operations(ofproto);
490
0697b5c3
BP
491 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
492 struct cls_cursor cursor;
493
494 cls_cursor_init(&cursor, table, NULL);
495 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
496 ofproto_rule_destroy(&rule->up);
497 }
7ee20df1
BP
498 }
499
abe529af
BP
500 for (i = 0; i < MAX_MIRRORS; i++) {
501 mirror_destroy(ofproto->mirrors[i]);
502 }
503
504 netflow_destroy(ofproto->netflow);
bae473fe 505 dpif_sflow_destroy(ofproto->sflow);
abe529af
BP
506 hmap_destroy(&ofproto->bundles);
507 mac_learning_destroy(ofproto->ml);
508
509 hmap_destroy(&ofproto->facets);
510
511 dpif_close(ofproto->dpif);
512}
513
514static int
515run(struct ofproto *ofproto_)
516{
517 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
518 struct ofport_dpif *ofport;
519 struct ofbundle *bundle;
520 int i;
521
7ee20df1
BP
522 if (!clogged) {
523 complete_operations(ofproto);
524 }
abe529af
BP
525 dpif_run(ofproto->dpif);
526
527 for (i = 0; i < 50; i++) {
528 struct dpif_upcall packet;
529 int error;
530
531 error = dpif_recv(ofproto->dpif, &packet);
532 if (error) {
533 if (error == ENODEV) {
534 /* Datapath destroyed. */
535 return error;
536 }
537 break;
538 }
539
540 handle_upcall(ofproto, &packet);
541 }
542
543 if (timer_expired(&ofproto->next_expiration)) {
544 int delay = expire(ofproto);
545 timer_set_duration(&ofproto->next_expiration, delay);
546 }
547
548 if (ofproto->netflow) {
549 netflow_run(ofproto->netflow);
550 }
551 if (ofproto->sflow) {
bae473fe 552 dpif_sflow_run(ofproto->sflow);
abe529af
BP
553 }
554
555 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
556 port_run(ofport);
557 }
558 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
559 bundle_run(bundle);
560 }
561
1c313b88
BP
562 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
563
abe529af
BP
564 /* Now revalidate if there's anything to do. */
565 if (ofproto->need_revalidate
566 || !tag_set_is_empty(&ofproto->revalidate_set)) {
567 struct tag_set revalidate_set = ofproto->revalidate_set;
568 bool revalidate_all = ofproto->need_revalidate;
569 struct facet *facet, *next;
570
571 /* Clear the revalidation flags. */
572 tag_set_init(&ofproto->revalidate_set);
573 ofproto->need_revalidate = false;
574
575 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
576 if (revalidate_all
577 || tag_set_intersects(&revalidate_set, facet->tags)) {
578 facet_revalidate(ofproto, facet);
579 }
580 }
581 }
582
583 return 0;
584}
585
586static void
587wait(struct ofproto *ofproto_)
588{
589 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
590 struct ofport_dpif *ofport;
591 struct ofbundle *bundle;
592
7ee20df1
BP
593 if (!clogged && !list_is_empty(&ofproto->completions)) {
594 poll_immediate_wake();
595 }
596
abe529af
BP
597 dpif_wait(ofproto->dpif);
598 dpif_recv_wait(ofproto->dpif);
599 if (ofproto->sflow) {
bae473fe 600 dpif_sflow_wait(ofproto->sflow);
abe529af
BP
601 }
602 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
603 poll_immediate_wake();
604 }
605 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
606 port_wait(ofport);
607 }
608 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
609 bundle_wait(bundle);
610 }
1c313b88 611 mac_learning_wait(ofproto->ml);
abe529af
BP
612 if (ofproto->need_revalidate) {
613 /* Shouldn't happen, but if it does just go around again. */
614 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
615 poll_immediate_wake();
616 } else {
617 timer_wait(&ofproto->next_expiration);
618 }
619}
620
621static void
622flush(struct ofproto *ofproto_)
623{
624 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
625 struct facet *facet, *next_facet;
626
627 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
628 /* Mark the facet as not installed so that facet_remove() doesn't
629 * bother trying to uninstall it. There is no point in uninstalling it
630 * individually since we are about to blow away all the facets with
631 * dpif_flow_flush(). */
632 facet->installed = false;
633 facet->dp_packet_count = 0;
634 facet->dp_byte_count = 0;
635 facet_remove(ofproto, facet);
636 }
637 dpif_flow_flush(ofproto->dpif);
638}
639
6c1491fb
BP
640static void
641get_features(struct ofproto *ofproto_ OVS_UNUSED,
642 bool *arp_match_ip, uint32_t *actions)
643{
644 *arp_match_ip = true;
645 *actions = ((1u << OFPAT_OUTPUT) |
646 (1u << OFPAT_SET_VLAN_VID) |
647 (1u << OFPAT_SET_VLAN_PCP) |
648 (1u << OFPAT_STRIP_VLAN) |
649 (1u << OFPAT_SET_DL_SRC) |
650 (1u << OFPAT_SET_DL_DST) |
651 (1u << OFPAT_SET_NW_SRC) |
652 (1u << OFPAT_SET_NW_DST) |
653 (1u << OFPAT_SET_NW_TOS) |
654 (1u << OFPAT_SET_TP_SRC) |
655 (1u << OFPAT_SET_TP_DST) |
656 (1u << OFPAT_ENQUEUE));
657}
658
659static void
660get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
661{
662 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
df2c07f4 663 struct ovs_dp_stats s;
6c1491fb
BP
664
665 strcpy(ots->name, "classifier");
666
667 dpif_get_dp_stats(ofproto->dpif, &s);
668 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
669 put_32aligned_be64(&ots->matched_count,
670 htonll(s.n_hit + ofproto->n_matches));
671}
672
abe529af
BP
673static int
674set_netflow(struct ofproto *ofproto_,
675 const struct netflow_options *netflow_options)
676{
677 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
678
679 if (netflow_options) {
680 if (!ofproto->netflow) {
681 ofproto->netflow = netflow_create();
682 }
683 return netflow_set_options(ofproto->netflow, netflow_options);
684 } else {
685 netflow_destroy(ofproto->netflow);
686 ofproto->netflow = NULL;
687 return 0;
688 }
689}
690
691static struct ofport *
692port_alloc(void)
693{
694 struct ofport_dpif *port = xmalloc(sizeof *port);
695 return &port->up;
696}
697
698static void
699port_dealloc(struct ofport *port_)
700{
701 struct ofport_dpif *port = ofport_dpif_cast(port_);
702 free(port);
703}
704
705static int
706port_construct(struct ofport *port_)
707{
708 struct ofport_dpif *port = ofport_dpif_cast(port_);
709 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
710
711 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
712 port->bundle = NULL;
713 port->cfm = NULL;
714 port->tag = tag_create_random();
d5ffa7f2 715 port->may_enable = true;
abe529af
BP
716
717 if (ofproto->sflow) {
bae473fe
JP
718 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
719 netdev_get_name(port->up.netdev));
abe529af
BP
720 }
721
722 return 0;
723}
724
725static void
726port_destruct(struct ofport *port_)
727{
728 struct ofport_dpif *port = ofport_dpif_cast(port_);
729 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
730
731 bundle_remove(port_);
a5610457 732 set_cfm(port_, NULL);
abe529af 733 if (ofproto->sflow) {
bae473fe 734 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
abe529af
BP
735 }
736}
737
738static void
739port_modified(struct ofport *port_)
740{
741 struct ofport_dpif *port = ofport_dpif_cast(port_);
742
743 if (port->bundle && port->bundle->bond) {
744 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
745 }
746}
747
748static void
749port_reconfigured(struct ofport *port_, ovs_be32 old_config)
750{
751 struct ofport_dpif *port = ofport_dpif_cast(port_);
752 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
753 ovs_be32 changed = old_config ^ port->up.opp.config;
754
755 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
756 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
757 ofproto->need_revalidate = true;
758 }
759}
760
761static int
762set_sflow(struct ofproto *ofproto_,
763 const struct ofproto_sflow_options *sflow_options)
764{
765 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bae473fe 766 struct dpif_sflow *ds = ofproto->sflow;
abe529af 767 if (sflow_options) {
bae473fe 768 if (!ds) {
abe529af
BP
769 struct ofport_dpif *ofport;
770
bae473fe 771 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
abe529af 772 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
bae473fe
JP
773 dpif_sflow_add_port(ds, ofport->odp_port,
774 netdev_get_name(ofport->up.netdev));
abe529af
BP
775 }
776 }
bae473fe 777 dpif_sflow_set_options(ds, sflow_options);
abe529af 778 } else {
bae473fe 779 dpif_sflow_destroy(ds);
abe529af
BP
780 ofproto->sflow = NULL;
781 }
782 return 0;
783}
784
785static int
a5610457 786set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
abe529af
BP
787{
788 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
789 int error;
790
a5610457 791 if (!s) {
abe529af
BP
792 error = 0;
793 } else {
794 if (!ofport->cfm) {
6f629657 795 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
abe529af
BP
796 }
797
a5610457 798 if (cfm_configure(ofport->cfm, s)) {
abe529af
BP
799 return 0;
800 }
801
802 error = EINVAL;
803 }
804 cfm_destroy(ofport->cfm);
805 ofport->cfm = NULL;
806 return error;
807}
808
809static int
a5610457 810get_cfm_fault(const struct ofport *ofport_)
abe529af
BP
811{
812 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
a5610457
EJ
813
814 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
abe529af 815}
1de11730
EJ
816
817static int
818get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
819 size_t *n_rmps)
820{
821 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
822
823 if (ofport->cfm) {
824 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
825 return 0;
826 } else {
827 return -1;
828 }
829}
abe529af
BP
830\f
831/* Bundles. */
832
833/* Expires all MAC learning entries associated with 'port' and forces ofproto
834 * to revalidate every flow. */
835static void
836bundle_flush_macs(struct ofbundle *bundle)
837{
838 struct ofproto_dpif *ofproto = bundle->ofproto;
839 struct mac_learning *ml = ofproto->ml;
840 struct mac_entry *mac, *next_mac;
841
842 ofproto->need_revalidate = true;
843 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
844 if (mac->port.p == bundle) {
845 mac_learning_expire(ml, mac);
846 }
847 }
848}
849
850static struct ofbundle *
851bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
852{
853 struct ofbundle *bundle;
854
855 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
856 &ofproto->bundles) {
857 if (bundle->aux == aux) {
858 return bundle;
859 }
860 }
861 return NULL;
862}
863
864/* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
865 * ones that are found to 'bundles'. */
866static void
867bundle_lookup_multiple(struct ofproto_dpif *ofproto,
868 void **auxes, size_t n_auxes,
869 struct hmapx *bundles)
870{
871 size_t i;
872
873 hmapx_init(bundles);
874 for (i = 0; i < n_auxes; i++) {
875 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
876 if (bundle) {
877 hmapx_add(bundles, bundle);
878 }
879 }
880}
881
882static void
883bundle_del_port(struct ofport_dpif *port)
884{
885 struct ofbundle *bundle = port->bundle;
886
6f77f4ae
BP
887 bundle->ofproto->need_revalidate = true;
888
abe529af
BP
889 list_remove(&port->bundle_node);
890 port->bundle = NULL;
891
892 if (bundle->lacp) {
893 lacp_slave_unregister(bundle->lacp, port);
894 }
895 if (bundle->bond) {
896 bond_slave_unregister(bundle->bond, port);
897 }
898
899 bundle->floodable = true;
900 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
901 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
902 bundle->floodable = false;
903 }
904 }
905}
906
907static bool
908bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
00794817
BP
909 struct lacp_slave_settings *lacp,
910 uint32_t bond_stable_id)
abe529af
BP
911{
912 struct ofport_dpif *port;
913
914 port = get_ofp_port(bundle->ofproto, ofp_port);
915 if (!port) {
916 return false;
917 }
918
919 if (port->bundle != bundle) {
6f77f4ae 920 bundle->ofproto->need_revalidate = true;
abe529af
BP
921 if (port->bundle) {
922 bundle_del_port(port);
923 }
924
925 port->bundle = bundle;
926 list_push_back(&bundle->ports, &port->bundle_node);
927 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
928 bundle->floodable = false;
929 }
930 }
931 if (lacp) {
932 lacp_slave_register(bundle->lacp, port, lacp);
933 }
934
00794817
BP
935 port->bond_stable_id = bond_stable_id;
936
abe529af
BP
937 return true;
938}
939
940static void
941bundle_destroy(struct ofbundle *bundle)
942{
943 struct ofproto_dpif *ofproto;
944 struct ofport_dpif *port, *next_port;
945 int i;
946
947 if (!bundle) {
948 return;
949 }
950
951 ofproto = bundle->ofproto;
952 for (i = 0; i < MAX_MIRRORS; i++) {
953 struct ofmirror *m = ofproto->mirrors[i];
954 if (m) {
955 if (m->out == bundle) {
956 mirror_destroy(m);
957 } else if (hmapx_find_and_delete(&m->srcs, bundle)
958 || hmapx_find_and_delete(&m->dsts, bundle)) {
959 ofproto->need_revalidate = true;
960 }
961 }
962 }
963
964 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
965 bundle_del_port(port);
966 }
967
968 bundle_flush_macs(bundle);
969 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
970 free(bundle->name);
971 free(bundle->trunks);
972 lacp_destroy(bundle->lacp);
973 bond_destroy(bundle->bond);
974 free(bundle);
975}
976
977static int
978bundle_set(struct ofproto *ofproto_, void *aux,
979 const struct ofproto_bundle_settings *s)
980{
981 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
982 bool need_flush = false;
983 const unsigned long *trunks;
984 struct ofport_dpif *port;
985 struct ofbundle *bundle;
986 size_t i;
987 bool ok;
988
989 if (!s) {
990 bundle_destroy(bundle_lookup(ofproto, aux));
991 return 0;
992 }
993
994 assert(s->n_slaves == 1 || s->bond != NULL);
995 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
996
997 bundle = bundle_lookup(ofproto, aux);
998 if (!bundle) {
999 bundle = xmalloc(sizeof *bundle);
1000
1001 bundle->ofproto = ofproto;
1002 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1003 hash_pointer(aux, 0));
1004 bundle->aux = aux;
1005 bundle->name = NULL;
1006
1007 list_init(&bundle->ports);
1008 bundle->vlan = -1;
1009 bundle->trunks = NULL;
1010 bundle->lacp = NULL;
1011 bundle->bond = NULL;
1012
1013 bundle->floodable = true;
1014
1015 bundle->src_mirrors = 0;
1016 bundle->dst_mirrors = 0;
1017 bundle->mirror_out = 0;
1018 }
1019
1020 if (!bundle->name || strcmp(s->name, bundle->name)) {
1021 free(bundle->name);
1022 bundle->name = xstrdup(s->name);
1023 }
1024
1025 /* LACP. */
1026 if (s->lacp) {
1027 if (!bundle->lacp) {
1028 bundle->lacp = lacp_create();
1029 }
1030 lacp_configure(bundle->lacp, s->lacp);
1031 } else {
1032 lacp_destroy(bundle->lacp);
1033 bundle->lacp = NULL;
1034 }
1035
1036 /* Update set of ports. */
1037 ok = true;
1038 for (i = 0; i < s->n_slaves; i++) {
1039 if (!bundle_add_port(bundle, s->slaves[i],
00794817
BP
1040 s->lacp ? &s->lacp_slaves[i] : NULL,
1041 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
abe529af
BP
1042 ok = false;
1043 }
1044 }
1045 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1046 struct ofport_dpif *next_port;
1047
1048 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1049 for (i = 0; i < s->n_slaves; i++) {
56c769ab 1050 if (s->slaves[i] == port->up.ofp_port) {
abe529af
BP
1051 goto found;
1052 }
1053 }
1054
1055 bundle_del_port(port);
1056 found: ;
1057 }
1058 }
1059 assert(list_size(&bundle->ports) <= s->n_slaves);
1060
1061 if (list_is_empty(&bundle->ports)) {
1062 bundle_destroy(bundle);
1063 return EINVAL;
1064 }
1065
1066 /* Set VLAN tag. */
1067 if (s->vlan != bundle->vlan) {
1068 bundle->vlan = s->vlan;
1069 need_flush = true;
1070 }
1071
1072 /* Get trunked VLANs. */
1073 trunks = s->vlan == -1 ? NULL : s->trunks;
1074 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1075 free(bundle->trunks);
1076 bundle->trunks = vlan_bitmap_clone(trunks);
1077 need_flush = true;
1078 }
1079
1080 /* Bonding. */
1081 if (!list_is_short(&bundle->ports)) {
1082 bundle->ofproto->has_bonded_bundles = true;
1083 if (bundle->bond) {
1084 if (bond_reconfigure(bundle->bond, s->bond)) {
1085 ofproto->need_revalidate = true;
1086 }
1087 } else {
1088 bundle->bond = bond_create(s->bond);
6f77f4ae 1089 ofproto->need_revalidate = true;
abe529af
BP
1090 }
1091
1092 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
00794817 1093 bond_slave_register(bundle->bond, port, port->bond_stable_id,
abe529af
BP
1094 port->up.netdev);
1095 }
1096 } else {
1097 bond_destroy(bundle->bond);
1098 bundle->bond = NULL;
1099 }
1100
1101 /* If we changed something that would affect MAC learning, un-learn
1102 * everything on this port and force flow revalidation. */
1103 if (need_flush) {
1104 bundle_flush_macs(bundle);
1105 }
1106
1107 return 0;
1108}
1109
1110static void
1111bundle_remove(struct ofport *port_)
1112{
1113 struct ofport_dpif *port = ofport_dpif_cast(port_);
1114 struct ofbundle *bundle = port->bundle;
1115
1116 if (bundle) {
1117 bundle_del_port(port);
1118 if (list_is_empty(&bundle->ports)) {
1119 bundle_destroy(bundle);
1120 } else if (list_is_short(&bundle->ports)) {
1121 bond_destroy(bundle->bond);
1122 bundle->bond = NULL;
1123 }
1124 }
1125}
1126
1127static void
5f877369 1128send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
abe529af
BP
1129{
1130 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1131 struct ofport_dpif *port = port_;
1132 uint8_t ea[ETH_ADDR_LEN];
1133 int error;
1134
1135 error = netdev_get_etheraddr(port->up.netdev, ea);
1136 if (!error) {
abe529af 1137 struct ofpbuf packet;
5f877369 1138 void *packet_pdu;
abe529af
BP
1139
1140 ofpbuf_init(&packet, 0);
1141 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
5f877369
EJ
1142 pdu_size);
1143 memcpy(packet_pdu, pdu, pdu_size);
1144
abe529af
BP
1145 error = netdev_send(port->up.netdev, &packet);
1146 if (error) {
1147 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1148 "(%s)", port->bundle->name,
1149 netdev_get_name(port->up.netdev), strerror(error));
1150 }
1151 ofpbuf_uninit(&packet);
1152 } else {
1153 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1154 "%s (%s)", port->bundle->name,
1155 netdev_get_name(port->up.netdev), strerror(error));
1156 }
1157}
1158
1159static void
1160bundle_send_learning_packets(struct ofbundle *bundle)
1161{
1162 struct ofproto_dpif *ofproto = bundle->ofproto;
1163 int error, n_packets, n_errors;
1164 struct mac_entry *e;
1165
1166 error = n_packets = n_errors = 0;
1167 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1168 if (e->port.p != bundle) {
1169 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1170 if (ret) {
1171 error = ret;
1172 n_errors++;
1173 }
1174 n_packets++;
1175 }
1176 }
1177
1178 if (n_errors) {
1179 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1180 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1181 "packets, last error was: %s",
1182 bundle->name, n_errors, n_packets, strerror(error));
1183 } else {
1184 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1185 bundle->name, n_packets);
1186 }
1187}
1188
1189static void
1190bundle_run(struct ofbundle *bundle)
1191{
1192 if (bundle->lacp) {
1193 lacp_run(bundle->lacp, send_pdu_cb);
1194 }
1195 if (bundle->bond) {
1196 struct ofport_dpif *port;
1197
1198 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
015e08bc 1199 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
abe529af
BP
1200 }
1201
1202 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1203 lacp_negotiated(bundle->lacp));
1204 if (bond_should_send_learning_packets(bundle->bond)) {
1205 bundle_send_learning_packets(bundle);
1206 }
1207 }
1208}
1209
1210static void
1211bundle_wait(struct ofbundle *bundle)
1212{
1213 if (bundle->lacp) {
1214 lacp_wait(bundle->lacp);
1215 }
1216 if (bundle->bond) {
1217 bond_wait(bundle->bond);
1218 }
1219}
1220\f
1221/* Mirrors. */
1222
1223static int
1224mirror_scan(struct ofproto_dpif *ofproto)
1225{
1226 int idx;
1227
1228 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1229 if (!ofproto->mirrors[idx]) {
1230 return idx;
1231 }
1232 }
1233 return -1;
1234}
1235
1236static struct ofmirror *
1237mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1238{
1239 int i;
1240
1241 for (i = 0; i < MAX_MIRRORS; i++) {
1242 struct ofmirror *mirror = ofproto->mirrors[i];
1243 if (mirror && mirror->aux == aux) {
1244 return mirror;
1245 }
1246 }
1247
1248 return NULL;
1249}
1250
1251static int
1252mirror_set(struct ofproto *ofproto_, void *aux,
1253 const struct ofproto_mirror_settings *s)
1254{
1255 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1256 mirror_mask_t mirror_bit;
1257 struct ofbundle *bundle;
1258 struct ofmirror *mirror;
1259 struct ofbundle *out;
1260 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1261 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1262 int out_vlan;
1263
1264 mirror = mirror_lookup(ofproto, aux);
1265 if (!s) {
1266 mirror_destroy(mirror);
1267 return 0;
1268 }
1269 if (!mirror) {
1270 int idx;
1271
1272 idx = mirror_scan(ofproto);
1273 if (idx < 0) {
1274 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1275 "cannot create %s",
1276 ofproto->up.name, MAX_MIRRORS, s->name);
1277 return EFBIG;
1278 }
1279
1280 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1281 mirror->ofproto = ofproto;
1282 mirror->idx = idx;
8b28d864 1283 mirror->aux = aux;
abe529af
BP
1284 mirror->out_vlan = -1;
1285 mirror->name = NULL;
1286 }
1287
1288 if (!mirror->name || strcmp(s->name, mirror->name)) {
1289 free(mirror->name);
1290 mirror->name = xstrdup(s->name);
1291 }
1292
1293 /* Get the new configuration. */
1294 if (s->out_bundle) {
1295 out = bundle_lookup(ofproto, s->out_bundle);
1296 if (!out) {
1297 mirror_destroy(mirror);
1298 return EINVAL;
1299 }
1300 out_vlan = -1;
1301 } else {
1302 out = NULL;
1303 out_vlan = s->out_vlan;
1304 }
1305 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1306 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1307
1308 /* If the configuration has not changed, do nothing. */
1309 if (hmapx_equals(&srcs, &mirror->srcs)
1310 && hmapx_equals(&dsts, &mirror->dsts)
1311 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1312 && mirror->out == out
1313 && mirror->out_vlan == out_vlan)
1314 {
1315 hmapx_destroy(&srcs);
1316 hmapx_destroy(&dsts);
1317 return 0;
1318 }
1319
1320 hmapx_swap(&srcs, &mirror->srcs);
1321 hmapx_destroy(&srcs);
1322
1323 hmapx_swap(&dsts, &mirror->dsts);
1324 hmapx_destroy(&dsts);
1325
1326 free(mirror->vlans);
1327 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1328
1329 mirror->out = out;
1330 mirror->out_vlan = out_vlan;
1331
1332 /* Update bundles. */
1333 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1334 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1335 if (hmapx_contains(&mirror->srcs, bundle)) {
1336 bundle->src_mirrors |= mirror_bit;
1337 } else {
1338 bundle->src_mirrors &= ~mirror_bit;
1339 }
1340
1341 if (hmapx_contains(&mirror->dsts, bundle)) {
1342 bundle->dst_mirrors |= mirror_bit;
1343 } else {
1344 bundle->dst_mirrors &= ~mirror_bit;
1345 }
1346
1347 if (mirror->out == bundle) {
1348 bundle->mirror_out |= mirror_bit;
1349 } else {
1350 bundle->mirror_out &= ~mirror_bit;
1351 }
1352 }
1353
1354 ofproto->need_revalidate = true;
1355 mac_learning_flush(ofproto->ml);
1356
1357 return 0;
1358}
1359
1360static void
1361mirror_destroy(struct ofmirror *mirror)
1362{
1363 struct ofproto_dpif *ofproto;
1364 mirror_mask_t mirror_bit;
1365 struct ofbundle *bundle;
1366
1367 if (!mirror) {
1368 return;
1369 }
1370
1371 ofproto = mirror->ofproto;
1372 ofproto->need_revalidate = true;
1373 mac_learning_flush(ofproto->ml);
1374
1375 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1376 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1377 bundle->src_mirrors &= ~mirror_bit;
1378 bundle->dst_mirrors &= ~mirror_bit;
1379 bundle->mirror_out &= ~mirror_bit;
1380 }
1381
1382 hmapx_destroy(&mirror->srcs);
1383 hmapx_destroy(&mirror->dsts);
1384 free(mirror->vlans);
1385
1386 ofproto->mirrors[mirror->idx] = NULL;
1387 free(mirror->name);
1388 free(mirror);
1389}
1390
1391static int
1392set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1393{
1394 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1395 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1396 ofproto->need_revalidate = true;
1397 mac_learning_flush(ofproto->ml);
1398 }
1399 return 0;
1400}
1401
1402static bool
1403is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1404{
1405 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1406 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1407 return bundle && bundle->mirror_out != 0;
1408}
8402c74b
SS
1409
1410static void
b53055f4 1411forward_bpdu_changed(struct ofproto *ofproto_)
8402c74b
SS
1412{
1413 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1414 /* Revalidate cached flows whenever forward_bpdu option changes. */
1415 ofproto->need_revalidate = true;
1416}
abe529af
BP
1417\f
1418/* Ports. */
1419
1420static struct ofport_dpif *
1421get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1422{
7df6a8bd
BP
1423 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1424 return ofport ? ofport_dpif_cast(ofport) : NULL;
abe529af
BP
1425}
1426
1427static struct ofport_dpif *
1428get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1429{
1430 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1431}
1432
1433static void
1434ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1435 struct dpif_port *dpif_port)
1436{
1437 ofproto_port->name = dpif_port->name;
1438 ofproto_port->type = dpif_port->type;
1439 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1440}
1441
1442static void
1443port_run(struct ofport_dpif *ofport)
1444{
015e08bc
EJ
1445 bool enable = netdev_get_carrier(ofport->up.netdev);
1446
abe529af
BP
1447 if (ofport->cfm) {
1448 cfm_run(ofport->cfm);
1449
1450 if (cfm_should_send_ccm(ofport->cfm)) {
1451 struct ofpbuf packet;
abe529af
BP
1452
1453 ofpbuf_init(&packet, 0);
c0a2e71d 1454 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
abe529af 1455 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
b2fda3ef 1456 ofport->odp_port, &packet);
abe529af
BP
1457 ofpbuf_uninit(&packet);
1458 }
015e08bc
EJ
1459
1460 enable = enable && !cfm_get_fault(ofport->cfm);
abe529af 1461 }
015e08bc
EJ
1462
1463 if (ofport->bundle) {
1464 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1465 }
1466
daff3353
EJ
1467 if (ofport->may_enable != enable) {
1468 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1469
1470 if (ofproto->has_bundle_action) {
1471 ofproto->need_revalidate = true;
1472 }
1473 }
1474
015e08bc 1475 ofport->may_enable = enable;
abe529af
BP
1476}
1477
1478static void
1479port_wait(struct ofport_dpif *ofport)
1480{
1481 if (ofport->cfm) {
1482 cfm_wait(ofport->cfm);
1483 }
1484}
1485
1486static int
1487port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1488 struct ofproto_port *ofproto_port)
1489{
1490 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1491 struct dpif_port dpif_port;
1492 int error;
1493
1494 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1495 if (!error) {
1496 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1497 }
1498 return error;
1499}
1500
1501static int
1502port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1503{
1504 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1505 uint16_t odp_port;
1506 int error;
1507
1508 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1509 if (!error) {
1510 *ofp_portp = odp_port_to_ofp_port(odp_port);
1511 }
1512 return error;
1513}
1514
1515static int
1516port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1517{
1518 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1519 int error;
1520
1521 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1522 if (!error) {
1523 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1524 if (ofport) {
1525 /* The caller is going to close ofport->up.netdev. If this is a
1526 * bonded port, then the bond is using that netdev, so remove it
1527 * from the bond. The client will need to reconfigure everything
1528 * after deleting ports, so then the slave will get re-added. */
1529 bundle_remove(&ofport->up);
1530 }
1531 }
1532 return error;
1533}
1534
1535struct port_dump_state {
1536 struct dpif_port_dump dump;
1537 bool done;
1538};
1539
1540static int
1541port_dump_start(const struct ofproto *ofproto_, void **statep)
1542{
1543 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1544 struct port_dump_state *state;
1545
1546 *statep = state = xmalloc(sizeof *state);
1547 dpif_port_dump_start(&state->dump, ofproto->dpif);
1548 state->done = false;
1549 return 0;
1550}
1551
1552static int
1553port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1554 struct ofproto_port *port)
1555{
1556 struct port_dump_state *state = state_;
1557 struct dpif_port dpif_port;
1558
1559 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1560 ofproto_port_from_dpif_port(port, &dpif_port);
1561 return 0;
1562 } else {
1563 int error = dpif_port_dump_done(&state->dump);
1564 state->done = true;
1565 return error ? error : EOF;
1566 }
1567}
1568
1569static int
1570port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1571{
1572 struct port_dump_state *state = state_;
1573
1574 if (!state->done) {
1575 dpif_port_dump_done(&state->dump);
1576 }
1577 free(state);
1578 return 0;
1579}
1580
1581static int
1582port_poll(const struct ofproto *ofproto_, char **devnamep)
1583{
1584 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1585 return dpif_port_poll(ofproto->dpif, devnamep);
1586}
1587
1588static void
1589port_poll_wait(const struct ofproto *ofproto_)
1590{
1591 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1592 dpif_port_poll_wait(ofproto->dpif);
1593}
1594
1595static int
1596port_is_lacp_current(const struct ofport *ofport_)
1597{
1598 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1599 return (ofport->bundle && ofport->bundle->lacp
1600 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1601 : -1);
1602}
1603\f
1604/* Upcall handling. */
1605
1606/* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1607 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1608 * their individual configurations.
1609 *
1610 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1611 * Otherwise, ownership is transferred to this function. */
1612static void
1613send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1614 const struct flow *flow, bool clone)
1615{
1616 struct ofputil_packet_in pin;
1617
1618 pin.packet = upcall->packet;
1619 pin.in_port = flow->in_port;
1620 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1621 pin.buffer_id = 0; /* not yet known */
1622 pin.send_len = upcall->userdata;
78bd1cd0 1623 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
abe529af
BP
1624 clone ? NULL : upcall->packet);
1625}
1626
1627static bool
1628process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1629 const struct ofpbuf *packet)
1630{
b6e001b6
EJ
1631 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1632
1633 if (!ofport) {
1634 return false;
1635 }
1636
ef9819b5 1637 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
b6e001b6 1638 if (packet) {
abe529af
BP
1639 cfm_process_heartbeat(ofport->cfm, packet);
1640 }
1641 return true;
b6e001b6
EJ
1642 } else if (ofport->bundle && ofport->bundle->lacp
1643 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1644 if (packet) {
1645 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
abe529af 1646 }
da37ebac 1647 return true;
abe529af
BP
1648 }
1649 return false;
1650}
1651
1652static void
1653handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1654{
1655 struct facet *facet;
1656 struct flow flow;
1657
1658 /* Obtain in_port and tun_id, at least. */
1659 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1660
1661 /* Set header pointers in 'flow'. */
1662 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1663
1664 /* Handle 802.1ag and LACP. */
1665 if (process_special(ofproto, &flow, upcall->packet)) {
1666 ofpbuf_delete(upcall->packet);
6c1491fb 1667 ofproto->n_matches++;
abe529af
BP
1668 return;
1669 }
1670
1671 /* Check with in-band control to see if this packet should be sent
1672 * to the local port regardless of the flow table. */
1673 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
df2c07f4 1674 send_packet(ofproto, OVSP_LOCAL, upcall->packet);
abe529af
BP
1675 }
1676
1677 facet = facet_lookup_valid(ofproto, &flow);
1678 if (!facet) {
29901626 1679 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow, 0);
abe529af
BP
1680 if (!rule) {
1681 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1682 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1683 if (port) {
1684 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1685 COVERAGE_INC(ofproto_dpif_no_packet_in);
1686 /* XXX install 'drop' flow entry */
1687 ofpbuf_delete(upcall->packet);
1688 return;
1689 }
1690 } else {
1691 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1692 flow.in_port);
1693 }
1694
1695 send_packet_in(ofproto, upcall, &flow, false);
1696 return;
1697 }
1698
1699 facet = facet_create(rule, &flow, upcall->packet);
1700 } else if (!facet->may_install) {
1701 /* The facet is not installable, that is, we need to process every
1702 * packet, so process the current packet's actions into 'facet'. */
1703 facet_make_actions(ofproto, facet, upcall->packet);
1704 }
1705
1706 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1707 /*
1708 * Extra-special case for fail-open mode.
1709 *
1710 * We are in fail-open mode and the packet matched the fail-open rule,
1711 * but we are connected to a controller too. We should send the packet
1712 * up to the controller in the hope that it will try to set up a flow
1713 * and thereby allow us to exit fail-open.
1714 *
1715 * See the top-level comment in fail-open.c for more information.
1716 */
1717 send_packet_in(ofproto, upcall, &flow, true);
1718 }
1719
1720 facet_execute(ofproto, facet, upcall->packet);
1721 facet_install(ofproto, facet, false);
6c1491fb 1722 ofproto->n_matches++;
abe529af
BP
1723}
1724
1725static void
1726handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1727{
1728 struct flow flow;
1729
1730 switch (upcall->type) {
1731 case DPIF_UC_ACTION:
1732 COVERAGE_INC(ofproto_dpif_ctlr_action);
1733 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1734 send_packet_in(ofproto, upcall, &flow, false);
1735 break;
1736
1737 case DPIF_UC_SAMPLE:
1738 if (ofproto->sflow) {
1739 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
bae473fe 1740 dpif_sflow_received(ofproto->sflow, upcall, &flow);
abe529af
BP
1741 }
1742 ofpbuf_delete(upcall->packet);
1743 break;
1744
1745 case DPIF_UC_MISS:
1746 handle_miss_upcall(ofproto, upcall);
1747 break;
1748
1749 case DPIF_N_UC_TYPES:
1750 default:
1751 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1752 break;
1753 }
1754}
1755\f
1756/* Flow expiration. */
1757
1758static int facet_max_idle(const struct ofproto_dpif *);
1759static void update_stats(struct ofproto_dpif *);
1760static void rule_expire(struct rule_dpif *);
1761static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1762
1763/* This function is called periodically by run(). Its job is to collect
1764 * updates for the flows that have been installed into the datapath, most
1765 * importantly when they last were used, and then use that information to
1766 * expire flows that have not been used recently.
1767 *
1768 * Returns the number of milliseconds after which it should be called again. */
1769static int
1770expire(struct ofproto_dpif *ofproto)
1771{
1772 struct rule_dpif *rule, *next_rule;
0697b5c3 1773 struct classifier *table;
abe529af
BP
1774 int dp_max_idle;
1775
1776 /* Update stats for each flow in the datapath. */
1777 update_stats(ofproto);
1778
1779 /* Expire facets that have been idle too long. */
1780 dp_max_idle = facet_max_idle(ofproto);
1781 expire_facets(ofproto, dp_max_idle);
1782
1783 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
0697b5c3
BP
1784 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1785 struct cls_cursor cursor;
1786
1787 cls_cursor_init(&cursor, table, NULL);
1788 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1789 rule_expire(rule);
1790 }
abe529af
BP
1791 }
1792
1793 /* All outstanding data in existing flows has been accounted, so it's a
1794 * good time to do bond rebalancing. */
1795 if (ofproto->has_bonded_bundles) {
1796 struct ofbundle *bundle;
1797
1798 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1799 if (bundle->bond) {
1800 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1801 }
1802 }
1803 }
1804
1805 return MIN(dp_max_idle, 1000);
1806}
1807
1808/* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1809 *
1810 * This function also pushes statistics updates to rules which each facet
1811 * resubmits into. Generally these statistics will be accurate. However, if a
1812 * facet changes the rule it resubmits into at some time in between
1813 * update_stats() runs, it is possible that statistics accrued to the
1814 * old rule will be incorrectly attributed to the new rule. This could be
1815 * avoided by calling update_stats() whenever rules are created or
1816 * deleted. However, the performance impact of making so many calls to the
1817 * datapath do not justify the benefit of having perfectly accurate statistics.
1818 */
1819static void
1820update_stats(struct ofproto_dpif *p)
1821{
1822 const struct dpif_flow_stats *stats;
1823 struct dpif_flow_dump dump;
1824 const struct nlattr *key;
1825 size_t key_len;
1826
1827 dpif_flow_dump_start(&dump, p->dpif);
1828 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1829 struct facet *facet;
1830 struct flow flow;
1831
1832 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1833 struct ds s;
1834
1835 ds_init(&s);
1836 odp_flow_key_format(key, key_len, &s);
df2c07f4 1837 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
abe529af
BP
1838 ds_cstr(&s));
1839 ds_destroy(&s);
1840
1841 continue;
1842 }
1843 facet = facet_find(p, &flow);
1844
1845 if (facet && facet->installed) {
1846
1847 if (stats->n_packets >= facet->dp_packet_count) {
1848 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1849 facet->packet_count += extra;
1850 } else {
1851 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1852 }
1853
1854 if (stats->n_bytes >= facet->dp_byte_count) {
1855 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1856 } else {
1857 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1858 }
1859
1860 facet->dp_packet_count = stats->n_packets;
1861 facet->dp_byte_count = stats->n_bytes;
1862
1863 facet_update_time(p, facet, stats->used);
55af77bb 1864 facet_account(p, facet);
abe529af
BP
1865 facet_push_stats(facet);
1866 } else {
1867 /* There's a flow in the datapath that we know nothing about.
1868 * Delete it. */
1869 COVERAGE_INC(facet_unexpected);
1870 dpif_flow_del(p->dpif, key, key_len, NULL);
1871 }
1872 }
1873 dpif_flow_dump_done(&dump);
1874}
1875
1876/* Calculates and returns the number of milliseconds of idle time after which
1877 * facets should expire from the datapath and we should fold their statistics
1878 * into their parent rules in userspace. */
1879static int
1880facet_max_idle(const struct ofproto_dpif *ofproto)
1881{
1882 /*
1883 * Idle time histogram.
1884 *
1885 * Most of the time a switch has a relatively small number of facets. When
1886 * this is the case we might as well keep statistics for all of them in
1887 * userspace and to cache them in the kernel datapath for performance as
1888 * well.
1889 *
1890 * As the number of facets increases, the memory required to maintain
1891 * statistics about them in userspace and in the kernel becomes
1892 * significant. However, with a large number of facets it is likely that
1893 * only a few of them are "heavy hitters" that consume a large amount of
1894 * bandwidth. At this point, only heavy hitters are worth caching in the
1895 * kernel and maintaining in userspaces; other facets we can discard.
1896 *
1897 * The technique used to compute the idle time is to build a histogram with
1898 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1899 * that is installed in the kernel gets dropped in the appropriate bucket.
1900 * After the histogram has been built, we compute the cutoff so that only
084f5290
SH
1901 * the most-recently-used 1% of facets (but at least
1902 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1903 * the most-recently-used bucket of facets is kept, so actually an
1904 * arbitrary number of facets can be kept in any given expiration run
1905 * (though the next run will delete most of those unless they receive
1906 * additional data).
abe529af
BP
1907 *
1908 * This requires a second pass through the facets, in addition to the pass
1909 * made by update_stats(), because the former function never looks
1910 * at uninstallable facets.
1911 */
1912 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1913 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1914 int buckets[N_BUCKETS] = { 0 };
f11c1ef4 1915 int total, subtotal, bucket;
abe529af 1916 struct facet *facet;
abe529af
BP
1917 long long int now;
1918 int i;
1919
1920 total = hmap_count(&ofproto->facets);
084f5290 1921 if (total <= ofproto->up.flow_eviction_threshold) {
abe529af
BP
1922 return N_BUCKETS * BUCKET_WIDTH;
1923 }
1924
1925 /* Build histogram. */
1926 now = time_msec();
1927 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1928 long long int idle = now - facet->used;
1929 int bucket = (idle <= 0 ? 0
1930 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1931 : (unsigned int) idle / BUCKET_WIDTH);
1932 buckets[bucket]++;
1933 }
1934
1935 /* Find the first bucket whose flows should be expired. */
f11c1ef4
SH
1936 subtotal = bucket = 0;
1937 do {
1938 subtotal += buckets[bucket++];
084f5290
SH
1939 } while (bucket < N_BUCKETS &&
1940 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
abe529af
BP
1941
1942 if (VLOG_IS_DBG_ENABLED()) {
1943 struct ds s;
1944
1945 ds_init(&s);
1946 ds_put_cstr(&s, "keep");
1947 for (i = 0; i < N_BUCKETS; i++) {
1948 if (i == bucket) {
1949 ds_put_cstr(&s, ", drop");
1950 }
1951 if (buckets[i]) {
1952 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1953 }
1954 }
1955 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1956 ds_destroy(&s);
1957 }
1958
1959 return bucket * BUCKET_WIDTH;
1960}
1961
1962static void
1963facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1964{
1965 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1966 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1967 struct ofexpired expired;
1968
1969 if (facet->installed) {
1970 struct dpif_flow_stats stats;
1971
1972 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1973 &stats);
1974 facet_update_stats(ofproto, facet, &stats);
1975 }
1976
1977 expired.flow = facet->flow;
1978 expired.packet_count = facet->packet_count;
1979 expired.byte_count = facet->byte_count;
1980 expired.used = facet->used;
1981 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1982 }
1983}
1984
1985static void
1986expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1987{
1988 long long int cutoff = time_msec() - dp_max_idle;
1989 struct facet *facet, *next_facet;
1990
1991 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1992 facet_active_timeout(ofproto, facet);
1993 if (facet->used < cutoff) {
1994 facet_remove(ofproto, facet);
1995 }
1996 }
1997}
1998
1999/* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2000 * then delete it entirely. */
2001static void
2002rule_expire(struct rule_dpif *rule)
2003{
2004 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2005 struct facet *facet, *next_facet;
2006 long long int now;
2007 uint8_t reason;
2008
2009 /* Has 'rule' expired? */
2010 now = time_msec();
2011 if (rule->up.hard_timeout
2012 && now > rule->up.created + rule->up.hard_timeout * 1000) {
2013 reason = OFPRR_HARD_TIMEOUT;
2014 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2015 && now > rule->used + rule->up.idle_timeout * 1000) {
2016 reason = OFPRR_IDLE_TIMEOUT;
2017 } else {
2018 return;
2019 }
2020
2021 COVERAGE_INC(ofproto_dpif_expired);
2022
2023 /* Update stats. (This is a no-op if the rule expired due to an idle
2024 * timeout, because that only happens when the rule has no facets left.) */
2025 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2026 facet_remove(ofproto, facet);
2027 }
2028
2029 /* Get rid of the rule. */
2030 ofproto_rule_expire(&rule->up, reason);
2031}
2032\f
2033/* Facets. */
2034
2035/* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2036 * example 'packet' within that flow.
2037 *
2038 * The caller must already have determined that no facet with an identical
2039 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2040 * the ofproto's classifier table. */
2041static struct facet *
2042facet_create(struct rule_dpif *rule, const struct flow *flow,
2043 const struct ofpbuf *packet)
2044{
2045 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2046 struct facet *facet;
2047
2048 facet = xzalloc(sizeof *facet);
2049 facet->used = time_msec();
2050 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2051 list_push_back(&rule->facets, &facet->list_node);
2052 facet->rule = rule;
2053 facet->flow = *flow;
2054 netflow_flow_init(&facet->nf_flow);
2055 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2056
2057 facet_make_actions(ofproto, facet, packet);
2058
2059 return facet;
2060}
2061
2062static void
2063facet_free(struct facet *facet)
2064{
2065 free(facet->actions);
2066 free(facet);
2067}
2068
2069/* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2070 * 'packet', which arrived on 'in_port'.
2071 *
2072 * Takes ownership of 'packet'. */
2073static bool
2074execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2075 const struct nlattr *odp_actions, size_t actions_len,
2076 struct ofpbuf *packet)
2077{
2078 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
df2c07f4 2079 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE) {
abe529af
BP
2080 /* As an optimization, avoid a round-trip from userspace to kernel to
2081 * userspace. This also avoids possibly filling up kernel packet
2082 * buffers along the way. */
2083 struct dpif_upcall upcall;
2084
2085 upcall.type = DPIF_UC_ACTION;
2086 upcall.packet = packet;
2087 upcall.key = NULL;
2088 upcall.key_len = 0;
2089 upcall.userdata = nl_attr_get_u64(odp_actions);
2090 upcall.sample_pool = 0;
2091 upcall.actions = NULL;
2092 upcall.actions_len = 0;
2093
2094 send_packet_in(ofproto, &upcall, flow, false);
2095
2096 return true;
2097 } else {
80e5eed9
BP
2098 struct odputil_keybuf keybuf;
2099 struct ofpbuf key;
abe529af
BP
2100 int error;
2101
80e5eed9
BP
2102 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2103 odp_flow_key_from_flow(&key, flow);
2104
2105 error = dpif_execute(ofproto->dpif, key.data, key.size,
2106 odp_actions, actions_len, packet);
2107
abe529af
BP
2108 ofpbuf_delete(packet);
2109 return !error;
2110 }
2111}
2112
2113/* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2114 * statistics appropriately. 'packet' must have at least sizeof(struct
2115 * ofp_packet_in) bytes of headroom.
2116 *
2117 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2118 * applying flow_extract() to 'packet' would yield the same flow as
2119 * 'facet->flow'.
2120 *
df2c07f4
JP
2121 * 'facet' must have accurately composed datapath actions; that is, it must
2122 * not be in need of revalidation.
abe529af
BP
2123 *
2124 * Takes ownership of 'packet'. */
2125static void
2126facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2127 struct ofpbuf *packet)
2128{
2129 struct dpif_flow_stats stats;
2130
2131 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2132
2133 flow_extract_stats(&facet->flow, packet, &stats);
2134 stats.used = time_msec();
2135 if (execute_odp_actions(ofproto, &facet->flow,
2136 facet->actions, facet->actions_len, packet)) {
2137 facet_update_stats(ofproto, facet, &stats);
2138 }
2139}
2140
2141/* Remove 'facet' from 'ofproto' and free up the associated memory:
2142 *
2143 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2144 * rule's statistics, via facet_uninstall().
2145 *
2146 * - Removes 'facet' from its rule and from ofproto->facets.
2147 */
2148static void
2149facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2150{
2151 facet_uninstall(ofproto, facet);
2152 facet_flush_stats(ofproto, facet);
2153 hmap_remove(&ofproto->facets, &facet->hmap_node);
2154 list_remove(&facet->list_node);
2155 facet_free(facet);
2156}
2157
df2c07f4 2158/* Composes the datapath actions for 'facet' based on its rule's actions. */
abe529af
BP
2159static void
2160facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2161 const struct ofpbuf *packet)
2162{
2163 const struct rule_dpif *rule = facet->rule;
2164 struct ofpbuf *odp_actions;
2165 struct action_xlate_ctx ctx;
2166
2167 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2168 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2169 facet->tags = ctx.tags;
2170 facet->may_install = ctx.may_set_up_flow;
2171 facet->nf_flow.output_iface = ctx.nf_output_iface;
2172
2173 if (facet->actions_len != odp_actions->size
2174 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2175 free(facet->actions);
2176 facet->actions_len = odp_actions->size;
2177 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2178 }
2179
2180 ofpbuf_delete(odp_actions);
2181}
2182
3a88e544
BP
2183/* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2184 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2185 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2186 * since 'facet' was last updated.
2187 *
2188 * Returns 0 if successful, otherwise a positive errno value.*/
abe529af
BP
2189static int
2190facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2191 const struct nlattr *actions, size_t actions_len,
2192 struct dpif_flow_stats *stats)
2193{
2194 struct odputil_keybuf keybuf;
2195 enum dpif_flow_put_flags flags;
2196 struct ofpbuf key;
3a88e544 2197 int ret;
abe529af
BP
2198
2199 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2200 if (stats) {
2201 flags |= DPIF_FP_ZERO_STATS;
abe529af
BP
2202 }
2203
2204 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2205 odp_flow_key_from_flow(&key, &facet->flow);
2206
3a88e544
BP
2207 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2208 actions, actions_len, stats);
2209
2210 if (stats) {
2211 facet_reset_dp_stats(facet, stats);
2212 }
2213
2214 return ret;
abe529af
BP
2215}
2216
2217/* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2218 * 'zero_stats' is true, clears any existing statistics from the datapath for
2219 * 'facet'. */
2220static void
2221facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2222{
2223 struct dpif_flow_stats stats;
2224
2225 if (facet->may_install
2226 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2227 zero_stats ? &stats : NULL)) {
2228 facet->installed = true;
2229 }
2230}
2231
d78be13b
BP
2232static int
2233vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2234{
2235 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2236}
2237
abe529af 2238static void
55af77bb 2239facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
abe529af 2240{
55af77bb 2241 uint64_t n_bytes;
abe529af
BP
2242 struct ofbundle *in_bundle;
2243 const struct nlattr *a;
2244 tag_type dummy = 0;
2245 unsigned int left;
d78be13b 2246 ovs_be16 vlan_tci;
abe529af
BP
2247 int vlan;
2248
55af77bb 2249 if (facet->byte_count <= facet->accounted_bytes) {
abe529af
BP
2250 return;
2251 }
55af77bb
EJ
2252 n_bytes = facet->byte_count - facet->accounted_bytes;
2253 facet->accounted_bytes = facet->byte_count;
abe529af
BP
2254
2255 /* Test that 'tags' is nonzero to ensure that only flows that include an
2256 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2257 * This works because OFPP_NORMAL always sets a nonzero tag value.
2258 *
2259 * Feed information from the active flows back into the learning table to
2260 * ensure that table is always in sync with what is actually flowing
2261 * through the datapath. */
2262 if (!facet->tags
2263 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2264 &vlan, &in_bundle)) {
2265 return;
2266 }
2267
2268 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2269
2270 if (!ofproto->has_bonded_bundles) {
2271 return;
2272 }
d78be13b
BP
2273
2274 /* This loop feeds byte counters to bond_account() for rebalancing to use
2275 * as a basis. We also need to track the actual VLAN on which the packet
2276 * is going to be sent to ensure that it matches the one passed to
2277 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2278 * hash bucket.) */
2279 vlan_tci = facet->flow.vlan_tci;
abe529af 2280 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
d78be13b 2281 struct ofport_dpif *port;
abe529af 2282
d78be13b 2283 switch (nl_attr_type(a)) {
df2c07f4 2284 case OVS_ACTION_ATTR_OUTPUT:
abe529af
BP
2285 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2286 if (port && port->bundle && port->bundle->bond) {
d78be13b
BP
2287 bond_account(port->bundle->bond, &facet->flow,
2288 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
abe529af 2289 }
d78be13b
BP
2290 break;
2291
d9065a90 2292 case OVS_ACTION_ATTR_POP_VLAN:
d78be13b
BP
2293 vlan_tci = htons(0);
2294 break;
2295
d9065a90 2296 case OVS_ACTION_ATTR_PUSH_VLAN:
d78be13b
BP
2297 vlan_tci = nl_attr_get_be16(a);
2298 break;
abe529af
BP
2299 }
2300 }
2301}
2302
2303/* If 'rule' is installed in the datapath, uninstalls it. */
2304static void
2305facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2306{
2307 if (facet->installed) {
2308 struct odputil_keybuf keybuf;
2309 struct dpif_flow_stats stats;
2310 struct ofpbuf key;
3a88e544 2311 int error;
abe529af
BP
2312
2313 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2314 odp_flow_key_from_flow(&key, &facet->flow);
2315
3a88e544
BP
2316 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2317 facet_reset_dp_stats(facet, &stats);
2318 if (!error) {
abe529af
BP
2319 facet_update_stats(p, facet, &stats);
2320 }
2321 facet->installed = false;
abe529af
BP
2322 } else {
2323 assert(facet->dp_packet_count == 0);
2324 assert(facet->dp_byte_count == 0);
2325 }
2326}
2327
2328/* Returns true if the only action for 'facet' is to send to the controller.
2329 * (We don't report NetFlow expiration messages for such facets because they
2330 * are just part of the control logic for the network, not real traffic). */
2331static bool
2332facet_is_controller_flow(struct facet *facet)
2333{
2334 return (facet
2335 && facet->rule->up.n_actions == 1
2336 && action_outputs_to_port(&facet->rule->up.actions[0],
2337 htons(OFPP_CONTROLLER)));
2338}
2339
3a88e544
BP
2340/* Resets 'facet''s datapath statistics counters. This should be called when
2341 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2342 * it should contain the statistics returned by dpif when 'facet' was reset in
2343 * the datapath. 'stats' will be modified to only included statistics new
2344 * since 'facet' was last updated. */
2345static void
2346facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2347{
2348 if (stats && facet->dp_packet_count <= stats->n_packets
2349 && facet->dp_byte_count <= stats->n_bytes) {
2350 stats->n_packets -= facet->dp_packet_count;
2351 stats->n_bytes -= facet->dp_byte_count;
2352 }
2353
2354 facet->dp_packet_count = 0;
2355 facet->dp_byte_count = 0;
2356}
2357
abe529af
BP
2358/* Folds all of 'facet''s statistics into its rule. Also updates the
2359 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2360 * 'facet''s statistics in the datapath should have been zeroed and folded into
2361 * its packet and byte counts before this function is called. */
2362static void
2363facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2364{
2365 assert(!facet->dp_byte_count);
2366 assert(!facet->dp_packet_count);
2367
2368 facet_push_stats(facet);
55af77bb 2369 facet_account(ofproto, facet);
abe529af
BP
2370
2371 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2372 struct ofexpired expired;
2373 expired.flow = facet->flow;
2374 expired.packet_count = facet->packet_count;
2375 expired.byte_count = facet->byte_count;
2376 expired.used = facet->used;
2377 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2378 }
2379
2380 facet->rule->packet_count += facet->packet_count;
2381 facet->rule->byte_count += facet->byte_count;
2382
2383 /* Reset counters to prevent double counting if 'facet' ever gets
2384 * reinstalled. */
bbb5d219 2385 facet_reset_counters(facet);
abe529af
BP
2386
2387 netflow_flow_clear(&facet->nf_flow);
2388}
2389
2390/* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2391 * Returns it if found, otherwise a null pointer.
2392 *
2393 * The returned facet might need revalidation; use facet_lookup_valid()
2394 * instead if that is important. */
2395static struct facet *
2396facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2397{
2398 struct facet *facet;
2399
2400 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2401 &ofproto->facets) {
2402 if (flow_equal(flow, &facet->flow)) {
2403 return facet;
2404 }
2405 }
2406
2407 return NULL;
2408}
2409
2410/* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2411 * Returns it if found, otherwise a null pointer.
2412 *
2413 * The returned facet is guaranteed to be valid. */
2414static struct facet *
2415facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2416{
2417 struct facet *facet = facet_find(ofproto, flow);
2418
2419 /* The facet we found might not be valid, since we could be in need of
2420 * revalidation. If it is not valid, don't return it. */
2421 if (facet
2422 && ofproto->need_revalidate
2423 && !facet_revalidate(ofproto, facet)) {
2424 COVERAGE_INC(facet_invalidated);
2425 return NULL;
2426 }
2427
2428 return facet;
2429}
2430
2431/* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2432 *
2433 * - If the rule found is different from 'facet''s current rule, moves
2434 * 'facet' to the new rule and recompiles its actions.
2435 *
2436 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2437 * where it is and recompiles its actions anyway.
2438 *
2439 * - If there is none, destroys 'facet'.
2440 *
2441 * Returns true if 'facet' still exists, false if it has been destroyed. */
2442static bool
2443facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2444{
2445 struct action_xlate_ctx ctx;
2446 struct ofpbuf *odp_actions;
2447 struct rule_dpif *new_rule;
2448 bool actions_changed;
2449
2450 COVERAGE_INC(facet_revalidate);
2451
2452 /* Determine the new rule. */
29901626 2453 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
abe529af
BP
2454 if (!new_rule) {
2455 /* No new rule, so delete the facet. */
2456 facet_remove(ofproto, facet);
2457 return false;
2458 }
2459
df2c07f4 2460 /* Calculate new datapath actions.
abe529af
BP
2461 *
2462 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2463 * emit a NetFlow expiration and, if so, we need to have the old state
2464 * around to properly compose it. */
2465 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2466 odp_actions = xlate_actions(&ctx,
2467 new_rule->up.actions, new_rule->up.n_actions);
2468 actions_changed = (facet->actions_len != odp_actions->size
2469 || memcmp(facet->actions, odp_actions->data,
2470 facet->actions_len));
2471
df2c07f4
JP
2472 /* If the datapath actions changed or the installability changed,
2473 * then we need to talk to the datapath. */
abe529af
BP
2474 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2475 if (ctx.may_set_up_flow) {
2476 struct dpif_flow_stats stats;
2477
2478 facet_put__(ofproto, facet,
2479 odp_actions->data, odp_actions->size, &stats);
2480 facet_update_stats(ofproto, facet, &stats);
2481 } else {
2482 facet_uninstall(ofproto, facet);
2483 }
2484
2485 /* The datapath flow is gone or has zeroed stats, so push stats out of
2486 * 'facet' into 'rule'. */
2487 facet_flush_stats(ofproto, facet);
2488 }
2489
2490 /* Update 'facet' now that we've taken care of all the old state. */
2491 facet->tags = ctx.tags;
2492 facet->nf_flow.output_iface = ctx.nf_output_iface;
2493 facet->may_install = ctx.may_set_up_flow;
2494 if (actions_changed) {
2495 free(facet->actions);
2496 facet->actions_len = odp_actions->size;
2497 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2498 }
2499 if (facet->rule != new_rule) {
2500 COVERAGE_INC(facet_changed_rule);
2501 list_remove(&facet->list_node);
2502 list_push_back(&new_rule->facets, &facet->list_node);
2503 facet->rule = new_rule;
2504 facet->used = new_rule->up.created;
2505 facet->rs_used = facet->used;
2506 }
2507
2508 ofpbuf_delete(odp_actions);
2509
2510 return true;
2511}
2512
2513/* Updates 'facet''s used time. Caller is responsible for calling
2514 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2515static void
2516facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2517 long long int used)
2518{
2519 if (used > facet->used) {
2520 facet->used = used;
2521 if (used > facet->rule->used) {
2522 facet->rule->used = used;
2523 }
2524 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2525 }
2526}
2527
2528/* Folds the statistics from 'stats' into the counters in 'facet'.
2529 *
2530 * Because of the meaning of a facet's counters, it only makes sense to do this
2531 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2532 * packet that was sent by hand or if it represents statistics that have been
2533 * cleared out of the datapath. */
2534static void
2535facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2536 const struct dpif_flow_stats *stats)
2537{
2538 if (stats->n_packets || stats->used > facet->used) {
2539 facet_update_time(ofproto, facet, stats->used);
2540 facet->packet_count += stats->n_packets;
2541 facet->byte_count += stats->n_bytes;
2542 facet_push_stats(facet);
2543 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2544 }
2545}
2546
bbb5d219
EJ
2547static void
2548facet_reset_counters(struct facet *facet)
2549{
2550 facet->packet_count = 0;
2551 facet->byte_count = 0;
2552 facet->rs_packet_count = 0;
2553 facet->rs_byte_count = 0;
2554 facet->accounted_bytes = 0;
2555}
2556
abe529af
BP
2557static void
2558facet_push_stats(struct facet *facet)
2559{
2560 uint64_t rs_packets, rs_bytes;
2561
2562 assert(facet->packet_count >= facet->rs_packet_count);
2563 assert(facet->byte_count >= facet->rs_byte_count);
2564 assert(facet->used >= facet->rs_used);
2565
2566 rs_packets = facet->packet_count - facet->rs_packet_count;
2567 rs_bytes = facet->byte_count - facet->rs_byte_count;
2568
2569 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2570 facet->rs_packet_count = facet->packet_count;
2571 facet->rs_byte_count = facet->byte_count;
2572 facet->rs_used = facet->used;
2573
2574 flow_push_stats(facet->rule, &facet->flow,
2575 rs_packets, rs_bytes, facet->used);
2576 }
2577}
2578
2579struct ofproto_push {
2580 struct action_xlate_ctx ctx;
2581 uint64_t packets;
2582 uint64_t bytes;
2583 long long int used;
2584};
2585
2586static void
2587push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2588{
2589 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2590
2591 if (rule) {
2592 rule->packet_count += push->packets;
2593 rule->byte_count += push->bytes;
2594 rule->used = MAX(push->used, rule->used);
2595 }
2596}
2597
2598/* Pushes flow statistics to the rules which 'flow' resubmits into given
2599 * 'rule''s actions. */
2600static void
2601flow_push_stats(const struct rule_dpif *rule,
2602 struct flow *flow, uint64_t packets, uint64_t bytes,
2603 long long int used)
2604{
2605 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2606 struct ofproto_push push;
2607
2608 push.packets = packets;
2609 push.bytes = bytes;
2610 push.used = used;
2611
2612 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2613 push.ctx.resubmit_hook = push_resubmit;
2614 ofpbuf_delete(xlate_actions(&push.ctx,
2615 rule->up.actions, rule->up.n_actions));
2616}
2617\f
2618/* Rules. */
2619
2620static struct rule_dpif *
29901626
BP
2621rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2622 uint8_t table_id)
abe529af 2623{
154896e3 2624 return rule_dpif_cast(rule_from_cls_rule(
29901626 2625 classifier_lookup(&ofproto->up.tables[table_id],
6c1491fb 2626 flow)));
abe529af
BP
2627}
2628
7ee20df1
BP
2629static void
2630complete_operation(struct rule_dpif *rule)
2631{
2632 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2633
2634 ofproto->need_revalidate = true;
2635 if (clogged) {
2636 struct dpif_completion *c = xmalloc(sizeof *c);
2637 c->op = rule->up.pending;
2638 list_push_back(&ofproto->completions, &c->list_node);
2639 } else {
2640 ofoperation_complete(rule->up.pending, 0);
2641 }
2642}
2643
abe529af
BP
2644static struct rule *
2645rule_alloc(void)
2646{
2647 struct rule_dpif *rule = xmalloc(sizeof *rule);
2648 return &rule->up;
2649}
2650
2651static void
2652rule_dealloc(struct rule *rule_)
2653{
2654 struct rule_dpif *rule = rule_dpif_cast(rule_);
2655 free(rule);
2656}
2657
2658static int
2659rule_construct(struct rule *rule_)
2660{
2661 struct rule_dpif *rule = rule_dpif_cast(rule_);
2662 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
7ee20df1 2663 struct rule_dpif *victim;
5bf0e941
BP
2664 int error;
2665
2666 error = validate_actions(rule->up.actions, rule->up.n_actions,
2667 &rule->up.cr.flow, ofproto->max_ports);
2668 if (error) {
2669 return error;
2670 }
abe529af
BP
2671
2672 rule->used = rule->up.created;
2673 rule->packet_count = 0;
2674 rule->byte_count = 0;
abe529af 2675
7ee20df1
BP
2676 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2677 if (victim && !list_is_empty(&victim->facets)) {
2678 struct facet *facet;
2679
2680 rule->facets = victim->facets;
2681 list_moved(&rule->facets);
2682 LIST_FOR_EACH (facet, list_node, &rule->facets) {
bbb5d219
EJ
2683 /* XXX: We're only clearing our local counters here. It's possible
2684 * that quite a few packets are unaccounted for in the datapath
2685 * statistics. These will be accounted to the new rule instead of
2686 * cleared as required. This could be fixed by clearing out the
2687 * datapath statistics for this facet, but currently it doesn't
2688 * seem worth it. */
2689 facet_reset_counters(facet);
7ee20df1
BP
2690 facet->rule = rule;
2691 }
2692 } else {
2693 /* Must avoid list_moved() in this case. */
2694 list_init(&rule->facets);
2695 }
abe529af 2696
7ee20df1 2697 complete_operation(rule);
abe529af
BP
2698 return 0;
2699}
2700
2701static void
2702rule_destruct(struct rule *rule_)
2703{
2704 struct rule_dpif *rule = rule_dpif_cast(rule_);
2705 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2706 struct facet *facet, *next_facet;
2707
abe529af
BP
2708 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2709 facet_revalidate(ofproto, facet);
2710 }
7ee20df1
BP
2711
2712 complete_operation(rule);
abe529af
BP
2713}
2714
2715static void
2716rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2717{
2718 struct rule_dpif *rule = rule_dpif_cast(rule_);
2719 struct facet *facet;
2720
2721 /* Start from historical data for 'rule' itself that are no longer tracked
2722 * in facets. This counts, for example, facets that have expired. */
2723 *packets = rule->packet_count;
2724 *bytes = rule->byte_count;
2725
2726 /* Add any statistics that are tracked by facets. This includes
2727 * statistical data recently updated by ofproto_update_stats() as well as
2728 * stats for packets that were executed "by hand" via dpif_execute(). */
2729 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2730 *packets += facet->packet_count;
2731 *bytes += facet->byte_count;
2732 }
2733}
2734
5bf0e941 2735static int
abe529af
BP
2736rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2737{
2738 struct rule_dpif *rule = rule_dpif_cast(rule_);
2739 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2740 struct action_xlate_ctx ctx;
2741 struct ofpbuf *odp_actions;
2742 struct facet *facet;
2743 size_t size;
2744
2745 /* First look for a related facet. If we find one, account it to that. */
2746 facet = facet_lookup_valid(ofproto, flow);
2747 if (facet && facet->rule == rule) {
2748 facet_execute(ofproto, facet, packet);
5bf0e941 2749 return 0;
abe529af
BP
2750 }
2751
2752 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2753 * create a new facet for it and use that. */
29901626 2754 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
abe529af
BP
2755 facet = facet_create(rule, flow, packet);
2756 facet_execute(ofproto, facet, packet);
2757 facet_install(ofproto, facet, true);
5bf0e941 2758 return 0;
abe529af
BP
2759 }
2760
2761 /* We can't account anything to a facet. If we were to try, then that
2762 * facet would have a non-matching rule, busting our invariants. */
2763 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2764 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2765 size = packet->size;
2766 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2767 odp_actions->size, packet)) {
2768 rule->used = time_msec();
2769 rule->packet_count++;
2770 rule->byte_count += size;
2771 flow_push_stats(rule, flow, 1, size, rule->used);
2772 }
2773 ofpbuf_delete(odp_actions);
5bf0e941
BP
2774
2775 return 0;
abe529af
BP
2776}
2777
7ee20df1
BP
2778static void
2779rule_modify_actions(struct rule *rule_)
abe529af
BP
2780{
2781 struct rule_dpif *rule = rule_dpif_cast(rule_);
2782 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2783 int error;
2784
7ee20df1
BP
2785 error = validate_actions(rule->up.actions, rule->up.n_actions,
2786 &rule->up.cr.flow, ofproto->max_ports);
2787 if (error) {
2788 ofoperation_complete(rule->up.pending, error);
2789 return;
abe529af 2790 }
7ee20df1
BP
2791
2792 complete_operation(rule);
abe529af
BP
2793}
2794\f
b2fda3ef 2795/* Sends 'packet' out of port 'odp_port' within 'p'.
abe529af
BP
2796 * Returns 0 if successful, otherwise a positive errno value. */
2797static int
b2fda3ef 2798send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
abe529af
BP
2799 const struct ofpbuf *packet)
2800{
80e5eed9
BP
2801 struct ofpbuf key, odp_actions;
2802 struct odputil_keybuf keybuf;
2803 struct flow flow;
abe529af
BP
2804 int error;
2805
80e5eed9
BP
2806 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2807 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2808 odp_flow_key_from_flow(&key, &flow);
2809
abe529af 2810 ofpbuf_init(&odp_actions, 32);
df2c07f4 2811 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
80e5eed9
BP
2812 error = dpif_execute(ofproto->dpif,
2813 key.data, key.size,
2814 odp_actions.data, odp_actions.size,
abe529af
BP
2815 packet);
2816 ofpbuf_uninit(&odp_actions);
2817
2818 if (error) {
2819 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2820 ofproto->up.name, odp_port, strerror(error));
2821 }
2822 return error;
2823}
2824\f
df2c07f4 2825/* OpenFlow to datapath action translation. */
abe529af
BP
2826
2827static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2828 struct action_xlate_ctx *ctx);
4cd78906 2829static void xlate_normal(struct action_xlate_ctx *);
abe529af 2830
b3e9b2ed
EJ
2831static void
2832commit_odp_actions(struct action_xlate_ctx *ctx)
2833{
2834 const struct flow *flow = &ctx->flow;
2835 struct flow *base = &ctx->base_flow;
2836 struct ofpbuf *odp_actions = ctx->odp_actions;
2837
2838 if (base->tun_id != flow->tun_id) {
df2c07f4 2839 nl_msg_put_be64(odp_actions, OVS_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
b3e9b2ed
EJ
2840 base->tun_id = flow->tun_id;
2841 }
2842
2843 if (base->nw_src != flow->nw_src) {
df2c07f4 2844 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
b3e9b2ed
EJ
2845 base->nw_src = flow->nw_src;
2846 }
2847
2848 if (base->nw_dst != flow->nw_dst) {
df2c07f4 2849 nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
b3e9b2ed
EJ
2850 base->nw_dst = flow->nw_dst;
2851 }
2852
150a9f15 2853 if (base->nw_tos != flow->nw_tos) {
df2c07f4 2854 nl_msg_put_u8(odp_actions, OVS_ACTION_ATTR_SET_NW_TOS, flow->nw_tos);
150a9f15
BP
2855 base->nw_tos = flow->nw_tos;
2856 }
2857
b3e9b2ed
EJ
2858 if (base->vlan_tci != flow->vlan_tci) {
2859 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
d9065a90 2860 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
b3e9b2ed 2861 } else {
2f2df2f4 2862 if (base->vlan_tci != htons(0)) {
d9065a90
PS
2863 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2864 }
2865 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
b3e9b2ed
EJ
2866 flow->vlan_tci & ~htons(VLAN_CFI));
2867 }
2868 base->vlan_tci = flow->vlan_tci;
2869 }
2870
2871 if (base->tp_src != flow->tp_src) {
df2c07f4 2872 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
b3e9b2ed
EJ
2873 base->tp_src = flow->tp_src;
2874 }
2875
2876 if (base->tp_dst != flow->tp_dst) {
df2c07f4 2877 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
b3e9b2ed
EJ
2878 base->tp_dst = flow->tp_dst;
2879 }
2880
2881 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
df2c07f4 2882 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_SRC,
b3e9b2ed
EJ
2883 flow->dl_src, ETH_ADDR_LEN);
2884 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2885 }
2886
2887 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
df2c07f4 2888 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_DST,
b3e9b2ed
EJ
2889 flow->dl_dst, ETH_ADDR_LEN);
2890 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2891 }
2892
2893 if (ctx->base_priority != ctx->priority) {
2894 if (ctx->priority) {
df2c07f4 2895 nl_msg_put_u32(odp_actions, OVS_ACTION_ATTR_SET_PRIORITY,
b3e9b2ed
EJ
2896 ctx->priority);
2897 } else {
df2c07f4 2898 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
b3e9b2ed
EJ
2899 }
2900 ctx->base_priority = ctx->priority;
2901 }
2902}
2903
abe529af
BP
2904static void
2905add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2906{
2907 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2908 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2909
2910 if (ofport) {
2911 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2912 /* Forwarding disabled on port. */
2913 return;
2914 }
2915 } else {
2916 /*
2917 * We don't have an ofport record for this port, but it doesn't hurt to
2918 * allow forwarding to it anyhow. Maybe such a port will appear later
2919 * and we're pre-populating the flow table.
2920 */
2921 }
2922
b3e9b2ed 2923 commit_odp_actions(ctx);
df2c07f4 2924 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
abe529af
BP
2925 ctx->nf_output_iface = ofp_port;
2926}
2927
2928static void
29901626
BP
2929xlate_table_action(struct action_xlate_ctx *ctx,
2930 uint16_t in_port, uint8_t table_id)
abe529af
BP
2931{
2932 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2933 struct rule_dpif *rule;
2934 uint16_t old_in_port;
29901626
BP
2935 uint8_t old_table_id;
2936
2937 old_table_id = ctx->table_id;
2938 ctx->table_id = table_id;
abe529af
BP
2939
2940 /* Look up a flow with 'in_port' as the input port. Then restore the
2941 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2942 * have surprising behavior). */
2943 old_in_port = ctx->flow.in_port;
2944 ctx->flow.in_port = in_port;
29901626 2945 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow, table_id);
abe529af
BP
2946 ctx->flow.in_port = old_in_port;
2947
2948 if (ctx->resubmit_hook) {
2949 ctx->resubmit_hook(ctx, rule);
2950 }
2951
2952 if (rule) {
2953 ctx->recurse++;
2954 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2955 ctx->recurse--;
2956 }
29901626
BP
2957
2958 ctx->table_id = old_table_id;
abe529af
BP
2959 } else {
2960 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2961
29901626 2962 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
abe529af
BP
2963 MAX_RESUBMIT_RECURSION);
2964 }
2965}
2966
29901626
BP
2967static void
2968xlate_resubmit_table(struct action_xlate_ctx *ctx,
2969 const struct nx_action_resubmit *nar)
2970{
2971 uint16_t in_port;
2972 uint8_t table_id;
2973
2974 in_port = (nar->in_port == htons(OFPP_IN_PORT)
2975 ? ctx->flow.in_port
2976 : ntohs(nar->in_port));
2977 table_id = nar->table == 255 ? ctx->table_id : nar->table;
2978
2979 xlate_table_action(ctx, in_port, table_id);
2980}
2981
abe529af 2982static void
b3e9b2ed 2983flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
abe529af
BP
2984{
2985 struct ofport_dpif *ofport;
2986
b3e9b2ed
EJ
2987 commit_odp_actions(ctx);
2988 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
abe529af 2989 uint16_t ofp_port = ofport->up.ofp_port;
b3e9b2ed 2990 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
df2c07f4 2991 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT,
abe529af
BP
2992 ofport->odp_port);
2993 }
2994 }
b3e9b2ed
EJ
2995
2996 ctx->nf_output_iface = NF_OUT_FLOOD;
abe529af
BP
2997}
2998
2999static void
3000xlate_output_action__(struct action_xlate_ctx *ctx,
3001 uint16_t port, uint16_t max_len)
3002{
3003 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3004
3005 ctx->nf_output_iface = NF_OUT_DROP;
3006
3007 switch (port) {
3008 case OFPP_IN_PORT:
3009 add_output_action(ctx, ctx->flow.in_port);
3010 break;
3011 case OFPP_TABLE:
29901626 3012 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
abe529af
BP
3013 break;
3014 case OFPP_NORMAL:
3015 xlate_normal(ctx);
3016 break;
3017 case OFPP_FLOOD:
b3e9b2ed 3018 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
abe529af
BP
3019 break;
3020 case OFPP_ALL:
b3e9b2ed 3021 flood_packets(ctx, htonl(0));
abe529af
BP
3022 break;
3023 case OFPP_CONTROLLER:
b3e9b2ed 3024 commit_odp_actions(ctx);
df2c07f4 3025 nl_msg_put_u64(ctx->odp_actions, OVS_ACTION_ATTR_USERSPACE, max_len);
abe529af
BP
3026 break;
3027 case OFPP_LOCAL:
3028 add_output_action(ctx, OFPP_LOCAL);
3029 break;
e81d2933
EJ
3030 case OFPP_NONE:
3031 break;
abe529af
BP
3032 default:
3033 if (port != ctx->flow.in_port) {
3034 add_output_action(ctx, port);
3035 }
3036 break;
3037 }
3038
3039 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3040 ctx->nf_output_iface = NF_OUT_FLOOD;
3041 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3042 ctx->nf_output_iface = prev_nf_output_iface;
3043 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3044 ctx->nf_output_iface != NF_OUT_FLOOD) {
3045 ctx->nf_output_iface = NF_OUT_MULTI;
3046 }
3047}
3048
f694937d
EJ
3049static void
3050xlate_output_reg_action(struct action_xlate_ctx *ctx,
3051 const struct nx_action_output_reg *naor)
3052{
3053 uint64_t ofp_port;
3054
3055 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3056
3057 if (ofp_port <= UINT16_MAX) {
3058 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3059 }
3060}
3061
abe529af
BP
3062static void
3063xlate_output_action(struct action_xlate_ctx *ctx,
3064 const struct ofp_action_output *oao)
3065{
3066 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3067}
3068
abe529af
BP
3069static void
3070xlate_enqueue_action(struct action_xlate_ctx *ctx,
3071 const struct ofp_action_enqueue *oae)
3072{
3073 uint16_t ofp_port, odp_port;
b3e9b2ed 3074 uint32_t ctx_priority, priority;
abe529af
BP
3075 int error;
3076
3077 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3078 &priority);
3079 if (error) {
3080 /* Fall back to ordinary output action. */
3081 xlate_output_action__(ctx, ntohs(oae->port), 0);
3082 return;
3083 }
3084
df2c07f4 3085 /* Figure out datapath output port. */
abe529af
BP
3086 ofp_port = ntohs(oae->port);
3087 if (ofp_port == OFPP_IN_PORT) {
3088 ofp_port = ctx->flow.in_port;
3089 }
3090 odp_port = ofp_port_to_odp_port(ofp_port);
3091
df2c07f4 3092 /* Add datapath actions. */
b3e9b2ed
EJ
3093 ctx_priority = ctx->priority;
3094 ctx->priority = priority;
abe529af 3095 add_output_action(ctx, odp_port);
b3e9b2ed 3096 ctx->priority = ctx_priority;
abe529af
BP
3097
3098 /* Update NetFlow output port. */
3099 if (ctx->nf_output_iface == NF_OUT_DROP) {
3100 ctx->nf_output_iface = odp_port;
3101 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3102 ctx->nf_output_iface = NF_OUT_MULTI;
3103 }
3104}
3105
3106static void
3107xlate_set_queue_action(struct action_xlate_ctx *ctx,
3108 const struct nx_action_set_queue *nasq)
3109{
3110 uint32_t priority;
3111 int error;
3112
3113 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3114 &priority);
3115 if (error) {
3116 /* Couldn't translate queue to a priority, so ignore. A warning
3117 * has already been logged. */
3118 return;
3119 }
3120
afabef2b 3121 ctx->priority = priority;
abe529af
BP
3122}
3123
3124struct xlate_reg_state {
3125 ovs_be16 vlan_tci;
3126 ovs_be64 tun_id;
3127};
3128
abe529af
BP
3129static void
3130xlate_autopath(struct action_xlate_ctx *ctx,
3131 const struct nx_action_autopath *naa)
3132{
3133 uint16_t ofp_port = ntohl(naa->id);
3134 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3135
3136 if (!port || !port->bundle) {
3137 ofp_port = OFPP_NONE;
3138 } else if (port->bundle->bond) {
3139 /* Autopath does not support VLAN hashing. */
3140 struct ofport_dpif *slave = bond_choose_output_slave(
3141 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3142 if (slave) {
3143 ofp_port = slave->up.ofp_port;
3144 }
3145 }
3146 autopath_execute(naa, &ctx->flow, ofp_port);
3147}
3148
daff3353
EJ
3149static bool
3150slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3151{
3152 struct ofproto_dpif *ofproto = ofproto_;
3153 struct ofport_dpif *port;
3154
3155 switch (ofp_port) {
3156 case OFPP_IN_PORT:
3157 case OFPP_TABLE:
3158 case OFPP_NORMAL:
3159 case OFPP_FLOOD:
3160 case OFPP_ALL:
3161 case OFPP_LOCAL:
3162 return true;
3163 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3164 return false;
3165 default:
3166 port = get_ofp_port(ofproto, ofp_port);
3167 return port ? port->may_enable : false;
3168 }
3169}
3170
abe529af
BP
3171static void
3172do_xlate_actions(const union ofp_action *in, size_t n_in,
3173 struct action_xlate_ctx *ctx)
3174{
3175 const struct ofport_dpif *port;
abe529af 3176 const union ofp_action *ia;
b4b8c781 3177 size_t left;
abe529af
BP
3178
3179 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3180 if (port
3181 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3182 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3183 ? htonl(OFPPC_NO_RECV_STP)
3184 : htonl(OFPPC_NO_RECV))) {
3185 /* Drop this flow. */
3186 return;
3187 }
3188
b4b8c781 3189 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
abe529af 3190 const struct ofp_action_dl_addr *oada;
38f2e360
BP
3191 const struct nx_action_resubmit *nar;
3192 const struct nx_action_set_tunnel *nast;
3193 const struct nx_action_set_queue *nasq;
3194 const struct nx_action_multipath *nam;
3195 const struct nx_action_autopath *naa;
daff3353 3196 const struct nx_action_bundle *nab;
f694937d 3197 const struct nx_action_output_reg *naor;
38f2e360
BP
3198 enum ofputil_action_code code;
3199 ovs_be64 tun_id;
3200
3201 code = ofputil_decode_action_unsafe(ia);
3202 switch (code) {
3203 case OFPUTIL_OFPAT_OUTPUT:
abe529af
BP
3204 xlate_output_action(ctx, &ia->output);
3205 break;
3206
38f2e360 3207 case OFPUTIL_OFPAT_SET_VLAN_VID:
abe529af
BP
3208 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3209 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
abe529af
BP
3210 break;
3211
38f2e360 3212 case OFPUTIL_OFPAT_SET_VLAN_PCP:
abe529af
BP
3213 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3214 ctx->flow.vlan_tci |= htons(
3215 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
abe529af
BP
3216 break;
3217
38f2e360 3218 case OFPUTIL_OFPAT_STRIP_VLAN:
abe529af 3219 ctx->flow.vlan_tci = htons(0);
abe529af
BP
3220 break;
3221
38f2e360 3222 case OFPUTIL_OFPAT_SET_DL_SRC:
abe529af 3223 oada = ((struct ofp_action_dl_addr *) ia);
abe529af
BP
3224 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3225 break;
3226
38f2e360 3227 case OFPUTIL_OFPAT_SET_DL_DST:
abe529af 3228 oada = ((struct ofp_action_dl_addr *) ia);
abe529af
BP
3229 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3230 break;
3231
38f2e360 3232 case OFPUTIL_OFPAT_SET_NW_SRC:
abe529af
BP
3233 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3234 break;
3235
38f2e360 3236 case OFPUTIL_OFPAT_SET_NW_DST:
abe529af
BP
3237 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3238 break;
3239
38f2e360 3240 case OFPUTIL_OFPAT_SET_NW_TOS:
1fa96cf4 3241 ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK;
abe529af
BP
3242 break;
3243
38f2e360 3244 case OFPUTIL_OFPAT_SET_TP_SRC:
abe529af
BP
3245 ctx->flow.tp_src = ia->tp_port.tp_port;
3246 break;
3247
38f2e360 3248 case OFPUTIL_OFPAT_SET_TP_DST:
abe529af
BP
3249 ctx->flow.tp_dst = ia->tp_port.tp_port;
3250 break;
3251
38f2e360
BP
3252 case OFPUTIL_OFPAT_ENQUEUE:
3253 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3254 break;
3255
3256 case OFPUTIL_NXAST_RESUBMIT:
3257 nar = (const struct nx_action_resubmit *) ia;
29901626
BP
3258 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
3259 break;
3260
3261 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3262 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
abe529af
BP
3263 break;
3264
38f2e360
BP
3265 case OFPUTIL_NXAST_SET_TUNNEL:
3266 nast = (const struct nx_action_set_tunnel *) ia;
3267 tun_id = htonll(ntohl(nast->tun_id));
3268 ctx->flow.tun_id = tun_id;
3269 break;
3270
3271 case OFPUTIL_NXAST_SET_QUEUE:
3272 nasq = (const struct nx_action_set_queue *) ia;
3273 xlate_set_queue_action(ctx, nasq);
3274 break;
3275
3276 case OFPUTIL_NXAST_POP_QUEUE:
3277 ctx->priority = 0;
3278 break;
3279
3280 case OFPUTIL_NXAST_REG_MOVE:
3281 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3282 &ctx->flow);
3283 break;
3284
3285 case OFPUTIL_NXAST_REG_LOAD:
3286 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3287 &ctx->flow);
3288 break;
3289
3290 case OFPUTIL_NXAST_NOTE:
3291 /* Nothing to do. */
3292 break;
3293
3294 case OFPUTIL_NXAST_SET_TUNNEL64:
3295 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3296 ctx->flow.tun_id = tun_id;
3297 break;
3298
3299 case OFPUTIL_NXAST_MULTIPATH:
3300 nam = (const struct nx_action_multipath *) ia;
3301 multipath_execute(nam, &ctx->flow);
abe529af
BP
3302 break;
3303
38f2e360
BP
3304 case OFPUTIL_NXAST_AUTOPATH:
3305 naa = (const struct nx_action_autopath *) ia;
3306 xlate_autopath(ctx, naa);
abe529af 3307 break;
daff3353
EJ
3308
3309 case OFPUTIL_NXAST_BUNDLE:
3310 ctx->ofproto->has_bundle_action = true;
3311 nab = (const struct nx_action_bundle *) ia;
3312 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3313 slave_enabled_cb,
3314 ctx->ofproto), 0);
3315 break;
a368bb53
EJ
3316
3317 case OFPUTIL_NXAST_BUNDLE_LOAD:
3318 ctx->ofproto->has_bundle_action = true;
3319 nab = (const struct nx_action_bundle *) ia;
3320 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3321 ctx->ofproto);
3322 break;
f694937d
EJ
3323
3324 case OFPUTIL_NXAST_OUTPUT_REG:
3325 naor = (const struct nx_action_output_reg *) ia;
3326 xlate_output_reg_action(ctx, naor);
3327 break;
abe529af
BP
3328 }
3329 }
3330}
3331
3332static void
3333action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3334 struct ofproto_dpif *ofproto, const struct flow *flow,
3335 const struct ofpbuf *packet)
3336{
3337 ctx->ofproto = ofproto;
3338 ctx->flow = *flow;
3339 ctx->packet = packet;
3340 ctx->resubmit_hook = NULL;
abe529af
BP
3341}
3342
3343static struct ofpbuf *
3344xlate_actions(struct action_xlate_ctx *ctx,
3345 const union ofp_action *in, size_t n_in)
3346{
3347 COVERAGE_INC(ofproto_dpif_xlate);
3348
3349 ctx->odp_actions = ofpbuf_new(512);
3350 ctx->tags = 0;
3351 ctx->may_set_up_flow = true;
3352 ctx->nf_output_iface = NF_OUT_DROP;
3353 ctx->recurse = 0;
afabef2b 3354 ctx->priority = 0;
b3e9b2ed
EJ
3355 ctx->base_priority = 0;
3356 ctx->base_flow = ctx->flow;
bf1e0371 3357 ctx->base_flow.tun_id = 0;
29901626 3358 ctx->table_id = 0;
abe529af 3359
fc08b7a2 3360 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
abe529af
BP
3361 ctx->may_set_up_flow = false;
3362 } else {
3363 do_xlate_actions(in, n_in, ctx);
3364 }
3365
abe529af
BP
3366 /* Check with in-band control to see if we're allowed to set up this
3367 * flow. */
3368 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3369 ctx->odp_actions->data,
3370 ctx->odp_actions->size)) {
3371 ctx->may_set_up_flow = false;
3372 }
3373
3374 return ctx->odp_actions;
3375}
3376\f
3377/* OFPP_NORMAL implementation. */
3378
3379struct dst {
3380 struct ofport_dpif *port;
3381 uint16_t vlan;
3382};
3383
3384struct dst_set {
3385 struct dst builtin[32];
3386 struct dst *dsts;
3387 size_t n, allocated;
3388};
3389
3390static void dst_set_init(struct dst_set *);
3391static void dst_set_add(struct dst_set *, const struct dst *);
3392static void dst_set_free(struct dst_set *);
3393
3394static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3395
3396static bool
3397set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3398 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3399{
3400 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3401 : in_bundle->vlan >= 0 ? in_bundle->vlan
3402 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3403 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3404
3405 dst->port = (!out_bundle->bond
3406 ? ofbundle_get_a_port(out_bundle)
3407 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3408 dst->vlan, &ctx->tags));
3409
3410 return dst->port != NULL;
3411}
3412
3413static int
3414mirror_mask_ffs(mirror_mask_t mask)
3415{
3416 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3417 return ffs(mask);
3418}
3419
3420static void
3421dst_set_init(struct dst_set *set)
3422{
3423 set->dsts = set->builtin;
3424 set->n = 0;
3425 set->allocated = ARRAY_SIZE(set->builtin);
3426}
3427
3428static void
3429dst_set_add(struct dst_set *set, const struct dst *dst)
3430{
3431 if (set->n >= set->allocated) {
3432 size_t new_allocated;
3433 struct dst *new_dsts;
3434
3435 new_allocated = set->allocated * 2;
3436 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3437 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3438
3439 dst_set_free(set);
3440
3441 set->dsts = new_dsts;
3442 set->allocated = new_allocated;
3443 }
3444 set->dsts[set->n++] = *dst;
3445}
3446
3447static void
3448dst_set_free(struct dst_set *set)
3449{
3450 if (set->dsts != set->builtin) {
3451 free(set->dsts);
3452 }
3453}
3454
3455static bool
3456dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3457{
3458 size_t i;
3459 for (i = 0; i < set->n; i++) {
3460 if (set->dsts[i].vlan == test->vlan
3461 && set->dsts[i].port == test->port) {
3462 return true;
3463 }
3464 }
3465 return false;
3466}
3467
3468static bool
3469ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3470{
fc3d7408
BP
3471 return (bundle->vlan < 0
3472 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
abe529af
BP
3473}
3474
3475static bool
3476ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3477{
3478 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3479}
3480
3481/* Returns an arbitrary interface within 'bundle'. */
3482static struct ofport_dpif *
3483ofbundle_get_a_port(const struct ofbundle *bundle)
3484{
3485 return CONTAINER_OF(list_front(&bundle->ports),
3486 struct ofport_dpif, bundle_node);
3487}
3488
3489static void
3490compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3491 const struct ofbundle *in_bundle,
3492 const struct ofbundle *out_bundle, struct dst_set *set)
3493{
3494 struct dst dst;
3495
3496 if (out_bundle == OFBUNDLE_FLOOD) {
3497 struct ofbundle *bundle;
3498
3499 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3500 if (bundle != in_bundle
3501 && ofbundle_includes_vlan(bundle, vlan)
3502 && bundle->floodable
3503 && !bundle->mirror_out
3504 && set_dst(ctx, &dst, in_bundle, bundle)) {
3505 dst_set_add(set, &dst);
3506 }
3507 }
3508 ctx->nf_output_iface = NF_OUT_FLOOD;
3509 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3510 dst_set_add(set, &dst);
3511 ctx->nf_output_iface = dst.port->odp_port;
3512 }
3513}
3514
3515static bool
3516vlan_is_mirrored(const struct ofmirror *m, int vlan)
3517{
fc3d7408 3518 return !m->vlans || bitmap_is_set(m->vlans, vlan);
abe529af
BP
3519}
3520
07817dfe
BP
3521/* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3522 * to a VLAN. In general most packets may be mirrored but we want to drop
3523 * protocols that may confuse switches. */
3524static bool
3525eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
3526{
3527 /* If you change this function's behavior, please update corresponding
3528 * documentation in vswitch.xml at the same time. */
3529 if (dst[0] != 0x01) {
3530 /* All the currently banned MACs happen to start with 01 currently, so
3531 * this is a quick way to eliminate most of the good ones. */
3532 } else {
3533 if (eth_addr_is_reserved(dst)) {
3534 /* Drop STP, IEEE pause frames, and other reserved protocols
3535 * (01-80-c2-00-00-0x). */
3536 return false;
3537 }
3538
3539 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
3540 /* Cisco OUI. */
3541 if ((dst[3] & 0xfe) == 0xcc &&
3542 (dst[4] & 0xfe) == 0xcc &&
3543 (dst[5] & 0xfe) == 0xcc) {
3544 /* Drop the following protocols plus others following the same
3545 pattern:
3546
3547 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3548 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3549 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3550 return false;
3551 }
3552
3553 if (!(dst[3] | dst[4] | dst[5])) {
3554 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3555 return false;
3556 }
3557 }
3558 }
3559 return true;
3560}
3561
abe529af
BP
3562static void
3563compose_mirror_dsts(struct action_xlate_ctx *ctx,
3564 uint16_t vlan, const struct ofbundle *in_bundle,
3565 struct dst_set *set)
3566{
3567 struct ofproto_dpif *ofproto = ctx->ofproto;
3568 mirror_mask_t mirrors;
3569 int flow_vlan;
3570 size_t i;
3571
3572 mirrors = in_bundle->src_mirrors;
3573 for (i = 0; i < set->n; i++) {
3574 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3575 }
3576
3577 if (!mirrors) {
3578 return;
3579 }
3580
3581 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3582 if (flow_vlan == 0) {
3583 flow_vlan = OFP_VLAN_NONE;
3584 }
3585
3586 while (mirrors) {
3587 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3588 if (vlan_is_mirrored(m, vlan)) {
3589 struct dst dst;
3590
3591 if (m->out) {
3592 if (set_dst(ctx, &dst, in_bundle, m->out)
3593 && !dst_is_duplicate(set, &dst)) {
3594 dst_set_add(set, &dst);
3595 }
07817dfe 3596 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
abe529af
BP
3597 struct ofbundle *bundle;
3598
3599 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3600 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3601 && set_dst(ctx, &dst, in_bundle, bundle))
3602 {
3603 if (bundle->vlan < 0) {
3604 dst.vlan = m->out_vlan;
3605 }
3606 if (dst_is_duplicate(set, &dst)) {
3607 continue;
3608 }
3609
3610 /* Use the vlan tag on the original flow instead of
3611 * the one passed in the vlan parameter. This ensures
3612 * that we compare the vlan from before any implicit
3613 * tagging tags place. This is necessary because
3614 * dst->vlan is the final vlan, after removing implicit
3615 * tags. */
3616 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3617 /* Don't send out input port on same VLAN. */
3618 continue;
3619 }
3620 dst_set_add(set, &dst);
3621 }
3622 }
3623 }
3624 }
3625 mirrors &= mirrors - 1;
3626 }
3627}
3628
3629static void
3630compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3631 const struct ofbundle *in_bundle,
3632 const struct ofbundle *out_bundle)
3633{
3634 uint16_t initial_vlan, cur_vlan;
3635 const struct dst *dst;
3636 struct dst_set set;
3637
3638 dst_set_init(&set);
3639 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3640 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3641
3642 /* Output all the packets we can without having to change the VLAN. */
3643 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3644 if (initial_vlan == 0) {
3645 initial_vlan = OFP_VLAN_NONE;
3646 }
3647 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3648 if (dst->vlan != initial_vlan) {
3649 continue;
3650 }
3651 nl_msg_put_u32(ctx->odp_actions,
df2c07f4 3652 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
abe529af
BP
3653 }
3654
3655 /* Then output the rest. */
3656 cur_vlan = initial_vlan;
3657 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3658 if (dst->vlan == initial_vlan) {
3659 continue;
3660 }
3661 if (dst->vlan != cur_vlan) {
3662 if (dst->vlan == OFP_VLAN_NONE) {
d9065a90 3663 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
abe529af
BP
3664 } else {
3665 ovs_be16 tci;
d9065a90
PS
3666
3667 if (cur_vlan != OFP_VLAN_NONE) {
3668 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3669 }
abe529af
BP
3670 tci = htons(dst->vlan & VLAN_VID_MASK);
3671 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3672 nl_msg_put_be16(ctx->odp_actions,
d9065a90 3673 OVS_ACTION_ATTR_PUSH_VLAN, tci);
abe529af
BP
3674 }
3675 cur_vlan = dst->vlan;
3676 }
3677 nl_msg_put_u32(ctx->odp_actions,
df2c07f4 3678 OVS_ACTION_ATTR_OUTPUT, dst->port->odp_port);
abe529af
BP
3679 }
3680
3681 dst_set_free(&set);
3682}
3683
3684/* Returns the effective vlan of a packet, taking into account both the
3685 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3686 * the packet is untagged and -1 indicates it has an invalid header and
3687 * should be dropped. */
3688static int
3689flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3690 struct ofbundle *in_bundle, bool have_packet)
3691{
3692 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3693 if (in_bundle->vlan >= 0) {
3694 if (vlan) {
3695 if (have_packet) {
3696 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3697 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3698 "packet received on port %s configured with "
3699 "implicit VLAN %"PRIu16,
3700 ofproto->up.name, vlan,
3701 in_bundle->name, in_bundle->vlan);
3702 }
3703 return -1;
3704 }
3705 vlan = in_bundle->vlan;
3706 } else {
3707 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3708 if (have_packet) {
3709 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3710 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3711 "packet received on port %s not configured for "
3712 "trunking VLAN %d",
3713 ofproto->up.name, vlan, in_bundle->name, vlan);
3714 }
3715 return -1;
3716 }
3717 }
3718
3719 return vlan;
3720}
3721
3722/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3723 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3724 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3725static bool
3726is_gratuitous_arp(const struct flow *flow)
3727{
3728 return (flow->dl_type == htons(ETH_TYPE_ARP)
3729 && eth_addr_is_broadcast(flow->dl_dst)
3730 && (flow->nw_proto == ARP_OP_REPLY
3731 || (flow->nw_proto == ARP_OP_REQUEST
3732 && flow->nw_src == flow->nw_dst)));
3733}
3734
3735static void
3736update_learning_table(struct ofproto_dpif *ofproto,
3737 const struct flow *flow, int vlan,
3738 struct ofbundle *in_bundle)
3739{
3740 struct mac_entry *mac;
3741
3742 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3743 return;
3744 }
3745
3746 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3747 if (is_gratuitous_arp(flow)) {
3748 /* We don't want to learn from gratuitous ARP packets that are
3749 * reflected back over bond slaves so we lock the learning table. */
3750 if (!in_bundle->bond) {
3751 mac_entry_set_grat_arp_lock(mac);
3752 } else if (mac_entry_is_grat_arp_locked(mac)) {
3753 return;
3754 }
3755 }
3756
3757 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3758 /* The log messages here could actually be useful in debugging,
3759 * so keep the rate limit relatively high. */
3760 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3761 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3762 "on port %s in VLAN %d",
3763 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3764 in_bundle->name, vlan);
3765
3766 mac->port.p = in_bundle;
3767 tag_set_add(&ofproto->revalidate_set,
3768 mac_learning_changed(ofproto->ml, mac));
3769 }
3770}
3771
3772/* Determines whether packets in 'flow' within 'br' should be forwarded or
3773 * dropped. Returns true if they may be forwarded, false if they should be
3774 * dropped.
3775 *
3776 * If 'have_packet' is true, it indicates that the caller is processing a
3777 * received packet. If 'have_packet' is false, then the caller is just
3778 * revalidating an existing flow because configuration has changed. Either
3779 * way, 'have_packet' only affects logging (there is no point in logging errors
3780 * during revalidation).
3781 *
3782 * Sets '*in_portp' to the input port. This will be a null pointer if
3783 * flow->in_port does not designate a known input port (in which case
3784 * is_admissible() returns false).
3785 *
3786 * When returning true, sets '*vlanp' to the effective VLAN of the input
3787 * packet, as returned by flow_get_vlan().
3788 *
3789 * May also add tags to '*tags', although the current implementation only does
3790 * so in one special case.
3791 */
3792static bool
3793is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3794 bool have_packet,
3795 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3796{
3797 struct ofport_dpif *in_port;
3798 struct ofbundle *in_bundle;
3799 int vlan;
3800
3801 /* Find the port and bundle for the received packet. */
3802 in_port = get_ofp_port(ofproto, flow->in_port);
23adee42 3803 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
abe529af
BP
3804 if (!in_port || !in_bundle) {
3805 /* No interface? Something fishy... */
3806 if (have_packet) {
3807 /* Odd. A few possible reasons here:
3808 *
3809 * - We deleted a port but there are still a few packets queued up
3810 * from it.
3811 *
3812 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3813 * we don't know about.
3814 *
3815 * - Packet arrived on the local port but the local port is not
3816 * part of a bundle.
3817 */
3818 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3819
3820 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3821 "port %"PRIu16,
3822 ofproto->up.name, flow->in_port);
3823 }
3824 return false;
3825 }
3826 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3827 if (vlan < 0) {
3828 return false;
3829 }
3830
b53055f4 3831 /* Drop frames for reserved multicast addresses
8402c74b 3832 * only if forward_bpdu option is absent. */
b53055f4 3833 if (eth_addr_is_reserved(flow->dl_dst) &&
8402c74b 3834 !ofproto->up.forward_bpdu) {
abe529af
BP
3835 return false;
3836 }
3837
3838 /* Drop frames on bundles reserved for mirroring. */
3839 if (in_bundle->mirror_out) {
3840 if (have_packet) {
3841 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3842 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3843 "%s, which is reserved exclusively for mirroring",
3844 ofproto->up.name, in_bundle->name);
3845 }
3846 return false;
3847 }
3848
3849 if (in_bundle->bond) {
3850 struct mac_entry *mac;
3851
3852 switch (bond_check_admissibility(in_bundle->bond, in_port,
3853 flow->dl_dst, tags)) {
3854 case BV_ACCEPT:
3855 break;
3856
3857 case BV_DROP:
3858 return false;
3859
3860 case BV_DROP_IF_MOVED:
3861 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3862 if (mac && mac->port.p != in_bundle &&
3863 (!is_gratuitous_arp(flow)
3864 || mac_entry_is_grat_arp_locked(mac))) {
3865 return false;
3866 }
3867 break;
3868 }
3869 }
3870
3871 return true;
3872}
3873
4cd78906 3874static void
abe529af
BP
3875xlate_normal(struct action_xlate_ctx *ctx)
3876{
3877 struct ofbundle *in_bundle;
3878 struct ofbundle *out_bundle;
3879 struct mac_entry *mac;
3880 int vlan;
3881
3882 /* Check whether we should drop packets in this flow. */
3883 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3884 &ctx->tags, &vlan, &in_bundle)) {
3885 out_bundle = NULL;
3886 goto done;
3887 }
3888
3889 /* Learn source MAC (but don't try to learn from revalidation). */
3890 if (ctx->packet) {
3891 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3892 }
3893
3894 /* Determine output bundle. */
3895 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3896 &ctx->tags);
3897 if (mac) {
3898 out_bundle = mac->port.p;
3899 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3900 /* If we are revalidating but don't have a learning entry then eject
3901 * the flow. Installing a flow that floods packets opens up a window
3902 * of time where we could learn from a packet reflected on a bond and
3903 * blackhole packets before the learning table is updated to reflect
3904 * the correct port. */
4cd78906
BP
3905 ctx->may_set_up_flow = false;
3906 return;
abe529af
BP
3907 } else {
3908 out_bundle = OFBUNDLE_FLOOD;
3909 }
3910
3911 /* Don't send packets out their input bundles. */
3912 if (in_bundle == out_bundle) {
3913 out_bundle = NULL;
3914 }
3915
3916done:
3917 if (in_bundle) {
3918 compose_actions(ctx, vlan, in_bundle, out_bundle);
3919 }
abe529af
BP
3920}
3921\f
3922static bool
3923get_drop_frags(struct ofproto *ofproto_)
3924{
3925 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3926 bool drop_frags;
3927
3928 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3929 return drop_frags;
3930}
3931
3932static void
3933set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3934{
3935 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3936
3937 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3938}
3939
3940static int
3941packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3942 const struct flow *flow,
3943 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3944{
3945 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3946 int error;
3947
3948 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3949 ofproto->max_ports);
3950 if (!error) {
80e5eed9 3951 struct odputil_keybuf keybuf;
abe529af
BP
3952 struct action_xlate_ctx ctx;
3953 struct ofpbuf *odp_actions;
80e5eed9
BP
3954 struct ofpbuf key;
3955
3956 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3957 odp_flow_key_from_flow(&key, flow);
abe529af
BP
3958
3959 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3960 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
80e5eed9
BP
3961 dpif_execute(ofproto->dpif, key.data, key.size,
3962 odp_actions->data, odp_actions->size, packet);
abe529af
BP
3963 ofpbuf_delete(odp_actions);
3964 }
3965 return error;
3966}
3967
3968static void
3969get_netflow_ids(const struct ofproto *ofproto_,
3970 uint8_t *engine_type, uint8_t *engine_id)
3971{
3972 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3973
3974 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3975}
3976\f
3977static struct ofproto_dpif *
3978ofproto_dpif_lookup(const char *name)
3979{
3980 struct ofproto *ofproto = ofproto_lookup(name);
3981 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3982 ? ofproto_dpif_cast(ofproto)
3983 : NULL);
3984}
3985
3986static void
3987ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3988 const char *args, void *aux OVS_UNUSED)
3989{
3990 struct ds ds = DS_EMPTY_INITIALIZER;
3991 const struct ofproto_dpif *ofproto;
3992 const struct mac_entry *e;
3993
3994 ofproto = ofproto_dpif_lookup(args);
3995 if (!ofproto) {
3996 unixctl_command_reply(conn, 501, "no such bridge");
3997 return;
3998 }
3999
4000 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4001 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4002 struct ofbundle *bundle = e->port.p;
4003 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
4004 ofbundle_get_a_port(bundle)->odp_port,
4005 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
4006 }
4007 unixctl_command_reply(conn, 200, ds_cstr(&ds));
4008 ds_destroy(&ds);
4009}
4010
4011struct ofproto_trace {
4012 struct action_xlate_ctx ctx;
4013 struct flow flow;
4014 struct ds *result;
4015};
4016
4017static void
29901626
BP
4018trace_format_rule(struct ds *result, uint8_t table_id, int level,
4019 const struct rule_dpif *rule)
abe529af
BP
4020{
4021 ds_put_char_multiple(result, '\t', level);
4022 if (!rule) {
4023 ds_put_cstr(result, "No match\n");
4024 return;
4025 }
4026
29901626
BP
4027 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4028 table_id, ntohll(rule->up.flow_cookie));
79feb7df 4029 cls_rule_format(&rule->up.cr, result);
abe529af
BP
4030 ds_put_char(result, '\n');
4031
4032 ds_put_char_multiple(result, '\t', level);
4033 ds_put_cstr(result, "OpenFlow ");
79feb7df 4034 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
abe529af
BP
4035 ds_put_char(result, '\n');
4036}
4037
4038static void
4039trace_format_flow(struct ds *result, int level, const char *title,
4040 struct ofproto_trace *trace)
4041{
4042 ds_put_char_multiple(result, '\t', level);
4043 ds_put_format(result, "%s: ", title);
4044 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4045 ds_put_cstr(result, "unchanged");
4046 } else {
4047 flow_format(result, &trace->ctx.flow);
4048 trace->flow = trace->ctx.flow;
4049 }
4050 ds_put_char(result, '\n');
4051}
4052
eb9e1c26
EJ
4053static void
4054trace_format_regs(struct ds *result, int level, const char *title,
4055 struct ofproto_trace *trace)
4056{
4057 size_t i;
4058
4059 ds_put_char_multiple(result, '\t', level);
4060 ds_put_format(result, "%s:", title);
4061 for (i = 0; i < FLOW_N_REGS; i++) {
4062 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
4063 }
4064 ds_put_char(result, '\n');
4065}
4066
abe529af
BP
4067static void
4068trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
4069{
4070 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4071 struct ds *result = trace->result;
4072
4073 ds_put_char(result, '\n');
4074 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
eb9e1c26 4075 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
29901626 4076 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
abe529af
BP
4077}
4078
4079static void
4080ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4081 void *aux OVS_UNUSED)
4082{
876b0e1c 4083 char *dpname, *arg1, *arg2, *arg3;
abe529af
BP
4084 char *args = xstrdup(args_);
4085 char *save_ptr = NULL;
4086 struct ofproto_dpif *ofproto;
876b0e1c
BP
4087 struct ofpbuf odp_key;
4088 struct ofpbuf *packet;
abe529af
BP
4089 struct rule_dpif *rule;
4090 struct ds result;
4091 struct flow flow;
abe529af
BP
4092 char *s;
4093
876b0e1c
BP
4094 packet = NULL;
4095 ofpbuf_init(&odp_key, 0);
abe529af
BP
4096 ds_init(&result);
4097
4098 dpname = strtok_r(args, " ", &save_ptr);
876b0e1c
BP
4099 arg1 = strtok_r(NULL, " ", &save_ptr);
4100 arg2 = strtok_r(NULL, " ", &save_ptr);
4101 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4102 if (dpname && arg1 && !arg2 && !arg3) {
4103 /* ofproto/trace dpname flow */
4104 int error;
4105
df2c07f4 4106 /* Convert string to datapath key. */
876b0e1c
BP
4107 ofpbuf_init(&odp_key, 0);
4108 error = odp_flow_key_from_string(arg1, &odp_key);
4109 if (error) {
4110 unixctl_command_reply(conn, 501, "Bad flow syntax");
4111 goto exit;
4112 }
4113
4114 /* Convert odp_key to flow. */
4115 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
4116 if (error) {
4117 unixctl_command_reply(conn, 501, "Invalid flow");
4118 goto exit;
4119 }
4120 } else if (dpname && arg1 && arg2 && arg3) {
4121 /* ofproto/trace dpname tun_id in_port packet */
4122 uint16_t in_port;
4123 ovs_be64 tun_id;
4124
4125 tun_id = htonll(strtoull(arg1, NULL, 0));
4126 in_port = ofp_port_to_odp_port(atoi(arg2));
4127
4128 packet = ofpbuf_new(strlen(args) / 2);
4129 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
4130 arg3 += strspn(arg3, " ");
4131 if (*arg3 != '\0') {
4132 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4133 goto exit;
4134 }
4135 if (packet->size < ETH_HEADER_LEN) {
4136 unixctl_command_reply(conn, 501,
4137 "Packet data too short for Ethernet");
4138 goto exit;
4139 }
4140
4141 ds_put_cstr(&result, "Packet: ");
4142 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
4143 ds_put_cstr(&result, s);
4144 free(s);
4145
4146 flow_extract(packet, tun_id, in_port, &flow);
4147 } else {
abe529af
BP
4148 unixctl_command_reply(conn, 501, "Bad command syntax");
4149 goto exit;
4150 }
4151
4152 ofproto = ofproto_dpif_lookup(dpname);
4153 if (!ofproto) {
4154 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4155 "for help)");
4156 goto exit;
4157 }
4158
abe529af
BP
4159 ds_put_cstr(&result, "Flow: ");
4160 flow_format(&result, &flow);
4161 ds_put_char(&result, '\n');
4162
29901626
BP
4163 rule = rule_dpif_lookup(ofproto, &flow, 0);
4164 trace_format_rule(&result, 0, 0, rule);
abe529af
BP
4165 if (rule) {
4166 struct ofproto_trace trace;
4167 struct ofpbuf *odp_actions;
4168
4169 trace.result = &result;
4170 trace.flow = flow;
876b0e1c 4171 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
abe529af
BP
4172 trace.ctx.resubmit_hook = trace_resubmit;
4173 odp_actions = xlate_actions(&trace.ctx,
4174 rule->up.actions, rule->up.n_actions);
4175
4176 ds_put_char(&result, '\n');
4177 trace_format_flow(&result, 0, "Final flow", &trace);
4178 ds_put_cstr(&result, "Datapath actions: ");
4179 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4180 ofpbuf_delete(odp_actions);
876b0e1c
BP
4181
4182 if (!trace.ctx.may_set_up_flow) {
4183 if (packet) {
4184 ds_put_cstr(&result, "\nThis flow is not cachable.");
4185 } else {
4186 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
4187 "for complete actions, please supply a packet.");
4188 }
4189 }
abe529af
BP
4190 }
4191
4192 unixctl_command_reply(conn, 200, ds_cstr(&result));
4193
4194exit:
4195 ds_destroy(&result);
876b0e1c
BP
4196 ofpbuf_delete(packet);
4197 ofpbuf_uninit(&odp_key);
abe529af
BP
4198 free(args);
4199}
4200
7ee20df1
BP
4201static void
4202ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
4203 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4204{
4205 clogged = true;
4206 unixctl_command_reply(conn, 200, NULL);
4207}
4208
4209static void
4210ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
4211 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4212{
4213 clogged = false;
4214 unixctl_command_reply(conn, 200, NULL);
4215}
4216
abe529af
BP
4217static void
4218ofproto_dpif_unixctl_init(void)
4219{
4220 static bool registered;
4221 if (registered) {
4222 return;
4223 }
4224 registered = true;
4225
4226 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4227 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
7ee20df1
BP
4228
4229 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
4230 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
abe529af
BP
4231}
4232\f
4233const struct ofproto_class ofproto_dpif_class = {
4234 enumerate_types,
4235 enumerate_names,
4236 del,
4237 alloc,
4238 construct,
4239 destruct,
4240 dealloc,
4241 run,
4242 wait,
4243 flush,
6c1491fb
BP
4244 get_features,
4245 get_tables,
abe529af
BP
4246 port_alloc,
4247 port_construct,
4248 port_destruct,
4249 port_dealloc,
4250 port_modified,
4251 port_reconfigured,
4252 port_query_by_name,
4253 port_add,
4254 port_del,
4255 port_dump_start,
4256 port_dump_next,
4257 port_dump_done,
4258 port_poll,
4259 port_poll_wait,
4260 port_is_lacp_current,
0ab6decf 4261 NULL, /* rule_choose_table */
abe529af
BP
4262 rule_alloc,
4263 rule_construct,
4264 rule_destruct,
4265 rule_dealloc,
abe529af
BP
4266 rule_get_stats,
4267 rule_execute,
4268 rule_modify_actions,
4269 get_drop_frags,
4270 set_drop_frags,
4271 packet_out,
4272 set_netflow,
4273 get_netflow_ids,
4274 set_sflow,
4275 set_cfm,
a5610457 4276 get_cfm_fault,
1de11730 4277 get_cfm_remote_mpids,
abe529af
BP
4278 bundle_set,
4279 bundle_remove,
4280 mirror_set,
4281 set_flood_vlans,
4282 is_mirror_output_bundle,
8402c74b 4283 forward_bpdu_changed,
abe529af 4284};