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