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