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