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