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