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