43a21c239e48e52f39a580f6bdc150a891b6138c
[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 = rule_dpif_lookup_get_init_table_id(flow);
3807
3808     return rule_dpif_lookup_from_table(ofproto, flow, wc, take_ref, stats,
3809                                        table_id, flow->in_port.ofp_port, true,
3810                                        true);
3811 }
3812
3813 /* The returned rule (if any) is valid at least until the next RCU quiescent
3814  * period.  If the rule needs to stay around longer, a non-zero 'take_ref'
3815  * must be passed in to cause a reference to be taken on it.
3816  *
3817  * 'flow' is non-const to allow for temporary modifications during the lookup.
3818  * Any changes are restored before returning. */
3819 static struct rule_dpif *
3820 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3821                           struct flow *flow, struct flow_wildcards *wc,
3822                           bool take_ref)
3823 {
3824     struct classifier *cls = &ofproto->up.tables[table_id].cls;
3825     const struct cls_rule *cls_rule;
3826     struct rule_dpif *rule;
3827
3828     do {
3829         cls_rule = classifier_lookup(cls, flow, wc);
3830
3831         rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3832
3833         /* Try again if the rule was released before we get the reference. */
3834     } while (rule && take_ref && !rule_dpif_try_ref(rule));
3835
3836     return rule;
3837 }
3838
3839 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3840  * Returns the rule that was found, which may be one of the special rules
3841  * according to packet miss hadling.  If 'may_packet_in' is false, returning of
3842  * the miss_rule (which issues packet ins for the controller) is avoided.
3843  * Updates 'wc', if nonnull, to reflect the fields that were used during the
3844  * lookup.
3845  *
3846  * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3847  * if none is found then the table miss configuration for that table is
3848  * honored, which can result in additional lookups in other OpenFlow tables.
3849  * In this case the function updates '*table_id' to reflect the final OpenFlow
3850  * table that was searched.
3851  *
3852  * If 'honor_table_miss' is false, then only one table lookup occurs, in
3853  * '*table_id'.
3854  *
3855  * The rule is returned in '*rule', which is valid at least until the next
3856  * RCU quiescent period.  If the '*rule' needs to stay around longer,
3857  * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3858  * on it before this returns.
3859  *
3860  * 'in_port' allows the lookup to take place as if the in port had the value
3861  * 'in_port'.  This is needed for resubmit action support.
3862  *
3863  * 'flow' is non-const to allow for temporary modifications during the lookup.
3864  * Any changes are restored before returning. */
3865 struct rule_dpif *
3866 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto, struct flow *flow,
3867                             struct flow_wildcards *wc, bool take_ref,
3868                             const struct dpif_flow_stats *stats,
3869                             uint8_t *table_id, ofp_port_t in_port,
3870                             bool may_packet_in, bool honor_table_miss)
3871 {
3872     ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3873     ofp_port_t old_in_port = flow->in_port.ofp_port;
3874     enum ofputil_table_miss miss_config;
3875     struct rule_dpif *rule;
3876     uint8_t next_id;
3877
3878     /* We always unwildcard nw_frag (for IP), so they
3879      * need not be unwildcarded here. */
3880     if (flow->nw_frag & FLOW_NW_FRAG_ANY
3881         && ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3882         if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3883             /* We must pretend that transport ports are unavailable. */
3884             flow->tp_src = htons(0);
3885             flow->tp_dst = htons(0);
3886         } else {
3887             /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3888              * Use the drop_frags_rule (which cannot disappear). */
3889             rule = ofproto->drop_frags_rule;
3890             if (take_ref) {
3891                 rule_dpif_ref(rule);
3892             }
3893             if (stats) {
3894                 struct oftable *tbl = &ofproto->up.tables[*table_id];
3895                 unsigned long orig;
3896
3897                 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3898             }
3899             return rule;
3900         }
3901     }
3902
3903     /* Look up a flow with 'in_port' as the input port.  Then restore the
3904      * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3905      * have surprising behavior). */
3906     flow->in_port.ofp_port = in_port;
3907
3908     /* Our current implementation depends on n_tables == N_TABLES, and
3909      * TBL_INTERNAL being the last table. */
3910     BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3911
3912     miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3913
3914     for (next_id = *table_id;
3915          next_id < ofproto->up.n_tables;
3916          next_id++, next_id += (next_id == TBL_INTERNAL))
3917     {
3918         *table_id = next_id;
3919         rule = rule_dpif_lookup_in_table(ofproto, next_id, flow, wc, take_ref);
3920         if (stats) {
3921             struct oftable *tbl = &ofproto->up.tables[next_id];
3922             unsigned long orig;
3923
3924             atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3925                                stats->n_packets, &orig);
3926         }
3927         if (rule) {
3928             goto out;   /* Match. */
3929         }
3930         if (honor_table_miss) {
3931             miss_config = ofproto_table_get_miss_config(&ofproto->up,
3932                                                         *table_id);
3933             if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3934                 continue;
3935             }
3936         }
3937         break;
3938     }
3939     /* Miss. */
3940     rule = ofproto->no_packet_in_rule;
3941     if (may_packet_in) {
3942         if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3943             || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3944             struct ofport_dpif *port;
3945
3946             port = get_ofp_port(ofproto, old_in_port);
3947             if (!port) {
3948                 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3949                              old_in_port);
3950             } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3951                 rule = ofproto->miss_rule;
3952             }
3953         } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3954                    connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3955             rule = ofproto->miss_rule;
3956         }
3957     }
3958     if (take_ref) {
3959         rule_dpif_ref(rule);
3960     }
3961 out:
3962     /* Restore port numbers, as they may have been modified above. */
3963     flow->tp_src = old_tp_src;
3964     flow->tp_dst = old_tp_dst;
3965     /* Restore the old in port. */
3966     flow->in_port.ofp_port = old_in_port;
3967
3968     return rule;
3969 }
3970
3971 static void
3972 complete_operation(struct rule_dpif *rule)
3973     OVS_REQUIRES(ofproto_mutex)
3974 {
3975     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3976
3977     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3978 }
3979
3980 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3981 {
3982     return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3983 }
3984
3985 static struct rule *
3986 rule_alloc(void)
3987 {
3988     struct rule_dpif *rule = xmalloc(sizeof *rule);
3989     return &rule->up;
3990 }
3991
3992 static void
3993 rule_dealloc(struct rule *rule_)
3994 {
3995     struct rule_dpif *rule = rule_dpif_cast(rule_);
3996     free(rule);
3997 }
3998
3999 static enum ofperr
4000 rule_construct(struct rule *rule_)
4001     OVS_NO_THREAD_SAFETY_ANALYSIS
4002 {
4003     struct rule_dpif *rule = rule_dpif_cast(rule_);
4004     ovs_mutex_init_adaptive(&rule->stats_mutex);
4005     rule->stats.n_packets = 0;
4006     rule->stats.n_bytes = 0;
4007     rule->stats.used = rule->up.modified;
4008     rule->recirc_id = 0;
4009
4010     return 0;
4011 }
4012
4013 static enum ofperr
4014 rule_insert(struct rule *rule_)
4015     OVS_REQUIRES(ofproto_mutex)
4016 {
4017     struct rule_dpif *rule = rule_dpif_cast(rule_);
4018     complete_operation(rule);
4019     return 0;
4020 }
4021
4022 static void
4023 rule_delete(struct rule *rule_)
4024     OVS_REQUIRES(ofproto_mutex)
4025 {
4026     struct rule_dpif *rule = rule_dpif_cast(rule_);
4027     complete_operation(rule);
4028 }
4029
4030 static void
4031 rule_destruct(struct rule *rule_)
4032 {
4033     struct rule_dpif *rule = rule_dpif_cast(rule_);
4034
4035     ovs_mutex_destroy(&rule->stats_mutex);
4036     if (rule->recirc_id) {
4037         struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4038
4039         ofproto_dpif_free_recirc_id(ofproto, rule->recirc_id);
4040     }
4041 }
4042
4043 static void
4044 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4045                long long int *used)
4046 {
4047     struct rule_dpif *rule = rule_dpif_cast(rule_);
4048
4049     ovs_mutex_lock(&rule->stats_mutex);
4050     *packets = rule->stats.n_packets;
4051     *bytes = rule->stats.n_bytes;
4052     *used = rule->stats.used;
4053     ovs_mutex_unlock(&rule->stats_mutex);
4054 }
4055
4056 static void
4057 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4058                   struct dp_packet *packet)
4059 {
4060     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4061
4062     ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4063 }
4064
4065 static enum ofperr
4066 rule_execute(struct rule *rule, const struct flow *flow,
4067              struct dp_packet *packet)
4068 {
4069     rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4070     dp_packet_delete(packet);
4071     return 0;
4072 }
4073
4074 static void
4075 rule_modify_actions(struct rule *rule_, bool reset_counters)
4076     OVS_REQUIRES(ofproto_mutex)
4077 {
4078     struct rule_dpif *rule = rule_dpif_cast(rule_);
4079
4080     if (reset_counters) {
4081         ovs_mutex_lock(&rule->stats_mutex);
4082         rule->stats.n_packets = 0;
4083         rule->stats.n_bytes = 0;
4084         ovs_mutex_unlock(&rule->stats_mutex);
4085     }
4086
4087     complete_operation(rule);
4088 }
4089
4090 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4091 {
4092     return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4093 }
4094
4095 static struct ofgroup *
4096 group_alloc(void)
4097 {
4098     struct group_dpif *group = xzalloc(sizeof *group);
4099     return &group->up;
4100 }
4101
4102 static void
4103 group_dealloc(struct ofgroup *group_)
4104 {
4105     struct group_dpif *group = group_dpif_cast(group_);
4106     free(group);
4107 }
4108
4109 static void
4110 group_construct_stats(struct group_dpif *group)
4111     OVS_REQUIRES(group->stats_mutex)
4112 {
4113     struct ofputil_bucket *bucket;
4114     const struct ovs_list *buckets;
4115
4116     group->packet_count = 0;
4117     group->byte_count = 0;
4118
4119     group_dpif_get_buckets(group, &buckets);
4120     LIST_FOR_EACH (bucket, list_node, buckets) {
4121         bucket->stats.packet_count = 0;
4122         bucket->stats.byte_count = 0;
4123     }
4124 }
4125
4126 void
4127 group_dpif_credit_stats(struct group_dpif *group,
4128                         struct ofputil_bucket *bucket,
4129                         const struct dpif_flow_stats *stats)
4130 {
4131     ovs_mutex_lock(&group->stats_mutex);
4132     group->packet_count += stats->n_packets;
4133     group->byte_count += stats->n_bytes;
4134     if (bucket) {
4135         bucket->stats.packet_count += stats->n_packets;
4136         bucket->stats.byte_count += stats->n_bytes;
4137     } else { /* Credit to all buckets */
4138         const struct ovs_list *buckets;
4139
4140         group_dpif_get_buckets(group, &buckets);
4141         LIST_FOR_EACH (bucket, list_node, buckets) {
4142             bucket->stats.packet_count += stats->n_packets;
4143             bucket->stats.byte_count += stats->n_bytes;
4144         }
4145     }
4146     ovs_mutex_unlock(&group->stats_mutex);
4147 }
4148
4149 static enum ofperr
4150 group_construct(struct ofgroup *group_)
4151 {
4152     struct group_dpif *group = group_dpif_cast(group_);
4153     const struct ofputil_bucket *bucket;
4154
4155     /* Prevent group chaining because our locking structure makes it hard to
4156      * implement deadlock-free.  (See xlate_group_resource_check().) */
4157     LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4158         const struct ofpact *a;
4159
4160         OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
4161             if (a->type == OFPACT_GROUP) {
4162                 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
4163             }
4164         }
4165     }
4166
4167     ovs_mutex_init_adaptive(&group->stats_mutex);
4168     ovs_mutex_lock(&group->stats_mutex);
4169     group_construct_stats(group);
4170     ovs_mutex_unlock(&group->stats_mutex);
4171     return 0;
4172 }
4173
4174 static void
4175 group_destruct(struct ofgroup *group_)
4176 {
4177     struct group_dpif *group = group_dpif_cast(group_);
4178     ovs_mutex_destroy(&group->stats_mutex);
4179 }
4180
4181 static enum ofperr
4182 group_modify(struct ofgroup *group_)
4183 {
4184     struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4185
4186     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4187
4188     return 0;
4189 }
4190
4191 static enum ofperr
4192 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4193 {
4194     struct group_dpif *group = group_dpif_cast(group_);
4195     struct ofputil_bucket *bucket;
4196     const struct ovs_list *buckets;
4197     struct bucket_counter *bucket_stats;
4198
4199     ovs_mutex_lock(&group->stats_mutex);
4200     ogs->packet_count = group->packet_count;
4201     ogs->byte_count = group->byte_count;
4202
4203     group_dpif_get_buckets(group, &buckets);
4204     bucket_stats = ogs->bucket_stats;
4205     LIST_FOR_EACH (bucket, list_node, buckets) {
4206         bucket_stats->packet_count = bucket->stats.packet_count;
4207         bucket_stats->byte_count = bucket->stats.byte_count;
4208         bucket_stats++;
4209     }
4210     ovs_mutex_unlock(&group->stats_mutex);
4211
4212     return 0;
4213 }
4214
4215 /* If the group exists, this function increments the groups's reference count.
4216  *
4217  * Make sure to call group_dpif_unref() after no longer needing to maintain
4218  * a reference to the group. */
4219 bool
4220 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4221                   struct group_dpif **group)
4222 {
4223     struct ofgroup *ofgroup;
4224     bool found;
4225
4226     found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4227     *group = found ?  group_dpif_cast(ofgroup) : NULL;
4228
4229     return found;
4230 }
4231
4232 void
4233 group_dpif_get_buckets(const struct group_dpif *group,
4234                        const struct ovs_list **buckets)
4235 {
4236     *buckets = &group->up.buckets;
4237 }
4238
4239 enum ofp11_group_type
4240 group_dpif_get_type(const struct group_dpif *group)
4241 {
4242     return group->up.type;
4243 }
4244
4245 const char *
4246 group_dpif_get_selection_method(const struct group_dpif *group)
4247 {
4248     return group->up.props.selection_method;
4249 }
4250 \f
4251 /* Sends 'packet' out 'ofport'.
4252  * May modify 'packet'.
4253  * Returns 0 if successful, otherwise a positive errno value. */
4254 int
4255 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4256 {
4257     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4258     int error;
4259
4260     error = xlate_send_packet(ofport, packet);
4261
4262     ovs_mutex_lock(&ofproto->stats_mutex);
4263     ofproto->stats.tx_packets++;
4264     ofproto->stats.tx_bytes += dp_packet_size(packet);
4265     ovs_mutex_unlock(&ofproto->stats_mutex);
4266     return error;
4267 }
4268 \f
4269 /* Return the version string of the datapath that backs up
4270  * this 'ofproto'.
4271  */
4272 static const char *
4273 get_datapath_version(const struct ofproto *ofproto_)
4274 {
4275     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4276
4277     return ofproto->backer->dp_version_string;
4278 }
4279
4280 static bool
4281 set_frag_handling(struct ofproto *ofproto_,
4282                   enum ofp_config_flags frag_handling)
4283 {
4284     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4285     if (frag_handling != OFPC_FRAG_REASM) {
4286         ofproto->backer->need_revalidate = REV_RECONFIGURE;
4287         return true;
4288     } else {
4289         return false;
4290     }
4291 }
4292
4293 static enum ofperr
4294 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4295            const struct flow *flow,
4296            const struct ofpact *ofpacts, size_t ofpacts_len)
4297 {
4298     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4299
4300     ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4301                                  ofpacts_len, packet);
4302     return 0;
4303 }
4304 \f
4305 /* NetFlow. */
4306
4307 static int
4308 set_netflow(struct ofproto *ofproto_,
4309             const struct netflow_options *netflow_options)
4310 {
4311     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4312
4313     if (netflow_options) {
4314         if (!ofproto->netflow) {
4315             ofproto->netflow = netflow_create();
4316             ofproto->backer->need_revalidate = REV_RECONFIGURE;
4317         }
4318         return netflow_set_options(ofproto->netflow, netflow_options);
4319     } else if (ofproto->netflow) {
4320         ofproto->backer->need_revalidate = REV_RECONFIGURE;
4321         netflow_unref(ofproto->netflow);
4322         ofproto->netflow = NULL;
4323     }
4324
4325     return 0;
4326 }
4327
4328 static void
4329 get_netflow_ids(const struct ofproto *ofproto_,
4330                 uint8_t *engine_type, uint8_t *engine_id)
4331 {
4332     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4333
4334     dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4335 }
4336 \f
4337 static struct ofproto_dpif *
4338 ofproto_dpif_lookup(const char *name)
4339 {
4340     struct ofproto_dpif *ofproto;
4341
4342     HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4343                              hash_string(name, 0), &all_ofproto_dpifs) {
4344         if (!strcmp(ofproto->up.name, name)) {
4345             return ofproto;
4346         }
4347     }
4348     return NULL;
4349 }
4350
4351 static void
4352 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4353                           const char *argv[], void *aux OVS_UNUSED)
4354 {
4355     struct ofproto_dpif *ofproto;
4356
4357     if (argc > 1) {
4358         ofproto = ofproto_dpif_lookup(argv[1]);
4359         if (!ofproto) {
4360             unixctl_command_reply_error(conn, "no such bridge");
4361             return;
4362         }
4363         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4364         mac_learning_flush(ofproto->ml);
4365         ovs_rwlock_unlock(&ofproto->ml->rwlock);
4366     } else {
4367         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4368             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4369             mac_learning_flush(ofproto->ml);
4370             ovs_rwlock_unlock(&ofproto->ml->rwlock);
4371         }
4372     }
4373
4374     unixctl_command_reply(conn, "table successfully flushed");
4375 }
4376
4377 static void
4378 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4379                                      const char *argv[], void *aux OVS_UNUSED)
4380 {
4381     struct ofproto_dpif *ofproto;
4382
4383     if (argc > 1) {
4384         ofproto = ofproto_dpif_lookup(argv[1]);
4385         if (!ofproto) {
4386             unixctl_command_reply_error(conn, "no such bridge");
4387             return;
4388         }
4389
4390         if (!mcast_snooping_enabled(ofproto->ms)) {
4391             unixctl_command_reply_error(conn, "multicast snooping is disabled");
4392             return;
4393         }
4394         mcast_snooping_mdb_flush(ofproto->ms);
4395     } else {
4396         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4397             if (!mcast_snooping_enabled(ofproto->ms)) {
4398                 continue;
4399             }
4400             mcast_snooping_mdb_flush(ofproto->ms);
4401         }
4402     }
4403
4404     unixctl_command_reply(conn, "table successfully flushed");
4405 }
4406
4407 static struct ofport_dpif *
4408 ofbundle_get_a_port(const struct ofbundle *bundle)
4409 {
4410     return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4411                         bundle_node);
4412 }
4413
4414 static void
4415 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4416                          const char *argv[], void *aux OVS_UNUSED)
4417 {
4418     struct ds ds = DS_EMPTY_INITIALIZER;
4419     const struct ofproto_dpif *ofproto;
4420     const struct mac_entry *e;
4421
4422     ofproto = ofproto_dpif_lookup(argv[1]);
4423     if (!ofproto) {
4424         unixctl_command_reply_error(conn, "no such bridge");
4425         return;
4426     }
4427
4428     ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
4429     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4430     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4431         struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4432         char name[OFP_MAX_PORT_NAME_LEN];
4433
4434         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4435                                name, sizeof name);
4436         ds_put_format(&ds, "%5s  %4d  "ETH_ADDR_FMT"  %3d\n",
4437                       name, e->vlan, ETH_ADDR_ARGS(e->mac),
4438                       mac_entry_age(ofproto->ml, e));
4439     }
4440     ovs_rwlock_unlock(&ofproto->ml->rwlock);
4441     unixctl_command_reply(conn, ds_cstr(&ds));
4442     ds_destroy(&ds);
4443 }
4444
4445 static void
4446 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4447                                     int argc OVS_UNUSED,
4448                                     const char *argv[],
4449                                     void *aux OVS_UNUSED)
4450 {
4451     struct ds ds = DS_EMPTY_INITIALIZER;
4452     const struct ofproto_dpif *ofproto;
4453     const struct ofbundle *bundle;
4454     const struct mcast_group *grp;
4455     struct mcast_group_bundle *b;
4456     struct mcast_mrouter_bundle *mrouter;
4457
4458     ofproto = ofproto_dpif_lookup(argv[1]);
4459     if (!ofproto) {
4460         unixctl_command_reply_error(conn, "no such bridge");
4461         return;
4462     }
4463
4464     if (!mcast_snooping_enabled(ofproto->ms)) {
4465         unixctl_command_reply_error(conn, "multicast snooping is disabled");
4466         return;
4467     }
4468
4469     ds_put_cstr(&ds, " port  VLAN  GROUP                Age\n");
4470     ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4471     LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4472         LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4473             char name[OFP_MAX_PORT_NAME_LEN];
4474
4475             bundle = b->port;
4476             ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4477                                    name, sizeof name);
4478             ds_put_format(&ds, "%5s  %4d  "IP_FMT"         %3d\n",
4479                           name, grp->vlan, IP_ARGS(grp->ip4),
4480                           mcast_bundle_age(ofproto->ms, b));
4481         }
4482     }
4483
4484     /* ports connected to multicast routers */
4485     LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4486         char name[OFP_MAX_PORT_NAME_LEN];
4487
4488         bundle = mrouter->port;
4489         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4490                                name, sizeof name);
4491             ds_put_format(&ds, "%5s  %4d  querier             %3d\n",
4492                       name, mrouter->vlan,
4493                       mcast_mrouter_age(ofproto->ms, mrouter));
4494     }
4495     ovs_rwlock_unlock(&ofproto->ms->rwlock);
4496     unixctl_command_reply(conn, ds_cstr(&ds));
4497     ds_destroy(&ds);
4498 }
4499
4500 struct trace_ctx {
4501     struct xlate_out xout;
4502     struct xlate_in xin;
4503     const struct flow *key;
4504     struct flow flow;
4505     struct flow_wildcards wc;
4506     struct ds *result;
4507 };
4508
4509 static void
4510 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4511 {
4512     const struct rule_actions *actions;
4513     ovs_be64 cookie;
4514
4515     ds_put_char_multiple(result, '\t', level);
4516     if (!rule) {
4517         ds_put_cstr(result, "No match\n");
4518         return;
4519     }
4520
4521     ovs_mutex_lock(&rule->up.mutex);
4522     cookie = rule->up.flow_cookie;
4523     ovs_mutex_unlock(&rule->up.mutex);
4524
4525     ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4526                   rule ? rule->up.table_id : 0, ntohll(cookie));
4527     cls_rule_format(&rule->up.cr, result);
4528     ds_put_char(result, '\n');
4529
4530     actions = rule_dpif_get_actions(rule);
4531
4532     ds_put_char_multiple(result, '\t', level);
4533     ds_put_cstr(result, "OpenFlow actions=");
4534     ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4535     ds_put_char(result, '\n');
4536 }
4537
4538 static void
4539 trace_format_flow(struct ds *result, int level, const char *title,
4540                   struct trace_ctx *trace)
4541 {
4542     ds_put_char_multiple(result, '\t', level);
4543     ds_put_format(result, "%s: ", title);
4544     /* Do not report unchanged flows for resubmits. */
4545     if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4546         || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4547         ds_put_cstr(result, "unchanged");
4548     } else {
4549         flow_format(result, &trace->xin.flow);
4550         trace->flow = trace->xin.flow;
4551     }
4552     ds_put_char(result, '\n');
4553 }
4554
4555 static void
4556 trace_format_regs(struct ds *result, int level, const char *title,
4557                   struct trace_ctx *trace)
4558 {
4559     size_t i;
4560
4561     ds_put_char_multiple(result, '\t', level);
4562     ds_put_format(result, "%s:", title);
4563     for (i = 0; i < FLOW_N_REGS; i++) {
4564         ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4565     }
4566     ds_put_char(result, '\n');
4567 }
4568
4569 static void
4570 trace_format_odp(struct ds *result, int level, const char *title,
4571                  struct trace_ctx *trace)
4572 {
4573     struct ofpbuf *odp_actions = trace->xout.odp_actions;
4574
4575     ds_put_char_multiple(result, '\t', level);
4576     ds_put_format(result, "%s: ", title);
4577     format_odp_actions(result, odp_actions->data, odp_actions->size);
4578     ds_put_char(result, '\n');
4579 }
4580
4581 static void
4582 trace_format_megaflow(struct ds *result, int level, const char *title,
4583                       struct trace_ctx *trace)
4584 {
4585     struct match match;
4586
4587     ds_put_char_multiple(result, '\t', level);
4588     ds_put_format(result, "%s: ", title);
4589     flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
4590     match_init(&match, trace->key, &trace->wc);
4591     match_format(&match, result, OFP_DEFAULT_PRIORITY);
4592     ds_put_char(result, '\n');
4593 }
4594
4595 static void trace_report(struct xlate_in *xin, const char *s, int recurse);
4596
4597 static void
4598 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4599 {
4600     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4601     struct ds *result = trace->result;
4602
4603     if (!recurse) {
4604         if (rule == xin->ofproto->miss_rule) {
4605             trace_report(xin, "No match, flow generates \"packet in\"s.",
4606                          recurse);
4607         } else if (rule == xin->ofproto->no_packet_in_rule) {
4608             trace_report(xin, "No match, packets dropped because "
4609                          "OFPPC_NO_PACKET_IN is set on in_port.", recurse);
4610         } else if (rule == xin->ofproto->drop_frags_rule) {
4611             trace_report(xin, "Packets dropped because they are IP "
4612                          "fragments and the fragment handling mode is "
4613                          "\"drop\".", recurse);
4614         }
4615     }
4616
4617     ds_put_char(result, '\n');
4618     if (recurse) {
4619         trace_format_flow(result, recurse, "Resubmitted flow", trace);
4620         trace_format_regs(result, recurse, "Resubmitted regs", trace);
4621         trace_format_odp(result,  recurse, "Resubmitted  odp", trace);
4622         trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
4623     }
4624     trace_format_rule(result, recurse, rule);
4625 }
4626
4627 static void
4628 trace_report(struct xlate_in *xin, const char *s, int recurse)
4629 {
4630     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4631     struct ds *result = trace->result;
4632
4633     ds_put_char_multiple(result, '\t', recurse);
4634     ds_put_cstr(result, s);
4635     ds_put_char(result, '\n');
4636 }
4637
4638 /* Parses the 'argc' elements of 'argv', ignoring argv[0].  The following
4639  * forms are supported:
4640  *
4641  *     - [dpname] odp_flow [-generate | packet]
4642  *     - bridge br_flow [-generate | packet]
4643  *
4644  * On success, initializes '*ofprotop' and 'flow' and returns NULL.  On failure
4645  * returns a nonnull malloced error message. */
4646 static char * OVS_WARN_UNUSED_RESULT
4647 parse_flow_and_packet(int argc, const char *argv[],
4648                       struct ofproto_dpif **ofprotop, struct flow *flow,
4649                       struct dp_packet **packetp)
4650 {
4651     const struct dpif_backer *backer = NULL;
4652     const char *error = NULL;
4653     char *m_err = NULL;
4654     struct simap port_names = SIMAP_INITIALIZER(&port_names);
4655     struct dp_packet *packet;
4656     struct ofpbuf odp_key;
4657     struct ofpbuf odp_mask;
4658
4659     ofpbuf_init(&odp_key, 0);
4660     ofpbuf_init(&odp_mask, 0);
4661
4662     /* Handle "-generate" or a hex string as the last argument. */
4663     if (!strcmp(argv[argc - 1], "-generate")) {
4664         packet = dp_packet_new(0);
4665         argc--;
4666     } else {
4667         error = eth_from_hex(argv[argc - 1], &packet);
4668         if (!error) {
4669             argc--;
4670         } else if (argc == 4) {
4671             /* The 3-argument form must end in "-generate' or a hex string. */
4672             goto exit;
4673         }
4674         error = NULL;
4675     }
4676
4677     /* odp_flow can have its in_port specified as a name instead of port no.
4678      * We do not yet know whether a given flow is a odp_flow or a br_flow.
4679      * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4680      * we need to create a simap of name to port no. */
4681     if (argc == 3) {
4682         const char *dp_type;
4683         if (!strncmp(argv[1], "ovs-", 4)) {
4684             dp_type = argv[1] + 4;
4685         } else {
4686             dp_type = argv[1];
4687         }
4688         backer = shash_find_data(&all_dpif_backers, dp_type);
4689     } else if (argc == 2) {
4690         struct shash_node *node;
4691         if (shash_count(&all_dpif_backers) == 1) {
4692             node = shash_first(&all_dpif_backers);
4693             backer = node->data;
4694         }
4695     } else {
4696         error = "Syntax error";
4697         goto exit;
4698     }
4699     if (backer && backer->dpif) {
4700         struct dpif_port dpif_port;
4701         struct dpif_port_dump port_dump;
4702         DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4703             simap_put(&port_names, dpif_port.name,
4704                       odp_to_u32(dpif_port.port_no));
4705         }
4706     }
4707
4708     /* Parse the flow and determine whether a datapath or
4709      * bridge is specified. If function odp_flow_key_from_string()
4710      * returns 0, the flow is a odp_flow. If function
4711      * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4712     if (!odp_flow_from_string(argv[argc - 1], &port_names,
4713                               &odp_key, &odp_mask)) {
4714         if (!backer) {
4715             error = "Cannot find the datapath";
4716             goto exit;
4717         }
4718
4719         if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4720             error = "Failed to parse flow key";
4721             goto exit;
4722         }
4723
4724         *ofprotop = xlate_lookup_ofproto(backer, flow,
4725                                          &flow->in_port.ofp_port);
4726         if (*ofprotop == NULL) {
4727             error = "Invalid datapath flow";
4728             goto exit;
4729         }
4730
4731         vsp_adjust_flow(*ofprotop, flow, NULL);
4732
4733     } else {
4734         char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4735
4736         if (err) {
4737             m_err = xasprintf("Bad flow syntax: %s", err);
4738             free(err);
4739             goto exit;
4740         } else {
4741             if (argc != 3) {
4742                 error = "Must specify bridge name";
4743                 goto exit;
4744             }
4745
4746             *ofprotop = ofproto_dpif_lookup(argv[1]);
4747             if (!*ofprotop) {
4748                 error = "Unknown bridge name";
4749                 goto exit;
4750             }
4751         }
4752     }
4753
4754     /* Generate a packet, if requested. */
4755     if (packet) {
4756         if (!dp_packet_size(packet)) {
4757             flow_compose(packet, flow);
4758         } else {
4759             /* Use the metadata from the flow and the packet argument
4760              * to reconstruct the flow. */
4761             pkt_metadata_from_flow(&packet->md, flow);
4762             flow_extract(packet, flow);
4763         }
4764     }
4765
4766 exit:
4767     if (error && !m_err) {
4768         m_err = xstrdup(error);
4769     }
4770     if (m_err) {
4771         dp_packet_delete(packet);
4772         packet = NULL;
4773     }
4774     *packetp = packet;
4775     ofpbuf_uninit(&odp_key);
4776     ofpbuf_uninit(&odp_mask);
4777     simap_destroy(&port_names);
4778     return m_err;
4779 }
4780
4781 static void
4782 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4783                       void *aux OVS_UNUSED)
4784 {
4785     struct ofproto_dpif *ofproto;
4786     struct dp_packet *packet;
4787     char *error;
4788     struct flow flow;
4789
4790     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4791     if (!error) {
4792         struct ds result;
4793
4794         ds_init(&result);
4795         ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4796         unixctl_command_reply(conn, ds_cstr(&result));
4797         ds_destroy(&result);
4798         dp_packet_delete(packet);
4799     } else {
4800         unixctl_command_reply_error(conn, error);
4801         free(error);
4802     }
4803 }
4804
4805 static void
4806 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4807                               const char *argv[], void *aux OVS_UNUSED)
4808 {
4809     enum ofputil_protocol usable_protocols;
4810     struct ofproto_dpif *ofproto;
4811     bool enforce_consistency;
4812     struct ofpbuf ofpacts;
4813     struct dp_packet *packet;
4814     struct ds result;
4815     struct flow flow;
4816     uint16_t in_port;
4817
4818     /* Three kinds of error return values! */
4819     enum ofperr retval;
4820     char *error;
4821
4822     packet = NULL;
4823     ds_init(&result);
4824     ofpbuf_init(&ofpacts, 0);
4825
4826     /* Parse actions. */
4827     error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4828     if (error) {
4829         unixctl_command_reply_error(conn, error);
4830         free(error);
4831         goto exit;
4832     }
4833
4834     /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4835      * consistency between the flow and the actions.  With -consistent, we
4836      * enforce consistency even for a flow supported in OpenFlow 1.0. */
4837     if (!strcmp(argv[1], "-consistent")) {
4838         enforce_consistency = true;
4839         argv++;
4840         argc--;
4841     } else {
4842         enforce_consistency = false;
4843     }
4844
4845     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4846     if (error) {
4847         unixctl_command_reply_error(conn, error);
4848         free(error);
4849         goto exit;
4850     }
4851
4852     /* Do the same checks as handle_packet_out() in ofproto.c.
4853      *
4854      * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4855      * strictly correct because these actions aren't in any table, but it's OK
4856      * because it 'table_id' is used only to check goto_table instructions, but
4857      * packet-outs take a list of actions and therefore it can't include
4858      * instructions.
4859      *
4860      * We skip the "meter" check here because meter is an instruction, not an
4861      * action, and thus cannot appear in ofpacts. */
4862     in_port = ofp_to_u16(flow.in_port.ofp_port);
4863     if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4864         unixctl_command_reply_error(conn, "invalid in_port");
4865         goto exit;
4866     }
4867     if (enforce_consistency) {
4868         retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
4869                                            &flow, u16_to_ofp(ofproto->up.max_ports),
4870                                            0, 0, usable_protocols);
4871     } else {
4872         retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
4873                                u16_to_ofp(ofproto->up.max_ports), 0, 0,
4874                                &usable_protocols);
4875     }
4876
4877     if (retval) {
4878         ds_clear(&result);
4879         ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4880         unixctl_command_reply_error(conn, ds_cstr(&result));
4881         goto exit;
4882     }
4883
4884     ofproto_trace(ofproto, &flow, packet,
4885                   ofpacts.data, ofpacts.size, &result);
4886     unixctl_command_reply(conn, ds_cstr(&result));
4887
4888 exit:
4889     ds_destroy(&result);
4890     dp_packet_delete(packet);
4891     ofpbuf_uninit(&ofpacts);
4892 }
4893
4894 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4895  * description of the results to 'ds'.
4896  *
4897  * The trace follows a packet with the specified 'flow' through the flow
4898  * table.  'packet' may be nonnull to trace an actual packet, with consequent
4899  * side effects (if it is nonnull then its flow must be 'flow').
4900  *
4901  * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4902  * trace, otherwise the actions are determined by a flow table lookup. */
4903 static void
4904 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4905               const struct dp_packet *packet,
4906               const struct ofpact ofpacts[], size_t ofpacts_len,
4907               struct ds *ds)
4908 {
4909     struct trace_ctx trace;
4910
4911     ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4912     ds_put_cstr(ds, "Flow: ");
4913     flow_format(ds, flow);
4914     ds_put_char(ds, '\n');
4915
4916     flow_wildcards_init_catchall(&trace.wc);
4917
4918     trace.result = ds;
4919     trace.key = flow; /* Original flow key, used for megaflow. */
4920     trace.flow = *flow; /* May be modified by actions. */
4921     xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
4922                   ntohs(flow->tcp_flags), packet);
4923     trace.xin.ofpacts = ofpacts;
4924     trace.xin.ofpacts_len = ofpacts_len;
4925     trace.xin.resubmit_hook = trace_resubmit;
4926     trace.xin.report_hook = trace_report;
4927
4928     xlate_actions(&trace.xin, &trace.xout);
4929
4930     ds_put_char(ds, '\n');
4931     trace_format_flow(ds, 0, "Final flow", &trace);
4932     trace_format_megaflow(ds, 0, "Megaflow", &trace);
4933
4934     ds_put_cstr(ds, "Datapath actions: ");
4935     format_odp_actions(ds, trace.xout.odp_actions->data,
4936                        trace.xout.odp_actions->size);
4937
4938     if (trace.xout.slow) {
4939         enum slow_path_reason slow;
4940
4941         ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4942                     "slow path because it:");
4943
4944         slow = trace.xout.slow;
4945         while (slow) {
4946             enum slow_path_reason bit = rightmost_1bit(slow);
4947
4948             ds_put_format(ds, "\n\t- %s.",
4949                           slow_path_reason_to_explanation(bit));
4950
4951             slow &= ~bit;
4952         }
4953     }
4954
4955     xlate_out_uninit(&trace.xout);
4956 }
4957
4958 /* Store the current ofprotos in 'ofproto_shash'.  Returns a sorted list
4959  * of the 'ofproto_shash' nodes.  It is the responsibility of the caller
4960  * to destroy 'ofproto_shash' and free the returned value. */
4961 static const struct shash_node **
4962 get_ofprotos(struct shash *ofproto_shash)
4963 {
4964     const struct ofproto_dpif *ofproto;
4965
4966     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4967         char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4968         shash_add_nocopy(ofproto_shash, name, ofproto);
4969     }
4970
4971     return shash_sort(ofproto_shash);
4972 }
4973
4974 static void
4975 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4976                               const char *argv[] OVS_UNUSED,
4977                               void *aux OVS_UNUSED)
4978 {
4979     struct ds ds = DS_EMPTY_INITIALIZER;
4980     struct shash ofproto_shash;
4981     const struct shash_node **sorted_ofprotos;
4982     int i;
4983
4984     shash_init(&ofproto_shash);
4985     sorted_ofprotos = get_ofprotos(&ofproto_shash);
4986     for (i = 0; i < shash_count(&ofproto_shash); i++) {
4987         const struct shash_node *node = sorted_ofprotos[i];
4988         ds_put_format(&ds, "%s\n", node->name);
4989     }
4990
4991     shash_destroy(&ofproto_shash);
4992     free(sorted_ofprotos);
4993
4994     unixctl_command_reply(conn, ds_cstr(&ds));
4995     ds_destroy(&ds);
4996 }
4997
4998 static void
4999 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5000 {
5001     const struct shash_node **ofprotos;
5002     struct dpif_dp_stats dp_stats;
5003     struct shash ofproto_shash;
5004     size_t i;
5005
5006     dpif_get_dp_stats(backer->dpif, &dp_stats);
5007
5008     ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5009                   dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5010
5011     shash_init(&ofproto_shash);
5012     ofprotos = get_ofprotos(&ofproto_shash);
5013     for (i = 0; i < shash_count(&ofproto_shash); i++) {
5014         struct ofproto_dpif *ofproto = ofprotos[i]->data;
5015         const struct shash_node **ports;
5016         size_t j;
5017
5018         if (ofproto->backer != backer) {
5019             continue;
5020         }
5021
5022         ds_put_format(ds, "\t%s:\n", ofproto->up.name);
5023
5024         ports = shash_sort(&ofproto->up.port_by_name);
5025         for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5026             const struct shash_node *node = ports[j];
5027             struct ofport *ofport = node->data;
5028             struct smap config;
5029             odp_port_t odp_port;
5030
5031             ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5032                           ofport->ofp_port);
5033
5034             odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5035             if (odp_port != ODPP_NONE) {
5036                 ds_put_format(ds, "%"PRIu32":", odp_port);
5037             } else {
5038                 ds_put_cstr(ds, "none:");
5039             }
5040
5041             ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5042
5043             smap_init(&config);
5044             if (!netdev_get_config(ofport->netdev, &config)) {
5045                 const struct smap_node **nodes;
5046                 size_t i;
5047
5048                 nodes = smap_sort(&config);
5049                 for (i = 0; i < smap_count(&config); i++) {
5050                     const struct smap_node *node = nodes[i];
5051                     ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5052                                   node->key, node->value);
5053                 }
5054                 free(nodes);
5055             }
5056             smap_destroy(&config);
5057
5058             ds_put_char(ds, ')');
5059             ds_put_char(ds, '\n');
5060         }
5061         free(ports);
5062     }
5063     shash_destroy(&ofproto_shash);
5064     free(ofprotos);
5065 }
5066
5067 static void
5068 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5069                           const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5070 {
5071     struct ds ds = DS_EMPTY_INITIALIZER;
5072     const struct shash_node **backers;
5073     int i;
5074
5075     backers = shash_sort(&all_dpif_backers);
5076     for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5077         dpif_show_backer(backers[i]->data, &ds);
5078     }
5079     free(backers);
5080
5081     unixctl_command_reply(conn, ds_cstr(&ds));
5082     ds_destroy(&ds);
5083 }
5084
5085 static void
5086 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5087                                 int argc OVS_UNUSED, const char *argv[],
5088                                 void *aux OVS_UNUSED)
5089 {
5090     const struct ofproto_dpif *ofproto;
5091
5092     struct ds ds = DS_EMPTY_INITIALIZER;
5093     bool verbosity = false;
5094
5095     struct dpif_port dpif_port;
5096     struct dpif_port_dump port_dump;
5097     struct hmap portno_names;
5098
5099     struct dpif_flow_dump *flow_dump;
5100     struct dpif_flow_dump_thread *flow_dump_thread;
5101     struct dpif_flow f;
5102     int error;
5103
5104     ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5105     if (!ofproto) {
5106         unixctl_command_reply_error(conn, "no such bridge");
5107         return;
5108     }
5109
5110     if (argc > 2 && !strcmp(argv[1], "-m")) {
5111         verbosity = true;
5112     }
5113
5114     hmap_init(&portno_names);
5115     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5116         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5117     }
5118
5119     ds_init(&ds);
5120     flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5121     flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5122     while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5123         struct flow flow;
5124
5125         if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5126             || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5127             continue;
5128         }
5129
5130         if (verbosity) {
5131             odp_format_ufid(&f.ufid, &ds);
5132             ds_put_cstr(&ds, " ");
5133         }
5134         odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5135                         &portno_names, &ds, verbosity);
5136         ds_put_cstr(&ds, ", ");
5137         dpif_flow_stats_format(&f.stats, &ds);
5138         ds_put_cstr(&ds, ", actions:");
5139         format_odp_actions(&ds, f.actions, f.actions_len);
5140         ds_put_char(&ds, '\n');
5141     }
5142     dpif_flow_dump_thread_destroy(flow_dump_thread);
5143     error = dpif_flow_dump_destroy(flow_dump);
5144
5145     if (error) {
5146         ds_clear(&ds);
5147         ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5148         unixctl_command_reply_error(conn, ds_cstr(&ds));
5149     } else {
5150         unixctl_command_reply(conn, ds_cstr(&ds));
5151     }
5152     odp_portno_names_destroy(&portno_names);
5153     hmap_destroy(&portno_names);
5154     ds_destroy(&ds);
5155 }
5156
5157 static void
5158 ofproto_revalidate_all_backers(void)
5159 {
5160     const struct shash_node **backers;
5161     int i;
5162
5163     backers = shash_sort(&all_dpif_backers);
5164     for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5165         struct dpif_backer *backer = backers[i]->data;
5166         backer->need_revalidate = REV_RECONFIGURE;
5167     }
5168     free(backers);
5169 }
5170
5171 static void
5172 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5173                      const char *argv[], void *aux OVS_UNUSED)
5174 {
5175     if (!strcasecmp(argv[1], "off")) {
5176         ofproto_use_tnl_push_pop = false;
5177         unixctl_command_reply(conn, "Tunnel push-pop off");
5178         ofproto_revalidate_all_backers();
5179     } else if (!strcasecmp(argv[1], "on")) {
5180         ofproto_use_tnl_push_pop = true;
5181         unixctl_command_reply(conn, "Tunnel push-pop on");
5182         ofproto_revalidate_all_backers();
5183     }
5184 }
5185
5186 static void
5187 ofproto_unixctl_init(void)
5188 {
5189     static bool registered;
5190     if (registered) {
5191         return;
5192     }
5193     registered = true;
5194
5195     unixctl_command_register(
5196         "ofproto/trace",
5197         "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5198         1, 3, ofproto_unixctl_trace, NULL);
5199     unixctl_command_register(
5200         "ofproto/trace-packet-out",
5201         "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5202         2, 6, ofproto_unixctl_trace_actions, NULL);
5203     unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5204                              ofproto_unixctl_fdb_flush, NULL);
5205     unixctl_command_register("fdb/show", "bridge", 1, 1,
5206                              ofproto_unixctl_fdb_show, NULL);
5207     unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5208                              ofproto_unixctl_mcast_snooping_flush, NULL);
5209     unixctl_command_register("mdb/show", "bridge", 1, 1,
5210                              ofproto_unixctl_mcast_snooping_show, NULL);
5211     unixctl_command_register("dpif/dump-dps", "", 0, 0,
5212                              ofproto_unixctl_dpif_dump_dps, NULL);
5213     unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5214                              NULL);
5215     unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5216                              ofproto_unixctl_dpif_dump_flows, NULL);
5217
5218     unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5219                              disable_tnl_push_pop, NULL);
5220 }
5221
5222 /* Returns true if 'table' is the table used for internal rules,
5223  * false otherwise. */
5224 bool
5225 table_is_internal(uint8_t table_id)
5226 {
5227     return table_id == TBL_INTERNAL;
5228 }
5229 \f
5230 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5231  *
5232  * This is deprecated.  It is only for compatibility with broken device drivers
5233  * in old versions of Linux that do not properly support VLANs when VLAN
5234  * devices are not used.  When broken device drivers are no longer in
5235  * widespread use, we will delete these interfaces. */
5236
5237 static int
5238 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5239 {
5240     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5241     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5242
5243     if (realdev_ofp_port == ofport->realdev_ofp_port
5244         && vid == ofport->vlandev_vid) {
5245         return 0;
5246     }
5247
5248     ofproto->backer->need_revalidate = REV_RECONFIGURE;
5249
5250     if (ofport->realdev_ofp_port) {
5251         vsp_remove(ofport);
5252     }
5253     if (realdev_ofp_port && ofport->bundle) {
5254         /* vlandevs are enslaved to their realdevs, so they are not allowed to
5255          * themselves be part of a bundle. */
5256         bundle_set(ofport_->ofproto, ofport->bundle, NULL);
5257     }
5258
5259     ofport->realdev_ofp_port = realdev_ofp_port;
5260     ofport->vlandev_vid = vid;
5261
5262     if (realdev_ofp_port) {
5263         vsp_add(ofport, realdev_ofp_port, vid);
5264     }
5265
5266     return 0;
5267 }
5268
5269 static uint32_t
5270 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5271 {
5272     return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5273 }
5274
5275 bool
5276 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5277     OVS_EXCLUDED(ofproto->vsp_mutex)
5278 {
5279     /* hmap_is_empty is thread safe. */
5280     return !hmap_is_empty(&ofproto->realdev_vid_map);
5281 }
5282
5283
5284 static ofp_port_t
5285 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5286                          ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5287     OVS_REQUIRES(ofproto->vsp_mutex)
5288 {
5289     if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5290         int vid = vlan_tci_to_vid(vlan_tci);
5291         const struct vlan_splinter *vsp;
5292
5293         HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5294                                  hash_realdev_vid(realdev_ofp_port, vid),
5295                                  &ofproto->realdev_vid_map) {
5296             if (vsp->realdev_ofp_port == realdev_ofp_port
5297                 && vsp->vid == vid) {
5298                 return vsp->vlandev_ofp_port;
5299             }
5300         }
5301     }
5302     return realdev_ofp_port;
5303 }
5304
5305 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5306  * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5307  * 'struct ofport_dpif'.  For example, given 'realdev_ofp_port' of eth0 and
5308  * 'vlan_tci' 9, it would return the port number of eth0.9.
5309  *
5310  * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5311  * function just returns its 'realdev_ofp_port' argument. */
5312 ofp_port_t
5313 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5314                        ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5315     OVS_EXCLUDED(ofproto->vsp_mutex)
5316 {
5317     ofp_port_t ret;
5318
5319     /* hmap_is_empty is thread safe, see if we can return immediately. */
5320     if (hmap_is_empty(&ofproto->realdev_vid_map)) {
5321         return realdev_ofp_port;
5322     }
5323     ovs_mutex_lock(&ofproto->vsp_mutex);
5324     ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5325     ovs_mutex_unlock(&ofproto->vsp_mutex);
5326     return ret;
5327 }
5328
5329 static struct vlan_splinter *
5330 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5331 {
5332     struct vlan_splinter *vsp;
5333
5334     HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5335                              hash_ofp_port(vlandev_ofp_port),
5336                              &ofproto->vlandev_map) {
5337         if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5338             return vsp;
5339         }
5340     }
5341
5342     return NULL;
5343 }
5344
5345 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5346  * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5347  * VLAN VID of the Linux VLAN device in '*vid'.  For example, given
5348  * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5349  * eth0 and store 9 in '*vid'.
5350  *
5351  * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5352  * VLAN device.  Unless VLAN splinters are enabled, this is what this function
5353  * always does.*/
5354 static ofp_port_t
5355 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5356                        ofp_port_t vlandev_ofp_port, int *vid)
5357     OVS_REQUIRES(ofproto->vsp_mutex)
5358 {
5359     if (!hmap_is_empty(&ofproto->vlandev_map)) {
5360         const struct vlan_splinter *vsp;
5361
5362         vsp = vlandev_find(ofproto, vlandev_ofp_port);
5363         if (vsp) {
5364             if (vid) {
5365                 *vid = vsp->vid;
5366             }
5367             return vsp->realdev_ofp_port;
5368         }
5369     }
5370     return 0;
5371 }
5372
5373 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5374  * whether 'flow->in_port' represents a Linux VLAN device.  If so, changes
5375  * 'flow->in_port' to the "real" device backing the VLAN device, sets
5376  * 'flow->vlan_tci' to the VLAN VID, and returns true.  Optionally pushes the
5377  * appropriate VLAN on 'packet' if provided.  Otherwise (which is always the
5378  * case unless VLAN splinters are enabled), returns false without making any
5379  * changes. */
5380 bool
5381 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
5382                 struct dp_packet *packet)
5383     OVS_EXCLUDED(ofproto->vsp_mutex)
5384 {
5385     ofp_port_t realdev;
5386     int vid;
5387
5388     /* hmap_is_empty is thread safe. */
5389     if (hmap_is_empty(&ofproto->vlandev_map)) {
5390         return false;
5391     }
5392
5393     ovs_mutex_lock(&ofproto->vsp_mutex);
5394     realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
5395     ovs_mutex_unlock(&ofproto->vsp_mutex);
5396     if (!realdev) {
5397         return false;
5398     }
5399
5400     /* Cause the flow to be processed as if it came in on the real device with
5401      * the VLAN device's VLAN ID. */
5402     flow->in_port.ofp_port = realdev;
5403     flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
5404
5405     if (packet) {
5406         /* Make the packet resemble the flow, so that it gets sent to an
5407          * OpenFlow controller properly, so that it looks correct for sFlow,
5408          * and so that flow_extract() will get the correct vlan_tci if it is
5409          * called on 'packet'. */
5410         eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
5411     }
5412
5413     return true;
5414 }
5415
5416 static void
5417 vsp_remove(struct ofport_dpif *port)
5418 {
5419     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5420     struct vlan_splinter *vsp;
5421
5422     ovs_mutex_lock(&ofproto->vsp_mutex);
5423     vsp = vlandev_find(ofproto, port->up.ofp_port);
5424     if (vsp) {
5425         hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5426         hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5427         free(vsp);
5428
5429         port->realdev_ofp_port = 0;
5430     } else {
5431         VLOG_ERR("missing vlan device record");
5432     }
5433     ovs_mutex_unlock(&ofproto->vsp_mutex);
5434 }
5435
5436 static void
5437 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
5438 {
5439     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5440
5441     ovs_mutex_lock(&ofproto->vsp_mutex);
5442     if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5443         && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
5444             == realdev_ofp_port)) {
5445         struct vlan_splinter *vsp;
5446
5447         vsp = xmalloc(sizeof *vsp);
5448         vsp->realdev_ofp_port = realdev_ofp_port;
5449         vsp->vlandev_ofp_port = port->up.ofp_port;
5450         vsp->vid = vid;
5451
5452         port->realdev_ofp_port = realdev_ofp_port;
5453
5454         hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5455                     hash_ofp_port(port->up.ofp_port));
5456         hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5457                     hash_realdev_vid(realdev_ofp_port, vid));
5458     } else {
5459         VLOG_ERR("duplicate vlan device record");
5460     }
5461     ovs_mutex_unlock(&ofproto->vsp_mutex);
5462 }
5463
5464 static odp_port_t
5465 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5466 {
5467     const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
5468     return ofport ? ofport->odp_port : ODPP_NONE;
5469 }
5470
5471 struct ofport_dpif *
5472 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5473 {
5474     struct ofport_dpif *port;
5475
5476     ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5477     HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5478                              &backer->odp_to_ofport_map) {
5479         if (port->odp_port == odp_port) {
5480             ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5481             return port;
5482         }
5483     }
5484
5485     ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5486     return NULL;
5487 }
5488
5489 static ofp_port_t
5490 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5491 {
5492     struct ofport_dpif *port;
5493
5494     port = odp_port_to_ofport(ofproto->backer, odp_port);
5495     if (port && &ofproto->up == port->up.ofproto) {
5496         return port->up.ofp_port;
5497     } else {
5498         return OFPP_NONE;
5499     }
5500 }
5501
5502 struct ofproto_dpif *
5503 ofproto_dpif_recirc_get_ofproto(const struct dpif_backer *backer,
5504                                 uint32_t recirc_id)
5505 {
5506     struct dpif_backer_recirc_node *node;
5507
5508     node = CONTAINER_OF(cmap_find(&backer->recirc_map, recirc_id),
5509                         struct dpif_backer_recirc_node, cmap_node);
5510
5511     return node ? node->ofproto : NULL;
5512 }
5513
5514 uint32_t
5515 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
5516 {
5517     struct dpif_backer *backer = ofproto->backer;
5518     uint32_t recirc_id = recirc_id_alloc(backer->rid_pool);
5519
5520     if (recirc_id) {
5521         struct dpif_backer_recirc_node *node = xmalloc(sizeof *node);
5522
5523         node->recirc_id = recirc_id;
5524         node->ofproto = ofproto;
5525
5526         ovs_mutex_lock(&backer->recirc_mutex);
5527         cmap_insert(&backer->recirc_map, &node->cmap_node, node->recirc_id);
5528         ovs_mutex_unlock(&backer->recirc_mutex);
5529     }
5530
5531     return recirc_id;
5532 }
5533
5534 void
5535 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
5536 {
5537     struct dpif_backer *backer = ofproto->backer;
5538     struct dpif_backer_recirc_node *node;
5539
5540     node = CONTAINER_OF(cmap_find(&backer->recirc_map, recirc_id),
5541                         struct dpif_backer_recirc_node, cmap_node);
5542     if (node) {
5543         ovs_mutex_lock(&backer->recirc_mutex);
5544         cmap_remove(&backer->recirc_map, &node->cmap_node, node->recirc_id);
5545         ovs_mutex_unlock(&backer->recirc_mutex);
5546         recirc_id_free(backer->rid_pool, node->recirc_id);
5547
5548         /* 'recirc_id' should never be freed by non-owning 'ofproto'. */
5549         ovs_assert(node->ofproto == ofproto);
5550
5551         /* RCU postpone the free, since other threads may be referring
5552          * to 'node' at same time. */
5553         ovsrcu_postpone(free, node);
5554     }
5555 }
5556
5557 int
5558 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5559                                const struct match *match, int priority,
5560                                uint16_t idle_timeout,
5561                                const struct ofpbuf *ofpacts,
5562                                struct rule **rulep)
5563 {
5564     struct ofputil_flow_mod fm;
5565     struct rule_dpif *rule;
5566     int error;
5567
5568     fm.match = *match;
5569     fm.priority = priority;
5570     fm.new_cookie = htonll(0);
5571     fm.cookie = htonll(0);
5572     fm.cookie_mask = htonll(0);
5573     fm.modify_cookie = false;
5574     fm.table_id = TBL_INTERNAL;
5575     fm.command = OFPFC_ADD;
5576     fm.idle_timeout = idle_timeout;
5577     fm.hard_timeout = 0;
5578     fm.importance = 0;
5579     fm.buffer_id = 0;
5580     fm.out_port = 0;
5581     fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5582     fm.ofpacts = ofpacts->data;
5583     fm.ofpacts_len = ofpacts->size;
5584
5585     error = ofproto_flow_mod(&ofproto->up, &fm);
5586     if (error) {
5587         VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5588                     ofperr_to_string(error));
5589         *rulep = NULL;
5590         return error;
5591     }
5592
5593     rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &fm.match.flow,
5594                                      &fm.match.wc, false);
5595     if (rule) {
5596         *rulep = &rule->up;
5597     } else {
5598         OVS_NOT_REACHED();
5599     }
5600     return 0;
5601 }
5602
5603 int
5604 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5605                                   struct match *match, int priority)
5606 {
5607     struct ofputil_flow_mod fm;
5608     int error;
5609
5610     fm.match = *match;
5611     fm.priority = priority;
5612     fm.new_cookie = htonll(0);
5613     fm.cookie = htonll(0);
5614     fm.cookie_mask = htonll(0);
5615     fm.modify_cookie = false;
5616     fm.table_id = TBL_INTERNAL;
5617     fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5618     fm.command = OFPFC_DELETE_STRICT;
5619
5620     error = ofproto_flow_mod(&ofproto->up, &fm);
5621     if (error) {
5622         VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5623                     ofperr_to_string(error));
5624         return error;
5625     }
5626
5627     return 0;
5628 }
5629
5630 const struct ofproto_class ofproto_dpif_class = {
5631     init,
5632     enumerate_types,
5633     enumerate_names,
5634     del,
5635     port_open_type,
5636     type_run,
5637     type_wait,
5638     alloc,
5639     construct,
5640     destruct,
5641     dealloc,
5642     run,
5643     wait,
5644     NULL,                       /* get_memory_usage. */
5645     type_get_memory_usage,
5646     flush,
5647     query_tables,
5648     port_alloc,
5649     port_construct,
5650     port_destruct,
5651     port_dealloc,
5652     port_modified,
5653     port_reconfigured,
5654     port_query_by_name,
5655     port_add,
5656     port_del,
5657     port_get_stats,
5658     port_dump_start,
5659     port_dump_next,
5660     port_dump_done,
5661     port_poll,
5662     port_poll_wait,
5663     port_is_lacp_current,
5664     port_get_lacp_stats,
5665     NULL,                       /* rule_choose_table */
5666     rule_alloc,
5667     rule_construct,
5668     rule_insert,
5669     rule_delete,
5670     rule_destruct,
5671     rule_dealloc,
5672     rule_get_stats,
5673     rule_execute,
5674     NULL,                       /* rule_premodify_actions */
5675     rule_modify_actions,
5676     set_frag_handling,
5677     packet_out,
5678     set_netflow,
5679     get_netflow_ids,
5680     set_sflow,
5681     set_ipfix,
5682     set_cfm,
5683     cfm_status_changed,
5684     get_cfm_status,
5685     set_lldp,
5686     get_lldp_status,
5687     set_aa,
5688     aa_mapping_set,
5689     aa_mapping_unset,
5690     aa_vlan_get_queued,
5691     aa_vlan_get_queue_size,
5692     set_bfd,
5693     bfd_status_changed,
5694     get_bfd_status,
5695     set_stp,
5696     get_stp_status,
5697     set_stp_port,
5698     get_stp_port_status,
5699     get_stp_port_stats,
5700     set_rstp,
5701     get_rstp_status,
5702     set_rstp_port,
5703     get_rstp_port_status,
5704     set_queues,
5705     bundle_set,
5706     bundle_remove,
5707     mirror_set__,
5708     mirror_get_stats__,
5709     set_flood_vlans,
5710     is_mirror_output_bundle,
5711     forward_bpdu_changed,
5712     set_mac_table_config,
5713     set_mcast_snooping,
5714     set_mcast_snooping_port,
5715     set_realdev,
5716     NULL,                       /* meter_get_features */
5717     NULL,                       /* meter_set */
5718     NULL,                       /* meter_get */
5719     NULL,                       /* meter_del */
5720     group_alloc,                /* group_alloc */
5721     group_construct,            /* group_construct */
5722     group_destruct,             /* group_destruct */
5723     group_dealloc,              /* group_dealloc */
5724     group_modify,               /* group_modify */
5725     group_get_stats,            /* group_get_stats */
5726     get_datapath_version,       /* get_datapath_version */
5727 };