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