]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto.c
ovs-atomic: Fix typo in comment.
[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 3892 ofproto_port_get_stats(port, &ops.stats);
971f4b39 3893 netdev_get_custom_stats(port->netdev, &ops.custom_stats);
f8e4867e
SH
3894
3895 ofputil_append_port_stat(replies, &ops);
971f4b39
MW
3896
3897 netdev_free_custom_stats_counters(&ops.custom_stats);
abaad8cf
JP
3898}
3899
70ae4f93
BP
3900static void
3901handle_port_request(struct ofconn *ofconn,
3902 const struct ofp_header *request, ofp_port_t port_no,
ca6ba700 3903 void (*cb)(struct ofport *, struct ovs_list *replies))
064af421 3904{
70ae4f93 3905 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421 3906 struct ofport *port;
ca6ba700 3907 struct ovs_list replies;
064af421 3908
982697a4 3909 ofpmp_init(&replies, request);
7f05e7ab 3910 if (port_no != OFPP_ANY) {
70ae4f93 3911 port = ofproto_get_port(ofproto, port_no);
abaad8cf 3912 if (port) {
70ae4f93 3913 cb(port, &replies);
abaad8cf
JP
3914 }
3915 } else {
70ae4f93
BP
3916 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3917 cb(port, &replies);
abaad8cf 3918 }
064af421
BP
3919 }
3920
63f2140a 3921 ofconn_send_replies(ofconn, &replies);
70ae4f93
BP
3922}
3923
3924static enum ofperr
3925handle_port_stats_request(struct ofconn *ofconn,
3926 const struct ofp_header *request)
3927{
3928 ofp_port_t port_no;
3929 enum ofperr error;
3930
3931 error = ofputil_decode_port_stats_request(request, &port_no);
3932 if (!error) {
3933 handle_port_request(ofconn, request, port_no, append_port_stat);
3934 }
3935 return error;
3936}
3937
3938static void
ca6ba700 3939append_port_desc(struct ofport *port, struct ovs_list *replies)
70ae4f93
BP
3940{
3941 ofputil_append_port_desc_stats_reply(&port->pp, replies);
064af421
BP
3942}
3943
2be393ed
JP
3944static enum ofperr
3945handle_port_desc_stats_request(struct ofconn *ofconn,
982697a4 3946 const struct ofp_header *request)
2be393ed 3947{
70ae4f93
BP
3948 ofp_port_t port_no;
3949 enum ofperr error;
2be393ed 3950
70ae4f93
BP
3951 error = ofputil_decode_port_desc_stats_request(request, &port_no);
3952 if (!error) {
3953 handle_port_request(ofconn, request, port_no, append_port_desc);
2be393ed 3954 }
70ae4f93 3955 return error;
2be393ed
JP
3956}
3957
98eaac36
JR
3958static uint32_t
3959hash_cookie(ovs_be64 cookie)
3960{
965607c8 3961 return hash_uint64((OVS_FORCE uint64_t)cookie);
98eaac36
JR
3962}
3963
3964static void
3965cookies_insert(struct ofproto *ofproto, struct rule *rule)
2c916028 3966 OVS_REQUIRES(ofproto_mutex)
98eaac36
JR
3967{
3968 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3969 hash_cookie(rule->flow_cookie));
3970}
3971
3972static void
3973cookies_remove(struct ofproto *ofproto, struct rule *rule)
2c916028 3974 OVS_REQUIRES(ofproto_mutex)
98eaac36
JR
3975{
3976 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3977}
3978
c6ebb8fb 3979static void
65e0be10
BP
3980calc_duration(long long int start, long long int now,
3981 uint32_t *sec, uint32_t *nsec)
c6ebb8fb 3982{
f27f2134 3983 long long int msecs = now - start;
588cd7b5
BP
3984 *sec = msecs / 1000;
3985 *nsec = (msecs % 1000) * (1000 * 1000);
3986}
3987
48266274 3988/* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
554764d1
SH
3989 * true if 'table_id' is OK, false otherwise. */
3990static bool
48266274
BP
3991check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3992{
554764d1 3993 return table_id == OFPTT_ALL || table_id < ofproto->n_tables;
48266274
BP
3994}
3995
5c67e4af 3996static struct oftable *
d4ce8a49 3997next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
5c67e4af
BP
3998{
3999 struct oftable *table;
4000
4001 for (table = &ofproto->tables[table_id];
4002 table < &ofproto->tables[ofproto->n_tables];
4003 table++) {
4004 if (!(table->flags & OFTABLE_HIDDEN)) {
4005 return table;
4006 }
4007 }
4008
4009 return NULL;
4010}
4011
d0918789 4012static struct oftable *
d4ce8a49 4013first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
064af421 4014{
6c1491fb 4015 if (table_id == 0xff) {
5c67e4af 4016 return next_visible_table(ofproto, 0);
6c1491fb
BP
4017 } else if (table_id < ofproto->n_tables) {
4018 return &ofproto->tables[table_id];
a02e5331 4019 } else {
6c1491fb 4020 return NULL;
a02e5331 4021 }
064af421
BP
4022}
4023
d0918789 4024static struct oftable *
d4ce8a49
BP
4025next_matching_table(const struct ofproto *ofproto,
4026 const struct oftable *table, uint8_t table_id)
6c1491fb 4027{
5c67e4af
BP
4028 return (table_id == 0xff
4029 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
6c1491fb
BP
4030 : NULL);
4031}
4032
d0918789 4033/* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
6c1491fb
BP
4034 *
4035 * - If TABLE_ID is 0xff, this iterates over every classifier table in
5c67e4af 4036 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
6c1491fb
BP
4037 *
4038 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
5c67e4af
BP
4039 * only once, for that table. (This can be used to access tables marked
4040 * OFTABLE_HIDDEN.)
6c1491fb 4041 *
48266274
BP
4042 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
4043 * entered at all. (Perhaps you should have validated TABLE_ID with
4044 * check_table_id().)
6c1491fb
BP
4045 *
4046 * All parameters are evaluated multiple times.
4047 */
d0918789
BP
4048#define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
4049 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
4050 (TABLE) != NULL; \
4051 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
6c1491fb 4052
a9b22b7f
BP
4053/* Initializes 'criteria' in a straightforward way based on the other
4054 * parameters.
4055 *
dd51dae2
BP
4056 * By default, the criteria include flows that are read-only, on the assumption
4057 * that the collected flows won't be modified. Call rule_criteria_require_rw()
4058 * if flows will be modified.
4059 *
a9b22b7f
BP
4060 * For "loose" matching, the 'priority' parameter is unimportant and may be
4061 * supplied as 0. */
4062static void
4063rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
18721c4a 4064 const struct match *match, int priority,
44e0c35d 4065 ovs_version_t version, ovs_be64 cookie,
18721c4a
JR
4066 ovs_be64 cookie_mask, ofp_port_t out_port,
4067 uint32_t out_group)
a9b22b7f
BP
4068{
4069 criteria->table_id = table_id;
bd53aa17
JR
4070 cls_rule_init(&criteria->cr, match, priority);
4071 criteria->version = version;
a9b22b7f
BP
4072 criteria->cookie = cookie;
4073 criteria->cookie_mask = cookie_mask;
4074 criteria->out_port = out_port;
4075 criteria->out_group = out_group;
dd51dae2
BP
4076
4077 /* We ordinarily want to skip hidden rules, but there has to be a way for
4078 * code internal to OVS to modify and delete them, so if the criteria
4079 * specify a priority that can only be for a hidden flow, then allow hidden
4080 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
4081 * with hidden flows because OpenFlow uses only a 16-bit field to specify
4082 * priority.) */
4083 criteria->include_hidden = priority > UINT16_MAX;
4084
4085 /* We assume that the criteria are being used to collect flows for reading
4086 * but not modification. Thus, we should collect read-only flows. */
4087 criteria->include_readonly = true;
4088}
4089
4090/* By default, criteria initialized by rule_criteria_init() will match flows
4091 * that are read-only, on the assumption that the collected flows won't be
4092 * modified. Call this function to match only flows that are be modifiable.
4093 *
4094 * Specify 'can_write_readonly' as false in ordinary circumstances, true if the
4095 * caller has special privileges that allow it to modify even "read-only"
4096 * flows. */
4097static void
4098rule_criteria_require_rw(struct rule_criteria *criteria,
4099 bool can_write_readonly)
4100{
4101 criteria->include_readonly = can_write_readonly;
a9b22b7f
BP
4102}
4103
4104static void
4105rule_criteria_destroy(struct rule_criteria *criteria)
4106{
4107 cls_rule_destroy(&criteria->cr);
5bacd5cd 4108 criteria->version = OVS_VERSION_NOT_REMOVED; /* Mark as destroyed. */
a9b22b7f
BP
4109}
4110
39c94593
JR
4111/* Schedules postponed removal of rules, destroys 'rules'. */
4112static void
cc099268 4113remove_rules_postponed(struct rule_collection *rules)
39c94593
JR
4114 OVS_REQUIRES(ofproto_mutex)
4115{
fe59694b
JR
4116 if (rule_collection_n(rules) > 0) {
4117 if (rule_collection_n(rules) == 1) {
4118 ovsrcu_postpone(remove_rule_rcu, rule_collection_rules(rules)[0]);
4119 rule_collection_init(rules);
39c94593
JR
4120 } else {
4121 ovsrcu_postpone(remove_rules_rcu, rule_collection_detach(rules));
4122 }
4123 }
4124}
4125
5d08a275
JR
4126/* Schedules postponed removal of groups, destroys 'groups'. */
4127static void
4128remove_groups_postponed(struct group_collection *groups)
4129 OVS_REQUIRES(ofproto_mutex)
4130{
4131 if (group_collection_n(groups) > 0) {
4132 if (group_collection_n(groups) == 1) {
4133 ovsrcu_postpone(remove_group_rcu,
4134 group_collection_groups(groups)[0]);
4135 group_collection_init(groups);
4136 } else {
4137 ovsrcu_postpone(remove_groups_rcu,
4138 group_collection_detach(groups));
4139 }
4140 }
4141}
4142
dd51dae2
BP
4143/* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This
4144 * function verifies most of the criteria in 'c' itself, but the caller must
4145 * check 'c->cr' itself.
4146 *
2b7b1427 4147 * Rules that have already been marked for removal are not collected.
ce59413f 4148 *
dd51dae2 4149 * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and
b20f4073
BP
4150 * 'c' only includes modifiable rules). */
4151static void
a9b22b7f 4152collect_rule(struct rule *rule, const struct rule_criteria *c,
dd51dae2 4153 struct rule_collection *rules, size_t *n_readonly)
15aaf599 4154 OVS_REQUIRES(ofproto_mutex)
6c1d7642 4155{
dd51dae2
BP
4156 if ((c->table_id == rule->table_id || c->table_id == 0xff)
4157 && ofproto_rule_has_out_port(rule, c->out_port)
4158 && ofproto_rule_has_out_group(rule, c->out_group)
4159 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)
ce59413f 4160 && (!rule_is_hidden(rule) || c->include_hidden)
bd53aa17 4161 && cls_rule_visible_in_version(&rule->cr, c->version)) {
dd51dae2 4162 /* Rule matches all the criteria... */
834fe5cb 4163 if (!rule_is_readonly(rule) || c->include_readonly) {
dd51dae2 4164 /* ...add it. */
a8e547c1 4165 rule_collection_add(rules, rule);
dd51dae2
BP
4166 } else {
4167 /* ...except it's read-only. */
4168 ++*n_readonly;
6c1d7642 4169 }
6c1d7642
BP
4170 }
4171}
4172
a9b22b7f
BP
4173/* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4174 * on classifiers rules are done in the "loose" way required for OpenFlow
4175 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
9ed18e46
BP
4176 * 'rules'.
4177 *
9ed18e46 4178 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 4179static enum ofperr
a9b22b7f 4180collect_rules_loose(struct ofproto *ofproto,
a8e547c1
BP
4181 const struct rule_criteria *criteria,
4182 struct rule_collection *rules)
15aaf599 4183 OVS_REQUIRES(ofproto_mutex)
9ed18e46 4184{
d0918789 4185 struct oftable *table;
554764d1 4186 enum ofperr error = 0;
dd51dae2 4187 size_t n_readonly = 0;
48266274 4188
a8e547c1
BP
4189 rule_collection_init(rules);
4190
554764d1
SH
4191 if (!check_table_id(ofproto, criteria->table_id)) {
4192 error = OFPERR_OFPBRC_BAD_TABLE_ID;
a8e547c1 4193 goto exit;
48266274 4194 }
9ed18e46 4195
b8266395 4196 if (criteria->cookie_mask == OVS_BE64_MAX) {
98eaac36
JR
4197 struct rule *rule;
4198
a9b22b7f
BP
4199 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
4200 hash_cookie(criteria->cookie),
98eaac36 4201 &ofproto->cookies) {
a9b22b7f 4202 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
b20f4073 4203 collect_rule(rule, criteria, rules, &n_readonly);
98eaac36
JR
4204 }
4205 }
6c1d7642 4206 } else {
a9b22b7f 4207 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
6c1d7642 4208 struct rule *rule;
9ed18e46 4209
bd53aa17
JR
4210 CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &criteria->cr,
4211 criteria->version) {
b20f4073 4212 collect_rule(rule, criteria, rules, &n_readonly);
9ed18e46
BP
4213 }
4214 }
4215 }
48d28ac1 4216
a8e547c1 4217exit:
fe59694b 4218 if (!error && !rule_collection_n(rules) && n_readonly) {
dd51dae2
BP
4219 /* We didn't find any rules to modify. We did find some read-only
4220 * rules that we're not allowed to modify, so report that. */
4221 error = OFPERR_OFPBRC_EPERM;
4222 }
a8e547c1
BP
4223 if (error) {
4224 rule_collection_destroy(rules);
4225 }
48d28ac1 4226 return error;
9ed18e46
BP
4227}
4228
a9b22b7f
BP
4229/* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
4230 * on classifiers rules are done in the "strict" way required for OpenFlow
4231 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
4232 * rules on list 'rules'.
9ed18e46 4233 *
9ed18e46 4234 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 4235static enum ofperr
a9b22b7f 4236collect_rules_strict(struct ofproto *ofproto,
a8e547c1
BP
4237 const struct rule_criteria *criteria,
4238 struct rule_collection *rules)
15aaf599 4239 OVS_REQUIRES(ofproto_mutex)
9ed18e46 4240{
d0918789 4241 struct oftable *table;
dd51dae2 4242 size_t n_readonly = 0;
d51c8b71 4243 enum ofperr error = 0;
48266274 4244
a8e547c1
BP
4245 rule_collection_init(rules);
4246
554764d1
SH
4247 if (!check_table_id(ofproto, criteria->table_id)) {
4248 error = OFPERR_OFPBRC_BAD_TABLE_ID;
a8e547c1 4249 goto exit;
48266274 4250 }
9ed18e46 4251
b8266395 4252 if (criteria->cookie_mask == OVS_BE64_MAX) {
98eaac36
JR
4253 struct rule *rule;
4254
a9b22b7f
BP
4255 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
4256 hash_cookie(criteria->cookie),
98eaac36 4257 &ofproto->cookies) {
a9b22b7f 4258 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
b20f4073 4259 collect_rule(rule, criteria, rules, &n_readonly);
98eaac36
JR
4260 }
4261 }
6c1d7642 4262 } else {
a9b22b7f 4263 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
6c1d7642 4264 struct rule *rule;
9ed18e46 4265
a9b22b7f 4266 rule = rule_from_cls_rule(classifier_find_rule_exactly(
bd53aa17
JR
4267 &table->cls, &criteria->cr,
4268 criteria->version));
6c1d7642 4269 if (rule) {
b20f4073 4270 collect_rule(rule, criteria, rules, &n_readonly);
9ed18e46
BP
4271 }
4272 }
4273 }
48d28ac1 4274
a8e547c1 4275exit:
fe59694b 4276 if (!error && !rule_collection_n(rules) && n_readonly) {
b20f4073
BP
4277 /* We didn't find any rules to modify. We did find some read-only
4278 * rules that we're not allowed to modify, so report that. */
4279 error = OFPERR_OFPBRC_EPERM;
4280 }
a8e547c1
BP
4281 if (error) {
4282 rule_collection_destroy(rules);
4283 }
6c1d7642 4284 return error;
9ed18e46
BP
4285}
4286
f27f2134
BP
4287/* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
4288 * forced into the range of a uint16_t. */
4289static int
4290age_secs(long long int age_ms)
4291{
4292 return (age_ms < 0 ? 0
4293 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
4294 : (unsigned int) age_ms / 1000);
4295}
4296
90bf1e07 4297static enum ofperr
63f2140a 4298handle_flow_stats_request(struct ofconn *ofconn,
982697a4 4299 const struct ofp_header *request)
15aaf599 4300 OVS_EXCLUDED(ofproto_mutex)
064af421 4301{
64ff1d96 4302 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
81d1ea94 4303 struct ofputil_flow_stats_request fsr;
a9b22b7f 4304 struct rule_criteria criteria;
a8e547c1 4305 struct rule_collection rules;
ca6ba700 4306 struct ovs_list replies;
90bf1e07 4307 enum ofperr error;
09246b99 4308
8d8ab6c2 4309 error = ofputil_decode_flow_stats_request(&fsr, request,
3cddeff0
YHW
4310 ofproto_get_tun_tab(ofproto),
4311 &ofproto->vl_mff_map);
09246b99
BP
4312 if (error) {
4313 return error;
4314 }
4315
44e0c35d 4316 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, OVS_VERSION_MAX,
2b7b1427
JR
4317 fsr.cookie, fsr.cookie_mask, fsr.out_port,
4318 fsr.out_group);
15aaf599
BP
4319
4320 ovs_mutex_lock(&ofproto_mutex);
a9b22b7f
BP
4321 error = collect_rules_loose(ofproto, &criteria, &rules);
4322 rule_criteria_destroy(&criteria);
15aaf599
BP
4323 if (!error) {
4324 rule_collection_ref(&rules);
4325 }
4326 ovs_mutex_unlock(&ofproto_mutex);
4327
9ed18e46
BP
4328 if (error) {
4329 return error;
4330 }
5ecc9d81 4331
982697a4 4332 ofpmp_init(&replies, request);
fe59694b
JR
4333 struct rule *rule;
4334 RULE_COLLECTION_FOR_EACH (rule, &rules) {
f27f2134 4335 long long int now = time_msec();
9ed18e46 4336 struct ofputil_flow_stats fs;
15aaf599 4337 long long int created, used, modified;
dc723c44 4338 const struct rule_actions *actions;
15aaf599 4339 enum ofputil_flow_mod_flags flags;
a3779dbc 4340
d10976b7 4341 ovs_mutex_lock(&rule->mutex);
15aaf599 4342 fs.cookie = rule->flow_cookie;
a3779dbc
EJ
4343 fs.idle_timeout = rule->idle_timeout;
4344 fs.hard_timeout = rule->hard_timeout;
ca26eb44 4345 fs.importance = rule->importance;
15aaf599 4346 created = rule->created;
15aaf599 4347 modified = rule->modified;
06a0f3e2 4348 actions = rule_get_actions(rule);
15aaf599 4349 flags = rule->flags;
d10976b7 4350 ovs_mutex_unlock(&rule->mutex);
a3779dbc 4351
dc437090
JR
4352 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
4353 &fs.byte_count, &used);
4354
15aaf599
BP
4355 minimatch_expand(&rule->cr.match, &fs.match);
4356 fs.table_id = rule->table_id;
4357 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
4358 fs.priority = rule->cr.priority;
4359 fs.idle_age = age_secs(now - used);
4360 fs.hard_age = age_secs(now - modified);
15aaf599
BP
4361 fs.ofpacts = actions->ofpacts;
4362 fs.ofpacts_len = actions->ofpacts_len;
3f517bcd 4363
15aaf599 4364 fs.flags = flags;
8d8ab6c2
JG
4365 ofputil_append_flow_stats_reply(&fs, &replies,
4366 ofproto_get_tun_tab(ofproto));
3c4486a5 4367 }
15aaf599
BP
4368
4369 rule_collection_unref(&rules);
a8e547c1
BP
4370 rule_collection_destroy(&rules);
4371
63f2140a 4372 ofconn_send_replies(ofconn, &replies);
5ecc9d81 4373
09246b99
BP
4374 return 0;
4375}
4376
4f2cad2c 4377static void
8d8ab6c2 4378flow_stats_ds(struct ofproto *ofproto, struct rule *rule, struct ds *results)
4f2cad2c 4379{
4f2cad2c 4380 uint64_t packet_count, byte_count;
dc723c44 4381 const struct rule_actions *actions;
dc437090 4382 long long int created, used;
4f2cad2c 4383
dc437090
JR
4384 rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
4385 &byte_count, &used);
4f2cad2c 4386
15aaf599 4387 ovs_mutex_lock(&rule->mutex);
06a0f3e2 4388 actions = rule_get_actions(rule);
15aaf599
BP
4389 created = rule->created;
4390 ovs_mutex_unlock(&rule->mutex);
4391
6c1491fb
BP
4392 if (rule->table_id != 0) {
4393 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
4394 }
15aaf599 4395 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
4f2cad2c
JP
4396 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
4397 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
50f96b10 4398 cls_rule_format(&rule->cr, ofproto_get_tun_tab(ofproto), NULL, results);
a5df0e72 4399 ds_put_char(results, ',');
15aaf599 4400
455ecd77 4401 ds_put_cstr(results, "actions=");
50f96b10 4402 ofpacts_format(actions->ofpacts, actions->ofpacts_len, NULL, results);
15aaf599 4403
4f2cad2c
JP
4404 ds_put_cstr(results, "\n");
4405}
4406
d295e8e9 4407/* Adds a pretty-printed description of all flows to 'results', including
ee8b231c 4408 * hidden flows (e.g., set up by in-band control). */
4f2cad2c
JP
4409void
4410ofproto_get_all_flows(struct ofproto *p, struct ds *results)
4411{
d0918789 4412 struct oftable *table;
6c1491fb 4413
d0918789 4414 OFPROTO_FOR_EACH_TABLE (table, p) {
6c1491fb 4415 struct rule *rule;
064af421 4416
5f0476ce 4417 CLS_FOR_EACH (rule, cr, &table->cls) {
8d8ab6c2 4418 flow_stats_ds(p, rule, results);
6c1491fb 4419 }
064af421 4420 }
064af421
BP
4421}
4422
b5827b24
BP
4423/* Obtains the NetFlow engine type and engine ID for 'ofproto' into
4424 * '*engine_type' and '*engine_id', respectively. */
4425void
4426ofproto_get_netflow_ids(const struct ofproto *ofproto,
4427 uint8_t *engine_type, uint8_t *engine_id)
4428{
abe529af 4429 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
b5827b24
BP
4430}
4431
8f5514fe
AW
4432/* Checks the status change of CFM on 'ofport'.
4433 *
4434 * Returns true if 'ofproto_class' does not support 'cfm_status_changed'. */
4435bool
4436ofproto_port_cfm_status_changed(struct ofproto *ofproto, ofp_port_t ofp_port)
4437{
4438 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
4439 return (ofport && ofproto->ofproto_class->cfm_status_changed
4440 ? ofproto->ofproto_class->cfm_status_changed(ofport)
4441 : true);
4442}
4443
88bf179a
AW
4444/* Checks the status of CFM configured on 'ofp_port' within 'ofproto'.
4445 * Returns 0 if the port's CFM status was successfully stored into
4446 * '*status'. Returns positive errno if the port did not have CFM
8f5514fe 4447 * configured.
9a9e3786 4448 *
88bf179a
AW
4449 * The caller must provide and own '*status', and must free 'status->rmps'.
4450 * '*status' is indeterminate if the return value is non-zero. */
4451int
4e022ec0 4452ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
685acfd9 4453 struct cfm_status *status)
3967a833
MM
4454{
4455 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
88bf179a
AW
4456 return (ofport && ofproto->ofproto_class->get_cfm_status
4457 ? ofproto->ofproto_class->get_cfm_status(ofport, status)
4458 : EOPNOTSUPP);
3967a833
MM
4459}
4460
90bf1e07 4461static enum ofperr
76c93b22 4462handle_aggregate_stats_request(struct ofconn *ofconn,
982697a4 4463 const struct ofp_header *oh)
15aaf599 4464 OVS_EXCLUDED(ofproto_mutex)
27d34fce 4465{
76c93b22 4466 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
81d1ea94 4467 struct ofputil_flow_stats_request request;
76c93b22 4468 struct ofputil_aggregate_stats stats;
5e9d0469 4469 bool unknown_packets, unknown_bytes;
a9b22b7f 4470 struct rule_criteria criteria;
a8e547c1 4471 struct rule_collection rules;
76c93b22 4472 struct ofpbuf *reply;
90bf1e07 4473 enum ofperr error;
27d34fce 4474
8d8ab6c2 4475 error = ofputil_decode_flow_stats_request(&request, oh,
3cddeff0
YHW
4476 ofproto_get_tun_tab(ofproto),
4477 &ofproto->vl_mff_map);
76c93b22
BP
4478 if (error) {
4479 return error;
4480 }
5ecc9d81 4481
a9b22b7f 4482 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
44e0c35d 4483 OVS_VERSION_MAX, request.cookie, request.cookie_mask,
a9b22b7f 4484 request.out_port, request.out_group);
15aaf599
BP
4485
4486 ovs_mutex_lock(&ofproto_mutex);
a9b22b7f
BP
4487 error = collect_rules_loose(ofproto, &criteria, &rules);
4488 rule_criteria_destroy(&criteria);
15aaf599
BP
4489 if (!error) {
4490 rule_collection_ref(&rules);
4491 }
4492 ovs_mutex_unlock(&ofproto_mutex);
4493
9ed18e46
BP
4494 if (error) {
4495 return error;
4496 }
3c4486a5 4497
9ed18e46 4498 memset(&stats, 0, sizeof stats);
5e9d0469 4499 unknown_packets = unknown_bytes = false;
fe59694b
JR
4500
4501 struct rule *rule;
4502 RULE_COLLECTION_FOR_EACH (rule, &rules) {
9ed18e46
BP
4503 uint64_t packet_count;
4504 uint64_t byte_count;
dc437090 4505 long long int used;
5ecc9d81 4506
9ed18e46 4507 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
dc437090 4508 &byte_count, &used);
5ecc9d81 4509
5e9d0469
BP
4510 if (packet_count == UINT64_MAX) {
4511 unknown_packets = true;
4512 } else {
4513 stats.packet_count += packet_count;
4514 }
4515
4516 if (byte_count == UINT64_MAX) {
4517 unknown_bytes = true;
4518 } else {
4519 stats.byte_count += byte_count;
4520 }
4521
9ed18e46 4522 stats.flow_count++;
3c4486a5 4523 }
5e9d0469
BP
4524 if (unknown_packets) {
4525 stats.packet_count = UINT64_MAX;
4526 }
4527 if (unknown_bytes) {
4528 stats.byte_count = UINT64_MAX;
4529 }
27d34fce 4530
15aaf599 4531 rule_collection_unref(&rules);
a8e547c1
BP
4532 rule_collection_destroy(&rules);
4533
982697a4 4534 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
76c93b22 4535 ofconn_send_reply(ofconn, reply);
09246b99
BP
4536
4537 return 0;
4538}
4539
c1c9c9c4 4540struct queue_stats_cbdata {
ca0f572c 4541 struct ofport *ofport;
ca6ba700 4542 struct ovs_list replies;
6dc34a0d 4543 long long int now;
c1c9c9c4
BP
4544};
4545
4546static void
db9220c3 4547put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
c1c9c9c4
BP
4548 const struct netdev_queue_stats *stats)
4549{
6dc34a0d 4550 struct ofputil_queue_stats oqs;
c1c9c9c4 4551
6dc34a0d
BP
4552 oqs.port_no = cbdata->ofport->pp.port_no;
4553 oqs.queue_id = queue_id;
4554 oqs.tx_bytes = stats->tx_bytes;
4555 oqs.tx_packets = stats->tx_packets;
4556 oqs.tx_errors = stats->tx_errors;
4557 if (stats->created != LLONG_MIN) {
4558 calc_duration(stats->created, cbdata->now,
4559 &oqs.duration_sec, &oqs.duration_nsec);
4560 } else {
4561 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
4562 }
64626975 4563 ofputil_append_queue_stat(&cbdata->replies, &oqs);
c1c9c9c4
BP
4564}
4565
4566static void
db9220c3 4567handle_queue_stats_dump_cb(uint32_t queue_id,
c1c9c9c4
BP
4568 struct netdev_queue_stats *stats,
4569 void *cbdata_)
4570{
4571 struct queue_stats_cbdata *cbdata = cbdata_;
4572
4573 put_queue_stats(cbdata, queue_id, stats);
4574}
4575
0414d158 4576static enum ofperr
ca0f572c 4577handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
c1c9c9c4
BP
4578 struct queue_stats_cbdata *cbdata)
4579{
ca0f572c 4580 cbdata->ofport = port;
c1c9c9c4
BP
4581 if (queue_id == OFPQ_ALL) {
4582 netdev_dump_queue_stats(port->netdev,
4583 handle_queue_stats_dump_cb, cbdata);
4584 } else {
4585 struct netdev_queue_stats stats;
4586
1ac788f6
BP
4587 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
4588 put_queue_stats(cbdata, queue_id, &stats);
0414d158
BP
4589 } else {
4590 return OFPERR_OFPQOFC_BAD_QUEUE;
1ac788f6 4591 }
c1c9c9c4 4592 }
0414d158 4593 return 0;
c1c9c9c4
BP
4594}
4595
90bf1e07 4596static enum ofperr
63f2140a 4597handle_queue_stats_request(struct ofconn *ofconn,
982697a4 4598 const struct ofp_header *rq)
c1c9c9c4 4599{
64ff1d96 4600 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
c1c9c9c4 4601 struct queue_stats_cbdata cbdata;
0414d158 4602 struct ofport *port;
0414d158 4603 enum ofperr error;
64626975 4604 struct ofputil_queue_stats_request oqsr;
c1c9c9c4 4605
c1c9c9c4
BP
4606 COVERAGE_INC(ofproto_queue_req);
4607
982697a4 4608 ofpmp_init(&cbdata.replies, rq);
6dc34a0d 4609 cbdata.now = time_msec();
c1c9c9c4 4610
64626975
SH
4611 error = ofputil_decode_queue_stats_request(rq, &oqsr);
4612 if (error) {
4613 return error;
4614 }
4615
7f05e7ab 4616 if (oqsr.port_no == OFPP_ANY) {
0414d158 4617 error = OFPERR_OFPQOFC_BAD_QUEUE;
4e8e4213 4618 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
64626975 4619 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
0414d158
BP
4620 error = 0;
4621 }
c1c9c9c4 4622 }
0414d158 4623 } else {
64626975 4624 port = ofproto_get_port(ofproto, oqsr.port_no);
0414d158 4625 error = (port
64626975 4626 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
0414d158
BP
4627 : OFPERR_OFPQOFC_BAD_PORT);
4628 }
4629 if (!error) {
4630 ofconn_send_replies(ofconn, &cbdata.replies);
c1c9c9c4 4631 } else {
63f2140a 4632 ofpbuf_list_delete(&cbdata.replies);
c1c9c9c4 4633 }
c1c9c9c4 4634
0414d158 4635 return error;
c1c9c9c4 4636}
7ee20df1 4637
2e31a9b8 4638static enum ofperr
6787a49f 4639evict_rules_from_table(struct oftable *table)
15aaf599 4640 OVS_REQUIRES(ofproto_mutex)
2e31a9b8 4641{
802f84ff
JR
4642 enum ofperr error = 0;
4643 struct rule_collection rules;
6787a49f 4644 unsigned int count = table->n_flows;
802f84ff
JR
4645 unsigned int max_flows = table->max_flows;
4646
4647 rule_collection_init(&rules);
4648
4649 while (count-- > max_flows) {
2e31a9b8 4650 struct rule *rule;
8037acb4 4651
2e31a9b8 4652 if (!choose_rule_to_evict(table, &rule)) {
802f84ff
JR
4653 error = OFPERR_OFPFMFC_TABLE_FULL;
4654 break;
2e31a9b8 4655 } else {
802f84ff
JR
4656 eviction_group_remove_rule(rule);
4657 rule_collection_add(&rules, rule);
2e31a9b8 4658 }
8037acb4 4659 }
802f84ff 4660 delete_flows__(&rules, OFPRR_EVICTION, NULL);
2e31a9b8 4661
802f84ff 4662 return error;
8037acb4
BP
4663}
4664
18080541
BP
4665static void
4666get_conjunctions(const struct ofputil_flow_mod *fm,
4667 struct cls_conjunction **conjsp, size_t *n_conjsp)
18080541
BP
4668{
4669 struct cls_conjunction *conjs = NULL;
4670 int n_conjs = 0;
4671
f08e39dd
BP
4672 const struct ofpact *ofpact;
4673 OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) {
4674 if (ofpact->type == OFPACT_CONJUNCTION) {
18080541 4675 n_conjs++;
f08e39dd
BP
4676 } else if (ofpact->type != OFPACT_NOTE) {
4677 /* "conjunction" may appear with "note" actions but not with any
4678 * other type of actions. */
4679 ovs_assert(!n_conjs);
4680 break;
18080541 4681 }
f08e39dd
BP
4682 }
4683 if (n_conjs) {
4684 int i = 0;
18080541
BP
4685
4686 conjs = xzalloc(n_conjs * sizeof *conjs);
18080541 4687 OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) {
f08e39dd
BP
4688 if (ofpact->type == OFPACT_CONJUNCTION) {
4689 struct ofpact_conjunction *oc = ofpact_get_CONJUNCTION(ofpact);
4690 conjs[i].clause = oc->clause;
4691 conjs[i].n_clauses = oc->n_clauses;
4692 conjs[i].id = oc->id;
4693 i++;
4694 }
18080541
BP
4695 }
4696 }
4697
4698 *conjsp = conjs;
4699 *n_conjsp = n_conjs;
4700}
4701
5bacd5cd
JR
4702/* add_flow_init(), add_flow_start(), add_flow_revert(), and add_flow_finish()
4703 * implement OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
1f42be1c
JR
4704 * in which no matching flow already exists in the flow table.
4705 *
5bacd5cd
JR
4706 * add_flow_init() creates a new flow according to 'fm' and stores it to 'ofm'
4707 * for later reference. If the flow replaces other flow, it will be updated to
4708 * match modify semantics later by add_flow_start() (by calling
4709 * replace_rule_start()).
1f42be1c 4710 *
5bacd5cd 4711 * Returns 0 on success, or an OpenFlow error code on failure.
1f42be1c 4712 *
5bacd5cd
JR
4713 * On successful return the caller must complete the operation by calling
4714 * add_flow_start(), and if that succeeds, then either add_flow_finish(), or
4715 * add_flow_revert() if the operation needs to be reverted due to a later
4716 * failure.
4717 */
90bf1e07 4718static enum ofperr
5bacd5cd
JR
4719add_flow_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
4720 const struct ofputil_flow_mod *fm)
4721 OVS_EXCLUDED(ofproto_mutex)
064af421 4722{
d0918789 4723 struct oftable *table;
8037acb4 4724 struct cls_rule cr;
5a361239 4725 uint8_t table_id;
39c94593 4726 enum ofperr error;
064af421 4727
554764d1 4728 if (!check_table_id(ofproto, fm->table_id)) {
5bacd5cd 4729 return OFPERR_OFPBRC_BAD_TABLE_ID;
48266274
BP
4730 }
4731
7ee20df1
BP
4732 /* Pick table. */
4733 if (fm->table_id == 0xff) {
13521ff5 4734 if (ofproto->ofproto_class->rule_choose_table) {
81a76618
BP
4735 error = ofproto->ofproto_class->rule_choose_table(ofproto,
4736 &fm->match,
7ee20df1
BP
4737 &table_id);
4738 if (error) {
4739 return error;
4740 }
cb22974d 4741 ovs_assert(table_id < ofproto->n_tables);
7ee20df1 4742 } else {
5a361239 4743 table_id = 0;
7ee20df1
BP
4744 }
4745 } else if (fm->table_id < ofproto->n_tables) {
5a361239 4746 table_id = fm->table_id;
7ee20df1 4747 } else {
c22c56bd 4748 return OFPERR_OFPBRC_BAD_TABLE_ID;
064af421
BP
4749 }
4750
5a361239 4751 table = &ofproto->tables[table_id];
834fe5cb
BP
4752 if (table->flags & OFTABLE_READONLY
4753 && !(fm->flags & OFPUTIL_FF_NO_READONLY)) {
5c67e4af
BP
4754 return OFPERR_OFPBRC_EPERM;
4755 }
4756
c84d8691
JR
4757 if (!(fm->flags & OFPUTIL_FF_HIDDEN_FIELDS)
4758 && !match_has_default_hidden_fields(&fm->match)) {
4759 VLOG_WARN_RL(&rl, "%s: (add_flow) only internal flows can set "
4760 "non-default values to hidden fields", ofproto->name);
4761 return OFPERR_OFPBRC_EPERM;
adcf00ba
AZ
4762 }
4763
2c7ee524
JR
4764 if (!ofm->temp_rule) {
4765 cls_rule_init(&cr, &fm->match, fm->priority);
8037acb4 4766
2c7ee524
JR
4767 /* Allocate new rule. Destroys 'cr'. */
4768 error = ofproto_rule_create(ofproto, &cr, table - ofproto->tables,
4769 fm->new_cookie, fm->idle_timeout,
4770 fm->hard_timeout, fm->flags,
4771 fm->importance, fm->ofpacts,
5c7c16d8
YHW
4772 fm->ofpacts_len,
4773 fm->match.flow.tunnel.metadata.present.map,
4774 fm->ofpacts_tlv_bitmap, &ofm->temp_rule);
2c7ee524
JR
4775 if (error) {
4776 return error;
4777 }
5bacd5cd 4778
2c7ee524
JR
4779 get_conjunctions(fm, &ofm->conjs, &ofm->n_conjs);
4780 }
5bacd5cd
JR
4781 return 0;
4782}
4783
2c7ee524 4784/* ofm->temp_rule is consumed only in the successful case. */
5bacd5cd
JR
4785static enum ofperr
4786add_flow_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
4787 OVS_REQUIRES(ofproto_mutex)
4788{
4789 struct rule *old_rule = NULL;
4790 struct rule *new_rule = ofm->temp_rule;
4791 const struct rule_actions *actions = rule_get_actions(new_rule);
4792 struct oftable *table = &ofproto->tables[new_rule->table_id];
4793 enum ofperr error;
4794
4795 /* Must check actions while holding ofproto_mutex to avoid a race. */
4796 error = ofproto_check_ofpacts(ofproto, actions->ofpacts,
4797 actions->ofpacts_len);
4798 if (error) {
4799 return error;
4800 }
4801
1f42be1c 4802 /* Check for the existence of an identical rule.
2b7b1427 4803 * This will not return rules earlier marked for removal. */
5bacd5cd
JR
4804 old_rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls,
4805 &new_rule->cr,
4806 ofm->version));
4807 if (!old_rule) {
c84d8691 4808 /* Check for overlap, if requested. */
5bacd5cd
JR
4809 if (new_rule->flags & OFPUTIL_FF_CHECK_OVERLAP
4810 && classifier_rule_overlaps(&table->cls, &new_rule->cr,
4811 ofm->version)) {
c84d8691 4812 return OFPERR_OFPFMFC_OVERLAP;
dd27be82 4813 }
b277c7cc 4814
c84d8691 4815 /* If necessary, evict an existing rule to clear out space. */
6787a49f 4816 if (table->n_flows >= table->max_flows) {
5bacd5cd
JR
4817 if (!choose_rule_to_evict(table, &old_rule)) {
4818 return OFPERR_OFPFMFC_TABLE_FULL;
6787a49f 4819 }
5bacd5cd
JR
4820 eviction_group_remove_rule(old_rule);
4821 /* Marks 'old_rule' as an evicted rule rather than replaced rule.
6787a49f 4822 */
5bacd5cd 4823 old_rule->removed_reason = OFPRR_EVICTION;
c09f755d 4824 }
39c94593 4825 } else {
7338102b 4826 ofm->modify_cookie = true;
39c94593 4827 }
81a76618 4828
5bacd5cd
JR
4829 if (old_rule) {
4830 rule_collection_add(&ofm->old_rules, old_rule);
064af421 4831 }
5bacd5cd 4832 /* Take ownership of the temp_rule. */
58026109 4833 rule_collection_add(&ofm->new_rules, new_rule);
5bacd5cd 4834 ofm->temp_rule = NULL;
8037acb4 4835
5bacd5cd 4836 replace_rule_start(ofproto, ofm, old_rule, new_rule);
c84d8691
JR
4837 return 0;
4838}
1ebeaaa7 4839
6787a49f 4840/* Revert the effects of add_flow_start(). */
1f42be1c 4841static void
8be00367 4842add_flow_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
4843 OVS_REQUIRES(ofproto_mutex)
4844{
5bacd5cd 4845 struct rule *old_rule = rule_collection_n(&ofm->old_rules)
58026109
JR
4846 ? rule_collection_rules(&ofm->old_rules)[0] : NULL;
4847 struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0];
8be00367 4848
39c94593 4849 replace_rule_revert(ofproto, old_rule, new_rule);
1f42be1c
JR
4850}
4851
39c94593 4852/* To be called after version bump. */
c84d8691 4853static void
8be00367 4854add_flow_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 4855 const struct openflow_mod_requester *req)
c84d8691
JR
4856 OVS_REQUIRES(ofproto_mutex)
4857{
5bacd5cd 4858 struct rule *old_rule = rule_collection_n(&ofm->old_rules)
58026109
JR
4859 ? rule_collection_rules(&ofm->old_rules)[0] : NULL;
4860 struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0];
39c94593 4861 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
8037acb4 4862
81dee635 4863 replace_rule_finish(ofproto, ofm, req, old_rule, new_rule, &dead_cookies);
39c94593 4864 learned_cookies_flush(ofproto, &dead_cookies);
802f84ff 4865
39c94593
JR
4866 if (old_rule) {
4867 ovsrcu_postpone(remove_rule_rcu, old_rule);
4868 } else {
39c94593 4869 ofmonitor_report(ofproto->connmgr, new_rule, NXFME_ADDED, 0,
c84d8691
JR
4870 req ? req->ofconn : NULL,
4871 req ? req->request->xid : 0, NULL);
6c6eedc5
SJ
4872
4873 /* Send Vacancy Events for OF1.4+. */
4874 send_table_status(ofproto, new_rule->table_id);
c84d8691 4875 }
c84d8691 4876}
79eee1eb
BP
4877\f
4878/* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
064af421 4879
5bacd5cd 4880/* Create a new rule. Note that the rule is NOT inserted into a any data
2c7ee524
JR
4881 * structures yet. Takes ownership of 'cr'. Only assigns '*new_rule' if
4882 * successful. */
90bf1e07 4883static enum ofperr
5bacd5cd
JR
4884ofproto_rule_create(struct ofproto *ofproto, struct cls_rule *cr,
4885 uint8_t table_id, ovs_be64 new_cookie,
4886 uint16_t idle_timeout, uint16_t hard_timeout,
4887 enum ofputil_flow_mod_flags flags, uint16_t importance,
4888 const struct ofpact *ofpacts, size_t ofpacts_len,
5c7c16d8 4889 uint64_t match_tlv_bitmap, uint64_t ofpacts_tlv_bitmap,
5bacd5cd
JR
4890 struct rule **new_rule)
4891 OVS_NO_THREAD_SAFETY_ANALYSIS
79eee1eb 4892{
39c94593 4893 struct rule *rule;
dd27be82 4894 enum ofperr error;
79eee1eb 4895
39c94593
JR
4896 /* Allocate new rule. */
4897 rule = ofproto->ofproto_class->rule_alloc();
4898 if (!rule) {
4899 cls_rule_destroy(cr);
4900 VLOG_WARN_RL(&rl, "%s: failed to allocate a rule.", ofproto->name);
4901 return OFPERR_OFPFMFC_UNKNOWN;
af822017
BP
4902 }
4903
39c94593
JR
4904 /* Initialize base state. */
4905 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4906 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), cr);
4907 ovs_refcount_init(&rule->ref_count);
834fe5cb 4908
39c94593
JR
4909 ovs_mutex_init(&rule->mutex);
4910 ovs_mutex_lock(&rule->mutex);
82a3160e 4911 *CONST_CAST(ovs_be64 *, &rule->flow_cookie) = new_cookie;
5bacd5cd
JR
4912 rule->created = rule->modified = time_msec();
4913 rule->idle_timeout = idle_timeout;
4914 rule->hard_timeout = hard_timeout;
4915 *CONST_CAST(uint16_t *, &rule->importance) = importance;
f695ebfa 4916 rule->removed_reason = OVS_OFPRR_NONE;
834fe5cb 4917
39c94593 4918 *CONST_CAST(uint8_t *, &rule->table_id) = table_id;
5bacd5cd
JR
4919 rule->flags = flags & OFPUTIL_FF_STATE;
4920
39c94593 4921 *CONST_CAST(const struct rule_actions **, &rule->actions)
5bacd5cd
JR
4922 = rule_actions_create(ofpacts, ofpacts_len);
4923
417e7e66 4924 ovs_list_init(&rule->meter_list_node);
39c94593 4925 rule->eviction_group = NULL;
39c94593
JR
4926 rule->monitor_flags = 0;
4927 rule->add_seqno = 0;
4928 rule->modify_seqno = 0;
5bacd5cd 4929 ovs_list_init(&rule->expirable);
dd27be82
JR
4930 ovs_mutex_unlock(&rule->mutex);
4931
39c94593
JR
4932 /* Construct rule, initializing derived state. */
4933 error = ofproto->ofproto_class->rule_construct(rule);
4934 if (error) {
4935 ofproto_rule_destroy__(rule);
4936 return error;
dd27be82 4937 }
b277c7cc 4938
30d0452c 4939 rule->state = RULE_INITIALIZED;
5c7c16d8
YHW
4940 rule->match_tlv_bitmap = match_tlv_bitmap;
4941 rule->ofpacts_tlv_bitmap = ofpacts_tlv_bitmap;
4942 mf_vl_mff_ref(&rule->ofproto->vl_mff_map, match_tlv_bitmap);
4943 mf_vl_mff_ref(&rule->ofproto->vl_mff_map, ofpacts_tlv_bitmap);
dd27be82 4944
39c94593
JR
4945 *new_rule = rule;
4946 return 0;
4947}
884e1dc4 4948
2c7ee524
JR
4949/* Initialize 'ofm' for a learn action. If the rule already existed, reference
4950 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
4951 * rule reference in both. This does not take the global 'ofproto_mutex'. */
4952enum ofperr
4953ofproto_flow_mod_init_for_learn(struct ofproto *ofproto,
4954 const struct ofputil_flow_mod *fm,
4955 struct ofproto_flow_mod *ofm)
4956 OVS_EXCLUDED(ofproto_mutex)
4957{
4958 enum ofperr error;
4959
4960 /* Reject flow mods that do not look like they were generated by a learn
4961 * action. */
4962 if (fm->command != OFPFC_MODIFY_STRICT || fm->table_id == OFPTT_ALL
4963 || fm->flags & OFPUTIL_FF_RESET_COUNTS
4964 || fm->buffer_id != UINT32_MAX) {
4965 return OFPERR_OFPFMFC_UNKNOWN;
4966 }
4967
4968 /* Check if the rule already exists, and we can get a reference to it. */
4969 struct oftable *table = &ofproto->tables[fm->table_id];
4970 struct rule *rule;
4971
4972 rule = rule_from_cls_rule(classifier_find_match_exactly(
4973 &table->cls, &fm->match, fm->priority,
4974 OVS_VERSION_MAX));
4975 if (rule) {
4976 /* Check if the rule's attributes match as well. */
4977 const struct rule_actions *actions;
4978
4979 ovs_mutex_lock(&rule->mutex);
4980 actions = rule_get_actions(rule);
4981 if (rule->idle_timeout == fm->idle_timeout
4982 && rule->hard_timeout == fm->hard_timeout
4983 && rule->importance == fm->importance
4984 && rule->flags == (fm->flags & OFPUTIL_FF_STATE)
4985 && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie))
4986 && ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
4987 actions->ofpacts, actions->ofpacts_len)) {
4988 /* Rule already exists and need not change, except for the modified
4989 * timestamp. Get a reference to the existing rule. */
4990 ovs_mutex_unlock(&rule->mutex);
4991 if (!ofproto_rule_try_ref(rule)) {
4992 rule = NULL; /* Pretend it did not exist. */
4993 }
4994 } else {
4995 ovs_mutex_unlock(&rule->mutex);
4996 rule = NULL;
4997 }
4998 }
4999
5000 /* Initialize ofproto_flow_mod for future use. */
5001 error = ofproto_flow_mod_init(ofproto, ofm, fm, rule);
5002 if (error) {
5003 ofproto_rule_unref(rule);
5004 return error;
5005 }
5006 return 0;
5007}
5008
2c7ee524 5009enum ofperr
1f4a8933 5010ofproto_flow_mod_learn_refresh(struct ofproto_flow_mod *ofm)
2c7ee524
JR
5011{
5012 enum ofperr error = 0;
5013
5014 /* ofm->temp_rule is our reference to the learned rule. We have a
5015 * reference to an existing rule, if it already was in the classifier,
5016 * otherwise we may have a fresh rule that we need to insert. */
5017 struct rule *rule = ofm->temp_rule;
5018 if (!rule) {
5019 return OFPERR_OFPFMFC_UNKNOWN;
5020 }
2c7ee524
JR
5021
5022 /* Create a new rule if the current one has been removed from the
5023 * classifier. We need to do this since RCU does not allow a current rule
5024 * to be reinserted before all threads have quiesced.
5025 *
5026 * It is possible that the rule is removed asynchronously, e.g., right
1f4a8933 5027 * after we have read the 'rule->state' below. In this case the next time
2c7ee524 5028 * this function is executed the rule will be reinstated. */
1f4a8933 5029 if (rule->state == RULE_REMOVED) {
2c7ee524
JR
5030 struct cls_rule cr;
5031
5032 cls_rule_clone(&cr, &rule->cr);
5033 ovs_mutex_lock(&rule->mutex);
1f4a8933 5034 error = ofproto_rule_create(rule->ofproto, &cr, rule->table_id,
2c7ee524
JR
5035 rule->flow_cookie,
5036 rule->idle_timeout,
5037 rule->hard_timeout, rule->flags,
5038 rule->importance,
5039 rule->actions->ofpacts,
5040 rule->actions->ofpacts_len,
5c7c16d8
YHW
5041 rule->match_tlv_bitmap,
5042 rule->ofpacts_tlv_bitmap,
2c7ee524
JR
5043 &ofm->temp_rule);
5044 ovs_mutex_unlock(&rule->mutex);
2c7ee524 5045 if (!error) {
1f4a8933 5046 ofproto_rule_unref(rule); /* Release old reference. */
2c7ee524 5047 }
2c7ee524
JR
5048 } else {
5049 /* Refresh the existing rule. */
5050 ovs_mutex_lock(&rule->mutex);
5051 rule->modified = time_msec();
5052 ovs_mutex_unlock(&rule->mutex);
5053 }
1f4a8933
JR
5054 return error;
5055}
5056
5057enum ofperr
5058ofproto_flow_mod_learn_start(struct ofproto_flow_mod *ofm)
5059 OVS_REQUIRES(ofproto_mutex)
5060{
5061 struct rule *rule = ofm->temp_rule;
5062
5063 /* ofproto_flow_mod_start() consumes the reference, so we
5064 * take a new one. */
5065 ofproto_rule_ref(rule);
5066 enum ofperr error = ofproto_flow_mod_start(rule->ofproto, ofm);
5067 ofm->temp_rule = rule;
5068
5069 return error;
5070}
5071
6dd3c787
JR
5072void
5073ofproto_flow_mod_learn_revert(struct ofproto_flow_mod *ofm)
5074 OVS_REQUIRES(ofproto_mutex)
5075{
5076 struct rule *rule = rule_collection_rules(&ofm->new_rules)[0];
5077 ofproto_flow_mod_revert(rule->ofproto, ofm);
5078}
5079
1f4a8933
JR
5080void
5081ofproto_flow_mod_learn_finish(struct ofproto_flow_mod *ofm,
5082 struct ofproto *orig_ofproto)
5083 OVS_REQUIRES(ofproto_mutex)
5084{
5085 struct rule *rule = rule_collection_rules(&ofm->new_rules)[0];
5086
5087 /* If learning on a different bridge, must bump its version
5088 * number and flush connmgr afterwards. */
5089 if (rule->ofproto != orig_ofproto) {
5090 ofproto_bump_tables_version(rule->ofproto);
5091 }
5092 ofproto_flow_mod_finish(rule->ofproto, ofm, NULL);
5093 if (rule->ofproto != orig_ofproto) {
5094 ofmonitor_flush(rule->ofproto->connmgr);
5095 }
5096}
5097
5098/* Refresh 'ofm->temp_rule', for which the caller holds a reference, if already
5099 * in the classifier, insert it otherwise. If the rule has already been
5100 * removed from the classifier, a new rule is created using 'ofm->temp_rule' as
5101 * a template and the reference to the old 'ofm->temp_rule' is freed. If
5102 * 'keep_ref' is true, then a reference to the current rule is held, otherwise
5103 * it is released and 'ofm->temp_rule' is set to NULL.
5104 *
4c71600d
DDP
5105 * If 'limit' != 0, insertion will fail if there are more than 'limit' rules
5106 * in the same table with the same cookie. If insertion succeeds,
86b7637c
JS
5107 * '*below_limitp' will be set to true. If insertion fails '*below_limitp'
5108 * will be set to false.
4c71600d 5109 *
1f4a8933
JR
5110 * Caller needs to be the exclusive owner of 'ofm' as it is being manipulated
5111 * during the call. */
5112enum ofperr
4c71600d 5113ofproto_flow_mod_learn(struct ofproto_flow_mod *ofm, bool keep_ref,
86b7637c 5114 unsigned limit, bool *below_limitp)
1f4a8933
JR
5115 OVS_EXCLUDED(ofproto_mutex)
5116{
5117 enum ofperr error = ofproto_flow_mod_learn_refresh(ofm);
5118 struct rule *rule = ofm->temp_rule;
86b7637c 5119 bool below_limit = true;
1f4a8933
JR
5120
5121 /* Do we need to insert the rule? */
5122 if (!error && rule->state == RULE_INITIALIZED) {
5123 ovs_mutex_lock(&ofproto_mutex);
4c71600d
DDP
5124
5125 if (limit) {
5126 struct rule_criteria criteria;
5127 struct rule_collection rules;
5128 struct match match;
5129
5130 match_init_catchall(&match);
5131 rule_criteria_init(&criteria, rule->table_id, &match, 0,
5132 OVS_VERSION_MAX, rule->flow_cookie,
5133 OVS_BE64_MAX, OFPP_ANY, OFPG_ANY);
5134 rule_criteria_require_rw(&criteria, false);
5135 collect_rules_loose(rule->ofproto, &criteria, &rules);
5136 if (rule_collection_n(&rules) >= limit) {
86b7637c 5137 below_limit = false;
4c71600d
DDP
5138 }
5139 rule_collection_destroy(&rules);
5140 rule_criteria_destroy(&criteria);
5141 }
5142
86b7637c 5143 if (below_limit) {
4c71600d
DDP
5144 ofm->version = rule->ofproto->tables_version + 1;
5145
5146 error = ofproto_flow_mod_learn_start(ofm);
5147 if (!error) {
5148 ofproto_flow_mod_learn_finish(ofm, NULL);
5149 }
5150 } else {
5151 ofproto_flow_mod_uninit(ofm);
1f4a8933
JR
5152 }
5153 ovs_mutex_unlock(&ofproto_mutex);
3d8e7354 5154
86b7637c 5155 if (!below_limit) {
3d8e7354
JS
5156 static struct vlog_rate_limit learn_rl = VLOG_RATE_LIMIT_INIT(1, 5);
5157 VLOG_INFO_RL(&learn_rl, "Learn limit for flow %"PRIu64" reached.",
5158 rule->flow_cookie);
5159 }
1f4a8933 5160 }
2c7ee524 5161
86b7637c 5162 if (!keep_ref && below_limit) {
2c7ee524
JR
5163 ofproto_rule_unref(rule);
5164 ofm->temp_rule = NULL;
5165 }
86b7637c
JS
5166 if (below_limitp) {
5167 *below_limitp = below_limit;
4c71600d 5168 }
2c7ee524
JR
5169 return error;
5170}
5171
39c94593 5172static void
5bacd5cd
JR
5173replace_rule_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
5174 struct rule *old_rule, struct rule *new_rule)
39c94593
JR
5175{
5176 struct oftable *table = &ofproto->tables[new_rule->table_id];
834fe5cb 5177
6787a49f 5178 /* 'old_rule' may be either an evicted rule or replaced rule. */
39c94593 5179 if (old_rule) {
5bacd5cd
JR
5180 /* Copy values from old rule for modify semantics. */
5181 if (old_rule->removed_reason != OFPRR_EVICTION) {
5182 bool change_cookie = (ofm->modify_cookie
5183 && new_rule->flow_cookie != OVS_BE64_MAX
5184 && new_rule->flow_cookie != old_rule->flow_cookie);
5185
5186 ovs_mutex_lock(&new_rule->mutex);
5187 ovs_mutex_lock(&old_rule->mutex);
5188 if (ofm->command != OFPFC_ADD) {
5189 new_rule->idle_timeout = old_rule->idle_timeout;
5190 new_rule->hard_timeout = old_rule->hard_timeout;
5191 *CONST_CAST(uint16_t *, &new_rule->importance) = old_rule->importance;
5192 new_rule->flags = old_rule->flags;
5193 new_rule->created = old_rule->created;
5194 }
5195 if (!change_cookie) {
82a3160e
BP
5196 *CONST_CAST(ovs_be64 *, &new_rule->flow_cookie)
5197 = old_rule->flow_cookie;
5bacd5cd
JR
5198 }
5199 ovs_mutex_unlock(&old_rule->mutex);
5200 ovs_mutex_unlock(&new_rule->mutex);
5201 }
5202
39c94593 5203 /* Mark the old rule for removal in the next version. */
5bacd5cd 5204 cls_rule_make_invisible_in_version(&old_rule->cr, ofm->version);
9bab38ff
JR
5205
5206 /* Remove the old rule from data structures. */
5207 ofproto_rule_remove__(ofproto, old_rule);
d79e3d70
JR
5208 } else {
5209 table->n_flows++;
dd27be82 5210 }
cc099268
JR
5211 /* Insert flow to ofproto data structures, so that later flow_mods may
5212 * relate to it. This is reversible, in case later errors require this to
39c94593
JR
5213 * be reverted. */
5214 ofproto_rule_insert__(ofproto, new_rule);
5215 /* Make the new rule visible for classifier lookups only from the next
5216 * version. */
5bacd5cd
JR
5217 classifier_insert(&table->cls, &new_rule->cr, ofm->version, ofm->conjs,
5218 ofm->n_conjs);
39c94593
JR
5219}
5220
cc099268
JR
5221static void
5222replace_rule_revert(struct ofproto *ofproto,
5223 struct rule *old_rule, struct rule *new_rule)
39c94593
JR
5224{
5225 struct oftable *table = &ofproto->tables[new_rule->table_id];
35f48b8b 5226
39c94593 5227 if (old_rule) {
5bacd5cd
JR
5228 if (old_rule->removed_reason == OFPRR_EVICTION) {
5229 /* Revert the eviction. */
5230 eviction_group_add_rule(old_rule);
5231 }
5232
9bab38ff
JR
5233 /* Restore the old rule to data structures. */
5234 ofproto_rule_insert__(ofproto, old_rule);
5235
d79e3d70 5236 /* Restore the original visibility of the old rule. */
39c94593 5237 cls_rule_restore_visibility(&old_rule->cr);
d79e3d70
JR
5238 } else {
5239 /* Restore table's rule count. */
5240 table->n_flows--;
834fe5cb
BP
5241 }
5242
39c94593
JR
5243 /* Remove the new rule immediately. It was never visible to lookups. */
5244 if (!classifier_remove(&table->cls, &new_rule->cr)) {
5245 OVS_NOT_REACHED();
5ecc9d81 5246 }
39c94593 5247 ofproto_rule_remove__(ofproto, new_rule);
39c94593 5248 ofproto_rule_unref(new_rule);
dd27be82 5249}
79eee1eb 5250
39c94593 5251/* Adds the 'new_rule', replacing the 'old_rule'. */
dd27be82 5252static void
81dee635 5253replace_rule_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5254 const struct openflow_mod_requester *req,
39c94593
JR
5255 struct rule *old_rule, struct rule *new_rule,
5256 struct ovs_list *dead_cookies)
dd27be82
JR
5257 OVS_REQUIRES(ofproto_mutex)
5258{
6787a49f
JR
5259 struct rule *replaced_rule;
5260
5aacc3e2
JR
5261 replaced_rule = (old_rule && old_rule->removed_reason != OFPRR_EVICTION)
5262 ? old_rule : NULL;
dd27be82 5263
6787a49f
JR
5264 /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule'
5265 * is a duplicate rule the 'new_rule' is replacing. The provider should
748eb2f5 5266 * link the packet and byte counts from the old rule to the new one if
8b6987d7
JR
5267 * 'modify_keep_counts' is 'true'. The 'replaced_rule' will be deleted
5268 * right after this call. */
6787a49f 5269 ofproto->ofproto_class->rule_insert(new_rule, replaced_rule,
8b6987d7 5270 ofm->modify_keep_counts);
39c94593
JR
5271 learned_cookies_inc(ofproto, rule_get_actions(new_rule));
5272
5273 if (old_rule) {
5274 const struct rule_actions *old_actions = rule_get_actions(old_rule);
81dee635 5275 const struct rule_actions *new_actions = rule_get_actions(new_rule);
39c94593 5276
39c94593
JR
5277 learned_cookies_dec(ofproto, old_actions, dead_cookies);
5278
6787a49f 5279 if (replaced_rule) {
5bacd5cd
JR
5280 enum nx_flow_update_event event = ofm->command == OFPFC_ADD
5281 ? NXFME_ADDED : NXFME_MODIFIED;
5282
81dee635
JR
5283 bool changed_cookie = (new_rule->flow_cookie
5284 != old_rule->flow_cookie);
6787a49f 5285
81dee635
JR
5286 bool changed_actions = !ofpacts_equal(new_actions->ofpacts,
5287 new_actions->ofpacts_len,
5288 old_actions->ofpacts,
5289 old_actions->ofpacts_len);
6787a49f 5290
5bacd5cd 5291 if (event != NXFME_MODIFIED || changed_actions
81dee635 5292 || changed_cookie) {
5bacd5cd 5293 ofmonitor_report(ofproto->connmgr, new_rule, event, 0,
6787a49f
JR
5294 req ? req->ofconn : NULL,
5295 req ? req->request->xid : 0,
81dee635 5296 changed_actions ? old_actions : NULL);
6787a49f
JR
5297 }
5298 } else {
5299 /* XXX: This is slight duplication with delete_flows_finish__() */
6787a49f
JR
5300 ofmonitor_report(ofproto->connmgr, old_rule, NXFME_DELETED,
5301 OFPRR_EVICTION,
39c94593 5302 req ? req->ofconn : NULL,
6787a49f 5303 req ? req->request->xid : 0, NULL);
39c94593 5304 }
dd27be82 5305 }
9ed18e46
BP
5306}
5307
2c7ee524 5308/* ofm->temp_rule is consumed only in the successful case. */
9387b970 5309static enum ofperr
8be00367 5310modify_flows_start__(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
dd27be82
JR
5311 OVS_REQUIRES(ofproto_mutex)
5312{
8be00367
JR
5313 struct rule_collection *old_rules = &ofm->old_rules;
5314 struct rule_collection *new_rules = &ofm->new_rules;
dd27be82
JR
5315 enum ofperr error;
5316
fe59694b 5317 if (rule_collection_n(old_rules) > 0) {
39c94593 5318 /* Create a new 'modified' rule for each old rule. */
5bacd5cd
JR
5319 struct rule *old_rule, *new_rule;
5320 const struct rule_actions *actions = rule_get_actions(ofm->temp_rule);
39c94593 5321
5bacd5cd
JR
5322 /* Must check actions while holding ofproto_mutex to avoid a race. */
5323 error = ofproto_check_ofpacts(ofproto, actions->ofpacts,
5324 actions->ofpacts_len);
5325 if (error) {
5326 return error;
5327 }
5328
5329 /* Use the temp rule as the first new rule, and as the template for
5330 * the rest. */
5331 struct rule *temp = ofm->temp_rule;
5332 ofm->temp_rule = NULL; /* We consume the template. */
5333
5334 bool first = true;
5335 RULE_COLLECTION_FOR_EACH (old_rule, old_rules) {
5336 if (first) {
5337 /* The template rule's match is possibly a loose one, so it
5338 * must be replaced with the old rule's match so that the new
5339 * rule actually replaces the old one. */
5340 cls_rule_destroy(CONST_CAST(struct cls_rule *, &temp->cr));
5341 cls_rule_clone(CONST_CAST(struct cls_rule *, &temp->cr),
5342 &old_rule->cr);
5c7c16d8
YHW
5343 if (temp->match_tlv_bitmap != old_rule->match_tlv_bitmap) {
5344 mf_vl_mff_unref(&temp->ofproto->vl_mff_map,
5345 temp->match_tlv_bitmap);
5346 temp->match_tlv_bitmap = old_rule->match_tlv_bitmap;
5347 mf_vl_mff_ref(&temp->ofproto->vl_mff_map,
5348 temp->match_tlv_bitmap);
5349 }
5bacd5cd
JR
5350 *CONST_CAST(uint8_t *, &temp->table_id) = old_rule->table_id;
5351 rule_collection_add(new_rules, temp);
5352 first = false;
39c94593 5353 } else {
5bacd5cd
JR
5354 struct cls_rule cr;
5355 cls_rule_clone(&cr, &old_rule->cr);
5356 error = ofproto_rule_create(ofproto, &cr, old_rule->table_id,
5357 temp->flow_cookie,
5358 temp->idle_timeout,
5359 temp->hard_timeout, temp->flags,
5360 temp->importance,
5361 temp->actions->ofpacts,
5362 temp->actions->ofpacts_len,
5c7c16d8
YHW
5363 old_rule->match_tlv_bitmap,
5364 temp->ofpacts_tlv_bitmap,
5bacd5cd
JR
5365 &new_rule);
5366 if (!error) {
5367 rule_collection_add(new_rules, new_rule);
5368 } else {
2c7ee524
JR
5369 /* Return the template rule in place in the error case. */
5370 ofm->temp_rule = temp;
5371 rule_collection_rules(new_rules)[0] = NULL;
5372
5bacd5cd
JR
5373 rule_collection_unref(new_rules);
5374 rule_collection_destroy(new_rules);
5375 return error;
5376 }
39c94593
JR
5377 }
5378 }
fe59694b
JR
5379 ovs_assert(rule_collection_n(new_rules)
5380 == rule_collection_n(old_rules));
39c94593 5381
fe59694b 5382 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
5bacd5cd 5383 replace_rule_start(ofproto, ofm, old_rule, new_rule);
39c94593 5384 }
81dee635 5385 } else if (ofm->modify_may_add_flow) {
5bacd5cd 5386 /* No match, add a new flow, consumes 'temp'. */
8be00367 5387 error = add_flow_start(ofproto, ofm);
dd27be82 5388 } else {
2c7ee524
JR
5389 /* No flow to modify and may not add a flow. */
5390 ofproto_rule_unref(ofm->temp_rule);
5391 ofm->temp_rule = NULL; /* We consume the template. */
d99f64d7 5392 error = 0;
dd27be82 5393 }
39c94593 5394
dd27be82
JR
5395 return error;
5396}
5397
5bacd5cd
JR
5398static enum ofperr
5399modify_flows_init_loose(struct ofproto *ofproto,
5400 struct ofproto_flow_mod *ofm,
5401 const struct ofputil_flow_mod *fm)
5402 OVS_EXCLUDED(ofproto_mutex)
5403{
5404 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0,
5405 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY,
5406 OFPG_ANY);
5407 rule_criteria_require_rw(&ofm->criteria,
5408 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5409 /* Must create a new flow in advance for the case that no matches are
5410 * found. Also used for template for multiple modified flows. */
5411 add_flow_init(ofproto, ofm, fm);
5412
5413 return 0;
5414}
5415
90bf1e07 5416/* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
c184807c 5417 * failure. */
90bf1e07 5418static enum ofperr
8be00367 5419modify_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5420 OVS_REQUIRES(ofproto_mutex)
9ed18e46 5421{
8be00367 5422 struct rule_collection *old_rules = &ofm->old_rules;
d51c8b71 5423 enum ofperr error;
9ed18e46 5424
5bacd5cd 5425 error = collect_rules_loose(ofproto, &ofm->criteria, old_rules);
a9b22b7f 5426
a8e547c1 5427 if (!error) {
8be00367 5428 error = modify_flows_start__(ofproto, ofm);
d99f64d7
JR
5429 }
5430
5431 if (error) {
39c94593 5432 rule_collection_destroy(old_rules);
d99f64d7 5433 }
5bacd5cd 5434
d99f64d7
JR
5435 return error;
5436}
5437
1f42be1c 5438static void
8be00367 5439modify_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
5440 OVS_REQUIRES(ofproto_mutex)
5441{
8be00367
JR
5442 struct rule_collection *old_rules = &ofm->old_rules;
5443 struct rule_collection *new_rules = &ofm->new_rules;
5444
39c94593 5445 /* Old rules were not changed yet, only need to revert new rules. */
5bacd5cd 5446 if (rule_collection_n(old_rules) > 0) {
fe59694b
JR
5447 struct rule *old_rule, *new_rule;
5448 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
5449 replace_rule_revert(ofproto, old_rule, new_rule);
39c94593
JR
5450 }
5451 rule_collection_destroy(new_rules);
5452 rule_collection_destroy(old_rules);
1f42be1c 5453 }
1f42be1c
JR
5454}
5455
d99f64d7 5456static void
8be00367 5457modify_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5458 const struct openflow_mod_requester *req)
d99f64d7
JR
5459 OVS_REQUIRES(ofproto_mutex)
5460{
8be00367
JR
5461 struct rule_collection *old_rules = &ofm->old_rules;
5462 struct rule_collection *new_rules = &ofm->new_rules;
5463
fe59694b
JR
5464 if (rule_collection_n(old_rules) == 0
5465 && rule_collection_n(new_rules) == 1) {
8be00367 5466 add_flow_finish(ofproto, ofm, req);
fe59694b 5467 } else if (rule_collection_n(old_rules) > 0) {
39c94593
JR
5468 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
5469
fe59694b
JR
5470 ovs_assert(rule_collection_n(new_rules)
5471 == rule_collection_n(old_rules));
39c94593 5472
fe59694b
JR
5473 struct rule *old_rule, *new_rule;
5474 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
81dee635 5475 replace_rule_finish(ofproto, ofm, req, old_rule, new_rule,
fe59694b 5476 &dead_cookies);
39c94593
JR
5477 }
5478 learned_cookies_flush(ofproto, &dead_cookies);
cc099268 5479 remove_rules_postponed(old_rules);
d99f64d7 5480 }
d99f64d7
JR
5481}
5482
5bacd5cd
JR
5483static enum ofperr
5484modify_flow_init_strict(struct ofproto *ofproto OVS_UNUSED,
5485 struct ofproto_flow_mod *ofm,
5486 const struct ofputil_flow_mod *fm)
5487 OVS_EXCLUDED(ofproto_mutex)
5488{
5489 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority,
5490 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY,
5491 OFPG_ANY);
5492 rule_criteria_require_rw(&ofm->criteria,
5493 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5494 /* Must create a new flow in advance for the case that no matches are
5495 * found. Also used for template for multiple modified flows. */
5496 add_flow_init(ofproto, ofm, fm);
5497
5498 return 0;
5499}
5500
79eee1eb 5501/* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
baae3d02 5502 * code on failure. */
90bf1e07 5503static enum ofperr
8be00367 5504modify_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5505 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5506{
8be00367 5507 struct rule_collection *old_rules = &ofm->old_rules;
d51c8b71 5508 enum ofperr error;
6c1491fb 5509
5bacd5cd 5510 error = collect_rules_strict(ofproto, &ofm->criteria, old_rules);
a9b22b7f 5511
a8e547c1 5512 if (!error) {
dd27be82 5513 /* collect_rules_strict() can return max 1 rule. */
8be00367 5514 error = modify_flows_start__(ofproto, ofm);
d99f64d7
JR
5515 }
5516
d99f64d7
JR
5517 return error;
5518}
9ed18e46
BP
5519\f
5520/* OFPFC_DELETE implementation. */
79eee1eb 5521
254750ce 5522static void
44e0c35d 5523delete_flows_start__(struct ofproto *ofproto, ovs_version_t version,
39c94593
JR
5524 const struct rule_collection *rules)
5525 OVS_REQUIRES(ofproto_mutex)
5526{
fe59694b
JR
5527 struct rule *rule;
5528
5529 RULE_COLLECTION_FOR_EACH (rule, rules) {
d79e3d70
JR
5530 struct oftable *table = &ofproto->tables[rule->table_id];
5531
5532 table->n_flows--;
8be00367 5533 cls_rule_make_invisible_in_version(&rule->cr, version);
9bab38ff
JR
5534
5535 /* Remove rule from ofproto data structures. */
5536 ofproto_rule_remove__(ofproto, rule);
39c94593
JR
5537 }
5538}
5539
25070e04
JR
5540static void
5541delete_flows_revert__(struct ofproto *ofproto,
5542 const struct rule_collection *rules)
5543 OVS_REQUIRES(ofproto_mutex)
5544{
5545 struct rule *rule;
5546
5547 RULE_COLLECTION_FOR_EACH (rule, rules) {
5548 struct oftable *table = &ofproto->tables[rule->table_id];
5549
5550 /* Add rule back to ofproto data structures. */
5551 ofproto_rule_insert__(ofproto, rule);
5552
5553 /* Restore table's rule count. */
5554 table->n_flows++;
5555
5556 /* Restore the original visibility of the rule. */
5557 cls_rule_restore_visibility(&rule->cr);
5558 }
5559}
5560
39c94593
JR
5561static void
5562delete_flows_finish__(struct ofproto *ofproto,
5563 struct rule_collection *rules,
5564 enum ofp_flow_removed_reason reason,
0a0d9385 5565 const struct openflow_mod_requester *req)
15aaf599 5566 OVS_REQUIRES(ofproto_mutex)
064af421 5567{
fe59694b 5568 if (rule_collection_n(rules)) {
55951e15 5569 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
fe59694b 5570 struct rule *rule;
79eee1eb 5571
fe59694b 5572 RULE_COLLECTION_FOR_EACH (rule, rules) {
f695ebfa
JR
5573 /* This value will be used to send the flow removed message right
5574 * before the rule is actually destroyed. */
5575 rule->removed_reason = reason;
5576
9ca4a86f 5577 ofmonitor_report(ofproto->connmgr, rule, NXFME_DELETED, reason,
c84d8691
JR
5578 req ? req->ofconn : NULL,
5579 req ? req->request->xid : 0, NULL);
6c6eedc5
SJ
5580
5581 /* Send Vacancy Event for OF1.4+. */
5582 send_table_status(ofproto, rule->table_id);
5583
802f84ff
JR
5584 learned_cookies_dec(ofproto, rule_get_actions(rule),
5585 &dead_cookies);
9ca4a86f 5586 }
cc099268 5587 remove_rules_postponed(rules);
39c94593 5588
35f48b8b 5589 learned_cookies_flush(ofproto, &dead_cookies);
39c94593
JR
5590 }
5591}
5592
5593/* Deletes the rules listed in 'rules'.
5594 * The deleted rules will become invisible to the lookups in the next version.
5595 * Destroys 'rules'. */
5596static void
5597delete_flows__(struct rule_collection *rules,
5598 enum ofp_flow_removed_reason reason,
0a0d9385 5599 const struct openflow_mod_requester *req)
39c94593
JR
5600 OVS_REQUIRES(ofproto_mutex)
5601{
fe59694b
JR
5602 if (rule_collection_n(rules)) {
5603 struct ofproto *ofproto = rule_collection_rules(rules)[0]->ofproto;
39c94593 5604
8be00367 5605 delete_flows_start__(ofproto, ofproto->tables_version + 1, rules);
39c94593
JR
5606 ofproto_bump_tables_version(ofproto);
5607 delete_flows_finish__(ofproto, rules, reason, req);
9ca4a86f
BP
5608 ofmonitor_flush(ofproto->connmgr);
5609 }
79eee1eb 5610}
79eee1eb 5611
5bacd5cd
JR
5612static enum ofperr
5613delete_flows_init_loose(struct ofproto *ofproto OVS_UNUSED,
5614 struct ofproto_flow_mod *ofm,
5615 const struct ofputil_flow_mod *fm)
5616 OVS_EXCLUDED(ofproto_mutex)
5617{
5618 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0,
5619 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask,
5620 fm->out_port, fm->out_group);
5621 rule_criteria_require_rw(&ofm->criteria,
5622 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5623 return 0;
5624}
5625
79eee1eb 5626/* Implements OFPFC_DELETE. */
90bf1e07 5627static enum ofperr
8be00367 5628delete_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5629 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5630{
8be00367 5631 struct rule_collection *rules = &ofm->old_rules;
90bf1e07 5632 enum ofperr error;
064af421 5633
5bacd5cd 5634 error = collect_rules_loose(ofproto, &ofm->criteria, rules);
a9b22b7f 5635
802f84ff 5636 if (!error) {
8be00367 5637 delete_flows_start__(ofproto, ofm->version, rules);
a8e547c1 5638 }
a8e547c1
BP
5639
5640 return error;
064af421
BP
5641}
5642
ce59413f 5643static void
8be00367 5644delete_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
ce59413f
JR
5645 OVS_REQUIRES(ofproto_mutex)
5646{
25070e04 5647 delete_flows_revert__(ofproto, &ofm->old_rules);
ce59413f
JR
5648}
5649
1f42be1c 5650static void
2c7ee524 5651delete_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5652 const struct openflow_mod_requester *req)
15aaf599 5653 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5654{
5aacc3e2 5655 delete_flows_finish__(ofproto, &ofm->old_rules, OFPRR_DELETE, req);
ce59413f
JR
5656}
5657
5bacd5cd
JR
5658static enum ofperr
5659delete_flows_init_strict(struct ofproto *ofproto OVS_UNUSED,
5660 struct ofproto_flow_mod *ofm,
5661 const struct ofputil_flow_mod *fm)
5662 OVS_EXCLUDED(ofproto_mutex)
5663{
5664 rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority,
5665 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask,
5666 fm->out_port, fm->out_group);
5667 rule_criteria_require_rw(&ofm->criteria,
5668 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
5669 return 0;
5670}
5671
ce59413f
JR
5672/* Implements OFPFC_DELETE_STRICT. */
5673static enum ofperr
2c7ee524 5674delete_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
ce59413f
JR
5675 OVS_REQUIRES(ofproto_mutex)
5676{
8be00367 5677 struct rule_collection *rules = &ofm->old_rules;
ce59413f
JR
5678 enum ofperr error;
5679
5bacd5cd 5680 error = collect_rules_strict(ofproto, &ofm->criteria, rules);
a9b22b7f 5681
802f84ff 5682 if (!error) {
8be00367 5683 delete_flows_start__(ofproto, ofm->version, rules);
ce59413f
JR
5684 }
5685
5686 return error;
5687}
5688
f695ebfa 5689/* This may only be called by rule_destroy_cb()! */
abe529af 5690static void
f695ebfa
JR
5691ofproto_rule_send_removed(struct rule *rule)
5692 OVS_EXCLUDED(ofproto_mutex)
abe529af
BP
5693{
5694 struct ofputil_flow_removed fr;
dc437090 5695 long long int used;
abe529af 5696
5cb7a798 5697 minimatch_expand(&rule->cr.match, &fr.match);
81a76618 5698 fr.priority = rule->cr.priority;
f695ebfa 5699
25010c68
JR
5700 /* Synchronize with connmgr_destroy() calls to prevent connmgr disappearing
5701 * while we use it. */
f695ebfa 5702 ovs_mutex_lock(&ofproto_mutex);
25010c68
JR
5703 struct connmgr *connmgr = rule->ofproto->connmgr;
5704 if (!connmgr) {
5705 ovs_mutex_unlock(&ofproto_mutex);
5706 return;
5707 }
5708
abe529af 5709 fr.cookie = rule->flow_cookie;
f695ebfa 5710 fr.reason = rule->removed_reason;
95216219 5711 fr.table_id = rule->table_id;
65e0be10
BP
5712 calc_duration(rule->created, time_msec(),
5713 &fr.duration_sec, &fr.duration_nsec);
d10976b7 5714 ovs_mutex_lock(&rule->mutex);
abe529af 5715 fr.idle_timeout = rule->idle_timeout;
fa2bad0f 5716 fr.hard_timeout = rule->hard_timeout;
d10976b7 5717 ovs_mutex_unlock(&rule->mutex);
abe529af 5718 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
dc437090 5719 &fr.byte_count, &used);
25010c68 5720 connmgr_send_flow_removed(connmgr, &fr);
f695ebfa 5721 ovs_mutex_unlock(&ofproto_mutex);
abe529af
BP
5722}
5723
5724/* Sends an OpenFlow "flow removed" message with the given 'reason' (either
5725 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
5726 * ofproto.
5727 *
5728 * ofproto implementation ->run() functions should use this function to expire
5729 * OpenFlow flows. */
5730void
5731ofproto_rule_expire(struct rule *rule, uint8_t reason)
15aaf599 5732 OVS_REQUIRES(ofproto_mutex)
abe529af 5733{
802f84ff
JR
5734 struct rule_collection rules;
5735
fe59694b
JR
5736 rule_collection_init(&rules);
5737 rule_collection_add(&rules, rule);
802f84ff 5738 delete_flows__(&rules, reason, NULL);
79eee1eb 5739}
994c9973
BP
5740
5741/* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5742 * means "infinite". */
5743static void
5744reduce_timeout(uint16_t max, uint16_t *timeout)
5745{
5746 if (max && (!*timeout || *timeout > max)) {
5747 *timeout = max;
5748 }
5749}
5750
5751/* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
5752 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
5753 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
5754 *
5755 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
1f4a8933
JR
5756void
5757ofproto_rule_reduce_timeouts__(struct rule *rule,
5758 uint16_t idle_timeout, uint16_t hard_timeout)
5759 OVS_REQUIRES(ofproto_mutex)
5760 OVS_EXCLUDED(rule->mutex)
5761{
5762 if (!idle_timeout && !hard_timeout) {
5763 return;
5764 }
5765
5766 if (ovs_list_is_empty(&rule->expirable)) {
5767 ovs_list_insert(&rule->ofproto->expirable, &rule->expirable);
5768 }
5769
5770 ovs_mutex_lock(&rule->mutex);
5771 reduce_timeout(idle_timeout, &rule->idle_timeout);
5772 reduce_timeout(hard_timeout, &rule->hard_timeout);
5773 ovs_mutex_unlock(&rule->mutex);
5774}
5775
994c9973
BP
5776void
5777ofproto_rule_reduce_timeouts(struct rule *rule,
5778 uint16_t idle_timeout, uint16_t hard_timeout)
d10976b7 5779 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
994c9973
BP
5780{
5781 if (!idle_timeout && !hard_timeout) {
5782 return;
5783 }
5784
abe7b10f 5785 ovs_mutex_lock(&ofproto_mutex);
417e7e66
BW
5786 if (ovs_list_is_empty(&rule->expirable)) {
5787 ovs_list_insert(&rule->ofproto->expirable, &rule->expirable);
994c9973 5788 }
abe7b10f 5789 ovs_mutex_unlock(&ofproto_mutex);
994c9973 5790
d10976b7 5791 ovs_mutex_lock(&rule->mutex);
994c9973
BP
5792 reduce_timeout(idle_timeout, &rule->idle_timeout);
5793 reduce_timeout(hard_timeout, &rule->hard_timeout);
d10976b7 5794 ovs_mutex_unlock(&rule->mutex);
994c9973 5795}
79eee1eb 5796\f
90bf1e07 5797static enum ofperr
2e4f5fcf 5798handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 5799 OVS_EXCLUDED(ofproto_mutex)
064af421 5800{
a5b8d268 5801 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7338102b 5802 struct ofputil_flow_mod fm;
f25d0cf3
BP
5803 uint64_t ofpacts_stub[1024 / 8];
5804 struct ofpbuf ofpacts;
90bf1e07 5805 enum ofperr error;
064af421 5806
76589937 5807 error = reject_slave_controller(ofconn);
9deba63b 5808 if (error) {
b0d38b2f 5809 return error;
9deba63b 5810 }
3052b0c5 5811
f25d0cf3 5812 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
7338102b 5813 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
04f48a68
YHW
5814 ofproto_get_tun_tab(ofproto),
5815 &ofproto->vl_mff_map, &ofpacts,
7e9f8266
BP
5816 u16_to_ofp(ofproto->max_ports),
5817 ofproto->n_tables);
548de4dd 5818 if (!error) {
0a0d9385 5819 struct openflow_mod_requester req = { ofconn, oh };
7338102b 5820 error = handle_flow_mod__(ofproto, &fm, &req);
49bdc010 5821 }
2e310801 5822
f25d0cf3 5823 ofpbuf_uninit(&ofpacts);
f25d0cf3 5824 return error;
75a75043
BP
5825}
5826
90bf1e07 5827static enum ofperr
7338102b 5828handle_flow_mod__(struct ofproto *ofproto, const struct ofputil_flow_mod *fm,
0a0d9385 5829 const struct openflow_mod_requester *req)
15aaf599 5830 OVS_EXCLUDED(ofproto_mutex)
75a75043 5831{
7338102b 5832 struct ofproto_flow_mod ofm;
35412852 5833 enum ofperr error;
75a75043 5834
2c7ee524 5835 error = ofproto_flow_mod_init(ofproto, &ofm, fm, NULL);
5bacd5cd
JR
5836 if (error) {
5837 return error;
5838 }
7338102b 5839
15aaf599 5840 ovs_mutex_lock(&ofproto_mutex);
7338102b
JR
5841 ofm.version = ofproto->tables_version + 1;
5842 error = ofproto_flow_mod_start(ofproto, &ofm);
1f42be1c 5843 if (!error) {
39c94593 5844 ofproto_bump_tables_version(ofproto);
7338102b 5845 ofproto_flow_mod_finish(ofproto, &ofm, req);
2c7ee524 5846 ofmonitor_flush(ofproto->connmgr);
3052b0c5 5847 }
15aaf599 5848 ovs_mutex_unlock(&ofproto_mutex);
35412852 5849
35412852 5850 return error;
3052b0c5
BP
5851}
5852
90bf1e07 5853static enum ofperr
d1e2cf21 5854handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
9deba63b 5855{
f4f1ea7e
BP
5856 struct ofputil_role_request request;
5857 struct ofputil_role_request reply;
9deba63b 5858 struct ofpbuf *buf;
6ea4776b
JR
5859 enum ofperr error;
5860
f4f1ea7e 5861 error = ofputil_decode_role_message(oh, &request);
6ea4776b
JR
5862 if (error) {
5863 return error;
5864 }
9deba63b 5865
f4f1ea7e 5866 if (request.role != OFPCR12_ROLE_NOCHANGE) {
d26eda9f
BP
5867 if (request.role != OFPCR12_ROLE_EQUAL
5868 && request.have_generation_id
f4f1ea7e
BP
5869 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
5870 return OFPERR_OFPRRFC_STALE;
6ea4776b 5871 }
6ea4776b 5872
f4f1ea7e
BP
5873 ofconn_set_role(ofconn, request.role);
5874 }
9deba63b 5875
f4f1ea7e 5876 reply.role = ofconn_get_role(ofconn);
147cc9d3
BP
5877 reply.have_generation_id = ofconn_get_master_election_id(
5878 ofconn, &reply.generation_id);
f4f1ea7e 5879 buf = ofputil_encode_role_reply(oh, &reply);
b0421aa2 5880 ofconn_send_reply(ofconn, buf);
9deba63b
BP
5881
5882 return 0;
5883}
5884
90bf1e07 5885static enum ofperr
6c1491fb
BP
5886handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
5887 const struct ofp_header *oh)
5888{
982697a4 5889 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
27527aa0
BP
5890 enum ofputil_protocol cur, next;
5891
5892 cur = ofconn_get_protocol(ofconn);
5893 next = ofputil_protocol_set_tid(cur, msg->set != 0);
5894 ofconn_set_protocol(ofconn, next);
6c1491fb 5895
6c1491fb
BP
5896 return 0;
5897}
5898
90bf1e07 5899static enum ofperr
d1e2cf21 5900handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
09246b99 5901{
982697a4 5902 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
27527aa0
BP
5903 enum ofputil_protocol cur, next;
5904 enum ofputil_protocol next_base;
09246b99 5905
27527aa0
BP
5906 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
5907 if (!next_base) {
90bf1e07 5908 return OFPERR_OFPBRC_EPERM;
09246b99 5909 }
7ee20df1 5910
27527aa0
BP
5911 cur = ofconn_get_protocol(ofconn);
5912 next = ofputil_protocol_set_base(cur, next_base);
27527aa0 5913 ofconn_set_protocol(ofconn, next);
b20f4073 5914
7ee20df1 5915 return 0;
09246b99
BP
5916}
5917
90bf1e07 5918static enum ofperr
54834960
EJ
5919handle_nxt_set_packet_in_format(struct ofconn *ofconn,
5920 const struct ofp_header *oh)
5921{
982697a4 5922 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
54834960
EJ
5923 uint32_t format;
5924
54834960 5925 format = ntohl(msg->format);
6409e008 5926 if (!ofputil_packet_in_format_is_valid(format)) {
90bf1e07 5927 return OFPERR_OFPBRC_EPERM;
54834960
EJ
5928 }
5929
54834960
EJ
5930 ofconn_set_packet_in_format(ofconn, format);
5931 return 0;
5932}
5933
80d5aefd
BP
5934static enum ofperr
5935handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
5936{
5876b4db
BP
5937 struct ofputil_async_cfg basis = ofconn_get_async_config(ofconn);
5938 struct ofputil_async_cfg ac;
d18cc1ee 5939 enum ofperr error;
80d5aefd 5940
5876b4db 5941 error = ofputil_decode_set_async_config(oh, false, &basis, &ac);
d18cc1ee
AA
5942 if (error) {
5943 return error;
5944 }
80d5aefd 5945
a930d4c5 5946 ofconn_set_async_config(ofconn, &ac);
4550b647
MM
5947 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
5948 !ofconn_get_miss_send_len(ofconn)) {
5949 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
5950 }
80d5aefd
BP
5951
5952 return 0;
5953}
5954
c423b3b3
AC
5955static enum ofperr
5956handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
5957{
a930d4c5 5958 struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn);
af7bc161 5959 ofconn_send_reply(ofconn, ofputil_encode_get_async_reply(oh, &ac));
c423b3b3
AC
5960
5961 return 0;
5962}
5963
a7349929
BP
5964static enum ofperr
5965handle_nxt_set_controller_id(struct ofconn *ofconn,
5966 const struct ofp_header *oh)
5967{
982697a4 5968 const struct nx_controller_id *nci = ofpmsg_body(oh);
a7349929 5969
a7349929
BP
5970 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
5971 return OFPERR_NXBRC_MUST_BE_ZERO;
5972 }
5973
5974 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
5975 return 0;
5976}
5977
90bf1e07 5978static enum ofperr
d1e2cf21 5979handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
246e61ea 5980{
246e61ea
JP
5981 struct ofpbuf *buf;
5982
982697a4
BP
5983 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
5984 ? OFPRAW_OFPT10_BARRIER_REPLY
5985 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
b0421aa2 5986 ofconn_send_reply(ofconn, buf);
246e61ea
JP
5987 return 0;
5988}
5989
2b07c8b1
BP
5990static void
5991ofproto_compose_flow_refresh_update(const struct rule *rule,
5992 enum nx_flow_monitor_flags flags,
140f36ba
DDP
5993 struct ovs_list *msgs,
5994 const struct tun_table *tun_table)
15aaf599 5995 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 5996{
6f00e29b 5997 const struct rule_actions *actions;
2b07c8b1
BP
5998 struct ofputil_flow_update fu;
5999
2b07c8b1
BP
6000 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
6001 ? NXFME_ADDED : NXFME_MODIFIED);
6002 fu.reason = 0;
d10976b7 6003 ovs_mutex_lock(&rule->mutex);
2b07c8b1
BP
6004 fu.idle_timeout = rule->idle_timeout;
6005 fu.hard_timeout = rule->hard_timeout;
d10976b7 6006 ovs_mutex_unlock(&rule->mutex);
2b07c8b1
BP
6007 fu.table_id = rule->table_id;
6008 fu.cookie = rule->flow_cookie;
140f36ba 6009 minimatch_expand(&rule->cr.match, &fu.match);
6b140a4e 6010 fu.priority = rule->cr.priority;
6f00e29b 6011
b20f4073 6012 actions = flags & NXFMF_ACTIONS ? rule_get_actions(rule) : NULL;
6f00e29b
BP
6013 fu.ofpacts = actions ? actions->ofpacts : NULL;
6014 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
2b07c8b1 6015
417e7e66 6016 if (ovs_list_is_empty(msgs)) {
2b07c8b1
BP
6017 ofputil_start_flow_update(msgs);
6018 }
140f36ba 6019 ofputil_append_flow_update(&fu, msgs, tun_table);
2b07c8b1
BP
6020}
6021
6022void
a8e547c1 6023ofmonitor_compose_refresh_updates(struct rule_collection *rules,
ca6ba700 6024 struct ovs_list *msgs)
15aaf599 6025 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 6026{
fe59694b 6027 struct rule *rule;
2b07c8b1 6028
fe59694b 6029 RULE_COLLECTION_FOR_EACH (rule, rules) {
2b07c8b1
BP
6030 enum nx_flow_monitor_flags flags = rule->monitor_flags;
6031 rule->monitor_flags = 0;
6032
140f36ba
DDP
6033 ofproto_compose_flow_refresh_update(rule, flags, msgs,
6034 ofproto_get_tun_tab(rule->ofproto));
2b07c8b1
BP
6035 }
6036}
6037
6038static void
6039ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
6040 struct rule *rule, uint64_t seqno,
a8e547c1 6041 struct rule_collection *rules)
15aaf599 6042 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6043{
6044 enum nx_flow_monitor_flags update;
6045
5e8b38b0 6046 if (rule_is_hidden(rule)) {
2b07c8b1
BP
6047 return;
6048 }
6049
b20f4073 6050 if (!ofproto_rule_has_out_port(rule, m->out_port)) {
2b07c8b1
BP
6051 return;
6052 }
6053
6054 if (seqno) {
6055 if (rule->add_seqno > seqno) {
6056 update = NXFMF_ADD | NXFMF_MODIFY;
6057 } else if (rule->modify_seqno > seqno) {
6058 update = NXFMF_MODIFY;
6059 } else {
6060 return;
6061 }
6062
6063 if (!(m->flags & update)) {
6064 return;
6065 }
6066 } else {
6067 update = NXFMF_INITIAL;
6068 }
6069
6070 if (!rule->monitor_flags) {
a8e547c1 6071 rule_collection_add(rules, rule);
2b07c8b1
BP
6072 }
6073 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
6074}
6075
6076static void
6077ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
6078 uint64_t seqno,
a8e547c1 6079 struct rule_collection *rules)
15aaf599 6080 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6081{
6082 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
2b07c8b1 6083 const struct oftable *table;
81a76618 6084 struct cls_rule target;
2b07c8b1 6085
bd53aa17 6086 cls_rule_init_from_minimatch(&target, &m->match, 0);
2b07c8b1 6087 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
2b07c8b1
BP
6088 struct rule *rule;
6089
44e0c35d 6090 CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &target, OVS_VERSION_MAX) {
2b07c8b1
BP
6091 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
6092 }
6093 }
48d28ac1 6094 cls_rule_destroy(&target);
2b07c8b1
BP
6095}
6096
6097static void
6098ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
a8e547c1 6099 struct rule_collection *rules)
15aaf599 6100 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6101{
6102 if (m->flags & NXFMF_INITIAL) {
6103 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
6104 }
6105}
6106
6107void
6108ofmonitor_collect_resume_rules(struct ofmonitor *m,
a8e547c1 6109 uint64_t seqno, struct rule_collection *rules)
15aaf599 6110 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
6111{
6112 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
6113}
6114
7b900689
SH
6115static enum ofperr
6116flow_monitor_delete(struct ofconn *ofconn, uint32_t id)
6117 OVS_REQUIRES(ofproto_mutex)
6118{
6119 struct ofmonitor *m;
6120 enum ofperr error;
6121
6122 m = ofmonitor_lookup(ofconn, id);
6123 if (m) {
6124 ofmonitor_destroy(m);
6125 error = 0;
6126 } else {
6127 error = OFPERR_OFPMOFC_UNKNOWN_MONITOR;
6128 }
6129
6130 return error;
6131}
6132
2b07c8b1 6133static enum ofperr
982697a4 6134handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 6135 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1
BP
6136{
6137 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2b07c8b1 6138
0a2869d5
BP
6139 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
6140
6141 struct ofmonitor **monitors = NULL;
6142 size_t allocated_monitors = 0;
6143 size_t n_monitors = 0;
6144
6145 enum ofperr error;
15aaf599
BP
6146
6147 ovs_mutex_lock(&ofproto_mutex);
2b07c8b1
BP
6148 for (;;) {
6149 struct ofputil_flow_monitor_request request;
6150 struct ofmonitor *m;
6151 int retval;
6152
6153 retval = ofputil_decode_flow_monitor_request(&request, &b);
6154 if (retval == EOF) {
6155 break;
6156 } else if (retval) {
6157 error = retval;
6158 goto error;
6159 }
6160
6161 if (request.table_id != 0xff
6162 && request.table_id >= ofproto->n_tables) {
6163 error = OFPERR_OFPBRC_BAD_TABLE_ID;
6164 goto error;
6165 }
6166
6167 error = ofmonitor_create(&request, ofconn, &m);
6168 if (error) {
6169 goto error;
6170 }
6171
6172 if (n_monitors >= allocated_monitors) {
6173 monitors = x2nrealloc(monitors, &allocated_monitors,
6174 sizeof *monitors);
6175 }
6176 monitors[n_monitors++] = m;
6177 }
6178
0a2869d5 6179 struct rule_collection rules;
a8e547c1 6180 rule_collection_init(&rules);
0a2869d5 6181 for (size_t i = 0; i < n_monitors; i++) {
2b07c8b1
BP
6182 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
6183 }
6184
0a2869d5 6185 struct ovs_list replies;
982697a4 6186 ofpmp_init(&replies, oh);
2b07c8b1 6187 ofmonitor_compose_refresh_updates(&rules, &replies);
15aaf599
BP
6188 ovs_mutex_unlock(&ofproto_mutex);
6189
a8e547c1
BP
6190 rule_collection_destroy(&rules);
6191
2b07c8b1 6192 ofconn_send_replies(ofconn, &replies);
2b07c8b1
BP
6193 free(monitors);
6194
6195 return 0;
6196
6197error:
0a2869d5 6198 for (size_t i = 0; i < n_monitors; i++) {
2b07c8b1
BP
6199 ofmonitor_destroy(monitors[i]);
6200 }
6201 free(monitors);
4cd1bc9d
BP
6202 ovs_mutex_unlock(&ofproto_mutex);
6203
2b07c8b1
BP
6204 return error;
6205}
6206
6207static enum ofperr
6208handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 6209 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1 6210{
15aaf599 6211 enum ofperr error;
2b07c8b1
BP
6212 uint32_t id;
6213
6214 id = ofputil_decode_flow_monitor_cancel(oh);
15aaf599
BP
6215
6216 ovs_mutex_lock(&ofproto_mutex);
7b900689 6217 error = flow_monitor_delete(ofconn, id);
15aaf599 6218 ovs_mutex_unlock(&ofproto_mutex);
2b07c8b1 6219
15aaf599 6220 return error;
2b07c8b1
BP
6221}
6222
9cae45dc
JR
6223/* Meters implementation.
6224 *
6225 * Meter table entry, indexed by the OpenFlow meter_id.
9cae45dc
JR
6226 * 'created' is used to compute the duration for meter stats.
6227 * 'list rules' is needed so that we can delete the dependent rules when the
6228 * meter table entry is deleted.
6229 * 'provider_meter_id' is for the provider's private use.
6230 */
6231struct meter {
ba8cff36 6232 struct hmap_node node; /* In ofproto->meters. */
9cae45dc 6233 long long int created; /* Time created. */
ca6ba700 6234 struct ovs_list rules; /* List of "struct rule_dpif"s. */
ba8cff36 6235 uint32_t id; /* OpenFlow meter_id. */
9cae45dc
JR
6236 ofproto_meter_id provider_meter_id;
6237 uint16_t flags; /* Meter flags. */
6238 uint16_t n_bands; /* Number of meter bands. */
6239 struct ofputil_meter_band *bands;
6240};
6241
ba8cff36
AZ
6242static struct meter *
6243ofproto_get_meter(const struct ofproto *ofproto, uint32_t meter_id)
6244{
6245 struct meter *meter;
6246 uint32_t hash = hash_int(meter_id, 0);
6247
6248 HMAP_FOR_EACH_WITH_HASH (meter, node, hash, &ofproto->meters) {
6249 if (meter->id == meter_id) {
6250 return meter;
6251 }
6252 }
6253
6254 return NULL;
6255}
6256
9e638f22
AZ
6257static uint32_t *
6258ofproto_upcall_meter_ptr(struct ofproto *ofproto, uint32_t meter_id)
6259{
6260 switch(meter_id) {
6261 case OFPM13_SLOWPATH:
6262 return &ofproto->slowpath_meter_id;
6263 break;
6264 case OFPM13_CONTROLLER:
6265 return &ofproto->controller_meter_id;
6266 break;
6267 case OFPM13_ALL:
6268 OVS_NOT_REACHED();
6269 default:
6270 return NULL;
6271 }
6272}
6273
ba8cff36
AZ
6274static void
6275ofproto_add_meter(struct ofproto *ofproto, struct meter *meter)
6276{
9e638f22
AZ
6277 uint32_t *upcall_meter_ptr = ofproto_upcall_meter_ptr(ofproto, meter->id);
6278
6279 /* Cache datapath meter IDs of special meters. */
6280 if (upcall_meter_ptr) {
6281 *upcall_meter_ptr = meter->provider_meter_id.uint32;
6282 }
6283
ba8cff36
AZ
6284 hmap_insert(&ofproto->meters, &meter->node, hash_int(meter->id, 0));
6285}
6286
9cae45dc 6287/*
076caa2f
JR
6288 * This is used in instruction validation at flow set-up time, to map
6289 * the OpenFlow meter ID to the corresponding datapath provider meter
6290 * ID. If either does not exist, returns false. Otherwise updates
6291 * the meter action and returns true.
9cae45dc 6292 */
076caa2f
JR
6293static bool
6294ofproto_fix_meter_action(const struct ofproto *ofproto,
6295 struct ofpact_meter *ma)
6296{
ba8cff36
AZ
6297 if (ma->meter_id) {
6298 const struct meter *meter = ofproto_get_meter(ofproto, ma->meter_id);
076caa2f
JR
6299
6300 if (meter && meter->provider_meter_id.uint32 != UINT32_MAX) {
6301 /* Update the action with the provider's meter ID, so that we
6302 * do not need any synchronization between ofproto_dpif_xlate
6303 * and ofproto for meter table access. */
6304 ma->provider_meter_id = meter->provider_meter_id.uint32;
6305 return true;
9cae45dc
JR
6306 }
6307 }
076caa2f 6308 return false;
9cae45dc
JR
6309}
6310
062fea06
BP
6311/* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
6312 * list of rules. */
6313static void
6314meter_insert_rule(struct rule *rule)
6315{
6316 const struct rule_actions *a = rule_get_actions(rule);
6317 uint32_t meter_id = ofpacts_get_meter(a->ofpacts, a->ofpacts_len);
ba8cff36 6318 struct meter *meter = ofproto_get_meter(rule->ofproto, meter_id);
062fea06 6319
417e7e66 6320 ovs_list_insert(&meter->rules, &rule->meter_list_node);
062fea06
BP
6321}
6322
9cae45dc
JR
6323static void
6324meter_update(struct meter *meter, const struct ofputil_meter_config *config)
6325{
6326 free(meter->bands);
6327
6328 meter->flags = config->flags;
6329 meter->n_bands = config->n_bands;
6330 meter->bands = xmemdup(config->bands,
6331 config->n_bands * sizeof *meter->bands);
6332}
6333
6334static struct meter *
6335meter_create(const struct ofputil_meter_config *config,
6336 ofproto_meter_id provider_meter_id)
6337{
6338 struct meter *meter;
6339
6340 meter = xzalloc(sizeof *meter);
6341 meter->provider_meter_id = provider_meter_id;
6342 meter->created = time_msec();
ba8cff36 6343 meter->id = config->meter_id;
417e7e66 6344 ovs_list_init(&meter->rules);
9cae45dc
JR
6345
6346 meter_update(meter, config);
6347
6348 return meter;
6349}
6350
6b3f7f02 6351static void
ba8cff36 6352meter_destroy(struct ofproto *ofproto, struct meter *meter)
15aaf599 6353 OVS_REQUIRES(ofproto_mutex)
6b3f7f02 6354{
9e638f22
AZ
6355 uint32_t *upcall_meter_ptr;
6356 upcall_meter_ptr = ofproto_upcall_meter_ptr(ofproto, meter->id);
6357 if (upcall_meter_ptr) {
6358 *upcall_meter_ptr = UINT32_MAX;
6359 }
6360
ba8cff36
AZ
6361 if (!ovs_list_is_empty(&meter->rules)) {
6362 struct rule_collection rules;
6363 struct rule *rule;
3059d7e5 6364
ba8cff36
AZ
6365 rule_collection_init(&rules);
6366 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
6367 rule_collection_add(&rules, rule);
6368 }
6369 delete_flows__(&rules, OFPRR_METER_DELETE, NULL);
6370 }
3059d7e5 6371
ba8cff36
AZ
6372 ofproto->ofproto_class->meter_del(ofproto, meter->provider_meter_id);
6373 free(meter->bands);
6374 free(meter);
6375}
3059d7e5 6376
ba8cff36
AZ
6377static void
6378meter_delete(struct ofproto *ofproto, uint32_t meter_id)
6379 OVS_REQUIRES(ofproto_mutex)
6380{
6381 struct meter *meter = ofproto_get_meter(ofproto, meter_id);
6382
6383 if (meter) {
6384 hmap_remove(&ofproto->meters, &meter->node);
6385 meter_destroy(ofproto, meter);
6386 }
6387}
6388
6389static void
6390meter_delete_all(struct ofproto *ofproto)
6391 OVS_REQUIRES(ofproto_mutex)
6392{
6393 struct meter *meter, *next;
6394
6395 HMAP_FOR_EACH_SAFE (meter, next, node, &ofproto->meters) {
6396 hmap_remove(&ofproto->meters, &meter->node);
6397 meter_destroy(ofproto, meter);
6b3f7f02
JR
6398 }
6399}
6400
9cae45dc
JR
6401static enum ofperr
6402handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
6403{
6404 ofproto_meter_id provider_meter_id = { UINT32_MAX };
ba8cff36 6405 struct meter *meter = ofproto_get_meter(ofproto, mm->meter.meter_id);
9cae45dc
JR
6406 enum ofperr error;
6407
ba8cff36 6408 if (meter) {
9cae45dc
JR
6409 return OFPERR_OFPMMFC_METER_EXISTS;
6410 }
6411
6412 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
6413 &mm->meter);
6414 if (!error) {
6415 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
ba8cff36
AZ
6416 meter = meter_create(&mm->meter, provider_meter_id);
6417 ofproto_add_meter(ofproto, meter);
9cae45dc 6418 }
f0f8c6c2 6419 return error;
9cae45dc
JR
6420}
6421
6422static enum ofperr
6423handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
6424{
ba8cff36 6425 struct meter *meter = ofproto_get_meter(ofproto, mm->meter.meter_id);
9cae45dc 6426 enum ofperr error;
e555eb7c 6427 uint32_t provider_meter_id;
9cae45dc
JR
6428
6429 if (!meter) {
6430 return OFPERR_OFPMMFC_UNKNOWN_METER;
6431 }
6432
e555eb7c 6433 provider_meter_id = meter->provider_meter_id.uint32;
9cae45dc
JR
6434 error = ofproto->ofproto_class->meter_set(ofproto,
6435 &meter->provider_meter_id,
6436 &mm->meter);
e555eb7c 6437 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
9cae45dc
JR
6438 if (!error) {
6439 meter_update(meter, &mm->meter);
6440 }
6441 return error;
6442}
6443
6444static enum ofperr
baae3d02 6445handle_delete_meter(struct ofconn *ofconn, struct ofputil_meter_mod *mm)
15aaf599 6446 OVS_EXCLUDED(ofproto_mutex)
9cae45dc
JR
6447{
6448 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6449 uint32_t meter_id = mm->meter.meter_id;
9cae45dc 6450
ba8cff36
AZ
6451 /* OpenFlow does not support Meter ID 0. */
6452 if (meter_id) {
6453 ovs_mutex_lock(&ofproto_mutex);
6454
6455 if (meter_id == OFPM13_ALL) {
6456 meter_delete_all(ofproto);
6457 } else {
6458 meter_delete(ofproto, meter_id);
9cae45dc 6459 }
9cae45dc 6460
ba8cff36
AZ
6461 ovs_mutex_unlock(&ofproto_mutex);
6462 }
a8e547c1 6463
ba8cff36 6464 return 0;
9cae45dc
JR
6465}
6466
6467static enum ofperr
6468handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
6469{
6470 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6471 struct ofputil_meter_mod mm;
6472 uint64_t bands_stub[256 / 8];
6473 struct ofpbuf bands;
6474 uint32_t meter_id;
6475 enum ofperr error;
6476
6477 error = reject_slave_controller(ofconn);
6478 if (error) {
6479 return error;
6480 }
6481
6482 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
6483
6484 error = ofputil_decode_meter_mod(oh, &mm, &bands);
6485 if (error) {
6486 goto exit_free_bands;
6487 }
6488
6489 meter_id = mm.meter.meter_id;
6490
6491 if (mm.command != OFPMC13_DELETE) {
6492 /* Fails also when meters are not implemented by the provider. */
9e638f22 6493 if (ofproto->meter_features.max_meters == 0) {
9cae45dc
JR
6494 error = OFPERR_OFPMMFC_INVALID_METER;
6495 goto exit_free_bands;
9e638f22
AZ
6496 }
6497
6498 if (meter_id == 0) {
6499 error = OFPERR_OFPMMFC_INVALID_METER;
b31e8700 6500 goto exit_free_bands;
9e638f22
AZ
6501 } else if (meter_id > OFPM13_MAX) {
6502 switch(meter_id) {
6503 case OFPM13_SLOWPATH:
6504 case OFPM13_CONTROLLER:
6505 break;
6506 case OFPM13_ALL:
6507 default:
6508 error = OFPERR_OFPMMFC_INVALID_METER;
6509 goto exit_free_bands;
6510 }
9cae45dc
JR
6511 }
6512 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
6513 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
6514 goto exit_free_bands;
6515 }
6516 }
6517
6518 switch (mm.command) {
6519 case OFPMC13_ADD:
6520 error = handle_add_meter(ofproto, &mm);
6521 break;
6522
6523 case OFPMC13_MODIFY:
6524 error = handle_modify_meter(ofproto, &mm);
6525 break;
6526
6527 case OFPMC13_DELETE:
baae3d02 6528 error = handle_delete_meter(ofconn, &mm);
9cae45dc
JR
6529 break;
6530
6531 default:
6532 error = OFPERR_OFPMMFC_BAD_COMMAND;
6533 break;
6534 }
6535
3c35db62
NR
6536 if (!error) {
6537 struct ofputil_requestforward rf;
6538 rf.xid = oh->xid;
6539 rf.reason = OFPRFR_METER_MOD;
6540 rf.meter_mod = &mm;
6541 connmgr_send_requestforward(ofproto->connmgr, ofconn, &rf);
6542 }
6543
9cae45dc
JR
6544exit_free_bands:
6545 ofpbuf_uninit(&bands);
6546 return error;
6547}
6548
6549static enum ofperr
6550handle_meter_features_request(struct ofconn *ofconn,
6551 const struct ofp_header *request)
6552{
6553 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6554 struct ofputil_meter_features features;
6555 struct ofpbuf *b;
6556
6557 if (ofproto->ofproto_class->meter_get_features) {
6558 ofproto->ofproto_class->meter_get_features(ofproto, &features);
6559 } else {
6560 memset(&features, 0, sizeof features);
6561 }
6562 b = ofputil_encode_meter_features_reply(&features, request);
6563
6564 ofconn_send_reply(ofconn, b);
6565 return 0;
6566}
6567
ba8cff36
AZ
6568static void
6569meter_request_reply(struct ofproto *ofproto, struct meter *meter,
6570 enum ofptype type, struct ovs_list *replies)
6571{
6572 uint64_t bands_stub[256 / 8];
6573 struct ofpbuf bands;
6574
6575 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
6576
6577 if (type == OFPTYPE_METER_STATS_REQUEST) {
6578 struct ofputil_meter_stats stats;
6579
6580 stats.meter_id = meter->id;
6581
6582 /* Provider sets the packet and byte counts, we do the rest. */
6583 stats.flow_count = ovs_list_size(&meter->rules);
6584 calc_duration(meter->created, time_msec(),
6585 &stats.duration_sec, &stats.duration_nsec);
6586 stats.n_bands = meter->n_bands;
6587 ofpbuf_clear(&bands);
6588 stats.bands = ofpbuf_put_uninit(&bands, meter->n_bands
6589 * sizeof *stats.bands);
6590
6591 if (!ofproto->ofproto_class->meter_get(ofproto,
6592 meter->provider_meter_id,
6593 &stats, meter->n_bands)) {
6594 ofputil_append_meter_stats(replies, &stats);
6595 }
6596 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
6597 struct ofputil_meter_config config;
6598
6599 config.meter_id = meter->id;
6600 config.flags = meter->flags;
6601 config.n_bands = meter->n_bands;
6602 config.bands = meter->bands;
6603 ofputil_append_meter_config(replies, &config);
6604 }
6605
6606 ofpbuf_uninit(&bands);
6607}
6608
6609static void
6610meter_request_reply_all(struct ofproto *ofproto, enum ofptype type,
6611 struct ovs_list *replies)
6612{
6613 struct meter *meter;
6614
6615 HMAP_FOR_EACH (meter, node, &ofproto->meters) {
6616 meter_request_reply(ofproto, meter, type, replies);
6617 }
6618}
6619
9cae45dc
JR
6620static enum ofperr
6621handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
6622 enum ofptype type)
6623{
6624 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
ca6ba700 6625 struct ovs_list replies;
ba8cff36
AZ
6626 uint32_t meter_id;
6627 struct meter *meter;
9cae45dc
JR
6628
6629 ofputil_decode_meter_request(request, &meter_id);
6630
ba8cff36
AZ
6631 if (meter_id != OFPM13_ALL) {
6632 meter = ofproto_get_meter(ofproto, meter_id);
6633 if (!meter) {
6634 /* Meter does not exist. */
9cae45dc
JR
6635 return OFPERR_OFPMMFC_UNKNOWN_METER;
6636 }
ba8cff36
AZ
6637 } else {
6638 /* GCC 4.9.2 complains about 'meter' can potentially be used
6639 * uninitialized. Logically, this is a false alarm since
6640 * meter is only used when meter_id != OFPM13_ALL.
6641 * Set NULL to make compiler happy. */
6642 meter = NULL;
9cae45dc
JR
6643 }
6644
9cae45dc 6645 ofpmp_init(&replies, request);
ba8cff36
AZ
6646 if (meter_id == OFPM13_ALL) {
6647 meter_request_reply_all(ofproto, type, &replies);
6648 } else {
6649 meter_request_reply(ofproto, meter, type, &replies);
9cae45dc 6650 }
9cae45dc 6651 ofconn_send_replies(ofconn, &replies);
9cae45dc
JR
6652 return 0;
6653}
6654
db88b35c
JR
6655/* Returned group is RCU protected. */
6656static struct ofgroup *
5d08a275
JR
6657ofproto_group_lookup__(const struct ofproto *ofproto, uint32_t group_id,
6658 ovs_version_t version)
db88b35c
JR
6659{
6660 struct ofgroup *group;
6661
6662 CMAP_FOR_EACH_WITH_HASH (group, cmap_node, hash_int(group_id, 0),
6663 &ofproto->groups) {
5d08a275
JR
6664 if (group->group_id == group_id
6665 && versions_visible_in_version(&group->versions, version)) {
db88b35c 6666 return group;
7395c052
NZ
6667 }
6668 }
3fc3b679 6669
db88b35c 6670 return NULL;
7395c052
NZ
6671}
6672
3fc3b679
AZ
6673/* If the group exists, this function increments the groups's reference count.
6674 *
6675 * Make sure to call ofproto_group_unref() after no longer needing to maintain
6676 * a reference to the group. */
db88b35c 6677struct ofgroup *
76973237 6678ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5d08a275 6679 ovs_version_t version, bool take_ref)
7395c052 6680{
db88b35c 6681 struct ofgroup *group;
7395c052 6682
5d08a275 6683 group = ofproto_group_lookup__(ofproto, group_id, version);
76973237 6684 if (group && take_ref) {
db88b35c
JR
6685 /* Not holding a lock, so it is possible that another thread releases
6686 * the last reference just before we manage to get one. */
6687 return ofproto_group_try_ref(group) ? group : NULL;
7395c052 6688 }
76973237 6689 return group;
7395c052
NZ
6690}
6691
cc099268 6692/* Caller should hold 'ofproto_mutex' if it is important that the
db88b35c 6693 * group is not removed by someone else. */
7e9f8266
BP
6694static bool
6695ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
7e9f8266 6696{
5d08a275 6697 return ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX) != NULL;
7e9f8266
BP
6698}
6699
cc099268
JR
6700static void
6701group_add_rule(struct ofgroup *group, struct rule *rule)
732feb93 6702{
cc099268
JR
6703 rule_collection_add(&group->rules, rule);
6704}
732feb93 6705
cc099268
JR
6706static void
6707group_remove_rule(struct ofgroup *group, struct rule *rule)
6708{
6709 rule_collection_remove(&group->rules, rule);
732feb93
SH
6710}
6711
7395c052 6712static void
ca6ba700 6713append_group_stats(struct ofgroup *group, struct ovs_list *replies)
cc099268 6714 OVS_REQUIRES(ofproto_mutex)
7395c052
NZ
6715{
6716 struct ofputil_group_stats ogs;
eaf004bd 6717 const struct ofproto *ofproto = group->ofproto;
7395c052
NZ
6718 long long int now = time_msec();
6719 int error;
6720
646b2a9c
SH
6721 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
6722
732feb93 6723 /* Provider sets the packet and byte counts, we do the rest. */
fe59694b 6724 ogs.ref_count = rule_collection_n(&group->rules);
732feb93
SH
6725 ogs.n_buckets = group->n_buckets;
6726
7395c052
NZ
6727 error = (ofproto->ofproto_class->group_get_stats
6728 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
6729 : EOPNOTSUPP);
6730 if (error) {
7395c052
NZ
6731 ogs.packet_count = UINT64_MAX;
6732 ogs.byte_count = UINT64_MAX;
7395c052
NZ
6733 memset(ogs.bucket_stats, 0xff,
6734 ogs.n_buckets * sizeof *ogs.bucket_stats);
6735 }
6736
6737 ogs.group_id = group->group_id;
6738 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
6739
6740 ofputil_append_group_stats(replies, &ogs);
646b2a9c
SH
6741
6742 free(ogs.bucket_stats);
7395c052
NZ
6743}
6744
19187a71
BP
6745static void
6746handle_group_request(struct ofconn *ofconn,
6747 const struct ofp_header *request, uint32_t group_id,
ca6ba700 6748 void (*cb)(struct ofgroup *, struct ovs_list *replies))
cc099268 6749 OVS_EXCLUDED(ofproto_mutex)
7395c052
NZ
6750{
6751 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7395c052 6752 struct ofgroup *group;
ca6ba700 6753 struct ovs_list replies;
7395c052
NZ
6754
6755 ofpmp_init(&replies, request);
cc099268
JR
6756 /* Must exclude modifications to guarantee iterating groups. */
6757 ovs_mutex_lock(&ofproto_mutex);
7395c052 6758 if (group_id == OFPG_ALL) {
db88b35c 6759 CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) {
3a46eaaf
BP
6760 if (versions_visible_in_version(&group->versions,
6761 OVS_VERSION_MAX)) {
6762 cb(group, &replies);
6763 }
7395c052 6764 }
7395c052 6765 } else {
5d08a275 6766 group = ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX);
db88b35c 6767 if (group) {
19187a71 6768 cb(group, &replies);
7395c052
NZ
6769 }
6770 }
cc099268 6771 ovs_mutex_unlock(&ofproto_mutex);
7395c052 6772 ofconn_send_replies(ofconn, &replies);
7395c052
NZ
6773}
6774
6775static enum ofperr
19187a71
BP
6776handle_group_stats_request(struct ofconn *ofconn,
6777 const struct ofp_header *request)
7395c052 6778{
19187a71
BP
6779 uint32_t group_id;
6780 enum ofperr error;
7395c052 6781
19187a71
BP
6782 error = ofputil_decode_group_stats_request(request, &group_id);
6783 if (error) {
6784 return error;
7395c052 6785 }
7395c052 6786
19187a71
BP
6787 handle_group_request(ofconn, request, group_id, append_group_stats);
6788 return 0;
6789}
6790
6791static void
ca6ba700 6792append_group_desc(struct ofgroup *group, struct ovs_list *replies)
19187a71
BP
6793{
6794 struct ofputil_group_desc gds;
7395c052 6795
19187a71
BP
6796 gds.group_id = group->group_id;
6797 gds.type = group->type;
53eb84a5
SH
6798 gds.props = group->props;
6799
19187a71
BP
6800 ofputil_append_group_desc_reply(&gds, &group->buckets, replies);
6801}
6802
6803static enum ofperr
6804handle_group_desc_stats_request(struct ofconn *ofconn,
6805 const struct ofp_header *request)
6806{
6807 handle_group_request(ofconn, request,
6808 ofputil_decode_group_desc_request(request),
6809 append_group_desc);
7395c052
NZ
6810 return 0;
6811}
6812
6813static enum ofperr
6814handle_group_features_stats_request(struct ofconn *ofconn,
6815 const struct ofp_header *request)
6816{
6817 struct ofproto *p = ofconn_get_ofproto(ofconn);
6818 struct ofpbuf *msg;
6819
6820 msg = ofputil_encode_group_features_reply(&p->ogf, request);
6821 if (msg) {
6822 ofconn_send_reply(ofconn, msg);
6823 }
6824
6825 return 0;
6826}
6827
56085be5 6828static void
e016fb63
BP
6829put_queue_get_config_reply(struct ofport *port, uint32_t queue,
6830 struct ovs_list *replies)
e8f9a7bb 6831{
e016fb63 6832 struct ofputil_queue_config qc;
e8f9a7bb 6833
e016fb63
BP
6834 /* None of the existing queues have compatible properties, so we hard-code
6835 * omitting min_rate and max_rate. */
6836 qc.port = port->ofp_port;
6837 qc.queue = queue;
6838 qc.min_rate = UINT16_MAX;
6839 qc.max_rate = UINT16_MAX;
6840 ofputil_append_queue_get_config_reply(&qc, replies);
6841}
e8f9a7bb 6842
e016fb63
BP
6843static int
6844handle_queue_get_config_request_for_port(struct ofport *port, uint32_t queue,
6845 struct ovs_list *replies)
6846{
6847 struct smap details = SMAP_INITIALIZER(&details);
6848 if (queue != OFPQ_ALL) {
6849 int error = netdev_get_queue(port->netdev, queue, &details);
6850 switch (error) {
6851 case 0:
6852 put_queue_get_config_reply(port, queue, replies);
6853 break;
6854 case EOPNOTSUPP:
6855 case EINVAL:
6856 return OFPERR_OFPQOFC_BAD_QUEUE;
6857 default:
6858 return OFPERR_NXQOFC_QUEUE_ERROR;
6859 }
6860 } else {
6861 struct netdev_queue_dump queue_dump;
6862 uint32_t queue_id;
6863
6864 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump,
6865 port->netdev) {
6866 put_queue_get_config_reply(port, queue_id, replies);
6867 }
6868 }
6869 smap_destroy(&details);
6870 return 0;
56085be5
BP
6871}
6872
6873static enum ofperr
6874handle_queue_get_config_request(struct ofconn *ofconn,
6875 const struct ofp_header *oh)
6876{
e016fb63
BP
6877 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6878 struct ovs_list replies;
6879 struct ofport *port;
6880 ofp_port_t req_port;
6881 uint32_t req_queue;
6882 enum ofperr error;
6883
6884 error = ofputil_decode_queue_get_config_request(oh, &req_port, &req_queue);
6885 if (error) {
6886 return error;
6887 }
6888
6889 ofputil_start_queue_get_config_reply(oh, &replies);
6890 if (req_port == OFPP_ANY) {
6891 error = OFPERR_OFPQOFC_BAD_QUEUE;
6892 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
6893 if (!handle_queue_get_config_request_for_port(port, req_queue,
6894 &replies)) {
6895 error = 0;
6896 }
6897 }
6898 } else {
6899 port = ofproto_get_port(ofproto, req_port);
6900 error = (port
6901 ? handle_queue_get_config_request_for_port(port, req_queue,
6902 &replies)
6903 : OFPERR_OFPQOFC_BAD_PORT);
6904 }
6905 if (!error) {
6906 ofconn_send_replies(ofconn, &replies);
6907 } else {
6908 ofpbuf_list_delete(&replies);
6909 }
6910
6911 return error;
e8f9a7bb
VG
6912}
6913
836476ce
BP
6914/* Allocates, initializes, and constructs a new group in 'ofproto', obtaining
6915 * all the attributes for it from 'gm', and stores a pointer to it in
6916 * '*ofgroup'. Makes the new group visible from the flow table starting from
6917 * 'version'.
6918 *
6919 * Returns 0 if successful, otherwise an error code. If there is an error then
6920 * '*ofgroup' is indeterminate upon return. */
809c7548 6921static enum ofperr
63eded98 6922init_group(struct ofproto *ofproto, const struct ofputil_group_mod *gm,
5d08a275 6923 ovs_version_t version, struct ofgroup **ofgroup)
809c7548
RW
6924{
6925 enum ofperr error;
eaf004bd 6926 const long long int now = time_msec();
809c7548
RW
6927
6928 if (gm->group_id > OFPG_MAX) {
6929 return OFPERR_OFPGMFC_INVALID_GROUP;
6930 }
6931 if (gm->type > OFPGT11_FF) {
6932 return OFPERR_OFPGMFC_BAD_TYPE;
6933 }
6934
6935 *ofgroup = ofproto->ofproto_class->group_alloc();
6936 if (!*ofgroup) {
6937 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
6938 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
6939 }
6940
5d08a275 6941 *CONST_CAST(struct ofproto **, &(*ofgroup)->ofproto) = ofproto;
eaf004bd
AZ
6942 *CONST_CAST(uint32_t *, &((*ofgroup)->group_id)) = gm->group_id;
6943 *CONST_CAST(enum ofp11_group_type *, &(*ofgroup)->type) = gm->type;
6944 *CONST_CAST(long long int *, &((*ofgroup)->created)) = now;
6945 *CONST_CAST(long long int *, &((*ofgroup)->modified)) = now;
809c7548 6946 ovs_refcount_init(&(*ofgroup)->ref_count);
cc099268 6947 (*ofgroup)->being_deleted = false;
809c7548 6948
db88b35c
JR
6949 ovs_list_init(CONST_CAST(struct ovs_list *, &(*ofgroup)->buckets));
6950 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
6951 &(*ofgroup)->buckets),
6952 &gm->buckets, NULL);
63eded98 6953
eaf004bd 6954 *CONST_CAST(uint32_t *, &(*ofgroup)->n_buckets) =
417e7e66 6955 ovs_list_size(&(*ofgroup)->buckets);
809c7548 6956
e8dba719
JR
6957 ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props *,
6958 &(*ofgroup)->props),
6959 &gm->props);
cc099268 6960 rule_collection_init(&(*ofgroup)->rules);
bc65c25a 6961
5d08a275
JR
6962 /* Make group visible from 'version'. */
6963 (*ofgroup)->versions = VERSIONS_INITIALIZER(version,
6964 OVS_VERSION_NOT_REMOVED);
6965
809c7548
RW
6966 /* Construct called BEFORE any locks are held. */
6967 error = ofproto->ofproto_class->group_construct(*ofgroup);
6968 if (error) {
e8dba719
JR
6969 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *,
6970 &(*ofgroup)->props));
db88b35c
JR
6971 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *,
6972 &(*ofgroup)->buckets));
809c7548
RW
6973 ofproto->ofproto_class->group_dealloc(*ofgroup);
6974 }
6975 return error;
6976}
6977
6c5b90ce
BP
6978/* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
6979 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
6980 * failure. */
7395c052 6981static enum ofperr
2b0b0b80 6982add_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 6983 OVS_REQUIRES(ofproto_mutex)
7395c052 6984{
7395c052
NZ
6985 enum ofperr error;
6986
2b0b0b80
JR
6987 if (ofproto_group_exists(ofproto, ogm->gm.group_id)) {
6988 return OFPERR_OFPGMFC_GROUP_EXISTS;
7395c052
NZ
6989 }
6990
2b0b0b80
JR
6991 if (ofproto->n_groups[ogm->gm.type]
6992 >= ofproto->ogf.max_groups[ogm->gm.type]) {
6993 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
7395c052
NZ
6994 }
6995
2b0b0b80 6996 /* Allocate new group and initialize it. */
5d08a275 6997 error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group);
2b0b0b80
JR
6998 if (!error) {
6999 /* Insert new group. */
7000 cmap_insert(&ofproto->groups, &ogm->new_group->cmap_node,
7001 hash_int(ogm->new_group->group_id, 0));
7002 ofproto->n_groups[ogm->new_group->type]++;
7395c052 7003 }
7395c052
NZ
7004 return error;
7005}
7006
fce4730c
SH
7007/* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets
7008 * already in 'new_ofgroup' will be placed just after the (copy of the) bucket
7009 * in 'ofgroup' with bucket ID 'command_bucket_id'. Special
7010 * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are
7011 * also honored. */
7012static enum ofperr
7013copy_buckets_for_insert_bucket(const struct ofgroup *ofgroup,
7014 struct ofgroup *new_ofgroup,
7015 uint32_t command_bucket_id)
7016{
7017 struct ofputil_bucket *last = NULL;
7018
7019 if (command_bucket_id <= OFPG15_BUCKET_MAX) {
7020 /* Check here to ensure that a bucket corresponding to
7021 * command_bucket_id exists in the old bucket list.
7022 *
7023 * The subsequent search of below of new_ofgroup covers
7024 * both buckets in the old bucket list and buckets added
7025 * by the insert buckets group mod message this function processes. */
7026 if (!ofputil_bucket_find(&ofgroup->buckets, command_bucket_id)) {
7027 return OFPERR_OFPGMFC_UNKNOWN_BUCKET;
7028 }
7029
417e7e66 7030 if (!ovs_list_is_empty(&new_ofgroup->buckets)) {
fce4730c
SH
7031 last = ofputil_bucket_list_back(&new_ofgroup->buckets);
7032 }
7033 }
7034
db88b35c
JR
7035 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
7036 &new_ofgroup->buckets),
7037 &ofgroup->buckets, NULL);
fce4730c 7038
23a31238 7039 if (ofputil_bucket_check_duplicate_id(&new_ofgroup->buckets)) {
d2873d53 7040 VLOG_INFO_RL(&rl, "Duplicate bucket id");
fce4730c
SH
7041 return OFPERR_OFPGMFC_BUCKET_EXISTS;
7042 }
7043
7044 /* Rearrange list according to command_bucket_id */
7045 if (command_bucket_id == OFPG15_BUCKET_LAST) {
417e7e66 7046 if (!ovs_list_is_empty(&ofgroup->buckets)) {
8e19e655
BP
7047 struct ofputil_bucket *new_first;
7048 const struct ofputil_bucket *first;
fce4730c 7049
8e19e655
BP
7050 first = ofputil_bucket_list_front(&ofgroup->buckets);
7051 new_first = ofputil_bucket_find(&new_ofgroup->buckets,
7052 first->bucket_id);
fce4730c 7053
417e7e66 7054 ovs_list_splice(new_ofgroup->buckets.next, &new_first->list_node,
db88b35c
JR
7055 CONST_CAST(struct ovs_list *,
7056 &new_ofgroup->buckets));
8e19e655 7057 }
fce4730c
SH
7058 } else if (command_bucket_id <= OFPG15_BUCKET_MAX && last) {
7059 struct ofputil_bucket *after;
7060
7061 /* Presence of bucket is checked above so after should never be NULL */
7062 after = ofputil_bucket_find(&new_ofgroup->buckets, command_bucket_id);
7063
417e7e66 7064 ovs_list_splice(after->list_node.next, new_ofgroup->buckets.next,
fce4730c
SH
7065 last->list_node.next);
7066 }
7067
7068 return 0;
7069}
7070
7071/* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup'
7072 * with the exception of the bucket whose bucket id is 'command_bucket_id'.
7073 * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST
7074 * and OFPG15_BUCKET_ALL are also honored. */
7075static enum ofperr
7076copy_buckets_for_remove_bucket(const struct ofgroup *ofgroup,
7077 struct ofgroup *new_ofgroup,
7078 uint32_t command_bucket_id)
7079{
7080 const struct ofputil_bucket *skip = NULL;
7081
7082 if (command_bucket_id == OFPG15_BUCKET_ALL) {
7083 return 0;
7084 }
7085
7086 if (command_bucket_id == OFPG15_BUCKET_FIRST) {
417e7e66 7087 if (!ovs_list_is_empty(&ofgroup->buckets)) {
fce4730c
SH
7088 skip = ofputil_bucket_list_front(&ofgroup->buckets);
7089 }
7090 } else if (command_bucket_id == OFPG15_BUCKET_LAST) {
417e7e66 7091 if (!ovs_list_is_empty(&ofgroup->buckets)) {
fce4730c
SH
7092 skip = ofputil_bucket_list_back(&ofgroup->buckets);
7093 }
7094 } else {
7095 skip = ofputil_bucket_find(&ofgroup->buckets, command_bucket_id);
7096 if (!skip) {
7097 return OFPERR_OFPGMFC_UNKNOWN_BUCKET;
7098 }
7099 }
7100
db88b35c
JR
7101 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
7102 &new_ofgroup->buckets),
7103 &ofgroup->buckets, skip);
fce4730c
SH
7104
7105 return 0;
7106}
7107
7108/* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and
7109 * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code
6c5b90ce 7110 * on failure.
7395c052 7111 *
809c7548
RW
7112 * Note that the group is re-created and then replaces the old group in
7113 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
6c5b90ce 7114 * the xlate module hold a pointer to the group. */
7395c052 7115static enum ofperr
2b0b0b80 7116modify_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 7117 OVS_REQUIRES(ofproto_mutex)
7395c052 7118{
2b0b0b80
JR
7119 struct ofgroup *old_group; /* Modified group. */
7120 struct ofgroup *new_group;
7395c052
NZ
7121 enum ofperr error;
7122
5d08a275
JR
7123 old_group = ofproto_group_lookup__(ofproto, ogm->gm.group_id,
7124 OVS_VERSION_MAX);
2b0b0b80
JR
7125 if (!old_group) {
7126 return OFPERR_OFPGMFC_UNKNOWN_GROUP;
7395c052
NZ
7127 }
7128
836476ce
BP
7129 /* Inserting or deleting a bucket should not change the group's type or
7130 * properties, so change the group mod so that these aspects match the old
7131 * group. (See EXT-570.) */
7132 if (ogm->gm.command == OFPGC15_INSERT_BUCKET ||
7133 ogm->gm.command == OFPGC15_REMOVE_BUCKET) {
7134 ogm->gm.type = old_group->type;
7135 ofputil_group_properties_destroy(&ogm->gm.props);
7136 ofputil_group_properties_copy(&ogm->gm.props, &old_group->props);
7137 }
7138
2b0b0b80
JR
7139 if (old_group->type != ogm->gm.type
7140 && (ofproto->n_groups[ogm->gm.type]
7141 >= ofproto->ogf.max_groups[ogm->gm.type])) {
7142 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
7395c052 7143 }
809c7548 7144
5d08a275 7145 error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group);
2b0b0b80
JR
7146 if (error) {
7147 return error;
7395c052 7148 }
2b0b0b80 7149 new_group = ogm->new_group;
7395c052 7150
fce4730c 7151 /* Manipulate bucket list for bucket commands */
2b0b0b80
JR
7152 if (ogm->gm.command == OFPGC15_INSERT_BUCKET) {
7153 error = copy_buckets_for_insert_bucket(old_group, new_group,
7154 ogm->gm.command_bucket_id);
7155 } else if (ogm->gm.command == OFPGC15_REMOVE_BUCKET) {
7156 error = copy_buckets_for_remove_bucket(old_group, new_group,
7157 ogm->gm.command_bucket_id);
fce4730c
SH
7158 }
7159 if (error) {
7160 goto out;
7161 }
7162
809c7548 7163 /* The group creation time does not change during modification. */
2b0b0b80
JR
7164 *CONST_CAST(long long int *, &(new_group->created)) = old_group->created;
7165 *CONST_CAST(long long int *, &(new_group->modified)) = time_msec();
7395c052 7166
2b0b0b80 7167 group_collection_add(&ogm->old_groups, old_group);
db88b35c 7168
5d08a275
JR
7169 /* Mark the old group for deletion. */
7170 versions_set_remove_version(&old_group->versions, ogm->version);
7171 /* Insert replacement group. */
7172 cmap_insert(&ofproto->groups, &new_group->cmap_node,
7173 hash_int(new_group->group_id, 0));
cc099268 7174 /* Transfer rules. */
2b0b0b80 7175 rule_collection_move(&new_group->rules, &old_group->rules);
db88b35c 7176
2b0b0b80
JR
7177 if (old_group->type != new_group->type) {
7178 ofproto->n_groups[old_group->type]--;
7179 ofproto->n_groups[new_group->type]++;
7395c052 7180 }
2b0b0b80 7181 return 0;
7395c052 7182
809c7548 7183out:
2b0b0b80 7184 ofproto_group_unref(new_group);
7395c052
NZ
7185 return error;
7186}
7187
88b87a36
JS
7188/* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not
7189 * exist yet and modifies it otherwise */
7190static enum ofperr
2b0b0b80
JR
7191add_or_modify_group_start(struct ofproto *ofproto,
7192 struct ofproto_group_mod *ogm)
cc099268 7193 OVS_REQUIRES(ofproto_mutex)
88b87a36 7194{
88b87a36 7195 enum ofperr error;
88b87a36 7196
2b0b0b80
JR
7197 if (!ofproto_group_exists(ofproto, ogm->gm.group_id)) {
7198 error = add_group_start(ofproto, ogm);
88b87a36 7199 } else {
2b0b0b80 7200 error = modify_group_start(ofproto, ogm);
88b87a36 7201 }
cc099268 7202
88b87a36
JS
7203 return error;
7204}
7205
7395c052 7206static void
5d08a275
JR
7207delete_group_start(struct ofproto *ofproto, ovs_version_t version,
7208 struct group_collection *groups, struct ofgroup *group)
cc099268 7209 OVS_REQUIRES(ofproto_mutex)
7395c052 7210{
2b0b0b80 7211 /* Makes flow deletion code leave the rule pointers in 'group->rules'
cc099268
JR
7212 * intact, so that we can later refer to the rules deleted due to the group
7213 * deletion. Rule pointers will be removed from all other groups, if any,
7214 * so we will never try to delete the same rule twice. */
2b0b0b80
JR
7215 group->being_deleted = true;
7216
7217 /* Mark all the referring groups for deletion. */
5d08a275 7218 delete_flows_start__(ofproto, version, &group->rules);
2b0b0b80 7219 group_collection_add(groups, group);
5d08a275 7220 versions_set_remove_version(&group->versions, version);
2b0b0b80
JR
7221 ofproto->n_groups[group->type]--;
7222}
276f6518 7223
2b0b0b80
JR
7224static void
7225delete_group_finish(struct ofproto *ofproto, struct ofgroup *group)
7226 OVS_REQUIRES(ofproto_mutex)
7227{
7228 /* Finish deletion of all flow entries containing this group in a group
7229 * action. */
7230 delete_flows_finish__(ofproto, &group->rules, OFPRR_GROUP_DELETE, NULL);
276f6518 7231
5d08a275 7232 /* Group removal is postponed by the caller. */
7395c052
NZ
7233}
7234
6c5b90ce 7235/* Implements OFPGC11_DELETE. */
7395c052 7236static void
2b0b0b80 7237delete_groups_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 7238 OVS_REQUIRES(ofproto_mutex)
7395c052 7239{
2b0b0b80 7240 struct ofgroup *group;
7395c052 7241
2b0b0b80
JR
7242 if (ogm->gm.group_id == OFPG_ALL) {
7243 CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) {
5d08a275
JR
7244 if (versions_visible_in_version(&group->versions, ogm->version)) {
7245 delete_group_start(ofproto, ogm->version, &ogm->old_groups,
7246 group);
7247 }
7395c052
NZ
7248 }
7249 } else {
5d08a275
JR
7250 group = ofproto_group_lookup__(ofproto, ogm->gm.group_id, ogm->version);
7251 if (group) {
7252 delete_group_start(ofproto, ogm->version, &ogm->old_groups, group);
7395c052
NZ
7253 }
7254 }
7395c052
NZ
7255}
7256
7257static enum ofperr
2b0b0b80
JR
7258ofproto_group_mod_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
7259 OVS_REQUIRES(ofproto_mutex)
7395c052 7260{
7395c052
NZ
7261 enum ofperr error;
7262
2b0b0b80
JR
7263 ogm->new_group = NULL;
7264 group_collection_init(&ogm->old_groups);
7395c052 7265
2b0b0b80 7266 switch (ogm->gm.command) {
7395c052 7267 case OFPGC11_ADD:
2b0b0b80 7268 error = add_group_start(ofproto, ogm);
3c35db62 7269 break;
7395c052
NZ
7270
7271 case OFPGC11_MODIFY:
2b0b0b80 7272 error = modify_group_start(ofproto, ogm);
3c35db62 7273 break;
7395c052 7274
88b87a36 7275 case OFPGC11_ADD_OR_MOD:
2b0b0b80 7276 error = add_or_modify_group_start(ofproto, ogm);
88b87a36
JS
7277 break;
7278
7395c052 7279 case OFPGC11_DELETE:
2b0b0b80 7280 delete_groups_start(ofproto, ogm);
3c35db62
NR
7281 error = 0;
7282 break;
7395c052 7283
fce4730c 7284 case OFPGC15_INSERT_BUCKET:
2b0b0b80 7285 error = modify_group_start(ofproto, ogm);
3c35db62 7286 break;
fce4730c
SH
7287
7288 case OFPGC15_REMOVE_BUCKET:
2b0b0b80 7289 error = modify_group_start(ofproto, ogm);
3c35db62 7290 break;
fce4730c 7291
7395c052 7292 default:
2b0b0b80 7293 if (ogm->gm.command > OFPGC11_DELETE) {
d2873d53 7294 VLOG_INFO_RL(&rl, "%s: Invalid group_mod command type %d",
2b0b0b80 7295 ofproto->name, ogm->gm.command);
7395c052 7296 }
63eded98 7297 error = OFPERR_OFPGMFC_BAD_COMMAND;
2b0b0b80 7298 break;
7395c052 7299 }
2b0b0b80
JR
7300 return error;
7301}
3c35db62 7302
25070e04
JR
7303static void
7304ofproto_group_mod_revert(struct ofproto *ofproto,
7305 struct ofproto_group_mod *ogm)
7306 OVS_REQUIRES(ofproto_mutex)
7307{
7308 struct ofgroup *new_group = ogm->new_group;
7309 struct ofgroup *old_group;
7310
7311 /* Restore replaced or deleted groups. */
7312 GROUP_COLLECTION_FOR_EACH (old_group, &ogm->old_groups) {
7313 ofproto->n_groups[old_group->type]++;
7314 if (new_group) {
7315 ovs_assert(group_collection_n(&ogm->old_groups) == 1);
7316 /* Transfer rules back. */
7317 rule_collection_move(&old_group->rules, &new_group->rules);
7318 } else {
7319 old_group->being_deleted = false;
7320 /* Revert rule deletion. */
7321 delete_flows_revert__(ofproto, &old_group->rules);
7322 }
7323 /* Restore visibility. */
7324 versions_set_remove_version(&old_group->versions,
7325 OVS_VERSION_NOT_REMOVED);
7326 }
7327 if (new_group) {
7328 /* Remove the new group immediately. It was never visible to
7329 * lookups. */
7330 cmap_remove(&ofproto->groups, &new_group->cmap_node,
7331 hash_int(new_group->group_id, 0));
7332 ofproto->n_groups[new_group->type]--;
7333 ofproto_group_unref(new_group);
7334 }
7335}
7336
2b0b0b80
JR
7337static void
7338ofproto_group_mod_finish(struct ofproto *ofproto,
7339 struct ofproto_group_mod *ogm,
7340 const struct openflow_mod_requester *req)
7341 OVS_REQUIRES(ofproto_mutex)
7342{
7343 struct ofgroup *new_group = ogm->new_group;
7344 struct ofgroup *old_group;
7345
ccb3bc08
JR
7346 if (new_group && group_collection_n(&ogm->old_groups) &&
7347 ofproto->ofproto_class->group_modify) {
2b0b0b80
JR
7348 /* Modify a group. */
7349 ovs_assert(group_collection_n(&ogm->old_groups) == 1);
2b0b0b80
JR
7350
7351 /* XXX: OK to lose old group's stats? */
7352 ofproto->ofproto_class->group_modify(new_group);
5d08a275 7353 }
2b0b0b80 7354
5d08a275
JR
7355 /* Delete old groups. */
7356 GROUP_COLLECTION_FOR_EACH(old_group, &ogm->old_groups) {
7357 delete_group_finish(ofproto, old_group);
2b0b0b80 7358 }
5d08a275 7359 remove_groups_postponed(&ogm->old_groups);
2b0b0b80
JR
7360
7361 if (req) {
3c35db62 7362 struct ofputil_requestforward rf;
2b0b0b80 7363 rf.xid = req->request->xid;
3c35db62 7364 rf.reason = OFPRFR_GROUP_MOD;
2b0b0b80
JR
7365 rf.group_mod = &ogm->gm;
7366 connmgr_send_requestforward(ofproto->connmgr, req->ofconn, &rf);
7367 }
7368}
7369
7370/* Delete all groups from 'ofproto'.
7371 *
7372 * This is intended for use within an ofproto provider's 'destruct'
7373 * function. */
d3b84833
BP
7374static void
7375ofproto_group_delete_all__(struct ofproto *ofproto)
7376 OVS_REQUIRES(ofproto_mutex)
2b0b0b80
JR
7377{
7378 struct ofproto_group_mod ogm;
2b0b0b80
JR
7379 ogm.gm.command = OFPGC11_DELETE;
7380 ogm.gm.group_id = OFPG_ALL;
5d08a275 7381 ogm.version = ofproto->tables_version + 1;
d3b84833 7382
2b0b0b80 7383 ofproto_group_mod_start(ofproto, &ogm);
5d08a275 7384 ofproto_bump_tables_version(ofproto);
2b0b0b80 7385 ofproto_group_mod_finish(ofproto, &ogm, NULL);
d3b84833
BP
7386}
7387
7388/* Delete all groups from 'ofproto'.
7389 *
7390 * This is intended for use within an ofproto provider's 'destruct'
7391 * function. */
7392void
7393ofproto_group_delete_all(struct ofproto *ofproto)
7394 OVS_EXCLUDED(ofproto_mutex)
7395{
7396 ovs_mutex_lock(&ofproto_mutex);
7397 ofproto_group_delete_all__(ofproto);
2b0b0b80
JR
7398 ovs_mutex_unlock(&ofproto_mutex);
7399}
7400
7401static enum ofperr
7402handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
7403 OVS_EXCLUDED(ofproto_mutex)
7404{
7405 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7406 struct ofproto_group_mod ogm;
7407 enum ofperr error;
7408
7409 error = reject_slave_controller(ofconn);
7410 if (error) {
7411 return error;
7412 }
7413
7414 error = ofputil_decode_group_mod(oh, &ogm.gm);
7415 if (error) {
7416 return error;
3c35db62 7417 }
2b0b0b80
JR
7418
7419 ovs_mutex_lock(&ofproto_mutex);
5d08a275 7420 ogm.version = ofproto->tables_version + 1;
2b0b0b80
JR
7421 error = ofproto_group_mod_start(ofproto, &ogm);
7422 if (!error) {
7423 struct openflow_mod_requester req = { ofconn, oh };
5d08a275
JR
7424
7425 ofproto_bump_tables_version(ofproto);
2b0b0b80 7426 ofproto_group_mod_finish(ofproto, &ogm, &req);
2c7ee524 7427 ofmonitor_flush(ofproto->connmgr);
2b0b0b80 7428 }
2b0b0b80
JR
7429 ovs_mutex_unlock(&ofproto_mutex);
7430
75868d0e 7431 ofputil_uninit_group_mod(&ogm.gm);
63eded98 7432
3c35db62 7433 return error;
7395c052
NZ
7434}
7435
3c1bb396
BP
7436enum ofputil_table_miss
7437ofproto_table_get_miss_config(const struct ofproto *ofproto, uint8_t table_id)
6d328fa2 7438{
82c22d34
BP
7439 enum ofputil_table_miss miss;
7440
7441 atomic_read_relaxed(&ofproto->tables[table_id].miss_config, &miss);
7442 return miss;
7443}
7444
7445static void
7446table_mod__(struct oftable *oftable,
de7d3c07 7447 const struct ofputil_table_mod *tm)
82c22d34 7448{
de7d3c07 7449 if (tm->miss == OFPUTIL_TABLE_MISS_DEFAULT) {
82c22d34
BP
7450 /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any
7451 * table-miss configuration (because the protocol used doesn't have
7452 * such a concept), so there's nothing to do. */
7453 } else {
de7d3c07 7454 atomic_store_relaxed(&oftable->miss_config, tm->miss);
82c22d34
BP
7455 }
7456
7457 unsigned int new_eviction = oftable->eviction;
de7d3c07 7458 if (tm->eviction == OFPUTIL_TABLE_EVICTION_ON) {
82c22d34 7459 new_eviction |= EVICTION_OPENFLOW;
de7d3c07 7460 } else if (tm->eviction == OFPUTIL_TABLE_EVICTION_OFF) {
82c22d34
BP
7461 new_eviction &= ~EVICTION_OPENFLOW;
7462 }
afcea9ea 7463
82c22d34
BP
7464 if (new_eviction != oftable->eviction) {
7465 ovs_mutex_lock(&ofproto_mutex);
7466 oftable_configure_eviction(oftable, new_eviction,
7467 oftable->eviction_fields,
7468 oftable->n_eviction_fields);
7469 ovs_mutex_unlock(&ofproto_mutex);
7470 }
de7d3c07
SJ
7471
7472 if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) {
7473 ovs_mutex_lock(&ofproto_mutex);
de7d3c07
SJ
7474 oftable->vacancy_down = tm->table_vacancy.vacancy_down;
7475 oftable->vacancy_up = tm->table_vacancy.vacancy_up;
6c6eedc5
SJ
7476 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_OFF) {
7477 oftable->vacancy_event = 0;
7478 } else if (!oftable->vacancy_event) {
7479 uint8_t vacancy = oftable_vacancy(oftable);
7480 oftable->vacancy_event = (vacancy < oftable->vacancy_up
7481 ? OFPTR_VACANCY_UP
7482 : OFPTR_VACANCY_DOWN);
7483 }
de7d3c07
SJ
7484 ovs_mutex_unlock(&ofproto_mutex);
7485 }
6d328fa2
SH
7486}
7487
67761761
SH
7488static enum ofperr
7489table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
7490{
3c1bb396 7491 if (!check_table_id(ofproto, tm->table_id)) {
67761761 7492 return OFPERR_OFPTMFC_BAD_TABLE;
82c22d34
BP
7493 }
7494
7495 /* Don't allow the eviction flags to be changed (except to the only fixed
7496 * value that OVS supports). OF1.4 says this is normal: "The
7497 * OFPTMPT_EVICTION property usually cannot be modified using a
7498 * OFP_TABLE_MOD request, because the eviction mechanism is switch
7499 * defined". */
7500 if (tm->eviction_flags != UINT32_MAX
7501 && tm->eviction_flags != OFPROTO_EVICTION_FLAGS) {
7502 return OFPERR_OFPTMFC_BAD_CONFIG;
7503 }
7504
7505 if (tm->table_id == OFPTT_ALL) {
7506 struct oftable *oftable;
7507 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
7508 if (!(oftable->flags & (OFTABLE_HIDDEN | OFTABLE_READONLY))) {
de7d3c07 7509 table_mod__(oftable, tm);
3c1bb396 7510 }
3c1bb396 7511 }
82c22d34
BP
7512 } else {
7513 struct oftable *oftable = &ofproto->tables[tm->table_id];
7514 if (oftable->flags & OFTABLE_READONLY) {
7515 return OFPERR_OFPTMFC_EPERM;
7516 }
de7d3c07 7517 table_mod__(oftable, tm);
67761761 7518 }
82c22d34 7519
67761761
SH
7520 return 0;
7521}
7522
918f2b82
AZ
7523static enum ofperr
7524handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
7525{
67761761 7526 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
918f2b82
AZ
7527 struct ofputil_table_mod tm;
7528 enum ofperr error;
7529
7530 error = reject_slave_controller(ofconn);
7531 if (error) {
7532 return error;
7533 }
7534
7535 error = ofputil_decode_table_mod(oh, &tm);
7536 if (error) {
7537 return error;
7538 }
7539
67761761 7540 return table_mod(ofproto, &tm);
918f2b82
AZ
7541}
7542
5bacd5cd
JR
7543/* Free resources that may be allocated by ofproto_flow_mod_init(). */
7544void
7545ofproto_flow_mod_uninit(struct ofproto_flow_mod *ofm)
7546{
7547 if (ofm->temp_rule) {
7548 ofproto_rule_unref(ofm->temp_rule);
7549 ofm->temp_rule = NULL;
7550 }
7551 if (ofm->criteria.version != OVS_VERSION_NOT_REMOVED) {
7552 rule_criteria_destroy(&ofm->criteria);
7553 }
7554 if (ofm->conjs) {
7555 free(ofm->conjs);
7556 ofm->conjs = NULL;
7557 ofm->n_conjs = 0;
7558 }
7559}
7560
777af88d 7561static enum ofperr
5bacd5cd 7562ofproto_flow_mod_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
2c7ee524 7563 const struct ofputil_flow_mod *fm, struct rule *rule)
5bacd5cd 7564 OVS_EXCLUDED(ofproto_mutex)
777af88d 7565{
5bacd5cd
JR
7566 enum ofperr error;
7567
7568 /* Forward flow mod fields we need later. */
7569 ofm->command = fm->command;
5bacd5cd
JR
7570 ofm->modify_cookie = fm->modify_cookie;
7571
7572 ofm->modify_may_add_flow = (fm->new_cookie != OVS_BE64_MAX
7573 && fm->cookie_mask == htonll(0));
8b6987d7
JR
7574 /* Old flags must be kept when modifying a flow, but we still must
7575 * honor the reset counts flag if present in the flow mod. */
7576 ofm->modify_keep_counts = !(fm->flags & OFPUTIL_FF_RESET_COUNTS);
5bacd5cd
JR
7577
7578 /* Initialize state needed by ofproto_flow_mod_uninit(). */
2c7ee524 7579 ofm->temp_rule = rule;
5bacd5cd
JR
7580 ofm->criteria.version = OVS_VERSION_NOT_REMOVED;
7581 ofm->conjs = NULL;
7582 ofm->n_conjs = 0;
7583
c184807c
JR
7584 bool check_buffer_id = false;
7585
5bacd5cd
JR
7586 switch (ofm->command) {
7587 case OFPFC_ADD:
c184807c 7588 check_buffer_id = true;
5bacd5cd
JR
7589 error = add_flow_init(ofproto, ofm, fm);
7590 break;
7591 case OFPFC_MODIFY:
c184807c 7592 check_buffer_id = true;
5bacd5cd
JR
7593 error = modify_flows_init_loose(ofproto, ofm, fm);
7594 break;
7595 case OFPFC_MODIFY_STRICT:
c184807c 7596 check_buffer_id = true;
5bacd5cd
JR
7597 error = modify_flow_init_strict(ofproto, ofm, fm);
7598 break;
7599 case OFPFC_DELETE:
7600 error = delete_flows_init_loose(ofproto, ofm, fm);
7601 break;
7602 case OFPFC_DELETE_STRICT:
7603 error = delete_flows_init_strict(ofproto, ofm, fm);
7604 break;
7605 default:
7606 error = OFPERR_OFPFMFC_BAD_COMMAND;
7607 break;
7608 }
c184807c
JR
7609 if (!error && check_buffer_id && fm->buffer_id != UINT32_MAX) {
7610 error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
7611 }
7612
cc099268 7613 if (error) {
5bacd5cd 7614 ofproto_flow_mod_uninit(ofm);
cc099268 7615 }
5bacd5cd
JR
7616 return error;
7617}
cc099268 7618
5bacd5cd
JR
7619static enum ofperr
7620ofproto_flow_mod_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
7621 OVS_REQUIRES(ofproto_mutex)
7622{
7623 enum ofperr error;
81dee635 7624
5bacd5cd
JR
7625 rule_collection_init(&ofm->old_rules);
7626 rule_collection_init(&ofm->new_rules);
81dee635 7627
5a27991d 7628 switch (ofm->command) {
1f42be1c 7629 case OFPFC_ADD:
5bacd5cd
JR
7630 error = add_flow_start(ofproto, ofm);
7631 break;
1f42be1c 7632 case OFPFC_MODIFY:
5bacd5cd
JR
7633 error = modify_flows_start_loose(ofproto, ofm);
7634 break;
1f42be1c 7635 case OFPFC_MODIFY_STRICT:
5bacd5cd
JR
7636 error = modify_flow_start_strict(ofproto, ofm);
7637 break;
1f42be1c 7638 case OFPFC_DELETE:
5bacd5cd
JR
7639 error = delete_flows_start_loose(ofproto, ofm);
7640 break;
1f42be1c 7641 case OFPFC_DELETE_STRICT:
5bacd5cd
JR
7642 error = delete_flow_start_strict(ofproto, ofm);
7643 break;
7644 default:
7645 OVS_NOT_REACHED();
1f42be1c 7646 }
5bacd5cd
JR
7647 /* Release resources not needed after start. */
7648 ofproto_flow_mod_uninit(ofm);
1f42be1c 7649
5bacd5cd
JR
7650 if (error) {
7651 rule_collection_destroy(&ofm->old_rules);
7652 rule_collection_destroy(&ofm->new_rules);
7653 }
7654 return error;
1f42be1c
JR
7655}
7656
7657static void
8be00367 7658ofproto_flow_mod_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
7659 OVS_REQUIRES(ofproto_mutex)
7660{
5a27991d 7661 switch (ofm->command) {
1f42be1c 7662 case OFPFC_ADD:
8be00367 7663 add_flow_revert(ofproto, ofm);
1f42be1c
JR
7664 break;
7665
7666 case OFPFC_MODIFY:
7667 case OFPFC_MODIFY_STRICT:
8be00367 7668 modify_flows_revert(ofproto, ofm);
1f42be1c
JR
7669 break;
7670
7671 case OFPFC_DELETE:
7672 case OFPFC_DELETE_STRICT:
8be00367 7673 delete_flows_revert(ofproto, ofm);
1f42be1c
JR
7674 break;
7675
7676 default:
7677 break;
7678 }
2c7ee524 7679
5bacd5cd
JR
7680 rule_collection_destroy(&ofm->old_rules);
7681 rule_collection_destroy(&ofm->new_rules);
1f42be1c
JR
7682}
7683
7684static void
2c7ee524 7685ofproto_flow_mod_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 7686 const struct openflow_mod_requester *req)
1f42be1c
JR
7687 OVS_REQUIRES(ofproto_mutex)
7688{
5a27991d 7689 switch (ofm->command) {
1f42be1c 7690 case OFPFC_ADD:
8be00367 7691 add_flow_finish(ofproto, ofm, req);
1f42be1c
JR
7692 break;
7693
7694 case OFPFC_MODIFY:
7695 case OFPFC_MODIFY_STRICT:
8be00367 7696 modify_flows_finish(ofproto, ofm, req);
1f42be1c
JR
7697 break;
7698
7699 case OFPFC_DELETE:
7700 case OFPFC_DELETE_STRICT:
8be00367 7701 delete_flows_finish(ofproto, ofm, req);
1f42be1c
JR
7702 break;
7703
7704 default:
7705 break;
7706 }
b0d38b2f 7707
5bacd5cd
JR
7708 rule_collection_destroy(&ofm->old_rules);
7709 rule_collection_destroy(&ofm->new_rules);
7710
b0d38b2f 7711 if (req) {
5a27991d 7712 ofconn_report_flow_mod(req->ofconn, ofm->command);
b0d38b2f 7713 }
1f42be1c
JR
7714}
7715
39c94593
JR
7716/* Commit phases (all while locking ofproto_mutex):
7717 *
7718 * 1. Begin: Gather resources and make changes visible in the next version.
7719 * - Mark affected rules for removal in the next version.
7720 * - Create new replacement rules, make visible in the next
7721 * version.
7722 * - Do not send any events or notifications.
7723 *
7724 * 2. Revert: Fail if any errors are found. After this point no errors are
7725 * possible. No visible changes were made, so rollback is minimal (remove
7726 * added invisible rules, restore visibility of rules marked for removal).
7727 *
1c38055d
JR
7728 * 3. Finish: Make the changes visible for lookups. Insert replacement rules to
7729 * the ofproto provider. Remove replaced and deleted rules from ofproto data
7730 * structures, and Schedule postponed removal of deleted rules from the
7731 * classifier. Send notifications, buffered packets, etc.
39c94593 7732 */
1f42be1c
JR
7733static enum ofperr
7734do_bundle_commit(struct ofconn *ofconn, uint32_t id, uint16_t flags)
7735{
7736 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
44e0c35d 7737 ovs_version_t version = ofproto->tables_version + 1;
1f42be1c
JR
7738 struct ofp_bundle *bundle;
7739 struct ofp_bundle_entry *be;
777af88d 7740 enum ofperr error;
1f42be1c
JR
7741
7742 bundle = ofconn_get_bundle(ofconn, id);
7743
7744 if (!bundle) {
7745 return OFPERR_OFPBFC_BAD_ID;
7746 }
7747 if (bundle->flags != flags) {
7748 error = OFPERR_OFPBFC_BAD_FLAGS;
7749 } else {
1c38055d
JR
7750 bool prev_is_port_mod = false;
7751
1f42be1c
JR
7752 error = 0;
7753 ovs_mutex_lock(&ofproto_mutex);
39c94593
JR
7754
7755 /* 1. Begin. */
1f42be1c
JR
7756 LIST_FOR_EACH (be, node, &bundle->msg_list) {
7757 if (be->type == OFPTYPE_PORT_MOD) {
1c38055d
JR
7758 /* Our port mods are not atomic. */
7759 if (flags & OFPBF_ATOMIC) {
7760 error = OFPERR_OFPBFC_MSG_FAILED;
7761 } else {
7762 prev_is_port_mod = true;
8be00367 7763 error = port_mod_start(ofconn, &be->opm.pm, &be->opm.port);
1c38055d 7764 }
25070e04
JR
7765 } else {
7766 /* Flow & group mods between port mods are applied as a single
7767 * version, but the versions are published only after we know
7768 * the commit is successful. */
1c38055d 7769 if (prev_is_port_mod) {
25070e04 7770 prev_is_port_mod = false;
8be00367 7771 ++version;
1c38055d 7772 }
25070e04
JR
7773 if (be->type == OFPTYPE_FLOW_MOD) {
7774 /* Store the version in which the changes should take
7775 * effect. */
7776 be->ofm.version = version;
7777 error = ofproto_flow_mod_start(ofproto, &be->ofm);
7778 } else if (be->type == OFPTYPE_GROUP_MOD) {
7779 /* Store the version in which the changes should take
7780 * effect. */
7781 be->ogm.version = version;
7782 error = ofproto_group_mod_start(ofproto, &be->ogm);
6dd3c787
JR
7783 } else if (be->type == OFPTYPE_PACKET_OUT) {
7784 be->opo.version = version;
7785 error = ofproto_packet_out_start(ofproto, &be->opo);
25070e04
JR
7786 } else {
7787 OVS_NOT_REACHED();
7788 }
1f42be1c
JR
7789 }
7790 if (error) {
7791 break;
7792 }
7793 }
1c38055d 7794
1f42be1c
JR
7795 if (error) {
7796 /* Send error referring to the original message. */
7797 if (error) {
52c57cbb 7798 ofconn_send_error(ofconn, be->msg, error);
1f42be1c
JR
7799 error = OFPERR_OFPBFC_MSG_FAILED;
7800 }
7801
39c94593 7802 /* 2. Revert. Undo all the changes made above. */
1f42be1c
JR
7803 LIST_FOR_EACH_REVERSE_CONTINUE(be, node, &bundle->msg_list) {
7804 if (be->type == OFPTYPE_FLOW_MOD) {
8be00367 7805 ofproto_flow_mod_revert(ofproto, &be->ofm);
25070e04
JR
7806 } else if (be->type == OFPTYPE_GROUP_MOD) {
7807 ofproto_group_mod_revert(ofproto, &be->ogm);
6dd3c787
JR
7808 } else if (be->type == OFPTYPE_PACKET_OUT) {
7809 ofproto_packet_out_revert(ofproto, &be->opo);
1f42be1c 7810 }
1c38055d 7811 /* Nothing needs to be reverted for a port mod. */
1f42be1c
JR
7812 }
7813 } else {
39c94593 7814 /* 4. Finish. */
1f42be1c 7815 LIST_FOR_EACH (be, node, &bundle->msg_list) {
25070e04 7816 if (be->type == OFPTYPE_PORT_MOD) {
1c38055d
JR
7817 /* Perform the actual port mod. This is not atomic, i.e.,
7818 * the effects will be immediately seen by upcall
7819 * processing regardless of the lookup version. It should
7820 * be noted that port configuration changes can originate
7821 * also from OVSDB changes asynchronously to all upcall
7822 * processing. */
8be00367 7823 port_mod_finish(ofconn, &be->opm.pm, be->opm.port);
25070e04 7824 } else {
6dd3c787
JR
7825 version =
7826 (be->type == OFPTYPE_FLOW_MOD) ? be->ofm.version :
7827 (be->type == OFPTYPE_GROUP_MOD) ? be->ogm.version :
7828 (be->type == OFPTYPE_PACKET_OUT) ? be->opo.version :
7829 version;
7830
7831 /* Bump the lookup version to the one of the current
7832 * message. This makes all the changes in the bundle at
7833 * this version visible to lookups at once. */
7834 if (ofproto->tables_version < version) {
7835 ofproto->tables_version = version;
7836 ofproto->ofproto_class->set_tables_version(
7837 ofproto, ofproto->tables_version);
7838 }
7839
52c57cbb 7840 struct openflow_mod_requester req = { ofconn, be->msg };
25070e04 7841
6dd3c787 7842 if (be->type == OFPTYPE_FLOW_MOD) {
25070e04
JR
7843 ofproto_flow_mod_finish(ofproto, &be->ofm, &req);
7844 } else if (be->type == OFPTYPE_GROUP_MOD) {
25070e04 7845 ofproto_group_mod_finish(ofproto, &be->ogm, &req);
6dd3c787
JR
7846 } else if (be->type == OFPTYPE_PACKET_OUT) {
7847 ofproto_packet_out_finish(ofproto, &be->opo);
25070e04 7848 }
1f42be1c
JR
7849 }
7850 }
7851 }
1c38055d 7852
1f42be1c
JR
7853 ofmonitor_flush(ofproto->connmgr);
7854 ovs_mutex_unlock(&ofproto_mutex);
1f42be1c
JR
7855 }
7856
7857 /* The bundle is discarded regardless the outcome. */
0c78eebe 7858 ofp_bundle_remove__(ofconn, bundle);
1f42be1c
JR
7859 return error;
7860}
7861
7862static enum ofperr
7863handle_bundle_control(struct ofconn *ofconn, const struct ofp_header *oh)
7864{
777af88d 7865 struct ofputil_bundle_ctrl_msg bctrl;
777af88d 7866 struct ofputil_bundle_ctrl_msg reply;
1f42be1c
JR
7867 struct ofpbuf *buf;
7868 enum ofperr error;
777af88d 7869
ba9d374a
JR
7870 error = reject_slave_controller(ofconn);
7871 if (error) {
7872 return error;
7873 }
7874
777af88d
AC
7875 error = ofputil_decode_bundle_ctrl(oh, &bctrl);
7876 if (error) {
7877 return error;
7878 }
7879 reply.flags = 0;
7880 reply.bundle_id = bctrl.bundle_id;
7881
7882 switch (bctrl.type) {
51bb26fa
JR
7883 case OFPBCT_OPEN_REQUEST:
7884 error = ofp_bundle_open(ofconn, bctrl.bundle_id, bctrl.flags, oh);
777af88d
AC
7885 reply.type = OFPBCT_OPEN_REPLY;
7886 break;
7887 case OFPBCT_CLOSE_REQUEST:
7888 error = ofp_bundle_close(ofconn, bctrl.bundle_id, bctrl.flags);
ea53e3a8 7889 reply.type = OFPBCT_CLOSE_REPLY;
777af88d
AC
7890 break;
7891 case OFPBCT_COMMIT_REQUEST:
1f42be1c 7892 error = do_bundle_commit(ofconn, bctrl.bundle_id, bctrl.flags);
777af88d
AC
7893 reply.type = OFPBCT_COMMIT_REPLY;
7894 break;
7895 case OFPBCT_DISCARD_REQUEST:
7896 error = ofp_bundle_discard(ofconn, bctrl.bundle_id);
7897 reply.type = OFPBCT_DISCARD_REPLY;
7898 break;
7899
7900 case OFPBCT_OPEN_REPLY:
7901 case OFPBCT_CLOSE_REPLY:
7902 case OFPBCT_COMMIT_REPLY:
7903 case OFPBCT_DISCARD_REPLY:
7904 return OFPERR_OFPBFC_BAD_TYPE;
7905 break;
7906 }
7907
7908 if (!error) {
7909 buf = ofputil_encode_bundle_ctrl_reply(oh, &reply);
7910 ofconn_send_reply(ofconn, buf);
7911 }
7912 return error;
7913}
7914
777af88d
AC
7915static enum ofperr
7916handle_bundle_add(struct ofconn *ofconn, const struct ofp_header *oh)
5bacd5cd 7917 OVS_EXCLUDED(ofproto_mutex)
777af88d 7918{
7ac27a04 7919 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
777af88d
AC
7920 enum ofperr error;
7921 struct ofputil_bundle_add_msg badd;
7ac27a04
JR
7922 struct ofp_bundle_entry *bmsg;
7923 enum ofptype type;
777af88d 7924
ba9d374a
JR
7925 error = reject_slave_controller(ofconn);
7926 if (error) {
7927 return error;
7928 }
7929
7ac27a04 7930 error = ofputil_decode_bundle_add(oh, &badd, &type);
777af88d
AC
7931 if (error) {
7932 return error;
7933 }
7934
1f42be1c 7935 bmsg = ofp_bundle_entry_alloc(type, badd.msg);
7ac27a04 7936
6dd3c787
JR
7937 struct ofpbuf ofpacts;
7938 uint64_t ofpacts_stub[1024 / 8];
7939 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
7940
7ac27a04 7941 if (type == OFPTYPE_PORT_MOD) {
8be00367 7942 error = ofputil_decode_port_mod(badd.msg, &bmsg->opm.pm, false);
7ac27a04 7943 } else if (type == OFPTYPE_FLOW_MOD) {
5bacd5cd 7944 struct ofputil_flow_mod fm;
7ac27a04 7945
5bacd5cd 7946 error = ofputil_decode_flow_mod(&fm, badd.msg,
7ac27a04 7947 ofconn_get_protocol(ofconn),
8d8ab6c2 7948 ofproto_get_tun_tab(ofproto),
04f48a68 7949 &ofproto->vl_mff_map, &ofpacts,
7ac27a04
JR
7950 u16_to_ofp(ofproto->max_ports),
7951 ofproto->n_tables);
5bacd5cd 7952 if (!error) {
2c7ee524 7953 error = ofproto_flow_mod_init(ofproto, &bmsg->ofm, &fm, NULL);
5bacd5cd 7954 }
25070e04
JR
7955 } else if (type == OFPTYPE_GROUP_MOD) {
7956 error = ofputil_decode_group_mod(badd.msg, &bmsg->ogm.gm);
6dd3c787
JR
7957 } else if (type == OFPTYPE_PACKET_OUT) {
7958 struct ofputil_packet_out po;
7959
7960 COVERAGE_INC(ofproto_packet_out);
7961
7962 /* Decode message. */
89d7927b
YHW
7963 error = ofputil_decode_packet_out(&po, badd.msg,
7964 ofproto_get_tun_tab(ofproto),
7965 &ofpacts);
6dd3c787
JR
7966 if (!error) {
7967 po.ofpacts = ofpbuf_steal_data(&ofpacts); /* Move to heap. */
7968 error = ofproto_packet_out_init(ofproto, ofconn, &bmsg->opo, &po);
7969 }
7ac27a04
JR
7970 } else {
7971 OVS_NOT_REACHED();
7972 }
7973
6dd3c787
JR
7974 ofpbuf_uninit(&ofpacts);
7975
7ac27a04
JR
7976 if (!error) {
7977 error = ofp_bundle_add_message(ofconn, badd.bundle_id, badd.flags,
51bb26fa 7978 bmsg, oh);
7ac27a04
JR
7979 }
7980
7981 if (error) {
7982 ofp_bundle_entry_free(bmsg);
7983 }
7984
7985 return error;
777af88d
AC
7986}
7987
6159c531 7988static enum ofperr
4e548ad9 7989handle_tlv_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
6159c531 7990{
8d8ab6c2
JG
7991 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7992 struct tun_table *old_tab, *new_tab;
4e548ad9 7993 struct ofputil_tlv_table_mod ttm;
6159c531
JG
7994 enum ofperr error;
7995
7996 error = reject_slave_controller(ofconn);
7997 if (error) {
7998 return error;
7999 }
8000
4e548ad9 8001 error = ofputil_decode_tlv_table_mod(oh, &ttm);
6159c531
JG
8002 if (error) {
8003 return error;
8004 }
8005
8d8ab6c2
JG
8006 old_tab = ovsrcu_get_protected(struct tun_table *, &ofproto->metadata_tab);
8007 error = tun_metadata_table_mod(&ttm, old_tab, &new_tab);
8008 if (!error) {
04f48a68
YHW
8009 ovs_mutex_lock(&ofproto->vl_mff_map.mutex);
8010 error = mf_vl_mff_map_mod_from_tun_metadata(&ofproto->vl_mff_map,
8011 &ttm);
8012 ovs_mutex_unlock(&ofproto->vl_mff_map.mutex);
5c7c16d8
YHW
8013 if (!error) {
8014 ovsrcu_set(&ofproto->metadata_tab, new_tab);
8015 tun_metadata_postpone_free(old_tab);
bc36c3a8
YHW
8016 } else {
8017 tun_metadata_free(new_tab);
5c7c16d8 8018 }
8d8ab6c2 8019 }
9558d2a5 8020
4e548ad9 8021 ofputil_uninit_tlv_table(&ttm.mappings);
9558d2a5 8022 return error;
6159c531
JG
8023}
8024
8025static enum ofperr
4e548ad9 8026handle_tlv_table_request(struct ofconn *ofconn, const struct ofp_header *oh)
6159c531 8027{
8d8ab6c2 8028 const struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4e548ad9 8029 struct ofputil_tlv_table_reply ttr;
9558d2a5
JG
8030 struct ofpbuf *b;
8031
8d8ab6c2
JG
8032 tun_metadata_table_request(ofproto_get_tun_tab(ofproto), &ttr);
8033
4e548ad9
ML
8034 b = ofputil_encode_tlv_table_reply(oh, &ttr);
8035 ofputil_uninit_tlv_table(&ttr.mappings);
9558d2a5
JG
8036
8037 ofconn_send_reply(ofconn, b);
8038 return 0;
6159c531
JG
8039}
8040
90bf1e07 8041static enum ofperr
d1e2cf21 8042handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
15aaf599 8043 OVS_EXCLUDED(ofproto_mutex)
064af421 8044{
6fd6ed71 8045 const struct ofp_header *oh = msg->data;
982697a4 8046 enum ofptype type;
90bf1e07 8047 enum ofperr error;
064af421 8048
982697a4 8049 error = ofptype_decode(&type, oh);
d1e2cf21
BP
8050 if (error) {
8051 return error;
8052 }
ff3c2c63
YT
8053 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
8054 && ofpmp_more(oh)) {
8055 /* We have no buffer implementation for multipart requests.
8056 * Report overflow for requests which consists of multiple
8057 * messages. */
8058 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
8059 }
064af421 8060
982697a4 8061 switch (type) {
d1e2cf21 8062 /* OpenFlow requests. */
982697a4 8063 case OFPTYPE_ECHO_REQUEST:
d1e2cf21 8064 return handle_echo_request(ofconn, oh);
064af421 8065
982697a4 8066 case OFPTYPE_FEATURES_REQUEST:
d1e2cf21 8067 return handle_features_request(ofconn, oh);
064af421 8068
982697a4 8069 case OFPTYPE_GET_CONFIG_REQUEST:
d1e2cf21 8070 return handle_get_config_request(ofconn, oh);
064af421 8071
982697a4
BP
8072 case OFPTYPE_SET_CONFIG:
8073 return handle_set_config(ofconn, oh);
064af421 8074
982697a4
BP
8075 case OFPTYPE_PACKET_OUT:
8076 return handle_packet_out(ofconn, oh);
064af421 8077
982697a4 8078 case OFPTYPE_PORT_MOD:
d1e2cf21 8079 return handle_port_mod(ofconn, oh);
064af421 8080
982697a4 8081 case OFPTYPE_FLOW_MOD:
2e4f5fcf 8082 return handle_flow_mod(ofconn, oh);
064af421 8083
7395c052
NZ
8084 case OFPTYPE_GROUP_MOD:
8085 return handle_group_mod(ofconn, oh);
8086
918f2b82
AZ
8087 case OFPTYPE_TABLE_MOD:
8088 return handle_table_mod(ofconn, oh);
8089
9cae45dc
JR
8090 case OFPTYPE_METER_MOD:
8091 return handle_meter_mod(ofconn, oh);
8092
982697a4 8093 case OFPTYPE_BARRIER_REQUEST:
d1e2cf21 8094 return handle_barrier_request(ofconn, oh);
064af421 8095
6ea4776b
JR
8096 case OFPTYPE_ROLE_REQUEST:
8097 return handle_role_request(ofconn, oh);
8098
d1e2cf21 8099 /* OpenFlow replies. */
982697a4 8100 case OFPTYPE_ECHO_REPLY:
d1e2cf21 8101 return 0;
246e61ea 8102
d1e2cf21 8103 /* Nicira extension requests. */
982697a4 8104 case OFPTYPE_FLOW_MOD_TABLE_ID:
6c1491fb 8105 return handle_nxt_flow_mod_table_id(ofconn, oh);
d1e2cf21 8106
982697a4 8107 case OFPTYPE_SET_FLOW_FORMAT:
d1e2cf21
BP
8108 return handle_nxt_set_flow_format(ofconn, oh);
8109
982697a4 8110 case OFPTYPE_SET_PACKET_IN_FORMAT:
54834960
EJ
8111 return handle_nxt_set_packet_in_format(ofconn, oh);
8112
982697a4 8113 case OFPTYPE_SET_CONTROLLER_ID:
a7349929
BP
8114 return handle_nxt_set_controller_id(ofconn, oh);
8115
982697a4 8116 case OFPTYPE_FLOW_AGE:
f27f2134
BP
8117 /* Nothing to do. */
8118 return 0;
8119
982697a4 8120 case OFPTYPE_FLOW_MONITOR_CANCEL:
2b07c8b1
BP
8121 return handle_flow_monitor_cancel(ofconn, oh);
8122
982697a4 8123 case OFPTYPE_SET_ASYNC_CONFIG:
80d5aefd
BP
8124 return handle_nxt_set_async_config(ofconn, oh);
8125
c423b3b3
AC
8126 case OFPTYPE_GET_ASYNC_REQUEST:
8127 return handle_nxt_get_async_request(ofconn, oh);
8128
77ab5fd2
BP
8129 case OFPTYPE_NXT_RESUME:
8130 return handle_nxt_resume(ofconn, oh);
8131
349adfb2 8132 /* Statistics requests. */
982697a4
BP
8133 case OFPTYPE_DESC_STATS_REQUEST:
8134 return handle_desc_stats_request(ofconn, oh);
8135
8136 case OFPTYPE_FLOW_STATS_REQUEST:
8137 return handle_flow_stats_request(ofconn, oh);
8138
8139 case OFPTYPE_AGGREGATE_STATS_REQUEST:
8140 return handle_aggregate_stats_request(ofconn, oh);
8141
8142 case OFPTYPE_TABLE_STATS_REQUEST:
8143 return handle_table_stats_request(ofconn, oh);
8144
3c4e10fb
BP
8145 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
8146 return handle_table_features_request(ofconn, oh);
8147
03c72922
BP
8148 case OFPTYPE_TABLE_DESC_REQUEST:
8149 return handle_table_desc_request(ofconn, oh);
8150
982697a4
BP
8151 case OFPTYPE_PORT_STATS_REQUEST:
8152 return handle_port_stats_request(ofconn, oh);
8153
8154 case OFPTYPE_QUEUE_STATS_REQUEST:
8155 return handle_queue_stats_request(ofconn, oh);
8156
8157 case OFPTYPE_PORT_DESC_STATS_REQUEST:
8158 return handle_port_desc_stats_request(ofconn, oh);
8159
8160 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
8161 return handle_flow_monitor_request(ofconn, oh);
8162
261bd854
BP
8163 case OFPTYPE_METER_STATS_REQUEST:
8164 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9cae45dc
JR
8165 return handle_meter_request(ofconn, oh, type);
8166
261bd854 8167 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9cae45dc
JR
8168 return handle_meter_features_request(ofconn, oh);
8169
261bd854 8170 case OFPTYPE_GROUP_STATS_REQUEST:
7395c052
NZ
8171 return handle_group_stats_request(ofconn, oh);
8172
261bd854 8173 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
7395c052
NZ
8174 return handle_group_desc_stats_request(ofconn, oh);
8175
261bd854 8176 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
7395c052
NZ
8177 return handle_group_features_stats_request(ofconn, oh);
8178
7395c052 8179 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
e8f9a7bb 8180 return handle_queue_get_config_request(ofconn, oh);
2e1ae200 8181
777af88d
AC
8182 case OFPTYPE_BUNDLE_CONTROL:
8183 return handle_bundle_control(ofconn, oh);
8184
8185 case OFPTYPE_BUNDLE_ADD_MESSAGE:
8186 return handle_bundle_add(ofconn, oh);
8187
4e548ad9
ML
8188 case OFPTYPE_NXT_TLV_TABLE_MOD:
8189 return handle_tlv_table_mod(ofconn, oh);
6159c531 8190
4e548ad9
ML
8191 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
8192 return handle_tlv_table_request(ofconn, oh);
6159c531 8193
fb8f22c1
BY
8194 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
8195 return handle_ipfix_bridge_stats_request(ofconn, oh);
8196
8197 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
8198 return handle_ipfix_flow_stats_request(ofconn, oh);
8199
2a7c4805
JP
8200 case OFPTYPE_CT_FLUSH_ZONE:
8201 return handle_nxt_ct_flush_zone(ofconn, oh);
8202
982697a4
BP
8203 case OFPTYPE_HELLO:
8204 case OFPTYPE_ERROR:
8205 case OFPTYPE_FEATURES_REPLY:
8206 case OFPTYPE_GET_CONFIG_REPLY:
8207 case OFPTYPE_PACKET_IN:
8208 case OFPTYPE_FLOW_REMOVED:
8209 case OFPTYPE_PORT_STATUS:
8210 case OFPTYPE_BARRIER_REPLY:
c545d38d 8211 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
982697a4
BP
8212 case OFPTYPE_DESC_STATS_REPLY:
8213 case OFPTYPE_FLOW_STATS_REPLY:
8214 case OFPTYPE_QUEUE_STATS_REPLY:
8215 case OFPTYPE_PORT_STATS_REPLY:
8216 case OFPTYPE_TABLE_STATS_REPLY:
8217 case OFPTYPE_AGGREGATE_STATS_REPLY:
8218 case OFPTYPE_PORT_DESC_STATS_REPLY:
8219 case OFPTYPE_ROLE_REPLY:
8220 case OFPTYPE_FLOW_MONITOR_PAUSED:
8221 case OFPTYPE_FLOW_MONITOR_RESUMED:
8222 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2e1ae200 8223 case OFPTYPE_GET_ASYNC_REPLY:
261bd854
BP
8224 case OFPTYPE_GROUP_STATS_REPLY:
8225 case OFPTYPE_GROUP_DESC_STATS_REPLY:
8226 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
8227 case OFPTYPE_METER_STATS_REPLY:
8228 case OFPTYPE_METER_CONFIG_STATS_REPLY:
8229 case OFPTYPE_METER_FEATURES_STATS_REPLY:
8230 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
03c72922 8231 case OFPTYPE_TABLE_DESC_REPLY:
252f3411 8232 case OFPTYPE_ROLE_STATUS:
3c35db62 8233 case OFPTYPE_REQUESTFORWARD:
6c6eedc5 8234 case OFPTYPE_TABLE_STATUS:
4e548ad9 8235 case OFPTYPE_NXT_TLV_TABLE_REPLY:
fb8f22c1
BY
8236 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
8237 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
064af421 8238 default:
76ec08e0
YT
8239 if (ofpmsg_is_stat_request(oh)) {
8240 return OFPERR_OFPBRC_BAD_STAT;
8241 } else {
8242 return OFPERR_OFPBRC_BAD_TYPE;
8243 }
064af421 8244 }
d1e2cf21 8245}
064af421 8246
b20f4073 8247static void
e03248b7 8248handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
15aaf599 8249 OVS_EXCLUDED(ofproto_mutex)
d1e2cf21 8250{
d51c8b71
JR
8251 enum ofperr error = handle_openflow__(ofconn, ofp_msg);
8252
b20f4073 8253 if (error) {
6fd6ed71 8254 ofconn_send_error(ofconn, ofp_msg->data, error);
064af421 8255 }
d1e2cf21 8256 COVERAGE_INC(ofproto_recv_openflow);
7ee20df1
BP
8257}
8258\f
064af421 8259static uint64_t
fa60c019 8260pick_datapath_id(const struct ofproto *ofproto)
064af421 8261{
fa60c019 8262 const struct ofport *port;
064af421 8263
abe529af 8264 port = ofproto_get_port(ofproto, OFPP_LOCAL);
fa60c019 8265 if (port) {
74ff3298 8266 struct eth_addr ea;
fa60c019
BP
8267 int error;
8268
74ff3298 8269 error = netdev_get_etheraddr(port->netdev, &ea);
064af421
BP
8270 if (!error) {
8271 return eth_addr_to_uint64(ea);
8272 }
fbfa2911
BP
8273 VLOG_WARN("%s: could not get MAC address for %s (%s)",
8274 ofproto->name, netdev_get_name(port->netdev),
10a89ef0 8275 ovs_strerror(error));
064af421 8276 }
fa60c019 8277 return ofproto->fallback_dpid;
064af421
BP
8278}
8279
8280static uint64_t
8281pick_fallback_dpid(void)
8282{
74ff3298
JR
8283 struct eth_addr ea;
8284 eth_addr_nicira_random(&ea);
064af421
BP
8285 return eth_addr_to_uint64(ea);
8286}
8287\f
254750ce
BP
8288/* Table overflow policy. */
8289
ad3efdcb
EJ
8290/* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
8291 * to NULL if the table is not configured to evict rules or if the table
8292 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
8293 * or with no timeouts are not evictable.) */
8294static bool
8295choose_rule_to_evict(struct oftable *table, struct rule **rulep)
15aaf599 8296 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8297{
8298 struct eviction_group *evg;
8299
ad3efdcb 8300 *rulep = NULL;
82c22d34 8301 if (!table->eviction) {
ad3efdcb 8302 return false;
254750ce
BP
8303 }
8304
8305 /* In the common case, the outer and inner loops here will each be entered
8306 * exactly once:
8307 *
8308 * - The inner loop normally "return"s in its first iteration. If the
8309 * eviction group has any evictable rules, then it always returns in
8310 * some iteration.
8311 *
8312 * - The outer loop only iterates more than once if the largest eviction
8313 * group has no evictable rules.
8314 *
8315 * - The outer loop can exit only if table's 'max_flows' is all filled up
afe2143d 8316 * by unevictable rules. */
254750ce
BP
8317 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
8318 struct rule *rule;
8319
8320 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
15aaf599
BP
8321 *rulep = rule;
8322 return true;
254750ce
BP
8323 }
8324 }
8325
ad3efdcb 8326 return false;
254750ce 8327}
254750ce
BP
8328\f
8329/* Eviction groups. */
8330
8331/* Returns the priority to use for an eviction_group that contains 'n_rules'
8332 * rules. The priority contains low-order random bits to ensure that eviction
8333 * groups with the same number of rules are prioritized randomly. */
8334static uint32_t
8335eviction_group_priority(size_t n_rules)
8336{
8337 uint16_t size = MIN(UINT16_MAX, n_rules);
8338 return (size << 16) | random_uint16();
8339}
8340
8341/* Updates 'evg', an eviction_group within 'table', following a change that
8342 * adds or removes rules in 'evg'. */
8343static void
8344eviction_group_resized(struct oftable *table, struct eviction_group *evg)
15aaf599 8345 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8346{
8347 heap_change(&table->eviction_groups_by_size, &evg->size_node,
8348 eviction_group_priority(heap_count(&evg->rules)));
8349}
8350
8351/* Destroys 'evg', an eviction_group within 'table':
8352 *
8353 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
8354 * rules themselves, just removes them from the eviction group.)
8355 *
8356 * - Removes 'evg' from 'table'.
8357 *
8358 * - Frees 'evg'. */
8359static void
8360eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
15aaf599 8361 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8362{
8363 while (!heap_is_empty(&evg->rules)) {
8364 struct rule *rule;
8365
8366 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
8367 rule->eviction_group = NULL;
8368 }
8369 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
8370 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
8371 heap_destroy(&evg->rules);
8372 free(evg);
8373}
8374
8375/* Removes 'rule' from its eviction group, if any. */
8376static void
8377eviction_group_remove_rule(struct rule *rule)
15aaf599 8378 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8379{
8380 if (rule->eviction_group) {
8381 struct oftable *table = &rule->ofproto->tables[rule->table_id];
8382 struct eviction_group *evg = rule->eviction_group;
8383
8384 rule->eviction_group = NULL;
8385 heap_remove(&evg->rules, &rule->evg_node);
8386 if (heap_is_empty(&evg->rules)) {
8387 eviction_group_destroy(table, evg);
8388 } else {
8389 eviction_group_resized(table, evg);
8390 }
8391 }
8392}
8393
8394/* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
8395 * returns the hash value. */
8396static uint32_t
8397eviction_group_hash_rule(struct rule *rule)
15aaf599 8398 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8399{
8400 struct oftable *table = &rule->ofproto->tables[rule->table_id];
8401 const struct mf_subfield *sf;
5cb7a798 8402 struct flow flow;
254750ce
BP
8403 uint32_t hash;
8404
8405 hash = table->eviction_group_id_basis;
8fd47924 8406 miniflow_expand(rule->cr.match.flow, &flow);
254750ce
BP
8407 for (sf = table->eviction_fields;
8408 sf < &table->eviction_fields[table->n_eviction_fields];
8409 sf++)
8410 {
aff49b8c 8411 if (mf_are_prereqs_ok(sf->field, &flow, NULL)) {
254750ce
BP
8412 union mf_value value;
8413
5cb7a798 8414 mf_get_value(sf->field, &flow, &value);
254750ce
BP
8415 if (sf->ofs) {
8416 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
8417 }
8418 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
8419 unsigned int start = sf->ofs + sf->n_bits;
8420 bitwise_zero(&value, sf->field->n_bytes, start,
8421 sf->field->n_bytes * 8 - start);
8422 }
8423 hash = hash_bytes(&value, sf->field->n_bytes, hash);
8424 } else {
8425 hash = hash_int(hash, 0);
8426 }
8427 }
8428
8429 return hash;
8430}
8431
8432/* Returns an eviction group within 'table' with the given 'id', creating one
8433 * if necessary. */
8434static struct eviction_group *
8435eviction_group_find(struct oftable *table, uint32_t id)
15aaf599 8436 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8437{
8438 struct eviction_group *evg;
8439
8440 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
8441 return evg;
8442 }
8443
8444 evg = xmalloc(sizeof *evg);
8445 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
8446 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
8447 eviction_group_priority(0));
8448 heap_init(&evg->rules);
8449
8450 return evg;
8451}
8452
8453/* Returns an eviction priority for 'rule'. The return value should be
f70b94de
BP
8454 * interpreted so that higher priorities make a rule a more attractive
8455 * candidate for eviction. */
8456static uint64_t
dc437090 8457rule_eviction_priority(struct ofproto *ofproto, struct rule *rule)
15aaf599 8458 OVS_REQUIRES(ofproto_mutex)
254750ce 8459{
f70b94de
BP
8460 /* Calculate absolute time when this flow will expire. If it will never
8461 * expire, then return 0 to make it unevictable. */
dc437090 8462 long long int expiration = LLONG_MAX;
dc437090 8463 if (rule->hard_timeout) {
f70b94de
BP
8464 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
8465 ovs_mutex_lock(&rule->mutex);
8466 long long int modified = rule->modified;
8467 ovs_mutex_unlock(&rule->mutex);
8468
dc437090
JR
8469 expiration = modified + rule->hard_timeout * 1000;
8470 }
8471 if (rule->idle_timeout) {
8472 uint64_t packets, bytes;
8473 long long int used;
8474 long long int idle_expiration;
8475
8476 ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used);
8477 idle_expiration = used + rule->idle_timeout * 1000;
8478 expiration = MIN(expiration, idle_expiration);
8479 }
254750ce
BP
8480 if (expiration == LLONG_MAX) {
8481 return 0;
8482 }
8483
8484 /* Calculate the time of expiration as a number of (approximate) seconds
8485 * after program startup.
8486 *
8487 * This should work OK for program runs that last UINT32_MAX seconds or
8488 * less. Therefore, please restart OVS at least once every 136 years. */
f70b94de 8489 uint32_t expiration_ofs = (expiration >> 10) - (time_boot_msec() >> 10);
254750ce 8490
f70b94de
BP
8491 /* Combine expiration time with OpenFlow "importance" to form a single
8492 * priority value. We want flows with relatively low "importance" to be
8493 * evicted before even considering expiration time, so put "importance" in
8494 * the most significant bits and expiration time in the least significant
8495 * bits.
8496 *
8497 * Small 'priority' should be evicted before those with large 'priority'.
8498 * The caller expects the opposite convention (a large return value being
8499 * more attractive for eviction) so we invert it before returning. */
8500 uint64_t priority = ((uint64_t) rule->importance << 32) + expiration_ofs;
8501 return UINT64_MAX - priority;
254750ce
BP
8502}
8503
8504/* Adds 'rule' to an appropriate eviction group for its oftable's
8505 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
8506 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
8507 * own).
8508 *
8509 * The caller must ensure that 'rule' is not already in an eviction group. */
8510static void
8511eviction_group_add_rule(struct rule *rule)
15aaf599 8512 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
8513{
8514 struct ofproto *ofproto = rule->ofproto;
8515 struct oftable *table = &ofproto->tables[rule->table_id];
a3779dbc 8516 bool has_timeout;
254750ce 8517
dc437090
JR
8518 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
8519 * so no additional protection is needed. */
a3779dbc 8520 has_timeout = rule->hard_timeout || rule->idle_timeout;
a3779dbc 8521
82c22d34 8522 if (table->eviction && has_timeout) {
254750ce
BP
8523 struct eviction_group *evg;
8524
8525 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
8526
8527 rule->eviction_group = evg;
8528 heap_insert(&evg->rules, &rule->evg_node,
dc437090 8529 rule_eviction_priority(ofproto, rule));
254750ce
BP
8530 eviction_group_resized(table, evg);
8531 }
8532}
8533\f
d0918789
BP
8534/* oftables. */
8535
8536/* Initializes 'table'. */
8537static void
8538oftable_init(struct oftable *table)
8539{
5c67e4af 8540 memset(table, 0, sizeof *table);
d70e8c28 8541 classifier_init(&table->cls, flow_segment_u64s);
ec1c5c7e 8542 table->max_flows = UINT_MAX;
d79e3d70 8543 table->n_flows = 0;
82c22d34
BP
8544 hmap_init(&table->eviction_groups_by_id);
8545 heap_init(&table->eviction_groups_by_size);
3c1bb396 8546 atomic_init(&table->miss_config, OFPUTIL_TABLE_MISS_DEFAULT);
f017d986 8547
f017d986
JR
8548 classifier_set_prefix_fields(&table->cls, default_prefix_fields,
8549 ARRAY_SIZE(default_prefix_fields));
d611866c
SH
8550
8551 atomic_init(&table->n_matched, 0);
8552 atomic_init(&table->n_missed, 0);
d0918789
BP
8553}
8554
254750ce 8555/* Destroys 'table', including its classifier and eviction groups.
d0918789
BP
8556 *
8557 * The caller is responsible for freeing 'table' itself. */
8558static void
8559oftable_destroy(struct oftable *table)
8560{
cb22974d 8561 ovs_assert(classifier_is_empty(&table->cls));
82c22d34 8562
7394b8fb 8563 ovs_mutex_lock(&ofproto_mutex);
82c22d34 8564 oftable_configure_eviction(table, 0, NULL, 0);
7394b8fb 8565 ovs_mutex_unlock(&ofproto_mutex);
82c22d34
BP
8566
8567 hmap_destroy(&table->eviction_groups_by_id);
8568 heap_destroy(&table->eviction_groups_by_size);
d0918789 8569 classifier_destroy(&table->cls);
254750ce
BP
8570 free(table->name);
8571}
8572
8573/* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
8574 * string, then 'table' will use its default name.
8575 *
8576 * This only affects the name exposed for a table exposed through the OpenFlow
8577 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
8578static void
8579oftable_set_name(struct oftable *table, const char *name)
8580{
8581 if (name && name[0]) {
8582 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
8583 if (!table->name || strncmp(name, table->name, len)) {
8584 free(table->name);
8585 table->name = xmemdup0(name, len);
8586 }
8587 } else {
8588 free(table->name);
8589 table->name = NULL;
8590 }
8591}
8592
254750ce
BP
8593/* oftables support a choice of two policies when adding a rule would cause the
8594 * number of flows in the table to exceed the configured maximum number: either
8595 * they can refuse to add the new flow or they can evict some existing flow.
8596 * This function configures the latter policy on 'table', with fairness based
8597 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
8598 * 'n_fields' as 0 disables fairness.) */
8599static void
82c22d34
BP
8600oftable_configure_eviction(struct oftable *table, unsigned int eviction,
8601 const struct mf_subfield *fields, size_t n_fields)
15aaf599 8602 OVS_REQUIRES(ofproto_mutex)
254750ce 8603{
254750ce
BP
8604 struct rule *rule;
8605
82c22d34 8606 if ((table->eviction != 0) == (eviction != 0)
254750ce
BP
8607 && n_fields == table->n_eviction_fields
8608 && (!n_fields
8609 || !memcmp(fields, table->eviction_fields,
8610 n_fields * sizeof *fields))) {
82c22d34
BP
8611 /* The set of eviction fields did not change. If 'eviction' changed,
8612 * it remains nonzero, so that we can just update table->eviction
8613 * without fussing with the eviction groups. */
8614 table->eviction = eviction;
254750ce
BP
8615 return;
8616 }
8617
82c22d34
BP
8618 /* Destroy existing eviction groups, then destroy and recreate data
8619 * structures to recover memory. */
8620 struct eviction_group *evg, *next;
8621 HMAP_FOR_EACH_SAFE (evg, next, id_node, &table->eviction_groups_by_id) {
8622 eviction_group_destroy(table, evg);
8623 }
8624 hmap_destroy(&table->eviction_groups_by_id);
254750ce 8625 hmap_init(&table->eviction_groups_by_id);
82c22d34 8626 heap_destroy(&table->eviction_groups_by_size);
254750ce
BP
8627 heap_init(&table->eviction_groups_by_size);
8628
82c22d34
BP
8629 /* Replace eviction groups by the new ones, if there is a change. Free the
8630 * old fields only after allocating the new ones, because 'fields ==
8631 * table->eviction_fields' is possible. */
8632 struct mf_subfield *old_fields = table->eviction_fields;
8633 table->n_eviction_fields = n_fields;
8634 table->eviction_fields = (fields
8635 ? xmemdup(fields, n_fields * sizeof *fields)
8636 : NULL);
8637 free(old_fields);
8638
8639 /* Add the new eviction groups, if enabled. */
8640 table->eviction = eviction;
8641 if (table->eviction) {
8642 table->eviction_group_id_basis = random_uint32();
8643 CLS_FOR_EACH (rule, cr, &table->cls) {
8644 eviction_group_add_rule(rule);
8645 }
254750ce 8646 }
d0918789
BP
8647}
8648
bc5e6a91
JR
8649/* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted
8650 * it to the classifier. */
8651static void
8652ofproto_rule_insert__(struct ofproto *ofproto, struct rule *rule)
8653 OVS_REQUIRES(ofproto_mutex)
8654{
8655 const struct rule_actions *actions = rule_get_actions(rule);
8656
30d0452c
JR
8657 /* A rule may not be reinserted. */
8658 ovs_assert(rule->state == RULE_INITIALIZED);
39c94593 8659
bc5e6a91 8660 if (rule->hard_timeout || rule->idle_timeout) {
417e7e66 8661 ovs_list_insert(&ofproto->expirable, &rule->expirable);
bc5e6a91
JR
8662 }
8663 cookies_insert(ofproto, rule);
8664 eviction_group_add_rule(rule);
8665 if (actions->has_meter) {
8666 meter_insert_rule(rule);
8667 }
cc099268
JR
8668 if (actions->has_groups) {
8669 const struct ofpact_group *a;
8670 OFPACT_FOR_EACH_TYPE_FLATTENED (a, GROUP, actions->ofpacts,
8671 actions->ofpacts_len) {
8672 struct ofgroup *group;
8673
5d08a275
JR
8674 group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX,
8675 false);
cc099268
JR
8676 ovs_assert(group != NULL);
8677 group_add_rule(group, rule);
8678 }
8679 }
8680
30d0452c 8681 rule->state = RULE_INSERTED;
bc5e6a91
JR
8682}
8683
6787a49f
JR
8684/* Removes 'rule' from the ofproto data structures. Caller may have deferred
8685 * the removal from the classifier. */
d0918789 8686static void
802f84ff 8687ofproto_rule_remove__(struct ofproto *ofproto, struct rule *rule)
15aaf599 8688 OVS_REQUIRES(ofproto_mutex)
d0918789 8689{
30d0452c 8690 ovs_assert(rule->state == RULE_INSERTED);
39c94593 8691
98eaac36 8692 cookies_remove(ofproto, rule);
2c916028 8693
254750ce 8694 eviction_group_remove_rule(rule);
417e7e66
BW
8695 if (!ovs_list_is_empty(&rule->expirable)) {
8696 ovs_list_remove(&rule->expirable);
e503cc19 8697 }
417e7e66
BW
8698 if (!ovs_list_is_empty(&rule->meter_list_node)) {
8699 ovs_list_remove(&rule->meter_list_node);
8700 ovs_list_init(&rule->meter_list_node);
9cae45dc 8701 }
802f84ff 8702
cc099268
JR
8703 /* Remove the rule from any groups, except from the group that is being
8704 * deleted, if any. */
8705 const struct rule_actions *actions = rule_get_actions(rule);
8706
8707 if (actions->has_groups) {
8708 const struct ofpact_group *a;
8709
8483173b 8710 OFPACT_FOR_EACH_TYPE_FLATTENED(a, GROUP, actions->ofpacts,
cc099268
JR
8711 actions->ofpacts_len) {
8712 struct ofgroup *group;
8713
5d08a275
JR
8714 group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX,
8715 false);
cc099268
JR
8716 ovs_assert(group);
8717
8718 /* Leave the rule for the group that is being deleted, if any,
8719 * as we still need the list of rules for clean-up. */
8720 if (!group->being_deleted) {
8721 group_remove_rule(group, rule);
8722 }
8723 }
8724 }
8725
30d0452c 8726 rule->state = RULE_REMOVED;
0b4f2078 8727}
d0918789 8728\f
abe529af 8729/* unixctl commands. */
7aa697dd 8730
abe529af 8731struct ofproto *
2ac6bedd 8732ofproto_lookup(const char *name)
7aa697dd 8733{
2ac6bedd 8734 struct ofproto *ofproto;
7aa697dd 8735
2ac6bedd
BP
8736 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
8737 &all_ofprotos) {
8738 if (!strcmp(ofproto->name, name)) {
8739 return ofproto;
8740 }
7aa697dd 8741 }
2ac6bedd 8742 return NULL;
7aa697dd
BP
8743}
8744
8745static void
0e15264f
BP
8746ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
8747 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7aa697dd 8748{
7aa697dd 8749 struct ofproto *ofproto;
7aa697dd 8750 struct ds results;
7aa697dd 8751
7aa697dd 8752 ds_init(&results);
2ac6bedd
BP
8753 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
8754 ds_put_format(&results, "%s\n", ofproto->name);
7aa697dd 8755 }
bde9f75d 8756 unixctl_command_reply(conn, ds_cstr(&results));
7aa697dd 8757 ds_destroy(&results);
7aa697dd
BP
8758}
8759
8760static void
8761ofproto_unixctl_init(void)
8762{
8763 static bool registered;
8764 if (registered) {
8765 return;
8766 }
8767 registered = true;
8768
0e15264f
BP
8769 unixctl_command_register("ofproto/list", "", 0, 0,
8770 ofproto_unixctl_list, NULL);
064af421 8771}
f0fb825a
EG
8772
8773void
8774ofproto_set_vlan_limit(int vlan_limit)
8775{
8776 flow_limit_vlans(vlan_limit);
8777}