unixctl: Make dpif/dump-flows fetch kernel flows.
[sliver-openvswitch.git] / ofproto / ofproto-dpif.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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-dpif.h"
20 #include "ofproto/ofproto-provider.h"
21
22 #include <errno.h>
23
24 #include "bfd.h"
25 #include "bond.h"
26 #include "bundle.h"
27 #include "byte-order.h"
28 #include "connectivity.h"
29 #include "connmgr.h"
30 #include "coverage.h"
31 #include "cfm.h"
32 #include "dpif.h"
33 #include "dynamic-string.h"
34 #include "fail-open.h"
35 #include "guarded-list.h"
36 #include "hmapx.h"
37 #include "lacp.h"
38 #include "learn.h"
39 #include "mac-learning.h"
40 #include "meta-flow.h"
41 #include "multipath.h"
42 #include "netdev-vport.h"
43 #include "netdev.h"
44 #include "netlink.h"
45 #include "nx-match.h"
46 #include "odp-util.h"
47 #include "odp-execute.h"
48 #include "ofp-util.h"
49 #include "ofpbuf.h"
50 #include "ofp-actions.h"
51 #include "ofp-parse.h"
52 #include "ofp-print.h"
53 #include "ofproto-dpif-governor.h"
54 #include "ofproto-dpif-ipfix.h"
55 #include "ofproto-dpif-mirror.h"
56 #include "ofproto-dpif-monitor.h"
57 #include "ofproto-dpif-sflow.h"
58 #include "ofproto-dpif-upcall.h"
59 #include "ofproto-dpif-xlate.h"
60 #include "poll-loop.h"
61 #include "seq.h"
62 #include "simap.h"
63 #include "smap.h"
64 #include "timer.h"
65 #include "tunnel.h"
66 #include "unaligned.h"
67 #include "unixctl.h"
68 #include "vlan-bitmap.h"
69 #include "vlog.h"
70
71 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
72
73 COVERAGE_DEFINE(ofproto_dpif_expired);
74 COVERAGE_DEFINE(facet_revalidate);
75 COVERAGE_DEFINE(facet_unexpected);
76 COVERAGE_DEFINE(facet_create);
77 COVERAGE_DEFINE(facet_remove);
78 COVERAGE_DEFINE(subfacet_create);
79 COVERAGE_DEFINE(subfacet_destroy);
80 COVERAGE_DEFINE(subfacet_install_fail);
81 COVERAGE_DEFINE(packet_in_overflow);
82
83 /* Number of implemented OpenFlow tables. */
84 enum { N_TABLES = 255 };
85 enum { TBL_INTERNAL = N_TABLES - 1 };    /* Used for internal hidden rules. */
86 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
87
88 struct flow_miss;
89 struct facet;
90
91 struct rule_dpif {
92     struct rule up;
93
94     /* These statistics:
95      *
96      *   - Do include packets and bytes from facets that have been deleted or
97      *     whose own statistics have been folded into the rule.
98      *
99      *   - Do include packets and bytes sent "by hand" that were accounted to
100      *     the rule without any facet being involved (this is a rare corner
101      *     case in rule_execute()).
102      *
103      *   - Do not include packet or bytes that can be obtained from any facet's
104      *     packet_count or byte_count member or that can be obtained from the
105      *     datapath by, e.g., dpif_flow_get() for any subfacet.
106      */
107     struct ovs_mutex stats_mutex;
108     uint64_t packet_count OVS_GUARDED;  /* Number of packets received. */
109     uint64_t byte_count OVS_GUARDED;    /* Number of bytes received. */
110 };
111
112 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes);
113 static struct rule_dpif *rule_dpif_cast(const struct rule *);
114
115 struct group_dpif {
116     struct ofgroup up;
117
118     /* These statistics:
119      *
120      *   - Do include packets and bytes from facets that have been deleted or
121      *     whose own statistics have been folded into the rule.
122      *
123      *   - Do include packets and bytes sent "by hand" that were accounted to
124      *     the rule without any facet being involved (this is a rare corner
125      *     case in rule_execute()).
126      *
127      *   - Do not include packet or bytes that can be obtained from any facet's
128      *     packet_count or byte_count member or that can be obtained from the
129      *     datapath by, e.g., dpif_flow_get() for any subfacet.
130      */
131     struct ovs_mutex stats_mutex;
132     uint64_t packet_count OVS_GUARDED;  /* Number of packets received. */
133     uint64_t byte_count OVS_GUARDED;    /* Number of bytes received. */
134     struct bucket_counter *bucket_stats OVS_GUARDED;  /* Bucket statistics. */
135 };
136
137 struct ofbundle {
138     struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
139     struct ofproto_dpif *ofproto; /* Owning ofproto. */
140     void *aux;                  /* Key supplied by ofproto's client. */
141     char *name;                 /* Identifier for log messages. */
142
143     /* Configuration. */
144     struct list ports;          /* Contains "struct ofport"s. */
145     enum port_vlan_mode vlan_mode; /* VLAN mode */
146     int vlan;                   /* -1=trunk port, else a 12-bit VLAN ID. */
147     unsigned long *trunks;      /* Bitmap of trunked VLANs, if 'vlan' == -1.
148                                  * NULL if all VLANs are trunked. */
149     struct lacp *lacp;          /* LACP if LACP is enabled, otherwise NULL. */
150     struct bond *bond;          /* Nonnull iff more than one port. */
151     bool use_priority_tags;     /* Use 802.1p tag for frames in VLAN 0? */
152
153     /* Status. */
154     bool floodable;          /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
155 };
156
157 static void bundle_remove(struct ofport *);
158 static void bundle_update(struct ofbundle *);
159 static void bundle_destroy(struct ofbundle *);
160 static void bundle_del_port(struct ofport_dpif *);
161 static void bundle_run(struct ofbundle *);
162 static void bundle_wait(struct ofbundle *);
163
164 static void stp_run(struct ofproto_dpif *ofproto);
165 static void stp_wait(struct ofproto_dpif *ofproto);
166 static int set_stp_port(struct ofport *,
167                         const struct ofproto_port_stp_settings *);
168
169 static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
170                               enum slow_path_reason,
171                               uint64_t *stub, size_t stub_size,
172                               const struct nlattr **actionsp,
173                               size_t *actions_lenp);
174
175 /* A subfacet (see "struct subfacet" below) has three possible installation
176  * states:
177  *
178  *   - SF_NOT_INSTALLED: Not installed in the datapath.  This will only be the
179  *     case just after the subfacet is created, just before the subfacet is
180  *     destroyed, or if the datapath returns an error when we try to install a
181  *     subfacet.
182  *
183  *   - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
184  *
185  *   - SF_SLOW_PATH: An action that sends every packet for the subfacet through
186  *     ofproto_dpif is installed in the datapath.
187  */
188 enum subfacet_path {
189     SF_NOT_INSTALLED,           /* No datapath flow for this subfacet. */
190     SF_FAST_PATH,               /* Full actions are installed. */
191     SF_SLOW_PATH,               /* Send-to-userspace action is installed. */
192 };
193
194 /* A dpif flow and actions associated with a facet.
195  *
196  * See also the large comment on struct facet. */
197 struct subfacet {
198     /* Owners. */
199     struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
200     struct list list_node;      /* In struct facet's 'facets' list. */
201     struct facet *facet;        /* Owning facet. */
202     struct dpif_backer *backer; /* Owning backer. */
203
204     struct nlattr *key;
205     int key_len;
206
207     long long int used;         /* Time last used; time created if not used. */
208     long long int created;      /* Time created. */
209
210     uint64_t dp_packet_count;   /* Last known packet count in the datapath. */
211     uint64_t dp_byte_count;     /* Last known byte count in the datapath. */
212
213     enum subfacet_path path;    /* Installed in datapath? */
214 };
215
216 #define SUBFACET_DESTROY_MAX_BATCH 50
217
218 static struct subfacet *subfacet_create(struct facet *, struct flow_miss *,
219                                         uint32_t key_hash);
220 static struct subfacet *subfacet_find(struct dpif_backer *,
221                                       const struct nlattr *key, size_t key_len,
222                                       uint32_t key_hash);
223 static void subfacet_destroy(struct subfacet *);
224 static void subfacet_destroy__(struct subfacet *);
225 static void subfacet_destroy_batch(struct dpif_backer *,
226                                    struct subfacet **, int n);
227 static void subfacet_reset_dp_stats(struct subfacet *,
228                                     struct dpif_flow_stats *);
229 static void subfacet_update_stats(struct subfacet *,
230                                   const struct dpif_flow_stats *);
231 static int subfacet_install(struct subfacet *,
232                             const struct ofpbuf *odp_actions,
233                             struct dpif_flow_stats *);
234 static void subfacet_uninstall(struct subfacet *);
235
236 /* A unique, non-overlapping instantiation of an OpenFlow flow.
237  *
238  * A facet associates a "struct flow", which represents the Open vSwitch
239  * userspace idea of an exact-match flow, with one or more subfacets.
240  * While the facet is created based on an exact-match flow, it is stored
241  * within the ofproto based on the wildcards that could be expressed
242  * based on the flow table and other configuration.  (See the 'wc'
243  * description in "struct xlate_out" for more details.)
244  *
245  * Each subfacet tracks the datapath's idea of the flow equivalent to
246  * the facet.  When the kernel module (or other dpif implementation) and
247  * Open vSwitch userspace agree on the definition of a flow key, there
248  * is exactly one subfacet per facet.  If the dpif implementation
249  * supports more-specific flow matching than userspace, however, a facet
250  * can have more than one subfacet.  Examples include the dpif
251  * implementation not supporting the same wildcards as userspace or some
252  * distinction in flow that userspace simply doesn't understand.
253  *
254  * Flow expiration works in terms of subfacets, so a facet must have at
255  * least one subfacet or it will never expire, leaking memory. */
256 struct facet {
257     /* Owner. */
258     struct ofproto_dpif *ofproto;
259
260     /* Owned data. */
261     struct list subfacets;
262     long long int used;         /* Time last used; time created if not used. */
263
264     /* Key. */
265     struct flow flow;           /* Flow of the creating subfacet. */
266     struct cls_rule cr;         /* In 'ofproto_dpif's facets classifier. */
267
268     /* These statistics:
269      *
270      *   - Do include packets and bytes sent "by hand", e.g. with
271      *     dpif_execute().
272      *
273      *   - Do include packets and bytes that were obtained from the datapath
274      *     when a subfacet's statistics were reset (e.g. dpif_flow_put() with
275      *     DPIF_FP_ZERO_STATS).
276      *
277      *   - Do not include packets or bytes that can be obtained from the
278      *     datapath for any existing subfacet.
279      */
280     uint64_t packet_count;       /* Number of packets received. */
281     uint64_t byte_count;         /* Number of bytes received. */
282
283     /* Resubmit statistics. */
284     uint64_t prev_packet_count;  /* Number of packets from last stats push. */
285     uint64_t prev_byte_count;    /* Number of bytes from last stats push. */
286     long long int prev_used;     /* Used time from last stats push. */
287
288     /* Accounting. */
289     uint16_t tcp_flags;          /* TCP flags seen for this 'rule'. */
290
291     struct xlate_out xout;
292
293     /* Storage for a single subfacet, to reduce malloc() time and space
294      * overhead.  (A facet always has at least one subfacet and in the common
295      * case has exactly one subfacet.  However, 'one_subfacet' may not
296      * always be valid, since it could have been removed after newer
297      * subfacets were pushed onto the 'subfacets' list.) */
298     struct subfacet one_subfacet;
299
300     long long int learn_rl;      /* Rate limiter for facet_learn(). */
301 };
302
303 static struct facet *facet_create(const struct flow_miss *);
304 static void facet_remove(struct facet *);
305 static void facet_free(struct facet *);
306
307 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
308 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
309                                         const struct flow *);
310 static bool facet_revalidate(struct facet *);
311 static bool facet_check_consistency(struct facet *);
312
313 static void facet_flush_stats(struct facet *);
314
315 static void facet_reset_counters(struct facet *);
316 static void flow_push_stats(struct ofproto_dpif *, struct flow *,
317                             struct dpif_flow_stats *, bool may_learn);
318 static void facet_push_stats(struct facet *, bool may_learn);
319 static void facet_learn(struct facet *);
320 static void push_all_stats(void);
321
322 static bool facet_is_controller_flow(struct facet *);
323
324 struct ofport_dpif {
325     struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
326     struct ofport up;
327
328     odp_port_t odp_port;
329     struct ofbundle *bundle;    /* Bundle that contains this port, if any. */
330     struct list bundle_node;    /* In struct ofbundle's "ports" list. */
331     struct cfm *cfm;            /* Connectivity Fault Management, if any. */
332     struct bfd *bfd;            /* BFD, if any. */
333     bool may_enable;            /* May be enabled in bonds. */
334     bool is_tunnel;             /* This port is a tunnel. */
335     bool is_layer3;             /* This is a layer 3 port. */
336     long long int carrier_seq;  /* Carrier status changes. */
337     struct ofport_dpif *peer;   /* Peer if patch port. */
338
339     /* Spanning tree. */
340     struct stp_port *stp_port;  /* Spanning Tree Protocol, if any. */
341     enum stp_state stp_state;   /* Always STP_DISABLED if STP not in use. */
342     long long int stp_state_entered;
343
344     /* Queue to DSCP mapping. */
345     struct ofproto_port_queue *qdscp;
346     size_t n_qdscp;
347
348     /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
349      *
350      * This is deprecated.  It is only for compatibility with broken device
351      * drivers in old versions of Linux that do not properly support VLANs when
352      * VLAN devices are not used.  When broken device drivers are no longer in
353      * widespread use, we will delete these interfaces. */
354     ofp_port_t realdev_ofp_port;
355     int vlandev_vid;
356 };
357
358 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
359  *
360  * This is deprecated.  It is only for compatibility with broken device drivers
361  * in old versions of Linux that do not properly support VLANs when VLAN
362  * devices are not used.  When broken device drivers are no longer in
363  * widespread use, we will delete these interfaces. */
364 struct vlan_splinter {
365     struct hmap_node realdev_vid_node;
366     struct hmap_node vlandev_node;
367     ofp_port_t realdev_ofp_port;
368     ofp_port_t vlandev_ofp_port;
369     int vid;
370 };
371
372 static void vsp_remove(struct ofport_dpif *);
373 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
374
375 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
376                                        ofp_port_t);
377
378 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
379                                        odp_port_t);
380
381 static struct ofport_dpif *
382 ofport_dpif_cast(const struct ofport *ofport)
383 {
384     return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
385 }
386
387 static void port_run(struct ofport_dpif *);
388 static int set_bfd(struct ofport *, const struct smap *);
389 static int set_cfm(struct ofport *, const struct cfm_settings *);
390 static void ofport_update_peer(struct ofport_dpif *);
391
392 struct dpif_completion {
393     struct list list_node;
394     struct ofoperation *op;
395 };
396
397 /* Reasons that we might need to revalidate every facet, and corresponding
398  * coverage counters.
399  *
400  * A value of 0 means that there is no need to revalidate.
401  *
402  * It would be nice to have some cleaner way to integrate with coverage
403  * counters, but with only a few reasons I guess this is good enough for
404  * now. */
405 enum revalidate_reason {
406     REV_RECONFIGURE = 1,       /* Switch configuration changed. */
407     REV_STP,                   /* Spanning tree protocol port status change. */
408     REV_BOND,                  /* Bonding changed. */
409     REV_PORT_TOGGLED,          /* Port enabled or disabled by CFM, LACP, ...*/
410     REV_FLOW_TABLE,            /* Flow table changed. */
411     REV_MAC_LEARNING,          /* Mac learning changed. */
412     REV_INCONSISTENCY          /* Facet self-check failed. */
413 };
414 COVERAGE_DEFINE(rev_reconfigure);
415 COVERAGE_DEFINE(rev_stp);
416 COVERAGE_DEFINE(rev_bond);
417 COVERAGE_DEFINE(rev_port_toggled);
418 COVERAGE_DEFINE(rev_flow_table);
419 COVERAGE_DEFINE(rev_mac_learning);
420 COVERAGE_DEFINE(rev_inconsistency);
421
422 /* All datapaths of a given type share a single dpif backer instance. */
423 struct dpif_backer {
424     char *type;
425     int refcount;
426     struct dpif *dpif;
427     struct udpif *udpif;
428     struct timer next_expiration;
429
430     struct ovs_rwlock odp_to_ofport_lock;
431     struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
432
433     struct simap tnl_backers;      /* Set of dpif ports backing tunnels. */
434
435     /* Facet revalidation flags applying to facets which use this backer. */
436     enum revalidate_reason need_revalidate; /* Revalidate every facet. */
437
438     struct hmap drop_keys; /* Set of dropped odp keys. */
439     bool recv_set_enable; /* Enables or disables receiving packets. */
440
441     struct hmap subfacets;
442     struct governor *governor;
443
444     /* Subfacet statistics.
445      *
446      * These keep track of the total number of subfacets added and deleted and
447      * flow life span.  They are useful for computing the flow rates stats
448      * exposed via "ovs-appctl dpif/show".  The goal is to learn about
449      * traffic patterns in ways that we can use later to improve Open vSwitch
450      * performance in new situations.  */
451     unsigned max_n_subfacet;         /* Maximum number of flows */
452     unsigned avg_n_subfacet;         /* Average number of flows. */
453 };
454
455 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
456 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
457
458 static void drop_key_clear(struct dpif_backer *);
459
460 struct ofproto_dpif {
461     struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
462     struct ofproto up;
463     struct dpif_backer *backer;
464
465     /* Special OpenFlow rules. */
466     struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
467     struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
468     struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
469
470     /* Bridging. */
471     struct netflow *netflow;
472     struct dpif_sflow *sflow;
473     struct dpif_ipfix *ipfix;
474     struct hmap bundles;        /* Contains "struct ofbundle"s. */
475     struct mac_learning *ml;
476     bool has_bonded_bundles;
477     bool lacp_enabled;
478     struct mbridge *mbridge;
479
480     /* Facets. */
481     struct classifier facets;     /* Contains 'struct facet's. */
482     long long int consistency_rl;
483
484     struct ovs_mutex stats_mutex;
485     struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
486                                             * consumed in userspace. */
487
488     /* Spanning tree. */
489     struct stp *stp;
490     long long int stp_last_tick;
491
492     /* VLAN splinters. */
493     struct ovs_mutex vsp_mutex;
494     struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
495     struct hmap vlandev_map OVS_GUARDED;     /* vlandev -> (realdev,vid). */
496
497     /* Ports. */
498     struct sset ports;             /* Set of standard port names. */
499     struct sset ghost_ports;       /* Ports with no datapath port. */
500     struct sset port_poll_set;     /* Queued names for port_poll() reply. */
501     int port_poll_errno;           /* Last errno for port_poll() reply. */
502     uint64_t change_seq;           /* Connectivity status changes. */
503
504     /* Per ofproto's dpif stats. */
505     uint64_t n_hit;
506     uint64_t n_missed;
507
508     /* Work queues. */
509     struct guarded_list pins;      /* Contains "struct ofputil_packet_in"s. */
510 };
511
512 /* By default, flows in the datapath are wildcarded (megaflows).  They
513  * may be disabled with the "ovs-appctl dpif/disable-megaflows" command. */
514 static bool enable_megaflows = true;
515
516 /* All existing ofproto_dpif instances, indexed by ->up.name. */
517 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
518
519 static void ofproto_dpif_unixctl_init(void);
520
521 static inline struct ofproto_dpif *
522 ofproto_dpif_cast(const struct ofproto *ofproto)
523 {
524     ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
525     return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
526 }
527
528 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
529                                         ofp_port_t ofp_port);
530 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
531                           const struct ofpbuf *packet,
532                           const struct ofpact[], size_t ofpacts_len,
533                           struct ds *);
534
535 /* Upcalls. */
536 static void handle_upcalls(struct dpif_backer *);
537
538 /* Flow expiration. */
539 static int expire(struct dpif_backer *);
540
541 /* Global variables. */
542 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
543
544 /* Initial mappings of port to bridge mappings. */
545 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
546
547 /* Executes 'fm'.  The caller retains ownership of 'fm' and everything in
548  * it. */
549 void
550 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
551                       struct ofputil_flow_mod *fm)
552 {
553     ofproto_flow_mod(&ofproto->up, fm);
554 }
555
556 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
557  * Takes ownership of 'pin' and pin->packet. */
558 void
559 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
560                             struct ofproto_packet_in *pin)
561 {
562     if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
563         COVERAGE_INC(packet_in_overflow);
564         free(CONST_CAST(void *, pin->up.packet));
565         free(pin);
566     }
567 }
568 \f
569 /* Factory functions. */
570
571 static void
572 init(const struct shash *iface_hints)
573 {
574     struct shash_node *node;
575
576     /* Make a local copy, since we don't own 'iface_hints' elements. */
577     SHASH_FOR_EACH(node, iface_hints) {
578         const struct iface_hint *orig_hint = node->data;
579         struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
580
581         new_hint->br_name = xstrdup(orig_hint->br_name);
582         new_hint->br_type = xstrdup(orig_hint->br_type);
583         new_hint->ofp_port = orig_hint->ofp_port;
584
585         shash_add(&init_ofp_ports, node->name, new_hint);
586     }
587 }
588
589 static void
590 enumerate_types(struct sset *types)
591 {
592     dp_enumerate_types(types);
593 }
594
595 static int
596 enumerate_names(const char *type, struct sset *names)
597 {
598     struct ofproto_dpif *ofproto;
599
600     sset_clear(names);
601     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
602         if (strcmp(type, ofproto->up.type)) {
603             continue;
604         }
605         sset_add(names, ofproto->up.name);
606     }
607
608     return 0;
609 }
610
611 static int
612 del(const char *type, const char *name)
613 {
614     struct dpif *dpif;
615     int error;
616
617     error = dpif_open(name, type, &dpif);
618     if (!error) {
619         error = dpif_delete(dpif);
620         dpif_close(dpif);
621     }
622     return error;
623 }
624 \f
625 static const char *
626 port_open_type(const char *datapath_type, const char *port_type)
627 {
628     return dpif_port_open_type(datapath_type, port_type);
629 }
630
631 /* Type functions. */
632
633 static void process_dpif_port_changes(struct dpif_backer *);
634 static void process_dpif_all_ports_changed(struct dpif_backer *);
635 static void process_dpif_port_change(struct dpif_backer *,
636                                      const char *devname);
637 static void process_dpif_port_error(struct dpif_backer *, int error);
638
639 static struct ofproto_dpif *
640 lookup_ofproto_dpif_by_port_name(const char *name)
641 {
642     struct ofproto_dpif *ofproto;
643
644     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
645         if (sset_contains(&ofproto->ports, name)) {
646             return ofproto;
647         }
648     }
649
650     return NULL;
651 }
652
653 static int
654 type_run(const char *type)
655 {
656     static long long int push_timer = LLONG_MIN;
657     struct dpif_backer *backer;
658
659     backer = shash_find_data(&all_dpif_backers, type);
660     if (!backer) {
661         /* This is not necessarily a problem, since backers are only
662          * created on demand. */
663         return 0;
664     }
665
666     dpif_run(backer->dpif);
667
668     handle_upcalls(backer);
669
670     /* The most natural place to push facet statistics is when they're pulled
671      * from the datapath.  However, when there are many flows in the datapath,
672      * this expensive operation can occur so frequently, that it reduces our
673      * ability to quickly set up flows.  To reduce the cost, we push statistics
674      * here instead. */
675     if (time_msec() > push_timer) {
676         push_timer = time_msec() + 2000;
677         push_all_stats();
678     }
679
680     /* If vswitchd started with other_config:flow_restore_wait set as "true",
681      * and the configuration has now changed to "false", enable receiving
682      * packets from the datapath. */
683     if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
684         int error;
685
686         backer->recv_set_enable = true;
687
688         error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
689         if (error) {
690             VLOG_ERR("Failed to enable receiving packets in dpif.");
691             return error;
692         }
693         dpif_flow_flush(backer->dpif);
694         backer->need_revalidate = REV_RECONFIGURE;
695     }
696
697     if (backer->recv_set_enable) {
698         udpif_set_threads(backer->udpif, n_handlers);
699     }
700
701     if (backer->need_revalidate) {
702         struct ofproto_dpif *ofproto;
703         struct simap_node *node;
704         struct simap tmp_backers;
705
706         /* Handle tunnel garbage collection. */
707         simap_init(&tmp_backers);
708         simap_swap(&backer->tnl_backers, &tmp_backers);
709
710         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
711             struct ofport_dpif *iter;
712
713             if (backer != ofproto->backer) {
714                 continue;
715             }
716
717             HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
718                 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
719                 const char *dp_port;
720
721                 if (!iter->is_tunnel) {
722                     continue;
723                 }
724
725                 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
726                                                      namebuf, sizeof namebuf);
727                 node = simap_find(&tmp_backers, dp_port);
728                 if (node) {
729                     simap_put(&backer->tnl_backers, dp_port, node->data);
730                     simap_delete(&tmp_backers, node);
731                     node = simap_find(&backer->tnl_backers, dp_port);
732                 } else {
733                     node = simap_find(&backer->tnl_backers, dp_port);
734                     if (!node) {
735                         odp_port_t odp_port = ODPP_NONE;
736
737                         if (!dpif_port_add(backer->dpif, iter->up.netdev,
738                                            &odp_port)) {
739                             simap_put(&backer->tnl_backers, dp_port,
740                                       odp_to_u32(odp_port));
741                             node = simap_find(&backer->tnl_backers, dp_port);
742                         }
743                     }
744                 }
745
746                 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
747                 if (tnl_port_reconfigure(iter, iter->up.netdev,
748                                          iter->odp_port)) {
749                     backer->need_revalidate = REV_RECONFIGURE;
750                 }
751             }
752         }
753
754         SIMAP_FOR_EACH (node, &tmp_backers) {
755             dpif_port_del(backer->dpif, u32_to_odp(node->data));
756         }
757         simap_destroy(&tmp_backers);
758
759         switch (backer->need_revalidate) {
760         case REV_RECONFIGURE:   COVERAGE_INC(rev_reconfigure);   break;
761         case REV_STP:           COVERAGE_INC(rev_stp);           break;
762         case REV_BOND:          COVERAGE_INC(rev_bond);          break;
763         case REV_PORT_TOGGLED:  COVERAGE_INC(rev_port_toggled);  break;
764         case REV_FLOW_TABLE:    COVERAGE_INC(rev_flow_table);    break;
765         case REV_MAC_LEARNING:  COVERAGE_INC(rev_mac_learning);  break;
766         case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
767         }
768         backer->need_revalidate = 0;
769
770         /* Clear the drop_keys in case we should now be accepting some
771          * formerly dropped flows. */
772         drop_key_clear(backer);
773
774         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
775             struct facet *facet, *next;
776             struct ofport_dpif *ofport;
777             struct cls_cursor cursor;
778             struct ofbundle *bundle;
779
780             if (ofproto->backer != backer) {
781                 continue;
782             }
783
784             ovs_rwlock_wrlock(&xlate_rwlock);
785             xlate_ofproto_set(ofproto, ofproto->up.name,
786                               ofproto->backer->dpif, ofproto->miss_rule,
787                               ofproto->no_packet_in_rule, ofproto->ml,
788                               ofproto->stp, ofproto->mbridge,
789                               ofproto->sflow, ofproto->ipfix,
790                               ofproto->netflow, ofproto->up.frag_handling,
791                               ofproto->up.forward_bpdu,
792                               connmgr_has_in_band(ofproto->up.connmgr));
793
794             HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
795                 xlate_bundle_set(ofproto, bundle, bundle->name,
796                                  bundle->vlan_mode, bundle->vlan,
797                                  bundle->trunks, bundle->use_priority_tags,
798                                  bundle->bond, bundle->lacp,
799                                  bundle->floodable);
800             }
801
802             HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
803                 int stp_port = ofport->stp_port
804                     ? stp_port_no(ofport->stp_port)
805                     : -1;
806                 xlate_ofport_set(ofproto, ofport->bundle, ofport,
807                                  ofport->up.ofp_port, ofport->odp_port,
808                                  ofport->up.netdev, ofport->cfm,
809                                  ofport->bfd, ofport->peer, stp_port,
810                                  ofport->qdscp, ofport->n_qdscp,
811                                  ofport->up.pp.config, ofport->up.pp.state,
812                                  ofport->is_tunnel, ofport->may_enable);
813             }
814             ovs_rwlock_unlock(&xlate_rwlock);
815
816             /* Only ofproto-dpif cares about the facet classifier so we just
817              * lock cls_cursor_init() to appease the thread safety analysis. */
818             ovs_rwlock_rdlock(&ofproto->facets.rwlock);
819             cls_cursor_init(&cursor, &ofproto->facets, NULL);
820             ovs_rwlock_unlock(&ofproto->facets.rwlock);
821             CLS_CURSOR_FOR_EACH_SAFE (facet, next, cr, &cursor) {
822                 facet_revalidate(facet);
823             }
824         }
825
826         udpif_revalidate(backer->udpif);
827     }
828
829     if (!backer->recv_set_enable) {
830         /* Wake up before a max of 1000ms. */
831         timer_set_duration(&backer->next_expiration, 1000);
832     } else if (timer_expired(&backer->next_expiration)) {
833         int delay = expire(backer);
834         timer_set_duration(&backer->next_expiration, delay);
835     }
836
837     process_dpif_port_changes(backer);
838
839     if (backer->governor) {
840         size_t n_subfacets;
841
842         governor_run(backer->governor);
843
844         /* If the governor has shrunk to its minimum size and the number of
845          * subfacets has dwindled, then drop the governor entirely.
846          *
847          * For hysteresis, the number of subfacets to drop the governor is
848          * smaller than the number needed to trigger its creation. */
849         n_subfacets = hmap_count(&backer->subfacets);
850         if (n_subfacets * 4 < flow_eviction_threshold
851             && governor_is_idle(backer->governor)) {
852             governor_destroy(backer->governor);
853             backer->governor = NULL;
854         }
855     }
856
857     return 0;
858 }
859
860 /* Check for and handle port changes in 'backer''s dpif. */
861 static void
862 process_dpif_port_changes(struct dpif_backer *backer)
863 {
864     for (;;) {
865         char *devname;
866         int error;
867
868         error = dpif_port_poll(backer->dpif, &devname);
869         switch (error) {
870         case EAGAIN:
871             return;
872
873         case ENOBUFS:
874             process_dpif_all_ports_changed(backer);
875             break;
876
877         case 0:
878             process_dpif_port_change(backer, devname);
879             free(devname);
880             break;
881
882         default:
883             process_dpif_port_error(backer, error);
884             break;
885         }
886     }
887 }
888
889 static void
890 process_dpif_all_ports_changed(struct dpif_backer *backer)
891 {
892     struct ofproto_dpif *ofproto;
893     struct dpif_port dpif_port;
894     struct dpif_port_dump dump;
895     struct sset devnames;
896     const char *devname;
897
898     sset_init(&devnames);
899     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
900         if (ofproto->backer == backer) {
901             struct ofport *ofport;
902
903             HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
904                 sset_add(&devnames, netdev_get_name(ofport->netdev));
905             }
906         }
907     }
908     DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
909         sset_add(&devnames, dpif_port.name);
910     }
911
912     SSET_FOR_EACH (devname, &devnames) {
913         process_dpif_port_change(backer, devname);
914     }
915     sset_destroy(&devnames);
916 }
917
918 static void
919 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
920 {
921     struct ofproto_dpif *ofproto;
922     struct dpif_port port;
923
924     /* Don't report on the datapath's device. */
925     if (!strcmp(devname, dpif_base_name(backer->dpif))) {
926         return;
927     }
928
929     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
930                    &all_ofproto_dpifs) {
931         if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
932             return;
933         }
934     }
935
936     ofproto = lookup_ofproto_dpif_by_port_name(devname);
937     if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
938         /* The port was removed.  If we know the datapath,
939          * report it through poll_set().  If we don't, it may be
940          * notifying us of a removal we initiated, so ignore it.
941          * If there's a pending ENOBUFS, let it stand, since
942          * everything will be reevaluated. */
943         if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
944             sset_add(&ofproto->port_poll_set, devname);
945             ofproto->port_poll_errno = 0;
946         }
947     } else if (!ofproto) {
948         /* The port was added, but we don't know with which
949          * ofproto we should associate it.  Delete it. */
950         dpif_port_del(backer->dpif, port.port_no);
951     } else {
952         struct ofport_dpif *ofport;
953
954         ofport = ofport_dpif_cast(shash_find_data(
955                                       &ofproto->up.port_by_name, devname));
956         if (ofport
957             && ofport->odp_port != port.port_no
958             && !odp_port_to_ofport(backer, port.port_no))
959         {
960             /* 'ofport''s datapath port number has changed from
961              * 'ofport->odp_port' to 'port.port_no'.  Update our internal data
962              * structures to match. */
963             ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
964             hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
965             ofport->odp_port = port.port_no;
966             hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
967                         hash_odp_port(port.port_no));
968             ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
969             backer->need_revalidate = REV_RECONFIGURE;
970         }
971     }
972     dpif_port_destroy(&port);
973 }
974
975 /* Propagate 'error' to all ofprotos based on 'backer'. */
976 static void
977 process_dpif_port_error(struct dpif_backer *backer, int error)
978 {
979     struct ofproto_dpif *ofproto;
980
981     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
982         if (ofproto->backer == backer) {
983             sset_clear(&ofproto->port_poll_set);
984             ofproto->port_poll_errno = error;
985         }
986     }
987 }
988
989 static void
990 type_wait(const char *type)
991 {
992     struct dpif_backer *backer;
993
994     backer = shash_find_data(&all_dpif_backers, type);
995     if (!backer) {
996         /* This is not necessarily a problem, since backers are only
997          * created on demand. */
998         return;
999     }
1000
1001     if (backer->governor) {
1002         governor_wait(backer->governor);
1003     }
1004
1005     timer_wait(&backer->next_expiration);
1006     dpif_wait(backer->dpif);
1007     udpif_wait(backer->udpif);
1008 }
1009 \f
1010 /* Basic life-cycle. */
1011
1012 static int add_internal_flows(struct ofproto_dpif *);
1013
1014 static struct ofproto *
1015 alloc(void)
1016 {
1017     struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
1018     return &ofproto->up;
1019 }
1020
1021 static void
1022 dealloc(struct ofproto *ofproto_)
1023 {
1024     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1025     free(ofproto);
1026 }
1027
1028 static void
1029 close_dpif_backer(struct dpif_backer *backer)
1030 {
1031     ovs_assert(backer->refcount > 0);
1032
1033     if (--backer->refcount) {
1034         return;
1035     }
1036
1037     drop_key_clear(backer);
1038     hmap_destroy(&backer->drop_keys);
1039
1040     udpif_destroy(backer->udpif);
1041
1042     simap_destroy(&backer->tnl_backers);
1043     ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
1044     hmap_destroy(&backer->odp_to_ofport_map);
1045     shash_find_and_delete(&all_dpif_backers, backer->type);
1046     free(backer->type);
1047     dpif_close(backer->dpif);
1048
1049     ovs_assert(hmap_is_empty(&backer->subfacets));
1050     hmap_destroy(&backer->subfacets);
1051     governor_destroy(backer->governor);
1052
1053     free(backer);
1054 }
1055
1056 /* Datapath port slated for removal from datapath. */
1057 struct odp_garbage {
1058     struct list list_node;
1059     odp_port_t odp_port;
1060 };
1061
1062 static int
1063 open_dpif_backer(const char *type, struct dpif_backer **backerp)
1064 {
1065     struct dpif_backer *backer;
1066     struct dpif_port_dump port_dump;
1067     struct dpif_port port;
1068     struct shash_node *node;
1069     struct list garbage_list;
1070     struct odp_garbage *garbage, *next;
1071     struct sset names;
1072     char *backer_name;
1073     const char *name;
1074     int error;
1075
1076     backer = shash_find_data(&all_dpif_backers, type);
1077     if (backer) {
1078         backer->refcount++;
1079         *backerp = backer;
1080         return 0;
1081     }
1082
1083     backer_name = xasprintf("ovs-%s", type);
1084
1085     /* Remove any existing datapaths, since we assume we're the only
1086      * userspace controlling the datapath. */
1087     sset_init(&names);
1088     dp_enumerate_names(type, &names);
1089     SSET_FOR_EACH(name, &names) {
1090         struct dpif *old_dpif;
1091
1092         /* Don't remove our backer if it exists. */
1093         if (!strcmp(name, backer_name)) {
1094             continue;
1095         }
1096
1097         if (dpif_open(name, type, &old_dpif)) {
1098             VLOG_WARN("couldn't open old datapath %s to remove it", name);
1099         } else {
1100             dpif_delete(old_dpif);
1101             dpif_close(old_dpif);
1102         }
1103     }
1104     sset_destroy(&names);
1105
1106     backer = xmalloc(sizeof *backer);
1107
1108     error = dpif_create_and_open(backer_name, type, &backer->dpif);
1109     free(backer_name);
1110     if (error) {
1111         VLOG_ERR("failed to open datapath of type %s: %s", type,
1112                  ovs_strerror(error));
1113         free(backer);
1114         return error;
1115     }
1116     backer->udpif = udpif_create(backer, backer->dpif);
1117
1118     backer->type = xstrdup(type);
1119     backer->governor = NULL;
1120     backer->refcount = 1;
1121     hmap_init(&backer->odp_to_ofport_map);
1122     ovs_rwlock_init(&backer->odp_to_ofport_lock);
1123     hmap_init(&backer->drop_keys);
1124     hmap_init(&backer->subfacets);
1125     timer_set_duration(&backer->next_expiration, 1000);
1126     backer->need_revalidate = 0;
1127     simap_init(&backer->tnl_backers);
1128     backer->recv_set_enable = !ofproto_get_flow_restore_wait();
1129     *backerp = backer;
1130
1131     if (backer->recv_set_enable) {
1132         dpif_flow_flush(backer->dpif);
1133     }
1134
1135     /* Loop through the ports already on the datapath and remove any
1136      * that we don't need anymore. */
1137     list_init(&garbage_list);
1138     dpif_port_dump_start(&port_dump, backer->dpif);
1139     while (dpif_port_dump_next(&port_dump, &port)) {
1140         node = shash_find(&init_ofp_ports, port.name);
1141         if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
1142             garbage = xmalloc(sizeof *garbage);
1143             garbage->odp_port = port.port_no;
1144             list_push_front(&garbage_list, &garbage->list_node);
1145         }
1146     }
1147     dpif_port_dump_done(&port_dump);
1148
1149     LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
1150         dpif_port_del(backer->dpif, garbage->odp_port);
1151         list_remove(&garbage->list_node);
1152         free(garbage);
1153     }
1154
1155     shash_add(&all_dpif_backers, type, backer);
1156
1157     error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
1158     if (error) {
1159         VLOG_ERR("failed to listen on datapath of type %s: %s",
1160                  type, ovs_strerror(error));
1161         close_dpif_backer(backer);
1162         return error;
1163     }
1164
1165     if (backer->recv_set_enable) {
1166         udpif_set_threads(backer->udpif, n_handlers);
1167     }
1168
1169     backer->max_n_subfacet = 0;
1170     backer->avg_n_subfacet = 0;
1171
1172     return error;
1173 }
1174
1175 static int
1176 construct(struct ofproto *ofproto_)
1177 {
1178     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1179     struct shash_node *node, *next;
1180     int error;
1181
1182     error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1183     if (error) {
1184         return error;
1185     }
1186
1187     ofproto->netflow = NULL;
1188     ofproto->sflow = NULL;
1189     ofproto->ipfix = NULL;
1190     ofproto->stp = NULL;
1191     hmap_init(&ofproto->bundles);
1192     ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1193     ofproto->mbridge = mbridge_create();
1194     ofproto->has_bonded_bundles = false;
1195     ofproto->lacp_enabled = false;
1196     ovs_mutex_init(&ofproto->stats_mutex);
1197     ovs_mutex_init(&ofproto->vsp_mutex);
1198
1199     classifier_init(&ofproto->facets, NULL);
1200     ofproto->consistency_rl = LLONG_MIN;
1201
1202     guarded_list_init(&ofproto->pins);
1203
1204     ofproto_dpif_unixctl_init();
1205
1206     hmap_init(&ofproto->vlandev_map);
1207     hmap_init(&ofproto->realdev_vid_map);
1208
1209     sset_init(&ofproto->ports);
1210     sset_init(&ofproto->ghost_ports);
1211     sset_init(&ofproto->port_poll_set);
1212     ofproto->port_poll_errno = 0;
1213     ofproto->change_seq = 0;
1214
1215     SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1216         struct iface_hint *iface_hint = node->data;
1217
1218         if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1219             /* Check if the datapath already has this port. */
1220             if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1221                 sset_add(&ofproto->ports, node->name);
1222             }
1223
1224             free(iface_hint->br_name);
1225             free(iface_hint->br_type);
1226             free(iface_hint);
1227             shash_delete(&init_ofp_ports, node);
1228         }
1229     }
1230
1231     hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1232                 hash_string(ofproto->up.name, 0));
1233     memset(&ofproto->stats, 0, sizeof ofproto->stats);
1234
1235     ofproto_init_tables(ofproto_, N_TABLES);
1236     error = add_internal_flows(ofproto);
1237     ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1238
1239     ofproto->n_hit = 0;
1240     ofproto->n_missed = 0;
1241
1242     return error;
1243 }
1244
1245 static int
1246 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1247                   const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1248 {
1249     struct ofputil_flow_mod fm;
1250     int error;
1251
1252     match_init_catchall(&fm.match);
1253     fm.priority = 0;
1254     match_set_reg(&fm.match, 0, id);
1255     fm.new_cookie = htonll(0);
1256     fm.cookie = htonll(0);
1257     fm.cookie_mask = htonll(0);
1258     fm.modify_cookie = false;
1259     fm.table_id = TBL_INTERNAL;
1260     fm.command = OFPFC_ADD;
1261     fm.idle_timeout = 0;
1262     fm.hard_timeout = 0;
1263     fm.buffer_id = 0;
1264     fm.out_port = 0;
1265     fm.flags = 0;
1266     fm.ofpacts = ofpacts->data;
1267     fm.ofpacts_len = ofpacts->size;
1268
1269     error = ofproto_flow_mod(&ofproto->up, &fm);
1270     if (error) {
1271         VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1272                     id, ofperr_to_string(error));
1273         return error;
1274     }
1275
1276     if (rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL, TBL_INTERNAL,
1277                                   rulep)) {
1278         rule_dpif_unref(*rulep);
1279     } else {
1280         OVS_NOT_REACHED();
1281     }
1282
1283     return 0;
1284 }
1285
1286 static int
1287 add_internal_flows(struct ofproto_dpif *ofproto)
1288 {
1289     struct ofpact_controller *controller;
1290     uint64_t ofpacts_stub[128 / 8];
1291     struct ofpbuf ofpacts;
1292     int error;
1293     int id;
1294
1295     ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1296     id = 1;
1297
1298     controller = ofpact_put_CONTROLLER(&ofpacts);
1299     controller->max_len = UINT16_MAX;
1300     controller->controller_id = 0;
1301     controller->reason = OFPR_NO_MATCH;
1302     ofpact_pad(&ofpacts);
1303
1304     error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1305     if (error) {
1306         return error;
1307     }
1308
1309     ofpbuf_clear(&ofpacts);
1310     error = add_internal_flow(ofproto, id++, &ofpacts,
1311                               &ofproto->no_packet_in_rule);
1312     if (error) {
1313         return error;
1314     }
1315
1316     error = add_internal_flow(ofproto, id++, &ofpacts,
1317                               &ofproto->drop_frags_rule);
1318     return error;
1319 }
1320
1321 static void
1322 destruct(struct ofproto *ofproto_)
1323 {
1324     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1325     struct rule_dpif *rule, *next_rule;
1326     struct ofproto_packet_in *pin, *next_pin;
1327     struct facet *facet, *next_facet;
1328     struct cls_cursor cursor;
1329     struct oftable *table;
1330     struct list pins;
1331
1332     ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1333     cls_cursor_init(&cursor, &ofproto->facets, NULL);
1334     ovs_rwlock_unlock(&ofproto->facets.rwlock);
1335     CLS_CURSOR_FOR_EACH_SAFE (facet, next_facet, cr, &cursor) {
1336         facet_remove(facet);
1337     }
1338
1339     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1340     ovs_rwlock_wrlock(&xlate_rwlock);
1341     xlate_remove_ofproto(ofproto);
1342     ovs_rwlock_unlock(&xlate_rwlock);
1343
1344     /* Discard any flow_miss_batches queued up for 'ofproto', avoiding a
1345      * use-after-free error. */
1346     udpif_revalidate(ofproto->backer->udpif);
1347
1348     hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1349
1350     OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1351         struct cls_cursor cursor;
1352
1353         ovs_rwlock_rdlock(&table->cls.rwlock);
1354         cls_cursor_init(&cursor, &table->cls, NULL);
1355         ovs_rwlock_unlock(&table->cls.rwlock);
1356         CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1357             ofproto_rule_delete(&ofproto->up, &rule->up);
1358         }
1359     }
1360
1361     guarded_list_pop_all(&ofproto->pins, &pins);
1362     LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1363         list_remove(&pin->list_node);
1364         free(CONST_CAST(void *, pin->up.packet));
1365         free(pin);
1366     }
1367     guarded_list_destroy(&ofproto->pins);
1368
1369     mbridge_unref(ofproto->mbridge);
1370
1371     netflow_unref(ofproto->netflow);
1372     dpif_sflow_unref(ofproto->sflow);
1373     hmap_destroy(&ofproto->bundles);
1374     mac_learning_unref(ofproto->ml);
1375
1376     classifier_destroy(&ofproto->facets);
1377
1378     hmap_destroy(&ofproto->vlandev_map);
1379     hmap_destroy(&ofproto->realdev_vid_map);
1380
1381     sset_destroy(&ofproto->ports);
1382     sset_destroy(&ofproto->ghost_ports);
1383     sset_destroy(&ofproto->port_poll_set);
1384
1385     ovs_mutex_destroy(&ofproto->stats_mutex);
1386     ovs_mutex_destroy(&ofproto->vsp_mutex);
1387
1388     close_dpif_backer(ofproto->backer);
1389 }
1390
1391 static int
1392 run(struct ofproto *ofproto_)
1393 {
1394     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1395     uint64_t new_seq;
1396
1397     if (mbridge_need_revalidate(ofproto->mbridge)) {
1398         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1399         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1400         mac_learning_flush(ofproto->ml);
1401         ovs_rwlock_unlock(&ofproto->ml->rwlock);
1402     }
1403
1404     /* Do not perform any periodic activity required by 'ofproto' while
1405      * waiting for flow restore to complete. */
1406     if (!ofproto_get_flow_restore_wait()) {
1407         struct ofproto_packet_in *pin, *next_pin;
1408         struct list pins;
1409
1410         guarded_list_pop_all(&ofproto->pins, &pins);
1411         LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1412             connmgr_send_packet_in(ofproto->up.connmgr, pin);
1413             list_remove(&pin->list_node);
1414             free(CONST_CAST(void *, pin->up.packet));
1415             free(pin);
1416         }
1417     }
1418
1419     if (ofproto->netflow) {
1420         netflow_run(ofproto->netflow);
1421     }
1422     if (ofproto->sflow) {
1423         dpif_sflow_run(ofproto->sflow);
1424     }
1425     if (ofproto->ipfix) {
1426         dpif_ipfix_run(ofproto->ipfix);
1427     }
1428
1429     new_seq = seq_read(connectivity_seq_get());
1430     if (ofproto->change_seq != new_seq) {
1431         struct ofport_dpif *ofport;
1432
1433         HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1434             port_run(ofport);
1435         }
1436
1437         ofproto->change_seq = new_seq;
1438     }
1439     if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1440         struct ofbundle *bundle;
1441
1442         HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1443             bundle_run(bundle);
1444         }
1445     }
1446
1447     stp_run(ofproto);
1448     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1449     if (mac_learning_run(ofproto->ml)) {
1450         ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1451     }
1452     ovs_rwlock_unlock(&ofproto->ml->rwlock);
1453
1454     /* Check the consistency of a random facet, to aid debugging. */
1455     ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1456     if (time_msec() >= ofproto->consistency_rl
1457         && !classifier_is_empty(&ofproto->facets)
1458         && !ofproto->backer->need_revalidate) {
1459         struct cls_subtable *table;
1460         struct cls_rule *cr;
1461         struct facet *facet;
1462
1463         ofproto->consistency_rl = time_msec() + 250;
1464
1465         table = CONTAINER_OF(hmap_random_node(&ofproto->facets.subtables),
1466                              struct cls_subtable, hmap_node);
1467         cr = CONTAINER_OF(hmap_random_node(&table->rules), struct cls_rule,
1468                           hmap_node);
1469         facet = CONTAINER_OF(cr, struct facet, cr);
1470
1471         if (!facet_check_consistency(facet)) {
1472             ofproto->backer->need_revalidate = REV_INCONSISTENCY;
1473         }
1474     }
1475     ovs_rwlock_unlock(&ofproto->facets.rwlock);
1476
1477     return 0;
1478 }
1479
1480 static void
1481 wait(struct ofproto *ofproto_)
1482 {
1483     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1484
1485     if (ofproto_get_flow_restore_wait()) {
1486         return;
1487     }
1488
1489     if (ofproto->sflow) {
1490         dpif_sflow_wait(ofproto->sflow);
1491     }
1492     if (ofproto->ipfix) {
1493         dpif_ipfix_wait(ofproto->ipfix);
1494     }
1495     if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1496         struct ofbundle *bundle;
1497
1498         HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1499             bundle_wait(bundle);
1500         }
1501     }
1502     if (ofproto->netflow) {
1503         netflow_wait(ofproto->netflow);
1504     }
1505     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1506     mac_learning_wait(ofproto->ml);
1507     ovs_rwlock_unlock(&ofproto->ml->rwlock);
1508     stp_wait(ofproto);
1509     if (ofproto->backer->need_revalidate) {
1510         /* Shouldn't happen, but if it does just go around again. */
1511         VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1512         poll_immediate_wake();
1513     }
1514 }
1515
1516 static void
1517 get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1518 {
1519     const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1520     struct cls_cursor cursor;
1521     size_t n_subfacets = 0;
1522     struct facet *facet;
1523
1524     ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1525     simap_increase(usage, "facets", classifier_count(&ofproto->facets));
1526     ovs_rwlock_unlock(&ofproto->facets.rwlock);
1527
1528     ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1529     cls_cursor_init(&cursor, &ofproto->facets, NULL);
1530     CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
1531         n_subfacets += list_size(&facet->subfacets);
1532     }
1533     ovs_rwlock_unlock(&ofproto->facets.rwlock);
1534     simap_increase(usage, "subfacets", n_subfacets);
1535 }
1536
1537 static void
1538 type_get_memory_usage(const char *type, struct simap *usage)
1539 {
1540     struct dpif_backer *backer;
1541
1542     backer = shash_find_data(&all_dpif_backers, type);
1543     if (backer) {
1544         udpif_get_memory_usage(backer->udpif, usage);
1545     }
1546 }
1547
1548 static void
1549 flush(struct ofproto *ofproto_)
1550 {
1551     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1552     struct subfacet *subfacet, *next_subfacet;
1553     struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
1554     int n_batch;
1555
1556     n_batch = 0;
1557     HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
1558                         &ofproto->backer->subfacets) {
1559         if (subfacet->facet->ofproto != ofproto) {
1560             continue;
1561         }
1562
1563         if (subfacet->path != SF_NOT_INSTALLED) {
1564             batch[n_batch++] = subfacet;
1565             if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
1566                 subfacet_destroy_batch(ofproto->backer, batch, n_batch);
1567                 n_batch = 0;
1568             }
1569         } else {
1570             subfacet_destroy(subfacet);
1571         }
1572     }
1573
1574     if (n_batch > 0) {
1575         subfacet_destroy_batch(ofproto->backer, batch, n_batch);
1576     }
1577 }
1578
1579 static void
1580 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1581              bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1582 {
1583     *arp_match_ip = true;
1584     *actions = (OFPUTIL_A_OUTPUT |
1585                 OFPUTIL_A_SET_VLAN_VID |
1586                 OFPUTIL_A_SET_VLAN_PCP |
1587                 OFPUTIL_A_STRIP_VLAN |
1588                 OFPUTIL_A_SET_DL_SRC |
1589                 OFPUTIL_A_SET_DL_DST |
1590                 OFPUTIL_A_SET_NW_SRC |
1591                 OFPUTIL_A_SET_NW_DST |
1592                 OFPUTIL_A_SET_NW_TOS |
1593                 OFPUTIL_A_SET_TP_SRC |
1594                 OFPUTIL_A_SET_TP_DST |
1595                 OFPUTIL_A_ENQUEUE);
1596 }
1597
1598 static void
1599 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1600 {
1601     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1602     struct dpif_dp_stats s;
1603     uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1604     uint64_t n_lookup;
1605
1606     strcpy(ots->name, "classifier");
1607
1608     dpif_get_dp_stats(ofproto->backer->dpif, &s);
1609     rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes);
1610     rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes);
1611     rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes);
1612
1613     n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1614     ots->lookup_count = htonll(n_lookup);
1615     ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1616 }
1617
1618 static struct ofport *
1619 port_alloc(void)
1620 {
1621     struct ofport_dpif *port = xmalloc(sizeof *port);
1622     return &port->up;
1623 }
1624
1625 static void
1626 port_dealloc(struct ofport *port_)
1627 {
1628     struct ofport_dpif *port = ofport_dpif_cast(port_);
1629     free(port);
1630 }
1631
1632 static int
1633 port_construct(struct ofport *port_)
1634 {
1635     struct ofport_dpif *port = ofport_dpif_cast(port_);
1636     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1637     const struct netdev *netdev = port->up.netdev;
1638     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1639     struct dpif_port dpif_port;
1640     int error;
1641
1642     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1643     port->bundle = NULL;
1644     port->cfm = NULL;
1645     port->bfd = NULL;
1646     port->may_enable = true;
1647     port->stp_port = NULL;
1648     port->stp_state = STP_DISABLED;
1649     port->is_tunnel = false;
1650     port->peer = NULL;
1651     port->qdscp = NULL;
1652     port->n_qdscp = 0;
1653     port->realdev_ofp_port = 0;
1654     port->vlandev_vid = 0;
1655     port->carrier_seq = netdev_get_carrier_resets(netdev);
1656     port->is_layer3 = netdev_vport_is_layer3(netdev);
1657
1658     if (netdev_vport_is_patch(netdev)) {
1659         /* By bailing out here, we don't submit the port to the sFlow module
1660          * to be considered for counter polling export.  This is correct
1661          * because the patch port represents an interface that sFlow considers
1662          * to be "internal" to the switch as a whole, and therefore not an
1663          * candidate for counter polling. */
1664         port->odp_port = ODPP_NONE;
1665         ofport_update_peer(port);
1666         return 0;
1667     }
1668
1669     error = dpif_port_query_by_name(ofproto->backer->dpif,
1670                                     netdev_vport_get_dpif_port(netdev, namebuf,
1671                                                                sizeof namebuf),
1672                                     &dpif_port);
1673     if (error) {
1674         return error;
1675     }
1676
1677     port->odp_port = dpif_port.port_no;
1678
1679     if (netdev_get_tunnel_config(netdev)) {
1680         tnl_port_add(port, port->up.netdev, port->odp_port);
1681         port->is_tunnel = true;
1682     } else {
1683         /* Sanity-check that a mapping doesn't already exist.  This
1684          * shouldn't happen for non-tunnel ports. */
1685         if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1686             VLOG_ERR("port %s already has an OpenFlow port number",
1687                      dpif_port.name);
1688             dpif_port_destroy(&dpif_port);
1689             return EBUSY;
1690         }
1691
1692         ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1693         hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1694                     hash_odp_port(port->odp_port));
1695         ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1696     }
1697     dpif_port_destroy(&dpif_port);
1698
1699     if (ofproto->sflow) {
1700         dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1701     }
1702
1703     return 0;
1704 }
1705
1706 static void
1707 port_destruct(struct ofport *port_)
1708 {
1709     struct ofport_dpif *port = ofport_dpif_cast(port_);
1710     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1711     const char *devname = netdev_get_name(port->up.netdev);
1712     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1713     const char *dp_port_name;
1714
1715     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1716     ovs_rwlock_wrlock(&xlate_rwlock);
1717     xlate_ofport_remove(port);
1718     ovs_rwlock_unlock(&xlate_rwlock);
1719
1720     dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1721                                               sizeof namebuf);
1722     if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1723         /* The underlying device is still there, so delete it.  This
1724          * happens when the ofproto is being destroyed, since the caller
1725          * assumes that removal of attached ports will happen as part of
1726          * destruction. */
1727         if (!port->is_tunnel) {
1728             dpif_port_del(ofproto->backer->dpif, port->odp_port);
1729         }
1730     }
1731
1732     if (port->peer) {
1733         port->peer->peer = NULL;
1734         port->peer = NULL;
1735     }
1736
1737     if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1738         ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1739         hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1740         ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1741     }
1742
1743     tnl_port_del(port);
1744     sset_find_and_delete(&ofproto->ports, devname);
1745     sset_find_and_delete(&ofproto->ghost_ports, devname);
1746     bundle_remove(port_);
1747     set_cfm(port_, NULL);
1748     set_bfd(port_, NULL);
1749     if (ofproto->sflow) {
1750         dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1751     }
1752
1753     free(port->qdscp);
1754 }
1755
1756 static void
1757 port_modified(struct ofport *port_)
1758 {
1759     struct ofport_dpif *port = ofport_dpif_cast(port_);
1760
1761     if (port->bundle && port->bundle->bond) {
1762         bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1763     }
1764
1765     if (port->cfm) {
1766         cfm_set_netdev(port->cfm, port->up.netdev);
1767     }
1768
1769     if (port->bfd) {
1770         bfd_set_netdev(port->bfd, port->up.netdev);
1771     }
1772
1773     ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1774                                      port->up.pp.hw_addr);
1775
1776     if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1777                                                 port->odp_port)) {
1778         ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1779             REV_RECONFIGURE;
1780     }
1781
1782     ofport_update_peer(port);
1783 }
1784
1785 static void
1786 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1787 {
1788     struct ofport_dpif *port = ofport_dpif_cast(port_);
1789     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1790     enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1791
1792     if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1793                    OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1794                    OFPUTIL_PC_NO_PACKET_IN)) {
1795         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1796
1797         if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1798             bundle_update(port->bundle);
1799         }
1800     }
1801 }
1802
1803 static int
1804 set_sflow(struct ofproto *ofproto_,
1805           const struct ofproto_sflow_options *sflow_options)
1806 {
1807     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1808     struct dpif_sflow *ds = ofproto->sflow;
1809
1810     if (sflow_options) {
1811         if (!ds) {
1812             struct ofport_dpif *ofport;
1813
1814             ds = ofproto->sflow = dpif_sflow_create();
1815             HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1816                 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1817             }
1818             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1819         }
1820         dpif_sflow_set_options(ds, sflow_options);
1821     } else {
1822         if (ds) {
1823             dpif_sflow_unref(ds);
1824             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1825             ofproto->sflow = NULL;
1826         }
1827     }
1828     return 0;
1829 }
1830
1831 static int
1832 set_ipfix(
1833     struct ofproto *ofproto_,
1834     const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1835     const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1836     size_t n_flow_exporters_options)
1837 {
1838     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1839     struct dpif_ipfix *di = ofproto->ipfix;
1840     bool has_options = bridge_exporter_options || flow_exporters_options;
1841
1842     if (has_options && !di) {
1843         di = ofproto->ipfix = dpif_ipfix_create();
1844     }
1845
1846     if (di) {
1847         /* Call set_options in any case to cleanly flush the flow
1848          * caches in the last exporters that are to be destroyed. */
1849         dpif_ipfix_set_options(
1850             di, bridge_exporter_options, flow_exporters_options,
1851             n_flow_exporters_options);
1852
1853         if (!has_options) {
1854             dpif_ipfix_unref(di);
1855             ofproto->ipfix = NULL;
1856         }
1857     }
1858
1859     return 0;
1860 }
1861
1862 static int
1863 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1864 {
1865     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1866     int error = 0;
1867
1868     if (s) {
1869         if (!ofport->cfm) {
1870             struct ofproto_dpif *ofproto;
1871
1872             ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1873             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1874             ofport->cfm = cfm_create(ofport->up.netdev);
1875         }
1876
1877         if (cfm_configure(ofport->cfm, s)) {
1878             error = 0;
1879             goto out;
1880         }
1881
1882         error = EINVAL;
1883     }
1884     cfm_unref(ofport->cfm);
1885     ofport->cfm = NULL;
1886 out:
1887     ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1888                                      ofport->up.pp.hw_addr);
1889     return error;
1890 }
1891
1892 static bool
1893 get_cfm_status(const struct ofport *ofport_,
1894                struct ofproto_cfm_status *status)
1895 {
1896     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1897
1898     if (ofport->cfm) {
1899         status->faults = cfm_get_fault(ofport->cfm);
1900         status->flap_count = cfm_get_flap_count(ofport->cfm);
1901         status->remote_opstate = cfm_get_opup(ofport->cfm);
1902         status->health = cfm_get_health(ofport->cfm);
1903         cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1904         return true;
1905     } else {
1906         return false;
1907     }
1908 }
1909
1910 static int
1911 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1912 {
1913     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1914     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1915     struct bfd *old;
1916
1917     old = ofport->bfd;
1918     ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1919                                 cfg, ofport->up.netdev);
1920     if (ofport->bfd != old) {
1921         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1922     }
1923     ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1924                                      ofport->up.pp.hw_addr);
1925     return 0;
1926 }
1927
1928 static int
1929 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1930 {
1931     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1932
1933     if (ofport->bfd) {
1934         bfd_get_status(ofport->bfd, smap);
1935         return 0;
1936     } else {
1937         return ENOENT;
1938     }
1939 }
1940 \f
1941 /* Spanning Tree. */
1942
1943 static void
1944 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1945 {
1946     struct ofproto_dpif *ofproto = ofproto_;
1947     struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1948     struct ofport_dpif *ofport;
1949
1950     ofport = stp_port_get_aux(sp);
1951     if (!ofport) {
1952         VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1953                      ofproto->up.name, port_num);
1954     } else {
1955         struct eth_header *eth = pkt->l2;
1956
1957         netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1958         if (eth_addr_is_zero(eth->eth_src)) {
1959             VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1960                          "with unknown MAC", ofproto->up.name, port_num);
1961         } else {
1962             ofproto_dpif_send_packet(ofport, pkt);
1963         }
1964     }
1965     ofpbuf_delete(pkt);
1966 }
1967
1968 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1969 static int
1970 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1971 {
1972     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1973
1974     /* Only revalidate flows if the configuration changed. */
1975     if (!s != !ofproto->stp) {
1976         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1977     }
1978
1979     if (s) {
1980         if (!ofproto->stp) {
1981             ofproto->stp = stp_create(ofproto_->name, s->system_id,
1982                                       send_bpdu_cb, ofproto);
1983             ofproto->stp_last_tick = time_msec();
1984         }
1985
1986         stp_set_bridge_id(ofproto->stp, s->system_id);
1987         stp_set_bridge_priority(ofproto->stp, s->priority);
1988         stp_set_hello_time(ofproto->stp, s->hello_time);
1989         stp_set_max_age(ofproto->stp, s->max_age);
1990         stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1991     }  else {
1992         struct ofport *ofport;
1993
1994         HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1995             set_stp_port(ofport, NULL);
1996         }
1997
1998         stp_unref(ofproto->stp);
1999         ofproto->stp = NULL;
2000     }
2001
2002     return 0;
2003 }
2004
2005 static int
2006 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2007 {
2008     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2009
2010     if (ofproto->stp) {
2011         s->enabled = true;
2012         s->bridge_id = stp_get_bridge_id(ofproto->stp);
2013         s->designated_root = stp_get_designated_root(ofproto->stp);
2014         s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2015     } else {
2016         s->enabled = false;
2017     }
2018
2019     return 0;
2020 }
2021
2022 static void
2023 update_stp_port_state(struct ofport_dpif *ofport)
2024 {
2025     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2026     enum stp_state state;
2027
2028     /* Figure out new state. */
2029     state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2030                              : STP_DISABLED;
2031
2032     /* Update state. */
2033     if (ofport->stp_state != state) {
2034         enum ofputil_port_state of_state;
2035         bool fwd_change;
2036
2037         VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
2038                     netdev_get_name(ofport->up.netdev),
2039                     stp_state_name(ofport->stp_state),
2040                     stp_state_name(state));
2041         if (stp_learn_in_state(ofport->stp_state)
2042                 != stp_learn_in_state(state)) {
2043             /* xxx Learning action flows should also be flushed. */
2044             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2045             mac_learning_flush(ofproto->ml);
2046             ovs_rwlock_unlock(&ofproto->ml->rwlock);
2047         }
2048         fwd_change = stp_forward_in_state(ofport->stp_state)
2049                         != stp_forward_in_state(state);
2050
2051         ofproto->backer->need_revalidate = REV_STP;
2052         ofport->stp_state = state;
2053         ofport->stp_state_entered = time_msec();
2054
2055         if (fwd_change && ofport->bundle) {
2056             bundle_update(ofport->bundle);
2057         }
2058
2059         /* Update the STP state bits in the OpenFlow port description. */
2060         of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2061         of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2062                      : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2063                      : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2064                      : state == STP_BLOCKING ?  OFPUTIL_PS_STP_BLOCK
2065                      : 0);
2066         ofproto_port_set_state(&ofport->up, of_state);
2067     }
2068 }
2069
2070 /* Configures STP on 'ofport_' using the settings defined in 's'.  The
2071  * caller is responsible for assigning STP port numbers and ensuring
2072  * there are no duplicates. */
2073 static int
2074 set_stp_port(struct ofport *ofport_,
2075              const struct ofproto_port_stp_settings *s)
2076 {
2077     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2078     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2079     struct stp_port *sp = ofport->stp_port;
2080
2081     if (!s || !s->enable) {
2082         if (sp) {
2083             ofport->stp_port = NULL;
2084             stp_port_disable(sp);
2085             update_stp_port_state(ofport);
2086         }
2087         return 0;
2088     } else if (sp && stp_port_no(sp) != s->port_num
2089             && ofport == stp_port_get_aux(sp)) {
2090         /* The port-id changed, so disable the old one if it's not
2091          * already in use by another port. */
2092         stp_port_disable(sp);
2093     }
2094
2095     sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2096     stp_port_enable(sp);
2097
2098     stp_port_set_aux(sp, ofport);
2099     stp_port_set_priority(sp, s->priority);
2100     stp_port_set_path_cost(sp, s->path_cost);
2101
2102     update_stp_port_state(ofport);
2103
2104     return 0;
2105 }
2106
2107 static int
2108 get_stp_port_status(struct ofport *ofport_,
2109                     struct ofproto_port_stp_status *s)
2110 {
2111     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2112     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2113     struct stp_port *sp = ofport->stp_port;
2114
2115     if (!ofproto->stp || !sp) {
2116         s->enabled = false;
2117         return 0;
2118     }
2119
2120     s->enabled = true;
2121     s->port_id = stp_port_get_id(sp);
2122     s->state = stp_port_get_state(sp);
2123     s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2124     s->role = stp_port_get_role(sp);
2125
2126     return 0;
2127 }
2128
2129 static int
2130 get_stp_port_stats(struct ofport *ofport_,
2131                    struct ofproto_port_stp_stats *s)
2132 {
2133     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2134     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2135     struct stp_port *sp = ofport->stp_port;
2136
2137     if (!ofproto->stp || !sp) {
2138         s->enabled = false;
2139         return 0;
2140     }
2141
2142     s->enabled = true;
2143     stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2144
2145     return 0;
2146 }
2147
2148 static void
2149 stp_run(struct ofproto_dpif *ofproto)
2150 {
2151     if (ofproto->stp) {
2152         long long int now = time_msec();
2153         long long int elapsed = now - ofproto->stp_last_tick;
2154         struct stp_port *sp;
2155
2156         if (elapsed > 0) {
2157             stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2158             ofproto->stp_last_tick = now;
2159         }
2160         while (stp_get_changed_port(ofproto->stp, &sp)) {
2161             struct ofport_dpif *ofport = stp_port_get_aux(sp);
2162
2163             if (ofport) {
2164                 update_stp_port_state(ofport);
2165             }
2166         }
2167
2168         if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2169             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2170             mac_learning_flush(ofproto->ml);
2171             ovs_rwlock_unlock(&ofproto->ml->rwlock);
2172         }
2173     }
2174 }
2175
2176 static void
2177 stp_wait(struct ofproto_dpif *ofproto)
2178 {
2179     if (ofproto->stp) {
2180         poll_timer_wait(1000);
2181     }
2182 }
2183 \f
2184 static int
2185 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2186            size_t n_qdscp)
2187 {
2188     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2189     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2190
2191     if (ofport->n_qdscp != n_qdscp
2192         || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2193                               n_qdscp * sizeof *qdscp))) {
2194         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2195         free(ofport->qdscp);
2196         ofport->qdscp = n_qdscp
2197             ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2198             : NULL;
2199         ofport->n_qdscp = n_qdscp;
2200     }
2201
2202     return 0;
2203 }
2204 \f
2205 /* Bundles. */
2206
2207 /* Expires all MAC learning entries associated with 'bundle' and forces its
2208  * ofproto to revalidate every flow.
2209  *
2210  * Normally MAC learning entries are removed only from the ofproto associated
2211  * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2212  * are removed from every ofproto.  When patch ports and SLB bonds are in use
2213  * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2214  * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2215  * with the host from which it migrated. */
2216 static void
2217 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2218 {
2219     struct ofproto_dpif *ofproto = bundle->ofproto;
2220     struct mac_learning *ml = ofproto->ml;
2221     struct mac_entry *mac, *next_mac;
2222
2223     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2224     ovs_rwlock_wrlock(&ml->rwlock);
2225     LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2226         if (mac->port.p == bundle) {
2227             if (all_ofprotos) {
2228                 struct ofproto_dpif *o;
2229
2230                 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2231                     if (o != ofproto) {
2232                         struct mac_entry *e;
2233
2234                         ovs_rwlock_wrlock(&o->ml->rwlock);
2235                         e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2236                         if (e) {
2237                             mac_learning_expire(o->ml, e);
2238                         }
2239                         ovs_rwlock_unlock(&o->ml->rwlock);
2240                     }
2241                 }
2242             }
2243
2244             mac_learning_expire(ml, mac);
2245         }
2246     }
2247     ovs_rwlock_unlock(&ml->rwlock);
2248 }
2249
2250 static struct ofbundle *
2251 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2252 {
2253     struct ofbundle *bundle;
2254
2255     HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2256                              &ofproto->bundles) {
2257         if (bundle->aux == aux) {
2258             return bundle;
2259         }
2260     }
2261     return NULL;
2262 }
2263
2264 static void
2265 bundle_update(struct ofbundle *bundle)
2266 {
2267     struct ofport_dpif *port;
2268
2269     bundle->floodable = true;
2270     LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2271         if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2272             || port->is_layer3
2273             || !stp_forward_in_state(port->stp_state)) {
2274             bundle->floodable = false;
2275             break;
2276         }
2277     }
2278 }
2279
2280 static void
2281 bundle_del_port(struct ofport_dpif *port)
2282 {
2283     struct ofbundle *bundle = port->bundle;
2284
2285     bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2286
2287     list_remove(&port->bundle_node);
2288     port->bundle = NULL;
2289
2290     if (bundle->lacp) {
2291         lacp_slave_unregister(bundle->lacp, port);
2292     }
2293     if (bundle->bond) {
2294         bond_slave_unregister(bundle->bond, port);
2295     }
2296
2297     bundle_update(bundle);
2298 }
2299
2300 static bool
2301 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2302                 struct lacp_slave_settings *lacp)
2303 {
2304     struct ofport_dpif *port;
2305
2306     port = get_ofp_port(bundle->ofproto, ofp_port);
2307     if (!port) {
2308         return false;
2309     }
2310
2311     if (port->bundle != bundle) {
2312         bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2313         if (port->bundle) {
2314             bundle_remove(&port->up);
2315         }
2316
2317         port->bundle = bundle;
2318         list_push_back(&bundle->ports, &port->bundle_node);
2319         if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2320             || port->is_layer3
2321             || !stp_forward_in_state(port->stp_state)) {
2322             bundle->floodable = false;
2323         }
2324     }
2325     if (lacp) {
2326         bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2327         lacp_slave_register(bundle->lacp, port, lacp);
2328     }
2329
2330     return true;
2331 }
2332
2333 static void
2334 bundle_destroy(struct ofbundle *bundle)
2335 {
2336     struct ofproto_dpif *ofproto;
2337     struct ofport_dpif *port, *next_port;
2338
2339     if (!bundle) {
2340         return;
2341     }
2342
2343     ofproto = bundle->ofproto;
2344     mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2345
2346     ovs_rwlock_wrlock(&xlate_rwlock);
2347     xlate_bundle_remove(bundle);
2348     ovs_rwlock_unlock(&xlate_rwlock);
2349
2350     LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2351         bundle_del_port(port);
2352     }
2353
2354     bundle_flush_macs(bundle, true);
2355     hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2356     free(bundle->name);
2357     free(bundle->trunks);
2358     lacp_unref(bundle->lacp);
2359     bond_unref(bundle->bond);
2360     free(bundle);
2361 }
2362
2363 static int
2364 bundle_set(struct ofproto *ofproto_, void *aux,
2365            const struct ofproto_bundle_settings *s)
2366 {
2367     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2368     bool need_flush = false;
2369     struct ofport_dpif *port;
2370     struct ofbundle *bundle;
2371     unsigned long *trunks;
2372     int vlan;
2373     size_t i;
2374     bool ok;
2375
2376     if (!s) {
2377         bundle_destroy(bundle_lookup(ofproto, aux));
2378         return 0;
2379     }
2380
2381     ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2382     ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2383
2384     bundle = bundle_lookup(ofproto, aux);
2385     if (!bundle) {
2386         bundle = xmalloc(sizeof *bundle);
2387
2388         bundle->ofproto = ofproto;
2389         hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2390                     hash_pointer(aux, 0));
2391         bundle->aux = aux;
2392         bundle->name = NULL;
2393
2394         list_init(&bundle->ports);
2395         bundle->vlan_mode = PORT_VLAN_TRUNK;
2396         bundle->vlan = -1;
2397         bundle->trunks = NULL;
2398         bundle->use_priority_tags = s->use_priority_tags;
2399         bundle->lacp = NULL;
2400         bundle->bond = NULL;
2401
2402         bundle->floodable = true;
2403         mbridge_register_bundle(ofproto->mbridge, bundle);
2404     }
2405
2406     if (!bundle->name || strcmp(s->name, bundle->name)) {
2407         free(bundle->name);
2408         bundle->name = xstrdup(s->name);
2409     }
2410
2411     /* LACP. */
2412     if (s->lacp) {
2413         ofproto->lacp_enabled = true;
2414         if (!bundle->lacp) {
2415             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2416             bundle->lacp = lacp_create();
2417         }
2418         lacp_configure(bundle->lacp, s->lacp);
2419     } else {
2420         lacp_unref(bundle->lacp);
2421         bundle->lacp = NULL;
2422     }
2423
2424     /* Update set of ports. */
2425     ok = true;
2426     for (i = 0; i < s->n_slaves; i++) {
2427         if (!bundle_add_port(bundle, s->slaves[i],
2428                              s->lacp ? &s->lacp_slaves[i] : NULL)) {
2429             ok = false;
2430         }
2431     }
2432     if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2433         struct ofport_dpif *next_port;
2434
2435         LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2436             for (i = 0; i < s->n_slaves; i++) {
2437                 if (s->slaves[i] == port->up.ofp_port) {
2438                     goto found;
2439                 }
2440             }
2441
2442             bundle_del_port(port);
2443         found: ;
2444         }
2445     }
2446     ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2447
2448     if (list_is_empty(&bundle->ports)) {
2449         bundle_destroy(bundle);
2450         return EINVAL;
2451     }
2452
2453     /* Set VLAN tagging mode */
2454     if (s->vlan_mode != bundle->vlan_mode
2455         || s->use_priority_tags != bundle->use_priority_tags) {
2456         bundle->vlan_mode = s->vlan_mode;
2457         bundle->use_priority_tags = s->use_priority_tags;
2458         need_flush = true;
2459     }
2460
2461     /* Set VLAN tag. */
2462     vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2463             : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2464             : 0);
2465     if (vlan != bundle->vlan) {
2466         bundle->vlan = vlan;
2467         need_flush = true;
2468     }
2469
2470     /* Get trunked VLANs. */
2471     switch (s->vlan_mode) {
2472     case PORT_VLAN_ACCESS:
2473         trunks = NULL;
2474         break;
2475
2476     case PORT_VLAN_TRUNK:
2477         trunks = CONST_CAST(unsigned long *, s->trunks);
2478         break;
2479
2480     case PORT_VLAN_NATIVE_UNTAGGED:
2481     case PORT_VLAN_NATIVE_TAGGED:
2482         if (vlan != 0 && (!s->trunks
2483                           || !bitmap_is_set(s->trunks, vlan)
2484                           || bitmap_is_set(s->trunks, 0))) {
2485             /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2486             if (s->trunks) {
2487                 trunks = bitmap_clone(s->trunks, 4096);
2488             } else {
2489                 trunks = bitmap_allocate1(4096);
2490             }
2491             bitmap_set1(trunks, vlan);
2492             bitmap_set0(trunks, 0);
2493         } else {
2494             trunks = CONST_CAST(unsigned long *, s->trunks);
2495         }
2496         break;
2497
2498     default:
2499         OVS_NOT_REACHED();
2500     }
2501     if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2502         free(bundle->trunks);
2503         if (trunks == s->trunks) {
2504             bundle->trunks = vlan_bitmap_clone(trunks);
2505         } else {
2506             bundle->trunks = trunks;
2507             trunks = NULL;
2508         }
2509         need_flush = true;
2510     }
2511     if (trunks != s->trunks) {
2512         free(trunks);
2513     }
2514
2515     /* Bonding. */
2516     if (!list_is_short(&bundle->ports)) {
2517         bundle->ofproto->has_bonded_bundles = true;
2518         if (bundle->bond) {
2519             if (bond_reconfigure(bundle->bond, s->bond)) {
2520                 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2521             }
2522         } else {
2523             bundle->bond = bond_create(s->bond);
2524             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2525         }
2526
2527         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2528             bond_slave_register(bundle->bond, port, port->up.netdev);
2529         }
2530     } else {
2531         bond_unref(bundle->bond);
2532         bundle->bond = NULL;
2533     }
2534
2535     /* If we changed something that would affect MAC learning, un-learn
2536      * everything on this port and force flow revalidation. */
2537     if (need_flush) {
2538         bundle_flush_macs(bundle, false);
2539     }
2540
2541     return 0;
2542 }
2543
2544 static void
2545 bundle_remove(struct ofport *port_)
2546 {
2547     struct ofport_dpif *port = ofport_dpif_cast(port_);
2548     struct ofbundle *bundle = port->bundle;
2549
2550     if (bundle) {
2551         bundle_del_port(port);
2552         if (list_is_empty(&bundle->ports)) {
2553             bundle_destroy(bundle);
2554         } else if (list_is_short(&bundle->ports)) {
2555             bond_unref(bundle->bond);
2556             bundle->bond = NULL;
2557         }
2558     }
2559 }
2560
2561 static void
2562 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2563 {
2564     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2565     struct ofport_dpif *port = port_;
2566     uint8_t ea[ETH_ADDR_LEN];
2567     int error;
2568
2569     error = netdev_get_etheraddr(port->up.netdev, ea);
2570     if (!error) {
2571         struct ofpbuf packet;
2572         void *packet_pdu;
2573
2574         ofpbuf_init(&packet, 0);
2575         packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2576                                  pdu_size);
2577         memcpy(packet_pdu, pdu, pdu_size);
2578
2579         ofproto_dpif_send_packet(port, &packet);
2580         ofpbuf_uninit(&packet);
2581     } else {
2582         VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2583                     "%s (%s)", port->bundle->name,
2584                     netdev_get_name(port->up.netdev), ovs_strerror(error));
2585     }
2586 }
2587
2588 static void
2589 bundle_send_learning_packets(struct ofbundle *bundle)
2590 {
2591     struct ofproto_dpif *ofproto = bundle->ofproto;
2592     struct ofpbuf *learning_packet;
2593     int error, n_packets, n_errors;
2594     struct mac_entry *e;
2595     struct list packets;
2596
2597     list_init(&packets);
2598     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2599     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2600         if (e->port.p != bundle) {
2601             void *port_void;
2602
2603             learning_packet = bond_compose_learning_packet(bundle->bond,
2604                                                            e->mac, e->vlan,
2605                                                            &port_void);
2606             learning_packet->private_p = port_void;
2607             list_push_back(&packets, &learning_packet->list_node);
2608         }
2609     }
2610     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2611
2612     error = n_packets = n_errors = 0;
2613     LIST_FOR_EACH (learning_packet, list_node, &packets) {
2614         int ret;
2615
2616         ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2617         if (ret) {
2618             error = ret;
2619             n_errors++;
2620         }
2621         n_packets++;
2622     }
2623     ofpbuf_list_delete(&packets);
2624
2625     if (n_errors) {
2626         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2627         VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2628                      "packets, last error was: %s",
2629                      bundle->name, n_errors, n_packets, ovs_strerror(error));
2630     } else {
2631         VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2632                  bundle->name, n_packets);
2633     }
2634 }
2635
2636 static void
2637 bundle_run(struct ofbundle *bundle)
2638 {
2639     if (bundle->lacp) {
2640         lacp_run(bundle->lacp, send_pdu_cb);
2641     }
2642     if (bundle->bond) {
2643         struct ofport_dpif *port;
2644
2645         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2646             bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2647         }
2648
2649         if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2650             bundle->ofproto->backer->need_revalidate = REV_BOND;
2651         }
2652
2653         if (bond_should_send_learning_packets(bundle->bond)) {
2654             bundle_send_learning_packets(bundle);
2655         }
2656     }
2657 }
2658
2659 static void
2660 bundle_wait(struct ofbundle *bundle)
2661 {
2662     if (bundle->lacp) {
2663         lacp_wait(bundle->lacp);
2664     }
2665     if (bundle->bond) {
2666         bond_wait(bundle->bond);
2667     }
2668 }
2669 \f
2670 /* Mirrors. */
2671
2672 static int
2673 mirror_set__(struct ofproto *ofproto_, void *aux,
2674              const struct ofproto_mirror_settings *s)
2675 {
2676     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2677     struct ofbundle **srcs, **dsts;
2678     int error;
2679     size_t i;
2680
2681     if (!s) {
2682         mirror_destroy(ofproto->mbridge, aux);
2683         return 0;
2684     }
2685
2686     srcs = xmalloc(s->n_srcs * sizeof *srcs);
2687     dsts = xmalloc(s->n_dsts * sizeof *dsts);
2688
2689     for (i = 0; i < s->n_srcs; i++) {
2690         srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2691     }
2692
2693     for (i = 0; i < s->n_dsts; i++) {
2694         dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2695     }
2696
2697     error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2698                        s->n_dsts, s->src_vlans,
2699                        bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2700     free(srcs);
2701     free(dsts);
2702     return error;
2703 }
2704
2705 static int
2706 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2707                    uint64_t *packets, uint64_t *bytes)
2708 {
2709     push_all_stats();
2710     return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2711                             bytes);
2712 }
2713
2714 static int
2715 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2716 {
2717     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2718     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2719     if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2720         mac_learning_flush(ofproto->ml);
2721     }
2722     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2723     return 0;
2724 }
2725
2726 static bool
2727 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2728 {
2729     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2730     struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2731     return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2732 }
2733
2734 static void
2735 forward_bpdu_changed(struct ofproto *ofproto_)
2736 {
2737     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2738     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2739 }
2740
2741 static void
2742 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2743                      size_t max_entries)
2744 {
2745     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2746     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2747     mac_learning_set_idle_time(ofproto->ml, idle_time);
2748     mac_learning_set_max_entries(ofproto->ml, max_entries);
2749     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2750 }
2751 \f
2752 /* Ports. */
2753
2754 static struct ofport_dpif *
2755 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2756 {
2757     struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2758     return ofport ? ofport_dpif_cast(ofport) : NULL;
2759 }
2760
2761 static void
2762 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2763                             struct ofproto_port *ofproto_port,
2764                             struct dpif_port *dpif_port)
2765 {
2766     ofproto_port->name = dpif_port->name;
2767     ofproto_port->type = dpif_port->type;
2768     ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2769 }
2770
2771 static void
2772 ofport_update_peer(struct ofport_dpif *ofport)
2773 {
2774     const struct ofproto_dpif *ofproto;
2775     struct dpif_backer *backer;
2776     char *peer_name;
2777
2778     if (!netdev_vport_is_patch(ofport->up.netdev)) {
2779         return;
2780     }
2781
2782     backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2783     backer->need_revalidate = REV_RECONFIGURE;
2784
2785     if (ofport->peer) {
2786         ofport->peer->peer = NULL;
2787         ofport->peer = NULL;
2788     }
2789
2790     peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2791     if (!peer_name) {
2792         return;
2793     }
2794
2795     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2796         struct ofport *peer_ofport;
2797         struct ofport_dpif *peer;
2798         char *peer_peer;
2799
2800         if (ofproto->backer != backer) {
2801             continue;
2802         }
2803
2804         peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2805         if (!peer_ofport) {
2806             continue;
2807         }
2808
2809         peer = ofport_dpif_cast(peer_ofport);
2810         peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2811         if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2812                                  peer_peer)) {
2813             ofport->peer = peer;
2814             ofport->peer->peer = ofport;
2815         }
2816         free(peer_peer);
2817
2818         break;
2819     }
2820     free(peer_name);
2821 }
2822
2823 static void
2824 port_run(struct ofport_dpif *ofport)
2825 {
2826     long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2827     bool carrier_changed = carrier_seq != ofport->carrier_seq;
2828     bool enable = netdev_get_carrier(ofport->up.netdev);
2829     bool cfm_enable = false;
2830     bool bfd_enable = false;
2831
2832     ofport->carrier_seq = carrier_seq;
2833
2834     if (ofport->cfm) {
2835         int cfm_opup = cfm_get_opup(ofport->cfm);
2836
2837         cfm_enable = !cfm_get_fault(ofport->cfm);
2838
2839         if (cfm_opup >= 0) {
2840             cfm_enable = cfm_enable && cfm_opup;
2841         }
2842     }
2843
2844     if (ofport->bfd) {
2845         bfd_enable = bfd_forwarding(ofport->bfd);
2846     }
2847
2848     if (ofport->bfd || ofport->cfm) {
2849         enable = enable && (cfm_enable || bfd_enable);
2850     }
2851
2852     if (ofport->bundle) {
2853         enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2854         if (carrier_changed) {
2855             lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2856         }
2857     }
2858
2859     if (ofport->may_enable != enable) {
2860         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2861         ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2862     }
2863
2864     ofport->may_enable = enable;
2865 }
2866
2867 static int
2868 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2869                    struct ofproto_port *ofproto_port)
2870 {
2871     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2872     struct dpif_port dpif_port;
2873     int error;
2874
2875     if (sset_contains(&ofproto->ghost_ports, devname)) {
2876         const char *type = netdev_get_type_from_name(devname);
2877
2878         /* We may be called before ofproto->up.port_by_name is populated with
2879          * the appropriate ofport.  For this reason, we must get the name and
2880          * type from the netdev layer directly. */
2881         if (type) {
2882             const struct ofport *ofport;
2883
2884             ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2885             ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2886             ofproto_port->name = xstrdup(devname);
2887             ofproto_port->type = xstrdup(type);
2888             return 0;
2889         }
2890         return ENODEV;
2891     }
2892
2893     if (!sset_contains(&ofproto->ports, devname)) {
2894         return ENODEV;
2895     }
2896     error = dpif_port_query_by_name(ofproto->backer->dpif,
2897                                     devname, &dpif_port);
2898     if (!error) {
2899         ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2900     }
2901     return error;
2902 }
2903
2904 static int
2905 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2906 {
2907     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2908     const char *devname = netdev_get_name(netdev);
2909     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2910     const char *dp_port_name;
2911
2912     if (netdev_vport_is_patch(netdev)) {
2913         sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2914         return 0;
2915     }
2916
2917     dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2918     if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2919         odp_port_t port_no = ODPP_NONE;
2920         int error;
2921
2922         error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2923         if (error) {
2924             return error;
2925         }
2926         if (netdev_get_tunnel_config(netdev)) {
2927             simap_put(&ofproto->backer->tnl_backers,
2928                       dp_port_name, odp_to_u32(port_no));
2929         }
2930     }
2931
2932     if (netdev_get_tunnel_config(netdev)) {
2933         sset_add(&ofproto->ghost_ports, devname);
2934     } else {
2935         sset_add(&ofproto->ports, devname);
2936     }
2937     return 0;
2938 }
2939
2940 static int
2941 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2942 {
2943     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2944     struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2945     int error = 0;
2946
2947     if (!ofport) {
2948         return 0;
2949     }
2950
2951     sset_find_and_delete(&ofproto->ghost_ports,
2952                          netdev_get_name(ofport->up.netdev));
2953     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2954     if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2955         error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2956         if (!error) {
2957             /* The caller is going to close ofport->up.netdev.  If this is a
2958              * bonded port, then the bond is using that netdev, so remove it
2959              * from the bond.  The client will need to reconfigure everything
2960              * after deleting ports, so then the slave will get re-added. */
2961             bundle_remove(&ofport->up);
2962         }
2963     }
2964     return error;
2965 }
2966
2967 static int
2968 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2969 {
2970     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2971     int error;
2972
2973     push_all_stats();
2974
2975     error = netdev_get_stats(ofport->up.netdev, stats);
2976
2977     if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2978         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2979
2980         ovs_mutex_lock(&ofproto->stats_mutex);
2981         /* ofproto->stats.tx_packets represents packets that we created
2982          * internally and sent to some port (e.g. packets sent with
2983          * ofproto_dpif_send_packet()).  Account for them as if they had
2984          * come from OFPP_LOCAL and got forwarded. */
2985
2986         if (stats->rx_packets != UINT64_MAX) {
2987             stats->rx_packets += ofproto->stats.tx_packets;
2988         }
2989
2990         if (stats->rx_bytes != UINT64_MAX) {
2991             stats->rx_bytes += ofproto->stats.tx_bytes;
2992         }
2993
2994         /* ofproto->stats.rx_packets represents packets that were received on
2995          * some port and we processed internally and dropped (e.g. STP).
2996          * Account for them as if they had been forwarded to OFPP_LOCAL. */
2997
2998         if (stats->tx_packets != UINT64_MAX) {
2999             stats->tx_packets += ofproto->stats.rx_packets;
3000         }
3001
3002         if (stats->tx_bytes != UINT64_MAX) {
3003             stats->tx_bytes += ofproto->stats.rx_bytes;
3004         }
3005         ovs_mutex_unlock(&ofproto->stats_mutex);
3006     }
3007
3008     return error;
3009 }
3010
3011 struct port_dump_state {
3012     uint32_t bucket;
3013     uint32_t offset;
3014     bool ghost;
3015
3016     struct ofproto_port port;
3017     bool has_port;
3018 };
3019
3020 static int
3021 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3022 {
3023     *statep = xzalloc(sizeof(struct port_dump_state));
3024     return 0;
3025 }
3026
3027 static int
3028 port_dump_next(const struct ofproto *ofproto_, void *state_,
3029                struct ofproto_port *port)
3030 {
3031     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3032     struct port_dump_state *state = state_;
3033     const struct sset *sset;
3034     struct sset_node *node;
3035
3036     if (state->has_port) {
3037         ofproto_port_destroy(&state->port);
3038         state->has_port = false;
3039     }
3040     sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3041     while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3042         int error;
3043
3044         error = port_query_by_name(ofproto_, node->name, &state->port);
3045         if (!error) {
3046             *port = state->port;
3047             state->has_port = true;
3048             return 0;
3049         } else if (error != ENODEV) {
3050             return error;
3051         }
3052     }
3053
3054     if (!state->ghost) {
3055         state->ghost = true;
3056         state->bucket = 0;
3057         state->offset = 0;
3058         return port_dump_next(ofproto_, state_, port);
3059     }
3060
3061     return EOF;
3062 }
3063
3064 static int
3065 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3066 {
3067     struct port_dump_state *state = state_;
3068
3069     if (state->has_port) {
3070         ofproto_port_destroy(&state->port);
3071     }
3072     free(state);
3073     return 0;
3074 }
3075
3076 static int
3077 port_poll(const struct ofproto *ofproto_, char **devnamep)
3078 {
3079     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3080
3081     if (ofproto->port_poll_errno) {
3082         int error = ofproto->port_poll_errno;
3083         ofproto->port_poll_errno = 0;
3084         return error;
3085     }
3086
3087     if (sset_is_empty(&ofproto->port_poll_set)) {
3088         return EAGAIN;
3089     }
3090
3091     *devnamep = sset_pop(&ofproto->port_poll_set);
3092     return 0;
3093 }
3094
3095 static void
3096 port_poll_wait(const struct ofproto *ofproto_)
3097 {
3098     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3099     dpif_port_poll_wait(ofproto->backer->dpif);
3100 }
3101
3102 static int
3103 port_is_lacp_current(const struct ofport *ofport_)
3104 {
3105     const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3106     return (ofport->bundle && ofport->bundle->lacp
3107             ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3108             : -1);
3109 }
3110 \f
3111 /* Upcall handling. */
3112
3113 struct flow_miss_op {
3114     struct dpif_op dpif_op;
3115
3116     uint64_t slow_stub[128 / 8]; /* Buffer for compose_slow_path() */
3117     struct xlate_out xout;
3118     bool xout_garbage;           /* 'xout' needs to be uninitialized? */
3119
3120     struct ofpbuf mask;          /* Flow mask for "put" ops. */
3121     struct odputil_keybuf maskbuf;
3122
3123     /* If this is a "put" op, then a pointer to the subfacet that should
3124      * be marked as uninstalled if the operation fails. */
3125     struct subfacet *subfacet;
3126 };
3127
3128 /* Figures out whether a flow that missed in 'ofproto', whose details are in
3129  * 'miss' masked by 'wc', is likely to be worth tracking in detail in userspace
3130  * and (usually) installing a datapath flow.  The answer is usually "yes" (a
3131  * return value of true).  However, for short flows the cost of bookkeeping is
3132  * much higher than the benefits, so when the datapath holds a large number of
3133  * flows we impose some heuristics to decide which flows are likely to be worth
3134  * tracking. */
3135 static bool
3136 flow_miss_should_make_facet(struct flow_miss *miss)
3137 {
3138     struct dpif_backer *backer = miss->ofproto->backer;
3139     uint32_t hash;
3140
3141     switch (flow_miss_model) {
3142     case OFPROTO_HANDLE_MISS_AUTO:
3143         break;
3144     case OFPROTO_HANDLE_MISS_WITH_FACETS:
3145         return true;
3146     case OFPROTO_HANDLE_MISS_WITHOUT_FACETS:
3147         return false;
3148     }
3149
3150     if (!backer->governor) {
3151         size_t n_subfacets;
3152
3153         n_subfacets = hmap_count(&backer->subfacets);
3154         if (n_subfacets * 2 <= flow_eviction_threshold) {
3155             return true;
3156         }
3157
3158         backer->governor = governor_create();
3159     }
3160
3161     hash = flow_hash_in_wildcards(&miss->flow, &miss->xout.wc, 0);
3162     return governor_should_install_flow(backer->governor, hash,
3163                                         miss->stats.n_packets);
3164 }
3165
3166 /* Handles 'miss', which matches 'facet'.  May add any required datapath
3167  * operations to 'ops', incrementing '*n_ops' for each new op.
3168  *
3169  * All of the packets in 'miss' are considered to have arrived at time
3170  * 'miss->stats.used'.  This is really important only for new facets: if we
3171  * just called time_msec() here, then the new subfacet or its packets could
3172  * look (occasionally) as though it was used some time after the facet was
3173  * used.  That can make a one-packet flow look like it has a nonzero duration,
3174  * which looks odd in e.g. NetFlow statistics. */
3175 static void
3176 handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
3177                             struct flow_miss_op *ops, size_t *n_ops)
3178 {
3179     enum subfacet_path want_path;
3180     struct subfacet *subfacet;
3181     uint32_t key_hash;
3182
3183     /* Update facet stats. */
3184     facet->packet_count += miss->stats.n_packets;
3185     facet->prev_packet_count += miss->stats.n_packets;
3186     facet->byte_count += miss->stats.n_bytes;
3187     facet->prev_byte_count += miss->stats.n_bytes;
3188
3189     /* Look for an existing subfacet.  If we find one, update its used time. */
3190     key_hash = odp_flow_key_hash(miss->key, miss->key_len);
3191     if (!list_is_empty(&facet->subfacets)) {
3192         subfacet = subfacet_find(miss->ofproto->backer,
3193                                  miss->key, miss->key_len, key_hash);
3194         if (subfacet) {
3195             if (subfacet->facet == facet) {
3196                 subfacet->used = MAX(subfacet->used, miss->stats.used);
3197             } else {
3198                 /* This shouldn't happen. */
3199                 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
3200                 subfacet_destroy(subfacet);
3201                 subfacet = NULL;
3202             }
3203         }
3204     } else {
3205         subfacet = NULL;
3206     }
3207
3208     /* Don't install the flow if it's the result of the "userspace"
3209      * action for an already installed facet.  This can occur when a
3210      * datapath flow with wildcards has a "userspace" action and flows
3211      * sent to userspace result in a different subfacet, which will then
3212      * be rejected as overlapping by the datapath. */
3213     if (miss->upcall_type == DPIF_UC_ACTION
3214         && !list_is_empty(&facet->subfacets)) {
3215         return;
3216     }
3217
3218     /* Create a subfacet, if we don't already have one. */
3219     if (!subfacet) {
3220         subfacet = subfacet_create(facet, miss, key_hash);
3221     }
3222
3223     /* Install the subfacet, if it's not already installed. */
3224     want_path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
3225     if (subfacet->path != want_path) {
3226         struct flow_miss_op *op = &ops[(*n_ops)++];
3227         struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
3228
3229         subfacet->path = want_path;
3230
3231         ofpbuf_use_stack(&op->mask, &op->maskbuf, sizeof op->maskbuf);
3232         if (enable_megaflows) {
3233             odp_flow_key_from_mask(&op->mask, &facet->xout.wc.masks,
3234                                    &miss->flow, UINT32_MAX);
3235         }
3236
3237         op->xout_garbage = false;
3238         op->dpif_op.type = DPIF_OP_FLOW_PUT;
3239         op->subfacet = subfacet;
3240         put->flags = DPIF_FP_CREATE;
3241         put->key = miss->key;
3242         put->key_len = miss->key_len;
3243         put->mask = op->mask.data;
3244         put->mask_len = op->mask.size;
3245
3246         if (want_path == SF_FAST_PATH) {
3247             put->actions = facet->xout.odp_actions.data;
3248             put->actions_len = facet->xout.odp_actions.size;
3249         } else {
3250             compose_slow_path(facet->ofproto, &miss->flow, facet->xout.slow,
3251                               op->slow_stub, sizeof op->slow_stub,
3252                               &put->actions, &put->actions_len);
3253         }
3254         put->stats = NULL;
3255     }
3256 }
3257
3258 /* Handles flow miss 'miss'.  May add any required datapath operations
3259  * to 'ops', incrementing '*n_ops' for each new op. */
3260 static void
3261 handle_flow_miss(struct flow_miss *miss, struct flow_miss_op *ops,
3262                  size_t *n_ops)
3263 {
3264     struct facet *facet;
3265
3266     miss->ofproto->n_missed += miss->stats.n_packets;
3267
3268     facet = facet_lookup_valid(miss->ofproto, &miss->flow);
3269     if (!facet) {
3270         /* There does not exist a bijection between 'struct flow' and datapath
3271          * flow keys with fitness ODP_FIT_TO_LITTLE.  This breaks a fundamental
3272          * assumption used throughout the facet and subfacet handling code.
3273          * Since we have to handle these misses in userspace anyway, we simply
3274          * skip facet creation, avoiding the problem altogether. */
3275         if (miss->key_fitness == ODP_FIT_TOO_LITTLE
3276             || !flow_miss_should_make_facet(miss)) {
3277             return;
3278         }
3279
3280         facet = facet_create(miss);
3281     }
3282     handle_flow_miss_with_facet(miss, facet, ops, n_ops);
3283 }
3284
3285 static struct drop_key *
3286 drop_key_lookup(const struct dpif_backer *backer, const struct nlattr *key,
3287                 size_t key_len)
3288 {
3289     struct drop_key *drop_key;
3290
3291     HMAP_FOR_EACH_WITH_HASH (drop_key, hmap_node, hash_bytes(key, key_len, 0),
3292                              &backer->drop_keys) {
3293         if (drop_key->key_len == key_len
3294             && !memcmp(drop_key->key, key, key_len)) {
3295             return drop_key;
3296         }
3297     }
3298     return NULL;
3299 }
3300
3301 static void
3302 drop_key_clear(struct dpif_backer *backer)
3303 {
3304     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3305     struct drop_key *drop_key, *next;
3306
3307     HMAP_FOR_EACH_SAFE (drop_key, next, hmap_node, &backer->drop_keys) {
3308         int error;
3309
3310         error = dpif_flow_del(backer->dpif, drop_key->key, drop_key->key_len,
3311                               NULL);
3312         if (error && !VLOG_DROP_WARN(&rl)) {
3313             struct ds ds = DS_EMPTY_INITIALIZER;
3314             odp_flow_key_format(drop_key->key, drop_key->key_len, &ds);
3315             VLOG_WARN("Failed to delete drop key (%s) (%s)",
3316                       ovs_strerror(error), ds_cstr(&ds));
3317             ds_destroy(&ds);
3318         }
3319
3320         hmap_remove(&backer->drop_keys, &drop_key->hmap_node);
3321         drop_key_destroy(drop_key);
3322     }
3323
3324     udpif_drop_key_clear(backer->udpif);
3325 }
3326
3327 static void
3328 handle_flow_misses(struct dpif_backer *backer, struct flow_miss_batch *fmb)
3329 {
3330     struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH];
3331     struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH];
3332     struct flow_miss *miss;
3333     size_t n_ops, i;
3334
3335     /* Process each element in the to-do list, constructing the set of
3336      * operations to batch. */
3337     n_ops = 0;
3338     HMAP_FOR_EACH (miss, hmap_node, &fmb->misses) {
3339         handle_flow_miss(miss, flow_miss_ops, &n_ops);
3340     }
3341     ovs_assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
3342
3343     /* Execute batch. */
3344     for (i = 0; i < n_ops; i++) {
3345         dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3346     }
3347     dpif_operate(backer->dpif, dpif_ops, n_ops);
3348
3349     for (i = 0; i < n_ops; i++) {
3350         if (dpif_ops[i]->error != 0
3351             && flow_miss_ops[i].dpif_op.type == DPIF_OP_FLOW_PUT
3352             && flow_miss_ops[i].subfacet) {
3353             struct subfacet *subfacet = flow_miss_ops[i].subfacet;
3354
3355             COVERAGE_INC(subfacet_install_fail);
3356
3357             /* Zero-out subfacet counters when installation failed, but
3358              * datapath reported hits.  This should not happen and
3359              * indicates a bug, since if the datapath flow exists, we
3360              * should not be attempting to create a new subfacet.  A
3361              * buggy datapath could trigger this, so just zero out the
3362              * counters and log an error. */
3363             if (subfacet->dp_packet_count || subfacet->dp_byte_count) {
3364                 VLOG_ERR_RL(&rl, "failed to install subfacet for which "
3365                             "datapath reported hits");
3366                 subfacet->dp_packet_count = subfacet->dp_byte_count = 0;
3367             }
3368
3369             subfacet->path = SF_NOT_INSTALLED;
3370         }
3371     }
3372 }
3373
3374 static void
3375 handle_upcalls(struct dpif_backer *backer)
3376 {
3377     struct flow_miss_batch *fmb;
3378     int n_processed;
3379
3380     for (n_processed = 0; n_processed < FLOW_MISS_MAX_BATCH; n_processed++) {
3381         struct drop_key *drop_key = drop_key_next(backer->udpif);
3382         if (!drop_key) {
3383             break;
3384         }
3385
3386         if (!drop_key_lookup(backer, drop_key->key, drop_key->key_len)) {
3387             hmap_insert(&backer->drop_keys, &drop_key->hmap_node,
3388                         hash_bytes(drop_key->key, drop_key->key_len, 0));
3389             dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
3390                           drop_key->key, drop_key->key_len,
3391                           NULL, 0, NULL, 0, NULL);
3392         } else {
3393             drop_key_destroy(drop_key);
3394         }
3395     }
3396
3397     fmb = flow_miss_batch_next(backer->udpif);
3398     if (fmb) {
3399         handle_flow_misses(backer, fmb);
3400         flow_miss_batch_destroy(fmb);
3401     }
3402 }
3403 \f
3404 /* Flow expiration. */
3405
3406 static int subfacet_max_idle(const struct dpif_backer *);
3407 static void update_stats(struct dpif_backer *);
3408 static void rule_expire(struct rule_dpif *) OVS_REQUIRES(ofproto_mutex);
3409 static void expire_subfacets(struct dpif_backer *, int dp_max_idle);
3410
3411 /* This function is called periodically by run().  Its job is to collect
3412  * updates for the flows that have been installed into the datapath, most
3413  * importantly when they last were used, and then use that information to
3414  * expire flows that have not been used recently.
3415  *
3416  * Returns the number of milliseconds after which it should be called again. */
3417 static int
3418 expire(struct dpif_backer *backer)
3419 {
3420     struct ofproto_dpif *ofproto;
3421     size_t n_subfacets;
3422     int max_idle;
3423
3424     /* Periodically clear out the drop keys in an effort to keep them
3425      * relatively few. */
3426     drop_key_clear(backer);
3427
3428     /* Update stats for each flow in the backer. */
3429     update_stats(backer);
3430
3431     n_subfacets = hmap_count(&backer->subfacets);
3432     backer->avg_n_subfacet += n_subfacets;
3433     backer->avg_n_subfacet /= 2;
3434
3435     backer->max_n_subfacet = MAX(backer->max_n_subfacet, n_subfacets);
3436
3437     max_idle = subfacet_max_idle(backer);
3438     expire_subfacets(backer, max_idle);
3439
3440     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3441         struct rule *rule, *next_rule;
3442
3443         if (ofproto->backer != backer) {
3444             continue;
3445         }
3446
3447         /* Expire OpenFlow flows whose idle_timeout or hard_timeout
3448          * has passed. */
3449         ovs_mutex_lock(&ofproto_mutex);
3450         LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
3451                             &ofproto->up.expirable) {
3452             rule_expire(rule_dpif_cast(rule));
3453         }
3454         ovs_mutex_unlock(&ofproto_mutex);
3455
3456         /* All outstanding data in existing flows has been accounted, so it's a
3457          * good time to do bond rebalancing. */
3458         if (ofproto->has_bonded_bundles) {
3459             struct ofbundle *bundle;
3460
3461             HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3462                 if (bundle->bond) {
3463                     bond_rebalance(bundle->bond);
3464                 }
3465             }
3466         }
3467     }
3468
3469     return MIN(max_idle, 1000);
3470 }
3471
3472 /* Updates flow table statistics given that the datapath just reported 'stats'
3473  * as 'subfacet''s statistics. */
3474 static void
3475 update_subfacet_stats(struct subfacet *subfacet,
3476                       const struct dpif_flow_stats *stats)
3477 {
3478     struct facet *facet = subfacet->facet;
3479     struct dpif_flow_stats diff;
3480
3481     diff.tcp_flags = stats->tcp_flags;
3482     diff.used = stats->used;
3483
3484     if (stats->n_packets >= subfacet->dp_packet_count) {
3485         diff.n_packets = stats->n_packets - subfacet->dp_packet_count;
3486     } else {
3487         VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3488         diff.n_packets = 0;
3489     }
3490
3491     if (stats->n_bytes >= subfacet->dp_byte_count) {
3492         diff.n_bytes = stats->n_bytes - subfacet->dp_byte_count;
3493     } else {
3494         VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3495         diff.n_bytes = 0;
3496     }
3497
3498     facet->ofproto->n_hit += diff.n_packets;
3499     subfacet->dp_packet_count = stats->n_packets;
3500     subfacet->dp_byte_count = stats->n_bytes;
3501     subfacet_update_stats(subfacet, &diff);
3502
3503     if (diff.n_packets) {
3504         facet_learn(facet);
3505     }
3506 }
3507
3508 /* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
3509  * about, or a flow that shouldn't be installed but was anyway.  Delete it. */
3510 static void
3511 delete_unexpected_flow(struct dpif_backer *backer,
3512                        const struct nlattr *key, size_t key_len)
3513 {
3514     if (!VLOG_DROP_WARN(&rl)) {
3515         struct ds s;
3516
3517         ds_init(&s);
3518         odp_flow_key_format(key, key_len, &s);
3519         VLOG_WARN("unexpected flow: %s", ds_cstr(&s));
3520         ds_destroy(&s);
3521     }
3522
3523     COVERAGE_INC(facet_unexpected);
3524     dpif_flow_del(backer->dpif, key, key_len, NULL);
3525 }
3526
3527 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3528  *
3529  * This function also pushes statistics updates to rules which each facet
3530  * resubmits into.  Generally these statistics will be accurate.  However, if a
3531  * facet changes the rule it resubmits into at some time in between
3532  * update_stats() runs, it is possible that statistics accrued to the
3533  * old rule will be incorrectly attributed to the new rule.  This could be
3534  * avoided by calling update_stats() whenever rules are created or
3535  * deleted.  However, the performance impact of making so many calls to the
3536  * datapath do not justify the benefit of having perfectly accurate statistics.
3537  *
3538  * In addition, this function maintains per ofproto flow hit counts. The patch
3539  * port is not treated specially. e.g. A packet ingress from br0 patched into
3540  * br1 will increase the hit count of br0 by 1, however, does not affect
3541  * the hit or miss counts of br1.
3542  */
3543 static void
3544 update_stats(struct dpif_backer *backer)
3545 {
3546     const struct dpif_flow_stats *stats;
3547     struct dpif_flow_dump dump;
3548     const struct nlattr *key, *mask;
3549     size_t key_len, mask_len;
3550
3551     dpif_flow_dump_start(&dump, backer->dpif);
3552     while (dpif_flow_dump_next(&dump, &key, &key_len,
3553                                &mask, &mask_len, NULL, NULL, &stats)) {
3554         struct subfacet *subfacet;
3555         uint32_t key_hash;
3556
3557         key_hash = odp_flow_key_hash(key, key_len);
3558         subfacet = subfacet_find(backer, key, key_len, key_hash);
3559         switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
3560         case SF_FAST_PATH:
3561             update_subfacet_stats(subfacet, stats);
3562             break;
3563
3564         case SF_SLOW_PATH:
3565             /* Stats are updated per-packet. */
3566             break;
3567
3568         case SF_NOT_INSTALLED:
3569         default:
3570             delete_unexpected_flow(backer, key, key_len);
3571             break;
3572         }
3573     }
3574     dpif_flow_dump_done(&dump);
3575 }
3576
3577 /* Calculates and returns the number of milliseconds of idle time after which
3578  * subfacets should expire from the datapath.  When a subfacet expires, we fold
3579  * its statistics into its facet, and when a facet's last subfacet expires, we
3580  * fold its statistic into its rule. */
3581 static int
3582 subfacet_max_idle(const struct dpif_backer *backer)
3583 {
3584     /*
3585      * Idle time histogram.
3586      *
3587      * Most of the time a switch has a relatively small number of subfacets.
3588      * When this is the case we might as well keep statistics for all of them
3589      * in userspace and to cache them in the kernel datapath for performance as
3590      * well.
3591      *
3592      * As the number of subfacets increases, the memory required to maintain
3593      * statistics about them in userspace and in the kernel becomes
3594      * significant.  However, with a large number of subfacets it is likely
3595      * that only a few of them are "heavy hitters" that consume a large amount
3596      * of bandwidth.  At this point, only heavy hitters are worth caching in
3597      * the kernel and maintaining in userspaces; other subfacets we can
3598      * discard.
3599      *
3600      * The technique used to compute the idle time is to build a histogram with
3601      * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each.  Each subfacet
3602      * that is installed in the kernel gets dropped in the appropriate bucket.
3603      * After the histogram has been built, we compute the cutoff so that only
3604      * the most-recently-used 1% of subfacets (but at least
3605      * flow_eviction_threshold flows) are kept cached.  At least
3606      * the most-recently-used bucket of subfacets is kept, so actually an
3607      * arbitrary number of subfacets can be kept in any given expiration run
3608      * (though the next run will delete most of those unless they receive
3609      * additional data).
3610      *
3611      * This requires a second pass through the subfacets, in addition to the
3612      * pass made by update_stats(), because the former function never looks at
3613      * uninstallable subfacets.
3614      */
3615     enum { BUCKET_WIDTH = 100 };
3616     enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3617     int buckets[N_BUCKETS] = { 0 };
3618     int total, subtotal, bucket;
3619     struct subfacet *subfacet;
3620     long long int now;
3621     int i;
3622
3623     total = hmap_count(&backer->subfacets);
3624     if (total <= flow_eviction_threshold) {
3625         return N_BUCKETS * BUCKET_WIDTH;
3626     }
3627
3628     /* Build histogram. */
3629     now = time_msec();
3630     HMAP_FOR_EACH (subfacet, hmap_node, &backer->subfacets) {
3631         long long int idle = now - subfacet->used;
3632         int bucket = (idle <= 0 ? 0
3633                       : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
3634                       : (unsigned int) idle / BUCKET_WIDTH);
3635         buckets[bucket]++;
3636     }
3637
3638     /* Find the first bucket whose flows should be expired. */
3639     subtotal = bucket = 0;
3640     do {
3641         subtotal += buckets[bucket++];
3642     } while (bucket < N_BUCKETS &&
3643              subtotal < MAX(flow_eviction_threshold, total / 100));
3644
3645     if (VLOG_IS_DBG_ENABLED()) {
3646         struct ds s;
3647
3648         ds_init(&s);
3649         ds_put_cstr(&s, "keep");
3650         for (i = 0; i < N_BUCKETS; i++) {
3651             if (i == bucket) {
3652                 ds_put_cstr(&s, ", drop");
3653             }
3654             if (buckets[i]) {
3655                 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
3656             }
3657         }
3658         VLOG_INFO("%s (msec:count)", ds_cstr(&s));
3659         ds_destroy(&s);
3660     }
3661
3662     return bucket * BUCKET_WIDTH;
3663 }
3664
3665 static void
3666 expire_subfacets(struct dpif_backer *backer, int dp_max_idle)
3667 {
3668     /* Cutoff time for most flows. */
3669     long long int normal_cutoff = time_msec() - dp_max_idle;
3670
3671     /* We really want to keep flows for special protocols around, so use a more
3672      * conservative cutoff. */
3673     long long int special_cutoff = time_msec() - 10000;
3674
3675     struct subfacet *subfacet, *next_subfacet;
3676     struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
3677     int n_batch;
3678
3679     n_batch = 0;
3680     HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
3681                         &backer->subfacets) {
3682         long long int cutoff;
3683
3684         cutoff = (subfacet->facet->xout.slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP
3685                                                 | SLOW_STP)
3686                   ? special_cutoff
3687                   : normal_cutoff);
3688         if (subfacet->used < cutoff) {
3689             if (subfacet->path != SF_NOT_INSTALLED) {
3690                 batch[n_batch++] = subfacet;
3691                 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
3692                     subfacet_destroy_batch(backer, batch, n_batch);
3693                     n_batch = 0;
3694                 }
3695             } else {
3696                 subfacet_destroy(subfacet);
3697             }
3698         }
3699     }
3700
3701     if (n_batch > 0) {
3702         subfacet_destroy_batch(backer, batch, n_batch);
3703     }
3704 }
3705
3706 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3707  * then delete it entirely. */
3708 static void
3709 rule_expire(struct rule_dpif *rule)
3710     OVS_REQUIRES(ofproto_mutex)
3711 {
3712     uint16_t idle_timeout, hard_timeout;
3713     long long int now = time_msec();
3714     int reason;
3715
3716     ovs_assert(!rule->up.pending);
3717
3718     /* Has 'rule' expired? */
3719     ovs_mutex_lock(&rule->up.mutex);
3720     hard_timeout = rule->up.hard_timeout;
3721     idle_timeout = rule->up.idle_timeout;
3722     if (hard_timeout && now > rule->up.modified + hard_timeout * 1000) {
3723         reason = OFPRR_HARD_TIMEOUT;
3724     } else if (idle_timeout && now > rule->up.used + idle_timeout * 1000) {
3725         reason = OFPRR_IDLE_TIMEOUT;
3726     } else {
3727         reason = -1;
3728     }
3729     ovs_mutex_unlock(&rule->up.mutex);
3730
3731     if (reason >= 0) {
3732         COVERAGE_INC(ofproto_dpif_expired);
3733         ofproto_rule_expire(&rule->up, reason);
3734     }
3735 }
3736 \f
3737 /* Facets. */
3738
3739 /* Creates and returns a new facet based on 'miss'.
3740  *
3741  * The caller must already have determined that no facet with an identical
3742  * 'miss->flow' exists in 'miss->ofproto'.
3743  *
3744  * 'rule' and 'xout' must have been created based on 'miss'.
3745  *
3746  * 'facet'' statistics are initialized based on 'stats'.
3747  *
3748  * The facet will initially have no subfacets.  The caller should create (at
3749  * least) one subfacet with subfacet_create(). */
3750 static struct facet *
3751 facet_create(const struct flow_miss *miss)
3752 {
3753     struct ofproto_dpif *ofproto = miss->ofproto;
3754     struct facet *facet;
3755     struct match match;
3756
3757     COVERAGE_INC(facet_create);
3758     facet = xzalloc(sizeof *facet);
3759     facet->ofproto = miss->ofproto;
3760     facet->used = miss->stats.used;
3761     facet->flow = miss->flow;
3762     facet->learn_rl = time_msec() + 500;
3763
3764     list_init(&facet->subfacets);
3765
3766     xlate_out_copy(&facet->xout, &miss->xout);
3767
3768     match_init(&match, &facet->flow, &facet->xout.wc);
3769     cls_rule_init(&facet->cr, &match, OFP_DEFAULT_PRIORITY);
3770     ovs_rwlock_wrlock(&ofproto->facets.rwlock);
3771     classifier_insert(&ofproto->facets, &facet->cr);
3772     ovs_rwlock_unlock(&ofproto->facets.rwlock);
3773
3774     return facet;
3775 }
3776
3777 static void
3778 facet_free(struct facet *facet)
3779 {
3780     if (facet) {
3781         xlate_out_uninit(&facet->xout);
3782         free(facet);
3783     }
3784 }
3785
3786 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3787  * 'flow' must reflect the data in 'packet'. */
3788 int
3789 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3790                              const struct flow *flow,
3791                              struct rule_dpif *rule,
3792                              const struct ofpact *ofpacts, size_t ofpacts_len,
3793                              struct ofpbuf *packet)
3794 {
3795     struct odputil_keybuf keybuf;
3796     struct dpif_flow_stats stats;
3797     struct xlate_out xout;
3798     struct xlate_in xin;
3799     ofp_port_t in_port;
3800     struct ofpbuf key;
3801     int error;
3802
3803     ovs_assert((rule != NULL) != (ofpacts != NULL));
3804
3805     dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3806     if (rule) {
3807         rule_dpif_credit_stats(rule, &stats);
3808     }
3809
3810     xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
3811     xin.ofpacts = ofpacts;
3812     xin.ofpacts_len = ofpacts_len;
3813     xin.resubmit_stats = &stats;
3814     xlate_actions(&xin, &xout);
3815
3816     ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3817     in_port = flow->in_port.ofp_port;
3818     if (in_port == OFPP_NONE) {
3819         in_port = OFPP_LOCAL;
3820     }
3821     odp_flow_key_from_flow(&key, flow, ofp_port_to_odp_port(ofproto, in_port));
3822
3823     error = dpif_execute(ofproto->backer->dpif, key.data, key.size,
3824                          xout.odp_actions.data, xout.odp_actions.size, packet,
3825                          (xout.slow & SLOW_ACTION) != 0);
3826     xlate_out_uninit(&xout);
3827
3828     return error;
3829 }
3830
3831 /* Remove 'facet' from its ofproto and free up the associated memory:
3832  *
3833  *   - If 'facet' was installed in the datapath, uninstalls it and updates its
3834  *     rule's statistics, via subfacet_uninstall().
3835  *
3836  *   - Removes 'facet' from its rule and from ofproto->facets.
3837  */
3838 static void
3839 facet_remove(struct facet *facet)
3840 {
3841     struct subfacet *subfacet, *next_subfacet;
3842
3843     COVERAGE_INC(facet_remove);
3844     ovs_assert(!list_is_empty(&facet->subfacets));
3845
3846     /* First uninstall all of the subfacets to get final statistics. */
3847     LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3848         subfacet_uninstall(subfacet);
3849     }
3850
3851     /* Flush the final stats to the rule.
3852      *
3853      * This might require us to have at least one subfacet around so that we
3854      * can use its actions for accounting in facet_account(), which is why we
3855      * have uninstalled but not yet destroyed the subfacets. */
3856     facet_flush_stats(facet);
3857
3858     /* Now we're really all done so destroy everything. */
3859     LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
3860                         &facet->subfacets) {
3861         subfacet_destroy__(subfacet);
3862     }
3863     ovs_rwlock_wrlock(&facet->ofproto->facets.rwlock);
3864     classifier_remove(&facet->ofproto->facets, &facet->cr);
3865     ovs_rwlock_unlock(&facet->ofproto->facets.rwlock);
3866     cls_rule_destroy(&facet->cr);
3867     facet_free(facet);
3868 }
3869
3870 /* Feed information from 'facet' back into the learning table to keep it in
3871  * sync with what is actually flowing through the datapath. */
3872 static void
3873 facet_learn(struct facet *facet)
3874 {
3875     long long int now = time_msec();
3876
3877     if (!facet->xout.has_fin_timeout && now < facet->learn_rl) {
3878         return;
3879     }
3880
3881     facet->learn_rl = now + 500;
3882
3883     if (!facet->xout.has_learn
3884         && !facet->xout.has_normal
3885         && (!facet->xout.has_fin_timeout
3886             || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
3887         return;
3888     }
3889
3890     facet_push_stats(facet, true);
3891 }
3892
3893 /* Returns true if the only action for 'facet' is to send to the controller.
3894  * (We don't report NetFlow expiration messages for such facets because they
3895  * are just part of the control logic for the network, not real traffic). */
3896 static bool
3897 facet_is_controller_flow(struct facet *facet)
3898 {
3899     if (facet) {
3900         struct ofproto_dpif *ofproto = facet->ofproto;
3901         const struct ofpact *ofpacts;
3902         struct rule_actions *actions;
3903         struct rule_dpif *rule;
3904         size_t ofpacts_len;
3905         bool is_controller;
3906
3907         rule_dpif_lookup(ofproto, &facet->flow, NULL, &rule);
3908         actions = rule_dpif_get_actions(rule);
3909         rule_dpif_unref(rule);
3910
3911         ofpacts_len = actions->ofpacts_len;
3912         ofpacts = actions->ofpacts;
3913         is_controller = ofpacts_len > 0
3914             && ofpacts->type == OFPACT_CONTROLLER
3915             && ofpact_next(ofpacts) >= ofpact_end(ofpacts, ofpacts_len);
3916         rule_actions_unref(actions);
3917
3918         return is_controller;
3919     }
3920     return false;
3921 }
3922
3923 /* Folds all of 'facet''s statistics into its rule.  Also updates the
3924  * accounting ofhook and emits a NetFlow expiration if appropriate.  All of
3925  * 'facet''s statistics in the datapath should have been zeroed and folded into
3926  * its packet and byte counts before this function is called. */
3927 static void
3928 facet_flush_stats(struct facet *facet)
3929 {
3930     struct ofproto_dpif *ofproto = facet->ofproto;
3931     struct subfacet *subfacet;
3932
3933     LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3934         ovs_assert(!subfacet->dp_byte_count);
3935         ovs_assert(!subfacet->dp_packet_count);
3936     }
3937
3938     facet_push_stats(facet, false);
3939
3940     if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3941         netflow_expire(ofproto->netflow, &facet->flow);
3942         netflow_flow_clear(ofproto->netflow, &facet->flow);
3943     }
3944
3945     /* Reset counters to prevent double counting if 'facet' ever gets
3946      * reinstalled. */
3947     facet_reset_counters(facet);
3948     facet->tcp_flags = 0;
3949 }
3950
3951 /* Searches 'ofproto''s table of facets for one which would be responsible for
3952  * 'flow'.  Returns it if found, otherwise a null pointer.
3953  *
3954  * The returned facet might need revalidation; use facet_lookup_valid()
3955  * instead if that is important. */
3956 static struct facet *
3957 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3958 {
3959     struct cls_rule *cr;
3960
3961     ovs_rwlock_rdlock(&ofproto->facets.rwlock);
3962     cr = classifier_lookup(&ofproto->facets, flow, NULL);
3963     ovs_rwlock_unlock(&ofproto->facets.rwlock);
3964     return cr ? CONTAINER_OF(cr, struct facet, cr) : NULL;
3965 }
3966
3967 /* Searches 'ofproto''s table of facets for one capable that covers
3968  * 'flow'.  Returns it if found, otherwise a null pointer.
3969  *
3970  * The returned facet is guaranteed to be valid. */
3971 static struct facet *
3972 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3973 {
3974     struct facet *facet;
3975
3976     facet = facet_find(ofproto, flow);
3977     if (facet
3978         && ofproto->backer->need_revalidate
3979         && !facet_revalidate(facet)) {
3980         return NULL;
3981     }
3982
3983     return facet;
3984 }
3985
3986 static bool
3987 facet_check_consistency(struct facet *facet)
3988 {
3989     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3990
3991     struct xlate_out xout;
3992     struct xlate_in xin;
3993     bool ok;
3994
3995     /* Check the datapath actions for consistency. */
3996     xlate_in_init(&xin, facet->ofproto, &facet->flow, NULL, 0, NULL);
3997     xlate_actions(&xin, &xout);
3998
3999     ok = ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)
4000         && facet->xout.slow == xout.slow;
4001     if (!ok && !VLOG_DROP_WARN(&rl)) {
4002         struct ds s = DS_EMPTY_INITIALIZER;
4003
4004         flow_format(&s, &facet->flow);
4005         ds_put_cstr(&s, ": inconsistency in facet");
4006
4007         if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
4008             ds_put_cstr(&s, " (actions were: ");
4009             format_odp_actions(&s, facet->xout.odp_actions.data,
4010                                facet->xout.odp_actions.size);
4011             ds_put_cstr(&s, ") (correct actions: ");
4012             format_odp_actions(&s, xout.odp_actions.data,
4013                                xout.odp_actions.size);
4014             ds_put_char(&s, ')');
4015         }
4016
4017         if (facet->xout.slow != xout.slow) {
4018             ds_put_format(&s, " slow path incorrect. should be %d", xout.slow);
4019         }
4020
4021         ds_destroy(&s);
4022     }
4023     xlate_out_uninit(&xout);
4024
4025     return ok;
4026 }
4027
4028 /* Re-searches the classifier for 'facet':
4029  *
4030  *   - If the rule found is different from 'facet''s current rule, moves
4031  *     'facet' to the new rule and recompiles its actions.
4032  *
4033  *   - If the rule found is the same as 'facet''s current rule, leaves 'facet'
4034  *     where it is and recompiles its actions anyway.
4035  *
4036  *   - If any of 'facet''s subfacets correspond to a new flow according to
4037  *     xlate_receive(), 'facet' is removed.
4038  *
4039  *   Returns true if 'facet' is still valid.  False if 'facet' was removed. */
4040 static bool
4041 facet_revalidate(struct facet *facet)
4042 {
4043     struct ofproto_dpif *ofproto = facet->ofproto;
4044     struct rule_dpif *new_rule;
4045     struct subfacet *subfacet;
4046     struct flow_wildcards wc;
4047     struct xlate_out xout;
4048     struct xlate_in xin;
4049
4050     COVERAGE_INC(facet_revalidate);
4051
4052     /* Check that child subfacets still correspond to this facet.  Tunnel
4053      * configuration changes could cause a subfacet's OpenFlow in_port to
4054      * change. */
4055     LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4056         struct ofproto_dpif *recv_ofproto;
4057         struct flow recv_flow;
4058         int error;
4059
4060         error = xlate_receive(ofproto->backer, NULL, subfacet->key,
4061                               subfacet->key_len, &recv_flow, NULL,
4062                               &recv_ofproto, NULL, NULL, NULL, NULL);
4063         if (error
4064             || recv_ofproto != ofproto
4065             || facet != facet_find(ofproto, &recv_flow)) {
4066             facet_remove(facet);
4067             return false;
4068         }
4069     }
4070
4071     flow_wildcards_init_catchall(&wc);
4072     rule_dpif_lookup(ofproto, &facet->flow, &wc, &new_rule);
4073
4074     /* Calculate new datapath actions.
4075      *
4076      * We do not modify any 'facet' state yet, because we might need to, e.g.,
4077      * emit a NetFlow expiration and, if so, we need to have the old state
4078      * around to properly compose it. */
4079     xlate_in_init(&xin, ofproto, &facet->flow, new_rule, 0, NULL);
4080     xlate_actions(&xin, &xout);
4081     flow_wildcards_or(&xout.wc, &xout.wc, &wc);
4082     /* Make sure non -packet fields are not masked. If not cleared,
4083      * the memcmp() below may fail, causing an otherwise valid facet
4084      * to be removed. */
4085     flow_wildcards_clear_non_packet_fields(&xout.wc);
4086
4087     /* A facet's slow path reason should only change under dramatic
4088      * circumstances.  Rather than try to update everything, it's simpler to
4089      * remove the facet and start over.
4090      *
4091      * More importantly, if a facet's wildcards change, it will be relatively
4092      * difficult to figure out if its subfacets still belong to it, and if not
4093      * which facet they may belong to.  Again, to avoid the complexity, we
4094      * simply give up instead. */
4095     if (facet->xout.slow != xout.slow
4096         || memcmp(&facet->xout.wc, &xout.wc, sizeof xout.wc)) {
4097         facet_remove(facet);
4098         xlate_out_uninit(&xout);
4099         rule_dpif_unref(new_rule);
4100         return false;
4101     }
4102
4103     if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
4104         LIST_FOR_EACH(subfacet, list_node, &facet->subfacets) {
4105             if (subfacet->path == SF_FAST_PATH) {
4106                 struct dpif_flow_stats stats;
4107
4108                 subfacet_install(subfacet, &xout.odp_actions, &stats);
4109                 subfacet_update_stats(subfacet, &stats);
4110             }
4111         }
4112
4113         facet_flush_stats(facet);
4114
4115         ofpbuf_clear(&facet->xout.odp_actions);
4116         ofpbuf_put(&facet->xout.odp_actions, xout.odp_actions.data,
4117                    xout.odp_actions.size);
4118     }
4119
4120     /* Update 'facet' now that we've taken care of all the old state. */
4121     facet->xout.slow = xout.slow;
4122     facet->xout.has_learn = xout.has_learn;
4123     facet->xout.has_normal = xout.has_normal;
4124     facet->xout.has_fin_timeout = xout.has_fin_timeout;
4125     facet->xout.nf_output_iface = xout.nf_output_iface;
4126     facet->xout.mirrors = xout.mirrors;
4127
4128     ovs_mutex_lock(&new_rule->up.mutex);
4129     facet->used = MAX(facet->used, new_rule->up.created);
4130     ovs_mutex_unlock(&new_rule->up.mutex);
4131
4132     xlate_out_uninit(&xout);
4133     rule_dpif_unref(new_rule);
4134     return true;
4135 }
4136
4137 static void
4138 facet_reset_counters(struct facet *facet)
4139 {
4140     facet->packet_count = 0;
4141     facet->byte_count = 0;
4142     facet->prev_packet_count = 0;
4143     facet->prev_byte_count = 0;
4144 }
4145
4146 static void
4147 flow_push_stats(struct ofproto_dpif *ofproto, struct flow *flow,
4148                 struct dpif_flow_stats *stats, bool may_learn)
4149 {
4150     struct xlate_in xin;
4151
4152     xlate_in_init(&xin, ofproto, flow, NULL, stats->tcp_flags, NULL);
4153     xin.resubmit_stats = stats;
4154     xin.may_learn = may_learn;
4155     xlate_actions_for_side_effects(&xin);
4156 }
4157
4158 static void
4159 facet_push_stats(struct facet *facet, bool may_learn)
4160 {
4161     struct dpif_flow_stats stats;
4162
4163     ovs_assert(facet->packet_count >= facet->prev_packet_count);
4164     ovs_assert(facet->byte_count >= facet->prev_byte_count);
4165     ovs_assert(facet->used >= facet->prev_used);
4166
4167     stats.n_packets = facet->packet_count - facet->prev_packet_count;
4168     stats.n_bytes = facet->byte_count - facet->prev_byte_count;
4169     stats.used = facet->used;
4170     stats.tcp_flags = facet->tcp_flags;
4171
4172     if (may_learn || stats.n_packets || facet->used > facet->prev_used) {
4173         facet->prev_packet_count = facet->packet_count;
4174         facet->prev_byte_count = facet->byte_count;
4175         facet->prev_used = facet->used;
4176         flow_push_stats(facet->ofproto, &facet->flow, &stats, may_learn);
4177     }
4178 }
4179
4180 static void
4181 push_all_stats(void)
4182 {
4183     static long long int rl = LLONG_MIN;
4184     struct ofproto_dpif *ofproto;
4185
4186     if (time_msec() < rl) {
4187         return;
4188     }
4189
4190     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4191         struct cls_cursor cursor;
4192         struct facet *facet;
4193
4194         ovs_rwlock_rdlock(&ofproto->facets.rwlock);
4195         cls_cursor_init(&cursor, &ofproto->facets, NULL);
4196         CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
4197             facet_push_stats(facet, false);
4198         }
4199         ovs_rwlock_unlock(&ofproto->facets.rwlock);
4200     }
4201
4202     rl = time_msec() + 100;
4203 }
4204
4205 void
4206 rule_dpif_credit_stats(struct rule_dpif *rule,
4207                        const struct dpif_flow_stats *stats)
4208 {
4209     ovs_mutex_lock(&rule->stats_mutex);
4210     rule->packet_count += stats->n_packets;
4211     rule->byte_count += stats->n_bytes;
4212     rule->up.used = MAX(rule->up.used, stats->used);
4213     ovs_mutex_unlock(&rule->stats_mutex);
4214 }
4215
4216 bool
4217 rule_dpif_is_fail_open(const struct rule_dpif *rule)
4218 {
4219     return is_fail_open_rule(&rule->up);
4220 }
4221
4222 bool
4223 rule_dpif_is_table_miss(const struct rule_dpif *rule)
4224 {
4225     return rule_is_table_miss(&rule->up);
4226 }
4227
4228 ovs_be64
4229 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
4230     OVS_REQUIRES(rule->up.mutex)
4231 {
4232     return rule->up.flow_cookie;
4233 }
4234
4235 void
4236 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
4237                      uint16_t hard_timeout)
4238 {
4239     ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
4240 }
4241
4242 /* Returns 'rule''s actions.  The caller owns a reference on the returned
4243  * actions and must eventually release it (with rule_actions_unref()) to avoid
4244  * a memory leak. */
4245 struct rule_actions *
4246 rule_dpif_get_actions(const struct rule_dpif *rule)
4247 {
4248     return rule_get_actions(&rule->up);
4249 }
4250 \f
4251 /* Subfacets. */
4252
4253 static struct subfacet *
4254 subfacet_find(struct dpif_backer *backer, const struct nlattr *key,
4255               size_t key_len, uint32_t key_hash)
4256 {
4257     struct subfacet *subfacet;
4258
4259     HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
4260                              &backer->subfacets) {
4261         if (subfacet->key_len == key_len
4262             && !memcmp(key, subfacet->key, key_len)) {
4263             return subfacet;
4264         }
4265     }
4266
4267     return NULL;
4268 }
4269
4270 /* Creates and returns a new subfacet within 'facet' for the flow in 'miss'.
4271  * 'key_hash' must be a hash over miss->key.  The caller must have already
4272  * ensured that no subfacet subfacet already exists. */
4273 static struct subfacet *
4274 subfacet_create(struct facet *facet, struct flow_miss *miss, uint32_t key_hash)
4275 {
4276     struct dpif_backer *backer = miss->ofproto->backer;
4277     const struct nlattr *key = miss->key;
4278     size_t key_len = miss->key_len;
4279     struct subfacet *subfacet;
4280
4281     subfacet = (list_is_empty(&facet->subfacets)
4282                 ? &facet->one_subfacet
4283                 : xmalloc(sizeof *subfacet));
4284
4285     COVERAGE_INC(subfacet_create);
4286     hmap_insert(&backer->subfacets, &subfacet->hmap_node, key_hash);
4287     list_push_back(&facet->subfacets, &subfacet->list_node);
4288     subfacet->facet = facet;
4289     subfacet->key = xmemdup(key, key_len);
4290     subfacet->key_len = key_len;
4291     subfacet->used = miss->stats.used;
4292     subfacet->created = subfacet->used;
4293     subfacet->dp_packet_count = 0;
4294     subfacet->dp_byte_count = 0;
4295     subfacet->path = SF_NOT_INSTALLED;
4296     subfacet->backer = backer;
4297
4298     return subfacet;
4299 }
4300
4301 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
4302  * its facet within 'ofproto', and frees it. */
4303 static void
4304 subfacet_destroy__(struct subfacet *subfacet)
4305 {
4306     struct facet *facet = subfacet->facet;
4307
4308     COVERAGE_INC(subfacet_destroy);
4309     subfacet_uninstall(subfacet);
4310     hmap_remove(&subfacet->backer->subfacets, &subfacet->hmap_node);
4311     list_remove(&subfacet->list_node);
4312     free(subfacet->key);
4313     if (subfacet != &facet->one_subfacet) {
4314         free(subfacet);
4315     }
4316 }
4317
4318 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
4319  * last remaining subfacet in its facet destroys the facet too. */
4320 static void
4321 subfacet_destroy(struct subfacet *subfacet)
4322 {
4323     struct facet *facet = subfacet->facet;
4324
4325     if (list_is_singleton(&facet->subfacets)) {
4326         /* facet_remove() needs at least one subfacet (it will remove it). */
4327         facet_remove(facet);
4328     } else {
4329         subfacet_destroy__(subfacet);
4330     }
4331 }
4332
4333 static void
4334 subfacet_destroy_batch(struct dpif_backer *backer,
4335                        struct subfacet **subfacets, int n)
4336 {
4337     struct dpif_op ops[SUBFACET_DESTROY_MAX_BATCH];
4338     struct dpif_op *opsp[SUBFACET_DESTROY_MAX_BATCH];
4339     struct dpif_flow_stats stats[SUBFACET_DESTROY_MAX_BATCH];
4340     int i;
4341
4342     for (i = 0; i < n; i++) {
4343         ops[i].type = DPIF_OP_FLOW_DEL;
4344         ops[i].u.flow_del.key = subfacets[i]->key;
4345         ops[i].u.flow_del.key_len = subfacets[i]->key_len;
4346         ops[i].u.flow_del.stats = &stats[i];
4347         opsp[i] = &ops[i];
4348     }
4349
4350     dpif_operate(backer->dpif, opsp, n);
4351     for (i = 0; i < n; i++) {
4352         subfacet_reset_dp_stats(subfacets[i], &stats[i]);
4353         subfacets[i]->path = SF_NOT_INSTALLED;
4354         subfacet_destroy(subfacets[i]);
4355     }
4356 }
4357
4358 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
4359  * bytes of actions in 'actions'.  If 'stats' is non-null, statistics counters
4360  * in the datapath will be zeroed and 'stats' will be updated with traffic new
4361  * since 'subfacet' was last updated.
4362  *
4363  * Returns 0 if successful, otherwise a positive errno value. */
4364 static int
4365 subfacet_install(struct subfacet *subfacet, const struct ofpbuf *odp_actions,
4366                  struct dpif_flow_stats *stats)
4367 {
4368     struct facet *facet = subfacet->facet;
4369     enum subfacet_path path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
4370     const struct nlattr *actions = odp_actions->data;
4371     size_t actions_len = odp_actions->size;
4372     struct odputil_keybuf maskbuf;
4373     struct ofpbuf mask;
4374
4375     uint64_t slow_path_stub[128 / 8];
4376     enum dpif_flow_put_flags flags;
4377     int ret;
4378
4379     flags = subfacet->path == SF_NOT_INSTALLED ? DPIF_FP_CREATE
4380                                                : DPIF_FP_MODIFY;
4381     if (stats) {
4382         flags |= DPIF_FP_ZERO_STATS;
4383     }
4384
4385     if (path == SF_SLOW_PATH) {
4386         compose_slow_path(facet->ofproto, &facet->flow, facet->xout.slow,
4387                           slow_path_stub, sizeof slow_path_stub,
4388                           &actions, &actions_len);
4389     }
4390
4391     ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
4392     if (enable_megaflows) {
4393         odp_flow_key_from_mask(&mask, &facet->xout.wc.masks,
4394                                &facet->flow, UINT32_MAX);
4395     }
4396
4397     ret = dpif_flow_put(subfacet->backer->dpif, flags, subfacet->key,
4398                         subfacet->key_len,  mask.data, mask.size,
4399                         actions, actions_len, stats);
4400
4401     if (stats) {
4402         subfacet_reset_dp_stats(subfacet, stats);
4403     }
4404
4405     if (ret) {
4406         COVERAGE_INC(subfacet_install_fail);
4407     } else {
4408         subfacet->path = path;
4409     }
4410     return ret;
4411 }
4412
4413 /* If 'subfacet' is installed in the datapath, uninstalls it. */
4414 static void
4415 subfacet_uninstall(struct subfacet *subfacet)
4416 {
4417     if (subfacet->path != SF_NOT_INSTALLED) {
4418         struct ofproto_dpif *ofproto = subfacet->facet->ofproto;
4419         struct dpif_flow_stats stats;
4420         int error;
4421
4422         error = dpif_flow_del(ofproto->backer->dpif, subfacet->key,
4423                               subfacet->key_len, &stats);
4424         subfacet_reset_dp_stats(subfacet, &stats);
4425         if (!error) {
4426             subfacet_update_stats(subfacet, &stats);
4427         }
4428         subfacet->path = SF_NOT_INSTALLED;
4429     } else {
4430         ovs_assert(subfacet->dp_packet_count == 0);
4431         ovs_assert(subfacet->dp_byte_count == 0);
4432     }
4433 }
4434
4435 /* Resets 'subfacet''s datapath statistics counters.  This should be called
4436  * when 'subfacet''s statistics are cleared in the datapath.  If 'stats' is
4437  * non-null, it should contain the statistics returned by dpif when 'subfacet'
4438  * was reset in the datapath.  'stats' will be modified to include only
4439  * statistics new since 'subfacet' was last updated. */
4440 static void
4441 subfacet_reset_dp_stats(struct subfacet *subfacet,
4442                         struct dpif_flow_stats *stats)
4443 {
4444     if (stats
4445         && subfacet->dp_packet_count <= stats->n_packets
4446         && subfacet->dp_byte_count <= stats->n_bytes) {
4447         stats->n_packets -= subfacet->dp_packet_count;
4448         stats->n_bytes -= subfacet->dp_byte_count;
4449     }
4450
4451     subfacet->dp_packet_count = 0;
4452     subfacet->dp_byte_count = 0;
4453 }
4454
4455 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
4456  *
4457  * Because of the meaning of a subfacet's counters, it only makes sense to do
4458  * this if 'stats' are not tracked in the datapath, that is, if 'stats'
4459  * represents a packet that was sent by hand or if it represents statistics
4460  * that have been cleared out of the datapath. */
4461 static void
4462 subfacet_update_stats(struct subfacet *subfacet,
4463                       const struct dpif_flow_stats *stats)
4464 {
4465     if (stats->n_packets || stats->used > subfacet->used) {
4466         struct facet *facet = subfacet->facet;
4467
4468         subfacet->used = MAX(subfacet->used, stats->used);
4469         facet->used = MAX(facet->used, stats->used);
4470         facet->packet_count += stats->n_packets;
4471         facet->byte_count += stats->n_bytes;
4472         facet->tcp_flags |= stats->tcp_flags;
4473     }
4474 }
4475 \f
4476 /* Rules. */
4477
4478 /* Lookup 'flow' in 'ofproto''s classifier.  If 'wc' is non-null, sets
4479  * the fields that were relevant as part of the lookup. */
4480 void
4481 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
4482                  struct flow_wildcards *wc, struct rule_dpif **rule)
4483 {
4484     struct ofport_dpif *port;
4485
4486     if (rule_dpif_lookup_in_table(ofproto, flow, wc, 0, rule)) {
4487         return;
4488     }
4489     port = get_ofp_port(ofproto, flow->in_port.ofp_port);
4490     if (!port) {
4491         VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
4492                      flow->in_port.ofp_port);
4493     }
4494
4495     choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
4496                      ofproto->no_packet_in_rule, rule);
4497 }
4498
4499 bool
4500 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
4501                           const struct flow *flow, struct flow_wildcards *wc,
4502                           uint8_t table_id, struct rule_dpif **rule)
4503 {
4504     const struct cls_rule *cls_rule;
4505     struct classifier *cls;
4506     bool frag;
4507
4508     *rule = NULL;
4509     if (table_id >= N_TABLES) {
4510         return false;
4511     }
4512
4513     if (wc) {
4514         memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4515         wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4516     }
4517
4518     cls = &ofproto->up.tables[table_id].cls;
4519     ovs_rwlock_rdlock(&cls->rwlock);
4520     frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
4521     if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
4522         /* We must pretend that transport ports are unavailable. */
4523         struct flow ofpc_normal_flow = *flow;
4524         ofpc_normal_flow.tp_src = htons(0);
4525         ofpc_normal_flow.tp_dst = htons(0);
4526         cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
4527     } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
4528         cls_rule = &ofproto->drop_frags_rule->up.cr;
4529         /* Frag mask in wc already set above. */
4530     } else {
4531         cls_rule = classifier_lookup(cls, flow, wc);
4532     }
4533
4534     *rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
4535     rule_dpif_ref(*rule);
4536     ovs_rwlock_unlock(&cls->rwlock);
4537
4538     return *rule != NULL;
4539 }
4540
4541 /* Given a port configuration (specified as zero if there's no port), chooses
4542  * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
4543  * flow table miss. */
4544 void
4545 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
4546                  struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
4547 {
4548     *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
4549     rule_dpif_ref(*rule);
4550 }
4551
4552 void
4553 rule_dpif_ref(struct rule_dpif *rule)
4554 {
4555     if (rule) {
4556         ofproto_rule_ref(&rule->up);
4557     }
4558 }
4559
4560 void
4561 rule_dpif_unref(struct rule_dpif *rule)
4562 {
4563     if (rule) {
4564         ofproto_rule_unref(&rule->up);
4565     }
4566 }
4567
4568 static void
4569 complete_operation(struct rule_dpif *rule)
4570     OVS_REQUIRES(ofproto_mutex)
4571 {
4572     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4573
4574     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4575     ofoperation_complete(rule->up.pending, 0);
4576 }
4577
4578 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
4579 {
4580     return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
4581 }
4582
4583 static struct rule *
4584 rule_alloc(void)
4585 {
4586     struct rule_dpif *rule = xmalloc(sizeof *rule);
4587     return &rule->up;
4588 }
4589
4590 static void
4591 rule_dealloc(struct rule *rule_)
4592 {
4593     struct rule_dpif *rule = rule_dpif_cast(rule_);
4594     free(rule);
4595 }
4596
4597 static enum ofperr
4598 rule_construct(struct rule *rule_)
4599 {
4600     struct rule_dpif *rule = rule_dpif_cast(rule_);
4601     ovs_mutex_init(&rule->stats_mutex);
4602     ovs_mutex_lock(&rule->stats_mutex);
4603     rule->packet_count = 0;
4604     rule->byte_count = 0;
4605     ovs_mutex_unlock(&rule->stats_mutex);
4606     return 0;
4607 }
4608
4609 static void
4610 rule_insert(struct rule *rule_)
4611     OVS_REQUIRES(ofproto_mutex)
4612 {
4613     struct rule_dpif *rule = rule_dpif_cast(rule_);
4614     complete_operation(rule);
4615 }
4616
4617 static void
4618 rule_delete(struct rule *rule_)
4619     OVS_REQUIRES(ofproto_mutex)
4620 {
4621     struct rule_dpif *rule = rule_dpif_cast(rule_);
4622     complete_operation(rule);
4623 }
4624
4625 static void
4626 rule_destruct(struct rule *rule_)
4627 {
4628     struct rule_dpif *rule = rule_dpif_cast(rule_);
4629     ovs_mutex_destroy(&rule->stats_mutex);
4630 }
4631
4632 static void
4633 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
4634 {
4635     struct rule_dpif *rule = rule_dpif_cast(rule_);
4636
4637     push_all_stats();
4638
4639     /* Start from historical data for 'rule' itself that are no longer tracked
4640      * in facets.  This counts, for example, facets that have expired. */
4641     ovs_mutex_lock(&rule->stats_mutex);
4642     *packets = rule->packet_count;
4643     *bytes = rule->byte_count;
4644     ovs_mutex_unlock(&rule->stats_mutex);
4645 }
4646
4647 static void
4648 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4649                   struct ofpbuf *packet)
4650 {
4651     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4652
4653     ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4654 }
4655
4656 static enum ofperr
4657 rule_execute(struct rule *rule, const struct flow *flow,
4658              struct ofpbuf *packet)
4659 {
4660     rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4661     ofpbuf_delete(packet);
4662     return 0;
4663 }
4664
4665 static void
4666 rule_modify_actions(struct rule *rule_, bool reset_counters)
4667     OVS_REQUIRES(ofproto_mutex)
4668 {
4669     struct rule_dpif *rule = rule_dpif_cast(rule_);
4670
4671     if (reset_counters) {
4672         ovs_mutex_lock(&rule->stats_mutex);
4673         rule->packet_count = 0;
4674         rule->byte_count = 0;
4675         ovs_mutex_unlock(&rule->stats_mutex);
4676     }
4677
4678     complete_operation(rule);
4679 }
4680
4681 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4682 {
4683     return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4684 }
4685
4686 static struct ofgroup *
4687 group_alloc(void)
4688 {
4689     struct group_dpif *group = xzalloc(sizeof *group);
4690     return &group->up;
4691 }
4692
4693 static void
4694 group_dealloc(struct ofgroup *group_)
4695 {
4696     struct group_dpif *group = group_dpif_cast(group_);
4697     free(group);
4698 }
4699
4700 static void
4701 group_construct_stats(struct group_dpif *group)
4702     OVS_REQUIRES(group->stats_mutex)
4703 {
4704     group->packet_count = 0;
4705     group->byte_count = 0;
4706     if (!group->bucket_stats) {
4707         group->bucket_stats = xcalloc(group->up.n_buckets,
4708                                       sizeof *group->bucket_stats);
4709     } else {
4710         memset(group->bucket_stats, 0, group->up.n_buckets *
4711                sizeof *group->bucket_stats);
4712     }
4713 }
4714
4715 static enum ofperr
4716 group_construct(struct ofgroup *group_)
4717 {
4718     struct group_dpif *group = group_dpif_cast(group_);
4719     ovs_mutex_init(&group->stats_mutex);
4720     ovs_mutex_lock(&group->stats_mutex);
4721     group_construct_stats(group);
4722     ovs_mutex_unlock(&group->stats_mutex);
4723     return 0;
4724 }
4725
4726 static void
4727 group_destruct__(struct group_dpif *group)
4728     OVS_REQUIRES(group->stats_mutex)
4729 {
4730     free(group->bucket_stats);
4731     group->bucket_stats = NULL;
4732 }
4733
4734 static void
4735 group_destruct(struct ofgroup *group_)
4736 {
4737     struct group_dpif *group = group_dpif_cast(group_);
4738     ovs_mutex_lock(&group->stats_mutex);
4739     group_destruct__(group);
4740     ovs_mutex_unlock(&group->stats_mutex);
4741     ovs_mutex_destroy(&group->stats_mutex);
4742 }
4743
4744 static enum ofperr
4745 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
4746 {
4747     struct group_dpif *group = group_dpif_cast(group_);
4748     struct group_dpif *victim = group_dpif_cast(victim_);
4749
4750     ovs_mutex_lock(&group->stats_mutex);
4751     if (victim->up.n_buckets < group->up.n_buckets) {
4752         group_destruct__(group);
4753     }
4754     group_construct_stats(group);
4755     ovs_mutex_unlock(&group->stats_mutex);
4756
4757     return 0;
4758 }
4759
4760 static enum ofperr
4761 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4762 {
4763     struct group_dpif *group = group_dpif_cast(group_);
4764
4765     /* Start from historical data for 'group' itself that are no longer tracked
4766      * in facets.  This counts, for example, facets that have expired. */
4767     ovs_mutex_lock(&group->stats_mutex);
4768     ogs->packet_count = group->packet_count;
4769     ogs->byte_count = group->byte_count;
4770     memcpy(ogs->bucket_stats, group->bucket_stats,
4771            group->up.n_buckets * sizeof *group->bucket_stats);
4772     ovs_mutex_unlock(&group->stats_mutex);
4773
4774     return 0;
4775 }
4776
4777 bool
4778 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4779                   struct group_dpif **group)
4780     OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
4781 {
4782     struct ofgroup *ofgroup;
4783     bool found;
4784
4785     *group = NULL;
4786     found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4787     *group = found ?  group_dpif_cast(ofgroup) : NULL;
4788
4789     return found;
4790 }
4791
4792 void
4793 group_dpif_release(struct group_dpif *group)
4794     OVS_RELEASES(group->up.rwlock)
4795 {
4796     ofproto_group_release(&group->up);
4797 }
4798
4799 void
4800 group_dpif_get_buckets(const struct group_dpif *group,
4801                        const struct list **buckets)
4802 {
4803     *buckets = &group->up.buckets;
4804 }
4805
4806 enum ofp11_group_type
4807 group_dpif_get_type(const struct group_dpif *group)
4808 {
4809     return group->up.type;
4810 }
4811 \f
4812 /* Sends 'packet' out 'ofport'.
4813  * May modify 'packet'.
4814  * Returns 0 if successful, otherwise a positive errno value. */
4815 int
4816 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4817 {
4818     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4819     int error;
4820
4821     error = xlate_send_packet(ofport, packet);
4822
4823     ovs_mutex_lock(&ofproto->stats_mutex);
4824     ofproto->stats.tx_packets++;
4825     ofproto->stats.tx_bytes += packet->size;
4826     ovs_mutex_unlock(&ofproto->stats_mutex);
4827     return error;
4828 }
4829
4830 /* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
4831  * The action will state 'slow' as the reason that the action is in the slow
4832  * path.  (This is purely informational: it allows a human viewing "ovs-dpctl
4833  * dump-flows" output to see why a flow is in the slow path.)
4834  *
4835  * The 'stub_size' bytes in 'stub' will be used to store the action.
4836  * 'stub_size' must be large enough for the action.
4837  *
4838  * The action and its size will be stored in '*actionsp' and '*actions_lenp',
4839  * respectively. */
4840 static void
4841 compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
4842                   enum slow_path_reason slow,
4843                   uint64_t *stub, size_t stub_size,
4844                   const struct nlattr **actionsp, size_t *actions_lenp)
4845 {
4846     union user_action_cookie cookie;
4847     struct ofpbuf buf;
4848
4849     cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
4850     cookie.slow_path.unused = 0;
4851     cookie.slow_path.reason = slow;
4852
4853     ofpbuf_use_stack(&buf, stub, stub_size);
4854     if (slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)) {
4855         uint32_t pid = dpif_port_get_pid(ofproto->backer->dpif,
4856                                          ODPP_NONE);
4857         odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, &buf);
4858     } else {
4859         odp_port_t odp_port;
4860         uint32_t pid;
4861
4862         odp_port = ofp_port_to_odp_port(ofproto, flow->in_port.ofp_port);
4863         pid = dpif_port_get_pid(ofproto->backer->dpif, odp_port);
4864         odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, &buf);
4865     }
4866     *actionsp = buf.data;
4867     *actions_lenp = buf.size;
4868 }
4869 \f
4870 static bool
4871 set_frag_handling(struct ofproto *ofproto_,
4872                   enum ofp_config_flags frag_handling)
4873 {
4874     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4875     if (frag_handling != OFPC_FRAG_REASM) {
4876         ofproto->backer->need_revalidate = REV_RECONFIGURE;
4877         return true;
4878     } else {
4879         return false;
4880     }
4881 }
4882
4883 static enum ofperr
4884 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
4885            const struct flow *flow,
4886            const struct ofpact *ofpacts, size_t ofpacts_len)
4887 {
4888     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4889
4890     ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4891                                  ofpacts_len, packet);
4892     return 0;
4893 }
4894 \f
4895 /* NetFlow. */
4896
4897 static int
4898 set_netflow(struct ofproto *ofproto_,
4899             const struct netflow_options *netflow_options)
4900 {
4901     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4902
4903     if (netflow_options) {
4904         if (!ofproto->netflow) {
4905             ofproto->netflow = netflow_create();
4906             ofproto->backer->need_revalidate = REV_RECONFIGURE;
4907         }
4908         return netflow_set_options(ofproto->netflow, netflow_options);
4909     } else if (ofproto->netflow) {
4910         ofproto->backer->need_revalidate = REV_RECONFIGURE;
4911         netflow_unref(ofproto->netflow);
4912         ofproto->netflow = NULL;
4913     }
4914
4915     return 0;
4916 }
4917
4918 static void
4919 get_netflow_ids(const struct ofproto *ofproto_,
4920                 uint8_t *engine_type, uint8_t *engine_id)
4921 {
4922     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4923
4924     dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4925 }
4926 \f
4927 static struct ofproto_dpif *
4928 ofproto_dpif_lookup(const char *name)
4929 {
4930     struct ofproto_dpif *ofproto;
4931
4932     HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4933                              hash_string(name, 0), &all_ofproto_dpifs) {
4934         if (!strcmp(ofproto->up.name, name)) {
4935             return ofproto;
4936         }
4937     }
4938     return NULL;
4939 }
4940
4941 static void
4942 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4943                           const char *argv[], void *aux OVS_UNUSED)
4944 {
4945     struct ofproto_dpif *ofproto;
4946
4947     if (argc > 1) {
4948         ofproto = ofproto_dpif_lookup(argv[1]);
4949         if (!ofproto) {
4950             unixctl_command_reply_error(conn, "no such bridge");
4951             return;
4952         }
4953         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4954         mac_learning_flush(ofproto->ml);
4955         ovs_rwlock_unlock(&ofproto->ml->rwlock);
4956     } else {
4957         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4958             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4959             mac_learning_flush(ofproto->ml);
4960             ovs_rwlock_unlock(&ofproto->ml->rwlock);
4961         }
4962     }
4963
4964     unixctl_command_reply(conn, "table successfully flushed");
4965 }
4966
4967 static struct ofport_dpif *
4968 ofbundle_get_a_port(const struct ofbundle *bundle)
4969 {
4970     return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4971                         bundle_node);
4972 }
4973
4974 static void
4975 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4976                          const char *argv[], void *aux OVS_UNUSED)
4977 {
4978     struct ds ds = DS_EMPTY_INITIALIZER;
4979     const struct ofproto_dpif *ofproto;
4980     const struct mac_entry *e;
4981
4982     ofproto = ofproto_dpif_lookup(argv[1]);
4983     if (!ofproto) {
4984         unixctl_command_reply_error(conn, "no such bridge");
4985         return;
4986     }
4987
4988     ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
4989     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4990     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4991         struct ofbundle *bundle = e->port.p;
4992         char name[OFP_MAX_PORT_NAME_LEN];
4993
4994         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4995                                name, sizeof name);
4996         ds_put_format(&ds, "%5s  %4d  "ETH_ADDR_FMT"  %3d\n",
4997                       name, e->vlan, ETH_ADDR_ARGS(e->mac),
4998                       mac_entry_age(ofproto->ml, e));
4999     }
5000     ovs_rwlock_unlock(&ofproto->ml->rwlock);
5001     unixctl_command_reply(conn, ds_cstr(&ds));
5002     ds_destroy(&ds);
5003 }
5004
5005 struct trace_ctx {
5006     struct xlate_out xout;
5007     struct xlate_in xin;
5008     struct flow flow;
5009     struct ds *result;
5010 };
5011
5012 static void
5013 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
5014 {
5015     struct rule_actions *actions;
5016     ovs_be64 cookie;
5017
5018     ds_put_char_multiple(result, '\t', level);
5019     if (!rule) {
5020         ds_put_cstr(result, "No match\n");
5021         return;
5022     }
5023
5024     ovs_mutex_lock(&rule->up.mutex);
5025     cookie = rule->up.flow_cookie;
5026     ovs_mutex_unlock(&rule->up.mutex);
5027
5028     ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5029                   rule ? rule->up.table_id : 0, ntohll(cookie));
5030     cls_rule_format(&rule->up.cr, result);
5031     ds_put_char(result, '\n');
5032
5033     actions = rule_dpif_get_actions(rule);
5034
5035     ds_put_char_multiple(result, '\t', level);
5036     ds_put_cstr(result, "OpenFlow actions=");
5037     ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
5038     ds_put_char(result, '\n');
5039
5040     rule_actions_unref(actions);
5041 }
5042
5043 static void
5044 trace_format_flow(struct ds *result, int level, const char *title,
5045                   struct trace_ctx *trace)
5046 {
5047     ds_put_char_multiple(result, '\t', level);
5048     ds_put_format(result, "%s: ", title);
5049     if (flow_equal(&trace->xin.flow, &trace->flow)) {
5050         ds_put_cstr(result, "unchanged");
5051     } else {
5052         flow_format(result, &trace->xin.flow);
5053         trace->flow = trace->xin.flow;
5054     }
5055     ds_put_char(result, '\n');
5056 }
5057
5058 static void
5059 trace_format_regs(struct ds *result, int level, const char *title,
5060                   struct trace_ctx *trace)
5061 {
5062     size_t i;
5063
5064     ds_put_char_multiple(result, '\t', level);
5065     ds_put_format(result, "%s:", title);
5066     for (i = 0; i < FLOW_N_REGS; i++) {
5067         ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
5068     }
5069     ds_put_char(result, '\n');
5070 }
5071
5072 static void
5073 trace_format_odp(struct ds *result, int level, const char *title,
5074                  struct trace_ctx *trace)
5075 {
5076     struct ofpbuf *odp_actions = &trace->xout.odp_actions;
5077
5078     ds_put_char_multiple(result, '\t', level);
5079     ds_put_format(result, "%s: ", title);
5080     format_odp_actions(result, odp_actions->data, odp_actions->size);
5081     ds_put_char(result, '\n');
5082 }
5083
5084 static void
5085 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
5086 {
5087     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
5088     struct ds *result = trace->result;
5089
5090     ds_put_char(result, '\n');
5091     trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
5092     trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
5093     trace_format_odp(result,  recurse + 1, "Resubmitted  odp", trace);
5094     trace_format_rule(result, recurse + 1, rule);
5095 }
5096
5097 static void
5098 trace_report(struct xlate_in *xin, const char *s, int recurse)
5099 {
5100     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
5101     struct ds *result = trace->result;
5102
5103     ds_put_char_multiple(result, '\t', recurse);
5104     ds_put_cstr(result, s);
5105     ds_put_char(result, '\n');
5106 }
5107
5108 /* Parses the 'argc' elements of 'argv', ignoring argv[0].  The following
5109  * forms are supported:
5110  *
5111  *     - [dpname] odp_flow [-generate | packet]
5112  *     - bridge br_flow [-generate | packet]
5113  *
5114  * On success, initializes '*ofprotop' and 'flow' and returns NULL.  On failure
5115  * returns a nonnull malloced error message. */
5116 static char * WARN_UNUSED_RESULT
5117 parse_flow_and_packet(int argc, const char *argv[],
5118                       struct ofproto_dpif **ofprotop, struct flow *flow,
5119                       struct ofpbuf **packetp)
5120 {
5121     const struct dpif_backer *backer = NULL;
5122     const char *error = NULL;
5123     char *m_err = NULL;
5124     struct simap port_names = SIMAP_INITIALIZER(&port_names);
5125     struct ofpbuf *packet;
5126     struct ofpbuf odp_key;
5127     struct ofpbuf odp_mask;
5128
5129     ofpbuf_init(&odp_key, 0);
5130     ofpbuf_init(&odp_mask, 0);
5131
5132     /* Handle "-generate" or a hex string as the last argument. */
5133     if (!strcmp(argv[argc - 1], "-generate")) {
5134         packet = ofpbuf_new(0);
5135         argc--;
5136     } else {
5137         error = eth_from_hex(argv[argc - 1], &packet);
5138         if (!error) {
5139             argc--;
5140         } else if (argc == 4) {
5141             /* The 3-argument form must end in "-generate' or a hex string. */
5142             goto exit;
5143         }
5144         error = NULL;
5145     }
5146
5147     /* odp_flow can have its in_port specified as a name instead of port no.
5148      * We do not yet know whether a given flow is a odp_flow or a br_flow.
5149      * But, to know whether a flow is odp_flow through odp_flow_from_string(),
5150      * we need to create a simap of name to port no. */
5151     if (argc == 3) {
5152         const char *dp_type;
5153         if (!strncmp(argv[1], "ovs-", 4)) {
5154             dp_type = argv[1] + 4;
5155         } else {
5156             dp_type = argv[1];
5157         }
5158         backer = shash_find_data(&all_dpif_backers, dp_type);
5159     } else if (argc == 2) {
5160         struct shash_node *node;
5161         if (shash_count(&all_dpif_backers) == 1) {
5162             node = shash_first(&all_dpif_backers);
5163             backer = node->data;
5164         }
5165     } else {
5166         error = "Syntax error";
5167         goto exit;
5168     }
5169     if (backer && backer->dpif) {
5170         struct dpif_port dpif_port;
5171         struct dpif_port_dump port_dump;
5172         DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
5173             simap_put(&port_names, dpif_port.name,
5174                       odp_to_u32(dpif_port.port_no));
5175         }
5176     }
5177
5178     /* Parse the flow and determine whether a datapath or
5179      * bridge is specified. If function odp_flow_key_from_string()
5180      * returns 0, the flow is a odp_flow. If function
5181      * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
5182     if (!odp_flow_from_string(argv[argc - 1], &port_names,
5183                               &odp_key, &odp_mask)) {
5184         if (!backer) {
5185             error = "Cannot find the datapath";
5186             goto exit;
5187         }
5188
5189         if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
5190                           NULL, ofprotop, NULL, NULL, NULL, NULL)) {
5191             error = "Invalid datapath flow";
5192             goto exit;
5193         }
5194     } else {
5195         char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
5196
5197         if (err) {
5198             m_err = xasprintf("Bad flow syntax: %s", err);
5199             free(err);
5200             goto exit;
5201         } else {
5202             if (argc != 3) {
5203                 error = "Must specify bridge name";
5204                 goto exit;
5205             }
5206
5207             *ofprotop = ofproto_dpif_lookup(argv[1]);
5208             if (!*ofprotop) {
5209                 error = "Unknown bridge name";
5210                 goto exit;
5211             }
5212         }
5213     }
5214
5215     /* Generate a packet, if requested. */
5216     if (packet) {
5217         if (!packet->size) {
5218             flow_compose(packet, flow);
5219         } else {
5220             union flow_in_port in_port = flow->in_port;
5221
5222             /* Use the metadata from the flow and the packet argument
5223              * to reconstruct the flow. */
5224             flow_extract(packet, flow->skb_priority, flow->pkt_mark, NULL,
5225                          &in_port, flow);
5226         }
5227     }
5228
5229 exit:
5230     if (error && !m_err) {
5231         m_err = xstrdup(error);
5232     }
5233     if (m_err) {
5234         ofpbuf_delete(packet);
5235         packet = NULL;
5236     }
5237     *packetp = packet;
5238     ofpbuf_uninit(&odp_key);
5239     ofpbuf_uninit(&odp_mask);
5240     simap_destroy(&port_names);
5241     return m_err;
5242 }
5243
5244 static void
5245 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
5246                       void *aux OVS_UNUSED)
5247 {
5248     struct ofproto_dpif *ofproto;
5249     struct ofpbuf *packet;
5250     char *error;
5251     struct flow flow;
5252
5253     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
5254     if (!error) {
5255         struct ds result;
5256
5257         ds_init(&result);
5258         ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
5259         unixctl_command_reply(conn, ds_cstr(&result));
5260         ds_destroy(&result);
5261         ofpbuf_delete(packet);
5262     } else {
5263         unixctl_command_reply_error(conn, error);
5264         free(error);
5265     }
5266 }
5267
5268 static void
5269 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
5270                               const char *argv[], void *aux OVS_UNUSED)
5271 {
5272     enum ofputil_protocol usable_protocols;
5273     struct ofproto_dpif *ofproto;
5274     bool enforce_consistency;
5275     struct ofpbuf ofpacts;
5276     struct ofpbuf *packet;
5277     struct ds result;
5278     struct flow flow;
5279     uint16_t in_port;
5280
5281     /* Three kinds of error return values! */
5282     enum ofperr retval;
5283     char *error;
5284
5285     packet = NULL;
5286     ds_init(&result);
5287     ofpbuf_init(&ofpacts, 0);
5288
5289     /* Parse actions. */
5290     error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
5291     if (error) {
5292         unixctl_command_reply_error(conn, error);
5293         free(error);
5294         goto exit;
5295     }
5296
5297     /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
5298      * consistency between the flow and the actions.  With -consistent, we
5299      * enforce consistency even for a flow supported in OpenFlow 1.0. */
5300     if (!strcmp(argv[1], "-consistent")) {
5301         enforce_consistency = true;
5302         argv++;
5303         argc--;
5304     } else {
5305         enforce_consistency = false;
5306     }
5307
5308     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
5309     if (error) {
5310         unixctl_command_reply_error(conn, error);
5311         free(error);
5312         goto exit;
5313     }
5314
5315     /* Do the same checks as handle_packet_out() in ofproto.c.
5316      *
5317      * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
5318      * strictly correct because these actions aren't in any table, but it's OK
5319      * because it 'table_id' is used only to check goto_table instructions, but
5320      * packet-outs take a list of actions and therefore it can't include
5321      * instructions.
5322      *
5323      * We skip the "meter" check here because meter is an instruction, not an
5324      * action, and thus cannot appear in ofpacts. */
5325     in_port = ofp_to_u16(flow.in_port.ofp_port);
5326     if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
5327         unixctl_command_reply_error(conn, "invalid in_port");
5328         goto exit;
5329     }
5330     if (enforce_consistency) {
5331         retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
5332                                            u16_to_ofp(ofproto->up.max_ports),
5333                                            0, 0, usable_protocols);
5334     } else {
5335         retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
5336                                u16_to_ofp(ofproto->up.max_ports), 0, 0,
5337                                &usable_protocols);
5338     }
5339
5340     if (retval) {
5341         ds_clear(&result);
5342         ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
5343         unixctl_command_reply_error(conn, ds_cstr(&result));
5344         goto exit;
5345     }
5346
5347     ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
5348     unixctl_command_reply(conn, ds_cstr(&result));
5349
5350 exit:
5351     ds_destroy(&result);
5352     ofpbuf_delete(packet);
5353     ofpbuf_uninit(&ofpacts);
5354 }
5355
5356 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
5357  * description of the results to 'ds'.
5358  *
5359  * The trace follows a packet with the specified 'flow' through the flow
5360  * table.  'packet' may be nonnull to trace an actual packet, with consequent
5361  * side effects (if it is nonnull then its flow must be 'flow').
5362  *
5363  * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
5364  * trace, otherwise the actions are determined by a flow table lookup. */
5365 static void
5366 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
5367               const struct ofpbuf *packet,
5368               const struct ofpact ofpacts[], size_t ofpacts_len,
5369               struct ds *ds)
5370 {
5371     struct rule_dpif *rule;
5372     struct flow_wildcards wc;
5373
5374     ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
5375     ds_put_cstr(ds, "Flow: ");
5376     flow_format(ds, flow);
5377     ds_put_char(ds, '\n');
5378
5379     flow_wildcards_init_catchall(&wc);
5380     if (ofpacts) {
5381         rule = NULL;
5382     } else {
5383         rule_dpif_lookup(ofproto, flow, &wc, &rule);
5384
5385         trace_format_rule(ds, 0, rule);
5386         if (rule == ofproto->miss_rule) {
5387             ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
5388         } else if (rule == ofproto->no_packet_in_rule) {
5389             ds_put_cstr(ds, "\nNo match, packets dropped because "
5390                         "OFPPC_NO_PACKET_IN is set on in_port.\n");
5391         } else if (rule == ofproto->drop_frags_rule) {
5392             ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
5393                         "and the fragment handling mode is \"drop\".\n");
5394         }
5395     }
5396
5397     if (rule || ofpacts) {
5398         uint64_t odp_actions_stub[1024 / 8];
5399         struct ofpbuf odp_actions;
5400         struct trace_ctx trace;
5401         struct match match;
5402         uint16_t tcp_flags;
5403
5404         tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
5405         trace.result = ds;
5406         trace.flow = *flow;
5407         ofpbuf_use_stub(&odp_actions,
5408                         odp_actions_stub, sizeof odp_actions_stub);
5409         xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
5410         if (ofpacts) {
5411             trace.xin.ofpacts = ofpacts;
5412             trace.xin.ofpacts_len = ofpacts_len;
5413         }
5414         trace.xin.resubmit_hook = trace_resubmit;
5415         trace.xin.report_hook = trace_report;
5416
5417         xlate_actions(&trace.xin, &trace.xout);
5418         flow_wildcards_or(&trace.xout.wc, &trace.xout.wc, &wc);
5419
5420         ds_put_char(ds, '\n');
5421         trace_format_flow(ds, 0, "Final flow", &trace);
5422
5423         match_init(&match, flow, &trace.xout.wc);
5424         ds_put_cstr(ds, "Relevant fields: ");
5425         match_format(&match, ds, OFP_DEFAULT_PRIORITY);
5426         ds_put_char(ds, '\n');
5427
5428         ds_put_cstr(ds, "Datapath actions: ");
5429         format_odp_actions(ds, trace.xout.odp_actions.data,
5430                            trace.xout.odp_actions.size);
5431
5432         if (trace.xout.slow) {
5433             enum slow_path_reason slow;
5434
5435             ds_put_cstr(ds, "\nThis flow is handled by the userspace "
5436                         "slow path because it:");
5437
5438             slow = trace.xout.slow;
5439             while (slow) {
5440                 enum slow_path_reason bit = rightmost_1bit(slow);
5441
5442                 ds_put_format(ds, "\n\t- %s.",
5443                               slow_path_reason_to_explanation(bit));
5444
5445                 slow &= ~bit;
5446             }
5447         }
5448
5449         xlate_out_uninit(&trace.xout);
5450     }
5451
5452     rule_dpif_unref(rule);
5453 }
5454
5455 /* Runs a self-check of flow translations in 'ofproto'.  Appends a message to
5456  * 'reply' describing the results. */
5457 static void
5458 ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
5459 {
5460     struct cls_cursor cursor;
5461     struct facet *facet;
5462     int errors;
5463
5464     errors = 0;
5465     ovs_rwlock_rdlock(&ofproto->facets.rwlock);
5466     cls_cursor_init(&cursor, &ofproto->facets, NULL);
5467     CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
5468         if (!facet_check_consistency(facet)) {
5469             errors++;
5470         }
5471     }
5472     ovs_rwlock_unlock(&ofproto->facets.rwlock);
5473     if (errors) {
5474         ofproto->backer->need_revalidate = REV_INCONSISTENCY;
5475     }
5476
5477     if (errors) {
5478         ds_put_format(reply, "%s: self-check failed (%d errors)\n",
5479                       ofproto->up.name, errors);
5480     } else {
5481         ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
5482     }
5483 }
5484
5485 static void
5486 ofproto_dpif_self_check(struct unixctl_conn *conn,
5487                         int argc, const char *argv[], void *aux OVS_UNUSED)
5488 {
5489     struct ds reply = DS_EMPTY_INITIALIZER;
5490     struct ofproto_dpif *ofproto;
5491
5492     if (argc > 1) {
5493         ofproto = ofproto_dpif_lookup(argv[1]);
5494         if (!ofproto) {
5495             unixctl_command_reply_error(conn, "Unknown ofproto (use "
5496                                         "ofproto/list for help)");
5497             return;
5498         }
5499         ofproto_dpif_self_check__(ofproto, &reply);
5500     } else {
5501         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5502             ofproto_dpif_self_check__(ofproto, &reply);
5503         }
5504     }
5505
5506     unixctl_command_reply(conn, ds_cstr(&reply));
5507     ds_destroy(&reply);
5508 }
5509
5510 /* Store the current ofprotos in 'ofproto_shash'.  Returns a sorted list
5511  * of the 'ofproto_shash' nodes.  It is the responsibility of the caller
5512  * to destroy 'ofproto_shash' and free the returned value. */
5513 static const struct shash_node **
5514 get_ofprotos(struct shash *ofproto_shash)
5515 {
5516     const struct ofproto_dpif *ofproto;
5517
5518     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5519         char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5520         shash_add_nocopy(ofproto_shash, name, ofproto);
5521     }
5522
5523     return shash_sort(ofproto_shash);
5524 }
5525
5526 static void
5527 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5528                               const char *argv[] OVS_UNUSED,
5529                               void *aux OVS_UNUSED)
5530 {
5531     struct ds ds = DS_EMPTY_INITIALIZER;
5532     struct shash ofproto_shash;
5533     const struct shash_node **sorted_ofprotos;
5534     int i;
5535
5536     shash_init(&ofproto_shash);
5537     sorted_ofprotos = get_ofprotos(&ofproto_shash);
5538     for (i = 0; i < shash_count(&ofproto_shash); i++) {
5539         const struct shash_node *node = sorted_ofprotos[i];
5540         ds_put_format(&ds, "%s\n", node->name);
5541     }
5542
5543     shash_destroy(&ofproto_shash);
5544     free(sorted_ofprotos);
5545
5546     unixctl_command_reply(conn, ds_cstr(&ds));
5547     ds_destroy(&ds);
5548 }
5549
5550 static void
5551 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5552 {
5553     const struct shash_node **ofprotos;
5554     struct ofproto_dpif *ofproto;
5555     struct shash ofproto_shash;
5556     uint64_t n_hit, n_missed;
5557     size_t i;
5558
5559     n_hit = n_missed = 0;
5560     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5561         if (ofproto->backer == backer) {
5562             n_missed += ofproto->n_missed;
5563             n_hit += ofproto->n_hit;
5564         }
5565     }
5566
5567     ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5568                   dpif_name(backer->dpif), n_hit, n_missed);
5569
5570     ds_put_format(ds, "\tflows: cur: %"PRIuSIZE", avg: %u, max: %u\n",
5571                   hmap_count(&backer->subfacets), backer->avg_n_subfacet,
5572                   backer->max_n_subfacet);
5573
5574     shash_init(&ofproto_shash);
5575     ofprotos = get_ofprotos(&ofproto_shash);
5576     for (i = 0; i < shash_count(&ofproto_shash); i++) {
5577         struct ofproto_dpif *ofproto = ofprotos[i]->data;
5578         const struct shash_node **ports;
5579         size_t j;
5580
5581         if (ofproto->backer != backer) {
5582             continue;
5583         }
5584
5585         ds_put_format(ds, "\t%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5586                       ofproto->up.name, ofproto->n_hit, ofproto->n_missed);
5587
5588         ports = shash_sort(&ofproto->up.port_by_name);
5589         for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5590             const struct shash_node *node = ports[j];
5591             struct ofport *ofport = node->data;
5592             struct smap config;
5593             odp_port_t odp_port;
5594
5595             ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5596                           ofport->ofp_port);
5597
5598             odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5599             if (odp_port != ODPP_NONE) {
5600                 ds_put_format(ds, "%"PRIu32":", odp_port);
5601             } else {
5602                 ds_put_cstr(ds, "none:");
5603             }
5604
5605             ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5606
5607             smap_init(&config);
5608             if (!netdev_get_config(ofport->netdev, &config)) {
5609                 const struct smap_node **nodes;
5610                 size_t i;
5611
5612                 nodes = smap_sort(&config);
5613                 for (i = 0; i < smap_count(&config); i++) {
5614                     const struct smap_node *node = nodes[i];
5615                     ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5616                                   node->key, node->value);
5617                 }
5618                 free(nodes);
5619             }
5620             smap_destroy(&config);
5621
5622             ds_put_char(ds, ')');
5623             ds_put_char(ds, '\n');
5624         }
5625         free(ports);
5626     }
5627     shash_destroy(&ofproto_shash);
5628     free(ofprotos);
5629 }
5630
5631 static void
5632 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5633                           const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5634 {
5635     struct ds ds = DS_EMPTY_INITIALIZER;
5636     const struct shash_node **backers;
5637     int i;
5638
5639     backers = shash_sort(&all_dpif_backers);
5640     for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5641         dpif_show_backer(backers[i]->data, &ds);
5642     }
5643     free(backers);
5644
5645     unixctl_command_reply(conn, ds_cstr(&ds));
5646     ds_destroy(&ds);
5647 }
5648
5649 /* Dump the megaflow (facet) cache.  This is useful to check the
5650  * correctness of flow wildcarding, since the same mechanism is used for
5651  * both xlate caching and kernel wildcarding.
5652  *
5653  * It's important to note that in the output the flow description uses
5654  * OpenFlow (OFP) ports, but the actions use datapath (ODP) ports.
5655  *
5656  * This command is only needed for advanced debugging, so it's not
5657  * documented in the man page. */
5658 static void
5659 ofproto_unixctl_dpif_dump_megaflows(struct unixctl_conn *conn,
5660                                     int argc OVS_UNUSED, const char *argv[],
5661                                     void *aux OVS_UNUSED)
5662 {
5663     struct ds ds = DS_EMPTY_INITIALIZER;
5664     const struct ofproto_dpif *ofproto;
5665     long long int now = time_msec();
5666     struct cls_cursor cursor;
5667     struct facet *facet;
5668
5669     ofproto = ofproto_dpif_lookup(argv[1]);
5670     if (!ofproto) {
5671         unixctl_command_reply_error(conn, "no such bridge");
5672         return;
5673     }
5674
5675     ovs_rwlock_rdlock(&ofproto->facets.rwlock);
5676     cls_cursor_init(&cursor, &ofproto->facets, NULL);
5677     CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
5678         cls_rule_format(&facet->cr, &ds);
5679         ds_put_cstr(&ds, ", ");
5680         ds_put_format(&ds, "n_subfacets:%"PRIuSIZE", ", list_size(&facet->subfacets));
5681         ds_put_format(&ds, "used:%.3fs, ", (now - facet->used) / 1000.0);
5682         ds_put_cstr(&ds, "Datapath actions: ");
5683         if (facet->xout.slow) {
5684             uint64_t slow_path_stub[128 / 8];
5685             const struct nlattr *actions;
5686             size_t actions_len;
5687
5688             compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
5689                               slow_path_stub, sizeof slow_path_stub,
5690                               &actions, &actions_len);
5691             format_odp_actions(&ds, actions, actions_len);
5692         } else {
5693             format_odp_actions(&ds, facet->xout.odp_actions.data,
5694                                facet->xout.odp_actions.size);
5695         }
5696         ds_put_cstr(&ds, "\n");
5697     }
5698     ovs_rwlock_unlock(&ofproto->facets.rwlock);
5699
5700     ds_chomp(&ds, '\n');
5701     unixctl_command_reply(conn, ds_cstr(&ds));
5702     ds_destroy(&ds);
5703 }
5704
5705 /* Disable using the megaflows.
5706  *
5707  * This command is only needed for advanced debugging, so it's not
5708  * documented in the man page. */
5709 static void
5710 ofproto_unixctl_dpif_disable_megaflows(struct unixctl_conn *conn,
5711                                        int argc OVS_UNUSED,
5712                                        const char *argv[] OVS_UNUSED,
5713                                        void *aux OVS_UNUSED)
5714 {
5715     struct ofproto_dpif *ofproto;
5716
5717     enable_megaflows = false;
5718
5719     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5720         flush(&ofproto->up);
5721     }
5722
5723     unixctl_command_reply(conn, "megaflows disabled");
5724 }
5725
5726 /* Re-enable using megaflows.
5727  *
5728  * This command is only needed for advanced debugging, so it's not
5729  * documented in the man page. */
5730 static void
5731 ofproto_unixctl_dpif_enable_megaflows(struct unixctl_conn *conn,
5732                                       int argc OVS_UNUSED,
5733                                       const char *argv[] OVS_UNUSED,
5734                                       void *aux OVS_UNUSED)
5735 {
5736     struct ofproto_dpif *ofproto;
5737
5738     enable_megaflows = true;
5739
5740     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5741         flush(&ofproto->up);
5742     }
5743
5744     unixctl_command_reply(conn, "megaflows enabled");
5745 }
5746
5747 static bool
5748 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
5749                            const struct nlattr *key, size_t key_len)
5750 {
5751     enum odp_key_fitness fitness;
5752     struct ofproto_dpif *ofp;
5753     struct flow flow;
5754
5755     xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &fitness, &ofp,
5756                   NULL, NULL, NULL, NULL);
5757     return ofp == ofproto;
5758 }
5759
5760 static void
5761 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5762                                 int argc OVS_UNUSED, const char *argv[],
5763                                 void *aux OVS_UNUSED)
5764 {
5765     struct ds ds = DS_EMPTY_INITIALIZER;
5766     const struct dpif_flow_stats *stats;
5767     const struct ofproto_dpif *ofproto;
5768     struct dpif_flow_dump flow_dump;
5769     const struct nlattr *actions;
5770     const struct nlattr *mask;
5771     const struct nlattr *key;
5772     size_t actions_len;
5773     size_t mask_len;
5774     size_t key_len;
5775
5776     ofproto = ofproto_dpif_lookup(argv[1]);
5777     if (!ofproto) {
5778         unixctl_command_reply_error(conn, "no such bridge");
5779         return;
5780     }
5781
5782     ds_init(&ds);
5783     dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
5784     while (dpif_flow_dump_next(&flow_dump, &key, &key_len, &mask, &mask_len,
5785                                &actions, &actions_len, &stats)) {
5786         if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
5787             continue;
5788         }
5789
5790         odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, false);
5791         ds_put_cstr(&ds, ", ");
5792         dpif_flow_stats_format(stats, &ds);
5793         ds_put_cstr(&ds, ", actions:");
5794         format_odp_actions(&ds, actions, actions_len);
5795         ds_put_char(&ds, '\n');
5796     }
5797
5798     if (dpif_flow_dump_done(&flow_dump)) {
5799         ds_clear(&ds);
5800         ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5801         unixctl_command_reply_error(conn, ds_cstr(&ds));
5802     } else {
5803         unixctl_command_reply(conn, ds_cstr(&ds));
5804     }
5805     ds_destroy(&ds);
5806 }
5807
5808 static void
5809 ofproto_dpif_unixctl_init(void)
5810 {
5811     static bool registered;
5812     if (registered) {
5813         return;
5814     }
5815     registered = true;
5816
5817     unixctl_command_register(
5818         "ofproto/trace",
5819         "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5820         1, 3, ofproto_unixctl_trace, NULL);
5821     unixctl_command_register(
5822         "ofproto/trace-packet-out",
5823         "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5824         2, 6, ofproto_unixctl_trace_actions, NULL);
5825     unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5826                              ofproto_unixctl_fdb_flush, NULL);
5827     unixctl_command_register("fdb/show", "bridge", 1, 1,
5828                              ofproto_unixctl_fdb_show, NULL);
5829     unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
5830                              ofproto_dpif_self_check, NULL);
5831     unixctl_command_register("dpif/dump-dps", "", 0, 0,
5832                              ofproto_unixctl_dpif_dump_dps, NULL);
5833     unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5834                              NULL);
5835     unixctl_command_register("dpif/dump-flows", "bridge", 1, 1,
5836                              ofproto_unixctl_dpif_dump_flows, NULL);
5837     unixctl_command_register("dpif/dump-megaflows", "bridge", 1, 1,
5838                              ofproto_unixctl_dpif_dump_megaflows, NULL);
5839     unixctl_command_register("dpif/disable-megaflows", "", 0, 0,
5840                              ofproto_unixctl_dpif_disable_megaflows, NULL);
5841     unixctl_command_register("dpif/enable-megaflows", "", 0, 0,
5842                              ofproto_unixctl_dpif_enable_megaflows, NULL);
5843 }
5844 \f
5845 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5846  *
5847  * This is deprecated.  It is only for compatibility with broken device drivers
5848  * in old versions of Linux that do not properly support VLANs when VLAN
5849  * devices are not used.  When broken device drivers are no longer in
5850  * widespread use, we will delete these interfaces. */
5851
5852 static int
5853 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5854 {
5855     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5856     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5857
5858     if (realdev_ofp_port == ofport->realdev_ofp_port
5859         && vid == ofport->vlandev_vid) {
5860         return 0;
5861     }
5862
5863     ofproto->backer->need_revalidate = REV_RECONFIGURE;
5864
5865     if (ofport->realdev_ofp_port) {
5866         vsp_remove(ofport);
5867     }
5868     if (realdev_ofp_port && ofport->bundle) {
5869         /* vlandevs are enslaved to their realdevs, so they are not allowed to
5870          * themselves be part of a bundle. */
5871         bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
5872     }
5873
5874     ofport->realdev_ofp_port = realdev_ofp_port;
5875     ofport->vlandev_vid = vid;
5876
5877     if (realdev_ofp_port) {
5878         vsp_add(ofport, realdev_ofp_port, vid);
5879     }
5880
5881     return 0;
5882 }
5883
5884 static uint32_t
5885 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5886 {
5887     return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5888 }
5889
5890 bool
5891 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5892     OVS_EXCLUDED(ofproto->vsp_mutex)
5893 {
5894     bool ret;
5895
5896     ovs_mutex_lock(&ofproto->vsp_mutex);
5897     ret = !hmap_is_empty(&ofproto->realdev_vid_map);
5898     ovs_mutex_unlock(&ofproto->vsp_mutex);
5899     return ret;
5900 }
5901
5902 static ofp_port_t
5903 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5904                          ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5905     OVS_REQUIRES(ofproto->vsp_mutex)
5906 {
5907     if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5908         int vid = vlan_tci_to_vid(vlan_tci);
5909         const struct vlan_splinter *vsp;
5910
5911         HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5912                                  hash_realdev_vid(realdev_ofp_port, vid),
5913                                  &ofproto->realdev_vid_map) {
5914             if (vsp->realdev_ofp_port == realdev_ofp_port
5915                 && vsp->vid == vid) {
5916                 return vsp->vlandev_ofp_port;
5917             }
5918         }
5919     }
5920     return realdev_ofp_port;
5921 }
5922
5923 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5924  * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5925  * 'struct ofport_dpif'.  For example, given 'realdev_ofp_port' of eth0 and
5926  * 'vlan_tci' 9, it would return the port number of eth0.9.
5927  *
5928  * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5929  * function just returns its 'realdev_ofp_port' argument. */
5930 ofp_port_t
5931 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5932                        ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5933     OVS_EXCLUDED(ofproto->vsp_mutex)
5934 {
5935     ofp_port_t ret;
5936
5937     ovs_mutex_lock(&ofproto->vsp_mutex);
5938     ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5939     ovs_mutex_unlock(&ofproto->vsp_mutex);
5940     return ret;
5941 }
5942
5943 static struct vlan_splinter *
5944 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5945 {
5946     struct vlan_splinter *vsp;
5947
5948     HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5949                              hash_ofp_port(vlandev_ofp_port),
5950                              &ofproto->vlandev_map) {
5951         if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5952             return vsp;
5953         }
5954     }
5955
5956     return NULL;
5957 }
5958
5959 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5960  * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5961  * VLAN VID of the Linux VLAN device in '*vid'.  For example, given
5962  * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5963  * eth0 and store 9 in '*vid'.
5964  *
5965  * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5966  * VLAN device.  Unless VLAN splinters are enabled, this is what this function
5967  * always does.*/
5968 static ofp_port_t
5969 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5970                        ofp_port_t vlandev_ofp_port, int *vid)
5971     OVS_REQUIRES(ofproto->vsp_mutex)
5972 {
5973     if (!hmap_is_empty(&ofproto->vlandev_map)) {
5974         const struct vlan_splinter *vsp;
5975
5976         vsp = vlandev_find(ofproto, vlandev_ofp_port);
5977         if (vsp) {
5978             if (vid) {
5979                 *vid = vsp->vid;
5980             }
5981             return vsp->realdev_ofp_port;
5982         }
5983     }
5984     return 0;
5985 }
5986
5987 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5988  * whether 'flow->in_port' represents a Linux VLAN device.  If so, changes
5989  * 'flow->in_port' to the "real" device backing the VLAN device, sets
5990  * 'flow->vlan_tci' to the VLAN VID, and returns true.  Otherwise (which is
5991  * always the case unless VLAN splinters are enabled), returns false without
5992  * making any changes. */
5993 bool
5994 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
5995     OVS_EXCLUDED(ofproto->vsp_mutex)
5996 {
5997     ofp_port_t realdev;
5998     int vid;
5999
6000     ovs_mutex_lock(&ofproto->vsp_mutex);
6001     realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
6002     ovs_mutex_unlock(&ofproto->vsp_mutex);
6003     if (!realdev) {
6004         return false;
6005     }
6006
6007     /* Cause the flow to be processed as if it came in on the real device with
6008      * the VLAN device's VLAN ID. */
6009     flow->in_port.ofp_port = realdev;
6010     flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
6011     return true;
6012 }
6013
6014 static void
6015 vsp_remove(struct ofport_dpif *port)
6016 {
6017     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6018     struct vlan_splinter *vsp;
6019
6020     ovs_mutex_lock(&ofproto->vsp_mutex);
6021     vsp = vlandev_find(ofproto, port->up.ofp_port);
6022     if (vsp) {
6023         hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
6024         hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
6025         free(vsp);
6026
6027         port->realdev_ofp_port = 0;
6028     } else {
6029         VLOG_ERR("missing vlan device record");
6030     }
6031     ovs_mutex_unlock(&ofproto->vsp_mutex);
6032 }
6033
6034 static void
6035 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
6036 {
6037     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6038
6039     ovs_mutex_lock(&ofproto->vsp_mutex);
6040     if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
6041         && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
6042             == realdev_ofp_port)) {
6043         struct vlan_splinter *vsp;
6044
6045         vsp = xmalloc(sizeof *vsp);
6046         vsp->realdev_ofp_port = realdev_ofp_port;
6047         vsp->vlandev_ofp_port = port->up.ofp_port;
6048         vsp->vid = vid;
6049
6050         port->realdev_ofp_port = realdev_ofp_port;
6051
6052         hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
6053                     hash_ofp_port(port->up.ofp_port));
6054         hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
6055                     hash_realdev_vid(realdev_ofp_port, vid));
6056     } else {
6057         VLOG_ERR("duplicate vlan device record");
6058     }
6059     ovs_mutex_unlock(&ofproto->vsp_mutex);
6060 }
6061
6062 static odp_port_t
6063 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
6064 {
6065     const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
6066     return ofport ? ofport->odp_port : ODPP_NONE;
6067 }
6068
6069 struct ofport_dpif *
6070 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
6071 {
6072     struct ofport_dpif *port;
6073
6074     ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
6075     HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
6076                              &backer->odp_to_ofport_map) {
6077         if (port->odp_port == odp_port) {
6078             ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
6079             return port;
6080         }
6081     }
6082
6083     ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
6084     return NULL;
6085 }
6086
6087 static ofp_port_t
6088 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
6089 {
6090     struct ofport_dpif *port;
6091
6092     port = odp_port_to_ofport(ofproto->backer, odp_port);
6093     if (port && &ofproto->up == port->up.ofproto) {
6094         return port->up.ofp_port;
6095     } else {
6096         return OFPP_NONE;
6097     }
6098 }
6099
6100 const struct ofproto_class ofproto_dpif_class = {
6101     init,
6102     enumerate_types,
6103     enumerate_names,
6104     del,
6105     port_open_type,
6106     type_run,
6107     type_wait,
6108     alloc,
6109     construct,
6110     destruct,
6111     dealloc,
6112     run,
6113     wait,
6114     get_memory_usage,
6115     type_get_memory_usage,
6116     flush,
6117     get_features,
6118     get_tables,
6119     port_alloc,
6120     port_construct,
6121     port_destruct,
6122     port_dealloc,
6123     port_modified,
6124     port_reconfigured,
6125     port_query_by_name,
6126     port_add,
6127     port_del,
6128     port_get_stats,
6129     port_dump_start,
6130     port_dump_next,
6131     port_dump_done,
6132     port_poll,
6133     port_poll_wait,
6134     port_is_lacp_current,
6135     NULL,                       /* rule_choose_table */
6136     rule_alloc,
6137     rule_construct,
6138     rule_insert,
6139     rule_delete,
6140     rule_destruct,
6141     rule_dealloc,
6142     rule_get_stats,
6143     rule_execute,
6144     rule_modify_actions,
6145     set_frag_handling,
6146     packet_out,
6147     set_netflow,
6148     get_netflow_ids,
6149     set_sflow,
6150     set_ipfix,
6151     set_cfm,
6152     get_cfm_status,
6153     set_bfd,
6154     get_bfd_status,
6155     set_stp,
6156     get_stp_status,
6157     set_stp_port,
6158     get_stp_port_status,
6159     get_stp_port_stats,
6160     set_queues,
6161     bundle_set,
6162     bundle_remove,
6163     mirror_set__,
6164     mirror_get_stats__,
6165     set_flood_vlans,
6166     is_mirror_output_bundle,
6167     forward_bpdu_changed,
6168     set_mac_table_config,
6169     set_realdev,
6170     NULL,                       /* meter_get_features */
6171     NULL,                       /* meter_set */
6172     NULL,                       /* meter_get */
6173     NULL,                       /* meter_del */
6174     group_alloc,                /* group_alloc */
6175     group_construct,            /* group_construct */
6176     group_destruct,             /* group_destruct */
6177     group_dealloc,              /* group_dealloc */
6178     group_modify,               /* group_modify */
6179     group_get_stats,            /* group_get_stats */
6180 };