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