]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif.c
ofproto-dpif: Use dp_hash as default selection method
[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;
ce058104 1523 ofpact_finish_CONTROLLER(&ofpacts, &controller);
f25d0cf3 1524
adcf00ba
AZ
1525 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1526 &ofproto->miss_rule);
c57b2226
BP
1527 if (error) {
1528 return error;
1529 }
1530
f25d0cf3 1531 ofpbuf_clear(&ofpacts);
adcf00ba 1532 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
dbb7c28f 1533 &ofproto->no_packet_in_rule);
7fd51d39
BP
1534 if (error) {
1535 return error;
1536 }
1537
adcf00ba 1538 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
dbb7c28f 1539 &ofproto->drop_frags_rule);
adcf00ba
AZ
1540 if (error) {
1541 return error;
1542 }
1543
0c7812e5
AW
1544 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1545 * zero when reaching this rule.
adcf00ba 1546 *
0c7812e5 1547 * (priority=2), recirc_id=0, actions=drop
adcf00ba 1548 */
0c7812e5 1549 ofpbuf_clear(&ofpacts);
adcf00ba
AZ
1550 match_init_catchall(&match);
1551 match_set_recirc_id(&match, 0);
290ad78a 1552 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
adcf00ba 1553 &unused_rulep);
c57b2226
BP
1554 return error;
1555}
1556
abe529af 1557static void
fe13ccdc 1558destruct(struct ofproto *ofproto_, bool del)
abe529af
BP
1559{
1560 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
a2b53dec 1561 struct ofproto_async_msg *am;
78c8df12 1562 struct rule_dpif *rule;
d0918789 1563 struct oftable *table;
a2b53dec 1564 struct ovs_list ams;
abe529af 1565
875b94ed 1566 ofproto->backer->need_revalidate = REV_RECONFIGURE;
84f0f298 1567 xlate_txn_start();
46c88433 1568 xlate_remove_ofproto(ofproto);
84f0f298 1569 xlate_txn_commit();
46c88433 1570
3f142f59
BP
1571 /* Ensure that the upcall processing threads have no remaining references
1572 * to the ofproto or anything in it. */
1573 udpif_synchronize(ofproto->backer->udpif);
1fe409d5 1574
911b4a7e
JP
1575 hmap_remove(&all_ofproto_dpifs_by_name,
1576 &ofproto->all_ofproto_dpifs_by_name_node);
1577 hmap_remove(&all_ofproto_dpifs_by_uuid,
1578 &ofproto->all_ofproto_dpifs_by_uuid_node);
7ee20df1 1579
0697b5c3 1580 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
de4ad4a2 1581 CLS_FOR_EACH (rule, up.cr, &table->cls) {
8b81d1ef 1582 ofproto_rule_delete(&ofproto->up, &rule->up);
0697b5c3 1583 }
7ee20df1 1584 }
daab0ae6 1585 ofproto_group_delete_all(&ofproto->up);
7ee20df1 1586
a2b53dec
BP
1587 guarded_list_pop_all(&ofproto->ams, &ams);
1588 LIST_FOR_EACH_POP (am, list_node, &ams) {
1589 ofproto_async_msg_free(am);
ada3a58d 1590 }
a2b53dec 1591 guarded_list_destroy(&ofproto->ams);
ada3a58d 1592
e672ff9b 1593 recirc_free_ofproto(ofproto, ofproto->up.name);
f9038ef6 1594
ec7ceaed 1595 mbridge_unref(ofproto->mbridge);
abe529af 1596
8e407f27 1597 netflow_unref(ofproto->netflow);
9723bcce 1598 dpif_sflow_unref(ofproto->sflow);
8b7ea2d4 1599 dpif_ipfix_unref(ofproto->ipfix);
abe529af 1600 hmap_destroy(&ofproto->bundles);
5d989517 1601 mac_learning_unref(ofproto->ml);
6d95c4e8 1602 mcast_snooping_unref(ofproto->ms);
7b91e3c8 1603 stp_unref(ofproto->stp);
1604 rstp_unref(ofproto->rstp);
abe529af 1605
acf60855 1606 sset_destroy(&ofproto->ports);
0a740f48 1607 sset_destroy(&ofproto->ghost_ports);
acf60855 1608 sset_destroy(&ofproto->port_poll_set);
e1b1d06a 1609
0da3c61b 1610 ovs_mutex_destroy(&ofproto->stats_mutex);
13501305 1611
a2b53dec 1612 seq_destroy(ofproto->ams_seq);
cfc50ae5 1613
fe13ccdc 1614 close_dpif_backer(ofproto->backer, del);
abe529af
BP
1615}
1616
5fcc0d00
BP
1617static int
1618run(struct ofproto *ofproto_)
1619{
1620 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
e79a6c83 1621 uint64_t new_seq, new_dump_seq;
5fcc0d00 1622
ec7ceaed
EJ
1623 if (mbridge_need_revalidate(ofproto->mbridge)) {
1624 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 1625 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 1626 mac_learning_flush(ofproto->ml);
509c0149 1627 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 1628 mcast_snooping_mdb_flush(ofproto->ms);
ec7ceaed
EJ
1629 }
1630
a2b53dec 1631 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
b53d5c33 1632 * flow restore. Even though nothing is processed during flow restore,
a2b53dec 1633 * all queued 'ams' will be handled immediately when flow restore
b53d5c33 1634 * completes. */
a2b53dec 1635 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
b53d5c33 1636
a6b7506d 1637 /* Do not perform any periodic activity required by 'ofproto' while
40358701 1638 * waiting for flow restore to complete. */
a6b7506d 1639 if (!ofproto_get_flow_restore_wait()) {
a2b53dec
BP
1640 struct ofproto_async_msg *am;
1641 struct ovs_list ams;
1642
1643 guarded_list_pop_all(&ofproto->ams, &ams);
1644 LIST_FOR_EACH_POP (am, list_node, &ams) {
1645 connmgr_send_async_msg(ofproto->up.connmgr, am);
1646 ofproto_async_msg_free(am);
a6b7506d 1647 }
abe529af
BP
1648 }
1649
abe529af 1650 if (ofproto->netflow) {
8bfaca5b 1651 netflow_run(ofproto->netflow);
abe529af
BP
1652 }
1653 if (ofproto->sflow) {
bae473fe 1654 dpif_sflow_run(ofproto->sflow);
abe529af 1655 }
978427a5
RL
1656 if (ofproto->ipfix) {
1657 dpif_ipfix_run(ofproto->ipfix);
1658 }
abe529af 1659
f23d157c
JS
1660 new_seq = seq_read(connectivity_seq_get());
1661 if (ofproto->change_seq != new_seq) {
1662 struct ofport_dpif *ofport;
1663
1664 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1665 port_run(ofport);
1666 }
1667
1668 ofproto->change_seq = new_seq;
abe529af 1669 }
e2d13c43
JS
1670 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1671 struct ofbundle *bundle;
1672
1673 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1674 bundle_run(bundle);
1675 }
abe529af
BP
1676 }
1677
21f7563c 1678 stp_run(ofproto);
9efd308e 1679 rstp_run(ofproto);
509c0149 1680 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594
EJ
1681 if (mac_learning_run(ofproto->ml)) {
1682 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1683 }
509c0149 1684 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af 1685
6d95c4e8
FL
1686 if (mcast_snooping_run(ofproto->ms)) {
1687 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1688 }
1689
e79a6c83
EJ
1690 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1691 if (ofproto->dump_seq != new_dump_seq) {
1692 struct rule *rule, *next_rule;
f5857865 1693 long long now = time_msec();
e79a6c83
EJ
1694
1695 /* We know stats are relatively fresh, so now is a good time to do some
1696 * periodic work. */
1697 ofproto->dump_seq = new_dump_seq;
1698
1699 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1700 * has passed. */
1701 ovs_mutex_lock(&ofproto_mutex);
1702 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1703 &ofproto->up.expirable) {
f5857865 1704 rule_expire(rule_dpif_cast(rule), now);
e79a6c83
EJ
1705 }
1706 ovs_mutex_unlock(&ofproto_mutex);
1707
1708 /* All outstanding data in existing flows has been accounted, so it's a
1709 * good time to do bond rebalancing. */
60cda7d6 1710 if (ofproto->has_bonded_bundles) {
e79a6c83
EJ
1711 struct ofbundle *bundle;
1712
1713 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
60cda7d6
AZ
1714 if (bundle->bond) {
1715 bond_rebalance(bundle->bond);
e79a6c83
EJ
1716 }
1717 }
6814e51f
BP
1718 }
1719 }
abe529af
BP
1720 return 0;
1721}
1722
1723static void
f07c97f5 1724ofproto_dpif_wait(struct ofproto *ofproto_)
abe529af
BP
1725{
1726 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 1727
40358701
GS
1728 if (ofproto_get_flow_restore_wait()) {
1729 return;
1730 }
1731
abe529af 1732 if (ofproto->sflow) {
bae473fe 1733 dpif_sflow_wait(ofproto->sflow);
abe529af 1734 }
978427a5
RL
1735 if (ofproto->ipfix) {
1736 dpif_ipfix_wait(ofproto->ipfix);
1737 }
e2d13c43
JS
1738 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1739 struct ofbundle *bundle;
1740
1741 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1742 bundle_wait(bundle);
1743 }
abe529af 1744 }
6fca1ffb
BP
1745 if (ofproto->netflow) {
1746 netflow_wait(ofproto->netflow);
1747 }
509c0149 1748 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1c313b88 1749 mac_learning_wait(ofproto->ml);
509c0149 1750 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 1751 mcast_snooping_wait(ofproto->ms);
21f7563c 1752 stp_wait(ofproto);
2cc3c58e 1753 if (ofproto->backer->need_revalidate) {
abe529af 1754 poll_immediate_wake();
abe529af 1755 }
abe529af 1756
e79a6c83 1757 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
a2b53dec 1758 seq_wait(ofproto->ams_seq, ofproto->ams_seqno);
0d085684
BP
1759}
1760
1c030aa5
EJ
1761static void
1762type_get_memory_usage(const char *type, struct simap *usage)
1763{
1764 struct dpif_backer *backer;
1765
1766 backer = shash_find_data(&all_dpif_backers, type);
1767 if (backer) {
1768 udpif_get_memory_usage(backer->udpif, usage);
1769 }
1770}
1771
abe529af 1772static void
1b5b5071 1773flush(struct ofproto *ofproto_)
abe529af 1774{
1b5b5071
AZ
1775 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1776 struct dpif_backer *backer = ofproto->backer;
1777
1778 if (backer) {
1779 udpif_flush(backer->udpif);
1780 }
abe529af
BP
1781}
1782
6c1491fb 1783static void
3c1bb396 1784query_tables(struct ofproto *ofproto,
4bc938cc 1785 struct ofputil_table_features *features OVS_UNUSED,
3c1bb396 1786 struct ofputil_table_stats *stats)
6c1491fb 1787{
3c1bb396
BP
1788 if (stats) {
1789 int i;
6c1491fb 1790
3c1bb396
BP
1791 for (i = 0; i < ofproto->n_tables; i++) {
1792 unsigned long missed, matched;
d611866c 1793
afcea9ea
JR
1794 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1795 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1796
3c1bb396 1797 stats[i].matched_count = matched;
3c1bb396
BP
1798 stats[i].lookup_count = matched + missed;
1799 }
d611866c 1800 }
6c1491fb
BP
1801}
1802
621b8064 1803static void
44e0c35d 1804set_tables_version(struct ofproto *ofproto_, ovs_version_t version)
621b8064
JR
1805{
1806 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1807
f9cf9e57
JR
1808 /* Use memory_order_release to signify that any prior memory accesses can
1809 * not be reordered to happen after this atomic store. This makes sure the
1810 * new version is properly set up when the readers can read this 'version'
1811 * value. */
1812 atomic_store_explicit(&ofproto->tables_version, version,
1813 memory_order_release);
1814 /* 'need_revalidate' can be reordered to happen before the atomic_store
1815 * above, but it does not matter as this variable is not accessed by other
1816 * threads. */
1fc71871 1817 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
621b8064
JR
1818}
1819
abe529af
BP
1820static struct ofport *
1821port_alloc(void)
1822{
3da29e32 1823 struct ofport_dpif *port = xzalloc(sizeof *port);
abe529af
BP
1824 return &port->up;
1825}
1826
1827static void
1828port_dealloc(struct ofport *port_)
1829{
1830 struct ofport_dpif *port = ofport_dpif_cast(port_);
1831 free(port);
1832}
1833
1834static int
1835port_construct(struct ofport *port_)
1836{
1837 struct ofport_dpif *port = ofport_dpif_cast(port_);
1838 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
b9ad7294 1839 const struct netdev *netdev = port->up.netdev;
3aa30359 1840 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
6d9e94b5 1841 const char *dp_port_name;
e1b1d06a
JP
1842 struct dpif_port dpif_port;
1843 int error;
abe529af 1844
2cc3c58e 1845 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1846 port->bundle = NULL;
1847 port->cfm = NULL;
ccc09689 1848 port->bfd = NULL;
0477baa9 1849 port->lldp = NULL;
f025bcb7 1850 port->may_enable = false;
21f7563c
JP
1851 port->stp_port = NULL;
1852 port->stp_state = STP_DISABLED;
9efd308e
DV
1853 port->rstp_port = NULL;
1854 port->rstp_state = RSTP_DISABLED;
42943cde 1855 port->is_tunnel = false;
6cbbf4fa 1856 port->peer = NULL;
55954f6e
EJ
1857 port->qdscp = NULL;
1858 port->n_qdscp = 0;
b9ad7294 1859 port->carrier_seq = netdev_get_carrier_resets(netdev);
abe529af 1860
b9ad7294 1861 if (netdev_vport_is_patch(netdev)) {
743cea45 1862 /* By bailing out here, we don't submit the port to the sFlow module
185b4e3a
MG
1863 * to be considered for counter polling export. This is correct
1864 * because the patch port represents an interface that sFlow considers
1865 * to be "internal" to the switch as a whole, and therefore not a
1866 * candidate for counter polling. */
4e022ec0 1867 port->odp_port = ODPP_NONE;
6cbbf4fa 1868 ofport_update_peer(port);
0a740f48
EJ
1869 return 0;
1870 }
1871
6d9e94b5
AZ
1872 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1873 error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
e1b1d06a
JP
1874 &dpif_port);
1875 if (error) {
1876 return error;
1877 }
1878
1879 port->odp_port = dpif_port.port_no;
1880
b9ad7294 1881 if (netdev_get_tunnel_config(netdev)) {
a36de779 1882 atomic_count_inc(&ofproto->backer->tnl_count);
ea0797c9 1883 error = tnl_port_add(port, port->up.netdev, port->odp_port,
6d9e94b5 1884 ovs_native_tunneling_is_on(ofproto), dp_port_name);
ea0797c9
BP
1885 if (error) {
1886 atomic_count_dec(&ofproto->backer->tnl_count);
1887 dpif_port_destroy(&dpif_port);
1888 return error;
1889 }
1890
42943cde 1891 port->is_tunnel = true;
b9ad7294
EJ
1892 } else {
1893 /* Sanity-check that a mapping doesn't already exist. This
1894 * shouldn't happen for non-tunnel ports. */
1895 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1896 VLOG_ERR("port %s already has an OpenFlow port number",
1897 dpif_port.name);
da78d43d 1898 dpif_port_destroy(&dpif_port);
b9ad7294
EJ
1899 return EBUSY;
1900 }
e1b1d06a 1901
8449c4d6 1902 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1903 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
f9c0c3ec 1904 hash_odp_port(port->odp_port));
8449c4d6 1905 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1906 }
da78d43d 1907 dpif_port_destroy(&dpif_port);
e1b1d06a 1908
abe529af 1909 if (ofproto->sflow) {
e1b1d06a 1910 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
abe529af 1911 }
cd32509e
MW
1912 if (ofproto->ipfix) {
1913 dpif_ipfix_add_port(ofproto->ipfix, port_, port->odp_port);
1914 }
abe529af
BP
1915
1916 return 0;
1917}
1918
1919static void
9aad5a5a 1920port_destruct(struct ofport *port_, bool del)
abe529af
BP
1921{
1922 struct ofport_dpif *port = ofport_dpif_cast(port_);
1923 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
02f8d646 1924 const char *devname = netdev_get_name(port->up.netdev);
3aa30359
BP
1925 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1926 const char *dp_port_name;
abe529af 1927
7894e7da 1928 ofproto->backer->need_revalidate = REV_RECONFIGURE;
84f0f298 1929 xlate_txn_start();
46c88433 1930 xlate_ofport_remove(port);
84f0f298 1931 xlate_txn_commit();
7894e7da 1932
3aa30359
BP
1933 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1934 sizeof namebuf);
9aad5a5a 1935 if (del && dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
acf60855
JP
1936 /* The underlying device is still there, so delete it. This
1937 * happens when the ofproto is being destroyed, since the caller
1938 * assumes that removal of attached ports will happen as part of
1939 * destruction. */
42943cde 1940 if (!port->is_tunnel) {
97459c2f
AV
1941 dpif_port_del(ofproto->backer->dpif, port->odp_port, false);
1942 }
1943 } else if (del) {
1944 /* The underlying device is already deleted (e.g. tunctl -d).
1945 * Calling dpif_port_remove to do local cleanup for the netdev */
1946 if (!port->is_tunnel) {
1947 dpif_port_del(ofproto->backer->dpif, port->odp_port, true);
a614d823 1948 }
acf60855
JP
1949 }
1950
6cbbf4fa
EJ
1951 if (port->peer) {
1952 port->peer->peer = NULL;
1953 port->peer = NULL;
1954 }
1955
42943cde 1956 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
8449c4d6 1957 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48 1958 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
8449c4d6 1959 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48
EJ
1960 }
1961
a36de779
PS
1962 if (port->is_tunnel) {
1963 atomic_count_dec(&ofproto->backer->tnl_count);
1964 }
1965
c8025aee 1966 tnl_port_del(port, port->odp_port);
02f8d646 1967 sset_find_and_delete(&ofproto->ports, devname);
0a740f48 1968 sset_find_and_delete(&ofproto->ghost_ports, devname);
abe529af 1969 bundle_remove(port_);
a5610457 1970 set_cfm(port_, NULL);
8aee94b6 1971 set_bfd(port_, NULL);
0477baa9 1972 set_lldp(port_, NULL);
0c0c32d8
BP
1973 if (port->stp_port) {
1974 stp_port_disable(port->stp_port);
1975 }
f025bcb7 1976 set_rstp_port(port_, NULL);
abe529af 1977 if (ofproto->sflow) {
bae473fe 1978 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
abe529af 1979 }
cd32509e
MW
1980 if (ofproto->ipfix) {
1981 dpif_ipfix_del_port(ofproto->ipfix, port->odp_port);
1982 }
8b36f51e 1983
55954f6e 1984 free(port->qdscp);
abe529af
BP
1985}
1986
1987static void
1988port_modified(struct ofport *port_)
1989{
1990 struct ofport_dpif *port = ofport_dpif_cast(port_);
a36de779 1991 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
6d9e94b5 1992 const char *dp_port_name;
a36de779 1993 struct netdev *netdev = port->up.netdev;
abe529af
BP
1994
1995 if (port->bundle && port->bundle->bond) {
a36de779 1996 bond_slave_set_netdev(port->bundle->bond, port, netdev);
abe529af 1997 }
9d46b444
EJ
1998
1999 if (port->cfm) {
a36de779 2000 cfm_set_netdev(port->cfm, netdev);
9d46b444 2001 }
f1e78bbd 2002
c1c4e8c7 2003 if (port->bfd) {
a36de779 2004 bfd_set_netdev(port->bfd, netdev);
c1c4e8c7
AW
2005 }
2006
f73b83fd
LS
2007 /* Set liveness, unless the link is administratively or
2008 * operationally down or link monitoring false */
2009 if (!(port->up.pp.config & OFPUTIL_PC_PORT_DOWN) &&
2010 !(port->up.pp.state & OFPUTIL_PS_LINK_DOWN) &&
2011 port->may_enable) {
2012 port->up.pp.state |= OFPUTIL_PS_LIVE;
2013 } else {
2014 port->up.pp.state &= ~OFPUTIL_PS_LIVE;
2015 }
2016
635c5db9 2017 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
74ff3298 2018 port->lldp, &port->up.pp.hw_addr);
635c5db9 2019
6d9e94b5 2020 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
a36de779
PS
2021
2022 if (port->is_tunnel) {
2023 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
2024
c8025aee 2025 if (tnl_port_reconfigure(port, netdev, port->odp_port, port->odp_port,
6d9e94b5
AZ
2026 ovs_native_tunneling_is_on(ofproto),
2027 dp_port_name)) {
a36de779
PS
2028 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2029 }
f1e78bbd 2030 }
6cbbf4fa
EJ
2031
2032 ofport_update_peer(port);
abe529af
BP
2033}
2034
2035static void
9e1fd49b 2036port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
abe529af
BP
2037{
2038 struct ofport_dpif *port = ofport_dpif_cast(port_);
2039 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
9e1fd49b 2040 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
abe529af 2041
9e1fd49b 2042 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
c57b2226
BP
2043 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
2044 OFPUTIL_PC_NO_PACKET_IN)) {
2cc3c58e 2045 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7bde8dd8 2046
9e1fd49b 2047 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
7bde8dd8
JP
2048 bundle_update(port->bundle);
2049 }
abe529af
BP
2050 }
2051}
2052
2053static int
2054set_sflow(struct ofproto *ofproto_,
2055 const struct ofproto_sflow_options *sflow_options)
2056{
2057 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bae473fe 2058 struct dpif_sflow *ds = ofproto->sflow;
6ff686f2 2059
abe529af 2060 if (sflow_options) {
4fe0f203 2061 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
bae473fe 2062 if (!ds) {
abe529af
BP
2063 struct ofport_dpif *ofport;
2064
4213f19d 2065 ds = ofproto->sflow = dpif_sflow_create();
abe529af 2066 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
e1b1d06a 2067 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
abe529af
BP
2068 }
2069 }
bae473fe 2070 dpif_sflow_set_options(ds, sflow_options);
4fe0f203
BP
2071 if (dpif_sflow_get_probability(ds) != old_probability) {
2072 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2073 }
abe529af 2074 } else {
6ff686f2 2075 if (ds) {
9723bcce 2076 dpif_sflow_unref(ds);
2cc3c58e 2077 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6ff686f2
PS
2078 ofproto->sflow = NULL;
2079 }
abe529af
BP
2080 }
2081 return 0;
2082}
2083
29089a54
RL
2084static int
2085set_ipfix(
2086 struct ofproto *ofproto_,
2087 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
2088 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
2089 size_t n_flow_exporters_options)
2090{
2091 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2092 struct dpif_ipfix *di = ofproto->ipfix;
978427a5 2093 bool has_options = bridge_exporter_options || flow_exporters_options;
8b7ea2d4 2094 bool new_di = false;
29089a54 2095
978427a5
RL
2096 if (has_options && !di) {
2097 di = ofproto->ipfix = dpif_ipfix_create();
8b7ea2d4 2098 new_di = true;
978427a5
RL
2099 }
2100
2101 if (di) {
2102 /* Call set_options in any case to cleanly flush the flow
2103 * caches in the last exporters that are to be destroyed. */
29089a54
RL
2104 dpif_ipfix_set_options(
2105 di, bridge_exporter_options, flow_exporters_options,
2106 n_flow_exporters_options);
978427a5 2107
cd32509e 2108 /* Add ports only when a new ipfix created */
8b7ea2d4
WZ
2109 if (new_di == true) {
2110 struct ofport_dpif *ofport;
2111 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
cd32509e 2112 dpif_ipfix_add_port(di, &ofport->up, ofport->odp_port);
8b7ea2d4
WZ
2113 }
2114 }
2115
978427a5 2116 if (!has_options) {
d857c8aa 2117 dpif_ipfix_unref(di);
29089a54
RL
2118 ofproto->ipfix = NULL;
2119 }
2120 }
978427a5 2121
29089a54
RL
2122 return 0;
2123}
2124
fb8f22c1
BY
2125static int
2126get_ipfix_stats(const struct ofproto *ofproto_,
2127 bool bridge_ipfix,
2128 struct ovs_list *replies)
2129{
2130 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2131 struct dpif_ipfix *di = ofproto->ipfix;
2132
2133 if (!di) {
2134 return OFPERR_NXST_NOT_CONFIGURED;
2135 }
2136
2137 return dpif_ipfix_get_stats(di, bridge_ipfix, replies);
2138}
2139
abe529af 2140static int
a5610457 2141set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
abe529af
BP
2142{
2143 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4e5e44e3
AW
2144 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2145 struct cfm *old = ofport->cfm;
635c5db9 2146 int error = 0;
abe529af 2147
635c5db9 2148 if (s) {
abe529af 2149 if (!ofport->cfm) {
90967e95 2150 ofport->cfm = cfm_create(ofport->up.netdev);
abe529af
BP
2151 }
2152
a5610457 2153 if (cfm_configure(ofport->cfm, s)) {
635c5db9
AW
2154 error = 0;
2155 goto out;
abe529af
BP
2156 }
2157
2158 error = EINVAL;
2159 }
8e9a73fa 2160 cfm_unref(ofport->cfm);
abe529af 2161 ofport->cfm = NULL;
635c5db9 2162out:
4e5e44e3
AW
2163 if (ofport->cfm != old) {
2164 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2165 }
635c5db9 2166 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
74ff3298 2167 ofport->lldp, &ofport->up.pp.hw_addr);
abe529af
BP
2168 return error;
2169}
2170
8f5514fe
AW
2171static bool
2172cfm_status_changed(struct ofport *ofport_)
2173{
2174 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2175
2176 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2177}
2178
88bf179a 2179static int
9a9e3786 2180get_cfm_status(const struct ofport *ofport_,
685acfd9 2181 struct cfm_status *status)
1de11730
EJ
2182{
2183 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
88bf179a 2184 int ret = 0;
1de11730
EJ
2185
2186 if (ofport->cfm) {
8f5514fe 2187 cfm_get_status(ofport->cfm, status);
1de11730 2188 } else {
88bf179a 2189 ret = ENOENT;
1de11730 2190 }
88bf179a
AW
2191
2192 return ret;
1de11730 2193}
ccc09689
EJ
2194
2195static int
2196set_bfd(struct ofport *ofport_, const struct smap *cfg)
2197{
2198 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2199 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2200 struct bfd *old;
2201
2202 old = ofport->bfd;
c1c4e8c7
AW
2203 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2204 cfg, ofport->up.netdev);
ccc09689
EJ
2205 if (ofport->bfd != old) {
2206 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2207 }
635c5db9 2208 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
74ff3298 2209 ofport->lldp, &ofport->up.pp.hw_addr);
ccc09689
EJ
2210 return 0;
2211}
2212
8f5514fe
AW
2213static bool
2214bfd_status_changed(struct ofport *ofport_)
2215{
2216 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2217
2218 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2219}
2220
ccc09689
EJ
2221static int
2222get_bfd_status(struct ofport *ofport_, struct smap *smap)
2223{
2224 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
88bf179a 2225 int ret = 0;
ccc09689
EJ
2226
2227 if (ofport->bfd) {
8f5514fe 2228 bfd_get_status(ofport->bfd, smap);
ccc09689 2229 } else {
88bf179a 2230 ret = ENOENT;
ccc09689 2231 }
88bf179a
AW
2232
2233 return ret;
ccc09689 2234}
0477baa9
DF
2235
2236static int
2237set_lldp(struct ofport *ofport_,
2238 const struct smap *cfg)
2239{
2240 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2241 int error = 0;
2242
2243 if (cfg) {
2244 if (!ofport->lldp) {
2245 struct ofproto_dpif *ofproto;
2246
2247 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2248 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2249 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2250 }
2251
19aef6ef
DF
2252 if (!lldp_configure(ofport->lldp, cfg)) {
2253 error = EINVAL;
0477baa9 2254 }
0477baa9 2255 }
19aef6ef
DF
2256 if (error) {
2257 lldp_unref(ofport->lldp);
2258 ofport->lldp = NULL;
2259 }
2260
0477baa9
DF
2261 ofproto_dpif_monitor_port_update(ofport,
2262 ofport->bfd,
2263 ofport->cfm,
2264 ofport->lldp,
74ff3298 2265 &ofport->up.pp.hw_addr);
0477baa9
DF
2266 return error;
2267}
2268
2269static bool
2270get_lldp_status(const struct ofport *ofport_,
2271 struct lldp_status *status OVS_UNUSED)
2272{
2273 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2274
2275 return ofport->lldp ? true : false;
2276}
2277
2278static int
2279set_aa(struct ofproto *ofproto OVS_UNUSED,
2280 const struct aa_settings *s)
2281{
2282 return aa_configure(s);
2283}
2284
2285static int
2286aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2287 const struct aa_mapping_settings *s)
2288{
2289 return aa_mapping_register(aux, s);
2290}
2291
2292static int
2293aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2294{
2295 return aa_mapping_unregister(aux);
2296}
2297
2298static int
2299aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2300{
2301 return aa_get_vlan_queued(list);
2302}
2303
2304static unsigned int
2305aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2306{
2307 return aa_get_vlan_queue_size();
2308}
2309
abe529af 2310\f
21f7563c
JP
2311/* Spanning Tree. */
2312
6b90bc57 2313/* Called while rstp_mutex is held. */
9efd308e 2314static void
cf62fa4c 2315rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
9efd308e
DV
2316{
2317 struct ofproto_dpif *ofproto = ofproto_;
6b90bc57 2318 struct ofport_dpif *ofport = ofport_;
2482b0b0 2319 struct eth_header *eth = dp_packet_eth(pkt);
6b90bc57 2320
74ff3298 2321 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
6b90bc57
JR
2322 if (eth_addr_is_zero(eth->eth_src)) {
2323 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2324 "does not have a configured source MAC address.",
2325 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
9efd308e 2326 } else {
2eb79142 2327 ofproto_dpif_send_packet(ofport, false, pkt);
9efd308e 2328 }
cf62fa4c 2329 dp_packet_delete(pkt);
9efd308e
DV
2330}
2331
21f7563c 2332static void
cf62fa4c 2333send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
21f7563c
JP
2334{
2335 struct ofproto_dpif *ofproto = ofproto_;
2336 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2337 struct ofport_dpif *ofport;
2338
2339 ofport = stp_port_get_aux(sp);
2340 if (!ofport) {
2341 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2342 ofproto->up.name, port_num);
2343 } else {
2482b0b0 2344 struct eth_header *eth = dp_packet_eth(pkt);
21f7563c 2345
74ff3298 2346 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
21f7563c
JP
2347 if (eth_addr_is_zero(eth->eth_src)) {
2348 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2349 "with unknown MAC", ofproto->up.name, port_num);
2350 } else {
2eb79142 2351 ofproto_dpif_send_packet(ofport, false, pkt);
21f7563c
JP
2352 }
2353 }
cf62fa4c 2354 dp_packet_delete(pkt);
21f7563c
JP
2355}
2356
9efd308e
DV
2357/* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2358static void
2359set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2360{
2361 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2362
2363 /* Only revalidate flows if the configuration changed. */
2364 if (!s != !ofproto->rstp) {
2365 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2366 }
2367
2368 if (s) {
2369 if (!ofproto->rstp) {
2370 ofproto->rstp = rstp_create(ofproto_->name, s->address,
4b30ba05 2371 rstp_send_bpdu_cb, ofproto);
9efd308e
DV
2372 ofproto->rstp_last_tick = time_msec();
2373 }
2374 rstp_set_bridge_address(ofproto->rstp, s->address);
2375 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2376 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2377 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2378 s->force_protocol_version);
2379 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2380 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2381 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2382 s->transmit_hold_count);
2383 } else {
2384 struct ofport *ofport;
2385 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2386 set_rstp_port(ofport, NULL);
2387 }
2388 rstp_unref(ofproto->rstp);
2389 ofproto->rstp = NULL;
2390 }
2391}
2392
2393static void
2394get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2395{
2396 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2397
2398 if (ofproto->rstp) {
2399 s->enabled = true;
2400 s->root_id = rstp_get_root_id(ofproto->rstp);
2401 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2402 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2403 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2404 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2405 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2406 } else {
2407 s->enabled = false;
2408 }
2409}
2410
2411static void
2412update_rstp_port_state(struct ofport_dpif *ofport)
2413{
2414 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2415 enum rstp_state state;
2416
2417 /* Figure out new state. */
2418 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2419 : RSTP_DISABLED;
2420
2421 /* Update state. */
2422 if (ofport->rstp_state != state) {
2423 enum ofputil_port_state of_state;
2424 bool fwd_change;
2425
9a0bb428
JR
2426 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2427 netdev_get_name(ofport->up.netdev),
2428 rstp_state_name(ofport->rstp_state),
2429 rstp_state_name(state));
2372c146 2430
9efd308e 2431 if (rstp_learn_in_state(ofport->rstp_state)
66ff280d
JR
2432 != rstp_learn_in_state(state)) {
2433 /* XXX: Learning action flows should also be flushed. */
2434 if (ofport->bundle) {
2372c146
JR
2435 if (!rstp_shift_root_learned_address(ofproto->rstp)
2436 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2437 bundle_flush_macs(ofport->bundle, false);
2438 }
66ff280d 2439 }
9efd308e
DV
2440 }
2441 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2442 != rstp_forward_in_state(state);
2443
2444 ofproto->backer->need_revalidate = REV_RSTP;
2445 ofport->rstp_state = state;
2446
2447 if (fwd_change && ofport->bundle) {
2448 bundle_update(ofport->bundle);
2449 }
2450
2451 /* Update the RSTP state bits in the OpenFlow port description. */
2452 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2453 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2454 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2455 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2456 : 0);
2457 ofproto_port_set_state(&ofport->up, of_state);
2458 }
2459}
2460
2461static void
2462rstp_run(struct ofproto_dpif *ofproto)
2463{
2464 if (ofproto->rstp) {
2465 long long int now = time_msec();
2466 long long int elapsed = now - ofproto->rstp_last_tick;
2467 struct rstp_port *rp;
f025bcb7 2468 struct ofport_dpif *ofport;
54d3863b 2469
9efd308e
DV
2470 /* Every second, decrease the values of the timers. */
2471 if (elapsed >= 1000) {
2472 rstp_tick_timers(ofproto->rstp);
2473 ofproto->rstp_last_tick = now;
2474 }
f025bcb7
JR
2475 rp = NULL;
2476 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2477 update_rstp_port_state(ofport);
9efd308e 2478 }
66ff280d
JR
2479 rp = NULL;
2480 ofport = NULL;
9efd308e
DV
2481 /* FIXME: This check should be done on-event (i.e., when setting
2482 * p->fdb_flush) and not periodically.
2483 */
66ff280d 2484 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2372c146
JR
2485 if (!rstp_shift_root_learned_address(ofproto->rstp)
2486 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2487 bundle_flush_macs(ofport->bundle, false);
2488 }
2489 }
2490
2491 if (rstp_shift_root_learned_address(ofproto->rstp)) {
c7e4f0b2
DV
2492 struct ofport_dpif *old_root_aux =
2493 (struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp);
2494 struct ofport_dpif *new_root_aux =
2495 (struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp);
2496 if (old_root_aux != NULL && new_root_aux != NULL) {
2497 bundle_move(old_root_aux->bundle, new_root_aux->bundle);
2498 rstp_reset_root_changed(ofproto->rstp);
2499 }
9efd308e
DV
2500 }
2501 }
2502}
2503
21f7563c
JP
2504/* Configures STP on 'ofproto_' using the settings defined in 's'. */
2505static int
2506set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2507{
2508 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2509
2510 /* Only revalidate flows if the configuration changed. */
2511 if (!s != !ofproto->stp) {
2cc3c58e 2512 ofproto->backer->need_revalidate = REV_RECONFIGURE;
21f7563c
JP
2513 }
2514
2515 if (s) {
2516 if (!ofproto->stp) {
2517 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2518 send_bpdu_cb, ofproto);
2519 ofproto->stp_last_tick = time_msec();
2520 }
2521
2522 stp_set_bridge_id(ofproto->stp, s->system_id);
2523 stp_set_bridge_priority(ofproto->stp, s->priority);
2524 stp_set_hello_time(ofproto->stp, s->hello_time);
2525 stp_set_max_age(ofproto->stp, s->max_age);
2526 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2527 } else {
851bf71d
EJ
2528 struct ofport *ofport;
2529
2530 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2531 set_stp_port(ofport, NULL);
2532 }
2533
bd54dbcd 2534 stp_unref(ofproto->stp);
21f7563c
JP
2535 ofproto->stp = NULL;
2536 }
2537
2538 return 0;
2539}
2540
2541static int
2542get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2543{
2544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2545
2546 if (ofproto->stp) {
2547 s->enabled = true;
2548 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2549 s->designated_root = stp_get_designated_root(ofproto->stp);
2550 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2551 } else {
2552 s->enabled = false;
2553 }
2554
2555 return 0;
2556}
2557
2558static void
2559update_stp_port_state(struct ofport_dpif *ofport)
2560{
2561 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2562 enum stp_state state;
2563
2564 /* Figure out new state. */
2565 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2566 : STP_DISABLED;
2567
2568 /* Update state. */
2569 if (ofport->stp_state != state) {
9e1fd49b 2570 enum ofputil_port_state of_state;
21f7563c
JP
2571 bool fwd_change;
2572
9a0bb428
JR
2573 VLOG_DBG("port %s: STP state changed from %s to %s",
2574 netdev_get_name(ofport->up.netdev),
2575 stp_state_name(ofport->stp_state),
2576 stp_state_name(state));
21f7563c
JP
2577 if (stp_learn_in_state(ofport->stp_state)
2578 != stp_learn_in_state(state)) {
2579 /* xxx Learning action flows should also be flushed. */
509c0149 2580 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 2581 mac_learning_flush(ofproto->ml);
509c0149 2582 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 2583 mcast_snooping_mdb_flush(ofproto->ms);
21f7563c
JP
2584 }
2585 fwd_change = stp_forward_in_state(ofport->stp_state)
2586 != stp_forward_in_state(state);
2587
2cc3c58e 2588 ofproto->backer->need_revalidate = REV_STP;
21f7563c
JP
2589 ofport->stp_state = state;
2590 ofport->stp_state_entered = time_msec();
2591
b308140a 2592 if (fwd_change && ofport->bundle) {
21f7563c
JP
2593 bundle_update(ofport->bundle);
2594 }
2595
2596 /* Update the STP state bits in the OpenFlow port description. */
9e1fd49b
BP
2597 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2598 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2599 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2600 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2601 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2602 : 0);
21f7563c
JP
2603 ofproto_port_set_state(&ofport->up, of_state);
2604 }
2605}
2606
52182c5f 2607static void
2608stp_check_and_update_link_state(struct ofproto_dpif *ofproto)
2609{
2610 struct ofport_dpif *ofport;
2611
2612 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2613 bool up = netdev_get_carrier(ofport->up.netdev);
2614
2615 if (ofport->stp_port &&
2616 up != (stp_port_get_state(ofport->stp_port) != STP_DISABLED)) {
2617
2618 VLOG_DBG("bridge %s, port %s is %s, %s it.",
2619 ofproto->up.name, netdev_get_name(ofport->up.netdev),
2620 up ? "up" : "down",
2621 up ? "enabling" : "disabling");
2622
2623 if (up) {
2624 stp_port_enable(ofport->stp_port);
2625 stp_port_set_aux(ofport->stp_port, ofport);
2626 } else {
2627 stp_port_disable(ofport->stp_port);
2628 }
2629
2630 update_stp_port_state(ofport);
2631 }
2632 }
2633}
2634
21f7563c
JP
2635/* Configures STP on 'ofport_' using the settings defined in 's'. The
2636 * caller is responsible for assigning STP port numbers and ensuring
2637 * there are no duplicates. */
2638static int
2639set_stp_port(struct ofport *ofport_,
2640 const struct ofproto_port_stp_settings *s)
2641{
2642 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2643 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2644 struct stp_port *sp = ofport->stp_port;
2645
2646 if (!s || !s->enable) {
2647 if (sp) {
2648 ofport->stp_port = NULL;
2649 stp_port_disable(sp);
ecd12731 2650 update_stp_port_state(ofport);
21f7563c
JP
2651 }
2652 return 0;
2653 } else if (sp && stp_port_no(sp) != s->port_num
f025bcb7 2654 && ofport == stp_port_get_aux(sp)) {
21f7563c
JP
2655 /* The port-id changed, so disable the old one if it's not
2656 * already in use by another port. */
2657 stp_port_disable(sp);
2658 }
2659
2660 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
21f7563c 2661
fb20b0bf
JR
2662 /* Set name before enabling the port so that debugging messages can print
2663 * the name. */
11306274 2664 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
52182c5f 2665
2666 if (netdev_get_carrier(ofport_->netdev)) {
2667 stp_port_enable(sp);
2668 } else {
2669 stp_port_disable(sp);
2670 }
fb20b0bf 2671
21f7563c
JP
2672 stp_port_set_aux(sp, ofport);
2673 stp_port_set_priority(sp, s->priority);
2674 stp_port_set_path_cost(sp, s->path_cost);
2675
2676 update_stp_port_state(ofport);
2677
2678 return 0;
2679}
2680
2681static int
2682get_stp_port_status(struct ofport *ofport_,
2683 struct ofproto_port_stp_status *s)
2684{
2685 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2686 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2687 struct stp_port *sp = ofport->stp_port;
2688
2689 if (!ofproto->stp || !sp) {
2690 s->enabled = false;
2691 return 0;
2692 }
2693
2694 s->enabled = true;
926c9a4a 2695 stp_port_get_status(sp, &s->port_id, &s->state, &s->role);
21f7563c 2696 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
fd28ce3a
JS
2697
2698 return 0;
2699}
2700
2701static int
2702get_stp_port_stats(struct ofport *ofport_,
2703 struct ofproto_port_stp_stats *s)
2704{
2705 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2706 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2707 struct stp_port *sp = ofport->stp_port;
2708
2709 if (!ofproto->stp || !sp) {
2710 s->enabled = false;
2711 return 0;
2712 }
2713
2714 s->enabled = true;
80740385 2715 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
21f7563c
JP
2716
2717 return 0;
2718}
2719
2720static void
2721stp_run(struct ofproto_dpif *ofproto)
2722{
2723 if (ofproto->stp) {
2724 long long int now = time_msec();
2725 long long int elapsed = now - ofproto->stp_last_tick;
2726 struct stp_port *sp;
2727
2728 if (elapsed > 0) {
2729 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2730 ofproto->stp_last_tick = now;
2731 }
52182c5f 2732
2733 stp_check_and_update_link_state(ofproto);
2734
21f7563c
JP
2735 while (stp_get_changed_port(ofproto->stp, &sp)) {
2736 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2737
2738 if (ofport) {
2739 update_stp_port_state(ofport);
2740 }
2741 }
6ae50723
EJ
2742
2743 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
509c0149 2744 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 2745 mac_learning_flush(ofproto->ml);
509c0149 2746 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 2747 mcast_snooping_mdb_flush(ofproto->ms);
6ae50723 2748 }
21f7563c
JP
2749 }
2750}
2751
2752static void
2753stp_wait(struct ofproto_dpif *ofproto)
2754{
2755 if (ofproto->stp) {
2756 poll_timer_wait(1000);
2757 }
2758}
9efd308e
DV
2759
2760/* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2761 * caller is responsible for assigning RSTP port numbers and ensuring
2762 * there are no duplicates. */
2763static void
2764set_rstp_port(struct ofport *ofport_,
cc33c223 2765 const struct ofproto_port_rstp_settings *s)
9efd308e
DV
2766{
2767 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2768 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2769 struct rstp_port *rp = ofport->rstp_port;
9efd308e
DV
2770
2771 if (!s || !s->enable) {
2772 if (rp) {
e73e9165
JR
2773 rstp_port_set_aux(rp, NULL);
2774 rstp_port_set_state(rp, RSTP_DISABLED);
2775 rstp_port_set_mac_operational(rp, false);
9efd308e 2776 ofport->rstp_port = NULL;
e73e9165 2777 rstp_port_unref(rp);
9efd308e
DV
2778 update_rstp_port_state(ofport);
2779 }
9efd308e
DV
2780 return;
2781 }
f025bcb7
JR
2782
2783 /* Check if need to add a new port. */
9efd308e
DV
2784 if (!rp) {
2785 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2786 }
9efd308e 2787
f025bcb7 2788 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
67e8c1ac
JR
2789 s->admin_edge_port, s->auto_edge,
2790 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
8d2f8375 2791 ofport, netdev_get_name(ofport->up.netdev));
9efd308e 2792 update_rstp_port_state(ofport);
29db47ce
JR
2793 /* Synchronize operational status. */
2794 rstp_port_set_mac_operational(rp, ofport->may_enable);
9efd308e
DV
2795}
2796
2797static void
2798get_rstp_port_status(struct ofport *ofport_,
2799 struct ofproto_port_rstp_status *s)
2800{
2801 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2802 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2803 struct rstp_port *rp = ofport->rstp_port;
2804
2805 if (!ofproto->rstp || !rp) {
2806 s->enabled = false;
2807 return;
2808 }
2809
2810 s->enabled = true;
9c64e6b8
JR
2811 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2812 &s->designated_bridge_id, &s->designated_port_id,
2813 &s->designated_path_cost, &s->tx_count,
f025bcb7 2814 &s->rx_count, &s->error_count, &s->uptime);
9efd308e
DV
2815}
2816
21f7563c 2817\f
8b36f51e 2818static int
55954f6e 2819set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
8b36f51e
EJ
2820 size_t n_qdscp)
2821{
2822 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2823 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
8b36f51e 2824
55954f6e
EJ
2825 if (ofport->n_qdscp != n_qdscp
2826 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2827 n_qdscp * sizeof *qdscp))) {
2cc3c58e 2828 ofproto->backer->need_revalidate = REV_RECONFIGURE;
55954f6e
EJ
2829 free(ofport->qdscp);
2830 ofport->qdscp = n_qdscp
2831 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2832 : NULL;
2833 ofport->n_qdscp = n_qdscp;
8b36f51e
EJ
2834 }
2835
8b36f51e
EJ
2836 return 0;
2837}
2838\f
abe529af
BP
2839/* Bundles. */
2840
b44a10b7
BP
2841/* Expires all MAC learning entries associated with 'bundle' and forces its
2842 * ofproto to revalidate every flow.
2843 *
2844 * Normally MAC learning entries are removed only from the ofproto associated
2845 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2846 * are removed from every ofproto. When patch ports and SLB bonds are in use
2847 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2848 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2849 * with the host from which it migrated. */
abe529af 2850static void
b44a10b7 2851bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
abe529af
BP
2852{
2853 struct ofproto_dpif *ofproto = bundle->ofproto;
2854 struct mac_learning *ml = ofproto->ml;
2855 struct mac_entry *mac, *next_mac;
2856
2cc3c58e 2857 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 2858 ovs_rwlock_wrlock(&ml->rwlock);
abe529af 2859 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
9d078ec2 2860 if (mac_entry_get_port(ml, mac) == bundle) {
b44a10b7
BP
2861 if (all_ofprotos) {
2862 struct ofproto_dpif *o;
2863
911b4a7e
JP
2864 HMAP_FOR_EACH (o, all_ofproto_dpifs_by_name_node,
2865 &all_ofproto_dpifs_by_name) {
b44a10b7
BP
2866 if (o != ofproto) {
2867 struct mac_entry *e;
2868
509c0149 2869 ovs_rwlock_wrlock(&o->ml->rwlock);
30618594 2870 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
b44a10b7 2871 if (e) {
b44a10b7
BP
2872 mac_learning_expire(o->ml, e);
2873 }
509c0149 2874 ovs_rwlock_unlock(&o->ml->rwlock);
b44a10b7
BP
2875 }
2876 }
2877 }
2878
abe529af
BP
2879 mac_learning_expire(ml, mac);
2880 }
2881 }
509c0149 2882 ovs_rwlock_unlock(&ml->rwlock);
abe529af
BP
2883}
2884
2372c146
JR
2885static void
2886bundle_move(struct ofbundle *old, struct ofbundle *new)
2887{
2888 struct ofproto_dpif *ofproto = old->ofproto;
2889 struct mac_learning *ml = ofproto->ml;
2890 struct mac_entry *mac, *next_mac;
2891
2892 ovs_assert(new->ofproto == old->ofproto);
2893
2894 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2895 ovs_rwlock_wrlock(&ml->rwlock);
2896 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
9d078ec2
BP
2897 if (mac_entry_get_port(ml, mac) == old) {
2898 mac_entry_set_port(ml, mac, new);
2372c146
JR
2899 }
2900 }
2901 ovs_rwlock_unlock(&ml->rwlock);
2902}
2903
abe529af
BP
2904static struct ofbundle *
2905bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2906{
2907 struct ofbundle *bundle;
2908
2909 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2910 &ofproto->bundles) {
2911 if (bundle->aux == aux) {
2912 return bundle;
2913 }
2914 }
2915 return NULL;
2916}
2917
7bde8dd8
JP
2918static void
2919bundle_update(struct ofbundle *bundle)
2920{
2921 struct ofport_dpif *port;
2922
2923 bundle->floodable = true;
2924 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
9e1fd49b 2925 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
875ab130 2926 || netdev_get_pt_mode(port->up.netdev) == NETDEV_PT_LEGACY_L3
4b5f1996
DV
2927 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2928 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
7bde8dd8
JP
2929 bundle->floodable = false;
2930 break;
2931 }
2932 }
2933}
2934
abe529af
BP
2935static void
2936bundle_del_port(struct ofport_dpif *port)
2937{
2938 struct ofbundle *bundle = port->bundle;
2939
2cc3c58e 2940 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
6f77f4ae 2941
417e7e66 2942 ovs_list_remove(&port->bundle_node);
abe529af
BP
2943 port->bundle = NULL;
2944
2945 if (bundle->lacp) {
2946 lacp_slave_unregister(bundle->lacp, port);
2947 }
2948 if (bundle->bond) {
2949 bond_slave_unregister(bundle->bond, port);
2950 }
2951
7bde8dd8 2952 bundle_update(bundle);
abe529af
BP
2953}
2954
2955static bool
4e022ec0 2956bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
df53d41c 2957 struct lacp_slave_settings *lacp)
abe529af
BP
2958{
2959 struct ofport_dpif *port;
2960
e672ff9b 2961 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
abe529af
BP
2962 if (!port) {
2963 return false;
2964 }
2965
2966 if (port->bundle != bundle) {
2cc3c58e 2967 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af 2968 if (port->bundle) {
e019a574 2969 bundle_remove(&port->up);
abe529af
BP
2970 }
2971
2972 port->bundle = bundle;
417e7e66 2973 ovs_list_push_back(&bundle->ports, &port->bundle_node);
9e1fd49b 2974 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
875ab130 2975 || netdev_get_pt_mode(port->up.netdev) == NETDEV_PT_LEGACY_L3
4b5f1996
DV
2976 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2977 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
abe529af
BP
2978 bundle->floodable = false;
2979 }
2980 }
2981 if (lacp) {
2cc3c58e 2982 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2983 lacp_slave_register(bundle->lacp, port, lacp);
2984 }
2985
2986 return true;
2987}
2988
2989static void
2990bundle_destroy(struct ofbundle *bundle)
2991{
2992 struct ofproto_dpif *ofproto;
2993 struct ofport_dpif *port, *next_port;
abe529af
BP
2994
2995 if (!bundle) {
2996 return;
2997 }
2998
2999 ofproto = bundle->ofproto;
67912650 3000 mbridge_unregister_bundle(ofproto->mbridge, bundle);
abe529af 3001
84f0f298 3002 xlate_txn_start();
46c88433 3003 xlate_bundle_remove(bundle);
84f0f298 3004 xlate_txn_commit();
46c88433 3005
abe529af
BP
3006 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
3007 bundle_del_port(port);
3008 }
3009
b44a10b7 3010 bundle_flush_macs(bundle, true);
b077575e 3011 mcast_snooping_flush_bundle(ofproto->ms, bundle);
abe529af
BP
3012 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
3013 free(bundle->name);
3014 free(bundle->trunks);
fed8962a 3015 free(bundle->cvlans);
91779071 3016 lacp_unref(bundle->lacp);
03366a2d 3017 bond_unref(bundle->bond);
abe529af
BP
3018 free(bundle);
3019}
3020
3021static int
3022bundle_set(struct ofproto *ofproto_, void *aux,
3023 const struct ofproto_bundle_settings *s)
3024{
3025 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3026 bool need_flush = false;
abe529af
BP
3027 struct ofport_dpif *port;
3028 struct ofbundle *bundle;
f0fb825a 3029 unsigned long *trunks = NULL;
fed8962a 3030 unsigned long *cvlans = NULL;
ecac4ebf 3031 int vlan;
abe529af
BP
3032 size_t i;
3033 bool ok;
3034
afea2e89
DJ
3035 bundle = bundle_lookup(ofproto, aux);
3036
abe529af 3037 if (!s) {
afea2e89 3038 bundle_destroy(bundle);
abe529af
BP
3039 return 0;
3040 }
3041
cb22974d
BP
3042 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
3043 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
abe529af 3044
abe529af
BP
3045 if (!bundle) {
3046 bundle = xmalloc(sizeof *bundle);
3047
3048 bundle->ofproto = ofproto;
3049 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
3050 hash_pointer(aux, 0));
3051 bundle->aux = aux;
3052 bundle->name = NULL;
3053
417e7e66 3054 ovs_list_init(&bundle->ports);
ecac4ebf 3055 bundle->vlan_mode = PORT_VLAN_TRUNK;
fed8962a 3056 bundle->qinq_ethtype = ETH_TYPE_VLAN_8021AD;
abe529af
BP
3057 bundle->vlan = -1;
3058 bundle->trunks = NULL;
fed8962a 3059 bundle->cvlans = NULL;
5e9ceccd 3060 bundle->use_priority_tags = s->use_priority_tags;
abe529af
BP
3061 bundle->lacp = NULL;
3062 bundle->bond = NULL;
3063
3064 bundle->floodable = true;
c005f976 3065 bundle->protected = false;
ec7ceaed 3066 mbridge_register_bundle(ofproto->mbridge, bundle);
abe529af
BP
3067 }
3068
3069 if (!bundle->name || strcmp(s->name, bundle->name)) {
3070 free(bundle->name);
3071 bundle->name = xstrdup(s->name);
3072 }
3073
3074 /* LACP. */
3075 if (s->lacp) {
e2d13c43 3076 ofproto->lacp_enabled = true;
abe529af 3077 if (!bundle->lacp) {
2cc3c58e 3078 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
3079 bundle->lacp = lacp_create();
3080 }
3081 lacp_configure(bundle->lacp, s->lacp);
3082 } else {
91779071 3083 lacp_unref(bundle->lacp);
abe529af
BP
3084 bundle->lacp = NULL;
3085 }
3086
3087 /* Update set of ports. */
3088 ok = true;
3089 for (i = 0; i < s->n_slaves; i++) {
3090 if (!bundle_add_port(bundle, s->slaves[i],
df53d41c 3091 s->lacp ? &s->lacp_slaves[i] : NULL)) {
abe529af
BP
3092 ok = false;
3093 }
3094 }
417e7e66 3095 if (!ok || ovs_list_size(&bundle->ports) != s->n_slaves) {
abe529af
BP
3096 struct ofport_dpif *next_port;
3097
3098 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
3099 for (i = 0; i < s->n_slaves; i++) {
56c769ab 3100 if (s->slaves[i] == port->up.ofp_port) {
abe529af
BP
3101 goto found;
3102 }
3103 }
3104
3105 bundle_del_port(port);
3106 found: ;
3107 }
3108 }
417e7e66 3109 ovs_assert(ovs_list_size(&bundle->ports) <= s->n_slaves);
abe529af 3110
417e7e66 3111 if (ovs_list_is_empty(&bundle->ports)) {
abe529af
BP
3112 bundle_destroy(bundle);
3113 return EINVAL;
3114 }
3115
ecac4ebf 3116 /* Set VLAN tagging mode */
5e9ceccd
BP
3117 if (s->vlan_mode != bundle->vlan_mode
3118 || s->use_priority_tags != bundle->use_priority_tags) {
ecac4ebf 3119 bundle->vlan_mode = s->vlan_mode;
5e9ceccd 3120 bundle->use_priority_tags = s->use_priority_tags;
ecac4ebf
BP
3121 need_flush = true;
3122 }
3123
fed8962a
EG
3124 if (s->qinq_ethtype != bundle->qinq_ethtype) {
3125 bundle->qinq_ethtype = s->qinq_ethtype;
3126 need_flush = true;
3127 }
3128
abe529af 3129 /* Set VLAN tag. */
ecac4ebf
BP
3130 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
3131 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
3132 : 0);
3133 if (vlan != bundle->vlan) {
3134 bundle->vlan = vlan;
abe529af
BP
3135 need_flush = true;
3136 }
3137
3138 /* Get trunked VLANs. */
ecac4ebf
BP
3139 switch (s->vlan_mode) {
3140 case PORT_VLAN_ACCESS:
3141 trunks = NULL;
3142 break;
3143
3144 case PORT_VLAN_TRUNK:
ebc56baa 3145 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
3146 break;
3147
3148 case PORT_VLAN_NATIVE_UNTAGGED:
3149 case PORT_VLAN_NATIVE_TAGGED:
3150 if (vlan != 0 && (!s->trunks
3151 || !bitmap_is_set(s->trunks, vlan)
3152 || bitmap_is_set(s->trunks, 0))) {
3153 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
3154 if (s->trunks) {
3155 trunks = bitmap_clone(s->trunks, 4096);
3156 } else {
3157 trunks = bitmap_allocate1(4096);
3158 }
3159 bitmap_set1(trunks, vlan);
3160 bitmap_set0(trunks, 0);
3161 } else {
ebc56baa 3162 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
3163 }
3164 break;
3165
fed8962a
EG
3166 case PORT_VLAN_DOT1Q_TUNNEL:
3167 cvlans = CONST_CAST(unsigned long *, s->cvlans);
3168 break;
3169
ecac4ebf 3170 default:
428b2edd 3171 OVS_NOT_REACHED();
ecac4ebf 3172 }
abe529af
BP
3173 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
3174 free(bundle->trunks);
ecac4ebf
BP
3175 if (trunks == s->trunks) {
3176 bundle->trunks = vlan_bitmap_clone(trunks);
3177 } else {
3178 bundle->trunks = trunks;
3179 trunks = NULL;
3180 }
abe529af
BP
3181 need_flush = true;
3182 }
ecac4ebf
BP
3183 if (trunks != s->trunks) {
3184 free(trunks);
3185 }
abe529af 3186
fed8962a
EG
3187 if (!vlan_bitmap_equal(cvlans, bundle->cvlans)) {
3188 free(bundle->cvlans);
3189 if (cvlans == s->cvlans) {
3190 bundle->cvlans = vlan_bitmap_clone(cvlans);
3191 } else {
3192 bundle->cvlans = cvlans;
3193 cvlans = NULL;
3194 }
3195 need_flush = true;
3196 }
3197 if (cvlans != s->cvlans) {
3198 free(cvlans);
3199 }
3200
abe529af 3201 /* Bonding. */
417e7e66 3202 if (!ovs_list_is_short(&bundle->ports)) {
abe529af
BP
3203 bundle->ofproto->has_bonded_bundles = true;
3204 if (bundle->bond) {
3205 if (bond_reconfigure(bundle->bond, s->bond)) {
2cc3c58e 3206 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
3207 }
3208 } else {
adcf00ba 3209 bundle->bond = bond_create(s->bond, ofproto);
2cc3c58e 3210 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
3211 }
3212
3213 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
adcf00ba
AZ
3214 bond_slave_register(bundle->bond, port,
3215 port->up.ofp_port, port->up.netdev);
abe529af
BP
3216 }
3217 } else {
03366a2d 3218 bond_unref(bundle->bond);
abe529af
BP
3219 bundle->bond = NULL;
3220 }
3221
c005f976
BK
3222 /* Set proteced port mode */
3223 if (s->protected != bundle->protected) {
3224 bundle->protected = s->protected;
3225 need_flush = true;
3226 }
3227
abe529af
BP
3228 /* If we changed something that would affect MAC learning, un-learn
3229 * everything on this port and force flow revalidation. */
3230 if (need_flush) {
b44a10b7 3231 bundle_flush_macs(bundle, false);
4fbbf862 3232 mcast_snooping_flush_bundle(ofproto->ms, bundle);
abe529af
BP
3233 }
3234
3235 return 0;
3236}
3237
3238static void
3239bundle_remove(struct ofport *port_)
3240{
3241 struct ofport_dpif *port = ofport_dpif_cast(port_);
3242 struct ofbundle *bundle = port->bundle;
3243
3244 if (bundle) {
3245 bundle_del_port(port);
417e7e66 3246 if (ovs_list_is_empty(&bundle->ports)) {
abe529af 3247 bundle_destroy(bundle);
417e7e66 3248 } else if (ovs_list_is_short(&bundle->ports)) {
03366a2d 3249 bond_unref(bundle->bond);
abe529af
BP
3250 bundle->bond = NULL;
3251 }
3252 }
3253}
3254
3255static void
5f877369 3256send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
abe529af 3257{
abe529af 3258 struct ofport_dpif *port = port_;
74ff3298 3259 struct eth_addr ea;
abe529af
BP
3260 int error;
3261
74ff3298 3262 error = netdev_get_etheraddr(port->up.netdev, &ea);
abe529af 3263 if (!error) {
cf62fa4c 3264 struct dp_packet packet;
5f877369 3265 void *packet_pdu;
abe529af 3266
cf62fa4c 3267 dp_packet_init(&packet, 0);
abe529af 3268 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
5f877369
EJ
3269 pdu_size);
3270 memcpy(packet_pdu, pdu, pdu_size);
3271
2eb79142 3272 ofproto_dpif_send_packet(port, false, &packet);
cf62fa4c 3273 dp_packet_uninit(&packet);
abe529af 3274 } else {
7ed58d4a
JP
3275 static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 10);
3276 VLOG_ERR_RL(&rll, "port %s: cannot obtain Ethernet address of iface "
abe529af 3277 "%s (%s)", port->bundle->name,
10a89ef0 3278 netdev_get_name(port->up.netdev), ovs_strerror(error));
abe529af
BP
3279 }
3280}
3281
3282static void
3283bundle_send_learning_packets(struct ofbundle *bundle)
3284{
3285 struct ofproto_dpif *ofproto = bundle->ofproto;
3286 int error, n_packets, n_errors;
3287 struct mac_entry *e;
8613db65
DDP
3288 struct pkt_list {
3289 struct ovs_list list_node;
3290 struct ofport_dpif *port;
3291 struct dp_packet *pkt;
3292 } *pkt_node;
ca6ba700 3293 struct ovs_list packets;
abe529af 3294
417e7e66 3295 ovs_list_init(&packets);
509c0149 3296 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
abe529af 3297 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
9d078ec2 3298 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
8613db65
DDP
3299 pkt_node = xmalloc(sizeof *pkt_node);
3300 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3301 e->mac, e->vlan,
3302 (void **)&pkt_node->port);
417e7e66 3303 ovs_list_push_back(&packets, &pkt_node->list_node);
abe529af
BP
3304 }
3305 }
509c0149 3306 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af 3307
0165217e 3308 error = n_packets = n_errors = 0;
8613db65 3309 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
0165217e
EJ
3310 int ret;
3311
2eb79142 3312 ret = ofproto_dpif_send_packet(pkt_node->port, false, pkt_node->pkt);
8613db65
DDP
3313 dp_packet_delete(pkt_node->pkt);
3314 free(pkt_node);
0165217e
EJ
3315 if (ret) {
3316 error = ret;
3317 n_errors++;
3318 }
3319 n_packets++;
3320 }
0165217e 3321
abe529af 3322 if (n_errors) {
7ed58d4a
JP
3323 static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 5);
3324 VLOG_WARN_RL(&rll, "bond %s: %d errors sending %d gratuitous learning "
abe529af 3325 "packets, last error was: %s",
10a89ef0 3326 bundle->name, n_errors, n_packets, ovs_strerror(error));
abe529af
BP
3327 } else {
3328 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3329 bundle->name, n_packets);
3330 }
3331}
3332
3333static void
3334bundle_run(struct ofbundle *bundle)
3335{
3336 if (bundle->lacp) {
3337 lacp_run(bundle->lacp, send_pdu_cb);
3338 }
3339 if (bundle->bond) {
3340 struct ofport_dpif *port;
3341
3342 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
015e08bc 3343 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
abe529af
BP
3344 }
3345
4a1b8f30
EJ
3346 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3347 bundle->ofproto->backer->need_revalidate = REV_BOND;
3348 }
3349
abe529af
BP
3350 if (bond_should_send_learning_packets(bundle->bond)) {
3351 bundle_send_learning_packets(bundle);
3352 }
3353 }
3354}
3355
3356static void
3357bundle_wait(struct ofbundle *bundle)
3358{
3359 if (bundle->lacp) {
3360 lacp_wait(bundle->lacp);
3361 }
3362 if (bundle->bond) {
3363 bond_wait(bundle->bond);
3364 }
3365}
3366\f
3367/* Mirrors. */
3368
3369static int
ec7ceaed
EJ
3370mirror_set__(struct ofproto *ofproto_, void *aux,
3371 const struct ofproto_mirror_settings *s)
abe529af
BP
3372{
3373 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
ec7ceaed
EJ
3374 struct ofbundle **srcs, **dsts;
3375 int error;
3376 size_t i;
abe529af 3377
abe529af 3378 if (!s) {
ec7ceaed 3379 mirror_destroy(ofproto->mbridge, aux);
abe529af
BP
3380 return 0;
3381 }
3382
ec7ceaed
EJ
3383 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3384 dsts = xmalloc(s->n_dsts * sizeof *dsts);
abe529af 3385
ec7ceaed
EJ
3386 for (i = 0; i < s->n_srcs; i++) {
3387 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
abe529af
BP
3388 }
3389
ec7ceaed
EJ
3390 for (i = 0; i < s->n_dsts; i++) {
3391 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
abe529af
BP
3392 }
3393
ec7ceaed
EJ
3394 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3395 s->n_dsts, s->src_vlans,
1356dbd1
WT
3396 bundle_lookup(ofproto, s->out_bundle),
3397 s->snaplen, s->out_vlan);
ec7ceaed
EJ
3398 free(srcs);
3399 free(dsts);
3400 return error;
abe529af
BP
3401}
3402
9d24de3b 3403static int
ec7ceaed
EJ
3404mirror_get_stats__(struct ofproto *ofproto, void *aux,
3405 uint64_t *packets, uint64_t *bytes)
9d24de3b 3406{
ec7ceaed
EJ
3407 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3408 bytes);
9d24de3b
JP
3409}
3410
abe529af
BP
3411static int
3412set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3413{
3414 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 3415 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
abe529af 3416 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
30618594 3417 mac_learning_flush(ofproto->ml);
abe529af 3418 }
509c0149 3419 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af
BP
3420 return 0;
3421}
3422
3423static bool
b4affc74 3424is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
abe529af
BP
3425{
3426 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3427 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
ec7ceaed 3428 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
abe529af 3429}
8402c74b
SS
3430
3431static void
b53055f4 3432forward_bpdu_changed(struct ofproto *ofproto_)
8402c74b
SS
3433{
3434 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2cc3c58e 3435 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8402c74b 3436}
e764773c
BP
3437
3438static void
c4069512
BP
3439set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3440 size_t max_entries)
e764773c
BP
3441{
3442 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 3443 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
e764773c 3444 mac_learning_set_idle_time(ofproto->ml, idle_time);
c4069512 3445 mac_learning_set_max_entries(ofproto->ml, max_entries);
509c0149 3446 ovs_rwlock_unlock(&ofproto->ml->rwlock);
e764773c 3447}
7c38d0a5
FL
3448
3449/* Configures multicast snooping on 'ofport' using the settings
3450 * defined in 's'. */
3451static int
3452set_mcast_snooping(struct ofproto *ofproto_,
3453 const struct ofproto_mcast_snooping_settings *s)
3454{
3455 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3456
3457 /* Only revalidate flows if the configuration changed. */
3458 if (!s != !ofproto->ms) {
3459 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3460 }
3461
3462 if (s) {
3463 if (!ofproto->ms) {
3464 ofproto->ms = mcast_snooping_create();
3465 }
3466
3467 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3468 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3469 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3470 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3471 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3472 }
3473 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3474 } else {
3475 mcast_snooping_unref(ofproto->ms);
3476 ofproto->ms = NULL;
3477 }
3478
3479 return 0;
3480}
3481
8e04a33f 3482/* Configures multicast snooping port's flood settings on 'ofproto'. */
7c38d0a5 3483static int
8e04a33f
FL
3484set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3485 const struct ofproto_mcast_snooping_port_settings *s)
7c38d0a5
FL
3486{
3487 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3488 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3489
8e04a33f 3490 if (ofproto->ms && s) {
7c38d0a5 3491 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
8e04a33f
FL
3492 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3493 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3494 s->flood_reports);
7c38d0a5
FL
3495 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3496 }
3497 return 0;
3498}
3499
abe529af
BP
3500\f
3501/* Ports. */
3502
e672ff9b
JR
3503struct ofport_dpif *
3504ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
abe529af 3505{
7df6a8bd
BP
3506 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3507 return ofport ? ofport_dpif_cast(ofport) : NULL;
abe529af
BP
3508}
3509
abe529af 3510static void
e1b1d06a
JP
3511ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3512 struct ofproto_port *ofproto_port,
abe529af
BP
3513 struct dpif_port *dpif_port)
3514{
3515 ofproto_port->name = dpif_port->name;
3516 ofproto_port->type = dpif_port->type;
e1b1d06a 3517 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
abe529af
BP
3518}
3519
6cbbf4fa
EJ
3520static void
3521ofport_update_peer(struct ofport_dpif *ofport)
0a740f48
EJ
3522{
3523 const struct ofproto_dpif *ofproto;
6cbbf4fa 3524 struct dpif_backer *backer;
161b6042 3525 char *peer_name;
6cbbf4fa
EJ
3526
3527 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3528 return;
3529 }
3530
3531 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
7894e7da 3532 backer->need_revalidate = REV_RECONFIGURE;
0a740f48 3533
6cbbf4fa
EJ
3534 if (ofport->peer) {
3535 ofport->peer->peer = NULL;
3536 ofport->peer = NULL;
3537 }
3538
3539 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3540 if (!peer_name) {
3541 return;
0a740f48
EJ
3542 }
3543
911b4a7e
JP
3544 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_by_name_node,
3545 &all_ofproto_dpifs_by_name) {
6cbbf4fa
EJ
3546 struct ofport *peer_ofport;
3547 struct ofport_dpif *peer;
161b6042 3548 char *peer_peer;
0a740f48 3549
462abef2
EJ
3550 if (ofproto->backer != backer) {
3551 continue;
3552 }
3553
6cbbf4fa
EJ
3554 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3555 if (!peer_ofport) {
3556 continue;
3557 }
3558
3559 peer = ofport_dpif_cast(peer_ofport);
3560 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3561 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3562 peer_peer)) {
3563 ofport->peer = peer;
3564 ofport->peer->peer = ofport;
0a740f48 3565 }
161b6042 3566 free(peer_peer);
6cbbf4fa 3567
161b6042 3568 break;
0a740f48 3569 }
161b6042 3570 free(peer_name);
0a740f48
EJ
3571}
3572
abe529af
BP
3573static void
3574port_run(struct ofport_dpif *ofport)
3575{
3e5b3fdb
EJ
3576 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3577 bool carrier_changed = carrier_seq != ofport->carrier_seq;
015e08bc 3578 bool enable = netdev_get_carrier(ofport->up.netdev);
2d344ba5
AW
3579 bool cfm_enable = false;
3580 bool bfd_enable = false;
015e08bc 3581
3e5b3fdb
EJ
3582 ofport->carrier_seq = carrier_seq;
3583
abe529af 3584 if (ofport->cfm) {
4653c558
EJ
3585 int cfm_opup = cfm_get_opup(ofport->cfm);
3586
2d344ba5 3587 cfm_enable = !cfm_get_fault(ofport->cfm);
4653c558
EJ
3588
3589 if (cfm_opup >= 0) {
2d344ba5 3590 cfm_enable = cfm_enable && cfm_opup;
4653c558 3591 }
abe529af 3592 }
015e08bc 3593
ccc09689 3594 if (ofport->bfd) {
2d344ba5
AW
3595 bfd_enable = bfd_forwarding(ofport->bfd);
3596 }
3597
3598 if (ofport->bfd || ofport->cfm) {
3599 enable = enable && (cfm_enable || bfd_enable);
ccc09689
EJ
3600 }
3601
015e08bc
EJ
3602 if (ofport->bundle) {
3603 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3e5b3fdb
EJ
3604 if (carrier_changed) {
3605 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3606 }
015e08bc
EJ
3607 }
3608
daff3353
EJ
3609 if (ofport->may_enable != enable) {
3610 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
daff3353 3611
f025bcb7 3612 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
9efd308e 3613
f025bcb7 3614 if (ofport->rstp_port) {
9efd308e
DV
3615 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3616 }
f73b83fd
LS
3617
3618 /* Propagate liveness, unless the link is administratively or
3619 * operationally down. */
3620 if (!(ofport->up.pp.config & OFPUTIL_PC_PORT_DOWN) &&
3621 !(ofport->up.pp.state & OFPUTIL_PS_LINK_DOWN)) {
3622 enum ofputil_port_state of_state = ofport->up.pp.state;
3623 if (enable) {
3624 of_state |= OFPUTIL_PS_LIVE;
3625 } else {
3626 of_state &= ~OFPUTIL_PS_LIVE;
3627 }
3628 ofproto_port_set_state(&ofport->up, of_state);
3629 }
9efd308e 3630 }
f025bcb7
JR
3631
3632 ofport->may_enable = enable;
abe529af
BP
3633}
3634
abe529af
BP
3635static int
3636port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3637 struct ofproto_port *ofproto_port)
3638{
3639 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3640 struct dpif_port dpif_port;
3641 int error;
3642
0a740f48
EJ
3643 if (sset_contains(&ofproto->ghost_ports, devname)) {
3644 const char *type = netdev_get_type_from_name(devname);
3645
3646 /* We may be called before ofproto->up.port_by_name is populated with
3647 * the appropriate ofport. For this reason, we must get the name and
3648 * type from the netdev layer directly. */
3649 if (type) {
3650 const struct ofport *ofport;
3651
3652 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3653 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3654 ofproto_port->name = xstrdup(devname);
3655 ofproto_port->type = xstrdup(type);
3656 return 0;
3657 }
3658 return ENODEV;
3659 }
3660
acf60855
JP
3661 if (!sset_contains(&ofproto->ports, devname)) {
3662 return ENODEV;
3663 }
3664 error = dpif_port_query_by_name(ofproto->backer->dpif,
3665 devname, &dpif_port);
abe529af 3666 if (!error) {
e1b1d06a 3667 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
abe529af
BP
3668 }
3669 return error;
3670}
3671
3672static int
e1b1d06a 3673port_add(struct ofproto *ofproto_, struct netdev *netdev)
abe529af
BP
3674{
3675 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
b9ad7294 3676 const char *devname = netdev_get_name(netdev);
3aa30359
BP
3677 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3678 const char *dp_port_name;
abe529af 3679
0a740f48
EJ
3680 if (netdev_vport_is_patch(netdev)) {
3681 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3682 return 0;
3683 }
3684
3aa30359 3685 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
b9ad7294 3686 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
4e022ec0 3687 odp_port_t port_no = ODPP_NONE;
7d82ab2e
KM
3688 int error;
3689
3690 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
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
08fdcc12
FL
5250static void
5251ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
5252 int argc OVS_UNUSED,
5253 const char *argv[],
5254 void *aux OVS_UNUSED)
5255{
5256 struct ds ds = DS_EMPTY_INITIALIZER;
5257 const struct ofproto_dpif *ofproto;
5258 const struct ofbundle *bundle;
5259 const struct mcast_group *grp;
5260 struct mcast_group_bundle *b;
5261 struct mcast_mrouter_bundle *mrouter;
5262
911b4a7e 5263 ofproto = ofproto_dpif_lookup_by_name(argv[1]);
08fdcc12
FL
5264 if (!ofproto) {
5265 unixctl_command_reply_error(conn, "no such bridge");
5266 return;
5267 }
5268
5269 if (!mcast_snooping_enabled(ofproto->ms)) {
5270 unixctl_command_reply_error(conn, "multicast snooping is disabled");
5271 return;
5272 }
5273
5274 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
5275 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
5276 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
5277 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2f2b904f 5278 char name[OFP10_MAX_PORT_NAME_LEN];
08fdcc12
FL
5279
5280 bundle = b->port;
5281 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
50f96b10 5282 NULL, name, sizeof name);
964a4d5f 5283 ds_put_format(&ds, "%5s %4d ", name, grp->vlan);
ac6d120f 5284 ipv6_format_mapped(&grp->addr, &ds);
964a4d5f 5285 ds_put_format(&ds, " %3d\n",
08fdcc12
FL
5286 mcast_bundle_age(ofproto->ms, b));
5287 }
5288 }
5289
5290 /* ports connected to multicast routers */
5291 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
2f2b904f 5292 char name[OFP10_MAX_PORT_NAME_LEN];
08fdcc12
FL
5293
5294 bundle = mrouter->port;
5295 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
50f96b10 5296 NULL, name, sizeof name);
c4bb1f8b 5297 ds_put_format(&ds, "%5s %4d querier %3d\n",
08fdcc12
FL
5298 name, mrouter->vlan,
5299 mcast_mrouter_age(ofproto->ms, mrouter));
5300 }
5301 ovs_rwlock_unlock(&ofproto->ms->rwlock);
5302 unixctl_command_reply(conn, ds_cstr(&ds));
5303 ds_destroy(&ds);
5304}
5305
27022416
JP
5306/* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5307 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5308 * to destroy 'ofproto_shash' and free the returned value. */
5309static const struct shash_node **
5310get_ofprotos(struct shash *ofproto_shash)
5311{
5312 const struct ofproto_dpif *ofproto;
5313
911b4a7e
JP
5314 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_by_name_node,
5315 &all_ofproto_dpifs_by_name) {
27022416
JP
5316 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5317 shash_add_nocopy(ofproto_shash, name, ofproto);
5318 }
5319
5320 return shash_sort(ofproto_shash);
5321}
5322
5323static void
5324ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5325 const char *argv[] OVS_UNUSED,
5326 void *aux OVS_UNUSED)
5327{
5328 struct ds ds = DS_EMPTY_INITIALIZER;
5329 struct shash ofproto_shash;
5330 const struct shash_node **sorted_ofprotos;
5331 int i;
5332
5333 shash_init(&ofproto_shash);
5334 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5335 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5336 const struct shash_node *node = sorted_ofprotos[i];
5337 ds_put_format(&ds, "%s\n", node->name);
5338 }
5339
5340 shash_destroy(&ofproto_shash);
5341 free(sorted_ofprotos);
5342
5343 unixctl_command_reply(conn, ds_cstr(&ds));
5344 ds_destroy(&ds);
5345}
5346
a98b700d 5347static void
1c2f091b 5348show_dp_feature_bool(struct ds *ds, const char *feature, bool b)
a98b700d
AZ
5349{
5350 ds_put_format(ds, "%s: %s\n", feature, b ? "Yes" : "No");
5351}
5352
5353static void
1c2f091b 5354show_dp_feature_size_t(struct ds *ds, const char *feature, size_t s)
a98b700d
AZ
5355{
5356 ds_put_format(ds, "%s: %"PRIuSIZE"\n", feature, s);
5357}
5358
18e5d1a8
AZ
5359enum dpif_support_field_type {
5360 DPIF_SUPPORT_FIELD_bool,
5361 DPIF_SUPPORT_FIELD_size_t,
5362};
5363
5364struct dpif_support_field {
88186383
AZ
5365 void *rt_ptr; /* Points to the 'rt_support' field. */
5366 const void *bt_ptr; /* Points to the 'bt_support' field. */
18e5d1a8
AZ
5367 const char *title;
5368 enum dpif_support_field_type type;
5369};
5370
88186383
AZ
5371#define DPIF_SUPPORT_FIELD_INTIALIZER(RT_PTR, BT_PTR, TITLE, TYPE) \
5372 (struct dpif_support_field) {RT_PTR, BT_PTR, TITLE, TYPE}
18e5d1a8 5373
a98b700d
AZ
5374static void
5375dpif_show_support(const struct dpif_backer_support *support, struct ds *ds)
5376{
1c2f091b
AZ
5377#define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5378 show_dp_feature_##TYPE (ds, TITLE, support->NAME);
5379 DPIF_SUPPORT_FIELDS
5380#undef DPIF_SUPPORT_FIELD
5381
5382#define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5383 show_dp_feature_##TYPE (ds, TITLE, support->odp.NAME );
5384 ODP_SUPPORT_FIELDS
5385#undef ODP_SUPPORT_FIELD
a98b700d
AZ
5386}
5387
18e5d1a8
AZ
5388static void
5389display_support_field(const char *name,
5390 const struct dpif_support_field *field,
5391 struct ds *ds)
5392{
5393 switch (field->type) {
5394 case DPIF_SUPPORT_FIELD_bool: {
88186383
AZ
5395 bool v = *(bool *)field->rt_ptr;
5396 bool b = *(bool *)field->bt_ptr;
5397 ds_put_format(ds, "%s (%s) : [run time]:%s, [boot time]:%s\n", name,
5398 field->title, v ? "true" : "false",
5399 b ? "true" : "false");
18e5d1a8
AZ
5400 break;
5401 }
5402 case DPIF_SUPPORT_FIELD_size_t:
88186383
AZ
5403 ds_put_format(ds, "%s (%s) : [run time]:%"PRIuSIZE
5404 ", [boot time]:%"PRIuSIZE"\n", name,
5405 field->title, *(size_t *)field->rt_ptr,
5406 *(size_t *)field->bt_ptr);
18e5d1a8
AZ
5407 break;
5408 default:
5409 OVS_NOT_REACHED();
5410 }
5411}
5412
88186383
AZ
5413/* Set a field of 'rt_support' to a new value.
5414 *
5415 * Returns 'true' if the value is actually set. */
5416static bool
5417dpif_set_support(struct dpif_backer_support *rt_support,
5418 struct dpif_backer_support *bt_support,
18e5d1a8
AZ
5419 const char *name, const char *value, struct ds *ds)
5420{
5421 struct shash all_fields = SHASH_INITIALIZER(&all_fields);
5422 struct dpif_support_field *field;
5423 struct shash_node *node;
88186383 5424 bool changed = false;
18e5d1a8
AZ
5425
5426#define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5427 {\
5428 struct dpif_support_field *f = xmalloc(sizeof *f); \
88186383
AZ
5429 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->NAME, \
5430 &bt_support->NAME, \
5431 TITLE, \
5432 DPIF_SUPPORT_FIELD_##TYPE);\
18e5d1a8
AZ
5433 shash_add_once(&all_fields, #NAME, f); \
5434 }
5435 DPIF_SUPPORT_FIELDS;
5436#undef DPIF_SUPPORT_FIELD
5437
5438#define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5439 {\
5440 struct dpif_support_field *f = xmalloc(sizeof *f); \
88186383
AZ
5441 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->odp.NAME, \
5442 &bt_support->odp.NAME, \
5443 TITLE, \
5444 DPIF_SUPPORT_FIELD_##TYPE);\
18e5d1a8
AZ
5445 shash_add_once(&all_fields, #NAME, f); \
5446 }
5447 ODP_SUPPORT_FIELDS;
5448#undef ODP_SUPPORT_FIELD
5449
5450 if (!name) {
18e5d1a8
AZ
5451 SHASH_FOR_EACH (node, &all_fields) {
5452 display_support_field(node->name, node->data, ds);
5453 }
5454 goto done;
5455 }
5456
5457 node = shash_find(&all_fields, name);
5458 if (!node) {
5459 ds_put_cstr(ds, "Unexpected support field");
5460 goto done;
5461 }
5462 field = node->data;
5463
5464 if (!value) {
5465 display_support_field(node->name, field, ds);
5466 goto done;
5467 }
5468
5469 if (field->type == DPIF_SUPPORT_FIELD_bool) {
88186383
AZ
5470 if (!strcasecmp(value, "true")) {
5471 if (*(bool *)field->bt_ptr) {
5472 *(bool *)field->rt_ptr = true;
5473 changed = true;
5474 } else {
5475 ds_put_cstr(ds, "Can not enable features not supported by the datapth");
5476 }
5477 } else if (!strcasecmp(value, "false")) {
5478 *(bool *)field->rt_ptr = false;
5479 changed = true;
18e5d1a8
AZ
5480 } else {
5481 ds_put_cstr(ds, "Boolean value expected");
5482 }
5483 } else if (field->type == DPIF_SUPPORT_FIELD_size_t) {
5484 int v;
5485 if (str_to_int(value, 10, &v)) {
5486 if (v >= 0) {
88186383
AZ
5487 if (v <= *(size_t *)field->bt_ptr) {
5488 *(size_t *)field->rt_ptr = v;
5489 changed = true;
5490 } else {
5491 ds_put_cstr(ds, "Can not set value beyond the datapath capability");
5492 }
18e5d1a8
AZ
5493 } else {
5494 ds_put_format(ds, "Negative number not expected");
5495 }
5496 } else {
5497 ds_put_cstr(ds, "Integer number expected");
5498 }
5499 }
5500
5501done:
5502 shash_destroy_free_data(&all_fields);
88186383 5503 return changed;
18e5d1a8
AZ
5504}
5505
dc54ef36
EJ
5506static void
5507dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
27022416 5508{
dc54ef36 5509 const struct shash_node **ofprotos;
e79a6c83 5510 struct dpif_dp_stats dp_stats;
dc54ef36 5511 struct shash ofproto_shash;
09672174 5512 size_t i;
655ab909 5513
e79a6c83 5514 dpif_get_dp_stats(backer->dpif, &dp_stats);
dc54ef36 5515 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
e79a6c83 5516 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
dc54ef36 5517
dc54ef36
EJ
5518 shash_init(&ofproto_shash);
5519 ofprotos = get_ofprotos(&ofproto_shash);
5520 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5521 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5522 const struct shash_node **ports;
5523 size_t j;
0a740f48 5524
dc54ef36
EJ
5525 if (ofproto->backer != backer) {
5526 continue;
0a740f48 5527 }
27022416 5528
e79a6c83 5529 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
dc54ef36
EJ
5530
5531 ports = shash_sort(&ofproto->up.port_by_name);
5532 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5533 const struct shash_node *node = ports[j];
5534 struct ofport *ofport = node->data;
5535 struct smap config;
4e022ec0 5536 odp_port_t odp_port;
27022416 5537
dc54ef36
EJ
5538 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5539 ofport->ofp_port);
27022416 5540
dc54ef36 5541 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4e022ec0 5542 if (odp_port != ODPP_NONE) {
dc54ef36
EJ
5543 ds_put_format(ds, "%"PRIu32":", odp_port);
5544 } else {
5545 ds_put_cstr(ds, "none:");
5546 }
27022416 5547
dc54ef36 5548 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
27022416 5549
dc54ef36
EJ
5550 smap_init(&config);
5551 if (!netdev_get_config(ofport->netdev, &config)) {
71f21279
BP
5552 const struct smap_node **nodes = smap_sort(&config);
5553 for (size_t k = 0; k < smap_count(&config); k++) {
5554 ds_put_format(ds, "%c %s=%s", k ? ',' : ':',
5555 nodes[k]->key, nodes[k]->value);
dc54ef36
EJ
5556 }
5557 free(nodes);
27022416 5558 }
dc54ef36
EJ
5559 smap_destroy(&config);
5560
27022416 5561 ds_put_char(ds, ')');
dc54ef36 5562 ds_put_char(ds, '\n');
27022416 5563 }
dc54ef36 5564 free(ports);
27022416 5565 }
dc54ef36
EJ
5566 shash_destroy(&ofproto_shash);
5567 free(ofprotos);
27022416
JP
5568}
5569
5570static void
dc54ef36
EJ
5571ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5572 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
27022416
JP
5573{
5574 struct ds ds = DS_EMPTY_INITIALIZER;
dc54ef36
EJ
5575 const struct shash_node **backers;
5576 int i;
27022416 5577
dc54ef36
EJ
5578 backers = shash_sort(&all_dpif_backers);
5579 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5580 dpif_show_backer(backers[i]->data, &ds);
27022416 5581 }
dc54ef36 5582 free(backers);
27022416
JP
5583
5584 unixctl_command_reply(conn, ds_cstr(&ds));
5585 ds_destroy(&ds);
5586}
5587
5588static void
5589ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5590 int argc OVS_UNUSED, const char *argv[],
5591 void *aux OVS_UNUSED)
5592{
27022416 5593 const struct ofproto_dpif *ofproto;
ac64794a
BP
5594
5595 struct ds ds = DS_EMPTY_INITIALIZER;
ac64794a
BP
5596
5597 struct dpif_flow_dump *flow_dump;
5598 struct dpif_flow_dump_thread *flow_dump_thread;
5599 struct dpif_flow f;
938eaa50 5600 int error;
27022416 5601
911b4a7e 5602 ofproto = ofproto_dpif_lookup_by_name(argv[argc - 1]);
27022416
JP
5603 if (!ofproto) {
5604 unixctl_command_reply_error(conn, "no such bridge");
5605 return;
5606 }
5607
d1fd1ea9
BP
5608 bool verbosity = false;
5609 bool names = false;
5610 bool set_names = false;
5611 for (int i = 1; i < argc - 1; i++) {
5612 if (!strcmp(argv[i], "-m")) {
5613 verbosity = true;
5614 } else if (!strcmp(argv[i], "--names")) {
5615 names = true;
5616 set_names = true;
5617 } else if (!strcmp(argv[i], "--no-names")) {
5618 names = false;
5619 set_names = true;
5620 }
5621 }
5622 if (!set_names) {
5623 names = verbosity;
04b541df
GS
5624 }
5625
d1fd1ea9
BP
5626 struct hmap *portno_names = NULL;
5627 if (names) {
5628 portno_names = xmalloc(sizeof *portno_names);
5629 hmap_init(portno_names);
5630
5631 struct dpif_port dpif_port;
5632 struct dpif_port_dump port_dump;
5633 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5634 odp_portno_names_set(portno_names, dpif_port.port_no,
5635 dpif_port.name);
5636 }
04b541df
GS
5637 }
5638
1b849273 5639 ds_init(&ds);
7e8b7199 5640 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false, NULL);
ac64794a
BP
5641 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5642 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
ef377a58
JR
5643 struct flow flow;
5644
5645 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5646 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
04d08d54
EJ
5647 continue;
5648 }
5649
70e5ed6f
JS
5650 if (verbosity) {
5651 odp_format_ufid(&f.ufid, &ds);
5652 ds_put_cstr(&ds, " ");
5653 }
ac64794a 5654 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
d1fd1ea9 5655 portno_names, &ds, verbosity);
1b849273 5656 ds_put_cstr(&ds, ", ");
ac64794a 5657 dpif_flow_stats_format(&f.stats, &ds);
27022416 5658 ds_put_cstr(&ds, ", actions:");
0722f341 5659 format_odp_actions(&ds, f.actions, f.actions_len, portno_names);
27022416
JP
5660 ds_put_char(&ds, '\n');
5661 }
ac64794a
BP
5662 dpif_flow_dump_thread_destroy(flow_dump_thread);
5663 error = dpif_flow_dump_destroy(flow_dump);
27022416 5664
938eaa50 5665 if (error) {
1b849273
JS
5666 ds_clear(&ds);
5667 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5668 unixctl_command_reply_error(conn, ds_cstr(&ds));
5669 } else {
5670 unixctl_command_reply(conn, ds_cstr(&ds));
5671 }
d1fd1ea9
BP
5672 if (portno_names) {
5673 odp_portno_names_destroy(portno_names);
5674 hmap_destroy(portno_names);
5675 free(portno_names);
5676 }
27022416
JP
5677 ds_destroy(&ds);
5678}
5679
a98b700d
AZ
5680static void
5681ofproto_unixctl_dpif_show_dp_features(struct unixctl_conn *conn,
5682 int argc, const char *argv[],
5683 void *aux OVS_UNUSED)
5684{
5685 struct ds ds = DS_EMPTY_INITIALIZER;
5686 const char *br = argv[argc -1];
911b4a7e 5687 struct ofproto_dpif *ofproto = ofproto_dpif_lookup_by_name(br);
a98b700d
AZ
5688
5689 if (!ofproto) {
5690 unixctl_command_reply_error(conn, "no such bridge");
5691 return;
5692 }
5693
88186383 5694 dpif_show_support(&ofproto->backer->bt_support, &ds);
a98b700d
AZ
5695 unixctl_command_reply(conn, ds_cstr(&ds));
5696}
5697
18e5d1a8
AZ
5698static void
5699ofproto_unixctl_dpif_set_dp_features(struct unixctl_conn *conn,
5700 int argc, const char *argv[],
5701 void *aux OVS_UNUSED)
5702{
5703 struct ds ds = DS_EMPTY_INITIALIZER;
5704 const char *br = argv[1];
5705 const char *name, *value;
911b4a7e 5706 struct ofproto_dpif *ofproto = ofproto_dpif_lookup_by_name(br);
88186383 5707 bool changed;
18e5d1a8
AZ
5708
5709 if (!ofproto) {
5710 unixctl_command_reply_error(conn, "no such bridge");
5711 return;
5712 }
5713
5714 name = argc > 2 ? argv[2] : NULL;
5715 value = argc > 3 ? argv[3] : NULL;
88186383
AZ
5716 changed = dpif_set_support(&ofproto->backer->rt_support,
5717 &ofproto->backer->bt_support,
5718 name, value, &ds);
5719 if (changed) {
5720 xlate_set_support(ofproto, &ofproto->backer->rt_support);
5721 udpif_flush(ofproto->backer->udpif);
5722 }
18e5d1a8 5723 unixctl_command_reply(conn, ds_cstr(&ds));
88186383 5724 ds_destroy(&ds);
18e5d1a8
AZ
5725}
5726
a36de779
PS
5727static void
5728ofproto_unixctl_init(void)
abe529af
BP
5729{
5730 static bool registered;
5731 if (registered) {
5732 return;
5733 }
5734 registered = true;
5735
96e466a3 5736 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
0e15264f
BP
5737 ofproto_unixctl_fdb_flush, NULL);
5738 unixctl_command_register("fdb/show", "bridge", 1, 1,
5739 ofproto_unixctl_fdb_show, NULL);
08fdcc12
FL
5740 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5741 ofproto_unixctl_mcast_snooping_flush, NULL);
5742 unixctl_command_register("mdb/show", "bridge", 1, 1,
5743 ofproto_unixctl_mcast_snooping_show, NULL);
27022416
JP
5744 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5745 ofproto_unixctl_dpif_dump_dps, NULL);
dc54ef36
EJ
5746 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5747 NULL);
a98b700d
AZ
5748 unixctl_command_register("dpif/show-dp-features", "bridge", 1, 1,
5749 ofproto_unixctl_dpif_show_dp_features, NULL);
d1fd1ea9 5750 unixctl_command_register("dpif/dump-flows", "[-m] [--names | --no-nmaes] bridge", 1, INT_MAX,
27022416 5751 ofproto_unixctl_dpif_dump_flows, NULL);
18e5d1a8
AZ
5752 unixctl_command_register("dpif/set-dp-features", "bridge", 1, 3 ,
5753 ofproto_unixctl_dpif_set_dp_features, NULL);
abe529af
BP
5754}
5755\f
46c88433 5756static odp_port_t
4e022ec0 5757ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
e1b1d06a 5758{
e672ff9b 5759 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
4e022ec0 5760 return ofport ? ofport->odp_port : ODPP_NONE;
e1b1d06a
JP
5761}
5762
8449c4d6 5763struct ofport_dpif *
4e022ec0 5764odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
e1b1d06a
JP
5765{
5766 struct ofport_dpif *port;
5767
8449c4d6 5768 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
f9c0c3ec 5769 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
acf60855 5770 &backer->odp_to_ofport_map) {
e1b1d06a 5771 if (port->odp_port == odp_port) {
8449c4d6 5772 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855 5773 return port;
e1b1d06a
JP
5774 }
5775 }
5776
8449c4d6 5777 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855
JP
5778 return NULL;
5779}
5780
4e022ec0
AW
5781static ofp_port_t
5782odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
acf60855
JP
5783{
5784 struct ofport_dpif *port;
5785
5786 port = odp_port_to_ofport(ofproto->backer, odp_port);
6472ba11 5787 if (port && &ofproto->up == port->up.ofproto) {
acf60855
JP
5788 return port->up.ofp_port;
5789 } else {
5790 return OFPP_NONE;
5791 }
e1b1d06a 5792}
655ab909 5793
6a6b7060
BP
5794/* 'match' is non-const to allow for temporary modifications. Any changes are
5795 * restored before returning. */
adcf00ba
AZ
5796int
5797ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
6a6b7060 5798 struct match *match, int priority,
290ad78a 5799 uint16_t idle_timeout,
adcf00ba
AZ
5800 const struct ofpbuf *ofpacts,
5801 struct rule **rulep)
5802{
7338102b 5803 struct ofputil_flow_mod fm;
adcf00ba
AZ
5804 struct rule_dpif *rule;
5805 int error;
5806
7338102b 5807 fm = (struct ofputil_flow_mod) {
c184807c 5808 .buffer_id = UINT32_MAX,
39cc5c4a
BP
5809 .priority = priority,
5810 .table_id = TBL_INTERNAL,
5811 .command = OFPFC_ADD,
5812 .idle_timeout = idle_timeout,
5813 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5814 .ofpacts = ofpacts->data,
5815 .ofpacts_len = ofpacts->size,
39cc5c4a 5816 };
6a6b7060 5817 minimatch_init(&fm.match, match);
7338102b 5818 error = ofproto_flow_mod(&ofproto->up, &fm);
6a6b7060
BP
5819 minimatch_destroy(&fm.match);
5820
adcf00ba
AZ
5821 if (error) {
5822 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5823 ofperr_to_string(error));
5824 *rulep = NULL;
5825 return error;
5826 }
5827
621b8064
JR
5828 rule = rule_dpif_lookup_in_table(ofproto,
5829 ofproto_dpif_get_tables_version(ofproto),
6a6b7060 5830 TBL_INTERNAL, &match->flow, &match->wc);
adcf00ba 5831 if (rule) {
adcf00ba
AZ
5832 *rulep = &rule->up;
5833 } else {
5834 OVS_NOT_REACHED();
5835 }
5836 return 0;
5837}
5838
5839int
5840ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5841 struct match *match, int priority)
5842{
7338102b 5843 struct ofputil_flow_mod fm;
adcf00ba
AZ
5844 int error;
5845
7338102b 5846 fm = (struct ofputil_flow_mod) {
c184807c 5847 .buffer_id = UINT32_MAX,
39cc5c4a
BP
5848 .priority = priority,
5849 .table_id = TBL_INTERNAL,
02a9757e
HH
5850 .out_port = OFPP_ANY,
5851 .out_group = OFPG_ANY,
39cc5c4a
BP
5852 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5853 .command = OFPFC_DELETE_STRICT,
5854 };
6a6b7060 5855 minimatch_init(&fm.match, match);
7338102b 5856 error = ofproto_flow_mod(&ofproto->up, &fm);
6a6b7060
BP
5857 minimatch_destroy(&fm.match);
5858
adcf00ba
AZ
5859 if (error) {
5860 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5861 ofperr_to_string(error));
5862 return error;
5863 }
5864
5865 return 0;
5866}
5867
5dddf960
JR
5868static void
5869meter_get_features(const struct ofproto *ofproto_,
5870 struct ofputil_meter_features *features)
5871{
5872 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5873
5874 dpif_meter_get_features(ofproto->backer->dpif, features);
5875}
5876
5877static enum ofperr
5878meter_set(struct ofproto *ofproto_, ofproto_meter_id *meter_id,
5879 struct ofputil_meter_config *config)
5880{
5881 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5882
333ad77d
AZ
5883 /* Provider ID unknown. Use backer to allocate a new DP meter */
5884 if (meter_id->uint32 == UINT32_MAX) {
5885 if (!ofproto->backer->meter_ids) {
5886 return EFBIG; /* Datapath does not support meter. */
5887 }
5888
5889 if(!id_pool_alloc_id(ofproto->backer->meter_ids, &meter_id->uint32)) {
5890 return ENOMEM; /* Can't allocate a DP meter. */
5891 }
5892 }
5893
5dddf960
JR
5894 switch (dpif_meter_set(ofproto->backer->dpif, meter_id, config)) {
5895 case 0:
5896 return 0;
5897 case EFBIG: /* meter_id out of range */
5898 case ENOMEM: /* Cannot allocate meter */
5899 return OFPERR_OFPMMFC_OUT_OF_METERS;
5900 case EBADF: /* Unsupported flags */
5901 return OFPERR_OFPMMFC_BAD_FLAGS;
5902 case EINVAL: /* Too many bands */
5903 return OFPERR_OFPMMFC_OUT_OF_BANDS;
5904 case ENODEV: /* Unsupported band type */
5905 return OFPERR_OFPMMFC_BAD_BAND;
66a396d4 5906 case EDOM: /* Rate must be non-zero */
2029ce9a 5907 return OFPERR_OFPMMFC_BAD_RATE;
5dddf960
JR
5908 default:
5909 return OFPERR_OFPMMFC_UNKNOWN;
5910 }
5911}
5912
5913static enum ofperr
5914meter_get(const struct ofproto *ofproto_, ofproto_meter_id meter_id,
5915 struct ofputil_meter_stats *stats, uint16_t n_bands)
5916{
5917 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5918
5919 if (!dpif_meter_get(ofproto->backer->dpif, meter_id, stats, n_bands)) {
5920 return 0;
5921 }
5922 return OFPERR_OFPMMFC_UNKNOWN_METER;
5923}
5924
333ad77d
AZ
5925struct free_meter_id_args {
5926 struct ofproto_dpif *ofproto;
5927 ofproto_meter_id meter_id;
5928};
5929
5930static void
5931free_meter_id(struct free_meter_id_args *args)
5932{
5933 struct ofproto_dpif *ofproto = args->ofproto;
5934
5935 dpif_meter_del(ofproto->backer->dpif, args->meter_id, NULL, 0);
5936 id_pool_free_id(ofproto->backer->meter_ids, args->meter_id.uint32);
5937 free(args);
5938}
5939
5dddf960
JR
5940static void
5941meter_del(struct ofproto *ofproto_, ofproto_meter_id meter_id)
5942{
333ad77d
AZ
5943 struct free_meter_id_args *arg = xmalloc(sizeof *arg);
5944
5945 /* Before a meter can be deleted, Openflow spec requires all rules
5946 * referring to the meter to be (automatically) removed before the
5947 * meter is deleted. However, since vswitchd is multi-threaded,
5948 * those rules and their actions remain accessible by other threads,
5949 * especially by the handler and revalidator threads.
5950 * Postpone meter deletion after RCU grace period, so that ongoing
5951 * upcall translation or flow revalidation can complete. */
5952 arg->ofproto = ofproto_dpif_cast(ofproto_);
5953 arg->meter_id = meter_id;
5954 ovsrcu_postpone(free_meter_id, arg);
5dddf960
JR
5955}
5956
abe529af 5957const struct ofproto_class ofproto_dpif_class = {
b0408fca 5958 init,
abe529af
BP
5959 enumerate_types,
5960 enumerate_names,
5961 del,
0aeaabc8 5962 port_open_type,
acf60855 5963 type_run,
acf60855 5964 type_wait,
abe529af
BP
5965 alloc,
5966 construct,
5967 destruct,
5968 dealloc,
5969 run,
f07c97f5 5970 ofproto_dpif_wait,
e79a6c83 5971 NULL, /* get_memory_usage. */
1c030aa5 5972 type_get_memory_usage,
abe529af 5973 flush,
3c1bb396 5974 query_tables,
621b8064 5975 set_tables_version,
abe529af
BP
5976 port_alloc,
5977 port_construct,
5978 port_destruct,
5979 port_dealloc,
5980 port_modified,
5981 port_reconfigured,
5982 port_query_by_name,
5983 port_add,
5984 port_del,
91364d18 5985 port_set_config,
6527c598 5986 port_get_stats,
abe529af
BP
5987 port_dump_start,
5988 port_dump_next,
5989 port_dump_done,
5990 port_poll,
5991 port_poll_wait,
5992 port_is_lacp_current,
50b9699f 5993 port_get_lacp_stats,
0ab6decf 5994 NULL, /* rule_choose_table */
abe529af
BP
5995 rule_alloc,
5996 rule_construct,
8037acb4 5997 rule_insert,
1fc71871 5998 NULL, /* rule_delete */
abe529af
BP
5999 rule_destruct,
6000 rule_dealloc,
abe529af 6001 rule_get_stats,
1f4a8933 6002 packet_xlate,
6dd3c787 6003 packet_xlate_revert,
1f4a8933 6004 packet_execute,
7257b535 6005 set_frag_handling,
77ab5fd2 6006 nxt_resume,
abe529af
BP
6007 set_netflow,
6008 get_netflow_ids,
6009 set_sflow,
29089a54 6010 set_ipfix,
fb8f22c1 6011 get_ipfix_stats,
abe529af 6012 set_cfm,
8f5514fe 6013 cfm_status_changed,
9a9e3786 6014 get_cfm_status,
0477baa9
DF
6015 set_lldp,
6016 get_lldp_status,
6017 set_aa,
6018 aa_mapping_set,
6019 aa_mapping_unset,
6020 aa_vlan_get_queued,
6021 aa_vlan_get_queue_size,
ccc09689 6022 set_bfd,
8f5514fe 6023 bfd_status_changed,
ccc09689 6024 get_bfd_status,
21f7563c
JP
6025 set_stp,
6026 get_stp_status,
6027 set_stp_port,
6028 get_stp_port_status,
fd28ce3a 6029 get_stp_port_stats,
9efd308e
DV
6030 set_rstp,
6031 get_rstp_status,
6032 set_rstp_port,
6033 get_rstp_port_status,
8b36f51e 6034 set_queues,
abe529af
BP
6035 bundle_set,
6036 bundle_remove,
ec7ceaed
EJ
6037 mirror_set__,
6038 mirror_get_stats__,
abe529af
BP
6039 set_flood_vlans,
6040 is_mirror_output_bundle,
8402c74b 6041 forward_bpdu_changed,
c4069512 6042 set_mac_table_config,
7c38d0a5
FL
6043 set_mcast_snooping,
6044 set_mcast_snooping_port,
5dddf960
JR
6045 meter_get_features,
6046 meter_set,
6047 meter_get,
6048 meter_del,
00430a3f
SH
6049 group_alloc, /* group_alloc */
6050 group_construct, /* group_construct */
6051 group_destruct, /* group_destruct */
6052 group_dealloc, /* group_dealloc */
ccb3bc08 6053 NULL, /* group_modify */
00430a3f 6054 group_get_stats, /* group_get_stats */
b5cbbcf6 6055 get_datapath_version, /* get_datapath_version */
d4f6865c 6056 type_set_config,
2a7c4805 6057 ct_flush, /* ct_flush */
abe529af 6058};