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