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