ofproto-dpif: Added Per backer recirculation ID management
[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 = actions.data;
950     execute.actions_len = actions.size;
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                               key.data, key.size, 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, key.data, key.size, 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 = ofpacts->data;
1121     fm.ofpacts_len = ofpacts->size;
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 = pkt->l2;
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             learning_packet->private_p = port_void;
2423             list_push_back(&packets, &learning_packet->list_node);
2424         }
2425     }
2426     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2427
2428     error = n_packets = n_errors = 0;
2429     LIST_FOR_EACH (learning_packet, list_node, &packets) {
2430         int ret;
2431
2432         ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2433         if (ret) {
2434             error = ret;
2435             n_errors++;
2436         }
2437         n_packets++;
2438     }
2439     ofpbuf_list_delete(&packets);
2440
2441     if (n_errors) {
2442         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2443         VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2444                      "packets, last error was: %s",
2445                      bundle->name, n_errors, n_packets, ovs_strerror(error));
2446     } else {
2447         VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2448                  bundle->name, n_packets);
2449     }
2450 }
2451
2452 static void
2453 bundle_run(struct ofbundle *bundle)
2454 {
2455     if (bundle->lacp) {
2456         lacp_run(bundle->lacp, send_pdu_cb);
2457     }
2458     if (bundle->bond) {
2459         struct ofport_dpif *port;
2460
2461         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2462             bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2463         }
2464
2465         if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2466             bundle->ofproto->backer->need_revalidate = REV_BOND;
2467         }
2468
2469         if (bond_should_send_learning_packets(bundle->bond)) {
2470             bundle_send_learning_packets(bundle);
2471         }
2472     }
2473 }
2474
2475 static void
2476 bundle_wait(struct ofbundle *bundle)
2477 {
2478     if (bundle->lacp) {
2479         lacp_wait(bundle->lacp);
2480     }
2481     if (bundle->bond) {
2482         bond_wait(bundle->bond);
2483     }
2484 }
2485 \f
2486 /* Mirrors. */
2487
2488 static int
2489 mirror_set__(struct ofproto *ofproto_, void *aux,
2490              const struct ofproto_mirror_settings *s)
2491 {
2492     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2493     struct ofbundle **srcs, **dsts;
2494     int error;
2495     size_t i;
2496
2497     if (!s) {
2498         mirror_destroy(ofproto->mbridge, aux);
2499         return 0;
2500     }
2501
2502     srcs = xmalloc(s->n_srcs * sizeof *srcs);
2503     dsts = xmalloc(s->n_dsts * sizeof *dsts);
2504
2505     for (i = 0; i < s->n_srcs; i++) {
2506         srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2507     }
2508
2509     for (i = 0; i < s->n_dsts; i++) {
2510         dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2511     }
2512
2513     error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2514                        s->n_dsts, s->src_vlans,
2515                        bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2516     free(srcs);
2517     free(dsts);
2518     return error;
2519 }
2520
2521 static int
2522 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2523                    uint64_t *packets, uint64_t *bytes)
2524 {
2525     return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2526                             bytes);
2527 }
2528
2529 static int
2530 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2531 {
2532     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2533     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2534     if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2535         mac_learning_flush(ofproto->ml);
2536     }
2537     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2538     return 0;
2539 }
2540
2541 static bool
2542 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2543 {
2544     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2545     struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2546     return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2547 }
2548
2549 static void
2550 forward_bpdu_changed(struct ofproto *ofproto_)
2551 {
2552     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2553     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2554 }
2555
2556 static void
2557 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2558                      size_t max_entries)
2559 {
2560     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2561     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2562     mac_learning_set_idle_time(ofproto->ml, idle_time);
2563     mac_learning_set_max_entries(ofproto->ml, max_entries);
2564     ovs_rwlock_unlock(&ofproto->ml->rwlock);
2565 }
2566 \f
2567 /* Ports. */
2568
2569 static struct ofport_dpif *
2570 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2571 {
2572     struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2573     return ofport ? ofport_dpif_cast(ofport) : NULL;
2574 }
2575
2576 static void
2577 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2578                             struct ofproto_port *ofproto_port,
2579                             struct dpif_port *dpif_port)
2580 {
2581     ofproto_port->name = dpif_port->name;
2582     ofproto_port->type = dpif_port->type;
2583     ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2584 }
2585
2586 static void
2587 ofport_update_peer(struct ofport_dpif *ofport)
2588 {
2589     const struct ofproto_dpif *ofproto;
2590     struct dpif_backer *backer;
2591     char *peer_name;
2592
2593     if (!netdev_vport_is_patch(ofport->up.netdev)) {
2594         return;
2595     }
2596
2597     backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2598     backer->need_revalidate = REV_RECONFIGURE;
2599
2600     if (ofport->peer) {
2601         ofport->peer->peer = NULL;
2602         ofport->peer = NULL;
2603     }
2604
2605     peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2606     if (!peer_name) {
2607         return;
2608     }
2609
2610     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2611         struct ofport *peer_ofport;
2612         struct ofport_dpif *peer;
2613         char *peer_peer;
2614
2615         if (ofproto->backer != backer) {
2616             continue;
2617         }
2618
2619         peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2620         if (!peer_ofport) {
2621             continue;
2622         }
2623
2624         peer = ofport_dpif_cast(peer_ofport);
2625         peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2626         if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2627                                  peer_peer)) {
2628             ofport->peer = peer;
2629             ofport->peer->peer = ofport;
2630         }
2631         free(peer_peer);
2632
2633         break;
2634     }
2635     free(peer_name);
2636 }
2637
2638 static void
2639 port_run(struct ofport_dpif *ofport)
2640 {
2641     long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2642     bool carrier_changed = carrier_seq != ofport->carrier_seq;
2643     bool enable = netdev_get_carrier(ofport->up.netdev);
2644     bool cfm_enable = false;
2645     bool bfd_enable = false;
2646
2647     ofport->carrier_seq = carrier_seq;
2648
2649     if (ofport->cfm) {
2650         int cfm_opup = cfm_get_opup(ofport->cfm);
2651
2652         cfm_enable = !cfm_get_fault(ofport->cfm);
2653
2654         if (cfm_opup >= 0) {
2655             cfm_enable = cfm_enable && cfm_opup;
2656         }
2657     }
2658
2659     if (ofport->bfd) {
2660         bfd_enable = bfd_forwarding(ofport->bfd);
2661     }
2662
2663     if (ofport->bfd || ofport->cfm) {
2664         enable = enable && (cfm_enable || bfd_enable);
2665     }
2666
2667     if (ofport->bundle) {
2668         enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2669         if (carrier_changed) {
2670             lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2671         }
2672     }
2673
2674     if (ofport->may_enable != enable) {
2675         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2676         ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2677     }
2678
2679     ofport->may_enable = enable;
2680 }
2681
2682 static int
2683 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2684                    struct ofproto_port *ofproto_port)
2685 {
2686     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2687     struct dpif_port dpif_port;
2688     int error;
2689
2690     if (sset_contains(&ofproto->ghost_ports, devname)) {
2691         const char *type = netdev_get_type_from_name(devname);
2692
2693         /* We may be called before ofproto->up.port_by_name is populated with
2694          * the appropriate ofport.  For this reason, we must get the name and
2695          * type from the netdev layer directly. */
2696         if (type) {
2697             const struct ofport *ofport;
2698
2699             ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2700             ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2701             ofproto_port->name = xstrdup(devname);
2702             ofproto_port->type = xstrdup(type);
2703             return 0;
2704         }
2705         return ENODEV;
2706     }
2707
2708     if (!sset_contains(&ofproto->ports, devname)) {
2709         return ENODEV;
2710     }
2711     error = dpif_port_query_by_name(ofproto->backer->dpif,
2712                                     devname, &dpif_port);
2713     if (!error) {
2714         ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2715     }
2716     return error;
2717 }
2718
2719 static int
2720 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2721 {
2722     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2723     const char *devname = netdev_get_name(netdev);
2724     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2725     const char *dp_port_name;
2726
2727     if (netdev_vport_is_patch(netdev)) {
2728         sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2729         return 0;
2730     }
2731
2732     dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2733     if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2734         odp_port_t port_no = ODPP_NONE;
2735         int error;
2736
2737         error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2738         if (error) {
2739             return error;
2740         }
2741         if (netdev_get_tunnel_config(netdev)) {
2742             simap_put(&ofproto->backer->tnl_backers,
2743                       dp_port_name, odp_to_u32(port_no));
2744         }
2745     }
2746
2747     if (netdev_get_tunnel_config(netdev)) {
2748         sset_add(&ofproto->ghost_ports, devname);
2749     } else {
2750         sset_add(&ofproto->ports, devname);
2751     }
2752     return 0;
2753 }
2754
2755 static int
2756 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2757 {
2758     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2759     struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2760     int error = 0;
2761
2762     if (!ofport) {
2763         return 0;
2764     }
2765
2766     sset_find_and_delete(&ofproto->ghost_ports,
2767                          netdev_get_name(ofport->up.netdev));
2768     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2769     if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2770         error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2771         if (!error) {
2772             /* The caller is going to close ofport->up.netdev.  If this is a
2773              * bonded port, then the bond is using that netdev, so remove it
2774              * from the bond.  The client will need to reconfigure everything
2775              * after deleting ports, so then the slave will get re-added. */
2776             bundle_remove(&ofport->up);
2777         }
2778     }
2779     return error;
2780 }
2781
2782 static int
2783 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2784 {
2785     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2786     int error;
2787
2788     error = netdev_get_stats(ofport->up.netdev, stats);
2789
2790     if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2791         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2792
2793         ovs_mutex_lock(&ofproto->stats_mutex);
2794         /* ofproto->stats.tx_packets represents packets that we created
2795          * internally and sent to some port (e.g. packets sent with
2796          * ofproto_dpif_send_packet()).  Account for them as if they had
2797          * come from OFPP_LOCAL and got forwarded. */
2798
2799         if (stats->rx_packets != UINT64_MAX) {
2800             stats->rx_packets += ofproto->stats.tx_packets;
2801         }
2802
2803         if (stats->rx_bytes != UINT64_MAX) {
2804             stats->rx_bytes += ofproto->stats.tx_bytes;
2805         }
2806
2807         /* ofproto->stats.rx_packets represents packets that were received on
2808          * some port and we processed internally and dropped (e.g. STP).
2809          * Account for them as if they had been forwarded to OFPP_LOCAL. */
2810
2811         if (stats->tx_packets != UINT64_MAX) {
2812             stats->tx_packets += ofproto->stats.rx_packets;
2813         }
2814
2815         if (stats->tx_bytes != UINT64_MAX) {
2816             stats->tx_bytes += ofproto->stats.rx_bytes;
2817         }
2818         ovs_mutex_unlock(&ofproto->stats_mutex);
2819     }
2820
2821     return error;
2822 }
2823
2824 struct port_dump_state {
2825     uint32_t bucket;
2826     uint32_t offset;
2827     bool ghost;
2828
2829     struct ofproto_port port;
2830     bool has_port;
2831 };
2832
2833 static int
2834 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2835 {
2836     *statep = xzalloc(sizeof(struct port_dump_state));
2837     return 0;
2838 }
2839
2840 static int
2841 port_dump_next(const struct ofproto *ofproto_, void *state_,
2842                struct ofproto_port *port)
2843 {
2844     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2845     struct port_dump_state *state = state_;
2846     const struct sset *sset;
2847     struct sset_node *node;
2848
2849     if (state->has_port) {
2850         ofproto_port_destroy(&state->port);
2851         state->has_port = false;
2852     }
2853     sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2854     while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2855         int error;
2856
2857         error = port_query_by_name(ofproto_, node->name, &state->port);
2858         if (!error) {
2859             *port = state->port;
2860             state->has_port = true;
2861             return 0;
2862         } else if (error != ENODEV) {
2863             return error;
2864         }
2865     }
2866
2867     if (!state->ghost) {
2868         state->ghost = true;
2869         state->bucket = 0;
2870         state->offset = 0;
2871         return port_dump_next(ofproto_, state_, port);
2872     }
2873
2874     return EOF;
2875 }
2876
2877 static int
2878 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2879 {
2880     struct port_dump_state *state = state_;
2881
2882     if (state->has_port) {
2883         ofproto_port_destroy(&state->port);
2884     }
2885     free(state);
2886     return 0;
2887 }
2888
2889 static int
2890 port_poll(const struct ofproto *ofproto_, char **devnamep)
2891 {
2892     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2893
2894     if (ofproto->port_poll_errno) {
2895         int error = ofproto->port_poll_errno;
2896         ofproto->port_poll_errno = 0;
2897         return error;
2898     }
2899
2900     if (sset_is_empty(&ofproto->port_poll_set)) {
2901         return EAGAIN;
2902     }
2903
2904     *devnamep = sset_pop(&ofproto->port_poll_set);
2905     return 0;
2906 }
2907
2908 static void
2909 port_poll_wait(const struct ofproto *ofproto_)
2910 {
2911     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2912     dpif_port_poll_wait(ofproto->backer->dpif);
2913 }
2914
2915 static int
2916 port_is_lacp_current(const struct ofport *ofport_)
2917 {
2918     const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2919     return (ofport->bundle && ofport->bundle->lacp
2920             ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2921             : -1);
2922 }
2923 \f
2924 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2925  * then delete it entirely. */
2926 static void
2927 rule_expire(struct rule_dpif *rule)
2928     OVS_REQUIRES(ofproto_mutex)
2929 {
2930     uint16_t hard_timeout, idle_timeout;
2931     long long int now = time_msec();
2932     int reason = -1;
2933
2934     ovs_assert(!rule->up.pending);
2935
2936     hard_timeout = rule->up.hard_timeout;
2937     idle_timeout = rule->up.idle_timeout;
2938
2939     /* Has 'rule' expired? */
2940     if (hard_timeout) {
2941         long long int modified;
2942
2943         ovs_mutex_lock(&rule->up.mutex);
2944         modified = rule->up.modified;
2945         ovs_mutex_unlock(&rule->up.mutex);
2946
2947         if (now > modified + hard_timeout * 1000) {
2948             reason = OFPRR_HARD_TIMEOUT;
2949         }
2950     }
2951
2952     if (reason < 0 && idle_timeout) {
2953         long long int used;
2954
2955         ovs_mutex_lock(&rule->stats_mutex);
2956         used = rule->stats.used;
2957         ovs_mutex_unlock(&rule->stats_mutex);
2958
2959         if (now > used + idle_timeout * 1000) {
2960             reason = OFPRR_IDLE_TIMEOUT;
2961         }
2962     }
2963
2964     if (reason >= 0) {
2965         COVERAGE_INC(ofproto_dpif_expired);
2966         ofproto_rule_expire(&rule->up, reason);
2967     }
2968 }
2969
2970 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2971  * 'flow' must reflect the data in 'packet'. */
2972 int
2973 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2974                              const struct flow *flow,
2975                              struct rule_dpif *rule,
2976                              const struct ofpact *ofpacts, size_t ofpacts_len,
2977                              struct ofpbuf *packet)
2978 {
2979     struct dpif_flow_stats stats;
2980     struct xlate_out xout;
2981     struct xlate_in xin;
2982     ofp_port_t in_port;
2983     struct dpif_execute execute;
2984     int error;
2985
2986     ovs_assert((rule != NULL) != (ofpacts != NULL));
2987
2988     dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2989     if (rule) {
2990         rule_dpif_credit_stats(rule, &stats);
2991     }
2992
2993     xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2994     xin.ofpacts = ofpacts;
2995     xin.ofpacts_len = ofpacts_len;
2996     xin.resubmit_stats = &stats;
2997     xlate_actions(&xin, &xout);
2998
2999     in_port = flow->in_port.ofp_port;
3000     if (in_port == OFPP_NONE) {
3001         in_port = OFPP_LOCAL;
3002     }
3003     execute.actions = xout.odp_actions.data;
3004     execute.actions_len = xout.odp_actions.size;
3005     execute.packet = packet;
3006     execute.md.tunnel = flow->tunnel;
3007     execute.md.skb_priority = flow->skb_priority;
3008     execute.md.pkt_mark = flow->pkt_mark;
3009     execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3010     execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3011
3012     error = dpif_execute(ofproto->backer->dpif, &execute);
3013
3014     xlate_out_uninit(&xout);
3015
3016     return error;
3017 }
3018
3019 void
3020 rule_dpif_credit_stats(struct rule_dpif *rule,
3021                        const struct dpif_flow_stats *stats)
3022 {
3023     ovs_mutex_lock(&rule->stats_mutex);
3024     rule->stats.n_packets += stats->n_packets;
3025     rule->stats.n_bytes += stats->n_bytes;
3026     rule->stats.used = MAX(rule->stats.used, stats->used);
3027     ovs_mutex_unlock(&rule->stats_mutex);
3028 }
3029
3030 bool
3031 rule_dpif_is_fail_open(const struct rule_dpif *rule)
3032 {
3033     return is_fail_open_rule(&rule->up);
3034 }
3035
3036 bool
3037 rule_dpif_is_table_miss(const struct rule_dpif *rule)
3038 {
3039     return rule_is_table_miss(&rule->up);
3040 }
3041
3042 bool
3043 rule_dpif_is_internal(const struct rule_dpif *rule)
3044 {
3045     return rule_is_internal(&rule->up);
3046 }
3047
3048 ovs_be64
3049 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3050     OVS_REQUIRES(rule->up.mutex)
3051 {
3052     return rule->up.flow_cookie;
3053 }
3054
3055 void
3056 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3057                      uint16_t hard_timeout)
3058 {
3059     ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3060 }
3061
3062 /* Returns 'rule''s actions.  The caller owns a reference on the returned
3063  * actions and must eventually release it (with rule_actions_unref()) to avoid
3064  * a memory leak. */
3065 struct rule_actions *
3066 rule_dpif_get_actions(const struct rule_dpif *rule)
3067 {
3068     return rule_get_actions(&rule->up);
3069 }
3070
3071 /* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3072  * If 'wc' is non-null, sets the fields that were relevant as part of
3073  * the lookup. Returns the table_id where a match or miss occurred.
3074  *
3075  * The return value will be zero unless there was a miss and
3076  * OFPTC_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3077  * where misses occur. */
3078 uint8_t
3079 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3080                  struct flow_wildcards *wc, struct rule_dpif **rule)
3081 {
3082     enum rule_dpif_lookup_verdict verdict;
3083     enum ofputil_port_config config = 0;
3084     uint8_t table_id = 0;
3085
3086     verdict = rule_dpif_lookup_from_table(ofproto, flow, wc, true,
3087                                           &table_id, rule);
3088
3089     switch (verdict) {
3090     case RULE_DPIF_LOOKUP_VERDICT_MATCH:
3091         return table_id;
3092     case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER: {
3093         struct ofport_dpif *port;
3094
3095         port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3096         if (!port) {
3097             VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3098                          flow->in_port.ofp_port);
3099         }
3100         config = port ? port->up.pp.config : 0;
3101         break;
3102     }
3103     case RULE_DPIF_LOOKUP_VERDICT_DROP:
3104         config = OFPUTIL_PC_NO_PACKET_IN;
3105         break;
3106     default:
3107         OVS_NOT_REACHED();
3108     }
3109
3110     choose_miss_rule(config, ofproto->miss_rule,
3111                      ofproto->no_packet_in_rule, rule);
3112     return table_id;
3113 }
3114
3115 static struct rule_dpif *
3116 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3117                           const struct flow *flow, struct flow_wildcards *wc)
3118 {
3119     struct classifier *cls = &ofproto->up.tables[table_id].cls;
3120     const struct cls_rule *cls_rule;
3121     struct rule_dpif *rule;
3122
3123     fat_rwlock_rdlock(&cls->rwlock);
3124     if (ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3125         if (wc) {
3126             memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3127             if (is_ip_any(flow)) {
3128                 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3129             }
3130         }
3131
3132         if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3133             if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3134                 /* We must pretend that transport ports are unavailable. */
3135                 struct flow ofpc_normal_flow = *flow;
3136                 ofpc_normal_flow.tp_src = htons(0);
3137                 ofpc_normal_flow.tp_dst = htons(0);
3138                 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3139             } else {
3140                 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM). */
3141                 cls_rule = &ofproto->drop_frags_rule->up.cr;
3142             }
3143         } else {
3144             cls_rule = classifier_lookup(cls, flow, wc);
3145         }
3146     } else {
3147         cls_rule = classifier_lookup(cls, flow, wc);
3148     }
3149
3150     rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3151     rule_dpif_ref(rule);
3152     fat_rwlock_unlock(&cls->rwlock);
3153
3154     return rule;
3155 }
3156
3157 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3158  * Stores the rule that was found in '*rule', or NULL if none was found.
3159  * Updates 'wc', if nonnull, to reflect the fields that were used during the
3160  * lookup.
3161  *
3162  * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3163  * if none is found then the table miss configuration for that table is
3164  * honored, which can result in additional lookups in other OpenFlow tables.
3165  * In this case the function updates '*table_id' to reflect the final OpenFlow
3166  * table that was searched.
3167  *
3168  * If 'honor_table_miss' is false, then only one table lookup occurs, in
3169  * '*table_id'.
3170  *
3171  * Returns:
3172  *
3173  *    - RULE_DPIF_LOOKUP_VERDICT_MATCH if a rule (in '*rule') was found.
3174  *
3175  *    - RULE_DPIF_LOOKUP_VERDICT_DROP if no rule was found and a table miss
3176  *      configuration specified that the packet should be dropped in this
3177  *      case.  (This occurs only if 'honor_table_miss' is true, because only in
3178  *      this case does the table miss configuration matter.)
3179  *
3180  *    - RULE_DPIF_LOOKUP_VERDICT_CONTROLLER if no rule was found otherwise. */
3181 enum rule_dpif_lookup_verdict
3182 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3183                             const struct flow *flow,
3184                             struct flow_wildcards *wc,
3185                             bool honor_table_miss,
3186                             uint8_t *table_id, struct rule_dpif **rule)
3187 {
3188     uint8_t next_id;
3189
3190     for (next_id = *table_id;
3191          next_id < ofproto->up.n_tables;
3192          next_id++, next_id += (next_id == TBL_INTERNAL))
3193     {
3194         *table_id = next_id;
3195         *rule = rule_dpif_lookup_in_table(ofproto, *table_id, flow, wc);
3196         if (*rule) {
3197             return RULE_DPIF_LOOKUP_VERDICT_MATCH;
3198         } else if (!honor_table_miss) {
3199             return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3200         } else {
3201             switch (table_get_config(&ofproto->up, *table_id)
3202                     & OFPTC11_TABLE_MISS_MASK) {
3203             case OFPTC11_TABLE_MISS_CONTINUE:
3204                 break;
3205
3206             case OFPTC11_TABLE_MISS_CONTROLLER:
3207                 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3208
3209             case OFPTC11_TABLE_MISS_DROP:
3210                 return RULE_DPIF_LOOKUP_VERDICT_DROP;
3211             }
3212         }
3213     }
3214
3215     return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3216 }
3217
3218 /* Given a port configuration (specified as zero if there's no port), chooses
3219  * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3220  * flow table miss. */
3221 void
3222 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3223                  struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
3224 {
3225     *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3226     rule_dpif_ref(*rule);
3227 }
3228
3229 void
3230 rule_dpif_ref(struct rule_dpif *rule)
3231 {
3232     if (rule) {
3233         ofproto_rule_ref(&rule->up);
3234     }
3235 }
3236
3237 void
3238 rule_dpif_unref(struct rule_dpif *rule)
3239 {
3240     if (rule) {
3241         ofproto_rule_unref(&rule->up);
3242     }
3243 }
3244
3245 static void
3246 complete_operation(struct rule_dpif *rule)
3247     OVS_REQUIRES(ofproto_mutex)
3248 {
3249     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3250
3251     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3252     ofoperation_complete(rule->up.pending, 0);
3253 }
3254
3255 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3256 {
3257     return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3258 }
3259
3260 static struct rule *
3261 rule_alloc(void)
3262 {
3263     struct rule_dpif *rule = xmalloc(sizeof *rule);
3264     return &rule->up;
3265 }
3266
3267 static void
3268 rule_dealloc(struct rule *rule_)
3269 {
3270     struct rule_dpif *rule = rule_dpif_cast(rule_);
3271     free(rule);
3272 }
3273
3274 static enum ofperr
3275 rule_construct(struct rule *rule_)
3276     OVS_NO_THREAD_SAFETY_ANALYSIS
3277 {
3278     struct rule_dpif *rule = rule_dpif_cast(rule_);
3279     ovs_mutex_init_adaptive(&rule->stats_mutex);
3280     rule->stats.n_packets = 0;
3281     rule->stats.n_bytes = 0;
3282     rule->stats.used = rule->up.modified;
3283     return 0;
3284 }
3285
3286 static void
3287 rule_insert(struct rule *rule_)
3288     OVS_REQUIRES(ofproto_mutex)
3289 {
3290     struct rule_dpif *rule = rule_dpif_cast(rule_);
3291     complete_operation(rule);
3292 }
3293
3294 static void
3295 rule_delete(struct rule *rule_)
3296     OVS_REQUIRES(ofproto_mutex)
3297 {
3298     struct rule_dpif *rule = rule_dpif_cast(rule_);
3299     complete_operation(rule);
3300 }
3301
3302 static void
3303 rule_destruct(struct rule *rule_)
3304 {
3305     struct rule_dpif *rule = rule_dpif_cast(rule_);
3306     ovs_mutex_destroy(&rule->stats_mutex);
3307 }
3308
3309 static void
3310 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3311                long long int *used)
3312 {
3313     struct rule_dpif *rule = rule_dpif_cast(rule_);
3314
3315     ovs_mutex_lock(&rule->stats_mutex);
3316     *packets = rule->stats.n_packets;
3317     *bytes = rule->stats.n_bytes;
3318     *used = rule->stats.used;
3319     ovs_mutex_unlock(&rule->stats_mutex);
3320 }
3321
3322 static void
3323 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3324                   struct ofpbuf *packet)
3325 {
3326     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3327
3328     ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3329 }
3330
3331 static enum ofperr
3332 rule_execute(struct rule *rule, const struct flow *flow,
3333              struct ofpbuf *packet)
3334 {
3335     rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3336     ofpbuf_delete(packet);
3337     return 0;
3338 }
3339
3340 static void
3341 rule_modify_actions(struct rule *rule_, bool reset_counters)
3342     OVS_REQUIRES(ofproto_mutex)
3343 {
3344     struct rule_dpif *rule = rule_dpif_cast(rule_);
3345
3346     if (reset_counters) {
3347         ovs_mutex_lock(&rule->stats_mutex);
3348         rule->stats.n_packets = 0;
3349         rule->stats.n_bytes = 0;
3350         ovs_mutex_unlock(&rule->stats_mutex);
3351     }
3352
3353     complete_operation(rule);
3354 }
3355
3356 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3357 {
3358     return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3359 }
3360
3361 static struct ofgroup *
3362 group_alloc(void)
3363 {
3364     struct group_dpif *group = xzalloc(sizeof *group);
3365     return &group->up;
3366 }
3367
3368 static void
3369 group_dealloc(struct ofgroup *group_)
3370 {
3371     struct group_dpif *group = group_dpif_cast(group_);
3372     free(group);
3373 }
3374
3375 static void
3376 group_construct_stats(struct group_dpif *group)
3377     OVS_REQUIRES(group->stats_mutex)
3378 {
3379     group->packet_count = 0;
3380     group->byte_count = 0;
3381     if (!group->bucket_stats) {
3382         group->bucket_stats = xcalloc(group->up.n_buckets,
3383                                       sizeof *group->bucket_stats);
3384     } else {
3385         memset(group->bucket_stats, 0, group->up.n_buckets *
3386                sizeof *group->bucket_stats);
3387     }
3388 }
3389
3390 static enum ofperr
3391 group_construct(struct ofgroup *group_)
3392 {
3393     struct group_dpif *group = group_dpif_cast(group_);
3394     const struct ofputil_bucket *bucket;
3395
3396     /* Prevent group chaining because our locking structure makes it hard to
3397      * implement deadlock-free.  (See xlate_group_resource_check().) */
3398     LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3399         const struct ofpact *a;
3400
3401         OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
3402             if (a->type == OFPACT_GROUP) {
3403                 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
3404             }
3405         }
3406     }
3407
3408     ovs_mutex_init_adaptive(&group->stats_mutex);
3409     ovs_mutex_lock(&group->stats_mutex);
3410     group_construct_stats(group);
3411     ovs_mutex_unlock(&group->stats_mutex);
3412     return 0;
3413 }
3414
3415 static void
3416 group_destruct__(struct group_dpif *group)
3417     OVS_REQUIRES(group->stats_mutex)
3418 {
3419     free(group->bucket_stats);
3420     group->bucket_stats = NULL;
3421 }
3422
3423 static void
3424 group_destruct(struct ofgroup *group_)
3425 {
3426     struct group_dpif *group = group_dpif_cast(group_);
3427     ovs_mutex_lock(&group->stats_mutex);
3428     group_destruct__(group);
3429     ovs_mutex_unlock(&group->stats_mutex);
3430     ovs_mutex_destroy(&group->stats_mutex);
3431 }
3432
3433 static enum ofperr
3434 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3435 {
3436     struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
3437     struct group_dpif *group = group_dpif_cast(group_);
3438     struct group_dpif *victim = group_dpif_cast(victim_);
3439
3440     ovs_mutex_lock(&group->stats_mutex);
3441     if (victim->up.n_buckets < group->up.n_buckets) {
3442         group_destruct__(group);
3443     }
3444     group_construct_stats(group);
3445     ovs_mutex_unlock(&group->stats_mutex);
3446
3447     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3448
3449     return 0;
3450 }
3451
3452 static enum ofperr
3453 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3454 {
3455     struct group_dpif *group = group_dpif_cast(group_);
3456
3457     ovs_mutex_lock(&group->stats_mutex);
3458     ogs->packet_count = group->packet_count;
3459     ogs->byte_count = group->byte_count;
3460     memcpy(ogs->bucket_stats, group->bucket_stats,
3461            group->up.n_buckets * sizeof *group->bucket_stats);
3462     ovs_mutex_unlock(&group->stats_mutex);
3463
3464     return 0;
3465 }
3466
3467 bool
3468 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3469                   struct group_dpif **group)
3470     OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3471 {
3472     struct ofgroup *ofgroup;
3473     bool found;
3474
3475     *group = NULL;
3476     found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3477     *group = found ?  group_dpif_cast(ofgroup) : NULL;
3478
3479     return found;
3480 }
3481
3482 void
3483 group_dpif_release(struct group_dpif *group)
3484     OVS_RELEASES(group->up.rwlock)
3485 {
3486     ofproto_group_release(&group->up);
3487 }
3488
3489 void
3490 group_dpif_get_buckets(const struct group_dpif *group,
3491                        const struct list **buckets)
3492 {
3493     *buckets = &group->up.buckets;
3494 }
3495
3496 enum ofp11_group_type
3497 group_dpif_get_type(const struct group_dpif *group)
3498 {
3499     return group->up.type;
3500 }
3501 \f
3502 /* Sends 'packet' out 'ofport'.
3503  * May modify 'packet'.
3504  * Returns 0 if successful, otherwise a positive errno value. */
3505 int
3506 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3507 {
3508     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3509     int error;
3510
3511     error = xlate_send_packet(ofport, packet);
3512
3513     ovs_mutex_lock(&ofproto->stats_mutex);
3514     ofproto->stats.tx_packets++;
3515     ofproto->stats.tx_bytes += packet->size;
3516     ovs_mutex_unlock(&ofproto->stats_mutex);
3517     return error;
3518 }
3519 \f
3520 static bool
3521 set_frag_handling(struct ofproto *ofproto_,
3522                   enum ofp_config_flags frag_handling)
3523 {
3524     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3525     if (frag_handling != OFPC_FRAG_REASM) {
3526         ofproto->backer->need_revalidate = REV_RECONFIGURE;
3527         return true;
3528     } else {
3529         return false;
3530     }
3531 }
3532
3533 static enum ofperr
3534 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3535            const struct flow *flow,
3536            const struct ofpact *ofpacts, size_t ofpacts_len)
3537 {
3538     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3539
3540     ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3541                                  ofpacts_len, packet);
3542     return 0;
3543 }
3544 \f
3545 /* NetFlow. */
3546
3547 static int
3548 set_netflow(struct ofproto *ofproto_,
3549             const struct netflow_options *netflow_options)
3550 {
3551     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3552
3553     if (netflow_options) {
3554         if (!ofproto->netflow) {
3555             ofproto->netflow = netflow_create();
3556             ofproto->backer->need_revalidate = REV_RECONFIGURE;
3557         }
3558         return netflow_set_options(ofproto->netflow, netflow_options);
3559     } else if (ofproto->netflow) {
3560         ofproto->backer->need_revalidate = REV_RECONFIGURE;
3561         netflow_unref(ofproto->netflow);
3562         ofproto->netflow = NULL;
3563     }
3564
3565     return 0;
3566 }
3567
3568 static void
3569 get_netflow_ids(const struct ofproto *ofproto_,
3570                 uint8_t *engine_type, uint8_t *engine_id)
3571 {
3572     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3573
3574     dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3575 }
3576 \f
3577 static struct ofproto_dpif *
3578 ofproto_dpif_lookup(const char *name)
3579 {
3580     struct ofproto_dpif *ofproto;
3581
3582     HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3583                              hash_string(name, 0), &all_ofproto_dpifs) {
3584         if (!strcmp(ofproto->up.name, name)) {
3585             return ofproto;
3586         }
3587     }
3588     return NULL;
3589 }
3590
3591 static void
3592 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3593                           const char *argv[], void *aux OVS_UNUSED)
3594 {
3595     struct ofproto_dpif *ofproto;
3596
3597     if (argc > 1) {
3598         ofproto = ofproto_dpif_lookup(argv[1]);
3599         if (!ofproto) {
3600             unixctl_command_reply_error(conn, "no such bridge");
3601             return;
3602         }
3603         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3604         mac_learning_flush(ofproto->ml);
3605         ovs_rwlock_unlock(&ofproto->ml->rwlock);
3606     } else {
3607         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3608             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3609             mac_learning_flush(ofproto->ml);
3610             ovs_rwlock_unlock(&ofproto->ml->rwlock);
3611         }
3612     }
3613
3614     unixctl_command_reply(conn, "table successfully flushed");
3615 }
3616
3617 static struct ofport_dpif *
3618 ofbundle_get_a_port(const struct ofbundle *bundle)
3619 {
3620     return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3621                         bundle_node);
3622 }
3623
3624 static void
3625 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3626                          const char *argv[], void *aux OVS_UNUSED)
3627 {
3628     struct ds ds = DS_EMPTY_INITIALIZER;
3629     const struct ofproto_dpif *ofproto;
3630     const struct mac_entry *e;
3631
3632     ofproto = ofproto_dpif_lookup(argv[1]);
3633     if (!ofproto) {
3634         unixctl_command_reply_error(conn, "no such bridge");
3635         return;
3636     }
3637
3638     ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
3639     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3640     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3641         struct ofbundle *bundle = e->port.p;
3642         char name[OFP_MAX_PORT_NAME_LEN];
3643
3644         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3645                                name, sizeof name);
3646         ds_put_format(&ds, "%5s  %4d  "ETH_ADDR_FMT"  %3d\n",
3647                       name, e->vlan, ETH_ADDR_ARGS(e->mac),
3648                       mac_entry_age(ofproto->ml, e));
3649     }
3650     ovs_rwlock_unlock(&ofproto->ml->rwlock);
3651     unixctl_command_reply(conn, ds_cstr(&ds));
3652     ds_destroy(&ds);
3653 }
3654
3655 struct trace_ctx {
3656     struct xlate_out xout;
3657     struct xlate_in xin;
3658     struct flow flow;
3659     struct flow_wildcards wc;
3660     struct ds *result;
3661 };
3662
3663 static void
3664 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3665 {
3666     struct rule_actions *actions;
3667     ovs_be64 cookie;
3668
3669     ds_put_char_multiple(result, '\t', level);
3670     if (!rule) {
3671         ds_put_cstr(result, "No match\n");
3672         return;
3673     }
3674
3675     ovs_mutex_lock(&rule->up.mutex);
3676     cookie = rule->up.flow_cookie;
3677     ovs_mutex_unlock(&rule->up.mutex);
3678
3679     ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3680                   rule ? rule->up.table_id : 0, ntohll(cookie));
3681     cls_rule_format(&rule->up.cr, result);
3682     ds_put_char(result, '\n');
3683
3684     actions = rule_dpif_get_actions(rule);
3685
3686     ds_put_char_multiple(result, '\t', level);
3687     ds_put_cstr(result, "OpenFlow actions=");
3688     ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3689     ds_put_char(result, '\n');
3690 }
3691
3692 static void
3693 trace_format_flow(struct ds *result, int level, const char *title,
3694                   struct trace_ctx *trace)
3695 {
3696     ds_put_char_multiple(result, '\t', level);
3697     ds_put_format(result, "%s: ", title);
3698     if (flow_equal(&trace->xin.flow, &trace->flow)) {
3699         ds_put_cstr(result, "unchanged");
3700     } else {
3701         flow_format(result, &trace->xin.flow);
3702         trace->flow = trace->xin.flow;
3703     }
3704     ds_put_char(result, '\n');
3705 }
3706
3707 static void
3708 trace_format_regs(struct ds *result, int level, const char *title,
3709                   struct trace_ctx *trace)
3710 {
3711     size_t i;
3712
3713     ds_put_char_multiple(result, '\t', level);
3714     ds_put_format(result, "%s:", title);
3715     for (i = 0; i < FLOW_N_REGS; i++) {
3716         ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3717     }
3718     ds_put_char(result, '\n');
3719 }
3720
3721 static void
3722 trace_format_odp(struct ds *result, int level, const char *title,
3723                  struct trace_ctx *trace)
3724 {
3725     struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3726
3727     ds_put_char_multiple(result, '\t', level);
3728     ds_put_format(result, "%s: ", title);
3729     format_odp_actions(result, odp_actions->data, odp_actions->size);
3730     ds_put_char(result, '\n');
3731 }
3732
3733 static void
3734 trace_format_megaflow(struct ds *result, int level, const char *title,
3735                       struct trace_ctx *trace)
3736 {
3737     struct match match;
3738
3739     ds_put_char_multiple(result, '\t', level);
3740     ds_put_format(result, "%s: ", title);
3741     flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3742     match_init(&match, &trace->flow, &trace->wc);
3743     match_format(&match, result, OFP_DEFAULT_PRIORITY);
3744     ds_put_char(result, '\n');
3745 }
3746
3747 static void
3748 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3749 {
3750     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3751     struct ds *result = trace->result;
3752
3753     ds_put_char(result, '\n');
3754     trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3755     trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3756     trace_format_odp(result,  recurse + 1, "Resubmitted  odp", trace);
3757     trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3758     trace_format_rule(result, recurse + 1, rule);
3759 }
3760
3761 static void
3762 trace_report(struct xlate_in *xin, const char *s, int recurse)
3763 {
3764     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3765     struct ds *result = trace->result;
3766
3767     ds_put_char_multiple(result, '\t', recurse);
3768     ds_put_cstr(result, s);
3769     ds_put_char(result, '\n');
3770 }
3771
3772 /* Parses the 'argc' elements of 'argv', ignoring argv[0].  The following
3773  * forms are supported:
3774  *
3775  *     - [dpname] odp_flow [-generate | packet]
3776  *     - bridge br_flow [-generate | packet]
3777  *
3778  * On success, initializes '*ofprotop' and 'flow' and returns NULL.  On failure
3779  * returns a nonnull malloced error message. */
3780 static char * WARN_UNUSED_RESULT
3781 parse_flow_and_packet(int argc, const char *argv[],
3782                       struct ofproto_dpif **ofprotop, struct flow *flow,
3783                       struct ofpbuf **packetp)
3784 {
3785     const struct dpif_backer *backer = NULL;
3786     const char *error = NULL;
3787     char *m_err = NULL;
3788     struct simap port_names = SIMAP_INITIALIZER(&port_names);
3789     struct ofpbuf *packet;
3790     struct ofpbuf odp_key;
3791     struct ofpbuf odp_mask;
3792
3793     ofpbuf_init(&odp_key, 0);
3794     ofpbuf_init(&odp_mask, 0);
3795
3796     /* Handle "-generate" or a hex string as the last argument. */
3797     if (!strcmp(argv[argc - 1], "-generate")) {
3798         packet = ofpbuf_new(0);
3799         argc--;
3800     } else {
3801         error = eth_from_hex(argv[argc - 1], &packet);
3802         if (!error) {
3803             argc--;
3804         } else if (argc == 4) {
3805             /* The 3-argument form must end in "-generate' or a hex string. */
3806             goto exit;
3807         }
3808         error = NULL;
3809     }
3810
3811     /* odp_flow can have its in_port specified as a name instead of port no.
3812      * We do not yet know whether a given flow is a odp_flow or a br_flow.
3813      * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3814      * we need to create a simap of name to port no. */
3815     if (argc == 3) {
3816         const char *dp_type;
3817         if (!strncmp(argv[1], "ovs-", 4)) {
3818             dp_type = argv[1] + 4;
3819         } else {
3820             dp_type = argv[1];
3821         }
3822         backer = shash_find_data(&all_dpif_backers, dp_type);
3823     } else if (argc == 2) {
3824         struct shash_node *node;
3825         if (shash_count(&all_dpif_backers) == 1) {
3826             node = shash_first(&all_dpif_backers);
3827             backer = node->data;
3828         }
3829     } else {
3830         error = "Syntax error";
3831         goto exit;
3832     }
3833     if (backer && backer->dpif) {
3834         struct dpif_port dpif_port;
3835         struct dpif_port_dump port_dump;
3836         DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3837             simap_put(&port_names, dpif_port.name,
3838                       odp_to_u32(dpif_port.port_no));
3839         }
3840     }
3841
3842     /* Parse the flow and determine whether a datapath or
3843      * bridge is specified. If function odp_flow_key_from_string()
3844      * returns 0, the flow is a odp_flow. If function
3845      * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
3846     if (!odp_flow_from_string(argv[argc - 1], &port_names,
3847                               &odp_key, &odp_mask)) {
3848         if (!backer) {
3849             error = "Cannot find the datapath";
3850             goto exit;
3851         }
3852
3853         if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
3854                           ofprotop, NULL, NULL, NULL, NULL)) {
3855             error = "Invalid datapath flow";
3856             goto exit;
3857         }
3858     } else {
3859         char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
3860
3861         if (err) {
3862             m_err = xasprintf("Bad flow syntax: %s", err);
3863             free(err);
3864             goto exit;
3865         } else {
3866             if (argc != 3) {
3867                 error = "Must specify bridge name";
3868                 goto exit;
3869             }
3870
3871             *ofprotop = ofproto_dpif_lookup(argv[1]);
3872             if (!*ofprotop) {
3873                 error = "Unknown bridge name";
3874                 goto exit;
3875             }
3876         }
3877     }
3878
3879     /* Generate a packet, if requested. */
3880     if (packet) {
3881         if (!packet->size) {
3882             flow_compose(packet, flow);
3883         } else {
3884             struct pkt_metadata md = pkt_metadata_from_flow(flow);
3885
3886             /* Use the metadata from the flow and the packet argument
3887              * to reconstruct the flow. */
3888             flow_extract(packet, &md, flow);
3889         }
3890     }
3891
3892 exit:
3893     if (error && !m_err) {
3894         m_err = xstrdup(error);
3895     }
3896     if (m_err) {
3897         ofpbuf_delete(packet);
3898         packet = NULL;
3899     }
3900     *packetp = packet;
3901     ofpbuf_uninit(&odp_key);
3902     ofpbuf_uninit(&odp_mask);
3903     simap_destroy(&port_names);
3904     return m_err;
3905 }
3906
3907 static void
3908 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3909                       void *aux OVS_UNUSED)
3910 {
3911     struct ofproto_dpif *ofproto;
3912     struct ofpbuf *packet;
3913     char *error;
3914     struct flow flow;
3915
3916     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3917     if (!error) {
3918         struct ds result;
3919
3920         ds_init(&result);
3921         ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
3922         unixctl_command_reply(conn, ds_cstr(&result));
3923         ds_destroy(&result);
3924         ofpbuf_delete(packet);
3925     } else {
3926         unixctl_command_reply_error(conn, error);
3927         free(error);
3928     }
3929 }
3930
3931 static void
3932 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3933                               const char *argv[], void *aux OVS_UNUSED)
3934 {
3935     enum ofputil_protocol usable_protocols;
3936     struct ofproto_dpif *ofproto;
3937     bool enforce_consistency;
3938     struct ofpbuf ofpacts;
3939     struct ofpbuf *packet;
3940     struct ds result;
3941     struct flow flow;
3942     uint16_t in_port;
3943
3944     /* Three kinds of error return values! */
3945     enum ofperr retval;
3946     char *error;
3947
3948     packet = NULL;
3949     ds_init(&result);
3950     ofpbuf_init(&ofpacts, 0);
3951
3952     /* Parse actions. */
3953     error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3954     if (error) {
3955         unixctl_command_reply_error(conn, error);
3956         free(error);
3957         goto exit;
3958     }
3959
3960     /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
3961      * consistency between the flow and the actions.  With -consistent, we
3962      * enforce consistency even for a flow supported in OpenFlow 1.0. */
3963     if (!strcmp(argv[1], "-consistent")) {
3964         enforce_consistency = true;
3965         argv++;
3966         argc--;
3967     } else {
3968         enforce_consistency = false;
3969     }
3970
3971     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3972     if (error) {
3973         unixctl_command_reply_error(conn, error);
3974         free(error);
3975         goto exit;
3976     }
3977
3978     /* Do the same checks as handle_packet_out() in ofproto.c.
3979      *
3980      * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3981      * strictly correct because these actions aren't in any table, but it's OK
3982      * because it 'table_id' is used only to check goto_table instructions, but
3983      * packet-outs take a list of actions and therefore it can't include
3984      * instructions.
3985      *
3986      * We skip the "meter" check here because meter is an instruction, not an
3987      * action, and thus cannot appear in ofpacts. */
3988     in_port = ofp_to_u16(flow.in_port.ofp_port);
3989     if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3990         unixctl_command_reply_error(conn, "invalid in_port");
3991         goto exit;
3992     }
3993     if (enforce_consistency) {
3994         retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3995                                            u16_to_ofp(ofproto->up.max_ports),
3996                                            0, 0, usable_protocols);
3997     } else {
3998         retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3999                                u16_to_ofp(ofproto->up.max_ports), 0, 0,
4000                                &usable_protocols);
4001     }
4002
4003     if (retval) {
4004         ds_clear(&result);
4005         ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4006         unixctl_command_reply_error(conn, ds_cstr(&result));
4007         goto exit;
4008     }
4009
4010     ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
4011     unixctl_command_reply(conn, ds_cstr(&result));
4012
4013 exit:
4014     ds_destroy(&result);
4015     ofpbuf_delete(packet);
4016     ofpbuf_uninit(&ofpacts);
4017 }
4018
4019 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4020  * description of the results to 'ds'.
4021  *
4022  * The trace follows a packet with the specified 'flow' through the flow
4023  * table.  'packet' may be nonnull to trace an actual packet, with consequent
4024  * side effects (if it is nonnull then its flow must be 'flow').
4025  *
4026  * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4027  * trace, otherwise the actions are determined by a flow table lookup. */
4028 static void
4029 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
4030               const struct ofpbuf *packet,
4031               const struct ofpact ofpacts[], size_t ofpacts_len,
4032               struct ds *ds)
4033 {
4034     struct rule_dpif *rule;
4035     struct trace_ctx trace;
4036
4037     ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4038     ds_put_cstr(ds, "Flow: ");
4039     flow_format(ds, flow);
4040     ds_put_char(ds, '\n');
4041
4042     flow_wildcards_init_catchall(&trace.wc);
4043     if (ofpacts) {
4044         rule = NULL;
4045     } else {
4046         rule_dpif_lookup(ofproto, flow, &trace.wc, &rule);
4047
4048         trace_format_rule(ds, 0, rule);
4049         if (rule == ofproto->miss_rule) {
4050             ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
4051         } else if (rule == ofproto->no_packet_in_rule) {
4052             ds_put_cstr(ds, "\nNo match, packets dropped because "
4053                         "OFPPC_NO_PACKET_IN is set on in_port.\n");
4054         } else if (rule == ofproto->drop_frags_rule) {
4055             ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
4056                         "and the fragment handling mode is \"drop\".\n");
4057         }
4058     }
4059
4060     if (rule || ofpacts) {
4061         trace.result = ds;
4062         trace.flow = *flow;
4063         xlate_in_init(&trace.xin, ofproto, flow, rule, ntohs(flow->tcp_flags),
4064                       packet);
4065         if (ofpacts) {
4066             trace.xin.ofpacts = ofpacts;
4067             trace.xin.ofpacts_len = ofpacts_len;
4068         }
4069         trace.xin.resubmit_hook = trace_resubmit;
4070         trace.xin.report_hook = trace_report;
4071
4072         xlate_actions(&trace.xin, &trace.xout);
4073
4074         ds_put_char(ds, '\n');
4075         trace_format_flow(ds, 0, "Final flow", &trace);
4076         trace_format_megaflow(ds, 0, "Megaflow", &trace);
4077
4078         ds_put_cstr(ds, "Datapath actions: ");
4079         format_odp_actions(ds, trace.xout.odp_actions.data,
4080                            trace.xout.odp_actions.size);
4081
4082         if (trace.xout.slow) {
4083             enum slow_path_reason slow;
4084
4085             ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4086                         "slow path because it:");
4087
4088             slow = trace.xout.slow;
4089             while (slow) {
4090                 enum slow_path_reason bit = rightmost_1bit(slow);
4091
4092                 ds_put_format(ds, "\n\t- %s.",
4093                               slow_path_reason_to_explanation(bit));
4094
4095                 slow &= ~bit;
4096             }
4097         }
4098
4099         xlate_out_uninit(&trace.xout);
4100     }
4101
4102     rule_dpif_unref(rule);
4103 }
4104
4105 /* Store the current ofprotos in 'ofproto_shash'.  Returns a sorted list
4106  * of the 'ofproto_shash' nodes.  It is the responsibility of the caller
4107  * to destroy 'ofproto_shash' and free the returned value. */
4108 static const struct shash_node **
4109 get_ofprotos(struct shash *ofproto_shash)
4110 {
4111     const struct ofproto_dpif *ofproto;
4112
4113     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4114         char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4115         shash_add_nocopy(ofproto_shash, name, ofproto);
4116     }
4117
4118     return shash_sort(ofproto_shash);
4119 }
4120
4121 static void
4122 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4123                               const char *argv[] OVS_UNUSED,
4124                               void *aux OVS_UNUSED)
4125 {
4126     struct ds ds = DS_EMPTY_INITIALIZER;
4127     struct shash ofproto_shash;
4128     const struct shash_node **sorted_ofprotos;
4129     int i;
4130
4131     shash_init(&ofproto_shash);
4132     sorted_ofprotos = get_ofprotos(&ofproto_shash);
4133     for (i = 0; i < shash_count(&ofproto_shash); i++) {
4134         const struct shash_node *node = sorted_ofprotos[i];
4135         ds_put_format(&ds, "%s\n", node->name);
4136     }
4137
4138     shash_destroy(&ofproto_shash);
4139     free(sorted_ofprotos);
4140
4141     unixctl_command_reply(conn, ds_cstr(&ds));
4142     ds_destroy(&ds);
4143 }
4144
4145 static void
4146 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4147 {
4148     const struct shash_node **ofprotos;
4149     struct dpif_dp_stats dp_stats;
4150     struct shash ofproto_shash;
4151     size_t i;
4152
4153     dpif_get_dp_stats(backer->dpif, &dp_stats);
4154
4155     ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4156                   dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4157
4158     shash_init(&ofproto_shash);
4159     ofprotos = get_ofprotos(&ofproto_shash);
4160     for (i = 0; i < shash_count(&ofproto_shash); i++) {
4161         struct ofproto_dpif *ofproto = ofprotos[i]->data;
4162         const struct shash_node **ports;
4163         size_t j;
4164
4165         if (ofproto->backer != backer) {
4166             continue;
4167         }
4168
4169         ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4170
4171         ports = shash_sort(&ofproto->up.port_by_name);
4172         for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4173             const struct shash_node *node = ports[j];
4174             struct ofport *ofport = node->data;
4175             struct smap config;
4176             odp_port_t odp_port;
4177
4178             ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4179                           ofport->ofp_port);
4180
4181             odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4182             if (odp_port != ODPP_NONE) {
4183                 ds_put_format(ds, "%"PRIu32":", odp_port);
4184             } else {
4185                 ds_put_cstr(ds, "none:");
4186             }
4187
4188             ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4189
4190             smap_init(&config);
4191             if (!netdev_get_config(ofport->netdev, &config)) {
4192                 const struct smap_node **nodes;
4193                 size_t i;
4194
4195                 nodes = smap_sort(&config);
4196                 for (i = 0; i < smap_count(&config); i++) {
4197                     const struct smap_node *node = nodes[i];
4198                     ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4199                                   node->key, node->value);
4200                 }
4201                 free(nodes);
4202             }
4203             smap_destroy(&config);
4204
4205             ds_put_char(ds, ')');
4206             ds_put_char(ds, '\n');
4207         }
4208         free(ports);
4209     }
4210     shash_destroy(&ofproto_shash);
4211     free(ofprotos);
4212 }
4213
4214 static void
4215 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4216                           const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4217 {
4218     struct ds ds = DS_EMPTY_INITIALIZER;
4219     const struct shash_node **backers;
4220     int i;
4221
4222     backers = shash_sort(&all_dpif_backers);
4223     for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4224         dpif_show_backer(backers[i]->data, &ds);
4225     }
4226     free(backers);
4227
4228     unixctl_command_reply(conn, ds_cstr(&ds));
4229     ds_destroy(&ds);
4230 }
4231
4232 static bool
4233 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4234                            const struct nlattr *key, size_t key_len)
4235 {
4236     struct ofproto_dpif *ofp;
4237     struct flow flow;
4238
4239     xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4240                   NULL, NULL, NULL, NULL);
4241     return ofp == ofproto;
4242 }
4243
4244 static void
4245 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4246                                 int argc OVS_UNUSED, const char *argv[],
4247                                 void *aux OVS_UNUSED)
4248 {
4249     struct ds ds = DS_EMPTY_INITIALIZER;
4250     const struct dpif_flow_stats *stats;
4251     const struct ofproto_dpif *ofproto;
4252     struct dpif_flow_dump flow_dump;
4253     const struct nlattr *actions;
4254     const struct nlattr *mask;
4255     const struct nlattr *key;
4256     size_t actions_len;
4257     size_t mask_len;
4258     size_t key_len;
4259     bool verbosity = false;
4260     struct dpif_port dpif_port;
4261     struct dpif_port_dump port_dump;
4262     struct hmap portno_names;
4263     void *state = NULL;
4264     int error;
4265
4266     ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4267     if (!ofproto) {
4268         unixctl_command_reply_error(conn, "no such bridge");
4269         return;
4270     }
4271
4272     if (argc > 2 && !strcmp(argv[1], "-m")) {
4273         verbosity = true;
4274     }
4275
4276     hmap_init(&portno_names);
4277     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4278         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4279     }
4280
4281     ds_init(&ds);
4282     error = dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4283     if (error) {
4284         goto exit;
4285     }
4286     dpif_flow_dump_state_init(ofproto->backer->dpif, &state);
4287     while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
4288                                &mask, &mask_len, &actions, &actions_len,
4289                                &stats)) {
4290         if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4291             continue;
4292         }
4293
4294         odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4295                         verbosity);
4296         ds_put_cstr(&ds, ", ");
4297         dpif_flow_stats_format(stats, &ds);
4298         ds_put_cstr(&ds, ", actions:");
4299         format_odp_actions(&ds, actions, actions_len);
4300         ds_put_char(&ds, '\n');
4301     }
4302     dpif_flow_dump_state_uninit(ofproto->backer->dpif, state);
4303     error = dpif_flow_dump_done(&flow_dump);
4304
4305 exit:
4306     if (error) {
4307         ds_clear(&ds);
4308         ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4309         unixctl_command_reply_error(conn, ds_cstr(&ds));
4310     } else {
4311         unixctl_command_reply(conn, ds_cstr(&ds));
4312     }
4313     odp_portno_names_destroy(&portno_names);
4314     hmap_destroy(&portno_names);
4315     ds_destroy(&ds);
4316 }
4317
4318 static void
4319 ofproto_dpif_unixctl_init(void)
4320 {
4321     static bool registered;
4322     if (registered) {
4323         return;
4324     }
4325     registered = true;
4326
4327     unixctl_command_register(
4328         "ofproto/trace",
4329         "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4330         1, 3, ofproto_unixctl_trace, NULL);
4331     unixctl_command_register(
4332         "ofproto/trace-packet-out",
4333         "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4334         2, 6, ofproto_unixctl_trace_actions, NULL);
4335     unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4336                              ofproto_unixctl_fdb_flush, NULL);
4337     unixctl_command_register("fdb/show", "bridge", 1, 1,
4338                              ofproto_unixctl_fdb_show, NULL);
4339     unixctl_command_register("dpif/dump-dps", "", 0, 0,
4340                              ofproto_unixctl_dpif_dump_dps, NULL);
4341     unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4342                              NULL);
4343     unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4344                              ofproto_unixctl_dpif_dump_flows, NULL);
4345 }
4346
4347
4348 /* Returns true if 'rule' is an internal rule, false otherwise. */
4349 bool
4350 rule_is_internal(const struct rule *rule)
4351 {
4352     return rule->table_id == TBL_INTERNAL;
4353 }
4354 \f
4355 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4356  *
4357  * This is deprecated.  It is only for compatibility with broken device drivers
4358  * in old versions of Linux that do not properly support VLANs when VLAN
4359  * devices are not used.  When broken device drivers are no longer in
4360  * widespread use, we will delete these interfaces. */
4361
4362 static int
4363 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4364 {
4365     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4366     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4367
4368     if (realdev_ofp_port == ofport->realdev_ofp_port
4369         && vid == ofport->vlandev_vid) {
4370         return 0;
4371     }
4372
4373     ofproto->backer->need_revalidate = REV_RECONFIGURE;
4374
4375     if (ofport->realdev_ofp_port) {
4376         vsp_remove(ofport);
4377     }
4378     if (realdev_ofp_port && ofport->bundle) {
4379         /* vlandevs are enslaved to their realdevs, so they are not allowed to
4380          * themselves be part of a bundle. */
4381         bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4382     }
4383
4384     ofport->realdev_ofp_port = realdev_ofp_port;
4385     ofport->vlandev_vid = vid;
4386
4387     if (realdev_ofp_port) {
4388         vsp_add(ofport, realdev_ofp_port, vid);
4389     }
4390
4391     return 0;
4392 }
4393
4394 static uint32_t
4395 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4396 {
4397     return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4398 }
4399
4400 bool
4401 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4402     OVS_EXCLUDED(ofproto->vsp_mutex)
4403 {
4404     /* hmap_is_empty is thread safe. */
4405     return !hmap_is_empty(&ofproto->realdev_vid_map);
4406 }
4407
4408
4409 static ofp_port_t
4410 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4411                          ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4412     OVS_REQUIRES(ofproto->vsp_mutex)
4413 {
4414     if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4415         int vid = vlan_tci_to_vid(vlan_tci);
4416         const struct vlan_splinter *vsp;
4417
4418         HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4419                                  hash_realdev_vid(realdev_ofp_port, vid),
4420                                  &ofproto->realdev_vid_map) {
4421             if (vsp->realdev_ofp_port == realdev_ofp_port
4422                 && vsp->vid == vid) {
4423                 return vsp->vlandev_ofp_port;
4424             }
4425         }
4426     }
4427     return realdev_ofp_port;
4428 }
4429
4430 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4431  * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4432  * 'struct ofport_dpif'.  For example, given 'realdev_ofp_port' of eth0 and
4433  * 'vlan_tci' 9, it would return the port number of eth0.9.
4434  *
4435  * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4436  * function just returns its 'realdev_ofp_port' argument. */
4437 ofp_port_t
4438 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4439                        ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4440     OVS_EXCLUDED(ofproto->vsp_mutex)
4441 {
4442     ofp_port_t ret;
4443
4444     /* hmap_is_empty is thread safe, see if we can return immediately. */
4445     if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4446         return realdev_ofp_port;
4447     }
4448     ovs_mutex_lock(&ofproto->vsp_mutex);
4449     ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4450     ovs_mutex_unlock(&ofproto->vsp_mutex);
4451     return ret;
4452 }
4453
4454 static struct vlan_splinter *
4455 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4456 {
4457     struct vlan_splinter *vsp;
4458
4459     HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4460                              hash_ofp_port(vlandev_ofp_port),
4461                              &ofproto->vlandev_map) {
4462         if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4463             return vsp;
4464         }
4465     }
4466
4467     return NULL;
4468 }
4469
4470 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4471  * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4472  * VLAN VID of the Linux VLAN device in '*vid'.  For example, given
4473  * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4474  * eth0 and store 9 in '*vid'.
4475  *
4476  * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4477  * VLAN device.  Unless VLAN splinters are enabled, this is what this function
4478  * always does.*/
4479 static ofp_port_t
4480 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4481                        ofp_port_t vlandev_ofp_port, int *vid)
4482     OVS_REQUIRES(ofproto->vsp_mutex)
4483 {
4484     if (!hmap_is_empty(&ofproto->vlandev_map)) {
4485         const struct vlan_splinter *vsp;
4486
4487         vsp = vlandev_find(ofproto, vlandev_ofp_port);
4488         if (vsp) {
4489             if (vid) {
4490                 *vid = vsp->vid;
4491             }
4492             return vsp->realdev_ofp_port;
4493         }
4494     }
4495     return 0;
4496 }
4497
4498 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4499  * whether 'flow->in_port' represents a Linux VLAN device.  If so, changes
4500  * 'flow->in_port' to the "real" device backing the VLAN device, sets
4501  * 'flow->vlan_tci' to the VLAN VID, and returns true.  Otherwise (which is
4502  * always the case unless VLAN splinters are enabled), returns false without
4503  * making any changes. */
4504 bool
4505 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4506     OVS_EXCLUDED(ofproto->vsp_mutex)
4507 {
4508     ofp_port_t realdev;
4509     int vid;
4510
4511     /* hmap_is_empty is thread safe. */
4512     if (hmap_is_empty(&ofproto->vlandev_map)) {
4513         return false;
4514     }
4515
4516     ovs_mutex_lock(&ofproto->vsp_mutex);
4517     realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4518     ovs_mutex_unlock(&ofproto->vsp_mutex);
4519     if (!realdev) {
4520         return false;
4521     }
4522
4523     /* Cause the flow to be processed as if it came in on the real device with
4524      * the VLAN device's VLAN ID. */
4525     flow->in_port.ofp_port = realdev;
4526     flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4527     return true;
4528 }
4529
4530 static void
4531 vsp_remove(struct ofport_dpif *port)
4532 {
4533     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4534     struct vlan_splinter *vsp;
4535
4536     ovs_mutex_lock(&ofproto->vsp_mutex);
4537     vsp = vlandev_find(ofproto, port->up.ofp_port);
4538     if (vsp) {
4539         hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4540         hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4541         free(vsp);
4542
4543         port->realdev_ofp_port = 0;
4544     } else {
4545         VLOG_ERR("missing vlan device record");
4546     }
4547     ovs_mutex_unlock(&ofproto->vsp_mutex);
4548 }
4549
4550 static void
4551 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4552 {
4553     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4554
4555     ovs_mutex_lock(&ofproto->vsp_mutex);
4556     if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4557         && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4558             == realdev_ofp_port)) {
4559         struct vlan_splinter *vsp;
4560
4561         vsp = xmalloc(sizeof *vsp);
4562         vsp->realdev_ofp_port = realdev_ofp_port;
4563         vsp->vlandev_ofp_port = port->up.ofp_port;
4564         vsp->vid = vid;
4565
4566         port->realdev_ofp_port = realdev_ofp_port;
4567
4568         hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4569                     hash_ofp_port(port->up.ofp_port));
4570         hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4571                     hash_realdev_vid(realdev_ofp_port, vid));
4572     } else {
4573         VLOG_ERR("duplicate vlan device record");
4574     }
4575     ovs_mutex_unlock(&ofproto->vsp_mutex);
4576 }
4577
4578 static odp_port_t
4579 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4580 {
4581     const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4582     return ofport ? ofport->odp_port : ODPP_NONE;
4583 }
4584
4585 struct ofport_dpif *
4586 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4587 {
4588     struct ofport_dpif *port;
4589
4590     ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4591     HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4592                              &backer->odp_to_ofport_map) {
4593         if (port->odp_port == odp_port) {
4594             ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4595             return port;
4596         }
4597     }
4598
4599     ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4600     return NULL;
4601 }
4602
4603 static ofp_port_t
4604 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4605 {
4606     struct ofport_dpif *port;
4607
4608     port = odp_port_to_ofport(ofproto->backer, odp_port);
4609     if (port && &ofproto->up == port->up.ofproto) {
4610         return port->up.ofp_port;
4611     } else {
4612         return OFPP_NONE;
4613     }
4614 }
4615
4616 uint32_t
4617 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
4618 {
4619     struct dpif_backer *backer = ofproto->backer;
4620
4621     return  recirc_id_alloc(backer->rid_pool);
4622 }
4623
4624 void
4625 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
4626 {
4627     struct dpif_backer *backer = ofproto->backer;
4628
4629     recirc_id_free(backer->rid_pool, recirc_id);
4630 }
4631
4632 const struct ofproto_class ofproto_dpif_class = {
4633     init,
4634     enumerate_types,
4635     enumerate_names,
4636     del,
4637     port_open_type,
4638     type_run,
4639     type_wait,
4640     alloc,
4641     construct,
4642     destruct,
4643     dealloc,
4644     run,
4645     wait,
4646     NULL,                       /* get_memory_usage. */
4647     type_get_memory_usage,
4648     flush,
4649     get_features,
4650     get_tables,
4651     port_alloc,
4652     port_construct,
4653     port_destruct,
4654     port_dealloc,
4655     port_modified,
4656     port_reconfigured,
4657     port_query_by_name,
4658     port_add,
4659     port_del,
4660     port_get_stats,
4661     port_dump_start,
4662     port_dump_next,
4663     port_dump_done,
4664     port_poll,
4665     port_poll_wait,
4666     port_is_lacp_current,
4667     NULL,                       /* rule_choose_table */
4668     rule_alloc,
4669     rule_construct,
4670     rule_insert,
4671     rule_delete,
4672     rule_destruct,
4673     rule_dealloc,
4674     rule_get_stats,
4675     rule_execute,
4676     rule_modify_actions,
4677     set_frag_handling,
4678     packet_out,
4679     set_netflow,
4680     get_netflow_ids,
4681     set_sflow,
4682     set_ipfix,
4683     set_cfm,
4684     get_cfm_status,
4685     set_bfd,
4686     get_bfd_status,
4687     set_stp,
4688     get_stp_status,
4689     set_stp_port,
4690     get_stp_port_status,
4691     get_stp_port_stats,
4692     set_queues,
4693     bundle_set,
4694     bundle_remove,
4695     mirror_set__,
4696     mirror_get_stats__,
4697     set_flood_vlans,
4698     is_mirror_output_bundle,
4699     forward_bpdu_changed,
4700     set_mac_table_config,
4701     set_realdev,
4702     NULL,                       /* meter_get_features */
4703     NULL,                       /* meter_set */
4704     NULL,                       /* meter_get */
4705     NULL,                       /* meter_del */
4706     group_alloc,                /* group_alloc */
4707     group_construct,            /* group_construct */
4708     group_destruct,             /* group_destruct */
4709     group_dealloc,              /* group_dealloc */
4710     group_modify,               /* group_modify */
4711     group_get_stats,            /* group_get_stats */
4712 };