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