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