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