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