]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto.c
ofproto: Delete all groups and meters when (un)configuring a controller.
[mirror_ovs.git] / ofproto / ofproto.c
CommitLineData
064af421 1/*
04f48a68 2 * Copyright (c) 2009-2017 Nicira, Inc.
43253595 3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
064af421 4 *
a14bc59f
BP
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
064af421 8 *
a14bc59f
BP
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
064af421
BP
16 */
17
18#include <config.h>
064af421
BP
19#include <errno.h>
20#include <inttypes.h>
064af421
BP
21#include <stdbool.h>
22#include <stdlib.h>
448a4b2f 23#include <unistd.h>
b598f214 24
52a90c29 25#include "bitmap.h"
b598f214 26#include "bundles.h"
10a24935 27#include "byte-order.h"
064af421 28#include "classifier.h"
da4a6191 29#include "connectivity.h"
19a87e36 30#include "connmgr.h"
064af421 31#include "coverage.h"
b598f214 32#include "dp-packet.h"
ca0f572c 33#include "hash.h"
ee89ea7b 34#include "openvswitch/hmap.h"
064af421 35#include "netdev.h"
09246b99 36#include "nx-match.h"
b598f214 37#include "ofproto.h"
5bee6e26 38#include "ofproto-provider.h"
064af421
BP
39#include "openflow/nicira-ext.h"
40#include "openflow/openflow.h"
d271907f
BW
41#include "openvswitch/dynamic-string.h"
42#include "openvswitch/meta-flow.h"
b598f214 43#include "openvswitch/ofp-actions.h"
d271907f
BW
44#include "openvswitch/ofp-errors.h"
45#include "openvswitch/ofp-msgs.h"
25d436fb 46#include "openvswitch/ofp-print.h"
d271907f
BW
47#include "openvswitch/ofp-util.h"
48#include "openvswitch/ofpbuf.h"
49#include "openvswitch/vlog.h"
06a0f3e2 50#include "ovs-rcu.h"
064af421
BP
51#include "packets.h"
52#include "pinsched.h"
fd016ae3 53#include "openvswitch/poll-loop.h"
254750ce 54#include "random.h"
da4a6191 55#include "seq.h"
ee89ea7b 56#include "openvswitch/shash.h"
0d085684 57#include "simap.h"
e8f9a7bb 58#include "smap.h"
b3c01ed3 59#include "sset.h"
064af421 60#include "timeval.h"
9558d2a5 61#include "tun-metadata.h"
c4617b3c 62#include "unaligned.h"
4f2cad2c 63#include "unixctl.h"
ee89ea7b 64#include "util.h"
064af421 65
d98e6007 66VLOG_DEFINE_THIS_MODULE(ofproto);
064af421 67
d76f09ea 68COVERAGE_DEFINE(ofproto_flush);
d76f09ea
BP
69COVERAGE_DEFINE(ofproto_packet_out);
70COVERAGE_DEFINE(ofproto_queue_req);
71COVERAGE_DEFINE(ofproto_recv_openflow);
72COVERAGE_DEFINE(ofproto_reinit_ports);
d76f09ea
BP
73COVERAGE_DEFINE(ofproto_update_port);
74
f017d986
JR
75/* Default fields to use for prefix tries in each flow table, unless something
76 * else is configured. */
77const enum mf_field_id default_prefix_fields[2] =
78 { MFF_IPV4_DST, MFF_IPV4_SRC };
79
d0918789
BP
80/* oftable. */
81static void oftable_init(struct oftable *);
82static void oftable_destroy(struct oftable *);
878ae780 83
254750ce
BP
84static void oftable_set_name(struct oftable *, const char *name);
85
6787a49f 86static enum ofperr evict_rules_from_table(struct oftable *)
834fe5cb 87 OVS_REQUIRES(ofproto_mutex);
82c22d34
BP
88static void oftable_configure_eviction(struct oftable *,
89 unsigned int eviction,
90 const struct mf_subfield *fields,
91 size_t n_fields)
7394b8fb 92 OVS_REQUIRES(ofproto_mutex);
254750ce 93
82c22d34
BP
94/* This is the only combination of OpenFlow eviction flags that OVS supports: a
95 * combination of OF1.4+ importance, the remaining lifetime of the flow, and
96 * fairness based on user-specified fields. */
97#define OFPROTO_EVICTION_FLAGS \
98 (OFPTMPEF14_OTHER | OFPTMPEF14_IMPORTANCE | OFPTMPEF14_LIFETIME)
99
254750ce
BP
100/* A set of rules within a single OpenFlow table (oftable) that have the same
101 * values for the oftable's eviction_fields. A rule to be evicted, when one is
102 * needed, is taken from the eviction group that contains the greatest number
103 * of rules.
104 *
105 * An oftable owns any number of eviction groups, each of which contains any
106 * number of rules.
107 *
108 * Membership in an eviction group is imprecise, based on the hash of the
109 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
110 * That is, if two rules have different eviction_fields, but those
111 * eviction_fields hash to the same value, then they will belong to the same
112 * eviction_group anyway.
113 *
114 * (When eviction is not enabled on an oftable, we don't track any eviction
115 * groups, to save time and space.) */
116struct eviction_group {
117 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
118 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
119 struct heap rules; /* Contains "struct rule"s. */
120};
121
3d900aa7
BP
122static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
123 OVS_REQUIRES(ofproto_mutex);
f70b94de
BP
124static uint64_t rule_eviction_priority(struct ofproto *ofproto, struct rule *)
125 OVS_REQUIRES(ofproto_mutex);
3d900aa7
BP
126static void eviction_group_add_rule(struct rule *)
127 OVS_REQUIRES(ofproto_mutex);
128static void eviction_group_remove_rule(struct rule *)
129 OVS_REQUIRES(ofproto_mutex);
f29152ca 130
a9b22b7f 131static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
eb391b76 132 const struct match *match, int priority,
44e0c35d 133 ovs_version_t version,
a9b22b7f
BP
134 ovs_be64 cookie, ovs_be64 cookie_mask,
135 ofp_port_t out_port, uint32_t out_group);
dd51dae2
BP
136static void rule_criteria_require_rw(struct rule_criteria *,
137 bool can_write_readonly);
a9b22b7f
BP
138static void rule_criteria_destroy(struct rule_criteria *);
139
35f48b8b
BP
140static enum ofperr collect_rules_loose(struct ofproto *,
141 const struct rule_criteria *,
142 struct rule_collection *);
143
35f48b8b
BP
144struct learned_cookie {
145 union {
146 /* In struct ofproto's 'learned_cookies' hmap. */
147 struct hmap_node hmap_node OVS_GUARDED_BY(ofproto_mutex);
148
149 /* In 'dead_cookies' list when removed from hmap. */
ca6ba700 150 struct ovs_list list_node;
35f48b8b
BP
151 } u;
152
153 /* Key. */
154 ovs_be64 cookie OVS_GUARDED_BY(ofproto_mutex);
155 uint8_t table_id OVS_GUARDED_BY(ofproto_mutex);
156
157 /* Number of references from "learn" actions.
158 *
159 * When this drops to 0, all of the flows in 'table_id' with the specified
160 * 'cookie' are deleted. */
161 int n OVS_GUARDED_BY(ofproto_mutex);
162};
163
164static const struct ofpact_learn *next_learn_with_delete(
165 const struct rule_actions *, const struct ofpact_learn *start);
166
167static void learned_cookies_inc(struct ofproto *, const struct rule_actions *)
168 OVS_REQUIRES(ofproto_mutex);
169static void learned_cookies_dec(struct ofproto *, const struct rule_actions *,
ca6ba700 170 struct ovs_list *dead_cookies)
35f48b8b 171 OVS_REQUIRES(ofproto_mutex);
ca6ba700 172static void learned_cookies_flush(struct ofproto *, struct ovs_list *dead_cookies)
35f48b8b
BP
173 OVS_REQUIRES(ofproto_mutex);
174
d0918789 175/* ofport. */
15aaf599 176static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex);
9aad5a5a 177static void ofport_destroy(struct ofport *, bool del);
3a414a0a
DDP
178static bool ofport_is_mtu_overridden(const struct ofproto *,
179 const struct ofport *);
d0918789 180
01c77073 181static int update_port(struct ofproto *, const char *devname);
d0918789
BP
182static int init_ports(struct ofproto *);
183static void reinit_ports(struct ofproto *);
7ee20df1 184
fdcea803
GS
185static long long int ofport_get_usage(const struct ofproto *,
186 ofp_port_t ofp_port);
187static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port,
188 long long int last_used);
865b26a4 189static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port);
fdcea803
GS
190
191/* Ofport usage.
192 *
193 * Keeps track of the currently used and recently used ofport values and is
194 * used to prevent immediate recycling of ofport values. */
195struct ofport_usage {
196 struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */
197 ofp_port_t ofp_port; /* OpenFlow port number. */
198 long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX
199 represents in-use ofports. */
200};
201
254750ce 202/* rule. */
f695ebfa
JR
203static void ofproto_rule_send_removed(struct rule *)
204 OVS_EXCLUDED(ofproto_mutex);
834fe5cb 205static bool rule_is_readonly(const struct rule *);
bc5e6a91
JR
206static void ofproto_rule_insert__(struct ofproto *, struct rule *)
207 OVS_REQUIRES(ofproto_mutex);
802f84ff
JR
208static void ofproto_rule_remove__(struct ofproto *, struct rule *)
209 OVS_REQUIRES(ofproto_mutex);
254750ce 210
0a0d9385 211/* The source of an OpenFlow request.
baae3d02 212 *
0a0d9385
JR
213 * A table modification request can be generated externally, via OpenFlow, or
214 * internally through a function call. This structure indicates the source of
215 * an OpenFlow-generated table modification. For an internal flow_mod, it
216 * isn't meaningful and thus supplied as NULL. */
217struct openflow_mod_requester {
baae3d02 218 struct ofconn *ofconn; /* Connection on which flow_mod arrived. */
c84d8691 219 const struct ofp_header *request;
baae3d02
BP
220};
221
d0918789 222/* OpenFlow. */
5bacd5cd
JR
223static enum ofperr ofproto_rule_create(struct ofproto *, struct cls_rule *,
224 uint8_t table_id, ovs_be64 new_cookie,
225 uint16_t idle_timeout,
226 uint16_t hard_timeout,
227 enum ofputil_flow_mod_flags flags,
228 uint16_t importance,
229 const struct ofpact *ofpacts,
230 size_t ofpacts_len,
5c7c16d8
YHW
231 uint64_t match_tlv_bitmap,
232 uint64_t ofpacts_tlv_bitmap,
39c94593 233 struct rule **new_rule)
5bacd5cd 234 OVS_NO_THREAD_SAFETY_ANALYSIS;
39c94593 235
5bacd5cd
JR
236static void replace_rule_start(struct ofproto *, struct ofproto_flow_mod *,
237 struct rule *old_rule, struct rule *new_rule)
dd27be82 238 OVS_REQUIRES(ofproto_mutex);
39c94593 239
6787a49f
JR
240static void replace_rule_revert(struct ofproto *, struct rule *old_rule,
241 struct rule *new_rule)
39c94593
JR
242 OVS_REQUIRES(ofproto_mutex);
243
81dee635 244static void replace_rule_finish(struct ofproto *, struct ofproto_flow_mod *,
0a0d9385 245 const struct openflow_mod_requester *,
39c94593
JR
246 struct rule *old_rule, struct rule *new_rule,
247 struct ovs_list *dead_cookies)
dd27be82 248 OVS_REQUIRES(ofproto_mutex);
39c94593 249static void delete_flows__(struct rule_collection *,
9ca4a86f 250 enum ofp_flow_removed_reason,
0a0d9385 251 const struct openflow_mod_requester *)
15aaf599 252 OVS_REQUIRES(ofproto_mutex);
834fe5cb 253
d3b84833
BP
254static void ofproto_group_delete_all__(struct ofproto *)
255 OVS_REQUIRES(ofproto_mutex);
db88b35c 256static bool ofproto_group_exists(const struct ofproto *, uint32_t group_id);
b20f4073 257static void handle_openflow(struct ofconn *, const struct ofpbuf *);
5bacd5cd
JR
258static enum ofperr ofproto_flow_mod_init(struct ofproto *,
259 struct ofproto_flow_mod *,
2c7ee524
JR
260 const struct ofputil_flow_mod *fm,
261 struct rule *)
5bacd5cd 262 OVS_EXCLUDED(ofproto_mutex);
8be00367
JR
263static enum ofperr ofproto_flow_mod_start(struct ofproto *,
264 struct ofproto_flow_mod *)
1f42be1c 265 OVS_REQUIRES(ofproto_mutex);
6dd3c787
JR
266static void ofproto_flow_mod_revert(struct ofproto *,
267 struct ofproto_flow_mod *)
268 OVS_REQUIRES(ofproto_mutex);
8be00367
JR
269static void ofproto_flow_mod_finish(struct ofproto *,
270 struct ofproto_flow_mod *,
0a0d9385 271 const struct openflow_mod_requester *)
1f42be1c 272 OVS_REQUIRES(ofproto_mutex);
baae3d02 273static enum ofperr handle_flow_mod__(struct ofproto *,
7338102b 274 const struct ofputil_flow_mod *,
0a0d9385 275 const struct openflow_mod_requester *)
15aaf599 276 OVS_EXCLUDED(ofproto_mutex);
65e0be10
BP
277static void calc_duration(long long int start, long long int now,
278 uint32_t *sec, uint32_t *nsec);
f29152ca 279
d0918789
BP
280/* ofproto. */
281static uint64_t pick_datapath_id(const struct ofproto *);
282static uint64_t pick_fallback_dpid(void);
283static void ofproto_destroy__(struct ofproto *);
ada3428f 284static void update_mtu(struct ofproto *, struct ofport *);
0670b5d0 285static void update_mtu_ofproto(struct ofproto *);
ba8cff36
AZ
286static void meter_delete(struct ofproto *, uint32_t);
287static void meter_delete_all(struct ofproto *);
062fea06 288static void meter_insert_rule(struct rule *);
f29152ca 289
d0918789 290/* unixctl. */
abe529af 291static void ofproto_unixctl_init(void);
f29152ca 292
abe529af
BP
293/* All registered ofproto classes, in probe order. */
294static const struct ofproto_class **ofproto_classes;
295static size_t n_ofproto_classes;
296static size_t allocated_ofproto_classes;
7aa697dd 297
15aaf599
BP
298/* Global lock that protects all flow table operations. */
299struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER;
abe7b10f 300
e79a6c83 301unsigned ofproto_flow_limit = OFPROTO_FLOW_LIMIT_DEFAULT;
72310b04 302unsigned ofproto_max_idle = OFPROTO_MAX_IDLE_DEFAULT;
380f49c4 303
e79a6c83 304size_t n_handlers, n_revalidators;
6567010f 305
f797957a 306/* Map from datapath name to struct ofproto, for use by unixctl commands. */
abe529af 307static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
ebe482fd 308
e1b1d06a
JP
309/* Initial mappings of port to OpenFlow number mappings. */
310static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
311
abe529af 312static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
f29152ca 313
40358701
GS
314/* The default value of true waits for flow restore. */
315static bool flow_restore_wait = true;
316
b0408fca
JP
317/* Must be called to initialize the ofproto library.
318 *
319 * The caller may pass in 'iface_hints', which contains an shash of
320 * "iface_hint" elements indexed by the interface's name. The provider
321 * may use these hints to describe the startup configuration in order to
322 * reinitialize its state. The caller owns the provided data, so a
323 * provider will make copies of anything required. An ofproto provider
324 * will remove any existing state that is not described by the hint, and
325 * may choose to remove it all. */
326void
327ofproto_init(const struct shash *iface_hints)
abe529af 328{
e1b1d06a 329 struct shash_node *node;
b0408fca 330 size_t i;
f29152ca 331
b0408fca
JP
332 ofproto_class_register(&ofproto_dpif_class);
333
e1b1d06a
JP
334 /* Make a local copy, since we don't own 'iface_hints' elements. */
335 SHASH_FOR_EACH(node, iface_hints) {
336 const struct iface_hint *orig_hint = node->data;
337 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
338 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
339
340 new_hint->br_name = xstrdup(orig_hint->br_name);
341 new_hint->br_type = xstrdup(br_type);
342 new_hint->ofp_port = orig_hint->ofp_port;
343
344 shash_add(&init_ofp_ports, node->name, new_hint);
345 }
346
b0408fca 347 for (i = 0; i < n_ofproto_classes; i++) {
e1b1d06a 348 ofproto_classes[i]->init(&init_ofp_ports);
abe529af 349 }
0fc1f5c0
HH
350
351 ofproto_unixctl_init();
abe529af 352}
f29152ca 353
abe529af
BP
354/* 'type' should be a normalized datapath type, as returned by
355 * ofproto_normalize_type(). Returns the corresponding ofproto_class
356 * structure, or a null pointer if there is none registered for 'type'. */
357static const struct ofproto_class *
358ofproto_class_find__(const char *type)
359{
360 size_t i;
f29152ca 361
abe529af
BP
362 for (i = 0; i < n_ofproto_classes; i++) {
363 const struct ofproto_class *class = ofproto_classes[i];
364 struct sset types;
365 bool found;
064af421 366
abe529af
BP
367 sset_init(&types);
368 class->enumerate_types(&types);
369 found = sset_contains(&types, type);
370 sset_destroy(&types);
bcf84111 371
abe529af
BP
372 if (found) {
373 return class;
374 }
375 }
376 VLOG_WARN("unknown datapath type %s", type);
377 return NULL;
378}
064af421 379
abe529af
BP
380/* Registers a new ofproto class. After successful registration, new ofprotos
381 * of that type can be created using ofproto_create(). */
382int
383ofproto_class_register(const struct ofproto_class *new_class)
384{
385 size_t i;
7aa697dd 386
abe529af
BP
387 for (i = 0; i < n_ofproto_classes; i++) {
388 if (ofproto_classes[i] == new_class) {
389 return EEXIST;
390 }
391 }
064af421 392
abe529af
BP
393 if (n_ofproto_classes >= allocated_ofproto_classes) {
394 ofproto_classes = x2nrealloc(ofproto_classes,
395 &allocated_ofproto_classes,
396 sizeof *ofproto_classes);
397 }
398 ofproto_classes[n_ofproto_classes++] = new_class;
399 return 0;
400}
064af421 401
abe529af
BP
402/* Unregisters a datapath provider. 'type' must have been previously
403 * registered and not currently be in use by any ofprotos. After
404 * unregistration new datapaths of that type cannot be opened using
405 * ofproto_create(). */
406int
407ofproto_class_unregister(const struct ofproto_class *class)
408{
409 size_t i;
76ce9432 410
abe529af
BP
411 for (i = 0; i < n_ofproto_classes; i++) {
412 if (ofproto_classes[i] == class) {
413 for (i++; i < n_ofproto_classes; i++) {
414 ofproto_classes[i - 1] = ofproto_classes[i];
415 }
416 n_ofproto_classes--;
417 return 0;
418 }
419 }
420 VLOG_WARN("attempted to unregister an ofproto class that is not "
421 "registered");
422 return EAFNOSUPPORT;
423}
4a4cdb3b 424
f79e673f
BP
425/* Clears 'types' and enumerates all registered ofproto types into it. The
426 * caller must first initialize the sset. */
427void
428ofproto_enumerate_types(struct sset *types)
429{
abe529af 430 size_t i;
064af421 431
c799c306 432 sset_clear(types);
abe529af
BP
433 for (i = 0; i < n_ofproto_classes; i++) {
434 ofproto_classes[i]->enumerate_types(types);
435 }
f79e673f 436}
064af421 437
f79e673f
BP
438/* Returns the fully spelled out name for the given ofproto 'type'.
439 *
440 * Normalized type string can be compared with strcmp(). Unnormalized type
441 * string might be the same even if they have different spellings. */
442const char *
443ofproto_normalize_type(const char *type)
444{
abe529af 445 return type && type[0] ? type : "system";
f79e673f 446}
064af421 447
f79e673f
BP
448/* Clears 'names' and enumerates the names of all known created ofprotos with
449 * the given 'type'. The caller must first initialize the sset. Returns 0 if
450 * successful, otherwise a positive errno value.
451 *
452 * Some kinds of datapaths might not be practically enumerable. This is not
453 * considered an error. */
454int
455ofproto_enumerate_names(const char *type, struct sset *names)
456{
abe529af
BP
457 const struct ofproto_class *class = ofproto_class_find__(type);
458 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
239ad7d1 459}
7aa697dd 460
39c94593
JR
461static void
462ofproto_bump_tables_version(struct ofproto *ofproto)
463{
464 ++ofproto->tables_version;
465 ofproto->ofproto_class->set_tables_version(ofproto,
466 ofproto->tables_version);
467}
468
064af421 469int
abe529af 470ofproto_create(const char *datapath_name, const char *datapath_type,
064af421 471 struct ofproto **ofprotop)
25010c68 472 OVS_EXCLUDED(ofproto_mutex)
064af421 473{
abe529af
BP
474 const struct ofproto_class *class;
475 struct ofproto *ofproto;
064af421 476 int error;
c2f0373a 477 int i;
064af421
BP
478
479 *ofprotop = NULL;
480
abe529af
BP
481 datapath_type = ofproto_normalize_type(datapath_type);
482 class = ofproto_class_find__(datapath_type);
483 if (!class) {
484 VLOG_WARN("could not create datapath %s of unknown type %s",
485 datapath_name, datapath_type);
486 return EAFNOSUPPORT;
064af421
BP
487 }
488
abe529af
BP
489 ofproto = class->alloc();
490 if (!ofproto) {
491 VLOG_ERR("failed to allocate datapath %s of type %s",
492 datapath_name, datapath_type);
493 return ENOMEM;
494 }
495
496 /* Initialize. */
abe7b10f 497 ovs_mutex_lock(&ofproto_mutex);
abe529af
BP
498 memset(ofproto, 0, sizeof *ofproto);
499 ofproto->ofproto_class = class;
500 ofproto->name = xstrdup(datapath_name);
501 ofproto->type = xstrdup(datapath_type);
502 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
503 hash_string(ofproto->name, 0));
504 ofproto->datapath_id = 0;
8402c74b 505 ofproto->forward_bpdu = false;
abe529af 506 ofproto->fallback_dpid = pick_fallback_dpid();
061bfea4
BP
507 ofproto->mfr_desc = NULL;
508 ofproto->hw_desc = NULL;
509 ofproto->sw_desc = NULL;
510 ofproto->serial_desc = NULL;
511 ofproto->dp_desc = NULL;
ad99e2ed 512 ofproto->frag_handling = OFPUTIL_FRAG_NORMAL;
abe529af 513 hmap_init(&ofproto->ports);
fdcea803 514 hmap_init(&ofproto->ofport_usage);
abe529af 515 shash_init(&ofproto->port_by_name);
e1b1d06a 516 simap_init(&ofproto->ofp_requests);
430dbb14 517 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
448c2fa8 518 ofproto->eviction_group_timer = LLONG_MIN;
6c1491fb
BP
519 ofproto->tables = NULL;
520 ofproto->n_tables = 0;
44e0c35d 521 ofproto->tables_version = OVS_VERSION_MIN;
98eaac36 522 hindex_init(&ofproto->cookies);
35f48b8b 523 hmap_init(&ofproto->learned_cookies);
417e7e66 524 ovs_list_init(&ofproto->expirable);
abe529af 525 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
7478b5a2 526 ofproto->min_mtu = INT_MAX;
db88b35c 527 cmap_init(&ofproto->groups);
abe7b10f 528 ovs_mutex_unlock(&ofproto_mutex);
0ae01c64 529 ofproto->ogf.types = 0xf;
7cb279c2
SH
530 ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS |
531 OFPGFC_SELECT_WEIGHT;
08d1e234
BP
532 for (i = 0; i < 4; i++) {
533 ofproto->ogf.max_groups[i] = OFPG_MAX;
534 ofproto->ogf.ofpacts[i] = (UINT64_C(1) << N_OFPACTS) - 1;
535 }
8d8ab6c2 536 ovsrcu_set(&ofproto->metadata_tab, tun_metadata_alloc(NULL));
abe529af 537
04f48a68
YHW
538 ovs_mutex_init(&ofproto->vl_mff_map.mutex);
539 cmap_init(&ofproto->vl_mff_map.cmap);
540
0f5f95a9 541 error = ofproto->ofproto_class->construct(ofproto);
19a87e36 542 if (error) {
abe529af 543 VLOG_ERR("failed to open datapath %s: %s",
10a89ef0 544 datapath_name, ovs_strerror(error));
25010c68 545 ovs_mutex_lock(&ofproto_mutex);
58f19539 546 connmgr_destroy(ofproto->connmgr);
25010c68
JR
547 ofproto->connmgr = NULL;
548 ovs_mutex_unlock(&ofproto_mutex);
abe529af 549 ofproto_destroy__(ofproto);
19a87e36
BP
550 return error;
551 }
073e2a6f 552
c2f0373a 553 /* Check that hidden tables, if any, are at the end. */
cb22974d 554 ovs_assert(ofproto->n_tables);
c2f0373a
BP
555 for (i = 0; i + 1 < ofproto->n_tables; i++) {
556 enum oftable_flags flags = ofproto->tables[i].flags;
557 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
558
cb22974d 559 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
c2f0373a 560 }
19a87e36 561
abe529af 562 ofproto->datapath_id = pick_datapath_id(ofproto);
abe529af 563 init_ports(ofproto);
7aa697dd 564
9cae45dc
JR
565 /* Initialize meters table. */
566 if (ofproto->ofproto_class->meter_get_features) {
567 ofproto->ofproto_class->meter_get_features(ofproto,
568 &ofproto->meter_features);
569 } else {
570 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
571 }
ba8cff36 572 hmap_init(&ofproto->meters);
9e638f22
AZ
573 ofproto->slowpath_meter_id = UINT32_MAX;
574 ofproto->controller_meter_id = UINT32_MAX;
9cae45dc 575
621b8064 576 /* Set the initial tables version. */
39c94593 577 ofproto_bump_tables_version(ofproto);
621b8064 578
abe529af 579 *ofprotop = ofproto;
064af421
BP
580 return 0;
581}
582
91858960
BP
583/* Must be called (only) by an ofproto implementation in its constructor
584 * function. See the large comment on 'construct' in struct ofproto_class for
585 * details. */
0f5f95a9
BP
586void
587ofproto_init_tables(struct ofproto *ofproto, int n_tables)
588{
589 struct oftable *table;
590
cb22974d
BP
591 ovs_assert(!ofproto->n_tables);
592 ovs_assert(n_tables >= 1 && n_tables <= 255);
0f5f95a9
BP
593
594 ofproto->n_tables = n_tables;
595 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
596 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
597 oftable_init(table);
598 }
599}
600
91858960
BP
601/* To be optionally called (only) by an ofproto implementation in its
602 * constructor function. See the large comment on 'construct' in struct
603 * ofproto_class for details.
604 *
605 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
606 * will then ensure that actions passed into the ofproto implementation will
607 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
608 * function is not called, there will be no such restriction.
609 *
610 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
611 * the 'max_ports' restriction. */
612void
430dbb14 613ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
91858960 614{
430dbb14 615 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
91858960
BP
616 ofproto->max_ports = max_ports;
617}
618
e825ace2
BP
619uint64_t
620ofproto_get_datapath_id(const struct ofproto *ofproto)
621{
622 return ofproto->datapath_id;
623}
624
064af421
BP
625void
626ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
627{
628 uint64_t old_dpid = p->datapath_id;
fa60c019 629 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
064af421 630 if (p->datapath_id != old_dpid) {
76ce9432
BP
631 /* Force all active connections to reconnect, since there is no way to
632 * notify a controller that the datapath ID has changed. */
fa05809b 633 ofproto_reconnect_controllers(p);
064af421
BP
634 }
635}
636
76ce9432
BP
637void
638ofproto_set_controllers(struct ofproto *p,
639 const struct ofproto_controller *controllers,
1d9ffc17 640 size_t n_controllers, uint32_t allowed_versions)
76ce9432 641{
1d9ffc17
SH
642 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
643 allowed_versions);
064af421
BP
644}
645
31681a5d
JP
646void
647ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
648{
19a87e36 649 connmgr_set_fail_mode(p->connmgr, fail_mode);
31681a5d
JP
650}
651
fa05809b
BP
652/* Drops the connections between 'ofproto' and all of its controllers, forcing
653 * them to reconnect. */
654void
655ofproto_reconnect_controllers(struct ofproto *ofproto)
656{
19a87e36 657 connmgr_reconnect(ofproto->connmgr);
917e50e1
BP
658}
659
660/* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
661 * in-band control should guarantee access, in the same way that in-band
662 * control guarantees access to OpenFlow controllers. */
663void
664ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
665 const struct sockaddr_in *extras, size_t n)
666{
19a87e36 667 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
917e50e1
BP
668}
669
b1da6250
BP
670/* Sets the OpenFlow queue used by flows set up by in-band control on
671 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
672 * flows will use the default queue. */
673void
674ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
675{
19a87e36 676 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
b1da6250
BP
677}
678
084f5290
SH
679/* Sets the number of flows at which eviction from the kernel flow table
680 * will occur. */
681void
e79a6c83 682ofproto_set_flow_limit(unsigned limit)
084f5290 683{
e79a6c83 684 ofproto_flow_limit = limit;
084f5290
SH
685}
686
72310b04
JS
687/* Sets the maximum idle time for flows in the datapath before they are
688 * expired. */
689void
690ofproto_set_max_idle(unsigned max_idle)
691{
692 ofproto_max_idle = max_idle;
693}
694
b53055f4 695/* If forward_bpdu is true, the NORMAL action will forward frames with
8402c74b
SS
696 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
697 * the NORMAL action will drop these frames. */
698void
699ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
700{
701 bool old_val = ofproto->forward_bpdu;
702 ofproto->forward_bpdu = forward_bpdu;
703 if (old_val != ofproto->forward_bpdu) {
704 if (ofproto->ofproto_class->forward_bpdu_changed) {
705 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
706 }
b53055f4 707 }
8402c74b
SS
708}
709
e764773c 710/* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
c4069512
BP
711 * 'idle_time', in seconds, and the maximum number of MAC table entries to
712 * 'max_entries'. */
e764773c 713void
c4069512
BP
714ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
715 size_t max_entries)
e764773c 716{
c4069512
BP
717 if (ofproto->ofproto_class->set_mac_table_config) {
718 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
719 max_entries);
e764773c
BP
720 }
721}
722
7c38d0a5
FL
723/* Multicast snooping configuration. */
724
725/* Configures multicast snooping on 'ofproto' using the settings
726 * defined in 's'. If 's' is NULL, disables multicast snooping.
727 *
728 * Returns 0 if successful, otherwise a positive errno value. */
729int
730ofproto_set_mcast_snooping(struct ofproto *ofproto,
731 const struct ofproto_mcast_snooping_settings *s)
732{
733 return (ofproto->ofproto_class->set_mcast_snooping
734 ? ofproto->ofproto_class->set_mcast_snooping(ofproto, s)
735 : EOPNOTSUPP);
736}
737
8e04a33f 738/* Configures multicast snooping flood settings on 'ofp_port' of 'ofproto'.
7c38d0a5
FL
739 *
740 * Returns 0 if successful, otherwise a positive errno value.*/
741int
8e04a33f
FL
742ofproto_port_set_mcast_snooping(struct ofproto *ofproto, void *aux,
743 const struct ofproto_mcast_snooping_port_settings *s)
7c38d0a5
FL
744{
745 return (ofproto->ofproto_class->set_mcast_snooping_port
8e04a33f 746 ? ofproto->ofproto_class->set_mcast_snooping_port(ofproto, aux, s)
7c38d0a5
FL
747 : EOPNOTSUPP);
748}
749
f2eee189 750void
d4f6865c 751ofproto_type_set_config(const char *datapath_type, const struct smap *cfg)
f2eee189 752{
d4f6865c
DDP
753 const struct ofproto_class *class;
754
755 datapath_type = ofproto_normalize_type(datapath_type);
756 class = ofproto_class_find__(datapath_type);
757
758 if (class->type_set_config) {
759 class->type_set_config(datapath_type, cfg);
760 }
f2eee189
AW
761}
762
448a4b2f 763void
30ea0da7 764ofproto_set_threads(int n_handlers_, int n_revalidators_)
448a4b2f 765{
e79a6c83
EJ
766 int threads = MAX(count_cpu_cores(), 2);
767
30ea0da7
GS
768 n_revalidators = MAX(n_revalidators_, 0);
769 n_handlers = MAX(n_handlers_, 0);
e79a6c83
EJ
770
771 if (!n_revalidators) {
772 n_revalidators = n_handlers
773 ? MAX(threads - (int) n_handlers, 1)
774 : threads / 4 + 1;
775 }
776
777 if (!n_handlers) {
778 n_handlers = MAX(threads - (int) n_revalidators, 1);
779 }
448a4b2f
AW
780}
781
8b6ff729
BP
782void
783ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
784{
785 free(p->dp_desc);
2225c0b9 786 p->dp_desc = nullable_xstrdup(dp_desc);
8b6ff729
BP
787}
788
064af421 789int
81e2083f 790ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
064af421 791{
19a87e36 792 return connmgr_set_snoops(ofproto->connmgr, snoops);
064af421
BP
793}
794
795int
0193b2af
JG
796ofproto_set_netflow(struct ofproto *ofproto,
797 const struct netflow_options *nf_options)
064af421 798{
abe529af
BP
799 if (nf_options && sset_is_empty(&nf_options->collectors)) {
800 nf_options = NULL;
801 }
802
803 if (ofproto->ofproto_class->set_netflow) {
804 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
064af421 805 } else {
abe529af 806 return nf_options ? EOPNOTSUPP : 0;
064af421
BP
807 }
808}
809
abe529af 810int
72b06300
BP
811ofproto_set_sflow(struct ofproto *ofproto,
812 const struct ofproto_sflow_options *oso)
813{
abe529af
BP
814 if (oso && sset_is_empty(&oso->targets)) {
815 oso = NULL;
816 }
72b06300 817
abe529af
BP
818 if (ofproto->ofproto_class->set_sflow) {
819 return ofproto->ofproto_class->set_sflow(ofproto, oso);
72b06300 820 } else {
abe529af 821 return oso ? EOPNOTSUPP : 0;
72b06300
BP
822 }
823}
29089a54
RL
824
825int
826ofproto_set_ipfix(struct ofproto *ofproto,
827 const struct ofproto_ipfix_bridge_exporter_options *bo,
828 const struct ofproto_ipfix_flow_exporter_options *fo,
829 size_t n_fo)
830{
831 if (ofproto->ofproto_class->set_ipfix) {
832 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
833 } else {
834 return (bo || fo) ? EOPNOTSUPP : 0;
835 }
836}
40358701 837
fb8f22c1
BY
838static int
839ofproto_get_ipfix_stats(struct ofproto *ofproto,
840 bool bridge_ipfix,
841 struct ovs_list *replies)
842{
843 int error;
844
845 if (ofproto->ofproto_class->get_ipfix_stats) {
846 error = ofproto->ofproto_class->get_ipfix_stats(ofproto,
847 bridge_ipfix,
848 replies);
849 } else {
850 error = EOPNOTSUPP;
851 }
852
853 return error;
854}
855
856static enum ofperr
857handle_ipfix_bridge_stats_request(struct ofconn *ofconn,
858 const struct ofp_header *request)
859{
860 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
861 struct ovs_list replies;
862 enum ofperr error;
863
864 ofpmp_init(&replies, request);
865 error = ofproto_get_ipfix_stats(ofproto, true, &replies);
866
867 if (!error) {
868 ofconn_send_replies(ofconn, &replies);
869 } else {
870 ofpbuf_list_delete(&replies);
871 }
872
873 return error;
874}
875
876static enum ofperr
877handle_ipfix_flow_stats_request(struct ofconn *ofconn,
878 const struct ofp_header *request)
879{
880 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
881 struct ovs_list replies;
882 enum ofperr error;
883
884 ofpmp_init(&replies, request);
885 error = ofproto_get_ipfix_stats(ofproto, false, &replies);
886
887 if (!error) {
888 ofconn_send_replies(ofconn, &replies);
889 } else {
890 ofpbuf_list_delete(&replies);
891 }
892
893 return error;
894}
895
2a7c4805
JP
896static enum ofperr
897handle_nxt_ct_flush_zone(struct ofconn *ofconn, const struct ofp_header *oh)
898{
899 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
900 const struct nx_zone_id *nzi = ofpmsg_body(oh);
901
902 if (!is_all_zeros(nzi->zero, sizeof nzi->zero)) {
903 return OFPERR_NXBRC_MUST_BE_ZERO;
904 }
905
906 uint16_t zone = ntohs(nzi->zone_id);
907 if (ofproto->ofproto_class->ct_flush) {
908 ofproto->ofproto_class->ct_flush(ofproto, &zone);
909 } else {
910 return EOPNOTSUPP;
911 }
912
913 return 0;
914}
915
40358701
GS
916void
917ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
918{
919 flow_restore_wait = flow_restore_wait_db;
920}
921
922bool
923ofproto_get_flow_restore_wait(void)
924{
925 return flow_restore_wait;
926}
927
e7934396 928\f
21f7563c
JP
929/* Spanning Tree Protocol (STP) configuration. */
930
931/* Configures STP on 'ofproto' using the settings defined in 's'. If
932 * 's' is NULL, disables STP.
933 *
934 * Returns 0 if successful, otherwise a positive errno value. */
935int
936ofproto_set_stp(struct ofproto *ofproto,
937 const struct ofproto_stp_settings *s)
938{
939 return (ofproto->ofproto_class->set_stp
940 ? ofproto->ofproto_class->set_stp(ofproto, s)
941 : EOPNOTSUPP);
942}
943
944/* Retrieves STP status of 'ofproto' and stores it in 's'. If the
945 * 'enabled' member of 's' is false, then the other members are not
946 * meaningful.
947 *
948 * Returns 0 if successful, otherwise a positive errno value. */
949int
950ofproto_get_stp_status(struct ofproto *ofproto,
951 struct ofproto_stp_status *s)
952{
953 return (ofproto->ofproto_class->get_stp_status
954 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
955 : EOPNOTSUPP);
956}
957
958/* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
959 * in 's'. The caller is responsible for assigning STP port numbers
960 * (using the 'port_num' member in the range of 1 through 255, inclusive)
961 * and ensuring there are no duplicates. If the 's' is NULL, then STP
962 * is disabled on the port.
963 *
964 * Returns 0 if successful, otherwise a positive errno value.*/
965int
4e022ec0 966ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
21f7563c
JP
967 const struct ofproto_port_stp_settings *s)
968{
969 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
970 if (!ofport) {
94783c7c 971 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu32,
21f7563c
JP
972 ofproto->name, ofp_port);
973 return ENODEV;
974 }
975
976 return (ofproto->ofproto_class->set_stp_port
977 ? ofproto->ofproto_class->set_stp_port(ofport, s)
978 : EOPNOTSUPP);
979}
980
981/* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
982 * 's'. If the 'enabled' member in 's' is false, then the other members
983 * are not meaningful.
984 *
985 * Returns 0 if successful, otherwise a positive errno value.*/
986int
4e022ec0 987ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
21f7563c
JP
988 struct ofproto_port_stp_status *s)
989{
990 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
991 if (!ofport) {
b0a5c43b 992 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
94783c7c 993 "port %"PRIu32, ofproto->name, ofp_port);
21f7563c
JP
994 return ENODEV;
995 }
996
997 return (ofproto->ofproto_class->get_stp_port_status
998 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
999 : EOPNOTSUPP);
1000}
fd28ce3a
JS
1001
1002/* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
1003 * 's'. If the 'enabled' member in 's' is false, then the other members
1004 * are not meaningful.
1005 *
1006 * Returns 0 if successful, otherwise a positive errno value.*/
1007int
1008ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port,
1009 struct ofproto_port_stp_stats *s)
1010{
1011 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1012 if (!ofport) {
1013 VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent "
94783c7c 1014 "port %"PRIu32, ofproto->name, ofp_port);
fd28ce3a
JS
1015 return ENODEV;
1016 }
1017
1018 return (ofproto->ofproto_class->get_stp_port_stats
1019 ? ofproto->ofproto_class->get_stp_port_stats(ofport, s)
1020 : EOPNOTSUPP);
1021}
9efd308e
DV
1022
1023/* Rapid Spanning Tree Protocol (RSTP) configuration. */
1024
1025/* Configures RSTP on 'ofproto' using the settings defined in 's'. If
1026 * 's' is NULL, disables RSTP.
1027 *
1028 * Returns 0 if successful, otherwise a positive errno value. */
1029int
1030ofproto_set_rstp(struct ofproto *ofproto,
1031 const struct ofproto_rstp_settings *s)
1032{
1033 if (!ofproto->ofproto_class->set_rstp) {
1034 return EOPNOTSUPP;
1035 }
1036 ofproto->ofproto_class->set_rstp(ofproto, s);
1037 return 0;
1038}
1039
1040/* Retrieves RSTP status of 'ofproto' and stores it in 's'. If the
1041 * 'enabled' member of 's' is false, then the other members are not
1042 * meaningful.
1043 *
1044 * Returns 0 if successful, otherwise a positive errno value. */
1045int
1046ofproto_get_rstp_status(struct ofproto *ofproto,
1047 struct ofproto_rstp_status *s)
1048{
1049 if (!ofproto->ofproto_class->get_rstp_status) {
1050 return EOPNOTSUPP;
1051 }
1052 ofproto->ofproto_class->get_rstp_status(ofproto, s);
1053 return 0;
1054}
1055
1056/* Configures RSTP on 'ofp_port' of 'ofproto' using the settings defined
1057 * in 's'. The caller is responsible for assigning RSTP port numbers
1058 * (using the 'port_num' member in the range of 1 through 255, inclusive)
1059 * and ensuring there are no duplicates. If the 's' is NULL, then RSTP
1060 * is disabled on the port.
1061 *
1062 * Returns 0 if successful, otherwise a positive errno value.*/
1063int
1064ofproto_port_set_rstp(struct ofproto *ofproto, ofp_port_t ofp_port,
1065 const struct ofproto_port_rstp_settings *s)
1066{
1067 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1068 if (!ofport) {
94783c7c 1069 VLOG_WARN("%s: cannot configure RSTP on nonexistent port %"PRIu32,
9efd308e
DV
1070 ofproto->name, ofp_port);
1071 return ENODEV;
1072 }
1073
1074 if (!ofproto->ofproto_class->set_rstp_port) {
1075 return EOPNOTSUPP;
1076 }
1077 ofproto->ofproto_class->set_rstp_port(ofport, s);
1078 return 0;
1079}
1080
1081/* Retrieves RSTP port status of 'ofp_port' on 'ofproto' and stores it in
1082 * 's'. If the 'enabled' member in 's' is false, then the other members
1083 * are not meaningful.
1084 *
1085 * Returns 0 if successful, otherwise a positive errno value.*/
1086int
1087ofproto_port_get_rstp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
1088 struct ofproto_port_rstp_status *s)
1089{
1090 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1091 if (!ofport) {
1092 VLOG_WARN_RL(&rl, "%s: cannot get RSTP status on nonexistent "
94783c7c 1093 "port %"PRIu32, ofproto->name, ofp_port);
9efd308e
DV
1094 return ENODEV;
1095 }
1096
1097 if (!ofproto->ofproto_class->get_rstp_port_status) {
1098 return EOPNOTSUPP;
1099 }
1100 ofproto->ofproto_class->get_rstp_port_status(ofport, s);
1101 return 0;
1102}
21f7563c 1103\f
8b36f51e
EJ
1104/* Queue DSCP configuration. */
1105
1106/* Registers meta-data associated with the 'n_qdscp' Qualities of Service
1107 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
1108 * to implement QoS. Instead, it is used to implement features which require
1109 * knowledge of what queues exist on a port, and some basic information about
1110 * them.
1111 *
1112 * Returns 0 if successful, otherwise a positive errno value. */
1113int
4e022ec0 1114ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
8b36f51e
EJ
1115 const struct ofproto_port_queue *queues,
1116 size_t n_queues)
1117{
1118 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1119
1120 if (!ofport) {
94783c7c 1121 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu32,
8b36f51e
EJ
1122 ofproto->name, ofp_port);
1123 return ENODEV;
1124 }
1125
1126 return (ofproto->ofproto_class->set_queues
1127 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
1128 : EOPNOTSUPP);
1129}
1130\f
0477baa9
DF
1131/* LLDP configuration. */
1132void
1133ofproto_port_set_lldp(struct ofproto *ofproto,
1134 ofp_port_t ofp_port,
1135 const struct smap *cfg)
1136{
1137 struct ofport *ofport;
1138 int error;
1139
1140 ofport = ofproto_get_port(ofproto, ofp_port);
1141 if (!ofport) {
94783c7c 1142 VLOG_WARN("%s: cannot configure LLDP on nonexistent port %"PRIu32,
0477baa9
DF
1143 ofproto->name, ofp_port);
1144 return;
1145 }
1146 error = (ofproto->ofproto_class->set_lldp
1147 ? ofproto->ofproto_class->set_lldp(ofport, cfg)
1148 : EOPNOTSUPP);
1149 if (error) {
94783c7c 1150 VLOG_WARN("%s: lldp configuration on port %"PRIu32" (%s) failed (%s)",
0477baa9
DF
1151 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
1152 ovs_strerror(error));
1153 }
1154}
1155
1156int
1157ofproto_set_aa(struct ofproto *ofproto, void *aux OVS_UNUSED,
1158 const struct aa_settings *s)
1159{
1160 if (!ofproto->ofproto_class->set_aa) {
1161 return EOPNOTSUPP;
1162 }
1163 ofproto->ofproto_class->set_aa(ofproto, s);
1164 return 0;
1165}
1166
1167int
1168ofproto_aa_mapping_register(struct ofproto *ofproto, void *aux,
1169 const struct aa_mapping_settings *s)
1170{
1171 if (!ofproto->ofproto_class->aa_mapping_set) {
1172 return EOPNOTSUPP;
1173 }
1174 ofproto->ofproto_class->aa_mapping_set(ofproto, aux, s);
1175 return 0;
1176}
1177
1178int
1179ofproto_aa_mapping_unregister(struct ofproto *ofproto, void *aux)
1180{
1181 if (!ofproto->ofproto_class->aa_mapping_unset) {
1182 return EOPNOTSUPP;
1183 }
1184 ofproto->ofproto_class->aa_mapping_unset(ofproto, aux);
1185 return 0;
1186}
1187
1188int
1189ofproto_aa_vlan_get_queued(struct ofproto *ofproto,
1190 struct ovs_list *list)
1191{
1192 if (!ofproto->ofproto_class->aa_vlan_get_queued) {
1193 return EOPNOTSUPP;
1194 }
1195 ofproto->ofproto_class->aa_vlan_get_queued(ofproto, list);
1196 return 0;
1197}
1198
1199unsigned int
1200ofproto_aa_vlan_get_queue_size(struct ofproto *ofproto)
1201{
1202 if (!ofproto->ofproto_class->aa_vlan_get_queue_size) {
1203 return EOPNOTSUPP;
1204 }
1205 return ofproto->ofproto_class->aa_vlan_get_queue_size(ofproto);
1206}
1207
e7934396
BP
1208/* Connectivity Fault Management configuration. */
1209
892815f5 1210/* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
e7934396 1211void
4e022ec0 1212ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
e7934396 1213{
abe529af
BP
1214 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1215 if (ofport && ofproto->ofproto_class->set_cfm) {
a5610457 1216 ofproto->ofproto_class->set_cfm(ofport, NULL);
e7934396
BP
1217 }
1218}
72b06300 1219
892815f5 1220/* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
93b8df38
EJ
1221 * basic configuration from the configuration members in 'cfm', and the remote
1222 * maintenance point ID from remote_mpid. Ignores the statistics members of
1223 * 'cfm'.
e7934396 1224 *
892815f5 1225 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
e7934396 1226void
4e022ec0 1227ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
a5610457 1228 const struct cfm_settings *s)
e7934396
BP
1229{
1230 struct ofport *ofport;
abe529af 1231 int error;
e7934396 1232
abe529af 1233 ofport = ofproto_get_port(ofproto, ofp_port);
e7934396 1234 if (!ofport) {
94783c7c 1235 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
892815f5 1236 ofproto->name, ofp_port);
e7934396
BP
1237 return;
1238 }
1239
93b8df38
EJ
1240 /* XXX: For configuration simplicity, we only support one remote_mpid
1241 * outside of the CFM module. It's not clear if this is the correct long
1242 * term solution or not. */
abe529af 1243 error = (ofproto->ofproto_class->set_cfm
a5610457 1244 ? ofproto->ofproto_class->set_cfm(ofport, s)
abe529af
BP
1245 : EOPNOTSUPP);
1246 if (error) {
94783c7c 1247 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed (%s)",
abe529af 1248 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
10a89ef0 1249 ovs_strerror(error));
e7934396 1250 }
e7934396 1251}
e7934396 1252
ccc09689
EJ
1253/* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
1254 * 'ofproto' does not have a port 'ofp_port'. */
1255void
4e022ec0 1256ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
ccc09689
EJ
1257 const struct smap *cfg)
1258{
1259 struct ofport *ofport;
1260 int error;
1261
1262 ofport = ofproto_get_port(ofproto, ofp_port);
1263 if (!ofport) {
94783c7c 1264 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu32,
ccc09689 1265 ofproto->name, ofp_port);
e8999bdc 1266 return;
ccc09689
EJ
1267 }
1268
1269 error = (ofproto->ofproto_class->set_bfd
1270 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
1271 : EOPNOTSUPP);
1272 if (error) {
94783c7c 1273 VLOG_WARN("%s: bfd configuration on port %"PRIu32" (%s) failed (%s)",
ccc09689 1274 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
10a89ef0 1275 ovs_strerror(error));
ccc09689
EJ
1276 }
1277}
1278
8f5514fe
AW
1279/* Checks the status change of BFD on 'ofport'.
1280 *
1281 * Returns true if 'ofproto_class' does not support 'bfd_status_changed'. */
1282bool
1283ofproto_port_bfd_status_changed(struct ofproto *ofproto, ofp_port_t ofp_port)
1284{
1285 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1286 return (ofport && ofproto->ofproto_class->bfd_status_changed
1287 ? ofproto->ofproto_class->bfd_status_changed(ofport)
1288 : true);
1289}
1290
88bf179a 1291/* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on
8f5514fe
AW
1292 * success. Returns a positive errno otherwise. Has no effect if 'ofp_port'
1293 * is not an OpenFlow port in 'ofproto'.
88bf179a
AW
1294 *
1295 * The caller must provide and own '*status'. */
ccc09689 1296int
4e022ec0 1297ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
ccc09689
EJ
1298 struct smap *status)
1299{
1300 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1301 return (ofport && ofproto->ofproto_class->get_bfd_status
1302 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
1303 : EOPNOTSUPP);
1304}
1305
fa066f01
BP
1306/* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
1307 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
1308 * 0 if LACP partner information is not current (generally indicating a
1309 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
1310int
4e022ec0 1311ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
fa066f01 1312{
abe529af
BP
1313 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1314 return (ofport && ofproto->ofproto_class->port_is_lacp_current
1315 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
fa066f01 1316 : -1);
e7934396 1317}
50b9699f
NM
1318
1319int
1320ofproto_port_get_lacp_stats(const struct ofport *port, struct lacp_slave_stats *stats)
1321{
1322 struct ofproto *ofproto = port->ofproto;
1323 int error;
1324
1325 if (ofproto->ofproto_class->port_get_lacp_stats) {
1326 error = ofproto->ofproto_class->port_get_lacp_stats(port, stats);
1327 } else {
1328 error = EOPNOTSUPP;
1329 }
1330
1331 return error;
1332}
e7934396 1333\f
fa066f01 1334/* Bundles. */
e7934396 1335
abe529af
BP
1336/* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
1337 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1338 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1339 * configuration plus, if there is more than one slave, a bonding
1340 * configuration.
1341 *
1342 * If 'aux' is already registered then this function updates its configuration
1343 * to 's'. Otherwise, this function registers a new bundle.
1344 *
1345 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1346 * port. */
1347int
1348ofproto_bundle_register(struct ofproto *ofproto, void *aux,
1349 const struct ofproto_bundle_settings *s)
fa066f01 1350{
abe529af
BP
1351 return (ofproto->ofproto_class->bundle_set
1352 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
1353 : EOPNOTSUPP);
fa066f01
BP
1354}
1355
abe529af
BP
1356/* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1357 * If no such bundle has been registered, this has no effect. */
1358int
1359ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
e7934396 1360{
abe529af 1361 return ofproto_bundle_register(ofproto, aux, NULL);
e7934396 1362}
fa066f01 1363
e7934396 1364\f
abe529af
BP
1365/* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1366 * If 'aux' is already registered then this function updates its configuration
c06bba01 1367 * to 's'. Otherwise, this function registers a new mirror. */
abe529af
BP
1368int
1369ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1370 const struct ofproto_mirror_settings *s)
064af421 1371{
abe529af
BP
1372 return (ofproto->ofproto_class->mirror_set
1373 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1374 : EOPNOTSUPP);
064af421
BP
1375}
1376
abe529af
BP
1377/* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1378 * If no mirror has been registered, this has no effect. */
1379int
1380ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
064af421 1381{
abe529af 1382 return ofproto_mirror_register(ofproto, aux, NULL);
064af421
BP
1383}
1384
9d24de3b
JP
1385/* Retrieves statistics from mirror associated with client data pointer
1386 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1387 * 'bytes', respectively. If a particular counters is not supported,
0477baa9
DF
1388 * the appropriate argument is set to UINT64_MAX.
1389 */
9d24de3b
JP
1390int
1391ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1392 uint64_t *packets, uint64_t *bytes)
1393{
1394 if (!ofproto->ofproto_class->mirror_get_stats) {
1395 *packets = *bytes = UINT64_MAX;
1396 return EOPNOTSUPP;
1397 }
1398
1399 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1400 packets, bytes);
1401}
1402
abe529af
BP
1403/* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1404 * which all packets are flooded, instead of using MAC learning. If
1405 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1406 *
1407 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1408 * port. */
1409int
1410ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
abdfe474 1411{
abe529af
BP
1412 return (ofproto->ofproto_class->set_flood_vlans
1413 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1414 : EOPNOTSUPP);
abdfe474
JP
1415}
1416
abe529af
BP
1417/* Returns true if 'aux' is a registered bundle that is currently in use as the
1418 * output for a mirror. */
1419bool
b4affc74 1420ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
abe529af
BP
1421{
1422 return (ofproto->ofproto_class->is_mirror_output_bundle
1423 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1424 : false);
1425}
1426\f
254750ce
BP
1427/* Configuration of OpenFlow tables. */
1428
1429/* Returns the number of OpenFlow tables in 'ofproto'. */
1430int
1431ofproto_get_n_tables(const struct ofproto *ofproto)
1432{
1433 return ofproto->n_tables;
1434}
1435
adcf00ba
AZ
1436/* Returns the number of Controller visible OpenFlow tables
1437 * in 'ofproto'. This number will exclude Hidden tables.
1438 * This funtion's return value should be less or equal to that of
1439 * ofproto_get_n_tables() . */
1440uint8_t
1441ofproto_get_n_visible_tables(const struct ofproto *ofproto)
1442{
1443 uint8_t n = ofproto->n_tables;
1444
1445 /* Count only non-hidden tables in the number of tables. (Hidden tables,
1446 * if present, are always at the end.) */
1447 while(n && (ofproto->tables[n - 1].flags & OFTABLE_HIDDEN)) {
1448 n--;
1449 }
1450
1451 return n;
1452}
1453
254750ce
BP
1454/* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1455 * settings from 's'. 'table_id' must be in the range 0 through the number of
1456 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1457 *
1458 * For read-only tables, only the name may be configured. */
1459void
1460ofproto_configure_table(struct ofproto *ofproto, int table_id,
1461 const struct ofproto_table_settings *s)
1462{
1463 struct oftable *table;
1464
cb22974d 1465 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
254750ce
BP
1466 table = &ofproto->tables[table_id];
1467
1468 oftable_set_name(table, s->name);
1469
1470 if (table->flags & OFTABLE_READONLY) {
1471 return;
1472 }
1473
f358a2cb
JR
1474 if (classifier_set_prefix_fields(&table->cls,
1475 s->prefix_fields, s->n_prefix_fields)) {
1476 /* XXX: Trigger revalidation. */
1477 }
834fe5cb
BP
1478
1479 ovs_mutex_lock(&ofproto_mutex);
82c22d34
BP
1480 unsigned int new_eviction = (s->enable_eviction
1481 ? table->eviction | EVICTION_CLIENT
1482 : table->eviction & ~EVICTION_CLIENT);
1483 oftable_configure_eviction(table, new_eviction, s->groups, s->n_groups);
7394b8fb 1484 table->max_flows = s->max_flows;
6787a49f 1485 evict_rules_from_table(table);
834fe5cb 1486 ovs_mutex_unlock(&ofproto_mutex);
254750ce
BP
1487}
1488\f
81e2083f
BP
1489bool
1490ofproto_has_snoops(const struct ofproto *ofproto)
1491{
1492 return connmgr_has_snoops(ofproto->connmgr);
1493}
1494
064af421 1495void
81e2083f 1496ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
064af421 1497{
19a87e36 1498 connmgr_get_snoops(ofproto->connmgr, snoops);
064af421
BP
1499}
1500
a73fcd71 1501/* Deletes 'rule' from 'ofproto'.
8037acb4 1502 *
b74cb5d8
BP
1503 * Within an ofproto implementation, this function allows an ofproto
1504 * implementation to destroy any rules that remain when its ->destruct()
1505 * function is called. This function is not suitable for use elsewhere in an
1506 * ofproto implementation.
1507 *
b74cb5d8 1508 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
8b81d1ef 1509 * Cycle" in ofproto-provider.h. */
b74cb5d8 1510void
8b81d1ef 1511ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule)
15aaf599 1512 OVS_EXCLUDED(ofproto_mutex)
7ee20df1 1513{
834fe5cb
BP
1514 /* This skips the ofmonitor and flow-removed notifications because the
1515 * switch is being deleted and any OpenFlow channels have been or soon will
1516 * be killed. */
15aaf599 1517 ovs_mutex_lock(&ofproto_mutex);
39c94593 1518
30d0452c 1519 if (rule->state == RULE_INSERTED) {
39c94593 1520 /* Make sure there is no postponed removal of the rule. */
44e0c35d 1521 ovs_assert(cls_rule_visible_in_version(&rule->cr, OVS_VERSION_MAX));
39c94593
JR
1522
1523 if (!classifier_remove(&rule->ofproto->tables[rule->table_id].cls,
1524 &rule->cr)) {
1525 OVS_NOT_REACHED();
1526 }
1527 ofproto_rule_remove__(rule->ofproto, rule);
1fc71871
JR
1528 if (ofproto->ofproto_class->rule_delete) {
1529 ofproto->ofproto_class->rule_delete(rule);
1530 }
25010c68
JR
1531
1532 /* This may not be the last reference to the rule. */
39c94593
JR
1533 ofproto_rule_unref(rule);
1534 }
15aaf599 1535 ovs_mutex_unlock(&ofproto_mutex);
8037acb4
BP
1536}
1537
1538static void
1539ofproto_flush__(struct ofproto *ofproto)
15aaf599 1540 OVS_EXCLUDED(ofproto_mutex)
8037acb4 1541{
d0918789 1542 struct oftable *table;
7ee20df1 1543
3948503a 1544 /* This will flush all datapath flows. */
7ee20df1
BP
1545 if (ofproto->ofproto_class->flush) {
1546 ofproto->ofproto_class->flush(ofproto);
1547 }
1548
3948503a
JR
1549 /* XXX: There is a small race window here, where new datapath flows can be
1550 * created by upcall handlers based on the existing flow table. We can not
1551 * call ofproto class flush while holding 'ofproto_mutex' to prevent this,
1552 * as then we could deadlock on syncing with the handler threads waiting on
1553 * the same mutex. */
1554
15aaf599 1555 ovs_mutex_lock(&ofproto_mutex);
b772ded8 1556 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
802f84ff 1557 struct rule_collection rules;
78c8df12 1558 struct rule *rule;
7ee20df1 1559
5c67e4af
BP
1560 if (table->flags & OFTABLE_HIDDEN) {
1561 continue;
1562 }
1563
802f84ff
JR
1564 rule_collection_init(&rules);
1565
de4ad4a2 1566 CLS_FOR_EACH (rule, cr, &table->cls) {
802f84ff 1567 rule_collection_add(&rules, rule);
7ee20df1 1568 }
802f84ff 1569 delete_flows__(&rules, OFPRR_DELETE, NULL);
7ee20df1 1570 }
d3b84833
BP
1571 ofproto_group_delete_all__(ofproto);
1572 meter_delete_all(ofproto);
3948503a
JR
1573 /* XXX: Concurrent handler threads may insert new learned flows based on
1574 * learn actions of the now deleted flows right after we release
1575 * 'ofproto_mutex'. */
15aaf599 1576 ovs_mutex_unlock(&ofproto_mutex);
7ee20df1
BP
1577}
1578
abe529af
BP
1579static void
1580ofproto_destroy__(struct ofproto *ofproto)
15aaf599 1581 OVS_EXCLUDED(ofproto_mutex)
abe529af 1582{
d0918789 1583 struct oftable *table;
6c1491fb 1584
db88b35c 1585 cmap_destroy(&ofproto->groups);
7395c052 1586
abe529af 1587 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
04f48a68 1588
abe529af 1589 free(ofproto->name);
955a7127 1590 free(ofproto->type);
abe529af
BP
1591 free(ofproto->mfr_desc);
1592 free(ofproto->hw_desc);
1593 free(ofproto->sw_desc);
1594 free(ofproto->serial_desc);
1595 free(ofproto->dp_desc);
abe529af 1596 hmap_destroy(&ofproto->ports);
fdcea803 1597 hmap_destroy(&ofproto->ofport_usage);
abe529af 1598 shash_destroy(&ofproto->port_by_name);
e1b1d06a 1599 simap_destroy(&ofproto->ofp_requests);
6c1491fb 1600
b772ded8 1601 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
d0918789 1602 oftable_destroy(table);
6c1491fb
BP
1603 }
1604 free(ofproto->tables);
fa066f01 1605
d3b84833
BP
1606 hmap_destroy(&ofproto->meters);
1607
a70436a3
YHW
1608 ovs_mutex_lock(&ofproto->vl_mff_map.mutex);
1609 mf_vl_mff_map_clear(&ofproto->vl_mff_map, true);
1610 ovs_mutex_unlock(&ofproto->vl_mff_map.mutex);
1611 cmap_destroy(&ofproto->vl_mff_map.cmap);
1612 ovs_mutex_destroy(&ofproto->vl_mff_map.mutex);
1613 tun_metadata_free(ovsrcu_get_protected(struct tun_table *,
1614 &ofproto->metadata_tab));
1615
1406c79b
BP
1616 ovs_assert(hindex_is_empty(&ofproto->cookies));
1617 hindex_destroy(&ofproto->cookies);
1618
35f48b8b
BP
1619 ovs_assert(hmap_is_empty(&ofproto->learned_cookies));
1620 hmap_destroy(&ofproto->learned_cookies);
1621
abe529af
BP
1622 ofproto->ofproto_class->dealloc(ofproto);
1623}
fa066f01 1624
39c94593
JR
1625/* Destroying rules is doubly deferred, must have 'ofproto' around for them.
1626 * - 1st we defer the removal of the rules from the classifier
1627 * - 2nd we defer the actual destruction of the rules. */
1628static void
1629ofproto_destroy_defer__(struct ofproto *ofproto)
1630 OVS_EXCLUDED(ofproto_mutex)
1631{
1632 ovsrcu_postpone(ofproto_destroy__, ofproto);
1633}
1634
064af421 1635void
9aad5a5a 1636ofproto_destroy(struct ofproto *p, bool del)
15aaf599 1637 OVS_EXCLUDED(ofproto_mutex)
064af421 1638{
ca0f572c 1639 struct ofport *ofport, *next_ofport;
4ec3d7c7 1640 struct ofport_usage *usage;
064af421
BP
1641
1642 if (!p) {
1643 return;
1644 }
1645
7ee20df1 1646 ofproto_flush__(p);
4e8e4213 1647 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
9aad5a5a 1648 ofport_destroy(ofport, del);
064af421 1649 }
064af421 1650
4ec3d7c7 1651 HMAP_FOR_EACH_POP (usage, hmap_node, &p->ofport_usage) {
fdcea803
GS
1652 free(usage);
1653 }
1654
fe13ccdc 1655 p->ofproto_class->destruct(p, del);
58f19539
DDP
1656
1657 /* We should not postpone this because it involves deleting a listening
25010c68
JR
1658 * socket which we may want to reopen soon. 'connmgr' may be used by other
1659 * threads only if they take the ofproto_mutex and read a non-NULL
1660 * 'ofproto->connmgr'. */
1661 ovs_mutex_lock(&ofproto_mutex);
58f19539 1662 connmgr_destroy(p->connmgr);
25010c68
JR
1663 p->connmgr = NULL;
1664 ovs_mutex_unlock(&ofproto_mutex);
58f19539 1665
f416c8d6 1666 /* Destroying rules is deferred, must have 'ofproto' around for them. */
39c94593 1667 ovsrcu_postpone(ofproto_destroy_defer__, p);
064af421
BP
1668}
1669
abe529af
BP
1670/* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1671 * kernel datapath, for example, this destroys the datapath in the kernel, and
1672 * with the netdev-based datapath, it tears down the data structures that
1673 * represent the datapath.
1674 *
1675 * The datapath should not be currently open as an ofproto. */
064af421 1676int
abe529af 1677ofproto_delete(const char *name, const char *type)
064af421 1678{
abe529af
BP
1679 const struct ofproto_class *class = ofproto_class_find__(type);
1680 return (!class ? EAFNOSUPPORT
1681 : !class->del ? EACCES
1682 : class->del(type, name));
064af421
BP
1683}
1684
e9e28be3
BP
1685static void
1686process_port_change(struct ofproto *ofproto, int error, char *devname)
1687{
1688 if (error == ENOBUFS) {
1689 reinit_ports(ofproto);
1690 } else if (!error) {
1691 update_port(ofproto, devname);
1692 free(devname);
1693 }
1694}
1695
11a574a7
JP
1696int
1697ofproto_type_run(const char *datapath_type)
1698{
1699 const struct ofproto_class *class;
1700 int error;
1701
1702 datapath_type = ofproto_normalize_type(datapath_type);
1703 class = ofproto_class_find__(datapath_type);
1704
1705 error = class->type_run ? class->type_run(datapath_type) : 0;
1706 if (error && error != EAGAIN) {
1707 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
10a89ef0 1708 datapath_type, ovs_strerror(error));
11a574a7
JP
1709 }
1710 return error;
1711}
1712
11a574a7
JP
1713void
1714ofproto_type_wait(const char *datapath_type)
1715{
1716 const struct ofproto_class *class;
1717
1718 datapath_type = ofproto_normalize_type(datapath_type);
1719 class = ofproto_class_find__(datapath_type);
1720
1721 if (class->type_wait) {
1722 class->type_wait(datapath_type);
1723 }
1724}
1725
064af421 1726int
abe529af 1727ofproto_run(struct ofproto *p)
064af421 1728{
064af421 1729 int error;
da4a6191 1730 uint64_t new_seq;
064af421 1731
abe529af 1732 error = p->ofproto_class->run(p);
5fcc0d00 1733 if (error && error != EAGAIN) {
10a89ef0 1734 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
149f577a
JG
1735 }
1736
448c2fa8
EJ
1737 /* Restore the eviction group heap invariant occasionally. */
1738 if (p->eviction_group_timer < time_msec()) {
1739 size_t i;
1740
1741 p->eviction_group_timer = time_msec() + 1000;
1742
1743 for (i = 0; i < p->n_tables; i++) {
1744 struct oftable *table = &p->tables[i];
1745 struct eviction_group *evg;
448c2fa8
EJ
1746 struct rule *rule;
1747
82c22d34 1748 if (!table->eviction) {
448c2fa8
EJ
1749 continue;
1750 }
1751
d79e3d70 1752 if (table->n_flows > 100000) {
1a9bb326
EJ
1753 static struct vlog_rate_limit count_rl =
1754 VLOG_RATE_LIMIT_INIT(1, 1);
1755 VLOG_WARN_RL(&count_rl, "Table %"PRIuSIZE" has an excessive"
d79e3d70 1756 " number of rules: %d", i, table->n_flows);
1a9bb326
EJ
1757 }
1758
15aaf599 1759 ovs_mutex_lock(&ofproto_mutex);
5f0476ce 1760 CLS_FOR_EACH (rule, cr, &table->cls) {
6d56c1f1
K
1761 if (rule->idle_timeout || rule->hard_timeout) {
1762 if (!rule->eviction_group) {
1763 eviction_group_add_rule(rule);
1764 } else {
1765 heap_raw_change(&rule->evg_node,
1766 rule_eviction_priority(p, rule));
1767 }
448c2fa8
EJ
1768 }
1769 }
6d56c1f1
K
1770
1771 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1772 heap_rebuild(&evg->rules);
1773 }
15aaf599 1774 ovs_mutex_unlock(&ofproto_mutex);
448c2fa8
EJ
1775 }
1776 }
1777
5bf0e941 1778 if (p->ofproto_class->port_poll) {
7436ed80
BP
1779 char *devname;
1780
5bf0e941
BP
1781 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1782 process_port_change(p, error, devname);
064af421 1783 }
e9e28be3 1784 }
031d8bff 1785
da4a6191
JS
1786 new_seq = seq_read(connectivity_seq_get());
1787 if (new_seq != p->change_seq) {
1788 struct sset devnames;
1789 const char *devname;
1790 struct ofport *ofport;
1791
1792 /* Update OpenFlow port status for any port whose netdev has changed.
1793 *
1794 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1795 * destroyed, so it's not safe to update ports directly from the
1796 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1797 * need this two-phase approach. */
1798 sset_init(&devnames);
1799 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
61501798
AW
1800 uint64_t port_change_seq;
1801
1802 port_change_seq = netdev_get_change_seq(ofport->netdev);
1803 if (ofport->change_seq != port_change_seq) {
1804 ofport->change_seq = port_change_seq;
1805 sset_add(&devnames, netdev_get_name(ofport->netdev));
1806 }
031d8bff 1807 }
da4a6191
JS
1808 SSET_FOR_EACH (devname, &devnames) {
1809 update_port(p, devname);
1810 }
1811 sset_destroy(&devnames);
1812
1813 p->change_seq = new_seq;
064af421
BP
1814 }
1815
834fe5cb 1816 connmgr_run(p->connmgr, handle_openflow);
064af421 1817
5fcc0d00
BP
1818 return error;
1819}
1820
064af421
BP
1821void
1822ofproto_wait(struct ofproto *p)
1823{
abe529af 1824 p->ofproto_class->wait(p);
5bf0e941
BP
1825 if (p->ofproto_class->port_poll_wait) {
1826 p->ofproto_class->port_poll_wait(p);
e7934396 1827 }
da4a6191 1828 seq_wait(connectivity_seq_get(), p->change_seq);
834fe5cb 1829 connmgr_wait(p->connmgr);
064af421
BP
1830}
1831
064af421
BP
1832bool
1833ofproto_is_alive(const struct ofproto *p)
1834{
19a87e36 1835 return connmgr_has_controllers(p->connmgr);
064af421
BP
1836}
1837
0d085684
BP
1838/* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1839 * memory_report(). */
1840void
1841ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1842{
1843 const struct oftable *table;
1844 unsigned int n_rules;
1845
1846 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
15aaf599 1847
0d085684
BP
1848 n_rules = 0;
1849 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
d79e3d70 1850 n_rules += table->n_flows;
0d085684
BP
1851 }
1852 simap_increase(usage, "rules", n_rules);
1853
1854 if (ofproto->ofproto_class->get_memory_usage) {
1855 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1856 }
1857
1858 connmgr_get_memory_usage(ofproto->connmgr, usage);
1859}
1860
1c030aa5
EJ
1861void
1862ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage)
1863{
1864 const struct ofproto_class *class;
1865
1866 datapath_type = ofproto_normalize_type(datapath_type);
1867 class = ofproto_class_find__(datapath_type);
1868
1869 if (class && class->type_get_memory_usage) {
1870 class->type_get_memory_usage(datapath_type, usage);
1871 }
1872}
1873
bffc0589 1874void
2cdcb898 1875ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
bffc0589
AE
1876 struct shash *info)
1877{
19a87e36 1878 connmgr_get_controller_info(ofproto->connmgr, info);
bffc0589
AE
1879}
1880
1881void
1882ofproto_free_ofproto_controller_info(struct shash *info)
1883{
72ba2ed3 1884 connmgr_free_controller_info(info);
bffc0589
AE
1885}
1886
b5827b24
BP
1887/* Makes a deep copy of 'old' into 'port'. */
1888void
1889ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1890{
1891 port->name = xstrdup(old->name);
1892 port->type = xstrdup(old->type);
1893 port->ofp_port = old->ofp_port;
1894}
1895
1896/* Frees memory allocated to members of 'ofproto_port'.
3a6ccc8c 1897 *
b5827b24
BP
1898 * Do not call this function on an ofproto_port obtained from
1899 * ofproto_port_dump_next(): that function retains ownership of the data in the
1900 * ofproto_port. */
1901void
1902ofproto_port_destroy(struct ofproto_port *ofproto_port)
1903{
1904 free(ofproto_port->name);
1905 free(ofproto_port->type);
1906}
1907
b5827b24 1908/* Initializes 'dump' to begin dumping the ports in an ofproto.
3a6ccc8c 1909 *
b5827b24
BP
1910 * This function provides no status indication. An error status for the entire
1911 * dump operation is provided when it is completed by calling
1912 * ofproto_port_dump_done().
1913 */
1914void
1915ofproto_port_dump_start(struct ofproto_port_dump *dump,
1916 const struct ofproto *ofproto)
1917{
abe529af
BP
1918 dump->ofproto = ofproto;
1919 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1920 &dump->state);
b5827b24
BP
1921}
1922
1923/* Attempts to retrieve another port from 'dump', which must have been created
1924 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1925 * 'port' and returns true. On failure, returns false.
1926 *
1927 * Failure might indicate an actual error or merely that the last port has been
1928 * dumped. An error status for the entire dump operation is provided when it
1929 * is completed by calling ofproto_port_dump_done().
1930 *
1931 * The ofproto owns the data stored in 'port'. It will remain valid until at
1932 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1933 * ofproto_port_dump_done(). */
1934bool
1935ofproto_port_dump_next(struct ofproto_port_dump *dump,
1936 struct ofproto_port *port)
1937{
abe529af
BP
1938 const struct ofproto *ofproto = dump->ofproto;
1939
1940 if (dump->error) {
1941 return false;
1942 }
b5827b24 1943
abe529af
BP
1944 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1945 port);
1946 if (dump->error) {
1947 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1948 return false;
b5827b24 1949 }
abe529af 1950 return true;
b5827b24
BP
1951}
1952
1953/* Completes port table dump operation 'dump', which must have been created
1954 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1955 * error-free, otherwise a positive errno value describing the problem. */
3a6ccc8c 1956int
b5827b24 1957ofproto_port_dump_done(struct ofproto_port_dump *dump)
3a6ccc8c 1958{
abe529af
BP
1959 const struct ofproto *ofproto = dump->ofproto;
1960 if (!dump->error) {
1961 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1962 dump->state);
1963 }
1964 return dump->error == EOF ? 0 : dump->error;
b5827b24
BP
1965}
1966
0aeaabc8
JP
1967/* Returns the type to pass to netdev_open() when a datapath of type
1968 * 'datapath_type' has a port of type 'port_type', for a few special
1969 * cases when a netdev type differs from a port type. For example, when
1970 * using the userspace datapath, a port of type "internal" needs to be
1971 * opened as "tap".
1972 *
1973 * Returns either 'type' itself or a string literal, which must not be
1974 * freed. */
1975const char *
1976ofproto_port_open_type(const char *datapath_type, const char *port_type)
1977{
1978 const struct ofproto_class *class;
1979
1980 datapath_type = ofproto_normalize_type(datapath_type);
1981 class = ofproto_class_find__(datapath_type);
1982 if (!class) {
1983 return port_type;
1984 }
1985
1986 return (class->port_open_type
1987 ? class->port_open_type(datapath_type, port_type)
1988 : port_type);
1989}
1990
81816a5f
JP
1991/* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1992 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1993 * the port's OpenFlow port number.
1994 *
1995 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1996 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1997 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1998 * 'ofp_portp' is non-null). */
b5827b24
BP
1999int
2000ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
4e022ec0 2001 ofp_port_t *ofp_portp)
b5827b24 2002{
4e022ec0 2003 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
3a6ccc8c
BP
2004 int error;
2005
e1b1d06a 2006 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1fa24dea 2007 if (!error) {
e1b1d06a
JP
2008 const char *netdev_name = netdev_get_name(netdev);
2009
4e022ec0
AW
2010 simap_put(&ofproto->ofp_requests, netdev_name,
2011 ofp_to_u16(ofp_port));
01c77073 2012 error = update_port(ofproto, netdev_name);
1fa24dea 2013 }
b5827b24 2014 if (ofp_portp) {
17f69db5
BP
2015 *ofp_portp = OFPP_NONE;
2016 if (!error) {
2017 struct ofproto_port ofproto_port;
2018
2019 error = ofproto_port_query_by_name(ofproto,
2020 netdev_get_name(netdev),
2021 &ofproto_port);
2022 if (!error) {
2023 *ofp_portp = ofproto_port.ofp_port;
2024 ofproto_port_destroy(&ofproto_port);
2025 }
2026 }
3a6ccc8c
BP
2027 }
2028 return error;
2029}
2030
b5827b24
BP
2031/* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
2032 * initializes '*port' appropriately; on failure, returns a positive errno
2033 * value.
2034 *
abe529af 2035 * The caller owns the data in 'ofproto_port' and must free it with
b5827b24
BP
2036 * ofproto_port_destroy() when it is no longer needed. */
2037int
2038ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
2039 struct ofproto_port *port)
a4e2e1f2 2040{
b5827b24
BP
2041 int error;
2042
abe529af
BP
2043 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
2044 if (error) {
2045 memset(port, 0, sizeof *port);
b5827b24
BP
2046 }
2047 return error;
a4e2e1f2
EJ
2048}
2049
b5827b24 2050/* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
3a6ccc8c 2051 * Returns 0 if successful, otherwise a positive errno. */
064af421 2052int
4e022ec0 2053ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
064af421 2054{
abe529af 2055 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1264ec95 2056 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
e1b1d06a 2057 struct simap_node *ofp_request_node;
3cf10406 2058 int error;
cdee00fd 2059
e1b1d06a
JP
2060 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
2061 if (ofp_request_node) {
2062 simap_delete(&ofproto->ofp_requests, ofp_request_node);
2063 }
2064
abe529af 2065 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
892815f5 2066 if (!error && ofport) {
1264ec95
BP
2067 /* 'name' is the netdev's name and update_port() is going to close the
2068 * netdev. Just in case update_port() refers to 'name' after it
3a6ccc8c
BP
2069 * destroys 'ofport', make a copy of it around the update_port()
2070 * call. */
2071 char *devname = xstrdup(name);
2072 update_port(ofproto, devname);
2073 free(devname);
3cf10406
BP
2074 }
2075 return error;
064af421
BP
2076}
2077
91364d18
IM
2078/* Refreshes datapath configuration of port number 'ofp_port' in 'ofproto'.
2079 *
2080 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
2081void
2082ofproto_port_set_config(struct ofproto *ofproto, ofp_port_t ofp_port,
2083 const struct smap *cfg)
2084{
2085 struct ofport *ofport;
2086 int error;
2087
2088 ofport = ofproto_get_port(ofproto, ofp_port);
2089 if (!ofport) {
94783c7c 2090 VLOG_WARN("%s: cannot configure datapath on nonexistent port %"PRIu32,
91364d18
IM
2091 ofproto->name, ofp_port);
2092 return;
2093 }
2094
2095 error = (ofproto->ofproto_class->port_set_config
2096 ? ofproto->ofproto_class->port_set_config(ofport, cfg)
2097 : EOPNOTSUPP);
2098 if (error) {
94783c7c 2099 VLOG_WARN("%s: datapath configuration on port %"PRIu32
91364d18
IM
2100 " (%s) failed (%s)",
2101 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
2102 ovs_strerror(error));
2103 }
2104}
2105
2106
276f6518
SH
2107static void
2108flow_mod_init(struct ofputil_flow_mod *fm,
eb391b76 2109 const struct match *match, int priority,
276f6518
SH
2110 const struct ofpact *ofpacts, size_t ofpacts_len,
2111 enum ofp_flow_mod_command command)
2112{
39cc5c4a
BP
2113 *fm = (struct ofputil_flow_mod) {
2114 .match = *match,
2115 .priority = priority,
2116 .table_id = 0,
2117 .command = command,
2118 .buffer_id = UINT32_MAX,
2119 .out_port = OFPP_ANY,
2120 .out_group = OFPG_ANY,
2121 .ofpacts = CONST_CAST(struct ofpact *, ofpacts),
2122 .ofpacts_len = ofpacts_len,
39cc5c4a 2123 };
276f6518
SH
2124}
2125
97f63e57
BP
2126static int
2127simple_flow_mod(struct ofproto *ofproto,
eb391b76 2128 const struct match *match, int priority,
97f63e57
BP
2129 const struct ofpact *ofpacts, size_t ofpacts_len,
2130 enum ofp_flow_mod_command command)
2131{
7338102b 2132 struct ofputil_flow_mod fm;
97f63e57 2133
7338102b 2134 flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command);
15aaf599 2135
7338102b 2136 return handle_flow_mod__(ofproto, &fm, NULL);
97f63e57
BP
2137}
2138
6c1491fb 2139/* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
fa8b054f
BP
2140 * performs the 'n_actions' actions in 'actions'. The new flow will not
2141 * timeout.
2142 *
2143 * If cls_rule->priority is in the range of priorities supported by OpenFlow
2144 * (0...65535, inclusive) then the flow will be visible to OpenFlow
2145 * controllers; otherwise, it will be hidden.
2146 *
f25d0cf3 2147 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
6c1491fb
BP
2148 *
2149 * This is a helper function for in-band control and fail-open. */
064af421 2150void
81a76618 2151ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
eb391b76 2152 int priority,
f25d0cf3 2153 const struct ofpact *ofpacts, size_t ofpacts_len)
15aaf599 2154 OVS_EXCLUDED(ofproto_mutex)
064af421 2155{
7ee20df1 2156 const struct rule *rule;
6f00e29b 2157 bool must_add;
7ee20df1 2158
97f63e57
BP
2159 /* First do a cheap check whether the rule we're looking for already exists
2160 * with the actions that we want. If it does, then we're done. */
81a76618 2161 rule = rule_from_cls_rule(classifier_find_match_exactly(
2b7b1427 2162 &ofproto->tables[0].cls, match, priority,
44e0c35d 2163 OVS_VERSION_MAX));
6f00e29b 2164 if (rule) {
dc723c44 2165 const struct rule_actions *actions = rule_get_actions(rule);
06a0f3e2 2166 must_add = !ofpacts_equal(actions->ofpacts, actions->ofpacts_len,
6f00e29b 2167 ofpacts, ofpacts_len);
6f00e29b
BP
2168 } else {
2169 must_add = true;
2170 }
97f63e57
BP
2171
2172 /* If there's no such rule or the rule doesn't have the actions we want,
2173 * fall back to a executing a full flow mod. We can't optimize this at
2174 * all because we didn't take enough locks above to ensure that the flow
2175 * table didn't already change beneath us. */
6f00e29b 2176 if (must_add) {
97f63e57
BP
2177 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
2178 OFPFC_MODIFY_STRICT);
7ee20df1 2179 }
064af421
BP
2180}
2181
d51c8b71
JR
2182/* Executes the flow modification specified in 'fm'. Returns 0 on success, or
2183 * an OFPERR_* OpenFlow error code on failure.
75a75043 2184 *
2c7ee524 2185 * This is a helper function for in-band control and fail-open. */
d51c8b71 2186enum ofperr
7338102b 2187ofproto_flow_mod(struct ofproto *ofproto, const struct ofputil_flow_mod *fm)
15aaf599 2188 OVS_EXCLUDED(ofproto_mutex)
75a75043 2189{
7338102b 2190 return handle_flow_mod__(ofproto, fm, NULL);
75a75043
BP
2191}
2192
6c1491fb
BP
2193/* Searches for a rule with matching criteria exactly equal to 'target' in
2194 * ofproto's table 0 and, if it finds one, deletes it.
2195 *
2196 * This is a helper function for in-band control and fail-open. */
b20f4073 2197void
81a76618 2198ofproto_delete_flow(struct ofproto *ofproto,
eb391b76 2199 const struct match *target, int priority)
25010c68 2200 OVS_REQUIRES(ofproto_mutex)
064af421 2201{
8037acb4 2202 struct classifier *cls = &ofproto->tables[0].cls;
064af421
BP
2203 struct rule *rule;
2204
97f63e57
BP
2205 /* First do a cheap check whether the rule we're looking for has already
2206 * been deleted. If so, then we're done. */
39c94593 2207 rule = rule_from_cls_rule(classifier_find_match_exactly(
44e0c35d 2208 cls, target, priority, OVS_VERSION_MAX));
834fe5cb
BP
2209 if (!rule) {
2210 return;
bcf84111 2211 }
834fe5cb 2212
25010c68
JR
2213 struct rule_collection rules;
2214
2215 rule_collection_init(&rules);
2216 rule_collection_add(&rules, rule);
2217 delete_flows__(&rules, OFPRR_DELETE, NULL);
2218 rule_collection_destroy(&rules);
142e1f5c
BP
2219}
2220
834fe5cb
BP
2221/* Delete all of the flows from all of ofproto's flow tables, then reintroduce
2222 * the flows required by in-band control and fail-open. */
142e1f5c
BP
2223void
2224ofproto_flush_flows(struct ofproto *ofproto)
2225{
7ee20df1 2226 COVERAGE_INC(ofproto_flush);
834fe5cb
BP
2227 ofproto_flush__(ofproto);
2228 connmgr_flushed(ofproto->connmgr);
064af421
BP
2229}
2230\f
2231static void
2232reinit_ports(struct ofproto *p)
2233{
abe529af 2234 struct ofproto_port_dump dump;
b3c01ed3 2235 struct sset devnames;
064af421 2236 struct ofport *ofport;
abe529af 2237 struct ofproto_port ofproto_port;
b3c01ed3 2238 const char *devname;
064af421 2239
898bf89d
JP
2240 COVERAGE_INC(ofproto_reinit_ports);
2241
b3c01ed3 2242 sset_init(&devnames);
4e8e4213 2243 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1264ec95 2244 sset_add(&devnames, netdev_get_name(ofport->netdev));
064af421 2245 }
abe529af
BP
2246 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2247 sset_add(&devnames, ofproto_port.name);
064af421 2248 }
064af421 2249
b3c01ed3
BP
2250 SSET_FOR_EACH (devname, &devnames) {
2251 update_port(p, devname);
064af421 2252 }
b3c01ed3 2253 sset_destroy(&devnames);
064af421
BP
2254}
2255
4e022ec0 2256static ofp_port_t
e1b1d06a
JP
2257alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
2258{
4e022ec0 2259 uint16_t port_idx;
e1b1d06a 2260
4e022ec0 2261 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
430dbb14 2262 port_idx = port_idx ? port_idx : UINT16_MAX;
4e022ec0 2263
430dbb14 2264 if (port_idx >= ofproto->max_ports
fdcea803
GS
2265 || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) {
2266 uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no;
2267 long long int last_used_at, lru = LLONG_MAX;
e1b1d06a 2268
e1b1d06a
JP
2269 /* Search for a free OpenFlow port number. We try not to
2270 * immediately reuse them to prevent problems due to old
484c8355
BP
2271 * flows.
2272 *
2273 * We limit the automatically assigned port numbers to the lower half
2274 * of the port range, to reserve the upper half for assignment by
2275 * controllers. */
6033d9d9 2276 for (;;) {
484c8355 2277 if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) {
fdcea803 2278 ofproto->alloc_port_no = 1;
6033d9d9 2279 }
fdcea803
GS
2280 last_used_at = ofport_get_usage(ofproto,
2281 u16_to_ofp(ofproto->alloc_port_no));
2282 if (!last_used_at) {
430dbb14 2283 port_idx = ofproto->alloc_port_no;
6033d9d9 2284 break;
865b26a4
GS
2285 } else if ( last_used_at < time_msec() - 60*60*1000) {
2286 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
2287 * more than an hour ago, consider it usable. */
2288 ofport_remove_usage(ofproto,
2289 u16_to_ofp(ofproto->alloc_port_no));
2290 port_idx = ofproto->alloc_port_no;
2291 break;
fdcea803
GS
2292 } else if (last_used_at < lru) {
2293 lru = last_used_at;
2294 lru_ofport = ofproto->alloc_port_no;
e1b1d06a 2295 }
fdcea803 2296
430dbb14 2297 if (ofproto->alloc_port_no == end_port_no) {
fdcea803
GS
2298 if (lru_ofport) {
2299 port_idx = lru_ofport;
2300 break;
2301 }
6033d9d9 2302 return OFPP_NONE;
e1b1d06a
JP
2303 }
2304 }
2305 }
fdcea803 2306 ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX);
4e022ec0 2307 return u16_to_ofp(port_idx);
e1b1d06a
JP
2308}
2309
2310static void
fdcea803 2311dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port)
e1b1d06a 2312{
430dbb14 2313 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
fdcea803 2314 ofport_set_usage(ofproto, ofp_port, time_msec());
40fa9417 2315 }
e1b1d06a
JP
2316}
2317
fbfa2911
BP
2318/* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
2319 * pointer if the netdev cannot be opened. On success, also fills in
d7d92203 2320 * '*pp'. */
b33951b8 2321static struct netdev *
e1b1d06a
JP
2322ofport_open(struct ofproto *ofproto,
2323 struct ofproto_port *ofproto_port,
9e1fd49b 2324 struct ofputil_phy_port *pp)
064af421
BP
2325{
2326 enum netdev_flags flags;
064af421 2327 struct netdev *netdev;
064af421
BP
2328 int error;
2329
18812dff 2330 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
064af421 2331 if (error) {
94783c7c 2332 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu32") because netdev %s "
064af421 2333 "cannot be opened (%s)",
fbfa2911 2334 ofproto->name,
abe529af 2335 ofproto_port->name, ofproto_port->ofp_port,
10a89ef0 2336 ofproto_port->name, ovs_strerror(error));
064af421
BP
2337 return NULL;
2338 }
2339
e1b1d06a
JP
2340 if (ofproto_port->ofp_port == OFPP_NONE) {
2341 if (!strcmp(ofproto->name, ofproto_port->name)) {
2342 ofproto_port->ofp_port = OFPP_LOCAL;
2343 } else {
2344 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
2345 ofproto_port->name);
2346 }
2347 }
9e1fd49b 2348 pp->port_no = ofproto_port->ofp_port;
74ff3298 2349 netdev_get_etheraddr(netdev, &pp->hw_addr);
2f2b904f 2350 pp->hw_addr64 = eth_addr64_zero;
9e1fd49b 2351 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
064af421 2352 netdev_get_flags(netdev, &flags);
9e1fd49b
BP
2353 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
2354 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
2355 netdev_get_features(netdev, &pp->curr, &pp->advertised,
2356 &pp->supported, &pp->peer);
cd65125d
BP
2357 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2358 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
118c4676 2359
b33951b8 2360 return netdev;
064af421
BP
2361}
2362
b33951b8 2363/* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
d7d92203 2364 * port number, and 'config' bits other than OFPUTIL_PC_PORT_DOWN are
b33951b8
BP
2365 * disregarded. */
2366static bool
9e1fd49b
BP
2367ofport_equal(const struct ofputil_phy_port *a,
2368 const struct ofputil_phy_port *b)
064af421 2369{
9e1fd49b 2370 return (eth_addr_equals(a->hw_addr, b->hw_addr)
2f2b904f 2371 && eth_addr64_equals(a->hw_addr64, b->hw_addr64)
064af421 2372 && a->state == b->state
9e1fd49b 2373 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
064af421
BP
2374 && a->curr == b->curr
2375 && a->advertised == b->advertised
2376 && a->supported == b->supported
9e1fd49b
BP
2377 && a->peer == b->peer
2378 && a->curr_speed == b->curr_speed
2379 && a->max_speed == b->max_speed);
064af421
BP
2380}
2381
b33951b8
BP
2382/* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2383 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2384 * one with the same name or port number). */
01c77073 2385static int
b33951b8 2386ofport_install(struct ofproto *p,
9e1fd49b 2387 struct netdev *netdev, const struct ofputil_phy_port *pp)
064af421 2388{
b33951b8
BP
2389 const char *netdev_name = netdev_get_name(netdev);
2390 struct ofport *ofport;
abe529af 2391 int error;
72b06300 2392
b33951b8 2393 /* Create ofport. */
abe529af
BP
2394 ofport = p->ofproto_class->port_alloc();
2395 if (!ofport) {
2396 error = ENOMEM;
2397 goto error;
2398 }
0f7d71a5 2399 ofport->ofproto = p;
b33951b8 2400 ofport->netdev = netdev;
61501798 2401 ofport->change_seq = netdev_get_change_seq(netdev);
9e1fd49b
BP
2402 ofport->pp = *pp;
2403 ofport->ofp_port = pp->port_no;
65e0be10 2404 ofport->created = time_msec();
b33951b8
BP
2405
2406 /* Add port to 'p'. */
4e022ec0 2407 hmap_insert(&p->ports, &ofport->hmap_node,
f9c0c3ec 2408 hash_ofp_port(ofport->ofp_port));
72b06300 2409 shash_add(&p->port_by_name, netdev_name, ofport);
abe529af 2410
ada3428f 2411 update_mtu(p, ofport);
9197df76 2412
abe529af
BP
2413 /* Let the ofproto_class initialize its private data. */
2414 error = p->ofproto_class->port_construct(ofport);
2415 if (error) {
2416 goto error;
2417 }
2a6f78e0 2418 connmgr_send_port_status(p->connmgr, NULL, pp, OFPPR_ADD);
01c77073 2419 return 0;
abe529af
BP
2420
2421error:
2422 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
10a89ef0 2423 p->name, netdev_name, ovs_strerror(error));
abe529af
BP
2424 if (ofport) {
2425 ofport_destroy__(ofport);
2426 } else {
2427 netdev_close(netdev);
72b06300 2428 }
01c77073 2429 return error;
064af421
BP
2430}
2431
b33951b8 2432/* Removes 'ofport' from 'p' and destroys it. */
064af421 2433static void
0f7d71a5 2434ofport_remove(struct ofport *ofport)
064af421 2435{
0670b5d0 2436 struct ofproto *p = ofport->ofproto;
3a414a0a 2437 bool is_mtu_overridden = ofport_is_mtu_overridden(p, ofport);
0670b5d0 2438
2a6f78e0 2439 connmgr_send_port_status(ofport->ofproto->connmgr, NULL, &ofport->pp,
fa066f01 2440 OFPPR_DELETE);
9aad5a5a 2441 ofport_destroy(ofport, true);
3a414a0a 2442 if (!is_mtu_overridden) {
0670b5d0 2443 update_mtu_ofproto(p);
2444 }
b33951b8
BP
2445}
2446
2447/* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2448 * destroys it. */
2449static void
2450ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2451{
2452 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2453 if (port) {
0f7d71a5 2454 ofport_remove(port);
b33951b8
BP
2455 }
2456}
2457
9e1fd49b 2458/* Updates 'port' with new 'pp' description.
b33951b8
BP
2459 *
2460 * Does not handle a name or port number change. The caller must implement
2461 * such a change as a delete followed by an add. */
2462static void
9e1fd49b 2463ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
b33951b8 2464{
74ff3298 2465 port->pp.hw_addr = pp->hw_addr;
2f2b904f 2466 port->pp.hw_addr64 = pp->hw_addr64;
9e1fd49b
BP
2467 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2468 | (pp->config & OFPUTIL_PC_PORT_DOWN));
53fd5c7c
BP
2469 port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN)
2470 | (pp->state & OFPUTIL_PS_LINK_DOWN));
9e1fd49b
BP
2471 port->pp.curr = pp->curr;
2472 port->pp.advertised = pp->advertised;
2473 port->pp.supported = pp->supported;
2474 port->pp.peer = pp->peer;
2475 port->pp.curr_speed = pp->curr_speed;
2476 port->pp.max_speed = pp->max_speed;
064af421
BP
2477}
2478
5a2dfd47
JP
2479/* Update OpenFlow 'state' in 'port' and notify controller. */
2480void
9e1fd49b 2481ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
5a2dfd47 2482{
9e1fd49b
BP
2483 if (port->pp.state != state) {
2484 port->pp.state = state;
2a6f78e0
BP
2485 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2486 &port->pp, OFPPR_MODIFY);
5a2dfd47
JP
2487 }
2488}
2489
abe529af 2490void
4e022ec0 2491ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
e7934396 2492{
abe529af
BP
2493 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2494 if (port) {
b308140a
JP
2495 if (port->ofproto->ofproto_class->set_stp_port) {
2496 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2497 }
9efd308e
DV
2498 if (port->ofproto->ofproto_class->set_rstp_port) {
2499 port->ofproto->ofproto_class->set_rstp_port(port, NULL);
2500 }
abe529af 2501 if (port->ofproto->ofproto_class->set_cfm) {
a5610457 2502 port->ofproto->ofproto_class->set_cfm(port, NULL);
abe529af
BP
2503 }
2504 if (port->ofproto->ofproto_class->bundle_remove) {
2505 port->ofproto->ofproto_class->bundle_remove(port);
e7934396
BP
2506 }
2507 }
2508}
2509
2510static void
abe529af 2511ofport_destroy__(struct ofport *port)
e7934396 2512{
abe529af
BP
2513 struct ofproto *ofproto = port->ofproto;
2514 const char *name = netdev_get_name(port->netdev);
fa066f01 2515
abe529af 2516 hmap_remove(&ofproto->ports, &port->hmap_node);
5978c2e3 2517 shash_find_and_delete(&ofproto->port_by_name, name);
fa066f01 2518
abe529af
BP
2519 netdev_close(port->netdev);
2520 ofproto->ofproto_class->port_dealloc(port);
e7934396
BP
2521}
2522
064af421 2523static void
9aad5a5a 2524ofport_destroy(struct ofport *port, bool del)
064af421 2525{
fa066f01 2526 if (port) {
e1b1d06a 2527 dealloc_ofp_port(port->ofproto, port->ofp_port);
9aad5a5a 2528 port->ofproto->ofproto_class->port_destruct(port, del);
abe529af
BP
2529 ofport_destroy__(port);
2530 }
064af421
BP
2531}
2532
abe529af 2533struct ofport *
4e022ec0 2534ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
ca0f572c
BP
2535{
2536 struct ofport *port;
2537
f9c0c3ec 2538 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
4e022ec0 2539 &ofproto->ports) {
abe529af 2540 if (port->ofp_port == ofp_port) {
ca0f572c
BP
2541 return port;
2542 }
2543 }
2544 return NULL;
2545}
2546
fdcea803
GS
2547static long long int
2548ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port)
2549{
2550 struct ofport_usage *usage;
2551
2552 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2553 &ofproto->ofport_usage) {
2554 if (usage->ofp_port == ofp_port) {
2555 return usage->last_used;
2556 }
2557 }
2558 return 0;
2559}
2560
2561static void
2562ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port,
2563 long long int last_used)
2564{
2565 struct ofport_usage *usage;
2566 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2567 &ofproto->ofport_usage) {
2568 if (usage->ofp_port == ofp_port) {
2569 usage->last_used = last_used;
2570 return;
2571 }
2572 }
2573 ovs_assert(last_used == LLONG_MAX);
2574
2575 usage = xmalloc(sizeof *usage);
2576 usage->ofp_port = ofp_port;
2577 usage->last_used = last_used;
2578 hmap_insert(&ofproto->ofport_usage, &usage->hmap_node,
2579 hash_ofp_port(ofp_port));
2580}
2581
865b26a4
GS
2582static void
2583ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port)
2584{
2585 struct ofport_usage *usage;
2586 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2587 &ofproto->ofport_usage) {
2588 if (usage->ofp_port == ofp_port) {
2589 hmap_remove(&ofproto->ofport_usage, &usage->hmap_node);
2590 free(usage);
2591 break;
2592 }
2593 }
2594}
2595
6527c598
PS
2596int
2597ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2598{
2599 struct ofproto *ofproto = port->ofproto;
2600 int error;
2601
2602 if (ofproto->ofproto_class->port_get_stats) {
2603 error = ofproto->ofproto_class->port_get_stats(port, stats);
2604 } else {
2605 error = EOPNOTSUPP;
2606 }
2607
2608 return error;
2609}
2610
01c77073 2611static int
b33951b8 2612update_port(struct ofproto *ofproto, const char *name)
064af421 2613{
abe529af 2614 struct ofproto_port ofproto_port;
9e1fd49b 2615 struct ofputil_phy_port pp;
b33951b8
BP
2616 struct netdev *netdev;
2617 struct ofport *port;
01c77073 2618 int error = 0;
064af421
BP
2619
2620 COVERAGE_INC(ofproto_update_port);
c874dc6d 2621
b33951b8 2622 /* Fetch 'name''s location and properties from the datapath. */
abe529af 2623 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
fbfa2911 2624 ? ofport_open(ofproto, &ofproto_port, &pp)
b33951b8 2625 : NULL);
4e022ec0 2626
b33951b8 2627 if (netdev) {
abe529af 2628 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
b33951b8 2629 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
e65942a0
BP
2630 struct netdev *old_netdev = port->netdev;
2631
b33951b8 2632 /* 'name' hasn't changed location. Any properties changed? */
f73b83fd
LS
2633 bool port_changed = !ofport_equal(&port->pp, &pp);
2634 if (port_changed) {
9e1fd49b 2635 ofport_modified(port, &pp);
abe529af
BP
2636 }
2637
ada3428f 2638 update_mtu(ofproto, port);
9197df76 2639
e65942a0
BP
2640 /* Install the newly opened netdev in case it has changed.
2641 * Don't close the old netdev yet in case port_modified has to
2642 * remove a retained reference to it.*/
abe529af 2643 port->netdev = netdev;
61501798 2644 port->change_seq = netdev_get_change_seq(netdev);
abe529af
BP
2645
2646 if (port->ofproto->ofproto_class->port_modified) {
2647 port->ofproto->ofproto_class->port_modified(port);
b33951b8 2648 }
e65942a0 2649
f73b83fd
LS
2650 /* Send status update, if any port property changed */
2651 if (port_changed) {
2652 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2653 &port->pp, OFPPR_MODIFY);
2654 }
2655
e65942a0 2656 netdev_close(old_netdev);
b33951b8
BP
2657 } else {
2658 /* If 'port' is nonnull then its name differs from 'name' and thus
2659 * we should delete it. If we think there's a port named 'name'
2660 * then its port number must be wrong now so delete it too. */
2661 if (port) {
0f7d71a5 2662 ofport_remove(port);
b33951b8
BP
2663 }
2664 ofport_remove_with_name(ofproto, name);
01c77073 2665 error = ofport_install(ofproto, netdev, &pp);
c874dc6d 2666 }
b33951b8
BP
2667 } else {
2668 /* Any port named 'name' is gone now. */
2669 ofport_remove_with_name(ofproto, name);
c874dc6d 2670 }
abe529af 2671 ofproto_port_destroy(&ofproto_port);
01c77073
BP
2672
2673 return error;
064af421
BP
2674}
2675
2676static int
2677init_ports(struct ofproto *p)
2678{
abe529af
BP
2679 struct ofproto_port_dump dump;
2680 struct ofproto_port ofproto_port;
e1b1d06a 2681 struct shash_node *node, *next;
abe529af
BP
2682
2683 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
e1b1d06a
JP
2684 const char *name = ofproto_port.name;
2685
2686 if (shash_find(&p->port_by_name, name)) {
fbfa2911 2687 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
e1b1d06a 2688 p->name, name);
abe529af 2689 } else {
9e1fd49b 2690 struct ofputil_phy_port pp;
b33951b8
BP
2691 struct netdev *netdev;
2692
e1b1d06a
JP
2693 /* Check if an OpenFlow port number had been requested. */
2694 node = shash_find(&init_ofp_ports, name);
2695 if (node) {
2696 const struct iface_hint *iface_hint = node->data;
4e022ec0
AW
2697 simap_put(&p->ofp_requests, name,
2698 ofp_to_u16(iface_hint->ofp_port));
e1b1d06a
JP
2699 }
2700
fbfa2911 2701 netdev = ofport_open(p, &ofproto_port, &pp);
b33951b8 2702 if (netdev) {
9e1fd49b 2703 ofport_install(p, netdev, &pp);
430dbb14 2704 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
7c35397c 2705 p->alloc_port_no = MAX(p->alloc_port_no,
430dbb14 2706 ofp_to_u16(ofproto_port.ofp_port));
7c35397c 2707 }
064af421
BP
2708 }
2709 }
2710 }
b0ec0f27 2711
e1b1d06a 2712 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
4f9e08a5 2713 struct iface_hint *iface_hint = node->data;
e1b1d06a
JP
2714
2715 if (!strcmp(iface_hint->br_name, p->name)) {
2716 free(iface_hint->br_name);
2717 free(iface_hint->br_type);
4f9e08a5 2718 free(iface_hint);
e1b1d06a
JP
2719 shash_delete(&init_ofp_ports, node);
2720 }
2721 }
2722
064af421
BP
2723 return 0;
2724}
9197df76 2725
3a414a0a 2726static bool
8c319e8b 2727ofport_is_internal_or_patch(const struct ofproto *p, const struct ofport *port)
0670b5d0 2728{
86fce577 2729 return !strcmp(netdev_get_type(port->netdev),
8c319e8b
NS
2730 ofproto_port_open_type(p->type, "internal")) ||
2731 !strcmp(netdev_get_type(port->netdev),
2732 ofproto_port_open_type(p->type, "patch"));
0670b5d0 2733}
2734
8c319e8b
NS
2735/* If 'port' is internal or patch and if the user didn't explicitly specify an
2736 * mtu through the database, we have to override it. */
3a414a0a
DDP
2737static bool
2738ofport_is_mtu_overridden(const struct ofproto *p, const struct ofport *port)
2739{
8c319e8b 2740 return ofport_is_internal_or_patch(p, port)
3a414a0a
DDP
2741 && !netdev_mtu_is_user_config(port->netdev);
2742}
2743
2744/* Find the minimum MTU of all non-overridden devices attached to 'p'.
9197df76
JP
2745 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2746static int
2747find_min_mtu(struct ofproto *p)
2748{
2749 struct ofport *ofport;
2750 int mtu = 0;
2751
2752 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2753 struct netdev *netdev = ofport->netdev;
2754 int dev_mtu;
2755
3a414a0a
DDP
2756 /* Skip any overridden port, since that's what we're trying to set. */
2757 if (ofport_is_mtu_overridden(p, ofport)) {
9197df76
JP
2758 continue;
2759 }
2760
2761 if (netdev_get_mtu(netdev, &dev_mtu)) {
2762 continue;
2763 }
2764 if (!mtu || dev_mtu < mtu) {
2765 mtu = dev_mtu;
2766 }
2767 }
2768
2769 return mtu ? mtu: ETH_PAYLOAD_MAX;
2770}
2771
3a414a0a
DDP
2772/* Update MTU of all overridden devices on 'p' to the minimum of the
2773 * non-overridden ports in event of 'port' added or changed. */
9197df76 2774static void
ada3428f 2775update_mtu(struct ofproto *p, struct ofport *port)
9197df76 2776{
ada3428f 2777 struct netdev *netdev = port->netdev;
0670b5d0 2778 int dev_mtu;
ada3428f
PS
2779
2780 if (netdev_get_mtu(netdev, &dev_mtu)) {
2781 port->mtu = 0;
2782 return;
2783 }
3a414a0a 2784 if (ofport_is_mtu_overridden(p, port)) {
7478b5a2 2785 if (dev_mtu > p->min_mtu) {
3a414a0a
DDP
2786 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2787 dev_mtu = p->min_mtu;
2788 }
ada3428f
PS
2789 }
2790 port->mtu = dev_mtu;
2791 return;
2792 }
2793
ada3428f 2794 port->mtu = dev_mtu;
3a414a0a
DDP
2795 /* For non-overridden port find new min mtu. */
2796
0670b5d0 2797 update_mtu_ofproto(p);
2798}
2799
2800static void
2801update_mtu_ofproto(struct ofproto *p)
2802{
0670b5d0 2803 struct ofport *ofport;
2804 int old_min = p->min_mtu;
2805
ada3428f
PS
2806 p->min_mtu = find_min_mtu(p);
2807 if (p->min_mtu == old_min) {
2808 return;
2809 }
9197df76
JP
2810
2811 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2812 struct netdev *netdev = ofport->netdev;
2813
3a414a0a 2814 if (ofport_is_mtu_overridden(p, ofport)) {
ada3428f
PS
2815 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2816 ofport->mtu = p->min_mtu;
2817 }
9197df76
JP
2818 }
2819 }
2820}
064af421 2821\f
f416c8d6
JR
2822static void
2823ofproto_rule_destroy__(struct rule *rule)
2824 OVS_NO_THREAD_SAFETY_ANALYSIS
2825{
2826 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2827 rule_actions_destroy(rule_get_actions(rule));
2828 ovs_mutex_destroy(&rule->mutex);
2829 rule->ofproto->ofproto_class->rule_dealloc(rule);
2830}
2831
2832static void
2833rule_destroy_cb(struct rule *rule)
f695ebfa 2834 OVS_NO_THREAD_SAFETY_ANALYSIS
f416c8d6 2835{
f695ebfa
JR
2836 /* Send rule removed if needed. */
2837 if (rule->flags & OFPUTIL_FF_SEND_FLOW_REM
2838 && rule->removed_reason != OVS_OFPRR_NONE
2839 && !rule_is_hidden(rule)) {
2840 ofproto_rule_send_removed(rule);
2841 }
f416c8d6 2842 rule->ofproto->ofproto_class->rule_destruct(rule);
5c7c16d8
YHW
2843 mf_vl_mff_unref(&rule->ofproto->vl_mff_map, rule->match_tlv_bitmap);
2844 mf_vl_mff_unref(&rule->ofproto->vl_mff_map, rule->ofpacts_tlv_bitmap);
f416c8d6
JR
2845 ofproto_rule_destroy__(rule);
2846}
2847
a2143702
BP
2848void
2849ofproto_rule_ref(struct rule *rule)
064af421 2850{
1eae3d33 2851 if (rule) {
37bec3d3 2852 ovs_refcount_ref(&rule->ref_count);
a2143702
BP
2853 }
2854}
2855
f5d16e55
JR
2856bool
2857ofproto_rule_try_ref(struct rule *rule)
2858{
2859 if (rule) {
2860 return ovs_refcount_try_ref_rcu(&rule->ref_count);
2861 }
2862 return false;
2863}
2864
f416c8d6
JR
2865/* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the
2866 * ref_count reaches 0.
2867 *
2868 * Use of RCU allows short term use (between RCU quiescent periods) without
2869 * keeping a reference. A reference must be taken if the rule needs to
2870 * stay around accross the RCU quiescent periods. */
a2143702
BP
2871void
2872ofproto_rule_unref(struct rule *rule)
2873{
24f83812 2874 if (rule && ovs_refcount_unref_relaxed(&rule->ref_count) == 1) {
2c7ee524 2875 ovs_assert(rule->state != RULE_INSERTED);
f416c8d6 2876 ovsrcu_postpone(rule_destroy_cb, rule);
1eae3d33 2877 }
064af421
BP
2878}
2879
39c94593
JR
2880static void
2881remove_rule_rcu__(struct rule *rule)
2882 OVS_REQUIRES(ofproto_mutex)
2883{
2884 struct ofproto *ofproto = rule->ofproto;
2885 struct oftable *table = &ofproto->tables[rule->table_id];
2886
44e0c35d 2887 ovs_assert(!cls_rule_visible_in_version(&rule->cr, OVS_VERSION_MAX));
39c94593
JR
2888 if (!classifier_remove(&table->cls, &rule->cr)) {
2889 OVS_NOT_REACHED();
2890 }
1fc71871
JR
2891 if (ofproto->ofproto_class->rule_delete) {
2892 ofproto->ofproto_class->rule_delete(rule);
2893 }
39c94593
JR
2894 ofproto_rule_unref(rule);
2895}
2896
2897static void
2898remove_rule_rcu(struct rule *rule)
2899 OVS_EXCLUDED(ofproto_mutex)
2900{
2901 ovs_mutex_lock(&ofproto_mutex);
2902 remove_rule_rcu__(rule);
2903 ovs_mutex_unlock(&ofproto_mutex);
2904}
2905
2906/* Removes and deletes rules from a NULL-terminated array of rule pointers. */
2907static void
2908remove_rules_rcu(struct rule **rules)
2909 OVS_EXCLUDED(ofproto_mutex)
2910{
2911 struct rule **orig_rules = rules;
2912
2913 if (*rules) {
2914 struct ofproto *ofproto = rules[0]->ofproto;
2915 unsigned long tables[BITMAP_N_LONGS(256)];
2916 struct rule *rule;
2917 size_t table_id;
2918
2919 memset(tables, 0, sizeof tables);
2920
2921 ovs_mutex_lock(&ofproto_mutex);
2922 while ((rule = *rules++)) {
2923 /* Defer once for each new table. This defers the subtable cleanup
2924 * until later, so that when removing large number of flows the
2925 * operation is faster. */
2926 if (!bitmap_is_set(tables, rule->table_id)) {
2927 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
2928
2929 bitmap_set1(tables, rule->table_id);
2930 classifier_defer(cls);
2931 }
2932 remove_rule_rcu__(rule);
2933 }
2934
2935 BITMAP_FOR_EACH_1(table_id, 256, tables) {
2936 struct classifier *cls = &ofproto->tables[table_id].cls;
2937
2938 classifier_publish(cls);
2939 }
2940 ovs_mutex_unlock(&ofproto_mutex);
2941 }
2942
2943 free(orig_rules);
2944}
2945
809c7548
RW
2946void
2947ofproto_group_ref(struct ofgroup *group)
2948{
2949 if (group) {
2950 ovs_refcount_ref(&group->ref_count);
2951 }
2952}
2953
db88b35c
JR
2954bool
2955ofproto_group_try_ref(struct ofgroup *group)
2956{
2957 if (group) {
2958 return ovs_refcount_try_ref_rcu(&group->ref_count);
2959 }
2960 return false;
2961}
2962
2963static void
2964group_destroy_cb(struct ofgroup *group)
2965{
2966 group->ofproto->ofproto_class->group_destruct(group);
e8dba719
JR
2967 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *,
2968 &group->props));
db88b35c
JR
2969 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *,
2970 &group->buckets));
2971 group->ofproto->ofproto_class->group_dealloc(group);
2972}
2973
809c7548
RW
2974void
2975ofproto_group_unref(struct ofgroup *group)
cc099268 2976 OVS_NO_THREAD_SAFETY_ANALYSIS
809c7548 2977{
db88b35c 2978 if (group && ovs_refcount_unref_relaxed(&group->ref_count) == 1) {
fe59694b 2979 ovs_assert(rule_collection_n(&group->rules) == 0);
db88b35c 2980 ovsrcu_postpone(group_destroy_cb, group);
809c7548
RW
2981 }
2982}
2983
5d08a275
JR
2984static void
2985remove_group_rcu__(struct ofgroup *group)
2986 OVS_REQUIRES(ofproto_mutex)
2987{
2988 struct ofproto *ofproto = group->ofproto;
2989
2990 ovs_assert(!versions_visible_in_version(&group->versions, OVS_VERSION_MAX));
2991
2992 cmap_remove(&ofproto->groups, &group->cmap_node,
2993 hash_int(group->group_id, 0));
2994 ofproto_group_unref(group);
2995}
2996
2997static void
2998remove_group_rcu(struct ofgroup *group)
2999 OVS_EXCLUDED(ofproto_mutex)
3000{
3001 ovs_mutex_lock(&ofproto_mutex);
3002 remove_group_rcu__(group);
3003 ovs_mutex_unlock(&ofproto_mutex);
3004}
3005
3006/* Removes and deletes groups from a NULL-terminated array of group
3007 * pointers. */
3008static void
3009remove_groups_rcu(struct ofgroup **groups)
3010 OVS_EXCLUDED(ofproto_mutex)
3011{
3012 ovs_mutex_lock(&ofproto_mutex);
3013 for (struct ofgroup **g = groups; *g; g++) {
3014 remove_group_rcu__(*g);
3015 }
3016 ovs_mutex_unlock(&ofproto_mutex);
3017 free(groups);
3018}
3019
076caa2f
JR
3020static bool ofproto_fix_meter_action(const struct ofproto *,
3021 struct ofpact_meter *);
65efd2ab 3022
dc723c44
JR
3023/* Creates and returns a new 'struct rule_actions', whose actions are a copy
3024 * of from the 'ofpacts_len' bytes of 'ofpacts'. */
3025const struct rule_actions *
4c7562c5 3026rule_actions_create(const struct ofpact *ofpacts, size_t ofpacts_len)
6f00e29b
BP
3027{
3028 struct rule_actions *actions;
3029
dc723c44 3030 actions = xmalloc(sizeof *actions + ofpacts_len);
6f00e29b 3031 actions->ofpacts_len = ofpacts_len;
dc723c44 3032 memcpy(actions->ofpacts, ofpacts, ofpacts_len);
cc099268
JR
3033 actions->has_meter = ofpacts_get_meter(ofpacts, ofpacts_len) != 0;
3034 actions->has_groups =
3035 (ofpact_find_type_flattened(ofpacts, OFPACT_GROUP,
3036 ofpact_end(ofpacts, ofpacts_len))
3037 != NULL);
35f48b8b
BP
3038 actions->has_learn_with_delete = (next_learn_with_delete(actions, NULL)
3039 != NULL);
3040
6f00e29b
BP
3041 return actions;
3042}
3043
dc723c44 3044/* Free the actions after the RCU quiescent period is reached. */
6f00e29b 3045void
dc723c44 3046rule_actions_destroy(const struct rule_actions *actions)
6f00e29b 3047{
06a0f3e2 3048 if (actions) {
dc723c44 3049 ovsrcu_postpone(free, CONST_CAST(struct rule_actions *, actions));
6f00e29b
BP
3050 }
3051}
3052
bcf84111 3053/* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
f25d0cf3 3054 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
cdbdeeda 3055bool
4e022ec0 3056ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
15aaf599 3057 OVS_REQUIRES(ofproto_mutex)
064af421 3058{
06a0f3e2
BP
3059 if (port == OFPP_ANY) {
3060 return true;
3061 } else {
3062 const struct rule_actions *actions = rule_get_actions(rule);
3063 return ofpacts_output_to_port(actions->ofpacts,
3064 actions->ofpacts_len, port);
3065 }
064af421
BP
3066}
3067
7395c052 3068/* Returns true if 'rule' has group and equals group_id. */
74e79b7c 3069static bool
7395c052 3070ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
15aaf599 3071 OVS_REQUIRES(ofproto_mutex)
7395c052 3072{
06a0f3e2
BP
3073 if (group_id == OFPG_ANY) {
3074 return true;
3075 } else {
3076 const struct rule_actions *actions = rule_get_actions(rule);
3077 return ofpacts_output_to_group(actions->ofpacts,
3078 actions->ofpacts_len, group_id);
3079 }
7395c052
NZ
3080}
3081
adcf00ba 3082static bool
834fe5cb 3083rule_is_readonly(const struct rule *rule)
5c67e4af 3084{
834fe5cb
BP
3085 const struct oftable *table = &rule->ofproto->tables[rule->table_id];
3086 return (table->flags & OFTABLE_READONLY) != 0;
5c67e4af 3087}
fa066f01 3088\f
35f48b8b
BP
3089static uint32_t
3090hash_learned_cookie(ovs_be64 cookie_, uint8_t table_id)
3091{
3092 uint64_t cookie = (OVS_FORCE uint64_t) cookie_;
3093 return hash_3words(cookie, cookie >> 32, table_id);
3094}
3095
3096static void
3097learned_cookies_update_one__(struct ofproto *ofproto,
3098 const struct ofpact_learn *learn,
ca6ba700 3099 int delta, struct ovs_list *dead_cookies)
35f48b8b
BP
3100 OVS_REQUIRES(ofproto_mutex)
3101{
3102 uint32_t hash = hash_learned_cookie(learn->cookie, learn->table_id);
3103 struct learned_cookie *c;
3104
3105 HMAP_FOR_EACH_WITH_HASH (c, u.hmap_node, hash, &ofproto->learned_cookies) {
3106 if (c->cookie == learn->cookie && c->table_id == learn->table_id) {
3107 c->n += delta;
3108 ovs_assert(c->n >= 0);
3109
3110 if (!c->n) {
3111 hmap_remove(&ofproto->learned_cookies, &c->u.hmap_node);
417e7e66 3112 ovs_list_push_back(dead_cookies, &c->u.list_node);
35f48b8b
BP
3113 }
3114
3115 return;
3116 }
3117 }
3118
3119 ovs_assert(delta > 0);
3120 c = xmalloc(sizeof *c);
3121 hmap_insert(&ofproto->learned_cookies, &c->u.hmap_node, hash);
3122 c->cookie = learn->cookie;
3123 c->table_id = learn->table_id;
3124 c->n = delta;
3125}
3126
3127static const struct ofpact_learn *
3128next_learn_with_delete(const struct rule_actions *actions,
3129 const struct ofpact_learn *start)
3130{
3131 const struct ofpact *pos;
3132
3133 for (pos = start ? ofpact_next(&start->ofpact) : actions->ofpacts;
3134 pos < ofpact_end(actions->ofpacts, actions->ofpacts_len);
3135 pos = ofpact_next(pos)) {
3136 if (pos->type == OFPACT_LEARN) {
3137 const struct ofpact_learn *learn = ofpact_get_LEARN(pos);
3138 if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
3139 return learn;
3140 }
3141 }
3142 }
3143
3144 return NULL;
3145}
3146
3147static void
3148learned_cookies_update__(struct ofproto *ofproto,
3149 const struct rule_actions *actions,
ca6ba700 3150 int delta, struct ovs_list *dead_cookies)
35f48b8b
BP
3151 OVS_REQUIRES(ofproto_mutex)
3152{
3153 if (actions->has_learn_with_delete) {
3154 const struct ofpact_learn *learn;
3155
3156 for (learn = next_learn_with_delete(actions, NULL); learn;
3157 learn = next_learn_with_delete(actions, learn)) {
3158 learned_cookies_update_one__(ofproto, learn, delta, dead_cookies);
3159 }
3160 }
3161}
3162
3163static void
3164learned_cookies_inc(struct ofproto *ofproto,
3165 const struct rule_actions *actions)
3166 OVS_REQUIRES(ofproto_mutex)
3167{
3168 learned_cookies_update__(ofproto, actions, +1, NULL);
3169}
3170
3171static void
3172learned_cookies_dec(struct ofproto *ofproto,
3173 const struct rule_actions *actions,
ca6ba700 3174 struct ovs_list *dead_cookies)
35f48b8b
BP
3175 OVS_REQUIRES(ofproto_mutex)
3176{
3177 learned_cookies_update__(ofproto, actions, -1, dead_cookies);
3178}
3179
3180static void
ca6ba700 3181learned_cookies_flush(struct ofproto *ofproto, struct ovs_list *dead_cookies)
35f48b8b
BP
3182 OVS_REQUIRES(ofproto_mutex)
3183{
5f03c983 3184 struct learned_cookie *c;
35f48b8b 3185
5f03c983 3186 LIST_FOR_EACH_POP (c, u.list_node, dead_cookies) {
35f48b8b
BP
3187 struct rule_criteria criteria;
3188 struct rule_collection rules;
3189 struct match match;
3190
3191 match_init_catchall(&match);
44e0c35d 3192 rule_criteria_init(&criteria, c->table_id, &match, 0, OVS_VERSION_MAX,
35f48b8b
BP
3193 c->cookie, OVS_BE64_MAX, OFPP_ANY, OFPG_ANY);
3194 rule_criteria_require_rw(&criteria, false);
3195 collect_rules_loose(ofproto, &criteria, &rules);
35f48b8b 3196 rule_criteria_destroy(&criteria);
39c94593 3197 delete_flows__(&rules, OFPRR_DELETE, NULL);
35f48b8b 3198
35f48b8b
BP
3199 free(c);
3200 }
3201}
3202\f
90bf1e07 3203static enum ofperr
d1e2cf21 3204handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 3205{
b0421aa2 3206 ofconn_send_reply(ofconn, make_echo_reply(oh));
064af421 3207 return 0;
064af421
BP
3208}
3209
3c1bb396
BP
3210static void
3211query_tables(struct ofproto *ofproto,
3212 struct ofputil_table_features **featuresp,
3213 struct ofputil_table_stats **statsp)
3214{
178742f9
BP
3215 struct mf_bitmap rw_fields = oxm_writable_fields();
3216 struct mf_bitmap match = oxm_matchable_fields();
3217 struct mf_bitmap mask = oxm_maskable_fields();
3c1bb396
BP
3218
3219 struct ofputil_table_features *features;
3220 struct ofputil_table_stats *stats;
3221 int i;
3222
3c1bb396
BP
3223 features = *featuresp = xcalloc(ofproto->n_tables, sizeof *features);
3224 for (i = 0; i < ofproto->n_tables; i++) {
3225 struct ofputil_table_features *f = &features[i];
3226
3227 f->table_id = i;
3228 sprintf(f->name, "table%d", i);
3229 f->metadata_match = OVS_BE64_MAX;
3230 f->metadata_write = OVS_BE64_MAX;
afcea9ea 3231 atomic_read_relaxed(&ofproto->tables[i].miss_config, &f->miss_config);
3c1bb396
BP
3232 f->max_entries = 1000000;
3233
5dc7516b
BP
3234 bool more_tables = false;
3235 for (int j = i + 1; j < ofproto->n_tables; j++) {
3236 if (!(ofproto->tables[j].flags & OFTABLE_HIDDEN)) {
3237 bitmap_set1(f->nonmiss.next, j);
3238 more_tables = true;
3239 }
3240 }
3c1bb396 3241 f->nonmiss.instructions = (1u << N_OVS_INSTRUCTIONS) - 1;
5dc7516b 3242 if (!more_tables) {
3c1bb396
BP
3243 f->nonmiss.instructions &= ~(1u << OVSINST_OFPIT11_GOTO_TABLE);
3244 }
3245 f->nonmiss.write.ofpacts = (UINT64_C(1) << N_OFPACTS) - 1;
3246 f->nonmiss.write.set_fields = rw_fields;
3247 f->nonmiss.apply = f->nonmiss.write;
3248 f->miss = f->nonmiss;
3249
3250 f->match = match;
3251 f->mask = mask;
3252 f->wildcard = match;
3253 }
3254
3255 if (statsp) {
3256 stats = *statsp = xcalloc(ofproto->n_tables, sizeof *stats);
3257 for (i = 0; i < ofproto->n_tables; i++) {
3258 struct ofputil_table_stats *s = &stats[i];
3c1bb396
BP
3259
3260 s->table_id = i;
d79e3d70 3261 s->active_count = ofproto->tables[i].n_flows;
3fbbcba7
BP
3262 if (i == 0) {
3263 s->active_count -= connmgr_count_hidden_rules(
3264 ofproto->connmgr);
3265 }
3c1bb396
BP
3266 }
3267 } else {
3268 stats = NULL;
3269 }
3270
3271 ofproto->ofproto_class->query_tables(ofproto, features, stats);
3272
3273 for (i = 0; i < ofproto->n_tables; i++) {
3274 const struct oftable *table = &ofproto->tables[i];
3275 struct ofputil_table_features *f = &features[i];
3276
3277 if (table->name) {
3278 ovs_strzcpy(f->name, table->name, sizeof f->name);
3279 }
3280
3281 if (table->max_flows < f->max_entries) {
3282 f->max_entries = table->max_flows;
3283 }
3284 }
3285}
3286
3287static void
3288query_switch_features(struct ofproto *ofproto,
3289 bool *arp_match_ip, uint64_t *ofpacts)
3290{
3291 struct ofputil_table_features *features, *f;
3292
3293 *arp_match_ip = false;
3294 *ofpacts = 0;
3295
3296 query_tables(ofproto, &features, NULL);
3297 for (f = features; f < &features[ofproto->n_tables]; f++) {
3298 *ofpacts |= f->nonmiss.apply.ofpacts | f->miss.apply.ofpacts;
3299 if (bitmap_is_set(f->match.bm, MFF_ARP_SPA) ||
3300 bitmap_is_set(f->match.bm, MFF_ARP_TPA)) {
3301 *arp_match_ip = true;
3302 }
3303 }
3304 free(features);
3305
3306 /* Sanity check. */
3307 ovs_assert(*ofpacts & (UINT64_C(1) << OFPACT_OUTPUT));
3308}
3309
90bf1e07 3310static enum ofperr
d1e2cf21 3311handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 3312{
64ff1d96 3313 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
9e1fd49b 3314 struct ofputil_switch_features features;
064af421 3315 struct ofport *port;
6c1491fb 3316 bool arp_match_ip;
9e1fd49b 3317 struct ofpbuf *b;
064af421 3318
3c1bb396 3319 query_switch_features(ofproto, &arp_match_ip, &features.ofpacts);
064af421 3320
9e1fd49b 3321 features.datapath_id = ofproto->datapath_id;
c184807c 3322 features.n_buffers = 0;
adcf00ba 3323 features.n_tables = ofproto_get_n_visible_tables(ofproto);
9e1fd49b 3324 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
4efe455d 3325 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS |
50b73fe1 3326 OFPUTIL_C_GROUP_STATS | OFPUTIL_C_BUNDLES);
6c1491fb 3327 if (arp_match_ip) {
9e1fd49b 3328 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
6c1491fb 3329 }
2e1ae200
JR
3330 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
3331 features.auxiliary_id = 0;
9e1fd49b
BP
3332 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
3333 oh->xid);
64ff1d96 3334 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
9e1fd49b 3335 ofputil_put_switch_features_port(&port->pp, b);
064af421 3336 }
064af421 3337
9e1fd49b 3338 ofconn_send_reply(ofconn, b);
064af421
BP
3339 return 0;
3340}
064af421 3341
90bf1e07 3342static enum ofperr
d1e2cf21 3343handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 3344{
ad99e2ed
BP
3345 struct ofputil_switch_config config;
3346 config.frag = ofconn_get_ofproto(ofconn)->frag_handling;
3347 config.invalid_ttl_to_controller
3348 = ofconn_get_invalid_ttl_to_controller(ofconn);
3349 config.miss_send_len = ofconn_get_miss_send_len(ofconn);
3350
3351 ofconn_send_reply(ofconn, ofputil_encode_get_config_reply(oh, &config));
2d70a31a 3352
064af421
BP
3353 return 0;
3354}
064af421 3355
90bf1e07 3356static enum ofperr
982697a4 3357handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 3358{
64ff1d96 3359 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
ad99e2ed
BP
3360 struct ofputil_switch_config config;
3361 enum ofperr error;
3362
3363 error = ofputil_decode_set_config(oh, &config);
3364 if (error) {
3365 return error;
3366 }
2d70a31a 3367
7257b535 3368 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
f4f1ea7e 3369 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
ad99e2ed
BP
3370 enum ofputil_frag_handling cur = ofproto->frag_handling;
3371 enum ofputil_frag_handling next = config.frag;
7257b535 3372
7257b535
BP
3373 if (cur != next) {
3374 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
3375 ofproto->frag_handling = next;
3376 } else {
3377 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
3378 ofproto->name,
3379 ofputil_frag_handling_to_string(next));
3380 }
064af421
BP
3381 }
3382 }
ebe482fd 3383
ad99e2ed
BP
3384 if (config.invalid_ttl_to_controller >= 0) {
3385 ofconn_set_invalid_ttl_to_controller(ofconn,
3386 config.invalid_ttl_to_controller);
3387 }
3388
3389 ofconn_set_miss_send_len(ofconn, config.miss_send_len);
0ad9b732 3390
064af421 3391 return 0;
064af421
BP
3392}
3393
9deba63b 3394/* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
90bf1e07
BP
3395 * error message code for the caller to propagate upward. Otherwise, returns
3396 * 0.
3397 *
3398 * The log message mentions 'msg_type'. */
3399static enum ofperr
3400reject_slave_controller(struct ofconn *ofconn)
9deba63b 3401{
1ce0a5fa 3402 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
f4f1ea7e 3403 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
20e4ec1a 3404 return OFPERR_OFPBRC_IS_SLAVE;
9deba63b
BP
3405 } else {
3406 return 0;
3407 }
3408}
3409
7e9f8266
BP
3410/* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
3411 * for 'ofproto':
3412 *
3413 * - If they use a meter, then 'ofproto' has that meter configured.
076caa2f 3414 * Updates the meter action with ofproto's datapath's provider_meter_id.
7e9f8266
BP
3415 *
3416 * - If they use any groups, then 'ofproto' has that group configured.
3417 *
1f4a8933
JR
3418 * Returns 0 if successful, otherwise an OpenFlow error. Caller must hold
3419 * 'ofproto_mutex' for the result to be valid also after this function
3420 * returns. */
89108874 3421enum ofperr
9cae45dc 3422ofproto_check_ofpacts(struct ofproto *ofproto,
7e9f8266 3423 const struct ofpact ofpacts[], size_t ofpacts_len)
1f4a8933 3424 OVS_REQUIRES(ofproto_mutex)
9cae45dc 3425{
076caa2f 3426 const struct ofpact *a;
9cae45dc 3427
076caa2f
JR
3428 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
3429 if (a->type == OFPACT_METER &&
3430 !ofproto_fix_meter_action(ofproto, ofpact_get_METER(a))) {
3431 return OFPERR_OFPMMFC_INVALID_METER;
3432 }
9cae45dc 3433
076caa2f
JR
3434 if (a->type == OFPACT_GROUP
3435 && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) {
7e9f8266 3436 return OFPERR_OFPBAC_BAD_OUT_GROUP;
84d68566
SH
3437 }
3438 }
3439
9cae45dc
JR
3440 return 0;
3441}
3442
1f4a8933
JR
3443void
3444ofproto_packet_out_uninit(struct ofproto_packet_out *opo)
3445{
3446 dp_packet_delete(opo->packet);
3447 opo->packet = NULL;
3448 free(opo->flow);
3449 opo->flow = NULL;
3450 free(opo->ofpacts);
3451 opo->ofpacts = NULL;
3452 opo->ofpacts_len = 0;
3453 ovs_assert(!opo->aux);
3454}
3455
3456/* Takes ownership of po->ofpacts, which must have been malloc'ed. */
3457static enum ofperr
3458ofproto_packet_out_init(struct ofproto *ofproto,
3459 struct ofconn *ofconn,
3460 struct ofproto_packet_out *opo,
3461 const struct ofputil_packet_out *po)
3462{
3463 enum ofperr error;
67210a55 3464 struct match match;
d7892c81
YHW
3465 struct {
3466 struct miniflow mf;
3467 uint64_t buf[FLOW_U64S];
3468 } m;
1f4a8933 3469
35eb6326
YHW
3470 uint16_t in_port = ofp_to_u16(po->flow_metadata.flow.in_port.ofp_port);
3471 if (in_port >= ofproto->max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
1f4a8933
JR
3472 return OFPERR_OFPBRC_BAD_PORT;
3473 }
3474
3475 /* Get payload. */
3476 if (po->buffer_id != UINT32_MAX) {
3477 return OFPERR_OFPBRC_BUFFER_UNKNOWN;
3478 }
3479
3480 /* Ensure that the L3 header is 32-bit aligned. */
3481 opo->packet = dp_packet_clone_data_with_headroom(po->packet,
3482 po->packet_len, 2);
cb1145d1
ZB
3483 /* Take the received packet_tpye as packet_type of the packet. */
3484 opo->packet->packet_type = po->flow_metadata.flow.packet_type;
3485
1f4a8933
JR
3486 /* Store struct flow. */
3487 opo->flow = xmalloc(sizeof *opo->flow);
d7892c81
YHW
3488 *opo->flow = po->flow_metadata.flow;
3489 miniflow_extract(opo->packet, &m.mf);
3490 flow_union_with_miniflow(opo->flow, &m.mf);
1f4a8933
JR
3491
3492 /* Check actions like for flow mods. We pass a 'table_id' of 0 to
3493 * ofproto_check_consistency(), which isn't strictly correct because these
3494 * actions aren't in any table. This is OK as 'table_id' is only used to
3495 * check instructions (e.g., goto-table), which can't appear on the action
3496 * list of a packet-out. */
67210a55
JR
3497 match_wc_init(&match, opo->flow);
3498 error = ofpacts_check_consistency(po->ofpacts, po->ofpacts_len, &match,
1f4a8933
JR
3499 u16_to_ofp(ofproto->max_ports), 0,
3500 ofproto->n_tables,
3501 ofconn_get_protocol(ofconn));
3502 if (error) {
3503 dp_packet_delete(opo->packet);
3504 free(opo->flow);
3505 return error;
3506 }
3507
3508 opo->ofpacts = po->ofpacts;
3509 opo->ofpacts_len = po->ofpacts_len;
3510
3511 opo->aux = NULL;
3512 return 0;
3513}
3514
3515static enum ofperr
3516ofproto_packet_out_start(struct ofproto *ofproto,
3517 struct ofproto_packet_out *opo)
3518 OVS_REQUIRES(ofproto_mutex)
3519{
3520 enum ofperr error;
3521
3522 error = ofproto_check_ofpacts(ofproto, opo->ofpacts, opo->ofpacts_len);
3523 if (error) {
3524 return error;
3525 }
3526
3527 return ofproto->ofproto_class->packet_xlate(ofproto, opo);
3528}
3529
6dd3c787
JR
3530static void
3531ofproto_packet_out_revert(struct ofproto *ofproto,
3532 struct ofproto_packet_out *opo)
3533 OVS_REQUIRES(ofproto_mutex)
3534{
3535 ofproto->ofproto_class->packet_xlate_revert(ofproto, opo);
3536}
3537
1f4a8933
JR
3538static void
3539ofproto_packet_out_finish(struct ofproto *ofproto,
3540 struct ofproto_packet_out *opo)
3541 OVS_REQUIRES(ofproto_mutex)
3542{
3543 ofproto->ofproto_class->packet_execute(ofproto, opo);
3544}
3545
90bf1e07 3546static enum ofperr
982697a4 3547handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
1f4a8933 3548 OVS_EXCLUDED(ofproto_mutex)
064af421 3549{
64ff1d96 3550 struct ofproto *p = ofconn_get_ofproto(ofconn);
c6a93eb7 3551 struct ofputil_packet_out po;
1f4a8933 3552 struct ofproto_packet_out opo;
f25d0cf3
BP
3553 uint64_t ofpacts_stub[1024 / 8];
3554 struct ofpbuf ofpacts;
90bf1e07 3555 enum ofperr error;
064af421 3556
ac51afaf
BP
3557 COVERAGE_INC(ofproto_packet_out);
3558
76589937 3559 error = reject_slave_controller(ofconn);
9deba63b 3560 if (error) {
1f4a8933 3561 return error;
9deba63b
BP
3562 }
3563
c6a93eb7 3564 /* Decode message. */
f25d0cf3 3565 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
89d7927b
YHW
3566 error = ofputil_decode_packet_out(&po, oh, ofproto_get_tun_tab(p),
3567 &ofpacts);
064af421 3568 if (error) {
1f4a8933
JR
3569 ofpbuf_uninit(&ofpacts);
3570 return error;
91858960 3571 }
064af421 3572
1f4a8933
JR
3573 po.ofpacts = ofpbuf_steal_data(&ofpacts); /* Move to heap. */
3574 error = ofproto_packet_out_init(p, ofconn, &opo, &po);
3575 if (error) {
3576 free(po.ofpacts);
3577 return error;
e1154f71 3578 }
89108874 3579
1f4a8933
JR
3580 ovs_mutex_lock(&ofproto_mutex);
3581 opo.version = p->tables_version;
3582 error = ofproto_packet_out_start(p, &opo);
548de4dd 3583 if (!error) {
1f4a8933 3584 ofproto_packet_out_finish(p, &opo);
548de4dd 3585 }
1f4a8933 3586 ovs_mutex_unlock(&ofproto_mutex);
abe529af 3587
1f4a8933 3588 ofproto_packet_out_uninit(&opo);
abe529af 3589 return error;
064af421
BP
3590}
3591
77ab5fd2
BP
3592static enum ofperr
3593handle_nxt_resume(struct ofconn *ofconn, const struct ofp_header *oh)
3594{
3595 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3596 struct ofputil_packet_in_private pin;
3597 enum ofperr error;
3598
8d8ab6c2 3599 error = ofputil_decode_packet_in_private(oh, false,
3cddeff0
YHW
3600 ofproto_get_tun_tab(ofproto),
3601 &ofproto->vl_mff_map, &pin, NULL,
3602 NULL);
77ab5fd2
BP
3603 if (error) {
3604 return error;
3605 }
3606
3607 error = (ofproto->ofproto_class->nxt_resume
3608 ? ofproto->ofproto_class->nxt_resume(ofproto, &pin)
3609 : OFPERR_NXR_NOT_SUPPORTED);
3610
3611 ofputil_packet_in_private_destroy(&pin);
3612
3613 return error;
3614}
3615
064af421 3616static void
2a6f78e0 3617update_port_config(struct ofconn *ofconn, struct ofport *port,
9e1fd49b
BP
3618 enum ofputil_port_config config,
3619 enum ofputil_port_config mask)
064af421 3620{
2a6f78e0 3621 enum ofputil_port_config toggle = (config ^ port->pp.config) & mask;
abe529af 3622
2a6f78e0
BP
3623 if (toggle & OFPUTIL_PC_PORT_DOWN
3624 && (config & OFPUTIL_PC_PORT_DOWN
3625 ? netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL)
3626 : netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL))) {
3627 /* We tried to bring the port up or down, but it failed, so don't
3628 * update the "down" bit. */
9e1fd49b 3629 toggle &= ~OFPUTIL_PC_PORT_DOWN;
064af421 3630 }
abe529af 3631
2a6f78e0
BP
3632 if (toggle) {
3633 enum ofputil_port_config old_config = port->pp.config;
3634 port->pp.config ^= toggle;
abe529af 3635 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2a6f78e0
BP
3636 connmgr_send_port_status(port->ofproto->connmgr, ofconn, &port->pp,
3637 OFPPR_MODIFY);
064af421
BP
3638 }
3639}
3640
90bf1e07 3641static enum ofperr
1c38055d
JR
3642port_mod_start(struct ofconn *ofconn, struct ofputil_port_mod *pm,
3643 struct ofport **port)
064af421 3644{
64ff1d96 3645 struct ofproto *p = ofconn_get_ofproto(ofconn);
1c38055d
JR
3646
3647 *port = ofproto_get_port(p, pm->port_no);
3648 if (!*port) {
3649 return OFPERR_OFPPMFC_BAD_PORT;
3650 }
2f2b904f
BP
3651 if (!eth_addr_equals((*port)->pp.hw_addr, pm->hw_addr) ||
3652 !eth_addr64_equals((*port)->pp.hw_addr64, pm->hw_addr64)) {
1c38055d
JR
3653 return OFPERR_OFPPMFC_BAD_HW_ADDR;
3654 }
3655 return 0;
3656}
3657
3658static void
3659port_mod_finish(struct ofconn *ofconn, struct ofputil_port_mod *pm,
3660 struct ofport *port)
3661{
3662 update_port_config(ofconn, port, pm->config, pm->mask);
3663 if (pm->advertise) {
3664 netdev_set_advertisements(port->netdev, pm->advertise);
3665 }
3666}
3667
3668static enum ofperr
3669handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3670{
9e1fd49b 3671 struct ofputil_port_mod pm;
064af421 3672 struct ofport *port;
9e1fd49b 3673 enum ofperr error;
064af421 3674
76589937 3675 error = reject_slave_controller(ofconn);
9deba63b
BP
3676 if (error) {
3677 return error;
3678 }
064af421 3679
18cc69d9 3680 error = ofputil_decode_port_mod(oh, &pm, false);
9e1fd49b
BP
3681 if (error) {
3682 return error;
3683 }
3684
1c38055d
JR
3685 error = port_mod_start(ofconn, &pm, &port);
3686 if (!error) {
3687 port_mod_finish(ofconn, &pm, port);
064af421 3688 }
1c38055d 3689 return error;
064af421
BP
3690}
3691
90bf1e07 3692static enum ofperr
3269c562 3693handle_desc_stats_request(struct ofconn *ofconn,
982697a4 3694 const struct ofp_header *request)
064af421 3695{
061bfea4
BP
3696 static const char *default_mfr_desc = "Nicira, Inc.";
3697 static const char *default_hw_desc = "Open vSwitch";
3698 static const char *default_sw_desc = VERSION;
3699 static const char *default_serial_desc = "None";
3700 static const char *default_dp_desc = "None";
3701
64ff1d96 3702 struct ofproto *p = ofconn_get_ofproto(ofconn);
064af421
BP
3703 struct ofp_desc_stats *ods;
3704 struct ofpbuf *msg;
3705
982697a4
BP
3706 msg = ofpraw_alloc_stats_reply(request, 0);
3707 ods = ofpbuf_put_zeros(msg, sizeof *ods);
061bfea4
BP
3708 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
3709 sizeof ods->mfr_desc);
3710 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
3711 sizeof ods->hw_desc);
3712 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
3713 sizeof ods->sw_desc);
3714 ovs_strlcpy(ods->serial_num,
3715 p->serial_desc ? p->serial_desc : default_serial_desc,
3716 sizeof ods->serial_num);
3717 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
3718 sizeof ods->dp_desc);
b0421aa2 3719 ofconn_send_reply(ofconn, msg);
064af421
BP
3720
3721 return 0;
3722}
3723
90bf1e07 3724static enum ofperr
3269c562 3725handle_table_stats_request(struct ofconn *ofconn,
982697a4 3726 const struct ofp_header *request)
064af421 3727{
3c1bb396
BP
3728 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3729 struct ofputil_table_features *features;
08d1e234 3730 struct ofputil_table_stats *stats;
3c1bb396 3731 struct ofpbuf *reply;
6c1491fb 3732 size_t i;
064af421 3733
3c1bb396 3734 query_tables(ofproto, &features, &stats);
254750ce 3735
3c1bb396
BP
3736 reply = ofputil_encode_table_stats_reply(request);
3737 for (i = 0; i < ofproto->n_tables; i++) {
3738 if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) {
3739 ofputil_append_table_stats_reply(reply, &stats[i], &features[i]);
254750ce
BP
3740 }
3741 }
3c1bb396 3742 ofconn_send_reply(ofconn, reply);
254750ce 3743
3c1bb396 3744 free(features);
08d1e234 3745 free(stats);
307975da 3746
064af421
BP
3747 return 0;
3748}
3749
3c4e10fb
BP
3750static enum ofperr
3751handle_table_features_request(struct ofconn *ofconn,
3752 const struct ofp_header *request)
3753{
3754 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
0a2869d5
BP
3755 struct ofpbuf msg = ofpbuf_const_initializer(request,
3756 ntohs(request->length));
3c4e10fb 3757 ofpraw_pull_assert(&msg);
6fd6ed71 3758 if (msg.size || ofpmp_more(request)) {
3c4e10fb
BP
3759 return OFPERR_OFPTFFC_EPERM;
3760 }
3761
0a2869d5 3762 struct ofputil_table_features *features;
3c4e10fb
BP
3763 query_tables(ofproto, &features, NULL);
3764
0a2869d5 3765 struct ovs_list replies;
3c4e10fb 3766 ofpmp_init(&replies, request);
0a2869d5 3767 for (size_t i = 0; i < ofproto->n_tables; i++) {
3c4e10fb
BP
3768 if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) {
3769 ofputil_append_table_features_reply(&features[i], &replies);
3770 }
3771 }
3772 ofconn_send_replies(ofconn, &replies);
3773
3774 free(features);
3775
3776 return 0;
3777}
3778
6c6eedc5
SJ
3779/* Returns the vacancy of 'oftable', a number that ranges from 0 (if the table
3780 * is full) to 100 (if the table is empty).
3781 *
3782 * A table without a limit on flows is considered to be empty. */
3783static uint8_t
3784oftable_vacancy(const struct oftable *t)
3785{
3786 return (!t->max_flows ? 100
3787 : t->n_flows >= t->max_flows ? 0
3788 : (t->max_flows - t->n_flows) * 100.0 / t->max_flows);
3789}
3790
bab86012
SJ
3791static void
3792query_table_desc__(struct ofputil_table_desc *td,
3793 struct ofproto *ofproto, uint8_t table_id)
3794{
6c6eedc5 3795 const struct oftable *t = &ofproto->tables[table_id];
bab86012
SJ
3796
3797 td->table_id = table_id;
6c6eedc5 3798 td->eviction = (t->eviction & EVICTION_OPENFLOW
bab86012
SJ
3799 ? OFPUTIL_TABLE_EVICTION_ON
3800 : OFPUTIL_TABLE_EVICTION_OFF);
3801 td->eviction_flags = OFPROTO_EVICTION_FLAGS;
6c6eedc5 3802 td->vacancy = (t->vacancy_event
bab86012
SJ
3803 ? OFPUTIL_TABLE_VACANCY_ON
3804 : OFPUTIL_TABLE_VACANCY_OFF);
6c6eedc5
SJ
3805 td->table_vacancy.vacancy_down = t->vacancy_down;
3806 td->table_vacancy.vacancy_up = t->vacancy_up;
3807 td->table_vacancy.vacancy = oftable_vacancy(t);
bab86012
SJ
3808}
3809
03c72922
BP
3810/* This function queries the database for dumping table-desc. */
3811static void
3812query_tables_desc(struct ofproto *ofproto, struct ofputil_table_desc **descp)
3813{
3814 struct ofputil_table_desc *table_desc;
3815 size_t i;
3816
3817 table_desc = *descp = xcalloc(ofproto->n_tables, sizeof *table_desc);
3818 for (i = 0; i < ofproto->n_tables; i++) {
3819 struct ofputil_table_desc *td = &table_desc[i];
bab86012 3820 query_table_desc__(td, ofproto, i);
03c72922
BP
3821 }
3822}
3823
3824/* Function to handle dump-table-desc request. */
3825static enum ofperr
3826handle_table_desc_request(struct ofconn *ofconn,
3827 const struct ofp_header *request)
3828{
3829 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3830 struct ofputil_table_desc *table_desc;
3831 struct ovs_list replies;
3832 size_t i;
3833
3834 query_tables_desc(ofproto, &table_desc);
3835 ofpmp_init(&replies, request);
3836 for (i = 0; i < ofproto->n_tables; i++) {
3837 if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) {
3838 ofputil_append_table_desc_reply(&table_desc[i], &replies,
3839 request->version);
3840 }
3841 }
3842 ofconn_send_replies(ofconn, &replies);
3843 free(table_desc);
3844 return 0;
3845}
3846
6c6eedc5
SJ
3847/* This function determines and sends the vacancy event, based on the value
3848 * of current vacancy and threshold vacancy. If the current vacancy is less
3849 * than or equal to vacancy_down, vacancy up events must be enabled, and when
3850 * the current vacancy is greater or equal to vacancy_up, vacancy down events
3851 * must be enabled. */
3852static void
3853send_table_status(struct ofproto *ofproto, uint8_t table_id)
3854{
3855 struct oftable *t = &ofproto->tables[table_id];
3856 if (!t->vacancy_event) {
3857 return;
3858 }
3859
3860 uint8_t vacancy = oftable_vacancy(t);
3861 enum ofp14_table_reason event;
3862 if (vacancy < t->vacancy_down) {
3863 event = OFPTR_VACANCY_DOWN;
3864 } else if (vacancy > t->vacancy_up) {
3865 event = OFPTR_VACANCY_UP;
3866 } else {
3867 return;
3868 }
3869
3870 if (event == t->vacancy_event) {
3871 struct ofputil_table_desc td;
3872 query_table_desc__(&td, ofproto, table_id);
3873 connmgr_send_table_status(ofproto->connmgr, &td, event);
3874
3875 t->vacancy_event = (event == OFPTR_VACANCY_DOWN
3876 ? OFPTR_VACANCY_UP
3877 : OFPTR_VACANCY_DOWN);
3878 }
3879}
3880
abaad8cf 3881static void
ca6ba700 3882append_port_stat(struct ofport *port, struct ovs_list *replies)
abaad8cf 3883{
f8e4867e 3884 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
abaad8cf 3885
65e0be10
BP
3886 calc_duration(port->created, time_msec(),
3887 &ops.duration_sec, &ops.duration_nsec);
3888
d295e8e9
JP
3889 /* Intentionally ignore return value, since errors will set
3890 * 'stats' to all-1s, which is correct for OpenFlow, and
abaad8cf 3891 * netdev_get_stats() will log errors. */
f8e4867e
SH
3892 ofproto_port_get_stats(port, &ops.stats);
3893
3894 ofputil_append_port_stat(replies, &ops);
abaad8cf
JP
3895}
3896
70ae4f93
BP
3897static void
3898handle_port_request(struct ofconn *ofconn,
3899 const struct ofp_header *request, ofp_port_t port_no,
ca6ba700 3900 void (*cb)(struct ofport *, struct ovs_list *replies))
064af421 3901{
70ae4f93 3902 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421 3903 struct ofport *port;
ca6ba700 3904 struct ovs_list replies;
064af421 3905
982697a4 3906 ofpmp_init(&replies, request);
7f05e7ab 3907 if (port_no != OFPP_ANY) {
70ae4f93 3908 port = ofproto_get_port(ofproto, port_no);
abaad8cf 3909 if (port) {
70ae4f93 3910 cb(port, &replies);
abaad8cf
JP
3911 }
3912 } else {
70ae4f93
BP
3913 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3914 cb(port, &replies);
abaad8cf 3915 }
064af421
BP
3916 }
3917
63f2140a 3918 ofconn_send_replies(ofconn, &replies);
70ae4f93
BP
3919}
3920
3921static enum ofperr
3922handle_port_stats_request(struct ofconn *ofconn,
3923 const struct ofp_header *request)
3924{
3925 ofp_port_t port_no;
3926 enum ofperr error;
3927
3928 error = ofputil_decode_port_stats_request(request, &port_no);
3929 if (!error) {
3930 handle_port_request(ofconn, request, port_no, append_port_stat);
3931 }
3932 return error;
3933}
3934
3935static void
ca6ba700 3936append_port_desc(struct ofport *port, struct ovs_list *replies)
70ae4f93
BP
3937{
3938 ofputil_append_port_desc_stats_reply(&port->pp, replies);
064af421
BP
3939}
3940
2be393ed
JP
3941static enum ofperr
3942handle_port_desc_stats_request(struct ofconn *ofconn,
982697a4 3943 const struct ofp_header *request)
2be393ed 3944{
70ae4f93
BP
3945 ofp_port_t port_no;
3946 enum ofperr error;
2be393ed 3947
70ae4f93
BP
3948 error = ofputil_decode_port_desc_stats_request(request, &port_no);
3949 if (!error) {
3950 handle_port_request(ofconn, request, port_no, append_port_desc);
2be393ed 3951 }
70ae4f93 3952 return error;
2be393ed
JP
3953}
3954
98eaac36
JR
3955static uint32_t
3956hash_cookie(ovs_be64 cookie)
3957{
965607c8 3958 return hash_uint64((OVS_FORCE uint64_t)cookie);
98eaac36
JR
3959}
3960
3961static void
3962cookies_insert(struct ofproto *ofproto, struct rule *rule)
2c916028 3963 OVS_REQUIRES(ofproto_mutex)
98eaac36
JR
3964{
3965 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3966 hash_cookie(rule->flow_cookie));
3967}
3968
3969static void
3970cookies_remove(struct ofproto *ofproto, struct rule *rule)
2c916028 3971 OVS_REQUIRES(ofproto_mutex)
98eaac36
JR
3972{
3973 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3974}
3975
c6ebb8fb 3976static void
65e0be10
BP
3977calc_duration(long long int start, long long int now,
3978 uint32_t *sec, uint32_t *nsec)
c6ebb8fb 3979{
f27f2134 3980 long long int msecs = now - start;
588cd7b5
BP
3981 *sec = msecs / 1000;
3982 *nsec = (msecs % 1000) * (1000 * 1000);
3983}
3984
48266274 3985/* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
554764d1
SH
3986 * true if 'table_id' is OK, false otherwise. */
3987static bool
48266274
BP
3988check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3989{
554764d1 3990 return table_id == OFPTT_ALL || table_id < ofproto->n_tables;
48266274
BP
3991}
3992
5c67e4af 3993static struct oftable *
d4ce8a49 3994next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
5c67e4af
BP
3995{
3996 struct oftable *table;
3997
3998 for (table = &ofproto->tables[table_id];
3999 table < &ofproto->tables[ofproto->n_tables];
4000 table++) {
4001 if (!(table->flags & OFTABLE_HIDDEN)) {
4002 return table;
4003 }
4004 }
4005
4006 return NULL;
4007}
4008
d0918789 4009static struct oftable *
d4ce8a49 4010first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
064af421 4011{
6c1491fb 4012 if (table_id == 0xff) {
5c67e4af 4013 return next_visible_table(ofproto, 0);
6c1491fb
BP
4014 } else if (table_id < ofproto->n_tables) {
4015 return &ofproto->tables[table_id];
a02e5331 4016 } else {
6c1491fb 4017 return NULL;
a02e5331 4018 }
064af421
BP
4019}
4020
d0918789 4021static struct oftable *
d4ce8a49
BP
4022next_matching_table(const struct ofproto *ofproto,
4023 const struct oftable *table, uint8_t table_id)
6c1491fb 4024{
5c67e4af
BP
4025 return (table_id == 0xff
4026 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
6c1491fb
BP
4027 : NULL);
4028}
4029
d0918789 4030/* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
6c1491fb
BP
4031 *
4032 * - If TABLE_ID is 0xff, this iterates over every classifier table in
5c67e4af 4033 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
6c1491fb
BP
4034 *
4035 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
5c67e4af
BP
4036 * only once, for that table. (This can be used to access tables marked
4037 * OFTABLE_HIDDEN.)
6c1491fb 4038 *
48266274
BP
4039 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
4040 * entered at all. (Perhaps you should have validated TABLE_ID with
4041 * check_table_id().)
6c1491fb
BP
4042 *
4043 * All parameters are evaluated multiple times.
4044 */
d0918789
BP
4045#define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
4046 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
4047 (TABLE) != NULL; \
4048 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
6c1491fb 4049
a9b22b7f
BP
4050/* Initializes 'criteria' in a straightforward way based on the other
4051 * parameters.
4052 *
dd51dae2
BP
4053 * By default, the criteria include flows that are read-only, on the assumption
4054 * that the collected flows won't be modified. Call rule_criteria_require_rw()
4055 * if flows will be modified.
4056 *
a9b22b7f
BP
4057 * For "loose" matching, the 'priority' parameter is unimportant and may be
4058 * supplied as 0. */
4059static void
4060rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
18721c4a 4061 const struct match *match, int priority,
44e0c35d 4062 ovs_version_t version, ovs_be64 cookie,
18721c4a
JR
4063 ovs_be64 cookie_mask, ofp_port_t out_port,
4064 uint32_t out_group)
a9b22b7f
BP
4065{
4066 criteria->table_id = table_id;
bd53aa17
JR
4067 cls_rule_init(&criteria->cr, match, priority);
4068 criteria->version = version;
a9b22b7f
BP
4069 criteria->cookie = cookie;
4070 criteria->cookie_mask = cookie_mask;
4071 criteria->out_port = out_port;
4072 criteria->out_group = out_group;
dd51dae2
BP
4073
4074 /* We ordinarily want to skip hidden rules, but there has to be a way for
4075 * code internal to OVS to modify and delete them, so if the criteria
4076 * specify a priority that can only be for a hidden flow, then allow hidden
4077 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
4078 * with hidden flows because OpenFlow uses only a 16-bit field to specify
4079 * priority.) */
4080 criteria->include_hidden = priority > UINT16_MAX;
4081
4082 /* We assume that the criteria are being used to collect flows for reading
4083 * but not modification. Thus, we should collect read-only flows. */
4084 criteria->include_readonly = true;
4085}
4086
4087/* By default, criteria initialized by rule_criteria_init() will match flows
4088 * that are read-only, on the assumption that the collected flows won't be
4089 * modified. Call this function to match only flows that are be modifiable.
4090 *
4091 * Specify 'can_write_readonly' as false in ordinary circumstances, true if the
4092 * caller has special privileges that allow it to modify even "read-only"
4093 * flows. */
4094static void
4095rule_criteria_require_rw(struct rule_criteria *criteria,
4096 bool can_write_readonly)
4097{
4098 criteria->include_readonly = can_write_readonly;
a9b22b7f
BP
4099}
4100
4101static void
4102rule_criteria_destroy(struct rule_criteria *criteria)
4103{
4104 cls_rule_destroy(&criteria->cr);
5bacd5cd 4105 criteria->version = OVS_VERSION_NOT_REMOVED; /* Mark as destroyed. */
a9b22b7f
BP
4106}
4107
39c94593
JR
4108/* Schedules postponed removal of rules, destroys 'rules'. */
4109static void
cc099268 4110remove_rules_postponed(struct rule_collection *rules)
39c94593
JR
4111 OVS_REQUIRES(ofproto_mutex)
4112{
fe59694b
JR
4113 if (rule_collection_n(rules) > 0) {
4114 if (rule_collection_n(rules) == 1) {
4115 ovsrcu_postpone(remove_rule_rcu, rule_collection_rules(rules)[0]);
4116 rule_collection_init(rules);
39c94593
JR
4117 } else {
4118 ovsrcu_postpone(remove_rules_rcu, rule_collection_detach(rules));
4119 }
4120 }
4121}
4122
5d08a275
JR
4123/* Schedules postponed removal of groups, destroys 'groups'. */
4124static void
4125remove_groups_postponed(struct group_collection *groups)
4126 OVS_REQUIRES(ofproto_mutex)
4127{
4128 if (group_collection_n(groups) > 0) {
4129 if (group_collection_n(groups) == 1) {
4130 ovsrcu_postpone(remove_group_rcu,
4131 group_collection_groups(groups)[0]);
4132 group_collection_init(groups);
4133 } else {
4134 ovsrcu_postpone(remove_groups_rcu,
4135 group_collection_detach(groups));
4136 }
4137 }
4138}
4139
dd51dae2
BP
4140/* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This
4141 * function verifies most of the criteria in 'c' itself, but the caller must
4142 * check 'c->cr' itself.
4143 *
2b7b1427 4144 * Rules that have already been marked for removal are not collected.
ce59413f 4145 *
dd51dae2 4146 * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and
b20f4073
BP
4147 * 'c' only includes modifiable rules). */
4148static void
a9b22b7f 4149collect_rule(struct rule *rule, const struct rule_criteria *c,
dd51dae2 4150 struct rule_collection *rules, size_t *n_readonly)
15aaf599 4151 OVS_REQUIRES(ofproto_mutex)
6c1d7642 4152{
dd51dae2
BP
4153 if ((c->table_id == rule->table_id || c->table_id == 0xff)
4154 && ofproto_rule_has_out_port(rule, c->out_port)
4155 && ofproto_rule_has_out_group(rule, c->out_group)
4156 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)
ce59413f 4157 && (!rule_is_hidden(rule) || c->include_hidden)
bd53aa17 4158 && cls_rule_visible_in_version(&rule->cr, c->version)) {
dd51dae2 4159 /* Rule matches all the criteria... */
834fe5cb 4160 if (!rule_is_readonly(rule) || c->include_readonly) {
dd51dae2 4161 /* ...add it. */
a8e547c1 4162 rule_collection_add(rules, rule);
dd51dae2
BP
4163 } else {
4164 /* ...except it's read-only. */
4165 ++*n_readonly;
6c1d7642 4166 }
6c1d7642
BP
4167 }
4168}
4169
a9b22b7f
BP
4170/* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4171 * on classifiers rules are done in the "loose" way required for OpenFlow
4172 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
9ed18e46
BP
4173 * 'rules'.
4174 *
9ed18e46 4175 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 4176static enum ofperr
a9b22b7f 4177collect_rules_loose(struct ofproto *ofproto,
a8e547c1
BP
4178 const struct rule_criteria *criteria,
4179 struct rule_collection *rules)
15aaf599 4180 OVS_REQUIRES(ofproto_mutex)
9ed18e46 4181{
d0918789 4182 struct oftable *table;
554764d1 4183 enum ofperr error = 0;
dd51dae2 4184 size_t n_readonly = 0;
48266274 4185
a8e547c1
BP
4186 rule_collection_init(rules);
4187
554764d1
SH
4188 if (!check_table_id(ofproto, criteria->table_id)) {
4189 error = OFPERR_OFPBRC_BAD_TABLE_ID;
a8e547c1 4190 goto exit;
48266274 4191 }
9ed18e46 4192
b8266395 4193 if (criteria->cookie_mask == OVS_BE64_MAX) {
98eaac36
JR
4194 struct rule *rule;
4195
a9b22b7f
BP
4196 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
4197 hash_cookie(criteria->cookie),
98eaac36 4198 &ofproto->cookies) {
a9b22b7f 4199 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
b20f4073 4200 collect_rule(rule, criteria, rules, &n_readonly);
98eaac36
JR
4201 }
4202 }
6c1d7642 4203 } else {
a9b22b7f 4204 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
6c1d7642 4205 struct rule *rule;
9ed18e46 4206
bd53aa17
JR
4207 CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &criteria->cr,
4208 criteria->version) {
b20f4073 4209 collect_rule(rule, criteria, rules, &n_readonly);
9ed18e46
BP
4210 }
4211 }
4212 }
48d28ac1 4213
a8e547c1 4214exit:
fe59694b 4215 if (!error && !rule_collection_n(rules) && n_readonly) {
dd51dae2
BP
4216 /* We didn't find any rules to modify. We did find some read-only
4217 * rules that we're not allowed to modify, so report that. */
4218 error = OFPERR_OFPBRC_EPERM;
4219 }
a8e547c1
BP
4220 if (error) {
4221 rule_collection_destroy(rules);
4222 }
48d28ac1 4223 return error;
9ed18e46
BP
4224}
4225
a9b22b7f
BP
4226/* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4227 * on classifiers rules are done in the "strict" way required for OpenFlow
4228 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
4229 * rules on list 'rules'.
9ed18e46 4230 *
9ed18e46 4231 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 4232static enum ofperr
a9b22b7f 4233collect_rules_strict(struct ofproto *ofproto,
a8e547c1
BP
4234 const struct rule_criteria *criteria,
4235 struct rule_collection *rules)
15aaf599 4236 OVS_REQUIRES(ofproto_mutex)
9ed18e46 4237{
d0918789 4238 struct oftable *table;
dd51dae2 4239 size_t n_readonly = 0;
d51c8b71 4240 enum ofperr error = 0;
48266274 4241
a8e547c1
BP
4242 rule_collection_init(rules);
4243
554764d1
SH
4244 if (!check_table_id(ofproto, criteria->table_id)) {
4245 error = OFPERR_OFPBRC_BAD_TABLE_ID;
a8e547c1 4246 goto exit;
48266274 4247 }
9ed18e46 4248
b8266395 4249 if (criteria->cookie_mask == OVS_BE64_MAX) {
98eaac36
JR
4250 struct rule *rule;
4251
a9b22b7f
BP
4252 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
4253 hash_cookie(criteria->cookie),
98eaac36 4254 &ofproto->cookies) {
a9b22b7f 4255 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
b20f4073 4256 collect_rule(rule, criteria, rules, &n_readonly);
98eaac36
JR
4257 }
4258 }
6c1d7642 4259 } else {
a9b22b7f 4260 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
6c1d7642 4261 struct rule *rule;
9ed18e46 4262
a9b22b7f 4263 rule = rule_from_cls_rule(classifier_find_rule_exactly(
bd53aa17
JR
4264 &table->cls, &criteria->cr,
4265 criteria->version));
6c1d7642 4266 if (rule) {
b20f4073 4267 collect_rule(rule, criteria, rules, &n_readonly);
9ed18e46
BP
4268 }
4269 }
4270 }
48d28ac1 4271
a8e547c1 4272exit:
fe59694b 4273 if (!error && !rule_collection_n(rules) && n_readonly) {
b20f4073
BP
4274 /* We didn't find any rules to modify. We did find some read-only
4275 * rules that we're not allowed to modify, so report that. */
4276 error = OFPERR_OFPBRC_EPERM;
4277 }
a8e547c1
BP
4278 if (error) {
4279 rule_collection_destroy(rules);
4280 }
6c1d7642 4281 return error;
9ed18e46
BP
4282}
4283
f27f2134
BP
4284/* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
4285 * forced into the range of a uint16_t. */
4286static int
4287age_secs(long long int age_ms)
4288{
4289 return (age_ms < 0 ? 0
4290 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
4291 : (unsigned int) age_ms / 1000);
4292}
4293
90bf1e07 4294static enum ofperr
63f2140a 4295handle_flow_stats_request(struct ofconn *ofconn,
982697a4 4296 const struct ofp_header *request)
15aaf599 4297 OVS_EXCLUDED(ofproto_mutex)
064af421 4298{
64ff1d96 4299 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
81d1ea94 4300 struct ofputil_flow_stats_request fsr;
a9b22b7f 4301 struct rule_criteria criteria;
a8e547c1 4302 struct rule_collection rules;
ca6ba700 4303 struct ovs_list replies;
90bf1e07 4304 enum ofperr error;
09246b99 4305
8d8ab6c2 4306 error = ofputil_decode_flow_stats_request(&fsr, request,
3cddeff0
YHW
4307 ofproto_get_tun_tab(ofproto),
4308 &ofproto->vl_mff_map);
09246b99
BP
4309 if (error) {
4310 return error;
4311 }
4312
44e0c35d 4313 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, OVS_VERSION_MAX,
2b7b1427
JR
4314 fsr.cookie, fsr.cookie_mask, fsr.out_port,
4315 fsr.out_group);
15aaf599
BP
4316
4317 ovs_mutex_lock(&ofproto_mutex);
a9b22b7f
BP
4318 error = collect_rules_loose(ofproto, &criteria, &rules);
4319 rule_criteria_destroy(&criteria);
15aaf599
BP
4320 if (!error) {
4321 rule_collection_ref(&rules);
4322 }
4323 ovs_mutex_unlock(&ofproto_mutex);
4324
9ed18e46
BP
4325 if (error) {
4326 return error;
4327 }
5ecc9d81 4328
982697a4 4329 ofpmp_init(&replies, request);
fe59694b
JR
4330 struct rule *rule;
4331 RULE_COLLECTION_FOR_EACH (rule, &rules) {
f27f2134 4332 long long int now = time_msec();
9ed18e46 4333 struct ofputil_flow_stats fs;
15aaf599 4334 long long int created, used, modified;
dc723c44 4335 const struct rule_actions *actions;
15aaf599 4336 enum ofputil_flow_mod_flags flags;
a3779dbc 4337
d10976b7 4338 ovs_mutex_lock(&rule->mutex);
15aaf599 4339 fs.cookie = rule->flow_cookie;
a3779dbc
EJ
4340 fs.idle_timeout = rule->idle_timeout;
4341 fs.hard_timeout = rule->hard_timeout;
ca26eb44 4342 fs.importance = rule->importance;
15aaf599 4343 created = rule->created;
15aaf599 4344 modified = rule->modified;
06a0f3e2 4345 actions = rule_get_actions(rule);
15aaf599 4346 flags = rule->flags;
d10976b7 4347 ovs_mutex_unlock(&rule->mutex);
a3779dbc 4348
dc437090
JR
4349 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
4350 &fs.byte_count, &used);
4351
15aaf599
BP
4352 minimatch_expand(&rule->cr.match, &fs.match);
4353 fs.table_id = rule->table_id;
4354 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
4355 fs.priority = rule->cr.priority;
4356 fs.idle_age = age_secs(now - used);
4357 fs.hard_age = age_secs(now - modified);
15aaf599
BP
4358 fs.ofpacts = actions->ofpacts;
4359 fs.ofpacts_len = actions->ofpacts_len;
3f517bcd 4360
15aaf599 4361 fs.flags = flags;
8d8ab6c2
JG
4362 ofputil_append_flow_stats_reply(&fs, &replies,
4363 ofproto_get_tun_tab(ofproto));
3c4486a5 4364 }
15aaf599
BP
4365
4366 rule_collection_unref(&rules);
a8e547c1
BP
4367 rule_collection_destroy(&rules);
4368
63f2140a 4369 ofconn_send_replies(ofconn, &replies);
5ecc9d81 4370
09246b99
BP
4371 return 0;
4372}
4373
4f2cad2c 4374static void
8d8ab6c2 4375flow_stats_ds(struct ofproto *ofproto, struct rule *rule, struct ds *results)
4f2cad2c 4376{
4f2cad2c 4377 uint64_t packet_count, byte_count;
dc723c44 4378 const struct rule_actions *actions;
dc437090 4379 long long int created, used;
4f2cad2c 4380
dc437090
JR
4381 rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
4382 &byte_count, &used);
4f2cad2c 4383
15aaf599 4384 ovs_mutex_lock(&rule->mutex);
06a0f3e2 4385 actions = rule_get_actions(rule);
15aaf599
BP
4386 created = rule->created;
4387 ovs_mutex_unlock(&rule->mutex);
4388
6c1491fb
BP
4389 if (rule->table_id != 0) {
4390 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
4391 }
15aaf599 4392 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
4f2cad2c
JP
4393 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
4394 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
50f96b10 4395 cls_rule_format(&rule->cr, ofproto_get_tun_tab(ofproto), NULL, results);
a5df0e72 4396 ds_put_char(results, ',');
15aaf599 4397
455ecd77 4398 ds_put_cstr(results, "actions=");
50f96b10 4399 ofpacts_format(actions->ofpacts, actions->ofpacts_len, NULL, results);
15aaf599 4400
4f2cad2c
JP
4401 ds_put_cstr(results, "\n");
4402}
4403
d295e8e9 4404/* Adds a pretty-printed description of all flows to 'results', including
ee8b231c 4405 * hidden flows (e.g., set up by in-band control). */
4f2cad2c
JP
4406void
4407ofproto_get_all_flows(struct ofproto *p, struct ds *results)
4408{
d0918789 4409 struct oftable *table;
6c1491fb 4410
d0918789 4411 OFPROTO_FOR_EACH_TABLE (table, p) {
6c1491fb 4412 struct rule *rule;
064af421 4413
5f0476ce 4414 CLS_FOR_EACH (rule, cr, &table->cls) {
8d8ab6c2 4415 flow_stats_ds(p, rule, results);
6c1491fb 4416 }
064af421 4417 }
064af421
BP
4418}
4419
b5827b24
BP
4420/* Obtains the NetFlow engine type and engine ID for 'ofproto' into
4421 * '*engine_type' and '*engine_id', respectively. */
4422void
4423ofproto_get_netflow_ids(const struct ofproto *ofproto,
4424 uint8_t *engine_type, uint8_t *engine_id)
4425{
abe529af 4426 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
b5827b24
BP
4427}
4428
8f5514fe
AW
4429/* Checks the status change of CFM on 'ofport'.
4430 *
4431 * Returns true if 'ofproto_class' does not support 'cfm_status_changed'. */
4432bool
4433ofproto_port_cfm_status_changed(struct ofproto *ofproto, ofp_port_t ofp_port)
4434{
4435 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
4436 return (ofport && ofproto->ofproto_class->cfm_status_changed
4437 ? ofproto->ofproto_class->cfm_status_changed(ofport)
4438 : true);
4439}
4440
88bf179a
AW
4441/* Checks the status of CFM configured on 'ofp_port' within 'ofproto'.
4442 * Returns 0 if the port's CFM status was successfully stored into
4443 * '*status'. Returns positive errno if the port did not have CFM
8f5514fe 4444 * configured.
9a9e3786 4445 *
88bf179a
AW
4446 * The caller must provide and own '*status', and must free 'status->rmps'.
4447 * '*status' is indeterminate if the return value is non-zero. */
4448int
4e022ec0 4449ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
685acfd9 4450 struct cfm_status *status)
3967a833
MM
4451{
4452 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
88bf179a
AW
4453 return (ofport && ofproto->ofproto_class->get_cfm_status
4454 ? ofproto->ofproto_class->get_cfm_status(ofport, status)
4455 : EOPNOTSUPP);
3967a833
MM
4456}
4457
90bf1e07 4458static enum ofperr
76c93b22 4459handle_aggregate_stats_request(struct ofconn *ofconn,
982697a4 4460 const struct ofp_header *oh)
15aaf599 4461 OVS_EXCLUDED(ofproto_mutex)
27d34fce 4462{
76c93b22 4463 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
81d1ea94 4464 struct ofputil_flow_stats_request request;
76c93b22 4465 struct ofputil_aggregate_stats stats;
5e9d0469 4466 bool unknown_packets, unknown_bytes;
a9b22b7f 4467 struct rule_criteria criteria;
a8e547c1 4468 struct rule_collection rules;
76c93b22 4469 struct ofpbuf *reply;
90bf1e07 4470 enum ofperr error;
27d34fce 4471
8d8ab6c2 4472 error = ofputil_decode_flow_stats_request(&request, oh,
3cddeff0
YHW
4473 ofproto_get_tun_tab(ofproto),
4474 &ofproto->vl_mff_map);
76c93b22
BP
4475 if (error) {
4476 return error;
4477 }
5ecc9d81 4478
a9b22b7f 4479 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
44e0c35d 4480 OVS_VERSION_MAX, request.cookie, request.cookie_mask,
a9b22b7f 4481 request.out_port, request.out_group);
15aaf599
BP
4482
4483 ovs_mutex_lock(&ofproto_mutex);
a9b22b7f
BP
4484 error = collect_rules_loose(ofproto, &criteria, &rules);
4485 rule_criteria_destroy(&criteria);
15aaf599
BP
4486 if (!error) {
4487 rule_collection_ref(&rules);
4488 }
4489 ovs_mutex_unlock(&ofproto_mutex);
4490
9ed18e46
BP
4491 if (error) {
4492 return error;
4493 }
3c4486a5 4494
9ed18e46 4495 memset(&stats, 0, sizeof stats);
5e9d0469 4496 unknown_packets = unknown_bytes = false;
fe59694b
JR
4497
4498 struct rule *rule;
4499 RULE_COLLECTION_FOR_EACH (rule, &rules) {
9ed18e46
BP
4500 uint64_t packet_count;
4501 uint64_t byte_count;
dc437090 4502 long long int used;
5ecc9d81 4503
9ed18e46 4504 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
dc437090 4505 &byte_count, &used);
5ecc9d81 4506
5e9d0469
BP
4507 if (packet_count == UINT64_MAX) {
4508 unknown_packets = true;
4509 } else {
4510 stats.packet_count += packet_count;
4511 }
4512
4513 if (byte_count == UINT64_MAX) {
4514 unknown_bytes = true;
4515 } else {
4516 stats.byte_count += byte_count;
4517 }
4518
9ed18e46 4519 stats.flow_count++;
3c4486a5 4520 }
5e9d0469
BP
4521 if (unknown_packets) {
4522 stats.packet_count = UINT64_MAX;
4523 }
4524 if (unknown_bytes) {
4525 stats.byte_count = UINT64_MAX;
4526 }
27d34fce 4527
15aaf599 4528 rule_collection_unref(&rules);
a8e547c1
BP
4529 rule_collection_destroy(&rules);
4530
982697a4 4531 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
76c93b22 4532 ofconn_send_reply(ofconn, reply);
09246b99
BP
4533
4534 return 0;
4535}
4536
c1c9c9c4 4537struct queue_stats_cbdata {
ca0f572c 4538 struct ofport *ofport;
ca6ba700 4539 struct ovs_list replies;
6dc34a0d 4540 long long int now;
c1c9c9c4
BP
4541};
4542
4543static void
db9220c3 4544put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
c1c9c9c4
BP
4545 const struct netdev_queue_stats *stats)
4546{
6dc34a0d 4547 struct ofputil_queue_stats oqs;
c1c9c9c4 4548
6dc34a0d
BP
4549 oqs.port_no = cbdata->ofport->pp.port_no;
4550 oqs.queue_id = queue_id;
4551 oqs.tx_bytes = stats->tx_bytes;
4552 oqs.tx_packets = stats->tx_packets;
4553 oqs.tx_errors = stats->tx_errors;
4554 if (stats->created != LLONG_MIN) {
4555 calc_duration(stats->created, cbdata->now,
4556 &oqs.duration_sec, &oqs.duration_nsec);
4557 } else {
4558 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
4559 }
64626975 4560 ofputil_append_queue_stat(&cbdata->replies, &oqs);
c1c9c9c4
BP
4561}
4562
4563static void
db9220c3 4564handle_queue_stats_dump_cb(uint32_t queue_id,
c1c9c9c4
BP
4565 struct netdev_queue_stats *stats,
4566 void *cbdata_)
4567{
4568 struct queue_stats_cbdata *cbdata = cbdata_;
4569
4570 put_queue_stats(cbdata, queue_id, stats);
4571}
4572
0414d158 4573static enum ofperr
ca0f572c 4574handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
c1c9c9c4
BP
4575 struct queue_stats_cbdata *cbdata)
4576{
ca0f572c 4577 cbdata->ofport = port;
c1c9c9c4
BP
4578 if (queue_id == OFPQ_ALL) {
4579 netdev_dump_queue_stats(port->netdev,
4580 handle_queue_stats_dump_cb, cbdata);
4581 } else {
4582 struct netdev_queue_stats stats;
4583
1ac788f6
BP
4584 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
4585 put_queue_stats(cbdata, queue_id, &stats);
0414d158
BP
4586 } else {
4587 return OFPERR_OFPQOFC_BAD_QUEUE;
1ac788f6 4588 }
c1c9c9c4 4589 }
0414d158 4590 return 0;
c1c9c9c4
BP
4591}
4592
90bf1e07 4593static enum ofperr
63f2140a 4594handle_queue_stats_request(struct ofconn *ofconn,
982697a4 4595 const struct ofp_header *rq)
c1c9c9c4 4596{
64ff1d96 4597 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
c1c9c9c4 4598 struct queue_stats_cbdata cbdata;
0414d158 4599 struct ofport *port;
0414d158 4600 enum ofperr error;
64626975 4601 struct ofputil_queue_stats_request oqsr;
c1c9c9c4 4602
c1c9c9c4
BP
4603 COVERAGE_INC(ofproto_queue_req);
4604
982697a4 4605 ofpmp_init(&cbdata.replies, rq);
6dc34a0d 4606 cbdata.now = time_msec();
c1c9c9c4 4607
64626975
SH
4608 error = ofputil_decode_queue_stats_request(rq, &oqsr);
4609 if (error) {
4610 return error;
4611 }
4612
7f05e7ab 4613 if (oqsr.port_no == OFPP_ANY) {
0414d158 4614 error = OFPERR_OFPQOFC_BAD_QUEUE;
4e8e4213 4615 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
64626975 4616 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
0414d158
BP
4617 error = 0;
4618 }
c1c9c9c4 4619 }
0414d158 4620 } else {
64626975 4621 port = ofproto_get_port(ofproto, oqsr.port_no);
0414d158 4622 error = (port
64626975 4623 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
0414d158
BP
4624 : OFPERR_OFPQOFC_BAD_PORT);
4625 }
4626 if (!error) {
4627 ofconn_send_replies(ofconn, &cbdata.replies);
c1c9c9c4 4628 } else {
63f2140a 4629 ofpbuf_list_delete(&cbdata.replies);
c1c9c9c4 4630 }
c1c9c9c4 4631
0414d158 4632 return error;
c1c9c9c4 4633}
7ee20df1 4634
2e31a9b8 4635static enum ofperr
6787a49f 4636evict_rules_from_table(struct oftable *table)
15aaf599 4637 OVS_REQUIRES(ofproto_mutex)
2e31a9b8 4638{
802f84ff
JR
4639 enum ofperr error = 0;
4640 struct rule_collection rules;
6787a49f 4641 unsigned int count = table->n_flows;
802f84ff
JR
4642 unsigned int max_flows = table->max_flows;
4643
4644 rule_collection_init(&rules);
4645
4646 while (count-- > max_flows) {
2e31a9b8 4647 struct rule *rule;
8037acb4 4648
2e31a9b8 4649 if (!choose_rule_to_evict(table, &rule)) {
802f84ff
JR
4650 error = OFPERR_OFPFMFC_TABLE_FULL;
4651 break;
2e31a9b8 4652 } else {
802f84ff
JR
4653 eviction_group_remove_rule(rule);
4654 rule_collection_add(&rules, rule);
2e31a9b8 4655 }
8037acb4 4656 }
802f84ff 4657 delete_flows__(&rules, OFPRR_EVICTION, NULL);
2e31a9b8 4658
802f84ff 4659 return error;
8037acb4
BP
4660}
4661
18080541
BP
4662static void
4663get_conjunctions(const struct ofputil_flow_mod *fm,
4664 struct cls_conjunction **conjsp, size_t *n_conjsp)
18080541
BP
4665{
4666 struct cls_conjunction *conjs = NULL;
4667 int n_conjs = 0;
4668
f08e39dd
BP
4669 const struct ofpact *ofpact;
4670 OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) {
4671 if (ofpact->type == OFPACT_CONJUNCTION) {
18080541 4672 n_conjs++;
f08e39dd
BP
4673 } else if (ofpact->type != OFPACT_NOTE) {
4674 /* "conjunction" may appear with "note" actions but not with any
4675 * other type of actions. */
4676 ovs_assert(!n_conjs);
4677 break;
18080541 4678 }
f08e39dd
BP
4679 }
4680 if (n_conjs) {
4681 int i = 0;
18080541
BP
4682
4683 conjs = xzalloc(n_conjs * sizeof *conjs);
18080541 4684 OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) {
f08e39dd
BP
4685 if (ofpact->type == OFPACT_CONJUNCTION) {
4686 struct ofpact_conjunction *oc = ofpact_get_CONJUNCTION(ofpact);
4687 conjs[i].clause = oc->clause;
4688 conjs[i].n_clauses = oc->n_clauses;
4689 conjs[i].id = oc->id;
4690 i++;
4691 }
18080541
BP
4692 }
4693 }
4694
4695 *conjsp = conjs;
4696 *n_conjsp = n_conjs;
4697}
4698
5bacd5cd
JR
4699/* add_flow_init(), add_flow_start(), add_flow_revert(), and add_flow_finish()
4700 * implement OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
1f42be1c
JR
4701 * in which no matching flow already exists in the flow table.
4702 *
5bacd5cd
JR
4703 * add_flow_init() creates a new flow according to 'fm' and stores it to 'ofm'
4704 * for later reference. If the flow replaces other flow, it will be updated to
4705 * match modify semantics later by add_flow_start() (by calling
4706 * replace_rule_start()).
1f42be1c 4707 *
5bacd5cd 4708 * Returns 0 on success, or an OpenFlow error code on failure.
1f42be1c 4709 *
5bacd5cd
JR
4710 * On successful return the caller must complete the operation by calling
4711 * add_flow_start(), and if that succeeds, then either add_flow_finish(), or
4712 * add_flow_revert() if the operation needs to be reverted due to a later
4713 * failure.
4714 */
90bf1e07 4715static enum ofperr
5bacd5cd
JR
4716add_flow_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
4717 const struct ofputil_flow_mod *fm)
4718 OVS_EXCLUDED(ofproto_mutex)
064af421 4719{
d0918789 4720 struct oftable *table;
8037acb4 4721 struct cls_rule cr;
5a361239 4722 uint8_t table_id;
39c94593 4723 enum ofperr error;
064af421 4724
554764d1 4725 if (!check_table_id(ofproto, fm->table_id)) {
5bacd5cd 4726 return OFPERR_OFPBRC_BAD_TABLE_ID;
48266274
BP
4727 }
4728
7ee20df1
BP
4729 /* Pick table. */
4730 if (fm->table_id == 0xff) {
13521ff5 4731 if (ofproto->ofproto_class->rule_choose_table) {
81a76618
BP
4732 error = ofproto->ofproto_class->rule_choose_table(ofproto,
4733 &fm->match,
7ee20df1
BP
4734 &table_id);
4735 if (error) {
4736 return error;
4737 }
cb22974d 4738 ovs_assert(table_id < ofproto->n_tables);
7ee20df1 4739 } else {
5a361239 4740 table_id = 0;
7ee20df1
BP
4741 }
4742 } else if (fm->table_id < ofproto->n_tables) {
5a361239 4743 table_id = fm->table_id;
7ee20df1 4744 } else {
c22c56bd 4745 return OFPERR_OFPBRC_BAD_TABLE_ID;
064af421
BP
4746 }
4747
5a361239 4748 table = &ofproto->tables[table_id];
834fe5cb
BP
4749 if (table->flags & OFTABLE_READONLY
4750 && !(fm->flags & OFPUTIL_FF_NO_READONLY)) {
5c67e4af
BP
4751 return OFPERR_OFPBRC_EPERM;
4752 }
4753
c84d8691
JR
4754 if (!(fm->flags & OFPUTIL_FF_HIDDEN_FIELDS)
4755 && !match_has_default_hidden_fields(&fm->match)) {
4756 VLOG_WARN_RL(&rl, "%s: (add_flow) only internal flows can set "
4757 "non-default values to hidden fields", ofproto->name);
4758 return OFPERR_OFPBRC_EPERM;
adcf00ba
AZ
4759 }
4760
2c7ee524
JR
4761 if (!ofm->temp_rule) {
4762 cls_rule_init(&cr, &fm->match, fm->priority);
8037acb4 4763
2c7ee524
JR
4764 /* Allocate new rule. Destroys 'cr'. */
4765 error = ofproto_rule_create(ofproto, &cr, table - ofproto->tables,
4766 fm->new_cookie, fm->idle_timeout,
4767 fm->hard_timeout, fm->flags,
4768 fm->importance, fm->ofpacts,
5c7c16d8
YHW
4769 fm->ofpacts_len,
4770 fm->match.flow.tunnel.metadata.present.map,
4771 fm->ofpacts_tlv_bitmap, &ofm->temp_rule);
2c7ee524
JR
4772 if (error) {
4773 return error;
4774 }
5bacd5cd 4775
2c7ee524
JR
4776 get_conjunctions(fm, &ofm->conjs, &ofm->n_conjs);
4777 }
5bacd5cd
JR
4778 return 0;
4779}
4780
2c7ee524 4781/* ofm->temp_rule is consumed only in the successful case. */
5bacd5cd
JR
4782static enum ofperr
4783add_flow_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
4784 OVS_REQUIRES(ofproto_mutex)
4785{
4786 struct rule *old_rule = NULL;
4787 struct rule *new_rule = ofm->temp_rule;
4788 const struct rule_actions *actions = rule_get_actions(new_rule);
4789 struct oftable *table = &ofproto->tables[new_rule->table_id];
4790 enum ofperr error;
4791
4792 /* Must check actions while holding ofproto_mutex to avoid a race. */
4793 error = ofproto_check_ofpacts(ofproto, actions->ofpacts,
4794 actions->ofpacts_len);
4795 if (error) {
4796 return error;
4797 }
4798
1f42be1c 4799 /* Check for the existence of an identical rule.
2b7b1427 4800 * This will not return rules earlier marked for removal. */
5bacd5cd
JR
4801 old_rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls,
4802 &new_rule->cr,
4803 ofm->version));
4804 if (!old_rule) {
c84d8691 4805 /* Check for overlap, if requested. */
5bacd5cd
JR
4806 if (new_rule->flags & OFPUTIL_FF_CHECK_OVERLAP
4807 && classifier_rule_overlaps(&table->cls, &new_rule->cr,
4808 ofm->version)) {
c84d8691 4809 return OFPERR_OFPFMFC_OVERLAP;
dd27be82 4810 }
b277c7cc 4811
c84d8691 4812 /* If necessary, evict an existing rule to clear out space. */
6787a49f 4813 if (table->n_flows >= table->max_flows) {
5bacd5cd
JR
4814 if (!choose_rule_to_evict(table, &old_rule)) {
4815 return OFPERR_OFPFMFC_TABLE_FULL;
6787a49f 4816 }
5bacd5cd
JR
4817 eviction_group_remove_rule(old_rule);
4818 /* Marks 'old_rule' as an evicted rule rather than replaced rule.
6787a49f 4819 */
5bacd5cd 4820 old_rule->removed_reason = OFPRR_EVICTION;
c09f755d 4821 }
39c94593 4822 } else {
7338102b 4823 ofm->modify_cookie = true;
39c94593 4824 }
81a76618 4825
5bacd5cd
JR
4826 if (old_rule) {
4827 rule_collection_add(&ofm->old_rules, old_rule);
064af421 4828 }
5bacd5cd 4829 /* Take ownership of the temp_rule. */
58026109 4830 rule_collection_add(&ofm->new_rules, new_rule);
5bacd5cd 4831 ofm->temp_rule = NULL;
8037acb4 4832
5bacd5cd 4833 replace_rule_start(ofproto, ofm, old_rule, new_rule);
c84d8691
JR
4834 return 0;
4835}
1ebeaaa7 4836
6787a49f 4837/* Revert the effects of add_flow_start(). */
1f42be1c 4838static void
8be00367 4839add_flow_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
4840 OVS_REQUIRES(ofproto_mutex)
4841{
5bacd5cd 4842 struct rule *old_rule = rule_collection_n(&ofm->old_rules)
58026109
JR
4843 ? rule_collection_rules(&ofm->old_rules)[0] : NULL;
4844 struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0];
8be00367 4845
39c94593 4846 replace_rule_revert(ofproto, old_rule, new_rule);
1f42be1c
JR
4847}
4848
39c94593 4849/* To be called after version bump. */
c84d8691 4850static void
8be00367 4851add_flow_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 4852 const struct openflow_mod_requester *req)
c84d8691
JR
4853 OVS_REQUIRES(ofproto_mutex)
4854{
5bacd5cd 4855 struct rule *old_rule = rule_collection_n(&ofm->old_rules)
58026109
JR
4856 ? rule_collection_rules(&ofm->old_rules)[0] : NULL;
4857 struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0];
39c94593 4858 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
8037acb4 4859
81dee635 4860 replace_rule_finish(ofproto, ofm, req, old_rule, new_rule, &dead_cookies);
39c94593 4861 learned_cookies_flush(ofproto, &dead_cookies);
802f84ff 4862
39c94593
JR
4863 if (old_rule) {
4864 ovsrcu_postpone(remove_rule_rcu, old_rule);
4865 } else {
39c94593 4866 ofmonitor_report(ofproto->connmgr, new_rule, NXFME_ADDED, 0,
c84d8691
JR
4867 req ? req->ofconn : NULL,
4868 req ? req->request->xid : 0, NULL);
6c6eedc5
SJ
4869
4870 /* Send Vacancy Events for OF1.4+. */
4871 send_table_status(ofproto, new_rule->table_id);
c84d8691 4872 }
c84d8691 4873}
79eee1eb
BP
4874\f
4875/* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
064af421 4876
5bacd5cd 4877/* Create a new rule. Note that the rule is NOT inserted into a any data
2c7ee524
JR
4878 * structures yet. Takes ownership of 'cr'. Only assigns '*new_rule' if
4879 * successful. */
90bf1e07 4880static enum ofperr
5bacd5cd
JR
4881ofproto_rule_create(struct ofproto *ofproto, struct cls_rule *cr,
4882 uint8_t table_id, ovs_be64 new_cookie,
4883 uint16_t idle_timeout, uint16_t hard_timeout,
4884 enum ofputil_flow_mod_flags flags, uint16_t importance,
4885 const struct ofpact *ofpacts, size_t ofpacts_len,
5c7c16d8 4886 uint64_t match_tlv_bitmap, uint64_t ofpacts_tlv_bitmap,
5bacd5cd
JR
4887 struct rule **new_rule)
4888 OVS_NO_THREAD_SAFETY_ANALYSIS
79eee1eb 4889{
39c94593 4890 struct rule *rule;
dd27be82 4891 enum ofperr error;
79eee1eb 4892
39c94593
JR
4893 /* Allocate new rule. */
4894 rule = ofproto->ofproto_class->rule_alloc();
4895 if (!rule) {
4896 cls_rule_destroy(cr);
4897 VLOG_WARN_RL(&rl, "%s: failed to allocate a rule.", ofproto->name);
4898 return OFPERR_OFPFMFC_UNKNOWN;
af822017
BP
4899 }
4900
39c94593
JR
4901 /* Initialize base state. */
4902 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4903 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), cr);
4904 ovs_refcount_init(&rule->ref_count);
834fe5cb 4905
39c94593
JR
4906 ovs_mutex_init(&rule->mutex);
4907 ovs_mutex_lock(&rule->mutex);
82a3160e 4908 *CONST_CAST(ovs_be64 *, &rule->flow_cookie) = new_cookie;
5bacd5cd
JR
4909 rule->created = rule->modified = time_msec();
4910 rule->idle_timeout = idle_timeout;
4911 rule->hard_timeout = hard_timeout;
4912 *CONST_CAST(uint16_t *, &rule->importance) = importance;
f695ebfa 4913 rule->removed_reason = OVS_OFPRR_NONE;
834fe5cb 4914
39c94593 4915 *CONST_CAST(uint8_t *, &rule->table_id) = table_id;
5bacd5cd
JR
4916 rule->flags = flags & OFPUTIL_FF_STATE;
4917
39c94593 4918 *CONST_CAST(const struct rule_actions **, &rule->actions)
5bacd5cd
JR
4919 = rule_actions_create(ofpacts, ofpacts_len);
4920
417e7e66 4921 ovs_list_init(&rule->meter_list_node);
39c94593 4922 rule->eviction_group = NULL;
39c94593
JR
4923 rule->monitor_flags = 0;
4924 rule->add_seqno = 0;
4925 rule->modify_seqno = 0;
5bacd5cd 4926 ovs_list_init(&rule->expirable);
dd27be82
JR
4927 ovs_mutex_unlock(&rule->mutex);
4928
39c94593
JR
4929 /* Construct rule, initializing derived state. */
4930 error = ofproto->ofproto_class->rule_construct(rule);
4931 if (error) {
4932 ofproto_rule_destroy__(rule);
4933 return error;
dd27be82 4934 }
b277c7cc 4935
30d0452c 4936 rule->state = RULE_INITIALIZED;
5c7c16d8
YHW
4937 rule->match_tlv_bitmap = match_tlv_bitmap;
4938 rule->ofpacts_tlv_bitmap = ofpacts_tlv_bitmap;
4939 mf_vl_mff_ref(&rule->ofproto->vl_mff_map, match_tlv_bitmap);
4940 mf_vl_mff_ref(&rule->ofproto->vl_mff_map, ofpacts_tlv_bitmap);
dd27be82 4941
39c94593
JR
4942 *new_rule = rule;
4943 return 0;
4944}
884e1dc4 4945
2c7ee524
JR
4946/* Initialize 'ofm' for a learn action. If the rule already existed, reference
4947 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
4948 * rule reference in both. This does not take the global 'ofproto_mutex'. */
4949enum ofperr
4950ofproto_flow_mod_init_for_learn(struct ofproto *ofproto,
4951 const struct ofputil_flow_mod *fm,
4952 struct ofproto_flow_mod *ofm)
4953 OVS_EXCLUDED(ofproto_mutex)
4954{
4955 enum ofperr error;
4956
4957 /* Reject flow mods that do not look like they were generated by a learn
4958 * action. */
4959 if (fm->command != OFPFC_MODIFY_STRICT || fm->table_id == OFPTT_ALL
4960 || fm->flags & OFPUTIL_FF_RESET_COUNTS
4961 || fm->buffer_id != UINT32_MAX) {
4962 return OFPERR_OFPFMFC_UNKNOWN;
4963 }
4964
4965 /* Check if the rule already exists, and we can get a reference to it. */
4966 struct oftable *table = &ofproto->tables[fm->table_id];
4967 struct rule *rule;
4968
4969 rule = rule_from_cls_rule(classifier_find_match_exactly(
4970 &table->cls, &fm->match, fm->priority,
4971 OVS_VERSION_MAX));
4972 if (rule) {
4973 /* Check if the rule's attributes match as well. */
4974 const struct rule_actions *actions;
4975
4976 ovs_mutex_lock(&rule->mutex);
4977 actions = rule_get_actions(rule);
4978 if (rule->idle_timeout == fm->idle_timeout
4979 && rule->hard_timeout == fm->hard_timeout
4980 && rule->importance == fm->importance
4981 && rule->flags == (fm->flags & OFPUTIL_FF_STATE)
4982 && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie))
4983 && ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
4984 actions->ofpacts, actions->ofpacts_len)) {
4985 /* Rule already exists and need not change, except for the modified
4986 * timestamp. Get a reference to the existing rule. */
4987 ovs_mutex_unlock(&rule->mutex);
4988 if (!ofproto_rule_try_ref(rule)) {
4989 rule = NULL; /* Pretend it did not exist. */
4990 }
4991 } else {
4992 ovs_mutex_unlock(&rule->mutex);
4993 rule = NULL;
4994 }
4995 }
4996
4997 /* Initialize ofproto_flow_mod for future use. */
4998 error = ofproto_flow_mod_init(ofproto, ofm, fm, rule);
4999 if (error) {
5000 ofproto_rule_unref(rule);
5001 return error;
5002 }
5003 return 0;
5004}
5005
2c7ee524 5006enum ofperr
1f4a8933 5007ofproto_flow_mod_learn_refresh(struct ofproto_flow_mod *ofm)
2c7ee524
JR
5008{
5009 enum ofperr error = 0;
5010
5011 /* ofm->temp_rule is our reference to the learned rule. We have a
5012 * reference to an existing rule, if it already was in the classifier,
5013 * otherwise we may have a fresh rule that we need to insert. */
5014 struct rule *rule = ofm->temp_rule;
5015 if (!rule) {
5016 return OFPERR_OFPFMFC_UNKNOWN;
5017 }
2c7ee524
JR
5018
5019 /* Create a new rule if the current one has been removed from the
5020 * classifier. We need to do this since RCU does not allow a current rule
5021 * to be reinserted before all threads have quiesced.
5022 *
5023 * It is possible that the rule is removed asynchronously, e.g., right
1f4a8933 5024 * after we have read the 'rule->state' below. In this case the next time
2c7ee524 5025 * this function is executed the rule will be reinstated. */
1f4a8933 5026 if (rule->state == RULE_REMOVED) {
2c7ee524
JR
5027 struct cls_rule cr;
5028
5029 cls_rule_clone(&cr, &rule->cr);
5030 ovs_mutex_lock(&rule->mutex);
1f4a8933 5031 error = ofproto_rule_create(rule->ofproto, &cr, rule->table_id,
2c7ee524
JR
5032 rule->flow_cookie,
5033 rule->idle_timeout,
5034 rule->hard_timeout, rule->flags,
5035 rule->importance,
5036 rule->actions->ofpacts,
5037 rule->actions->ofpacts_len,
5c7c16d8
YHW
5038 rule->match_tlv_bitmap,
5039 rule->ofpacts_tlv_bitmap,
2c7ee524
JR
5040 &ofm->temp_rule);
5041 ovs_mutex_unlock(&rule->mutex);
2c7ee524 5042 if (!error) {
1f4a8933 5043 ofproto_rule_unref(rule); /* Release old reference. */
2c7ee524 5044 }
2c7ee524
JR
5045 } else {
5046 /* Refresh the existing rule. */
5047 ovs_mutex_lock(&rule->mutex);
5048 rule->modified = time_msec();
5049 ovs_mutex_unlock(&rule->mutex);
5050 }
1f4a8933
JR
5051 return error;
5052}
5053
5054enum ofperr
5055ofproto_flow_mod_learn_start(struct ofproto_flow_mod *ofm)
5056 OVS_REQUIRES(ofproto_mutex)
5057{
5058 struct rule *rule = ofm->temp_rule;
5059
5060 /* ofproto_flow_mod_start() consumes the reference, so we
5061 * take a new one. */
5062 ofproto_rule_ref(rule);
5063 enum ofperr error = ofproto_flow_mod_start(rule->ofproto, ofm);
5064 ofm->temp_rule = rule;
5065
5066 return error;
5067}
5068
6dd3c787
JR
5069void
5070ofproto_flow_mod_learn_revert(struct ofproto_flow_mod *ofm)
5071 OVS_REQUIRES(ofproto_mutex)
5072{
5073 struct rule *rule = rule_collection_rules(&ofm->new_rules)[0];
5074 ofproto_flow_mod_revert(rule->ofproto, ofm);
5075}
5076
1f4a8933
JR
5077void
5078ofproto_flow_mod_learn_finish(struct ofproto_flow_mod *ofm,
5079 struct ofproto *orig_ofproto)
5080 OVS_REQUIRES(ofproto_mutex)
5081{
5082 struct rule *rule = rule_collection_rules(&ofm->new_rules)[0];
5083
5084 /* If learning on a different bridge, must bump its version
5085 * number and flush connmgr afterwards. */
5086 if (rule->ofproto != orig_ofproto) {
5087 ofproto_bump_tables_version(rule->ofproto);
5088 }
5089 ofproto_flow_mod_finish(rule->ofproto, ofm, NULL);
5090 if (rule->ofproto != orig_ofproto) {
5091 ofmonitor_flush(rule->ofproto->connmgr);
5092 }
5093}
5094
5095/* Refresh 'ofm->temp_rule', for which the caller holds a reference, if already
5096 * in the classifier, insert it otherwise. If the rule has already been
5097 * removed from the classifier, a new rule is created using 'ofm->temp_rule' as
5098 * a template and the reference to the old 'ofm->temp_rule' is freed. If
5099 * 'keep_ref' is true, then a reference to the current rule is held, otherwise
5100 * it is released and 'ofm->temp_rule' is set to NULL.
5101 *
4c71600d
DDP
5102 * If 'limit' != 0, insertion will fail if there are more than 'limit' rules
5103 * in the same table with the same cookie. If insertion succeeds,
86b7637c
JS
5104 * '*below_limitp' will be set to true. If insertion fails '*below_limitp'
5105 * will be set to false.
4c71600d 5106 *
1f4a8933
JR
5107 * Caller needs to be the exclusive owner of 'ofm' as it is being manipulated
5108 * during the call. */
5109enum ofperr
4c71600d 5110ofproto_flow_mod_learn(struct ofproto_flow_mod *ofm, bool keep_ref,
86b7637c 5111 unsigned limit, bool *below_limitp)
1f4a8933
JR
5112 OVS_EXCLUDED(ofproto_mutex)
5113{
5114 enum ofperr error = ofproto_flow_mod_learn_refresh(ofm);
5115 struct rule *rule = ofm->temp_rule;
86b7637c 5116 bool below_limit = true;
1f4a8933
JR
5117
5118 /* Do we need to insert the rule? */
5119 if (!error && rule->state == RULE_INITIALIZED) {
5120 ovs_mutex_lock(&ofproto_mutex);
4c71600d
DDP
5121
5122 if (limit) {
5123 struct rule_criteria criteria;
5124 struct rule_collection rules;
5125 struct match match;
5126
5127 match_init_catchall(&match);
5128 rule_criteria_init(&criteria, rule->table_id, &match, 0,
5129 OVS_VERSION_MAX, rule->flow_cookie,
5130 OVS_BE64_MAX, OFPP_ANY, OFPG_ANY);
5131 rule_criteria_require_rw(&criteria, false);
5132 collect_rules_loose(rule->ofproto, &criteria, &rules);
5133 if (rule_collection_n(&rules) >= limit) {
86b7637c 5134 below_limit = false;
4c71600d
DDP
5135 }
5136 rule_collection_destroy(&rules);
5137 rule_criteria_destroy(&criteria);
5138 }
5139
86b7637c 5140 if (below_limit) {
4c71600d
DDP
5141 ofm->version = rule->ofproto->tables_version + 1;
5142
5143 error = ofproto_flow_mod_learn_start(ofm);
5144 if (!error) {
5145 ofproto_flow_mod_learn_finish(ofm, NULL);
5146 }
5147 } else {
5148 ofproto_flow_mod_uninit(ofm);
1f4a8933
JR
5149 }
5150 ovs_mutex_unlock(&ofproto_mutex);
3d8e7354 5151
86b7637c 5152 if (!below_limit) {
3d8e7354
JS
5153 static struct vlog_rate_limit learn_rl = VLOG_RATE_LIMIT_INIT(1, 5);
5154 VLOG_INFO_RL(&learn_rl, "Learn limit for flow %"PRIu64" reached.",
5155 rule->flow_cookie);
5156 }
1f4a8933 5157 }
2c7ee524 5158
86b7637c 5159 if (!keep_ref && below_limit) {
2c7ee524
JR
5160 ofproto_rule_unref(rule);
5161 ofm->temp_rule = NULL;
5162 }
86b7637c
JS
5163 if (below_limitp) {
5164 *below_limitp = below_limit;
4c71600d 5165 }
2c7ee524
JR
5166 return error;
5167}
5168
39c94593 5169static void
5bacd5cd
JR
5170replace_rule_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
5171 struct rule *old_rule, struct rule *new_rule)
39c94593
JR
5172{
5173 struct oftable *table = &ofproto->tables[new_rule->table_id];
834fe5cb 5174
6787a49f 5175 /* 'old_rule' may be either an evicted rule or replaced rule. */
39c94593 5176 if (old_rule) {
5bacd5cd
JR
5177 /* Copy values from old rule for modify semantics. */
5178 if (old_rule->removed_reason != OFPRR_EVICTION) {
5179 bool change_cookie = (ofm->modify_cookie
5180 && new_rule->flow_cookie != OVS_BE64_MAX
5181 && new_rule->flow_cookie != old_rule->flow_cookie);
5182
5183 ovs_mutex_lock(&new_rule->mutex);
5184 ovs_mutex_lock(&old_rule->mutex);
5185 if (ofm->command != OFPFC_ADD) {
5186 new_rule->idle_timeout = old_rule->idle_timeout;
5187 new_rule->hard_timeout = old_rule->hard_timeout;
5188 *CONST_CAST(uint16_t *, &new_rule->importance) = old_rule->importance;
5189 new_rule->flags = old_rule->flags;
5190 new_rule->created = old_rule->created;
5191 }
5192 if (!change_cookie) {
82a3160e
BP
5193 *CONST_CAST(ovs_be64 *, &new_rule->flow_cookie)
5194 = old_rule->flow_cookie;
5bacd5cd
JR
5195 }
5196 ovs_mutex_unlock(&old_rule->mutex);
5197 ovs_mutex_unlock(&new_rule->mutex);
5198 }
5199
39c94593 5200 /* Mark the old rule for removal in the next version. */
5bacd5cd 5201 cls_rule_make_invisible_in_version(&old_rule->cr, ofm->version);
9bab38ff
JR
5202
5203 /* Remove the old rule from data structures. */
5204 ofproto_rule_remove__(ofproto, old_rule);
d79e3d70
JR
5205 } else {
5206 table->n_flows++;
dd27be82 5207 }
cc099268
JR
5208 /* Insert flow to ofproto data structures, so that later flow_mods may
5209 * relate to it. This is reversible, in case later errors require this to
39c94593
JR
5210 * be reverted. */
5211 ofproto_rule_insert__(ofproto, new_rule);
5212 /* Make the new rule visible for classifier lookups only from the next
5213 * version. */
5bacd5cd
JR
5214 classifier_insert(&table->cls, &new_rule->cr, ofm->version, ofm->conjs,
5215 ofm->n_conjs);
39c94593
JR
5216}
5217
cc099268
JR
5218static void
5219replace_rule_revert(struct ofproto *ofproto,
5220 struct rule *old_rule, struct rule *new_rule)
39c94593
JR
5221{
5222 struct oftable *table = &ofproto->tables[new_rule->table_id];
35f48b8b 5223
39c94593 5224 if (old_rule) {
5bacd5cd
JR
5225 if (old_rule->removed_reason == OFPRR_EVICTION) {
5226 /* Revert the eviction. */
5227 eviction_group_add_rule(old_rule);
5228 }
5229
9bab38ff
JR
5230 /* Restore the old rule to data structures. */
5231 ofproto_rule_insert__(ofproto, old_rule);
5232
d79e3d70 5233 /* Restore the original visibility of the old rule. */
39c94593 5234 cls_rule_restore_visibility(&old_rule->cr);
d79e3d70
JR
5235 } else {
5236 /* Restore table's rule count. */
5237 table->n_flows--;
834fe5cb
BP
5238 }
5239
39c94593
JR
5240 /* Remove the new rule immediately. It was never visible to lookups. */
5241 if (!classifier_remove(&table->cls, &new_rule->cr)) {
5242 OVS_NOT_REACHED();
5ecc9d81 5243 }
39c94593 5244 ofproto_rule_remove__(ofproto, new_rule);
39c94593 5245 ofproto_rule_unref(new_rule);
dd27be82 5246}
79eee1eb 5247
39c94593 5248/* Adds the 'new_rule', replacing the 'old_rule'. */
dd27be82 5249static void
81dee635 5250replace_rule_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5251 const struct openflow_mod_requester *req,
39c94593
JR
5252 struct rule *old_rule, struct rule *new_rule,
5253 struct ovs_list *dead_cookies)
dd27be82
JR
5254 OVS_REQUIRES(ofproto_mutex)
5255{
6787a49f
JR
5256 struct rule *replaced_rule;
5257
5aacc3e2
JR
5258 replaced_rule = (old_rule && old_rule->removed_reason != OFPRR_EVICTION)
5259 ? old_rule : NULL;
dd27be82 5260
6787a49f
JR
5261 /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule'
5262 * is a duplicate rule the 'new_rule' is replacing. The provider should
748eb2f5 5263 * link the packet and byte counts from the old rule to the new one if
8b6987d7
JR
5264 * 'modify_keep_counts' is 'true'. The 'replaced_rule' will be deleted
5265 * right after this call. */
6787a49f 5266 ofproto->ofproto_class->rule_insert(new_rule, replaced_rule,
8b6987d7 5267 ofm->modify_keep_counts);
39c94593
JR
5268 learned_cookies_inc(ofproto, rule_get_actions(new_rule));
5269
5270 if (old_rule) {
5271 const struct rule_actions *old_actions = rule_get_actions(old_rule);
81dee635 5272 const struct rule_actions *new_actions = rule_get_actions(new_rule);
39c94593 5273
39c94593
JR
5274 learned_cookies_dec(ofproto, old_actions, dead_cookies);
5275
6787a49f 5276 if (replaced_rule) {
5bacd5cd
JR
5277 enum nx_flow_update_event event = ofm->command == OFPFC_ADD
5278 ? NXFME_ADDED : NXFME_MODIFIED;
5279
81dee635
JR
5280 bool changed_cookie = (new_rule->flow_cookie
5281 != old_rule->flow_cookie);
6787a49f 5282
81dee635
JR
5283 bool changed_actions = !ofpacts_equal(new_actions->ofpacts,
5284 new_actions->ofpacts_len,
5285 old_actions->ofpacts,
5286 old_actions->ofpacts_len);
6787a49f 5287
5bacd5cd 5288 if (event != NXFME_MODIFIED || changed_actions
81dee635 5289 || changed_cookie) {
5bacd5cd 5290 ofmonitor_report(ofproto->connmgr, new_rule, event, 0,
6787a49f
JR
5291 req ? req->ofconn : NULL,
5292 req ? req->request->xid : 0,
81dee635 5293 changed_actions ? old_actions : NULL);
6787a49f
JR
5294 }
5295 } else {
5296 /* XXX: This is slight duplication with delete_flows_finish__() */
6787a49f
JR
5297 ofmonitor_report(ofproto->connmgr, old_rule, NXFME_DELETED,
5298 OFPRR_EVICTION,
39c94593 5299 req ? req->ofconn : NULL,
6787a49f 5300 req ? req->request->xid : 0, NULL);
39c94593 5301 }
dd27be82 5302 }
9ed18e46
BP
5303}
5304
2c7ee524 5305/* ofm->temp_rule is consumed only in the successful case. */
9387b970 5306static enum ofperr
8be00367 5307modify_flows_start__(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
dd27be82
JR
5308 OVS_REQUIRES(ofproto_mutex)
5309{
8be00367
JR
5310 struct rule_collection *old_rules = &ofm->old_rules;
5311 struct rule_collection *new_rules = &ofm->new_rules;
dd27be82
JR
5312 enum ofperr error;
5313
fe59694b 5314 if (rule_collection_n(old_rules) > 0) {
39c94593 5315 /* Create a new 'modified' rule for each old rule. */
5bacd5cd
JR
5316 struct rule *old_rule, *new_rule;
5317 const struct rule_actions *actions = rule_get_actions(ofm->temp_rule);
39c94593 5318
5bacd5cd
JR
5319 /* Must check actions while holding ofproto_mutex to avoid a race. */
5320 error = ofproto_check_ofpacts(ofproto, actions->ofpacts,
5321 actions->ofpacts_len);
5322 if (error) {
5323 return error;
5324 }
5325
5326 /* Use the temp rule as the first new rule, and as the template for
5327 * the rest. */
5328 struct rule *temp = ofm->temp_rule;
5329 ofm->temp_rule = NULL; /* We consume the template. */
5330
5331 bool first = true;
5332 RULE_COLLECTION_FOR_EACH (old_rule, old_rules) {
5333 if (first) {
5334 /* The template rule's match is possibly a loose one, so it
5335 * must be replaced with the old rule's match so that the new
5336 * rule actually replaces the old one. */
5337 cls_rule_destroy(CONST_CAST(struct cls_rule *, &temp->cr));
5338 cls_rule_clone(CONST_CAST(struct cls_rule *, &temp->cr),
5339 &old_rule->cr);
5c7c16d8
YHW
5340 if (temp->match_tlv_bitmap != old_rule->match_tlv_bitmap) {
5341 mf_vl_mff_unref(&temp->ofproto->vl_mff_map,
5342 temp->match_tlv_bitmap);
5343 temp->match_tlv_bitmap = old_rule->match_tlv_bitmap;
5344 mf_vl_mff_ref(&temp->ofproto->vl_mff_map,
5345 temp->match_tlv_bitmap);
5346 }
5bacd5cd
JR
5347 *CONST_CAST(uint8_t *, &temp->table_id) = old_rule->table_id;
5348 rule_collection_add(new_rules, temp);
5349 first = false;
39c94593 5350 } else {
5bacd5cd
JR
5351 struct cls_rule cr;
5352 cls_rule_clone(&cr, &old_rule->cr);
5353 error = ofproto_rule_create(ofproto, &cr, old_rule->table_id,
5354 temp->flow_cookie,
5355 temp->idle_timeout,
5356 temp->hard_timeout, temp->flags,
5357 temp->importance,
5358 temp->actions->ofpacts,
5359 temp->actions->ofpacts_len,
5c7c16d8
YHW
5360 old_rule->match_tlv_bitmap,
5361 temp->ofpacts_tlv_bitmap,
5bacd5cd
JR
5362 &new_rule);
5363 if (!error) {
5364 rule_collection_add(new_rules, new_rule);
5365 } else {
2c7ee524
JR
5366 /* Return the template rule in place in the error case. */
5367 ofm->temp_rule = temp;
5368 rule_collection_rules(new_rules)[0] = NULL;
5369
5bacd5cd
JR
5370 rule_collection_unref(new_rules);
5371 rule_collection_destroy(new_rules);
5372 return error;
5373 }
39c94593
JR
5374 }
5375 }
fe59694b
JR
5376 ovs_assert(rule_collection_n(new_rules)
5377 == rule_collection_n(old_rules));
39c94593 5378
fe59694b 5379 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
5bacd5cd 5380 replace_rule_start(ofproto, ofm, old_rule, new_rule);
39c94593 5381 }
81dee635 5382 } else if (ofm->modify_may_add_flow) {
5bacd5cd 5383 /* No match, add a new flow, consumes 'temp'. */
8be00367 5384 error = add_flow_start(ofproto, ofm);
dd27be82 5385 } else {
2c7ee524
JR
5386 /* No flow to modify and may not add a flow. */
5387 ofproto_rule_unref(ofm->temp_rule);
5388 ofm->temp_rule = NULL; /* We consume the template. */
d99f64d7 5389 error = 0;
dd27be82 5390 }
39c94593 5391
dd27be82
JR
5392 return error;
5393}
5394
5bacd5cd
JR
5395static enum ofperr
5396modify_flows_init_loose(struct ofproto *ofproto,
5397 struct ofproto_flow_mod *ofm,
5398 const struct ofputil_flow_mod *fm)
5399 OVS_EXCLUDED(ofproto_mutex)
5400{
5401 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0,
5402 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY,
5403 OFPG_ANY);
5404 rule_criteria_require_rw(&ofm->criteria,
5405 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5406 /* Must create a new flow in advance for the case that no matches are
5407 * found. Also used for template for multiple modified flows. */
5408 add_flow_init(ofproto, ofm, fm);
5409
5410 return 0;
5411}
5412
90bf1e07 5413/* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
c184807c 5414 * failure. */
90bf1e07 5415static enum ofperr
8be00367 5416modify_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5417 OVS_REQUIRES(ofproto_mutex)
9ed18e46 5418{
8be00367 5419 struct rule_collection *old_rules = &ofm->old_rules;
d51c8b71 5420 enum ofperr error;
9ed18e46 5421
5bacd5cd 5422 error = collect_rules_loose(ofproto, &ofm->criteria, old_rules);
a9b22b7f 5423
a8e547c1 5424 if (!error) {
8be00367 5425 error = modify_flows_start__(ofproto, ofm);
d99f64d7
JR
5426 }
5427
5428 if (error) {
39c94593 5429 rule_collection_destroy(old_rules);
d99f64d7 5430 }
5bacd5cd 5431
d99f64d7
JR
5432 return error;
5433}
5434
1f42be1c 5435static void
8be00367 5436modify_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
5437 OVS_REQUIRES(ofproto_mutex)
5438{
8be00367
JR
5439 struct rule_collection *old_rules = &ofm->old_rules;
5440 struct rule_collection *new_rules = &ofm->new_rules;
5441
39c94593 5442 /* Old rules were not changed yet, only need to revert new rules. */
5bacd5cd 5443 if (rule_collection_n(old_rules) > 0) {
fe59694b
JR
5444 struct rule *old_rule, *new_rule;
5445 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
5446 replace_rule_revert(ofproto, old_rule, new_rule);
39c94593
JR
5447 }
5448 rule_collection_destroy(new_rules);
5449 rule_collection_destroy(old_rules);
1f42be1c 5450 }
1f42be1c
JR
5451}
5452
d99f64d7 5453static void
8be00367 5454modify_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5455 const struct openflow_mod_requester *req)
d99f64d7
JR
5456 OVS_REQUIRES(ofproto_mutex)
5457{
8be00367
JR
5458 struct rule_collection *old_rules = &ofm->old_rules;
5459 struct rule_collection *new_rules = &ofm->new_rules;
5460
fe59694b
JR
5461 if (rule_collection_n(old_rules) == 0
5462 && rule_collection_n(new_rules) == 1) {
8be00367 5463 add_flow_finish(ofproto, ofm, req);
fe59694b 5464 } else if (rule_collection_n(old_rules) > 0) {
39c94593
JR
5465 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
5466
fe59694b
JR
5467 ovs_assert(rule_collection_n(new_rules)
5468 == rule_collection_n(old_rules));
39c94593 5469
fe59694b
JR
5470 struct rule *old_rule, *new_rule;
5471 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
81dee635 5472 replace_rule_finish(ofproto, ofm, req, old_rule, new_rule,
fe59694b 5473 &dead_cookies);
39c94593
JR
5474 }
5475 learned_cookies_flush(ofproto, &dead_cookies);
cc099268 5476 remove_rules_postponed(old_rules);
d99f64d7 5477 }
d99f64d7
JR
5478}
5479
5bacd5cd
JR
5480static enum ofperr
5481modify_flow_init_strict(struct ofproto *ofproto OVS_UNUSED,
5482 struct ofproto_flow_mod *ofm,
5483 const struct ofputil_flow_mod *fm)
5484 OVS_EXCLUDED(ofproto_mutex)
5485{
5486 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority,
5487 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY,
5488 OFPG_ANY);
5489 rule_criteria_require_rw(&ofm->criteria,
5490 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5491 /* Must create a new flow in advance for the case that no matches are
5492 * found. Also used for template for multiple modified flows. */
5493 add_flow_init(ofproto, ofm, fm);
5494
5495 return 0;
5496}
5497
79eee1eb 5498/* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
baae3d02 5499 * code on failure. */
90bf1e07 5500static enum ofperr
8be00367 5501modify_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5502 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5503{
8be00367 5504 struct rule_collection *old_rules = &ofm->old_rules;
d51c8b71 5505 enum ofperr error;
6c1491fb 5506
5bacd5cd 5507 error = collect_rules_strict(ofproto, &ofm->criteria, old_rules);
a9b22b7f 5508
a8e547c1 5509 if (!error) {
dd27be82 5510 /* collect_rules_strict() can return max 1 rule. */
8be00367 5511 error = modify_flows_start__(ofproto, ofm);
d99f64d7
JR
5512 }
5513
d99f64d7
JR
5514 return error;
5515}
9ed18e46
BP
5516\f
5517/* OFPFC_DELETE implementation. */
79eee1eb 5518
254750ce 5519static void
44e0c35d 5520delete_flows_start__(struct ofproto *ofproto, ovs_version_t version,
39c94593
JR
5521 const struct rule_collection *rules)
5522 OVS_REQUIRES(ofproto_mutex)
5523{
fe59694b
JR
5524 struct rule *rule;
5525
5526 RULE_COLLECTION_FOR_EACH (rule, rules) {
d79e3d70
JR
5527 struct oftable *table = &ofproto->tables[rule->table_id];
5528
5529 table->n_flows--;
8be00367 5530 cls_rule_make_invisible_in_version(&rule->cr, version);
9bab38ff
JR
5531
5532 /* Remove rule from ofproto data structures. */
5533 ofproto_rule_remove__(ofproto, rule);
39c94593
JR
5534 }
5535}
5536
25070e04
JR
5537static void
5538delete_flows_revert__(struct ofproto *ofproto,
5539 const struct rule_collection *rules)
5540 OVS_REQUIRES(ofproto_mutex)
5541{
5542 struct rule *rule;
5543
5544 RULE_COLLECTION_FOR_EACH (rule, rules) {
5545 struct oftable *table = &ofproto->tables[rule->table_id];
5546
5547 /* Add rule back to ofproto data structures. */
5548 ofproto_rule_insert__(ofproto, rule);
5549
5550 /* Restore table's rule count. */
5551 table->n_flows++;
5552
5553 /* Restore the original visibility of the rule. */
5554 cls_rule_restore_visibility(&rule->cr);
5555 }
5556}
5557
39c94593
JR
5558static void
5559delete_flows_finish__(struct ofproto *ofproto,
5560 struct rule_collection *rules,
5561 enum ofp_flow_removed_reason reason,
0a0d9385 5562 const struct openflow_mod_requester *req)
15aaf599 5563 OVS_REQUIRES(ofproto_mutex)
064af421 5564{
fe59694b 5565 if (rule_collection_n(rules)) {
55951e15 5566 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
fe59694b 5567 struct rule *rule;
79eee1eb 5568
fe59694b 5569 RULE_COLLECTION_FOR_EACH (rule, rules) {
f695ebfa
JR
5570 /* This value will be used to send the flow removed message right
5571 * before the rule is actually destroyed. */
5572 rule->removed_reason = reason;
5573
9ca4a86f 5574 ofmonitor_report(ofproto->connmgr, rule, NXFME_DELETED, reason,
c84d8691
JR
5575 req ? req->ofconn : NULL,
5576 req ? req->request->xid : 0, NULL);
6c6eedc5
SJ
5577
5578 /* Send Vacancy Event for OF1.4+. */
5579 send_table_status(ofproto, rule->table_id);
5580
802f84ff
JR
5581 learned_cookies_dec(ofproto, rule_get_actions(rule),
5582 &dead_cookies);
9ca4a86f 5583 }
cc099268 5584 remove_rules_postponed(rules);
39c94593 5585
35f48b8b 5586 learned_cookies_flush(ofproto, &dead_cookies);
39c94593
JR
5587 }
5588}
5589
5590/* Deletes the rules listed in 'rules'.
5591 * The deleted rules will become invisible to the lookups in the next version.
5592 * Destroys 'rules'. */
5593static void
5594delete_flows__(struct rule_collection *rules,
5595 enum ofp_flow_removed_reason reason,
0a0d9385 5596 const struct openflow_mod_requester *req)
39c94593
JR
5597 OVS_REQUIRES(ofproto_mutex)
5598{
fe59694b
JR
5599 if (rule_collection_n(rules)) {
5600 struct ofproto *ofproto = rule_collection_rules(rules)[0]->ofproto;
39c94593 5601
8be00367 5602 delete_flows_start__(ofproto, ofproto->tables_version + 1, rules);
39c94593
JR
5603 ofproto_bump_tables_version(ofproto);
5604 delete_flows_finish__(ofproto, rules, reason, req);
9ca4a86f
BP
5605 ofmonitor_flush(ofproto->connmgr);
5606 }
79eee1eb 5607}
79eee1eb 5608
5bacd5cd
JR
5609static enum ofperr
5610delete_flows_init_loose(struct ofproto *ofproto OVS_UNUSED,
5611 struct ofproto_flow_mod *ofm,
5612 const struct ofputil_flow_mod *fm)
5613 OVS_EXCLUDED(ofproto_mutex)
5614{
5615 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0,
5616 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask,
5617 fm->out_port, fm->out_group);
5618 rule_criteria_require_rw(&ofm->criteria,
5619 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5620 return 0;
5621}
5622
79eee1eb 5623/* Implements OFPFC_DELETE. */
90bf1e07 5624static enum ofperr
8be00367 5625delete_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5626 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5627{
8be00367 5628 struct rule_collection *rules = &ofm->old_rules;
90bf1e07 5629 enum ofperr error;
064af421 5630
5bacd5cd 5631 error = collect_rules_loose(ofproto, &ofm->criteria, rules);
a9b22b7f 5632
802f84ff 5633 if (!error) {
8be00367 5634 delete_flows_start__(ofproto, ofm->version, rules);
a8e547c1 5635 }
a8e547c1
BP
5636
5637 return error;
064af421
BP
5638}
5639
ce59413f 5640static void
8be00367 5641delete_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
ce59413f
JR
5642 OVS_REQUIRES(ofproto_mutex)
5643{
25070e04 5644 delete_flows_revert__(ofproto, &ofm->old_rules);
ce59413f
JR
5645}
5646
1f42be1c 5647static void
2c7ee524 5648delete_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5649 const struct openflow_mod_requester *req)
15aaf599 5650 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5651{
5aacc3e2 5652 delete_flows_finish__(ofproto, &ofm->old_rules, OFPRR_DELETE, req);
ce59413f
JR
5653}
5654
5bacd5cd
JR
5655static enum ofperr
5656delete_flows_init_strict(struct ofproto *ofproto OVS_UNUSED,
5657 struct ofproto_flow_mod *ofm,
5658 const struct ofputil_flow_mod *fm)
5659 OVS_EXCLUDED(ofproto_mutex)
5660{
5661 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority,
5662 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask,
5663 fm->out_port, fm->out_group);
5664 rule_criteria_require_rw(&ofm->criteria,
5665 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5666 return 0;
5667}
5668
ce59413f
JR
5669/* Implements OFPFC_DELETE_STRICT. */
5670static enum ofperr
2c7ee524 5671delete_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
ce59413f
JR
5672 OVS_REQUIRES(ofproto_mutex)
5673{
8be00367 5674 struct rule_collection *rules = &ofm->old_rules;
ce59413f
JR
5675 enum ofperr error;
5676
5bacd5cd 5677 error = collect_rules_strict(ofproto, &ofm->criteria, rules);
a9b22b7f 5678
802f84ff 5679 if (!error) {
8be00367 5680 delete_flows_start__(ofproto, ofm->version, rules);
ce59413f
JR
5681 }
5682
5683 return error;
5684}
5685
f695ebfa 5686/* This may only be called by rule_destroy_cb()! */
abe529af 5687static void
f695ebfa
JR
5688ofproto_rule_send_removed(struct rule *rule)
5689 OVS_EXCLUDED(ofproto_mutex)
abe529af
BP
5690{
5691 struct ofputil_flow_removed fr;
dc437090 5692 long long int used;
abe529af 5693
5cb7a798 5694 minimatch_expand(&rule->cr.match, &fr.match);
81a76618 5695 fr.priority = rule->cr.priority;
f695ebfa 5696
25010c68
JR
5697 /* Synchronize with connmgr_destroy() calls to prevent connmgr disappearing
5698 * while we use it. */
f695ebfa 5699 ovs_mutex_lock(&ofproto_mutex);
25010c68
JR
5700 struct connmgr *connmgr = rule->ofproto->connmgr;
5701 if (!connmgr) {
5702 ovs_mutex_unlock(&ofproto_mutex);
5703 return;
5704 }
5705
abe529af 5706 fr.cookie = rule->flow_cookie;
f695ebfa 5707 fr.reason = rule->removed_reason;
95216219 5708 fr.table_id = rule->table_id;
65e0be10
BP
5709 calc_duration(rule->created, time_msec(),
5710 &fr.duration_sec, &fr.duration_nsec);
d10976b7 5711 ovs_mutex_lock(&rule->mutex);
abe529af 5712 fr.idle_timeout = rule->idle_timeout;
fa2bad0f 5713 fr.hard_timeout = rule->hard_timeout;
d10976b7 5714 ovs_mutex_unlock(&rule->mutex);
abe529af 5715 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
dc437090 5716 &fr.byte_count, &used);
25010c68 5717 connmgr_send_flow_removed(connmgr, &fr);
f695ebfa 5718 ovs_mutex_unlock(&ofproto_mutex);
abe529af
BP
5719}
5720
5721/* Sends an OpenFlow "flow removed" message with the given 'reason' (either
5722 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
5723 * ofproto.
5724 *
5725 * ofproto implementation ->run() functions should use this function to expire
5726 * OpenFlow flows. */
5727void
5728ofproto_rule_expire(struct rule *rule, uint8_t reason)
15aaf599 5729 OVS_REQUIRES(ofproto_mutex)
abe529af 5730{
802f84ff
JR
5731 struct rule_collection rules;
5732
fe59694b
JR
5733 rule_collection_init(&rules);
5734 rule_collection_add(&rules, rule);
802f84ff 5735 delete_flows__(&rules, reason, NULL);
79eee1eb 5736}
994c9973
BP
5737
5738/* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5739 * means "infinite". */
5740static void
5741reduce_timeout(uint16_t max, uint16_t *timeout)
5742{
5743 if (max && (!*timeout || *timeout > max)) {
5744 *timeout = max;
5745 }
5746}
5747
5748/* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
5749 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
5750 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
5751 *
5752 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
1f4a8933
JR
5753void
5754ofproto_rule_reduce_timeouts__(struct rule *rule,
5755 uint16_t idle_timeout, uint16_t hard_timeout)
5756 OVS_REQUIRES(ofproto_mutex)
5757 OVS_EXCLUDED(rule->mutex)
5758{
5759 if (!idle_timeout && !hard_timeout) {
5760 return;
5761 }
5762
5763 if (ovs_list_is_empty(&rule->expirable)) {
5764 ovs_list_insert(&rule->ofproto->expirable, &rule->expirable);
5765 }
5766
5767 ovs_mutex_lock(&rule->mutex);
5768 reduce_timeout(idle_timeout, &rule->idle_timeout);
5769 reduce_timeout(hard_timeout, &rule->hard_timeout);
5770 ovs_mutex_unlock(&rule->mutex);
5771}
5772
994c9973
BP
5773void
5774ofproto_rule_reduce_timeouts(struct rule *rule,
5775 uint16_t idle_timeout, uint16_t hard_timeout)
d10976b7 5776 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
994c9973
BP
5777{
5778 if (!idle_timeout && !hard_timeout) {
5779 return;
5780 }
5781
abe7b10f 5782 ovs_mutex_lock(&ofproto_mutex);
417e7e66
BW
5783 if (ovs_list_is_empty(&rule->expirable)) {
5784 ovs_list_insert(&rule->ofproto->expirable, &rule->expirable);
994c9973 5785 }
abe7b10f 5786 ovs_mutex_unlock(&ofproto_mutex);
994c9973 5787
d10976b7 5788 ovs_mutex_lock(&rule->mutex);
994c9973
BP
5789 reduce_timeout(idle_timeout, &rule->idle_timeout);
5790 reduce_timeout(hard_timeout, &rule->hard_timeout);
d10976b7 5791 ovs_mutex_unlock(&rule->mutex);
994c9973 5792}
79eee1eb 5793\f
90bf1e07 5794static enum ofperr
2e4f5fcf 5795handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 5796 OVS_EXCLUDED(ofproto_mutex)
064af421 5797{
a5b8d268 5798 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7338102b 5799 struct ofputil_flow_mod fm;
f25d0cf3
BP
5800 uint64_t ofpacts_stub[1024 / 8];
5801 struct ofpbuf ofpacts;
90bf1e07 5802 enum ofperr error;
064af421 5803
76589937 5804 error = reject_slave_controller(ofconn);
9deba63b 5805 if (error) {
b0d38b2f 5806 return error;
9deba63b 5807 }
3052b0c5 5808
f25d0cf3 5809 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
7338102b 5810 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
04f48a68
YHW
5811 ofproto_get_tun_tab(ofproto),
5812 &ofproto->vl_mff_map, &ofpacts,
7e9f8266
BP
5813 u16_to_ofp(ofproto->max_ports),
5814 ofproto->n_tables);
548de4dd 5815 if (!error) {
0a0d9385 5816 struct openflow_mod_requester req = { ofconn, oh };
7338102b 5817 error = handle_flow_mod__(ofproto, &fm, &req);
49bdc010 5818 }
2e310801 5819
f25d0cf3 5820 ofpbuf_uninit(&ofpacts);
f25d0cf3 5821 return error;
75a75043
BP
5822}
5823
90bf1e07 5824static enum ofperr
7338102b 5825handle_flow_mod__(struct ofproto *ofproto, const struct ofputil_flow_mod *fm,
0a0d9385 5826 const struct openflow_mod_requester *req)
15aaf599 5827 OVS_EXCLUDED(ofproto_mutex)
75a75043 5828{
7338102b 5829 struct ofproto_flow_mod ofm;
35412852 5830 enum ofperr error;
75a75043 5831
2c7ee524 5832 error = ofproto_flow_mod_init(ofproto, &ofm, fm, NULL);
5bacd5cd
JR
5833 if (error) {
5834 return error;
5835 }
7338102b 5836
15aaf599 5837 ovs_mutex_lock(&ofproto_mutex);
7338102b
JR
5838 ofm.version = ofproto->tables_version + 1;
5839 error = ofproto_flow_mod_start(ofproto, &ofm);
1f42be1c 5840 if (!error) {
39c94593 5841 ofproto_bump_tables_version(ofproto);
7338102b 5842 ofproto_flow_mod_finish(ofproto, &ofm, req);
2c7ee524 5843 ofmonitor_flush(ofproto->connmgr);
3052b0c5 5844 }
15aaf599 5845 ovs_mutex_unlock(&ofproto_mutex);
35412852 5846
35412852 5847 return error;
3052b0c5
BP
5848}
5849
90bf1e07 5850static enum ofperr
d1e2cf21 5851handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
9deba63b 5852{
f4f1ea7e
BP
5853 struct ofputil_role_request request;
5854 struct ofputil_role_request reply;
9deba63b 5855 struct ofpbuf *buf;
6ea4776b
JR
5856 enum ofperr error;
5857
f4f1ea7e 5858 error = ofputil_decode_role_message(oh, &request);
6ea4776b
JR
5859 if (error) {
5860 return error;
5861 }
9deba63b 5862
f4f1ea7e 5863 if (request.role != OFPCR12_ROLE_NOCHANGE) {
d26eda9f
BP
5864 if (request.role != OFPCR12_ROLE_EQUAL
5865 && request.have_generation_id
f4f1ea7e
BP
5866 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
5867 return OFPERR_OFPRRFC_STALE;
6ea4776b 5868 }
6ea4776b 5869
f4f1ea7e
BP
5870 ofconn_set_role(ofconn, request.role);
5871 }
9deba63b 5872
f4f1ea7e 5873 reply.role = ofconn_get_role(ofconn);
147cc9d3
BP
5874 reply.have_generation_id = ofconn_get_master_election_id(
5875 ofconn, &reply.generation_id);
f4f1ea7e 5876 buf = ofputil_encode_role_reply(oh, &reply);
b0421aa2 5877 ofconn_send_reply(ofconn, buf);
9deba63b
BP
5878
5879 return 0;
5880}
5881
90bf1e07 5882static enum ofperr
6c1491fb
BP
5883handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
5884 const struct ofp_header *oh)
5885{
982697a4 5886 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
27527aa0
BP
5887 enum ofputil_protocol cur, next;
5888
5889 cur = ofconn_get_protocol(ofconn);
5890 next = ofputil_protocol_set_tid(cur, msg->set != 0);
5891 ofconn_set_protocol(ofconn, next);
6c1491fb 5892
6c1491fb
BP
5893 return 0;
5894}
5895
90bf1e07 5896static enum ofperr
d1e2cf21 5897handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
09246b99 5898{
982697a4 5899 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
27527aa0
BP
5900 enum ofputil_protocol cur, next;
5901 enum ofputil_protocol next_base;
09246b99 5902
27527aa0
BP
5903 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
5904 if (!next_base) {
90bf1e07 5905 return OFPERR_OFPBRC_EPERM;
09246b99 5906 }
7ee20df1 5907
27527aa0
BP
5908 cur = ofconn_get_protocol(ofconn);
5909 next = ofputil_protocol_set_base(cur, next_base);
27527aa0 5910 ofconn_set_protocol(ofconn, next);
b20f4073 5911
7ee20df1 5912 return 0;
09246b99
BP
5913}
5914
90bf1e07 5915static enum ofperr
54834960
EJ
5916handle_nxt_set_packet_in_format(struct ofconn *ofconn,
5917 const struct ofp_header *oh)
5918{
982697a4 5919 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
54834960
EJ
5920 uint32_t format;
5921
54834960 5922 format = ntohl(msg->format);
6409e008 5923 if (!ofputil_packet_in_format_is_valid(format)) {
90bf1e07 5924 return OFPERR_OFPBRC_EPERM;
54834960
EJ
5925 }
5926
54834960
EJ
5927 ofconn_set_packet_in_format(ofconn, format);
5928 return 0;
5929}
5930
80d5aefd
BP
5931static enum ofperr
5932handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
5933{
5876b4db
BP
5934 struct ofputil_async_cfg basis = ofconn_get_async_config(ofconn);
5935 struct ofputil_async_cfg ac;
d18cc1ee 5936 enum ofperr error;
80d5aefd 5937
5876b4db 5938 error = ofputil_decode_set_async_config(oh, false, &basis, &ac);
d18cc1ee
AA
5939 if (error) {
5940 return error;
5941 }
80d5aefd 5942
a930d4c5 5943 ofconn_set_async_config(ofconn, &ac);
4550b647
MM
5944 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
5945 !ofconn_get_miss_send_len(ofconn)) {
5946 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
5947 }
80d5aefd
BP
5948
5949 return 0;
5950}
5951
c423b3b3
AC
5952static enum ofperr
5953handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
5954{
a930d4c5 5955 struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn);
af7bc161 5956 ofconn_send_reply(ofconn, ofputil_encode_get_async_reply(oh, &ac));
c423b3b3
AC
5957
5958 return 0;
5959}
5960
a7349929
BP
5961static enum ofperr
5962handle_nxt_set_controller_id(struct ofconn *ofconn,
5963 const struct ofp_header *oh)
5964{
982697a4 5965 const struct nx_controller_id *nci = ofpmsg_body(oh);
a7349929 5966
a7349929
BP
5967 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
5968 return OFPERR_NXBRC_MUST_BE_ZERO;
5969 }
5970
5971 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
5972 return 0;
5973}
5974
90bf1e07 5975static enum ofperr
d1e2cf21 5976handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
246e61ea 5977{
246e61ea
JP
5978 struct ofpbuf *buf;
5979
982697a4
BP
5980 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
5981 ? OFPRAW_OFPT10_BARRIER_REPLY
5982 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
b0421aa2 5983 ofconn_send_reply(ofconn, buf);
246e61ea
JP
5984 return 0;
5985}
5986
2b07c8b1
BP
5987static void
5988ofproto_compose_flow_refresh_update(const struct rule *rule,
5989 enum nx_flow_monitor_flags flags,
140f36ba
DDP
5990 struct ovs_list *msgs,
5991 const struct tun_table *tun_table)
15aaf599 5992 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 5993{
6f00e29b 5994 const struct rule_actions *actions;
2b07c8b1
BP
5995 struct ofputil_flow_update fu;
5996
2b07c8b1
BP
5997 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
5998 ? NXFME_ADDED : NXFME_MODIFIED);
5999 fu.reason = 0;
d10976b7 6000 ovs_mutex_lock(&rule->mutex);
2b07c8b1
BP
6001 fu.idle_timeout = rule->idle_timeout;
6002 fu.hard_timeout = rule->hard_timeout;
d10976b7 6003 ovs_mutex_unlock(&rule->mutex);
2b07c8b1
BP
6004 fu.table_id = rule->table_id;
6005 fu.cookie = rule->flow_cookie;
140f36ba 6006 minimatch_expand(&rule->cr.match, &fu.match);
6b140a4e 6007 fu.priority = rule->cr.priority;
6f00e29b 6008
b20f4073 6009 actions = flags & NXFMF_ACTIONS ? rule_get_actions(rule) : NULL;
6f00e29b
BP
6010 fu.ofpacts = actions ? actions->ofpacts : NULL;
6011 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
2b07c8b1 6012
417e7e66 6013 if (ovs_list_is_empty(msgs)) {
2b07c8b1
BP
6014 ofputil_start_flow_update(msgs);
6015 }
140f36ba 6016 ofputil_append_flow_update(&fu, msgs, tun_table);
2b07c8b1
BP
6017}
6018
6019void
a8e547c1 6020ofmonitor_compose_refresh_updates(struct rule_collection *rules,
ca6ba700 6021 struct ovs_list *msgs)
15aaf599 6022 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 6023{
fe59694b 6024 struct rule *rule;
2b07c8b1 6025
fe59694b 6026 RULE_COLLECTION_FOR_EACH (rule, rules) {
2b07c8b1
BP
6027 enum nx_flow_monitor_flags flags = rule->monitor_flags;
6028 rule->monitor_flags = 0;
6029
140f36ba
DDP
6030 ofproto_compose_flow_refresh_update(rule, flags, msgs,
6031 ofproto_get_tun_tab(rule->ofproto));
2b07c8b1
BP
6032 }
6033}
6034
6035static void
6036ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
6037 struct rule *rule, uint64_t seqno,
a8e547c1 6038 struct rule_collection *rules)
15aaf599 6039 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6040{
6041 enum nx_flow_monitor_flags update;
6042
5e8b38b0 6043 if (rule_is_hidden(rule)) {
2b07c8b1
BP
6044 return;
6045 }
6046
b20f4073 6047 if (!ofproto_rule_has_out_port(rule, m->out_port)) {
2b07c8b1
BP
6048 return;
6049 }
6050
6051 if (seqno) {
6052 if (rule->add_seqno > seqno) {
6053 update = NXFMF_ADD | NXFMF_MODIFY;
6054 } else if (rule->modify_seqno > seqno) {
6055 update = NXFMF_MODIFY;
6056 } else {
6057 return;
6058 }
6059
6060 if (!(m->flags & update)) {
6061 return;
6062 }
6063 } else {
6064 update = NXFMF_INITIAL;
6065 }
6066
6067 if (!rule->monitor_flags) {
a8e547c1 6068 rule_collection_add(rules, rule);
2b07c8b1
BP
6069 }
6070 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
6071}
6072
6073static void
6074ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
6075 uint64_t seqno,
a8e547c1 6076 struct rule_collection *rules)
15aaf599 6077 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6078{
6079 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
2b07c8b1 6080 const struct oftable *table;
81a76618 6081 struct cls_rule target;
2b07c8b1 6082
bd53aa17 6083 cls_rule_init_from_minimatch(&target, &m->match, 0);
2b07c8b1 6084 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
2b07c8b1
BP
6085 struct rule *rule;
6086
44e0c35d 6087 CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &target, OVS_VERSION_MAX) {
2b07c8b1
BP
6088 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
6089 }
6090 }
48d28ac1 6091 cls_rule_destroy(&target);
2b07c8b1
BP
6092}
6093
6094static void
6095ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
a8e547c1 6096 struct rule_collection *rules)
15aaf599 6097 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6098{
6099 if (m->flags & NXFMF_INITIAL) {
6100 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
6101 }
6102}
6103
6104void
6105ofmonitor_collect_resume_rules(struct ofmonitor *m,
a8e547c1 6106 uint64_t seqno, struct rule_collection *rules)
15aaf599 6107 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6108{
6109 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
6110}
6111
7b900689
SH
6112static enum ofperr
6113flow_monitor_delete(struct ofconn *ofconn, uint32_t id)
6114 OVS_REQUIRES(ofproto_mutex)
6115{
6116 struct ofmonitor *m;
6117 enum ofperr error;
6118
6119 m = ofmonitor_lookup(ofconn, id);
6120 if (m) {
6121 ofmonitor_destroy(m);
6122 error = 0;
6123 } else {
6124 error = OFPERR_OFPMOFC_UNKNOWN_MONITOR;
6125 }
6126
6127 return error;
6128}
6129
2b07c8b1 6130static enum ofperr
982697a4 6131handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 6132 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1
BP
6133{
6134 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2b07c8b1 6135
0a2869d5
BP
6136 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
6137
6138 struct ofmonitor **monitors = NULL;
6139 size_t allocated_monitors = 0;
6140 size_t n_monitors = 0;
6141
6142 enum ofperr error;
15aaf599
BP
6143
6144 ovs_mutex_lock(&ofproto_mutex);
2b07c8b1
BP
6145 for (;;) {
6146 struct ofputil_flow_monitor_request request;
6147 struct ofmonitor *m;
6148 int retval;
6149
6150 retval = ofputil_decode_flow_monitor_request(&request, &b);
6151 if (retval == EOF) {
6152 break;
6153 } else if (retval) {
6154 error = retval;
6155 goto error;
6156 }
6157
6158 if (request.table_id != 0xff
6159 && request.table_id >= ofproto->n_tables) {
6160 error = OFPERR_OFPBRC_BAD_TABLE_ID;
6161 goto error;
6162 }
6163
6164 error = ofmonitor_create(&request, ofconn, &m);
6165 if (error) {
6166 goto error;
6167 }
6168
6169 if (n_monitors >= allocated_monitors) {
6170 monitors = x2nrealloc(monitors, &allocated_monitors,
6171 sizeof *monitors);
6172 }
6173 monitors[n_monitors++] = m;
6174 }
6175
0a2869d5 6176 struct rule_collection rules;
a8e547c1 6177 rule_collection_init(&rules);
0a2869d5 6178 for (size_t i = 0; i < n_monitors; i++) {
2b07c8b1
BP
6179 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
6180 }
6181
0a2869d5 6182 struct ovs_list replies;
982697a4 6183 ofpmp_init(&replies, oh);
2b07c8b1 6184 ofmonitor_compose_refresh_updates(&rules, &replies);
15aaf599
BP
6185 ovs_mutex_unlock(&ofproto_mutex);
6186
a8e547c1
BP
6187 rule_collection_destroy(&rules);
6188
2b07c8b1 6189 ofconn_send_replies(ofconn, &replies);
2b07c8b1
BP
6190 free(monitors);
6191
6192 return 0;
6193
6194error:
0a2869d5 6195 for (size_t i = 0; i < n_monitors; i++) {
2b07c8b1
BP
6196 ofmonitor_destroy(monitors[i]);
6197 }
6198 free(monitors);
4cd1bc9d
BP
6199 ovs_mutex_unlock(&ofproto_mutex);
6200
2b07c8b1
BP
6201 return error;
6202}
6203
6204static enum ofperr
6205handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 6206 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1 6207{
15aaf599 6208 enum ofperr error;
2b07c8b1
BP
6209 uint32_t id;
6210
6211 id = ofputil_decode_flow_monitor_cancel(oh);
15aaf599
BP
6212
6213 ovs_mutex_lock(&ofproto_mutex);
7b900689 6214 error = flow_monitor_delete(ofconn, id);
15aaf599 6215 ovs_mutex_unlock(&ofproto_mutex);
2b07c8b1 6216
15aaf599 6217 return error;
2b07c8b1
BP
6218}
6219
9cae45dc
JR
6220/* Meters implementation.
6221 *
6222 * Meter table entry, indexed by the OpenFlow meter_id.
9cae45dc
JR
6223 * 'created' is used to compute the duration for meter stats.
6224 * 'list rules' is needed so that we can delete the dependent rules when the
6225 * meter table entry is deleted.
6226 * 'provider_meter_id' is for the provider's private use.
6227 */
6228struct meter {
ba8cff36 6229 struct hmap_node node; /* In ofproto->meters. */
9cae45dc 6230 long long int created; /* Time created. */
ca6ba700 6231 struct ovs_list rules; /* List of "struct rule_dpif"s. */
ba8cff36 6232 uint32_t id; /* OpenFlow meter_id. */
9cae45dc
JR
6233 ofproto_meter_id provider_meter_id;
6234 uint16_t flags; /* Meter flags. */
6235 uint16_t n_bands; /* Number of meter bands. */
6236 struct ofputil_meter_band *bands;
6237};
6238
ba8cff36
AZ
6239static struct meter *
6240ofproto_get_meter(const struct ofproto *ofproto, uint32_t meter_id)
6241{
6242 struct meter *meter;
6243 uint32_t hash = hash_int(meter_id, 0);
6244
6245 HMAP_FOR_EACH_WITH_HASH (meter, node, hash, &ofproto->meters) {
6246 if (meter->id == meter_id) {
6247 return meter;
6248 }
6249 }
6250
6251 return NULL;
6252}
6253
9e638f22
AZ
6254static uint32_t *
6255ofproto_upcall_meter_ptr(struct ofproto *ofproto, uint32_t meter_id)
6256{
6257 switch(meter_id) {
6258 case OFPM13_SLOWPATH:
6259 return &ofproto->slowpath_meter_id;
6260 break;
6261 case OFPM13_CONTROLLER:
6262 return &ofproto->controller_meter_id;
6263 break;
6264 case OFPM13_ALL:
6265 OVS_NOT_REACHED();
6266 default:
6267 return NULL;
6268 }
6269}
6270
ba8cff36
AZ
6271static void
6272ofproto_add_meter(struct ofproto *ofproto, struct meter *meter)
6273{
9e638f22
AZ
6274 uint32_t *upcall_meter_ptr = ofproto_upcall_meter_ptr(ofproto, meter->id);
6275
6276 /* Cache datapath meter IDs of special meters. */
6277 if (upcall_meter_ptr) {
6278 *upcall_meter_ptr = meter->provider_meter_id.uint32;
6279 }
6280
ba8cff36
AZ
6281 hmap_insert(&ofproto->meters, &meter->node, hash_int(meter->id, 0));
6282}
6283
9cae45dc 6284/*
076caa2f
JR
6285 * This is used in instruction validation at flow set-up time, to map
6286 * the OpenFlow meter ID to the corresponding datapath provider meter
6287 * ID. If either does not exist, returns false. Otherwise updates
6288 * the meter action and returns true.
9cae45dc 6289 */
076caa2f
JR
6290static bool
6291ofproto_fix_meter_action(const struct ofproto *ofproto,
6292 struct ofpact_meter *ma)
6293{
ba8cff36
AZ
6294 if (ma->meter_id) {
6295 const struct meter *meter = ofproto_get_meter(ofproto, ma->meter_id);
076caa2f
JR
6296
6297 if (meter && meter->provider_meter_id.uint32 != UINT32_MAX) {
6298 /* Update the action with the provider's meter ID, so that we
6299 * do not need any synchronization between ofproto_dpif_xlate
6300 * and ofproto for meter table access. */
6301 ma->provider_meter_id = meter->provider_meter_id.uint32;
6302 return true;
9cae45dc
JR
6303 }
6304 }
076caa2f 6305 return false;
9cae45dc
JR
6306}
6307
062fea06
BP
6308/* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
6309 * list of rules. */
6310static void
6311meter_insert_rule(struct rule *rule)
6312{
6313 const struct rule_actions *a = rule_get_actions(rule);
6314 uint32_t meter_id = ofpacts_get_meter(a->ofpacts, a->ofpacts_len);
ba8cff36 6315 struct meter *meter = ofproto_get_meter(rule->ofproto, meter_id);
062fea06 6316
417e7e66 6317 ovs_list_insert(&meter->rules, &rule->meter_list_node);
062fea06
BP
6318}
6319
9cae45dc
JR
6320static void
6321meter_update(struct meter *meter, const struct ofputil_meter_config *config)
6322{
6323 free(meter->bands);
6324
6325 meter->flags = config->flags;
6326 meter->n_bands = config->n_bands;
6327 meter->bands = xmemdup(config->bands,
6328 config->n_bands * sizeof *meter->bands);
6329}
6330
6331static struct meter *
6332meter_create(const struct ofputil_meter_config *config,
6333 ofproto_meter_id provider_meter_id)
6334{
6335 struct meter *meter;
6336
6337 meter = xzalloc(sizeof *meter);
6338 meter->provider_meter_id = provider_meter_id;
6339 meter->created = time_msec();
ba8cff36 6340 meter->id = config->meter_id;
417e7e66 6341 ovs_list_init(&meter->rules);
9cae45dc
JR
6342
6343 meter_update(meter, config);
6344
6345 return meter;
6346}
6347
6b3f7f02 6348static void
ba8cff36 6349meter_destroy(struct ofproto *ofproto, struct meter *meter)
15aaf599 6350 OVS_REQUIRES(ofproto_mutex)
6b3f7f02 6351{
9e638f22
AZ
6352 uint32_t *upcall_meter_ptr;
6353 upcall_meter_ptr = ofproto_upcall_meter_ptr(ofproto, meter->id);
6354 if (upcall_meter_ptr) {
6355 *upcall_meter_ptr = UINT32_MAX;
6356 }
6357
ba8cff36
AZ
6358 if (!ovs_list_is_empty(&meter->rules)) {
6359 struct rule_collection rules;
6360 struct rule *rule;
3059d7e5 6361
ba8cff36
AZ
6362 rule_collection_init(&rules);
6363 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
6364 rule_collection_add(&rules, rule);
6365 }
6366 delete_flows__(&rules, OFPRR_METER_DELETE, NULL);
6367 }
3059d7e5 6368
ba8cff36
AZ
6369 ofproto->ofproto_class->meter_del(ofproto, meter->provider_meter_id);
6370 free(meter->bands);
6371 free(meter);
6372}
3059d7e5 6373
ba8cff36
AZ
6374static void
6375meter_delete(struct ofproto *ofproto, uint32_t meter_id)
6376 OVS_REQUIRES(ofproto_mutex)
6377{
6378 struct meter *meter = ofproto_get_meter(ofproto, meter_id);
6379
6380 if (meter) {
6381 hmap_remove(&ofproto->meters, &meter->node);
6382 meter_destroy(ofproto, meter);
6383 }
6384}
6385
6386static void
6387meter_delete_all(struct ofproto *ofproto)
6388 OVS_REQUIRES(ofproto_mutex)
6389{
6390 struct meter *meter, *next;
6391
6392 HMAP_FOR_EACH_SAFE (meter, next, node, &ofproto->meters) {
6393 hmap_remove(&ofproto->meters, &meter->node);
6394 meter_destroy(ofproto, meter);
6b3f7f02
JR
6395 }
6396}
6397
9cae45dc
JR
6398static enum ofperr
6399handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
6400{
6401 ofproto_meter_id provider_meter_id = { UINT32_MAX };
ba8cff36 6402 struct meter *meter = ofproto_get_meter(ofproto, mm->meter.meter_id);
9cae45dc
JR
6403 enum ofperr error;
6404
ba8cff36 6405 if (meter) {
9cae45dc
JR
6406 return OFPERR_OFPMMFC_METER_EXISTS;
6407 }
6408
6409 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
6410 &mm->meter);
6411 if (!error) {
6412 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
ba8cff36
AZ
6413 meter = meter_create(&mm->meter, provider_meter_id);
6414 ofproto_add_meter(ofproto, meter);
9cae45dc 6415 }
f0f8c6c2 6416 return error;
9cae45dc
JR
6417}
6418
6419static enum ofperr
6420handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
6421{
ba8cff36 6422 struct meter *meter = ofproto_get_meter(ofproto, mm->meter.meter_id);
9cae45dc 6423 enum ofperr error;
e555eb7c 6424 uint32_t provider_meter_id;
9cae45dc
JR
6425
6426 if (!meter) {
6427 return OFPERR_OFPMMFC_UNKNOWN_METER;
6428 }
6429
e555eb7c 6430 provider_meter_id = meter->provider_meter_id.uint32;
9cae45dc
JR
6431 error = ofproto->ofproto_class->meter_set(ofproto,
6432 &meter->provider_meter_id,
6433 &mm->meter);
e555eb7c 6434 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
9cae45dc
JR
6435 if (!error) {
6436 meter_update(meter, &mm->meter);
6437 }
6438 return error;
6439}
6440
6441static enum ofperr
baae3d02 6442handle_delete_meter(struct ofconn *ofconn, struct ofputil_meter_mod *mm)
15aaf599 6443 OVS_EXCLUDED(ofproto_mutex)
9cae45dc
JR
6444{
6445 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6446 uint32_t meter_id = mm->meter.meter_id;
9cae45dc 6447
ba8cff36
AZ
6448 /* OpenFlow does not support Meter ID 0. */
6449 if (meter_id) {
6450 ovs_mutex_lock(&ofproto_mutex);
6451
6452 if (meter_id == OFPM13_ALL) {
6453 meter_delete_all(ofproto);
6454 } else {
6455 meter_delete(ofproto, meter_id);
9cae45dc 6456 }
9cae45dc 6457
ba8cff36
AZ
6458 ovs_mutex_unlock(&ofproto_mutex);
6459 }
a8e547c1 6460
ba8cff36 6461 return 0;
9cae45dc
JR
6462}
6463
6464static enum ofperr
6465handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
6466{
6467 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6468 struct ofputil_meter_mod mm;
6469 uint64_t bands_stub[256 / 8];
6470 struct ofpbuf bands;
6471 uint32_t meter_id;
6472 enum ofperr error;
6473
6474 error = reject_slave_controller(ofconn);
6475 if (error) {
6476 return error;
6477 }
6478
6479 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
6480
6481 error = ofputil_decode_meter_mod(oh, &mm, &bands);
6482 if (error) {
6483 goto exit_free_bands;
6484 }
6485
6486 meter_id = mm.meter.meter_id;
6487
6488 if (mm.command != OFPMC13_DELETE) {
6489 /* Fails also when meters are not implemented by the provider. */
9e638f22 6490 if (ofproto->meter_features.max_meters == 0) {
9cae45dc
JR
6491 error = OFPERR_OFPMMFC_INVALID_METER;
6492 goto exit_free_bands;
9e638f22
AZ
6493 }
6494
6495 if (meter_id == 0) {
6496 error = OFPERR_OFPMMFC_INVALID_METER;
b31e8700 6497 goto exit_free_bands;
9e638f22
AZ
6498 } else if (meter_id > OFPM13_MAX) {
6499 switch(meter_id) {
6500 case OFPM13_SLOWPATH:
6501 case OFPM13_CONTROLLER:
6502 break;
6503 case OFPM13_ALL:
6504 default:
6505 error = OFPERR_OFPMMFC_INVALID_METER;
6506 goto exit_free_bands;
6507 }
9cae45dc
JR
6508 }
6509 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
6510 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
6511 goto exit_free_bands;
6512 }
6513 }
6514
6515 switch (mm.command) {
6516 case OFPMC13_ADD:
6517 error = handle_add_meter(ofproto, &mm);
6518 break;
6519
6520 case OFPMC13_MODIFY:
6521 error = handle_modify_meter(ofproto, &mm);
6522 break;
6523
6524 case OFPMC13_DELETE:
baae3d02 6525 error = handle_delete_meter(ofconn, &mm);
9cae45dc
JR
6526 break;
6527
6528 default:
6529 error = OFPERR_OFPMMFC_BAD_COMMAND;
6530 break;
6531 }
6532
3c35db62
NR
6533 if (!error) {
6534 struct ofputil_requestforward rf;
6535 rf.xid = oh->xid;
6536 rf.reason = OFPRFR_METER_MOD;
6537 rf.meter_mod = &mm;
6538 connmgr_send_requestforward(ofproto->connmgr, ofconn, &rf);
6539 }
6540
9cae45dc
JR
6541exit_free_bands:
6542 ofpbuf_uninit(&bands);
6543 return error;
6544}
6545
6546static enum ofperr
6547handle_meter_features_request(struct ofconn *ofconn,
6548 const struct ofp_header *request)
6549{
6550 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6551 struct ofputil_meter_features features;
6552 struct ofpbuf *b;
6553
6554 if (ofproto->ofproto_class->meter_get_features) {
6555 ofproto->ofproto_class->meter_get_features(ofproto, &features);
6556 } else {
6557 memset(&features, 0, sizeof features);
6558 }
6559 b = ofputil_encode_meter_features_reply(&features, request);
6560
6561 ofconn_send_reply(ofconn, b);
6562 return 0;
6563}
6564
ba8cff36
AZ
6565static void
6566meter_request_reply(struct ofproto *ofproto, struct meter *meter,
6567 enum ofptype type, struct ovs_list *replies)
6568{
6569 uint64_t bands_stub[256 / 8];
6570 struct ofpbuf bands;
6571
6572 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
6573
6574 if (type == OFPTYPE_METER_STATS_REQUEST) {
6575 struct ofputil_meter_stats stats;
6576
6577 stats.meter_id = meter->id;
6578
6579 /* Provider sets the packet and byte counts, we do the rest. */
6580 stats.flow_count = ovs_list_size(&meter->rules);
6581 calc_duration(meter->created, time_msec(),
6582 &stats.duration_sec, &stats.duration_nsec);
6583 stats.n_bands = meter->n_bands;
6584 ofpbuf_clear(&bands);
6585 stats.bands = ofpbuf_put_uninit(&bands, meter->n_bands
6586 * sizeof *stats.bands);
6587
6588 if (!ofproto->ofproto_class->meter_get(ofproto,
6589 meter->provider_meter_id,
6590 &stats, meter->n_bands)) {
6591 ofputil_append_meter_stats(replies, &stats);
6592 }
6593 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
6594 struct ofputil_meter_config config;
6595
6596 config.meter_id = meter->id;
6597 config.flags = meter->flags;
6598 config.n_bands = meter->n_bands;
6599 config.bands = meter->bands;
6600 ofputil_append_meter_config(replies, &config);
6601 }
6602
6603 ofpbuf_uninit(&bands);
6604}
6605
6606static void
6607meter_request_reply_all(struct ofproto *ofproto, enum ofptype type,
6608 struct ovs_list *replies)
6609{
6610 struct meter *meter;
6611
6612 HMAP_FOR_EACH (meter, node, &ofproto->meters) {
6613 meter_request_reply(ofproto, meter, type, replies);
6614 }
6615}
6616
9cae45dc
JR
6617static enum ofperr
6618handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
6619 enum ofptype type)
6620{
6621 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
ca6ba700 6622 struct ovs_list replies;
ba8cff36
AZ
6623 uint32_t meter_id;
6624 struct meter *meter;
9cae45dc
JR
6625
6626 ofputil_decode_meter_request(request, &meter_id);
6627
ba8cff36
AZ
6628 if (meter_id != OFPM13_ALL) {
6629 meter = ofproto_get_meter(ofproto, meter_id);
6630 if (!meter) {
6631 /* Meter does not exist. */
9cae45dc
JR
6632 return OFPERR_OFPMMFC_UNKNOWN_METER;
6633 }
ba8cff36
AZ
6634 } else {
6635 /* GCC 4.9.2 complains about 'meter' can potentially be used
6636 * uninitialized. Logically, this is a false alarm since
6637 * meter is only used when meter_id != OFPM13_ALL.
6638 * Set NULL to make compiler happy. */
6639 meter = NULL;
9cae45dc
JR
6640 }
6641
9cae45dc 6642 ofpmp_init(&replies, request);
ba8cff36
AZ
6643 if (meter_id == OFPM13_ALL) {
6644 meter_request_reply_all(ofproto, type, &replies);
6645 } else {
6646 meter_request_reply(ofproto, meter, type, &replies);
9cae45dc 6647 }
9cae45dc 6648 ofconn_send_replies(ofconn, &replies);
9cae45dc
JR
6649 return 0;
6650}
6651
db88b35c
JR
6652/* Returned group is RCU protected. */
6653static struct ofgroup *
5d08a275
JR
6654ofproto_group_lookup__(const struct ofproto *ofproto, uint32_t group_id,
6655 ovs_version_t version)
db88b35c
JR
6656{
6657 struct ofgroup *group;
6658
6659 CMAP_FOR_EACH_WITH_HASH (group, cmap_node, hash_int(group_id, 0),
6660 &ofproto->groups) {
5d08a275
JR
6661 if (group->group_id == group_id
6662 && versions_visible_in_version(&group->versions, version)) {
db88b35c 6663 return group;
7395c052
NZ
6664 }
6665 }
3fc3b679 6666
db88b35c 6667 return NULL;
7395c052
NZ
6668}
6669
3fc3b679
AZ
6670/* If the group exists, this function increments the groups's reference count.
6671 *
6672 * Make sure to call ofproto_group_unref() after no longer needing to maintain
6673 * a reference to the group. */
db88b35c 6674struct ofgroup *
76973237 6675ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5d08a275 6676 ovs_version_t version, bool take_ref)
7395c052 6677{
db88b35c 6678 struct ofgroup *group;
7395c052 6679
5d08a275 6680 group = ofproto_group_lookup__(ofproto, group_id, version);
76973237 6681 if (group && take_ref) {
db88b35c
JR
6682 /* Not holding a lock, so it is possible that another thread releases
6683 * the last reference just before we manage to get one. */
6684 return ofproto_group_try_ref(group) ? group : NULL;
7395c052 6685 }
76973237 6686 return group;
7395c052
NZ
6687}
6688
cc099268 6689/* Caller should hold 'ofproto_mutex' if it is important that the
db88b35c 6690 * group is not removed by someone else. */
7e9f8266
BP
6691static bool
6692ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
7e9f8266 6693{
5d08a275 6694 return ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX) != NULL;
7e9f8266
BP
6695}
6696
cc099268
JR
6697static void
6698group_add_rule(struct ofgroup *group, struct rule *rule)
732feb93 6699{
cc099268
JR
6700 rule_collection_add(&group->rules, rule);
6701}
732feb93 6702
cc099268
JR
6703static void
6704group_remove_rule(struct ofgroup *group, struct rule *rule)
6705{
6706 rule_collection_remove(&group->rules, rule);
732feb93
SH
6707}
6708
7395c052 6709static void
ca6ba700 6710append_group_stats(struct ofgroup *group, struct ovs_list *replies)
cc099268 6711 OVS_REQUIRES(ofproto_mutex)
7395c052
NZ
6712{
6713 struct ofputil_group_stats ogs;
eaf004bd 6714 const struct ofproto *ofproto = group->ofproto;
7395c052
NZ
6715 long long int now = time_msec();
6716 int error;
6717
646b2a9c
SH
6718 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
6719
732feb93 6720 /* Provider sets the packet and byte counts, we do the rest. */
fe59694b 6721 ogs.ref_count = rule_collection_n(&group->rules);
732feb93
SH
6722 ogs.n_buckets = group->n_buckets;
6723
7395c052
NZ
6724 error = (ofproto->ofproto_class->group_get_stats
6725 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
6726 : EOPNOTSUPP);
6727 if (error) {
7395c052
NZ
6728 ogs.packet_count = UINT64_MAX;
6729 ogs.byte_count = UINT64_MAX;
7395c052
NZ
6730 memset(ogs.bucket_stats, 0xff,
6731 ogs.n_buckets * sizeof *ogs.bucket_stats);
6732 }
6733
6734 ogs.group_id = group->group_id;
6735 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
6736
6737 ofputil_append_group_stats(replies, &ogs);
646b2a9c
SH
6738
6739 free(ogs.bucket_stats);
7395c052
NZ
6740}
6741
19187a71
BP
6742static void
6743handle_group_request(struct ofconn *ofconn,
6744 const struct ofp_header *request, uint32_t group_id,
ca6ba700 6745 void (*cb)(struct ofgroup *, struct ovs_list *replies))
cc099268 6746 OVS_EXCLUDED(ofproto_mutex)
7395c052
NZ
6747{
6748 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7395c052 6749 struct ofgroup *group;
ca6ba700 6750 struct ovs_list replies;
7395c052
NZ
6751
6752 ofpmp_init(&replies, request);
cc099268
JR
6753 /* Must exclude modifications to guarantee iterating groups. */
6754 ovs_mutex_lock(&ofproto_mutex);
7395c052 6755 if (group_id == OFPG_ALL) {
db88b35c 6756 CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) {
3a46eaaf
BP
6757 if (versions_visible_in_version(&group->versions,
6758 OVS_VERSION_MAX)) {
6759 cb(group, &replies);
6760 }
7395c052 6761 }
7395c052 6762 } else {
5d08a275 6763 group = ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX);
db88b35c 6764 if (group) {
19187a71 6765 cb(group, &replies);
7395c052
NZ
6766 }
6767 }
cc099268 6768 ovs_mutex_unlock(&ofproto_mutex);
7395c052 6769 ofconn_send_replies(ofconn, &replies);
7395c052
NZ
6770}
6771
6772static enum ofperr
19187a71
BP
6773handle_group_stats_request(struct ofconn *ofconn,
6774 const struct ofp_header *request)
7395c052 6775{
19187a71
BP
6776 uint32_t group_id;
6777 enum ofperr error;
7395c052 6778
19187a71
BP
6779 error = ofputil_decode_group_stats_request(request, &group_id);
6780 if (error) {
6781 return error;
7395c052 6782 }
7395c052 6783
19187a71
BP
6784 handle_group_request(ofconn, request, group_id, append_group_stats);
6785 return 0;
6786}
6787
6788static void
ca6ba700 6789append_group_desc(struct ofgroup *group, struct ovs_list *replies)
19187a71
BP
6790{
6791 struct ofputil_group_desc gds;
7395c052 6792
19187a71
BP
6793 gds.group_id = group->group_id;
6794 gds.type = group->type;
53eb84a5
SH
6795 gds.props = group->props;
6796
19187a71
BP
6797 ofputil_append_group_desc_reply(&gds, &group->buckets, replies);
6798}
6799
6800static enum ofperr
6801handle_group_desc_stats_request(struct ofconn *ofconn,
6802 const struct ofp_header *request)
6803{
6804 handle_group_request(ofconn, request,
6805 ofputil_decode_group_desc_request(request),
6806 append_group_desc);
7395c052
NZ
6807 return 0;
6808}
6809
6810static enum ofperr
6811handle_group_features_stats_request(struct ofconn *ofconn,
6812 const struct ofp_header *request)
6813{
6814 struct ofproto *p = ofconn_get_ofproto(ofconn);
6815 struct ofpbuf *msg;
6816
6817 msg = ofputil_encode_group_features_reply(&p->ogf, request);
6818 if (msg) {
6819 ofconn_send_reply(ofconn, msg);
6820 }
6821
6822 return 0;
6823}
6824
56085be5 6825static void
e016fb63
BP
6826put_queue_get_config_reply(struct ofport *port, uint32_t queue,
6827 struct ovs_list *replies)
e8f9a7bb 6828{
e016fb63 6829 struct ofputil_queue_config qc;
e8f9a7bb 6830
e016fb63
BP
6831 /* None of the existing queues have compatible properties, so we hard-code
6832 * omitting min_rate and max_rate. */
6833 qc.port = port->ofp_port;
6834 qc.queue = queue;
6835 qc.min_rate = UINT16_MAX;
6836 qc.max_rate = UINT16_MAX;
6837 ofputil_append_queue_get_config_reply(&qc, replies);
6838}
e8f9a7bb 6839
e016fb63
BP
6840static int
6841handle_queue_get_config_request_for_port(struct ofport *port, uint32_t queue,
6842 struct ovs_list *replies)
6843{
6844 struct smap details = SMAP_INITIALIZER(&details);
6845 if (queue != OFPQ_ALL) {
6846 int error = netdev_get_queue(port->netdev, queue, &details);
6847 switch (error) {
6848 case 0:
6849 put_queue_get_config_reply(port, queue, replies);
6850 break;
6851 case EOPNOTSUPP:
6852 case EINVAL:
6853 return OFPERR_OFPQOFC_BAD_QUEUE;
6854 default:
6855 return OFPERR_NXQOFC_QUEUE_ERROR;
6856 }
6857 } else {
6858 struct netdev_queue_dump queue_dump;
6859 uint32_t queue_id;
6860
6861 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump,
6862 port->netdev) {
6863 put_queue_get_config_reply(port, queue_id, replies);
6864 }
6865 }
6866 smap_destroy(&details);
6867 return 0;
56085be5
BP
6868}
6869
6870static enum ofperr
6871handle_queue_get_config_request(struct ofconn *ofconn,
6872 const struct ofp_header *oh)
6873{
e016fb63
BP
6874 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6875 struct ovs_list replies;
6876 struct ofport *port;
6877 ofp_port_t req_port;
6878 uint32_t req_queue;
6879 enum ofperr error;
6880
6881 error = ofputil_decode_queue_get_config_request(oh, &req_port, &req_queue);
6882 if (error) {
6883 return error;
6884 }
6885
6886 ofputil_start_queue_get_config_reply(oh, &replies);
6887 if (req_port == OFPP_ANY) {
6888 error = OFPERR_OFPQOFC_BAD_QUEUE;
6889 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
6890 if (!handle_queue_get_config_request_for_port(port, req_queue,
6891 &replies)) {
6892 error = 0;
6893 }
6894 }
6895 } else {
6896 port = ofproto_get_port(ofproto, req_port);
6897 error = (port
6898 ? handle_queue_get_config_request_for_port(port, req_queue,
6899 &replies)
6900 : OFPERR_OFPQOFC_BAD_PORT);
6901 }
6902 if (!error) {
6903 ofconn_send_replies(ofconn, &replies);
6904 } else {
6905 ofpbuf_list_delete(&replies);
6906 }
6907
6908 return error;
e8f9a7bb
VG
6909}
6910
836476ce
BP
6911/* Allocates, initializes, and constructs a new group in 'ofproto', obtaining
6912 * all the attributes for it from 'gm', and stores a pointer to it in
6913 * '*ofgroup'. Makes the new group visible from the flow table starting from
6914 * 'version'.
6915 *
6916 * Returns 0 if successful, otherwise an error code. If there is an error then
6917 * '*ofgroup' is indeterminate upon return. */
809c7548 6918static enum ofperr
63eded98 6919init_group(struct ofproto *ofproto, const struct ofputil_group_mod *gm,
5d08a275 6920 ovs_version_t version, struct ofgroup **ofgroup)
809c7548
RW
6921{
6922 enum ofperr error;
eaf004bd 6923 const long long int now = time_msec();
809c7548
RW
6924
6925 if (gm->group_id > OFPG_MAX) {
6926 return OFPERR_OFPGMFC_INVALID_GROUP;
6927 }
6928 if (gm->type > OFPGT11_FF) {
6929 return OFPERR_OFPGMFC_BAD_TYPE;
6930 }
6931
6932 *ofgroup = ofproto->ofproto_class->group_alloc();
6933 if (!*ofgroup) {
6934 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
6935 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
6936 }
6937
5d08a275 6938 *CONST_CAST(struct ofproto **, &(*ofgroup)->ofproto) = ofproto;
eaf004bd
AZ
6939 *CONST_CAST(uint32_t *, &((*ofgroup)->group_id)) = gm->group_id;
6940 *CONST_CAST(enum ofp11_group_type *, &(*ofgroup)->type) = gm->type;
6941 *CONST_CAST(long long int *, &((*ofgroup)->created)) = now;
6942 *CONST_CAST(long long int *, &((*ofgroup)->modified)) = now;
809c7548 6943 ovs_refcount_init(&(*ofgroup)->ref_count);
cc099268 6944 (*ofgroup)->being_deleted = false;
809c7548 6945
db88b35c
JR
6946 ovs_list_init(CONST_CAST(struct ovs_list *, &(*ofgroup)->buckets));
6947 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
6948 &(*ofgroup)->buckets),
6949 &gm->buckets, NULL);
63eded98 6950
eaf004bd 6951 *CONST_CAST(uint32_t *, &(*ofgroup)->n_buckets) =
417e7e66 6952 ovs_list_size(&(*ofgroup)->buckets);
809c7548 6953
e8dba719
JR
6954 ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props *,
6955 &(*ofgroup)->props),
6956 &gm->props);
cc099268 6957 rule_collection_init(&(*ofgroup)->rules);
bc65c25a 6958
5d08a275
JR
6959 /* Make group visible from 'version'. */
6960 (*ofgroup)->versions = VERSIONS_INITIALIZER(version,
6961 OVS_VERSION_NOT_REMOVED);
6962
809c7548
RW
6963 /* Construct called BEFORE any locks are held. */
6964 error = ofproto->ofproto_class->group_construct(*ofgroup);
6965 if (error) {
e8dba719
JR
6966 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *,
6967 &(*ofgroup)->props));
db88b35c
JR
6968 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *,
6969 &(*ofgroup)->buckets));
809c7548
RW
6970 ofproto->ofproto_class->group_dealloc(*ofgroup);
6971 }
6972 return error;
6973}
6974
6c5b90ce
BP
6975/* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
6976 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
6977 * failure. */
7395c052 6978static enum ofperr
2b0b0b80 6979add_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 6980 OVS_REQUIRES(ofproto_mutex)
7395c052 6981{
7395c052
NZ
6982 enum ofperr error;
6983
2b0b0b80
JR
6984 if (ofproto_group_exists(ofproto, ogm->gm.group_id)) {
6985 return OFPERR_OFPGMFC_GROUP_EXISTS;
7395c052
NZ
6986 }
6987
2b0b0b80
JR
6988 if (ofproto->n_groups[ogm->gm.type]
6989 >= ofproto->ogf.max_groups[ogm->gm.type]) {
6990 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
7395c052
NZ
6991 }
6992
2b0b0b80 6993 /* Allocate new group and initialize it. */
5d08a275 6994 error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group);
2b0b0b80
JR
6995 if (!error) {
6996 /* Insert new group. */
6997 cmap_insert(&ofproto->groups, &ogm->new_group->cmap_node,
6998 hash_int(ogm->new_group->group_id, 0));
6999 ofproto->n_groups[ogm->new_group->type]++;
7395c052 7000 }
7395c052
NZ
7001 return error;
7002}
7003
fce4730c
SH
7004/* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets
7005 * already in 'new_ofgroup' will be placed just after the (copy of the) bucket
7006 * in 'ofgroup' with bucket ID 'command_bucket_id'. Special
7007 * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are
7008 * also honored. */
7009static enum ofperr
7010copy_buckets_for_insert_bucket(const struct ofgroup *ofgroup,
7011 struct ofgroup *new_ofgroup,
7012 uint32_t command_bucket_id)
7013{
7014 struct ofputil_bucket *last = NULL;
7015
7016 if (command_bucket_id <= OFPG15_BUCKET_MAX) {
7017 /* Check here to ensure that a bucket corresponding to
7018 * command_bucket_id exists in the old bucket list.
7019 *
7020 * The subsequent search of below of new_ofgroup covers
7021 * both buckets in the old bucket list and buckets added
7022 * by the insert buckets group mod message this function processes. */
7023 if (!ofputil_bucket_find(&ofgroup->buckets, command_bucket_id)) {
7024 return OFPERR_OFPGMFC_UNKNOWN_BUCKET;
7025 }
7026
417e7e66 7027 if (!ovs_list_is_empty(&new_ofgroup->buckets)) {
fce4730c
SH
7028 last = ofputil_bucket_list_back(&new_ofgroup->buckets);
7029 }
7030 }
7031
db88b35c
JR
7032 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
7033 &new_ofgroup->buckets),
7034 &ofgroup->buckets, NULL);
fce4730c 7035
23a31238 7036 if (ofputil_bucket_check_duplicate_id(&new_ofgroup->buckets)) {
d2873d53 7037 VLOG_INFO_RL(&rl, "Duplicate bucket id");
fce4730c
SH
7038 return OFPERR_OFPGMFC_BUCKET_EXISTS;
7039 }
7040
7041 /* Rearrange list according to command_bucket_id */
7042 if (command_bucket_id == OFPG15_BUCKET_LAST) {
417e7e66 7043 if (!ovs_list_is_empty(&ofgroup->buckets)) {
8e19e655
BP
7044 struct ofputil_bucket *new_first;
7045 const struct ofputil_bucket *first;
fce4730c 7046
8e19e655
BP
7047 first = ofputil_bucket_list_front(&ofgroup->buckets);
7048 new_first = ofputil_bucket_find(&new_ofgroup->buckets,
7049 first->bucket_id);
fce4730c 7050
417e7e66 7051 ovs_list_splice(new_ofgroup->buckets.next, &new_first->list_node,
db88b35c
JR
7052 CONST_CAST(struct ovs_list *,
7053 &new_ofgroup->buckets));
8e19e655 7054 }
fce4730c
SH
7055 } else if (command_bucket_id <= OFPG15_BUCKET_MAX && last) {
7056 struct ofputil_bucket *after;
7057
7058 /* Presence of bucket is checked above so after should never be NULL */
7059 after = ofputil_bucket_find(&new_ofgroup->buckets, command_bucket_id);
7060
417e7e66 7061 ovs_list_splice(after->list_node.next, new_ofgroup->buckets.next,
fce4730c
SH
7062 last->list_node.next);
7063 }
7064
7065 return 0;
7066}
7067
7068/* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup'
7069 * with the exception of the bucket whose bucket id is 'command_bucket_id'.
7070 * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST
7071 * and OFPG15_BUCKET_ALL are also honored. */
7072static enum ofperr
7073copy_buckets_for_remove_bucket(const struct ofgroup *ofgroup,
7074 struct ofgroup *new_ofgroup,
7075 uint32_t command_bucket_id)
7076{
7077 const struct ofputil_bucket *skip = NULL;
7078
7079 if (command_bucket_id == OFPG15_BUCKET_ALL) {
7080 return 0;
7081 }
7082
7083 if (command_bucket_id == OFPG15_BUCKET_FIRST) {
417e7e66 7084 if (!ovs_list_is_empty(&ofgroup->buckets)) {
fce4730c
SH
7085 skip = ofputil_bucket_list_front(&ofgroup->buckets);
7086 }
7087 } else if (command_bucket_id == OFPG15_BUCKET_LAST) {
417e7e66 7088 if (!ovs_list_is_empty(&ofgroup->buckets)) {
fce4730c
SH
7089 skip = ofputil_bucket_list_back(&ofgroup->buckets);
7090 }
7091 } else {
7092 skip = ofputil_bucket_find(&ofgroup->buckets, command_bucket_id);
7093 if (!skip) {
7094 return OFPERR_OFPGMFC_UNKNOWN_BUCKET;
7095 }
7096 }
7097
db88b35c
JR
7098 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
7099 &new_ofgroup->buckets),
7100 &ofgroup->buckets, skip);
fce4730c
SH
7101
7102 return 0;
7103}
7104
7105/* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and
7106 * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code
6c5b90ce 7107 * on failure.
7395c052 7108 *
809c7548
RW
7109 * Note that the group is re-created and then replaces the old group in
7110 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
6c5b90ce 7111 * the xlate module hold a pointer to the group. */
7395c052 7112static enum ofperr
2b0b0b80 7113modify_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 7114 OVS_REQUIRES(ofproto_mutex)
7395c052 7115{
2b0b0b80
JR
7116 struct ofgroup *old_group; /* Modified group. */
7117 struct ofgroup *new_group;
7395c052
NZ
7118 enum ofperr error;
7119
5d08a275
JR
7120 old_group = ofproto_group_lookup__(ofproto, ogm->gm.group_id,
7121 OVS_VERSION_MAX);
2b0b0b80
JR
7122 if (!old_group) {
7123 return OFPERR_OFPGMFC_UNKNOWN_GROUP;
7395c052
NZ
7124 }
7125
836476ce
BP
7126 /* Inserting or deleting a bucket should not change the group's type or
7127 * properties, so change the group mod so that these aspects match the old
7128 * group. (See EXT-570.) */
7129 if (ogm->gm.command == OFPGC15_INSERT_BUCKET ||
7130 ogm->gm.command == OFPGC15_REMOVE_BUCKET) {
7131 ogm->gm.type = old_group->type;
7132 ofputil_group_properties_destroy(&ogm->gm.props);
7133 ofputil_group_properties_copy(&ogm->gm.props, &old_group->props);
7134 }
7135
2b0b0b80
JR
7136 if (old_group->type != ogm->gm.type
7137 && (ofproto->n_groups[ogm->gm.type]
7138 >= ofproto->ogf.max_groups[ogm->gm.type])) {
7139 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
7395c052 7140 }
809c7548 7141
5d08a275 7142 error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group);
2b0b0b80
JR
7143 if (error) {
7144 return error;
7395c052 7145 }
2b0b0b80 7146 new_group = ogm->new_group;
7395c052 7147
fce4730c 7148 /* Manipulate bucket list for bucket commands */
2b0b0b80
JR
7149 if (ogm->gm.command == OFPGC15_INSERT_BUCKET) {
7150 error = copy_buckets_for_insert_bucket(old_group, new_group,
7151 ogm->gm.command_bucket_id);
7152 } else if (ogm->gm.command == OFPGC15_REMOVE_BUCKET) {
7153 error = copy_buckets_for_remove_bucket(old_group, new_group,
7154 ogm->gm.command_bucket_id);
fce4730c
SH
7155 }
7156 if (error) {
7157 goto out;
7158 }
7159
809c7548 7160 /* The group creation time does not change during modification. */
2b0b0b80
JR
7161 *CONST_CAST(long long int *, &(new_group->created)) = old_group->created;
7162 *CONST_CAST(long long int *, &(new_group->modified)) = time_msec();
7395c052 7163
2b0b0b80 7164 group_collection_add(&ogm->old_groups, old_group);
db88b35c 7165
5d08a275
JR
7166 /* Mark the old group for deletion. */
7167 versions_set_remove_version(&old_group->versions, ogm->version);
7168 /* Insert replacement group. */
7169 cmap_insert(&ofproto->groups, &new_group->cmap_node,
7170 hash_int(new_group->group_id, 0));
cc099268 7171 /* Transfer rules. */
2b0b0b80 7172 rule_collection_move(&new_group->rules, &old_group->rules);
db88b35c 7173
2b0b0b80
JR
7174 if (old_group->type != new_group->type) {
7175 ofproto->n_groups[old_group->type]--;
7176 ofproto->n_groups[new_group->type]++;
7395c052 7177 }
2b0b0b80 7178 return 0;
7395c052 7179
809c7548 7180out:
2b0b0b80 7181 ofproto_group_unref(new_group);
7395c052
NZ
7182 return error;
7183}
7184
88b87a36
JS
7185/* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not
7186 * exist yet and modifies it otherwise */
7187static enum ofperr
2b0b0b80
JR
7188add_or_modify_group_start(struct ofproto *ofproto,
7189 struct ofproto_group_mod *ogm)
cc099268 7190 OVS_REQUIRES(ofproto_mutex)
88b87a36 7191{
88b87a36 7192 enum ofperr error;
88b87a36 7193
2b0b0b80
JR
7194 if (!ofproto_group_exists(ofproto, ogm->gm.group_id)) {
7195 error = add_group_start(ofproto, ogm);
88b87a36 7196 } else {
2b0b0b80 7197 error = modify_group_start(ofproto, ogm);
88b87a36 7198 }
cc099268 7199
88b87a36
JS
7200 return error;
7201}
7202
7395c052 7203static void
5d08a275
JR
7204delete_group_start(struct ofproto *ofproto, ovs_version_t version,
7205 struct group_collection *groups, struct ofgroup *group)
cc099268 7206 OVS_REQUIRES(ofproto_mutex)
7395c052 7207{
2b0b0b80 7208 /* Makes flow deletion code leave the rule pointers in 'group->rules'
cc099268
JR
7209 * intact, so that we can later refer to the rules deleted due to the group
7210 * deletion. Rule pointers will be removed from all other groups, if any,
7211 * so we will never try to delete the same rule twice. */
2b0b0b80
JR
7212 group->being_deleted = true;
7213
7214 /* Mark all the referring groups for deletion. */
5d08a275 7215 delete_flows_start__(ofproto, version, &group->rules);
2b0b0b80 7216 group_collection_add(groups, group);
5d08a275 7217 versions_set_remove_version(&group->versions, version);
2b0b0b80
JR
7218 ofproto->n_groups[group->type]--;
7219}
276f6518 7220
2b0b0b80
JR
7221static void
7222delete_group_finish(struct ofproto *ofproto, struct ofgroup *group)
7223 OVS_REQUIRES(ofproto_mutex)
7224{
7225 /* Finish deletion of all flow entries containing this group in a group
7226 * action. */
7227 delete_flows_finish__(ofproto, &group->rules, OFPRR_GROUP_DELETE, NULL);
276f6518 7228
5d08a275 7229 /* Group removal is postponed by the caller. */
7395c052
NZ
7230}
7231
6c5b90ce 7232/* Implements OFPGC11_DELETE. */
7395c052 7233static void
2b0b0b80 7234delete_groups_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 7235 OVS_REQUIRES(ofproto_mutex)
7395c052 7236{
2b0b0b80 7237 struct ofgroup *group;
7395c052 7238
2b0b0b80
JR
7239 if (ogm->gm.group_id == OFPG_ALL) {
7240 CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) {
5d08a275
JR
7241 if (versions_visible_in_version(&group->versions, ogm->version)) {
7242 delete_group_start(ofproto, ogm->version, &ogm->old_groups,
7243 group);
7244 }
7395c052
NZ
7245 }
7246 } else {
5d08a275
JR
7247 group = ofproto_group_lookup__(ofproto, ogm->gm.group_id, ogm->version);
7248 if (group) {
7249 delete_group_start(ofproto, ogm->version, &ogm->old_groups, group);
7395c052
NZ
7250 }
7251 }
7395c052
NZ
7252}
7253
7254static enum ofperr
2b0b0b80
JR
7255ofproto_group_mod_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
7256 OVS_REQUIRES(ofproto_mutex)
7395c052 7257{
7395c052
NZ
7258 enum ofperr error;
7259
2b0b0b80
JR
7260 ogm->new_group = NULL;
7261 group_collection_init(&ogm->old_groups);
7395c052 7262
2b0b0b80 7263 switch (ogm->gm.command) {
7395c052 7264 case OFPGC11_ADD:
2b0b0b80 7265 error = add_group_start(ofproto, ogm);
3c35db62 7266 break;
7395c052
NZ
7267
7268 case OFPGC11_MODIFY:
2b0b0b80 7269 error = modify_group_start(ofproto, ogm);
3c35db62 7270 break;
7395c052 7271
88b87a36 7272 case OFPGC11_ADD_OR_MOD:
2b0b0b80 7273 error = add_or_modify_group_start(ofproto, ogm);
88b87a36
JS
7274 break;
7275
7395c052 7276 case OFPGC11_DELETE:
2b0b0b80 7277 delete_groups_start(ofproto, ogm);
3c35db62
NR
7278 error = 0;
7279 break;
7395c052 7280
fce4730c 7281 case OFPGC15_INSERT_BUCKET:
2b0b0b80 7282 error = modify_group_start(ofproto, ogm);
3c35db62 7283 break;
fce4730c
SH
7284
7285 case OFPGC15_REMOVE_BUCKET:
2b0b0b80 7286 error = modify_group_start(ofproto, ogm);
3c35db62 7287 break;
fce4730c 7288
7395c052 7289 default:
2b0b0b80 7290 if (ogm->gm.command > OFPGC11_DELETE) {
d2873d53 7291 VLOG_INFO_RL(&rl, "%s: Invalid group_mod command type %d",
2b0b0b80 7292 ofproto->name, ogm->gm.command);
7395c052 7293 }
63eded98 7294 error = OFPERR_OFPGMFC_BAD_COMMAND;
2b0b0b80 7295 break;
7395c052 7296 }
2b0b0b80
JR
7297 return error;
7298}
3c35db62 7299
25070e04
JR
7300static void
7301ofproto_group_mod_revert(struct ofproto *ofproto,
7302 struct ofproto_group_mod *ogm)
7303 OVS_REQUIRES(ofproto_mutex)
7304{
7305 struct ofgroup *new_group = ogm->new_group;
7306 struct ofgroup *old_group;
7307
7308 /* Restore replaced or deleted groups. */
7309 GROUP_COLLECTION_FOR_EACH (old_group, &ogm->old_groups) {
7310 ofproto->n_groups[old_group->type]++;
7311 if (new_group) {
7312 ovs_assert(group_collection_n(&ogm->old_groups) == 1);
7313 /* Transfer rules back. */
7314 rule_collection_move(&old_group->rules, &new_group->rules);
7315 } else {
7316 old_group->being_deleted = false;
7317 /* Revert rule deletion. */
7318 delete_flows_revert__(ofproto, &old_group->rules);
7319 }
7320 /* Restore visibility. */
7321 versions_set_remove_version(&old_group->versions,
7322 OVS_VERSION_NOT_REMOVED);
7323 }
7324 if (new_group) {
7325 /* Remove the new group immediately. It was never visible to
7326 * lookups. */
7327 cmap_remove(&ofproto->groups, &new_group->cmap_node,
7328 hash_int(new_group->group_id, 0));
7329 ofproto->n_groups[new_group->type]--;
7330 ofproto_group_unref(new_group);
7331 }
7332}
7333
2b0b0b80
JR
7334static void
7335ofproto_group_mod_finish(struct ofproto *ofproto,
7336 struct ofproto_group_mod *ogm,
7337 const struct openflow_mod_requester *req)
7338 OVS_REQUIRES(ofproto_mutex)
7339{
7340 struct ofgroup *new_group = ogm->new_group;
7341 struct ofgroup *old_group;
7342
ccb3bc08
JR
7343 if (new_group && group_collection_n(&ogm->old_groups) &&
7344 ofproto->ofproto_class->group_modify) {
2b0b0b80
JR
7345 /* Modify a group. */
7346 ovs_assert(group_collection_n(&ogm->old_groups) == 1);
2b0b0b80
JR
7347
7348 /* XXX: OK to lose old group's stats? */
7349 ofproto->ofproto_class->group_modify(new_group);
5d08a275 7350 }
2b0b0b80 7351
5d08a275
JR
7352 /* Delete old groups. */
7353 GROUP_COLLECTION_FOR_EACH(old_group, &ogm->old_groups) {
7354 delete_group_finish(ofproto, old_group);
2b0b0b80 7355 }
5d08a275 7356 remove_groups_postponed(&ogm->old_groups);
2b0b0b80
JR
7357
7358 if (req) {
3c35db62 7359 struct ofputil_requestforward rf;
2b0b0b80 7360 rf.xid = req->request->xid;
3c35db62 7361 rf.reason = OFPRFR_GROUP_MOD;
2b0b0b80
JR
7362 rf.group_mod = &ogm->gm;
7363 connmgr_send_requestforward(ofproto->connmgr, req->ofconn, &rf);
7364 }
7365}
7366
7367/* Delete all groups from 'ofproto'.
7368 *
7369 * This is intended for use within an ofproto provider's 'destruct'
7370 * function. */
d3b84833
BP
7371static void
7372ofproto_group_delete_all__(struct ofproto *ofproto)
7373 OVS_REQUIRES(ofproto_mutex)
2b0b0b80
JR
7374{
7375 struct ofproto_group_mod ogm;
2b0b0b80
JR
7376 ogm.gm.command = OFPGC11_DELETE;
7377 ogm.gm.group_id = OFPG_ALL;
5d08a275 7378 ogm.version = ofproto->tables_version + 1;
d3b84833 7379
2b0b0b80 7380 ofproto_group_mod_start(ofproto, &ogm);
5d08a275 7381 ofproto_bump_tables_version(ofproto);
2b0b0b80 7382 ofproto_group_mod_finish(ofproto, &ogm, NULL);
d3b84833
BP
7383}
7384
7385/* Delete all groups from 'ofproto'.
7386 *
7387 * This is intended for use within an ofproto provider's 'destruct'
7388 * function. */
7389void
7390ofproto_group_delete_all(struct ofproto *ofproto)
7391 OVS_EXCLUDED(ofproto_mutex)
7392{
7393 ovs_mutex_lock(&ofproto_mutex);
7394 ofproto_group_delete_all__(ofproto);
2b0b0b80
JR
7395 ovs_mutex_unlock(&ofproto_mutex);
7396}
7397
7398static enum ofperr
7399handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
7400 OVS_EXCLUDED(ofproto_mutex)
7401{
7402 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7403 struct ofproto_group_mod ogm;
7404 enum ofperr error;
7405
7406 error = reject_slave_controller(ofconn);
7407 if (error) {
7408 return error;
7409 }
7410
7411 error = ofputil_decode_group_mod(oh, &ogm.gm);
7412 if (error) {
7413 return error;
3c35db62 7414 }
2b0b0b80
JR
7415
7416 ovs_mutex_lock(&ofproto_mutex);
5d08a275 7417 ogm.version = ofproto->tables_version + 1;
2b0b0b80
JR
7418 error = ofproto_group_mod_start(ofproto, &ogm);
7419 if (!error) {
7420 struct openflow_mod_requester req = { ofconn, oh };
5d08a275
JR
7421
7422 ofproto_bump_tables_version(ofproto);
2b0b0b80 7423 ofproto_group_mod_finish(ofproto, &ogm, &req);
2c7ee524 7424 ofmonitor_flush(ofproto->connmgr);
2b0b0b80 7425 }
2b0b0b80
JR
7426 ovs_mutex_unlock(&ofproto_mutex);
7427
75868d0e 7428 ofputil_uninit_group_mod(&ogm.gm);
63eded98 7429
3c35db62 7430 return error;
7395c052
NZ
7431}
7432
3c1bb396
BP
7433enum ofputil_table_miss
7434ofproto_table_get_miss_config(const struct ofproto *ofproto, uint8_t table_id)
6d328fa2 7435{
82c22d34
BP
7436 enum ofputil_table_miss miss;
7437
7438 atomic_read_relaxed(&ofproto->tables[table_id].miss_config, &miss);
7439 return miss;
7440}
7441
7442static void
7443table_mod__(struct oftable *oftable,
de7d3c07 7444 const struct ofputil_table_mod *tm)
82c22d34 7445{
de7d3c07 7446 if (tm->miss == OFPUTIL_TABLE_MISS_DEFAULT) {
82c22d34
BP
7447 /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any
7448 * table-miss configuration (because the protocol used doesn't have
7449 * such a concept), so there's nothing to do. */
7450 } else {
de7d3c07 7451 atomic_store_relaxed(&oftable->miss_config, tm->miss);
82c22d34
BP
7452 }
7453
7454 unsigned int new_eviction = oftable->eviction;
de7d3c07 7455 if (tm->eviction == OFPUTIL_TABLE_EVICTION_ON) {
82c22d34 7456 new_eviction |= EVICTION_OPENFLOW;
de7d3c07 7457 } else if (tm->eviction == OFPUTIL_TABLE_EVICTION_OFF) {
82c22d34
BP
7458 new_eviction &= ~EVICTION_OPENFLOW;
7459 }
afcea9ea 7460
82c22d34
BP
7461 if (new_eviction != oftable->eviction) {
7462 ovs_mutex_lock(&ofproto_mutex);
7463 oftable_configure_eviction(oftable, new_eviction,
7464 oftable->eviction_fields,
7465 oftable->n_eviction_fields);
7466 ovs_mutex_unlock(&ofproto_mutex);
7467 }
de7d3c07
SJ
7468
7469 if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) {
7470 ovs_mutex_lock(&ofproto_mutex);
de7d3c07
SJ
7471 oftable->vacancy_down = tm->table_vacancy.vacancy_down;
7472 oftable->vacancy_up = tm->table_vacancy.vacancy_up;
6c6eedc5
SJ
7473 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_OFF) {
7474 oftable->vacancy_event = 0;
7475 } else if (!oftable->vacancy_event) {
7476 uint8_t vacancy = oftable_vacancy(oftable);
7477 oftable->vacancy_event = (vacancy < oftable->vacancy_up
7478 ? OFPTR_VACANCY_UP
7479 : OFPTR_VACANCY_DOWN);
7480 }
de7d3c07
SJ
7481 ovs_mutex_unlock(&ofproto_mutex);
7482 }
6d328fa2
SH
7483}
7484
67761761
SH
7485static enum ofperr
7486table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
7487{
3c1bb396 7488 if (!check_table_id(ofproto, tm->table_id)) {
67761761 7489 return OFPERR_OFPTMFC_BAD_TABLE;
82c22d34
BP
7490 }
7491
7492 /* Don't allow the eviction flags to be changed (except to the only fixed
7493 * value that OVS supports). OF1.4 says this is normal: "The
7494 * OFPTMPT_EVICTION property usually cannot be modified using a
7495 * OFP_TABLE_MOD request, because the eviction mechanism is switch
7496 * defined". */
7497 if (tm->eviction_flags != UINT32_MAX
7498 && tm->eviction_flags != OFPROTO_EVICTION_FLAGS) {
7499 return OFPERR_OFPTMFC_BAD_CONFIG;
7500 }
7501
7502 if (tm->table_id == OFPTT_ALL) {
7503 struct oftable *oftable;
7504 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
7505 if (!(oftable->flags & (OFTABLE_HIDDEN | OFTABLE_READONLY))) {
de7d3c07 7506 table_mod__(oftable, tm);
3c1bb396 7507 }
3c1bb396 7508 }
82c22d34
BP
7509 } else {
7510 struct oftable *oftable = &ofproto->tables[tm->table_id];
7511 if (oftable->flags & OFTABLE_READONLY) {
7512 return OFPERR_OFPTMFC_EPERM;
7513 }
de7d3c07 7514 table_mod__(oftable, tm);
67761761 7515 }
82c22d34 7516
67761761
SH
7517 return 0;
7518}
7519
918f2b82
AZ
7520static enum ofperr
7521handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
7522{
67761761 7523 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
918f2b82
AZ
7524 struct ofputil_table_mod tm;
7525 enum ofperr error;
7526
7527 error = reject_slave_controller(ofconn);
7528 if (error) {
7529 return error;
7530 }
7531
7532 error = ofputil_decode_table_mod(oh, &tm);
7533 if (error) {
7534 return error;
7535 }
7536
67761761 7537 return table_mod(ofproto, &tm);
918f2b82
AZ
7538}
7539
5bacd5cd
JR
7540/* Free resources that may be allocated by ofproto_flow_mod_init(). */
7541void
7542ofproto_flow_mod_uninit(struct ofproto_flow_mod *ofm)
7543{
7544 if (ofm->temp_rule) {
7545 ofproto_rule_unref(ofm->temp_rule);
7546 ofm->temp_rule = NULL;
7547 }
7548 if (ofm->criteria.version != OVS_VERSION_NOT_REMOVED) {
7549 rule_criteria_destroy(&ofm->criteria);
7550 }
7551 if (ofm->conjs) {
7552 free(ofm->conjs);
7553 ofm->conjs = NULL;
7554 ofm->n_conjs = 0;
7555 }
7556}
7557
777af88d 7558static enum ofperr
5bacd5cd 7559ofproto_flow_mod_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
2c7ee524 7560 const struct ofputil_flow_mod *fm, struct rule *rule)
5bacd5cd 7561 OVS_EXCLUDED(ofproto_mutex)
777af88d 7562{
5bacd5cd
JR
7563 enum ofperr error;
7564
7565 /* Forward flow mod fields we need later. */
7566 ofm->command = fm->command;
5bacd5cd
JR
7567 ofm->modify_cookie = fm->modify_cookie;
7568
7569 ofm->modify_may_add_flow = (fm->new_cookie != OVS_BE64_MAX
7570 && fm->cookie_mask == htonll(0));
8b6987d7
JR
7571 /* Old flags must be kept when modifying a flow, but we still must
7572 * honor the reset counts flag if present in the flow mod. */
7573 ofm->modify_keep_counts = !(fm->flags & OFPUTIL_FF_RESET_COUNTS);
5bacd5cd
JR
7574
7575 /* Initialize state needed by ofproto_flow_mod_uninit(). */
2c7ee524 7576 ofm->temp_rule = rule;
5bacd5cd
JR
7577 ofm->criteria.version = OVS_VERSION_NOT_REMOVED;
7578 ofm->conjs = NULL;
7579 ofm->n_conjs = 0;
7580
c184807c
JR
7581 bool check_buffer_id = false;
7582
5bacd5cd
JR
7583 switch (ofm->command) {
7584 case OFPFC_ADD:
c184807c 7585 check_buffer_id = true;
5bacd5cd
JR
7586 error = add_flow_init(ofproto, ofm, fm);
7587 break;
7588 case OFPFC_MODIFY:
c184807c 7589 check_buffer_id = true;
5bacd5cd
JR
7590 error = modify_flows_init_loose(ofproto, ofm, fm);
7591 break;
7592 case OFPFC_MODIFY_STRICT:
c184807c 7593 check_buffer_id = true;
5bacd5cd
JR
7594 error = modify_flow_init_strict(ofproto, ofm, fm);
7595 break;
7596 case OFPFC_DELETE:
7597 error = delete_flows_init_loose(ofproto, ofm, fm);
7598 break;
7599 case OFPFC_DELETE_STRICT:
7600 error = delete_flows_init_strict(ofproto, ofm, fm);
7601 break;
7602 default:
7603 error = OFPERR_OFPFMFC_BAD_COMMAND;
7604 break;
7605 }
c184807c
JR
7606 if (!error && check_buffer_id && fm->buffer_id != UINT32_MAX) {
7607 error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
7608 }
7609
cc099268 7610 if (error) {
5bacd5cd 7611 ofproto_flow_mod_uninit(ofm);
cc099268 7612 }
5bacd5cd
JR
7613 return error;
7614}
cc099268 7615
5bacd5cd
JR
7616static enum ofperr
7617ofproto_flow_mod_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
7618 OVS_REQUIRES(ofproto_mutex)
7619{
7620 enum ofperr error;
81dee635 7621
5bacd5cd
JR
7622 rule_collection_init(&ofm->old_rules);
7623 rule_collection_init(&ofm->new_rules);
81dee635 7624
5a27991d 7625 switch (ofm->command) {
1f42be1c 7626 case OFPFC_ADD:
5bacd5cd
JR
7627 error = add_flow_start(ofproto, ofm);
7628 break;
1f42be1c 7629 case OFPFC_MODIFY:
5bacd5cd
JR
7630 error = modify_flows_start_loose(ofproto, ofm);
7631 break;
1f42be1c 7632 case OFPFC_MODIFY_STRICT:
5bacd5cd
JR
7633 error = modify_flow_start_strict(ofproto, ofm);
7634 break;
1f42be1c 7635 case OFPFC_DELETE:
5bacd5cd
JR
7636 error = delete_flows_start_loose(ofproto, ofm);
7637 break;
1f42be1c 7638 case OFPFC_DELETE_STRICT:
5bacd5cd
JR
7639 error = delete_flow_start_strict(ofproto, ofm);
7640 break;
7641 default:
7642 OVS_NOT_REACHED();
1f42be1c 7643 }
5bacd5cd
JR
7644 /* Release resources not needed after start. */
7645 ofproto_flow_mod_uninit(ofm);
1f42be1c 7646
5bacd5cd
JR
7647 if (error) {
7648 rule_collection_destroy(&ofm->old_rules);
7649 rule_collection_destroy(&ofm->new_rules);
7650 }
7651 return error;
1f42be1c
JR
7652}
7653
7654static void
8be00367 7655ofproto_flow_mod_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
7656 OVS_REQUIRES(ofproto_mutex)
7657{
5a27991d 7658 switch (ofm->command) {
1f42be1c 7659 case OFPFC_ADD:
8be00367 7660 add_flow_revert(ofproto, ofm);
1f42be1c
JR
7661 break;
7662
7663 case OFPFC_MODIFY:
7664 case OFPFC_MODIFY_STRICT:
8be00367 7665 modify_flows_revert(ofproto, ofm);
1f42be1c
JR
7666 break;
7667
7668 case OFPFC_DELETE:
7669 case OFPFC_DELETE_STRICT:
8be00367 7670 delete_flows_revert(ofproto, ofm);
1f42be1c
JR
7671 break;
7672
7673 default:
7674 break;
7675 }
2c7ee524 7676
5bacd5cd
JR
7677 rule_collection_destroy(&ofm->old_rules);
7678 rule_collection_destroy(&ofm->new_rules);
1f42be1c
JR
7679}
7680
7681static void
2c7ee524 7682ofproto_flow_mod_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 7683 const struct openflow_mod_requester *req)
1f42be1c
JR
7684 OVS_REQUIRES(ofproto_mutex)
7685{
5a27991d 7686 switch (ofm->command) {
1f42be1c 7687 case OFPFC_ADD:
8be00367 7688 add_flow_finish(ofproto, ofm, req);
1f42be1c
JR
7689 break;
7690
7691 case OFPFC_MODIFY:
7692 case OFPFC_MODIFY_STRICT:
8be00367 7693 modify_flows_finish(ofproto, ofm, req);
1f42be1c
JR
7694 break;
7695
7696 case OFPFC_DELETE:
7697 case OFPFC_DELETE_STRICT:
8be00367 7698 delete_flows_finish(ofproto, ofm, req);
1f42be1c
JR
7699 break;
7700
7701 default:
7702 break;
7703 }
b0d38b2f 7704
5bacd5cd
JR
7705 rule_collection_destroy(&ofm->old_rules);
7706 rule_collection_destroy(&ofm->new_rules);
7707
b0d38b2f 7708 if (req) {
5a27991d 7709 ofconn_report_flow_mod(req->ofconn, ofm->command);
b0d38b2f 7710 }
1f42be1c
JR
7711}
7712
39c94593
JR
7713/* Commit phases (all while locking ofproto_mutex):
7714 *
7715 * 1. Begin: Gather resources and make changes visible in the next version.
7716 * - Mark affected rules for removal in the next version.
7717 * - Create new replacement rules, make visible in the next
7718 * version.
7719 * - Do not send any events or notifications.
7720 *
7721 * 2. Revert: Fail if any errors are found. After this point no errors are
7722 * possible. No visible changes were made, so rollback is minimal (remove
7723 * added invisible rules, restore visibility of rules marked for removal).
7724 *
1c38055d
JR
7725 * 3. Finish: Make the changes visible for lookups. Insert replacement rules to
7726 * the ofproto provider. Remove replaced and deleted rules from ofproto data
7727 * structures, and Schedule postponed removal of deleted rules from the
7728 * classifier. Send notifications, buffered packets, etc.
39c94593 7729 */
1f42be1c
JR
7730static enum ofperr
7731do_bundle_commit(struct ofconn *ofconn, uint32_t id, uint16_t flags)
7732{
7733 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
44e0c35d 7734 ovs_version_t version = ofproto->tables_version + 1;
1f42be1c
JR
7735 struct ofp_bundle *bundle;
7736 struct ofp_bundle_entry *be;
777af88d 7737 enum ofperr error;
1f42be1c
JR
7738
7739 bundle = ofconn_get_bundle(ofconn, id);
7740
7741 if (!bundle) {
7742 return OFPERR_OFPBFC_BAD_ID;
7743 }
7744 if (bundle->flags != flags) {
7745 error = OFPERR_OFPBFC_BAD_FLAGS;
7746 } else {
1c38055d
JR
7747 bool prev_is_port_mod = false;
7748
1f42be1c
JR
7749 error = 0;
7750 ovs_mutex_lock(&ofproto_mutex);
39c94593
JR
7751
7752 /* 1. Begin. */
1f42be1c
JR
7753 LIST_FOR_EACH (be, node, &bundle->msg_list) {
7754 if (be->type == OFPTYPE_PORT_MOD) {
1c38055d
JR
7755 /* Our port mods are not atomic. */
7756 if (flags & OFPBF_ATOMIC) {
7757 error = OFPERR_OFPBFC_MSG_FAILED;
7758 } else {
7759 prev_is_port_mod = true;
8be00367 7760 error = port_mod_start(ofconn, &be->opm.pm, &be->opm.port);
1c38055d 7761 }
25070e04
JR
7762 } else {
7763 /* Flow & group mods between port mods are applied as a single
7764 * version, but the versions are published only after we know
7765 * the commit is successful. */
1c38055d 7766 if (prev_is_port_mod) {
25070e04 7767 prev_is_port_mod = false;
8be00367 7768 ++version;
1c38055d 7769 }
25070e04
JR
7770 if (be->type == OFPTYPE_FLOW_MOD) {
7771 /* Store the version in which the changes should take
7772 * effect. */
7773 be->ofm.version = version;
7774 error = ofproto_flow_mod_start(ofproto, &be->ofm);
7775 } else if (be->type == OFPTYPE_GROUP_MOD) {
7776 /* Store the version in which the changes should take
7777 * effect. */
7778 be->ogm.version = version;
7779 error = ofproto_group_mod_start(ofproto, &be->ogm);
6dd3c787
JR
7780 } else if (be->type == OFPTYPE_PACKET_OUT) {
7781 be->opo.version = version;
7782 error = ofproto_packet_out_start(ofproto, &be->opo);
25070e04
JR
7783 } else {
7784 OVS_NOT_REACHED();
7785 }
1f42be1c
JR
7786 }
7787 if (error) {
7788 break;
7789 }
7790 }
1c38055d 7791
1f42be1c
JR
7792 if (error) {
7793 /* Send error referring to the original message. */
7794 if (error) {
51bb26fa 7795 ofconn_send_error(ofconn, &be->ofp_msg, error);
1f42be1c
JR
7796 error = OFPERR_OFPBFC_MSG_FAILED;
7797 }
7798
39c94593 7799 /* 2. Revert. Undo all the changes made above. */
1f42be1c
JR
7800 LIST_FOR_EACH_REVERSE_CONTINUE(be, node, &bundle->msg_list) {
7801 if (be->type == OFPTYPE_FLOW_MOD) {
8be00367 7802 ofproto_flow_mod_revert(ofproto, &be->ofm);
25070e04
JR
7803 } else if (be->type == OFPTYPE_GROUP_MOD) {
7804 ofproto_group_mod_revert(ofproto, &be->ogm);
6dd3c787
JR
7805 } else if (be->type == OFPTYPE_PACKET_OUT) {
7806 ofproto_packet_out_revert(ofproto, &be->opo);
1f42be1c 7807 }
1c38055d 7808 /* Nothing needs to be reverted for a port mod. */
1f42be1c
JR
7809 }
7810 } else {
39c94593 7811 /* 4. Finish. */
1f42be1c 7812 LIST_FOR_EACH (be, node, &bundle->msg_list) {
25070e04 7813 if (be->type == OFPTYPE_PORT_MOD) {
1c38055d
JR
7814 /* Perform the actual port mod. This is not atomic, i.e.,
7815 * the effects will be immediately seen by upcall
7816 * processing regardless of the lookup version. It should
7817 * be noted that port configuration changes can originate
7818 * also from OVSDB changes asynchronously to all upcall
7819 * processing. */
8be00367 7820 port_mod_finish(ofconn, &be->opm.pm, be->opm.port);
25070e04 7821 } else {
6dd3c787
JR
7822 version =
7823 (be->type == OFPTYPE_FLOW_MOD) ? be->ofm.version :
7824 (be->type == OFPTYPE_GROUP_MOD) ? be->ogm.version :
7825 (be->type == OFPTYPE_PACKET_OUT) ? be->opo.version :
7826 version;
7827
7828 /* Bump the lookup version to the one of the current
7829 * message. This makes all the changes in the bundle at
7830 * this version visible to lookups at once. */
7831 if (ofproto->tables_version < version) {
7832 ofproto->tables_version = version;
7833 ofproto->ofproto_class->set_tables_version(
7834 ofproto, ofproto->tables_version);
7835 }
7836
25070e04 7837 struct openflow_mod_requester req = { ofconn,
51bb26fa 7838 &be->ofp_msg };
25070e04 7839
6dd3c787 7840 if (be->type == OFPTYPE_FLOW_MOD) {
25070e04
JR
7841 ofproto_flow_mod_finish(ofproto, &be->ofm, &req);
7842 } else if (be->type == OFPTYPE_GROUP_MOD) {
25070e04 7843 ofproto_group_mod_finish(ofproto, &be->ogm, &req);
6dd3c787
JR
7844 } else if (be->type == OFPTYPE_PACKET_OUT) {
7845 ofproto_packet_out_finish(ofproto, &be->opo);
25070e04 7846 }
1f42be1c
JR
7847 }
7848 }
7849 }
1c38055d 7850
1f42be1c
JR
7851 ofmonitor_flush(ofproto->connmgr);
7852 ovs_mutex_unlock(&ofproto_mutex);
1f42be1c
JR
7853 }
7854
7855 /* The bundle is discarded regardless the outcome. */
0c78eebe 7856 ofp_bundle_remove__(ofconn, bundle);
1f42be1c
JR
7857 return error;
7858}
7859
7860static enum ofperr
7861handle_bundle_control(struct ofconn *ofconn, const struct ofp_header *oh)
7862{
777af88d 7863 struct ofputil_bundle_ctrl_msg bctrl;
777af88d 7864 struct ofputil_bundle_ctrl_msg reply;
1f42be1c
JR
7865 struct ofpbuf *buf;
7866 enum ofperr error;
777af88d 7867
ba9d374a
JR
7868 error = reject_slave_controller(ofconn);
7869 if (error) {
7870 return error;
7871 }
7872
777af88d
AC
7873 error = ofputil_decode_bundle_ctrl(oh, &bctrl);
7874 if (error) {
7875 return error;
7876 }
7877 reply.flags = 0;
7878 reply.bundle_id = bctrl.bundle_id;
7879
7880 switch (bctrl.type) {
51bb26fa
JR
7881 case OFPBCT_OPEN_REQUEST:
7882 error = ofp_bundle_open(ofconn, bctrl.bundle_id, bctrl.flags, oh);
777af88d
AC
7883 reply.type = OFPBCT_OPEN_REPLY;
7884 break;
7885 case OFPBCT_CLOSE_REQUEST:
7886 error = ofp_bundle_close(ofconn, bctrl.bundle_id, bctrl.flags);
ea53e3a8 7887 reply.type = OFPBCT_CLOSE_REPLY;
777af88d
AC
7888 break;
7889 case OFPBCT_COMMIT_REQUEST:
1f42be1c 7890 error = do_bundle_commit(ofconn, bctrl.bundle_id, bctrl.flags);
777af88d
AC
7891 reply.type = OFPBCT_COMMIT_REPLY;
7892 break;
7893 case OFPBCT_DISCARD_REQUEST:
7894 error = ofp_bundle_discard(ofconn, bctrl.bundle_id);
7895 reply.type = OFPBCT_DISCARD_REPLY;
7896 break;
7897
7898 case OFPBCT_OPEN_REPLY:
7899 case OFPBCT_CLOSE_REPLY:
7900 case OFPBCT_COMMIT_REPLY:
7901 case OFPBCT_DISCARD_REPLY:
7902 return OFPERR_OFPBFC_BAD_TYPE;
7903 break;
7904 }
7905
7906 if (!error) {
7907 buf = ofputil_encode_bundle_ctrl_reply(oh, &reply);
7908 ofconn_send_reply(ofconn, buf);
7909 }
7910 return error;
7911}
7912
777af88d
AC
7913static enum ofperr
7914handle_bundle_add(struct ofconn *ofconn, const struct ofp_header *oh)
5bacd5cd 7915 OVS_EXCLUDED(ofproto_mutex)
777af88d 7916{
7ac27a04 7917 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
777af88d
AC
7918 enum ofperr error;
7919 struct ofputil_bundle_add_msg badd;
7ac27a04
JR
7920 struct ofp_bundle_entry *bmsg;
7921 enum ofptype type;
777af88d 7922
ba9d374a
JR
7923 error = reject_slave_controller(ofconn);
7924 if (error) {
7925 return error;
7926 }
7927
7ac27a04 7928 error = ofputil_decode_bundle_add(oh, &badd, &type);
777af88d
AC
7929 if (error) {
7930 return error;
7931 }
7932
1f42be1c 7933 bmsg = ofp_bundle_entry_alloc(type, badd.msg);
7ac27a04 7934
6dd3c787
JR
7935 struct ofpbuf ofpacts;
7936 uint64_t ofpacts_stub[1024 / 8];
7937 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
7938
7ac27a04 7939 if (type == OFPTYPE_PORT_MOD) {
8be00367 7940 error = ofputil_decode_port_mod(badd.msg, &bmsg->opm.pm, false);
7ac27a04 7941 } else if (type == OFPTYPE_FLOW_MOD) {
5bacd5cd 7942 struct ofputil_flow_mod fm;
7ac27a04 7943
5bacd5cd 7944 error = ofputil_decode_flow_mod(&fm, badd.msg,
7ac27a04 7945 ofconn_get_protocol(ofconn),
8d8ab6c2 7946 ofproto_get_tun_tab(ofproto),
04f48a68 7947 &ofproto->vl_mff_map, &ofpacts,
7ac27a04
JR
7948 u16_to_ofp(ofproto->max_ports),
7949 ofproto->n_tables);
5bacd5cd 7950 if (!error) {
2c7ee524 7951 error = ofproto_flow_mod_init(ofproto, &bmsg->ofm, &fm, NULL);
5bacd5cd 7952 }
25070e04
JR
7953 } else if (type == OFPTYPE_GROUP_MOD) {
7954 error = ofputil_decode_group_mod(badd.msg, &bmsg->ogm.gm);
6dd3c787
JR
7955 } else if (type == OFPTYPE_PACKET_OUT) {
7956 struct ofputil_packet_out po;
7957
7958 COVERAGE_INC(ofproto_packet_out);
7959
7960 /* Decode message. */
89d7927b
YHW
7961 error = ofputil_decode_packet_out(&po, badd.msg,
7962 ofproto_get_tun_tab(ofproto),
7963 &ofpacts);
6dd3c787
JR
7964 if (!error) {
7965 po.ofpacts = ofpbuf_steal_data(&ofpacts); /* Move to heap. */
7966 error = ofproto_packet_out_init(ofproto, ofconn, &bmsg->opo, &po);
7967 }
7ac27a04
JR
7968 } else {
7969 OVS_NOT_REACHED();
7970 }
7971
6dd3c787
JR
7972 ofpbuf_uninit(&ofpacts);
7973
7ac27a04
JR
7974 if (!error) {
7975 error = ofp_bundle_add_message(ofconn, badd.bundle_id, badd.flags,
51bb26fa 7976 bmsg, oh);
7ac27a04
JR
7977 }
7978
7979 if (error) {
7980 ofp_bundle_entry_free(bmsg);
7981 }
7982
7983 return error;
777af88d
AC
7984}
7985
6159c531 7986static enum ofperr
4e548ad9 7987handle_tlv_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
6159c531 7988{
8d8ab6c2
JG
7989 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7990 struct tun_table *old_tab, *new_tab;
4e548ad9 7991 struct ofputil_tlv_table_mod ttm;
6159c531
JG
7992 enum ofperr error;
7993
7994 error = reject_slave_controller(ofconn);
7995 if (error) {
7996 return error;
7997 }
7998
4e548ad9 7999 error = ofputil_decode_tlv_table_mod(oh, &ttm);
6159c531
JG
8000 if (error) {
8001 return error;
8002 }
8003
8d8ab6c2
JG
8004 old_tab = ovsrcu_get_protected(struct tun_table *, &ofproto->metadata_tab);
8005 error = tun_metadata_table_mod(&ttm, old_tab, &new_tab);
8006 if (!error) {
04f48a68
YHW
8007 ovs_mutex_lock(&ofproto->vl_mff_map.mutex);
8008 error = mf_vl_mff_map_mod_from_tun_metadata(&ofproto->vl_mff_map,
8009 &ttm);
8010 ovs_mutex_unlock(&ofproto->vl_mff_map.mutex);
5c7c16d8
YHW
8011 if (!error) {
8012 ovsrcu_set(&ofproto->metadata_tab, new_tab);
8013 tun_metadata_postpone_free(old_tab);
bc36c3a8
YHW
8014 } else {
8015 tun_metadata_free(new_tab);
5c7c16d8 8016 }
8d8ab6c2 8017 }
9558d2a5 8018
4e548ad9 8019 ofputil_uninit_tlv_table(&ttm.mappings);
9558d2a5 8020 return error;
6159c531
JG
8021}
8022
8023static enum ofperr
4e548ad9 8024handle_tlv_table_request(struct ofconn *ofconn, const struct ofp_header *oh)
6159c531 8025{
8d8ab6c2 8026 const struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4e548ad9 8027 struct ofputil_tlv_table_reply ttr;
9558d2a5
JG
8028 struct ofpbuf *b;
8029
8d8ab6c2
JG
8030 tun_metadata_table_request(ofproto_get_tun_tab(ofproto), &ttr);
8031
4e548ad9
ML
8032 b = ofputil_encode_tlv_table_reply(oh, &ttr);
8033 ofputil_uninit_tlv_table(&ttr.mappings);
9558d2a5
JG
8034
8035 ofconn_send_reply(ofconn, b);
8036 return 0;
6159c531
JG
8037}
8038
90bf1e07 8039static enum ofperr
d1e2cf21 8040handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
15aaf599 8041 OVS_EXCLUDED(ofproto_mutex)
064af421 8042{
6fd6ed71 8043 const struct ofp_header *oh = msg->data;
982697a4 8044 enum ofptype type;
90bf1e07 8045 enum ofperr error;
064af421 8046
982697a4 8047 error = ofptype_decode(&type, oh);
d1e2cf21
BP
8048 if (error) {
8049 return error;
8050 }
ff3c2c63
YT
8051 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
8052 && ofpmp_more(oh)) {
8053 /* We have no buffer implementation for multipart requests.
8054 * Report overflow for requests which consists of multiple
8055 * messages. */
8056 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
8057 }
064af421 8058
982697a4 8059 switch (type) {
d1e2cf21 8060 /* OpenFlow requests. */
982697a4 8061 case OFPTYPE_ECHO_REQUEST:
d1e2cf21 8062 return handle_echo_request(ofconn, oh);
064af421 8063
982697a4 8064 case OFPTYPE_FEATURES_REQUEST:
d1e2cf21 8065 return handle_features_request(ofconn, oh);
064af421 8066
982697a4 8067 case OFPTYPE_GET_CONFIG_REQUEST:
d1e2cf21 8068 return handle_get_config_request(ofconn, oh);
064af421 8069
982697a4
BP
8070 case OFPTYPE_SET_CONFIG:
8071 return handle_set_config(ofconn, oh);
064af421 8072
982697a4
BP
8073 case OFPTYPE_PACKET_OUT:
8074 return handle_packet_out(ofconn, oh);
064af421 8075
982697a4 8076 case OFPTYPE_PORT_MOD:
d1e2cf21 8077 return handle_port_mod(ofconn, oh);
064af421 8078
982697a4 8079 case OFPTYPE_FLOW_MOD:
2e4f5fcf 8080 return handle_flow_mod(ofconn, oh);
064af421 8081
7395c052
NZ
8082 case OFPTYPE_GROUP_MOD:
8083 return handle_group_mod(ofconn, oh);
8084
918f2b82
AZ
8085 case OFPTYPE_TABLE_MOD:
8086 return handle_table_mod(ofconn, oh);
8087
9cae45dc
JR
8088 case OFPTYPE_METER_MOD:
8089 return handle_meter_mod(ofconn, oh);
8090
982697a4 8091 case OFPTYPE_BARRIER_REQUEST:
d1e2cf21 8092 return handle_barrier_request(ofconn, oh);
064af421 8093
6ea4776b
JR
8094 case OFPTYPE_ROLE_REQUEST:
8095 return handle_role_request(ofconn, oh);
8096
d1e2cf21 8097 /* OpenFlow replies. */
982697a4 8098 case OFPTYPE_ECHO_REPLY:
d1e2cf21 8099 return 0;
246e61ea 8100
d1e2cf21 8101 /* Nicira extension requests. */
982697a4 8102 case OFPTYPE_FLOW_MOD_TABLE_ID:
6c1491fb 8103 return handle_nxt_flow_mod_table_id(ofconn, oh);
d1e2cf21 8104
982697a4 8105 case OFPTYPE_SET_FLOW_FORMAT:
d1e2cf21
BP
8106 return handle_nxt_set_flow_format(ofconn, oh);
8107
982697a4 8108 case OFPTYPE_SET_PACKET_IN_FORMAT:
54834960
EJ
8109 return handle_nxt_set_packet_in_format(ofconn, oh);
8110
982697a4 8111 case OFPTYPE_SET_CONTROLLER_ID:
a7349929
BP
8112 return handle_nxt_set_controller_id(ofconn, oh);
8113
982697a4 8114 case OFPTYPE_FLOW_AGE:
f27f2134
BP
8115 /* Nothing to do. */
8116 return 0;
8117
982697a4 8118 case OFPTYPE_FLOW_MONITOR_CANCEL:
2b07c8b1
BP
8119 return handle_flow_monitor_cancel(ofconn, oh);
8120
982697a4 8121 case OFPTYPE_SET_ASYNC_CONFIG:
80d5aefd
BP
8122 return handle_nxt_set_async_config(ofconn, oh);
8123
c423b3b3
AC
8124 case OFPTYPE_GET_ASYNC_REQUEST:
8125 return handle_nxt_get_async_request(ofconn, oh);
8126
77ab5fd2
BP
8127 case OFPTYPE_NXT_RESUME:
8128 return handle_nxt_resume(ofconn, oh);
8129
349adfb2 8130 /* Statistics requests. */
982697a4
BP
8131 case OFPTYPE_DESC_STATS_REQUEST:
8132 return handle_desc_stats_request(ofconn, oh);
8133
8134 case OFPTYPE_FLOW_STATS_REQUEST:
8135 return handle_flow_stats_request(ofconn, oh);
8136
8137 case OFPTYPE_AGGREGATE_STATS_REQUEST:
8138 return handle_aggregate_stats_request(ofconn, oh);
8139
8140 case OFPTYPE_TABLE_STATS_REQUEST:
8141 return handle_table_stats_request(ofconn, oh);
8142
3c4e10fb
BP
8143 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
8144 return handle_table_features_request(ofconn, oh);
8145
03c72922
BP
8146 case OFPTYPE_TABLE_DESC_REQUEST:
8147 return handle_table_desc_request(ofconn, oh);
8148
982697a4
BP
8149 case OFPTYPE_PORT_STATS_REQUEST:
8150 return handle_port_stats_request(ofconn, oh);
8151
8152 case OFPTYPE_QUEUE_STATS_REQUEST:
8153 return handle_queue_stats_request(ofconn, oh);
8154
8155 case OFPTYPE_PORT_DESC_STATS_REQUEST:
8156 return handle_port_desc_stats_request(ofconn, oh);
8157
8158 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
8159 return handle_flow_monitor_request(ofconn, oh);
8160
261bd854
BP
8161 case OFPTYPE_METER_STATS_REQUEST:
8162 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9cae45dc
JR
8163 return handle_meter_request(ofconn, oh, type);
8164
261bd854 8165 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9cae45dc
JR
8166 return handle_meter_features_request(ofconn, oh);
8167
261bd854 8168 case OFPTYPE_GROUP_STATS_REQUEST:
7395c052
NZ
8169 return handle_group_stats_request(ofconn, oh);
8170
261bd854 8171 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
7395c052
NZ
8172 return handle_group_desc_stats_request(ofconn, oh);
8173
261bd854 8174 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
7395c052
NZ
8175 return handle_group_features_stats_request(ofconn, oh);
8176
7395c052 8177 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
e8f9a7bb 8178 return handle_queue_get_config_request(ofconn, oh);
2e1ae200 8179
777af88d
AC
8180 case OFPTYPE_BUNDLE_CONTROL:
8181 return handle_bundle_control(ofconn, oh);
8182
8183 case OFPTYPE_BUNDLE_ADD_MESSAGE:
8184 return handle_bundle_add(ofconn, oh);
8185
4e548ad9
ML
8186 case OFPTYPE_NXT_TLV_TABLE_MOD:
8187 return handle_tlv_table_mod(ofconn, oh);
6159c531 8188
4e548ad9
ML
8189 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
8190 return handle_tlv_table_request(ofconn, oh);
6159c531 8191
fb8f22c1
BY
8192 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
8193 return handle_ipfix_bridge_stats_request(ofconn, oh);
8194
8195 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
8196 return handle_ipfix_flow_stats_request(ofconn, oh);
8197
2a7c4805
JP
8198 case OFPTYPE_CT_FLUSH_ZONE:
8199 return handle_nxt_ct_flush_zone(ofconn, oh);
8200
982697a4
BP
8201 case OFPTYPE_HELLO:
8202 case OFPTYPE_ERROR:
8203 case OFPTYPE_FEATURES_REPLY:
8204 case OFPTYPE_GET_CONFIG_REPLY:
8205 case OFPTYPE_PACKET_IN:
8206 case OFPTYPE_FLOW_REMOVED:
8207 case OFPTYPE_PORT_STATUS:
8208 case OFPTYPE_BARRIER_REPLY:
c545d38d 8209 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
982697a4
BP
8210 case OFPTYPE_DESC_STATS_REPLY:
8211 case OFPTYPE_FLOW_STATS_REPLY:
8212 case OFPTYPE_QUEUE_STATS_REPLY:
8213 case OFPTYPE_PORT_STATS_REPLY:
8214 case OFPTYPE_TABLE_STATS_REPLY:
8215 case OFPTYPE_AGGREGATE_STATS_REPLY:
8216 case OFPTYPE_PORT_DESC_STATS_REPLY:
8217 case OFPTYPE_ROLE_REPLY:
8218 case OFPTYPE_FLOW_MONITOR_PAUSED:
8219 case OFPTYPE_FLOW_MONITOR_RESUMED:
8220 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2e1ae200 8221 case OFPTYPE_GET_ASYNC_REPLY:
261bd854
BP
8222 case OFPTYPE_GROUP_STATS_REPLY:
8223 case OFPTYPE_GROUP_DESC_STATS_REPLY:
8224 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
8225 case OFPTYPE_METER_STATS_REPLY:
8226 case OFPTYPE_METER_CONFIG_STATS_REPLY:
8227 case OFPTYPE_METER_FEATURES_STATS_REPLY:
8228 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
03c72922 8229 case OFPTYPE_TABLE_DESC_REPLY:
252f3411 8230 case OFPTYPE_ROLE_STATUS:
3c35db62 8231 case OFPTYPE_REQUESTFORWARD:
6c6eedc5 8232 case OFPTYPE_TABLE_STATUS:
4e548ad9 8233 case OFPTYPE_NXT_TLV_TABLE_REPLY:
fb8f22c1
BY
8234 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
8235 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
064af421 8236 default:
76ec08e0
YT
8237 if (ofpmsg_is_stat_request(oh)) {
8238 return OFPERR_OFPBRC_BAD_STAT;
8239 } else {
8240 return OFPERR_OFPBRC_BAD_TYPE;
8241 }
064af421 8242 }
d1e2cf21 8243}
064af421 8244
b20f4073 8245static void
e03248b7 8246handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
15aaf599 8247 OVS_EXCLUDED(ofproto_mutex)
d1e2cf21 8248{
d51c8b71
JR
8249 enum ofperr error = handle_openflow__(ofconn, ofp_msg);
8250
b20f4073 8251 if (error) {
6fd6ed71 8252 ofconn_send_error(ofconn, ofp_msg->data, error);
064af421 8253 }
d1e2cf21 8254 COVERAGE_INC(ofproto_recv_openflow);
7ee20df1
BP
8255}
8256\f
064af421 8257static uint64_t
fa60c019 8258pick_datapath_id(const struct ofproto *ofproto)
064af421 8259{
fa60c019 8260 const struct ofport *port;
064af421 8261
abe529af 8262 port = ofproto_get_port(ofproto, OFPP_LOCAL);
fa60c019 8263 if (port) {
74ff3298 8264 struct eth_addr ea;
fa60c019
BP
8265 int error;
8266
74ff3298 8267 error = netdev_get_etheraddr(port->netdev, &ea);
064af421
BP
8268 if (!error) {
8269 return eth_addr_to_uint64(ea);
8270 }
fbfa2911
BP
8271 VLOG_WARN("%s: could not get MAC address for %s (%s)",
8272 ofproto->name, netdev_get_name(port->netdev),
10a89ef0 8273 ovs_strerror(error));
064af421 8274 }
fa60c019 8275 return ofproto->fallback_dpid;
064af421
BP
8276}
8277
8278static uint64_t
8279pick_fallback_dpid(void)
8280{
74ff3298
JR
8281 struct eth_addr ea;
8282 eth_addr_nicira_random(&ea);
064af421
BP
8283 return eth_addr_to_uint64(ea);
8284}
8285\f
254750ce
BP
8286/* Table overflow policy. */
8287
ad3efdcb
EJ
8288/* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
8289 * to NULL if the table is not configured to evict rules or if the table
8290 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
8291 * or with no timeouts are not evictable.) */
8292static bool
8293choose_rule_to_evict(struct oftable *table, struct rule **rulep)
15aaf599 8294 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8295{
8296 struct eviction_group *evg;
8297
ad3efdcb 8298 *rulep = NULL;
82c22d34 8299 if (!table->eviction) {
ad3efdcb 8300 return false;
254750ce
BP
8301 }
8302
8303 /* In the common case, the outer and inner loops here will each be entered
8304 * exactly once:
8305 *
8306 * - The inner loop normally "return"s in its first iteration. If the
8307 * eviction group has any evictable rules, then it always returns in
8308 * some iteration.
8309 *
8310 * - The outer loop only iterates more than once if the largest eviction
8311 * group has no evictable rules.
8312 *
8313 * - The outer loop can exit only if table's 'max_flows' is all filled up
afe2143d 8314 * by unevictable rules. */
254750ce
BP
8315 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
8316 struct rule *rule;
8317
8318 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
15aaf599
BP
8319 *rulep = rule;
8320 return true;
254750ce
BP
8321 }
8322 }
8323
ad3efdcb 8324 return false;
254750ce 8325}
254750ce
BP
8326\f
8327/* Eviction groups. */
8328
8329/* Returns the priority to use for an eviction_group that contains 'n_rules'
8330 * rules. The priority contains low-order random bits to ensure that eviction
8331 * groups with the same number of rules are prioritized randomly. */
8332static uint32_t
8333eviction_group_priority(size_t n_rules)
8334{
8335 uint16_t size = MIN(UINT16_MAX, n_rules);
8336 return (size << 16) | random_uint16();
8337}
8338
8339/* Updates 'evg', an eviction_group within 'table', following a change that
8340 * adds or removes rules in 'evg'. */
8341static void
8342eviction_group_resized(struct oftable *table, struct eviction_group *evg)
15aaf599 8343 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8344{
8345 heap_change(&table->eviction_groups_by_size, &evg->size_node,
8346 eviction_group_priority(heap_count(&evg->rules)));
8347}
8348
8349/* Destroys 'evg', an eviction_group within 'table':
8350 *
8351 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
8352 * rules themselves, just removes them from the eviction group.)
8353 *
8354 * - Removes 'evg' from 'table'.
8355 *
8356 * - Frees 'evg'. */
8357static void
8358eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
15aaf599 8359 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8360{
8361 while (!heap_is_empty(&evg->rules)) {
8362 struct rule *rule;
8363
8364 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
8365 rule->eviction_group = NULL;
8366 }
8367 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
8368 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
8369 heap_destroy(&evg->rules);
8370 free(evg);
8371}
8372
8373/* Removes 'rule' from its eviction group, if any. */
8374static void
8375eviction_group_remove_rule(struct rule *rule)
15aaf599 8376 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8377{
8378 if (rule->eviction_group) {
8379 struct oftable *table = &rule->ofproto->tables[rule->table_id];
8380 struct eviction_group *evg = rule->eviction_group;
8381
8382 rule->eviction_group = NULL;
8383 heap_remove(&evg->rules, &rule->evg_node);
8384 if (heap_is_empty(&evg->rules)) {
8385 eviction_group_destroy(table, evg);
8386 } else {
8387 eviction_group_resized(table, evg);
8388 }
8389 }
8390}
8391
8392/* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
8393 * returns the hash value. */
8394static uint32_t
8395eviction_group_hash_rule(struct rule *rule)
15aaf599 8396 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8397{
8398 struct oftable *table = &rule->ofproto->tables[rule->table_id];
8399 const struct mf_subfield *sf;
5cb7a798 8400 struct flow flow;
254750ce
BP
8401 uint32_t hash;
8402
8403 hash = table->eviction_group_id_basis;
8fd47924 8404 miniflow_expand(rule->cr.match.flow, &flow);
254750ce
BP
8405 for (sf = table->eviction_fields;
8406 sf < &table->eviction_fields[table->n_eviction_fields];
8407 sf++)
8408 {
aff49b8c 8409 if (mf_are_prereqs_ok(sf->field, &flow, NULL)) {
254750ce
BP
8410 union mf_value value;
8411
5cb7a798 8412 mf_get_value(sf->field, &flow, &value);
254750ce
BP
8413 if (sf->ofs) {
8414 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
8415 }
8416 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
8417 unsigned int start = sf->ofs + sf->n_bits;
8418 bitwise_zero(&value, sf->field->n_bytes, start,
8419 sf->field->n_bytes * 8 - start);
8420 }
8421 hash = hash_bytes(&value, sf->field->n_bytes, hash);
8422 } else {
8423 hash = hash_int(hash, 0);
8424 }
8425 }
8426
8427 return hash;
8428}
8429
8430/* Returns an eviction group within 'table' with the given 'id', creating one
8431 * if necessary. */
8432static struct eviction_group *
8433eviction_group_find(struct oftable *table, uint32_t id)
15aaf599 8434 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8435{
8436 struct eviction_group *evg;
8437
8438 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
8439 return evg;
8440 }
8441
8442 evg = xmalloc(sizeof *evg);
8443 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
8444 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
8445 eviction_group_priority(0));
8446 heap_init(&evg->rules);
8447
8448 return evg;
8449}
8450
8451/* Returns an eviction priority for 'rule'. The return value should be
f70b94de
BP
8452 * interpreted so that higher priorities make a rule a more attractive
8453 * candidate for eviction. */
8454static uint64_t
dc437090 8455rule_eviction_priority(struct ofproto *ofproto, struct rule *rule)
15aaf599 8456 OVS_REQUIRES(ofproto_mutex)
254750ce 8457{
f70b94de
BP
8458 /* Calculate absolute time when this flow will expire. If it will never
8459 * expire, then return 0 to make it unevictable. */
dc437090 8460 long long int expiration = LLONG_MAX;
dc437090 8461 if (rule->hard_timeout) {
f70b94de
BP
8462 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
8463 ovs_mutex_lock(&rule->mutex);
8464 long long int modified = rule->modified;
8465 ovs_mutex_unlock(&rule->mutex);
8466
dc437090
JR
8467 expiration = modified + rule->hard_timeout * 1000;
8468 }
8469 if (rule->idle_timeout) {
8470 uint64_t packets, bytes;
8471 long long int used;
8472 long long int idle_expiration;
8473
8474 ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used);
8475 idle_expiration = used + rule->idle_timeout * 1000;
8476 expiration = MIN(expiration, idle_expiration);
8477 }
254750ce
BP
8478 if (expiration == LLONG_MAX) {
8479 return 0;
8480 }
8481
8482 /* Calculate the time of expiration as a number of (approximate) seconds
8483 * after program startup.
8484 *
8485 * This should work OK for program runs that last UINT32_MAX seconds or
8486 * less. Therefore, please restart OVS at least once every 136 years. */
f70b94de 8487 uint32_t expiration_ofs = (expiration >> 10) - (time_boot_msec() >> 10);
254750ce 8488
f70b94de
BP
8489 /* Combine expiration time with OpenFlow "importance" to form a single
8490 * priority value. We want flows with relatively low "importance" to be
8491 * evicted before even considering expiration time, so put "importance" in
8492 * the most significant bits and expiration time in the least significant
8493 * bits.
8494 *
8495 * Small 'priority' should be evicted before those with large 'priority'.
8496 * The caller expects the opposite convention (a large return value being
8497 * more attractive for eviction) so we invert it before returning. */
8498 uint64_t priority = ((uint64_t) rule->importance << 32) + expiration_ofs;
8499 return UINT64_MAX - priority;
254750ce
BP
8500}
8501
8502/* Adds 'rule' to an appropriate eviction group for its oftable's
8503 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
8504 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
8505 * own).
8506 *
8507 * The caller must ensure that 'rule' is not already in an eviction group. */
8508static void
8509eviction_group_add_rule(struct rule *rule)
15aaf599 8510 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8511{
8512 struct ofproto *ofproto = rule->ofproto;
8513 struct oftable *table = &ofproto->tables[rule->table_id];
a3779dbc 8514 bool has_timeout;
254750ce 8515
dc437090
JR
8516 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
8517 * so no additional protection is needed. */
a3779dbc 8518 has_timeout = rule->hard_timeout || rule->idle_timeout;
a3779dbc 8519
82c22d34 8520 if (table->eviction && has_timeout) {
254750ce
BP
8521 struct eviction_group *evg;
8522
8523 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
8524
8525 rule->eviction_group = evg;
8526 heap_insert(&evg->rules, &rule->evg_node,
dc437090 8527 rule_eviction_priority(ofproto, rule));
254750ce
BP
8528 eviction_group_resized(table, evg);
8529 }
8530}
8531\f
d0918789
BP
8532/* oftables. */
8533
8534/* Initializes 'table'. */
8535static void
8536oftable_init(struct oftable *table)
8537{
5c67e4af 8538 memset(table, 0, sizeof *table);
d70e8c28 8539 classifier_init(&table->cls, flow_segment_u64s);
ec1c5c7e 8540 table->max_flows = UINT_MAX;
d79e3d70 8541 table->n_flows = 0;
82c22d34
BP
8542 hmap_init(&table->eviction_groups_by_id);
8543 heap_init(&table->eviction_groups_by_size);
3c1bb396 8544 atomic_init(&table->miss_config, OFPUTIL_TABLE_MISS_DEFAULT);
f017d986 8545
f017d986
JR
8546 classifier_set_prefix_fields(&table->cls, default_prefix_fields,
8547 ARRAY_SIZE(default_prefix_fields));
d611866c
SH
8548
8549 atomic_init(&table->n_matched, 0);
8550 atomic_init(&table->n_missed, 0);
d0918789
BP
8551}
8552
254750ce 8553/* Destroys 'table', including its classifier and eviction groups.
d0918789
BP
8554 *
8555 * The caller is responsible for freeing 'table' itself. */
8556static void
8557oftable_destroy(struct oftable *table)
8558{
cb22974d 8559 ovs_assert(classifier_is_empty(&table->cls));
82c22d34 8560
7394b8fb 8561 ovs_mutex_lock(&ofproto_mutex);
82c22d34 8562 oftable_configure_eviction(table, 0, NULL, 0);
7394b8fb 8563 ovs_mutex_unlock(&ofproto_mutex);
82c22d34
BP
8564
8565 hmap_destroy(&table->eviction_groups_by_id);
8566 heap_destroy(&table->eviction_groups_by_size);
d0918789 8567 classifier_destroy(&table->cls);
254750ce
BP
8568 free(table->name);
8569}
8570
8571/* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
8572 * string, then 'table' will use its default name.
8573 *
8574 * This only affects the name exposed for a table exposed through the OpenFlow
8575 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
8576static void
8577oftable_set_name(struct oftable *table, const char *name)
8578{
8579 if (name && name[0]) {
8580 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
8581 if (!table->name || strncmp(name, table->name, len)) {
8582 free(table->name);
8583 table->name = xmemdup0(name, len);
8584 }
8585 } else {
8586 free(table->name);
8587 table->name = NULL;
8588 }
8589}
8590
254750ce
BP
8591/* oftables support a choice of two policies when adding a rule would cause the
8592 * number of flows in the table to exceed the configured maximum number: either
8593 * they can refuse to add the new flow or they can evict some existing flow.
8594 * This function configures the latter policy on 'table', with fairness based
8595 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
8596 * 'n_fields' as 0 disables fairness.) */
8597static void
82c22d34
BP
8598oftable_configure_eviction(struct oftable *table, unsigned int eviction,
8599 const struct mf_subfield *fields, size_t n_fields)
15aaf599 8600 OVS_REQUIRES(ofproto_mutex)
254750ce 8601{
254750ce
BP
8602 struct rule *rule;
8603
82c22d34 8604 if ((table->eviction != 0) == (eviction != 0)
254750ce
BP
8605 && n_fields == table->n_eviction_fields
8606 && (!n_fields
8607 || !memcmp(fields, table->eviction_fields,
8608 n_fields * sizeof *fields))) {
82c22d34
BP
8609 /* The set of eviction fields did not change. If 'eviction' changed,
8610 * it remains nonzero, so that we can just update table->eviction
8611 * without fussing with the eviction groups. */
8612 table->eviction = eviction;
254750ce
BP
8613 return;
8614 }
8615
82c22d34
BP
8616 /* Destroy existing eviction groups, then destroy and recreate data
8617 * structures to recover memory. */
8618 struct eviction_group *evg, *next;
8619 HMAP_FOR_EACH_SAFE (evg, next, id_node, &table->eviction_groups_by_id) {
8620 eviction_group_destroy(table, evg);
8621 }
8622 hmap_destroy(&table->eviction_groups_by_id);
254750ce 8623 hmap_init(&table->eviction_groups_by_id);
82c22d34 8624 heap_destroy(&table->eviction_groups_by_size);
254750ce
BP
8625 heap_init(&table->eviction_groups_by_size);
8626
82c22d34
BP
8627 /* Replace eviction groups by the new ones, if there is a change. Free the
8628 * old fields only after allocating the new ones, because 'fields ==
8629 * table->eviction_fields' is possible. */
8630 struct mf_subfield *old_fields = table->eviction_fields;
8631 table->n_eviction_fields = n_fields;
8632 table->eviction_fields = (fields
8633 ? xmemdup(fields, n_fields * sizeof *fields)
8634 : NULL);
8635 free(old_fields);
8636
8637 /* Add the new eviction groups, if enabled. */
8638 table->eviction = eviction;
8639 if (table->eviction) {
8640 table->eviction_group_id_basis = random_uint32();
8641 CLS_FOR_EACH (rule, cr, &table->cls) {
8642 eviction_group_add_rule(rule);
8643 }
254750ce 8644 }
d0918789
BP
8645}
8646
bc5e6a91
JR
8647/* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted
8648 * it to the classifier. */
8649static void
8650ofproto_rule_insert__(struct ofproto *ofproto, struct rule *rule)
8651 OVS_REQUIRES(ofproto_mutex)
8652{
8653 const struct rule_actions *actions = rule_get_actions(rule);
8654
30d0452c
JR
8655 /* A rule may not be reinserted. */
8656 ovs_assert(rule->state == RULE_INITIALIZED);
39c94593 8657
bc5e6a91 8658 if (rule->hard_timeout || rule->idle_timeout) {
417e7e66 8659 ovs_list_insert(&ofproto->expirable, &rule->expirable);
bc5e6a91
JR
8660 }
8661 cookies_insert(ofproto, rule);
8662 eviction_group_add_rule(rule);
8663 if (actions->has_meter) {
8664 meter_insert_rule(rule);
8665 }
cc099268
JR
8666 if (actions->has_groups) {
8667 const struct ofpact_group *a;
8668 OFPACT_FOR_EACH_TYPE_FLATTENED (a, GROUP, actions->ofpacts,
8669 actions->ofpacts_len) {
8670 struct ofgroup *group;
8671
5d08a275
JR
8672 group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX,
8673 false);
cc099268
JR
8674 ovs_assert(group != NULL);
8675 group_add_rule(group, rule);
8676 }
8677 }
8678
30d0452c 8679 rule->state = RULE_INSERTED;
bc5e6a91
JR
8680}
8681
6787a49f
JR
8682/* Removes 'rule' from the ofproto data structures. Caller may have deferred
8683 * the removal from the classifier. */
d0918789 8684static void
802f84ff 8685ofproto_rule_remove__(struct ofproto *ofproto, struct rule *rule)
15aaf599 8686 OVS_REQUIRES(ofproto_mutex)
d0918789 8687{
30d0452c 8688 ovs_assert(rule->state == RULE_INSERTED);
39c94593 8689
98eaac36 8690 cookies_remove(ofproto, rule);
2c916028 8691
254750ce 8692 eviction_group_remove_rule(rule);
417e7e66
BW
8693 if (!ovs_list_is_empty(&rule->expirable)) {
8694 ovs_list_remove(&rule->expirable);
e503cc19 8695 }
417e7e66
BW
8696 if (!ovs_list_is_empty(&rule->meter_list_node)) {
8697 ovs_list_remove(&rule->meter_list_node);
8698 ovs_list_init(&rule->meter_list_node);
9cae45dc 8699 }
802f84ff 8700
cc099268
JR
8701 /* Remove the rule from any groups, except from the group that is being
8702 * deleted, if any. */
8703 const struct rule_actions *actions = rule_get_actions(rule);
8704
8705 if (actions->has_groups) {
8706 const struct ofpact_group *a;
8707
8483173b 8708 OFPACT_FOR_EACH_TYPE_FLATTENED(a, GROUP, actions->ofpacts,
cc099268
JR
8709 actions->ofpacts_len) {
8710 struct ofgroup *group;
8711
5d08a275
JR
8712 group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX,
8713 false);
cc099268
JR
8714 ovs_assert(group);
8715
8716 /* Leave the rule for the group that is being deleted, if any,
8717 * as we still need the list of rules for clean-up. */
8718 if (!group->being_deleted) {
8719 group_remove_rule(group, rule);
8720 }
8721 }
8722 }
8723
30d0452c 8724 rule->state = RULE_REMOVED;
0b4f2078 8725}
d0918789 8726\f
abe529af 8727/* unixctl commands. */
7aa697dd 8728
abe529af 8729struct ofproto *
2ac6bedd 8730ofproto_lookup(const char *name)
7aa697dd 8731{
2ac6bedd 8732 struct ofproto *ofproto;
7aa697dd 8733
2ac6bedd
BP
8734 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
8735 &all_ofprotos) {
8736 if (!strcmp(ofproto->name, name)) {
8737 return ofproto;
8738 }
7aa697dd 8739 }
2ac6bedd 8740 return NULL;
7aa697dd
BP
8741}
8742
8743static void
0e15264f
BP
8744ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
8745 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7aa697dd 8746{
7aa697dd 8747 struct ofproto *ofproto;
7aa697dd 8748 struct ds results;
7aa697dd 8749
7aa697dd 8750 ds_init(&results);
2ac6bedd
BP
8751 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
8752 ds_put_format(&results, "%s\n", ofproto->name);
7aa697dd 8753 }
bde9f75d 8754 unixctl_command_reply(conn, ds_cstr(&results));
7aa697dd 8755 ds_destroy(&results);
7aa697dd
BP
8756}
8757
8758static void
8759ofproto_unixctl_init(void)
8760{
8761 static bool registered;
8762 if (registered) {
8763 return;
8764 }
8765 registered = true;
8766
0e15264f
BP
8767 unixctl_command_register("ofproto/list", "", 0, 0,
8768 ofproto_unixctl_list, NULL);
064af421 8769}
f0fb825a
EG
8770
8771void
8772ofproto_set_vlan_limit(int vlan_limit)
8773{
8774 flow_limit_vlans(vlan_limit);
8775}