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