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