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