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