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