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