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