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