]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif.c
datapath: Compact sw_flow_key.
[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,
1954e6bb 933 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
4b97b70d
BP
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;
6d328fa2 1098 struct classifier *cls;
c57b2226
BP
1099 int error;
1100
81a76618
BP
1101 match_init_catchall(&fm.match);
1102 fm.priority = 0;
1103 match_set_reg(&fm.match, 0, id);
623e1caf 1104 fm.new_cookie = htonll(0);
c57b2226
BP
1105 fm.cookie = htonll(0);
1106 fm.cookie_mask = htonll(0);
23342857 1107 fm.modify_cookie = false;
c57b2226
BP
1108 fm.table_id = TBL_INTERNAL;
1109 fm.command = OFPFC_ADD;
1110 fm.idle_timeout = 0;
1111 fm.hard_timeout = 0;
1112 fm.buffer_id = 0;
1113 fm.out_port = 0;
1114 fm.flags = 0;
f25d0cf3
BP
1115 fm.ofpacts = ofpacts->data;
1116 fm.ofpacts_len = ofpacts->size;
c57b2226
BP
1117
1118 error = ofproto_flow_mod(&ofproto->up, &fm);
1119 if (error) {
1120 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1121 id, ofperr_to_string(error));
1122 return error;
1123 }
1124
6d328fa2
SH
1125 cls = &ofproto->up.tables[TBL_INTERNAL].cls;
1126 fat_rwlock_rdlock(&cls->rwlock);
1127 *rulep = rule_dpif_cast(rule_from_cls_rule(
1128 classifier_lookup(cls, &fm.match.flow, NULL)));
1129 ovs_assert(*rulep != NULL);
1130 fat_rwlock_unlock(&cls->rwlock);
0f5f95a9 1131
abe529af
BP
1132 return 0;
1133}
1134
c57b2226
BP
1135static int
1136add_internal_flows(struct ofproto_dpif *ofproto)
1137{
f25d0cf3
BP
1138 struct ofpact_controller *controller;
1139 uint64_t ofpacts_stub[128 / 8];
1140 struct ofpbuf ofpacts;
c57b2226
BP
1141 int error;
1142 int id;
1143
f25d0cf3 1144 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
c57b2226
BP
1145 id = 1;
1146
f25d0cf3
BP
1147 controller = ofpact_put_CONTROLLER(&ofpacts);
1148 controller->max_len = UINT16_MAX;
1149 controller->controller_id = 0;
1150 controller->reason = OFPR_NO_MATCH;
1151 ofpact_pad(&ofpacts);
1152
1153 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
c57b2226
BP
1154 if (error) {
1155 return error;
1156 }
1157
f25d0cf3
BP
1158 ofpbuf_clear(&ofpacts);
1159 error = add_internal_flow(ofproto, id++, &ofpacts,
c57b2226 1160 &ofproto->no_packet_in_rule);
7fd51d39
BP
1161 if (error) {
1162 return error;
1163 }
1164
1165 error = add_internal_flow(ofproto, id++, &ofpacts,
1166 &ofproto->drop_frags_rule);
c57b2226
BP
1167 return error;
1168}
1169
abe529af
BP
1170static void
1171destruct(struct ofproto *ofproto_)
1172{
1173 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7ee20df1 1174 struct rule_dpif *rule, *next_rule;
0fb7792a 1175 struct ofproto_packet_in *pin, *next_pin;
d0918789 1176 struct oftable *table;
1b63b91e 1177 struct list pins;
abe529af 1178
875b94ed 1179 ofproto->backer->need_revalidate = REV_RECONFIGURE;
dc24a00f 1180 ovs_rwlock_wrlock(&xlate_rwlock);
46c88433 1181 xlate_remove_ofproto(ofproto);
dc24a00f 1182 ovs_rwlock_unlock(&xlate_rwlock);
46c88433 1183
3f142f59
BP
1184 /* Ensure that the upcall processing threads have no remaining references
1185 * to the ofproto or anything in it. */
1186 udpif_synchronize(ofproto->backer->udpif);
1fe409d5 1187
b44a10b7 1188 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
7ee20df1 1189
0697b5c3
BP
1190 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1191 struct cls_cursor cursor;
1192
06f81620 1193 fat_rwlock_rdlock(&table->cls.rwlock);
d0918789 1194 cls_cursor_init(&cursor, &table->cls, NULL);
06f81620 1195 fat_rwlock_unlock(&table->cls.rwlock);
0697b5c3 1196 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
8b81d1ef 1197 ofproto_rule_delete(&ofproto->up, &rule->up);
0697b5c3 1198 }
7ee20df1
BP
1199 }
1200
05067881
BP
1201 guarded_list_pop_all(&ofproto->pins, &pins);
1202 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
ada3a58d 1203 list_remove(&pin->list_node);
0fb7792a 1204 free(CONST_CAST(void *, pin->up.packet));
ada3a58d
EJ
1205 free(pin);
1206 }
05067881 1207 guarded_list_destroy(&ofproto->pins);
ada3a58d 1208
ec7ceaed 1209 mbridge_unref(ofproto->mbridge);
abe529af 1210
8e407f27 1211 netflow_unref(ofproto->netflow);
9723bcce 1212 dpif_sflow_unref(ofproto->sflow);
abe529af 1213 hmap_destroy(&ofproto->bundles);
5d989517 1214 mac_learning_unref(ofproto->ml);
abe529af 1215
52a90c29
BP
1216 hmap_destroy(&ofproto->vlandev_map);
1217 hmap_destroy(&ofproto->realdev_vid_map);
1218
acf60855 1219 sset_destroy(&ofproto->ports);
0a740f48 1220 sset_destroy(&ofproto->ghost_ports);
acf60855 1221 sset_destroy(&ofproto->port_poll_set);
e1b1d06a 1222
0da3c61b 1223 ovs_mutex_destroy(&ofproto->stats_mutex);
13501305
EJ
1224 ovs_mutex_destroy(&ofproto->vsp_mutex);
1225
acf60855 1226 close_dpif_backer(ofproto->backer);
abe529af
BP
1227}
1228
5fcc0d00
BP
1229static int
1230run(struct ofproto *ofproto_)
1231{
1232 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
e79a6c83 1233 uint64_t new_seq, new_dump_seq;
5fcc0d00 1234
ec7ceaed
EJ
1235 if (mbridge_need_revalidate(ofproto->mbridge)) {
1236 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 1237 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 1238 mac_learning_flush(ofproto->ml);
509c0149 1239 ovs_rwlock_unlock(&ofproto->ml->rwlock);
ec7ceaed
EJ
1240 }
1241
a6b7506d 1242 /* Do not perform any periodic activity required by 'ofproto' while
40358701 1243 * waiting for flow restore to complete. */
a6b7506d
EJ
1244 if (!ofproto_get_flow_restore_wait()) {
1245 struct ofproto_packet_in *pin, *next_pin;
1246 struct list pins;
40358701 1247
a6b7506d
EJ
1248 guarded_list_pop_all(&ofproto->pins, &pins);
1249 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1250 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1251 list_remove(&pin->list_node);
1252 free(CONST_CAST(void *, pin->up.packet));
1253 free(pin);
1254 }
abe529af
BP
1255 }
1256
abe529af 1257 if (ofproto->netflow) {
8bfaca5b 1258 netflow_run(ofproto->netflow);
abe529af
BP
1259 }
1260 if (ofproto->sflow) {
bae473fe 1261 dpif_sflow_run(ofproto->sflow);
abe529af 1262 }
978427a5
RL
1263 if (ofproto->ipfix) {
1264 dpif_ipfix_run(ofproto->ipfix);
1265 }
abe529af 1266
f23d157c
JS
1267 new_seq = seq_read(connectivity_seq_get());
1268 if (ofproto->change_seq != new_seq) {
1269 struct ofport_dpif *ofport;
1270
1271 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1272 port_run(ofport);
1273 }
1274
1275 ofproto->change_seq = new_seq;
abe529af 1276 }
e2d13c43
JS
1277 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1278 struct ofbundle *bundle;
1279
1280 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1281 bundle_run(bundle);
1282 }
abe529af
BP
1283 }
1284
21f7563c 1285 stp_run(ofproto);
509c0149 1286 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594
EJ
1287 if (mac_learning_run(ofproto->ml)) {
1288 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1289 }
509c0149 1290 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af 1291
e79a6c83
EJ
1292 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1293 if (ofproto->dump_seq != new_dump_seq) {
1294 struct rule *rule, *next_rule;
1295
1296 /* We know stats are relatively fresh, so now is a good time to do some
1297 * periodic work. */
1298 ofproto->dump_seq = new_dump_seq;
1299
1300 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1301 * has passed. */
1302 ovs_mutex_lock(&ofproto_mutex);
1303 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1304 &ofproto->up.expirable) {
1305 rule_expire(rule_dpif_cast(rule));
1306 }
1307 ovs_mutex_unlock(&ofproto_mutex);
1308
1309 /* All outstanding data in existing flows has been accounted, so it's a
1310 * good time to do bond rebalancing. */
1311 if (ofproto->has_bonded_bundles) {
1312 struct ofbundle *bundle;
1313
1314 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1315 if (bundle->bond) {
1316 bond_rebalance(bundle->bond);
1317 }
1318 }
6814e51f
BP
1319 }
1320 }
1321
abe529af
BP
1322 return 0;
1323}
1324
1325static void
1326wait(struct ofproto *ofproto_)
1327{
1328 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 1329
40358701
GS
1330 if (ofproto_get_flow_restore_wait()) {
1331 return;
1332 }
1333
abe529af 1334 if (ofproto->sflow) {
bae473fe 1335 dpif_sflow_wait(ofproto->sflow);
abe529af 1336 }
978427a5
RL
1337 if (ofproto->ipfix) {
1338 dpif_ipfix_wait(ofproto->ipfix);
1339 }
e2d13c43
JS
1340 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1341 struct ofbundle *bundle;
1342
1343 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1344 bundle_wait(bundle);
1345 }
abe529af 1346 }
6fca1ffb
BP
1347 if (ofproto->netflow) {
1348 netflow_wait(ofproto->netflow);
1349 }
509c0149 1350 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1c313b88 1351 mac_learning_wait(ofproto->ml);
509c0149 1352 ovs_rwlock_unlock(&ofproto->ml->rwlock);
21f7563c 1353 stp_wait(ofproto);
2cc3c58e 1354 if (ofproto->backer->need_revalidate) {
abe529af
BP
1355 /* Shouldn't happen, but if it does just go around again. */
1356 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1357 poll_immediate_wake();
abe529af 1358 }
abe529af 1359
e79a6c83 1360 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
0d085684
BP
1361}
1362
1c030aa5
EJ
1363static void
1364type_get_memory_usage(const char *type, struct simap *usage)
1365{
1366 struct dpif_backer *backer;
1367
1368 backer = shash_find_data(&all_dpif_backers, type);
1369 if (backer) {
1370 udpif_get_memory_usage(backer->udpif, usage);
1371 }
1372}
1373
abe529af 1374static void
1b5b5071 1375flush(struct ofproto *ofproto_)
abe529af 1376{
1b5b5071
AZ
1377 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1378 struct dpif_backer *backer = ofproto->backer;
1379
1380 if (backer) {
1381 udpif_flush(backer->udpif);
1382 }
abe529af
BP
1383}
1384
6c1491fb
BP
1385static void
1386get_features(struct ofproto *ofproto_ OVS_UNUSED,
9e1fd49b 1387 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
6c1491fb
BP
1388{
1389 *arp_match_ip = true;
9e1fd49b
BP
1390 *actions = (OFPUTIL_A_OUTPUT |
1391 OFPUTIL_A_SET_VLAN_VID |
1392 OFPUTIL_A_SET_VLAN_PCP |
1393 OFPUTIL_A_STRIP_VLAN |
1394 OFPUTIL_A_SET_DL_SRC |
1395 OFPUTIL_A_SET_DL_DST |
1396 OFPUTIL_A_SET_NW_SRC |
1397 OFPUTIL_A_SET_NW_DST |
1398 OFPUTIL_A_SET_NW_TOS |
1399 OFPUTIL_A_SET_TP_SRC |
1400 OFPUTIL_A_SET_TP_DST |
1401 OFPUTIL_A_ENQUEUE);
6c1491fb
BP
1402}
1403
1404static void
307975da 1405get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
6c1491fb
BP
1406{
1407 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
a8d9304d 1408 struct dpif_dp_stats s;
7fd51d39 1409 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
7e741ffb 1410 uint64_t n_lookup;
dc437090 1411 long long int used;
6c1491fb
BP
1412
1413 strcpy(ots->name, "classifier");
1414
acf60855 1415 dpif_get_dp_stats(ofproto->backer->dpif, &s);
dc437090
JR
1416 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes, &used);
1417 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes,
1418 &used);
1419 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes,
1420 &used);
7fd51d39 1421 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
7e741ffb
JG
1422 ots->lookup_count = htonll(n_lookup);
1423 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
6c1491fb
BP
1424}
1425
abe529af
BP
1426static struct ofport *
1427port_alloc(void)
1428{
1429 struct ofport_dpif *port = xmalloc(sizeof *port);
1430 return &port->up;
1431}
1432
1433static void
1434port_dealloc(struct ofport *port_)
1435{
1436 struct ofport_dpif *port = ofport_dpif_cast(port_);
1437 free(port);
1438}
1439
1440static int
1441port_construct(struct ofport *port_)
1442{
1443 struct ofport_dpif *port = ofport_dpif_cast(port_);
1444 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
b9ad7294 1445 const struct netdev *netdev = port->up.netdev;
3aa30359 1446 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
e1b1d06a
JP
1447 struct dpif_port dpif_port;
1448 int error;
abe529af 1449
2cc3c58e 1450 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1451 port->bundle = NULL;
1452 port->cfm = NULL;
ccc09689 1453 port->bfd = NULL;
d5ffa7f2 1454 port->may_enable = true;
21f7563c
JP
1455 port->stp_port = NULL;
1456 port->stp_state = STP_DISABLED;
42943cde 1457 port->is_tunnel = false;
6cbbf4fa 1458 port->peer = NULL;
55954f6e
EJ
1459 port->qdscp = NULL;
1460 port->n_qdscp = 0;
52a90c29
BP
1461 port->realdev_ofp_port = 0;
1462 port->vlandev_vid = 0;
b9ad7294 1463 port->carrier_seq = netdev_get_carrier_resets(netdev);
a6363cfd 1464 port->is_layer3 = netdev_vport_is_layer3(netdev);
abe529af 1465
b9ad7294 1466 if (netdev_vport_is_patch(netdev)) {
743cea45
NM
1467 /* By bailing out here, we don't submit the port to the sFlow module
1468 * to be considered for counter polling export. This is correct
1469 * because the patch port represents an interface that sFlow considers
1470 * to be "internal" to the switch as a whole, and therefore not an
1471 * candidate for counter polling. */
4e022ec0 1472 port->odp_port = ODPP_NONE;
6cbbf4fa 1473 ofport_update_peer(port);
0a740f48
EJ
1474 return 0;
1475 }
1476
acf60855 1477 error = dpif_port_query_by_name(ofproto->backer->dpif,
3aa30359
BP
1478 netdev_vport_get_dpif_port(netdev, namebuf,
1479 sizeof namebuf),
e1b1d06a
JP
1480 &dpif_port);
1481 if (error) {
1482 return error;
1483 }
1484
1485 port->odp_port = dpif_port.port_no;
1486
b9ad7294 1487 if (netdev_get_tunnel_config(netdev)) {
42943cde
EJ
1488 tnl_port_add(port, port->up.netdev, port->odp_port);
1489 port->is_tunnel = true;
b9ad7294
EJ
1490 } else {
1491 /* Sanity-check that a mapping doesn't already exist. This
1492 * shouldn't happen for non-tunnel ports. */
1493 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1494 VLOG_ERR("port %s already has an OpenFlow port number",
1495 dpif_port.name);
da78d43d 1496 dpif_port_destroy(&dpif_port);
b9ad7294
EJ
1497 return EBUSY;
1498 }
e1b1d06a 1499
8449c4d6 1500 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1501 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
f9c0c3ec 1502 hash_odp_port(port->odp_port));
8449c4d6 1503 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1504 }
da78d43d 1505 dpif_port_destroy(&dpif_port);
e1b1d06a 1506
abe529af 1507 if (ofproto->sflow) {
e1b1d06a 1508 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
abe529af
BP
1509 }
1510
1511 return 0;
1512}
1513
1514static void
1515port_destruct(struct ofport *port_)
1516{
1517 struct ofport_dpif *port = ofport_dpif_cast(port_);
1518 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
02f8d646 1519 const char *devname = netdev_get_name(port->up.netdev);
3aa30359
BP
1520 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1521 const char *dp_port_name;
abe529af 1522
7894e7da 1523 ofproto->backer->need_revalidate = REV_RECONFIGURE;
dc24a00f 1524 ovs_rwlock_wrlock(&xlate_rwlock);
46c88433 1525 xlate_ofport_remove(port);
dc24a00f 1526 ovs_rwlock_unlock(&xlate_rwlock);
7894e7da 1527
3aa30359
BP
1528 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1529 sizeof namebuf);
a614d823 1530 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
acf60855
JP
1531 /* The underlying device is still there, so delete it. This
1532 * happens when the ofproto is being destroyed, since the caller
1533 * assumes that removal of attached ports will happen as part of
1534 * destruction. */
42943cde 1535 if (!port->is_tunnel) {
a614d823
KM
1536 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1537 }
acf60855
JP
1538 }
1539
6cbbf4fa
EJ
1540 if (port->peer) {
1541 port->peer->peer = NULL;
1542 port->peer = NULL;
1543 }
1544
42943cde 1545 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
8449c4d6 1546 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48 1547 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
8449c4d6 1548 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48
EJ
1549 }
1550
42943cde 1551 tnl_port_del(port);
02f8d646 1552 sset_find_and_delete(&ofproto->ports, devname);
0a740f48 1553 sset_find_and_delete(&ofproto->ghost_ports, devname);
abe529af 1554 bundle_remove(port_);
a5610457 1555 set_cfm(port_, NULL);
8aee94b6 1556 set_bfd(port_, NULL);
0c0c32d8
BP
1557 if (port->stp_port) {
1558 stp_port_disable(port->stp_port);
1559 }
abe529af 1560 if (ofproto->sflow) {
bae473fe 1561 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
abe529af 1562 }
8b36f51e 1563
55954f6e 1564 free(port->qdscp);
abe529af
BP
1565}
1566
1567static void
1568port_modified(struct ofport *port_)
1569{
1570 struct ofport_dpif *port = ofport_dpif_cast(port_);
1571
1572 if (port->bundle && port->bundle->bond) {
1573 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1574 }
9d46b444
EJ
1575
1576 if (port->cfm) {
1577 cfm_set_netdev(port->cfm, port->up.netdev);
1578 }
f1e78bbd 1579
c1c4e8c7
AW
1580 if (port->bfd) {
1581 bfd_set_netdev(port->bfd, port->up.netdev);
1582 }
1583
635c5db9
AW
1584 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1585 port->up.pp.hw_addr);
1586
42943cde
EJ
1587 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1588 port->odp_port)) {
7894e7da
EJ
1589 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1590 REV_RECONFIGURE;
f1e78bbd 1591 }
6cbbf4fa
EJ
1592
1593 ofport_update_peer(port);
abe529af
BP
1594}
1595
1596static void
9e1fd49b 1597port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
abe529af
BP
1598{
1599 struct ofport_dpif *port = ofport_dpif_cast(port_);
1600 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
9e1fd49b 1601 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
abe529af 1602
9e1fd49b 1603 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
c57b2226
BP
1604 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1605 OFPUTIL_PC_NO_PACKET_IN)) {
2cc3c58e 1606 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7bde8dd8 1607
9e1fd49b 1608 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
7bde8dd8
JP
1609 bundle_update(port->bundle);
1610 }
abe529af
BP
1611 }
1612}
1613
1614static int
1615set_sflow(struct ofproto *ofproto_,
1616 const struct ofproto_sflow_options *sflow_options)
1617{
1618 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bae473fe 1619 struct dpif_sflow *ds = ofproto->sflow;
6ff686f2 1620
abe529af 1621 if (sflow_options) {
bae473fe 1622 if (!ds) {
abe529af
BP
1623 struct ofport_dpif *ofport;
1624
4213f19d 1625 ds = ofproto->sflow = dpif_sflow_create();
abe529af 1626 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
e1b1d06a 1627 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
abe529af 1628 }
2cc3c58e 1629 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af 1630 }
bae473fe 1631 dpif_sflow_set_options(ds, sflow_options);
abe529af 1632 } else {
6ff686f2 1633 if (ds) {
9723bcce 1634 dpif_sflow_unref(ds);
2cc3c58e 1635 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6ff686f2
PS
1636 ofproto->sflow = NULL;
1637 }
abe529af
BP
1638 }
1639 return 0;
1640}
1641
29089a54
RL
1642static int
1643set_ipfix(
1644 struct ofproto *ofproto_,
1645 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1646 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1647 size_t n_flow_exporters_options)
1648{
1649 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1650 struct dpif_ipfix *di = ofproto->ipfix;
978427a5 1651 bool has_options = bridge_exporter_options || flow_exporters_options;
29089a54 1652
978427a5
RL
1653 if (has_options && !di) {
1654 di = ofproto->ipfix = dpif_ipfix_create();
1655 }
1656
1657 if (di) {
1658 /* Call set_options in any case to cleanly flush the flow
1659 * caches in the last exporters that are to be destroyed. */
29089a54
RL
1660 dpif_ipfix_set_options(
1661 di, bridge_exporter_options, flow_exporters_options,
1662 n_flow_exporters_options);
978427a5
RL
1663
1664 if (!has_options) {
d857c8aa 1665 dpif_ipfix_unref(di);
29089a54
RL
1666 ofproto->ipfix = NULL;
1667 }
1668 }
978427a5 1669
29089a54
RL
1670 return 0;
1671}
1672
abe529af 1673static int
a5610457 1674set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
abe529af
BP
1675{
1676 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
635c5db9 1677 int error = 0;
abe529af 1678
635c5db9 1679 if (s) {
abe529af 1680 if (!ofport->cfm) {
8c977421
EJ
1681 struct ofproto_dpif *ofproto;
1682
1683 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2cc3c58e 1684 ofproto->backer->need_revalidate = REV_RECONFIGURE;
90967e95 1685 ofport->cfm = cfm_create(ofport->up.netdev);
abe529af
BP
1686 }
1687
a5610457 1688 if (cfm_configure(ofport->cfm, s)) {
635c5db9
AW
1689 error = 0;
1690 goto out;
abe529af
BP
1691 }
1692
1693 error = EINVAL;
1694 }
8e9a73fa 1695 cfm_unref(ofport->cfm);
abe529af 1696 ofport->cfm = NULL;
635c5db9
AW
1697out:
1698 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1699 ofport->up.pp.hw_addr);
abe529af
BP
1700 return error;
1701}
1702
9a9e3786
BP
1703static bool
1704get_cfm_status(const struct ofport *ofport_,
1705 struct ofproto_cfm_status *status)
1de11730
EJ
1706{
1707 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1708
1709 if (ofport->cfm) {
9a9e3786 1710 status->faults = cfm_get_fault(ofport->cfm);
76c4290d 1711 status->flap_count = cfm_get_flap_count(ofport->cfm);
9a9e3786
BP
1712 status->remote_opstate = cfm_get_opup(ofport->cfm);
1713 status->health = cfm_get_health(ofport->cfm);
1714 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1715 return true;
1de11730 1716 } else {
9a9e3786 1717 return false;
1de11730
EJ
1718 }
1719}
ccc09689
EJ
1720
1721static int
1722set_bfd(struct ofport *ofport_, const struct smap *cfg)
1723{
1724 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1725 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1726 struct bfd *old;
1727
1728 old = ofport->bfd;
c1c4e8c7
AW
1729 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1730 cfg, ofport->up.netdev);
ccc09689
EJ
1731 if (ofport->bfd != old) {
1732 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1733 }
635c5db9
AW
1734 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1735 ofport->up.pp.hw_addr);
ccc09689
EJ
1736 return 0;
1737}
1738
1739static int
1740get_bfd_status(struct ofport *ofport_, struct smap *smap)
1741{
1742 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1743
1744 if (ofport->bfd) {
1745 bfd_get_status(ofport->bfd, smap);
1746 return 0;
1747 } else {
1748 return ENOENT;
1749 }
1750}
abe529af 1751\f
21f7563c
JP
1752/* Spanning Tree. */
1753
1754static void
1755send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1756{
1757 struct ofproto_dpif *ofproto = ofproto_;
1758 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1759 struct ofport_dpif *ofport;
1760
1761 ofport = stp_port_get_aux(sp);
1762 if (!ofport) {
1763 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1764 ofproto->up.name, port_num);
1765 } else {
1766 struct eth_header *eth = pkt->l2;
1767
1768 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1769 if (eth_addr_is_zero(eth->eth_src)) {
1770 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1771 "with unknown MAC", ofproto->up.name, port_num);
1772 } else {
91d6cd12 1773 ofproto_dpif_send_packet(ofport, pkt);
21f7563c
JP
1774 }
1775 }
1776 ofpbuf_delete(pkt);
1777}
1778
1779/* Configures STP on 'ofproto_' using the settings defined in 's'. */
1780static int
1781set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1782{
1783 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1784
1785 /* Only revalidate flows if the configuration changed. */
1786 if (!s != !ofproto->stp) {
2cc3c58e 1787 ofproto->backer->need_revalidate = REV_RECONFIGURE;
21f7563c
JP
1788 }
1789
1790 if (s) {
1791 if (!ofproto->stp) {
1792 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1793 send_bpdu_cb, ofproto);
1794 ofproto->stp_last_tick = time_msec();
1795 }
1796
1797 stp_set_bridge_id(ofproto->stp, s->system_id);
1798 stp_set_bridge_priority(ofproto->stp, s->priority);
1799 stp_set_hello_time(ofproto->stp, s->hello_time);
1800 stp_set_max_age(ofproto->stp, s->max_age);
1801 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1802 } else {
851bf71d
EJ
1803 struct ofport *ofport;
1804
1805 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1806 set_stp_port(ofport, NULL);
1807 }
1808
bd54dbcd 1809 stp_unref(ofproto->stp);
21f7563c
JP
1810 ofproto->stp = NULL;
1811 }
1812
1813 return 0;
1814}
1815
1816static int
1817get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1818{
1819 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1820
1821 if (ofproto->stp) {
1822 s->enabled = true;
1823 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1824 s->designated_root = stp_get_designated_root(ofproto->stp);
1825 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1826 } else {
1827 s->enabled = false;
1828 }
1829
1830 return 0;
1831}
1832
1833static void
1834update_stp_port_state(struct ofport_dpif *ofport)
1835{
1836 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1837 enum stp_state state;
1838
1839 /* Figure out new state. */
1840 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1841 : STP_DISABLED;
1842
1843 /* Update state. */
1844 if (ofport->stp_state != state) {
9e1fd49b 1845 enum ofputil_port_state of_state;
21f7563c
JP
1846 bool fwd_change;
1847
1848 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1849 netdev_get_name(ofport->up.netdev),
1850 stp_state_name(ofport->stp_state),
1851 stp_state_name(state));
1852 if (stp_learn_in_state(ofport->stp_state)
1853 != stp_learn_in_state(state)) {
1854 /* xxx Learning action flows should also be flushed. */
509c0149 1855 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 1856 mac_learning_flush(ofproto->ml);
509c0149 1857 ovs_rwlock_unlock(&ofproto->ml->rwlock);
21f7563c
JP
1858 }
1859 fwd_change = stp_forward_in_state(ofport->stp_state)
1860 != stp_forward_in_state(state);
1861
2cc3c58e 1862 ofproto->backer->need_revalidate = REV_STP;
21f7563c
JP
1863 ofport->stp_state = state;
1864 ofport->stp_state_entered = time_msec();
1865
b308140a 1866 if (fwd_change && ofport->bundle) {
21f7563c
JP
1867 bundle_update(ofport->bundle);
1868 }
1869
1870 /* Update the STP state bits in the OpenFlow port description. */
9e1fd49b
BP
1871 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1872 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1873 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1874 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1875 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1876 : 0);
21f7563c
JP
1877 ofproto_port_set_state(&ofport->up, of_state);
1878 }
1879}
1880
1881/* Configures STP on 'ofport_' using the settings defined in 's'. The
1882 * caller is responsible for assigning STP port numbers and ensuring
1883 * there are no duplicates. */
1884static int
1885set_stp_port(struct ofport *ofport_,
1886 const struct ofproto_port_stp_settings *s)
1887{
1888 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1889 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1890 struct stp_port *sp = ofport->stp_port;
1891
1892 if (!s || !s->enable) {
1893 if (sp) {
1894 ofport->stp_port = NULL;
1895 stp_port_disable(sp);
ecd12731 1896 update_stp_port_state(ofport);
21f7563c
JP
1897 }
1898 return 0;
1899 } else if (sp && stp_port_no(sp) != s->port_num
1900 && ofport == stp_port_get_aux(sp)) {
1901 /* The port-id changed, so disable the old one if it's not
1902 * already in use by another port. */
1903 stp_port_disable(sp);
1904 }
1905
1906 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1907 stp_port_enable(sp);
1908
1909 stp_port_set_aux(sp, ofport);
1910 stp_port_set_priority(sp, s->priority);
1911 stp_port_set_path_cost(sp, s->path_cost);
1912
1913 update_stp_port_state(ofport);
1914
1915 return 0;
1916}
1917
1918static int
1919get_stp_port_status(struct ofport *ofport_,
1920 struct ofproto_port_stp_status *s)
1921{
1922 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1923 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1924 struct stp_port *sp = ofport->stp_port;
1925
1926 if (!ofproto->stp || !sp) {
1927 s->enabled = false;
1928 return 0;
1929 }
1930
1931 s->enabled = true;
1932 s->port_id = stp_port_get_id(sp);
1933 s->state = stp_port_get_state(sp);
1934 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1935 s->role = stp_port_get_role(sp);
fd28ce3a
JS
1936
1937 return 0;
1938}
1939
1940static int
1941get_stp_port_stats(struct ofport *ofport_,
1942 struct ofproto_port_stp_stats *s)
1943{
1944 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1945 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1946 struct stp_port *sp = ofport->stp_port;
1947
1948 if (!ofproto->stp || !sp) {
1949 s->enabled = false;
1950 return 0;
1951 }
1952
1953 s->enabled = true;
80740385 1954 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
21f7563c
JP
1955
1956 return 0;
1957}
1958
1959static void
1960stp_run(struct ofproto_dpif *ofproto)
1961{
1962 if (ofproto->stp) {
1963 long long int now = time_msec();
1964 long long int elapsed = now - ofproto->stp_last_tick;
1965 struct stp_port *sp;
1966
1967 if (elapsed > 0) {
1968 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1969 ofproto->stp_last_tick = now;
1970 }
1971 while (stp_get_changed_port(ofproto->stp, &sp)) {
1972 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1973
1974 if (ofport) {
1975 update_stp_port_state(ofport);
1976 }
1977 }
6ae50723
EJ
1978
1979 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
509c0149 1980 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 1981 mac_learning_flush(ofproto->ml);
509c0149 1982 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6ae50723 1983 }
21f7563c
JP
1984 }
1985}
1986
1987static void
1988stp_wait(struct ofproto_dpif *ofproto)
1989{
1990 if (ofproto->stp) {
1991 poll_timer_wait(1000);
1992 }
1993}
21f7563c 1994\f
8b36f51e 1995static int
55954f6e 1996set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
8b36f51e
EJ
1997 size_t n_qdscp)
1998{
1999 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2000 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
8b36f51e 2001
55954f6e
EJ
2002 if (ofport->n_qdscp != n_qdscp
2003 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2004 n_qdscp * sizeof *qdscp))) {
2cc3c58e 2005 ofproto->backer->need_revalidate = REV_RECONFIGURE;
55954f6e
EJ
2006 free(ofport->qdscp);
2007 ofport->qdscp = n_qdscp
2008 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2009 : NULL;
2010 ofport->n_qdscp = n_qdscp;
8b36f51e
EJ
2011 }
2012
8b36f51e
EJ
2013 return 0;
2014}
2015\f
abe529af
BP
2016/* Bundles. */
2017
b44a10b7
BP
2018/* Expires all MAC learning entries associated with 'bundle' and forces its
2019 * ofproto to revalidate every flow.
2020 *
2021 * Normally MAC learning entries are removed only from the ofproto associated
2022 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2023 * are removed from every ofproto. When patch ports and SLB bonds are in use
2024 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2025 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2026 * with the host from which it migrated. */
abe529af 2027static void
b44a10b7 2028bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
abe529af
BP
2029{
2030 struct ofproto_dpif *ofproto = bundle->ofproto;
2031 struct mac_learning *ml = ofproto->ml;
2032 struct mac_entry *mac, *next_mac;
2033
2cc3c58e 2034 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 2035 ovs_rwlock_wrlock(&ml->rwlock);
abe529af
BP
2036 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2037 if (mac->port.p == bundle) {
b44a10b7
BP
2038 if (all_ofprotos) {
2039 struct ofproto_dpif *o;
2040
2041 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2042 if (o != ofproto) {
2043 struct mac_entry *e;
2044
509c0149 2045 ovs_rwlock_wrlock(&o->ml->rwlock);
30618594 2046 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
b44a10b7 2047 if (e) {
b44a10b7
BP
2048 mac_learning_expire(o->ml, e);
2049 }
509c0149 2050 ovs_rwlock_unlock(&o->ml->rwlock);
b44a10b7
BP
2051 }
2052 }
2053 }
2054
abe529af
BP
2055 mac_learning_expire(ml, mac);
2056 }
2057 }
509c0149 2058 ovs_rwlock_unlock(&ml->rwlock);
abe529af
BP
2059}
2060
2061static struct ofbundle *
2062bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2063{
2064 struct ofbundle *bundle;
2065
2066 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2067 &ofproto->bundles) {
2068 if (bundle->aux == aux) {
2069 return bundle;
2070 }
2071 }
2072 return NULL;
2073}
2074
7bde8dd8
JP
2075static void
2076bundle_update(struct ofbundle *bundle)
2077{
2078 struct ofport_dpif *port;
2079
2080 bundle->floodable = true;
2081 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
9e1fd49b 2082 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
a6363cfd 2083 || port->is_layer3
9e1fd49b 2084 || !stp_forward_in_state(port->stp_state)) {
7bde8dd8
JP
2085 bundle->floodable = false;
2086 break;
2087 }
2088 }
2089}
2090
abe529af
BP
2091static void
2092bundle_del_port(struct ofport_dpif *port)
2093{
2094 struct ofbundle *bundle = port->bundle;
2095
2cc3c58e 2096 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
6f77f4ae 2097
abe529af
BP
2098 list_remove(&port->bundle_node);
2099 port->bundle = NULL;
2100
2101 if (bundle->lacp) {
2102 lacp_slave_unregister(bundle->lacp, port);
2103 }
2104 if (bundle->bond) {
2105 bond_slave_unregister(bundle->bond, port);
2106 }
2107
7bde8dd8 2108 bundle_update(bundle);
abe529af
BP
2109}
2110
2111static bool
4e022ec0 2112bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
df53d41c 2113 struct lacp_slave_settings *lacp)
abe529af
BP
2114{
2115 struct ofport_dpif *port;
2116
2117 port = get_ofp_port(bundle->ofproto, ofp_port);
2118 if (!port) {
2119 return false;
2120 }
2121
2122 if (port->bundle != bundle) {
2cc3c58e 2123 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af 2124 if (port->bundle) {
e019a574 2125 bundle_remove(&port->up);
abe529af
BP
2126 }
2127
2128 port->bundle = bundle;
2129 list_push_back(&bundle->ports, &port->bundle_node);
9e1fd49b 2130 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
a6363cfd 2131 || port->is_layer3
9e1fd49b 2132 || !stp_forward_in_state(port->stp_state)) {
abe529af
BP
2133 bundle->floodable = false;
2134 }
2135 }
2136 if (lacp) {
2cc3c58e 2137 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2138 lacp_slave_register(bundle->lacp, port, lacp);
2139 }
2140
2141 return true;
2142}
2143
2144static void
2145bundle_destroy(struct ofbundle *bundle)
2146{
2147 struct ofproto_dpif *ofproto;
2148 struct ofport_dpif *port, *next_port;
abe529af
BP
2149
2150 if (!bundle) {
2151 return;
2152 }
2153
2154 ofproto = bundle->ofproto;
ec7ceaed 2155 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
abe529af 2156
dc24a00f 2157 ovs_rwlock_wrlock(&xlate_rwlock);
46c88433 2158 xlate_bundle_remove(bundle);
dc24a00f 2159 ovs_rwlock_unlock(&xlate_rwlock);
46c88433 2160
abe529af
BP
2161 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2162 bundle_del_port(port);
2163 }
2164
b44a10b7 2165 bundle_flush_macs(bundle, true);
abe529af
BP
2166 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2167 free(bundle->name);
2168 free(bundle->trunks);
91779071 2169 lacp_unref(bundle->lacp);
03366a2d 2170 bond_unref(bundle->bond);
abe529af
BP
2171 free(bundle);
2172}
2173
2174static int
2175bundle_set(struct ofproto *ofproto_, void *aux,
2176 const struct ofproto_bundle_settings *s)
2177{
2178 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2179 bool need_flush = false;
abe529af
BP
2180 struct ofport_dpif *port;
2181 struct ofbundle *bundle;
ecac4ebf
BP
2182 unsigned long *trunks;
2183 int vlan;
abe529af
BP
2184 size_t i;
2185 bool ok;
2186
2187 if (!s) {
2188 bundle_destroy(bundle_lookup(ofproto, aux));
2189 return 0;
2190 }
2191
cb22974d
BP
2192 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2193 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
abe529af
BP
2194
2195 bundle = bundle_lookup(ofproto, aux);
2196 if (!bundle) {
2197 bundle = xmalloc(sizeof *bundle);
2198
2199 bundle->ofproto = ofproto;
2200 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2201 hash_pointer(aux, 0));
2202 bundle->aux = aux;
2203 bundle->name = NULL;
2204
2205 list_init(&bundle->ports);
ecac4ebf 2206 bundle->vlan_mode = PORT_VLAN_TRUNK;
abe529af
BP
2207 bundle->vlan = -1;
2208 bundle->trunks = NULL;
5e9ceccd 2209 bundle->use_priority_tags = s->use_priority_tags;
abe529af
BP
2210 bundle->lacp = NULL;
2211 bundle->bond = NULL;
2212
2213 bundle->floodable = true;
ec7ceaed 2214 mbridge_register_bundle(ofproto->mbridge, bundle);
abe529af
BP
2215 }
2216
2217 if (!bundle->name || strcmp(s->name, bundle->name)) {
2218 free(bundle->name);
2219 bundle->name = xstrdup(s->name);
2220 }
2221
2222 /* LACP. */
2223 if (s->lacp) {
e2d13c43 2224 ofproto->lacp_enabled = true;
abe529af 2225 if (!bundle->lacp) {
2cc3c58e 2226 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2227 bundle->lacp = lacp_create();
2228 }
2229 lacp_configure(bundle->lacp, s->lacp);
2230 } else {
91779071 2231 lacp_unref(bundle->lacp);
abe529af
BP
2232 bundle->lacp = NULL;
2233 }
2234
2235 /* Update set of ports. */
2236 ok = true;
2237 for (i = 0; i < s->n_slaves; i++) {
2238 if (!bundle_add_port(bundle, s->slaves[i],
df53d41c 2239 s->lacp ? &s->lacp_slaves[i] : NULL)) {
abe529af
BP
2240 ok = false;
2241 }
2242 }
2243 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2244 struct ofport_dpif *next_port;
2245
2246 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2247 for (i = 0; i < s->n_slaves; i++) {
56c769ab 2248 if (s->slaves[i] == port->up.ofp_port) {
abe529af
BP
2249 goto found;
2250 }
2251 }
2252
2253 bundle_del_port(port);
2254 found: ;
2255 }
2256 }
cb22974d 2257 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
abe529af
BP
2258
2259 if (list_is_empty(&bundle->ports)) {
2260 bundle_destroy(bundle);
2261 return EINVAL;
2262 }
2263
ecac4ebf 2264 /* Set VLAN tagging mode */
5e9ceccd
BP
2265 if (s->vlan_mode != bundle->vlan_mode
2266 || s->use_priority_tags != bundle->use_priority_tags) {
ecac4ebf 2267 bundle->vlan_mode = s->vlan_mode;
5e9ceccd 2268 bundle->use_priority_tags = s->use_priority_tags;
ecac4ebf
BP
2269 need_flush = true;
2270 }
2271
abe529af 2272 /* Set VLAN tag. */
ecac4ebf
BP
2273 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2274 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2275 : 0);
2276 if (vlan != bundle->vlan) {
2277 bundle->vlan = vlan;
abe529af
BP
2278 need_flush = true;
2279 }
2280
2281 /* Get trunked VLANs. */
ecac4ebf
BP
2282 switch (s->vlan_mode) {
2283 case PORT_VLAN_ACCESS:
2284 trunks = NULL;
2285 break;
2286
2287 case PORT_VLAN_TRUNK:
ebc56baa 2288 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
2289 break;
2290
2291 case PORT_VLAN_NATIVE_UNTAGGED:
2292 case PORT_VLAN_NATIVE_TAGGED:
2293 if (vlan != 0 && (!s->trunks
2294 || !bitmap_is_set(s->trunks, vlan)
2295 || bitmap_is_set(s->trunks, 0))) {
2296 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2297 if (s->trunks) {
2298 trunks = bitmap_clone(s->trunks, 4096);
2299 } else {
2300 trunks = bitmap_allocate1(4096);
2301 }
2302 bitmap_set1(trunks, vlan);
2303 bitmap_set0(trunks, 0);
2304 } else {
ebc56baa 2305 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
2306 }
2307 break;
2308
2309 default:
428b2edd 2310 OVS_NOT_REACHED();
ecac4ebf 2311 }
abe529af
BP
2312 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2313 free(bundle->trunks);
ecac4ebf
BP
2314 if (trunks == s->trunks) {
2315 bundle->trunks = vlan_bitmap_clone(trunks);
2316 } else {
2317 bundle->trunks = trunks;
2318 trunks = NULL;
2319 }
abe529af
BP
2320 need_flush = true;
2321 }
ecac4ebf
BP
2322 if (trunks != s->trunks) {
2323 free(trunks);
2324 }
abe529af
BP
2325
2326 /* Bonding. */
2327 if (!list_is_short(&bundle->ports)) {
2328 bundle->ofproto->has_bonded_bundles = true;
2329 if (bundle->bond) {
2330 if (bond_reconfigure(bundle->bond, s->bond)) {
2cc3c58e 2331 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2332 }
2333 } else {
2334 bundle->bond = bond_create(s->bond);
2cc3c58e 2335 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2336 }
2337
2338 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
df53d41c 2339 bond_slave_register(bundle->bond, port, port->up.netdev);
abe529af
BP
2340 }
2341 } else {
03366a2d 2342 bond_unref(bundle->bond);
abe529af
BP
2343 bundle->bond = NULL;
2344 }
2345
2346 /* If we changed something that would affect MAC learning, un-learn
2347 * everything on this port and force flow revalidation. */
2348 if (need_flush) {
b44a10b7 2349 bundle_flush_macs(bundle, false);
abe529af
BP
2350 }
2351
2352 return 0;
2353}
2354
2355static void
2356bundle_remove(struct ofport *port_)
2357{
2358 struct ofport_dpif *port = ofport_dpif_cast(port_);
2359 struct ofbundle *bundle = port->bundle;
2360
2361 if (bundle) {
2362 bundle_del_port(port);
2363 if (list_is_empty(&bundle->ports)) {
2364 bundle_destroy(bundle);
2365 } else if (list_is_short(&bundle->ports)) {
03366a2d 2366 bond_unref(bundle->bond);
abe529af
BP
2367 bundle->bond = NULL;
2368 }
2369 }
2370}
2371
2372static void
5f877369 2373send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
abe529af
BP
2374{
2375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2376 struct ofport_dpif *port = port_;
2377 uint8_t ea[ETH_ADDR_LEN];
2378 int error;
2379
2380 error = netdev_get_etheraddr(port->up.netdev, ea);
2381 if (!error) {
abe529af 2382 struct ofpbuf packet;
5f877369 2383 void *packet_pdu;
abe529af
BP
2384
2385 ofpbuf_init(&packet, 0);
2386 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
5f877369
EJ
2387 pdu_size);
2388 memcpy(packet_pdu, pdu, pdu_size);
2389
91d6cd12 2390 ofproto_dpif_send_packet(port, &packet);
abe529af
BP
2391 ofpbuf_uninit(&packet);
2392 } else {
2393 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2394 "%s (%s)", port->bundle->name,
10a89ef0 2395 netdev_get_name(port->up.netdev), ovs_strerror(error));
abe529af
BP
2396 }
2397}
2398
2399static void
2400bundle_send_learning_packets(struct ofbundle *bundle)
2401{
2402 struct ofproto_dpif *ofproto = bundle->ofproto;
0165217e 2403 struct ofpbuf *learning_packet;
abe529af
BP
2404 int error, n_packets, n_errors;
2405 struct mac_entry *e;
0165217e 2406 struct list packets;
abe529af 2407
0165217e 2408 list_init(&packets);
509c0149 2409 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
abe529af
BP
2410 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2411 if (e->port.p != bundle) {
4dd1e3ca 2412 void *port_void;
ea131871 2413
4dd1e3ca
BP
2414 learning_packet = bond_compose_learning_packet(bundle->bond,
2415 e->mac, e->vlan,
2416 &port_void);
0165217e
EJ
2417 learning_packet->private_p = port_void;
2418 list_push_back(&packets, &learning_packet->list_node);
abe529af
BP
2419 }
2420 }
509c0149 2421 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af 2422
0165217e
EJ
2423 error = n_packets = n_errors = 0;
2424 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2425 int ret;
2426
91d6cd12 2427 ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
0165217e
EJ
2428 if (ret) {
2429 error = ret;
2430 n_errors++;
2431 }
2432 n_packets++;
2433 }
2434 ofpbuf_list_delete(&packets);
2435
abe529af
BP
2436 if (n_errors) {
2437 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2438 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2439 "packets, last error was: %s",
10a89ef0 2440 bundle->name, n_errors, n_packets, ovs_strerror(error));
abe529af
BP
2441 } else {
2442 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2443 bundle->name, n_packets);
2444 }
2445}
2446
2447static void
2448bundle_run(struct ofbundle *bundle)
2449{
2450 if (bundle->lacp) {
2451 lacp_run(bundle->lacp, send_pdu_cb);
2452 }
2453 if (bundle->bond) {
2454 struct ofport_dpif *port;
2455
2456 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
015e08bc 2457 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
abe529af
BP
2458 }
2459
4a1b8f30
EJ
2460 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2461 bundle->ofproto->backer->need_revalidate = REV_BOND;
2462 }
2463
abe529af
BP
2464 if (bond_should_send_learning_packets(bundle->bond)) {
2465 bundle_send_learning_packets(bundle);
2466 }
2467 }
2468}
2469
2470static void
2471bundle_wait(struct ofbundle *bundle)
2472{
2473 if (bundle->lacp) {
2474 lacp_wait(bundle->lacp);
2475 }
2476 if (bundle->bond) {
2477 bond_wait(bundle->bond);
2478 }
2479}
2480\f
2481/* Mirrors. */
2482
2483static int
ec7ceaed
EJ
2484mirror_set__(struct ofproto *ofproto_, void *aux,
2485 const struct ofproto_mirror_settings *s)
abe529af
BP
2486{
2487 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
ec7ceaed
EJ
2488 struct ofbundle **srcs, **dsts;
2489 int error;
2490 size_t i;
abe529af 2491
abe529af 2492 if (!s) {
ec7ceaed 2493 mirror_destroy(ofproto->mbridge, aux);
abe529af
BP
2494 return 0;
2495 }
2496
ec7ceaed
EJ
2497 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2498 dsts = xmalloc(s->n_dsts * sizeof *dsts);
abe529af 2499
ec7ceaed
EJ
2500 for (i = 0; i < s->n_srcs; i++) {
2501 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
abe529af
BP
2502 }
2503
ec7ceaed
EJ
2504 for (i = 0; i < s->n_dsts; i++) {
2505 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
abe529af
BP
2506 }
2507
ec7ceaed
EJ
2508 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2509 s->n_dsts, s->src_vlans,
2510 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2511 free(srcs);
2512 free(dsts);
2513 return error;
abe529af
BP
2514}
2515
9d24de3b 2516static int
ec7ceaed
EJ
2517mirror_get_stats__(struct ofproto *ofproto, void *aux,
2518 uint64_t *packets, uint64_t *bytes)
9d24de3b 2519{
ec7ceaed
EJ
2520 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2521 bytes);
9d24de3b
JP
2522}
2523
abe529af
BP
2524static int
2525set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2526{
2527 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 2528 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
abe529af 2529 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
30618594 2530 mac_learning_flush(ofproto->ml);
abe529af 2531 }
509c0149 2532 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af
BP
2533 return 0;
2534}
2535
2536static bool
b4affc74 2537is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
abe529af
BP
2538{
2539 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2540 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
ec7ceaed 2541 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
abe529af 2542}
8402c74b
SS
2543
2544static void
b53055f4 2545forward_bpdu_changed(struct ofproto *ofproto_)
8402c74b
SS
2546{
2547 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2cc3c58e 2548 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8402c74b 2549}
e764773c
BP
2550
2551static void
c4069512
BP
2552set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2553 size_t max_entries)
e764773c
BP
2554{
2555 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 2556 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
e764773c 2557 mac_learning_set_idle_time(ofproto->ml, idle_time);
c4069512 2558 mac_learning_set_max_entries(ofproto->ml, max_entries);
509c0149 2559 ovs_rwlock_unlock(&ofproto->ml->rwlock);
e764773c 2560}
abe529af
BP
2561\f
2562/* Ports. */
2563
46c88433 2564static struct ofport_dpif *
4e022ec0 2565get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
abe529af 2566{
7df6a8bd
BP
2567 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2568 return ofport ? ofport_dpif_cast(ofport) : NULL;
abe529af
BP
2569}
2570
abe529af 2571static void
e1b1d06a
JP
2572ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2573 struct ofproto_port *ofproto_port,
abe529af
BP
2574 struct dpif_port *dpif_port)
2575{
2576 ofproto_port->name = dpif_port->name;
2577 ofproto_port->type = dpif_port->type;
e1b1d06a 2578 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
abe529af
BP
2579}
2580
6cbbf4fa
EJ
2581static void
2582ofport_update_peer(struct ofport_dpif *ofport)
0a740f48
EJ
2583{
2584 const struct ofproto_dpif *ofproto;
6cbbf4fa 2585 struct dpif_backer *backer;
161b6042 2586 char *peer_name;
6cbbf4fa
EJ
2587
2588 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2589 return;
2590 }
2591
2592 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
7894e7da 2593 backer->need_revalidate = REV_RECONFIGURE;
0a740f48 2594
6cbbf4fa
EJ
2595 if (ofport->peer) {
2596 ofport->peer->peer = NULL;
2597 ofport->peer = NULL;
2598 }
2599
2600 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2601 if (!peer_name) {
2602 return;
0a740f48
EJ
2603 }
2604
2605 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6cbbf4fa
EJ
2606 struct ofport *peer_ofport;
2607 struct ofport_dpif *peer;
161b6042 2608 char *peer_peer;
0a740f48 2609
462abef2
EJ
2610 if (ofproto->backer != backer) {
2611 continue;
2612 }
2613
6cbbf4fa
EJ
2614 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2615 if (!peer_ofport) {
2616 continue;
2617 }
2618
2619 peer = ofport_dpif_cast(peer_ofport);
2620 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2621 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2622 peer_peer)) {
2623 ofport->peer = peer;
2624 ofport->peer->peer = ofport;
0a740f48 2625 }
161b6042 2626 free(peer_peer);
6cbbf4fa 2627
161b6042 2628 break;
0a740f48 2629 }
161b6042 2630 free(peer_name);
0a740f48
EJ
2631}
2632
abe529af
BP
2633static void
2634port_run(struct ofport_dpif *ofport)
2635{
3e5b3fdb
EJ
2636 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2637 bool carrier_changed = carrier_seq != ofport->carrier_seq;
015e08bc 2638 bool enable = netdev_get_carrier(ofport->up.netdev);
2d344ba5
AW
2639 bool cfm_enable = false;
2640 bool bfd_enable = false;
015e08bc 2641
3e5b3fdb
EJ
2642 ofport->carrier_seq = carrier_seq;
2643
abe529af 2644 if (ofport->cfm) {
4653c558
EJ
2645 int cfm_opup = cfm_get_opup(ofport->cfm);
2646
2d344ba5 2647 cfm_enable = !cfm_get_fault(ofport->cfm);
4653c558
EJ
2648
2649 if (cfm_opup >= 0) {
2d344ba5 2650 cfm_enable = cfm_enable && cfm_opup;
4653c558 2651 }
abe529af 2652 }
015e08bc 2653
ccc09689 2654 if (ofport->bfd) {
2d344ba5
AW
2655 bfd_enable = bfd_forwarding(ofport->bfd);
2656 }
2657
2658 if (ofport->bfd || ofport->cfm) {
2659 enable = enable && (cfm_enable || bfd_enable);
ccc09689
EJ
2660 }
2661
015e08bc
EJ
2662 if (ofport->bundle) {
2663 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3e5b3fdb
EJ
2664 if (carrier_changed) {
2665 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2666 }
015e08bc
EJ
2667 }
2668
daff3353
EJ
2669 if (ofport->may_enable != enable) {
2670 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
83341407 2671 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
daff3353
EJ
2672 }
2673
015e08bc 2674 ofport->may_enable = enable;
abe529af
BP
2675}
2676
abe529af
BP
2677static int
2678port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2679 struct ofproto_port *ofproto_port)
2680{
2681 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2682 struct dpif_port dpif_port;
2683 int error;
2684
0a740f48
EJ
2685 if (sset_contains(&ofproto->ghost_ports, devname)) {
2686 const char *type = netdev_get_type_from_name(devname);
2687
2688 /* We may be called before ofproto->up.port_by_name is populated with
2689 * the appropriate ofport. For this reason, we must get the name and
2690 * type from the netdev layer directly. */
2691 if (type) {
2692 const struct ofport *ofport;
2693
2694 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2695 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2696 ofproto_port->name = xstrdup(devname);
2697 ofproto_port->type = xstrdup(type);
2698 return 0;
2699 }
2700 return ENODEV;
2701 }
2702
acf60855
JP
2703 if (!sset_contains(&ofproto->ports, devname)) {
2704 return ENODEV;
2705 }
2706 error = dpif_port_query_by_name(ofproto->backer->dpif,
2707 devname, &dpif_port);
abe529af 2708 if (!error) {
e1b1d06a 2709 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
abe529af
BP
2710 }
2711 return error;
2712}
2713
2714static int
e1b1d06a 2715port_add(struct ofproto *ofproto_, struct netdev *netdev)
abe529af
BP
2716{
2717 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
b9ad7294 2718 const char *devname = netdev_get_name(netdev);
3aa30359
BP
2719 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2720 const char *dp_port_name;
abe529af 2721
0a740f48
EJ
2722 if (netdev_vport_is_patch(netdev)) {
2723 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2724 return 0;
2725 }
2726
3aa30359 2727 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
b9ad7294 2728 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
4e022ec0 2729 odp_port_t port_no = ODPP_NONE;
7d82ab2e
KM
2730 int error;
2731
2732 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
b9ad7294
EJ
2733 if (error) {
2734 return error;
2735 }
7d82ab2e 2736 if (netdev_get_tunnel_config(netdev)) {
4e022ec0
AW
2737 simap_put(&ofproto->backer->tnl_backers,
2738 dp_port_name, odp_to_u32(port_no));
7d82ab2e 2739 }
acf60855 2740 }
b9ad7294
EJ
2741
2742 if (netdev_get_tunnel_config(netdev)) {
2743 sset_add(&ofproto->ghost_ports, devname);
b9ad7294
EJ
2744 } else {
2745 sset_add(&ofproto->ports, devname);
2746 }
2747 return 0;
2748}
2749
abe529af 2750static int
4e022ec0 2751port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
abe529af
BP
2752{
2753 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
b9ad7294 2754 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
e1b1d06a 2755 int error = 0;
abe529af 2756
b9ad7294
EJ
2757 if (!ofport) {
2758 return 0;
e1b1d06a 2759 }
b9ad7294
EJ
2760
2761 sset_find_and_delete(&ofproto->ghost_ports,
2762 netdev_get_name(ofport->up.netdev));
a614d823 2763 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8911c674 2764 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
b9ad7294
EJ
2765 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2766 if (!error) {
abe529af
BP
2767 /* The caller is going to close ofport->up.netdev. If this is a
2768 * bonded port, then the bond is using that netdev, so remove it
2769 * from the bond. The client will need to reconfigure everything
2770 * after deleting ports, so then the slave will get re-added. */
2771 bundle_remove(&ofport->up);
2772 }
2773 }
2774 return error;
2775}
2776
6527c598
PS
2777static int
2778port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2779{
2780 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2781 int error;
2782
2783 error = netdev_get_stats(ofport->up.netdev, stats);
2784
ee382d89 2785 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
6527c598
PS
2786 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2787
0da3c61b 2788 ovs_mutex_lock(&ofproto->stats_mutex);
6527c598
PS
2789 /* ofproto->stats.tx_packets represents packets that we created
2790 * internally and sent to some port (e.g. packets sent with
91d6cd12
AW
2791 * ofproto_dpif_send_packet()). Account for them as if they had
2792 * come from OFPP_LOCAL and got forwarded. */
6527c598
PS
2793
2794 if (stats->rx_packets != UINT64_MAX) {
2795 stats->rx_packets += ofproto->stats.tx_packets;
2796 }
2797
2798 if (stats->rx_bytes != UINT64_MAX) {
2799 stats->rx_bytes += ofproto->stats.tx_bytes;
2800 }
2801
2802 /* ofproto->stats.rx_packets represents packets that were received on
2803 * some port and we processed internally and dropped (e.g. STP).
4e090bc7 2804 * Account for them as if they had been forwarded to OFPP_LOCAL. */
6527c598
PS
2805
2806 if (stats->tx_packets != UINT64_MAX) {
2807 stats->tx_packets += ofproto->stats.rx_packets;
2808 }
2809
2810 if (stats->tx_bytes != UINT64_MAX) {
2811 stats->tx_bytes += ofproto->stats.rx_bytes;
2812 }
0da3c61b 2813 ovs_mutex_unlock(&ofproto->stats_mutex);
6527c598
PS
2814 }
2815
2816 return error;
2817}
2818
abe529af 2819struct port_dump_state {
acf60855
JP
2820 uint32_t bucket;
2821 uint32_t offset;
0a740f48 2822 bool ghost;
da78d43d
BP
2823
2824 struct ofproto_port port;
2825 bool has_port;
abe529af
BP
2826};
2827
2828static int
acf60855 2829port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
abe529af 2830{
0a740f48 2831 *statep = xzalloc(sizeof(struct port_dump_state));
abe529af
BP
2832 return 0;
2833}
2834
2835static int
b9ad7294 2836port_dump_next(const struct ofproto *ofproto_, void *state_,
abe529af
BP
2837 struct ofproto_port *port)
2838{
e1b1d06a 2839 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 2840 struct port_dump_state *state = state_;
0a740f48 2841 const struct sset *sset;
acf60855 2842 struct sset_node *node;
abe529af 2843
da78d43d
BP
2844 if (state->has_port) {
2845 ofproto_port_destroy(&state->port);
2846 state->has_port = false;
2847 }
0a740f48
EJ
2848 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2849 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
acf60855
JP
2850 int error;
2851
da78d43d
BP
2852 error = port_query_by_name(ofproto_, node->name, &state->port);
2853 if (!error) {
2854 *port = state->port;
2855 state->has_port = true;
2856 return 0;
2857 } else if (error != ENODEV) {
acf60855
JP
2858 return error;
2859 }
abe529af 2860 }
acf60855 2861
0a740f48
EJ
2862 if (!state->ghost) {
2863 state->ghost = true;
2864 state->bucket = 0;
2865 state->offset = 0;
2866 return port_dump_next(ofproto_, state_, port);
2867 }
2868
acf60855 2869 return EOF;
abe529af
BP
2870}
2871
2872static int
2873port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2874{
2875 struct port_dump_state *state = state_;
2876
da78d43d
BP
2877 if (state->has_port) {
2878 ofproto_port_destroy(&state->port);
2879 }
abe529af
BP
2880 free(state);
2881 return 0;
2882}
2883
2884static int
2885port_poll(const struct ofproto *ofproto_, char **devnamep)
2886{
2887 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855
JP
2888
2889 if (ofproto->port_poll_errno) {
2890 int error = ofproto->port_poll_errno;
2891 ofproto->port_poll_errno = 0;
2892 return error;
2893 }
2894
2895 if (sset_is_empty(&ofproto->port_poll_set)) {
2896 return EAGAIN;
2897 }
2898
2899 *devnamep = sset_pop(&ofproto->port_poll_set);
2900 return 0;
abe529af
BP
2901}
2902
2903static void
2904port_poll_wait(const struct ofproto *ofproto_)
2905{
2906 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855 2907 dpif_port_poll_wait(ofproto->backer->dpif);
abe529af
BP
2908}
2909
2910static int
2911port_is_lacp_current(const struct ofport *ofport_)
2912{
2913 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2914 return (ofport->bundle && ofport->bundle->lacp
2915 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2916 : -1);
2917}
2918\f
e79a6c83
EJ
2919/* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2920 * then delete it entirely. */
2921static void
2922rule_expire(struct rule_dpif *rule)
2923 OVS_REQUIRES(ofproto_mutex)
9d6ac44e 2924{
dc437090 2925 uint16_t hard_timeout, idle_timeout;
e79a6c83 2926 long long int now = time_msec();
dc437090 2927 int reason = -1;
9d6ac44e 2928
e79a6c83 2929 ovs_assert(!rule->up.pending);
9d6ac44e 2930
e79a6c83
EJ
2931 hard_timeout = rule->up.hard_timeout;
2932 idle_timeout = rule->up.idle_timeout;
dc437090
JR
2933
2934 /* Has 'rule' expired? */
2935 if (hard_timeout) {
2936 long long int modified;
2937
2938 ovs_mutex_lock(&rule->up.mutex);
2939 modified = rule->up.modified;
2940 ovs_mutex_unlock(&rule->up.mutex);
2941
2942 if (now > modified + hard_timeout * 1000) {
2943 reason = OFPRR_HARD_TIMEOUT;
2944 }
2945 }
2946
2947 if (reason < 0 && idle_timeout) {
2948 long long int used;
2949
2950 ovs_mutex_lock(&rule->stats_mutex);
1a4ec18d 2951 used = rule->stats.used;
dc437090
JR
2952 ovs_mutex_unlock(&rule->stats_mutex);
2953
2954 if (now > used + idle_timeout * 1000) {
2955 reason = OFPRR_IDLE_TIMEOUT;
2956 }
f9c37233 2957 }
501f8d1f 2958
e79a6c83
EJ
2959 if (reason >= 0) {
2960 COVERAGE_INC(ofproto_dpif_expired);
2961 ofproto_rule_expire(&rule->up, reason);
a41d7bf2 2962 }
e79a6c83 2963}
a41d7bf2 2964
e79a6c83
EJ
2965/* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2966 * 'flow' must reflect the data in 'packet'. */
2967int
2968ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2969 const struct flow *flow,
2970 struct rule_dpif *rule,
2971 const struct ofpact *ofpacts, size_t ofpacts_len,
2972 struct ofpbuf *packet)
2973{
e79a6c83
EJ
2974 struct dpif_flow_stats stats;
2975 struct xlate_out xout;
2976 struct xlate_in xin;
2977 ofp_port_t in_port;
758c456d 2978 struct dpif_execute execute;
e79a6c83 2979 int error;
f9c37233 2980
e79a6c83 2981 ovs_assert((rule != NULL) != (ofpacts != NULL));
501f8d1f 2982
e79a6c83
EJ
2983 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2984 if (rule) {
2985 rule_dpif_credit_stats(rule, &stats);
2986 }
c84451a6 2987
e79a6c83
EJ
2988 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2989 xin.ofpacts = ofpacts;
2990 xin.ofpacts_len = ofpacts_len;
2991 xin.resubmit_stats = &stats;
2992 xlate_actions(&xin, &xout);
d445cc16 2993
e79a6c83
EJ
2994 in_port = flow->in_port.ofp_port;
2995 if (in_port == OFPP_NONE) {
2996 in_port = OFPP_LOCAL;
501f8d1f 2997 }
758c456d
JR
2998 execute.actions = xout.odp_actions.data;
2999 execute.actions_len = xout.odp_actions.size;
3000 execute.packet = packet;
3001 execute.md.tunnel = flow->tunnel;
3002 execute.md.skb_priority = flow->skb_priority;
3003 execute.md.pkt_mark = flow->pkt_mark;
b5e7e61a 3004 execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
758c456d
JR
3005 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3006
3007 error = dpif_execute(ofproto->backer->dpif, &execute);
501f8d1f 3008
e79a6c83 3009 xlate_out_uninit(&xout);
9d6ac44e 3010
e79a6c83 3011 return error;
9d6ac44e
BP
3012}
3013
e79a6c83
EJ
3014void
3015rule_dpif_credit_stats(struct rule_dpif *rule,
3016 const struct dpif_flow_stats *stats)
8f73d537 3017{
e79a6c83 3018 ovs_mutex_lock(&rule->stats_mutex);
1a4ec18d
JS
3019 rule->stats.n_packets += stats->n_packets;
3020 rule->stats.n_bytes += stats->n_bytes;
3021 rule->stats.used = MAX(rule->stats.used, stats->used);
e79a6c83 3022 ovs_mutex_unlock(&rule->stats_mutex);
8f73d537
EJ
3023}
3024
e79a6c83
EJ
3025bool
3026rule_dpif_is_fail_open(const struct rule_dpif *rule)
8f73d537 3027{
e79a6c83
EJ
3028 return is_fail_open_rule(&rule->up);
3029}
e1ec7dd4 3030
e79a6c83
EJ
3031bool
3032rule_dpif_is_table_miss(const struct rule_dpif *rule)
3033{
3034 return rule_is_table_miss(&rule->up);
8f73d537
EJ
3035}
3036
32260212
SH
3037bool
3038rule_dpif_is_internal(const struct rule_dpif *rule)
3039{
3040 return rule_is_internal(&rule->up);
3041}
3042
e79a6c83
EJ
3043ovs_be64
3044rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3045 OVS_REQUIRES(rule->up.mutex)
3046{
3047 return rule->up.flow_cookie;
3048}
cfd5c9eb 3049
e79a6c83
EJ
3050void
3051rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3052 uint16_t hard_timeout)
3053{
3054 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
6a7e895f
BP
3055}
3056
e79a6c83
EJ
3057/* Returns 'rule''s actions. The caller owns a reference on the returned
3058 * actions and must eventually release it (with rule_actions_unref()) to avoid
3059 * a memory leak. */
3060struct rule_actions *
3061rule_dpif_get_actions(const struct rule_dpif *rule)
6ff686f2 3062{
e79a6c83
EJ
3063 return rule_get_actions(&rule->up);
3064}
abe529af 3065
6d328fa2
SH
3066/* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3067 * If 'wc' is non-null, sets the fields that were relevant as part of
3068 * the lookup. Returns the table_id where a match or miss occurred.
3069 *
3070 * The return value will be zero unless there was a miss and
3071 * OFPTC_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3072 * where misses occur. */
3073uint8_t
bcd2633a 3074rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
ad3efdcb 3075 struct flow_wildcards *wc, struct rule_dpif **rule)
c57b2226 3076{
6d328fa2
SH
3077 enum rule_dpif_lookup_verdict verdict;
3078 enum ofputil_port_config config = 0;
3079 uint8_t table_id = 0;
c57b2226 3080
6d328fa2
SH
3081 verdict = rule_dpif_lookup_from_table(ofproto, flow, wc, true,
3082 &table_id, rule);
3083
3084 switch (verdict) {
3085 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
3086 return table_id;
3087 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER: {
3088 struct ofport_dpif *port;
3089
3090 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3091 if (!port) {
3092 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3093 flow->in_port.ofp_port);
3094 }
3095 config = port ? port->up.pp.config : 0;
3096 break;
c57b2226 3097 }
6d328fa2
SH
3098 case RULE_DPIF_LOOKUP_VERDICT_DROP:
3099 config = OFPUTIL_PC_NO_PACKET_IN;
3100 break;
3101 default:
3102 OVS_NOT_REACHED();
ec89fc6f 3103 }
c57b2226 3104
6d328fa2 3105 choose_miss_rule(config, ofproto->miss_rule,
70742c7f 3106 ofproto->no_packet_in_rule, rule);
6d328fa2 3107 return table_id;
c57b2226
BP
3108}
3109
6d328fa2
SH
3110static struct rule_dpif *
3111rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3112 const struct flow *flow, struct flow_wildcards *wc)
abe529af 3113{
6d328fa2 3114 struct classifier *cls = &ofproto->up.tables[table_id].cls;
49a0e0eb 3115 const struct cls_rule *cls_rule;
6d328fa2 3116 struct rule_dpif *rule;
9cdaaebe 3117
6d328fa2
SH
3118 fat_rwlock_rdlock(&cls->rwlock);
3119 if (ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3120 if (wc) {
3121 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3122 if (is_ip_any(flow)) {
3123 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3124 }
0934ebba 3125 }
1dd35f8a 3126
6d328fa2
SH
3127 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3128 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3129 /* We must pretend that transport ports are unavailable. */
3130 struct flow ofpc_normal_flow = *flow;
3131 ofpc_normal_flow.tp_src = htons(0);
3132 ofpc_normal_flow.tp_dst = htons(0);
3133 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3134 } else {
3135 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM). */
3136 cls_rule = &ofproto->drop_frags_rule->up.cr;
3137 }
3138 } else {
3139 cls_rule = classifier_lookup(cls, flow, wc);
3140 }
7257b535 3141 } else {
bcd2633a 3142 cls_rule = classifier_lookup(cls, flow, wc);
7257b535 3143 }
ad3efdcb 3144
6d328fa2
SH
3145 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3146 rule_dpif_ref(rule);
06f81620 3147 fat_rwlock_unlock(&cls->rwlock);
ad3efdcb 3148
6d328fa2
SH
3149 return rule;
3150}
3151
3152/* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3153 * Stores the rule that was found in '*rule', or NULL if none was found.
3154 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3155 * lookup.
3156 *
3157 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3158 * if none is found then the table miss configuration for that table is
3159 * honored, which can result in additional lookups in other OpenFlow tables.
3160 * In this case the function updates '*table_id' to reflect the final OpenFlow
3161 * table that was searched.
3162 *
3163 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3164 * '*table_id'.
3165 *
3166 * Returns:
3167 *
3168 * - RULE_DPIF_LOOKUP_VERDICT_MATCH if a rule (in '*rule') was found.
3169 *
3170 * - RULE_DPIF_LOOKUP_VERDICT_DROP if no rule was found and a table miss
3171 * configuration specified that the packet should be dropped in this
3172 * case. (This occurs only if 'honor_table_miss' is true, because only in
3173 * this case does the table miss configuration matter.)
3174 *
3175 * - RULE_DPIF_LOOKUP_VERDICT_CONTROLLER if no rule was found otherwise. */
3176enum rule_dpif_lookup_verdict
3177rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3178 const struct flow *flow,
3179 struct flow_wildcards *wc,
3180 bool honor_table_miss,
3181 uint8_t *table_id, struct rule_dpif **rule)
3182{
3183 uint8_t next_id;
3184
3185 for (next_id = *table_id;
3186 next_id < ofproto->up.n_tables;
3187 next_id++, next_id += (next_id == TBL_INTERNAL))
3188 {
3189 *table_id = next_id;
3190 *rule = rule_dpif_lookup_in_table(ofproto, *table_id, flow, wc);
3191 if (*rule) {
3192 return RULE_DPIF_LOOKUP_VERDICT_MATCH;
3193 } else if (!honor_table_miss) {
3194 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3195 } else {
3196 switch (table_get_config(&ofproto->up, *table_id)
3197 & OFPTC11_TABLE_MISS_MASK) {
3198 case OFPTC11_TABLE_MISS_CONTINUE:
3199 break;
3200
3201 case OFPTC11_TABLE_MISS_CONTROLLER:
3202 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3203
3204 case OFPTC11_TABLE_MISS_DROP:
3205 return RULE_DPIF_LOOKUP_VERDICT_DROP;
3206 }
3207 }
3208 }
3209
3210 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
abe529af
BP
3211}
3212
ec89fc6f
EJ
3213/* Given a port configuration (specified as zero if there's no port), chooses
3214 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3215 * flow table miss. */
70742c7f 3216void
ec89fc6f 3217choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
70742c7f 3218 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
c376f9a3 3219{
70742c7f 3220 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
a2143702
BP
3221 rule_dpif_ref(*rule);
3222}
3223
3224void
3225rule_dpif_ref(struct rule_dpif *rule)
3226{
3227 if (rule) {
3228 ofproto_rule_ref(&rule->up);
3229 }
c376f9a3
IY
3230}
3231
ad3efdcb 3232void
a2143702 3233rule_dpif_unref(struct rule_dpif *rule)
ad3efdcb
EJ
3234{
3235 if (rule) {
a2143702 3236 ofproto_rule_unref(&rule->up);
ad3efdcb
EJ
3237 }
3238}
3239
7ee20df1
BP
3240static void
3241complete_operation(struct rule_dpif *rule)
15aaf599 3242 OVS_REQUIRES(ofproto_mutex)
7ee20df1
BP
3243{
3244 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3245
a1616063 3246 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
35232d32 3247 ofoperation_complete(rule->up.pending, 0);
7ee20df1
BP
3248}
3249
70742c7f
EJ
3250static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3251{
3252 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3253}
3254
abe529af
BP
3255static struct rule *
3256rule_alloc(void)
3257{
3258 struct rule_dpif *rule = xmalloc(sizeof *rule);
3259 return &rule->up;
3260}
3261
3262static void
3263rule_dealloc(struct rule *rule_)
3264{
3265 struct rule_dpif *rule = rule_dpif_cast(rule_);
3266 free(rule);
3267}
3268
90bf1e07 3269static enum ofperr
abe529af 3270rule_construct(struct rule *rule_)
dc437090 3271 OVS_NO_THREAD_SAFETY_ANALYSIS
abe529af
BP
3272{
3273 struct rule_dpif *rule = rule_dpif_cast(rule_);
97ba2d36 3274 ovs_mutex_init_adaptive(&rule->stats_mutex);
1a4ec18d
JS
3275 rule->stats.n_packets = 0;
3276 rule->stats.n_bytes = 0;
3277 rule->stats.used = rule->up.modified;
abe529af
BP
3278 return 0;
3279}
3280
3281static void
8037acb4 3282rule_insert(struct rule *rule_)
15aaf599 3283 OVS_REQUIRES(ofproto_mutex)
abe529af 3284{
9fbe253e
EJ
3285 struct rule_dpif *rule = rule_dpif_cast(rule_);
3286 complete_operation(rule);
8037acb4
BP
3287}
3288
3289static void
3290rule_delete(struct rule *rule_)
15aaf599 3291 OVS_REQUIRES(ofproto_mutex)
8037acb4
BP
3292{
3293 struct rule_dpif *rule = rule_dpif_cast(rule_);
3294 complete_operation(rule);
3295}
3296
3297static void
3298rule_destruct(struct rule *rule_)
3299{
3300 struct rule_dpif *rule = rule_dpif_cast(rule_);
9fbe253e 3301 ovs_mutex_destroy(&rule->stats_mutex);
abe529af
BP
3302}
3303
3304static void
dc437090
JR
3305rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3306 long long int *used)
abe529af
BP
3307{
3308 struct rule_dpif *rule = rule_dpif_cast(rule_);
abe529af 3309
9fbe253e 3310 ovs_mutex_lock(&rule->stats_mutex);
1a4ec18d
JS
3311 *packets = rule->stats.n_packets;
3312 *bytes = rule->stats.n_bytes;
3313 *used = rule->stats.used;
9fbe253e 3314 ovs_mutex_unlock(&rule->stats_mutex);
abe529af
BP
3315}
3316
0a740f48
EJ
3317static void
3318rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3319 struct ofpbuf *packet)
abe529af 3320{
419460f4
AW
3321 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3322
3323 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
0a740f48 3324}
5bf0e941 3325
0a740f48
EJ
3326static enum ofperr
3327rule_execute(struct rule *rule, const struct flow *flow,
3328 struct ofpbuf *packet)
3329{
3330 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3331 ofpbuf_delete(packet);
5bf0e941 3332 return 0;
abe529af
BP
3333}
3334
7ee20df1 3335static void
b277c7cc 3336rule_modify_actions(struct rule *rule_, bool reset_counters)
15aaf599 3337 OVS_REQUIRES(ofproto_mutex)
abe529af
BP
3338{
3339 struct rule_dpif *rule = rule_dpif_cast(rule_);
7ee20df1 3340
b277c7cc
BP
3341 if (reset_counters) {
3342 ovs_mutex_lock(&rule->stats_mutex);
1a4ec18d
JS
3343 rule->stats.n_packets = 0;
3344 rule->stats.n_bytes = 0;
b277c7cc
BP
3345 ovs_mutex_unlock(&rule->stats_mutex);
3346 }
3347
7ee20df1 3348 complete_operation(rule);
abe529af 3349}
00430a3f
SH
3350
3351static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3352{
3353 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3354}
3355
3356static struct ofgroup *
3357group_alloc(void)
3358{
3359 struct group_dpif *group = xzalloc(sizeof *group);
3360 return &group->up;
3361}
3362
3363static void
3364group_dealloc(struct ofgroup *group_)
3365{
3366 struct group_dpif *group = group_dpif_cast(group_);
3367 free(group);
3368}
3369
3370static void
3371group_construct_stats(struct group_dpif *group)
3372 OVS_REQUIRES(group->stats_mutex)
3373{
3374 group->packet_count = 0;
3375 group->byte_count = 0;
3376 if (!group->bucket_stats) {
3377 group->bucket_stats = xcalloc(group->up.n_buckets,
3378 sizeof *group->bucket_stats);
3379 } else {
3380 memset(group->bucket_stats, 0, group->up.n_buckets *
3381 sizeof *group->bucket_stats);
3382 }
3383}
3384
3385static enum ofperr
3386group_construct(struct ofgroup *group_)
3387{
3388 struct group_dpif *group = group_dpif_cast(group_);
5a070238
BP
3389 const struct ofputil_bucket *bucket;
3390
3391 /* Prevent group chaining because our locking structure makes it hard to
3392 * implement deadlock-free. (See xlate_group_resource_check().) */
3393 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3394 const struct ofpact *a;
3395
3396 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
3397 if (a->type == OFPACT_GROUP) {
3398 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
3399 }
3400 }
3401 }
3402
97ba2d36 3403 ovs_mutex_init_adaptive(&group->stats_mutex);
00430a3f
SH
3404 ovs_mutex_lock(&group->stats_mutex);
3405 group_construct_stats(group);
3406 ovs_mutex_unlock(&group->stats_mutex);
3407 return 0;
3408}
3409
3410static void
3411group_destruct__(struct group_dpif *group)
3412 OVS_REQUIRES(group->stats_mutex)
3413{
3414 free(group->bucket_stats);
3415 group->bucket_stats = NULL;
3416}
3417
3418static void
3419group_destruct(struct ofgroup *group_)
3420{
3421 struct group_dpif *group = group_dpif_cast(group_);
3422 ovs_mutex_lock(&group->stats_mutex);
3423 group_destruct__(group);
3424 ovs_mutex_unlock(&group->stats_mutex);
3425 ovs_mutex_destroy(&group->stats_mutex);
3426}
3427
3428static enum ofperr
3429group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3430{
4c9dbc0b 3431 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
00430a3f
SH
3432 struct group_dpif *group = group_dpif_cast(group_);
3433 struct group_dpif *victim = group_dpif_cast(victim_);
3434
3435 ovs_mutex_lock(&group->stats_mutex);
3436 if (victim->up.n_buckets < group->up.n_buckets) {
3437 group_destruct__(group);
3438 }
3439 group_construct_stats(group);
3440 ovs_mutex_unlock(&group->stats_mutex);
3441
4c9dbc0b
BP
3442 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3443
00430a3f
SH
3444 return 0;
3445}
3446
3447static enum ofperr
3448group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3449{
3450 struct group_dpif *group = group_dpif_cast(group_);
3451
00430a3f
SH
3452 ovs_mutex_lock(&group->stats_mutex);
3453 ogs->packet_count = group->packet_count;
3454 ogs->byte_count = group->byte_count;
3455 memcpy(ogs->bucket_stats, group->bucket_stats,
3456 group->up.n_buckets * sizeof *group->bucket_stats);
3457 ovs_mutex_unlock(&group->stats_mutex);
3458
3459 return 0;
3460}
f4fb341b
SH
3461
3462bool
3463group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3464 struct group_dpif **group)
3465 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3466{
3467 struct ofgroup *ofgroup;
3468 bool found;
3469
3470 *group = NULL;
3471 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3472 *group = found ? group_dpif_cast(ofgroup) : NULL;
3473
3474 return found;
3475}
3476
3477void
3478group_dpif_release(struct group_dpif *group)
3479 OVS_RELEASES(group->up.rwlock)
3480{
3481 ofproto_group_release(&group->up);
3482}
3483
3484void
3485group_dpif_get_buckets(const struct group_dpif *group,
3486 const struct list **buckets)
3487{
3488 *buckets = &group->up.buckets;
3489}
3490
3491enum ofp11_group_type
3492group_dpif_get_type(const struct group_dpif *group)
3493{
3494 return group->up.type;
3495}
abe529af 3496\f
97d6520b 3497/* Sends 'packet' out 'ofport'.
52a90c29 3498 * May modify 'packet'.
abe529af 3499 * Returns 0 if successful, otherwise a positive errno value. */
91d6cd12
AW
3500int
3501ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
abe529af 3502{
79e15b78 3503 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
abe529af
BP
3504 int error;
3505
91d6cd12 3506 error = xlate_send_packet(ofport, packet);
79e15b78 3507
0da3c61b 3508 ovs_mutex_lock(&ofproto->stats_mutex);
79e15b78
EJ
3509 ofproto->stats.tx_packets++;
3510 ofproto->stats.tx_bytes += packet->size;
0da3c61b 3511 ovs_mutex_unlock(&ofproto->stats_mutex);
abe529af
BP
3512 return error;
3513}
9583bc14
EJ
3514\f
3515static bool
3516set_frag_handling(struct ofproto *ofproto_,
3517 enum ofp_config_flags frag_handling)
abe529af
BP
3518{
3519 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7257b535 3520 if (frag_handling != OFPC_FRAG_REASM) {
2cc3c58e 3521 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7257b535
BP
3522 return true;
3523 } else {
3524 return false;
3525 }
abe529af
BP
3526}
3527
90bf1e07 3528static enum ofperr
419460f4 3529packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
abe529af 3530 const struct flow *flow,
f25d0cf3 3531 const struct ofpact *ofpacts, size_t ofpacts_len)
abe529af 3532{
419460f4
AW
3533 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3534
3535 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3536 ofpacts_len, packet);
548de4dd 3537 return 0;
abe529af 3538}
6fca1ffb
BP
3539\f
3540/* NetFlow. */
3541
3542static int
3543set_netflow(struct ofproto *ofproto_,
3544 const struct netflow_options *netflow_options)
3545{
3546 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3547
3548 if (netflow_options) {
3549 if (!ofproto->netflow) {
3550 ofproto->netflow = netflow_create();
7bab1576 3551 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6fca1ffb
BP
3552 }
3553 return netflow_set_options(ofproto->netflow, netflow_options);
7bab1576
EJ
3554 } else if (ofproto->netflow) {
3555 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8e407f27 3556 netflow_unref(ofproto->netflow);
6fca1ffb 3557 ofproto->netflow = NULL;
6fca1ffb 3558 }
7bab1576
EJ
3559
3560 return 0;
6fca1ffb 3561}
abe529af
BP
3562
3563static void
3564get_netflow_ids(const struct ofproto *ofproto_,
3565 uint8_t *engine_type, uint8_t *engine_id)
3566{
3567 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3568
acf60855 3569 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
abe529af
BP
3570}
3571\f
3572static struct ofproto_dpif *
3573ofproto_dpif_lookup(const char *name)
3574{
b44a10b7
BP
3575 struct ofproto_dpif *ofproto;
3576
3577 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3578 hash_string(name, 0), &all_ofproto_dpifs) {
3579 if (!strcmp(ofproto->up.name, name)) {
3580 return ofproto;
3581 }
3582 }
3583 return NULL;
abe529af
BP
3584}
3585
f0a3aa2e 3586static void
96e466a3 3587ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
0e15264f 3588 const char *argv[], void *aux OVS_UNUSED)
f0a3aa2e 3589{
490df1ef 3590 struct ofproto_dpif *ofproto;
f0a3aa2e 3591
96e466a3
EJ
3592 if (argc > 1) {
3593 ofproto = ofproto_dpif_lookup(argv[1]);
3594 if (!ofproto) {
bde9f75d 3595 unixctl_command_reply_error(conn, "no such bridge");
96e466a3
EJ
3596 return;
3597 }
509c0149 3598 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 3599 mac_learning_flush(ofproto->ml);
509c0149 3600 ovs_rwlock_unlock(&ofproto->ml->rwlock);
96e466a3
EJ
3601 } else {
3602 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
509c0149 3603 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 3604 mac_learning_flush(ofproto->ml);
509c0149 3605 ovs_rwlock_unlock(&ofproto->ml->rwlock);
96e466a3 3606 }
f0a3aa2e 3607 }
f0a3aa2e 3608
bde9f75d 3609 unixctl_command_reply(conn, "table successfully flushed");
f0a3aa2e
AA
3610}
3611
46c88433
EJ
3612static struct ofport_dpif *
3613ofbundle_get_a_port(const struct ofbundle *bundle)
3614{
3615 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3616 bundle_node);
3617}
3618
abe529af 3619static void
0e15264f
BP
3620ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3621 const char *argv[], void *aux OVS_UNUSED)
abe529af
BP
3622{
3623 struct ds ds = DS_EMPTY_INITIALIZER;
3624 const struct ofproto_dpif *ofproto;
3625 const struct mac_entry *e;
3626
0e15264f 3627 ofproto = ofproto_dpif_lookup(argv[1]);
abe529af 3628 if (!ofproto) {
bde9f75d 3629 unixctl_command_reply_error(conn, "no such bridge");
abe529af
BP
3630 return;
3631 }
3632
3633 ds_put_cstr(&ds, " port VLAN MAC Age\n");
509c0149 3634 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
abe529af
BP
3635 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3636 struct ofbundle *bundle = e->port.p;
ad28062f
BP
3637 char name[OFP_MAX_PORT_NAME_LEN];
3638
3639 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3640 name, sizeof name);
3641 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3642 name, e->vlan, ETH_ADDR_ARGS(e->mac),
e764773c 3643 mac_entry_age(ofproto->ml, e));
abe529af 3644 }
509c0149 3645 ovs_rwlock_unlock(&ofproto->ml->rwlock);
bde9f75d 3646 unixctl_command_reply(conn, ds_cstr(&ds));
abe529af
BP
3647 ds_destroy(&ds);
3648}
3649
6a6455e5 3650struct trace_ctx {
bbafd73b
EJ
3651 struct xlate_out xout;
3652 struct xlate_in xin;
abe529af 3653 struct flow flow;
41a91a0b 3654 struct flow_wildcards wc;
abe529af
BP
3655 struct ds *result;
3656};
3657
3658static void
4d0acc70 3659trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
abe529af 3660{
15aaf599
BP
3661 struct rule_actions *actions;
3662 ovs_be64 cookie;
3663
abe529af
BP
3664 ds_put_char_multiple(result, '\t', level);
3665 if (!rule) {
3666 ds_put_cstr(result, "No match\n");
3667 return;
3668 }
3669
15aaf599
BP
3670 ovs_mutex_lock(&rule->up.mutex);
3671 cookie = rule->up.flow_cookie;
3672 ovs_mutex_unlock(&rule->up.mutex);
3673
29901626 3674 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
15aaf599 3675 rule ? rule->up.table_id : 0, ntohll(cookie));
79feb7df 3676 cls_rule_format(&rule->up.cr, result);
abe529af
BP
3677 ds_put_char(result, '\n');
3678
15aaf599
BP
3679 actions = rule_dpif_get_actions(rule);
3680
abe529af 3681 ds_put_char_multiple(result, '\t', level);
455ecd77 3682 ds_put_cstr(result, "OpenFlow actions=");
15aaf599 3683 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
abe529af
BP
3684 ds_put_char(result, '\n');
3685}
3686
3687static void
3688trace_format_flow(struct ds *result, int level, const char *title,
bbafd73b 3689 struct trace_ctx *trace)
abe529af
BP
3690{
3691 ds_put_char_multiple(result, '\t', level);
3692 ds_put_format(result, "%s: ", title);
bbafd73b 3693 if (flow_equal(&trace->xin.flow, &trace->flow)) {
abe529af
BP
3694 ds_put_cstr(result, "unchanged");
3695 } else {
bbafd73b
EJ
3696 flow_format(result, &trace->xin.flow);
3697 trace->flow = trace->xin.flow;
abe529af
BP
3698 }
3699 ds_put_char(result, '\n');
3700}
3701
eb9e1c26
EJ
3702static void
3703trace_format_regs(struct ds *result, int level, const char *title,
6a6455e5 3704 struct trace_ctx *trace)
eb9e1c26
EJ
3705{
3706 size_t i;
3707
3708 ds_put_char_multiple(result, '\t', level);
3709 ds_put_format(result, "%s:", title);
3710 for (i = 0; i < FLOW_N_REGS; i++) {
34582733 3711 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
eb9e1c26
EJ
3712 }
3713 ds_put_char(result, '\n');
3714}
3715
1ed8d352
EJ
3716static void
3717trace_format_odp(struct ds *result, int level, const char *title,
6a6455e5 3718 struct trace_ctx *trace)
1ed8d352 3719{
bbafd73b 3720 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
1ed8d352
EJ
3721
3722 ds_put_char_multiple(result, '\t', level);
3723 ds_put_format(result, "%s: ", title);
3724 format_odp_actions(result, odp_actions->data, odp_actions->size);
3725 ds_put_char(result, '\n');
3726}
3727
41a91a0b
AW
3728static void
3729trace_format_megaflow(struct ds *result, int level, const char *title,
3730 struct trace_ctx *trace)
3731{
3732 struct match match;
3733
3734 ds_put_char_multiple(result, '\t', level);
3735 ds_put_format(result, "%s: ", title);
3736 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3737 match_init(&match, &trace->flow, &trace->wc);
3738 match_format(&match, result, OFP_DEFAULT_PRIORITY);
3739 ds_put_char(result, '\n');
3740}
3741
abe529af 3742static void
4d0acc70 3743trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
abe529af 3744{
4d0acc70 3745 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
abe529af
BP
3746 struct ds *result = trace->result;
3747
3748 ds_put_char(result, '\n');
4d0acc70
EJ
3749 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3750 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3751 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
41a91a0b 3752 trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
4d0acc70 3753 trace_format_rule(result, recurse + 1, rule);
abe529af
BP
3754}
3755
479df176 3756static void
4d0acc70 3757trace_report(struct xlate_in *xin, const char *s, int recurse)
479df176 3758{
4d0acc70 3759 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
479df176
BP
3760 struct ds *result = trace->result;
3761
4d0acc70 3762 ds_put_char_multiple(result, '\t', recurse);
479df176
BP
3763 ds_put_cstr(result, s);
3764 ds_put_char(result, '\n');
3765}
3766
b6f00895
BP
3767/* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3768 * forms are supported:
3769 *
3770 * - [dpname] odp_flow [-generate | packet]
3771 * - bridge br_flow [-generate | packet]
3772 *
3773 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
316078c7
JR
3774 * returns a nonnull malloced error message. */
3775static char * WARN_UNUSED_RESULT
b6f00895
BP
3776parse_flow_and_packet(int argc, const char *argv[],
3777 struct ofproto_dpif **ofprotop, struct flow *flow,
3778 struct ofpbuf **packetp)
abe529af 3779{
0a37839c 3780 const struct dpif_backer *backer = NULL;
b6f00895 3781 const char *error = NULL;
316078c7 3782 char *m_err = NULL;
b6f00895 3783 struct simap port_names = SIMAP_INITIALIZER(&port_names);
876b0e1c 3784 struct ofpbuf *packet;
b6f00895
BP
3785 struct ofpbuf odp_key;
3786 struct ofpbuf odp_mask;
abe529af 3787
50aa28fd 3788 ofpbuf_init(&odp_key, 0);
ea5e3887 3789 ofpbuf_init(&odp_mask, 0);
abe529af 3790
50aa28fd
AW
3791 /* Handle "-generate" or a hex string as the last argument. */
3792 if (!strcmp(argv[argc - 1], "-generate")) {
3793 packet = ofpbuf_new(0);
3794 argc--;
3795 } else {
b6f00895 3796 error = eth_from_hex(argv[argc - 1], &packet);
50aa28fd
AW
3797 if (!error) {
3798 argc--;
3799 } else if (argc == 4) {
3800 /* The 3-argument form must end in "-generate' or a hex string. */
50aa28fd
AW
3801 goto exit;
3802 }
316078c7 3803 error = NULL;
e84173dc 3804 }
876b0e1c 3805
0a37839c
GS
3806 /* odp_flow can have its in_port specified as a name instead of port no.
3807 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3808 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3809 * we need to create a simap of name to port no. */
3810 if (argc == 3) {
3811 const char *dp_type;
3812 if (!strncmp(argv[1], "ovs-", 4)) {
3813 dp_type = argv[1] + 4;
3814 } else {
3815 dp_type = argv[1];
3816 }
3817 backer = shash_find_data(&all_dpif_backers, dp_type);
b6f00895 3818 } else if (argc == 2) {
0a37839c
GS
3819 struct shash_node *node;
3820 if (shash_count(&all_dpif_backers) == 1) {
3821 node = shash_first(&all_dpif_backers);
3822 backer = node->data;
3823 }
b6f00895
BP
3824 } else {
3825 error = "Syntax error";
3826 goto exit;
0a37839c
GS
3827 }
3828 if (backer && backer->dpif) {
3829 struct dpif_port dpif_port;
3830 struct dpif_port_dump port_dump;
3831 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3832 simap_put(&port_names, dpif_port.name,
3833 odp_to_u32(dpif_port.port_no));
3834 }
3835 }
3836
50aa28fd
AW
3837 /* Parse the flow and determine whether a datapath or
3838 * bridge is specified. If function odp_flow_key_from_string()
3839 * returns 0, the flow is a odp_flow. If function
316078c7 3840 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
b6f00895
BP
3841 if (!odp_flow_from_string(argv[argc - 1], &port_names,
3842 &odp_key, &odp_mask)) {
0a37839c 3843 if (!backer) {
b6f00895 3844 error = "Cannot find the datapath";
0a37839c 3845 goto exit;
876b0e1c 3846 }
8b3b8dd1 3847
b6f00895 3848 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
836fbda7 3849 ofprotop, NULL, NULL, NULL, NULL)) {
b6f00895 3850 error = "Invalid datapath flow";
50aa28fd 3851 goto exit;
8b3b8dd1 3852 }
316078c7
JR
3853 } else {
3854 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
876b0e1c 3855
316078c7
JR
3856 if (err) {
3857 m_err = xasprintf("Bad flow syntax: %s", err);
3858 free(err);
50aa28fd 3859 goto exit;
316078c7
JR
3860 } else {
3861 if (argc != 3) {
3862 error = "Must specify bridge name";
3863 goto exit;
3864 }
3865
3866 *ofprotop = ofproto_dpif_lookup(argv[1]);
3867 if (!*ofprotop) {
3868 error = "Unknown bridge name";
3869 goto exit;
3870 }
50aa28fd 3871 }
abe529af
BP
3872 }
3873
50aa28fd
AW
3874 /* Generate a packet, if requested. */
3875 if (packet) {
3876 if (!packet->size) {
b6f00895 3877 flow_compose(packet, flow);
50aa28fd 3878 } else {
03fbdf8d 3879 struct pkt_metadata md = PKT_METADATA_INITIALIZER_FLOW(flow);
50aa28fd
AW
3880
3881 /* Use the metadata from the flow and the packet argument
3882 * to reconstruct the flow. */
b5e7e61a 3883 flow_extract(packet, &md, flow);
50aa28fd
AW
3884 }
3885 }
3886
6a6455e5 3887exit:
316078c7
JR
3888 if (error && !m_err) {
3889 m_err = xstrdup(error);
3890 }
3891 if (m_err) {
b6f00895
BP
3892 ofpbuf_delete(packet);
3893 packet = NULL;
3894 }
3895 *packetp = packet;
6a6455e5 3896 ofpbuf_uninit(&odp_key);
ea5e3887 3897 ofpbuf_uninit(&odp_mask);
0a37839c 3898 simap_destroy(&port_names);
316078c7 3899 return m_err;
b6f00895
BP
3900}
3901
3902static void
3903ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3904 void *aux OVS_UNUSED)
3905{
3906 struct ofproto_dpif *ofproto;
3907 struct ofpbuf *packet;
316078c7 3908 char *error;
b6f00895
BP
3909 struct flow flow;
3910
3911 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3912 if (!error) {
3913 struct ds result;
3914
3915 ds_init(&result);
aee0979b 3916 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
b6f00895
BP
3917 unixctl_command_reply(conn, ds_cstr(&result));
3918 ds_destroy(&result);
3919 ofpbuf_delete(packet);
3920 } else {
3921 unixctl_command_reply_error(conn, error);
316078c7 3922 free(error);
b6f00895 3923 }
6a6455e5
EJ
3924}
3925
aee0979b
BP
3926static void
3927ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3928 const char *argv[], void *aux OVS_UNUSED)
3929{
3930 enum ofputil_protocol usable_protocols;
3931 struct ofproto_dpif *ofproto;
3932 bool enforce_consistency;
3933 struct ofpbuf ofpacts;
3934 struct ofpbuf *packet;
3935 struct ds result;
3936 struct flow flow;
3937 uint16_t in_port;
3938
3939 /* Three kinds of error return values! */
3940 enum ofperr retval;
316078c7 3941 char *error;
aee0979b
BP
3942
3943 packet = NULL;
3944 ds_init(&result);
3945 ofpbuf_init(&ofpacts, 0);
3946
3947 /* Parse actions. */
316078c7
JR
3948 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3949 if (error) {
3950 unixctl_command_reply_error(conn, error);
3951 free(error);
aee0979b
BP
3952 goto exit;
3953 }
3954
3955 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
ba2fe8e9
BP
3956 * consistency between the flow and the actions. With -consistent, we
3957 * enforce consistency even for a flow supported in OpenFlow 1.0. */
aee0979b
BP
3958 if (!strcmp(argv[1], "-consistent")) {
3959 enforce_consistency = true;
3960 argv++;
3961 argc--;
ba2fe8e9
BP
3962 } else {
3963 enforce_consistency = false;
aee0979b
BP
3964 }
3965
3966 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3967 if (error) {
3968 unixctl_command_reply_error(conn, error);
316078c7 3969 free(error);
aee0979b
BP
3970 goto exit;
3971 }
3972
3973 /* Do the same checks as handle_packet_out() in ofproto.c.
3974 *
3975 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3976 * strictly correct because these actions aren't in any table, but it's OK
3977 * because it 'table_id' is used only to check goto_table instructions, but
3978 * packet-outs take a list of actions and therefore it can't include
3979 * instructions.
3980 *
3981 * We skip the "meter" check here because meter is an instruction, not an
3982 * action, and thus cannot appear in ofpacts. */
3983 in_port = ofp_to_u16(flow.in_port.ofp_port);
3984 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3985 unixctl_command_reply_error(conn, "invalid in_port");
3986 goto exit;
3987 }
ba2fe8e9
BP
3988 if (enforce_consistency) {
3989 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3990 u16_to_ofp(ofproto->up.max_ports),
3991 0, 0, usable_protocols);
3992 } else {
3993 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3994 u16_to_ofp(ofproto->up.max_ports), 0, 0,
3995 &usable_protocols);
3996 }
3997
aee0979b
BP
3998 if (retval) {
3999 ds_clear(&result);
4000 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4001 unixctl_command_reply_error(conn, ds_cstr(&result));
4002 goto exit;
4003 }
4004
4005 ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
4006 unixctl_command_reply(conn, ds_cstr(&result));
4007
4008exit:
4009 ds_destroy(&result);
4010 ofpbuf_delete(packet);
4011 ofpbuf_uninit(&ofpacts);
4012}
4013
4014/* Implements a "trace" through 'ofproto''s flow table, appending a textual
4015 * description of the results to 'ds'.
4016 *
4017 * The trace follows a packet with the specified 'flow' through the flow
4018 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4019 * side effects (if it is nonnull then its flow must be 'flow').
4020 *
4021 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4022 * trace, otherwise the actions are determined by a flow table lookup. */
ade6ad9c 4023static void
6a6455e5 4024ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
aee0979b
BP
4025 const struct ofpbuf *packet,
4026 const struct ofpact ofpacts[], size_t ofpacts_len,
4027 struct ds *ds)
6a6455e5
EJ
4028{
4029 struct rule_dpif *rule;
41a91a0b 4030 struct trace_ctx trace;
6a6455e5 4031
aee0979b 4032 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
6a6455e5
EJ
4033 ds_put_cstr(ds, "Flow: ");
4034 flow_format(ds, flow);
4035 ds_put_char(ds, '\n');
abe529af 4036
41a91a0b 4037 flow_wildcards_init_catchall(&trace.wc);
aee0979b
BP
4038 if (ofpacts) {
4039 rule = NULL;
4040 } else {
41a91a0b 4041 rule_dpif_lookup(ofproto, flow, &trace.wc, &rule);
aee0979b
BP
4042
4043 trace_format_rule(ds, 0, rule);
4044 if (rule == ofproto->miss_rule) {
4045 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
4046 } else if (rule == ofproto->no_packet_in_rule) {
4047 ds_put_cstr(ds, "\nNo match, packets dropped because "
4048 "OFPPC_NO_PACKET_IN is set on in_port.\n");
4049 } else if (rule == ofproto->drop_frags_rule) {
4050 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
4051 "and the fragment handling mode is \"drop\".\n");
4052 }
c57b2226
BP
4053 }
4054
aee0979b 4055 if (rule || ofpacts) {
6a6455e5
EJ
4056 trace.result = ds;
4057 trace.flow = *flow;
e0eecb1c
JR
4058 xlate_in_init(&trace.xin, ofproto, flow, rule, ntohs(flow->tcp_flags),
4059 packet);
aee0979b
BP
4060 if (ofpacts) {
4061 trace.xin.ofpacts = ofpacts;
4062 trace.xin.ofpacts_len = ofpacts_len;
4063 }
bbafd73b
EJ
4064 trace.xin.resubmit_hook = trace_resubmit;
4065 trace.xin.report_hook = trace_report;
bcd2633a 4066
bbafd73b 4067 xlate_actions(&trace.xin, &trace.xout);
abe529af 4068
6a6455e5
EJ
4069 ds_put_char(ds, '\n');
4070 trace_format_flow(ds, 0, "Final flow", &trace);
41a91a0b 4071 trace_format_megaflow(ds, 0, "Megaflow", &trace);
bcd2633a 4072
6a6455e5 4073 ds_put_cstr(ds, "Datapath actions: ");
bbafd73b
EJ
4074 format_odp_actions(ds, trace.xout.odp_actions.data,
4075 trace.xout.odp_actions.size);
876b0e1c 4076
bbafd73b 4077 if (trace.xout.slow) {
04594cd5
BP
4078 enum slow_path_reason slow;
4079
6a7e895f
BP
4080 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4081 "slow path because it:");
04594cd5
BP
4082
4083 slow = trace.xout.slow;
4084 while (slow) {
4085 enum slow_path_reason bit = rightmost_1bit(slow);
4086
4087 ds_put_format(ds, "\n\t- %s.",
4088 slow_path_reason_to_explanation(bit));
4089
4090 slow &= ~bit;
6a7e895f 4091 }
876b0e1c 4092 }
bbafd73b
EJ
4093
4094 xlate_out_uninit(&trace.xout);
abe529af 4095 }
ad3efdcb 4096
a2143702 4097 rule_dpif_unref(rule);
abe529af
BP
4098}
4099
27022416
JP
4100/* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4101 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4102 * to destroy 'ofproto_shash' and free the returned value. */
4103static const struct shash_node **
4104get_ofprotos(struct shash *ofproto_shash)
4105{
4106 const struct ofproto_dpif *ofproto;
4107
4108 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4109 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4110 shash_add_nocopy(ofproto_shash, name, ofproto);
4111 }
4112
4113 return shash_sort(ofproto_shash);
4114}
4115
4116static void
4117ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4118 const char *argv[] OVS_UNUSED,
4119 void *aux OVS_UNUSED)
4120{
4121 struct ds ds = DS_EMPTY_INITIALIZER;
4122 struct shash ofproto_shash;
4123 const struct shash_node **sorted_ofprotos;
4124 int i;
4125
4126 shash_init(&ofproto_shash);
4127 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4128 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4129 const struct shash_node *node = sorted_ofprotos[i];
4130 ds_put_format(&ds, "%s\n", node->name);
4131 }
4132
4133 shash_destroy(&ofproto_shash);
4134 free(sorted_ofprotos);
4135
4136 unixctl_command_reply(conn, ds_cstr(&ds));
4137 ds_destroy(&ds);
4138}
4139
dc54ef36
EJ
4140static void
4141dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
27022416 4142{
dc54ef36 4143 const struct shash_node **ofprotos;
e79a6c83 4144 struct dpif_dp_stats dp_stats;
dc54ef36 4145 struct shash ofproto_shash;
09672174 4146 size_t i;
655ab909 4147
e79a6c83 4148 dpif_get_dp_stats(backer->dpif, &dp_stats);
655ab909 4149
dc54ef36 4150 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
e79a6c83 4151 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
dc54ef36 4152
dc54ef36
EJ
4153 shash_init(&ofproto_shash);
4154 ofprotos = get_ofprotos(&ofproto_shash);
4155 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4156 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4157 const struct shash_node **ports;
4158 size_t j;
0a740f48 4159
dc54ef36
EJ
4160 if (ofproto->backer != backer) {
4161 continue;
0a740f48 4162 }
27022416 4163
e79a6c83 4164 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
dc54ef36
EJ
4165
4166 ports = shash_sort(&ofproto->up.port_by_name);
4167 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4168 const struct shash_node *node = ports[j];
4169 struct ofport *ofport = node->data;
4170 struct smap config;
4e022ec0 4171 odp_port_t odp_port;
27022416 4172
dc54ef36
EJ
4173 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4174 ofport->ofp_port);
27022416 4175
dc54ef36 4176 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4e022ec0 4177 if (odp_port != ODPP_NONE) {
dc54ef36
EJ
4178 ds_put_format(ds, "%"PRIu32":", odp_port);
4179 } else {
4180 ds_put_cstr(ds, "none:");
4181 }
27022416 4182
dc54ef36 4183 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
27022416 4184
dc54ef36
EJ
4185 smap_init(&config);
4186 if (!netdev_get_config(ofport->netdev, &config)) {
4187 const struct smap_node **nodes;
4188 size_t i;
27022416 4189
dc54ef36
EJ
4190 nodes = smap_sort(&config);
4191 for (i = 0; i < smap_count(&config); i++) {
4192 const struct smap_node *node = nodes[i];
4193 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4194 node->key, node->value);
4195 }
4196 free(nodes);
27022416 4197 }
dc54ef36
EJ
4198 smap_destroy(&config);
4199
27022416 4200 ds_put_char(ds, ')');
dc54ef36 4201 ds_put_char(ds, '\n');
27022416 4202 }
dc54ef36 4203 free(ports);
27022416 4204 }
dc54ef36
EJ
4205 shash_destroy(&ofproto_shash);
4206 free(ofprotos);
27022416
JP
4207}
4208
4209static void
dc54ef36
EJ
4210ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4211 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
27022416
JP
4212{
4213 struct ds ds = DS_EMPTY_INITIALIZER;
dc54ef36
EJ
4214 const struct shash_node **backers;
4215 int i;
27022416 4216
dc54ef36
EJ
4217 backers = shash_sort(&all_dpif_backers);
4218 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4219 dpif_show_backer(backers[i]->data, &ds);
27022416 4220 }
dc54ef36 4221 free(backers);
27022416
JP
4222
4223 unixctl_command_reply(conn, ds_cstr(&ds));
4224 ds_destroy(&ds);
4225}
4226
1b849273
JS
4227static bool
4228ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4229 const struct nlattr *key, size_t key_len)
4230{
1b849273
JS
4231 struct ofproto_dpif *ofp;
4232 struct flow flow;
4233
836fbda7 4234 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
1b849273
JS
4235 NULL, NULL, NULL, NULL);
4236 return ofp == ofproto;
4237}
4238
27022416
JP
4239static void
4240ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4241 int argc OVS_UNUSED, const char *argv[],
4242 void *aux OVS_UNUSED)
4243{
4244 struct ds ds = DS_EMPTY_INITIALIZER;
1b849273 4245 const struct dpif_flow_stats *stats;
27022416 4246 const struct ofproto_dpif *ofproto;
1b849273
JS
4247 struct dpif_flow_dump flow_dump;
4248 const struct nlattr *actions;
4249 const struct nlattr *mask;
4250 const struct nlattr *key;
4251 size_t actions_len;
4252 size_t mask_len;
4253 size_t key_len;
04b541df
GS
4254 bool verbosity = false;
4255 struct dpif_port dpif_port;
4256 struct dpif_port_dump port_dump;
4257 struct hmap portno_names;
d2ad7ef1 4258 void *state = NULL;
938eaa50 4259 int error;
27022416 4260
04b541df 4261 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
27022416
JP
4262 if (!ofproto) {
4263 unixctl_command_reply_error(conn, "no such bridge");
4264 return;
4265 }
4266
04b541df
GS
4267 if (argc > 2 && !strcmp(argv[1], "-m")) {
4268 verbosity = true;
4269 }
4270
4271 hmap_init(&portno_names);
4272 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4273 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4274 }
4275
1b849273 4276 ds_init(&ds);
938eaa50
JS
4277 error = dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4278 if (error) {
4279 goto exit;
4280 }
d2ad7ef1
JS
4281 dpif_flow_dump_state_init(ofproto->backer->dpif, &state);
4282 while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
4283 &mask, &mask_len, &actions, &actions_len,
4284 &stats)) {
1b849273 4285 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
04d08d54
EJ
4286 continue;
4287 }
4288
04b541df
GS
4289 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4290 verbosity);
1b849273
JS
4291 ds_put_cstr(&ds, ", ");
4292 dpif_flow_stats_format(stats, &ds);
27022416 4293 ds_put_cstr(&ds, ", actions:");
1b849273 4294 format_odp_actions(&ds, actions, actions_len);
27022416
JP
4295 ds_put_char(&ds, '\n');
4296 }
d2ad7ef1 4297 dpif_flow_dump_state_uninit(ofproto->backer->dpif, state);
938eaa50 4298 error = dpif_flow_dump_done(&flow_dump);
27022416 4299
938eaa50
JS
4300exit:
4301 if (error) {
1b849273
JS
4302 ds_clear(&ds);
4303 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4304 unixctl_command_reply_error(conn, ds_cstr(&ds));
4305 } else {
4306 unixctl_command_reply(conn, ds_cstr(&ds));
4307 }
04b541df
GS
4308 odp_portno_names_destroy(&portno_names);
4309 hmap_destroy(&portno_names);
27022416
JP
4310 ds_destroy(&ds);
4311}
4312
abe529af
BP
4313static void
4314ofproto_dpif_unixctl_init(void)
4315{
4316 static bool registered;
4317 if (registered) {
4318 return;
4319 }
4320 registered = true;
4321
0e15264f
BP
4322 unixctl_command_register(
4323 "ofproto/trace",
dc8ce81f 4324 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
50aa28fd 4325 1, 3, ofproto_unixctl_trace, NULL);
aee0979b
BP
4326 unixctl_command_register(
4327 "ofproto/trace-packet-out",
4328 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4329 2, 6, ofproto_unixctl_trace_actions, NULL);
96e466a3 4330 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
0e15264f
BP
4331 ofproto_unixctl_fdb_flush, NULL);
4332 unixctl_command_register("fdb/show", "bridge", 1, 1,
4333 ofproto_unixctl_fdb_show, NULL);
27022416
JP
4334 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4335 ofproto_unixctl_dpif_dump_dps, NULL);
dc54ef36
EJ
4336 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4337 NULL);
04b541df 4338 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
27022416 4339 ofproto_unixctl_dpif_dump_flows, NULL);
abe529af 4340}
32260212
SH
4341
4342
4343/* Returns true if 'rule' is an internal rule, false otherwise. */
4344bool
4345rule_is_internal(const struct rule *rule)
4346{
4347 return rule->table_id == TBL_INTERNAL;
4348}
abe529af 4349\f
52a90c29
BP
4350/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4351 *
4352 * This is deprecated. It is only for compatibility with broken device drivers
4353 * in old versions of Linux that do not properly support VLANs when VLAN
4354 * devices are not used. When broken device drivers are no longer in
4355 * widespread use, we will delete these interfaces. */
4356
4357static int
4e022ec0 4358set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
52a90c29
BP
4359{
4360 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4361 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4362
4363 if (realdev_ofp_port == ofport->realdev_ofp_port
4364 && vid == ofport->vlandev_vid) {
4365 return 0;
4366 }
4367
2cc3c58e 4368 ofproto->backer->need_revalidate = REV_RECONFIGURE;
52a90c29
BP
4369
4370 if (ofport->realdev_ofp_port) {
4371 vsp_remove(ofport);
4372 }
4373 if (realdev_ofp_port && ofport->bundle) {
4374 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4375 * themselves be part of a bundle. */
4376 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4377 }
4378
4379 ofport->realdev_ofp_port = realdev_ofp_port;
4380 ofport->vlandev_vid = vid;
4381
4382 if (realdev_ofp_port) {
4383 vsp_add(ofport, realdev_ofp_port, vid);
4384 }
4385
4386 return 0;
4387}
4388
4389static uint32_t
4e022ec0 4390hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
52a90c29 4391{
4e022ec0 4392 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
52a90c29
BP
4393}
4394
46c88433
EJ
4395bool
4396ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
13501305 4397 OVS_EXCLUDED(ofproto->vsp_mutex)
46c88433 4398{
7614e5d0
JR
4399 /* hmap_is_empty is thread safe. */
4400 return !hmap_is_empty(&ofproto->realdev_vid_map);
46c88433
EJ
4401}
4402
13501305
EJ
4403static ofp_port_t
4404vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4405 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4406 OVS_REQUIRES(ofproto->vsp_mutex)
52a90c29
BP
4407{
4408 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
52a90c29
BP
4409 int vid = vlan_tci_to_vid(vlan_tci);
4410 const struct vlan_splinter *vsp;
4411
4412 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4413 hash_realdev_vid(realdev_ofp_port, vid),
4414 &ofproto->realdev_vid_map) {
4415 if (vsp->realdev_ofp_port == realdev_ofp_port
4416 && vsp->vid == vid) {
deea1200 4417 return vsp->vlandev_ofp_port;
52a90c29
BP
4418 }
4419 }
4420 }
deea1200 4421 return realdev_ofp_port;
52a90c29
BP
4422}
4423
13501305
EJ
4424/* Returns the OFP port number of the Linux VLAN device that corresponds to
4425 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4426 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4427 * 'vlan_tci' 9, it would return the port number of eth0.9.
4428 *
4429 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4430 * function just returns its 'realdev_ofp_port' argument. */
4431ofp_port_t
4432vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4433 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4434 OVS_EXCLUDED(ofproto->vsp_mutex)
4435{
4436 ofp_port_t ret;
4437
7614e5d0
JR
4438 /* hmap_is_empty is thread safe, see if we can return immediately. */
4439 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4440 return realdev_ofp_port;
4441 }
13501305
EJ
4442 ovs_mutex_lock(&ofproto->vsp_mutex);
4443 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4444 ovs_mutex_unlock(&ofproto->vsp_mutex);
4445 return ret;
4446}
4447
52a90c29 4448static struct vlan_splinter *
4e022ec0 4449vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
52a90c29
BP
4450{
4451 struct vlan_splinter *vsp;
4452
4e022ec0 4453 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
f9c0c3ec 4454 hash_ofp_port(vlandev_ofp_port),
52a90c29
BP
4455 &ofproto->vlandev_map) {
4456 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4457 return vsp;
4458 }
4459 }
4460
4461 return NULL;
4462}
4463
40e05935
BP
4464/* Returns the OpenFlow port number of the "real" device underlying the Linux
4465 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4466 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4467 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4468 * eth0 and store 9 in '*vid'.
4469 *
4470 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4471 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4472 * always does.*/
4e022ec0 4473static ofp_port_t
52a90c29 4474vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4e022ec0 4475 ofp_port_t vlandev_ofp_port, int *vid)
bd3950dd 4476 OVS_REQUIRES(ofproto->vsp_mutex)
52a90c29
BP
4477{
4478 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4479 const struct vlan_splinter *vsp;
4480
4481 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4482 if (vsp) {
4483 if (vid) {
4484 *vid = vsp->vid;
4485 }
4486 return vsp->realdev_ofp_port;
4487 }
4488 }
4489 return 0;
4490}
4491
b98d8985
BP
4492/* Given 'flow', a flow representing a packet received on 'ofproto', checks
4493 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4494 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4495 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4496 * always the case unless VLAN splinters are enabled), returns false without
4497 * making any changes. */
8449c4d6 4498bool
b98d8985 4499vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
13501305 4500 OVS_EXCLUDED(ofproto->vsp_mutex)
b98d8985 4501{
4e022ec0 4502 ofp_port_t realdev;
b98d8985
BP
4503 int vid;
4504
7614e5d0
JR
4505 /* hmap_is_empty is thread safe. */
4506 if (hmap_is_empty(&ofproto->vlandev_map)) {
4507 return false;
4508 }
4509
13501305 4510 ovs_mutex_lock(&ofproto->vsp_mutex);
4e022ec0 4511 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
13501305 4512 ovs_mutex_unlock(&ofproto->vsp_mutex);
b98d8985
BP
4513 if (!realdev) {
4514 return false;
4515 }
4516
4517 /* Cause the flow to be processed as if it came in on the real device with
4518 * the VLAN device's VLAN ID. */
4e022ec0 4519 flow->in_port.ofp_port = realdev;
b98d8985
BP
4520 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4521 return true;
4522}
4523
52a90c29
BP
4524static void
4525vsp_remove(struct ofport_dpif *port)
4526{
4527 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4528 struct vlan_splinter *vsp;
4529
13501305 4530 ovs_mutex_lock(&ofproto->vsp_mutex);
52a90c29
BP
4531 vsp = vlandev_find(ofproto, port->up.ofp_port);
4532 if (vsp) {
4533 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4534 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4535 free(vsp);
4536
4537 port->realdev_ofp_port = 0;
4538 } else {
4539 VLOG_ERR("missing vlan device record");
4540 }
13501305 4541 ovs_mutex_unlock(&ofproto->vsp_mutex);
52a90c29
BP
4542}
4543
4544static void
4e022ec0 4545vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
52a90c29
BP
4546{
4547 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4548
13501305 4549 ovs_mutex_lock(&ofproto->vsp_mutex);
52a90c29 4550 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
13501305 4551 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
52a90c29
BP
4552 == realdev_ofp_port)) {
4553 struct vlan_splinter *vsp;
4554
4555 vsp = xmalloc(sizeof *vsp);
52a90c29
BP
4556 vsp->realdev_ofp_port = realdev_ofp_port;
4557 vsp->vlandev_ofp_port = port->up.ofp_port;
4558 vsp->vid = vid;
4559
4560 port->realdev_ofp_port = realdev_ofp_port;
13501305
EJ
4561
4562 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4563 hash_ofp_port(port->up.ofp_port));
4564 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4565 hash_realdev_vid(realdev_ofp_port, vid));
52a90c29
BP
4566 } else {
4567 VLOG_ERR("duplicate vlan device record");
4568 }
13501305 4569 ovs_mutex_unlock(&ofproto->vsp_mutex);
52a90c29 4570}
e1b1d06a 4571
46c88433 4572static odp_port_t
4e022ec0 4573ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
e1b1d06a
JP
4574{
4575 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4e022ec0 4576 return ofport ? ofport->odp_port : ODPP_NONE;
e1b1d06a
JP
4577}
4578
8449c4d6 4579struct ofport_dpif *
4e022ec0 4580odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
e1b1d06a
JP
4581{
4582 struct ofport_dpif *port;
4583
8449c4d6 4584 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
f9c0c3ec 4585 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
acf60855 4586 &backer->odp_to_ofport_map) {
e1b1d06a 4587 if (port->odp_port == odp_port) {
8449c4d6 4588 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855 4589 return port;
e1b1d06a
JP
4590 }
4591 }
4592
8449c4d6 4593 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855
JP
4594 return NULL;
4595}
4596
4e022ec0
AW
4597static ofp_port_t
4598odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
acf60855
JP
4599{
4600 struct ofport_dpif *port;
4601
4602 port = odp_port_to_ofport(ofproto->backer, odp_port);
6472ba11 4603 if (port && &ofproto->up == port->up.ofproto) {
acf60855
JP
4604 return port->up.ofp_port;
4605 } else {
4606 return OFPP_NONE;
4607 }
e1b1d06a 4608}
655ab909 4609
abe529af 4610const struct ofproto_class ofproto_dpif_class = {
b0408fca 4611 init,
abe529af
BP
4612 enumerate_types,
4613 enumerate_names,
4614 del,
0aeaabc8 4615 port_open_type,
acf60855 4616 type_run,
acf60855 4617 type_wait,
abe529af
BP
4618 alloc,
4619 construct,
4620 destruct,
4621 dealloc,
4622 run,
4623 wait,
e79a6c83 4624 NULL, /* get_memory_usage. */
1c030aa5 4625 type_get_memory_usage,
abe529af 4626 flush,
6c1491fb
BP
4627 get_features,
4628 get_tables,
abe529af
BP
4629 port_alloc,
4630 port_construct,
4631 port_destruct,
4632 port_dealloc,
4633 port_modified,
4634 port_reconfigured,
4635 port_query_by_name,
4636 port_add,
4637 port_del,
6527c598 4638 port_get_stats,
abe529af
BP
4639 port_dump_start,
4640 port_dump_next,
4641 port_dump_done,
4642 port_poll,
4643 port_poll_wait,
4644 port_is_lacp_current,
0ab6decf 4645 NULL, /* rule_choose_table */
abe529af
BP
4646 rule_alloc,
4647 rule_construct,
8037acb4
BP
4648 rule_insert,
4649 rule_delete,
abe529af
BP
4650 rule_destruct,
4651 rule_dealloc,
abe529af
BP
4652 rule_get_stats,
4653 rule_execute,
4654 rule_modify_actions,
7257b535 4655 set_frag_handling,
abe529af
BP
4656 packet_out,
4657 set_netflow,
4658 get_netflow_ids,
4659 set_sflow,
29089a54 4660 set_ipfix,
abe529af 4661 set_cfm,
9a9e3786 4662 get_cfm_status,
ccc09689
EJ
4663 set_bfd,
4664 get_bfd_status,
21f7563c
JP
4665 set_stp,
4666 get_stp_status,
4667 set_stp_port,
4668 get_stp_port_status,
fd28ce3a 4669 get_stp_port_stats,
8b36f51e 4670 set_queues,
abe529af
BP
4671 bundle_set,
4672 bundle_remove,
ec7ceaed
EJ
4673 mirror_set__,
4674 mirror_get_stats__,
abe529af
BP
4675 set_flood_vlans,
4676 is_mirror_output_bundle,
8402c74b 4677 forward_bpdu_changed,
c4069512 4678 set_mac_table_config,
52a90c29 4679 set_realdev,
9cae45dc
JR
4680 NULL, /* meter_get_features */
4681 NULL, /* meter_set */
4682 NULL, /* meter_get */
4683 NULL, /* meter_del */
00430a3f
SH
4684 group_alloc, /* group_alloc */
4685 group_construct, /* group_construct */
4686 group_destruct, /* group_destruct */
4687 group_dealloc, /* group_dealloc */
4688 group_modify, /* group_modify */
4689 group_get_stats, /* group_get_stats */
abe529af 4690};