]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif.c
rhel: add python-sphinx as a build dependency
[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);
7b91e3c8 1497 stp_unref(ofproto->stp);
1498 rstp_unref(ofproto->rstp);
abe529af 1499
acf60855 1500 sset_destroy(&ofproto->ports);
0a740f48 1501 sset_destroy(&ofproto->ghost_ports);
acf60855 1502 sset_destroy(&ofproto->port_poll_set);
e1b1d06a 1503
0da3c61b 1504 ovs_mutex_destroy(&ofproto->stats_mutex);
13501305 1505
a2b53dec 1506 seq_destroy(ofproto->ams_seq);
cfc50ae5 1507
acf60855 1508 close_dpif_backer(ofproto->backer);
abe529af
BP
1509}
1510
5fcc0d00
BP
1511static int
1512run(struct ofproto *ofproto_)
1513{
1514 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
e79a6c83 1515 uint64_t new_seq, new_dump_seq;
5fcc0d00 1516
ec7ceaed
EJ
1517 if (mbridge_need_revalidate(ofproto->mbridge)) {
1518 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 1519 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 1520 mac_learning_flush(ofproto->ml);
509c0149 1521 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 1522 mcast_snooping_mdb_flush(ofproto->ms);
ec7ceaed
EJ
1523 }
1524
a2b53dec 1525 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
b53d5c33 1526 * flow restore. Even though nothing is processed during flow restore,
a2b53dec 1527 * all queued 'ams' will be handled immediately when flow restore
b53d5c33 1528 * completes. */
a2b53dec 1529 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
b53d5c33 1530
a6b7506d 1531 /* Do not perform any periodic activity required by 'ofproto' while
40358701 1532 * waiting for flow restore to complete. */
a6b7506d 1533 if (!ofproto_get_flow_restore_wait()) {
a2b53dec
BP
1534 struct ofproto_async_msg *am;
1535 struct ovs_list ams;
1536
1537 guarded_list_pop_all(&ofproto->ams, &ams);
1538 LIST_FOR_EACH_POP (am, list_node, &ams) {
1539 connmgr_send_async_msg(ofproto->up.connmgr, am);
1540 ofproto_async_msg_free(am);
a6b7506d 1541 }
abe529af
BP
1542 }
1543
abe529af 1544 if (ofproto->netflow) {
8bfaca5b 1545 netflow_run(ofproto->netflow);
abe529af
BP
1546 }
1547 if (ofproto->sflow) {
bae473fe 1548 dpif_sflow_run(ofproto->sflow);
abe529af 1549 }
978427a5
RL
1550 if (ofproto->ipfix) {
1551 dpif_ipfix_run(ofproto->ipfix);
1552 }
abe529af 1553
f23d157c
JS
1554 new_seq = seq_read(connectivity_seq_get());
1555 if (ofproto->change_seq != new_seq) {
1556 struct ofport_dpif *ofport;
1557
1558 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1559 port_run(ofport);
1560 }
1561
1562 ofproto->change_seq = new_seq;
abe529af 1563 }
e2d13c43
JS
1564 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1565 struct ofbundle *bundle;
1566
1567 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1568 bundle_run(bundle);
1569 }
abe529af
BP
1570 }
1571
21f7563c 1572 stp_run(ofproto);
9efd308e 1573 rstp_run(ofproto);
509c0149 1574 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594
EJ
1575 if (mac_learning_run(ofproto->ml)) {
1576 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1577 }
509c0149 1578 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af 1579
6d95c4e8
FL
1580 if (mcast_snooping_run(ofproto->ms)) {
1581 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1582 }
1583
e79a6c83
EJ
1584 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1585 if (ofproto->dump_seq != new_dump_seq) {
1586 struct rule *rule, *next_rule;
f5857865 1587 long long now = time_msec();
e79a6c83
EJ
1588
1589 /* We know stats are relatively fresh, so now is a good time to do some
1590 * periodic work. */
1591 ofproto->dump_seq = new_dump_seq;
1592
1593 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1594 * has passed. */
1595 ovs_mutex_lock(&ofproto_mutex);
1596 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1597 &ofproto->up.expirable) {
f5857865 1598 rule_expire(rule_dpif_cast(rule), now);
e79a6c83
EJ
1599 }
1600 ovs_mutex_unlock(&ofproto_mutex);
1601
1602 /* All outstanding data in existing flows has been accounted, so it's a
1603 * good time to do bond rebalancing. */
60cda7d6 1604 if (ofproto->has_bonded_bundles) {
e79a6c83
EJ
1605 struct ofbundle *bundle;
1606
1607 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
60cda7d6
AZ
1608 if (bundle->bond) {
1609 bond_rebalance(bundle->bond);
e79a6c83
EJ
1610 }
1611 }
6814e51f
BP
1612 }
1613 }
abe529af
BP
1614 return 0;
1615}
1616
1617static void
f07c97f5 1618ofproto_dpif_wait(struct ofproto *ofproto_)
abe529af
BP
1619{
1620 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 1621
40358701
GS
1622 if (ofproto_get_flow_restore_wait()) {
1623 return;
1624 }
1625
abe529af 1626 if (ofproto->sflow) {
bae473fe 1627 dpif_sflow_wait(ofproto->sflow);
abe529af 1628 }
978427a5
RL
1629 if (ofproto->ipfix) {
1630 dpif_ipfix_wait(ofproto->ipfix);
1631 }
e2d13c43
JS
1632 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1633 struct ofbundle *bundle;
1634
1635 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1636 bundle_wait(bundle);
1637 }
abe529af 1638 }
6fca1ffb
BP
1639 if (ofproto->netflow) {
1640 netflow_wait(ofproto->netflow);
1641 }
509c0149 1642 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1c313b88 1643 mac_learning_wait(ofproto->ml);
509c0149 1644 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 1645 mcast_snooping_wait(ofproto->ms);
21f7563c 1646 stp_wait(ofproto);
2cc3c58e 1647 if (ofproto->backer->need_revalidate) {
abe529af 1648 poll_immediate_wake();
abe529af 1649 }
abe529af 1650
e79a6c83 1651 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
a2b53dec 1652 seq_wait(ofproto->ams_seq, ofproto->ams_seqno);
0d085684
BP
1653}
1654
1c030aa5
EJ
1655static void
1656type_get_memory_usage(const char *type, struct simap *usage)
1657{
1658 struct dpif_backer *backer;
1659
1660 backer = shash_find_data(&all_dpif_backers, type);
1661 if (backer) {
1662 udpif_get_memory_usage(backer->udpif, usage);
1663 }
1664}
1665
abe529af 1666static void
1b5b5071 1667flush(struct ofproto *ofproto_)
abe529af 1668{
1b5b5071
AZ
1669 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1670 struct dpif_backer *backer = ofproto->backer;
1671
1672 if (backer) {
1673 udpif_flush(backer->udpif);
1674 }
abe529af
BP
1675}
1676
6c1491fb 1677static void
3c1bb396
BP
1678query_tables(struct ofproto *ofproto,
1679 struct ofputil_table_features *features,
1680 struct ofputil_table_stats *stats)
6c1491fb 1681{
3c1bb396 1682 strcpy(features->name, "classifier");
6c1491fb 1683
3c1bb396
BP
1684 if (stats) {
1685 int i;
6c1491fb 1686
3c1bb396
BP
1687 for (i = 0; i < ofproto->n_tables; i++) {
1688 unsigned long missed, matched;
d611866c 1689
afcea9ea
JR
1690 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1691 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1692
3c1bb396 1693 stats[i].matched_count = matched;
3c1bb396
BP
1694 stats[i].lookup_count = matched + missed;
1695 }
d611866c 1696 }
6c1491fb
BP
1697}
1698
621b8064 1699static void
44e0c35d 1700set_tables_version(struct ofproto *ofproto_, ovs_version_t version)
621b8064
JR
1701{
1702 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1703
f9cf9e57
JR
1704 /* Use memory_order_release to signify that any prior memory accesses can
1705 * not be reordered to happen after this atomic store. This makes sure the
1706 * new version is properly set up when the readers can read this 'version'
1707 * value. */
1708 atomic_store_explicit(&ofproto->tables_version, version,
1709 memory_order_release);
1710 /* 'need_revalidate' can be reordered to happen before the atomic_store
1711 * above, but it does not matter as this variable is not accessed by other
1712 * threads. */
1fc71871 1713 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
621b8064
JR
1714}
1715
abe529af
BP
1716static struct ofport *
1717port_alloc(void)
1718{
3da29e32 1719 struct ofport_dpif *port = xzalloc(sizeof *port);
abe529af
BP
1720 return &port->up;
1721}
1722
1723static void
1724port_dealloc(struct ofport *port_)
1725{
1726 struct ofport_dpif *port = ofport_dpif_cast(port_);
1727 free(port);
1728}
1729
1730static int
1731port_construct(struct ofport *port_)
1732{
1733 struct ofport_dpif *port = ofport_dpif_cast(port_);
1734 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
b9ad7294 1735 const struct netdev *netdev = port->up.netdev;
3aa30359 1736 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
6d9e94b5 1737 const char *dp_port_name;
e1b1d06a
JP
1738 struct dpif_port dpif_port;
1739 int error;
abe529af 1740
2cc3c58e 1741 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1742 port->bundle = NULL;
1743 port->cfm = NULL;
ccc09689 1744 port->bfd = NULL;
0477baa9 1745 port->lldp = NULL;
f025bcb7 1746 port->may_enable = false;
21f7563c
JP
1747 port->stp_port = NULL;
1748 port->stp_state = STP_DISABLED;
9efd308e
DV
1749 port->rstp_port = NULL;
1750 port->rstp_state = RSTP_DISABLED;
42943cde 1751 port->is_tunnel = false;
6cbbf4fa 1752 port->peer = NULL;
55954f6e
EJ
1753 port->qdscp = NULL;
1754 port->n_qdscp = 0;
b9ad7294 1755 port->carrier_seq = netdev_get_carrier_resets(netdev);
a6363cfd 1756 port->is_layer3 = netdev_vport_is_layer3(netdev);
abe529af 1757
b9ad7294 1758 if (netdev_vport_is_patch(netdev)) {
743cea45 1759 /* By bailing out here, we don't submit the port to the sFlow module
185b4e3a
MG
1760 * to be considered for counter polling export. This is correct
1761 * because the patch port represents an interface that sFlow considers
1762 * to be "internal" to the switch as a whole, and therefore not a
1763 * candidate for counter polling. */
4e022ec0 1764 port->odp_port = ODPP_NONE;
6cbbf4fa 1765 ofport_update_peer(port);
0a740f48
EJ
1766 return 0;
1767 }
1768
6d9e94b5
AZ
1769 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1770 error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
e1b1d06a
JP
1771 &dpif_port);
1772 if (error) {
1773 return error;
1774 }
1775
1776 port->odp_port = dpif_port.port_no;
1777
b9ad7294 1778 if (netdev_get_tunnel_config(netdev)) {
a36de779 1779 atomic_count_inc(&ofproto->backer->tnl_count);
ea0797c9 1780 error = tnl_port_add(port, port->up.netdev, port->odp_port,
6d9e94b5 1781 ovs_native_tunneling_is_on(ofproto), dp_port_name);
ea0797c9
BP
1782 if (error) {
1783 atomic_count_dec(&ofproto->backer->tnl_count);
1784 dpif_port_destroy(&dpif_port);
1785 return error;
1786 }
1787
42943cde 1788 port->is_tunnel = true;
8b7ea2d4
WZ
1789 if (ofproto->ipfix) {
1790 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1791 }
b9ad7294
EJ
1792 } else {
1793 /* Sanity-check that a mapping doesn't already exist. This
1794 * shouldn't happen for non-tunnel ports. */
1795 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1796 VLOG_ERR("port %s already has an OpenFlow port number",
1797 dpif_port.name);
da78d43d 1798 dpif_port_destroy(&dpif_port);
b9ad7294
EJ
1799 return EBUSY;
1800 }
e1b1d06a 1801
8449c4d6 1802 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1803 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
f9c0c3ec 1804 hash_odp_port(port->odp_port));
8449c4d6 1805 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1806 }
da78d43d 1807 dpif_port_destroy(&dpif_port);
e1b1d06a 1808
abe529af 1809 if (ofproto->sflow) {
e1b1d06a 1810 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
abe529af
BP
1811 }
1812
1813 return 0;
1814}
1815
1816static void
9aad5a5a 1817port_destruct(struct ofport *port_, bool del)
abe529af
BP
1818{
1819 struct ofport_dpif *port = ofport_dpif_cast(port_);
1820 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
02f8d646 1821 const char *devname = netdev_get_name(port->up.netdev);
3aa30359
BP
1822 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1823 const char *dp_port_name;
abe529af 1824
7894e7da 1825 ofproto->backer->need_revalidate = REV_RECONFIGURE;
84f0f298 1826 xlate_txn_start();
46c88433 1827 xlate_ofport_remove(port);
84f0f298 1828 xlate_txn_commit();
7894e7da 1829
3aa30359
BP
1830 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1831 sizeof namebuf);
9aad5a5a 1832 if (del && dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
acf60855
JP
1833 /* The underlying device is still there, so delete it. This
1834 * happens when the ofproto is being destroyed, since the caller
1835 * assumes that removal of attached ports will happen as part of
1836 * destruction. */
42943cde 1837 if (!port->is_tunnel) {
a614d823
KM
1838 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1839 }
acf60855
JP
1840 }
1841
6cbbf4fa
EJ
1842 if (port->peer) {
1843 port->peer->peer = NULL;
1844 port->peer = NULL;
1845 }
1846
42943cde 1847 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
8449c4d6 1848 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48 1849 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
8449c4d6 1850 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48
EJ
1851 }
1852
a36de779
PS
1853 if (port->is_tunnel) {
1854 atomic_count_dec(&ofproto->backer->tnl_count);
1855 }
1856
8b7ea2d4
WZ
1857 if (port->is_tunnel && ofproto->ipfix) {
1858 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1859 }
1860
42943cde 1861 tnl_port_del(port);
02f8d646 1862 sset_find_and_delete(&ofproto->ports, devname);
0a740f48 1863 sset_find_and_delete(&ofproto->ghost_ports, devname);
abe529af 1864 bundle_remove(port_);
a5610457 1865 set_cfm(port_, NULL);
8aee94b6 1866 set_bfd(port_, NULL);
0477baa9 1867 set_lldp(port_, NULL);
0c0c32d8
BP
1868 if (port->stp_port) {
1869 stp_port_disable(port->stp_port);
1870 }
f025bcb7 1871 set_rstp_port(port_, NULL);
abe529af 1872 if (ofproto->sflow) {
bae473fe 1873 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
abe529af 1874 }
8b36f51e 1875
55954f6e 1876 free(port->qdscp);
abe529af
BP
1877}
1878
1879static void
1880port_modified(struct ofport *port_)
1881{
1882 struct ofport_dpif *port = ofport_dpif_cast(port_);
a36de779 1883 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
6d9e94b5 1884 const char *dp_port_name;
a36de779 1885 struct netdev *netdev = port->up.netdev;
abe529af
BP
1886
1887 if (port->bundle && port->bundle->bond) {
a36de779 1888 bond_slave_set_netdev(port->bundle->bond, port, netdev);
abe529af 1889 }
9d46b444
EJ
1890
1891 if (port->cfm) {
a36de779 1892 cfm_set_netdev(port->cfm, netdev);
9d46b444 1893 }
f1e78bbd 1894
c1c4e8c7 1895 if (port->bfd) {
a36de779 1896 bfd_set_netdev(port->bfd, netdev);
c1c4e8c7
AW
1897 }
1898
f73b83fd
LS
1899 /* Set liveness, unless the link is administratively or
1900 * operationally down or link monitoring false */
1901 if (!(port->up.pp.config & OFPUTIL_PC_PORT_DOWN) &&
1902 !(port->up.pp.state & OFPUTIL_PS_LINK_DOWN) &&
1903 port->may_enable) {
1904 port->up.pp.state |= OFPUTIL_PS_LIVE;
1905 } else {
1906 port->up.pp.state &= ~OFPUTIL_PS_LIVE;
1907 }
1908
635c5db9 1909 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
74ff3298 1910 port->lldp, &port->up.pp.hw_addr);
635c5db9 1911
6d9e94b5 1912 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
a36de779
PS
1913
1914 if (port->is_tunnel) {
1915 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1916
1917 if (tnl_port_reconfigure(port, netdev, port->odp_port,
6d9e94b5
AZ
1918 ovs_native_tunneling_is_on(ofproto),
1919 dp_port_name)) {
a36de779
PS
1920 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1921 }
f1e78bbd 1922 }
6cbbf4fa
EJ
1923
1924 ofport_update_peer(port);
abe529af
BP
1925}
1926
1927static void
9e1fd49b 1928port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
abe529af
BP
1929{
1930 struct ofport_dpif *port = ofport_dpif_cast(port_);
1931 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
9e1fd49b 1932 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
abe529af 1933
9e1fd49b 1934 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
c57b2226
BP
1935 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1936 OFPUTIL_PC_NO_PACKET_IN)) {
2cc3c58e 1937 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7bde8dd8 1938
9e1fd49b 1939 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
7bde8dd8
JP
1940 bundle_update(port->bundle);
1941 }
abe529af
BP
1942 }
1943}
1944
1945static int
1946set_sflow(struct ofproto *ofproto_,
1947 const struct ofproto_sflow_options *sflow_options)
1948{
1949 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bae473fe 1950 struct dpif_sflow *ds = ofproto->sflow;
6ff686f2 1951
abe529af 1952 if (sflow_options) {
4fe0f203 1953 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
bae473fe 1954 if (!ds) {
abe529af
BP
1955 struct ofport_dpif *ofport;
1956
4213f19d 1957 ds = ofproto->sflow = dpif_sflow_create();
abe529af 1958 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
e1b1d06a 1959 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
abe529af
BP
1960 }
1961 }
bae473fe 1962 dpif_sflow_set_options(ds, sflow_options);
4fe0f203
BP
1963 if (dpif_sflow_get_probability(ds) != old_probability) {
1964 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1965 }
abe529af 1966 } else {
6ff686f2 1967 if (ds) {
9723bcce 1968 dpif_sflow_unref(ds);
2cc3c58e 1969 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6ff686f2
PS
1970 ofproto->sflow = NULL;
1971 }
abe529af
BP
1972 }
1973 return 0;
1974}
1975
29089a54
RL
1976static int
1977set_ipfix(
1978 struct ofproto *ofproto_,
1979 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1980 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1981 size_t n_flow_exporters_options)
1982{
1983 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1984 struct dpif_ipfix *di = ofproto->ipfix;
978427a5 1985 bool has_options = bridge_exporter_options || flow_exporters_options;
8b7ea2d4 1986 bool new_di = false;
29089a54 1987
978427a5
RL
1988 if (has_options && !di) {
1989 di = ofproto->ipfix = dpif_ipfix_create();
8b7ea2d4 1990 new_di = true;
978427a5
RL
1991 }
1992
1993 if (di) {
1994 /* Call set_options in any case to cleanly flush the flow
1995 * caches in the last exporters that are to be destroyed. */
29089a54
RL
1996 dpif_ipfix_set_options(
1997 di, bridge_exporter_options, flow_exporters_options,
1998 n_flow_exporters_options);
978427a5 1999
8b7ea2d4
WZ
2000 /* Add tunnel ports only when a new ipfix created */
2001 if (new_di == true) {
2002 struct ofport_dpif *ofport;
2003 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2004 if (ofport->is_tunnel == true) {
2005 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
2006 }
2007 }
2008 }
2009
978427a5 2010 if (!has_options) {
d857c8aa 2011 dpif_ipfix_unref(di);
29089a54
RL
2012 ofproto->ipfix = NULL;
2013 }
2014 }
978427a5 2015
29089a54
RL
2016 return 0;
2017}
2018
fb8f22c1
BY
2019static int
2020get_ipfix_stats(const struct ofproto *ofproto_,
2021 bool bridge_ipfix,
2022 struct ovs_list *replies)
2023{
2024 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2025 struct dpif_ipfix *di = ofproto->ipfix;
2026
2027 if (!di) {
2028 return OFPERR_NXST_NOT_CONFIGURED;
2029 }
2030
2031 return dpif_ipfix_get_stats(di, bridge_ipfix, replies);
2032}
2033
abe529af 2034static int
a5610457 2035set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
abe529af
BP
2036{
2037 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4e5e44e3
AW
2038 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2039 struct cfm *old = ofport->cfm;
635c5db9 2040 int error = 0;
abe529af 2041
635c5db9 2042 if (s) {
abe529af 2043 if (!ofport->cfm) {
90967e95 2044 ofport->cfm = cfm_create(ofport->up.netdev);
abe529af
BP
2045 }
2046
a5610457 2047 if (cfm_configure(ofport->cfm, s)) {
635c5db9
AW
2048 error = 0;
2049 goto out;
abe529af
BP
2050 }
2051
2052 error = EINVAL;
2053 }
8e9a73fa 2054 cfm_unref(ofport->cfm);
abe529af 2055 ofport->cfm = NULL;
635c5db9 2056out:
4e5e44e3
AW
2057 if (ofport->cfm != old) {
2058 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2059 }
635c5db9 2060 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
74ff3298 2061 ofport->lldp, &ofport->up.pp.hw_addr);
abe529af
BP
2062 return error;
2063}
2064
8f5514fe
AW
2065static bool
2066cfm_status_changed(struct ofport *ofport_)
2067{
2068 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2069
2070 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2071}
2072
88bf179a 2073static int
9a9e3786 2074get_cfm_status(const struct ofport *ofport_,
685acfd9 2075 struct cfm_status *status)
1de11730
EJ
2076{
2077 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
88bf179a 2078 int ret = 0;
1de11730
EJ
2079
2080 if (ofport->cfm) {
8f5514fe 2081 cfm_get_status(ofport->cfm, status);
1de11730 2082 } else {
88bf179a 2083 ret = ENOENT;
1de11730 2084 }
88bf179a
AW
2085
2086 return ret;
1de11730 2087}
ccc09689
EJ
2088
2089static int
2090set_bfd(struct ofport *ofport_, const struct smap *cfg)
2091{
2092 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2093 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2094 struct bfd *old;
2095
2096 old = ofport->bfd;
c1c4e8c7
AW
2097 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2098 cfg, ofport->up.netdev);
ccc09689
EJ
2099 if (ofport->bfd != old) {
2100 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2101 }
635c5db9 2102 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
74ff3298 2103 ofport->lldp, &ofport->up.pp.hw_addr);
ccc09689
EJ
2104 return 0;
2105}
2106
8f5514fe
AW
2107static bool
2108bfd_status_changed(struct ofport *ofport_)
2109{
2110 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2111
2112 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2113}
2114
ccc09689
EJ
2115static int
2116get_bfd_status(struct ofport *ofport_, struct smap *smap)
2117{
2118 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
88bf179a 2119 int ret = 0;
ccc09689
EJ
2120
2121 if (ofport->bfd) {
8f5514fe 2122 bfd_get_status(ofport->bfd, smap);
ccc09689 2123 } else {
88bf179a 2124 ret = ENOENT;
ccc09689 2125 }
88bf179a
AW
2126
2127 return ret;
ccc09689 2128}
0477baa9
DF
2129
2130static int
2131set_lldp(struct ofport *ofport_,
2132 const struct smap *cfg)
2133{
2134 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2135 int error = 0;
2136
2137 if (cfg) {
2138 if (!ofport->lldp) {
2139 struct ofproto_dpif *ofproto;
2140
2141 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2142 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2143 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2144 }
2145
19aef6ef
DF
2146 if (!lldp_configure(ofport->lldp, cfg)) {
2147 error = EINVAL;
0477baa9 2148 }
0477baa9 2149 }
19aef6ef
DF
2150 if (error) {
2151 lldp_unref(ofport->lldp);
2152 ofport->lldp = NULL;
2153 }
2154
0477baa9
DF
2155 ofproto_dpif_monitor_port_update(ofport,
2156 ofport->bfd,
2157 ofport->cfm,
2158 ofport->lldp,
74ff3298 2159 &ofport->up.pp.hw_addr);
0477baa9
DF
2160 return error;
2161}
2162
2163static bool
2164get_lldp_status(const struct ofport *ofport_,
2165 struct lldp_status *status OVS_UNUSED)
2166{
2167 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2168
2169 return ofport->lldp ? true : false;
2170}
2171
2172static int
2173set_aa(struct ofproto *ofproto OVS_UNUSED,
2174 const struct aa_settings *s)
2175{
2176 return aa_configure(s);
2177}
2178
2179static int
2180aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2181 const struct aa_mapping_settings *s)
2182{
2183 return aa_mapping_register(aux, s);
2184}
2185
2186static int
2187aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2188{
2189 return aa_mapping_unregister(aux);
2190}
2191
2192static int
2193aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2194{
2195 return aa_get_vlan_queued(list);
2196}
2197
2198static unsigned int
2199aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2200{
2201 return aa_get_vlan_queue_size();
2202}
2203
abe529af 2204\f
21f7563c
JP
2205/* Spanning Tree. */
2206
6b90bc57 2207/* Called while rstp_mutex is held. */
9efd308e 2208static void
cf62fa4c 2209rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
9efd308e
DV
2210{
2211 struct ofproto_dpif *ofproto = ofproto_;
6b90bc57 2212 struct ofport_dpif *ofport = ofport_;
cf62fa4c 2213 struct eth_header *eth = dp_packet_l2(pkt);
6b90bc57 2214
74ff3298 2215 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
6b90bc57
JR
2216 if (eth_addr_is_zero(eth->eth_src)) {
2217 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2218 "does not have a configured source MAC address.",
2219 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
9efd308e 2220 } else {
2eb79142 2221 ofproto_dpif_send_packet(ofport, false, pkt);
9efd308e 2222 }
cf62fa4c 2223 dp_packet_delete(pkt);
9efd308e
DV
2224}
2225
21f7563c 2226static void
cf62fa4c 2227send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
21f7563c
JP
2228{
2229 struct ofproto_dpif *ofproto = ofproto_;
2230 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2231 struct ofport_dpif *ofport;
2232
2233 ofport = stp_port_get_aux(sp);
2234 if (!ofport) {
2235 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2236 ofproto->up.name, port_num);
2237 } else {
cf62fa4c 2238 struct eth_header *eth = dp_packet_l2(pkt);
21f7563c 2239
74ff3298 2240 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
21f7563c
JP
2241 if (eth_addr_is_zero(eth->eth_src)) {
2242 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2243 "with unknown MAC", ofproto->up.name, port_num);
2244 } else {
2eb79142 2245 ofproto_dpif_send_packet(ofport, false, pkt);
21f7563c
JP
2246 }
2247 }
cf62fa4c 2248 dp_packet_delete(pkt);
21f7563c
JP
2249}
2250
9efd308e
DV
2251/* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2252static void
2253set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2254{
2255 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2256
2257 /* Only revalidate flows if the configuration changed. */
2258 if (!s != !ofproto->rstp) {
2259 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2260 }
2261
2262 if (s) {
2263 if (!ofproto->rstp) {
2264 ofproto->rstp = rstp_create(ofproto_->name, s->address,
4b30ba05 2265 rstp_send_bpdu_cb, ofproto);
9efd308e
DV
2266 ofproto->rstp_last_tick = time_msec();
2267 }
2268 rstp_set_bridge_address(ofproto->rstp, s->address);
2269 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2270 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2271 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2272 s->force_protocol_version);
2273 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2274 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2275 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2276 s->transmit_hold_count);
2277 } else {
2278 struct ofport *ofport;
2279 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2280 set_rstp_port(ofport, NULL);
2281 }
2282 rstp_unref(ofproto->rstp);
2283 ofproto->rstp = NULL;
2284 }
2285}
2286
2287static void
2288get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2289{
2290 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2291
2292 if (ofproto->rstp) {
2293 s->enabled = true;
2294 s->root_id = rstp_get_root_id(ofproto->rstp);
2295 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2296 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2297 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2298 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2299 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2300 } else {
2301 s->enabled = false;
2302 }
2303}
2304
2305static void
2306update_rstp_port_state(struct ofport_dpif *ofport)
2307{
2308 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2309 enum rstp_state state;
2310
2311 /* Figure out new state. */
2312 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2313 : RSTP_DISABLED;
2314
2315 /* Update state. */
2316 if (ofport->rstp_state != state) {
2317 enum ofputil_port_state of_state;
2318 bool fwd_change;
2319
9a0bb428
JR
2320 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2321 netdev_get_name(ofport->up.netdev),
2322 rstp_state_name(ofport->rstp_state),
2323 rstp_state_name(state));
2372c146 2324
9efd308e 2325 if (rstp_learn_in_state(ofport->rstp_state)
66ff280d
JR
2326 != rstp_learn_in_state(state)) {
2327 /* XXX: Learning action flows should also be flushed. */
2328 if (ofport->bundle) {
2372c146
JR
2329 if (!rstp_shift_root_learned_address(ofproto->rstp)
2330 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2331 bundle_flush_macs(ofport->bundle, false);
2332 }
66ff280d 2333 }
9efd308e
DV
2334 }
2335 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2336 != rstp_forward_in_state(state);
2337
2338 ofproto->backer->need_revalidate = REV_RSTP;
2339 ofport->rstp_state = state;
2340
2341 if (fwd_change && ofport->bundle) {
2342 bundle_update(ofport->bundle);
2343 }
2344
2345 /* Update the RSTP state bits in the OpenFlow port description. */
2346 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2347 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2348 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2349 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2350 : 0);
2351 ofproto_port_set_state(&ofport->up, of_state);
2352 }
2353}
2354
2355static void
2356rstp_run(struct ofproto_dpif *ofproto)
2357{
2358 if (ofproto->rstp) {
2359 long long int now = time_msec();
2360 long long int elapsed = now - ofproto->rstp_last_tick;
2361 struct rstp_port *rp;
f025bcb7 2362 struct ofport_dpif *ofport;
54d3863b 2363
9efd308e
DV
2364 /* Every second, decrease the values of the timers. */
2365 if (elapsed >= 1000) {
2366 rstp_tick_timers(ofproto->rstp);
2367 ofproto->rstp_last_tick = now;
2368 }
f025bcb7
JR
2369 rp = NULL;
2370 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2371 update_rstp_port_state(ofport);
9efd308e 2372 }
66ff280d
JR
2373 rp = NULL;
2374 ofport = NULL;
9efd308e
DV
2375 /* FIXME: This check should be done on-event (i.e., when setting
2376 * p->fdb_flush) and not periodically.
2377 */
66ff280d 2378 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2372c146
JR
2379 if (!rstp_shift_root_learned_address(ofproto->rstp)
2380 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2381 bundle_flush_macs(ofport->bundle, false);
2382 }
2383 }
2384
2385 if (rstp_shift_root_learned_address(ofproto->rstp)) {
c7e4f0b2
DV
2386 struct ofport_dpif *old_root_aux =
2387 (struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp);
2388 struct ofport_dpif *new_root_aux =
2389 (struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp);
2390 if (old_root_aux != NULL && new_root_aux != NULL) {
2391 bundle_move(old_root_aux->bundle, new_root_aux->bundle);
2392 rstp_reset_root_changed(ofproto->rstp);
2393 }
9efd308e
DV
2394 }
2395 }
2396}
2397
21f7563c
JP
2398/* Configures STP on 'ofproto_' using the settings defined in 's'. */
2399static int
2400set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2401{
2402 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2403
2404 /* Only revalidate flows if the configuration changed. */
2405 if (!s != !ofproto->stp) {
2cc3c58e 2406 ofproto->backer->need_revalidate = REV_RECONFIGURE;
21f7563c
JP
2407 }
2408
2409 if (s) {
2410 if (!ofproto->stp) {
2411 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2412 send_bpdu_cb, ofproto);
2413 ofproto->stp_last_tick = time_msec();
2414 }
2415
2416 stp_set_bridge_id(ofproto->stp, s->system_id);
2417 stp_set_bridge_priority(ofproto->stp, s->priority);
2418 stp_set_hello_time(ofproto->stp, s->hello_time);
2419 stp_set_max_age(ofproto->stp, s->max_age);
2420 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2421 } else {
851bf71d
EJ
2422 struct ofport *ofport;
2423
2424 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2425 set_stp_port(ofport, NULL);
2426 }
2427
bd54dbcd 2428 stp_unref(ofproto->stp);
21f7563c
JP
2429 ofproto->stp = NULL;
2430 }
2431
2432 return 0;
2433}
2434
2435static int
2436get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2437{
2438 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2439
2440 if (ofproto->stp) {
2441 s->enabled = true;
2442 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2443 s->designated_root = stp_get_designated_root(ofproto->stp);
2444 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2445 } else {
2446 s->enabled = false;
2447 }
2448
2449 return 0;
2450}
2451
2452static void
2453update_stp_port_state(struct ofport_dpif *ofport)
2454{
2455 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2456 enum stp_state state;
2457
2458 /* Figure out new state. */
2459 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2460 : STP_DISABLED;
2461
2462 /* Update state. */
2463 if (ofport->stp_state != state) {
9e1fd49b 2464 enum ofputil_port_state of_state;
21f7563c
JP
2465 bool fwd_change;
2466
9a0bb428
JR
2467 VLOG_DBG("port %s: STP state changed from %s to %s",
2468 netdev_get_name(ofport->up.netdev),
2469 stp_state_name(ofport->stp_state),
2470 stp_state_name(state));
21f7563c
JP
2471 if (stp_learn_in_state(ofport->stp_state)
2472 != stp_learn_in_state(state)) {
2473 /* xxx Learning action flows should also be flushed. */
509c0149 2474 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 2475 mac_learning_flush(ofproto->ml);
509c0149 2476 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 2477 mcast_snooping_mdb_flush(ofproto->ms);
21f7563c
JP
2478 }
2479 fwd_change = stp_forward_in_state(ofport->stp_state)
2480 != stp_forward_in_state(state);
2481
2cc3c58e 2482 ofproto->backer->need_revalidate = REV_STP;
21f7563c
JP
2483 ofport->stp_state = state;
2484 ofport->stp_state_entered = time_msec();
2485
b308140a 2486 if (fwd_change && ofport->bundle) {
21f7563c
JP
2487 bundle_update(ofport->bundle);
2488 }
2489
2490 /* Update the STP state bits in the OpenFlow port description. */
9e1fd49b
BP
2491 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2492 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2493 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2494 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2495 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2496 : 0);
21f7563c
JP
2497 ofproto_port_set_state(&ofport->up, of_state);
2498 }
2499}
2500
2501/* Configures STP on 'ofport_' using the settings defined in 's'. The
2502 * caller is responsible for assigning STP port numbers and ensuring
2503 * there are no duplicates. */
2504static int
2505set_stp_port(struct ofport *ofport_,
2506 const struct ofproto_port_stp_settings *s)
2507{
2508 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2509 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2510 struct stp_port *sp = ofport->stp_port;
2511
2512 if (!s || !s->enable) {
2513 if (sp) {
2514 ofport->stp_port = NULL;
2515 stp_port_disable(sp);
ecd12731 2516 update_stp_port_state(ofport);
21f7563c
JP
2517 }
2518 return 0;
2519 } else if (sp && stp_port_no(sp) != s->port_num
f025bcb7 2520 && ofport == stp_port_get_aux(sp)) {
21f7563c
JP
2521 /* The port-id changed, so disable the old one if it's not
2522 * already in use by another port. */
2523 stp_port_disable(sp);
2524 }
2525
2526 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
21f7563c 2527
fb20b0bf
JR
2528 /* Set name before enabling the port so that debugging messages can print
2529 * the name. */
11306274 2530 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
fb20b0bf
JR
2531 stp_port_enable(sp);
2532
21f7563c
JP
2533 stp_port_set_aux(sp, ofport);
2534 stp_port_set_priority(sp, s->priority);
2535 stp_port_set_path_cost(sp, s->path_cost);
2536
2537 update_stp_port_state(ofport);
2538
2539 return 0;
2540}
2541
2542static int
2543get_stp_port_status(struct ofport *ofport_,
2544 struct ofproto_port_stp_status *s)
2545{
2546 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2547 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2548 struct stp_port *sp = ofport->stp_port;
2549
2550 if (!ofproto->stp || !sp) {
2551 s->enabled = false;
2552 return 0;
2553 }
2554
2555 s->enabled = true;
926c9a4a 2556 stp_port_get_status(sp, &s->port_id, &s->state, &s->role);
21f7563c 2557 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
fd28ce3a
JS
2558
2559 return 0;
2560}
2561
2562static int
2563get_stp_port_stats(struct ofport *ofport_,
2564 struct ofproto_port_stp_stats *s)
2565{
2566 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2567 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2568 struct stp_port *sp = ofport->stp_port;
2569
2570 if (!ofproto->stp || !sp) {
2571 s->enabled = false;
2572 return 0;
2573 }
2574
2575 s->enabled = true;
80740385 2576 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
21f7563c
JP
2577
2578 return 0;
2579}
2580
2581static void
2582stp_run(struct ofproto_dpif *ofproto)
2583{
2584 if (ofproto->stp) {
2585 long long int now = time_msec();
2586 long long int elapsed = now - ofproto->stp_last_tick;
2587 struct stp_port *sp;
2588
2589 if (elapsed > 0) {
2590 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2591 ofproto->stp_last_tick = now;
2592 }
2593 while (stp_get_changed_port(ofproto->stp, &sp)) {
2594 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2595
2596 if (ofport) {
2597 update_stp_port_state(ofport);
2598 }
2599 }
6ae50723
EJ
2600
2601 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
509c0149 2602 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 2603 mac_learning_flush(ofproto->ml);
509c0149 2604 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6d95c4e8 2605 mcast_snooping_mdb_flush(ofproto->ms);
6ae50723 2606 }
21f7563c
JP
2607 }
2608}
2609
2610static void
2611stp_wait(struct ofproto_dpif *ofproto)
2612{
2613 if (ofproto->stp) {
2614 poll_timer_wait(1000);
2615 }
2616}
9efd308e
DV
2617
2618/* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2619 * caller is responsible for assigning RSTP port numbers and ensuring
2620 * there are no duplicates. */
2621static void
2622set_rstp_port(struct ofport *ofport_,
cc33c223 2623 const struct ofproto_port_rstp_settings *s)
9efd308e
DV
2624{
2625 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2626 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2627 struct rstp_port *rp = ofport->rstp_port;
9efd308e
DV
2628
2629 if (!s || !s->enable) {
2630 if (rp) {
e73e9165
JR
2631 rstp_port_set_aux(rp, NULL);
2632 rstp_port_set_state(rp, RSTP_DISABLED);
2633 rstp_port_set_mac_operational(rp, false);
9efd308e 2634 ofport->rstp_port = NULL;
e73e9165 2635 rstp_port_unref(rp);
9efd308e
DV
2636 update_rstp_port_state(ofport);
2637 }
9efd308e
DV
2638 return;
2639 }
f025bcb7
JR
2640
2641 /* Check if need to add a new port. */
9efd308e
DV
2642 if (!rp) {
2643 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2644 }
9efd308e 2645
f025bcb7 2646 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
67e8c1ac
JR
2647 s->admin_edge_port, s->auto_edge,
2648 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2649 ofport);
9efd308e 2650 update_rstp_port_state(ofport);
29db47ce
JR
2651 /* Synchronize operational status. */
2652 rstp_port_set_mac_operational(rp, ofport->may_enable);
9efd308e
DV
2653}
2654
2655static void
2656get_rstp_port_status(struct ofport *ofport_,
2657 struct ofproto_port_rstp_status *s)
2658{
2659 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2660 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2661 struct rstp_port *rp = ofport->rstp_port;
2662
2663 if (!ofproto->rstp || !rp) {
2664 s->enabled = false;
2665 return;
2666 }
2667
2668 s->enabled = true;
9c64e6b8
JR
2669 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2670 &s->designated_bridge_id, &s->designated_port_id,
2671 &s->designated_path_cost, &s->tx_count,
f025bcb7 2672 &s->rx_count, &s->error_count, &s->uptime);
9efd308e
DV
2673}
2674
21f7563c 2675\f
8b36f51e 2676static int
55954f6e 2677set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
8b36f51e
EJ
2678 size_t n_qdscp)
2679{
2680 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2681 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
8b36f51e 2682
55954f6e
EJ
2683 if (ofport->n_qdscp != n_qdscp
2684 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2685 n_qdscp * sizeof *qdscp))) {
2cc3c58e 2686 ofproto->backer->need_revalidate = REV_RECONFIGURE;
55954f6e
EJ
2687 free(ofport->qdscp);
2688 ofport->qdscp = n_qdscp
2689 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2690 : NULL;
2691 ofport->n_qdscp = n_qdscp;
8b36f51e
EJ
2692 }
2693
8b36f51e
EJ
2694 return 0;
2695}
2696\f
abe529af
BP
2697/* Bundles. */
2698
b44a10b7
BP
2699/* Expires all MAC learning entries associated with 'bundle' and forces its
2700 * ofproto to revalidate every flow.
2701 *
2702 * Normally MAC learning entries are removed only from the ofproto associated
2703 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2704 * are removed from every ofproto. When patch ports and SLB bonds are in use
2705 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2706 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2707 * with the host from which it migrated. */
abe529af 2708static void
b44a10b7 2709bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
abe529af
BP
2710{
2711 struct ofproto_dpif *ofproto = bundle->ofproto;
2712 struct mac_learning *ml = ofproto->ml;
2713 struct mac_entry *mac, *next_mac;
2714
2cc3c58e 2715 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 2716 ovs_rwlock_wrlock(&ml->rwlock);
abe529af 2717 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
9d078ec2 2718 if (mac_entry_get_port(ml, mac) == bundle) {
b44a10b7
BP
2719 if (all_ofprotos) {
2720 struct ofproto_dpif *o;
2721
2722 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2723 if (o != ofproto) {
2724 struct mac_entry *e;
2725
509c0149 2726 ovs_rwlock_wrlock(&o->ml->rwlock);
30618594 2727 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
b44a10b7 2728 if (e) {
b44a10b7
BP
2729 mac_learning_expire(o->ml, e);
2730 }
509c0149 2731 ovs_rwlock_unlock(&o->ml->rwlock);
b44a10b7
BP
2732 }
2733 }
2734 }
2735
abe529af
BP
2736 mac_learning_expire(ml, mac);
2737 }
2738 }
509c0149 2739 ovs_rwlock_unlock(&ml->rwlock);
abe529af
BP
2740}
2741
2372c146
JR
2742static void
2743bundle_move(struct ofbundle *old, struct ofbundle *new)
2744{
2745 struct ofproto_dpif *ofproto = old->ofproto;
2746 struct mac_learning *ml = ofproto->ml;
2747 struct mac_entry *mac, *next_mac;
2748
2749 ovs_assert(new->ofproto == old->ofproto);
2750
2751 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2752 ovs_rwlock_wrlock(&ml->rwlock);
2753 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
9d078ec2
BP
2754 if (mac_entry_get_port(ml, mac) == old) {
2755 mac_entry_set_port(ml, mac, new);
2372c146
JR
2756 }
2757 }
2758 ovs_rwlock_unlock(&ml->rwlock);
2759}
2760
abe529af
BP
2761static struct ofbundle *
2762bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2763{
2764 struct ofbundle *bundle;
2765
2766 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2767 &ofproto->bundles) {
2768 if (bundle->aux == aux) {
2769 return bundle;
2770 }
2771 }
2772 return NULL;
2773}
2774
7bde8dd8
JP
2775static void
2776bundle_update(struct ofbundle *bundle)
2777{
2778 struct ofport_dpif *port;
2779
2780 bundle->floodable = true;
2781 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
9e1fd49b 2782 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
a6363cfd 2783 || port->is_layer3
4b5f1996
DV
2784 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2785 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
7bde8dd8
JP
2786 bundle->floodable = false;
2787 break;
2788 }
2789 }
2790}
2791
abe529af
BP
2792static void
2793bundle_del_port(struct ofport_dpif *port)
2794{
2795 struct ofbundle *bundle = port->bundle;
2796
2cc3c58e 2797 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
6f77f4ae 2798
417e7e66 2799 ovs_list_remove(&port->bundle_node);
abe529af
BP
2800 port->bundle = NULL;
2801
2802 if (bundle->lacp) {
2803 lacp_slave_unregister(bundle->lacp, port);
2804 }
2805 if (bundle->bond) {
2806 bond_slave_unregister(bundle->bond, port);
2807 }
2808
7bde8dd8 2809 bundle_update(bundle);
abe529af
BP
2810}
2811
2812static bool
4e022ec0 2813bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
df53d41c 2814 struct lacp_slave_settings *lacp)
abe529af
BP
2815{
2816 struct ofport_dpif *port;
2817
e672ff9b 2818 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
abe529af
BP
2819 if (!port) {
2820 return false;
2821 }
2822
2823 if (port->bundle != bundle) {
2cc3c58e 2824 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af 2825 if (port->bundle) {
e019a574 2826 bundle_remove(&port->up);
abe529af
BP
2827 }
2828
2829 port->bundle = bundle;
417e7e66 2830 ovs_list_push_back(&bundle->ports, &port->bundle_node);
9e1fd49b 2831 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
a6363cfd 2832 || port->is_layer3
4b5f1996
DV
2833 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2834 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
abe529af
BP
2835 bundle->floodable = false;
2836 }
2837 }
2838 if (lacp) {
2cc3c58e 2839 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2840 lacp_slave_register(bundle->lacp, port, lacp);
2841 }
2842
2843 return true;
2844}
2845
2846static void
2847bundle_destroy(struct ofbundle *bundle)
2848{
2849 struct ofproto_dpif *ofproto;
2850 struct ofport_dpif *port, *next_port;
abe529af
BP
2851
2852 if (!bundle) {
2853 return;
2854 }
2855
2856 ofproto = bundle->ofproto;
67912650 2857 mbridge_unregister_bundle(ofproto->mbridge, bundle);
abe529af 2858
84f0f298 2859 xlate_txn_start();
46c88433 2860 xlate_bundle_remove(bundle);
84f0f298 2861 xlate_txn_commit();
46c88433 2862
abe529af
BP
2863 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2864 bundle_del_port(port);
2865 }
2866
b44a10b7 2867 bundle_flush_macs(bundle, true);
b077575e 2868 mcast_snooping_flush_bundle(ofproto->ms, bundle);
abe529af
BP
2869 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2870 free(bundle->name);
2871 free(bundle->trunks);
fed8962a 2872 free(bundle->cvlans);
91779071 2873 lacp_unref(bundle->lacp);
03366a2d 2874 bond_unref(bundle->bond);
abe529af
BP
2875 free(bundle);
2876}
2877
2878static int
2879bundle_set(struct ofproto *ofproto_, void *aux,
2880 const struct ofproto_bundle_settings *s)
2881{
2882 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2883 bool need_flush = false;
abe529af
BP
2884 struct ofport_dpif *port;
2885 struct ofbundle *bundle;
f0fb825a 2886 unsigned long *trunks = NULL;
fed8962a 2887 unsigned long *cvlans = NULL;
ecac4ebf 2888 int vlan;
abe529af
BP
2889 size_t i;
2890 bool ok;
2891
2892 if (!s) {
2893 bundle_destroy(bundle_lookup(ofproto, aux));
2894 return 0;
2895 }
2896
cb22974d
BP
2897 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2898 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
abe529af
BP
2899
2900 bundle = bundle_lookup(ofproto, aux);
2901 if (!bundle) {
2902 bundle = xmalloc(sizeof *bundle);
2903
2904 bundle->ofproto = ofproto;
2905 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2906 hash_pointer(aux, 0));
2907 bundle->aux = aux;
2908 bundle->name = NULL;
2909
417e7e66 2910 ovs_list_init(&bundle->ports);
ecac4ebf 2911 bundle->vlan_mode = PORT_VLAN_TRUNK;
fed8962a 2912 bundle->qinq_ethtype = ETH_TYPE_VLAN_8021AD;
abe529af
BP
2913 bundle->vlan = -1;
2914 bundle->trunks = NULL;
fed8962a 2915 bundle->cvlans = NULL;
5e9ceccd 2916 bundle->use_priority_tags = s->use_priority_tags;
abe529af
BP
2917 bundle->lacp = NULL;
2918 bundle->bond = NULL;
2919
2920 bundle->floodable = true;
c005f976 2921 bundle->protected = false;
ec7ceaed 2922 mbridge_register_bundle(ofproto->mbridge, bundle);
abe529af
BP
2923 }
2924
2925 if (!bundle->name || strcmp(s->name, bundle->name)) {
2926 free(bundle->name);
2927 bundle->name = xstrdup(s->name);
2928 }
2929
2930 /* LACP. */
2931 if (s->lacp) {
e2d13c43 2932 ofproto->lacp_enabled = true;
abe529af 2933 if (!bundle->lacp) {
2cc3c58e 2934 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2935 bundle->lacp = lacp_create();
2936 }
2937 lacp_configure(bundle->lacp, s->lacp);
2938 } else {
91779071 2939 lacp_unref(bundle->lacp);
abe529af
BP
2940 bundle->lacp = NULL;
2941 }
2942
2943 /* Update set of ports. */
2944 ok = true;
2945 for (i = 0; i < s->n_slaves; i++) {
2946 if (!bundle_add_port(bundle, s->slaves[i],
df53d41c 2947 s->lacp ? &s->lacp_slaves[i] : NULL)) {
abe529af
BP
2948 ok = false;
2949 }
2950 }
417e7e66 2951 if (!ok || ovs_list_size(&bundle->ports) != s->n_slaves) {
abe529af
BP
2952 struct ofport_dpif *next_port;
2953
2954 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2955 for (i = 0; i < s->n_slaves; i++) {
56c769ab 2956 if (s->slaves[i] == port->up.ofp_port) {
abe529af
BP
2957 goto found;
2958 }
2959 }
2960
2961 bundle_del_port(port);
2962 found: ;
2963 }
2964 }
417e7e66 2965 ovs_assert(ovs_list_size(&bundle->ports) <= s->n_slaves);
abe529af 2966
417e7e66 2967 if (ovs_list_is_empty(&bundle->ports)) {
abe529af
BP
2968 bundle_destroy(bundle);
2969 return EINVAL;
2970 }
2971
ecac4ebf 2972 /* Set VLAN tagging mode */
5e9ceccd
BP
2973 if (s->vlan_mode != bundle->vlan_mode
2974 || s->use_priority_tags != bundle->use_priority_tags) {
ecac4ebf 2975 bundle->vlan_mode = s->vlan_mode;
5e9ceccd 2976 bundle->use_priority_tags = s->use_priority_tags;
ecac4ebf
BP
2977 need_flush = true;
2978 }
2979
fed8962a
EG
2980 if (s->qinq_ethtype != bundle->qinq_ethtype) {
2981 bundle->qinq_ethtype = s->qinq_ethtype;
2982 need_flush = true;
2983 }
2984
abe529af 2985 /* Set VLAN tag. */
ecac4ebf
BP
2986 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2987 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2988 : 0);
2989 if (vlan != bundle->vlan) {
2990 bundle->vlan = vlan;
abe529af
BP
2991 need_flush = true;
2992 }
2993
2994 /* Get trunked VLANs. */
ecac4ebf
BP
2995 switch (s->vlan_mode) {
2996 case PORT_VLAN_ACCESS:
2997 trunks = NULL;
2998 break;
2999
3000 case PORT_VLAN_TRUNK:
ebc56baa 3001 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
3002 break;
3003
3004 case PORT_VLAN_NATIVE_UNTAGGED:
3005 case PORT_VLAN_NATIVE_TAGGED:
3006 if (vlan != 0 && (!s->trunks
3007 || !bitmap_is_set(s->trunks, vlan)
3008 || bitmap_is_set(s->trunks, 0))) {
3009 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
3010 if (s->trunks) {
3011 trunks = bitmap_clone(s->trunks, 4096);
3012 } else {
3013 trunks = bitmap_allocate1(4096);
3014 }
3015 bitmap_set1(trunks, vlan);
3016 bitmap_set0(trunks, 0);
3017 } else {
ebc56baa 3018 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
3019 }
3020 break;
3021
fed8962a
EG
3022 case PORT_VLAN_DOT1Q_TUNNEL:
3023 cvlans = CONST_CAST(unsigned long *, s->cvlans);
3024 break;
3025
ecac4ebf 3026 default:
428b2edd 3027 OVS_NOT_REACHED();
ecac4ebf 3028 }
abe529af
BP
3029 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
3030 free(bundle->trunks);
ecac4ebf
BP
3031 if (trunks == s->trunks) {
3032 bundle->trunks = vlan_bitmap_clone(trunks);
3033 } else {
3034 bundle->trunks = trunks;
3035 trunks = NULL;
3036 }
abe529af
BP
3037 need_flush = true;
3038 }
ecac4ebf
BP
3039 if (trunks != s->trunks) {
3040 free(trunks);
3041 }
abe529af 3042
fed8962a
EG
3043 if (!vlan_bitmap_equal(cvlans, bundle->cvlans)) {
3044 free(bundle->cvlans);
3045 if (cvlans == s->cvlans) {
3046 bundle->cvlans = vlan_bitmap_clone(cvlans);
3047 } else {
3048 bundle->cvlans = cvlans;
3049 cvlans = NULL;
3050 }
3051 need_flush = true;
3052 }
3053 if (cvlans != s->cvlans) {
3054 free(cvlans);
3055 }
3056
abe529af 3057 /* Bonding. */
417e7e66 3058 if (!ovs_list_is_short(&bundle->ports)) {
abe529af
BP
3059 bundle->ofproto->has_bonded_bundles = true;
3060 if (bundle->bond) {
3061 if (bond_reconfigure(bundle->bond, s->bond)) {
2cc3c58e 3062 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
3063 }
3064 } else {
adcf00ba 3065 bundle->bond = bond_create(s->bond, ofproto);
2cc3c58e 3066 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
3067 }
3068
3069 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
adcf00ba
AZ
3070 bond_slave_register(bundle->bond, port,
3071 port->up.ofp_port, port->up.netdev);
abe529af
BP
3072 }
3073 } else {
03366a2d 3074 bond_unref(bundle->bond);
abe529af
BP
3075 bundle->bond = NULL;
3076 }
3077
c005f976
BK
3078 /* Set proteced port mode */
3079 if (s->protected != bundle->protected) {
3080 bundle->protected = s->protected;
3081 need_flush = true;
3082 }
3083
abe529af
BP
3084 /* If we changed something that would affect MAC learning, un-learn
3085 * everything on this port and force flow revalidation. */
3086 if (need_flush) {
b44a10b7 3087 bundle_flush_macs(bundle, false);
4fbbf862 3088 mcast_snooping_flush_bundle(ofproto->ms, bundle);
abe529af
BP
3089 }
3090
3091 return 0;
3092}
3093
3094static void
3095bundle_remove(struct ofport *port_)
3096{
3097 struct ofport_dpif *port = ofport_dpif_cast(port_);
3098 struct ofbundle *bundle = port->bundle;
3099
3100 if (bundle) {
3101 bundle_del_port(port);
417e7e66 3102 if (ovs_list_is_empty(&bundle->ports)) {
abe529af 3103 bundle_destroy(bundle);
417e7e66 3104 } else if (ovs_list_is_short(&bundle->ports)) {
03366a2d 3105 bond_unref(bundle->bond);
abe529af
BP
3106 bundle->bond = NULL;
3107 }
3108 }
3109}
3110
3111static void
5f877369 3112send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
abe529af
BP
3113{
3114 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3115 struct ofport_dpif *port = port_;
74ff3298 3116 struct eth_addr ea;
abe529af
BP
3117 int error;
3118
74ff3298 3119 error = netdev_get_etheraddr(port->up.netdev, &ea);
abe529af 3120 if (!error) {
cf62fa4c 3121 struct dp_packet packet;
5f877369 3122 void *packet_pdu;
abe529af 3123
cf62fa4c 3124 dp_packet_init(&packet, 0);
abe529af 3125 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
5f877369
EJ
3126 pdu_size);
3127 memcpy(packet_pdu, pdu, pdu_size);
3128
2eb79142 3129 ofproto_dpif_send_packet(port, false, &packet);
cf62fa4c 3130 dp_packet_uninit(&packet);
abe529af
BP
3131 } else {
3132 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
3133 "%s (%s)", port->bundle->name,
10a89ef0 3134 netdev_get_name(port->up.netdev), ovs_strerror(error));
abe529af
BP
3135 }
3136}
3137
3138static void
3139bundle_send_learning_packets(struct ofbundle *bundle)
3140{
3141 struct ofproto_dpif *ofproto = bundle->ofproto;
3142 int error, n_packets, n_errors;
3143 struct mac_entry *e;
8613db65
DDP
3144 struct pkt_list {
3145 struct ovs_list list_node;
3146 struct ofport_dpif *port;
3147 struct dp_packet *pkt;
3148 } *pkt_node;
ca6ba700 3149 struct ovs_list packets;
abe529af 3150
417e7e66 3151 ovs_list_init(&packets);
509c0149 3152 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
abe529af 3153 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
9d078ec2 3154 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
8613db65
DDP
3155 pkt_node = xmalloc(sizeof *pkt_node);
3156 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3157 e->mac, e->vlan,
3158 (void **)&pkt_node->port);
417e7e66 3159 ovs_list_push_back(&packets, &pkt_node->list_node);
abe529af
BP
3160 }
3161 }
509c0149 3162 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af 3163
0165217e 3164 error = n_packets = n_errors = 0;
8613db65 3165 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
0165217e
EJ
3166 int ret;
3167
2eb79142 3168 ret = ofproto_dpif_send_packet(pkt_node->port, false, pkt_node->pkt);
8613db65
DDP
3169 dp_packet_delete(pkt_node->pkt);
3170 free(pkt_node);
0165217e
EJ
3171 if (ret) {
3172 error = ret;
3173 n_errors++;
3174 }
3175 n_packets++;
3176 }
0165217e 3177
abe529af
BP
3178 if (n_errors) {
3179 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3180 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3181 "packets, last error was: %s",
10a89ef0 3182 bundle->name, n_errors, n_packets, ovs_strerror(error));
abe529af
BP
3183 } else {
3184 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3185 bundle->name, n_packets);
3186 }
3187}
3188
3189static void
3190bundle_run(struct ofbundle *bundle)
3191{
3192 if (bundle->lacp) {
3193 lacp_run(bundle->lacp, send_pdu_cb);
3194 }
3195 if (bundle->bond) {
3196 struct ofport_dpif *port;
3197
3198 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
015e08bc 3199 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
abe529af
BP
3200 }
3201
4a1b8f30
EJ
3202 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3203 bundle->ofproto->backer->need_revalidate = REV_BOND;
3204 }
3205
abe529af
BP
3206 if (bond_should_send_learning_packets(bundle->bond)) {
3207 bundle_send_learning_packets(bundle);
3208 }
3209 }
3210}
3211
3212static void
3213bundle_wait(struct ofbundle *bundle)
3214{
3215 if (bundle->lacp) {
3216 lacp_wait(bundle->lacp);
3217 }
3218 if (bundle->bond) {
3219 bond_wait(bundle->bond);
3220 }
3221}
3222\f
3223/* Mirrors. */
3224
3225static int
ec7ceaed
EJ
3226mirror_set__(struct ofproto *ofproto_, void *aux,
3227 const struct ofproto_mirror_settings *s)
abe529af
BP
3228{
3229 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
ec7ceaed
EJ
3230 struct ofbundle **srcs, **dsts;
3231 int error;
3232 size_t i;
abe529af 3233
abe529af 3234 if (!s) {
ec7ceaed 3235 mirror_destroy(ofproto->mbridge, aux);
abe529af
BP
3236 return 0;
3237 }
3238
ec7ceaed
EJ
3239 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3240 dsts = xmalloc(s->n_dsts * sizeof *dsts);
abe529af 3241
ec7ceaed
EJ
3242 for (i = 0; i < s->n_srcs; i++) {
3243 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
abe529af
BP
3244 }
3245
ec7ceaed
EJ
3246 for (i = 0; i < s->n_dsts; i++) {
3247 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
abe529af
BP
3248 }
3249
ec7ceaed
EJ
3250 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3251 s->n_dsts, s->src_vlans,
1356dbd1
WT
3252 bundle_lookup(ofproto, s->out_bundle),
3253 s->snaplen, s->out_vlan);
ec7ceaed
EJ
3254 free(srcs);
3255 free(dsts);
3256 return error;
abe529af
BP
3257}
3258
9d24de3b 3259static int
ec7ceaed
EJ
3260mirror_get_stats__(struct ofproto *ofproto, void *aux,
3261 uint64_t *packets, uint64_t *bytes)
9d24de3b 3262{
ec7ceaed
EJ
3263 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3264 bytes);
9d24de3b
JP
3265}
3266
abe529af
BP
3267static int
3268set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3269{
3270 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 3271 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
abe529af 3272 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
30618594 3273 mac_learning_flush(ofproto->ml);
abe529af 3274 }
509c0149 3275 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af
BP
3276 return 0;
3277}
3278
3279static bool
b4affc74 3280is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
abe529af
BP
3281{
3282 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3283 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
ec7ceaed 3284 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
abe529af 3285}
8402c74b
SS
3286
3287static void
b53055f4 3288forward_bpdu_changed(struct ofproto *ofproto_)
8402c74b
SS
3289{
3290 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2cc3c58e 3291 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8402c74b 3292}
e764773c
BP
3293
3294static void
c4069512
BP
3295set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3296 size_t max_entries)
e764773c
BP
3297{
3298 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 3299 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
e764773c 3300 mac_learning_set_idle_time(ofproto->ml, idle_time);
c4069512 3301 mac_learning_set_max_entries(ofproto->ml, max_entries);
509c0149 3302 ovs_rwlock_unlock(&ofproto->ml->rwlock);
e764773c 3303}
7c38d0a5
FL
3304
3305/* Configures multicast snooping on 'ofport' using the settings
3306 * defined in 's'. */
3307static int
3308set_mcast_snooping(struct ofproto *ofproto_,
3309 const struct ofproto_mcast_snooping_settings *s)
3310{
3311 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3312
3313 /* Only revalidate flows if the configuration changed. */
3314 if (!s != !ofproto->ms) {
3315 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3316 }
3317
3318 if (s) {
3319 if (!ofproto->ms) {
3320 ofproto->ms = mcast_snooping_create();
3321 }
3322
3323 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3324 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3325 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3326 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3327 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3328 }
3329 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3330 } else {
3331 mcast_snooping_unref(ofproto->ms);
3332 ofproto->ms = NULL;
3333 }
3334
3335 return 0;
3336}
3337
8e04a33f 3338/* Configures multicast snooping port's flood settings on 'ofproto'. */
7c38d0a5 3339static int
8e04a33f
FL
3340set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3341 const struct ofproto_mcast_snooping_port_settings *s)
7c38d0a5
FL
3342{
3343 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3344 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3345
8e04a33f 3346 if (ofproto->ms && s) {
7c38d0a5 3347 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
8e04a33f
FL
3348 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3349 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3350 s->flood_reports);
7c38d0a5
FL
3351 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3352 }
3353 return 0;
3354}
3355
abe529af
BP
3356\f
3357/* Ports. */
3358
e672ff9b
JR
3359struct ofport_dpif *
3360ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
abe529af 3361{
7df6a8bd
BP
3362 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3363 return ofport ? ofport_dpif_cast(ofport) : NULL;
abe529af
BP
3364}
3365
abe529af 3366static void
e1b1d06a
JP
3367ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3368 struct ofproto_port *ofproto_port,
abe529af
BP
3369 struct dpif_port *dpif_port)
3370{
3371 ofproto_port->name = dpif_port->name;
3372 ofproto_port->type = dpif_port->type;
e1b1d06a 3373 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
abe529af
BP
3374}
3375
6cbbf4fa
EJ
3376static void
3377ofport_update_peer(struct ofport_dpif *ofport)
0a740f48
EJ
3378{
3379 const struct ofproto_dpif *ofproto;
6cbbf4fa 3380 struct dpif_backer *backer;
161b6042 3381 char *peer_name;
6cbbf4fa
EJ
3382
3383 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3384 return;
3385 }
3386
3387 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
7894e7da 3388 backer->need_revalidate = REV_RECONFIGURE;
0a740f48 3389
6cbbf4fa
EJ
3390 if (ofport->peer) {
3391 ofport->peer->peer = NULL;
3392 ofport->peer = NULL;
3393 }
3394
3395 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3396 if (!peer_name) {
3397 return;
0a740f48
EJ
3398 }
3399
3400 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6cbbf4fa
EJ
3401 struct ofport *peer_ofport;
3402 struct ofport_dpif *peer;
161b6042 3403 char *peer_peer;
0a740f48 3404
462abef2
EJ
3405 if (ofproto->backer != backer) {
3406 continue;
3407 }
3408
6cbbf4fa
EJ
3409 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3410 if (!peer_ofport) {
3411 continue;
3412 }
3413
3414 peer = ofport_dpif_cast(peer_ofport);
3415 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3416 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3417 peer_peer)) {
3418 ofport->peer = peer;
3419 ofport->peer->peer = ofport;
0a740f48 3420 }
161b6042 3421 free(peer_peer);
6cbbf4fa 3422
161b6042 3423 break;
0a740f48 3424 }
161b6042 3425 free(peer_name);
0a740f48
EJ
3426}
3427
abe529af
BP
3428static void
3429port_run(struct ofport_dpif *ofport)
3430{
3e5b3fdb
EJ
3431 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3432 bool carrier_changed = carrier_seq != ofport->carrier_seq;
015e08bc 3433 bool enable = netdev_get_carrier(ofport->up.netdev);
2d344ba5
AW
3434 bool cfm_enable = false;
3435 bool bfd_enable = false;
015e08bc 3436
3e5b3fdb
EJ
3437 ofport->carrier_seq = carrier_seq;
3438
abe529af 3439 if (ofport->cfm) {
4653c558
EJ
3440 int cfm_opup = cfm_get_opup(ofport->cfm);
3441
2d344ba5 3442 cfm_enable = !cfm_get_fault(ofport->cfm);
4653c558
EJ
3443
3444 if (cfm_opup >= 0) {
2d344ba5 3445 cfm_enable = cfm_enable && cfm_opup;
4653c558 3446 }
abe529af 3447 }
015e08bc 3448
ccc09689 3449 if (ofport->bfd) {
2d344ba5
AW
3450 bfd_enable = bfd_forwarding(ofport->bfd);
3451 }
3452
3453 if (ofport->bfd || ofport->cfm) {
3454 enable = enable && (cfm_enable || bfd_enable);
ccc09689
EJ
3455 }
3456
015e08bc
EJ
3457 if (ofport->bundle) {
3458 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3e5b3fdb
EJ
3459 if (carrier_changed) {
3460 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3461 }
015e08bc
EJ
3462 }
3463
daff3353
EJ
3464 if (ofport->may_enable != enable) {
3465 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
daff3353 3466
f025bcb7 3467 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
9efd308e 3468
f025bcb7 3469 if (ofport->rstp_port) {
9efd308e
DV
3470 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3471 }
f73b83fd
LS
3472
3473 /* Propagate liveness, unless the link is administratively or
3474 * operationally down. */
3475 if (!(ofport->up.pp.config & OFPUTIL_PC_PORT_DOWN) &&
3476 !(ofport->up.pp.state & OFPUTIL_PS_LINK_DOWN)) {
3477 enum ofputil_port_state of_state = ofport->up.pp.state;
3478 if (enable) {
3479 of_state |= OFPUTIL_PS_LIVE;
3480 } else {
3481 of_state &= ~OFPUTIL_PS_LIVE;
3482 }
3483 ofproto_port_set_state(&ofport->up, of_state);
3484 }
9efd308e 3485 }
f025bcb7
JR
3486
3487 ofport->may_enable = enable;
abe529af
BP
3488}
3489
abe529af
BP
3490static int
3491port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3492 struct ofproto_port *ofproto_port)
3493{
3494 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3495 struct dpif_port dpif_port;
3496 int error;
3497
0a740f48
EJ
3498 if (sset_contains(&ofproto->ghost_ports, devname)) {
3499 const char *type = netdev_get_type_from_name(devname);
3500
3501 /* We may be called before ofproto->up.port_by_name is populated with
3502 * the appropriate ofport. For this reason, we must get the name and
3503 * type from the netdev layer directly. */
3504 if (type) {
3505 const struct ofport *ofport;
3506
3507 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3508 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3509 ofproto_port->name = xstrdup(devname);
3510 ofproto_port->type = xstrdup(type);
3511 return 0;
3512 }
3513 return ENODEV;
3514 }
3515
acf60855
JP
3516 if (!sset_contains(&ofproto->ports, devname)) {
3517 return ENODEV;
3518 }
3519 error = dpif_port_query_by_name(ofproto->backer->dpif,
3520 devname, &dpif_port);
abe529af 3521 if (!error) {
e1b1d06a 3522 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
abe529af
BP
3523 }
3524 return error;
3525}
3526
3527static int
e1b1d06a 3528port_add(struct ofproto *ofproto_, struct netdev *netdev)
abe529af
BP
3529{
3530 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
b9ad7294 3531 const char *devname = netdev_get_name(netdev);
3aa30359
BP
3532 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3533 const char *dp_port_name;
abe529af 3534
0a740f48
EJ
3535 if (netdev_vport_is_patch(netdev)) {
3536 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3537 return 0;
3538 }
3539
3aa30359 3540 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
b9ad7294 3541 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
4e022ec0 3542 odp_port_t port_no = ODPP_NONE;
7d82ab2e
KM
3543 int error;
3544
3545 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
b9ad7294
EJ
3546 if (error) {
3547 return error;
3548 }
7d82ab2e 3549 if (netdev_get_tunnel_config(netdev)) {
4e022ec0
AW
3550 simap_put(&ofproto->backer->tnl_backers,
3551 dp_port_name, odp_to_u32(port_no));
7d82ab2e 3552 }
acf60855 3553 }
b9ad7294
EJ
3554
3555 if (netdev_get_tunnel_config(netdev)) {
3556 sset_add(&ofproto->ghost_ports, devname);
b9ad7294
EJ
3557 } else {
3558 sset_add(&ofproto->ports, devname);
3559 }
3560 return 0;
3561}
3562
abe529af 3563static int
4e022ec0 3564port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
abe529af
BP
3565{
3566 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
e672ff9b 3567 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
e1b1d06a 3568 int error = 0;
abe529af 3569
b9ad7294
EJ
3570 if (!ofport) {
3571 return 0;
e1b1d06a 3572 }
b9ad7294
EJ
3573
3574 sset_find_and_delete(&ofproto->ghost_ports,
3575 netdev_get_name(ofport->up.netdev));
a614d823 3576 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8911c674 3577 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
b9ad7294
EJ
3578 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3579 if (!error) {
abe529af
BP
3580 /* The caller is going to close ofport->up.netdev. If this is a
3581 * bonded port, then the bond is using that netdev, so remove it
3582 * from the bond. The client will need to reconfigure everything
3583 * after deleting ports, so then the slave will get re-added. */
3584 bundle_remove(&ofport->up);
3585 }
3586 }
3587 return error;
3588}
3589
91364d18
IM
3590static int
3591port_set_config(const struct ofport *ofport_, const struct smap *cfg)
3592{
3593 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3594 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3595
3596 if (sset_contains(&ofproto->ghost_ports,
3597 netdev_get_name(ofport->up.netdev))) {
3598 return 0;
3599 }
3600
3601 return dpif_port_set_config(ofproto->backer->dpif, ofport->odp_port, cfg);
3602}
3603
6527c598
PS
3604static int
3605port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3606{
3607 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3608 int error;
3609
3610 error = netdev_get_stats(ofport->up.netdev, stats);
3611
ee382d89 3612 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
6527c598
PS
3613 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3614
0da3c61b 3615 ovs_mutex_lock(&ofproto->stats_mutex);
6527c598
PS
3616 /* ofproto->stats.tx_packets represents packets that we created
3617 * internally and sent to some port (e.g. packets sent with
91d6cd12
AW
3618 * ofproto_dpif_send_packet()). Account for them as if they had
3619 * come from OFPP_LOCAL and got forwarded. */
6527c598
PS
3620
3621 if (stats->rx_packets != UINT64_MAX) {
3622 stats->rx_packets += ofproto->stats.tx_packets;
3623 }
3624
3625 if (stats->rx_bytes != UINT64_MAX) {
3626 stats->rx_bytes += ofproto->stats.tx_bytes;
3627 }
3628
3629 /* ofproto->stats.rx_packets represents packets that were received on
3630 * some port and we processed internally and dropped (e.g. STP).
4e090bc7 3631 * Account for them as if they had been forwarded to OFPP_LOCAL. */
6527c598
PS
3632
3633 if (stats->tx_packets != UINT64_MAX) {
3634 stats->tx_packets += ofproto->stats.rx_packets;
3635 }
3636
3637 if (stats->tx_bytes != UINT64_MAX) {
3638 stats->tx_bytes += ofproto->stats.rx_bytes;
3639 }
0da3c61b 3640 ovs_mutex_unlock(&ofproto->stats_mutex);
6527c598
PS
3641 }
3642
3643 return error;
3644}
3645
50b9699f
NM
3646static int
3647port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3648{
3649 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3650 if (ofport->bundle && ofport->bundle->lacp) {
3651 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3652 return 0;
3653 }
3654 }
3655 return -1;
3656}
3657
abe529af 3658struct port_dump_state {
bfbcebc2 3659 struct sset_position pos;
0a740f48 3660 bool ghost;
da78d43d
BP
3661
3662 struct ofproto_port port;
3663 bool has_port;
abe529af
BP
3664};
3665
3666static int
acf60855 3667port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
abe529af 3668{
0a740f48 3669 *statep = xzalloc(sizeof(struct port_dump_state));
abe529af
BP
3670 return 0;
3671}
3672
3673static int
b9ad7294 3674port_dump_next(const struct ofproto *ofproto_, void *state_,
abe529af
BP
3675 struct ofproto_port *port)
3676{
e1b1d06a 3677 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 3678 struct port_dump_state *state = state_;
0a740f48 3679 const struct sset *sset;
acf60855 3680 struct sset_node *node;
abe529af 3681
da78d43d
BP
3682 if (state->has_port) {
3683 ofproto_port_destroy(&state->port);
3684 state->has_port = false;
3685 }
0a740f48 3686 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
bfbcebc2 3687 while ((node = sset_at_position(sset, &state->pos))) {
acf60855
JP
3688 int error;
3689
da78d43d
BP
3690 error = port_query_by_name(ofproto_, node->name, &state->port);
3691 if (!error) {
3692 *port = state->port;
3693 state->has_port = true;
3694 return 0;
3695 } else if (error != ENODEV) {
acf60855
JP
3696 return error;
3697 }
abe529af 3698 }
acf60855 3699
0a740f48
EJ
3700 if (!state->ghost) {
3701 state->ghost = true;
bfbcebc2 3702 memset(&state->pos, 0, sizeof state->pos);
0a740f48
EJ
3703 return port_dump_next(ofproto_, state_, port);
3704 }
3705
acf60855 3706 return EOF;
abe529af
BP
3707}
3708
3709static int
3710port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3711{
3712 struct port_dump_state *state = state_;
3713
da78d43d
BP
3714 if (state->has_port) {
3715 ofproto_port_destroy(&state->port);
3716 }
abe529af
BP
3717 free(state);
3718 return 0;
3719}
3720
3721static int
3722port_poll(const struct ofproto *ofproto_, char **devnamep)
3723{
3724 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855
JP
3725
3726 if (ofproto->port_poll_errno) {
3727 int error = ofproto->port_poll_errno;
3728 ofproto->port_poll_errno = 0;
3729 return error;
3730 }
3731
3732 if (sset_is_empty(&ofproto->port_poll_set)) {
3733 return EAGAIN;
3734 }
3735
3736 *devnamep = sset_pop(&ofproto->port_poll_set);
3737 return 0;
abe529af
BP
3738}
3739
3740static void
3741port_poll_wait(const struct ofproto *ofproto_)
3742{
3743 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855 3744 dpif_port_poll_wait(ofproto->backer->dpif);
abe529af
BP
3745}
3746
3747static int
3748port_is_lacp_current(const struct ofport *ofport_)
3749{
3750 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3751 return (ofport->bundle && ofport->bundle->lacp
3752 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3753 : -1);
3754}
3755\f
e79a6c83
EJ
3756/* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3757 * then delete it entirely. */
3758static void
f5857865 3759rule_expire(struct rule_dpif *rule, long long now)
e79a6c83 3760 OVS_REQUIRES(ofproto_mutex)
9d6ac44e 3761{
dc437090 3762 uint16_t hard_timeout, idle_timeout;
dc437090 3763 int reason = -1;
9d6ac44e 3764
e79a6c83
EJ
3765 hard_timeout = rule->up.hard_timeout;
3766 idle_timeout = rule->up.idle_timeout;
dc437090
JR
3767
3768 /* Has 'rule' expired? */
3769 if (hard_timeout) {
3770 long long int modified;
3771
3772 ovs_mutex_lock(&rule->up.mutex);
3773 modified = rule->up.modified;
3774 ovs_mutex_unlock(&rule->up.mutex);
3775
3776 if (now > modified + hard_timeout * 1000) {
3777 reason = OFPRR_HARD_TIMEOUT;
3778 }
3779 }
3780
3781 if (reason < 0 && idle_timeout) {
3782 long long int used;
3783
3784 ovs_mutex_lock(&rule->stats_mutex);
1a4ec18d 3785 used = rule->stats.used;
dc437090
JR
3786 ovs_mutex_unlock(&rule->stats_mutex);
3787
3788 if (now > used + idle_timeout * 1000) {
3789 reason = OFPRR_IDLE_TIMEOUT;
3790 }
f9c37233 3791 }
501f8d1f 3792
e79a6c83
EJ
3793 if (reason >= 0) {
3794 COVERAGE_INC(ofproto_dpif_expired);
3795 ofproto_rule_expire(&rule->up, reason);
a41d7bf2 3796 }
e79a6c83 3797}
a41d7bf2 3798
1df7f7aa
NS
3799static void
3800ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif *ofproto,
3801 ofp_port_t in_port, struct dp_packet *packet)
3802{
3803 if (in_port == OFPP_NONE) {
3804 in_port = OFPP_LOCAL;
3805 }
3806 packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3807}
3808
e79a6c83 3809int
cdd42eda 3810ofproto_dpif_execute_actions__(struct ofproto_dpif *ofproto,
1f4a8933 3811 ovs_version_t version, const struct flow *flow,
cdd42eda
JG
3812 struct rule_dpif *rule,
3813 const struct ofpact *ofpacts, size_t ofpacts_len,
2d9b49dd 3814 int depth, int resubmits,
cdd42eda 3815 struct dp_packet *packet)
e79a6c83 3816{
e79a6c83
EJ
3817 struct dpif_flow_stats stats;
3818 struct xlate_out xout;
3819 struct xlate_in xin;
758c456d 3820 struct dpif_execute execute;
e79a6c83 3821 int error;
f9c37233 3822
e79a6c83 3823 ovs_assert((rule != NULL) != (ofpacts != NULL));
501f8d1f 3824
e79a6c83 3825 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
adcf00ba 3826
e79a6c83
EJ
3827 if (rule) {
3828 rule_dpif_credit_stats(rule, &stats);
3829 }
c84451a6 3830
1520ef4f
BP
3831 uint64_t odp_actions_stub[1024 / 8];
3832 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
1f4a8933 3833 xlate_in_init(&xin, ofproto, version, flow, flow->in_port.ofp_port, rule,
1520ef4f 3834 stats.tcp_flags, packet, NULL, &odp_actions);
e79a6c83
EJ
3835 xin.ofpacts = ofpacts;
3836 xin.ofpacts_len = ofpacts_len;
3837 xin.resubmit_stats = &stats;
790c5d26 3838 xin.depth = depth;
cdd42eda 3839 xin.resubmits = resubmits;
fff1b9c0
JR
3840 if (xlate_actions(&xin, &xout) != XLATE_OK) {
3841 error = EINVAL;
3842 goto out;
3843 }
d445cc16 3844
1520ef4f
BP
3845 execute.actions = odp_actions.data;
3846 execute.actions_len = odp_actions.size;
a36de779 3847
cf62fa4c 3848 pkt_metadata_from_flow(&packet->md, flow);
08edf837 3849 execute.packet = packet;
1cceb31b 3850 execute.flow = flow;
08edf837 3851 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
43f9ac0a 3852 execute.probe = false;
27130224 3853 execute.mtu = 0;
08edf837
BP
3854
3855 /* Fix up in_port. */
1df7f7aa 3856 ofproto_dpif_set_packet_odp_port(ofproto, flow->in_port.ofp_port, packet);
758c456d
JR
3857
3858 error = dpif_execute(ofproto->backer->dpif, &execute);
fff1b9c0 3859out:
e79a6c83 3860 xlate_out_uninit(&xout);
1520ef4f 3861 ofpbuf_uninit(&odp_actions);
9d6ac44e 3862
e79a6c83 3863 return error;
9d6ac44e
BP
3864}
3865
cdd42eda
JG
3866/* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3867 * 'flow' must reflect the data in 'packet'. */
3868int
3869ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
1f4a8933 3870 ovs_version_t version, const struct flow *flow,
cdd42eda
JG
3871 struct rule_dpif *rule,
3872 const struct ofpact *ofpacts, size_t ofpacts_len,
3873 struct dp_packet *packet)
3874{
1f4a8933 3875 return ofproto_dpif_execute_actions__(ofproto, version, flow, rule,
2d9b49dd 3876 ofpacts, ofpacts_len, 0, 0, packet);
cdd42eda
JG
3877}
3878
748eb2f5
JR
3879static void
3880rule_dpif_credit_stats__(struct rule_dpif *rule,
3881 const struct dpif_flow_stats *stats,
3882 bool credit_counts)
3883 OVS_REQUIRES(rule->stats_mutex)
3884{
3885 if (credit_counts) {
3886 rule->stats.n_packets += stats->n_packets;
3887 rule->stats.n_bytes += stats->n_bytes;
3888 }
3889 rule->stats.used = MAX(rule->stats.used, stats->used);
3890}
3891
e79a6c83
EJ
3892void
3893rule_dpif_credit_stats(struct rule_dpif *rule,
3894 const struct dpif_flow_stats *stats)
8f73d537 3895{
e79a6c83 3896 ovs_mutex_lock(&rule->stats_mutex);
39c94593 3897 if (OVS_UNLIKELY(rule->new_rule)) {
748eb2f5
JR
3898 ovs_mutex_lock(&rule->new_rule->stats_mutex);
3899 rule_dpif_credit_stats__(rule->new_rule, stats, rule->forward_counts);
3900 ovs_mutex_unlock(&rule->new_rule->stats_mutex);
39c94593 3901 } else {
748eb2f5 3902 rule_dpif_credit_stats__(rule, stats, true);
39c94593 3903 }
e79a6c83 3904 ovs_mutex_unlock(&rule->stats_mutex);
8f73d537
EJ
3905}
3906
888ac0d7
SH
3907/* Sets 'rule''s recirculation id. */
3908static void
3909rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3910 OVS_REQUIRES(rule->up.mutex)
3911{
e672ff9b
JR
3912 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
3913 if (rule->recirc_id == id) {
3914 /* Release the new reference to the same id. */
3915 recirc_free_id(id);
3916 } else {
3917 rule->recirc_id = id;
888ac0d7 3918 }
888ac0d7
SH
3919}
3920
3921/* Sets 'rule''s recirculation id. */
3922void
3923rule_set_recirc_id(struct rule *rule_, uint32_t id)
3924{
3925 struct rule_dpif *rule = rule_dpif_cast(rule_);
3926
3927 ovs_mutex_lock(&rule->up.mutex);
3928 rule_dpif_set_recirc_id(rule, id);
3929 ovs_mutex_unlock(&rule->up.mutex);
3930}
3931
44e0c35d 3932ovs_version_t
1f4a8933 3933ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto)
621b8064 3934{
44e0c35d 3935 ovs_version_t version;
621b8064 3936
f9cf9e57
JR
3937 /* Use memory_order_acquire to signify that any following memory accesses
3938 * can not be reordered to happen before this atomic read. This makes sure
3939 * all following reads relate to this or a newer version, but never to an
3940 * older version. */
3941 atomic_read_explicit(&ofproto->tables_version, &version,
3942 memory_order_acquire);
621b8064
JR
3943 return version;
3944}
3945
34dd0d78 3946/* The returned rule (if any) is valid at least until the next RCU quiescent
1e1e1d19
BP
3947 * period. If the rule needs to stay around longer, the caller should take
3948 * a reference.
2e0bded4
BP
3949 *
3950 * 'flow' is non-const to allow for temporary modifications during the lookup.
3951 * Any changes are restored before returning. */
6d328fa2 3952static struct rule_dpif *
44e0c35d 3953rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, ovs_version_t version,
621b8064 3954 uint8_t table_id, struct flow *flow,
1e1e1d19 3955 struct flow_wildcards *wc)
abe529af 3956{
6d328fa2 3957 struct classifier *cls = &ofproto->up.tables[table_id].cls;
1e1e1d19
BP
3958 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls, version,
3959 flow, wc)));
6d328fa2
SH
3960}
3961
a027899e
JR
3962void
3963ofproto_dpif_credit_table_stats(struct ofproto_dpif *ofproto, uint8_t table_id,
3964 uint64_t n_matches, uint64_t n_misses)
3965{
3966 struct oftable *tbl = &ofproto->up.tables[table_id];
3967 unsigned long orig;
3968
3969 if (n_matches) {
3970 atomic_add_relaxed(&tbl->n_matched, n_matches, &orig);
3971 }
3972 if (n_misses) {
3973 atomic_add_relaxed(&tbl->n_missed, n_misses, &orig);
3974 }
3975}
3976
39c94593
JR
3977/* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3978 * table '*table_id'. Returns the rule that was found, which may be one of the
3979 * special rules according to packet miss hadling. If 'may_packet_in' is
3980 * false, returning of the miss_rule (which issues packet ins for the
3981 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3982 * that were used during the lookup.
6d328fa2
SH
3983 *
3984 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3985 * if none is found then the table miss configuration for that table is
3986 * honored, which can result in additional lookups in other OpenFlow tables.
3987 * In this case the function updates '*table_id' to reflect the final OpenFlow
3988 * table that was searched.
3989 *
3990 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3991 * '*table_id'.
3992 *
1a7c0cd7 3993 * The rule is returned in '*rule', which is valid at least until the next
1e1e1d19
BP
3994 * RCU quiescent period. If the '*rule' needs to stay around longer, the
3995 * caller must take a reference.
34dd0d78
JR
3996 *
3997 * 'in_port' allows the lookup to take place as if the in port had the value
2e0bded4
BP
3998 * 'in_port'. This is needed for resubmit action support.
3999 *
4000 * 'flow' is non-const to allow for temporary modifications during the lookup.
4001 * Any changes are restored before returning. */
34dd0d78 4002struct rule_dpif *
18721c4a 4003rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
44e0c35d 4004 ovs_version_t version, struct flow *flow,
1e1e1d19 4005 struct flow_wildcards *wc,
18721c4a 4006 const struct dpif_flow_stats *stats,
34dd0d78 4007 uint8_t *table_id, ofp_port_t in_port,
a027899e
JR
4008 bool may_packet_in, bool honor_table_miss,
4009 struct xlate_cache *xcache)
6d328fa2 4010{
d8227eba 4011 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
34dd0d78
JR
4012 ofp_port_t old_in_port = flow->in_port.ofp_port;
4013 enum ofputil_table_miss miss_config;
4014 struct rule_dpif *rule;
6d328fa2
SH
4015 uint8_t next_id;
4016
d8227eba
JR
4017 /* We always unwildcard nw_frag (for IP), so they
4018 * need not be unwildcarded here. */
4019 if (flow->nw_frag & FLOW_NW_FRAG_ANY
ad99e2ed
BP
4020 && ofproto->up.frag_handling != OFPUTIL_FRAG_NX_MATCH) {
4021 if (ofproto->up.frag_handling == OFPUTIL_FRAG_NORMAL) {
d8227eba
JR
4022 /* We must pretend that transport ports are unavailable. */
4023 flow->tp_src = htons(0);
4024 flow->tp_dst = htons(0);
4025 } else {
ad99e2ed 4026 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
d8227eba 4027 * Use the drop_frags_rule (which cannot disappear). */
34dd0d78 4028 rule = ofproto->drop_frags_rule;
d8227eba
JR
4029 if (stats) {
4030 struct oftable *tbl = &ofproto->up.tables[*table_id];
4031 unsigned long orig;
4032
4033 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
4034 }
a027899e
JR
4035 if (xcache) {
4036 struct xc_entry *entry;
4037
4038 entry = xlate_cache_add_entry(xcache, XC_TABLE);
4039 entry->table.ofproto = ofproto;
4040 entry->table.id = *table_id;
4041 entry->table.match = true;
4042 }
34dd0d78 4043 return rule;
d8227eba
JR
4044 }
4045 }
4046
34dd0d78
JR
4047 /* Look up a flow with 'in_port' as the input port. Then restore the
4048 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
4049 * have surprising behavior). */
4050 flow->in_port.ofp_port = in_port;
4051
4052 /* Our current implementation depends on n_tables == N_TABLES, and
4053 * TBL_INTERNAL being the last table. */
4054 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
4055
4056 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
d8227eba 4057
6d328fa2
SH
4058 for (next_id = *table_id;
4059 next_id < ofproto->up.n_tables;
4060 next_id++, next_id += (next_id == TBL_INTERNAL))
4061 {
4062 *table_id = next_id;
1e1e1d19 4063 rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc);
d611866c
SH
4064 if (stats) {
4065 struct oftable *tbl = &ofproto->up.tables[next_id];
d611866c 4066 unsigned long orig;
afcea9ea 4067
34dd0d78 4068 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
afcea9ea 4069 stats->n_packets, &orig);
d611866c 4070 }
a027899e
JR
4071 if (xcache) {
4072 struct xc_entry *entry;
4073
4074 entry = xlate_cache_add_entry(xcache, XC_TABLE);
4075 entry->table.ofproto = ofproto;
4076 entry->table.id = next_id;
4077 entry->table.match = (rule != NULL);
4078 }
34dd0d78
JR
4079 if (rule) {
4080 goto out; /* Match. */
4081 }
4082 if (honor_table_miss) {
4083 miss_config = ofproto_table_get_miss_config(&ofproto->up,
4084 *table_id);
4085 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
4086 continue;
4087 }
4088 }
4089 break;
4090 }
4091 /* Miss. */
4092 rule = ofproto->no_packet_in_rule;
4093 if (may_packet_in) {
4094 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
4095 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
4096 struct ofport_dpif *port;
4097
e672ff9b 4098 port = ofp_port_to_ofport(ofproto, old_in_port);
34dd0d78 4099 if (!port) {
94783c7c 4100 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu32,
34dd0d78
JR
4101 old_in_port);
4102 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
4103 rule = ofproto->miss_rule;
6d328fa2 4104 }
34dd0d78
JR
4105 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
4106 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
4107 rule = ofproto->miss_rule;
6d328fa2
SH
4108 }
4109 }
d8227eba
JR
4110out:
4111 /* Restore port numbers, as they may have been modified above. */
4112 flow->tp_src = old_tp_src;
4113 flow->tp_dst = old_tp_dst;
34dd0d78
JR
4114 /* Restore the old in port. */
4115 flow->in_port.ofp_port = old_in_port;
6d328fa2 4116
34dd0d78 4117 return rule;
a2143702
BP
4118}
4119
70742c7f
EJ
4120static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
4121{
4122 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
4123}
4124
abe529af
BP
4125static struct rule *
4126rule_alloc(void)
4127{
3da29e32 4128 struct rule_dpif *rule = xzalloc(sizeof *rule);
abe529af
BP
4129 return &rule->up;
4130}
4131
4132static void
4133rule_dealloc(struct rule *rule_)
4134{
4135 struct rule_dpif *rule = rule_dpif_cast(rule_);
4136 free(rule);
4137}
4138
07659514 4139static enum ofperr
1b43cf9e 4140check_mask(struct ofproto_dpif *ofproto, const struct miniflow *flow)
07659514 4141{
95619d8c 4142 const struct odp_support *support;
07659514 4143 uint16_t ct_state, ct_zone;
efba5ae4 4144 ovs_u128 ct_label;
8e53fe8c 4145 uint32_t ct_mark;
07659514 4146
07a3cd5c 4147 support = &ofproto->backer->support.odp;
2a28ccc8 4148 ct_state = MINIFLOW_GET_U8(flow, ct_state);
d73ccab1
JR
4149
4150 /* Do not bother dissecting the flow further if the datapath supports all
4151 * the features we know of. */
11968381 4152 if (support->ct_state && support->ct_zone && support->ct_mark
d73ccab1
JR
4153 && support->ct_label && support->ct_state_nat
4154 && support->ct_orig_tuple) {
11968381
JS
4155 return ct_state & CS_UNSUPPORTED_MASK ? OFPERR_OFPBMC_BAD_MASK : 0;
4156 }
4157
1b43cf9e
JS
4158 ct_zone = MINIFLOW_GET_U16(flow, ct_zone);
4159 ct_mark = MINIFLOW_GET_U32(flow, ct_mark);
4160 ct_label = MINIFLOW_GET_U128(flow, ct_label);
07659514 4161
95619d8c
JS
4162 if ((ct_state && !support->ct_state)
4163 || (ct_state & CS_UNSUPPORTED_MASK)
7b27258c 4164 || ((ct_state & (CS_SRC_NAT | CS_DST_NAT)) && !support->ct_state_nat)
95619d8c
JS
4165 || (ct_zone && !support->ct_zone)
4166 || (ct_mark && !support->ct_mark)
2ff8484b 4167 || (!ovs_u128_is_zero(ct_label) && !support->ct_label)) {
95619d8c 4168 return OFPERR_OFPBMC_BAD_MASK;
07659514 4169 }
95619d8c 4170
d73ccab1
JR
4171 if (!support->ct_orig_tuple &&
4172 (MINIFLOW_GET_U8(flow, ct_nw_proto) ||
4173 MINIFLOW_GET_U16(flow, ct_tp_src) ||
4174 MINIFLOW_GET_U16(flow, ct_tp_dst) ||
4175 MINIFLOW_GET_U32(flow, ct_nw_src) ||
4176 MINIFLOW_GET_U32(flow, ct_nw_dst) ||
4177 !ovs_u128_is_zero(MINIFLOW_GET_U128(flow, ct_ipv6_src)) ||
4178 !ovs_u128_is_zero(MINIFLOW_GET_U128(flow, ct_ipv6_dst)))) {
4179 return OFPERR_OFPBMC_BAD_MASK;
4180 }
4181
07659514
JS
4182 return 0;
4183}
4184
caa5c156 4185static void
76c62d19 4186report_unsupported_act(const char *action, const char *detail)
caa5c156
BP
4187{
4188 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
76c62d19
JR
4189 VLOG_WARN_RL(&rl, "Rejecting %s action because datapath does not support"
4190 "%s%s (your kernel module may be out of date)",
4191 action, detail ? " " : "", detail ? detail : "");
caa5c156
BP
4192}
4193
1b43cf9e
JS
4194static enum ofperr
4195check_actions(const struct ofproto_dpif *ofproto,
4196 const struct rule_actions *const actions)
4197{
4198 const struct ofpact *ofpact;
76c62d19 4199 const struct odp_support *support = &ofproto->backer->support.odp;
1b43cf9e
JS
4200
4201 OFPACT_FOR_EACH (ofpact, actions->ofpacts, actions->ofpacts_len) {
76c62d19
JR
4202 if (ofpact->type == OFPACT_CT) {
4203 const struct ofpact_conntrack *ct;
4204 const struct ofpact *a;
1b43cf9e 4205
76c62d19 4206 ct = CONTAINER_OF(ofpact, struct ofpact_conntrack, ofpact);
1b43cf9e 4207
76c62d19
JR
4208 if (!support->ct_state) {
4209 report_unsupported_act("ct", "ct action");
4210 return OFPERR_OFPBAC_BAD_TYPE;
4211 }
4212 if ((ct->zone_imm || ct->zone_src.field) && !support->ct_zone) {
4213 report_unsupported_act("ct", "ct zones");
4214 return OFPERR_OFPBAC_BAD_ARGUMENT;
4215 }
4216 /* So far the force commit feature is implemented together with the
4217 * original direction tuple feature by all datapaths, so we use the
4218 * support flag for the 'ct_orig_tuple' to indicate support for the
4219 * force commit feature as well. */
4220 if ((ct->flags & NX_CT_F_FORCE) && !support->ct_orig_tuple) {
4221 report_unsupported_act("ct", "force commit");
4222 return OFPERR_OFPBAC_BAD_ARGUMENT;
4223 }
1b43cf9e 4224
76c62d19
JR
4225 OFPACT_FOR_EACH(a, ct->actions, ofpact_ct_get_action_len(ct)) {
4226 const struct mf_field *dst = ofpact_get_mf_dst(a);
1b43cf9e 4227
76c62d19
JR
4228 if (a->type == OFPACT_NAT && !support->ct_state_nat) {
4229 /* The backer doesn't seem to support the NAT bits in
4230 * 'ct_state': assume that it doesn't support the NAT
4231 * action. */
4232 report_unsupported_act("ct", "nat");
4233 return OFPERR_OFPBAC_BAD_TYPE;
4234 }
4235 if (dst && ((dst->id == MFF_CT_MARK && !support->ct_mark) ||
4236 (dst->id == MFF_CT_LABEL && !support->ct_label))) {
4237 report_unsupported_act("ct", "setting mark and/or label");
4238 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
4239 }
4240 }
4241 } else if (ofpact->type == OFPACT_RESUBMIT) {
4242 struct ofpact_resubmit *resubmit = ofpact_get_RESUBMIT(ofpact);
1b43cf9e 4243
76c62d19
JR
4244 if (resubmit->with_ct_orig && !support->ct_orig_tuple) {
4245 report_unsupported_act("resubmit",
4246 "ct original direction tuple");
7b27258c
DDP
4247 return OFPERR_OFPBAC_BAD_TYPE;
4248 }
1b43cf9e
JS
4249 }
4250 }
4251
4252 return 0;
4253}
4254
4255static enum ofperr
4256rule_check(struct rule *rule)
4257{
4258 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->ofproto);
4259 enum ofperr err;
4260
4261 err = check_mask(ofproto, &rule->cr.match.mask->masks);
4262 if (err) {
4263 return err;
4264 }
4265 return check_actions(ofproto, rule->actions);
4266}
4267
90bf1e07 4268static enum ofperr
abe529af 4269rule_construct(struct rule *rule_)
dc437090 4270 OVS_NO_THREAD_SAFETY_ANALYSIS
abe529af
BP
4271{
4272 struct rule_dpif *rule = rule_dpif_cast(rule_);
07659514
JS
4273 int error;
4274
4275 error = rule_check(rule_);
4276 if (error) {
4277 return error;
4278 }
4279
97ba2d36 4280 ovs_mutex_init_adaptive(&rule->stats_mutex);
1a4ec18d
JS
4281 rule->stats.n_packets = 0;
4282 rule->stats.n_bytes = 0;
4283 rule->stats.used = rule->up.modified;
888ac0d7 4284 rule->recirc_id = 0;
39c94593 4285 rule->new_rule = NULL;
748eb2f5 4286 rule->forward_counts = false;
888ac0d7 4287
abe529af
BP
4288 return 0;
4289}
4290
39c94593 4291static void
748eb2f5 4292rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_counts)
15aaf599 4293 OVS_REQUIRES(ofproto_mutex)
abe529af 4294{
9fbe253e 4295 struct rule_dpif *rule = rule_dpif_cast(rule_);
39c94593 4296
748eb2f5 4297 if (old_rule_) {
39c94593
JR
4298 struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
4299
4300 ovs_assert(!old_rule->new_rule);
4301
4302 /* Take a reference to the new rule, and refer all stats updates from
4303 * the old rule to the new rule. */
07a3cd5c 4304 ofproto_rule_ref(&rule->up);
39c94593
JR
4305
4306 ovs_mutex_lock(&old_rule->stats_mutex);
4307 ovs_mutex_lock(&rule->stats_mutex);
4308 old_rule->new_rule = rule; /* Forward future stats. */
748eb2f5
JR
4309 old_rule->forward_counts = forward_counts;
4310
4311 if (forward_counts) {
4312 rule->stats = old_rule->stats; /* Transfer stats to the new
4313 * rule. */
4314 } else {
4315 /* Used timestamp must be forwarded whenever a rule is modified. */
4316 rule->stats.used = old_rule->stats.used;
4317 }
39c94593
JR
4318 ovs_mutex_unlock(&rule->stats_mutex);
4319 ovs_mutex_unlock(&old_rule->stats_mutex);
4320 }
8037acb4
BP
4321}
4322
4323static void
4324rule_destruct(struct rule *rule_)
39c94593 4325 OVS_NO_THREAD_SAFETY_ANALYSIS
8037acb4
BP
4326{
4327 struct rule_dpif *rule = rule_dpif_cast(rule_);
888ac0d7 4328
9fbe253e 4329 ovs_mutex_destroy(&rule->stats_mutex);
39c94593
JR
4330 /* Release reference to the new rule, if any. */
4331 if (rule->new_rule) {
07a3cd5c 4332 ofproto_rule_unref(&rule->new_rule->up);
39c94593 4333 }
888ac0d7 4334 if (rule->recirc_id) {
e672ff9b 4335 recirc_free_id(rule->recirc_id);
888ac0d7 4336 }
abe529af
BP
4337}
4338
4339static void
dc437090
JR
4340rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4341 long long int *used)
abe529af
BP
4342{
4343 struct rule_dpif *rule = rule_dpif_cast(rule_);
abe529af 4344
9fbe253e 4345 ovs_mutex_lock(&rule->stats_mutex);
39c94593
JR
4346 if (OVS_UNLIKELY(rule->new_rule)) {
4347 rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4348 } else {
4349 *packets = rule->stats.n_packets;
4350 *bytes = rule->stats.n_bytes;
4351 *used = rule->stats.used;
4352 }
9fbe253e 4353 ovs_mutex_unlock(&rule->stats_mutex);
abe529af
BP
4354}
4355
1f4a8933
JR
4356struct ofproto_dpif_packet_out {
4357 struct xlate_cache xcache;
4358 struct ofpbuf odp_actions;
4359 struct recirc_refs rr;
4360 bool needs_help;
4361};
4362
4363
4364static struct ofproto_dpif_packet_out *
4365ofproto_dpif_packet_out_new(void)
4366{
4367 struct ofproto_dpif_packet_out *aux = xmalloc(sizeof *aux);
4368 xlate_cache_init(&aux->xcache);
4369 ofpbuf_init(&aux->odp_actions, 64);
4370 aux->rr = RECIRC_REFS_EMPTY_INITIALIZER;
4371 aux->needs_help = false;
4372
4373 return aux;
4374}
4375
4376static void
4377ofproto_dpif_packet_out_delete(struct ofproto_dpif_packet_out *aux)
4378{
4379 if (aux) {
4380 xlate_cache_uninit(&aux->xcache);
4381 ofpbuf_uninit(&aux->odp_actions);
4382 recirc_refs_unref(&aux->rr);
4383 free(aux);
4384 }
4385}
4386
4387static enum ofperr
4388packet_xlate(struct ofproto *ofproto_, struct ofproto_packet_out *opo)
4389 OVS_REQUIRES(ofproto_mutex)
4390{
4391 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4392 struct xlate_out xout;
4393 struct xlate_in xin;
4394 enum ofperr error = 0;
4395
4396 struct ofproto_dpif_packet_out *aux = ofproto_dpif_packet_out_new();
4397
4398 xlate_in_init(&xin, ofproto, opo->version, opo->flow,
4399 opo->flow->in_port.ofp_port, NULL, 0, opo->packet, NULL,
4400 &aux->odp_actions);
4401 xin.ofpacts = opo->ofpacts;
4402 xin.ofpacts_len = opo->ofpacts_len;
4403 /* No learning or stats, but collect side effects to xcache. */
4404 xin.allow_side_effects = false;
4405 xin.resubmit_stats = NULL;
4406 xin.xcache = &aux->xcache;
331c07ac 4407 xin.in_packet_out = true;
1f4a8933
JR
4408
4409 if (xlate_actions(&xin, &xout) != XLATE_OK) {
4410 error = OFPERR_OFPFMFC_UNKNOWN; /* Error processing actions. */
4411 goto error_out;
4412 } else {
4413 /* Prepare learn actions. */
4414 struct xc_entry *entry;
4415 struct ofpbuf entries = aux->xcache.entries;
4416
4417 XC_ENTRY_FOR_EACH (entry, &entries) {
4418 if (entry->type == XC_LEARN) {
4419 struct ofproto_flow_mod *ofm = entry->learn.ofm;
4420
4421 error = ofproto_flow_mod_learn_refresh(ofm);
4422 if (error) {
4423 goto error_out;
4424 }
4425 struct rule *rule = ofm->temp_rule;
4426 ofm->learn_adds_rule = (rule->state == RULE_INITIALIZED);
4427 if (ofm->learn_adds_rule) {
4428 /* If learning on a different bridge, must use its next
4429 * version number. */
4430 ofm->version = (rule->ofproto == ofproto_)
4431 ? opo->version : rule->ofproto->tables_version + 1;
4432 error = ofproto_flow_mod_learn_start(ofm);
4433 if (error) {
4434 goto error_out;
4435 }
4436 }
4437 }
4438 }
4439
4440 /* Success. */
4441 aux->needs_help = (xout.slow & SLOW_ACTION) != 0;
4442 recirc_refs_swap(&aux->rr, &xout.recircs); /* Hold recirc refs. */
4443 }
4444 xlate_out_uninit(&xout);
4445 opo->aux = aux;
4446 return 0;
4447
4448error_out:
4449 xlate_out_uninit(&xout);
4450 ofproto_dpif_packet_out_delete(aux);
4451 opo->aux = NULL;
4452 return error;
4453}
4454
6dd3c787
JR
4455static void
4456packet_xlate_revert(struct ofproto *ofproto OVS_UNUSED,
4457 struct ofproto_packet_out *opo)
4458 OVS_REQUIRES(ofproto_mutex)
4459{
4460 struct ofproto_dpif_packet_out *aux = opo->aux;
4461 ovs_assert(aux);
4462
4463 /* Revert the learned flows. */
4464 struct xc_entry *entry;
4465 struct ofpbuf entries = aux->xcache.entries;
4466
4467 XC_ENTRY_FOR_EACH (entry, &entries) {
4468 if (entry->type == XC_LEARN && entry->learn.ofm->learn_adds_rule) {
4469 ofproto_flow_mod_learn_revert(entry->learn.ofm);
4470 }
4471 }
4472
4473 ofproto_dpif_packet_out_delete(aux);
4474 opo->aux = NULL;
4475}
4476
1f4a8933
JR
4477/* Push stats and perform side effects of flow translation. */
4478static void
4479ofproto_dpif_xcache_execute(struct ofproto_dpif *ofproto,
4480 struct xlate_cache *xcache,
4481 const struct dpif_flow_stats *stats)
4482 OVS_REQUIRES(ofproto_mutex)
4483{
4484 struct xc_entry *entry;
4485 struct ofpbuf entries = xcache->entries;
4486
4487 XC_ENTRY_FOR_EACH (entry, &entries) {
4488 switch (entry->type) {
4489 case XC_LEARN:
4490 /* Finish the learned flows. */
4491 if (entry->learn.ofm->learn_adds_rule) {
4492 ofproto_flow_mod_learn_finish(entry->learn.ofm, &ofproto->up);
4493 }
4494 break;
4495 case XC_FIN_TIMEOUT:
4496 if (stats->tcp_flags & (TCP_FIN | TCP_RST)) {
4497 /* 'ofproto_mutex' already held */
4498 ofproto_rule_reduce_timeouts__(&entry->fin.rule->up,
4499 entry->fin.idle,
4500 entry->fin.hard);
4501 }
4502 break;
4503 /* All the rest can be dealt with by the xlate layer. */
4504 case XC_TABLE:
4505 case XC_RULE:
4506 case XC_BOND:
4507 case XC_NETDEV:
4508 case XC_NETFLOW:
4509 case XC_MIRROR:
4510 case XC_NORMAL:
4511 case XC_GROUP:
4512 case XC_TNL_NEIGH:
4513 case XC_CONTROLLER:
4514 xlate_push_stats_entry(entry, stats);
4515 break;
4516 default:
4517 OVS_NOT_REACHED();
4518 }
4519 }
4520}
4521
4522static void
4523packet_execute(struct ofproto *ofproto_, struct ofproto_packet_out *opo)
4524 OVS_REQUIRES(ofproto_mutex)
4525{
4526 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4527 struct dpif_flow_stats stats;
4528 struct dpif_execute execute;
4529
4530 struct ofproto_dpif_packet_out *aux = opo->aux;
4531 ovs_assert(aux);
4532
4533 /* Run the side effects from the xcache. */
4534 dpif_flow_stats_extract(opo->flow, opo->packet, time_msec(), &stats);
4535 ofproto_dpif_xcache_execute(ofproto, &aux->xcache, &stats);
4536
4537 execute.actions = aux->odp_actions.data;
4538 execute.actions_len = aux->odp_actions.size;
4539
4540 pkt_metadata_from_flow(&opo->packet->md, opo->flow);
4541 execute.packet = opo->packet;
4542 execute.flow = opo->flow;
4543 execute.needs_help = aux->needs_help;
4544 execute.probe = false;
4545 execute.mtu = 0;
4546
4547 /* Fix up in_port. */
4548 ofproto_dpif_set_packet_odp_port(ofproto, opo->flow->in_port.ofp_port,
4549 opo->packet);
4550
4551 dpif_execute(ofproto->backer->dpif, &execute);
4552 ofproto_dpif_packet_out_delete(aux);
4553 opo->aux = NULL;
4554}
4555
00430a3f
SH
4556static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4557{
4558 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4559}
4560
4561static struct ofgroup *
4562group_alloc(void)
4563{
4564 struct group_dpif *group = xzalloc(sizeof *group);
4565 return &group->up;
4566}
4567
4568static void
4569group_dealloc(struct ofgroup *group_)
4570{
4571 struct group_dpif *group = group_dpif_cast(group_);
4572 free(group);
4573}
4574
4575static void
4576group_construct_stats(struct group_dpif *group)
4577 OVS_REQUIRES(group->stats_mutex)
4578{
4579 group->packet_count = 0;
4580 group->byte_count = 0;
1e684d7d 4581
07a3cd5c
BP
4582 struct ofputil_bucket *bucket;
4583 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
1e684d7d
RW
4584 bucket->stats.packet_count = 0;
4585 bucket->stats.byte_count = 0;
00430a3f
SH
4586 }
4587}
4588
1e684d7d
RW
4589void
4590group_dpif_credit_stats(struct group_dpif *group,
4591 struct ofputil_bucket *bucket,
4592 const struct dpif_flow_stats *stats)
4593{
4594 ovs_mutex_lock(&group->stats_mutex);
4595 group->packet_count += stats->n_packets;
4596 group->byte_count += stats->n_bytes;
4597 if (bucket) {
4598 bucket->stats.packet_count += stats->n_packets;
4599 bucket->stats.byte_count += stats->n_bytes;
4600 } else { /* Credit to all buckets */
07a3cd5c
BP
4601 struct ofputil_bucket *bucket;
4602 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
1e684d7d
RW
4603 bucket->stats.packet_count += stats->n_packets;
4604 bucket->stats.byte_count += stats->n_bytes;
4605 }
4606 }
4607 ovs_mutex_unlock(&group->stats_mutex);
4608}
4609
00430a3f
SH
4610static enum ofperr
4611group_construct(struct ofgroup *group_)
4612{
4613 struct group_dpif *group = group_dpif_cast(group_);
5a070238 4614
97ba2d36 4615 ovs_mutex_init_adaptive(&group->stats_mutex);
00430a3f
SH
4616 ovs_mutex_lock(&group->stats_mutex);
4617 group_construct_stats(group);
4618 ovs_mutex_unlock(&group->stats_mutex);
4619 return 0;
4620}
4621
00430a3f
SH
4622static void
4623group_destruct(struct ofgroup *group_)
4624{
4625 struct group_dpif *group = group_dpif_cast(group_);
00430a3f
SH
4626 ovs_mutex_destroy(&group->stats_mutex);
4627}
4628
00430a3f
SH
4629static enum ofperr
4630group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4631{
4632 struct group_dpif *group = group_dpif_cast(group_);
4633
00430a3f
SH
4634 ovs_mutex_lock(&group->stats_mutex);
4635 ogs->packet_count = group->packet_count;
4636 ogs->byte_count = group->byte_count;
1e684d7d 4637
07a3cd5c
BP
4638 struct bucket_counter *bucket_stats = ogs->bucket_stats;
4639 struct ofputil_bucket *bucket;
4640 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
1e684d7d
RW
4641 bucket_stats->packet_count = bucket->stats.packet_count;
4642 bucket_stats->byte_count = bucket->stats.byte_count;
4643 bucket_stats++;
4644 }
00430a3f
SH
4645 ovs_mutex_unlock(&group->stats_mutex);
4646
4647 return 0;
4648}
f4fb341b 4649
809c7548
RW
4650/* If the group exists, this function increments the groups's reference count.
4651 *
07a3cd5c 4652 * Make sure to call ofproto_group_unref() after no longer needing to maintain
809c7548 4653 * a reference to the group. */
db88b35c 4654struct group_dpif *
76973237 4655group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
5d08a275 4656 ovs_version_t version, bool take_ref)
f4fb341b 4657{
76973237 4658 struct ofgroup *ofgroup = ofproto_group_lookup(&ofproto->up, group_id,
5d08a275 4659 version, take_ref);
db88b35c 4660 return ofgroup ? group_dpif_cast(ofgroup) : NULL;
f4fb341b 4661}
abe529af 4662\f
2eb79142
JG
4663/* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
4664 * supports a notion of an OAM flag, sets it if 'oam' is true.
52a90c29 4665 * May modify 'packet'.
abe529af 4666 * Returns 0 if successful, otherwise a positive errno value. */
91d6cd12 4667int
2eb79142
JG
4668ofproto_dpif_send_packet(const struct ofport_dpif *ofport, bool oam,
4669 struct dp_packet *packet)
abe529af 4670{
79e15b78 4671 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
abe529af
BP
4672 int error;
4673
2eb79142 4674 error = xlate_send_packet(ofport, oam, packet);
79e15b78 4675
0da3c61b 4676 ovs_mutex_lock(&ofproto->stats_mutex);
79e15b78 4677 ofproto->stats.tx_packets++;
cf62fa4c 4678 ofproto->stats.tx_bytes += dp_packet_size(packet);
0da3c61b 4679 ovs_mutex_unlock(&ofproto->stats_mutex);
abe529af
BP
4680 return error;
4681}
9583bc14 4682\f
b5cbbcf6
AZ
4683/* Return the version string of the datapath that backs up
4684 * this 'ofproto'.
4685 */
4686static const char *
4687get_datapath_version(const struct ofproto *ofproto_)
4688{
4689 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4690
4691 return ofproto->backer->dp_version_string;
4692}
4693
d4f6865c
DDP
4694static void
4695type_set_config(const char *type, const struct smap *other_config)
4696{
4697 struct dpif_backer *backer;
4698
4699 backer = shash_find_data(&all_dpif_backers, type);
4700 if (!backer) {
4701 /* This is not necessarily a problem, since backers are only
4702 * created on demand. */
4703 return;
4704 }
4705
4706 dpif_set_config(backer->dpif, other_config);
4707}
4708
2a7c4805
JP
4709static void
4710ct_flush(const struct ofproto *ofproto_, const uint16_t *zone)
4711{
4712 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4713
4714 ct_dpif_flush(ofproto->backer->dpif, zone);
4715}
4716
9583bc14
EJ
4717static bool
4718set_frag_handling(struct ofproto *ofproto_,
ad99e2ed 4719 enum ofputil_frag_handling frag_handling)
abe529af
BP
4720{
4721 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
ad99e2ed 4722 if (frag_handling != OFPUTIL_FRAG_REASM) {
2cc3c58e 4723 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7257b535
BP
4724 return true;
4725 } else {
4726 return false;
4727 }
abe529af
BP
4728}
4729
77ab5fd2
BP
4730static enum ofperr
4731nxt_resume(struct ofproto *ofproto_,
4732 const struct ofputil_packet_in_private *pin)
4733{
4734 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4735
4736 /* Translate pin into datapath actions. */
4737 uint64_t odp_actions_stub[1024 / 8];
4738 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
4739 enum slow_path_reason slow;
4740 enum ofperr error = xlate_resume(ofproto, pin, &odp_actions, &slow);
4741
4742 /* Steal 'pin->packet' and put it into a dp_packet. */
4743 struct dp_packet packet;
4744 dp_packet_init(&packet, pin->public.packet_len);
4745 dp_packet_put(&packet, pin->public.packet, pin->public.packet_len);
4746
1df7f7aa
NS
4747 pkt_metadata_from_flow(&packet.md, &pin->public.flow_metadata.flow);
4748
4749 /* Fix up in_port. */
4750 ofproto_dpif_set_packet_odp_port(ofproto,
4751 pin->public.flow_metadata.flow.in_port.ofp_port,
4752 &packet);
4753
10b3f505
NS
4754 struct flow headers;
4755 flow_extract(&packet, &headers);
4756
77ab5fd2
BP
4757 /* Execute the datapath actions on the packet. */
4758 struct dpif_execute execute = {
4759 .actions = odp_actions.data,
4760 .actions_len = odp_actions.size,
4761 .needs_help = (slow & SLOW_ACTION) != 0,
4762 .packet = &packet,
1cceb31b 4763 .flow = &headers,
77ab5fd2
BP
4764 };
4765 dpif_execute(ofproto->backer->dpif, &execute);
4766
4767 /* Clean up. */
4768 ofpbuf_uninit(&odp_actions);
4769 dp_packet_uninit(&packet);
4770
4771 return error;
4772}
6fca1ffb
BP
4773\f
4774/* NetFlow. */
4775
4776static int
4777set_netflow(struct ofproto *ofproto_,
4778 const struct netflow_options *netflow_options)
4779{
4780 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4781
4782 if (netflow_options) {
4783 if (!ofproto->netflow) {
4784 ofproto->netflow = netflow_create();
7bab1576 4785 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6fca1ffb
BP
4786 }
4787 return netflow_set_options(ofproto->netflow, netflow_options);
7bab1576
EJ
4788 } else if (ofproto->netflow) {
4789 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8e407f27 4790 netflow_unref(ofproto->netflow);
6fca1ffb 4791 ofproto->netflow = NULL;
6fca1ffb 4792 }
7bab1576
EJ
4793
4794 return 0;
6fca1ffb 4795}
abe529af
BP
4796
4797static void
4798get_netflow_ids(const struct ofproto *ofproto_,
4799 uint8_t *engine_type, uint8_t *engine_id)
4800{
4801 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4802
acf60855 4803 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
abe529af
BP
4804}
4805\f
d13ee228 4806struct ofproto_dpif *
abe529af
BP
4807ofproto_dpif_lookup(const char *name)
4808{
b44a10b7
BP
4809 struct ofproto_dpif *ofproto;
4810
4811 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4812 hash_string(name, 0), &all_ofproto_dpifs) {
4813 if (!strcmp(ofproto->up.name, name)) {
4814 return ofproto;
4815 }
4816 }
4817 return NULL;
abe529af
BP
4818}
4819
f0a3aa2e 4820static void
96e466a3 4821ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
0e15264f 4822 const char *argv[], void *aux OVS_UNUSED)
f0a3aa2e 4823{
490df1ef 4824 struct ofproto_dpif *ofproto;
f0a3aa2e 4825
96e466a3
EJ
4826 if (argc > 1) {
4827 ofproto = ofproto_dpif_lookup(argv[1]);
4828 if (!ofproto) {
bde9f75d 4829 unixctl_command_reply_error(conn, "no such bridge");
96e466a3
EJ
4830 return;
4831 }
509c0149 4832 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 4833 mac_learning_flush(ofproto->ml);
509c0149 4834 ovs_rwlock_unlock(&ofproto->ml->rwlock);
96e466a3
EJ
4835 } else {
4836 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
509c0149 4837 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 4838 mac_learning_flush(ofproto->ml);
509c0149 4839 ovs_rwlock_unlock(&ofproto->ml->rwlock);
96e466a3 4840 }
f0a3aa2e 4841 }
f0a3aa2e 4842
bde9f75d 4843 unixctl_command_reply(conn, "table successfully flushed");
f0a3aa2e
AA
4844}
4845
08fdcc12
FL
4846static void
4847ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4848 const char *argv[], void *aux OVS_UNUSED)
4849{
4850 struct ofproto_dpif *ofproto;
4851
4852 if (argc > 1) {
4853 ofproto = ofproto_dpif_lookup(argv[1]);
4854 if (!ofproto) {
4855 unixctl_command_reply_error(conn, "no such bridge");
4856 return;
4857 }
4858
4859 if (!mcast_snooping_enabled(ofproto->ms)) {
4860 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4861 return;
4862 }
4863 mcast_snooping_mdb_flush(ofproto->ms);
4864 } else {
4865 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4866 if (!mcast_snooping_enabled(ofproto->ms)) {
4867 continue;
4868 }
4869 mcast_snooping_mdb_flush(ofproto->ms);
4870 }
4871 }
4872
4873 unixctl_command_reply(conn, "table successfully flushed");
4874}
4875
46c88433
EJ
4876static struct ofport_dpif *
4877ofbundle_get_a_port(const struct ofbundle *bundle)
4878{
417e7e66 4879 return CONTAINER_OF(ovs_list_front(&bundle->ports), struct ofport_dpif,
46c88433
EJ
4880 bundle_node);
4881}
4882
abe529af 4883static void
0e15264f
BP
4884ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4885 const char *argv[], void *aux OVS_UNUSED)
abe529af
BP
4886{
4887 struct ds ds = DS_EMPTY_INITIALIZER;
4888 const struct ofproto_dpif *ofproto;
4889 const struct mac_entry *e;
4890
0e15264f 4891 ofproto = ofproto_dpif_lookup(argv[1]);
abe529af 4892 if (!ofproto) {
bde9f75d 4893 unixctl_command_reply_error(conn, "no such bridge");
abe529af
BP
4894 return;
4895 }
4896
4897 ds_put_cstr(&ds, " port VLAN MAC Age\n");
509c0149 4898 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
abe529af 4899 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
9d078ec2 4900 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
2f2b904f 4901 char name[OFP10_MAX_PORT_NAME_LEN];
ad28062f
BP
4902
4903 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4904 name, sizeof name);
4905 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4906 name, e->vlan, ETH_ADDR_ARGS(e->mac),
e764773c 4907 mac_entry_age(ofproto->ml, e));
abe529af 4908 }
509c0149 4909 ovs_rwlock_unlock(&ofproto->ml->rwlock);
bde9f75d 4910 unixctl_command_reply(conn, ds_cstr(&ds));
abe529af
BP
4911 ds_destroy(&ds);
4912}
4913
08fdcc12
FL
4914static void
4915ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4916 int argc OVS_UNUSED,
4917 const char *argv[],
4918 void *aux OVS_UNUSED)
4919{
4920 struct ds ds = DS_EMPTY_INITIALIZER;
4921 const struct ofproto_dpif *ofproto;
4922 const struct ofbundle *bundle;
4923 const struct mcast_group *grp;
4924 struct mcast_group_bundle *b;
4925 struct mcast_mrouter_bundle *mrouter;
4926
4927 ofproto = ofproto_dpif_lookup(argv[1]);
4928 if (!ofproto) {
4929 unixctl_command_reply_error(conn, "no such bridge");
4930 return;
4931 }
4932
4933 if (!mcast_snooping_enabled(ofproto->ms)) {
4934 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4935 return;
4936 }
4937
4938 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4939 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4940 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4941 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2f2b904f 4942 char name[OFP10_MAX_PORT_NAME_LEN];
08fdcc12
FL
4943
4944 bundle = b->port;
4945 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4946 name, sizeof name);
964a4d5f 4947 ds_put_format(&ds, "%5s %4d ", name, grp->vlan);
ac6d120f 4948 ipv6_format_mapped(&grp->addr, &ds);
964a4d5f 4949 ds_put_format(&ds, " %3d\n",
08fdcc12
FL
4950 mcast_bundle_age(ofproto->ms, b));
4951 }
4952 }
4953
4954 /* ports connected to multicast routers */
4955 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
2f2b904f 4956 char name[OFP10_MAX_PORT_NAME_LEN];
08fdcc12
FL
4957
4958 bundle = mrouter->port;
4959 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4960 name, sizeof name);
c4bb1f8b 4961 ds_put_format(&ds, "%5s %4d querier %3d\n",
08fdcc12
FL
4962 name, mrouter->vlan,
4963 mcast_mrouter_age(ofproto->ms, mrouter));
4964 }
4965 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4966 unixctl_command_reply(conn, ds_cstr(&ds));
4967 ds_destroy(&ds);
4968}
4969
27022416
JP
4970/* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4971 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4972 * to destroy 'ofproto_shash' and free the returned value. */
4973static const struct shash_node **
4974get_ofprotos(struct shash *ofproto_shash)
4975{
4976 const struct ofproto_dpif *ofproto;
4977
4978 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4979 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4980 shash_add_nocopy(ofproto_shash, name, ofproto);
4981 }
4982
4983 return shash_sort(ofproto_shash);
4984}
4985
4986static void
4987ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4988 const char *argv[] OVS_UNUSED,
4989 void *aux OVS_UNUSED)
4990{
4991 struct ds ds = DS_EMPTY_INITIALIZER;
4992 struct shash ofproto_shash;
4993 const struct shash_node **sorted_ofprotos;
4994 int i;
4995
4996 shash_init(&ofproto_shash);
4997 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4998 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4999 const struct shash_node *node = sorted_ofprotos[i];
5000 ds_put_format(&ds, "%s\n", node->name);
5001 }
5002
5003 shash_destroy(&ofproto_shash);
5004 free(sorted_ofprotos);
5005
5006 unixctl_command_reply(conn, ds_cstr(&ds));
5007 ds_destroy(&ds);
5008}
5009
a98b700d 5010static void
1c2f091b 5011show_dp_feature_bool(struct ds *ds, const char *feature, bool b)
a98b700d
AZ
5012{
5013 ds_put_format(ds, "%s: %s\n", feature, b ? "Yes" : "No");
5014}
5015
5016static void
1c2f091b 5017show_dp_feature_size_t(struct ds *ds, const char *feature, size_t s)
a98b700d
AZ
5018{
5019 ds_put_format(ds, "%s: %"PRIuSIZE"\n", feature, s);
5020}
5021
5022static void
5023dpif_show_support(const struct dpif_backer_support *support, struct ds *ds)
5024{
1c2f091b
AZ
5025#define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5026 show_dp_feature_##TYPE (ds, TITLE, support->NAME);
5027 DPIF_SUPPORT_FIELDS
5028#undef DPIF_SUPPORT_FIELD
5029
5030#define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5031 show_dp_feature_##TYPE (ds, TITLE, support->odp.NAME );
5032 ODP_SUPPORT_FIELDS
5033#undef ODP_SUPPORT_FIELD
a98b700d
AZ
5034}
5035
dc54ef36
EJ
5036static void
5037dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
27022416 5038{
dc54ef36 5039 const struct shash_node **ofprotos;
e79a6c83 5040 struct dpif_dp_stats dp_stats;
dc54ef36 5041 struct shash ofproto_shash;
09672174 5042 size_t i;
655ab909 5043
e79a6c83 5044 dpif_get_dp_stats(backer->dpif, &dp_stats);
dc54ef36 5045 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
e79a6c83 5046 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
dc54ef36 5047
dc54ef36
EJ
5048 shash_init(&ofproto_shash);
5049 ofprotos = get_ofprotos(&ofproto_shash);
5050 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5051 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5052 const struct shash_node **ports;
5053 size_t j;
0a740f48 5054
dc54ef36
EJ
5055 if (ofproto->backer != backer) {
5056 continue;
0a740f48 5057 }
27022416 5058
e79a6c83 5059 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
dc54ef36
EJ
5060
5061 ports = shash_sort(&ofproto->up.port_by_name);
5062 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5063 const struct shash_node *node = ports[j];
5064 struct ofport *ofport = node->data;
5065 struct smap config;
4e022ec0 5066 odp_port_t odp_port;
27022416 5067
dc54ef36
EJ
5068 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5069 ofport->ofp_port);
27022416 5070
dc54ef36 5071 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4e022ec0 5072 if (odp_port != ODPP_NONE) {
dc54ef36
EJ
5073 ds_put_format(ds, "%"PRIu32":", odp_port);
5074 } else {
5075 ds_put_cstr(ds, "none:");
5076 }
27022416 5077
dc54ef36 5078 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
27022416 5079
dc54ef36
EJ
5080 smap_init(&config);
5081 if (!netdev_get_config(ofport->netdev, &config)) {
5082 const struct smap_node **nodes;
5083 size_t i;
27022416 5084
dc54ef36
EJ
5085 nodes = smap_sort(&config);
5086 for (i = 0; i < smap_count(&config); i++) {
5087 const struct smap_node *node = nodes[i];
5088 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5089 node->key, node->value);
5090 }
5091 free(nodes);
27022416 5092 }
dc54ef36
EJ
5093 smap_destroy(&config);
5094
27022416 5095 ds_put_char(ds, ')');
dc54ef36 5096 ds_put_char(ds, '\n');
27022416 5097 }
dc54ef36 5098 free(ports);
27022416 5099 }
dc54ef36
EJ
5100 shash_destroy(&ofproto_shash);
5101 free(ofprotos);
27022416
JP
5102}
5103
5104static void
dc54ef36
EJ
5105ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5106 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
27022416
JP
5107{
5108 struct ds ds = DS_EMPTY_INITIALIZER;
dc54ef36
EJ
5109 const struct shash_node **backers;
5110 int i;
27022416 5111
dc54ef36
EJ
5112 backers = shash_sort(&all_dpif_backers);
5113 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5114 dpif_show_backer(backers[i]->data, &ds);
27022416 5115 }
dc54ef36 5116 free(backers);
27022416
JP
5117
5118 unixctl_command_reply(conn, ds_cstr(&ds));
5119 ds_destroy(&ds);
5120}
5121
5122static void
5123ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5124 int argc OVS_UNUSED, const char *argv[],
5125 void *aux OVS_UNUSED)
5126{
27022416 5127 const struct ofproto_dpif *ofproto;
ac64794a
BP
5128
5129 struct ds ds = DS_EMPTY_INITIALIZER;
04b541df 5130 bool verbosity = false;
ac64794a 5131
04b541df
GS
5132 struct dpif_port dpif_port;
5133 struct dpif_port_dump port_dump;
5134 struct hmap portno_names;
ac64794a
BP
5135
5136 struct dpif_flow_dump *flow_dump;
5137 struct dpif_flow_dump_thread *flow_dump_thread;
5138 struct dpif_flow f;
938eaa50 5139 int error;
27022416 5140
04b541df 5141 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
27022416
JP
5142 if (!ofproto) {
5143 unixctl_command_reply_error(conn, "no such bridge");
5144 return;
5145 }
5146
04b541df
GS
5147 if (argc > 2 && !strcmp(argv[1], "-m")) {
5148 verbosity = true;
5149 }
5150
5151 hmap_init(&portno_names);
5152 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5153 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5154 }
5155
1b849273 5156 ds_init(&ds);
64bb477f 5157 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
ac64794a
BP
5158 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5159 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
ef377a58
JR
5160 struct flow flow;
5161
5162 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5163 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
04d08d54
EJ
5164 continue;
5165 }
5166
70e5ed6f
JS
5167 if (verbosity) {
5168 odp_format_ufid(&f.ufid, &ds);
5169 ds_put_cstr(&ds, " ");
5170 }
ac64794a
BP
5171 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5172 &portno_names, &ds, verbosity);
1b849273 5173 ds_put_cstr(&ds, ", ");
ac64794a 5174 dpif_flow_stats_format(&f.stats, &ds);
27022416 5175 ds_put_cstr(&ds, ", actions:");
ac64794a 5176 format_odp_actions(&ds, f.actions, f.actions_len);
27022416
JP
5177 ds_put_char(&ds, '\n');
5178 }
ac64794a
BP
5179 dpif_flow_dump_thread_destroy(flow_dump_thread);
5180 error = dpif_flow_dump_destroy(flow_dump);
27022416 5181
938eaa50 5182 if (error) {
1b849273
JS
5183 ds_clear(&ds);
5184 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5185 unixctl_command_reply_error(conn, ds_cstr(&ds));
5186 } else {
5187 unixctl_command_reply(conn, ds_cstr(&ds));
5188 }
04b541df
GS
5189 odp_portno_names_destroy(&portno_names);
5190 hmap_destroy(&portno_names);
27022416
JP
5191 ds_destroy(&ds);
5192}
5193
abe529af 5194static void
a36de779
PS
5195ofproto_revalidate_all_backers(void)
5196{
5197 const struct shash_node **backers;
5198 int i;
5199
5200 backers = shash_sort(&all_dpif_backers);
5201 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5202 struct dpif_backer *backer = backers[i]->data;
5203 backer->need_revalidate = REV_RECONFIGURE;
5204 }
5205 free(backers);
5206}
5207
5208static void
5209disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5210 const char *argv[], void *aux OVS_UNUSED)
5211{
5212 if (!strcasecmp(argv[1], "off")) {
5213 ofproto_use_tnl_push_pop = false;
5214 unixctl_command_reply(conn, "Tunnel push-pop off");
5215 ofproto_revalidate_all_backers();
5216 } else if (!strcasecmp(argv[1], "on")) {
5217 ofproto_use_tnl_push_pop = true;
5218 unixctl_command_reply(conn, "Tunnel push-pop on");
5219 ofproto_revalidate_all_backers();
a6313b25
IM
5220 } else {
5221 unixctl_command_reply_error(conn, "Invalid argument");
a36de779
PS
5222 }
5223}
5224
aaca4fe0
WT
5225static void
5226disable_datapath_truncate(struct unixctl_conn *conn OVS_UNUSED,
5227 int argc OVS_UNUSED,
5228 const char *argv[] OVS_UNUSED,
5229 void *aux OVS_UNUSED)
5230{
5231 const struct shash_node **backers;
5232 int i;
5233
5234 backers = shash_sort(&all_dpif_backers);
5235 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5236 struct dpif_backer *backer = backers[i]->data;
5237 backer->support.trunc = false;
5238 }
5239 free(backers);
5240 unixctl_command_reply(conn, "Datapath truncate action diabled");
5241}
5242
bef503e8
AZ
5243static void
5244disable_datapath_clone(struct unixctl_conn *conn OVS_UNUSED,
5245 int argc, const char *argv[],
5246 void *aux OVS_UNUSED)
5247{
5248 struct ds ds = DS_EMPTY_INITIALIZER;
5249 const char *br = argv[argc -1];
5250 struct ofproto_dpif *ofproto;
5251
5252 ofproto = ofproto_dpif_lookup(br);
5253 if (!ofproto) {
5254 unixctl_command_reply_error(conn, "no such bridge");
5255 return;
5256 }
5257 xlate_disable_dp_clone(ofproto);
5258 udpif_flush(ofproto->backer->udpif);
5259 ds_put_format(&ds, "Datapath clone action disabled for bridge %s", br);
5260 unixctl_command_reply(conn, ds_cstr(&ds));
391097b6 5261 ds_destroy(&ds);
bef503e8
AZ
5262}
5263
a98b700d
AZ
5264static void
5265ofproto_unixctl_dpif_show_dp_features(struct unixctl_conn *conn,
5266 int argc, const char *argv[],
5267 void *aux OVS_UNUSED)
5268{
5269 struct ds ds = DS_EMPTY_INITIALIZER;
5270 const char *br = argv[argc -1];
5271 struct ofproto_dpif *ofproto = ofproto_dpif_lookup(br);
5272
5273 if (!ofproto) {
5274 unixctl_command_reply_error(conn, "no such bridge");
5275 return;
5276 }
5277
5278 dpif_show_support(&ofproto->backer->support, &ds);
5279 unixctl_command_reply(conn, ds_cstr(&ds));
5280}
5281
a36de779
PS
5282static void
5283ofproto_unixctl_init(void)
abe529af
BP
5284{
5285 static bool registered;
5286 if (registered) {
5287 return;
5288 }
5289 registered = true;
5290
96e466a3 5291 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
0e15264f
BP
5292 ofproto_unixctl_fdb_flush, NULL);
5293 unixctl_command_register("fdb/show", "bridge", 1, 1,
5294 ofproto_unixctl_fdb_show, NULL);
08fdcc12
FL
5295 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5296 ofproto_unixctl_mcast_snooping_flush, NULL);
5297 unixctl_command_register("mdb/show", "bridge", 1, 1,
5298 ofproto_unixctl_mcast_snooping_show, NULL);
27022416
JP
5299 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5300 ofproto_unixctl_dpif_dump_dps, NULL);
dc54ef36
EJ
5301 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5302 NULL);
a98b700d
AZ
5303 unixctl_command_register("dpif/show-dp-features", "bridge", 1, 1,
5304 ofproto_unixctl_dpif_show_dp_features, NULL);
04b541df 5305 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
27022416 5306 ofproto_unixctl_dpif_dump_flows, NULL);
a36de779
PS
5307
5308 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5309 disable_tnl_push_pop, NULL);
aaca4fe0
WT
5310
5311 unixctl_command_register("dpif/disable-truncate", "", 0, 0,
5312 disable_datapath_truncate, NULL);
bef503e8
AZ
5313
5314 unixctl_command_register("dpif/disable-dp-clone", "bridge", 1, 1,
5315 disable_datapath_clone, NULL);
abe529af
BP
5316}
5317\f
46c88433 5318static odp_port_t
4e022ec0 5319ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
e1b1d06a 5320{
e672ff9b 5321 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
4e022ec0 5322 return ofport ? ofport->odp_port : ODPP_NONE;
e1b1d06a
JP
5323}
5324
8449c4d6 5325struct ofport_dpif *
4e022ec0 5326odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
e1b1d06a
JP
5327{
5328 struct ofport_dpif *port;
5329
8449c4d6 5330 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
f9c0c3ec 5331 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
acf60855 5332 &backer->odp_to_ofport_map) {
e1b1d06a 5333 if (port->odp_port == odp_port) {
8449c4d6 5334 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855 5335 return port;
e1b1d06a
JP
5336 }
5337 }
5338
8449c4d6 5339 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855
JP
5340 return NULL;
5341}
5342
4e022ec0
AW
5343static ofp_port_t
5344odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
acf60855
JP
5345{
5346 struct ofport_dpif *port;
5347
5348 port = odp_port_to_ofport(ofproto->backer, odp_port);
6472ba11 5349 if (port && &ofproto->up == port->up.ofproto) {
acf60855
JP
5350 return port->up.ofp_port;
5351 } else {
5352 return OFPP_NONE;
5353 }
e1b1d06a 5354}
655ab909 5355
adcf00ba
AZ
5356int
5357ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
fe99c360 5358 const struct match *match, int priority,
290ad78a 5359 uint16_t idle_timeout,
adcf00ba
AZ
5360 const struct ofpbuf *ofpacts,
5361 struct rule **rulep)
5362{
7338102b 5363 struct ofputil_flow_mod fm;
adcf00ba
AZ
5364 struct rule_dpif *rule;
5365 int error;
5366
7338102b 5367 fm = (struct ofputil_flow_mod) {
c184807c 5368 .buffer_id = UINT32_MAX,
39cc5c4a
BP
5369 .match = *match,
5370 .priority = priority,
5371 .table_id = TBL_INTERNAL,
5372 .command = OFPFC_ADD,
5373 .idle_timeout = idle_timeout,
5374 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5375 .ofpacts = ofpacts->data,
5376 .ofpacts_len = ofpacts->size,
39cc5c4a 5377 };
8be00367 5378
7338102b 5379 error = ofproto_flow_mod(&ofproto->up, &fm);
adcf00ba
AZ
5380 if (error) {
5381 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5382 ofperr_to_string(error));
5383 *rulep = NULL;
5384 return error;
5385 }
5386
621b8064
JR
5387 rule = rule_dpif_lookup_in_table(ofproto,
5388 ofproto_dpif_get_tables_version(ofproto),
7338102b
JR
5389 TBL_INTERNAL, &fm.match.flow,
5390 &fm.match.wc);
adcf00ba 5391 if (rule) {
adcf00ba
AZ
5392 *rulep = &rule->up;
5393 } else {
5394 OVS_NOT_REACHED();
5395 }
5396 return 0;
5397}
5398
5399int
5400ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5401 struct match *match, int priority)
5402{
7338102b 5403 struct ofputil_flow_mod fm;
adcf00ba
AZ
5404 int error;
5405
7338102b 5406 fm = (struct ofputil_flow_mod) {
c184807c 5407 .buffer_id = UINT32_MAX,
39cc5c4a
BP
5408 .match = *match,
5409 .priority = priority,
5410 .table_id = TBL_INTERNAL,
02a9757e
HH
5411 .out_port = OFPP_ANY,
5412 .out_group = OFPG_ANY,
39cc5c4a
BP
5413 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5414 .command = OFPFC_DELETE_STRICT,
5415 };
8be00367 5416
7338102b 5417 error = ofproto_flow_mod(&ofproto->up, &fm);
adcf00ba
AZ
5418 if (error) {
5419 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5420 ofperr_to_string(error));
5421 return error;
5422 }
5423
5424 return 0;
5425}
5426
5dddf960
JR
5427static void
5428meter_get_features(const struct ofproto *ofproto_,
5429 struct ofputil_meter_features *features)
5430{
5431 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5432
5433 dpif_meter_get_features(ofproto->backer->dpif, features);
5434}
5435
5436static enum ofperr
5437meter_set(struct ofproto *ofproto_, ofproto_meter_id *meter_id,
5438 struct ofputil_meter_config *config)
5439{
5440 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5441
5442 switch (dpif_meter_set(ofproto->backer->dpif, meter_id, config)) {
5443 case 0:
5444 return 0;
5445 case EFBIG: /* meter_id out of range */
5446 case ENOMEM: /* Cannot allocate meter */
5447 return OFPERR_OFPMMFC_OUT_OF_METERS;
5448 case EBADF: /* Unsupported flags */
5449 return OFPERR_OFPMMFC_BAD_FLAGS;
5450 case EINVAL: /* Too many bands */
5451 return OFPERR_OFPMMFC_OUT_OF_BANDS;
5452 case ENODEV: /* Unsupported band type */
5453 return OFPERR_OFPMMFC_BAD_BAND;
5454 default:
5455 return OFPERR_OFPMMFC_UNKNOWN;
5456 }
5457}
5458
5459static enum ofperr
5460meter_get(const struct ofproto *ofproto_, ofproto_meter_id meter_id,
5461 struct ofputil_meter_stats *stats, uint16_t n_bands)
5462{
5463 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5464
5465 if (!dpif_meter_get(ofproto->backer->dpif, meter_id, stats, n_bands)) {
5466 return 0;
5467 }
5468 return OFPERR_OFPMMFC_UNKNOWN_METER;
5469}
5470
5471static void
5472meter_del(struct ofproto *ofproto_, ofproto_meter_id meter_id)
5473{
5474 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5475
5476 dpif_meter_del(ofproto->backer->dpif, meter_id, NULL, 0);
5477}
5478
abe529af 5479const struct ofproto_class ofproto_dpif_class = {
b0408fca 5480 init,
abe529af
BP
5481 enumerate_types,
5482 enumerate_names,
5483 del,
0aeaabc8 5484 port_open_type,
acf60855 5485 type_run,
acf60855 5486 type_wait,
abe529af
BP
5487 alloc,
5488 construct,
5489 destruct,
5490 dealloc,
5491 run,
f07c97f5 5492 ofproto_dpif_wait,
e79a6c83 5493 NULL, /* get_memory_usage. */
1c030aa5 5494 type_get_memory_usage,
abe529af 5495 flush,
3c1bb396 5496 query_tables,
621b8064 5497 set_tables_version,
abe529af
BP
5498 port_alloc,
5499 port_construct,
5500 port_destruct,
5501 port_dealloc,
5502 port_modified,
5503 port_reconfigured,
5504 port_query_by_name,
5505 port_add,
5506 port_del,
91364d18 5507 port_set_config,
6527c598 5508 port_get_stats,
abe529af
BP
5509 port_dump_start,
5510 port_dump_next,
5511 port_dump_done,
5512 port_poll,
5513 port_poll_wait,
5514 port_is_lacp_current,
50b9699f 5515 port_get_lacp_stats,
0ab6decf 5516 NULL, /* rule_choose_table */
abe529af
BP
5517 rule_alloc,
5518 rule_construct,
8037acb4 5519 rule_insert,
1fc71871 5520 NULL, /* rule_delete */
abe529af
BP
5521 rule_destruct,
5522 rule_dealloc,
abe529af 5523 rule_get_stats,
1f4a8933 5524 packet_xlate,
6dd3c787 5525 packet_xlate_revert,
1f4a8933 5526 packet_execute,
7257b535 5527 set_frag_handling,
77ab5fd2 5528 nxt_resume,
abe529af
BP
5529 set_netflow,
5530 get_netflow_ids,
5531 set_sflow,
29089a54 5532 set_ipfix,
fb8f22c1 5533 get_ipfix_stats,
abe529af 5534 set_cfm,
8f5514fe 5535 cfm_status_changed,
9a9e3786 5536 get_cfm_status,
0477baa9
DF
5537 set_lldp,
5538 get_lldp_status,
5539 set_aa,
5540 aa_mapping_set,
5541 aa_mapping_unset,
5542 aa_vlan_get_queued,
5543 aa_vlan_get_queue_size,
ccc09689 5544 set_bfd,
8f5514fe 5545 bfd_status_changed,
ccc09689 5546 get_bfd_status,
21f7563c
JP
5547 set_stp,
5548 get_stp_status,
5549 set_stp_port,
5550 get_stp_port_status,
fd28ce3a 5551 get_stp_port_stats,
9efd308e
DV
5552 set_rstp,
5553 get_rstp_status,
5554 set_rstp_port,
5555 get_rstp_port_status,
8b36f51e 5556 set_queues,
abe529af
BP
5557 bundle_set,
5558 bundle_remove,
ec7ceaed
EJ
5559 mirror_set__,
5560 mirror_get_stats__,
abe529af
BP
5561 set_flood_vlans,
5562 is_mirror_output_bundle,
8402c74b 5563 forward_bpdu_changed,
c4069512 5564 set_mac_table_config,
7c38d0a5
FL
5565 set_mcast_snooping,
5566 set_mcast_snooping_port,
5dddf960
JR
5567 meter_get_features,
5568 meter_set,
5569 meter_get,
5570 meter_del,
00430a3f
SH
5571 group_alloc, /* group_alloc */
5572 group_construct, /* group_construct */
5573 group_destruct, /* group_destruct */
5574 group_dealloc, /* group_dealloc */
ccb3bc08 5575 NULL, /* group_modify */
00430a3f 5576 group_get_stats, /* group_get_stats */
b5cbbcf6 5577 get_datapath_version, /* get_datapath_version */
d4f6865c 5578 type_set_config,
2a7c4805 5579 ct_flush, /* ct_flush */
abe529af 5580};