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