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