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