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