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