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