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