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