Add connection tracking mark support.
[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 CHECK_FEATURE(ct_mark)
1273
1274 #undef CHECK_FEATURE
1275 #undef CHECK_FEATURE__
1276
1277 static void
1278 check_support(struct dpif_backer *backer)
1279 {
1280     /* This feature needs to be tested after udpif threads are set. */
1281     backer->support.variable_length_userdata = false;
1282
1283     backer->support.odp.recirc = check_recirc(backer);
1284     backer->support.odp.max_mpls_depth = check_max_mpls_depth(backer);
1285     backer->support.masked_set_action = check_masked_set_action(backer);
1286     backer->support.ufid = check_ufid(backer);
1287     backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1288
1289     backer->support.odp.ct_state = check_ct_state(backer);
1290     backer->support.odp.ct_zone = check_ct_zone(backer);
1291     backer->support.odp.ct_mark = check_ct_mark(backer);
1292 }
1293
1294 static int
1295 construct(struct ofproto *ofproto_)
1296 {
1297     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1298     struct shash_node *node, *next;
1299     int error;
1300
1301     /* Tunnel module can get used right after the udpif threads are running. */
1302     ofproto_tunnel_init();
1303
1304     error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1305     if (error) {
1306         return error;
1307     }
1308
1309     atomic_init(&ofproto->tables_version, CLS_MIN_VERSION);
1310     ofproto->netflow = NULL;
1311     ofproto->sflow = NULL;
1312     ofproto->ipfix = NULL;
1313     ofproto->stp = NULL;
1314     ofproto->rstp = NULL;
1315     ofproto->dump_seq = 0;
1316     hmap_init(&ofproto->bundles);
1317     ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1318     ofproto->ms = NULL;
1319     ofproto->mbridge = mbridge_create();
1320     ofproto->has_bonded_bundles = false;
1321     ofproto->lacp_enabled = false;
1322     ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1323     ovs_mutex_init(&ofproto->vsp_mutex);
1324
1325     guarded_list_init(&ofproto->pins);
1326
1327     hmap_init(&ofproto->vlandev_map);
1328     hmap_init(&ofproto->realdev_vid_map);
1329
1330     sset_init(&ofproto->ports);
1331     sset_init(&ofproto->ghost_ports);
1332     sset_init(&ofproto->port_poll_set);
1333     ofproto->port_poll_errno = 0;
1334     ofproto->change_seq = 0;
1335     ofproto->pins_seq = seq_create();
1336     ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1337
1338
1339     SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1340         struct iface_hint *iface_hint = node->data;
1341
1342         if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1343             /* Check if the datapath already has this port. */
1344             if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1345                 sset_add(&ofproto->ports, node->name);
1346             }
1347
1348             free(iface_hint->br_name);
1349             free(iface_hint->br_type);
1350             free(iface_hint);
1351             shash_delete(&init_ofp_ports, node);
1352         }
1353     }
1354
1355     hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1356                 hash_string(ofproto->up.name, 0));
1357     memset(&ofproto->stats, 0, sizeof ofproto->stats);
1358
1359     ofproto_init_tables(ofproto_, N_TABLES);
1360     error = add_internal_flows(ofproto);
1361
1362     ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1363
1364     return error;
1365 }
1366
1367 static int
1368 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1369                   const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1370 {
1371     struct match match;
1372     int error;
1373     struct rule *rule;
1374
1375     match_init_catchall(&match);
1376     match_set_reg(&match, 0, id);
1377
1378     error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1379                                            &rule);
1380     *rulep = error ? NULL : rule_dpif_cast(rule);
1381
1382     return error;
1383 }
1384
1385 static int
1386 add_internal_flows(struct ofproto_dpif *ofproto)
1387 {
1388     struct ofpact_controller *controller;
1389     uint64_t ofpacts_stub[128 / 8];
1390     struct ofpbuf ofpacts;
1391     struct rule *unused_rulep OVS_UNUSED;
1392     struct match match;
1393     int error;
1394     int id;
1395
1396     ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1397     id = 1;
1398
1399     controller = ofpact_put_CONTROLLER(&ofpacts);
1400     controller->max_len = UINT16_MAX;
1401     controller->controller_id = 0;
1402     controller->reason = OFPR_NO_MATCH;
1403     ofpact_pad(&ofpacts);
1404
1405     error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1406                                    &ofproto->miss_rule);
1407     if (error) {
1408         return error;
1409     }
1410
1411     ofpbuf_clear(&ofpacts);
1412     error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1413                                    &ofproto->no_packet_in_rule);
1414     if (error) {
1415         return error;
1416     }
1417
1418     error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1419                                    &ofproto->drop_frags_rule);
1420     if (error) {
1421         return error;
1422     }
1423
1424     /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1425      * zero when reaching this rule.
1426      *
1427      * (priority=2), recirc_id=0, actions=drop
1428      */
1429     ofpbuf_clear(&ofpacts);
1430     match_init_catchall(&match);
1431     match_set_recirc_id(&match, 0);
1432     error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1433                                            &unused_rulep);
1434     return error;
1435 }
1436
1437 static void
1438 destruct(struct ofproto *ofproto_)
1439 {
1440     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1441     struct ofproto_packet_in *pin;
1442     struct rule_dpif *rule;
1443     struct oftable *table;
1444     struct ovs_list pins;
1445
1446     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1447     xlate_txn_start();
1448     xlate_remove_ofproto(ofproto);
1449     xlate_txn_commit();
1450
1451     /* Ensure that the upcall processing threads have no remaining references
1452      * to the ofproto or anything in it. */
1453     udpif_synchronize(ofproto->backer->udpif);
1454
1455     hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1456
1457     OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1458         CLS_FOR_EACH (rule, up.cr, &table->cls) {
1459             ofproto_rule_delete(&ofproto->up, &rule->up);
1460         }
1461     }
1462     ofproto_group_delete_all(&ofproto->up);
1463
1464     guarded_list_pop_all(&ofproto->pins, &pins);
1465     LIST_FOR_EACH_POP (pin, list_node, &pins) {
1466         free(CONST_CAST(void *, pin->up.packet));
1467         free(pin);
1468     }
1469     guarded_list_destroy(&ofproto->pins);
1470
1471     recirc_free_ofproto(ofproto, ofproto->up.name);
1472
1473     mbridge_unref(ofproto->mbridge);
1474
1475     netflow_unref(ofproto->netflow);
1476     dpif_sflow_unref(ofproto->sflow);
1477     dpif_ipfix_unref(ofproto->ipfix);
1478     hmap_destroy(&ofproto->bundles);
1479     mac_learning_unref(ofproto->ml);
1480     mcast_snooping_unref(ofproto->ms);
1481
1482     hmap_destroy(&ofproto->vlandev_map);
1483     hmap_destroy(&ofproto->realdev_vid_map);
1484
1485     sset_destroy(&ofproto->ports);
1486     sset_destroy(&ofproto->ghost_ports);
1487     sset_destroy(&ofproto->port_poll_set);
1488
1489     ovs_mutex_destroy(&ofproto->stats_mutex);
1490     ovs_mutex_destroy(&ofproto->vsp_mutex);
1491
1492     seq_destroy(ofproto->pins_seq);
1493
1494     close_dpif_backer(ofproto->backer);
1495 }
1496
1497 static int
1498 run(struct ofproto *ofproto_)
1499 {
1500     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1501     uint64_t new_seq, new_dump_seq;
1502
1503     if (mbridge_need_revalidate(ofproto->mbridge)) {
1504         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1505         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1506         mac_learning_flush(ofproto->ml);
1507         ovs_rwlock_unlock(&ofproto->ml->rwlock);
1508         mcast_snooping_mdb_flush(ofproto->ms);
1509     }
1510
1511     /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1512      * flow restore.  Even though nothing is processed during flow restore,
1513      * all queued 'pins' will be handled immediately when flow restore
1514      * completes. */
1515     ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1516
1517     /* Do not perform any periodic activity required by 'ofproto' while
1518      * waiting for flow restore to complete. */
1519     if (!ofproto_get_flow_restore_wait()) {
1520         struct ofproto_packet_in *pin;
1521         struct ovs_list pins;
1522
1523         guarded_list_pop_all(&ofproto->pins, &pins);
1524         LIST_FOR_EACH_POP (pin, list_node, &pins) {
1525             connmgr_send_packet_in(ofproto->up.connmgr, pin);
1526             free(CONST_CAST(void *, pin->up.packet));
1527             free(pin);
1528         }
1529     }
1530
1531     if (ofproto->netflow) {
1532         netflow_run(ofproto->netflow);
1533     }
1534     if (ofproto->sflow) {
1535         dpif_sflow_run(ofproto->sflow);
1536     }
1537     if (ofproto->ipfix) {
1538         dpif_ipfix_run(ofproto->ipfix);
1539     }
1540
1541     new_seq = seq_read(connectivity_seq_get());
1542     if (ofproto->change_seq != new_seq) {
1543         struct ofport_dpif *ofport;
1544
1545         HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1546             port_run(ofport);
1547         }
1548
1549         ofproto->change_seq = new_seq;
1550     }
1551     if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1552         struct ofbundle *bundle;
1553
1554         HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1555             bundle_run(bundle);
1556         }
1557     }
1558
1559     stp_run(ofproto);
1560     rstp_run(ofproto);
1561     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1562     if (mac_learning_run(ofproto->ml)) {
1563         ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1564     }
1565     ovs_rwlock_unlock(&ofproto->ml->rwlock);
1566
1567     if (mcast_snooping_run(ofproto->ms)) {
1568         ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1569     }
1570
1571     new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1572     if (ofproto->dump_seq != new_dump_seq) {
1573         struct rule *rule, *next_rule;
1574
1575         /* We know stats are relatively fresh, so now is a good time to do some
1576          * periodic work. */
1577         ofproto->dump_seq = new_dump_seq;
1578
1579         /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1580          * has passed. */
1581         ovs_mutex_lock(&ofproto_mutex);
1582         LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1583                             &ofproto->up.expirable) {
1584             rule_expire(rule_dpif_cast(rule));
1585         }
1586         ovs_mutex_unlock(&ofproto_mutex);
1587
1588         /* All outstanding data in existing flows has been accounted, so it's a
1589          * good time to do bond rebalancing. */
1590         if (ofproto->has_bonded_bundles) {
1591             struct ofbundle *bundle;
1592
1593             HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1594                 if (bundle->bond) {
1595                     bond_rebalance(bundle->bond);
1596                 }
1597             }
1598         }
1599     }
1600     return 0;
1601 }
1602
1603 static void
1604 wait(struct ofproto *ofproto_)
1605 {
1606     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1607
1608     if (ofproto_get_flow_restore_wait()) {
1609         return;
1610     }
1611
1612     if (ofproto->sflow) {
1613         dpif_sflow_wait(ofproto->sflow);
1614     }
1615     if (ofproto->ipfix) {
1616         dpif_ipfix_wait(ofproto->ipfix);
1617     }
1618     if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1619         struct ofbundle *bundle;
1620
1621         HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1622             bundle_wait(bundle);
1623         }
1624     }
1625     if (ofproto->netflow) {
1626         netflow_wait(ofproto->netflow);
1627     }
1628     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1629     mac_learning_wait(ofproto->ml);
1630     ovs_rwlock_unlock(&ofproto->ml->rwlock);
1631     mcast_snooping_wait(ofproto->ms);
1632     stp_wait(ofproto);
1633     if (ofproto->backer->need_revalidate) {
1634         /* Shouldn't happen, but if it does just go around again. */
1635         VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1636         poll_immediate_wake();
1637     }
1638
1639     seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1640     seq_wait(ofproto->pins_seq, ofproto->pins_seqno);
1641 }
1642
1643 static void
1644 type_get_memory_usage(const char *type, struct simap *usage)
1645 {
1646     struct dpif_backer *backer;
1647
1648     backer = shash_find_data(&all_dpif_backers, type);
1649     if (backer) {
1650         udpif_get_memory_usage(backer->udpif, usage);
1651     }
1652 }
1653
1654 static void
1655 flush(struct ofproto *ofproto_)
1656 {
1657     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1658     struct dpif_backer *backer = ofproto->backer;
1659
1660     if (backer) {
1661         udpif_flush(backer->udpif);
1662     }
1663 }
1664
1665 static void
1666 query_tables(struct ofproto *ofproto,
1667              struct ofputil_table_features *features,
1668              struct ofputil_table_stats *stats)
1669 {
1670     strcpy(features->name, "classifier");
1671
1672     if (stats) {
1673         int i;
1674
1675         for (i = 0; i < ofproto->n_tables; i++) {
1676             unsigned long missed, matched;
1677
1678             atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1679             atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1680
1681             stats[i].matched_count = matched;
1682             stats[i].lookup_count = matched + missed;
1683         }
1684     }
1685 }
1686
1687 static void
1688 set_tables_version(struct ofproto *ofproto_, cls_version_t version)
1689 {
1690     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1691
1692     atomic_store_relaxed(&ofproto->tables_version, version);
1693 }
1694
1695
1696 static struct ofport *
1697 port_alloc(void)
1698 {
1699     struct ofport_dpif *port = xzalloc(sizeof *port);
1700     return &port->up;
1701 }
1702
1703 static void
1704 port_dealloc(struct ofport *port_)
1705 {
1706     struct ofport_dpif *port = ofport_dpif_cast(port_);
1707     free(port);
1708 }
1709
1710 static int
1711 port_construct(struct ofport *port_)
1712 {
1713     struct ofport_dpif *port = ofport_dpif_cast(port_);
1714     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1715     const struct netdev *netdev = port->up.netdev;
1716     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1717     const char *dp_port_name;
1718     struct dpif_port dpif_port;
1719     int error;
1720
1721     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1722     port->bundle = NULL;
1723     port->cfm = NULL;
1724     port->bfd = NULL;
1725     port->lldp = NULL;
1726     port->may_enable = false;
1727     port->stp_port = NULL;
1728     port->stp_state = STP_DISABLED;
1729     port->rstp_port = NULL;
1730     port->rstp_state = RSTP_DISABLED;
1731     port->is_tunnel = false;
1732     port->peer = NULL;
1733     port->qdscp = NULL;
1734     port->n_qdscp = 0;
1735     port->realdev_ofp_port = 0;
1736     port->vlandev_vid = 0;
1737     port->carrier_seq = netdev_get_carrier_resets(netdev);
1738     port->is_layer3 = netdev_vport_is_layer3(netdev);
1739
1740     if (netdev_vport_is_patch(netdev)) {
1741         /* By bailing out here, we don't submit the port to the sFlow module
1742          * to be considered for counter polling export.  This is correct
1743          * because the patch port represents an interface that sFlow considers
1744          * to be "internal" to the switch as a whole, and therefore not a
1745          * candidate for counter polling. */
1746         port->odp_port = ODPP_NONE;
1747         ofport_update_peer(port);
1748         return 0;
1749     }
1750
1751     dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1752     error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
1753                                     &dpif_port);
1754     if (error) {
1755         return error;
1756     }
1757
1758     port->odp_port = dpif_port.port_no;
1759
1760     if (netdev_get_tunnel_config(netdev)) {
1761         atomic_count_inc(&ofproto->backer->tnl_count);
1762         error = tnl_port_add(port, port->up.netdev, port->odp_port,
1763                              ovs_native_tunneling_is_on(ofproto), dp_port_name);
1764         if (error) {
1765             atomic_count_dec(&ofproto->backer->tnl_count);
1766             dpif_port_destroy(&dpif_port);
1767             return error;
1768         }
1769
1770         port->is_tunnel = true;
1771         if (ofproto->ipfix) {
1772            dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1773         }
1774     } else {
1775         /* Sanity-check that a mapping doesn't already exist.  This
1776          * shouldn't happen for non-tunnel ports. */
1777         if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1778             VLOG_ERR("port %s already has an OpenFlow port number",
1779                      dpif_port.name);
1780             dpif_port_destroy(&dpif_port);
1781             return EBUSY;
1782         }
1783
1784         ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1785         hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1786                     hash_odp_port(port->odp_port));
1787         ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1788     }
1789     dpif_port_destroy(&dpif_port);
1790
1791     if (ofproto->sflow) {
1792         dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1793     }
1794
1795     return 0;
1796 }
1797
1798 static void
1799 port_destruct(struct ofport *port_)
1800 {
1801     struct ofport_dpif *port = ofport_dpif_cast(port_);
1802     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1803     const char *devname = netdev_get_name(port->up.netdev);
1804     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1805     const char *dp_port_name;
1806
1807     ofproto->backer->need_revalidate = REV_RECONFIGURE;
1808     xlate_txn_start();
1809     xlate_ofport_remove(port);
1810     xlate_txn_commit();
1811
1812     dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1813                                               sizeof namebuf);
1814     if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1815         /* The underlying device is still there, so delete it.  This
1816          * happens when the ofproto is being destroyed, since the caller
1817          * assumes that removal of attached ports will happen as part of
1818          * destruction. */
1819         if (!port->is_tunnel) {
1820             dpif_port_del(ofproto->backer->dpif, port->odp_port);
1821         }
1822     }
1823
1824     if (port->peer) {
1825         port->peer->peer = NULL;
1826         port->peer = NULL;
1827     }
1828
1829     if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1830         ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1831         hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1832         ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1833     }
1834
1835     if (port->is_tunnel) {
1836         atomic_count_dec(&ofproto->backer->tnl_count);
1837     }
1838
1839     if (port->is_tunnel && ofproto->ipfix) {
1840        dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1841     }
1842
1843     tnl_port_del(port);
1844     sset_find_and_delete(&ofproto->ports, devname);
1845     sset_find_and_delete(&ofproto->ghost_ports, devname);
1846     bundle_remove(port_);
1847     set_cfm(port_, NULL);
1848     set_bfd(port_, NULL);
1849     set_lldp(port_, NULL);
1850     if (port->stp_port) {
1851         stp_port_disable(port->stp_port);
1852     }
1853     set_rstp_port(port_, NULL);
1854     if (ofproto->sflow) {
1855         dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1856     }
1857
1858     free(port->qdscp);
1859 }
1860
1861 static void
1862 port_modified(struct ofport *port_)
1863 {
1864     struct ofport_dpif *port = ofport_dpif_cast(port_);
1865     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1866     const char *dp_port_name;
1867     struct netdev *netdev = port->up.netdev;
1868
1869     if (port->bundle && port->bundle->bond) {
1870         bond_slave_set_netdev(port->bundle->bond, port, netdev);
1871     }
1872
1873     if (port->cfm) {
1874         cfm_set_netdev(port->cfm, netdev);
1875     }
1876
1877     if (port->bfd) {
1878         bfd_set_netdev(port->bfd, netdev);
1879     }
1880
1881     ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1882                                      port->lldp, &port->up.pp.hw_addr);
1883
1884     dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1885
1886     if (port->is_tunnel) {
1887         struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1888
1889         if (tnl_port_reconfigure(port, netdev, port->odp_port,
1890                                  ovs_native_tunneling_is_on(ofproto),
1891                                  dp_port_name)) {
1892             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1893         }
1894     }
1895
1896     ofport_update_peer(port);
1897 }
1898
1899 static void
1900 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1901 {
1902     struct ofport_dpif *port = ofport_dpif_cast(port_);
1903     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1904     enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1905
1906     if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1907                    OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1908                    OFPUTIL_PC_NO_PACKET_IN)) {
1909         ofproto->backer->need_revalidate = REV_RECONFIGURE;
1910
1911         if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1912             bundle_update(port->bundle);
1913         }
1914     }
1915 }
1916
1917 static int
1918 set_sflow(struct ofproto *ofproto_,
1919           const struct ofproto_sflow_options *sflow_options)
1920 {
1921     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1922     struct dpif_sflow *ds = ofproto->sflow;
1923
1924     if (sflow_options) {
1925         uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1926         if (!ds) {
1927             struct ofport_dpif *ofport;
1928
1929             ds = ofproto->sflow = dpif_sflow_create();
1930             HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1931                 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1932             }
1933         }
1934         dpif_sflow_set_options(ds, sflow_options);
1935         if (dpif_sflow_get_probability(ds) != old_probability) {
1936             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1937         }
1938     } else {
1939         if (ds) {
1940             dpif_sflow_unref(ds);
1941             ofproto->backer->need_revalidate = REV_RECONFIGURE;
1942             ofproto->sflow = NULL;
1943         }
1944     }
1945     return 0;
1946 }
1947
1948 static int
1949 set_ipfix(
1950     struct ofproto *ofproto_,
1951     const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1952     const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1953     size_t n_flow_exporters_options)
1954 {
1955     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1956     struct dpif_ipfix *di = ofproto->ipfix;
1957     bool has_options = bridge_exporter_options || flow_exporters_options;
1958     bool new_di = false;
1959
1960     if (has_options && !di) {
1961         di = ofproto->ipfix = dpif_ipfix_create();
1962         new_di = true;
1963     }
1964
1965     if (di) {
1966         /* Call set_options in any case to cleanly flush the flow
1967          * caches in the last exporters that are to be destroyed. */
1968         dpif_ipfix_set_options(
1969             di, bridge_exporter_options, flow_exporters_options,
1970             n_flow_exporters_options);
1971
1972         /* Add tunnel ports only when a new ipfix created */
1973         if (new_di == true) {
1974             struct ofport_dpif *ofport;
1975             HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1976                 if (ofport->is_tunnel == true) {
1977                     dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
1978                 }
1979             }
1980         }
1981
1982         if (!has_options) {
1983             dpif_ipfix_unref(di);
1984             ofproto->ipfix = NULL;
1985         }
1986     }
1987
1988     return 0;
1989 }
1990
1991 static int
1992 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1993 {
1994     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1995     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1996     struct cfm *old = ofport->cfm;
1997     int error = 0;
1998
1999     if (s) {
2000         if (!ofport->cfm) {
2001             ofport->cfm = cfm_create(ofport->up.netdev);
2002         }
2003
2004         if (cfm_configure(ofport->cfm, s)) {
2005             error = 0;
2006             goto out;
2007         }
2008
2009         error = EINVAL;
2010     }
2011     cfm_unref(ofport->cfm);
2012     ofport->cfm = NULL;
2013 out:
2014     if (ofport->cfm != old) {
2015         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2016     }
2017     ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2018                                      ofport->lldp, &ofport->up.pp.hw_addr);
2019     return error;
2020 }
2021
2022 static bool
2023 cfm_status_changed(struct ofport *ofport_)
2024 {
2025     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2026
2027     return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2028 }
2029
2030 static int
2031 get_cfm_status(const struct ofport *ofport_,
2032                struct cfm_status *status)
2033 {
2034     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2035     int ret = 0;
2036
2037     if (ofport->cfm) {
2038         cfm_get_status(ofport->cfm, status);
2039     } else {
2040         ret = ENOENT;
2041     }
2042
2043     return ret;
2044 }
2045
2046 static int
2047 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2048 {
2049     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2050     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2051     struct bfd *old;
2052
2053     old = ofport->bfd;
2054     ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2055                                 cfg, ofport->up.netdev);
2056     if (ofport->bfd != old) {
2057         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2058     }
2059     ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2060                                      ofport->lldp, &ofport->up.pp.hw_addr);
2061     return 0;
2062 }
2063
2064 static bool
2065 bfd_status_changed(struct ofport *ofport_)
2066 {
2067     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2068
2069     return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2070 }
2071
2072 static int
2073 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2074 {
2075     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2076     int ret = 0;
2077
2078     if (ofport->bfd) {
2079         bfd_get_status(ofport->bfd, smap);
2080     } else {
2081         ret = ENOENT;
2082     }
2083
2084     return ret;
2085 }
2086
2087 static int
2088 set_lldp(struct ofport *ofport_,
2089          const struct smap *cfg)
2090 {
2091     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2092     int error = 0;
2093
2094     if (cfg) {
2095         if (!ofport->lldp) {
2096             struct ofproto_dpif *ofproto;
2097
2098             ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2099             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2100             ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2101         }
2102
2103         if (!lldp_configure(ofport->lldp, cfg)) {
2104             error = EINVAL;
2105         }
2106     }
2107     if (error) {
2108         lldp_unref(ofport->lldp);
2109         ofport->lldp = NULL;
2110     }
2111
2112     ofproto_dpif_monitor_port_update(ofport,
2113                                      ofport->bfd,
2114                                      ofport->cfm,
2115                                      ofport->lldp,
2116                                      &ofport->up.pp.hw_addr);
2117     return error;
2118 }
2119
2120 static bool
2121 get_lldp_status(const struct ofport *ofport_,
2122                struct lldp_status *status OVS_UNUSED)
2123 {
2124     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2125
2126     return ofport->lldp ? true : false;
2127 }
2128
2129 static int
2130 set_aa(struct ofproto *ofproto OVS_UNUSED,
2131        const struct aa_settings *s)
2132 {
2133     return aa_configure(s);
2134 }
2135
2136 static int
2137 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2138                const struct aa_mapping_settings *s)
2139 {
2140     return aa_mapping_register(aux, s);
2141 }
2142
2143 static int
2144 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2145 {
2146     return aa_mapping_unregister(aux);
2147 }
2148
2149 static int
2150 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2151 {
2152     return aa_get_vlan_queued(list);
2153 }
2154
2155 static unsigned int
2156 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2157 {
2158     return aa_get_vlan_queue_size();
2159 }
2160
2161 \f
2162 /* Spanning Tree. */
2163
2164 /* Called while rstp_mutex is held. */
2165 static void
2166 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2167 {
2168     struct ofproto_dpif *ofproto = ofproto_;
2169     struct ofport_dpif *ofport = ofport_;
2170     struct eth_header *eth = dp_packet_l2(pkt);
2171
2172     netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
2173     if (eth_addr_is_zero(eth->eth_src)) {
2174         VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2175                      "does not have a configured source MAC address.",
2176                      ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2177     } else {
2178         ofproto_dpif_send_packet(ofport, pkt);
2179     }
2180     dp_packet_delete(pkt);
2181 }
2182
2183 static void
2184 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2185 {
2186     struct ofproto_dpif *ofproto = ofproto_;
2187     struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2188     struct ofport_dpif *ofport;
2189
2190     ofport = stp_port_get_aux(sp);
2191     if (!ofport) {
2192         VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2193                      ofproto->up.name, port_num);
2194     } else {
2195         struct eth_header *eth = dp_packet_l2(pkt);
2196
2197         netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
2198         if (eth_addr_is_zero(eth->eth_src)) {
2199             VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2200                          "with unknown MAC", ofproto->up.name, port_num);
2201         } else {
2202             ofproto_dpif_send_packet(ofport, pkt);
2203         }
2204     }
2205     dp_packet_delete(pkt);
2206 }
2207
2208 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2209 static void
2210 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2211 {
2212     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2213
2214     /* Only revalidate flows if the configuration changed. */
2215     if (!s != !ofproto->rstp) {
2216         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2217     }
2218
2219     if (s) {
2220         if (!ofproto->rstp) {
2221             ofproto->rstp = rstp_create(ofproto_->name, s->address,
2222                                         rstp_send_bpdu_cb, ofproto);
2223             ofproto->rstp_last_tick = time_msec();
2224         }
2225         rstp_set_bridge_address(ofproto->rstp, s->address);
2226         rstp_set_bridge_priority(ofproto->rstp, s->priority);
2227         rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2228         rstp_set_bridge_force_protocol_version(ofproto->rstp,
2229                                                s->force_protocol_version);
2230         rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2231         rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2232         rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2233                                             s->transmit_hold_count);
2234     } else {
2235         struct ofport *ofport;
2236         HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2237             set_rstp_port(ofport, NULL);
2238         }
2239         rstp_unref(ofproto->rstp);
2240         ofproto->rstp = NULL;
2241     }
2242 }
2243
2244 static void
2245 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2246 {
2247     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2248
2249     if (ofproto->rstp) {
2250         s->enabled = true;
2251         s->root_id = rstp_get_root_id(ofproto->rstp);
2252         s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2253         s->designated_id = rstp_get_designated_id(ofproto->rstp);
2254         s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2255         s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2256         s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2257     } else {
2258         s->enabled = false;
2259     }
2260 }
2261
2262 static void
2263 update_rstp_port_state(struct ofport_dpif *ofport)
2264 {
2265     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2266     enum rstp_state state;
2267
2268     /* Figure out new state. */
2269     state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2270         : RSTP_DISABLED;
2271
2272     /* Update state. */
2273     if (ofport->rstp_state != state) {
2274         enum ofputil_port_state of_state;
2275         bool fwd_change;
2276
2277         VLOG_DBG("port %s: RSTP state changed from %s to %s",
2278                  netdev_get_name(ofport->up.netdev),
2279                  rstp_state_name(ofport->rstp_state),
2280                  rstp_state_name(state));
2281
2282         if (rstp_learn_in_state(ofport->rstp_state)
2283             != rstp_learn_in_state(state)) {
2284             /* XXX: Learning action flows should also be flushed. */
2285             if (ofport->bundle) {
2286                 if (!rstp_shift_root_learned_address(ofproto->rstp)
2287                     || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2288                     bundle_flush_macs(ofport->bundle, false);
2289                 }
2290             }
2291         }
2292         fwd_change = rstp_forward_in_state(ofport->rstp_state)
2293             != rstp_forward_in_state(state);
2294
2295         ofproto->backer->need_revalidate = REV_RSTP;
2296         ofport->rstp_state = state;
2297
2298         if (fwd_change && ofport->bundle) {
2299             bundle_update(ofport->bundle);
2300         }
2301
2302         /* Update the RSTP state bits in the OpenFlow port description. */
2303         of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2304         of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2305                 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2306                 : state == RSTP_DISCARDING ?  OFPUTIL_PS_STP_LISTEN
2307                 : 0);
2308         ofproto_port_set_state(&ofport->up, of_state);
2309     }
2310 }
2311
2312 static void
2313 rstp_run(struct ofproto_dpif *ofproto)
2314 {
2315     if (ofproto->rstp) {
2316         long long int now = time_msec();
2317         long long int elapsed = now - ofproto->rstp_last_tick;
2318         struct rstp_port *rp;
2319         struct ofport_dpif *ofport;
2320
2321         /* Every second, decrease the values of the timers. */
2322         if (elapsed >= 1000) {
2323             rstp_tick_timers(ofproto->rstp);
2324             ofproto->rstp_last_tick = now;
2325         }
2326         rp = NULL;
2327         while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2328             update_rstp_port_state(ofport);
2329         }
2330         rp = NULL;
2331         ofport = NULL;
2332         /* FIXME: This check should be done on-event (i.e., when setting
2333          * p->fdb_flush) and not periodically.
2334          */
2335         while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2336             if (!rstp_shift_root_learned_address(ofproto->rstp)
2337                 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2338                 bundle_flush_macs(ofport->bundle, false);
2339             }
2340         }
2341
2342         if (rstp_shift_root_learned_address(ofproto->rstp)) {
2343             struct ofport_dpif *old_root_aux =
2344                 (struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp);
2345             struct ofport_dpif *new_root_aux =
2346                 (struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp);
2347             if (old_root_aux != NULL && new_root_aux != NULL) {
2348                 bundle_move(old_root_aux->bundle, new_root_aux->bundle);
2349                 rstp_reset_root_changed(ofproto->rstp);
2350             }
2351         }
2352     }
2353 }
2354
2355 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2356 static int
2357 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2358 {
2359     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2360
2361     /* Only revalidate flows if the configuration changed. */
2362     if (!s != !ofproto->stp) {
2363         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2364     }
2365
2366     if (s) {
2367         if (!ofproto->stp) {
2368             ofproto->stp = stp_create(ofproto_->name, s->system_id,
2369                                       send_bpdu_cb, ofproto);
2370             ofproto->stp_last_tick = time_msec();
2371         }
2372
2373         stp_set_bridge_id(ofproto->stp, s->system_id);
2374         stp_set_bridge_priority(ofproto->stp, s->priority);
2375         stp_set_hello_time(ofproto->stp, s->hello_time);
2376         stp_set_max_age(ofproto->stp, s->max_age);
2377         stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2378     }  else {
2379         struct ofport *ofport;
2380
2381         HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2382             set_stp_port(ofport, NULL);
2383         }
2384
2385         stp_unref(ofproto->stp);
2386         ofproto->stp = NULL;
2387     }
2388
2389     return 0;
2390 }
2391
2392 static int
2393 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2394 {
2395     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2396
2397     if (ofproto->stp) {
2398         s->enabled = true;
2399         s->bridge_id = stp_get_bridge_id(ofproto->stp);
2400         s->designated_root = stp_get_designated_root(ofproto->stp);
2401         s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2402     } else {
2403         s->enabled = false;
2404     }
2405
2406     return 0;
2407 }
2408
2409 static void
2410 update_stp_port_state(struct ofport_dpif *ofport)
2411 {
2412     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2413     enum stp_state state;
2414
2415     /* Figure out new state. */
2416     state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2417                              : STP_DISABLED;
2418
2419     /* Update state. */
2420     if (ofport->stp_state != state) {
2421         enum ofputil_port_state of_state;
2422         bool fwd_change;
2423
2424         VLOG_DBG("port %s: STP state changed from %s to %s",
2425                  netdev_get_name(ofport->up.netdev),
2426                  stp_state_name(ofport->stp_state),
2427                  stp_state_name(state));
2428         if (stp_learn_in_state(ofport->stp_state)
2429                 != stp_learn_in_state(state)) {
2430             /* xxx Learning action flows should also be flushed. */
2431             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2432             mac_learning_flush(ofproto->ml);
2433             ovs_rwlock_unlock(&ofproto->ml->rwlock);
2434             mcast_snooping_mdb_flush(ofproto->ms);
2435         }
2436         fwd_change = stp_forward_in_state(ofport->stp_state)
2437                         != stp_forward_in_state(state);
2438
2439         ofproto->backer->need_revalidate = REV_STP;
2440         ofport->stp_state = state;
2441         ofport->stp_state_entered = time_msec();
2442
2443         if (fwd_change && ofport->bundle) {
2444             bundle_update(ofport->bundle);
2445         }
2446
2447         /* Update the STP state bits in the OpenFlow port description. */
2448         of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2449         of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2450                      : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2451                      : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2452                      : state == STP_BLOCKING ?  OFPUTIL_PS_STP_BLOCK
2453                      : 0);
2454         ofproto_port_set_state(&ofport->up, of_state);
2455     }
2456 }
2457
2458 /* Configures STP on 'ofport_' using the settings defined in 's'.  The
2459  * caller is responsible for assigning STP port numbers and ensuring
2460  * there are no duplicates. */
2461 static int
2462 set_stp_port(struct ofport *ofport_,
2463              const struct ofproto_port_stp_settings *s)
2464 {
2465     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2466     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2467     struct stp_port *sp = ofport->stp_port;
2468
2469     if (!s || !s->enable) {
2470         if (sp) {
2471             ofport->stp_port = NULL;
2472             stp_port_disable(sp);
2473             update_stp_port_state(ofport);
2474         }
2475         return 0;
2476     } else if (sp && stp_port_no(sp) != s->port_num
2477                && ofport == stp_port_get_aux(sp)) {
2478         /* The port-id changed, so disable the old one if it's not
2479          * already in use by another port. */
2480         stp_port_disable(sp);
2481     }
2482
2483     sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2484
2485     /* Set name before enabling the port so that debugging messages can print
2486      * the name. */
2487     stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2488     stp_port_enable(sp);
2489
2490     stp_port_set_aux(sp, ofport);
2491     stp_port_set_priority(sp, s->priority);
2492     stp_port_set_path_cost(sp, s->path_cost);
2493
2494     update_stp_port_state(ofport);
2495
2496     return 0;
2497 }
2498
2499 static int
2500 get_stp_port_status(struct ofport *ofport_,
2501                     struct ofproto_port_stp_status *s)
2502 {
2503     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2504     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2505     struct stp_port *sp = ofport->stp_port;
2506
2507     if (!ofproto->stp || !sp) {
2508         s->enabled = false;
2509         return 0;
2510     }
2511
2512     s->enabled = true;
2513     s->port_id = stp_port_get_id(sp);
2514     s->state = stp_port_get_state(sp);
2515     s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2516     s->role = stp_port_get_role(sp);
2517
2518     return 0;
2519 }
2520
2521 static int
2522 get_stp_port_stats(struct ofport *ofport_,
2523                    struct ofproto_port_stp_stats *s)
2524 {
2525     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2526     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2527     struct stp_port *sp = ofport->stp_port;
2528
2529     if (!ofproto->stp || !sp) {
2530         s->enabled = false;
2531         return 0;
2532     }
2533
2534     s->enabled = true;
2535     stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2536
2537     return 0;
2538 }
2539
2540 static void
2541 stp_run(struct ofproto_dpif *ofproto)
2542 {
2543     if (ofproto->stp) {
2544         long long int now = time_msec();
2545         long long int elapsed = now - ofproto->stp_last_tick;
2546         struct stp_port *sp;
2547
2548         if (elapsed > 0) {
2549             stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2550             ofproto->stp_last_tick = now;
2551         }
2552         while (stp_get_changed_port(ofproto->stp, &sp)) {
2553             struct ofport_dpif *ofport = stp_port_get_aux(sp);
2554
2555             if (ofport) {
2556                 update_stp_port_state(ofport);
2557             }
2558         }
2559
2560         if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2561             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2562             mac_learning_flush(ofproto->ml);
2563             ovs_rwlock_unlock(&ofproto->ml->rwlock);
2564             mcast_snooping_mdb_flush(ofproto->ms);
2565         }
2566     }
2567 }
2568
2569 static void
2570 stp_wait(struct ofproto_dpif *ofproto)
2571 {
2572     if (ofproto->stp) {
2573         poll_timer_wait(1000);
2574     }
2575 }
2576
2577 /* Configures RSTP on 'ofport_' using the settings defined in 's'.  The
2578  * caller is responsible for assigning RSTP port numbers and ensuring
2579  * there are no duplicates. */
2580 static void
2581 set_rstp_port(struct ofport *ofport_,
2582               const struct ofproto_port_rstp_settings *s)
2583 {
2584     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2585     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2586     struct rstp_port *rp = ofport->rstp_port;
2587
2588     if (!s || !s->enable) {
2589         if (rp) {
2590             rstp_port_set_aux(rp, NULL);
2591             rstp_port_set_state(rp, RSTP_DISABLED);
2592             rstp_port_set_mac_operational(rp, false);
2593             ofport->rstp_port = NULL;
2594             rstp_port_unref(rp);
2595             update_rstp_port_state(ofport);
2596         }
2597         return;
2598     }
2599
2600     /* Check if need to add a new port. */
2601     if (!rp) {
2602         rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2603     }
2604
2605     rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2606                   s->admin_edge_port, s->auto_edge,
2607                   s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2608                   ofport);
2609     update_rstp_port_state(ofport);
2610     /* Synchronize operational status. */
2611     rstp_port_set_mac_operational(rp, ofport->may_enable);
2612 }
2613
2614 static void
2615 get_rstp_port_status(struct ofport *ofport_,
2616         struct ofproto_port_rstp_status *s)
2617 {
2618     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2619     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2620     struct rstp_port *rp = ofport->rstp_port;
2621
2622     if (!ofproto->rstp || !rp) {
2623         s->enabled = false;
2624         return;
2625     }
2626
2627     s->enabled = true;
2628     rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2629                          &s->designated_bridge_id, &s->designated_port_id,
2630                          &s->designated_path_cost, &s->tx_count,
2631                          &s->rx_count, &s->error_count, &s->uptime);
2632 }
2633
2634 \f
2635 static int
2636 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2637            size_t n_qdscp)
2638 {
2639     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2640     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2641
2642     if (ofport->n_qdscp != n_qdscp
2643         || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2644                               n_qdscp * sizeof *qdscp))) {
2645         ofproto->backer->need_revalidate = REV_RECONFIGURE;
2646         free(ofport->qdscp);
2647         ofport->qdscp = n_qdscp
2648             ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2649             : NULL;
2650         ofport->n_qdscp = n_qdscp;
2651     }
2652
2653     return 0;
2654 }
2655 \f
2656 /* Bundles. */
2657
2658 /* Expires all MAC learning entries associated with 'bundle' and forces its
2659  * ofproto to revalidate every flow.
2660  *
2661  * Normally MAC learning entries are removed only from the ofproto associated
2662  * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2663  * are removed from every ofproto.  When patch ports and SLB bonds are in use
2664  * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2665  * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2666  * with the host from which it migrated. */
2667 static void
2668 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2669 {
2670     struct ofproto_dpif *ofproto = bundle->ofproto;
2671     struct mac_learning *ml = ofproto->ml;
2672     struct mac_entry *mac, *next_mac;
2673
2674     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2675     ovs_rwlock_wrlock(&ml->rwlock);
2676     LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2677         if (mac_entry_get_port(ml, mac) == bundle) {
2678             if (all_ofprotos) {
2679                 struct ofproto_dpif *o;
2680
2681                 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2682                     if (o != ofproto) {
2683                         struct mac_entry *e;
2684
2685                         ovs_rwlock_wrlock(&o->ml->rwlock);
2686                         e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2687                         if (e) {
2688                             mac_learning_expire(o->ml, e);
2689                         }
2690                         ovs_rwlock_unlock(&o->ml->rwlock);
2691                     }
2692                 }
2693             }
2694
2695             mac_learning_expire(ml, mac);
2696         }
2697     }
2698     ovs_rwlock_unlock(&ml->rwlock);
2699 }
2700
2701 static void
2702 bundle_move(struct ofbundle *old, struct ofbundle *new)
2703 {
2704     struct ofproto_dpif *ofproto = old->ofproto;
2705     struct mac_learning *ml = ofproto->ml;
2706     struct mac_entry *mac, *next_mac;
2707
2708     ovs_assert(new->ofproto == old->ofproto);
2709
2710     ofproto->backer->need_revalidate = REV_RECONFIGURE;
2711     ovs_rwlock_wrlock(&ml->rwlock);
2712     LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2713         if (mac_entry_get_port(ml, mac) == old) {
2714             mac_entry_set_port(ml, mac, new);
2715         }
2716     }
2717     ovs_rwlock_unlock(&ml->rwlock);
2718 }
2719
2720 static struct ofbundle *
2721 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2722 {
2723     struct ofbundle *bundle;
2724
2725     HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2726                              &ofproto->bundles) {
2727         if (bundle->aux == aux) {
2728             return bundle;
2729         }
2730     }
2731     return NULL;
2732 }
2733
2734 static void
2735 bundle_update(struct ofbundle *bundle)
2736 {
2737     struct ofport_dpif *port;
2738
2739     bundle->floodable = true;
2740     LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2741         if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2742             || port->is_layer3
2743             || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2744             || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2745             bundle->floodable = false;
2746             break;
2747         }
2748     }
2749 }
2750
2751 static void
2752 bundle_del_port(struct ofport_dpif *port)
2753 {
2754     struct ofbundle *bundle = port->bundle;
2755
2756     bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2757
2758     list_remove(&port->bundle_node);
2759     port->bundle = NULL;
2760
2761     if (bundle->lacp) {
2762         lacp_slave_unregister(bundle->lacp, port);
2763     }
2764     if (bundle->bond) {
2765         bond_slave_unregister(bundle->bond, port);
2766     }
2767
2768     bundle_update(bundle);
2769 }
2770
2771 static bool
2772 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2773                 struct lacp_slave_settings *lacp)
2774 {
2775     struct ofport_dpif *port;
2776
2777     port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2778     if (!port) {
2779         return false;
2780     }
2781
2782     if (port->bundle != bundle) {
2783         bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2784         if (port->bundle) {
2785             bundle_remove(&port->up);
2786         }
2787
2788         port->bundle = bundle;
2789         list_push_back(&bundle->ports, &port->bundle_node);
2790         if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2791             || port->is_layer3
2792             || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2793             || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2794             bundle->floodable = false;
2795         }
2796     }
2797     if (lacp) {
2798         bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2799         lacp_slave_register(bundle->lacp, port, lacp);
2800     }
2801
2802     return true;
2803 }
2804
2805 static void
2806 bundle_destroy(struct ofbundle *bundle)
2807 {
2808     struct ofproto_dpif *ofproto;
2809     struct ofport_dpif *port, *next_port;
2810
2811     if (!bundle) {
2812         return;
2813     }
2814
2815     ofproto = bundle->ofproto;
2816     mbridge_unregister_bundle(ofproto->mbridge, bundle);
2817
2818     xlate_txn_start();
2819     xlate_bundle_remove(bundle);
2820     xlate_txn_commit();
2821
2822     LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2823         bundle_del_port(port);
2824     }
2825
2826     bundle_flush_macs(bundle, true);
2827     hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2828     free(bundle->name);
2829     free(bundle->trunks);
2830     lacp_unref(bundle->lacp);
2831     bond_unref(bundle->bond);
2832     free(bundle);
2833 }
2834
2835 static int
2836 bundle_set(struct ofproto *ofproto_, void *aux,
2837            const struct ofproto_bundle_settings *s)
2838 {
2839     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2840     bool need_flush = false;
2841     struct ofport_dpif *port;
2842     struct ofbundle *bundle;
2843     unsigned long *trunks;
2844     int vlan;
2845     size_t i;
2846     bool ok;
2847
2848     if (!s) {
2849         bundle_destroy(bundle_lookup(ofproto, aux));
2850         return 0;
2851     }
2852
2853     ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2854     ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2855
2856     bundle = bundle_lookup(ofproto, aux);
2857     if (!bundle) {
2858         bundle = xmalloc(sizeof *bundle);
2859
2860         bundle->ofproto = ofproto;
2861         hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2862                     hash_pointer(aux, 0));
2863         bundle->aux = aux;
2864         bundle->name = NULL;
2865
2866         list_init(&bundle->ports);
2867         bundle->vlan_mode = PORT_VLAN_TRUNK;
2868         bundle->vlan = -1;
2869         bundle->trunks = NULL;
2870         bundle->use_priority_tags = s->use_priority_tags;
2871         bundle->lacp = NULL;
2872         bundle->bond = NULL;
2873
2874         bundle->floodable = true;
2875         mbridge_register_bundle(ofproto->mbridge, bundle);
2876     }
2877
2878     if (!bundle->name || strcmp(s->name, bundle->name)) {
2879         free(bundle->name);
2880         bundle->name = xstrdup(s->name);
2881     }
2882
2883     /* LACP. */
2884     if (s->lacp) {
2885         ofproto->lacp_enabled = true;
2886         if (!bundle->lacp) {
2887             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2888             bundle->lacp = lacp_create();
2889         }
2890         lacp_configure(bundle->lacp, s->lacp);
2891     } else {
2892         lacp_unref(bundle->lacp);
2893         bundle->lacp = NULL;
2894     }
2895
2896     /* Update set of ports. */
2897     ok = true;
2898     for (i = 0; i < s->n_slaves; i++) {
2899         if (!bundle_add_port(bundle, s->slaves[i],
2900                              s->lacp ? &s->lacp_slaves[i] : NULL)) {
2901             ok = false;
2902         }
2903     }
2904     if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2905         struct ofport_dpif *next_port;
2906
2907         LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2908             for (i = 0; i < s->n_slaves; i++) {
2909                 if (s->slaves[i] == port->up.ofp_port) {
2910                     goto found;
2911                 }
2912             }
2913
2914             bundle_del_port(port);
2915         found: ;
2916         }
2917     }
2918     ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2919
2920     if (list_is_empty(&bundle->ports)) {
2921         bundle_destroy(bundle);
2922         return EINVAL;
2923     }
2924
2925     /* Set VLAN tagging mode */
2926     if (s->vlan_mode != bundle->vlan_mode
2927         || s->use_priority_tags != bundle->use_priority_tags) {
2928         bundle->vlan_mode = s->vlan_mode;
2929         bundle->use_priority_tags = s->use_priority_tags;
2930         need_flush = true;
2931     }
2932
2933     /* Set VLAN tag. */
2934     vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2935             : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2936             : 0);
2937     if (vlan != bundle->vlan) {
2938         bundle->vlan = vlan;
2939         need_flush = true;
2940     }
2941
2942     /* Get trunked VLANs. */
2943     switch (s->vlan_mode) {
2944     case PORT_VLAN_ACCESS:
2945         trunks = NULL;
2946         break;
2947
2948     case PORT_VLAN_TRUNK:
2949         trunks = CONST_CAST(unsigned long *, s->trunks);
2950         break;
2951
2952     case PORT_VLAN_NATIVE_UNTAGGED:
2953     case PORT_VLAN_NATIVE_TAGGED:
2954         if (vlan != 0 && (!s->trunks
2955                           || !bitmap_is_set(s->trunks, vlan)
2956                           || bitmap_is_set(s->trunks, 0))) {
2957             /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2958             if (s->trunks) {
2959                 trunks = bitmap_clone(s->trunks, 4096);
2960             } else {
2961                 trunks = bitmap_allocate1(4096);
2962             }
2963             bitmap_set1(trunks, vlan);
2964             bitmap_set0(trunks, 0);
2965         } else {
2966             trunks = CONST_CAST(unsigned long *, s->trunks);
2967         }
2968         break;
2969
2970     default:
2971         OVS_NOT_REACHED();
2972     }
2973     if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2974         free(bundle->trunks);
2975         if (trunks == s->trunks) {
2976             bundle->trunks = vlan_bitmap_clone(trunks);
2977         } else {
2978             bundle->trunks = trunks;
2979             trunks = NULL;
2980         }
2981         need_flush = true;
2982     }
2983     if (trunks != s->trunks) {
2984         free(trunks);
2985     }
2986
2987     /* Bonding. */
2988     if (!list_is_short(&bundle->ports)) {
2989         bundle->ofproto->has_bonded_bundles = true;
2990         if (bundle->bond) {
2991             if (bond_reconfigure(bundle->bond, s->bond)) {
2992                 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2993             }
2994         } else {
2995             bundle->bond = bond_create(s->bond, ofproto);
2996             ofproto->backer->need_revalidate = REV_RECONFIGURE;
2997         }
2998
2999         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3000             bond_slave_register(bundle->bond, port,
3001                                 port->up.ofp_port, port->up.netdev);
3002         }
3003     } else {
3004         bond_unref(bundle->bond);
3005         bundle->bond = NULL;
3006     }
3007
3008     /* If we changed something that would affect MAC learning, un-learn
3009      * everything on this port and force flow revalidation. */
3010     if (need_flush) {
3011         bundle_flush_macs(bundle, false);
3012     }
3013
3014     return 0;
3015 }
3016
3017 static void
3018 bundle_remove(struct ofport *port_)
3019 {
3020     struct ofport_dpif *port = ofport_dpif_cast(port_);
3021     struct ofbundle *bundle = port->bundle;
3022
3023     if (bundle) {
3024         bundle_del_port(port);
3025         if (list_is_empty(&bundle->ports)) {
3026             bundle_destroy(bundle);
3027         } else if (list_is_short(&bundle->ports)) {
3028             bond_unref(bundle->bond);
3029             bundle->bond = NULL;
3030         }
3031     }
3032 }
3033
3034 static void
3035 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
3036 {
3037     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3038     struct ofport_dpif *port = port_;
3039     struct eth_addr ea;
3040     int error;
3041
3042     error = netdev_get_etheraddr(port->up.netdev, &ea);
3043     if (!error) {
3044         struct dp_packet packet;
3045         void *packet_pdu;
3046
3047         dp_packet_init(&packet, 0);
3048         packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
3049                                  pdu_size);
3050         memcpy(packet_pdu, pdu, pdu_size);
3051
3052         ofproto_dpif_send_packet(port, &packet);
3053         dp_packet_uninit(&packet);
3054     } else {
3055         VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
3056                     "%s (%s)", port->bundle->name,
3057                     netdev_get_name(port->up.netdev), ovs_strerror(error));
3058     }
3059 }
3060
3061 static void
3062 bundle_send_learning_packets(struct ofbundle *bundle)
3063 {
3064     struct ofproto_dpif *ofproto = bundle->ofproto;
3065     int error, n_packets, n_errors;
3066     struct mac_entry *e;
3067     struct pkt_list {
3068         struct ovs_list list_node;
3069         struct ofport_dpif *port;
3070         struct dp_packet *pkt;
3071     } *pkt_node;
3072     struct ovs_list packets;
3073
3074     list_init(&packets);
3075     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3076     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3077         if (mac_entry_get_port(ofproto->ml, e) != bundle) {
3078             pkt_node = xmalloc(sizeof *pkt_node);
3079             pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3080                                                          e->mac, e->vlan,
3081                                                          (void **)&pkt_node->port);
3082             list_push_back(&packets, &pkt_node->list_node);
3083         }
3084     }
3085     ovs_rwlock_unlock(&ofproto->ml->rwlock);
3086
3087     error = n_packets = n_errors = 0;
3088     LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
3089         int ret;
3090
3091         ret = ofproto_dpif_send_packet(pkt_node->port, pkt_node->pkt);
3092         dp_packet_delete(pkt_node->pkt);
3093         free(pkt_node);
3094         if (ret) {
3095             error = ret;
3096             n_errors++;
3097         }
3098         n_packets++;
3099     }
3100
3101     if (n_errors) {
3102         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3103         VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3104                      "packets, last error was: %s",
3105                      bundle->name, n_errors, n_packets, ovs_strerror(error));
3106     } else {
3107         VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3108                  bundle->name, n_packets);
3109     }
3110 }
3111
3112 static void
3113 bundle_run(struct ofbundle *bundle)
3114 {
3115     if (bundle->lacp) {
3116         lacp_run(bundle->lacp, send_pdu_cb);
3117     }
3118     if (bundle->bond) {
3119         struct ofport_dpif *port;
3120
3121         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3122             bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3123         }
3124
3125         if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3126             bundle->ofproto->backer->need_revalidate = REV_BOND;
3127         }
3128
3129         if (bond_should_send_learning_packets(bundle->bond)) {
3130             bundle_send_learning_packets(bundle);
3131         }
3132     }
3133 }
3134
3135 static void
3136 bundle_wait(struct ofbundle *bundle)
3137 {
3138     if (bundle->lacp) {
3139         lacp_wait(bundle->lacp);
3140     }
3141     if (bundle->bond) {
3142         bond_wait(bundle->bond);
3143     }
3144 }
3145 \f
3146 /* Mirrors. */
3147
3148 static int
3149 mirror_set__(struct ofproto *ofproto_, void *aux,
3150              const struct ofproto_mirror_settings *s)
3151 {
3152     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3153     struct ofbundle **srcs, **dsts;
3154     int error;
3155     size_t i;
3156
3157     if (!s) {
3158         mirror_destroy(ofproto->mbridge, aux);
3159         return 0;
3160     }
3161
3162     srcs = xmalloc(s->n_srcs * sizeof *srcs);
3163     dsts = xmalloc(s->n_dsts * sizeof *dsts);
3164
3165     for (i = 0; i < s->n_srcs; i++) {
3166         srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3167     }
3168
3169     for (i = 0; i < s->n_dsts; i++) {
3170         dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3171     }
3172
3173     error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3174                        s->n_dsts, s->src_vlans,
3175                        bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
3176     free(srcs);
3177     free(dsts);
3178     return error;
3179 }
3180
3181 static int
3182 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3183                    uint64_t *packets, uint64_t *bytes)
3184 {
3185     return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3186                             bytes);
3187 }
3188
3189 static int
3190 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3191 {
3192     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3193     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3194     if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3195         mac_learning_flush(ofproto->ml);
3196     }
3197     ovs_rwlock_unlock(&ofproto->ml->rwlock);
3198     return 0;
3199 }
3200
3201 static bool
3202 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3203 {
3204     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3205     struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3206     return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3207 }
3208
3209 static void
3210 forward_bpdu_changed(struct ofproto *ofproto_)
3211 {
3212     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3213     ofproto->backer->need_revalidate = REV_RECONFIGURE;
3214 }
3215
3216 static void
3217 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3218                      size_t max_entries)
3219 {
3220     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3221     ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3222     mac_learning_set_idle_time(ofproto->ml, idle_time);
3223     mac_learning_set_max_entries(ofproto->ml, max_entries);
3224     ovs_rwlock_unlock(&ofproto->ml->rwlock);
3225 }
3226
3227 /* Configures multicast snooping on 'ofport' using the settings
3228  * defined in 's'. */
3229 static int
3230 set_mcast_snooping(struct ofproto *ofproto_,
3231                    const struct ofproto_mcast_snooping_settings *s)
3232 {
3233     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3234
3235     /* Only revalidate flows if the configuration changed. */
3236     if (!s != !ofproto->ms) {
3237         ofproto->backer->need_revalidate = REV_RECONFIGURE;
3238     }
3239
3240     if (s) {
3241         if (!ofproto->ms) {
3242             ofproto->ms = mcast_snooping_create();
3243         }
3244
3245         ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3246         mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3247         mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3248         if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3249             ofproto->backer->need_revalidate = REV_RECONFIGURE;
3250         }
3251         ovs_rwlock_unlock(&ofproto->ms->rwlock);
3252     } else {
3253         mcast_snooping_unref(ofproto->ms);
3254         ofproto->ms = NULL;
3255     }
3256
3257     return 0;
3258 }
3259
3260 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3261 static int
3262 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3263                         const struct ofproto_mcast_snooping_port_settings *s)
3264 {
3265     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3266     struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3267
3268     if (ofproto->ms && s) {
3269         ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3270         mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3271         mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3272                                               s->flood_reports);
3273         ovs_rwlock_unlock(&ofproto->ms->rwlock);
3274     }
3275     return 0;
3276 }
3277
3278 \f
3279 /* Ports. */
3280
3281 struct ofport_dpif *
3282 ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3283 {
3284     struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3285     return ofport ? ofport_dpif_cast(ofport) : NULL;
3286 }
3287
3288 static void
3289 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3290                             struct ofproto_port *ofproto_port,
3291                             struct dpif_port *dpif_port)
3292 {
3293     ofproto_port->name = dpif_port->name;
3294     ofproto_port->type = dpif_port->type;
3295     ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3296 }
3297
3298 static void
3299 ofport_update_peer(struct ofport_dpif *ofport)
3300 {
3301     const struct ofproto_dpif *ofproto;
3302     struct dpif_backer *backer;
3303     char *peer_name;
3304
3305     if (!netdev_vport_is_patch(ofport->up.netdev)) {
3306         return;
3307     }
3308
3309     backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3310     backer->need_revalidate = REV_RECONFIGURE;
3311
3312     if (ofport->peer) {
3313         ofport->peer->peer = NULL;
3314         ofport->peer = NULL;
3315     }
3316
3317     peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3318     if (!peer_name) {
3319         return;
3320     }
3321
3322     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3323         struct ofport *peer_ofport;
3324         struct ofport_dpif *peer;
3325         char *peer_peer;
3326
3327         if (ofproto->backer != backer) {
3328             continue;
3329         }
3330
3331         peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3332         if (!peer_ofport) {
3333             continue;
3334         }
3335
3336         peer = ofport_dpif_cast(peer_ofport);
3337         peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3338         if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3339                                  peer_peer)) {
3340             ofport->peer = peer;
3341             ofport->peer->peer = ofport;
3342         }
3343         free(peer_peer);
3344
3345         break;
3346     }
3347     free(peer_name);
3348 }
3349
3350 static void
3351 port_run(struct ofport_dpif *ofport)
3352 {
3353     long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3354     bool carrier_changed = carrier_seq != ofport->carrier_seq;
3355     bool enable = netdev_get_carrier(ofport->up.netdev);
3356     bool cfm_enable = false;
3357     bool bfd_enable = false;
3358
3359     ofport->carrier_seq = carrier_seq;
3360
3361     if (ofport->cfm) {
3362         int cfm_opup = cfm_get_opup(ofport->cfm);
3363
3364         cfm_enable = !cfm_get_fault(ofport->cfm);
3365
3366         if (cfm_opup >= 0) {
3367             cfm_enable = cfm_enable && cfm_opup;
3368         }
3369     }
3370
3371     if (ofport->bfd) {
3372         bfd_enable = bfd_forwarding(ofport->bfd);
3373     }
3374
3375     if (ofport->bfd || ofport->cfm) {
3376         enable = enable && (cfm_enable || bfd_enable);
3377     }
3378
3379     if (ofport->bundle) {
3380         enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3381         if (carrier_changed) {
3382             lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3383         }
3384     }
3385
3386     if (ofport->may_enable != enable) {
3387         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3388
3389         ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3390
3391         if (ofport->rstp_port) {
3392             rstp_port_set_mac_operational(ofport->rstp_port, enable);
3393         }
3394     }
3395
3396     ofport->may_enable = enable;
3397 }
3398
3399 static int
3400 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3401                    struct ofproto_port *ofproto_port)
3402 {
3403     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3404     struct dpif_port dpif_port;
3405     int error;
3406
3407     if (sset_contains(&ofproto->ghost_ports, devname)) {
3408         const char *type = netdev_get_type_from_name(devname);
3409
3410         /* We may be called before ofproto->up.port_by_name is populated with
3411          * the appropriate ofport.  For this reason, we must get the name and
3412          * type from the netdev layer directly. */
3413         if (type) {
3414             const struct ofport *ofport;
3415
3416             ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3417             ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3418             ofproto_port->name = xstrdup(devname);
3419             ofproto_port->type = xstrdup(type);
3420             return 0;
3421         }
3422         return ENODEV;
3423     }
3424
3425     if (!sset_contains(&ofproto->ports, devname)) {
3426         return ENODEV;
3427     }
3428     error = dpif_port_query_by_name(ofproto->backer->dpif,
3429                                     devname, &dpif_port);
3430     if (!error) {
3431         ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3432     }
3433     return error;
3434 }
3435
3436 static int
3437 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3438 {
3439     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3440     const char *devname = netdev_get_name(netdev);
3441     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3442     const char *dp_port_name;
3443
3444     if (netdev_vport_is_patch(netdev)) {
3445         sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3446         return 0;
3447     }
3448
3449     dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3450     if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3451         odp_port_t port_no = ODPP_NONE;
3452         int error;
3453
3454         error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3455         if (error) {
3456             return error;
3457         }
3458         if (netdev_get_tunnel_config(netdev)) {
3459             simap_put(&ofproto->backer->tnl_backers,
3460                       dp_port_name, odp_to_u32(port_no));
3461         }
3462     }
3463
3464     if (netdev_get_tunnel_config(netdev)) {
3465         sset_add(&ofproto->ghost_ports, devname);
3466     } else {
3467         sset_add(&ofproto->ports, devname);
3468     }
3469     return 0;
3470 }
3471
3472 static int
3473 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3474 {
3475     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3476     struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3477     int error = 0;
3478
3479     if (!ofport) {
3480         return 0;
3481     }
3482
3483     sset_find_and_delete(&ofproto->ghost_ports,
3484                          netdev_get_name(ofport->up.netdev));
3485     ofproto->backer->need_revalidate = REV_RECONFIGURE;
3486     if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3487         error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3488         if (!error) {
3489             /* The caller is going to close ofport->up.netdev.  If this is a
3490              * bonded port, then the bond is using that netdev, so remove it
3491              * from the bond.  The client will need to reconfigure everything
3492              * after deleting ports, so then the slave will get re-added. */
3493             bundle_remove(&ofport->up);
3494         }
3495     }
3496     return error;
3497 }
3498
3499 static int
3500 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3501 {
3502     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3503     int error;
3504
3505     error = netdev_get_stats(ofport->up.netdev, stats);
3506
3507     if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3508         struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3509
3510         ovs_mutex_lock(&ofproto->stats_mutex);
3511         /* ofproto->stats.tx_packets represents packets that we created
3512          * internally and sent to some port (e.g. packets sent with
3513          * ofproto_dpif_send_packet()).  Account for them as if they had
3514          * come from OFPP_LOCAL and got forwarded. */
3515
3516         if (stats->rx_packets != UINT64_MAX) {
3517             stats->rx_packets += ofproto->stats.tx_packets;
3518         }
3519
3520         if (stats->rx_bytes != UINT64_MAX) {
3521             stats->rx_bytes += ofproto->stats.tx_bytes;
3522         }
3523
3524         /* ofproto->stats.rx_packets represents packets that were received on
3525          * some port and we processed internally and dropped (e.g. STP).
3526          * Account for them as if they had been forwarded to OFPP_LOCAL. */
3527
3528         if (stats->tx_packets != UINT64_MAX) {
3529             stats->tx_packets += ofproto->stats.rx_packets;
3530         }
3531
3532         if (stats->tx_bytes != UINT64_MAX) {
3533             stats->tx_bytes += ofproto->stats.rx_bytes;
3534         }
3535         ovs_mutex_unlock(&ofproto->stats_mutex);
3536     }
3537
3538     return error;
3539 }
3540
3541 static int
3542 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3543 {
3544     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3545     if (ofport->bundle && ofport->bundle->lacp) {
3546         if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3547             return 0;
3548         }
3549     }
3550     return -1;
3551 }
3552
3553 struct port_dump_state {
3554     uint32_t bucket;
3555     uint32_t offset;
3556     bool ghost;
3557
3558     struct ofproto_port port;
3559     bool has_port;
3560 };
3561
3562 static int
3563 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3564 {
3565     *statep = xzalloc(sizeof(struct port_dump_state));
3566     return 0;
3567 }
3568
3569 static int
3570 port_dump_next(const struct ofproto *ofproto_, void *state_,
3571                struct ofproto_port *port)
3572 {
3573     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3574     struct port_dump_state *state = state_;
3575     const struct sset *sset;
3576     struct sset_node *node;
3577
3578     if (state->has_port) {
3579         ofproto_port_destroy(&state->port);
3580         state->has_port = false;
3581     }
3582     sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3583     while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3584         int error;
3585
3586         error = port_query_by_name(ofproto_, node->name, &state->port);
3587         if (!error) {
3588             *port = state->port;
3589             state->has_port = true;
3590             return 0;
3591         } else if (error != ENODEV) {
3592             return error;
3593         }
3594     }
3595
3596     if (!state->ghost) {
3597         state->ghost = true;
3598         state->bucket = 0;
3599         state->offset = 0;
3600         return port_dump_next(ofproto_, state_, port);
3601     }
3602
3603     return EOF;
3604 }
3605
3606 static int
3607 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3608 {
3609     struct port_dump_state *state = state_;
3610
3611     if (state->has_port) {
3612         ofproto_port_destroy(&state->port);
3613     }
3614     free(state);
3615     return 0;
3616 }
3617
3618 static int
3619 port_poll(const struct ofproto *ofproto_, char **devnamep)
3620 {
3621     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3622
3623     if (ofproto->port_poll_errno) {
3624         int error = ofproto->port_poll_errno;
3625         ofproto->port_poll_errno = 0;
3626         return error;
3627     }
3628
3629     if (sset_is_empty(&ofproto->port_poll_set)) {
3630         return EAGAIN;
3631     }
3632
3633     *devnamep = sset_pop(&ofproto->port_poll_set);
3634     return 0;
3635 }
3636
3637 static void
3638 port_poll_wait(const struct ofproto *ofproto_)
3639 {
3640     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3641     dpif_port_poll_wait(ofproto->backer->dpif);
3642 }
3643
3644 static int
3645 port_is_lacp_current(const struct ofport *ofport_)
3646 {
3647     const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3648     return (ofport->bundle && ofport->bundle->lacp
3649             ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3650             : -1);
3651 }
3652 \f
3653 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3654  * then delete it entirely. */
3655 static void
3656 rule_expire(struct rule_dpif *rule)
3657     OVS_REQUIRES(ofproto_mutex)
3658 {
3659     uint16_t hard_timeout, idle_timeout;
3660     long long int now = time_msec();
3661     int reason = -1;
3662
3663     hard_timeout = rule->up.hard_timeout;
3664     idle_timeout = rule->up.idle_timeout;
3665
3666     /* Has 'rule' expired? */
3667     if (hard_timeout) {
3668         long long int modified;
3669
3670         ovs_mutex_lock(&rule->up.mutex);
3671         modified = rule->up.modified;
3672         ovs_mutex_unlock(&rule->up.mutex);
3673
3674         if (now > modified + hard_timeout * 1000) {
3675             reason = OFPRR_HARD_TIMEOUT;
3676         }
3677     }
3678
3679     if (reason < 0 && idle_timeout) {
3680         long long int used;
3681
3682         ovs_mutex_lock(&rule->stats_mutex);
3683         used = rule->stats.used;
3684         ovs_mutex_unlock(&rule->stats_mutex);
3685
3686         if (now > used + idle_timeout * 1000) {
3687             reason = OFPRR_IDLE_TIMEOUT;
3688         }
3689     }
3690
3691     if (reason >= 0) {
3692         COVERAGE_INC(ofproto_dpif_expired);
3693         ofproto_rule_expire(&rule->up, reason);
3694     }
3695 }
3696
3697 int
3698 ofproto_dpif_execute_actions__(struct ofproto_dpif *ofproto,
3699                                const struct flow *flow,
3700                                struct rule_dpif *rule,
3701                                const struct ofpact *ofpacts, size_t ofpacts_len,
3702                                int recurse, int resubmits,
3703                                struct dp_packet *packet)
3704 {
3705     struct dpif_flow_stats stats;
3706     struct xlate_out xout;
3707     struct xlate_in xin;
3708     ofp_port_t in_port;
3709     struct dpif_execute execute;
3710     int error;
3711
3712     ovs_assert((rule != NULL) != (ofpacts != NULL));
3713
3714     dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3715
3716     if (rule) {
3717         rule_dpif_credit_stats(rule, &stats);
3718     }
3719
3720     uint64_t odp_actions_stub[1024 / 8];
3721     struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
3722     xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3723                   stats.tcp_flags, packet, NULL, &odp_actions);
3724     xin.ofpacts = ofpacts;
3725     xin.ofpacts_len = ofpacts_len;
3726     xin.resubmit_stats = &stats;
3727     xin.recurse = recurse;
3728     xin.resubmits = resubmits;
3729     xlate_actions(&xin, &xout);
3730
3731     execute.actions = odp_actions.data;
3732     execute.actions_len = odp_actions.size;
3733
3734     pkt_metadata_from_flow(&packet->md, flow);
3735     execute.packet = packet;
3736     execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3737     execute.probe = false;
3738
3739     /* Fix up in_port. */
3740     in_port = flow->in_port.ofp_port;
3741     if (in_port == OFPP_NONE) {
3742         in_port = OFPP_LOCAL;
3743     }
3744     execute.packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3745
3746     error = dpif_execute(ofproto->backer->dpif, &execute);
3747
3748     xlate_out_uninit(&xout);
3749     ofpbuf_uninit(&odp_actions);
3750
3751     return error;
3752 }
3753
3754 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3755  * 'flow' must reflect the data in 'packet'. */
3756 int
3757 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3758                              const struct flow *flow,
3759                              struct rule_dpif *rule,
3760                              const struct ofpact *ofpacts, size_t ofpacts_len,
3761                              struct dp_packet *packet)
3762 {
3763     return ofproto_dpif_execute_actions__(ofproto, flow, rule, ofpacts,
3764                                           ofpacts_len, 0, 0, packet);
3765 }
3766
3767 void
3768 rule_dpif_credit_stats(struct rule_dpif *rule,
3769                        const struct dpif_flow_stats *stats)
3770 {
3771     ovs_mutex_lock(&rule->stats_mutex);
3772     if (OVS_UNLIKELY(rule->new_rule)) {
3773         rule_dpif_credit_stats(rule->new_rule, stats);
3774     } else {
3775         rule->stats.n_packets += stats->n_packets;
3776         rule->stats.n_bytes += stats->n_bytes;
3777         rule->stats.used = MAX(rule->stats.used, stats->used);
3778     }
3779     ovs_mutex_unlock(&rule->stats_mutex);
3780 }
3781
3782 ovs_be64
3783 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3784     OVS_REQUIRES(rule->up.mutex)
3785 {
3786     return rule->up.flow_cookie;
3787 }
3788
3789 void
3790 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3791                      uint16_t hard_timeout)
3792 {
3793     ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3794 }
3795
3796 /* Returns 'rule''s actions.  The returned actions are RCU-protected, and can
3797  * be read until the calling thread quiesces. */
3798 const struct rule_actions *
3799 rule_dpif_get_actions(const struct rule_dpif *rule)
3800 {
3801     return rule_get_actions(&rule->up);
3802 }
3803
3804 /* Sets 'rule''s recirculation id. */
3805 static void
3806 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3807     OVS_REQUIRES(rule->up.mutex)
3808 {
3809     ovs_assert(!rule->recirc_id || rule->recirc_id == id);
3810     if (rule->recirc_id == id) {
3811         /* Release the new reference to the same id. */
3812         recirc_free_id(id);
3813     } else {
3814         rule->recirc_id = id;
3815     }
3816 }
3817
3818 /* Sets 'rule''s recirculation id. */
3819 void
3820 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3821 {
3822     struct rule_dpif *rule = rule_dpif_cast(rule_);
3823
3824     ovs_mutex_lock(&rule->up.mutex);
3825     rule_dpif_set_recirc_id(rule, id);
3826     ovs_mutex_unlock(&rule->up.mutex);
3827 }
3828
3829 cls_version_t
3830 ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto OVS_UNUSED)
3831 {
3832     cls_version_t version;
3833
3834     atomic_read_relaxed(&ofproto->tables_version, &version);
3835
3836     return version;
3837 }
3838
3839 /* The returned rule (if any) is valid at least until the next RCU quiescent
3840  * period.  If the rule needs to stay around longer, the caller should take
3841  * a reference.
3842  *
3843  * 'flow' is non-const to allow for temporary modifications during the lookup.
3844  * Any changes are restored before returning. */
3845 static struct rule_dpif *
3846 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, cls_version_t version,
3847                           uint8_t table_id, struct flow *flow,
3848                           struct flow_wildcards *wc)
3849 {
3850     struct classifier *cls = &ofproto->up.tables[table_id].cls;
3851     return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls, version,
3852                                                                flow, wc)));
3853 }
3854
3855 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3856  * table '*table_id'.  Returns the rule that was found, which may be one of the
3857  * special rules according to packet miss hadling.  If 'may_packet_in' is
3858  * false, returning of the miss_rule (which issues packet ins for the
3859  * controller) is avoided.  Updates 'wc', if nonnull, to reflect the fields
3860  * that were used during the lookup.
3861  *
3862  * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3863  * if none is found then the table miss configuration for that table is
3864  * honored, which can result in additional lookups in other OpenFlow tables.
3865  * In this case the function updates '*table_id' to reflect the final OpenFlow
3866  * table that was searched.
3867  *
3868  * If 'honor_table_miss' is false, then only one table lookup occurs, in
3869  * '*table_id'.
3870  *
3871  * The rule is returned in '*rule', which is valid at least until the next
3872  * RCU quiescent period.  If the '*rule' needs to stay around longer, the
3873  * caller must take a reference.
3874  *
3875  * 'in_port' allows the lookup to take place as if the in port had the value
3876  * 'in_port'.  This is needed for resubmit action support.
3877  *
3878  * 'flow' is non-const to allow for temporary modifications during the lookup.
3879  * Any changes are restored before returning. */
3880 struct rule_dpif *
3881 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3882                             cls_version_t version, struct flow *flow,
3883                             struct flow_wildcards *wc,
3884                             const struct dpif_flow_stats *stats,
3885                             uint8_t *table_id, ofp_port_t in_port,
3886                             bool may_packet_in, bool honor_table_miss)
3887 {
3888     ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3889     ofp_port_t old_in_port = flow->in_port.ofp_port;
3890     enum ofputil_table_miss miss_config;
3891     struct rule_dpif *rule;
3892     uint8_t next_id;
3893
3894     /* We always unwildcard nw_frag (for IP), so they
3895      * need not be unwildcarded here. */
3896     if (flow->nw_frag & FLOW_NW_FRAG_ANY
3897         && ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3898         if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3899             /* We must pretend that transport ports are unavailable. */
3900             flow->tp_src = htons(0);
3901             flow->tp_dst = htons(0);
3902         } else {
3903             /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3904              * Use the drop_frags_rule (which cannot disappear). */
3905             rule = ofproto->drop_frags_rule;
3906             if (stats) {
3907                 struct oftable *tbl = &ofproto->up.tables[*table_id];
3908                 unsigned long orig;
3909
3910                 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3911             }
3912             return rule;
3913         }
3914     }
3915
3916     /* Look up a flow with 'in_port' as the input port.  Then restore the
3917      * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3918      * have surprising behavior). */
3919     flow->in_port.ofp_port = in_port;
3920
3921     /* Our current implementation depends on n_tables == N_TABLES, and
3922      * TBL_INTERNAL being the last table. */
3923     BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3924
3925     miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3926
3927     for (next_id = *table_id;
3928          next_id < ofproto->up.n_tables;
3929          next_id++, next_id += (next_id == TBL_INTERNAL))
3930     {
3931         *table_id = next_id;
3932         rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc);
3933         if (stats) {
3934             struct oftable *tbl = &ofproto->up.tables[next_id];
3935             unsigned long orig;
3936
3937             atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3938                                stats->n_packets, &orig);
3939         }
3940         if (rule) {
3941             goto out;   /* Match. */
3942         }
3943         if (honor_table_miss) {
3944             miss_config = ofproto_table_get_miss_config(&ofproto->up,
3945                                                         *table_id);
3946             if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3947                 continue;
3948             }
3949         }
3950         break;
3951     }
3952     /* Miss. */
3953     rule = ofproto->no_packet_in_rule;
3954     if (may_packet_in) {
3955         if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3956             || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3957             struct ofport_dpif *port;
3958
3959             port = ofp_port_to_ofport(ofproto, old_in_port);
3960             if (!port) {
3961                 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3962                              old_in_port);
3963             } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3964                 rule = ofproto->miss_rule;
3965             }
3966         } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3967                    connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3968             rule = ofproto->miss_rule;
3969         }
3970     }
3971 out:
3972     /* Restore port numbers, as they may have been modified above. */
3973     flow->tp_src = old_tp_src;
3974     flow->tp_dst = old_tp_dst;
3975     /* Restore the old in port. */
3976     flow->in_port.ofp_port = old_in_port;
3977
3978     return rule;
3979 }
3980
3981 static void
3982 complete_operation(struct rule_dpif *rule)
3983     OVS_REQUIRES(ofproto_mutex)
3984 {
3985     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3986
3987     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3988 }
3989
3990 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3991 {
3992     return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3993 }
3994
3995 static struct rule *
3996 rule_alloc(void)
3997 {
3998     struct rule_dpif *rule = xzalloc(sizeof *rule);
3999     return &rule->up;
4000 }
4001
4002 static void
4003 rule_dealloc(struct rule *rule_)
4004 {
4005     struct rule_dpif *rule = rule_dpif_cast(rule_);
4006     free(rule);
4007 }
4008
4009 static enum ofperr
4010 rule_check(struct rule *rule)
4011 {
4012     uint16_t ct_state, ct_zone;
4013     uint32_t ct_mark;
4014
4015     ct_state = MINIFLOW_GET_U16(rule->cr.match.flow, ct_state);
4016     ct_zone = MINIFLOW_GET_U16(rule->cr.match.flow, ct_zone);
4017     ct_mark = MINIFLOW_GET_U32(rule->cr.match.flow, ct_mark);
4018
4019     if (ct_state || ct_zone || ct_mark) {
4020         struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->ofproto);
4021         const struct odp_support *support = &ofproto_dpif_get_support(ofproto)->odp;
4022
4023         if ((ct_state && !support->ct_state)
4024             || (ct_zone && !support->ct_zone)
4025             || (ct_mark && !support->ct_mark)) {
4026             return OFPERR_OFPBMC_BAD_FIELD;
4027         }
4028         if (ct_state & CS_UNSUPPORTED_MASK) {
4029             return OFPERR_OFPBMC_BAD_MASK;
4030         }
4031     }
4032     return 0;
4033 }
4034
4035 static enum ofperr
4036 rule_construct(struct rule *rule_)
4037     OVS_NO_THREAD_SAFETY_ANALYSIS
4038 {
4039     struct rule_dpif *rule = rule_dpif_cast(rule_);
4040     int error;
4041
4042     error = rule_check(rule_);
4043     if (error) {
4044         return error;
4045     }
4046
4047     ovs_mutex_init_adaptive(&rule->stats_mutex);
4048     rule->stats.n_packets = 0;
4049     rule->stats.n_bytes = 0;
4050     rule->stats.used = rule->up.modified;
4051     rule->recirc_id = 0;
4052     rule->new_rule = NULL;
4053
4054     return 0;
4055 }
4056
4057 static void
4058 rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_stats)
4059     OVS_REQUIRES(ofproto_mutex)
4060 {
4061     struct rule_dpif *rule = rule_dpif_cast(rule_);
4062
4063     if (old_rule_ && forward_stats) {
4064         struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
4065
4066         ovs_assert(!old_rule->new_rule);
4067
4068         /* Take a reference to the new rule, and refer all stats updates from
4069          * the old rule to the new rule. */
4070         rule_dpif_ref(rule);
4071
4072         ovs_mutex_lock(&old_rule->stats_mutex);
4073         ovs_mutex_lock(&rule->stats_mutex);
4074         old_rule->new_rule = rule;       /* Forward future stats. */
4075         rule->stats = old_rule->stats;   /* Transfer stats to the new rule. */
4076         ovs_mutex_unlock(&rule->stats_mutex);
4077         ovs_mutex_unlock(&old_rule->stats_mutex);
4078     }
4079
4080     complete_operation(rule);
4081 }
4082
4083 static void
4084 rule_delete(struct rule *rule_)
4085     OVS_REQUIRES(ofproto_mutex)
4086 {
4087     struct rule_dpif *rule = rule_dpif_cast(rule_);
4088     complete_operation(rule);
4089 }
4090
4091 static void
4092 rule_destruct(struct rule *rule_)
4093     OVS_NO_THREAD_SAFETY_ANALYSIS
4094 {
4095     struct rule_dpif *rule = rule_dpif_cast(rule_);
4096
4097     ovs_mutex_destroy(&rule->stats_mutex);
4098     /* Release reference to the new rule, if any. */
4099     if (rule->new_rule) {
4100         rule_dpif_unref(rule->new_rule);
4101     }
4102     if (rule->recirc_id) {
4103         recirc_free_id(rule->recirc_id);
4104     }
4105 }
4106
4107 static void
4108 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4109                long long int *used)
4110 {
4111     struct rule_dpif *rule = rule_dpif_cast(rule_);
4112
4113     ovs_mutex_lock(&rule->stats_mutex);
4114     if (OVS_UNLIKELY(rule->new_rule)) {
4115         rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4116     } else {
4117         *packets = rule->stats.n_packets;
4118         *bytes = rule->stats.n_bytes;
4119         *used = rule->stats.used;
4120     }
4121     ovs_mutex_unlock(&rule->stats_mutex);
4122 }
4123
4124 static void
4125 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4126                   struct dp_packet *packet)
4127 {
4128     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4129
4130     ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4131 }
4132
4133 static enum ofperr
4134 rule_execute(struct rule *rule, const struct flow *flow,
4135              struct dp_packet *packet)
4136 {
4137     rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4138     dp_packet_delete(packet);
4139     return 0;
4140 }
4141
4142 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4143 {
4144     return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4145 }
4146
4147 static struct ofgroup *
4148 group_alloc(void)
4149 {
4150     struct group_dpif *group = xzalloc(sizeof *group);
4151     return &group->up;
4152 }
4153
4154 static void
4155 group_dealloc(struct ofgroup *group_)
4156 {
4157     struct group_dpif *group = group_dpif_cast(group_);
4158     free(group);
4159 }
4160
4161 static void
4162 group_construct_stats(struct group_dpif *group)
4163     OVS_REQUIRES(group->stats_mutex)
4164 {
4165     struct ofputil_bucket *bucket;
4166     const struct ovs_list *buckets;
4167
4168     group->packet_count = 0;
4169     group->byte_count = 0;
4170
4171     group_dpif_get_buckets(group, &buckets);
4172     LIST_FOR_EACH (bucket, list_node, buckets) {
4173         bucket->stats.packet_count = 0;
4174         bucket->stats.byte_count = 0;
4175     }
4176 }
4177
4178 void
4179 group_dpif_credit_stats(struct group_dpif *group,
4180                         struct ofputil_bucket *bucket,
4181                         const struct dpif_flow_stats *stats)
4182 {
4183     ovs_mutex_lock(&group->stats_mutex);
4184     group->packet_count += stats->n_packets;
4185     group->byte_count += stats->n_bytes;
4186     if (bucket) {
4187         bucket->stats.packet_count += stats->n_packets;
4188         bucket->stats.byte_count += stats->n_bytes;
4189     } else { /* Credit to all buckets */
4190         const struct ovs_list *buckets;
4191
4192         group_dpif_get_buckets(group, &buckets);
4193         LIST_FOR_EACH (bucket, list_node, buckets) {
4194             bucket->stats.packet_count += stats->n_packets;
4195             bucket->stats.byte_count += stats->n_bytes;
4196         }
4197     }
4198     ovs_mutex_unlock(&group->stats_mutex);
4199 }
4200
4201 static enum ofperr
4202 group_construct(struct ofgroup *group_)
4203 {
4204     struct group_dpif *group = group_dpif_cast(group_);
4205
4206     ovs_mutex_init_adaptive(&group->stats_mutex);
4207     ovs_mutex_lock(&group->stats_mutex);
4208     group_construct_stats(group);
4209     ovs_mutex_unlock(&group->stats_mutex);
4210     return 0;
4211 }
4212
4213 static void
4214 group_destruct(struct ofgroup *group_)
4215 {
4216     struct group_dpif *group = group_dpif_cast(group_);
4217     ovs_mutex_destroy(&group->stats_mutex);
4218 }
4219
4220 static enum ofperr
4221 group_modify(struct ofgroup *group_)
4222 {
4223     struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4224
4225     ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4226
4227     return 0;
4228 }
4229
4230 static enum ofperr
4231 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4232 {
4233     struct group_dpif *group = group_dpif_cast(group_);
4234     struct ofputil_bucket *bucket;
4235     const struct ovs_list *buckets;
4236     struct bucket_counter *bucket_stats;
4237
4238     ovs_mutex_lock(&group->stats_mutex);
4239     ogs->packet_count = group->packet_count;
4240     ogs->byte_count = group->byte_count;
4241
4242     group_dpif_get_buckets(group, &buckets);
4243     bucket_stats = ogs->bucket_stats;
4244     LIST_FOR_EACH (bucket, list_node, buckets) {
4245         bucket_stats->packet_count = bucket->stats.packet_count;
4246         bucket_stats->byte_count = bucket->stats.byte_count;
4247         bucket_stats++;
4248     }
4249     ovs_mutex_unlock(&group->stats_mutex);
4250
4251     return 0;
4252 }
4253
4254 /* If the group exists, this function increments the groups's reference count.
4255  *
4256  * Make sure to call group_dpif_unref() after no longer needing to maintain
4257  * a reference to the group. */
4258 bool
4259 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4260                   struct group_dpif **group)
4261 {
4262     struct ofgroup *ofgroup;
4263     bool found;
4264
4265     found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4266     *group = found ?  group_dpif_cast(ofgroup) : NULL;
4267
4268     return found;
4269 }
4270
4271 void
4272 group_dpif_get_buckets(const struct group_dpif *group,
4273                        const struct ovs_list **buckets)
4274 {
4275     *buckets = &group->up.buckets;
4276 }
4277
4278 enum ofp11_group_type
4279 group_dpif_get_type(const struct group_dpif *group)
4280 {
4281     return group->up.type;
4282 }
4283
4284 const char *
4285 group_dpif_get_selection_method(const struct group_dpif *group)
4286 {
4287     return group->up.props.selection_method;
4288 }
4289 \f
4290 /* Sends 'packet' out 'ofport'.
4291  * May modify 'packet'.
4292  * Returns 0 if successful, otherwise a positive errno value. */
4293 int
4294 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4295 {
4296     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4297     int error;
4298
4299     error = xlate_send_packet(ofport, packet);
4300
4301     ovs_mutex_lock(&ofproto->stats_mutex);
4302     ofproto->stats.tx_packets++;
4303     ofproto->stats.tx_bytes += dp_packet_size(packet);
4304     ovs_mutex_unlock(&ofproto->stats_mutex);
4305     return error;
4306 }
4307
4308 uint64_t
4309 group_dpif_get_selection_method_param(const struct group_dpif *group)
4310 {
4311     return group->up.props.selection_method_param;
4312 }
4313
4314 const struct field_array *
4315 group_dpif_get_fields(const struct group_dpif *group)
4316 {
4317     return &group->up.props.fields;
4318 }
4319 \f
4320 /* Return the version string of the datapath that backs up
4321  * this 'ofproto'.
4322  */
4323 static const char *
4324 get_datapath_version(const struct ofproto *ofproto_)
4325 {
4326     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4327
4328     return ofproto->backer->dp_version_string;
4329 }
4330
4331 static bool
4332 set_frag_handling(struct ofproto *ofproto_,
4333                   enum ofp_config_flags frag_handling)
4334 {
4335     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4336     if (frag_handling != OFPC_FRAG_REASM) {
4337         ofproto->backer->need_revalidate = REV_RECONFIGURE;
4338         return true;
4339     } else {
4340         return false;
4341     }
4342 }
4343
4344 static enum ofperr
4345 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4346            const struct flow *flow,
4347            const struct ofpact *ofpacts, size_t ofpacts_len)
4348 {
4349     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4350
4351     ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4352                                  ofpacts_len, packet);
4353     return 0;
4354 }
4355 \f
4356 /* NetFlow. */
4357
4358 static int
4359 set_netflow(struct ofproto *ofproto_,
4360             const struct netflow_options *netflow_options)
4361 {
4362     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4363
4364     if (netflow_options) {
4365         if (!ofproto->netflow) {
4366             ofproto->netflow = netflow_create();
4367             ofproto->backer->need_revalidate = REV_RECONFIGURE;
4368         }
4369         return netflow_set_options(ofproto->netflow, netflow_options);
4370     } else if (ofproto->netflow) {
4371         ofproto->backer->need_revalidate = REV_RECONFIGURE;
4372         netflow_unref(ofproto->netflow);
4373         ofproto->netflow = NULL;
4374     }
4375
4376     return 0;
4377 }
4378
4379 static void
4380 get_netflow_ids(const struct ofproto *ofproto_,
4381                 uint8_t *engine_type, uint8_t *engine_id)
4382 {
4383     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4384
4385     dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4386 }
4387 \f
4388 static struct ofproto_dpif *
4389 ofproto_dpif_lookup(const char *name)
4390 {
4391     struct ofproto_dpif *ofproto;
4392
4393     HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4394                              hash_string(name, 0), &all_ofproto_dpifs) {
4395         if (!strcmp(ofproto->up.name, name)) {
4396             return ofproto;
4397         }
4398     }
4399     return NULL;
4400 }
4401
4402 static void
4403 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4404                           const char *argv[], void *aux OVS_UNUSED)
4405 {
4406     struct ofproto_dpif *ofproto;
4407
4408     if (argc > 1) {
4409         ofproto = ofproto_dpif_lookup(argv[1]);
4410         if (!ofproto) {
4411             unixctl_command_reply_error(conn, "no such bridge");
4412             return;
4413         }
4414         ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4415         mac_learning_flush(ofproto->ml);
4416         ovs_rwlock_unlock(&ofproto->ml->rwlock);
4417     } else {
4418         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4419             ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4420             mac_learning_flush(ofproto->ml);
4421             ovs_rwlock_unlock(&ofproto->ml->rwlock);
4422         }
4423     }
4424
4425     unixctl_command_reply(conn, "table successfully flushed");
4426 }
4427
4428 static void
4429 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4430                                      const char *argv[], void *aux OVS_UNUSED)
4431 {
4432     struct ofproto_dpif *ofproto;
4433
4434     if (argc > 1) {
4435         ofproto = ofproto_dpif_lookup(argv[1]);
4436         if (!ofproto) {
4437             unixctl_command_reply_error(conn, "no such bridge");
4438             return;
4439         }
4440
4441         if (!mcast_snooping_enabled(ofproto->ms)) {
4442             unixctl_command_reply_error(conn, "multicast snooping is disabled");
4443             return;
4444         }
4445         mcast_snooping_mdb_flush(ofproto->ms);
4446     } else {
4447         HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4448             if (!mcast_snooping_enabled(ofproto->ms)) {
4449                 continue;
4450             }
4451             mcast_snooping_mdb_flush(ofproto->ms);
4452         }
4453     }
4454
4455     unixctl_command_reply(conn, "table successfully flushed");
4456 }
4457
4458 static struct ofport_dpif *
4459 ofbundle_get_a_port(const struct ofbundle *bundle)
4460 {
4461     return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4462                         bundle_node);
4463 }
4464
4465 static void
4466 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4467                          const char *argv[], void *aux OVS_UNUSED)
4468 {
4469     struct ds ds = DS_EMPTY_INITIALIZER;
4470     const struct ofproto_dpif *ofproto;
4471     const struct mac_entry *e;
4472
4473     ofproto = ofproto_dpif_lookup(argv[1]);
4474     if (!ofproto) {
4475         unixctl_command_reply_error(conn, "no such bridge");
4476         return;
4477     }
4478
4479     ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
4480     ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4481     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4482         struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4483         char name[OFP_MAX_PORT_NAME_LEN];
4484
4485         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4486                                name, sizeof name);
4487         ds_put_format(&ds, "%5s  %4d  "ETH_ADDR_FMT"  %3d\n",
4488                       name, e->vlan, ETH_ADDR_ARGS(e->mac),
4489                       mac_entry_age(ofproto->ml, e));
4490     }
4491     ovs_rwlock_unlock(&ofproto->ml->rwlock);
4492     unixctl_command_reply(conn, ds_cstr(&ds));
4493     ds_destroy(&ds);
4494 }
4495
4496 static void
4497 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4498                                     int argc OVS_UNUSED,
4499                                     const char *argv[],
4500                                     void *aux OVS_UNUSED)
4501 {
4502     struct ds ds = DS_EMPTY_INITIALIZER;
4503     const struct ofproto_dpif *ofproto;
4504     const struct ofbundle *bundle;
4505     const struct mcast_group *grp;
4506     struct mcast_group_bundle *b;
4507     struct mcast_mrouter_bundle *mrouter;
4508
4509     ofproto = ofproto_dpif_lookup(argv[1]);
4510     if (!ofproto) {
4511         unixctl_command_reply_error(conn, "no such bridge");
4512         return;
4513     }
4514
4515     if (!mcast_snooping_enabled(ofproto->ms)) {
4516         unixctl_command_reply_error(conn, "multicast snooping is disabled");
4517         return;
4518     }
4519
4520     ds_put_cstr(&ds, " port  VLAN  GROUP                Age\n");
4521     ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4522     LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4523         LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4524             char name[OFP_MAX_PORT_NAME_LEN];
4525
4526             bundle = b->port;
4527             ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4528                                    name, sizeof name);
4529             ds_put_format(&ds, "%5s  %4d  ", name, grp->vlan);
4530             print_ipv6_mapped(&ds, &grp->addr);
4531             ds_put_format(&ds, "         %3d\n",
4532                           mcast_bundle_age(ofproto->ms, b));
4533         }
4534     }
4535
4536     /* ports connected to multicast routers */
4537     LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4538         char name[OFP_MAX_PORT_NAME_LEN];
4539
4540         bundle = mrouter->port;
4541         ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4542                                name, sizeof name);
4543         ds_put_format(&ds, "%5s  %4d  querier             %3d\n",
4544                       name, mrouter->vlan,
4545                       mcast_mrouter_age(ofproto->ms, mrouter));
4546     }
4547     ovs_rwlock_unlock(&ofproto->ms->rwlock);
4548     unixctl_command_reply(conn, ds_cstr(&ds));
4549     ds_destroy(&ds);
4550 }
4551
4552 struct trace_ctx {
4553     struct xlate_out xout;
4554     struct xlate_in xin;
4555     const struct flow *key;
4556     struct flow flow;
4557     struct ds *result;
4558     struct flow_wildcards wc;
4559     struct ofpbuf odp_actions;
4560 };
4561
4562 static void
4563 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4564 {
4565     const struct rule_actions *actions;
4566     ovs_be64 cookie;
4567
4568     ds_put_char_multiple(result, '\t', level);
4569     if (!rule) {
4570         ds_put_cstr(result, "No match\n");
4571         return;
4572     }
4573
4574     ovs_mutex_lock(&rule->up.mutex);
4575     cookie = rule->up.flow_cookie;
4576     ovs_mutex_unlock(&rule->up.mutex);
4577
4578     ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4579                   rule ? rule->up.table_id : 0, ntohll(cookie));
4580     cls_rule_format(&rule->up.cr, result);
4581     ds_put_char(result, '\n');
4582
4583     actions = rule_dpif_get_actions(rule);
4584
4585     ds_put_char_multiple(result, '\t', level);
4586     ds_put_cstr(result, "OpenFlow actions=");
4587     ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4588     ds_put_char(result, '\n');
4589 }
4590
4591 static void
4592 trace_format_flow(struct ds *result, int level, const char *title,
4593                   struct trace_ctx *trace)
4594 {
4595     ds_put_char_multiple(result, '\t', level);
4596     ds_put_format(result, "%s: ", title);
4597     /* Do not report unchanged flows for resubmits. */
4598     if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4599         || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4600         ds_put_cstr(result, "unchanged");
4601     } else {
4602         flow_format(result, &trace->xin.flow);
4603         trace->flow = trace->xin.flow;
4604     }
4605     ds_put_char(result, '\n');
4606 }
4607
4608 static void
4609 trace_format_regs(struct ds *result, int level, const char *title,
4610                   struct trace_ctx *trace)
4611 {
4612     size_t i;
4613
4614     ds_put_char_multiple(result, '\t', level);
4615     ds_put_format(result, "%s:", title);
4616     for (i = 0; i < FLOW_N_REGS; i++) {
4617         ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4618     }
4619     ds_put_char(result, '\n');
4620 }
4621
4622 static void
4623 trace_format_odp(struct ds *result, int level, const char *title,
4624                  struct trace_ctx *trace)
4625 {
4626     struct ofpbuf *odp_actions = &trace->odp_actions;
4627
4628     ds_put_char_multiple(result, '\t', level);
4629     ds_put_format(result, "%s: ", title);
4630     format_odp_actions(result, odp_actions->data, odp_actions->size);
4631     ds_put_char(result, '\n');
4632 }
4633
4634 static void
4635 trace_format_megaflow(struct ds *result, int level, const char *title,
4636                       struct trace_ctx *trace)
4637 {
4638     struct match match;
4639
4640     ds_put_char_multiple(result, '\t', level);
4641     ds_put_format(result, "%s: ", title);
4642     match_init(&match, trace->key, &trace->wc);
4643     match_format(&match, result, OFP_DEFAULT_PRIORITY);
4644     ds_put_char(result, '\n');
4645 }
4646
4647 static void trace_report(struct xlate_in *, int recurse,
4648                          const char *format, ...)
4649     OVS_PRINTF_FORMAT(3, 4);
4650 static void trace_report_valist(struct xlate_in *, int recurse,
4651                                 const char *format, va_list args)
4652     OVS_PRINTF_FORMAT(3, 0);
4653
4654 static void
4655 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4656 {
4657     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4658     struct ds *result = trace->result;
4659
4660     if (!recurse) {
4661         if (rule == xin->ofproto->miss_rule) {
4662             trace_report(xin, recurse,
4663                          "No match, flow generates \"packet in\"s.");
4664         } else if (rule == xin->ofproto->no_packet_in_rule) {
4665             trace_report(xin, recurse, "No match, packets dropped because "
4666                          "OFPPC_NO_PACKET_IN is set on in_port.");
4667         } else if (rule == xin->ofproto->drop_frags_rule) {
4668             trace_report(xin, recurse, "Packets dropped because they are IP "
4669                          "fragments and the fragment handling mode is "
4670                          "\"drop\".");
4671         }
4672     }
4673
4674     ds_put_char(result, '\n');
4675     if (recurse) {
4676         trace_format_flow(result, recurse, "Resubmitted flow", trace);
4677         trace_format_regs(result, recurse, "Resubmitted regs", trace);
4678         trace_format_odp(result,  recurse, "Resubmitted  odp", trace);
4679         trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
4680     }
4681     trace_format_rule(result, recurse, rule);
4682 }
4683
4684 static void
4685 trace_report_valist(struct xlate_in *xin, int recurse,
4686                     const char *format, va_list args)
4687 {
4688     struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4689     struct ds *result = trace->result;
4690
4691     ds_put_char_multiple(result, '\t', recurse);
4692     ds_put_format_valist(result, format, args);
4693     ds_put_char(result, '\n');
4694 }
4695
4696 static void
4697 trace_report(struct xlate_in *xin, int recurse, const char *format, ...)
4698 {
4699     va_list args;
4700
4701     va_start(args, format);
4702     trace_report_valist(xin, recurse, format, args);
4703     va_end(args);
4704 }
4705
4706 /* Parses the 'argc' elements of 'argv', ignoring argv[0].  The following
4707  * forms are supported:
4708  *
4709  *     - [dpname] odp_flow [-generate | packet]
4710  *     - bridge br_flow [-generate | packet]
4711  *
4712  * On success, initializes '*ofprotop' and 'flow' and returns NULL.  On failure
4713  * returns a nonnull malloced error message. */
4714 static char * OVS_WARN_UNUSED_RESULT
4715 parse_flow_and_packet(int argc, const char *argv[],
4716                       struct ofproto_dpif **ofprotop, struct flow *flow,
4717                       struct dp_packet **packetp)
4718 {
4719     const struct dpif_backer *backer = NULL;
4720     const char *error = NULL;
4721     char *m_err = NULL;
4722     struct simap port_names = SIMAP_INITIALIZER(&port_names);
4723     struct dp_packet *packet;
4724     struct ofpbuf odp_key;
4725     struct ofpbuf odp_mask;
4726
4727     ofpbuf_init(&odp_key, 0);
4728     ofpbuf_init(&odp_mask, 0);
4729
4730     /* Handle "-generate" or a hex string as the last argument. */
4731     if (!strcmp(argv[argc - 1], "-generate")) {
4732         packet = dp_packet_new(0);
4733         argc--;
4734     } else {
4735         error = eth_from_hex(argv[argc - 1], &packet);
4736         if (!error) {
4737             argc--;
4738         } else if (argc == 4) {
4739             /* The 3-argument form must end in "-generate' or a hex string. */
4740             goto exit;
4741         }
4742         error = NULL;
4743     }
4744
4745     /* odp_flow can have its in_port specified as a name instead of port no.
4746      * We do not yet know whether a given flow is a odp_flow or a br_flow.
4747      * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4748      * we need to create a simap of name to port no. */
4749     if (argc == 3) {
4750         const char *dp_type;
4751         if (!strncmp(argv[1], "ovs-", 4)) {
4752             dp_type = argv[1] + 4;
4753         } else {
4754             dp_type = argv[1];
4755         }
4756         backer = shash_find_data(&all_dpif_backers, dp_type);
4757     } else if (argc == 2) {
4758         struct shash_node *node;
4759         if (shash_count(&all_dpif_backers) == 1) {
4760             node = shash_first(&all_dpif_backers);
4761             backer = node->data;
4762         }
4763     } else {
4764         error = "Syntax error";
4765         goto exit;
4766     }
4767     if (backer && backer->dpif) {
4768         struct dpif_port dpif_port;
4769         struct dpif_port_dump port_dump;
4770         DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4771             simap_put(&port_names, dpif_port.name,
4772                       odp_to_u32(dpif_port.port_no));
4773         }
4774     }
4775
4776     /* Parse the flow and determine whether a datapath or
4777      * bridge is specified. If function odp_flow_key_from_string()
4778      * returns 0, the flow is a odp_flow. If function
4779      * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4780     if (!odp_flow_from_string(argv[argc - 1], &port_names,
4781                               &odp_key, &odp_mask)) {
4782         if (!backer) {
4783             error = "Cannot find the datapath";
4784             goto exit;
4785         }
4786
4787         if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4788             error = "Failed to parse datapath flow key";
4789             goto exit;
4790         }
4791
4792         *ofprotop = xlate_lookup_ofproto(backer, flow,
4793                                          &flow->in_port.ofp_port);
4794         if (*ofprotop == NULL) {
4795             error = "Invalid datapath flow";
4796             goto exit;
4797         }
4798
4799         vsp_adjust_flow(*ofprotop, flow, NULL);
4800
4801     } else {
4802         char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4803
4804         if (err) {
4805             m_err = xasprintf("Bad openflow flow syntax: %s", err);
4806             free(err);
4807             goto exit;
4808         } else {
4809             if (argc != 3) {
4810                 error = "Must specify bridge name";
4811                 goto exit;
4812             }
4813
4814             *ofprotop = ofproto_dpif_lookup(argv[1]);
4815             if (!*ofprotop) {
4816                 error = "Unknown bridge name";
4817                 goto exit;
4818             }
4819         }
4820     }
4821
4822     /* Generate a packet, if requested. */
4823     if (packet) {
4824         if (!dp_packet_size(packet)) {
4825             flow_compose(packet, flow);
4826         } else {
4827             /* Use the metadata from the flow and the packet argument
4828              * to reconstruct the flow. */
4829             pkt_metadata_from_flow(&packet->md, flow);
4830             flow_extract(packet, flow);
4831         }
4832     }
4833
4834 exit:
4835     if (error && !m_err) {
4836         m_err = xstrdup(error);
4837     }
4838     if (m_err) {
4839         dp_packet_delete(packet);
4840         packet = NULL;
4841     }
4842     *packetp = packet;
4843     ofpbuf_uninit(&odp_key);
4844     ofpbuf_uninit(&odp_mask);
4845     simap_destroy(&port_names);
4846     return m_err;
4847 }
4848
4849 static void
4850 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4851                       void *aux OVS_UNUSED)
4852 {
4853     struct ofproto_dpif *ofproto;
4854     struct dp_packet *packet;
4855     char *error;
4856     struct flow flow;
4857
4858     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4859     if (!error) {
4860         struct ds result;
4861
4862         ds_init(&result);
4863         ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4864         unixctl_command_reply(conn, ds_cstr(&result));
4865         ds_destroy(&result);
4866         dp_packet_delete(packet);
4867     } else {
4868         unixctl_command_reply_error(conn, error);
4869         free(error);
4870     }
4871 }
4872
4873 static void
4874 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4875                               const char *argv[], void *aux OVS_UNUSED)
4876 {
4877     enum ofputil_protocol usable_protocols;
4878     struct ofproto_dpif *ofproto;
4879     bool enforce_consistency;
4880     struct ofpbuf ofpacts;
4881     struct dp_packet *packet;
4882     struct ds result;
4883     struct flow flow;
4884     uint16_t in_port;
4885
4886     /* Three kinds of error return values! */
4887     enum ofperr retval;
4888     char *error;
4889
4890     packet = NULL;
4891     ds_init(&result);
4892     ofpbuf_init(&ofpacts, 0);
4893
4894     /* Parse actions. */
4895     error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4896     if (error) {
4897         unixctl_command_reply_error(conn, error);
4898         free(error);
4899         goto exit;
4900     }
4901
4902     /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4903      * consistency between the flow and the actions.  With -consistent, we
4904      * enforce consistency even for a flow supported in OpenFlow 1.0. */
4905     if (!strcmp(argv[1], "-consistent")) {
4906         enforce_consistency = true;
4907         argv++;
4908         argc--;
4909     } else {
4910         enforce_consistency = false;
4911     }
4912
4913     error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4914     if (error) {
4915         unixctl_command_reply_error(conn, error);
4916         free(error);
4917         goto exit;
4918     }
4919
4920     /* Do the same checks as handle_packet_out() in ofproto.c.
4921      *
4922      * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4923      * strictly correct because these actions aren't in any table, but it's OK
4924      * because it 'table_id' is used only to check goto_table instructions, but
4925      * packet-outs take a list of actions and therefore it can't include
4926      * instructions.
4927      *
4928      * We skip the "meter" check here because meter is an instruction, not an
4929      * action, and thus cannot appear in ofpacts. */
4930     in_port = ofp_to_u16(flow.in_port.ofp_port);
4931     if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4932         unixctl_command_reply_error(conn, "invalid in_port");
4933         goto exit;
4934     }
4935     if (enforce_consistency) {
4936         retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
4937                                            &flow, u16_to_ofp(ofproto->up.max_ports),
4938                                            0, 0, usable_protocols);
4939     } else {
4940         retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
4941                                u16_to_ofp(ofproto->up.max_ports), 0, 0,
4942                                &usable_protocols);
4943     }
4944
4945     if (retval) {
4946         ds_clear(&result);
4947         ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4948         unixctl_command_reply_error(conn, ds_cstr(&result));
4949         goto exit;
4950     }
4951
4952     ofproto_trace(ofproto, &flow, packet,
4953                   ofpacts.data, ofpacts.size, &result);
4954     unixctl_command_reply(conn, ds_cstr(&result));
4955
4956 exit:
4957     ds_destroy(&result);
4958     dp_packet_delete(packet);
4959     ofpbuf_uninit(&ofpacts);
4960 }
4961
4962 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4963  * description of the results to 'ds'.
4964  *
4965  * The trace follows a packet with the specified 'flow' through the flow
4966  * table.  'packet' may be nonnull to trace an actual packet, with consequent
4967  * side effects (if it is nonnull then its flow must be 'flow').
4968  *
4969  * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4970  * trace, otherwise the actions are determined by a flow table lookup. */
4971 static void
4972 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4973               const struct dp_packet *packet,
4974               const struct ofpact ofpacts[], size_t ofpacts_len,
4975               struct ds *ds)
4976 {
4977     struct trace_ctx trace;
4978
4979     ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4980     ds_put_cstr(ds, "Flow: ");
4981     flow_format(ds, flow);
4982     ds_put_char(ds, '\n');
4983
4984     ofpbuf_init(&trace.odp_actions, 0);
4985
4986     trace.result = ds;
4987     trace.key = flow; /* Original flow key, used for megaflow. */
4988     trace.flow = *flow; /* May be modified by actions. */
4989     xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
4990                   ntohs(flow->tcp_flags), packet, &trace.wc,
4991                   &trace.odp_actions);
4992     trace.xin.ofpacts = ofpacts;
4993     trace.xin.ofpacts_len = ofpacts_len;
4994     trace.xin.resubmit_hook = trace_resubmit;
4995     trace.xin.report_hook = trace_report_valist;
4996
4997     xlate_actions(&trace.xin, &trace.xout);
4998
4999     ds_put_char(ds, '\n');
5000     trace_format_flow(ds, 0, "Final flow", &trace);
5001     trace_format_megaflow(ds, 0, "Megaflow", &trace);
5002
5003     ds_put_cstr(ds, "Datapath actions: ");
5004     format_odp_actions(ds, trace.odp_actions.data, trace.odp_actions.size);
5005
5006     if (trace.xout.slow) {
5007         enum slow_path_reason slow;
5008
5009         ds_put_cstr(ds, "\nThis flow is handled by the userspace "
5010                     "slow path because it:");
5011
5012         slow = trace.xout.slow;
5013         while (slow) {
5014             enum slow_path_reason bit = rightmost_1bit(slow);
5015
5016             ds_put_format(ds, "\n\t- %s.",
5017                           slow_path_reason_to_explanation(bit));
5018
5019             slow &= ~bit;
5020         }
5021     }
5022
5023     xlate_out_uninit(&trace.xout);
5024     ofpbuf_uninit(&trace.odp_actions);
5025 }
5026
5027 /* Store the current ofprotos in 'ofproto_shash'.  Returns a sorted list
5028  * of the 'ofproto_shash' nodes.  It is the responsibility of the caller
5029  * to destroy 'ofproto_shash' and free the returned value. */
5030 static const struct shash_node **
5031 get_ofprotos(struct shash *ofproto_shash)
5032 {
5033     const struct ofproto_dpif *ofproto;
5034
5035     HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5036         char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5037         shash_add_nocopy(ofproto_shash, name, ofproto);
5038     }
5039
5040     return shash_sort(ofproto_shash);
5041 }
5042
5043 static void
5044 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5045                               const char *argv[] OVS_UNUSED,
5046                               void *aux OVS_UNUSED)
5047 {
5048     struct ds ds = DS_EMPTY_INITIALIZER;
5049     struct shash ofproto_shash;
5050     const struct shash_node **sorted_ofprotos;
5051     int i;
5052
5053     shash_init(&ofproto_shash);
5054     sorted_ofprotos = get_ofprotos(&ofproto_shash);
5055     for (i = 0; i < shash_count(&ofproto_shash); i++) {
5056         const struct shash_node *node = sorted_ofprotos[i];
5057         ds_put_format(&ds, "%s\n", node->name);
5058     }
5059
5060     shash_destroy(&ofproto_shash);
5061     free(sorted_ofprotos);
5062
5063     unixctl_command_reply(conn, ds_cstr(&ds));
5064     ds_destroy(&ds);
5065 }
5066
5067 static void
5068 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5069 {
5070     const struct shash_node **ofprotos;
5071     struct dpif_dp_stats dp_stats;
5072     struct shash ofproto_shash;
5073     size_t i;
5074
5075     dpif_get_dp_stats(backer->dpif, &dp_stats);
5076
5077     ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5078                   dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5079
5080     shash_init(&ofproto_shash);
5081     ofprotos = get_ofprotos(&ofproto_shash);
5082     for (i = 0; i < shash_count(&ofproto_shash); i++) {
5083         struct ofproto_dpif *ofproto = ofprotos[i]->data;
5084         const struct shash_node **ports;
5085         size_t j;
5086
5087         if (ofproto->backer != backer) {
5088             continue;
5089         }
5090
5091         ds_put_format(ds, "\t%s:\n", ofproto->up.name);
5092
5093         ports = shash_sort(&ofproto->up.port_by_name);
5094         for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5095             const struct shash_node *node = ports[j];
5096             struct ofport *ofport = node->data;
5097             struct smap config;
5098             odp_port_t odp_port;
5099
5100             ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5101                           ofport->ofp_port);
5102
5103             odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5104             if (odp_port != ODPP_NONE) {
5105                 ds_put_format(ds, "%"PRIu32":", odp_port);
5106             } else {
5107                 ds_put_cstr(ds, "none:");
5108             }
5109
5110             ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5111
5112             smap_init(&config);
5113             if (!netdev_get_config(ofport->netdev, &config)) {
5114                 const struct smap_node **nodes;
5115                 size_t i;
5116
5117                 nodes = smap_sort(&config);
5118                 for (i = 0; i < smap_count(&config); i++) {
5119                     const struct smap_node *node = nodes[i];
5120                     ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5121                                   node->key, node->value);
5122                 }
5123                 free(nodes);
5124             }
5125             smap_destroy(&config);
5126
5127             ds_put_char(ds, ')');
5128             ds_put_char(ds, '\n');
5129         }
5130         free(ports);
5131     }
5132     shash_destroy(&ofproto_shash);
5133     free(ofprotos);
5134 }
5135
5136 static void
5137 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5138                           const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5139 {
5140     struct ds ds = DS_EMPTY_INITIALIZER;
5141     const struct shash_node **backers;
5142     int i;
5143
5144     backers = shash_sort(&all_dpif_backers);
5145     for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5146         dpif_show_backer(backers[i]->data, &ds);
5147     }
5148     free(backers);
5149
5150     unixctl_command_reply(conn, ds_cstr(&ds));
5151     ds_destroy(&ds);
5152 }
5153
5154 static void
5155 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5156                                 int argc OVS_UNUSED, const char *argv[],
5157                                 void *aux OVS_UNUSED)
5158 {
5159     const struct ofproto_dpif *ofproto;
5160
5161     struct ds ds = DS_EMPTY_INITIALIZER;
5162     bool verbosity = false;
5163
5164     struct dpif_port dpif_port;
5165     struct dpif_port_dump port_dump;
5166     struct hmap portno_names;
5167
5168     struct dpif_flow_dump *flow_dump;
5169     struct dpif_flow_dump_thread *flow_dump_thread;
5170     struct dpif_flow f;
5171     int error;
5172
5173     ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5174     if (!ofproto) {
5175         unixctl_command_reply_error(conn, "no such bridge");
5176         return;
5177     }
5178
5179     if (argc > 2 && !strcmp(argv[1], "-m")) {
5180         verbosity = true;
5181     }
5182
5183     hmap_init(&portno_names);
5184     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5185         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5186     }
5187
5188     ds_init(&ds);
5189     flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5190     flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5191     while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5192         struct flow flow;
5193
5194         if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5195             || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5196             continue;
5197         }
5198
5199         if (verbosity) {
5200             odp_format_ufid(&f.ufid, &ds);
5201             ds_put_cstr(&ds, " ");
5202         }
5203         odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5204                         &portno_names, &ds, verbosity);
5205         ds_put_cstr(&ds, ", ");
5206         dpif_flow_stats_format(&f.stats, &ds);
5207         ds_put_cstr(&ds, ", actions:");
5208         format_odp_actions(&ds, f.actions, f.actions_len);
5209         ds_put_char(&ds, '\n');
5210     }
5211     dpif_flow_dump_thread_destroy(flow_dump_thread);
5212     error = dpif_flow_dump_destroy(flow_dump);
5213
5214     if (error) {
5215         ds_clear(&ds);
5216         ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5217         unixctl_command_reply_error(conn, ds_cstr(&ds));
5218     } else {
5219         unixctl_command_reply(conn, ds_cstr(&ds));
5220     }
5221     odp_portno_names_destroy(&portno_names);
5222     hmap_destroy(&portno_names);
5223     ds_destroy(&ds);
5224 }
5225
5226 static void
5227 ofproto_revalidate_all_backers(void)
5228 {
5229     const struct shash_node **backers;
5230     int i;
5231
5232     backers = shash_sort(&all_dpif_backers);
5233     for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5234         struct dpif_backer *backer = backers[i]->data;
5235         backer->need_revalidate = REV_RECONFIGURE;
5236     }
5237     free(backers);
5238 }
5239
5240 static void
5241 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5242                      const char *argv[], void *aux OVS_UNUSED)
5243 {
5244     if (!strcasecmp(argv[1], "off")) {
5245         ofproto_use_tnl_push_pop = false;
5246         unixctl_command_reply(conn, "Tunnel push-pop off");
5247         ofproto_revalidate_all_backers();
5248     } else if (!strcasecmp(argv[1], "on")) {
5249         ofproto_use_tnl_push_pop = true;
5250         unixctl_command_reply(conn, "Tunnel push-pop on");
5251         ofproto_revalidate_all_backers();
5252     }
5253 }
5254
5255 static void
5256 ofproto_unixctl_init(void)
5257 {
5258     static bool registered;
5259     if (registered) {
5260         return;
5261     }
5262     registered = true;
5263
5264     unixctl_command_register(
5265         "ofproto/trace",
5266         "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5267         1, 3, ofproto_unixctl_trace, NULL);
5268     unixctl_command_register(
5269         "ofproto/trace-packet-out",
5270         "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5271         2, 6, ofproto_unixctl_trace_actions, NULL);
5272     unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5273                              ofproto_unixctl_fdb_flush, NULL);
5274     unixctl_command_register("fdb/show", "bridge", 1, 1,
5275                              ofproto_unixctl_fdb_show, NULL);
5276     unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5277                              ofproto_unixctl_mcast_snooping_flush, NULL);
5278     unixctl_command_register("mdb/show", "bridge", 1, 1,
5279                              ofproto_unixctl_mcast_snooping_show, NULL);
5280     unixctl_command_register("dpif/dump-dps", "", 0, 0,
5281                              ofproto_unixctl_dpif_dump_dps, NULL);
5282     unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5283                              NULL);
5284     unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5285                              ofproto_unixctl_dpif_dump_flows, NULL);
5286
5287     unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5288                              disable_tnl_push_pop, NULL);
5289 }
5290
5291 /* Returns true if 'table' is the table used for internal rules,
5292  * false otherwise. */
5293 bool
5294 table_is_internal(uint8_t table_id)
5295 {
5296     return table_id == TBL_INTERNAL;
5297 }
5298 \f
5299 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5300  *
5301  * This is deprecated.  It is only for compatibility with broken device drivers
5302  * in old versions of Linux that do not properly support VLANs when VLAN
5303  * devices are not used.  When broken device drivers are no longer in
5304  * widespread use, we will delete these interfaces. */
5305
5306 static int
5307 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5308 {
5309     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5310     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5311
5312     if (realdev_ofp_port == ofport->realdev_ofp_port
5313         && vid == ofport->vlandev_vid) {
5314         return 0;
5315     }
5316
5317     ofproto->backer->need_revalidate = REV_RECONFIGURE;
5318
5319     if (ofport->realdev_ofp_port) {
5320         vsp_remove(ofport);
5321     }
5322     if (realdev_ofp_port && ofport->bundle) {
5323         /* vlandevs are enslaved to their realdevs, so they are not allowed to
5324          * themselves be part of a bundle. */
5325         bundle_set(ofport_->ofproto, ofport->bundle, NULL);
5326     }
5327
5328     ofport->realdev_ofp_port = realdev_ofp_port;
5329     ofport->vlandev_vid = vid;
5330
5331     if (realdev_ofp_port) {
5332         vsp_add(ofport, realdev_ofp_port, vid);
5333     }
5334
5335     return 0;
5336 }
5337
5338 static uint32_t
5339 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5340 {
5341     return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5342 }
5343
5344 bool
5345 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5346     OVS_EXCLUDED(ofproto->vsp_mutex)
5347 {
5348     /* hmap_is_empty is thread safe. */
5349     return !hmap_is_empty(&ofproto->realdev_vid_map);
5350 }
5351
5352
5353 static ofp_port_t
5354 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5355                          ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5356     OVS_REQUIRES(ofproto->vsp_mutex)
5357 {
5358     if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5359         int vid = vlan_tci_to_vid(vlan_tci);
5360         const struct vlan_splinter *vsp;
5361
5362         HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5363                                  hash_realdev_vid(realdev_ofp_port, vid),
5364                                  &ofproto->realdev_vid_map) {
5365             if (vsp->realdev_ofp_port == realdev_ofp_port
5366                 && vsp->vid == vid) {
5367                 return vsp->vlandev_ofp_port;
5368             }
5369         }
5370     }
5371     return realdev_ofp_port;
5372 }
5373
5374 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5375  * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5376  * 'struct ofport_dpif'.  For example, given 'realdev_ofp_port' of eth0 and
5377  * 'vlan_tci' 9, it would return the port number of eth0.9.
5378  *
5379  * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5380  * function just returns its 'realdev_ofp_port' argument. */
5381 ofp_port_t
5382 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5383                        ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5384     OVS_EXCLUDED(ofproto->vsp_mutex)
5385 {
5386     ofp_port_t ret;
5387
5388     /* hmap_is_empty is thread safe, see if we can return immediately. */
5389     if (hmap_is_empty(&ofproto->realdev_vid_map)) {
5390         return realdev_ofp_port;
5391     }
5392     ovs_mutex_lock(&ofproto->vsp_mutex);
5393     ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5394     ovs_mutex_unlock(&ofproto->vsp_mutex);
5395     return ret;
5396 }
5397
5398 static struct vlan_splinter *
5399 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5400 {
5401     struct vlan_splinter *vsp;
5402
5403     HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5404                              hash_ofp_port(vlandev_ofp_port),
5405                              &ofproto->vlandev_map) {
5406         if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5407             return vsp;
5408         }
5409     }
5410
5411     return NULL;
5412 }
5413
5414 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5415  * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5416  * VLAN VID of the Linux VLAN device in '*vid'.  For example, given
5417  * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5418  * eth0 and store 9 in '*vid'.
5419  *
5420  * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5421  * VLAN device.  Unless VLAN splinters are enabled, this is what this function
5422  * always does.*/
5423 static ofp_port_t
5424 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5425                        ofp_port_t vlandev_ofp_port, int *vid)
5426     OVS_REQUIRES(ofproto->vsp_mutex)
5427 {
5428     if (!hmap_is_empty(&ofproto->vlandev_map)) {
5429         const struct vlan_splinter *vsp;
5430
5431         vsp = vlandev_find(ofproto, vlandev_ofp_port);
5432         if (vsp) {
5433             if (vid) {
5434                 *vid = vsp->vid;
5435             }
5436             return vsp->realdev_ofp_port;
5437         }
5438     }
5439     return 0;
5440 }
5441
5442 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5443  * whether 'flow->in_port' represents a Linux VLAN device.  If so, changes
5444  * 'flow->in_port' to the "real" device backing the VLAN device, sets
5445  * 'flow->vlan_tci' to the VLAN VID, and returns true.  Optionally pushes the
5446  * appropriate VLAN on 'packet' if provided.  Otherwise (which is always the
5447  * case unless VLAN splinters are enabled), returns false without making any
5448  * changes. */
5449 bool
5450 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
5451                 struct dp_packet *packet)
5452     OVS_EXCLUDED(ofproto->vsp_mutex)
5453 {
5454     ofp_port_t realdev;
5455     int vid;
5456
5457     /* hmap_is_empty is thread safe. */
5458     if (hmap_is_empty(&ofproto->vlandev_map)) {
5459         return false;
5460     }
5461
5462     ovs_mutex_lock(&ofproto->vsp_mutex);
5463     realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
5464     ovs_mutex_unlock(&ofproto->vsp_mutex);
5465     if (!realdev) {
5466         return false;
5467     }
5468
5469     /* Cause the flow to be processed as if it came in on the real device with
5470      * the VLAN device's VLAN ID. */
5471     flow->in_port.ofp_port = realdev;
5472     flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
5473
5474     if (packet) {
5475         /* Make the packet resemble the flow, so that it gets sent to an
5476          * OpenFlow controller properly, so that it looks correct for sFlow,
5477          * and so that flow_extract() will get the correct vlan_tci if it is
5478          * called on 'packet'. */
5479         eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
5480     }
5481
5482     return true;
5483 }
5484
5485 static void
5486 vsp_remove(struct ofport_dpif *port)
5487 {
5488     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5489     struct vlan_splinter *vsp;
5490
5491     ovs_mutex_lock(&ofproto->vsp_mutex);
5492     vsp = vlandev_find(ofproto, port->up.ofp_port);
5493     if (vsp) {
5494         hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5495         hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5496         free(vsp);
5497
5498         port->realdev_ofp_port = 0;
5499     } else {
5500         VLOG_ERR("missing vlan device record");
5501     }
5502     ovs_mutex_unlock(&ofproto->vsp_mutex);
5503 }
5504
5505 static void
5506 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
5507 {
5508     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5509
5510     ovs_mutex_lock(&ofproto->vsp_mutex);
5511     if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5512         && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
5513             == realdev_ofp_port)) {
5514         struct vlan_splinter *vsp;
5515
5516         vsp = xmalloc(sizeof *vsp);
5517         vsp->realdev_ofp_port = realdev_ofp_port;
5518         vsp->vlandev_ofp_port = port->up.ofp_port;
5519         vsp->vid = vid;
5520
5521         port->realdev_ofp_port = realdev_ofp_port;
5522
5523         hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5524                     hash_ofp_port(port->up.ofp_port));
5525         hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5526                     hash_realdev_vid(realdev_ofp_port, vid));
5527     } else {
5528         VLOG_ERR("duplicate vlan device record");
5529     }
5530     ovs_mutex_unlock(&ofproto->vsp_mutex);
5531 }
5532
5533 static odp_port_t
5534 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5535 {
5536     const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5537     return ofport ? ofport->odp_port : ODPP_NONE;
5538 }
5539
5540 struct ofport_dpif *
5541 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5542 {
5543     struct ofport_dpif *port;
5544
5545     ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5546     HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5547                              &backer->odp_to_ofport_map) {
5548         if (port->odp_port == odp_port) {
5549             ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5550             return port;
5551         }
5552     }
5553
5554     ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5555     return NULL;
5556 }
5557
5558 static ofp_port_t
5559 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5560 {
5561     struct ofport_dpif *port;
5562
5563     port = odp_port_to_ofport(ofproto->backer, odp_port);
5564     if (port && &ofproto->up == port->up.ofproto) {
5565         return port->up.ofp_port;
5566     } else {
5567         return OFPP_NONE;
5568     }
5569 }
5570
5571 int
5572 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5573                                const struct match *match, int priority,
5574                                uint16_t idle_timeout,
5575                                const struct ofpbuf *ofpacts,
5576                                struct rule **rulep)
5577 {
5578     struct ofproto_flow_mod ofm;
5579     struct rule_dpif *rule;
5580     int error;
5581
5582     ofm.fm.match = *match;
5583     ofm.fm.priority = priority;
5584     ofm.fm.new_cookie = htonll(0);
5585     ofm.fm.cookie = htonll(0);
5586     ofm.fm.cookie_mask = htonll(0);
5587     ofm.fm.modify_cookie = false;
5588     ofm.fm.table_id = TBL_INTERNAL;
5589     ofm.fm.command = OFPFC_ADD;
5590     ofm.fm.idle_timeout = idle_timeout;
5591     ofm.fm.hard_timeout = 0;
5592     ofm.fm.importance = 0;
5593     ofm.fm.buffer_id = 0;
5594     ofm.fm.out_port = 0;
5595     ofm.fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5596     ofm.fm.ofpacts = ofpacts->data;
5597     ofm.fm.ofpacts_len = ofpacts->size;
5598
5599     error = ofproto_flow_mod(&ofproto->up, &ofm);
5600     if (error) {
5601         VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5602                     ofperr_to_string(error));
5603         *rulep = NULL;
5604         return error;
5605     }
5606
5607     rule = rule_dpif_lookup_in_table(ofproto,
5608                                      ofproto_dpif_get_tables_version(ofproto),
5609                                      TBL_INTERNAL, &ofm.fm.match.flow,
5610                                      &ofm.fm.match.wc);
5611     if (rule) {
5612         *rulep = &rule->up;
5613     } else {
5614         OVS_NOT_REACHED();
5615     }
5616     return 0;
5617 }
5618
5619 int
5620 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5621                                   struct match *match, int priority)
5622 {
5623     struct ofproto_flow_mod ofm;
5624     int error;
5625
5626     ofm.fm.match = *match;
5627     ofm.fm.priority = priority;
5628     ofm.fm.new_cookie = htonll(0);
5629     ofm.fm.cookie = htonll(0);
5630     ofm.fm.cookie_mask = htonll(0);
5631     ofm.fm.modify_cookie = false;
5632     ofm.fm.table_id = TBL_INTERNAL;
5633     ofm.fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5634     ofm.fm.command = OFPFC_DELETE_STRICT;
5635
5636     error = ofproto_flow_mod(&ofproto->up, &ofm);
5637     if (error) {
5638         VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5639                     ofperr_to_string(error));
5640         return error;
5641     }
5642
5643     return 0;
5644 }
5645
5646 const struct ofproto_class ofproto_dpif_class = {
5647     init,
5648     enumerate_types,
5649     enumerate_names,
5650     del,
5651     port_open_type,
5652     type_run,
5653     type_wait,
5654     alloc,
5655     construct,
5656     destruct,
5657     dealloc,
5658     run,
5659     wait,
5660     NULL,                       /* get_memory_usage. */
5661     type_get_memory_usage,
5662     flush,
5663     query_tables,
5664     set_tables_version,
5665     port_alloc,
5666     port_construct,
5667     port_destruct,
5668     port_dealloc,
5669     port_modified,
5670     port_reconfigured,
5671     port_query_by_name,
5672     port_add,
5673     port_del,
5674     port_get_stats,
5675     port_dump_start,
5676     port_dump_next,
5677     port_dump_done,
5678     port_poll,
5679     port_poll_wait,
5680     port_is_lacp_current,
5681     port_get_lacp_stats,
5682     NULL,                       /* rule_choose_table */
5683     rule_alloc,
5684     rule_construct,
5685     rule_insert,
5686     rule_delete,
5687     rule_destruct,
5688     rule_dealloc,
5689     rule_get_stats,
5690     rule_execute,
5691     set_frag_handling,
5692     packet_out,
5693     set_netflow,
5694     get_netflow_ids,
5695     set_sflow,
5696     set_ipfix,
5697     set_cfm,
5698     cfm_status_changed,
5699     get_cfm_status,
5700     set_lldp,
5701     get_lldp_status,
5702     set_aa,
5703     aa_mapping_set,
5704     aa_mapping_unset,
5705     aa_vlan_get_queued,
5706     aa_vlan_get_queue_size,
5707     set_bfd,
5708     bfd_status_changed,
5709     get_bfd_status,
5710     set_stp,
5711     get_stp_status,
5712     set_stp_port,
5713     get_stp_port_status,
5714     get_stp_port_stats,
5715     set_rstp,
5716     get_rstp_status,
5717     set_rstp_port,
5718     get_rstp_port_status,
5719     set_queues,
5720     bundle_set,
5721     bundle_remove,
5722     mirror_set__,
5723     mirror_get_stats__,
5724     set_flood_vlans,
5725     is_mirror_output_bundle,
5726     forward_bpdu_changed,
5727     set_mac_table_config,
5728     set_mcast_snooping,
5729     set_mcast_snooping_port,
5730     set_realdev,
5731     NULL,                       /* meter_get_features */
5732     NULL,                       /* meter_set */
5733     NULL,                       /* meter_get */
5734     NULL,                       /* meter_del */
5735     group_alloc,                /* group_alloc */
5736     group_construct,            /* group_construct */
5737     group_destruct,             /* group_destruct */
5738     group_dealloc,              /* group_dealloc */
5739     group_modify,               /* group_modify */
5740     group_get_stats,            /* group_get_stats */
5741     get_datapath_version,       /* get_datapath_version */
5742 };