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