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