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