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