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