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