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