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