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