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