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