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