]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif.c
netdev: Clean up on "construct" error in netdev_open().
[mirror_ovs.git] / ofproto / ofproto-dpif.c
CommitLineData
abe529af 1/*
e09ee259 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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
BP
27#include "byte-order.h"
28#include "connmgr.h"
29#include "coverage.h"
30#include "cfm.h"
31#include "dpif.h"
32#include "dynamic-string.h"
33#include "fail-open.h"
34#include "hmapx.h"
35#include "lacp.h"
75a75043 36#include "learn.h"
abe529af 37#include "mac-learning.h"
816fd533 38#include "meta-flow.h"
abe529af 39#include "multipath.h"
0a740f48 40#include "netdev-vport.h"
abe529af
BP
41#include "netdev.h"
42#include "netlink.h"
43#include "nx-match.h"
44#include "odp-util.h"
1ac7c9bd 45#include "odp-execute.h"
abe529af
BP
46#include "ofp-util.h"
47#include "ofpbuf.h"
f25d0cf3 48#include "ofp-actions.h"
31a19d69 49#include "ofp-parse.h"
abe529af 50#include "ofp-print.h"
9d6ac44e 51#include "ofproto-dpif-governor.h"
29089a54 52#include "ofproto-dpif-ipfix.h"
ec7ceaed 53#include "ofproto-dpif-mirror.h"
bae473fe 54#include "ofproto-dpif-sflow.h"
9583bc14 55#include "ofproto-dpif-xlate.h"
abe529af 56#include "poll-loop.h"
0d085684 57#include "simap.h"
27022416 58#include "smap.h"
abe529af 59#include "timer.h"
b9ad7294 60#include "tunnel.h"
6c1491fb 61#include "unaligned.h"
abe529af
BP
62#include "unixctl.h"
63#include "vlan-bitmap.h"
64#include "vlog.h"
65
66VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
67
abe529af 68COVERAGE_DEFINE(ofproto_dpif_expired);
abe529af 69COVERAGE_DEFINE(facet_changed_rule);
abe529af
BP
70COVERAGE_DEFINE(facet_revalidate);
71COVERAGE_DEFINE(facet_unexpected);
9d6ac44e 72COVERAGE_DEFINE(facet_suppress);
9b1a48c2 73COVERAGE_DEFINE(subfacet_install_fail);
ada3a58d 74COVERAGE_DEFINE(packet_in_overflow);
3d9c5e58 75COVERAGE_DEFINE(flow_mod_overflow);
abe529af 76
46c88433
EJ
77/* Number of implemented OpenFlow tables. */
78enum { N_TABLES = 255 };
79enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
80BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
81
a088a1ff 82struct flow_miss;
ac35f9c8 83struct facet;
abe529af 84
29901626 85static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
bcd2633a
JP
86 const struct flow *,
87 struct flow_wildcards *wc);
abe529af 88
7e741ffb 89static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes);
b0f7b9b5 90
46c88433
EJ
91struct ofbundle {
92 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
93 struct ofproto_dpif *ofproto; /* Owning ofproto. */
94 void *aux; /* Key supplied by ofproto's client. */
95 char *name; /* Identifier for log messages. */
96
97 /* Configuration. */
98 struct list ports; /* Contains "struct ofport"s. */
99 enum port_vlan_mode vlan_mode; /* VLAN mode */
100 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
101 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
102 * NULL if all VLANs are trunked. */
103 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
104 struct bond *bond; /* Nonnull iff more than one port. */
105 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
106
107 /* Status. */
108 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
109};
110
abe529af 111static void bundle_remove(struct ofport *);
7bde8dd8 112static void bundle_update(struct ofbundle *);
abe529af
BP
113static void bundle_destroy(struct ofbundle *);
114static void bundle_del_port(struct ofport_dpif *);
115static void bundle_run(struct ofbundle *);
116static void bundle_wait(struct ofbundle *);
33158a18 117
21f7563c
JP
118static void stp_run(struct ofproto_dpif *ofproto);
119static void stp_wait(struct ofproto_dpif *ofproto);
851bf71d
EJ
120static int set_stp_port(struct ofport *,
121 const struct ofproto_port_stp_settings *);
21f7563c 122
6a7e895f
BP
123static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
124 enum slow_path_reason,
125 uint64_t *stub, size_t stub_size,
126 const struct nlattr **actionsp,
127 size_t *actions_lenp);
128
129/* A subfacet (see "struct subfacet" below) has three possible installation
130 * states:
131 *
132 * - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
133 * case just after the subfacet is created, just before the subfacet is
134 * destroyed, or if the datapath returns an error when we try to install a
135 * subfacet.
136 *
137 * - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
138 *
139 * - SF_SLOW_PATH: An action that sends every packet for the subfacet through
140 * ofproto_dpif is installed in the datapath.
141 */
142enum subfacet_path {
143 SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
144 SF_FAST_PATH, /* Full actions are installed. */
145 SF_SLOW_PATH, /* Send-to-userspace action is installed. */
146};
147
5f5fbd17
BP
148/* A dpif flow and actions associated with a facet.
149 *
150 * See also the large comment on struct facet. */
151struct subfacet {
152 /* Owners. */
153 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
154 struct list list_node; /* In struct facet's 'facets' list. */
155 struct facet *facet; /* Owning facet. */
04d08d54 156 struct dpif_backer *backer; /* Owning backer. */
5f5fbd17 157
5f5fbd17
BP
158 enum odp_key_fitness key_fitness;
159 struct nlattr *key;
160 int key_len;
161
162 long long int used; /* Time last used; time created if not used. */
655ab909 163 long long int created; /* Time created. */
5f5fbd17
BP
164
165 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
166 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
167
6a7e895f 168 enum subfacet_path path; /* Installed in datapath? */
5f5fbd17
BP
169};
170
1d85f9e5
JP
171#define SUBFACET_DESTROY_MAX_BATCH 50
172
a088a1ff 173static struct subfacet *subfacet_create(struct facet *, struct flow_miss *miss,
459b16a1 174 long long int now);
04d08d54 175static struct subfacet *subfacet_find(struct dpif_backer *,
acf60855 176 const struct nlattr *key, size_t key_len,
9566abf9 177 uint32_t key_hash);
5f5fbd17
BP
178static void subfacet_destroy(struct subfacet *);
179static void subfacet_destroy__(struct subfacet *);
04d08d54 180static void subfacet_destroy_batch(struct dpif_backer *,
1d85f9e5 181 struct subfacet **, int n);
5f5fbd17
BP
182static void subfacet_reset_dp_stats(struct subfacet *,
183 struct dpif_flow_stats *);
5f5fbd17
BP
184static void subfacet_update_stats(struct subfacet *,
185 const struct dpif_flow_stats *);
5f5fbd17 186static int subfacet_install(struct subfacet *,
4dff9097
EJ
187 const struct ofpbuf *odp_actions,
188 struct dpif_flow_stats *);
5f5fbd17
BP
189static void subfacet_uninstall(struct subfacet *);
190
bcd2633a 191/* A unique, non-overlapping instantiation of an OpenFlow flow.
b0f7b9b5
BP
192 *
193 * A facet associates a "struct flow", which represents the Open vSwitch
bcd2633a
JP
194 * userspace idea of an exact-match flow, with one or more subfacets.
195 * While the facet is created based on an exact-match flow, it is stored
196 * within the ofproto based on the wildcards that could be expressed
197 * based on the flow table and other configuration. (See the 'wc'
198 * description in "struct xlate_out" for more details.)
b0f7b9b5 199 *
bcd2633a
JP
200 * Each subfacet tracks the datapath's idea of the flow equivalent to
201 * the facet. When the kernel module (or other dpif implementation) and
202 * Open vSwitch userspace agree on the definition of a flow key, there
203 * is exactly one subfacet per facet. If the dpif implementation
204 * supports more-specific flow matching than userspace, however, a facet
205 * can have more than one subfacet. Examples include the dpif
206 * implementation not supporting the same wildcards as userspace or some
207 * distinction in flow that userspace simply doesn't understand.
208 *
209 * Flow expiration works in terms of subfacets, so a facet must have at
210 * least one subfacet or it will never expire, leaking memory. */
abe529af 211struct facet {
b0f7b9b5
BP
212 /* Owners. */
213 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
b7e2f6b3 214 struct ofproto_dpif *ofproto;
b0f7b9b5
BP
215
216 /* Owned data. */
217 struct list subfacets;
abe529af
BP
218 long long int used; /* Time last used; time created if not used. */
219
b0f7b9b5 220 /* Key. */
bcd2633a
JP
221 struct flow flow; /* Flow of the creating subfacet. */
222 struct cls_rule cr; /* In 'ofproto_dpif's facets classifier. */
b0f7b9b5 223
abe529af
BP
224 /* These statistics:
225 *
226 * - Do include packets and bytes sent "by hand", e.g. with
227 * dpif_execute().
228 *
229 * - Do include packets and bytes that were obtained from the datapath
b0f7b9b5 230 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
abe529af 231 * DPIF_FP_ZERO_STATS).
b0f7b9b5
BP
232 *
233 * - Do not include packets or bytes that can be obtained from the
234 * datapath for any existing subfacet.
abe529af
BP
235 */
236 uint64_t packet_count; /* Number of packets received. */
237 uint64_t byte_count; /* Number of bytes received. */
238
b0f7b9b5 239 /* Resubmit statistics. */
9d24de3b
JP
240 uint64_t prev_packet_count; /* Number of packets from last stats push. */
241 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
242 long long int prev_used; /* Used time from last stats push. */
abe529af 243
b0f7b9b5 244 /* Accounting. */
907a4c5e 245 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
b0f7b9b5 246 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
0e553d9c 247 uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
abe529af 248
bbafd73b 249 struct xlate_out xout;
b7e2f6b3 250 bool fail_open; /* Facet matched the fail open rule. */
4dff9097 251
26cd7e34
BP
252 /* Storage for a single subfacet, to reduce malloc() time and space
253 * overhead. (A facet always has at least one subfacet and in the common
bcd516b7
JP
254 * case has exactly one subfacet. However, 'one_subfacet' may not
255 * always be valid, since it could have been removed after newer
256 * subfacets were pushed onto the 'subfacets' list.) */
26cd7e34 257 struct subfacet one_subfacet;
6cf474d7
EJ
258
259 long long int learn_rl; /* Rate limiter for facet_learn(). */
abe529af
BP
260};
261
bcd2633a
JP
262static struct facet *facet_create(const struct flow_miss *, struct rule_dpif *,
263 struct xlate_out *,
264 struct dpif_flow_stats *);
15baa734 265static void facet_remove(struct facet *);
abe529af
BP
266static void facet_free(struct facet *);
267
bcd2633a 268static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
abe529af 269static struct facet *facet_lookup_valid(struct ofproto_dpif *,
bcd2633a 270 const struct flow *);
5bf64ade 271static bool facet_revalidate(struct facet *);
6814e51f 272static bool facet_check_consistency(struct facet *);
abe529af 273
15baa734 274static void facet_flush_stats(struct facet *);
abe529af 275
bbb5d219 276static void facet_reset_counters(struct facet *);
9dfb1f78 277static void facet_push_stats(struct facet *, bool may_learn);
3de9590b
BP
278static void facet_learn(struct facet *);
279static void facet_account(struct facet *);
8844e035 280static void push_all_stats(void);
abe529af
BP
281
282static bool facet_is_controller_flow(struct facet *);
283
46c88433
EJ
284struct ofport_dpif {
285 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
286 struct ofport up;
287
288 odp_port_t odp_port;
289 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
290 struct list bundle_node; /* In struct ofbundle's "ports" list. */
291 struct cfm *cfm; /* Connectivity Fault Management, if any. */
292 struct bfd *bfd; /* BFD, if any. */
46c88433
EJ
293 bool may_enable; /* May be enabled in bonds. */
294 bool is_tunnel; /* This port is a tunnel. */
295 long long int carrier_seq; /* Carrier status changes. */
296 struct ofport_dpif *peer; /* Peer if patch port. */
297
298 /* Spanning tree. */
299 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
300 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
301 long long int stp_state_entered;
302
55954f6e
EJ
303 /* Queue to DSCP mapping. */
304 struct ofproto_port_queue *qdscp;
305 size_t n_qdscp;
46c88433
EJ
306
307 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
308 *
309 * This is deprecated. It is only for compatibility with broken device
310 * drivers in old versions of Linux that do not properly support VLANs when
311 * VLAN devices are not used. When broken device drivers are no longer in
312 * widespread use, we will delete these interfaces. */
313 ofp_port_t realdev_ofp_port;
314 int vlandev_vid;
315};
316
52a90c29
BP
317/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
318 *
319 * This is deprecated. It is only for compatibility with broken device drivers
320 * in old versions of Linux that do not properly support VLANs when VLAN
321 * devices are not used. When broken device drivers are no longer in
322 * widespread use, we will delete these interfaces. */
323struct vlan_splinter {
324 struct hmap_node realdev_vid_node;
325 struct hmap_node vlandev_node;
4e022ec0
AW
326 ofp_port_t realdev_ofp_port;
327 ofp_port_t vlandev_ofp_port;
52a90c29
BP
328 int vid;
329};
330
52a90c29 331static void vsp_remove(struct ofport_dpif *);
4e022ec0 332static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
52a90c29 333
46c88433
EJ
334static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
335 ofp_port_t);
336
4e022ec0 337static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
46c88433 338 odp_port_t);
e1b1d06a 339
abe529af
BP
340static struct ofport_dpif *
341ofport_dpif_cast(const struct ofport *ofport)
342{
abe529af
BP
343 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
344}
345
346static void port_run(struct ofport_dpif *);
0aa66d6e 347static void port_run_fast(struct ofport_dpif *);
abe529af 348static void port_wait(struct ofport_dpif *);
8aee94b6 349static int set_bfd(struct ofport *, const struct smap *);
a5610457 350static int set_cfm(struct ofport *, const struct cfm_settings *);
6cbbf4fa 351static void ofport_update_peer(struct ofport_dpif *);
8fa4d1d0 352static void run_fast_rl(void);
3d9c5e58 353static int run_fast(struct ofproto *);
abe529af 354
7ee20df1
BP
355struct dpif_completion {
356 struct list list_node;
357 struct ofoperation *op;
358};
359
cdc3ab65
EJ
360/* Reasons that we might need to revalidate every facet, and corresponding
361 * coverage counters.
362 *
363 * A value of 0 means that there is no need to revalidate.
364 *
365 * It would be nice to have some cleaner way to integrate with coverage
366 * counters, but with only a few reasons I guess this is good enough for
367 * now. */
368enum revalidate_reason {
369 REV_RECONFIGURE = 1, /* Switch configuration changed. */
370 REV_STP, /* Spanning tree protocol port status change. */
4a1b8f30 371 REV_BOND, /* Bonding changed. */
cdc3ab65
EJ
372 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
373 REV_FLOW_TABLE, /* Flow table changed. */
30618594 374 REV_MAC_LEARNING, /* Mac learning changed. */
cdc3ab65
EJ
375 REV_INCONSISTENCY /* Facet self-check failed. */
376};
3c4a309c
BP
377COVERAGE_DEFINE(rev_reconfigure);
378COVERAGE_DEFINE(rev_stp);
4a1b8f30 379COVERAGE_DEFINE(rev_bond);
3c4a309c
BP
380COVERAGE_DEFINE(rev_port_toggled);
381COVERAGE_DEFINE(rev_flow_table);
30618594 382COVERAGE_DEFINE(rev_mac_learning);
3c4a309c
BP
383COVERAGE_DEFINE(rev_inconsistency);
384
8f73d537
EJ
385/* Drop keys are odp flow keys which have drop flows installed in the kernel.
386 * These are datapath flows which have no associated ofproto, if they did we
387 * would use facets. */
388struct drop_key {
389 struct hmap_node hmap_node;
390 struct nlattr *key;
391 size_t key_len;
392};
393
cdc3ab65
EJ
394struct avg_subfacet_rates {
395 double add_rate; /* Moving average of new flows created per minute. */
396 double del_rate; /* Moving average of flows deleted per minute. */
397};
398
399/* All datapaths of a given type share a single dpif backer instance. */
400struct dpif_backer {
401 char *type;
402 int refcount;
403 struct dpif *dpif;
404 struct timer next_expiration;
8449c4d6
EJ
405
406 struct ovs_rwlock odp_to_ofport_lock;
407 struct hmap odp_to_ofport_map OVS_GUARDED; /* ODP port to ofport map. */
cdc3ab65
EJ
408
409 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
410
411 /* Facet revalidation flags applying to facets which use this backer. */
412 enum revalidate_reason need_revalidate; /* Revalidate every facet. */
cdc3ab65
EJ
413
414 struct hmap drop_keys; /* Set of dropped odp keys. */
415 bool recv_set_enable; /* Enables or disables receiving packets. */
416
417 struct hmap subfacets;
418 struct governor *governor;
419
420 /* Subfacet statistics.
421 *
422 * These keep track of the total number of subfacets added and deleted and
423 * flow life span. They are useful for computing the flow rates stats
424 * exposed via "ovs-appctl dpif/show". The goal is to learn about
425 * traffic patterns in ways that we can use later to improve Open vSwitch
426 * performance in new situations. */
427 long long int created; /* Time when it is created. */
428 unsigned max_n_subfacet; /* Maximum number of flows */
429 unsigned avg_n_subfacet; /* Average number of flows. */
430 long long int avg_subfacet_life; /* Average life span of subfacets. */
431
432 /* The average number of subfacets... */
433 struct avg_subfacet_rates hourly; /* ...over the last hour. */
434 struct avg_subfacet_rates daily; /* ...over the last day. */
435 struct avg_subfacet_rates lifetime; /* ...over the switch lifetime. */
436 long long int last_minute; /* Last time 'hourly' was updated. */
437
438 /* Number of subfacets added or deleted since 'last_minute'. */
439 unsigned subfacet_add_count;
440 unsigned subfacet_del_count;
441
442 /* Number of subfacets added or deleted from 'created' to 'last_minute.' */
443 unsigned long long int total_subfacet_add_count;
444 unsigned long long int total_subfacet_del_count;
445};
446
acf60855
JP
447/* All existing ofproto_backer instances, indexed by ofproto->up.type. */
448static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
449
8f73d537 450static void drop_key_clear(struct dpif_backer *);
dc54ef36 451static void update_moving_averages(struct dpif_backer *backer);
735d7efb 452
46c88433
EJ
453struct ofproto_dpif {
454 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
455 struct ofproto up;
456 struct dpif_backer *backer;
457
458 /* Special OpenFlow rules. */
459 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
460 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
461 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
462
463 /* Bridging. */
464 struct netflow *netflow;
465 struct dpif_sflow *sflow;
466 struct dpif_ipfix *ipfix;
467 struct hmap bundles; /* Contains "struct ofbundle"s. */
468 struct mac_learning *ml;
469 bool has_bonded_bundles;
470 struct mbridge *mbridge;
471
472 /* Facets. */
473 struct classifier facets; /* Contains 'struct facet's. */
474 long long int consistency_rl;
475
46c88433
EJ
476 /* Support for debugging async flow mods. */
477 struct list completions;
478
479 struct netdev_stats stats; /* To account packets generated and consumed in
480 * userspace. */
481
482 /* Spanning tree. */
483 struct stp *stp;
484 long long int stp_last_tick;
485
486 /* VLAN splinters. */
13501305
EJ
487 struct ovs_mutex vsp_mutex;
488 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
489 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
46c88433
EJ
490
491 /* Ports. */
492 struct sset ports; /* Set of standard port names. */
493 struct sset ghost_ports; /* Ports with no datapath port. */
494 struct sset port_poll_set; /* Queued names for port_poll() reply. */
495 int port_poll_errno; /* Last errno for port_poll() reply. */
496
497 /* Per ofproto's dpif stats. */
498 uint64_t n_hit;
499 uint64_t n_missed;
3d9c5e58
EJ
500
501 /* Work queues. */
502 struct ovs_mutex flow_mod_mutex;
503 struct list flow_mods OVS_GUARDED;
504 size_t n_flow_mods OVS_GUARDED;
ada3a58d
EJ
505
506 struct ovs_mutex pin_mutex;
507 struct list pins OVS_GUARDED;
508 size_t n_pins OVS_GUARDED;
46c88433
EJ
509};
510
7ee20df1
BP
511/* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
512 * for debugging the asynchronous flow_mod implementation.) */
513static bool clogged;
514
6b35e630
JP
515/* By default, flows in the datapath are wildcarded (megaflows). They
516 * may be disabled with the "ovs-appctl dpif/disable-megaflows" command. */
517static bool enable_megaflows = true;
518
b44a10b7
BP
519/* All existing ofproto_dpif instances, indexed by ->up.name. */
520static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
521
abe529af
BP
522static void ofproto_dpif_unixctl_init(void);
523
46c88433
EJ
524static inline struct ofproto_dpif *
525ofproto_dpif_cast(const struct ofproto *ofproto)
526{
527 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
528 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
529}
530
531static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
532 ofp_port_t ofp_port);
ade6ad9c
EJ
533static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
534 const struct ofpbuf *packet, struct ds *);
46c88433 535
501f8d1f
BP
536/* Upcalls. */
537#define FLOW_MISS_MAX_BATCH 50
acf60855 538static int handle_upcalls(struct dpif_backer *, unsigned int max_batch);
abe529af
BP
539
540/* Flow expiration. */
acf60855 541static int expire(struct dpif_backer *);
abe529af 542
6fca1ffb
BP
543/* NetFlow. */
544static void send_netflow_active_timeouts(struct ofproto_dpif *);
545
abe529af 546/* Utilities. */
52a90c29 547static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
9583bc14 548
abe529af
BP
549/* Global variables. */
550static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
acf60855
JP
551
552/* Initial mappings of port to bridge mappings. */
553static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
46c88433 554
3d9c5e58
EJ
555/* Executes and takes ownership of 'fm'. */
556void
46c88433
EJ
557ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
558 struct ofputil_flow_mod *fm)
559{
3d9c5e58
EJ
560 ovs_mutex_lock(&ofproto->flow_mod_mutex);
561 if (ofproto->n_flow_mods > 1024) {
562 ovs_mutex_unlock(&ofproto->flow_mod_mutex);
563 COVERAGE_INC(flow_mod_overflow);
564 free(fm->ofpacts);
565 free(fm);
566 return;
567 }
568
569 list_push_back(&ofproto->flow_mods, &fm->list_node);
570 ofproto->n_flow_mods++;
571 ovs_mutex_unlock(&ofproto->flow_mod_mutex);
46c88433
EJ
572}
573
574void
575ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
576 struct ofputil_packet_in *pin)
577{
ada3a58d
EJ
578 ovs_mutex_lock(&ofproto->pin_mutex);
579 if (ofproto->n_pins > 1024) {
580 ovs_mutex_unlock(&ofproto->pin_mutex);
581 COVERAGE_INC(packet_in_overflow);
582 free(CONST_CAST(void *, pin->packet));
583 free(pin);
584 return;
585 }
586
587 list_push_back(&ofproto->pins, &pin->list_node);
588 ofproto->n_pins++;
589 ovs_mutex_unlock(&ofproto->pin_mutex);
46c88433 590}
abe529af
BP
591\f
592/* Factory functions. */
593
b0408fca 594static void
acf60855 595init(const struct shash *iface_hints)
b0408fca 596{
acf60855
JP
597 struct shash_node *node;
598
599 /* Make a local copy, since we don't own 'iface_hints' elements. */
600 SHASH_FOR_EACH(node, iface_hints) {
601 const struct iface_hint *orig_hint = node->data;
602 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
603
604 new_hint->br_name = xstrdup(orig_hint->br_name);
605 new_hint->br_type = xstrdup(orig_hint->br_type);
606 new_hint->ofp_port = orig_hint->ofp_port;
607
608 shash_add(&init_ofp_ports, node->name, new_hint);
609 }
b0408fca
JP
610}
611
abe529af
BP
612static void
613enumerate_types(struct sset *types)
614{
615 dp_enumerate_types(types);
616}
617
618static int
619enumerate_names(const char *type, struct sset *names)
620{
acf60855
JP
621 struct ofproto_dpif *ofproto;
622
623 sset_clear(names);
624 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
625 if (strcmp(type, ofproto->up.type)) {
626 continue;
627 }
628 sset_add(names, ofproto->up.name);
629 }
630
631 return 0;
abe529af
BP
632}
633
634static int
635del(const char *type, const char *name)
636{
637 struct dpif *dpif;
638 int error;
639
640 error = dpif_open(name, type, &dpif);
641 if (!error) {
642 error = dpif_delete(dpif);
643 dpif_close(dpif);
644 }
645 return error;
646}
647\f
0aeaabc8
JP
648static const char *
649port_open_type(const char *datapath_type, const char *port_type)
650{
651 return dpif_port_open_type(datapath_type, port_type);
652}
653
acf60855
JP
654/* Type functions. */
655
36beb9be
BP
656static void process_dpif_port_changes(struct dpif_backer *);
657static void process_dpif_all_ports_changed(struct dpif_backer *);
658static void process_dpif_port_change(struct dpif_backer *,
659 const char *devname);
660static void process_dpif_port_error(struct dpif_backer *, int error);
661
476cb42a
BP
662static struct ofproto_dpif *
663lookup_ofproto_dpif_by_port_name(const char *name)
664{
665 struct ofproto_dpif *ofproto;
666
667 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
668 if (sset_contains(&ofproto->ports, name)) {
669 return ofproto;
670 }
671 }
672
673 return NULL;
674}
675
acf60855
JP
676static int
677type_run(const char *type)
678{
e20b5746 679 static long long int push_timer = LLONG_MIN;
acf60855 680 struct dpif_backer *backer;
acf60855
JP
681
682 backer = shash_find_data(&all_dpif_backers, type);
683 if (!backer) {
684 /* This is not necessarily a problem, since backers are only
685 * created on demand. */
686 return 0;
687 }
688
689 dpif_run(backer->dpif);
690
e20b5746
EJ
691 /* The most natural place to push facet statistics is when they're pulled
692 * from the datapath. However, when there are many flows in the datapath,
693 * this expensive operation can occur so frequently, that it reduces our
694 * ability to quickly set up flows. To reduce the cost, we push statistics
695 * here instead. */
696 if (time_msec() > push_timer) {
697 push_timer = time_msec() + 2000;
698 push_all_stats();
699 }
700
40358701
GS
701 /* If vswitchd started with other_config:flow_restore_wait set as "true",
702 * and the configuration has now changed to "false", enable receiving
703 * packets from the datapath. */
704 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
36beb9be
BP
705 int error;
706
40358701
GS
707 backer->recv_set_enable = true;
708
709 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
710 if (error) {
711 VLOG_ERR("Failed to enable receiving packets in dpif.");
712 return error;
713 }
714 dpif_flow_flush(backer->dpif);
715 backer->need_revalidate = REV_RECONFIGURE;
716 }
717
a1616063 718 if (backer->need_revalidate) {
2cc3c58e 719 struct ofproto_dpif *ofproto;
a614d823
KM
720 struct simap_node *node;
721 struct simap tmp_backers;
722
723 /* Handle tunnel garbage collection. */
724 simap_init(&tmp_backers);
725 simap_swap(&backer->tnl_backers, &tmp_backers);
726
727 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
728 struct ofport_dpif *iter;
729
730 if (backer != ofproto->backer) {
731 continue;
732 }
733
734 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
3aa30359 735 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
a614d823
KM
736 const char *dp_port;
737
42943cde 738 if (!iter->is_tunnel) {
a614d823
KM
739 continue;
740 }
741
3aa30359
BP
742 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
743 namebuf, sizeof namebuf);
a614d823
KM
744 node = simap_find(&tmp_backers, dp_port);
745 if (node) {
746 simap_put(&backer->tnl_backers, dp_port, node->data);
747 simap_delete(&tmp_backers, node);
748 node = simap_find(&backer->tnl_backers, dp_port);
749 } else {
750 node = simap_find(&backer->tnl_backers, dp_port);
751 if (!node) {
4e022ec0 752 odp_port_t odp_port = ODPP_NONE;
a614d823
KM
753
754 if (!dpif_port_add(backer->dpif, iter->up.netdev,
755 &odp_port)) {
4e022ec0
AW
756 simap_put(&backer->tnl_backers, dp_port,
757 odp_to_u32(odp_port));
a614d823
KM
758 node = simap_find(&backer->tnl_backers, dp_port);
759 }
760 }
761 }
762
4e022ec0 763 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
42943cde
EJ
764 if (tnl_port_reconfigure(iter, iter->up.netdev,
765 iter->odp_port)) {
a614d823
KM
766 backer->need_revalidate = REV_RECONFIGURE;
767 }
768 }
769 }
770
771 SIMAP_FOR_EACH (node, &tmp_backers) {
4e022ec0 772 dpif_port_del(backer->dpif, u32_to_odp(node->data));
a614d823
KM
773 }
774 simap_destroy(&tmp_backers);
2cc3c58e
EJ
775
776 switch (backer->need_revalidate) {
777 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
778 case REV_STP: COVERAGE_INC(rev_stp); break;
4a1b8f30 779 case REV_BOND: COVERAGE_INC(rev_bond); break;
2cc3c58e
EJ
780 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
781 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
30618594 782 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
2cc3c58e
EJ
783 case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
784 }
f728af2e
BP
785 backer->need_revalidate = 0;
786
a1616063
EJ
787 /* Clear the drop_keys in case we should now be accepting some
788 * formerly dropped flows. */
789 drop_key_clear(backer);
790
2cc3c58e 791 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
f231418e 792 struct facet *facet, *next;
a1616063 793 struct ofport_dpif *ofport;
bcd2633a 794 struct cls_cursor cursor;
a1616063 795 struct ofbundle *bundle;
2cc3c58e
EJ
796
797 if (ofproto->backer != backer) {
798 continue;
799 }
800
dc24a00f 801 ovs_rwlock_wrlock(&xlate_rwlock);
89a8a7f0 802 xlate_ofproto_set(ofproto, ofproto->up.name,
ec89fc6f
EJ
803 ofproto->backer->dpif, ofproto->miss_rule,
804 ofproto->no_packet_in_rule, ofproto->ml,
9d189a50
EJ
805 ofproto->stp, ofproto->mbridge,
806 ofproto->sflow, ofproto->ipfix,
807 ofproto->up.frag_handling,
a1616063
EJ
808 ofproto->up.forward_bpdu,
809 connmgr_has_in_band(ofproto->up.connmgr),
9d189a50 810 ofproto->netflow != NULL);
46c88433 811
a1616063
EJ
812 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
813 xlate_bundle_set(ofproto, bundle, bundle->name,
814 bundle->vlan_mode, bundle->vlan,
815 bundle->trunks, bundle->use_priority_tags,
816 bundle->bond, bundle->lacp,
817 bundle->floodable);
818 }
819
820 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
9d189a50
EJ
821 int stp_port = ofport->stp_port
822 ? stp_port_no(ofport->stp_port)
823 : 0;
a1616063
EJ
824 xlate_ofport_set(ofproto, ofport->bundle, ofport,
825 ofport->up.ofp_port, ofport->odp_port,
826 ofport->up.netdev, ofport->cfm,
9d189a50 827 ofport->bfd, ofport->peer, stp_port,
55954f6e 828 ofport->qdscp, ofport->n_qdscp,
9d189a50
EJ
829 ofport->up.pp.config, ofport->is_tunnel,
830 ofport->may_enable);
46c88433 831 }
dc24a00f 832 ovs_rwlock_unlock(&xlate_rwlock);
46c88433 833
0b4f2078
EJ
834 /* Only ofproto-dpif cares about the facet classifier so we just
835 * lock cls_cursor_init() to appease the thread safety analysis. */
836 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
bcd2633a 837 cls_cursor_init(&cursor, &ofproto->facets, NULL);
0b4f2078 838 ovs_rwlock_unlock(&ofproto->facets.rwlock);
bcd2633a 839 CLS_CURSOR_FOR_EACH_SAFE (facet, next, cr, &cursor) {
a1616063
EJ
840 facet_revalidate(facet);
841 run_fast_rl();
2cc3c58e
EJ
842 }
843 }
2cc3c58e
EJ
844 }
845
40358701
GS
846 if (!backer->recv_set_enable) {
847 /* Wake up before a max of 1000ms. */
848 timer_set_duration(&backer->next_expiration, 1000);
849 } else if (timer_expired(&backer->next_expiration)) {
acf60855
JP
850 int delay = expire(backer);
851 timer_set_duration(&backer->next_expiration, delay);
852 }
853
36beb9be 854 process_dpif_port_changes(backer);
acf60855 855
04d08d54
EJ
856 if (backer->governor) {
857 size_t n_subfacets;
858
859 governor_run(backer->governor);
860
861 /* If the governor has shrunk to its minimum size and the number of
862 * subfacets has dwindled, then drop the governor entirely.
863 *
864 * For hysteresis, the number of subfacets to drop the governor is
865 * smaller than the number needed to trigger its creation. */
866 n_subfacets = hmap_count(&backer->subfacets);
867 if (n_subfacets * 4 < flow_eviction_threshold
868 && governor_is_idle(backer->governor)) {
869 governor_destroy(backer->governor);
870 backer->governor = NULL;
871 }
872 }
873
acf60855
JP
874 return 0;
875}
876
36beb9be
BP
877/* Check for and handle port changes in 'backer''s dpif. */
878static void
879process_dpif_port_changes(struct dpif_backer *backer)
880{
881 for (;;) {
882 char *devname;
883 int error;
884
885 error = dpif_port_poll(backer->dpif, &devname);
886 switch (error) {
887 case EAGAIN:
888 return;
889
890 case ENOBUFS:
891 process_dpif_all_ports_changed(backer);
892 break;
893
894 case 0:
895 process_dpif_port_change(backer, devname);
896 free(devname);
897 break;
898
899 default:
900 process_dpif_port_error(backer, error);
901 break;
902 }
903 }
904}
905
906static void
907process_dpif_all_ports_changed(struct dpif_backer *backer)
908{
909 struct ofproto_dpif *ofproto;
910 struct dpif_port dpif_port;
911 struct dpif_port_dump dump;
912 struct sset devnames;
913 const char *devname;
914
915 sset_init(&devnames);
916 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
917 if (ofproto->backer == backer) {
918 struct ofport *ofport;
919
920 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
921 sset_add(&devnames, netdev_get_name(ofport->netdev));
922 }
923 }
924 }
925 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
926 sset_add(&devnames, dpif_port.name);
927 }
928
929 SSET_FOR_EACH (devname, &devnames) {
930 process_dpif_port_change(backer, devname);
931 }
932 sset_destroy(&devnames);
933}
934
935static void
936process_dpif_port_change(struct dpif_backer *backer, const char *devname)
937{
938 struct ofproto_dpif *ofproto;
939 struct dpif_port port;
940
941 /* Don't report on the datapath's device. */
942 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
943 return;
944 }
945
946 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
947 &all_ofproto_dpifs) {
948 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
949 return;
950 }
951 }
952
953 ofproto = lookup_ofproto_dpif_by_port_name(devname);
954 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
955 /* The port was removed. If we know the datapath,
956 * report it through poll_set(). If we don't, it may be
957 * notifying us of a removal we initiated, so ignore it.
958 * If there's a pending ENOBUFS, let it stand, since
959 * everything will be reevaluated. */
960 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
961 sset_add(&ofproto->port_poll_set, devname);
962 ofproto->port_poll_errno = 0;
963 }
964 } else if (!ofproto) {
965 /* The port was added, but we don't know with which
966 * ofproto we should associate it. Delete it. */
967 dpif_port_del(backer->dpif, port.port_no);
74cc3969
BP
968 } else {
969 struct ofport_dpif *ofport;
970
971 ofport = ofport_dpif_cast(shash_find_data(
972 &ofproto->up.port_by_name, devname));
973 if (ofport
974 && ofport->odp_port != port.port_no
975 && !odp_port_to_ofport(backer, port.port_no))
976 {
977 /* 'ofport''s datapath port number has changed from
978 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
979 * structures to match. */
8449c4d6 980 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
74cc3969
BP
981 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
982 ofport->odp_port = port.port_no;
983 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
984 hash_odp_port(port.port_no));
8449c4d6 985 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
74cc3969
BP
986 backer->need_revalidate = REV_RECONFIGURE;
987 }
36beb9be
BP
988 }
989 dpif_port_destroy(&port);
990}
991
992/* Propagate 'error' to all ofprotos based on 'backer'. */
993static void
994process_dpif_port_error(struct dpif_backer *backer, int error)
995{
996 struct ofproto_dpif *ofproto;
997
998 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
999 if (ofproto->backer == backer) {
1000 sset_clear(&ofproto->port_poll_set);
1001 ofproto->port_poll_errno = error;
1002 }
1003 }
1004}
1005
acf60855 1006static int
8fa4d1d0 1007dpif_backer_run_fast(struct dpif_backer *backer, int max_batch)
acf60855 1008{
acf60855
JP
1009 unsigned int work;
1010
40358701
GS
1011 /* If recv_set_enable is false, we should not handle upcalls. */
1012 if (!backer->recv_set_enable) {
1013 return 0;
1014 }
1015
acf60855
JP
1016 /* Handle one or more batches of upcalls, until there's nothing left to do
1017 * or until we do a fixed total amount of work.
1018 *
1019 * We do work in batches because it can be much cheaper to set up a number
1020 * of flows and fire off their patches all at once. We do multiple batches
1021 * because in some cases handling a packet can cause another packet to be
1022 * queued almost immediately as part of the return flow. Both
1023 * optimizations can make major improvements on some benchmarks and
1024 * presumably for real traffic as well. */
1025 work = 0;
8fa4d1d0
EJ
1026 while (work < max_batch) {
1027 int retval = handle_upcalls(backer, max_batch - work);
acf60855
JP
1028 if (retval <= 0) {
1029 return -retval;
1030 }
1031 work += retval;
1032 }
1033
1034 return 0;
1035}
1036
8fa4d1d0
EJ
1037static int
1038type_run_fast(const char *type)
1039{
1040 struct dpif_backer *backer;
1041
1042 backer = shash_find_data(&all_dpif_backers, type);
1043 if (!backer) {
1044 /* This is not necessarily a problem, since backers are only
1045 * created on demand. */
1046 return 0;
1047 }
1048
1049 return dpif_backer_run_fast(backer, FLOW_MISS_MAX_BATCH);
1050}
1051
1052static void
1053run_fast_rl(void)
1054{
1055 static long long int port_rl = LLONG_MIN;
1056 static unsigned int backer_rl = 0;
1057
1058 if (time_msec() >= port_rl) {
1059 struct ofproto_dpif *ofproto;
8fa4d1d0
EJ
1060
1061 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3d9c5e58 1062 run_fast(&ofproto->up);
8fa4d1d0
EJ
1063 }
1064 port_rl = time_msec() + 200;
1065 }
1066
1067 /* XXX: We have to be careful not to do too much work in this function. If
1068 * we call dpif_backer_run_fast() too often, or with too large a batch,
1069 * performance improves signifcantly, but at a cost. It's possible for the
1070 * number of flows in the datapath to increase without bound, and for poll
1071 * loops to take 10s of seconds. The correct solution to this problem,
1072 * long term, is to separate flow miss handling into it's own thread so it
1073 * isn't affected by revalidations, and expirations. Until then, this is
1074 * the best we can do. */
1075 if (++backer_rl >= 10) {
1076 struct shash_node *node;
1077
1078 backer_rl = 0;
1079 SHASH_FOR_EACH (node, &all_dpif_backers) {
1080 dpif_backer_run_fast(node->data, 1);
1081 }
1082 }
1083}
1084
acf60855
JP
1085static void
1086type_wait(const char *type)
1087{
1088 struct dpif_backer *backer;
1089
1090 backer = shash_find_data(&all_dpif_backers, type);
1091 if (!backer) {
1092 /* This is not necessarily a problem, since backers are only
1093 * created on demand. */
1094 return;
1095 }
1096
04d08d54
EJ
1097 if (backer->governor) {
1098 governor_wait(backer->governor);
1099 }
1100
acf60855
JP
1101 timer_wait(&backer->next_expiration);
1102}
1103\f
abe529af
BP
1104/* Basic life-cycle. */
1105
c57b2226
BP
1106static int add_internal_flows(struct ofproto_dpif *);
1107
abe529af
BP
1108static struct ofproto *
1109alloc(void)
1110{
1111 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
1112 return &ofproto->up;
1113}
1114
1115static void
1116dealloc(struct ofproto *ofproto_)
1117{
1118 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1119 free(ofproto);
1120}
1121
acf60855
JP
1122static void
1123close_dpif_backer(struct dpif_backer *backer)
1124{
1125 struct shash_node *node;
1126
cb22974d 1127 ovs_assert(backer->refcount > 0);
acf60855
JP
1128
1129 if (--backer->refcount) {
1130 return;
1131 }
1132
8f73d537
EJ
1133 drop_key_clear(backer);
1134 hmap_destroy(&backer->drop_keys);
1135
7d82ab2e 1136 simap_destroy(&backer->tnl_backers);
8449c4d6 1137 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
acf60855
JP
1138 hmap_destroy(&backer->odp_to_ofport_map);
1139 node = shash_find(&all_dpif_backers, backer->type);
1140 free(backer->type);
1141 shash_delete(&all_dpif_backers, node);
1142 dpif_close(backer->dpif);
1143
04d08d54
EJ
1144 ovs_assert(hmap_is_empty(&backer->subfacets));
1145 hmap_destroy(&backer->subfacets);
1146 governor_destroy(backer->governor);
1147
acf60855
JP
1148 free(backer);
1149}
1150
1151/* Datapath port slated for removal from datapath. */
1152struct odp_garbage {
1153 struct list list_node;
4e022ec0 1154 odp_port_t odp_port;
acf60855
JP
1155};
1156
1157static int
1158open_dpif_backer(const char *type, struct dpif_backer **backerp)
1159{
1160 struct dpif_backer *backer;
1161 struct dpif_port_dump port_dump;
1162 struct dpif_port port;
1163 struct shash_node *node;
1164 struct list garbage_list;
1165 struct odp_garbage *garbage, *next;
1166 struct sset names;
1167 char *backer_name;
1168 const char *name;
1169 int error;
1170
1171 backer = shash_find_data(&all_dpif_backers, type);
1172 if (backer) {
1173 backer->refcount++;
1174 *backerp = backer;
1175 return 0;
1176 }
1177
1178 backer_name = xasprintf("ovs-%s", type);
1179
1180 /* Remove any existing datapaths, since we assume we're the only
1181 * userspace controlling the datapath. */
1182 sset_init(&names);
1183 dp_enumerate_names(type, &names);
1184 SSET_FOR_EACH(name, &names) {
1185 struct dpif *old_dpif;
1186
1187 /* Don't remove our backer if it exists. */
1188 if (!strcmp(name, backer_name)) {
1189 continue;
1190 }
1191
1192 if (dpif_open(name, type, &old_dpif)) {
1193 VLOG_WARN("couldn't open old datapath %s to remove it", name);
1194 } else {
1195 dpif_delete(old_dpif);
1196 dpif_close(old_dpif);
1197 }
1198 }
1199 sset_destroy(&names);
1200
1201 backer = xmalloc(sizeof *backer);
1202
1203 error = dpif_create_and_open(backer_name, type, &backer->dpif);
1204 free(backer_name);
1205 if (error) {
1206 VLOG_ERR("failed to open datapath of type %s: %s", type,
10a89ef0 1207 ovs_strerror(error));
4c1b1289 1208 free(backer);
acf60855
JP
1209 return error;
1210 }
1211
1212 backer->type = xstrdup(type);
04d08d54 1213 backer->governor = NULL;
acf60855
JP
1214 backer->refcount = 1;
1215 hmap_init(&backer->odp_to_ofport_map);
8449c4d6 1216 ovs_rwlock_init(&backer->odp_to_ofport_lock);
8f73d537 1217 hmap_init(&backer->drop_keys);
04d08d54 1218 hmap_init(&backer->subfacets);
acf60855 1219 timer_set_duration(&backer->next_expiration, 1000);
2cc3c58e 1220 backer->need_revalidate = 0;
7d82ab2e 1221 simap_init(&backer->tnl_backers);
40358701 1222 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
acf60855
JP
1223 *backerp = backer;
1224
40358701
GS
1225 if (backer->recv_set_enable) {
1226 dpif_flow_flush(backer->dpif);
1227 }
acf60855
JP
1228
1229 /* Loop through the ports already on the datapath and remove any
1230 * that we don't need anymore. */
1231 list_init(&garbage_list);
1232 dpif_port_dump_start(&port_dump, backer->dpif);
1233 while (dpif_port_dump_next(&port_dump, &port)) {
1234 node = shash_find(&init_ofp_ports, port.name);
1235 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
1236 garbage = xmalloc(sizeof *garbage);
1237 garbage->odp_port = port.port_no;
1238 list_push_front(&garbage_list, &garbage->list_node);
1239 }
1240 }
1241 dpif_port_dump_done(&port_dump);
1242
1243 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
1244 dpif_port_del(backer->dpif, garbage->odp_port);
1245 list_remove(&garbage->list_node);
1246 free(garbage);
1247 }
1248
1249 shash_add(&all_dpif_backers, type, backer);
1250
40358701 1251 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
acf60855
JP
1252 if (error) {
1253 VLOG_ERR("failed to listen on datapath of type %s: %s",
10a89ef0 1254 type, ovs_strerror(error));
acf60855
JP
1255 close_dpif_backer(backer);
1256 return error;
1257 }
1258
dc54ef36
EJ
1259 backer->max_n_subfacet = 0;
1260 backer->created = time_msec();
1261 backer->last_minute = backer->created;
1262 memset(&backer->hourly, 0, sizeof backer->hourly);
1263 memset(&backer->daily, 0, sizeof backer->daily);
1264 memset(&backer->lifetime, 0, sizeof backer->lifetime);
1265 backer->subfacet_add_count = 0;
1266 backer->subfacet_del_count = 0;
1267 backer->total_subfacet_add_count = 0;
1268 backer->total_subfacet_del_count = 0;
1269 backer->avg_n_subfacet = 0;
1270 backer->avg_subfacet_life = 0;
1271
acf60855
JP
1272 return error;
1273}
1274
abe529af 1275static int
0f5f95a9 1276construct(struct ofproto *ofproto_)
abe529af
BP
1277{
1278 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855 1279 struct shash_node *node, *next;
4e022ec0 1280 odp_port_t max_ports;
abe529af 1281 int error;
abe529af 1282
acf60855 1283 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
abe529af 1284 if (error) {
abe529af
BP
1285 return error;
1286 }
1287
acf60855 1288 max_ports = dpif_get_max_ports(ofproto->backer->dpif);
4e022ec0
AW
1289 ofproto_init_max_ports(ofproto_, u16_to_ofp(MIN(odp_to_u32(max_ports),
1290 ofp_to_u16(OFPP_MAX))));
91858960 1291
abe529af
BP
1292 ofproto->netflow = NULL;
1293 ofproto->sflow = NULL;
29089a54 1294 ofproto->ipfix = NULL;
21f7563c 1295 ofproto->stp = NULL;
abe529af 1296 hmap_init(&ofproto->bundles);
e764773c 1297 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
ec7ceaed 1298 ofproto->mbridge = mbridge_create();
abe529af 1299 ofproto->has_bonded_bundles = false;
13501305 1300 ovs_mutex_init(&ofproto->vsp_mutex, PTHREAD_MUTEX_NORMAL);
abe529af 1301
bcd2633a 1302 classifier_init(&ofproto->facets);
4d2a0f39 1303 ofproto->consistency_rl = LLONG_MIN;
54a9cbc9 1304
7ee20df1
BP
1305 list_init(&ofproto->completions);
1306
3d9c5e58
EJ
1307 ovs_mutex_init(&ofproto->flow_mod_mutex, PTHREAD_MUTEX_NORMAL);
1308 ovs_mutex_lock(&ofproto->flow_mod_mutex);
1309 list_init(&ofproto->flow_mods);
1310 ofproto->n_flow_mods = 0;
1311 ovs_mutex_unlock(&ofproto->flow_mod_mutex);
1312
ada3a58d
EJ
1313 ovs_mutex_init(&ofproto->pin_mutex, PTHREAD_MUTEX_NORMAL);
1314 ovs_mutex_lock(&ofproto->pin_mutex);
1315 list_init(&ofproto->pins);
1316 ofproto->n_pins = 0;
1317 ovs_mutex_unlock(&ofproto->pin_mutex);
1318
abe529af
BP
1319 ofproto_dpif_unixctl_init();
1320
52a90c29
BP
1321 hmap_init(&ofproto->vlandev_map);
1322 hmap_init(&ofproto->realdev_vid_map);
1323
acf60855 1324 sset_init(&ofproto->ports);
0a740f48 1325 sset_init(&ofproto->ghost_ports);
acf60855
JP
1326 sset_init(&ofproto->port_poll_set);
1327 ofproto->port_poll_errno = 0;
1328
1329 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
4f9e08a5 1330 struct iface_hint *iface_hint = node->data;
acf60855
JP
1331
1332 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1333 /* Check if the datapath already has this port. */
1334 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1335 sset_add(&ofproto->ports, node->name);
1336 }
1337
1338 free(iface_hint->br_name);
1339 free(iface_hint->br_type);
4f9e08a5 1340 free(iface_hint);
acf60855
JP
1341 shash_delete(&init_ofp_ports, node);
1342 }
1343 }
e1b1d06a 1344
b44a10b7
BP
1345 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1346 hash_string(ofproto->up.name, 0));
6527c598 1347 memset(&ofproto->stats, 0, sizeof ofproto->stats);
0f5f95a9
BP
1348
1349 ofproto_init_tables(ofproto_, N_TABLES);
c57b2226
BP
1350 error = add_internal_flows(ofproto);
1351 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1352
735d7efb
AZ
1353 ofproto->n_hit = 0;
1354 ofproto->n_missed = 0;
1355
c57b2226
BP
1356 return error;
1357}
1358
1359static int
1360add_internal_flow(struct ofproto_dpif *ofproto, int id,
f25d0cf3 1361 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
c57b2226
BP
1362{
1363 struct ofputil_flow_mod fm;
1364 int error;
1365
81a76618
BP
1366 match_init_catchall(&fm.match);
1367 fm.priority = 0;
1368 match_set_reg(&fm.match, 0, id);
623e1caf 1369 fm.new_cookie = htonll(0);
c57b2226
BP
1370 fm.cookie = htonll(0);
1371 fm.cookie_mask = htonll(0);
23342857 1372 fm.modify_cookie = false;
c57b2226
BP
1373 fm.table_id = TBL_INTERNAL;
1374 fm.command = OFPFC_ADD;
1375 fm.idle_timeout = 0;
1376 fm.hard_timeout = 0;
1377 fm.buffer_id = 0;
1378 fm.out_port = 0;
1379 fm.flags = 0;
f25d0cf3
BP
1380 fm.ofpacts = ofpacts->data;
1381 fm.ofpacts_len = ofpacts->size;
c57b2226
BP
1382
1383 error = ofproto_flow_mod(&ofproto->up, &fm);
1384 if (error) {
1385 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1386 id, ofperr_to_string(error));
1387 return error;
1388 }
1389
9583bc14
EJ
1390 *rulep = rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL,
1391 TBL_INTERNAL);
cb22974d 1392 ovs_assert(*rulep != NULL);
0f5f95a9 1393
abe529af
BP
1394 return 0;
1395}
1396
c57b2226
BP
1397static int
1398add_internal_flows(struct ofproto_dpif *ofproto)
1399{
f25d0cf3
BP
1400 struct ofpact_controller *controller;
1401 uint64_t ofpacts_stub[128 / 8];
1402 struct ofpbuf ofpacts;
c57b2226
BP
1403 int error;
1404 int id;
1405
f25d0cf3 1406 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
c57b2226
BP
1407 id = 1;
1408
f25d0cf3
BP
1409 controller = ofpact_put_CONTROLLER(&ofpacts);
1410 controller->max_len = UINT16_MAX;
1411 controller->controller_id = 0;
1412 controller->reason = OFPR_NO_MATCH;
1413 ofpact_pad(&ofpacts);
1414
1415 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
c57b2226
BP
1416 if (error) {
1417 return error;
1418 }
1419
f25d0cf3
BP
1420 ofpbuf_clear(&ofpacts);
1421 error = add_internal_flow(ofproto, id++, &ofpacts,
c57b2226 1422 &ofproto->no_packet_in_rule);
7fd51d39
BP
1423 if (error) {
1424 return error;
1425 }
1426
1427 error = add_internal_flow(ofproto, id++, &ofpacts,
1428 &ofproto->drop_frags_rule);
c57b2226
BP
1429 return error;
1430}
1431
7ee20df1
BP
1432static void
1433complete_operations(struct ofproto_dpif *ofproto)
1434{
1435 struct dpif_completion *c, *next;
1436
1437 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
1438 ofoperation_complete(c->op, 0);
1439 list_remove(&c->list_node);
1440 free(c);
1441 }
1442}
1443
abe529af
BP
1444static void
1445destruct(struct ofproto *ofproto_)
1446{
1447 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7ee20df1 1448 struct rule_dpif *rule, *next_rule;
ada3a58d 1449 struct ofputil_flow_mod *pin, *next_pin;
3d9c5e58 1450 struct ofputil_flow_mod *fm, *next_fm;
d0918789 1451 struct oftable *table;
abe529af 1452
875b94ed 1453 ofproto->backer->need_revalidate = REV_RECONFIGURE;
dc24a00f 1454 ovs_rwlock_wrlock(&xlate_rwlock);
46c88433 1455 xlate_remove_ofproto(ofproto);
dc24a00f 1456 ovs_rwlock_unlock(&xlate_rwlock);
46c88433 1457
b44a10b7 1458 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
7ee20df1
BP
1459 complete_operations(ofproto);
1460
0697b5c3
BP
1461 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1462 struct cls_cursor cursor;
1463
0b4f2078 1464 ovs_rwlock_wrlock(&table->cls.rwlock);
d0918789 1465 cls_cursor_init(&cursor, &table->cls, NULL);
0697b5c3 1466 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
0b4f2078 1467 ofproto_rule_destroy(&ofproto->up, &table->cls, &rule->up);
0697b5c3 1468 }
0b4f2078 1469 ovs_rwlock_unlock(&table->cls.rwlock);
7ee20df1
BP
1470 }
1471
3d9c5e58
EJ
1472 ovs_mutex_lock(&ofproto->flow_mod_mutex);
1473 LIST_FOR_EACH_SAFE (fm, next_fm, list_node, &ofproto->flow_mods) {
1474 list_remove(&fm->list_node);
1475 ofproto->n_flow_mods--;
1476 free(fm->ofpacts);
1477 free(fm);
1478 }
1479 ovs_mutex_unlock(&ofproto->flow_mod_mutex);
1480 ovs_mutex_destroy(&ofproto->flow_mod_mutex);
1481
ada3a58d
EJ
1482 ovs_mutex_lock(&ofproto->pin_mutex);
1483 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &ofproto->pins) {
1484 list_remove(&pin->list_node);
1485 ofproto->n_pins--;
1486 free(pin->ofpacts);
1487 free(pin);
1488 }
1489 ovs_mutex_unlock(&ofproto->pin_mutex);
1490 ovs_mutex_destroy(&ofproto->pin_mutex);
1491
ec7ceaed 1492 mbridge_unref(ofproto->mbridge);
abe529af
BP
1493
1494 netflow_destroy(ofproto->netflow);
9723bcce 1495 dpif_sflow_unref(ofproto->sflow);
abe529af 1496 hmap_destroy(&ofproto->bundles);
5d989517 1497 mac_learning_unref(ofproto->ml);
abe529af 1498
bcd2633a 1499 classifier_destroy(&ofproto->facets);
abe529af 1500
52a90c29
BP
1501 hmap_destroy(&ofproto->vlandev_map);
1502 hmap_destroy(&ofproto->realdev_vid_map);
1503
acf60855 1504 sset_destroy(&ofproto->ports);
0a740f48 1505 sset_destroy(&ofproto->ghost_ports);
acf60855 1506 sset_destroy(&ofproto->port_poll_set);
e1b1d06a 1507
13501305
EJ
1508 ovs_mutex_destroy(&ofproto->vsp_mutex);
1509
acf60855 1510 close_dpif_backer(ofproto->backer);
abe529af
BP
1511}
1512
1513static int
5fcc0d00 1514run_fast(struct ofproto *ofproto_)
abe529af
BP
1515{
1516 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
ada3a58d
EJ
1517 struct ofputil_packet_in *pin, *next_pin;
1518 struct ofputil_flow_mod *fm, *next_fm;
1519 struct list flow_mods, pins;
0aa66d6e 1520 struct ofport_dpif *ofport;
abe529af 1521
40358701
GS
1522 /* Do not perform any periodic activity required by 'ofproto' while
1523 * waiting for flow restore to complete. */
1524 if (ofproto_get_flow_restore_wait()) {
1525 return 0;
1526 }
1527
3d9c5e58
EJ
1528 ovs_mutex_lock(&ofproto->flow_mod_mutex);
1529 if (ofproto->n_flow_mods) {
1530 flow_mods = ofproto->flow_mods;
1531 list_moved(&flow_mods);
1532 list_init(&ofproto->flow_mods);
1533 ofproto->n_flow_mods = 0;
1534 } else {
1535 list_init(&flow_mods);
1536 }
1537 ovs_mutex_unlock(&ofproto->flow_mod_mutex);
1538
ada3a58d 1539 LIST_FOR_EACH_SAFE (fm, next_fm, list_node, &flow_mods) {
3d9c5e58
EJ
1540 int error = ofproto_flow_mod(&ofproto->up, fm);
1541 if (error && !VLOG_DROP_WARN(&rl)) {
1542 VLOG_WARN("learning action failed to modify flow table (%s)",
1543 ofperr_get_name(error));
1544 }
1545
1546 list_remove(&fm->list_node);
1547 free(fm->ofpacts);
1548 free(fm);
1549 }
1550
ada3a58d
EJ
1551 ovs_mutex_lock(&ofproto->pin_mutex);
1552 if (ofproto->n_pins) {
1553 pins = ofproto->pins;
1554 list_moved(&pins);
1555 list_init(&ofproto->pins);
1556 ofproto->n_pins = 0;
1557 } else {
1558 list_init(&pins);
1559 }
1560 ovs_mutex_unlock(&ofproto->pin_mutex);
1561
1562 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1563 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1564 list_remove(&pin->list_node);
1565 free(CONST_CAST(void *, pin->packet));
1566 free(pin);
1567 }
1568
0aa66d6e
EJ
1569 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1570 port_run_fast(ofport);
1571 }
1572
5fcc0d00
BP
1573 return 0;
1574}
1575
1576static int
1577run(struct ofproto *ofproto_)
1578{
1579 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1580 struct ofport_dpif *ofport;
1581 struct ofbundle *bundle;
1582 int error;
1583
1584 if (!clogged) {
1585 complete_operations(ofproto);
1586 }
5fcc0d00 1587
ec7ceaed
EJ
1588 if (mbridge_need_revalidate(ofproto->mbridge)) {
1589 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 1590 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 1591 mac_learning_flush(ofproto->ml);
509c0149 1592 ovs_rwlock_unlock(&ofproto->ml->rwlock);
ec7ceaed
EJ
1593 }
1594
40358701
GS
1595 /* Do not perform any periodic activity below required by 'ofproto' while
1596 * waiting for flow restore to complete. */
1597 if (ofproto_get_flow_restore_wait()) {
1598 return 0;
1599 }
1600
5fcc0d00
BP
1601 error = run_fast(ofproto_);
1602 if (error) {
1603 return error;
abe529af
BP
1604 }
1605
abe529af 1606 if (ofproto->netflow) {
6fca1ffb
BP
1607 if (netflow_run(ofproto->netflow)) {
1608 send_netflow_active_timeouts(ofproto);
1609 }
abe529af
BP
1610 }
1611 if (ofproto->sflow) {
bae473fe 1612 dpif_sflow_run(ofproto->sflow);
abe529af
BP
1613 }
1614
1615 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1616 port_run(ofport);
1617 }
1618 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1619 bundle_run(bundle);
1620 }
1621
21f7563c 1622 stp_run(ofproto);
509c0149 1623 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594
EJ
1624 if (mac_learning_run(ofproto->ml)) {
1625 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1626 }
509c0149 1627 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af 1628
6814e51f 1629 /* Check the consistency of a random facet, to aid debugging. */
0b4f2078 1630 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
4d2a0f39 1631 if (time_msec() >= ofproto->consistency_rl
bcd2633a 1632 && !classifier_is_empty(&ofproto->facets)
2cc3c58e 1633 && !ofproto->backer->need_revalidate) {
bcd2633a
JP
1634 struct cls_table *table;
1635 struct cls_rule *cr;
6814e51f
BP
1636 struct facet *facet;
1637
4d2a0f39
EJ
1638 ofproto->consistency_rl = time_msec() + 250;
1639
bcd2633a
JP
1640 table = CONTAINER_OF(hmap_random_node(&ofproto->facets.tables),
1641 struct cls_table, hmap_node);
1642 cr = CONTAINER_OF(hmap_random_node(&table->rules), struct cls_rule,
1643 hmap_node);
1644 facet = CONTAINER_OF(cr, struct facet, cr);
1645
a1616063
EJ
1646 if (!facet_check_consistency(facet)) {
1647 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
6814e51f
BP
1648 }
1649 }
0b4f2078 1650 ovs_rwlock_unlock(&ofproto->facets.rwlock);
6814e51f 1651
abe529af
BP
1652 return 0;
1653}
1654
1655static void
1656wait(struct ofproto *ofproto_)
1657{
1658 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1659 struct ofport_dpif *ofport;
1660 struct ofbundle *bundle;
1661
7ee20df1
BP
1662 if (!clogged && !list_is_empty(&ofproto->completions)) {
1663 poll_immediate_wake();
1664 }
1665
40358701
GS
1666 if (ofproto_get_flow_restore_wait()) {
1667 return;
1668 }
1669
acf60855
JP
1670 dpif_wait(ofproto->backer->dpif);
1671 dpif_recv_wait(ofproto->backer->dpif);
abe529af 1672 if (ofproto->sflow) {
bae473fe 1673 dpif_sflow_wait(ofproto->sflow);
abe529af 1674 }
abe529af
BP
1675 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1676 port_wait(ofport);
1677 }
1678 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1679 bundle_wait(bundle);
1680 }
6fca1ffb
BP
1681 if (ofproto->netflow) {
1682 netflow_wait(ofproto->netflow);
1683 }
509c0149 1684 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1c313b88 1685 mac_learning_wait(ofproto->ml);
509c0149 1686 ovs_rwlock_unlock(&ofproto->ml->rwlock);
21f7563c 1687 stp_wait(ofproto);
2cc3c58e 1688 if (ofproto->backer->need_revalidate) {
abe529af
BP
1689 /* Shouldn't happen, but if it does just go around again. */
1690 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1691 poll_immediate_wake();
abe529af
BP
1692 }
1693}
1694
0d085684
BP
1695static void
1696get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1697{
1698 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bcd2633a 1699 struct cls_cursor cursor;
04d08d54
EJ
1700 size_t n_subfacets = 0;
1701 struct facet *facet;
0d085684 1702
0b4f2078 1703 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
bcd2633a 1704 simap_increase(usage, "facets", classifier_count(&ofproto->facets));
0b4f2078 1705 ovs_rwlock_unlock(&ofproto->facets.rwlock);
bcd2633a 1706
0b4f2078 1707 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
bcd2633a
JP
1708 cls_cursor_init(&cursor, &ofproto->facets, NULL);
1709 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
04d08d54
EJ
1710 n_subfacets += list_size(&facet->subfacets);
1711 }
0b4f2078 1712 ovs_rwlock_unlock(&ofproto->facets.rwlock);
04d08d54 1713 simap_increase(usage, "subfacets", n_subfacets);
0d085684
BP
1714}
1715
abe529af
BP
1716static void
1717flush(struct ofproto *ofproto_)
1718{
1719 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855
JP
1720 struct subfacet *subfacet, *next_subfacet;
1721 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
1722 int n_batch;
b0f7b9b5 1723
acf60855
JP
1724 n_batch = 0;
1725 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
04d08d54 1726 &ofproto->backer->subfacets) {
b7e2f6b3 1727 if (subfacet->facet->ofproto != ofproto) {
04d08d54
EJ
1728 continue;
1729 }
1730
acf60855
JP
1731 if (subfacet->path != SF_NOT_INSTALLED) {
1732 batch[n_batch++] = subfacet;
1733 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
04d08d54 1734 subfacet_destroy_batch(ofproto->backer, batch, n_batch);
acf60855
JP
1735 n_batch = 0;
1736 }
1737 } else {
1738 subfacet_destroy(subfacet);
b0f7b9b5 1739 }
abe529af 1740 }
acf60855
JP
1741
1742 if (n_batch > 0) {
04d08d54 1743 subfacet_destroy_batch(ofproto->backer, batch, n_batch);
acf60855 1744 }
abe529af
BP
1745}
1746
6c1491fb
BP
1747static void
1748get_features(struct ofproto *ofproto_ OVS_UNUSED,
9e1fd49b 1749 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
6c1491fb
BP
1750{
1751 *arp_match_ip = true;
9e1fd49b
BP
1752 *actions = (OFPUTIL_A_OUTPUT |
1753 OFPUTIL_A_SET_VLAN_VID |
1754 OFPUTIL_A_SET_VLAN_PCP |
1755 OFPUTIL_A_STRIP_VLAN |
1756 OFPUTIL_A_SET_DL_SRC |
1757 OFPUTIL_A_SET_DL_DST |
1758 OFPUTIL_A_SET_NW_SRC |
1759 OFPUTIL_A_SET_NW_DST |
1760 OFPUTIL_A_SET_NW_TOS |
1761 OFPUTIL_A_SET_TP_SRC |
1762 OFPUTIL_A_SET_TP_DST |
1763 OFPUTIL_A_ENQUEUE);
6c1491fb
BP
1764}
1765
1766static void
307975da 1767get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
6c1491fb
BP
1768{
1769 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
a8d9304d 1770 struct dpif_dp_stats s;
7fd51d39 1771 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
7e741ffb 1772 uint64_t n_lookup;
6c1491fb
BP
1773
1774 strcpy(ots->name, "classifier");
1775
acf60855 1776 dpif_get_dp_stats(ofproto->backer->dpif, &s);
7e741ffb
JG
1777 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes);
1778 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes);
7fd51d39 1779 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes);
acf60855 1780
7fd51d39 1781 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
7e741ffb
JG
1782 ots->lookup_count = htonll(n_lookup);
1783 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
6c1491fb
BP
1784}
1785
abe529af
BP
1786static struct ofport *
1787port_alloc(void)
1788{
1789 struct ofport_dpif *port = xmalloc(sizeof *port);
1790 return &port->up;
1791}
1792
1793static void
1794port_dealloc(struct ofport *port_)
1795{
1796 struct ofport_dpif *port = ofport_dpif_cast(port_);
1797 free(port);
1798}
1799
1800static int
1801port_construct(struct ofport *port_)
1802{
1803 struct ofport_dpif *port = ofport_dpif_cast(port_);
1804 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
b9ad7294 1805 const struct netdev *netdev = port->up.netdev;
3aa30359 1806 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
e1b1d06a
JP
1807 struct dpif_port dpif_port;
1808 int error;
abe529af 1809
2cc3c58e 1810 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
1811 port->bundle = NULL;
1812 port->cfm = NULL;
ccc09689 1813 port->bfd = NULL;
d5ffa7f2 1814 port->may_enable = true;
21f7563c
JP
1815 port->stp_port = NULL;
1816 port->stp_state = STP_DISABLED;
42943cde 1817 port->is_tunnel = false;
6cbbf4fa 1818 port->peer = NULL;
55954f6e
EJ
1819 port->qdscp = NULL;
1820 port->n_qdscp = 0;
52a90c29
BP
1821 port->realdev_ofp_port = 0;
1822 port->vlandev_vid = 0;
b9ad7294 1823 port->carrier_seq = netdev_get_carrier_resets(netdev);
abe529af 1824
b9ad7294 1825 if (netdev_vport_is_patch(netdev)) {
743cea45
NM
1826 /* By bailing out here, we don't submit the port to the sFlow module
1827 * to be considered for counter polling export. This is correct
1828 * because the patch port represents an interface that sFlow considers
1829 * to be "internal" to the switch as a whole, and therefore not an
1830 * candidate for counter polling. */
4e022ec0 1831 port->odp_port = ODPP_NONE;
6cbbf4fa 1832 ofport_update_peer(port);
0a740f48
EJ
1833 return 0;
1834 }
1835
acf60855 1836 error = dpif_port_query_by_name(ofproto->backer->dpif,
3aa30359
BP
1837 netdev_vport_get_dpif_port(netdev, namebuf,
1838 sizeof namebuf),
e1b1d06a
JP
1839 &dpif_port);
1840 if (error) {
1841 return error;
1842 }
1843
1844 port->odp_port = dpif_port.port_no;
1845
b9ad7294 1846 if (netdev_get_tunnel_config(netdev)) {
42943cde
EJ
1847 tnl_port_add(port, port->up.netdev, port->odp_port);
1848 port->is_tunnel = true;
b9ad7294
EJ
1849 } else {
1850 /* Sanity-check that a mapping doesn't already exist. This
1851 * shouldn't happen for non-tunnel ports. */
1852 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1853 VLOG_ERR("port %s already has an OpenFlow port number",
1854 dpif_port.name);
da78d43d 1855 dpif_port_destroy(&dpif_port);
b9ad7294
EJ
1856 return EBUSY;
1857 }
e1b1d06a 1858
8449c4d6 1859 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1860 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
f9c0c3ec 1861 hash_odp_port(port->odp_port));
8449c4d6 1862 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
b9ad7294 1863 }
da78d43d 1864 dpif_port_destroy(&dpif_port);
e1b1d06a 1865
abe529af 1866 if (ofproto->sflow) {
e1b1d06a 1867 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
abe529af
BP
1868 }
1869
1870 return 0;
1871}
1872
1873static void
1874port_destruct(struct ofport *port_)
1875{
1876 struct ofport_dpif *port = ofport_dpif_cast(port_);
1877 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
02f8d646 1878 const char *devname = netdev_get_name(port->up.netdev);
3aa30359
BP
1879 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1880 const char *dp_port_name;
abe529af 1881
7894e7da 1882 ofproto->backer->need_revalidate = REV_RECONFIGURE;
dc24a00f 1883 ovs_rwlock_wrlock(&xlate_rwlock);
46c88433 1884 xlate_ofport_remove(port);
dc24a00f 1885 ovs_rwlock_unlock(&xlate_rwlock);
7894e7da 1886
3aa30359
BP
1887 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1888 sizeof namebuf);
a614d823 1889 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
acf60855
JP
1890 /* The underlying device is still there, so delete it. This
1891 * happens when the ofproto is being destroyed, since the caller
1892 * assumes that removal of attached ports will happen as part of
1893 * destruction. */
42943cde 1894 if (!port->is_tunnel) {
a614d823
KM
1895 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1896 }
acf60855
JP
1897 }
1898
6cbbf4fa
EJ
1899 if (port->peer) {
1900 port->peer->peer = NULL;
1901 port->peer = NULL;
1902 }
1903
42943cde 1904 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
8449c4d6 1905 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48 1906 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
8449c4d6 1907 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
0a740f48
EJ
1908 }
1909
42943cde 1910 tnl_port_del(port);
02f8d646 1911 sset_find_and_delete(&ofproto->ports, devname);
0a740f48 1912 sset_find_and_delete(&ofproto->ghost_ports, devname);
abe529af 1913 bundle_remove(port_);
a5610457 1914 set_cfm(port_, NULL);
8aee94b6 1915 set_bfd(port_, NULL);
abe529af 1916 if (ofproto->sflow) {
bae473fe 1917 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
abe529af 1918 }
8b36f51e 1919
55954f6e 1920 free(port->qdscp);
abe529af
BP
1921}
1922
1923static void
1924port_modified(struct ofport *port_)
1925{
1926 struct ofport_dpif *port = ofport_dpif_cast(port_);
1927
1928 if (port->bundle && port->bundle->bond) {
1929 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1930 }
9d46b444
EJ
1931
1932 if (port->cfm) {
1933 cfm_set_netdev(port->cfm, port->up.netdev);
1934 }
f1e78bbd 1935
42943cde
EJ
1936 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1937 port->odp_port)) {
7894e7da
EJ
1938 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1939 REV_RECONFIGURE;
f1e78bbd 1940 }
6cbbf4fa
EJ
1941
1942 ofport_update_peer(port);
abe529af
BP
1943}
1944
1945static void
9e1fd49b 1946port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
abe529af
BP
1947{
1948 struct ofport_dpif *port = ofport_dpif_cast(port_);
1949 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
9e1fd49b 1950 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
abe529af 1951
9e1fd49b 1952 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
c57b2226
BP
1953 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1954 OFPUTIL_PC_NO_PACKET_IN)) {
2cc3c58e 1955 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7bde8dd8 1956
9e1fd49b 1957 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
7bde8dd8
JP
1958 bundle_update(port->bundle);
1959 }
abe529af
BP
1960 }
1961}
1962
1963static int
1964set_sflow(struct ofproto *ofproto_,
1965 const struct ofproto_sflow_options *sflow_options)
1966{
1967 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
bae473fe 1968 struct dpif_sflow *ds = ofproto->sflow;
6ff686f2 1969
abe529af 1970 if (sflow_options) {
bae473fe 1971 if (!ds) {
abe529af
BP
1972 struct ofport_dpif *ofport;
1973
4213f19d 1974 ds = ofproto->sflow = dpif_sflow_create();
abe529af 1975 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
e1b1d06a 1976 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
abe529af 1977 }
2cc3c58e 1978 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af 1979 }
bae473fe 1980 dpif_sflow_set_options(ds, sflow_options);
abe529af 1981 } else {
6ff686f2 1982 if (ds) {
9723bcce 1983 dpif_sflow_unref(ds);
2cc3c58e 1984 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6ff686f2
PS
1985 ofproto->sflow = NULL;
1986 }
abe529af
BP
1987 }
1988 return 0;
1989}
1990
29089a54
RL
1991static int
1992set_ipfix(
1993 struct ofproto *ofproto_,
1994 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1995 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1996 size_t n_flow_exporters_options)
1997{
1998 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1999 struct dpif_ipfix *di = ofproto->ipfix;
2000
2001 if (bridge_exporter_options || flow_exporters_options) {
2002 if (!di) {
2003 di = ofproto->ipfix = dpif_ipfix_create();
2004 }
2005 dpif_ipfix_set_options(
2006 di, bridge_exporter_options, flow_exporters_options,
2007 n_flow_exporters_options);
2008 } else {
2009 if (di) {
d857c8aa 2010 dpif_ipfix_unref(di);
29089a54
RL
2011 ofproto->ipfix = NULL;
2012 }
2013 }
2014 return 0;
2015}
2016
abe529af 2017static int
a5610457 2018set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
abe529af
BP
2019{
2020 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2021 int error;
2022
a5610457 2023 if (!s) {
abe529af
BP
2024 error = 0;
2025 } else {
2026 if (!ofport->cfm) {
8c977421
EJ
2027 struct ofproto_dpif *ofproto;
2028
2029 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2cc3c58e 2030 ofproto->backer->need_revalidate = REV_RECONFIGURE;
90967e95 2031 ofport->cfm = cfm_create(ofport->up.netdev);
abe529af
BP
2032 }
2033
a5610457 2034 if (cfm_configure(ofport->cfm, s)) {
abe529af
BP
2035 return 0;
2036 }
2037
2038 error = EINVAL;
2039 }
8e9a73fa 2040 cfm_unref(ofport->cfm);
abe529af
BP
2041 ofport->cfm = NULL;
2042 return error;
2043}
2044
9a9e3786
BP
2045static bool
2046get_cfm_status(const struct ofport *ofport_,
2047 struct ofproto_cfm_status *status)
1de11730
EJ
2048{
2049 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2050
2051 if (ofport->cfm) {
9a9e3786
BP
2052 status->faults = cfm_get_fault(ofport->cfm);
2053 status->remote_opstate = cfm_get_opup(ofport->cfm);
2054 status->health = cfm_get_health(ofport->cfm);
2055 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
2056 return true;
1de11730 2057 } else {
9a9e3786 2058 return false;
1de11730
EJ
2059 }
2060}
ccc09689
EJ
2061
2062static int
2063set_bfd(struct ofport *ofport_, const struct smap *cfg)
2064{
2065 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2066 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2067 struct bfd *old;
2068
2069 old = ofport->bfd;
2070 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev), cfg);
2071 if (ofport->bfd != old) {
2072 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2073 }
2074
2075 return 0;
2076}
2077
2078static int
2079get_bfd_status(struct ofport *ofport_, struct smap *smap)
2080{
2081 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2082
2083 if (ofport->bfd) {
2084 bfd_get_status(ofport->bfd, smap);
2085 return 0;
2086 } else {
2087 return ENOENT;
2088 }
2089}
abe529af 2090\f
21f7563c
JP
2091/* Spanning Tree. */
2092
2093static void
2094send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
2095{
2096 struct ofproto_dpif *ofproto = ofproto_;
2097 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2098 struct ofport_dpif *ofport;
2099
2100 ofport = stp_port_get_aux(sp);
2101 if (!ofport) {
2102 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2103 ofproto->up.name, port_num);
2104 } else {
2105 struct eth_header *eth = pkt->l2;
2106
2107 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2108 if (eth_addr_is_zero(eth->eth_src)) {
2109 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2110 "with unknown MAC", ofproto->up.name, port_num);
2111 } else {
97d6520b 2112 send_packet(ofport, pkt);
21f7563c
JP
2113 }
2114 }
2115 ofpbuf_delete(pkt);
2116}
2117
2118/* Configures STP on 'ofproto_' using the settings defined in 's'. */
2119static int
2120set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2121{
2122 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2123
2124 /* Only revalidate flows if the configuration changed. */
2125 if (!s != !ofproto->stp) {
2cc3c58e 2126 ofproto->backer->need_revalidate = REV_RECONFIGURE;
21f7563c
JP
2127 }
2128
2129 if (s) {
2130 if (!ofproto->stp) {
2131 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2132 send_bpdu_cb, ofproto);
2133 ofproto->stp_last_tick = time_msec();
2134 }
2135
2136 stp_set_bridge_id(ofproto->stp, s->system_id);
2137 stp_set_bridge_priority(ofproto->stp, s->priority);
2138 stp_set_hello_time(ofproto->stp, s->hello_time);
2139 stp_set_max_age(ofproto->stp, s->max_age);
2140 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2141 } else {
851bf71d
EJ
2142 struct ofport *ofport;
2143
2144 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2145 set_stp_port(ofport, NULL);
2146 }
2147
bd54dbcd 2148 stp_unref(ofproto->stp);
21f7563c
JP
2149 ofproto->stp = NULL;
2150 }
2151
2152 return 0;
2153}
2154
2155static int
2156get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2157{
2158 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2159
2160 if (ofproto->stp) {
2161 s->enabled = true;
2162 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2163 s->designated_root = stp_get_designated_root(ofproto->stp);
2164 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2165 } else {
2166 s->enabled = false;
2167 }
2168
2169 return 0;
2170}
2171
2172static void
2173update_stp_port_state(struct ofport_dpif *ofport)
2174{
2175 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2176 enum stp_state state;
2177
2178 /* Figure out new state. */
2179 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2180 : STP_DISABLED;
2181
2182 /* Update state. */
2183 if (ofport->stp_state != state) {
9e1fd49b 2184 enum ofputil_port_state of_state;
21f7563c
JP
2185 bool fwd_change;
2186
2187 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
2188 netdev_get_name(ofport->up.netdev),
2189 stp_state_name(ofport->stp_state),
2190 stp_state_name(state));
2191 if (stp_learn_in_state(ofport->stp_state)
2192 != stp_learn_in_state(state)) {
2193 /* xxx Learning action flows should also be flushed. */
509c0149 2194 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 2195 mac_learning_flush(ofproto->ml);
509c0149 2196 ovs_rwlock_unlock(&ofproto->ml->rwlock);
21f7563c
JP
2197 }
2198 fwd_change = stp_forward_in_state(ofport->stp_state)
2199 != stp_forward_in_state(state);
2200
2cc3c58e 2201 ofproto->backer->need_revalidate = REV_STP;
21f7563c
JP
2202 ofport->stp_state = state;
2203 ofport->stp_state_entered = time_msec();
2204
b308140a 2205 if (fwd_change && ofport->bundle) {
21f7563c
JP
2206 bundle_update(ofport->bundle);
2207 }
2208
2209 /* Update the STP state bits in the OpenFlow port description. */
9e1fd49b
BP
2210 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2211 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2212 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2213 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2214 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2215 : 0);
21f7563c
JP
2216 ofproto_port_set_state(&ofport->up, of_state);
2217 }
2218}
2219
2220/* Configures STP on 'ofport_' using the settings defined in 's'. The
2221 * caller is responsible for assigning STP port numbers and ensuring
2222 * there are no duplicates. */
2223static int
2224set_stp_port(struct ofport *ofport_,
2225 const struct ofproto_port_stp_settings *s)
2226{
2227 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2228 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2229 struct stp_port *sp = ofport->stp_port;
2230
2231 if (!s || !s->enable) {
2232 if (sp) {
2233 ofport->stp_port = NULL;
2234 stp_port_disable(sp);
ecd12731 2235 update_stp_port_state(ofport);
21f7563c
JP
2236 }
2237 return 0;
2238 } else if (sp && stp_port_no(sp) != s->port_num
2239 && ofport == stp_port_get_aux(sp)) {
2240 /* The port-id changed, so disable the old one if it's not
2241 * already in use by another port. */
2242 stp_port_disable(sp);
2243 }
2244
2245 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2246 stp_port_enable(sp);
2247
2248 stp_port_set_aux(sp, ofport);
2249 stp_port_set_priority(sp, s->priority);
2250 stp_port_set_path_cost(sp, s->path_cost);
2251
2252 update_stp_port_state(ofport);
2253
2254 return 0;
2255}
2256
2257static int
2258get_stp_port_status(struct ofport *ofport_,
2259 struct ofproto_port_stp_status *s)
2260{
2261 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2262 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2263 struct stp_port *sp = ofport->stp_port;
2264
2265 if (!ofproto->stp || !sp) {
2266 s->enabled = false;
2267 return 0;
2268 }
2269
2270 s->enabled = true;
2271 s->port_id = stp_port_get_id(sp);
2272 s->state = stp_port_get_state(sp);
2273 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2274 s->role = stp_port_get_role(sp);
80740385 2275 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
21f7563c
JP
2276
2277 return 0;
2278}
2279
2280static void
2281stp_run(struct ofproto_dpif *ofproto)
2282{
2283 if (ofproto->stp) {
2284 long long int now = time_msec();
2285 long long int elapsed = now - ofproto->stp_last_tick;
2286 struct stp_port *sp;
2287
2288 if (elapsed > 0) {
2289 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2290 ofproto->stp_last_tick = now;
2291 }
2292 while (stp_get_changed_port(ofproto->stp, &sp)) {
2293 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2294
2295 if (ofport) {
2296 update_stp_port_state(ofport);
2297 }
2298 }
6ae50723
EJ
2299
2300 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
509c0149 2301 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 2302 mac_learning_flush(ofproto->ml);
509c0149 2303 ovs_rwlock_unlock(&ofproto->ml->rwlock);
6ae50723 2304 }
21f7563c
JP
2305 }
2306}
2307
2308static void
2309stp_wait(struct ofproto_dpif *ofproto)
2310{
2311 if (ofproto->stp) {
2312 poll_timer_wait(1000);
2313 }
2314}
21f7563c 2315\f
8b36f51e 2316static int
55954f6e 2317set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
8b36f51e
EJ
2318 size_t n_qdscp)
2319{
2320 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2321 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
8b36f51e 2322
55954f6e
EJ
2323 if (ofport->n_qdscp != n_qdscp
2324 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2325 n_qdscp * sizeof *qdscp))) {
2cc3c58e 2326 ofproto->backer->need_revalidate = REV_RECONFIGURE;
55954f6e
EJ
2327 free(ofport->qdscp);
2328 ofport->qdscp = n_qdscp
2329 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2330 : NULL;
2331 ofport->n_qdscp = n_qdscp;
8b36f51e
EJ
2332 }
2333
8b36f51e
EJ
2334 return 0;
2335}
2336\f
abe529af
BP
2337/* Bundles. */
2338
b44a10b7
BP
2339/* Expires all MAC learning entries associated with 'bundle' and forces its
2340 * ofproto to revalidate every flow.
2341 *
2342 * Normally MAC learning entries are removed only from the ofproto associated
2343 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2344 * are removed from every ofproto. When patch ports and SLB bonds are in use
2345 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2346 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2347 * with the host from which it migrated. */
abe529af 2348static void
b44a10b7 2349bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
abe529af
BP
2350{
2351 struct ofproto_dpif *ofproto = bundle->ofproto;
2352 struct mac_learning *ml = ofproto->ml;
2353 struct mac_entry *mac, *next_mac;
2354
2cc3c58e 2355 ofproto->backer->need_revalidate = REV_RECONFIGURE;
509c0149 2356 ovs_rwlock_wrlock(&ml->rwlock);
abe529af
BP
2357 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2358 if (mac->port.p == bundle) {
b44a10b7
BP
2359 if (all_ofprotos) {
2360 struct ofproto_dpif *o;
2361
2362 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2363 if (o != ofproto) {
2364 struct mac_entry *e;
2365
509c0149 2366 ovs_rwlock_wrlock(&o->ml->rwlock);
30618594 2367 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
b44a10b7 2368 if (e) {
b44a10b7
BP
2369 mac_learning_expire(o->ml, e);
2370 }
509c0149 2371 ovs_rwlock_unlock(&o->ml->rwlock);
b44a10b7
BP
2372 }
2373 }
2374 }
2375
abe529af
BP
2376 mac_learning_expire(ml, mac);
2377 }
2378 }
509c0149 2379 ovs_rwlock_unlock(&ml->rwlock);
abe529af
BP
2380}
2381
2382static struct ofbundle *
2383bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2384{
2385 struct ofbundle *bundle;
2386
2387 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2388 &ofproto->bundles) {
2389 if (bundle->aux == aux) {
2390 return bundle;
2391 }
2392 }
2393 return NULL;
2394}
2395
7bde8dd8
JP
2396static void
2397bundle_update(struct ofbundle *bundle)
2398{
2399 struct ofport_dpif *port;
2400
2401 bundle->floodable = true;
2402 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
9e1fd49b
BP
2403 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2404 || !stp_forward_in_state(port->stp_state)) {
7bde8dd8
JP
2405 bundle->floodable = false;
2406 break;
2407 }
2408 }
2409}
2410
abe529af
BP
2411static void
2412bundle_del_port(struct ofport_dpif *port)
2413{
2414 struct ofbundle *bundle = port->bundle;
2415
2cc3c58e 2416 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
6f77f4ae 2417
abe529af
BP
2418 list_remove(&port->bundle_node);
2419 port->bundle = NULL;
2420
2421 if (bundle->lacp) {
2422 lacp_slave_unregister(bundle->lacp, port);
2423 }
2424 if (bundle->bond) {
2425 bond_slave_unregister(bundle->bond, port);
2426 }
2427
7bde8dd8 2428 bundle_update(bundle);
abe529af
BP
2429}
2430
2431static bool
4e022ec0 2432bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
df53d41c 2433 struct lacp_slave_settings *lacp)
abe529af
BP
2434{
2435 struct ofport_dpif *port;
2436
2437 port = get_ofp_port(bundle->ofproto, ofp_port);
2438 if (!port) {
2439 return false;
2440 }
2441
2442 if (port->bundle != bundle) {
2cc3c58e 2443 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2444 if (port->bundle) {
2445 bundle_del_port(port);
2446 }
2447
2448 port->bundle = bundle;
2449 list_push_back(&bundle->ports, &port->bundle_node);
9e1fd49b
BP
2450 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2451 || !stp_forward_in_state(port->stp_state)) {
abe529af
BP
2452 bundle->floodable = false;
2453 }
2454 }
2455 if (lacp) {
2cc3c58e 2456 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2457 lacp_slave_register(bundle->lacp, port, lacp);
2458 }
2459
2460 return true;
2461}
2462
2463static void
2464bundle_destroy(struct ofbundle *bundle)
2465{
2466 struct ofproto_dpif *ofproto;
2467 struct ofport_dpif *port, *next_port;
abe529af
BP
2468
2469 if (!bundle) {
2470 return;
2471 }
2472
2473 ofproto = bundle->ofproto;
ec7ceaed 2474 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
abe529af 2475
dc24a00f 2476 ovs_rwlock_wrlock(&xlate_rwlock);
46c88433 2477 xlate_bundle_remove(bundle);
dc24a00f 2478 ovs_rwlock_unlock(&xlate_rwlock);
46c88433 2479
abe529af
BP
2480 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2481 bundle_del_port(port);
2482 }
2483
b44a10b7 2484 bundle_flush_macs(bundle, true);
abe529af
BP
2485 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2486 free(bundle->name);
2487 free(bundle->trunks);
91779071 2488 lacp_unref(bundle->lacp);
03366a2d 2489 bond_unref(bundle->bond);
abe529af
BP
2490 free(bundle);
2491}
2492
2493static int
2494bundle_set(struct ofproto *ofproto_, void *aux,
2495 const struct ofproto_bundle_settings *s)
2496{
2497 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2498 bool need_flush = false;
abe529af
BP
2499 struct ofport_dpif *port;
2500 struct ofbundle *bundle;
ecac4ebf
BP
2501 unsigned long *trunks;
2502 int vlan;
abe529af
BP
2503 size_t i;
2504 bool ok;
2505
2506 if (!s) {
2507 bundle_destroy(bundle_lookup(ofproto, aux));
2508 return 0;
2509 }
2510
cb22974d
BP
2511 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2512 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
abe529af
BP
2513
2514 bundle = bundle_lookup(ofproto, aux);
2515 if (!bundle) {
2516 bundle = xmalloc(sizeof *bundle);
2517
2518 bundle->ofproto = ofproto;
2519 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2520 hash_pointer(aux, 0));
2521 bundle->aux = aux;
2522 bundle->name = NULL;
2523
2524 list_init(&bundle->ports);
ecac4ebf 2525 bundle->vlan_mode = PORT_VLAN_TRUNK;
abe529af
BP
2526 bundle->vlan = -1;
2527 bundle->trunks = NULL;
5e9ceccd 2528 bundle->use_priority_tags = s->use_priority_tags;
abe529af
BP
2529 bundle->lacp = NULL;
2530 bundle->bond = NULL;
2531
2532 bundle->floodable = true;
ec7ceaed 2533 mbridge_register_bundle(ofproto->mbridge, bundle);
abe529af
BP
2534 }
2535
2536 if (!bundle->name || strcmp(s->name, bundle->name)) {
2537 free(bundle->name);
2538 bundle->name = xstrdup(s->name);
2539 }
2540
2541 /* LACP. */
2542 if (s->lacp) {
2543 if (!bundle->lacp) {
2cc3c58e 2544 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2545 bundle->lacp = lacp_create();
2546 }
2547 lacp_configure(bundle->lacp, s->lacp);
2548 } else {
91779071 2549 lacp_unref(bundle->lacp);
abe529af
BP
2550 bundle->lacp = NULL;
2551 }
2552
2553 /* Update set of ports. */
2554 ok = true;
2555 for (i = 0; i < s->n_slaves; i++) {
2556 if (!bundle_add_port(bundle, s->slaves[i],
df53d41c 2557 s->lacp ? &s->lacp_slaves[i] : NULL)) {
abe529af
BP
2558 ok = false;
2559 }
2560 }
2561 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2562 struct ofport_dpif *next_port;
2563
2564 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2565 for (i = 0; i < s->n_slaves; i++) {
56c769ab 2566 if (s->slaves[i] == port->up.ofp_port) {
abe529af
BP
2567 goto found;
2568 }
2569 }
2570
2571 bundle_del_port(port);
2572 found: ;
2573 }
2574 }
cb22974d 2575 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
abe529af
BP
2576
2577 if (list_is_empty(&bundle->ports)) {
2578 bundle_destroy(bundle);
2579 return EINVAL;
2580 }
2581
ecac4ebf 2582 /* Set VLAN tagging mode */
5e9ceccd
BP
2583 if (s->vlan_mode != bundle->vlan_mode
2584 || s->use_priority_tags != bundle->use_priority_tags) {
ecac4ebf 2585 bundle->vlan_mode = s->vlan_mode;
5e9ceccd 2586 bundle->use_priority_tags = s->use_priority_tags;
ecac4ebf
BP
2587 need_flush = true;
2588 }
2589
abe529af 2590 /* Set VLAN tag. */
ecac4ebf
BP
2591 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2592 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2593 : 0);
2594 if (vlan != bundle->vlan) {
2595 bundle->vlan = vlan;
abe529af
BP
2596 need_flush = true;
2597 }
2598
2599 /* Get trunked VLANs. */
ecac4ebf
BP
2600 switch (s->vlan_mode) {
2601 case PORT_VLAN_ACCESS:
2602 trunks = NULL;
2603 break;
2604
2605 case PORT_VLAN_TRUNK:
ebc56baa 2606 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
2607 break;
2608
2609 case PORT_VLAN_NATIVE_UNTAGGED:
2610 case PORT_VLAN_NATIVE_TAGGED:
2611 if (vlan != 0 && (!s->trunks
2612 || !bitmap_is_set(s->trunks, vlan)
2613 || bitmap_is_set(s->trunks, 0))) {
2614 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2615 if (s->trunks) {
2616 trunks = bitmap_clone(s->trunks, 4096);
2617 } else {
2618 trunks = bitmap_allocate1(4096);
2619 }
2620 bitmap_set1(trunks, vlan);
2621 bitmap_set0(trunks, 0);
2622 } else {
ebc56baa 2623 trunks = CONST_CAST(unsigned long *, s->trunks);
ecac4ebf
BP
2624 }
2625 break;
2626
2627 default:
2628 NOT_REACHED();
2629 }
abe529af
BP
2630 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2631 free(bundle->trunks);
ecac4ebf
BP
2632 if (trunks == s->trunks) {
2633 bundle->trunks = vlan_bitmap_clone(trunks);
2634 } else {
2635 bundle->trunks = trunks;
2636 trunks = NULL;
2637 }
abe529af
BP
2638 need_flush = true;
2639 }
ecac4ebf
BP
2640 if (trunks != s->trunks) {
2641 free(trunks);
2642 }
abe529af
BP
2643
2644 /* Bonding. */
2645 if (!list_is_short(&bundle->ports)) {
2646 bundle->ofproto->has_bonded_bundles = true;
2647 if (bundle->bond) {
2648 if (bond_reconfigure(bundle->bond, s->bond)) {
2cc3c58e 2649 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2650 }
2651 } else {
2652 bundle->bond = bond_create(s->bond);
2cc3c58e 2653 ofproto->backer->need_revalidate = REV_RECONFIGURE;
abe529af
BP
2654 }
2655
2656 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
df53d41c 2657 bond_slave_register(bundle->bond, port, port->up.netdev);
abe529af
BP
2658 }
2659 } else {
03366a2d 2660 bond_unref(bundle->bond);
abe529af
BP
2661 bundle->bond = NULL;
2662 }
2663
2664 /* If we changed something that would affect MAC learning, un-learn
2665 * everything on this port and force flow revalidation. */
2666 if (need_flush) {
b44a10b7 2667 bundle_flush_macs(bundle, false);
abe529af
BP
2668 }
2669
2670 return 0;
2671}
2672
2673static void
2674bundle_remove(struct ofport *port_)
2675{
2676 struct ofport_dpif *port = ofport_dpif_cast(port_);
2677 struct ofbundle *bundle = port->bundle;
2678
2679 if (bundle) {
2680 bundle_del_port(port);
2681 if (list_is_empty(&bundle->ports)) {
2682 bundle_destroy(bundle);
2683 } else if (list_is_short(&bundle->ports)) {
03366a2d 2684 bond_unref(bundle->bond);
abe529af
BP
2685 bundle->bond = NULL;
2686 }
2687 }
2688}
2689
2690static void
5f877369 2691send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
abe529af
BP
2692{
2693 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2694 struct ofport_dpif *port = port_;
2695 uint8_t ea[ETH_ADDR_LEN];
2696 int error;
2697
2698 error = netdev_get_etheraddr(port->up.netdev, ea);
2699 if (!error) {
abe529af 2700 struct ofpbuf packet;
5f877369 2701 void *packet_pdu;
abe529af
BP
2702
2703 ofpbuf_init(&packet, 0);
2704 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
5f877369
EJ
2705 pdu_size);
2706 memcpy(packet_pdu, pdu, pdu_size);
2707
97d6520b 2708 send_packet(port, &packet);
abe529af
BP
2709 ofpbuf_uninit(&packet);
2710 } else {
2711 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2712 "%s (%s)", port->bundle->name,
10a89ef0 2713 netdev_get_name(port->up.netdev), ovs_strerror(error));
abe529af
BP
2714 }
2715}
2716
2717static void
2718bundle_send_learning_packets(struct ofbundle *bundle)
2719{
2720 struct ofproto_dpif *ofproto = bundle->ofproto;
2721 int error, n_packets, n_errors;
2722 struct mac_entry *e;
2723
2724 error = n_packets = n_errors = 0;
509c0149 2725 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
abe529af
BP
2726 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2727 if (e->port.p != bundle) {
ea131871
JG
2728 struct ofpbuf *learning_packet;
2729 struct ofport_dpif *port;
4dd1e3ca 2730 void *port_void;
ea131871
JG
2731 int ret;
2732
4dd1e3ca
BP
2733 /* The assignment to "port" is unnecessary but makes "grep"ing for
2734 * struct ofport_dpif more effective. */
2735 learning_packet = bond_compose_learning_packet(bundle->bond,
2736 e->mac, e->vlan,
2737 &port_void);
2738 port = port_void;
97d6520b 2739 ret = send_packet(port, learning_packet);
ea131871 2740 ofpbuf_delete(learning_packet);
abe529af
BP
2741 if (ret) {
2742 error = ret;
2743 n_errors++;
2744 }
2745 n_packets++;
2746 }
2747 }
509c0149 2748 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af
BP
2749
2750 if (n_errors) {
2751 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2752 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2753 "packets, last error was: %s",
10a89ef0 2754 bundle->name, n_errors, n_packets, ovs_strerror(error));
abe529af
BP
2755 } else {
2756 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2757 bundle->name, n_packets);
2758 }
2759}
2760
2761static void
2762bundle_run(struct ofbundle *bundle)
2763{
2764 if (bundle->lacp) {
2765 lacp_run(bundle->lacp, send_pdu_cb);
2766 }
2767 if (bundle->bond) {
2768 struct ofport_dpif *port;
2769
2770 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
015e08bc 2771 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
abe529af
BP
2772 }
2773
4a1b8f30
EJ
2774 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2775 bundle->ofproto->backer->need_revalidate = REV_BOND;
2776 }
2777
abe529af
BP
2778 if (bond_should_send_learning_packets(bundle->bond)) {
2779 bundle_send_learning_packets(bundle);
2780 }
2781 }
2782}
2783
2784static void
2785bundle_wait(struct ofbundle *bundle)
2786{
2787 if (bundle->lacp) {
2788 lacp_wait(bundle->lacp);
2789 }
2790 if (bundle->bond) {
2791 bond_wait(bundle->bond);
2792 }
2793}
2794\f
2795/* Mirrors. */
2796
2797static int
ec7ceaed
EJ
2798mirror_set__(struct ofproto *ofproto_, void *aux,
2799 const struct ofproto_mirror_settings *s)
abe529af
BP
2800{
2801 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
ec7ceaed
EJ
2802 struct ofbundle **srcs, **dsts;
2803 int error;
2804 size_t i;
abe529af 2805
abe529af 2806 if (!s) {
ec7ceaed 2807 mirror_destroy(ofproto->mbridge, aux);
abe529af
BP
2808 return 0;
2809 }
2810
ec7ceaed
EJ
2811 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2812 dsts = xmalloc(s->n_dsts * sizeof *dsts);
abe529af 2813
ec7ceaed
EJ
2814 for (i = 0; i < s->n_srcs; i++) {
2815 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
abe529af
BP
2816 }
2817
ec7ceaed
EJ
2818 for (i = 0; i < s->n_dsts; i++) {
2819 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
abe529af
BP
2820 }
2821
ec7ceaed
EJ
2822 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2823 s->n_dsts, s->src_vlans,
2824 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2825 free(srcs);
2826 free(dsts);
2827 return error;
abe529af
BP
2828}
2829
9d24de3b 2830static int
ec7ceaed
EJ
2831mirror_get_stats__(struct ofproto *ofproto, void *aux,
2832 uint64_t *packets, uint64_t *bytes)
9d24de3b 2833{
8844e035 2834 push_all_stats();
ec7ceaed
EJ
2835 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2836 bytes);
9d24de3b
JP
2837}
2838
abe529af
BP
2839static int
2840set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2841{
2842 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 2843 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
abe529af 2844 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
30618594 2845 mac_learning_flush(ofproto->ml);
abe529af 2846 }
509c0149 2847 ovs_rwlock_unlock(&ofproto->ml->rwlock);
abe529af
BP
2848 return 0;
2849}
2850
2851static bool
b4affc74 2852is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
abe529af
BP
2853{
2854 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2855 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
ec7ceaed 2856 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
abe529af 2857}
8402c74b
SS
2858
2859static void
b53055f4 2860forward_bpdu_changed(struct ofproto *ofproto_)
8402c74b
SS
2861{
2862 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2cc3c58e 2863 ofproto->backer->need_revalidate = REV_RECONFIGURE;
8402c74b 2864}
e764773c
BP
2865
2866static void
c4069512
BP
2867set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2868 size_t max_entries)
e764773c
BP
2869{
2870 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
509c0149 2871 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
e764773c 2872 mac_learning_set_idle_time(ofproto->ml, idle_time);
c4069512 2873 mac_learning_set_max_entries(ofproto->ml, max_entries);
509c0149 2874 ovs_rwlock_unlock(&ofproto->ml->rwlock);
e764773c 2875}
abe529af
BP
2876\f
2877/* Ports. */
2878
46c88433 2879static struct ofport_dpif *
4e022ec0 2880get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
abe529af 2881{
7df6a8bd
BP
2882 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2883 return ofport ? ofport_dpif_cast(ofport) : NULL;
abe529af
BP
2884}
2885
46c88433 2886static struct ofport_dpif *
4e022ec0 2887get_odp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
abe529af 2888{
7c33b188
JR
2889 struct ofport_dpif *port = odp_port_to_ofport(ofproto->backer, odp_port);
2890 return port && &ofproto->up == port->up.ofproto ? port : NULL;
abe529af
BP
2891}
2892
2893static void
e1b1d06a
JP
2894ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2895 struct ofproto_port *ofproto_port,
abe529af
BP
2896 struct dpif_port *dpif_port)
2897{
2898 ofproto_port->name = dpif_port->name;
2899 ofproto_port->type = dpif_port->type;
e1b1d06a 2900 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
abe529af
BP
2901}
2902
6cbbf4fa
EJ
2903static void
2904ofport_update_peer(struct ofport_dpif *ofport)
0a740f48
EJ
2905{
2906 const struct ofproto_dpif *ofproto;
6cbbf4fa 2907 struct dpif_backer *backer;
161b6042 2908 char *peer_name;
6cbbf4fa
EJ
2909
2910 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2911 return;
2912 }
2913
2914 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
7894e7da 2915 backer->need_revalidate = REV_RECONFIGURE;
0a740f48 2916
6cbbf4fa
EJ
2917 if (ofport->peer) {
2918 ofport->peer->peer = NULL;
2919 ofport->peer = NULL;
2920 }
2921
2922 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2923 if (!peer_name) {
2924 return;
0a740f48
EJ
2925 }
2926
2927 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6cbbf4fa
EJ
2928 struct ofport *peer_ofport;
2929 struct ofport_dpif *peer;
161b6042 2930 char *peer_peer;
0a740f48 2931
462abef2
EJ
2932 if (ofproto->backer != backer) {
2933 continue;
2934 }
2935
6cbbf4fa
EJ
2936 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2937 if (!peer_ofport) {
2938 continue;
2939 }
2940
2941 peer = ofport_dpif_cast(peer_ofport);
2942 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2943 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2944 peer_peer)) {
2945 ofport->peer = peer;
2946 ofport->peer->peer = ofport;
0a740f48 2947 }
161b6042 2948 free(peer_peer);
6cbbf4fa 2949
161b6042 2950 break;
0a740f48 2951 }
161b6042 2952 free(peer_name);
0a740f48
EJ
2953}
2954
0aa66d6e
EJ
2955static void
2956port_run_fast(struct ofport_dpif *ofport)
2957{
2958 if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) {
2959 struct ofpbuf packet;
2960
2961 ofpbuf_init(&packet, 0);
2962 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr);
2963 send_packet(ofport, &packet);
2964 ofpbuf_uninit(&packet);
2965 }
ccc09689
EJ
2966
2967 if (ofport->bfd && bfd_should_send_packet(ofport->bfd)) {
2968 struct ofpbuf packet;
2969
2970 ofpbuf_init(&packet, 0);
2971 bfd_put_packet(ofport->bfd, &packet, ofport->up.pp.hw_addr);
2972 send_packet(ofport, &packet);
2973 ofpbuf_uninit(&packet);
2974 }
0aa66d6e
EJ
2975}
2976
abe529af
BP
2977static void
2978port_run(struct ofport_dpif *ofport)
2979{
3e5b3fdb
EJ
2980 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2981 bool carrier_changed = carrier_seq != ofport->carrier_seq;
015e08bc
EJ
2982 bool enable = netdev_get_carrier(ofport->up.netdev);
2983
3e5b3fdb
EJ
2984 ofport->carrier_seq = carrier_seq;
2985
0aa66d6e 2986 port_run_fast(ofport);
b9ad7294 2987
abe529af 2988 if (ofport->cfm) {
4653c558
EJ
2989 int cfm_opup = cfm_get_opup(ofport->cfm);
2990
abe529af 2991 cfm_run(ofport->cfm);
4653c558
EJ
2992 enable = enable && !cfm_get_fault(ofport->cfm);
2993
2994 if (cfm_opup >= 0) {
2995 enable = enable && cfm_opup;
2996 }
abe529af 2997 }
015e08bc 2998
ccc09689
EJ
2999 if (ofport->bfd) {
3000 bfd_run(ofport->bfd);
3001 enable = enable && bfd_forwarding(ofport->bfd);
3002 }
3003
015e08bc
EJ
3004 if (ofport->bundle) {
3005 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3e5b3fdb
EJ
3006 if (carrier_changed) {
3007 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3008 }
015e08bc
EJ
3009 }
3010
daff3353
EJ
3011 if (ofport->may_enable != enable) {
3012 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
83341407 3013 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
daff3353
EJ
3014 }
3015
015e08bc 3016 ofport->may_enable = enable;
abe529af
BP
3017}
3018
3019static void
3020port_wait(struct ofport_dpif *ofport)
3021{
3022 if (ofport->cfm) {
3023 cfm_wait(ofport->cfm);
3024 }
ccc09689
EJ
3025
3026 if (ofport->bfd) {
3027 bfd_wait(ofport->bfd);
3028 }
abe529af
BP
3029}
3030
3031static int
3032port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3033 struct ofproto_port *ofproto_port)
3034{
3035 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3036 struct dpif_port dpif_port;
3037 int error;
3038
0a740f48
EJ
3039 if (sset_contains(&ofproto->ghost_ports, devname)) {
3040 const char *type = netdev_get_type_from_name(devname);
3041
3042 /* We may be called before ofproto->up.port_by_name is populated with
3043 * the appropriate ofport. For this reason, we must get the name and
3044 * type from the netdev layer directly. */
3045 if (type) {
3046 const struct ofport *ofport;
3047
3048 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3049 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3050 ofproto_port->name = xstrdup(devname);
3051 ofproto_port->type = xstrdup(type);
3052 return 0;
3053 }
3054 return ENODEV;
3055 }
3056
acf60855
JP
3057 if (!sset_contains(&ofproto->ports, devname)) {
3058 return ENODEV;
3059 }
3060 error = dpif_port_query_by_name(ofproto->backer->dpif,
3061 devname, &dpif_port);
abe529af 3062 if (!error) {
e1b1d06a 3063 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
abe529af
BP
3064 }
3065 return error;
3066}
3067
3068static int
e1b1d06a 3069port_add(struct ofproto *ofproto_, struct netdev *netdev)
abe529af
BP
3070{
3071 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
b9ad7294 3072 const char *devname = netdev_get_name(netdev);
3aa30359
BP
3073 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3074 const char *dp_port_name;
abe529af 3075
0a740f48
EJ
3076 if (netdev_vport_is_patch(netdev)) {
3077 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3078 return 0;
3079 }
3080
3aa30359 3081 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
b9ad7294 3082 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
4e022ec0 3083 odp_port_t port_no = ODPP_NONE;
7d82ab2e
KM
3084 int error;
3085
3086 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
b9ad7294
EJ
3087 if (error) {
3088 return error;
3089 }
7d82ab2e 3090 if (netdev_get_tunnel_config(netdev)) {
4e022ec0
AW
3091 simap_put(&ofproto->backer->tnl_backers,
3092 dp_port_name, odp_to_u32(port_no));
7d82ab2e 3093 }
acf60855 3094 }
b9ad7294
EJ
3095
3096 if (netdev_get_tunnel_config(netdev)) {
3097 sset_add(&ofproto->ghost_ports, devname);
b9ad7294
EJ
3098 } else {
3099 sset_add(&ofproto->ports, devname);
3100 }
3101 return 0;
3102}
3103
abe529af 3104static int
4e022ec0 3105port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
abe529af
BP
3106{
3107 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
b9ad7294 3108 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
e1b1d06a 3109 int error = 0;
abe529af 3110
b9ad7294
EJ
3111 if (!ofport) {
3112 return 0;
e1b1d06a 3113 }
b9ad7294
EJ
3114
3115 sset_find_and_delete(&ofproto->ghost_ports,
3116 netdev_get_name(ofport->up.netdev));
a614d823 3117 ofproto->backer->need_revalidate = REV_RECONFIGURE;
42943cde 3118 if (!ofport->is_tunnel) {
b9ad7294
EJ
3119 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3120 if (!error) {
abe529af
BP
3121 /* The caller is going to close ofport->up.netdev. If this is a
3122 * bonded port, then the bond is using that netdev, so remove it
3123 * from the bond. The client will need to reconfigure everything
3124 * after deleting ports, so then the slave will get re-added. */
3125 bundle_remove(&ofport->up);
3126 }
3127 }
3128 return error;
3129}
3130
6527c598
PS
3131static int
3132port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3133{
3134 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3135 int error;
3136
8844e035
EJ
3137 push_all_stats();
3138
6527c598
PS
3139 error = netdev_get_stats(ofport->up.netdev, stats);
3140
ee382d89 3141 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
6527c598
PS
3142 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3143
3144 /* ofproto->stats.tx_packets represents packets that we created
3145 * internally and sent to some port (e.g. packets sent with
3146 * send_packet()). Account for them as if they had come from
3147 * OFPP_LOCAL and got forwarded. */
3148
3149 if (stats->rx_packets != UINT64_MAX) {
3150 stats->rx_packets += ofproto->stats.tx_packets;
3151 }
3152
3153 if (stats->rx_bytes != UINT64_MAX) {
3154 stats->rx_bytes += ofproto->stats.tx_bytes;
3155 }
3156
3157 /* ofproto->stats.rx_packets represents packets that were received on
3158 * some port and we processed internally and dropped (e.g. STP).
4e090bc7 3159 * Account for them as if they had been forwarded to OFPP_LOCAL. */
6527c598
PS
3160
3161 if (stats->tx_packets != UINT64_MAX) {
3162 stats->tx_packets += ofproto->stats.rx_packets;
3163 }
3164
3165 if (stats->tx_bytes != UINT64_MAX) {
3166 stats->tx_bytes += ofproto->stats.rx_bytes;
3167 }
3168 }
3169
3170 return error;
3171}
3172
abe529af 3173struct port_dump_state {
acf60855
JP
3174 uint32_t bucket;
3175 uint32_t offset;
0a740f48 3176 bool ghost;
da78d43d
BP
3177
3178 struct ofproto_port port;
3179 bool has_port;
abe529af
BP
3180};
3181
3182static int
acf60855 3183port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
abe529af 3184{
0a740f48 3185 *statep = xzalloc(sizeof(struct port_dump_state));
abe529af
BP
3186 return 0;
3187}
3188
3189static int
b9ad7294 3190port_dump_next(const struct ofproto *ofproto_, void *state_,
abe529af
BP
3191 struct ofproto_port *port)
3192{
e1b1d06a 3193 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
abe529af 3194 struct port_dump_state *state = state_;
0a740f48 3195 const struct sset *sset;
acf60855 3196 struct sset_node *node;
abe529af 3197
da78d43d
BP
3198 if (state->has_port) {
3199 ofproto_port_destroy(&state->port);
3200 state->has_port = false;
3201 }
0a740f48
EJ
3202 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3203 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
acf60855
JP
3204 int error;
3205
da78d43d
BP
3206 error = port_query_by_name(ofproto_, node->name, &state->port);
3207 if (!error) {
3208 *port = state->port;
3209 state->has_port = true;
3210 return 0;
3211 } else if (error != ENODEV) {
acf60855
JP
3212 return error;
3213 }
abe529af 3214 }
acf60855 3215
0a740f48
EJ
3216 if (!state->ghost) {
3217 state->ghost = true;
3218 state->bucket = 0;
3219 state->offset = 0;
3220 return port_dump_next(ofproto_, state_, port);
3221 }
3222
acf60855 3223 return EOF;
abe529af
BP
3224}
3225
3226static int
3227port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3228{
3229 struct port_dump_state *state = state_;
3230
da78d43d
BP
3231 if (state->has_port) {
3232 ofproto_port_destroy(&state->port);
3233 }
abe529af
BP
3234 free(state);
3235 return 0;
3236}
3237
3238static int
3239port_poll(const struct ofproto *ofproto_, char **devnamep)
3240{
3241 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855
JP
3242
3243 if (ofproto->port_poll_errno) {
3244 int error = ofproto->port_poll_errno;
3245 ofproto->port_poll_errno = 0;
3246 return error;
3247 }
3248
3249 if (sset_is_empty(&ofproto->port_poll_set)) {
3250 return EAGAIN;
3251 }
3252
3253 *devnamep = sset_pop(&ofproto->port_poll_set);
3254 return 0;
abe529af
BP
3255}
3256
3257static void
3258port_poll_wait(const struct ofproto *ofproto_)
3259{
3260 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
acf60855 3261 dpif_port_poll_wait(ofproto->backer->dpif);
abe529af
BP
3262}
3263
3264static int
3265port_is_lacp_current(const struct ofport *ofport_)
3266{
3267 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3268 return (ofport->bundle && ofport->bundle->lacp
3269 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3270 : -1);
3271}
3272\f
3273/* Upcall handling. */
3274
501f8d1f
BP
3275/* Flow miss batching.
3276 *
3277 * Some dpifs implement operations faster when you hand them off in a batch.
3278 * To allow batching, "struct flow_miss" queues the dpif-related work needed
3279 * for a given flow. Each "struct flow_miss" corresponds to sending one or
3280 * more packets, plus possibly installing the flow in the dpif.
3281 *
3282 * So far we only batch the operations that affect flow setup time the most.
3283 * It's possible to batch more than that, but the benefit might be minimal. */
3284struct flow_miss {
3285 struct hmap_node hmap_node;
acf60855 3286 struct ofproto_dpif *ofproto;
501f8d1f 3287 struct flow flow;
b0f7b9b5 3288 enum odp_key_fitness key_fitness;
501f8d1f
BP
3289 const struct nlattr *key;
3290 size_t key_len;
3291 struct list packets;
6a7e895f 3292 enum dpif_upcall_type upcall_type;
501f8d1f
BP
3293};
3294
3295struct flow_miss_op {
c2b565b5 3296 struct dpif_op dpif_op;
bbafd73b
EJ
3297
3298 uint64_t slow_stub[128 / 8]; /* Buffer for compose_slow_path() */
3299 struct xlate_out xout;
3300 bool xout_garbage; /* 'xout' needs to be uninitialized? */
9b1a48c2 3301
d445cc16
JP
3302 struct ofpbuf mask; /* Flow mask for "put" ops. */
3303 struct odputil_keybuf maskbuf;
3304
9b1a48c2
JP
3305 /* If this is a "put" op, then a pointer to the subfacet that should
3306 * be marked as uninstalled if the operation fails. */
3307 struct subfacet *subfacet;
501f8d1f
BP
3308};
3309
62cd7072
BP
3310/* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
3311 * OpenFlow controller as necessary according to their individual
29ebe880 3312 * configurations. */
62cd7072 3313static void
a39edbd4 3314send_packet_in_miss(struct ofproto_dpif *ofproto, const struct ofpbuf *packet,
29ebe880 3315 const struct flow *flow)
62cd7072
BP
3316{
3317 struct ofputil_packet_in pin;
3318
3e3252fa
EJ
3319 pin.packet = packet->data;
3320 pin.packet_len = packet->size;
62cd7072 3321 pin.reason = OFPR_NO_MATCH;
a7349929 3322 pin.controller_id = 0;
54834960
EJ
3323
3324 pin.table_id = 0;
3325 pin.cookie = 0;
3326
62cd7072 3327 pin.send_len = 0; /* not used for flow table misses */
5d6c3af0
EJ
3328
3329 flow_get_metadata(flow, &pin.fmd);
3330
d8653c38 3331 connmgr_send_packet_in(ofproto->up.connmgr, &pin);
62cd7072
BP
3332}
3333
501f8d1f 3334static struct flow_miss *
ddbc5954
JP
3335flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
3336 const struct flow *flow, uint32_t hash)
abe529af 3337{
501f8d1f 3338 struct flow_miss *miss;
abe529af 3339
501f8d1f 3340 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
ddbc5954 3341 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
501f8d1f
BP
3342 return miss;
3343 }
3344 }
abe529af 3345
b23cdad9 3346 return NULL;
501f8d1f 3347}
abe529af 3348
9d6ac44e
BP
3349/* Partially Initializes 'op' as an "execute" operation for 'miss' and
3350 * 'packet'. The caller must initialize op->actions and op->actions_len. If
3351 * 'miss' is associated with a subfacet the caller must also initialize the
3352 * returned op->subfacet, and if anything needs to be freed after processing
3353 * the op, the caller must initialize op->garbage also. */
501f8d1f 3354static void
9d6ac44e
BP
3355init_flow_miss_execute_op(struct flow_miss *miss, struct ofpbuf *packet,
3356 struct flow_miss_op *op)
501f8d1f 3357{
4e022ec0
AW
3358 if (miss->flow.in_port.ofp_port
3359 != vsp_realdev_to_vlandev(miss->ofproto, miss->flow.in_port.ofp_port,
47b0deae 3360 miss->flow.vlan_tci)) {
9d6ac44e
BP
3361 /* This packet was received on a VLAN splinter port. We
3362 * added a VLAN to the packet to make the packet resemble
3363 * the flow, but the actions were composed assuming that
3364 * the packet contained no VLAN. So, we must remove the
3365 * VLAN header from the packet before trying to execute the
3366 * actions. */
3367 eth_pop_vlan(packet);
3368 }
3369
9b1a48c2 3370 op->subfacet = NULL;
bbafd73b 3371 op->xout_garbage = false;
9d6ac44e
BP
3372 op->dpif_op.type = DPIF_OP_EXECUTE;
3373 op->dpif_op.u.execute.key = miss->key;
3374 op->dpif_op.u.execute.key_len = miss->key_len;
3375 op->dpif_op.u.execute.packet = packet;
d445cc16 3376 ofpbuf_use_stack(&op->mask, &op->maskbuf, sizeof op->maskbuf);
9d6ac44e
BP
3377}
3378
3379/* Helper for handle_flow_miss_without_facet() and
3380 * handle_flow_miss_with_facet(). */
3381static void
b7e2f6b3
EJ
3382handle_flow_miss_common(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
3383 const struct flow *flow, bool fail_open)
9d6ac44e 3384{
b7e2f6b3 3385 if (fail_open) {
9d6ac44e
BP
3386 /*
3387 * Extra-special case for fail-open mode.
3388 *
3389 * We are in fail-open mode and the packet matched the fail-open
3390 * rule, but we are connected to a controller too. We should send
3391 * the packet up to the controller in the hope that it will try to
3392 * set up a flow and thereby allow us to exit fail-open.
3393 *
3394 * See the top-level comment in fail-open.c for more information.
3395 */
3396 send_packet_in_miss(ofproto, packet, flow);
3397 }
3398}
3399
3400/* Figures out whether a flow that missed in 'ofproto', whose details are in
bcd2633a
JP
3401 * 'miss' masked by 'wc', is likely to be worth tracking in detail in userspace
3402 * and (usually) installing a datapath flow. The answer is usually "yes" (a
3403 * return value of true). However, for short flows the cost of bookkeeping is
3404 * much higher than the benefits, so when the datapath holds a large number of
3405 * flows we impose some heuristics to decide which flows are likely to be worth
3406 * tracking. */
9d6ac44e 3407static bool
bcd2633a 3408flow_miss_should_make_facet(struct flow_miss *miss, struct flow_wildcards *wc)
9d6ac44e 3409{
04d08d54 3410 struct dpif_backer *backer = miss->ofproto->backer;
bcd2633a 3411 uint32_t hash;
04d08d54 3412
7155fa52
JS
3413 switch (flow_miss_model) {
3414 case OFPROTO_HANDLE_MISS_AUTO:
3415 break;
3416 case OFPROTO_HANDLE_MISS_WITH_FACETS:
3417 return true;
3418 case OFPROTO_HANDLE_MISS_WITHOUT_FACETS:
3419 return false;
3420 }
3421
04d08d54 3422 if (!backer->governor) {
9d6ac44e
BP
3423 size_t n_subfacets;
3424
04d08d54 3425 n_subfacets = hmap_count(&backer->subfacets);
380f49c4 3426 if (n_subfacets * 2 <= flow_eviction_threshold) {
9d6ac44e
BP
3427 return true;
3428 }
3429
c985ec94 3430 backer->governor = governor_create();
9d6ac44e
BP
3431 }
3432
bcd2633a 3433 hash = flow_hash_in_wildcards(&miss->flow, wc, 0);
04d08d54 3434 return governor_should_install_flow(backer->governor, hash,
9d6ac44e
BP
3435 list_size(&miss->packets));
3436}
3437
bcd2633a
JP
3438/* Handles 'miss' without creating a facet or subfacet or creating any datapath
3439 * flow. 'miss->flow' must have matched 'rule' and been xlated into 'xout'.
3440 * May add an "execute" operation to 'ops' and increment '*n_ops'. */
9d6ac44e 3441static void
bcd2633a
JP
3442handle_flow_miss_without_facet(struct rule_dpif *rule, struct xlate_out *xout,
3443 struct flow_miss *miss,
9d6ac44e
BP
3444 struct flow_miss_op *ops, size_t *n_ops)
3445{
530a1d91 3446 struct ofpbuf *packet;
2b459b83 3447
9d6ac44e 3448 LIST_FOR_EACH (packet, list_node, &miss->packets) {
abe529af 3449
9d6ac44e 3450 COVERAGE_INC(facet_suppress);
501f8d1f 3451
b7e2f6b3
EJ
3452 handle_flow_miss_common(miss->ofproto, packet, &miss->flow,
3453 rule->up.cr.priority == FAIL_OPEN_PRIORITY);
7820e253 3454
bcd2633a
JP
3455 if (xout->slow) {
3456 struct xlate_in xin;
abe529af 3457
47b0deae 3458 xlate_in_init(&xin, miss->ofproto, &miss->flow, rule, 0, packet);
bcd2633a
JP
3459 xlate_actions_for_side_effects(&xin);
3460 }
abe529af 3461
bcd2633a
JP
3462 if (xout->odp_actions.size) {
3463 struct flow_miss_op *op = &ops[*n_ops];
9d6ac44e
BP
3464 struct dpif_execute *execute = &op->dpif_op.u.execute;
3465
3466 init_flow_miss_execute_op(miss, packet, op);
bcd2633a 3467 xlate_out_copy(&op->xout, xout);
bbafd73b
EJ
3468 execute->actions = op->xout.odp_actions.data;
3469 execute->actions_len = op->xout.odp_actions.size;
3470 op->xout_garbage = true;
9d6ac44e
BP
3471
3472 (*n_ops)++;
9d6ac44e 3473 }
abe529af 3474 }
9d6ac44e
BP
3475}
3476
3477/* Handles 'miss', which matches 'facet'. May add any required datapath
459b16a1
BP
3478 * operations to 'ops', incrementing '*n_ops' for each new op.
3479 *
3480 * All of the packets in 'miss' are considered to have arrived at time 'now'.
3481 * This is really important only for new facets: if we just called time_msec()
3482 * here, then the new subfacet or its packets could look (occasionally) as
3483 * though it was used some time after the facet was used. That can make a
3484 * one-packet flow look like it has a nonzero duration, which looks odd in
bcd2633a
JP
3485 * e.g. NetFlow statistics.
3486 *
3487 * If non-null, 'stats' will be folded into 'facet'. */
9d6ac44e
BP
3488static void
3489handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
bcd2633a 3490 long long int now, struct dpif_flow_stats *stats,
9d6ac44e
BP
3491 struct flow_miss_op *ops, size_t *n_ops)
3492{
6a7e895f 3493 enum subfacet_path want_path;
9d6ac44e
BP
3494 struct subfacet *subfacet;
3495 struct ofpbuf *packet;
abe529af 3496
bcd2633a 3497 want_path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
b0f7b9b5 3498
530a1d91 3499 LIST_FOR_EACH (packet, list_node, &miss->packets) {
5fe20d5d 3500 struct flow_miss_op *op = &ops[*n_ops];
67d91f78 3501
b7e2f6b3
EJ
3502 handle_flow_miss_common(miss->ofproto, packet, &miss->flow,
3503 facet->fail_open);
501f8d1f 3504
4dff9097 3505 if (want_path != SF_FAST_PATH) {
b7e2f6b3 3506 struct rule_dpif *rule;
bbafd73b 3507 struct xlate_in xin;
454a77e5 3508
b7e2f6b3
EJ
3509 rule = rule_dpif_lookup(facet->ofproto, &facet->flow, NULL);
3510 xlate_in_init(&xin, facet->ofproto, &miss->flow, rule, 0, packet);
bbafd73b 3511 xlate_actions_for_side_effects(&xin);
501f8d1f 3512 }
67d91f78 3513
bbafd73b 3514 if (facet->xout.odp_actions.size) {
9d6ac44e 3515 struct dpif_execute *execute = &op->dpif_op.u.execute;
8338659a 3516
9d6ac44e 3517 init_flow_miss_execute_op(miss, packet, op);
bbafd73b
EJ
3518 execute->actions = facet->xout.odp_actions.data,
3519 execute->actions_len = facet->xout.odp_actions.size;
9d6ac44e 3520 (*n_ops)++;
5fe20d5d 3521 }
501f8d1f
BP
3522 }
3523
a41d7bf2
JP
3524 /* Don't install the flow if it's the result of the "userspace"
3525 * action for an already installed facet. This can occur when a
3526 * datapath flow with wildcards has a "userspace" action and flows
3527 * sent to userspace result in a different subfacet, which will then
3528 * be rejected as overlapping by the datapath. */
3529 if (miss->upcall_type == DPIF_UC_ACTION
3530 && !list_is_empty(&facet->subfacets)) {
3531 if (stats) {
3532 facet->used = MAX(facet->used, stats->used);
3533 facet->packet_count += stats->n_packets;
3534 facet->byte_count += stats->n_bytes;
3535 facet->tcp_flags |= stats->tcp_flags;
3536 }
3537 return;
3538 }
3539
3540 subfacet = subfacet_create(facet, miss, now);
3541 if (stats) {
3542 subfacet_update_stats(subfacet, stats);
3543 }
3544
24ce4b08 3545 if (subfacet->path != want_path) {
501f8d1f 3546 struct flow_miss_op *op = &ops[(*n_ops)++];
c2b565b5 3547 struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
501f8d1f 3548
c84451a6
EJ
3549 subfacet->path = want_path;
3550
d445cc16 3551 ofpbuf_use_stack(&op->mask, &op->maskbuf, sizeof op->maskbuf);
6b35e630
JP
3552 if (enable_megaflows) {
3553 odp_flow_key_from_mask(&op->mask, &facet->xout.wc.masks,
3554 &miss->flow, UINT32_MAX);
3555 }
d445cc16 3556
bbafd73b 3557 op->xout_garbage = false;
c2b565b5 3558 op->dpif_op.type = DPIF_OP_FLOW_PUT;
9b1a48c2 3559 op->subfacet = subfacet;
0f74f625 3560 put->flags = DPIF_FP_CREATE;
501f8d1f
BP
3561 put->key = miss->key;
3562 put->key_len = miss->key_len;
d445cc16
JP
3563 put->mask = op->mask.data;
3564 put->mask_len = op->mask.size;
3565
6a7e895f 3566 if (want_path == SF_FAST_PATH) {
bbafd73b
EJ
3567 put->actions = facet->xout.odp_actions.data;
3568 put->actions_len = facet->xout.odp_actions.size;
6a7e895f 3569 } else {
b7e2f6b3 3570 compose_slow_path(facet->ofproto, &miss->flow, facet->xout.slow,
bbafd73b 3571 op->slow_stub, sizeof op->slow_stub,
6a7e895f
BP
3572 &put->actions, &put->actions_len);
3573 }
501f8d1f
BP
3574 put->stats = NULL;
3575 }
3576}
3577
acf60855
JP
3578/* Handles flow miss 'miss'. May add any required datapath operations
3579 * to 'ops', incrementing '*n_ops' for each new op. */
9d6ac44e 3580static void
acf60855
JP
3581handle_flow_miss(struct flow_miss *miss, struct flow_miss_op *ops,
3582 size_t *n_ops)
9d6ac44e 3583{
acf60855 3584 struct ofproto_dpif *ofproto = miss->ofproto;
bcd2633a
JP
3585 struct dpif_flow_stats stats__;
3586 struct dpif_flow_stats *stats = &stats__;
3587 struct ofpbuf *packet;
9d6ac44e 3588 struct facet *facet;
459b16a1 3589 long long int now;
9d6ac44e 3590
bcd2633a
JP
3591 now = time_msec();
3592 memset(stats, 0, sizeof *stats);
3593 stats->used = now;
3594 LIST_FOR_EACH (packet, list_node, &miss->packets) {
3595 stats->tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
3596 stats->n_bytes += packet->size;
3597 stats->n_packets++;
3598 }
9d6ac44e 3599
bcd2633a 3600 facet = facet_lookup_valid(ofproto, &miss->flow);
9d6ac44e 3601 if (!facet) {
bcd2633a
JP
3602 struct flow_wildcards wc;
3603 struct rule_dpif *rule;
3604 struct xlate_out xout;
3605 struct xlate_in xin;
3606
3607 flow_wildcards_init_catchall(&wc);
3608 rule = rule_dpif_lookup(ofproto, &miss->flow, &wc);
3609 rule_credit_stats(rule, stats);
3610
47b0deae
EJ
3611 xlate_in_init(&xin, ofproto, &miss->flow, rule, stats->tcp_flags,
3612 NULL);
bcd2633a
JP
3613 xin.resubmit_stats = stats;
3614 xin.may_learn = true;
3615 xlate_actions(&xin, &xout);
3616 flow_wildcards_or(&xout.wc, &xout.wc, &wc);
3617
ba33dd03
EJ
3618 /* There does not exist a bijection between 'struct flow' and datapath
3619 * flow keys with fitness ODP_FIT_TO_LITTLE. This breaks a fundamental
3620 * assumption used throughout the facet and subfacet handling code.
3621 * Since we have to handle these misses in userspace anyway, we simply
ec9f40dc 3622 * skip facet creation, avoiding the problem altogether. */
ba33dd03 3623 if (miss->key_fitness == ODP_FIT_TOO_LITTLE
bcd2633a
JP
3624 || !flow_miss_should_make_facet(miss, &xout.wc)) {
3625 handle_flow_miss_without_facet(rule, &xout, miss, ops, n_ops);
9d6ac44e
BP
3626 return;
3627 }
3628
bcd2633a
JP
3629 facet = facet_create(miss, rule, &xout, stats);
3630 stats = NULL;
9d6ac44e 3631 }
bcd2633a 3632 handle_flow_miss_with_facet(miss, facet, now, stats, ops, n_ops);
9d6ac44e
BP
3633}
3634
8f73d537
EJ
3635static struct drop_key *
3636drop_key_lookup(const struct dpif_backer *backer, const struct nlattr *key,
3637 size_t key_len)
3638{
3639 struct drop_key *drop_key;
3640
3641 HMAP_FOR_EACH_WITH_HASH (drop_key, hmap_node, hash_bytes(key, key_len, 0),
3642 &backer->drop_keys) {
3643 if (drop_key->key_len == key_len
3644 && !memcmp(drop_key->key, key, key_len)) {
3645 return drop_key;
3646 }
3647 }
3648 return NULL;
3649}
3650
3651static void
3652drop_key_clear(struct dpif_backer *backer)
3653{
3654 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3655 struct drop_key *drop_key, *next;
3656
3657 HMAP_FOR_EACH_SAFE (drop_key, next, hmap_node, &backer->drop_keys) {
3658 int error;
3659
3660 error = dpif_flow_del(backer->dpif, drop_key->key, drop_key->key_len,
3661 NULL);
3662 if (error && !VLOG_DROP_WARN(&rl)) {
3663 struct ds ds = DS_EMPTY_INITIALIZER;
3664 odp_flow_key_format(drop_key->key, drop_key->key_len, &ds);
10a89ef0
BP
3665 VLOG_WARN("Failed to delete drop key (%s) (%s)",
3666 ovs_strerror(error), ds_cstr(&ds));
8f73d537
EJ
3667 ds_destroy(&ds);
3668 }
3669
3670 hmap_remove(&backer->drop_keys, &drop_key->hmap_node);
3671 free(drop_key->key);
3672 free(drop_key);
3673 }
3674}
3675
501f8d1f 3676static void
acf60855 3677handle_miss_upcalls(struct dpif_backer *backer, struct dpif_upcall *upcalls,
501f8d1f
BP
3678 size_t n_upcalls)
3679{
3680 struct dpif_upcall *upcall;
b23cdad9
BP
3681 struct flow_miss *miss;
3682 struct flow_miss misses[FLOW_MISS_MAX_BATCH];
501f8d1f 3683 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
c2b565b5 3684 struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
501f8d1f 3685 struct hmap todo;
b23cdad9 3686 int n_misses;
501f8d1f
BP
3687 size_t n_ops;
3688 size_t i;
3689
3690 if (!n_upcalls) {
3691 return;
3692 }
3693
3694 /* Construct the to-do list.
3695 *
3696 * This just amounts to extracting the flow from each packet and sticking
3697 * the packets that have the same flow in the same "flow_miss" structure so
3698 * that we can process them together. */
3699 hmap_init(&todo);
b23cdad9 3700 n_misses = 0;
501f8d1f 3701 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
b23cdad9
BP
3702 struct flow_miss *miss = &misses[n_misses];
3703 struct flow_miss *existing_miss;
acf60855 3704 struct ofproto_dpif *ofproto;
4e022ec0 3705 odp_port_t odp_in_port;
1d446463 3706 struct flow flow;
b23cdad9 3707 uint32_t hash;
e09ee259 3708 int error;
501f8d1f 3709
8449c4d6
EJ
3710 error = xlate_receive(backer, upcall->packet, upcall->key,
3711 upcall->key_len, &flow, &miss->key_fitness,
3712 &ofproto, &odp_in_port);
e09ee259 3713 if (error == ENODEV) {
8f73d537
EJ
3714 struct drop_key *drop_key;
3715
c3385516
AW
3716 /* Received packet on datapath port for which we couldn't
3717 * associate an ofproto. This can happen if a port is removed
3718 * while traffic is being received. Print a rate-limited message
8f73d537
EJ
3719 * in case it happens frequently. Install a drop flow so
3720 * that future packets of the flow are inexpensively dropped
3721 * in the kernel. */
c3385516
AW
3722 VLOG_INFO_RL(&rl, "received packet on unassociated datapath port "
3723 "%"PRIu32, odp_in_port);
8f73d537
EJ
3724
3725 drop_key = drop_key_lookup(backer, upcall->key, upcall->key_len);
3726 if (!drop_key) {
92e02bc6
AZ
3727 int ret;
3728 ret = dpif_flow_put(backer->dpif,
3729 DPIF_FP_CREATE | DPIF_FP_MODIFY,
3730 upcall->key, upcall->key_len,
3731 NULL, 0, NULL, 0, NULL);
3732
3733 if (!ret) {
3734 drop_key = xmalloc(sizeof *drop_key);
3735 drop_key->key = xmemdup(upcall->key, upcall->key_len);
3736 drop_key->key_len = upcall->key_len;
3737
3738 hmap_insert(&backer->drop_keys, &drop_key->hmap_node,
3739 hash_bytes(drop_key->key, drop_key->key_len, 0));
3740 }
8f73d537
EJ
3741 }
3742 continue;
acf60855 3743 }
e09ee259 3744 if (error) {
b0f7b9b5
BP
3745 continue;
3746 }
735d7efb
AZ
3747
3748 ofproto->n_missed++;
72e8bf28 3749 flow_extract(upcall->packet, flow.skb_priority, flow.skb_mark,
4e022ec0 3750 &flow.tunnel, &flow.in_port, &miss->flow);
501f8d1f 3751
501f8d1f 3752 /* Add other packets to a to-do list. */
b23cdad9 3753 hash = flow_hash(&miss->flow, 0);
ddbc5954 3754 existing_miss = flow_miss_find(&todo, ofproto, &miss->flow, hash);
b23cdad9
BP
3755 if (!existing_miss) {
3756 hmap_insert(&todo, &miss->hmap_node, hash);
acf60855 3757 miss->ofproto = ofproto;
b23cdad9
BP
3758 miss->key = upcall->key;
3759 miss->key_len = upcall->key_len;
6a7e895f 3760 miss->upcall_type = upcall->type;
b23cdad9
BP
3761 list_init(&miss->packets);
3762
3763 n_misses++;
3764 } else {
3765 miss = existing_miss;
3766 }
501f8d1f
BP
3767 list_push_back(&miss->packets, &upcall->packet->list_node);
3768 }
3769
3770 /* Process each element in the to-do list, constructing the set of
3771 * operations to batch. */
3772 n_ops = 0;
33bb0caa 3773 HMAP_FOR_EACH (miss, hmap_node, &todo) {
acf60855 3774 handle_flow_miss(miss, flow_miss_ops, &n_ops);
abe529af 3775 }
cb22974d 3776 ovs_assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
501f8d1f
BP
3777
3778 /* Execute batch. */
3779 for (i = 0; i < n_ops; i++) {
3780 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3781 }
acf60855 3782 dpif_operate(backer->dpif, dpif_ops, n_ops);
501f8d1f 3783
501f8d1f 3784 for (i = 0; i < n_ops; i++) {
9b1a48c2
JP
3785 if (dpif_ops[i]->error != 0
3786 && flow_miss_ops[i].dpif_op.type == DPIF_OP_FLOW_PUT
3787 && flow_miss_ops[i].subfacet) {
3788 struct subfacet *subfacet = flow_miss_ops[i].subfacet;
3789
3790 COVERAGE_INC(subfacet_install_fail);
3791
cfd5c9eb
JP
3792 /* Zero-out subfacet counters when installation failed, but
3793 * datapath reported hits. This should not happen and
3794 * indicates a bug, since if the datapath flow exists, we
3795 * should not be attempting to create a new subfacet. A
3796 * buggy datapath could trigger this, so just zero out the
3797 * counters and log an error. */
3798 if (subfacet->dp_packet_count || subfacet->dp_byte_count) {
3799 VLOG_ERR_RL(&rl, "failed to install subfacet for which "
3800 "datapath reported hits");
3801 subfacet->dp_packet_count = subfacet->dp_byte_count = 0;
3802 }
3803
9b1a48c2
JP
3804 subfacet->path = SF_NOT_INSTALLED;
3805 }
3806
3807 /* Free memory. */
bbafd73b
EJ
3808 if (flow_miss_ops[i].xout_garbage) {
3809 xlate_out_uninit(&flow_miss_ops[i].xout);
3810 }
501f8d1f 3811 }
33bb0caa 3812 hmap_destroy(&todo);
abe529af
BP
3813}
3814
29089a54
RL
3815static enum { SFLOW_UPCALL, MISS_UPCALL, BAD_UPCALL, FLOW_SAMPLE_UPCALL,
3816 IPFIX_UPCALL }
6a7e895f
BP
3817classify_upcall(const struct dpif_upcall *upcall)
3818{
29089a54 3819 size_t userdata_len;
6a7e895f
BP
3820 union user_action_cookie cookie;
3821
3822 /* First look at the upcall type. */
3823 switch (upcall->type) {
3824 case DPIF_UC_ACTION:
3825 break;
3826
3827 case DPIF_UC_MISS:
3828 return MISS_UPCALL;
3829
3830 case DPIF_N_UC_TYPES:
3831 default:
3832 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3833 return BAD_UPCALL;
3834 }
3835
3836 /* "action" upcalls need a closer look. */
e995e3df
BP
3837 if (!upcall->userdata) {
3838 VLOG_WARN_RL(&rl, "action upcall missing cookie");
3839 return BAD_UPCALL;
3840 }
29089a54
RL
3841 userdata_len = nl_attr_get_size(upcall->userdata);
3842 if (userdata_len < sizeof cookie.type
3843 || userdata_len > sizeof cookie) {
e995e3df 3844 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %zu",
29089a54 3845 userdata_len);
e995e3df
BP
3846 return BAD_UPCALL;
3847 }
29089a54
RL
3848 memset(&cookie, 0, sizeof cookie);
3849 memcpy(&cookie, nl_attr_get(upcall->userdata), userdata_len);
3850 if (userdata_len == sizeof cookie.sflow
3851 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
6a7e895f 3852 return SFLOW_UPCALL;
29089a54
RL
3853 } else if (userdata_len == sizeof cookie.slow_path
3854 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
6a7e895f 3855 return MISS_UPCALL;
29089a54
RL
3856 } else if (userdata_len == sizeof cookie.flow_sample
3857 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
3858 return FLOW_SAMPLE_UPCALL;
3859 } else if (userdata_len == sizeof cookie.ipfix
3860 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
3861 return IPFIX_UPCALL;
3862 } else {
3863 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
3864 " and size %zu", cookie.type, userdata_len);
6a7e895f
BP
3865 return BAD_UPCALL;
3866 }
3867}
3868
abe529af 3869static void
acf60855 3870handle_sflow_upcall(struct dpif_backer *backer,
6a7e895f 3871 const struct dpif_upcall *upcall)
abe529af 3872{
acf60855 3873 struct ofproto_dpif *ofproto;
1673e0e4 3874 union user_action_cookie cookie;
e84173dc 3875 struct flow flow;
4e022ec0 3876 odp_port_t odp_in_port;
abe529af 3877
8449c4d6
EJ
3878 if (xlate_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3879 &flow, NULL, &ofproto, &odp_in_port)
e09ee259 3880 || !ofproto->sflow) {
e84173dc
BP
3881 return;
3882 }
3883
29089a54
RL
3884 memset(&cookie, 0, sizeof cookie);
3885 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof cookie.sflow);
e1b1d06a
JP
3886 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
3887 odp_in_port, &cookie);
6ff686f2
PS
3888}
3889
29089a54
RL
3890static void
3891handle_flow_sample_upcall(struct dpif_backer *backer,
3892 const struct dpif_upcall *upcall)
3893{
3894 struct ofproto_dpif *ofproto;
3895 union user_action_cookie cookie;
3896 struct flow flow;
3897
8449c4d6
EJ
3898 if (xlate_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3899 &flow, NULL, &ofproto, NULL)
29089a54
RL
3900 || !ofproto->ipfix) {
3901 return;
3902 }
3903
3904 memset(&cookie, 0, sizeof cookie);
3905 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof cookie.flow_sample);
3906
3907 /* The flow reflects exactly the contents of the packet. Sample
3908 * the packet using it. */
3909 dpif_ipfix_flow_sample(ofproto->ipfix, upcall->packet, &flow,
3910 cookie.flow_sample.collector_set_id,
3911 cookie.flow_sample.probability,
3912 cookie.flow_sample.obs_domain_id,
3913 cookie.flow_sample.obs_point_id);
3914}
3915
3916static void
3917handle_ipfix_upcall(struct dpif_backer *backer,
3918 const struct dpif_upcall *upcall)
3919{
3920 struct ofproto_dpif *ofproto;
3921 struct flow flow;
3922
8449c4d6
EJ
3923 if (xlate_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3924 &flow, NULL, &ofproto, NULL)
29089a54
RL
3925 || !ofproto->ipfix) {
3926 return;
3927 }
3928
3929 /* The flow reflects exactly the contents of the packet. Sample
3930 * the packet using it. */
3931 dpif_ipfix_bridge_sample(ofproto->ipfix, upcall->packet, &flow);
3932}
3933
9b16c439 3934static int
acf60855 3935handle_upcalls(struct dpif_backer *backer, unsigned int max_batch)
6ff686f2 3936{
9b16c439 3937 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
90a7c55e
BP
3938 struct ofpbuf miss_bufs[FLOW_MISS_MAX_BATCH];
3939 uint64_t miss_buf_stubs[FLOW_MISS_MAX_BATCH][4096 / 8];
3940 int n_processed;
9b16c439
BP
3941 int n_misses;
3942 int i;
abe529af 3943
cb22974d 3944 ovs_assert(max_batch <= FLOW_MISS_MAX_BATCH);
abe529af 3945
9b16c439 3946 n_misses = 0;
90a7c55e 3947 for (n_processed = 0; n_processed < max_batch; n_processed++) {
9b16c439 3948 struct dpif_upcall *upcall = &misses[n_misses];
90a7c55e 3949 struct ofpbuf *buf = &miss_bufs[n_misses];
9b16c439
BP
3950 int error;
3951
90a7c55e
BP
3952 ofpbuf_use_stub(buf, miss_buf_stubs[n_misses],
3953 sizeof miss_buf_stubs[n_misses]);
acf60855 3954 error = dpif_recv(backer->dpif, upcall, buf);
9b16c439 3955 if (error) {
90a7c55e 3956 ofpbuf_uninit(buf);
9b16c439
BP
3957 break;
3958 }
3959
6a7e895f
BP
3960 switch (classify_upcall(upcall)) {
3961 case MISS_UPCALL:
9b16c439
BP
3962 /* Handle it later. */
3963 n_misses++;
3964 break;
3965
6a7e895f 3966 case SFLOW_UPCALL:
acf60855 3967 handle_sflow_upcall(backer, upcall);
6a7e895f
BP
3968 ofpbuf_uninit(buf);
3969 break;
3970
29089a54
RL
3971 case FLOW_SAMPLE_UPCALL:
3972 handle_flow_sample_upcall(backer, upcall);
3973 ofpbuf_uninit(buf);
3974 break;
3975
3976 case IPFIX_UPCALL:
3977 handle_ipfix_upcall(backer, upcall);
3978 ofpbuf_uninit(buf);
3979 break;
3980
6a7e895f
BP
3981 case BAD_UPCALL:
3982 ofpbuf_uninit(buf);
9b16c439
BP
3983 break;
3984 }
abe529af 3985 }
9b16c439 3986
6a7e895f 3987 /* Handle deferred MISS_UPCALL processing. */
acf60855 3988 handle_miss_upcalls(backer, misses, n_misses);
90a7c55e
BP
3989 for (i = 0; i < n_misses; i++) {
3990 ofpbuf_uninit(&miss_bufs[i]);
3991 }
9b16c439 3992
90a7c55e 3993 return n_processed;
abe529af
BP
3994}
3995\f
3996/* Flow expiration. */
3997
04d08d54 3998static int subfacet_max_idle(const struct dpif_backer *);
acf60855 3999static void update_stats(struct dpif_backer *);
abe529af 4000static void rule_expire(struct rule_dpif *);
04d08d54 4001static void expire_subfacets(struct dpif_backer *, int dp_max_idle);
abe529af
BP
4002
4003/* This function is called periodically by run(). Its job is to collect
4004 * updates for the flows that have been installed into the datapath, most
4005 * importantly when they last were used, and then use that information to
4006 * expire flows that have not been used recently.
4007 *
4008 * Returns the number of milliseconds after which it should be called again. */
4009static int
acf60855 4010expire(struct dpif_backer *backer)
abe529af 4011{
acf60855 4012 struct ofproto_dpif *ofproto;
dc54ef36 4013 size_t n_subfacets;
04d08d54 4014 int max_idle;
abe529af 4015
8f73d537
EJ
4016 /* Periodically clear out the drop keys in an effort to keep them
4017 * relatively few. */
4018 drop_key_clear(backer);
4019
acf60855
JP
4020 /* Update stats for each flow in the backer. */
4021 update_stats(backer);
abe529af 4022
04d08d54
EJ
4023 n_subfacets = hmap_count(&backer->subfacets);
4024 if (n_subfacets) {
9fc0165a 4025 struct subfacet *subfacet;
04d08d54 4026 long long int total, now;
abe529af 4027
04d08d54
EJ
4028 total = 0;
4029 now = time_msec();
4030 HMAP_FOR_EACH (subfacet, hmap_node, &backer->subfacets) {
4031 total += now - subfacet->created;
acf60855 4032 }
04d08d54
EJ
4033 backer->avg_subfacet_life += total / n_subfacets;
4034 }
4035 backer->avg_subfacet_life /= 2;
0697b5c3 4036
04d08d54
EJ
4037 backer->avg_n_subfacet += n_subfacets;
4038 backer->avg_n_subfacet /= 2;
4039
4040 backer->max_n_subfacet = MAX(backer->max_n_subfacet, n_subfacets);
655ab909 4041
04d08d54
EJ
4042 max_idle = subfacet_max_idle(backer);
4043 expire_subfacets(backer, max_idle);
acf60855 4044
04d08d54
EJ
4045 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4046 struct rule *rule, *next_rule;
4047
4048 if (ofproto->backer != backer) {
4049 continue;
4050 }
acf60855
JP
4051
4052 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
4053 * has passed. */
83b16b98 4054 ovs_mutex_lock(&ofproto->up.expirable_mutex);
e503cc19
SH
4055 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
4056 &ofproto->up.expirable) {
4057 rule_expire(rule_dpif_cast(rule));
0697b5c3 4058 }
83b16b98 4059 ovs_mutex_unlock(&ofproto->up.expirable_mutex);
abe529af 4060
acf60855
JP
4061 /* All outstanding data in existing flows has been accounted, so it's a
4062 * good time to do bond rebalancing. */
4063 if (ofproto->has_bonded_bundles) {
4064 struct ofbundle *bundle;
abe529af 4065
acf60855
JP
4066 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4067 if (bundle->bond) {
4a1b8f30 4068 bond_rebalance(bundle->bond);
acf60855 4069 }
abe529af
BP
4070 }
4071 }
4072 }
4073
acf60855 4074 return MIN(max_idle, 1000);
abe529af
BP
4075}
4076
a218c879
BP
4077/* Updates flow table statistics given that the datapath just reported 'stats'
4078 * as 'subfacet''s statistics. */
4079static void
4080update_subfacet_stats(struct subfacet *subfacet,
4081 const struct dpif_flow_stats *stats)
4082{
4083 struct facet *facet = subfacet->facet;
9dfb1f78
EJ
4084 struct dpif_flow_stats diff;
4085
4086 diff.tcp_flags = stats->tcp_flags;
4087 diff.used = stats->used;
a218c879
BP
4088
4089 if (stats->n_packets >= subfacet->dp_packet_count) {
9dfb1f78 4090 diff.n_packets = stats->n_packets - subfacet->dp_packet_count;
a218c879
BP
4091 } else {
4092 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
9dfb1f78 4093 diff.n_packets = 0;
a218c879
BP
4094 }
4095
4096 if (stats->n_bytes >= subfacet->dp_byte_count) {
9dfb1f78 4097 diff.n_bytes = stats->n_bytes - subfacet->dp_byte_count;
a218c879
BP
4098 } else {
4099 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
9dfb1f78 4100 diff.n_bytes = 0;
a218c879
BP
4101 }
4102
b7e2f6b3 4103 facet->ofproto->n_hit += diff.n_packets;
a218c879
BP
4104 subfacet->dp_packet_count = stats->n_packets;
4105 subfacet->dp_byte_count = stats->n_bytes;
9dfb1f78 4106 subfacet_update_stats(subfacet, &diff);
a218c879 4107
a218c879
BP
4108 if (facet->accounted_bytes < facet->byte_count) {
4109 facet_learn(facet);
4110 facet_account(facet);
4111 facet->accounted_bytes = facet->byte_count;
4112 }
a218c879
BP
4113}
4114
4115/* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
4116 * about, or a flow that shouldn't be installed but was anyway. Delete it. */
4117static void
04d08d54 4118delete_unexpected_flow(struct dpif_backer *backer,
a218c879
BP
4119 const struct nlattr *key, size_t key_len)
4120{
4121 if (!VLOG_DROP_WARN(&rl)) {
4122 struct ds s;
4123
4124 ds_init(&s);
4125 odp_flow_key_format(key, key_len, &s);
04d08d54 4126 VLOG_WARN("unexpected flow: %s", ds_cstr(&s));
a218c879
BP
4127 ds_destroy(&s);
4128 }
4129
4130 COVERAGE_INC(facet_unexpected);
04d08d54 4131 dpif_flow_del(backer->dpif, key, key_len, NULL);
a218c879
BP
4132}
4133
abe529af
BP
4134/* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
4135 *
4136 * This function also pushes statistics updates to rules which each facet
4137 * resubmits into. Generally these statistics will be accurate. However, if a
4138 * facet changes the rule it resubmits into at some time in between
4139 * update_stats() runs, it is possible that statistics accrued to the
4140 * old rule will be incorrectly attributed to the new rule. This could be
4141 * avoided by calling update_stats() whenever rules are created or
4142 * deleted. However, the performance impact of making so many calls to the
4143 * datapath do not justify the benefit of having perfectly accurate statistics.
735d7efb
AZ
4144 *
4145 * In addition, this function maintains per ofproto flow hit counts. The patch
4146 * port is not treated specially. e.g. A packet ingress from br0 patched into
4147 * br1 will increase the hit count of br0 by 1, however, does not affect
4148 * the hit or miss counts of br1.
abe529af
BP
4149 */
4150static void
acf60855 4151update_stats(struct dpif_backer *backer)
abe529af
BP
4152{
4153 const struct dpif_flow_stats *stats;
4154 struct dpif_flow_dump dump;
61fb711d
JP
4155 const struct nlattr *key, *mask;
4156 size_t key_len, mask_len;
abe529af 4157
acf60855 4158 dpif_flow_dump_start(&dump, backer->dpif);
e6cc0bab 4159 while (dpif_flow_dump_next(&dump, &key, &key_len,
61fb711d 4160 &mask, &mask_len, NULL, NULL, &stats)) {
b0f7b9b5 4161 struct subfacet *subfacet;
acf60855 4162 uint32_t key_hash;
abe529af 4163
acf60855 4164 key_hash = odp_flow_key_hash(key, key_len);
04d08d54 4165 subfacet = subfacet_find(backer, key, key_len, key_hash);
6a7e895f
BP
4166 switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
4167 case SF_FAST_PATH:
a218c879 4168 update_subfacet_stats(subfacet, stats);
6a7e895f
BP
4169 break;
4170
4171 case SF_SLOW_PATH:
4172 /* Stats are updated per-packet. */
4173 break;
4174
4175 case SF_NOT_INSTALLED:
4176 default:
04d08d54 4177 delete_unexpected_flow(backer, key, key_len);
6a7e895f 4178 break;
abe529af 4179 }
8fa4d1d0 4180 run_fast_rl();
abe529af
BP
4181 }
4182 dpif_flow_dump_done(&dump);
3af56aef 4183
dc54ef36 4184 update_moving_averages(backer);
abe529af
BP
4185}
4186
4187/* Calculates and returns the number of milliseconds of idle time after which
b0f7b9b5
BP
4188 * subfacets should expire from the datapath. When a subfacet expires, we fold
4189 * its statistics into its facet, and when a facet's last subfacet expires, we
4190 * fold its statistic into its rule. */
abe529af 4191static int
04d08d54 4192subfacet_max_idle(const struct dpif_backer *backer)
abe529af
BP
4193{
4194 /*
4195 * Idle time histogram.
4196 *
b0f7b9b5
BP
4197 * Most of the time a switch has a relatively small number of subfacets.
4198 * When this is the case we might as well keep statistics for all of them
4199 * in userspace and to cache them in the kernel datapath for performance as
abe529af
BP
4200 * well.
4201 *
b0f7b9b5 4202 * As the number of subfacets increases, the memory required to maintain
abe529af 4203 * statistics about them in userspace and in the kernel becomes
b0f7b9b5
BP
4204 * significant. However, with a large number of subfacets it is likely
4205 * that only a few of them are "heavy hitters" that consume a large amount
4206 * of bandwidth. At this point, only heavy hitters are worth caching in
4207 * the kernel and maintaining in userspaces; other subfacets we can
4208 * discard.
abe529af
BP
4209 *
4210 * The technique used to compute the idle time is to build a histogram with
b0f7b9b5 4211 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
abe529af
BP
4212 * that is installed in the kernel gets dropped in the appropriate bucket.
4213 * After the histogram has been built, we compute the cutoff so that only
b0f7b9b5 4214 * the most-recently-used 1% of subfacets (but at least
380f49c4 4215 * flow_eviction_threshold flows) are kept cached. At least
b0f7b9b5
BP
4216 * the most-recently-used bucket of subfacets is kept, so actually an
4217 * arbitrary number of subfacets can be kept in any given expiration run
084f5290
SH
4218 * (though the next run will delete most of those unless they receive
4219 * additional data).
abe529af 4220 *
b0f7b9b5
BP
4221 * This requires a second pass through the subfacets, in addition to the
4222 * pass made by update_stats(), because the former function never looks at
4223 * uninstallable subfacets.
abe529af
BP
4224 */
4225 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
4226 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
4227 int buckets[N_BUCKETS] = { 0 };
f11c1ef4 4228 int total, subtotal, bucket;
b0f7b9b5 4229 struct subfacet *subfacet;
abe529af
BP
4230 long long int now;
4231 int i;
4232
04d08d54 4233 total = hmap_count(&backer->subfacets);
380f49c4 4234 if (total <= flow_eviction_threshold) {
abe529af
BP
4235 return N_BUCKETS * BUCKET_WIDTH;
4236 }
4237
4238 /* Build histogram. */
4239 now = time_msec();
04d08d54 4240 HMAP_FOR_EACH (subfacet, hmap_node, &backer->subfacets) {
b0f7b9b5 4241 long long int idle = now - subfacet->used;
abe529af
BP
4242 int bucket = (idle <= 0 ? 0
4243 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4244 : (unsigned int) idle / BUCKET_WIDTH);
4245 buckets[bucket]++;
4246 }
4247
4248 /* Find the first bucket whose flows should be expired. */
f11c1ef4
SH
4249 subtotal = bucket = 0;
4250 do {
4251 subtotal += buckets[bucket++];
084f5290 4252 } while (bucket < N_BUCKETS &&
380f49c4 4253 subtotal < MAX(flow_eviction_threshold, total / 100));
abe529af
BP
4254
4255 if (VLOG_IS_DBG_ENABLED()) {
4256 struct ds s;
4257
4258 ds_init(&s);
4259 ds_put_cstr(&s, "keep");
4260 for (i = 0; i < N_BUCKETS; i++) {
4261 if (i == bucket) {
4262 ds_put_cstr(&s, ", drop");
4263 }
4264 if (buckets[i]) {
4265 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4266 }
4267 }
04d08d54 4268 VLOG_INFO("%s (msec:count)", ds_cstr(&s));
abe529af
BP
4269 ds_destroy(&s);
4270 }
4271
4272 return bucket * BUCKET_WIDTH;
4273}
4274
abe529af 4275static void
04d08d54 4276expire_subfacets(struct dpif_backer *backer, int dp_max_idle)
abe529af 4277{
625b0720
BP
4278 /* Cutoff time for most flows. */
4279 long long int normal_cutoff = time_msec() - dp_max_idle;
4280
4281 /* We really want to keep flows for special protocols around, so use a more
4282 * conservative cutoff. */
4283 long long int special_cutoff = time_msec() - 10000;
b99d3cee 4284
b0f7b9b5 4285 struct subfacet *subfacet, *next_subfacet;
1d85f9e5 4286 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
b99d3cee 4287 int n_batch;
abe529af 4288
b99d3cee 4289 n_batch = 0;
b0f7b9b5 4290 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
04d08d54 4291 &backer->subfacets) {
625b0720
BP
4292 long long int cutoff;
4293
bbafd73b
EJ
4294 cutoff = (subfacet->facet->xout.slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP
4295 | SLOW_STP)
625b0720
BP
4296 ? special_cutoff
4297 : normal_cutoff);
b0f7b9b5 4298 if (subfacet->used < cutoff) {
6a7e895f 4299 if (subfacet->path != SF_NOT_INSTALLED) {
b99d3cee 4300 batch[n_batch++] = subfacet;
1d85f9e5 4301 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
04d08d54 4302 subfacet_destroy_batch(backer, batch, n_batch);
b99d3cee
BP
4303 n_batch = 0;
4304 }
4305 } else {
4306 subfacet_destroy(subfacet);
4307 }
abe529af
BP
4308 }
4309 }
b99d3cee
BP
4310
4311 if (n_batch > 0) {
04d08d54 4312 subfacet_destroy_batch(backer, batch, n_batch);
b99d3cee 4313 }
abe529af
BP
4314}
4315
4316/* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4317 * then delete it entirely. */
4318static void
4319rule_expire(struct rule_dpif *rule)
4320{
a3779dbc 4321 uint16_t idle_timeout, hard_timeout;
abe529af
BP
4322 long long int now;
4323 uint8_t reason;
4324
e2a3d183
BP
4325 if (rule->up.pending) {
4326 /* We'll have to expire it later. */
4327 return;
4328 }
4329
a3779dbc
EJ
4330 ovs_mutex_lock(&rule->up.timeout_mutex);
4331 hard_timeout = rule->up.hard_timeout;
4332 idle_timeout = rule->up.idle_timeout;
4333 ovs_mutex_unlock(&rule->up.timeout_mutex);
4334
abe529af
BP
4335 /* Has 'rule' expired? */
4336 now = time_msec();
a3779dbc 4337 if (hard_timeout && now > rule->up.modified + hard_timeout * 1000) {
abe529af 4338 reason = OFPRR_HARD_TIMEOUT;
a3779dbc 4339 } else if (idle_timeout && now > rule->up.used + idle_timeout * 1000) {
abe529af
BP
4340 reason = OFPRR_IDLE_TIMEOUT;
4341 } else {
4342 return;
4343 }
4344
4345 COVERAGE_INC(ofproto_dpif_expired);
4346
abe529af
BP
4347 /* Get rid of the rule. */
4348 ofproto_rule_expire(&rule->up, reason);
4349}
4350\f
4351/* Facets. */
4352
4dff9097 4353/* Creates and returns a new facet based on 'miss'.
abe529af
BP
4354 *
4355 * The caller must already have determined that no facet with an identical
4dff9097 4356 * 'miss->flow' exists in 'miss->ofproto'.
f3827897 4357 *
bcd2633a
JP
4358 * 'rule' and 'xout' must have been created based on 'miss'.
4359 *
4360 * 'facet'' statistics are initialized based on 'stats'.
2b459b83 4361 *
b0f7b9b5
BP
4362 * The facet will initially have no subfacets. The caller should create (at
4363 * least) one subfacet with subfacet_create(). */
abe529af 4364static struct facet *
bcd2633a
JP
4365facet_create(const struct flow_miss *miss, struct rule_dpif *rule,
4366 struct xlate_out *xout, struct dpif_flow_stats *stats)
abe529af 4367{
4dff9097 4368 struct ofproto_dpif *ofproto = miss->ofproto;
abe529af 4369 struct facet *facet;
bcd2633a 4370 struct match match;
abe529af
BP
4371
4372 facet = xzalloc(sizeof *facet);
b7e2f6b3 4373 facet->ofproto = miss->ofproto;
bcd2633a
JP
4374 facet->packet_count = facet->prev_packet_count = stats->n_packets;
4375 facet->byte_count = facet->prev_byte_count = stats->n_bytes;
4376 facet->tcp_flags = stats->tcp_flags;
4377 facet->used = stats->used;
4dff9097 4378 facet->flow = miss->flow;
4dff9097
EJ
4379 facet->learn_rl = time_msec() + 500;
4380
b0f7b9b5 4381 list_init(&facet->subfacets);
abe529af
BP
4382 netflow_flow_init(&facet->nf_flow);
4383 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
4384
bcd2633a
JP
4385 xlate_out_copy(&facet->xout, xout);
4386
4387 match_init(&match, &facet->flow, &facet->xout.wc);
4388 cls_rule_init(&facet->cr, &match, OFP_DEFAULT_PRIORITY);
0b4f2078 4389 ovs_rwlock_wrlock(&ofproto->facets.rwlock);
bcd2633a 4390 classifier_insert(&ofproto->facets, &facet->cr);
0b4f2078 4391 ovs_rwlock_unlock(&ofproto->facets.rwlock);
bcd2633a 4392
bbafd73b 4393 facet->nf_flow.output_iface = facet->xout.nf_output_iface;
b7e2f6b3 4394 facet->fail_open = rule->up.cr.priority == FAIL_OPEN_PRIORITY;
6cf474d7 4395
abe529af
BP
4396 return facet;
4397}
4398
4399static void
4400facet_free(struct facet *facet)
4401{
4dff9097 4402 if (facet) {
bbafd73b 4403 xlate_out_uninit(&facet->xout);
4dff9097
EJ
4404 free(facet);
4405 }
abe529af
BP
4406}
4407
3d9e05f8 4408/* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
0a740f48 4409 * 'packet', which arrived on 'in_port'. */
3d9e05f8
BP
4410static bool
4411execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
4412 const struct nlattr *odp_actions, size_t actions_len,
4413 struct ofpbuf *packet)
4414{
4415 struct odputil_keybuf keybuf;
4416 struct ofpbuf key;
4417 int error;
4418
6ff686f2 4419 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
e1b1d06a 4420 odp_flow_key_from_flow(&key, flow,
4e022ec0 4421 ofp_port_to_odp_port(ofproto, flow->in_port.ofp_port));
80e5eed9 4422
acf60855 4423 error = dpif_execute(ofproto->backer->dpif, key.data, key.size,
6ff686f2 4424 odp_actions, actions_len, packet);
6ff686f2 4425 return !error;
abe529af
BP
4426}
4427
bcd2633a 4428/* Remove 'facet' from its ofproto and free up the associated memory:
abe529af
BP
4429 *
4430 * - If 'facet' was installed in the datapath, uninstalls it and updates its
b0f7b9b5 4431 * rule's statistics, via subfacet_uninstall().
abe529af
BP
4432 *
4433 * - Removes 'facet' from its rule and from ofproto->facets.
4434 */
4435static void
15baa734 4436facet_remove(struct facet *facet)
abe529af 4437{
b0f7b9b5
BP
4438 struct subfacet *subfacet, *next_subfacet;
4439
cb22974d 4440 ovs_assert(!list_is_empty(&facet->subfacets));
551a2f6c
BP
4441
4442 /* First uninstall all of the subfacets to get final statistics. */
4443 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
15baa734 4444 subfacet_uninstall(subfacet);
551a2f6c
BP
4445 }
4446
4447 /* Flush the final stats to the rule.
4448 *
4449 * This might require us to have at least one subfacet around so that we
4450 * can use its actions for accounting in facet_account(), which is why we
4451 * have uninstalled but not yet destroyed the subfacets. */
15baa734 4452 facet_flush_stats(facet);
551a2f6c
BP
4453
4454 /* Now we're really all done so destroy everything. */
b0f7b9b5
BP
4455 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
4456 &facet->subfacets) {
15baa734 4457 subfacet_destroy__(subfacet);
b0f7b9b5 4458 }
0b4f2078 4459 ovs_rwlock_wrlock(&facet->ofproto->facets.rwlock);
b7e2f6b3 4460 classifier_remove(&facet->ofproto->facets, &facet->cr);
0b4f2078 4461 ovs_rwlock_unlock(&facet->ofproto->facets.rwlock);
bcd2633a 4462 cls_rule_destroy(&facet->cr);
abe529af
BP
4463 facet_free(facet);
4464}
4465
3de9590b
BP
4466/* Feed information from 'facet' back into the learning table to keep it in
4467 * sync with what is actually flowing through the datapath. */
abe529af 4468static void
3de9590b 4469facet_learn(struct facet *facet)
abe529af 4470{
62bd7349 4471 long long int now = time_msec();
abe529af 4472
bbafd73b 4473 if (!facet->xout.has_fin_timeout && now < facet->learn_rl) {
6cf474d7
EJ
4474 return;
4475 }
4476
62bd7349 4477 facet->learn_rl = now + 500;
6cf474d7 4478
bbafd73b
EJ
4479 if (!facet->xout.has_learn
4480 && !facet->xout.has_normal
4481 && (!facet->xout.has_fin_timeout
3de9590b 4482 || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
abe529af
BP
4483 return;
4484 }
abe529af 4485
9dfb1f78 4486 facet_push_stats(facet, true);
3de9590b
BP
4487}
4488
4489static void
4490facet_account(struct facet *facet)
4491{
3de9590b
BP
4492 const struct nlattr *a;
4493 unsigned int left;
4494 ovs_be16 vlan_tci;
4495 uint64_t n_bytes;
abe529af 4496
b7e2f6b3 4497 if (!facet->xout.has_normal || !facet->ofproto->has_bonded_bundles) {
abe529af
BP
4498 return;
4499 }
3de9590b 4500 n_bytes = facet->byte_count - facet->accounted_bytes;
d78be13b
BP
4501
4502 /* This loop feeds byte counters to bond_account() for rebalancing to use
4503 * as a basis. We also need to track the actual VLAN on which the packet
4504 * is going to be sent to ensure that it matches the one passed to
4505 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
b95fc6ba
BP
4506 * hash bucket.)
4507 *
4508 * We use the actions from an arbitrary subfacet because they should all
4509 * be equally valid for our purpose. */
d78be13b 4510 vlan_tci = facet->flow.vlan_tci;
bbafd73b
EJ
4511 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->xout.odp_actions.data,
4512 facet->xout.odp_actions.size) {
fea393b1 4513 const struct ovs_action_push_vlan *vlan;
d78be13b 4514 struct ofport_dpif *port;
abe529af 4515
d78be13b 4516 switch (nl_attr_type(a)) {
df2c07f4 4517 case OVS_ACTION_ATTR_OUTPUT:
b7e2f6b3 4518 port = get_odp_port(facet->ofproto, nl_attr_get_odp_port(a));
abe529af 4519 if (port && port->bundle && port->bundle->bond) {
d78be13b 4520 bond_account(port->bundle->bond, &facet->flow,
dc155bff 4521 vlan_tci_to_vid(vlan_tci), n_bytes);
abe529af 4522 }
d78be13b
BP
4523 break;
4524
fea393b1
BP
4525 case OVS_ACTION_ATTR_POP_VLAN:
4526 vlan_tci = htons(0);
d78be13b
BP
4527 break;
4528
fea393b1
BP
4529 case OVS_ACTION_ATTR_PUSH_VLAN:
4530 vlan = nl_attr_get(a);
4531 vlan_tci = vlan->vlan_tci;
d78be13b 4532 break;
abe529af
BP
4533 }
4534 }
4535}
4536
abe529af
BP
4537/* Returns true if the only action for 'facet' is to send to the controller.
4538 * (We don't report NetFlow expiration messages for such facets because they
4539 * are just part of the control logic for the network, not real traffic). */
4540static bool
4541facet_is_controller_flow(struct facet *facet)
4542{
f25d0cf3 4543 if (facet) {
b7e2f6b3
EJ
4544 struct ofproto_dpif *ofproto = facet->ofproto;
4545 const struct rule_dpif *rule = rule_dpif_lookup(ofproto, &facet->flow,
4546 NULL);
4547 const struct ofpact *ofpacts = rule->up.ofpacts;
4548 size_t ofpacts_len = rule->up.ofpacts_len;
f25d0cf3 4549
dd30ff28
BP
4550 if (ofpacts_len > 0 &&
4551 ofpacts->type == OFPACT_CONTROLLER &&
f25d0cf3
BP
4552 ofpact_next(ofpacts) >= ofpact_end(ofpacts, ofpacts_len)) {
4553 return true;
4554 }
4555 }
4556 return false;
abe529af
BP
4557}
4558
4559/* Folds all of 'facet''s statistics into its rule. Also updates the
4560 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
4561 * 'facet''s statistics in the datapath should have been zeroed and folded into
4562 * its packet and byte counts before this function is called. */
4563static void
15baa734 4564facet_flush_stats(struct facet *facet)
abe529af 4565{
b7e2f6b3 4566 struct ofproto_dpif *ofproto = facet->ofproto;
b0f7b9b5
BP
4567 struct subfacet *subfacet;
4568
4569 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
cb22974d
BP
4570 ovs_assert(!subfacet->dp_byte_count);
4571 ovs_assert(!subfacet->dp_packet_count);
b0f7b9b5 4572 }
abe529af 4573
9dfb1f78 4574 facet_push_stats(facet, false);
3de9590b
BP
4575 if (facet->accounted_bytes < facet->byte_count) {
4576 facet_account(facet);
4577 facet->accounted_bytes = facet->byte_count;
4578 }
abe529af
BP
4579
4580 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
4581 struct ofexpired expired;
4582 expired.flow = facet->flow;
4583 expired.packet_count = facet->packet_count;
4584 expired.byte_count = facet->byte_count;
4585 expired.used = facet->used;
4586 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4587 }
4588
abe529af
BP
4589 /* Reset counters to prevent double counting if 'facet' ever gets
4590 * reinstalled. */
bbb5d219 4591 facet_reset_counters(facet);
abe529af
BP
4592
4593 netflow_flow_clear(&facet->nf_flow);
0e553d9c 4594 facet->tcp_flags = 0;
abe529af
BP
4595}
4596
bcd2633a
JP
4597/* Searches 'ofproto''s table of facets for one which would be responsible for
4598 * 'flow'. Returns it if found, otherwise a null pointer.
2b459b83 4599 *
abe529af
BP
4600 * The returned facet might need revalidation; use facet_lookup_valid()
4601 * instead if that is important. */
4602static struct facet *
bcd2633a 4603facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
abe529af 4604{
0b4f2078
EJ
4605 struct cls_rule *cr;
4606
4607 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
4608 cr = classifier_lookup(&ofproto->facets, flow, NULL);
4609 ovs_rwlock_unlock(&ofproto->facets.rwlock);
bcd2633a 4610 return cr ? CONTAINER_OF(cr, struct facet, cr) : NULL;
abe529af
BP
4611}
4612
bcd2633a
JP
4613/* Searches 'ofproto''s table of facets for one capable that covers
4614 * 'flow'. Returns it if found, otherwise a null pointer.
2b459b83 4615 *
abe529af
BP
4616 * The returned facet is guaranteed to be valid. */
4617static struct facet *
bcd2633a 4618facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
abe529af 4619{
c57b2226 4620 struct facet *facet;
abe529af 4621
bcd2633a 4622 facet = facet_find(ofproto, flow);
abe529af 4623 if (facet
a1616063 4624 && ofproto->backer->need_revalidate
5bf64ade 4625 && !facet_revalidate(facet)) {
0305ce1f 4626 return NULL;
abe529af
BP
4627 }
4628
4629 return facet;
4630}
4631
6814e51f
BP
4632static bool
4633facet_check_consistency(struct facet *facet)
4634{
4635 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
4636
bbafd73b
EJ
4637 struct xlate_out xout;
4638 struct xlate_in xin;
050ac423 4639
6814e51f 4640 struct rule_dpif *rule;
b7e2f6b3 4641 bool ok, fail_open;
6814e51f
BP
4642
4643 /* Check the datapath actions for consistency. */
b7e2f6b3
EJ
4644 rule = rule_dpif_lookup(facet->ofproto, &facet->flow, NULL);
4645 xlate_in_init(&xin, facet->ofproto, &facet->flow, rule, 0, NULL);
bbafd73b 4646 xlate_actions(&xin, &xout);
6814e51f 4647
b7e2f6b3 4648 fail_open = rule->up.cr.priority == FAIL_OPEN_PRIORITY;
bbafd73b 4649 ok = ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)
b7e2f6b3
EJ
4650 && facet->xout.slow == xout.slow
4651 && facet->fail_open == fail_open;
4dff9097
EJ
4652 if (!ok && !VLOG_DROP_WARN(&rl)) {
4653 struct ds s = DS_EMPTY_INITIALIZER;
c53e1132 4654
4dff9097
EJ
4655 flow_format(&s, &facet->flow);
4656 ds_put_cstr(&s, ": inconsistency in facet");
c53e1132 4657
bbafd73b 4658 if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
9616614b 4659 ds_put_cstr(&s, " (actions were: ");
bbafd73b
EJ
4660 format_odp_actions(&s, facet->xout.odp_actions.data,
4661 facet->xout.odp_actions.size);
9616614b 4662 ds_put_cstr(&s, ") (correct actions: ");
bbafd73b
EJ
4663 format_odp_actions(&s, xout.odp_actions.data,
4664 xout.odp_actions.size);
c0a71f4e 4665 ds_put_char(&s, ')');
6814e51f 4666 }
4dff9097 4667
bbafd73b
EJ
4668 if (facet->xout.slow != xout.slow) {
4669 ds_put_format(&s, " slow path incorrect. should be %d", xout.slow);
4dff9097
EJ
4670 }
4671
b7e2f6b3
EJ
4672 if (facet->fail_open != fail_open) {
4673 ds_put_format(&s, " fail open incorrect. should be %s",
4674 fail_open ? "true" : "false");
4675 }
9616614b 4676 ds_destroy(&s);
6814e51f 4677 }
bbafd73b 4678 xlate_out_uninit(&xout);
6814e51f
BP
4679
4680 return ok;
4681}
4682
15baa734 4683/* Re-searches the classifier for 'facet':
abe529af
BP
4684 *
4685 * - If the rule found is different from 'facet''s current rule, moves
4686 * 'facet' to the new rule and recompiles its actions.
4687 *
4688 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
f231418e
EJ
4689 * where it is and recompiles its actions anyway.
4690 *
4691 * - If any of 'facet''s subfacets correspond to a new flow according to
8449c4d6 4692 * xlate_receive(), 'facet' is removed.
5bf64ade
EJ
4693 *
4694 * Returns true if 'facet' is still valid. False if 'facet' was removed. */
4695static bool
15baa734 4696facet_revalidate(struct facet *facet)
abe529af 4697{
b7e2f6b3 4698 struct ofproto_dpif *ofproto = facet->ofproto;
abe529af 4699 struct rule_dpif *new_rule;
b0f7b9b5 4700 struct subfacet *subfacet;
bcd2633a 4701 struct flow_wildcards wc;
bbafd73b
EJ
4702 struct xlate_out xout;
4703 struct xlate_in xin;
abe529af
BP
4704
4705 COVERAGE_INC(facet_revalidate);
4706
f231418e
EJ
4707 /* Check that child subfacets still correspond to this facet. Tunnel
4708 * configuration changes could cause a subfacet's OpenFlow in_port to
4709 * change. */
4710 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4711 struct ofproto_dpif *recv_ofproto;
4712 struct flow recv_flow;
4713 int error;
4714
8449c4d6
EJ
4715 error = xlate_receive(ofproto->backer, NULL, subfacet->key,
4716 subfacet->key_len, &recv_flow, NULL,
4717 &recv_ofproto, NULL);
f231418e
EJ
4718 if (error
4719 || recv_ofproto != ofproto
bcd2633a 4720 || facet != facet_find(ofproto, &recv_flow)) {
f231418e 4721 facet_remove(facet);
5bf64ade 4722 return false;
f231418e
EJ
4723 }
4724 }
4725
bcd2633a
JP
4726 flow_wildcards_init_catchall(&wc);
4727 new_rule = rule_dpif_lookup(ofproto, &facet->flow, &wc);
abe529af 4728
df2c07f4 4729 /* Calculate new datapath actions.
abe529af
BP
4730 *
4731 * We do not modify any 'facet' state yet, because we might need to, e.g.,
4732 * emit a NetFlow expiration and, if so, we need to have the old state
4733 * around to properly compose it. */
47b0deae 4734 xlate_in_init(&xin, ofproto, &facet->flow, new_rule, 0, NULL);
bbafd73b 4735 xlate_actions(&xin, &xout);
bcd2633a 4736 flow_wildcards_or(&xout.wc, &xout.wc, &wc);
4dff9097
EJ
4737
4738 /* A facet's slow path reason should only change under dramatic
4739 * circumstances. Rather than try to update everything, it's simpler to
bcd2633a
JP
4740 * remove the facet and start over.
4741 *
4742 * More importantly, if a facet's wildcards change, it will be relatively
4743 * difficult to figure out if its subfacets still belong to it, and if not
4744 * which facet they may belong to. Again, to avoid the complexity, we
4745 * simply give up instead. */
4746 if (facet->xout.slow != xout.slow
4747 || memcmp(&facet->xout.wc, &xout.wc, sizeof xout.wc)) {
4dff9097 4748 facet_remove(facet);
bbafd73b 4749 xlate_out_uninit(&xout);
4dff9097
EJ
4750 return false;
4751 }
6a7e895f 4752
bbafd73b 4753 if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
4dff9097
EJ
4754 LIST_FOR_EACH(subfacet, list_node, &facet->subfacets) {
4755 if (subfacet->path == SF_FAST_PATH) {
4756 struct dpif_flow_stats stats;
b95fc6ba 4757
bbafd73b 4758 subfacet_install(subfacet, &xout.odp_actions, &stats);
4dff9097 4759 subfacet_update_stats(subfacet, &stats);
b95fc6ba 4760 }
abe529af 4761 }
b95fc6ba 4762
15baa734 4763 facet_flush_stats(facet);
4dff9097 4764
bbafd73b
EJ
4765 ofpbuf_clear(&facet->xout.odp_actions);
4766 ofpbuf_put(&facet->xout.odp_actions, xout.odp_actions.data,
4767 xout.odp_actions.size);
abe529af
BP
4768 }
4769
4770 /* Update 'facet' now that we've taken care of all the old state. */
bbafd73b
EJ
4771 facet->xout.slow = xout.slow;
4772 facet->xout.has_learn = xout.has_learn;
4773 facet->xout.has_normal = xout.has_normal;
4774 facet->xout.has_fin_timeout = xout.has_fin_timeout;
4775 facet->xout.nf_output_iface = xout.nf_output_iface;
4776 facet->xout.mirrors = xout.mirrors;
4777 facet->nf_flow.output_iface = facet->xout.nf_output_iface;
b7e2f6b3
EJ
4778 facet->used = MAX(facet->used, new_rule->up.created);
4779 facet->fail_open = new_rule->up.cr.priority == FAIL_OPEN_PRIORITY;
5bf64ade 4780
bbafd73b 4781 xlate_out_uninit(&xout);
5bf64ade 4782 return true;
abe529af
BP
4783}
4784
bbb5d219
EJ
4785static void
4786facet_reset_counters(struct facet *facet)
4787{
4788 facet->packet_count = 0;
4789 facet->byte_count = 0;
9d24de3b
JP
4790 facet->prev_packet_count = 0;
4791 facet->prev_byte_count = 0;
bbb5d219
EJ
4792 facet->accounted_bytes = 0;
4793}
4794
abe529af 4795static void
9dfb1f78 4796facet_push_stats(struct facet *facet, bool may_learn)
abe529af 4797{
112bc5f4 4798 struct dpif_flow_stats stats;
abe529af 4799
cb22974d
BP
4800 ovs_assert(facet->packet_count >= facet->prev_packet_count);
4801 ovs_assert(facet->byte_count >= facet->prev_byte_count);
4802 ovs_assert(facet->used >= facet->prev_used);
abe529af 4803
112bc5f4
BP
4804 stats.n_packets = facet->packet_count - facet->prev_packet_count;
4805 stats.n_bytes = facet->byte_count - facet->prev_byte_count;
4806 stats.used = facet->used;
9dfb1f78
EJ
4807 stats.tcp_flags = facet->tcp_flags;
4808
4809 if (may_learn || stats.n_packets || facet->used > facet->prev_used) {
b7e2f6b3 4810 struct ofproto_dpif *ofproto = facet->ofproto;
9dfb1f78 4811 struct ofport_dpif *in_port;
b7e2f6b3 4812 struct rule_dpif *rule;
9dfb1f78 4813 struct xlate_in xin;
abe529af 4814
9d24de3b
JP
4815 facet->prev_packet_count = facet->packet_count;
4816 facet->prev_byte_count = facet->byte_count;
4817 facet->prev_used = facet->used;
abe529af 4818
4e022ec0 4819 in_port = get_ofp_port(ofproto, facet->flow.in_port.ofp_port);
42943cde 4820 if (in_port && in_port->is_tunnel) {
9dfb1f78
EJ
4821 netdev_vport_inc_rx(in_port->up.netdev, &stats);
4822 }
9d24de3b 4823
b7e2f6b3
EJ
4824 rule = rule_dpif_lookup(ofproto, &facet->flow, NULL);
4825 rule_credit_stats(rule, &stats);
9dfb1f78
EJ
4826 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow,
4827 facet->used);
4828 netflow_flow_update_flags(&facet->nf_flow, facet->tcp_flags);
ec7ceaed
EJ
4829 mirror_update_stats(ofproto->mbridge, facet->xout.mirrors,
4830 stats.n_packets, stats.n_bytes);
9dfb1f78 4831
b7e2f6b3
EJ
4832 xlate_in_init(&xin, ofproto, &facet->flow, rule, stats.tcp_flags,
4833 NULL);
9dfb1f78
EJ
4834 xin.resubmit_stats = &stats;
4835 xin.may_learn = may_learn;
4836 xlate_actions_for_side_effects(&xin);
abe529af
BP
4837 }
4838}
4839
8844e035 4840static void
4464589e 4841push_all_stats__(bool run_fast)
8844e035
EJ
4842{
4843 static long long int rl = LLONG_MIN;
4844 struct ofproto_dpif *ofproto;
4845
4846 if (time_msec() < rl) {
4847 return;
4848 }
4849
4850 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
bcd2633a 4851 struct cls_cursor cursor;
8844e035
EJ
4852 struct facet *facet;
4853
0b4f2078 4854 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
bcd2633a
JP
4855 cls_cursor_init(&cursor, &ofproto->facets, NULL);
4856 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
9dfb1f78 4857 facet_push_stats(facet, false);
4464589e
EJ
4858 if (run_fast) {
4859 run_fast_rl();
4860 }
8844e035 4861 }
0b4f2078 4862 ovs_rwlock_unlock(&ofproto->facets.rwlock);
8844e035
EJ
4863 }
4864
4865 rl = time_msec() + 100;
4866}
4867
4464589e
EJ
4868static void
4869push_all_stats(void)
4870{
4871 push_all_stats__(true);
4872}
4873
9583bc14 4874void
112bc5f4 4875rule_credit_stats(struct rule_dpif *rule, const struct dpif_flow_stats *stats)
abe529af 4876{
9fbe253e 4877 ovs_mutex_lock(&rule->stats_mutex);
112bc5f4
BP
4878 rule->packet_count += stats->n_packets;
4879 rule->byte_count += stats->n_bytes;
4880 ofproto_rule_update_used(&rule->up, stats->used);
9fbe253e 4881 ovs_mutex_unlock(&rule->stats_mutex);
abe529af 4882}
abe529af 4883\f
b0f7b9b5
BP
4884/* Subfacets. */
4885
4886static struct subfacet *
04d08d54
EJ
4887subfacet_find(struct dpif_backer *backer, const struct nlattr *key,
4888 size_t key_len, uint32_t key_hash)
b0f7b9b5
BP
4889{
4890 struct subfacet *subfacet;
4891
4892 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
04d08d54 4893 &backer->subfacets) {
9566abf9
EJ
4894 if (subfacet->key_len == key_len
4895 && !memcmp(key, subfacet->key, key_len)) {
b0f7b9b5
BP
4896 return subfacet;
4897 }
4898 }
4899
4900 return NULL;
4901}
4902
4903/* Searches 'facet' (within 'ofproto') for a subfacet with the specified
a088a1ff
JP
4904 * 'key_fitness', 'key', and 'key_len' members in 'miss'. Returns the
4905 * existing subfacet if there is one, otherwise creates and returns a
4dff9097 4906 * new subfacet. */
b0f7b9b5 4907static struct subfacet *
a088a1ff
JP
4908subfacet_create(struct facet *facet, struct flow_miss *miss,
4909 long long int now)
b0f7b9b5 4910{
04d08d54 4911 struct dpif_backer *backer = miss->ofproto->backer;
a088a1ff
JP
4912 enum odp_key_fitness key_fitness = miss->key_fitness;
4913 const struct nlattr *key = miss->key;
4914 size_t key_len = miss->key_len;
4915 uint32_t key_hash;
b0f7b9b5
BP
4916 struct subfacet *subfacet;
4917
a088a1ff
JP
4918 key_hash = odp_flow_key_hash(key, key_len);
4919
3b145dd7
BP
4920 if (list_is_empty(&facet->subfacets)) {
4921 subfacet = &facet->one_subfacet;
4922 } else {
04d08d54 4923 subfacet = subfacet_find(backer, key, key_len, key_hash);
3b145dd7
BP
4924 if (subfacet) {
4925 if (subfacet->facet == facet) {
4926 return subfacet;
4927 }
4928
4929 /* This shouldn't happen. */
4930 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
4931 subfacet_destroy(subfacet);
b0f7b9b5
BP
4932 }
4933
3b145dd7 4934 subfacet = xmalloc(sizeof *subfacet);
b0f7b9b5
BP
4935 }
4936
04d08d54 4937 hmap_insert(&backer->subfacets, &subfacet->hmap_node, key_hash);
b0f7b9b5
BP
4938 list_push_back(&facet->subfacets, &subfacet->list_node);
4939 subfacet->facet = facet;
b0f7b9b5 4940 subfacet->key_fitness = key_fitness;
9566abf9
EJ
4941 subfacet->key = xmemdup(key, key_len);
4942 subfacet->key_len = key_len;
459b16a1 4943 subfacet->used = now;
655ab909 4944 subfacet->created = now;
26cd7e34
BP
4945 subfacet->dp_packet_count = 0;
4946 subfacet->dp_byte_count = 0;
6a7e895f 4947 subfacet->path = SF_NOT_INSTALLED;
04d08d54 4948 subfacet->backer = backer;
b0f7b9b5 4949
04d08d54 4950 backer->subfacet_add_count++;
b0f7b9b5
BP
4951 return subfacet;
4952}
4953
b0f7b9b5
BP
4954/* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
4955 * its facet within 'ofproto', and frees it. */
4956static void
15baa734 4957subfacet_destroy__(struct subfacet *subfacet)
b0f7b9b5 4958{
15baa734 4959 struct facet *facet = subfacet->facet;
b7e2f6b3 4960 struct ofproto_dpif *ofproto = facet->ofproto;
15baa734 4961
655ab909 4962 /* Update ofproto stats before uninstall the subfacet. */
dc54ef36 4963 ofproto->backer->subfacet_del_count++;
655ab909 4964
15baa734 4965 subfacet_uninstall(subfacet);
04d08d54 4966 hmap_remove(&subfacet->backer->subfacets, &subfacet->hmap_node);
b0f7b9b5
BP
4967 list_remove(&subfacet->list_node);
4968 free(subfacet->key);
26cd7e34
BP
4969 if (subfacet != &facet->one_subfacet) {
4970 free(subfacet);
4971 }
b0f7b9b5
BP
4972}
4973
4974/* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
4975 * last remaining subfacet in its facet destroys the facet too. */
4976static void
15baa734 4977subfacet_destroy(struct subfacet *subfacet)
b0f7b9b5
BP
4978{
4979 struct facet *facet = subfacet->facet;
4980
551a2f6c
BP
4981 if (list_is_singleton(&facet->subfacets)) {
4982 /* facet_remove() needs at least one subfacet (it will remove it). */
15baa734 4983 facet_remove(facet);
551a2f6c 4984 } else {
15baa734 4985 subfacet_destroy__(subfacet);
b0f7b9b5
BP
4986 }
4987}
4988
1d85f9e5 4989static void
04d08d54 4990subfacet_destroy_batch(struct dpif_backer *backer,
1d85f9e5
JP
4991 struct subfacet **subfacets, int n)
4992{
1d85f9e5
JP
4993 struct dpif_op ops[SUBFACET_DESTROY_MAX_BATCH];
4994 struct dpif_op *opsp[SUBFACET_DESTROY_MAX_BATCH];
1d85f9e5
JP
4995 struct dpif_flow_stats stats[SUBFACET_DESTROY_MAX_BATCH];
4996 int i;
4997
4998 for (i = 0; i < n; i++) {
4999 ops[i].type = DPIF_OP_FLOW_DEL;
9566abf9
EJ
5000 ops[i].u.flow_del.key = subfacets[i]->key;
5001 ops[i].u.flow_del.key_len = subfacets[i]->key_len;
1d85f9e5
JP
5002 ops[i].u.flow_del.stats = &stats[i];
5003 opsp[i] = &ops[i];
5004 }
5005
04d08d54 5006 dpif_operate(backer->dpif, opsp, n);
1d85f9e5
JP
5007 for (i = 0; i < n; i++) {
5008 subfacet_reset_dp_stats(subfacets[i], &stats[i]);
5009 subfacets[i]->path = SF_NOT_INSTALLED;
5010 subfacet_destroy(subfacets[i]);
8fa4d1d0 5011 run_fast_rl();
1d85f9e5
JP
5012 }
5013}
5014
b0f7b9b5
BP
5015/* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
5016 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
5017 * in the datapath will be zeroed and 'stats' will be updated with traffic new
5018 * since 'subfacet' was last updated.
5019 *
5020 * Returns 0 if successful, otherwise a positive errno value. */
5021static int
4dff9097
EJ
5022subfacet_install(struct subfacet *subfacet, const struct ofpbuf *odp_actions,
5023 struct dpif_flow_stats *stats)
b0f7b9b5 5024{
15baa734 5025 struct facet *facet = subfacet->facet;
bbafd73b 5026 enum subfacet_path path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
4dff9097
EJ
5027 const struct nlattr *actions = odp_actions->data;
5028 size_t actions_len = odp_actions->size;
d445cc16
JP
5029 struct odputil_keybuf maskbuf;
5030 struct ofpbuf mask;
4dff9097 5031
6a7e895f 5032 uint64_t slow_path_stub[128 / 8];
b0f7b9b5 5033 enum dpif_flow_put_flags flags;
b0f7b9b5
BP
5034 int ret;
5035
0f74f625
AZ
5036 flags = subfacet->path == SF_NOT_INSTALLED ? DPIF_FP_CREATE
5037 : DPIF_FP_MODIFY;
b0f7b9b5
BP
5038 if (stats) {
5039 flags |= DPIF_FP_ZERO_STATS;
5040 }
5041
6a7e895f 5042 if (path == SF_SLOW_PATH) {
b7e2f6b3 5043 compose_slow_path(facet->ofproto, &facet->flow, facet->xout.slow,
6a7e895f
BP
5044 slow_path_stub, sizeof slow_path_stub,
5045 &actions, &actions_len);
5046 }
5047
d445cc16 5048 ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
6b35e630
JP
5049 if (enable_megaflows) {
5050 odp_flow_key_from_mask(&mask, &facet->xout.wc.masks,
5051 &facet->flow, UINT32_MAX);
5052 }
d445cc16
JP
5053
5054 ret = dpif_flow_put(subfacet->backer->dpif, flags, subfacet->key,
5055 subfacet->key_len, mask.data, mask.size,
e6cc0bab 5056 actions, actions_len, stats);
b0f7b9b5
BP
5057
5058 if (stats) {
5059 subfacet_reset_dp_stats(subfacet, stats);
5060 }
5061
9b1a48c2
JP
5062 if (ret) {
5063 COVERAGE_INC(subfacet_install_fail);
5064 } else {
6a7e895f
BP
5065 subfacet->path = path;
5066 }
b0f7b9b5
BP
5067 return ret;
5068}
5069
5070/* If 'subfacet' is installed in the datapath, uninstalls it. */
5071static void
15baa734 5072subfacet_uninstall(struct subfacet *subfacet)
b0f7b9b5 5073{
6a7e895f 5074 if (subfacet->path != SF_NOT_INSTALLED) {
b7e2f6b3 5075 struct ofproto_dpif *ofproto = subfacet->facet->ofproto;
b0f7b9b5 5076 struct dpif_flow_stats stats;
b0f7b9b5
BP
5077 int error;
5078
9566abf9
EJ
5079 error = dpif_flow_del(ofproto->backer->dpif, subfacet->key,
5080 subfacet->key_len, &stats);
b0f7b9b5
BP
5081 subfacet_reset_dp_stats(subfacet, &stats);
5082 if (!error) {
15baa734 5083 subfacet_update_stats(subfacet, &stats);
b0f7b9b5 5084 }
6a7e895f 5085 subfacet->path = SF_NOT_INSTALLED;
b0f7b9b5 5086 } else {
cb22974d
BP
5087 ovs_assert(subfacet->dp_packet_count == 0);
5088 ovs_assert(subfacet->dp_byte_count == 0);
b0f7b9b5
BP
5089 }
5090}
5091
5092/* Resets 'subfacet''s datapath statistics counters. This should be called
5093 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
5094 * non-null, it should contain the statistics returned by dpif when 'subfacet'
5095 * was reset in the datapath. 'stats' will be modified to include only
5096 * statistics new since 'subfacet' was last updated. */
5097static void
5098subfacet_reset_dp_stats(struct subfacet *subfacet,
5099 struct dpif_flow_stats *stats)
5100{
5101 if (stats
5102 && subfacet->dp_packet_count <= stats->n_packets
5103 && subfacet->dp_byte_count <= stats->n_bytes) {
5104 stats->n_packets -= subfacet->dp_packet_count;
5105 stats->n_bytes -= subfacet->dp_byte_count;
5106 }
5107
5108 subfacet->dp_packet_count = 0;
5109 subfacet->dp_byte_count = 0;
5110}
5111
b0f7b9b5
BP
5112/* Folds the statistics from 'stats' into the counters in 'subfacet'.
5113 *
5114 * Because of the meaning of a subfacet's counters, it only makes sense to do
5115 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
5116 * represents a packet that was sent by hand or if it represents statistics
5117 * that have been cleared out of the datapath. */
5118static void
15baa734 5119subfacet_update_stats(struct subfacet *subfacet,
b0f7b9b5
BP
5120 const struct dpif_flow_stats *stats)
5121{
5122 if (stats->n_packets || stats->used > subfacet->used) {
5123 struct facet *facet = subfacet->facet;
5124
9dfb1f78
EJ
5125 subfacet->used = MAX(subfacet->used, stats->used);
5126 facet->used = MAX(facet->used, stats->used);
b0f7b9b5
BP
5127 facet->packet_count += stats->n_packets;
5128 facet->byte_count += stats->n_bytes;
0e553d9c 5129 facet->tcp_flags |= stats->tcp_flags;
b0f7b9b5
BP
5130 }
5131}
5132\f
abe529af
BP
5133/* Rules. */
5134
bcd2633a
JP
5135/* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
5136 * the fields that were relevant as part of the lookup. */
abe529af 5137static struct rule_dpif *
bcd2633a
JP
5138rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
5139 struct flow_wildcards *wc)
c57b2226 5140{
ec89fc6f 5141 struct ofport_dpif *port;
c57b2226
BP
5142 struct rule_dpif *rule;
5143
9583bc14 5144 rule = rule_dpif_lookup_in_table(ofproto, flow, wc, 0);
c57b2226
BP
5145 if (rule) {
5146 return rule;
5147 }
ec89fc6f
EJ
5148 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
5149 if (!port) {
5150 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
5151 flow->in_port.ofp_port);
5152 }
c57b2226 5153
ec89fc6f
EJ
5154 return choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
5155 ofproto->no_packet_in_rule);
c57b2226
BP
5156}
5157
9583bc14
EJ
5158struct rule_dpif *
5159rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
5160 const struct flow *flow, struct flow_wildcards *wc,
5161 uint8_t table_id)
abe529af 5162{
7257b535
BP
5163 struct cls_rule *cls_rule;
5164 struct classifier *cls;
7fd51d39 5165 bool frag;
7257b535 5166
9cdaaebe
BP
5167 if (table_id >= N_TABLES) {
5168 return NULL;
5169 }
5170
1dd35f8a 5171 if (wc) {
7431e171 5172 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1dd35f8a
JP
5173 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
5174 }
5175
d0918789 5176 cls = &ofproto->up.tables[table_id].cls;
7fd51d39
BP
5177 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
5178 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
5179 /* We must pretend that transport ports are unavailable. */
7257b535
BP
5180 struct flow ofpc_normal_flow = *flow;
5181 ofpc_normal_flow.tp_src = htons(0);
5182 ofpc_normal_flow.tp_dst = htons(0);
0b4f2078 5183 ovs_rwlock_rdlock(&cls->rwlock);
bcd2633a 5184 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
0b4f2078 5185 ovs_rwlock_unlock(&cls->rwlock);
7fd51d39
BP
5186 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
5187 cls_rule = &ofproto->drop_frags_rule->up.cr;
bcd2633a
JP
5188 if (wc) {
5189 flow_wildcards_init_exact(wc);
5190 }
7257b535 5191 } else {
0b4f2078 5192 ovs_rwlock_rdlock(&cls->rwlock);
bcd2633a 5193 cls_rule = classifier_lookup(cls, flow, wc);
0b4f2078 5194 ovs_rwlock_unlock(&cls->rwlock);
7257b535
BP
5195 }
5196 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
abe529af
BP
5197}
5198
ec89fc6f
EJ
5199/* Given a port configuration (specified as zero if there's no port), chooses
5200 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
5201 * flow table miss. */
9583bc14 5202struct rule_dpif *
ec89fc6f
EJ
5203choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
5204 struct rule_dpif *no_packet_in_rule)
c376f9a3 5205{
ec89fc6f 5206 return config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
c376f9a3
IY
5207}
5208
7ee20df1
BP
5209static void
5210complete_operation(struct rule_dpif *rule)
5211{
5212 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5213
a1616063 5214 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
7ee20df1
BP
5215 if (clogged) {
5216 struct dpif_completion *c = xmalloc(sizeof *c);
5217 c->op = rule->up.pending;
5218 list_push_back(&ofproto->completions, &c->list_node);
5219 } else {
5220 ofoperation_complete(rule->up.pending, 0);
5221 }
5222}
5223
abe529af
BP
5224static struct rule *
5225rule_alloc(void)
5226{
5227 struct rule_dpif *rule = xmalloc(sizeof *rule);
5228 return &rule->up;
5229}
5230
5231static void
5232rule_dealloc(struct rule *rule_)
5233{
5234 struct rule_dpif *rule = rule_dpif_cast(rule_);
5235 free(rule);
5236}
5237
90bf1e07 5238static enum ofperr
abe529af
BP
5239rule_construct(struct rule *rule_)
5240{
5241 struct rule_dpif *rule = rule_dpif_cast(rule_);
9fbe253e
EJ
5242 ovs_mutex_init(&rule->stats_mutex, PTHREAD_MUTEX_NORMAL);
5243 ovs_mutex_lock(&rule->stats_mutex);
abe529af
BP
5244 rule->packet_count = 0;
5245 rule->byte_count = 0;
9fbe253e 5246 ovs_mutex_unlock(&rule->stats_mutex);
7ee20df1 5247 complete_operation(rule);
abe529af
BP
5248 return 0;
5249}
5250
5251static void
9fbe253e 5252rule_destruct(struct rule *rule_)
abe529af 5253{
9fbe253e
EJ
5254 struct rule_dpif *rule = rule_dpif_cast(rule_);
5255 complete_operation(rule);
5256 ovs_mutex_destroy(&rule->stats_mutex);
abe529af
BP
5257}
5258
5259static void
5260rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
5261{
5262 struct rule_dpif *rule = rule_dpif_cast(rule_);
abe529af 5263
4464589e
EJ
5264 /* push_all_stats() can handle flow misses which, when using the learn
5265 * action, can cause rules to be added and deleted. This can corrupt our
5266 * caller's datastructures which assume that rule_get_stats() doesn't have
5267 * an impact on the flow table. To be safe, we disable miss handling. */
5268 push_all_stats__(false);
bf1e8ff9 5269
abe529af
BP
5270 /* Start from historical data for 'rule' itself that are no longer tracked
5271 * in facets. This counts, for example, facets that have expired. */
9fbe253e 5272 ovs_mutex_lock(&rule->stats_mutex);
abe529af
BP
5273 *packets = rule->packet_count;
5274 *bytes = rule->byte_count;
9fbe253e 5275 ovs_mutex_unlock(&rule->stats_mutex);
abe529af
BP
5276}
5277
0a740f48
EJ
5278static void
5279rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
5280 struct ofpbuf *packet)
abe529af 5281{
abe529af 5282 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
112bc5f4 5283 struct dpif_flow_stats stats;
bbafd73b
EJ
5284 struct xlate_out xout;
5285 struct xlate_in xin;
abe529af 5286
a7752d4a 5287 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
112bc5f4
BP
5288 rule_credit_stats(rule, &stats);
5289
47b0deae 5290 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
bbafd73b
EJ
5291 xin.resubmit_stats = &stats;
5292 xlate_actions(&xin, &xout);
112bc5f4 5293
bbafd73b
EJ
5294 execute_odp_actions(ofproto, flow, xout.odp_actions.data,
5295 xout.odp_actions.size, packet);
112bc5f4 5296
bbafd73b 5297 xlate_out_uninit(&xout);
0a740f48 5298}
5bf0e941 5299
0a740f48
EJ
5300static enum ofperr
5301rule_execute(struct rule *rule, const struct flow *flow,
5302 struct ofpbuf *packet)
5303{
5304 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
5305 ofpbuf_delete(packet);
5bf0e941 5306 return 0;
abe529af
BP
5307}
5308
7ee20df1
BP
5309static void
5310rule_modify_actions(struct rule *rule_)
abe529af
BP
5311{
5312 struct rule_dpif *rule = rule_dpif_cast(rule_);
7ee20df1
BP
5313
5314 complete_operation(rule);
abe529af
BP
5315}
5316\f
97d6520b 5317/* Sends 'packet' out 'ofport'.
52a90c29 5318 * May modify 'packet'.
abe529af
BP
5319 * Returns 0 if successful, otherwise a positive errno value. */
5320static int
52a90c29 5321send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
abe529af 5322{
79e15b78 5323 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
b9ad7294 5324 uint64_t odp_actions_stub[1024 / 8];
80e5eed9 5325 struct ofpbuf key, odp_actions;
0f49659a 5326 struct dpif_flow_stats stats;
80e5eed9 5327 struct odputil_keybuf keybuf;
0f49659a 5328 struct ofpact_output output;
bbafd73b
EJ
5329 struct xlate_out xout;
5330 struct xlate_in xin;
80e5eed9 5331 struct flow flow;
4e022ec0 5332 union flow_in_port in_port_;
abe529af
BP
5333 int error;
5334
b9ad7294 5335 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
0f49659a 5336 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
b9ad7294 5337
0f49659a 5338 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4e022ec0
AW
5339 in_port_.ofp_port = OFPP_NONE;
5340 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
0f49659a
EJ
5341 odp_flow_key_from_flow(&key, &flow, ofp_port_to_odp_port(ofproto,
5342 OFPP_LOCAL));
5343 dpif_flow_stats_extract(&flow, packet, time_msec(), &stats);
b9ad7294 5344
0f49659a
EJ
5345 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5346 output.port = ofport->up.ofp_port;
5347 output.max_len = 0;
b9ad7294 5348
47b0deae 5349 xlate_in_init(&xin, ofproto, &flow, NULL, 0, packet);
bbafd73b
EJ
5350 xin.ofpacts_len = sizeof output;
5351 xin.ofpacts = &output.ofpact;
5352 xin.resubmit_stats = &stats;
5353 xlate_actions(&xin, &xout);
6ff686f2 5354
acf60855 5355 error = dpif_execute(ofproto->backer->dpif,
80e5eed9 5356 key.data, key.size,
bbafd73b 5357 xout.odp_actions.data, xout.odp_actions.size,
abe529af 5358 packet);
bbafd73b 5359 xlate_out_uninit(&xout);
abe529af
BP
5360
5361 if (error) {
0f49659a
EJ
5362 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %s (%s)",
5363 ofproto->up.name, netdev_get_name(ofport->up.netdev),
10a89ef0 5364 ovs_strerror(error));
abe529af 5365 }
79e15b78
EJ
5366
5367 ofproto->stats.tx_packets++;
5368 ofproto->stats.tx_bytes += packet->size;
abe529af
BP
5369 return error;
5370}
abe529af 5371
6a7e895f
BP
5372/* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
5373 * The action will state 'slow' as the reason that the action is in the slow
5374 * path. (This is purely informational: it allows a human viewing "ovs-dpctl
5375 * dump-flows" output to see why a flow is in the slow path.)
5376 *
5377 * The 'stub_size' bytes in 'stub' will be used to store the action.
5378 * 'stub_size' must be large enough for the action.
5379 *
5380 * The action and its size will be stored in '*actionsp' and '*actions_lenp',
5381 * respectively. */
5382static void
5383compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
5384 enum slow_path_reason slow,
5385 uint64_t *stub, size_t stub_size,
5386 const struct nlattr **actionsp, size_t *actions_lenp)
5387{
5388 union user_action_cookie cookie;
5389 struct ofpbuf buf;
5390
5391 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
5392 cookie.slow_path.unused = 0;
5393 cookie.slow_path.reason = slow;
5394
5395 ofpbuf_use_stack(&buf, stub, stub_size);
ccc09689 5396 if (slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)) {
4e022ec0
AW
5397 uint32_t pid = dpif_port_get_pid(ofproto->backer->dpif,
5398 ODPP_NONE);
29089a54 5399 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, &buf);
625b0720 5400 } else {
89a8a7f0
EJ
5401 odp_port_t odp_port;
5402 uint32_t pid;
5403
5404 odp_port = ofp_port_to_odp_port(ofproto, flow->in_port.ofp_port);
5405 pid = dpif_port_get_pid(ofproto->backer->dpif, odp_port);
5406 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, &buf);
625b0720 5407 }
6a7e895f
BP
5408 *actionsp = buf.data;
5409 *actions_lenp = buf.size;
5410}
9583bc14
EJ
5411\f
5412static bool
5413set_frag_handling(struct ofproto *ofproto_,
5414 enum ofp_config_flags frag_handling)
abe529af
BP
5415{
5416 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
7257b535 5417 if (frag_handling != OFPC_FRAG_REASM) {
2cc3c58e 5418 ofproto->backer->need_revalidate = REV_RECONFIGURE;
7257b535
BP
5419 return true;
5420 } else {
5421 return false;
5422 }
abe529af
BP
5423}
5424
90bf1e07 5425static enum ofperr
abe529af
BP
5426packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5427 const struct flow *flow,
f25d0cf3 5428 const struct ofpact *ofpacts, size_t ofpacts_len)
abe529af
BP
5429{
5430 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
548de4dd
BP
5431 struct odputil_keybuf keybuf;
5432 struct dpif_flow_stats stats;
bbafd73b
EJ
5433 struct xlate_out xout;
5434 struct xlate_in xin;
548de4dd 5435 struct ofpbuf key;
112bc5f4 5436
80e5eed9 5437
548de4dd 5438 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
e1b1d06a 5439 odp_flow_key_from_flow(&key, flow,
4e022ec0
AW
5440 ofp_port_to_odp_port(ofproto,
5441 flow->in_port.ofp_port));
050ac423 5442
548de4dd 5443 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
abe529af 5444
47b0deae 5445 xlate_in_init(&xin, ofproto, flow, NULL, stats.tcp_flags, packet);
bbafd73b
EJ
5446 xin.resubmit_stats = &stats;
5447 xin.ofpacts_len = ofpacts_len;
5448 xin.ofpacts = ofpacts;
2284188b 5449
bbafd73b 5450 xlate_actions(&xin, &xout);
acf60855 5451 dpif_execute(ofproto->backer->dpif, key.data, key.size,
bbafd73b
EJ
5452 xout.odp_actions.data, xout.odp_actions.size, packet);
5453 xlate_out_uninit(&xout);
2284188b 5454
548de4dd 5455 return 0;
abe529af 5456}
6fca1ffb
BP
5457\f
5458/* NetFlow. */
5459
5460static int
5461set_netflow(struct ofproto *ofproto_,
5462 const struct netflow_options *netflow_options)
5463{
5464 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5465
5466 if (netflow_options) {
5467 if (!ofproto->netflow) {
5468 ofproto->netflow = netflow_create();
7bab1576 5469 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6fca1ffb
BP
5470 }
5471 return netflow_set_options(ofproto->netflow, netflow_options);
7bab1576
EJ
5472 } else if (ofproto->netflow) {
5473 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6fca1ffb
BP
5474 netflow_destroy(ofproto->netflow);
5475 ofproto->netflow = NULL;
6fca1ffb 5476 }
7bab1576
EJ
5477
5478 return 0;
6fca1ffb 5479}
abe529af
BP
5480
5481static void
5482get_netflow_ids(const struct ofproto *ofproto_,
5483 uint8_t *engine_type, uint8_t *engine_id)
5484{
5485 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5486
acf60855 5487 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
abe529af 5488}
6fca1ffb
BP
5489
5490static void
5491send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5492{
5493 if (!facet_is_controller_flow(facet) &&
5494 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
b0f7b9b5 5495 struct subfacet *subfacet;
6fca1ffb
BP
5496 struct ofexpired expired;
5497
b0f7b9b5 5498 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
6a7e895f 5499 if (subfacet->path == SF_FAST_PATH) {
b0f7b9b5 5500 struct dpif_flow_stats stats;
6fca1ffb 5501
bcd2633a
JP
5502 subfacet_install(subfacet, &facet->xout.odp_actions,
5503 &stats);
15baa734 5504 subfacet_update_stats(subfacet, &stats);
b0f7b9b5 5505 }
6fca1ffb
BP
5506 }
5507
5508 expired.flow = facet->flow;
5509 expired.packet_count = facet->packet_count;
5510 expired.byte_count = facet->byte_count;
5511 expired.used = facet->used;
5512 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5513 }
5514}
5515
5516static void
5517send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5518{
bcd2633a 5519 struct cls_cursor cursor;
6fca1ffb
BP
5520 struct facet *facet;
5521
0b4f2078 5522 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
bcd2633a
JP
5523 cls_cursor_init(&cursor, &ofproto->facets, NULL);
5524 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
6fca1ffb
BP
5525 send_active_timeout(ofproto, facet);
5526 }
0b4f2078 5527 ovs_rwlock_unlock(&ofproto->facets.rwlock);
6fca1ffb 5528}
abe529af
BP
5529\f
5530static struct ofproto_dpif *
5531ofproto_dpif_lookup(const char *name)
5532{
b44a10b7
BP
5533 struct ofproto_dpif *ofproto;
5534
5535 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
5536 hash_string(name, 0), &all_ofproto_dpifs) {
5537 if (!strcmp(ofproto->up.name, name)) {
5538 return ofproto;
5539 }
5540 }
5541 return NULL;
abe529af
BP
5542}
5543
f0a3aa2e 5544static void
96e466a3 5545ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
0e15264f 5546 const char *argv[], void *aux OVS_UNUSED)
f0a3aa2e 5547{
490df1ef 5548 struct ofproto_dpif *ofproto;
f0a3aa2e 5549
96e466a3
EJ
5550 if (argc > 1) {
5551 ofproto = ofproto_dpif_lookup(argv[1]);
5552 if (!ofproto) {
bde9f75d 5553 unixctl_command_reply_error(conn, "no such bridge");
96e466a3
EJ
5554 return;
5555 }
509c0149 5556 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 5557 mac_learning_flush(ofproto->ml);
509c0149 5558 ovs_rwlock_unlock(&ofproto->ml->rwlock);
96e466a3
EJ
5559 } else {
5560 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
509c0149 5561 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
30618594 5562 mac_learning_flush(ofproto->ml);
509c0149 5563 ovs_rwlock_unlock(&ofproto->ml->rwlock);
96e466a3 5564 }
f0a3aa2e 5565 }
f0a3aa2e 5566
bde9f75d 5567 unixctl_command_reply(conn, "table successfully flushed");
f0a3aa2e
AA
5568}
5569
46c88433
EJ
5570static struct ofport_dpif *
5571ofbundle_get_a_port(const struct ofbundle *bundle)
5572{
5573 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
5574 bundle_node);
5575}
5576
abe529af 5577static void
0e15264f
BP
5578ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5579 const char *argv[], void *aux OVS_UNUSED)
abe529af
BP
5580{
5581 struct ds ds = DS_EMPTY_INITIALIZER;
5582 const struct ofproto_dpif *ofproto;
5583 const struct mac_entry *e;
5584
0e15264f 5585 ofproto = ofproto_dpif_lookup(argv[1]);
abe529af 5586 if (!ofproto) {
bde9f75d 5587 unixctl_command_reply_error(conn, "no such bridge");
abe529af
BP
5588 return;
5589 }
5590
5591 ds_put_cstr(&ds, " port VLAN MAC Age\n");
509c0149 5592 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
abe529af
BP
5593 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5594 struct ofbundle *bundle = e->port.p;
ad28062f
BP
5595 char name[OFP_MAX_PORT_NAME_LEN];
5596
5597 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5598 name, sizeof name);
5599 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
5600 name, e->vlan, ETH_ADDR_ARGS(e->mac),
e764773c 5601 mac_entry_age(ofproto->ml, e));
abe529af 5602 }
509c0149 5603 ovs_rwlock_unlock(&ofproto->ml->rwlock);
bde9f75d 5604 unixctl_command_reply(conn, ds_cstr(&ds));
abe529af
BP
5605 ds_destroy(&ds);
5606}
5607
6a6455e5 5608struct trace_ctx {
bbafd73b
EJ
5609 struct xlate_out xout;
5610 struct xlate_in xin;
abe529af
BP
5611 struct flow flow;
5612 struct ds *result;
5613};
5614
5615static void
4d0acc70 5616trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
abe529af
BP
5617{
5618 ds_put_char_multiple(result, '\t', level);
5619 if (!rule) {
5620 ds_put_cstr(result, "No match\n");
5621 return;
5622 }
5623
29901626 5624 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4d0acc70 5625 rule ? rule->up.table_id : 0, ntohll(rule->up.flow_cookie));
79feb7df 5626 cls_rule_format(&rule->up.cr, result);
abe529af
BP
5627 ds_put_char(result, '\n');
5628
5629 ds_put_char_multiple(result, '\t', level);
5630 ds_put_cstr(result, "OpenFlow ");
f25d0cf3 5631 ofpacts_format(rule->up.ofpacts, rule->up.ofpacts_len, result);
abe529af
BP
5632 ds_put_char(result, '\n');
5633}
5634
5635static void
5636trace_format_flow(struct ds *result, int level, const char *title,
bbafd73b 5637 struct trace_ctx *trace)
abe529af
BP
5638{
5639 ds_put_char_multiple(result, '\t', level);
5640 ds_put_format(result, "%s: ", title);
bbafd73b 5641 if (flow_equal(&trace->xin.flow, &trace->flow)) {
abe529af
BP
5642 ds_put_cstr(result, "unchanged");
5643 } else {
bbafd73b
EJ
5644 flow_format(result, &trace->xin.flow);
5645 trace->flow = trace->xin.flow;
abe529af
BP
5646 }
5647 ds_put_char(result, '\n');
5648}
5649
eb9e1c26
EJ
5650static void
5651trace_format_regs(struct ds *result, int level, const char *title,
6a6455e5 5652 struct trace_ctx *trace)
eb9e1c26
EJ
5653{
5654 size_t i;
5655
5656 ds_put_char_multiple(result, '\t', level);
5657 ds_put_format(result, "%s:", title);
5658 for (i = 0; i < FLOW_N_REGS; i++) {
5659 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5660 }
5661 ds_put_char(result, '\n');
5662}
5663
1ed8d352
EJ
5664static void
5665trace_format_odp(struct ds *result, int level, const char *title,
6a6455e5 5666 struct trace_ctx *trace)
1ed8d352 5667{
bbafd73b 5668 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
1ed8d352
EJ
5669
5670 ds_put_char_multiple(result, '\t', level);
5671 ds_put_format(result, "%s: ", title);
5672 format_odp_actions(result, odp_actions->data, odp_actions->size);
5673 ds_put_char(result, '\n');
5674}
5675
abe529af 5676static void
4d0acc70 5677trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
abe529af 5678{
4d0acc70 5679 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
abe529af
BP
5680 struct ds *result = trace->result;
5681
5682 ds_put_char(result, '\n');
4d0acc70
EJ
5683 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
5684 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
5685 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
5686 trace_format_rule(result, recurse + 1, rule);
abe529af
BP
5687}
5688
479df176 5689static void
4d0acc70 5690trace_report(struct xlate_in *xin, const char *s, int recurse)
479df176 5691{
4d0acc70 5692 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
479df176
BP
5693 struct ds *result = trace->result;
5694
4d0acc70 5695 ds_put_char_multiple(result, '\t', recurse);
479df176
BP
5696 ds_put_cstr(result, s);
5697 ds_put_char(result, '\n');
5698}
5699
abe529af 5700static void
0e15264f 5701ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
abe529af
BP
5702 void *aux OVS_UNUSED)
5703{
50aa28fd 5704 const struct dpif_backer *backer;
abe529af 5705 struct ofproto_dpif *ofproto;
ea5e3887 5706 struct ofpbuf odp_key, odp_mask;
876b0e1c 5707 struct ofpbuf *packet;
abe529af
BP
5708 struct ds result;
5709 struct flow flow;
abe529af
BP
5710 char *s;
5711
876b0e1c 5712 packet = NULL;
50aa28fd 5713 backer = NULL;
abe529af 5714 ds_init(&result);
50aa28fd 5715 ofpbuf_init(&odp_key, 0);
ea5e3887 5716 ofpbuf_init(&odp_mask, 0);
abe529af 5717
50aa28fd
AW
5718 /* Handle "-generate" or a hex string as the last argument. */
5719 if (!strcmp(argv[argc - 1], "-generate")) {
5720 packet = ofpbuf_new(0);
5721 argc--;
5722 } else {
5723 const char *error = eth_from_hex(argv[argc - 1], &packet);
5724 if (!error) {
5725 argc--;
5726 } else if (argc == 4) {
5727 /* The 3-argument form must end in "-generate' or a hex string. */
5728 unixctl_command_reply_error(conn, error);
5729 goto exit;
5730 }
e84173dc 5731 }
876b0e1c 5732
50aa28fd
AW
5733 /* Parse the flow and determine whether a datapath or
5734 * bridge is specified. If function odp_flow_key_from_string()
5735 * returns 0, the flow is a odp_flow. If function
5736 * parse_ofp_exact_flow() returns 0, the flow is a br_flow. */
ea5e3887 5737 if (!odp_flow_from_string(argv[argc - 1], NULL, &odp_key, &odp_mask)) {
50aa28fd
AW
5738 /* If the odp_flow is the second argument,
5739 * the datapath name is the first argument. */
5740 if (argc == 3) {
5741 const char *dp_type;
5742 if (!strncmp(argv[1], "ovs-", 4)) {
5743 dp_type = argv[1] + 4;
5744 } else {
5745 dp_type = argv[1];
31a19d69 5746 }
50aa28fd
AW
5747 backer = shash_find_data(&all_dpif_backers, dp_type);
5748 if (!backer) {
5749 unixctl_command_reply_error(conn, "Cannot find datapath "
5750 "of this name");
31a19d69
BP
5751 goto exit;
5752 }
5753 } else {
50aa28fd
AW
5754 /* No datapath name specified, so there should be only one
5755 * datapath. */
5756 struct shash_node *node;
5757 if (shash_count(&all_dpif_backers) != 1) {
5758 unixctl_command_reply_error(conn, "Must specify datapath "
5759 "name, there is more than one type of datapath");
31a19d69
BP
5760 goto exit;
5761 }
50aa28fd
AW
5762 node = shash_first(&all_dpif_backers);
5763 backer = node->data;
876b0e1c 5764 }
8b3b8dd1 5765
8449c4d6
EJ
5766 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, &flow,
5767 NULL, &ofproto, NULL)) {
50aa28fd
AW
5768 unixctl_command_reply_error(conn, "Invalid datapath flow");
5769 goto exit;
8b3b8dd1 5770 }
50aa28fd
AW
5771 ds_put_format(&result, "Bridge: %s\n", ofproto->up.name);
5772 } else if (!parse_ofp_exact_flow(&flow, argv[argc - 1])) {
5773 if (argc != 3) {
5774 unixctl_command_reply_error(conn, "Must specify bridge name");
876b0e1c
BP
5775 goto exit;
5776 }
5777
50aa28fd
AW
5778 ofproto = ofproto_dpif_lookup(argv[1]);
5779 if (!ofproto) {
5780 unixctl_command_reply_error(conn, "Unknown bridge name");
5781 goto exit;
5782 }
876b0e1c 5783 } else {
50aa28fd 5784 unixctl_command_reply_error(conn, "Bad flow syntax");
abe529af
BP
5785 goto exit;
5786 }
5787
50aa28fd
AW
5788 /* Generate a packet, if requested. */
5789 if (packet) {
5790 if (!packet->size) {
5791 flow_compose(packet, &flow);
5792 } else {
4e022ec0
AW
5793 union flow_in_port in_port_;
5794
5795 in_port_ = flow.in_port;
50aa28fd
AW
5796 ds_put_cstr(&result, "Packet: ");
5797 s = ofp_packet_to_string(packet->data, packet->size);
5798 ds_put_cstr(&result, s);
5799 free(s);
5800
5801 /* Use the metadata from the flow and the packet argument
5802 * to reconstruct the flow. */
5803 flow_extract(packet, flow.skb_priority, flow.skb_mark, NULL,
4e022ec0 5804 &in_port_, &flow);
50aa28fd
AW
5805 }
5806 }
5807
47b0deae 5808 ofproto_trace(ofproto, &flow, packet, &result);
6a6455e5
EJ
5809 unixctl_command_reply(conn, ds_cstr(&result));
5810
5811exit:
5812 ds_destroy(&result);
5813 ofpbuf_delete(packet);
5814 ofpbuf_uninit(&odp_key);
ea5e3887 5815 ofpbuf_uninit(&odp_mask);
6a6455e5
EJ
5816}
5817
ade6ad9c 5818static void
6a6455e5 5819ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
47b0deae 5820 const struct ofpbuf *packet, struct ds *ds)
6a6455e5
EJ
5821{
5822 struct rule_dpif *rule;
5823
5824 ds_put_cstr(ds, "Flow: ");
5825 flow_format(ds, flow);
5826 ds_put_char(ds, '\n');
abe529af 5827
bcd2633a 5828 rule = rule_dpif_lookup(ofproto, flow, NULL);
c57b2226 5829
4d0acc70 5830 trace_format_rule(ds, 0, rule);
c57b2226
BP
5831 if (rule == ofproto->miss_rule) {
5832 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
5833 } else if (rule == ofproto->no_packet_in_rule) {
5834 ds_put_cstr(ds, "\nNo match, packets dropped because "
5835 "OFPPC_NO_PACKET_IN is set on in_port.\n");
7fd51d39
BP
5836 } else if (rule == ofproto->drop_frags_rule) {
5837 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
5838 "and the fragment handling mode is \"drop\".\n");
c57b2226
BP
5839 }
5840
abe529af 5841 if (rule) {
050ac423
BP
5842 uint64_t odp_actions_stub[1024 / 8];
5843 struct ofpbuf odp_actions;
6a6455e5 5844 struct trace_ctx trace;
bcd2633a 5845 struct match match;
0e553d9c 5846 uint8_t tcp_flags;
abe529af 5847
6a6455e5
EJ
5848 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
5849 trace.result = ds;
5850 trace.flow = *flow;
050ac423
BP
5851 ofpbuf_use_stub(&odp_actions,
5852 odp_actions_stub, sizeof odp_actions_stub);
47b0deae 5853 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
bbafd73b
EJ
5854 trace.xin.resubmit_hook = trace_resubmit;
5855 trace.xin.report_hook = trace_report;
bcd2633a 5856
bbafd73b 5857 xlate_actions(&trace.xin, &trace.xout);
abe529af 5858
6a6455e5
EJ
5859 ds_put_char(ds, '\n');
5860 trace_format_flow(ds, 0, "Final flow", &trace);
bcd2633a
JP
5861
5862 match_init(&match, flow, &trace.xout.wc);
5863 ds_put_cstr(ds, "Relevant fields: ");
5864 match_format(&match, ds, OFP_DEFAULT_PRIORITY);
5865 ds_put_char(ds, '\n');
5866
6a6455e5 5867 ds_put_cstr(ds, "Datapath actions: ");
bbafd73b
EJ
5868 format_odp_actions(ds, trace.xout.odp_actions.data,
5869 trace.xout.odp_actions.size);
876b0e1c 5870
bbafd73b 5871 if (trace.xout.slow) {
6a7e895f
BP
5872 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
5873 "slow path because it:");
bbafd73b 5874 switch (trace.xout.slow) {
98f0520f
EJ
5875 case SLOW_CFM:
5876 ds_put_cstr(ds, "\n\t- Consists of CFM packets.");
5877 break;
5878 case SLOW_LACP:
5879 ds_put_cstr(ds, "\n\t- Consists of LACP packets.");
5880 break;
5881 case SLOW_STP:
5882 ds_put_cstr(ds, "\n\t- Consists of STP packets.");
5883 break;
5884 case SLOW_BFD:
5885 ds_put_cstr(ds, "\n\t- Consists of BFD packets.");
5886 break;
5887 case SLOW_CONTROLLER:
5888 ds_put_cstr(ds, "\n\t- Sends \"packet-in\" messages "
5889 "to the OpenFlow controller.");
5890 break;
5891 case __SLOW_MAX:
5892 NOT_REACHED();
6a7e895f 5893 }
876b0e1c 5894 }
bbafd73b
EJ
5895
5896 xlate_out_uninit(&trace.xout);
abe529af 5897 }
abe529af
BP
5898}
5899
7ee20df1 5900static void
0e15264f
BP
5901ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5902 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7ee20df1
BP
5903{
5904 clogged = true;
bde9f75d 5905 unixctl_command_reply(conn, NULL);
7ee20df1
BP
5906}
5907
5908static void
0e15264f
BP
5909ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5910 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7ee20df1
BP
5911{
5912 clogged = false;
bde9f75d 5913 unixctl_command_reply(conn, NULL);
7ee20df1
BP
5914}
5915
6814e51f
BP
5916/* Runs a self-check of flow translations in 'ofproto'. Appends a message to
5917 * 'reply' describing the results. */
5918static void
5919ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
5920{
bcd2633a 5921 struct cls_cursor cursor;
6814e51f
BP
5922 struct facet *facet;
5923 int errors;
5924
5925 errors = 0;
0b4f2078 5926 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
bcd2633a
JP
5927 cls_cursor_init(&cursor, &ofproto->facets, NULL);
5928 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
6814e51f
BP
5929 if (!facet_check_consistency(facet)) {
5930 errors++;
5931 }
5932 }
0b4f2078 5933 ovs_rwlock_unlock(&ofproto->facets.rwlock);
6814e51f 5934 if (errors) {
2cc3c58e 5935 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
6814e51f
BP
5936 }
5937
5938 if (errors) {
5939 ds_put_format(reply, "%s: self-check failed (%d errors)\n",
5940 ofproto->up.name, errors);
5941 } else {
5942 ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
5943 }
5944}
5945
5946static void
5947ofproto_dpif_self_check(struct unixctl_conn *conn,
5948 int argc, const char *argv[], void *aux OVS_UNUSED)
5949{
5950 struct ds reply = DS_EMPTY_INITIALIZER;
5951 struct ofproto_dpif *ofproto;
5952
5953 if (argc > 1) {
5954 ofproto = ofproto_dpif_lookup(argv[1]);
5955 if (!ofproto) {
bde9f75d
EJ
5956 unixctl_command_reply_error(conn, "Unknown ofproto (use "
5957 "ofproto/list for help)");
6814e51f
BP
5958 return;
5959 }
5960 ofproto_dpif_self_check__(ofproto, &reply);
5961 } else {
5962 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5963 ofproto_dpif_self_check__(ofproto, &reply);
5964 }
5965 }
5966
bde9f75d 5967 unixctl_command_reply(conn, ds_cstr(&reply));
6814e51f
BP
5968 ds_destroy(&reply);
5969}
5970
27022416
JP
5971/* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5972 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5973 * to destroy 'ofproto_shash' and free the returned value. */
5974static const struct shash_node **
5975get_ofprotos(struct shash *ofproto_shash)
5976{
5977 const struct ofproto_dpif *ofproto;
5978
5979 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5980 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5981 shash_add_nocopy(ofproto_shash, name, ofproto);
5982 }
5983
5984 return shash_sort(ofproto_shash);
5985}
5986
5987static void
5988ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5989 const char *argv[] OVS_UNUSED,
5990 void *aux OVS_UNUSED)
5991{
5992 struct ds ds = DS_EMPTY_INITIALIZER;
5993 struct shash ofproto_shash;
5994 const struct shash_node **sorted_ofprotos;
5995 int i;
5996
5997 shash_init(&ofproto_shash);
5998 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5999 for (i = 0; i < shash_count(&ofproto_shash); i++) {
6000 const struct shash_node *node = sorted_ofprotos[i];
6001 ds_put_format(&ds, "%s\n", node->name);
6002 }
6003
6004 shash_destroy(&ofproto_shash);
6005 free(sorted_ofprotos);
6006
6007 unixctl_command_reply(conn, ds_cstr(&ds));
6008 ds_destroy(&ds);
6009}
6010
6011static void
dc54ef36
EJ
6012show_dp_rates(struct ds *ds, const char *heading,
6013 const struct avg_subfacet_rates *rates)
6014{
6015 ds_put_format(ds, "%s add rate: %5.3f/min, del rate: %5.3f/min\n",
6016 heading, rates->add_rate, rates->del_rate);
6017}
6018
6019static void
6020dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
27022416 6021{
dc54ef36
EJ
6022 const struct shash_node **ofprotos;
6023 struct ofproto_dpif *ofproto;
6024 struct shash ofproto_shash;
09672174 6025 uint64_t n_hit, n_missed;
dc54ef36 6026 long long int minutes;
09672174 6027 size_t i;
655ab909 6028
04d08d54 6029 n_hit = n_missed = 0;
dc54ef36
EJ
6030 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6031 if (ofproto->backer == backer) {
dc54ef36
EJ
6032 n_missed += ofproto->n_missed;
6033 n_hit += ofproto->n_hit;
6034 }
6035 }
655ab909 6036
dc54ef36
EJ
6037 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
6038 dpif_name(backer->dpif), n_hit, n_missed);
6039 ds_put_format(ds, "\tflows: cur: %zu, avg: %u, max: %u,"
04d08d54 6040 " life span: %lldms\n", hmap_count(&backer->subfacets),
dc54ef36
EJ
6041 backer->avg_n_subfacet, backer->max_n_subfacet,
6042 backer->avg_subfacet_life);
6043
6044 minutes = (time_msec() - backer->created) / (1000 * 60);
655ab909 6045 if (minutes >= 60) {
dc54ef36 6046 show_dp_rates(ds, "\thourly avg:", &backer->hourly);
655ab909
AZ
6047 }
6048 if (minutes >= 60 * 24) {
dc54ef36 6049 show_dp_rates(ds, "\tdaily avg:", &backer->daily);
655ab909 6050 }
dc54ef36 6051 show_dp_rates(ds, "\toverall avg:", &backer->lifetime);
27022416 6052
dc54ef36
EJ
6053 shash_init(&ofproto_shash);
6054 ofprotos = get_ofprotos(&ofproto_shash);
6055 for (i = 0; i < shash_count(&ofproto_shash); i++) {
6056 struct ofproto_dpif *ofproto = ofprotos[i]->data;
6057 const struct shash_node **ports;
6058 size_t j;
0a740f48 6059
dc54ef36
EJ
6060 if (ofproto->backer != backer) {
6061 continue;
0a740f48 6062 }
27022416 6063
dc54ef36
EJ
6064 ds_put_format(ds, "\t%s: hit:%"PRIu64" missed:%"PRIu64"\n",
6065 ofproto->up.name, ofproto->n_hit, ofproto->n_missed);
6066
6067 ports = shash_sort(&ofproto->up.port_by_name);
6068 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
6069 const struct shash_node *node = ports[j];
6070 struct ofport *ofport = node->data;
6071 struct smap config;
4e022ec0 6072 odp_port_t odp_port;
27022416 6073
dc54ef36
EJ
6074 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
6075 ofport->ofp_port);
27022416 6076
dc54ef36 6077 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4e022ec0 6078 if (odp_port != ODPP_NONE) {
dc54ef36
EJ
6079 ds_put_format(ds, "%"PRIu32":", odp_port);
6080 } else {
6081 ds_put_cstr(ds, "none:");
6082 }
27022416 6083
dc54ef36 6084 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
27022416 6085
dc54ef36
EJ
6086 smap_init(&config);
6087 if (!netdev_get_config(ofport->netdev, &config)) {
6088 const struct smap_node **nodes;
6089 size_t i;
27022416 6090
dc54ef36
EJ
6091 nodes = smap_sort(&config);
6092 for (i = 0; i < smap_count(&config); i++) {
6093 const struct smap_node *node = nodes[i];
6094 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
6095 node->key, node->value);
6096 }
6097 free(nodes);
27022416 6098 }
dc54ef36
EJ
6099 smap_destroy(&config);
6100
27022416 6101 ds_put_char(ds, ')');
dc54ef36 6102 ds_put_char(ds, '\n');
27022416 6103 }
dc54ef36 6104 free(ports);
27022416 6105 }
dc54ef36
EJ
6106 shash_destroy(&ofproto_shash);
6107 free(ofprotos);
27022416
JP
6108}
6109
6110static void
dc54ef36
EJ
6111ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
6112 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
27022416
JP
6113{
6114 struct ds ds = DS_EMPTY_INITIALIZER;
dc54ef36
EJ
6115 const struct shash_node **backers;
6116 int i;
27022416 6117
dc54ef36
EJ
6118 backers = shash_sort(&all_dpif_backers);
6119 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
6120 dpif_show_backer(backers[i]->data, &ds);
27022416 6121 }
dc54ef36 6122 free(backers);
27022416
JP
6123
6124 unixctl_command_reply(conn, ds_cstr(&ds));
6125 ds_destroy(&ds);
6126}
6127
bcd2633a
JP
6128/* Dump the megaflow (facet) cache. This is useful to check the
6129 * correctness of flow wildcarding, since the same mechanism is used for
6130 * both xlate caching and kernel wildcarding.
6131 *
6132 * It's important to note that in the output the flow description uses
6133 * OpenFlow (OFP) ports, but the actions use datapath (ODP) ports.
6134 *
6135 * This command is only needed for advanced debugging, so it's not
6136 * documented in the man page. */
6137static void
6138ofproto_unixctl_dpif_dump_megaflows(struct unixctl_conn *conn,
6139 int argc OVS_UNUSED, const char *argv[],
6140 void *aux OVS_UNUSED)
6141{
6142 struct ds ds = DS_EMPTY_INITIALIZER;
6143 const struct ofproto_dpif *ofproto;
6144 long long int now = time_msec();
6145 struct cls_cursor cursor;
6146 struct facet *facet;
6147
6148 ofproto = ofproto_dpif_lookup(argv[1]);
6149 if (!ofproto) {
6150 unixctl_command_reply_error(conn, "no such bridge");
6151 return;
6152 }
6153
0b4f2078 6154 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
bcd2633a
JP
6155 cls_cursor_init(&cursor, &ofproto->facets, NULL);
6156 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
6157 cls_rule_format(&facet->cr, &ds);
6158 ds_put_cstr(&ds, ", ");
7ee9e841 6159 ds_put_format(&ds, "n_subfacets:%zu, ", list_size(&facet->subfacets));
bcd2633a
JP
6160 ds_put_format(&ds, "used:%.3fs, ", (now - facet->used) / 1000.0);
6161 ds_put_cstr(&ds, "Datapath actions: ");
8ecafa3e
JP
6162 if (facet->xout.slow) {
6163 uint64_t slow_path_stub[128 / 8];
6164 const struct nlattr *actions;
6165 size_t actions_len;
6166
6167 compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
6168 slow_path_stub, sizeof slow_path_stub,
6169 &actions, &actions_len);
6170 format_odp_actions(&ds, actions, actions_len);
6171 } else {
6172 format_odp_actions(&ds, facet->xout.odp_actions.data,
6173 facet->xout.odp_actions.size);
6174 }
bcd2633a
JP
6175 ds_put_cstr(&ds, "\n");
6176 }
0b4f2078 6177 ovs_rwlock_unlock(&ofproto->facets.rwlock);
bcd2633a
JP
6178
6179 ds_chomp(&ds, '\n');
6180 unixctl_command_reply(conn, ds_cstr(&ds));
6181 ds_destroy(&ds);
6182}
6183
6b35e630
JP
6184/* Disable using the megaflows.
6185 *
6186 * This command is only needed for advanced debugging, so it's not
6187 * documented in the man page. */
6188static void
6189ofproto_unixctl_dpif_disable_megaflows(struct unixctl_conn *conn,
6190 int argc OVS_UNUSED,
6191 const char *argv[] OVS_UNUSED,
6192 void *aux OVS_UNUSED)
6193{
6194 struct ofproto_dpif *ofproto;
6195
6196 enable_megaflows = false;
6197
6198 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6199 flush(&ofproto->up);
6200 }
6201
6202 unixctl_command_reply(conn, "megaflows disabled");
6203}
6204
6205/* Re-enable using megaflows.
6206 *
6207 * This command is only needed for advanced debugging, so it's not
6208 * documented in the man page. */
6209static void
6210ofproto_unixctl_dpif_enable_megaflows(struct unixctl_conn *conn,
6211 int argc OVS_UNUSED,
6212 const char *argv[] OVS_UNUSED,
6213 void *aux OVS_UNUSED)
6214{
6215 struct ofproto_dpif *ofproto;
6216
6217 enable_megaflows = true;
6218
6219 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6220 flush(&ofproto->up);
6221 }
6222
6223 unixctl_command_reply(conn, "megaflows enabled");
6224}
6225
27022416
JP
6226static void
6227ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
6228 int argc OVS_UNUSED, const char *argv[],
6229 void *aux OVS_UNUSED)
6230{
6231 struct ds ds = DS_EMPTY_INITIALIZER;
6232 const struct ofproto_dpif *ofproto;
6233 struct subfacet *subfacet;
6234
6235 ofproto = ofproto_dpif_lookup(argv[1]);
6236 if (!ofproto) {
6237 unixctl_command_reply_error(conn, "no such bridge");
6238 return;
6239 }
6240
af37354d
EJ
6241 update_stats(ofproto->backer);
6242
04d08d54 6243 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->backer->subfacets) {
4dff9097 6244 struct facet *facet = subfacet->facet;
64f9cb76
JP
6245 struct odputil_keybuf maskbuf;
6246 struct ofpbuf mask;
4dff9097 6247
b7e2f6b3 6248 if (facet->ofproto != ofproto) {
04d08d54
EJ
6249 continue;
6250 }
6251
64f9cb76
JP
6252 ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
6253 if (enable_megaflows) {
6254 odp_flow_key_from_mask(&mask, &facet->xout.wc.masks,
6255 &facet->flow, UINT32_MAX);
6256 }
6257
6258 odp_flow_format(subfacet->key, subfacet->key_len,
041e7168 6259 mask.data, mask.size, &ds, false);
27022416
JP
6260
6261 ds_put_format(&ds, ", packets:%"PRIu64", bytes:%"PRIu64", used:",
6262 subfacet->dp_packet_count, subfacet->dp_byte_count);
6263 if (subfacet->used) {
6264 ds_put_format(&ds, "%.3fs",
6265 (time_msec() - subfacet->used) / 1000.0);
6266 } else {
6267 ds_put_format(&ds, "never");
6268 }
6269 if (subfacet->facet->tcp_flags) {
6270 ds_put_cstr(&ds, ", flags:");
6271 packet_format_tcp_flags(&ds, subfacet->facet->tcp_flags);
6272 }
6273
6274 ds_put_cstr(&ds, ", actions:");
bbafd73b 6275 if (facet->xout.slow) {
f2245da3
JP
6276 uint64_t slow_path_stub[128 / 8];
6277 const struct nlattr *actions;
6278 size_t actions_len;
6279
bbafd73b 6280 compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
f2245da3
JP
6281 slow_path_stub, sizeof slow_path_stub,
6282 &actions, &actions_len);
6283 format_odp_actions(&ds, actions, actions_len);
6284 } else {
bbafd73b
EJ
6285 format_odp_actions(&ds, facet->xout.odp_actions.data,
6286 facet->xout.odp_actions.size);
f2245da3 6287 }
27022416
JP
6288 ds_put_char(&ds, '\n');
6289 }
6290
6291 unixctl_command_reply(conn, ds_cstr(&ds));
6292 ds_destroy(&ds);
6293}
6294
6295static void
6296ofproto_unixctl_dpif_del_flows(struct unixctl_conn *conn,
6297 int argc OVS_UNUSED, const char *argv[],
6298 void *aux OVS_UNUSED)
6299{
6300 struct ds ds = DS_EMPTY_INITIALIZER;
6301 struct ofproto_dpif *ofproto;
6302
6303 ofproto = ofproto_dpif_lookup(argv[1]);
6304 if (!ofproto) {
6305 unixctl_command_reply_error(conn, "no such bridge");
6306 return;
6307 }
6308
6309 flush(&ofproto->up);
6310
6311 unixctl_command_reply(conn, ds_cstr(&ds));
6312 ds_destroy(&ds);
6313}
6314
abe529af
BP
6315static void
6316ofproto_dpif_unixctl_init(void)
6317{
6318 static bool registered;
6319 if (registered) {
6320 return;
6321 }
6322 registered = true;
6323
0e15264f
BP
6324 unixctl_command_register(
6325 "ofproto/trace",
50aa28fd
AW
6326 "[dp_name]|bridge odp_flow|br_flow [-generate|packet]",
6327 1, 3, ofproto_unixctl_trace, NULL);
96e466a3 6328 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
0e15264f
BP
6329 ofproto_unixctl_fdb_flush, NULL);
6330 unixctl_command_register("fdb/show", "bridge", 1, 1,
6331 ofproto_unixctl_fdb_show, NULL);
6332 unixctl_command_register("ofproto/clog", "", 0, 0,
6333 ofproto_dpif_clog, NULL);
6334 unixctl_command_register("ofproto/unclog", "", 0, 0,
6335 ofproto_dpif_unclog, NULL);
6814e51f
BP
6336 unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
6337 ofproto_dpif_self_check, NULL);
27022416
JP
6338 unixctl_command_register("dpif/dump-dps", "", 0, 0,
6339 ofproto_unixctl_dpif_dump_dps, NULL);
dc54ef36
EJ
6340 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
6341 NULL);
27022416
JP
6342 unixctl_command_register("dpif/dump-flows", "bridge", 1, 1,
6343 ofproto_unixctl_dpif_dump_flows, NULL);
6344 unixctl_command_register("dpif/del-flows", "bridge", 1, 1,
6345 ofproto_unixctl_dpif_del_flows, NULL);
bcd2633a
JP
6346 unixctl_command_register("dpif/dump-megaflows", "bridge", 1, 1,
6347 ofproto_unixctl_dpif_dump_megaflows, NULL);
6b35e630
JP
6348 unixctl_command_register("dpif/disable-megaflows", "", 0, 0,
6349 ofproto_unixctl_dpif_disable_megaflows, NULL);
6350 unixctl_command_register("dpif/enable-megaflows", "", 0, 0,
6351 ofproto_unixctl_dpif_enable_megaflows, NULL);
abe529af
BP
6352}
6353\f
52a90c29
BP
6354/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6355 *
6356 * This is deprecated. It is only for compatibility with broken device drivers
6357 * in old versions of Linux that do not properly support VLANs when VLAN
6358 * devices are not used. When broken device drivers are no longer in
6359 * widespread use, we will delete these interfaces. */
6360
6361static int
4e022ec0 6362set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
52a90c29
BP
6363{
6364 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
6365 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
6366
6367 if (realdev_ofp_port == ofport->realdev_ofp_port
6368 && vid == ofport->vlandev_vid) {
6369 return 0;
6370 }
6371
2cc3c58e 6372 ofproto->backer->need_revalidate = REV_RECONFIGURE;
52a90c29
BP
6373
6374 if (ofport->realdev_ofp_port) {
6375 vsp_remove(ofport);
6376 }
6377 if (realdev_ofp_port && ofport->bundle) {
6378 /* vlandevs are enslaved to their realdevs, so they are not allowed to
6379 * themselves be part of a bundle. */
6380 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
6381 }
6382
6383 ofport->realdev_ofp_port = realdev_ofp_port;
6384 ofport->vlandev_vid = vid;
6385
6386 if (realdev_ofp_port) {
6387 vsp_add(ofport, realdev_ofp_port, vid);
6388 }
6389
6390 return 0;
6391}
6392
6393static uint32_t
4e022ec0 6394hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
52a90c29 6395{
4e022ec0 6396 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
52a90c29
BP
6397}
6398
46c88433
EJ
6399bool
6400ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
13501305 6401 OVS_EXCLUDED(ofproto->vsp_mutex)
46c88433 6402{
13501305
EJ
6403 bool ret;
6404
6405 ovs_mutex_lock(&ofproto->vsp_mutex);
6406 ret = !hmap_is_empty(&ofproto->realdev_vid_map);
6407 ovs_mutex_unlock(&ofproto->vsp_mutex);
6408 return ret;
46c88433
EJ
6409}
6410
13501305
EJ
6411static ofp_port_t
6412vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
6413 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
6414 OVS_REQUIRES(ofproto->vsp_mutex)
52a90c29
BP
6415{
6416 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
52a90c29
BP
6417 int vid = vlan_tci_to_vid(vlan_tci);
6418 const struct vlan_splinter *vsp;
6419
6420 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
6421 hash_realdev_vid(realdev_ofp_port, vid),
6422 &ofproto->realdev_vid_map) {
6423 if (vsp->realdev_ofp_port == realdev_ofp_port
6424 && vsp->vid == vid) {
deea1200 6425 return vsp->vlandev_ofp_port;
52a90c29
BP
6426 }
6427 }
6428 }
deea1200 6429 return realdev_ofp_port;
52a90c29
BP
6430}
6431
13501305
EJ
6432/* Returns the OFP port number of the Linux VLAN device that corresponds to
6433 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
6434 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
6435 * 'vlan_tci' 9, it would return the port number of eth0.9.
6436 *
6437 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
6438 * function just returns its 'realdev_ofp_port' argument. */
6439ofp_port_t
6440vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
6441 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
6442 OVS_EXCLUDED(ofproto->vsp_mutex)
6443{
6444 ofp_port_t ret;
6445
6446 ovs_mutex_lock(&ofproto->vsp_mutex);
6447 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
6448 ovs_mutex_unlock(&ofproto->vsp_mutex);
6449 return ret;
6450}
6451
52a90c29 6452static struct vlan_splinter *
4e022ec0 6453vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
52a90c29
BP
6454{
6455 struct vlan_splinter *vsp;
6456
4e022ec0 6457 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
f9c0c3ec 6458 hash_ofp_port(vlandev_ofp_port),
52a90c29
BP
6459 &ofproto->vlandev_map) {
6460 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
6461 return vsp;
6462 }
6463 }
6464
6465 return NULL;
6466}
6467
40e05935
BP
6468/* Returns the OpenFlow port number of the "real" device underlying the Linux
6469 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
6470 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
6471 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
6472 * eth0 and store 9 in '*vid'.
6473 *
6474 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
6475 * VLAN device. Unless VLAN splinters are enabled, this is what this function
6476 * always does.*/
4e022ec0 6477static ofp_port_t
52a90c29 6478vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4e022ec0 6479 ofp_port_t vlandev_ofp_port, int *vid)
bd3950dd 6480 OVS_REQUIRES(ofproto->vsp_mutex)
52a90c29
BP
6481{
6482 if (!hmap_is_empty(&ofproto->vlandev_map)) {
6483 const struct vlan_splinter *vsp;
6484
6485 vsp = vlandev_find(ofproto, vlandev_ofp_port);
6486 if (vsp) {
6487 if (vid) {
6488 *vid = vsp->vid;
6489 }
6490 return vsp->realdev_ofp_port;
6491 }
6492 }
6493 return 0;
6494}
6495
b98d8985
BP
6496/* Given 'flow', a flow representing a packet received on 'ofproto', checks
6497 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
6498 * 'flow->in_port' to the "real" device backing the VLAN device, sets
6499 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
6500 * always the case unless VLAN splinters are enabled), returns false without
6501 * making any changes. */
8449c4d6 6502bool
b98d8985 6503vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
13501305 6504 OVS_EXCLUDED(ofproto->vsp_mutex)
b98d8985 6505{
4e022ec0 6506 ofp_port_t realdev;
b98d8985
BP
6507 int vid;
6508
13501305 6509 ovs_mutex_lock(&ofproto->vsp_mutex);
4e022ec0 6510 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
13501305 6511 ovs_mutex_unlock(&ofproto->vsp_mutex);
b98d8985
BP
6512 if (!realdev) {
6513 return false;
6514 }
6515
6516 /* Cause the flow to be processed as if it came in on the real device with
6517 * the VLAN device's VLAN ID. */
4e022ec0 6518 flow->in_port.ofp_port = realdev;
b98d8985
BP
6519 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
6520 return true;
6521}
6522
52a90c29
BP
6523static void
6524vsp_remove(struct ofport_dpif *port)
6525{
6526 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6527 struct vlan_splinter *vsp;
6528
13501305 6529 ovs_mutex_lock(&ofproto->vsp_mutex);
52a90c29
BP
6530 vsp = vlandev_find(ofproto, port->up.ofp_port);
6531 if (vsp) {
6532 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
6533 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
6534 free(vsp);
6535
6536 port->realdev_ofp_port = 0;
6537 } else {
6538 VLOG_ERR("missing vlan device record");
6539 }
13501305 6540 ovs_mutex_unlock(&ofproto->vsp_mutex);
52a90c29
BP
6541}
6542
6543static void
4e022ec0 6544vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
52a90c29
BP
6545{
6546 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6547
13501305 6548 ovs_mutex_lock(&ofproto->vsp_mutex);
52a90c29 6549 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
13501305 6550 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
52a90c29
BP
6551 == realdev_ofp_port)) {
6552 struct vlan_splinter *vsp;
6553
6554 vsp = xmalloc(sizeof *vsp);
52a90c29
BP
6555 vsp->realdev_ofp_port = realdev_ofp_port;
6556 vsp->vlandev_ofp_port = port->up.ofp_port;
6557 vsp->vid = vid;
6558
6559 port->realdev_ofp_port = realdev_ofp_port;
13501305
EJ
6560
6561 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
6562 hash_ofp_port(port->up.ofp_port));
6563 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
6564 hash_realdev_vid(realdev_ofp_port, vid));
52a90c29
BP
6565 } else {
6566 VLOG_ERR("duplicate vlan device record");
6567 }
13501305 6568 ovs_mutex_unlock(&ofproto->vsp_mutex);
52a90c29 6569}
e1b1d06a 6570
46c88433 6571static odp_port_t
4e022ec0 6572ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
e1b1d06a
JP
6573{
6574 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4e022ec0 6575 return ofport ? ofport->odp_port : ODPP_NONE;
e1b1d06a
JP
6576}
6577
8449c4d6 6578struct ofport_dpif *
4e022ec0 6579odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
e1b1d06a
JP
6580{
6581 struct ofport_dpif *port;
6582
8449c4d6 6583 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
f9c0c3ec 6584 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
acf60855 6585 &backer->odp_to_ofport_map) {
e1b1d06a 6586 if (port->odp_port == odp_port) {
8449c4d6 6587 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855 6588 return port;
e1b1d06a
JP
6589 }
6590 }
6591
8449c4d6 6592 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
acf60855
JP
6593 return NULL;
6594}
6595
4e022ec0
AW
6596static ofp_port_t
6597odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
acf60855
JP
6598{
6599 struct ofport_dpif *port;
6600
6601 port = odp_port_to_ofport(ofproto->backer, odp_port);
6472ba11 6602 if (port && &ofproto->up == port->up.ofproto) {
acf60855
JP
6603 return port->up.ofp_port;
6604 } else {
6605 return OFPP_NONE;
6606 }
e1b1d06a 6607}
655ab909 6608
655ab909
AZ
6609/* Compute exponentially weighted moving average, adding 'new' as the newest,
6610 * most heavily weighted element. 'base' designates the rate of decay: after
6611 * 'base' further updates, 'new''s weight in the EWMA decays to about 1/e
6612 * (about .37). */
6613static void
6614exp_mavg(double *avg, int base, double new)
6615{
6616 *avg = (*avg * (base - 1) + new) / base;
6617}
6618
6619static void
dc54ef36 6620update_moving_averages(struct dpif_backer *backer)
655ab909
AZ
6621{
6622 const int min_ms = 60 * 1000; /* milliseconds in one minute. */
dc54ef36
EJ
6623 long long int minutes = (time_msec() - backer->created) / min_ms;
6624
6625 if (minutes > 0) {
6626 backer->lifetime.add_rate = (double) backer->total_subfacet_add_count
6627 / minutes;
6628 backer->lifetime.del_rate = (double) backer->total_subfacet_del_count
6629 / minutes;
6630 } else {
6631 backer->lifetime.add_rate = 0.0;
6632 backer->lifetime.del_rate = 0.0;
6633 }
655ab909
AZ
6634
6635 /* Update hourly averages on the minute boundaries. */
dc54ef36
EJ
6636 if (time_msec() - backer->last_minute >= min_ms) {
6637 exp_mavg(&backer->hourly.add_rate, 60, backer->subfacet_add_count);
6638 exp_mavg(&backer->hourly.del_rate, 60, backer->subfacet_del_count);
655ab909
AZ
6639
6640 /* Update daily averages on the hour boundaries. */
dc54ef36
EJ
6641 if ((backer->last_minute - backer->created) / min_ms % 60 == 59) {
6642 exp_mavg(&backer->daily.add_rate, 24, backer->hourly.add_rate);
6643 exp_mavg(&backer->daily.del_rate, 24, backer->hourly.del_rate);
655ab909
AZ
6644 }
6645
dc54ef36
EJ
6646 backer->total_subfacet_add_count += backer->subfacet_add_count;
6647 backer->total_subfacet_del_count += backer->subfacet_del_count;
6648 backer->subfacet_add_count = 0;
6649 backer->subfacet_del_count = 0;
6650 backer->last_minute += min_ms;
655ab909
AZ
6651 }
6652}
e1b1d06a 6653
abe529af 6654const struct ofproto_class ofproto_dpif_class = {
b0408fca 6655 init,
abe529af
BP
6656 enumerate_types,
6657 enumerate_names,
6658 del,
0aeaabc8 6659 port_open_type,
acf60855
JP
6660 type_run,
6661 type_run_fast,
6662 type_wait,
abe529af
BP
6663 alloc,
6664 construct,
6665 destruct,
6666 dealloc,
6667 run,
5fcc0d00 6668 run_fast,
abe529af 6669 wait,
0d085684 6670 get_memory_usage,
abe529af 6671 flush,
6c1491fb
BP
6672 get_features,
6673 get_tables,
abe529af
BP
6674 port_alloc,
6675 port_construct,
6676 port_destruct,
6677 port_dealloc,
6678 port_modified,
6679 port_reconfigured,
6680 port_query_by_name,
6681 port_add,
6682 port_del,
6527c598 6683 port_get_stats,
abe529af
BP
6684 port_dump_start,
6685 port_dump_next,
6686 port_dump_done,
6687 port_poll,
6688 port_poll_wait,
6689 port_is_lacp_current,
0ab6decf 6690 NULL, /* rule_choose_table */
abe529af
BP
6691 rule_alloc,
6692 rule_construct,
6693 rule_destruct,
6694 rule_dealloc,
abe529af
BP
6695 rule_get_stats,
6696 rule_execute,
6697 rule_modify_actions,
7257b535 6698 set_frag_handling,
abe529af
BP
6699 packet_out,
6700 set_netflow,
6701 get_netflow_ids,
6702 set_sflow,
29089a54 6703 set_ipfix,
abe529af 6704 set_cfm,
9a9e3786 6705 get_cfm_status,
ccc09689
EJ
6706 set_bfd,
6707 get_bfd_status,
21f7563c
JP
6708 set_stp,
6709 get_stp_status,
6710 set_stp_port,
6711 get_stp_port_status,
8b36f51e 6712 set_queues,
abe529af
BP
6713 bundle_set,
6714 bundle_remove,
ec7ceaed
EJ
6715 mirror_set__,
6716 mirror_get_stats__,
abe529af
BP
6717 set_flood_vlans,
6718 is_mirror_output_bundle,
8402c74b 6719 forward_bpdu_changed,
c4069512 6720 set_mac_table_config,
52a90c29 6721 set_realdev,
9cae45dc
JR
6722 NULL, /* meter_get_features */
6723 NULL, /* meter_set */
6724 NULL, /* meter_get */
6725 NULL, /* meter_del */
abe529af 6726};