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