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