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