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