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