48d0b820538dd1ba48cfb7f9821d5d9fd8d44de1
[cascardo/ovs.git] / ofproto / ofproto-dpif.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 "mcast-snooping.h"
41 #include "meta-flow.h"
42 #include "multipath.h"
43 #include "netdev-vport.h"
44 #include "netdev.h"
45 #include "netlink.h"
46 #include "nx-match.h"
47 #include "odp-util.h"
48 #include "odp-execute.h"
49 #include "ofp-util.h"
50 #include "ofpbuf.h"
51 #include "ofp-actions.h"
52 #include "ofp-parse.h"
53 #include "ofp-print.h"
54 #include "ofproto-dpif-ipfix.h"
55 #include "ofproto-dpif-mirror.h"
56 #include "ofproto-dpif-monitor.h"
57 #include "ofproto-dpif-rid.h"
58 #include "ofproto-dpif-sflow.h"
59 #include "ofproto-dpif-upcall.h"
60 #include "ofproto-dpif-xlate.h"
61 #include "poll-loop.h"
62 #include "seq.h"
63 #include "simap.h"
64 #include "smap.h"
65 #include "timer.h"
66 #include "tunnel.h"
67 #include "unaligned.h"
68 #include "unixctl.h"
69 #include "vlan-bitmap.h"
70 #include "vlog.h"
71
72 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
73
74 COVERAGE_DEFINE(ofproto_dpif_expired);
75 COVERAGE_DEFINE(packet_in_overflow);
76
77 struct flow_miss;
78
79 struct rule_dpif {
80     struct rule up;
81
82     /* These statistics:
83      *
84      *   - Do include packets and bytes from datapath flows which have not
85      *   recently been processed by a revalidator. */
86     struct ovs_mutex stats_mutex;
87     struct dpif_flow_stats stats OVS_GUARDED;
88
89     /* If non-zero then the recirculation id that has
90      * been allocated for use with this rule.
91      * The recirculation id and associated internal flow should
92      * be freed when the rule is freed */
93     uint32_t recirc_id;
94 };
95
96 /* RULE_CAST() depends on this. */
97 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
98
99 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
100                            long long int *used);
101 static struct rule_dpif *rule_dpif_cast(const struct rule *);
102 static void rule_expire(struct rule_dpif *);
103
104 struct group_dpif {
105     struct ofgroup up;
106
107     /* These statistics:
108      *
109      *   - Do include packets and bytes from datapath flows which have not
110      *   recently been processed by a revalidator. */
111     struct ovs_mutex stats_mutex;
112     uint64_t packet_count OVS_GUARDED;  /* Number of packets received. */
113     uint64_t byte_count OVS_GUARDED;    /* Number of bytes received. */
114 };
115
116 struct ofbundle {
117     struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
118     struct ofproto_dpif *ofproto; /* Owning ofproto. */
119     void *aux;                  /* Key supplied by ofproto's client. */
120     char *name;                 /* Identifier for log messages. */
121
122     /* Configuration. */
123     struct list ports;          /* Contains "struct ofport"s. */
124     enum port_vlan_mode vlan_mode; /* VLAN mode */
125     int vlan;                   /* -1=trunk port, else a 12-bit VLAN ID. */
126     unsigned long *trunks;      /* Bitmap of trunked VLANs, if 'vlan' == -1.
127                                  * NULL if all VLANs are trunked. */
128     struct lacp *lacp;          /* LACP if LACP is enabled, otherwise NULL. */
129     struct bond *bond;          /* Nonnull iff more than one port. */
130     bool use_priority_tags;     /* Use 802.1p tag for frames in VLAN 0? */
131
132     /* Status. */
133     bool floodable;          /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
134 };
135
136 static void bundle_remove(struct ofport *);
137 static void bundle_update(struct ofbundle *);
138 static void bundle_destroy(struct ofbundle *);
139 static void bundle_del_port(struct ofport_dpif *);
140 static void bundle_run(struct ofbundle *);
141 static void bundle_wait(struct ofbundle *);
142
143 static void stp_run(struct ofproto_dpif *ofproto);
144 static void stp_wait(struct ofproto_dpif *ofproto);
145 static int set_stp_port(struct ofport *,
146                         const struct ofproto_port_stp_settings *);
147
148 struct ofport_dpif {
149     struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
150     struct ofport up;
151
152     odp_port_t odp_port;
153     struct ofbundle *bundle;    /* Bundle that contains this port, if any. */
154     struct list bundle_node;    /* In struct ofbundle's "ports" list. */
155     struct cfm *cfm;            /* Connectivity Fault Management, if any. */
156     struct bfd *bfd;            /* BFD, if any. */
157     bool may_enable;            /* May be enabled in bonds. */
158     bool is_tunnel;             /* This port is a tunnel. */
159     bool is_layer3;             /* This is a layer 3 port. */
160     long long int carrier_seq;  /* Carrier status changes. */
161     struct ofport_dpif *peer;   /* Peer if patch port. */
162
163     /* Spanning tree. */
164     struct stp_port *stp_port;  /* Spanning Tree Protocol, if any. */
165     enum stp_state stp_state;   /* Always STP_DISABLED if STP not in use. */
166     long long int stp_state_entered;
167
168     /* Queue to DSCP mapping. */
169     struct ofproto_port_queue *qdscp;
170     size_t n_qdscp;
171
172     /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
173      *
174      * This is deprecated.  It is only for compatibility with broken device
175      * drivers in old versions of Linux that do not properly support VLANs when
176      * VLAN devices are not used.  When broken device drivers are no longer in
177      * widespread use, we will delete these interfaces. */
178     ofp_port_t realdev_ofp_port;
179     int vlandev_vid;
180 };
181
182 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
183  *
184  * This is deprecated.  It is only for compatibility with broken device drivers
185  * in old versions of Linux that do not properly support VLANs when VLAN
186  * devices are not used.  When broken device drivers are no longer in
187  * widespread use, we will delete these interfaces. */
188 struct vlan_splinter {
189     struct hmap_node realdev_vid_node;
190     struct hmap_node vlandev_node;
191     ofp_port_t realdev_ofp_port;
192     ofp_port_t vlandev_ofp_port;
193     int vid;
194 };
195
196 static void vsp_remove(struct ofport_dpif *);
197 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
198
199 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
200                                        ofp_port_t);
201
202 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
203                                        odp_port_t);
204
205 static struct ofport_dpif *
206 ofport_dpif_cast(const struct ofport *ofport)
207 {
208     return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
209 }
210
211 static void port_run(struct ofport_dpif *);
212 static int set_bfd(struct ofport *, const struct smap *);
213 static int set_cfm(struct ofport *, const struct cfm_settings *);
214 static void ofport_update_peer(struct ofport_dpif *);
215
216 /* Reasons that we might need to revalidate every datapath flow, and
217  * corresponding coverage counters.
218  *
219  * A value of 0 means that there is no need to revalidate.
220  *
221  * It would be nice to have some cleaner way to integrate with coverage
222  * counters, but with only a few reasons I guess this is good enough for
223  * now. */
224 enum revalidate_reason {
225     REV_RECONFIGURE = 1,       /* Switch configuration changed. */
226     REV_STP,                   /* Spanning tree protocol port status change. */
227     REV_BOND,                  /* Bonding changed. */
228     REV_PORT_TOGGLED,          /* Port enabled or disabled by CFM, LACP, ...*/
229     REV_FLOW_TABLE,            /* Flow table changed. */
230     REV_MAC_LEARNING,          /* Mac learning changed. */
231     REV_MCAST_SNOOPING,        /* Multicast snooping changed. */
232 };
233 COVERAGE_DEFINE(rev_reconfigure);
234 COVERAGE_DEFINE(rev_stp);
235 COVERAGE_DEFINE(rev_bond);
236 COVERAGE_DEFINE(rev_port_toggled);
237 COVERAGE_DEFINE(rev_flow_table);
238 COVERAGE_DEFINE(rev_mac_learning);
239 COVERAGE_DEFINE(rev_mcast_snooping);
240
241 /* All datapaths of a given type share a single dpif backer instance. */
242 struct dpif_backer {
243     char *type;
244     int refcount;
245     struct dpif *dpif;
246     struct udpif *udpif;
247
248     struct ovs_rwlock odp_to_ofport_lock;
249     struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
250
251     struct simap tnl_backers;      /* Set of dpif ports backing tunnels. */
252
253     enum revalidate_reason need_revalidate; /* Revalidate all flows. */
254
255     bool recv_set_enable; /* Enables or disables receiving packets. */
256
257     /* Recirculation. */
258     struct recirc_id_pool *rid_pool;       /* Recirculation ID pool. */
259     bool enable_recirc;   /* True if the datapath supports recirculation */
260
261     /* True if the datapath supports variable-length
262      * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
263      * False if the datapath supports only 8-byte (or shorter) userdata. */
264     bool variable_length_userdata;
265
266     /* Maximum number of MPLS label stack entries that the datapath supports
267      * in a match */
268     size_t max_mpls_depth;
269 };
270
271 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
272 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
273
274 struct ofproto_dpif {
275     struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
276     struct ofproto up;
277     struct dpif_backer *backer;
278
279     uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
280
281     /* Special OpenFlow rules. */
282     struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
283     struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
284     struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
285
286     /* Bridging. */
287     struct netflow *netflow;
288     struct dpif_sflow *sflow;
289     struct dpif_ipfix *ipfix;
290     struct hmap bundles;        /* Contains "struct ofbundle"s. */
291     struct mac_learning *ml;
292     struct mcast_snooping *ms;
293     bool has_bonded_bundles;
294     bool lacp_enabled;
295     struct mbridge *mbridge;
296
297     struct ovs_mutex stats_mutex;
298     struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
299                                             * consumed in userspace. */
300
301     /* Spanning tree. */
302     struct stp *stp;
303     long long int stp_last_tick;
304
305     /* VLAN splinters. */
306     struct ovs_mutex vsp_mutex;
307     struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
308     struct hmap vlandev_map OVS_GUARDED;     /* vlandev -> (realdev,vid). */
309
310     /* Ports. */
311     struct sset ports;             /* Set of standard port names. */
312     struct sset ghost_ports;       /* Ports with no datapath port. */
313     struct sset port_poll_set;     /* Queued names for port_poll() reply. */
314     int port_poll_errno;           /* Last errno for port_poll() reply. */
315     uint64_t change_seq;           /* Connectivity status changes. */
316
317     /* Work queues. */
318     struct guarded_list pins;      /* Contains "struct ofputil_packet_in"s. */
319     struct seq *pins_seq;          /* For notifying 'pins' reception. */
320     uint64_t pins_seqno;
321 };
322
323 /* All existing ofproto_dpif instances, indexed by ->up.name. */
324 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
325
326 static void ofproto_dpif_unixctl_init(void);
327
328 static inline struct ofproto_dpif *
329 ofproto_dpif_cast(const struct ofproto *ofproto)
330 {
331     ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
332     return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
333 }
334
335 size_t
336 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
337 {
338     return ofproto->backer->max_mpls_depth;
339 }
340
341 bool
342 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
343 {
344     return ofproto->backer->enable_recirc;
345 }
346
347 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
348                                         ofp_port_t ofp_port);
349 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
350                           const struct ofpbuf *packet,
351                           const struct ofpact[], size_t ofpacts_len,
352                           struct ds *);
353
354 /* Global variables. */
355 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
356
357 /* Initial mappings of port to bridge mappings. */
358 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
359
360 /* Executes 'fm'.  The caller retains ownership of 'fm' and everything in
361  * it. */
362 void
363 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
364                       struct ofputil_flow_mod *fm)
365 {
366     ofproto_flow_mod(&ofproto->up, fm);
367 }
368
369 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
370  * Takes ownership of 'pin' and pin->packet. */
371 void
372 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
373                             struct ofproto_packet_in *pin)
374 {
375     if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
376         COVERAGE_INC(packet_in_overflow);
377         free(CONST_CAST(void *, pin->up.packet));
378         free(pin);
379     }
380
381     /* Wakes up main thread for packet-in I/O. */
382     seq_change(ofproto->pins_seq);
383 }
384
385 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
386  * packet rather than to send the packet to the controller.
387  *
388  * This function returns false to indicate that a packet_in message
389  * for a "table-miss" should be sent to at least one controller.
390  * False otherwise. */
391 bool
392 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
393 {
394     return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
395 }
396 \f
397 /* Factory functions. */
398
399 static void
400 init(const struct shash *iface_hints)
401 {
402     struct shash_node *node;
403
404     /* Make a local copy, since we don't own 'iface_hints' elements. */
405     SHASH_FOR_EACH(node, iface_hints) {
406         const struct iface_hint *orig_hint = node->data;
407         struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
408
409         new_hint->br_name = xstrdup(orig_hint->br_name);
410         new_hint->br_type = xstrdup(orig_hint->br_type);
411         new_hint->ofp_port = orig_hint->ofp_port;
412
413         shash_add(&init_ofp_ports, node->name, new_hint);
414     }
415 }
416
417 static void
418 enumerate_types(struct sset *types)
419 {
420     dp_enumerate_types(types);
421 }
422
423 static int
424 enumerate_names(const char *type, struct sset *names)
425 {
426     struct ofproto_dpif *ofproto;
427
428     sset_clear(names);
429     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
430         if (strcmp(type, ofproto->up.type)) {
431             continue;
432         }
433         sset_add(names, ofproto->up.name);
434     }
435
436     return 0;
437 }
438
439 static int
440 del(const char *type, const char *name)
441 {
442     struct dpif *dpif;
443     int error;
444
445     error = dpif_open(name, type, &dpif);
446     if (!error) {
447         error = dpif_delete(dpif);
448         dpif_close(dpif);
449     }
450     return error;
451 }
452 \f
453 static const char *
454 port_open_type(const char *datapath_type, const char *port_type)
455 {
456     return dpif_port_open_type(datapath_type, port_type);
457 }
458
459 /* Type functions. */
460
461 static void process_dpif_port_changes(struct dpif_backer *);
462 static void process_dpif_all_ports_changed(struct dpif_backer *);
463 static void process_dpif_port_change(struct dpif_backer *,
464                                      const char *devname);
465 static void process_dpif_port_error(struct dpif_backer *, int error);
466
467 static struct ofproto_dpif *
468 lookup_ofproto_dpif_by_port_name(const char *name)
469 {
470     struct ofproto_dpif *ofproto;
471
472     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
473         if (sset_contains(&ofproto->ports, name)) {
474             return ofproto;
475         }
476     }
477
478     return NULL;
479 }
480
481 static int
482 type_run(const char *type)
483 {
484     struct dpif_backer *backer;
485
486     backer = shash_find_data(&all_dpif_backers, type);
487     if (!backer) {
488         /* This is not necessarily a problem, since backers are only
489          * created on demand. */
490         return 0;
491     }
492
493     dpif_run(backer->dpif);
494     udpif_run(backer->udpif);
495
496     /* If vswitchd started with other_config:flow_restore_wait set as "true",
497      * and the configuration has now changed to "false", enable receiving
498      * packets from the datapath. */
499     if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
500         int error;
501
502         backer->recv_set_enable = true;
503
504         error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
505         if (error) {
506             VLOG_ERR("Failed to enable receiving packets in dpif.");
507             return error;
508         }
509         dpif_flow_flush(backer->dpif);
510         backer->need_revalidate = REV_RECONFIGURE;
511     }
512
513     if (backer->recv_set_enable) {
514         udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
515     }
516
517     if (backer->need_revalidate) {
518         struct ofproto_dpif *ofproto;
519         struct simap_node *node;
520         struct simap tmp_backers;
521
522         /* Handle tunnel garbage collection. */
523         simap_init(&tmp_backers);
524         simap_swap(&backer->tnl_backers, &tmp_backers);
525
526         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
527             struct ofport_dpif *iter;
528
529             if (backer != ofproto->backer) {
530                 continue;
531             }
532
533             HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
534                 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
535                 const char *dp_port;
536
537                 if (!iter->is_tunnel) {
538                     continue;
539                 }
540
541                 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
542                                                      namebuf, sizeof namebuf);
543                 node = simap_find(&tmp_backers, dp_port);
544                 if (node) {
545                     simap_put(&backer->tnl_backers, dp_port, node->data);
546                     simap_delete(&tmp_backers, node);
547                     node = simap_find(&backer->tnl_backers, dp_port);
548                 } else {
549                     node = simap_find(&backer->tnl_backers, dp_port);
550                     if (!node) {
551                         odp_port_t odp_port = ODPP_NONE;
552
553                         if (!dpif_port_add(backer->dpif, iter->up.netdev,
554                                            &odp_port)) {
555                             simap_put(&backer->tnl_backers, dp_port,
556                                       odp_to_u32(odp_port));
557                             node = simap_find(&backer->tnl_backers, dp_port);
558                         }
559                     }
560                 }
561
562                 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
563                 if (tnl_port_reconfigure(iter, iter->up.netdev,
564                                          iter->odp_port)) {
565                     backer->need_revalidate = REV_RECONFIGURE;
566                 }
567             }
568         }
569
570         SIMAP_FOR_EACH (node, &tmp_backers) {
571             dpif_port_del(backer->dpif, u32_to_odp(node->data));
572         }
573         simap_destroy(&tmp_backers);
574
575         switch (backer->need_revalidate) {
576         case REV_RECONFIGURE:    COVERAGE_INC(rev_reconfigure);    break;
577         case REV_STP:            COVERAGE_INC(rev_stp);            break;
578         case REV_BOND:           COVERAGE_INC(rev_bond);           break;
579         case REV_PORT_TOGGLED:   COVERAGE_INC(rev_port_toggled);   break;
580         case REV_FLOW_TABLE:     COVERAGE_INC(rev_flow_table);     break;
581         case REV_MAC_LEARNING:   COVERAGE_INC(rev_mac_learning);   break;
582         case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
583         }
584         backer->need_revalidate = 0;
585
586         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
587             struct ofport_dpif *ofport;
588             struct ofbundle *bundle;
589
590             if (ofproto->backer != backer) {
591                 continue;
592             }
593
594             xlate_txn_start();
595             xlate_ofproto_set(ofproto, ofproto->up.name,
596                               ofproto->backer->dpif, ofproto->miss_rule,
597                               ofproto->no_packet_in_rule, ofproto->ml,
598                               ofproto->stp, ofproto->ms, ofproto->mbridge,
599                               ofproto->sflow, ofproto->ipfix,
600                               ofproto->netflow, ofproto->up.frag_handling,
601                               ofproto->up.forward_bpdu,
602                               connmgr_has_in_band(ofproto->up.connmgr),
603                               ofproto->backer->enable_recirc,
604                               ofproto->backer->variable_length_userdata,
605                               ofproto->backer->max_mpls_depth);
606
607             HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
608                 xlate_bundle_set(ofproto, bundle, bundle->name,
609                                  bundle->vlan_mode, bundle->vlan,
610                                  bundle->trunks, bundle->use_priority_tags,
611                                  bundle->bond, bundle->lacp,
612                                  bundle->floodable);
613             }
614
615             HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
616                 int stp_port = ofport->stp_port
617                     ? stp_port_no(ofport->stp_port)
618                     : -1;
619                 xlate_ofport_set(ofproto, ofport->bundle, ofport,
620                                  ofport->up.ofp_port, ofport->odp_port,
621                                  ofport->up.netdev, ofport->cfm,
622                                  ofport->bfd, ofport->peer, stp_port,
623                                  ofport->qdscp, ofport->n_qdscp,
624                                  ofport->up.pp.config, ofport->up.pp.state,
625                                  ofport->is_tunnel, ofport->may_enable);
626             }
627             xlate_txn_commit();
628         }
629
630         udpif_revalidate(backer->udpif);
631     }
632
633     process_dpif_port_changes(backer);
634
635     return 0;
636 }
637
638 /* Check for and handle port changes in 'backer''s dpif. */
639 static void
640 process_dpif_port_changes(struct dpif_backer *backer)
641 {
642     for (;;) {
643         char *devname;
644         int error;
645
646         error = dpif_port_poll(backer->dpif, &devname);
647         switch (error) {
648         case EAGAIN:
649             return;
650
651         case ENOBUFS:
652             process_dpif_all_ports_changed(backer);
653             break;
654
655         case 0:
656             process_dpif_port_change(backer, devname);
657             free(devname);
658             break;
659
660         default:
661             process_dpif_port_error(backer, error);
662             break;
663         }
664     }
665 }
666
667 static void
668 process_dpif_all_ports_changed(struct dpif_backer *backer)
669 {
670     struct ofproto_dpif *ofproto;
671     struct dpif_port dpif_port;
672     struct dpif_port_dump dump;
673     struct sset devnames;
674     const char *devname;
675
676     sset_init(&devnames);
677     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
678         if (ofproto->backer == backer) {
679             struct ofport *ofport;
680
681             HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
682                 sset_add(&devnames, netdev_get_name(ofport->netdev));
683             }
684         }
685     }
686     DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
687         sset_add(&devnames, dpif_port.name);
688     }
689
690     SSET_FOR_EACH (devname, &devnames) {
691         process_dpif_port_change(backer, devname);
692     }
693     sset_destroy(&devnames);
694 }
695
696 static void
697 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
698 {
699     struct ofproto_dpif *ofproto;
700     struct dpif_port port;
701
702     /* Don't report on the datapath's device. */
703     if (!strcmp(devname, dpif_base_name(backer->dpif))) {
704         return;
705     }
706
707     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
708                    &all_ofproto_dpifs) {
709         if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
710             return;
711         }
712     }
713
714     ofproto = lookup_ofproto_dpif_by_port_name(devname);
715     if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
716         /* The port was removed.  If we know the datapath,
717          * report it through poll_set().  If we don't, it may be
718          * notifying us of a removal we initiated, so ignore it.
719          * If there's a pending ENOBUFS, let it stand, since
720          * everything will be reevaluated. */
721         if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
722             sset_add(&ofproto->port_poll_set, devname);
723             ofproto->port_poll_errno = 0;
724         }
725     } else if (!ofproto) {
726         /* The port was added, but we don't know with which
727          * ofproto we should associate it.  Delete it. */
728         dpif_port_del(backer->dpif, port.port_no);
729     } else {
730         struct ofport_dpif *ofport;
731
732         ofport = ofport_dpif_cast(shash_find_data(
733                                       &ofproto->up.port_by_name, devname));
734         if (ofport
735             && ofport->odp_port != port.port_no
736             && !odp_port_to_ofport(backer, port.port_no))
737         {
738             /* 'ofport''s datapath port number has changed from
739              * 'ofport->odp_port' to 'port.port_no'.  Update our internal data
740              * structures to match. */
741             ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
742             hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
743             ofport->odp_port = port.port_no;
744             hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
745                         hash_odp_port(port.port_no));
746             ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
747             backer->need_revalidate = REV_RECONFIGURE;
748         }
749     }
750     dpif_port_destroy(&port);
751 }
752
753 /* Propagate 'error' to all ofprotos based on 'backer'. */
754 static void
755 process_dpif_port_error(struct dpif_backer *backer, int error)
756 {
757     struct ofproto_dpif *ofproto;
758
759     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
760         if (ofproto->backer == backer) {
761             sset_clear(&ofproto->port_poll_set);
762             ofproto->port_poll_errno = error;
763         }
764     }
765 }
766
767 static void
768 type_wait(const char *type)
769 {
770     struct dpif_backer *backer;
771
772     backer = shash_find_data(&all_dpif_backers, type);
773     if (!backer) {
774         /* This is not necessarily a problem, since backers are only
775          * created on demand. */
776         return;
777     }
778
779     dpif_wait(backer->dpif);
780 }
781 \f
782 /* Basic life-cycle. */
783
784 static int add_internal_flows(struct ofproto_dpif *);
785
786 static struct ofproto *
787 alloc(void)
788 {
789     struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
790     return &ofproto->up;
791 }
792
793 static void
794 dealloc(struct ofproto *ofproto_)
795 {
796     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
797     free(ofproto);
798 }
799
800 static void
801 close_dpif_backer(struct dpif_backer *backer)
802 {
803     ovs_assert(backer->refcount > 0);
804
805     if (--backer->refcount) {
806         return;
807     }
808
809     udpif_destroy(backer->udpif);
810
811     simap_destroy(&backer->tnl_backers);
812     ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
813     hmap_destroy(&backer->odp_to_ofport_map);
814     shash_find_and_delete(&all_dpif_backers, backer->type);
815     recirc_id_pool_destroy(backer->rid_pool);
816     free(backer->type);
817     dpif_close(backer->dpif);
818     free(backer);
819 }
820
821 /* Datapath port slated for removal from datapath. */
822 struct odp_garbage {
823     struct list list_node;
824     odp_port_t odp_port;
825 };
826
827 static bool check_variable_length_userdata(struct dpif_backer *backer);
828 static size_t check_max_mpls_depth(struct dpif_backer *backer);
829 static bool check_recirc(struct dpif_backer *backer);
830
831 static int
832 open_dpif_backer(const char *type, struct dpif_backer **backerp)
833 {
834     struct dpif_backer *backer;
835     struct dpif_port_dump port_dump;
836     struct dpif_port port;
837     struct shash_node *node;
838     struct list garbage_list;
839     struct odp_garbage *garbage, *next;
840
841     struct sset names;
842     char *backer_name;
843     const char *name;
844     int error;
845
846     backer = shash_find_data(&all_dpif_backers, type);
847     if (backer) {
848         backer->refcount++;
849         *backerp = backer;
850         return 0;
851     }
852
853     backer_name = xasprintf("ovs-%s", type);
854
855     /* Remove any existing datapaths, since we assume we're the only
856      * userspace controlling the datapath. */
857     sset_init(&names);
858     dp_enumerate_names(type, &names);
859     SSET_FOR_EACH(name, &names) {
860         struct dpif *old_dpif;
861
862         /* Don't remove our backer if it exists. */
863         if (!strcmp(name, backer_name)) {
864             continue;
865         }
866
867         if (dpif_open(name, type, &old_dpif)) {
868             VLOG_WARN("couldn't open old datapath %s to remove it", name);
869         } else {
870             dpif_delete(old_dpif);
871             dpif_close(old_dpif);
872         }
873     }
874     sset_destroy(&names);
875
876     backer = xmalloc(sizeof *backer);
877
878     error = dpif_create_and_open(backer_name, type, &backer->dpif);
879     free(backer_name);
880     if (error) {
881         VLOG_ERR("failed to open datapath of type %s: %s", type,
882                  ovs_strerror(error));
883         free(backer);
884         return error;
885     }
886     backer->udpif = udpif_create(backer, backer->dpif);
887
888     backer->type = xstrdup(type);
889     backer->refcount = 1;
890     hmap_init(&backer->odp_to_ofport_map);
891     ovs_rwlock_init(&backer->odp_to_ofport_lock);
892     backer->need_revalidate = 0;
893     simap_init(&backer->tnl_backers);
894     backer->recv_set_enable = !ofproto_get_flow_restore_wait();
895     *backerp = backer;
896
897     if (backer->recv_set_enable) {
898         dpif_flow_flush(backer->dpif);
899     }
900
901     /* Loop through the ports already on the datapath and remove any
902      * that we don't need anymore. */
903     list_init(&garbage_list);
904     dpif_port_dump_start(&port_dump, backer->dpif);
905     while (dpif_port_dump_next(&port_dump, &port)) {
906         node = shash_find(&init_ofp_ports, port.name);
907         if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
908             garbage = xmalloc(sizeof *garbage);
909             garbage->odp_port = port.port_no;
910             list_push_front(&garbage_list, &garbage->list_node);
911         }
912     }
913     dpif_port_dump_done(&port_dump);
914
915     LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
916         dpif_port_del(backer->dpif, garbage->odp_port);
917         list_remove(&garbage->list_node);
918         free(garbage);
919     }
920
921     shash_add(&all_dpif_backers, type, backer);
922
923     backer->enable_recirc = check_recirc(backer);
924     backer->variable_length_userdata = check_variable_length_userdata(backer);
925     backer->max_mpls_depth = check_max_mpls_depth(backer);
926     backer->rid_pool = recirc_id_pool_create();
927
928     error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
929     if (error) {
930         VLOG_ERR("failed to listen on datapath of type %s: %s",
931                  type, ovs_strerror(error));
932         close_dpif_backer(backer);
933         return error;
934     }
935
936     if (backer->recv_set_enable) {
937         udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
938     }
939
940     return error;
941 }
942
943 /* Tests whether 'backer''s datapath supports recirculation.  Only newer
944  * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys.  We need to disable some
945  * features on older datapaths that don't support this feature.
946  *
947  * Returns false if 'backer' definitely does not support recirculation, true if
948  * it seems to support recirculation or if at least the error we get is
949  * ambiguous. */
950 static bool
951 check_recirc(struct dpif_backer *backer)
952 {
953     struct flow flow;
954     struct odputil_keybuf keybuf;
955     struct ofpbuf key;
956     int error;
957     bool enable_recirc = false;
958
959     memset(&flow, 0, sizeof flow);
960     flow.recirc_id = 1;
961     flow.dp_hash = 1;
962
963     ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
964     odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
965
966     error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE,
967                           ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL,
968                           0, NULL);
969     if (error && error != EEXIST) {
970         if (error != EINVAL) {
971             VLOG_WARN("%s: Reciculation flow probe failed (%s)",
972                       dpif_name(backer->dpif), ovs_strerror(error));
973         }
974         goto done;
975     }
976
977     error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key),
978                           NULL);
979     if (error) {
980         VLOG_WARN("%s: failed to delete recirculation feature probe flow",
981                   dpif_name(backer->dpif));
982     }
983
984     enable_recirc = true;
985
986 done:
987     if (enable_recirc) {
988         VLOG_INFO("%s: Datapath supports recirculation",
989                   dpif_name(backer->dpif));
990     } else {
991         VLOG_INFO("%s: Datapath does not support recirculation",
992                   dpif_name(backer->dpif));
993     }
994
995     return enable_recirc;
996 }
997
998 /* Tests whether 'backer''s datapath supports variable-length
999  * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.  We need
1000  * to disable some features on older datapaths that don't support this
1001  * feature.
1002  *
1003  * Returns false if 'backer' definitely does not support variable-length
1004  * userdata, true if it seems to support them or if at least the error we get
1005  * is ambiguous. */
1006 static bool
1007 check_variable_length_userdata(struct dpif_backer *backer)
1008 {
1009     struct eth_header *eth;
1010     struct ofpbuf actions;
1011     struct dpif_execute execute;
1012     struct ofpbuf packet;
1013     size_t start;
1014     int error;
1015
1016     /* Compose a userspace action that will cause an ERANGE error on older
1017      * datapaths that don't support variable-length userdata.
1018      *
1019      * We really test for using userdata longer than 8 bytes, but older
1020      * datapaths accepted these, silently truncating the userdata to 8 bytes.
1021      * The same older datapaths rejected userdata shorter than 8 bytes, so we
1022      * test for that instead as a proxy for longer userdata support. */
1023     ofpbuf_init(&actions, 64);
1024     start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1025     nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1026                    dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1027     nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1028     nl_msg_end_nested(&actions, start);
1029
1030     /* Compose a dummy ethernet packet. */
1031     ofpbuf_init(&packet, ETH_HEADER_LEN);
1032     eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
1033     eth->eth_type = htons(0x1234);
1034
1035     /* Execute the actions.  On older datapaths this fails with ERANGE, on
1036      * newer datapaths it succeeds. */
1037     execute.actions = ofpbuf_data(&actions);
1038     execute.actions_len = ofpbuf_size(&actions);
1039     execute.packet = &packet;
1040     execute.md = PKT_METADATA_INITIALIZER(0);
1041     execute.needs_help = false;
1042
1043     error = dpif_execute(backer->dpif, &execute);
1044
1045     ofpbuf_uninit(&packet);
1046     ofpbuf_uninit(&actions);
1047
1048     switch (error) {
1049     case 0:
1050         return true;
1051
1052     case ERANGE:
1053         /* Variable-length userdata is not supported. */
1054         VLOG_WARN("%s: datapath does not support variable-length userdata "
1055                   "feature (needs Linux 3.10+ or kernel module from OVS "
1056                   "1..11+).  The NXAST_SAMPLE action will be ignored.",
1057                   dpif_name(backer->dpif));
1058         return false;
1059
1060     default:
1061         /* Something odd happened.  We're not sure whether variable-length
1062          * userdata is supported.  Default to "yes". */
1063         VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1064                   dpif_name(backer->dpif), ovs_strerror(error));
1065         return true;
1066     }
1067 }
1068
1069 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1070  *
1071  * Returns the number of elements in a struct flow's mpls_lse field
1072  * if the datapath supports at least that many entries in an
1073  * MPLS label stack.
1074  * Otherwise returns the number of MPLS push actions supported by
1075  * the datapath. */
1076 static size_t
1077 check_max_mpls_depth(struct dpif_backer *backer)
1078 {
1079     struct flow flow;
1080     int n;
1081
1082     for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1083         struct odputil_keybuf keybuf;
1084         struct ofpbuf key;
1085         int error;
1086
1087         memset(&flow, 0, sizeof flow);
1088         flow.dl_type = htons(ETH_TYPE_MPLS);
1089         flow_set_mpls_bos(&flow, n, 1);
1090
1091         ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1092         odp_flow_key_from_flow(&key, &flow, NULL, 0, false);
1093
1094         error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE,
1095                               ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL, 0, NULL);
1096         if (error && error != EEXIST) {
1097             if (error != EINVAL) {
1098                 VLOG_WARN("%s: MPLS stack length feature probe failed (%s)",
1099                           dpif_name(backer->dpif), ovs_strerror(error));
1100             }
1101             break;
1102         }
1103
1104         error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key), NULL);
1105         if (error) {
1106             VLOG_WARN("%s: failed to delete MPLS feature probe flow",
1107                       dpif_name(backer->dpif));
1108         }
1109     }
1110
1111     VLOG_INFO("%s: MPLS label stack length probed as %d",
1112               dpif_name(backer->dpif), n);
1113     return n;
1114 }
1115
1116 static int
1117 construct(struct ofproto *ofproto_)
1118 {
1119     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1120     struct shash_node *node, *next;
1121     int error;
1122
1123     error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1124     if (error) {
1125         return error;
1126     }
1127
1128     ofproto->netflow = NULL;
1129     ofproto->sflow = NULL;
1130     ofproto->ipfix = NULL;
1131     ofproto->stp = NULL;
1132     ofproto->dump_seq = 0;
1133     hmap_init(&ofproto->bundles);
1134     ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1135     ofproto->ms = NULL;
1136     ofproto->mbridge = mbridge_create();
1137     ofproto->has_bonded_bundles = false;
1138     ofproto->lacp_enabled = false;
1139     ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1140     ovs_mutex_init(&ofproto->vsp_mutex);
1141
1142     guarded_list_init(&ofproto->pins);
1143
1144     ofproto_dpif_unixctl_init();
1145
1146     hmap_init(&ofproto->vlandev_map);
1147     hmap_init(&ofproto->realdev_vid_map);
1148
1149     sset_init(&ofproto->ports);
1150     sset_init(&ofproto->ghost_ports);
1151     sset_init(&ofproto->port_poll_set);
1152     ofproto->port_poll_errno = 0;
1153     ofproto->change_seq = 0;
1154     ofproto->pins_seq = seq_create();
1155     ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1156
1157
1158     SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1159         struct iface_hint *iface_hint = node->data;
1160
1161         if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1162             /* Check if the datapath already has this port. */
1163             if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1164                 sset_add(&ofproto->ports, node->name);
1165             }
1166
1167             free(iface_hint->br_name);
1168             free(iface_hint->br_type);
1169             free(iface_hint);
1170             shash_delete(&init_ofp_ports, node);
1171         }
1172     }
1173
1174     hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1175                 hash_string(ofproto->up.name, 0));
1176     memset(&ofproto->stats, 0, sizeof ofproto->stats);
1177
1178     ofproto_init_tables(ofproto_, N_TABLES);
1179     error = add_internal_flows(ofproto);
1180
1181     ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1182
1183     return error;
1184 }
1185
1186 static int
1187 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1188                   const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1189 {
1190     struct match match;
1191     int error;
1192     struct rule *rule;
1193
1194     match_init_catchall(&match);
1195     match_set_reg(&match, 0, id);
1196
1197     error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1198                                            &rule);
1199     *rulep = error ? NULL : rule_dpif_cast(rule);
1200
1201     return error;
1202 }
1203
1204 static int
1205 add_internal_flows(struct ofproto_dpif *ofproto)
1206 {
1207     struct ofpact_controller *controller;
1208     uint64_t ofpacts_stub[128 / 8];
1209     struct ofpbuf ofpacts;
1210     struct rule *unused_rulep OVS_UNUSED;
1211     struct ofpact_resubmit *resubmit;
1212     struct match match;
1213     int error;
1214     int id;
1215
1216     ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1217     id = 1;
1218
1219     controller = ofpact_put_CONTROLLER(&ofpacts);
1220     controller->max_len = UINT16_MAX;
1221     controller->controller_id = 0;
1222     controller->reason = OFPR_NO_MATCH;
1223     ofpact_pad(&ofpacts);
1224
1225     error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1226                                    &ofproto->miss_rule);
1227     if (error) {
1228         return error;
1229     }
1230
1231     ofpbuf_clear(&ofpacts);
1232     error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1233                                    &ofproto->no_packet_in_rule);
1234     if (error) {
1235         return error;
1236     }
1237
1238     error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1239                                    &ofproto->drop_frags_rule);
1240     if (error) {
1241         return error;
1242     }
1243
1244     /* Continue non-recirculation rule lookups from table 0.
1245      *
1246      * (priority=2), recirc=0, actions=resubmit(, 0)
1247      */
1248     resubmit = ofpact_put_RESUBMIT(&ofpacts);
1249     resubmit->ofpact.compat = 0;
1250     resubmit->in_port = OFPP_IN_PORT;
1251     resubmit->table_id = 0;
1252
1253     match_init_catchall(&match);
1254     match_set_recirc_id(&match, 0);
1255
1256     error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1257                                            &unused_rulep);
1258     if (error) {
1259         return error;
1260     }
1261
1262     /* Drop any run away recirc rule lookups. Recirc_id has to be
1263      * non-zero when reaching this rule.
1264      *
1265      * (priority=1), *, actions=drop
1266      */
1267     ofpbuf_clear(&ofpacts);
1268     match_init_catchall(&match);
1269     error = ofproto_dpif_add_internal_flow(ofproto, &match, 1, 0, &ofpacts,
1270                                            &unused_rulep);
1271
1272     return error;
1273 }
1274
1275 static void
1276 destruct(struct ofproto *ofproto_)
1277 {
1278     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1279     struct ofproto_packet_in *pin, *next_pin;
1280     struct rule_dpif *rule;
1281     struct oftable *table;
1282     struct list pins;
1283
1284     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1285     xlate_txn_start();
1286     xlate_remove_ofproto(ofproto);
1287     xlate_txn_commit();
1288
1289     /* Ensure that the upcall processing threads have no remaining references
1290      * to the ofproto or anything in it. */
1291     udpif_synchronize(ofproto->backer->udpif);
1292
1293     hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1294
1295     OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1296         CLS_FOR_EACH_SAFE (rule, up.cr, &table->cls) {
1297             ofproto_rule_delete(&ofproto->up, &rule->up);
1298         }
1299     }
1300
1301     guarded_list_pop_all(&ofproto->pins, &pins);
1302     LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1303         list_remove(&pin->list_node);
1304         free(CONST_CAST(void *, pin->up.packet));
1305         free(pin);
1306     }
1307     guarded_list_destroy(&ofproto->pins);
1308
1309     mbridge_unref(ofproto->mbridge);
1310
1311     netflow_unref(ofproto->netflow);
1312     dpif_sflow_unref(ofproto->sflow);
1313     hmap_destroy(&ofproto->bundles);
1314     mac_learning_unref(ofproto->ml);
1315     mcast_snooping_unref(ofproto->ms);
1316
1317     hmap_destroy(&ofproto->vlandev_map);
1318     hmap_destroy(&ofproto->realdev_vid_map);
1319
1320     sset_destroy(&ofproto->ports);
1321     sset_destroy(&ofproto->ghost_ports);
1322     sset_destroy(&ofproto->port_poll_set);
1323
1324     ovs_mutex_destroy(&ofproto->stats_mutex);
1325     ovs_mutex_destroy(&ofproto->vsp_mutex);
1326
1327     seq_destroy(ofproto->pins_seq);
1328
1329     close_dpif_backer(ofproto->backer);
1330 }
1331
1332 static int
1333 run(struct ofproto *ofproto_)
1334 {
1335     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1336     uint64_t new_seq, new_dump_seq;
1337
1338     if (mbridge_need_revalidate(ofproto->mbridge)) {
1339         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1340         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1341         mac_learning_flush(ofproto->ml);
1342         ovs_rwlock_unlock(&ofproto->ml->rwlock);
1343         mcast_snooping_mdb_flush(ofproto->ms);
1344     }
1345
1346     /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1347      * flow restore.  Even though nothing is processed during flow restore,
1348      * all queued 'pins' will be handled immediately when flow restore
1349      * completes. */
1350     ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1351
1352     /* Do not perform any periodic activity required by 'ofproto' while
1353      * waiting for flow restore to complete. */
1354     if (!ofproto_get_flow_restore_wait()) {
1355         struct ofproto_packet_in *pin, *next_pin;
1356         struct list pins;
1357
1358         guarded_list_pop_all(&ofproto->pins, &pins);
1359         LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1360             connmgr_send_packet_in(ofproto->up.connmgr, pin);
1361             list_remove(&pin->list_node);
1362             free(CONST_CAST(void *, pin->up.packet));
1363             free(pin);
1364         }
1365     }
1366
1367     if (ofproto->netflow) {
1368         netflow_run(ofproto->netflow);
1369     }
1370     if (ofproto->sflow) {
1371         dpif_sflow_run(ofproto->sflow);
1372     }
1373     if (ofproto->ipfix) {
1374         dpif_ipfix_run(ofproto->ipfix);
1375     }
1376
1377     new_seq = seq_read(connectivity_seq_get());
1378     if (ofproto->change_seq != new_seq) {
1379         struct ofport_dpif *ofport;
1380
1381         HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1382             port_run(ofport);
1383         }
1384
1385         ofproto->change_seq = new_seq;
1386     }
1387     if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1388         struct ofbundle *bundle;
1389
1390         HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1391             bundle_run(bundle);
1392         }
1393     }
1394
1395     stp_run(ofproto);
1396     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1397     if (mac_learning_run(ofproto->ml)) {
1398         ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1399     }
1400     ovs_rwlock_unlock(&ofproto->ml->rwlock);
1401
1402     if (mcast_snooping_run(ofproto->ms)) {
1403         ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1404     }
1405
1406     new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1407     if (ofproto->dump_seq != new_dump_seq) {
1408         struct rule *rule, *next_rule;
1409
1410         /* We know stats are relatively fresh, so now is a good time to do some
1411          * periodic work. */
1412         ofproto->dump_seq = new_dump_seq;
1413
1414         /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1415          * has passed. */
1416         ovs_mutex_lock(&ofproto_mutex);
1417         LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1418                             &ofproto->up.expirable) {
1419             rule_expire(rule_dpif_cast(rule));
1420         }
1421         ovs_mutex_unlock(&ofproto_mutex);
1422
1423         /* All outstanding data in existing flows has been accounted, so it's a
1424          * good time to do bond rebalancing. */
1425         if (ofproto->has_bonded_bundles) {
1426             struct ofbundle *bundle;
1427
1428             HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1429                 if (bundle->bond) {
1430                     bond_rebalance(bundle->bond);
1431                 }
1432             }
1433         }
1434     }
1435
1436     return 0;
1437 }
1438
1439 static void
1440 wait(struct ofproto *ofproto_)
1441 {
1442     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1443
1444     if (ofproto_get_flow_restore_wait()) {
1445         return;
1446     }
1447
1448     if (ofproto->sflow) {
1449         dpif_sflow_wait(ofproto->sflow);
1450     }
1451     if (ofproto->ipfix) {
1452         dpif_ipfix_wait(ofproto->ipfix);
1453     }
1454     if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1455         struct ofbundle *bundle;
1456
1457         HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1458             bundle_wait(bundle);
1459         }
1460     }
1461     if (ofproto->netflow) {
1462         netflow_wait(ofproto->netflow);
1463     }
1464     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1465     mac_learning_wait(ofproto->ml);
1466     ovs_rwlock_unlock(&ofproto->ml->rwlock);
1467     mcast_snooping_wait(ofproto->ms);
1468     stp_wait(ofproto);
1469     if (ofproto->backer->need_revalidate) {
1470         /* Shouldn't happen, but if it does just go around again. */
1471         VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1472         poll_immediate_wake();
1473     }
1474
1475     seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1476     seq_wait(ofproto->pins_seq, ofproto->pins_seqno);
1477 }
1478
1479 static void
1480 type_get_memory_usage(const char *type, struct simap *usage)
1481 {
1482     struct dpif_backer *backer;
1483
1484     backer = shash_find_data(&all_dpif_backers, type);
1485     if (backer) {
1486         udpif_get_memory_usage(backer->udpif, usage);
1487     }
1488 }
1489
1490 static void
1491 flush(struct ofproto *ofproto_)
1492 {
1493     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1494     struct dpif_backer *backer = ofproto->backer;
1495
1496     if (backer) {
1497         udpif_flush(backer->udpif);
1498     }
1499 }
1500
1501 static void
1502 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1503              bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1504 {
1505     *arp_match_ip = true;
1506     *actions = (OFPUTIL_A_OUTPUT |
1507                 OFPUTIL_A_SET_VLAN_VID |
1508                 OFPUTIL_A_SET_VLAN_PCP |
1509                 OFPUTIL_A_STRIP_VLAN |
1510                 OFPUTIL_A_SET_DL_SRC |
1511                 OFPUTIL_A_SET_DL_DST |
1512                 OFPUTIL_A_SET_NW_SRC |
1513                 OFPUTIL_A_SET_NW_DST |
1514                 OFPUTIL_A_SET_NW_TOS |
1515                 OFPUTIL_A_SET_TP_SRC |
1516                 OFPUTIL_A_SET_TP_DST |
1517                 OFPUTIL_A_ENQUEUE);
1518 }
1519
1520 static void
1521 get_tables(struct ofproto *ofproto, struct ofp12_table_stats *ots)
1522 {
1523     int i;
1524
1525     strcpy(ots->name, "classifier");
1526
1527     for (i = 0; i < ofproto->n_tables; i++) {
1528         unsigned long missed, matched;
1529
1530         atomic_read(&ofproto->tables[i].n_matched, &matched);
1531         ots[i].matched_count = htonll(matched);
1532         atomic_read(&ofproto->tables[i].n_missed, &missed);
1533         ots[i].lookup_count = htonll(matched + missed);
1534     }
1535 }
1536
1537 static struct ofport *
1538 port_alloc(void)
1539 {
1540     struct ofport_dpif *port = xmalloc(sizeof *port);
1541     return &port->up;
1542 }
1543
1544 static void
1545 port_dealloc(struct ofport *port_)
1546 {
1547     struct ofport_dpif *port = ofport_dpif_cast(port_);
1548     free(port);
1549 }
1550
1551 static int
1552 port_construct(struct ofport *port_)
1553 {
1554     struct ofport_dpif *port = ofport_dpif_cast(port_);
1555     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1556     const struct netdev *netdev = port->up.netdev;
1557     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1558     struct dpif_port dpif_port;
1559     int error;
1560
1561     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1562     port->bundle = NULL;
1563     port->cfm = NULL;
1564     port->bfd = NULL;
1565     port->may_enable = true;
1566     port->stp_port = NULL;
1567     port->stp_state = STP_DISABLED;
1568     port->is_tunnel = false;
1569     port->peer = NULL;
1570     port->qdscp = NULL;
1571     port->n_qdscp = 0;
1572     port->realdev_ofp_port = 0;
1573     port->vlandev_vid = 0;
1574     port->carrier_seq = netdev_get_carrier_resets(netdev);
1575     port->is_layer3 = netdev_vport_is_layer3(netdev);
1576
1577     if (netdev_vport_is_patch(netdev)) {
1578         /* By bailing out here, we don't submit the port to the sFlow module
1579          * to be considered for counter polling export.  This is correct
1580          * because the patch port represents an interface that sFlow considers
1581          * to be "internal" to the switch as a whole, and therefore not an
1582          * candidate for counter polling. */
1583         port->odp_port = ODPP_NONE;
1584         ofport_update_peer(port);
1585         return 0;
1586     }
1587
1588     error = dpif_port_query_by_name(ofproto->backer->dpif,
1589                                     netdev_vport_get_dpif_port(netdev, namebuf,
1590                                                                sizeof namebuf),
1591                                     &dpif_port);
1592     if (error) {
1593         return error;
1594     }
1595
1596     port->odp_port = dpif_port.port_no;
1597
1598     if (netdev_get_tunnel_config(netdev)) {
1599         tnl_port_add(port, port->up.netdev, port->odp_port);
1600         port->is_tunnel = true;
1601     } else {
1602         /* Sanity-check that a mapping doesn't already exist.  This
1603          * shouldn't happen for non-tunnel ports. */
1604         if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1605             VLOG_ERR("port %s already has an OpenFlow port number",
1606                      dpif_port.name);
1607             dpif_port_destroy(&dpif_port);
1608             return EBUSY;
1609         }
1610
1611         ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1612         hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1613                     hash_odp_port(port->odp_port));
1614         ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1615     }
1616     dpif_port_destroy(&dpif_port);
1617
1618     if (ofproto->sflow) {
1619         dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1620     }
1621
1622     return 0;
1623 }
1624
1625 static void
1626 port_destruct(struct ofport *port_)
1627 {
1628     struct ofport_dpif *port = ofport_dpif_cast(port_);
1629     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1630     const char *devname = netdev_get_name(port->up.netdev);
1631     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1632     const char *dp_port_name;
1633
1634     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1635     xlate_txn_start();
1636     xlate_ofport_remove(port);
1637     xlate_txn_commit();
1638
1639     dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1640                                               sizeof namebuf);
1641     if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1642         /* The underlying device is still there, so delete it.  This
1643          * happens when the ofproto is being destroyed, since the caller
1644          * assumes that removal of attached ports will happen as part of
1645          * destruction. */
1646         if (!port->is_tunnel) {
1647             dpif_port_del(ofproto->backer->dpif, port->odp_port);
1648         }
1649     }
1650
1651     if (port->peer) {
1652         port->peer->peer = NULL;
1653         port->peer = NULL;
1654     }
1655
1656     if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1657         ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1658         hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1659         ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1660     }
1661
1662     tnl_port_del(port);
1663     sset_find_and_delete(&ofproto->ports, devname);
1664     sset_find_and_delete(&ofproto->ghost_ports, devname);
1665     bundle_remove(port_);
1666     set_cfm(port_, NULL);
1667     set_bfd(port_, NULL);
1668     if (port->stp_port) {
1669         stp_port_disable(port->stp_port);
1670     }
1671     if (ofproto->sflow) {
1672         dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1673     }
1674
1675     free(port->qdscp);
1676 }
1677
1678 static void
1679 port_modified(struct ofport *port_)
1680 {
1681     struct ofport_dpif *port = ofport_dpif_cast(port_);
1682
1683     if (port->bundle && port->bundle->bond) {
1684         bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1685     }
1686
1687     if (port->cfm) {
1688         cfm_set_netdev(port->cfm, port->up.netdev);
1689     }
1690
1691     if (port->bfd) {
1692         bfd_set_netdev(port->bfd, port->up.netdev);
1693     }
1694
1695     ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1696                                      port->up.pp.hw_addr);
1697
1698     if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1699                                                 port->odp_port)) {
1700         ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1701             REV_RECONFIGURE;
1702     }
1703
1704     ofport_update_peer(port);
1705 }
1706
1707 static void
1708 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1709 {
1710     struct ofport_dpif *port = ofport_dpif_cast(port_);
1711     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1712     enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1713
1714     if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1715                    OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1716                    OFPUTIL_PC_NO_PACKET_IN)) {
1717         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1718
1719         if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1720             bundle_update(port->bundle);
1721         }
1722     }
1723 }
1724
1725 static int
1726 set_sflow(struct ofproto *ofproto_,
1727           const struct ofproto_sflow_options *sflow_options)
1728 {
1729     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1730     struct dpif_sflow *ds = ofproto->sflow;
1731
1732     if (sflow_options) {
1733         if (!ds) {
1734             struct ofport_dpif *ofport;
1735
1736             ds = ofproto->sflow = dpif_sflow_create();
1737             HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1738                 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1739             }
1740             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1741         }
1742         dpif_sflow_set_options(ds, sflow_options);
1743     } else {
1744         if (ds) {
1745             dpif_sflow_unref(ds);
1746             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1747             ofproto->sflow = NULL;
1748         }
1749     }
1750     return 0;
1751 }
1752
1753 static int
1754 set_ipfix(
1755     struct ofproto *ofproto_,
1756     const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1757     const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1758     size_t n_flow_exporters_options)
1759 {
1760     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1761     struct dpif_ipfix *di = ofproto->ipfix;
1762     bool has_options = bridge_exporter_options || flow_exporters_options;
1763
1764     if (has_options && !di) {
1765         di = ofproto->ipfix = dpif_ipfix_create();
1766     }
1767
1768     if (di) {
1769         /* Call set_options in any case to cleanly flush the flow
1770          * caches in the last exporters that are to be destroyed. */
1771         dpif_ipfix_set_options(
1772             di, bridge_exporter_options, flow_exporters_options,
1773             n_flow_exporters_options);
1774
1775         if (!has_options) {
1776             dpif_ipfix_unref(di);
1777             ofproto->ipfix = NULL;
1778         }
1779     }
1780
1781     return 0;
1782 }
1783
1784 static int
1785 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1786 {
1787     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1788     int error = 0;
1789
1790     if (s) {
1791         if (!ofport->cfm) {
1792             struct ofproto_dpif *ofproto;
1793
1794             ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1795             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1796             ofport->cfm = cfm_create(ofport->up.netdev);
1797         }
1798
1799         if (cfm_configure(ofport->cfm, s)) {
1800             error = 0;
1801             goto out;
1802         }
1803
1804         error = EINVAL;
1805     }
1806     cfm_unref(ofport->cfm);
1807     ofport->cfm = NULL;
1808 out:
1809     ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1810                                      ofport->up.pp.hw_addr);
1811     return error;
1812 }
1813
1814 static bool
1815 cfm_status_changed(struct ofport *ofport_)
1816 {
1817     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1818
1819     return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
1820 }
1821
1822 static int
1823 get_cfm_status(const struct ofport *ofport_,
1824                struct cfm_status *status)
1825 {
1826     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1827     int ret = 0;
1828
1829     if (ofport->cfm) {
1830         cfm_get_status(ofport->cfm, status);
1831     } else {
1832         ret = ENOENT;
1833     }
1834
1835     return ret;
1836 }
1837
1838 static int
1839 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1840 {
1841     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1842     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1843     struct bfd *old;
1844
1845     old = ofport->bfd;
1846     ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1847                                 cfg, ofport->up.netdev);
1848     if (ofport->bfd != old) {
1849         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1850     }
1851     ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1852                                      ofport->up.pp.hw_addr);
1853     return 0;
1854 }
1855
1856 static bool
1857 bfd_status_changed(struct ofport *ofport_)
1858 {
1859     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1860
1861     return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
1862 }
1863
1864 static int
1865 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1866 {
1867     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1868     int ret = 0;
1869
1870     if (ofport->bfd) {
1871         bfd_get_status(ofport->bfd, smap);
1872     } else {
1873         ret = ENOENT;
1874     }
1875
1876     return ret;
1877 }
1878 \f
1879 /* Spanning Tree. */
1880
1881 static void
1882 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1883 {
1884     struct ofproto_dpif *ofproto = ofproto_;
1885     struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1886     struct ofport_dpif *ofport;
1887
1888     ofport = stp_port_get_aux(sp);
1889     if (!ofport) {
1890         VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1891                      ofproto->up.name, port_num);
1892     } else {
1893         struct eth_header *eth = ofpbuf_l2(pkt);
1894
1895         netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1896         if (eth_addr_is_zero(eth->eth_src)) {
1897             VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1898                          "with unknown MAC", ofproto->up.name, port_num);
1899         } else {
1900             ofproto_dpif_send_packet(ofport, pkt);
1901         }
1902     }
1903     ofpbuf_delete(pkt);
1904 }
1905
1906 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1907 static int
1908 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1909 {
1910     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1911
1912     /* Only revalidate flows if the configuration changed. */
1913     if (!s != !ofproto->stp) {
1914         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1915     }
1916
1917     if (s) {
1918         if (!ofproto->stp) {
1919             ofproto->stp = stp_create(ofproto_->name, s->system_id,
1920                                       send_bpdu_cb, ofproto);
1921             ofproto->stp_last_tick = time_msec();
1922         }
1923
1924         stp_set_bridge_id(ofproto->stp, s->system_id);
1925         stp_set_bridge_priority(ofproto->stp, s->priority);
1926         stp_set_hello_time(ofproto->stp, s->hello_time);
1927         stp_set_max_age(ofproto->stp, s->max_age);
1928         stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1929     }  else {
1930         struct ofport *ofport;
1931
1932         HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1933             set_stp_port(ofport, NULL);
1934         }
1935
1936         stp_unref(ofproto->stp);
1937         ofproto->stp = NULL;
1938     }
1939
1940     return 0;
1941 }
1942
1943 static int
1944 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1945 {
1946     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1947
1948     if (ofproto->stp) {
1949         s->enabled = true;
1950         s->bridge_id = stp_get_bridge_id(ofproto->stp);
1951         s->designated_root = stp_get_designated_root(ofproto->stp);
1952         s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1953     } else {
1954         s->enabled = false;
1955     }
1956
1957     return 0;
1958 }
1959
1960 static void
1961 update_stp_port_state(struct ofport_dpif *ofport)
1962 {
1963     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1964     enum stp_state state;
1965
1966     /* Figure out new state. */
1967     state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1968                              : STP_DISABLED;
1969
1970     /* Update state. */
1971     if (ofport->stp_state != state) {
1972         enum ofputil_port_state of_state;
1973         bool fwd_change;
1974
1975         VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1976                     netdev_get_name(ofport->up.netdev),
1977                     stp_state_name(ofport->stp_state),
1978                     stp_state_name(state));
1979         if (stp_learn_in_state(ofport->stp_state)
1980                 != stp_learn_in_state(state)) {
1981             /* xxx Learning action flows should also be flushed. */
1982             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1983             mac_learning_flush(ofproto->ml);
1984             ovs_rwlock_unlock(&ofproto->ml->rwlock);
1985             mcast_snooping_mdb_flush(ofproto->ms);
1986         }
1987         fwd_change = stp_forward_in_state(ofport->stp_state)
1988                         != stp_forward_in_state(state);
1989
1990         ofproto->backer->need_revalidate = REV_STP;
1991         ofport->stp_state = state;
1992         ofport->stp_state_entered = time_msec();
1993
1994         if (fwd_change && ofport->bundle) {
1995             bundle_update(ofport->bundle);
1996         }
1997
1998         /* Update the STP state bits in the OpenFlow port description. */
1999         of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2000         of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2001                      : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2002                      : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2003                      : state == STP_BLOCKING ?  OFPUTIL_PS_STP_BLOCK
2004                      : 0);
2005         ofproto_port_set_state(&ofport->up, of_state);
2006     }
2007 }
2008
2009 /* Configures STP on 'ofport_' using the settings defined in 's'.  The
2010  * caller is responsible for assigning STP port numbers and ensuring
2011  * there are no duplicates. */
2012 static int
2013 set_stp_port(struct ofport *ofport_,
2014              const struct ofproto_port_stp_settings *s)
2015 {
2016     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2017     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2018     struct stp_port *sp = ofport->stp_port;
2019
2020     if (!s || !s->enable) {
2021         if (sp) {
2022             ofport->stp_port = NULL;
2023             stp_port_disable(sp);
2024             update_stp_port_state(ofport);
2025         }
2026         return 0;
2027     } else if (sp && stp_port_no(sp) != s->port_num
2028             && ofport == stp_port_get_aux(sp)) {
2029         /* The port-id changed, so disable the old one if it's not
2030          * already in use by another port. */
2031         stp_port_disable(sp);
2032     }
2033
2034     sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2035     stp_port_enable(sp);
2036
2037     stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2038     stp_port_set_aux(sp, ofport);
2039     stp_port_set_priority(sp, s->priority);
2040     stp_port_set_path_cost(sp, s->path_cost);
2041
2042     update_stp_port_state(ofport);
2043
2044     return 0;
2045 }
2046
2047 static int
2048 get_stp_port_status(struct ofport *ofport_,
2049                     struct ofproto_port_stp_status *s)
2050 {
2051     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2052     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2053     struct stp_port *sp = ofport->stp_port;
2054
2055     if (!ofproto->stp || !sp) {
2056         s->enabled = false;
2057         return 0;
2058     }
2059
2060     s->enabled = true;
2061     s->port_id = stp_port_get_id(sp);
2062     s->state = stp_port_get_state(sp);
2063     s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2064     s->role = stp_port_get_role(sp);
2065
2066     return 0;
2067 }
2068
2069 static int
2070 get_stp_port_stats(struct ofport *ofport_,
2071                    struct ofproto_port_stp_stats *s)
2072 {
2073     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2074     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2075     struct stp_port *sp = ofport->stp_port;
2076
2077     if (!ofproto->stp || !sp) {
2078         s->enabled = false;
2079         return 0;
2080     }
2081
2082     s->enabled = true;
2083     stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2084
2085     return 0;
2086 }
2087
2088 static void
2089 stp_run(struct ofproto_dpif *ofproto)
2090 {
2091     if (ofproto->stp) {
2092         long long int now = time_msec();
2093         long long int elapsed = now - ofproto->stp_last_tick;
2094         struct stp_port *sp;
2095
2096         if (elapsed > 0) {
2097             stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2098             ofproto->stp_last_tick = now;
2099         }
2100         while (stp_get_changed_port(ofproto->stp, &sp)) {
2101             struct ofport_dpif *ofport = stp_port_get_aux(sp);
2102
2103             if (ofport) {
2104                 update_stp_port_state(ofport);
2105             }
2106         }
2107
2108         if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2109             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2110             mac_learning_flush(ofproto->ml);
2111             ovs_rwlock_unlock(&ofproto->ml->rwlock);
2112             mcast_snooping_mdb_flush(ofproto->ms);
2113         }
2114     }
2115 }
2116
2117 static void
2118 stp_wait(struct ofproto_dpif *ofproto)
2119 {
2120     if (ofproto->stp) {
2121         poll_timer_wait(1000);
2122     }
2123 }
2124 \f
2125 static int
2126 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2127            size_t n_qdscp)
2128 {
2129     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2130     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2131
2132     if (ofport->n_qdscp != n_qdscp
2133         || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2134                               n_qdscp * sizeof *qdscp))) {
2135         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2136         free(ofport->qdscp);
2137         ofport->qdscp = n_qdscp
2138             ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2139             : NULL;
2140         ofport->n_qdscp = n_qdscp;
2141     }
2142
2143     return 0;
2144 }
2145 \f
2146 /* Bundles. */
2147
2148 /* Expires all MAC learning entries associated with 'bundle' and forces its
2149  * ofproto to revalidate every flow.
2150  *
2151  * Normally MAC learning entries are removed only from the ofproto associated
2152  * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2153  * are removed from every ofproto.  When patch ports and SLB bonds are in use
2154  * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2155  * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2156  * with the host from which it migrated. */
2157 static void
2158 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2159 {
2160     struct ofproto_dpif *ofproto = bundle->ofproto;
2161     struct mac_learning *ml = ofproto->ml;
2162     struct mac_entry *mac, *next_mac;
2163
2164     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2165     ovs_rwlock_wrlock(&ml->rwlock);
2166     LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2167         if (mac->port.p == bundle) {
2168             if (all_ofprotos) {
2169                 struct ofproto_dpif *o;
2170
2171                 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2172                     if (o != ofproto) {
2173                         struct mac_entry *e;
2174
2175                         ovs_rwlock_wrlock(&o->ml->rwlock);
2176                         e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2177                         if (e) {
2178                             mac_learning_expire(o->ml, e);
2179                         }
2180                         ovs_rwlock_unlock(&o->ml->rwlock);
2181                     }
2182                 }
2183             }
2184
2185             mac_learning_expire(ml, mac);
2186         }
2187     }
2188     ovs_rwlock_unlock(&ml->rwlock);
2189 }
2190
2191 static struct ofbundle *
2192 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2193 {
2194     struct ofbundle *bundle;
2195
2196     HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2197                              &ofproto->bundles) {
2198         if (bundle->aux == aux) {
2199             return bundle;
2200         }
2201     }
2202     return NULL;
2203 }
2204
2205 static void
2206 bundle_update(struct ofbundle *bundle)
2207 {
2208     struct ofport_dpif *port;
2209
2210     bundle->floodable = true;
2211     LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2212         if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2213             || port->is_layer3
2214             || !stp_forward_in_state(port->stp_state)) {
2215             bundle->floodable = false;
2216             break;
2217         }
2218     }
2219 }
2220
2221 static void
2222 bundle_del_port(struct ofport_dpif *port)
2223 {
2224     struct ofbundle *bundle = port->bundle;
2225
2226     bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2227
2228     list_remove(&port->bundle_node);
2229     port->bundle = NULL;
2230
2231     if (bundle->lacp) {
2232         lacp_slave_unregister(bundle->lacp, port);
2233     }
2234     if (bundle->bond) {
2235         bond_slave_unregister(bundle->bond, port);
2236     }
2237
2238     bundle_update(bundle);
2239 }
2240
2241 static bool
2242 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2243                 struct lacp_slave_settings *lacp)
2244 {
2245     struct ofport_dpif *port;
2246
2247     port = get_ofp_port(bundle->ofproto, ofp_port);
2248     if (!port) {
2249         return false;
2250     }
2251
2252     if (port->bundle != bundle) {
2253         bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2254         if (port->bundle) {
2255             bundle_remove(&port->up);
2256         }
2257
2258         port->bundle = bundle;
2259         list_push_back(&bundle->ports, &port->bundle_node);
2260         if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2261             || port->is_layer3
2262             || !stp_forward_in_state(port->stp_state)) {
2263             bundle->floodable = false;
2264         }
2265     }
2266     if (lacp) {
2267         bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2268         lacp_slave_register(bundle->lacp, port, lacp);
2269     }
2270
2271     return true;
2272 }
2273
2274 static void
2275 bundle_destroy(struct ofbundle *bundle)
2276 {
2277     struct ofproto_dpif *ofproto;
2278     struct ofport_dpif *port, *next_port;
2279
2280     if (!bundle) {
2281         return;
2282     }
2283
2284     ofproto = bundle->ofproto;
2285     mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2286
2287     xlate_txn_start();
2288     xlate_bundle_remove(bundle);
2289     xlate_txn_commit();
2290
2291     LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2292         bundle_del_port(port);
2293     }
2294
2295     bundle_flush_macs(bundle, true);
2296     hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2297     free(bundle->name);
2298     free(bundle->trunks);
2299     lacp_unref(bundle->lacp);
2300     bond_unref(bundle->bond);
2301     free(bundle);
2302 }
2303
2304 static int
2305 bundle_set(struct ofproto *ofproto_, void *aux,
2306            const struct ofproto_bundle_settings *s)
2307 {
2308     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2309     bool need_flush = false;
2310     struct ofport_dpif *port;
2311     struct ofbundle *bundle;
2312     unsigned long *trunks;
2313     int vlan;
2314     size_t i;
2315     bool ok;
2316
2317     if (!s) {
2318         bundle_destroy(bundle_lookup(ofproto, aux));
2319         return 0;
2320     }
2321
2322     ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2323     ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2324
2325     bundle = bundle_lookup(ofproto, aux);
2326     if (!bundle) {
2327         bundle = xmalloc(sizeof *bundle);
2328
2329         bundle->ofproto = ofproto;
2330         hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2331                     hash_pointer(aux, 0));
2332         bundle->aux = aux;
2333         bundle->name = NULL;
2334
2335         list_init(&bundle->ports);
2336         bundle->vlan_mode = PORT_VLAN_TRUNK;
2337         bundle->vlan = -1;
2338         bundle->trunks = NULL;
2339         bundle->use_priority_tags = s->use_priority_tags;
2340         bundle->lacp = NULL;
2341         bundle->bond = NULL;
2342
2343         bundle->floodable = true;
2344         mbridge_register_bundle(ofproto->mbridge, bundle);
2345     }
2346
2347     if (!bundle->name || strcmp(s->name, bundle->name)) {
2348         free(bundle->name);
2349         bundle->name = xstrdup(s->name);
2350     }
2351
2352     /* LACP. */
2353     if (s->lacp) {
2354         ofproto->lacp_enabled = true;
2355         if (!bundle->lacp) {
2356             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2357             bundle->lacp = lacp_create();
2358         }
2359         lacp_configure(bundle->lacp, s->lacp);
2360     } else {
2361         lacp_unref(bundle->lacp);
2362         bundle->lacp = NULL;
2363     }
2364
2365     /* Update set of ports. */
2366     ok = true;
2367     for (i = 0; i < s->n_slaves; i++) {
2368         if (!bundle_add_port(bundle, s->slaves[i],
2369                              s->lacp ? &s->lacp_slaves[i] : NULL)) {
2370             ok = false;
2371         }
2372     }
2373     if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2374         struct ofport_dpif *next_port;
2375
2376         LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2377             for (i = 0; i < s->n_slaves; i++) {
2378                 if (s->slaves[i] == port->up.ofp_port) {
2379                     goto found;
2380                 }
2381             }
2382
2383             bundle_del_port(port);
2384         found: ;
2385         }
2386     }
2387     ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2388
2389     if (list_is_empty(&bundle->ports)) {
2390         bundle_destroy(bundle);
2391         return EINVAL;
2392     }
2393
2394     /* Set VLAN tagging mode */
2395     if (s->vlan_mode != bundle->vlan_mode
2396         || s->use_priority_tags != bundle->use_priority_tags) {
2397         bundle->vlan_mode = s->vlan_mode;
2398         bundle->use_priority_tags = s->use_priority_tags;
2399         need_flush = true;
2400     }
2401
2402     /* Set VLAN tag. */
2403     vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2404             : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2405             : 0);
2406     if (vlan != bundle->vlan) {
2407         bundle->vlan = vlan;
2408         need_flush = true;
2409     }
2410
2411     /* Get trunked VLANs. */
2412     switch (s->vlan_mode) {
2413     case PORT_VLAN_ACCESS:
2414         trunks = NULL;
2415         break;
2416
2417     case PORT_VLAN_TRUNK:
2418         trunks = CONST_CAST(unsigned long *, s->trunks);
2419         break;
2420
2421     case PORT_VLAN_NATIVE_UNTAGGED:
2422     case PORT_VLAN_NATIVE_TAGGED:
2423         if (vlan != 0 && (!s->trunks
2424                           || !bitmap_is_set(s->trunks, vlan)
2425                           || bitmap_is_set(s->trunks, 0))) {
2426             /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2427             if (s->trunks) {
2428                 trunks = bitmap_clone(s->trunks, 4096);
2429             } else {
2430                 trunks = bitmap_allocate1(4096);
2431             }
2432             bitmap_set1(trunks, vlan);
2433             bitmap_set0(trunks, 0);
2434         } else {
2435             trunks = CONST_CAST(unsigned long *, s->trunks);
2436         }
2437         break;
2438
2439     default:
2440         OVS_NOT_REACHED();
2441     }
2442     if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2443         free(bundle->trunks);
2444         if (trunks == s->trunks) {
2445             bundle->trunks = vlan_bitmap_clone(trunks);
2446         } else {
2447             bundle->trunks = trunks;
2448             trunks = NULL;
2449         }
2450         need_flush = true;
2451     }
2452     if (trunks != s->trunks) {
2453         free(trunks);
2454     }
2455
2456     /* Bonding. */
2457     if (!list_is_short(&bundle->ports)) {
2458         bundle->ofproto->has_bonded_bundles = true;
2459         if (bundle->bond) {
2460             if (bond_reconfigure(bundle->bond, s->bond)) {
2461                 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2462             }
2463         } else {
2464             bundle->bond = bond_create(s->bond, ofproto);
2465             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2466         }
2467
2468         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2469             bond_slave_register(bundle->bond, port,
2470                                 port->up.ofp_port, port->up.netdev);
2471         }
2472     } else {
2473         bond_unref(bundle->bond);
2474         bundle->bond = NULL;
2475     }
2476
2477     /* If we changed something that would affect MAC learning, un-learn
2478      * everything on this port and force flow revalidation. */
2479     if (need_flush) {
2480         bundle_flush_macs(bundle, false);
2481     }
2482
2483     return 0;
2484 }
2485
2486 static void
2487 bundle_remove(struct ofport *port_)
2488 {
2489     struct ofport_dpif *port = ofport_dpif_cast(port_);
2490     struct ofbundle *bundle = port->bundle;
2491
2492     if (bundle) {
2493         bundle_del_port(port);
2494         if (list_is_empty(&bundle->ports)) {
2495             bundle_destroy(bundle);
2496         } else if (list_is_short(&bundle->ports)) {
2497             bond_unref(bundle->bond);
2498             bundle->bond = NULL;
2499         }
2500     }
2501 }
2502
2503 static void
2504 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2505 {
2506     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2507     struct ofport_dpif *port = port_;
2508     uint8_t ea[ETH_ADDR_LEN];
2509     int error;
2510
2511     error = netdev_get_etheraddr(port->up.netdev, ea);
2512     if (!error) {
2513         struct ofpbuf packet;
2514         void *packet_pdu;
2515
2516         ofpbuf_init(&packet, 0);
2517         packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2518                                  pdu_size);
2519         memcpy(packet_pdu, pdu, pdu_size);
2520
2521         ofproto_dpif_send_packet(port, &packet);
2522         ofpbuf_uninit(&packet);
2523     } else {
2524         VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2525                     "%s (%s)", port->bundle->name,
2526                     netdev_get_name(port->up.netdev), ovs_strerror(error));
2527     }
2528 }
2529
2530 static void
2531 bundle_send_learning_packets(struct ofbundle *bundle)
2532 {
2533     struct ofproto_dpif *ofproto = bundle->ofproto;
2534     struct ofpbuf *learning_packet;
2535     int error, n_packets, n_errors;
2536     struct mac_entry *e;
2537     struct list packets;
2538
2539     list_init(&packets);
2540     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2541     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2542         if (e->port.p != bundle) {
2543             void *port_void;
2544
2545             learning_packet = bond_compose_learning_packet(bundle->bond,
2546                                                            e->mac, e->vlan,
2547                                                            &port_void);
2548             /* Temporarily use 'frame' as a private pointer (see below). */
2549             ovs_assert(learning_packet->frame == ofpbuf_data(learning_packet));
2550             learning_packet->frame = port_void;
2551             list_push_back(&packets, &learning_packet->list_node);
2552         }
2553     }
2554     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2555
2556     error = n_packets = n_errors = 0;
2557     LIST_FOR_EACH (learning_packet, list_node, &packets) {
2558         int ret;
2559         void *port_void = learning_packet->frame;
2560
2561         /* Restore 'frame'. */
2562         learning_packet->frame = ofpbuf_data(learning_packet);
2563         ret = ofproto_dpif_send_packet(port_void, learning_packet);
2564         if (ret) {
2565             error = ret;
2566             n_errors++;
2567         }
2568         n_packets++;
2569     }
2570     ofpbuf_list_delete(&packets);
2571
2572     if (n_errors) {
2573         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2574         VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2575                      "packets, last error was: %s",
2576                      bundle->name, n_errors, n_packets, ovs_strerror(error));
2577     } else {
2578         VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2579                  bundle->name, n_packets);
2580     }
2581 }
2582
2583 static void
2584 bundle_run(struct ofbundle *bundle)
2585 {
2586     if (bundle->lacp) {
2587         lacp_run(bundle->lacp, send_pdu_cb);
2588     }
2589     if (bundle->bond) {
2590         struct ofport_dpif *port;
2591
2592         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2593             bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2594         }
2595
2596         if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2597             bundle->ofproto->backer->need_revalidate = REV_BOND;
2598         }
2599
2600         if (bond_should_send_learning_packets(bundle->bond)) {
2601             bundle_send_learning_packets(bundle);
2602         }
2603     }
2604 }
2605
2606 static void
2607 bundle_wait(struct ofbundle *bundle)
2608 {
2609     if (bundle->lacp) {
2610         lacp_wait(bundle->lacp);
2611     }
2612     if (bundle->bond) {
2613         bond_wait(bundle->bond);
2614     }
2615 }
2616 \f
2617 /* Mirrors. */
2618
2619 static int
2620 mirror_set__(struct ofproto *ofproto_, void *aux,
2621              const struct ofproto_mirror_settings *s)
2622 {
2623     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2624     struct ofbundle **srcs, **dsts;
2625     int error;
2626     size_t i;
2627
2628     if (!s) {
2629         mirror_destroy(ofproto->mbridge, aux);
2630         return 0;
2631     }
2632
2633     srcs = xmalloc(s->n_srcs * sizeof *srcs);
2634     dsts = xmalloc(s->n_dsts * sizeof *dsts);
2635
2636     for (i = 0; i < s->n_srcs; i++) {
2637         srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2638     }
2639
2640     for (i = 0; i < s->n_dsts; i++) {
2641         dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2642     }
2643
2644     error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2645                        s->n_dsts, s->src_vlans,
2646                        bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2647     free(srcs);
2648     free(dsts);
2649     return error;
2650 }
2651
2652 static int
2653 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2654                    uint64_t *packets, uint64_t *bytes)
2655 {
2656     return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2657                             bytes);
2658 }
2659
2660 static int
2661 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2662 {
2663     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2664     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2665     if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2666         mac_learning_flush(ofproto->ml);
2667     }
2668     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2669     return 0;
2670 }
2671
2672 static bool
2673 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2674 {
2675     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2676     struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2677     return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2678 }
2679
2680 static void
2681 forward_bpdu_changed(struct ofproto *ofproto_)
2682 {
2683     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2684     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2685 }
2686
2687 static void
2688 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2689                      size_t max_entries)
2690 {
2691     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2692     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2693     mac_learning_set_idle_time(ofproto->ml, idle_time);
2694     mac_learning_set_max_entries(ofproto->ml, max_entries);
2695     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2696 }
2697
2698 /* Configures multicast snooping on 'ofport' using the settings
2699  * defined in 's'. */
2700 static int
2701 set_mcast_snooping(struct ofproto *ofproto_,
2702                    const struct ofproto_mcast_snooping_settings *s)
2703 {
2704     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2705
2706     /* Only revalidate flows if the configuration changed. */
2707     if (!s != !ofproto->ms) {
2708         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2709     }
2710
2711     if (s) {
2712         if (!ofproto->ms) {
2713             ofproto->ms = mcast_snooping_create();
2714         }
2715
2716         ovs_rwlock_wrlock(&ofproto->ms->rwlock);
2717         mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
2718         mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
2719         if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
2720             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2721         }
2722         ovs_rwlock_unlock(&ofproto->ms->rwlock);
2723     } else {
2724         mcast_snooping_unref(ofproto->ms);
2725         ofproto->ms = NULL;
2726     }
2727
2728     return 0;
2729 }
2730
2731 /* Configures multicast snooping port's flood setting on 'ofproto'. */
2732 static int
2733 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux, bool flood)
2734 {
2735     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2736     struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2737
2738     if (ofproto->ms) {
2739         ovs_rwlock_wrlock(&ofproto->ms->rwlock);
2740         mcast_snooping_set_port_flood(ofproto->ms, bundle->vlan, bundle,
2741                                       flood);
2742         ovs_rwlock_unlock(&ofproto->ms->rwlock);
2743     }
2744     return 0;
2745 }
2746
2747 \f
2748 /* Ports. */
2749
2750 static struct ofport_dpif *
2751 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2752 {
2753     struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2754     return ofport ? ofport_dpif_cast(ofport) : NULL;
2755 }
2756
2757 static void
2758 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2759                             struct ofproto_port *ofproto_port,
2760                             struct dpif_port *dpif_port)
2761 {
2762     ofproto_port->name = dpif_port->name;
2763     ofproto_port->type = dpif_port->type;
2764     ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2765 }
2766
2767 static void
2768 ofport_update_peer(struct ofport_dpif *ofport)
2769 {
2770     const struct ofproto_dpif *ofproto;
2771     struct dpif_backer *backer;
2772     char *peer_name;
2773
2774     if (!netdev_vport_is_patch(ofport->up.netdev)) {
2775         return;
2776     }
2777
2778     backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2779     backer->need_revalidate = REV_RECONFIGURE;
2780
2781     if (ofport->peer) {
2782         ofport->peer->peer = NULL;
2783         ofport->peer = NULL;
2784     }
2785
2786     peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2787     if (!peer_name) {
2788         return;
2789     }
2790
2791     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2792         struct ofport *peer_ofport;
2793         struct ofport_dpif *peer;
2794         char *peer_peer;
2795
2796         if (ofproto->backer != backer) {
2797             continue;
2798         }
2799
2800         peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2801         if (!peer_ofport) {
2802             continue;
2803         }
2804
2805         peer = ofport_dpif_cast(peer_ofport);
2806         peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2807         if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2808                                  peer_peer)) {
2809             ofport->peer = peer;
2810             ofport->peer->peer = ofport;
2811         }
2812         free(peer_peer);
2813
2814         break;
2815     }
2816     free(peer_name);
2817 }
2818
2819 static void
2820 port_run(struct ofport_dpif *ofport)
2821 {
2822     long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2823     bool carrier_changed = carrier_seq != ofport->carrier_seq;
2824     bool enable = netdev_get_carrier(ofport->up.netdev);
2825     bool cfm_enable = false;
2826     bool bfd_enable = false;
2827
2828     ofport->carrier_seq = carrier_seq;
2829
2830     if (ofport->cfm) {
2831         int cfm_opup = cfm_get_opup(ofport->cfm);
2832
2833         cfm_enable = !cfm_get_fault(ofport->cfm);
2834
2835         if (cfm_opup >= 0) {
2836             cfm_enable = cfm_enable && cfm_opup;
2837         }
2838     }
2839
2840     if (ofport->bfd) {
2841         bfd_enable = bfd_forwarding(ofport->bfd);
2842     }
2843
2844     if (ofport->bfd || ofport->cfm) {
2845         enable = enable && (cfm_enable || bfd_enable);
2846     }
2847
2848     if (ofport->bundle) {
2849         enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2850         if (carrier_changed) {
2851             lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2852         }
2853     }
2854
2855     if (ofport->may_enable != enable) {
2856         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2857         ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2858     }
2859
2860     ofport->may_enable = enable;
2861 }
2862
2863 static int
2864 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2865                    struct ofproto_port *ofproto_port)
2866 {
2867     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2868     struct dpif_port dpif_port;
2869     int error;
2870
2871     if (sset_contains(&ofproto->ghost_ports, devname)) {
2872         const char *type = netdev_get_type_from_name(devname);
2873
2874         /* We may be called before ofproto->up.port_by_name is populated with
2875          * the appropriate ofport.  For this reason, we must get the name and
2876          * type from the netdev layer directly. */
2877         if (type) {
2878             const struct ofport *ofport;
2879
2880             ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2881             ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2882             ofproto_port->name = xstrdup(devname);
2883             ofproto_port->type = xstrdup(type);
2884             return 0;
2885         }
2886         return ENODEV;
2887     }
2888
2889     if (!sset_contains(&ofproto->ports, devname)) {
2890         return ENODEV;
2891     }
2892     error = dpif_port_query_by_name(ofproto->backer->dpif,
2893                                     devname, &dpif_port);
2894     if (!error) {
2895         ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2896     }
2897     return error;
2898 }
2899
2900 static int
2901 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2902 {
2903     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2904     const char *devname = netdev_get_name(netdev);
2905     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2906     const char *dp_port_name;
2907
2908     if (netdev_vport_is_patch(netdev)) {
2909         sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2910         return 0;
2911     }
2912
2913     dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2914     if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2915         odp_port_t port_no = ODPP_NONE;
2916         int error;
2917
2918         error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2919         if (error) {
2920             return error;
2921         }
2922         if (netdev_get_tunnel_config(netdev)) {
2923             simap_put(&ofproto->backer->tnl_backers,
2924                       dp_port_name, odp_to_u32(port_no));
2925         }
2926     }
2927
2928     if (netdev_get_tunnel_config(netdev)) {
2929         sset_add(&ofproto->ghost_ports, devname);
2930     } else {
2931         sset_add(&ofproto->ports, devname);
2932     }
2933     return 0;
2934 }
2935
2936 static int
2937 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2938 {
2939     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2940     struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2941     int error = 0;
2942
2943     if (!ofport) {
2944         return 0;
2945     }
2946
2947     sset_find_and_delete(&ofproto->ghost_ports,
2948                          netdev_get_name(ofport->up.netdev));
2949     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2950     if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2951         error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2952         if (!error) {
2953             /* The caller is going to close ofport->up.netdev.  If this is a
2954              * bonded port, then the bond is using that netdev, so remove it
2955              * from the bond.  The client will need to reconfigure everything
2956              * after deleting ports, so then the slave will get re-added. */
2957             bundle_remove(&ofport->up);
2958         }
2959     }
2960     return error;
2961 }
2962
2963 static int
2964 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2965 {
2966     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2967     int error;
2968
2969     error = netdev_get_stats(ofport->up.netdev, stats);
2970
2971     if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2972         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2973
2974         ovs_mutex_lock(&ofproto->stats_mutex);
2975         /* ofproto->stats.tx_packets represents packets that we created
2976          * internally and sent to some port (e.g. packets sent with
2977          * ofproto_dpif_send_packet()).  Account for them as if they had
2978          * come from OFPP_LOCAL and got forwarded. */
2979
2980         if (stats->rx_packets != UINT64_MAX) {
2981             stats->rx_packets += ofproto->stats.tx_packets;
2982         }
2983
2984         if (stats->rx_bytes != UINT64_MAX) {
2985             stats->rx_bytes += ofproto->stats.tx_bytes;
2986         }
2987
2988         /* ofproto->stats.rx_packets represents packets that were received on
2989          * some port and we processed internally and dropped (e.g. STP).
2990          * Account for them as if they had been forwarded to OFPP_LOCAL. */
2991
2992         if (stats->tx_packets != UINT64_MAX) {
2993             stats->tx_packets += ofproto->stats.rx_packets;
2994         }
2995
2996         if (stats->tx_bytes != UINT64_MAX) {
2997             stats->tx_bytes += ofproto->stats.rx_bytes;
2998         }
2999         ovs_mutex_unlock(&ofproto->stats_mutex);
3000     }
3001
3002     return error;
3003 }
3004
3005 struct port_dump_state {
3006     uint32_t bucket;
3007     uint32_t offset;
3008     bool ghost;
3009
3010     struct ofproto_port port;
3011     bool has_port;
3012 };
3013
3014 static int
3015 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3016 {
3017     *statep = xzalloc(sizeof(struct port_dump_state));
3018     return 0;
3019 }
3020
3021 static int
3022 port_dump_next(const struct ofproto *ofproto_, void *state_,
3023                struct ofproto_port *port)
3024 {
3025     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3026     struct port_dump_state *state = state_;
3027     const struct sset *sset;
3028     struct sset_node *node;
3029
3030     if (state->has_port) {
3031         ofproto_port_destroy(&state->port);
3032         state->has_port = false;
3033     }
3034     sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3035     while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3036         int error;
3037
3038         error = port_query_by_name(ofproto_, node->name, &state->port);
3039         if (!error) {
3040             *port = state->port;
3041             state->has_port = true;
3042             return 0;
3043         } else if (error != ENODEV) {
3044             return error;
3045         }
3046     }
3047
3048     if (!state->ghost) {
3049         state->ghost = true;
3050         state->bucket = 0;
3051         state->offset = 0;
3052         return port_dump_next(ofproto_, state_, port);
3053     }
3054
3055     return EOF;
3056 }
3057
3058 static int
3059 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3060 {
3061     struct port_dump_state *state = state_;
3062
3063     if (state->has_port) {
3064         ofproto_port_destroy(&state->port);
3065     }
3066     free(state);
3067     return 0;
3068 }
3069
3070 static int
3071 port_poll(const struct ofproto *ofproto_, char **devnamep)
3072 {
3073     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3074
3075     if (ofproto->port_poll_errno) {
3076         int error = ofproto->port_poll_errno;
3077         ofproto->port_poll_errno = 0;
3078         return error;
3079     }
3080
3081     if (sset_is_empty(&ofproto->port_poll_set)) {
3082         return EAGAIN;
3083     }
3084
3085     *devnamep = sset_pop(&ofproto->port_poll_set);
3086     return 0;
3087 }
3088
3089 static void
3090 port_poll_wait(const struct ofproto *ofproto_)
3091 {
3092     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3093     dpif_port_poll_wait(ofproto->backer->dpif);
3094 }
3095
3096 static int
3097 port_is_lacp_current(const struct ofport *ofport_)
3098 {
3099     const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3100     return (ofport->bundle && ofport->bundle->lacp
3101             ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3102             : -1);
3103 }
3104 \f
3105 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3106  * then delete it entirely. */
3107 static void
3108 rule_expire(struct rule_dpif *rule)
3109     OVS_REQUIRES(ofproto_mutex)
3110 {
3111     uint16_t hard_timeout, idle_timeout;
3112     long long int now = time_msec();
3113     int reason = -1;
3114
3115     hard_timeout = rule->up.hard_timeout;
3116     idle_timeout = rule->up.idle_timeout;
3117
3118     /* Has 'rule' expired? */
3119     if (hard_timeout) {
3120         long long int modified;
3121
3122         ovs_mutex_lock(&rule->up.mutex);
3123         modified = rule->up.modified;
3124         ovs_mutex_unlock(&rule->up.mutex);
3125
3126         if (now > modified + hard_timeout * 1000) {
3127             reason = OFPRR_HARD_TIMEOUT;
3128         }
3129     }
3130
3131     if (reason < 0 && idle_timeout) {
3132         long long int used;
3133
3134         ovs_mutex_lock(&rule->stats_mutex);
3135         used = rule->stats.used;
3136         ovs_mutex_unlock(&rule->stats_mutex);
3137
3138         if (now > used + idle_timeout * 1000) {
3139             reason = OFPRR_IDLE_TIMEOUT;
3140         }
3141     }
3142
3143     if (reason >= 0) {
3144         COVERAGE_INC(ofproto_dpif_expired);
3145         ofproto_rule_expire(&rule->up, reason);
3146     }
3147 }
3148
3149 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3150  * 'flow' must reflect the data in 'packet'. */
3151 int
3152 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3153                              const struct flow *flow,
3154                              struct rule_dpif *rule,
3155                              const struct ofpact *ofpacts, size_t ofpacts_len,
3156                              struct ofpbuf *packet)
3157 {
3158     struct dpif_flow_stats stats;
3159     struct xlate_out xout;
3160     struct xlate_in xin;
3161     ofp_port_t in_port;
3162     struct dpif_execute execute;
3163     int error;
3164
3165     ovs_assert((rule != NULL) != (ofpacts != NULL));
3166
3167     dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3168
3169     if (rule) {
3170         rule_dpif_credit_stats(rule, &stats);
3171     }
3172
3173     xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
3174     xin.ofpacts = ofpacts;
3175     xin.ofpacts_len = ofpacts_len;
3176     xin.resubmit_stats = &stats;
3177     xlate_actions(&xin, &xout);
3178
3179     execute.actions = ofpbuf_data(&xout.odp_actions);
3180     execute.actions_len = ofpbuf_size(&xout.odp_actions);
3181     execute.packet = packet;
3182     execute.md = pkt_metadata_from_flow(flow);
3183     execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3184
3185     /* Fix up in_port. */
3186     in_port = flow->in_port.ofp_port;
3187     if (in_port == OFPP_NONE) {
3188         in_port = OFPP_LOCAL;
3189     }
3190     execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3191
3192     error = dpif_execute(ofproto->backer->dpif, &execute);
3193
3194     xlate_out_uninit(&xout);
3195
3196     return error;
3197 }
3198
3199 void
3200 rule_dpif_credit_stats(struct rule_dpif *rule,
3201                        const struct dpif_flow_stats *stats)
3202 {
3203     ovs_mutex_lock(&rule->stats_mutex);
3204     rule->stats.n_packets += stats->n_packets;
3205     rule->stats.n_bytes += stats->n_bytes;
3206     rule->stats.used = MAX(rule->stats.used, stats->used);
3207     ovs_mutex_unlock(&rule->stats_mutex);
3208 }
3209
3210 ovs_be64
3211 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3212     OVS_REQUIRES(rule->up.mutex)
3213 {
3214     return rule->up.flow_cookie;
3215 }
3216
3217 void
3218 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3219                      uint16_t hard_timeout)
3220 {
3221     ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3222 }
3223
3224 /* Returns 'rule''s actions.  The caller owns a reference on the returned
3225  * actions and must eventually release it (with rule_actions_unref()) to avoid
3226  * a memory leak. */
3227 const struct rule_actions *
3228 rule_dpif_get_actions(const struct rule_dpif *rule)
3229 {
3230     return rule_get_actions(&rule->up);
3231 }
3232
3233 /* Sets 'rule''s recirculation id. */
3234 static void
3235 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3236     OVS_REQUIRES(rule->up.mutex)
3237 {
3238     ovs_assert(!rule->recirc_id);
3239     rule->recirc_id = id;
3240 }
3241
3242 /* Returns 'rule''s recirculation id. */
3243 uint32_t
3244 rule_dpif_get_recirc_id(struct rule_dpif *rule)
3245     OVS_REQUIRES(rule->up.mutex)
3246 {
3247     if (!rule->recirc_id) {
3248         struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3249
3250         rule_dpif_set_recirc_id(rule, ofproto_dpif_alloc_recirc_id(ofproto));
3251     }
3252     return rule->recirc_id;
3253 }
3254
3255 /* Sets 'rule''s recirculation id. */
3256 void
3257 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3258 {
3259     struct rule_dpif *rule = rule_dpif_cast(rule_);
3260
3261     ovs_mutex_lock(&rule->up.mutex);
3262     rule_dpif_set_recirc_id(rule, id);
3263     ovs_mutex_unlock(&rule->up.mutex);
3264 }
3265
3266 /* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3267  * If 'wc' is non-null, sets the fields that were relevant as part of
3268  * the lookup. Returns the table_id where a match or miss occurred.
3269  *
3270  * The return value will be zero unless there was a miss and
3271  * OFPTC11_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3272  * where misses occur.
3273  *
3274  * The rule is returned in '*rule', which is valid at least until the next
3275  * RCU quiescent period.  If the '*rule' needs to stay around longer,
3276  * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3277  * on it before this returns. */
3278 uint8_t
3279 rule_dpif_lookup(struct ofproto_dpif *ofproto, struct flow *flow,
3280                  struct flow_wildcards *wc, struct rule_dpif **rule,
3281                  bool take_ref, const struct dpif_flow_stats *stats)
3282 {
3283     enum rule_dpif_lookup_verdict verdict;
3284     enum ofputil_port_config config = 0;
3285     uint8_t table_id;
3286
3287     if (ofproto_dpif_get_enable_recirc(ofproto)) {
3288         /* Always exactly match recirc_id since datapath supports
3289          * recirculation.  */
3290         if (wc) {
3291             wc->masks.recirc_id = UINT32_MAX;
3292         }
3293
3294         /* Start looking up from internal table for post recirculation flows
3295          * or packets. We can also simply send all, including normal flows
3296          * or packets to the internal table. They will not match any post
3297          * recirculation rules except the 'catch all' rule that resubmit
3298          * them to table 0.
3299          *
3300          * As an optimization, we send normal flows and packets to table 0
3301          * directly, saving one table lookup.  */
3302         table_id = flow->recirc_id ? TBL_INTERNAL : 0;
3303     } else {
3304         table_id = 0;
3305     }
3306
3307     verdict = rule_dpif_lookup_from_table(ofproto, flow, wc, true,
3308                                           &table_id, rule, take_ref, stats);
3309
3310     switch (verdict) {
3311     case RULE_DPIF_LOOKUP_VERDICT_MATCH:
3312         return table_id;
3313     case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER: {
3314         struct ofport_dpif *port;
3315
3316         port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3317         if (!port) {
3318             VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3319                          flow->in_port.ofp_port);
3320         }
3321         config = port ? port->up.pp.config : 0;
3322         break;
3323     }
3324     case RULE_DPIF_LOOKUP_VERDICT_DROP:
3325         config = OFPUTIL_PC_NO_PACKET_IN;
3326         break;
3327     case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
3328         if (!connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3329             config = OFPUTIL_PC_NO_PACKET_IN;
3330         }
3331         break;
3332     default:
3333         OVS_NOT_REACHED();
3334     }
3335
3336     choose_miss_rule(config, ofproto->miss_rule,
3337                      ofproto->no_packet_in_rule, rule, take_ref);
3338     return table_id;
3339 }
3340
3341 /* The returned rule is valid at least until the next RCU quiescent period.
3342  * If the '*rule' needs to stay around longer, a non-zero 'take_ref' must be
3343  * passed in to cause a reference to be taken on it before this returns. */
3344 static struct rule_dpif *
3345 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3346                           const struct flow *flow, struct flow_wildcards *wc,
3347                           bool take_ref)
3348 {
3349     struct classifier *cls = &ofproto->up.tables[table_id].cls;
3350     const struct cls_rule *cls_rule;
3351     struct rule_dpif *rule;
3352     struct flow ofpc_normal_flow;
3353
3354     if (ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3355         /* We always unwildcard dl_type and nw_frag (for IP), so they
3356          * need not be unwildcarded here. */
3357
3358         if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3359             if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3360                 /* We must pretend that transport ports are unavailable. */
3361                 ofpc_normal_flow = *flow;
3362                 ofpc_normal_flow.tp_src = htons(0);
3363                 ofpc_normal_flow.tp_dst = htons(0);
3364                 flow = &ofpc_normal_flow;
3365             } else {
3366                 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3367                  * Use the drop_frags_rule (which cannot disappear). */
3368                 cls_rule = &ofproto->drop_frags_rule->up.cr;
3369                 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3370                 if (take_ref) {
3371                     rule_dpif_ref(rule);
3372                 }
3373                 return rule;
3374             }
3375         }
3376     }
3377
3378     do {
3379         cls_rule = classifier_lookup(cls, flow, wc);
3380
3381         rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3382
3383         /* Try again if the rule was released before we get the reference. */
3384     } while (rule && take_ref && !rule_dpif_try_ref(rule));
3385
3386     return rule;
3387 }
3388
3389 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3390  * Stores the rule that was found in '*rule', or NULL if none was found.
3391  * Updates 'wc', if nonnull, to reflect the fields that were used during the
3392  * lookup.
3393  *
3394  * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3395  * if none is found then the table miss configuration for that table is
3396  * honored, which can result in additional lookups in other OpenFlow tables.
3397  * In this case the function updates '*table_id' to reflect the final OpenFlow
3398  * table that was searched.
3399  *
3400  * If 'honor_table_miss' is false, then only one table lookup occurs, in
3401  * '*table_id'.
3402  *
3403  * Returns:
3404  *
3405  *    - RULE_DPIF_LOOKUP_VERDICT_MATCH if a rule (in '*rule') was found.
3406  *
3407  *    - RULE_OFPTC_TABLE_MISS_CONTROLLER if no rule was found and either:
3408  *      + 'honor_table_miss' is false
3409  *      + a table miss configuration specified that the packet should be
3410  *        sent to the controller in this case.
3411  *
3412  *    - RULE_DPIF_LOOKUP_VERDICT_DROP if no rule was found, 'honor_table_miss'
3413  *      is true and a table miss configuration specified that the packet
3414  *      should be dropped in this case.
3415  *
3416  *    - RULE_DPIF_LOOKUP_VERDICT_DEFAULT if no rule was found,
3417  *      'honor_table_miss' is true and a table miss configuration has
3418  *      not been specified in this case.
3419  *
3420  * The rule is returned in '*rule', which is valid at least until the next
3421  * RCU quiescent period.  If the '*rule' needs to stay around longer,
3422  * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3423  * on it before this returns. */
3424 enum rule_dpif_lookup_verdict
3425 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3426                             const struct flow *flow,
3427                             struct flow_wildcards *wc,
3428                             bool honor_table_miss,
3429                             uint8_t *table_id, struct rule_dpif **rule,
3430                             bool take_ref, const struct dpif_flow_stats *stats)
3431 {
3432     uint8_t next_id;
3433
3434     for (next_id = *table_id;
3435          next_id < ofproto->up.n_tables;
3436          next_id++, next_id += (next_id == TBL_INTERNAL))
3437     {
3438         *table_id = next_id;
3439         *rule = rule_dpif_lookup_in_table(ofproto, *table_id, flow, wc,
3440                                           take_ref);
3441         if (stats) {
3442             struct oftable *tbl = &ofproto->up.tables[next_id];
3443             atomic_ulong *stat = *rule ? &tbl->n_matched : &tbl->n_missed;
3444             unsigned long orig;
3445             atomic_add(stat, stats->n_packets, &orig);
3446         }
3447         if (*rule) {
3448             return RULE_DPIF_LOOKUP_VERDICT_MATCH;
3449         } else if (!honor_table_miss) {
3450             return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3451         } else {
3452             switch (ofproto_table_get_config(&ofproto->up, *table_id)) {
3453             case OFPROTO_TABLE_MISS_CONTINUE:
3454                 break;
3455
3456             case OFPROTO_TABLE_MISS_CONTROLLER:
3457                 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3458
3459             case OFPROTO_TABLE_MISS_DROP:
3460                 return RULE_DPIF_LOOKUP_VERDICT_DROP;
3461
3462             case OFPROTO_TABLE_MISS_DEFAULT:
3463                 return RULE_DPIF_LOOKUP_VERDICT_DEFAULT;
3464             }
3465         }
3466     }
3467
3468     return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3469 }
3470
3471 /* Given a port configuration (specified as zero if there's no port), chooses
3472  * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3473  * flow table miss.
3474  *
3475  * The rule is returned in '*rule', which is valid at least until the next
3476  * RCU quiescent period.  If the '*rule' needs to stay around longer,
3477  * a reference must be taken on it (rule_dpif_ref()).
3478  */
3479 void
3480 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3481                  struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule,
3482                  bool take_ref)
3483 {
3484     *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3485     if (take_ref) {
3486         rule_dpif_ref(*rule);
3487     }
3488 }
3489
3490 static void
3491 complete_operation(struct rule_dpif *rule)
3492     OVS_REQUIRES(ofproto_mutex)
3493 {
3494     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3495
3496     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3497 }
3498
3499 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3500 {
3501     return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3502 }
3503
3504 static struct rule *
3505 rule_alloc(void)
3506 {
3507     struct rule_dpif *rule = xmalloc(sizeof *rule);
3508     return &rule->up;
3509 }
3510
3511 static void
3512 rule_dealloc(struct rule *rule_)
3513 {
3514     struct rule_dpif *rule = rule_dpif_cast(rule_);
3515     free(rule);
3516 }
3517
3518 static enum ofperr
3519 rule_construct(struct rule *rule_)
3520     OVS_NO_THREAD_SAFETY_ANALYSIS
3521 {
3522     struct rule_dpif *rule = rule_dpif_cast(rule_);
3523     ovs_mutex_init_adaptive(&rule->stats_mutex);
3524     rule->stats.n_packets = 0;
3525     rule->stats.n_bytes = 0;
3526     rule->stats.used = rule->up.modified;
3527     rule->recirc_id = 0;
3528
3529     return 0;
3530 }
3531
3532 static enum ofperr
3533 rule_insert(struct rule *rule_)
3534     OVS_REQUIRES(ofproto_mutex)
3535 {
3536     struct rule_dpif *rule = rule_dpif_cast(rule_);
3537     complete_operation(rule);
3538     return 0;
3539 }
3540
3541 static void
3542 rule_delete(struct rule *rule_)
3543     OVS_REQUIRES(ofproto_mutex)
3544 {
3545     struct rule_dpif *rule = rule_dpif_cast(rule_);
3546     complete_operation(rule);
3547 }
3548
3549 static void
3550 rule_destruct(struct rule *rule_)
3551 {
3552     struct rule_dpif *rule = rule_dpif_cast(rule_);
3553
3554     ovs_mutex_destroy(&rule->stats_mutex);
3555     if (rule->recirc_id) {
3556         struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3557
3558         ofproto_dpif_free_recirc_id(ofproto, rule->recirc_id);
3559     }
3560 }
3561
3562 static void
3563 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3564                long long int *used)
3565 {
3566     struct rule_dpif *rule = rule_dpif_cast(rule_);
3567
3568     ovs_mutex_lock(&rule->stats_mutex);
3569     *packets = rule->stats.n_packets;
3570     *bytes = rule->stats.n_bytes;
3571     *used = rule->stats.used;
3572     ovs_mutex_unlock(&rule->stats_mutex);
3573 }
3574
3575 static void
3576 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3577                   struct ofpbuf *packet)
3578 {
3579     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3580
3581     ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3582 }
3583
3584 static enum ofperr
3585 rule_execute(struct rule *rule, const struct flow *flow,
3586              struct ofpbuf *packet)
3587 {
3588     rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3589     ofpbuf_delete(packet);
3590     return 0;
3591 }
3592
3593 static void
3594 rule_modify_actions(struct rule *rule_, bool reset_counters)
3595     OVS_REQUIRES(ofproto_mutex)
3596 {
3597     struct rule_dpif *rule = rule_dpif_cast(rule_);
3598
3599     if (reset_counters) {
3600         ovs_mutex_lock(&rule->stats_mutex);
3601         rule->stats.n_packets = 0;
3602         rule->stats.n_bytes = 0;
3603         ovs_mutex_unlock(&rule->stats_mutex);
3604     }
3605
3606     complete_operation(rule);
3607 }
3608
3609 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3610 {
3611     return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3612 }
3613
3614 static struct ofgroup *
3615 group_alloc(void)
3616 {
3617     struct group_dpif *group = xzalloc(sizeof *group);
3618     return &group->up;
3619 }
3620
3621 static void
3622 group_dealloc(struct ofgroup *group_)
3623 {
3624     struct group_dpif *group = group_dpif_cast(group_);
3625     free(group);
3626 }
3627
3628 static void
3629 group_construct_stats(struct group_dpif *group)
3630     OVS_REQUIRES(group->stats_mutex)
3631 {
3632     struct ofputil_bucket *bucket;
3633     const struct list *buckets;
3634
3635     group->packet_count = 0;
3636     group->byte_count = 0;
3637
3638     group_dpif_get_buckets(group, &buckets);
3639     LIST_FOR_EACH (bucket, list_node, buckets) {
3640         bucket->stats.packet_count = 0;
3641         bucket->stats.byte_count = 0;
3642     }
3643 }
3644
3645 void
3646 group_dpif_credit_stats(struct group_dpif *group,
3647                         struct ofputil_bucket *bucket,
3648                         const struct dpif_flow_stats *stats)
3649 {
3650     ovs_mutex_lock(&group->stats_mutex);
3651     group->packet_count += stats->n_packets;
3652     group->byte_count += stats->n_bytes;
3653     if (bucket) {
3654         bucket->stats.packet_count += stats->n_packets;
3655         bucket->stats.byte_count += stats->n_bytes;
3656     } else { /* Credit to all buckets */
3657         const struct list *buckets;
3658
3659         group_dpif_get_buckets(group, &buckets);
3660         LIST_FOR_EACH (bucket, list_node, buckets) {
3661             bucket->stats.packet_count += stats->n_packets;
3662             bucket->stats.byte_count += stats->n_bytes;
3663         }
3664     }
3665     ovs_mutex_unlock(&group->stats_mutex);
3666 }
3667
3668 static enum ofperr
3669 group_construct(struct ofgroup *group_)
3670 {
3671     struct group_dpif *group = group_dpif_cast(group_);
3672     const struct ofputil_bucket *bucket;
3673
3674     /* Prevent group chaining because our locking structure makes it hard to
3675      * implement deadlock-free.  (See xlate_group_resource_check().) */
3676     LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3677         const struct ofpact *a;
3678
3679         OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
3680             if (a->type == OFPACT_GROUP) {
3681                 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
3682             }
3683         }
3684     }
3685
3686     ovs_mutex_init_adaptive(&group->stats_mutex);
3687     ovs_mutex_lock(&group->stats_mutex);
3688     group_construct_stats(group);
3689     ovs_mutex_unlock(&group->stats_mutex);
3690     return 0;
3691 }
3692
3693 static void
3694 group_destruct(struct ofgroup *group_)
3695 {
3696     struct group_dpif *group = group_dpif_cast(group_);
3697     ovs_mutex_destroy(&group->stats_mutex);
3698 }
3699
3700 static enum ofperr
3701 group_modify(struct ofgroup *group_)
3702 {
3703     struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
3704
3705     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3706
3707     return 0;
3708 }
3709
3710 static enum ofperr
3711 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3712 {
3713     struct group_dpif *group = group_dpif_cast(group_);
3714     struct ofputil_bucket *bucket;
3715     const struct list *buckets;
3716     struct bucket_counter *bucket_stats;
3717
3718     ovs_mutex_lock(&group->stats_mutex);
3719     ogs->packet_count = group->packet_count;
3720     ogs->byte_count = group->byte_count;
3721
3722     group_dpif_get_buckets(group, &buckets);
3723     bucket_stats = ogs->bucket_stats;
3724     LIST_FOR_EACH (bucket, list_node, buckets) {
3725         bucket_stats->packet_count = bucket->stats.packet_count;
3726         bucket_stats->byte_count = bucket->stats.byte_count;
3727         bucket_stats++;
3728     }
3729     ovs_mutex_unlock(&group->stats_mutex);
3730
3731     return 0;
3732 }
3733
3734 /* If the group exists, this function increments the groups's reference count.
3735  *
3736  * Make sure to call group_dpif_unref() after no longer needing to maintain
3737  * a reference to the group. */
3738 bool
3739 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3740                   struct group_dpif **group)
3741 {
3742     struct ofgroup *ofgroup;
3743     bool found;
3744
3745     found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3746     *group = found ?  group_dpif_cast(ofgroup) : NULL;
3747
3748     return found;
3749 }
3750
3751 void
3752 group_dpif_get_buckets(const struct group_dpif *group,
3753                        const struct list **buckets)
3754 {
3755     *buckets = &group->up.buckets;
3756 }
3757
3758 enum ofp11_group_type
3759 group_dpif_get_type(const struct group_dpif *group)
3760 {
3761     return group->up.type;
3762 }
3763 \f
3764 /* Sends 'packet' out 'ofport'.
3765  * May modify 'packet'.
3766  * Returns 0 if successful, otherwise a positive errno value. */
3767 int
3768 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3769 {
3770     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3771     int error;
3772
3773     error = xlate_send_packet(ofport, packet);
3774
3775     ovs_mutex_lock(&ofproto->stats_mutex);
3776     ofproto->stats.tx_packets++;
3777     ofproto->stats.tx_bytes += ofpbuf_size(packet);
3778     ovs_mutex_unlock(&ofproto->stats_mutex);
3779     return error;
3780 }
3781 \f
3782 static bool
3783 set_frag_handling(struct ofproto *ofproto_,
3784                   enum ofp_config_flags frag_handling)
3785 {
3786     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3787     if (frag_handling != OFPC_FRAG_REASM) {
3788         ofproto->backer->need_revalidate = REV_RECONFIGURE;
3789         return true;
3790     } else {
3791         return false;
3792     }
3793 }
3794
3795 static enum ofperr
3796 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3797            const struct flow *flow,
3798            const struct ofpact *ofpacts, size_t ofpacts_len)
3799 {
3800     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3801
3802     ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3803                                  ofpacts_len, packet);
3804     return 0;
3805 }
3806 \f
3807 /* NetFlow. */
3808
3809 static int
3810 set_netflow(struct ofproto *ofproto_,
3811             const struct netflow_options *netflow_options)
3812 {
3813     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3814
3815     if (netflow_options) {
3816         if (!ofproto->netflow) {
3817             ofproto->netflow = netflow_create();
3818             ofproto->backer->need_revalidate = REV_RECONFIGURE;
3819         }
3820         return netflow_set_options(ofproto->netflow, netflow_options);
3821     } else if (ofproto->netflow) {
3822         ofproto->backer->need_revalidate = REV_RECONFIGURE;
3823         netflow_unref(ofproto->netflow);
3824         ofproto->netflow = NULL;
3825     }
3826
3827     return 0;
3828 }
3829
3830 static void
3831 get_netflow_ids(const struct ofproto *ofproto_,
3832                 uint8_t *engine_type, uint8_t *engine_id)
3833 {
3834     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3835
3836     dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3837 }
3838 \f
3839 static struct ofproto_dpif *
3840 ofproto_dpif_lookup(const char *name)
3841 {
3842     struct ofproto_dpif *ofproto;
3843
3844     HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3845                              hash_string(name, 0), &all_ofproto_dpifs) {
3846         if (!strcmp(ofproto->up.name, name)) {
3847             return ofproto;
3848         }
3849     }
3850     return NULL;
3851 }
3852
3853 static void
3854 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3855                           const char *argv[], void *aux OVS_UNUSED)
3856 {
3857     struct ofproto_dpif *ofproto;
3858
3859     if (argc > 1) {
3860         ofproto = ofproto_dpif_lookup(argv[1]);
3861         if (!ofproto) {
3862             unixctl_command_reply_error(conn, "no such bridge");
3863             return;
3864         }
3865         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3866         mac_learning_flush(ofproto->ml);
3867         ovs_rwlock_unlock(&ofproto->ml->rwlock);
3868     } else {
3869         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3870             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3871             mac_learning_flush(ofproto->ml);
3872             ovs_rwlock_unlock(&ofproto->ml->rwlock);
3873         }
3874     }
3875
3876     unixctl_command_reply(conn, "table successfully flushed");
3877 }
3878
3879 static void
3880 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
3881                                      const char *argv[], void *aux OVS_UNUSED)
3882 {
3883     struct ofproto_dpif *ofproto;
3884
3885     if (argc > 1) {
3886         ofproto = ofproto_dpif_lookup(argv[1]);
3887         if (!ofproto) {
3888             unixctl_command_reply_error(conn, "no such bridge");
3889             return;
3890         }
3891
3892         if (!mcast_snooping_enabled(ofproto->ms)) {
3893             unixctl_command_reply_error(conn, "multicast snooping is disabled");
3894             return;
3895         }
3896         mcast_snooping_mdb_flush(ofproto->ms);
3897     } else {
3898         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3899             if (!mcast_snooping_enabled(ofproto->ms)) {
3900                 continue;
3901             }
3902             mcast_snooping_mdb_flush(ofproto->ms);
3903         }
3904     }
3905
3906     unixctl_command_reply(conn, "table successfully flushed");
3907 }
3908
3909 static struct ofport_dpif *
3910 ofbundle_get_a_port(const struct ofbundle *bundle)
3911 {
3912     return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3913                         bundle_node);
3914 }
3915
3916 static void
3917 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3918                          const char *argv[], void *aux OVS_UNUSED)
3919 {
3920     struct ds ds = DS_EMPTY_INITIALIZER;
3921     const struct ofproto_dpif *ofproto;
3922     const struct mac_entry *e;
3923
3924     ofproto = ofproto_dpif_lookup(argv[1]);
3925     if (!ofproto) {
3926         unixctl_command_reply_error(conn, "no such bridge");
3927         return;
3928     }
3929
3930     ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
3931     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3932     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3933         struct ofbundle *bundle = e->port.p;
3934         char name[OFP_MAX_PORT_NAME_LEN];
3935
3936         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3937                                name, sizeof name);
3938         ds_put_format(&ds, "%5s  %4d  "ETH_ADDR_FMT"  %3d\n",
3939                       name, e->vlan, ETH_ADDR_ARGS(e->mac),
3940                       mac_entry_age(ofproto->ml, e));
3941     }
3942     ovs_rwlock_unlock(&ofproto->ml->rwlock);
3943     unixctl_command_reply(conn, ds_cstr(&ds));
3944     ds_destroy(&ds);
3945 }
3946
3947 static void
3948 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
3949                                     int argc OVS_UNUSED,
3950                                     const char *argv[],
3951                                     void *aux OVS_UNUSED)
3952 {
3953     struct ds ds = DS_EMPTY_INITIALIZER;
3954     const struct ofproto_dpif *ofproto;
3955     const struct ofbundle *bundle;
3956     const struct mcast_group *grp;
3957     struct mcast_group_bundle *b;
3958     struct mcast_mrouter_bundle *mrouter;
3959
3960     ofproto = ofproto_dpif_lookup(argv[1]);
3961     if (!ofproto) {
3962         unixctl_command_reply_error(conn, "no such bridge");
3963         return;
3964     }
3965
3966     if (!mcast_snooping_enabled(ofproto->ms)) {
3967         unixctl_command_reply_error(conn, "multicast snooping is disabled");
3968         return;
3969     }
3970
3971     ds_put_cstr(&ds, " port  VLAN  GROUP                Age\n");
3972     ovs_rwlock_rdlock(&ofproto->ms->rwlock);
3973     LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
3974         LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
3975             char name[OFP_MAX_PORT_NAME_LEN];
3976
3977             bundle = b->port;
3978             ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3979                                    name, sizeof name);
3980             ds_put_format(&ds, "%5s  %4d  "IP_FMT"         %3d\n",
3981                           name, grp->vlan, IP_ARGS(grp->ip4),
3982                           mcast_bundle_age(ofproto->ms, b));
3983         }
3984     }
3985
3986     /* ports connected to multicast routers */
3987     LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
3988         char name[OFP_MAX_PORT_NAME_LEN];
3989
3990         bundle = mrouter->port;
3991         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3992                                name, sizeof name);
3993             ds_put_format(&ds, "%5s  %4d  querier             %3d\n",
3994                       name, mrouter->vlan,
3995                       mcast_mrouter_age(ofproto->ms, mrouter));
3996     }
3997     ovs_rwlock_unlock(&ofproto->ms->rwlock);
3998     unixctl_command_reply(conn, ds_cstr(&ds));
3999     ds_destroy(&ds);
4000 }
4001
4002 struct trace_ctx {
4003     struct xlate_out xout;
4004     struct xlate_in xin;
4005     const struct flow *key;
4006     struct flow flow;
4007     struct flow_wildcards wc;
4008     struct ds *result;
4009 };
4010
4011 static void
4012 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4013 {
4014     const struct rule_actions *actions;
4015     ovs_be64 cookie;
4016
4017     ds_put_char_multiple(result, '\t', level);
4018     if (!rule) {
4019         ds_put_cstr(result, "No match\n");
4020         return;
4021     }
4022
4023     ovs_mutex_lock(&rule->up.mutex);
4024     cookie = rule->up.flow_cookie;
4025     ovs_mutex_unlock(&rule->up.mutex);
4026
4027     ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4028                   rule ? rule->up.table_id : 0, ntohll(cookie));
4029     cls_rule_format(&rule->up.cr, result);
4030     ds_put_char(result, '\n');
4031
4032     actions = rule_dpif_get_actions(rule);
4033
4034     ds_put_char_multiple(result, '\t', level);
4035     ds_put_cstr(result, "OpenFlow actions=");
4036     ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4037     ds_put_char(result, '\n');
4038 }
4039
4040 static void
4041 trace_format_flow(struct ds *result, int level, const char *title,
4042                   struct trace_ctx *trace)
4043 {
4044     ds_put_char_multiple(result, '\t', level);
4045     ds_put_format(result, "%s: ", title);
4046     /* Do not report unchanged flows for resubmits. */
4047     if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4048         || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4049         ds_put_cstr(result, "unchanged");
4050     } else {
4051         flow_format(result, &trace->xin.flow);
4052         trace->flow = trace->xin.flow;
4053     }
4054     ds_put_char(result, '\n');
4055 }
4056
4057 static void
4058 trace_format_regs(struct ds *result, int level, const char *title,
4059                   struct trace_ctx *trace)
4060 {
4061     size_t i;
4062
4063     ds_put_char_multiple(result, '\t', level);
4064     ds_put_format(result, "%s:", title);
4065     for (i = 0; i < FLOW_N_REGS; i++) {
4066         ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4067     }
4068     ds_put_char(result, '\n');
4069 }
4070
4071 static void
4072 trace_format_odp(struct ds *result, int level, const char *title,
4073                  struct trace_ctx *trace)
4074 {
4075     struct ofpbuf *odp_actions = &trace->xout.odp_actions;
4076
4077     ds_put_char_multiple(result, '\t', level);
4078     ds_put_format(result, "%s: ", title);
4079     format_odp_actions(result, ofpbuf_data(odp_actions),
4080                                ofpbuf_size(odp_actions));
4081     ds_put_char(result, '\n');
4082 }
4083
4084 static void
4085 trace_format_megaflow(struct ds *result, int level, const char *title,
4086                       struct trace_ctx *trace)
4087 {
4088     struct match match;
4089
4090     ds_put_char_multiple(result, '\t', level);
4091     ds_put_format(result, "%s: ", title);
4092     flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
4093     match_init(&match, trace->key, &trace->wc);
4094     match_format(&match, result, OFP_DEFAULT_PRIORITY);
4095     ds_put_char(result, '\n');
4096 }
4097
4098 static void
4099 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4100 {
4101     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4102     struct ds *result = trace->result;
4103
4104     ds_put_char(result, '\n');
4105     trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
4106     trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
4107     trace_format_odp(result,  recurse + 1, "Resubmitted  odp", trace);
4108     trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
4109     trace_format_rule(result, recurse + 1, rule);
4110 }
4111
4112 static void
4113 trace_report(struct xlate_in *xin, const char *s, int recurse)
4114 {
4115     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4116     struct ds *result = trace->result;
4117
4118     ds_put_char_multiple(result, '\t', recurse);
4119     ds_put_cstr(result, s);
4120     ds_put_char(result, '\n');
4121 }
4122
4123 /* Parses the 'argc' elements of 'argv', ignoring argv[0].  The following
4124  * forms are supported:
4125  *
4126  *     - [dpname] odp_flow [-generate | packet]
4127  *     - bridge br_flow [-generate | packet]
4128  *
4129  * On success, initializes '*ofprotop' and 'flow' and returns NULL.  On failure
4130  * returns a nonnull malloced error message. */
4131 static char * WARN_UNUSED_RESULT
4132 parse_flow_and_packet(int argc, const char *argv[],
4133                       struct ofproto_dpif **ofprotop, struct flow *flow,
4134                       struct ofpbuf **packetp)
4135 {
4136     const struct dpif_backer *backer = NULL;
4137     const char *error = NULL;
4138     char *m_err = NULL;
4139     struct simap port_names = SIMAP_INITIALIZER(&port_names);
4140     struct ofpbuf *packet;
4141     struct ofpbuf odp_key;
4142     struct ofpbuf odp_mask;
4143
4144     ofpbuf_init(&odp_key, 0);
4145     ofpbuf_init(&odp_mask, 0);
4146
4147     /* Handle "-generate" or a hex string as the last argument. */
4148     if (!strcmp(argv[argc - 1], "-generate")) {
4149         packet = ofpbuf_new(0);
4150         argc--;
4151     } else {
4152         error = eth_from_hex(argv[argc - 1], &packet);
4153         if (!error) {
4154             argc--;
4155         } else if (argc == 4) {
4156             /* The 3-argument form must end in "-generate' or a hex string. */
4157             goto exit;
4158         }
4159         error = NULL;
4160     }
4161
4162     /* odp_flow can have its in_port specified as a name instead of port no.
4163      * We do not yet know whether a given flow is a odp_flow or a br_flow.
4164      * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4165      * we need to create a simap of name to port no. */
4166     if (argc == 3) {
4167         const char *dp_type;
4168         if (!strncmp(argv[1], "ovs-", 4)) {
4169             dp_type = argv[1] + 4;
4170         } else {
4171             dp_type = argv[1];
4172         }
4173         backer = shash_find_data(&all_dpif_backers, dp_type);
4174     } else if (argc == 2) {
4175         struct shash_node *node;
4176         if (shash_count(&all_dpif_backers) == 1) {
4177             node = shash_first(&all_dpif_backers);
4178             backer = node->data;
4179         }
4180     } else {
4181         error = "Syntax error";
4182         goto exit;
4183     }
4184     if (backer && backer->dpif) {
4185         struct dpif_port dpif_port;
4186         struct dpif_port_dump port_dump;
4187         DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4188             simap_put(&port_names, dpif_port.name,
4189                       odp_to_u32(dpif_port.port_no));
4190         }
4191     }
4192
4193     /* Parse the flow and determine whether a datapath or
4194      * bridge is specified. If function odp_flow_key_from_string()
4195      * returns 0, the flow is a odp_flow. If function
4196      * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4197     if (!odp_flow_from_string(argv[argc - 1], &port_names,
4198                               &odp_key, &odp_mask)) {
4199         if (!backer) {
4200             error = "Cannot find the datapath";
4201             goto exit;
4202         }
4203
4204         if (xlate_receive(backer, NULL, ofpbuf_data(&odp_key),
4205                           ofpbuf_size(&odp_key), flow,
4206                           ofprotop, NULL, NULL, NULL, NULL)) {
4207             error = "Invalid datapath flow";
4208             goto exit;
4209         }
4210     } else {
4211         char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4212
4213         if (err) {
4214             m_err = xasprintf("Bad flow syntax: %s", err);
4215             free(err);
4216             goto exit;
4217         } else {
4218             if (argc != 3) {
4219                 error = "Must specify bridge name";
4220                 goto exit;
4221             }
4222
4223             *ofprotop = ofproto_dpif_lookup(argv[1]);
4224             if (!*ofprotop) {
4225                 error = "Unknown bridge name";
4226                 goto exit;
4227             }
4228         }
4229     }
4230
4231     /* Generate a packet, if requested. */
4232     if (packet) {
4233         if (!ofpbuf_size(packet)) {
4234             flow_compose(packet, flow);
4235         } else {
4236             struct pkt_metadata md = pkt_metadata_from_flow(flow);
4237
4238             /* Use the metadata from the flow and the packet argument
4239              * to reconstruct the flow. */
4240             flow_extract(packet, &md, flow);
4241         }
4242     }
4243
4244 exit:
4245     if (error && !m_err) {
4246         m_err = xstrdup(error);
4247     }
4248     if (m_err) {
4249         ofpbuf_delete(packet);
4250         packet = NULL;
4251     }
4252     *packetp = packet;
4253     ofpbuf_uninit(&odp_key);
4254     ofpbuf_uninit(&odp_mask);
4255     simap_destroy(&port_names);
4256     return m_err;
4257 }
4258
4259 static void
4260 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4261                       void *aux OVS_UNUSED)
4262 {
4263     struct ofproto_dpif *ofproto;
4264     struct ofpbuf *packet;
4265     char *error;
4266     struct flow flow;
4267
4268     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4269     if (!error) {
4270         struct ds result;
4271
4272         ds_init(&result);
4273         ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4274         unixctl_command_reply(conn, ds_cstr(&result));
4275         ds_destroy(&result);
4276         ofpbuf_delete(packet);
4277     } else {
4278         unixctl_command_reply_error(conn, error);
4279         free(error);
4280     }
4281 }
4282
4283 static void
4284 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4285                               const char *argv[], void *aux OVS_UNUSED)
4286 {
4287     enum ofputil_protocol usable_protocols;
4288     struct ofproto_dpif *ofproto;
4289     bool enforce_consistency;
4290     struct ofpbuf ofpacts;
4291     struct ofpbuf *packet;
4292     struct ds result;
4293     struct flow flow;
4294     uint16_t in_port;
4295
4296     /* Three kinds of error return values! */
4297     enum ofperr retval;
4298     char *error;
4299
4300     packet = NULL;
4301     ds_init(&result);
4302     ofpbuf_init(&ofpacts, 0);
4303
4304     /* Parse actions. */
4305     error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
4306     if (error) {
4307         unixctl_command_reply_error(conn, error);
4308         free(error);
4309         goto exit;
4310     }
4311
4312     /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4313      * consistency between the flow and the actions.  With -consistent, we
4314      * enforce consistency even for a flow supported in OpenFlow 1.0. */
4315     if (!strcmp(argv[1], "-consistent")) {
4316         enforce_consistency = true;
4317         argv++;
4318         argc--;
4319     } else {
4320         enforce_consistency = false;
4321     }
4322
4323     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4324     if (error) {
4325         unixctl_command_reply_error(conn, error);
4326         free(error);
4327         goto exit;
4328     }
4329
4330     /* Do the same checks as handle_packet_out() in ofproto.c.
4331      *
4332      * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
4333      * strictly correct because these actions aren't in any table, but it's OK
4334      * because it 'table_id' is used only to check goto_table instructions, but
4335      * packet-outs take a list of actions and therefore it can't include
4336      * instructions.
4337      *
4338      * We skip the "meter" check here because meter is an instruction, not an
4339      * action, and thus cannot appear in ofpacts. */
4340     in_port = ofp_to_u16(flow.in_port.ofp_port);
4341     if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4342         unixctl_command_reply_error(conn, "invalid in_port");
4343         goto exit;
4344     }
4345     if (enforce_consistency) {
4346         retval = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
4347                                            &flow, u16_to_ofp(ofproto->up.max_ports),
4348                                            0, 0, usable_protocols);
4349     } else {
4350         retval = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &flow,
4351                                u16_to_ofp(ofproto->up.max_ports), 0, 0,
4352                                &usable_protocols);
4353     }
4354
4355     if (retval) {
4356         ds_clear(&result);
4357         ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4358         unixctl_command_reply_error(conn, ds_cstr(&result));
4359         goto exit;
4360     }
4361
4362     ofproto_trace(ofproto, &flow, packet,
4363                   ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &result);
4364     unixctl_command_reply(conn, ds_cstr(&result));
4365
4366 exit:
4367     ds_destroy(&result);
4368     ofpbuf_delete(packet);
4369     ofpbuf_uninit(&ofpacts);
4370 }
4371
4372 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4373  * description of the results to 'ds'.
4374  *
4375  * The trace follows a packet with the specified 'flow' through the flow
4376  * table.  'packet' may be nonnull to trace an actual packet, with consequent
4377  * side effects (if it is nonnull then its flow must be 'flow').
4378  *
4379  * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4380  * trace, otherwise the actions are determined by a flow table lookup. */
4381 static void
4382 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4383               const struct ofpbuf *packet,
4384               const struct ofpact ofpacts[], size_t ofpacts_len,
4385               struct ds *ds)
4386 {
4387     struct rule_dpif *rule;
4388     struct trace_ctx trace;
4389
4390     ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4391     ds_put_cstr(ds, "Flow: ");
4392     flow_format(ds, flow);
4393     ds_put_char(ds, '\n');
4394
4395     flow_wildcards_init_catchall(&trace.wc);
4396     if (ofpacts) {
4397         rule = NULL;
4398     } else {
4399         rule_dpif_lookup(ofproto, flow, &trace.wc, &rule, false, NULL);
4400
4401         trace_format_rule(ds, 0, rule);
4402         if (rule == ofproto->miss_rule) {
4403             ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
4404         } else if (rule == ofproto->no_packet_in_rule) {
4405             ds_put_cstr(ds, "\nNo match, packets dropped because "
4406                         "OFPPC_NO_PACKET_IN is set on in_port.\n");
4407         } else if (rule == ofproto->drop_frags_rule) {
4408             ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
4409                         "and the fragment handling mode is \"drop\".\n");
4410         }
4411     }
4412
4413     if (rule || ofpacts) {
4414         trace.result = ds;
4415         trace.key = flow; /* Original flow key, used for megaflow. */
4416         trace.flow = *flow; /* May be modified by actions. */
4417         xlate_in_init(&trace.xin, ofproto, flow, rule, ntohs(flow->tcp_flags),
4418                       packet);
4419         if (ofpacts) {
4420             trace.xin.ofpacts = ofpacts;
4421             trace.xin.ofpacts_len = ofpacts_len;
4422         }
4423         trace.xin.resubmit_hook = trace_resubmit;
4424         trace.xin.report_hook = trace_report;
4425
4426         xlate_actions(&trace.xin, &trace.xout);
4427
4428         ds_put_char(ds, '\n');
4429         trace_format_flow(ds, 0, "Final flow", &trace);
4430         trace_format_megaflow(ds, 0, "Megaflow", &trace);
4431
4432         ds_put_cstr(ds, "Datapath actions: ");
4433         format_odp_actions(ds, ofpbuf_data(&trace.xout.odp_actions),
4434                            ofpbuf_size(&trace.xout.odp_actions));
4435
4436         if (trace.xout.slow) {
4437             enum slow_path_reason slow;
4438
4439             ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4440                         "slow path because it:");
4441
4442             slow = trace.xout.slow;
4443             while (slow) {
4444                 enum slow_path_reason bit = rightmost_1bit(slow);
4445
4446                 ds_put_format(ds, "\n\t- %s.",
4447                               slow_path_reason_to_explanation(bit));
4448
4449                 slow &= ~bit;
4450             }
4451         }
4452
4453         xlate_out_uninit(&trace.xout);
4454     }
4455 }
4456
4457 /* Store the current ofprotos in 'ofproto_shash'.  Returns a sorted list
4458  * of the 'ofproto_shash' nodes.  It is the responsibility of the caller
4459  * to destroy 'ofproto_shash' and free the returned value. */
4460 static const struct shash_node **
4461 get_ofprotos(struct shash *ofproto_shash)
4462 {
4463     const struct ofproto_dpif *ofproto;
4464
4465     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4466         char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4467         shash_add_nocopy(ofproto_shash, name, ofproto);
4468     }
4469
4470     return shash_sort(ofproto_shash);
4471 }
4472
4473 static void
4474 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4475                               const char *argv[] OVS_UNUSED,
4476                               void *aux OVS_UNUSED)
4477 {
4478     struct ds ds = DS_EMPTY_INITIALIZER;
4479     struct shash ofproto_shash;
4480     const struct shash_node **sorted_ofprotos;
4481     int i;
4482
4483     shash_init(&ofproto_shash);
4484     sorted_ofprotos = get_ofprotos(&ofproto_shash);
4485     for (i = 0; i < shash_count(&ofproto_shash); i++) {
4486         const struct shash_node *node = sorted_ofprotos[i];
4487         ds_put_format(&ds, "%s\n", node->name);
4488     }
4489
4490     shash_destroy(&ofproto_shash);
4491     free(sorted_ofprotos);
4492
4493     unixctl_command_reply(conn, ds_cstr(&ds));
4494     ds_destroy(&ds);
4495 }
4496
4497 static void
4498 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4499 {
4500     const struct shash_node **ofprotos;
4501     struct dpif_dp_stats dp_stats;
4502     struct shash ofproto_shash;
4503     size_t i;
4504
4505     dpif_get_dp_stats(backer->dpif, &dp_stats);
4506
4507     ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4508                   dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4509
4510     shash_init(&ofproto_shash);
4511     ofprotos = get_ofprotos(&ofproto_shash);
4512     for (i = 0; i < shash_count(&ofproto_shash); i++) {
4513         struct ofproto_dpif *ofproto = ofprotos[i]->data;
4514         const struct shash_node **ports;
4515         size_t j;
4516
4517         if (ofproto->backer != backer) {
4518             continue;
4519         }
4520
4521         ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4522
4523         ports = shash_sort(&ofproto->up.port_by_name);
4524         for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4525             const struct shash_node *node = ports[j];
4526             struct ofport *ofport = node->data;
4527             struct smap config;
4528             odp_port_t odp_port;
4529
4530             ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4531                           ofport->ofp_port);
4532
4533             odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4534             if (odp_port != ODPP_NONE) {
4535                 ds_put_format(ds, "%"PRIu32":", odp_port);
4536             } else {
4537                 ds_put_cstr(ds, "none:");
4538             }
4539
4540             ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4541
4542             smap_init(&config);
4543             if (!netdev_get_config(ofport->netdev, &config)) {
4544                 const struct smap_node **nodes;
4545                 size_t i;
4546
4547                 nodes = smap_sort(&config);
4548                 for (i = 0; i < smap_count(&config); i++) {
4549                     const struct smap_node *node = nodes[i];
4550                     ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4551                                   node->key, node->value);
4552                 }
4553                 free(nodes);
4554             }
4555             smap_destroy(&config);
4556
4557             ds_put_char(ds, ')');
4558             ds_put_char(ds, '\n');
4559         }
4560         free(ports);
4561     }
4562     shash_destroy(&ofproto_shash);
4563     free(ofprotos);
4564 }
4565
4566 static void
4567 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4568                           const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4569 {
4570     struct ds ds = DS_EMPTY_INITIALIZER;
4571     const struct shash_node **backers;
4572     int i;
4573
4574     backers = shash_sort(&all_dpif_backers);
4575     for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4576         dpif_show_backer(backers[i]->data, &ds);
4577     }
4578     free(backers);
4579
4580     unixctl_command_reply(conn, ds_cstr(&ds));
4581     ds_destroy(&ds);
4582 }
4583
4584 static bool
4585 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4586                            const struct nlattr *key, size_t key_len)
4587 {
4588     struct ofproto_dpif *ofp;
4589     struct flow flow;
4590
4591     xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4592                   NULL, NULL, NULL, NULL);
4593     return ofp == ofproto;
4594 }
4595
4596 static void
4597 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4598                                 int argc OVS_UNUSED, const char *argv[],
4599                                 void *aux OVS_UNUSED)
4600 {
4601     const struct ofproto_dpif *ofproto;
4602
4603     struct ds ds = DS_EMPTY_INITIALIZER;
4604     bool verbosity = false;
4605
4606     struct dpif_port dpif_port;
4607     struct dpif_port_dump port_dump;
4608     struct hmap portno_names;
4609
4610     struct dpif_flow_dump *flow_dump;
4611     struct dpif_flow_dump_thread *flow_dump_thread;
4612     struct dpif_flow f;
4613     int error;
4614
4615     ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4616     if (!ofproto) {
4617         unixctl_command_reply_error(conn, "no such bridge");
4618         return;
4619     }
4620
4621     if (argc > 2 && !strcmp(argv[1], "-m")) {
4622         verbosity = true;
4623     }
4624
4625     hmap_init(&portno_names);
4626     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4627         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4628     }
4629
4630     ds_init(&ds);
4631     flow_dump = dpif_flow_dump_create(ofproto->backer->dpif);
4632     flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
4633     while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
4634         if (!ofproto_dpif_contains_flow(ofproto, f.key, f.key_len)) {
4635             continue;
4636         }
4637
4638         odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
4639                         &portno_names, &ds, verbosity);
4640         ds_put_cstr(&ds, ", ");
4641         dpif_flow_stats_format(&f.stats, &ds);
4642         ds_put_cstr(&ds, ", actions:");
4643         format_odp_actions(&ds, f.actions, f.actions_len);
4644         ds_put_char(&ds, '\n');
4645     }
4646     dpif_flow_dump_thread_destroy(flow_dump_thread);
4647     error = dpif_flow_dump_destroy(flow_dump);
4648
4649     if (error) {
4650         ds_clear(&ds);
4651         ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4652         unixctl_command_reply_error(conn, ds_cstr(&ds));
4653     } else {
4654         unixctl_command_reply(conn, ds_cstr(&ds));
4655     }
4656     odp_portno_names_destroy(&portno_names);
4657     hmap_destroy(&portno_names);
4658     ds_destroy(&ds);
4659 }
4660
4661 static void
4662 ofproto_dpif_unixctl_init(void)
4663 {
4664     static bool registered;
4665     if (registered) {
4666         return;
4667     }
4668     registered = true;
4669
4670     unixctl_command_register(
4671         "ofproto/trace",
4672         "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4673         1, 3, ofproto_unixctl_trace, NULL);
4674     unixctl_command_register(
4675         "ofproto/trace-packet-out",
4676         "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4677         2, 6, ofproto_unixctl_trace_actions, NULL);
4678     unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4679                              ofproto_unixctl_fdb_flush, NULL);
4680     unixctl_command_register("fdb/show", "bridge", 1, 1,
4681                              ofproto_unixctl_fdb_show, NULL);
4682     unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
4683                              ofproto_unixctl_mcast_snooping_flush, NULL);
4684     unixctl_command_register("mdb/show", "bridge", 1, 1,
4685                              ofproto_unixctl_mcast_snooping_show, NULL);
4686     unixctl_command_register("dpif/dump-dps", "", 0, 0,
4687                              ofproto_unixctl_dpif_dump_dps, NULL);
4688     unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4689                              NULL);
4690     unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4691                              ofproto_unixctl_dpif_dump_flows, NULL);
4692 }
4693
4694 /* Returns true if 'table' is the table used for internal rules,
4695  * false otherwise. */
4696 bool
4697 table_is_internal(uint8_t table_id)
4698 {
4699     return table_id == TBL_INTERNAL;
4700 }
4701 \f
4702 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4703  *
4704  * This is deprecated.  It is only for compatibility with broken device drivers
4705  * in old versions of Linux that do not properly support VLANs when VLAN
4706  * devices are not used.  When broken device drivers are no longer in
4707  * widespread use, we will delete these interfaces. */
4708
4709 static int
4710 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4711 {
4712     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4713     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4714
4715     if (realdev_ofp_port == ofport->realdev_ofp_port
4716         && vid == ofport->vlandev_vid) {
4717         return 0;
4718     }
4719
4720     ofproto->backer->need_revalidate = REV_RECONFIGURE;
4721
4722     if (ofport->realdev_ofp_port) {
4723         vsp_remove(ofport);
4724     }
4725     if (realdev_ofp_port && ofport->bundle) {
4726         /* vlandevs are enslaved to their realdevs, so they are not allowed to
4727          * themselves be part of a bundle. */
4728         bundle_set(ofport_->ofproto, ofport->bundle, NULL);
4729     }
4730
4731     ofport->realdev_ofp_port = realdev_ofp_port;
4732     ofport->vlandev_vid = vid;
4733
4734     if (realdev_ofp_port) {
4735         vsp_add(ofport, realdev_ofp_port, vid);
4736     }
4737
4738     return 0;
4739 }
4740
4741 static uint32_t
4742 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4743 {
4744     return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4745 }
4746
4747 bool
4748 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4749     OVS_EXCLUDED(ofproto->vsp_mutex)
4750 {
4751     /* hmap_is_empty is thread safe. */
4752     return !hmap_is_empty(&ofproto->realdev_vid_map);
4753 }
4754
4755
4756 static ofp_port_t
4757 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4758                          ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4759     OVS_REQUIRES(ofproto->vsp_mutex)
4760 {
4761     if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4762         int vid = vlan_tci_to_vid(vlan_tci);
4763         const struct vlan_splinter *vsp;
4764
4765         HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4766                                  hash_realdev_vid(realdev_ofp_port, vid),
4767                                  &ofproto->realdev_vid_map) {
4768             if (vsp->realdev_ofp_port == realdev_ofp_port
4769                 && vsp->vid == vid) {
4770                 return vsp->vlandev_ofp_port;
4771             }
4772         }
4773     }
4774     return realdev_ofp_port;
4775 }
4776
4777 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4778  * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4779  * 'struct ofport_dpif'.  For example, given 'realdev_ofp_port' of eth0 and
4780  * 'vlan_tci' 9, it would return the port number of eth0.9.
4781  *
4782  * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4783  * function just returns its 'realdev_ofp_port' argument. */
4784 ofp_port_t
4785 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4786                        ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4787     OVS_EXCLUDED(ofproto->vsp_mutex)
4788 {
4789     ofp_port_t ret;
4790
4791     /* hmap_is_empty is thread safe, see if we can return immediately. */
4792     if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4793         return realdev_ofp_port;
4794     }
4795     ovs_mutex_lock(&ofproto->vsp_mutex);
4796     ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4797     ovs_mutex_unlock(&ofproto->vsp_mutex);
4798     return ret;
4799 }
4800
4801 static struct vlan_splinter *
4802 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4803 {
4804     struct vlan_splinter *vsp;
4805
4806     HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4807                              hash_ofp_port(vlandev_ofp_port),
4808                              &ofproto->vlandev_map) {
4809         if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4810             return vsp;
4811         }
4812     }
4813
4814     return NULL;
4815 }
4816
4817 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4818  * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4819  * VLAN VID of the Linux VLAN device in '*vid'.  For example, given
4820  * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4821  * eth0 and store 9 in '*vid'.
4822  *
4823  * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4824  * VLAN device.  Unless VLAN splinters are enabled, this is what this function
4825  * always does.*/
4826 static ofp_port_t
4827 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4828                        ofp_port_t vlandev_ofp_port, int *vid)
4829     OVS_REQUIRES(ofproto->vsp_mutex)
4830 {
4831     if (!hmap_is_empty(&ofproto->vlandev_map)) {
4832         const struct vlan_splinter *vsp;
4833
4834         vsp = vlandev_find(ofproto, vlandev_ofp_port);
4835         if (vsp) {
4836             if (vid) {
4837                 *vid = vsp->vid;
4838             }
4839             return vsp->realdev_ofp_port;
4840         }
4841     }
4842     return 0;
4843 }
4844
4845 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4846  * whether 'flow->in_port' represents a Linux VLAN device.  If so, changes
4847  * 'flow->in_port' to the "real" device backing the VLAN device, sets
4848  * 'flow->vlan_tci' to the VLAN VID, and returns true.  Otherwise (which is
4849  * always the case unless VLAN splinters are enabled), returns false without
4850  * making any changes. */
4851 bool
4852 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4853     OVS_EXCLUDED(ofproto->vsp_mutex)
4854 {
4855     ofp_port_t realdev;
4856     int vid;
4857
4858     /* hmap_is_empty is thread safe. */
4859     if (hmap_is_empty(&ofproto->vlandev_map)) {
4860         return false;
4861     }
4862
4863     ovs_mutex_lock(&ofproto->vsp_mutex);
4864     realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4865     ovs_mutex_unlock(&ofproto->vsp_mutex);
4866     if (!realdev) {
4867         return false;
4868     }
4869
4870     /* Cause the flow to be processed as if it came in on the real device with
4871      * the VLAN device's VLAN ID. */
4872     flow->in_port.ofp_port = realdev;
4873     flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4874     return true;
4875 }
4876
4877 static void
4878 vsp_remove(struct ofport_dpif *port)
4879 {
4880     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4881     struct vlan_splinter *vsp;
4882
4883     ovs_mutex_lock(&ofproto->vsp_mutex);
4884     vsp = vlandev_find(ofproto, port->up.ofp_port);
4885     if (vsp) {
4886         hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4887         hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4888         free(vsp);
4889
4890         port->realdev_ofp_port = 0;
4891     } else {
4892         VLOG_ERR("missing vlan device record");
4893     }
4894     ovs_mutex_unlock(&ofproto->vsp_mutex);
4895 }
4896
4897 static void
4898 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4899 {
4900     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4901
4902     ovs_mutex_lock(&ofproto->vsp_mutex);
4903     if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4904         && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4905             == realdev_ofp_port)) {
4906         struct vlan_splinter *vsp;
4907
4908         vsp = xmalloc(sizeof *vsp);
4909         vsp->realdev_ofp_port = realdev_ofp_port;
4910         vsp->vlandev_ofp_port = port->up.ofp_port;
4911         vsp->vid = vid;
4912
4913         port->realdev_ofp_port = realdev_ofp_port;
4914
4915         hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4916                     hash_ofp_port(port->up.ofp_port));
4917         hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4918                     hash_realdev_vid(realdev_ofp_port, vid));
4919     } else {
4920         VLOG_ERR("duplicate vlan device record");
4921     }
4922     ovs_mutex_unlock(&ofproto->vsp_mutex);
4923 }
4924
4925 static odp_port_t
4926 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4927 {
4928     const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4929     return ofport ? ofport->odp_port : ODPP_NONE;
4930 }
4931
4932 struct ofport_dpif *
4933 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4934 {
4935     struct ofport_dpif *port;
4936
4937     ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4938     HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4939                              &backer->odp_to_ofport_map) {
4940         if (port->odp_port == odp_port) {
4941             ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4942             return port;
4943         }
4944     }
4945
4946     ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4947     return NULL;
4948 }
4949
4950 static ofp_port_t
4951 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4952 {
4953     struct ofport_dpif *port;
4954
4955     port = odp_port_to_ofport(ofproto->backer, odp_port);
4956     if (port && &ofproto->up == port->up.ofproto) {
4957         return port->up.ofp_port;
4958     } else {
4959         return OFPP_NONE;
4960     }
4961 }
4962
4963 uint32_t
4964 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
4965 {
4966     struct dpif_backer *backer = ofproto->backer;
4967
4968     return  recirc_id_alloc(backer->rid_pool);
4969 }
4970
4971 void
4972 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
4973 {
4974     struct dpif_backer *backer = ofproto->backer;
4975
4976     recirc_id_free(backer->rid_pool, recirc_id);
4977 }
4978
4979 int
4980 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
4981                                const struct match *match, int priority,
4982                                uint16_t idle_timeout,
4983                                const struct ofpbuf *ofpacts,
4984                                struct rule **rulep)
4985 {
4986     struct ofputil_flow_mod fm;
4987     struct rule_dpif *rule;
4988     int error;
4989
4990     fm.match = *match;
4991     fm.priority = priority;
4992     fm.new_cookie = htonll(0);
4993     fm.cookie = htonll(0);
4994     fm.cookie_mask = htonll(0);
4995     fm.modify_cookie = false;
4996     fm.table_id = TBL_INTERNAL;
4997     fm.command = OFPFC_ADD;
4998     fm.idle_timeout = idle_timeout;
4999     fm.hard_timeout = 0;
5000     fm.buffer_id = 0;
5001     fm.out_port = 0;
5002     fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5003     fm.ofpacts = ofpbuf_data(ofpacts);
5004     fm.ofpacts_len = ofpbuf_size(ofpacts);
5005
5006     error = ofproto_flow_mod(&ofproto->up, &fm);
5007     if (error) {
5008         VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5009                     ofperr_to_string(error));
5010         *rulep = NULL;
5011         return error;
5012     }
5013
5014     rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &fm.match.flow,
5015                                      &fm.match.wc, false);
5016     if (rule) {
5017         *rulep = &rule->up;
5018     } else {
5019         OVS_NOT_REACHED();
5020     }
5021     return 0;
5022 }
5023
5024 int
5025 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5026                                   struct match *match, int priority)
5027 {
5028     struct ofputil_flow_mod fm;
5029     int error;
5030
5031     fm.match = *match;
5032     fm.priority = priority;
5033     fm.new_cookie = htonll(0);
5034     fm.cookie = htonll(0);
5035     fm.cookie_mask = htonll(0);
5036     fm.modify_cookie = false;
5037     fm.table_id = TBL_INTERNAL;
5038     fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5039     fm.command = OFPFC_DELETE_STRICT;
5040
5041     error = ofproto_flow_mod(&ofproto->up, &fm);
5042     if (error) {
5043         VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5044                     ofperr_to_string(error));
5045         return error;
5046     }
5047
5048     return 0;
5049 }
5050
5051 const struct ofproto_class ofproto_dpif_class = {
5052     init,
5053     enumerate_types,
5054     enumerate_names,
5055     del,
5056     port_open_type,
5057     type_run,
5058     type_wait,
5059     alloc,
5060     construct,
5061     destruct,
5062     dealloc,
5063     run,
5064     wait,
5065     NULL,                       /* get_memory_usage. */
5066     type_get_memory_usage,
5067     flush,
5068     get_features,
5069     get_tables,
5070     port_alloc,
5071     port_construct,
5072     port_destruct,
5073     port_dealloc,
5074     port_modified,
5075     port_reconfigured,
5076     port_query_by_name,
5077     port_add,
5078     port_del,
5079     port_get_stats,
5080     port_dump_start,
5081     port_dump_next,
5082     port_dump_done,
5083     port_poll,
5084     port_poll_wait,
5085     port_is_lacp_current,
5086     NULL,                       /* rule_choose_table */
5087     rule_alloc,
5088     rule_construct,
5089     rule_insert,
5090     rule_delete,
5091     rule_destruct,
5092     rule_dealloc,
5093     rule_get_stats,
5094     rule_execute,
5095     NULL,                       /* rule_premodify_actions */
5096     rule_modify_actions,
5097     set_frag_handling,
5098     packet_out,
5099     set_netflow,
5100     get_netflow_ids,
5101     set_sflow,
5102     set_ipfix,
5103     set_cfm,
5104     cfm_status_changed,
5105     get_cfm_status,
5106     set_bfd,
5107     bfd_status_changed,
5108     get_bfd_status,
5109     set_stp,
5110     get_stp_status,
5111     set_stp_port,
5112     get_stp_port_status,
5113     get_stp_port_stats,
5114     set_queues,
5115     bundle_set,
5116     bundle_remove,
5117     mirror_set__,
5118     mirror_get_stats__,
5119     set_flood_vlans,
5120     is_mirror_output_bundle,
5121     forward_bpdu_changed,
5122     set_mac_table_config,
5123     set_mcast_snooping,
5124     set_mcast_snooping_port,
5125     set_realdev,
5126     NULL,                       /* meter_get_features */
5127     NULL,                       /* meter_set */
5128     NULL,                       /* meter_get */
5129     NULL,                       /* meter_del */
5130     group_alloc,                /* group_alloc */
5131     group_construct,            /* group_construct */
5132     group_destruct,             /* group_destruct */
5133     group_dealloc,              /* group_dealloc */
5134     group_modify,               /* group_modify */
5135     group_get_stats,            /* group_get_stats */
5136 };