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