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